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