1 /*
2 * Copyright 2001-2020 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 "crypto/ec.h"
18
19 #if defined(__SUNPRO_C)
20 # if __SUNPRO_C >= 0x520
21 # pragma error_messages (off,E_ARRAY_OF_INCOMPLETE_NONAME,E_ARRAY_OF_INCOMPLETE)
22 # endif
23 #endif
24
25 /* Use default functions for poin2oct, oct2point and compressed coordinates */
26 #define EC_FLAGS_DEFAULT_OCT 0x1
27
28 /* Use custom formats for EC_GROUP, EC_POINT and EC_KEY */
29 #define EC_FLAGS_CUSTOM_CURVE 0x2
30
31 /* Curve does not support signing operations */
32 #define EC_FLAGS_NO_SIGN 0x4
33
34 /*
35 * Structure details are not part of the exported interface, so all this may
36 * change in future versions.
37 */
38
39 struct ec_method_st {
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 *);
52 /* used by EC_GROUP_set_curve, EC_GROUP_get_curve: */
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 *);
59 int (*group_order_bits) (const EC_GROUP *);
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 *);
70 /*-
71 * used by EC_POINT_set_to_infinity,
72 * EC_POINT_set_Jprojective_coordinates_GFp,
73 * EC_POINT_get_Jprojective_coordinates_GFp,
74 * EC_POINT_set_affine_coordinates,
75 * EC_POINT_get_affine_coordinates,
76 * EC_POINT_set_compressed_coordinates:
77 */
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 */
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 */
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 *);
156 /*-
157 * 'field_inv' computes the multiplicative inverse of a in the field,
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 *);
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 *);
170 /* private key operations */
171 size_t (*priv2oct)(const EC_KEY *eckey, unsigned char *buf, size_t len);
172 int (*oct2priv)(EC_KEY *eckey, const unsigned char *buf, size_t len);
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 */
180 int (*ecdh_compute_key)(unsigned char **pout, size_t *poutlen,
181 const EC_POINT *pub_key, const EC_KEY *ecdh);
182 /* Inverse modulo order */
183 int (*field_inverse_mod_ord)(const EC_GROUP *, BIGNUM *r,
184 const BIGNUM *x, BN_CTX *);
185 int (*blind_coordinates)(const EC_GROUP *group, EC_POINT *p, BN_CTX *ctx);
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);
195 };
196
197 /*
198 * Types and functions to manipulate pre-computed values.
199 */
200 typedef struct nistp224_pre_comp_st NISTP224_PRE_COMP;
201 typedef struct nistp256_pre_comp_st NISTP256_PRE_COMP;
202 typedef struct nistp521_pre_comp_st NISTP521_PRE_COMP;
203 typedef struct nistz256_pre_comp_st NISTZ256_PRE_COMP;
204 typedef struct ec_pre_comp_st EC_PRE_COMP;
205
206 struct ec_group_st {
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 int decoded_from_explicit_params; /* set if decoded from explicit
213 * curve parameters encoding */
214 point_conversion_form_t asn1_form;
215 unsigned char *seed; /* optional seed for parameters (appears in
216 * ASN1) */
217 size_t seed_len;
218 /*
219 * The following members are handled by the method functions, even if
220 * they appear generic
221 */
222 /*
223 * Field specification. For curves over GF(p), this is the modulus; for
224 * curves over GF(2^m), this is the irreducible polynomial defining the
225 * field.
226 */
227 BIGNUM *field;
228 /*
229 * Field specification for curves over GF(2^m). The irreducible f(t) is
230 * then of the form: t^poly[0] + t^poly[1] + ... + t^poly[k] where m =
231 * poly[0] > poly[1] > ... > poly[k] = 0. The array is terminated with
232 * poly[k+1]=-1. All elliptic curve irreducibles have at most 5 non-zero
233 * terms.
234 */
235 int poly[6];
236 /*
237 * Curve coefficients. (Here the assumption is that BIGNUMs can be used
238 * or abused for all kinds of fields, not just GF(p).) For characteristic
239 * > 3, the curve is defined by a Weierstrass equation of the form y^2 =
240 * x^3 + a*x + b. For characteristic 2, the curve is defined by an
241 * equation of the form y^2 + x*y = x^3 + a*x^2 + b.
242 */
243 BIGNUM *a, *b;
244 /* enable optimized point arithmetics for special case */
245 int a_is_minus3;
246 /* method-specific (e.g., Montgomery structure) */
247 void *field_data1;
248 /* method-specific */
249 void *field_data2;
250 /* method-specific */
251 int (*field_mod_func) (BIGNUM *, const BIGNUM *, const BIGNUM *,
252 BN_CTX *);
253 /* data for ECDSA inverse */
254 BN_MONT_CTX *mont_data;
255
256 /*
257 * Precomputed values for speed. The PCT_xxx names match the
258 * pre_comp.xxx union names; see the SETPRECOMP and HAVEPRECOMP
259 * macros, below.
260 */
261 enum {
262 PCT_none,
263 PCT_nistp224, PCT_nistp256, PCT_nistp521, PCT_nistz256,
264 PCT_ec
265 } pre_comp_type;
266 union {
267 NISTP224_PRE_COMP *nistp224;
268 NISTP256_PRE_COMP *nistp256;
269 NISTP521_PRE_COMP *nistp521;
270 NISTZ256_PRE_COMP *nistz256;
271 EC_PRE_COMP *ec;
272 } pre_comp;
273 };
274
275 #define SETPRECOMP(g, type, pre) \
276 g->pre_comp_type = PCT_##type, g->pre_comp.type = pre
277 #define HAVEPRECOMP(g, type) \
278 g->pre_comp_type == PCT_##type && g->pre_comp.type != NULL
279
280 struct ec_key_st {
281 const EC_KEY_METHOD *meth;
282 ENGINE *engine;
283 int version;
284 EC_GROUP *group;
285 EC_POINT *pub_key;
286 BIGNUM *priv_key;
287 unsigned int enc_flag;
288 point_conversion_form_t conv_form;
289 CRYPTO_REF_COUNT references;
290 int flags;
291 CRYPTO_EX_DATA ex_data;
292 CRYPTO_RWLOCK *lock;
293 };
294
295 struct ec_point_st {
296 const EC_METHOD *meth;
297 /* NID for the curve if known */
298 int curve_name;
299 /*
300 * All members except 'meth' are handled by the method functions, even if
301 * they appear generic
302 */
303 BIGNUM *X;
304 BIGNUM *Y;
305 BIGNUM *Z; /* Jacobian projective coordinates: * (X, Y,
306 * Z) represents (X/Z^2, Y/Z^3) if Z != 0 */
307 int Z_is_one; /* enable optimized point arithmetics for
308 * special case */
309 };
310
ec_point_is_compat(const EC_POINT * point,const EC_GROUP * group)311 static ossl_inline int ec_point_is_compat(const EC_POINT *point,
312 const EC_GROUP *group)
313 {
314 if (group->meth != point->meth
315 || (group->curve_name != 0
316 && point->curve_name != 0
317 && group->curve_name != point->curve_name))
318 return 0;
319
320 return 1;
321 }
322
323 NISTP224_PRE_COMP *EC_nistp224_pre_comp_dup(NISTP224_PRE_COMP *);
324 NISTP256_PRE_COMP *EC_nistp256_pre_comp_dup(NISTP256_PRE_COMP *);
325 NISTP521_PRE_COMP *EC_nistp521_pre_comp_dup(NISTP521_PRE_COMP *);
326 NISTZ256_PRE_COMP *EC_nistz256_pre_comp_dup(NISTZ256_PRE_COMP *);
327 NISTP256_PRE_COMP *EC_nistp256_pre_comp_dup(NISTP256_PRE_COMP *);
328 EC_PRE_COMP *EC_ec_pre_comp_dup(EC_PRE_COMP *);
329
330 void EC_pre_comp_free(EC_GROUP *group);
331 void EC_nistp224_pre_comp_free(NISTP224_PRE_COMP *);
332 void EC_nistp256_pre_comp_free(NISTP256_PRE_COMP *);
333 void EC_nistp521_pre_comp_free(NISTP521_PRE_COMP *);
334 void EC_nistz256_pre_comp_free(NISTZ256_PRE_COMP *);
335 void EC_ec_pre_comp_free(EC_PRE_COMP *);
336
337 /*
338 * method functions in ec_mult.c (ec_lib.c uses these as defaults if
339 * group->method->mul is 0)
340 */
341 int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
342 size_t num, const EC_POINT *points[], const BIGNUM *scalars[],
343 BN_CTX *);
344 int ec_wNAF_precompute_mult(EC_GROUP *group, BN_CTX *);
345 int ec_wNAF_have_precompute_mult(const EC_GROUP *group);
346
347 /* method functions in ecp_smpl.c */
348 int ec_GFp_simple_group_init(EC_GROUP *);
349 void ec_GFp_simple_group_finish(EC_GROUP *);
350 void ec_GFp_simple_group_clear_finish(EC_GROUP *);
351 int ec_GFp_simple_group_copy(EC_GROUP *, const EC_GROUP *);
352 int ec_GFp_simple_group_set_curve(EC_GROUP *, const BIGNUM *p,
353 const BIGNUM *a, const BIGNUM *b, BN_CTX *);
354 int ec_GFp_simple_group_get_curve(const EC_GROUP *, BIGNUM *p, BIGNUM *a,
355 BIGNUM *b, BN_CTX *);
356 int ec_GFp_simple_group_get_degree(const EC_GROUP *);
357 int ec_GFp_simple_group_check_discriminant(const EC_GROUP *, BN_CTX *);
358 int ec_GFp_simple_point_init(EC_POINT *);
359 void ec_GFp_simple_point_finish(EC_POINT *);
360 void ec_GFp_simple_point_clear_finish(EC_POINT *);
361 int ec_GFp_simple_point_copy(EC_POINT *, const EC_POINT *);
362 int ec_GFp_simple_point_set_to_infinity(const EC_GROUP *, EC_POINT *);
363 int ec_GFp_simple_set_Jprojective_coordinates_GFp(const EC_GROUP *,
364 EC_POINT *, const BIGNUM *x,
365 const BIGNUM *y,
366 const BIGNUM *z, BN_CTX *);
367 int ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP *,
368 const EC_POINT *, BIGNUM *x,
369 BIGNUM *y, BIGNUM *z,
370 BN_CTX *);
371 int ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP *, EC_POINT *,
372 const BIGNUM *x,
373 const BIGNUM *y, BN_CTX *);
374 int ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP *,
375 const EC_POINT *, BIGNUM *x,
376 BIGNUM *y, BN_CTX *);
377 int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *, EC_POINT *,
378 const BIGNUM *x, int y_bit,
379 BN_CTX *);
380 size_t ec_GFp_simple_point2oct(const EC_GROUP *, const EC_POINT *,
381 point_conversion_form_t form,
382 unsigned char *buf, size_t len, BN_CTX *);
383 int ec_GFp_simple_oct2point(const EC_GROUP *, EC_POINT *,
384 const unsigned char *buf, size_t len, BN_CTX *);
385 int ec_GFp_simple_add(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
386 const EC_POINT *b, BN_CTX *);
387 int ec_GFp_simple_dbl(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
388 BN_CTX *);
389 int ec_GFp_simple_invert(const EC_GROUP *, EC_POINT *, BN_CTX *);
390 int ec_GFp_simple_is_at_infinity(const EC_GROUP *, const EC_POINT *);
391 int ec_GFp_simple_is_on_curve(const EC_GROUP *, const EC_POINT *, BN_CTX *);
392 int ec_GFp_simple_cmp(const EC_GROUP *, const EC_POINT *a, const EC_POINT *b,
393 BN_CTX *);
394 int ec_GFp_simple_make_affine(const EC_GROUP *, EC_POINT *, BN_CTX *);
395 int ec_GFp_simple_points_make_affine(const EC_GROUP *, size_t num,
396 EC_POINT *[], BN_CTX *);
397 int ec_GFp_simple_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
398 const BIGNUM *b, BN_CTX *);
399 int ec_GFp_simple_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
400 BN_CTX *);
401 int ec_GFp_simple_field_inv(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
402 BN_CTX *);
403 int ec_GFp_simple_blind_coordinates(const EC_GROUP *group, EC_POINT *p,
404 BN_CTX *ctx);
405 int ec_GFp_simple_ladder_pre(const EC_GROUP *group,
406 EC_POINT *r, EC_POINT *s,
407 EC_POINT *p, BN_CTX *ctx);
408 int ec_GFp_simple_ladder_step(const EC_GROUP *group,
409 EC_POINT *r, EC_POINT *s,
410 EC_POINT *p, BN_CTX *ctx);
411 int ec_GFp_simple_ladder_post(const EC_GROUP *group,
412 EC_POINT *r, EC_POINT *s,
413 EC_POINT *p, BN_CTX *ctx);
414
415 /* method functions in ecp_mont.c */
416 int ec_GFp_mont_group_init(EC_GROUP *);
417 int ec_GFp_mont_group_set_curve(EC_GROUP *, const BIGNUM *p, const BIGNUM *a,
418 const BIGNUM *b, BN_CTX *);
419 void ec_GFp_mont_group_finish(EC_GROUP *);
420 void ec_GFp_mont_group_clear_finish(EC_GROUP *);
421 int ec_GFp_mont_group_copy(EC_GROUP *, const EC_GROUP *);
422 int ec_GFp_mont_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
423 const BIGNUM *b, BN_CTX *);
424 int ec_GFp_mont_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
425 BN_CTX *);
426 int ec_GFp_mont_field_inv(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
427 BN_CTX *);
428 int ec_GFp_mont_field_encode(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
429 BN_CTX *);
430 int ec_GFp_mont_field_decode(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
431 BN_CTX *);
432 int ec_GFp_mont_field_set_to_one(const EC_GROUP *, BIGNUM *r, BN_CTX *);
433
434 /* method functions in ecp_nist.c */
435 int ec_GFp_nist_group_copy(EC_GROUP *dest, const EC_GROUP *src);
436 int ec_GFp_nist_group_set_curve(EC_GROUP *, const BIGNUM *p, const BIGNUM *a,
437 const BIGNUM *b, BN_CTX *);
438 int ec_GFp_nist_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
439 const BIGNUM *b, BN_CTX *);
440 int ec_GFp_nist_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
441 BN_CTX *);
442
443 /* method functions in ec2_smpl.c */
444 int ec_GF2m_simple_group_init(EC_GROUP *);
445 void ec_GF2m_simple_group_finish(EC_GROUP *);
446 void ec_GF2m_simple_group_clear_finish(EC_GROUP *);
447 int ec_GF2m_simple_group_copy(EC_GROUP *, const EC_GROUP *);
448 int ec_GF2m_simple_group_set_curve(EC_GROUP *, const BIGNUM *p,
449 const BIGNUM *a, const BIGNUM *b,
450 BN_CTX *);
451 int ec_GF2m_simple_group_get_curve(const EC_GROUP *, BIGNUM *p, BIGNUM *a,
452 BIGNUM *b, BN_CTX *);
453 int ec_GF2m_simple_group_get_degree(const EC_GROUP *);
454 int ec_GF2m_simple_group_check_discriminant(const EC_GROUP *, BN_CTX *);
455 int ec_GF2m_simple_point_init(EC_POINT *);
456 void ec_GF2m_simple_point_finish(EC_POINT *);
457 void ec_GF2m_simple_point_clear_finish(EC_POINT *);
458 int ec_GF2m_simple_point_copy(EC_POINT *, const EC_POINT *);
459 int ec_GF2m_simple_point_set_to_infinity(const EC_GROUP *, EC_POINT *);
460 int ec_GF2m_simple_point_set_affine_coordinates(const EC_GROUP *, EC_POINT *,
461 const BIGNUM *x,
462 const BIGNUM *y, BN_CTX *);
463 int ec_GF2m_simple_point_get_affine_coordinates(const EC_GROUP *,
464 const EC_POINT *, BIGNUM *x,
465 BIGNUM *y, BN_CTX *);
466 int ec_GF2m_simple_set_compressed_coordinates(const EC_GROUP *, EC_POINT *,
467 const BIGNUM *x, int y_bit,
468 BN_CTX *);
469 size_t ec_GF2m_simple_point2oct(const EC_GROUP *, const EC_POINT *,
470 point_conversion_form_t form,
471 unsigned char *buf, size_t len, BN_CTX *);
472 int ec_GF2m_simple_oct2point(const EC_GROUP *, EC_POINT *,
473 const unsigned char *buf, size_t len, BN_CTX *);
474 int ec_GF2m_simple_add(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
475 const EC_POINT *b, BN_CTX *);
476 int ec_GF2m_simple_dbl(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
477 BN_CTX *);
478 int ec_GF2m_simple_invert(const EC_GROUP *, EC_POINT *, BN_CTX *);
479 int ec_GF2m_simple_is_at_infinity(const EC_GROUP *, const EC_POINT *);
480 int ec_GF2m_simple_is_on_curve(const EC_GROUP *, const EC_POINT *, BN_CTX *);
481 int ec_GF2m_simple_cmp(const EC_GROUP *, const EC_POINT *a, const EC_POINT *b,
482 BN_CTX *);
483 int ec_GF2m_simple_make_affine(const EC_GROUP *, EC_POINT *, BN_CTX *);
484 int ec_GF2m_simple_points_make_affine(const EC_GROUP *, size_t num,
485 EC_POINT *[], BN_CTX *);
486 int ec_GF2m_simple_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
487 const BIGNUM *b, BN_CTX *);
488 int ec_GF2m_simple_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
489 BN_CTX *);
490 int ec_GF2m_simple_field_div(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
491 const BIGNUM *b, BN_CTX *);
492
493 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
494 /* method functions in ecp_nistp224.c */
495 int ec_GFp_nistp224_group_init(EC_GROUP *group);
496 int ec_GFp_nistp224_group_set_curve(EC_GROUP *group, const BIGNUM *p,
497 const BIGNUM *a, const BIGNUM *n,
498 BN_CTX *);
499 int ec_GFp_nistp224_point_get_affine_coordinates(const EC_GROUP *group,
500 const EC_POINT *point,
501 BIGNUM *x, BIGNUM *y,
502 BN_CTX *ctx);
503 int ec_GFp_nistp224_mul(const EC_GROUP *group, EC_POINT *r,
504 const BIGNUM *scalar, size_t num,
505 const EC_POINT *points[], const BIGNUM *scalars[],
506 BN_CTX *);
507 int ec_GFp_nistp224_points_mul(const EC_GROUP *group, EC_POINT *r,
508 const BIGNUM *scalar, size_t num,
509 const EC_POINT *points[],
510 const BIGNUM *scalars[], BN_CTX *ctx);
511 int ec_GFp_nistp224_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
512 int ec_GFp_nistp224_have_precompute_mult(const EC_GROUP *group);
513
514 /* method functions in ecp_nistp256.c */
515 int ec_GFp_nistp256_group_init(EC_GROUP *group);
516 int ec_GFp_nistp256_group_set_curve(EC_GROUP *group, const BIGNUM *p,
517 const BIGNUM *a, const BIGNUM *n,
518 BN_CTX *);
519 int ec_GFp_nistp256_point_get_affine_coordinates(const EC_GROUP *group,
520 const EC_POINT *point,
521 BIGNUM *x, BIGNUM *y,
522 BN_CTX *ctx);
523 int ec_GFp_nistp256_mul(const EC_GROUP *group, EC_POINT *r,
524 const BIGNUM *scalar, size_t num,
525 const EC_POINT *points[], const BIGNUM *scalars[],
526 BN_CTX *);
527 int ec_GFp_nistp256_points_mul(const EC_GROUP *group, EC_POINT *r,
528 const BIGNUM *scalar, size_t num,
529 const EC_POINT *points[],
530 const BIGNUM *scalars[], BN_CTX *ctx);
531 int ec_GFp_nistp256_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
532 int ec_GFp_nistp256_have_precompute_mult(const EC_GROUP *group);
533
534 /* method functions in ecp_nistp521.c */
535 int ec_GFp_nistp521_group_init(EC_GROUP *group);
536 int ec_GFp_nistp521_group_set_curve(EC_GROUP *group, const BIGNUM *p,
537 const BIGNUM *a, const BIGNUM *n,
538 BN_CTX *);
539 int ec_GFp_nistp521_point_get_affine_coordinates(const EC_GROUP *group,
540 const EC_POINT *point,
541 BIGNUM *x, BIGNUM *y,
542 BN_CTX *ctx);
543 int ec_GFp_nistp521_mul(const EC_GROUP *group, EC_POINT *r,
544 const BIGNUM *scalar, size_t num,
545 const EC_POINT *points[], const BIGNUM *scalars[],
546 BN_CTX *);
547 int ec_GFp_nistp521_points_mul(const EC_GROUP *group, EC_POINT *r,
548 const BIGNUM *scalar, size_t num,
549 const EC_POINT *points[],
550 const BIGNUM *scalars[], BN_CTX *ctx);
551 int ec_GFp_nistp521_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
552 int ec_GFp_nistp521_have_precompute_mult(const EC_GROUP *group);
553
554 /* utility functions in ecp_nistputil.c */
555 void ec_GFp_nistp_points_make_affine_internal(size_t num, void *point_array,
556 size_t felem_size,
557 void *tmp_felems,
558 void (*felem_one) (void *out),
559 int (*felem_is_zero) (const void
560 *in),
561 void (*felem_assign) (void *out,
562 const void
563 *in),
564 void (*felem_square) (void *out,
565 const void
566 *in),
567 void (*felem_mul) (void *out,
568 const void
569 *in1,
570 const void
571 *in2),
572 void (*felem_inv) (void *out,
573 const void
574 *in),
575 void (*felem_contract) (void
576 *out,
577 const
578 void
579 *in));
580 void ec_GFp_nistp_recode_scalar_bits(unsigned char *sign,
581 unsigned char *digit, unsigned char in);
582 #endif
583 int ec_group_simple_order_bits(const EC_GROUP *group);
584
585 #ifdef ECP_NISTZ256_ASM
586 /** Returns GFp methods using montgomery multiplication, with x86-64 optimized
587 * P256. See http://eprint.iacr.org/2013/816.
588 * \return EC_METHOD object
589 */
590 const EC_METHOD *EC_GFp_nistz256_method(void);
591 #endif
592
593 size_t ec_key_simple_priv2oct(const EC_KEY *eckey,
594 unsigned char *buf, size_t len);
595 int ec_key_simple_oct2priv(EC_KEY *eckey, const unsigned char *buf, size_t len);
596 int ec_key_simple_generate_key(EC_KEY *eckey);
597 int ec_key_simple_generate_public_key(EC_KEY *eckey);
598 int ec_key_simple_check_key(const EC_KEY *eckey);
599
600 int ec_curve_nid_from_params(const EC_GROUP *group, BN_CTX *ctx);
601
602 /* EC_METHOD definitions */
603
604 struct ec_key_method_st {
605 const char *name;
606 int32_t flags;
607 int (*init)(EC_KEY *key);
608 void (*finish)(EC_KEY *key);
609 int (*copy)(EC_KEY *dest, const EC_KEY *src);
610 int (*set_group)(EC_KEY *key, const EC_GROUP *grp);
611 int (*set_private)(EC_KEY *key, const BIGNUM *priv_key);
612 int (*set_public)(EC_KEY *key, const EC_POINT *pub_key);
613 int (*keygen)(EC_KEY *key);
614 int (*compute_key)(unsigned char **pout, size_t *poutlen,
615 const EC_POINT *pub_key, const EC_KEY *ecdh);
616 int (*sign)(int type, const unsigned char *dgst, int dlen, unsigned char
617 *sig, unsigned int *siglen, const BIGNUM *kinv,
618 const BIGNUM *r, EC_KEY *eckey);
619 int (*sign_setup)(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
620 BIGNUM **rp);
621 ECDSA_SIG *(*sign_sig)(const unsigned char *dgst, int dgst_len,
622 const BIGNUM *in_kinv, const BIGNUM *in_r,
623 EC_KEY *eckey);
624
625 int (*verify)(int type, const unsigned char *dgst, int dgst_len,
626 const unsigned char *sigbuf, int sig_len, EC_KEY *eckey);
627 int (*verify_sig)(const unsigned char *dgst, int dgst_len,
628 const ECDSA_SIG *sig, EC_KEY *eckey);
629 };
630
631 #define EC_KEY_METHOD_DYNAMIC 1
632
633 int ossl_ec_key_gen(EC_KEY *eckey);
634 int ossl_ecdh_compute_key(unsigned char **pout, size_t *poutlen,
635 const EC_POINT *pub_key, const EC_KEY *ecdh);
636 int ecdh_simple_compute_key(unsigned char **pout, size_t *poutlen,
637 const EC_POINT *pub_key, const EC_KEY *ecdh);
638
639 struct ECDSA_SIG_st {
640 BIGNUM *r;
641 BIGNUM *s;
642 };
643
644 int ossl_ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
645 BIGNUM **rp);
646 int ossl_ecdsa_sign(int type, const unsigned char *dgst, int dlen,
647 unsigned char *sig, unsigned int *siglen,
648 const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey);
649 ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
650 const BIGNUM *in_kinv, const BIGNUM *in_r,
651 EC_KEY *eckey);
652 int ossl_ecdsa_verify(int type, const unsigned char *dgst, int dgst_len,
653 const unsigned char *sigbuf, int sig_len, EC_KEY *eckey);
654 int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len,
655 const ECDSA_SIG *sig, EC_KEY *eckey);
656
657 int ED25519_sign(uint8_t *out_sig, const uint8_t *message, size_t message_len,
658 const uint8_t public_key[32], const uint8_t private_key[32]);
659 int ED25519_verify(const uint8_t *message, size_t message_len,
660 const uint8_t signature[64], const uint8_t public_key[32]);
661 void ED25519_public_from_private(uint8_t out_public_key[32],
662 const uint8_t private_key[32]);
663
664 int X25519(uint8_t out_shared_key[32], const uint8_t private_key[32],
665 const uint8_t peer_public_value[32]);
666 void X25519_public_from_private(uint8_t out_public_value[32],
667 const uint8_t private_key[32]);
668
669 /*-
670 * This functions computes a single point multiplication over the EC group,
671 * using, at a high level, a Montgomery ladder with conditional swaps, with
672 * various timing attack defenses.
673 *
674 * It performs either a fixed point multiplication
675 * (scalar * generator)
676 * when point is NULL, or a variable point multiplication
677 * (scalar * point)
678 * when point is not NULL.
679 *
680 * `scalar` cannot be NULL and should be in the range [0,n) otherwise all
681 * constant time bets are off (where n is the cardinality of the EC group).
682 *
683 * This function expects `group->order` and `group->cardinality` to be well
684 * defined and non-zero: it fails with an error code otherwise.
685 *
686 * NB: This says nothing about the constant-timeness of the ladder step
687 * implementation (i.e., the default implementation is based on EC_POINT_add and
688 * EC_POINT_dbl, which of course are not constant time themselves) or the
689 * underlying multiprecision arithmetic.
690 *
691 * The product is stored in `r`.
692 *
693 * This is an internal function: callers are in charge of ensuring that the
694 * input parameters `group`, `r`, `scalar` and `ctx` are not NULL.
695 *
696 * Returns 1 on success, 0 otherwise.
697 */
698 int ec_scalar_mul_ladder(const EC_GROUP *group, EC_POINT *r,
699 const BIGNUM *scalar, const EC_POINT *point,
700 BN_CTX *ctx);
701
702 int ec_point_blind_coordinates(const EC_GROUP *group, EC_POINT *p, BN_CTX *ctx);
703
ec_point_ladder_pre(const EC_GROUP * group,EC_POINT * r,EC_POINT * s,EC_POINT * p,BN_CTX * ctx)704 static ossl_inline int ec_point_ladder_pre(const EC_GROUP *group,
705 EC_POINT *r, EC_POINT *s,
706 EC_POINT *p, BN_CTX *ctx)
707 {
708 if (group->meth->ladder_pre != NULL)
709 return group->meth->ladder_pre(group, r, s, p, ctx);
710
711 if (!EC_POINT_copy(s, p)
712 || !EC_POINT_dbl(group, r, s, ctx))
713 return 0;
714
715 return 1;
716 }
717
ec_point_ladder_step(const EC_GROUP * group,EC_POINT * r,EC_POINT * s,EC_POINT * p,BN_CTX * ctx)718 static ossl_inline int ec_point_ladder_step(const EC_GROUP *group,
719 EC_POINT *r, EC_POINT *s,
720 EC_POINT *p, BN_CTX *ctx)
721 {
722 if (group->meth->ladder_step != NULL)
723 return group->meth->ladder_step(group, r, s, p, ctx);
724
725 if (!EC_POINT_add(group, s, r, s, ctx)
726 || !EC_POINT_dbl(group, r, r, ctx))
727 return 0;
728
729 return 1;
730
731 }
732
ec_point_ladder_post(const EC_GROUP * group,EC_POINT * r,EC_POINT * s,EC_POINT * p,BN_CTX * ctx)733 static ossl_inline int ec_point_ladder_post(const EC_GROUP *group,
734 EC_POINT *r, EC_POINT *s,
735 EC_POINT *p, BN_CTX *ctx)
736 {
737 if (group->meth->ladder_post != NULL)
738 return group->meth->ladder_post(group, r, s, p, ctx);
739
740 return 1;
741 }
742