選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

229 行
7.2 KiB

  1. /* Copyright (c) 2017, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include "cavp_test_util.h"
  15. #include <openssl/bn.h>
  16. #include <openssl/digest.h>
  17. #include <openssl/ec.h>
  18. #include <openssl/nid.h>
  19. std::string EncodeHex(const uint8_t *in, size_t in_len) {
  20. static const char kHexDigits[] = "0123456789abcdef";
  21. std::string ret;
  22. ret.reserve(in_len * 2);
  23. for (size_t i = 0; i < in_len; i++) {
  24. ret += kHexDigits[in[i] >> 4];
  25. ret += kHexDigits[in[i] & 0xf];
  26. }
  27. return ret;
  28. }
  29. const EVP_CIPHER *GetCipher(const std::string &name) {
  30. if (name == "des-cbc") {
  31. return EVP_des_cbc();
  32. } else if (name == "des-ecb") {
  33. return EVP_des_ecb();
  34. } else if (name == "des-ede") {
  35. return EVP_des_ede();
  36. } else if (name == "des-ede3") {
  37. return EVP_des_ede3();
  38. } else if (name == "des-ede-cbc") {
  39. return EVP_des_ede_cbc();
  40. } else if (name == "des-ede3-cbc") {
  41. return EVP_des_ede3_cbc();
  42. } else if (name == "rc4") {
  43. return EVP_rc4();
  44. } else if (name == "aes-128-ecb") {
  45. return EVP_aes_128_ecb();
  46. } else if (name == "aes-256-ecb") {
  47. return EVP_aes_256_ecb();
  48. } else if (name == "aes-128-cbc") {
  49. return EVP_aes_128_cbc();
  50. } else if (name == "aes-128-gcm") {
  51. return EVP_aes_128_gcm();
  52. } else if (name == "aes-128-ofb") {
  53. return EVP_aes_128_ofb();
  54. } else if (name == "aes-192-cbc") {
  55. return EVP_aes_192_cbc();
  56. } else if (name == "aes-192-ctr") {
  57. return EVP_aes_192_ctr();
  58. } else if (name == "aes-192-ecb") {
  59. return EVP_aes_192_ecb();
  60. } else if (name == "aes-256-cbc") {
  61. return EVP_aes_256_cbc();
  62. } else if (name == "aes-128-ctr") {
  63. return EVP_aes_128_ctr();
  64. } else if (name == "aes-256-ctr") {
  65. return EVP_aes_256_ctr();
  66. } else if (name == "aes-256-gcm") {
  67. return EVP_aes_256_gcm();
  68. } else if (name == "aes-256-ofb") {
  69. return EVP_aes_256_ofb();
  70. }
  71. return nullptr;
  72. }
  73. bool CipherOperation(const EVP_CIPHER *cipher, std::vector<uint8_t> *out,
  74. bool encrypt, const std::vector<uint8_t> &key,
  75. const std::vector<uint8_t> &iv,
  76. const std::vector<uint8_t> &in) {
  77. bssl::ScopedEVP_CIPHER_CTX ctx;
  78. if (!EVP_CipherInit_ex(ctx.get(), cipher, nullptr, nullptr, nullptr,
  79. encrypt ? 1 : 0)) {
  80. return false;
  81. }
  82. if (!iv.empty() && iv.size() != EVP_CIPHER_CTX_iv_length(ctx.get())) {
  83. return false;
  84. }
  85. int result_len1 = 0, result_len2;
  86. *out = std::vector<uint8_t>(in.size());
  87. if (!EVP_CIPHER_CTX_set_key_length(ctx.get(), key.size()) ||
  88. !EVP_CipherInit_ex(ctx.get(), nullptr, nullptr, key.data(), iv.data(),
  89. -1) ||
  90. !EVP_CIPHER_CTX_set_padding(ctx.get(), 0) ||
  91. !EVP_CipherUpdate(ctx.get(), out->data(), &result_len1, in.data(),
  92. in.size()) ||
  93. !EVP_CipherFinal_ex(ctx.get(), out->data() + result_len1, &result_len2)) {
  94. return false;
  95. }
  96. out->resize(result_len1 + result_len2);
  97. return true;
  98. }
  99. bool AEADEncrypt(const EVP_AEAD *aead, std::vector<uint8_t> *ct,
  100. std::vector<uint8_t> *tag, size_t tag_len,
  101. const std::vector<uint8_t> &key,
  102. const std::vector<uint8_t> &pt,
  103. const std::vector<uint8_t> &aad, std::vector<uint8_t> *iv) {
  104. bssl::ScopedEVP_AEAD_CTX ctx;
  105. if (!EVP_AEAD_CTX_init_with_direction(ctx.get(), aead, key.data(), key.size(),
  106. tag->size(), evp_aead_seal)) {
  107. return false;
  108. }
  109. std::vector<uint8_t> out;
  110. out.resize(pt.size() + EVP_AEAD_max_overhead(aead));
  111. size_t out_len;
  112. if (!EVP_AEAD_CTX_seal(ctx.get(), out.data(), &out_len, out.size(),
  113. nullptr /* iv */, 0 /* iv_len */, pt.data(), pt.size(),
  114. aad.data(), aad.size())) {
  115. return false;
  116. }
  117. static const size_t iv_len = EVP_AEAD_nonce_length(aead);
  118. iv->assign(out.begin(), out.begin() + iv_len);
  119. ct->assign(out.begin() + iv_len, out.end() - tag_len);
  120. tag->assign(out.end() - tag_len, out.end());
  121. return true;
  122. }
  123. bool AEADDecrypt(const EVP_AEAD *aead, std::vector<uint8_t> *pt,
  124. std::vector<uint8_t> *aad, size_t pt_len, size_t aad_len,
  125. const std::vector<uint8_t> &key,
  126. const std::vector<uint8_t> &ct,
  127. const std::vector<uint8_t> &tag, std::vector<uint8_t> &iv) {
  128. bssl::ScopedEVP_AEAD_CTX ctx;
  129. if (!EVP_AEAD_CTX_init_with_direction(ctx.get(), aead, key.data(), key.size(),
  130. tag.size(), evp_aead_open)) {
  131. return false;
  132. }
  133. std::vector<uint8_t> in = iv;
  134. in.reserve(in.size() + ct.size() + tag.size());
  135. in.insert(in.end(), ct.begin(), ct.end());
  136. in.insert(in.end(), tag.begin(), tag.end());
  137. pt->resize(pt_len);
  138. aad->resize(aad_len);
  139. size_t out_pt_len;
  140. if (!EVP_AEAD_CTX_open(ctx.get(), pt->data(), &out_pt_len, pt->size(),
  141. nullptr /* iv */, 0 /* iv_len */, in.data(), in.size(),
  142. aad->data(), aad->size()) ||
  143. out_pt_len != pt_len) {
  144. return false;
  145. }
  146. return true;
  147. }
  148. static int HexToBIGNUM(bssl::UniquePtr<BIGNUM> *out, const char *in) {
  149. BIGNUM *raw = NULL;
  150. int ret = BN_hex2bn(&raw, in);
  151. out->reset(raw);
  152. return ret;
  153. }
  154. bssl::UniquePtr<BIGNUM> GetBIGNUM(FileTest *t, const char *attribute) {
  155. std::string hex;
  156. if (!t->GetAttribute(&hex, attribute)) {
  157. return nullptr;
  158. }
  159. bssl::UniquePtr<BIGNUM> ret;
  160. if (HexToBIGNUM(&ret, hex.c_str()) != static_cast<int>(hex.size())) {
  161. t->PrintLine("Could not decode '%s'.", hex.c_str());
  162. return nullptr;
  163. }
  164. return ret;
  165. }
  166. int GetECGroupNIDFromInstruction(FileTest *t, const char **out_str) {
  167. const char *dummy;
  168. if (out_str == nullptr) {
  169. out_str = &dummy;
  170. }
  171. if (t->HasInstruction("P-224")) {
  172. *out_str = "P-224";
  173. return NID_secp224r1;
  174. }
  175. if (t->HasInstruction("P-256")) {
  176. *out_str = "P-256";
  177. return NID_X9_62_prime256v1;
  178. }
  179. if (t->HasInstruction("P-384")) {
  180. *out_str = "P-384";
  181. return NID_secp384r1;
  182. }
  183. if (t->HasInstruction("P-521")) {
  184. *out_str = "P-521";
  185. return NID_secp521r1;
  186. }
  187. t->PrintLine("No supported group specified.");
  188. return NID_undef;
  189. }
  190. const EVP_MD *GetDigestFromInstruction(FileTest *t) {
  191. if (t->HasInstruction("SHA-1")) {
  192. return EVP_sha1();
  193. }
  194. if (t->HasInstruction("SHA-224")) {
  195. return EVP_sha224();
  196. }
  197. if (t->HasInstruction("SHA-256")) {
  198. return EVP_sha256();
  199. }
  200. if (t->HasInstruction("SHA-384")) {
  201. return EVP_sha384();
  202. }
  203. if (t->HasInstruction("SHA-512")) {
  204. return EVP_sha512();
  205. }
  206. t->PrintLine("No supported digest function specified.");
  207. return nullptr;
  208. }