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