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 OpenSSL license (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
14 # include <openssl/opensslconf.h>
16 # ifndef OPENSSL_NO_EC
17 # include <openssl/asn1.h>
18 # include <openssl/symhacks.h>
19 # if OPENSSL_API_COMPAT < 0x10100000L
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 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 void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag);
216 int EC_GROUP_get_asn1_flag(const EC_GROUP *group);
218 void EC_GROUP_set_point_conversion_form(EC_GROUP *group,
219 point_conversion_form_t form);
220 point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP *);
222 unsigned char *EC_GROUP_get0_seed(const EC_GROUP *x);
223 size_t EC_GROUP_get_seed_len(const EC_GROUP *);
224 size_t EC_GROUP_set_seed(EC_GROUP *, const unsigned char *, size_t len);
226 /** Sets the parameters of a ec curve defined by y^2 = x^3 + a*x + b (for GFp)
227 * or y^2 + x*y = x^3 + a*x^2 + b (for GF2m)
228 * \param group EC_GROUP object
229 * \param p BIGNUM with the prime number (GFp) or the polynomial
230 * defining the underlying field (GF2m)
231 * \param a BIGNUM with parameter a of the equation
232 * \param b BIGNUM with parameter b of the equation
233 * \param ctx BN_CTX object (optional)
234 * \return 1 on success and 0 if an error occurred
236 int EC_GROUP_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
237 const BIGNUM *b, BN_CTX *ctx);
239 /** Gets the parameters of the ec curve defined by y^2 = x^3 + a*x + b (for GFp)
240 * or y^2 + x*y = x^3 + a*x^2 + b (for GF2m)
241 * \param group EC_GROUP object
242 * \param p BIGNUM with the prime number (GFp) or the polynomial
243 * defining the underlying field (GF2m)
244 * \param a BIGNUM for parameter a of the equation
245 * \param b BIGNUM for parameter b of the equation
246 * \param ctx BN_CTX object (optional)
247 * \return 1 on success and 0 if an error occurred
249 int EC_GROUP_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b,
252 /** Sets the parameters of an ec curve. Synonym for EC_GROUP_set_curve
253 * \param group EC_GROUP object
254 * \param p BIGNUM with the prime number (GFp) or the polynomial
255 * defining the underlying field (GF2m)
256 * \param a BIGNUM with parameter a of the equation
257 * \param b BIGNUM with parameter b of the equation
258 * \param ctx BN_CTX object (optional)
259 * \return 1 on success and 0 if an error occurred
261 int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
262 const BIGNUM *b, BN_CTX *ctx);
264 /** Gets the parameters of an ec curve. Synonym for EC_GROUP_get_curve
265 * \param group EC_GROUP object
266 * \param p BIGNUM with the prime number (GFp) or the polynomial
267 * defining the underlying field (GF2m)
268 * \param a BIGNUM for parameter a of the equation
269 * \param b BIGNUM for parameter b of the equation
270 * \param ctx BN_CTX object (optional)
271 * \return 1 on success and 0 if an error occurred
273 int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
274 BIGNUM *b, BN_CTX *ctx);
276 # ifndef OPENSSL_NO_EC2M
277 /** Sets the parameter of an ec curve. Synonym for EC_GROUP_set_curve
278 * \param group EC_GROUP object
279 * \param p BIGNUM with the prime number (GFp) or the polynomial
280 * defining the underlying field (GF2m)
281 * \param a BIGNUM with parameter a of the equation
282 * \param b BIGNUM with parameter b of the equation
283 * \param ctx BN_CTX object (optional)
284 * \return 1 on success and 0 if an error occurred
286 int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
287 const BIGNUM *b, BN_CTX *ctx);
289 /** Gets the parameters of an ec curve. Synonym for EC_GROUP_get_curve
290 * \param group EC_GROUP object
291 * \param p BIGNUM with the prime number (GFp) or the polynomial
292 * defining the underlying field (GF2m)
293 * \param a BIGNUM for parameter a of the equation
294 * \param b BIGNUM for parameter b of the equation
295 * \param ctx BN_CTX object (optional)
296 * \return 1 on success and 0 if an error occurred
298 int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
299 BIGNUM *b, BN_CTX *ctx);
301 /** Returns the number of bits needed to represent a field element
302 * \param group EC_GROUP object
303 * \return number of bits needed to represent a field element
305 int EC_GROUP_get_degree(const EC_GROUP *group);
307 /** Checks whether the parameter in the EC_GROUP define a valid ec group
308 * \param group EC_GROUP object
309 * \param ctx BN_CTX object (optional)
310 * \return 1 if group is a valid ec group and 0 otherwise
312 int EC_GROUP_check(const EC_GROUP *group, BN_CTX *ctx);
314 /** Checks whether the discriminant of the elliptic curve is zero or not
315 * \param group EC_GROUP object
316 * \param ctx BN_CTX object (optional)
317 * \return 1 if the discriminant is not zero and 0 otherwise
319 int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx);
321 /** Compares two EC_GROUP objects
322 * \param a first EC_GROUP object
323 * \param b second EC_GROUP object
324 * \param ctx BN_CTX object (optional)
325 * \return 0 if the groups are equal, 1 if not, or -1 on error
327 int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx);
330 * EC_GROUP_new_GF*() calls EC_GROUP_new() and EC_GROUP_set_GF*() after
331 * choosing an appropriate EC_METHOD
334 /** Creates a new EC_GROUP object with the specified parameters defined
335 * over GFp (defined by the equation y^2 = x^3 + a*x + b)
336 * \param p BIGNUM with the prime number
337 * \param a BIGNUM with the parameter a of the equation
338 * \param b BIGNUM with the parameter b of the equation
339 * \param ctx BN_CTX object (optional)
340 * \return newly created EC_GROUP object with the specified parameters
342 EC_GROUP *EC_GROUP_new_curve_GFp(const BIGNUM *p, const BIGNUM *a,
343 const BIGNUM *b, BN_CTX *ctx);
344 # ifndef OPENSSL_NO_EC2M
345 /** Creates a new EC_GROUP object with the specified parameters defined
346 * over GF2m (defined by the equation y^2 + x*y = x^3 + a*x^2 + b)
347 * \param p BIGNUM with the polynomial defining the underlying field
348 * \param a BIGNUM with the parameter a of the equation
349 * \param b BIGNUM with the parameter b of the equation
350 * \param ctx BN_CTX object (optional)
351 * \return newly created EC_GROUP object with the specified parameters
353 EC_GROUP *EC_GROUP_new_curve_GF2m(const BIGNUM *p, const BIGNUM *a,
354 const BIGNUM *b, BN_CTX *ctx);
357 /** Creates a EC_GROUP object with a curve specified by a NID
358 * \param nid NID of the OID of the curve name
359 * \return newly created EC_GROUP object with specified curve or NULL
360 * if an error occurred
362 EC_GROUP *EC_GROUP_new_by_curve_name(int nid);
364 /** Creates a new EC_GROUP object from an ECPARAMETERS object
365 * \param params pointer to the ECPARAMETERS object
366 * \return newly created EC_GROUP object with specified curve or NULL
367 * if an error occurred
369 EC_GROUP *EC_GROUP_new_from_ecparameters(const ECPARAMETERS *params);
371 /** Creates an ECPARAMETERS object for the given EC_GROUP object.
372 * \param group pointer to the EC_GROUP object
373 * \param params pointer to an existing ECPARAMETERS object or NULL
374 * \return pointer to the new ECPARAMETERS object or NULL
375 * if an error occurred.
377 ECPARAMETERS *EC_GROUP_get_ecparameters(const EC_GROUP *group,
378 ECPARAMETERS *params);
380 /** Creates a new EC_GROUP object from an ECPKPARAMETERS object
381 * \param params pointer to an existing ECPKPARAMETERS object, or NULL
382 * \return newly created EC_GROUP object with specified curve, or NULL
383 * if an error occurred
385 EC_GROUP *EC_GROUP_new_from_ecpkparameters(const ECPKPARAMETERS *params);
387 /** Creates an ECPKPARAMETERS object for the given EC_GROUP object.
388 * \param group pointer to the EC_GROUP object
389 * \param params pointer to an existing ECPKPARAMETERS object or NULL
390 * \return pointer to the new ECPKPARAMETERS object or NULL
391 * if an error occurred.
393 ECPKPARAMETERS *EC_GROUP_get_ecpkparameters(const EC_GROUP *group,
394 ECPKPARAMETERS *params);
396 /********************************************************************/
397 /* handling of internal curves */
398 /********************************************************************/
406 * EC_builtin_curves(EC_builtin_curve *r, size_t size) returns number of all
407 * available curves or zero if a error occurred. In case r is not zero,
408 * nitems EC_builtin_curve structures are filled with the data of the first
409 * nitems internal groups
411 size_t EC_get_builtin_curves(EC_builtin_curve *r, size_t nitems);
413 const char *EC_curve_nid2nist(int nid);
414 int EC_curve_nist2nid(const char *name);
416 /********************************************************************/
417 /* EC_POINT functions */
418 /********************************************************************/
420 /** Creates a new EC_POINT object for the specified EC_GROUP
421 * \param group EC_GROUP the underlying EC_GROUP object
422 * \return newly created EC_POINT object or NULL if an error occurred
424 EC_POINT *EC_POINT_new(const EC_GROUP *group);
426 /** Frees a EC_POINT object
427 * \param point EC_POINT object to be freed
429 void EC_POINT_free(EC_POINT *point);
431 /** Clears and frees a EC_POINT object
432 * \param point EC_POINT object to be cleared and freed
434 void EC_POINT_clear_free(EC_POINT *point);
436 /** Copies EC_POINT object
437 * \param dst destination EC_POINT object
438 * \param src source EC_POINT object
439 * \return 1 on success and 0 if an error occurred
441 int EC_POINT_copy(EC_POINT *dst, const EC_POINT *src);
443 /** Creates a new EC_POINT object and copies the content of the supplied
445 * \param src source EC_POINT object
446 * \param group underlying the EC_GROUP object
447 * \return newly created EC_POINT object or NULL if an error occurred
449 EC_POINT *EC_POINT_dup(const EC_POINT *src, const EC_GROUP *group);
451 /** Returns the EC_METHOD used in EC_POINT object
452 * \param point EC_POINT object
453 * \return the EC_METHOD used
455 const EC_METHOD *EC_POINT_method_of(const EC_POINT *point);
457 /** Sets a point to infinity (neutral element)
458 * \param group underlying EC_GROUP object
459 * \param point EC_POINT to set to infinity
460 * \return 1 on success and 0 if an error occurred
462 int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point);
464 /** Sets the jacobian projective coordinates of a EC_POINT over GFp
465 * \param group underlying EC_GROUP object
466 * \param p EC_POINT object
467 * \param x BIGNUM with the x-coordinate
468 * \param y BIGNUM with the y-coordinate
469 * \param z BIGNUM with the z-coordinate
470 * \param ctx BN_CTX object (optional)
471 * \return 1 on success and 0 if an error occurred
473 int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
474 EC_POINT *p, const BIGNUM *x,
475 const BIGNUM *y, const BIGNUM *z,
478 /** Gets the jacobian projective coordinates of a EC_POINT over GFp
479 * \param group underlying EC_GROUP object
480 * \param p EC_POINT object
481 * \param x BIGNUM for the x-coordinate
482 * \param y BIGNUM for the y-coordinate
483 * \param z BIGNUM for the z-coordinate
484 * \param ctx BN_CTX object (optional)
485 * \return 1 on success and 0 if an error occurred
487 int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
488 const EC_POINT *p, BIGNUM *x,
489 BIGNUM *y, BIGNUM *z,
492 /** Sets the affine coordinates of an EC_POINT
493 * \param group underlying EC_GROUP object
494 * \param p EC_POINT object
495 * \param x BIGNUM with the x-coordinate
496 * \param y BIGNUM with the y-coordinate
497 * \param ctx BN_CTX object (optional)
498 * \return 1 on success and 0 if an error occurred
500 int EC_POINT_set_affine_coordinates(const EC_GROUP *group, EC_POINT *p,
501 const BIGNUM *x, const BIGNUM *y,
504 /** Gets the affine coordinates of an EC_POINT.
505 * \param group underlying EC_GROUP object
506 * \param p EC_POINT object
507 * \param x BIGNUM for the x-coordinate
508 * \param y BIGNUM for the y-coordinate
509 * \param ctx BN_CTX object (optional)
510 * \return 1 on success and 0 if an error occurred
512 int EC_POINT_get_affine_coordinates(const EC_GROUP *group, const EC_POINT *p,
513 BIGNUM *x, BIGNUM *y, BN_CTX *ctx);
515 /** Sets the affine coordinates of an EC_POINT. A synonym of
516 * EC_POINT_set_affine_coordinates
517 * \param group underlying EC_GROUP object
518 * \param p EC_POINT object
519 * \param x BIGNUM with the x-coordinate
520 * \param y BIGNUM with the y-coordinate
521 * \param ctx BN_CTX object (optional)
522 * \return 1 on success and 0 if an error occurred
524 int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group, EC_POINT *p,
525 const BIGNUM *x, const BIGNUM *y,
528 /** Gets the affine coordinates of an EC_POINT. A synonym of
529 * EC_POINT_get_affine_coordinates
530 * \param group underlying EC_GROUP object
531 * \param p EC_POINT object
532 * \param x BIGNUM for the x-coordinate
533 * \param y BIGNUM for the y-coordinate
534 * \param ctx BN_CTX object (optional)
535 * \return 1 on success and 0 if an error occurred
537 int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group,
538 const EC_POINT *p, BIGNUM *x,
539 BIGNUM *y, BN_CTX *ctx);
541 /** Sets the x9.62 compressed coordinates of a EC_POINT
542 * \param group underlying EC_GROUP object
543 * \param p EC_POINT object
544 * \param x BIGNUM with x-coordinate
545 * \param y_bit integer with the y-Bit (either 0 or 1)
546 * \param ctx BN_CTX object (optional)
547 * \return 1 on success and 0 if an error occurred
549 int EC_POINT_set_compressed_coordinates(const EC_GROUP *group, EC_POINT *p,
550 const BIGNUM *x, int y_bit,
553 /** Sets the x9.62 compressed coordinates of a EC_POINT. A synonym of
554 * EC_POINT_set_compressed_coordinates
555 * \param group underlying EC_GROUP object
556 * \param p EC_POINT object
557 * \param x BIGNUM with x-coordinate
558 * \param y_bit integer with the y-Bit (either 0 or 1)
559 * \param ctx BN_CTX object (optional)
560 * \return 1 on success and 0 if an error occurred
562 int EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group,
563 EC_POINT *p, const BIGNUM *x,
564 int y_bit, BN_CTX *ctx);
565 # ifndef OPENSSL_NO_EC2M
566 /** Sets the affine coordinates of an EC_POINT. A synonym of
567 * EC_POINT_set_affine_coordinates
568 * \param group underlying EC_GROUP object
569 * \param p EC_POINT object
570 * \param x BIGNUM with the x-coordinate
571 * \param y BIGNUM with the y-coordinate
572 * \param ctx BN_CTX object (optional)
573 * \return 1 on success and 0 if an error occurred
575 int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group, EC_POINT *p,
576 const BIGNUM *x, const BIGNUM *y,
579 /** Gets the affine coordinates of an EC_POINT. A synonym of
580 * EC_POINT_get_affine_coordinates
581 * \param group underlying EC_GROUP object
582 * \param p EC_POINT object
583 * \param x BIGNUM for the x-coordinate
584 * \param y BIGNUM for the y-coordinate
585 * \param ctx BN_CTX object (optional)
586 * \return 1 on success and 0 if an error occurred
588 int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group,
589 const EC_POINT *p, BIGNUM *x,
590 BIGNUM *y, BN_CTX *ctx);
592 /** Sets the x9.62 compressed coordinates of a EC_POINT. A synonym of
593 * EC_POINT_set_compressed_coordinates
594 * \param group underlying EC_GROUP object
595 * \param p EC_POINT object
596 * \param x BIGNUM with x-coordinate
597 * \param y_bit integer with the y-Bit (either 0 or 1)
598 * \param ctx BN_CTX object (optional)
599 * \return 1 on success and 0 if an error occurred
601 int EC_POINT_set_compressed_coordinates_GF2m(const EC_GROUP *group,
602 EC_POINT *p, const BIGNUM *x,
603 int y_bit, BN_CTX *ctx);
605 /** Encodes a EC_POINT object to a octet string
606 * \param group underlying EC_GROUP object
607 * \param p EC_POINT object
608 * \param form point conversion form
609 * \param buf memory buffer for the result. If NULL the function returns
610 * required buffer size.
611 * \param len length of the memory buffer
612 * \param ctx BN_CTX object (optional)
613 * \return the length of the encoded octet string or 0 if an error occurred
615 size_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *p,
616 point_conversion_form_t form,
617 unsigned char *buf, size_t len, BN_CTX *ctx);
619 /** Decodes a EC_POINT from a octet string
620 * \param group underlying EC_GROUP object
621 * \param p EC_POINT object
622 * \param buf memory buffer with the encoded ec point
623 * \param len length of the encoded ec point
624 * \param ctx BN_CTX object (optional)
625 * \return 1 on success and 0 if an error occurred
627 int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *p,
628 const unsigned char *buf, size_t len, BN_CTX *ctx);
630 /** Encodes an EC_POINT object to an allocated octet string
631 * \param group underlying EC_GROUP object
632 * \param point EC_POINT object
633 * \param form point conversion form
634 * \param pbuf returns pointer to allocated 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_point2buf(const EC_GROUP *group, const EC_POINT *point,
639 point_conversion_form_t form,
640 unsigned char **pbuf, BN_CTX *ctx);
642 /* other interfaces to point2oct/oct2point: */
643 BIGNUM *EC_POINT_point2bn(const EC_GROUP *, const EC_POINT *,
644 point_conversion_form_t form, BIGNUM *, BN_CTX *);
645 EC_POINT *EC_POINT_bn2point(const EC_GROUP *, const BIGNUM *,
646 EC_POINT *, BN_CTX *);
647 char *EC_POINT_point2hex(const EC_GROUP *, const EC_POINT *,
648 point_conversion_form_t form, BN_CTX *);
649 EC_POINT *EC_POINT_hex2point(const EC_GROUP *, const char *,
650 EC_POINT *, BN_CTX *);
652 /********************************************************************/
653 /* functions for doing EC_POINT arithmetic */
654 /********************************************************************/
656 /** Computes the sum of two EC_POINT
657 * \param group underlying EC_GROUP object
658 * \param r EC_POINT object for the result (r = a + b)
659 * \param a EC_POINT object with the first summand
660 * \param b EC_POINT object with the second summand
661 * \param ctx BN_CTX object (optional)
662 * \return 1 on success and 0 if an error occurred
664 int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
665 const EC_POINT *b, BN_CTX *ctx);
667 /** Computes the double of a EC_POINT
668 * \param group underlying EC_GROUP object
669 * \param r EC_POINT object for the result (r = 2 * a)
670 * \param a EC_POINT object
671 * \param ctx BN_CTX object (optional)
672 * \return 1 on success and 0 if an error occurred
674 int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
677 /** Computes the inverse of a EC_POINT
678 * \param group underlying EC_GROUP object
679 * \param a EC_POINT object to be inverted (it's used for the result as well)
680 * \param ctx BN_CTX object (optional)
681 * \return 1 on success and 0 if an error occurred
683 int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx);
685 /** Checks whether the point is the neutral element of the group
686 * \param group the underlying EC_GROUP object
687 * \param p EC_POINT object
688 * \return 1 if the point is the neutral element and 0 otherwise
690 int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *p);
692 /** Checks whether the point is on the curve
693 * \param group underlying EC_GROUP object
694 * \param point EC_POINT object to check
695 * \param ctx BN_CTX object (optional)
696 * \return 1 if the point is on the curve, 0 if not, or -1 on error
698 int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
701 /** Compares two EC_POINTs
702 * \param group underlying EC_GROUP object
703 * \param a first EC_POINT object
704 * \param b second EC_POINT object
705 * \param ctx BN_CTX object (optional)
706 * \return 1 if the points are not equal, 0 if they are, or -1 on error
708 int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b,
711 int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx);
712 int EC_POINTs_make_affine(const EC_GROUP *group, size_t num,
713 EC_POINT *points[], BN_CTX *ctx);
715 /** Computes r = generator * n + sum_{i=0}^{num-1} p[i] * m[i]
716 * \param group underlying EC_GROUP object
717 * \param r EC_POINT object for the result
718 * \param n BIGNUM with the multiplier for the group generator (optional)
719 * \param num number further summands
720 * \param p array of size num of EC_POINT objects
721 * \param m array of size num of BIGNUM objects
722 * \param ctx BN_CTX object (optional)
723 * \return 1 on success and 0 if an error occurred
725 int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *n,
726 size_t num, const EC_POINT *p[], const BIGNUM *m[],
729 /** Computes r = generator * n + q * m
730 * \param group underlying EC_GROUP object
731 * \param r EC_POINT object for the result
732 * \param n BIGNUM with the multiplier for the group generator (optional)
733 * \param q EC_POINT object with the first factor of the second summand
734 * \param m BIGNUM with the second factor of the second summand
735 * \param ctx BN_CTX object (optional)
736 * \return 1 on success and 0 if an error occurred
738 int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *n,
739 const EC_POINT *q, const BIGNUM *m, BN_CTX *ctx);
741 /** Stores multiples of generator for faster point multiplication
742 * \param group EC_GROUP object
743 * \param ctx BN_CTX object (optional)
744 * \return 1 on success and 0 if an error occurred
746 int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
748 /** Reports whether a precomputation has been done
749 * \param group EC_GROUP object
750 * \return 1 if a pre-computation has been done and 0 otherwise
752 int EC_GROUP_have_precompute_mult(const EC_GROUP *group);
754 /********************************************************************/
756 /********************************************************************/
758 DECLARE_ASN1_ITEM(ECPKPARAMETERS)
759 DECLARE_ASN1_ALLOC_FUNCTIONS(ECPKPARAMETERS)
760 DECLARE_ASN1_ITEM(ECPARAMETERS)
761 DECLARE_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS)
764 * EC_GROUP_get_basis_type() returns the NID of the basis type used to
765 * represent the field elements
767 int EC_GROUP_get_basis_type(const EC_GROUP *);
768 # ifndef OPENSSL_NO_EC2M
769 int EC_GROUP_get_trinomial_basis(const EC_GROUP *, unsigned int *k);
770 int EC_GROUP_get_pentanomial_basis(const EC_GROUP *, unsigned int *k1,
771 unsigned int *k2, unsigned int *k3);
774 # define OPENSSL_EC_EXPLICIT_CURVE 0x000
775 # define OPENSSL_EC_NAMED_CURVE 0x001
777 EC_GROUP *d2i_ECPKParameters(EC_GROUP **, const unsigned char **in, long len);
778 int i2d_ECPKParameters(const EC_GROUP *, unsigned char **out);
780 # define d2i_ECPKParameters_bio(bp,x) ASN1_d2i_bio_of(EC_GROUP,NULL,d2i_ECPKParameters,bp,x)
781 # define i2d_ECPKParameters_bio(bp,x) ASN1_i2d_bio_of_const(EC_GROUP,i2d_ECPKParameters,bp,x)
782 # define d2i_ECPKParameters_fp(fp,x) (EC_GROUP *)ASN1_d2i_fp(NULL, \
783 (char *(*)())d2i_ECPKParameters,(fp),(unsigned char **)(x))
784 # define i2d_ECPKParameters_fp(fp,x) ASN1_i2d_fp(i2d_ECPKParameters,(fp), \
785 (unsigned char *)(x))
787 int ECPKParameters_print(BIO *bp, const EC_GROUP *x, int off);
788 # ifndef OPENSSL_NO_STDIO
789 int ECPKParameters_print_fp(FILE *fp, const EC_GROUP *x, int off);
792 /********************************************************************/
793 /* EC_KEY functions */
794 /********************************************************************/
796 /* some values for the encoding_flag */
797 # define EC_PKEY_NO_PARAMETERS 0x001
798 # define EC_PKEY_NO_PUBKEY 0x002
800 /* some values for the flags field */
801 # define EC_FLAG_NON_FIPS_ALLOW 0x1
802 # define EC_FLAG_FIPS_CHECKED 0x2
803 # define EC_FLAG_COFACTOR_ECDH 0x1000
805 /** Creates a new EC_KEY object.
806 * \return EC_KEY object or NULL if an error occurred.
808 EC_KEY *EC_KEY_new(void);
810 int EC_KEY_get_flags(const EC_KEY *key);
812 void EC_KEY_set_flags(EC_KEY *key, int flags);
814 void EC_KEY_clear_flags(EC_KEY *key, int flags);
816 /** Creates a new EC_KEY object using a named curve as underlying
818 * \param nid NID of the named curve.
819 * \return EC_KEY object or NULL if an error occurred.
821 EC_KEY *EC_KEY_new_by_curve_name(int nid);
823 /** Frees a EC_KEY object.
824 * \param key EC_KEY object to be freed.
826 void EC_KEY_free(EC_KEY *key);
828 /** Copies a EC_KEY object.
829 * \param dst destination EC_KEY object
830 * \param src src EC_KEY object
831 * \return dst or NULL if an error occurred.
833 EC_KEY *EC_KEY_copy(EC_KEY *dst, const EC_KEY *src);
835 /** Creates a new EC_KEY object and copies the content from src to it.
836 * \param src the source EC_KEY object
837 * \return newly created EC_KEY object or NULL if an error occurred.
839 EC_KEY *EC_KEY_dup(const EC_KEY *src);
841 /** Increases the internal reference count of a EC_KEY object.
842 * \param key EC_KEY object
843 * \return 1 on success and 0 if an error occurred.
845 int EC_KEY_up_ref(EC_KEY *key);
847 /** Returns the ENGINE object of a EC_KEY object
848 * \param eckey EC_KEY object
849 * \return the ENGINE object (possibly NULL).
851 ENGINE *EC_KEY_get0_engine(const EC_KEY *eckey);
853 /** Returns the EC_GROUP object of a EC_KEY object
854 * \param key EC_KEY object
855 * \return the EC_GROUP object (possibly NULL).
857 const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key);
859 /** Sets the EC_GROUP of a EC_KEY object.
860 * \param key EC_KEY object
861 * \param group EC_GROUP to use in the EC_KEY object (note: the EC_KEY
862 * object will use an own copy of the EC_GROUP).
863 * \return 1 on success and 0 if an error occurred.
865 int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group);
867 /** Returns the private key of a EC_KEY object.
868 * \param key EC_KEY object
869 * \return a BIGNUM with the private key (possibly NULL).
871 const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key);
873 /** Sets the private key of a EC_KEY object.
874 * \param key EC_KEY object
875 * \param prv BIGNUM with the private key (note: the EC_KEY object
876 * will use an own copy of the BIGNUM).
877 * \return 1 on success and 0 if an error occurred.
879 int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *prv);
881 /** Returns the public key of a EC_KEY object.
882 * \param key the EC_KEY object
883 * \return a EC_POINT object with the public key (possibly NULL)
885 const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key);
887 /** Sets the public key of a EC_KEY object.
888 * \param key EC_KEY object
889 * \param pub EC_POINT object with the public key (note: the EC_KEY object
890 * will use an own copy of the EC_POINT object).
891 * \return 1 on success and 0 if an error occurred.
893 int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub);
895 unsigned EC_KEY_get_enc_flags(const EC_KEY *key);
896 void EC_KEY_set_enc_flags(EC_KEY *eckey, unsigned int flags);
897 point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key);
898 void EC_KEY_set_conv_form(EC_KEY *eckey, point_conversion_form_t cform);
900 #define EC_KEY_get_ex_new_index(l, p, newf, dupf, freef) \
901 CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_EC_KEY, l, p, newf, dupf, freef)
902 int EC_KEY_set_ex_data(EC_KEY *key, int idx, void *arg);
903 void *EC_KEY_get_ex_data(const EC_KEY *key, int idx);
905 /* wrapper functions for the underlying EC_GROUP object */
906 void EC_KEY_set_asn1_flag(EC_KEY *eckey, int asn1_flag);
908 /** Creates a table of pre-computed multiples of the generator to
909 * accelerate further EC_KEY operations.
910 * \param key EC_KEY object
911 * \param ctx BN_CTX object (optional)
912 * \return 1 on success and 0 if an error occurred.
914 int EC_KEY_precompute_mult(EC_KEY *key, BN_CTX *ctx);
916 /** Creates a new ec private (and optional a new public) key.
917 * \param key EC_KEY object
918 * \return 1 on success and 0 if an error occurred.
920 int EC_KEY_generate_key(EC_KEY *key);
922 /** Verifies that a private and/or public key is valid.
923 * \param key the EC_KEY object
924 * \return 1 on success and 0 otherwise.
926 int EC_KEY_check_key(const EC_KEY *key);
928 /** Indicates if an EC_KEY can be used for signing.
929 * \param eckey the EC_KEY object
930 * \return 1 if can can sign and 0 otherwise.
932 int EC_KEY_can_sign(const EC_KEY *eckey);
934 /** Sets a public key from affine coordinates performing
935 * necessary NIST PKV tests.
936 * \param key the EC_KEY object
937 * \param x public key x coordinate
938 * \param y public key y coordinate
939 * \return 1 on success and 0 otherwise.
941 int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x,
944 /** Encodes an EC_KEY public key to an allocated octet string
945 * \param key key to encode
946 * \param form point conversion form
947 * \param pbuf returns pointer to allocated buffer
948 * \param ctx BN_CTX object (optional)
949 * \return the length of the encoded octet string or 0 if an error occurred
951 size_t EC_KEY_key2buf(const EC_KEY *key, point_conversion_form_t form,
952 unsigned char **pbuf, BN_CTX *ctx);
954 /** Decodes a EC_KEY public key from a octet string
955 * \param key key to decode
956 * \param buf memory buffer with the encoded ec point
957 * \param len length of the encoded ec point
958 * \param ctx BN_CTX object (optional)
959 * \return 1 on success and 0 if an error occurred
962 int EC_KEY_oct2key(EC_KEY *key, const unsigned char *buf, size_t len,
965 /** Decodes an EC_KEY private key from an octet string
966 * \param key key to decode
967 * \param buf memory buffer with the encoded private key
968 * \param len length of the encoded key
969 * \return 1 on success and 0 if an error occurred
972 int EC_KEY_oct2priv(EC_KEY *key, const unsigned char *buf, size_t len);
974 /** Encodes a EC_KEY private key to an octet string
975 * \param key key to encode
976 * \param buf memory buffer for the result. If NULL the function returns
977 * required buffer size.
978 * \param len length of the memory buffer
979 * \return the length of the encoded octet string or 0 if an error occurred
982 size_t EC_KEY_priv2oct(const EC_KEY *key, unsigned char *buf, size_t len);
984 /** Encodes an EC_KEY private key to an allocated octet string
985 * \param eckey key to encode
986 * \param pbuf returns pointer to allocated buffer
987 * \return the length of the encoded octet string or 0 if an error occurred
989 size_t EC_KEY_priv2buf(const EC_KEY *eckey, unsigned char **pbuf);
991 /********************************************************************/
992 /* de- and encoding functions for SEC1 ECPrivateKey */
993 /********************************************************************/
995 /** Decodes a private key from a memory buffer.
996 * \param key a pointer to a EC_KEY object which should be used (or NULL)
997 * \param in pointer to memory with the DER encoded private key
998 * \param len length of the DER encoded private key
999 * \return the decoded private key or NULL if an error occurred.
1001 EC_KEY *d2i_ECPrivateKey(EC_KEY **key, const unsigned char **in, long len);
1003 /** Encodes a private key object and stores the result in a buffer.
1004 * \param key the EC_KEY object to encode
1005 * \param out the buffer for the result (if NULL the function returns number
1007 * \return 1 on success and 0 if an error occurred.
1009 int i2d_ECPrivateKey(EC_KEY *key, unsigned char **out);
1011 /********************************************************************/
1012 /* de- and encoding functions for EC parameters */
1013 /********************************************************************/
1015 /** Decodes ec parameter from a memory buffer.
1016 * \param key a pointer to a EC_KEY object which should be used (or NULL)
1017 * \param in pointer to memory with the DER encoded ec parameters
1018 * \param len length of the DER encoded ec parameters
1019 * \return a EC_KEY object with the decoded parameters or NULL if an error
1022 EC_KEY *d2i_ECParameters(EC_KEY **key, const unsigned char **in, long len);
1024 /** Encodes ec parameter and stores the result in a buffer.
1025 * \param key the EC_KEY object with ec parameters to encode
1026 * \param out the buffer for the result (if NULL the function returns number
1028 * \return 1 on success and 0 if an error occurred.
1030 int i2d_ECParameters(EC_KEY *key, unsigned char **out);
1032 /********************************************************************/
1033 /* de- and encoding functions for EC public key */
1034 /* (octet string, not DER -- hence 'o2i' and 'i2o') */
1035 /********************************************************************/
1037 /** Decodes a ec public key from a octet string.
1038 * \param key a pointer to a EC_KEY object which should be used
1039 * \param in memory buffer with the encoded public key
1040 * \param len length of the encoded public key
1041 * \return EC_KEY object with decoded public key or NULL if an error
1044 EC_KEY *o2i_ECPublicKey(EC_KEY **key, const unsigned char **in, long len);
1046 /** Encodes a ec public key in an octet string.
1047 * \param key the EC_KEY object with the public key
1048 * \param out the buffer for the result (if NULL the function returns number
1050 * \return 1 on success and 0 if an error occurred
1052 int i2o_ECPublicKey(const EC_KEY *key, unsigned char **out);
1054 /** Prints out the ec parameters on human readable form.
1055 * \param bp BIO object to which the information is printed
1056 * \param key EC_KEY object
1057 * \return 1 on success and 0 if an error occurred
1059 int ECParameters_print(BIO *bp, const EC_KEY *key);
1061 /** Prints out the contents of a EC_KEY object
1062 * \param bp BIO object to which the information is printed
1063 * \param key EC_KEY object
1064 * \param off line offset
1065 * \return 1 on success and 0 if an error occurred
1067 int EC_KEY_print(BIO *bp, const EC_KEY *key, int off);
1069 # ifndef OPENSSL_NO_STDIO
1070 /** Prints out the ec parameters on human readable form.
1071 * \param fp file descriptor to which the information is printed
1072 * \param key EC_KEY object
1073 * \return 1 on success and 0 if an error occurred
1075 int ECParameters_print_fp(FILE *fp, const EC_KEY *key);
1077 /** Prints out the contents of a EC_KEY object
1078 * \param fp file descriptor to which the information is printed
1079 * \param key EC_KEY object
1080 * \param off line offset
1081 * \return 1 on success and 0 if an error occurred
1083 int EC_KEY_print_fp(FILE *fp, const EC_KEY *key, int off);
1087 const EC_KEY_METHOD *EC_KEY_OpenSSL(void);
1088 const EC_KEY_METHOD *EC_KEY_get_default_method(void);
1089 void EC_KEY_set_default_method(const EC_KEY_METHOD *meth);
1090 const EC_KEY_METHOD *EC_KEY_get_method(const EC_KEY *key);
1091 int EC_KEY_set_method(EC_KEY *key, const EC_KEY_METHOD *meth);
1092 EC_KEY *EC_KEY_new_method(ENGINE *engine);
1094 int ECDH_KDF_X9_62(unsigned char *out, size_t outlen,
1095 const unsigned char *Z, size_t Zlen,
1096 const unsigned char *sinfo, size_t sinfolen,
1099 int ECDH_compute_key(void *out, size_t outlen, const EC_POINT *pub_key,
1101 void *(*KDF) (const void *in, size_t inlen,
1102 void *out, size_t *outlen));
1104 typedef struct ECDSA_SIG_st ECDSA_SIG;
1106 /** Allocates and initialize a ECDSA_SIG structure
1107 * \return pointer to a ECDSA_SIG structure or NULL if an error occurred
1109 ECDSA_SIG *ECDSA_SIG_new(void);
1111 /** frees a ECDSA_SIG structure
1112 * \param sig pointer to the ECDSA_SIG structure
1114 void ECDSA_SIG_free(ECDSA_SIG *sig);
1116 /** DER encode content of ECDSA_SIG object (note: this function modifies *pp
1117 * (*pp += length of the DER encoded signature)).
1118 * \param sig pointer to the ECDSA_SIG object
1119 * \param pp pointer to a unsigned char pointer for the output or NULL
1120 * \return the length of the DER encoded ECDSA_SIG object or 0
1122 int i2d_ECDSA_SIG(const ECDSA_SIG *sig, unsigned char **pp);
1124 /** Decodes a DER encoded ECDSA signature (note: this function changes *pp
1126 * \param sig pointer to ECDSA_SIG pointer (may be NULL)
1127 * \param pp memory buffer with the DER encoded signature
1128 * \param len length of the buffer
1129 * \return pointer to the decoded ECDSA_SIG structure (or NULL)
1131 ECDSA_SIG *d2i_ECDSA_SIG(ECDSA_SIG **sig, const unsigned char **pp, long len);
1133 /** Accessor for r and s fields of ECDSA_SIG
1134 * \param sig pointer to ECDSA_SIG structure
1135 * \param pr pointer to BIGNUM pointer for r (may be NULL)
1136 * \param ps pointer to BIGNUM pointer for s (may be NULL)
1138 void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps);
1140 /** Accessor for r field of ECDSA_SIG
1141 * \param sig pointer to ECDSA_SIG structure
1143 const BIGNUM *ECDSA_SIG_get0_r(const ECDSA_SIG *sig);
1145 /** Accessor for s field of ECDSA_SIG
1146 * \param sig pointer to ECDSA_SIG structure
1148 const BIGNUM *ECDSA_SIG_get0_s(const ECDSA_SIG *sig);
1150 /** Setter for r and s fields of ECDSA_SIG
1151 * \param sig pointer to ECDSA_SIG structure
1152 * \param r pointer to BIGNUM for r (may be NULL)
1153 * \param s pointer to BIGNUM for s (may be NULL)
1155 int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s);
1157 /** Computes the ECDSA signature of the given hash value using
1158 * the supplied private key and returns the created signature.
1159 * \param dgst pointer to the hash value
1160 * \param dgst_len length of the hash value
1161 * \param eckey EC_KEY object containing a private EC key
1162 * \return pointer to a ECDSA_SIG structure or NULL if an error occurred
1164 ECDSA_SIG *ECDSA_do_sign(const unsigned char *dgst, int dgst_len,
1167 /** Computes ECDSA signature of a given hash value using the supplied
1168 * private key (note: sig must point to ECDSA_size(eckey) bytes of memory).
1169 * \param dgst pointer to the hash value to sign
1170 * \param dgstlen length of the hash value
1171 * \param kinv BIGNUM with a pre-computed inverse k (optional)
1172 * \param rp BIGNUM with a pre-computed rp value (optional),
1173 * see ECDSA_sign_setup
1174 * \param eckey EC_KEY object containing a private EC key
1175 * \return pointer to a ECDSA_SIG structure or NULL if an error occurred
1177 ECDSA_SIG *ECDSA_do_sign_ex(const unsigned char *dgst, int dgstlen,
1178 const BIGNUM *kinv, const BIGNUM *rp,
1181 /** Verifies that the supplied signature is a valid ECDSA
1182 * signature of the supplied hash value using the supplied public key.
1183 * \param dgst pointer to the hash value
1184 * \param dgst_len length of the hash value
1185 * \param sig ECDSA_SIG structure
1186 * \param eckey EC_KEY object containing a public EC key
1187 * \return 1 if the signature is valid, 0 if the signature is invalid
1190 int ECDSA_do_verify(const unsigned char *dgst, int dgst_len,
1191 const ECDSA_SIG *sig, EC_KEY *eckey);
1193 /** Precompute parts of the signing operation
1194 * \param eckey EC_KEY object containing a private EC key
1195 * \param ctx BN_CTX object (optional)
1196 * \param kinv BIGNUM pointer for the inverse of k
1197 * \param rp BIGNUM pointer for x coordinate of k * generator
1198 * \return 1 on success and 0 otherwise
1200 int ECDSA_sign_setup(EC_KEY *eckey, BN_CTX *ctx, BIGNUM **kinv, BIGNUM **rp);
1202 /** Computes ECDSA signature of a given hash value using the supplied
1203 * private key (note: sig must point to ECDSA_size(eckey) bytes of memory).
1204 * \param type this parameter is ignored
1205 * \param dgst pointer to the hash value to sign
1206 * \param dgstlen length of the hash value
1207 * \param sig memory for the DER encoded created signature
1208 * \param siglen pointer to the length of the returned signature
1209 * \param eckey EC_KEY object containing a private EC key
1210 * \return 1 on success and 0 otherwise
1212 int ECDSA_sign(int type, const unsigned char *dgst, int dgstlen,
1213 unsigned char *sig, unsigned int *siglen, EC_KEY *eckey);
1215 /** Computes ECDSA signature of a given hash value using the supplied
1216 * private key (note: sig must point to ECDSA_size(eckey) bytes of memory).
1217 * \param type this parameter is ignored
1218 * \param dgst pointer to the hash value to sign
1219 * \param dgstlen length of the hash value
1220 * \param sig buffer to hold the DER encoded signature
1221 * \param siglen pointer to the length of the returned signature
1222 * \param kinv BIGNUM with a pre-computed inverse k (optional)
1223 * \param rp BIGNUM with a pre-computed rp value (optional),
1224 * see ECDSA_sign_setup
1225 * \param eckey EC_KEY object containing a private EC key
1226 * \return 1 on success and 0 otherwise
1228 int ECDSA_sign_ex(int type, const unsigned char *dgst, int dgstlen,
1229 unsigned char *sig, unsigned int *siglen,
1230 const BIGNUM *kinv, const BIGNUM *rp, EC_KEY *eckey);
1232 /** Verifies that the given signature is valid ECDSA signature
1233 * of the supplied hash value using the specified public key.
1234 * \param type this parameter is ignored
1235 * \param dgst pointer to the hash value
1236 * \param dgstlen length of the hash value
1237 * \param sig pointer to the DER encoded signature
1238 * \param siglen length of the DER encoded signature
1239 * \param eckey EC_KEY object containing a public EC key
1240 * \return 1 if the signature is valid, 0 if the signature is invalid
1243 int ECDSA_verify(int type, const unsigned char *dgst, int dgstlen,
1244 const unsigned char *sig, int siglen, EC_KEY *eckey);
1246 /** Returns the maximum length of the DER encoded signature
1247 * \param eckey EC_KEY object
1248 * \return numbers of bytes required for the DER encoded signature
1250 int ECDSA_size(const EC_KEY *eckey);
1252 /********************************************************************/
1253 /* EC_KEY_METHOD constructors, destructors, writers and accessors */
1254 /********************************************************************/
1256 EC_KEY_METHOD *EC_KEY_METHOD_new(const EC_KEY_METHOD *meth);
1257 void EC_KEY_METHOD_free(EC_KEY_METHOD *meth);
1258 void EC_KEY_METHOD_set_init(EC_KEY_METHOD *meth,
1259 int (*init)(EC_KEY *key),
1260 void (*finish)(EC_KEY *key),
1261 int (*copy)(EC_KEY *dest, const EC_KEY *src),
1262 int (*set_group)(EC_KEY *key, const EC_GROUP *grp),
1263 int (*set_private)(EC_KEY *key,
1264 const BIGNUM *priv_key),
1265 int (*set_public)(EC_KEY *key,
1266 const EC_POINT *pub_key));
1268 void EC_KEY_METHOD_set_keygen(EC_KEY_METHOD *meth,
1269 int (*keygen)(EC_KEY *key));
1271 void EC_KEY_METHOD_set_compute_key(EC_KEY_METHOD *meth,
1272 int (*ckey)(unsigned char **psec,
1274 const EC_POINT *pub_key,
1275 const EC_KEY *ecdh));
1277 void EC_KEY_METHOD_set_sign(EC_KEY_METHOD *meth,
1278 int (*sign)(int type, const unsigned char *dgst,
1279 int dlen, unsigned char *sig,
1280 unsigned int *siglen,
1281 const BIGNUM *kinv, const BIGNUM *r,
1283 int (*sign_setup)(EC_KEY *eckey, BN_CTX *ctx_in,
1284 BIGNUM **kinvp, BIGNUM **rp),
1285 ECDSA_SIG *(*sign_sig)(const unsigned char *dgst,
1287 const BIGNUM *in_kinv,
1291 void EC_KEY_METHOD_set_verify(EC_KEY_METHOD *meth,
1292 int (*verify)(int type, const unsigned
1293 char *dgst, int dgst_len,
1294 const unsigned char *sigbuf,
1295 int sig_len, EC_KEY *eckey),
1296 int (*verify_sig)(const unsigned char *dgst,
1298 const ECDSA_SIG *sig,
1301 void EC_KEY_METHOD_get_init(const EC_KEY_METHOD *meth,
1302 int (**pinit)(EC_KEY *key),
1303 void (**pfinish)(EC_KEY *key),
1304 int (**pcopy)(EC_KEY *dest, const EC_KEY *src),
1305 int (**pset_group)(EC_KEY *key,
1306 const EC_GROUP *grp),
1307 int (**pset_private)(EC_KEY *key,
1308 const BIGNUM *priv_key),
1309 int (**pset_public)(EC_KEY *key,
1310 const EC_POINT *pub_key));
1312 void EC_KEY_METHOD_get_keygen(const EC_KEY_METHOD *meth,
1313 int (**pkeygen)(EC_KEY *key));
1315 void EC_KEY_METHOD_get_compute_key(const EC_KEY_METHOD *meth,
1316 int (**pck)(unsigned char **psec,
1318 const EC_POINT *pub_key,
1319 const EC_KEY *ecdh));
1321 void EC_KEY_METHOD_get_sign(const EC_KEY_METHOD *meth,
1322 int (**psign)(int type, const unsigned char *dgst,
1323 int dlen, unsigned char *sig,
1324 unsigned int *siglen,
1325 const BIGNUM *kinv, const BIGNUM *r,
1327 int (**psign_setup)(EC_KEY *eckey, BN_CTX *ctx_in,
1328 BIGNUM **kinvp, BIGNUM **rp),
1329 ECDSA_SIG *(**psign_sig)(const unsigned char *dgst,
1331 const BIGNUM *in_kinv,
1335 void EC_KEY_METHOD_get_verify(const EC_KEY_METHOD *meth,
1336 int (**pverify)(int type, const unsigned
1337 char *dgst, int dgst_len,
1338 const unsigned char *sigbuf,
1339 int sig_len, EC_KEY *eckey),
1340 int (**pverify_sig)(const unsigned char *dgst,
1342 const ECDSA_SIG *sig,
1345 # define ECParameters_dup(x) ASN1_dup_of(EC_KEY,i2d_ECParameters,d2i_ECParameters,x)
1347 # ifndef __cplusplus
1348 # if defined(__SUNPRO_C)
1349 # if __SUNPRO_C >= 0x520
1350 # pragma error_messages (default,E_ARRAY_OF_INCOMPLETE_NONAME,E_ARRAY_OF_INCOMPLETE)
1355 # define EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid) \
1356 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
1357 EVP_PKEY_OP_PARAMGEN|EVP_PKEY_OP_KEYGEN, \
1358 EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID, nid, NULL)
1360 # define EVP_PKEY_CTX_set_ec_param_enc(ctx, flag) \
1361 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
1362 EVP_PKEY_OP_PARAMGEN|EVP_PKEY_OP_KEYGEN, \
1363 EVP_PKEY_CTRL_EC_PARAM_ENC, flag, NULL)
1365 # define EVP_PKEY_CTX_set_ecdh_cofactor_mode(ctx, flag) \
1366 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
1367 EVP_PKEY_OP_DERIVE, \
1368 EVP_PKEY_CTRL_EC_ECDH_COFACTOR, flag, NULL)
1370 # define EVP_PKEY_CTX_get_ecdh_cofactor_mode(ctx) \
1371 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
1372 EVP_PKEY_OP_DERIVE, \
1373 EVP_PKEY_CTRL_EC_ECDH_COFACTOR, -2, NULL)
1375 # define EVP_PKEY_CTX_set_ecdh_kdf_type(ctx, kdf) \
1376 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
1377 EVP_PKEY_OP_DERIVE, \
1378 EVP_PKEY_CTRL_EC_KDF_TYPE, kdf, NULL)
1380 # define EVP_PKEY_CTX_get_ecdh_kdf_type(ctx) \
1381 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
1382 EVP_PKEY_OP_DERIVE, \
1383 EVP_PKEY_CTRL_EC_KDF_TYPE, -2, NULL)
1385 # define EVP_PKEY_CTX_set_ecdh_kdf_md(ctx, md) \
1386 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
1387 EVP_PKEY_OP_DERIVE, \
1388 EVP_PKEY_CTRL_EC_KDF_MD, 0, (void *)(md))
1390 # define EVP_PKEY_CTX_get_ecdh_kdf_md(ctx, pmd) \
1391 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
1392 EVP_PKEY_OP_DERIVE, \
1393 EVP_PKEY_CTRL_GET_EC_KDF_MD, 0, (void *)(pmd))
1395 # define EVP_PKEY_CTX_set_ecdh_kdf_outlen(ctx, len) \
1396 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
1397 EVP_PKEY_OP_DERIVE, \
1398 EVP_PKEY_CTRL_EC_KDF_OUTLEN, len, NULL)
1400 # define EVP_PKEY_CTX_get_ecdh_kdf_outlen(ctx, plen) \
1401 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
1402 EVP_PKEY_OP_DERIVE, \
1403 EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN, 0, \
1406 # define EVP_PKEY_CTX_set0_ecdh_kdf_ukm(ctx, p, plen) \
1407 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
1408 EVP_PKEY_OP_DERIVE, \
1409 EVP_PKEY_CTRL_EC_KDF_UKM, plen, (void *)(p))
1411 # define EVP_PKEY_CTX_get0_ecdh_kdf_ukm(ctx, p) \
1412 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
1413 EVP_PKEY_OP_DERIVE, \
1414 EVP_PKEY_CTRL_GET_EC_KDF_UKM, 0, (void *)(p))
1416 # define EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID (EVP_PKEY_ALG_CTRL + 1)
1417 # define EVP_PKEY_CTRL_EC_PARAM_ENC (EVP_PKEY_ALG_CTRL + 2)
1418 # define EVP_PKEY_CTRL_EC_ECDH_COFACTOR (EVP_PKEY_ALG_CTRL + 3)
1419 # define EVP_PKEY_CTRL_EC_KDF_TYPE (EVP_PKEY_ALG_CTRL + 4)
1420 # define EVP_PKEY_CTRL_EC_KDF_MD (EVP_PKEY_ALG_CTRL + 5)
1421 # define EVP_PKEY_CTRL_GET_EC_KDF_MD (EVP_PKEY_ALG_CTRL + 6)
1422 # define EVP_PKEY_CTRL_EC_KDF_OUTLEN (EVP_PKEY_ALG_CTRL + 7)
1423 # define EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN (EVP_PKEY_ALG_CTRL + 8)
1424 # define EVP_PKEY_CTRL_EC_KDF_UKM (EVP_PKEY_ALG_CTRL + 9)
1425 # define EVP_PKEY_CTRL_GET_EC_KDF_UKM (EVP_PKEY_ALG_CTRL + 10)
1427 # define EVP_PKEY_ECDH_KDF_NONE 1
1428 # define EVP_PKEY_ECDH_KDF_X9_62 2