blob: 849b515852bda9d2290636bdae3fefd7b12c2ee0 [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020019 * SPDX-License-Identifier: Apache-2.0
20 *
21 * Licensed under the Apache License, Version 2.0 (the "License"); you may
22 * not use this file except in compliance with the License.
23 * You may obtain a copy of the License at
24 *
25 * http://www.apache.org/licenses/LICENSE-2.0
26 *
27 * Unless required by applicable law or agreed to in writing, software
28 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
29 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30 * See the License for the specific language governing permissions and
31 * limitations under the License.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010032 */
Rose Zadika7a61552018-04-24 13:14:01 +010033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#ifndef MBEDTLS_ECP_H
35#define MBEDTLS_ECP_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020036#include "mbedtls/private_access.h"
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010037
Bence Szépkútic662b362021-05-27 11:25:03 +020038#include "mbedtls/build_info.h"
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050039
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010040#include "mbedtls/bignum.h"
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010041
42/*
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +010043 * ECP error codes
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010044 */
Gilles Peskined2971572021-07-26 18:48:10 +020045/** Bad input parameters to function. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020046#define MBEDTLS_ERR_ECP_BAD_INPUT_DATA -0x4F80
Gilles Peskined2971572021-07-26 18:48:10 +020047/** The buffer is too small to write to. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020048#define MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL -0x4F00
49/** The requested feature is not available, for example, the requested curve is
50 * not supported. */
51#define MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE -0x4E80
Gilles Peskined2971572021-07-26 18:48:10 +020052/** The signature is not valid. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020053#define MBEDTLS_ERR_ECP_VERIFY_FAILED -0x4E00
Gilles Peskined2971572021-07-26 18:48:10 +020054/** Memory allocation failed. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020055#define MBEDTLS_ERR_ECP_ALLOC_FAILED -0x4D80
Gilles Peskined2971572021-07-26 18:48:10 +020056/** Generation of random value, such as ephemeral key, failed. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020057#define MBEDTLS_ERR_ECP_RANDOM_FAILED -0x4D00
Gilles Peskined2971572021-07-26 18:48:10 +020058/** Invalid private or public key. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020059#define MBEDTLS_ERR_ECP_INVALID_KEY -0x4C80
Gilles Peskined2971572021-07-26 18:48:10 +020060/** The buffer contains a valid signature followed by more data. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020061#define MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH -0x4C00
Gilles Peskined2971572021-07-26 18:48:10 +020062/** Operation in progress, call again with the same parameters to continue. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020063#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) || \
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020072 defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || \
73 defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || \
74 defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || \
Gilles Peskinea3de08d2020-07-22 01:23:37 +020075 defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || \
76 defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || \
77 defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020078# define MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED
Gilles Peskine9b99a892018-09-14 18:32:19 +020079#endif
80#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || \
81 defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020082# define MBEDTLS_ECP_MONTGOMERY_ENABLED
Gilles Peskine9b99a892018-09-14 18:32:19 +020083#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 Peskine6e1387a2021-06-07 20:24:51 +0200102 * - Update the calculation of MBEDTLS_ECP_MAX_BITS below.
Gilles Peskine963a2072018-09-14 18:31:30 +0200103 * - Add the corresponding MBEDTLS_ECP_DP_xxx_ENABLED macro definition to
Bence Szépkútibb0cfeb2021-05-28 09:42:25 +0200104 * mbedtls_config.h.
Gilles Peskine963a2072018-09-14 18:31:30 +0200105 * - 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.
Gilles Peskine601d3432021-06-07 20:27:01 +0200111 * - Add the curve to applicable profiles in x509_crt.c.
112 * - Add the curve to applicable presets in ssl_tls.c.
Gilles Peskine963a2072018-09-14 18:31:30 +0200113 */
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200114typedef enum
115{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200116 MBEDTLS_ECP_DP_NONE = 0, /*!< Curve not defined. */
117 MBEDTLS_ECP_DP_SECP192R1, /*!< Domain parameters for the 192-bit curve
118 defined by FIPS 186-4 and SEC1. */
119 MBEDTLS_ECP_DP_SECP224R1, /*!< Domain parameters for the 224-bit curve
120 defined by FIPS 186-4 and SEC1. */
121 MBEDTLS_ECP_DP_SECP256R1, /*!< Domain parameters for the 256-bit curve
122 defined by FIPS 186-4 and SEC1. */
123 MBEDTLS_ECP_DP_SECP384R1, /*!< Domain parameters for the 384-bit curve
124 defined by FIPS 186-4 and SEC1. */
125 MBEDTLS_ECP_DP_SECP521R1, /*!< Domain parameters for the 521-bit curve
126 defined by FIPS 186-4 and SEC1. */
127 MBEDTLS_ECP_DP_BP256R1, /*!< Domain parameters for 256-bit Brainpool curve.
128 */
129 MBEDTLS_ECP_DP_BP384R1, /*!< Domain parameters for 384-bit Brainpool curve.
130 */
131 MBEDTLS_ECP_DP_BP512R1, /*!< Domain parameters for 512-bit Brainpool curve.
132 */
133 MBEDTLS_ECP_DP_CURVE25519, /*!< Domain parameters for Curve25519. */
134 MBEDTLS_ECP_DP_SECP192K1, /*!< Domain parameters for 192-bit "Koblitz"
135 curve. */
136 MBEDTLS_ECP_DP_SECP224K1, /*!< Domain parameters for 224-bit "Koblitz"
137 curve. */
138 MBEDTLS_ECP_DP_SECP256K1, /*!< Domain parameters for 256-bit "Koblitz"
139 curve. */
140 MBEDTLS_ECP_DP_CURVE448, /*!< Domain parameters for Curve448. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141} mbedtls_ecp_group_id;
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200142
143/**
Rose Zadikf56cb342018-04-19 12:49:10 +0100144 * The number of supported curves, plus one for #MBEDTLS_ECP_DP_NONE.
Manuel Pégourié-Gonnard66153662013-12-03 14:12:26 +0100145 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100146 * \note Montgomery curves are currently excluded.
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200147 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200148#define MBEDTLS_ECP_DP_MAX 12
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200149
Janos Follathdf9295b2019-02-26 12:36:52 +0000150/*
151 * Curve types
152 */
153typedef enum
154{
155 MBEDTLS_ECP_TYPE_NONE = 0,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200156 MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS, /* y^2 = x^3 + a x + b */
157 MBEDTLS_ECP_TYPE_MONTGOMERY, /* y^2 = x^3 + a x^2 + x */
Janos Follathdf9295b2019-02-26 12:36:52 +0000158} mbedtls_ecp_curve_type;
159
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200160/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100161 * Curve information, for use by other modules.
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200162 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200163typedef struct mbedtls_ecp_curve_info {
164 mbedtls_ecp_group_id MBEDTLS_PRIVATE(grp_id); /*!< An internal identifier.
165 */
166 uint16_t MBEDTLS_PRIVATE(tls_id); /*!< The TLS NamedCurve identifier. */
167 uint16_t MBEDTLS_PRIVATE(bit_size); /*!< The curve size in bits. */
168 const char *MBEDTLS_PRIVATE(name); /*!< A human-friendly name. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169} mbedtls_ecp_curve_info;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200170
171/**
Rose Zadikf56cb342018-04-19 12:49:10 +0100172 * \brief The ECP point structure, in Jacobian coordinates.
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100173 *
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100174 * \note All functions expect and return points satisfying
Rose Zadikf56cb342018-04-19 12:49:10 +0100175 * the following condition: <code>Z == 0</code> or
Rose Zadika7a61552018-04-24 13:14:01 +0100176 * <code>Z == 1</code>. Other values of \p Z are
Rose Zadikf56cb342018-04-19 12:49:10 +0100177 * used only by internal functions.
178 * The point is zero, or "at infinity", if <code>Z == 0</code>.
Rose Zadika7a61552018-04-24 13:14:01 +0100179 * Otherwise, \p X and \p Y are its standard (affine)
Rose Zadikf56cb342018-04-19 12:49:10 +0100180 * coordinates.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100181 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200182typedef struct mbedtls_ecp_point {
183 mbedtls_mpi MBEDTLS_PRIVATE(X); /*!< The X coordinate of the ECP point. */
184 mbedtls_mpi MBEDTLS_PRIVATE(Y); /*!< The Y coordinate of the ECP point. */
185 mbedtls_mpi MBEDTLS_PRIVATE(Z); /*!< The Z coordinate of the ECP point. */
186} mbedtls_ecp_point;
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100187
188#if !defined(MBEDTLS_ECP_ALT)
189/*
190 * default mbed TLS elliptic curve arithmetic implementation
191 *
192 * (in case MBEDTLS_ECP_ALT is defined then the developer has to provide an
193 * alternative implementation for the whole module and it will replace this
194 * one.)
195 */
196
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100197/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100198 * \brief The ECP group structure.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100199 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100200 * We consider two types of curve equations:
Rose Zadikb2e111a2018-04-20 10:13:48 +0100201 * <ul><li>Short Weierstrass: <code>y^2 = x^3 + A x + B mod P</code>
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100202 * (SEC1 + RFC-4492)</li>
Rose Zadikf56cb342018-04-19 12:49:10 +0100203 * <li>Montgomery: <code>y^2 = x^3 + A x^2 + x mod P</code> (Curve25519,
Rose Zadikb2e111a2018-04-20 10:13:48 +0100204 * Curve448)</li></ul>
Rose Zadikf56cb342018-04-19 12:49:10 +0100205 * In both cases, the generator (\p G) for a prime-order subgroup is fixed.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100206 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100207 * For Short Weierstrass, this subgroup is the whole curve, and its
Rose Zadikf56cb342018-04-19 12:49:10 +0100208 * cardinality is denoted by \p N. Our code requires that \p N is an
Rose Zadikb2e111a2018-04-20 10:13:48 +0100209 * odd prime as mbedtls_ecp_mul() requires an odd number, and
210 * mbedtls_ecdsa_sign() requires that it is prime for blinding purposes.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100211 *
Rose Zadikf56cb342018-04-19 12:49:10 +0100212 * For Montgomery curves, we do not store \p A, but <code>(A + 2) / 4</code>,
Rose Zadika7a61552018-04-24 13:14:01 +0100213 * which is the quantity used in the formulas. Additionally, \p nbits is
Rose Zadikf56cb342018-04-19 12:49:10 +0100214 * not the size of \p N but the required size for private keys.
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100215 *
Rose Zadika7a61552018-04-24 13:14:01 +0100216 * If \p modp is NULL, reduction modulo \p P is done using a generic algorithm.
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200217 * Otherwise, \p modp must point to a function that takes an \p mbedtls_mpi in
218 * the range of <code>0..2^(2*pbits)-1</code>, and transforms it in-place to an
219 * integer which is congruent mod \p P to the given MPI, and is close enough to
220 * \p pbits in size, so that it may be efficiently brought in the 0..P-1 range
221 * by a few additions or subtractions. Therefore, it is only an approximative
222 * modular reduction. It must return 0 on success and non-zero on failure.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100223 *
Gilles Peskinebf69ea52021-05-27 23:53:07 +0200224 * \note Alternative implementations of the ECP module must obey the
225 * following constraints.
226 * * Group IDs must be distinct: if two group structures have
227 * the same ID, then they must be identical.
228 * * The fields \c id, \c P, \c A, \c B, \c G, \c N,
229 * \c pbits and \c nbits must have the same type and semantics
230 * as in the built-in implementation.
231 * They must be available for reading, but direct modification
232 * of these fields does not need to be supported.
233 * They do not need to be at the same offset in the structure.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100234 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200235typedef struct mbedtls_ecp_group {
236 mbedtls_ecp_group_id id; /*!< An internal group identifier. */
237 mbedtls_mpi P; /*!< The prime modulus of the base field. */
238 mbedtls_mpi A; /*!< For Short Weierstrass: \p A in the equation. For
239 Montgomery curves: <code>(A + 2) / 4</code>. */
240 mbedtls_mpi B; /*!< For Short Weierstrass: \p B in the equation.
241 For Montgomery curves: unused. */
242 mbedtls_ecp_point G; /*!< The generator of the subgroup used. */
243 mbedtls_mpi N; /*!< The order of \p G. */
244 size_t pbits; /*!< The number of bits in \p P.*/
245 size_t nbits; /*!< For Short Weierstrass: The number of bits in \p P.
246 For Montgomery curves: the number of bits in the
247 private keys. */
Gilles Peskinebf69ea52021-05-27 23:53:07 +0200248 /* End of public fields */
249
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200250 unsigned int MBEDTLS_PRIVATE(h); /*!< \internal 1 if the constants are
251 static. */
252 int (*MBEDTLS_PRIVATE(modp))(mbedtls_mpi *); /*!< The function for fast
253 pseudo-reduction mod \p P (see above).*/
254 int (*MBEDTLS_PRIVATE(t_pre))(mbedtls_ecp_point *, void *); /*!< Unused. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200255 int (*MBEDTLS_PRIVATE(t_post))(mbedtls_ecp_point *, void *); /*!< Unused. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200256 void *MBEDTLS_PRIVATE(t_data); /*!< Unused. */
257 mbedtls_ecp_point *MBEDTLS_PRIVATE(T); /*!< Pre-computed points for
258 ecp_mul_comb(). */
259 size_t MBEDTLS_PRIVATE(T_size); /*!< The number of dynamic allocated
260 pre-computed points. */
261} mbedtls_ecp_group;
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100262
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500263/**
264 * \name SECTION: Module settings
265 *
266 * The configuration options you can set for this module are in this section.
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200267 * Either change them in mbedtls_config.h, or define them using the compiler
268 * command line.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500269 * \{
270 */
271
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200272# if !defined(MBEDTLS_ECP_WINDOW_SIZE)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500273/*
274 * Maximum "window" size used for point multiplication.
Gilles Peskine5752e592021-06-02 13:27:03 +0200275 * Default: a point where higher memory usage yields disminishing performance
276 * returns.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500277 * Minimum value: 2. Maximum value: 7.
278 *
279 * Result is an array of at most ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) )
280 * points used for point multiplication. This value is directly tied to EC
281 * peak memory usage, so decreasing it by one should roughly cut memory usage
282 * by two (if large curves are in use).
283 *
284 * Reduction in size may reduce speed, but larger curves are impacted first.
285 * Sample performances (in ECDHE handshakes/s, with FIXED_POINT_OPTIM = 1):
286 * w-size: 6 5 4 3 2
287 * 521 145 141 135 120 97
288 * 384 214 209 198 177 146
289 * 256 320 320 303 262 226
290 * 224 475 475 453 398 342
291 * 192 640 640 633 587 476
292 */
Mateusz Starzyk16fec332021-07-22 16:43:35 +0200293/** The maximum window size used. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200294# define MBEDTLS_ECP_WINDOW_SIZE 4
295# endif /* MBEDTLS_ECP_WINDOW_SIZE */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500296
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200297# if !defined(MBEDTLS_ECP_FIXED_POINT_OPTIM)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500298/*
kXuan782c2b92021-06-02 16:53:42 +0800299 * Trade code size for speed on fixed-point multiplication.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500300 *
301 * This speeds up repeated multiplication of the generator (that is, the
302 * multiplication in ECDSA signatures, and half of the multiplications in
303 * ECDSA verification and ECDHE) by a factor roughly 3 to 4.
304 *
kXuan782c2b92021-06-02 16:53:42 +0800305 * For each n-bit Short Weierstrass curve that is enabled, this adds 4n bytes
306 * of code size if n < 384 and 8n otherwise.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500307 *
kXuan782c2b92021-06-02 16:53:42 +0800308 * Change this value to 0 to reduce code size.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500309 */
Mateusz Starzyk16fec332021-07-22 16:43:35 +0200310/** Enable fixed-point speed-up. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200311# define MBEDTLS_ECP_FIXED_POINT_OPTIM 1
312# endif /* MBEDTLS_ECP_FIXED_POINT_OPTIM */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500313
314/* \} name SECTION: Module settings */
315
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200316#else /* MBEDTLS_ECP_ALT */
317# include "ecp_alt.h"
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500318#endif /* MBEDTLS_ECP_ALT */
319
Gilles Peskined0b16302021-05-28 00:17:26 +0200320/**
321 * The maximum size of the groups, that is, of \c N and \c P.
322 */
323#if !defined(MBEDTLS_ECP_C)
324/* Dummy definition to help code that has optional ECP support and
325 * defines an MBEDTLS_ECP_MAX_BYTES-sized array unconditionally. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200326# define MBEDTLS_ECP_MAX_BITS 1
Gilles Peskined0b16302021-05-28 00:17:26 +0200327/* Note: the curves must be listed in DECREASING size! */
328#elif defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200329# define MBEDTLS_ECP_MAX_BITS 521
Gilles Peskined0b16302021-05-28 00:17:26 +0200330#elif defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200331# define MBEDTLS_ECP_MAX_BITS 512
Gilles Peskined0b16302021-05-28 00:17:26 +0200332#elif defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200333# define MBEDTLS_ECP_MAX_BITS 448
Gilles Peskined0b16302021-05-28 00:17:26 +0200334#elif defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200335# define MBEDTLS_ECP_MAX_BITS 384
Gilles Peskined0b16302021-05-28 00:17:26 +0200336#elif defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200337# define MBEDTLS_ECP_MAX_BITS 384
Gilles Peskined0b16302021-05-28 00:17:26 +0200338#elif defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200339# define MBEDTLS_ECP_MAX_BITS 256
Gilles Peskined0b16302021-05-28 00:17:26 +0200340#elif defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200341# define MBEDTLS_ECP_MAX_BITS 256
Gilles Peskined0b16302021-05-28 00:17:26 +0200342#elif defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200343# define MBEDTLS_ECP_MAX_BITS 256
Gilles Peskined0b16302021-05-28 00:17:26 +0200344#elif defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200345# define MBEDTLS_ECP_MAX_BITS 255
Gilles Peskined0b16302021-05-28 00:17:26 +0200346#elif defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200347# define MBEDTLS_ECP_MAX_BITS 225 // n is slightly above 2^224
Gilles Peskined0b16302021-05-28 00:17:26 +0200348#elif defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200349# define MBEDTLS_ECP_MAX_BITS 224
Gilles Peskined0b16302021-05-28 00:17:26 +0200350#elif defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200351# define MBEDTLS_ECP_MAX_BITS 192
Gilles Peskined0b16302021-05-28 00:17:26 +0200352#elif defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200353# define MBEDTLS_ECP_MAX_BITS 192
Gilles Peskined0b16302021-05-28 00:17:26 +0200354#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200355# error "Missing definition of MBEDTLS_ECP_MAX_BITS"
Gilles Peskined0b16302021-05-28 00:17:26 +0200356#endif
357
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200358#define MBEDTLS_ECP_MAX_BYTES ((MBEDTLS_ECP_MAX_BITS + 7) / 8)
359#define MBEDTLS_ECP_MAX_PT_LEN (2 * MBEDTLS_ECP_MAX_BYTES + 1)
Gilles Peskine9ef1ea72021-05-28 00:16:18 +0200360
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200361#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardb5a50e72017-04-20 16:06:13 +0200362
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200363/**
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +0200364 * \brief Internal restart context for multiplication
365 *
366 * \note Opaque struct
367 */
368typedef struct mbedtls_ecp_restart_mul mbedtls_ecp_restart_mul_ctx;
369
370/**
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200371 * \brief Internal restart context for ecp_muladd()
372 *
373 * \note Opaque struct
374 */
375typedef struct mbedtls_ecp_restart_muladd mbedtls_ecp_restart_muladd_ctx;
376
377/**
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200378 * \brief General context for resuming ECC operations
379 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200380typedef struct {
381 unsigned MBEDTLS_PRIVATE(ops_done); /*!< current ops count */
382 unsigned MBEDTLS_PRIVATE(depth); /*!< call depth (0 = top-level) */
383 mbedtls_ecp_restart_mul_ctx *MBEDTLS_PRIVATE(rsm); /*!< ecp_mul_comb()
384 sub-context */
385 mbedtls_ecp_restart_muladd_ctx *MBEDTLS_PRIVATE(ma); /*!< ecp_muladd()
386 sub-context */
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200387} mbedtls_ecp_restart_ctx;
Manuel Pégourié-Gonnardb5a50e72017-04-20 16:06:13 +0200388
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +0200389/*
390 * Operation counts for restartable functions
391 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200392# define MBEDTLS_ECP_OPS_CHK \
393 3 /*!< basic ops count for ecp_check_pubkey() */
394# define MBEDTLS_ECP_OPS_DBL \
395 8 /*!< basic ops count for ecp_double_jac() */
396# define MBEDTLS_ECP_OPS_ADD \
397 11 /*!< basic ops count for see ecp_add_mixed() */
398# define MBEDTLS_ECP_OPS_INV \
399 120 /*!< empirical equivalent for mpi_mod_inv() */
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +0200400
401/**
402 * \brief Internal; for restartable functions in other modules.
403 * Check and update basic ops budget.
404 *
405 * \param grp Group structure
406 * \param rs_ctx Restart context
407 * \param ops Number of basic ops to do
408 *
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200409 * \return \c 0 if doing \p ops basic ops is still allowed,
410 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS otherwise.
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +0200411 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200412int mbedtls_ecp_check_budget(const mbedtls_ecp_group *grp,
413 mbedtls_ecp_restart_ctx *rs_ctx,
414 unsigned ops);
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +0200415
416/* Utility macro for checking and updating ops budget */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200417# define MBEDTLS_ECP_BUDGET(ops) \
418 MBEDTLS_MPI_CHK(mbedtls_ecp_check_budget(grp, rs_ctx, (unsigned)(ops)));
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +0200419
Manuel Pégourié-Gonnardb5a50e72017-04-20 16:06:13 +0200420#else /* MBEDTLS_ECP_RESTARTABLE */
421
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200422# define MBEDTLS_ECP_BUDGET(ops) /* no-op; for compatibility */
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +0200423
Manuel Pégourié-Gonnardb5a50e72017-04-20 16:06:13 +0200424/* We want to declare restartable versions of existing functions anyway */
425typedef void mbedtls_ecp_restart_ctx;
426
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200427#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200428
Paul Bakker088c5c52014-04-25 11:11:10 +0200429/**
Jaeden Amero89453432018-04-24 17:15:38 +0100430 * \brief The ECP key-pair structure.
Ron Eldor05d0e512018-04-16 17:40:04 +0300431 *
Jaeden Amero89453432018-04-24 17:15:38 +0100432 * A generic key-pair that may be used for ECDSA and fixed ECDH, for example.
Ron Eldor05d0e512018-04-16 17:40:04 +0300433 *
Jaeden Amero89453432018-04-24 17:15:38 +0100434 * \note Members are deliberately in the same order as in the
435 * ::mbedtls_ecdsa_context structure.
Ron Eldor05d0e512018-04-16 17:40:04 +0300436 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200437typedef struct mbedtls_ecp_keypair {
438 mbedtls_ecp_group MBEDTLS_PRIVATE(grp); /*!< Elliptic curve and base point
439 */
440 mbedtls_mpi MBEDTLS_PRIVATE(d); /*!< our secret value */
441 mbedtls_ecp_point MBEDTLS_PRIVATE(Q); /*!< our public value */
442} mbedtls_ecp_keypair;
Ron Eldor05d0e512018-04-16 17:40:04 +0300443
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100444/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100445 * Point formats, from RFC 4492's enum ECPointFormat
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100446 */
Mateusz Starzyk16fec332021-07-22 16:43:35 +0200447/** Uncompressed point format. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200448#define MBEDTLS_ECP_PF_UNCOMPRESSED 0
Mateusz Starzyk16fec332021-07-22 16:43:35 +0200449/** Compressed point format. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200450#define MBEDTLS_ECP_PF_COMPRESSED 1
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100451
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100452/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100453 * Some other constants from RFC 4492
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100454 */
Mateusz Starzyk16fec332021-07-22 16:43:35 +0200455/** The named_curve of ECCurveType. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200456#define MBEDTLS_ECP_TLS_NAMED_CURVE 3
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100457
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200458#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100459/**
460 * \brief Set the maximum number of basic operations done in a row.
461 *
462 * If more operations are needed to complete a computation,
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200463 * #MBEDTLS_ERR_ECP_IN_PROGRESS will be returned by the
Manuel Pégourié-Gonnard8467e682017-04-20 09:47:06 +0200464 * function performing the computation. It is then the
465 * caller's responsibility to either call again with the same
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +0200466 * parameters until it returns 0 or an error code; or to free
Manuel Pégourié-Gonnard8467e682017-04-20 09:47:06 +0200467 * the restart context if the operation is to be aborted.
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100468 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +0200469 * It is strictly required that all input parameters and the
470 * restart context be the same on successive calls for the
471 * same operation, but output parameters need not be the
472 * same; they must not be used until the function finally
473 * returns 0.
474 *
Gilles Peskine1174db52020-02-26 19:52:06 +0100475 * This only applies to functions whose documentation
476 * mentions they may return #MBEDTLS_ERR_ECP_IN_PROGRESS (or
477 * #MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS for functions in the
478 * SSL module). For functions that accept a "restart context"
479 * argument, passing NULL disables restart and makes the
480 * function equivalent to the function with the same name
Manuel Pégourié-Gonnard32df9112018-10-15 13:29:21 +0200481 * with \c _restartable removed. For functions in the ECDH
Gilles Peskine1174db52020-02-26 19:52:06 +0100482 * module, restart is disabled unless the function accepts
483 * an "ECDH context" argument and
484 * mbedtls_ecdh_enable_restart() was previously called on
485 * that context. For function in the SSL module, restart is
486 * only enabled for specific sides and key exchanges
487 * (currently only for clients and ECDHE-ECDSA).
Manuel Pégourié-Gonnard8f28add2017-04-19 10:20:49 +0200488 *
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100489 * \param max_ops Maximum number of basic operations done in a row.
490 * Default: 0 (unlimited).
491 * Lower (non-zero) values mean ECC functions will block for
492 * a lesser maximum amount of time.
493 *
Manuel Pégourié-Gonnarde6854492017-03-20 14:35:19 +0100494 * \note A "basic operation" is defined as a rough equivalent of a
495 * multiplication in GF(p) for the NIST P-256 curve.
496 * As an indication, with default settings, a scalar
497 * multiplication (full run of \c mbedtls_ecp_mul()) is:
498 * - about 3300 basic operations for P-256
499 * - about 9400 basic operations for P-384
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100500 *
Manuel Pégourié-Gonnarde58f65a2017-03-20 14:59:54 +0100501 * \note Very low values are not always respected: sometimes
Manuel Pégourié-Gonnard1c678e02017-03-20 13:39:39 +0100502 * functions need to block for a minimum number of
503 * operations, and will do so even if max_ops is set to a
504 * lower value. That minimum depends on the curve size, and
505 * can be made lower by decreasing the value of
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +0200506 * \c MBEDTLS_ECP_WINDOW_SIZE. As an indication, here is the
507 * lowest effective value for various curves and values of
508 * that parameter (w for short):
509 * w=6 w=5 w=4 w=3 w=2
510 * P-256 208 208 160 136 124
511 * P-384 682 416 320 272 248
512 * P-521 1364 832 640 544 496
Manuel Pégourié-Gonnarde58f65a2017-03-20 14:59:54 +0100513 *
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200514 * \note This setting is currently ignored by Curve25519.
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100515 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200516void mbedtls_ecp_set_max_ops(unsigned max_ops);
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200517
518/**
519 * \brief Check if restart is enabled (max_ops != 0)
520 *
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200521 * \return \c 0 if \c max_ops == 0 (restart disabled)
522 * \return \c 1 otherwise (restart enabled)
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200523 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200524int mbedtls_ecp_restart_is_enabled(void);
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200525#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100526
Janos Follathdf9295b2019-02-26 12:36:52 +0000527/*
528 * Get the type of a curve
529 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200530mbedtls_ecp_curve_type mbedtls_ecp_get_type(const mbedtls_ecp_group *grp);
Janos Follathdf9295b2019-02-26 12:36:52 +0000531
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100532/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100533 * \brief This function retrieves the information defined in
Gilles Peskine377c91e2021-06-02 14:37:57 +0200534 * mbedtls_ecp_curve_info() for all supported curves.
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200535 *
Gilles Peskined2085f52019-02-11 18:43:55 +0100536 * \note This function returns information about all curves
537 * supported by the library. Some curves may not be
538 * supported for all algorithms. Call mbedtls_ecdh_can_do()
539 * or mbedtls_ecdsa_can_do() to check if a curve is
540 * supported for ECDH or ECDSA.
541 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100542 * \return A statically allocated array. The last entry is 0.
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200543 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200544const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list(void);
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200545
546/**
Rose Zadikf56cb342018-04-19 12:49:10 +0100547 * \brief This function retrieves the list of internal group
548 * identifiers of all supported curves in the order of
549 * preference.
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100550 *
Gilles Peskined2085f52019-02-11 18:43:55 +0100551 * \note This function returns information about all curves
552 * supported by the library. Some curves may not be
553 * supported for all algorithms. Call mbedtls_ecdh_can_do()
554 * or mbedtls_ecdsa_can_do() to check if a curve is
555 * supported for ECDH or ECDSA.
556 *
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100557 * \return A statically allocated array,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 * terminated with MBEDTLS_ECP_DP_NONE.
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100559 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200560const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list(void);
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100561
562/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100563 * \brief This function retrieves curve information from an internal
564 * group identifier.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200565 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100566 * \param grp_id An \c MBEDTLS_ECP_DP_XXX value.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200567 *
Rose Zadikf56cb342018-04-19 12:49:10 +0100568 * \return The associated curve information on success.
569 * \return NULL on failure.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200570 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200571const mbedtls_ecp_curve_info *
572mbedtls_ecp_curve_info_from_grp_id(mbedtls_ecp_group_id grp_id);
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200573
574/**
Rose Zadika7a61552018-04-24 13:14:01 +0100575 * \brief This function retrieves curve information from a TLS
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100576 * NamedCurve value.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200577 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100578 * \param tls_id An \c MBEDTLS_ECP_DP_XXX value.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200579 *
Rose Zadikf56cb342018-04-19 12:49:10 +0100580 * \return The associated curve information on success.
581 * \return NULL on failure.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200582 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200583const mbedtls_ecp_curve_info *
584mbedtls_ecp_curve_info_from_tls_id(uint16_t tls_id);
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200585
586/**
Rose Zadika7a61552018-04-24 13:14:01 +0100587 * \brief This function retrieves curve information from a
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100588 * human-readable name.
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100589 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100590 * \param name The human-readable name.
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100591 *
Rose Zadikf56cb342018-04-19 12:49:10 +0100592 * \return The associated curve information on success.
593 * \return NULL on failure.
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100594 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200595const mbedtls_ecp_curve_info *
596mbedtls_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 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200603void 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 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200614void 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 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200621void 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 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200628void 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 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200637void 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 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200646void 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 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200655void 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 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200664void 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 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200678int 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 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200691int 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 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200703int 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 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200714int 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 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200728int 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 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200743int mbedtls_ecp_point_read_string(mbedtls_ecp_point *P,
744 int radix,
745 const char *x,
746 const char *y);
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100747
748/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100749 * \brief This function exports a point into unsigned binary data.
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100750 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100751 * \param grp The group to which the point should belong.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500752 * This must be initialized and have group parameters
753 * set, for example through mbedtls_ecp_group_load().
754 * \param P The point to export. This must be initialized.
755 * \param format The point format. This must be either
756 * #MBEDTLS_ECP_PF_COMPRESSED or #MBEDTLS_ECP_PF_UNCOMPRESSED.
Janos Follath28eb06d2019-02-26 10:53:34 +0000757 * (For groups without these formats, this parameter is
758 * ignored. But it still has to be either of the above
759 * values.)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500760 * \param olen The address at which to store the length of
761 * the output in Bytes. This must not be \c NULL.
762 * \param buf The output buffer. This must be a writable buffer
763 * of length \p buflen Bytes.
764 * \param buflen The length of the output buffer \p buf in Bytes.
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100765 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100766 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500767 * \return #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the output buffer
768 * is too small to hold the point.
Janos Follath28eb06d2019-02-26 10:53:34 +0000769 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the point format
770 * or the export for the given group is not implemented.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500771 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100772 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200773int mbedtls_ecp_point_write_binary(const mbedtls_ecp_group *grp,
774 const mbedtls_ecp_point *P,
775 int format,
776 size_t *olen,
777 unsigned char *buf,
778 size_t buflen);
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100779
780/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100781 * \brief This function imports a point from unsigned binary data.
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100782 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100783 * \note This function does not check that the point actually
Rose Zadika7a61552018-04-24 13:14:01 +0100784 * belongs to the given group, see mbedtls_ecp_check_pubkey()
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100785 * for that.
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100786 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100787 * \param grp The group to which the point should belong.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500788 * This must be initialized and have group parameters
789 * set, for example through mbedtls_ecp_group_load().
790 * \param P The destination context to import the point to.
791 * This must be initialized.
792 * \param buf The input buffer. This must be a readable buffer
793 * of length \p ilen Bytes.
794 * \param ilen The length of the input buffer \p buf in Bytes.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100795 *
796 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500797 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the input is invalid.
Rose Zadikf56cb342018-04-19 12:49:10 +0100798 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
Janos Follath28eb06d2019-02-26 10:53:34 +0000799 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the import for the
800 * given group is not implemented.
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100801 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200802int mbedtls_ecp_point_read_binary(const mbedtls_ecp_group *grp,
803 mbedtls_ecp_point *P,
804 const unsigned char *buf,
805 size_t ilen);
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100806
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100807/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100808 * \brief This function imports a point from a TLS ECPoint record.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200809 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500810 * \note On function return, \p *buf is updated to point immediately
Rose Zadikf56cb342018-04-19 12:49:10 +0100811 * after the ECPoint record.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200812 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500813 * \param grp The ECP group to use.
814 * This must be initialized and have group parameters
815 * set, for example through mbedtls_ecp_group_load().
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100816 * \param pt The destination point.
Rose Zadikf56cb342018-04-19 12:49:10 +0100817 * \param buf The address of the pointer to the start of the input buffer.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100818 * \param len The length of the buffer.
Manuel Pégourié-Gonnard150c4f62014-11-21 09:14:52 +0100819 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100820 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500821 * \return An \c MBEDTLS_ERR_MPI_XXX error code on initialization
822 * failure.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100823 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200824 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200825int mbedtls_ecp_tls_read_point(const mbedtls_ecp_group *grp,
826 mbedtls_ecp_point *pt,
827 const unsigned char **buf,
828 size_t len);
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200829
830/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500831 * \brief This function exports a point as a TLS ECPoint record
832 * defined in RFC 4492, Section 5.4.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200833 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500834 * \param grp The ECP group to use.
835 * This must be initialized and have group parameters
836 * set, for example through mbedtls_ecp_group_load().
837 * \param pt The point to be exported. This must be initialized.
838 * \param format The point format to use. This must be either
839 * #MBEDTLS_ECP_PF_COMPRESSED or #MBEDTLS_ECP_PF_UNCOMPRESSED.
840 * \param olen The address at which to store the length in Bytes
841 * of the data written.
842 * \param buf The target buffer. This must be a writable buffer of
843 * length \p blen Bytes.
844 * \param blen The length of the target buffer \p buf in Bytes.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200845 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100846 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500847 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the input is invalid.
848 * \return #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the target buffer
849 * is too small to hold the exported point.
850 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200851 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200852int mbedtls_ecp_tls_write_point(const mbedtls_ecp_group *grp,
853 const mbedtls_ecp_point *pt,
854 int format,
855 size_t *olen,
856 unsigned char *buf,
857 size_t blen);
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200858
859/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500860 * \brief This function sets up an ECP group context
861 * from a standardized set of domain parameters.
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100862 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100863 * \note The index should be a value of the NamedCurve enum,
Rose Zadika7a61552018-04-24 13:14:01 +0100864 * as defined in <em>RFC-4492: Elliptic Curve Cryptography
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100865 * (ECC) Cipher Suites for Transport Layer Security (TLS)</em>,
866 * usually in the form of an \c MBEDTLS_ECP_DP_XXX macro.
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100867 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500868 * \param grp The group context to setup. This must be initialized.
Rose Zadikf56cb342018-04-19 12:49:10 +0100869 * \param id The identifier of the domain parameter set to load.
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100870 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500871 * \return \c 0 on success.
872 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if \p id doesn't
873 * correspond to a known group.
874 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100875 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200876int mbedtls_ecp_group_load(mbedtls_ecp_group *grp, mbedtls_ecp_group_id id);
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100877
878/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500879 * \brief This function sets up an ECP group context from a TLS
880 * ECParameters record as defined in RFC 4492, Section 5.4.
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100881 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500882 * \note The read pointer \p buf is updated to point right after
883 * the ECParameters record on exit.
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100884 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500885 * \param grp The group context to setup. This must be initialized.
Rose Zadikf56cb342018-04-19 12:49:10 +0100886 * \param buf The address of the pointer to the start of the input buffer.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500887 * \param len The length of the input buffer \c *buf in Bytes.
Manuel Pégourié-Gonnard150c4f62014-11-21 09:14:52 +0100888 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100889 * \return \c 0 on success.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100890 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500891 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the group is not
892 * recognized.
893 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100894 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200895int mbedtls_ecp_tls_read_group(mbedtls_ecp_group *grp,
896 const unsigned char **buf,
897 size_t len);
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100898
899/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500900 * \brief This function extracts an elliptic curve group ID from a
901 * TLS ECParameters record as defined in RFC 4492, Section 5.4.
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100902 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500903 * \note The read pointer \p buf is updated to point right after
904 * the ECParameters record on exit.
905 *
906 * \param grp The address at which to store the group id.
907 * This must not be \c NULL.
908 * \param buf The address of the pointer to the start of the input buffer.
909 * \param len The length of the input buffer \c *buf in Bytes.
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100910 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100911 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500912 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid.
913 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the group is not
914 * recognized.
915 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100916 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200917int mbedtls_ecp_tls_read_group_id(mbedtls_ecp_group_id *grp,
918 const unsigned char **buf,
919 size_t len);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500920/**
921 * \brief This function exports an elliptic curve as a TLS
922 * ECParameters record as defined in RFC 4492, Section 5.4.
923 *
924 * \param grp The ECP group to be exported.
925 * This must be initialized and have group parameters
926 * set, for example through mbedtls_ecp_group_load().
927 * \param olen The address at which to store the number of Bytes written.
928 * This must not be \c NULL.
929 * \param buf The buffer to write to. This must be a writable buffer
930 * of length \p blen Bytes.
931 * \param blen The length of the output buffer \p buf in Bytes.
932 *
933 * \return \c 0 on success.
934 * \return #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the output
935 * buffer is too small to hold the exported group.
936 * \return Another negative error code on other kinds of failure.
937 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200938int mbedtls_ecp_tls_write_group(const mbedtls_ecp_group *grp,
939 size_t *olen,
940 unsigned char *buf,
941 size_t blen);
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100942
943/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500944 * \brief This function performs a scalar multiplication of a point
945 * by an integer: \p R = \p m * \p P.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100946 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100947 * It is not thread-safe to use same group in multiple threads.
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200948 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100949 * \note To prevent timing attacks, this function
950 * executes the exact same sequence of base-field
951 * operations for any valid \p m. It avoids any if-branch or
Manuel Pégourié-Gonnardaa3ed6f2021-06-15 11:29:26 +0200952 * array index depending on the value of \p m. If also uses
953 * \p f_rng to randomize some intermediate results.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100954 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500955 * \param grp The ECP group to use.
956 * This must be initialized and have group parameters
957 * set, for example through mbedtls_ecp_group_load().
958 * \param R The point in which to store the result of the calculation.
959 * This must be initialized.
960 * \param m The integer by which to multiply. This must be initialized.
961 * \param P The point to multiply. This must be initialized.
Manuel Pégourié-Gonnardaa3ed6f2021-06-15 11:29:26 +0200962 * \param f_rng The RNG function. This must not be \c NULL.
963 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c
964 * NULL if \p f_rng doesn't need a context.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100965 *
966 * \return \c 0 on success.
Rose Zadikf56cb342018-04-19 12:49:10 +0100967 * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m is not a valid private
968 * key, or \p P is not a valid public key.
969 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500970 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200971 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200972int mbedtls_ecp_mul(mbedtls_ecp_group *grp,
973 mbedtls_ecp_point *R,
974 const mbedtls_mpi *m,
975 const mbedtls_ecp_point *P,
976 int (*f_rng)(void *, unsigned char *, size_t),
977 void *p_rng);
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200978
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200979/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200980 * \brief This function performs multiplication of a point by
981 * an integer: \p R = \p m * \p P in a restartable way.
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200982 *
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200983 * \see mbedtls_ecp_mul()
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200984 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200985 * \note This function does the same as \c mbedtls_ecp_mul(), but
986 * it can return early and restart according to the limit set
987 * with \c mbedtls_ecp_set_max_ops() to reduce blocking.
988 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500989 * \param grp The ECP group to use.
990 * This must be initialized and have group parameters
991 * set, for example through mbedtls_ecp_group_load().
992 * \param R The point in which to store the result of the calculation.
993 * This must be initialized.
994 * \param m The integer by which to multiply. This must be initialized.
995 * \param P The point to multiply. This must be initialized.
Manuel Pégourié-Gonnardaa3ed6f2021-06-15 11:29:26 +0200996 * \param f_rng The RNG function. This must not be \c NULL.
997 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c
998 * NULL if \p f_rng doesn't need a context.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200999 * \param rs_ctx The restart context (NULL disables restart).
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02001000 *
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +02001001 * \return \c 0 on success.
1002 * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m is not a valid private
1003 * key, or \p P is not a valid public key.
1004 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
1005 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnard8467e682017-04-20 09:47:06 +02001006 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001007 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001008 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001009int mbedtls_ecp_mul_restartable(mbedtls_ecp_group *grp,
1010 mbedtls_ecp_point *R,
1011 const mbedtls_mpi *m,
1012 const mbedtls_ecp_point *P,
1013 int (*f_rng)(void *, unsigned char *, size_t),
1014 void *p_rng,
1015 mbedtls_ecp_restart_ctx *rs_ctx);
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001016
Gilles Peskine9b99a892018-09-14 18:32:19 +02001017#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001018/**
Rose Zadika7a61552018-04-24 13:14:01 +01001019 * \brief This function performs multiplication and addition of two
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001020 * points by integers: \p R = \p m * \p P + \p n * \p Q
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02001021 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001022 * It is not thread-safe to use same group in multiple threads.
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02001023 *
Rose Zadika7a61552018-04-24 13:14:01 +01001024 * \note In contrast to mbedtls_ecp_mul(), this function does not
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001025 * guarantee a constant execution flow and timing.
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02001026 *
Gilles Peskine9b99a892018-09-14 18:32:19 +02001027 * \note This function is only defined for short Weierstrass curves.
1028 * It may not be included in builds without any short
1029 * Weierstrass curve.
1030 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001031 * \param grp The ECP group to use.
1032 * This must be initialized and have group parameters
1033 * set, for example through mbedtls_ecp_group_load().
1034 * \param R The point in which to store the result of the calculation.
1035 * This must be initialized.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001036 * \param m The integer by which to multiply \p P.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001037 * This must be initialized.
1038 * \param P The point to multiply by \p m. This must be initialized.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001039 * \param n The integer by which to multiply \p Q.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001040 * This must be initialized.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001041 * \param Q The point to be multiplied by \p n.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001042 * This must be initialized.
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02001043 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001044 * \return \c 0 on success.
Rose Zadika7a61552018-04-24 13:14:01 +01001045 * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m or \p n are not
1046 * valid private keys, or \p P or \p Q are not valid public
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001047 * keys.
Rose Zadikf56cb342018-04-19 12:49:10 +01001048 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
Gilles Peskine9b99a892018-09-14 18:32:19 +02001049 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if \p grp does not
1050 * designate a short Weierstrass curve.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001051 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02001052 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001053int mbedtls_ecp_muladd(mbedtls_ecp_group *grp,
1054 mbedtls_ecp_point *R,
1055 const mbedtls_mpi *m,
1056 const mbedtls_ecp_point *P,
1057 const mbedtls_mpi *n,
1058 const mbedtls_ecp_point *Q);
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02001059
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02001060/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001061 * \brief This function performs multiplication and addition of two
1062 * points by integers: \p R = \p m * \p P + \p n * \p Q in a
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +02001063 * restartable way.
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02001064 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001065 * \see \c mbedtls_ecp_muladd()
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02001066 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001067 * \note This function works the same as \c mbedtls_ecp_muladd(),
1068 * but it can return early and restart according to the limit
1069 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02001070 *
Gilles Peskine9b99a892018-09-14 18:32:19 +02001071 * \note This function is only defined for short Weierstrass curves.
1072 * It may not be included in builds without any short
1073 * Weierstrass curve.
1074 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001075 * \param grp The ECP group to use.
1076 * This must be initialized and have group parameters
1077 * set, for example through mbedtls_ecp_group_load().
1078 * \param R The point in which to store the result of the calculation.
1079 * This must be initialized.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001080 * \param m The integer by which to multiply \p P.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001081 * This must be initialized.
1082 * \param P The point to multiply by \p m. This must be initialized.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001083 * \param n The integer by which to multiply \p Q.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001084 * This must be initialized.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001085 * \param Q The point to be multiplied by \p n.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001086 * This must be initialized.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +02001087 * \param rs_ctx The restart context (NULL disables restart).
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001088 *
1089 * \return \c 0 on success.
1090 * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m or \p n are not
1091 * valid private keys, or \p P or \p Q are not valid public
1092 * keys.
1093 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
Gilles Peskine9b99a892018-09-14 18:32:19 +02001094 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if \p grp does not
1095 * designate a short Weierstrass curve.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001096 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02001097 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001098 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02001099 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001100int mbedtls_ecp_muladd_restartable(mbedtls_ecp_group *grp,
1101 mbedtls_ecp_point *R,
1102 const mbedtls_mpi *m,
1103 const mbedtls_ecp_point *P,
1104 const mbedtls_mpi *n,
1105 const mbedtls_ecp_point *Q,
1106 mbedtls_ecp_restart_ctx *rs_ctx);
Gilles Peskine9b99a892018-09-14 18:32:19 +02001107#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02001108
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02001109/**
Rose Zadika7a61552018-04-24 13:14:01 +01001110 * \brief This function checks that a point is a valid public key
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001111 * on this curve.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001112 *
Rose Zadika7a61552018-04-24 13:14:01 +01001113 * It only checks that the point is non-zero, has
1114 * valid coordinates and lies on the curve. It does not verify
1115 * that it is indeed a multiple of \p G. This additional
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001116 * check is computationally more expensive, is not required
1117 * by standards, and should not be necessary if the group
Rose Zadika7a61552018-04-24 13:14:01 +01001118 * used has a small cofactor. In particular, it is useless for
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001119 * the NIST groups which all have a cofactor of 1.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001120 *
Rose Zadika7a61552018-04-24 13:14:01 +01001121 * \note This function uses bare components rather than an
1122 * ::mbedtls_ecp_keypair structure, to ease use with other
1123 * structures, such as ::mbedtls_ecdh_context or
Rose Zadikb2e111a2018-04-20 10:13:48 +01001124 * ::mbedtls_ecdsa_context.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001125 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001126 * \param grp The ECP group the point should belong to.
1127 * This must be initialized and have group parameters
1128 * set, for example through mbedtls_ecp_group_load().
1129 * \param pt The point to check. This must be initialized.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001130 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001131 * \return \c 0 if the point is a valid public key.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001132 * \return #MBEDTLS_ERR_ECP_INVALID_KEY if the point is not
1133 * a valid public key for the given curve.
1134 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001135 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001136int mbedtls_ecp_check_pubkey(const mbedtls_ecp_group *grp,
1137 const mbedtls_ecp_point *pt);
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001138
1139/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001140 * \brief This function checks that an \p mbedtls_mpi is a
1141 * valid private key for this curve.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001142 *
Rose Zadika7a61552018-04-24 13:14:01 +01001143 * \note This function uses bare components rather than an
1144 * ::mbedtls_ecp_keypair structure to ease use with other
1145 * structures, such as ::mbedtls_ecdh_context or
Rose Zadikb2e111a2018-04-20 10:13:48 +01001146 * ::mbedtls_ecdsa_context.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001147 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001148 * \param grp The ECP group the private key should belong to.
1149 * This must be initialized and have group parameters
1150 * set, for example through mbedtls_ecp_group_load().
1151 * \param d The integer to check. This must be initialized.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001152 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001153 * \return \c 0 if the point is a valid private key.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001154 * \return #MBEDTLS_ERR_ECP_INVALID_KEY if the point is not a valid
1155 * private key for the given curve.
1156 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001157 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001158int mbedtls_ecp_check_privkey(const mbedtls_ecp_group *grp,
1159 const mbedtls_mpi *d);
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001160
1161/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001162 * \brief This function generates a private key.
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02001163 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001164 * \param grp The ECP group to generate a private key for.
1165 * This must be initialized and have group parameters
1166 * set, for example through mbedtls_ecp_group_load().
1167 * \param d The destination MPI (secret part). This must be initialized.
1168 * \param f_rng The RNG function. This must not be \c NULL.
1169 * \param p_rng The RNG parameter to be passed to \p f_rng. This may be
1170 * \c NULL if \p f_rng doesn't need a context argument.
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02001171 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001172 * \return \c 0 on success.
1173 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
1174 * on failure.
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02001175 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001176int mbedtls_ecp_gen_privkey(const mbedtls_ecp_group *grp,
1177 mbedtls_mpi *d,
1178 int (*f_rng)(void *, unsigned char *, size_t),
1179 void *p_rng);
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02001180
1181/**
Rose Zadika7a61552018-04-24 13:14:01 +01001182 * \brief This function generates a keypair with a configurable base
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001183 * point.
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +02001184 *
Rose Zadika7a61552018-04-24 13:14:01 +01001185 * \note This function uses bare components rather than an
1186 * ::mbedtls_ecp_keypair structure to ease use with other
1187 * structures, such as ::mbedtls_ecdh_context or
Rose Zadikb2e111a2018-04-20 10:13:48 +01001188 * ::mbedtls_ecdsa_context.
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +02001189 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001190 * \param grp The ECP group to generate a key pair for.
1191 * This must be initialized and have group parameters
1192 * set, for example through mbedtls_ecp_group_load().
1193 * \param G The base point to use. This must be initialized
1194 * and belong to \p grp. It replaces the default base
1195 * point \c grp->G used by mbedtls_ecp_gen_keypair().
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001196 * \param d The destination MPI (secret part).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001197 * This must be initialized.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001198 * \param Q The destination point (public part).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001199 * This must be initialized.
1200 * \param f_rng The RNG function. This must not be \c NULL.
1201 * \param p_rng The RNG context to be passed to \p f_rng. This may
1202 * be \c NULL if \p f_rng doesn't need a context argument.
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +02001203 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001204 * \return \c 0 on success.
1205 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
1206 * on failure.
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +02001207 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001208int mbedtls_ecp_gen_keypair_base(mbedtls_ecp_group *grp,
1209 const mbedtls_ecp_point *G,
1210 mbedtls_mpi *d,
1211 mbedtls_ecp_point *Q,
1212 int (*f_rng)(void *, unsigned char *, size_t),
1213 void *p_rng);
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +02001214
1215/**
Rose Zadikf56cb342018-04-19 12:49:10 +01001216 * \brief This function generates an ECP keypair.
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001217 *
Rose Zadika7a61552018-04-24 13:14:01 +01001218 * \note This function uses bare components rather than an
1219 * ::mbedtls_ecp_keypair structure to ease use with other
1220 * structures, such as ::mbedtls_ecdh_context or
Rose Zadikb2e111a2018-04-20 10:13:48 +01001221 * ::mbedtls_ecdsa_context.
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001222 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001223 * \param grp The ECP group to generate a key pair for.
1224 * This must be initialized and have group parameters
1225 * set, for example through mbedtls_ecp_group_load().
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001226 * \param d The destination MPI (secret part).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001227 * This must be initialized.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001228 * \param Q The destination point (public part).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001229 * This must be initialized.
1230 * \param f_rng The RNG function. This must not be \c NULL.
1231 * \param p_rng The RNG context to be passed to \p f_rng. This may
1232 * be \c NULL if \p f_rng doesn't need a context argument.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001233 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001234 * \return \c 0 on success.
1235 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
1236 * on failure.
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001237 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001238int mbedtls_ecp_gen_keypair(mbedtls_ecp_group *grp,
1239 mbedtls_mpi *d,
1240 mbedtls_ecp_point *Q,
1241 int (*f_rng)(void *, unsigned char *, size_t),
1242 void *p_rng);
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001243
1244/**
Rose Zadikf56cb342018-04-19 12:49:10 +01001245 * \brief This function generates an ECP key.
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01001246 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001247 * \param grp_id The ECP group identifier.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001248 * \param key The destination key. This must be initialized.
1249 * \param f_rng The RNG function to use. This must not be \c NULL.
1250 * \param p_rng The RNG context to be passed to \p f_rng. This may
1251 * be \c NULL if \p f_rng doesn't need a context argument.
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01001252 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001253 * \return \c 0 on success.
1254 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
1255 * on failure.
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01001256 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001257int mbedtls_ecp_gen_key(mbedtls_ecp_group_id grp_id,
1258 mbedtls_ecp_keypair *key,
1259 int (*f_rng)(void *, unsigned char *, size_t),
1260 void *p_rng);
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01001261
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01001262/**
Janos Follathb65853c2019-02-25 16:33:28 +00001263 * \brief This function reads an elliptic curve private key.
Janos Follath171a7ef2019-02-15 16:17:45 +00001264 *
1265 * \param grp_id The ECP group identifier.
1266 * \param key The destination key.
Tobias Nießen1e8ca122021-05-10 19:53:15 +02001267 * \param buf The buffer containing the binary representation of the
Janos Follath171a7ef2019-02-15 16:17:45 +00001268 * key. (Big endian integer for Weierstrass curves, byte
1269 * string for Montgomery curves.)
1270 * \param buflen The length of the buffer in bytes.
1271 *
1272 * \return \c 0 on success.
Janos Follathb65853c2019-02-25 16:33:28 +00001273 * \return #MBEDTLS_ERR_ECP_INVALID_KEY error if the key is
1274 * invalid.
1275 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
Janos Follath28eb06d2019-02-26 10:53:34 +00001276 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the operation for
1277 * the group is not implemented.
Janos Follathb65853c2019-02-25 16:33:28 +00001278 * \return Another negative error code on different kinds of failure.
Janos Follath171a7ef2019-02-15 16:17:45 +00001279 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001280int mbedtls_ecp_read_key(mbedtls_ecp_group_id grp_id,
1281 mbedtls_ecp_keypair *key,
1282 const unsigned char *buf,
1283 size_t buflen);
Steven Cooremande8593f2020-06-09 19:55:26 +02001284
1285/**
1286 * \brief This function exports an elliptic curve private key.
1287 *
Steven Cooremande8593f2020-06-09 19:55:26 +02001288 * \param key The private key.
Steven Cooremanc9b7f782020-06-11 17:00:36 +02001289 * \param buf The output buffer for containing the binary representation
1290 * of the key. (Big endian integer for Weierstrass curves, byte
Steven Cooremande8593f2020-06-09 19:55:26 +02001291 * string for Montgomery curves.)
1292 * \param buflen The total length of the buffer in bytes.
1293 *
1294 * \return \c 0 on success.
Steven Cooremanc9b7f782020-06-11 17:00:36 +02001295 * \return #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the \p key
1296 representation is larger than the available space in \p buf.
Steven Cooremande8593f2020-06-09 19:55:26 +02001297 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the operation for
1298 * the group is not implemented.
1299 * \return Another negative error code on different kinds of failure.
1300 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001301int mbedtls_ecp_write_key(mbedtls_ecp_keypair *key,
1302 unsigned char *buf,
1303 size_t buflen);
Steven Cooremande8593f2020-06-09 19:55:26 +02001304
Janos Follath171a7ef2019-02-15 16:17:45 +00001305/**
Rose Zadika7a61552018-04-24 13:14:01 +01001306 * \brief This function checks that the keypair objects
Rose Zadikb2e111a2018-04-20 10:13:48 +01001307 * \p pub and \p prv have the same group and the
1308 * same public point, and that the private key in
1309 * \p prv is consistent with the public key.
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01001310 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001311 * \param pub The keypair structure holding the public key. This
1312 * must be initialized. If it contains a private key, that
1313 * part is ignored.
Rose Zadikb2e111a2018-04-20 10:13:48 +01001314 * \param prv The keypair structure holding the full keypair.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001315 * This must be initialized.
Manuel Pégourié-Gonnardf8c24bf2021-06-15 11:29:26 +02001316 * \param f_rng The RNG function. This must not be \c NULL.
1317 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c
1318 * NULL if \p f_rng doesn't need a context.
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01001319 *
Rose Zadika7a61552018-04-24 13:14:01 +01001320 * \return \c 0 on success, meaning that the keys are valid and match.
Rose Zadikb2e111a2018-04-20 10:13:48 +01001321 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the keys are invalid or do not match.
Rose Zadika7a61552018-04-24 13:14:01 +01001322 * \return An \c MBEDTLS_ERR_ECP_XXX or an \c MBEDTLS_ERR_MPI_XXX
Rose Zadikb2e111a2018-04-20 10:13:48 +01001323 * error code on calculation failure.
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01001324 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001325int mbedtls_ecp_check_pub_priv(const mbedtls_ecp_keypair *pub,
1326 const mbedtls_ecp_keypair *prv,
1327 int (*f_rng)(void *, unsigned char *, size_t),
1328 void *p_rng);
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01001329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330#if defined(MBEDTLS_SELF_TEST)
Janos Follathb0697532016-08-18 12:38:46 +01001331
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01001332/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001333 * \brief The ECP checkup routine.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001334 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001335 * \return \c 0 on success.
1336 * \return \c 1 on failure.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001337 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001338int mbedtls_ecp_self_test(int verbose);
Janos Follathb0697532016-08-18 12:38:46 +01001339
Janos Follath372697b2016-10-28 16:53:11 +01001340#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001341
1342#ifdef __cplusplus
1343}
1344#endif
1345
Paul Bakker9af723c2014-05-01 13:03:14 +02001346#endif /* ecp.h */