EC GFp ladder
[openssl.git] / crypto / ec / ec_lcl.h
1 /*
2  * Copyright 2001-2018 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4  *
5  * Licensed under the OpenSSL license (the "License").  You may not use
6  * this file except in compliance with the License.  You can obtain a copy
7  * in the file LICENSE in the source distribution or at
8  * https://www.openssl.org/source/license.html
9  */
10
11 #include <stdlib.h>
12
13 #include <openssl/obj_mac.h>
14 #include <openssl/ec.h>
15 #include <openssl/bn.h>
16 #include "internal/refcount.h"
17 #include "internal/ec_int.h"
18 #include "curve448/curve448_lcl.h"
19
20 #if defined(__SUNPRO_C)
21 # if __SUNPRO_C >= 0x520
22 #  pragma error_messages (off,E_ARRAY_OF_INCOMPLETE_NONAME,E_ARRAY_OF_INCOMPLETE)
23 # endif
24 #endif
25
26 /* Use default functions for poin2oct, oct2point and compressed coordinates */
27 #define EC_FLAGS_DEFAULT_OCT    0x1
28
29 /* Use custom formats for EC_GROUP, EC_POINT and EC_KEY */
30 #define EC_FLAGS_CUSTOM_CURVE   0x2
31
32 /* Curve does not support signing operations */
33 #define EC_FLAGS_NO_SIGN        0x4
34
35 /*
36  * Structure details are not part of the exported interface, so all this may
37  * change in future versions.
38  */
39
40 struct ec_method_st {
41     /* Various method flags */
42     int flags;
43     /* used by EC_METHOD_get_field_type: */
44     int field_type;             /* a NID */
45     /*
46      * used by EC_GROUP_new, EC_GROUP_free, EC_GROUP_clear_free,
47      * EC_GROUP_copy:
48      */
49     int (*group_init) (EC_GROUP *);
50     void (*group_finish) (EC_GROUP *);
51     void (*group_clear_finish) (EC_GROUP *);
52     int (*group_copy) (EC_GROUP *, const EC_GROUP *);
53     /* used by EC_GROUP_set_curve_GFp, EC_GROUP_get_curve_GFp, */
54     /* EC_GROUP_set_curve_GF2m, and EC_GROUP_get_curve_GF2m: */
55     int (*group_set_curve) (EC_GROUP *, const BIGNUM *p, const BIGNUM *a,
56                             const BIGNUM *b, BN_CTX *);
57     int (*group_get_curve) (const EC_GROUP *, BIGNUM *p, BIGNUM *a, BIGNUM *b,
58                             BN_CTX *);
59     /* used by EC_GROUP_get_degree: */
60     int (*group_get_degree) (const EC_GROUP *);
61     int (*group_order_bits) (const EC_GROUP *);
62     /* used by EC_GROUP_check: */
63     int (*group_check_discriminant) (const EC_GROUP *, BN_CTX *);
64     /*
65      * used by EC_POINT_new, EC_POINT_free, EC_POINT_clear_free,
66      * EC_POINT_copy:
67      */
68     int (*point_init) (EC_POINT *);
69     void (*point_finish) (EC_POINT *);
70     void (*point_clear_finish) (EC_POINT *);
71     int (*point_copy) (EC_POINT *, const EC_POINT *);
72     /*-
73      * used by EC_POINT_set_to_infinity,
74      * EC_POINT_set_Jprojective_coordinates_GFp,
75      * EC_POINT_get_Jprojective_coordinates_GFp,
76      * EC_POINT_set_affine_coordinates_GFp,     ..._GF2m,
77      * EC_POINT_get_affine_coordinates_GFp,     ..._GF2m,
78      * EC_POINT_set_compressed_coordinates_GFp, ..._GF2m:
79      */
80     int (*point_set_to_infinity) (const EC_GROUP *, EC_POINT *);
81     int (*point_set_Jprojective_coordinates_GFp) (const EC_GROUP *,
82                                                   EC_POINT *, const BIGNUM *x,
83                                                   const BIGNUM *y,
84                                                   const BIGNUM *z, BN_CTX *);
85     int (*point_get_Jprojective_coordinates_GFp) (const EC_GROUP *,
86                                                   const EC_POINT *, BIGNUM *x,
87                                                   BIGNUM *y, BIGNUM *z,
88                                                   BN_CTX *);
89     int (*point_set_affine_coordinates) (const EC_GROUP *, EC_POINT *,
90                                          const BIGNUM *x, const BIGNUM *y,
91                                          BN_CTX *);
92     int (*point_get_affine_coordinates) (const EC_GROUP *, const EC_POINT *,
93                                          BIGNUM *x, BIGNUM *y, BN_CTX *);
94     int (*point_set_compressed_coordinates) (const EC_GROUP *, EC_POINT *,
95                                              const BIGNUM *x, int y_bit,
96                                              BN_CTX *);
97     /* used by EC_POINT_point2oct, EC_POINT_oct2point: */
98     size_t (*point2oct) (const EC_GROUP *, const EC_POINT *,
99                          point_conversion_form_t form, unsigned char *buf,
100                          size_t len, BN_CTX *);
101     int (*oct2point) (const EC_GROUP *, EC_POINT *, const unsigned char *buf,
102                       size_t len, BN_CTX *);
103     /* used by EC_POINT_add, EC_POINT_dbl, ECP_POINT_invert: */
104     int (*add) (const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
105                 const EC_POINT *b, BN_CTX *);
106     int (*dbl) (const EC_GROUP *, EC_POINT *r, const EC_POINT *a, BN_CTX *);
107     int (*invert) (const EC_GROUP *, EC_POINT *, BN_CTX *);
108     /*
109      * used by EC_POINT_is_at_infinity, EC_POINT_is_on_curve, EC_POINT_cmp:
110      */
111     int (*is_at_infinity) (const EC_GROUP *, const EC_POINT *);
112     int (*is_on_curve) (const EC_GROUP *, const EC_POINT *, BN_CTX *);
113     int (*point_cmp) (const EC_GROUP *, const EC_POINT *a, const EC_POINT *b,
114                       BN_CTX *);
115     /* used by EC_POINT_make_affine, EC_POINTs_make_affine: */
116     int (*make_affine) (const EC_GROUP *, EC_POINT *, BN_CTX *);
117     int (*points_make_affine) (const EC_GROUP *, size_t num, EC_POINT *[],
118                                BN_CTX *);
119     /*
120      * used by EC_POINTs_mul, EC_POINT_mul, EC_POINT_precompute_mult,
121      * EC_POINT_have_precompute_mult (default implementations are used if the
122      * 'mul' pointer is 0):
123      */
124     /*-
125      * mul() calculates the value
126      *
127      *   r := generator * scalar
128      *        + points[0] * scalars[0]
129      *        + ...
130      *        + points[num-1] * scalars[num-1].
131      *
132      * For a fixed point multiplication (scalar != NULL, num == 0)
133      * or a variable point multiplication (scalar == NULL, num == 1),
134      * mul() must use a constant time algorithm: in both cases callers
135      * should provide an input scalar (either scalar or scalars[0])
136      * in the range [0, ec_group_order); for robustness, implementers
137      * should handle the case when the scalar has not been reduced, but
138      * may treat it as an unusual input, without any constant-timeness
139      * guarantee.
140      */
141     int (*mul) (const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
142                 size_t num, const EC_POINT *points[], const BIGNUM *scalars[],
143                 BN_CTX *);
144     int (*precompute_mult) (EC_GROUP *group, BN_CTX *);
145     int (*have_precompute_mult) (const EC_GROUP *group);
146     /* internal functions */
147     /*
148      * 'field_mul', 'field_sqr', and 'field_div' can be used by 'add' and
149      * 'dbl' so that the same implementations of point operations can be used
150      * with different optimized implementations of expensive field
151      * operations:
152      */
153     int (*field_mul) (const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
154                       const BIGNUM *b, BN_CTX *);
155     int (*field_sqr) (const EC_GROUP *, BIGNUM *r, const BIGNUM *a, BN_CTX *);
156     int (*field_div) (const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
157                       const BIGNUM *b, BN_CTX *);
158     /* e.g. to Montgomery */
159     int (*field_encode) (const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
160                          BN_CTX *);
161     /* e.g. from Montgomery */
162     int (*field_decode) (const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
163                          BN_CTX *);
164     int (*field_set_to_one) (const EC_GROUP *, BIGNUM *r, BN_CTX *);
165     /* private key operations */
166     size_t (*priv2oct)(const EC_KEY *eckey, unsigned char *buf, size_t len);
167     int (*oct2priv)(EC_KEY *eckey, const unsigned char *buf, size_t len);
168     int (*set_private)(EC_KEY *eckey, const BIGNUM *priv_key);
169     int (*keygen)(EC_KEY *eckey);
170     int (*keycheck)(const EC_KEY *eckey);
171     int (*keygenpub)(EC_KEY *eckey);
172     int (*keycopy)(EC_KEY *dst, const EC_KEY *src);
173     void (*keyfinish)(EC_KEY *eckey);
174     /* custom ECDH operation */
175     int (*ecdh_compute_key)(unsigned char **pout, size_t *poutlen,
176                             const EC_POINT *pub_key, const EC_KEY *ecdh);
177     /* Inverse modulo order */
178     int (*field_inverse_mod_ord)(const EC_GROUP *, BIGNUM *r,
179                                  const BIGNUM *x, BN_CTX *);
180     int (*blind_coordinates)(const EC_GROUP *group, EC_POINT *p, BN_CTX *ctx);
181     int (*ladder_pre)(const EC_GROUP *group,
182                       EC_POINT *r, EC_POINT *s,
183                       EC_POINT *p, BN_CTX *ctx);
184     int (*ladder_step)(const EC_GROUP *group,
185                        EC_POINT *r, EC_POINT *s,
186                        EC_POINT *p, BN_CTX *ctx);
187     int (*ladder_post)(const EC_GROUP *group,
188                        EC_POINT *r, EC_POINT *s,
189                        EC_POINT *p, BN_CTX *ctx);
190 };
191
192 /*
193  * Types and functions to manipulate pre-computed values.
194  */
195 typedef struct nistp224_pre_comp_st NISTP224_PRE_COMP;
196 typedef struct nistp256_pre_comp_st NISTP256_PRE_COMP;
197 typedef struct nistp521_pre_comp_st NISTP521_PRE_COMP;
198 typedef struct nistz256_pre_comp_st NISTZ256_PRE_COMP;
199 typedef struct ec_pre_comp_st EC_PRE_COMP;
200
201 struct ec_group_st {
202     const EC_METHOD *meth;
203     EC_POINT *generator;        /* optional */
204     BIGNUM *order, *cofactor;
205     int curve_name;             /* optional NID for named curve */
206     int asn1_flag;              /* flag to control the asn1 encoding */
207     point_conversion_form_t asn1_form;
208     unsigned char *seed;        /* optional seed for parameters (appears in
209                                  * ASN1) */
210     size_t seed_len;
211     /*
212      * The following members are handled by the method functions, even if
213      * they appear generic
214      */
215     /*
216      * Field specification. For curves over GF(p), this is the modulus; for
217      * curves over GF(2^m), this is the irreducible polynomial defining the
218      * field.
219      */
220     BIGNUM *field;
221     /*
222      * Field specification for curves over GF(2^m). The irreducible f(t) is
223      * then of the form: t^poly[0] + t^poly[1] + ... + t^poly[k] where m =
224      * poly[0] > poly[1] > ... > poly[k] = 0. The array is terminated with
225      * poly[k+1]=-1. All elliptic curve irreducibles have at most 5 non-zero
226      * terms.
227      */
228     int poly[6];
229     /*
230      * Curve coefficients. (Here the assumption is that BIGNUMs can be used
231      * or abused for all kinds of fields, not just GF(p).) For characteristic
232      * > 3, the curve is defined by a Weierstrass equation of the form y^2 =
233      * x^3 + a*x + b. For characteristic 2, the curve is defined by an
234      * equation of the form y^2 + x*y = x^3 + a*x^2 + b.
235      */
236     BIGNUM *a, *b;
237     /* enable optimized point arithmetics for special case */
238     int a_is_minus3;
239     /* method-specific (e.g., Montgomery structure) */
240     void *field_data1;
241     /* method-specific */
242     void *field_data2;
243     /* method-specific */
244     int (*field_mod_func) (BIGNUM *, const BIGNUM *, const BIGNUM *,
245                            BN_CTX *);
246     /* data for ECDSA inverse */
247     BN_MONT_CTX *mont_data;
248
249     /*
250      * Precomputed values for speed. The PCT_xxx names match the
251      * pre_comp.xxx union names; see the SETPRECOMP and HAVEPRECOMP
252      * macros, below.
253      */
254     enum {
255         PCT_none,
256         PCT_nistp224, PCT_nistp256, PCT_nistp521, PCT_nistz256,
257         PCT_ec
258     } pre_comp_type;
259     union {
260         NISTP224_PRE_COMP *nistp224;
261         NISTP256_PRE_COMP *nistp256;
262         NISTP521_PRE_COMP *nistp521;
263         NISTZ256_PRE_COMP *nistz256;
264         EC_PRE_COMP *ec;
265     } pre_comp;
266 };
267
268 #define SETPRECOMP(g, type, pre) \
269     g->pre_comp_type = PCT_##type, g->pre_comp.type = pre
270 #define HAVEPRECOMP(g, type) \
271     g->pre_comp_type == PCT_##type && g->pre_comp.type != NULL
272
273 struct ec_key_st {
274     const EC_KEY_METHOD *meth;
275     ENGINE *engine;
276     int version;
277     EC_GROUP *group;
278     EC_POINT *pub_key;
279     BIGNUM *priv_key;
280     unsigned int enc_flag;
281     point_conversion_form_t conv_form;
282     CRYPTO_REF_COUNT references;
283     int flags;
284     CRYPTO_EX_DATA ex_data;
285     CRYPTO_RWLOCK *lock;
286 };
287
288 struct ec_point_st {
289     const EC_METHOD *meth;
290     /* NID for the curve if known */
291     int curve_name;
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 };
303
304 static ossl_inline int ec_point_is_compat(const EC_POINT *point,
305                                           const EC_GROUP *group)
306 {
307     if (group->meth != point->meth
308         || (group->curve_name != 0
309             && point->curve_name != 0
310             && group->curve_name != point->curve_name))
311         return 0;
312
313     return 1;
314 }
315
316 NISTP224_PRE_COMP *EC_nistp224_pre_comp_dup(NISTP224_PRE_COMP *);
317 NISTP256_PRE_COMP *EC_nistp256_pre_comp_dup(NISTP256_PRE_COMP *);
318 NISTP521_PRE_COMP *EC_nistp521_pre_comp_dup(NISTP521_PRE_COMP *);
319 NISTZ256_PRE_COMP *EC_nistz256_pre_comp_dup(NISTZ256_PRE_COMP *);
320 NISTP256_PRE_COMP *EC_nistp256_pre_comp_dup(NISTP256_PRE_COMP *);
321 EC_PRE_COMP *EC_ec_pre_comp_dup(EC_PRE_COMP *);
322
323 void EC_pre_comp_free(EC_GROUP *group);
324 void EC_nistp224_pre_comp_free(NISTP224_PRE_COMP *);
325 void EC_nistp256_pre_comp_free(NISTP256_PRE_COMP *);
326 void EC_nistp521_pre_comp_free(NISTP521_PRE_COMP *);
327 void EC_nistz256_pre_comp_free(NISTZ256_PRE_COMP *);
328 void EC_ec_pre_comp_free(EC_PRE_COMP *);
329
330 /*
331  * method functions in ec_mult.c (ec_lib.c uses these as defaults if
332  * group->method->mul is 0)
333  */
334 int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
335                 size_t num, const EC_POINT *points[], const BIGNUM *scalars[],
336                 BN_CTX *);
337 int ec_wNAF_precompute_mult(EC_GROUP *group, BN_CTX *);
338 int ec_wNAF_have_precompute_mult(const EC_GROUP *group);
339
340 /* method functions in ecp_smpl.c */
341 int ec_GFp_simple_group_init(EC_GROUP *);
342 void ec_GFp_simple_group_finish(EC_GROUP *);
343 void ec_GFp_simple_group_clear_finish(EC_GROUP *);
344 int ec_GFp_simple_group_copy(EC_GROUP *, const EC_GROUP *);
345 int ec_GFp_simple_group_set_curve(EC_GROUP *, const BIGNUM *p,
346                                   const BIGNUM *a, const BIGNUM *b, BN_CTX *);
347 int ec_GFp_simple_group_get_curve(const EC_GROUP *, BIGNUM *p, BIGNUM *a,
348                                   BIGNUM *b, BN_CTX *);
349 int ec_GFp_simple_group_get_degree(const EC_GROUP *);
350 int ec_GFp_simple_group_check_discriminant(const EC_GROUP *, BN_CTX *);
351 int ec_GFp_simple_point_init(EC_POINT *);
352 void ec_GFp_simple_point_finish(EC_POINT *);
353 void ec_GFp_simple_point_clear_finish(EC_POINT *);
354 int ec_GFp_simple_point_copy(EC_POINT *, const EC_POINT *);
355 int ec_GFp_simple_point_set_to_infinity(const EC_GROUP *, EC_POINT *);
356 int ec_GFp_simple_set_Jprojective_coordinates_GFp(const EC_GROUP *,
357                                                   EC_POINT *, const BIGNUM *x,
358                                                   const BIGNUM *y,
359                                                   const BIGNUM *z, BN_CTX *);
360 int ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP *,
361                                                   const EC_POINT *, BIGNUM *x,
362                                                   BIGNUM *y, BIGNUM *z,
363                                                   BN_CTX *);
364 int ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP *, EC_POINT *,
365                                                const BIGNUM *x,
366                                                const BIGNUM *y, BN_CTX *);
367 int ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP *,
368                                                const EC_POINT *, BIGNUM *x,
369                                                BIGNUM *y, BN_CTX *);
370 int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *, EC_POINT *,
371                                              const BIGNUM *x, int y_bit,
372                                              BN_CTX *);
373 size_t ec_GFp_simple_point2oct(const EC_GROUP *, const EC_POINT *,
374                                point_conversion_form_t form,
375                                unsigned char *buf, size_t len, BN_CTX *);
376 int ec_GFp_simple_oct2point(const EC_GROUP *, EC_POINT *,
377                             const unsigned char *buf, size_t len, BN_CTX *);
378 int ec_GFp_simple_add(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
379                       const EC_POINT *b, BN_CTX *);
380 int ec_GFp_simple_dbl(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
381                       BN_CTX *);
382 int ec_GFp_simple_invert(const EC_GROUP *, EC_POINT *, BN_CTX *);
383 int ec_GFp_simple_is_at_infinity(const EC_GROUP *, const EC_POINT *);
384 int ec_GFp_simple_is_on_curve(const EC_GROUP *, const EC_POINT *, BN_CTX *);
385 int ec_GFp_simple_cmp(const EC_GROUP *, const EC_POINT *a, const EC_POINT *b,
386                       BN_CTX *);
387 int ec_GFp_simple_make_affine(const EC_GROUP *, EC_POINT *, BN_CTX *);
388 int ec_GFp_simple_points_make_affine(const EC_GROUP *, size_t num,
389                                      EC_POINT *[], BN_CTX *);
390 int ec_GFp_simple_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
391                             const BIGNUM *b, BN_CTX *);
392 int ec_GFp_simple_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
393                             BN_CTX *);
394 int ec_GFp_simple_blind_coordinates(const EC_GROUP *group, EC_POINT *p,
395                                     BN_CTX *ctx);
396 int ec_GFp_simple_ladder_pre(const EC_GROUP *group,
397                              EC_POINT *r, EC_POINT *s,
398                              EC_POINT *p, BN_CTX *ctx);
399 int ec_GFp_simple_ladder_step(const EC_GROUP *group,
400                               EC_POINT *r, EC_POINT *s,
401                               EC_POINT *p, BN_CTX *ctx);
402 int ec_GFp_simple_ladder_post(const EC_GROUP *group,
403                               EC_POINT *r, EC_POINT *s,
404                               EC_POINT *p, BN_CTX *ctx);
405
406 /* method functions in ecp_mont.c */
407 int ec_GFp_mont_group_init(EC_GROUP *);
408 int ec_GFp_mont_group_set_curve(EC_GROUP *, const BIGNUM *p, const BIGNUM *a,
409                                 const BIGNUM *b, BN_CTX *);
410 void ec_GFp_mont_group_finish(EC_GROUP *);
411 void ec_GFp_mont_group_clear_finish(EC_GROUP *);
412 int ec_GFp_mont_group_copy(EC_GROUP *, const EC_GROUP *);
413 int ec_GFp_mont_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
414                           const BIGNUM *b, BN_CTX *);
415 int ec_GFp_mont_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
416                           BN_CTX *);
417 int ec_GFp_mont_field_encode(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
418                              BN_CTX *);
419 int ec_GFp_mont_field_decode(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
420                              BN_CTX *);
421 int ec_GFp_mont_field_set_to_one(const EC_GROUP *, BIGNUM *r, BN_CTX *);
422
423 /* method functions in ecp_nist.c */
424 int ec_GFp_nist_group_copy(EC_GROUP *dest, const EC_GROUP *src);
425 int ec_GFp_nist_group_set_curve(EC_GROUP *, const BIGNUM *p, const BIGNUM *a,
426                                 const BIGNUM *b, BN_CTX *);
427 int ec_GFp_nist_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
428                           const BIGNUM *b, BN_CTX *);
429 int ec_GFp_nist_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
430                           BN_CTX *);
431
432 /* method functions in ec2_smpl.c */
433 int ec_GF2m_simple_group_init(EC_GROUP *);
434 void ec_GF2m_simple_group_finish(EC_GROUP *);
435 void ec_GF2m_simple_group_clear_finish(EC_GROUP *);
436 int ec_GF2m_simple_group_copy(EC_GROUP *, const EC_GROUP *);
437 int ec_GF2m_simple_group_set_curve(EC_GROUP *, const BIGNUM *p,
438                                    const BIGNUM *a, const BIGNUM *b,
439                                    BN_CTX *);
440 int ec_GF2m_simple_group_get_curve(const EC_GROUP *, BIGNUM *p, BIGNUM *a,
441                                    BIGNUM *b, BN_CTX *);
442 int ec_GF2m_simple_group_get_degree(const EC_GROUP *);
443 int ec_GF2m_simple_group_check_discriminant(const EC_GROUP *, BN_CTX *);
444 int ec_GF2m_simple_point_init(EC_POINT *);
445 void ec_GF2m_simple_point_finish(EC_POINT *);
446 void ec_GF2m_simple_point_clear_finish(EC_POINT *);
447 int ec_GF2m_simple_point_copy(EC_POINT *, const EC_POINT *);
448 int ec_GF2m_simple_point_set_to_infinity(const EC_GROUP *, EC_POINT *);
449 int ec_GF2m_simple_point_set_affine_coordinates(const EC_GROUP *, EC_POINT *,
450                                                 const BIGNUM *x,
451                                                 const BIGNUM *y, BN_CTX *);
452 int ec_GF2m_simple_point_get_affine_coordinates(const EC_GROUP *,
453                                                 const EC_POINT *, BIGNUM *x,
454                                                 BIGNUM *y, BN_CTX *);
455 int ec_GF2m_simple_set_compressed_coordinates(const EC_GROUP *, EC_POINT *,
456                                               const BIGNUM *x, int y_bit,
457                                               BN_CTX *);
458 size_t ec_GF2m_simple_point2oct(const EC_GROUP *, const EC_POINT *,
459                                 point_conversion_form_t form,
460                                 unsigned char *buf, size_t len, BN_CTX *);
461 int ec_GF2m_simple_oct2point(const EC_GROUP *, EC_POINT *,
462                              const unsigned char *buf, size_t len, BN_CTX *);
463 int ec_GF2m_simple_add(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
464                        const EC_POINT *b, BN_CTX *);
465 int ec_GF2m_simple_dbl(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
466                        BN_CTX *);
467 int ec_GF2m_simple_invert(const EC_GROUP *, EC_POINT *, BN_CTX *);
468 int ec_GF2m_simple_is_at_infinity(const EC_GROUP *, const EC_POINT *);
469 int ec_GF2m_simple_is_on_curve(const EC_GROUP *, const EC_POINT *, BN_CTX *);
470 int ec_GF2m_simple_cmp(const EC_GROUP *, const EC_POINT *a, const EC_POINT *b,
471                        BN_CTX *);
472 int ec_GF2m_simple_make_affine(const EC_GROUP *, EC_POINT *, BN_CTX *);
473 int ec_GF2m_simple_points_make_affine(const EC_GROUP *, size_t num,
474                                       EC_POINT *[], BN_CTX *);
475 int ec_GF2m_simple_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
476                              const BIGNUM *b, BN_CTX *);
477 int ec_GF2m_simple_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
478                              BN_CTX *);
479 int ec_GF2m_simple_field_div(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
480                              const BIGNUM *b, BN_CTX *);
481
482 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
483 /* method functions in ecp_nistp224.c */
484 int ec_GFp_nistp224_group_init(EC_GROUP *group);
485 int ec_GFp_nistp224_group_set_curve(EC_GROUP *group, const BIGNUM *p,
486                                     const BIGNUM *a, const BIGNUM *n,
487                                     BN_CTX *);
488 int ec_GFp_nistp224_point_get_affine_coordinates(const EC_GROUP *group,
489                                                  const EC_POINT *point,
490                                                  BIGNUM *x, BIGNUM *y,
491                                                  BN_CTX *ctx);
492 int ec_GFp_nistp224_mul(const EC_GROUP *group, EC_POINT *r,
493                         const BIGNUM *scalar, size_t num,
494                         const EC_POINT *points[], const BIGNUM *scalars[],
495                         BN_CTX *);
496 int ec_GFp_nistp224_points_mul(const EC_GROUP *group, EC_POINT *r,
497                                const BIGNUM *scalar, size_t num,
498                                const EC_POINT *points[],
499                                const BIGNUM *scalars[], BN_CTX *ctx);
500 int ec_GFp_nistp224_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
501 int ec_GFp_nistp224_have_precompute_mult(const EC_GROUP *group);
502
503 /* method functions in ecp_nistp256.c */
504 int ec_GFp_nistp256_group_init(EC_GROUP *group);
505 int ec_GFp_nistp256_group_set_curve(EC_GROUP *group, const BIGNUM *p,
506                                     const BIGNUM *a, const BIGNUM *n,
507                                     BN_CTX *);
508 int ec_GFp_nistp256_point_get_affine_coordinates(const EC_GROUP *group,
509                                                  const EC_POINT *point,
510                                                  BIGNUM *x, BIGNUM *y,
511                                                  BN_CTX *ctx);
512 int ec_GFp_nistp256_mul(const EC_GROUP *group, EC_POINT *r,
513                         const BIGNUM *scalar, size_t num,
514                         const EC_POINT *points[], const BIGNUM *scalars[],
515                         BN_CTX *);
516 int ec_GFp_nistp256_points_mul(const EC_GROUP *group, EC_POINT *r,
517                                const BIGNUM *scalar, size_t num,
518                                const EC_POINT *points[],
519                                const BIGNUM *scalars[], BN_CTX *ctx);
520 int ec_GFp_nistp256_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
521 int ec_GFp_nistp256_have_precompute_mult(const EC_GROUP *group);
522
523 /* method functions in ecp_nistp521.c */
524 int ec_GFp_nistp521_group_init(EC_GROUP *group);
525 int ec_GFp_nistp521_group_set_curve(EC_GROUP *group, const BIGNUM *p,
526                                     const BIGNUM *a, const BIGNUM *n,
527                                     BN_CTX *);
528 int ec_GFp_nistp521_point_get_affine_coordinates(const EC_GROUP *group,
529                                                  const EC_POINT *point,
530                                                  BIGNUM *x, BIGNUM *y,
531                                                  BN_CTX *ctx);
532 int ec_GFp_nistp521_mul(const EC_GROUP *group, EC_POINT *r,
533                         const BIGNUM *scalar, size_t num,
534                         const EC_POINT *points[], const BIGNUM *scalars[],
535                         BN_CTX *);
536 int ec_GFp_nistp521_points_mul(const EC_GROUP *group, EC_POINT *r,
537                                const BIGNUM *scalar, size_t num,
538                                const EC_POINT *points[],
539                                const BIGNUM *scalars[], BN_CTX *ctx);
540 int ec_GFp_nistp521_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
541 int ec_GFp_nistp521_have_precompute_mult(const EC_GROUP *group);
542
543 /* utility functions in ecp_nistputil.c */
544 void ec_GFp_nistp_points_make_affine_internal(size_t num, void *point_array,
545                                               size_t felem_size,
546                                               void *tmp_felems,
547                                               void (*felem_one) (void *out),
548                                               int (*felem_is_zero) (const void
549                                                                     *in),
550                                               void (*felem_assign) (void *out,
551                                                                     const void
552                                                                     *in),
553                                               void (*felem_square) (void *out,
554                                                                     const void
555                                                                     *in),
556                                               void (*felem_mul) (void *out,
557                                                                  const void
558                                                                  *in1,
559                                                                  const void
560                                                                  *in2),
561                                               void (*felem_inv) (void *out,
562                                                                  const void
563                                                                  *in),
564                                               void (*felem_contract) (void
565                                                                       *out,
566                                                                       const
567                                                                       void
568                                                                       *in));
569 void ec_GFp_nistp_recode_scalar_bits(unsigned char *sign,
570                                      unsigned char *digit, unsigned char in);
571 #endif
572 int ec_group_simple_order_bits(const EC_GROUP *group);
573
574 #ifdef ECP_NISTZ256_ASM
575 /** Returns GFp methods using montgomery multiplication, with x86-64 optimized
576  * P256. See http://eprint.iacr.org/2013/816.
577  *  \return  EC_METHOD object
578  */
579 const EC_METHOD *EC_GFp_nistz256_method(void);
580 #endif
581
582 size_t ec_key_simple_priv2oct(const EC_KEY *eckey,
583                               unsigned char *buf, size_t len);
584 int ec_key_simple_oct2priv(EC_KEY *eckey, const unsigned char *buf, size_t len);
585 int ec_key_simple_generate_key(EC_KEY *eckey);
586 int ec_key_simple_generate_public_key(EC_KEY *eckey);
587 int ec_key_simple_check_key(const EC_KEY *eckey);
588
589 /* EC_METHOD definitions */
590
591 struct ec_key_method_st {
592     const char *name;
593     int32_t flags;
594     int (*init)(EC_KEY *key);
595     void (*finish)(EC_KEY *key);
596     int (*copy)(EC_KEY *dest, const EC_KEY *src);
597     int (*set_group)(EC_KEY *key, const EC_GROUP *grp);
598     int (*set_private)(EC_KEY *key, const BIGNUM *priv_key);
599     int (*set_public)(EC_KEY *key, const EC_POINT *pub_key);
600     int (*keygen)(EC_KEY *key);
601     int (*compute_key)(unsigned char **pout, size_t *poutlen,
602                        const EC_POINT *pub_key, const EC_KEY *ecdh);
603     int (*sign)(int type, const unsigned char *dgst, int dlen, unsigned char
604                 *sig, unsigned int *siglen, const BIGNUM *kinv,
605                 const BIGNUM *r, EC_KEY *eckey);
606     int (*sign_setup)(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
607                       BIGNUM **rp);
608     ECDSA_SIG *(*sign_sig)(const unsigned char *dgst, int dgst_len,
609                            const BIGNUM *in_kinv, const BIGNUM *in_r,
610                            EC_KEY *eckey);
611
612     int (*verify)(int type, const unsigned char *dgst, int dgst_len,
613                   const unsigned char *sigbuf, int sig_len, EC_KEY *eckey);
614     int (*verify_sig)(const unsigned char *dgst, int dgst_len,
615                       const ECDSA_SIG *sig, EC_KEY *eckey);
616 };
617
618 #define EC_KEY_METHOD_DYNAMIC   1
619
620 int ossl_ec_key_gen(EC_KEY *eckey);
621 int ossl_ecdh_compute_key(unsigned char **pout, size_t *poutlen,
622                           const EC_POINT *pub_key, const EC_KEY *ecdh);
623 int ecdh_simple_compute_key(unsigned char **pout, size_t *poutlen,
624                             const EC_POINT *pub_key, const EC_KEY *ecdh);
625
626 struct ECDSA_SIG_st {
627     BIGNUM *r;
628     BIGNUM *s;
629 };
630
631 int ossl_ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
632                           BIGNUM **rp);
633 int ossl_ecdsa_sign(int type, const unsigned char *dgst, int dlen,
634                     unsigned char *sig, unsigned int *siglen,
635                     const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey);
636 ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
637                                const BIGNUM *in_kinv, const BIGNUM *in_r,
638                                EC_KEY *eckey);
639 int ossl_ecdsa_verify(int type, const unsigned char *dgst, int dgst_len,
640                       const unsigned char *sigbuf, int sig_len, EC_KEY *eckey);
641 int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len,
642                           const ECDSA_SIG *sig, EC_KEY *eckey);
643
644 int ED25519_sign(uint8_t *out_sig, const uint8_t *message, size_t message_len,
645                  const uint8_t public_key[32], const uint8_t private_key[32]);
646 int ED25519_verify(const uint8_t *message, size_t message_len,
647                    const uint8_t signature[64], const uint8_t public_key[32]);
648 void ED25519_public_from_private(uint8_t out_public_key[32],
649                                  const uint8_t private_key[32]);
650
651 int X25519(uint8_t out_shared_key[32], const uint8_t private_key[32],
652            const uint8_t peer_public_value[32]);
653 void X25519_public_from_private(uint8_t out_public_value[32],
654                                 const uint8_t private_key[32]);
655
656 /*-
657  * This functions computes a single point multiplication over the EC group,
658  * using, at a high level, a Montgomery ladder with conditional swaps, with
659  * various timing attack defenses.
660  *
661  * It performs either a fixed point multiplication
662  *          (scalar * generator)
663  * when point is NULL, or a variable point multiplication
664  *          (scalar * point)
665  * when point is not NULL.
666  *
667  * `scalar` cannot be NULL and should be in the range [0,n) otherwise all
668  * constant time bets are off (where n is the cardinality of the EC group).
669  *
670  * This function expects `group->order` and `group->cardinality` to be well
671  * defined and non-zero: it fails with an error code otherwise.
672  *
673  * NB: This says nothing about the constant-timeness of the ladder step
674  * implementation (i.e., the default implementation is based on EC_POINT_add and
675  * EC_POINT_dbl, which of course are not constant time themselves) or the
676  * underlying multiprecision arithmetic.
677  *
678  * The product is stored in `r`.
679  *
680  * This is an internal function: callers are in charge of ensuring that the
681  * input parameters `group`, `r`, `scalar` and `ctx` are not NULL.
682  *
683  * Returns 1 on success, 0 otherwise.
684  */
685 int ec_scalar_mul_ladder(const EC_GROUP *group, EC_POINT *r,
686                          const BIGNUM *scalar, const EC_POINT *point,
687                          BN_CTX *ctx);
688
689 int ec_point_blind_coordinates(const EC_GROUP *group, EC_POINT *p, BN_CTX *ctx);
690
691 static ossl_inline int ec_point_ladder_pre(const EC_GROUP *group,
692                                            EC_POINT *r, EC_POINT *s,
693                                            EC_POINT *p, BN_CTX *ctx)
694 {
695     if (group->meth->ladder_pre != NULL)
696         return group->meth->ladder_pre(group, r, s, p, ctx);
697
698     if (!EC_POINT_copy(s, p)
699         || !EC_POINT_dbl(group, r, s, ctx))
700         return 0;
701
702     return 1;
703 }
704
705 static ossl_inline int ec_point_ladder_step(const EC_GROUP *group,
706                                             EC_POINT *r, EC_POINT *s,
707                                             EC_POINT *p, BN_CTX *ctx)
708 {
709     if (group->meth->ladder_step != NULL)
710         return group->meth->ladder_step(group, r, s, p, ctx);
711
712     if (!EC_POINT_add(group, s, r, s, ctx)
713         || !EC_POINT_dbl(group, r, r, ctx))
714         return 0;
715
716     return 1;
717
718 }
719
720 static ossl_inline int ec_point_ladder_post(const EC_GROUP *group,
721                                             EC_POINT *r, EC_POINT *s,
722                                             EC_POINT *p, BN_CTX *ctx)
723 {
724     if (group->meth->ladder_post != NULL)
725         return group->meth->ladder_post(group, r, s, p, ctx);
726
727     return 1;
728 }