2 * Copyright 2001-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 /* ====================================================================
11 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
13 * Portions of the attached software ("Contribution") are developed by
14 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
16 * The Contribution is licensed pursuant to the OpenSSL open source
17 * license provided above.
19 * The elliptic curve binary polynomial software is originally written by
20 * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
26 #include <openssl/obj_mac.h>
27 #include <openssl/ec.h>
28 #include <openssl/bn.h>
32 #if defined(__SUNPRO_C)
33 # if __SUNPRO_C >= 0x520
34 # pragma error_messages (off,E_ARRAY_OF_INCOMPLETE_NONAME,E_ARRAY_OF_INCOMPLETE)
38 /* Use default functions for poin2oct, oct2point and compressed coordinates */
39 #define EC_FLAGS_DEFAULT_OCT 0x1
41 /* Use custom formats for EC_GROUP, EC_POINT and EC_KEY */
42 #define EC_FLAGS_CUSTOM_CURVE 0x2
44 /* Curve does not support signing operations */
45 #define EC_FLAGS_NO_SIGN 0x4
48 * Structure details are not part of the exported interface, so all this may
49 * change in future versions.
53 /* Various method flags */
55 /* used by EC_METHOD_get_field_type: */
56 int field_type; /* a NID */
58 * used by EC_GROUP_new, EC_GROUP_free, EC_GROUP_clear_free,
61 int (*group_init) (EC_GROUP *);
62 void (*group_finish) (EC_GROUP *);
63 void (*group_clear_finish) (EC_GROUP *);
64 int (*group_copy) (EC_GROUP *, const EC_GROUP *);
65 /* used by EC_GROUP_set_curve_GFp, EC_GROUP_get_curve_GFp, */
66 /* EC_GROUP_set_curve_GF2m, and EC_GROUP_get_curve_GF2m: */
67 int (*group_set_curve) (EC_GROUP *, const BIGNUM *p, const BIGNUM *a,
68 const BIGNUM *b, BN_CTX *);
69 int (*group_get_curve) (const EC_GROUP *, BIGNUM *p, BIGNUM *a, BIGNUM *b,
71 /* used by EC_GROUP_get_degree: */
72 int (*group_get_degree) (const EC_GROUP *);
73 int (*group_order_bits) (const EC_GROUP *);
74 /* used by EC_GROUP_check: */
75 int (*group_check_discriminant) (const EC_GROUP *, BN_CTX *);
77 * used by EC_POINT_new, EC_POINT_free, EC_POINT_clear_free,
80 int (*point_init) (EC_POINT *);
81 void (*point_finish) (EC_POINT *);
82 void (*point_clear_finish) (EC_POINT *);
83 int (*point_copy) (EC_POINT *, const EC_POINT *);
85 * used by EC_POINT_set_to_infinity,
86 * EC_POINT_set_Jprojective_coordinates_GFp,
87 * EC_POINT_get_Jprojective_coordinates_GFp,
88 * EC_POINT_set_affine_coordinates_GFp, ..._GF2m,
89 * EC_POINT_get_affine_coordinates_GFp, ..._GF2m,
90 * EC_POINT_set_compressed_coordinates_GFp, ..._GF2m:
92 int (*point_set_to_infinity) (const EC_GROUP *, EC_POINT *);
93 int (*point_set_Jprojective_coordinates_GFp) (const EC_GROUP *,
94 EC_POINT *, const BIGNUM *x,
96 const BIGNUM *z, BN_CTX *);
97 int (*point_get_Jprojective_coordinates_GFp) (const EC_GROUP *,
98 const EC_POINT *, BIGNUM *x,
101 int (*point_set_affine_coordinates) (const EC_GROUP *, EC_POINT *,
102 const BIGNUM *x, const BIGNUM *y,
104 int (*point_get_affine_coordinates) (const EC_GROUP *, const EC_POINT *,
105 BIGNUM *x, BIGNUM *y, BN_CTX *);
106 int (*point_set_compressed_coordinates) (const EC_GROUP *, EC_POINT *,
107 const BIGNUM *x, int y_bit,
109 /* used by EC_POINT_point2oct, EC_POINT_oct2point: */
110 size_t (*point2oct) (const EC_GROUP *, const EC_POINT *,
111 point_conversion_form_t form, unsigned char *buf,
112 size_t len, BN_CTX *);
113 int (*oct2point) (const EC_GROUP *, EC_POINT *, const unsigned char *buf,
114 size_t len, BN_CTX *);
115 /* used by EC_POINT_add, EC_POINT_dbl, ECP_POINT_invert: */
116 int (*add) (const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
117 const EC_POINT *b, BN_CTX *);
118 int (*dbl) (const EC_GROUP *, EC_POINT *r, const EC_POINT *a, BN_CTX *);
119 int (*invert) (const EC_GROUP *, EC_POINT *, BN_CTX *);
121 * used by EC_POINT_is_at_infinity, EC_POINT_is_on_curve, EC_POINT_cmp:
123 int (*is_at_infinity) (const EC_GROUP *, const EC_POINT *);
124 int (*is_on_curve) (const EC_GROUP *, const EC_POINT *, BN_CTX *);
125 int (*point_cmp) (const EC_GROUP *, const EC_POINT *a, const EC_POINT *b,
127 /* used by EC_POINT_make_affine, EC_POINTs_make_affine: */
128 int (*make_affine) (const EC_GROUP *, EC_POINT *, BN_CTX *);
129 int (*points_make_affine) (const EC_GROUP *, size_t num, EC_POINT *[],
132 * used by EC_POINTs_mul, EC_POINT_mul, EC_POINT_precompute_mult,
133 * EC_POINT_have_precompute_mult (default implementations are used if the
134 * 'mul' pointer is 0):
136 int (*mul) (const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
137 size_t num, const EC_POINT *points[], const BIGNUM *scalars[],
139 int (*precompute_mult) (EC_GROUP *group, BN_CTX *);
140 int (*have_precompute_mult) (const EC_GROUP *group);
141 /* internal functions */
143 * 'field_mul', 'field_sqr', and 'field_div' can be used by 'add' and
144 * 'dbl' so that the same implementations of point operations can be used
145 * with different optimized implementations of expensive field
148 int (*field_mul) (const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
149 const BIGNUM *b, BN_CTX *);
150 int (*field_sqr) (const EC_GROUP *, BIGNUM *r, const BIGNUM *a, BN_CTX *);
151 int (*field_div) (const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
152 const BIGNUM *b, BN_CTX *);
153 /* e.g. to Montgomery */
154 int (*field_encode) (const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
156 /* e.g. from Montgomery */
157 int (*field_decode) (const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
159 int (*field_set_to_one) (const EC_GROUP *, BIGNUM *r, BN_CTX *);
160 /* private key operations */
161 size_t (*priv2oct)(const EC_KEY *eckey, unsigned char *buf, size_t len);
162 int (*oct2priv)(EC_KEY *eckey, const unsigned char *buf, size_t len);
163 int (*set_private)(EC_KEY *eckey, const BIGNUM *priv_key);
164 int (*keygen)(EC_KEY *eckey);
165 int (*keycheck)(const EC_KEY *eckey);
166 int (*keygenpub)(EC_KEY *eckey);
167 int (*keycopy)(EC_KEY *dst, const EC_KEY *src);
168 void (*keyfinish)(EC_KEY *eckey);
169 /* custom ECDH operation */
170 int (*ecdh_compute_key)(unsigned char **pout, size_t *poutlen,
171 const EC_POINT *pub_key, const EC_KEY *ecdh);
172 int (*blind_coordinates)(const EC_GROUP *group, EC_POINT *p, BN_CTX *ctx);
176 * Types and functions to manipulate pre-computed values.
178 typedef struct nistp224_pre_comp_st NISTP224_PRE_COMP;
179 typedef struct nistp256_pre_comp_st NISTP256_PRE_COMP;
180 typedef struct nistp521_pre_comp_st NISTP521_PRE_COMP;
181 typedef struct nistz256_pre_comp_st NISTZ256_PRE_COMP;
182 typedef struct ec_pre_comp_st EC_PRE_COMP;
185 const EC_METHOD *meth;
186 EC_POINT *generator; /* optional */
187 BIGNUM *order, *cofactor;
188 int curve_name; /* optional NID for named curve */
189 int asn1_flag; /* flag to control the asn1 encoding */
190 point_conversion_form_t asn1_form;
191 unsigned char *seed; /* optional seed for parameters (appears in
195 * The following members are handled by the method functions, even if
196 * they appear generic
199 * Field specification. For curves over GF(p), this is the modulus; for
200 * curves over GF(2^m), this is the irreducible polynomial defining the
205 * Field specification for curves over GF(2^m). The irreducible f(t) is
206 * then of the form: t^poly[0] + t^poly[1] + ... + t^poly[k] where m =
207 * poly[0] > poly[1] > ... > poly[k] = 0. The array is terminated with
208 * poly[k+1]=-1. All elliptic curve irreducibles have at most 5 non-zero
213 * Curve coefficients. (Here the assumption is that BIGNUMs can be used
214 * or abused for all kinds of fields, not just GF(p).) For characteristic
215 * > 3, the curve is defined by a Weierstrass equation of the form y^2 =
216 * x^3 + a*x + b. For characteristic 2, the curve is defined by an
217 * equation of the form y^2 + x*y = x^3 + a*x^2 + b.
220 /* enable optimized point arithmetics for special case */
222 /* method-specific (e.g., Montgomery structure) */
224 /* method-specific */
226 /* method-specific */
227 int (*field_mod_func) (BIGNUM *, const BIGNUM *, const BIGNUM *,
229 /* data for ECDSA inverse */
230 BN_MONT_CTX *mont_data;
233 * Precomputed values for speed. The PCT_xxx names match the
234 * pre_comp.xxx union names; see the SETPRECOMP and HAVEPRECOMP
239 PCT_nistp224, PCT_nistp256, PCT_nistp521, PCT_nistz256,
243 NISTP224_PRE_COMP *nistp224;
244 NISTP256_PRE_COMP *nistp256;
245 NISTP521_PRE_COMP *nistp521;
246 NISTZ256_PRE_COMP *nistz256;
251 #define SETPRECOMP(g, type, pre) \
252 g->pre_comp_type = PCT_##type, g->pre_comp.type = pre
253 #define HAVEPRECOMP(g, type) \
254 g->pre_comp_type == PCT_##type && g->pre_comp.type != NULL
257 const EC_KEY_METHOD *meth;
263 unsigned int enc_flag;
264 point_conversion_form_t conv_form;
267 CRYPTO_EX_DATA ex_data;
272 const EC_METHOD *meth;
273 /* NID for the curve if known */
276 * All members except 'meth' are handled by the method functions, even if
277 * they appear generic
281 BIGNUM *Z; /* Jacobian projective coordinates: * (X, Y,
282 * Z) represents (X/Z^2, Y/Z^3) if Z != 0 */
283 int Z_is_one; /* enable optimized point arithmetics for
288 static ossl_inline int ec_point_is_compat(const EC_POINT *point,
289 const EC_GROUP *group)
291 if (group->meth != point->meth
292 || (group->curve_name != 0
293 && point->curve_name != 0
294 && group->curve_name != point->curve_name))
301 NISTP224_PRE_COMP *EC_nistp224_pre_comp_dup(NISTP224_PRE_COMP *);
302 NISTP256_PRE_COMP *EC_nistp256_pre_comp_dup(NISTP256_PRE_COMP *);
303 NISTP521_PRE_COMP *EC_nistp521_pre_comp_dup(NISTP521_PRE_COMP *);
304 NISTZ256_PRE_COMP *EC_nistz256_pre_comp_dup(NISTZ256_PRE_COMP *);
305 NISTP256_PRE_COMP *EC_nistp256_pre_comp_dup(NISTP256_PRE_COMP *);
306 EC_PRE_COMP *EC_ec_pre_comp_dup(EC_PRE_COMP *);
308 void EC_pre_comp_free(EC_GROUP *group);
309 void EC_nistp224_pre_comp_free(NISTP224_PRE_COMP *);
310 void EC_nistp256_pre_comp_free(NISTP256_PRE_COMP *);
311 void EC_nistp521_pre_comp_free(NISTP521_PRE_COMP *);
312 void EC_nistz256_pre_comp_free(NISTZ256_PRE_COMP *);
313 void EC_ec_pre_comp_free(EC_PRE_COMP *);
316 * method functions in ec_mult.c (ec_lib.c uses these as defaults if
317 * group->method->mul is 0)
319 int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
320 size_t num, const EC_POINT *points[], const BIGNUM *scalars[],
322 int ec_wNAF_precompute_mult(EC_GROUP *group, BN_CTX *);
323 int ec_wNAF_have_precompute_mult(const EC_GROUP *group);
325 /* method functions in ecp_smpl.c */
326 int ec_GFp_simple_group_init(EC_GROUP *);
327 void ec_GFp_simple_group_finish(EC_GROUP *);
328 void ec_GFp_simple_group_clear_finish(EC_GROUP *);
329 int ec_GFp_simple_group_copy(EC_GROUP *, const EC_GROUP *);
330 int ec_GFp_simple_group_set_curve(EC_GROUP *, const BIGNUM *p,
331 const BIGNUM *a, const BIGNUM *b, BN_CTX *);
332 int ec_GFp_simple_group_get_curve(const EC_GROUP *, BIGNUM *p, BIGNUM *a,
333 BIGNUM *b, BN_CTX *);
334 int ec_GFp_simple_group_get_degree(const EC_GROUP *);
335 int ec_GFp_simple_group_check_discriminant(const EC_GROUP *, BN_CTX *);
336 int ec_GFp_simple_point_init(EC_POINT *);
337 void ec_GFp_simple_point_finish(EC_POINT *);
338 void ec_GFp_simple_point_clear_finish(EC_POINT *);
339 int ec_GFp_simple_point_copy(EC_POINT *, const EC_POINT *);
340 int ec_GFp_simple_point_set_to_infinity(const EC_GROUP *, EC_POINT *);
341 int ec_GFp_simple_set_Jprojective_coordinates_GFp(const EC_GROUP *,
342 EC_POINT *, const BIGNUM *x,
344 const BIGNUM *z, BN_CTX *);
345 int ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP *,
346 const EC_POINT *, BIGNUM *x,
347 BIGNUM *y, BIGNUM *z,
349 int ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP *, EC_POINT *,
351 const BIGNUM *y, BN_CTX *);
352 int ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP *,
353 const EC_POINT *, BIGNUM *x,
354 BIGNUM *y, BN_CTX *);
355 int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *, EC_POINT *,
356 const BIGNUM *x, int y_bit,
358 size_t ec_GFp_simple_point2oct(const EC_GROUP *, const EC_POINT *,
359 point_conversion_form_t form,
360 unsigned char *buf, size_t len, BN_CTX *);
361 int ec_GFp_simple_oct2point(const EC_GROUP *, EC_POINT *,
362 const unsigned char *buf, size_t len, BN_CTX *);
363 int ec_GFp_simple_add(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
364 const EC_POINT *b, BN_CTX *);
365 int ec_GFp_simple_dbl(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
367 int ec_GFp_simple_invert(const EC_GROUP *, EC_POINT *, BN_CTX *);
368 int ec_GFp_simple_is_at_infinity(const EC_GROUP *, const EC_POINT *);
369 int ec_GFp_simple_is_on_curve(const EC_GROUP *, const EC_POINT *, BN_CTX *);
370 int ec_GFp_simple_cmp(const EC_GROUP *, const EC_POINT *a, const EC_POINT *b,
372 int ec_GFp_simple_make_affine(const EC_GROUP *, EC_POINT *, BN_CTX *);
373 int ec_GFp_simple_points_make_affine(const EC_GROUP *, size_t num,
374 EC_POINT *[], BN_CTX *);
375 int ec_GFp_simple_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
376 const BIGNUM *b, BN_CTX *);
377 int ec_GFp_simple_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
379 int ec_GFp_simple_blind_coordinates(const EC_GROUP *group, EC_POINT *p,
382 /* method functions in ecp_mont.c */
383 int ec_GFp_mont_group_init(EC_GROUP *);
384 int ec_GFp_mont_group_set_curve(EC_GROUP *, const BIGNUM *p, const BIGNUM *a,
385 const BIGNUM *b, BN_CTX *);
386 void ec_GFp_mont_group_finish(EC_GROUP *);
387 void ec_GFp_mont_group_clear_finish(EC_GROUP *);
388 int ec_GFp_mont_group_copy(EC_GROUP *, const EC_GROUP *);
389 int ec_GFp_mont_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
390 const BIGNUM *b, BN_CTX *);
391 int ec_GFp_mont_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
393 int ec_GFp_mont_field_encode(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
395 int ec_GFp_mont_field_decode(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
397 int ec_GFp_mont_field_set_to_one(const EC_GROUP *, BIGNUM *r, BN_CTX *);
399 /* method functions in ecp_nist.c */
400 int ec_GFp_nist_group_copy(EC_GROUP *dest, const EC_GROUP *src);
401 int ec_GFp_nist_group_set_curve(EC_GROUP *, const BIGNUM *p, const BIGNUM *a,
402 const BIGNUM *b, BN_CTX *);
403 int ec_GFp_nist_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
404 const BIGNUM *b, BN_CTX *);
405 int ec_GFp_nist_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
408 /* method functions in ec2_smpl.c */
409 int ec_GF2m_simple_group_init(EC_GROUP *);
410 void ec_GF2m_simple_group_finish(EC_GROUP *);
411 void ec_GF2m_simple_group_clear_finish(EC_GROUP *);
412 int ec_GF2m_simple_group_copy(EC_GROUP *, const EC_GROUP *);
413 int ec_GF2m_simple_group_set_curve(EC_GROUP *, const BIGNUM *p,
414 const BIGNUM *a, const BIGNUM *b,
416 int ec_GF2m_simple_group_get_curve(const EC_GROUP *, BIGNUM *p, BIGNUM *a,
417 BIGNUM *b, BN_CTX *);
418 int ec_GF2m_simple_group_get_degree(const EC_GROUP *);
419 int ec_GF2m_simple_group_check_discriminant(const EC_GROUP *, BN_CTX *);
420 int ec_GF2m_simple_point_init(EC_POINT *);
421 void ec_GF2m_simple_point_finish(EC_POINT *);
422 void ec_GF2m_simple_point_clear_finish(EC_POINT *);
423 int ec_GF2m_simple_point_copy(EC_POINT *, const EC_POINT *);
424 int ec_GF2m_simple_point_set_to_infinity(const EC_GROUP *, EC_POINT *);
425 int ec_GF2m_simple_point_set_affine_coordinates(const EC_GROUP *, EC_POINT *,
427 const BIGNUM *y, BN_CTX *);
428 int ec_GF2m_simple_point_get_affine_coordinates(const EC_GROUP *,
429 const EC_POINT *, BIGNUM *x,
430 BIGNUM *y, BN_CTX *);
431 int ec_GF2m_simple_set_compressed_coordinates(const EC_GROUP *, EC_POINT *,
432 const BIGNUM *x, int y_bit,
434 size_t ec_GF2m_simple_point2oct(const EC_GROUP *, const EC_POINT *,
435 point_conversion_form_t form,
436 unsigned char *buf, size_t len, BN_CTX *);
437 int ec_GF2m_simple_oct2point(const EC_GROUP *, EC_POINT *,
438 const unsigned char *buf, size_t len, BN_CTX *);
439 int ec_GF2m_simple_add(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
440 const EC_POINT *b, BN_CTX *);
441 int ec_GF2m_simple_dbl(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
443 int ec_GF2m_simple_invert(const EC_GROUP *, EC_POINT *, BN_CTX *);
444 int ec_GF2m_simple_is_at_infinity(const EC_GROUP *, const EC_POINT *);
445 int ec_GF2m_simple_is_on_curve(const EC_GROUP *, const EC_POINT *, BN_CTX *);
446 int ec_GF2m_simple_cmp(const EC_GROUP *, const EC_POINT *a, const EC_POINT *b,
448 int ec_GF2m_simple_make_affine(const EC_GROUP *, EC_POINT *, BN_CTX *);
449 int ec_GF2m_simple_points_make_affine(const EC_GROUP *, size_t num,
450 EC_POINT *[], BN_CTX *);
451 int ec_GF2m_simple_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
452 const BIGNUM *b, BN_CTX *);
453 int ec_GF2m_simple_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
455 int ec_GF2m_simple_field_div(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
456 const BIGNUM *b, BN_CTX *);
458 /* method functions in ec2_mult.c */
459 int ec_GF2m_simple_mul(const EC_GROUP *group, EC_POINT *r,
460 const BIGNUM *scalar, size_t num,
461 const EC_POINT *points[], const BIGNUM *scalars[],
463 int ec_GF2m_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
464 int ec_GF2m_have_precompute_mult(const EC_GROUP *group);
466 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
467 /* method functions in ecp_nistp224.c */
468 int ec_GFp_nistp224_group_init(EC_GROUP *group);
469 int ec_GFp_nistp224_group_set_curve(EC_GROUP *group, const BIGNUM *p,
470 const BIGNUM *a, const BIGNUM *n,
472 int ec_GFp_nistp224_point_get_affine_coordinates(const EC_GROUP *group,
473 const EC_POINT *point,
474 BIGNUM *x, BIGNUM *y,
476 int ec_GFp_nistp224_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_nistp224_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_nistp224_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
485 int ec_GFp_nistp224_have_precompute_mult(const EC_GROUP *group);
487 /* method functions in ecp_nistp256.c */
488 int ec_GFp_nistp256_group_init(EC_GROUP *group);
489 int ec_GFp_nistp256_group_set_curve(EC_GROUP *group, const BIGNUM *p,
490 const BIGNUM *a, const BIGNUM *n,
492 int ec_GFp_nistp256_point_get_affine_coordinates(const EC_GROUP *group,
493 const EC_POINT *point,
494 BIGNUM *x, BIGNUM *y,
496 int ec_GFp_nistp256_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_nistp256_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_nistp256_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
505 int ec_GFp_nistp256_have_precompute_mult(const EC_GROUP *group);
507 /* method functions in ecp_nistp521.c */
508 int ec_GFp_nistp521_group_init(EC_GROUP *group);
509 int ec_GFp_nistp521_group_set_curve(EC_GROUP *group, const BIGNUM *p,
510 const BIGNUM *a, const BIGNUM *n,
512 int ec_GFp_nistp521_point_get_affine_coordinates(const EC_GROUP *group,
513 const EC_POINT *point,
514 BIGNUM *x, BIGNUM *y,
516 int ec_GFp_nistp521_mul(const EC_GROUP *group, EC_POINT *r,
517 const BIGNUM *scalar, size_t num,
518 const EC_POINT *points[], const BIGNUM *scalars[],
520 int ec_GFp_nistp521_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 ec_GFp_nistp521_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
525 int ec_GFp_nistp521_have_precompute_mult(const EC_GROUP *group);
527 /* utility functions in ecp_nistputil.c */
528 void ec_GFp_nistp_points_make_affine_internal(size_t num, void *point_array,
531 void (*felem_one) (void *out),
532 int (*felem_is_zero) (const void
534 void (*felem_assign) (void *out,
537 void (*felem_square) (void *out,
540 void (*felem_mul) (void *out,
545 void (*felem_inv) (void *out,
548 void (*felem_contract) (void
553 void ec_GFp_nistp_recode_scalar_bits(unsigned char *sign,
554 unsigned char *digit, unsigned char in);
556 int ec_precompute_mont_data(EC_GROUP *);
557 int ec_group_simple_order_bits(const EC_GROUP *group);
559 #ifdef ECP_NISTZ256_ASM
560 /** Returns GFp methods using montgomery multiplication, with x86-64 optimized
561 * P256. See http://eprint.iacr.org/2013/816.
562 * \return EC_METHOD object
564 const EC_METHOD *EC_GFp_nistz256_method(void);
567 size_t ec_key_simple_priv2oct(const EC_KEY *eckey,
568 unsigned char *buf, size_t len);
569 int ec_key_simple_oct2priv(EC_KEY *eckey, const unsigned char *buf, size_t len);
570 int ec_key_simple_generate_key(EC_KEY *eckey);
571 int ec_key_simple_generate_public_key(EC_KEY *eckey);
572 int ec_key_simple_check_key(const EC_KEY *eckey);
574 /* EC_METHOD definitions */
576 struct ec_key_method_st {
579 int (*init)(EC_KEY *key);
580 void (*finish)(EC_KEY *key);
581 int (*copy)(EC_KEY *dest, const EC_KEY *src);
582 int (*set_group)(EC_KEY *key, const EC_GROUP *grp);
583 int (*set_private)(EC_KEY *key, const BIGNUM *priv_key);
584 int (*set_public)(EC_KEY *key, const EC_POINT *pub_key);
585 int (*keygen)(EC_KEY *key);
586 int (*compute_key)(unsigned char **pout, size_t *poutlen,
587 const EC_POINT *pub_key, const EC_KEY *ecdh);
588 int (*sign)(int type, const unsigned char *dgst, int dlen, unsigned char
589 *sig, unsigned int *siglen, const BIGNUM *kinv,
590 const BIGNUM *r, EC_KEY *eckey);
591 int (*sign_setup)(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
593 ECDSA_SIG *(*sign_sig)(const unsigned char *dgst, int dgst_len,
594 const BIGNUM *in_kinv, const BIGNUM *in_r,
597 int (*verify)(int type, const unsigned char *dgst, int dgst_len,
598 const unsigned char *sigbuf, int sig_len, EC_KEY *eckey);
599 int (*verify_sig)(const unsigned char *dgst, int dgst_len,
600 const ECDSA_SIG *sig, EC_KEY *eckey);
603 #define EC_KEY_METHOD_DYNAMIC 1
605 int ossl_ec_key_gen(EC_KEY *eckey);
606 int ossl_ecdh_compute_key(unsigned char **pout, size_t *poutlen,
607 const EC_POINT *pub_key, const EC_KEY *ecdh);
608 int ecdh_simple_compute_key(unsigned char **pout, size_t *poutlen,
609 const EC_POINT *pub_key, const EC_KEY *ecdh);
611 struct ECDSA_SIG_st {
616 int ossl_ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
618 int ossl_ecdsa_sign(int type, const unsigned char *dgst, int dlen,
619 unsigned char *sig, unsigned int *siglen,
620 const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey);
621 ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
622 const BIGNUM *in_kinv, const BIGNUM *in_r,
624 int ossl_ecdsa_verify(int type, const unsigned char *dgst, int dgst_len,
625 const unsigned char *sigbuf, int sig_len, EC_KEY *eckey);
626 int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len,
627 const ECDSA_SIG *sig, EC_KEY *eckey);
629 int X25519(uint8_t out_shared_key[32], const uint8_t private_key[32],
630 const uint8_t peer_public_value[32]);
631 void X25519_public_from_private(uint8_t out_public_value[32],
632 const uint8_t private_key[32]);
634 int ec_point_blind_coordinates(const EC_GROUP *group, EC_POINT *p, BN_CTX *ctx);