Implement coordinate blinding for EC_POINT
[oweals/openssl.git] / crypto / ec / ec_lcl.h
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
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 "curve448/curve448_lcl.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_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,
57                             BN_CTX *);
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 *);
63     /*
64      * used by EC_POINT_new, EC_POINT_free, EC_POINT_clear_free,
65      * EC_POINT_copy:
66      */
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 *);
71     /*-
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:
78      */
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,
82                                                   const BIGNUM *y,
83                                                   const BIGNUM *z, BN_CTX *);
84     int (*point_get_Jprojective_coordinates_GFp) (const EC_GROUP *,
85                                                   const EC_POINT *, BIGNUM *x,
86                                                   BIGNUM *y, BIGNUM *z,
87                                                   BN_CTX *);
88     int (*point_set_affine_coordinates) (const EC_GROUP *, EC_POINT *,
89                                          const BIGNUM *x, const BIGNUM *y,
90                                          BN_CTX *);
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,
95                                              BN_CTX *);
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 *);
107     /*
108      * used by EC_POINT_is_at_infinity, EC_POINT_is_on_curve, EC_POINT_cmp:
109      */
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,
113                       BN_CTX *);
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 *[],
117                                BN_CTX *);
118     /*
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):
122      */
123     /*-
124      * mul() calculates the value
125      *
126      *   r := generator * scalar
127      *        + points[0] * scalars[0]
128      *        + ...
129      *        + points[num-1] * scalars[num-1].
130      *
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
138      * guarantee.
139      */
140     int (*mul) (const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
141                 size_t num, const EC_POINT *points[], const BIGNUM *scalars[],
142                 BN_CTX *);
143     int (*precompute_mult) (EC_GROUP *group, BN_CTX *);
144     int (*have_precompute_mult) (const EC_GROUP *group);
145     /* internal functions */
146     /*
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
150      * operations:
151      */
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,
159                          BN_CTX *);
160     /* e.g. from Montgomery */
161     int (*field_decode) (const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
162                          BN_CTX *);
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,
178                                  BN_CTX *ctx);
179     int (*blind_coordinates)(const EC_GROUP *group, EC_POINT *p, BN_CTX *ctx);
180 };
181
182 /*
183  * Types and functions to manipulate pre-computed values.
184  */
185 typedef struct nistp224_pre_comp_st NISTP224_PRE_COMP;
186 typedef struct nistp256_pre_comp_st NISTP256_PRE_COMP;
187 typedef struct nistp521_pre_comp_st NISTP521_PRE_COMP;
188 typedef struct nistz256_pre_comp_st NISTZ256_PRE_COMP;
189 typedef struct ec_pre_comp_st EC_PRE_COMP;
190
191 struct ec_group_st {
192     const EC_METHOD *meth;
193     EC_POINT *generator;        /* optional */
194     BIGNUM *order, *cofactor;
195     int curve_name;             /* optional NID for named curve */
196     int asn1_flag;              /* flag to control the asn1 encoding */
197     point_conversion_form_t asn1_form;
198     unsigned char *seed;        /* optional seed for parameters (appears in
199                                  * ASN1) */
200     size_t seed_len;
201     /*
202      * The following members are handled by the method functions, even if
203      * they appear generic
204      */
205     /*
206      * Field specification. For curves over GF(p), this is the modulus; for
207      * curves over GF(2^m), this is the irreducible polynomial defining the
208      * field.
209      */
210     BIGNUM *field;
211     /*
212      * Field specification for curves over GF(2^m). The irreducible f(t) is
213      * then of the form: t^poly[0] + t^poly[1] + ... + t^poly[k] where m =
214      * poly[0] > poly[1] > ... > poly[k] = 0. The array is terminated with
215      * poly[k+1]=-1. All elliptic curve irreducibles have at most 5 non-zero
216      * terms.
217      */
218     int poly[6];
219     /*
220      * Curve coefficients. (Here the assumption is that BIGNUMs can be used
221      * or abused for all kinds of fields, not just GF(p).) For characteristic
222      * > 3, the curve is defined by a Weierstrass equation of the form y^2 =
223      * x^3 + a*x + b. For characteristic 2, the curve is defined by an
224      * equation of the form y^2 + x*y = x^3 + a*x^2 + b.
225      */
226     BIGNUM *a, *b;
227     /* enable optimized point arithmetics for special case */
228     int a_is_minus3;
229     /* method-specific (e.g., Montgomery structure) */
230     void *field_data1;
231     /* method-specific */
232     void *field_data2;
233     /* method-specific */
234     int (*field_mod_func) (BIGNUM *, const BIGNUM *, const BIGNUM *,
235                            BN_CTX *);
236     /* data for ECDSA inverse */
237     BN_MONT_CTX *mont_data;
238
239     /*
240      * Precomputed values for speed. The PCT_xxx names match the
241      * pre_comp.xxx union names; see the SETPRECOMP and HAVEPRECOMP
242      * macros, below.
243      */
244     enum {
245         PCT_none,
246         PCT_nistp224, PCT_nistp256, PCT_nistp521, PCT_nistz256,
247         PCT_ec
248     } pre_comp_type;
249     union {
250         NISTP224_PRE_COMP *nistp224;
251         NISTP256_PRE_COMP *nistp256;
252         NISTP521_PRE_COMP *nistp521;
253         NISTZ256_PRE_COMP *nistz256;
254         EC_PRE_COMP *ec;
255     } pre_comp;
256 };
257
258 #define SETPRECOMP(g, type, pre) \
259     g->pre_comp_type = PCT_##type, g->pre_comp.type = pre
260 #define HAVEPRECOMP(g, type) \
261     g->pre_comp_type == PCT_##type && g->pre_comp.type != NULL
262
263 struct ec_key_st {
264     const EC_KEY_METHOD *meth;
265     ENGINE *engine;
266     int version;
267     EC_GROUP *group;
268     EC_POINT *pub_key;
269     BIGNUM *priv_key;
270     unsigned int enc_flag;
271     point_conversion_form_t conv_form;
272     CRYPTO_REF_COUNT references;
273     int flags;
274     CRYPTO_EX_DATA ex_data;
275     CRYPTO_RWLOCK *lock;
276 };
277
278 struct ec_point_st {
279     const EC_METHOD *meth;
280     /* NID for the curve if known */
281     int curve_name;
282     /*
283      * All members except 'meth' are handled by the method functions, even if
284      * they appear generic
285      */
286     BIGNUM *X;
287     BIGNUM *Y;
288     BIGNUM *Z;                  /* Jacobian projective coordinates: * (X, Y,
289                                  * Z) represents (X/Z^2, Y/Z^3) if Z != 0 */
290     int Z_is_one;               /* enable optimized point arithmetics for
291                                  * special case */
292 };
293
294
295 static ossl_inline int ec_point_is_compat(const EC_POINT *point,
296                                           const EC_GROUP *group)
297 {
298     if (group->meth != point->meth
299         || (group->curve_name != 0
300             && point->curve_name != 0
301             && group->curve_name != point->curve_name))
302         return 0;
303
304     return 1;
305 }
306
307
308 NISTP224_PRE_COMP *EC_nistp224_pre_comp_dup(NISTP224_PRE_COMP *);
309 NISTP256_PRE_COMP *EC_nistp256_pre_comp_dup(NISTP256_PRE_COMP *);
310 NISTP521_PRE_COMP *EC_nistp521_pre_comp_dup(NISTP521_PRE_COMP *);
311 NISTZ256_PRE_COMP *EC_nistz256_pre_comp_dup(NISTZ256_PRE_COMP *);
312 NISTP256_PRE_COMP *EC_nistp256_pre_comp_dup(NISTP256_PRE_COMP *);
313 EC_PRE_COMP *EC_ec_pre_comp_dup(EC_PRE_COMP *);
314
315 void EC_pre_comp_free(EC_GROUP *group);
316 void EC_nistp224_pre_comp_free(NISTP224_PRE_COMP *);
317 void EC_nistp256_pre_comp_free(NISTP256_PRE_COMP *);
318 void EC_nistp521_pre_comp_free(NISTP521_PRE_COMP *);
319 void EC_nistz256_pre_comp_free(NISTZ256_PRE_COMP *);
320 void EC_ec_pre_comp_free(EC_PRE_COMP *);
321
322 /*
323  * method functions in ec_mult.c (ec_lib.c uses these as defaults if
324  * group->method->mul is 0)
325  */
326 int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
327                 size_t num, const EC_POINT *points[], const BIGNUM *scalars[],
328                 BN_CTX *);
329 int ec_wNAF_precompute_mult(EC_GROUP *group, BN_CTX *);
330 int ec_wNAF_have_precompute_mult(const EC_GROUP *group);
331
332 /* method functions in ecp_smpl.c */
333 int ec_GFp_simple_group_init(EC_GROUP *);
334 void ec_GFp_simple_group_finish(EC_GROUP *);
335 void ec_GFp_simple_group_clear_finish(EC_GROUP *);
336 int ec_GFp_simple_group_copy(EC_GROUP *, const EC_GROUP *);
337 int ec_GFp_simple_group_set_curve(EC_GROUP *, const BIGNUM *p,
338                                   const BIGNUM *a, const BIGNUM *b, BN_CTX *);
339 int ec_GFp_simple_group_get_curve(const EC_GROUP *, BIGNUM *p, BIGNUM *a,
340                                   BIGNUM *b, BN_CTX *);
341 int ec_GFp_simple_group_get_degree(const EC_GROUP *);
342 int ec_GFp_simple_group_check_discriminant(const EC_GROUP *, BN_CTX *);
343 int ec_GFp_simple_point_init(EC_POINT *);
344 void ec_GFp_simple_point_finish(EC_POINT *);
345 void ec_GFp_simple_point_clear_finish(EC_POINT *);
346 int ec_GFp_simple_point_copy(EC_POINT *, const EC_POINT *);
347 int ec_GFp_simple_point_set_to_infinity(const EC_GROUP *, EC_POINT *);
348 int ec_GFp_simple_set_Jprojective_coordinates_GFp(const EC_GROUP *,
349                                                   EC_POINT *, const BIGNUM *x,
350                                                   const BIGNUM *y,
351                                                   const BIGNUM *z, BN_CTX *);
352 int ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP *,
353                                                   const EC_POINT *, BIGNUM *x,
354                                                   BIGNUM *y, BIGNUM *z,
355                                                   BN_CTX *);
356 int ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP *, EC_POINT *,
357                                                const BIGNUM *x,
358                                                const BIGNUM *y, BN_CTX *);
359 int ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP *,
360                                                const EC_POINT *, BIGNUM *x,
361                                                BIGNUM *y, BN_CTX *);
362 int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *, EC_POINT *,
363                                              const BIGNUM *x, int y_bit,
364                                              BN_CTX *);
365 size_t ec_GFp_simple_point2oct(const EC_GROUP *, const EC_POINT *,
366                                point_conversion_form_t form,
367                                unsigned char *buf, size_t len, BN_CTX *);
368 int ec_GFp_simple_oct2point(const EC_GROUP *, EC_POINT *,
369                             const unsigned char *buf, size_t len, BN_CTX *);
370 int ec_GFp_simple_add(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
371                       const EC_POINT *b, BN_CTX *);
372 int ec_GFp_simple_dbl(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
373                       BN_CTX *);
374 int ec_GFp_simple_invert(const EC_GROUP *, EC_POINT *, BN_CTX *);
375 int ec_GFp_simple_is_at_infinity(const EC_GROUP *, const EC_POINT *);
376 int ec_GFp_simple_is_on_curve(const EC_GROUP *, const EC_POINT *, BN_CTX *);
377 int ec_GFp_simple_cmp(const EC_GROUP *, const EC_POINT *a, const EC_POINT *b,
378                       BN_CTX *);
379 int ec_GFp_simple_make_affine(const EC_GROUP *, EC_POINT *, BN_CTX *);
380 int ec_GFp_simple_points_make_affine(const EC_GROUP *, size_t num,
381                                      EC_POINT *[], BN_CTX *);
382 int ec_GFp_simple_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
383                             const BIGNUM *b, BN_CTX *);
384 int ec_GFp_simple_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
385                             BN_CTX *);
386 int ec_GFp_simple_blind_coordinates(const EC_GROUP *group, EC_POINT *p,
387                                    BN_CTX *ctx);
388
389 /* method functions in ecp_mont.c */
390 int ec_GFp_mont_group_init(EC_GROUP *);
391 int ec_GFp_mont_group_set_curve(EC_GROUP *, const BIGNUM *p, const BIGNUM *a,
392                                 const BIGNUM *b, BN_CTX *);
393 void ec_GFp_mont_group_finish(EC_GROUP *);
394 void ec_GFp_mont_group_clear_finish(EC_GROUP *);
395 int ec_GFp_mont_group_copy(EC_GROUP *, const EC_GROUP *);
396 int ec_GFp_mont_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
397                           const BIGNUM *b, BN_CTX *);
398 int ec_GFp_mont_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
399                           BN_CTX *);
400 int ec_GFp_mont_field_encode(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
401                              BN_CTX *);
402 int ec_GFp_mont_field_decode(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
403                              BN_CTX *);
404 int ec_GFp_mont_field_set_to_one(const EC_GROUP *, BIGNUM *r, BN_CTX *);
405
406 /* method functions in ecp_nist.c */
407 int ec_GFp_nist_group_copy(EC_GROUP *dest, const EC_GROUP *src);
408 int ec_GFp_nist_group_set_curve(EC_GROUP *, const BIGNUM *p, const BIGNUM *a,
409                                 const BIGNUM *b, BN_CTX *);
410 int ec_GFp_nist_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
411                           const BIGNUM *b, BN_CTX *);
412 int ec_GFp_nist_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
413                           BN_CTX *);
414
415 /* method functions in ec2_smpl.c */
416 int ec_GF2m_simple_group_init(EC_GROUP *);
417 void ec_GF2m_simple_group_finish(EC_GROUP *);
418 void ec_GF2m_simple_group_clear_finish(EC_GROUP *);
419 int ec_GF2m_simple_group_copy(EC_GROUP *, const EC_GROUP *);
420 int ec_GF2m_simple_group_set_curve(EC_GROUP *, const BIGNUM *p,
421                                    const BIGNUM *a, const BIGNUM *b,
422                                    BN_CTX *);
423 int ec_GF2m_simple_group_get_curve(const EC_GROUP *, BIGNUM *p, BIGNUM *a,
424                                    BIGNUM *b, BN_CTX *);
425 int ec_GF2m_simple_group_get_degree(const EC_GROUP *);
426 int ec_GF2m_simple_group_check_discriminant(const EC_GROUP *, BN_CTX *);
427 int ec_GF2m_simple_point_init(EC_POINT *);
428 void ec_GF2m_simple_point_finish(EC_POINT *);
429 void ec_GF2m_simple_point_clear_finish(EC_POINT *);
430 int ec_GF2m_simple_point_copy(EC_POINT *, const EC_POINT *);
431 int ec_GF2m_simple_point_set_to_infinity(const EC_GROUP *, EC_POINT *);
432 int ec_GF2m_simple_point_set_affine_coordinates(const EC_GROUP *, EC_POINT *,
433                                                 const BIGNUM *x,
434                                                 const BIGNUM *y, BN_CTX *);
435 int ec_GF2m_simple_point_get_affine_coordinates(const EC_GROUP *,
436                                                 const EC_POINT *, BIGNUM *x,
437                                                 BIGNUM *y, BN_CTX *);
438 int ec_GF2m_simple_set_compressed_coordinates(const EC_GROUP *, EC_POINT *,
439                                               const BIGNUM *x, int y_bit,
440                                               BN_CTX *);
441 size_t ec_GF2m_simple_point2oct(const EC_GROUP *, const EC_POINT *,
442                                 point_conversion_form_t form,
443                                 unsigned char *buf, size_t len, BN_CTX *);
444 int ec_GF2m_simple_oct2point(const EC_GROUP *, EC_POINT *,
445                              const unsigned char *buf, size_t len, BN_CTX *);
446 int ec_GF2m_simple_add(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
447                        const EC_POINT *b, BN_CTX *);
448 int ec_GF2m_simple_dbl(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
449                        BN_CTX *);
450 int ec_GF2m_simple_invert(const EC_GROUP *, EC_POINT *, BN_CTX *);
451 int ec_GF2m_simple_is_at_infinity(const EC_GROUP *, const EC_POINT *);
452 int ec_GF2m_simple_is_on_curve(const EC_GROUP *, const EC_POINT *, BN_CTX *);
453 int ec_GF2m_simple_cmp(const EC_GROUP *, const EC_POINT *a, const EC_POINT *b,
454                        BN_CTX *);
455 int ec_GF2m_simple_make_affine(const EC_GROUP *, EC_POINT *, BN_CTX *);
456 int ec_GF2m_simple_points_make_affine(const EC_GROUP *, size_t num,
457                                       EC_POINT *[], BN_CTX *);
458 int ec_GF2m_simple_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
459                              const BIGNUM *b, BN_CTX *);
460 int ec_GF2m_simple_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
461                              BN_CTX *);
462 int ec_GF2m_simple_field_div(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
463                              const BIGNUM *b, BN_CTX *);
464
465 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
466 /* method functions in ecp_nistp224.c */
467 int ec_GFp_nistp224_group_init(EC_GROUP *group);
468 int ec_GFp_nistp224_group_set_curve(EC_GROUP *group, const BIGNUM *p,
469                                     const BIGNUM *a, const BIGNUM *n,
470                                     BN_CTX *);
471 int ec_GFp_nistp224_point_get_affine_coordinates(const EC_GROUP *group,
472                                                  const EC_POINT *point,
473                                                  BIGNUM *x, BIGNUM *y,
474                                                  BN_CTX *ctx);
475 int ec_GFp_nistp224_mul(const EC_GROUP *group, EC_POINT *r,
476                         const BIGNUM *scalar, size_t num,
477                         const EC_POINT *points[], const BIGNUM *scalars[],
478                         BN_CTX *);
479 int ec_GFp_nistp224_points_mul(const EC_GROUP *group, EC_POINT *r,
480                                const BIGNUM *scalar, size_t num,
481                                const EC_POINT *points[],
482                                const BIGNUM *scalars[], BN_CTX *ctx);
483 int ec_GFp_nistp224_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
484 int ec_GFp_nistp224_have_precompute_mult(const EC_GROUP *group);
485
486 /* method functions in ecp_nistp256.c */
487 int ec_GFp_nistp256_group_init(EC_GROUP *group);
488 int ec_GFp_nistp256_group_set_curve(EC_GROUP *group, const BIGNUM *p,
489                                     const BIGNUM *a, const BIGNUM *n,
490                                     BN_CTX *);
491 int ec_GFp_nistp256_point_get_affine_coordinates(const EC_GROUP *group,
492                                                  const EC_POINT *point,
493                                                  BIGNUM *x, BIGNUM *y,
494                                                  BN_CTX *ctx);
495 int ec_GFp_nistp256_mul(const EC_GROUP *group, EC_POINT *r,
496                         const BIGNUM *scalar, size_t num,
497                         const EC_POINT *points[], const BIGNUM *scalars[],
498                         BN_CTX *);
499 int ec_GFp_nistp256_points_mul(const EC_GROUP *group, EC_POINT *r,
500                                const BIGNUM *scalar, size_t num,
501                                const EC_POINT *points[],
502                                const BIGNUM *scalars[], BN_CTX *ctx);
503 int ec_GFp_nistp256_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
504 int ec_GFp_nistp256_have_precompute_mult(const EC_GROUP *group);
505
506 /* method functions in ecp_nistp521.c */
507 int ec_GFp_nistp521_group_init(EC_GROUP *group);
508 int ec_GFp_nistp521_group_set_curve(EC_GROUP *group, const BIGNUM *p,
509                                     const BIGNUM *a, const BIGNUM *n,
510                                     BN_CTX *);
511 int ec_GFp_nistp521_point_get_affine_coordinates(const EC_GROUP *group,
512                                                  const EC_POINT *point,
513                                                  BIGNUM *x, BIGNUM *y,
514                                                  BN_CTX *ctx);
515 int ec_GFp_nistp521_mul(const EC_GROUP *group, EC_POINT *r,
516                         const BIGNUM *scalar, size_t num,
517                         const EC_POINT *points[], const BIGNUM *scalars[],
518                         BN_CTX *);
519 int ec_GFp_nistp521_points_mul(const EC_GROUP *group, EC_POINT *r,
520                                const BIGNUM *scalar, size_t num,
521                                const EC_POINT *points[],
522                                const BIGNUM *scalars[], BN_CTX *ctx);
523 int ec_GFp_nistp521_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
524 int ec_GFp_nistp521_have_precompute_mult(const EC_GROUP *group);
525
526 /* utility functions in ecp_nistputil.c */
527 void ec_GFp_nistp_points_make_affine_internal(size_t num, void *point_array,
528                                               size_t felem_size,
529                                               void *tmp_felems,
530                                               void (*felem_one) (void *out),
531                                               int (*felem_is_zero) (const void
532                                                                     *in),
533                                               void (*felem_assign) (void *out,
534                                                                     const void
535                                                                     *in),
536                                               void (*felem_square) (void *out,
537                                                                     const void
538                                                                     *in),
539                                               void (*felem_mul) (void *out,
540                                                                  const void
541                                                                  *in1,
542                                                                  const void
543                                                                  *in2),
544                                               void (*felem_inv) (void *out,
545                                                                  const void
546                                                                  *in),
547                                               void (*felem_contract) (void
548                                                                       *out,
549                                                                       const
550                                                                       void
551                                                                       *in));
552 void ec_GFp_nistp_recode_scalar_bits(unsigned char *sign,
553                                      unsigned char *digit, unsigned char in);
554 #endif
555 int ec_group_simple_order_bits(const EC_GROUP *group);
556
557 #ifdef ECP_NISTZ256_ASM
558 /** Returns GFp methods using montgomery multiplication, with x86-64 optimized
559  * P256. See http://eprint.iacr.org/2013/816.
560  *  \return  EC_METHOD object
561  */
562 const EC_METHOD *EC_GFp_nistz256_method(void);
563 #endif
564
565 size_t ec_key_simple_priv2oct(const EC_KEY *eckey,
566                               unsigned char *buf, size_t len);
567 int ec_key_simple_oct2priv(EC_KEY *eckey, const unsigned char *buf, size_t len);
568 int ec_key_simple_generate_key(EC_KEY *eckey);
569 int ec_key_simple_generate_public_key(EC_KEY *eckey);
570 int ec_key_simple_check_key(const EC_KEY *eckey);
571
572 /* EC_METHOD definitions */
573
574 struct ec_key_method_st {
575     const char *name;
576     int32_t flags;
577     int (*init)(EC_KEY *key);
578     void (*finish)(EC_KEY *key);
579     int (*copy)(EC_KEY *dest, const EC_KEY *src);
580     int (*set_group)(EC_KEY *key, const EC_GROUP *grp);
581     int (*set_private)(EC_KEY *key, const BIGNUM *priv_key);
582     int (*set_public)(EC_KEY *key, const EC_POINT *pub_key);
583     int (*keygen)(EC_KEY *key);
584     int (*compute_key)(unsigned char **pout, size_t *poutlen,
585                        const EC_POINT *pub_key, const EC_KEY *ecdh);
586     int (*sign)(int type, const unsigned char *dgst, int dlen, unsigned char
587                 *sig, unsigned int *siglen, const BIGNUM *kinv,
588                 const BIGNUM *r, EC_KEY *eckey);
589     int (*sign_setup)(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
590                       BIGNUM **rp);
591     ECDSA_SIG *(*sign_sig)(const unsigned char *dgst, int dgst_len,
592                            const BIGNUM *in_kinv, const BIGNUM *in_r,
593                            EC_KEY *eckey);
594
595     int (*verify)(int type, const unsigned char *dgst, int dgst_len,
596                   const unsigned char *sigbuf, int sig_len, EC_KEY *eckey);
597     int (*verify_sig)(const unsigned char *dgst, int dgst_len,
598                       const ECDSA_SIG *sig, EC_KEY *eckey);
599 };
600
601 #define EC_KEY_METHOD_DYNAMIC   1
602
603 int ossl_ec_key_gen(EC_KEY *eckey);
604 int ossl_ecdh_compute_key(unsigned char **pout, size_t *poutlen,
605                           const EC_POINT *pub_key, const EC_KEY *ecdh);
606 int ecdh_simple_compute_key(unsigned char **pout, size_t *poutlen,
607                             const EC_POINT *pub_key, const EC_KEY *ecdh);
608
609 struct ECDSA_SIG_st {
610     BIGNUM *r;
611     BIGNUM *s;
612 };
613
614 int ossl_ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
615                           BIGNUM **rp);
616 int ossl_ecdsa_sign(int type, const unsigned char *dgst, int dlen,
617                     unsigned char *sig, unsigned int *siglen,
618                     const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey);
619 ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
620                                const BIGNUM *in_kinv, const BIGNUM *in_r,
621                                EC_KEY *eckey);
622 int ossl_ecdsa_verify(int type, const unsigned char *dgst, int dgst_len,
623                       const unsigned char *sigbuf, int sig_len, EC_KEY *eckey);
624 int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len,
625                           const ECDSA_SIG *sig, EC_KEY *eckey);
626
627 int ED25519_sign(uint8_t *out_sig, const uint8_t *message, size_t message_len,
628                  const uint8_t public_key[32], const uint8_t private_key[32]);
629 int ED25519_verify(const uint8_t *message, size_t message_len,
630                    const uint8_t signature[64], const uint8_t public_key[32]);
631 void ED25519_public_from_private(uint8_t out_public_key[32],
632                                  const uint8_t private_key[32]);
633
634 int X25519(uint8_t out_shared_key[32], const uint8_t private_key[32],
635            const uint8_t peer_public_value[32]);
636 void X25519_public_from_private(uint8_t out_public_value[32],
637                                 const uint8_t private_key[32]);
638
639 int EC_GROUP_do_inverse_ord(const EC_GROUP *group, BIGNUM *res,
640                             BIGNUM *x, BN_CTX *ctx);
641
642 int ec_point_blind_coordinates(const EC_GROUP *group, EC_POINT *p, BN_CTX *ctx);