You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

347 lines
12 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/crypto.h>
  18. #include <openssl/ec_key.h>
  19. #include <openssl/err.h>
  20. #include <openssl/mem.h>
  21. #include "../test/scoped_types.h"
  22. // kECKeyWithoutPublic is an ECPrivateKey with the optional publicKey field
  23. // omitted.
  24. static const uint8_t kECKeyWithoutPublic[] = {
  25. 0x30, 0x31, 0x02, 0x01, 0x01, 0x04, 0x20, 0xc6, 0xc1, 0xaa, 0xda, 0x15, 0xb0,
  26. 0x76, 0x61, 0xf8, 0x14, 0x2c, 0x6c, 0xaf, 0x0f, 0xdb, 0x24, 0x1a, 0xff, 0x2e,
  27. 0xfe, 0x46, 0xc0, 0x93, 0x8b, 0x74, 0xf2, 0xbc, 0xc5, 0x30, 0x52, 0xb0, 0x77,
  28. 0xa0, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  29. };
  30. // kECKeyMissingZeros is an ECPrivateKey containing a degenerate P-256 key where
  31. // the private key is one. The private key is incorrectly encoded without zero
  32. // padding.
  33. static const uint8_t kECKeyMissingZeros[] = {
  34. 0x30, 0x58, 0x02, 0x01, 0x01, 0x04, 0x01, 0x01, 0xa0, 0x0a, 0x06, 0x08, 0x2a,
  35. 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0xa1, 0x44, 0x03, 0x42, 0x00, 0x04,
  36. 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc, 0xe6, 0xe5, 0x63,
  37. 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d, 0xeb, 0x33, 0xa0, 0xf4, 0xa1,
  38. 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96, 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f,
  39. 0x9b, 0x8e, 0xe7, 0xeb, 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57,
  40. 0x6b, 0x31, 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  41. };
  42. // kECKeyMissingZeros is an ECPrivateKey containing a degenerate P-256 key where
  43. // the private key is one. The private key is encoded with the required zero
  44. // padding.
  45. static const uint8_t kECKeyWithZeros[] = {
  46. 0x30, 0x77, 0x02, 0x01, 0x01, 0x04, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  47. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  48. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  49. 0xa0, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0xa1,
  50. 0x44, 0x03, 0x42, 0x00, 0x04, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47,
  51. 0xf8, 0xbc, 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  52. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96, 0x4f, 0xe3,
  53. 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb, 0x4a, 0x7c, 0x0f, 0x9e,
  54. 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31, 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68,
  55. 0x37, 0xbf, 0x51, 0xf5,
  56. };
  57. // DecodeECPrivateKey decodes |in| as an ECPrivateKey structure and returns the
  58. // result or nullptr on error.
  59. static ScopedEC_KEY DecodeECPrivateKey(const uint8_t *in, size_t in_len) {
  60. const uint8_t *inp = in;
  61. ScopedEC_KEY ret(d2i_ECPrivateKey(NULL, &inp, in_len));
  62. if (!ret || inp != in + in_len) {
  63. return nullptr;
  64. }
  65. return ret;
  66. }
  67. // EncodeECPrivateKey encodes |key| as an ECPrivateKey structure into |*out|. It
  68. // returns true on success or false on error.
  69. static bool EncodeECPrivateKey(std::vector<uint8_t> *out, EC_KEY *key) {
  70. int len = i2d_ECPrivateKey(key, NULL);
  71. out->resize(len);
  72. uint8_t *outp = out->data();
  73. return i2d_ECPrivateKey(key, &outp) == len;
  74. }
  75. bool Testd2i_ECPrivateKey() {
  76. ScopedEC_KEY key = DecodeECPrivateKey(kECKeyWithoutPublic,
  77. sizeof(kECKeyWithoutPublic));
  78. if (!key) {
  79. fprintf(stderr, "Failed to parse private key.\n");
  80. ERR_print_errors_fp(stderr);
  81. return false;
  82. }
  83. std::vector<uint8_t> out;
  84. if (!EncodeECPrivateKey(&out, key.get())) {
  85. fprintf(stderr, "Failed to serialize private key.\n");
  86. ERR_print_errors_fp(stderr);
  87. return false;
  88. }
  89. if (std::vector<uint8_t>(kECKeyWithoutPublic,
  90. kECKeyWithoutPublic + sizeof(kECKeyWithoutPublic)) !=
  91. out) {
  92. fprintf(stderr, "Serialisation of key doesn't match original.\n");
  93. return false;
  94. }
  95. const EC_POINT *pub_key = EC_KEY_get0_public_key(key.get());
  96. if (pub_key == NULL) {
  97. fprintf(stderr, "Public key missing.\n");
  98. return false;
  99. }
  100. ScopedBIGNUM x(BN_new());
  101. ScopedBIGNUM y(BN_new());
  102. if (!x || !y) {
  103. return false;
  104. }
  105. if (!EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(key.get()),
  106. pub_key, x.get(), y.get(), NULL)) {
  107. fprintf(stderr, "Failed to get public key in affine coordinates.\n");
  108. return false;
  109. }
  110. ScopedOpenSSLString x_hex(BN_bn2hex(x.get()));
  111. ScopedOpenSSLString y_hex(BN_bn2hex(y.get()));
  112. if (!x_hex || !y_hex) {
  113. return false;
  114. }
  115. if (0 != strcmp(
  116. x_hex.get(),
  117. "c81561ecf2e54edefe6617db1c7a34a70744ddb261f269b83dacfcd2ade5a681") ||
  118. 0 != strcmp(
  119. y_hex.get(),
  120. "e0e2afa3f9b6abe4c698ef6495f1be49a3196c5056acb3763fe4507eec596e88")) {
  121. fprintf(stderr, "Incorrect public key: %s %s\n", x_hex.get(), y_hex.get());
  122. return false;
  123. }
  124. return true;
  125. }
  126. static bool TestZeroPadding() {
  127. // Check that the correct encoding round-trips.
  128. ScopedEC_KEY key = DecodeECPrivateKey(kECKeyWithZeros,
  129. sizeof(kECKeyWithZeros));
  130. std::vector<uint8_t> out;
  131. if (!key || !EncodeECPrivateKey(&out, key.get())) {
  132. ERR_print_errors_fp(stderr);
  133. return false;
  134. }
  135. if (std::vector<uint8_t>(kECKeyWithZeros,
  136. kECKeyWithZeros + sizeof(kECKeyWithZeros)) != out) {
  137. fprintf(stderr, "Serialisation of key was incorrect.\n");
  138. return false;
  139. }
  140. // Keys without leading zeros also parse, but they encode correctly.
  141. key = DecodeECPrivateKey(kECKeyMissingZeros, sizeof(kECKeyMissingZeros));
  142. if (!key || !EncodeECPrivateKey(&out, key.get())) {
  143. ERR_print_errors_fp(stderr);
  144. return false;
  145. }
  146. if (std::vector<uint8_t>(kECKeyWithZeros,
  147. kECKeyWithZeros + sizeof(kECKeyWithZeros)) != out) {
  148. fprintf(stderr, "Serialisation of key was incorrect.\n");
  149. return false;
  150. }
  151. return true;
  152. }
  153. bool TestSetAffine(const int nid) {
  154. ScopedEC_KEY key(EC_KEY_new_by_curve_name(nid));
  155. if (!key) {
  156. return false;
  157. }
  158. const EC_GROUP *const group = EC_KEY_get0_group(key.get());
  159. if (!EC_KEY_generate_key(key.get())) {
  160. fprintf(stderr, "EC_KEY_generate_key failed with nid %d\n", nid);
  161. ERR_print_errors_fp(stderr);
  162. return false;
  163. }
  164. if (!EC_POINT_is_on_curve(group, EC_KEY_get0_public_key(key.get()),
  165. nullptr)) {
  166. fprintf(stderr, "generated point is not on curve with nid %d", nid);
  167. ERR_print_errors_fp(stderr);
  168. return false;
  169. }
  170. ScopedBIGNUM x(BN_new());
  171. ScopedBIGNUM y(BN_new());
  172. if (!EC_POINT_get_affine_coordinates_GFp(group,
  173. EC_KEY_get0_public_key(key.get()),
  174. x.get(), y.get(), nullptr)) {
  175. fprintf(stderr, "EC_POINT_get_affine_coordinates_GFp failed with nid %d\n",
  176. nid);
  177. ERR_print_errors_fp(stderr);
  178. return false;
  179. }
  180. ScopedEC_POINT point(EC_POINT_new(group));
  181. if (!point) {
  182. return false;
  183. }
  184. if (!EC_POINT_set_affine_coordinates_GFp(group, point.get(), x.get(), y.get(),
  185. nullptr)) {
  186. fprintf(stderr, "EC_POINT_set_affine_coordinates_GFp failed with nid %d\n",
  187. nid);
  188. ERR_print_errors_fp(stderr);
  189. return false;
  190. }
  191. // Subtract one from |y| to make the point no longer on the curve.
  192. if (!BN_sub(y.get(), y.get(), BN_value_one())) {
  193. return false;
  194. }
  195. ScopedEC_POINT invalid_point(EC_POINT_new(group));
  196. if (!invalid_point) {
  197. return false;
  198. }
  199. if (EC_POINT_set_affine_coordinates_GFp(group, invalid_point.get(), x.get(),
  200. y.get(), nullptr)) {
  201. fprintf(stderr,
  202. "EC_POINT_set_affine_coordinates_GFp succeeded with invalid "
  203. "coordinates with nid %d\n",
  204. nid);
  205. ERR_print_errors_fp(stderr);
  206. return false;
  207. }
  208. return true;
  209. }
  210. static bool TestArbitraryCurve() {
  211. // Make a P-256 key and extract the affine coordinates.
  212. ScopedEC_KEY key(EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  213. if (!key || !EC_KEY_generate_key(key.get())) {
  214. return false;
  215. }
  216. // Make an arbitrary curve which is identical to P-256.
  217. static const uint8_t kP[] = {
  218. 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  219. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
  220. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  221. };
  222. static const uint8_t kA[] = {
  223. 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  224. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
  225. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc,
  226. };
  227. static const uint8_t kB[] = {
  228. 0x5a, 0xc6, 0x35, 0xd8, 0xaa, 0x3a, 0x93, 0xe7, 0xb3, 0xeb, 0xbd,
  229. 0x55, 0x76, 0x98, 0x86, 0xbc, 0x65, 0x1d, 0x06, 0xb0, 0xcc, 0x53,
  230. 0xb0, 0xf6, 0x3b, 0xce, 0x3c, 0x3e, 0x27, 0xd2, 0x60, 0x4b,
  231. };
  232. static const uint8_t kX[] = {
  233. 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc, 0xe6,
  234. 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d, 0xeb,
  235. 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  236. };
  237. static const uint8_t kY[] = {
  238. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  239. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  240. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  241. };
  242. static const uint8_t kOrder[] = {
  243. 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff,
  244. 0xff, 0xff, 0xff, 0xff, 0xff, 0xbc, 0xe6, 0xfa, 0xad, 0xa7, 0x17,
  245. 0x9e, 0x84, 0xf3, 0xb9, 0xca, 0xc2, 0xfc, 0x63, 0x25, 0x51,
  246. };
  247. ScopedBIGNUM p(BN_bin2bn(kP, sizeof(kP), nullptr));
  248. ScopedBIGNUM a(BN_bin2bn(kA, sizeof(kA), nullptr));
  249. ScopedBIGNUM b(BN_bin2bn(kB, sizeof(kB), nullptr));
  250. ScopedBIGNUM x(BN_bin2bn(kX, sizeof(kX), nullptr));
  251. ScopedBIGNUM y(BN_bin2bn(kY, sizeof(kY), nullptr));
  252. ScopedBIGNUM order(BN_bin2bn(kOrder, sizeof(kOrder), nullptr));
  253. ScopedBIGNUM cofactor(BN_new());
  254. if (!p || !a || !b || !x || !y || !order || !cofactor ||
  255. !BN_set_word(cofactor.get(), 1)) {
  256. return false;
  257. }
  258. ScopedEC_GROUP group(EC_GROUP_new_arbitrary(p.get(), a.get(), b.get(),
  259. x.get(), y.get(), order.get(),
  260. cofactor.get()));
  261. if (!group) {
  262. return false;
  263. }
  264. // |group| should not have a curve name.
  265. if (EC_GROUP_get_curve_name(group.get()) != NID_undef) {
  266. return false;
  267. }
  268. // Copy |key| to |key2| using |group|.
  269. ScopedEC_KEY key2(EC_KEY_new());
  270. ScopedEC_POINT point(EC_POINT_new(group.get()));
  271. if (!key2 || !point ||
  272. !EC_KEY_set_group(key2.get(), group.get()) ||
  273. !EC_KEY_set_private_key(key2.get(), EC_KEY_get0_private_key(key.get())) ||
  274. !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(key.get()),
  275. EC_KEY_get0_public_key(key.get()),
  276. x.get(), y.get(), nullptr) ||
  277. !EC_POINT_set_affine_coordinates_GFp(group.get(), point.get(), x.get(),
  278. y.get(), nullptr) ||
  279. !EC_KEY_set_public_key(key2.get(), point.get())) {
  280. fprintf(stderr, "Could not copy key.\n");
  281. return false;
  282. }
  283. // The key must be valid according to the new group too.
  284. if (!EC_KEY_check_key(key2.get())) {
  285. fprintf(stderr, "Copied key is not valid.\n");
  286. return false;
  287. }
  288. return true;
  289. }
  290. int main(void) {
  291. CRYPTO_library_init();
  292. ERR_load_crypto_strings();
  293. if (!Testd2i_ECPrivateKey() ||
  294. !TestZeroPadding() ||
  295. !TestSetAffine(NID_secp224r1) ||
  296. !TestSetAffine(NID_X9_62_prime256v1) ||
  297. !TestSetAffine(NID_secp384r1) ||
  298. !TestSetAffine(NID_secp521r1) ||
  299. !TestArbitraryCurve()) {
  300. fprintf(stderr, "failed\n");
  301. return 1;
  302. }
  303. printf("PASS\n");
  304. return 0;
  305. }