ECDSA_SIG: add simple getters for commonly used struct members
[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 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 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 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 structure
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 /** Accessor for r field of ECDSA_SIG
1070  *  \param  sig  pointer to ECDSA_SIG structure
1071  */
1072 const BIGNUM *ECDSA_SIG_get0_r(const ECDSA_SIG *sig);
1073
1074 /** Accessor for s field of ECDSA_SIG
1075  *  \param  sig  pointer to ECDSA_SIG structure
1076  */
1077 const BIGNUM *ECDSA_SIG_get0_s(const ECDSA_SIG *sig);
1078
1079 /** Setter for r and s fields of ECDSA_SIG
1080  *  \param  sig  pointer to ECDSA_SIG structure
1081  */
1082 int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s);
1083
1084 /** Computes the ECDSA signature of the given hash value using
1085  *  the supplied private key and returns the created signature.
1086  *  \param  dgst      pointer to the hash value
1087  *  \param  dgst_len  length of the hash value
1088  *  \param  eckey     EC_KEY object containing a private EC key
1089  *  \return pointer to a ECDSA_SIG structure or NULL if an error occurred
1090  */
1091 ECDSA_SIG *ECDSA_do_sign(const unsigned char *dgst, int dgst_len,
1092                          EC_KEY *eckey);
1093
1094 /** Computes ECDSA signature of a given hash value using the supplied
1095  *  private key (note: sig must point to ECDSA_size(eckey) bytes of memory).
1096  *  \param  dgst     pointer to the hash value to sign
1097  *  \param  dgstlen  length of the hash value
1098  *  \param  kinv     BIGNUM with a pre-computed inverse k (optional)
1099  *  \param  rp       BIGNUM with a pre-computed rp value (optional),
1100  *                   see ECDSA_sign_setup
1101  *  \param  eckey    EC_KEY object containing a private EC key
1102  *  \return pointer to a ECDSA_SIG structure or NULL if an error occurred
1103  */
1104 ECDSA_SIG *ECDSA_do_sign_ex(const unsigned char *dgst, int dgstlen,
1105                             const BIGNUM *kinv, const BIGNUM *rp,
1106                             EC_KEY *eckey);
1107
1108 /** Verifies that the supplied signature is a valid ECDSA
1109  *  signature of the supplied hash value using the supplied public key.
1110  *  \param  dgst      pointer to the hash value
1111  *  \param  dgst_len  length of the hash value
1112  *  \param  sig       ECDSA_SIG structure
1113  *  \param  eckey     EC_KEY object containing a public EC key
1114  *  \return 1 if the signature is valid, 0 if the signature is invalid
1115  *          and -1 on error
1116  */
1117 int ECDSA_do_verify(const unsigned char *dgst, int dgst_len,
1118                     const ECDSA_SIG *sig, EC_KEY *eckey);
1119
1120 /** Precompute parts of the signing operation
1121  *  \param  eckey  EC_KEY object containing a private EC key
1122  *  \param  ctx    BN_CTX object (optional)
1123  *  \param  kinv   BIGNUM pointer for the inverse of k
1124  *  \param  rp     BIGNUM pointer for x coordinate of k * generator
1125  *  \return 1 on success and 0 otherwise
1126  */
1127 int ECDSA_sign_setup(EC_KEY *eckey, BN_CTX *ctx, BIGNUM **kinv, BIGNUM **rp);
1128
1129 /** Computes ECDSA signature of a given hash value using the supplied
1130  *  private key (note: sig must point to ECDSA_size(eckey) bytes of memory).
1131  *  \param  type     this parameter is ignored
1132  *  \param  dgst     pointer to the hash value to sign
1133  *  \param  dgstlen  length of the hash value
1134  *  \param  sig      memory for the DER encoded created signature
1135  *  \param  siglen   pointer to the length of the returned signature
1136  *  \param  eckey    EC_KEY object containing a private EC key
1137  *  \return 1 on success and 0 otherwise
1138  */
1139 int ECDSA_sign(int type, const unsigned char *dgst, int dgstlen,
1140                unsigned char *sig, unsigned int *siglen, EC_KEY *eckey);
1141
1142 /** Computes ECDSA signature of a given hash value using the supplied
1143  *  private key (note: sig must point to ECDSA_size(eckey) bytes of memory).
1144  *  \param  type     this parameter is ignored
1145  *  \param  dgst     pointer to the hash value to sign
1146  *  \param  dgstlen  length of the hash value
1147  *  \param  sig      buffer to hold the DER encoded signature
1148  *  \param  siglen   pointer to the length of the returned signature
1149  *  \param  kinv     BIGNUM with a pre-computed inverse k (optional)
1150  *  \param  rp       BIGNUM with a pre-computed rp value (optional),
1151  *                   see ECDSA_sign_setup
1152  *  \param  eckey    EC_KEY object containing a private EC key
1153  *  \return 1 on success and 0 otherwise
1154  */
1155 int ECDSA_sign_ex(int type, const unsigned char *dgst, int dgstlen,
1156                   unsigned char *sig, unsigned int *siglen,
1157                   const BIGNUM *kinv, const BIGNUM *rp, EC_KEY *eckey);
1158
1159 /** Verifies that the given signature is valid ECDSA signature
1160  *  of the supplied hash value using the specified public key.
1161  *  \param  type     this parameter is ignored
1162  *  \param  dgst     pointer to the hash value
1163  *  \param  dgstlen  length of the hash value
1164  *  \param  sig      pointer to the DER encoded signature
1165  *  \param  siglen   length of the DER encoded signature
1166  *  \param  eckey    EC_KEY object containing a public EC key
1167  *  \return 1 if the signature is valid, 0 if the signature is invalid
1168  *          and -1 on error
1169  */
1170 int ECDSA_verify(int type, const unsigned char *dgst, int dgstlen,
1171                  const unsigned char *sig, int siglen, EC_KEY *eckey);
1172
1173 /** Returns the maximum length of the DER encoded signature
1174  *  \param  eckey  EC_KEY object
1175  *  \return numbers of bytes required for the DER encoded signature
1176  */
1177 int ECDSA_size(const EC_KEY *eckey);
1178
1179 /********************************************************************/
1180 /*  EC_KEY_METHOD constructors, destructors, writers and accessors  */
1181 /********************************************************************/
1182
1183 EC_KEY_METHOD *EC_KEY_METHOD_new(const EC_KEY_METHOD *meth);
1184 void EC_KEY_METHOD_free(EC_KEY_METHOD *meth);
1185 void EC_KEY_METHOD_set_init(EC_KEY_METHOD *meth,
1186                             int (*init)(EC_KEY *key),
1187                             void (*finish)(EC_KEY *key),
1188                             int (*copy)(EC_KEY *dest, const EC_KEY *src),
1189                             int (*set_group)(EC_KEY *key, const EC_GROUP *grp),
1190                             int (*set_private)(EC_KEY *key,
1191                                                const BIGNUM *priv_key),
1192                             int (*set_public)(EC_KEY *key,
1193                                               const EC_POINT *pub_key));
1194
1195 void EC_KEY_METHOD_set_keygen(EC_KEY_METHOD *meth,
1196                               int (*keygen)(EC_KEY *key));
1197
1198 void EC_KEY_METHOD_set_compute_key(EC_KEY_METHOD *meth,
1199                                    int (*ckey)(unsigned char **psec,
1200                                                size_t *pseclen,
1201                                                const EC_POINT *pub_key,
1202                                                const EC_KEY *ecdh));
1203
1204 void EC_KEY_METHOD_set_sign(EC_KEY_METHOD *meth,
1205                             int (*sign)(int type, const unsigned char *dgst,
1206                                         int dlen, unsigned char *sig,
1207                                         unsigned int *siglen,
1208                                         const BIGNUM *kinv, const BIGNUM *r,
1209                                         EC_KEY *eckey),
1210                             int (*sign_setup)(EC_KEY *eckey, BN_CTX *ctx_in,
1211                                               BIGNUM **kinvp, BIGNUM **rp),
1212                             ECDSA_SIG *(*sign_sig)(const unsigned char *dgst,
1213                                                    int dgst_len,
1214                                                    const BIGNUM *in_kinv,
1215                                                    const BIGNUM *in_r,
1216                                                    EC_KEY *eckey));
1217
1218 void EC_KEY_METHOD_set_verify(EC_KEY_METHOD *meth,
1219                               int (*verify)(int type, const unsigned
1220                                             char *dgst, int dgst_len,
1221                                             const unsigned char *sigbuf,
1222                                             int sig_len, EC_KEY *eckey),
1223                               int (*verify_sig)(const unsigned char *dgst,
1224                                                 int dgst_len,
1225                                                 const ECDSA_SIG *sig,
1226                                                 EC_KEY *eckey));
1227
1228 void EC_KEY_METHOD_get_init(const EC_KEY_METHOD *meth,
1229                             int (**pinit)(EC_KEY *key),
1230                             void (**pfinish)(EC_KEY *key),
1231                             int (**pcopy)(EC_KEY *dest, const EC_KEY *src),
1232                             int (**pset_group)(EC_KEY *key,
1233                                                const EC_GROUP *grp),
1234                             int (**pset_private)(EC_KEY *key,
1235                                                  const BIGNUM *priv_key),
1236                             int (**pset_public)(EC_KEY *key,
1237                                                 const EC_POINT *pub_key));
1238
1239 void EC_KEY_METHOD_get_keygen(const EC_KEY_METHOD *meth,
1240                               int (**pkeygen)(EC_KEY *key));
1241
1242 void EC_KEY_METHOD_get_compute_key(const EC_KEY_METHOD *meth,
1243                                    int (**pck)(unsigned char **psec,
1244                                                size_t *pseclen,
1245                                                const EC_POINT *pub_key,
1246                                                const EC_KEY *ecdh));
1247
1248 void EC_KEY_METHOD_get_sign(const EC_KEY_METHOD *meth,
1249                             int (**psign)(int type, const unsigned char *dgst,
1250                                           int dlen, unsigned char *sig,
1251                                           unsigned int *siglen,
1252                                           const BIGNUM *kinv, const BIGNUM *r,
1253                                           EC_KEY *eckey),
1254                             int (**psign_setup)(EC_KEY *eckey, BN_CTX *ctx_in,
1255                                                 BIGNUM **kinvp, BIGNUM **rp),
1256                             ECDSA_SIG *(**psign_sig)(const unsigned char *dgst,
1257                                                      int dgst_len,
1258                                                      const BIGNUM *in_kinv,
1259                                                      const BIGNUM *in_r,
1260                                                      EC_KEY *eckey));
1261
1262 void EC_KEY_METHOD_get_verify(const EC_KEY_METHOD *meth,
1263                               int (**pverify)(int type, const unsigned
1264                                               char *dgst, int dgst_len,
1265                                               const unsigned char *sigbuf,
1266                                               int sig_len, EC_KEY *eckey),
1267                               int (**pverify_sig)(const unsigned char *dgst,
1268                                                   int dgst_len,
1269                                                   const ECDSA_SIG *sig,
1270                                                   EC_KEY *eckey));
1271
1272 # define ECParameters_dup(x) ASN1_dup_of(EC_KEY,i2d_ECParameters,d2i_ECParameters,x)
1273
1274 # ifndef __cplusplus
1275 #  if defined(__SUNPRO_C)
1276 #   if __SUNPRO_C >= 0x520
1277 #    pragma error_messages (default,E_ARRAY_OF_INCOMPLETE_NONAME,E_ARRAY_OF_INCOMPLETE)
1278 #   endif
1279 #  endif
1280 # endif
1281
1282 # define EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid) \
1283         EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
1284                                 EVP_PKEY_OP_PARAMGEN|EVP_PKEY_OP_KEYGEN, \
1285                                 EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID, nid, NULL)
1286
1287 # define EVP_PKEY_CTX_set_ec_param_enc(ctx, flag) \
1288         EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
1289                                 EVP_PKEY_OP_PARAMGEN|EVP_PKEY_OP_KEYGEN, \
1290                                 EVP_PKEY_CTRL_EC_PARAM_ENC, flag, NULL)
1291
1292 # define EVP_PKEY_CTX_set_ecdh_cofactor_mode(ctx, flag) \
1293         EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
1294                                 EVP_PKEY_OP_DERIVE, \
1295                                 EVP_PKEY_CTRL_EC_ECDH_COFACTOR, flag, NULL)
1296
1297 # define EVP_PKEY_CTX_get_ecdh_cofactor_mode(ctx) \
1298         EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
1299                                 EVP_PKEY_OP_DERIVE, \
1300                                 EVP_PKEY_CTRL_EC_ECDH_COFACTOR, -2, NULL)
1301
1302 # define EVP_PKEY_CTX_set_ecdh_kdf_type(ctx, kdf) \
1303         EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
1304                                 EVP_PKEY_OP_DERIVE, \
1305                                 EVP_PKEY_CTRL_EC_KDF_TYPE, kdf, NULL)
1306
1307 # define EVP_PKEY_CTX_get_ecdh_kdf_type(ctx) \
1308         EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
1309                                 EVP_PKEY_OP_DERIVE, \
1310                                 EVP_PKEY_CTRL_EC_KDF_TYPE, -2, NULL)
1311
1312 # define EVP_PKEY_CTX_set_ecdh_kdf_md(ctx, md) \
1313         EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
1314                                 EVP_PKEY_OP_DERIVE, \
1315                                 EVP_PKEY_CTRL_EC_KDF_MD, 0, (void *)(md))
1316
1317 # define EVP_PKEY_CTX_get_ecdh_kdf_md(ctx, pmd) \
1318         EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
1319                                 EVP_PKEY_OP_DERIVE, \
1320                                 EVP_PKEY_CTRL_GET_EC_KDF_MD, 0, (void *)(pmd))
1321
1322 # define EVP_PKEY_CTX_set_ecdh_kdf_outlen(ctx, len) \
1323         EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
1324                                 EVP_PKEY_OP_DERIVE, \
1325                                 EVP_PKEY_CTRL_EC_KDF_OUTLEN, len, NULL)
1326
1327 # define EVP_PKEY_CTX_get_ecdh_kdf_outlen(ctx, plen) \
1328         EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
1329                                 EVP_PKEY_OP_DERIVE, \
1330                                 EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN, 0, \
1331                                 (void *)(plen))
1332
1333 # define EVP_PKEY_CTX_set0_ecdh_kdf_ukm(ctx, p, plen) \
1334         EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
1335                                 EVP_PKEY_OP_DERIVE, \
1336                                 EVP_PKEY_CTRL_EC_KDF_UKM, plen, (void *)(p))
1337
1338 # define EVP_PKEY_CTX_get0_ecdh_kdf_ukm(ctx, p) \
1339         EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
1340                                 EVP_PKEY_OP_DERIVE, \
1341                                 EVP_PKEY_CTRL_GET_EC_KDF_UKM, 0, (void *)(p))
1342
1343 # define EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID             (EVP_PKEY_ALG_CTRL + 1)
1344 # define EVP_PKEY_CTRL_EC_PARAM_ENC                      (EVP_PKEY_ALG_CTRL + 2)
1345 # define EVP_PKEY_CTRL_EC_ECDH_COFACTOR                  (EVP_PKEY_ALG_CTRL + 3)
1346 # define EVP_PKEY_CTRL_EC_KDF_TYPE                       (EVP_PKEY_ALG_CTRL + 4)
1347 # define EVP_PKEY_CTRL_EC_KDF_MD                         (EVP_PKEY_ALG_CTRL + 5)
1348 # define EVP_PKEY_CTRL_GET_EC_KDF_MD                     (EVP_PKEY_ALG_CTRL + 6)
1349 # define EVP_PKEY_CTRL_EC_KDF_OUTLEN                     (EVP_PKEY_ALG_CTRL + 7)
1350 # define EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN                 (EVP_PKEY_ALG_CTRL + 8)
1351 # define EVP_PKEY_CTRL_EC_KDF_UKM                        (EVP_PKEY_ALG_CTRL + 9)
1352 # define EVP_PKEY_CTRL_GET_EC_KDF_UKM                    (EVP_PKEY_ALG_CTRL + 10)
1353 /* KDF types */
1354 # define EVP_PKEY_ECDH_KDF_NONE                          1
1355 # define EVP_PKEY_ECDH_KDF_X9_62                         2
1356
1357
1358 #  ifdef  __cplusplus
1359 }
1360 #  endif
1361 # endif
1362 #endif