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