blob: d75f038d46a93d0ff3e49c390fcba05a6078df49 [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é-Gonnard883f3132012-11-02 09:40:25 +010039
Janos Follathb0697532016-08-18 12:38:46 +010040#if !defined(MBEDTLS_ECP_ALT)
41// Regular implementation
42//
43
Paul Bakker407a0da2013-06-27 14:29:21 +020044#ifdef __cplusplus
45extern "C" {
46#endif
47
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010048/**
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +020049 * Domain parameters (curve, subgroup and generator) identifiers.
50 *
51 * Only curves over prime fields are supported.
52 *
53 * \warning This library does not support validation of arbitrary domain
54 * parameters. Therefore, only well-known domain parameters from trusted
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +020055 * sources should be used. See mbedtls_ecp_group_load().
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +020056 */
57typedef enum
58{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059 MBEDTLS_ECP_DP_NONE = 0,
60 MBEDTLS_ECP_DP_SECP192R1, /*!< 192-bits NIST curve */
61 MBEDTLS_ECP_DP_SECP224R1, /*!< 224-bits NIST curve */
62 MBEDTLS_ECP_DP_SECP256R1, /*!< 256-bits NIST curve */
63 MBEDTLS_ECP_DP_SECP384R1, /*!< 384-bits NIST curve */
64 MBEDTLS_ECP_DP_SECP521R1, /*!< 521-bits NIST curve */
65 MBEDTLS_ECP_DP_BP256R1, /*!< 256-bits Brainpool curve */
66 MBEDTLS_ECP_DP_BP384R1, /*!< 384-bits Brainpool curve */
67 MBEDTLS_ECP_DP_BP512R1, /*!< 512-bits Brainpool curve */
Manuel Pégourié-Gonnard07894332015-06-23 00:18:41 +020068 MBEDTLS_ECP_DP_CURVE25519, /*!< Curve25519 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069 MBEDTLS_ECP_DP_SECP192K1, /*!< 192-bits "Koblitz" curve */
70 MBEDTLS_ECP_DP_SECP224K1, /*!< 224-bits "Koblitz" curve */
71 MBEDTLS_ECP_DP_SECP256K1, /*!< 256-bits "Koblitz" curve */
72} mbedtls_ecp_group_id;
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +020073
74/**
Manuel Pégourié-Gonnard66153662013-12-03 14:12:26 +010075 * Number of supported curves (plus one for NONE).
76 *
77 * (Montgomery curves excluded for now.)
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +020078 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079#define MBEDTLS_ECP_DP_MAX 12
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +020080
81/**
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +020082 * Curve information for use by other modules
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020083 */
84typedef struct
85{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 mbedtls_ecp_group_id grp_id; /*!< Internal identifier */
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +020087 uint16_t tls_id; /*!< TLS NamedCurve identifier */
88 uint16_t bit_size; /*!< Curve size in bits */
89 const char *name; /*!< Human-friendly name */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090} mbedtls_ecp_curve_info;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020091
92/**
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010093 * \brief ECP point structure (jacobian coordinates)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +010094 *
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010095 * \note All functions expect and return points satisfying
96 * the following condition: Z == 0 or Z == 1. (Other
97 * values of Z are used by internal functions only.)
98 * The point is zero, or "at infinity", if Z == 0.
99 * Otherwise, X and Y are its standard (affine) coordinates.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100100 */
101typedef struct
102{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 mbedtls_mpi X; /*!< the point's X coordinate */
104 mbedtls_mpi Y; /*!< the point's Y coordinate */
105 mbedtls_mpi Z; /*!< the point's Z coordinate */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100106}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107mbedtls_ecp_point;
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100108
109/**
110 * \brief ECP group structure
111 *
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100112 * We consider two types of curves equations:
113 * 1. Short Weierstrass y^2 = x^3 + A x + B mod P (SEC1 + RFC 4492)
Manuel Pégourié-Gonnard07894332015-06-23 00:18:41 +0200114 * 2. Montgomery, y^2 = x^3 + A x^2 + x mod P (Curve25519 + draft)
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100115 * In both cases, a generator G for a prime-order subgroup is fixed. In the
116 * short weierstrass, this subgroup is actually the whole curve, and its
117 * cardinal is denoted by N.
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100118 *
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200119 * In the case of Short Weierstrass curves, our code requires that N is an odd
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 * prime. (Use odd in mbedtls_ecp_mul() and prime in mbedtls_ecdsa_sign() for blinding.)
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200121 *
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100122 * In the case of Montgomery curves, we don't store A but (A + 2) / 4 which is
Paul Bakker75342a62014-04-08 17:35:40 +0200123 * the quantity actually used in the formulas. Also, nbits is not the size of N
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100124 * but the required size for private keys.
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100125 *
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200126 * If modp is NULL, reduction modulo P is done using a generic algorithm.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 * Otherwise, it must point to a function that takes an mbedtls_mpi in the range
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200128 * 0..2^(2*pbits)-1 and transforms it in-place in an integer of little more
129 * than pbits, so that the integer may be efficiently brought in the 0..P-1
130 * range by a few additions or substractions. It must return 0 on success and
131 * non-zero on failure.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100132 */
133typedef struct
134{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 mbedtls_ecp_group_id id; /*!< internal group identifier */
136 mbedtls_mpi P; /*!< prime modulus of the base field */
137 mbedtls_mpi A; /*!< 1. A in the equation, or 2. (A + 2) / 4 */
138 mbedtls_mpi B; /*!< 1. B in the equation, or 2. unused */
139 mbedtls_ecp_point G; /*!< generator of the (sub)group used */
140 mbedtls_mpi N; /*!< 1. the order of G, or 2. unused */
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +0200141 size_t pbits; /*!< number of bits in P */
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100142 size_t nbits; /*!< number of bits in 1. P, or 2. private keys */
Manuel Pégourié-Gonnard1f82b042013-12-06 12:51:50 +0100143 unsigned int h; /*!< internal: 1 if the constants are static */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 int (*modp)(mbedtls_mpi *); /*!< function for fast reduction mod P */
145 int (*t_pre)(mbedtls_ecp_point *, void *); /*!< unused */
146 int (*t_post)(mbedtls_ecp_point *, void *); /*!< unused */
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100147 void *t_data; /*!< unused */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_ecp_point *T; /*!< pre-computed points for ecp_mul_comb() */
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +0200149 size_t T_size; /*!< number for pre-computed points */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100150}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151mbedtls_ecp_group;
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100152
153/**
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200154 * \brief ECP key pair structure
155 *
156 * A generic key pair that could be used for ECDSA, fixed ECDH, etc.
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200157 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 * \note Members purposefully in the same order as struc mbedtls_ecdsa_context.
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200159 */
160typedef struct
161{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 mbedtls_ecp_group grp; /*!< Elliptic curve and base point */
163 mbedtls_mpi d; /*!< our secret value */
164 mbedtls_ecp_point Q; /*!< our public value */
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200165}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166mbedtls_ecp_keypair;
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200167
Paul Bakker088c5c52014-04-25 11:11:10 +0200168/**
169 * \name SECTION: Module settings
170 *
171 * The configuration options you can set for this module are in this section.
172 * Either change them in config.h or define them on the compiler command line.
173 * \{
174 */
175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176#if !defined(MBEDTLS_ECP_MAX_BITS)
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200177/**
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200178 * Maximum size of the groups (that is, of N and P)
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100179 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180#define MBEDTLS_ECP_MAX_BITS 521 /**< Maximum bit size of groups */
Paul Bakkere1b665e2013-12-11 16:02:58 +0100181#endif
182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183#define MBEDTLS_ECP_MAX_BYTES ( ( MBEDTLS_ECP_MAX_BITS + 7 ) / 8 )
184#define MBEDTLS_ECP_MAX_PT_LEN ( 2 * MBEDTLS_ECP_MAX_BYTES + 1 )
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186#if !defined(MBEDTLS_ECP_WINDOW_SIZE)
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100187/*
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +0100188 * Maximum "window" size used for point multiplication.
189 * Default: 6.
190 * Minimum value: 2. Maximum value: 7.
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100191 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 * Result is an array of at most ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) )
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +0100193 * points used for point multiplication. This value is directly tied to EC
194 * peak memory usage, so decreasing it by one should roughly cut memory usage
195 * by two (if large curves are in use).
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100196 *
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +0100197 * Reduction in size may reduce speed, but larger curves are impacted first.
198 * Sample performances (in ECDHE handshakes/s, with FIXED_POINT_OPTIM = 1):
199 * w-size: 6 5 4 3 2
200 * 521 145 141 135 120 97
201 * 384 214 209 198 177 146
202 * 256 320 320 303 262 226
Paul Bakker088c5c52014-04-25 11:11:10 +0200203
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +0100204 * 224 475 475 453 398 342
205 * 192 640 640 633 587 476
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100206 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207#define MBEDTLS_ECP_WINDOW_SIZE 6 /**< Maximum window size used */
208#endif /* MBEDTLS_ECP_WINDOW_SIZE */
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +0100209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210#if !defined(MBEDTLS_ECP_FIXED_POINT_OPTIM)
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +0100211/*
212 * Trade memory for speed on fixed-point multiplication.
213 *
214 * This speeds up repeated multiplication of the generator (that is, the
215 * multiplication in ECDSA signatures, and half of the multiplications in
216 * ECDSA verification and ECDHE) by a factor roughly 3 to 4.
217 *
218 * The cost is increasing EC peak memory usage by a factor roughly 2.
219 *
220 * Change this value to 0 to reduce peak memory usage.
221 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222#define MBEDTLS_ECP_FIXED_POINT_OPTIM 1 /**< Enable fixed-point speed-up */
223#endif /* MBEDTLS_ECP_FIXED_POINT_OPTIM */
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100224
Paul Bakker088c5c52014-04-25 11:11:10 +0200225/* \} name SECTION: Module settings */
226
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100227/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100228 * Point formats, from RFC 4492's enum ECPointFormat
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100229 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230#define MBEDTLS_ECP_PF_UNCOMPRESSED 0 /**< Uncompressed point format */
231#define MBEDTLS_ECP_PF_COMPRESSED 1 /**< Compressed point format */
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100232
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100233/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100234 * Some other constants from RFC 4492
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100235 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236#define MBEDTLS_ECP_TLS_NAMED_CURVE 3 /**< ECCurveType's named_curve */
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100237
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100238/**
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100239 * \brief Get the list of supported curves in order of preferrence
240 * (full information)
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200241 *
242 * \return A statically allocated array, the last entry is 0.
243 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list( void );
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200245
246/**
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100247 * \brief Get the list of supported curves in order of preferrence
248 * (grp_id only)
249 *
250 * \return A statically allocated array,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 * terminated with MBEDTLS_ECP_DP_NONE.
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100252 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list( void );
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100254
255/**
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200256 * \brief Get curve information from an internal group identifier
257 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 * \param grp_id A MBEDTLS_ECP_DP_XXX value
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200259 *
260 * \return The associated curve information or NULL
261 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262const 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 +0200263
264/**
265 * \brief Get curve information from a TLS NamedCurve value
266 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 * \param tls_id A MBEDTLS_ECP_DP_XXX value
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200268 *
269 * \return The associated curve information or NULL
270 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id( uint16_t tls_id );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200272
273/**
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100274 * \brief Get curve information from a human-readable name
275 *
276 * \param name The name
277 *
278 * \return The associated curve information or NULL
279 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name( const char *name );
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100281
282/**
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100283 * \brief Initialize a point (as zero)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100284 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285void mbedtls_ecp_point_init( mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100286
287/**
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100288 * \brief Initialize a group (to something meaningless)
289 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290void mbedtls_ecp_group_init( mbedtls_ecp_group *grp );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100291
292/**
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200293 * \brief Initialize a key pair (as an invalid one)
294 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295void mbedtls_ecp_keypair_init( mbedtls_ecp_keypair *key );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200296
297/**
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100298 * \brief Free the components of a point
299 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300void mbedtls_ecp_point_free( mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100301
302/**
303 * \brief Free the components of an ECP group
304 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305void mbedtls_ecp_group_free( mbedtls_ecp_group *grp );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100306
307/**
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200308 * \brief Free the components of a key pair
309 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair *key );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200311
312/**
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100313 * \brief Copy the contents of point Q into P
314 *
315 * \param P Destination point
316 * \param Q Source point
317 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100318 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200319 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100320 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321int mbedtls_ecp_copy( mbedtls_ecp_point *P, const mbedtls_ecp_point *Q );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100322
323/**
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200324 * \brief Copy the contents of a group object
325 *
326 * \param dst Destination group
327 * \param src Source group
328 *
329 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200330 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200331 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst, const mbedtls_ecp_group *src );
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200333
334/**
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200335 * \brief Set a point to zero
336 *
337 * \param pt Destination point
338 *
339 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200340 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200341 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342int mbedtls_ecp_set_zero( mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200343
344/**
345 * \brief Tell if a point is zero
346 *
347 * \param pt Point to test
348 *
349 * \return 1 if point is zero, 0 otherwise
350 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200352
353/**
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200354 * \brief Compare two points
355 *
356 * \note This assumes the points are normalized. Otherwise,
357 * they may compare as "not equal" even if they are.
358 *
359 * \param P First point to compare
360 * \param Q Second point to compare
361 *
362 * \return 0 if the points are equal,
363 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA otherwise
364 */
365int mbedtls_ecp_point_cmp( const mbedtls_ecp_point *P,
366 const mbedtls_ecp_point *Q );
367
368/**
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100369 * \brief Import a non-zero point from two ASCII strings
370 *
371 * \param P Destination point
372 * \param radix Input numeric base
373 * \param x First affine coordinate as a null-terminated string
374 * \param y Second affine coordinate as a null-terminated string
375 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100377 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378int mbedtls_ecp_point_read_string( mbedtls_ecp_point *P, int radix,
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100379 const char *x, const char *y );
380
381/**
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100382 * \brief Export a point into unsigned binary data
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100383 *
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100384 * \param grp Group to which the point should belong
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100385 * \param P Point to export
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 * \param format Point format, should be a MBEDTLS_ECP_PF_XXX macro
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100387 * \param olen Length of the actual output
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100388 * \param buf Output buffer
389 * \param buflen Length of the output buffer
390 *
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100391 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392 * or MBEDTLS_ERR_ECP_BAD_INPUT_DATA
393 * or MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100394 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100396 int format, size_t *olen,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100397 unsigned char *buf, size_t buflen );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100398
399/**
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100400 * \brief Import a point from unsigned binary data
401 *
402 * \param grp Group to which the point should belong
403 * \param P Point to import
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100404 * \param buf Input buffer
405 * \param ilen Actual length of input
406 *
407 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200409 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 * MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the point format
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100411 * is not implemented.
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100412 *
413 * \note This function does NOT check that the point actually
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 * belongs to the given group, see mbedtls_ecp_check_pubkey() for
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100415 * that.
416 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100418 const unsigned char *buf, size_t ilen );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100419
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100420/**
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200421 * \brief Import a point from a TLS ECPoint record
422 *
423 * \param grp ECP group used
424 * \param pt Destination point
425 * \param buf $(Start of input buffer)
426 * \param len Buffer length
427 *
Manuel Pégourié-Gonnard150c4f62014-11-21 09:14:52 +0100428 * \note buf is updated to point right after the ECPoint on exit
429 *
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +0200430 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 * MBEDTLS_ERR_MPI_XXX if initialization failed
432 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200433 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200435 const unsigned char **buf, size_t len );
436
437/**
438 * \brief Export a point as a TLS ECPoint record
439 *
440 * \param grp ECP group used
441 * \param pt Point to export
442 * \param format Export format
443 * \param olen length of data written
444 * \param buf Buffer to write to
445 * \param blen Buffer length
446 *
447 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 * or MBEDTLS_ERR_ECP_BAD_INPUT_DATA
449 * or MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200450 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451int mbedtls_ecp_tls_write_point( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt,
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200452 int format, size_t *olen,
453 unsigned char *buf, size_t blen );
454
455/**
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100456 * \brief Set a group using well-known domain parameters
457 *
458 * \param grp Destination group
459 * \param index Index in the list of well-known domain parameters
460 *
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +0200461 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 * MBEDTLS_ERR_MPI_XXX if initialization failed
463 * MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE for unkownn groups
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100464 *
Manuel Pégourié-Gonnardaff37e52015-05-11 18:11:57 +0200465 * \note Index should be a value of RFC 4492's enum NamedCurve,
466 * usually in the form of a MBEDTLS_ECP_DP_XXX macro.
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100467 */
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200468int mbedtls_ecp_group_load( mbedtls_ecp_group *grp, mbedtls_ecp_group_id index );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100469
470/**
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100471 * \brief Set a group from a TLS ECParameters record
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100472 *
473 * \param grp Destination group
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100474 * \param buf &(Start of input buffer)
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100475 * \param len Buffer length
476 *
Manuel Pégourié-Gonnard150c4f62014-11-21 09:14:52 +0100477 * \note buf is updated to point right after ECParameters on exit
478 *
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +0200479 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480 * MBEDTLS_ERR_MPI_XXX if initialization failed
481 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100482 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483int 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 +0100484
485/**
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100486 * \brief Write the TLS ECParameters record for a group
487 *
488 * \param grp ECP group used
489 * \param olen Number of bytes actually written
490 * \param buf Buffer to write to
491 * \param blen Buffer length
492 *
493 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 * or MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100495 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, size_t *olen,
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100497 unsigned char *buf, size_t blen );
498
499/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100500 * \brief Multiplication by an integer: R = m * P
Paul Bakker6838bd12013-09-30 13:56:38 +0200501 * (Not thread-safe to use same group in multiple threads)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100502 *
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200503 * \note In order to prevent timing attacks, this function
504 * executes the exact same sequence of (base field)
505 * operations for any valid m. It avoids any if-branch or
506 * array index depending on the value of m.
507 *
508 * \note If f_rng is not NULL, it is used to randomize intermediate
509 * results in order to prevent potential timing attacks
510 * targeting these results. It is recommended to always
511 * provide a non-NULL f_rng (the overhead is negligible).
512 *
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100513 * \param grp ECP group
514 * \param R Destination point
515 * \param m Integer by which to multiply
516 * \param P Point to multiply
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200517 * \param f_rng RNG function (see notes)
518 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100519 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100520 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 * MBEDTLS_ERR_ECP_INVALID_KEY if m is not a valid privkey
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +0100522 * or P is not a valid pubkey,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200523 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100524 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
526 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard09ceaf42013-11-20 23:06:14 +0100527 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100528
529/**
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200530 * \brief Multiplication and addition of two points by integers:
531 * R = m * P + n * Q
532 * (Not thread-safe to use same group in multiple threads)
533 *
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +0200534 * \note In contrast to mbedtls_ecp_mul(), this function does not guarantee
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200535 * a constant execution flow and timing.
536 *
537 * \param grp ECP group
538 * \param R Destination point
539 * \param m Integer by which to multiply P
540 * \param P Point to multiply by m
541 * \param n Integer by which to multiply Q
542 * \param Q Point to be multiplied by n
543 *
544 * \return 0 if successful,
545 * MBEDTLS_ERR_ECP_INVALID_KEY if m or n is not a valid privkey
546 * or P or Q is not a valid pubkey,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200547 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200548 */
549int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
550 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
551 const mbedtls_mpi *n, const mbedtls_ecp_point *Q );
552
553/**
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200554 * \brief Check that a point is a valid public key on this curve
555 *
556 * \param grp Curve/group the point should belong to
557 * \param pt Point to check
558 *
559 * \return 0 if point is a valid public key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 * MBEDTLS_ERR_ECP_INVALID_KEY otherwise.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200561 *
562 * \note This function only checks the point is non-zero, has valid
563 * coordinates and lies on the curve, but not that it is
564 * indeed a multiple of G. This is additional check is more
565 * expensive, isn't required by standards, and shouldn't be
566 * necessary if the group used has a small cofactor. In
567 * particular, it is useless for the NIST groups which all
568 * have a cofactor of 1.
569 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 * \note Uses bare components rather than an mbedtls_ecp_keypair structure
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200571 * in order to ease use with other structures such as
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 * mbedtls_ecdh_context of mbedtls_ecdsa_context.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200573 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200575
576/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 * \brief Check that an mbedtls_mpi is a valid private key for this curve
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200578 *
579 * \param grp Group used
580 * \param d Integer to check
581 *
582 * \return 0 if point is a valid private key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 * MBEDTLS_ERR_ECP_INVALID_KEY otherwise.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200584 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 * \note Uses bare components rather than an mbedtls_ecp_keypair structure
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200586 * in order to ease use with other structures such as
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 * mbedtls_ecdh_context of mbedtls_ecdsa_context.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200588 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp, const mbedtls_mpi *d );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200590
591/**
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +0200592 * \brief Generate a keypair with configurable base point
593 *
594 * \param grp ECP group
595 * \param G Chosen base point
596 * \param d Destination MPI (secret part)
597 * \param Q Destination point (public part)
598 * \param f_rng RNG function
599 * \param p_rng RNG parameter
600 *
601 * \return 0 if successful,
602 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
603 *
604 * \note Uses bare components rather than an mbedtls_ecp_keypair structure
605 * in order to ease use with other structures such as
606 * mbedtls_ecdh_context of mbedtls_ecdsa_context.
607 */
608int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp,
609 const mbedtls_ecp_point *G,
610 mbedtls_mpi *d, mbedtls_ecp_point *Q,
611 int (*f_rng)(void *, unsigned char *, size_t),
612 void *p_rng );
613
614/**
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100615 * \brief Generate a keypair
616 *
617 * \param grp ECP group
618 * \param d Destination MPI (secret part)
619 * \param Q Destination point (public part)
620 * \param f_rng RNG function
621 * \param p_rng RNG parameter
622 *
623 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200625 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 * \note Uses bare components rather than an mbedtls_ecp_keypair structure
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200627 * in order to ease use with other structures such as
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 * mbedtls_ecdh_context of mbedtls_ecdsa_context.
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100629 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100631 int (*f_rng)(void *, unsigned char *, size_t),
632 void *p_rng );
633
634/**
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +0100635 * \brief Generate a keypair
636 *
637 * \param grp_id ECP group identifier
638 * \param key Destination keypair
639 * \param f_rng RNG function
640 * \param p_rng RNG parameter
641 *
642 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +0100644 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +0100646 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
647
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +0100648/**
649 * \brief Check a public-private key pair
650 *
651 * \param pub Keypair structure holding a public key
652 * \param prv Keypair structure holding a private (plus public) key
653 *
Manuel Pégourié-Gonnard862d5032015-04-15 11:30:46 +0200654 * \return 0 if successful (keys are valid and match), or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA, or
656 * a MBEDTLS_ERR_ECP_XXX or MBEDTLS_ERR_MPI_XXX code.
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +0100657 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +0100659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660#if defined(MBEDTLS_SELF_TEST)
Janos Follathb0697532016-08-18 12:38:46 +0100661
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +0100662/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100663 * \brief Checkup routine
664 *
Manuel Pégourié-Gonnard13a1ef82014-03-27 20:12:44 +0100665 * \return 0 if successful, or 1 if a test failed
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100666 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667int mbedtls_ecp_self_test( int verbose );
Janos Follathb0697532016-08-18 12:38:46 +0100668
669#endif // MBEDTLS_SELF_TEST
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100670
671#ifdef __cplusplus
672}
673#endif
674
Janos Follathb0697532016-08-18 12:38:46 +0100675#else /* MBEDTLS_ECP_ALT */
676#include "ecp_alt.h"
677#endif /* MBEDTLS_ECP_ALT */
678
Paul Bakker9af723c2014-05-01 13:03:14 +0200679#endif /* ecp.h */