blob: e33fc78033e1f74b182ce8b01c069ae722e23b8c [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/*
Rose Zadikd3c9bfc2018-04-17 10:56:55 +010018 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
Bence Szépkútif744bd72020-06-05 13:02:18 +020019 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
20 *
21 * This file is provided under the Apache License 2.0, or the
22 * GNU General Public License v2.0 or later.
23 *
24 * **********
25 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020026 *
27 * Licensed under the Apache License, Version 2.0 (the "License"); you may
28 * not use this file except in compliance with the License.
29 * You may obtain a copy of the License at
30 *
31 * http://www.apache.org/licenses/LICENSE-2.0
32 *
33 * Unless required by applicable law or agreed to in writing, software
34 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
35 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
36 * See the License for the specific language governing permissions and
37 * limitations under the License.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010038 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020039 * **********
40 *
41 * **********
42 * GNU General Public License v2.0 or later:
43 *
44 * This program is free software; you can redistribute it and/or modify
45 * it under the terms of the GNU General Public License as published by
46 * the Free Software Foundation; either version 2 of the License, or
47 * (at your option) any later version.
48 *
49 * This program is distributed in the hope that it will be useful,
50 * but WITHOUT ANY WARRANTY; without even the implied warranty of
51 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
52 * GNU General Public License for more details.
53 *
54 * You should have received a copy of the GNU General Public License along
55 * with this program; if not, write to the Free Software Foundation, Inc.,
56 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
57 *
58 * **********
59 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +010060 * This file is part of Mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010061 */
Rose Zadika7a61552018-04-24 13:14:01 +010062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#ifndef MBEDTLS_ECP_H
64#define MBEDTLS_ECP_H
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010065
Ron Eldor8b0cf2e2018-02-14 16:02:41 +020066#if !defined(MBEDTLS_CONFIG_FILE)
67#include "config.h"
68#else
69#include MBEDTLS_CONFIG_FILE
70#endif
71
Manuel Pégourié-Gonnardbdc96762013-10-03 11:50:39 +020072#include "bignum.h"
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010073
74/*
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +010075 * ECP error codes
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010076 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077#define MBEDTLS_ERR_ECP_BAD_INPUT_DATA -0x4F80 /**< Bad input parameters to function. */
78#define MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL -0x4F00 /**< The buffer is too small to write to. */
Rose Zadikd3534052018-04-23 16:12:42 +010079#define MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE -0x4E80 /**< The requested feature is not available, for example, the requested curve is not supported. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080#define MBEDTLS_ERR_ECP_VERIFY_FAILED -0x4E00 /**< The signature is not valid. */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +020081#define MBEDTLS_ERR_ECP_ALLOC_FAILED -0x4D80 /**< Memory allocation failed. */
Rose Zadikd3c9bfc2018-04-17 10:56:55 +010082#define MBEDTLS_ERR_ECP_RANDOM_FAILED -0x4D00 /**< Generation of random value, such as ephemeral key, failed. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083#define MBEDTLS_ERR_ECP_INVALID_KEY -0x4C80 /**< Invalid private or public key. */
Gilles Peskine5114d3e2018-03-30 07:12:15 +020084#define MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH -0x4C00 /**< The buffer contains a valid signature followed by more data. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030085
86/* MBEDTLS_ERR_ECP_HW_ACCEL_FAILED is deprecated and should not be used. */
Rose Zadikd3c9bfc2018-04-17 10:56:55 +010087#define MBEDTLS_ERR_ECP_HW_ACCEL_FAILED -0x4B80 /**< The ECP hardware accelerator failed. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030088
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +020089#define MBEDTLS_ERR_ECP_IN_PROGRESS -0x4B00 /**< Operation in progress, call again with the same parameters to continue. */
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +010090
Paul Bakker407a0da2013-06-27 14:29:21 +020091#ifdef __cplusplus
92extern "C" {
93#endif
94
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010095/**
Rose Zadikd76ac582018-04-23 06:29:34 +010096 * Domain-parameter identifiers: curve, subgroup, and generator.
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +020097 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +010098 * \note Only curves over prime fields are supported.
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +020099 *
100 * \warning This library does not support validation of arbitrary domain
Rose Zadikf56cb342018-04-19 12:49:10 +0100101 * parameters. Therefore, only standardized domain parameters from trusted
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200102 * sources should be used. See mbedtls_ecp_group_load().
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200103 */
104typedef enum
105{
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100106 MBEDTLS_ECP_DP_NONE = 0, /*!< Curve not defined. */
Rose Zadikb2e111a2018-04-20 10:13:48 +0100107 MBEDTLS_ECP_DP_SECP192R1, /*!< Domain parameters for the 192-bit curve defined by FIPS 186-4 and SEC1. */
108 MBEDTLS_ECP_DP_SECP224R1, /*!< Domain parameters for the 224-bit curve defined by FIPS 186-4 and SEC1. */
109 MBEDTLS_ECP_DP_SECP256R1, /*!< Domain parameters for the 256-bit curve defined by FIPS 186-4 and SEC1. */
110 MBEDTLS_ECP_DP_SECP384R1, /*!< Domain parameters for the 384-bit curve defined by FIPS 186-4 and SEC1. */
111 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 +0100112 MBEDTLS_ECP_DP_BP256R1, /*!< Domain parameters for 256-bit Brainpool curve. */
113 MBEDTLS_ECP_DP_BP384R1, /*!< Domain parameters for 384-bit Brainpool curve. */
114 MBEDTLS_ECP_DP_BP512R1, /*!< Domain parameters for 512-bit Brainpool curve. */
Rose Zadikb2e111a2018-04-20 10:13:48 +0100115 MBEDTLS_ECP_DP_CURVE25519, /*!< Domain parameters for Curve25519. */
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100116 MBEDTLS_ECP_DP_SECP192K1, /*!< Domain parameters for 192-bit "Koblitz" curve. */
117 MBEDTLS_ECP_DP_SECP224K1, /*!< Domain parameters for 224-bit "Koblitz" curve. */
118 MBEDTLS_ECP_DP_SECP256K1, /*!< Domain parameters for 256-bit "Koblitz" curve. */
Jaeden Amerofe0669f2018-04-27 17:43:32 +0100119 MBEDTLS_ECP_DP_CURVE448, /*!< Domain parameters for Curve448. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120} mbedtls_ecp_group_id;
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200121
122/**
Rose Zadikf56cb342018-04-19 12:49:10 +0100123 * The number of supported curves, plus one for #MBEDTLS_ECP_DP_NONE.
Manuel Pégourié-Gonnard66153662013-12-03 14:12:26 +0100124 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100125 * \note Montgomery curves are currently excluded.
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200126 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127#define MBEDTLS_ECP_DP_MAX 12
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200128
129/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100130 * Curve information, for use by other modules.
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200131 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200132typedef struct mbedtls_ecp_curve_info
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200133{
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100134 mbedtls_ecp_group_id grp_id; /*!< An internal identifier. */
135 uint16_t tls_id; /*!< The TLS NamedCurve identifier. */
Rose Zadikb2e111a2018-04-20 10:13:48 +0100136 uint16_t bit_size; /*!< The curve size in bits. */
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100137 const char *name; /*!< A human-friendly name. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138} mbedtls_ecp_curve_info;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200139
140/**
Rose Zadikf56cb342018-04-19 12:49:10 +0100141 * \brief The ECP point structure, in Jacobian coordinates.
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100142 *
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100143 * \note All functions expect and return points satisfying
Rose Zadikf56cb342018-04-19 12:49:10 +0100144 * the following condition: <code>Z == 0</code> or
Rose Zadika7a61552018-04-24 13:14:01 +0100145 * <code>Z == 1</code>. Other values of \p Z are
Rose Zadikf56cb342018-04-19 12:49:10 +0100146 * used only by internal functions.
147 * The point is zero, or "at infinity", if <code>Z == 0</code>.
Rose Zadika7a61552018-04-24 13:14:01 +0100148 * Otherwise, \p X and \p Y are its standard (affine)
Rose Zadikf56cb342018-04-19 12:49:10 +0100149 * coordinates.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100150 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200151typedef struct mbedtls_ecp_point
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100152{
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100153 mbedtls_mpi X; /*!< The X coordinate of the ECP point. */
154 mbedtls_mpi Y; /*!< The Y coordinate of the ECP point. */
155 mbedtls_mpi Z; /*!< The Z coordinate of the ECP point. */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100156}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157mbedtls_ecp_point;
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100158
159#if !defined(MBEDTLS_ECP_ALT)
160/*
161 * default mbed TLS elliptic curve arithmetic implementation
162 *
163 * (in case MBEDTLS_ECP_ALT is defined then the developer has to provide an
164 * alternative implementation for the whole module and it will replace this
165 * one.)
166 */
167
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100168/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100169 * \brief The ECP group structure.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100170 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100171 * We consider two types of curve equations:
Rose Zadikb2e111a2018-04-20 10:13:48 +0100172 * <ul><li>Short Weierstrass: <code>y^2 = x^3 + A x + B mod P</code>
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100173 * (SEC1 + RFC-4492)</li>
Rose Zadikf56cb342018-04-19 12:49:10 +0100174 * <li>Montgomery: <code>y^2 = x^3 + A x^2 + x mod P</code> (Curve25519,
Rose Zadikb2e111a2018-04-20 10:13:48 +0100175 * Curve448)</li></ul>
Rose Zadikf56cb342018-04-19 12:49:10 +0100176 * In both cases, the generator (\p G) for a prime-order subgroup is fixed.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100177 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100178 * For Short Weierstrass, this subgroup is the whole curve, and its
Rose Zadikf56cb342018-04-19 12:49:10 +0100179 * cardinality is denoted by \p N. Our code requires that \p N is an
Rose Zadikb2e111a2018-04-20 10:13:48 +0100180 * odd prime as mbedtls_ecp_mul() requires an odd number, and
181 * mbedtls_ecdsa_sign() requires that it is prime for blinding purposes.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100182 *
Rose Zadikf56cb342018-04-19 12:49:10 +0100183 * For Montgomery curves, we do not store \p A, but <code>(A + 2) / 4</code>,
Rose Zadika7a61552018-04-24 13:14:01 +0100184 * which is the quantity used in the formulas. Additionally, \p nbits is
Rose Zadikf56cb342018-04-19 12:49:10 +0100185 * not the size of \p N but the required size for private keys.
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100186 *
Rose Zadika7a61552018-04-24 13:14:01 +0100187 * If \p modp is NULL, reduction modulo \p P is done using a generic algorithm.
188 * Otherwise, \p modp must point to a function that takes an \p mbedtls_mpi in the
189 * range of <code>0..2^(2*pbits)-1</code>, and transforms it in-place to an integer
190 * which is congruent mod \p P to the given MPI, and is close enough to \p pbits
191 * in size, so that it may be efficiently brought in the 0..P-1 range by a few
192 * additions or subtractions. Therefore, it is only an approximative modular
193 * reduction. It must return 0 on success and non-zero on failure.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100194 *
Janos Follathc3b680b2018-12-05 16:01:13 +0000195 * \note Alternative implementations must keep the group IDs distinct. If
196 * two group structures have the same ID, then they must be
197 * identical.
198 *
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100199 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200200typedef struct mbedtls_ecp_group
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100201{
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100202 mbedtls_ecp_group_id id; /*!< An internal group identifier. */
Rose Zadikf56cb342018-04-19 12:49:10 +0100203 mbedtls_mpi P; /*!< The prime modulus of the base field. */
Rose Zadikb2e111a2018-04-20 10:13:48 +0100204 mbedtls_mpi A; /*!< For Short Weierstrass: \p A in the equation. For
205 Montgomery curves: <code>(A + 2) / 4</code>. */
206 mbedtls_mpi B; /*!< For Short Weierstrass: \p B in the equation.
207 For Montgomery curves: unused. */
Rose Zadikf56cb342018-04-19 12:49:10 +0100208 mbedtls_ecp_point G; /*!< The generator of the subgroup used. */
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100209 mbedtls_mpi N; /*!< The order of \p G. */
210 size_t pbits; /*!< The number of bits in \p P.*/
Rose Zadikb2e111a2018-04-20 10:13:48 +0100211 size_t nbits; /*!< For Short Weierstrass: The number of bits in \p P.
Rose Zadika7a61552018-04-24 13:14:01 +0100212 For Montgomery curves: the number of bits in the
Rose Zadikd76ac582018-04-23 06:29:34 +0100213 private keys. */
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100214 unsigned int h; /*!< \internal 1 if the constants are static. */
Rose Zadika7a61552018-04-24 13:14:01 +0100215 int (*modp)(mbedtls_mpi *); /*!< The function for fast pseudo-reduction
Rose Zadikf56cb342018-04-19 12:49:10 +0100216 mod \p P (see above).*/
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100217 int (*t_pre)(mbedtls_ecp_point *, void *); /*!< Unused. */
218 int (*t_post)(mbedtls_ecp_point *, void *); /*!< Unused. */
219 void *t_data; /*!< Unused. */
220 mbedtls_ecp_point *T; /*!< Pre-computed points for ecp_mul_comb(). */
Rose Zadikb2e111a2018-04-20 10:13:48 +0100221 size_t T_size; /*!< The number of pre-computed points. */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100222}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223mbedtls_ecp_group;
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100224
Darryl Greenb7797592019-01-04 16:18:06 +0000225/**
226 * \name SECTION: Module settings
227 *
228 * The configuration options you can set for this module are in this section.
229 * Either change them in config.h, or define them using the compiler command line.
230 * \{
231 */
232
233#if !defined(MBEDTLS_ECP_MAX_BITS)
234/**
235 * The maximum size of the groups, that is, of \c N and \c P.
236 */
237#define MBEDTLS_ECP_MAX_BITS 521 /**< The maximum size of groups, in bits. */
238#endif
239
240#define MBEDTLS_ECP_MAX_BYTES ( ( MBEDTLS_ECP_MAX_BITS + 7 ) / 8 )
241#define MBEDTLS_ECP_MAX_PT_LEN ( 2 * MBEDTLS_ECP_MAX_BYTES + 1 )
242
243#if !defined(MBEDTLS_ECP_WINDOW_SIZE)
244/*
245 * Maximum "window" size used for point multiplication.
246 * Default: 6.
247 * Minimum value: 2. Maximum value: 7.
248 *
249 * Result is an array of at most ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) )
250 * points used for point multiplication. This value is directly tied to EC
251 * peak memory usage, so decreasing it by one should roughly cut memory usage
252 * by two (if large curves are in use).
253 *
254 * Reduction in size may reduce speed, but larger curves are impacted first.
255 * Sample performances (in ECDHE handshakes/s, with FIXED_POINT_OPTIM = 1):
256 * w-size: 6 5 4 3 2
257 * 521 145 141 135 120 97
258 * 384 214 209 198 177 146
259 * 256 320 320 303 262 226
260 * 224 475 475 453 398 342
261 * 192 640 640 633 587 476
262 */
263#define MBEDTLS_ECP_WINDOW_SIZE 6 /**< The maximum window size used. */
264#endif /* MBEDTLS_ECP_WINDOW_SIZE */
265
266#if !defined(MBEDTLS_ECP_FIXED_POINT_OPTIM)
267/*
268 * Trade memory for speed on fixed-point multiplication.
269 *
270 * This speeds up repeated multiplication of the generator (that is, the
271 * multiplication in ECDSA signatures, and half of the multiplications in
272 * ECDSA verification and ECDHE) by a factor roughly 3 to 4.
273 *
274 * The cost is increasing EC peak memory usage by a factor roughly 2.
275 *
276 * Change this value to 0 to reduce peak memory usage.
277 */
278#define MBEDTLS_ECP_FIXED_POINT_OPTIM 1 /**< Enable fixed-point speed-up. */
279#endif /* MBEDTLS_ECP_FIXED_POINT_OPTIM */
280
281/* \} name SECTION: Module settings */
282
283#else /* MBEDTLS_ECP_ALT */
284#include "ecp_alt.h"
285#endif /* MBEDTLS_ECP_ALT */
286
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200287#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardb5a50e72017-04-20 16:06:13 +0200288
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200289/**
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +0200290 * \brief Internal restart context for multiplication
291 *
292 * \note Opaque struct
293 */
294typedef struct mbedtls_ecp_restart_mul mbedtls_ecp_restart_mul_ctx;
295
296/**
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200297 * \brief Internal restart context for ecp_muladd()
298 *
299 * \note Opaque struct
300 */
301typedef struct mbedtls_ecp_restart_muladd mbedtls_ecp_restart_muladd_ctx;
302
303/**
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200304 * \brief General context for resuming ECC operations
305 */
306typedef struct
307{
Manuel Pégourié-Gonnard646393b2017-04-20 10:03:45 +0200308 unsigned ops_done; /*!< current ops count */
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +0200309 unsigned depth; /*!< call depth (0 = top-level) */
Manuel Pégourié-Gonnard646393b2017-04-20 10:03:45 +0200310 mbedtls_ecp_restart_mul_ctx *rsm; /*!< ecp_mul_comb() sub-context */
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200311 mbedtls_ecp_restart_muladd_ctx *ma; /*!< ecp_muladd() sub-context */
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200312} mbedtls_ecp_restart_ctx;
Manuel Pégourié-Gonnardb5a50e72017-04-20 16:06:13 +0200313
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +0200314/*
315 * Operation counts for restartable functions
316 */
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200317#define MBEDTLS_ECP_OPS_CHK 3 /*!< basic ops count for ecp_check_pubkey() */
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +0200318#define MBEDTLS_ECP_OPS_DBL 8 /*!< basic ops count for ecp_double_jac() */
319#define MBEDTLS_ECP_OPS_ADD 11 /*!< basic ops count for see ecp_add_mixed() */
320#define MBEDTLS_ECP_OPS_INV 120 /*!< empirical equivalent for mpi_mod_inv() */
321
322/**
323 * \brief Internal; for restartable functions in other modules.
324 * Check and update basic ops budget.
325 *
326 * \param grp Group structure
327 * \param rs_ctx Restart context
328 * \param ops Number of basic ops to do
329 *
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200330 * \return \c 0 if doing \p ops basic ops is still allowed,
331 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS otherwise.
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +0200332 */
333int mbedtls_ecp_check_budget( const mbedtls_ecp_group *grp,
334 mbedtls_ecp_restart_ctx *rs_ctx,
335 unsigned ops );
336
337/* Utility macro for checking and updating ops budget */
Hanno Beckerabdf67e2018-10-26 13:28:32 +0100338#define MBEDTLS_ECP_BUDGET( ops ) \
339 MBEDTLS_MPI_CHK( mbedtls_ecp_check_budget( grp, rs_ctx, \
340 (unsigned) (ops) ) );
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +0200341
Manuel Pégourié-Gonnardb5a50e72017-04-20 16:06:13 +0200342#else /* MBEDTLS_ECP_RESTARTABLE */
343
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +0200344#define MBEDTLS_ECP_BUDGET( ops ) /* no-op; for compatibility */
345
Manuel Pégourié-Gonnardb5a50e72017-04-20 16:06:13 +0200346/* We want to declare restartable versions of existing functions anyway */
347typedef void mbedtls_ecp_restart_ctx;
348
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200349#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200350
Paul Bakker088c5c52014-04-25 11:11:10 +0200351/**
Jaeden Amero89453432018-04-24 17:15:38 +0100352 * \brief The ECP key-pair structure.
Ron Eldor05d0e512018-04-16 17:40:04 +0300353 *
Jaeden Amero89453432018-04-24 17:15:38 +0100354 * A generic key-pair that may be used for ECDSA and fixed ECDH, for example.
Ron Eldor05d0e512018-04-16 17:40:04 +0300355 *
Jaeden Amero89453432018-04-24 17:15:38 +0100356 * \note Members are deliberately in the same order as in the
357 * ::mbedtls_ecdsa_context structure.
Ron Eldor05d0e512018-04-16 17:40:04 +0300358 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200359typedef struct mbedtls_ecp_keypair
Ron Eldor05d0e512018-04-16 17:40:04 +0300360{
361 mbedtls_ecp_group grp; /*!< Elliptic curve and base point */
362 mbedtls_mpi d; /*!< our secret value */
363 mbedtls_ecp_point Q; /*!< our public value */
364}
365mbedtls_ecp_keypair;
366
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100367/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100368 * Point formats, from RFC 4492's enum ECPointFormat
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100369 */
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100370#define MBEDTLS_ECP_PF_UNCOMPRESSED 0 /**< Uncompressed point format. */
371#define MBEDTLS_ECP_PF_COMPRESSED 1 /**< Compressed point format. */
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100372
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100373/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100374 * Some other constants from RFC 4492
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100375 */
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100376#define MBEDTLS_ECP_TLS_NAMED_CURVE 3 /**< The named_curve of ECCurveType. */
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100377
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200378#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100379/**
380 * \brief Set the maximum number of basic operations done in a row.
381 *
382 * If more operations are needed to complete a computation,
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200383 * #MBEDTLS_ERR_ECP_IN_PROGRESS will be returned by the
Manuel Pégourié-Gonnard8467e682017-04-20 09:47:06 +0200384 * function performing the computation. It is then the
385 * caller's responsibility to either call again with the same
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +0200386 * parameters until it returns 0 or an error code; or to free
Manuel Pégourié-Gonnard8467e682017-04-20 09:47:06 +0200387 * the restart context if the operation is to be aborted.
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100388 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +0200389 * It is strictly required that all input parameters and the
390 * restart context be the same on successive calls for the
391 * same operation, but output parameters need not be the
392 * same; they must not be used until the function finally
393 * returns 0.
394 *
Manuel Pégourié-Gonnard32df9112018-10-15 13:29:21 +0200395 * This only applies to functions whose documentation
396 * mentions they may return #MBEDTLS_ERR_ECP_IN_PROGRESS (or
397 * #MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS for functions in the
398 * SSL module). For functions that accept a "restart context"
399 * argument, passing NULL disables restart and makes the
400 * function equivalent to the function with the same name
401 * with \c _restartable removed. For functions in the ECDH
402 * module, restart is disabled unless the function accepts
403 * an "ECDH context" argument and
404 * mbedtls_ecdh_enable_restart() was previously called on
405 * that context. For function in the SSL module, restart is
406 * only enabled for specific sides and key exchanges
407 * (currently only for clients and ECDHE-ECDSA).
Manuel Pégourié-Gonnard8f28add2017-04-19 10:20:49 +0200408 *
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100409 * \param max_ops Maximum number of basic operations done in a row.
410 * Default: 0 (unlimited).
411 * Lower (non-zero) values mean ECC functions will block for
412 * a lesser maximum amount of time.
413 *
Manuel Pégourié-Gonnarde6854492017-03-20 14:35:19 +0100414 * \note A "basic operation" is defined as a rough equivalent of a
415 * multiplication in GF(p) for the NIST P-256 curve.
416 * As an indication, with default settings, a scalar
417 * multiplication (full run of \c mbedtls_ecp_mul()) is:
418 * - about 3300 basic operations for P-256
419 * - about 9400 basic operations for P-384
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100420 *
Manuel Pégourié-Gonnarde58f65a2017-03-20 14:59:54 +0100421 * \note Very low values are not always respected: sometimes
Manuel Pégourié-Gonnard1c678e02017-03-20 13:39:39 +0100422 * functions need to block for a minimum number of
423 * operations, and will do so even if max_ops is set to a
424 * lower value. That minimum depends on the curve size, and
425 * can be made lower by decreasing the value of
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +0200426 * \c MBEDTLS_ECP_WINDOW_SIZE. As an indication, here is the
427 * lowest effective value for various curves and values of
428 * that parameter (w for short):
429 * w=6 w=5 w=4 w=3 w=2
430 * P-256 208 208 160 136 124
431 * P-384 682 416 320 272 248
432 * P-521 1364 832 640 544 496
Manuel Pégourié-Gonnarde58f65a2017-03-20 14:59:54 +0100433 *
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200434 * \note This setting is currently ignored by Curve25519.
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100435 */
436void mbedtls_ecp_set_max_ops( unsigned max_ops );
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200437
438/**
439 * \brief Check if restart is enabled (max_ops != 0)
440 *
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200441 * \return \c 0 if \c max_ops == 0 (restart disabled)
442 * \return \c 1 otherwise (restart enabled)
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200443 */
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200444int mbedtls_ecp_restart_is_enabled( void );
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200445#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100446
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100447/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100448 * \brief This function retrieves the information defined in
Rose Zadikf56cb342018-04-19 12:49:10 +0100449 * mbedtls_ecp_curve_info() for all supported curves in order
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100450 * of preference.
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200451 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100452 * \return A statically allocated array. The last entry is 0.
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200453 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list( void );
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200455
456/**
Rose Zadikf56cb342018-04-19 12:49:10 +0100457 * \brief This function retrieves the list of internal group
458 * identifiers of all supported curves in the order of
459 * preference.
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100460 *
461 * \return A statically allocated array,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 * terminated with MBEDTLS_ECP_DP_NONE.
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100463 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list( void );
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100465
466/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100467 * \brief This function retrieves curve information from an internal
468 * group identifier.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200469 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100470 * \param grp_id An \c MBEDTLS_ECP_DP_XXX value.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200471 *
Rose Zadikf56cb342018-04-19 12:49:10 +0100472 * \return The associated curve information on success.
473 * \return NULL on failure.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200474 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475const 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 +0200476
477/**
Rose Zadika7a61552018-04-24 13:14:01 +0100478 * \brief This function retrieves curve information from a TLS
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100479 * NamedCurve value.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200480 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100481 * \param tls_id An \c MBEDTLS_ECP_DP_XXX value.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200482 *
Rose Zadikf56cb342018-04-19 12:49:10 +0100483 * \return The associated curve information on success.
484 * \return NULL on failure.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200485 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id( uint16_t tls_id );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200487
488/**
Rose Zadika7a61552018-04-24 13:14:01 +0100489 * \brief This function retrieves curve information from a
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100490 * human-readable name.
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100491 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100492 * \param name The human-readable name.
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100493 *
Rose Zadikf56cb342018-04-19 12:49:10 +0100494 * \return The associated curve information on success.
495 * \return NULL on failure.
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100496 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name( const char *name );
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100498
499/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100500 * \brief This function initializes a point as zero.
501 *
502 * \param pt The point to initialize.
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100503 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504void mbedtls_ecp_point_init( mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100505
506/**
Rose Zadikf56cb342018-04-19 12:49:10 +0100507 * \brief This function initializes an ECP group context
Rose Zadika7a61552018-04-24 13:14:01 +0100508 * without loading any domain parameters.
Rose Zadikf56cb342018-04-19 12:49:10 +0100509 *
510 * \note After this function is called, domain parameters
511 * for various ECP groups can be loaded through the
Gilles Peskine4dc50bc2018-11-07 22:06:48 +0100512 * mbedtls_ecp_group_load() or mbedtls_ecp_tls_read_group()
Rose Zadikf56cb342018-04-19 12:49:10 +0100513 * functions.
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100514 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515void mbedtls_ecp_group_init( mbedtls_ecp_group *grp );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100516
517/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100518 * \brief This function initializes a key pair as an invalid one.
519 *
520 * \param key The key pair to initialize.
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200521 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522void mbedtls_ecp_keypair_init( mbedtls_ecp_keypair *key );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200523
524/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100525 * \brief This function frees the components of a point.
526 *
527 * \param pt The point to free.
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100528 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529void mbedtls_ecp_point_free( mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100530
531/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100532 * \brief This function frees the components of an ECP group.
Hanno Beckerebffa792018-12-14 15:07:50 +0000533 *
534 * \param grp The group to free. This may be \c NULL, in which
Hanno Becker486f1b32018-12-18 13:00:34 +0000535 * case this function returns immediately. If it is not
536 * \c NULL, it must point to an initialized ECP group.
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100537 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538void mbedtls_ecp_group_free( mbedtls_ecp_group *grp );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100539
540/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100541 * \brief This function frees the components of a key pair.
Hanno Beckerebffa792018-12-14 15:07:50 +0000542 *
543 * \param key The key pair to free. This may be \c NULL, in which
Hanno Becker486f1b32018-12-18 13:00:34 +0000544 * case this function returns immediately. If it is not
545 * \c NULL, it must point to an initialized ECP key pair.
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200546 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair *key );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200548
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200549#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200550/**
Hanno Beckerebffa792018-12-14 15:07:50 +0000551 * \brief Initialize a restart context.
552 *
553 * \param ctx The restart context to initialize. This must
554 * not be \c NULL.
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200555 */
556void mbedtls_ecp_restart_init( mbedtls_ecp_restart_ctx *ctx );
557
558/**
Hanno Beckerebffa792018-12-14 15:07:50 +0000559 * \brief Free the components of a restart context.
560 *
561 * \param ctx The restart context to free. This may be \c NULL, in which
Hanno Becker486f1b32018-12-18 13:00:34 +0000562 * case this function returns immediately. If it is not
563 * \c NULL, it must point to an initialized restart context.
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200564 */
565void mbedtls_ecp_restart_free( mbedtls_ecp_restart_ctx *ctx );
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200566#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200567
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200568/**
Rose Zadika7a61552018-04-24 13:14:01 +0100569 * \brief This function copies the contents of point \p Q into
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100570 * point \p P.
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100571 *
Hanno Beckerebffa792018-12-14 15:07:50 +0000572 * \param P The destination point. This must be initialized.
573 * \param Q The source point. This must be initialized.
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100574 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100575 * \return \c 0 on success.
Rose Zadikf56cb342018-04-19 12:49:10 +0100576 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
Hanno Beckerebffa792018-12-14 15:07:50 +0000577 * \return Another negative error code for other kinds of failure.
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100578 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579int mbedtls_ecp_copy( mbedtls_ecp_point *P, const mbedtls_ecp_point *Q );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100580
581/**
Rose Zadika7a61552018-04-24 13:14:01 +0100582 * \brief This function copies the contents of group \p src into
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100583 * group \p dst.
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200584 *
Hanno Beckerebffa792018-12-14 15:07:50 +0000585 * \param dst The destination group. This must be initialized.
586 * \param src The source group. This must be initialized.
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200587 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100588 * \return \c 0 on success.
Rose Zadikf56cb342018-04-19 12:49:10 +0100589 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
Hanno Beckerebffa792018-12-14 15:07:50 +0000590 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200591 */
Hanno Beckerebffa792018-12-14 15:07:50 +0000592int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst,
593 const mbedtls_ecp_group *src );
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200594
595/**
Hanno Beckerebffa792018-12-14 15:07:50 +0000596 * \brief This function sets a point to the point at infinity.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200597 *
Hanno Beckerebffa792018-12-14 15:07:50 +0000598 * \param pt The point to set. This must be initialized.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200599 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100600 * \return \c 0 on success.
Rose Zadikf56cb342018-04-19 12:49:10 +0100601 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
Hanno Beckerebffa792018-12-14 15:07:50 +0000602 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200603 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604int mbedtls_ecp_set_zero( mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200605
606/**
Hanno Beckerebffa792018-12-14 15:07:50 +0000607 * \brief This function checks if a point is the point at infinity.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200608 *
Hanno Beckerebffa792018-12-14 15:07:50 +0000609 * \param pt The point to test. This must be initialized.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200610 *
Rose Zadikf56cb342018-04-19 12:49:10 +0100611 * \return \c 1 if the point is zero.
612 * \return \c 0 if the point is non-zero.
Hanno Beckerebffa792018-12-14 15:07:50 +0000613 * \return A negative error code on failure.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200614 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200616
617/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100618 * \brief This function compares two points.
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200619 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100620 * \note This assumes that the points are normalized. Otherwise,
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200621 * they may compare as "not equal" even if they are.
622 *
Hanno Beckerebffa792018-12-14 15:07:50 +0000623 * \param P The first point to compare. This must be initialized.
624 * \param Q The second point to compare. This must be initialized.
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200625 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100626 * \return \c 0 if the points are equal.
627 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the points are not equal.
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200628 */
629int mbedtls_ecp_point_cmp( const mbedtls_ecp_point *P,
630 const mbedtls_ecp_point *Q );
631
632/**
Rose Zadika7a61552018-04-24 13:14:01 +0100633 * \brief This function imports a non-zero point from two ASCII
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100634 * strings.
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100635 *
Hanno Beckerebffa792018-12-14 15:07:50 +0000636 * \param P The destination point. This must be initialized.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100637 * \param radix The numeric base of the input.
638 * \param x The first affine coordinate, as a null-terminated string.
639 * \param y The second affine coordinate, as a null-terminated string.
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100640 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100641 * \return \c 0 on success.
642 * \return An \c MBEDTLS_ERR_MPI_XXX error code on failure.
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100643 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644int mbedtls_ecp_point_read_string( mbedtls_ecp_point *P, int radix,
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100645 const char *x, const char *y );
646
647/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100648 * \brief This function exports a point into unsigned binary data.
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100649 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100650 * \param grp The group to which the point should belong.
Hanno Beckerebffa792018-12-14 15:07:50 +0000651 * This must be initialized and have group parameters
652 * set, for example through mbedtls_ecp_group_load().
653 * \param P The point to export. This must be initialized.
654 * \param format The point format. This must be either
655 * #MBEDTLS_ECP_PF_COMPRESSED or #MBEDTLS_ECP_PF_UNCOMPRESSED.
656 * \param olen The address at which to store the length of
Hanno Becker5edcfa52018-12-18 12:49:55 +0000657 * the output in Bytes. This must not be \c NULL.
Hanno Beckerebffa792018-12-14 15:07:50 +0000658 * \param buf The output buffer. This must be a writable buffer
659 * of length \p buflen Bytes.
660 * \param buflen The length of the output buffer \p buf in Bytes.
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100661 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100662 * \return \c 0 on success.
Hanno Beckerebffa792018-12-14 15:07:50 +0000663 * \return #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the output buffer
664 * is too small to hold the point.
665 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100666 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100668 int format, size_t *olen,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100669 unsigned char *buf, size_t buflen );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100670
671/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100672 * \brief This function imports a point from unsigned binary data.
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100673 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100674 * \note This function does not check that the point actually
Rose Zadika7a61552018-04-24 13:14:01 +0100675 * belongs to the given group, see mbedtls_ecp_check_pubkey()
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100676 * for that.
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100677 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100678 * \param grp The group to which the point should belong.
Hanno Beckerebffa792018-12-14 15:07:50 +0000679 * This must be initialized and have group parameters
680 * set, for example through mbedtls_ecp_group_load().
681 * \param P The destination context to import the point to.
682 * This must be initialized.
683 * \param buf The input buffer. This must be a readable buffer
684 * of length \p ilen Bytes.
685 * \param ilen The length of the input buffer \p buf in Bytes.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100686 *
687 * \return \c 0 on success.
Hanno Beckerebffa792018-12-14 15:07:50 +0000688 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the input is invalid.
Rose Zadikf56cb342018-04-19 12:49:10 +0100689 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100690 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the point format
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100691 * is not implemented.
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100692 */
Hanno Beckerebffa792018-12-14 15:07:50 +0000693int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp,
694 mbedtls_ecp_point *P,
695 const unsigned char *buf, size_t ilen );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100696
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100697/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100698 * \brief This function imports a point from a TLS ECPoint record.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200699 *
Janos Follath89ac8c92018-10-30 11:24:05 +0000700 * \note On function return, \p *buf is updated to point immediately
Rose Zadikf56cb342018-04-19 12:49:10 +0100701 * after the ECPoint record.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200702 *
Hanno Beckerebffa792018-12-14 15:07:50 +0000703 * \param grp The ECP group to use.
704 * This must be initialized and have group parameters
705 * set, for example through mbedtls_ecp_group_load().
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100706 * \param pt The destination point.
Rose Zadikf56cb342018-04-19 12:49:10 +0100707 * \param buf The address of the pointer to the start of the input buffer.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100708 * \param len The length of the buffer.
Manuel Pégourié-Gonnard150c4f62014-11-21 09:14:52 +0100709 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100710 * \return \c 0 on success.
Janos Follath89ac8c92018-10-30 11:24:05 +0000711 * \return An \c MBEDTLS_ERR_MPI_XXX error code on initialization
712 * failure.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100713 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200714 */
Hanno Beckerebffa792018-12-14 15:07:50 +0000715int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp,
716 mbedtls_ecp_point *pt,
717 const unsigned char **buf, size_t len );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200718
719/**
Hanno Beckerebffa792018-12-14 15:07:50 +0000720 * \brief This function exports a point as a TLS ECPoint record
721 * defined in RFC 4492, Section 5.4.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200722 *
Hanno Beckerebffa792018-12-14 15:07:50 +0000723 * \param grp The ECP group to use.
724 * This must be initialized and have group parameters
725 * set, for example through mbedtls_ecp_group_load().
726 * \param pt The point to be exported. This must be initialized.
727 * \param format The point format to use. This must be either
728 * #MBEDTLS_ECP_PF_COMPRESSED or #MBEDTLS_ECP_PF_UNCOMPRESSED.
729 * \param olen The address at which to store the length in Bytes
730 * of the data written.
731 * \param buf The target buffer. This must be a writable buffer of
732 * length \p blen Bytes.
733 * \param blen The length of the target buffer \p buf in Bytes.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200734 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100735 * \return \c 0 on success.
Hanno Beckerebffa792018-12-14 15:07:50 +0000736 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the input is invalid.
737 * \return #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the target buffer
738 * is too small to hold the exported point.
739 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200740 */
Hanno Beckerebffa792018-12-14 15:07:50 +0000741int mbedtls_ecp_tls_write_point( const mbedtls_ecp_group *grp,
742 const mbedtls_ecp_point *pt,
743 int format, size_t *olen,
744 unsigned char *buf, size_t blen );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200745
746/**
Hanno Beckerebffa792018-12-14 15:07:50 +0000747 * \brief This function sets up an ECP group context
748 * from a standardized set of domain parameters.
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100749 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100750 * \note The index should be a value of the NamedCurve enum,
Rose Zadika7a61552018-04-24 13:14:01 +0100751 * as defined in <em>RFC-4492: Elliptic Curve Cryptography
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100752 * (ECC) Cipher Suites for Transport Layer Security (TLS)</em>,
753 * usually in the form of an \c MBEDTLS_ECP_DP_XXX macro.
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100754 *
Hanno Beckerebffa792018-12-14 15:07:50 +0000755 * \param grp The group context to setup. This must be initialized.
Rose Zadikf56cb342018-04-19 12:49:10 +0100756 * \param id The identifier of the domain parameter set to load.
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100757 *
Hanno Beckerebffa792018-12-14 15:07:50 +0000758 * \return \c 0 on success.
759 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if \p id doesn't
760 * correspond to a known group.
761 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100762 */
Hanno Becker61937d42017-04-26 15:01:23 +0100763int mbedtls_ecp_group_load( mbedtls_ecp_group *grp, mbedtls_ecp_group_id id );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100764
765/**
Hanno Beckerebffa792018-12-14 15:07:50 +0000766 * \brief This function sets up an ECP group context from a TLS
767 * ECParameters record as defined in RFC 4492, Section 5.4.
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100768 *
Hanno Beckerebffa792018-12-14 15:07:50 +0000769 * \note The read pointer \p buf is updated to point right after
770 * the ECParameters record on exit.
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100771 *
Hanno Beckerebffa792018-12-14 15:07:50 +0000772 * \param grp The group context to setup. This must be initialized.
Rose Zadikf56cb342018-04-19 12:49:10 +0100773 * \param buf The address of the pointer to the start of the input buffer.
Hanno Beckerebffa792018-12-14 15:07:50 +0000774 * \param len The length of the input buffer \c *buf in Bytes.
Manuel Pégourié-Gonnard150c4f62014-11-21 09:14:52 +0100775 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100776 * \return \c 0 on success.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100777 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid.
Janos Follath89ac8c92018-10-30 11:24:05 +0000778 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the group is not
Hanno Beckerebffa792018-12-14 15:07:50 +0000779 * recognized.
780 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100781 */
Hanno Beckerebffa792018-12-14 15:07:50 +0000782int mbedtls_ecp_tls_read_group( mbedtls_ecp_group *grp,
783 const unsigned char **buf, size_t len );
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100784
785/**
Hanno Beckerebffa792018-12-14 15:07:50 +0000786 * \brief This function extracts an elliptic curve group ID from a
787 * TLS ECParameters record as defined in RFC 4492, Section 5.4.
Janos Follath89ac8c92018-10-30 11:24:05 +0000788 *
Hanno Beckerebffa792018-12-14 15:07:50 +0000789 * \note The read pointer \p buf is updated to point right after
790 * the ECParameters record on exit.
Janos Follath89ac8c92018-10-30 11:24:05 +0000791 *
Hanno Beckerebffa792018-12-14 15:07:50 +0000792 * \param grp The address at which to store the group id.
793 * This must not be \c NULL.
Janos Follath89ac8c92018-10-30 11:24:05 +0000794 * \param buf The address of the pointer to the start of the input buffer.
Hanno Beckerebffa792018-12-14 15:07:50 +0000795 * \param len The length of the input buffer \c *buf in Bytes.
Janos Follath89ac8c92018-10-30 11:24:05 +0000796 *
797 * \return \c 0 on success.
798 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid.
799 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the group is not
Hanno Beckerebffa792018-12-14 15:07:50 +0000800 * recognized.
801 * \return Another negative error code on other kinds of failure.
Janos Follath89ac8c92018-10-30 11:24:05 +0000802 */
803int mbedtls_ecp_tls_read_group_id( mbedtls_ecp_group_id *grp,
Hanno Beckerebffa792018-12-14 15:07:50 +0000804 const unsigned char **buf,
805 size_t len );
Janos Follath89ac8c92018-10-30 11:24:05 +0000806/**
Hanno Beckerebffa792018-12-14 15:07:50 +0000807 * \brief This function exports an elliptic curve as a TLS
808 * ECParameters record as defined in RFC 4492, Section 5.4.
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100809 *
Hanno Beckerebffa792018-12-14 15:07:50 +0000810 * \param grp The ECP group to be exported.
811 * This must be initialized and have group parameters
812 * set, for example through mbedtls_ecp_group_load().
813 * \param olen The address at which to store the number of Bytes written.
814 * This must not be \c NULL.
815 * \param buf The buffer to write to. This must be a writable buffer
816 * of length \p blen Bytes.
817 * \param blen The length of the output buffer \p buf in Bytes.
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100818 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100819 * \return \c 0 on success.
Hanno Beckerebffa792018-12-14 15:07:50 +0000820 * \return #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the output
821 * buffer is too small to hold the exported group.
822 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100823 */
Hanno Beckerebffa792018-12-14 15:07:50 +0000824int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp,
825 size_t *olen,
826 unsigned char *buf, size_t blen );
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100827
828/**
Hanno Becker5edcfa52018-12-18 12:49:55 +0000829 * \brief This function performs a scalar multiplication of a point
Hanno Beckerebffa792018-12-14 15:07:50 +0000830 * by an integer: \p R = \p m * \p P.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100831 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100832 * It is not thread-safe to use same group in multiple threads.
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200833 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100834 * \note To prevent timing attacks, this function
835 * executes the exact same sequence of base-field
836 * operations for any valid \p m. It avoids any if-branch or
837 * array index depending on the value of \p m.
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200838 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100839 * \note If \p f_rng is not NULL, it is used to randomize
840 * intermediate results to prevent potential timing attacks
841 * targeting these results. We recommend always providing
842 * a non-NULL \p f_rng. The overhead is negligible.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100843 *
Hanno Beckerebffa792018-12-14 15:07:50 +0000844 * \param grp The ECP group to use.
845 * This must be initialized and have group parameters
846 * set, for example through mbedtls_ecp_group_load().
847 * \param R The point in which to store the result of the calculation.
848 * This must be initialized.
849 * \param m The integer by which to multiply. This must be initialized.
850 * \param P The point to multiply. This must be initialized.
851 * \param f_rng The RNG function. This may be \c NULL if randomization
852 * of intermediate results isn't desired (discouraged).
853 * \param p_rng The RNG context to be passed to \p p_rng.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100854 *
855 * \return \c 0 on success.
Rose Zadikf56cb342018-04-19 12:49:10 +0100856 * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m is not a valid private
857 * key, or \p P is not a valid public key.
858 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
Hanno Beckerebffa792018-12-14 15:07:50 +0000859 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200860 */
861int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
862 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
863 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
864
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200865/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200866 * \brief This function performs multiplication of a point by
867 * an integer: \p R = \p m * \p P in a restartable way.
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200868 *
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200869 * \see mbedtls_ecp_mul()
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200870 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200871 * \note This function does the same as \c mbedtls_ecp_mul(), but
872 * it can return early and restart according to the limit set
873 * with \c mbedtls_ecp_set_max_ops() to reduce blocking.
874 *
Hanno Beckerebffa792018-12-14 15:07:50 +0000875 * \param grp The ECP group to use.
876 * This must be initialized and have group parameters
877 * set, for example through mbedtls_ecp_group_load().
878 * \param R The point in which to store the result of the calculation.
879 * This must be initialized.
880 * \param m The integer by which to multiply. This must be initialized.
881 * \param P The point to multiply. This must be initialized.
882 * \param f_rng The RNG function. This may be \c NULL if randomization
883 * of intermediate results isn't desired (discouraged).
884 * \param p_rng The RNG context to be passed to \p p_rng.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200885 * \param rs_ctx The restart context (NULL disables restart).
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200886 *
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200887 * \return \c 0 on success.
888 * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m is not a valid private
889 * key, or \p P is not a valid public key.
890 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
891 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnard8467e682017-04-20 09:47:06 +0200892 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Hanno Beckerebffa792018-12-14 15:07:50 +0000893 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100894 */
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200895int mbedtls_ecp_mul_restartable( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200897 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
898 mbedtls_ecp_restart_ctx *rs_ctx );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100899
900/**
Rose Zadika7a61552018-04-24 13:14:01 +0100901 * \brief This function performs multiplication and addition of two
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100902 * points by integers: \p R = \p m * \p P + \p n * \p Q
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200903 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100904 * It is not thread-safe to use same group in multiple threads.
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200905 *
Rose Zadika7a61552018-04-24 13:14:01 +0100906 * \note In contrast to mbedtls_ecp_mul(), this function does not
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100907 * guarantee a constant execution flow and timing.
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200908 *
Hanno Beckerebffa792018-12-14 15:07:50 +0000909 * \param grp The ECP group to use.
910 * This must be initialized and have group parameters
911 * set, for example through mbedtls_ecp_group_load().
912 * \param R The point in which to store the result of the calculation.
913 * This must be initialized.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100914 * \param m The integer by which to multiply \p P.
Hanno Beckerebffa792018-12-14 15:07:50 +0000915 * This must be initialized.
916 * \param P The point to multiply by \p m. This must be initialized.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100917 * \param n The integer by which to multiply \p Q.
Hanno Beckerebffa792018-12-14 15:07:50 +0000918 * This must be initialized.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100919 * \param Q The point to be multiplied by \p n.
Hanno Beckerebffa792018-12-14 15:07:50 +0000920 * This must be initialized.
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200921 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100922 * \return \c 0 on success.
Rose Zadika7a61552018-04-24 13:14:01 +0100923 * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m or \p n are not
924 * valid private keys, or \p P or \p Q are not valid public
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100925 * keys.
Rose Zadikf56cb342018-04-19 12:49:10 +0100926 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
Hanno Beckerebffa792018-12-14 15:07:50 +0000927 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200928 */
929int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
930 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
931 const mbedtls_mpi *n, const mbedtls_ecp_point *Q );
932
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200933/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200934 * \brief This function performs multiplication and addition of two
935 * points by integers: \p R = \p m * \p P + \p n * \p Q in a
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200936 * restartable way.
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200937 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200938 * \see \c mbedtls_ecp_muladd()
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200939 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200940 * \note This function works the same as \c mbedtls_ecp_muladd(),
941 * but it can return early and restart according to the limit
942 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200943 *
Hanno Beckerebffa792018-12-14 15:07:50 +0000944 * \param grp The ECP group to use.
945 * This must be initialized and have group parameters
946 * set, for example through mbedtls_ecp_group_load().
947 * \param R The point in which to store the result of the calculation.
948 * This must be initialized.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200949 * \param m The integer by which to multiply \p P.
Hanno Beckerebffa792018-12-14 15:07:50 +0000950 * This must be initialized.
951 * \param P The point to multiply by \p m. This must be initialized.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200952 * \param n The integer by which to multiply \p Q.
Hanno Beckerebffa792018-12-14 15:07:50 +0000953 * This must be initialized.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200954 * \param Q The point to be multiplied by \p n.
Hanno Beckerebffa792018-12-14 15:07:50 +0000955 * This must be initialized.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200956 * \param rs_ctx The restart context (NULL disables restart).
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200957 *
958 * \return \c 0 on success.
959 * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m or \p n are not
960 * valid private keys, or \p P or \p Q are not valid public
961 * keys.
962 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
963 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200964 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Hanno Beckerebffa792018-12-14 15:07:50 +0000965 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200966 */
967int mbedtls_ecp_muladd_restartable(
968 mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
969 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
970 const mbedtls_mpi *n, const mbedtls_ecp_point *Q,
971 mbedtls_ecp_restart_ctx *rs_ctx );
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200972
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200973/**
Rose Zadika7a61552018-04-24 13:14:01 +0100974 * \brief This function checks that a point is a valid public key
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100975 * on this curve.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200976 *
Rose Zadika7a61552018-04-24 13:14:01 +0100977 * It only checks that the point is non-zero, has
978 * valid coordinates and lies on the curve. It does not verify
979 * that it is indeed a multiple of \p G. This additional
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100980 * check is computationally more expensive, is not required
981 * by standards, and should not be necessary if the group
Rose Zadika7a61552018-04-24 13:14:01 +0100982 * used has a small cofactor. In particular, it is useless for
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100983 * the NIST groups which all have a cofactor of 1.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200984 *
Rose Zadika7a61552018-04-24 13:14:01 +0100985 * \note This function uses bare components rather than an
986 * ::mbedtls_ecp_keypair structure, to ease use with other
987 * structures, such as ::mbedtls_ecdh_context or
Rose Zadikb2e111a2018-04-20 10:13:48 +0100988 * ::mbedtls_ecdsa_context.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200989 *
Hanno Beckerebffa792018-12-14 15:07:50 +0000990 * \param grp The ECP group the point should belong to.
991 * This must be initialized and have group parameters
992 * set, for example through mbedtls_ecp_group_load().
993 * \param pt The point to check. This must be initialized.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200994 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100995 * \return \c 0 if the point is a valid public key.
Hanno Beckerebffa792018-12-14 15:07:50 +0000996 * \return #MBEDTLS_ERR_ECP_INVALID_KEY if the point is not
997 * a valid public key for the given curve.
998 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200999 */
Hanno Beckerebffa792018-12-14 15:07:50 +00001000int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp,
1001 const mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001002
1003/**
Hanno Beckerebffa792018-12-14 15:07:50 +00001004 * \brief This function checks that an \p mbedtls_mpi is a
1005 * valid private key for this curve.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001006 *
Rose Zadika7a61552018-04-24 13:14:01 +01001007 * \note This function uses bare components rather than an
1008 * ::mbedtls_ecp_keypair structure to ease use with other
1009 * structures, such as ::mbedtls_ecdh_context or
Rose Zadikb2e111a2018-04-20 10:13:48 +01001010 * ::mbedtls_ecdsa_context.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001011 *
Hanno Beckerebffa792018-12-14 15:07:50 +00001012 * \param grp The ECP group the private key should belong to.
1013 * This must be initialized and have group parameters
1014 * set, for example through mbedtls_ecp_group_load().
1015 * \param d The integer to check. This must be initialized.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001016 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001017 * \return \c 0 if the point is a valid private key.
Hanno Beckerebffa792018-12-14 15:07:50 +00001018 * \return #MBEDTLS_ERR_ECP_INVALID_KEY if the point is not a valid
1019 * private key for the given curve.
1020 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001021 */
Hanno Beckerebffa792018-12-14 15:07:50 +00001022int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp,
1023 const mbedtls_mpi *d );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001024
1025/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001026 * \brief This function generates a private key.
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02001027 *
Hanno Beckerebffa792018-12-14 15:07:50 +00001028 * \param grp The ECP group to generate a private key for.
1029 * This must be initialized and have group parameters
1030 * set, for example through mbedtls_ecp_group_load().
1031 * \param d The destination MPI (secret part). This must be initialized.
1032 * \param f_rng The RNG function. This must not be \c NULL.
1033 * \param p_rng The RNG parameter to be passed to \p f_rng. This may be
1034 * \c NULL if \p f_rng doesn't need a context argument.
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02001035 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001036 * \return \c 0 on success.
1037 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
1038 * on failure.
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02001039 */
1040int mbedtls_ecp_gen_privkey( const mbedtls_ecp_group *grp,
1041 mbedtls_mpi *d,
1042 int (*f_rng)(void *, unsigned char *, size_t),
1043 void *p_rng );
1044
1045/**
Rose Zadika7a61552018-04-24 13:14:01 +01001046 * \brief This function generates a keypair with a configurable base
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001047 * point.
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +02001048 *
Rose Zadika7a61552018-04-24 13:14:01 +01001049 * \note This function uses bare components rather than an
1050 * ::mbedtls_ecp_keypair structure to ease use with other
1051 * structures, such as ::mbedtls_ecdh_context or
Rose Zadikb2e111a2018-04-20 10:13:48 +01001052 * ::mbedtls_ecdsa_context.
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +02001053 *
Hanno Beckerebffa792018-12-14 15:07:50 +00001054 * \param grp The ECP group to generate a key pair for.
1055 * This must be initialized and have group parameters
1056 * set, for example through mbedtls_ecp_group_load().
1057 * \param G The base point to use. This must be initialized
1058 * and belong to \p grp. It replaces the default base
1059 * point \c grp->G used by mbedtls_ecp_gen_keypair().
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001060 * \param d The destination MPI (secret part).
Hanno Beckerebffa792018-12-14 15:07:50 +00001061 * This must be initialized.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001062 * \param Q The destination point (public part).
Hanno Beckerebffa792018-12-14 15:07:50 +00001063 * This must be initialized.
1064 * \param f_rng The RNG function. This must not be \c NULL.
1065 * \param p_rng The RNG context to be passed to \p f_rng. This may
1066 * be \c NULL if \p f_rng doesn't need a context argument.
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +02001067 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001068 * \return \c 0 on success.
1069 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
1070 * on failure.
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +02001071 */
1072int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp,
Hanno Beckerebffa792018-12-14 15:07:50 +00001073 const mbedtls_ecp_point *G,
1074 mbedtls_mpi *d, mbedtls_ecp_point *Q,
1075 int (*f_rng)(void *, unsigned char *, size_t),
1076 void *p_rng );
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +02001077
1078/**
Rose Zadikf56cb342018-04-19 12:49:10 +01001079 * \brief This function generates an ECP keypair.
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001080 *
Rose Zadika7a61552018-04-24 13:14:01 +01001081 * \note This function uses bare components rather than an
1082 * ::mbedtls_ecp_keypair structure to ease use with other
1083 * structures, such as ::mbedtls_ecdh_context or
Rose Zadikb2e111a2018-04-20 10:13:48 +01001084 * ::mbedtls_ecdsa_context.
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001085 *
Hanno Beckerebffa792018-12-14 15:07:50 +00001086 * \param grp The ECP group to generate a key pair for.
1087 * This must be initialized and have group parameters
1088 * set, for example through mbedtls_ecp_group_load().
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001089 * \param d The destination MPI (secret part).
Hanno Beckerebffa792018-12-14 15:07:50 +00001090 * This must be initialized.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001091 * \param Q The destination point (public part).
Hanno Beckerebffa792018-12-14 15:07:50 +00001092 * This must be initialized.
1093 * \param f_rng The RNG function. This must not be \c NULL.
1094 * \param p_rng The RNG context to be passed to \p f_rng. This may
1095 * be \c NULL if \p f_rng doesn't need a context argument.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001096 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001097 * \return \c 0 on success.
1098 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
1099 * on failure.
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001100 */
Hanno Beckerebffa792018-12-14 15:07:50 +00001101int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp, mbedtls_mpi *d,
1102 mbedtls_ecp_point *Q,
1103 int (*f_rng)(void *, unsigned char *, size_t),
1104 void *p_rng );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001105
1106/**
Rose Zadikf56cb342018-04-19 12:49:10 +01001107 * \brief This function generates an ECP key.
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01001108 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001109 * \param grp_id The ECP group identifier.
Hanno Beckerebffa792018-12-14 15:07:50 +00001110 * \param key The destination key. This must be initialized.
1111 * \param f_rng The RNG function to use. This must not be \c NULL.
1112 * \param p_rng The RNG context to be passed to \p f_rng. This may
1113 * be \c NULL if \p f_rng doesn't need a context argument.
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01001114 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001115 * \return \c 0 on success.
1116 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
1117 * on failure.
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01001118 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
Hanno Beckerebffa792018-12-14 15:07:50 +00001120 int (*f_rng)(void *, unsigned char *, size_t),
1121 void *p_rng );
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01001122
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01001123/**
Rose Zadika7a61552018-04-24 13:14:01 +01001124 * \brief This function checks that the keypair objects
Rose Zadikb2e111a2018-04-20 10:13:48 +01001125 * \p pub and \p prv have the same group and the
1126 * same public point, and that the private key in
1127 * \p prv is consistent with the public key.
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01001128 *
Hanno Beckerebffa792018-12-14 15:07:50 +00001129 * \param pub The keypair structure holding the public key. This
1130 * must be initialized. If it contains a private key, that
1131 * part is ignored.
Rose Zadikb2e111a2018-04-20 10:13:48 +01001132 * \param prv The keypair structure holding the full keypair.
Hanno Beckerebffa792018-12-14 15:07:50 +00001133 * This must be initialized.
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01001134 *
Rose Zadika7a61552018-04-24 13:14:01 +01001135 * \return \c 0 on success, meaning that the keys are valid and match.
Rose Zadikb2e111a2018-04-20 10:13:48 +01001136 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the keys are invalid or do not match.
Rose Zadika7a61552018-04-24 13:14:01 +01001137 * \return An \c MBEDTLS_ERR_ECP_XXX or an \c MBEDTLS_ERR_MPI_XXX
Rose Zadikb2e111a2018-04-20 10:13:48 +01001138 * error code on calculation failure.
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01001139 */
Hanno Beckerebffa792018-12-14 15:07:50 +00001140int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub,
1141 const mbedtls_ecp_keypair *prv );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01001142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143#if defined(MBEDTLS_SELF_TEST)
Janos Follathb0697532016-08-18 12:38:46 +01001144
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01001145/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001146 * \brief The ECP checkup routine.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001147 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001148 * \return \c 0 on success.
1149 * \return \c 1 on failure.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001150 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151int mbedtls_ecp_self_test( int verbose );
Janos Follathb0697532016-08-18 12:38:46 +01001152
Janos Follath372697b2016-10-28 16:53:11 +01001153#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001154
1155#ifdef __cplusplus
1156}
1157#endif
1158
Paul Bakker9af723c2014-05-01 13:03:14 +02001159#endif /* ecp.h */