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