2 * Copyright 2002-2018 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
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
14 # include <openssl/opensslconf.h>
16 # ifndef OPENSSL_NO_EC
17 # include <openssl/asn1.h>
18 # include <openssl/symhacks.h>
19 # if !OPENSSL_API_1_1_0
20 # include <openssl/bn.h>
22 # include <openssl/ecerr.h>
27 # ifndef OPENSSL_ECC_MAX_FIELD_BITS
28 # define OPENSSL_ECC_MAX_FIELD_BITS 661
31 /** Enum for the point conversion form as defined in X9.62 (ECDSA)
32 * for the encoding of a elliptic curve point (x,y) */
34 /** the point is encoded as z||x, where the octet z specifies
35 * which solution of the quadratic equation y is */
36 POINT_CONVERSION_COMPRESSED = 2,
37 /** the point is encoded as z||x||y, where z is the octet 0x04 */
38 POINT_CONVERSION_UNCOMPRESSED = 4,
39 /** the point is encoded as z||x||y, where the octet z specifies
40 * which solution of the quadratic equation y is */
41 POINT_CONVERSION_HYBRID = 6
42 } point_conversion_form_t;
44 typedef struct ec_method_st EC_METHOD;
45 typedef struct ec_group_st EC_GROUP;
46 typedef struct ec_point_st EC_POINT;
47 typedef struct ecpk_parameters_st ECPKPARAMETERS;
48 typedef struct ec_parameters_st ECPARAMETERS;
50 /********************************************************************/
51 /* EC_METHODs for curves over GF(p) */
52 /********************************************************************/
54 /** Returns the basic GFp ec methods which provides the basis for the
56 * \return EC_METHOD object
58 const EC_METHOD *EC_GFp_simple_method(void);
60 /** Returns GFp methods using montgomery multiplication.
61 * \return EC_METHOD object
63 const EC_METHOD *EC_GFp_mont_method(void);
65 /** Returns GFp methods using optimized methods for NIST recommended curves
66 * \return EC_METHOD object
68 const EC_METHOD *EC_GFp_nist_method(void);
70 # ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
71 /** Returns 64-bit optimized methods for nistp224
72 * \return EC_METHOD object
74 const EC_METHOD *EC_GFp_nistp224_method(void);
76 /** Returns 64-bit optimized methods for nistp256
77 * \return EC_METHOD object
79 const EC_METHOD *EC_GFp_nistp256_method(void);
81 /** Returns 64-bit optimized methods for nistp521
82 * \return EC_METHOD object
84 const EC_METHOD *EC_GFp_nistp521_method(void);
87 # ifndef OPENSSL_NO_EC2M
88 /********************************************************************/
89 /* EC_METHOD for curves over GF(2^m) */
90 /********************************************************************/
92 /** Returns the basic GF2m ec method
93 * \return EC_METHOD object
95 const EC_METHOD *EC_GF2m_simple_method(void);
99 /********************************************************************/
100 /* EC_GROUP functions */
101 /********************************************************************/
103 /** Creates a new EC_GROUP object
104 * \param meth EC_METHOD to use
105 * \return newly created EC_GROUP object or NULL in case of an error.
107 EC_GROUP *EC_GROUP_new(const EC_METHOD *meth);
109 /** Frees a EC_GROUP object
110 * \param group EC_GROUP object to be freed.
112 void EC_GROUP_free(EC_GROUP *group);
114 /** Clears and frees a EC_GROUP object
115 * \param group EC_GROUP object to be cleared and freed.
117 void EC_GROUP_clear_free(EC_GROUP *group);
119 /** Copies EC_GROUP objects. Note: both EC_GROUPs must use the same EC_METHOD.
120 * \param dst destination EC_GROUP object
121 * \param src source EC_GROUP object
122 * \return 1 on success and 0 if an error occurred.
124 int EC_GROUP_copy(EC_GROUP *dst, const EC_GROUP *src);
126 /** Creates a new EC_GROUP object and copies the content
127 * form src to the newly created EC_KEY object
128 * \param src source EC_GROUP object
129 * \return newly created EC_GROUP object or NULL in case of an error.
131 EC_GROUP *EC_GROUP_dup(const EC_GROUP *src);
133 /** Returns the EC_METHOD of the EC_GROUP object.
134 * \param group EC_GROUP object
135 * \return EC_METHOD used in this EC_GROUP object.
137 const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group);
139 /** Returns the field type of the EC_METHOD.
140 * \param meth EC_METHOD object
141 * \return NID of the underlying field type OID.
143 int EC_METHOD_get_field_type(const EC_METHOD *meth);
145 /** Sets the generator and it's order/cofactor of a EC_GROUP object.
146 * \param group EC_GROUP object
147 * \param generator EC_POINT object with the generator.
148 * \param order the order of the group generated by the generator.
149 * \param cofactor the index of the sub-group generated by the generator
150 * in the group of all points on the elliptic curve.
151 * \return 1 on success and 0 if an error occurred
153 int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator,
154 const BIGNUM *order, const BIGNUM *cofactor);
156 /** Returns the generator of a EC_GROUP object.
157 * \param group EC_GROUP object
158 * \return the currently used generator (possibly NULL).
160 const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group);
162 /** Returns the montgomery data for order(Generator)
163 * \param group EC_GROUP object
164 * \return the currently used montgomery data (possibly NULL).
166 BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group);
168 /** Gets the order of a EC_GROUP
169 * \param group EC_GROUP object
170 * \param order BIGNUM to which the order is copied
172 * \return 1 on success and 0 if an error occurred
174 int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx);
176 /** Gets the order of an EC_GROUP
177 * \param group EC_GROUP object
178 * \return the group order
180 const BIGNUM *EC_GROUP_get0_order(const EC_GROUP *group);
182 /** Gets the number of bits of the order of an EC_GROUP
183 * \param group EC_GROUP object
184 * \return number of bits of group order.
186 int EC_GROUP_order_bits(const EC_GROUP *group);
188 /** Gets the cofactor of a EC_GROUP
189 * \param group EC_GROUP object
190 * \param cofactor BIGNUM to which the cofactor is copied
192 * \return 1 on success and 0 if an error occurred
194 int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor,
197 /** Gets the cofactor of an EC_GROUP
198 * \param group EC_GROUP object
199 * \return the group cofactor
201 const BIGNUM *EC_GROUP_get0_cofactor(const EC_GROUP *group);
203 /** Sets the name of a EC_GROUP object
204 * \param group EC_GROUP object
205 * \param nid NID of the curve name OID
207 void EC_GROUP_set_curve_name(EC_GROUP *group, int nid);
209 /** Returns the curve name of a EC_GROUP object
210 * \param group EC_GROUP object
211 * \return NID of the curve name OID or 0 if not set.
213 int EC_GROUP_get_curve_name(const EC_GROUP *group);
215 /** Gets the field of an EC_GROUP
216 * \param group EC_GROUP object
217 * \return the group field
219 const BIGNUM *EC_GROUP_get0_field(const EC_GROUP *group);
221 void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag);
222 int EC_GROUP_get_asn1_flag(const EC_GROUP *group);
224 void EC_GROUP_set_point_conversion_form(EC_GROUP *group,
225 point_conversion_form_t form);
226 point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP *);
228 unsigned char *EC_GROUP_get0_seed(const EC_GROUP *x);
229 size_t EC_GROUP_get_seed_len(const EC_GROUP *);
230 size_t EC_GROUP_set_seed(EC_GROUP *, const unsigned char *, size_t len);
232 /** Sets the parameters of a ec curve defined by y^2 = x^3 + a*x + b (for GFp)
233 * or y^2 + x*y = x^3 + a*x^2 + b (for GF2m)
234 * \param group EC_GROUP object
235 * \param p BIGNUM with the prime number (GFp) or the polynomial
236 * defining the underlying field (GF2m)
237 * \param a BIGNUM with parameter a of the equation
238 * \param b BIGNUM with parameter b of the equation
239 * \param ctx BN_CTX object (optional)
240 * \return 1 on success and 0 if an error occurred
242 int EC_GROUP_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
243 const BIGNUM *b, BN_CTX *ctx);
245 /** Gets the parameters of the ec curve defined by y^2 = x^3 + a*x + b (for GFp)
246 * or y^2 + x*y = x^3 + a*x^2 + b (for GF2m)
247 * \param group EC_GROUP object
248 * \param p BIGNUM with the prime number (GFp) or the polynomial
249 * defining the underlying field (GF2m)
250 * \param a BIGNUM for parameter a of the equation
251 * \param b BIGNUM for parameter b of the equation
252 * \param ctx BN_CTX object (optional)
253 * \return 1 on success and 0 if an error occurred
255 int EC_GROUP_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b,
258 /** Sets the parameters of an ec curve. Synonym for EC_GROUP_set_curve
259 * \param group EC_GROUP object
260 * \param p BIGNUM with the prime number (GFp) or the polynomial
261 * defining the underlying field (GF2m)
262 * \param a BIGNUM with parameter a of the equation
263 * \param b BIGNUM with parameter b of the equation
264 * \param ctx BN_CTX object (optional)
265 * \return 1 on success and 0 if an error occurred
267 DEPRECATEDIN_3(int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p,
268 const BIGNUM *a, const BIGNUM *b,
271 /** Gets the parameters of an ec curve. Synonym for EC_GROUP_get_curve
272 * \param group EC_GROUP object
273 * \param p BIGNUM with the prime number (GFp) or the polynomial
274 * defining the underlying field (GF2m)
275 * \param a BIGNUM for parameter a of the equation
276 * \param b BIGNUM for parameter b of the equation
277 * \param ctx BN_CTX object (optional)
278 * \return 1 on success and 0 if an error occurred
280 DEPRECATEDIN_3(int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p,
281 BIGNUM *a, BIGNUM *b,
284 # ifndef OPENSSL_NO_EC2M
285 /** Sets the parameter of an ec curve. Synonym for EC_GROUP_set_curve
286 * \param group EC_GROUP object
287 * \param p BIGNUM with the prime number (GFp) or the polynomial
288 * defining the underlying field (GF2m)
289 * \param a BIGNUM with parameter a of the equation
290 * \param b BIGNUM with parameter b of the equation
291 * \param ctx BN_CTX object (optional)
292 * \return 1 on success and 0 if an error occurred
294 DEPRECATEDIN_3(int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p,
295 const BIGNUM *a, const BIGNUM *b,
298 /** Gets the parameters of an ec curve. Synonym for EC_GROUP_get_curve
299 * \param group EC_GROUP object
300 * \param p BIGNUM with the prime number (GFp) or the polynomial
301 * defining the underlying field (GF2m)
302 * \param a BIGNUM for parameter a of the equation
303 * \param b BIGNUM for parameter b of the equation
304 * \param ctx BN_CTX object (optional)
305 * \return 1 on success and 0 if an error occurred
307 DEPRECATEDIN_3(int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p,
308 BIGNUM *a, BIGNUM *b,
311 /** Returns the number of bits needed to represent a field element
312 * \param group EC_GROUP object
313 * \return number of bits needed to represent a field element
315 int EC_GROUP_get_degree(const EC_GROUP *group);
317 /** Checks whether the parameter in the EC_GROUP define a valid ec group
318 * \param group EC_GROUP object
319 * \param ctx BN_CTX object (optional)
320 * \return 1 if group is a valid ec group and 0 otherwise
322 int EC_GROUP_check(const EC_GROUP *group, BN_CTX *ctx);
324 /** Checks whether the discriminant of the elliptic curve is zero or not
325 * \param group EC_GROUP object
326 * \param ctx BN_CTX object (optional)
327 * \return 1 if the discriminant is not zero and 0 otherwise
329 int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx);
331 /** Compares two EC_GROUP objects
332 * \param a first EC_GROUP object
333 * \param b second EC_GROUP object
334 * \param ctx BN_CTX object (optional)
335 * \return 0 if the groups are equal, 1 if not, or -1 on error
337 int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx);
340 * EC_GROUP_new_GF*() calls EC_GROUP_new() and EC_GROUP_set_GF*() after
341 * choosing an appropriate EC_METHOD
344 /** Creates a new EC_GROUP object with the specified parameters defined
345 * over GFp (defined by the equation y^2 = x^3 + a*x + b)
346 * \param p BIGNUM with the prime number
347 * \param a BIGNUM with the parameter a of the equation
348 * \param b BIGNUM with the parameter b of the equation
349 * \param ctx BN_CTX object (optional)
350 * \return newly created EC_GROUP object with the specified parameters
352 EC_GROUP *EC_GROUP_new_curve_GFp(const BIGNUM *p, const BIGNUM *a,
353 const BIGNUM *b, BN_CTX *ctx);
354 # ifndef OPENSSL_NO_EC2M
355 /** Creates a new EC_GROUP object with the specified parameters defined
356 * over GF2m (defined by the equation y^2 + x*y = x^3 + a*x^2 + b)
357 * \param p BIGNUM with the polynomial defining the underlying field
358 * \param a BIGNUM with the parameter a of the equation
359 * \param b BIGNUM with the parameter b of the equation
360 * \param ctx BN_CTX object (optional)
361 * \return newly created EC_GROUP object with the specified parameters
363 EC_GROUP *EC_GROUP_new_curve_GF2m(const BIGNUM *p, const BIGNUM *a,
364 const BIGNUM *b, BN_CTX *ctx);
367 /** Creates a EC_GROUP object with a curve specified by a NID
368 * \param nid NID of the OID of the curve name
369 * \return newly created EC_GROUP object with specified curve or NULL
370 * if an error occurred
372 EC_GROUP *EC_GROUP_new_by_curve_name(int nid);
374 /** Creates a new EC_GROUP object from an ECPARAMETERS object
375 * \param params pointer to the ECPARAMETERS object
376 * \return newly created EC_GROUP object with specified curve or NULL
377 * if an error occurred
379 EC_GROUP *EC_GROUP_new_from_ecparameters(const ECPARAMETERS *params);
381 /** Creates an ECPARAMETERS object for the given EC_GROUP object.
382 * \param group pointer to the EC_GROUP object
383 * \param params pointer to an existing ECPARAMETERS object or NULL
384 * \return pointer to the new ECPARAMETERS object or NULL
385 * if an error occurred.
387 ECPARAMETERS *EC_GROUP_get_ecparameters(const EC_GROUP *group,
388 ECPARAMETERS *params);
390 /** Creates a new EC_GROUP object from an ECPKPARAMETERS object
391 * \param params pointer to an existing ECPKPARAMETERS object, or NULL
392 * \return newly created EC_GROUP object with specified curve, or NULL
393 * if an error occurred
395 EC_GROUP *EC_GROUP_new_from_ecpkparameters(const ECPKPARAMETERS *params);
397 /** Creates an ECPKPARAMETERS object for the given EC_GROUP object.
398 * \param group pointer to the EC_GROUP object
399 * \param params pointer to an existing ECPKPARAMETERS object or NULL
400 * \return pointer to the new ECPKPARAMETERS object or NULL
401 * if an error occurred.
403 ECPKPARAMETERS *EC_GROUP_get_ecpkparameters(const EC_GROUP *group,
404 ECPKPARAMETERS *params);
406 /********************************************************************/
407 /* handling of internal curves */
408 /********************************************************************/
416 * EC_builtin_curves(EC_builtin_curve *r, size_t size) returns number of all
417 * available curves or zero if a error occurred. In case r is not zero,
418 * nitems EC_builtin_curve structures are filled with the data of the first
419 * nitems internal groups
421 size_t EC_get_builtin_curves(EC_builtin_curve *r, size_t nitems);
423 const char *EC_curve_nid2nist(int nid);
424 int EC_curve_nist2nid(const char *name);
425 int EC_GROUP_check_named_curve(const EC_GROUP *group, int nist_only);
427 /********************************************************************/
428 /* EC_POINT functions */
429 /********************************************************************/
431 /** Creates a new EC_POINT object for the specified EC_GROUP
432 * \param group EC_GROUP the underlying EC_GROUP object
433 * \return newly created EC_POINT object or NULL if an error occurred
435 EC_POINT *EC_POINT_new(const EC_GROUP *group);
437 /** Frees a EC_POINT object
438 * \param point EC_POINT object to be freed
440 void EC_POINT_free(EC_POINT *point);
442 /** Clears and frees a EC_POINT object
443 * \param point EC_POINT object to be cleared and freed
445 void EC_POINT_clear_free(EC_POINT *point);
447 /** Copies EC_POINT object
448 * \param dst destination EC_POINT object
449 * \param src source EC_POINT object
450 * \return 1 on success and 0 if an error occurred
452 int EC_POINT_copy(EC_POINT *dst, const EC_POINT *src);
454 /** Creates a new EC_POINT object and copies the content of the supplied
456 * \param src source EC_POINT object
457 * \param group underlying the EC_GROUP object
458 * \return newly created EC_POINT object or NULL if an error occurred
460 EC_POINT *EC_POINT_dup(const EC_POINT *src, const EC_GROUP *group);
462 /** Returns the EC_METHOD used in EC_POINT object
463 * \param point EC_POINT object
464 * \return the EC_METHOD used
466 const EC_METHOD *EC_POINT_method_of(const EC_POINT *point);
468 /** Sets a point to infinity (neutral element)
469 * \param group underlying EC_GROUP object
470 * \param point EC_POINT to set to infinity
471 * \return 1 on success and 0 if an error occurred
473 int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point);
475 /** Sets the jacobian projective coordinates of a EC_POINT over GFp
476 * \param group underlying EC_GROUP object
477 * \param p EC_POINT object
478 * \param x BIGNUM with the x-coordinate
479 * \param y BIGNUM with the y-coordinate
480 * \param z BIGNUM with the z-coordinate
481 * \param ctx BN_CTX object (optional)
482 * \return 1 on success and 0 if an error occurred
484 int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
485 EC_POINT *p, const BIGNUM *x,
486 const BIGNUM *y, const BIGNUM *z,
489 /** Gets the jacobian projective coordinates of a EC_POINT over GFp
490 * \param group underlying EC_GROUP object
491 * \param p EC_POINT object
492 * \param x BIGNUM for the x-coordinate
493 * \param y BIGNUM for the y-coordinate
494 * \param z BIGNUM for the z-coordinate
495 * \param ctx BN_CTX object (optional)
496 * \return 1 on success and 0 if an error occurred
498 int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
499 const EC_POINT *p, BIGNUM *x,
500 BIGNUM *y, BIGNUM *z,
503 /** Sets the affine coordinates of an EC_POINT
504 * \param group underlying EC_GROUP object
505 * \param p EC_POINT object
506 * \param x BIGNUM with the x-coordinate
507 * \param y BIGNUM with the y-coordinate
508 * \param ctx BN_CTX object (optional)
509 * \return 1 on success and 0 if an error occurred
511 int EC_POINT_set_affine_coordinates(const EC_GROUP *group, EC_POINT *p,
512 const BIGNUM *x, const BIGNUM *y,
515 /** Gets the affine coordinates of an EC_POINT.
516 * \param group underlying EC_GROUP object
517 * \param p EC_POINT object
518 * \param x BIGNUM for the x-coordinate
519 * \param y BIGNUM for the y-coordinate
520 * \param ctx BN_CTX object (optional)
521 * \return 1 on success and 0 if an error occurred
523 int EC_POINT_get_affine_coordinates(const EC_GROUP *group, const EC_POINT *p,
524 BIGNUM *x, BIGNUM *y, BN_CTX *ctx);
526 /** Sets the affine coordinates of an EC_POINT. A synonym of
527 * EC_POINT_set_affine_coordinates
528 * \param group underlying EC_GROUP object
529 * \param p EC_POINT object
530 * \param x BIGNUM with the x-coordinate
531 * \param y BIGNUM with the y-coordinate
532 * \param ctx BN_CTX object (optional)
533 * \return 1 on success and 0 if an error occurred
535 DEPRECATEDIN_3(int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group,
541 /** Gets the affine coordinates of an EC_POINT. A synonym of
542 * EC_POINT_get_affine_coordinates
543 * \param group underlying EC_GROUP object
544 * \param p EC_POINT object
545 * \param x BIGNUM for the x-coordinate
546 * \param y BIGNUM for the y-coordinate
547 * \param ctx BN_CTX object (optional)
548 * \return 1 on success and 0 if an error occurred
550 DEPRECATEDIN_3(int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group,
556 /** Sets the x9.62 compressed coordinates of a EC_POINT
557 * \param group underlying EC_GROUP object
558 * \param p EC_POINT object
559 * \param x BIGNUM with x-coordinate
560 * \param y_bit integer with the y-Bit (either 0 or 1)
561 * \param ctx BN_CTX object (optional)
562 * \return 1 on success and 0 if an error occurred
564 int EC_POINT_set_compressed_coordinates(const EC_GROUP *group, EC_POINT *p,
565 const BIGNUM *x, int y_bit,
568 /** Sets the x9.62 compressed coordinates of a EC_POINT. A synonym of
569 * EC_POINT_set_compressed_coordinates
570 * \param group underlying EC_GROUP object
571 * \param p EC_POINT object
572 * \param x BIGNUM with x-coordinate
573 * \param y_bit integer with the y-Bit (either 0 or 1)
574 * \param ctx BN_CTX object (optional)
575 * \return 1 on success and 0 if an error occurred
577 DEPRECATEDIN_3(int EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group,
582 # ifndef OPENSSL_NO_EC2M
583 /** Sets the affine coordinates of an EC_POINT. A synonym of
584 * EC_POINT_set_affine_coordinates
585 * \param group underlying EC_GROUP object
586 * \param p EC_POINT object
587 * \param x BIGNUM with the x-coordinate
588 * \param y BIGNUM with the y-coordinate
589 * \param ctx BN_CTX object (optional)
590 * \return 1 on success and 0 if an error occurred
592 DEPRECATEDIN_3(int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group,
598 /** Gets the affine coordinates of an EC_POINT. A synonym of
599 * EC_POINT_get_affine_coordinates
600 * \param group underlying EC_GROUP object
601 * \param p EC_POINT object
602 * \param x BIGNUM for the x-coordinate
603 * \param y BIGNUM for the y-coordinate
604 * \param ctx BN_CTX object (optional)
605 * \return 1 on success and 0 if an error occurred
607 DEPRECATEDIN_3(int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group,
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
622 DEPRECATEDIN_3(int EC_POINT_set_compressed_coordinates_GF2m(const EC_GROUP *group,
628 /** Encodes a EC_POINT object to a octet string
629 * \param group underlying EC_GROUP object
630 * \param p EC_POINT object
631 * \param form point conversion form
632 * \param buf memory buffer for the result. If NULL the function returns
633 * required buffer size.
634 * \param len length of the memory buffer
635 * \param ctx BN_CTX object (optional)
636 * \return the length of the encoded octet string or 0 if an error occurred
638 size_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *p,
639 point_conversion_form_t form,
640 unsigned char *buf, size_t len, BN_CTX *ctx);
642 /** Decodes a EC_POINT from a octet string
643 * \param group underlying EC_GROUP object
644 * \param p EC_POINT object
645 * \param buf memory buffer with the encoded ec point
646 * \param len length of the encoded ec point
647 * \param ctx BN_CTX object (optional)
648 * \return 1 on success and 0 if an error occurred
650 int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *p,
651 const unsigned char *buf, size_t len, BN_CTX *ctx);
653 /** Encodes an EC_POINT object to an allocated octet string
654 * \param group underlying EC_GROUP object
655 * \param point EC_POINT object
656 * \param form point conversion form
657 * \param pbuf returns pointer to allocated buffer
658 * \param ctx BN_CTX object (optional)
659 * \return the length of the encoded octet string or 0 if an error occurred
661 size_t EC_POINT_point2buf(const EC_GROUP *group, const EC_POINT *point,
662 point_conversion_form_t form,
663 unsigned char **pbuf, BN_CTX *ctx);
665 /* other interfaces to point2oct/oct2point: */
666 BIGNUM *EC_POINT_point2bn(const EC_GROUP *, const EC_POINT *,
667 point_conversion_form_t form, BIGNUM *, BN_CTX *);
668 EC_POINT *EC_POINT_bn2point(const EC_GROUP *, const BIGNUM *,
669 EC_POINT *, BN_CTX *);
670 char *EC_POINT_point2hex(const EC_GROUP *, const EC_POINT *,
671 point_conversion_form_t form, BN_CTX *);
672 EC_POINT *EC_POINT_hex2point(const EC_GROUP *, const char *,
673 EC_POINT *, BN_CTX *);
675 /********************************************************************/
676 /* functions for doing EC_POINT arithmetic */
677 /********************************************************************/
679 /** Computes the sum of two EC_POINT
680 * \param group underlying EC_GROUP object
681 * \param r EC_POINT object for the result (r = a + b)
682 * \param a EC_POINT object with the first summand
683 * \param b EC_POINT object with the second summand
684 * \param ctx BN_CTX object (optional)
685 * \return 1 on success and 0 if an error occurred
687 int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
688 const EC_POINT *b, BN_CTX *ctx);
690 /** Computes the double of a EC_POINT
691 * \param group underlying EC_GROUP object
692 * \param r EC_POINT object for the result (r = 2 * a)
693 * \param a EC_POINT object
694 * \param ctx BN_CTX object (optional)
695 * \return 1 on success and 0 if an error occurred
697 int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
700 /** Computes the inverse of a EC_POINT
701 * \param group underlying EC_GROUP object
702 * \param a EC_POINT object to be inverted (it's used for the result as well)
703 * \param ctx BN_CTX object (optional)
704 * \return 1 on success and 0 if an error occurred
706 int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx);
708 /** Checks whether the point is the neutral element of the group
709 * \param group the underlying EC_GROUP object
710 * \param p EC_POINT object
711 * \return 1 if the point is the neutral element and 0 otherwise
713 int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *p);
715 /** Checks whether the point is on the curve
716 * \param group underlying EC_GROUP object
717 * \param point EC_POINT object to check
718 * \param ctx BN_CTX object (optional)
719 * \return 1 if the point is on the curve, 0 if not, or -1 on error
721 int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
724 /** Compares two EC_POINTs
725 * \param group underlying EC_GROUP object
726 * \param a first EC_POINT object
727 * \param b second EC_POINT object
728 * \param ctx BN_CTX object (optional)
729 * \return 1 if the points are not equal, 0 if they are, or -1 on error
731 int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b,
734 int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx);
735 int EC_POINTs_make_affine(const EC_GROUP *group, size_t num,
736 EC_POINT *points[], BN_CTX *ctx);
738 /** Computes r = generator * n + sum_{i=0}^{num-1} p[i] * m[i]
739 * \param group underlying EC_GROUP object
740 * \param r EC_POINT object for the result
741 * \param n BIGNUM with the multiplier for the group generator (optional)
742 * \param num number further summands
743 * \param p array of size num of EC_POINT objects
744 * \param m array of size num of BIGNUM objects
745 * \param ctx BN_CTX object (optional)
746 * \return 1 on success and 0 if an error occurred
748 int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *n,
749 size_t num, const EC_POINT *p[], const BIGNUM *m[],
752 /** Computes r = generator * n + q * m
753 * \param group underlying EC_GROUP object
754 * \param r EC_POINT object for the result
755 * \param n BIGNUM with the multiplier for the group generator (optional)
756 * \param q EC_POINT object with the first factor of the second summand
757 * \param m BIGNUM with the second factor of the second summand
758 * \param ctx BN_CTX object (optional)
759 * \return 1 on success and 0 if an error occurred
761 int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *n,
762 const EC_POINT *q, const BIGNUM *m, BN_CTX *ctx);
764 /** Stores multiples of generator for faster point multiplication
765 * \param group EC_GROUP object
766 * \param ctx BN_CTX object (optional)
767 * \return 1 on success and 0 if an error occurred
769 int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
771 /** Reports whether a precomputation has been done
772 * \param group EC_GROUP object
773 * \return 1 if a pre-computation has been done and 0 otherwise
775 int EC_GROUP_have_precompute_mult(const EC_GROUP *group);
777 /********************************************************************/
779 /********************************************************************/
781 DECLARE_ASN1_ITEM(ECPKPARAMETERS)
782 DECLARE_ASN1_ALLOC_FUNCTIONS(ECPKPARAMETERS)
783 DECLARE_ASN1_ITEM(ECPARAMETERS)
784 DECLARE_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS)
787 * EC_GROUP_get_basis_type() returns the NID of the basis type used to
788 * represent the field elements
790 int EC_GROUP_get_basis_type(const EC_GROUP *);
791 # ifndef OPENSSL_NO_EC2M
792 int EC_GROUP_get_trinomial_basis(const EC_GROUP *, unsigned int *k);
793 int EC_GROUP_get_pentanomial_basis(const EC_GROUP *, unsigned int *k1,
794 unsigned int *k2, unsigned int *k3);
797 # define OPENSSL_EC_EXPLICIT_CURVE 0x000
798 # define OPENSSL_EC_NAMED_CURVE 0x001
800 EC_GROUP *d2i_ECPKParameters(EC_GROUP **, const unsigned char **in, long len);
801 int i2d_ECPKParameters(const EC_GROUP *, unsigned char **out);
803 # define d2i_ECPKParameters_bio(bp,x) ASN1_d2i_bio_of(EC_GROUP,NULL,d2i_ECPKParameters,bp,x)
804 # define i2d_ECPKParameters_bio(bp,x) ASN1_i2d_bio_of(EC_GROUP,i2d_ECPKParameters,bp,x)
805 # define d2i_ECPKParameters_fp(fp,x) (EC_GROUP *)ASN1_d2i_fp(NULL, \
806 (char *(*)())d2i_ECPKParameters,(fp),(unsigned char **)(x))
807 # define i2d_ECPKParameters_fp(fp,x) ASN1_i2d_fp(i2d_ECPKParameters,(fp), \
808 (unsigned char *)(x))
810 int ECPKParameters_print(BIO *bp, const EC_GROUP *x, int off);
811 # ifndef OPENSSL_NO_STDIO
812 int ECPKParameters_print_fp(FILE *fp, const EC_GROUP *x, int off);
815 /********************************************************************/
816 /* EC_KEY functions */
817 /********************************************************************/
819 /* some values for the encoding_flag */
820 # define EC_PKEY_NO_PARAMETERS 0x001
821 # define EC_PKEY_NO_PUBKEY 0x002
823 /* some values for the flags field */
824 # define EC_FLAG_NON_FIPS_ALLOW 0x1
825 # define EC_FLAG_FIPS_CHECKED 0x2
826 # define EC_FLAG_COFACTOR_ECDH 0x1000
828 /** Creates a new EC_KEY object.
829 * \return EC_KEY object or NULL if an error occurred.
831 EC_KEY *EC_KEY_new(void);
833 int EC_KEY_get_flags(const EC_KEY *key);
835 void EC_KEY_set_flags(EC_KEY *key, int flags);
837 void EC_KEY_clear_flags(EC_KEY *key, int flags);
839 /** Creates a new EC_KEY object using a named curve as underlying
841 * \param nid NID of the named curve.
842 * \return EC_KEY object or NULL if an error occurred.
844 EC_KEY *EC_KEY_new_by_curve_name(int nid);
846 /** Frees a EC_KEY object.
847 * \param key EC_KEY object to be freed.
849 void EC_KEY_free(EC_KEY *key);
851 /** Copies a EC_KEY object.
852 * \param dst destination EC_KEY object
853 * \param src src EC_KEY object
854 * \return dst or NULL if an error occurred.
856 EC_KEY *EC_KEY_copy(EC_KEY *dst, const EC_KEY *src);
858 /** Creates a new EC_KEY object and copies the content from src to it.
859 * \param src the source EC_KEY object
860 * \return newly created EC_KEY object or NULL if an error occurred.
862 EC_KEY *EC_KEY_dup(const EC_KEY *src);
864 /** Increases the internal reference count of a EC_KEY object.
865 * \param key EC_KEY object
866 * \return 1 on success and 0 if an error occurred.
868 int EC_KEY_up_ref(EC_KEY *key);
870 /** Returns the ENGINE object of a EC_KEY object
871 * \param eckey EC_KEY object
872 * \return the ENGINE object (possibly NULL).
874 ENGINE *EC_KEY_get0_engine(const EC_KEY *eckey);
876 /** Returns the EC_GROUP object of a EC_KEY object
877 * \param key EC_KEY object
878 * \return the EC_GROUP object (possibly NULL).
880 const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key);
882 /** Sets the EC_GROUP of a EC_KEY object.
883 * \param key EC_KEY object
884 * \param group EC_GROUP to use in the EC_KEY object (note: the EC_KEY
885 * object will use an own copy of the EC_GROUP).
886 * \return 1 on success and 0 if an error occurred.
888 int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group);
890 /** Returns the private key of a EC_KEY object.
891 * \param key EC_KEY object
892 * \return a BIGNUM with the private key (possibly NULL).
894 const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key);
896 /** Sets the private key of a EC_KEY object.
897 * \param key EC_KEY object
898 * \param prv BIGNUM with the private key (note: the EC_KEY object
899 * will use an own copy of the BIGNUM).
900 * \return 1 on success and 0 if an error occurred.
902 int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *prv);
904 /** Returns the public key of a EC_KEY object.
905 * \param key the EC_KEY object
906 * \return a EC_POINT object with the public key (possibly NULL)
908 const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key);
910 /** Sets the public key of a EC_KEY object.
911 * \param key EC_KEY object
912 * \param pub EC_POINT object with the public key (note: the EC_KEY object
913 * will use an own copy of the EC_POINT object).
914 * \return 1 on success and 0 if an error occurred.
916 int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub);
918 unsigned EC_KEY_get_enc_flags(const EC_KEY *key);
919 void EC_KEY_set_enc_flags(EC_KEY *eckey, unsigned int flags);
920 point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key);
921 void EC_KEY_set_conv_form(EC_KEY *eckey, point_conversion_form_t cform);
923 #define EC_KEY_get_ex_new_index(l, p, newf, dupf, freef) \
924 CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_EC_KEY, l, p, newf, dupf, freef)
925 int EC_KEY_set_ex_data(EC_KEY *key, int idx, void *arg);
926 void *EC_KEY_get_ex_data(const EC_KEY *key, int idx);
928 /* wrapper functions for the underlying EC_GROUP object */
929 void EC_KEY_set_asn1_flag(EC_KEY *eckey, int asn1_flag);
931 /** Creates a table of pre-computed multiples of the generator to
932 * accelerate further EC_KEY operations.
933 * \param key EC_KEY object
934 * \param ctx BN_CTX object (optional)
935 * \return 1 on success and 0 if an error occurred.
937 int EC_KEY_precompute_mult(EC_KEY *key, BN_CTX *ctx);
939 /** Creates a new ec private (and optional a new public) key.
940 * \param key EC_KEY object
941 * \return 1 on success and 0 if an error occurred.
943 int EC_KEY_generate_key(EC_KEY *key);
945 /** Verifies that a private and/or public key is valid.
946 * \param key the EC_KEY object
947 * \return 1 on success and 0 otherwise.
949 int EC_KEY_check_key(const EC_KEY *key);
951 /** Indicates if an EC_KEY can be used for signing.
952 * \param eckey the EC_KEY object
953 * \return 1 if can can sign and 0 otherwise.
955 int EC_KEY_can_sign(const EC_KEY *eckey);
957 /** Sets a public key from affine coordinates performing
958 * necessary NIST PKV tests.
959 * \param key the EC_KEY object
960 * \param x public key x coordinate
961 * \param y public key y coordinate
962 * \return 1 on success and 0 otherwise.
964 int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x,
967 /** Encodes an EC_KEY public key to an allocated octet string
968 * \param key key to encode
969 * \param form point conversion form
970 * \param pbuf returns pointer to allocated buffer
971 * \param ctx BN_CTX object (optional)
972 * \return the length of the encoded octet string or 0 if an error occurred
974 size_t EC_KEY_key2buf(const EC_KEY *key, point_conversion_form_t form,
975 unsigned char **pbuf, BN_CTX *ctx);
977 /** Decodes a EC_KEY public key from a octet string
978 * \param key key to decode
979 * \param buf memory buffer with the encoded ec point
980 * \param len length of the encoded ec point
981 * \param ctx BN_CTX object (optional)
982 * \return 1 on success and 0 if an error occurred
985 int EC_KEY_oct2key(EC_KEY *key, const unsigned char *buf, size_t len,
988 /** Decodes an EC_KEY private key from an octet string
989 * \param key key to decode
990 * \param buf memory buffer with the encoded private key
991 * \param len length of the encoded key
992 * \return 1 on success and 0 if an error occurred
995 int EC_KEY_oct2priv(EC_KEY *key, const unsigned char *buf, size_t len);
997 /** Encodes a EC_KEY private key to an octet string
998 * \param key key to encode
999 * \param buf memory buffer for the result. If NULL the function returns
1000 * required buffer size.
1001 * \param len length of the memory buffer
1002 * \return the length of the encoded octet string or 0 if an error occurred
1005 size_t EC_KEY_priv2oct(const EC_KEY *key, unsigned char *buf, size_t len);
1007 /** Encodes an EC_KEY private key to an allocated octet string
1008 * \param eckey key to encode
1009 * \param pbuf returns pointer to allocated buffer
1010 * \return the length of the encoded octet string or 0 if an error occurred
1012 size_t EC_KEY_priv2buf(const EC_KEY *eckey, unsigned char **pbuf);
1014 /********************************************************************/
1015 /* de- and encoding functions for SEC1 ECPrivateKey */
1016 /********************************************************************/
1018 /** Decodes a private key from a memory buffer.
1019 * \param key a pointer to a EC_KEY object which should be used (or NULL)
1020 * \param in pointer to memory with the DER encoded private key
1021 * \param len length of the DER encoded private key
1022 * \return the decoded private key or NULL if an error occurred.
1024 EC_KEY *d2i_ECPrivateKey(EC_KEY **key, const unsigned char **in, long len);
1026 /** Encodes a private key object and stores the result in a buffer.
1027 * \param key the EC_KEY object to encode
1028 * \param out the buffer for the result (if NULL the function returns number
1030 * \return 1 on success and 0 if an error occurred.
1032 int i2d_ECPrivateKey(const EC_KEY *key, unsigned char **out);
1034 /********************************************************************/
1035 /* de- and encoding functions for EC parameters */
1036 /********************************************************************/
1038 /** Decodes ec parameter from a memory buffer.
1039 * \param key a pointer to a EC_KEY object which should be used (or NULL)
1040 * \param in pointer to memory with the DER encoded ec parameters
1041 * \param len length of the DER encoded ec parameters
1042 * \return a EC_KEY object with the decoded parameters or NULL if an error
1045 EC_KEY *d2i_ECParameters(EC_KEY **key, const unsigned char **in, long len);
1047 /** Encodes ec parameter and stores the result in a buffer.
1048 * \param key the EC_KEY object with ec parameters to encode
1049 * \param out the buffer for the result (if NULL the function returns number
1051 * \return 1 on success and 0 if an error occurred.
1053 int i2d_ECParameters(const EC_KEY *key, unsigned char **out);
1055 /********************************************************************/
1056 /* de- and encoding functions for EC public key */
1057 /* (octet string, not DER -- hence 'o2i' and 'i2o') */
1058 /********************************************************************/
1060 /** Decodes a ec public key from a octet string.
1061 * \param key a pointer to a EC_KEY object which should be used
1062 * \param in memory buffer with the encoded public key
1063 * \param len length of the encoded public key
1064 * \return EC_KEY object with decoded public key or NULL if an error
1067 EC_KEY *o2i_ECPublicKey(EC_KEY **key, const unsigned char **in, long len);
1069 /** Encodes a ec public key in an octet string.
1070 * \param key the EC_KEY object with the public key
1071 * \param out the buffer for the result (if NULL the function returns number
1073 * \return 1 on success and 0 if an error occurred
1075 int i2o_ECPublicKey(const EC_KEY *key, unsigned char **out);
1077 /** Prints out the ec parameters on human readable form.
1078 * \param bp BIO object to which the information is printed
1079 * \param key EC_KEY object
1080 * \return 1 on success and 0 if an error occurred
1082 int ECParameters_print(BIO *bp, const EC_KEY *key);
1084 /** Prints out the contents of a EC_KEY object
1085 * \param bp BIO object to which the information is printed
1086 * \param key EC_KEY object
1087 * \param off line offset
1088 * \return 1 on success and 0 if an error occurred
1090 int EC_KEY_print(BIO *bp, const EC_KEY *key, int off);
1092 # ifndef OPENSSL_NO_STDIO
1093 /** Prints out the ec parameters on human readable form.
1094 * \param fp file descriptor to which the information is printed
1095 * \param key EC_KEY object
1096 * \return 1 on success and 0 if an error occurred
1098 int ECParameters_print_fp(FILE *fp, const EC_KEY *key);
1100 /** Prints out the contents of a EC_KEY object
1101 * \param fp file descriptor to which the information is printed
1102 * \param key EC_KEY object
1103 * \param off line offset
1104 * \return 1 on success and 0 if an error occurred
1106 int EC_KEY_print_fp(FILE *fp, const EC_KEY *key, int off);
1110 const EC_KEY_METHOD *EC_KEY_OpenSSL(void);
1111 const EC_KEY_METHOD *EC_KEY_get_default_method(void);
1112 void EC_KEY_set_default_method(const EC_KEY_METHOD *meth);
1113 const EC_KEY_METHOD *EC_KEY_get_method(const EC_KEY *key);
1114 int EC_KEY_set_method(EC_KEY *key, const EC_KEY_METHOD *meth);
1115 EC_KEY *EC_KEY_new_method(ENGINE *engine);
1117 /** The old name for ecdh_KDF_X9_63
1118 * The ECDH KDF specification has been mistakingly attributed to ANSI X9.62,
1119 * it is actually specified in ANSI X9.63.
1120 * This identifier is retained for backwards compatibility
1122 DEPRECATEDIN_3(int ECDH_KDF_X9_62(unsigned char *out, size_t outlen,
1123 const unsigned char *Z, size_t Zlen,
1124 const unsigned char *sinfo, size_t sinfolen,
1127 int ECDH_compute_key(void *out, size_t outlen, const EC_POINT *pub_key,
1129 void *(*KDF) (const void *in, size_t inlen,
1130 void *out, size_t *outlen));
1132 typedef struct ECDSA_SIG_st ECDSA_SIG;
1134 /** Allocates and initialize a ECDSA_SIG structure
1135 * \return pointer to a ECDSA_SIG structure or NULL if an error occurred
1137 ECDSA_SIG *ECDSA_SIG_new(void);
1139 /** frees a ECDSA_SIG structure
1140 * \param sig pointer to the ECDSA_SIG structure
1142 void ECDSA_SIG_free(ECDSA_SIG *sig);
1144 /** i2d_ECDSA_SIG encodes content of ECDSA_SIG (note: this function modifies *pp
1145 * (*pp += length of the DER encoded signature)).
1146 * \param sig pointer to the ECDSA_SIG object
1147 * \param pp pointer to a unsigned char pointer for the output or NULL
1148 * \return the length of the DER encoded ECDSA_SIG object or 0
1150 DECLARE_ASN1_ENCODE_FUNCTIONS_only(ECDSA_SIG, ECDSA_SIG)
1152 /** d2i_ECDSA_SIG decodes an ECDSA signature (note: this function modifies *pp
1154 * \param sig pointer to ECDSA_SIG pointer (may be NULL)
1155 * \param pp memory buffer with the DER encoded signature
1156 * \param len length of the buffer
1157 * \return pointer to the decoded ECDSA_SIG structure (or NULL)
1160 /** Accessor for r and s fields of ECDSA_SIG
1161 * \param sig pointer to ECDSA_SIG structure
1162 * \param pr pointer to BIGNUM pointer for r (may be NULL)
1163 * \param ps pointer to BIGNUM pointer for s (may be NULL)
1165 void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps);
1167 /** Accessor for r field of ECDSA_SIG
1168 * \param sig pointer to ECDSA_SIG structure
1170 const BIGNUM *ECDSA_SIG_get0_r(const ECDSA_SIG *sig);
1172 /** Accessor for s field of ECDSA_SIG
1173 * \param sig pointer to ECDSA_SIG structure
1175 const BIGNUM *ECDSA_SIG_get0_s(const ECDSA_SIG *sig);
1177 /** Setter for r and s fields of ECDSA_SIG
1178 * \param sig pointer to ECDSA_SIG structure
1179 * \param r pointer to BIGNUM for r (may be NULL)
1180 * \param s pointer to BIGNUM for s (may be NULL)
1182 int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s);
1184 /** Computes the ECDSA signature of the given hash value using
1185 * the supplied private key and returns the created signature.
1186 * \param dgst pointer to the hash value
1187 * \param dgst_len length of the hash value
1188 * \param eckey EC_KEY object containing a private EC key
1189 * \return pointer to a ECDSA_SIG structure or NULL if an error occurred
1191 ECDSA_SIG *ECDSA_do_sign(const unsigned char *dgst, int dgst_len,
1194 /** Computes ECDSA signature of a given hash value using the supplied
1195 * private key (note: sig must point to ECDSA_size(eckey) bytes of memory).
1196 * \param dgst pointer to the hash value to sign
1197 * \param dgstlen length of the hash value
1198 * \param kinv BIGNUM with a pre-computed inverse k (optional)
1199 * \param rp BIGNUM with a pre-computed rp value (optional),
1200 * see ECDSA_sign_setup
1201 * \param eckey EC_KEY object containing a private EC key
1202 * \return pointer to a ECDSA_SIG structure or NULL if an error occurred
1204 ECDSA_SIG *ECDSA_do_sign_ex(const unsigned char *dgst, int dgstlen,
1205 const BIGNUM *kinv, const BIGNUM *rp,
1208 /** Verifies that the supplied signature is a valid ECDSA
1209 * signature of the supplied hash value using the supplied public key.
1210 * \param dgst pointer to the hash value
1211 * \param dgst_len length of the hash value
1212 * \param sig ECDSA_SIG structure
1213 * \param eckey EC_KEY object containing a public EC key
1214 * \return 1 if the signature is valid, 0 if the signature is invalid
1217 int ECDSA_do_verify(const unsigned char *dgst, int dgst_len,
1218 const ECDSA_SIG *sig, EC_KEY *eckey);
1220 /** Precompute parts of the signing operation
1221 * \param eckey EC_KEY object containing a private EC key
1222 * \param ctx BN_CTX object (optional)
1223 * \param kinv BIGNUM pointer for the inverse of k
1224 * \param rp BIGNUM pointer for x coordinate of k * generator
1225 * \return 1 on success and 0 otherwise
1227 int ECDSA_sign_setup(EC_KEY *eckey, BN_CTX *ctx, BIGNUM **kinv, BIGNUM **rp);
1229 /** Computes ECDSA signature of a given hash value using the supplied
1230 * private key (note: sig must point to ECDSA_size(eckey) bytes of memory).
1231 * \param type this parameter is ignored
1232 * \param dgst pointer to the hash value to sign
1233 * \param dgstlen length of the hash value
1234 * \param sig memory for the DER encoded created signature
1235 * \param siglen pointer to the length of the returned signature
1236 * \param eckey EC_KEY object containing a private EC key
1237 * \return 1 on success and 0 otherwise
1239 int ECDSA_sign(int type, const unsigned char *dgst, int dgstlen,
1240 unsigned char *sig, unsigned int *siglen, EC_KEY *eckey);
1242 /** Computes ECDSA signature of a given hash value using the supplied
1243 * private key (note: sig must point to ECDSA_size(eckey) bytes of memory).
1244 * \param type this parameter is ignored
1245 * \param dgst pointer to the hash value to sign
1246 * \param dgstlen length of the hash value
1247 * \param sig buffer to hold the DER encoded signature
1248 * \param siglen pointer to the length of the returned signature
1249 * \param kinv BIGNUM with a pre-computed inverse k (optional)
1250 * \param rp BIGNUM with a pre-computed rp value (optional),
1251 * see ECDSA_sign_setup
1252 * \param eckey EC_KEY object containing a private EC key
1253 * \return 1 on success and 0 otherwise
1255 int ECDSA_sign_ex(int type, const unsigned char *dgst, int dgstlen,
1256 unsigned char *sig, unsigned int *siglen,
1257 const BIGNUM *kinv, const BIGNUM *rp, EC_KEY *eckey);
1259 /** Verifies that the given signature is valid ECDSA signature
1260 * of the supplied hash value using the specified public key.
1261 * \param type this parameter is ignored
1262 * \param dgst pointer to the hash value
1263 * \param dgstlen length of the hash value
1264 * \param sig pointer to the DER encoded signature
1265 * \param siglen length of the DER encoded signature
1266 * \param eckey EC_KEY object containing a public EC key
1267 * \return 1 if the signature is valid, 0 if the signature is invalid
1270 int ECDSA_verify(int type, const unsigned char *dgst, int dgstlen,
1271 const unsigned char *sig, int siglen, EC_KEY *eckey);
1273 /** Returns the maximum length of the DER encoded signature
1274 * \param eckey EC_KEY object
1275 * \return numbers of bytes required for the DER encoded signature
1277 int ECDSA_size(const EC_KEY *eckey);
1279 /********************************************************************/
1280 /* EC_KEY_METHOD constructors, destructors, writers and accessors */
1281 /********************************************************************/
1283 EC_KEY_METHOD *EC_KEY_METHOD_new(const EC_KEY_METHOD *meth);
1284 void EC_KEY_METHOD_free(EC_KEY_METHOD *meth);
1285 void EC_KEY_METHOD_set_init(EC_KEY_METHOD *meth,
1286 int (*init)(EC_KEY *key),
1287 void (*finish)(EC_KEY *key),
1288 int (*copy)(EC_KEY *dest, const EC_KEY *src),
1289 int (*set_group)(EC_KEY *key, const EC_GROUP *grp),
1290 int (*set_private)(EC_KEY *key,
1291 const BIGNUM *priv_key),
1292 int (*set_public)(EC_KEY *key,
1293 const EC_POINT *pub_key));
1295 void EC_KEY_METHOD_set_keygen(EC_KEY_METHOD *meth,
1296 int (*keygen)(EC_KEY *key));
1298 void EC_KEY_METHOD_set_compute_key(EC_KEY_METHOD *meth,
1299 int (*ckey)(unsigned char **psec,
1301 const EC_POINT *pub_key,
1302 const EC_KEY *ecdh));
1304 void EC_KEY_METHOD_set_sign(EC_KEY_METHOD *meth,
1305 int (*sign)(int type, const unsigned char *dgst,
1306 int dlen, unsigned char *sig,
1307 unsigned int *siglen,
1308 const BIGNUM *kinv, const BIGNUM *r,
1310 int (*sign_setup)(EC_KEY *eckey, BN_CTX *ctx_in,
1311 BIGNUM **kinvp, BIGNUM **rp),
1312 ECDSA_SIG *(*sign_sig)(const unsigned char *dgst,
1314 const BIGNUM *in_kinv,
1318 void EC_KEY_METHOD_set_verify(EC_KEY_METHOD *meth,
1319 int (*verify)(int type, const unsigned
1320 char *dgst, int dgst_len,
1321 const unsigned char *sigbuf,
1322 int sig_len, EC_KEY *eckey),
1323 int (*verify_sig)(const unsigned char *dgst,
1325 const ECDSA_SIG *sig,
1328 void EC_KEY_METHOD_get_init(const EC_KEY_METHOD *meth,
1329 int (**pinit)(EC_KEY *key),
1330 void (**pfinish)(EC_KEY *key),
1331 int (**pcopy)(EC_KEY *dest, const EC_KEY *src),
1332 int (**pset_group)(EC_KEY *key,
1333 const EC_GROUP *grp),
1334 int (**pset_private)(EC_KEY *key,
1335 const BIGNUM *priv_key),
1336 int (**pset_public)(EC_KEY *key,
1337 const EC_POINT *pub_key));
1339 void EC_KEY_METHOD_get_keygen(const EC_KEY_METHOD *meth,
1340 int (**pkeygen)(EC_KEY *key));
1342 void EC_KEY_METHOD_get_compute_key(const EC_KEY_METHOD *meth,
1343 int (**pck)(unsigned char **psec,
1345 const EC_POINT *pub_key,
1346 const EC_KEY *ecdh));
1348 void EC_KEY_METHOD_get_sign(const EC_KEY_METHOD *meth,
1349 int (**psign)(int type, const unsigned char *dgst,
1350 int dlen, unsigned char *sig,
1351 unsigned int *siglen,
1352 const BIGNUM *kinv, const BIGNUM *r,
1354 int (**psign_setup)(EC_KEY *eckey, BN_CTX *ctx_in,
1355 BIGNUM **kinvp, BIGNUM **rp),
1356 ECDSA_SIG *(**psign_sig)(const unsigned char *dgst,
1358 const BIGNUM *in_kinv,
1362 void EC_KEY_METHOD_get_verify(const EC_KEY_METHOD *meth,
1363 int (**pverify)(int type, const unsigned
1364 char *dgst, int dgst_len,
1365 const unsigned char *sigbuf,
1366 int sig_len, EC_KEY *eckey),
1367 int (**pverify_sig)(const unsigned char *dgst,
1369 const ECDSA_SIG *sig,
1372 # define ECParameters_dup(x) ASN1_dup_of(EC_KEY,i2d_ECParameters,d2i_ECParameters,x)
1374 # ifndef __cplusplus
1375 # if defined(__SUNPRO_C)
1376 # if __SUNPRO_C >= 0x520
1377 # pragma error_messages (default,E_ARRAY_OF_INCOMPLETE_NONAME,E_ARRAY_OF_INCOMPLETE)
1382 # define EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid) \
1383 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
1384 EVP_PKEY_OP_PARAMGEN|EVP_PKEY_OP_KEYGEN, \
1385 EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID, nid, NULL)
1387 # define EVP_PKEY_CTX_set_ec_param_enc(ctx, flag) \
1388 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
1389 EVP_PKEY_OP_PARAMGEN|EVP_PKEY_OP_KEYGEN, \
1390 EVP_PKEY_CTRL_EC_PARAM_ENC, flag, NULL)
1392 # define EVP_PKEY_CTX_set_ecdh_cofactor_mode(ctx, flag) \
1393 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
1394 EVP_PKEY_OP_DERIVE, \
1395 EVP_PKEY_CTRL_EC_ECDH_COFACTOR, flag, NULL)
1397 # define EVP_PKEY_CTX_get_ecdh_cofactor_mode(ctx) \
1398 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
1399 EVP_PKEY_OP_DERIVE, \
1400 EVP_PKEY_CTRL_EC_ECDH_COFACTOR, -2, NULL)
1402 # define EVP_PKEY_CTX_set_ecdh_kdf_type(ctx, kdf) \
1403 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
1404 EVP_PKEY_OP_DERIVE, \
1405 EVP_PKEY_CTRL_EC_KDF_TYPE, kdf, NULL)
1407 # define EVP_PKEY_CTX_get_ecdh_kdf_type(ctx) \
1408 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
1409 EVP_PKEY_OP_DERIVE, \
1410 EVP_PKEY_CTRL_EC_KDF_TYPE, -2, NULL)
1412 # define EVP_PKEY_CTX_set_ecdh_kdf_md(ctx, md) \
1413 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
1414 EVP_PKEY_OP_DERIVE, \
1415 EVP_PKEY_CTRL_EC_KDF_MD, 0, (void *)(md))
1417 # define EVP_PKEY_CTX_get_ecdh_kdf_md(ctx, pmd) \
1418 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
1419 EVP_PKEY_OP_DERIVE, \
1420 EVP_PKEY_CTRL_GET_EC_KDF_MD, 0, (void *)(pmd))
1422 # define EVP_PKEY_CTX_set_ecdh_kdf_outlen(ctx, len) \
1423 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
1424 EVP_PKEY_OP_DERIVE, \
1425 EVP_PKEY_CTRL_EC_KDF_OUTLEN, len, NULL)
1427 # define EVP_PKEY_CTX_get_ecdh_kdf_outlen(ctx, plen) \
1428 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
1429 EVP_PKEY_OP_DERIVE, \
1430 EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN, 0, \
1433 # define EVP_PKEY_CTX_set0_ecdh_kdf_ukm(ctx, p, plen) \
1434 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
1435 EVP_PKEY_OP_DERIVE, \
1436 EVP_PKEY_CTRL_EC_KDF_UKM, plen, (void *)(p))
1438 # define EVP_PKEY_CTX_get0_ecdh_kdf_ukm(ctx, p) \
1439 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
1440 EVP_PKEY_OP_DERIVE, \
1441 EVP_PKEY_CTRL_GET_EC_KDF_UKM, 0, (void *)(p))
1443 /* SM2 will skip the operation check so no need to pass operation here */
1444 # define EVP_PKEY_CTX_set1_id(ctx, id, id_len) \
1445 EVP_PKEY_CTX_ctrl(ctx, -1, -1, \
1446 EVP_PKEY_CTRL_SET1_ID, (int)id_len, (void*)(id))
1448 # define EVP_PKEY_CTX_get1_id(ctx, id) \
1449 EVP_PKEY_CTX_ctrl(ctx, -1, -1, \
1450 EVP_PKEY_CTRL_GET1_ID, 0, (void*)(id))
1452 # define EVP_PKEY_CTX_get1_id_len(ctx, id_len) \
1453 EVP_PKEY_CTX_ctrl(ctx, -1, -1, \
1454 EVP_PKEY_CTRL_GET1_ID_LEN, 0, (void*)(id_len))
1456 # define EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID (EVP_PKEY_ALG_CTRL + 1)
1457 # define EVP_PKEY_CTRL_EC_PARAM_ENC (EVP_PKEY_ALG_CTRL + 2)
1458 # define EVP_PKEY_CTRL_EC_ECDH_COFACTOR (EVP_PKEY_ALG_CTRL + 3)
1459 # define EVP_PKEY_CTRL_EC_KDF_TYPE (EVP_PKEY_ALG_CTRL + 4)
1460 # define EVP_PKEY_CTRL_EC_KDF_MD (EVP_PKEY_ALG_CTRL + 5)
1461 # define EVP_PKEY_CTRL_GET_EC_KDF_MD (EVP_PKEY_ALG_CTRL + 6)
1462 # define EVP_PKEY_CTRL_EC_KDF_OUTLEN (EVP_PKEY_ALG_CTRL + 7)
1463 # define EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN (EVP_PKEY_ALG_CTRL + 8)
1464 # define EVP_PKEY_CTRL_EC_KDF_UKM (EVP_PKEY_ALG_CTRL + 9)
1465 # define EVP_PKEY_CTRL_GET_EC_KDF_UKM (EVP_PKEY_ALG_CTRL + 10)
1466 # define EVP_PKEY_CTRL_SET1_ID (EVP_PKEY_ALG_CTRL + 11)
1467 # define EVP_PKEY_CTRL_GET1_ID (EVP_PKEY_ALG_CTRL + 12)
1468 # define EVP_PKEY_CTRL_GET1_ID_LEN (EVP_PKEY_ALG_CTRL + 13)
1470 # define EVP_PKEY_ECDH_KDF_NONE 1
1471 # define EVP_PKEY_ECDH_KDF_X9_63 2
1472 /** The old name for EVP_PKEY_ECDH_KDF_X9_63
1473 * The ECDH KDF specification has been mistakingly attributed to ANSI X9.62,
1474 * it is actually specified in ANSI X9.63.
1475 * This identifier is retained for backwards compatibility
1477 # define EVP_PKEY_ECDH_KDF_X9_62 EVP_PKEY_ECDH_KDF_X9_63