ec_local.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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. # ifdef B_ENDIAN
  486. # error "Can not enable ec_nistp_64_gcc_128 on big-endian systems"
  487. # endif
  488. /* method functions in ecp_nistp224.c */
  489. int ossl_ec_GFp_nistp224_group_init(EC_GROUP *group);
  490. int ossl_ec_GFp_nistp224_group_set_curve(EC_GROUP *group, const BIGNUM *p,
  491. const BIGNUM *a, const BIGNUM *n,
  492. BN_CTX *);
  493. int ossl_ec_GFp_nistp224_point_get_affine_coordinates(const EC_GROUP *group,
  494. const EC_POINT *point,
  495. BIGNUM *x, BIGNUM *y,
  496. BN_CTX *ctx);
  497. int ossl_ec_GFp_nistp224_mul(const EC_GROUP *group, EC_POINT *r,
  498. const BIGNUM *scalar, size_t num,
  499. const EC_POINT *points[], const BIGNUM *scalars[],
  500. BN_CTX *);
  501. int ossl_ec_GFp_nistp224_points_mul(const EC_GROUP *group, EC_POINT *r,
  502. const BIGNUM *scalar, size_t num,
  503. const EC_POINT *points[],
  504. const BIGNUM *scalars[], BN_CTX *ctx);
  505. int ossl_ec_GFp_nistp224_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
  506. int ossl_ec_GFp_nistp224_have_precompute_mult(const EC_GROUP *group);
  507. /* method functions in ecp_nistp256.c */
  508. int ossl_ec_GFp_nistp256_group_init(EC_GROUP *group);
  509. int ossl_ec_GFp_nistp256_group_set_curve(EC_GROUP *group, const BIGNUM *p,
  510. const BIGNUM *a, const BIGNUM *n,
  511. BN_CTX *);
  512. int ossl_ec_GFp_nistp256_point_get_affine_coordinates(const EC_GROUP *group,
  513. const EC_POINT *point,
  514. BIGNUM *x, BIGNUM *y,
  515. BN_CTX *ctx);
  516. int ossl_ec_GFp_nistp256_mul(const EC_GROUP *group, EC_POINT *r,
  517. const BIGNUM *scalar, size_t num,
  518. const EC_POINT *points[], const BIGNUM *scalars[],
  519. BN_CTX *);
  520. int ossl_ec_GFp_nistp256_points_mul(const EC_GROUP *group, EC_POINT *r,
  521. const BIGNUM *scalar, size_t num,
  522. const EC_POINT *points[],
  523. const BIGNUM *scalars[], BN_CTX *ctx);
  524. int ossl_ec_GFp_nistp256_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
  525. int ossl_ec_GFp_nistp256_have_precompute_mult(const EC_GROUP *group);
  526. /* method functions in ecp_nistp521.c */
  527. int ossl_ec_GFp_nistp521_group_init(EC_GROUP *group);
  528. int ossl_ec_GFp_nistp521_group_set_curve(EC_GROUP *group, const BIGNUM *p,
  529. const BIGNUM *a, const BIGNUM *n,
  530. BN_CTX *);
  531. int ossl_ec_GFp_nistp521_point_get_affine_coordinates(const EC_GROUP *group,
  532. const EC_POINT *point,
  533. BIGNUM *x, BIGNUM *y,
  534. BN_CTX *ctx);
  535. int ossl_ec_GFp_nistp521_mul(const EC_GROUP *group, EC_POINT *r,
  536. const BIGNUM *scalar, size_t num,
  537. const EC_POINT *points[], const BIGNUM *scalars[],
  538. BN_CTX *);
  539. int ossl_ec_GFp_nistp521_points_mul(const EC_GROUP *group, EC_POINT *r,
  540. const BIGNUM *scalar, size_t num,
  541. const EC_POINT *points[],
  542. const BIGNUM *scalars[], BN_CTX *ctx);
  543. int ossl_ec_GFp_nistp521_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
  544. int ossl_ec_GFp_nistp521_have_precompute_mult(const EC_GROUP *group);
  545. /* utility functions in ecp_nistputil.c */
  546. void ossl_ec_GFp_nistp_points_make_affine_internal(size_t num, void *point_array,
  547. size_t felem_size,
  548. void *tmp_felems,
  549. void (*felem_one) (void *out),
  550. int (*felem_is_zero)
  551. (const void *in),
  552. void (*felem_assign)
  553. (void *out, const void *in),
  554. void (*felem_square)
  555. (void *out, const void *in),
  556. void (*felem_mul)
  557. (void *out,
  558. const void *in1,
  559. const void *in2),
  560. void (*felem_inv)
  561. (void *out, const void *in),
  562. void (*felem_contract)
  563. (void *out, const void *in));
  564. void ossl_ec_GFp_nistp_recode_scalar_bits(unsigned char *sign,
  565. unsigned char *digit,
  566. unsigned char in);
  567. #endif
  568. int ossl_ec_group_simple_order_bits(const EC_GROUP *group);
  569. /**
  570. * Creates a new EC_GROUP object
  571. * \param libctx The associated library context or NULL for the default
  572. * library context
  573. * \param propq Any property query string
  574. * \param meth EC_METHOD to use
  575. * \return newly created EC_GROUP object or NULL in case of an error.
  576. */
  577. EC_GROUP *ossl_ec_group_new_ex(OSSL_LIB_CTX *libctx, const char *propq,
  578. const EC_METHOD *meth);
  579. #ifdef ECP_NISTZ256_ASM
  580. /** Returns GFp methods using montgomery multiplication, with x86-64 optimized
  581. * P256. See http://eprint.iacr.org/2013/816.
  582. * \return EC_METHOD object
  583. */
  584. const EC_METHOD *EC_GFp_nistz256_method(void);
  585. #endif
  586. #ifdef S390X_EC_ASM
  587. const EC_METHOD *EC_GFp_s390x_nistp256_method(void);
  588. const EC_METHOD *EC_GFp_s390x_nistp384_method(void);
  589. const EC_METHOD *EC_GFp_s390x_nistp521_method(void);
  590. #endif
  591. size_t ossl_ec_key_simple_priv2oct(const EC_KEY *eckey,
  592. unsigned char *buf, size_t len);
  593. int ossl_ec_key_simple_oct2priv(EC_KEY *eckey, const unsigned char *buf,
  594. size_t len);
  595. int ossl_ec_key_simple_generate_key(EC_KEY *eckey);
  596. int ossl_ec_key_simple_generate_public_key(EC_KEY *eckey);
  597. int ossl_ec_key_simple_check_key(const EC_KEY *eckey);
  598. int ossl_ec_curve_nid_from_params(const EC_GROUP *group, BN_CTX *ctx);
  599. /* EC_METHOD definitions */
  600. struct ec_key_method_st {
  601. const char *name;
  602. int32_t flags;
  603. int (*init)(EC_KEY *key);
  604. void (*finish)(EC_KEY *key);
  605. int (*copy)(EC_KEY *dest, const EC_KEY *src);
  606. int (*set_group)(EC_KEY *key, const EC_GROUP *grp);
  607. int (*set_private)(EC_KEY *key, const BIGNUM *priv_key);
  608. int (*set_public)(EC_KEY *key, const EC_POINT *pub_key);
  609. int (*keygen)(EC_KEY *key);
  610. int (*compute_key)(unsigned char **pout, size_t *poutlen,
  611. const EC_POINT *pub_key, const EC_KEY *ecdh);
  612. int (*sign)(int type, const unsigned char *dgst, int dlen, unsigned char
  613. *sig, unsigned int *siglen, const BIGNUM *kinv,
  614. const BIGNUM *r, EC_KEY *eckey);
  615. int (*sign_setup)(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
  616. BIGNUM **rp);
  617. ECDSA_SIG *(*sign_sig)(const unsigned char *dgst, int dgst_len,
  618. const BIGNUM *in_kinv, const BIGNUM *in_r,
  619. EC_KEY *eckey);
  620. int (*verify)(int type, const unsigned char *dgst, int dgst_len,
  621. const unsigned char *sigbuf, int sig_len, EC_KEY *eckey);
  622. int (*verify_sig)(const unsigned char *dgst, int dgst_len,
  623. const ECDSA_SIG *sig, EC_KEY *eckey);
  624. };
  625. #define EC_KEY_METHOD_DYNAMIC 1
  626. EC_KEY *ossl_ec_key_new_method_int(OSSL_LIB_CTX *libctx, const char *propq,
  627. ENGINE *engine);
  628. int ossl_ec_key_gen(EC_KEY *eckey);
  629. int ossl_ecdh_compute_key(unsigned char **pout, size_t *poutlen,
  630. const EC_POINT *pub_key, const EC_KEY *ecdh);
  631. int ossl_ecdh_simple_compute_key(unsigned char **pout, size_t *poutlen,
  632. const EC_POINT *pub_key, const EC_KEY *ecdh);
  633. struct ECDSA_SIG_st {
  634. BIGNUM *r;
  635. BIGNUM *s;
  636. };
  637. int ossl_ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
  638. BIGNUM **rp);
  639. int ossl_ecdsa_sign(int type, const unsigned char *dgst, int dlen,
  640. unsigned char *sig, unsigned int *siglen,
  641. const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey);
  642. ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
  643. const BIGNUM *in_kinv, const BIGNUM *in_r,
  644. EC_KEY *eckey);
  645. int ossl_ecdsa_verify(int type, const unsigned char *dgst, int dgst_len,
  646. const unsigned char *sigbuf, int sig_len, EC_KEY *eckey);
  647. int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len,
  648. const ECDSA_SIG *sig, EC_KEY *eckey);
  649. int ossl_ecdsa_simple_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
  650. BIGNUM **rp);
  651. ECDSA_SIG *ossl_ecdsa_simple_sign_sig(const unsigned char *dgst, int dgst_len,
  652. const BIGNUM *in_kinv, const BIGNUM *in_r,
  653. EC_KEY *eckey);
  654. int ossl_ecdsa_simple_verify_sig(const unsigned char *dgst, int dgst_len,
  655. const ECDSA_SIG *sig, EC_KEY *eckey);
  656. /*-
  657. * This functions computes a single point multiplication over the EC group,
  658. * using, at a high level, a Montgomery ladder with conditional swaps, with
  659. * various timing attack defenses.
  660. *
  661. * It performs either a fixed point multiplication
  662. * (scalar * generator)
  663. * when point is NULL, or a variable point multiplication
  664. * (scalar * point)
  665. * when point is not NULL.
  666. *
  667. * `scalar` cannot be NULL and should be in the range [0,n) otherwise all
  668. * constant time bets are off (where n is the cardinality of the EC group).
  669. *
  670. * This function expects `group->order` and `group->cardinality` to be well
  671. * defined and non-zero: it fails with an error code otherwise.
  672. *
  673. * NB: This says nothing about the constant-timeness of the ladder step
  674. * implementation (i.e., the default implementation is based on EC_POINT_add and
  675. * EC_POINT_dbl, which of course are not constant time themselves) or the
  676. * underlying multiprecision arithmetic.
  677. *
  678. * The product is stored in `r`.
  679. *
  680. * This is an internal function: callers are in charge of ensuring that the
  681. * input parameters `group`, `r`, `scalar` and `ctx` are not NULL.
  682. *
  683. * Returns 1 on success, 0 otherwise.
  684. */
  685. int ossl_ec_scalar_mul_ladder(const EC_GROUP *group, EC_POINT *r,
  686. const BIGNUM *scalar, const EC_POINT *point,
  687. BN_CTX *ctx);
  688. int ossl_ec_point_blind_coordinates(const EC_GROUP *group, EC_POINT *p,
  689. BN_CTX *ctx);
  690. static ossl_inline int ec_point_ladder_pre(const EC_GROUP *group,
  691. EC_POINT *r, EC_POINT *s,
  692. EC_POINT *p, BN_CTX *ctx)
  693. {
  694. if (group->meth->ladder_pre != NULL)
  695. return group->meth->ladder_pre(group, r, s, p, ctx);
  696. if (!EC_POINT_copy(s, p)
  697. || !EC_POINT_dbl(group, r, s, ctx))
  698. return 0;
  699. return 1;
  700. }
  701. static ossl_inline int ec_point_ladder_step(const EC_GROUP *group,
  702. EC_POINT *r, EC_POINT *s,
  703. EC_POINT *p, BN_CTX *ctx)
  704. {
  705. if (group->meth->ladder_step != NULL)
  706. return group->meth->ladder_step(group, r, s, p, ctx);
  707. if (!EC_POINT_add(group, s, r, s, ctx)
  708. || !EC_POINT_dbl(group, r, r, ctx))
  709. return 0;
  710. return 1;
  711. }
  712. static ossl_inline int ec_point_ladder_post(const EC_GROUP *group,
  713. EC_POINT *r, EC_POINT *s,
  714. EC_POINT *p, BN_CTX *ctx)
  715. {
  716. if (group->meth->ladder_post != NULL)
  717. return group->meth->ladder_post(group, r, s, p, ctx);
  718. return 1;
  719. }