25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ecdh_test.cc 10 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /* Copyright (c) 2016, 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 <stdio.h>
  15. #include <utility>
  16. #include <vector>
  17. #include <gtest/gtest.h>
  18. #include <openssl/bn.h>
  19. #include <openssl/bytestring.h>
  20. #include <openssl/crypto.h>
  21. #include <openssl/ec.h>
  22. #include <openssl/ec_key.h>
  23. #include <openssl/ecdh.h>
  24. #include <openssl/err.h>
  25. #include <openssl/evp.h>
  26. #include <openssl/nid.h>
  27. #include "../test/file_test.h"
  28. #include "../test/test_util.h"
  29. #include "../test/wycheproof_util.h"
  30. static bssl::UniquePtr<EC_GROUP> GetCurve(FileTest *t, const char *key) {
  31. std::string curve_name;
  32. if (!t->GetAttribute(&curve_name, key)) {
  33. return nullptr;
  34. }
  35. if (curve_name == "P-224") {
  36. return bssl::UniquePtr<EC_GROUP>(EC_GROUP_new_by_curve_name(NID_secp224r1));
  37. }
  38. if (curve_name == "P-256") {
  39. return bssl::UniquePtr<EC_GROUP>(EC_GROUP_new_by_curve_name(
  40. NID_X9_62_prime256v1));
  41. }
  42. if (curve_name == "P-384") {
  43. return bssl::UniquePtr<EC_GROUP>(EC_GROUP_new_by_curve_name(NID_secp384r1));
  44. }
  45. if (curve_name == "P-521") {
  46. return bssl::UniquePtr<EC_GROUP>(EC_GROUP_new_by_curve_name(NID_secp521r1));
  47. }
  48. t->PrintLine("Unknown curve '%s'", curve_name.c_str());
  49. return nullptr;
  50. }
  51. static bssl::UniquePtr<BIGNUM> GetBIGNUM(FileTest *t, const char *key) {
  52. std::vector<uint8_t> bytes;
  53. if (!t->GetBytes(&bytes, key)) {
  54. return nullptr;
  55. }
  56. return bssl::UniquePtr<BIGNUM>(BN_bin2bn(bytes.data(), bytes.size(), nullptr));
  57. }
  58. TEST(ECDHTest, TestVectors) {
  59. FileTestGTest("crypto/ecdh/ecdh_tests.txt", [](FileTest *t) {
  60. bssl::UniquePtr<EC_GROUP> group = GetCurve(t, "Curve");
  61. ASSERT_TRUE(group);
  62. bssl::UniquePtr<BIGNUM> priv_key = GetBIGNUM(t, "Private");
  63. ASSERT_TRUE(priv_key);
  64. bssl::UniquePtr<BIGNUM> x = GetBIGNUM(t, "X");
  65. ASSERT_TRUE(x);
  66. bssl::UniquePtr<BIGNUM> y = GetBIGNUM(t, "Y");
  67. ASSERT_TRUE(y);
  68. bssl::UniquePtr<BIGNUM> peer_x = GetBIGNUM(t, "PeerX");
  69. ASSERT_TRUE(peer_x);
  70. bssl::UniquePtr<BIGNUM> peer_y = GetBIGNUM(t, "PeerY");
  71. ASSERT_TRUE(peer_y);
  72. std::vector<uint8_t> z;
  73. ASSERT_TRUE(t->GetBytes(&z, "Z"));
  74. bssl::UniquePtr<EC_KEY> key(EC_KEY_new());
  75. ASSERT_TRUE(key);
  76. bssl::UniquePtr<EC_POINT> pub_key(EC_POINT_new(group.get()));
  77. ASSERT_TRUE(pub_key);
  78. bssl::UniquePtr<EC_POINT> peer_pub_key(EC_POINT_new(group.get()));
  79. ASSERT_TRUE(peer_pub_key);
  80. ASSERT_TRUE(EC_KEY_set_group(key.get(), group.get()));
  81. ASSERT_TRUE(EC_KEY_set_private_key(key.get(), priv_key.get()));
  82. ASSERT_TRUE(EC_POINT_set_affine_coordinates_GFp(group.get(), pub_key.get(),
  83. x.get(), y.get(), nullptr));
  84. ASSERT_TRUE(EC_POINT_set_affine_coordinates_GFp(
  85. group.get(), peer_pub_key.get(), peer_x.get(), peer_y.get(), nullptr));
  86. ASSERT_TRUE(EC_KEY_set_public_key(key.get(), pub_key.get()));
  87. ASSERT_TRUE(EC_KEY_check_key(key.get()));
  88. std::vector<uint8_t> actual_z;
  89. // Make |actual_z| larger than expected to ensure |ECDH_compute_key| returns
  90. // the right amount of data.
  91. actual_z.resize(z.size() + 1);
  92. int ret = ECDH_compute_key(actual_z.data(), actual_z.size(),
  93. peer_pub_key.get(), key.get(), nullptr);
  94. ASSERT_GE(ret, 0);
  95. EXPECT_EQ(Bytes(z), Bytes(actual_z.data(), static_cast<size_t>(ret)));
  96. // Test |ECDH_compute_key| truncates.
  97. actual_z.resize(z.size() - 1);
  98. ret = ECDH_compute_key(actual_z.data(), actual_z.size(), peer_pub_key.get(),
  99. key.get(), nullptr);
  100. ASSERT_GE(ret, 0);
  101. EXPECT_EQ(Bytes(z.data(), z.size() - 1),
  102. Bytes(actual_z.data(), static_cast<size_t>(ret)));
  103. });
  104. }
  105. TEST(ECDHTest, Wycheproof) {
  106. FileTestGTest("third_party/wycheproof_testvectors/ecdh_test.txt",
  107. [](FileTest *t) {
  108. t->IgnoreInstruction("curve"); // This is redundant with the per-test one.
  109. bssl::UniquePtr<EC_GROUP> group = GetWycheproofCurve(t, "curve", false);
  110. ASSERT_TRUE(group);
  111. bssl::UniquePtr<BIGNUM> priv_key = GetWycheproofBIGNUM(t, "private", false);
  112. ASSERT_TRUE(priv_key);
  113. std::vector<uint8_t> peer_spki;
  114. ASSERT_TRUE(t->GetBytes(&peer_spki, "public"));
  115. WycheproofResult result;
  116. ASSERT_TRUE(GetWycheproofResult(t, &result));
  117. std::vector<uint8_t> shared;
  118. ASSERT_TRUE(t->GetBytes(&shared, "shared"));
  119. // Wycheproof stores the peer key in an SPKI to mimic a Java API mistake.
  120. // This is non-standard and error-prone.
  121. CBS cbs;
  122. CBS_init(&cbs, peer_spki.data(), peer_spki.size());
  123. bssl::UniquePtr<EVP_PKEY> peer_evp(EVP_parse_public_key(&cbs));
  124. if (!peer_evp) {
  125. // Note some of Wycheproof's "acceptable" entries are unsupported by
  126. // BoringSSL because they test named curves (explicitly forbidden by RFC
  127. // 5480), while others are supported because they used compressed
  128. // coordinates. If the peer key fails to parse, we consider it to match
  129. // "acceptable", but if the resulting shared secret matches below, it too
  130. // matches "acceptable".
  131. //
  132. // TODO(davidben): Use the flags field to disambiguate these. Possibly
  133. // first get the Wycheproof folks to use flags more consistently.
  134. EXPECT_NE(WycheproofResult::kValid, result);
  135. return;
  136. }
  137. EC_KEY *peer_ec = EVP_PKEY_get0_EC_KEY(peer_evp.get());
  138. ASSERT_TRUE(peer_ec);
  139. bssl::UniquePtr<EC_KEY> key(EC_KEY_new());
  140. ASSERT_TRUE(key);
  141. ASSERT_TRUE(EC_KEY_set_group(key.get(), group.get()));
  142. ASSERT_TRUE(EC_KEY_set_private_key(key.get(), priv_key.get()));
  143. std::vector<uint8_t> actual((EC_GROUP_get_degree(group.get()) + 7) / 8);
  144. int ret =
  145. ECDH_compute_key(actual.data(), actual.size(),
  146. EC_KEY_get0_public_key(peer_ec), key.get(), nullptr);
  147. if (result == WycheproofResult::kInvalid) {
  148. EXPECT_EQ(-1, ret);
  149. } else {
  150. EXPECT_EQ(static_cast<int>(actual.size()), ret);
  151. EXPECT_EQ(Bytes(shared), Bytes(actual.data(), static_cast<size_t>(ret)));
  152. }
  153. });
  154. }
  155. // MakeCustomGroup returns an |EC_GROUP| containing a non-standard group. (P-256
  156. // with the wrong generator.)
  157. static bssl::UniquePtr<EC_GROUP> MakeCustomGroup() {
  158. static const uint8_t kP[] = {
  159. 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  160. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
  161. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  162. };
  163. static const uint8_t kA[] = {
  164. 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  165. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
  166. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc,
  167. };
  168. static const uint8_t kB[] = {
  169. 0x5a, 0xc6, 0x35, 0xd8, 0xaa, 0x3a, 0x93, 0xe7, 0xb3, 0xeb, 0xbd,
  170. 0x55, 0x76, 0x98, 0x86, 0xbc, 0x65, 0x1d, 0x06, 0xb0, 0xcc, 0x53,
  171. 0xb0, 0xf6, 0x3b, 0xce, 0x3c, 0x3e, 0x27, 0xd2, 0x60, 0x4b,
  172. };
  173. static const uint8_t kX[] = {
  174. 0xe6, 0x2b, 0x69, 0xe2, 0xbf, 0x65, 0x9f, 0x97, 0xbe, 0x2f, 0x1e,
  175. 0x0d, 0x94, 0x8a, 0x4c, 0xd5, 0x97, 0x6b, 0xb7, 0xa9, 0x1e, 0x0d,
  176. 0x46, 0xfb, 0xdd, 0xa9, 0xa9, 0x1e, 0x9d, 0xdc, 0xba, 0x5a,
  177. };
  178. static const uint8_t kY[] = {
  179. 0x01, 0xe7, 0xd6, 0x97, 0xa8, 0x0a, 0x18, 0xf9, 0xc3, 0xc4, 0xa3,
  180. 0x1e, 0x56, 0xe2, 0x7c, 0x83, 0x48, 0xdb, 0x16, 0x1a, 0x1c, 0xf5,
  181. 0x1d, 0x7e, 0xf1, 0x94, 0x2d, 0x4b, 0xcf, 0x72, 0x22, 0xc1,
  182. };
  183. static const uint8_t kOrder[] = {
  184. 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff,
  185. 0xff, 0xff, 0xff, 0xff, 0xff, 0xbc, 0xe6, 0xfa, 0xad, 0xa7, 0x17,
  186. 0x9e, 0x84, 0xf3, 0xb9, 0xca, 0xc2, 0xfc, 0x63, 0x25, 0x51,
  187. };
  188. bssl::UniquePtr<BN_CTX> ctx(BN_CTX_new());
  189. bssl::UniquePtr<BIGNUM> p(BN_bin2bn(kP, sizeof(kP), nullptr));
  190. bssl::UniquePtr<BIGNUM> a(BN_bin2bn(kA, sizeof(kA), nullptr));
  191. bssl::UniquePtr<BIGNUM> b(BN_bin2bn(kB, sizeof(kB), nullptr));
  192. bssl::UniquePtr<BIGNUM> x(BN_bin2bn(kX, sizeof(kX), nullptr));
  193. bssl::UniquePtr<BIGNUM> y(BN_bin2bn(kY, sizeof(kY), nullptr));
  194. bssl::UniquePtr<BIGNUM> order(BN_bin2bn(kOrder, sizeof(kOrder), nullptr));
  195. if (!ctx || !p || !a || !b || !x || !y || !order) {
  196. return nullptr;
  197. }
  198. bssl::UniquePtr<EC_GROUP> group(
  199. EC_GROUP_new_curve_GFp(p.get(), a.get(), b.get(), ctx.get()));
  200. if (!group) {
  201. return nullptr;
  202. }
  203. bssl::UniquePtr<EC_POINT> generator(EC_POINT_new(group.get()));
  204. if (!generator ||
  205. !EC_POINT_set_affine_coordinates_GFp(group.get(), generator.get(),
  206. x.get(), y.get(), ctx.get()) ||
  207. !EC_GROUP_set_generator(group.get(), generator.get(), order.get(),
  208. BN_value_one())) {
  209. return nullptr;
  210. }
  211. return group;
  212. }
  213. TEST(ECDHTest, GroupMismatch) {
  214. const size_t num_curves = EC_get_builtin_curves(nullptr, 0);
  215. std::vector<EC_builtin_curve> curves(num_curves);
  216. EC_get_builtin_curves(curves.data(), num_curves);
  217. // Instantiate all the built-in curves.
  218. std::vector<bssl::UniquePtr<EC_GROUP>> groups;
  219. for (const auto &curve : curves) {
  220. groups.emplace_back(EC_GROUP_new_by_curve_name(curve.nid));
  221. ASSERT_TRUE(groups.back());
  222. }
  223. // Also create some arbitrary group. (This is P-256 with the wrong generator.)
  224. groups.push_back(MakeCustomGroup());
  225. ASSERT_TRUE(groups.back());
  226. for (const auto &a : groups) {
  227. for (const auto &b : groups) {
  228. if (a.get() == b.get()) {
  229. continue;
  230. }
  231. bssl::UniquePtr<EC_KEY> key(EC_KEY_new());
  232. ASSERT_TRUE(EC_KEY_set_group(key.get(), a.get()));
  233. ASSERT_TRUE(EC_KEY_generate_key(key.get()));
  234. // ECDH across the groups should not work.
  235. char out[64];
  236. const EC_POINT *peer = EC_GROUP_get0_generator(b.get());
  237. EXPECT_EQ(-1,
  238. ECDH_compute_key(out, sizeof(out), peer, key.get(), nullptr));
  239. ERR_clear_error();
  240. }
  241. }
  242. }