ec_local.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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 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 <stdlib.h>
  11. #include <openssl/obj_mac.h>
  12. #include <openssl/ec.h>
  13. #include <openssl/bn.h>
  14. #include "internal/refcount.h"
  15. #include "crypto/ec.h"
  16. #if defined(__SUNPRO_C)
  17. # if __SUNPRO_C >= 0x520
  18. # pragma error_messages (off,E_ARRAY_OF_INCOMPLETE_NONAME,E_ARRAY_OF_INCOMPLETE)
  19. # endif
  20. #endif
  21. /* Use default functions for poin2oct, oct2point and compressed coordinates */
  22. #define EC_FLAGS_DEFAULT_OCT 0x1
  23. /* Use custom formats for EC_GROUP, EC_POINT and EC_KEY */
  24. #define EC_FLAGS_CUSTOM_CURVE 0x2
  25. /* Curve does not support signing operations */
  26. #define EC_FLAGS_NO_SIGN 0x4
  27. /*
  28. * Structure details are not part of the exported interface, so all this may
  29. * change in future versions.
  30. */
  31. struct ec_method_st {
  32. /* Various method flags */
  33. int flags;
  34. /* used by EC_METHOD_get_field_type: */
  35. int field_type; /* a NID */
  36. /*
  37. * used by EC_GROUP_new, EC_GROUP_free, EC_GROUP_clear_free,
  38. * EC_GROUP_copy:
  39. */
  40. int (*group_init) (EC_GROUP *);
  41. void (*group_finish) (EC_GROUP *);
  42. void (*group_clear_finish) (EC_GROUP *);
  43. int (*group_copy) (EC_GROUP *, const EC_GROUP *);
  44. /* used by EC_GROUP_set_curve, EC_GROUP_get_curve: */
  45. int (*group_set_curve) (EC_GROUP *, const BIGNUM *p, const BIGNUM *a,
  46. const BIGNUM *b, BN_CTX *);
  47. int (*group_get_curve) (const EC_GROUP *, BIGNUM *p, BIGNUM *a, BIGNUM *b,
  48. BN_CTX *);
  49. /* used by EC_GROUP_get_degree: */
  50. int (*group_get_degree) (const EC_GROUP *);
  51. int (*group_order_bits) (const EC_GROUP *);
  52. /* used by EC_GROUP_check: */
  53. int (*group_check_discriminant) (const EC_GROUP *, BN_CTX *);
  54. /*
  55. * used by EC_POINT_new, EC_POINT_free, EC_POINT_clear_free,
  56. * EC_POINT_copy:
  57. */
  58. int (*point_init) (EC_POINT *);
  59. void (*point_finish) (EC_POINT *);
  60. void (*point_clear_finish) (EC_POINT *);
  61. int (*point_copy) (EC_POINT *, const EC_POINT *);
  62. /*-
  63. * used by EC_POINT_set_to_infinity,
  64. * EC_POINT_set_Jprojective_coordinates_GFp,
  65. * EC_POINT_get_Jprojective_coordinates_GFp,
  66. * EC_POINT_set_affine_coordinates,
  67. * EC_POINT_get_affine_coordinates,
  68. * EC_POINT_set_compressed_coordinates:
  69. */
  70. int (*point_set_to_infinity) (const EC_GROUP *, EC_POINT *);
  71. int (*point_set_Jprojective_coordinates_GFp) (const EC_GROUP *,
  72. EC_POINT *, const BIGNUM *x,
  73. const BIGNUM *y,
  74. const BIGNUM *z, BN_CTX *);
  75. int (*point_get_Jprojective_coordinates_GFp) (const EC_GROUP *,
  76. const EC_POINT *, BIGNUM *x,
  77. BIGNUM *y, BIGNUM *z,
  78. BN_CTX *);
  79. int (*point_set_affine_coordinates) (const EC_GROUP *, EC_POINT *,
  80. const BIGNUM *x, const BIGNUM *y,
  81. BN_CTX *);
  82. int (*point_get_affine_coordinates) (const EC_GROUP *, const EC_POINT *,
  83. BIGNUM *x, BIGNUM *y, BN_CTX *);
  84. int (*point_set_compressed_coordinates) (const EC_GROUP *, EC_POINT *,
  85. const BIGNUM *x, int y_bit,
  86. BN_CTX *);
  87. /* used by EC_POINT_point2oct, EC_POINT_oct2point: */
  88. size_t (*point2oct) (const EC_GROUP *, const EC_POINT *,
  89. point_conversion_form_t form, unsigned char *buf,
  90. size_t len, BN_CTX *);
  91. int (*oct2point) (const EC_GROUP *, EC_POINT *, const unsigned char *buf,
  92. size_t len, BN_CTX *);
  93. /* used by EC_POINT_add, EC_POINT_dbl, ECP_POINT_invert: */
  94. int (*add) (const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
  95. const EC_POINT *b, BN_CTX *);
  96. int (*dbl) (const EC_GROUP *, EC_POINT *r, const EC_POINT *a, BN_CTX *);
  97. int (*invert) (const EC_GROUP *, EC_POINT *, BN_CTX *);
  98. /*
  99. * used by EC_POINT_is_at_infinity, EC_POINT_is_on_curve, EC_POINT_cmp:
  100. */
  101. int (*is_at_infinity) (const EC_GROUP *, const EC_POINT *);
  102. int (*is_on_curve) (const EC_GROUP *, const EC_POINT *, BN_CTX *);
  103. int (*point_cmp) (const EC_GROUP *, const EC_POINT *a, const EC_POINT *b,
  104. BN_CTX *);
  105. /* used by EC_POINT_make_affine, EC_POINTs_make_affine: */
  106. int (*make_affine) (const EC_GROUP *, EC_POINT *, BN_CTX *);
  107. int (*points_make_affine) (const EC_GROUP *, size_t num, EC_POINT *[],
  108. BN_CTX *);
  109. /*
  110. * used by EC_POINTs_mul, EC_POINT_mul, EC_POINT_precompute_mult,
  111. * EC_POINT_have_precompute_mult (default implementations are used if the
  112. * 'mul' pointer is 0):
  113. */
  114. /*-
  115. * mul() calculates the value
  116. *
  117. * r := generator * scalar
  118. * + points[0] * scalars[0]
  119. * + ...
  120. * + points[num-1] * scalars[num-1].
  121. *
  122. * For a fixed point multiplication (scalar != NULL, num == 0)
  123. * or a variable point multiplication (scalar == NULL, num == 1),
  124. * mul() must use a constant time algorithm: in both cases callers
  125. * should provide an input scalar (either scalar or scalars[0])
  126. * in the range [0, ec_group_order); for robustness, implementers
  127. * should handle the case when the scalar has not been reduced, but
  128. * may treat it as an unusual input, without any constant-timeness
  129. * guarantee.
  130. */
  131. int (*mul) (const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
  132. size_t num, const EC_POINT *points[], const BIGNUM *scalars[],
  133. BN_CTX *);
  134. int (*precompute_mult) (EC_GROUP *group, BN_CTX *);
  135. int (*have_precompute_mult) (const EC_GROUP *group);
  136. /* internal functions */
  137. /*
  138. * 'field_mul', 'field_sqr', and 'field_div' can be used by 'add' and
  139. * 'dbl' so that the same implementations of point operations can be used
  140. * with different optimized implementations of expensive field
  141. * operations:
  142. */
  143. int (*field_mul) (const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  144. const BIGNUM *b, BN_CTX *);
  145. int (*field_sqr) (const EC_GROUP *, BIGNUM *r, const BIGNUM *a, BN_CTX *);
  146. int (*field_div) (const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  147. const BIGNUM *b, BN_CTX *);
  148. /*-
  149. * 'field_inv' computes the multiplicative inverse of a in the field,
  150. * storing the result in r.
  151. *
  152. * If 'a' is zero (or equivalent), you'll get an EC_R_CANNOT_INVERT error.
  153. */
  154. int (*field_inv) (const EC_GROUP *, BIGNUM *r, const BIGNUM *a, BN_CTX *);
  155. /* e.g. to Montgomery */
  156. int (*field_encode) (const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  157. BN_CTX *);
  158. /* e.g. from Montgomery */
  159. int (*field_decode) (const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  160. BN_CTX *);
  161. int (*field_set_to_one) (const EC_GROUP *, BIGNUM *r, BN_CTX *);
  162. /* private key operations */
  163. size_t (*priv2oct)(const EC_KEY *eckey, unsigned char *buf, size_t len);
  164. int (*oct2priv)(EC_KEY *eckey, const unsigned char *buf, size_t len);
  165. int (*set_private)(EC_KEY *eckey, const BIGNUM *priv_key);
  166. int (*keygen)(EC_KEY *eckey);
  167. int (*keycheck)(const EC_KEY *eckey);
  168. int (*keygenpub)(EC_KEY *eckey);
  169. int (*keycopy)(EC_KEY *dst, const EC_KEY *src);
  170. void (*keyfinish)(EC_KEY *eckey);
  171. /* custom ECDH operation */
  172. int (*ecdh_compute_key)(unsigned char **pout, size_t *poutlen,
  173. const EC_POINT *pub_key, const EC_KEY *ecdh);
  174. /* Inverse modulo order */
  175. int (*field_inverse_mod_ord)(const EC_GROUP *, BIGNUM *r,
  176. const BIGNUM *x, BN_CTX *);
  177. int (*blind_coordinates)(const EC_GROUP *group, EC_POINT *p, BN_CTX *ctx);
  178. int (*ladder_pre)(const EC_GROUP *group,
  179. EC_POINT *r, EC_POINT *s,
  180. EC_POINT *p, BN_CTX *ctx);
  181. int (*ladder_step)(const EC_GROUP *group,
  182. EC_POINT *r, EC_POINT *s,
  183. EC_POINT *p, BN_CTX *ctx);
  184. int (*ladder_post)(const EC_GROUP *group,
  185. EC_POINT *r, EC_POINT *s,
  186. EC_POINT *p, BN_CTX *ctx);
  187. };
  188. /*
  189. * Types and functions to manipulate pre-computed values.
  190. */
  191. typedef struct nistp224_pre_comp_st NISTP224_PRE_COMP;
  192. typedef struct nistp256_pre_comp_st NISTP256_PRE_COMP;
  193. typedef struct nistp521_pre_comp_st NISTP521_PRE_COMP;
  194. typedef struct nistz256_pre_comp_st NISTZ256_PRE_COMP;
  195. typedef struct ec_pre_comp_st EC_PRE_COMP;
  196. struct ec_group_st {
  197. const EC_METHOD *meth;
  198. EC_POINT *generator; /* optional */
  199. BIGNUM *order, *cofactor;
  200. int curve_name; /* optional NID for named curve */
  201. int asn1_flag; /* flag to control the asn1 encoding */
  202. int decoded_from_explicit_params; /* set if decoded from explicit
  203. * curve parameters encoding */
  204. point_conversion_form_t asn1_form;
  205. unsigned char *seed; /* optional seed for parameters (appears in
  206. * ASN1) */
  207. size_t seed_len;
  208. /*
  209. * The following members are handled by the method functions, even if
  210. * they appear generic
  211. */
  212. /*
  213. * Field specification. For curves over GF(p), this is the modulus; for
  214. * curves over GF(2^m), this is the irreducible polynomial defining the
  215. * field.
  216. */
  217. BIGNUM *field;
  218. /*
  219. * Field specification for curves over GF(2^m). The irreducible f(t) is
  220. * then of the form: t^poly[0] + t^poly[1] + ... + t^poly[k] where m =
  221. * poly[0] > poly[1] > ... > poly[k] = 0. The array is terminated with
  222. * poly[k+1]=-1. All elliptic curve irreducibles have at most 5 non-zero
  223. * terms.
  224. */
  225. int poly[6];
  226. /*
  227. * Curve coefficients. (Here the assumption is that BIGNUMs can be used
  228. * or abused for all kinds of fields, not just GF(p).) For characteristic
  229. * > 3, the curve is defined by a Weierstrass equation of the form y^2 =
  230. * x^3 + a*x + b. For characteristic 2, the curve is defined by an
  231. * equation of the form y^2 + x*y = x^3 + a*x^2 + b.
  232. */
  233. BIGNUM *a, *b;
  234. /* enable optimized point arithmetics for special case */
  235. int a_is_minus3;
  236. /* method-specific (e.g., Montgomery structure) */
  237. void *field_data1;
  238. /* method-specific */
  239. void *field_data2;
  240. /* method-specific */
  241. int (*field_mod_func) (BIGNUM *, const BIGNUM *, const BIGNUM *,
  242. BN_CTX *);
  243. /* data for ECDSA inverse */
  244. BN_MONT_CTX *mont_data;
  245. /*
  246. * Precomputed values for speed. The PCT_xxx names match the
  247. * pre_comp.xxx union names; see the SETPRECOMP and HAVEPRECOMP
  248. * macros, below.
  249. */
  250. enum {
  251. PCT_none,
  252. PCT_nistp224, PCT_nistp256, PCT_nistp521, PCT_nistz256,
  253. PCT_ec
  254. } pre_comp_type;
  255. union {
  256. NISTP224_PRE_COMP *nistp224;
  257. NISTP256_PRE_COMP *nistp256;
  258. NISTP521_PRE_COMP *nistp521;
  259. NISTZ256_PRE_COMP *nistz256;
  260. EC_PRE_COMP *ec;
  261. } pre_comp;
  262. };
  263. #define SETPRECOMP(g, type, pre) \
  264. g->pre_comp_type = PCT_##type, g->pre_comp.type = pre
  265. #define HAVEPRECOMP(g, type) \
  266. g->pre_comp_type == PCT_##type && g->pre_comp.type != NULL
  267. struct ec_key_st {
  268. const EC_KEY_METHOD *meth;
  269. ENGINE *engine;
  270. int version;
  271. EC_GROUP *group;
  272. EC_POINT *pub_key;
  273. BIGNUM *priv_key;
  274. unsigned int enc_flag;
  275. point_conversion_form_t conv_form;
  276. CRYPTO_REF_COUNT references;
  277. int flags;
  278. CRYPTO_EX_DATA ex_data;
  279. CRYPTO_RWLOCK *lock;
  280. };
  281. struct ec_point_st {
  282. const EC_METHOD *meth;
  283. /* NID for the curve if known */
  284. int curve_name;
  285. /*
  286. * All members except 'meth' are handled by the method functions, even if
  287. * they appear generic
  288. */
  289. BIGNUM *X;
  290. BIGNUM *Y;
  291. BIGNUM *Z; /* Jacobian projective coordinates: * (X, Y,
  292. * Z) represents (X/Z^2, Y/Z^3) if Z != 0 */
  293. int Z_is_one; /* enable optimized point arithmetics for
  294. * special case */
  295. };
  296. static ossl_inline int ec_point_is_compat(const EC_POINT *point,
  297. const EC_GROUP *group)
  298. {
  299. if (group->meth != point->meth
  300. || (group->curve_name != 0
  301. && point->curve_name != 0
  302. && group->curve_name != point->curve_name))
  303. return 0;
  304. return 1;
  305. }
  306. NISTP224_PRE_COMP *EC_nistp224_pre_comp_dup(NISTP224_PRE_COMP *);
  307. NISTP256_PRE_COMP *EC_nistp256_pre_comp_dup(NISTP256_PRE_COMP *);
  308. NISTP521_PRE_COMP *EC_nistp521_pre_comp_dup(NISTP521_PRE_COMP *);
  309. NISTZ256_PRE_COMP *EC_nistz256_pre_comp_dup(NISTZ256_PRE_COMP *);
  310. NISTP256_PRE_COMP *EC_nistp256_pre_comp_dup(NISTP256_PRE_COMP *);
  311. EC_PRE_COMP *EC_ec_pre_comp_dup(EC_PRE_COMP *);
  312. void EC_pre_comp_free(EC_GROUP *group);
  313. void EC_nistp224_pre_comp_free(NISTP224_PRE_COMP *);
  314. void EC_nistp256_pre_comp_free(NISTP256_PRE_COMP *);
  315. void EC_nistp521_pre_comp_free(NISTP521_PRE_COMP *);
  316. void EC_nistz256_pre_comp_free(NISTZ256_PRE_COMP *);
  317. void EC_ec_pre_comp_free(EC_PRE_COMP *);
  318. /*
  319. * method functions in ec_mult.c (ec_lib.c uses these as defaults if
  320. * group->method->mul is 0)
  321. */
  322. int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
  323. size_t num, const EC_POINT *points[], const BIGNUM *scalars[],
  324. BN_CTX *);
  325. int ec_wNAF_precompute_mult(EC_GROUP *group, BN_CTX *);
  326. int ec_wNAF_have_precompute_mult(const EC_GROUP *group);
  327. /* method functions in ecp_smpl.c */
  328. int ec_GFp_simple_group_init(EC_GROUP *);
  329. void ec_GFp_simple_group_finish(EC_GROUP *);
  330. void ec_GFp_simple_group_clear_finish(EC_GROUP *);
  331. int ec_GFp_simple_group_copy(EC_GROUP *, const EC_GROUP *);
  332. int ec_GFp_simple_group_set_curve(EC_GROUP *, const BIGNUM *p,
  333. const BIGNUM *a, const BIGNUM *b, BN_CTX *);
  334. int ec_GFp_simple_group_get_curve(const EC_GROUP *, BIGNUM *p, BIGNUM *a,
  335. BIGNUM *b, BN_CTX *);
  336. int ec_GFp_simple_group_get_degree(const EC_GROUP *);
  337. int ec_GFp_simple_group_check_discriminant(const EC_GROUP *, BN_CTX *);
  338. int ec_GFp_simple_point_init(EC_POINT *);
  339. void ec_GFp_simple_point_finish(EC_POINT *);
  340. void ec_GFp_simple_point_clear_finish(EC_POINT *);
  341. int ec_GFp_simple_point_copy(EC_POINT *, const EC_POINT *);
  342. int ec_GFp_simple_point_set_to_infinity(const EC_GROUP *, EC_POINT *);
  343. int ec_GFp_simple_set_Jprojective_coordinates_GFp(const EC_GROUP *,
  344. EC_POINT *, const BIGNUM *x,
  345. const BIGNUM *y,
  346. const BIGNUM *z, BN_CTX *);
  347. int ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP *,
  348. const EC_POINT *, BIGNUM *x,
  349. BIGNUM *y, BIGNUM *z,
  350. BN_CTX *);
  351. int ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP *, EC_POINT *,
  352. const BIGNUM *x,
  353. const BIGNUM *y, BN_CTX *);
  354. int ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP *,
  355. const EC_POINT *, BIGNUM *x,
  356. BIGNUM *y, BN_CTX *);
  357. int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *, EC_POINT *,
  358. const BIGNUM *x, int y_bit,
  359. BN_CTX *);
  360. size_t ec_GFp_simple_point2oct(const EC_GROUP *, const EC_POINT *,
  361. point_conversion_form_t form,
  362. unsigned char *buf, size_t len, BN_CTX *);
  363. int ec_GFp_simple_oct2point(const EC_GROUP *, EC_POINT *,
  364. const unsigned char *buf, size_t len, BN_CTX *);
  365. int ec_GFp_simple_add(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
  366. const EC_POINT *b, BN_CTX *);
  367. int ec_GFp_simple_dbl(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
  368. BN_CTX *);
  369. int ec_GFp_simple_invert(const EC_GROUP *, EC_POINT *, BN_CTX *);
  370. int ec_GFp_simple_is_at_infinity(const EC_GROUP *, const EC_POINT *);
  371. int ec_GFp_simple_is_on_curve(const EC_GROUP *, const EC_POINT *, BN_CTX *);
  372. int ec_GFp_simple_cmp(const EC_GROUP *, const EC_POINT *a, const EC_POINT *b,
  373. BN_CTX *);
  374. int ec_GFp_simple_make_affine(const EC_GROUP *, EC_POINT *, BN_CTX *);
  375. int ec_GFp_simple_points_make_affine(const EC_GROUP *, size_t num,
  376. EC_POINT *[], BN_CTX *);
  377. int ec_GFp_simple_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  378. const BIGNUM *b, BN_CTX *);
  379. int ec_GFp_simple_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  380. BN_CTX *);
  381. int ec_GFp_simple_field_inv(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  382. BN_CTX *);
  383. int ec_GFp_simple_blind_coordinates(const EC_GROUP *group, EC_POINT *p,
  384. BN_CTX *ctx);
  385. int ec_GFp_simple_ladder_pre(const EC_GROUP *group,
  386. EC_POINT *r, EC_POINT *s,
  387. EC_POINT *p, BN_CTX *ctx);
  388. int ec_GFp_simple_ladder_step(const EC_GROUP *group,
  389. EC_POINT *r, EC_POINT *s,
  390. EC_POINT *p, BN_CTX *ctx);
  391. int ec_GFp_simple_ladder_post(const EC_GROUP *group,
  392. EC_POINT *r, EC_POINT *s,
  393. EC_POINT *p, BN_CTX *ctx);
  394. /* method functions in ecp_mont.c */
  395. int ec_GFp_mont_group_init(EC_GROUP *);
  396. int ec_GFp_mont_group_set_curve(EC_GROUP *, const BIGNUM *p, const BIGNUM *a,
  397. const BIGNUM *b, BN_CTX *);
  398. void ec_GFp_mont_group_finish(EC_GROUP *);
  399. void ec_GFp_mont_group_clear_finish(EC_GROUP *);
  400. int ec_GFp_mont_group_copy(EC_GROUP *, const EC_GROUP *);
  401. int ec_GFp_mont_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  402. const BIGNUM *b, BN_CTX *);
  403. int ec_GFp_mont_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  404. BN_CTX *);
  405. int ec_GFp_mont_field_inv(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  406. BN_CTX *);
  407. int ec_GFp_mont_field_encode(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  408. BN_CTX *);
  409. int ec_GFp_mont_field_decode(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  410. BN_CTX *);
  411. int ec_GFp_mont_field_set_to_one(const EC_GROUP *, BIGNUM *r, BN_CTX *);
  412. /* method functions in ecp_nist.c */
  413. int ec_GFp_nist_group_copy(EC_GROUP *dest, const EC_GROUP *src);
  414. int ec_GFp_nist_group_set_curve(EC_GROUP *, const BIGNUM *p, const BIGNUM *a,
  415. const BIGNUM *b, BN_CTX *);
  416. int ec_GFp_nist_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  417. const BIGNUM *b, BN_CTX *);
  418. int ec_GFp_nist_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  419. BN_CTX *);
  420. /* method functions in ec2_smpl.c */
  421. int ec_GF2m_simple_group_init(EC_GROUP *);
  422. void ec_GF2m_simple_group_finish(EC_GROUP *);
  423. void ec_GF2m_simple_group_clear_finish(EC_GROUP *);
  424. int ec_GF2m_simple_group_copy(EC_GROUP *, const EC_GROUP *);
  425. int ec_GF2m_simple_group_set_curve(EC_GROUP *, const BIGNUM *p,
  426. const BIGNUM *a, const BIGNUM *b,
  427. BN_CTX *);
  428. int ec_GF2m_simple_group_get_curve(const EC_GROUP *, BIGNUM *p, BIGNUM *a,
  429. BIGNUM *b, BN_CTX *);
  430. int ec_GF2m_simple_group_get_degree(const EC_GROUP *);
  431. int ec_GF2m_simple_group_check_discriminant(const EC_GROUP *, BN_CTX *);
  432. int ec_GF2m_simple_point_init(EC_POINT *);
  433. void ec_GF2m_simple_point_finish(EC_POINT *);
  434. void ec_GF2m_simple_point_clear_finish(EC_POINT *);
  435. int ec_GF2m_simple_point_copy(EC_POINT *, const EC_POINT *);
  436. int ec_GF2m_simple_point_set_to_infinity(const EC_GROUP *, EC_POINT *);
  437. int ec_GF2m_simple_point_set_affine_coordinates(const EC_GROUP *, EC_POINT *,
  438. const BIGNUM *x,
  439. const BIGNUM *y, BN_CTX *);
  440. int ec_GF2m_simple_point_get_affine_coordinates(const EC_GROUP *,
  441. const EC_POINT *, BIGNUM *x,
  442. BIGNUM *y, BN_CTX *);
  443. int ec_GF2m_simple_set_compressed_coordinates(const EC_GROUP *, EC_POINT *,
  444. const BIGNUM *x, int y_bit,
  445. BN_CTX *);
  446. size_t ec_GF2m_simple_point2oct(const EC_GROUP *, const EC_POINT *,
  447. point_conversion_form_t form,
  448. unsigned char *buf, size_t len, BN_CTX *);
  449. int ec_GF2m_simple_oct2point(const EC_GROUP *, EC_POINT *,
  450. const unsigned char *buf, size_t len, BN_CTX *);
  451. int ec_GF2m_simple_add(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
  452. const EC_POINT *b, BN_CTX *);
  453. int ec_GF2m_simple_dbl(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
  454. BN_CTX *);
  455. int ec_GF2m_simple_invert(const EC_GROUP *, EC_POINT *, BN_CTX *);
  456. int ec_GF2m_simple_is_at_infinity(const EC_GROUP *, const EC_POINT *);
  457. int ec_GF2m_simple_is_on_curve(const EC_GROUP *, const EC_POINT *, BN_CTX *);
  458. int ec_GF2m_simple_cmp(const EC_GROUP *, const EC_POINT *a, const EC_POINT *b,
  459. BN_CTX *);
  460. int ec_GF2m_simple_make_affine(const EC_GROUP *, EC_POINT *, BN_CTX *);
  461. int ec_GF2m_simple_points_make_affine(const EC_GROUP *, size_t num,
  462. EC_POINT *[], BN_CTX *);
  463. int ec_GF2m_simple_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  464. const BIGNUM *b, BN_CTX *);
  465. int ec_GF2m_simple_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  466. BN_CTX *);
  467. int ec_GF2m_simple_field_div(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  468. const BIGNUM *b, BN_CTX *);
  469. #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
  470. /* method functions in ecp_nistp224.c */
  471. int ec_GFp_nistp224_group_init(EC_GROUP *group);
  472. int ec_GFp_nistp224_group_set_curve(EC_GROUP *group, const BIGNUM *p,
  473. const BIGNUM *a, const BIGNUM *n,
  474. BN_CTX *);
  475. int ec_GFp_nistp224_point_get_affine_coordinates(const EC_GROUP *group,
  476. const EC_POINT *point,
  477. BIGNUM *x, BIGNUM *y,
  478. BN_CTX *ctx);
  479. int ec_GFp_nistp224_mul(const EC_GROUP *group, EC_POINT *r,
  480. const BIGNUM *scalar, size_t num,
  481. const EC_POINT *points[], const BIGNUM *scalars[],
  482. BN_CTX *);
  483. int ec_GFp_nistp224_points_mul(const EC_GROUP *group, EC_POINT *r,
  484. const BIGNUM *scalar, size_t num,
  485. const EC_POINT *points[],
  486. const BIGNUM *scalars[], BN_CTX *ctx);
  487. int ec_GFp_nistp224_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
  488. int ec_GFp_nistp224_have_precompute_mult(const EC_GROUP *group);
  489. /* method functions in ecp_nistp256.c */
  490. int ec_GFp_nistp256_group_init(EC_GROUP *group);
  491. int ec_GFp_nistp256_group_set_curve(EC_GROUP *group, const BIGNUM *p,
  492. const BIGNUM *a, const BIGNUM *n,
  493. BN_CTX *);
  494. int ec_GFp_nistp256_point_get_affine_coordinates(const EC_GROUP *group,
  495. const EC_POINT *point,
  496. BIGNUM *x, BIGNUM *y,
  497. BN_CTX *ctx);
  498. int ec_GFp_nistp256_mul(const EC_GROUP *group, EC_POINT *r,
  499. const BIGNUM *scalar, size_t num,
  500. const EC_POINT *points[], const BIGNUM *scalars[],
  501. BN_CTX *);
  502. int ec_GFp_nistp256_points_mul(const EC_GROUP *group, EC_POINT *r,
  503. const BIGNUM *scalar, size_t num,
  504. const EC_POINT *points[],
  505. const BIGNUM *scalars[], BN_CTX *ctx);
  506. int ec_GFp_nistp256_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
  507. int ec_GFp_nistp256_have_precompute_mult(const EC_GROUP *group);
  508. /* method functions in ecp_nistp521.c */
  509. int ec_GFp_nistp521_group_init(EC_GROUP *group);
  510. int ec_GFp_nistp521_group_set_curve(EC_GROUP *group, const BIGNUM *p,
  511. const BIGNUM *a, const BIGNUM *n,
  512. BN_CTX *);
  513. int ec_GFp_nistp521_point_get_affine_coordinates(const EC_GROUP *group,
  514. const EC_POINT *point,
  515. BIGNUM *x, BIGNUM *y,
  516. BN_CTX *ctx);
  517. int ec_GFp_nistp521_mul(const EC_GROUP *group, EC_POINT *r,
  518. const BIGNUM *scalar, size_t num,
  519. const EC_POINT *points[], const BIGNUM *scalars[],
  520. BN_CTX *);
  521. int ec_GFp_nistp521_points_mul(const EC_GROUP *group, EC_POINT *r,
  522. const BIGNUM *scalar, size_t num,
  523. const EC_POINT *points[],
  524. const BIGNUM *scalars[], BN_CTX *ctx);
  525. int ec_GFp_nistp521_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
  526. int ec_GFp_nistp521_have_precompute_mult(const EC_GROUP *group);
  527. /* utility functions in ecp_nistputil.c */
  528. void ec_GFp_nistp_points_make_affine_internal(size_t num, void *point_array,
  529. size_t felem_size,
  530. void *tmp_felems,
  531. void (*felem_one) (void *out),
  532. int (*felem_is_zero) (const void
  533. *in),
  534. void (*felem_assign) (void *out,
  535. const void
  536. *in),
  537. void (*felem_square) (void *out,
  538. const void
  539. *in),
  540. void (*felem_mul) (void *out,
  541. const void
  542. *in1,
  543. const void
  544. *in2),
  545. void (*felem_inv) (void *out,
  546. const void
  547. *in),
  548. void (*felem_contract) (void
  549. *out,
  550. const
  551. void
  552. *in));
  553. void ec_GFp_nistp_recode_scalar_bits(unsigned char *sign,
  554. unsigned char *digit, unsigned char in);
  555. #endif
  556. int ec_group_simple_order_bits(const EC_GROUP *group);
  557. #ifdef ECP_NISTZ256_ASM
  558. /** Returns GFp methods using montgomery multiplication, with x86-64 optimized
  559. * P256. See http://eprint.iacr.org/2013/816.
  560. * \return EC_METHOD object
  561. */
  562. const EC_METHOD *EC_GFp_nistz256_method(void);
  563. #endif
  564. size_t ec_key_simple_priv2oct(const EC_KEY *eckey,
  565. unsigned char *buf, size_t len);
  566. int ec_key_simple_oct2priv(EC_KEY *eckey, const unsigned char *buf, size_t len);
  567. int ec_key_simple_generate_key(EC_KEY *eckey);
  568. int ec_key_simple_generate_public_key(EC_KEY *eckey);
  569. int ec_key_simple_check_key(const EC_KEY *eckey);
  570. int ec_curve_nid_from_params(const EC_GROUP *group, BN_CTX *ctx);
  571. /* EC_METHOD definitions */
  572. struct ec_key_method_st {
  573. const char *name;
  574. int32_t flags;
  575. int (*init)(EC_KEY *key);
  576. void (*finish)(EC_KEY *key);
  577. int (*copy)(EC_KEY *dest, const EC_KEY *src);
  578. int (*set_group)(EC_KEY *key, const EC_GROUP *grp);
  579. int (*set_private)(EC_KEY *key, const BIGNUM *priv_key);
  580. int (*set_public)(EC_KEY *key, const EC_POINT *pub_key);
  581. int (*keygen)(EC_KEY *key);
  582. int (*compute_key)(unsigned char **pout, size_t *poutlen,
  583. const EC_POINT *pub_key, const EC_KEY *ecdh);
  584. int (*sign)(int type, const unsigned char *dgst, int dlen, unsigned char
  585. *sig, unsigned int *siglen, const BIGNUM *kinv,
  586. const BIGNUM *r, EC_KEY *eckey);
  587. int (*sign_setup)(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
  588. BIGNUM **rp);
  589. ECDSA_SIG *(*sign_sig)(const unsigned char *dgst, int dgst_len,
  590. const BIGNUM *in_kinv, const BIGNUM *in_r,
  591. EC_KEY *eckey);
  592. int (*verify)(int type, const unsigned char *dgst, int dgst_len,
  593. const unsigned char *sigbuf, int sig_len, EC_KEY *eckey);
  594. int (*verify_sig)(const unsigned char *dgst, int dgst_len,
  595. const ECDSA_SIG *sig, EC_KEY *eckey);
  596. };
  597. #define EC_KEY_METHOD_DYNAMIC 1
  598. int ossl_ec_key_gen(EC_KEY *eckey);
  599. int ossl_ecdh_compute_key(unsigned char **pout, size_t *poutlen,
  600. const EC_POINT *pub_key, const EC_KEY *ecdh);
  601. int ecdh_simple_compute_key(unsigned char **pout, size_t *poutlen,
  602. const EC_POINT *pub_key, const EC_KEY *ecdh);
  603. struct ECDSA_SIG_st {
  604. BIGNUM *r;
  605. BIGNUM *s;
  606. };
  607. int ossl_ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
  608. BIGNUM **rp);
  609. int ossl_ecdsa_sign(int type, const unsigned char *dgst, int dlen,
  610. unsigned char *sig, unsigned int *siglen,
  611. const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey);
  612. ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
  613. const BIGNUM *in_kinv, const BIGNUM *in_r,
  614. EC_KEY *eckey);
  615. int ossl_ecdsa_verify(int type, const unsigned char *dgst, int dgst_len,
  616. const unsigned char *sigbuf, int sig_len, EC_KEY *eckey);
  617. int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len,
  618. const ECDSA_SIG *sig, EC_KEY *eckey);
  619. int ED25519_sign(uint8_t *out_sig, const uint8_t *message, size_t message_len,
  620. const uint8_t public_key[32], const uint8_t private_key[32]);
  621. int ED25519_verify(const uint8_t *message, size_t message_len,
  622. const uint8_t signature[64], const uint8_t public_key[32]);
  623. void ED25519_public_from_private(uint8_t out_public_key[32],
  624. const uint8_t private_key[32]);
  625. int X25519(uint8_t out_shared_key[32], const uint8_t private_key[32],
  626. const uint8_t peer_public_value[32]);
  627. void X25519_public_from_private(uint8_t out_public_value[32],
  628. const uint8_t private_key[32]);
  629. /*-
  630. * This functions computes a single point multiplication over the EC group,
  631. * using, at a high level, a Montgomery ladder with conditional swaps, with
  632. * various timing attack defenses.
  633. *
  634. * It performs either a fixed point multiplication
  635. * (scalar * generator)
  636. * when point is NULL, or a variable point multiplication
  637. * (scalar * point)
  638. * when point is not NULL.
  639. *
  640. * `scalar` cannot be NULL and should be in the range [0,n) otherwise all
  641. * constant time bets are off (where n is the cardinality of the EC group).
  642. *
  643. * This function expects `group->order` and `group->cardinality` to be well
  644. * defined and non-zero: it fails with an error code otherwise.
  645. *
  646. * NB: This says nothing about the constant-timeness of the ladder step
  647. * implementation (i.e., the default implementation is based on EC_POINT_add and
  648. * EC_POINT_dbl, which of course are not constant time themselves) or the
  649. * underlying multiprecision arithmetic.
  650. *
  651. * The product is stored in `r`.
  652. *
  653. * This is an internal function: callers are in charge of ensuring that the
  654. * input parameters `group`, `r`, `scalar` and `ctx` are not NULL.
  655. *
  656. * Returns 1 on success, 0 otherwise.
  657. */
  658. int ec_scalar_mul_ladder(const EC_GROUP *group, EC_POINT *r,
  659. const BIGNUM *scalar, const EC_POINT *point,
  660. BN_CTX *ctx);
  661. int ec_point_blind_coordinates(const EC_GROUP *group, EC_POINT *p, BN_CTX *ctx);
  662. static ossl_inline int ec_point_ladder_pre(const EC_GROUP *group,
  663. EC_POINT *r, EC_POINT *s,
  664. EC_POINT *p, BN_CTX *ctx)
  665. {
  666. if (group->meth->ladder_pre != NULL)
  667. return group->meth->ladder_pre(group, r, s, p, ctx);
  668. if (!EC_POINT_copy(s, p)
  669. || !EC_POINT_dbl(group, r, s, ctx))
  670. return 0;
  671. return 1;
  672. }
  673. static ossl_inline int ec_point_ladder_step(const EC_GROUP *group,
  674. EC_POINT *r, EC_POINT *s,
  675. EC_POINT *p, BN_CTX *ctx)
  676. {
  677. if (group->meth->ladder_step != NULL)
  678. return group->meth->ladder_step(group, r, s, p, ctx);
  679. if (!EC_POINT_add(group, s, r, s, ctx)
  680. || !EC_POINT_dbl(group, r, r, ctx))
  681. return 0;
  682. return 1;
  683. }
  684. static ossl_inline int ec_point_ladder_post(const EC_GROUP *group,
  685. EC_POINT *r, EC_POINT *s,
  686. EC_POINT *p, BN_CTX *ctx)
  687. {
  688. if (group->meth->ladder_post != NULL)
  689. return group->meth->ladder_post(group, r, s, p, ctx);
  690. return 1;
  691. }