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