blob: 82052f33424093f4b22e6c449f64a0b15c1f5b76 [file] [log] [blame]
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001/**
2 * \file ecp.h
3 *
4 * \brief Elliptic curves over GF(p)
Darryl Greena40a1012018-01-05 15:33:17 +00005 */
6/*
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02007 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
9 *
10 * This file is provided under the Apache License 2.0, or the
11 * GNU General Public License v2.0 or later.
12 *
13 * **********
14 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020015 *
16 * Licensed under the Apache License, Version 2.0 (the "License"); you may
17 * not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
19 *
20 * http://www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010027 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020028 * **********
29 *
30 * **********
31 * GNU General Public License v2.0 or later:
32 *
33 * This program is free software; you can redistribute it and/or modify
34 * it under the terms of the GNU General Public License as published by
35 * the Free Software Foundation; either version 2 of the License, or
36 * (at your option) any later version.
37 *
38 * This program is distributed in the hope that it will be useful,
39 * but WITHOUT ANY WARRANTY; without even the implied warranty of
40 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41 * GNU General Public License for more details.
42 *
43 * You should have received a copy of the GNU General Public License along
44 * with this program; if not, write to the Free Software Foundation, Inc.,
45 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
46 *
47 * **********
48 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000049 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010050 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#ifndef MBEDTLS_ECP_H
52#define MBEDTLS_ECP_H
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010053
Ron Eldor0559c662018-02-14 16:02:41 +020054#if !defined(MBEDTLS_CONFIG_FILE)
55#include "config.h"
56#else
57#include MBEDTLS_CONFIG_FILE
58#endif
59
Manuel Pégourié-Gonnardbdc96762013-10-03 11:50:39 +020060#include "bignum.h"
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010061
62/*
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +010063 * ECP error codes
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010064 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#define MBEDTLS_ERR_ECP_BAD_INPUT_DATA -0x4F80 /**< Bad input parameters to function. */
66#define MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL -0x4F00 /**< The buffer is too small to write to. */
67#define MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE -0x4E80 /**< Requested curve not available. */
68#define MBEDTLS_ERR_ECP_VERIFY_FAILED -0x4E00 /**< The signature is not valid. */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +020069#define MBEDTLS_ERR_ECP_ALLOC_FAILED -0x4D80 /**< Memory allocation failed. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#define MBEDTLS_ERR_ECP_RANDOM_FAILED -0x4D00 /**< Generation of random value, such as (ephemeral) key, failed. */
71#define MBEDTLS_ERR_ECP_INVALID_KEY -0x4C80 /**< Invalid private or public key. */
Gilles Peskine5114d3e2018-03-30 07:12:15 +020072#define MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH -0x4C00 /**< The buffer contains a valid signature followed by more data. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010073#define MBEDTLS_ERR_ECP_HW_ACCEL_FAILED -0x4B80 /**< ECP hardware accelerator failed. */
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +010074
Janos Follathb0697532016-08-18 12:38:46 +010075#if !defined(MBEDTLS_ECP_ALT)
Janos Follathc44ab972016-11-18 16:38:23 +000076/*
77 * default mbed TLS elliptic curve arithmetic implementation
78 *
79 * (in case MBEDTLS_ECP_ALT is defined then the developer has to provide an
80 * alternative implementation for the whole module and it will replace this
81 * one.)
82 */
Janos Follathb0697532016-08-18 12:38:46 +010083
Paul Bakker407a0da2013-06-27 14:29:21 +020084#ifdef __cplusplus
85extern "C" {
86#endif
87
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010088/**
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +020089 * Domain parameters (curve, subgroup and generator) identifiers.
90 *
91 * Only curves over prime fields are supported.
92 *
93 * \warning This library does not support validation of arbitrary domain
94 * parameters. Therefore, only well-known domain parameters from trusted
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +020095 * sources should be used. See mbedtls_ecp_group_load().
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +020096 */
97typedef enum
98{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 MBEDTLS_ECP_DP_NONE = 0,
100 MBEDTLS_ECP_DP_SECP192R1, /*!< 192-bits NIST curve */
101 MBEDTLS_ECP_DP_SECP224R1, /*!< 224-bits NIST curve */
102 MBEDTLS_ECP_DP_SECP256R1, /*!< 256-bits NIST curve */
103 MBEDTLS_ECP_DP_SECP384R1, /*!< 384-bits NIST curve */
104 MBEDTLS_ECP_DP_SECP521R1, /*!< 521-bits NIST curve */
105 MBEDTLS_ECP_DP_BP256R1, /*!< 256-bits Brainpool curve */
106 MBEDTLS_ECP_DP_BP384R1, /*!< 384-bits Brainpool curve */
107 MBEDTLS_ECP_DP_BP512R1, /*!< 512-bits Brainpool curve */
Manuel Pégourié-Gonnard07894332015-06-23 00:18:41 +0200108 MBEDTLS_ECP_DP_CURVE25519, /*!< Curve25519 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 MBEDTLS_ECP_DP_SECP192K1, /*!< 192-bits "Koblitz" curve */
110 MBEDTLS_ECP_DP_SECP224K1, /*!< 224-bits "Koblitz" curve */
111 MBEDTLS_ECP_DP_SECP256K1, /*!< 256-bits "Koblitz" curve */
112} mbedtls_ecp_group_id;
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200113
114/**
Manuel Pégourié-Gonnard66153662013-12-03 14:12:26 +0100115 * Number of supported curves (plus one for NONE).
116 *
117 * (Montgomery curves excluded for now.)
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200118 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119#define MBEDTLS_ECP_DP_MAX 12
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200120
121/**
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200122 * Curve information for use by other modules
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200123 */
124typedef struct
125{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 mbedtls_ecp_group_id grp_id; /*!< Internal identifier */
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200127 uint16_t tls_id; /*!< TLS NamedCurve identifier */
128 uint16_t bit_size; /*!< Curve size in bits */
129 const char *name; /*!< Human-friendly name */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130} mbedtls_ecp_curve_info;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200131
132/**
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100133 * \brief ECP point structure (jacobian coordinates)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100134 *
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100135 * \note All functions expect and return points satisfying
136 * the following condition: Z == 0 or Z == 1. (Other
137 * values of Z are used by internal functions only.)
138 * The point is zero, or "at infinity", if Z == 0.
139 * Otherwise, X and Y are its standard (affine) coordinates.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100140 */
141typedef struct
142{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 mbedtls_mpi X; /*!< the point's X coordinate */
144 mbedtls_mpi Y; /*!< the point's Y coordinate */
145 mbedtls_mpi Z; /*!< the point's Z coordinate */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100146}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147mbedtls_ecp_point;
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100148
149/**
150 * \brief ECP group structure
151 *
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100152 * We consider two types of curves equations:
153 * 1. Short Weierstrass y^2 = x^3 + A x + B mod P (SEC1 + RFC 4492)
Manuel Pégourié-Gonnard07894332015-06-23 00:18:41 +0200154 * 2. Montgomery, y^2 = x^3 + A x^2 + x mod P (Curve25519 + draft)
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100155 * In both cases, a generator G for a prime-order subgroup is fixed. In the
156 * short weierstrass, this subgroup is actually the whole curve, and its
157 * cardinal is denoted by N.
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100158 *
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200159 * In the case of Short Weierstrass curves, our code requires that N is an odd
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 * prime. (Use odd in mbedtls_ecp_mul() and prime in mbedtls_ecdsa_sign() for blinding.)
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200161 *
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100162 * In the case of Montgomery curves, we don't store A but (A + 2) / 4 which is
Paul Bakker75342a62014-04-08 17:35:40 +0200163 * the quantity actually used in the formulas. Also, nbits is not the size of N
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100164 * but the required size for private keys.
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100165 *
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200166 * If modp is NULL, reduction modulo P is done using a generic algorithm.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 * Otherwise, it must point to a function that takes an mbedtls_mpi in the range
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200168 * 0..2^(2*pbits)-1 and transforms it in-place in an integer of little more
169 * than pbits, so that the integer may be efficiently brought in the 0..P-1
170 * range by a few additions or substractions. It must return 0 on success and
171 * non-zero on failure.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100172 */
173typedef struct
174{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 mbedtls_ecp_group_id id; /*!< internal group identifier */
176 mbedtls_mpi P; /*!< prime modulus of the base field */
177 mbedtls_mpi A; /*!< 1. A in the equation, or 2. (A + 2) / 4 */
178 mbedtls_mpi B; /*!< 1. B in the equation, or 2. unused */
179 mbedtls_ecp_point G; /*!< generator of the (sub)group used */
180 mbedtls_mpi N; /*!< 1. the order of G, or 2. unused */
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +0200181 size_t pbits; /*!< number of bits in P */
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100182 size_t nbits; /*!< number of bits in 1. P, or 2. private keys */
Manuel Pégourié-Gonnard1f82b042013-12-06 12:51:50 +0100183 unsigned int h; /*!< internal: 1 if the constants are static */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 int (*modp)(mbedtls_mpi *); /*!< function for fast reduction mod P */
185 int (*t_pre)(mbedtls_ecp_point *, void *); /*!< unused */
186 int (*t_post)(mbedtls_ecp_point *, void *); /*!< unused */
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100187 void *t_data; /*!< unused */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 mbedtls_ecp_point *T; /*!< pre-computed points for ecp_mul_comb() */
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +0200189 size_t T_size; /*!< number for pre-computed points */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100190}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191mbedtls_ecp_group;
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100192
193/**
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200194 * \brief ECP key pair structure
195 *
196 * A generic key pair that could be used for ECDSA, fixed ECDH, etc.
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200197 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 * \note Members purposefully in the same order as struc mbedtls_ecdsa_context.
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200199 */
200typedef struct
201{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 mbedtls_ecp_group grp; /*!< Elliptic curve and base point */
203 mbedtls_mpi d; /*!< our secret value */
204 mbedtls_ecp_point Q; /*!< our public value */
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200205}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206mbedtls_ecp_keypair;
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200207
Paul Bakker088c5c52014-04-25 11:11:10 +0200208/**
209 * \name SECTION: Module settings
210 *
211 * The configuration options you can set for this module are in this section.
212 * Either change them in config.h or define them on the compiler command line.
213 * \{
214 */
215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216#if !defined(MBEDTLS_ECP_MAX_BITS)
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200217/**
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200218 * Maximum size of the groups (that is, of N and P)
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100219 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220#define MBEDTLS_ECP_MAX_BITS 521 /**< Maximum bit size of groups */
Paul Bakkere1b665e2013-12-11 16:02:58 +0100221#endif
222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223#define MBEDTLS_ECP_MAX_BYTES ( ( MBEDTLS_ECP_MAX_BITS + 7 ) / 8 )
224#define MBEDTLS_ECP_MAX_PT_LEN ( 2 * MBEDTLS_ECP_MAX_BYTES + 1 )
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226#if !defined(MBEDTLS_ECP_WINDOW_SIZE)
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100227/*
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +0100228 * Maximum "window" size used for point multiplication.
229 * Default: 6.
230 * Minimum value: 2. Maximum value: 7.
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100231 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 * Result is an array of at most ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) )
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +0100233 * points used for point multiplication. This value is directly tied to EC
234 * peak memory usage, so decreasing it by one should roughly cut memory usage
235 * by two (if large curves are in use).
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100236 *
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +0100237 * Reduction in size may reduce speed, but larger curves are impacted first.
238 * Sample performances (in ECDHE handshakes/s, with FIXED_POINT_OPTIM = 1):
239 * w-size: 6 5 4 3 2
240 * 521 145 141 135 120 97
241 * 384 214 209 198 177 146
242 * 256 320 320 303 262 226
Paul Bakker088c5c52014-04-25 11:11:10 +0200243
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +0100244 * 224 475 475 453 398 342
245 * 192 640 640 633 587 476
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100246 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247#define MBEDTLS_ECP_WINDOW_SIZE 6 /**< Maximum window size used */
248#endif /* MBEDTLS_ECP_WINDOW_SIZE */
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +0100249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250#if !defined(MBEDTLS_ECP_FIXED_POINT_OPTIM)
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +0100251/*
252 * Trade memory for speed on fixed-point multiplication.
253 *
254 * This speeds up repeated multiplication of the generator (that is, the
255 * multiplication in ECDSA signatures, and half of the multiplications in
256 * ECDSA verification and ECDHE) by a factor roughly 3 to 4.
257 *
258 * The cost is increasing EC peak memory usage by a factor roughly 2.
259 *
260 * Change this value to 0 to reduce peak memory usage.
261 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262#define MBEDTLS_ECP_FIXED_POINT_OPTIM 1 /**< Enable fixed-point speed-up */
263#endif /* MBEDTLS_ECP_FIXED_POINT_OPTIM */
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100264
Paul Bakker088c5c52014-04-25 11:11:10 +0200265/* \} name SECTION: Module settings */
266
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100267/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100268 * Point formats, from RFC 4492's enum ECPointFormat
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100269 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270#define MBEDTLS_ECP_PF_UNCOMPRESSED 0 /**< Uncompressed point format */
271#define MBEDTLS_ECP_PF_COMPRESSED 1 /**< Compressed point format */
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100272
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100273/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100274 * Some other constants from RFC 4492
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100275 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276#define MBEDTLS_ECP_TLS_NAMED_CURVE 3 /**< ECCurveType's named_curve */
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100277
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100278/**
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100279 * \brief Get the list of supported curves in order of preferrence
280 * (full information)
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200281 *
282 * \return A statically allocated array, the last entry is 0.
283 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list( void );
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200285
286/**
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100287 * \brief Get the list of supported curves in order of preferrence
288 * (grp_id only)
289 *
290 * \return A statically allocated array,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 * terminated with MBEDTLS_ECP_DP_NONE.
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100292 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list( void );
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100294
295/**
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200296 * \brief Get curve information from an internal group identifier
297 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 * \param grp_id A MBEDTLS_ECP_DP_XXX value
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200299 *
300 * \return The associated curve information or NULL
301 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302const 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 +0200303
304/**
305 * \brief Get curve information from a TLS NamedCurve value
306 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 * \param tls_id A MBEDTLS_ECP_DP_XXX value
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200308 *
309 * \return The associated curve information or NULL
310 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id( uint16_t tls_id );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200312
313/**
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100314 * \brief Get curve information from a human-readable name
315 *
316 * \param name The name
317 *
318 * \return The associated curve information or NULL
319 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name( const char *name );
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100321
322/**
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100323 * \brief Initialize a point (as zero)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100324 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325void mbedtls_ecp_point_init( mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100326
327/**
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100328 * \brief Initialize a group (to something meaningless)
329 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330void mbedtls_ecp_group_init( mbedtls_ecp_group *grp );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100331
332/**
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200333 * \brief Initialize a key pair (as an invalid one)
334 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335void mbedtls_ecp_keypair_init( mbedtls_ecp_keypair *key );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200336
337/**
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100338 * \brief Free the components of a point
339 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340void mbedtls_ecp_point_free( mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100341
342/**
343 * \brief Free the components of an ECP group
344 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345void mbedtls_ecp_group_free( mbedtls_ecp_group *grp );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100346
347/**
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200348 * \brief Free the components of a key pair
349 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair *key );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200351
352/**
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100353 * \brief Copy the contents of point Q into P
354 *
355 * \param P Destination point
356 * \param Q Source point
357 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100358 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200359 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100360 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361int mbedtls_ecp_copy( mbedtls_ecp_point *P, const mbedtls_ecp_point *Q );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100362
363/**
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200364 * \brief Copy the contents of a group object
365 *
366 * \param dst Destination group
367 * \param src Source group
368 *
369 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200370 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200371 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst, const mbedtls_ecp_group *src );
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200373
374/**
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200375 * \brief Set a point to zero
376 *
377 * \param pt Destination point
378 *
379 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200380 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200381 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382int mbedtls_ecp_set_zero( mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200383
384/**
385 * \brief Tell if a point is zero
386 *
387 * \param pt Point to test
388 *
389 * \return 1 if point is zero, 0 otherwise
390 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200392
393/**
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200394 * \brief Compare two points
395 *
396 * \note This assumes the points are normalized. Otherwise,
397 * they may compare as "not equal" even if they are.
398 *
399 * \param P First point to compare
400 * \param Q Second point to compare
401 *
402 * \return 0 if the points are equal,
403 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA otherwise
404 */
405int mbedtls_ecp_point_cmp( const mbedtls_ecp_point *P,
406 const mbedtls_ecp_point *Q );
407
408/**
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100409 * \brief Import a non-zero point from two ASCII strings
410 *
411 * \param P Destination point
412 * \param radix Input numeric base
413 * \param x First affine coordinate as a null-terminated string
414 * \param y Second affine coordinate as a null-terminated string
415 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100417 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418int mbedtls_ecp_point_read_string( mbedtls_ecp_point *P, int radix,
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100419 const char *x, const char *y );
420
421/**
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100422 * \brief Export a point into unsigned binary data
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100423 *
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100424 * \param grp Group to which the point should belong
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100425 * \param P Point to export
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 * \param format Point format, should be a MBEDTLS_ECP_PF_XXX macro
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100427 * \param olen Length of the actual output
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100428 * \param buf Output buffer
429 * \param buflen Length of the output buffer
430 *
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100431 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 * or MBEDTLS_ERR_ECP_BAD_INPUT_DATA
433 * or MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100434 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100436 int format, size_t *olen,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100437 unsigned char *buf, size_t buflen );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100438
439/**
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100440 * \brief Import a point from unsigned binary data
441 *
442 * \param grp Group to which the point should belong
443 * \param P Point to import
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100444 * \param buf Input buffer
445 * \param ilen Actual length of input
446 *
447 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200449 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 * MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the point format
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100451 * is not implemented.
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100452 *
453 * \note This function does NOT check that the point actually
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 * belongs to the given group, see mbedtls_ecp_check_pubkey() for
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100455 * that.
456 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100458 const unsigned char *buf, size_t ilen );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100459
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100460/**
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200461 * \brief Import a point from a TLS ECPoint record
462 *
463 * \param grp ECP group used
464 * \param pt Destination point
465 * \param buf $(Start of input buffer)
466 * \param len Buffer length
467 *
Manuel Pégourié-Gonnard150c4f62014-11-21 09:14:52 +0100468 * \note buf is updated to point right after the ECPoint on exit
469 *
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +0200470 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471 * MBEDTLS_ERR_MPI_XXX if initialization failed
472 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200473 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200475 const unsigned char **buf, size_t len );
476
477/**
478 * \brief Export a point as a TLS ECPoint record
479 *
480 * \param grp ECP group used
481 * \param pt Point to export
482 * \param format Export format
483 * \param olen length of data written
484 * \param buf Buffer to write to
485 * \param blen Buffer length
486 *
487 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 * or MBEDTLS_ERR_ECP_BAD_INPUT_DATA
489 * or MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200490 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491int mbedtls_ecp_tls_write_point( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt,
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200492 int format, size_t *olen,
493 unsigned char *buf, size_t blen );
494
495/**
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100496 * \brief Set a group using well-known domain parameters
497 *
498 * \param grp Destination group
Hanno Becker61937d42017-04-26 15:01:23 +0100499 * \param id Index in the list of well-known domain parameters
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100500 *
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +0200501 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 * MBEDTLS_ERR_MPI_XXX if initialization failed
503 * MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE for unkownn groups
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100504 *
Manuel Pégourié-Gonnardaff37e52015-05-11 18:11:57 +0200505 * \note Index should be a value of RFC 4492's enum NamedCurve,
506 * usually in the form of a MBEDTLS_ECP_DP_XXX macro.
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100507 */
Hanno Becker61937d42017-04-26 15:01:23 +0100508int mbedtls_ecp_group_load( mbedtls_ecp_group *grp, mbedtls_ecp_group_id id );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100509
510/**
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100511 * \brief Set a group from a TLS ECParameters record
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100512 *
513 * \param grp Destination group
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100514 * \param buf &(Start of input buffer)
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100515 * \param len Buffer length
516 *
Manuel Pégourié-Gonnard150c4f62014-11-21 09:14:52 +0100517 * \note buf is updated to point right after ECParameters on exit
518 *
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +0200519 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 * MBEDTLS_ERR_MPI_XXX if initialization failed
521 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100522 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523int mbedtls_ecp_tls_read_group( mbedtls_ecp_group *grp, const unsigned char **buf, size_t len );
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100524
525/**
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100526 * \brief Write the TLS ECParameters record for a group
527 *
528 * \param grp ECP group used
529 * \param olen Number of bytes actually written
530 * \param buf Buffer to write to
531 * \param blen Buffer length
532 *
533 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 * or MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100535 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, size_t *olen,
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100537 unsigned char *buf, size_t blen );
538
539/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100540 * \brief Multiplication by an integer: R = m * P
Paul Bakker6838bd12013-09-30 13:56:38 +0200541 * (Not thread-safe to use same group in multiple threads)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100542 *
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200543 * \note In order to prevent timing attacks, this function
544 * executes the exact same sequence of (base field)
545 * operations for any valid m. It avoids any if-branch or
546 * array index depending on the value of m.
547 *
548 * \note If f_rng is not NULL, it is used to randomize intermediate
549 * results in order to prevent potential timing attacks
550 * targeting these results. It is recommended to always
551 * provide a non-NULL f_rng (the overhead is negligible).
552 *
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100553 * \param grp ECP group
554 * \param R Destination point
555 * \param m Integer by which to multiply
556 * \param P Point to multiply
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200557 * \param f_rng RNG function (see notes)
558 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100559 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100560 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 * MBEDTLS_ERR_ECP_INVALID_KEY if m is not a valid privkey
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +0100562 * or P is not a valid pubkey,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200563 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100564 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
566 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard09ceaf42013-11-20 23:06:14 +0100567 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100568
569/**
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200570 * \brief Multiplication and addition of two points by integers:
571 * R = m * P + n * Q
572 * (Not thread-safe to use same group in multiple threads)
573 *
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +0200574 * \note In contrast to mbedtls_ecp_mul(), this function does not guarantee
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200575 * a constant execution flow and timing.
576 *
577 * \param grp ECP group
578 * \param R Destination point
579 * \param m Integer by which to multiply P
580 * \param P Point to multiply by m
581 * \param n Integer by which to multiply Q
582 * \param Q Point to be multiplied by n
583 *
584 * \return 0 if successful,
585 * MBEDTLS_ERR_ECP_INVALID_KEY if m or n is not a valid privkey
586 * or P or Q is not a valid pubkey,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200587 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200588 */
589int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
590 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
591 const mbedtls_mpi *n, const mbedtls_ecp_point *Q );
592
593/**
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200594 * \brief Check that a point is a valid public key on this curve
595 *
596 * \param grp Curve/group the point should belong to
597 * \param pt Point to check
598 *
599 * \return 0 if point is a valid public key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 * MBEDTLS_ERR_ECP_INVALID_KEY otherwise.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200601 *
602 * \note This function only checks the point is non-zero, has valid
603 * coordinates and lies on the curve, but not that it is
604 * indeed a multiple of G. This is additional check is more
605 * expensive, isn't required by standards, and shouldn't be
606 * necessary if the group used has a small cofactor. In
607 * particular, it is useless for the NIST groups which all
608 * have a cofactor of 1.
609 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 * \note Uses bare components rather than an mbedtls_ecp_keypair structure
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200611 * in order to ease use with other structures such as
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 * mbedtls_ecdh_context of mbedtls_ecdsa_context.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200613 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200615
616/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 * \brief Check that an mbedtls_mpi is a valid private key for this curve
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200618 *
619 * \param grp Group used
620 * \param d Integer to check
621 *
622 * \return 0 if point is a valid private key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 * MBEDTLS_ERR_ECP_INVALID_KEY otherwise.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200624 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 * \note Uses bare components rather than an mbedtls_ecp_keypair structure
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200626 * in order to ease use with other structures such as
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 * mbedtls_ecdh_context of mbedtls_ecdsa_context.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200628 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp, const mbedtls_mpi *d );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200630
631/**
Manuel Pégourié-Gonnardc80555d2017-04-20 15:37:46 +0200632 * \brief Generate a private key
633 *
634 * \param grp ECP group
635 * \param d Destination MPI (secret part)
636 * \param f_rng RNG function
637 * \param p_rng RNG parameter
638 *
639 * \return 0 if successful,
640 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
641 */
642int mbedtls_ecp_gen_privkey( const mbedtls_ecp_group *grp,
643 mbedtls_mpi *d,
644 int (*f_rng)(void *, unsigned char *, size_t),
645 void *p_rng );
646
647/**
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +0200648 * \brief Generate a keypair with configurable base point
649 *
650 * \param grp ECP group
651 * \param G Chosen base point
652 * \param d Destination MPI (secret part)
653 * \param Q Destination point (public part)
654 * \param f_rng RNG function
655 * \param p_rng RNG parameter
656 *
657 * \return 0 if successful,
658 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
659 *
660 * \note Uses bare components rather than an mbedtls_ecp_keypair structure
661 * in order to ease use with other structures such as
662 * mbedtls_ecdh_context of mbedtls_ecdsa_context.
663 */
664int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp,
665 const mbedtls_ecp_point *G,
666 mbedtls_mpi *d, mbedtls_ecp_point *Q,
667 int (*f_rng)(void *, unsigned char *, size_t),
668 void *p_rng );
669
670/**
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100671 * \brief Generate a keypair
672 *
673 * \param grp ECP group
674 * \param d Destination MPI (secret part)
675 * \param Q Destination point (public part)
676 * \param f_rng RNG function
677 * \param p_rng RNG parameter
678 *
679 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200681 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 * \note Uses bare components rather than an mbedtls_ecp_keypair structure
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200683 * in order to ease use with other structures such as
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 * mbedtls_ecdh_context of mbedtls_ecdsa_context.
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100685 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100687 int (*f_rng)(void *, unsigned char *, size_t),
688 void *p_rng );
689
690/**
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +0100691 * \brief Generate a keypair
692 *
693 * \param grp_id ECP group identifier
694 * \param key Destination keypair
695 * \param f_rng RNG function
696 * \param p_rng RNG parameter
697 *
698 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +0100700 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +0100702 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
703
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +0100704/**
705 * \brief Check a public-private key pair
706 *
707 * \param pub Keypair structure holding a public key
708 * \param prv Keypair structure holding a private (plus public) key
709 *
Manuel Pégourié-Gonnard862d5032015-04-15 11:30:46 +0200710 * \return 0 if successful (keys are valid and match), or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA, or
712 * a MBEDTLS_ERR_ECP_XXX or MBEDTLS_ERR_MPI_XXX code.
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +0100713 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +0100715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716#if defined(MBEDTLS_SELF_TEST)
Janos Follathb0697532016-08-18 12:38:46 +0100717
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +0100718/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100719 * \brief Checkup routine
720 *
Manuel Pégourié-Gonnard13a1ef82014-03-27 20:12:44 +0100721 * \return 0 if successful, or 1 if a test failed
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100722 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723int mbedtls_ecp_self_test( int verbose );
Janos Follathb0697532016-08-18 12:38:46 +0100724
Janos Follath372697b2016-10-28 16:53:11 +0100725#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100726
727#ifdef __cplusplus
728}
729#endif
730
Janos Follathb0697532016-08-18 12:38:46 +0100731#else /* MBEDTLS_ECP_ALT */
732#include "ecp_alt.h"
733#endif /* MBEDTLS_ECP_ALT */
734
Paul Bakker9af723c2014-05-01 13:03:14 +0200735#endif /* ecp.h */