blob: e4a1ed81cf02343c1a1f87c44b171acc4bb176cf [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útia2947ac2020-08-19 16:37:36 +020018 * Copyright The Mbed TLS Contributors
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 * **********
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010059 */
Rose Zadika7a61552018-04-24 13:14:01 +010060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#ifndef MBEDTLS_ECP_H
62#define MBEDTLS_ECP_H
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010063
Ron Eldor8b0cf2e2018-02-14 16:02:41 +020064#if !defined(MBEDTLS_CONFIG_FILE)
65#include "config.h"
66#else
67#include MBEDTLS_CONFIG_FILE
68#endif
69
Manuel Pégourié-Gonnardbdc96762013-10-03 11:50:39 +020070#include "bignum.h"
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010071
72/*
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +010073 * ECP error codes
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010074 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075#define MBEDTLS_ERR_ECP_BAD_INPUT_DATA -0x4F80 /**< Bad input parameters to function. */
76#define MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL -0x4F00 /**< The buffer is too small to write to. */
Rose Zadikd3534052018-04-23 16:12:42 +010077#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 +020078#define MBEDTLS_ERR_ECP_VERIFY_FAILED -0x4E00 /**< The signature is not valid. */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +020079#define MBEDTLS_ERR_ECP_ALLOC_FAILED -0x4D80 /**< Memory allocation failed. */
Rose Zadikd3c9bfc2018-04-17 10:56:55 +010080#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 +020081#define MBEDTLS_ERR_ECP_INVALID_KEY -0x4C80 /**< Invalid private or public key. */
Gilles Peskine5114d3e2018-03-30 07:12:15 +020082#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 +030083
84/* MBEDTLS_ERR_ECP_HW_ACCEL_FAILED is deprecated and should not be used. */
Rose Zadikd3c9bfc2018-04-17 10:56:55 +010085#define MBEDTLS_ERR_ECP_HW_ACCEL_FAILED -0x4B80 /**< The ECP hardware accelerator failed. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030086
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +020087#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 +010088
Paul Bakker407a0da2013-06-27 14:29:21 +020089#ifdef __cplusplus
90extern "C" {
91#endif
92
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010093/**
Rose Zadikd76ac582018-04-23 06:29:34 +010094 * Domain-parameter identifiers: curve, subgroup, and generator.
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +020095 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +010096 * \note Only curves over prime fields are supported.
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +020097 *
98 * \warning This library does not support validation of arbitrary domain
Rose Zadikf56cb342018-04-19 12:49:10 +010099 * parameters. Therefore, only standardized domain parameters from trusted
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200100 * sources should be used. See mbedtls_ecp_group_load().
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200101 */
102typedef enum
103{
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100104 MBEDTLS_ECP_DP_NONE = 0, /*!< Curve not defined. */
Rose Zadikb2e111a2018-04-20 10:13:48 +0100105 MBEDTLS_ECP_DP_SECP192R1, /*!< Domain parameters for the 192-bit curve defined by FIPS 186-4 and SEC1. */
106 MBEDTLS_ECP_DP_SECP224R1, /*!< Domain parameters for the 224-bit curve defined by FIPS 186-4 and SEC1. */
107 MBEDTLS_ECP_DP_SECP256R1, /*!< Domain parameters for the 256-bit curve defined by FIPS 186-4 and SEC1. */
108 MBEDTLS_ECP_DP_SECP384R1, /*!< Domain parameters for the 384-bit curve defined by FIPS 186-4 and SEC1. */
109 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 +0100110 MBEDTLS_ECP_DP_BP256R1, /*!< Domain parameters for 256-bit Brainpool curve. */
111 MBEDTLS_ECP_DP_BP384R1, /*!< Domain parameters for 384-bit Brainpool curve. */
112 MBEDTLS_ECP_DP_BP512R1, /*!< Domain parameters for 512-bit Brainpool curve. */
Rose Zadikb2e111a2018-04-20 10:13:48 +0100113 MBEDTLS_ECP_DP_CURVE25519, /*!< Domain parameters for Curve25519. */
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100114 MBEDTLS_ECP_DP_SECP192K1, /*!< Domain parameters for 192-bit "Koblitz" curve. */
115 MBEDTLS_ECP_DP_SECP224K1, /*!< Domain parameters for 224-bit "Koblitz" curve. */
116 MBEDTLS_ECP_DP_SECP256K1, /*!< Domain parameters for 256-bit "Koblitz" curve. */
Jaeden Amerofe0669f2018-04-27 17:43:32 +0100117 MBEDTLS_ECP_DP_CURVE448, /*!< Domain parameters for Curve448. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118} mbedtls_ecp_group_id;
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200119
120/**
Rose Zadikf56cb342018-04-19 12:49:10 +0100121 * The number of supported curves, plus one for #MBEDTLS_ECP_DP_NONE.
Manuel Pégourié-Gonnard66153662013-12-03 14:12:26 +0100122 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100123 * \note Montgomery curves are currently excluded.
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200124 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125#define MBEDTLS_ECP_DP_MAX 12
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200126
127/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100128 * Curve information, for use by other modules.
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200129 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200130typedef struct mbedtls_ecp_curve_info
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200131{
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100132 mbedtls_ecp_group_id grp_id; /*!< An internal identifier. */
133 uint16_t tls_id; /*!< The TLS NamedCurve identifier. */
Rose Zadikb2e111a2018-04-20 10:13:48 +0100134 uint16_t bit_size; /*!< The curve size in bits. */
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100135 const char *name; /*!< A human-friendly name. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136} mbedtls_ecp_curve_info;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200137
138/**
Rose Zadikf56cb342018-04-19 12:49:10 +0100139 * \brief The ECP point structure, in Jacobian coordinates.
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100140 *
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100141 * \note All functions expect and return points satisfying
Rose Zadikf56cb342018-04-19 12:49:10 +0100142 * the following condition: <code>Z == 0</code> or
Rose Zadika7a61552018-04-24 13:14:01 +0100143 * <code>Z == 1</code>. Other values of \p Z are
Rose Zadikf56cb342018-04-19 12:49:10 +0100144 * used only by internal functions.
145 * The point is zero, or "at infinity", if <code>Z == 0</code>.
Rose Zadika7a61552018-04-24 13:14:01 +0100146 * Otherwise, \p X and \p Y are its standard (affine)
Rose Zadikf56cb342018-04-19 12:49:10 +0100147 * coordinates.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100148 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200149typedef struct mbedtls_ecp_point
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100150{
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100151 mbedtls_mpi X; /*!< The X coordinate of the ECP point. */
152 mbedtls_mpi Y; /*!< The Y coordinate of the ECP point. */
153 mbedtls_mpi Z; /*!< The Z coordinate of the ECP point. */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100154}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155mbedtls_ecp_point;
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100156
Gilles Peskineeaf74422021-06-02 23:21:07 +0200157/* Determine the minimum safe value of MBEDTLS_ECP_MAX_BITS. */
158#if !defined(MBEDTLS_ECP_C)
159#define MBEDTLS_ECP_MAX_BITS_MIN 0
160/* Note: the curves must be listed in DECREASING size! */
161#elif defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
162#define MBEDTLS_ECP_MAX_BITS_MIN 521
163#elif defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
164#define MBEDTLS_ECP_MAX_BITS_MIN 512
165#elif defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
166#define MBEDTLS_ECP_MAX_BITS_MIN 448
167#elif defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
168#define MBEDTLS_ECP_MAX_BITS_MIN 384
169#elif defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
170#define MBEDTLS_ECP_MAX_BITS_MIN 384
171#elif defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
172#define MBEDTLS_ECP_MAX_BITS_MIN 256
173#elif defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
174#define MBEDTLS_ECP_MAX_BITS_MIN 256
175#elif defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
176#define MBEDTLS_ECP_MAX_BITS_MIN 256
177#elif defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
178#define MBEDTLS_ECP_MAX_BITS_MIN 255
179#elif defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
180#define MBEDTLS_ECP_MAX_BITS_MIN 225 // n is slightly above 2^224
181#elif defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
182#define MBEDTLS_ECP_MAX_BITS_MIN 224
183#elif defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
184#define MBEDTLS_ECP_MAX_BITS_MIN 192
185#elif defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
186#define MBEDTLS_ECP_MAX_BITS_MIN 192
187#else
188#error "MBEDTLS_ECP_C enabled, but no curve?"
189#endif
190
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100191#if !defined(MBEDTLS_ECP_ALT)
192/*
193 * default mbed TLS elliptic curve arithmetic implementation
194 *
195 * (in case MBEDTLS_ECP_ALT is defined then the developer has to provide an
196 * alternative implementation for the whole module and it will replace this
197 * one.)
198 */
199
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100200/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100201 * \brief The ECP group structure.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100202 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100203 * We consider two types of curve equations:
Rose Zadikb2e111a2018-04-20 10:13:48 +0100204 * <ul><li>Short Weierstrass: <code>y^2 = x^3 + A x + B mod P</code>
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100205 * (SEC1 + RFC-4492)</li>
Rose Zadikf56cb342018-04-19 12:49:10 +0100206 * <li>Montgomery: <code>y^2 = x^3 + A x^2 + x mod P</code> (Curve25519,
Rose Zadikb2e111a2018-04-20 10:13:48 +0100207 * Curve448)</li></ul>
Rose Zadikf56cb342018-04-19 12:49:10 +0100208 * In both cases, the generator (\p G) for a prime-order subgroup is fixed.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100209 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100210 * For Short Weierstrass, this subgroup is the whole curve, and its
Rose Zadikf56cb342018-04-19 12:49:10 +0100211 * cardinality is denoted by \p N. Our code requires that \p N is an
Rose Zadikb2e111a2018-04-20 10:13:48 +0100212 * odd prime as mbedtls_ecp_mul() requires an odd number, and
213 * mbedtls_ecdsa_sign() requires that it is prime for blinding purposes.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100214 *
Rose Zadikf56cb342018-04-19 12:49:10 +0100215 * For Montgomery curves, we do not store \p A, but <code>(A + 2) / 4</code>,
Rose Zadika7a61552018-04-24 13:14:01 +0100216 * which is the quantity used in the formulas. Additionally, \p nbits is
Rose Zadikf56cb342018-04-19 12:49:10 +0100217 * not the size of \p N but the required size for private keys.
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100218 *
Rose Zadika7a61552018-04-24 13:14:01 +0100219 * If \p modp is NULL, reduction modulo \p P is done using a generic algorithm.
220 * Otherwise, \p modp must point to a function that takes an \p mbedtls_mpi in the
221 * range of <code>0..2^(2*pbits)-1</code>, and transforms it in-place to an integer
222 * which is congruent mod \p P to the given MPI, and is close enough to \p pbits
223 * in size, so that it may be efficiently brought in the 0..P-1 range by a few
224 * additions or subtractions. Therefore, it is only an approximative modular
225 * reduction. It must return 0 on success and non-zero on failure.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100226 *
Janos Follathc3b680b2018-12-05 16:01:13 +0000227 * \note Alternative implementations must keep the group IDs distinct. If
228 * two group structures have the same ID, then they must be
229 * identical.
230 *
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100231 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200232typedef struct mbedtls_ecp_group
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100233{
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100234 mbedtls_ecp_group_id id; /*!< An internal group identifier. */
Rose Zadikf56cb342018-04-19 12:49:10 +0100235 mbedtls_mpi P; /*!< The prime modulus of the base field. */
Rose Zadikb2e111a2018-04-20 10:13:48 +0100236 mbedtls_mpi A; /*!< For Short Weierstrass: \p A in the equation. For
237 Montgomery curves: <code>(A + 2) / 4</code>. */
238 mbedtls_mpi B; /*!< For Short Weierstrass: \p B in the equation.
239 For Montgomery curves: unused. */
Rose Zadikf56cb342018-04-19 12:49:10 +0100240 mbedtls_ecp_point G; /*!< The generator of the subgroup used. */
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100241 mbedtls_mpi N; /*!< The order of \p G. */
242 size_t pbits; /*!< The number of bits in \p P.*/
Rose Zadikb2e111a2018-04-20 10:13:48 +0100243 size_t nbits; /*!< For Short Weierstrass: The number of bits in \p P.
Rose Zadika7a61552018-04-24 13:14:01 +0100244 For Montgomery curves: the number of bits in the
Rose Zadikd76ac582018-04-23 06:29:34 +0100245 private keys. */
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100246 unsigned int h; /*!< \internal 1 if the constants are static. */
Rose Zadika7a61552018-04-24 13:14:01 +0100247 int (*modp)(mbedtls_mpi *); /*!< The function for fast pseudo-reduction
Rose Zadikf56cb342018-04-19 12:49:10 +0100248 mod \p P (see above).*/
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100249 int (*t_pre)(mbedtls_ecp_point *, void *); /*!< Unused. */
250 int (*t_post)(mbedtls_ecp_point *, void *); /*!< Unused. */
251 void *t_data; /*!< Unused. */
252 mbedtls_ecp_point *T; /*!< Pre-computed points for ecp_mul_comb(). */
Rose Zadikb2e111a2018-04-20 10:13:48 +0100253 size_t T_size; /*!< The number of pre-computed points. */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100254}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255mbedtls_ecp_group;
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100256
Darryl Greenb7797592019-01-04 16:18:06 +0000257/**
258 * \name SECTION: Module settings
259 *
260 * The configuration options you can set for this module are in this section.
261 * Either change them in config.h, or define them using the compiler command line.
262 * \{
263 */
264
Gilles Peskineeaf74422021-06-02 23:21:07 +0200265#if defined(MBEDTLS_ECP_MAX_BITS)
266
267#if MBEDTLS_ECP_MAX_BITS < MBEDTLS_ECP_MAX_BITS_MIN
268#error "MBEDTLS_ECP_MAX_BITS is smaller than the largest supported curve"
269#endif
270
271#else
Darryl Greenb7797592019-01-04 16:18:06 +0000272/**
273 * The maximum size of the groups, that is, of \c N and \c P.
274 */
275#define MBEDTLS_ECP_MAX_BITS 521 /**< The maximum size of groups, in bits. */
276#endif
277
278#define MBEDTLS_ECP_MAX_BYTES ( ( MBEDTLS_ECP_MAX_BITS + 7 ) / 8 )
279#define MBEDTLS_ECP_MAX_PT_LEN ( 2 * MBEDTLS_ECP_MAX_BYTES + 1 )
280
281#if !defined(MBEDTLS_ECP_WINDOW_SIZE)
282/*
283 * Maximum "window" size used for point multiplication.
284 * Default: 6.
285 * Minimum value: 2. Maximum value: 7.
286 *
287 * Result is an array of at most ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) )
288 * points used for point multiplication. This value is directly tied to EC
289 * peak memory usage, so decreasing it by one should roughly cut memory usage
290 * by two (if large curves are in use).
291 *
292 * Reduction in size may reduce speed, but larger curves are impacted first.
293 * Sample performances (in ECDHE handshakes/s, with FIXED_POINT_OPTIM = 1):
294 * w-size: 6 5 4 3 2
295 * 521 145 141 135 120 97
296 * 384 214 209 198 177 146
297 * 256 320 320 303 262 226
298 * 224 475 475 453 398 342
299 * 192 640 640 633 587 476
300 */
301#define MBEDTLS_ECP_WINDOW_SIZE 6 /**< The maximum window size used. */
302#endif /* MBEDTLS_ECP_WINDOW_SIZE */
303
304#if !defined(MBEDTLS_ECP_FIXED_POINT_OPTIM)
305/*
306 * Trade memory for speed on fixed-point multiplication.
307 *
308 * This speeds up repeated multiplication of the generator (that is, the
309 * multiplication in ECDSA signatures, and half of the multiplications in
310 * ECDSA verification and ECDHE) by a factor roughly 3 to 4.
311 *
312 * The cost is increasing EC peak memory usage by a factor roughly 2.
313 *
314 * Change this value to 0 to reduce peak memory usage.
315 */
316#define MBEDTLS_ECP_FIXED_POINT_OPTIM 1 /**< Enable fixed-point speed-up. */
317#endif /* MBEDTLS_ECP_FIXED_POINT_OPTIM */
318
319/* \} name SECTION: Module settings */
320
321#else /* MBEDTLS_ECP_ALT */
322#include "ecp_alt.h"
323#endif /* MBEDTLS_ECP_ALT */
324
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200325#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardb5a50e72017-04-20 16:06:13 +0200326
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200327/**
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +0200328 * \brief Internal restart context for multiplication
329 *
330 * \note Opaque struct
331 */
332typedef struct mbedtls_ecp_restart_mul mbedtls_ecp_restart_mul_ctx;
333
334/**
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200335 * \brief Internal restart context for ecp_muladd()
336 *
337 * \note Opaque struct
338 */
339typedef struct mbedtls_ecp_restart_muladd mbedtls_ecp_restart_muladd_ctx;
340
341/**
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200342 * \brief General context for resuming ECC operations
343 */
344typedef struct
345{
Manuel Pégourié-Gonnard646393b2017-04-20 10:03:45 +0200346 unsigned ops_done; /*!< current ops count */
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +0200347 unsigned depth; /*!< call depth (0 = top-level) */
Manuel Pégourié-Gonnard646393b2017-04-20 10:03:45 +0200348 mbedtls_ecp_restart_mul_ctx *rsm; /*!< ecp_mul_comb() sub-context */
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200349 mbedtls_ecp_restart_muladd_ctx *ma; /*!< ecp_muladd() sub-context */
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200350} mbedtls_ecp_restart_ctx;
Manuel Pégourié-Gonnardb5a50e72017-04-20 16:06:13 +0200351
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +0200352/*
353 * Operation counts for restartable functions
354 */
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200355#define MBEDTLS_ECP_OPS_CHK 3 /*!< basic ops count for ecp_check_pubkey() */
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +0200356#define MBEDTLS_ECP_OPS_DBL 8 /*!< basic ops count for ecp_double_jac() */
357#define MBEDTLS_ECP_OPS_ADD 11 /*!< basic ops count for see ecp_add_mixed() */
358#define MBEDTLS_ECP_OPS_INV 120 /*!< empirical equivalent for mpi_mod_inv() */
359
360/**
361 * \brief Internal; for restartable functions in other modules.
362 * Check and update basic ops budget.
363 *
364 * \param grp Group structure
365 * \param rs_ctx Restart context
366 * \param ops Number of basic ops to do
367 *
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200368 * \return \c 0 if doing \p ops basic ops is still allowed,
369 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS otherwise.
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +0200370 */
371int mbedtls_ecp_check_budget( const mbedtls_ecp_group *grp,
372 mbedtls_ecp_restart_ctx *rs_ctx,
373 unsigned ops );
374
375/* Utility macro for checking and updating ops budget */
Hanno Beckerabdf67e2018-10-26 13:28:32 +0100376#define MBEDTLS_ECP_BUDGET( ops ) \
377 MBEDTLS_MPI_CHK( mbedtls_ecp_check_budget( grp, rs_ctx, \
378 (unsigned) (ops) ) );
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +0200379
Manuel Pégourié-Gonnardb5a50e72017-04-20 16:06:13 +0200380#else /* MBEDTLS_ECP_RESTARTABLE */
381
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +0200382#define MBEDTLS_ECP_BUDGET( ops ) /* no-op; for compatibility */
383
Manuel Pégourié-Gonnardb5a50e72017-04-20 16:06:13 +0200384/* We want to declare restartable versions of existing functions anyway */
385typedef void mbedtls_ecp_restart_ctx;
386
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200387#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200388
Paul Bakker088c5c52014-04-25 11:11:10 +0200389/**
Jaeden Amero89453432018-04-24 17:15:38 +0100390 * \brief The ECP key-pair structure.
Ron Eldor05d0e512018-04-16 17:40:04 +0300391 *
Jaeden Amero89453432018-04-24 17:15:38 +0100392 * A generic key-pair that may be used for ECDSA and fixed ECDH, for example.
Ron Eldor05d0e512018-04-16 17:40:04 +0300393 *
Jaeden Amero89453432018-04-24 17:15:38 +0100394 * \note Members are deliberately in the same order as in the
395 * ::mbedtls_ecdsa_context structure.
Ron Eldor05d0e512018-04-16 17:40:04 +0300396 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200397typedef struct mbedtls_ecp_keypair
Ron Eldor05d0e512018-04-16 17:40:04 +0300398{
399 mbedtls_ecp_group grp; /*!< Elliptic curve and base point */
400 mbedtls_mpi d; /*!< our secret value */
401 mbedtls_ecp_point Q; /*!< our public value */
402}
403mbedtls_ecp_keypair;
404
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100405/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100406 * Point formats, from RFC 4492's enum ECPointFormat
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100407 */
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100408#define MBEDTLS_ECP_PF_UNCOMPRESSED 0 /**< Uncompressed point format. */
409#define MBEDTLS_ECP_PF_COMPRESSED 1 /**< Compressed point format. */
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100410
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100411/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100412 * Some other constants from RFC 4492
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100413 */
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100414#define MBEDTLS_ECP_TLS_NAMED_CURVE 3 /**< The named_curve of ECCurveType. */
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100415
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200416#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100417/**
418 * \brief Set the maximum number of basic operations done in a row.
419 *
420 * If more operations are needed to complete a computation,
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200421 * #MBEDTLS_ERR_ECP_IN_PROGRESS will be returned by the
Manuel Pégourié-Gonnard8467e682017-04-20 09:47:06 +0200422 * function performing the computation. It is then the
423 * caller's responsibility to either call again with the same
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +0200424 * parameters until it returns 0 or an error code; or to free
Manuel Pégourié-Gonnard8467e682017-04-20 09:47:06 +0200425 * the restart context if the operation is to be aborted.
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100426 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +0200427 * It is strictly required that all input parameters and the
428 * restart context be the same on successive calls for the
429 * same operation, but output parameters need not be the
430 * same; they must not be used until the function finally
431 * returns 0.
432 *
Manuel Pégourié-Gonnard32df9112018-10-15 13:29:21 +0200433 * This only applies to functions whose documentation
434 * mentions they may return #MBEDTLS_ERR_ECP_IN_PROGRESS (or
435 * #MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS for functions in the
436 * SSL module). For functions that accept a "restart context"
437 * argument, passing NULL disables restart and makes the
438 * function equivalent to the function with the same name
439 * with \c _restartable removed. For functions in the ECDH
440 * module, restart is disabled unless the function accepts
441 * an "ECDH context" argument and
442 * mbedtls_ecdh_enable_restart() was previously called on
443 * that context. For function in the SSL module, restart is
444 * only enabled for specific sides and key exchanges
445 * (currently only for clients and ECDHE-ECDSA).
Manuel Pégourié-Gonnard8f28add2017-04-19 10:20:49 +0200446 *
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100447 * \param max_ops Maximum number of basic operations done in a row.
448 * Default: 0 (unlimited).
449 * Lower (non-zero) values mean ECC functions will block for
450 * a lesser maximum amount of time.
451 *
Manuel Pégourié-Gonnarde6854492017-03-20 14:35:19 +0100452 * \note A "basic operation" is defined as a rough equivalent of a
453 * multiplication in GF(p) for the NIST P-256 curve.
454 * As an indication, with default settings, a scalar
455 * multiplication (full run of \c mbedtls_ecp_mul()) is:
456 * - about 3300 basic operations for P-256
457 * - about 9400 basic operations for P-384
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100458 *
Manuel Pégourié-Gonnarde58f65a2017-03-20 14:59:54 +0100459 * \note Very low values are not always respected: sometimes
Manuel Pégourié-Gonnard1c678e02017-03-20 13:39:39 +0100460 * functions need to block for a minimum number of
461 * operations, and will do so even if max_ops is set to a
462 * lower value. That minimum depends on the curve size, and
463 * can be made lower by decreasing the value of
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +0200464 * \c MBEDTLS_ECP_WINDOW_SIZE. As an indication, here is the
465 * lowest effective value for various curves and values of
466 * that parameter (w for short):
467 * w=6 w=5 w=4 w=3 w=2
468 * P-256 208 208 160 136 124
469 * P-384 682 416 320 272 248
470 * P-521 1364 832 640 544 496
Manuel Pégourié-Gonnarde58f65a2017-03-20 14:59:54 +0100471 *
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200472 * \note This setting is currently ignored by Curve25519.
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100473 */
474void mbedtls_ecp_set_max_ops( unsigned max_ops );
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200475
476/**
477 * \brief Check if restart is enabled (max_ops != 0)
478 *
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200479 * \return \c 0 if \c max_ops == 0 (restart disabled)
480 * \return \c 1 otherwise (restart enabled)
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200481 */
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200482int mbedtls_ecp_restart_is_enabled( void );
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200483#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100484
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100485/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100486 * \brief This function retrieves the information defined in
Rose Zadikf56cb342018-04-19 12:49:10 +0100487 * mbedtls_ecp_curve_info() for all supported curves in order
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100488 * of preference.
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200489 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100490 * \return A statically allocated array. The last entry is 0.
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200491 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list( void );
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200493
494/**
Rose Zadikf56cb342018-04-19 12:49:10 +0100495 * \brief This function retrieves the list of internal group
496 * identifiers of all supported curves in the order of
497 * preference.
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100498 *
499 * \return A statically allocated array,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 * terminated with MBEDTLS_ECP_DP_NONE.
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100501 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list( void );
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100503
504/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100505 * \brief This function retrieves curve information from an internal
506 * group identifier.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200507 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100508 * \param grp_id An \c MBEDTLS_ECP_DP_XXX value.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200509 *
Rose Zadikf56cb342018-04-19 12:49:10 +0100510 * \return The associated curve information on success.
511 * \return NULL on failure.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200512 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513const 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 +0200514
515/**
Rose Zadika7a61552018-04-24 13:14:01 +0100516 * \brief This function retrieves curve information from a TLS
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100517 * NamedCurve value.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200518 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100519 * \param tls_id An \c MBEDTLS_ECP_DP_XXX value.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200520 *
Rose Zadikf56cb342018-04-19 12:49:10 +0100521 * \return The associated curve information on success.
522 * \return NULL on failure.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200523 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id( uint16_t tls_id );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200525
526/**
Rose Zadika7a61552018-04-24 13:14:01 +0100527 * \brief This function retrieves curve information from a
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100528 * human-readable name.
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100529 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100530 * \param name The human-readable name.
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100531 *
Rose Zadikf56cb342018-04-19 12:49:10 +0100532 * \return The associated curve information on success.
533 * \return NULL on failure.
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100534 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name( const char *name );
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100536
537/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100538 * \brief This function initializes a point as zero.
539 *
540 * \param pt The point to initialize.
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100541 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542void mbedtls_ecp_point_init( mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100543
544/**
Rose Zadikf56cb342018-04-19 12:49:10 +0100545 * \brief This function initializes an ECP group context
Rose Zadika7a61552018-04-24 13:14:01 +0100546 * without loading any domain parameters.
Rose Zadikf56cb342018-04-19 12:49:10 +0100547 *
548 * \note After this function is called, domain parameters
549 * for various ECP groups can be loaded through the
Gilles Peskine4dc50bc2018-11-07 22:06:48 +0100550 * mbedtls_ecp_group_load() or mbedtls_ecp_tls_read_group()
Rose Zadikf56cb342018-04-19 12:49:10 +0100551 * functions.
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100552 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553void mbedtls_ecp_group_init( mbedtls_ecp_group *grp );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100554
555/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100556 * \brief This function initializes a key pair as an invalid one.
557 *
558 * \param key The key pair to initialize.
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200559 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560void mbedtls_ecp_keypair_init( mbedtls_ecp_keypair *key );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200561
562/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100563 * \brief This function frees the components of a point.
564 *
565 * \param pt The point to free.
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100566 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567void mbedtls_ecp_point_free( mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100568
569/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100570 * \brief This function frees the components of an ECP group.
Hanno Beckerebffa792018-12-14 15:07:50 +0000571 *
572 * \param grp The group to free. This may be \c NULL, in which
Hanno Becker486f1b32018-12-18 13:00:34 +0000573 * case this function returns immediately. If it is not
574 * \c NULL, it must point to an initialized ECP group.
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100575 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576void mbedtls_ecp_group_free( mbedtls_ecp_group *grp );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100577
578/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100579 * \brief This function frees the components of a key pair.
Hanno Beckerebffa792018-12-14 15:07:50 +0000580 *
581 * \param key The key pair to free. This may be \c NULL, in which
Hanno Becker486f1b32018-12-18 13:00:34 +0000582 * case this function returns immediately. If it is not
583 * \c NULL, it must point to an initialized ECP key pair.
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200584 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair *key );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200586
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200587#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200588/**
Hanno Beckerebffa792018-12-14 15:07:50 +0000589 * \brief Initialize a restart context.
590 *
591 * \param ctx The restart context to initialize. This must
592 * not be \c NULL.
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200593 */
594void mbedtls_ecp_restart_init( mbedtls_ecp_restart_ctx *ctx );
595
596/**
Hanno Beckerebffa792018-12-14 15:07:50 +0000597 * \brief Free the components of a restart context.
598 *
599 * \param ctx The restart context to free. This may be \c NULL, in which
Hanno Becker486f1b32018-12-18 13:00:34 +0000600 * case this function returns immediately. If it is not
601 * \c NULL, it must point to an initialized restart context.
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200602 */
603void mbedtls_ecp_restart_free( mbedtls_ecp_restart_ctx *ctx );
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200604#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200605
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200606/**
Rose Zadika7a61552018-04-24 13:14:01 +0100607 * \brief This function copies the contents of point \p Q into
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100608 * point \p P.
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100609 *
Hanno Beckerebffa792018-12-14 15:07:50 +0000610 * \param P The destination point. This must be initialized.
611 * \param Q The source point. This must be initialized.
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100612 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100613 * \return \c 0 on success.
Rose Zadikf56cb342018-04-19 12:49:10 +0100614 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
Hanno Beckerebffa792018-12-14 15:07:50 +0000615 * \return Another negative error code for other kinds of failure.
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100616 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617int mbedtls_ecp_copy( mbedtls_ecp_point *P, const mbedtls_ecp_point *Q );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100618
619/**
Rose Zadika7a61552018-04-24 13:14:01 +0100620 * \brief This function copies the contents of group \p src into
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100621 * group \p dst.
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200622 *
Hanno Beckerebffa792018-12-14 15:07:50 +0000623 * \param dst The destination group. This must be initialized.
624 * \param src The source group. This must be initialized.
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200625 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100626 * \return \c 0 on success.
Rose Zadikf56cb342018-04-19 12:49:10 +0100627 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
Hanno Beckerebffa792018-12-14 15:07:50 +0000628 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200629 */
Hanno Beckerebffa792018-12-14 15:07:50 +0000630int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst,
631 const mbedtls_ecp_group *src );
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200632
633/**
Hanno Beckerebffa792018-12-14 15:07:50 +0000634 * \brief This function sets a point to the point at infinity.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200635 *
Hanno Beckerebffa792018-12-14 15:07:50 +0000636 * \param pt The point to set. This must be initialized.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200637 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100638 * \return \c 0 on success.
Rose Zadikf56cb342018-04-19 12:49:10 +0100639 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
Hanno Beckerebffa792018-12-14 15:07:50 +0000640 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200641 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642int mbedtls_ecp_set_zero( mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200643
644/**
Hanno Beckerebffa792018-12-14 15:07:50 +0000645 * \brief This function checks if a point is the point at infinity.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200646 *
Hanno Beckerebffa792018-12-14 15:07:50 +0000647 * \param pt The point to test. This must be initialized.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200648 *
Rose Zadikf56cb342018-04-19 12:49:10 +0100649 * \return \c 1 if the point is zero.
650 * \return \c 0 if the point is non-zero.
Hanno Beckerebffa792018-12-14 15:07:50 +0000651 * \return A negative error code on failure.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200652 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200654
655/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100656 * \brief This function compares two points.
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200657 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100658 * \note This assumes that the points are normalized. Otherwise,
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200659 * they may compare as "not equal" even if they are.
660 *
Hanno Beckerebffa792018-12-14 15:07:50 +0000661 * \param P The first point to compare. This must be initialized.
662 * \param Q The second point to compare. This must be initialized.
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200663 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100664 * \return \c 0 if the points are equal.
665 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the points are not equal.
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200666 */
667int mbedtls_ecp_point_cmp( const mbedtls_ecp_point *P,
668 const mbedtls_ecp_point *Q );
669
670/**
Rose Zadika7a61552018-04-24 13:14:01 +0100671 * \brief This function imports a non-zero point from two ASCII
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100672 * strings.
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100673 *
Hanno Beckerebffa792018-12-14 15:07:50 +0000674 * \param P The destination point. This must be initialized.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100675 * \param radix The numeric base of the input.
676 * \param x The first affine coordinate, as a null-terminated string.
677 * \param y The second affine coordinate, as a null-terminated string.
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100678 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100679 * \return \c 0 on success.
680 * \return An \c MBEDTLS_ERR_MPI_XXX error code on failure.
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100681 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682int mbedtls_ecp_point_read_string( mbedtls_ecp_point *P, int radix,
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100683 const char *x, const char *y );
684
685/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100686 * \brief This function exports a point into unsigned binary data.
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100687 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100688 * \param grp The group to which the point should belong.
Hanno Beckerebffa792018-12-14 15:07:50 +0000689 * This must be initialized and have group parameters
690 * set, for example through mbedtls_ecp_group_load().
691 * \param P The point to export. This must be initialized.
692 * \param format The point format. This must be either
693 * #MBEDTLS_ECP_PF_COMPRESSED or #MBEDTLS_ECP_PF_UNCOMPRESSED.
694 * \param olen The address at which to store the length of
Hanno Becker5edcfa52018-12-18 12:49:55 +0000695 * the output in Bytes. This must not be \c NULL.
Hanno Beckerebffa792018-12-14 15:07:50 +0000696 * \param buf The output buffer. This must be a writable buffer
697 * of length \p buflen Bytes.
698 * \param buflen The length of the output buffer \p buf in Bytes.
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100699 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100700 * \return \c 0 on success.
Hanno Beckerebffa792018-12-14 15:07:50 +0000701 * \return #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the output buffer
702 * is too small to hold the point.
703 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100704 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100706 int format, size_t *olen,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100707 unsigned char *buf, size_t buflen );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100708
709/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100710 * \brief This function imports a point from unsigned binary data.
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100711 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100712 * \note This function does not check that the point actually
Rose Zadika7a61552018-04-24 13:14:01 +0100713 * belongs to the given group, see mbedtls_ecp_check_pubkey()
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100714 * for that.
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100715 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100716 * \param grp The group to which the point should belong.
Hanno Beckerebffa792018-12-14 15:07:50 +0000717 * This must be initialized and have group parameters
718 * set, for example through mbedtls_ecp_group_load().
719 * \param P The destination context to import the point to.
720 * This must be initialized.
721 * \param buf The input buffer. This must be a readable buffer
722 * of length \p ilen Bytes.
723 * \param ilen The length of the input buffer \p buf in Bytes.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100724 *
725 * \return \c 0 on success.
Hanno Beckerebffa792018-12-14 15:07:50 +0000726 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the input is invalid.
Rose Zadikf56cb342018-04-19 12:49:10 +0100727 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100728 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the point format
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100729 * is not implemented.
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100730 */
Hanno Beckerebffa792018-12-14 15:07:50 +0000731int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp,
732 mbedtls_ecp_point *P,
733 const unsigned char *buf, size_t ilen );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100734
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100735/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100736 * \brief This function imports a point from a TLS ECPoint record.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200737 *
Janos Follath89ac8c92018-10-30 11:24:05 +0000738 * \note On function return, \p *buf is updated to point immediately
Rose Zadikf56cb342018-04-19 12:49:10 +0100739 * after the ECPoint record.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200740 *
Hanno Beckerebffa792018-12-14 15:07:50 +0000741 * \param grp The ECP group to use.
742 * This must be initialized and have group parameters
743 * set, for example through mbedtls_ecp_group_load().
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100744 * \param pt The destination point.
Rose Zadikf56cb342018-04-19 12:49:10 +0100745 * \param buf The address of the pointer to the start of the input buffer.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100746 * \param len The length of the buffer.
Manuel Pégourié-Gonnard150c4f62014-11-21 09:14:52 +0100747 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100748 * \return \c 0 on success.
Janos Follath89ac8c92018-10-30 11:24:05 +0000749 * \return An \c MBEDTLS_ERR_MPI_XXX error code on initialization
750 * failure.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100751 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200752 */
Hanno Beckerebffa792018-12-14 15:07:50 +0000753int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp,
754 mbedtls_ecp_point *pt,
755 const unsigned char **buf, size_t len );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200756
757/**
Hanno Beckerebffa792018-12-14 15:07:50 +0000758 * \brief This function exports a point as a TLS ECPoint record
759 * defined in RFC 4492, Section 5.4.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200760 *
Hanno Beckerebffa792018-12-14 15:07:50 +0000761 * \param grp The ECP group to use.
762 * This must be initialized and have group parameters
763 * set, for example through mbedtls_ecp_group_load().
764 * \param pt The point to be exported. This must be initialized.
765 * \param format The point format to use. This must be either
766 * #MBEDTLS_ECP_PF_COMPRESSED or #MBEDTLS_ECP_PF_UNCOMPRESSED.
767 * \param olen The address at which to store the length in Bytes
768 * of the data written.
769 * \param buf The target buffer. This must be a writable buffer of
770 * length \p blen Bytes.
771 * \param blen The length of the target buffer \p buf in Bytes.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200772 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100773 * \return \c 0 on success.
Hanno Beckerebffa792018-12-14 15:07:50 +0000774 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the input is invalid.
775 * \return #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the target buffer
776 * is too small to hold the exported point.
777 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200778 */
Hanno Beckerebffa792018-12-14 15:07:50 +0000779int mbedtls_ecp_tls_write_point( const mbedtls_ecp_group *grp,
780 const mbedtls_ecp_point *pt,
781 int format, size_t *olen,
782 unsigned char *buf, size_t blen );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200783
784/**
Hanno Beckerebffa792018-12-14 15:07:50 +0000785 * \brief This function sets up an ECP group context
786 * from a standardized set of domain parameters.
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100787 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100788 * \note The index should be a value of the NamedCurve enum,
Rose Zadika7a61552018-04-24 13:14:01 +0100789 * as defined in <em>RFC-4492: Elliptic Curve Cryptography
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100790 * (ECC) Cipher Suites for Transport Layer Security (TLS)</em>,
791 * usually in the form of an \c MBEDTLS_ECP_DP_XXX macro.
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100792 *
Hanno Beckerebffa792018-12-14 15:07:50 +0000793 * \param grp The group context to setup. This must be initialized.
Rose Zadikf56cb342018-04-19 12:49:10 +0100794 * \param id The identifier of the domain parameter set to load.
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100795 *
Hanno Beckerebffa792018-12-14 15:07:50 +0000796 * \return \c 0 on success.
797 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if \p id doesn't
798 * correspond to a known group.
799 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100800 */
Hanno Becker61937d42017-04-26 15:01:23 +0100801int mbedtls_ecp_group_load( mbedtls_ecp_group *grp, mbedtls_ecp_group_id id );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100802
803/**
Hanno Beckerebffa792018-12-14 15:07:50 +0000804 * \brief This function sets up an ECP group context from a TLS
805 * ECParameters record as defined in RFC 4492, Section 5.4.
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100806 *
Hanno Beckerebffa792018-12-14 15:07:50 +0000807 * \note The read pointer \p buf is updated to point right after
808 * the ECParameters record on exit.
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100809 *
Hanno Beckerebffa792018-12-14 15:07:50 +0000810 * \param grp The group context to setup. This must be initialized.
Rose Zadikf56cb342018-04-19 12:49:10 +0100811 * \param buf The address of the pointer to the start of the input buffer.
Hanno Beckerebffa792018-12-14 15:07:50 +0000812 * \param len The length of the input buffer \c *buf in Bytes.
Manuel Pégourié-Gonnard150c4f62014-11-21 09:14:52 +0100813 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100814 * \return \c 0 on success.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100815 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid.
Janos Follath89ac8c92018-10-30 11:24:05 +0000816 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the group is not
Hanno Beckerebffa792018-12-14 15:07:50 +0000817 * recognized.
818 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100819 */
Hanno Beckerebffa792018-12-14 15:07:50 +0000820int mbedtls_ecp_tls_read_group( mbedtls_ecp_group *grp,
821 const unsigned char **buf, size_t len );
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100822
823/**
Hanno Beckerebffa792018-12-14 15:07:50 +0000824 * \brief This function extracts an elliptic curve group ID from a
825 * TLS ECParameters record as defined in RFC 4492, Section 5.4.
Janos Follath89ac8c92018-10-30 11:24:05 +0000826 *
Hanno Beckerebffa792018-12-14 15:07:50 +0000827 * \note The read pointer \p buf is updated to point right after
828 * the ECParameters record on exit.
Janos Follath89ac8c92018-10-30 11:24:05 +0000829 *
Hanno Beckerebffa792018-12-14 15:07:50 +0000830 * \param grp The address at which to store the group id.
831 * This must not be \c NULL.
Janos Follath89ac8c92018-10-30 11:24:05 +0000832 * \param buf The address of the pointer to the start of the input buffer.
Hanno Beckerebffa792018-12-14 15:07:50 +0000833 * \param len The length of the input buffer \c *buf in Bytes.
Janos Follath89ac8c92018-10-30 11:24:05 +0000834 *
835 * \return \c 0 on success.
836 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid.
837 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the group is not
Hanno Beckerebffa792018-12-14 15:07:50 +0000838 * recognized.
839 * \return Another negative error code on other kinds of failure.
Janos Follath89ac8c92018-10-30 11:24:05 +0000840 */
841int mbedtls_ecp_tls_read_group_id( mbedtls_ecp_group_id *grp,
Hanno Beckerebffa792018-12-14 15:07:50 +0000842 const unsigned char **buf,
843 size_t len );
Janos Follath89ac8c92018-10-30 11:24:05 +0000844/**
Hanno Beckerebffa792018-12-14 15:07:50 +0000845 * \brief This function exports an elliptic curve as a TLS
846 * ECParameters record as defined in RFC 4492, Section 5.4.
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100847 *
Hanno Beckerebffa792018-12-14 15:07:50 +0000848 * \param grp The ECP group to be exported.
849 * This must be initialized and have group parameters
850 * set, for example through mbedtls_ecp_group_load().
851 * \param olen The address at which to store the number of Bytes written.
852 * This must not be \c NULL.
853 * \param buf The buffer to write to. This must be a writable buffer
854 * of length \p blen Bytes.
855 * \param blen The length of the output buffer \p buf in Bytes.
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100856 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100857 * \return \c 0 on success.
Hanno Beckerebffa792018-12-14 15:07:50 +0000858 * \return #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the output
859 * buffer is too small to hold the exported group.
860 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100861 */
Hanno Beckerebffa792018-12-14 15:07:50 +0000862int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp,
863 size_t *olen,
864 unsigned char *buf, size_t blen );
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100865
866/**
Hanno Becker5edcfa52018-12-18 12:49:55 +0000867 * \brief This function performs a scalar multiplication of a point
Hanno Beckerebffa792018-12-14 15:07:50 +0000868 * by an integer: \p R = \p m * \p P.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100869 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100870 * It is not thread-safe to use same group in multiple threads.
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200871 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100872 * \note To prevent timing attacks, this function
873 * executes the exact same sequence of base-field
874 * operations for any valid \p m. It avoids any if-branch or
875 * array index depending on the value of \p m.
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200876 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100877 * \note If \p f_rng is not NULL, it is used to randomize
878 * intermediate results to prevent potential timing attacks
879 * targeting these results. We recommend always providing
880 * a non-NULL \p f_rng. The overhead is negligible.
Manuel Pégourié-Gonnardb34aeeb2020-06-04 10:20:12 +0200881 * Note: unless #MBEDTLS_ECP_NO_INTERNAL_RNG is defined, when
882 * \p f_rng is NULL, an internal RNG (seeded from the value
883 * of \p m) will be used instead.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100884 *
Hanno Beckerebffa792018-12-14 15:07:50 +0000885 * \param grp The ECP group to use.
886 * This must be initialized and have group parameters
887 * set, for example through mbedtls_ecp_group_load().
888 * \param R The point in which to store the result of the calculation.
889 * This must be initialized.
890 * \param m The integer by which to multiply. This must be initialized.
891 * \param P The point to multiply. This must be initialized.
892 * \param f_rng The RNG function. This may be \c NULL if randomization
893 * of intermediate results isn't desired (discouraged).
894 * \param p_rng The RNG context to be passed to \p p_rng.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100895 *
896 * \return \c 0 on success.
Rose Zadikf56cb342018-04-19 12:49:10 +0100897 * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m is not a valid private
898 * key, or \p P is not a valid public key.
899 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
Hanno Beckerebffa792018-12-14 15:07:50 +0000900 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200901 */
902int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
903 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
904 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
905
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200906/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200907 * \brief This function performs multiplication of a point by
908 * an integer: \p R = \p m * \p P in a restartable way.
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200909 *
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200910 * \see mbedtls_ecp_mul()
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200911 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200912 * \note This function does the same as \c mbedtls_ecp_mul(), but
913 * it can return early and restart according to the limit set
914 * with \c mbedtls_ecp_set_max_ops() to reduce blocking.
915 *
Hanno Beckerebffa792018-12-14 15:07:50 +0000916 * \param grp The ECP group to use.
917 * This must be initialized and have group parameters
918 * set, for example through mbedtls_ecp_group_load().
919 * \param R The point in which to store the result of the calculation.
920 * This must be initialized.
921 * \param m The integer by which to multiply. This must be initialized.
922 * \param P The point to multiply. This must be initialized.
923 * \param f_rng The RNG function. This may be \c NULL if randomization
924 * of intermediate results isn't desired (discouraged).
925 * \param p_rng The RNG context to be passed to \p p_rng.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200926 * \param rs_ctx The restart context (NULL disables restart).
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200927 *
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200928 * \return \c 0 on success.
929 * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m is not a valid private
930 * key, or \p P is not a valid public key.
931 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
932 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnard8467e682017-04-20 09:47:06 +0200933 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Hanno Beckerebffa792018-12-14 15:07:50 +0000934 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100935 */
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200936int mbedtls_ecp_mul_restartable( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200938 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
939 mbedtls_ecp_restart_ctx *rs_ctx );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100940
941/**
Rose Zadika7a61552018-04-24 13:14:01 +0100942 * \brief This function performs multiplication and addition of two
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100943 * points by integers: \p R = \p m * \p P + \p n * \p Q
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200944 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100945 * It is not thread-safe to use same group in multiple threads.
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200946 *
Rose Zadika7a61552018-04-24 13:14:01 +0100947 * \note In contrast to mbedtls_ecp_mul(), this function does not
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100948 * guarantee a constant execution flow and timing.
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200949 *
Hanno Beckerebffa792018-12-14 15:07:50 +0000950 * \param grp The ECP group to use.
951 * This must be initialized and have group parameters
952 * set, for example through mbedtls_ecp_group_load().
953 * \param R The point in which to store the result of the calculation.
954 * This must be initialized.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100955 * \param m The integer by which to multiply \p P.
Hanno Beckerebffa792018-12-14 15:07:50 +0000956 * This must be initialized.
957 * \param P The point to multiply by \p m. This must be initialized.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100958 * \param n The integer by which to multiply \p Q.
Hanno Beckerebffa792018-12-14 15:07:50 +0000959 * This must be initialized.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100960 * \param Q The point to be multiplied by \p n.
Hanno Beckerebffa792018-12-14 15:07:50 +0000961 * This must be initialized.
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200962 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100963 * \return \c 0 on success.
Rose Zadika7a61552018-04-24 13:14:01 +0100964 * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m or \p n are not
965 * valid private keys, or \p P or \p Q are not valid public
Rose Zadikd3c9bfc2018-04-17 10:56:55 +0100966 * keys.
Rose Zadikf56cb342018-04-19 12:49:10 +0100967 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
Hanno Beckerebffa792018-12-14 15:07:50 +0000968 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200969 */
970int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
971 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
972 const mbedtls_mpi *n, const mbedtls_ecp_point *Q );
973
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200974/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200975 * \brief This function performs multiplication and addition of two
976 * points by integers: \p R = \p m * \p P + \p n * \p Q in a
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200977 * restartable way.
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200978 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200979 * \see \c mbedtls_ecp_muladd()
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200980 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200981 * \note This function works the same as \c mbedtls_ecp_muladd(),
982 * but it can return early and restart according to the limit
983 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200984 *
Hanno Beckerebffa792018-12-14 15:07:50 +0000985 * \param grp The ECP group to use.
986 * This must be initialized and have group parameters
987 * set, for example through mbedtls_ecp_group_load().
988 * \param R The point in which to store the result of the calculation.
989 * This must be initialized.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200990 * \param m The integer by which to multiply \p P.
Hanno Beckerebffa792018-12-14 15:07:50 +0000991 * This must be initialized.
992 * \param P The point to multiply by \p m. This must be initialized.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200993 * \param n The integer by which to multiply \p Q.
Hanno Beckerebffa792018-12-14 15:07:50 +0000994 * This must be initialized.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200995 * \param Q The point to be multiplied by \p n.
Hanno Beckerebffa792018-12-14 15:07:50 +0000996 * This must be initialized.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200997 * \param rs_ctx The restart context (NULL disables restart).
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200998 *
999 * \return \c 0 on success.
1000 * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m or \p n are not
1001 * valid private keys, or \p P or \p Q are not valid public
1002 * keys.
1003 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
1004 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02001005 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Hanno Beckerebffa792018-12-14 15:07:50 +00001006 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02001007 */
1008int mbedtls_ecp_muladd_restartable(
1009 mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
1010 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
1011 const mbedtls_mpi *n, const mbedtls_ecp_point *Q,
1012 mbedtls_ecp_restart_ctx *rs_ctx );
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02001013
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02001014/**
Rose Zadika7a61552018-04-24 13:14:01 +01001015 * \brief This function checks that a point is a valid public key
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001016 * on this curve.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001017 *
Rose Zadika7a61552018-04-24 13:14:01 +01001018 * It only checks that the point is non-zero, has
1019 * valid coordinates and lies on the curve. It does not verify
1020 * that it is indeed a multiple of \p G. This additional
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001021 * check is computationally more expensive, is not required
1022 * by standards, and should not be necessary if the group
Rose Zadika7a61552018-04-24 13:14:01 +01001023 * used has a small cofactor. In particular, it is useless for
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001024 * the NIST groups which all have a cofactor of 1.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001025 *
Rose Zadika7a61552018-04-24 13:14:01 +01001026 * \note This function uses bare components rather than an
1027 * ::mbedtls_ecp_keypair structure, to ease use with other
1028 * structures, such as ::mbedtls_ecdh_context or
Rose Zadikb2e111a2018-04-20 10:13:48 +01001029 * ::mbedtls_ecdsa_context.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001030 *
Hanno Beckerebffa792018-12-14 15:07:50 +00001031 * \param grp The ECP group the point should belong to.
1032 * This must be initialized and have group parameters
1033 * set, for example through mbedtls_ecp_group_load().
1034 * \param pt The point to check. This must be initialized.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001035 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001036 * \return \c 0 if the point is a valid public key.
Hanno Beckerebffa792018-12-14 15:07:50 +00001037 * \return #MBEDTLS_ERR_ECP_INVALID_KEY if the point is not
1038 * a valid public key for the given curve.
1039 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001040 */
Hanno Beckerebffa792018-12-14 15:07:50 +00001041int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp,
1042 const mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001043
1044/**
Hanno Beckerebffa792018-12-14 15:07:50 +00001045 * \brief This function checks that an \p mbedtls_mpi is a
1046 * valid private key for this curve.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001047 *
Rose Zadika7a61552018-04-24 13:14:01 +01001048 * \note This function uses bare components rather than an
1049 * ::mbedtls_ecp_keypair structure to ease use with other
1050 * structures, such as ::mbedtls_ecdh_context or
Rose Zadikb2e111a2018-04-20 10:13:48 +01001051 * ::mbedtls_ecdsa_context.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001052 *
Hanno Beckerebffa792018-12-14 15:07:50 +00001053 * \param grp The ECP group the private key should belong to.
1054 * This must be initialized and have group parameters
1055 * set, for example through mbedtls_ecp_group_load().
1056 * \param d The integer to check. This must be initialized.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001057 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001058 * \return \c 0 if the point is a valid private key.
Hanno Beckerebffa792018-12-14 15:07:50 +00001059 * \return #MBEDTLS_ERR_ECP_INVALID_KEY if the point is not a valid
1060 * private key for the given curve.
1061 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001062 */
Hanno Beckerebffa792018-12-14 15:07:50 +00001063int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp,
1064 const mbedtls_mpi *d );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001065
1066/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001067 * \brief This function generates a private key.
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02001068 *
Hanno Beckerebffa792018-12-14 15:07:50 +00001069 * \param grp The ECP group to generate a private key for.
1070 * This must be initialized and have group parameters
1071 * set, for example through mbedtls_ecp_group_load().
1072 * \param d The destination MPI (secret part). This must be initialized.
1073 * \param f_rng The RNG function. This must not be \c NULL.
1074 * \param p_rng The RNG parameter to be passed to \p f_rng. This may be
1075 * \c NULL if \p f_rng doesn't need a context argument.
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02001076 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001077 * \return \c 0 on success.
1078 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
1079 * on failure.
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02001080 */
1081int mbedtls_ecp_gen_privkey( const mbedtls_ecp_group *grp,
1082 mbedtls_mpi *d,
1083 int (*f_rng)(void *, unsigned char *, size_t),
1084 void *p_rng );
1085
1086/**
Rose Zadika7a61552018-04-24 13:14:01 +01001087 * \brief This function generates a keypair with a configurable base
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001088 * point.
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +02001089 *
Rose Zadika7a61552018-04-24 13:14:01 +01001090 * \note This function uses bare components rather than an
1091 * ::mbedtls_ecp_keypair structure to ease use with other
1092 * structures, such as ::mbedtls_ecdh_context or
Rose Zadikb2e111a2018-04-20 10:13:48 +01001093 * ::mbedtls_ecdsa_context.
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +02001094 *
Hanno Beckerebffa792018-12-14 15:07:50 +00001095 * \param grp The ECP group to generate a key pair for.
1096 * This must be initialized and have group parameters
1097 * set, for example through mbedtls_ecp_group_load().
1098 * \param G The base point to use. This must be initialized
1099 * and belong to \p grp. It replaces the default base
1100 * point \c grp->G used by mbedtls_ecp_gen_keypair().
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001101 * \param d The destination MPI (secret part).
Hanno Beckerebffa792018-12-14 15:07:50 +00001102 * This must be initialized.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001103 * \param Q The destination point (public part).
Hanno Beckerebffa792018-12-14 15:07:50 +00001104 * This must be initialized.
1105 * \param f_rng The RNG function. This must not be \c NULL.
1106 * \param p_rng The RNG context to be passed to \p f_rng. This may
1107 * be \c NULL if \p f_rng doesn't need a context argument.
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +02001108 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001109 * \return \c 0 on success.
1110 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
1111 * on failure.
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +02001112 */
1113int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp,
Hanno Beckerebffa792018-12-14 15:07:50 +00001114 const mbedtls_ecp_point *G,
1115 mbedtls_mpi *d, mbedtls_ecp_point *Q,
1116 int (*f_rng)(void *, unsigned char *, size_t),
1117 void *p_rng );
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +02001118
1119/**
Rose Zadikf56cb342018-04-19 12:49:10 +01001120 * \brief This function generates an ECP keypair.
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001121 *
Rose Zadika7a61552018-04-24 13:14:01 +01001122 * \note This function uses bare components rather than an
1123 * ::mbedtls_ecp_keypair structure to ease use with other
1124 * structures, such as ::mbedtls_ecdh_context or
Rose Zadikb2e111a2018-04-20 10:13:48 +01001125 * ::mbedtls_ecdsa_context.
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001126 *
Hanno Beckerebffa792018-12-14 15:07:50 +00001127 * \param grp The ECP group to generate a key pair for.
1128 * This must be initialized and have group parameters
1129 * set, for example through mbedtls_ecp_group_load().
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001130 * \param d The destination MPI (secret part).
Hanno Beckerebffa792018-12-14 15:07:50 +00001131 * This must be initialized.
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001132 * \param Q The destination point (public part).
Hanno Beckerebffa792018-12-14 15:07:50 +00001133 * This must be initialized.
1134 * \param f_rng The RNG function. This must not be \c NULL.
1135 * \param p_rng The RNG context to be passed to \p f_rng. This may
1136 * be \c NULL if \p f_rng doesn't need a context argument.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001137 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001138 * \return \c 0 on success.
1139 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
1140 * on failure.
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001141 */
Hanno Beckerebffa792018-12-14 15:07:50 +00001142int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp, mbedtls_mpi *d,
1143 mbedtls_ecp_point *Q,
1144 int (*f_rng)(void *, unsigned char *, size_t),
1145 void *p_rng );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001146
1147/**
Rose Zadikf56cb342018-04-19 12:49:10 +01001148 * \brief This function generates an ECP key.
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01001149 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001150 * \param grp_id The ECP group identifier.
Hanno Beckerebffa792018-12-14 15:07:50 +00001151 * \param key The destination key. This must be initialized.
1152 * \param f_rng The RNG function to use. This must not be \c NULL.
1153 * \param p_rng The RNG context to be passed to \p f_rng. This may
1154 * be \c NULL if \p f_rng doesn't need a context argument.
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01001155 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001156 * \return \c 0 on success.
1157 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
1158 * on failure.
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01001159 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
Hanno Beckerebffa792018-12-14 15:07:50 +00001161 int (*f_rng)(void *, unsigned char *, size_t),
1162 void *p_rng );
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01001163
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01001164/**
Rose Zadika7a61552018-04-24 13:14:01 +01001165 * \brief This function checks that the keypair objects
Rose Zadikb2e111a2018-04-20 10:13:48 +01001166 * \p pub and \p prv have the same group and the
1167 * same public point, and that the private key in
1168 * \p prv is consistent with the public key.
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01001169 *
Hanno Beckerebffa792018-12-14 15:07:50 +00001170 * \param pub The keypair structure holding the public key. This
1171 * must be initialized. If it contains a private key, that
1172 * part is ignored.
Rose Zadikb2e111a2018-04-20 10:13:48 +01001173 * \param prv The keypair structure holding the full keypair.
Hanno Beckerebffa792018-12-14 15:07:50 +00001174 * This must be initialized.
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01001175 *
Rose Zadika7a61552018-04-24 13:14:01 +01001176 * \return \c 0 on success, meaning that the keys are valid and match.
Rose Zadikb2e111a2018-04-20 10:13:48 +01001177 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the keys are invalid or do not match.
Rose Zadika7a61552018-04-24 13:14:01 +01001178 * \return An \c MBEDTLS_ERR_ECP_XXX or an \c MBEDTLS_ERR_MPI_XXX
Rose Zadikb2e111a2018-04-20 10:13:48 +01001179 * error code on calculation failure.
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01001180 */
Hanno Beckerebffa792018-12-14 15:07:50 +00001181int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub,
1182 const mbedtls_ecp_keypair *prv );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01001183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184#if defined(MBEDTLS_SELF_TEST)
Janos Follathb0697532016-08-18 12:38:46 +01001185
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01001186/**
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001187 * \brief The ECP checkup routine.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001188 *
Rose Zadikd3c9bfc2018-04-17 10:56:55 +01001189 * \return \c 0 on success.
1190 * \return \c 1 on failure.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001191 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192int mbedtls_ecp_self_test( int verbose );
Janos Follathb0697532016-08-18 12:38:46 +01001193
Janos Follath372697b2016-10-28 16:53:11 +01001194#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001195
1196#ifdef __cplusplus
1197}
1198#endif
1199
Paul Bakker9af723c2014-05-01 13:03:14 +02001200#endif /* ecp.h */