2
0

ecp_mont.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * Copyright 2001-2021 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 <openssl/err.h>
  16. #include "ec_local.h"
  17. const EC_METHOD *EC_GFp_mont_method(void)
  18. {
  19. static const EC_METHOD ret = {
  20. EC_FLAGS_DEFAULT_OCT,
  21. NID_X9_62_prime_field,
  22. ossl_ec_GFp_mont_group_init,
  23. ossl_ec_GFp_mont_group_finish,
  24. ossl_ec_GFp_mont_group_clear_finish,
  25. ossl_ec_GFp_mont_group_copy,
  26. ossl_ec_GFp_mont_group_set_curve,
  27. ossl_ec_GFp_simple_group_get_curve,
  28. ossl_ec_GFp_simple_group_get_degree,
  29. ossl_ec_group_simple_order_bits,
  30. ossl_ec_GFp_simple_group_check_discriminant,
  31. ossl_ec_GFp_simple_point_init,
  32. ossl_ec_GFp_simple_point_finish,
  33. ossl_ec_GFp_simple_point_clear_finish,
  34. ossl_ec_GFp_simple_point_copy,
  35. ossl_ec_GFp_simple_point_set_to_infinity,
  36. ossl_ec_GFp_simple_point_set_affine_coordinates,
  37. ossl_ec_GFp_simple_point_get_affine_coordinates,
  38. 0, 0, 0,
  39. ossl_ec_GFp_simple_add,
  40. ossl_ec_GFp_simple_dbl,
  41. ossl_ec_GFp_simple_invert,
  42. ossl_ec_GFp_simple_is_at_infinity,
  43. ossl_ec_GFp_simple_is_on_curve,
  44. ossl_ec_GFp_simple_cmp,
  45. ossl_ec_GFp_simple_make_affine,
  46. ossl_ec_GFp_simple_points_make_affine,
  47. 0 /* mul */ ,
  48. 0 /* precompute_mult */ ,
  49. 0 /* have_precompute_mult */ ,
  50. ossl_ec_GFp_mont_field_mul,
  51. ossl_ec_GFp_mont_field_sqr,
  52. 0 /* field_div */ ,
  53. ossl_ec_GFp_mont_field_inv,
  54. ossl_ec_GFp_mont_field_encode,
  55. ossl_ec_GFp_mont_field_decode,
  56. ossl_ec_GFp_mont_field_set_to_one,
  57. ossl_ec_key_simple_priv2oct,
  58. ossl_ec_key_simple_oct2priv,
  59. 0, /* set private */
  60. ossl_ec_key_simple_generate_key,
  61. ossl_ec_key_simple_check_key,
  62. ossl_ec_key_simple_generate_public_key,
  63. 0, /* keycopy */
  64. 0, /* keyfinish */
  65. ossl_ecdh_simple_compute_key,
  66. ossl_ecdsa_simple_sign_setup,
  67. ossl_ecdsa_simple_sign_sig,
  68. ossl_ecdsa_simple_verify_sig,
  69. 0, /* field_inverse_mod_ord */
  70. ossl_ec_GFp_simple_blind_coordinates,
  71. ossl_ec_GFp_simple_ladder_pre,
  72. ossl_ec_GFp_simple_ladder_step,
  73. ossl_ec_GFp_simple_ladder_post
  74. };
  75. return &ret;
  76. }
  77. int ossl_ec_GFp_mont_group_init(EC_GROUP *group)
  78. {
  79. int ok;
  80. ok = ossl_ec_GFp_simple_group_init(group);
  81. group->field_data1 = NULL;
  82. group->field_data2 = NULL;
  83. return ok;
  84. }
  85. void ossl_ec_GFp_mont_group_finish(EC_GROUP *group)
  86. {
  87. BN_MONT_CTX_free(group->field_data1);
  88. group->field_data1 = NULL;
  89. BN_free(group->field_data2);
  90. group->field_data2 = NULL;
  91. ossl_ec_GFp_simple_group_finish(group);
  92. }
  93. void ossl_ec_GFp_mont_group_clear_finish(EC_GROUP *group)
  94. {
  95. BN_MONT_CTX_free(group->field_data1);
  96. group->field_data1 = NULL;
  97. BN_clear_free(group->field_data2);
  98. group->field_data2 = NULL;
  99. ossl_ec_GFp_simple_group_clear_finish(group);
  100. }
  101. int ossl_ec_GFp_mont_group_copy(EC_GROUP *dest, const EC_GROUP *src)
  102. {
  103. BN_MONT_CTX_free(dest->field_data1);
  104. dest->field_data1 = NULL;
  105. BN_clear_free(dest->field_data2);
  106. dest->field_data2 = NULL;
  107. if (!ossl_ec_GFp_simple_group_copy(dest, src))
  108. return 0;
  109. if (src->field_data1 != NULL) {
  110. dest->field_data1 = BN_MONT_CTX_new();
  111. if (dest->field_data1 == NULL)
  112. return 0;
  113. if (!BN_MONT_CTX_copy(dest->field_data1, src->field_data1))
  114. goto err;
  115. }
  116. if (src->field_data2 != NULL) {
  117. dest->field_data2 = BN_dup(src->field_data2);
  118. if (dest->field_data2 == NULL)
  119. goto err;
  120. }
  121. return 1;
  122. err:
  123. BN_MONT_CTX_free(dest->field_data1);
  124. dest->field_data1 = NULL;
  125. return 0;
  126. }
  127. int ossl_ec_GFp_mont_group_set_curve(EC_GROUP *group, const BIGNUM *p,
  128. const BIGNUM *a, const BIGNUM *b,
  129. BN_CTX *ctx)
  130. {
  131. BN_CTX *new_ctx = NULL;
  132. BN_MONT_CTX *mont = NULL;
  133. BIGNUM *one = NULL;
  134. int ret = 0;
  135. BN_MONT_CTX_free(group->field_data1);
  136. group->field_data1 = NULL;
  137. BN_free(group->field_data2);
  138. group->field_data2 = NULL;
  139. if (ctx == NULL) {
  140. ctx = new_ctx = BN_CTX_new_ex(group->libctx);
  141. if (ctx == NULL)
  142. return 0;
  143. }
  144. mont = BN_MONT_CTX_new();
  145. if (mont == NULL)
  146. goto err;
  147. if (!BN_MONT_CTX_set(mont, p, ctx)) {
  148. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  149. goto err;
  150. }
  151. one = BN_new();
  152. if (one == NULL)
  153. goto err;
  154. if (!BN_to_montgomery(one, BN_value_one(), mont, ctx))
  155. goto err;
  156. group->field_data1 = mont;
  157. mont = NULL;
  158. group->field_data2 = one;
  159. one = NULL;
  160. ret = ossl_ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
  161. if (!ret) {
  162. BN_MONT_CTX_free(group->field_data1);
  163. group->field_data1 = NULL;
  164. BN_free(group->field_data2);
  165. group->field_data2 = NULL;
  166. }
  167. err:
  168. BN_free(one);
  169. BN_CTX_free(new_ctx);
  170. BN_MONT_CTX_free(mont);
  171. return ret;
  172. }
  173. int ossl_ec_GFp_mont_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
  174. const BIGNUM *b, BN_CTX *ctx)
  175. {
  176. if (group->field_data1 == NULL) {
  177. ERR_raise(ERR_LIB_EC, EC_R_NOT_INITIALIZED);
  178. return 0;
  179. }
  180. return BN_mod_mul_montgomery(r, a, b, group->field_data1, ctx);
  181. }
  182. int ossl_ec_GFp_mont_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
  183. BN_CTX *ctx)
  184. {
  185. if (group->field_data1 == NULL) {
  186. ERR_raise(ERR_LIB_EC, EC_R_NOT_INITIALIZED);
  187. return 0;
  188. }
  189. return BN_mod_mul_montgomery(r, a, a, group->field_data1, ctx);
  190. }
  191. /*-
  192. * Computes the multiplicative inverse of a in GF(p), storing the result in r.
  193. * If a is zero (or equivalent), you'll get a EC_R_CANNOT_INVERT error.
  194. * We have a Mont structure, so SCA hardening is FLT inversion.
  195. */
  196. int ossl_ec_GFp_mont_field_inv(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
  197. BN_CTX *ctx)
  198. {
  199. BIGNUM *e = NULL;
  200. BN_CTX *new_ctx = NULL;
  201. int ret = 0;
  202. if (group->field_data1 == NULL)
  203. return 0;
  204. if (ctx == NULL
  205. && (ctx = new_ctx = BN_CTX_secure_new_ex(group->libctx)) == NULL)
  206. return 0;
  207. BN_CTX_start(ctx);
  208. if ((e = BN_CTX_get(ctx)) == NULL)
  209. goto err;
  210. /* Inverse in constant time with Fermats Little Theorem */
  211. if (!BN_set_word(e, 2))
  212. goto err;
  213. if (!BN_sub(e, group->field, e))
  214. goto err;
  215. /*-
  216. * Exponent e is public.
  217. * No need for scatter-gather or BN_FLG_CONSTTIME.
  218. */
  219. if (!BN_mod_exp_mont(r, a, e, group->field, ctx, group->field_data1))
  220. goto err;
  221. /* throw an error on zero */
  222. if (BN_is_zero(r)) {
  223. ERR_raise(ERR_LIB_EC, EC_R_CANNOT_INVERT);
  224. goto err;
  225. }
  226. ret = 1;
  227. err:
  228. BN_CTX_end(ctx);
  229. BN_CTX_free(new_ctx);
  230. return ret;
  231. }
  232. int ossl_ec_GFp_mont_field_encode(const EC_GROUP *group, BIGNUM *r,
  233. const BIGNUM *a, BN_CTX *ctx)
  234. {
  235. if (group->field_data1 == NULL) {
  236. ERR_raise(ERR_LIB_EC, EC_R_NOT_INITIALIZED);
  237. return 0;
  238. }
  239. return BN_to_montgomery(r, a, (BN_MONT_CTX *)group->field_data1, ctx);
  240. }
  241. int ossl_ec_GFp_mont_field_decode(const EC_GROUP *group, BIGNUM *r,
  242. const BIGNUM *a, BN_CTX *ctx)
  243. {
  244. if (group->field_data1 == NULL) {
  245. ERR_raise(ERR_LIB_EC, EC_R_NOT_INITIALIZED);
  246. return 0;
  247. }
  248. return BN_from_montgomery(r, a, group->field_data1, ctx);
  249. }
  250. int ossl_ec_GFp_mont_field_set_to_one(const EC_GROUP *group, BIGNUM *r,
  251. BN_CTX *ctx)
  252. {
  253. if (group->field_data2 == NULL) {
  254. ERR_raise(ERR_LIB_EC, EC_R_NOT_INITIALIZED);
  255. return 0;
  256. }
  257. if (!BN_copy(r, group->field_data2))
  258. return 0;
  259. return 1;
  260. }