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