ecp_nist.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright 2001-2018 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. ec_GFp_simple_group_init,
  25. ec_GFp_simple_group_finish,
  26. ec_GFp_simple_group_clear_finish,
  27. ec_GFp_nist_group_copy,
  28. ec_GFp_nist_group_set_curve,
  29. ec_GFp_simple_group_get_curve,
  30. ec_GFp_simple_group_get_degree,
  31. ec_group_simple_order_bits,
  32. ec_GFp_simple_group_check_discriminant,
  33. ec_GFp_simple_point_init,
  34. ec_GFp_simple_point_finish,
  35. ec_GFp_simple_point_clear_finish,
  36. ec_GFp_simple_point_copy,
  37. ec_GFp_simple_point_set_to_infinity,
  38. ec_GFp_simple_set_Jprojective_coordinates_GFp,
  39. ec_GFp_simple_get_Jprojective_coordinates_GFp,
  40. ec_GFp_simple_point_set_affine_coordinates,
  41. ec_GFp_simple_point_get_affine_coordinates,
  42. 0, 0, 0,
  43. ec_GFp_simple_add,
  44. ec_GFp_simple_dbl,
  45. ec_GFp_simple_invert,
  46. ec_GFp_simple_is_at_infinity,
  47. ec_GFp_simple_is_on_curve,
  48. ec_GFp_simple_cmp,
  49. ec_GFp_simple_make_affine,
  50. ec_GFp_simple_points_make_affine,
  51. 0 /* mul */ ,
  52. 0 /* precompute_mult */ ,
  53. 0 /* have_precompute_mult */ ,
  54. ec_GFp_nist_field_mul,
  55. ec_GFp_nist_field_sqr,
  56. 0 /* field_div */ ,
  57. ec_GFp_simple_field_inv,
  58. 0 /* field_encode */ ,
  59. 0 /* field_decode */ ,
  60. 0, /* field_set_to_one */
  61. ec_key_simple_priv2oct,
  62. ec_key_simple_oct2priv,
  63. 0, /* set private */
  64. ec_key_simple_generate_key,
  65. ec_key_simple_check_key,
  66. ec_key_simple_generate_public_key,
  67. 0, /* keycopy */
  68. 0, /* keyfinish */
  69. ecdh_simple_compute_key,
  70. ecdsa_simple_sign_setup,
  71. ecdsa_simple_sign_sig,
  72. ecdsa_simple_verify_sig,
  73. 0, /* field_inverse_mod_ord */
  74. ec_GFp_simple_blind_coordinates,
  75. ec_GFp_simple_ladder_pre,
  76. ec_GFp_simple_ladder_step,
  77. ec_GFp_simple_ladder_post
  78. };
  79. return &ret;
  80. }
  81. int ec_GFp_nist_group_copy(EC_GROUP *dest, const EC_GROUP *src)
  82. {
  83. dest->field_mod_func = src->field_mod_func;
  84. return ec_GFp_simple_group_copy(dest, src);
  85. }
  86. int ec_GFp_nist_group_set_curve(EC_GROUP *group, const BIGNUM *p,
  87. const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
  88. {
  89. int ret = 0;
  90. BN_CTX *new_ctx = NULL;
  91. if (ctx == NULL)
  92. if ((ctx = new_ctx = BN_CTX_new_ex(group->libctx)) == NULL)
  93. return 0;
  94. BN_CTX_start(ctx);
  95. if (BN_ucmp(BN_get0_nist_prime_192(), p) == 0)
  96. group->field_mod_func = BN_nist_mod_192;
  97. else if (BN_ucmp(BN_get0_nist_prime_224(), p) == 0)
  98. group->field_mod_func = BN_nist_mod_224;
  99. else if (BN_ucmp(BN_get0_nist_prime_256(), p) == 0)
  100. group->field_mod_func = BN_nist_mod_256;
  101. else if (BN_ucmp(BN_get0_nist_prime_384(), p) == 0)
  102. group->field_mod_func = BN_nist_mod_384;
  103. else if (BN_ucmp(BN_get0_nist_prime_521(), p) == 0)
  104. group->field_mod_func = BN_nist_mod_521;
  105. else {
  106. ECerr(EC_F_EC_GFP_NIST_GROUP_SET_CURVE, EC_R_NOT_A_NIST_PRIME);
  107. goto err;
  108. }
  109. ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
  110. err:
  111. BN_CTX_end(ctx);
  112. BN_CTX_free(new_ctx);
  113. return ret;
  114. }
  115. int ec_GFp_nist_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
  116. const BIGNUM *b, BN_CTX *ctx)
  117. {
  118. int ret = 0;
  119. BN_CTX *ctx_new = NULL;
  120. if (!group || !r || !a || !b) {
  121. ECerr(EC_F_EC_GFP_NIST_FIELD_MUL, ERR_R_PASSED_NULL_PARAMETER);
  122. goto err;
  123. }
  124. if (!ctx)
  125. if ((ctx_new = ctx = BN_CTX_new_ex(group->libctx)) == NULL)
  126. goto err;
  127. if (!BN_mul(r, a, b, ctx))
  128. goto err;
  129. if (!group->field_mod_func(r, r, group->field, ctx))
  130. goto err;
  131. ret = 1;
  132. err:
  133. BN_CTX_free(ctx_new);
  134. return ret;
  135. }
  136. int ec_GFp_nist_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
  137. BN_CTX *ctx)
  138. {
  139. int ret = 0;
  140. BN_CTX *ctx_new = NULL;
  141. if (!group || !r || !a) {
  142. ECerr(EC_F_EC_GFP_NIST_FIELD_SQR, EC_R_PASSED_NULL_PARAMETER);
  143. goto err;
  144. }
  145. if (!ctx)
  146. if ((ctx_new = ctx = BN_CTX_new_ex(group->libctx)) == NULL)
  147. goto err;
  148. if (!BN_sqr(r, a, ctx))
  149. goto err;
  150. if (!group->field_mod_func(r, r, group->field, ctx))
  151. goto err;
  152. ret = 1;
  153. err:
  154. BN_CTX_free(ctx_new);
  155. return ret;
  156. }