blob: 2a43a7a9daea84e67acdf5c32bfca620aa113b29 [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)
Janos Follath372697b2016-10-28 16:53:11 +010041// default mbed TLS elliptic curve arithmetic implementation
Janos Follathb0697532016-08-18 12:38:46 +010042//
Janos Follath372697b2016-10-28 16:53:11 +010043// (in case MBEDTLS_ECP_ALT is defined then the developer has to provide an
44// alternative implementation for the whole module and it will replace this
45// one.)
Janos Follathb0697532016-08-18 12:38:46 +010046
Paul Bakker407a0da2013-06-27 14:29:21 +020047#ifdef __cplusplus
48extern "C" {
49#endif
50
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010051/**
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +020052 * Domain parameters (curve, subgroup and generator) identifiers.
53 *
54 * Only curves over prime fields are supported.
55 *
56 * \warning This library does not support validation of arbitrary domain
57 * parameters. Therefore, only well-known domain parameters from trusted
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +020058 * sources should be used. See mbedtls_ecp_group_load().
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +020059 */
60typedef enum
61{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062 MBEDTLS_ECP_DP_NONE = 0,
63 MBEDTLS_ECP_DP_SECP192R1, /*!< 192-bits NIST curve */
64 MBEDTLS_ECP_DP_SECP224R1, /*!< 224-bits NIST curve */
65 MBEDTLS_ECP_DP_SECP256R1, /*!< 256-bits NIST curve */
66 MBEDTLS_ECP_DP_SECP384R1, /*!< 384-bits NIST curve */
67 MBEDTLS_ECP_DP_SECP521R1, /*!< 521-bits NIST curve */
68 MBEDTLS_ECP_DP_BP256R1, /*!< 256-bits Brainpool curve */
69 MBEDTLS_ECP_DP_BP384R1, /*!< 384-bits Brainpool curve */
70 MBEDTLS_ECP_DP_BP512R1, /*!< 512-bits Brainpool curve */
Manuel Pégourié-Gonnard07894332015-06-23 00:18:41 +020071 MBEDTLS_ECP_DP_CURVE25519, /*!< Curve25519 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072 MBEDTLS_ECP_DP_SECP192K1, /*!< 192-bits "Koblitz" curve */
73 MBEDTLS_ECP_DP_SECP224K1, /*!< 224-bits "Koblitz" curve */
74 MBEDTLS_ECP_DP_SECP256K1, /*!< 256-bits "Koblitz" curve */
75} mbedtls_ecp_group_id;
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +020076
77/**
Manuel Pégourié-Gonnard66153662013-12-03 14:12:26 +010078 * Number of supported curves (plus one for NONE).
79 *
80 * (Montgomery curves excluded for now.)
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +020081 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082#define MBEDTLS_ECP_DP_MAX 12
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +020083
84/**
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +020085 * Curve information for use by other modules
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020086 */
87typedef struct
88{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 mbedtls_ecp_group_id grp_id; /*!< Internal identifier */
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +020090 uint16_t tls_id; /*!< TLS NamedCurve identifier */
91 uint16_t bit_size; /*!< Curve size in bits */
92 const char *name; /*!< Human-friendly name */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093} mbedtls_ecp_curve_info;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020094
95/**
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010096 * \brief ECP point structure (jacobian coordinates)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +010097 *
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010098 * \note All functions expect and return points satisfying
99 * the following condition: Z == 0 or Z == 1. (Other
100 * values of Z are used by internal functions only.)
101 * The point is zero, or "at infinity", if Z == 0.
102 * Otherwise, X and Y are its standard (affine) coordinates.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100103 */
104typedef struct
105{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106 mbedtls_mpi X; /*!< the point's X coordinate */
107 mbedtls_mpi Y; /*!< the point's Y coordinate */
108 mbedtls_mpi Z; /*!< the point's Z coordinate */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100109}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110mbedtls_ecp_point;
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100111
112/**
113 * \brief ECP group structure
114 *
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100115 * We consider two types of curves equations:
116 * 1. Short Weierstrass y^2 = x^3 + A x + B mod P (SEC1 + RFC 4492)
Manuel Pégourié-Gonnard07894332015-06-23 00:18:41 +0200117 * 2. Montgomery, y^2 = x^3 + A x^2 + x mod P (Curve25519 + draft)
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100118 * In both cases, a generator G for a prime-order subgroup is fixed. In the
119 * short weierstrass, this subgroup is actually the whole curve, and its
120 * cardinal is denoted by N.
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100121 *
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200122 * In the case of Short Weierstrass curves, our code requires that N is an odd
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 * prime. (Use odd in mbedtls_ecp_mul() and prime in mbedtls_ecdsa_sign() for blinding.)
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200124 *
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100125 * In the case of Montgomery curves, we don't store A but (A + 2) / 4 which is
Paul Bakker75342a62014-04-08 17:35:40 +0200126 * the quantity actually used in the formulas. Also, nbits is not the size of N
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100127 * but the required size for private keys.
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100128 *
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200129 * If modp is NULL, reduction modulo P is done using a generic algorithm.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 * Otherwise, it must point to a function that takes an mbedtls_mpi in the range
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200131 * 0..2^(2*pbits)-1 and transforms it in-place in an integer of little more
132 * than pbits, so that the integer may be efficiently brought in the 0..P-1
133 * range by a few additions or substractions. It must return 0 on success and
134 * non-zero on failure.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100135 */
136typedef struct
137{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 mbedtls_ecp_group_id id; /*!< internal group identifier */
139 mbedtls_mpi P; /*!< prime modulus of the base field */
140 mbedtls_mpi A; /*!< 1. A in the equation, or 2. (A + 2) / 4 */
141 mbedtls_mpi B; /*!< 1. B in the equation, or 2. unused */
142 mbedtls_ecp_point G; /*!< generator of the (sub)group used */
143 mbedtls_mpi N; /*!< 1. the order of G, or 2. unused */
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +0200144 size_t pbits; /*!< number of bits in P */
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100145 size_t nbits; /*!< number of bits in 1. P, or 2. private keys */
Manuel Pégourié-Gonnard1f82b042013-12-06 12:51:50 +0100146 unsigned int h; /*!< internal: 1 if the constants are static */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 int (*modp)(mbedtls_mpi *); /*!< function for fast reduction mod P */
148 int (*t_pre)(mbedtls_ecp_point *, void *); /*!< unused */
149 int (*t_post)(mbedtls_ecp_point *, void *); /*!< unused */
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100150 void *t_data; /*!< unused */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_ecp_point *T; /*!< pre-computed points for ecp_mul_comb() */
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +0200152 size_t T_size; /*!< number for pre-computed points */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100153}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154mbedtls_ecp_group;
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100155
156/**
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200157 * \brief ECP key pair structure
158 *
159 * A generic key pair that could be used for ECDSA, fixed ECDH, etc.
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200160 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 * \note Members purposefully in the same order as struc mbedtls_ecdsa_context.
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200162 */
163typedef struct
164{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 mbedtls_ecp_group grp; /*!< Elliptic curve and base point */
166 mbedtls_mpi d; /*!< our secret value */
167 mbedtls_ecp_point Q; /*!< our public value */
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200168}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169mbedtls_ecp_keypair;
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200170
Paul Bakker088c5c52014-04-25 11:11:10 +0200171/**
172 * \name SECTION: Module settings
173 *
174 * The configuration options you can set for this module are in this section.
175 * Either change them in config.h or define them on the compiler command line.
176 * \{
177 */
178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179#if !defined(MBEDTLS_ECP_MAX_BITS)
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200180/**
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200181 * Maximum size of the groups (that is, of N and P)
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100182 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183#define MBEDTLS_ECP_MAX_BITS 521 /**< Maximum bit size of groups */
Paul Bakkere1b665e2013-12-11 16:02:58 +0100184#endif
185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186#define MBEDTLS_ECP_MAX_BYTES ( ( MBEDTLS_ECP_MAX_BITS + 7 ) / 8 )
187#define MBEDTLS_ECP_MAX_PT_LEN ( 2 * MBEDTLS_ECP_MAX_BYTES + 1 )
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189#if !defined(MBEDTLS_ECP_WINDOW_SIZE)
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100190/*
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +0100191 * Maximum "window" size used for point multiplication.
192 * Default: 6.
193 * Minimum value: 2. Maximum value: 7.
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100194 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 * Result is an array of at most ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) )
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +0100196 * points used for point multiplication. This value is directly tied to EC
197 * peak memory usage, so decreasing it by one should roughly cut memory usage
198 * by two (if large curves are in use).
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100199 *
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +0100200 * Reduction in size may reduce speed, but larger curves are impacted first.
201 * Sample performances (in ECDHE handshakes/s, with FIXED_POINT_OPTIM = 1):
202 * w-size: 6 5 4 3 2
203 * 521 145 141 135 120 97
204 * 384 214 209 198 177 146
205 * 256 320 320 303 262 226
Paul Bakker088c5c52014-04-25 11:11:10 +0200206
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +0100207 * 224 475 475 453 398 342
208 * 192 640 640 633 587 476
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100209 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210#define MBEDTLS_ECP_WINDOW_SIZE 6 /**< Maximum window size used */
211#endif /* MBEDTLS_ECP_WINDOW_SIZE */
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +0100212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213#if !defined(MBEDTLS_ECP_FIXED_POINT_OPTIM)
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +0100214/*
215 * Trade memory for speed on fixed-point multiplication.
216 *
217 * This speeds up repeated multiplication of the generator (that is, the
218 * multiplication in ECDSA signatures, and half of the multiplications in
219 * ECDSA verification and ECDHE) by a factor roughly 3 to 4.
220 *
221 * The cost is increasing EC peak memory usage by a factor roughly 2.
222 *
223 * Change this value to 0 to reduce peak memory usage.
224 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225#define MBEDTLS_ECP_FIXED_POINT_OPTIM 1 /**< Enable fixed-point speed-up */
226#endif /* MBEDTLS_ECP_FIXED_POINT_OPTIM */
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100227
Paul Bakker088c5c52014-04-25 11:11:10 +0200228/* \} name SECTION: Module settings */
229
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100230/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100231 * Point formats, from RFC 4492's enum ECPointFormat
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100232 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233#define MBEDTLS_ECP_PF_UNCOMPRESSED 0 /**< Uncompressed point format */
234#define MBEDTLS_ECP_PF_COMPRESSED 1 /**< Compressed point format */
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100235
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100236/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100237 * Some other constants from RFC 4492
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100238 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239#define MBEDTLS_ECP_TLS_NAMED_CURVE 3 /**< ECCurveType's named_curve */
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100240
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100241/**
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100242 * \brief Get the list of supported curves in order of preferrence
243 * (full information)
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200244 *
245 * \return A statically allocated array, the last entry is 0.
246 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list( void );
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200248
249/**
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100250 * \brief Get the list of supported curves in order of preferrence
251 * (grp_id only)
252 *
253 * \return A statically allocated array,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 * terminated with MBEDTLS_ECP_DP_NONE.
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100255 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list( void );
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100257
258/**
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200259 * \brief Get curve information from an internal group identifier
260 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 * \param grp_id A MBEDTLS_ECP_DP_XXX value
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200262 *
263 * \return The associated curve information or NULL
264 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265const 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 +0200266
267/**
268 * \brief Get curve information from a TLS NamedCurve value
269 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 * \param tls_id A MBEDTLS_ECP_DP_XXX value
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200271 *
272 * \return The associated curve information or NULL
273 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id( uint16_t tls_id );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200275
276/**
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100277 * \brief Get curve information from a human-readable name
278 *
279 * \param name The name
280 *
281 * \return The associated curve information or NULL
282 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name( const char *name );
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100284
285/**
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100286 * \brief Initialize a point (as zero)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100287 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288void mbedtls_ecp_point_init( mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100289
290/**
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100291 * \brief Initialize a group (to something meaningless)
292 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293void mbedtls_ecp_group_init( mbedtls_ecp_group *grp );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100294
295/**
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200296 * \brief Initialize a key pair (as an invalid one)
297 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298void mbedtls_ecp_keypair_init( mbedtls_ecp_keypair *key );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200299
300/**
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100301 * \brief Free the components of a point
302 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303void mbedtls_ecp_point_free( mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100304
305/**
306 * \brief Free the components of an ECP group
307 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308void mbedtls_ecp_group_free( mbedtls_ecp_group *grp );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100309
310/**
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200311 * \brief Free the components of a key pair
312 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair *key );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200314
315/**
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100316 * \brief Copy the contents of point Q into P
317 *
318 * \param P Destination point
319 * \param Q Source point
320 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100321 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200322 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100323 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324int mbedtls_ecp_copy( mbedtls_ecp_point *P, const mbedtls_ecp_point *Q );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100325
326/**
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200327 * \brief Copy the contents of a group object
328 *
329 * \param dst Destination group
330 * \param src Source group
331 *
332 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200333 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200334 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst, const mbedtls_ecp_group *src );
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200336
337/**
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200338 * \brief Set a point to zero
339 *
340 * \param pt Destination point
341 *
342 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200343 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200344 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345int mbedtls_ecp_set_zero( mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200346
347/**
348 * \brief Tell if a point is zero
349 *
350 * \param pt Point to test
351 *
352 * \return 1 if point is zero, 0 otherwise
353 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200355
356/**
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200357 * \brief Compare two points
358 *
359 * \note This assumes the points are normalized. Otherwise,
360 * they may compare as "not equal" even if they are.
361 *
362 * \param P First point to compare
363 * \param Q Second point to compare
364 *
365 * \return 0 if the points are equal,
366 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA otherwise
367 */
368int mbedtls_ecp_point_cmp( const mbedtls_ecp_point *P,
369 const mbedtls_ecp_point *Q );
370
371/**
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100372 * \brief Import a non-zero point from two ASCII strings
373 *
374 * \param P Destination point
375 * \param radix Input numeric base
376 * \param x First affine coordinate as a null-terminated string
377 * \param y Second affine coordinate as a null-terminated string
378 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100380 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381int mbedtls_ecp_point_read_string( mbedtls_ecp_point *P, int radix,
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100382 const char *x, const char *y );
383
384/**
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100385 * \brief Export a point into unsigned binary data
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100386 *
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100387 * \param grp Group to which the point should belong
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100388 * \param P Point to export
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 * \param format Point format, should be a MBEDTLS_ECP_PF_XXX macro
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100390 * \param olen Length of the actual output
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100391 * \param buf Output buffer
392 * \param buflen Length of the output buffer
393 *
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100394 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 * or MBEDTLS_ERR_ECP_BAD_INPUT_DATA
396 * or MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100397 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100399 int format, size_t *olen,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100400 unsigned char *buf, size_t buflen );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100401
402/**
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100403 * \brief Import a point from unsigned binary data
404 *
405 * \param grp Group to which the point should belong
406 * \param P Point to import
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100407 * \param buf Input buffer
408 * \param ilen Actual length of input
409 *
410 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200412 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 * MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the point format
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100414 * is not implemented.
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100415 *
416 * \note This function does NOT check that the point actually
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 * belongs to the given group, see mbedtls_ecp_check_pubkey() for
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100418 * that.
419 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100421 const unsigned char *buf, size_t ilen );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100422
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100423/**
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200424 * \brief Import a point from a TLS ECPoint record
425 *
426 * \param grp ECP group used
427 * \param pt Destination point
428 * \param buf $(Start of input buffer)
429 * \param len Buffer length
430 *
Manuel Pégourié-Gonnard150c4f62014-11-21 09:14:52 +0100431 * \note buf is updated to point right after the ECPoint on exit
432 *
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +0200433 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 * MBEDTLS_ERR_MPI_XXX if initialization failed
435 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200436 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200438 const unsigned char **buf, size_t len );
439
440/**
441 * \brief Export a point as a TLS ECPoint record
442 *
443 * \param grp ECP group used
444 * \param pt Point to export
445 * \param format Export format
446 * \param olen length of data written
447 * \param buf Buffer to write to
448 * \param blen Buffer length
449 *
450 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 * or MBEDTLS_ERR_ECP_BAD_INPUT_DATA
452 * or MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200453 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454int mbedtls_ecp_tls_write_point( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt,
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200455 int format, size_t *olen,
456 unsigned char *buf, size_t blen );
457
458/**
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100459 * \brief Set a group using well-known domain parameters
460 *
461 * \param grp Destination group
462 * \param index Index in the list of well-known domain parameters
463 *
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +0200464 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 * MBEDTLS_ERR_MPI_XXX if initialization failed
466 * MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE for unkownn groups
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100467 *
Manuel Pégourié-Gonnardaff37e52015-05-11 18:11:57 +0200468 * \note Index should be a value of RFC 4492's enum NamedCurve,
469 * usually in the form of a MBEDTLS_ECP_DP_XXX macro.
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100470 */
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200471int mbedtls_ecp_group_load( mbedtls_ecp_group *grp, mbedtls_ecp_group_id index );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100472
473/**
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100474 * \brief Set a group from a TLS ECParameters record
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100475 *
476 * \param grp Destination group
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100477 * \param buf &(Start of input buffer)
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100478 * \param len Buffer length
479 *
Manuel Pégourié-Gonnard150c4f62014-11-21 09:14:52 +0100480 * \note buf is updated to point right after ECParameters on exit
481 *
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +0200482 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 * MBEDTLS_ERR_MPI_XXX if initialization failed
484 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100485 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486int 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 +0100487
488/**
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100489 * \brief Write the TLS ECParameters record for a group
490 *
491 * \param grp ECP group used
492 * \param olen Number of bytes actually written
493 * \param buf Buffer to write to
494 * \param blen Buffer length
495 *
496 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 * or MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100498 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, size_t *olen,
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100500 unsigned char *buf, size_t blen );
501
502/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100503 * \brief Multiplication by an integer: R = m * P
Paul Bakker6838bd12013-09-30 13:56:38 +0200504 * (Not thread-safe to use same group in multiple threads)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100505 *
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200506 * \note In order to prevent timing attacks, this function
507 * executes the exact same sequence of (base field)
508 * operations for any valid m. It avoids any if-branch or
509 * array index depending on the value of m.
510 *
511 * \note If f_rng is not NULL, it is used to randomize intermediate
512 * results in order to prevent potential timing attacks
513 * targeting these results. It is recommended to always
514 * provide a non-NULL f_rng (the overhead is negligible).
515 *
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100516 * \param grp ECP group
517 * \param R Destination point
518 * \param m Integer by which to multiply
519 * \param P Point to multiply
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200520 * \param f_rng RNG function (see notes)
521 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100522 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100523 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 * MBEDTLS_ERR_ECP_INVALID_KEY if m is not a valid privkey
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +0100525 * or P is not a valid pubkey,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200526 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100527 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
529 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard09ceaf42013-11-20 23:06:14 +0100530 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100531
532/**
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200533 * \brief Multiplication and addition of two points by integers:
534 * R = m * P + n * Q
535 * (Not thread-safe to use same group in multiple threads)
536 *
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +0200537 * \note In contrast to mbedtls_ecp_mul(), this function does not guarantee
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200538 * a constant execution flow and timing.
539 *
540 * \param grp ECP group
541 * \param R Destination point
542 * \param m Integer by which to multiply P
543 * \param P Point to multiply by m
544 * \param n Integer by which to multiply Q
545 * \param Q Point to be multiplied by n
546 *
547 * \return 0 if successful,
548 * MBEDTLS_ERR_ECP_INVALID_KEY if m or n is not a valid privkey
549 * or P or Q is not a valid pubkey,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200550 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200551 */
552int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
553 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
554 const mbedtls_mpi *n, const mbedtls_ecp_point *Q );
555
556/**
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200557 * \brief Check that a point is a valid public key on this curve
558 *
559 * \param grp Curve/group the point should belong to
560 * \param pt Point to check
561 *
562 * \return 0 if point is a valid public key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 * MBEDTLS_ERR_ECP_INVALID_KEY otherwise.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200564 *
565 * \note This function only checks the point is non-zero, has valid
566 * coordinates and lies on the curve, but not that it is
567 * indeed a multiple of G. This is additional check is more
568 * expensive, isn't required by standards, and shouldn't be
569 * necessary if the group used has a small cofactor. In
570 * particular, it is useless for the NIST groups which all
571 * have a cofactor of 1.
572 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 * \note Uses bare components rather than an mbedtls_ecp_keypair structure
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200574 * in order to ease use with other structures such as
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 * mbedtls_ecdh_context of mbedtls_ecdsa_context.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200576 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200578
579/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 * \brief Check that an mbedtls_mpi is a valid private key for this curve
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200581 *
582 * \param grp Group used
583 * \param d Integer to check
584 *
585 * \return 0 if point is a valid private key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 * MBEDTLS_ERR_ECP_INVALID_KEY otherwise.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200587 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 * \note Uses bare components rather than an mbedtls_ecp_keypair structure
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200589 * in order to ease use with other structures such as
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 * mbedtls_ecdh_context of mbedtls_ecdsa_context.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200591 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp, const mbedtls_mpi *d );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200593
594/**
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +0200595 * \brief Generate a keypair with configurable base point
596 *
597 * \param grp ECP group
598 * \param G Chosen base point
599 * \param d Destination MPI (secret part)
600 * \param Q Destination point (public part)
601 * \param f_rng RNG function
602 * \param p_rng RNG parameter
603 *
604 * \return 0 if successful,
605 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
606 *
607 * \note Uses bare components rather than an mbedtls_ecp_keypair structure
608 * in order to ease use with other structures such as
609 * mbedtls_ecdh_context of mbedtls_ecdsa_context.
610 */
611int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp,
612 const mbedtls_ecp_point *G,
613 mbedtls_mpi *d, mbedtls_ecp_point *Q,
614 int (*f_rng)(void *, unsigned char *, size_t),
615 void *p_rng );
616
617/**
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100618 * \brief Generate a keypair
619 *
620 * \param grp ECP group
621 * \param d Destination MPI (secret part)
622 * \param Q Destination point (public part)
623 * \param f_rng RNG function
624 * \param p_rng RNG parameter
625 *
626 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200628 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 * \note Uses bare components rather than an mbedtls_ecp_keypair structure
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200630 * in order to ease use with other structures such as
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 * mbedtls_ecdh_context of mbedtls_ecdsa_context.
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100632 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100634 int (*f_rng)(void *, unsigned char *, size_t),
635 void *p_rng );
636
637/**
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +0100638 * \brief Generate a keypair
639 *
640 * \param grp_id ECP group identifier
641 * \param key Destination keypair
642 * \param f_rng RNG function
643 * \param p_rng RNG parameter
644 *
645 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +0100647 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +0100649 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
650
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +0100651/**
652 * \brief Check a public-private key pair
653 *
654 * \param pub Keypair structure holding a public key
655 * \param prv Keypair structure holding a private (plus public) key
656 *
Manuel Pégourié-Gonnard862d5032015-04-15 11:30:46 +0200657 * \return 0 if successful (keys are valid and match), or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA, or
659 * a MBEDTLS_ERR_ECP_XXX or MBEDTLS_ERR_MPI_XXX code.
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +0100660 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +0100662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663#if defined(MBEDTLS_SELF_TEST)
Janos Follathb0697532016-08-18 12:38:46 +0100664
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +0100665/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100666 * \brief Checkup routine
667 *
Manuel Pégourié-Gonnard13a1ef82014-03-27 20:12:44 +0100668 * \return 0 if successful, or 1 if a test failed
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100669 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670int mbedtls_ecp_self_test( int verbose );
Janos Follathb0697532016-08-18 12:38:46 +0100671
Janos Follath372697b2016-10-28 16:53:11 +0100672#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100673
674#ifdef __cplusplus
675}
676#endif
677
Janos Follathb0697532016-08-18 12:38:46 +0100678#else /* MBEDTLS_ECP_ALT */
679#include "ecp_alt.h"
680#endif /* MBEDTLS_ECP_ALT */
681
Paul Bakker9af723c2014-05-01 13:03:14 +0200682#endif /* ecp.h */