blob: 7bcc69c0174f14328bc31f304154ef80a8f457a2 [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)
5 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010020 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000021 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#ifndef MBEDTLS_ECP_H
24#define MBEDTLS_ECP_H
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010025
Manuel Pégourié-Gonnardbdc96762013-10-03 11:50:39 +020026#include "bignum.h"
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010027
28/*
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +010029 * ECP error codes
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010030 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#define MBEDTLS_ERR_ECP_BAD_INPUT_DATA -0x4F80 /**< Bad input parameters to function. */
32#define MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL -0x4F00 /**< The buffer is too small to write to. */
33#define MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE -0x4E80 /**< Requested curve not available. */
34#define MBEDTLS_ERR_ECP_VERIFY_FAILED -0x4E00 /**< The signature is not valid. */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +020035#define MBEDTLS_ERR_ECP_ALLOC_FAILED -0x4D80 /**< Memory allocation failed. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#define MBEDTLS_ERR_ECP_RANDOM_FAILED -0x4D00 /**< Generation of random value, such as (ephemeral) key, failed. */
37#define MBEDTLS_ERR_ECP_INVALID_KEY -0x4C80 /**< Invalid private or public key. */
38#define MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH -0x4C00 /**< Signature is valid but shorter than the user-supplied length. */
Manuel Pégourié-Gonnard5e3c62f2017-03-08 10:14:11 +010039#define MBEDTLS_ERR_ECP_IN_PROGRESS -0x4B80 /**< Operation in progress, try again with the same parameters. */
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +010040
Janos Follathb0697532016-08-18 12:38:46 +010041#if !defined(MBEDTLS_ECP_ALT)
Janos Follathc44ab972016-11-18 16:38:23 +000042/*
43 * default mbed TLS elliptic curve arithmetic implementation
44 *
45 * (in case MBEDTLS_ECP_ALT is defined then the developer has to provide an
46 * alternative implementation for the whole module and it will replace this
47 * one.)
48 */
Janos Follathb0697532016-08-18 12:38:46 +010049
Paul Bakker407a0da2013-06-27 14:29:21 +020050#ifdef __cplusplus
51extern "C" {
52#endif
53
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010054/**
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +020055 * Domain parameters (curve, subgroup and generator) identifiers.
56 *
57 * Only curves over prime fields are supported.
58 *
59 * \warning This library does not support validation of arbitrary domain
60 * parameters. Therefore, only well-known domain parameters from trusted
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +020061 * sources should be used. See mbedtls_ecp_group_load().
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +020062 */
63typedef enum
64{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065 MBEDTLS_ECP_DP_NONE = 0,
66 MBEDTLS_ECP_DP_SECP192R1, /*!< 192-bits NIST curve */
67 MBEDTLS_ECP_DP_SECP224R1, /*!< 224-bits NIST curve */
68 MBEDTLS_ECP_DP_SECP256R1, /*!< 256-bits NIST curve */
69 MBEDTLS_ECP_DP_SECP384R1, /*!< 384-bits NIST curve */
70 MBEDTLS_ECP_DP_SECP521R1, /*!< 521-bits NIST curve */
71 MBEDTLS_ECP_DP_BP256R1, /*!< 256-bits Brainpool curve */
72 MBEDTLS_ECP_DP_BP384R1, /*!< 384-bits Brainpool curve */
73 MBEDTLS_ECP_DP_BP512R1, /*!< 512-bits Brainpool curve */
Manuel Pégourié-Gonnard07894332015-06-23 00:18:41 +020074 MBEDTLS_ECP_DP_CURVE25519, /*!< Curve25519 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 MBEDTLS_ECP_DP_SECP192K1, /*!< 192-bits "Koblitz" curve */
76 MBEDTLS_ECP_DP_SECP224K1, /*!< 224-bits "Koblitz" curve */
77 MBEDTLS_ECP_DP_SECP256K1, /*!< 256-bits "Koblitz" curve */
78} mbedtls_ecp_group_id;
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +020079
80/**
Manuel Pégourié-Gonnard66153662013-12-03 14:12:26 +010081 * Number of supported curves (plus one for NONE).
82 *
83 * (Montgomery curves excluded for now.)
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +020084 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085#define MBEDTLS_ECP_DP_MAX 12
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +020086
87/**
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +020088 * Curve information for use by other modules
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020089 */
90typedef struct
91{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 mbedtls_ecp_group_id grp_id; /*!< Internal identifier */
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +020093 uint16_t tls_id; /*!< TLS NamedCurve identifier */
94 uint16_t bit_size; /*!< Curve size in bits */
95 const char *name; /*!< Human-friendly name */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096} mbedtls_ecp_curve_info;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020097
98/**
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010099 * \brief ECP point structure (jacobian coordinates)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100100 *
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100101 * \note All functions expect and return points satisfying
102 * the following condition: Z == 0 or Z == 1. (Other
103 * values of Z are used by internal functions only.)
104 * The point is zero, or "at infinity", if Z == 0.
105 * Otherwise, X and Y are its standard (affine) coordinates.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100106 */
107typedef struct
108{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 mbedtls_mpi X; /*!< the point's X coordinate */
110 mbedtls_mpi Y; /*!< the point's Y coordinate */
111 mbedtls_mpi Z; /*!< the point's Z coordinate */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100112}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113mbedtls_ecp_point;
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100114
115/**
116 * \brief ECP group structure
117 *
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100118 * We consider two types of curves equations:
119 * 1. Short Weierstrass y^2 = x^3 + A x + B mod P (SEC1 + RFC 4492)
Manuel Pégourié-Gonnard07894332015-06-23 00:18:41 +0200120 * 2. Montgomery, y^2 = x^3 + A x^2 + x mod P (Curve25519 + draft)
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100121 * In both cases, a generator G for a prime-order subgroup is fixed. In the
122 * short weierstrass, this subgroup is actually the whole curve, and its
123 * cardinal is denoted by N.
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100124 *
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200125 * In the case of Short Weierstrass curves, our code requires that N is an odd
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 * prime. (Use odd in mbedtls_ecp_mul() and prime in mbedtls_ecdsa_sign() for blinding.)
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200127 *
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100128 * In the case of Montgomery curves, we don't store A but (A + 2) / 4 which is
Paul Bakker75342a62014-04-08 17:35:40 +0200129 * the quantity actually used in the formulas. Also, nbits is not the size of N
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100130 * but the required size for private keys.
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100131 *
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200132 * If modp is NULL, reduction modulo P is done using a generic algorithm.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 * Otherwise, it must point to a function that takes an mbedtls_mpi in the range
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200134 * 0..2^(2*pbits)-1 and transforms it in-place in an integer of little more
135 * than pbits, so that the integer may be efficiently brought in the 0..P-1
136 * range by a few additions or substractions. It must return 0 on success and
137 * non-zero on failure.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100138 */
139typedef struct
140{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 mbedtls_ecp_group_id id; /*!< internal group identifier */
142 mbedtls_mpi P; /*!< prime modulus of the base field */
143 mbedtls_mpi A; /*!< 1. A in the equation, or 2. (A + 2) / 4 */
144 mbedtls_mpi B; /*!< 1. B in the equation, or 2. unused */
145 mbedtls_ecp_point G; /*!< generator of the (sub)group used */
146 mbedtls_mpi N; /*!< 1. the order of G, or 2. unused */
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +0200147 size_t pbits; /*!< number of bits in P */
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100148 size_t nbits; /*!< number of bits in 1. P, or 2. private keys */
Manuel Pégourié-Gonnard1f82b042013-12-06 12:51:50 +0100149 unsigned int h; /*!< internal: 1 if the constants are static */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 int (*modp)(mbedtls_mpi *); /*!< function for fast reduction mod P */
151 int (*t_pre)(mbedtls_ecp_point *, void *); /*!< unused */
152 int (*t_post)(mbedtls_ecp_point *, void *); /*!< unused */
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100153 void *t_data; /*!< unused */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 mbedtls_ecp_point *T; /*!< pre-computed points for ecp_mul_comb() */
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +0200155 size_t T_size; /*!< number for pre-computed points */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100156}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157mbedtls_ecp_group;
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100158
159/**
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200160 * \brief ECP key pair structure
161 *
162 * A generic key pair that could be used for ECDSA, fixed ECDH, etc.
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200163 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 * \note Members purposefully in the same order as struc mbedtls_ecdsa_context.
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200165 */
166typedef struct
167{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 mbedtls_ecp_group grp; /*!< Elliptic curve and base point */
169 mbedtls_mpi d; /*!< our secret value */
170 mbedtls_ecp_point Q; /*!< our public value */
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200171}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172mbedtls_ecp_keypair;
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200173
Paul Bakker088c5c52014-04-25 11:11:10 +0200174/**
175 * \name SECTION: Module settings
176 *
177 * The configuration options you can set for this module are in this section.
178 * Either change them in config.h or define them on the compiler command line.
179 * \{
180 */
181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182#if !defined(MBEDTLS_ECP_MAX_BITS)
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200183/**
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200184 * Maximum size of the groups (that is, of N and P)
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100185 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186#define MBEDTLS_ECP_MAX_BITS 521 /**< Maximum bit size of groups */
Paul Bakkere1b665e2013-12-11 16:02:58 +0100187#endif
188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189#define MBEDTLS_ECP_MAX_BYTES ( ( MBEDTLS_ECP_MAX_BITS + 7 ) / 8 )
190#define MBEDTLS_ECP_MAX_PT_LEN ( 2 * MBEDTLS_ECP_MAX_BYTES + 1 )
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192#if !defined(MBEDTLS_ECP_WINDOW_SIZE)
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100193/*
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +0100194 * Maximum "window" size used for point multiplication.
195 * Default: 6.
196 * Minimum value: 2. Maximum value: 7.
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100197 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 * Result is an array of at most ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) )
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +0100199 * points used for point multiplication. This value is directly tied to EC
200 * peak memory usage, so decreasing it by one should roughly cut memory usage
201 * by two (if large curves are in use).
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100202 *
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +0100203 * Reduction in size may reduce speed, but larger curves are impacted first.
204 * Sample performances (in ECDHE handshakes/s, with FIXED_POINT_OPTIM = 1):
205 * w-size: 6 5 4 3 2
206 * 521 145 141 135 120 97
207 * 384 214 209 198 177 146
208 * 256 320 320 303 262 226
209 * 224 475 475 453 398 342
210 * 192 640 640 633 587 476
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100211 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212#define MBEDTLS_ECP_WINDOW_SIZE 6 /**< Maximum window size used */
213#endif /* MBEDTLS_ECP_WINDOW_SIZE */
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +0100214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215#if !defined(MBEDTLS_ECP_FIXED_POINT_OPTIM)
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +0100216/*
217 * Trade memory for speed on fixed-point multiplication.
218 *
219 * This speeds up repeated multiplication of the generator (that is, the
220 * multiplication in ECDSA signatures, and half of the multiplications in
221 * ECDSA verification and ECDHE) by a factor roughly 3 to 4.
222 *
223 * The cost is increasing EC peak memory usage by a factor roughly 2.
224 *
225 * Change this value to 0 to reduce peak memory usage.
226 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227#define MBEDTLS_ECP_FIXED_POINT_OPTIM 1 /**< Enable fixed-point speed-up */
228#endif /* MBEDTLS_ECP_FIXED_POINT_OPTIM */
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100229
Paul Bakker088c5c52014-04-25 11:11:10 +0200230/* \} name SECTION: Module settings */
231
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100232/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100233 * Point formats, from RFC 4492's enum ECPointFormat
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100234 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235#define MBEDTLS_ECP_PF_UNCOMPRESSED 0 /**< Uncompressed point format */
236#define MBEDTLS_ECP_PF_COMPRESSED 1 /**< Compressed point format */
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100237
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100238/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100239 * Some other constants from RFC 4492
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100240 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241#define MBEDTLS_ECP_TLS_NAMED_CURVE 3 /**< ECCurveType's named_curve */
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100242
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100243#if defined(MBEDTLS_ECP_EARLY_RETURN)
244/**
245 * \brief Set the maximum number of basic operations done in a row.
246 *
247 * If more operations are needed to complete a computation,
248 * MBEDTLS_ERR_ECP_IN_PROGRESS will be returned by the
249 * function performing the computation. That function will
250 * then need to be called again with the same arguments until
251 * it returns 0 or an other error code.
252 *
253 * \param max_ops Maximum number of basic operations done in a row.
254 * Default: 0 (unlimited).
255 * Lower (non-zero) values mean ECC functions will block for
256 * a lesser maximum amount of time.
257 *
258 * \note A "basic operation" is roughly multiplication in GF(p),
259 * or whatever takes a roughly equivalent amount of time.
260 * As an indication, a scalar multiplication on P-256 is
261 * of the order of 3600 "basic operations" with default
262 * settings.
263 *
264 * \warning Values lower than 120 are currently not well-supported, in
265 * that sometimes functions will have to block for longer.
266 */
267void mbedtls_ecp_set_max_ops( unsigned max_ops );
268#endif /* MBEDTLS_ECP_EARLY_RETURN */
269
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100270/**
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100271 * \brief Get the list of supported curves in order of preferrence
272 * (full information)
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200273 *
274 * \return A statically allocated array, the last entry is 0.
275 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list( void );
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200277
278/**
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100279 * \brief Get the list of supported curves in order of preferrence
280 * (grp_id only)
281 *
282 * \return A statically allocated array,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 * terminated with MBEDTLS_ECP_DP_NONE.
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100284 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list( void );
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100286
287/**
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200288 * \brief Get curve information from an internal group identifier
289 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 * \param grp_id A MBEDTLS_ECP_DP_XXX value
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200291 *
292 * \return The associated curve information or NULL
293 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294const 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 +0200295
296/**
297 * \brief Get curve information from a TLS NamedCurve value
298 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 * \param tls_id A MBEDTLS_ECP_DP_XXX value
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200300 *
301 * \return The associated curve information or NULL
302 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id( uint16_t tls_id );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200304
305/**
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100306 * \brief Get curve information from a human-readable name
307 *
308 * \param name The name
309 *
310 * \return The associated curve information or NULL
311 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name( const char *name );
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100313
314/**
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100315 * \brief Initialize a point (as zero)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100316 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317void mbedtls_ecp_point_init( mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100318
319/**
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100320 * \brief Initialize a group (to something meaningless)
321 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322void mbedtls_ecp_group_init( mbedtls_ecp_group *grp );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100323
324/**
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200325 * \brief Initialize a key pair (as an invalid one)
326 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327void mbedtls_ecp_keypair_init( mbedtls_ecp_keypair *key );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200328
329/**
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100330 * \brief Free the components of a point
331 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332void mbedtls_ecp_point_free( mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100333
334/**
335 * \brief Free the components of an ECP group
336 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337void mbedtls_ecp_group_free( mbedtls_ecp_group *grp );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100338
339/**
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200340 * \brief Free the components of a key pair
341 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair *key );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200343
344/**
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100345 * \brief Copy the contents of point Q into P
346 *
347 * \param P Destination point
348 * \param Q Source point
349 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100350 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200351 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100352 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353int mbedtls_ecp_copy( mbedtls_ecp_point *P, const mbedtls_ecp_point *Q );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100354
355/**
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200356 * \brief Copy the contents of a group object
357 *
358 * \param dst Destination group
359 * \param src Source group
360 *
361 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200362 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200363 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst, const mbedtls_ecp_group *src );
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200365
366/**
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200367 * \brief Set a point to zero
368 *
369 * \param pt Destination point
370 *
371 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200372 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200373 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374int mbedtls_ecp_set_zero( mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200375
376/**
377 * \brief Tell if a point is zero
378 *
379 * \param pt Point to test
380 *
381 * \return 1 if point is zero, 0 otherwise
382 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200384
385/**
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200386 * \brief Compare two points
387 *
388 * \note This assumes the points are normalized. Otherwise,
389 * they may compare as "not equal" even if they are.
390 *
391 * \param P First point to compare
392 * \param Q Second point to compare
393 *
394 * \return 0 if the points are equal,
395 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA otherwise
396 */
397int mbedtls_ecp_point_cmp( const mbedtls_ecp_point *P,
398 const mbedtls_ecp_point *Q );
399
400/**
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100401 * \brief Import a non-zero point from two ASCII strings
402 *
403 * \param P Destination point
404 * \param radix Input numeric base
405 * \param x First affine coordinate as a null-terminated string
406 * \param y Second affine coordinate as a null-terminated string
407 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100409 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410int mbedtls_ecp_point_read_string( mbedtls_ecp_point *P, int radix,
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100411 const char *x, const char *y );
412
413/**
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100414 * \brief Export a point into unsigned binary data
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100415 *
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100416 * \param grp Group to which the point should belong
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100417 * \param P Point to export
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418 * \param format Point format, should be a MBEDTLS_ECP_PF_XXX macro
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100419 * \param olen Length of the actual output
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100420 * \param buf Output buffer
421 * \param buflen Length of the output buffer
422 *
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100423 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 * or MBEDTLS_ERR_ECP_BAD_INPUT_DATA
425 * or MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100426 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100428 int format, size_t *olen,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100429 unsigned char *buf, size_t buflen );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100430
431/**
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100432 * \brief Import a point from unsigned binary data
433 *
434 * \param grp Group to which the point should belong
435 * \param P Point to import
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100436 * \param buf Input buffer
437 * \param ilen Actual length of input
438 *
439 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200441 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 * MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the point format
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100443 * is not implemented.
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100444 *
445 * \note This function does NOT check that the point actually
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 * belongs to the given group, see mbedtls_ecp_check_pubkey() for
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100447 * that.
448 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100450 const unsigned char *buf, size_t ilen );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100451
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100452/**
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200453 * \brief Import a point from a TLS ECPoint record
454 *
455 * \param grp ECP group used
456 * \param pt Destination point
457 * \param buf $(Start of input buffer)
458 * \param len Buffer length
459 *
Manuel Pégourié-Gonnard150c4f62014-11-21 09:14:52 +0100460 * \note buf is updated to point right after the ECPoint on exit
461 *
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +0200462 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 * MBEDTLS_ERR_MPI_XXX if initialization failed
464 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200465 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200467 const unsigned char **buf, size_t len );
468
469/**
470 * \brief Export a point as a TLS ECPoint record
471 *
472 * \param grp ECP group used
473 * \param pt Point to export
474 * \param format Export format
475 * \param olen length of data written
476 * \param buf Buffer to write to
477 * \param blen Buffer length
478 *
479 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480 * or MBEDTLS_ERR_ECP_BAD_INPUT_DATA
481 * or MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200482 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483int mbedtls_ecp_tls_write_point( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt,
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200484 int format, size_t *olen,
485 unsigned char *buf, size_t blen );
486
487/**
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100488 * \brief Set a group using well-known domain parameters
489 *
490 * \param grp Destination group
491 * \param index Index in the list of well-known domain parameters
492 *
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +0200493 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 * MBEDTLS_ERR_MPI_XXX if initialization failed
495 * MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE for unkownn groups
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100496 *
Manuel Pégourié-Gonnardaff37e52015-05-11 18:11:57 +0200497 * \note Index should be a value of RFC 4492's enum NamedCurve,
498 * usually in the form of a MBEDTLS_ECP_DP_XXX macro.
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100499 */
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200500int mbedtls_ecp_group_load( mbedtls_ecp_group *grp, mbedtls_ecp_group_id index );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100501
502/**
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100503 * \brief Set a group from a TLS ECParameters record
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100504 *
505 * \param grp Destination group
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100506 * \param buf &(Start of input buffer)
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100507 * \param len Buffer length
508 *
Manuel Pégourié-Gonnard150c4f62014-11-21 09:14:52 +0100509 * \note buf is updated to point right after ECParameters on exit
510 *
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +0200511 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 * MBEDTLS_ERR_MPI_XXX if initialization failed
513 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100514 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515int 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 +0100516
517/**
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100518 * \brief Write the TLS ECParameters record for a group
519 *
520 * \param grp ECP group used
521 * \param olen Number of bytes actually written
522 * \param buf Buffer to write to
523 * \param blen Buffer length
524 *
525 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 * or MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100527 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, size_t *olen,
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100529 unsigned char *buf, size_t blen );
530
531/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100532 * \brief Multiplication by an integer: R = m * P
Paul Bakker6838bd12013-09-30 13:56:38 +0200533 * (Not thread-safe to use same group in multiple threads)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100534 *
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200535 * \note In order to prevent timing attacks, this function
536 * executes the exact same sequence of (base field)
537 * operations for any valid m. It avoids any if-branch or
538 * array index depending on the value of m.
539 *
540 * \note If f_rng is not NULL, it is used to randomize intermediate
541 * results in order to prevent potential timing attacks
542 * targeting these results. It is recommended to always
543 * provide a non-NULL f_rng (the overhead is negligible).
544 *
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100545 * \param grp ECP group
546 * \param R Destination point
547 * \param m Integer by which to multiply
548 * \param P Point to multiply
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200549 * \param f_rng RNG function (see notes)
550 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100551 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100552 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 * MBEDTLS_ERR_ECP_INVALID_KEY if m is not a valid privkey
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +0100554 * or P is not a valid pubkey,
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100555 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
556 * MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
557 * operations was reached (see \c mbedtls_ecp_set_max_ops()),
558 * indicating the function should be called again with the
559 * exact same arguments.
560 *
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100561 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
563 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard09ceaf42013-11-20 23:06:14 +0100564 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100565
566/**
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200567 * \brief Multiplication and addition of two points by integers:
568 * R = m * P + n * Q
569 * (Not thread-safe to use same group in multiple threads)
570 *
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +0200571 * \note In contrast to mbedtls_ecp_mul(), this function does not guarantee
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200572 * a constant execution flow and timing.
573 *
574 * \param grp ECP group
575 * \param R Destination point
576 * \param m Integer by which to multiply P
577 * \param P Point to multiply by m
578 * \param n Integer by which to multiply Q
579 * \param Q Point to be multiplied by n
580 *
581 * \return 0 if successful,
582 * MBEDTLS_ERR_ECP_INVALID_KEY if m or n is not a valid privkey
583 * or P or Q is not a valid pubkey,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200584 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200585 */
586int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
587 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
588 const mbedtls_mpi *n, const mbedtls_ecp_point *Q );
589
590/**
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200591 * \brief Check that a point is a valid public key on this curve
592 *
593 * \param grp Curve/group the point should belong to
594 * \param pt Point to check
595 *
596 * \return 0 if point is a valid public key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 * MBEDTLS_ERR_ECP_INVALID_KEY otherwise.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200598 *
599 * \note This function only checks the point is non-zero, has valid
600 * coordinates and lies on the curve, but not that it is
601 * indeed a multiple of G. This is additional check is more
602 * expensive, isn't required by standards, and shouldn't be
603 * necessary if the group used has a small cofactor. In
604 * particular, it is useless for the NIST groups which all
605 * have a cofactor of 1.
606 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 * \note Uses bare components rather than an mbedtls_ecp_keypair structure
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200608 * in order to ease use with other structures such as
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 * mbedtls_ecdh_context of mbedtls_ecdsa_context.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200610 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200612
613/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 * \brief Check that an mbedtls_mpi is a valid private key for this curve
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200615 *
616 * \param grp Group used
617 * \param d Integer to check
618 *
619 * \return 0 if point is a valid private key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 * MBEDTLS_ERR_ECP_INVALID_KEY otherwise.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200621 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 * \note Uses bare components rather than an mbedtls_ecp_keypair structure
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200623 * in order to ease use with other structures such as
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 * mbedtls_ecdh_context of mbedtls_ecdsa_context.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200625 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp, const mbedtls_mpi *d );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200627
628/**
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +0200629 * \brief Generate a keypair with configurable base point
630 *
631 * \param grp ECP group
632 * \param G Chosen base point
633 * \param d Destination MPI (secret part)
634 * \param Q Destination point (public part)
635 * \param f_rng RNG function
636 * \param p_rng RNG parameter
637 *
638 * \return 0 if successful,
639 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
640 *
641 * \note Uses bare components rather than an mbedtls_ecp_keypair structure
642 * in order to ease use with other structures such as
643 * mbedtls_ecdh_context of mbedtls_ecdsa_context.
644 */
645int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp,
646 const mbedtls_ecp_point *G,
647 mbedtls_mpi *d, mbedtls_ecp_point *Q,
648 int (*f_rng)(void *, unsigned char *, size_t),
649 void *p_rng );
650
651/**
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100652 * \brief Generate a keypair
653 *
654 * \param grp ECP group
655 * \param d Destination MPI (secret part)
656 * \param Q Destination point (public part)
657 * \param f_rng RNG function
658 * \param p_rng RNG parameter
659 *
660 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200662 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 * \note Uses bare components rather than an mbedtls_ecp_keypair structure
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200664 * in order to ease use with other structures such as
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 * mbedtls_ecdh_context of mbedtls_ecdsa_context.
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100666 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100668 int (*f_rng)(void *, unsigned char *, size_t),
669 void *p_rng );
670
671/**
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +0100672 * \brief Generate a keypair
673 *
674 * \param grp_id ECP group identifier
675 * \param key Destination keypair
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é-Gonnard104ee1d2013-11-30 14:13:16 +0100681 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +0100683 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
684
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +0100685/**
686 * \brief Check a public-private key pair
687 *
688 * \param pub Keypair structure holding a public key
689 * \param prv Keypair structure holding a private (plus public) key
690 *
Manuel Pégourié-Gonnard862d5032015-04-15 11:30:46 +0200691 * \return 0 if successful (keys are valid and match), or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA, or
693 * a MBEDTLS_ERR_ECP_XXX or MBEDTLS_ERR_MPI_XXX code.
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +0100694 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +0100696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697#if defined(MBEDTLS_SELF_TEST)
Janos Follathb0697532016-08-18 12:38:46 +0100698
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +0100699/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100700 * \brief Checkup routine
701 *
Manuel Pégourié-Gonnard13a1ef82014-03-27 20:12:44 +0100702 * \return 0 if successful, or 1 if a test failed
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100703 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704int mbedtls_ecp_self_test( int verbose );
Janos Follathb0697532016-08-18 12:38:46 +0100705
Janos Follath372697b2016-10-28 16:53:11 +0100706#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100707
708#ifdef __cplusplus
709}
710#endif
711
Janos Follathb0697532016-08-18 12:38:46 +0100712#else /* MBEDTLS_ECP_ALT */
713#include "ecp_alt.h"
714#endif /* MBEDTLS_ECP_ALT */
715
Paul Bakker9af723c2014-05-01 13:03:14 +0200716#endif /* ecp.h */