2
0

ecp_nist.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright 2001-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
  4. *
  5. * Licensed under the Apache License 2.0 (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. /*
  11. * ECDSA low level APIs are deprecated for public use, but still ok for
  12. * internal use.
  13. */
  14. #include "internal/deprecated.h"
  15. #include <limits.h>
  16. #include <openssl/err.h>
  17. #include <openssl/obj_mac.h>
  18. #include "ec_local.h"
  19. const EC_METHOD *EC_GFp_nist_method(void)
  20. {
  21. static const EC_METHOD ret = {
  22. EC_FLAGS_DEFAULT_OCT,
  23. NID_X9_62_prime_field,
  24. ossl_ec_GFp_simple_group_init,
  25. ossl_ec_GFp_simple_group_finish,
  26. ossl_ec_GFp_simple_group_clear_finish,
  27. ossl_ec_GFp_nist_group_copy,
  28. ossl_ec_GFp_nist_group_set_curve,
  29. ossl_ec_GFp_simple_group_get_curve,
  30. ossl_ec_GFp_simple_group_get_degree,
  31. ossl_ec_group_simple_order_bits,
  32. ossl_ec_GFp_simple_group_check_discriminant,
  33. ossl_ec_GFp_simple_point_init,
  34. ossl_ec_GFp_simple_point_finish,
  35. ossl_ec_GFp_simple_point_clear_finish,
  36. ossl_ec_GFp_simple_point_copy,
  37. ossl_ec_GFp_simple_point_set_to_infinity,
  38. ossl_ec_GFp_simple_point_set_affine_coordinates,
  39. ossl_ec_GFp_simple_point_get_affine_coordinates,
  40. 0, 0, 0,
  41. ossl_ec_GFp_simple_add,
  42. ossl_ec_GFp_simple_dbl,
  43. ossl_ec_GFp_simple_invert,
  44. ossl_ec_GFp_simple_is_at_infinity,
  45. ossl_ec_GFp_simple_is_on_curve,
  46. ossl_ec_GFp_simple_cmp,
  47. ossl_ec_GFp_simple_make_affine,
  48. ossl_ec_GFp_simple_points_make_affine,
  49. 0 /* mul */ ,
  50. 0 /* precompute_mult */ ,
  51. 0 /* have_precompute_mult */ ,
  52. ossl_ec_GFp_nist_field_mul,
  53. ossl_ec_GFp_nist_field_sqr,
  54. 0 /* field_div */ ,
  55. ossl_ec_GFp_simple_field_inv,
  56. 0 /* field_encode */ ,
  57. 0 /* field_decode */ ,
  58. 0, /* field_set_to_one */
  59. ossl_ec_key_simple_priv2oct,
  60. ossl_ec_key_simple_oct2priv,
  61. 0, /* set private */
  62. ossl_ec_key_simple_generate_key,
  63. ossl_ec_key_simple_check_key,
  64. ossl_ec_key_simple_generate_public_key,
  65. 0, /* keycopy */
  66. 0, /* keyfinish */
  67. ossl_ecdh_simple_compute_key,
  68. ossl_ecdsa_simple_sign_setup,
  69. ossl_ecdsa_simple_sign_sig,
  70. ossl_ecdsa_simple_verify_sig,
  71. 0, /* field_inverse_mod_ord */
  72. ossl_ec_GFp_simple_blind_coordinates,
  73. ossl_ec_GFp_simple_ladder_pre,
  74. ossl_ec_GFp_simple_ladder_step,
  75. ossl_ec_GFp_simple_ladder_post
  76. };
  77. return &ret;
  78. }
  79. int ossl_ec_GFp_nist_group_copy(EC_GROUP *dest, const EC_GROUP *src)
  80. {
  81. dest->field_mod_func = src->field_mod_func;
  82. return ossl_ec_GFp_simple_group_copy(dest, src);
  83. }
  84. int ossl_ec_GFp_nist_group_set_curve(EC_GROUP *group, const BIGNUM *p,
  85. const BIGNUM *a, const BIGNUM *b,
  86. BN_CTX *ctx)
  87. {
  88. int ret = 0;
  89. BN_CTX *new_ctx = NULL;
  90. if (ctx == NULL)
  91. if ((ctx = new_ctx = BN_CTX_new_ex(group->libctx)) == NULL)
  92. return 0;
  93. BN_CTX_start(ctx);
  94. if (BN_ucmp(BN_get0_nist_prime_192(), p) == 0)
  95. group->field_mod_func = BN_nist_mod_192;
  96. else if (BN_ucmp(BN_get0_nist_prime_224(), p) == 0)
  97. group->field_mod_func = BN_nist_mod_224;
  98. else if (BN_ucmp(BN_get0_nist_prime_256(), p) == 0)
  99. group->field_mod_func = BN_nist_mod_256;
  100. else if (BN_ucmp(BN_get0_nist_prime_384(), p) == 0)
  101. group->field_mod_func = BN_nist_mod_384;
  102. else if (BN_ucmp(BN_get0_nist_prime_521(), p) == 0)
  103. group->field_mod_func = BN_nist_mod_521;
  104. else {
  105. ERR_raise(ERR_LIB_EC, EC_R_NOT_A_NIST_PRIME);
  106. goto err;
  107. }
  108. ret = ossl_ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
  109. err:
  110. BN_CTX_end(ctx);
  111. BN_CTX_free(new_ctx);
  112. return ret;
  113. }
  114. int ossl_ec_GFp_nist_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
  115. const BIGNUM *b, BN_CTX *ctx)
  116. {
  117. int ret = 0;
  118. BN_CTX *ctx_new = NULL;
  119. if (!group || !r || !a || !b) {
  120. ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
  121. goto err;
  122. }
  123. if (!ctx)
  124. if ((ctx_new = ctx = BN_CTX_new_ex(group->libctx)) == NULL)
  125. goto err;
  126. if (!BN_mul(r, a, b, ctx))
  127. goto err;
  128. if (!group->field_mod_func(r, r, group->field, ctx))
  129. goto err;
  130. ret = 1;
  131. err:
  132. BN_CTX_free(ctx_new);
  133. return ret;
  134. }
  135. int ossl_ec_GFp_nist_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
  136. BN_CTX *ctx)
  137. {
  138. int ret = 0;
  139. BN_CTX *ctx_new = NULL;
  140. if (!group || !r || !a) {
  141. ERR_raise(ERR_LIB_EC, EC_R_PASSED_NULL_PARAMETER);
  142. goto err;
  143. }
  144. if (!ctx)
  145. if ((ctx_new = ctx = BN_CTX_new_ex(group->libctx)) == NULL)
  146. goto err;
  147. if (!BN_sqr(r, a, ctx))
  148. goto err;
  149. if (!group->field_mod_func(r, r, group->field, ctx))
  150. goto err;
  151. ret = 1;
  152. err:
  153. BN_CTX_free(ctx_new);
  154. return ret;
  155. }