ecp_nist.c 4.9 KB

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