blob: df59891de255a0dc62c9e5f306e0db0d5827a093 [file] [log] [blame]
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001/**
2 * \file ecp.h
3 *
Rose Zadikb2e111a2018-04-20 10:13:48 +01004 * \brief This file provides an API for Elliptic Curves over GF(P) (ECP).
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01005 *
Rose Zadikb2e111a2018-04-20 10:13:48 +01006 * The use of ECP in cryptography and TLS is defined in
7 * <em>Standards for Efficient Cryptography Group (SECG): SEC1
8 * Elliptic Curve Cryptography</em> and
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01009 * <em>RFC-4492: Elliptic Curve Cryptography (ECC) Cipher Suites
10 * for Transport Layer Security (TLS)</em>.
Rose Zadika7a61552018-04-24 13:14:01 +010011 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +010012 * <em>RFC-2409: The Internet Key Exchange (IKE)</em> defines ECP
13 * group types.
Rose Zadika7a61552018-04-24 13:14:01 +010014 *
Darryl Greena40a1012018-01-05 15:33:17 +000015 */
Rose Zadikd3c9bfc2018-04-17 10:56:55 +010016
Darryl Greena40a1012018-01-05 15:33:17 +000017/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020018 * Copyright The Mbed TLS Contributors
Dave Rodgman7ff79652023-11-03 12:04:52 +000019 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010020 */
Rose Zadika7a61552018-04-24 13:14:01 +010021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#ifndef MBEDTLS_ECP_H
23#define MBEDTLS_ECP_H
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010024
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050025#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010026#include "mbedtls/config.h"
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050027#else
28#include MBEDTLS_CONFIG_FILE
29#endif
30
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010031#include "mbedtls/bignum.h"
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010032
Chien Wong8aa8d782023-08-10 12:24:43 +080033#if (defined(__ARMCC_VERSION) || defined(_MSC_VER)) && \
34 !defined(inline) && !defined(__cplusplus)
35#define inline __inline
36#endif
37
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010038/*
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +010039 * ECP error codes
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010040 */
Gilles Peskinea3974432021-07-26 18:48:10 +020041/** Bad input parameters to function. */
42#define MBEDTLS_ERR_ECP_BAD_INPUT_DATA -0x4F80
43/** The buffer is too small to write to. */
44#define MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL -0x4F00
45/** The requested feature is not available, for example, the requested curve is not supported. */
46#define MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE -0x4E80
47/** The signature is not valid. */
48#define MBEDTLS_ERR_ECP_VERIFY_FAILED -0x4E00
49/** Memory allocation failed. */
50#define MBEDTLS_ERR_ECP_ALLOC_FAILED -0x4D80
51/** Generation of random value, such as ephemeral key, failed. */
52#define MBEDTLS_ERR_ECP_RANDOM_FAILED -0x4D00
53/** Invalid private or public key. */
54#define MBEDTLS_ERR_ECP_INVALID_KEY -0x4C80
55/** The buffer contains a valid signature followed by more data. */
56#define MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH -0x4C00
Ron Eldor9924bdc2018-10-04 10:59:13 +030057
58/* MBEDTLS_ERR_ECP_HW_ACCEL_FAILED is deprecated and should not be used. */
Gilles Peskinea3974432021-07-26 18:48:10 +020059/** The ECP hardware accelerator failed. */
60#define MBEDTLS_ERR_ECP_HW_ACCEL_FAILED -0x4B80
Ron Eldor9924bdc2018-10-04 10:59:13 +030061
Gilles Peskinea3974432021-07-26 18:48:10 +020062/** Operation in progress, call again with the same parameters to continue. */
63#define MBEDTLS_ERR_ECP_IN_PROGRESS -0x4B00
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +010064
Gilles Peskine9b99a892018-09-14 18:32:19 +020065/* Flags indicating whether to include code that is specific to certain
66 * types of curves. These flags are for internal library use only. */
67#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || \
68 defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || \
69 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || \
70 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || \
71 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || \
Gilles Peskine9b99a892018-09-14 18:32:19 +020072 defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || \
73 defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || \
Gilles Peskinea3de08d2020-07-22 01:23:37 +020074 defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || \
75 defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || \
76 defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || \
77 defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
Gilles Peskine9b99a892018-09-14 18:32:19 +020078#define MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED
79#endif
80#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || \
81 defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
82#define MBEDTLS_ECP_MONTGOMERY_ENABLED
83#endif
84
Paul Bakker407a0da2013-06-27 14:29:21 +020085#ifdef __cplusplus
86extern "C" {
87#endif
88
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010089/**
Rose Zadikd76ac582018-04-23 06:29:34 +010090 * Domain-parameter identifiers: curve, subgroup, and generator.
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +020091 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +010092 * \note Only curves over prime fields are supported.
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +020093 *
94 * \warning This library does not support validation of arbitrary domain
Rose Zadikf56cb342018-04-19 12:49:10 +010095 * parameters. Therefore, only standardized domain parameters from trusted
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +020096 * sources should be used. See mbedtls_ecp_group_load().
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +020097 */
Gilles Peskine963a2072018-09-14 18:31:30 +020098/* Note: when adding a new curve:
99 * - Add it at the end of this enum, otherwise you'll break the ABI by
100 * changing the numerical value for existing curves.
Gilles Peskine6d9c8d72020-07-22 01:26:25 +0200101 * - Increment MBEDTLS_ECP_DP_MAX below if needed.
Gilles Peskine32239402021-06-07 20:24:51 +0200102 * - Update the calculation of MBEDTLS_ECP_MAX_BITS_MIN below.
Gilles Peskine963a2072018-09-14 18:31:30 +0200103 * - Add the corresponding MBEDTLS_ECP_DP_xxx_ENABLED macro definition to
104 * config.h.
105 * - List the curve as a dependency of MBEDTLS_ECP_C and
106 * MBEDTLS_ECDSA_C if supported in check_config.h.
107 * - Add the curve to the appropriate curve type macro
108 * MBEDTLS_ECP_yyy_ENABLED above.
109 * - Add the necessary definitions to ecp_curves.c.
110 * - Add the curve to the ecp_supported_curves array in ecp.c.
111 * - Add the curve to applicable profiles in x509_crt.c if applicable.
112 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100113typedef enum {
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100114 MBEDTLS_ECP_DP_NONE = 0, /*!< Curve not defined. */
Rose Zadikb2e111a2018-04-20 10:13:48 +0100115 MBEDTLS_ECP_DP_SECP192R1, /*!< Domain parameters for the 192-bit curve defined by FIPS 186-4 and SEC1. */
116 MBEDTLS_ECP_DP_SECP224R1, /*!< Domain parameters for the 224-bit curve defined by FIPS 186-4 and SEC1. */
117 MBEDTLS_ECP_DP_SECP256R1, /*!< Domain parameters for the 256-bit curve defined by FIPS 186-4 and SEC1. */
118 MBEDTLS_ECP_DP_SECP384R1, /*!< Domain parameters for the 384-bit curve defined by FIPS 186-4 and SEC1. */
119 MBEDTLS_ECP_DP_SECP521R1, /*!< Domain parameters for the 521-bit curve defined by FIPS 186-4 and SEC1. */
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100120 MBEDTLS_ECP_DP_BP256R1, /*!< Domain parameters for 256-bit Brainpool curve. */
121 MBEDTLS_ECP_DP_BP384R1, /*!< Domain parameters for 384-bit Brainpool curve. */
122 MBEDTLS_ECP_DP_BP512R1, /*!< Domain parameters for 512-bit Brainpool curve. */
Rose Zadikb2e111a2018-04-20 10:13:48 +0100123 MBEDTLS_ECP_DP_CURVE25519, /*!< Domain parameters for Curve25519. */
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100124 MBEDTLS_ECP_DP_SECP192K1, /*!< Domain parameters for 192-bit "Koblitz" curve. */
125 MBEDTLS_ECP_DP_SECP224K1, /*!< Domain parameters for 224-bit "Koblitz" curve. */
126 MBEDTLS_ECP_DP_SECP256K1, /*!< Domain parameters for 256-bit "Koblitz" curve. */
Jaeden Amerofe0669f2018-04-27 17:43:32 +0100127 MBEDTLS_ECP_DP_CURVE448, /*!< Domain parameters for Curve448. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128} mbedtls_ecp_group_id;
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200129
130/**
Rose Zadikf56cb342018-04-19 12:49:10 +0100131 * The number of supported curves, plus one for #MBEDTLS_ECP_DP_NONE.
Manuel Pégourié-Gonnard66153662013-12-03 14:12:26 +0100132 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100133 * \note Montgomery curves are currently excluded.
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200134 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135#define MBEDTLS_ECP_DP_MAX 12
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200136
Janos Follathdf9295b2019-02-26 12:36:52 +0000137/*
138 * Curve types
139 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100140typedef enum {
Janos Follathdf9295b2019-02-26 12:36:52 +0000141 MBEDTLS_ECP_TYPE_NONE = 0,
142 MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS, /* y^2 = x^3 + a x + b */
143 MBEDTLS_ECP_TYPE_MONTGOMERY, /* y^2 = x^3 + a x^2 + x */
144} mbedtls_ecp_curve_type;
145
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200146/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100147 * Curve information, for use by other modules.
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200148 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100149typedef struct mbedtls_ecp_curve_info {
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100150 mbedtls_ecp_group_id grp_id; /*!< An internal identifier. */
151 uint16_t tls_id; /*!< The TLS NamedCurve identifier. */
Rose Zadikb2e111a2018-04-20 10:13:48 +0100152 uint16_t bit_size; /*!< The curve size in bits. */
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100153 const char *name; /*!< A human-friendly name. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154} mbedtls_ecp_curve_info;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200155
156/**
Rose Zadikf56cb342018-04-19 12:49:10 +0100157 * \brief The ECP point structure, in Jacobian coordinates.
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100158 *
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100159 * \note All functions expect and return points satisfying
Rose Zadikf56cb342018-04-19 12:49:10 +0100160 * the following condition: <code>Z == 0</code> or
Rose Zadika7a61552018-04-24 13:14:01 +0100161 * <code>Z == 1</code>. Other values of \p Z are
Rose Zadikf56cb342018-04-19 12:49:10 +0100162 * used only by internal functions.
163 * The point is zero, or "at infinity", if <code>Z == 0</code>.
Rose Zadika7a61552018-04-24 13:14:01 +0100164 * Otherwise, \p X and \p Y are its standard (affine)
Rose Zadikf56cb342018-04-19 12:49:10 +0100165 * coordinates.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100166 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100167typedef struct mbedtls_ecp_point {
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100168 mbedtls_mpi X; /*!< The X coordinate of the ECP point. */
169 mbedtls_mpi Y; /*!< The Y coordinate of the ECP point. */
170 mbedtls_mpi Z; /*!< The Z coordinate of the ECP point. */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100171}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172mbedtls_ecp_point;
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100173
Gilles Peskine6dba3202021-06-02 23:21:07 +0200174/* Determine the minimum safe value of MBEDTLS_ECP_MAX_BITS. */
175#if !defined(MBEDTLS_ECP_C)
176#define MBEDTLS_ECP_MAX_BITS_MIN 0
177/* Note: the curves must be listed in DECREASING size! */
178#elif defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
179#define MBEDTLS_ECP_MAX_BITS_MIN 521
180#elif defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
181#define MBEDTLS_ECP_MAX_BITS_MIN 512
182#elif defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
183#define MBEDTLS_ECP_MAX_BITS_MIN 448
184#elif defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
185#define MBEDTLS_ECP_MAX_BITS_MIN 384
186#elif defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
187#define MBEDTLS_ECP_MAX_BITS_MIN 384
188#elif defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
189#define MBEDTLS_ECP_MAX_BITS_MIN 256
190#elif defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
191#define MBEDTLS_ECP_MAX_BITS_MIN 256
192#elif defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
193#define MBEDTLS_ECP_MAX_BITS_MIN 256
194#elif defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
195#define MBEDTLS_ECP_MAX_BITS_MIN 255
196#elif defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
197#define MBEDTLS_ECP_MAX_BITS_MIN 225 // n is slightly above 2^224
198#elif defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
199#define MBEDTLS_ECP_MAX_BITS_MIN 224
200#elif defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
201#define MBEDTLS_ECP_MAX_BITS_MIN 192
202#elif defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
203#define MBEDTLS_ECP_MAX_BITS_MIN 192
204#else
205#error "MBEDTLS_ECP_C enabled, but no curve?"
206#endif
207
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100208#if !defined(MBEDTLS_ECP_ALT)
209/*
Gilles Peskinef08ca832023-09-12 19:21:54 +0200210 * default Mbed TLS elliptic curve arithmetic implementation
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100211 *
212 * (in case MBEDTLS_ECP_ALT is defined then the developer has to provide an
213 * alternative implementation for the whole module and it will replace this
214 * one.)
215 */
216
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100217/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100218 * \brief The ECP group structure.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100219 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100220 * We consider two types of curve equations:
Rose Zadikb2e111a2018-04-20 10:13:48 +0100221 * <ul><li>Short Weierstrass: <code>y^2 = x^3 + A x + B mod P</code>
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100222 * (SEC1 + RFC-4492)</li>
Rose Zadikf56cb342018-04-19 12:49:10 +0100223 * <li>Montgomery: <code>y^2 = x^3 + A x^2 + x mod P</code> (Curve25519,
Rose Zadikb2e111a2018-04-20 10:13:48 +0100224 * Curve448)</li></ul>
Rose Zadikf56cb342018-04-19 12:49:10 +0100225 * In both cases, the generator (\p G) for a prime-order subgroup is fixed.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100226 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100227 * For Short Weierstrass, this subgroup is the whole curve, and its
Rose Zadikf56cb342018-04-19 12:49:10 +0100228 * cardinality is denoted by \p N. Our code requires that \p N is an
Rose Zadikb2e111a2018-04-20 10:13:48 +0100229 * odd prime as mbedtls_ecp_mul() requires an odd number, and
230 * mbedtls_ecdsa_sign() requires that it is prime for blinding purposes.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100231 *
Chien Wong12f6f282023-08-09 22:15:14 +0800232 * The default implementation only initializes \p A without setting it to the
233 * authentic value for curves with <code>A = -3</code>(SECP256R1, etc), in which
234 * case you need to load \p A by yourself when using domain parameters directly,
235 * for example:
236 * \code
237 * mbedtls_mpi_init(&A);
238 * mbedtls_ecp_group_init(&grp);
239 * CHECK_RETURN(mbedtls_ecp_group_load(&grp, grp_id));
240 * if (mbedtls_ecp_group_a_is_minus_3(&grp)) {
241 * CHECK_RETURN(mbedtls_mpi_sub_int(&A, &grp.P, 3));
242 * } else {
243 * CHECK_RETURN(mbedtls_mpi_copy(&A, &grp.A));
244 * }
245 *
246 * do_something_with_a(&A);
247 *
248 * cleanup:
249 * mbedtls_mpi_free(&A);
250 * mbedtls_ecp_group_free(&grp);
251 * \endcode
252 *
Rose Zadikf56cb342018-04-19 12:49:10 +0100253 * For Montgomery curves, we do not store \p A, but <code>(A + 2) / 4</code>,
Rose Zadika7a61552018-04-24 13:14:01 +0100254 * which is the quantity used in the formulas. Additionally, \p nbits is
Rose Zadikf56cb342018-04-19 12:49:10 +0100255 * not the size of \p N but the required size for private keys.
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100256 *
Rose Zadika7a61552018-04-24 13:14:01 +0100257 * If \p modp is NULL, reduction modulo \p P is done using a generic algorithm.
258 * Otherwise, \p modp must point to a function that takes an \p mbedtls_mpi in the
259 * range of <code>0..2^(2*pbits)-1</code>, and transforms it in-place to an integer
260 * which is congruent mod \p P to the given MPI, and is close enough to \p pbits
261 * in size, so that it may be efficiently brought in the 0..P-1 range by a few
262 * additions or subtractions. Therefore, it is only an approximative modular
263 * reduction. It must return 0 on success and non-zero on failure.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100264 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500265 * \note Alternative implementations must keep the group IDs distinct. If
266 * two group structures have the same ID, then they must be
267 * identical.
268 *
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100269 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100270typedef struct mbedtls_ecp_group {
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100271 mbedtls_ecp_group_id id; /*!< An internal group identifier. */
Rose Zadikf56cb342018-04-19 12:49:10 +0100272 mbedtls_mpi P; /*!< The prime modulus of the base field. */
Chien Wong12f6f282023-08-09 22:15:14 +0800273 mbedtls_mpi A; /*!< For Short Weierstrass: \p A in the equation. Note that
274 \p A is not set to the authentic value in some cases.
275 Refer to detailed description of ::mbedtls_ecp_group if
276 using domain parameters in the structure.
277 For Montgomery curves: <code>(A + 2) / 4</code>. */
Rose Zadikb2e111a2018-04-20 10:13:48 +0100278 mbedtls_mpi B; /*!< For Short Weierstrass: \p B in the equation.
279 For Montgomery curves: unused. */
Rose Zadikf56cb342018-04-19 12:49:10 +0100280 mbedtls_ecp_point G; /*!< The generator of the subgroup used. */
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100281 mbedtls_mpi N; /*!< The order of \p G. */
282 size_t pbits; /*!< The number of bits in \p P.*/
Rose Zadikb2e111a2018-04-20 10:13:48 +0100283 size_t nbits; /*!< For Short Weierstrass: The number of bits in \p P.
Rose Zadika7a61552018-04-24 13:14:01 +0100284 For Montgomery curves: the number of bits in the
Rose Zadikd76ac582018-04-23 06:29:34 +0100285 private keys. */
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100286 unsigned int h; /*!< \internal 1 if the constants are static. */
Rose Zadika7a61552018-04-24 13:14:01 +0100287 int (*modp)(mbedtls_mpi *); /*!< The function for fast pseudo-reduction
Rose Zadikf56cb342018-04-19 12:49:10 +0100288 mod \p P (see above).*/
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100289 int (*t_pre)(mbedtls_ecp_point *, void *); /*!< Unused. */
290 int (*t_post)(mbedtls_ecp_point *, void *); /*!< Unused. */
291 void *t_data; /*!< Unused. */
292 mbedtls_ecp_point *T; /*!< Pre-computed points for ecp_mul_comb(). */
Rose Zadikb2e111a2018-04-20 10:13:48 +0100293 size_t T_size; /*!< The number of pre-computed points. */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100294}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295mbedtls_ecp_group;
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100296
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500297/**
298 * \name SECTION: Module settings
299 *
300 * The configuration options you can set for this module are in this section.
301 * Either change them in config.h, or define them using the compiler command line.
302 * \{
303 */
304
Gilles Peskine6dba3202021-06-02 23:21:07 +0200305#if defined(MBEDTLS_ECP_MAX_BITS)
306
307#if MBEDTLS_ECP_MAX_BITS < MBEDTLS_ECP_MAX_BITS_MIN
308#error "MBEDTLS_ECP_MAX_BITS is smaller than the largest supported curve"
309#endif
310
Gilles Peskine33c92f02021-06-02 23:34:02 +0200311#elif defined(MBEDTLS_ECP_C)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500312/**
313 * The maximum size of the groups, that is, of \c N and \c P.
314 */
Gilles Peskine33c92f02021-06-02 23:34:02 +0200315#define MBEDTLS_ECP_MAX_BITS MBEDTLS_ECP_MAX_BITS_MIN
316
317#else
318/* MBEDTLS_ECP_MAX_BITS is not relevant without MBEDTLS_ECP_C, but set it
319 * to a nonzero value so that code that unconditionally allocates an array
320 * of a size based on it keeps working if built without ECC support. */
321#define MBEDTLS_ECP_MAX_BITS 1
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500322#endif
323
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100324#define MBEDTLS_ECP_MAX_BYTES ((MBEDTLS_ECP_MAX_BITS + 7) / 8)
325#define MBEDTLS_ECP_MAX_PT_LEN (2 * MBEDTLS_ECP_MAX_BYTES + 1)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500326
327#if !defined(MBEDTLS_ECP_WINDOW_SIZE)
328/*
329 * Maximum "window" size used for point multiplication.
Tom Cosgrove2b150752022-05-26 11:55:43 +0100330 * Default: a point where higher memory usage yields diminishing performance
Gilles Peskineb3ca90b2021-06-02 13:27:03 +0200331 * returns.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500332 * Minimum value: 2. Maximum value: 7.
333 *
334 * Result is an array of at most ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) )
335 * points used for point multiplication. This value is directly tied to EC
336 * peak memory usage, so decreasing it by one should roughly cut memory usage
337 * by two (if large curves are in use).
338 *
339 * Reduction in size may reduce speed, but larger curves are impacted first.
340 * Sample performances (in ECDHE handshakes/s, with FIXED_POINT_OPTIM = 1):
341 * w-size: 6 5 4 3 2
342 * 521 145 141 135 120 97
343 * 384 214 209 198 177 146
344 * 256 320 320 303 262 226
345 * 224 475 475 453 398 342
346 * 192 640 640 633 587 476
347 */
Gilles Peskineb3ca90b2021-06-02 13:27:03 +0200348#define MBEDTLS_ECP_WINDOW_SIZE 4 /**< The maximum window size used. */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500349#endif /* MBEDTLS_ECP_WINDOW_SIZE */
350
351#if !defined(MBEDTLS_ECP_FIXED_POINT_OPTIM)
352/*
353 * Trade memory for speed on fixed-point multiplication.
354 *
355 * This speeds up repeated multiplication of the generator (that is, the
356 * multiplication in ECDSA signatures, and half of the multiplications in
357 * ECDSA verification and ECDHE) by a factor roughly 3 to 4.
358 *
359 * The cost is increasing EC peak memory usage by a factor roughly 2.
360 *
361 * Change this value to 0 to reduce peak memory usage.
362 */
363#define MBEDTLS_ECP_FIXED_POINT_OPTIM 1 /**< Enable fixed-point speed-up. */
364#endif /* MBEDTLS_ECP_FIXED_POINT_OPTIM */
365
Andrzej Kurek73afe272022-01-24 10:31:06 -0500366/** \} name SECTION: Module settings */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500367
368#else /* MBEDTLS_ECP_ALT */
369#include "ecp_alt.h"
370#endif /* MBEDTLS_ECP_ALT */
371
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200372#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardb5a50e72017-04-20 16:06:13 +0200373
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200374/**
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +0200375 * \brief Internal restart context for multiplication
376 *
377 * \note Opaque struct
378 */
379typedef struct mbedtls_ecp_restart_mul mbedtls_ecp_restart_mul_ctx;
380
381/**
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200382 * \brief Internal restart context for ecp_muladd()
383 *
384 * \note Opaque struct
385 */
386typedef struct mbedtls_ecp_restart_muladd mbedtls_ecp_restart_muladd_ctx;
387
388/**
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200389 * \brief General context for resuming ECC operations
390 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100391typedef struct {
Manuel Pégourié-Gonnard646393b2017-04-20 10:03:45 +0200392 unsigned ops_done; /*!< current ops count */
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +0200393 unsigned depth; /*!< call depth (0 = top-level) */
Manuel Pégourié-Gonnard646393b2017-04-20 10:03:45 +0200394 mbedtls_ecp_restart_mul_ctx *rsm; /*!< ecp_mul_comb() sub-context */
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200395 mbedtls_ecp_restart_muladd_ctx *ma; /*!< ecp_muladd() sub-context */
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200396} mbedtls_ecp_restart_ctx;
Manuel Pégourié-Gonnardb5a50e72017-04-20 16:06:13 +0200397
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +0200398/*
399 * Operation counts for restartable functions
400 */
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200401#define MBEDTLS_ECP_OPS_CHK 3 /*!< basic ops count for ecp_check_pubkey() */
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +0200402#define MBEDTLS_ECP_OPS_DBL 8 /*!< basic ops count for ecp_double_jac() */
403#define MBEDTLS_ECP_OPS_ADD 11 /*!< basic ops count for see ecp_add_mixed() */
404#define MBEDTLS_ECP_OPS_INV 120 /*!< empirical equivalent for mpi_mod_inv() */
405
406/**
407 * \brief Internal; for restartable functions in other modules.
408 * Check and update basic ops budget.
409 *
410 * \param grp Group structure
411 * \param rs_ctx Restart context
412 * \param ops Number of basic ops to do
413 *
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200414 * \return \c 0 if doing \p ops basic ops is still allowed,
415 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS otherwise.
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +0200416 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100417int mbedtls_ecp_check_budget(const mbedtls_ecp_group *grp,
418 mbedtls_ecp_restart_ctx *rs_ctx,
419 unsigned ops);
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +0200420
421/* Utility macro for checking and updating ops budget */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100422#define MBEDTLS_ECP_BUDGET(ops) \
423 MBEDTLS_MPI_CHK(mbedtls_ecp_check_budget(grp, rs_ctx, \
424 (unsigned) (ops)));
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +0200425
Manuel Pégourié-Gonnardb5a50e72017-04-20 16:06:13 +0200426#else /* MBEDTLS_ECP_RESTARTABLE */
427
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100428#define MBEDTLS_ECP_BUDGET(ops) /* no-op; for compatibility */
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +0200429
Manuel Pégourié-Gonnardb5a50e72017-04-20 16:06:13 +0200430/* We want to declare restartable versions of existing functions anyway */
431typedef void mbedtls_ecp_restart_ctx;
432
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200433#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200434
Paul Bakker088c5c52014-04-25 11:11:10 +0200435/**
Jaeden Amero89453432018-04-24 17:15:38 +0100436 * \brief The ECP key-pair structure.
Ron Eldor05d0e512018-04-16 17:40:04 +0300437 *
Jaeden Amero89453432018-04-24 17:15:38 +0100438 * A generic key-pair that may be used for ECDSA and fixed ECDH, for example.
Ron Eldor05d0e512018-04-16 17:40:04 +0300439 *
Jaeden Amero89453432018-04-24 17:15:38 +0100440 * \note Members are deliberately in the same order as in the
441 * ::mbedtls_ecdsa_context structure.
Ron Eldor05d0e512018-04-16 17:40:04 +0300442 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100443typedef struct mbedtls_ecp_keypair {
Ron Eldor05d0e512018-04-16 17:40:04 +0300444 mbedtls_ecp_group grp; /*!< Elliptic curve and base point */
445 mbedtls_mpi d; /*!< our secret value */
446 mbedtls_ecp_point Q; /*!< our public value */
447}
448mbedtls_ecp_keypair;
449
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100450/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100451 * Point formats, from RFC 4492's enum ECPointFormat
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100452 */
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100453#define MBEDTLS_ECP_PF_UNCOMPRESSED 0 /**< Uncompressed point format. */
454#define MBEDTLS_ECP_PF_COMPRESSED 1 /**< Compressed point format. */
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100455
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100456/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100457 * Some other constants from RFC 4492
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100458 */
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100459#define MBEDTLS_ECP_TLS_NAMED_CURVE 3 /**< The named_curve of ECCurveType. */
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100460
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200461#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100462/**
463 * \brief Set the maximum number of basic operations done in a row.
464 *
465 * If more operations are needed to complete a computation,
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200466 * #MBEDTLS_ERR_ECP_IN_PROGRESS will be returned by the
Manuel Pégourié-Gonnard8467e682017-04-20 09:47:06 +0200467 * function performing the computation. It is then the
468 * caller's responsibility to either call again with the same
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +0200469 * parameters until it returns 0 or an error code; or to free
Manuel Pégourié-Gonnard8467e682017-04-20 09:47:06 +0200470 * the restart context if the operation is to be aborted.
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100471 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +0200472 * It is strictly required that all input parameters and the
473 * restart context be the same on successive calls for the
474 * same operation, but output parameters need not be the
475 * same; they must not be used until the function finally
476 * returns 0.
477 *
Gilles Peskine1174db52020-02-26 19:52:06 +0100478 * This only applies to functions whose documentation
479 * mentions they may return #MBEDTLS_ERR_ECP_IN_PROGRESS (or
480 * #MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS for functions in the
481 * SSL module). For functions that accept a "restart context"
482 * argument, passing NULL disables restart and makes the
483 * function equivalent to the function with the same name
Manuel Pégourié-Gonnard32df9112018-10-15 13:29:21 +0200484 * with \c _restartable removed. For functions in the ECDH
Gilles Peskine1174db52020-02-26 19:52:06 +0100485 * module, restart is disabled unless the function accepts
486 * an "ECDH context" argument and
487 * mbedtls_ecdh_enable_restart() was previously called on
488 * that context. For function in the SSL module, restart is
489 * only enabled for specific sides and key exchanges
490 * (currently only for clients and ECDHE-ECDSA).
Manuel Pégourié-Gonnard8f28add2017-04-19 10:20:49 +0200491 *
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100492 * \param max_ops Maximum number of basic operations done in a row.
493 * Default: 0 (unlimited).
494 * Lower (non-zero) values mean ECC functions will block for
495 * a lesser maximum amount of time.
496 *
Manuel Pégourié-Gonnarde6854492017-03-20 14:35:19 +0100497 * \note A "basic operation" is defined as a rough equivalent of a
498 * multiplication in GF(p) for the NIST P-256 curve.
499 * As an indication, with default settings, a scalar
500 * multiplication (full run of \c mbedtls_ecp_mul()) is:
501 * - about 3300 basic operations for P-256
502 * - about 9400 basic operations for P-384
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100503 *
Manuel Pégourié-Gonnarde58f65a2017-03-20 14:59:54 +0100504 * \note Very low values are not always respected: sometimes
Manuel Pégourié-Gonnard1c678e02017-03-20 13:39:39 +0100505 * functions need to block for a minimum number of
506 * operations, and will do so even if max_ops is set to a
507 * lower value. That minimum depends on the curve size, and
508 * can be made lower by decreasing the value of
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +0200509 * \c MBEDTLS_ECP_WINDOW_SIZE. As an indication, here is the
510 * lowest effective value for various curves and values of
511 * that parameter (w for short):
512 * w=6 w=5 w=4 w=3 w=2
513 * P-256 208 208 160 136 124
514 * P-384 682 416 320 272 248
515 * P-521 1364 832 640 544 496
Manuel Pégourié-Gonnarde58f65a2017-03-20 14:59:54 +0100516 *
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200517 * \note This setting is currently ignored by Curve25519.
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100518 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100519void mbedtls_ecp_set_max_ops(unsigned max_ops);
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200520
521/**
522 * \brief Check if restart is enabled (max_ops != 0)
523 *
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200524 * \return \c 0 if \c max_ops == 0 (restart disabled)
525 * \return \c 1 otherwise (restart enabled)
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200526 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100527int mbedtls_ecp_restart_is_enabled(void);
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200528#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100529
Janos Follathdf9295b2019-02-26 12:36:52 +0000530/*
531 * Get the type of a curve
532 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100533mbedtls_ecp_curve_type mbedtls_ecp_get_type(const mbedtls_ecp_group *grp);
Janos Follathdf9295b2019-02-26 12:36:52 +0000534
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100535/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100536 * \brief This function retrieves the information defined in
Gilles Peskineda728b32021-06-02 14:37:57 +0200537 * mbedtls_ecp_curve_info() for all supported curves.
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200538 *
Gilles Peskined2085f52019-02-11 18:43:55 +0100539 * \note This function returns information about all curves
540 * supported by the library. Some curves may not be
541 * supported for all algorithms. Call mbedtls_ecdh_can_do()
542 * or mbedtls_ecdsa_can_do() to check if a curve is
543 * supported for ECDH or ECDSA.
544 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100545 * \return A statically allocated array. The last entry is 0.
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200546 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100547const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list(void);
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200548
549/**
Rose Zadikf56cb342018-04-19 12:49:10 +0100550 * \brief This function retrieves the list of internal group
551 * identifiers of all supported curves in the order of
552 * preference.
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100553 *
Gilles Peskined2085f52019-02-11 18:43:55 +0100554 * \note This function returns information about all curves
555 * supported by the library. Some curves may not be
556 * supported for all algorithms. Call mbedtls_ecdh_can_do()
557 * or mbedtls_ecdsa_can_do() to check if a curve is
558 * supported for ECDH or ECDSA.
559 *
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100560 * \return A statically allocated array,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 * terminated with MBEDTLS_ECP_DP_NONE.
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100562 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100563const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list(void);
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100564
565/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100566 * \brief This function retrieves curve information from an internal
567 * group identifier.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200568 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100569 * \param grp_id An \c MBEDTLS_ECP_DP_XXX value.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200570 *
Rose Zadikf56cb342018-04-19 12:49:10 +0100571 * \return The associated curve information on success.
572 * \return NULL on failure.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200573 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100574const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_grp_id(mbedtls_ecp_group_id grp_id);
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200575
576/**
Rose Zadika7a61552018-04-24 13:14:01 +0100577 * \brief This function retrieves curve information from a TLS
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100578 * NamedCurve value.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200579 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100580 * \param tls_id An \c MBEDTLS_ECP_DP_XXX value.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200581 *
Rose Zadikf56cb342018-04-19 12:49:10 +0100582 * \return The associated curve information on success.
583 * \return NULL on failure.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200584 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100585const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id(uint16_t tls_id);
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200586
587/**
Rose Zadika7a61552018-04-24 13:14:01 +0100588 * \brief This function retrieves curve information from a
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100589 * human-readable name.
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100590 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100591 * \param name The human-readable name.
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100592 *
Rose Zadikf56cb342018-04-19 12:49:10 +0100593 * \return The associated curve information on success.
594 * \return NULL on failure.
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100595 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100596const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name(const char *name);
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100597
598/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100599 * \brief This function initializes a point as zero.
600 *
601 * \param pt The point to initialize.
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100602 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100603void mbedtls_ecp_point_init(mbedtls_ecp_point *pt);
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100604
605/**
Rose Zadikf56cb342018-04-19 12:49:10 +0100606 * \brief This function initializes an ECP group context
Rose Zadika7a61552018-04-24 13:14:01 +0100607 * without loading any domain parameters.
Rose Zadikf56cb342018-04-19 12:49:10 +0100608 *
609 * \note After this function is called, domain parameters
610 * for various ECP groups can be loaded through the
Gilles Peskine13cf3ec2018-11-07 22:06:48 +0100611 * mbedtls_ecp_group_load() or mbedtls_ecp_tls_read_group()
Rose Zadikf56cb342018-04-19 12:49:10 +0100612 * functions.
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100613 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100614void mbedtls_ecp_group_init(mbedtls_ecp_group *grp);
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100615
616/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100617 * \brief This function initializes a key pair as an invalid one.
618 *
619 * \param key The key pair to initialize.
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200620 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100621void mbedtls_ecp_keypair_init(mbedtls_ecp_keypair *key);
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200622
623/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100624 * \brief This function frees the components of a point.
625 *
626 * \param pt The point to free.
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100627 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100628void mbedtls_ecp_point_free(mbedtls_ecp_point *pt);
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100629
630/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100631 * \brief This function frees the components of an ECP group.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500632 *
633 * \param grp The group to free. This may be \c NULL, in which
634 * case this function returns immediately. If it is not
635 * \c NULL, it must point to an initialized ECP group.
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100636 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100637void mbedtls_ecp_group_free(mbedtls_ecp_group *grp);
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100638
639/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100640 * \brief This function frees the components of a key pair.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500641 *
642 * \param key The key pair to free. This may be \c NULL, in which
643 * case this function returns immediately. If it is not
644 * \c NULL, it must point to an initialized ECP key pair.
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200645 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100646void mbedtls_ecp_keypair_free(mbedtls_ecp_keypair *key);
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200647
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200648#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200649/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500650 * \brief Initialize a restart context.
651 *
652 * \param ctx The restart context to initialize. This must
653 * not be \c NULL.
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200654 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100655void mbedtls_ecp_restart_init(mbedtls_ecp_restart_ctx *ctx);
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200656
657/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500658 * \brief Free the components of a restart context.
659 *
660 * \param ctx The restart context to free. This may be \c NULL, in which
661 * case this function returns immediately. If it is not
662 * \c NULL, it must point to an initialized restart context.
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200663 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100664void mbedtls_ecp_restart_free(mbedtls_ecp_restart_ctx *ctx);
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200665#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200666
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200667/**
Rose Zadika7a61552018-04-24 13:14:01 +0100668 * \brief This function copies the contents of point \p Q into
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100669 * point \p P.
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100670 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500671 * \param P The destination point. This must be initialized.
672 * \param Q The source point. This must be initialized.
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100673 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100674 * \return \c 0 on success.
Rose Zadikf56cb342018-04-19 12:49:10 +0100675 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500676 * \return Another negative error code for other kinds of failure.
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100677 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100678int mbedtls_ecp_copy(mbedtls_ecp_point *P, const mbedtls_ecp_point *Q);
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100679
680/**
Rose Zadika7a61552018-04-24 13:14:01 +0100681 * \brief This function copies the contents of group \p src into
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100682 * group \p dst.
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200683 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500684 * \param dst The destination group. This must be initialized.
685 * \param src The source group. This must be initialized.
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200686 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100687 * \return \c 0 on success.
Rose Zadikf56cb342018-04-19 12:49:10 +0100688 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500689 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200690 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100691int mbedtls_ecp_group_copy(mbedtls_ecp_group *dst,
692 const mbedtls_ecp_group *src);
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200693
694/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500695 * \brief This function sets a point to the point at infinity.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200696 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500697 * \param pt The point to set. This must be initialized.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200698 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100699 * \return \c 0 on success.
Rose Zadikf56cb342018-04-19 12:49:10 +0100700 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500701 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200702 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100703int mbedtls_ecp_set_zero(mbedtls_ecp_point *pt);
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200704
705/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500706 * \brief This function checks if a point is the point at infinity.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200707 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500708 * \param pt The point to test. This must be initialized.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200709 *
Rose Zadikf56cb342018-04-19 12:49:10 +0100710 * \return \c 1 if the point is zero.
711 * \return \c 0 if the point is non-zero.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500712 * \return A negative error code on failure.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200713 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100714int mbedtls_ecp_is_zero(mbedtls_ecp_point *pt);
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200715
716/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100717 * \brief This function compares two points.
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200718 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100719 * \note This assumes that the points are normalized. Otherwise,
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200720 * they may compare as "not equal" even if they are.
721 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500722 * \param P The first point to compare. This must be initialized.
723 * \param Q The second point to compare. This must be initialized.
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200724 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100725 * \return \c 0 if the points are equal.
726 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the points are not equal.
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200727 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100728int mbedtls_ecp_point_cmp(const mbedtls_ecp_point *P,
729 const mbedtls_ecp_point *Q);
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200730
731/**
Rose Zadika7a61552018-04-24 13:14:01 +0100732 * \brief This function imports a non-zero point from two ASCII
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100733 * strings.
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100734 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500735 * \param P The destination point. This must be initialized.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100736 * \param radix The numeric base of the input.
737 * \param x The first affine coordinate, as a null-terminated string.
738 * \param y The second affine coordinate, as a null-terminated string.
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100739 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100740 * \return \c 0 on success.
741 * \return An \c MBEDTLS_ERR_MPI_XXX error code on failure.
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100742 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100743int mbedtls_ecp_point_read_string(mbedtls_ecp_point *P, int radix,
744 const char *x, const char *y);
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100745
746/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100747 * \brief This function exports a point into unsigned binary data.
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100748 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100749 * \param grp The group to which the point should belong.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500750 * This must be initialized and have group parameters
751 * set, for example through mbedtls_ecp_group_load().
752 * \param P The point to export. This must be initialized.
753 * \param format The point format. This must be either
754 * #MBEDTLS_ECP_PF_COMPRESSED or #MBEDTLS_ECP_PF_UNCOMPRESSED.
Janos Follath28eb06d2019-02-26 10:53:34 +0000755 * (For groups without these formats, this parameter is
756 * ignored. But it still has to be either of the above
757 * values.)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500758 * \param olen The address at which to store the length of
759 * the output in Bytes. This must not be \c NULL.
760 * \param buf The output buffer. This must be a writable buffer
761 * of length \p buflen Bytes.
762 * \param buflen The length of the output buffer \p buf in Bytes.
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100763 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100764 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500765 * \return #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the output buffer
766 * is too small to hold the point.
Janos Follath28eb06d2019-02-26 10:53:34 +0000767 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the point format
768 * or the export for the given group is not implemented.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500769 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100770 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100771int mbedtls_ecp_point_write_binary(const mbedtls_ecp_group *grp,
772 const mbedtls_ecp_point *P,
773 int format, size_t *olen,
774 unsigned char *buf, size_t buflen);
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100775
776/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100777 * \brief This function imports a point from unsigned binary data.
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100778 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100779 * \note This function does not check that the point actually
Rose Zadika7a61552018-04-24 13:14:01 +0100780 * belongs to the given group, see mbedtls_ecp_check_pubkey()
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100781 * for that.
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100782 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100783 * \param grp The group to which the point should belong.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500784 * This must be initialized and have group parameters
785 * set, for example through mbedtls_ecp_group_load().
786 * \param P The destination context to import the point to.
787 * This must be initialized.
788 * \param buf The input buffer. This must be a readable buffer
789 * of length \p ilen Bytes.
790 * \param ilen The length of the input buffer \p buf in Bytes.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100791 *
792 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500793 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the input is invalid.
Rose Zadikf56cb342018-04-19 12:49:10 +0100794 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
Janos Follath28eb06d2019-02-26 10:53:34 +0000795 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the import for the
796 * given group is not implemented.
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100797 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100798int mbedtls_ecp_point_read_binary(const mbedtls_ecp_group *grp,
799 mbedtls_ecp_point *P,
800 const unsigned char *buf, size_t ilen);
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100801
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100802/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100803 * \brief This function imports a point from a TLS ECPoint record.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200804 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500805 * \note On function return, \p *buf is updated to point immediately
Rose Zadikf56cb342018-04-19 12:49:10 +0100806 * after the ECPoint record.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200807 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500808 * \param grp The ECP group to use.
809 * This must be initialized and have group parameters
810 * set, for example through mbedtls_ecp_group_load().
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100811 * \param pt The destination point.
Rose Zadikf56cb342018-04-19 12:49:10 +0100812 * \param buf The address of the pointer to the start of the input buffer.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100813 * \param len The length of the buffer.
Manuel Pégourié-Gonnard150c4f62014-11-21 09:14:52 +0100814 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100815 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500816 * \return An \c MBEDTLS_ERR_MPI_XXX error code on initialization
817 * failure.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100818 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200819 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100820int mbedtls_ecp_tls_read_point(const mbedtls_ecp_group *grp,
821 mbedtls_ecp_point *pt,
822 const unsigned char **buf, size_t len);
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200823
824/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500825 * \brief This function exports a point as a TLS ECPoint record
826 * defined in RFC 4492, Section 5.4.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200827 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500828 * \param grp The ECP group to use.
829 * This must be initialized and have group parameters
830 * set, for example through mbedtls_ecp_group_load().
831 * \param pt The point to be exported. This must be initialized.
832 * \param format The point format to use. This must be either
833 * #MBEDTLS_ECP_PF_COMPRESSED or #MBEDTLS_ECP_PF_UNCOMPRESSED.
834 * \param olen The address at which to store the length in Bytes
835 * of the data written.
836 * \param buf The target buffer. This must be a writable buffer of
837 * length \p blen Bytes.
838 * \param blen The length of the target buffer \p buf in Bytes.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200839 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100840 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500841 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the input is invalid.
842 * \return #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the target buffer
843 * is too small to hold the exported point.
844 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200845 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100846int mbedtls_ecp_tls_write_point(const mbedtls_ecp_group *grp,
847 const mbedtls_ecp_point *pt,
848 int format, size_t *olen,
849 unsigned char *buf, size_t blen);
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200850
851/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500852 * \brief This function sets up an ECP group context
853 * from a standardized set of domain parameters.
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100854 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100855 * \note The index should be a value of the NamedCurve enum,
Rose Zadika7a61552018-04-24 13:14:01 +0100856 * as defined in <em>RFC-4492: Elliptic Curve Cryptography
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100857 * (ECC) Cipher Suites for Transport Layer Security (TLS)</em>,
858 * usually in the form of an \c MBEDTLS_ECP_DP_XXX macro.
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100859 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500860 * \param grp The group context to setup. This must be initialized.
Rose Zadikf56cb342018-04-19 12:49:10 +0100861 * \param id The identifier of the domain parameter set to load.
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100862 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500863 * \return \c 0 on success.
864 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if \p id doesn't
865 * correspond to a known group.
866 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100867 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100868int mbedtls_ecp_group_load(mbedtls_ecp_group *grp, mbedtls_ecp_group_id id);
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100869
870/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500871 * \brief This function sets up an ECP group context from a TLS
872 * ECParameters record as defined in RFC 4492, Section 5.4.
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100873 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500874 * \note The read pointer \p buf is updated to point right after
875 * the ECParameters record on exit.
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100876 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500877 * \param grp The group context to setup. This must be initialized.
Rose Zadikf56cb342018-04-19 12:49:10 +0100878 * \param buf The address of the pointer to the start of the input buffer.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500879 * \param len The length of the input buffer \c *buf in Bytes.
Manuel Pégourié-Gonnard150c4f62014-11-21 09:14:52 +0100880 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100881 * \return \c 0 on success.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100882 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500883 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the group is not
884 * recognized.
885 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100886 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100887int mbedtls_ecp_tls_read_group(mbedtls_ecp_group *grp,
888 const unsigned char **buf, size_t len);
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100889
890/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500891 * \brief This function extracts an elliptic curve group ID from a
892 * TLS ECParameters record as defined in RFC 4492, Section 5.4.
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100893 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500894 * \note The read pointer \p buf is updated to point right after
895 * the ECParameters record on exit.
896 *
897 * \param grp The address at which to store the group id.
898 * This must not be \c NULL.
899 * \param buf The address of the pointer to the start of the input buffer.
900 * \param len The length of the input buffer \c *buf in Bytes.
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100901 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100902 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500903 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid.
904 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the group is not
905 * recognized.
906 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100907 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100908int mbedtls_ecp_tls_read_group_id(mbedtls_ecp_group_id *grp,
909 const unsigned char **buf,
910 size_t len);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500911/**
912 * \brief This function exports an elliptic curve as a TLS
913 * ECParameters record as defined in RFC 4492, Section 5.4.
914 *
915 * \param grp The ECP group to be exported.
916 * This must be initialized and have group parameters
917 * set, for example through mbedtls_ecp_group_load().
918 * \param olen The address at which to store the number of Bytes written.
919 * This must not be \c NULL.
920 * \param buf The buffer to write to. This must be a writable buffer
921 * of length \p blen Bytes.
922 * \param blen The length of the output buffer \p buf in Bytes.
923 *
924 * \return \c 0 on success.
925 * \return #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the output
926 * buffer is too small to hold the exported group.
927 * \return Another negative error code on other kinds of failure.
928 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100929int mbedtls_ecp_tls_write_group(const mbedtls_ecp_group *grp,
930 size_t *olen,
931 unsigned char *buf, size_t blen);
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100932
933/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500934 * \brief This function performs a scalar multiplication of a point
935 * by an integer: \p R = \p m * \p P.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100936 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100937 * It is not thread-safe to use same group in multiple threads.
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200938 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100939 * \note To prevent timing attacks, this function
940 * executes the exact same sequence of base-field
941 * operations for any valid \p m. It avoids any if-branch or
942 * array index depending on the value of \p m.
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200943 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100944 * \note If \p f_rng is not NULL, it is used to randomize
945 * intermediate results to prevent potential timing attacks
946 * targeting these results. We recommend always providing
947 * a non-NULL \p f_rng. The overhead is negligible.
Manuel Pégourié-Gonnard71d56672020-06-04 10:20:12 +0200948 * Note: unless #MBEDTLS_ECP_NO_INTERNAL_RNG is defined, when
949 * \p f_rng is NULL, an internal RNG (seeded from the value
950 * of \p m) will be used instead.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100951 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500952 * \param grp The ECP group to use.
953 * This must be initialized and have group parameters
954 * set, for example through mbedtls_ecp_group_load().
955 * \param R The point in which to store the result of the calculation.
956 * This must be initialized.
957 * \param m The integer by which to multiply. This must be initialized.
958 * \param P The point to multiply. This must be initialized.
959 * \param f_rng The RNG function. This may be \c NULL if randomization
960 * of intermediate results isn't desired (discouraged).
961 * \param p_rng The RNG context to be passed to \p p_rng.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100962 *
963 * \return \c 0 on success.
Rose Zadikf56cb342018-04-19 12:49:10 +0100964 * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m is not a valid private
965 * key, or \p P is not a valid public key.
966 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500967 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200968 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100969int mbedtls_ecp_mul(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
970 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
971 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200972
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200973/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200974 * \brief This function performs multiplication of a point by
975 * an integer: \p R = \p m * \p P in a restartable way.
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200976 *
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200977 * \see mbedtls_ecp_mul()
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200978 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200979 * \note This function does the same as \c mbedtls_ecp_mul(), but
980 * it can return early and restart according to the limit set
981 * with \c mbedtls_ecp_set_max_ops() to reduce blocking.
982 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500983 * \param grp The ECP group to use.
984 * This must be initialized and have group parameters
985 * set, for example through mbedtls_ecp_group_load().
986 * \param R The point in which to store the result of the calculation.
987 * This must be initialized.
988 * \param m The integer by which to multiply. This must be initialized.
989 * \param P The point to multiply. This must be initialized.
990 * \param f_rng The RNG function. This may be \c NULL if randomization
991 * of intermediate results isn't desired (discouraged).
992 * \param p_rng The RNG context to be passed to \p p_rng.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200993 * \param rs_ctx The restart context (NULL disables restart).
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200994 *
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200995 * \return \c 0 on success.
996 * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m is not a valid private
997 * key, or \p P is not a valid public key.
998 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
999 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnard8467e682017-04-20 09:47:06 +02001000 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001001 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001002 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001003int mbedtls_ecp_mul_restartable(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
1004 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
1005 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1006 mbedtls_ecp_restart_ctx *rs_ctx);
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001007
Gilles Peskine9b99a892018-09-14 18:32:19 +02001008#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001009/**
Chien Wong12f6f282023-08-09 22:15:14 +08001010 * \brief This function checks if domain parameter A of the curve is
1011 * \c -3.
1012 *
1013 * \note This function is only defined for short Weierstrass curves.
1014 * It may not be included in builds without any short
1015 * Weierstrass curve.
1016 *
1017 * \param grp The ECP group to use.
1018 * This must be initialized and have group parameters
1019 * set, for example through mbedtls_ecp_group_load().
1020 *
1021 * \return \c 1 if <code>A = -3</code>.
1022 * \return \c 0 Otherwise.
1023 */
1024static inline int mbedtls_ecp_group_a_is_minus_3(const mbedtls_ecp_group *grp)
1025{
1026 return grp->A.p == NULL;
1027}
1028
1029/**
Rose Zadika7a61552018-04-24 13:14:01 +01001030 * \brief This function performs multiplication and addition of two
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001031 * points by integers: \p R = \p m * \p P + \p n * \p Q
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02001032 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001033 * It is not thread-safe to use same group in multiple threads.
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02001034 *
Rose Zadika7a61552018-04-24 13:14:01 +01001035 * \note In contrast to mbedtls_ecp_mul(), this function does not
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001036 * guarantee a constant execution flow and timing.
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02001037 *
Gilles Peskine9b99a892018-09-14 18:32:19 +02001038 * \note This function is only defined for short Weierstrass curves.
1039 * It may not be included in builds without any short
1040 * Weierstrass curve.
1041 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001042 * \param grp The ECP group to use.
1043 * This must be initialized and have group parameters
1044 * set, for example through mbedtls_ecp_group_load().
1045 * \param R The point in which to store the result of the calculation.
1046 * This must be initialized.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001047 * \param m The integer by which to multiply \p P.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001048 * This must be initialized.
1049 * \param P The point to multiply by \p m. This must be initialized.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001050 * \param n The integer by which to multiply \p Q.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001051 * This must be initialized.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001052 * \param Q The point to be multiplied by \p n.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001053 * This must be initialized.
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02001054 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001055 * \return \c 0 on success.
Rose Zadika7a61552018-04-24 13:14:01 +01001056 * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m or \p n are not
1057 * valid private keys, or \p P or \p Q are not valid public
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001058 * keys.
Rose Zadikf56cb342018-04-19 12:49:10 +01001059 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
Gilles Peskine9b99a892018-09-14 18:32:19 +02001060 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if \p grp does not
1061 * designate a short Weierstrass curve.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001062 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02001063 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001064int mbedtls_ecp_muladd(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
1065 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
1066 const mbedtls_mpi *n, const mbedtls_ecp_point *Q);
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02001067
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02001068/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001069 * \brief This function performs multiplication and addition of two
1070 * points by integers: \p R = \p m * \p P + \p n * \p Q in a
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +02001071 * restartable way.
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02001072 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001073 * \see \c mbedtls_ecp_muladd()
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02001074 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001075 * \note This function works the same as \c mbedtls_ecp_muladd(),
1076 * but it can return early and restart according to the limit
1077 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02001078 *
Gilles Peskine9b99a892018-09-14 18:32:19 +02001079 * \note This function is only defined for short Weierstrass curves.
1080 * It may not be included in builds without any short
1081 * Weierstrass curve.
1082 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001083 * \param grp The ECP group to use.
1084 * This must be initialized and have group parameters
1085 * set, for example through mbedtls_ecp_group_load().
1086 * \param R The point in which to store the result of the calculation.
1087 * This must be initialized.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001088 * \param m The integer by which to multiply \p P.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001089 * This must be initialized.
1090 * \param P The point to multiply by \p m. This must be initialized.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001091 * \param n The integer by which to multiply \p Q.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001092 * This must be initialized.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001093 * \param Q The point to be multiplied by \p n.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001094 * This must be initialized.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +02001095 * \param rs_ctx The restart context (NULL disables restart).
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001096 *
1097 * \return \c 0 on success.
1098 * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m or \p n are not
1099 * valid private keys, or \p P or \p Q are not valid public
1100 * keys.
1101 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
Gilles Peskine9b99a892018-09-14 18:32:19 +02001102 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if \p grp does not
1103 * designate a short Weierstrass curve.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001104 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02001105 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001106 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02001107 */
1108int mbedtls_ecp_muladd_restartable(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001109 mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
1110 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
1111 const mbedtls_mpi *n, const mbedtls_ecp_point *Q,
1112 mbedtls_ecp_restart_ctx *rs_ctx);
Gilles Peskine9b99a892018-09-14 18:32:19 +02001113#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02001114
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02001115/**
Rose Zadika7a61552018-04-24 13:14:01 +01001116 * \brief This function checks that a point is a valid public key
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001117 * on this curve.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001118 *
Rose Zadika7a61552018-04-24 13:14:01 +01001119 * It only checks that the point is non-zero, has
1120 * valid coordinates and lies on the curve. It does not verify
Andrzej Kurek96ce1b02023-07-14 05:22:42 -04001121 * that it is indeed a multiple of \c G. This additional
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001122 * check is computationally more expensive, is not required
1123 * by standards, and should not be necessary if the group
Rose Zadika7a61552018-04-24 13:14:01 +01001124 * used has a small cofactor. In particular, it is useless for
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001125 * the NIST groups which all have a cofactor of 1.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001126 *
Rose Zadika7a61552018-04-24 13:14:01 +01001127 * \note This function uses bare components rather than an
1128 * ::mbedtls_ecp_keypair structure, to ease use with other
1129 * structures, such as ::mbedtls_ecdh_context or
Rose Zadikb2e111a2018-04-20 10:13:48 +01001130 * ::mbedtls_ecdsa_context.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001131 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001132 * \param grp The ECP group the point should belong to.
1133 * This must be initialized and have group parameters
1134 * set, for example through mbedtls_ecp_group_load().
1135 * \param pt The point to check. This must be initialized.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001136 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001137 * \return \c 0 if the point is a valid public key.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001138 * \return #MBEDTLS_ERR_ECP_INVALID_KEY if the point is not
1139 * a valid public key for the given curve.
1140 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001141 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001142int mbedtls_ecp_check_pubkey(const mbedtls_ecp_group *grp,
1143 const mbedtls_ecp_point *pt);
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001144
1145/**
Andrzej Kurek96ce1b02023-07-14 05:22:42 -04001146 * \brief This function checks that an \c mbedtls_mpi is a
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001147 * valid private key for this curve.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001148 *
Rose Zadika7a61552018-04-24 13:14:01 +01001149 * \note This function uses bare components rather than an
1150 * ::mbedtls_ecp_keypair structure to ease use with other
1151 * structures, such as ::mbedtls_ecdh_context or
Rose Zadikb2e111a2018-04-20 10:13:48 +01001152 * ::mbedtls_ecdsa_context.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001153 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001154 * \param grp The ECP group the private key should belong to.
1155 * This must be initialized and have group parameters
1156 * set, for example through mbedtls_ecp_group_load().
1157 * \param d The integer to check. This must be initialized.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001158 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001159 * \return \c 0 if the point is a valid private key.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001160 * \return #MBEDTLS_ERR_ECP_INVALID_KEY if the point is not a valid
1161 * private key for the given curve.
1162 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001163 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001164int mbedtls_ecp_check_privkey(const mbedtls_ecp_group *grp,
1165 const mbedtls_mpi *d);
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001166
1167/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001168 * \brief This function generates a private key.
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02001169 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001170 * \param grp The ECP group to generate a private key for.
1171 * This must be initialized and have group parameters
1172 * set, for example through mbedtls_ecp_group_load().
1173 * \param d The destination MPI (secret part). This must be initialized.
1174 * \param f_rng The RNG function. This must not be \c NULL.
1175 * \param p_rng The RNG parameter to be passed to \p f_rng. This may be
1176 * \c NULL if \p f_rng doesn't need a context argument.
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02001177 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001178 * \return \c 0 on success.
1179 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
1180 * on failure.
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02001181 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001182int mbedtls_ecp_gen_privkey(const mbedtls_ecp_group *grp,
1183 mbedtls_mpi *d,
1184 int (*f_rng)(void *, unsigned char *, size_t),
1185 void *p_rng);
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02001186
1187/**
Rose Zadika7a61552018-04-24 13:14:01 +01001188 * \brief This function generates a keypair with a configurable base
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001189 * point.
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +02001190 *
Rose Zadika7a61552018-04-24 13:14:01 +01001191 * \note This function uses bare components rather than an
1192 * ::mbedtls_ecp_keypair structure to ease use with other
1193 * structures, such as ::mbedtls_ecdh_context or
Rose Zadikb2e111a2018-04-20 10:13:48 +01001194 * ::mbedtls_ecdsa_context.
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +02001195 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001196 * \param grp The ECP group to generate a key pair for.
1197 * This must be initialized and have group parameters
1198 * set, for example through mbedtls_ecp_group_load().
1199 * \param G The base point to use. This must be initialized
1200 * and belong to \p grp. It replaces the default base
1201 * point \c grp->G used by mbedtls_ecp_gen_keypair().
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001202 * \param d The destination MPI (secret part).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001203 * This must be initialized.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001204 * \param Q The destination point (public part).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001205 * This must be initialized.
1206 * \param f_rng The RNG function. This must not be \c NULL.
1207 * \param p_rng The RNG context to be passed to \p f_rng. This may
1208 * be \c NULL if \p f_rng doesn't need a context argument.
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +02001209 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001210 * \return \c 0 on success.
1211 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
1212 * on failure.
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +02001213 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001214int mbedtls_ecp_gen_keypair_base(mbedtls_ecp_group *grp,
1215 const mbedtls_ecp_point *G,
1216 mbedtls_mpi *d, mbedtls_ecp_point *Q,
1217 int (*f_rng)(void *, unsigned char *, size_t),
1218 void *p_rng);
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +02001219
1220/**
Rose Zadikf56cb342018-04-19 12:49:10 +01001221 * \brief This function generates an ECP keypair.
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001222 *
Rose Zadika7a61552018-04-24 13:14:01 +01001223 * \note This function uses bare components rather than an
1224 * ::mbedtls_ecp_keypair structure to ease use with other
1225 * structures, such as ::mbedtls_ecdh_context or
Rose Zadikb2e111a2018-04-20 10:13:48 +01001226 * ::mbedtls_ecdsa_context.
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001227 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001228 * \param grp The ECP group to generate a key pair for.
1229 * This must be initialized and have group parameters
1230 * set, for example through mbedtls_ecp_group_load().
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001231 * \param d The destination MPI (secret part).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001232 * This must be initialized.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001233 * \param Q The destination point (public part).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001234 * This must be initialized.
1235 * \param f_rng The RNG function. This must not be \c NULL.
1236 * \param p_rng The RNG context to be passed to \p f_rng. This may
1237 * be \c NULL if \p f_rng doesn't need a context argument.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001238 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001239 * \return \c 0 on success.
1240 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
1241 * on failure.
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001242 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001243int mbedtls_ecp_gen_keypair(mbedtls_ecp_group *grp, mbedtls_mpi *d,
1244 mbedtls_ecp_point *Q,
1245 int (*f_rng)(void *, unsigned char *, size_t),
1246 void *p_rng);
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001247
1248/**
Rose Zadikf56cb342018-04-19 12:49:10 +01001249 * \brief This function generates an ECP key.
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01001250 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001251 * \param grp_id The ECP group identifier.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001252 * \param key The destination key. This must be initialized.
1253 * \param f_rng The RNG function to use. This must not be \c NULL.
1254 * \param p_rng The RNG context to be passed to \p f_rng. This may
1255 * be \c NULL if \p f_rng doesn't need a context argument.
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01001256 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001257 * \return \c 0 on success.
1258 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
1259 * on failure.
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01001260 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001261int mbedtls_ecp_gen_key(mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
1262 int (*f_rng)(void *, unsigned char *, size_t),
1263 void *p_rng);
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01001264
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01001265/**
Janos Follathb65853c2019-02-25 16:33:28 +00001266 * \brief This function reads an elliptic curve private key.
Janos Follath171a7ef2019-02-15 16:17:45 +00001267 *
1268 * \param grp_id The ECP group identifier.
1269 * \param key The destination key.
Tobias Nießen02b6fba2021-05-10 19:53:15 +02001270 * \param buf The buffer containing the binary representation of the
Janos Follath171a7ef2019-02-15 16:17:45 +00001271 * key. (Big endian integer for Weierstrass curves, byte
1272 * string for Montgomery curves.)
1273 * \param buflen The length of the buffer in bytes.
1274 *
1275 * \return \c 0 on success.
Janos Follathb65853c2019-02-25 16:33:28 +00001276 * \return #MBEDTLS_ERR_ECP_INVALID_KEY error if the key is
1277 * invalid.
1278 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
Janos Follath28eb06d2019-02-26 10:53:34 +00001279 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the operation for
1280 * the group is not implemented.
Janos Follathb65853c2019-02-25 16:33:28 +00001281 * \return Another negative error code on different kinds of failure.
Janos Follath171a7ef2019-02-15 16:17:45 +00001282 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001283int mbedtls_ecp_read_key(mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
1284 const unsigned char *buf, size_t buflen);
Steven Cooremande8593f2020-06-09 19:55:26 +02001285
1286/**
1287 * \brief This function exports an elliptic curve private key.
1288 *
Steven Cooremande8593f2020-06-09 19:55:26 +02001289 * \param key The private key.
Steven Cooremanc9b7f782020-06-11 17:00:36 +02001290 * \param buf The output buffer for containing the binary representation
1291 * of the key. (Big endian integer for Weierstrass curves, byte
Steven Cooremande8593f2020-06-09 19:55:26 +02001292 * string for Montgomery curves.)
1293 * \param buflen The total length of the buffer in bytes.
1294 *
1295 * \return \c 0 on success.
Steven Cooremanc9b7f782020-06-11 17:00:36 +02001296 * \return #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the \p key
1297 representation is larger than the available space in \p buf.
Steven Cooremande8593f2020-06-09 19:55:26 +02001298 * \return Another negative error code on different kinds of failure.
1299 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001300int mbedtls_ecp_write_key(mbedtls_ecp_keypair *key,
1301 unsigned char *buf, size_t buflen);
Steven Cooremande8593f2020-06-09 19:55:26 +02001302
Janos Follath171a7ef2019-02-15 16:17:45 +00001303/**
Rose Zadika7a61552018-04-24 13:14:01 +01001304 * \brief This function checks that the keypair objects
Rose Zadikb2e111a2018-04-20 10:13:48 +01001305 * \p pub and \p prv have the same group and the
1306 * same public point, and that the private key in
1307 * \p prv is consistent with the public key.
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01001308 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001309 * \param pub The keypair structure holding the public key. This
1310 * must be initialized. If it contains a private key, that
1311 * part is ignored.
Rose Zadikb2e111a2018-04-20 10:13:48 +01001312 * \param prv The keypair structure holding the full keypair.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001313 * This must be initialized.
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01001314 *
Rose Zadika7a61552018-04-24 13:14:01 +01001315 * \return \c 0 on success, meaning that the keys are valid and match.
Rose Zadikb2e111a2018-04-20 10:13:48 +01001316 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the keys are invalid or do not match.
Rose Zadika7a61552018-04-24 13:14:01 +01001317 * \return An \c MBEDTLS_ERR_ECP_XXX or an \c MBEDTLS_ERR_MPI_XXX
Rose Zadikb2e111a2018-04-20 10:13:48 +01001318 * error code on calculation failure.
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01001319 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001320int mbedtls_ecp_check_pub_priv(const mbedtls_ecp_keypair *pub,
1321 const mbedtls_ecp_keypair *prv);
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01001322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323#if defined(MBEDTLS_SELF_TEST)
Janos Follathb0697532016-08-18 12:38:46 +01001324
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01001325/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001326 * \brief The ECP checkup routine.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001327 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001328 * \return \c 0 on success.
1329 * \return \c 1 on failure.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001330 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001331int mbedtls_ecp_self_test(int verbose);
Janos Follathb0697532016-08-18 12:38:46 +01001332
Janos Follath372697b2016-10-28 16:53:11 +01001333#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001334
1335#ifdef __cplusplus
1336}
1337#endif
1338
Paul Bakker9af723c2014-05-01 13:03:14 +02001339#endif /* ecp.h */