blob: 45a2452a1a7bb6a34a60292f3122e46200ffbbba [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02008 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010021 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000022 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010023 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#ifndef MBEDTLS_ECP_H
25#define MBEDTLS_ECP_H
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010026
Manuel Pégourié-Gonnardbdc96762013-10-03 11:50:39 +020027#include "bignum.h"
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010028
29/*
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +010030 * ECP error codes
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010031 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#define MBEDTLS_ERR_ECP_BAD_INPUT_DATA -0x4F80 /**< Bad input parameters to function. */
33#define MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL -0x4F00 /**< The buffer is too small to write to. */
34#define MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE -0x4E80 /**< Requested curve not available. */
35#define MBEDTLS_ERR_ECP_VERIFY_FAILED -0x4E00 /**< The signature is not valid. */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +020036#define MBEDTLS_ERR_ECP_ALLOC_FAILED -0x4D80 /**< Memory allocation failed. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#define MBEDTLS_ERR_ECP_RANDOM_FAILED -0x4D00 /**< Generation of random value, such as (ephemeral) key, failed. */
38#define MBEDTLS_ERR_ECP_INVALID_KEY -0x4C80 /**< Invalid private or public key. */
39#define MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH -0x4C00 /**< Signature is valid but shorter than the user-supplied length. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010040#define MBEDTLS_ERR_ECP_HW_ACCEL_FAILED -0x4B80 /**< ECP hardware accelerator failed. */
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +010041
Ron Eldor810e6502018-04-01 15:59:58 +030042#ifdef __cplusplus
43extern "C" {
44#endif
45
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010046/**
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +020047 * Domain parameters (curve, subgroup and generator) identifiers.
48 *
49 * Only curves over prime fields are supported.
50 *
51 * \warning This library does not support validation of arbitrary domain
52 * parameters. Therefore, only well-known domain parameters from trusted
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +020053 * sources should be used. See mbedtls_ecp_group_load().
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +020054 */
55typedef enum
56{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057 MBEDTLS_ECP_DP_NONE = 0,
58 MBEDTLS_ECP_DP_SECP192R1, /*!< 192-bits NIST curve */
59 MBEDTLS_ECP_DP_SECP224R1, /*!< 224-bits NIST curve */
60 MBEDTLS_ECP_DP_SECP256R1, /*!< 256-bits NIST curve */
61 MBEDTLS_ECP_DP_SECP384R1, /*!< 384-bits NIST curve */
62 MBEDTLS_ECP_DP_SECP521R1, /*!< 521-bits NIST curve */
63 MBEDTLS_ECP_DP_BP256R1, /*!< 256-bits Brainpool curve */
64 MBEDTLS_ECP_DP_BP384R1, /*!< 384-bits Brainpool curve */
65 MBEDTLS_ECP_DP_BP512R1, /*!< 512-bits Brainpool curve */
Manuel Pégourié-Gonnard07894332015-06-23 00:18:41 +020066 MBEDTLS_ECP_DP_CURVE25519, /*!< Curve25519 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067 MBEDTLS_ECP_DP_SECP192K1, /*!< 192-bits "Koblitz" curve */
68 MBEDTLS_ECP_DP_SECP224K1, /*!< 224-bits "Koblitz" curve */
69 MBEDTLS_ECP_DP_SECP256K1, /*!< 256-bits "Koblitz" curve */
70} mbedtls_ecp_group_id;
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +020071
72/**
Manuel Pégourié-Gonnard66153662013-12-03 14:12:26 +010073 * Number of supported curves (plus one for NONE).
74 *
75 * (Montgomery curves excluded for now.)
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +020076 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077#define MBEDTLS_ECP_DP_MAX 12
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +020078
79/**
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +020080 * Curve information for use by other modules
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020081 */
82typedef struct
83{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 mbedtls_ecp_group_id grp_id; /*!< Internal identifier */
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +020085 uint16_t tls_id; /*!< TLS NamedCurve identifier */
86 uint16_t bit_size; /*!< Curve size in bits */
87 const char *name; /*!< Human-friendly name */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088} mbedtls_ecp_curve_info;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020089
90/**
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010091 * \brief ECP point structure (jacobian coordinates)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +010092 *
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010093 * \note All functions expect and return points satisfying
94 * the following condition: Z == 0 or Z == 1. (Other
95 * values of Z are used by internal functions only.)
96 * The point is zero, or "at infinity", if Z == 0.
97 * Otherwise, X and Y are its standard (affine) coordinates.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010098 */
99typedef struct
100{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 mbedtls_mpi X; /*!< the point's X coordinate */
102 mbedtls_mpi Y; /*!< the point's Y coordinate */
103 mbedtls_mpi Z; /*!< the point's Z coordinate */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100104}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105mbedtls_ecp_point;
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100106
Ron Eldor05d0e512018-04-16 17:40:04 +0300107#if !defined(MBEDTLS_ECP_ALT)
108/*
109 * default mbed TLS elliptic curve arithmetic implementation
110 *
111 * (in case MBEDTLS_ECP_ALT is defined then the developer has to provide an
112 * alternative implementation for the whole module and it will replace this
113 * one.)
114 */
115
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100116/**
117 * \brief ECP group structure
118 *
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100119 * We consider two types of curves equations:
120 * 1. Short Weierstrass y^2 = x^3 + A x + B mod P (SEC1 + RFC 4492)
Manuel Pégourié-Gonnard07894332015-06-23 00:18:41 +0200121 * 2. Montgomery, y^2 = x^3 + A x^2 + x mod P (Curve25519 + draft)
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100122 * In both cases, a generator G for a prime-order subgroup is fixed. In the
123 * short weierstrass, this subgroup is actually the whole curve, and its
124 * cardinal is denoted by N.
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100125 *
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200126 * In the case of Short Weierstrass curves, our code requires that N is an odd
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 * prime. (Use odd in mbedtls_ecp_mul() and prime in mbedtls_ecdsa_sign() for blinding.)
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200128 *
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100129 * In the case of Montgomery curves, we don't store A but (A + 2) / 4 which is
Paul Bakker75342a62014-04-08 17:35:40 +0200130 * the quantity actually used in the formulas. Also, nbits is not the size of N
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100131 * but the required size for private keys.
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100132 *
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200133 * If modp is NULL, reduction modulo P is done using a generic algorithm.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 * Otherwise, it must point to a function that takes an mbedtls_mpi in the range
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200135 * 0..2^(2*pbits)-1 and transforms it in-place in an integer of little more
136 * than pbits, so that the integer may be efficiently brought in the 0..P-1
137 * range by a few additions or substractions. It must return 0 on success and
138 * non-zero on failure.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100139 */
140typedef struct
141{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 mbedtls_ecp_group_id id; /*!< internal group identifier */
143 mbedtls_mpi P; /*!< prime modulus of the base field */
144 mbedtls_mpi A; /*!< 1. A in the equation, or 2. (A + 2) / 4 */
145 mbedtls_mpi B; /*!< 1. B in the equation, or 2. unused */
146 mbedtls_ecp_point G; /*!< generator of the (sub)group used */
Nicholas Wilson54fc34e2016-05-16 15:15:45 +0100147 mbedtls_mpi N; /*!< the order of G */
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +0200148 size_t pbits; /*!< number of bits in P */
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100149 size_t nbits; /*!< number of bits in 1. P, or 2. private keys */
Manuel Pégourié-Gonnard1f82b042013-12-06 12:51:50 +0100150 unsigned int h; /*!< internal: 1 if the constants are static */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 int (*modp)(mbedtls_mpi *); /*!< function for fast reduction mod P */
152 int (*t_pre)(mbedtls_ecp_point *, void *); /*!< unused */
153 int (*t_post)(mbedtls_ecp_point *, void *); /*!< unused */
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100154 void *t_data; /*!< unused */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 mbedtls_ecp_point *T; /*!< pre-computed points for ecp_mul_comb() */
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +0200156 size_t T_size; /*!< number for pre-computed points */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100157}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158mbedtls_ecp_group;
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100159
160/**
Paul Bakker088c5c52014-04-25 11:11:10 +0200161 * \name SECTION: Module settings
162 *
163 * The configuration options you can set for this module are in this section.
164 * Either change them in config.h or define them on the compiler command line.
165 * \{
166 */
167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168#if !defined(MBEDTLS_ECP_MAX_BITS)
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200169/**
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200170 * Maximum size of the groups (that is, of N and P)
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100171 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172#define MBEDTLS_ECP_MAX_BITS 521 /**< Maximum bit size of groups */
Paul Bakkere1b665e2013-12-11 16:02:58 +0100173#endif
174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175#define MBEDTLS_ECP_MAX_BYTES ( ( MBEDTLS_ECP_MAX_BITS + 7 ) / 8 )
176#define MBEDTLS_ECP_MAX_PT_LEN ( 2 * MBEDTLS_ECP_MAX_BYTES + 1 )
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178#if !defined(MBEDTLS_ECP_WINDOW_SIZE)
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100179/*
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +0100180 * Maximum "window" size used for point multiplication.
181 * Default: 6.
182 * Minimum value: 2. Maximum value: 7.
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100183 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 * Result is an array of at most ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) )
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +0100185 * points used for point multiplication. This value is directly tied to EC
186 * peak memory usage, so decreasing it by one should roughly cut memory usage
187 * by two (if large curves are in use).
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100188 *
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +0100189 * Reduction in size may reduce speed, but larger curves are impacted first.
190 * Sample performances (in ECDHE handshakes/s, with FIXED_POINT_OPTIM = 1):
191 * w-size: 6 5 4 3 2
192 * 521 145 141 135 120 97
193 * 384 214 209 198 177 146
194 * 256 320 320 303 262 226
Paul Bakker088c5c52014-04-25 11:11:10 +0200195
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +0100196 * 224 475 475 453 398 342
197 * 192 640 640 633 587 476
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100198 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199#define MBEDTLS_ECP_WINDOW_SIZE 6 /**< Maximum window size used */
200#endif /* MBEDTLS_ECP_WINDOW_SIZE */
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +0100201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#if !defined(MBEDTLS_ECP_FIXED_POINT_OPTIM)
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +0100203/*
204 * Trade memory for speed on fixed-point multiplication.
205 *
206 * This speeds up repeated multiplication of the generator (that is, the
207 * multiplication in ECDSA signatures, and half of the multiplications in
208 * ECDSA verification and ECDHE) by a factor roughly 3 to 4.
209 *
210 * The cost is increasing EC peak memory usage by a factor roughly 2.
211 *
212 * Change this value to 0 to reduce peak memory usage.
213 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214#define MBEDTLS_ECP_FIXED_POINT_OPTIM 1 /**< Enable fixed-point speed-up */
215#endif /* MBEDTLS_ECP_FIXED_POINT_OPTIM */
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100216
Paul Bakker088c5c52014-04-25 11:11:10 +0200217/* \} name SECTION: Module settings */
218
Ron Eldorbf027e72018-04-09 15:51:19 +0300219#else /* MBEDTLS_ECP_ALT */
220#include "ecp_alt.h"
221#endif /* MBEDTLS_ECP_ALT */
222
Ron Eldor05d0e512018-04-16 17:40:04 +0300223/**
224 * \brief ECP key pair structure
225 *
226 * A generic key pair that could be used for ECDSA, fixed ECDH, etc.
227 *
228 * \note Members purposefully in the same order as struc mbedtls_ecdsa_context.
229 */
230typedef struct
231{
232 mbedtls_ecp_group grp; /*!< Elliptic curve and base point */
233 mbedtls_mpi d; /*!< our secret value */
234 mbedtls_ecp_point Q; /*!< our public value */
235}
236mbedtls_ecp_keypair;
237
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100238/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100239 * Point formats, from RFC 4492's enum ECPointFormat
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100240 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241#define MBEDTLS_ECP_PF_UNCOMPRESSED 0 /**< Uncompressed point format */
242#define MBEDTLS_ECP_PF_COMPRESSED 1 /**< Compressed point format */
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100243
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100244/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100245 * Some other constants from RFC 4492
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100246 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247#define MBEDTLS_ECP_TLS_NAMED_CURVE 3 /**< ECCurveType's named_curve */
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100248
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100249/**
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100250 * \brief Get the list of supported curves in order of preferrence
251 * (full information)
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200252 *
253 * \return A statically allocated array, the last entry is 0.
254 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list( void );
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200256
257/**
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100258 * \brief Get the list of supported curves in order of preferrence
259 * (grp_id only)
260 *
261 * \return A statically allocated array,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 * terminated with MBEDTLS_ECP_DP_NONE.
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100263 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list( void );
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100265
266/**
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200267 * \brief Get curve information from an internal group identifier
268 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 * \param grp_id A MBEDTLS_ECP_DP_XXX value
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200270 *
271 * \return The associated curve information or NULL
272 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273const 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 +0200274
275/**
276 * \brief Get curve information from a TLS NamedCurve value
277 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 * \param tls_id A MBEDTLS_ECP_DP_XXX value
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200279 *
280 * \return The associated curve information or NULL
281 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id( uint16_t tls_id );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200283
284/**
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100285 * \brief Get curve information from a human-readable name
286 *
287 * \param name The name
288 *
289 * \return The associated curve information or NULL
290 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name( const char *name );
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100292
293/**
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100294 * \brief Initialize a point (as zero)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100295 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296void mbedtls_ecp_point_init( mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100297
298/**
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100299 * \brief Initialize a group (to something meaningless)
300 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301void mbedtls_ecp_group_init( mbedtls_ecp_group *grp );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100302
303/**
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200304 * \brief Initialize a key pair (as an invalid one)
305 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306void mbedtls_ecp_keypair_init( mbedtls_ecp_keypair *key );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200307
308/**
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100309 * \brief Free the components of a point
310 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311void mbedtls_ecp_point_free( mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100312
313/**
314 * \brief Free the components of an ECP group
315 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316void mbedtls_ecp_group_free( mbedtls_ecp_group *grp );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100317
318/**
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200319 * \brief Free the components of a key pair
320 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair *key );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200322
323/**
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100324 * \brief Copy the contents of point Q into P
325 *
326 * \param P Destination point
327 * \param Q Source point
328 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100329 * \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é-Gonnard883f3132012-11-02 09:40:25 +0100331 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332int mbedtls_ecp_copy( mbedtls_ecp_point *P, const mbedtls_ecp_point *Q );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100333
334/**
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200335 * \brief Copy the contents of a group object
336 *
337 * \param dst Destination group
338 * \param src Source group
339 *
340 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200341 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200342 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst, const mbedtls_ecp_group *src );
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200344
345/**
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200346 * \brief Set a point to zero
347 *
348 * \param pt Destination point
349 *
350 * \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é-Gonnardcae6f3e2013-10-23 20:19:57 +0200352 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353int mbedtls_ecp_set_zero( mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200354
355/**
356 * \brief Tell if a point is zero
357 *
358 * \param pt Point to test
359 *
360 * \return 1 if point is zero, 0 otherwise
361 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200363
364/**
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200365 * \brief Compare two points
366 *
367 * \note This assumes the points are normalized. Otherwise,
368 * they may compare as "not equal" even if they are.
369 *
370 * \param P First point to compare
371 * \param Q Second point to compare
372 *
373 * \return 0 if the points are equal,
374 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA otherwise
375 */
376int mbedtls_ecp_point_cmp( const mbedtls_ecp_point *P,
377 const mbedtls_ecp_point *Q );
378
379/**
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100380 * \brief Import a non-zero point from two ASCII strings
381 *
382 * \param P Destination point
383 * \param radix Input numeric base
384 * \param x First affine coordinate as a null-terminated string
385 * \param y Second affine coordinate as a null-terminated string
386 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100388 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389int mbedtls_ecp_point_read_string( mbedtls_ecp_point *P, int radix,
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100390 const char *x, const char *y );
391
392/**
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100393 * \brief Export a point into unsigned binary data
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100394 *
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100395 * \param grp Group to which the point should belong
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100396 * \param P Point to export
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 * \param format Point format, should be a MBEDTLS_ECP_PF_XXX macro
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100398 * \param olen Length of the actual output
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100399 * \param buf Output buffer
400 * \param buflen Length of the output buffer
401 *
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100402 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 * or MBEDTLS_ERR_ECP_BAD_INPUT_DATA
404 * or MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100405 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100407 int format, size_t *olen,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100408 unsigned char *buf, size_t buflen );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100409
410/**
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100411 * \brief Import a point from unsigned binary data
412 *
413 * \param grp Group to which the point should belong
414 * \param P Point to import
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100415 * \param buf Input buffer
416 * \param ilen Actual length of input
417 *
418 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200420 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 * MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the point format
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100422 * is not implemented.
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100423 *
424 * \note This function does NOT check that the point actually
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 * belongs to the given group, see mbedtls_ecp_check_pubkey() for
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100426 * that.
427 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100429 const unsigned char *buf, size_t ilen );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100430
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100431/**
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200432 * \brief Import a point from a TLS ECPoint record
433 *
434 * \param grp ECP group used
435 * \param pt Destination point
436 * \param buf $(Start of input buffer)
437 * \param len Buffer length
438 *
Manuel Pégourié-Gonnard150c4f62014-11-21 09:14:52 +0100439 * \note buf is updated to point right after the ECPoint on exit
440 *
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +0200441 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 * MBEDTLS_ERR_MPI_XXX if initialization failed
443 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200444 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200446 const unsigned char **buf, size_t len );
447
448/**
449 * \brief Export a point as a TLS ECPoint record
450 *
451 * \param grp ECP group used
452 * \param pt Point to export
453 * \param format Export format
454 * \param olen length of data written
455 * \param buf Buffer to write to
456 * \param blen Buffer length
457 *
458 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 * or MBEDTLS_ERR_ECP_BAD_INPUT_DATA
460 * or MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200461 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462int mbedtls_ecp_tls_write_point( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt,
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200463 int format, size_t *olen,
464 unsigned char *buf, size_t blen );
465
466/**
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100467 * \brief Set a group using well-known domain parameters
468 *
469 * \param grp Destination group
Hanno Becker61937d42017-04-26 15:01:23 +0100470 * \param id Index in the list of well-known domain parameters
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100471 *
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +0200472 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 * MBEDTLS_ERR_MPI_XXX if initialization failed
474 * MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE for unkownn groups
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100475 *
Manuel Pégourié-Gonnardaff37e52015-05-11 18:11:57 +0200476 * \note Index should be a value of RFC 4492's enum NamedCurve,
477 * usually in the form of a MBEDTLS_ECP_DP_XXX macro.
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100478 */
Hanno Becker61937d42017-04-26 15:01:23 +0100479int mbedtls_ecp_group_load( mbedtls_ecp_group *grp, mbedtls_ecp_group_id id );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100480
481/**
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100482 * \brief Set a group from a TLS ECParameters record
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100483 *
484 * \param grp Destination group
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100485 * \param buf &(Start of input buffer)
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100486 * \param len Buffer length
487 *
Manuel Pégourié-Gonnard150c4f62014-11-21 09:14:52 +0100488 * \note buf is updated to point right after ECParameters on exit
489 *
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +0200490 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 * MBEDTLS_ERR_MPI_XXX if initialization failed
492 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100493 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494int 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 +0100495
496/**
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100497 * \brief Write the TLS ECParameters record for a group
498 *
499 * \param grp ECP group used
500 * \param olen Number of bytes actually written
501 * \param buf Buffer to write to
502 * \param blen Buffer length
503 *
504 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 * or MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100506 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, size_t *olen,
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100508 unsigned char *buf, size_t blen );
509
510/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100511 * \brief Multiplication by an integer: R = m * P
Paul Bakker6838bd12013-09-30 13:56:38 +0200512 * (Not thread-safe to use same group in multiple threads)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100513 *
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200514 * \note In order to prevent timing attacks, this function
515 * executes the exact same sequence of (base field)
516 * operations for any valid m. It avoids any if-branch or
517 * array index depending on the value of m.
518 *
519 * \note If f_rng is not NULL, it is used to randomize intermediate
520 * results in order to prevent potential timing attacks
521 * targeting these results. It is recommended to always
522 * provide a non-NULL f_rng (the overhead is negligible).
523 *
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100524 * \param grp ECP group
525 * \param R Destination point
526 * \param m Integer by which to multiply
527 * \param P Point to multiply
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200528 * \param f_rng RNG function (see notes)
529 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100530 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100531 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 * MBEDTLS_ERR_ECP_INVALID_KEY if m is not a valid privkey
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +0100533 * or P is not a valid pubkey,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200534 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100535 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
537 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard09ceaf42013-11-20 23:06:14 +0100538 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100539
540/**
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200541 * \brief Multiplication and addition of two points by integers:
542 * R = m * P + n * Q
543 * (Not thread-safe to use same group in multiple threads)
544 *
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +0200545 * \note In contrast to mbedtls_ecp_mul(), this function does not guarantee
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200546 * a constant execution flow and timing.
547 *
548 * \param grp ECP group
549 * \param R Destination point
550 * \param m Integer by which to multiply P
551 * \param P Point to multiply by m
552 * \param n Integer by which to multiply Q
553 * \param Q Point to be multiplied by n
554 *
555 * \return 0 if successful,
556 * MBEDTLS_ERR_ECP_INVALID_KEY if m or n is not a valid privkey
557 * or P or Q is not a valid pubkey,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200558 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200559 */
560int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
561 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
562 const mbedtls_mpi *n, const mbedtls_ecp_point *Q );
563
564/**
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200565 * \brief Check that a point is a valid public key on this curve
566 *
567 * \param grp Curve/group the point should belong to
568 * \param pt Point to check
569 *
570 * \return 0 if point is a valid public key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 * MBEDTLS_ERR_ECP_INVALID_KEY otherwise.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200572 *
573 * \note This function only checks the point is non-zero, has valid
574 * coordinates and lies on the curve, but not that it is
575 * indeed a multiple of G. This is additional check is more
576 * expensive, isn't required by standards, and shouldn't be
577 * necessary if the group used has a small cofactor. In
578 * particular, it is useless for the NIST groups which all
579 * have a cofactor of 1.
580 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 * \note Uses bare components rather than an mbedtls_ecp_keypair structure
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200582 * in order to ease use with other structures such as
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 * mbedtls_ecdh_context of mbedtls_ecdsa_context.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200584 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200586
587/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 * \brief Check that an mbedtls_mpi is a valid private key for this curve
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200589 *
590 * \param grp Group used
591 * \param d Integer to check
592 *
593 * \return 0 if point is a valid private key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 * MBEDTLS_ERR_ECP_INVALID_KEY otherwise.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200595 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 * \note Uses bare components rather than an mbedtls_ecp_keypair structure
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200597 * in order to ease use with other structures such as
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598 * mbedtls_ecdh_context of mbedtls_ecdsa_context.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200599 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp, const mbedtls_mpi *d );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200601
602/**
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +0200603 * \brief Generate a keypair with configurable base point
604 *
605 * \param grp ECP group
606 * \param G Chosen base point
607 * \param d Destination MPI (secret part)
608 * \param Q Destination point (public part)
609 * \param f_rng RNG function
610 * \param p_rng RNG parameter
611 *
612 * \return 0 if successful,
613 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
614 *
615 * \note Uses bare components rather than an mbedtls_ecp_keypair structure
616 * in order to ease use with other structures such as
617 * mbedtls_ecdh_context of mbedtls_ecdsa_context.
618 */
619int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp,
620 const mbedtls_ecp_point *G,
621 mbedtls_mpi *d, mbedtls_ecp_point *Q,
622 int (*f_rng)(void *, unsigned char *, size_t),
623 void *p_rng );
624
625/**
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100626 * \brief Generate a keypair
627 *
628 * \param grp ECP group
629 * \param d Destination MPI (secret part)
630 * \param Q Destination point (public part)
631 * \param f_rng RNG function
632 * \param p_rng RNG parameter
633 *
634 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200636 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 * \note Uses bare components rather than an mbedtls_ecp_keypair structure
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200638 * in order to ease use with other structures such as
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 * mbedtls_ecdh_context of mbedtls_ecdsa_context.
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100640 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100642 int (*f_rng)(void *, unsigned char *, size_t),
643 void *p_rng );
644
645/**
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +0100646 * \brief Generate a keypair
647 *
648 * \param grp_id ECP group identifier
649 * \param key Destination keypair
650 * \param f_rng RNG function
651 * \param p_rng RNG parameter
652 *
653 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +0100655 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +0100657 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
658
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +0100659/**
660 * \brief Check a public-private key pair
661 *
662 * \param pub Keypair structure holding a public key
663 * \param prv Keypair structure holding a private (plus public) key
664 *
Manuel Pégourié-Gonnard862d5032015-04-15 11:30:46 +0200665 * \return 0 if successful (keys are valid and match), or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA, or
667 * a MBEDTLS_ERR_ECP_XXX or MBEDTLS_ERR_MPI_XXX code.
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +0100668 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +0100670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671#if defined(MBEDTLS_SELF_TEST)
Janos Follathb0697532016-08-18 12:38:46 +0100672
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +0100673/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100674 * \brief Checkup routine
675 *
Manuel Pégourié-Gonnard13a1ef82014-03-27 20:12:44 +0100676 * \return 0 if successful, or 1 if a test failed
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100677 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678int mbedtls_ecp_self_test( int verbose );
Janos Follathb0697532016-08-18 12:38:46 +0100679
Janos Follath372697b2016-10-28 16:53:11 +0100680#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100681
682#ifdef __cplusplus
683}
684#endif
685
Paul Bakker9af723c2014-05-01 13:03:14 +0200686#endif /* ecp.h */