ecp_mont.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Copyright 2001-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
  4. *
  5. * Licensed under the OpenSSL license (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 <openssl/err.h>
  11. #include "ec_lcl.h"
  12. const EC_METHOD *EC_GFp_mont_method(void)
  13. {
  14. static const EC_METHOD ret = {
  15. EC_FLAGS_DEFAULT_OCT,
  16. NID_X9_62_prime_field,
  17. ec_GFp_mont_group_init,
  18. ec_GFp_mont_group_finish,
  19. ec_GFp_mont_group_clear_finish,
  20. ec_GFp_mont_group_copy,
  21. ec_GFp_mont_group_set_curve,
  22. ec_GFp_simple_group_get_curve,
  23. ec_GFp_simple_group_get_degree,
  24. ec_group_simple_order_bits,
  25. ec_GFp_simple_group_check_discriminant,
  26. ec_GFp_simple_point_init,
  27. ec_GFp_simple_point_finish,
  28. ec_GFp_simple_point_clear_finish,
  29. ec_GFp_simple_point_copy,
  30. ec_GFp_simple_point_set_to_infinity,
  31. ec_GFp_simple_set_Jprojective_coordinates_GFp,
  32. ec_GFp_simple_get_Jprojective_coordinates_GFp,
  33. ec_GFp_simple_point_set_affine_coordinates,
  34. ec_GFp_simple_point_get_affine_coordinates,
  35. 0, 0, 0,
  36. ec_GFp_simple_add,
  37. ec_GFp_simple_dbl,
  38. ec_GFp_simple_invert,
  39. ec_GFp_simple_is_at_infinity,
  40. ec_GFp_simple_is_on_curve,
  41. ec_GFp_simple_cmp,
  42. ec_GFp_simple_make_affine,
  43. ec_GFp_simple_points_make_affine,
  44. 0 /* mul */ ,
  45. 0 /* precompute_mult */ ,
  46. 0 /* have_precompute_mult */ ,
  47. ec_GFp_mont_field_mul,
  48. ec_GFp_mont_field_sqr,
  49. 0 /* field_div */ ,
  50. ec_GFp_mont_field_encode,
  51. ec_GFp_mont_field_decode,
  52. ec_GFp_mont_field_set_to_one,
  53. ec_key_simple_priv2oct,
  54. ec_key_simple_oct2priv,
  55. 0, /* set private */
  56. ec_key_simple_generate_key,
  57. ec_key_simple_check_key,
  58. ec_key_simple_generate_public_key,
  59. 0, /* keycopy */
  60. 0, /* keyfinish */
  61. ecdh_simple_compute_key,
  62. 0, /* field_inverse_mod_ord */
  63. ec_GFp_simple_blind_coordinates
  64. };
  65. return &ret;
  66. }
  67. int ec_GFp_mont_group_init(EC_GROUP *group)
  68. {
  69. int ok;
  70. ok = ec_GFp_simple_group_init(group);
  71. group->field_data1 = NULL;
  72. group->field_data2 = NULL;
  73. return ok;
  74. }
  75. void ec_GFp_mont_group_finish(EC_GROUP *group)
  76. {
  77. BN_MONT_CTX_free(group->field_data1);
  78. group->field_data1 = NULL;
  79. BN_free(group->field_data2);
  80. group->field_data2 = NULL;
  81. ec_GFp_simple_group_finish(group);
  82. }
  83. void ec_GFp_mont_group_clear_finish(EC_GROUP *group)
  84. {
  85. BN_MONT_CTX_free(group->field_data1);
  86. group->field_data1 = NULL;
  87. BN_clear_free(group->field_data2);
  88. group->field_data2 = NULL;
  89. ec_GFp_simple_group_clear_finish(group);
  90. }
  91. int ec_GFp_mont_group_copy(EC_GROUP *dest, const EC_GROUP *src)
  92. {
  93. BN_MONT_CTX_free(dest->field_data1);
  94. dest->field_data1 = NULL;
  95. BN_clear_free(dest->field_data2);
  96. dest->field_data2 = NULL;
  97. if (!ec_GFp_simple_group_copy(dest, src))
  98. return 0;
  99. if (src->field_data1 != NULL) {
  100. dest->field_data1 = BN_MONT_CTX_new();
  101. if (dest->field_data1 == NULL)
  102. return 0;
  103. if (!BN_MONT_CTX_copy(dest->field_data1, src->field_data1))
  104. goto err;
  105. }
  106. if (src->field_data2 != NULL) {
  107. dest->field_data2 = BN_dup(src->field_data2);
  108. if (dest->field_data2 == NULL)
  109. goto err;
  110. }
  111. return 1;
  112. err:
  113. BN_MONT_CTX_free(dest->field_data1);
  114. dest->field_data1 = NULL;
  115. return 0;
  116. }
  117. int ec_GFp_mont_group_set_curve(EC_GROUP *group, const BIGNUM *p,
  118. const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
  119. {
  120. BN_CTX *new_ctx = NULL;
  121. BN_MONT_CTX *mont = NULL;
  122. BIGNUM *one = NULL;
  123. int ret = 0;
  124. BN_MONT_CTX_free(group->field_data1);
  125. group->field_data1 = NULL;
  126. BN_free(group->field_data2);
  127. group->field_data2 = NULL;
  128. if (ctx == NULL) {
  129. ctx = new_ctx = BN_CTX_new();
  130. if (ctx == NULL)
  131. return 0;
  132. }
  133. mont = BN_MONT_CTX_new();
  134. if (mont == NULL)
  135. goto err;
  136. if (!BN_MONT_CTX_set(mont, p, ctx)) {
  137. ECerr(EC_F_EC_GFP_MONT_GROUP_SET_CURVE, ERR_R_BN_LIB);
  138. goto err;
  139. }
  140. one = BN_new();
  141. if (one == NULL)
  142. goto err;
  143. if (!BN_to_montgomery(one, BN_value_one(), mont, ctx))
  144. goto err;
  145. group->field_data1 = mont;
  146. mont = NULL;
  147. group->field_data2 = one;
  148. one = NULL;
  149. ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
  150. if (!ret) {
  151. BN_MONT_CTX_free(group->field_data1);
  152. group->field_data1 = NULL;
  153. BN_free(group->field_data2);
  154. group->field_data2 = NULL;
  155. }
  156. err:
  157. BN_free(one);
  158. BN_CTX_free(new_ctx);
  159. BN_MONT_CTX_free(mont);
  160. return ret;
  161. }
  162. int ec_GFp_mont_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
  163. const BIGNUM *b, BN_CTX *ctx)
  164. {
  165. if (group->field_data1 == NULL) {
  166. ECerr(EC_F_EC_GFP_MONT_FIELD_MUL, EC_R_NOT_INITIALIZED);
  167. return 0;
  168. }
  169. return BN_mod_mul_montgomery(r, a, b, group->field_data1, ctx);
  170. }
  171. int ec_GFp_mont_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
  172. BN_CTX *ctx)
  173. {
  174. if (group->field_data1 == NULL) {
  175. ECerr(EC_F_EC_GFP_MONT_FIELD_SQR, EC_R_NOT_INITIALIZED);
  176. return 0;
  177. }
  178. return BN_mod_mul_montgomery(r, a, a, group->field_data1, ctx);
  179. }
  180. int ec_GFp_mont_field_encode(const EC_GROUP *group, BIGNUM *r,
  181. const BIGNUM *a, BN_CTX *ctx)
  182. {
  183. if (group->field_data1 == NULL) {
  184. ECerr(EC_F_EC_GFP_MONT_FIELD_ENCODE, EC_R_NOT_INITIALIZED);
  185. return 0;
  186. }
  187. return BN_to_montgomery(r, a, (BN_MONT_CTX *)group->field_data1, ctx);
  188. }
  189. int ec_GFp_mont_field_decode(const EC_GROUP *group, BIGNUM *r,
  190. const BIGNUM *a, BN_CTX *ctx)
  191. {
  192. if (group->field_data1 == NULL) {
  193. ECerr(EC_F_EC_GFP_MONT_FIELD_DECODE, EC_R_NOT_INITIALIZED);
  194. return 0;
  195. }
  196. return BN_from_montgomery(r, a, group->field_data1, ctx);
  197. }
  198. int ec_GFp_mont_field_set_to_one(const EC_GROUP *group, BIGNUM *r,
  199. BN_CTX *ctx)
  200. {
  201. if (group->field_data2 == NULL) {
  202. ECerr(EC_F_EC_GFP_MONT_FIELD_SET_TO_ONE, EC_R_NOT_INITIALIZED);
  203. return 0;
  204. }
  205. if (!BN_copy(r, group->field_data2))
  206. return 0;
  207. return 1;
  208. }