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