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