ecp_mont.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Copyright 2001-2020 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. ec_GFp_mont_group_init,
  23. ec_GFp_mont_group_finish,
  24. ec_GFp_mont_group_clear_finish,
  25. ec_GFp_mont_group_copy,
  26. ec_GFp_mont_group_set_curve,
  27. ec_GFp_simple_group_get_curve,
  28. ec_GFp_simple_group_get_degree,
  29. ec_group_simple_order_bits,
  30. ec_GFp_simple_group_check_discriminant,
  31. ec_GFp_simple_point_init,
  32. ec_GFp_simple_point_finish,
  33. ec_GFp_simple_point_clear_finish,
  34. ec_GFp_simple_point_copy,
  35. ec_GFp_simple_point_set_to_infinity,
  36. ec_GFp_simple_point_set_affine_coordinates,
  37. ec_GFp_simple_point_get_affine_coordinates,
  38. 0, 0, 0,
  39. ec_GFp_simple_add,
  40. ec_GFp_simple_dbl,
  41. ec_GFp_simple_invert,
  42. ec_GFp_simple_is_at_infinity,
  43. ec_GFp_simple_is_on_curve,
  44. ec_GFp_simple_cmp,
  45. ec_GFp_simple_make_affine,
  46. ec_GFp_simple_points_make_affine,
  47. 0 /* mul */ ,
  48. 0 /* precompute_mult */ ,
  49. 0 /* have_precompute_mult */ ,
  50. ec_GFp_mont_field_mul,
  51. ec_GFp_mont_field_sqr,
  52. 0 /* field_div */ ,
  53. ec_GFp_mont_field_inv,
  54. ec_GFp_mont_field_encode,
  55. ec_GFp_mont_field_decode,
  56. ec_GFp_mont_field_set_to_one,
  57. ec_key_simple_priv2oct,
  58. ec_key_simple_oct2priv,
  59. 0, /* set private */
  60. ec_key_simple_generate_key,
  61. ec_key_simple_check_key,
  62. ec_key_simple_generate_public_key,
  63. 0, /* keycopy */
  64. 0, /* keyfinish */
  65. ecdh_simple_compute_key,
  66. ecdsa_simple_sign_setup,
  67. ecdsa_simple_sign_sig,
  68. ecdsa_simple_verify_sig,
  69. 0, /* field_inverse_mod_ord */
  70. ec_GFp_simple_blind_coordinates,
  71. ec_GFp_simple_ladder_pre,
  72. ec_GFp_simple_ladder_step,
  73. ec_GFp_simple_ladder_post
  74. };
  75. return &ret;
  76. }
  77. int ec_GFp_mont_group_init(EC_GROUP *group)
  78. {
  79. int ok;
  80. ok = ec_GFp_simple_group_init(group);
  81. group->field_data1 = NULL;
  82. group->field_data2 = NULL;
  83. return ok;
  84. }
  85. void 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. ec_GFp_simple_group_finish(group);
  92. }
  93. void 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. ec_GFp_simple_group_clear_finish(group);
  100. }
  101. int 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 (!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 ec_GFp_mont_group_set_curve(EC_GROUP *group, const BIGNUM *p,
  128. const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
  129. {
  130. BN_CTX *new_ctx = NULL;
  131. BN_MONT_CTX *mont = NULL;
  132. BIGNUM *one = NULL;
  133. int ret = 0;
  134. BN_MONT_CTX_free(group->field_data1);
  135. group->field_data1 = NULL;
  136. BN_free(group->field_data2);
  137. group->field_data2 = NULL;
  138. if (ctx == NULL) {
  139. ctx = new_ctx = BN_CTX_new_ex(group->libctx);
  140. if (ctx == NULL)
  141. return 0;
  142. }
  143. mont = BN_MONT_CTX_new();
  144. if (mont == NULL)
  145. goto err;
  146. if (!BN_MONT_CTX_set(mont, p, ctx)) {
  147. ECerr(EC_F_EC_GFP_MONT_GROUP_SET_CURVE, ERR_R_BN_LIB);
  148. goto err;
  149. }
  150. one = BN_new();
  151. if (one == NULL)
  152. goto err;
  153. if (!BN_to_montgomery(one, BN_value_one(), mont, ctx))
  154. goto err;
  155. group->field_data1 = mont;
  156. mont = NULL;
  157. group->field_data2 = one;
  158. one = NULL;
  159. ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
  160. if (!ret) {
  161. BN_MONT_CTX_free(group->field_data1);
  162. group->field_data1 = NULL;
  163. BN_free(group->field_data2);
  164. group->field_data2 = NULL;
  165. }
  166. err:
  167. BN_free(one);
  168. BN_CTX_free(new_ctx);
  169. BN_MONT_CTX_free(mont);
  170. return ret;
  171. }
  172. int ec_GFp_mont_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
  173. const BIGNUM *b, BN_CTX *ctx)
  174. {
  175. if (group->field_data1 == NULL) {
  176. ECerr(EC_F_EC_GFP_MONT_FIELD_MUL, EC_R_NOT_INITIALIZED);
  177. return 0;
  178. }
  179. return BN_mod_mul_montgomery(r, a, b, group->field_data1, ctx);
  180. }
  181. int ec_GFp_mont_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
  182. BN_CTX *ctx)
  183. {
  184. if (group->field_data1 == NULL) {
  185. ECerr(EC_F_EC_GFP_MONT_FIELD_SQR, EC_R_NOT_INITIALIZED);
  186. return 0;
  187. }
  188. return BN_mod_mul_montgomery(r, a, a, group->field_data1, ctx);
  189. }
  190. /*-
  191. * Computes the multiplicative inverse of a in GF(p), storing the result in r.
  192. * If a is zero (or equivalent), you'll get a EC_R_CANNOT_INVERT error.
  193. * We have a Mont structure, so SCA hardening is FLT inversion.
  194. */
  195. int ec_GFp_mont_field_inv(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
  196. BN_CTX *ctx)
  197. {
  198. BIGNUM *e = NULL;
  199. BN_CTX *new_ctx = NULL;
  200. int ret = 0;
  201. if (group->field_data1 == NULL)
  202. return 0;
  203. if (ctx == NULL
  204. && (ctx = new_ctx = BN_CTX_secure_new_ex(group->libctx)) == NULL)
  205. return 0;
  206. BN_CTX_start(ctx);
  207. if ((e = BN_CTX_get(ctx)) == NULL)
  208. goto err;
  209. /* Inverse in constant time with Fermats Little Theorem */
  210. if (!BN_set_word(e, 2))
  211. goto err;
  212. if (!BN_sub(e, group->field, e))
  213. goto err;
  214. /*-
  215. * Exponent e is public.
  216. * No need for scatter-gather or BN_FLG_CONSTTIME.
  217. */
  218. if (!BN_mod_exp_mont(r, a, e, group->field, ctx, group->field_data1))
  219. goto err;
  220. /* throw an error on zero */
  221. if (BN_is_zero(r)) {
  222. ECerr(EC_F_EC_GFP_MONT_FIELD_INV, EC_R_CANNOT_INVERT);
  223. goto err;
  224. }
  225. ret = 1;
  226. err:
  227. BN_CTX_end(ctx);
  228. BN_CTX_free(new_ctx);
  229. return ret;
  230. }
  231. int ec_GFp_mont_field_encode(const EC_GROUP *group, BIGNUM *r,
  232. const BIGNUM *a, BN_CTX *ctx)
  233. {
  234. if (group->field_data1 == NULL) {
  235. ECerr(EC_F_EC_GFP_MONT_FIELD_ENCODE, EC_R_NOT_INITIALIZED);
  236. return 0;
  237. }
  238. return BN_to_montgomery(r, a, (BN_MONT_CTX *)group->field_data1, ctx);
  239. }
  240. int ec_GFp_mont_field_decode(const EC_GROUP *group, BIGNUM *r,
  241. const BIGNUM *a, BN_CTX *ctx)
  242. {
  243. if (group->field_data1 == NULL) {
  244. ECerr(EC_F_EC_GFP_MONT_FIELD_DECODE, EC_R_NOT_INITIALIZED);
  245. return 0;
  246. }
  247. return BN_from_montgomery(r, a, group->field_data1, ctx);
  248. }
  249. int ec_GFp_mont_field_set_to_one(const EC_GROUP *group, BIGNUM *r,
  250. BN_CTX *ctx)
  251. {
  252. if (group->field_data2 == NULL) {
  253. ECerr(EC_F_EC_GFP_MONT_FIELD_SET_TO_ONE, EC_R_NOT_INITIALIZED);
  254. return 0;
  255. }
  256. if (!BN_copy(r, group->field_data2))
  257. return 0;
  258. return 1;
  259. }