ecp_mont.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. };
  63. return &ret;
  64. }
  65. int ec_GFp_mont_group_init(EC_GROUP *group)
  66. {
  67. int ok;
  68. ok = ec_GFp_simple_group_init(group);
  69. group->field_data1 = NULL;
  70. group->field_data2 = NULL;
  71. return ok;
  72. }
  73. void ec_GFp_mont_group_finish(EC_GROUP *group)
  74. {
  75. BN_MONT_CTX_free(group->field_data1);
  76. group->field_data1 = NULL;
  77. BN_free(group->field_data2);
  78. group->field_data2 = NULL;
  79. ec_GFp_simple_group_finish(group);
  80. }
  81. void ec_GFp_mont_group_clear_finish(EC_GROUP *group)
  82. {
  83. BN_MONT_CTX_free(group->field_data1);
  84. group->field_data1 = NULL;
  85. BN_clear_free(group->field_data2);
  86. group->field_data2 = NULL;
  87. ec_GFp_simple_group_clear_finish(group);
  88. }
  89. int ec_GFp_mont_group_copy(EC_GROUP *dest, const EC_GROUP *src)
  90. {
  91. BN_MONT_CTX_free(dest->field_data1);
  92. dest->field_data1 = NULL;
  93. BN_clear_free(dest->field_data2);
  94. dest->field_data2 = NULL;
  95. if (!ec_GFp_simple_group_copy(dest, src))
  96. return 0;
  97. if (src->field_data1 != NULL) {
  98. dest->field_data1 = BN_MONT_CTX_new();
  99. if (dest->field_data1 == NULL)
  100. return 0;
  101. if (!BN_MONT_CTX_copy(dest->field_data1, src->field_data1))
  102. goto err;
  103. }
  104. if (src->field_data2 != NULL) {
  105. dest->field_data2 = BN_dup(src->field_data2);
  106. if (dest->field_data2 == NULL)
  107. goto err;
  108. }
  109. return 1;
  110. err:
  111. BN_MONT_CTX_free(dest->field_data1);
  112. dest->field_data1 = NULL;
  113. return 0;
  114. }
  115. int ec_GFp_mont_group_set_curve(EC_GROUP *group, const BIGNUM *p,
  116. const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
  117. {
  118. BN_CTX *new_ctx = NULL;
  119. BN_MONT_CTX *mont = NULL;
  120. BIGNUM *one = NULL;
  121. int ret = 0;
  122. BN_MONT_CTX_free(group->field_data1);
  123. group->field_data1 = NULL;
  124. BN_free(group->field_data2);
  125. group->field_data2 = NULL;
  126. if (ctx == NULL) {
  127. ctx = new_ctx = BN_CTX_new();
  128. if (ctx == NULL)
  129. return 0;
  130. }
  131. mont = BN_MONT_CTX_new();
  132. if (mont == NULL)
  133. goto err;
  134. if (!BN_MONT_CTX_set(mont, p, ctx)) {
  135. ECerr(EC_F_EC_GFP_MONT_GROUP_SET_CURVE, ERR_R_BN_LIB);
  136. goto err;
  137. }
  138. one = BN_new();
  139. if (one == NULL)
  140. goto err;
  141. if (!BN_to_montgomery(one, BN_value_one(), mont, ctx))
  142. goto err;
  143. group->field_data1 = mont;
  144. mont = NULL;
  145. group->field_data2 = one;
  146. one = NULL;
  147. ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
  148. if (!ret) {
  149. BN_MONT_CTX_free(group->field_data1);
  150. group->field_data1 = NULL;
  151. BN_free(group->field_data2);
  152. group->field_data2 = NULL;
  153. }
  154. err:
  155. BN_free(one);
  156. BN_CTX_free(new_ctx);
  157. BN_MONT_CTX_free(mont);
  158. return ret;
  159. }
  160. int ec_GFp_mont_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
  161. const BIGNUM *b, BN_CTX *ctx)
  162. {
  163. if (group->field_data1 == NULL) {
  164. ECerr(EC_F_EC_GFP_MONT_FIELD_MUL, EC_R_NOT_INITIALIZED);
  165. return 0;
  166. }
  167. return BN_mod_mul_montgomery(r, a, b, group->field_data1, ctx);
  168. }
  169. int ec_GFp_mont_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
  170. BN_CTX *ctx)
  171. {
  172. if (group->field_data1 == NULL) {
  173. ECerr(EC_F_EC_GFP_MONT_FIELD_SQR, EC_R_NOT_INITIALIZED);
  174. return 0;
  175. }
  176. return BN_mod_mul_montgomery(r, a, a, group->field_data1, ctx);
  177. }
  178. int ec_GFp_mont_field_encode(const EC_GROUP *group, BIGNUM *r,
  179. const BIGNUM *a, BN_CTX *ctx)
  180. {
  181. if (group->field_data1 == NULL) {
  182. ECerr(EC_F_EC_GFP_MONT_FIELD_ENCODE, EC_R_NOT_INITIALIZED);
  183. return 0;
  184. }
  185. return BN_to_montgomery(r, a, (BN_MONT_CTX *)group->field_data1, ctx);
  186. }
  187. int ec_GFp_mont_field_decode(const EC_GROUP *group, BIGNUM *r,
  188. const BIGNUM *a, BN_CTX *ctx)
  189. {
  190. if (group->field_data1 == NULL) {
  191. ECerr(EC_F_EC_GFP_MONT_FIELD_DECODE, EC_R_NOT_INITIALIZED);
  192. return 0;
  193. }
  194. return BN_from_montgomery(r, a, group->field_data1, ctx);
  195. }
  196. int ec_GFp_mont_field_set_to_one(const EC_GROUP *group, BIGNUM *r,
  197. BN_CTX *ctx)
  198. {
  199. if (group->field_data2 == NULL) {
  200. ECerr(EC_F_EC_GFP_MONT_FIELD_SET_TO_ONE, EC_R_NOT_INITIALIZED);
  201. return 0;
  202. }
  203. if (!BN_copy(r, group->field_data2))
  204. return 0;
  205. return 1;
  206. }