]> git.ipfire.org Git - thirdparty/openssl.git/blob - include/openssl/ec.h
Deprecate EC_POINT_bn2point and EC_POINT_point2bn.
[thirdparty/openssl.git] / include / openssl / ec.h
1 /*
2 * Copyright 2002-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 Apache License 2.0 (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 #ifndef OPENSSL_EC_H
12 # define OPENSSL_EC_H
13 # pragma once
14
15 # include <openssl/macros.h>
16 # ifndef OPENSSL_NO_DEPRECATED_3_0
17 # define HEADER_EC_H
18 # endif
19
20 # include <openssl/opensslconf.h>
21
22 # ifndef OPENSSL_NO_EC
23 # include <openssl/asn1.h>
24 # include <openssl/symhacks.h>
25 # ifndef OPENSSL_NO_DEPRECATED_1_1_0
26 # include <openssl/bn.h>
27 # endif
28 # include <openssl/ecerr.h>
29 # ifdef __cplusplus
30 extern "C" {
31 # endif
32
33 # ifndef OPENSSL_ECC_MAX_FIELD_BITS
34 # define OPENSSL_ECC_MAX_FIELD_BITS 661
35 # endif
36
37 /** Enum for the point conversion form as defined in X9.62 (ECDSA)
38 * for the encoding of a elliptic curve point (x,y) */
39 typedef enum {
40 /** the point is encoded as z||x, where the octet z specifies
41 * which solution of the quadratic equation y is */
42 POINT_CONVERSION_COMPRESSED = 2,
43 /** the point is encoded as z||x||y, where z is the octet 0x04 */
44 POINT_CONVERSION_UNCOMPRESSED = 4,
45 /** the point is encoded as z||x||y, where the octet z specifies
46 * which solution of the quadratic equation y is */
47 POINT_CONVERSION_HYBRID = 6
48 } point_conversion_form_t;
49
50 # include <openssl/params.h>
51 # ifndef OPENSSL_NO_DEPRECATED_3_0
52 typedef struct ec_method_st EC_METHOD;
53 # endif
54 typedef struct ec_group_st EC_GROUP;
55 typedef struct ec_point_st EC_POINT;
56 typedef struct ecpk_parameters_st ECPKPARAMETERS;
57 typedef struct ec_parameters_st ECPARAMETERS;
58
59 /********************************************************************/
60 /* EC_METHODs for curves over GF(p) */
61 /********************************************************************/
62
63 /** Returns the basic GFp ec methods which provides the basis for the
64 * optimized methods.
65 * \return EC_METHOD object
66 */
67 DEPRECATEDIN_3_0(const EC_METHOD *EC_GFp_simple_method(void))
68
69 /** Returns GFp methods using montgomery multiplication.
70 * \return EC_METHOD object
71 */
72 DEPRECATEDIN_3_0(const EC_METHOD *EC_GFp_mont_method(void))
73
74 /** Returns GFp methods using optimized methods for NIST recommended curves
75 * \return EC_METHOD object
76 */
77 DEPRECATEDIN_3_0(const EC_METHOD *EC_GFp_nist_method(void))
78
79 # ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
80 /** Returns 64-bit optimized methods for nistp224
81 * \return EC_METHOD object
82 */
83 DEPRECATEDIN_3_0(const EC_METHOD *EC_GFp_nistp224_method(void))
84
85 /** Returns 64-bit optimized methods for nistp256
86 * \return EC_METHOD object
87 */
88 DEPRECATEDIN_3_0(const EC_METHOD *EC_GFp_nistp256_method(void))
89
90 /** Returns 64-bit optimized methods for nistp521
91 * \return EC_METHOD object
92 */
93 DEPRECATEDIN_3_0(const EC_METHOD *EC_GFp_nistp521_method(void))
94 # endif
95
96 # ifndef OPENSSL_NO_EC2M
97 /********************************************************************/
98 /* EC_METHOD for curves over GF(2^m) */
99 /********************************************************************/
100
101 /** Returns the basic GF2m ec method
102 * \return EC_METHOD object
103 */
104 DEPRECATEDIN_3_0(const EC_METHOD *EC_GF2m_simple_method(void))
105
106 # endif
107
108 /********************************************************************/
109 /* EC_GROUP functions */
110 /********************************************************************/
111
112 /**
113 * Creates a new EC_GROUP object
114 * \param meth EC_METHOD to use
115 * \return newly created EC_GROUP object or NULL in case of an error.
116 */
117 DEPRECATEDIN_3_0(EC_GROUP *EC_GROUP_new(const EC_METHOD *meth))
118
119 /** Frees a EC_GROUP object
120 * \param group EC_GROUP object to be freed.
121 */
122 void EC_GROUP_free(EC_GROUP *group);
123
124 /** Clears and frees a EC_GROUP object
125 * \param group EC_GROUP object to be cleared and freed.
126 */
127 DEPRECATEDIN_3_0(void EC_GROUP_clear_free(EC_GROUP *group))
128
129 /** Copies EC_GROUP objects. Note: both EC_GROUPs must use the same EC_METHOD.
130 * \param dst destination EC_GROUP object
131 * \param src source EC_GROUP object
132 * \return 1 on success and 0 if an error occurred.
133 */
134 int EC_GROUP_copy(EC_GROUP *dst, const EC_GROUP *src);
135
136 /** Creates a new EC_GROUP object and copies the content
137 * form src to the newly created EC_KEY object
138 * \param src source EC_GROUP object
139 * \return newly created EC_GROUP object or NULL in case of an error.
140 */
141 EC_GROUP *EC_GROUP_dup(const EC_GROUP *src);
142
143 /** Returns the EC_METHOD of the EC_GROUP object.
144 * \param group EC_GROUP object
145 * \return EC_METHOD used in this EC_GROUP object.
146 */
147 DEPRECATEDIN_3_0(const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group))
148
149 /** Returns the field type of the EC_METHOD.
150 * \param meth EC_METHOD object
151 * \return NID of the underlying field type OID.
152 */
153 DEPRECATEDIN_3_0(int EC_METHOD_get_field_type(const EC_METHOD *meth))
154
155 /** Sets the generator and its order/cofactor of a EC_GROUP object.
156 * \param group EC_GROUP object
157 * \param generator EC_POINT object with the generator.
158 * \param order the order of the group generated by the generator.
159 * \param cofactor the index of the sub-group generated by the generator
160 * in the group of all points on the elliptic curve.
161 * \return 1 on success and 0 if an error occurred
162 */
163 int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator,
164 const BIGNUM *order, const BIGNUM *cofactor);
165
166 /** Returns the generator of a EC_GROUP object.
167 * \param group EC_GROUP object
168 * \return the currently used generator (possibly NULL).
169 */
170 const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group);
171
172 /** Returns the montgomery data for order(Generator)
173 * \param group EC_GROUP object
174 * \return the currently used montgomery data (possibly NULL).
175 */
176 BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group);
177
178 /** Gets the order of a EC_GROUP
179 * \param group EC_GROUP object
180 * \param order BIGNUM to which the order is copied
181 * \param ctx unused
182 * \return 1 on success and 0 if an error occurred
183 */
184 int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx);
185
186 /** Gets the order of an EC_GROUP
187 * \param group EC_GROUP object
188 * \return the group order
189 */
190 const BIGNUM *EC_GROUP_get0_order(const EC_GROUP *group);
191
192 /** Gets the number of bits of the order of an EC_GROUP
193 * \param group EC_GROUP object
194 * \return number of bits of group order.
195 */
196 int EC_GROUP_order_bits(const EC_GROUP *group);
197
198 /** Gets the cofactor of a EC_GROUP
199 * \param group EC_GROUP object
200 * \param cofactor BIGNUM to which the cofactor is copied
201 * \param ctx unused
202 * \return 1 on success and 0 if an error occurred
203 */
204 int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor,
205 BN_CTX *ctx);
206
207 /** Gets the cofactor of an EC_GROUP
208 * \param group EC_GROUP object
209 * \return the group cofactor
210 */
211 const BIGNUM *EC_GROUP_get0_cofactor(const EC_GROUP *group);
212
213 /** Sets the name of a EC_GROUP object
214 * \param group EC_GROUP object
215 * \param nid NID of the curve name OID
216 */
217 void EC_GROUP_set_curve_name(EC_GROUP *group, int nid);
218
219 /** Returns the curve name of a EC_GROUP object
220 * \param group EC_GROUP object
221 * \return NID of the curve name OID or 0 if not set.
222 */
223 int EC_GROUP_get_curve_name(const EC_GROUP *group);
224
225 /** Gets the field of an EC_GROUP
226 * \param group EC_GROUP object
227 * \return the group field
228 */
229 const BIGNUM *EC_GROUP_get0_field(const EC_GROUP *group);
230
231 /** Returns the field type of the EC_GROUP.
232 * \param group EC_GROUP object
233 * \return NID of the underlying field type OID.
234 */
235 int EC_GROUP_get_field_type(const EC_GROUP *group);
236
237 void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag);
238 int EC_GROUP_get_asn1_flag(const EC_GROUP *group);
239
240 void EC_GROUP_set_point_conversion_form(EC_GROUP *group,
241 point_conversion_form_t form);
242 point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP *);
243
244 unsigned char *EC_GROUP_get0_seed(const EC_GROUP *x);
245 size_t EC_GROUP_get_seed_len(const EC_GROUP *);
246 size_t EC_GROUP_set_seed(EC_GROUP *, const unsigned char *, size_t len);
247
248 /** Sets the parameters of a ec curve defined by y^2 = x^3 + a*x + b (for GFp)
249 * or y^2 + x*y = x^3 + a*x^2 + b (for GF2m)
250 * \param group EC_GROUP object
251 * \param p BIGNUM with the prime number (GFp) or the polynomial
252 * defining the underlying field (GF2m)
253 * \param a BIGNUM with parameter a of the equation
254 * \param b BIGNUM with parameter b of the equation
255 * \param ctx BN_CTX object (optional)
256 * \return 1 on success and 0 if an error occurred
257 */
258 int EC_GROUP_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
259 const BIGNUM *b, BN_CTX *ctx);
260
261 /** Gets the parameters of the ec curve defined by y^2 = x^3 + a*x + b (for GFp)
262 * or y^2 + x*y = x^3 + a*x^2 + b (for GF2m)
263 * \param group EC_GROUP object
264 * \param p BIGNUM with the prime number (GFp) or the polynomial
265 * defining the underlying field (GF2m)
266 * \param a BIGNUM for parameter a of the equation
267 * \param b BIGNUM for parameter b of the equation
268 * \param ctx BN_CTX object (optional)
269 * \return 1 on success and 0 if an error occurred
270 */
271 int EC_GROUP_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b,
272 BN_CTX *ctx);
273
274 /** Sets the parameters of an ec curve. Synonym for EC_GROUP_set_curve
275 * \param group EC_GROUP object
276 * \param p BIGNUM with the prime number (GFp) or the polynomial
277 * defining the underlying field (GF2m)
278 * \param a BIGNUM with parameter a of the equation
279 * \param b BIGNUM with parameter b of the equation
280 * \param ctx BN_CTX object (optional)
281 * \return 1 on success and 0 if an error occurred
282 */
283 DEPRECATEDIN_3_0(int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p,
284 const BIGNUM *a, const BIGNUM *b,
285 BN_CTX *ctx))
286
287 /** Gets the parameters of an ec curve. Synonym for EC_GROUP_get_curve
288 * \param group EC_GROUP object
289 * \param p BIGNUM with the prime number (GFp) or the polynomial
290 * defining the underlying field (GF2m)
291 * \param a BIGNUM for parameter a of the equation
292 * \param b BIGNUM for parameter b of the equation
293 * \param ctx BN_CTX object (optional)
294 * \return 1 on success and 0 if an error occurred
295 */
296 DEPRECATEDIN_3_0(int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p,
297 BIGNUM *a, BIGNUM *b,
298 BN_CTX *ctx))
299
300 # ifndef OPENSSL_NO_EC2M
301 /** Sets the parameter of an ec curve. Synonym for EC_GROUP_set_curve
302 * \param group EC_GROUP object
303 * \param p BIGNUM with the prime number (GFp) or the polynomial
304 * defining the underlying field (GF2m)
305 * \param a BIGNUM with parameter a of the equation
306 * \param b BIGNUM with parameter b of the equation
307 * \param ctx BN_CTX object (optional)
308 * \return 1 on success and 0 if an error occurred
309 */
310 DEPRECATEDIN_3_0(int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p,
311 const BIGNUM *a, const BIGNUM *b,
312 BN_CTX *ctx))
313
314 /** Gets the parameters of an ec curve. Synonym for EC_GROUP_get_curve
315 * \param group EC_GROUP object
316 * \param p BIGNUM with the prime number (GFp) or the polynomial
317 * defining the underlying field (GF2m)
318 * \param a BIGNUM for parameter a of the equation
319 * \param b BIGNUM for parameter b of the equation
320 * \param ctx BN_CTX object (optional)
321 * \return 1 on success and 0 if an error occurred
322 */
323 DEPRECATEDIN_3_0(int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p,
324 BIGNUM *a, BIGNUM *b,
325 BN_CTX *ctx))
326 # endif
327 /** Returns the number of bits needed to represent a field element
328 * \param group EC_GROUP object
329 * \return number of bits needed to represent a field element
330 */
331 int EC_GROUP_get_degree(const EC_GROUP *group);
332
333 /** Checks whether the parameter in the EC_GROUP define a valid ec group
334 * \param group EC_GROUP object
335 * \param ctx BN_CTX object (optional)
336 * \return 1 if group is a valid ec group and 0 otherwise
337 */
338 int EC_GROUP_check(const EC_GROUP *group, BN_CTX *ctx);
339
340 /** Checks whether the discriminant of the elliptic curve is zero or not
341 * \param group EC_GROUP object
342 * \param ctx BN_CTX object (optional)
343 * \return 1 if the discriminant is not zero and 0 otherwise
344 */
345 int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx);
346
347 /** Compares two EC_GROUP objects
348 * \param a first EC_GROUP object
349 * \param b second EC_GROUP object
350 * \param ctx BN_CTX object (optional)
351 * \return 0 if the groups are equal, 1 if not, or -1 on error
352 */
353 int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx);
354
355 /*
356 * EC_GROUP_new_GF*() calls EC_GROUP_new() and EC_GROUP_set_GF*() after
357 * choosing an appropriate EC_METHOD
358 */
359
360 /** Creates a new EC_GROUP object with the specified parameters defined
361 * over GFp (defined by the equation y^2 = x^3 + a*x + b)
362 * \param p BIGNUM with the prime number
363 * \param a BIGNUM with the parameter a of the equation
364 * \param b BIGNUM with the parameter b of the equation
365 * \param ctx BN_CTX object (optional)
366 * \return newly created EC_GROUP object with the specified parameters
367 */
368 EC_GROUP *EC_GROUP_new_curve_GFp(const BIGNUM *p, const BIGNUM *a,
369 const BIGNUM *b, BN_CTX *ctx);
370 # ifndef OPENSSL_NO_EC2M
371 /** Creates a new EC_GROUP object with the specified parameters defined
372 * over GF2m (defined by the equation y^2 + x*y = x^3 + a*x^2 + b)
373 * \param p BIGNUM with the polynomial defining the underlying field
374 * \param a BIGNUM with the parameter a of the equation
375 * \param b BIGNUM with the parameter b of the equation
376 * \param ctx BN_CTX object (optional)
377 * \return newly created EC_GROUP object with the specified parameters
378 */
379 EC_GROUP *EC_GROUP_new_curve_GF2m(const BIGNUM *p, const BIGNUM *a,
380 const BIGNUM *b, BN_CTX *ctx);
381 # endif
382
383 /**
384 * Creates a EC_GROUP object with a curve specified by parameters.
385 * The parameters may be explicit or a named curve,
386 * \param params A list of parameters describing the group.
387 * \param libctx The associated library context or NULL for the default
388 * context
389 * \param propq A property query string
390 * \return newly created EC_GROUP object with specified parameters or NULL
391 * if an error occurred
392 */
393 EC_GROUP *EC_GROUP_new_from_params(const OSSL_PARAM params[],
394 OSSL_LIB_CTX *libctx, const char *propq);
395
396 /**
397 * Creates a EC_GROUP object with a curve specified by a NID
398 * \param libctx The associated library context or NULL for the default
399 * context
400 * \param propq A property query string
401 * \param nid NID of the OID of the curve name
402 * \return newly created EC_GROUP object with specified curve or NULL
403 * if an error occurred
404 */
405 EC_GROUP *EC_GROUP_new_by_curve_name_ex(OSSL_LIB_CTX *libctx, const char *propq,
406 int nid);
407
408 /**
409 * Creates a EC_GROUP object with a curve specified by a NID. Same as
410 * EC_GROUP_new_by_curve_name_ex but the libctx and propq are always
411 * NULL.
412 * \param nid NID of the OID of the curve name
413 * \return newly created EC_GROUP object with specified curve or NULL
414 * if an error occurred
415 */
416 EC_GROUP *EC_GROUP_new_by_curve_name(int nid);
417
418 /** Creates a new EC_GROUP object from an ECPARAMETERS object
419 * \param params pointer to the ECPARAMETERS object
420 * \return newly created EC_GROUP object with specified curve or NULL
421 * if an error occurred
422 */
423 EC_GROUP *EC_GROUP_new_from_ecparameters(const ECPARAMETERS *params);
424
425 /** Creates an ECPARAMETERS object for the given EC_GROUP object.
426 * \param group pointer to the EC_GROUP object
427 * \param params pointer to an existing ECPARAMETERS object or NULL
428 * \return pointer to the new ECPARAMETERS object or NULL
429 * if an error occurred.
430 */
431 ECPARAMETERS *EC_GROUP_get_ecparameters(const EC_GROUP *group,
432 ECPARAMETERS *params);
433
434 /** Creates a new EC_GROUP object from an ECPKPARAMETERS object
435 * \param params pointer to an existing ECPKPARAMETERS object, or NULL
436 * \return newly created EC_GROUP object with specified curve, or NULL
437 * if an error occurred
438 */
439 EC_GROUP *EC_GROUP_new_from_ecpkparameters(const ECPKPARAMETERS *params);
440
441 /** Creates an ECPKPARAMETERS object for the given EC_GROUP object.
442 * \param group pointer to the EC_GROUP object
443 * \param params pointer to an existing ECPKPARAMETERS object or NULL
444 * \return pointer to the new ECPKPARAMETERS object or NULL
445 * if an error occurred.
446 */
447 ECPKPARAMETERS *EC_GROUP_get_ecpkparameters(const EC_GROUP *group,
448 ECPKPARAMETERS *params);
449
450 /********************************************************************/
451 /* handling of internal curves */
452 /********************************************************************/
453
454 typedef struct {
455 int nid;
456 const char *comment;
457 } EC_builtin_curve;
458
459 /*
460 * EC_builtin_curves(EC_builtin_curve *r, size_t size) returns number of all
461 * available curves or zero if a error occurred. In case r is not zero,
462 * nitems EC_builtin_curve structures are filled with the data of the first
463 * nitems internal groups
464 */
465 size_t EC_get_builtin_curves(EC_builtin_curve *r, size_t nitems);
466
467 const char *EC_curve_nid2nist(int nid);
468 int EC_curve_nist2nid(const char *name);
469 int EC_GROUP_check_named_curve(const EC_GROUP *group, int nist_only,
470 BN_CTX *ctx);
471
472 /********************************************************************/
473 /* EC_POINT functions */
474 /********************************************************************/
475
476 /** Creates a new EC_POINT object for the specified EC_GROUP
477 * \param group EC_GROUP the underlying EC_GROUP object
478 * \return newly created EC_POINT object or NULL if an error occurred
479 */
480 EC_POINT *EC_POINT_new(const EC_GROUP *group);
481
482 /** Frees a EC_POINT object
483 * \param point EC_POINT object to be freed
484 */
485 void EC_POINT_free(EC_POINT *point);
486
487 /** Clears and frees a EC_POINT object
488 * \param point EC_POINT object to be cleared and freed
489 */
490 void EC_POINT_clear_free(EC_POINT *point);
491
492 /** Copies EC_POINT object
493 * \param dst destination EC_POINT object
494 * \param src source EC_POINT object
495 * \return 1 on success and 0 if an error occurred
496 */
497 int EC_POINT_copy(EC_POINT *dst, const EC_POINT *src);
498
499 /** Creates a new EC_POINT object and copies the content of the supplied
500 * EC_POINT
501 * \param src source EC_POINT object
502 * \param group underlying the EC_GROUP object
503 * \return newly created EC_POINT object or NULL if an error occurred
504 */
505 EC_POINT *EC_POINT_dup(const EC_POINT *src, const EC_GROUP *group);
506
507 /** Returns the EC_METHOD used in EC_POINT object
508 * \param point EC_POINT object
509 * \return the EC_METHOD used
510 */
511 DEPRECATEDIN_3_0(const EC_METHOD *EC_POINT_method_of(const EC_POINT *point))
512
513 /** Sets a point to infinity (neutral element)
514 * \param group underlying EC_GROUP object
515 * \param point EC_POINT to set to infinity
516 * \return 1 on success and 0 if an error occurred
517 */
518 int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point);
519
520 /** Sets the jacobian projective coordinates of a EC_POINT over GFp
521 * \param group underlying EC_GROUP object
522 * \param p EC_POINT object
523 * \param x BIGNUM with the x-coordinate
524 * \param y BIGNUM with the y-coordinate
525 * \param z BIGNUM with the z-coordinate
526 * \param ctx BN_CTX object (optional)
527 * \return 1 on success and 0 if an error occurred
528 */
529 DEPRECATEDIN_3_0(int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
530 EC_POINT *p, const BIGNUM *x,
531 const BIGNUM *y, const BIGNUM *z,
532 BN_CTX *ctx))
533
534 /** Gets the jacobian projective coordinates of a EC_POINT over GFp
535 * \param group underlying EC_GROUP object
536 * \param p EC_POINT object
537 * \param x BIGNUM for the x-coordinate
538 * \param y BIGNUM for the y-coordinate
539 * \param z BIGNUM for the z-coordinate
540 * \param ctx BN_CTX object (optional)
541 * \return 1 on success and 0 if an error occurred
542 */
543 DEPRECATEDIN_3_0(int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
544 const EC_POINT *p, BIGNUM *x,
545 BIGNUM *y, BIGNUM *z,
546 BN_CTX *ctx))
547
548 /** Sets the affine coordinates of an EC_POINT
549 * \param group underlying EC_GROUP object
550 * \param p EC_POINT object
551 * \param x BIGNUM with the x-coordinate
552 * \param y BIGNUM with the y-coordinate
553 * \param ctx BN_CTX object (optional)
554 * \return 1 on success and 0 if an error occurred
555 */
556 int EC_POINT_set_affine_coordinates(const EC_GROUP *group, EC_POINT *p,
557 const BIGNUM *x, const BIGNUM *y,
558 BN_CTX *ctx);
559
560 /** Gets the affine coordinates of an EC_POINT.
561 * \param group underlying EC_GROUP object
562 * \param p EC_POINT object
563 * \param x BIGNUM for the x-coordinate
564 * \param y BIGNUM for the y-coordinate
565 * \param ctx BN_CTX object (optional)
566 * \return 1 on success and 0 if an error occurred
567 */
568 int EC_POINT_get_affine_coordinates(const EC_GROUP *group, const EC_POINT *p,
569 BIGNUM *x, BIGNUM *y, BN_CTX *ctx);
570
571 /** Sets the affine coordinates of an EC_POINT. A synonym of
572 * EC_POINT_set_affine_coordinates
573 * \param group underlying EC_GROUP object
574 * \param p EC_POINT object
575 * \param x BIGNUM with the x-coordinate
576 * \param y BIGNUM with the y-coordinate
577 * \param ctx BN_CTX object (optional)
578 * \return 1 on success and 0 if an error occurred
579 */
580 DEPRECATEDIN_3_0(int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group,
581 EC_POINT *p,
582 const BIGNUM *x,
583 const BIGNUM *y,
584 BN_CTX *ctx))
585
586 /** Gets the affine coordinates of an EC_POINT. A synonym of
587 * EC_POINT_get_affine_coordinates
588 * \param group underlying EC_GROUP object
589 * \param p EC_POINT object
590 * \param x BIGNUM for the x-coordinate
591 * \param y BIGNUM for the y-coordinate
592 * \param ctx BN_CTX object (optional)
593 * \return 1 on success and 0 if an error occurred
594 */
595 DEPRECATEDIN_3_0(int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group,
596 const EC_POINT *p,
597 BIGNUM *x,
598 BIGNUM *y,
599 BN_CTX *ctx))
600
601 /** Sets the x9.62 compressed coordinates of a EC_POINT
602 * \param group underlying EC_GROUP object
603 * \param p EC_POINT object
604 * \param x BIGNUM with x-coordinate
605 * \param y_bit integer with the y-Bit (either 0 or 1)
606 * \param ctx BN_CTX object (optional)
607 * \return 1 on success and 0 if an error occurred
608 */
609 int EC_POINT_set_compressed_coordinates(const EC_GROUP *group, EC_POINT *p,
610 const BIGNUM *x, int y_bit,
611 BN_CTX *ctx);
612
613 /** Sets the x9.62 compressed coordinates of a EC_POINT. A synonym of
614 * EC_POINT_set_compressed_coordinates
615 * \param group underlying EC_GROUP object
616 * \param p EC_POINT object
617 * \param x BIGNUM with x-coordinate
618 * \param y_bit integer with the y-Bit (either 0 or 1)
619 * \param ctx BN_CTX object (optional)
620 * \return 1 on success and 0 if an error occurred
621 */
622 DEPRECATEDIN_3_0(int EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group,
623 EC_POINT *p,
624 const BIGNUM *x,
625 int y_bit,
626 BN_CTX *ctx))
627 # ifndef OPENSSL_NO_EC2M
628 /** Sets the affine coordinates of an EC_POINT. A synonym of
629 * EC_POINT_set_affine_coordinates
630 * \param group underlying EC_GROUP object
631 * \param p EC_POINT object
632 * \param x BIGNUM with the x-coordinate
633 * \param y BIGNUM with the y-coordinate
634 * \param ctx BN_CTX object (optional)
635 * \return 1 on success and 0 if an error occurred
636 */
637 DEPRECATEDIN_3_0(int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group,
638 EC_POINT *p,
639 const BIGNUM *x,
640 const BIGNUM *y,
641 BN_CTX *ctx))
642
643 /** Gets the affine coordinates of an EC_POINT. A synonym of
644 * EC_POINT_get_affine_coordinates
645 * \param group underlying EC_GROUP object
646 * \param p EC_POINT object
647 * \param x BIGNUM for the x-coordinate
648 * \param y BIGNUM for the y-coordinate
649 * \param ctx BN_CTX object (optional)
650 * \return 1 on success and 0 if an error occurred
651 */
652 DEPRECATEDIN_3_0(int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group,
653 const EC_POINT *p,
654 BIGNUM *x,
655 BIGNUM *y,
656 BN_CTX *ctx))
657
658 /** Sets the x9.62 compressed coordinates of a EC_POINT. A synonym of
659 * EC_POINT_set_compressed_coordinates
660 * \param group underlying EC_GROUP object
661 * \param p EC_POINT object
662 * \param x BIGNUM with x-coordinate
663 * \param y_bit integer with the y-Bit (either 0 or 1)
664 * \param ctx BN_CTX object (optional)
665 * \return 1 on success and 0 if an error occurred
666 */
667 DEPRECATEDIN_3_0(int EC_POINT_set_compressed_coordinates_GF2m(const EC_GROUP *group,
668 EC_POINT *p,
669 const BIGNUM *x,
670 int y_bit,
671 BN_CTX *ctx))
672 # endif
673 /** Encodes a EC_POINT object to a octet string
674 * \param group underlying EC_GROUP object
675 * \param p EC_POINT object
676 * \param form point conversion form
677 * \param buf memory buffer for the result. If NULL the function returns
678 * required buffer size.
679 * \param len length of the memory buffer
680 * \param ctx BN_CTX object (optional)
681 * \return the length of the encoded octet string or 0 if an error occurred
682 */
683 size_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *p,
684 point_conversion_form_t form,
685 unsigned char *buf, size_t len, BN_CTX *ctx);
686
687 /** Decodes a EC_POINT from a octet string
688 * \param group underlying EC_GROUP object
689 * \param p EC_POINT object
690 * \param buf memory buffer with the encoded ec point
691 * \param len length of the encoded ec point
692 * \param ctx BN_CTX object (optional)
693 * \return 1 on success and 0 if an error occurred
694 */
695 int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *p,
696 const unsigned char *buf, size_t len, BN_CTX *ctx);
697
698 /** Encodes an EC_POINT object to an allocated octet string
699 * \param group underlying EC_GROUP object
700 * \param point EC_POINT object
701 * \param form point conversion form
702 * \param pbuf returns pointer to allocated buffer
703 * \param ctx BN_CTX object (optional)
704 * \return the length of the encoded octet string or 0 if an error occurred
705 */
706 size_t EC_POINT_point2buf(const EC_GROUP *group, const EC_POINT *point,
707 point_conversion_form_t form,
708 unsigned char **pbuf, BN_CTX *ctx);
709
710 /* other interfaces to point2oct/oct2point: */
711 # ifndef OPENSSL_NO_DEPRECATED_3_0
712 OSSL_DEPRECATEDIN_3_0 BIGNUM *EC_POINT_point2bn(const EC_GROUP *,
713 const EC_POINT *,
714 point_conversion_form_t form,
715 BIGNUM *, BN_CTX *);
716 OSSL_DEPRECATEDIN_3_0 EC_POINT *EC_POINT_bn2point(const EC_GROUP *,
717 const BIGNUM *,
718 EC_POINT *, BN_CTX *);
719 # endif /* OPENSSL_NO_DEPRECATED_3_0 */
720
721 char *EC_POINT_point2hex(const EC_GROUP *, const EC_POINT *,
722 point_conversion_form_t form, BN_CTX *);
723 EC_POINT *EC_POINT_hex2point(const EC_GROUP *, const char *,
724 EC_POINT *, BN_CTX *);
725
726 /********************************************************************/
727 /* functions for doing EC_POINT arithmetic */
728 /********************************************************************/
729
730 /** Computes the sum of two EC_POINT
731 * \param group underlying EC_GROUP object
732 * \param r EC_POINT object for the result (r = a + b)
733 * \param a EC_POINT object with the first summand
734 * \param b EC_POINT object with the second summand
735 * \param ctx BN_CTX object (optional)
736 * \return 1 on success and 0 if an error occurred
737 */
738 int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
739 const EC_POINT *b, BN_CTX *ctx);
740
741 /** Computes the double of a EC_POINT
742 * \param group underlying EC_GROUP object
743 * \param r EC_POINT object for the result (r = 2 * a)
744 * \param a EC_POINT object
745 * \param ctx BN_CTX object (optional)
746 * \return 1 on success and 0 if an error occurred
747 */
748 int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
749 BN_CTX *ctx);
750
751 /** Computes the inverse of a EC_POINT
752 * \param group underlying EC_GROUP object
753 * \param a EC_POINT object to be inverted (it's used for the result as well)
754 * \param ctx BN_CTX object (optional)
755 * \return 1 on success and 0 if an error occurred
756 */
757 int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx);
758
759 /** Checks whether the point is the neutral element of the group
760 * \param group the underlying EC_GROUP object
761 * \param p EC_POINT object
762 * \return 1 if the point is the neutral element and 0 otherwise
763 */
764 int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *p);
765
766 /** Checks whether the point is on the curve
767 * \param group underlying EC_GROUP object
768 * \param point EC_POINT object to check
769 * \param ctx BN_CTX object (optional)
770 * \return 1 if the point is on the curve, 0 if not, or -1 on error
771 */
772 int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
773 BN_CTX *ctx);
774
775 /** Compares two EC_POINTs
776 * \param group underlying EC_GROUP object
777 * \param a first EC_POINT object
778 * \param b second EC_POINT object
779 * \param ctx BN_CTX object (optional)
780 * \return 1 if the points are not equal, 0 if they are, or -1 on error
781 */
782 int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b,
783 BN_CTX *ctx);
784
785 DEPRECATEDIN_3_0(int EC_POINT_make_affine(const EC_GROUP *group,
786 EC_POINT *point, BN_CTX *ctx))
787 DEPRECATEDIN_3_0(int EC_POINTs_make_affine(const EC_GROUP *group, size_t num,
788 EC_POINT *points[], BN_CTX *ctx))
789
790 /** Computes r = generator * n + sum_{i=0}^{num-1} p[i] * m[i]
791 * \param group underlying EC_GROUP object
792 * \param r EC_POINT object for the result
793 * \param n BIGNUM with the multiplier for the group generator (optional)
794 * \param num number further summands
795 * \param p array of size num of EC_POINT objects
796 * \param m array of size num of BIGNUM objects
797 * \param ctx BN_CTX object (optional)
798 * \return 1 on success and 0 if an error occurred
799 */
800 DEPRECATEDIN_3_0(int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r,
801 const BIGNUM *n, size_t num,
802 const EC_POINT *p[], const BIGNUM *m[],
803 BN_CTX *ctx))
804
805 /** Computes r = generator * n + q * m
806 * \param group underlying EC_GROUP object
807 * \param r EC_POINT object for the result
808 * \param n BIGNUM with the multiplier for the group generator (optional)
809 * \param q EC_POINT object with the first factor of the second summand
810 * \param m BIGNUM with the second factor of the second summand
811 * \param ctx BN_CTX object (optional)
812 * \return 1 on success and 0 if an error occurred
813 */
814 int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *n,
815 const EC_POINT *q, const BIGNUM *m, BN_CTX *ctx);
816
817 /** Stores multiples of generator for faster point multiplication
818 * \param group EC_GROUP object
819 * \param ctx BN_CTX object (optional)
820 * \return 1 on success and 0 if an error occurred
821 */
822 DEPRECATEDIN_3_0(int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx))
823
824 /** Reports whether a precomputation has been done
825 * \param group EC_GROUP object
826 * \return 1 if a pre-computation has been done and 0 otherwise
827 */
828 DEPRECATEDIN_3_0(int EC_GROUP_have_precompute_mult(const EC_GROUP *group))
829
830 /********************************************************************/
831 /* ASN1 stuff */
832 /********************************************************************/
833
834 DECLARE_ASN1_ITEM(ECPKPARAMETERS)
835 DECLARE_ASN1_ALLOC_FUNCTIONS(ECPKPARAMETERS)
836 DECLARE_ASN1_ITEM(ECPARAMETERS)
837 DECLARE_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS)
838
839 /*
840 * EC_GROUP_get_basis_type() returns the NID of the basis type used to
841 * represent the field elements
842 */
843 int EC_GROUP_get_basis_type(const EC_GROUP *);
844 # ifndef OPENSSL_NO_EC2M
845 int EC_GROUP_get_trinomial_basis(const EC_GROUP *, unsigned int *k);
846 int EC_GROUP_get_pentanomial_basis(const EC_GROUP *, unsigned int *k1,
847 unsigned int *k2, unsigned int *k3);
848 # endif
849
850 # define OPENSSL_EC_EXPLICIT_CURVE 0x000
851 # define OPENSSL_EC_NAMED_CURVE 0x001
852
853 EC_GROUP *d2i_ECPKParameters(EC_GROUP **, const unsigned char **in, long len);
854 int i2d_ECPKParameters(const EC_GROUP *, unsigned char **out);
855
856 # define d2i_ECPKParameters_bio(bp,x) \
857 ASN1_d2i_bio_of(EC_GROUP, NULL, d2i_ECPKParameters, bp, x)
858 # define i2d_ECPKParameters_bio(bp,x) \
859 ASN1_i2d_bio_of(EC_GROUP, i2d_ECPKParameters, bp, x)
860 # define d2i_ECPKParameters_fp(fp,x) \
861 (EC_GROUP *)ASN1_d2i_fp(NULL, (char *(*)())d2i_ECPKParameters, (fp), \
862 (unsigned char **)(x))
863 # define i2d_ECPKParameters_fp(fp,x) \
864 ASN1_i2d_fp(i2d_ECPKParameters,(fp), (unsigned char *)(x))
865
866 int ECPKParameters_print(BIO *bp, const EC_GROUP *x, int off);
867 # ifndef OPENSSL_NO_STDIO
868 int ECPKParameters_print_fp(FILE *fp, const EC_GROUP *x, int off);
869 # endif
870
871 /********************************************************************/
872 /* EC_KEY functions */
873 /********************************************************************/
874
875 /* some values for the encoding_flag */
876 # define EC_PKEY_NO_PARAMETERS 0x001
877 # define EC_PKEY_NO_PUBKEY 0x002
878
879 /* some values for the flags field */
880 # define EC_FLAG_NON_FIPS_ALLOW 0x1
881 # define EC_FLAG_FIPS_CHECKED 0x2
882 # define EC_FLAG_COFACTOR_ECDH 0x1000
883 # define EC_FLAG_SM2_RANGE 0x4
884
885 /**
886 * Creates a new EC_KEY object.
887 * \param ctx The library context for to use for this EC_KEY. May be NULL in
888 * which case the default library context is used.
889 * \return EC_KEY object or NULL if an error occurred.
890 */
891 EC_KEY *EC_KEY_new_ex(OSSL_LIB_CTX *ctx, const char *propq);
892
893 /**
894 * Creates a new EC_KEY object. Same as calling EC_KEY_new_ex with a
895 * NULL library context
896 * \return EC_KEY object or NULL if an error occurred.
897 */
898 EC_KEY *EC_KEY_new(void);
899
900 int EC_KEY_get_flags(const EC_KEY *key);
901
902 void EC_KEY_set_flags(EC_KEY *key, int flags);
903
904 void EC_KEY_clear_flags(EC_KEY *key, int flags);
905
906 int EC_KEY_decoded_from_explicit_params(const EC_KEY *key);
907
908 /**
909 * Creates a new EC_KEY object using a named curve as underlying
910 * EC_GROUP object.
911 * \param ctx The library context for to use for this EC_KEY. May be NULL in
912 * which case the default library context is used.
913 * \param propq Any property query string
914 * \param nid NID of the named curve.
915 * \return EC_KEY object or NULL if an error occurred.
916 */
917 EC_KEY *EC_KEY_new_by_curve_name_ex(OSSL_LIB_CTX *ctx, const char *propq,
918 int nid);
919
920 /**
921 * Creates a new EC_KEY object using a named curve as underlying
922 * EC_GROUP object. Same as calling EC_KEY_new_by_curve_name_ex with a NULL
923 * library context and property query string.
924 * \param nid NID of the named curve.
925 * \return EC_KEY object or NULL if an error occurred.
926 */
927 EC_KEY *EC_KEY_new_by_curve_name(int nid);
928
929
930 /** Frees a EC_KEY object.
931 * \param key EC_KEY object to be freed.
932 */
933 void EC_KEY_free(EC_KEY *key);
934
935 /** Copies a EC_KEY object.
936 * \param dst destination EC_KEY object
937 * \param src src EC_KEY object
938 * \return dst or NULL if an error occurred.
939 */
940 EC_KEY *EC_KEY_copy(EC_KEY *dst, const EC_KEY *src);
941
942 /** Creates a new EC_KEY object and copies the content from src to it.
943 * \param src the source EC_KEY object
944 * \return newly created EC_KEY object or NULL if an error occurred.
945 */
946 EC_KEY *EC_KEY_dup(const EC_KEY *src);
947
948 /** Increases the internal reference count of a EC_KEY object.
949 * \param key EC_KEY object
950 * \return 1 on success and 0 if an error occurred.
951 */
952 int EC_KEY_up_ref(EC_KEY *key);
953
954 /** Returns the ENGINE object of a EC_KEY object
955 * \param eckey EC_KEY object
956 * \return the ENGINE object (possibly NULL).
957 */
958 ENGINE *EC_KEY_get0_engine(const EC_KEY *eckey);
959
960 /** Returns the EC_GROUP object of a EC_KEY object
961 * \param key EC_KEY object
962 * \return the EC_GROUP object (possibly NULL).
963 */
964 const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key);
965
966 /** Sets the EC_GROUP of a EC_KEY object.
967 * \param key EC_KEY object
968 * \param group EC_GROUP to use in the EC_KEY object (note: the EC_KEY
969 * object will use an own copy of the EC_GROUP).
970 * \return 1 on success and 0 if an error occurred.
971 */
972 int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group);
973
974 /** Returns the private key of a EC_KEY object.
975 * \param key EC_KEY object
976 * \return a BIGNUM with the private key (possibly NULL).
977 */
978 const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key);
979
980 /** Sets the private key of a EC_KEY object.
981 * \param key EC_KEY object
982 * \param prv BIGNUM with the private key (note: the EC_KEY object
983 * will use an own copy of the BIGNUM).
984 * \return 1 on success and 0 if an error occurred.
985 */
986 int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *prv);
987
988 /** Returns the public key of a EC_KEY object.
989 * \param key the EC_KEY object
990 * \return a EC_POINT object with the public key (possibly NULL)
991 */
992 const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key);
993
994 /** Sets the public key of a EC_KEY object.
995 * \param key EC_KEY object
996 * \param pub EC_POINT object with the public key (note: the EC_KEY object
997 * will use an own copy of the EC_POINT object).
998 * \return 1 on success and 0 if an error occurred.
999 */
1000 int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub);
1001
1002 unsigned EC_KEY_get_enc_flags(const EC_KEY *key);
1003 void EC_KEY_set_enc_flags(EC_KEY *eckey, unsigned int flags);
1004 point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key);
1005 void EC_KEY_set_conv_form(EC_KEY *eckey, point_conversion_form_t cform);
1006
1007 # define EC_KEY_get_ex_new_index(l, p, newf, dupf, freef) \
1008 CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_EC_KEY, l, p, newf, dupf, freef)
1009 int EC_KEY_set_ex_data(EC_KEY *key, int idx, void *arg);
1010 void *EC_KEY_get_ex_data(const EC_KEY *key, int idx);
1011
1012 /* wrapper functions for the underlying EC_GROUP object */
1013 void EC_KEY_set_asn1_flag(EC_KEY *eckey, int asn1_flag);
1014
1015 /** Creates a table of pre-computed multiples of the generator to
1016 * accelerate further EC_KEY operations.
1017 * \param key EC_KEY object
1018 * \param ctx BN_CTX object (optional)
1019 * \return 1 on success and 0 if an error occurred.
1020 */
1021 DEPRECATEDIN_3_0(int EC_KEY_precompute_mult(EC_KEY *key, BN_CTX *ctx))
1022
1023 /** Creates a new ec private (and optional a new public) key.
1024 * \param key EC_KEY object
1025 * \return 1 on success and 0 if an error occurred.
1026 */
1027 int EC_KEY_generate_key(EC_KEY *key);
1028
1029 /** Verifies that a private and/or public key is valid.
1030 * \param key the EC_KEY object
1031 * \return 1 on success and 0 otherwise.
1032 */
1033 int EC_KEY_check_key(const EC_KEY *key);
1034
1035 /** Indicates if an EC_KEY can be used for signing.
1036 * \param eckey the EC_KEY object
1037 * \return 1 if can can sign and 0 otherwise.
1038 */
1039 int EC_KEY_can_sign(const EC_KEY *eckey);
1040
1041 /** Sets a public key from affine coordinates performing
1042 * necessary NIST PKV tests.
1043 * \param key the EC_KEY object
1044 * \param x public key x coordinate
1045 * \param y public key y coordinate
1046 * \return 1 on success and 0 otherwise.
1047 */
1048 int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x,
1049 BIGNUM *y);
1050
1051 /** Encodes an EC_KEY public key to an allocated octet string
1052 * \param key key to encode
1053 * \param form point conversion form
1054 * \param pbuf returns pointer to allocated buffer
1055 * \param ctx BN_CTX object (optional)
1056 * \return the length of the encoded octet string or 0 if an error occurred
1057 */
1058 size_t EC_KEY_key2buf(const EC_KEY *key, point_conversion_form_t form,
1059 unsigned char **pbuf, BN_CTX *ctx);
1060
1061 /** Decodes a EC_KEY public key from a octet string
1062 * \param key key to decode
1063 * \param buf memory buffer with the encoded ec point
1064 * \param len length of the encoded ec point
1065 * \param ctx BN_CTX object (optional)
1066 * \return 1 on success and 0 if an error occurred
1067 */
1068
1069 int EC_KEY_oct2key(EC_KEY *key, const unsigned char *buf, size_t len,
1070 BN_CTX *ctx);
1071
1072 /** Decodes an EC_KEY private key from an octet string
1073 * \param key key to decode
1074 * \param buf memory buffer with the encoded private key
1075 * \param len length of the encoded key
1076 * \return 1 on success and 0 if an error occurred
1077 */
1078
1079 int EC_KEY_oct2priv(EC_KEY *key, const unsigned char *buf, size_t len);
1080
1081 /** Encodes a EC_KEY private key to an octet string
1082 * \param key key to encode
1083 * \param buf memory buffer for the result. If NULL the function returns
1084 * required buffer size.
1085 * \param len length of the memory buffer
1086 * \return the length of the encoded octet string or 0 if an error occurred
1087 */
1088
1089 size_t EC_KEY_priv2oct(const EC_KEY *key, unsigned char *buf, size_t len);
1090
1091 /** Encodes an EC_KEY private key to an allocated octet string
1092 * \param eckey key to encode
1093 * \param pbuf returns pointer to allocated buffer
1094 * \return the length of the encoded octet string or 0 if an error occurred
1095 */
1096 size_t EC_KEY_priv2buf(const EC_KEY *eckey, unsigned char **pbuf);
1097
1098 /********************************************************************/
1099 /* de- and encoding functions for SEC1 ECPrivateKey */
1100 /********************************************************************/
1101
1102 /** Decodes a private key from a memory buffer.
1103 * \param key a pointer to a EC_KEY object which should be used (or NULL)
1104 * \param in pointer to memory with the DER encoded private key
1105 * \param len length of the DER encoded private key
1106 * \return the decoded private key or NULL if an error occurred.
1107 */
1108 EC_KEY *d2i_ECPrivateKey(EC_KEY **key, const unsigned char **in, long len);
1109
1110 /** Encodes a private key object and stores the result in a buffer.
1111 * \param key the EC_KEY object to encode
1112 * \param out the buffer for the result (if NULL the function returns number
1113 * of bytes needed).
1114 * \return 1 on success and 0 if an error occurred.
1115 */
1116 int i2d_ECPrivateKey(const EC_KEY *key, unsigned char **out);
1117
1118 /********************************************************************/
1119 /* de- and encoding functions for EC parameters */
1120 /********************************************************************/
1121
1122 /** Decodes ec parameter from a memory buffer.
1123 * \param key a pointer to a EC_KEY object which should be used (or NULL)
1124 * \param in pointer to memory with the DER encoded ec parameters
1125 * \param len length of the DER encoded ec parameters
1126 * \return a EC_KEY object with the decoded parameters or NULL if an error
1127 * occurred.
1128 */
1129 EC_KEY *d2i_ECParameters(EC_KEY **key, const unsigned char **in, long len);
1130
1131 /** Encodes ec parameter and stores the result in a buffer.
1132 * \param key the EC_KEY object with ec parameters to encode
1133 * \param out the buffer for the result (if NULL the function returns number
1134 * of bytes needed).
1135 * \return 1 on success and 0 if an error occurred.
1136 */
1137 int i2d_ECParameters(const EC_KEY *key, unsigned char **out);
1138
1139 /********************************************************************/
1140 /* de- and encoding functions for EC public key */
1141 /* (octet string, not DER -- hence 'o2i' and 'i2o') */
1142 /********************************************************************/
1143
1144 /** Decodes a ec public key from a octet string.
1145 * \param key a pointer to a EC_KEY object which should be used
1146 * \param in memory buffer with the encoded public key
1147 * \param len length of the encoded public key
1148 * \return EC_KEY object with decoded public key or NULL if an error
1149 * occurred.
1150 */
1151 EC_KEY *o2i_ECPublicKey(EC_KEY **key, const unsigned char **in, long len);
1152
1153 /** Encodes a ec public key in an octet string.
1154 * \param key the EC_KEY object with the public key
1155 * \param out the buffer for the result (if NULL the function returns number
1156 * of bytes needed).
1157 * \return 1 on success and 0 if an error occurred
1158 */
1159 int i2o_ECPublicKey(const EC_KEY *key, unsigned char **out);
1160
1161 /** Prints out the ec parameters on human readable form.
1162 * \param bp BIO object to which the information is printed
1163 * \param key EC_KEY object
1164 * \return 1 on success and 0 if an error occurred
1165 */
1166 int ECParameters_print(BIO *bp, const EC_KEY *key);
1167
1168 /** Prints out the contents of a EC_KEY object
1169 * \param bp BIO object to which the information is printed
1170 * \param key EC_KEY object
1171 * \param off line offset
1172 * \return 1 on success and 0 if an error occurred
1173 */
1174 int EC_KEY_print(BIO *bp, const EC_KEY *key, int off);
1175
1176 # ifndef OPENSSL_NO_STDIO
1177 /** Prints out the ec parameters on human readable form.
1178 * \param fp file descriptor to which the information is printed
1179 * \param key EC_KEY object
1180 * \return 1 on success and 0 if an error occurred
1181 */
1182 int ECParameters_print_fp(FILE *fp, const EC_KEY *key);
1183
1184 /** Prints out the contents of a EC_KEY object
1185 * \param fp file descriptor to which the information is printed
1186 * \param key EC_KEY object
1187 * \param off line offset
1188 * \return 1 on success and 0 if an error occurred
1189 */
1190 int EC_KEY_print_fp(FILE *fp, const EC_KEY *key, int off);
1191
1192 # endif
1193
1194 const EC_KEY_METHOD *EC_KEY_OpenSSL(void);
1195 const EC_KEY_METHOD *EC_KEY_get_default_method(void);
1196 void EC_KEY_set_default_method(const EC_KEY_METHOD *meth);
1197 const EC_KEY_METHOD *EC_KEY_get_method(const EC_KEY *key);
1198 int EC_KEY_set_method(EC_KEY *key, const EC_KEY_METHOD *meth);
1199 EC_KEY *EC_KEY_new_method(ENGINE *engine);
1200
1201 /** The old name for ecdh_KDF_X9_63
1202 * The ECDH KDF specification has been mistakingly attributed to ANSI X9.62,
1203 * it is actually specified in ANSI X9.63.
1204 * This identifier is retained for backwards compatibility
1205 */
1206 DEPRECATEDIN_3_0(int ECDH_KDF_X9_62(unsigned char *out, size_t outlen,
1207 const unsigned char *Z, size_t Zlen,
1208 const unsigned char *sinfo, size_t sinfolen,
1209 const EVP_MD *md))
1210
1211 DEPRECATEDIN_3_0(int ECDH_compute_key(void *out, size_t outlen,
1212 const EC_POINT *pub_key,
1213 const EC_KEY *ecdh,
1214 void *(*KDF)(const void *in, size_t inlen,
1215 void *out, size_t *outlen)))
1216
1217 typedef struct ECDSA_SIG_st ECDSA_SIG;
1218
1219 /** Allocates and initialize a ECDSA_SIG structure
1220 * \return pointer to a ECDSA_SIG structure or NULL if an error occurred
1221 */
1222 ECDSA_SIG *ECDSA_SIG_new(void);
1223
1224 /** frees a ECDSA_SIG structure
1225 * \param sig pointer to the ECDSA_SIG structure
1226 */
1227 void ECDSA_SIG_free(ECDSA_SIG *sig);
1228
1229 /** i2d_ECDSA_SIG encodes content of ECDSA_SIG (note: this function modifies *pp
1230 * (*pp += length of the DER encoded signature)).
1231 * \param sig pointer to the ECDSA_SIG object
1232 * \param pp pointer to a unsigned char pointer for the output or NULL
1233 * \return the length of the DER encoded ECDSA_SIG object or a negative value
1234 * on error
1235 */
1236 DECLARE_ASN1_ENCODE_FUNCTIONS_only(ECDSA_SIG, ECDSA_SIG)
1237
1238 /** d2i_ECDSA_SIG decodes an ECDSA signature (note: this function modifies *pp
1239 * (*pp += len)).
1240 * \param sig pointer to ECDSA_SIG pointer (may be NULL)
1241 * \param pp memory buffer with the DER encoded signature
1242 * \param len length of the buffer
1243 * \return pointer to the decoded ECDSA_SIG structure (or NULL)
1244 */
1245
1246 /** Accessor for r and s fields of ECDSA_SIG
1247 * \param sig pointer to ECDSA_SIG structure
1248 * \param pr pointer to BIGNUM pointer for r (may be NULL)
1249 * \param ps pointer to BIGNUM pointer for s (may be NULL)
1250 */
1251 void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps);
1252
1253 /** Accessor for r field of ECDSA_SIG
1254 * \param sig pointer to ECDSA_SIG structure
1255 */
1256 const BIGNUM *ECDSA_SIG_get0_r(const ECDSA_SIG *sig);
1257
1258 /** Accessor for s field of ECDSA_SIG
1259 * \param sig pointer to ECDSA_SIG structure
1260 */
1261 const BIGNUM *ECDSA_SIG_get0_s(const ECDSA_SIG *sig);
1262
1263 /** Setter for r and s fields of ECDSA_SIG
1264 * \param sig pointer to ECDSA_SIG structure
1265 * \param r pointer to BIGNUM for r (may be NULL)
1266 * \param s pointer to BIGNUM for s (may be NULL)
1267 */
1268 int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s);
1269
1270 /** Computes the ECDSA signature of the given hash value using
1271 * the supplied private key and returns the created signature.
1272 * \param dgst pointer to the hash value
1273 * \param dgst_len length of the hash value
1274 * \param eckey EC_KEY object containing a private EC key
1275 * \return pointer to a ECDSA_SIG structure or NULL if an error occurred
1276 */
1277 DEPRECATEDIN_3_0(ECDSA_SIG *ECDSA_do_sign(const unsigned char *dgst,
1278 int dgst_len, EC_KEY *eckey))
1279
1280 /** Computes ECDSA signature of a given hash value using the supplied
1281 * private key (note: sig must point to ECDSA_size(eckey) bytes of memory).
1282 * \param dgst pointer to the hash value to sign
1283 * \param dgstlen length of the hash value
1284 * \param kinv BIGNUM with a pre-computed inverse k (optional)
1285 * \param rp BIGNUM with a pre-computed rp value (optional),
1286 * see ECDSA_sign_setup
1287 * \param eckey EC_KEY object containing a private EC key
1288 * \return pointer to a ECDSA_SIG structure or NULL if an error occurred
1289 */
1290 DEPRECATEDIN_3_0(ECDSA_SIG *ECDSA_do_sign_ex(const unsigned char *dgst,
1291 int dgstlen, const BIGNUM *kinv,
1292 const BIGNUM *rp, EC_KEY *eckey))
1293
1294 /** Verifies that the supplied signature is a valid ECDSA
1295 * signature of the supplied hash value using the supplied public key.
1296 * \param dgst pointer to the hash value
1297 * \param dgst_len length of the hash value
1298 * \param sig ECDSA_SIG structure
1299 * \param eckey EC_KEY object containing a public EC key
1300 * \return 1 if the signature is valid, 0 if the signature is invalid
1301 * and -1 on error
1302 */
1303 DEPRECATEDIN_3_0(int ECDSA_do_verify(const unsigned char *dgst, int dgst_len,
1304 const ECDSA_SIG *sig, EC_KEY *eckey))
1305
1306 /** Precompute parts of the signing operation
1307 * \param eckey EC_KEY object containing a private EC key
1308 * \param ctx BN_CTX object (optional)
1309 * \param kinv BIGNUM pointer for the inverse of k
1310 * \param rp BIGNUM pointer for x coordinate of k * generator
1311 * \return 1 on success and 0 otherwise
1312 */
1313 DEPRECATEDIN_3_0(int ECDSA_sign_setup(EC_KEY *eckey, BN_CTX *ctx,
1314 BIGNUM **kinv, BIGNUM **rp))
1315
1316 /** Computes ECDSA signature of a given hash value using the supplied
1317 * private key (note: sig must point to ECDSA_size(eckey) bytes of memory).
1318 * \param type this parameter is ignored
1319 * \param dgst pointer to the hash value to sign
1320 * \param dgstlen length of the hash value
1321 * \param sig memory for the DER encoded created signature
1322 * \param siglen pointer to the length of the returned signature
1323 * \param eckey EC_KEY object containing a private EC key
1324 * \return 1 on success and 0 otherwise
1325 */
1326 DEPRECATEDIN_3_0(int ECDSA_sign(int type, const unsigned char *dgst,
1327 int dgstlen, unsigned char *sig,
1328 unsigned int *siglen, EC_KEY *eckey))
1329
1330 /** Computes ECDSA signature of a given hash value using the supplied
1331 * private key (note: sig must point to ECDSA_size(eckey) bytes of memory).
1332 * \param type this parameter is ignored
1333 * \param dgst pointer to the hash value to sign
1334 * \param dgstlen length of the hash value
1335 * \param sig buffer to hold the DER encoded signature
1336 * \param siglen pointer to the length of the returned signature
1337 * \param kinv BIGNUM with a pre-computed inverse k (optional)
1338 * \param rp BIGNUM with a pre-computed rp value (optional),
1339 * see ECDSA_sign_setup
1340 * \param eckey EC_KEY object containing a private EC key
1341 * \return 1 on success and 0 otherwise
1342 */
1343 DEPRECATEDIN_3_0(int ECDSA_sign_ex(int type, const unsigned char *dgst,
1344 int dgstlen, unsigned char *sig,
1345 unsigned int *siglen, const BIGNUM *kinv,
1346 const BIGNUM *rp, EC_KEY *eckey))
1347
1348 /** Verifies that the given signature is valid ECDSA signature
1349 * of the supplied hash value using the specified public key.
1350 * \param type this parameter is ignored
1351 * \param dgst pointer to the hash value
1352 * \param dgstlen length of the hash value
1353 * \param sig pointer to the DER encoded signature
1354 * \param siglen length of the DER encoded signature
1355 * \param eckey EC_KEY object containing a public EC key
1356 * \return 1 if the signature is valid, 0 if the signature is invalid
1357 * and -1 on error
1358 */
1359 DEPRECATEDIN_3_0(int ECDSA_verify(int type, const unsigned char *dgst,
1360 int dgstlen, const unsigned char *sig,
1361 int siglen, EC_KEY *eckey))
1362
1363 /** Returns the maximum length of the DER encoded signature
1364 * \param eckey EC_KEY object
1365 * \return numbers of bytes required for the DER encoded signature
1366 */
1367 DEPRECATEDIN_3_0(int ECDSA_size(const EC_KEY *eckey))
1368
1369 /********************************************************************/
1370 /* EC_KEY_METHOD constructors, destructors, writers and accessors */
1371 /********************************************************************/
1372
1373 DEPRECATEDIN_3_0(EC_KEY_METHOD *EC_KEY_METHOD_new(const EC_KEY_METHOD *meth))
1374 DEPRECATEDIN_3_0(void EC_KEY_METHOD_free(EC_KEY_METHOD *meth))
1375 DEPRECATEDIN_3_0(void EC_KEY_METHOD_set_init
1376 (EC_KEY_METHOD *meth,
1377 int (*init)(EC_KEY *key),
1378 void (*finish)(EC_KEY *key),
1379 int (*copy)(EC_KEY *dest, const EC_KEY *src),
1380 int (*set_group)(EC_KEY *key, const EC_GROUP *grp),
1381 int (*set_private)(EC_KEY *key,
1382 const BIGNUM *priv_key),
1383 int (*set_public)(EC_KEY *key,
1384 const EC_POINT *pub_key)))
1385
1386 DEPRECATEDIN_3_0(void EC_KEY_METHOD_set_keygen(EC_KEY_METHOD *meth,
1387 int (*keygen)(EC_KEY *key)))
1388
1389 DEPRECATEDIN_3_0(void EC_KEY_METHOD_set_compute_key
1390 (EC_KEY_METHOD *meth,
1391 int (*ckey)(unsigned char **psec,
1392 size_t *pseclen,
1393 const EC_POINT *pub_key,
1394 const EC_KEY *ecdh)))
1395
1396 DEPRECATEDIN_3_0(void EC_KEY_METHOD_set_sign
1397 (EC_KEY_METHOD *meth,
1398 int (*sign)(int type, const unsigned char *dgst,
1399 int dlen, unsigned char *sig,
1400 unsigned int *siglen,
1401 const BIGNUM *kinv, const BIGNUM *r,
1402 EC_KEY *eckey),
1403 int (*sign_setup)(EC_KEY *eckey, BN_CTX *ctx_in,
1404 BIGNUM **kinvp, BIGNUM **rp),
1405 ECDSA_SIG *(*sign_sig)(const unsigned char *dgst,
1406 int dgst_len,
1407 const BIGNUM *in_kinv,
1408 const BIGNUM *in_r,
1409 EC_KEY *eckey)))
1410
1411 DEPRECATEDIN_3_0(void EC_KEY_METHOD_set_verify
1412 (EC_KEY_METHOD *meth,
1413 int (*verify)(int type, const unsigned
1414 char *dgst, int dgst_len,
1415 const unsigned char *sigbuf,
1416 int sig_len, EC_KEY *eckey),
1417 int (*verify_sig)(const unsigned char *dgst,
1418 int dgst_len,
1419 const ECDSA_SIG *sig,
1420 EC_KEY *eckey)))
1421
1422 DEPRECATEDIN_3_0(void EC_KEY_METHOD_get_init
1423 (const EC_KEY_METHOD *meth,
1424 int (**pinit)(EC_KEY *key),
1425 void (**pfinish)(EC_KEY *key),
1426 int (**pcopy)(EC_KEY *dest, const EC_KEY *src),
1427 int (**pset_group)(EC_KEY *key,
1428 const EC_GROUP *grp),
1429 int (**pset_private)(EC_KEY *key,
1430 const BIGNUM *priv_key),
1431 int (**pset_public)(EC_KEY *key,
1432 const EC_POINT *pub_key)))
1433
1434 DEPRECATEDIN_3_0(void EC_KEY_METHOD_get_keygen(const EC_KEY_METHOD *meth,
1435 int (**pkeygen)(EC_KEY *key)))
1436
1437 DEPRECATEDIN_3_0(void EC_KEY_METHOD_get_compute_key
1438 (const EC_KEY_METHOD *meth,
1439 int (**pck)(unsigned char **psec,
1440 size_t *pseclen,
1441 const EC_POINT *pub_key,
1442 const EC_KEY *ecdh)))
1443
1444 DEPRECATEDIN_3_0(void EC_KEY_METHOD_get_sign
1445 (const EC_KEY_METHOD *meth,
1446 int (**psign)(int type, const unsigned char *dgst,
1447 int dlen, unsigned char *sig,
1448 unsigned int *siglen,
1449 const BIGNUM *kinv, const BIGNUM *r,
1450 EC_KEY *eckey),
1451 int (**psign_setup)(EC_KEY *eckey, BN_CTX *ctx_in,
1452 BIGNUM **kinvp, BIGNUM **rp),
1453 ECDSA_SIG *(**psign_sig)(const unsigned char *dgst,
1454 int dgst_len,
1455 const BIGNUM *in_kinv,
1456 const BIGNUM *in_r,
1457 EC_KEY *eckey)))
1458
1459 DEPRECATEDIN_3_0(void EC_KEY_METHOD_get_verify
1460 (const EC_KEY_METHOD *meth,
1461 int (**pverify)(int type, const unsigned
1462 char *dgst, int dgst_len,
1463 const unsigned char *sigbuf,
1464 int sig_len, EC_KEY *eckey),
1465 int (**pverify_sig)(const unsigned char *dgst,
1466 int dgst_len,
1467 const ECDSA_SIG *sig,
1468 EC_KEY *eckey)))
1469
1470 # define ECParameters_dup(x) ASN1_dup_of(EC_KEY, i2d_ECParameters, \
1471 d2i_ECParameters, x)
1472
1473 # ifndef __cplusplus
1474 # if defined(__SUNPRO_C)
1475 # if __SUNPRO_C >= 0x520
1476 # pragma error_messages (default,E_ARRAY_OF_INCOMPLETE_NONAME,E_ARRAY_OF_INCOMPLETE)
1477 # endif
1478 # endif
1479 # endif
1480
1481 int EVP_PKEY_CTX_set_ec_paramgen_curve_nid(EVP_PKEY_CTX *ctx, int nid);
1482 int EVP_PKEY_CTX_set_ec_param_enc(EVP_PKEY_CTX *ctx, int param_enc);
1483 int EVP_PKEY_CTX_set_ecdh_cofactor_mode(EVP_PKEY_CTX *ctx, int cofactor_mode);
1484 int EVP_PKEY_CTX_get_ecdh_cofactor_mode(EVP_PKEY_CTX *ctx);
1485
1486 int EVP_PKEY_CTX_set_ecdh_kdf_type(EVP_PKEY_CTX *ctx, int kdf);
1487 int EVP_PKEY_CTX_get_ecdh_kdf_type(EVP_PKEY_CTX *ctx);
1488
1489 int EVP_PKEY_CTX_set_ecdh_kdf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md);
1490 int EVP_PKEY_CTX_get_ecdh_kdf_md(EVP_PKEY_CTX *ctx, const EVP_MD **md);
1491
1492 int EVP_PKEY_CTX_set_ecdh_kdf_outlen(EVP_PKEY_CTX *ctx, int len);
1493 int EVP_PKEY_CTX_get_ecdh_kdf_outlen(EVP_PKEY_CTX *ctx, int *len);
1494
1495 int EVP_PKEY_CTX_set0_ecdh_kdf_ukm(EVP_PKEY_CTX *ctx, unsigned char *ukm,
1496 int len);
1497 int EVP_PKEY_CTX_get0_ecdh_kdf_ukm(EVP_PKEY_CTX *ctx, unsigned char **ukm);
1498
1499 # define EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID (EVP_PKEY_ALG_CTRL + 1)
1500 # define EVP_PKEY_CTRL_EC_PARAM_ENC (EVP_PKEY_ALG_CTRL + 2)
1501 # define EVP_PKEY_CTRL_EC_ECDH_COFACTOR (EVP_PKEY_ALG_CTRL + 3)
1502 # define EVP_PKEY_CTRL_EC_KDF_TYPE (EVP_PKEY_ALG_CTRL + 4)
1503 # define EVP_PKEY_CTRL_EC_KDF_MD (EVP_PKEY_ALG_CTRL + 5)
1504 # define EVP_PKEY_CTRL_GET_EC_KDF_MD (EVP_PKEY_ALG_CTRL + 6)
1505 # define EVP_PKEY_CTRL_EC_KDF_OUTLEN (EVP_PKEY_ALG_CTRL + 7)
1506 # define EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN (EVP_PKEY_ALG_CTRL + 8)
1507 # define EVP_PKEY_CTRL_EC_KDF_UKM (EVP_PKEY_ALG_CTRL + 9)
1508 # define EVP_PKEY_CTRL_GET_EC_KDF_UKM (EVP_PKEY_ALG_CTRL + 10)
1509
1510 /* KDF types */
1511 # define EVP_PKEY_ECDH_KDF_NONE 1
1512 # define EVP_PKEY_ECDH_KDF_X9_63 2
1513 /** The old name for EVP_PKEY_ECDH_KDF_X9_63
1514 * The ECDH KDF specification has been mistakingly attributed to ANSI X9.62,
1515 * it is actually specified in ANSI X9.63.
1516 * This identifier is retained for backwards compatibility
1517 */
1518 # define EVP_PKEY_ECDH_KDF_X9_62 EVP_PKEY_ECDH_KDF_X9_63
1519
1520 # ifdef __cplusplus
1521 }
1522 # endif
1523 # endif
1524 #endif