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