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