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