2
0

ec_lcl.h 30 KB

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