ec_local.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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. #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. #ifdef OPENSSL_NO_DEPRECATED_3_0
  28. typedef struct ec_method_st EC_METHOD;
  29. #endif
  30. /*
  31. * Structure details are not part of the exported interface, so all this may
  32. * change in future versions.
  33. */
  34. struct ec_method_st {
  35. /* Various method flags */
  36. int flags;
  37. /* used by EC_METHOD_get_field_type: */
  38. int field_type; /* a NID */
  39. /*
  40. * used by EC_GROUP_new, EC_GROUP_free, EC_GROUP_clear_free,
  41. * EC_GROUP_copy:
  42. */
  43. int (*group_init) (EC_GROUP *);
  44. void (*group_finish) (EC_GROUP *);
  45. void (*group_clear_finish) (EC_GROUP *);
  46. int (*group_copy) (EC_GROUP *, const EC_GROUP *);
  47. /* used by EC_GROUP_set_curve, EC_GROUP_get_curve: */
  48. int (*group_set_curve) (EC_GROUP *, const BIGNUM *p, const BIGNUM *a,
  49. const BIGNUM *b, BN_CTX *);
  50. int (*group_get_curve) (const EC_GROUP *, BIGNUM *p, BIGNUM *a, BIGNUM *b,
  51. BN_CTX *);
  52. /* used by EC_GROUP_get_degree: */
  53. int (*group_get_degree) (const EC_GROUP *);
  54. int (*group_order_bits) (const EC_GROUP *);
  55. /* used by EC_GROUP_check: */
  56. int (*group_check_discriminant) (const EC_GROUP *, BN_CTX *);
  57. /*
  58. * used by EC_POINT_new, EC_POINT_free, EC_POINT_clear_free,
  59. * EC_POINT_copy:
  60. */
  61. int (*point_init) (EC_POINT *);
  62. void (*point_finish) (EC_POINT *);
  63. void (*point_clear_finish) (EC_POINT *);
  64. int (*point_copy) (EC_POINT *, const EC_POINT *);
  65. /*-
  66. * used by EC_POINT_set_to_infinity,
  67. * EC_POINT_set_Jprojective_coordinates_GFp,
  68. * EC_POINT_get_Jprojective_coordinates_GFp,
  69. * EC_POINT_set_affine_coordinates,
  70. * EC_POINT_get_affine_coordinates,
  71. * EC_POINT_set_compressed_coordinates:
  72. */
  73. int (*point_set_to_infinity) (const EC_GROUP *, EC_POINT *);
  74. int (*point_set_affine_coordinates) (const EC_GROUP *, EC_POINT *,
  75. const BIGNUM *x, const BIGNUM *y,
  76. BN_CTX *);
  77. int (*point_get_affine_coordinates) (const EC_GROUP *, const EC_POINT *,
  78. BIGNUM *x, BIGNUM *y, BN_CTX *);
  79. int (*point_set_compressed_coordinates) (const EC_GROUP *, EC_POINT *,
  80. const BIGNUM *x, int y_bit,
  81. BN_CTX *);
  82. /* used by EC_POINT_point2oct, EC_POINT_oct2point: */
  83. size_t (*point2oct) (const EC_GROUP *, const EC_POINT *,
  84. point_conversion_form_t form, unsigned char *buf,
  85. size_t len, BN_CTX *);
  86. int (*oct2point) (const EC_GROUP *, EC_POINT *, const unsigned char *buf,
  87. size_t len, BN_CTX *);
  88. /* used by EC_POINT_add, EC_POINT_dbl, ECP_POINT_invert: */
  89. int (*add) (const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
  90. const EC_POINT *b, BN_CTX *);
  91. int (*dbl) (const EC_GROUP *, EC_POINT *r, const EC_POINT *a, BN_CTX *);
  92. int (*invert) (const EC_GROUP *, EC_POINT *, BN_CTX *);
  93. /*
  94. * used by EC_POINT_is_at_infinity, EC_POINT_is_on_curve, EC_POINT_cmp:
  95. */
  96. int (*is_at_infinity) (const EC_GROUP *, const EC_POINT *);
  97. int (*is_on_curve) (const EC_GROUP *, const EC_POINT *, BN_CTX *);
  98. int (*point_cmp) (const EC_GROUP *, const EC_POINT *a, const EC_POINT *b,
  99. BN_CTX *);
  100. /* used by EC_POINT_make_affine, EC_POINTs_make_affine: */
  101. int (*make_affine) (const EC_GROUP *, EC_POINT *, BN_CTX *);
  102. int (*points_make_affine) (const EC_GROUP *, size_t num, EC_POINT *[],
  103. BN_CTX *);
  104. /*
  105. * used by EC_POINTs_mul, EC_POINT_mul, EC_POINT_precompute_mult,
  106. * EC_POINT_have_precompute_mult (default implementations are used if the
  107. * 'mul' pointer is 0):
  108. */
  109. /*-
  110. * mul() calculates the value
  111. *
  112. * r := generator * scalar
  113. * + points[0] * scalars[0]
  114. * + ...
  115. * + points[num-1] * scalars[num-1].
  116. *
  117. * For a fixed point multiplication (scalar != NULL, num == 0)
  118. * or a variable point multiplication (scalar == NULL, num == 1),
  119. * mul() must use a constant time algorithm: in both cases callers
  120. * should provide an input scalar (either scalar or scalars[0])
  121. * in the range [0, ec_group_order); for robustness, implementers
  122. * should handle the case when the scalar has not been reduced, but
  123. * may treat it as an unusual input, without any constant-timeness
  124. * guarantee.
  125. */
  126. int (*mul) (const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
  127. size_t num, const EC_POINT *points[], const BIGNUM *scalars[],
  128. BN_CTX *);
  129. int (*precompute_mult) (EC_GROUP *group, BN_CTX *);
  130. int (*have_precompute_mult) (const EC_GROUP *group);
  131. /* internal functions */
  132. /*
  133. * 'field_mul', 'field_sqr', and 'field_div' can be used by 'add' and
  134. * 'dbl' so that the same implementations of point operations can be used
  135. * with different optimized implementations of expensive field
  136. * operations:
  137. */
  138. int (*field_mul) (const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  139. const BIGNUM *b, BN_CTX *);
  140. int (*field_sqr) (const EC_GROUP *, BIGNUM *r, const BIGNUM *a, BN_CTX *);
  141. int (*field_div) (const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  142. const BIGNUM *b, BN_CTX *);
  143. /*-
  144. * 'field_inv' computes the multiplicative inverse of a in the field,
  145. * storing the result in r.
  146. *
  147. * If 'a' is zero (or equivalent), you'll get an EC_R_CANNOT_INVERT error.
  148. */
  149. int (*field_inv) (const EC_GROUP *, BIGNUM *r, const BIGNUM *a, BN_CTX *);
  150. /* e.g. to Montgomery */
  151. int (*field_encode) (const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  152. BN_CTX *);
  153. /* e.g. from Montgomery */
  154. int (*field_decode) (const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  155. BN_CTX *);
  156. int (*field_set_to_one) (const EC_GROUP *, BIGNUM *r, BN_CTX *);
  157. /* private key operations */
  158. size_t (*priv2oct)(const EC_KEY *eckey, unsigned char *buf, size_t len);
  159. int (*oct2priv)(EC_KEY *eckey, const unsigned char *buf, size_t len);
  160. int (*set_private)(EC_KEY *eckey, const BIGNUM *priv_key);
  161. int (*keygen)(EC_KEY *eckey);
  162. int (*keycheck)(const EC_KEY *eckey);
  163. int (*keygenpub)(EC_KEY *eckey);
  164. int (*keycopy)(EC_KEY *dst, const EC_KEY *src);
  165. void (*keyfinish)(EC_KEY *eckey);
  166. /* custom ECDH operation */
  167. int (*ecdh_compute_key)(unsigned char **pout, size_t *poutlen,
  168. const EC_POINT *pub_key, const EC_KEY *ecdh);
  169. /* custom ECDSA */
  170. int (*ecdsa_sign_setup)(EC_KEY *eckey, BN_CTX *ctx, BIGNUM **kinvp,
  171. BIGNUM **rp);
  172. ECDSA_SIG *(*ecdsa_sign_sig)(const unsigned char *dgst, int dgstlen,
  173. const BIGNUM *kinv, const BIGNUM *r,
  174. EC_KEY *eckey);
  175. int (*ecdsa_verify_sig)(const unsigned char *dgst, int dgstlen,
  176. const ECDSA_SIG *sig, EC_KEY *eckey);
  177. /* Inverse modulo order */
  178. int (*field_inverse_mod_ord)(const EC_GROUP *, BIGNUM *r,
  179. const BIGNUM *x, BN_CTX *);
  180. int (*blind_coordinates)(const EC_GROUP *group, EC_POINT *p, BN_CTX *ctx);
  181. int (*ladder_pre)(const EC_GROUP *group,
  182. EC_POINT *r, EC_POINT *s,
  183. EC_POINT *p, BN_CTX *ctx);
  184. int (*ladder_step)(const EC_GROUP *group,
  185. EC_POINT *r, EC_POINT *s,
  186. EC_POINT *p, BN_CTX *ctx);
  187. int (*ladder_post)(const EC_GROUP *group,
  188. EC_POINT *r, EC_POINT *s,
  189. EC_POINT *p, BN_CTX *ctx);
  190. };
  191. /*
  192. * Types and functions to manipulate pre-computed values.
  193. */
  194. typedef struct nistp224_pre_comp_st NISTP224_PRE_COMP;
  195. typedef struct nistp256_pre_comp_st NISTP256_PRE_COMP;
  196. typedef struct nistp521_pre_comp_st NISTP521_PRE_COMP;
  197. typedef struct nistz256_pre_comp_st NISTZ256_PRE_COMP;
  198. typedef struct ec_pre_comp_st EC_PRE_COMP;
  199. struct ec_group_st {
  200. const EC_METHOD *meth;
  201. EC_POINT *generator; /* optional */
  202. BIGNUM *order, *cofactor;
  203. int curve_name; /* optional NID for named curve */
  204. int asn1_flag; /* flag to control the asn1 encoding */
  205. int decoded_from_explicit_params; /* set if decoded from explicit
  206. * curve parameters encoding */
  207. point_conversion_form_t asn1_form;
  208. unsigned char *seed; /* optional seed for parameters (appears in
  209. * ASN1) */
  210. size_t seed_len;
  211. /*
  212. * The following members are handled by the method functions, even if
  213. * they appear generic
  214. */
  215. /*
  216. * Field specification. For curves over GF(p), this is the modulus; for
  217. * curves over GF(2^m), this is the irreducible polynomial defining the
  218. * field.
  219. */
  220. BIGNUM *field;
  221. /*
  222. * Field specification for curves over GF(2^m). The irreducible f(t) is
  223. * then of the form: t^poly[0] + t^poly[1] + ... + t^poly[k] where m =
  224. * poly[0] > poly[1] > ... > poly[k] = 0. The array is terminated with
  225. * poly[k+1]=-1. All elliptic curve irreducibles have at most 5 non-zero
  226. * terms.
  227. */
  228. int poly[6];
  229. /*
  230. * Curve coefficients. (Here the assumption is that BIGNUMs can be used
  231. * or abused for all kinds of fields, not just GF(p).) For characteristic
  232. * > 3, the curve is defined by a Weierstrass equation of the form y^2 =
  233. * x^3 + a*x + b. For characteristic 2, the curve is defined by an
  234. * equation of the form y^2 + x*y = x^3 + a*x^2 + b.
  235. */
  236. BIGNUM *a, *b;
  237. /* enable optimized point arithmetics for special case */
  238. int a_is_minus3;
  239. /* method-specific (e.g., Montgomery structure) */
  240. void *field_data1;
  241. /* method-specific */
  242. void *field_data2;
  243. /* method-specific */
  244. int (*field_mod_func) (BIGNUM *, const BIGNUM *, const BIGNUM *,
  245. BN_CTX *);
  246. /* data for ECDSA inverse */
  247. BN_MONT_CTX *mont_data;
  248. /*
  249. * Precomputed values for speed. The PCT_xxx names match the
  250. * pre_comp.xxx union names; see the SETPRECOMP and HAVEPRECOMP
  251. * macros, below.
  252. */
  253. enum {
  254. PCT_none,
  255. PCT_nistp224, PCT_nistp256, PCT_nistp521, PCT_nistz256,
  256. PCT_ec
  257. } pre_comp_type;
  258. union {
  259. NISTP224_PRE_COMP *nistp224;
  260. NISTP256_PRE_COMP *nistp256;
  261. NISTP521_PRE_COMP *nistp521;
  262. NISTZ256_PRE_COMP *nistz256;
  263. EC_PRE_COMP *ec;
  264. } pre_comp;
  265. OSSL_LIB_CTX *libctx;
  266. char *propq;
  267. };
  268. #define SETPRECOMP(g, type, pre) \
  269. g->pre_comp_type = PCT_##type, g->pre_comp.type = pre
  270. #define HAVEPRECOMP(g, type) \
  271. g->pre_comp_type == PCT_##type && g->pre_comp.type != NULL
  272. struct ec_key_st {
  273. const EC_KEY_METHOD *meth;
  274. ENGINE *engine;
  275. int version;
  276. EC_GROUP *group;
  277. EC_POINT *pub_key;
  278. BIGNUM *priv_key;
  279. unsigned int enc_flag;
  280. point_conversion_form_t conv_form;
  281. CRYPTO_REF_COUNT references;
  282. int flags;
  283. #ifndef FIPS_MODULE
  284. CRYPTO_EX_DATA ex_data;
  285. #endif
  286. CRYPTO_RWLOCK *lock;
  287. OSSL_LIB_CTX *libctx;
  288. char *propq;
  289. /* Provider data */
  290. size_t dirty_cnt; /* If any key material changes, increment this */
  291. };
  292. struct ec_point_st {
  293. const EC_METHOD *meth;
  294. /* NID for the curve if known */
  295. int curve_name;
  296. /*
  297. * All members except 'meth' are handled by the method functions, even if
  298. * they appear generic
  299. */
  300. BIGNUM *X;
  301. BIGNUM *Y;
  302. BIGNUM *Z; /* Jacobian projective coordinates: * (X, Y,
  303. * Z) represents (X/Z^2, Y/Z^3) if Z != 0 */
  304. int Z_is_one; /* enable optimized point arithmetics for
  305. * special case */
  306. };
  307. static ossl_inline int ec_point_is_compat(const EC_POINT *point,
  308. const EC_GROUP *group)
  309. {
  310. return group->meth == point->meth
  311. && (group->curve_name == 0
  312. || point->curve_name == 0
  313. || group->curve_name == point->curve_name);
  314. }
  315. NISTP224_PRE_COMP *EC_nistp224_pre_comp_dup(NISTP224_PRE_COMP *);
  316. NISTP256_PRE_COMP *EC_nistp256_pre_comp_dup(NISTP256_PRE_COMP *);
  317. NISTP521_PRE_COMP *EC_nistp521_pre_comp_dup(NISTP521_PRE_COMP *);
  318. NISTZ256_PRE_COMP *EC_nistz256_pre_comp_dup(NISTZ256_PRE_COMP *);
  319. NISTP256_PRE_COMP *EC_nistp256_pre_comp_dup(NISTP256_PRE_COMP *);
  320. EC_PRE_COMP *EC_ec_pre_comp_dup(EC_PRE_COMP *);
  321. void EC_pre_comp_free(EC_GROUP *group);
  322. void EC_nistp224_pre_comp_free(NISTP224_PRE_COMP *);
  323. void EC_nistp256_pre_comp_free(NISTP256_PRE_COMP *);
  324. void EC_nistp521_pre_comp_free(NISTP521_PRE_COMP *);
  325. void EC_nistz256_pre_comp_free(NISTZ256_PRE_COMP *);
  326. void EC_ec_pre_comp_free(EC_PRE_COMP *);
  327. /*
  328. * method functions in ec_mult.c (ec_lib.c uses these as defaults if
  329. * group->method->mul is 0)
  330. */
  331. int ossl_ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
  332. size_t num, const EC_POINT *points[],
  333. const BIGNUM *scalars[], BN_CTX *);
  334. int ossl_ec_wNAF_precompute_mult(EC_GROUP *group, BN_CTX *);
  335. int ossl_ec_wNAF_have_precompute_mult(const EC_GROUP *group);
  336. /* method functions in ecp_smpl.c */
  337. int ossl_ec_GFp_simple_group_init(EC_GROUP *);
  338. void ossl_ec_GFp_simple_group_finish(EC_GROUP *);
  339. void ossl_ec_GFp_simple_group_clear_finish(EC_GROUP *);
  340. int ossl_ec_GFp_simple_group_copy(EC_GROUP *, const EC_GROUP *);
  341. int ossl_ec_GFp_simple_group_set_curve(EC_GROUP *, const BIGNUM *p,
  342. const BIGNUM *a, const BIGNUM *b,
  343. BN_CTX *);
  344. int ossl_ec_GFp_simple_group_get_curve(const EC_GROUP *, BIGNUM *p, BIGNUM *a,
  345. BIGNUM *b, BN_CTX *);
  346. int ossl_ec_GFp_simple_group_get_degree(const EC_GROUP *);
  347. int ossl_ec_GFp_simple_group_check_discriminant(const EC_GROUP *, BN_CTX *);
  348. int ossl_ec_GFp_simple_point_init(EC_POINT *);
  349. void ossl_ec_GFp_simple_point_finish(EC_POINT *);
  350. void ossl_ec_GFp_simple_point_clear_finish(EC_POINT *);
  351. int ossl_ec_GFp_simple_point_copy(EC_POINT *, const EC_POINT *);
  352. int ossl_ec_GFp_simple_point_set_to_infinity(const EC_GROUP *, EC_POINT *);
  353. int ossl_ec_GFp_simple_set_Jprojective_coordinates_GFp(const EC_GROUP *,
  354. EC_POINT *,
  355. const BIGNUM *x,
  356. const BIGNUM *y,
  357. const BIGNUM *z,
  358. BN_CTX *);
  359. int ossl_ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP *,
  360. const EC_POINT *,
  361. BIGNUM *x,
  362. BIGNUM *y, BIGNUM *z,
  363. BN_CTX *);
  364. int ossl_ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP *, EC_POINT *,
  365. const BIGNUM *x,
  366. const BIGNUM *y, BN_CTX *);
  367. int ossl_ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP *,
  368. const EC_POINT *, BIGNUM *x,
  369. BIGNUM *y, BN_CTX *);
  370. int ossl_ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *, EC_POINT *,
  371. const BIGNUM *x, int y_bit,
  372. BN_CTX *);
  373. size_t ossl_ec_GFp_simple_point2oct(const EC_GROUP *, const EC_POINT *,
  374. point_conversion_form_t form,
  375. unsigned char *buf, size_t len, BN_CTX *);
  376. int ossl_ec_GFp_simple_oct2point(const EC_GROUP *, EC_POINT *,
  377. const unsigned char *buf, size_t len, BN_CTX *);
  378. int ossl_ec_GFp_simple_add(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
  379. const EC_POINT *b, BN_CTX *);
  380. int ossl_ec_GFp_simple_dbl(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
  381. BN_CTX *);
  382. int ossl_ec_GFp_simple_invert(const EC_GROUP *, EC_POINT *, BN_CTX *);
  383. int ossl_ec_GFp_simple_is_at_infinity(const EC_GROUP *, const EC_POINT *);
  384. int ossl_ec_GFp_simple_is_on_curve(const EC_GROUP *, const EC_POINT *, BN_CTX *);
  385. int ossl_ec_GFp_simple_cmp(const EC_GROUP *, const EC_POINT *a,
  386. const EC_POINT *b, BN_CTX *);
  387. int ossl_ec_GFp_simple_make_affine(const EC_GROUP *, EC_POINT *, BN_CTX *);
  388. int ossl_ec_GFp_simple_points_make_affine(const EC_GROUP *, size_t num,
  389. EC_POINT *[], BN_CTX *);
  390. int ossl_ec_GFp_simple_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  391. const BIGNUM *b, BN_CTX *);
  392. int ossl_ec_GFp_simple_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  393. BN_CTX *);
  394. int ossl_ec_GFp_simple_field_inv(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  395. BN_CTX *);
  396. int ossl_ec_GFp_simple_blind_coordinates(const EC_GROUP *group, EC_POINT *p,
  397. BN_CTX *ctx);
  398. int ossl_ec_GFp_simple_ladder_pre(const EC_GROUP *group,
  399. EC_POINT *r, EC_POINT *s,
  400. EC_POINT *p, BN_CTX *ctx);
  401. int ossl_ec_GFp_simple_ladder_step(const EC_GROUP *group,
  402. EC_POINT *r, EC_POINT *s,
  403. EC_POINT *p, BN_CTX *ctx);
  404. int ossl_ec_GFp_simple_ladder_post(const EC_GROUP *group,
  405. EC_POINT *r, EC_POINT *s,
  406. EC_POINT *p, BN_CTX *ctx);
  407. /* method functions in ecp_mont.c */
  408. int ossl_ec_GFp_mont_group_init(EC_GROUP *);
  409. int ossl_ec_GFp_mont_group_set_curve(EC_GROUP *, const BIGNUM *p,
  410. const BIGNUM *a,
  411. const BIGNUM *b, BN_CTX *);
  412. void ossl_ec_GFp_mont_group_finish(EC_GROUP *);
  413. void ossl_ec_GFp_mont_group_clear_finish(EC_GROUP *);
  414. int ossl_ec_GFp_mont_group_copy(EC_GROUP *, const EC_GROUP *);
  415. int ossl_ec_GFp_mont_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  416. const BIGNUM *b, BN_CTX *);
  417. int ossl_ec_GFp_mont_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  418. BN_CTX *);
  419. int ossl_ec_GFp_mont_field_inv(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  420. BN_CTX *);
  421. int ossl_ec_GFp_mont_field_encode(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  422. BN_CTX *);
  423. int ossl_ec_GFp_mont_field_decode(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  424. BN_CTX *);
  425. int ossl_ec_GFp_mont_field_set_to_one(const EC_GROUP *, BIGNUM *r, BN_CTX *);
  426. /* method functions in ecp_nist.c */
  427. int ossl_ec_GFp_nist_group_copy(EC_GROUP *dest, const EC_GROUP *src);
  428. int ossl_ec_GFp_nist_group_set_curve(EC_GROUP *, const BIGNUM *p,
  429. const BIGNUM *a, const BIGNUM *b, BN_CTX *);
  430. int ossl_ec_GFp_nist_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  431. const BIGNUM *b, BN_CTX *);
  432. int ossl_ec_GFp_nist_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  433. BN_CTX *);
  434. /* method functions in ec2_smpl.c */
  435. int ossl_ec_GF2m_simple_group_init(EC_GROUP *);
  436. void ossl_ec_GF2m_simple_group_finish(EC_GROUP *);
  437. void ossl_ec_GF2m_simple_group_clear_finish(EC_GROUP *);
  438. int ossl_ec_GF2m_simple_group_copy(EC_GROUP *, const EC_GROUP *);
  439. int ossl_ec_GF2m_simple_group_set_curve(EC_GROUP *, const BIGNUM *p,
  440. const BIGNUM *a, const BIGNUM *b,
  441. BN_CTX *);
  442. int ossl_ec_GF2m_simple_group_get_curve(const EC_GROUP *, BIGNUM *p, BIGNUM *a,
  443. BIGNUM *b, BN_CTX *);
  444. int ossl_ec_GF2m_simple_group_get_degree(const EC_GROUP *);
  445. int ossl_ec_GF2m_simple_group_check_discriminant(const EC_GROUP *, BN_CTX *);
  446. int ossl_ec_GF2m_simple_point_init(EC_POINT *);
  447. void ossl_ec_GF2m_simple_point_finish(EC_POINT *);
  448. void ossl_ec_GF2m_simple_point_clear_finish(EC_POINT *);
  449. int ossl_ec_GF2m_simple_point_copy(EC_POINT *, const EC_POINT *);
  450. int ossl_ec_GF2m_simple_point_set_to_infinity(const EC_GROUP *, EC_POINT *);
  451. int ossl_ec_GF2m_simple_point_set_affine_coordinates(const EC_GROUP *,
  452. EC_POINT *,
  453. const BIGNUM *x,
  454. const BIGNUM *y, BN_CTX *);
  455. int ossl_ec_GF2m_simple_point_get_affine_coordinates(const EC_GROUP *,
  456. const EC_POINT *, BIGNUM *x,
  457. BIGNUM *y, BN_CTX *);
  458. int ossl_ec_GF2m_simple_set_compressed_coordinates(const EC_GROUP *, EC_POINT *,
  459. const BIGNUM *x, int y_bit,
  460. BN_CTX *);
  461. size_t ossl_ec_GF2m_simple_point2oct(const EC_GROUP *, const EC_POINT *,
  462. point_conversion_form_t form,
  463. unsigned char *buf, size_t len, BN_CTX *);
  464. int ossl_ec_GF2m_simple_oct2point(const EC_GROUP *, EC_POINT *,
  465. const unsigned char *buf, size_t len, BN_CTX *);
  466. int ossl_ec_GF2m_simple_add(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
  467. const EC_POINT *b, BN_CTX *);
  468. int ossl_ec_GF2m_simple_dbl(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
  469. BN_CTX *);
  470. int ossl_ec_GF2m_simple_invert(const EC_GROUP *, EC_POINT *, BN_CTX *);
  471. int ossl_ec_GF2m_simple_is_at_infinity(const EC_GROUP *, const EC_POINT *);
  472. int ossl_ec_GF2m_simple_is_on_curve(const EC_GROUP *, const EC_POINT *, BN_CTX *);
  473. int ossl_ec_GF2m_simple_cmp(const EC_GROUP *, const EC_POINT *a,
  474. const EC_POINT *b, BN_CTX *);
  475. int ossl_ec_GF2m_simple_make_affine(const EC_GROUP *, EC_POINT *, BN_CTX *);
  476. int ossl_ec_GF2m_simple_points_make_affine(const EC_GROUP *, size_t num,
  477. EC_POINT *[], BN_CTX *);
  478. int ossl_ec_GF2m_simple_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  479. const BIGNUM *b, BN_CTX *);
  480. int ossl_ec_GF2m_simple_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  481. BN_CTX *);
  482. int ossl_ec_GF2m_simple_field_div(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  483. const BIGNUM *b, BN_CTX *);
  484. #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
  485. /* method functions in ecp_nistp224.c */
  486. int ossl_ec_GFp_nistp224_group_init(EC_GROUP *group);
  487. int ossl_ec_GFp_nistp224_group_set_curve(EC_GROUP *group, const BIGNUM *p,
  488. const BIGNUM *a, const BIGNUM *n,
  489. BN_CTX *);
  490. int ossl_ec_GFp_nistp224_point_get_affine_coordinates(const EC_GROUP *group,
  491. const EC_POINT *point,
  492. BIGNUM *x, BIGNUM *y,
  493. BN_CTX *ctx);
  494. int ossl_ec_GFp_nistp224_mul(const EC_GROUP *group, EC_POINT *r,
  495. const BIGNUM *scalar, size_t num,
  496. const EC_POINT *points[], const BIGNUM *scalars[],
  497. BN_CTX *);
  498. int ossl_ec_GFp_nistp224_points_mul(const EC_GROUP *group, EC_POINT *r,
  499. const BIGNUM *scalar, size_t num,
  500. const EC_POINT *points[],
  501. const BIGNUM *scalars[], BN_CTX *ctx);
  502. int ossl_ec_GFp_nistp224_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
  503. int ossl_ec_GFp_nistp224_have_precompute_mult(const EC_GROUP *group);
  504. /* method functions in ecp_nistp256.c */
  505. int ossl_ec_GFp_nistp256_group_init(EC_GROUP *group);
  506. int ossl_ec_GFp_nistp256_group_set_curve(EC_GROUP *group, const BIGNUM *p,
  507. const BIGNUM *a, const BIGNUM *n,
  508. BN_CTX *);
  509. int ossl_ec_GFp_nistp256_point_get_affine_coordinates(const EC_GROUP *group,
  510. const EC_POINT *point,
  511. BIGNUM *x, BIGNUM *y,
  512. BN_CTX *ctx);
  513. int ossl_ec_GFp_nistp256_mul(const EC_GROUP *group, EC_POINT *r,
  514. const BIGNUM *scalar, size_t num,
  515. const EC_POINT *points[], const BIGNUM *scalars[],
  516. BN_CTX *);
  517. int ossl_ec_GFp_nistp256_points_mul(const EC_GROUP *group, EC_POINT *r,
  518. const BIGNUM *scalar, size_t num,
  519. const EC_POINT *points[],
  520. const BIGNUM *scalars[], BN_CTX *ctx);
  521. int ossl_ec_GFp_nistp256_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
  522. int ossl_ec_GFp_nistp256_have_precompute_mult(const EC_GROUP *group);
  523. /* method functions in ecp_nistp521.c */
  524. int ossl_ec_GFp_nistp521_group_init(EC_GROUP *group);
  525. int ossl_ec_GFp_nistp521_group_set_curve(EC_GROUP *group, const BIGNUM *p,
  526. const BIGNUM *a, const BIGNUM *n,
  527. BN_CTX *);
  528. int ossl_ec_GFp_nistp521_point_get_affine_coordinates(const EC_GROUP *group,
  529. const EC_POINT *point,
  530. BIGNUM *x, BIGNUM *y,
  531. BN_CTX *ctx);
  532. int ossl_ec_GFp_nistp521_mul(const EC_GROUP *group, EC_POINT *r,
  533. const BIGNUM *scalar, size_t num,
  534. const EC_POINT *points[], const BIGNUM *scalars[],
  535. BN_CTX *);
  536. int ossl_ec_GFp_nistp521_points_mul(const EC_GROUP *group, EC_POINT *r,
  537. const BIGNUM *scalar, size_t num,
  538. const EC_POINT *points[],
  539. const BIGNUM *scalars[], BN_CTX *ctx);
  540. int ossl_ec_GFp_nistp521_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
  541. int ossl_ec_GFp_nistp521_have_precompute_mult(const EC_GROUP *group);
  542. /* utility functions in ecp_nistputil.c */
  543. void ossl_ec_GFp_nistp_points_make_affine_internal(size_t num, void *point_array,
  544. size_t felem_size,
  545. void *tmp_felems,
  546. void (*felem_one) (void *out),
  547. int (*felem_is_zero)
  548. (const void *in),
  549. void (*felem_assign)
  550. (void *out, const void *in),
  551. void (*felem_square)
  552. (void *out, const void *in),
  553. void (*felem_mul)
  554. (void *out,
  555. const void *in1,
  556. const void *in2),
  557. void (*felem_inv)
  558. (void *out, const void *in),
  559. void (*felem_contract)
  560. (void *out, const void *in));
  561. void ossl_ec_GFp_nistp_recode_scalar_bits(unsigned char *sign,
  562. unsigned char *digit,
  563. unsigned char in);
  564. #endif
  565. int ossl_ec_group_simple_order_bits(const EC_GROUP *group);
  566. /**
  567. * Creates a new EC_GROUP object
  568. * \param libctx The associated library context or NULL for the default
  569. * library context
  570. * \param propq Any property query string
  571. * \param meth EC_METHOD to use
  572. * \return newly created EC_GROUP object or NULL in case of an error.
  573. */
  574. EC_GROUP *ossl_ec_group_new_ex(OSSL_LIB_CTX *libctx, const char *propq,
  575. const EC_METHOD *meth);
  576. #ifdef ECP_NISTZ256_ASM
  577. /** Returns GFp methods using montgomery multiplication, with x86-64 optimized
  578. * P256. See http://eprint.iacr.org/2013/816.
  579. * \return EC_METHOD object
  580. */
  581. const EC_METHOD *EC_GFp_nistz256_method(void);
  582. #endif
  583. #ifdef S390X_EC_ASM
  584. const EC_METHOD *EC_GFp_s390x_nistp256_method(void);
  585. const EC_METHOD *EC_GFp_s390x_nistp384_method(void);
  586. const EC_METHOD *EC_GFp_s390x_nistp521_method(void);
  587. #endif
  588. size_t ossl_ec_key_simple_priv2oct(const EC_KEY *eckey,
  589. unsigned char *buf, size_t len);
  590. int ossl_ec_key_simple_oct2priv(EC_KEY *eckey, const unsigned char *buf,
  591. size_t len);
  592. int ossl_ec_key_simple_generate_key(EC_KEY *eckey);
  593. int ossl_ec_key_simple_generate_public_key(EC_KEY *eckey);
  594. int ossl_ec_key_simple_check_key(const EC_KEY *eckey);
  595. int ossl_ec_curve_nid_from_params(const EC_GROUP *group, BN_CTX *ctx);
  596. /* EC_METHOD definitions */
  597. struct ec_key_method_st {
  598. const char *name;
  599. int32_t flags;
  600. int (*init)(EC_KEY *key);
  601. void (*finish)(EC_KEY *key);
  602. int (*copy)(EC_KEY *dest, const EC_KEY *src);
  603. int (*set_group)(EC_KEY *key, const EC_GROUP *grp);
  604. int (*set_private)(EC_KEY *key, const BIGNUM *priv_key);
  605. int (*set_public)(EC_KEY *key, const EC_POINT *pub_key);
  606. int (*keygen)(EC_KEY *key);
  607. int (*compute_key)(unsigned char **pout, size_t *poutlen,
  608. const EC_POINT *pub_key, const EC_KEY *ecdh);
  609. int (*sign)(int type, const unsigned char *dgst, int dlen, unsigned char
  610. *sig, unsigned int *siglen, const BIGNUM *kinv,
  611. const BIGNUM *r, EC_KEY *eckey);
  612. int (*sign_setup)(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
  613. BIGNUM **rp);
  614. ECDSA_SIG *(*sign_sig)(const unsigned char *dgst, int dgst_len,
  615. const BIGNUM *in_kinv, const BIGNUM *in_r,
  616. EC_KEY *eckey);
  617. int (*verify)(int type, const unsigned char *dgst, int dgst_len,
  618. const unsigned char *sigbuf, int sig_len, EC_KEY *eckey);
  619. int (*verify_sig)(const unsigned char *dgst, int dgst_len,
  620. const ECDSA_SIG *sig, EC_KEY *eckey);
  621. };
  622. #define EC_KEY_METHOD_DYNAMIC 1
  623. EC_KEY *ossl_ec_key_new_method_int(OSSL_LIB_CTX *libctx, const char *propq,
  624. ENGINE *engine);
  625. int ossl_ec_key_gen(EC_KEY *eckey);
  626. int ossl_ecdh_compute_key(unsigned char **pout, size_t *poutlen,
  627. const EC_POINT *pub_key, const EC_KEY *ecdh);
  628. int ossl_ecdh_simple_compute_key(unsigned char **pout, size_t *poutlen,
  629. const EC_POINT *pub_key, const EC_KEY *ecdh);
  630. struct ECDSA_SIG_st {
  631. BIGNUM *r;
  632. BIGNUM *s;
  633. };
  634. int ossl_ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
  635. BIGNUM **rp);
  636. int ossl_ecdsa_sign(int type, const unsigned char *dgst, int dlen,
  637. unsigned char *sig, unsigned int *siglen,
  638. const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey);
  639. ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
  640. const BIGNUM *in_kinv, const BIGNUM *in_r,
  641. EC_KEY *eckey);
  642. int ossl_ecdsa_verify(int type, const unsigned char *dgst, int dgst_len,
  643. const unsigned char *sigbuf, int sig_len, EC_KEY *eckey);
  644. int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len,
  645. const ECDSA_SIG *sig, EC_KEY *eckey);
  646. int ossl_ecdsa_simple_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
  647. BIGNUM **rp);
  648. ECDSA_SIG *ossl_ecdsa_simple_sign_sig(const unsigned char *dgst, int dgst_len,
  649. const BIGNUM *in_kinv, const BIGNUM *in_r,
  650. EC_KEY *eckey);
  651. int ossl_ecdsa_simple_verify_sig(const unsigned char *dgst, int dgst_len,
  652. const ECDSA_SIG *sig, EC_KEY *eckey);
  653. /*-
  654. * This functions computes a single point multiplication over the EC group,
  655. * using, at a high level, a Montgomery ladder with conditional swaps, with
  656. * various timing attack defenses.
  657. *
  658. * It performs either a fixed point multiplication
  659. * (scalar * generator)
  660. * when point is NULL, or a variable point multiplication
  661. * (scalar * point)
  662. * when point is not NULL.
  663. *
  664. * `scalar` cannot be NULL and should be in the range [0,n) otherwise all
  665. * constant time bets are off (where n is the cardinality of the EC group).
  666. *
  667. * This function expects `group->order` and `group->cardinality` to be well
  668. * defined and non-zero: it fails with an error code otherwise.
  669. *
  670. * NB: This says nothing about the constant-timeness of the ladder step
  671. * implementation (i.e., the default implementation is based on EC_POINT_add and
  672. * EC_POINT_dbl, which of course are not constant time themselves) or the
  673. * underlying multiprecision arithmetic.
  674. *
  675. * The product is stored in `r`.
  676. *
  677. * This is an internal function: callers are in charge of ensuring that the
  678. * input parameters `group`, `r`, `scalar` and `ctx` are not NULL.
  679. *
  680. * Returns 1 on success, 0 otherwise.
  681. */
  682. int ossl_ec_scalar_mul_ladder(const EC_GROUP *group, EC_POINT *r,
  683. const BIGNUM *scalar, const EC_POINT *point,
  684. BN_CTX *ctx);
  685. int ossl_ec_point_blind_coordinates(const EC_GROUP *group, EC_POINT *p,
  686. BN_CTX *ctx);
  687. static ossl_inline int ec_point_ladder_pre(const EC_GROUP *group,
  688. EC_POINT *r, EC_POINT *s,
  689. EC_POINT *p, BN_CTX *ctx)
  690. {
  691. if (group->meth->ladder_pre != NULL)
  692. return group->meth->ladder_pre(group, r, s, p, ctx);
  693. if (!EC_POINT_copy(s, p)
  694. || !EC_POINT_dbl(group, r, s, ctx))
  695. return 0;
  696. return 1;
  697. }
  698. static ossl_inline int ec_point_ladder_step(const EC_GROUP *group,
  699. EC_POINT *r, EC_POINT *s,
  700. EC_POINT *p, BN_CTX *ctx)
  701. {
  702. if (group->meth->ladder_step != NULL)
  703. return group->meth->ladder_step(group, r, s, p, ctx);
  704. if (!EC_POINT_add(group, s, r, s, ctx)
  705. || !EC_POINT_dbl(group, r, r, ctx))
  706. return 0;
  707. return 1;
  708. }
  709. static ossl_inline int ec_point_ladder_post(const EC_GROUP *group,
  710. EC_POINT *r, EC_POINT *s,
  711. EC_POINT *p, BN_CTX *ctx)
  712. {
  713. if (group->meth->ladder_post != NULL)
  714. return group->meth->ladder_post(group, r, s, p, ctx);
  715. return 1;
  716. }