ec_local.h 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  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 nistp384_pre_comp_st NISTP384_PRE_COMP;
  197. typedef struct nistp521_pre_comp_st NISTP521_PRE_COMP;
  198. typedef struct nistz256_pre_comp_st NISTZ256_PRE_COMP;
  199. typedef struct ec_pre_comp_st EC_PRE_COMP;
  200. struct ec_group_st {
  201. const EC_METHOD *meth;
  202. EC_POINT *generator; /* optional */
  203. BIGNUM *order, *cofactor;
  204. int curve_name; /* optional NID for named curve */
  205. int asn1_flag; /* flag to control the asn1 encoding */
  206. int decoded_from_explicit_params; /* set if decoded from explicit
  207. * curve parameters encoding */
  208. point_conversion_form_t asn1_form;
  209. unsigned char *seed; /* optional seed for parameters (appears in
  210. * ASN1) */
  211. size_t seed_len;
  212. /*
  213. * The following members are handled by the method functions, even if
  214. * they appear generic
  215. */
  216. /*
  217. * Field specification. For curves over GF(p), this is the modulus; for
  218. * curves over GF(2^m), this is the irreducible polynomial defining the
  219. * field.
  220. */
  221. BIGNUM *field;
  222. /*
  223. * Field specification for curves over GF(2^m). The irreducible f(t) is
  224. * then of the form: t^poly[0] + t^poly[1] + ... + t^poly[k] where m =
  225. * poly[0] > poly[1] > ... > poly[k] = 0. The array is terminated with
  226. * poly[k+1]=-1. All elliptic curve irreducibles have at most 5 non-zero
  227. * terms.
  228. */
  229. int poly[6];
  230. /*
  231. * Curve coefficients. (Here the assumption is that BIGNUMs can be used
  232. * or abused for all kinds of fields, not just GF(p).) For characteristic
  233. * > 3, the curve is defined by a Weierstrass equation of the form y^2 =
  234. * x^3 + a*x + b. For characteristic 2, the curve is defined by an
  235. * equation of the form y^2 + x*y = x^3 + a*x^2 + b.
  236. */
  237. BIGNUM *a, *b;
  238. /* enable optimized point arithmetic for special case */
  239. int a_is_minus3;
  240. /* method-specific (e.g., Montgomery structure) */
  241. void *field_data1;
  242. /* method-specific */
  243. void *field_data2;
  244. /* method-specific */
  245. int (*field_mod_func) (BIGNUM *, const BIGNUM *, const BIGNUM *,
  246. BN_CTX *);
  247. /* data for ECDSA inverse */
  248. BN_MONT_CTX *mont_data;
  249. /*
  250. * Precomputed values for speed. The PCT_xxx names match the
  251. * pre_comp.xxx union names; see the SETPRECOMP and HAVEPRECOMP
  252. * macros, below.
  253. */
  254. enum {
  255. PCT_none,
  256. PCT_nistp224, PCT_nistp256, PCT_nistp384, PCT_nistp521, PCT_nistz256,
  257. PCT_ec
  258. } pre_comp_type;
  259. union {
  260. NISTP224_PRE_COMP *nistp224;
  261. NISTP256_PRE_COMP *nistp256;
  262. NISTP384_PRE_COMP *nistp384;
  263. NISTP521_PRE_COMP *nistp521;
  264. NISTZ256_PRE_COMP *nistz256;
  265. EC_PRE_COMP *ec;
  266. } pre_comp;
  267. OSSL_LIB_CTX *libctx;
  268. char *propq;
  269. };
  270. #define SETPRECOMP(g, type, pre) \
  271. g->pre_comp_type = PCT_##type, g->pre_comp.type = pre
  272. #define HAVEPRECOMP(g, type) \
  273. g->pre_comp_type == PCT_##type && g->pre_comp.type != NULL
  274. struct ec_key_st {
  275. const EC_KEY_METHOD *meth;
  276. ENGINE *engine;
  277. int version;
  278. EC_GROUP *group;
  279. EC_POINT *pub_key;
  280. BIGNUM *priv_key;
  281. unsigned int enc_flag;
  282. point_conversion_form_t conv_form;
  283. CRYPTO_REF_COUNT references;
  284. int flags;
  285. #ifndef FIPS_MODULE
  286. CRYPTO_EX_DATA ex_data;
  287. #endif
  288. OSSL_LIB_CTX *libctx;
  289. char *propq;
  290. /* Provider data */
  291. size_t dirty_cnt; /* If any key material changes, increment this */
  292. };
  293. struct ec_point_st {
  294. const EC_METHOD *meth;
  295. /* NID for the curve if known */
  296. int curve_name;
  297. /*
  298. * All members except 'meth' are handled by the method functions, even if
  299. * they appear generic
  300. */
  301. BIGNUM *X;
  302. BIGNUM *Y;
  303. BIGNUM *Z; /* Jacobian projective coordinates: * (X, Y,
  304. * Z) represents (X/Z^2, Y/Z^3) if Z != 0 */
  305. int Z_is_one; /* enable optimized point arithmetic for
  306. * special case */
  307. };
  308. static ossl_inline int ec_point_is_compat(const EC_POINT *point,
  309. const EC_GROUP *group)
  310. {
  311. return group->meth == point->meth
  312. && (group->curve_name == 0
  313. || point->curve_name == 0
  314. || group->curve_name == point->curve_name);
  315. }
  316. NISTP224_PRE_COMP *EC_nistp224_pre_comp_dup(NISTP224_PRE_COMP *);
  317. NISTP256_PRE_COMP *EC_nistp256_pre_comp_dup(NISTP256_PRE_COMP *);
  318. NISTP384_PRE_COMP *ossl_ec_nistp384_pre_comp_dup(NISTP384_PRE_COMP *);
  319. NISTP521_PRE_COMP *EC_nistp521_pre_comp_dup(NISTP521_PRE_COMP *);
  320. NISTZ256_PRE_COMP *EC_nistz256_pre_comp_dup(NISTZ256_PRE_COMP *);
  321. NISTP256_PRE_COMP *EC_nistp256_pre_comp_dup(NISTP256_PRE_COMP *);
  322. EC_PRE_COMP *EC_ec_pre_comp_dup(EC_PRE_COMP *);
  323. void EC_pre_comp_free(EC_GROUP *group);
  324. void EC_nistp224_pre_comp_free(NISTP224_PRE_COMP *);
  325. void EC_nistp256_pre_comp_free(NISTP256_PRE_COMP *);
  326. void ossl_ec_nistp384_pre_comp_free(NISTP384_PRE_COMP *);
  327. void EC_nistp521_pre_comp_free(NISTP521_PRE_COMP *);
  328. void EC_nistz256_pre_comp_free(NISTZ256_PRE_COMP *);
  329. void EC_ec_pre_comp_free(EC_PRE_COMP *);
  330. /*
  331. * method functions in ec_mult.c (ec_lib.c uses these as defaults if
  332. * group->method->mul is 0)
  333. */
  334. int ossl_ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
  335. size_t num, const EC_POINT *points[],
  336. const BIGNUM *scalars[], BN_CTX *);
  337. int ossl_ec_wNAF_precompute_mult(EC_GROUP *group, BN_CTX *);
  338. int ossl_ec_wNAF_have_precompute_mult(const EC_GROUP *group);
  339. /* method functions in ecp_smpl.c */
  340. int ossl_ec_GFp_simple_group_init(EC_GROUP *);
  341. void ossl_ec_GFp_simple_group_finish(EC_GROUP *);
  342. void ossl_ec_GFp_simple_group_clear_finish(EC_GROUP *);
  343. int ossl_ec_GFp_simple_group_copy(EC_GROUP *, const EC_GROUP *);
  344. int ossl_ec_GFp_simple_group_set_curve(EC_GROUP *, const BIGNUM *p,
  345. const BIGNUM *a, const BIGNUM *b,
  346. BN_CTX *);
  347. int ossl_ec_GFp_simple_group_get_curve(const EC_GROUP *, BIGNUM *p, BIGNUM *a,
  348. BIGNUM *b, BN_CTX *);
  349. int ossl_ec_GFp_simple_group_get_degree(const EC_GROUP *);
  350. int ossl_ec_GFp_simple_group_check_discriminant(const EC_GROUP *, BN_CTX *);
  351. int ossl_ec_GFp_simple_point_init(EC_POINT *);
  352. void ossl_ec_GFp_simple_point_finish(EC_POINT *);
  353. void ossl_ec_GFp_simple_point_clear_finish(EC_POINT *);
  354. int ossl_ec_GFp_simple_point_copy(EC_POINT *, const EC_POINT *);
  355. int ossl_ec_GFp_simple_point_set_to_infinity(const EC_GROUP *, EC_POINT *);
  356. int ossl_ec_GFp_simple_set_Jprojective_coordinates_GFp(const EC_GROUP *,
  357. EC_POINT *,
  358. const BIGNUM *x,
  359. const BIGNUM *y,
  360. const BIGNUM *z,
  361. BN_CTX *);
  362. int ossl_ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP *,
  363. const EC_POINT *,
  364. BIGNUM *x,
  365. BIGNUM *y, BIGNUM *z,
  366. BN_CTX *);
  367. int ossl_ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP *, EC_POINT *,
  368. const BIGNUM *x,
  369. const BIGNUM *y, BN_CTX *);
  370. int ossl_ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP *,
  371. const EC_POINT *, BIGNUM *x,
  372. BIGNUM *y, BN_CTX *);
  373. int ossl_ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *, EC_POINT *,
  374. const BIGNUM *x, int y_bit,
  375. BN_CTX *);
  376. size_t ossl_ec_GFp_simple_point2oct(const EC_GROUP *, const EC_POINT *,
  377. point_conversion_form_t form,
  378. unsigned char *buf, size_t len, BN_CTX *);
  379. int ossl_ec_GFp_simple_oct2point(const EC_GROUP *, EC_POINT *,
  380. const unsigned char *buf, size_t len, BN_CTX *);
  381. int ossl_ec_GFp_simple_add(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
  382. const EC_POINT *b, BN_CTX *);
  383. int ossl_ec_GFp_simple_dbl(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
  384. BN_CTX *);
  385. int ossl_ec_GFp_simple_invert(const EC_GROUP *, EC_POINT *, BN_CTX *);
  386. int ossl_ec_GFp_simple_is_at_infinity(const EC_GROUP *, const EC_POINT *);
  387. int ossl_ec_GFp_simple_is_on_curve(const EC_GROUP *, const EC_POINT *, BN_CTX *);
  388. int ossl_ec_GFp_simple_cmp(const EC_GROUP *, const EC_POINT *a,
  389. const EC_POINT *b, BN_CTX *);
  390. int ossl_ec_GFp_simple_make_affine(const EC_GROUP *, EC_POINT *, BN_CTX *);
  391. int ossl_ec_GFp_simple_points_make_affine(const EC_GROUP *, size_t num,
  392. EC_POINT *[], BN_CTX *);
  393. int ossl_ec_GFp_simple_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  394. const BIGNUM *b, BN_CTX *);
  395. int ossl_ec_GFp_simple_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  396. BN_CTX *);
  397. int ossl_ec_GFp_simple_field_inv(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  398. BN_CTX *);
  399. int ossl_ec_GFp_simple_blind_coordinates(const EC_GROUP *group, EC_POINT *p,
  400. BN_CTX *ctx);
  401. int ossl_ec_GFp_simple_ladder_pre(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_step(const EC_GROUP *group,
  405. EC_POINT *r, EC_POINT *s,
  406. EC_POINT *p, BN_CTX *ctx);
  407. int ossl_ec_GFp_simple_ladder_post(const EC_GROUP *group,
  408. EC_POINT *r, EC_POINT *s,
  409. EC_POINT *p, BN_CTX *ctx);
  410. /* method functions in ecp_mont.c */
  411. int ossl_ec_GFp_mont_group_init(EC_GROUP *);
  412. int ossl_ec_GFp_mont_group_set_curve(EC_GROUP *, const BIGNUM *p,
  413. const BIGNUM *a,
  414. const BIGNUM *b, BN_CTX *);
  415. void ossl_ec_GFp_mont_group_finish(EC_GROUP *);
  416. void ossl_ec_GFp_mont_group_clear_finish(EC_GROUP *);
  417. int ossl_ec_GFp_mont_group_copy(EC_GROUP *, const EC_GROUP *);
  418. int ossl_ec_GFp_mont_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  419. const BIGNUM *b, BN_CTX *);
  420. int ossl_ec_GFp_mont_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  421. BN_CTX *);
  422. int ossl_ec_GFp_mont_field_inv(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  423. BN_CTX *);
  424. int ossl_ec_GFp_mont_field_encode(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  425. BN_CTX *);
  426. int ossl_ec_GFp_mont_field_decode(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  427. BN_CTX *);
  428. int ossl_ec_GFp_mont_field_set_to_one(const EC_GROUP *, BIGNUM *r, BN_CTX *);
  429. /* method functions in ecp_nist.c */
  430. int ossl_ec_GFp_nist_group_copy(EC_GROUP *dest, const EC_GROUP *src);
  431. int ossl_ec_GFp_nist_group_set_curve(EC_GROUP *, const BIGNUM *p,
  432. const BIGNUM *a, const BIGNUM *b, BN_CTX *);
  433. int ossl_ec_GFp_nist_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  434. const BIGNUM *b, BN_CTX *);
  435. int ossl_ec_GFp_nist_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  436. BN_CTX *);
  437. /* method functions in ec2_smpl.c */
  438. int ossl_ec_GF2m_simple_group_init(EC_GROUP *);
  439. void ossl_ec_GF2m_simple_group_finish(EC_GROUP *);
  440. void ossl_ec_GF2m_simple_group_clear_finish(EC_GROUP *);
  441. int ossl_ec_GF2m_simple_group_copy(EC_GROUP *, const EC_GROUP *);
  442. int ossl_ec_GF2m_simple_group_set_curve(EC_GROUP *, const BIGNUM *p,
  443. const BIGNUM *a, const BIGNUM *b,
  444. BN_CTX *);
  445. int ossl_ec_GF2m_simple_group_get_curve(const EC_GROUP *, BIGNUM *p, BIGNUM *a,
  446. BIGNUM *b, BN_CTX *);
  447. int ossl_ec_GF2m_simple_group_get_degree(const EC_GROUP *);
  448. int ossl_ec_GF2m_simple_group_check_discriminant(const EC_GROUP *, BN_CTX *);
  449. int ossl_ec_GF2m_simple_point_init(EC_POINT *);
  450. void ossl_ec_GF2m_simple_point_finish(EC_POINT *);
  451. void ossl_ec_GF2m_simple_point_clear_finish(EC_POINT *);
  452. int ossl_ec_GF2m_simple_point_copy(EC_POINT *, const EC_POINT *);
  453. int ossl_ec_GF2m_simple_point_set_to_infinity(const EC_GROUP *, EC_POINT *);
  454. int ossl_ec_GF2m_simple_point_set_affine_coordinates(const EC_GROUP *,
  455. EC_POINT *,
  456. const BIGNUM *x,
  457. const BIGNUM *y, BN_CTX *);
  458. int ossl_ec_GF2m_simple_point_get_affine_coordinates(const EC_GROUP *,
  459. const EC_POINT *, BIGNUM *x,
  460. BIGNUM *y, BN_CTX *);
  461. int ossl_ec_GF2m_simple_set_compressed_coordinates(const EC_GROUP *, EC_POINT *,
  462. const BIGNUM *x, int y_bit,
  463. BN_CTX *);
  464. size_t ossl_ec_GF2m_simple_point2oct(const EC_GROUP *, const EC_POINT *,
  465. point_conversion_form_t form,
  466. unsigned char *buf, size_t len, BN_CTX *);
  467. int ossl_ec_GF2m_simple_oct2point(const EC_GROUP *, EC_POINT *,
  468. const unsigned char *buf, size_t len, BN_CTX *);
  469. int ossl_ec_GF2m_simple_add(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
  470. const EC_POINT *b, BN_CTX *);
  471. int ossl_ec_GF2m_simple_dbl(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
  472. BN_CTX *);
  473. int ossl_ec_GF2m_simple_invert(const EC_GROUP *, EC_POINT *, BN_CTX *);
  474. int ossl_ec_GF2m_simple_is_at_infinity(const EC_GROUP *, const EC_POINT *);
  475. int ossl_ec_GF2m_simple_is_on_curve(const EC_GROUP *, const EC_POINT *, BN_CTX *);
  476. int ossl_ec_GF2m_simple_cmp(const EC_GROUP *, const EC_POINT *a,
  477. const EC_POINT *b, BN_CTX *);
  478. int ossl_ec_GF2m_simple_make_affine(const EC_GROUP *, EC_POINT *, BN_CTX *);
  479. int ossl_ec_GF2m_simple_points_make_affine(const EC_GROUP *, size_t num,
  480. EC_POINT *[], BN_CTX *);
  481. int ossl_ec_GF2m_simple_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  482. const BIGNUM *b, BN_CTX *);
  483. int ossl_ec_GF2m_simple_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  484. BN_CTX *);
  485. int ossl_ec_GF2m_simple_field_div(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  486. const BIGNUM *b, BN_CTX *);
  487. #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
  488. # ifdef B_ENDIAN
  489. # error "Can not enable ec_nistp_64_gcc_128 on big-endian systems"
  490. # endif
  491. /* method functions in ecp_nistp224.c */
  492. int ossl_ec_GFp_nistp224_group_init(EC_GROUP *group);
  493. int ossl_ec_GFp_nistp224_group_set_curve(EC_GROUP *group, const BIGNUM *p,
  494. const BIGNUM *a, const BIGNUM *n,
  495. BN_CTX *);
  496. int ossl_ec_GFp_nistp224_point_get_affine_coordinates(const EC_GROUP *group,
  497. const EC_POINT *point,
  498. BIGNUM *x, BIGNUM *y,
  499. BN_CTX *ctx);
  500. int ossl_ec_GFp_nistp224_mul(const EC_GROUP *group, EC_POINT *r,
  501. const BIGNUM *scalar, size_t num,
  502. const EC_POINT *points[], const BIGNUM *scalars[],
  503. BN_CTX *);
  504. int ossl_ec_GFp_nistp224_points_mul(const EC_GROUP *group, EC_POINT *r,
  505. const BIGNUM *scalar, size_t num,
  506. const EC_POINT *points[],
  507. const BIGNUM *scalars[], BN_CTX *ctx);
  508. int ossl_ec_GFp_nistp224_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
  509. int ossl_ec_GFp_nistp224_have_precompute_mult(const EC_GROUP *group);
  510. /* method functions in ecp_nistp256.c */
  511. int ossl_ec_GFp_nistp256_group_init(EC_GROUP *group);
  512. int ossl_ec_GFp_nistp256_group_set_curve(EC_GROUP *group, const BIGNUM *p,
  513. const BIGNUM *a, const BIGNUM *n,
  514. BN_CTX *);
  515. int ossl_ec_GFp_nistp256_point_get_affine_coordinates(const EC_GROUP *group,
  516. const EC_POINT *point,
  517. BIGNUM *x, BIGNUM *y,
  518. BN_CTX *ctx);
  519. int ossl_ec_GFp_nistp256_mul(const EC_GROUP *group, EC_POINT *r,
  520. const BIGNUM *scalar, size_t num,
  521. const EC_POINT *points[], const BIGNUM *scalars[],
  522. BN_CTX *);
  523. int ossl_ec_GFp_nistp256_points_mul(const EC_GROUP *group, EC_POINT *r,
  524. const BIGNUM *scalar, size_t num,
  525. const EC_POINT *points[],
  526. const BIGNUM *scalars[], BN_CTX *ctx);
  527. int ossl_ec_GFp_nistp256_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
  528. int ossl_ec_GFp_nistp256_have_precompute_mult(const EC_GROUP *group);
  529. /* method functions in ecp_nistp384.c */
  530. int ossl_ec_GFp_nistp384_group_init(EC_GROUP *group);
  531. int ossl_ec_GFp_nistp384_group_set_curve(EC_GROUP *group, const BIGNUM *p,
  532. const BIGNUM *a, const BIGNUM *n,
  533. BN_CTX *);
  534. int ossl_ec_GFp_nistp384_point_get_affine_coordinates(const EC_GROUP *group,
  535. const EC_POINT *point,
  536. BIGNUM *x, BIGNUM *y,
  537. BN_CTX *ctx);
  538. int ossl_ec_GFp_nistp384_mul(const EC_GROUP *group, EC_POINT *r,
  539. const BIGNUM *scalar, size_t num,
  540. const EC_POINT *points[], const BIGNUM *scalars[],
  541. BN_CTX *);
  542. int ossl_ec_GFp_nistp384_points_mul(const EC_GROUP *group, EC_POINT *r,
  543. const BIGNUM *scalar, size_t num,
  544. const EC_POINT *points[],
  545. const BIGNUM *scalars[], BN_CTX *ctx);
  546. int ossl_ec_GFp_nistp384_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
  547. int ossl_ec_GFp_nistp384_have_precompute_mult(const EC_GROUP *group);
  548. const EC_METHOD *ossl_ec_GFp_nistp384_method(void);
  549. /* method functions in ecp_nistp521.c */
  550. int ossl_ec_GFp_nistp521_group_init(EC_GROUP *group);
  551. int ossl_ec_GFp_nistp521_group_set_curve(EC_GROUP *group, const BIGNUM *p,
  552. const BIGNUM *a, const BIGNUM *n,
  553. BN_CTX *);
  554. int ossl_ec_GFp_nistp521_point_get_affine_coordinates(const EC_GROUP *group,
  555. const EC_POINT *point,
  556. BIGNUM *x, BIGNUM *y,
  557. BN_CTX *ctx);
  558. int ossl_ec_GFp_nistp521_mul(const EC_GROUP *group, EC_POINT *r,
  559. const BIGNUM *scalar, size_t num,
  560. const EC_POINT *points[], const BIGNUM *scalars[],
  561. BN_CTX *);
  562. int ossl_ec_GFp_nistp521_points_mul(const EC_GROUP *group, EC_POINT *r,
  563. const BIGNUM *scalar, size_t num,
  564. const EC_POINT *points[],
  565. const BIGNUM *scalars[], BN_CTX *ctx);
  566. int ossl_ec_GFp_nistp521_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
  567. int ossl_ec_GFp_nistp521_have_precompute_mult(const EC_GROUP *group);
  568. /* utility functions in ecp_nistputil.c */
  569. void ossl_ec_GFp_nistp_points_make_affine_internal(size_t num, void *point_array,
  570. size_t felem_size,
  571. void *tmp_felems,
  572. void (*felem_one) (void *out),
  573. int (*felem_is_zero)
  574. (const void *in),
  575. void (*felem_assign)
  576. (void *out, const void *in),
  577. void (*felem_square)
  578. (void *out, const void *in),
  579. void (*felem_mul)
  580. (void *out,
  581. const void *in1,
  582. const void *in2),
  583. void (*felem_inv)
  584. (void *out, const void *in),
  585. void (*felem_contract)
  586. (void *out, const void *in));
  587. void ossl_ec_GFp_nistp_recode_scalar_bits(unsigned char *sign,
  588. unsigned char *digit,
  589. unsigned char in);
  590. #endif
  591. int ossl_ec_group_simple_order_bits(const EC_GROUP *group);
  592. /**
  593. * Creates a new EC_GROUP object
  594. * \param libctx The associated library context or NULL for the default
  595. * library context
  596. * \param propq Any property query string
  597. * \param meth EC_METHOD to use
  598. * \return newly created EC_GROUP object or NULL in case of an error.
  599. */
  600. EC_GROUP *ossl_ec_group_new_ex(OSSL_LIB_CTX *libctx, const char *propq,
  601. const EC_METHOD *meth);
  602. #ifdef ECP_NISTZ256_ASM
  603. /** Returns GFp methods using montgomery multiplication, with x86-64 optimized
  604. * P256. See http://eprint.iacr.org/2013/816.
  605. * \return EC_METHOD object
  606. */
  607. const EC_METHOD *EC_GFp_nistz256_method(void);
  608. #endif
  609. #ifdef S390X_EC_ASM
  610. const EC_METHOD *EC_GFp_s390x_nistp256_method(void);
  611. const EC_METHOD *EC_GFp_s390x_nistp384_method(void);
  612. const EC_METHOD *EC_GFp_s390x_nistp521_method(void);
  613. #endif
  614. size_t ossl_ec_key_simple_priv2oct(const EC_KEY *eckey,
  615. unsigned char *buf, size_t len);
  616. int ossl_ec_key_simple_oct2priv(EC_KEY *eckey, const unsigned char *buf,
  617. size_t len);
  618. int ossl_ec_key_simple_generate_key(EC_KEY *eckey);
  619. int ossl_ec_key_simple_generate_public_key(EC_KEY *eckey);
  620. int ossl_ec_key_simple_check_key(const EC_KEY *eckey);
  621. #ifdef ECP_SM2P256_ASM
  622. /* Returns optimized methods for SM2 */
  623. const EC_METHOD *EC_GFp_sm2p256_method(void);
  624. #endif
  625. int ossl_ec_curve_nid_from_params(const EC_GROUP *group, BN_CTX *ctx);
  626. /* EC_METHOD definitions */
  627. struct ec_key_method_st {
  628. const char *name;
  629. int32_t flags;
  630. int (*init)(EC_KEY *key);
  631. void (*finish)(EC_KEY *key);
  632. int (*copy)(EC_KEY *dest, const EC_KEY *src);
  633. int (*set_group)(EC_KEY *key, const EC_GROUP *grp);
  634. int (*set_private)(EC_KEY *key, const BIGNUM *priv_key);
  635. int (*set_public)(EC_KEY *key, const EC_POINT *pub_key);
  636. int (*keygen)(EC_KEY *key);
  637. int (*compute_key)(unsigned char **pout, size_t *poutlen,
  638. const EC_POINT *pub_key, const EC_KEY *ecdh);
  639. int (*sign)(int type, const unsigned char *dgst, int dlen, unsigned char
  640. *sig, unsigned int *siglen, const BIGNUM *kinv,
  641. const BIGNUM *r, EC_KEY *eckey);
  642. int (*sign_setup)(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
  643. BIGNUM **rp);
  644. ECDSA_SIG *(*sign_sig)(const unsigned char *dgst, int dgst_len,
  645. const BIGNUM *in_kinv, const BIGNUM *in_r,
  646. EC_KEY *eckey);
  647. int (*verify)(int type, const unsigned char *dgst, int dgst_len,
  648. const unsigned char *sigbuf, int sig_len, EC_KEY *eckey);
  649. int (*verify_sig)(const unsigned char *dgst, int dgst_len,
  650. const ECDSA_SIG *sig, EC_KEY *eckey);
  651. };
  652. #define EC_KEY_METHOD_DYNAMIC 1
  653. EC_KEY *ossl_ec_key_new_method_int(OSSL_LIB_CTX *libctx, const char *propq,
  654. ENGINE *engine);
  655. int ossl_ec_key_gen(EC_KEY *eckey);
  656. int ossl_ecdh_compute_key(unsigned char **pout, size_t *poutlen,
  657. const EC_POINT *pub_key, const EC_KEY *ecdh);
  658. int ossl_ecdh_simple_compute_key(unsigned char **pout, size_t *poutlen,
  659. const EC_POINT *pub_key, const EC_KEY *ecdh);
  660. struct ECDSA_SIG_st {
  661. BIGNUM *r;
  662. BIGNUM *s;
  663. };
  664. int ossl_ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
  665. BIGNUM **rp);
  666. int ossl_ecdsa_sign(int type, const unsigned char *dgst, int dlen,
  667. unsigned char *sig, unsigned int *siglen,
  668. const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey);
  669. ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
  670. const BIGNUM *in_kinv, const BIGNUM *in_r,
  671. EC_KEY *eckey);
  672. int ossl_ecdsa_verify(int type, const unsigned char *dgst, int dgst_len,
  673. const unsigned char *sigbuf, int sig_len, EC_KEY *eckey);
  674. int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len,
  675. const ECDSA_SIG *sig, EC_KEY *eckey);
  676. int ossl_ecdsa_simple_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
  677. BIGNUM **rp);
  678. ECDSA_SIG *ossl_ecdsa_simple_sign_sig(const unsigned char *dgst, int dgst_len,
  679. const BIGNUM *in_kinv, const BIGNUM *in_r,
  680. EC_KEY *eckey);
  681. int ossl_ecdsa_simple_verify_sig(const unsigned char *dgst, int dgst_len,
  682. const ECDSA_SIG *sig, EC_KEY *eckey);
  683. /*-
  684. * This functions computes a single point multiplication over the EC group,
  685. * using, at a high level, a Montgomery ladder with conditional swaps, with
  686. * various timing attack defenses.
  687. *
  688. * It performs either a fixed point multiplication
  689. * (scalar * generator)
  690. * when point is NULL, or a variable point multiplication
  691. * (scalar * point)
  692. * when point is not NULL.
  693. *
  694. * `scalar` cannot be NULL and should be in the range [0,n) otherwise all
  695. * constant time bets are off (where n is the cardinality of the EC group).
  696. *
  697. * This function expects `group->order` and `group->cardinality` to be well
  698. * defined and non-zero: it fails with an error code otherwise.
  699. *
  700. * NB: This says nothing about the constant-timeness of the ladder step
  701. * implementation (i.e., the default implementation is based on EC_POINT_add and
  702. * EC_POINT_dbl, which of course are not constant time themselves) or the
  703. * underlying multiprecision arithmetic.
  704. *
  705. * The product is stored in `r`.
  706. *
  707. * This is an internal function: callers are in charge of ensuring that the
  708. * input parameters `group`, `r`, `scalar` and `ctx` are not NULL.
  709. *
  710. * Returns 1 on success, 0 otherwise.
  711. */
  712. int ossl_ec_scalar_mul_ladder(const EC_GROUP *group, EC_POINT *r,
  713. const BIGNUM *scalar, const EC_POINT *point,
  714. BN_CTX *ctx);
  715. int ossl_ec_point_blind_coordinates(const EC_GROUP *group, EC_POINT *p,
  716. BN_CTX *ctx);
  717. static ossl_inline int ec_point_ladder_pre(const EC_GROUP *group,
  718. EC_POINT *r, EC_POINT *s,
  719. EC_POINT *p, BN_CTX *ctx)
  720. {
  721. if (group->meth->ladder_pre != NULL)
  722. return group->meth->ladder_pre(group, r, s, p, ctx);
  723. if (!EC_POINT_copy(s, p)
  724. || !EC_POINT_dbl(group, r, s, ctx))
  725. return 0;
  726. return 1;
  727. }
  728. static ossl_inline int ec_point_ladder_step(const EC_GROUP *group,
  729. EC_POINT *r, EC_POINT *s,
  730. EC_POINT *p, BN_CTX *ctx)
  731. {
  732. if (group->meth->ladder_step != NULL)
  733. return group->meth->ladder_step(group, r, s, p, ctx);
  734. if (!EC_POINT_add(group, s, r, s, ctx)
  735. || !EC_POINT_dbl(group, r, r, ctx))
  736. return 0;
  737. return 1;
  738. }
  739. static ossl_inline int ec_point_ladder_post(const EC_GROUP *group,
  740. EC_POINT *r, EC_POINT *s,
  741. EC_POINT *p, BN_CTX *ctx)
  742. {
  743. if (group->meth->ladder_post != NULL)
  744. return group->meth->ladder_post(group, r, s, p, ctx);
  745. return 1;
  746. }