Copyright consolidation 06/10
[openssl.git] / crypto / ec / ec_lcl.h
1 /*
2  * Originally written by Bodo Moeller for the OpenSSL project.
3  */
4 /* ====================================================================
5  * Copyright (c) 1998-2010 The OpenSSL Project.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  *
19  * 3. All advertising materials mentioning features or use of this
20  *    software must display the following acknowledgment:
21  *    "This product includes software developed by the OpenSSL Project
22  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
23  *
24  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25  *    endorse or promote products derived from this software without
26  *    prior written permission. For written permission, please contact
27  *    openssl-core@openssl.org.
28  *
29  * 5. Products derived from this software may not be called "OpenSSL"
30  *    nor may "OpenSSL" appear in their names without prior written
31  *    permission of the OpenSSL Project.
32  *
33  * 6. Redistributions of any form whatsoever must retain the following
34  *    acknowledgment:
35  *    "This product includes software developed by the OpenSSL Project
36  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
37  *
38  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
42  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49  * OF THE POSSIBILITY OF SUCH DAMAGE.
50  * ====================================================================
51  *
52  * This product includes cryptographic software written by Eric Young
53  * (eay@cryptsoft.com).  This product includes software written by Tim
54  * Hudson (tjh@cryptsoft.com).
55  *
56  */
57 /* ====================================================================
58  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
59  *
60  * Portions of the attached software ("Contribution") are developed by
61  * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
62  *
63  * The Contribution is licensed pursuant to the OpenSSL open source
64  * license provided above.
65  *
66  * The elliptic curve binary polynomial software is originally written by
67  * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
68  *
69  */
70
71 #include <stdlib.h>
72
73 #include <openssl/obj_mac.h>
74 #include <openssl/ec.h>
75 #include <openssl/bn.h>
76
77 #include "e_os.h"
78
79 #if defined(__SUNPRO_C)
80 # if __SUNPRO_C >= 0x520
81 #  pragma error_messages (off,E_ARRAY_OF_INCOMPLETE_NONAME,E_ARRAY_OF_INCOMPLETE)
82 # endif
83 #endif
84
85 /* Use default functions for poin2oct, oct2point and compressed coordinates */
86 #define EC_FLAGS_DEFAULT_OCT    0x1
87
88 /* Use custom formats for EC_GROUP, EC_POINT and EC_KEY */
89 #define EC_FLAGS_CUSTOM_CURVE   0x2
90
91 /* Curve does not support signing operations */
92 #define EC_FLAGS_NO_SIGN        0x4
93
94 /*
95  * Structure details are not part of the exported interface, so all this may
96  * change in future versions.
97  */
98
99 struct ec_method_st {
100     /* Various method flags */
101     int flags;
102     /* used by EC_METHOD_get_field_type: */
103     int field_type;             /* a NID */
104     /*
105      * used by EC_GROUP_new, EC_GROUP_free, EC_GROUP_clear_free,
106      * EC_GROUP_copy:
107      */
108     int (*group_init) (EC_GROUP *);
109     void (*group_finish) (EC_GROUP *);
110     void (*group_clear_finish) (EC_GROUP *);
111     int (*group_copy) (EC_GROUP *, const EC_GROUP *);
112     /* used by EC_GROUP_set_curve_GFp, EC_GROUP_get_curve_GFp, */
113     /* EC_GROUP_set_curve_GF2m, and EC_GROUP_get_curve_GF2m: */
114     int (*group_set_curve) (EC_GROUP *, const BIGNUM *p, const BIGNUM *a,
115                             const BIGNUM *b, BN_CTX *);
116     int (*group_get_curve) (const EC_GROUP *, BIGNUM *p, BIGNUM *a, BIGNUM *b,
117                             BN_CTX *);
118     /* used by EC_GROUP_get_degree: */
119     int (*group_get_degree) (const EC_GROUP *);
120     int (*group_order_bits) (const EC_GROUP *);
121     /* used by EC_GROUP_check: */
122     int (*group_check_discriminant) (const EC_GROUP *, BN_CTX *);
123     /*
124      * used by EC_POINT_new, EC_POINT_free, EC_POINT_clear_free,
125      * EC_POINT_copy:
126      */
127     int (*point_init) (EC_POINT *);
128     void (*point_finish) (EC_POINT *);
129     void (*point_clear_finish) (EC_POINT *);
130     int (*point_copy) (EC_POINT *, const EC_POINT *);
131     /*-
132      * used by EC_POINT_set_to_infinity,
133      * EC_POINT_set_Jprojective_coordinates_GFp,
134      * EC_POINT_get_Jprojective_coordinates_GFp,
135      * EC_POINT_set_affine_coordinates_GFp,     ..._GF2m,
136      * EC_POINT_get_affine_coordinates_GFp,     ..._GF2m,
137      * EC_POINT_set_compressed_coordinates_GFp, ..._GF2m:
138      */
139     int (*point_set_to_infinity) (const EC_GROUP *, EC_POINT *);
140     int (*point_set_Jprojective_coordinates_GFp) (const EC_GROUP *,
141                                                   EC_POINT *, const BIGNUM *x,
142                                                   const BIGNUM *y,
143                                                   const BIGNUM *z, BN_CTX *);
144     int (*point_get_Jprojective_coordinates_GFp) (const EC_GROUP *,
145                                                   const EC_POINT *, BIGNUM *x,
146                                                   BIGNUM *y, BIGNUM *z,
147                                                   BN_CTX *);
148     int (*point_set_affine_coordinates) (const EC_GROUP *, EC_POINT *,
149                                          const BIGNUM *x, const BIGNUM *y,
150                                          BN_CTX *);
151     int (*point_get_affine_coordinates) (const EC_GROUP *, const EC_POINT *,
152                                          BIGNUM *x, BIGNUM *y, BN_CTX *);
153     int (*point_set_compressed_coordinates) (const EC_GROUP *, EC_POINT *,
154                                              const BIGNUM *x, int y_bit,
155                                              BN_CTX *);
156     /* used by EC_POINT_point2oct, EC_POINT_oct2point: */
157     size_t (*point2oct) (const EC_GROUP *, const EC_POINT *,
158                          point_conversion_form_t form, unsigned char *buf,
159                          size_t len, BN_CTX *);
160     int (*oct2point) (const EC_GROUP *, EC_POINT *, const unsigned char *buf,
161                       size_t len, BN_CTX *);
162     /* used by EC_POINT_add, EC_POINT_dbl, ECP_POINT_invert: */
163     int (*add) (const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
164                 const EC_POINT *b, BN_CTX *);
165     int (*dbl) (const EC_GROUP *, EC_POINT *r, const EC_POINT *a, BN_CTX *);
166     int (*invert) (const EC_GROUP *, EC_POINT *, BN_CTX *);
167     /*
168      * used by EC_POINT_is_at_infinity, EC_POINT_is_on_curve, EC_POINT_cmp:
169      */
170     int (*is_at_infinity) (const EC_GROUP *, const EC_POINT *);
171     int (*is_on_curve) (const EC_GROUP *, const EC_POINT *, BN_CTX *);
172     int (*point_cmp) (const EC_GROUP *, const EC_POINT *a, const EC_POINT *b,
173                       BN_CTX *);
174     /* used by EC_POINT_make_affine, EC_POINTs_make_affine: */
175     int (*make_affine) (const EC_GROUP *, EC_POINT *, BN_CTX *);
176     int (*points_make_affine) (const EC_GROUP *, size_t num, EC_POINT *[],
177                                BN_CTX *);
178     /*
179      * used by EC_POINTs_mul, EC_POINT_mul, EC_POINT_precompute_mult,
180      * EC_POINT_have_precompute_mult (default implementations are used if the
181      * 'mul' pointer is 0):
182      */
183     int (*mul) (const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
184                 size_t num, const EC_POINT *points[], const BIGNUM *scalars[],
185                 BN_CTX *);
186     int (*precompute_mult) (EC_GROUP *group, BN_CTX *);
187     int (*have_precompute_mult) (const EC_GROUP *group);
188     /* internal functions */
189     /*
190      * 'field_mul', 'field_sqr', and 'field_div' can be used by 'add' and
191      * 'dbl' so that the same implementations of point operations can be used
192      * with different optimized implementations of expensive field
193      * operations:
194      */
195     int (*field_mul) (const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
196                       const BIGNUM *b, BN_CTX *);
197     int (*field_sqr) (const EC_GROUP *, BIGNUM *r, const BIGNUM *a, BN_CTX *);
198     int (*field_div) (const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
199                       const BIGNUM *b, BN_CTX *);
200     /* e.g. to Montgomery */
201     int (*field_encode) (const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
202                          BN_CTX *);
203     /* e.g. from Montgomery */
204     int (*field_decode) (const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
205                          BN_CTX *);
206     int (*field_set_to_one) (const EC_GROUP *, BIGNUM *r, BN_CTX *);
207     /* private key operations */
208     size_t (*priv2oct)(const EC_KEY *eckey, unsigned char *buf, size_t len);
209     int (*oct2priv)(EC_KEY *eckey, unsigned char *buf, size_t len);
210     int (*set_private)(EC_KEY *eckey, const BIGNUM *priv_key);
211     int (*keygen)(EC_KEY *eckey);
212     int (*keycheck)(const EC_KEY *eckey);
213     int (*keygenpub)(EC_KEY *eckey);
214     int (*keycopy)(EC_KEY *dst, const EC_KEY *src);
215     void (*keyfinish)(EC_KEY *eckey);
216     /* custom ECDH operation */
217     int (*ecdh_compute_key)(unsigned char **pout, size_t *poutlen,
218                             const EC_POINT *pub_key, const EC_KEY *ecdh);
219 } /* EC_METHOD */ ;
220
221 /*
222  * Types and functions to manipulate pre-computed values.
223  */
224 typedef struct nistp224_pre_comp_st NISTP224_PRE_COMP;
225 typedef struct nistp256_pre_comp_st NISTP256_PRE_COMP;
226 typedef struct nistp521_pre_comp_st NISTP521_PRE_COMP;
227 typedef struct nistz256_pre_comp_st NISTZ256_PRE_COMP;
228 typedef struct ec_pre_comp_st EC_PRE_COMP;
229
230 struct ec_group_st {
231     const EC_METHOD *meth;
232     EC_POINT *generator;        /* optional */
233     BIGNUM *order, *cofactor;
234     int curve_name;             /* optional NID for named curve */
235     int asn1_flag;              /* flag to control the asn1 encoding */
236     point_conversion_form_t asn1_form;
237     unsigned char *seed;        /* optional seed for parameters (appears in
238                                  * ASN1) */
239     size_t seed_len;
240     /*
241      * The following members are handled by the method functions, even if
242      * they appear generic
243      */
244     /*
245      * Field specification. For curves over GF(p), this is the modulus; for
246      * curves over GF(2^m), this is the irreducible polynomial defining the
247      * field.
248      */
249     BIGNUM *field;
250     /*
251      * Field specification for curves over GF(2^m). The irreducible f(t) is
252      * then of the form: t^poly[0] + t^poly[1] + ... + t^poly[k] where m =
253      * poly[0] > poly[1] > ... > poly[k] = 0. The array is terminated with
254      * poly[k+1]=-1. All elliptic curve irreducibles have at most 5 non-zero
255      * terms.
256      */
257     int poly[6];
258     /*
259      * Curve coefficients. (Here the assumption is that BIGNUMs can be used
260      * or abused for all kinds of fields, not just GF(p).) For characteristic
261      * > 3, the curve is defined by a Weierstrass equation of the form y^2 =
262      * x^3 + a*x + b. For characteristic 2, the curve is defined by an
263      * equation of the form y^2 + x*y = x^3 + a*x^2 + b.
264      */
265     BIGNUM *a, *b;
266     /* enable optimized point arithmetics for special case */
267     int a_is_minus3;
268     /* method-specific (e.g., Montgomery structure) */
269     void *field_data1;
270     /* method-specific */
271     void *field_data2;
272     /* method-specific */
273     int (*field_mod_func) (BIGNUM *, const BIGNUM *, const BIGNUM *,
274                            BN_CTX *);
275     /* data for ECDSA inverse */
276     BN_MONT_CTX *mont_data;
277
278     /* precomputed values for speed. */
279     enum {
280         pct_none,
281         pct_nistp224, pct_nistp256, pct_nistp521, pct_nistz256,
282         pct_ec } pre_comp_type;
283     union {
284         NISTP224_PRE_COMP *nistp224;
285         NISTP256_PRE_COMP *nistp256;
286         NISTP521_PRE_COMP *nistp521;
287         NISTZ256_PRE_COMP *nistz256;
288         EC_PRE_COMP *ec;
289     } pre_comp;
290 } /* EC_GROUP */ ;
291
292 #define SETPRECOMP(g, type, pre) \
293     g->pre_comp_type = pct_##type, g->pre_comp.type = pre
294 #define HAVEPRECOMP(g, type) \
295     g->pre_comp_type == pct_##type && g->pre_comp.type != NULL
296
297 struct ec_key_st {
298     const EC_KEY_METHOD *meth;
299     ENGINE *engine;
300     int version;
301     EC_GROUP *group;
302     EC_POINT *pub_key;
303     BIGNUM *priv_key;
304     /*
305      * Arbitrary extra data.
306      * For example in X25519 this contains the raw private key in a 32 byte
307      * buffer.
308      */
309     void *custom_data;
310     unsigned int enc_flag;
311     point_conversion_form_t conv_form;
312     int references;
313     int flags;
314     CRYPTO_EX_DATA ex_data;
315     CRYPTO_RWLOCK *lock;
316 } /* EC_KEY */ ;
317
318 struct ec_point_st {
319     const EC_METHOD *meth;
320     /*
321      * All members except 'meth' are handled by the method functions, even if
322      * they appear generic
323      */
324     BIGNUM *X;
325     BIGNUM *Y;
326     BIGNUM *Z;                  /* Jacobian projective coordinates: * (X, Y,
327                                  * Z) represents (X/Z^2, Y/Z^3) if Z != 0 */
328     int Z_is_one;               /* enable optimized point arithmetics for
329                                  * special case */
330     /*
331      * Arbitrary extra data.
332      * For example in X25519 this contains the public key in a 32 byte buffer.
333      */
334     void *custom_data;
335 } /* EC_POINT */ ;
336
337 NISTP224_PRE_COMP *EC_nistp224_pre_comp_dup(NISTP224_PRE_COMP *);
338 NISTP256_PRE_COMP *EC_nistp256_pre_comp_dup(NISTP256_PRE_COMP *);
339 NISTP521_PRE_COMP *EC_nistp521_pre_comp_dup(NISTP521_PRE_COMP *);
340 NISTZ256_PRE_COMP *EC_nistz256_pre_comp_dup(NISTZ256_PRE_COMP *);
341 NISTP256_PRE_COMP *EC_nistp256_pre_comp_dup(NISTP256_PRE_COMP *);
342 EC_PRE_COMP *EC_ec_pre_comp_dup(EC_PRE_COMP *);
343
344 void EC_pre_comp_free(EC_GROUP *group);
345 void EC_nistp224_pre_comp_free(NISTP224_PRE_COMP *);
346 void EC_nistp256_pre_comp_free(NISTP256_PRE_COMP *);
347 void EC_nistp521_pre_comp_free(NISTP521_PRE_COMP *);
348 void EC_nistz256_pre_comp_free(NISTZ256_PRE_COMP *);
349 void EC_ec_pre_comp_free(EC_PRE_COMP *);
350
351 /*
352  * method functions in ec_mult.c (ec_lib.c uses these as defaults if
353  * group->method->mul is 0)
354  */
355 int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
356                 size_t num, const EC_POINT *points[], const BIGNUM *scalars[],
357                 BN_CTX *);
358 int ec_wNAF_precompute_mult(EC_GROUP *group, BN_CTX *);
359 int ec_wNAF_have_precompute_mult(const EC_GROUP *group);
360
361 /* method functions in ecp_smpl.c */
362 int ec_GFp_simple_group_init(EC_GROUP *);
363 void ec_GFp_simple_group_finish(EC_GROUP *);
364 void ec_GFp_simple_group_clear_finish(EC_GROUP *);
365 int ec_GFp_simple_group_copy(EC_GROUP *, const EC_GROUP *);
366 int ec_GFp_simple_group_set_curve(EC_GROUP *, const BIGNUM *p,
367                                   const BIGNUM *a, const BIGNUM *b, BN_CTX *);
368 int ec_GFp_simple_group_get_curve(const EC_GROUP *, BIGNUM *p, BIGNUM *a,
369                                   BIGNUM *b, BN_CTX *);
370 int ec_GFp_simple_group_get_degree(const EC_GROUP *);
371 int ec_GFp_simple_group_check_discriminant(const EC_GROUP *, BN_CTX *);
372 int ec_GFp_simple_point_init(EC_POINT *);
373 void ec_GFp_simple_point_finish(EC_POINT *);
374 void ec_GFp_simple_point_clear_finish(EC_POINT *);
375 int ec_GFp_simple_point_copy(EC_POINT *, const EC_POINT *);
376 int ec_GFp_simple_point_set_to_infinity(const EC_GROUP *, EC_POINT *);
377 int ec_GFp_simple_set_Jprojective_coordinates_GFp(const EC_GROUP *,
378                                                   EC_POINT *, const BIGNUM *x,
379                                                   const BIGNUM *y,
380                                                   const BIGNUM *z, BN_CTX *);
381 int ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP *,
382                                                   const EC_POINT *, BIGNUM *x,
383                                                   BIGNUM *y, BIGNUM *z,
384                                                   BN_CTX *);
385 int ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP *, EC_POINT *,
386                                                const BIGNUM *x,
387                                                const BIGNUM *y, BN_CTX *);
388 int ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP *,
389                                                const EC_POINT *, BIGNUM *x,
390                                                BIGNUM *y, BN_CTX *);
391 int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *, EC_POINT *,
392                                              const BIGNUM *x, int y_bit,
393                                              BN_CTX *);
394 size_t ec_GFp_simple_point2oct(const EC_GROUP *, const EC_POINT *,
395                                point_conversion_form_t form,
396                                unsigned char *buf, size_t len, BN_CTX *);
397 int ec_GFp_simple_oct2point(const EC_GROUP *, EC_POINT *,
398                             const unsigned char *buf, size_t len, BN_CTX *);
399 int ec_GFp_simple_add(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
400                       const EC_POINT *b, BN_CTX *);
401 int ec_GFp_simple_dbl(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
402                       BN_CTX *);
403 int ec_GFp_simple_invert(const EC_GROUP *, EC_POINT *, BN_CTX *);
404 int ec_GFp_simple_is_at_infinity(const EC_GROUP *, const EC_POINT *);
405 int ec_GFp_simple_is_on_curve(const EC_GROUP *, const EC_POINT *, BN_CTX *);
406 int ec_GFp_simple_cmp(const EC_GROUP *, const EC_POINT *a, const EC_POINT *b,
407                       BN_CTX *);
408 int ec_GFp_simple_make_affine(const EC_GROUP *, EC_POINT *, BN_CTX *);
409 int ec_GFp_simple_points_make_affine(const EC_GROUP *, size_t num,
410                                      EC_POINT *[], BN_CTX *);
411 int ec_GFp_simple_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
412                             const BIGNUM *b, BN_CTX *);
413 int ec_GFp_simple_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
414                             BN_CTX *);
415
416 /* method functions in ecp_mont.c */
417 int ec_GFp_mont_group_init(EC_GROUP *);
418 int ec_GFp_mont_group_set_curve(EC_GROUP *, const BIGNUM *p, const BIGNUM *a,
419                                 const BIGNUM *b, BN_CTX *);
420 void ec_GFp_mont_group_finish(EC_GROUP *);
421 void ec_GFp_mont_group_clear_finish(EC_GROUP *);
422 int ec_GFp_mont_group_copy(EC_GROUP *, const EC_GROUP *);
423 int ec_GFp_mont_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
424                           const BIGNUM *b, BN_CTX *);
425 int ec_GFp_mont_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
426                           BN_CTX *);
427 int ec_GFp_mont_field_encode(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
428                              BN_CTX *);
429 int ec_GFp_mont_field_decode(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
430                              BN_CTX *);
431 int ec_GFp_mont_field_set_to_one(const EC_GROUP *, BIGNUM *r, BN_CTX *);
432
433 /* method functions in ecp_nist.c */
434 int ec_GFp_nist_group_copy(EC_GROUP *dest, const EC_GROUP *src);
435 int ec_GFp_nist_group_set_curve(EC_GROUP *, const BIGNUM *p, const BIGNUM *a,
436                                 const BIGNUM *b, BN_CTX *);
437 int ec_GFp_nist_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
438                           const BIGNUM *b, BN_CTX *);
439 int ec_GFp_nist_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
440                           BN_CTX *);
441
442 /* method functions in ec2_smpl.c */
443 int ec_GF2m_simple_group_init(EC_GROUP *);
444 void ec_GF2m_simple_group_finish(EC_GROUP *);
445 void ec_GF2m_simple_group_clear_finish(EC_GROUP *);
446 int ec_GF2m_simple_group_copy(EC_GROUP *, const EC_GROUP *);
447 int ec_GF2m_simple_group_set_curve(EC_GROUP *, const BIGNUM *p,
448                                    const BIGNUM *a, const BIGNUM *b,
449                                    BN_CTX *);
450 int ec_GF2m_simple_group_get_curve(const EC_GROUP *, BIGNUM *p, BIGNUM *a,
451                                    BIGNUM *b, BN_CTX *);
452 int ec_GF2m_simple_group_get_degree(const EC_GROUP *);
453 int ec_GF2m_simple_group_check_discriminant(const EC_GROUP *, BN_CTX *);
454 int ec_GF2m_simple_point_init(EC_POINT *);
455 void ec_GF2m_simple_point_finish(EC_POINT *);
456 void ec_GF2m_simple_point_clear_finish(EC_POINT *);
457 int ec_GF2m_simple_point_copy(EC_POINT *, const EC_POINT *);
458 int ec_GF2m_simple_point_set_to_infinity(const EC_GROUP *, EC_POINT *);
459 int ec_GF2m_simple_point_set_affine_coordinates(const EC_GROUP *, EC_POINT *,
460                                                 const BIGNUM *x,
461                                                 const BIGNUM *y, BN_CTX *);
462 int ec_GF2m_simple_point_get_affine_coordinates(const EC_GROUP *,
463                                                 const EC_POINT *, BIGNUM *x,
464                                                 BIGNUM *y, BN_CTX *);
465 int ec_GF2m_simple_set_compressed_coordinates(const EC_GROUP *, EC_POINT *,
466                                               const BIGNUM *x, int y_bit,
467                                               BN_CTX *);
468 size_t ec_GF2m_simple_point2oct(const EC_GROUP *, const EC_POINT *,
469                                 point_conversion_form_t form,
470                                 unsigned char *buf, size_t len, BN_CTX *);
471 int ec_GF2m_simple_oct2point(const EC_GROUP *, EC_POINT *,
472                              const unsigned char *buf, size_t len, BN_CTX *);
473 int ec_GF2m_simple_add(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
474                        const EC_POINT *b, BN_CTX *);
475 int ec_GF2m_simple_dbl(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
476                        BN_CTX *);
477 int ec_GF2m_simple_invert(const EC_GROUP *, EC_POINT *, BN_CTX *);
478 int ec_GF2m_simple_is_at_infinity(const EC_GROUP *, const EC_POINT *);
479 int ec_GF2m_simple_is_on_curve(const EC_GROUP *, const EC_POINT *, BN_CTX *);
480 int ec_GF2m_simple_cmp(const EC_GROUP *, const EC_POINT *a, const EC_POINT *b,
481                        BN_CTX *);
482 int ec_GF2m_simple_make_affine(const EC_GROUP *, EC_POINT *, BN_CTX *);
483 int ec_GF2m_simple_points_make_affine(const EC_GROUP *, size_t num,
484                                       EC_POINT *[], BN_CTX *);
485 int ec_GF2m_simple_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
486                              const BIGNUM *b, BN_CTX *);
487 int ec_GF2m_simple_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
488                              BN_CTX *);
489 int ec_GF2m_simple_field_div(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
490                              const BIGNUM *b, BN_CTX *);
491
492 /* method functions in ec2_mult.c */
493 int ec_GF2m_simple_mul(const EC_GROUP *group, EC_POINT *r,
494                        const BIGNUM *scalar, size_t num,
495                        const EC_POINT *points[], const BIGNUM *scalars[],
496                        BN_CTX *);
497 int ec_GF2m_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
498 int ec_GF2m_have_precompute_mult(const EC_GROUP *group);
499
500 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
501 /* method functions in ecp_nistp224.c */
502 int ec_GFp_nistp224_group_init(EC_GROUP *group);
503 int ec_GFp_nistp224_group_set_curve(EC_GROUP *group, const BIGNUM *p,
504                                     const BIGNUM *a, const BIGNUM *n,
505                                     BN_CTX *);
506 int ec_GFp_nistp224_point_get_affine_coordinates(const EC_GROUP *group,
507                                                  const EC_POINT *point,
508                                                  BIGNUM *x, BIGNUM *y,
509                                                  BN_CTX *ctx);
510 int ec_GFp_nistp224_mul(const EC_GROUP *group, EC_POINT *r,
511                         const BIGNUM *scalar, size_t num,
512                         const EC_POINT *points[], const BIGNUM *scalars[],
513                         BN_CTX *);
514 int ec_GFp_nistp224_points_mul(const EC_GROUP *group, EC_POINT *r,
515                                const BIGNUM *scalar, size_t num,
516                                const EC_POINT *points[],
517                                const BIGNUM *scalars[], BN_CTX *ctx);
518 int ec_GFp_nistp224_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
519 int ec_GFp_nistp224_have_precompute_mult(const EC_GROUP *group);
520
521 /* method functions in ecp_nistp256.c */
522 int ec_GFp_nistp256_group_init(EC_GROUP *group);
523 int ec_GFp_nistp256_group_set_curve(EC_GROUP *group, const BIGNUM *p,
524                                     const BIGNUM *a, const BIGNUM *n,
525                                     BN_CTX *);
526 int ec_GFp_nistp256_point_get_affine_coordinates(const EC_GROUP *group,
527                                                  const EC_POINT *point,
528                                                  BIGNUM *x, BIGNUM *y,
529                                                  BN_CTX *ctx);
530 int ec_GFp_nistp256_mul(const EC_GROUP *group, EC_POINT *r,
531                         const BIGNUM *scalar, size_t num,
532                         const EC_POINT *points[], const BIGNUM *scalars[],
533                         BN_CTX *);
534 int ec_GFp_nistp256_points_mul(const EC_GROUP *group, EC_POINT *r,
535                                const BIGNUM *scalar, size_t num,
536                                const EC_POINT *points[],
537                                const BIGNUM *scalars[], BN_CTX *ctx);
538 int ec_GFp_nistp256_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
539 int ec_GFp_nistp256_have_precompute_mult(const EC_GROUP *group);
540
541 /* method functions in ecp_nistp521.c */
542 int ec_GFp_nistp521_group_init(EC_GROUP *group);
543 int ec_GFp_nistp521_group_set_curve(EC_GROUP *group, const BIGNUM *p,
544                                     const BIGNUM *a, const BIGNUM *n,
545                                     BN_CTX *);
546 int ec_GFp_nistp521_point_get_affine_coordinates(const EC_GROUP *group,
547                                                  const EC_POINT *point,
548                                                  BIGNUM *x, BIGNUM *y,
549                                                  BN_CTX *ctx);
550 int ec_GFp_nistp521_mul(const EC_GROUP *group, EC_POINT *r,
551                         const BIGNUM *scalar, size_t num,
552                         const EC_POINT *points[], const BIGNUM *scalars[],
553                         BN_CTX *);
554 int ec_GFp_nistp521_points_mul(const EC_GROUP *group, EC_POINT *r,
555                                const BIGNUM *scalar, size_t num,
556                                const EC_POINT *points[],
557                                const BIGNUM *scalars[], BN_CTX *ctx);
558 int ec_GFp_nistp521_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
559 int ec_GFp_nistp521_have_precompute_mult(const EC_GROUP *group);
560
561 /* utility functions in ecp_nistputil.c */
562 void ec_GFp_nistp_points_make_affine_internal(size_t num, void *point_array,
563                                               size_t felem_size,
564                                               void *tmp_felems,
565                                               void (*felem_one) (void *out),
566                                               int (*felem_is_zero) (const void
567                                                                     *in),
568                                               void (*felem_assign) (void *out,
569                                                                     const void
570                                                                     *in),
571                                               void (*felem_square) (void *out,
572                                                                     const void
573                                                                     *in),
574                                               void (*felem_mul) (void *out,
575                                                                  const void
576                                                                  *in1,
577                                                                  const void
578                                                                  *in2),
579                                               void (*felem_inv) (void *out,
580                                                                  const void
581                                                                  *in),
582                                               void (*felem_contract) (void
583                                                                       *out,
584                                                                       const
585                                                                       void
586                                                                       *in));
587 void ec_GFp_nistp_recode_scalar_bits(unsigned char *sign,
588                                      unsigned char *digit, unsigned char in);
589 #endif
590 int ec_precompute_mont_data(EC_GROUP *);
591 int ec_group_simple_order_bits(const EC_GROUP *group);
592
593 #ifdef ECP_NISTZ256_ASM
594 /** Returns GFp methods using montgomery multiplication, with x86-64 optimized
595  * P256. See http://eprint.iacr.org/2013/816.
596  *  \return  EC_METHOD object
597  */
598 const EC_METHOD *EC_GFp_nistz256_method(void);
599 #endif
600
601 size_t ec_key_simple_priv2oct(const EC_KEY *eckey,
602                               unsigned char *buf, size_t len);
603 int ec_key_simple_oct2priv(EC_KEY *eckey, unsigned char *buf, size_t len);
604 int ec_key_simple_generate_key(EC_KEY *eckey);
605 int ec_key_simple_generate_public_key(EC_KEY *eckey);
606 int ec_key_simple_check_key(const EC_KEY *eckey);
607
608 /* EC_METHOD definitions */
609
610 struct ec_key_method_st {
611     const char *name;
612     int32_t flags;
613     int (*init)(EC_KEY *key);
614     void (*finish)(EC_KEY *key);
615     int (*copy)(EC_KEY *dest, const EC_KEY *src);
616     int (*set_group)(EC_KEY *key, const EC_GROUP *grp);
617     int (*set_private)(EC_KEY *key, const BIGNUM *priv_key);
618     int (*set_public)(EC_KEY *key, const EC_POINT *pub_key);
619     int (*keygen)(EC_KEY *key);
620     int (*compute_key)(unsigned char **pout, size_t *poutlen,
621                        const EC_POINT *pub_key, const EC_KEY *ecdh);
622     int (*sign)(int type, const unsigned char *dgst, int dlen, unsigned char
623                 *sig, unsigned int *siglen, const BIGNUM *kinv,
624                 const BIGNUM *r, EC_KEY *eckey);
625     int (*sign_setup)(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
626                       BIGNUM **rp);
627     ECDSA_SIG *(*sign_sig)(const unsigned char *dgst, int dgst_len,
628                            const BIGNUM *in_kinv, const BIGNUM *in_r,
629                            EC_KEY *eckey);
630
631     int (*verify)(int type, const unsigned char *dgst, int dgst_len,
632                   const unsigned char *sigbuf, int sig_len, EC_KEY *eckey);
633     int (*verify_sig)(const unsigned char *dgst, int dgst_len,
634                       const ECDSA_SIG *sig, EC_KEY *eckey);
635 } /* EC_KEY_METHOD */ ;
636
637 #define EC_KEY_METHOD_DYNAMIC   1
638
639 int ossl_ec_key_gen(EC_KEY *eckey);
640 int ossl_ecdh_compute_key(unsigned char **pout, size_t *poutlen,
641                           const EC_POINT *pub_key, const EC_KEY *ecdh);
642 int ecdh_simple_compute_key(unsigned char **pout, size_t *poutlen,
643                             const EC_POINT *pub_key, const EC_KEY *ecdh);
644
645 struct ECDSA_SIG_st {
646     BIGNUM *r;
647     BIGNUM *s;
648 };
649
650 int ossl_ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
651                           BIGNUM **rp);
652 int ossl_ecdsa_sign(int type, const unsigned char *dgst, int dlen,
653                     unsigned char *sig, unsigned int *siglen,
654                     const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey);
655 ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
656                                const BIGNUM *in_kinv, const BIGNUM *in_r,
657                                EC_KEY *eckey);
658 int ossl_ecdsa_verify(int type, const unsigned char *dgst, int dgst_len,
659                       const unsigned char *sigbuf, int sig_len, EC_KEY *eckey);
660 int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len,
661                           const ECDSA_SIG *sig, EC_KEY *eckey);
662
663 const EC_METHOD *ec_x25519_meth(void);
664
665 int X25519(uint8_t out_shared_key[32], const uint8_t private_key[32],
666            const uint8_t peer_public_value[32]);
667 void X25519_public_from_private(uint8_t out_public_value[32],
668                                 const uint8_t private_key[32]);