2 * Copyright 2001-2018 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
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
13 #include <openssl/obj_mac.h>
14 #include <openssl/ec.h>
15 #include <openssl/bn.h>
16 #include "internal/refcount.h"
17 #include "curve448/curve448_lcl.h"
19 #if defined(__SUNPRO_C)
20 # if __SUNPRO_C >= 0x520
21 # pragma error_messages (off,E_ARRAY_OF_INCOMPLETE_NONAME,E_ARRAY_OF_INCOMPLETE)
25 /* Use default functions for poin2oct, oct2point and compressed coordinates */
26 #define EC_FLAGS_DEFAULT_OCT 0x1
28 /* Use custom formats for EC_GROUP, EC_POINT and EC_KEY */
29 #define EC_FLAGS_CUSTOM_CURVE 0x2
31 /* Curve does not support signing operations */
32 #define EC_FLAGS_NO_SIGN 0x4
35 * Structure details are not part of the exported interface, so all this may
36 * change in future versions.
40 /* Various method flags */
42 /* used by EC_METHOD_get_field_type: */
43 int field_type; /* a NID */
45 * used by EC_GROUP_new, EC_GROUP_free, EC_GROUP_clear_free,
48 int (*group_init) (EC_GROUP *);
49 void (*group_finish) (EC_GROUP *);
50 void (*group_clear_finish) (EC_GROUP *);
51 int (*group_copy) (EC_GROUP *, const EC_GROUP *);
52 /* used by EC_GROUP_set_curve_GFp, EC_GROUP_get_curve_GFp, */
53 /* EC_GROUP_set_curve_GF2m, and EC_GROUP_get_curve_GF2m: */
54 int (*group_set_curve) (EC_GROUP *, const BIGNUM *p, const BIGNUM *a,
55 const BIGNUM *b, BN_CTX *);
56 int (*group_get_curve) (const EC_GROUP *, BIGNUM *p, BIGNUM *a, BIGNUM *b,
58 /* used by EC_GROUP_get_degree: */
59 int (*group_get_degree) (const EC_GROUP *);
60 int (*group_order_bits) (const EC_GROUP *);
61 /* used by EC_GROUP_check: */
62 int (*group_check_discriminant) (const EC_GROUP *, BN_CTX *);
64 * used by EC_POINT_new, EC_POINT_free, EC_POINT_clear_free,
67 int (*point_init) (EC_POINT *);
68 void (*point_finish) (EC_POINT *);
69 void (*point_clear_finish) (EC_POINT *);
70 int (*point_copy) (EC_POINT *, const EC_POINT *);
72 * used by EC_POINT_set_to_infinity,
73 * EC_POINT_set_Jprojective_coordinates_GFp,
74 * EC_POINT_get_Jprojective_coordinates_GFp,
75 * EC_POINT_set_affine_coordinates_GFp, ..._GF2m,
76 * EC_POINT_get_affine_coordinates_GFp, ..._GF2m,
77 * EC_POINT_set_compressed_coordinates_GFp, ..._GF2m:
79 int (*point_set_to_infinity) (const EC_GROUP *, EC_POINT *);
80 int (*point_set_Jprojective_coordinates_GFp) (const EC_GROUP *,
81 EC_POINT *, const BIGNUM *x,
83 const BIGNUM *z, BN_CTX *);
84 int (*point_get_Jprojective_coordinates_GFp) (const EC_GROUP *,
85 const EC_POINT *, BIGNUM *x,
88 int (*point_set_affine_coordinates) (const EC_GROUP *, EC_POINT *,
89 const BIGNUM *x, const BIGNUM *y,
91 int (*point_get_affine_coordinates) (const EC_GROUP *, const EC_POINT *,
92 BIGNUM *x, BIGNUM *y, BN_CTX *);
93 int (*point_set_compressed_coordinates) (const EC_GROUP *, EC_POINT *,
94 const BIGNUM *x, int y_bit,
96 /* used by EC_POINT_point2oct, EC_POINT_oct2point: */
97 size_t (*point2oct) (const EC_GROUP *, const EC_POINT *,
98 point_conversion_form_t form, unsigned char *buf,
99 size_t len, BN_CTX *);
100 int (*oct2point) (const EC_GROUP *, EC_POINT *, const unsigned char *buf,
101 size_t len, BN_CTX *);
102 /* used by EC_POINT_add, EC_POINT_dbl, ECP_POINT_invert: */
103 int (*add) (const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
104 const EC_POINT *b, BN_CTX *);
105 int (*dbl) (const EC_GROUP *, EC_POINT *r, const EC_POINT *a, BN_CTX *);
106 int (*invert) (const EC_GROUP *, EC_POINT *, BN_CTX *);
108 * used by EC_POINT_is_at_infinity, EC_POINT_is_on_curve, EC_POINT_cmp:
110 int (*is_at_infinity) (const EC_GROUP *, const EC_POINT *);
111 int (*is_on_curve) (const EC_GROUP *, const EC_POINT *, BN_CTX *);
112 int (*point_cmp) (const EC_GROUP *, const EC_POINT *a, const EC_POINT *b,
114 /* used by EC_POINT_make_affine, EC_POINTs_make_affine: */
115 int (*make_affine) (const EC_GROUP *, EC_POINT *, BN_CTX *);
116 int (*points_make_affine) (const EC_GROUP *, size_t num, EC_POINT *[],
119 * used by EC_POINTs_mul, EC_POINT_mul, EC_POINT_precompute_mult,
120 * EC_POINT_have_precompute_mult (default implementations are used if the
121 * 'mul' pointer is 0):
124 * mul() calculates the value
126 * r := generator * scalar
127 * + points[0] * scalars[0]
129 * + points[num-1] * scalars[num-1].
131 * For a fixed point multiplication (scalar != NULL, num == 0)
132 * or a variable point multiplication (scalar == NULL, num == 1),
133 * mul() must use a constant time algorithm: in both cases callers
134 * should provide an input scalar (either scalar or scalars[0])
135 * in the range [0, ec_group_order); for robustness, implementers
136 * should handle the case when the scalar has not been reduced, but
137 * may treat it as an unusual input, without any constant-timeness
140 int (*mul) (const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
141 size_t num, const EC_POINT *points[], const BIGNUM *scalars[],
143 int (*precompute_mult) (EC_GROUP *group, BN_CTX *);
144 int (*have_precompute_mult) (const EC_GROUP *group);
145 /* internal functions */
147 * 'field_mul', 'field_sqr', and 'field_div' can be used by 'add' and
148 * 'dbl' so that the same implementations of point operations can be used
149 * with different optimized implementations of expensive field
152 int (*field_mul) (const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
153 const BIGNUM *b, BN_CTX *);
154 int (*field_sqr) (const EC_GROUP *, BIGNUM *r, const BIGNUM *a, BN_CTX *);
155 int (*field_div) (const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
156 const BIGNUM *b, BN_CTX *);
157 /* e.g. to Montgomery */
158 int (*field_encode) (const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
160 /* e.g. from Montgomery */
161 int (*field_decode) (const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
163 int (*field_set_to_one) (const EC_GROUP *, BIGNUM *r, BN_CTX *);
164 /* private key operations */
165 size_t (*priv2oct)(const EC_KEY *eckey, unsigned char *buf, size_t len);
166 int (*oct2priv)(EC_KEY *eckey, const unsigned char *buf, size_t len);
167 int (*set_private)(EC_KEY *eckey, const BIGNUM *priv_key);
168 int (*keygen)(EC_KEY *eckey);
169 int (*keycheck)(const EC_KEY *eckey);
170 int (*keygenpub)(EC_KEY *eckey);
171 int (*keycopy)(EC_KEY *dst, const EC_KEY *src);
172 void (*keyfinish)(EC_KEY *eckey);
173 /* custom ECDH operation */
174 int (*ecdh_compute_key)(unsigned char **pout, size_t *poutlen,
175 const EC_POINT *pub_key, const EC_KEY *ecdh);
176 /* Inverse modulo order */
177 int (*field_inverse_mod_ord)(const EC_GROUP *, BIGNUM *r, BIGNUM *x,
182 * Types and functions to manipulate pre-computed values.
184 typedef struct nistp224_pre_comp_st NISTP224_PRE_COMP;
185 typedef struct nistp256_pre_comp_st NISTP256_PRE_COMP;
186 typedef struct nistp521_pre_comp_st NISTP521_PRE_COMP;
187 typedef struct nistz256_pre_comp_st NISTZ256_PRE_COMP;
188 typedef struct ec_pre_comp_st EC_PRE_COMP;
191 const EC_METHOD *meth;
192 EC_POINT *generator; /* optional */
193 BIGNUM *order, *cofactor;
194 int curve_name; /* optional NID for named curve */
195 int asn1_flag; /* flag to control the asn1 encoding */
196 point_conversion_form_t asn1_form;
197 unsigned char *seed; /* optional seed for parameters (appears in
201 * The following members are handled by the method functions, even if
202 * they appear generic
205 * Field specification. For curves over GF(p), this is the modulus; for
206 * curves over GF(2^m), this is the irreducible polynomial defining the
211 * Field specification for curves over GF(2^m). The irreducible f(t) is
212 * then of the form: t^poly[0] + t^poly[1] + ... + t^poly[k] where m =
213 * poly[0] > poly[1] > ... > poly[k] = 0. The array is terminated with
214 * poly[k+1]=-1. All elliptic curve irreducibles have at most 5 non-zero
219 * Curve coefficients. (Here the assumption is that BIGNUMs can be used
220 * or abused for all kinds of fields, not just GF(p).) For characteristic
221 * > 3, the curve is defined by a Weierstrass equation of the form y^2 =
222 * x^3 + a*x + b. For characteristic 2, the curve is defined by an
223 * equation of the form y^2 + x*y = x^3 + a*x^2 + b.
226 /* enable optimized point arithmetics for special case */
228 /* method-specific (e.g., Montgomery structure) */
230 /* method-specific */
232 /* method-specific */
233 int (*field_mod_func) (BIGNUM *, const BIGNUM *, const BIGNUM *,
235 /* data for ECDSA inverse */
236 BN_MONT_CTX *mont_data;
239 * Precomputed values for speed. The PCT_xxx names match the
240 * pre_comp.xxx union names; see the SETPRECOMP and HAVEPRECOMP
245 PCT_nistp224, PCT_nistp256, PCT_nistp521, PCT_nistz256,
249 NISTP224_PRE_COMP *nistp224;
250 NISTP256_PRE_COMP *nistp256;
251 NISTP521_PRE_COMP *nistp521;
252 NISTZ256_PRE_COMP *nistz256;
257 #define SETPRECOMP(g, type, pre) \
258 g->pre_comp_type = PCT_##type, g->pre_comp.type = pre
259 #define HAVEPRECOMP(g, type) \
260 g->pre_comp_type == PCT_##type && g->pre_comp.type != NULL
263 const EC_KEY_METHOD *meth;
269 unsigned int enc_flag;
270 point_conversion_form_t conv_form;
271 CRYPTO_REF_COUNT references;
273 CRYPTO_EX_DATA ex_data;
278 const EC_METHOD *meth;
280 * All members except 'meth' are handled by the method functions, even if
281 * they appear generic
285 BIGNUM *Z; /* Jacobian projective coordinates: * (X, Y,
286 * Z) represents (X/Z^2, Y/Z^3) if Z != 0 */
287 int Z_is_one; /* enable optimized point arithmetics for
291 NISTP224_PRE_COMP *EC_nistp224_pre_comp_dup(NISTP224_PRE_COMP *);
292 NISTP256_PRE_COMP *EC_nistp256_pre_comp_dup(NISTP256_PRE_COMP *);
293 NISTP521_PRE_COMP *EC_nistp521_pre_comp_dup(NISTP521_PRE_COMP *);
294 NISTZ256_PRE_COMP *EC_nistz256_pre_comp_dup(NISTZ256_PRE_COMP *);
295 NISTP256_PRE_COMP *EC_nistp256_pre_comp_dup(NISTP256_PRE_COMP *);
296 EC_PRE_COMP *EC_ec_pre_comp_dup(EC_PRE_COMP *);
298 void EC_pre_comp_free(EC_GROUP *group);
299 void EC_nistp224_pre_comp_free(NISTP224_PRE_COMP *);
300 void EC_nistp256_pre_comp_free(NISTP256_PRE_COMP *);
301 void EC_nistp521_pre_comp_free(NISTP521_PRE_COMP *);
302 void EC_nistz256_pre_comp_free(NISTZ256_PRE_COMP *);
303 void EC_ec_pre_comp_free(EC_PRE_COMP *);
306 * method functions in ec_mult.c (ec_lib.c uses these as defaults if
307 * group->method->mul is 0)
309 int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
310 size_t num, const EC_POINT *points[], const BIGNUM *scalars[],
312 int ec_wNAF_precompute_mult(EC_GROUP *group, BN_CTX *);
313 int ec_wNAF_have_precompute_mult(const EC_GROUP *group);
315 /* method functions in ecp_smpl.c */
316 int ec_GFp_simple_group_init(EC_GROUP *);
317 void ec_GFp_simple_group_finish(EC_GROUP *);
318 void ec_GFp_simple_group_clear_finish(EC_GROUP *);
319 int ec_GFp_simple_group_copy(EC_GROUP *, const EC_GROUP *);
320 int ec_GFp_simple_group_set_curve(EC_GROUP *, const BIGNUM *p,
321 const BIGNUM *a, const BIGNUM *b, BN_CTX *);
322 int ec_GFp_simple_group_get_curve(const EC_GROUP *, BIGNUM *p, BIGNUM *a,
323 BIGNUM *b, BN_CTX *);
324 int ec_GFp_simple_group_get_degree(const EC_GROUP *);
325 int ec_GFp_simple_group_check_discriminant(const EC_GROUP *, BN_CTX *);
326 int ec_GFp_simple_point_init(EC_POINT *);
327 void ec_GFp_simple_point_finish(EC_POINT *);
328 void ec_GFp_simple_point_clear_finish(EC_POINT *);
329 int ec_GFp_simple_point_copy(EC_POINT *, const EC_POINT *);
330 int ec_GFp_simple_point_set_to_infinity(const EC_GROUP *, EC_POINT *);
331 int ec_GFp_simple_set_Jprojective_coordinates_GFp(const EC_GROUP *,
332 EC_POINT *, const BIGNUM *x,
334 const BIGNUM *z, BN_CTX *);
335 int ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP *,
336 const EC_POINT *, BIGNUM *x,
337 BIGNUM *y, BIGNUM *z,
339 int ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP *, EC_POINT *,
341 const BIGNUM *y, BN_CTX *);
342 int ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP *,
343 const EC_POINT *, BIGNUM *x,
344 BIGNUM *y, BN_CTX *);
345 int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *, EC_POINT *,
346 const BIGNUM *x, int y_bit,
348 size_t ec_GFp_simple_point2oct(const EC_GROUP *, const EC_POINT *,
349 point_conversion_form_t form,
350 unsigned char *buf, size_t len, BN_CTX *);
351 int ec_GFp_simple_oct2point(const EC_GROUP *, EC_POINT *,
352 const unsigned char *buf, size_t len, BN_CTX *);
353 int ec_GFp_simple_add(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
354 const EC_POINT *b, BN_CTX *);
355 int ec_GFp_simple_dbl(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
357 int ec_GFp_simple_invert(const EC_GROUP *, EC_POINT *, BN_CTX *);
358 int ec_GFp_simple_is_at_infinity(const EC_GROUP *, const EC_POINT *);
359 int ec_GFp_simple_is_on_curve(const EC_GROUP *, const EC_POINT *, BN_CTX *);
360 int ec_GFp_simple_cmp(const EC_GROUP *, const EC_POINT *a, const EC_POINT *b,
362 int ec_GFp_simple_make_affine(const EC_GROUP *, EC_POINT *, BN_CTX *);
363 int ec_GFp_simple_points_make_affine(const EC_GROUP *, size_t num,
364 EC_POINT *[], BN_CTX *);
365 int ec_GFp_simple_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
366 const BIGNUM *b, BN_CTX *);
367 int ec_GFp_simple_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
370 /* method functions in ecp_mont.c */
371 int ec_GFp_mont_group_init(EC_GROUP *);
372 int ec_GFp_mont_group_set_curve(EC_GROUP *, const BIGNUM *p, const BIGNUM *a,
373 const BIGNUM *b, BN_CTX *);
374 void ec_GFp_mont_group_finish(EC_GROUP *);
375 void ec_GFp_mont_group_clear_finish(EC_GROUP *);
376 int ec_GFp_mont_group_copy(EC_GROUP *, const EC_GROUP *);
377 int ec_GFp_mont_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
378 const BIGNUM *b, BN_CTX *);
379 int ec_GFp_mont_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
381 int ec_GFp_mont_field_encode(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
383 int ec_GFp_mont_field_decode(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
385 int ec_GFp_mont_field_set_to_one(const EC_GROUP *, BIGNUM *r, BN_CTX *);
387 /* method functions in ecp_nist.c */
388 int ec_GFp_nist_group_copy(EC_GROUP *dest, const EC_GROUP *src);
389 int ec_GFp_nist_group_set_curve(EC_GROUP *, const BIGNUM *p, const BIGNUM *a,
390 const BIGNUM *b, BN_CTX *);
391 int ec_GFp_nist_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
392 const BIGNUM *b, BN_CTX *);
393 int ec_GFp_nist_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
396 /* method functions in ec2_smpl.c */
397 int ec_GF2m_simple_group_init(EC_GROUP *);
398 void ec_GF2m_simple_group_finish(EC_GROUP *);
399 void ec_GF2m_simple_group_clear_finish(EC_GROUP *);
400 int ec_GF2m_simple_group_copy(EC_GROUP *, const EC_GROUP *);
401 int ec_GF2m_simple_group_set_curve(EC_GROUP *, const BIGNUM *p,
402 const BIGNUM *a, const BIGNUM *b,
404 int ec_GF2m_simple_group_get_curve(const EC_GROUP *, BIGNUM *p, BIGNUM *a,
405 BIGNUM *b, BN_CTX *);
406 int ec_GF2m_simple_group_get_degree(const EC_GROUP *);
407 int ec_GF2m_simple_group_check_discriminant(const EC_GROUP *, BN_CTX *);
408 int ec_GF2m_simple_point_init(EC_POINT *);
409 void ec_GF2m_simple_point_finish(EC_POINT *);
410 void ec_GF2m_simple_point_clear_finish(EC_POINT *);
411 int ec_GF2m_simple_point_copy(EC_POINT *, const EC_POINT *);
412 int ec_GF2m_simple_point_set_to_infinity(const EC_GROUP *, EC_POINT *);
413 int ec_GF2m_simple_point_set_affine_coordinates(const EC_GROUP *, EC_POINT *,
415 const BIGNUM *y, BN_CTX *);
416 int ec_GF2m_simple_point_get_affine_coordinates(const EC_GROUP *,
417 const EC_POINT *, BIGNUM *x,
418 BIGNUM *y, BN_CTX *);
419 int ec_GF2m_simple_set_compressed_coordinates(const EC_GROUP *, EC_POINT *,
420 const BIGNUM *x, int y_bit,
422 size_t ec_GF2m_simple_point2oct(const EC_GROUP *, const EC_POINT *,
423 point_conversion_form_t form,
424 unsigned char *buf, size_t len, BN_CTX *);
425 int ec_GF2m_simple_oct2point(const EC_GROUP *, EC_POINT *,
426 const unsigned char *buf, size_t len, BN_CTX *);
427 int ec_GF2m_simple_add(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
428 const EC_POINT *b, BN_CTX *);
429 int ec_GF2m_simple_dbl(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
431 int ec_GF2m_simple_invert(const EC_GROUP *, EC_POINT *, BN_CTX *);
432 int ec_GF2m_simple_is_at_infinity(const EC_GROUP *, const EC_POINT *);
433 int ec_GF2m_simple_is_on_curve(const EC_GROUP *, const EC_POINT *, BN_CTX *);
434 int ec_GF2m_simple_cmp(const EC_GROUP *, const EC_POINT *a, const EC_POINT *b,
436 int ec_GF2m_simple_make_affine(const EC_GROUP *, EC_POINT *, BN_CTX *);
437 int ec_GF2m_simple_points_make_affine(const EC_GROUP *, size_t num,
438 EC_POINT *[], BN_CTX *);
439 int ec_GF2m_simple_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
440 const BIGNUM *b, BN_CTX *);
441 int ec_GF2m_simple_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
443 int ec_GF2m_simple_field_div(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
444 const BIGNUM *b, BN_CTX *);
446 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
447 /* method functions in ecp_nistp224.c */
448 int ec_GFp_nistp224_group_init(EC_GROUP *group);
449 int ec_GFp_nistp224_group_set_curve(EC_GROUP *group, const BIGNUM *p,
450 const BIGNUM *a, const BIGNUM *n,
452 int ec_GFp_nistp224_point_get_affine_coordinates(const EC_GROUP *group,
453 const EC_POINT *point,
454 BIGNUM *x, BIGNUM *y,
456 int ec_GFp_nistp224_mul(const EC_GROUP *group, EC_POINT *r,
457 const BIGNUM *scalar, size_t num,
458 const EC_POINT *points[], const BIGNUM *scalars[],
460 int ec_GFp_nistp224_points_mul(const EC_GROUP *group, EC_POINT *r,
461 const BIGNUM *scalar, size_t num,
462 const EC_POINT *points[],
463 const BIGNUM *scalars[], BN_CTX *ctx);
464 int ec_GFp_nistp224_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
465 int ec_GFp_nistp224_have_precompute_mult(const EC_GROUP *group);
467 /* method functions in ecp_nistp256.c */
468 int ec_GFp_nistp256_group_init(EC_GROUP *group);
469 int ec_GFp_nistp256_group_set_curve(EC_GROUP *group, const BIGNUM *p,
470 const BIGNUM *a, const BIGNUM *n,
472 int ec_GFp_nistp256_point_get_affine_coordinates(const EC_GROUP *group,
473 const EC_POINT *point,
474 BIGNUM *x, BIGNUM *y,
476 int ec_GFp_nistp256_mul(const EC_GROUP *group, EC_POINT *r,
477 const BIGNUM *scalar, size_t num,
478 const EC_POINT *points[], const BIGNUM *scalars[],
480 int ec_GFp_nistp256_points_mul(const EC_GROUP *group, EC_POINT *r,
481 const BIGNUM *scalar, size_t num,
482 const EC_POINT *points[],
483 const BIGNUM *scalars[], BN_CTX *ctx);
484 int ec_GFp_nistp256_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
485 int ec_GFp_nistp256_have_precompute_mult(const EC_GROUP *group);
487 /* method functions in ecp_nistp521.c */
488 int ec_GFp_nistp521_group_init(EC_GROUP *group);
489 int ec_GFp_nistp521_group_set_curve(EC_GROUP *group, const BIGNUM *p,
490 const BIGNUM *a, const BIGNUM *n,
492 int ec_GFp_nistp521_point_get_affine_coordinates(const EC_GROUP *group,
493 const EC_POINT *point,
494 BIGNUM *x, BIGNUM *y,
496 int ec_GFp_nistp521_mul(const EC_GROUP *group, EC_POINT *r,
497 const BIGNUM *scalar, size_t num,
498 const EC_POINT *points[], const BIGNUM *scalars[],
500 int ec_GFp_nistp521_points_mul(const EC_GROUP *group, EC_POINT *r,
501 const BIGNUM *scalar, size_t num,
502 const EC_POINT *points[],
503 const BIGNUM *scalars[], BN_CTX *ctx);
504 int ec_GFp_nistp521_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
505 int ec_GFp_nistp521_have_precompute_mult(const EC_GROUP *group);
507 /* utility functions in ecp_nistputil.c */
508 void ec_GFp_nistp_points_make_affine_internal(size_t num, void *point_array,
511 void (*felem_one) (void *out),
512 int (*felem_is_zero) (const void
514 void (*felem_assign) (void *out,
517 void (*felem_square) (void *out,
520 void (*felem_mul) (void *out,
525 void (*felem_inv) (void *out,
528 void (*felem_contract) (void
533 void ec_GFp_nistp_recode_scalar_bits(unsigned char *sign,
534 unsigned char *digit, unsigned char in);
536 int ec_group_simple_order_bits(const EC_GROUP *group);
538 #ifdef ECP_NISTZ256_ASM
539 /** Returns GFp methods using montgomery multiplication, with x86-64 optimized
540 * P256. See http://eprint.iacr.org/2013/816.
541 * \return EC_METHOD object
543 const EC_METHOD *EC_GFp_nistz256_method(void);
546 size_t ec_key_simple_priv2oct(const EC_KEY *eckey,
547 unsigned char *buf, size_t len);
548 int ec_key_simple_oct2priv(EC_KEY *eckey, const unsigned char *buf, size_t len);
549 int ec_key_simple_generate_key(EC_KEY *eckey);
550 int ec_key_simple_generate_public_key(EC_KEY *eckey);
551 int ec_key_simple_check_key(const EC_KEY *eckey);
553 /* EC_METHOD definitions */
555 struct ec_key_method_st {
558 int (*init)(EC_KEY *key);
559 void (*finish)(EC_KEY *key);
560 int (*copy)(EC_KEY *dest, const EC_KEY *src);
561 int (*set_group)(EC_KEY *key, const EC_GROUP *grp);
562 int (*set_private)(EC_KEY *key, const BIGNUM *priv_key);
563 int (*set_public)(EC_KEY *key, const EC_POINT *pub_key);
564 int (*keygen)(EC_KEY *key);
565 int (*compute_key)(unsigned char **pout, size_t *poutlen,
566 const EC_POINT *pub_key, const EC_KEY *ecdh);
567 int (*sign)(int type, const unsigned char *dgst, int dlen, unsigned char
568 *sig, unsigned int *siglen, const BIGNUM *kinv,
569 const BIGNUM *r, EC_KEY *eckey);
570 int (*sign_setup)(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
572 ECDSA_SIG *(*sign_sig)(const unsigned char *dgst, int dgst_len,
573 const BIGNUM *in_kinv, const BIGNUM *in_r,
576 int (*verify)(int type, const unsigned char *dgst, int dgst_len,
577 const unsigned char *sigbuf, int sig_len, EC_KEY *eckey);
578 int (*verify_sig)(const unsigned char *dgst, int dgst_len,
579 const ECDSA_SIG *sig, EC_KEY *eckey);
582 #define EC_KEY_METHOD_DYNAMIC 1
584 int ossl_ec_key_gen(EC_KEY *eckey);
585 int ossl_ecdh_compute_key(unsigned char **pout, size_t *poutlen,
586 const EC_POINT *pub_key, const EC_KEY *ecdh);
587 int ecdh_simple_compute_key(unsigned char **pout, size_t *poutlen,
588 const EC_POINT *pub_key, const EC_KEY *ecdh);
590 struct ECDSA_SIG_st {
595 int ossl_ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
597 int ossl_ecdsa_sign(int type, const unsigned char *dgst, int dlen,
598 unsigned char *sig, unsigned int *siglen,
599 const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey);
600 ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
601 const BIGNUM *in_kinv, const BIGNUM *in_r,
603 int ossl_ecdsa_verify(int type, const unsigned char *dgst, int dgst_len,
604 const unsigned char *sigbuf, int sig_len, EC_KEY *eckey);
605 int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len,
606 const ECDSA_SIG *sig, EC_KEY *eckey);
608 int ED25519_sign(uint8_t *out_sig, const uint8_t *message, size_t message_len,
609 const uint8_t public_key[32], const uint8_t private_key[32]);
610 int ED25519_verify(const uint8_t *message, size_t message_len,
611 const uint8_t signature[64], const uint8_t public_key[32]);
612 void ED25519_public_from_private(uint8_t out_public_key[32],
613 const uint8_t private_key[32]);
615 int X25519(uint8_t out_shared_key[32], const uint8_t private_key[32],
616 const uint8_t peer_public_value[32]);
617 void X25519_public_from_private(uint8_t out_public_value[32],
618 const uint8_t private_key[32]);
620 int EC_GROUP_do_inverse_ord(const EC_GROUP *group, BIGNUM *res,
621 BIGNUM *x, BN_CTX *ctx);