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