ec_internal_test.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include "internal/nelem.h"
  10. #include "testutil.h"
  11. #include <openssl/ec.h>
  12. #include "ec_local.h"
  13. #include <openssl/objects.h>
  14. static size_t crv_len = 0;
  15. static EC_builtin_curve *curves = NULL;
  16. /* sanity checks field_inv function pointer in EC_METHOD */
  17. static int group_field_tests(const EC_GROUP *group, BN_CTX *ctx)
  18. {
  19. BIGNUM *a = NULL, *b = NULL, *c = NULL;
  20. int ret = 0;
  21. if (group->meth->field_inv == NULL || group->meth->field_mul == NULL)
  22. return 1;
  23. BN_CTX_start(ctx);
  24. a = BN_CTX_get(ctx);
  25. b = BN_CTX_get(ctx);
  26. if (!TEST_ptr(c = BN_CTX_get(ctx))
  27. /* 1/1 = 1 */
  28. || !TEST_true(group->meth->field_inv(group, b, BN_value_one(), ctx))
  29. || !TEST_true(BN_is_one(b))
  30. /* (1/a)*a = 1 */
  31. || !TEST_true(BN_pseudo_rand(a, BN_num_bits(group->field) - 1,
  32. BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))
  33. || !TEST_true(group->meth->field_inv(group, b, a, ctx))
  34. || (group->meth->field_encode &&
  35. !TEST_true(group->meth->field_encode(group, a, a, ctx)))
  36. || (group->meth->field_encode &&
  37. !TEST_true(group->meth->field_encode(group, b, b, ctx)))
  38. || !TEST_true(group->meth->field_mul(group, c, a, b, ctx))
  39. || (group->meth->field_decode &&
  40. !TEST_true(group->meth->field_decode(group, c, c, ctx)))
  41. || !TEST_true(BN_is_one(c)))
  42. goto err;
  43. /* 1/0 = error */
  44. BN_zero(a);
  45. if (!TEST_false(group->meth->field_inv(group, b, a, ctx))
  46. || !TEST_true(ERR_GET_LIB(ERR_peek_last_error()) == ERR_LIB_EC)
  47. || !TEST_true(ERR_GET_REASON(ERR_peek_last_error()) ==
  48. EC_R_CANNOT_INVERT)
  49. /* 1/p = error */
  50. || !TEST_false(group->meth->field_inv(group, b, group->field, ctx))
  51. || !TEST_true(ERR_GET_LIB(ERR_peek_last_error()) == ERR_LIB_EC)
  52. || !TEST_true(ERR_GET_REASON(ERR_peek_last_error()) ==
  53. EC_R_CANNOT_INVERT))
  54. goto err;
  55. ERR_clear_error();
  56. ret = 1;
  57. err:
  58. BN_CTX_end(ctx);
  59. return ret;
  60. }
  61. /* wrapper for group_field_tests for explicit curve params and EC_METHOD */
  62. static int field_tests(const EC_METHOD *meth, const unsigned char *params,
  63. int len)
  64. {
  65. BN_CTX *ctx = NULL;
  66. BIGNUM *p = NULL, *a = NULL, *b = NULL;
  67. EC_GROUP *group = NULL;
  68. int ret = 0;
  69. if (!TEST_ptr(ctx = BN_CTX_new()))
  70. return 0;
  71. BN_CTX_start(ctx);
  72. p = BN_CTX_get(ctx);
  73. a = BN_CTX_get(ctx);
  74. if (!TEST_ptr(b = BN_CTX_get(ctx))
  75. || !TEST_ptr(group = EC_GROUP_new(meth))
  76. || !TEST_true(BN_bin2bn(params, len, p))
  77. || !TEST_true(BN_bin2bn(params + len, len, a))
  78. || !TEST_true(BN_bin2bn(params + 2 * len, len, b))
  79. || !TEST_true(EC_GROUP_set_curve(group, p, a, b, ctx))
  80. || !group_field_tests(group, ctx))
  81. goto err;
  82. ret = 1;
  83. err:
  84. BN_CTX_end(ctx);
  85. BN_CTX_free(ctx);
  86. if (group != NULL)
  87. EC_GROUP_free(group);
  88. return ret;
  89. }
  90. /* NIST prime curve P-256 */
  91. static const unsigned char params_p256[] = {
  92. /* p */
  93. 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  94. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
  95. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  96. /* a */
  97. 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  98. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
  99. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC,
  100. /* b */
  101. 0x5A, 0xC6, 0x35, 0xD8, 0xAA, 0x3A, 0x93, 0xE7, 0xB3, 0xEB, 0xBD, 0x55,
  102. 0x76, 0x98, 0x86, 0xBC, 0x65, 0x1D, 0x06, 0xB0, 0xCC, 0x53, 0xB0, 0xF6,
  103. 0x3B, 0xCE, 0x3C, 0x3E, 0x27, 0xD2, 0x60, 0x4B
  104. };
  105. #ifndef OPENSSL_NO_EC2M
  106. /* NIST binary curve B-283 */
  107. static const unsigned char params_b283[] = {
  108. /* p */
  109. 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  110. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  111. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0xA1,
  112. /* a */
  113. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  114. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  115. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  116. /* b */
  117. 0x02, 0x7B, 0x68, 0x0A, 0xC8, 0xB8, 0x59, 0x6D, 0xA5, 0xA4, 0xAF, 0x8A,
  118. 0x19, 0xA0, 0x30, 0x3F, 0xCA, 0x97, 0xFD, 0x76, 0x45, 0x30, 0x9F, 0xA2,
  119. 0xA5, 0x81, 0x48, 0x5A, 0xF6, 0x26, 0x3E, 0x31, 0x3B, 0x79, 0xA2, 0xF5
  120. };
  121. #endif
  122. /* test EC_GFp_simple_method directly */
  123. static int field_tests_ecp_simple(void)
  124. {
  125. TEST_info("Testing EC_GFp_simple_method()\n");
  126. return field_tests(EC_GFp_simple_method(), params_p256,
  127. sizeof(params_p256) / 3);
  128. }
  129. /* test EC_GFp_mont_method directly */
  130. static int field_tests_ecp_mont(void)
  131. {
  132. TEST_info("Testing EC_GFp_mont_method()\n");
  133. return field_tests(EC_GFp_mont_method(), params_p256,
  134. sizeof(params_p256) / 3);
  135. }
  136. #ifndef OPENSSL_NO_EC2M
  137. /* test EC_GF2m_simple_method directly */
  138. static int field_tests_ec2_simple(void)
  139. {
  140. TEST_info("Testing EC_GF2m_simple_method()\n");
  141. return field_tests(EC_GF2m_simple_method(), params_b283,
  142. sizeof(params_b283) / 3);
  143. }
  144. #endif
  145. /* test default method for a named curve */
  146. static int field_tests_default(int n)
  147. {
  148. BN_CTX *ctx = NULL;
  149. EC_GROUP *group = NULL;
  150. int nid = curves[n].nid;
  151. int ret = 0;
  152. TEST_info("Testing curve %s\n", OBJ_nid2sn(nid));
  153. if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(nid))
  154. || !TEST_ptr(ctx = BN_CTX_new())
  155. || !group_field_tests(group, ctx))
  156. goto err;
  157. ret = 1;
  158. err:
  159. if (group != NULL)
  160. EC_GROUP_free(group);
  161. if (ctx != NULL)
  162. BN_CTX_free(ctx);
  163. return ret;
  164. }
  165. /*
  166. * Tests behavior of the decoded_from_explicit_params flag and API
  167. */
  168. static int decoded_flag_test(void)
  169. {
  170. EC_GROUP *grp;
  171. EC_GROUP *grp_copy = NULL;
  172. ECPARAMETERS *ecparams = NULL;
  173. ECPKPARAMETERS *ecpkparams = NULL;
  174. EC_KEY *key = NULL;
  175. unsigned char *encodedparams = NULL;
  176. const unsigned char *encp;
  177. int encodedlen;
  178. int testresult = 0;
  179. /* Test EC_GROUP_new not setting the flag */
  180. grp = EC_GROUP_new(EC_GFp_simple_method());
  181. if (!TEST_ptr(grp)
  182. || !TEST_int_eq(grp->decoded_from_explicit_params, 0))
  183. goto err;
  184. EC_GROUP_free(grp);
  185. /* Test EC_GROUP_new_by_curve_name not setting the flag */
  186. grp = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
  187. if (!TEST_ptr(grp)
  188. || !TEST_int_eq(grp->decoded_from_explicit_params, 0))
  189. goto err;
  190. /* Test EC_GROUP_new_from_ecparameters not setting the flag */
  191. if (!TEST_ptr(ecparams = EC_GROUP_get_ecparameters(grp, NULL))
  192. || !TEST_ptr(grp_copy = EC_GROUP_new_from_ecparameters(ecparams))
  193. || !TEST_int_eq(grp_copy->decoded_from_explicit_params, 0))
  194. goto err;
  195. EC_GROUP_free(grp_copy);
  196. grp_copy = NULL;
  197. ECPARAMETERS_free(ecparams);
  198. ecparams = NULL;
  199. /* Test EC_GROUP_new_from_ecpkparameters not setting the flag */
  200. if (!TEST_int_eq(EC_GROUP_get_asn1_flag(grp), OPENSSL_EC_NAMED_CURVE)
  201. || !TEST_ptr(ecpkparams = EC_GROUP_get_ecpkparameters(grp, NULL))
  202. || !TEST_ptr(grp_copy = EC_GROUP_new_from_ecpkparameters(ecpkparams))
  203. || !TEST_int_eq(grp_copy->decoded_from_explicit_params, 0)
  204. || !TEST_ptr(key = EC_KEY_new())
  205. /* Test EC_KEY_decoded_from_explicit_params on key without a group */
  206. || !TEST_int_eq(EC_KEY_decoded_from_explicit_params(key), -1)
  207. || !TEST_int_eq(EC_KEY_set_group(key, grp_copy), 1)
  208. /* Test EC_KEY_decoded_from_explicit_params negative case */
  209. || !TEST_int_eq(EC_KEY_decoded_from_explicit_params(key), 0))
  210. goto err;
  211. EC_GROUP_free(grp_copy);
  212. grp_copy = NULL;
  213. ECPKPARAMETERS_free(ecpkparams);
  214. ecpkparams = NULL;
  215. /* Test d2i_ECPKParameters with named params not setting the flag */
  216. if (!TEST_int_gt(encodedlen = i2d_ECPKParameters(grp, &encodedparams), 0)
  217. || !TEST_ptr(encp = encodedparams)
  218. || !TEST_ptr(grp_copy = d2i_ECPKParameters(NULL, &encp, encodedlen))
  219. || !TEST_int_eq(grp_copy->decoded_from_explicit_params, 0))
  220. goto err;
  221. EC_GROUP_free(grp_copy);
  222. grp_copy = NULL;
  223. OPENSSL_free(encodedparams);
  224. encodedparams = NULL;
  225. /* Asn1 flag stays set to explicit with EC_GROUP_new_from_ecpkparameters */
  226. EC_GROUP_set_asn1_flag(grp, OPENSSL_EC_EXPLICIT_CURVE);
  227. if (!TEST_ptr(ecpkparams = EC_GROUP_get_ecpkparameters(grp, NULL))
  228. || !TEST_ptr(grp_copy = EC_GROUP_new_from_ecpkparameters(ecpkparams))
  229. || !TEST_int_eq(EC_GROUP_get_asn1_flag(grp_copy), OPENSSL_EC_EXPLICIT_CURVE)
  230. || !TEST_int_eq(grp_copy->decoded_from_explicit_params, 0))
  231. goto err;
  232. EC_GROUP_free(grp_copy);
  233. grp_copy = NULL;
  234. /* Test d2i_ECPKParameters with explicit params setting the flag */
  235. if (!TEST_int_gt(encodedlen = i2d_ECPKParameters(grp, &encodedparams), 0)
  236. || !TEST_ptr(encp = encodedparams)
  237. || !TEST_ptr(grp_copy = d2i_ECPKParameters(NULL, &encp, encodedlen))
  238. || !TEST_int_eq(EC_GROUP_get_asn1_flag(grp_copy), OPENSSL_EC_EXPLICIT_CURVE)
  239. || !TEST_int_eq(grp_copy->decoded_from_explicit_params, 1)
  240. || !TEST_int_eq(EC_KEY_set_group(key, grp_copy), 1)
  241. /* Test EC_KEY_decoded_from_explicit_params positive case */
  242. || !TEST_int_eq(EC_KEY_decoded_from_explicit_params(key), 1))
  243. goto err;
  244. testresult = 1;
  245. err:
  246. EC_KEY_free(key);
  247. EC_GROUP_free(grp);
  248. EC_GROUP_free(grp_copy);
  249. ECPARAMETERS_free(ecparams);
  250. ECPKPARAMETERS_free(ecpkparams);
  251. OPENSSL_free(encodedparams);
  252. return testresult;
  253. }
  254. static
  255. int ecpkparams_i2d2i_test(int n)
  256. {
  257. EC_GROUP *g1 = NULL, *g2 = NULL;
  258. FILE *fp = NULL;
  259. int nid = curves[n].nid;
  260. int testresult = 0;
  261. /* create group */
  262. if (!TEST_ptr(g1 = EC_GROUP_new_by_curve_name(nid)))
  263. goto end;
  264. /* encode params to file */
  265. if (!TEST_ptr(fp = fopen("params.der", "wb"))
  266. || !TEST_true(i2d_ECPKParameters_fp(fp, g1)))
  267. goto end;
  268. /* flush and close file */
  269. if (!TEST_int_eq(fclose(fp), 0)) {
  270. fp = NULL;
  271. goto end;
  272. }
  273. fp = NULL;
  274. /* decode params from file */
  275. if (!TEST_ptr(fp = fopen("params.der", "rb"))
  276. || !TEST_ptr(g2 = d2i_ECPKParameters_fp(fp, NULL)))
  277. goto end;
  278. testresult = 1; /* PASS */
  279. end:
  280. if (fp != NULL)
  281. fclose(fp);
  282. EC_GROUP_free(g1);
  283. EC_GROUP_free(g2);
  284. return testresult;
  285. }
  286. int setup_tests(void)
  287. {
  288. crv_len = EC_get_builtin_curves(NULL, 0);
  289. if (!TEST_ptr(curves = OPENSSL_malloc(sizeof(*curves) * crv_len))
  290. || !TEST_true(EC_get_builtin_curves(curves, crv_len)))
  291. return 0;
  292. ADD_TEST(field_tests_ecp_simple);
  293. ADD_TEST(field_tests_ecp_mont);
  294. #ifndef OPENSSL_NO_EC2M
  295. ADD_TEST(field_tests_ec2_simple);
  296. #endif
  297. ADD_ALL_TESTS(field_tests_default, crv_len);
  298. ADD_TEST(decoded_flag_test);
  299. ADD_ALL_TESTS(ecpkparams_i2d2i_test, crv_len);
  300. return 1;
  301. }
  302. void cleanup_tests(void)
  303. {
  304. OPENSSL_free(curves);
  305. }