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