ec_internal_test.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 <openssl/ec.h>
  10. #include "ec_lcl.h"
  11. #include <openssl/objects.h>
  12. #include "testutil.h"
  13. static size_t crv_len = 0;
  14. static EC_builtin_curve *curves = NULL;
  15. /* sanity checks field_inv function pointer in EC_METHOD */
  16. static int group_field_tests(const EC_GROUP *group, BN_CTX *ctx)
  17. {
  18. BIGNUM *a = NULL, *b = NULL, *c = NULL;
  19. int ret = 0;
  20. if (group->meth->field_inv == NULL || group->meth->field_mul == NULL)
  21. return 1;
  22. BN_CTX_start(ctx);
  23. a = BN_CTX_get(ctx);
  24. b = BN_CTX_get(ctx);
  25. TEST_check(NULL != (c = BN_CTX_get(ctx)));
  26. /* 1/1 = 1 */
  27. TEST_check(group->meth->field_inv(group, b, BN_value_one(), ctx));
  28. TEST_check(BN_is_one(b));
  29. /* (1/a)*a = 1 */
  30. TEST_check(BN_pseudo_rand(a, BN_num_bits(group->field) - 1,
  31. BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY));
  32. TEST_check(group->meth->field_inv(group, b, a, ctx));
  33. if (group->meth->field_encode) {
  34. TEST_check(group->meth->field_encode(group, a, a, ctx));
  35. TEST_check(group->meth->field_encode(group, b, b, ctx));
  36. }
  37. TEST_check(group->meth->field_mul(group, c, a, b, ctx));
  38. if (group->meth->field_decode) {
  39. TEST_check(group->meth->field_decode(group, c, c, ctx));
  40. }
  41. TEST_check(BN_is_one(c));
  42. /* 1/0 = error */
  43. BN_zero(a);
  44. TEST_check(!group->meth->field_inv(group, b, a, ctx));
  45. TEST_check(ERR_GET_LIB(ERR_peek_last_error()) == ERR_LIB_EC);
  46. TEST_check(ERR_GET_REASON(ERR_peek_last_error()) == EC_R_CANNOT_INVERT);
  47. /* 1/p = error */
  48. TEST_check(!group->meth->field_inv(group, b, group->field, ctx));
  49. TEST_check(ERR_GET_LIB(ERR_peek_last_error()) == ERR_LIB_EC);
  50. TEST_check(ERR_GET_REASON(ERR_peek_last_error()) == EC_R_CANNOT_INVERT);
  51. ERR_clear_error();
  52. ret = 1;
  53. BN_CTX_end(ctx);
  54. return ret;
  55. }
  56. #define EC_GROUP_set_curve(g,p,a,b,ctx) \
  57. EC_GROUP_set_curve_GFp(g,p,a,b,ctx)
  58. /* wrapper for group_field_tests for explicit curve params and EC_METHOD */
  59. static int field_tests(const EC_METHOD *meth, const unsigned char *params,
  60. int len)
  61. {
  62. BN_CTX *ctx = NULL;
  63. BIGNUM *p = NULL, *a = NULL, *b = NULL;
  64. EC_GROUP *group = NULL;
  65. int ret = 0;
  66. TEST_check(NULL != (ctx = BN_CTX_new()));
  67. BN_CTX_start(ctx);
  68. p = BN_CTX_get(ctx);
  69. a = BN_CTX_get(ctx);
  70. TEST_check(NULL != (b = BN_CTX_get(ctx)));
  71. TEST_check(NULL != (group = EC_GROUP_new(meth)));
  72. TEST_check(BN_bin2bn(params, len, p));
  73. TEST_check(BN_bin2bn(params + len, len, a));
  74. TEST_check(BN_bin2bn(params + 2 * len, len, b));
  75. TEST_check(EC_GROUP_set_curve(group, p, a, b, ctx));
  76. TEST_check(group_field_tests(group, ctx));
  77. ret = 1;
  78. BN_CTX_end(ctx);
  79. BN_CTX_free(ctx);
  80. if (group != NULL)
  81. EC_GROUP_free(group);
  82. return ret;
  83. }
  84. #undef EC_GROUP_set_curve
  85. /* NIST prime curve P-256 */
  86. static const unsigned char params_p256[] = {
  87. /* p */
  88. 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  89. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
  90. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  91. /* a */
  92. 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  93. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
  94. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC,
  95. /* b */
  96. 0x5A, 0xC6, 0x35, 0xD8, 0xAA, 0x3A, 0x93, 0xE7, 0xB3, 0xEB, 0xBD, 0x55,
  97. 0x76, 0x98, 0x86, 0xBC, 0x65, 0x1D, 0x06, 0xB0, 0xCC, 0x53, 0xB0, 0xF6,
  98. 0x3B, 0xCE, 0x3C, 0x3E, 0x27, 0xD2, 0x60, 0x4B
  99. };
  100. #ifndef OPENSSL_NO_EC2M
  101. /* NIST binary curve B-283 */
  102. static const unsigned char params_b283[] = {
  103. /* p */
  104. 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  105. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  106. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0xA1,
  107. /* a */
  108. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  109. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  110. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  111. /* b */
  112. 0x02, 0x7B, 0x68, 0x0A, 0xC8, 0xB8, 0x59, 0x6D, 0xA5, 0xA4, 0xAF, 0x8A,
  113. 0x19, 0xA0, 0x30, 0x3F, 0xCA, 0x97, 0xFD, 0x76, 0x45, 0x30, 0x9F, 0xA2,
  114. 0xA5, 0x81, 0x48, 0x5A, 0xF6, 0x26, 0x3E, 0x31, 0x3B, 0x79, 0xA2, 0xF5
  115. };
  116. #endif
  117. /* test EC_GFp_simple_method directly */
  118. static int field_tests_ecp_simple(void)
  119. {
  120. fprintf(stdout, "Testing EC_GFp_simple_method()\n");
  121. return field_tests(EC_GFp_simple_method(), params_p256,
  122. sizeof(params_p256) / 3);
  123. }
  124. /* test EC_GFp_mont_method directly */
  125. static int field_tests_ecp_mont(void)
  126. {
  127. fprintf(stdout, "Testing EC_GFp_mont_method()\n");
  128. return field_tests(EC_GFp_mont_method(), params_p256,
  129. sizeof(params_p256) / 3);
  130. }
  131. #ifndef OPENSSL_NO_EC2M
  132. /* test EC_GF2m_simple_method directly */
  133. static int field_tests_ec2_simple(void)
  134. {
  135. fprintf(stdout, "Testing EC_GF2m_simple_method()\n");
  136. return field_tests(EC_GF2m_simple_method(), params_b283,
  137. sizeof(params_b283) / 3);
  138. }
  139. #endif
  140. /* test default method for a named curve */
  141. static int field_tests_default(int n)
  142. {
  143. BN_CTX *ctx = NULL;
  144. EC_GROUP *group = NULL;
  145. int nid = curves[n].nid;
  146. int ret = 0;
  147. fprintf(stdout, "Testing curve %s\n", OBJ_nid2sn(nid));
  148. TEST_check(NULL != (group = EC_GROUP_new_by_curve_name(nid)));
  149. TEST_check(NULL != (ctx = BN_CTX_new()));
  150. TEST_check(group_field_tests(group, ctx));
  151. ret = 1;
  152. if (group != NULL)
  153. EC_GROUP_free(group);
  154. if (ctx != NULL)
  155. BN_CTX_free(ctx);
  156. return ret;
  157. }
  158. static int setup_tests(void)
  159. {
  160. crv_len = EC_get_builtin_curves(NULL, 0);
  161. TEST_check(NULL != (curves = OPENSSL_malloc(sizeof(*curves) * crv_len)));
  162. TEST_check(EC_get_builtin_curves(curves, crv_len));
  163. ADD_TEST(field_tests_ecp_simple);
  164. ADD_TEST(field_tests_ecp_mont);
  165. #ifndef OPENSSL_NO_EC2M
  166. ADD_TEST(field_tests_ec2_simple);
  167. #endif
  168. ADD_ALL_TESTS(field_tests_default, crv_len);
  169. return 1;
  170. }
  171. static void cleanup_tests(void)
  172. {
  173. OPENSSL_free(curves);
  174. }
  175. int main(int argc, char **argv)
  176. {
  177. int ret;
  178. setup_tests();
  179. ret = run_tests(argv[0]);
  180. cleanup_tests();
  181. return ret;
  182. }