blob: 085222c9e1a6f38100fb651cfcb4e5291a984604 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Originally written by Bodo Moeller for the OpenSSL project.
2 * ====================================================================
3 * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21 *
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * openssl-core@openssl.org.
26 *
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
30 *
31 * 6. Redistributions of any form whatsoever must retain the following
32 * acknowledgment:
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
49 *
50 * This product includes cryptographic software written by Eric Young
51 * (eay@cryptsoft.com). This product includes software written by Tim
52 * Hudson (tjh@cryptsoft.com).
53 *
54 */
55/* ====================================================================
56 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
57 *
58 * Portions of the attached software ("Contribution") are developed by
59 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
60 *
61 * The Contribution is licensed pursuant to the OpenSSL open source
62 * license provided above.
63 *
64 * The elliptic curve binary polynomial software is originally written by
65 * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems
66 * Laboratories. */
67
68#ifndef OPENSSL_HEADER_EC_H
69#define OPENSSL_HEADER_EC_H
70
71#include <openssl/base.h>
72
73#if defined(__cplusplus)
74extern "C" {
75#endif
76
77
David Benjamin4512b792017-08-18 19:21:50 -040078// Low-level operations on elliptic curves.
David Benjamin5b082e82014-12-26 00:54:52 -050079
80
David Benjamin4512b792017-08-18 19:21:50 -040081// point_conversion_form_t enumerates forms, as defined in X9.62 (ECDSA), for
82// the encoding of a elliptic curve point (x,y)
Adam Langley95c29f32014-06-20 12:00:00 -070083typedef enum {
David Benjamin4512b792017-08-18 19:21:50 -040084 // POINT_CONVERSION_COMPRESSED indicates that the point is encoded as z||x,
85 // where the octet z specifies which solution of the quadratic equation y
86 // is.
Adam Langleyce9d85e2016-01-24 15:58:39 -080087 POINT_CONVERSION_COMPRESSED = 2,
88
David Benjamin4512b792017-08-18 19:21:50 -040089 // POINT_CONVERSION_UNCOMPRESSED indicates that the point is encoded as
90 // z||x||y, where z is the octet 0x04.
Adam Langleyce9d85e2016-01-24 15:58:39 -080091 POINT_CONVERSION_UNCOMPRESSED = 4,
92
David Benjamin4512b792017-08-18 19:21:50 -040093 // POINT_CONVERSION_HYBRID indicates that the point is encoded as z||x||y,
94 // where z specifies which solution of the quadratic equation y is. This is
95 // not supported by the code and has never been observed in use.
96 //
97 // TODO(agl): remove once node.js no longer references this.
Adam Langleyce9d85e2016-01-24 15:58:39 -080098 POINT_CONVERSION_HYBRID = 6,
Adam Langley95c29f32014-06-20 12:00:00 -070099} point_conversion_form_t;
100
101
David Benjamin4512b792017-08-18 19:21:50 -0400102// Elliptic curve groups.
David Benjaminc3947132023-09-27 15:24:58 -0400103//
104// Elliptic curve groups are represented by |EC_GROUP| objects. Unlike OpenSSL,
105// if limited to the APIs in this section, callers may treat |EC_GROUP|s as
106// static, immutable objects which do not need to be copied or released. In
107// BoringSSL, only custom |EC_GROUP|s created by |EC_GROUP_new_curve_GFp|
108// (deprecated) are dynamic.
109//
110// Callers may cast away |const| and use |EC_GROUP_dup| and |EC_GROUP_free| with
111// static groups, for compatibility with OpenSSL or dynamic groups, but it is
112// otherwise unnecessary.
Adam Langley95c29f32014-06-20 12:00:00 -0700113
David Benjamin417069f2023-02-13 18:33:02 -0500114// EC_group_p224 returns an |EC_GROUP| for P-224, also known as secp224r1.
115OPENSSL_EXPORT const EC_GROUP *EC_group_p224(void);
116
117// EC_group_p256 returns an |EC_GROUP| for P-256, also known as secp256r1 or
118// prime256v1.
119OPENSSL_EXPORT const EC_GROUP *EC_group_p256(void);
120
121// EC_group_p384 returns an |EC_GROUP| for P-384, also known as secp384r1.
122OPENSSL_EXPORT const EC_GROUP *EC_group_p384(void);
123
124// EC_group_p521 returns an |EC_GROUP| for P-521, also known as secp521r1.
125OPENSSL_EXPORT const EC_GROUP *EC_group_p521(void);
126
127// EC_GROUP_new_by_curve_name returns the |EC_GROUP| object for the elliptic
128// curve specified by |nid|, or NULL on unsupported NID. For OpenSSL
129// compatibility, this function returns a non-const pointer which may be passed
130// to |EC_GROUP_free|. However, the resulting object is actually static and
131// calling |EC_GROUP_free| is optional.
David Benjamin4512b792017-08-18 19:21:50 -0400132//
133// The supported NIDs are:
David Benjamina942d572023-12-14 14:27:27 -0500134// - |NID_secp224r1| (P-224)
135// - |NID_X9_62_prime256v1| (P-256)
136// - |NID_secp384r1| (P-384)
137// - |NID_secp521r1| (P-521)
David Benjamind61334d2018-03-20 15:56:17 -0400138//
David Benjamin417069f2023-02-13 18:33:02 -0500139// Calling this function causes all four curves to be linked into the binary.
140// Prefer calling |EC_group_*| to allow the static linker to drop unused curves.
141//
David Benjamind61334d2018-03-20 15:56:17 -0400142// If in doubt, use |NID_X9_62_prime256v1|, or see the curve25519.h header for
143// more modern primitives.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700144OPENSSL_EXPORT EC_GROUP *EC_GROUP_new_by_curve_name(int nid);
Adam Langley95c29f32014-06-20 12:00:00 -0700145
David Benjamin4512b792017-08-18 19:21:50 -0400146// EC_GROUP_cmp returns zero if |a| and |b| are the same group and non-zero
147// otherwise.
Adam Langley93531bd2015-02-13 10:47:56 -0800148OPENSSL_EXPORT int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b,
149 BN_CTX *ignored);
Adam Langley95c29f32014-06-20 12:00:00 -0700150
David Benjamin4512b792017-08-18 19:21:50 -0400151// EC_GROUP_get0_generator returns a pointer to the internal |EC_POINT| object
152// in |group| that specifies the generator for the group.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700153OPENSSL_EXPORT const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group);
Adam Langley95c29f32014-06-20 12:00:00 -0700154
David Benjamin4512b792017-08-18 19:21:50 -0400155// EC_GROUP_get0_order returns a pointer to the internal |BIGNUM| object in
156// |group| that specifies the order of the group.
Brian Smitha3d9de02015-11-18 17:07:14 -1000157OPENSSL_EXPORT const BIGNUM *EC_GROUP_get0_order(const EC_GROUP *group);
Adam Langley95c29f32014-06-20 12:00:00 -0700158
Jeremy Apthorp79c7ec02018-12-19 14:42:26 -0800159// EC_GROUP_order_bits returns the number of bits of the order of |group|.
160OPENSSL_EXPORT int EC_GROUP_order_bits(const EC_GROUP *group);
161
David Benjamin4512b792017-08-18 19:21:50 -0400162// EC_GROUP_get_cofactor sets |*cofactor| to the cofactor of |group| using
163// |ctx|, if it's not NULL. It returns one on success and zero otherwise.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700164OPENSSL_EXPORT int EC_GROUP_get_cofactor(const EC_GROUP *group,
165 BIGNUM *cofactor, BN_CTX *ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700166
David Benjamin4512b792017-08-18 19:21:50 -0400167// EC_GROUP_get_curve_GFp gets various parameters about a group. It sets
168// |*out_p| to the order of the coordinate field and |*out_a| and |*out_b| to
169// the parameters of the curve when expressed as y² = x³ + ax + b. Any of the
170// output parameters can be NULL. It returns one on success and zero on
171// error.
Adam Langley0eb1aae2014-08-22 12:24:16 -0700172OPENSSL_EXPORT int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *out_p,
173 BIGNUM *out_a, BIGNUM *out_b,
174 BN_CTX *ctx);
175
David Benjamin4512b792017-08-18 19:21:50 -0400176// EC_GROUP_get_curve_name returns a NID that identifies |group|.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700177OPENSSL_EXPORT int EC_GROUP_get_curve_name(const EC_GROUP *group);
Adam Langley95c29f32014-06-20 12:00:00 -0700178
David Benjamin4512b792017-08-18 19:21:50 -0400179// EC_GROUP_get_degree returns the number of bits needed to represent an
180// element of the field underlying |group|.
Brian Smith274341d2015-10-08 17:10:15 -1000181OPENSSL_EXPORT unsigned EC_GROUP_get_degree(const EC_GROUP *group);
Adam Langley95c29f32014-06-20 12:00:00 -0700182
David Benjamin0318b052018-05-07 20:38:20 -0400183// EC_curve_nid2nist returns the NIST name of the elliptic curve specified by
184// |nid|, or NULL if |nid| is not a NIST curve. For example, it returns "P-256"
185// for |NID_X9_62_prime256v1|.
186OPENSSL_EXPORT const char *EC_curve_nid2nist(int nid);
187
David Benjamin23dcf882019-01-25 04:44:22 +0000188// EC_curve_nist2nid returns the NID of the elliptic curve specified by the NIST
189// name |name|, or |NID_undef| if |name| is not a recognized name. For example,
190// it returns |NID_X9_62_prime256v1| for "P-256".
191OPENSSL_EXPORT int EC_curve_nist2nid(const char *name);
192
Adam Langley95c29f32014-06-20 12:00:00 -0700193
David Benjamin4512b792017-08-18 19:21:50 -0400194// Points on elliptic curves.
Adam Langley95c29f32014-06-20 12:00:00 -0700195
David Benjamin4512b792017-08-18 19:21:50 -0400196// EC_POINT_new returns a fresh |EC_POINT| object in the given group, or NULL
197// on error.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700198OPENSSL_EXPORT EC_POINT *EC_POINT_new(const EC_GROUP *group);
Adam Langley95c29f32014-06-20 12:00:00 -0700199
David Benjamin4512b792017-08-18 19:21:50 -0400200// EC_POINT_free frees |point| and the data that it points to.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700201OPENSSL_EXPORT void EC_POINT_free(EC_POINT *point);
Adam Langley95c29f32014-06-20 12:00:00 -0700202
David Benjamin4512b792017-08-18 19:21:50 -0400203// EC_POINT_copy sets |*dest| equal to |*src|. It returns one on success and
204// zero otherwise.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700205OPENSSL_EXPORT int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src);
Adam Langley95c29f32014-06-20 12:00:00 -0700206
David Benjamin4512b792017-08-18 19:21:50 -0400207// EC_POINT_dup returns a fresh |EC_POINT| that contains the same values as
208// |src|, or NULL on error.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700209OPENSSL_EXPORT EC_POINT *EC_POINT_dup(const EC_POINT *src,
210 const EC_GROUP *group);
Adam Langley95c29f32014-06-20 12:00:00 -0700211
David Benjamin4512b792017-08-18 19:21:50 -0400212// EC_POINT_set_to_infinity sets |point| to be the "point at infinity" for the
213// given group.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700214OPENSSL_EXPORT int EC_POINT_set_to_infinity(const EC_GROUP *group,
215 EC_POINT *point);
Adam Langley95c29f32014-06-20 12:00:00 -0700216
David Benjamin4512b792017-08-18 19:21:50 -0400217// EC_POINT_is_at_infinity returns one iff |point| is the point at infinity and
218// zero otherwise.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700219OPENSSL_EXPORT int EC_POINT_is_at_infinity(const EC_GROUP *group,
220 const EC_POINT *point);
Adam Langley95c29f32014-06-20 12:00:00 -0700221
David Benjamin4512b792017-08-18 19:21:50 -0400222// EC_POINT_is_on_curve returns one if |point| is an element of |group| and
223// and zero otherwise or when an error occurs. This is different from OpenSSL,
224// which returns -1 on error. If |ctx| is non-NULL, it may be used.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700225OPENSSL_EXPORT int EC_POINT_is_on_curve(const EC_GROUP *group,
226 const EC_POINT *point, BN_CTX *ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700227
David Benjamin4512b792017-08-18 19:21:50 -0400228// EC_POINT_cmp returns zero if |a| is equal to |b|, greater than zero if
229// not equal and -1 on error. If |ctx| is not NULL, it may be used.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700230OPENSSL_EXPORT int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a,
231 const EC_POINT *b, BN_CTX *ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700232
Adam Langley95c29f32014-06-20 12:00:00 -0700233
David Benjamin4512b792017-08-18 19:21:50 -0400234// Point conversion.
Adam Langley95c29f32014-06-20 12:00:00 -0700235
David Benjamin4512b792017-08-18 19:21:50 -0400236// EC_POINT_get_affine_coordinates_GFp sets |x| and |y| to the affine value of
237// |point| using |ctx|, if it's not NULL. It returns one on success and zero
238// otherwise.
David Benjamin3d2c6b02018-03-05 13:53:54 -0500239//
240// Either |x| or |y| may be NULL to skip computing that coordinate. This is
241// slightly faster in the common case where only the x-coordinate is needed.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700242OPENSSL_EXPORT int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group,
243 const EC_POINT *point,
244 BIGNUM *x, BIGNUM *y,
245 BN_CTX *ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700246
Adam Langley5dd18d02021-01-07 12:45:59 -0800247// EC_POINT_get_affine_coordinates is an alias of
248// |EC_POINT_get_affine_coordinates_GFp|.
249OPENSSL_EXPORT int EC_POINT_get_affine_coordinates(const EC_GROUP *group,
250 const EC_POINT *point,
251 BIGNUM *x, BIGNUM *y,
252 BN_CTX *ctx);
253
David Benjamin4512b792017-08-18 19:21:50 -0400254// EC_POINT_set_affine_coordinates_GFp sets the value of |point| to be
255// (|x|, |y|). The |ctx| argument may be used if not NULL. It returns one
David Benjamin0deb91a2020-01-07 11:33:18 -0500256// on success or zero on error. It's considered an error if the point is not on
257// the curve.
258//
259// Note that the corresponding function in OpenSSL versions prior to 1.0.2s does
260// not check if the point is on the curve. This is a security-critical check, so
261// code additionally supporting OpenSSL should repeat the check with
262// |EC_POINT_is_on_curve| or check for older OpenSSL versions with
263// |OPENSSL_VERSION_NUMBER|.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700264OPENSSL_EXPORT int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group,
265 EC_POINT *point,
266 const BIGNUM *x,
267 const BIGNUM *y,
268 BN_CTX *ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700269
Adam Langley76164b12021-01-06 16:34:01 -0800270// EC_POINT_set_affine_coordinates is an alias of
271// |EC_POINT_set_affine_coordinates_GFp|.
272OPENSSL_EXPORT int EC_POINT_set_affine_coordinates(const EC_GROUP *group,
273 EC_POINT *point,
274 const BIGNUM *x,
275 const BIGNUM *y,
276 BN_CTX *ctx);
277
David Benjamin4512b792017-08-18 19:21:50 -0400278// EC_POINT_point2oct serialises |point| into the X9.62 form given by |form|
David Benjamin38f621a2022-11-04 17:16:49 -0400279// into, at most, |max_out| bytes at |buf|. It returns the number of bytes
280// written or zero on error if |buf| is non-NULL, else the number of bytes
281// needed. The |ctx| argument may be used if not NULL.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700282OPENSSL_EXPORT size_t EC_POINT_point2oct(const EC_GROUP *group,
283 const EC_POINT *point,
284 point_conversion_form_t form,
David Benjamin38f621a2022-11-04 17:16:49 -0400285 uint8_t *buf, size_t max_out,
286 BN_CTX *ctx);
287
288// EC_POINT_point2buf serialises |point| into the X9.62 form given by |form| to
289// a newly-allocated buffer and sets |*out_buf| to point to it. It returns the
290// length of the result on success or zero on error. The caller must release
291// |*out_buf| with |OPENSSL_free| when done.
292OPENSSL_EXPORT size_t EC_POINT_point2buf(const EC_GROUP *group,
293 const EC_POINT *point,
294 point_conversion_form_t form,
295 uint8_t **out_buf, BN_CTX *ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700296
David Benjamin4512b792017-08-18 19:21:50 -0400297// EC_POINT_point2cbb behaves like |EC_POINT_point2oct| but appends the
298// serialised point to |cbb|. It returns one on success and zero on error.
David Benjamin6014ea62015-12-30 21:39:34 -0500299OPENSSL_EXPORT int EC_POINT_point2cbb(CBB *out, const EC_GROUP *group,
300 const EC_POINT *point,
301 point_conversion_form_t form,
302 BN_CTX *ctx);
303
David Benjamin4512b792017-08-18 19:21:50 -0400304// EC_POINT_oct2point sets |point| from |len| bytes of X9.62 format
David Benjamin0deb91a2020-01-07 11:33:18 -0500305// serialisation in |buf|. It returns one on success and zero on error. The
306// |ctx| argument may be used if not NULL. It's considered an error if |buf|
307// does not represent a point on the curve.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700308OPENSSL_EXPORT int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point,
309 const uint8_t *buf, size_t len,
310 BN_CTX *ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700311
David Benjamin4512b792017-08-18 19:21:50 -0400312// EC_POINT_set_compressed_coordinates_GFp sets |point| to equal the point with
313// the given |x| coordinate and the y coordinate specified by |y_bit| (see
314// X9.62). It returns one on success and zero otherwise.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700315OPENSSL_EXPORT int EC_POINT_set_compressed_coordinates_GFp(
316 const EC_GROUP *group, EC_POINT *point, const BIGNUM *x, int y_bit,
317 BN_CTX *ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700318
319
David Benjamin4512b792017-08-18 19:21:50 -0400320// Group operations.
Adam Langley95c29f32014-06-20 12:00:00 -0700321
David Benjamin4512b792017-08-18 19:21:50 -0400322// EC_POINT_add sets |r| equal to |a| plus |b|. It returns one on success and
323// zero otherwise. If |ctx| is not NULL, it may be used.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700324OPENSSL_EXPORT int EC_POINT_add(const EC_GROUP *group, EC_POINT *r,
325 const EC_POINT *a, const EC_POINT *b,
326 BN_CTX *ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700327
David Benjamin4512b792017-08-18 19:21:50 -0400328// EC_POINT_dbl sets |r| equal to |a| plus |a|. It returns one on success and
329// zero otherwise. If |ctx| is not NULL, it may be used.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700330OPENSSL_EXPORT int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r,
331 const EC_POINT *a, BN_CTX *ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700332
David Benjamin4512b792017-08-18 19:21:50 -0400333// EC_POINT_invert sets |a| equal to minus |a|. It returns one on success and
334// zero otherwise. If |ctx| is not NULL, it may be used.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700335OPENSSL_EXPORT int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a,
336 BN_CTX *ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700337
David Benjamin4512b792017-08-18 19:21:50 -0400338// EC_POINT_mul sets r = generator*n + q*m. It returns one on success and zero
339// otherwise. If |ctx| is not NULL, it may be used.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700340OPENSSL_EXPORT int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r,
341 const BIGNUM *n, const EC_POINT *q,
342 const BIGNUM *m, BN_CTX *ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700343
Adam Langley95c29f32014-06-20 12:00:00 -0700344
David Benjamin3950d6c2023-02-09 19:24:06 -0500345// Hash-to-curve.
346//
David Benjamin39a75072023-08-14 20:48:50 -0400347// The following functions implement primitives from RFC 9380. The |dst|
348// parameter in each function is the domain separation tag and must be unique
349// for each protocol and between the |hash_to_curve| and |hash_to_scalar|
350// variants. See section 3.1 of the spec for additional guidance on this
351// parameter.
David Benjamin3950d6c2023-02-09 19:24:06 -0500352
353// EC_hash_to_curve_p256_xmd_sha256_sswu hashes |msg| to a point on |group| and
354// writes the result to |out|, implementing the P256_XMD:SHA-256_SSWU_RO_ suite
David Benjamin39a75072023-08-14 20:48:50 -0400355// from RFC 9380. It returns one on success and zero on error.
David Benjamin3950d6c2023-02-09 19:24:06 -0500356OPENSSL_EXPORT int EC_hash_to_curve_p256_xmd_sha256_sswu(
357 const EC_GROUP *group, EC_POINT *out, const uint8_t *dst, size_t dst_len,
358 const uint8_t *msg, size_t msg_len);
359
360// EC_hash_to_curve_p384_xmd_sha384_sswu hashes |msg| to a point on |group| and
361// writes the result to |out|, implementing the P384_XMD:SHA-384_SSWU_RO_ suite
David Benjamin39a75072023-08-14 20:48:50 -0400362// from RFC 9380. It returns one on success and zero on error.
David Benjamin3950d6c2023-02-09 19:24:06 -0500363OPENSSL_EXPORT int EC_hash_to_curve_p384_xmd_sha384_sswu(
364 const EC_GROUP *group, EC_POINT *out, const uint8_t *dst, size_t dst_len,
365 const uint8_t *msg, size_t msg_len);
366
367
David Benjamin4512b792017-08-18 19:21:50 -0400368// Deprecated functions.
Adam Langleyc3ef76f2015-04-13 14:34:17 -0700369
David Benjaminc3947132023-09-27 15:24:58 -0400370// EC_GROUP_free releases a reference to |group|, if |group| was created by
371// |EC_GROUP_new_curve_GFp|. If |group| is static, it does nothing.
372//
373// This function exists for OpenSSL compatibilty, and to manage dynamic
374// |EC_GROUP|s constructed by |EC_GROUP_new_curve_GFp|. Callers that do not need
375// either may ignore this function.
376OPENSSL_EXPORT void EC_GROUP_free(EC_GROUP *group);
377
378// EC_GROUP_dup increments |group|'s reference count and returns it, if |group|
379// was created by |EC_GROUP_new_curve_GFp|. If |group| is static, it simply
380// returns |group|.
381//
382// This function exists for OpenSSL compatibilty, and to manage dynamic
383// |EC_GROUP|s constructed by |EC_GROUP_new_curve_GFp|. Callers that do not need
384// either may ignore this function.
385OPENSSL_EXPORT EC_GROUP *EC_GROUP_dup(const EC_GROUP *group);
386
David Benjamin4512b792017-08-18 19:21:50 -0400387// EC_GROUP_new_curve_GFp creates a new, arbitrary elliptic curve group based
388// on the equation y² = x³ + a·x + b. It returns the new group or NULL on
David Benjaminc3947132023-09-27 15:24:58 -0400389// error. The lifetime of the resulting object must be managed with
390// |EC_GROUP_dup| and |EC_GROUP_free|.
David Benjamin4512b792017-08-18 19:21:50 -0400391//
392// This new group has no generator. It is an error to use a generator-less group
393// with any functions except for |EC_GROUP_free|, |EC_POINT_new|,
394// |EC_POINT_set_affine_coordinates_GFp|, and |EC_GROUP_set_generator|.
395//
396// |EC_GROUP|s returned by this function will always compare as unequal via
397// |EC_GROUP_cmp| (even to themselves). |EC_GROUP_get_curve_name| will always
398// return |NID_undef|.
399//
Adam Langleyc7a3c462022-03-15 09:52:36 -0700400// This function is provided for compatibility with some legacy applications
401// only. Avoid using arbitrary curves and use |EC_GROUP_new_by_curve_name|
402// instead. This ensures the result meets preconditions necessary for
403// elliptic curve algorithms to function correctly and securely.
404//
405// Given invalid parameters, this function may fail or it may return an
406// |EC_GROUP| which breaks these preconditions. Subsequent operations may then
407// return arbitrary, incorrect values. Callers should not pass
408// attacker-controlled values to this function.
David Benjamin6f7374b2016-03-11 16:08:39 -0500409OPENSSL_EXPORT EC_GROUP *EC_GROUP_new_curve_GFp(const BIGNUM *p,
410 const BIGNUM *a,
411 const BIGNUM *b, BN_CTX *ctx);
412
David Benjamin4512b792017-08-18 19:21:50 -0400413// EC_GROUP_set_generator sets the generator for |group| to |generator|, which
414// must have the given order and cofactor. It may only be used with |EC_GROUP|
415// objects returned by |EC_GROUP_new_curve_GFp| and may only be used once on
David Benjamincb16f172017-10-26 15:48:18 -0400416// each group. |generator| must have been created using |group|.
David Benjamin6f7374b2016-03-11 16:08:39 -0500417OPENSSL_EXPORT int EC_GROUP_set_generator(EC_GROUP *group,
418 const EC_POINT *generator,
419 const BIGNUM *order,
420 const BIGNUM *cofactor);
421
David Benjamin4512b792017-08-18 19:21:50 -0400422// EC_GROUP_get_order sets |*order| to the order of |group|, if it's not
423// NULL. It returns one on success and zero otherwise. |ctx| is ignored. Use
424// |EC_GROUP_get0_order| instead.
Brian Smitha3d9de02015-11-18 17:07:14 -1000425OPENSSL_EXPORT int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order,
426 BN_CTX *ctx);
427
David Benjamine7bb89b2021-02-26 18:34:48 -0500428#define OPENSSL_EC_EXPLICIT_CURVE 0
429#define OPENSSL_EC_NAMED_CURVE 1
430
David Benjamin4512b792017-08-18 19:21:50 -0400431// EC_GROUP_set_asn1_flag does nothing.
Adam Langleyc3ef76f2015-04-13 14:34:17 -0700432OPENSSL_EXPORT void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag);
433
David Benjamine7bb89b2021-02-26 18:34:48 -0500434// EC_GROUP_get_asn1_flag returns |OPENSSL_EC_NAMED_CURVE|.
435OPENSSL_EXPORT int EC_GROUP_get_asn1_flag(const EC_GROUP *group);
Adam Langleyc3ef76f2015-04-13 14:34:17 -0700436
437typedef struct ec_method_st EC_METHOD;
438
David Benjamin2f5100e2018-05-11 18:20:56 -0400439// EC_GROUP_method_of returns a dummy non-NULL pointer.
Adam Langleyc3ef76f2015-04-13 14:34:17 -0700440OPENSSL_EXPORT const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group);
441
David Benjamin4512b792017-08-18 19:21:50 -0400442// EC_METHOD_get_field_type returns NID_X9_62_prime_field.
Adam Langleyc3ef76f2015-04-13 14:34:17 -0700443OPENSSL_EXPORT int EC_METHOD_get_field_type(const EC_METHOD *meth);
444
David Benjamin4512b792017-08-18 19:21:50 -0400445// EC_GROUP_set_point_conversion_form aborts the process if |form| is not
446// |POINT_CONVERSION_UNCOMPRESSED| and otherwise does nothing.
Adam Langley126320c2015-05-04 17:43:00 -0700447OPENSSL_EXPORT void EC_GROUP_set_point_conversion_form(
448 EC_GROUP *group, point_conversion_form_t form);
449
David Benjamin4512b792017-08-18 19:21:50 -0400450// EC_builtin_curve describes a supported elliptic curve.
Adam Langleyce9d85e2016-01-24 15:58:39 -0800451typedef struct {
452 int nid;
453 const char *comment;
454} EC_builtin_curve;
455
David Benjamin4512b792017-08-18 19:21:50 -0400456// EC_get_builtin_curves writes at most |max_num_curves| elements to
457// |out_curves| and returns the total number that it would have written, had
458// |max_num_curves| been large enough.
459//
460// The |EC_builtin_curve| items describe the supported elliptic curves.
Adam Langleyce9d85e2016-01-24 15:58:39 -0800461OPENSSL_EXPORT size_t EC_get_builtin_curves(EC_builtin_curve *out_curves,
462 size_t max_num_curves);
Adam Langleyc3ef76f2015-04-13 14:34:17 -0700463
David Benjamind24fd472017-10-26 21:13:16 -0400464// EC_POINT_clear_free calls |EC_POINT_free|.
465OPENSSL_EXPORT void EC_POINT_clear_free(EC_POINT *point);
466
Adam Langley95c29f32014-06-20 12:00:00 -0700467
468#if defined(__cplusplus)
David Benjamin4512b792017-08-18 19:21:50 -0400469} // extern C
David Benjaminf09df692018-11-09 13:21:02 -0600470#endif
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700471
David Benjaminf09df692018-11-09 13:21:02 -0600472// Old code expects to get EC_KEY from ec.h.
473#include <openssl/ec_key.h>
474
475#if defined(__cplusplus)
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700476extern "C++" {
477
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -0700478BSSL_NAMESPACE_BEGIN
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700479
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700480BORINGSSL_MAKE_DELETER(EC_POINT, EC_POINT_free)
481BORINGSSL_MAKE_DELETER(EC_GROUP, EC_GROUP_free)
482
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -0700483BSSL_NAMESPACE_END
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700484
David Benjamin4512b792017-08-18 19:21:50 -0400485} // extern C++
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700486
Adam Langley8c3c3132016-07-11 15:22:19 -0700487#endif
488
David Benjamin689be0f2015-02-11 15:55:26 -0500489#define EC_R_BUFFER_TOO_SMALL 100
490#define EC_R_COORDINATES_OUT_OF_RANGE 101
491#define EC_R_D2I_ECPKPARAMETERS_FAILURE 102
492#define EC_R_EC_GROUP_NEW_BY_NAME_FAILURE 103
493#define EC_R_GROUP2PKPARAMETERS_FAILURE 104
494#define EC_R_I2D_ECPKPARAMETERS_FAILURE 105
495#define EC_R_INCOMPATIBLE_OBJECTS 106
496#define EC_R_INVALID_COMPRESSED_POINT 107
497#define EC_R_INVALID_COMPRESSION_BIT 108
498#define EC_R_INVALID_ENCODING 109
499#define EC_R_INVALID_FIELD 110
500#define EC_R_INVALID_FORM 111
501#define EC_R_INVALID_GROUP_ORDER 112
502#define EC_R_INVALID_PRIVATE_KEY 113
503#define EC_R_MISSING_PARAMETERS 114
504#define EC_R_MISSING_PRIVATE_KEY 115
505#define EC_R_NON_NAMED_CURVE 116
506#define EC_R_NOT_INITIALIZED 117
507#define EC_R_PKPARAMETERS2GROUP_FAILURE 118
508#define EC_R_POINT_AT_INFINITY 119
509#define EC_R_POINT_IS_NOT_ON_CURVE 120
510#define EC_R_SLOT_FULL 121
511#define EC_R_UNDEFINED_GENERATOR 122
512#define EC_R_UNKNOWN_GROUP 123
513#define EC_R_UNKNOWN_ORDER 124
514#define EC_R_WRONG_ORDER 125
Adam Langleyad6b28e2015-04-14 12:07:44 -0700515#define EC_R_BIGNUM_OUT_OF_RANGE 126
516#define EC_R_WRONG_CURVE_PARAMETERS 127
David Benjamin2f6410b2016-01-03 00:57:37 -0800517#define EC_R_DECODE_ERROR 128
518#define EC_R_ENCODE_ERROR 129
519#define EC_R_GROUP_MISMATCH 130
David Benjamin8cf79af2016-06-16 14:58:36 -0400520#define EC_R_INVALID_COFACTOR 131
Steven Valdezb15143f2017-04-13 13:14:12 -0400521#define EC_R_PUBLIC_KEY_VALIDATION_FAILED 132
David Benjamina838f9d2017-11-13 11:58:00 +0800522#define EC_R_INVALID_SCALAR 133
Adam Langley95c29f32014-06-20 12:00:00 -0700523
David Benjamin4512b792017-08-18 19:21:50 -0400524#endif // OPENSSL_HEADER_EC_H