d9383f10a7a6553b2f330e6da6fe0705e26a2bc3
[openssl.git] / crypto / ec / ec_local.h
1 /*
2  * Copyright 2001-2020 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
11 #include <stdlib.h>
12
13 #include <openssl/obj_mac.h>
14 #include <openssl/ec.h>
15 #include <openssl/bn.h>
16 #include "internal/refcount.h"
17 #include "crypto/ec.h"
18
19 #if defined(__SUNPRO_C)
20 # if __SUNPRO_C >= 0x520
21 #  pragma error_messages (off,E_ARRAY_OF_INCOMPLETE_NONAME,E_ARRAY_OF_INCOMPLETE)
22 # endif
23 #endif
24
25 /* Use default functions for poin2oct, oct2point and compressed coordinates */
26 #define EC_FLAGS_DEFAULT_OCT    0x1
27
28 /* Use custom formats for EC_GROUP, EC_POINT and EC_KEY */
29 #define EC_FLAGS_CUSTOM_CURVE   0x2
30
31 /* Curve does not support signing operations */
32 #define EC_FLAGS_NO_SIGN        0x4
33
34 /*
35  * Structure details are not part of the exported interface, so all this may
36  * change in future versions.
37  */
38
39 struct ec_method_st {
40     /* Various method flags */
41     int flags;
42     /* used by EC_METHOD_get_field_type: */
43     int field_type;             /* a NID */
44     /*
45      * used by EC_GROUP_new, EC_GROUP_free, EC_GROUP_clear_free,
46      * EC_GROUP_copy:
47      */
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, EC_GROUP_get_curve: */
53     int (*group_set_curve) (EC_GROUP *, const BIGNUM *p, const BIGNUM *a,
54                             const BIGNUM *b, BN_CTX *);
55     int (*group_get_curve) (const EC_GROUP *, BIGNUM *p, BIGNUM *a, BIGNUM *b,
56                             BN_CTX *);
57     /* used by EC_GROUP_get_degree: */
58     int (*group_get_degree) (const EC_GROUP *);
59     int (*group_order_bits) (const EC_GROUP *);
60     /* used by EC_GROUP_check: */
61     int (*group_check_discriminant) (const EC_GROUP *, BN_CTX *);
62     /*
63      * used by EC_POINT_new, EC_POINT_free, EC_POINT_clear_free,
64      * EC_POINT_copy:
65      */
66     int (*point_init) (EC_POINT *);
67     void (*point_finish) (EC_POINT *);
68     void (*point_clear_finish) (EC_POINT *);
69     int (*point_copy) (EC_POINT *, const EC_POINT *);
70     /*-
71      * used by EC_POINT_set_to_infinity,
72      * EC_POINT_set_Jprojective_coordinates_GFp,
73      * EC_POINT_get_Jprojective_coordinates_GFp,
74      * EC_POINT_set_affine_coordinates,
75      * EC_POINT_get_affine_coordinates,
76      * EC_POINT_set_compressed_coordinates:
77      */
78     int (*point_set_to_infinity) (const EC_GROUP *, EC_POINT *);
79     int (*point_set_affine_coordinates) (const EC_GROUP *, EC_POINT *,
80                                          const BIGNUM *x, const BIGNUM *y,
81                                          BN_CTX *);
82     int (*point_get_affine_coordinates) (const EC_GROUP *, const EC_POINT *,
83                                          BIGNUM *x, BIGNUM *y, BN_CTX *);
84     int (*point_set_compressed_coordinates) (const EC_GROUP *, EC_POINT *,
85                                              const BIGNUM *x, int y_bit,
86                                              BN_CTX *);
87     /* used by EC_POINT_point2oct, EC_POINT_oct2point: */
88     size_t (*point2oct) (const EC_GROUP *, const EC_POINT *,
89                          point_conversion_form_t form, unsigned char *buf,
90                          size_t len, BN_CTX *);
91     int (*oct2point) (const EC_GROUP *, EC_POINT *, const unsigned char *buf,
92                       size_t len, BN_CTX *);
93     /* used by EC_POINT_add, EC_POINT_dbl, ECP_POINT_invert: */
94     int (*add) (const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
95                 const EC_POINT *b, BN_CTX *);
96     int (*dbl) (const EC_GROUP *, EC_POINT *r, const EC_POINT *a, BN_CTX *);
97     int (*invert) (const EC_GROUP *, EC_POINT *, BN_CTX *);
98     /*
99      * used by EC_POINT_is_at_infinity, EC_POINT_is_on_curve, EC_POINT_cmp:
100      */
101     int (*is_at_infinity) (const EC_GROUP *, const EC_POINT *);
102     int (*is_on_curve) (const EC_GROUP *, const EC_POINT *, BN_CTX *);
103     int (*point_cmp) (const EC_GROUP *, const EC_POINT *a, const EC_POINT *b,
104                       BN_CTX *);
105     /* used by EC_POINT_make_affine, EC_POINTs_make_affine: */
106     int (*make_affine) (const EC_GROUP *, EC_POINT *, BN_CTX *);
107     int (*points_make_affine) (const EC_GROUP *, size_t num, EC_POINT *[],
108                                BN_CTX *);
109     /*
110      * used by EC_POINTs_mul, EC_POINT_mul, EC_POINT_precompute_mult,
111      * EC_POINT_have_precompute_mult (default implementations are used if the
112      * 'mul' pointer is 0):
113      */
114     /*-
115      * mul() calculates the value
116      *
117      *   r := generator * scalar
118      *        + points[0] * scalars[0]
119      *        + ...
120      *        + points[num-1] * scalars[num-1].
121      *
122      * For a fixed point multiplication (scalar != NULL, num == 0)
123      * or a variable point multiplication (scalar == NULL, num == 1),
124      * mul() must use a constant time algorithm: in both cases callers
125      * should provide an input scalar (either scalar or scalars[0])
126      * in the range [0, ec_group_order); for robustness, implementers
127      * should handle the case when the scalar has not been reduced, but
128      * may treat it as an unusual input, without any constant-timeness
129      * guarantee.
130      */
131     int (*mul) (const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
132                 size_t num, const EC_POINT *points[], const BIGNUM *scalars[],
133                 BN_CTX *);
134     int (*precompute_mult) (EC_GROUP *group, BN_CTX *);
135     int (*have_precompute_mult) (const EC_GROUP *group);
136     /* internal functions */
137     /*
138      * 'field_mul', 'field_sqr', and 'field_div' can be used by 'add' and
139      * 'dbl' so that the same implementations of point operations can be used
140      * with different optimized implementations of expensive field
141      * operations:
142      */
143     int (*field_mul) (const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
144                       const BIGNUM *b, BN_CTX *);
145     int (*field_sqr) (const EC_GROUP *, BIGNUM *r, const BIGNUM *a, BN_CTX *);
146     int (*field_div) (const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
147                       const BIGNUM *b, BN_CTX *);
148     /*-
149      * 'field_inv' computes the multiplicative inverse of a in the field,
150      * storing the result in r.
151      *
152      * If 'a' is zero (or equivalent), you'll get an EC_R_CANNOT_INVERT error.
153      */
154     int (*field_inv) (const EC_GROUP *, BIGNUM *r, const BIGNUM *a, BN_CTX *);
155     /* e.g. to Montgomery */
156     int (*field_encode) (const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
157                          BN_CTX *);
158     /* e.g. from Montgomery */
159     int (*field_decode) (const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
160                          BN_CTX *);
161     int (*field_set_to_one) (const EC_GROUP *, BIGNUM *r, BN_CTX *);
162     /* private key operations */
163     size_t (*priv2oct)(const EC_KEY *eckey, unsigned char *buf, size_t len);
164     int (*oct2priv)(EC_KEY *eckey, const unsigned char *buf, size_t len);
165     int (*set_private)(EC_KEY *eckey, const BIGNUM *priv_key);
166     int (*keygen)(EC_KEY *eckey);
167     int (*keycheck)(const EC_KEY *eckey);
168     int (*keygenpub)(EC_KEY *eckey);
169     int (*keycopy)(EC_KEY *dst, const EC_KEY *src);
170     void (*keyfinish)(EC_KEY *eckey);
171     /* custom ECDH operation */
172     int (*ecdh_compute_key)(unsigned char **pout, size_t *poutlen,
173                             const EC_POINT *pub_key, const EC_KEY *ecdh);
174     /* custom ECDSA */
175     int (*ecdsa_sign_setup)(EC_KEY *eckey, BN_CTX *ctx, BIGNUM **kinvp,
176                             BIGNUM **rp);
177     ECDSA_SIG *(*ecdsa_sign_sig)(const unsigned char *dgst, int dgstlen,
178                                  const BIGNUM *kinv, const BIGNUM *r,
179                                  EC_KEY *eckey);
180     int (*ecdsa_verify_sig)(const unsigned char *dgst, int dgstlen,
181                             const ECDSA_SIG *sig, EC_KEY *eckey);
182     /* Inverse modulo order */
183     int (*field_inverse_mod_ord)(const EC_GROUP *, BIGNUM *r,
184                                  const BIGNUM *x, BN_CTX *);
185     int (*blind_coordinates)(const EC_GROUP *group, EC_POINT *p, BN_CTX *ctx);
186     int (*ladder_pre)(const EC_GROUP *group,
187                       EC_POINT *r, EC_POINT *s,
188                       EC_POINT *p, BN_CTX *ctx);
189     int (*ladder_step)(const EC_GROUP *group,
190                        EC_POINT *r, EC_POINT *s,
191                        EC_POINT *p, BN_CTX *ctx);
192     int (*ladder_post)(const EC_GROUP *group,
193                        EC_POINT *r, EC_POINT *s,
194                        EC_POINT *p, BN_CTX *ctx);
195 };
196
197 /*
198  * Types and functions to manipulate pre-computed values.
199  */
200 typedef struct nistp224_pre_comp_st NISTP224_PRE_COMP;
201 typedef struct nistp256_pre_comp_st NISTP256_PRE_COMP;
202 typedef struct nistp521_pre_comp_st NISTP521_PRE_COMP;
203 typedef struct nistz256_pre_comp_st NISTZ256_PRE_COMP;
204 typedef struct ec_pre_comp_st EC_PRE_COMP;
205
206 struct ec_group_st {
207     const EC_METHOD *meth;
208     EC_POINT *generator;        /* optional */
209     BIGNUM *order, *cofactor;
210     int curve_name;             /* optional NID for named curve */
211     int asn1_flag;              /* flag to control the asn1 encoding */
212     point_conversion_form_t asn1_form;
213     unsigned char *seed;        /* optional seed for parameters (appears in
214                                  * ASN1) */
215     size_t seed_len;
216     /*
217      * The following members are handled by the method functions, even if
218      * they appear generic
219      */
220     /*
221      * Field specification. For curves over GF(p), this is the modulus; for
222      * curves over GF(2^m), this is the irreducible polynomial defining the
223      * field.
224      */
225     BIGNUM *field;
226     /*
227      * Field specification for curves over GF(2^m). The irreducible f(t) is
228      * then of the form: t^poly[0] + t^poly[1] + ... + t^poly[k] where m =
229      * poly[0] > poly[1] > ... > poly[k] = 0. The array is terminated with
230      * poly[k+1]=-1. All elliptic curve irreducibles have at most 5 non-zero
231      * terms.
232      */
233     int poly[6];
234     /*
235      * Curve coefficients. (Here the assumption is that BIGNUMs can be used
236      * or abused for all kinds of fields, not just GF(p).) For characteristic
237      * > 3, the curve is defined by a Weierstrass equation of the form y^2 =
238      * x^3 + a*x + b. For characteristic 2, the curve is defined by an
239      * equation of the form y^2 + x*y = x^3 + a*x^2 + b.
240      */
241     BIGNUM *a, *b;
242     /* enable optimized point arithmetics for special case */
243     int a_is_minus3;
244     /* method-specific (e.g., Montgomery structure) */
245     void *field_data1;
246     /* method-specific */
247     void *field_data2;
248     /* method-specific */
249     int (*field_mod_func) (BIGNUM *, const BIGNUM *, const BIGNUM *,
250                            BN_CTX *);
251     /* data for ECDSA inverse */
252     BN_MONT_CTX *mont_data;
253
254     /*
255      * Precomputed values for speed. The PCT_xxx names match the
256      * pre_comp.xxx union names; see the SETPRECOMP and HAVEPRECOMP
257      * macros, below.
258      */
259     enum {
260         PCT_none,
261         PCT_nistp224, PCT_nistp256, PCT_nistp521, PCT_nistz256,
262         PCT_ec
263     } pre_comp_type;
264     union {
265         NISTP224_PRE_COMP *nistp224;
266         NISTP256_PRE_COMP *nistp256;
267         NISTP521_PRE_COMP *nistp521;
268         NISTZ256_PRE_COMP *nistz256;
269         EC_PRE_COMP *ec;
270     } pre_comp;
271
272     OPENSSL_CTX *libctx;
273 };
274
275 #define SETPRECOMP(g, type, pre) \
276     g->pre_comp_type = PCT_##type, g->pre_comp.type = pre
277 #define HAVEPRECOMP(g, type) \
278     g->pre_comp_type == PCT_##type && g->pre_comp.type != NULL
279
280 struct ec_key_st {
281     const EC_KEY_METHOD *meth;
282     ENGINE *engine;
283     int version;
284     EC_GROUP *group;
285     EC_POINT *pub_key;
286     BIGNUM *priv_key;
287     unsigned int enc_flag;
288     point_conversion_form_t conv_form;
289     CRYPTO_REF_COUNT references;
290     int flags;
291 #ifndef FIPS_MODULE
292     CRYPTO_EX_DATA ex_data;
293 #endif
294     CRYPTO_RWLOCK *lock;
295     OPENSSL_CTX *libctx;
296
297     /* Provider data */
298     size_t dirty_cnt; /* If any key material changes, increment this */
299 };
300
301 struct ec_point_st {
302     const EC_METHOD *meth;
303     /* NID for the curve if known */
304     int curve_name;
305     /*
306      * All members except 'meth' are handled by the method functions, even if
307      * they appear generic
308      */
309     BIGNUM *X;
310     BIGNUM *Y;
311     BIGNUM *Z;                  /* Jacobian projective coordinates: * (X, Y,
312                                  * Z) represents (X/Z^2, Y/Z^3) if Z != 0 */
313     int Z_is_one;               /* enable optimized point arithmetics for
314                                  * special case */
315 };
316
317 static ossl_inline int ec_point_is_compat(const EC_POINT *point,
318                                           const EC_GROUP *group)
319 {
320     return group->meth == point->meth
321            && (group->curve_name == 0
322                || point->curve_name == 0
323                || group->curve_name == point->curve_name);
324 }
325
326 NISTP224_PRE_COMP *EC_nistp224_pre_comp_dup(NISTP224_PRE_COMP *);
327 NISTP256_PRE_COMP *EC_nistp256_pre_comp_dup(NISTP256_PRE_COMP *);
328 NISTP521_PRE_COMP *EC_nistp521_pre_comp_dup(NISTP521_PRE_COMP *);
329 NISTZ256_PRE_COMP *EC_nistz256_pre_comp_dup(NISTZ256_PRE_COMP *);
330 NISTP256_PRE_COMP *EC_nistp256_pre_comp_dup(NISTP256_PRE_COMP *);
331 EC_PRE_COMP *EC_ec_pre_comp_dup(EC_PRE_COMP *);
332
333 void EC_pre_comp_free(EC_GROUP *group);
334 void EC_nistp224_pre_comp_free(NISTP224_PRE_COMP *);
335 void EC_nistp256_pre_comp_free(NISTP256_PRE_COMP *);
336 void EC_nistp521_pre_comp_free(NISTP521_PRE_COMP *);
337 void EC_nistz256_pre_comp_free(NISTZ256_PRE_COMP *);
338 void EC_ec_pre_comp_free(EC_PRE_COMP *);
339
340 /*
341  * method functions in ec_mult.c (ec_lib.c uses these as defaults if
342  * group->method->mul is 0)
343  */
344 int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
345                 size_t num, const EC_POINT *points[], const BIGNUM *scalars[],
346                 BN_CTX *);
347 int ec_wNAF_precompute_mult(EC_GROUP *group, BN_CTX *);
348 int ec_wNAF_have_precompute_mult(const EC_GROUP *group);
349
350 /* method functions in ecp_smpl.c */
351 int ec_GFp_simple_group_init(EC_GROUP *);
352 void ec_GFp_simple_group_finish(EC_GROUP *);
353 void ec_GFp_simple_group_clear_finish(EC_GROUP *);
354 int ec_GFp_simple_group_copy(EC_GROUP *, const EC_GROUP *);
355 int ec_GFp_simple_group_set_curve(EC_GROUP *, const BIGNUM *p,
356                                   const BIGNUM *a, const BIGNUM *b, BN_CTX *);
357 int ec_GFp_simple_group_get_curve(const EC_GROUP *, BIGNUM *p, BIGNUM *a,
358                                   BIGNUM *b, BN_CTX *);
359 int ec_GFp_simple_group_get_degree(const EC_GROUP *);
360 int ec_GFp_simple_group_check_discriminant(const EC_GROUP *, BN_CTX *);
361 int ec_GFp_simple_point_init(EC_POINT *);
362 void ec_GFp_simple_point_finish(EC_POINT *);
363 void ec_GFp_simple_point_clear_finish(EC_POINT *);
364 int ec_GFp_simple_point_copy(EC_POINT *, const EC_POINT *);
365 int ec_GFp_simple_point_set_to_infinity(const EC_GROUP *, EC_POINT *);
366 int ec_GFp_simple_set_Jprojective_coordinates_GFp(const EC_GROUP *,
367                                                   EC_POINT *, const BIGNUM *x,
368                                                   const BIGNUM *y,
369                                                   const BIGNUM *z, BN_CTX *);
370 int ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP *,
371                                                   const EC_POINT *, BIGNUM *x,
372                                                   BIGNUM *y, BIGNUM *z,
373                                                   BN_CTX *);
374 int ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP *, EC_POINT *,
375                                                const BIGNUM *x,
376                                                const BIGNUM *y, BN_CTX *);
377 int ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP *,
378                                                const EC_POINT *, BIGNUM *x,
379                                                BIGNUM *y, BN_CTX *);
380 int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *, EC_POINT *,
381                                              const BIGNUM *x, int y_bit,
382                                              BN_CTX *);
383 size_t ec_GFp_simple_point2oct(const EC_GROUP *, const EC_POINT *,
384                                point_conversion_form_t form,
385                                unsigned char *buf, size_t len, BN_CTX *);
386 int ec_GFp_simple_oct2point(const EC_GROUP *, EC_POINT *,
387                             const unsigned char *buf, size_t len, BN_CTX *);
388 int ec_GFp_simple_add(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
389                       const EC_POINT *b, BN_CTX *);
390 int ec_GFp_simple_dbl(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
391                       BN_CTX *);
392 int ec_GFp_simple_invert(const EC_GROUP *, EC_POINT *, BN_CTX *);
393 int ec_GFp_simple_is_at_infinity(const EC_GROUP *, const EC_POINT *);
394 int ec_GFp_simple_is_on_curve(const EC_GROUP *, const EC_POINT *, BN_CTX *);
395 int ec_GFp_simple_cmp(const EC_GROUP *, const EC_POINT *a, const EC_POINT *b,
396                       BN_CTX *);
397 int ec_GFp_simple_make_affine(const EC_GROUP *, EC_POINT *, BN_CTX *);
398 int ec_GFp_simple_points_make_affine(const EC_GROUP *, size_t num,
399                                      EC_POINT *[], BN_CTX *);
400 int ec_GFp_simple_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
401                             const BIGNUM *b, BN_CTX *);
402 int ec_GFp_simple_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
403                             BN_CTX *);
404 int ec_GFp_simple_field_inv(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
405                             BN_CTX *);
406 int ec_GFp_simple_blind_coordinates(const EC_GROUP *group, EC_POINT *p,
407                                     BN_CTX *ctx);
408 int ec_GFp_simple_ladder_pre(const EC_GROUP *group,
409                              EC_POINT *r, EC_POINT *s,
410                              EC_POINT *p, BN_CTX *ctx);
411 int ec_GFp_simple_ladder_step(const EC_GROUP *group,
412                               EC_POINT *r, EC_POINT *s,
413                               EC_POINT *p, BN_CTX *ctx);
414 int ec_GFp_simple_ladder_post(const EC_GROUP *group,
415                               EC_POINT *r, EC_POINT *s,
416                               EC_POINT *p, BN_CTX *ctx);
417
418 /* method functions in ecp_mont.c */
419 int ec_GFp_mont_group_init(EC_GROUP *);
420 int ec_GFp_mont_group_set_curve(EC_GROUP *, const BIGNUM *p, const BIGNUM *a,
421                                 const BIGNUM *b, BN_CTX *);
422 void ec_GFp_mont_group_finish(EC_GROUP *);
423 void ec_GFp_mont_group_clear_finish(EC_GROUP *);
424 int ec_GFp_mont_group_copy(EC_GROUP *, const EC_GROUP *);
425 int ec_GFp_mont_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
426                           const BIGNUM *b, BN_CTX *);
427 int ec_GFp_mont_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
428                           BN_CTX *);
429 int ec_GFp_mont_field_inv(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
430                           BN_CTX *);
431 int ec_GFp_mont_field_encode(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
432                              BN_CTX *);
433 int ec_GFp_mont_field_decode(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
434                              BN_CTX *);
435 int ec_GFp_mont_field_set_to_one(const EC_GROUP *, BIGNUM *r, BN_CTX *);
436
437 /* method functions in ecp_nist.c */
438 int ec_GFp_nist_group_copy(EC_GROUP *dest, const EC_GROUP *src);
439 int ec_GFp_nist_group_set_curve(EC_GROUP *, const BIGNUM *p, const BIGNUM *a,
440                                 const BIGNUM *b, BN_CTX *);
441 int ec_GFp_nist_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
442                           const BIGNUM *b, BN_CTX *);
443 int ec_GFp_nist_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
444                           BN_CTX *);
445
446 /* method functions in ec2_smpl.c */
447 int ec_GF2m_simple_group_init(EC_GROUP *);
448 void ec_GF2m_simple_group_finish(EC_GROUP *);
449 void ec_GF2m_simple_group_clear_finish(EC_GROUP *);
450 int ec_GF2m_simple_group_copy(EC_GROUP *, const EC_GROUP *);
451 int ec_GF2m_simple_group_set_curve(EC_GROUP *, const BIGNUM *p,
452                                    const BIGNUM *a, const BIGNUM *b,
453                                    BN_CTX *);
454 int ec_GF2m_simple_group_get_curve(const EC_GROUP *, BIGNUM *p, BIGNUM *a,
455                                    BIGNUM *b, BN_CTX *);
456 int ec_GF2m_simple_group_get_degree(const EC_GROUP *);
457 int ec_GF2m_simple_group_check_discriminant(const EC_GROUP *, BN_CTX *);
458 int ec_GF2m_simple_point_init(EC_POINT *);
459 void ec_GF2m_simple_point_finish(EC_POINT *);
460 void ec_GF2m_simple_point_clear_finish(EC_POINT *);
461 int ec_GF2m_simple_point_copy(EC_POINT *, const EC_POINT *);
462 int ec_GF2m_simple_point_set_to_infinity(const EC_GROUP *, EC_POINT *);
463 int ec_GF2m_simple_point_set_affine_coordinates(const EC_GROUP *, EC_POINT *,
464                                                 const BIGNUM *x,
465                                                 const BIGNUM *y, BN_CTX *);
466 int ec_GF2m_simple_point_get_affine_coordinates(const EC_GROUP *,
467                                                 const EC_POINT *, BIGNUM *x,
468                                                 BIGNUM *y, BN_CTX *);
469 int ec_GF2m_simple_set_compressed_coordinates(const EC_GROUP *, EC_POINT *,
470                                               const BIGNUM *x, int y_bit,
471                                               BN_CTX *);
472 size_t ec_GF2m_simple_point2oct(const EC_GROUP *, const EC_POINT *,
473                                 point_conversion_form_t form,
474                                 unsigned char *buf, size_t len, BN_CTX *);
475 int ec_GF2m_simple_oct2point(const EC_GROUP *, EC_POINT *,
476                              const unsigned char *buf, size_t len, BN_CTX *);
477 int ec_GF2m_simple_add(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
478                        const EC_POINT *b, BN_CTX *);
479 int ec_GF2m_simple_dbl(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
480                        BN_CTX *);
481 int ec_GF2m_simple_invert(const EC_GROUP *, EC_POINT *, BN_CTX *);
482 int ec_GF2m_simple_is_at_infinity(const EC_GROUP *, const EC_POINT *);
483 int ec_GF2m_simple_is_on_curve(const EC_GROUP *, const EC_POINT *, BN_CTX *);
484 int ec_GF2m_simple_cmp(const EC_GROUP *, const EC_POINT *a, const EC_POINT *b,
485                        BN_CTX *);
486 int ec_GF2m_simple_make_affine(const EC_GROUP *, EC_POINT *, BN_CTX *);
487 int ec_GF2m_simple_points_make_affine(const EC_GROUP *, size_t num,
488                                       EC_POINT *[], BN_CTX *);
489 int ec_GF2m_simple_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
490                              const BIGNUM *b, BN_CTX *);
491 int ec_GF2m_simple_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
492                              BN_CTX *);
493 int ec_GF2m_simple_field_div(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
494                              const BIGNUM *b, BN_CTX *);
495
496 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
497 /* method functions in ecp_nistp224.c */
498 int ec_GFp_nistp224_group_init(EC_GROUP *group);
499 int ec_GFp_nistp224_group_set_curve(EC_GROUP *group, const BIGNUM *p,
500                                     const BIGNUM *a, const BIGNUM *n,
501                                     BN_CTX *);
502 int ec_GFp_nistp224_point_get_affine_coordinates(const EC_GROUP *group,
503                                                  const EC_POINT *point,
504                                                  BIGNUM *x, BIGNUM *y,
505                                                  BN_CTX *ctx);
506 int ec_GFp_nistp224_mul(const EC_GROUP *group, EC_POINT *r,
507                         const BIGNUM *scalar, size_t num,
508                         const EC_POINT *points[], const BIGNUM *scalars[],
509                         BN_CTX *);
510 int ec_GFp_nistp224_points_mul(const EC_GROUP *group, EC_POINT *r,
511                                const BIGNUM *scalar, size_t num,
512                                const EC_POINT *points[],
513                                const BIGNUM *scalars[], BN_CTX *ctx);
514 int ec_GFp_nistp224_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
515 int ec_GFp_nistp224_have_precompute_mult(const EC_GROUP *group);
516
517 /* method functions in ecp_nistp256.c */
518 int ec_GFp_nistp256_group_init(EC_GROUP *group);
519 int ec_GFp_nistp256_group_set_curve(EC_GROUP *group, const BIGNUM *p,
520                                     const BIGNUM *a, const BIGNUM *n,
521                                     BN_CTX *);
522 int ec_GFp_nistp256_point_get_affine_coordinates(const EC_GROUP *group,
523                                                  const EC_POINT *point,
524                                                  BIGNUM *x, BIGNUM *y,
525                                                  BN_CTX *ctx);
526 int ec_GFp_nistp256_mul(const EC_GROUP *group, EC_POINT *r,
527                         const BIGNUM *scalar, size_t num,
528                         const EC_POINT *points[], const BIGNUM *scalars[],
529                         BN_CTX *);
530 int ec_GFp_nistp256_points_mul(const EC_GROUP *group, EC_POINT *r,
531                                const BIGNUM *scalar, size_t num,
532                                const EC_POINT *points[],
533                                const BIGNUM *scalars[], BN_CTX *ctx);
534 int ec_GFp_nistp256_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
535 int ec_GFp_nistp256_have_precompute_mult(const EC_GROUP *group);
536
537 /* method functions in ecp_nistp521.c */
538 int ec_GFp_nistp521_group_init(EC_GROUP *group);
539 int ec_GFp_nistp521_group_set_curve(EC_GROUP *group, const BIGNUM *p,
540                                     const BIGNUM *a, const BIGNUM *n,
541                                     BN_CTX *);
542 int ec_GFp_nistp521_point_get_affine_coordinates(const EC_GROUP *group,
543                                                  const EC_POINT *point,
544                                                  BIGNUM *x, BIGNUM *y,
545                                                  BN_CTX *ctx);
546 int ec_GFp_nistp521_mul(const EC_GROUP *group, EC_POINT *r,
547                         const BIGNUM *scalar, size_t num,
548                         const EC_POINT *points[], const BIGNUM *scalars[],
549                         BN_CTX *);
550 int ec_GFp_nistp521_points_mul(const EC_GROUP *group, EC_POINT *r,
551                                const BIGNUM *scalar, size_t num,
552                                const EC_POINT *points[],
553                                const BIGNUM *scalars[], BN_CTX *ctx);
554 int ec_GFp_nistp521_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
555 int ec_GFp_nistp521_have_precompute_mult(const EC_GROUP *group);
556
557 /* utility functions in ecp_nistputil.c */
558 void ec_GFp_nistp_points_make_affine_internal(size_t num, void *point_array,
559                                               size_t felem_size,
560                                               void *tmp_felems,
561                                               void (*felem_one) (void *out),
562                                               int (*felem_is_zero) (const void
563                                                                     *in),
564                                               void (*felem_assign) (void *out,
565                                                                     const void
566                                                                     *in),
567                                               void (*felem_square) (void *out,
568                                                                     const void
569                                                                     *in),
570                                               void (*felem_mul) (void *out,
571                                                                  const void
572                                                                  *in1,
573                                                                  const void
574                                                                  *in2),
575                                               void (*felem_inv) (void *out,
576                                                                  const void
577                                                                  *in),
578                                               void (*felem_contract) (void
579                                                                       *out,
580                                                                       const
581                                                                       void
582                                                                       *in));
583 void ec_GFp_nistp_recode_scalar_bits(unsigned char *sign,
584                                      unsigned char *digit, unsigned char in);
585 #endif
586 int ec_group_simple_order_bits(const EC_GROUP *group);
587
588 #ifdef ECP_NISTZ256_ASM
589 /** Returns GFp methods using montgomery multiplication, with x86-64 optimized
590  * P256. See http://eprint.iacr.org/2013/816.
591  *  \return  EC_METHOD object
592  */
593 const EC_METHOD *EC_GFp_nistz256_method(void);
594 #endif
595 #ifdef S390X_EC_ASM
596 const EC_METHOD *EC_GFp_s390x_nistp256_method(void);
597 const EC_METHOD *EC_GFp_s390x_nistp384_method(void);
598 const EC_METHOD *EC_GFp_s390x_nistp521_method(void);
599 #endif
600
601 size_t ec_key_simple_priv2oct(const EC_KEY *eckey,
602                               unsigned char *buf, size_t len);
603 int ec_key_simple_oct2priv(EC_KEY *eckey, const unsigned char *buf, size_t len);
604 int ec_key_simple_generate_key(EC_KEY *eckey);
605 int ec_key_simple_generate_public_key(EC_KEY *eckey);
606 int ec_key_simple_check_key(const EC_KEY *eckey);
607
608 int ec_curve_nid_from_params(const EC_GROUP *group, BN_CTX *ctx);
609
610 /* EC_METHOD definitions */
611
612 struct ec_key_method_st {
613     const char *name;
614     int32_t flags;
615     int (*init)(EC_KEY *key);
616     void (*finish)(EC_KEY *key);
617     int (*copy)(EC_KEY *dest, const EC_KEY *src);
618     int (*set_group)(EC_KEY *key, const EC_GROUP *grp);
619     int (*set_private)(EC_KEY *key, const BIGNUM *priv_key);
620     int (*set_public)(EC_KEY *key, const EC_POINT *pub_key);
621     int (*keygen)(EC_KEY *key);
622     int (*compute_key)(unsigned char **pout, size_t *poutlen,
623                        const EC_POINT *pub_key, const EC_KEY *ecdh);
624     int (*sign)(int type, const unsigned char *dgst, int dlen, unsigned char
625                 *sig, unsigned int *siglen, const BIGNUM *kinv,
626                 const BIGNUM *r, EC_KEY *eckey);
627     int (*sign_setup)(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
628                       BIGNUM **rp);
629     ECDSA_SIG *(*sign_sig)(const unsigned char *dgst, int dgst_len,
630                            const BIGNUM *in_kinv, const BIGNUM *in_r,
631                            EC_KEY *eckey);
632
633     int (*verify)(int type, const unsigned char *dgst, int dgst_len,
634                   const unsigned char *sigbuf, int sig_len, EC_KEY *eckey);
635     int (*verify_sig)(const unsigned char *dgst, int dgst_len,
636                       const ECDSA_SIG *sig, EC_KEY *eckey);
637 };
638
639 #define EC_KEY_METHOD_DYNAMIC   1
640
641 EC_KEY *ec_key_new_method_int(OPENSSL_CTX *libctx, ENGINE *engine);
642
643 int ossl_ec_key_gen(EC_KEY *eckey);
644 int ossl_ecdh_compute_key(unsigned char **pout, size_t *poutlen,
645                           const EC_POINT *pub_key, const EC_KEY *ecdh);
646 int ecdh_simple_compute_key(unsigned char **pout, size_t *poutlen,
647                             const EC_POINT *pub_key, const EC_KEY *ecdh);
648
649 struct ECDSA_SIG_st {
650     BIGNUM *r;
651     BIGNUM *s;
652 };
653
654 int ossl_ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
655                           BIGNUM **rp);
656 int ossl_ecdsa_sign(int type, const unsigned char *dgst, int dlen,
657                     unsigned char *sig, unsigned int *siglen,
658                     const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey);
659 ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
660                                const BIGNUM *in_kinv, const BIGNUM *in_r,
661                                EC_KEY *eckey);
662 int ossl_ecdsa_verify(int type, const unsigned char *dgst, int dgst_len,
663                       const unsigned char *sigbuf, int sig_len, EC_KEY *eckey);
664 int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len,
665                           const ECDSA_SIG *sig, EC_KEY *eckey);
666 int ecdsa_simple_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
667                             BIGNUM **rp);
668 ECDSA_SIG *ecdsa_simple_sign_sig(const unsigned char *dgst, int dgst_len,
669                                  const BIGNUM *in_kinv, const BIGNUM *in_r,
670                                  EC_KEY *eckey);
671 int ecdsa_simple_verify_sig(const unsigned char *dgst, int dgst_len,
672                             const ECDSA_SIG *sig, EC_KEY *eckey);
673
674
675 /*-
676  * This functions computes a single point multiplication over the EC group,
677  * using, at a high level, a Montgomery ladder with conditional swaps, with
678  * various timing attack defenses.
679  *
680  * It performs either a fixed point multiplication
681  *          (scalar * generator)
682  * when point is NULL, or a variable point multiplication
683  *          (scalar * point)
684  * when point is not NULL.
685  *
686  * `scalar` cannot be NULL and should be in the range [0,n) otherwise all
687  * constant time bets are off (where n is the cardinality of the EC group).
688  *
689  * This function expects `group->order` and `group->cardinality` to be well
690  * defined and non-zero: it fails with an error code otherwise.
691  *
692  * NB: This says nothing about the constant-timeness of the ladder step
693  * implementation (i.e., the default implementation is based on EC_POINT_add and
694  * EC_POINT_dbl, which of course are not constant time themselves) or the
695  * underlying multiprecision arithmetic.
696  *
697  * The product is stored in `r`.
698  *
699  * This is an internal function: callers are in charge of ensuring that the
700  * input parameters `group`, `r`, `scalar` and `ctx` are not NULL.
701  *
702  * Returns 1 on success, 0 otherwise.
703  */
704 int ec_scalar_mul_ladder(const EC_GROUP *group, EC_POINT *r,
705                          const BIGNUM *scalar, const EC_POINT *point,
706                          BN_CTX *ctx);
707
708 int ec_point_blind_coordinates(const EC_GROUP *group, EC_POINT *p, BN_CTX *ctx);
709
710 static ossl_inline int ec_point_ladder_pre(const EC_GROUP *group,
711                                            EC_POINT *r, EC_POINT *s,
712                                            EC_POINT *p, BN_CTX *ctx)
713 {
714     if (group->meth->ladder_pre != NULL)
715         return group->meth->ladder_pre(group, r, s, p, ctx);
716
717     if (!EC_POINT_copy(s, p)
718         || !EC_POINT_dbl(group, r, s, ctx))
719         return 0;
720
721     return 1;
722 }
723
724 static ossl_inline int ec_point_ladder_step(const EC_GROUP *group,
725                                             EC_POINT *r, EC_POINT *s,
726                                             EC_POINT *p, BN_CTX *ctx)
727 {
728     if (group->meth->ladder_step != NULL)
729         return group->meth->ladder_step(group, r, s, p, ctx);
730
731     if (!EC_POINT_add(group, s, r, s, ctx)
732         || !EC_POINT_dbl(group, r, r, ctx))
733         return 0;
734
735     return 1;
736
737 }
738
739 static ossl_inline int ec_point_ladder_post(const EC_GROUP *group,
740                                             EC_POINT *r, EC_POINT *s,
741                                             EC_POINT *p, BN_CTX *ctx)
742 {
743     if (group->meth->ladder_post != NULL)
744         return group->meth->ladder_post(group, r, s, p, ctx);
745
746     return 1;
747 }