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