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