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