2
0

ecp_mont.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Copyright 2001-2019 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_local.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_inv,
  51. ec_GFp_mont_field_encode,
  52. ec_GFp_mont_field_decode,
  53. ec_GFp_mont_field_set_to_one,
  54. ec_key_simple_priv2oct,
  55. ec_key_simple_oct2priv,
  56. 0, /* set private */
  57. ec_key_simple_generate_key,
  58. ec_key_simple_check_key,
  59. ec_key_simple_generate_public_key,
  60. 0, /* keycopy */
  61. 0, /* keyfinish */
  62. ecdh_simple_compute_key,
  63. 0, /* field_inverse_mod_ord */
  64. ec_GFp_simple_blind_coordinates,
  65. ec_GFp_simple_ladder_pre,
  66. ec_GFp_simple_ladder_step,
  67. ec_GFp_simple_ladder_post
  68. };
  69. return &ret;
  70. }
  71. int ec_GFp_mont_group_init(EC_GROUP *group)
  72. {
  73. int ok;
  74. ok = ec_GFp_simple_group_init(group);
  75. group->field_data1 = NULL;
  76. group->field_data2 = NULL;
  77. return ok;
  78. }
  79. void ec_GFp_mont_group_finish(EC_GROUP *group)
  80. {
  81. BN_MONT_CTX_free(group->field_data1);
  82. group->field_data1 = NULL;
  83. BN_free(group->field_data2);
  84. group->field_data2 = NULL;
  85. ec_GFp_simple_group_finish(group);
  86. }
  87. void ec_GFp_mont_group_clear_finish(EC_GROUP *group)
  88. {
  89. BN_MONT_CTX_free(group->field_data1);
  90. group->field_data1 = NULL;
  91. BN_clear_free(group->field_data2);
  92. group->field_data2 = NULL;
  93. ec_GFp_simple_group_clear_finish(group);
  94. }
  95. int ec_GFp_mont_group_copy(EC_GROUP *dest, const EC_GROUP *src)
  96. {
  97. BN_MONT_CTX_free(dest->field_data1);
  98. dest->field_data1 = NULL;
  99. BN_clear_free(dest->field_data2);
  100. dest->field_data2 = NULL;
  101. if (!ec_GFp_simple_group_copy(dest, src))
  102. return 0;
  103. if (src->field_data1 != NULL) {
  104. dest->field_data1 = BN_MONT_CTX_new();
  105. if (dest->field_data1 == NULL)
  106. return 0;
  107. if (!BN_MONT_CTX_copy(dest->field_data1, src->field_data1))
  108. goto err;
  109. }
  110. if (src->field_data2 != NULL) {
  111. dest->field_data2 = BN_dup(src->field_data2);
  112. if (dest->field_data2 == NULL)
  113. goto err;
  114. }
  115. return 1;
  116. err:
  117. BN_MONT_CTX_free(dest->field_data1);
  118. dest->field_data1 = NULL;
  119. return 0;
  120. }
  121. int ec_GFp_mont_group_set_curve(EC_GROUP *group, const BIGNUM *p,
  122. const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
  123. {
  124. BN_CTX *new_ctx = NULL;
  125. BN_MONT_CTX *mont = NULL;
  126. BIGNUM *one = NULL;
  127. int ret = 0;
  128. BN_MONT_CTX_free(group->field_data1);
  129. group->field_data1 = NULL;
  130. BN_free(group->field_data2);
  131. group->field_data2 = NULL;
  132. if (ctx == NULL) {
  133. ctx = new_ctx = BN_CTX_new();
  134. if (ctx == NULL)
  135. return 0;
  136. }
  137. mont = BN_MONT_CTX_new();
  138. if (mont == NULL)
  139. goto err;
  140. if (!BN_MONT_CTX_set(mont, p, ctx)) {
  141. ECerr(EC_F_EC_GFP_MONT_GROUP_SET_CURVE, ERR_R_BN_LIB);
  142. goto err;
  143. }
  144. one = BN_new();
  145. if (one == NULL)
  146. goto err;
  147. if (!BN_to_montgomery(one, BN_value_one(), mont, ctx))
  148. goto err;
  149. group->field_data1 = mont;
  150. mont = NULL;
  151. group->field_data2 = one;
  152. one = NULL;
  153. ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
  154. if (!ret) {
  155. BN_MONT_CTX_free(group->field_data1);
  156. group->field_data1 = NULL;
  157. BN_free(group->field_data2);
  158. group->field_data2 = NULL;
  159. }
  160. err:
  161. BN_free(one);
  162. BN_CTX_free(new_ctx);
  163. BN_MONT_CTX_free(mont);
  164. return ret;
  165. }
  166. int ec_GFp_mont_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
  167. const BIGNUM *b, BN_CTX *ctx)
  168. {
  169. if (group->field_data1 == NULL) {
  170. ECerr(EC_F_EC_GFP_MONT_FIELD_MUL, EC_R_NOT_INITIALIZED);
  171. return 0;
  172. }
  173. return BN_mod_mul_montgomery(r, a, b, group->field_data1, ctx);
  174. }
  175. int ec_GFp_mont_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
  176. BN_CTX *ctx)
  177. {
  178. if (group->field_data1 == NULL) {
  179. ECerr(EC_F_EC_GFP_MONT_FIELD_SQR, EC_R_NOT_INITIALIZED);
  180. return 0;
  181. }
  182. return BN_mod_mul_montgomery(r, a, a, group->field_data1, ctx);
  183. }
  184. /*-
  185. * Computes the multiplicative inverse of a in GF(p), storing the result in r.
  186. * If a is zero (or equivalent), you'll get a EC_R_CANNOT_INVERT error.
  187. * We have a Mont structure, so SCA hardening is FLT inversion.
  188. */
  189. int ec_GFp_mont_field_inv(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
  190. BN_CTX *ctx)
  191. {
  192. BIGNUM *e = NULL;
  193. BN_CTX *new_ctx = NULL;
  194. int ret = 0;
  195. if (group->field_data1 == NULL)
  196. return 0;
  197. if (ctx == NULL && (ctx = new_ctx = BN_CTX_secure_new()) == NULL)
  198. return 0;
  199. BN_CTX_start(ctx);
  200. if ((e = BN_CTX_get(ctx)) == NULL)
  201. goto err;
  202. /* Inverse in constant time with Fermats Little Theorem */
  203. if (!BN_set_word(e, 2))
  204. goto err;
  205. if (!BN_sub(e, group->field, e))
  206. goto err;
  207. /*-
  208. * Exponent e is public.
  209. * No need for scatter-gather or BN_FLG_CONSTTIME.
  210. */
  211. if (!BN_mod_exp_mont(r, a, e, group->field, ctx, group->field_data1))
  212. goto err;
  213. /* throw an error on zero */
  214. if (BN_is_zero(r)) {
  215. ECerr(EC_F_EC_GFP_MONT_FIELD_INV, EC_R_CANNOT_INVERT);
  216. goto err;
  217. }
  218. ret = 1;
  219. err:
  220. BN_CTX_end(ctx);
  221. BN_CTX_free(new_ctx);
  222. return ret;
  223. }
  224. int ec_GFp_mont_field_encode(const EC_GROUP *group, BIGNUM *r,
  225. const BIGNUM *a, BN_CTX *ctx)
  226. {
  227. if (group->field_data1 == NULL) {
  228. ECerr(EC_F_EC_GFP_MONT_FIELD_ENCODE, EC_R_NOT_INITIALIZED);
  229. return 0;
  230. }
  231. return BN_to_montgomery(r, a, (BN_MONT_CTX *)group->field_data1, ctx);
  232. }
  233. int ec_GFp_mont_field_decode(const EC_GROUP *group, BIGNUM *r,
  234. const BIGNUM *a, BN_CTX *ctx)
  235. {
  236. if (group->field_data1 == NULL) {
  237. ECerr(EC_F_EC_GFP_MONT_FIELD_DECODE, EC_R_NOT_INITIALIZED);
  238. return 0;
  239. }
  240. return BN_from_montgomery(r, a, group->field_data1, ctx);
  241. }
  242. int ec_GFp_mont_field_set_to_one(const EC_GROUP *group, BIGNUM *r,
  243. BN_CTX *ctx)
  244. {
  245. if (group->field_data2 == NULL) {
  246. ECerr(EC_F_EC_GFP_MONT_FIELD_SET_TO_ONE, EC_R_NOT_INITIALIZED);
  247. return 0;
  248. }
  249. if (!BN_copy(r, group->field_data2))
  250. return 0;
  251. return 1;
  252. }