Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

295 рядки
11 KiB

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