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