Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

537 wiersze
19 KiB

  1. /* Copyright (c) 2014, 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 <string.h>
  16. #include <vector>
  17. #include <openssl/bn.h>
  18. #include <openssl/bytestring.h>
  19. #include <openssl/crypto.h>
  20. #include <openssl/ec_key.h>
  21. #include <openssl/err.h>
  22. #include <openssl/mem.h>
  23. #include <openssl/nid.h>
  24. // kECKeyWithoutPublic is an ECPrivateKey with the optional publicKey field
  25. // omitted.
  26. static const uint8_t kECKeyWithoutPublic[] = {
  27. 0x30, 0x31, 0x02, 0x01, 0x01, 0x04, 0x20, 0xc6, 0xc1, 0xaa, 0xda, 0x15, 0xb0,
  28. 0x76, 0x61, 0xf8, 0x14, 0x2c, 0x6c, 0xaf, 0x0f, 0xdb, 0x24, 0x1a, 0xff, 0x2e,
  29. 0xfe, 0x46, 0xc0, 0x93, 0x8b, 0x74, 0xf2, 0xbc, 0xc5, 0x30, 0x52, 0xb0, 0x77,
  30. 0xa0, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  31. };
  32. // kECKeySpecifiedCurve is the above key with P-256's parameters explicitly
  33. // spelled out rather than using a named curve.
  34. static const uint8_t kECKeySpecifiedCurve[] = {
  35. 0x30, 0x82, 0x01, 0x22, 0x02, 0x01, 0x01, 0x04, 0x20, 0xc6, 0xc1, 0xaa,
  36. 0xda, 0x15, 0xb0, 0x76, 0x61, 0xf8, 0x14, 0x2c, 0x6c, 0xaf, 0x0f, 0xdb,
  37. 0x24, 0x1a, 0xff, 0x2e, 0xfe, 0x46, 0xc0, 0x93, 0x8b, 0x74, 0xf2, 0xbc,
  38. 0xc5, 0x30, 0x52, 0xb0, 0x77, 0xa0, 0x81, 0xfa, 0x30, 0x81, 0xf7, 0x02,
  39. 0x01, 0x01, 0x30, 0x2c, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01,
  40. 0x01, 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01,
  41. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  42. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  43. 0x30, 0x5b, 0x04, 0x20, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01,
  44. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  45. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc,
  46. 0x04, 0x20, 0x5a, 0xc6, 0x35, 0xd8, 0xaa, 0x3a, 0x93, 0xe7, 0xb3, 0xeb,
  47. 0xbd, 0x55, 0x76, 0x98, 0x86, 0xbc, 0x65, 0x1d, 0x06, 0xb0, 0xcc, 0x53,
  48. 0xb0, 0xf6, 0x3b, 0xce, 0x3c, 0x3e, 0x27, 0xd2, 0x60, 0x4b, 0x03, 0x15,
  49. 0x00, 0xc4, 0x9d, 0x36, 0x08, 0x86, 0xe7, 0x04, 0x93, 0x6a, 0x66, 0x78,
  50. 0xe1, 0x13, 0x9d, 0x26, 0xb7, 0x81, 0x9f, 0x7e, 0x90, 0x04, 0x41, 0x04,
  51. 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc, 0xe6, 0xe5,
  52. 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d, 0xeb, 0x33, 0xa0,
  53. 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96, 0x4f, 0xe3, 0x42, 0xe2,
  54. 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb, 0x4a, 0x7c, 0x0f, 0x9e, 0x16,
  55. 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31, 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68,
  56. 0x37, 0xbf, 0x51, 0xf5, 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00,
  57. 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xbc,
  58. 0xe6, 0xfa, 0xad, 0xa7, 0x17, 0x9e, 0x84, 0xf3, 0xb9, 0xca, 0xc2, 0xfc,
  59. 0x63, 0x25, 0x51, 0x02, 0x01, 0x01,
  60. };
  61. // kECKeyMissingZeros is an ECPrivateKey containing a degenerate P-256 key where
  62. // the private key is one. The private key is incorrectly encoded without zero
  63. // padding.
  64. static const uint8_t kECKeyMissingZeros[] = {
  65. 0x30, 0x58, 0x02, 0x01, 0x01, 0x04, 0x01, 0x01, 0xa0, 0x0a, 0x06, 0x08, 0x2a,
  66. 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0xa1, 0x44, 0x03, 0x42, 0x00, 0x04,
  67. 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc, 0xe6, 0xe5, 0x63,
  68. 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d, 0xeb, 0x33, 0xa0, 0xf4, 0xa1,
  69. 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96, 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f,
  70. 0x9b, 0x8e, 0xe7, 0xeb, 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57,
  71. 0x6b, 0x31, 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  72. };
  73. // kECKeyMissingZeros is an ECPrivateKey containing a degenerate P-256 key where
  74. // the private key is one. The private key is encoded with the required zero
  75. // padding.
  76. static const uint8_t kECKeyWithZeros[] = {
  77. 0x30, 0x77, 0x02, 0x01, 0x01, 0x04, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  78. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  79. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  80. 0xa0, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0xa1,
  81. 0x44, 0x03, 0x42, 0x00, 0x04, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47,
  82. 0xf8, 0xbc, 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  83. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96, 0x4f, 0xe3,
  84. 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb, 0x4a, 0x7c, 0x0f, 0x9e,
  85. 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31, 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68,
  86. 0x37, 0xbf, 0x51, 0xf5,
  87. };
  88. // DecodeECPrivateKey decodes |in| as an ECPrivateKey structure and returns the
  89. // result or nullptr on error.
  90. static bssl::UniquePtr<EC_KEY> DecodeECPrivateKey(const uint8_t *in,
  91. size_t in_len) {
  92. CBS cbs;
  93. CBS_init(&cbs, in, in_len);
  94. bssl::UniquePtr<EC_KEY> ret(EC_KEY_parse_private_key(&cbs, NULL));
  95. if (!ret || CBS_len(&cbs) != 0) {
  96. return nullptr;
  97. }
  98. return ret;
  99. }
  100. // EncodeECPrivateKey encodes |key| as an ECPrivateKey structure into |*out|. It
  101. // returns true on success or false on error.
  102. static bool EncodeECPrivateKey(std::vector<uint8_t> *out, const EC_KEY *key) {
  103. bssl::ScopedCBB cbb;
  104. uint8_t *der;
  105. size_t der_len;
  106. if (!CBB_init(cbb.get(), 0) ||
  107. !EC_KEY_marshal_private_key(cbb.get(), key, EC_KEY_get_enc_flags(key)) ||
  108. !CBB_finish(cbb.get(), &der, &der_len)) {
  109. return false;
  110. }
  111. out->assign(der, der + der_len);
  112. OPENSSL_free(der);
  113. return true;
  114. }
  115. static bool Testd2i_ECPrivateKey() {
  116. bssl::UniquePtr<EC_KEY> key = DecodeECPrivateKey(kECKeyWithoutPublic,
  117. sizeof(kECKeyWithoutPublic));
  118. if (!key) {
  119. fprintf(stderr, "Failed to parse private key.\n");
  120. ERR_print_errors_fp(stderr);
  121. return false;
  122. }
  123. std::vector<uint8_t> out;
  124. if (!EncodeECPrivateKey(&out, key.get())) {
  125. fprintf(stderr, "Failed to serialize private key.\n");
  126. ERR_print_errors_fp(stderr);
  127. return false;
  128. }
  129. if (std::vector<uint8_t>(kECKeyWithoutPublic,
  130. kECKeyWithoutPublic + sizeof(kECKeyWithoutPublic)) !=
  131. out) {
  132. fprintf(stderr, "Serialisation of key doesn't match original.\n");
  133. return false;
  134. }
  135. const EC_POINT *pub_key = EC_KEY_get0_public_key(key.get());
  136. if (pub_key == NULL) {
  137. fprintf(stderr, "Public key missing.\n");
  138. return false;
  139. }
  140. bssl::UniquePtr<BIGNUM> x(BN_new());
  141. bssl::UniquePtr<BIGNUM> y(BN_new());
  142. if (!x || !y) {
  143. return false;
  144. }
  145. if (!EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(key.get()),
  146. pub_key, x.get(), y.get(), NULL)) {
  147. fprintf(stderr, "Failed to get public key in affine coordinates.\n");
  148. return false;
  149. }
  150. bssl::UniquePtr<char> x_hex(BN_bn2hex(x.get()));
  151. bssl::UniquePtr<char> y_hex(BN_bn2hex(y.get()));
  152. if (!x_hex || !y_hex) {
  153. return false;
  154. }
  155. if (0 != strcmp(
  156. x_hex.get(),
  157. "c81561ecf2e54edefe6617db1c7a34a70744ddb261f269b83dacfcd2ade5a681") ||
  158. 0 != strcmp(
  159. y_hex.get(),
  160. "e0e2afa3f9b6abe4c698ef6495f1be49a3196c5056acb3763fe4507eec596e88")) {
  161. fprintf(stderr, "Incorrect public key: %s %s\n", x_hex.get(), y_hex.get());
  162. return false;
  163. }
  164. return true;
  165. }
  166. static bool TestZeroPadding() {
  167. // Check that the correct encoding round-trips.
  168. bssl::UniquePtr<EC_KEY> key = DecodeECPrivateKey(kECKeyWithZeros,
  169. sizeof(kECKeyWithZeros));
  170. std::vector<uint8_t> out;
  171. if (!key || !EncodeECPrivateKey(&out, key.get())) {
  172. ERR_print_errors_fp(stderr);
  173. return false;
  174. }
  175. if (std::vector<uint8_t>(kECKeyWithZeros,
  176. kECKeyWithZeros + sizeof(kECKeyWithZeros)) != out) {
  177. fprintf(stderr, "Serialisation of key was incorrect.\n");
  178. return false;
  179. }
  180. // Keys without leading zeros also parse, but they encode correctly.
  181. key = DecodeECPrivateKey(kECKeyMissingZeros, sizeof(kECKeyMissingZeros));
  182. if (!key || !EncodeECPrivateKey(&out, key.get())) {
  183. ERR_print_errors_fp(stderr);
  184. return false;
  185. }
  186. if (std::vector<uint8_t>(kECKeyWithZeros,
  187. kECKeyWithZeros + sizeof(kECKeyWithZeros)) != out) {
  188. fprintf(stderr, "Serialisation of key was incorrect.\n");
  189. return false;
  190. }
  191. return true;
  192. }
  193. static bool TestSpecifiedCurve() {
  194. // Test keys with specified curves may be decoded.
  195. bssl::UniquePtr<EC_KEY> key =
  196. DecodeECPrivateKey(kECKeySpecifiedCurve, sizeof(kECKeySpecifiedCurve));
  197. if (!key) {
  198. ERR_print_errors_fp(stderr);
  199. return false;
  200. }
  201. // The group should have been interpreted as P-256.
  202. if (EC_GROUP_get_curve_name(EC_KEY_get0_group(key.get())) !=
  203. NID_X9_62_prime256v1) {
  204. fprintf(stderr, "Curve name incorrect.\n");
  205. return false;
  206. }
  207. // Encoding the key should still use named form.
  208. std::vector<uint8_t> out;
  209. if (!EncodeECPrivateKey(&out, key.get())) {
  210. ERR_print_errors_fp(stderr);
  211. return false;
  212. }
  213. if (std::vector<uint8_t>(kECKeyWithoutPublic,
  214. kECKeyWithoutPublic + sizeof(kECKeyWithoutPublic)) !=
  215. out) {
  216. fprintf(stderr, "Serialisation of key was incorrect.\n");
  217. return false;
  218. }
  219. return true;
  220. }
  221. static bool TestSetAffine(const int nid) {
  222. bssl::UniquePtr<EC_KEY> key(EC_KEY_new_by_curve_name(nid));
  223. if (!key) {
  224. return false;
  225. }
  226. const EC_GROUP *const group = EC_KEY_get0_group(key.get());
  227. if (!EC_KEY_generate_key(key.get())) {
  228. fprintf(stderr, "EC_KEY_generate_key failed with nid %d\n", nid);
  229. ERR_print_errors_fp(stderr);
  230. return false;
  231. }
  232. if (!EC_POINT_is_on_curve(group, EC_KEY_get0_public_key(key.get()),
  233. nullptr)) {
  234. fprintf(stderr, "generated point is not on curve with nid %d", nid);
  235. ERR_print_errors_fp(stderr);
  236. return false;
  237. }
  238. bssl::UniquePtr<BIGNUM> x(BN_new());
  239. bssl::UniquePtr<BIGNUM> y(BN_new());
  240. if (!EC_POINT_get_affine_coordinates_GFp(group,
  241. EC_KEY_get0_public_key(key.get()),
  242. x.get(), y.get(), nullptr)) {
  243. fprintf(stderr, "EC_POINT_get_affine_coordinates_GFp failed with nid %d\n",
  244. nid);
  245. ERR_print_errors_fp(stderr);
  246. return false;
  247. }
  248. auto point = bssl::UniquePtr<EC_POINT>(EC_POINT_new(group));
  249. if (!point) {
  250. return false;
  251. }
  252. if (!EC_POINT_set_affine_coordinates_GFp(group, point.get(), x.get(), y.get(),
  253. nullptr)) {
  254. fprintf(stderr, "EC_POINT_set_affine_coordinates_GFp failed with nid %d\n",
  255. nid);
  256. ERR_print_errors_fp(stderr);
  257. return false;
  258. }
  259. // Subtract one from |y| to make the point no longer on the curve.
  260. if (!BN_sub(y.get(), y.get(), BN_value_one())) {
  261. return false;
  262. }
  263. bssl::UniquePtr<EC_POINT> invalid_point(EC_POINT_new(group));
  264. if (!invalid_point) {
  265. return false;
  266. }
  267. if (EC_POINT_set_affine_coordinates_GFp(group, invalid_point.get(), x.get(),
  268. y.get(), nullptr)) {
  269. fprintf(stderr,
  270. "EC_POINT_set_affine_coordinates_GFp succeeded with invalid "
  271. "coordinates with nid %d\n",
  272. nid);
  273. ERR_print_errors_fp(stderr);
  274. return false;
  275. }
  276. return true;
  277. }
  278. static bool TestArbitraryCurve() {
  279. // Make a P-256 key and extract the affine coordinates.
  280. bssl::UniquePtr<EC_KEY> key(EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  281. if (!key || !EC_KEY_generate_key(key.get())) {
  282. return false;
  283. }
  284. // Make an arbitrary curve which is identical to P-256.
  285. static const uint8_t kP[] = {
  286. 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  287. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
  288. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  289. };
  290. static const uint8_t kA[] = {
  291. 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  292. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
  293. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc,
  294. };
  295. static const uint8_t kB[] = {
  296. 0x5a, 0xc6, 0x35, 0xd8, 0xaa, 0x3a, 0x93, 0xe7, 0xb3, 0xeb, 0xbd,
  297. 0x55, 0x76, 0x98, 0x86, 0xbc, 0x65, 0x1d, 0x06, 0xb0, 0xcc, 0x53,
  298. 0xb0, 0xf6, 0x3b, 0xce, 0x3c, 0x3e, 0x27, 0xd2, 0x60, 0x4b,
  299. };
  300. static const uint8_t kX[] = {
  301. 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc, 0xe6,
  302. 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d, 0xeb,
  303. 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  304. };
  305. static const uint8_t kY[] = {
  306. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  307. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  308. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  309. };
  310. static const uint8_t kOrder[] = {
  311. 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff,
  312. 0xff, 0xff, 0xff, 0xff, 0xff, 0xbc, 0xe6, 0xfa, 0xad, 0xa7, 0x17,
  313. 0x9e, 0x84, 0xf3, 0xb9, 0xca, 0xc2, 0xfc, 0x63, 0x25, 0x51,
  314. };
  315. bssl::UniquePtr<BN_CTX> ctx(BN_CTX_new());
  316. bssl::UniquePtr<BIGNUM> p(BN_bin2bn(kP, sizeof(kP), nullptr));
  317. bssl::UniquePtr<BIGNUM> a(BN_bin2bn(kA, sizeof(kA), nullptr));
  318. bssl::UniquePtr<BIGNUM> b(BN_bin2bn(kB, sizeof(kB), nullptr));
  319. bssl::UniquePtr<BIGNUM> gx(BN_bin2bn(kX, sizeof(kX), nullptr));
  320. bssl::UniquePtr<BIGNUM> gy(BN_bin2bn(kY, sizeof(kY), nullptr));
  321. bssl::UniquePtr<BIGNUM> order(BN_bin2bn(kOrder, sizeof(kOrder), nullptr));
  322. bssl::UniquePtr<BIGNUM> cofactor(BN_new());
  323. if (!ctx || !p || !a || !b || !gx || !gy || !order || !cofactor ||
  324. !BN_set_word(cofactor.get(), 1)) {
  325. return false;
  326. }
  327. bssl::UniquePtr<EC_GROUP> group(
  328. EC_GROUP_new_curve_GFp(p.get(), a.get(), b.get(), ctx.get()));
  329. if (!group) {
  330. return false;
  331. }
  332. bssl::UniquePtr<EC_POINT> generator(EC_POINT_new(group.get()));
  333. if (!generator ||
  334. !EC_POINT_set_affine_coordinates_GFp(group.get(), generator.get(),
  335. gx.get(), gy.get(), ctx.get()) ||
  336. !EC_GROUP_set_generator(group.get(), generator.get(), order.get(),
  337. cofactor.get())) {
  338. return false;
  339. }
  340. // |group| should not have a curve name.
  341. if (EC_GROUP_get_curve_name(group.get()) != NID_undef) {
  342. return false;
  343. }
  344. // Copy |key| to |key2| using |group|.
  345. bssl::UniquePtr<EC_KEY> key2(EC_KEY_new());
  346. bssl::UniquePtr<EC_POINT> point(EC_POINT_new(group.get()));
  347. bssl::UniquePtr<BIGNUM> x(BN_new()), y(BN_new());
  348. if (!key2 || !point || !x || !y ||
  349. !EC_KEY_set_group(key2.get(), group.get()) ||
  350. !EC_KEY_set_private_key(key2.get(), EC_KEY_get0_private_key(key.get())) ||
  351. !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(key.get()),
  352. EC_KEY_get0_public_key(key.get()),
  353. x.get(), y.get(), nullptr) ||
  354. !EC_POINT_set_affine_coordinates_GFp(group.get(), point.get(), x.get(),
  355. y.get(), nullptr) ||
  356. !EC_KEY_set_public_key(key2.get(), point.get())) {
  357. fprintf(stderr, "Could not copy key.\n");
  358. return false;
  359. }
  360. // The key must be valid according to the new group too.
  361. if (!EC_KEY_check_key(key2.get())) {
  362. fprintf(stderr, "Copied key is not valid.\n");
  363. return false;
  364. }
  365. return true;
  366. }
  367. static bool TestAddingEqualPoints(int nid) {
  368. bssl::UniquePtr<EC_KEY> key(EC_KEY_new_by_curve_name(nid));
  369. if (!key) {
  370. return false;
  371. }
  372. const EC_GROUP *const group = EC_KEY_get0_group(key.get());
  373. if (!EC_KEY_generate_key(key.get())) {
  374. fprintf(stderr, "EC_KEY_generate_key failed with nid %d\n", nid);
  375. ERR_print_errors_fp(stderr);
  376. return false;
  377. }
  378. bssl::UniquePtr<EC_POINT> p1(EC_POINT_new(group));
  379. bssl::UniquePtr<EC_POINT> p2(EC_POINT_new(group));
  380. bssl::UniquePtr<EC_POINT> double_p1(EC_POINT_new(group));
  381. bssl::UniquePtr<EC_POINT> p1_plus_p2(EC_POINT_new(group));
  382. if (!p1 || !p2 || !double_p1 || !p1_plus_p2) {
  383. return false;
  384. }
  385. if (!EC_POINT_copy(p1.get(), EC_KEY_get0_public_key(key.get())) ||
  386. !EC_POINT_copy(p2.get(), EC_KEY_get0_public_key(key.get()))) {
  387. fprintf(stderr, "EC_POINT_COPY failed with nid %d\n", nid);
  388. ERR_print_errors_fp(stderr);
  389. return false;
  390. }
  391. bssl::UniquePtr<BN_CTX> ctx(BN_CTX_new());
  392. if (!ctx) {
  393. return false;
  394. }
  395. if (!EC_POINT_dbl(group, double_p1.get(), p1.get(), ctx.get()) ||
  396. !EC_POINT_add(group, p1_plus_p2.get(), p1.get(), p2.get(), ctx.get())) {
  397. fprintf(stderr, "Point operation failed with nid %d\n", nid);
  398. ERR_print_errors_fp(stderr);
  399. return false;
  400. }
  401. if (EC_POINT_cmp(group, double_p1.get(), p1_plus_p2.get(), ctx.get()) != 0) {
  402. fprintf(stderr, "A+A != 2A for nid %d", nid);
  403. return false;
  404. }
  405. return true;
  406. }
  407. static bool TestMulZero(int nid) {
  408. bssl::UniquePtr<EC_GROUP> group(EC_GROUP_new_by_curve_name(nid));
  409. if (!group) {
  410. return false;
  411. }
  412. bssl::UniquePtr<EC_POINT> point(EC_POINT_new(group.get()));
  413. bssl::UniquePtr<BIGNUM> zero(BN_new());
  414. if (!point || !zero) {
  415. return false;
  416. }
  417. BN_zero(zero.get());
  418. if (!EC_POINT_mul(group.get(), point.get(), zero.get(), nullptr, nullptr,
  419. nullptr)) {
  420. return false;
  421. }
  422. if (!EC_POINT_is_at_infinity(group.get(), point.get())) {
  423. fprintf(stderr, "g * 0 did not return point at infinity.\n");
  424. return false;
  425. }
  426. // Test that zero times an arbitrary point is also infinity. The generator is
  427. // used as the arbitrary point.
  428. bssl::UniquePtr<EC_POINT> generator(EC_POINT_new(group.get()));
  429. bssl::UniquePtr<BIGNUM> one(BN_new());
  430. if (!generator ||
  431. !one ||
  432. !BN_one(one.get()) ||
  433. !EC_POINT_mul(group.get(), generator.get(), one.get(), nullptr, nullptr,
  434. nullptr) ||
  435. !EC_POINT_mul(group.get(), point.get(), nullptr, generator.get(),
  436. zero.get(), nullptr)) {
  437. return false;
  438. }
  439. if (!EC_POINT_is_at_infinity(group.get(), point.get())) {
  440. fprintf(stderr, "p * 0 did not return point at infinity.\n");
  441. return false;
  442. }
  443. return true;
  444. }
  445. static bool ForEachCurve(bool (*test_func)(int nid)) {
  446. const size_t num_curves = EC_get_builtin_curves(nullptr, 0);
  447. std::vector<EC_builtin_curve> curves(num_curves);
  448. EC_get_builtin_curves(curves.data(), num_curves);
  449. for (const auto& curve : curves) {
  450. if (!test_func(curve.nid)) {
  451. fprintf(stderr, "Test failed for %s\n", curve.comment);
  452. return false;
  453. }
  454. }
  455. return true;
  456. }
  457. int main() {
  458. CRYPTO_library_init();
  459. if (!Testd2i_ECPrivateKey() ||
  460. !TestZeroPadding() ||
  461. !TestSpecifiedCurve() ||
  462. !ForEachCurve(TestSetAffine) ||
  463. !ForEachCurve(TestAddingEqualPoints) ||
  464. !ForEachCurve(TestMulZero) ||
  465. !TestArbitraryCurve()) {
  466. fprintf(stderr, "failed\n");
  467. return 1;
  468. }
  469. printf("PASS\n");
  470. return 0;
  471. }