ec_internal_test.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright 2019 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_lcl.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. int setup_tests(void)
  166. {
  167. crv_len = EC_get_builtin_curves(NULL, 0);
  168. if (!TEST_ptr(curves = OPENSSL_malloc(sizeof(*curves) * crv_len))
  169. || !TEST_true(EC_get_builtin_curves(curves, crv_len)))
  170. return 0;
  171. ADD_TEST(field_tests_ecp_simple);
  172. ADD_TEST(field_tests_ecp_mont);
  173. #ifndef OPENSSL_NO_EC2M
  174. ADD_TEST(field_tests_ec2_simple);
  175. #endif
  176. ADD_ALL_TESTS(field_tests_default, crv_len);
  177. return 1;
  178. }
  179. void cleanup_tests(void)
  180. {
  181. OPENSSL_free(curves);
  182. }