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