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