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