blob: 07ed110cdc5185f3124b46d7c48f3204a7b27b25 [file] [log] [blame]
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001/**
2 * \file ecp.h
3 *
4 * \brief Elliptic curves over GF(p)
5 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010020 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000021 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#ifndef MBEDTLS_ECP_H
24#define MBEDTLS_ECP_H
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010025
Manuel Pégourié-Gonnardbdc96762013-10-03 11:50:39 +020026#include "bignum.h"
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010027
28/*
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +010029 * ECP error codes
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010030 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#define MBEDTLS_ERR_ECP_BAD_INPUT_DATA -0x4F80 /**< Bad input parameters to function. */
32#define MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL -0x4F00 /**< The buffer is too small to write to. */
33#define MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE -0x4E80 /**< Requested curve not available. */
34#define MBEDTLS_ERR_ECP_VERIFY_FAILED -0x4E00 /**< The signature is not valid. */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +020035#define MBEDTLS_ERR_ECP_ALLOC_FAILED -0x4D80 /**< Memory allocation failed. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#define MBEDTLS_ERR_ECP_RANDOM_FAILED -0x4D00 /**< Generation of random value, such as (ephemeral) key, failed. */
37#define MBEDTLS_ERR_ECP_INVALID_KEY -0x4C80 /**< Invalid private or public key. */
38#define MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH -0x4C00 /**< Signature is valid but shorter than the user-supplied length. */
Manuel Pégourié-Gonnard5e3c62f2017-03-08 10:14:11 +010039#define MBEDTLS_ERR_ECP_IN_PROGRESS -0x4B80 /**< Operation in progress, try again with the same parameters. */
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +010040
Janos Follathb0697532016-08-18 12:38:46 +010041#if !defined(MBEDTLS_ECP_ALT)
Janos Follathc44ab972016-11-18 16:38:23 +000042/*
43 * default mbed TLS elliptic curve arithmetic implementation
44 *
45 * (in case MBEDTLS_ECP_ALT is defined then the developer has to provide an
46 * alternative implementation for the whole module and it will replace this
47 * one.)
48 */
Janos Follathb0697532016-08-18 12:38:46 +010049
Paul Bakker407a0da2013-06-27 14:29:21 +020050#ifdef __cplusplus
51extern "C" {
52#endif
53
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010054/**
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +020055 * Domain parameters (curve, subgroup and generator) identifiers.
56 *
57 * Only curves over prime fields are supported.
58 *
59 * \warning This library does not support validation of arbitrary domain
60 * parameters. Therefore, only well-known domain parameters from trusted
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +020061 * sources should be used. See mbedtls_ecp_group_load().
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +020062 */
63typedef enum
64{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065 MBEDTLS_ECP_DP_NONE = 0,
66 MBEDTLS_ECP_DP_SECP192R1, /*!< 192-bits NIST curve */
67 MBEDTLS_ECP_DP_SECP224R1, /*!< 224-bits NIST curve */
68 MBEDTLS_ECP_DP_SECP256R1, /*!< 256-bits NIST curve */
69 MBEDTLS_ECP_DP_SECP384R1, /*!< 384-bits NIST curve */
70 MBEDTLS_ECP_DP_SECP521R1, /*!< 521-bits NIST curve */
71 MBEDTLS_ECP_DP_BP256R1, /*!< 256-bits Brainpool curve */
72 MBEDTLS_ECP_DP_BP384R1, /*!< 384-bits Brainpool curve */
73 MBEDTLS_ECP_DP_BP512R1, /*!< 512-bits Brainpool curve */
Manuel Pégourié-Gonnard07894332015-06-23 00:18:41 +020074 MBEDTLS_ECP_DP_CURVE25519, /*!< Curve25519 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 MBEDTLS_ECP_DP_SECP192K1, /*!< 192-bits "Koblitz" curve */
76 MBEDTLS_ECP_DP_SECP224K1, /*!< 224-bits "Koblitz" curve */
77 MBEDTLS_ECP_DP_SECP256K1, /*!< 256-bits "Koblitz" curve */
78} mbedtls_ecp_group_id;
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +020079
80/**
Manuel Pégourié-Gonnard66153662013-12-03 14:12:26 +010081 * Number of supported curves (plus one for NONE).
82 *
83 * (Montgomery curves excluded for now.)
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +020084 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085#define MBEDTLS_ECP_DP_MAX 12
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +020086
87/**
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +020088 * Curve information for use by other modules
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020089 */
90typedef struct
91{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 mbedtls_ecp_group_id grp_id; /*!< Internal identifier */
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +020093 uint16_t tls_id; /*!< TLS NamedCurve identifier */
94 uint16_t bit_size; /*!< Curve size in bits */
95 const char *name; /*!< Human-friendly name */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096} mbedtls_ecp_curve_info;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020097
98/**
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010099 * \brief ECP point structure (jacobian coordinates)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100100 *
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100101 * \note All functions expect and return points satisfying
102 * the following condition: Z == 0 or Z == 1. (Other
103 * values of Z are used by internal functions only.)
104 * The point is zero, or "at infinity", if Z == 0.
105 * Otherwise, X and Y are its standard (affine) coordinates.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100106 */
107typedef struct
108{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 mbedtls_mpi X; /*!< the point's X coordinate */
110 mbedtls_mpi Y; /*!< the point's Y coordinate */
111 mbedtls_mpi Z; /*!< the point's Z coordinate */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100112}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113mbedtls_ecp_point;
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100114
115/**
116 * \brief ECP group structure
117 *
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100118 * We consider two types of curves equations:
119 * 1. Short Weierstrass y^2 = x^3 + A x + B mod P (SEC1 + RFC 4492)
Manuel Pégourié-Gonnard07894332015-06-23 00:18:41 +0200120 * 2. Montgomery, y^2 = x^3 + A x^2 + x mod P (Curve25519 + draft)
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100121 * In both cases, a generator G for a prime-order subgroup is fixed. In the
122 * short weierstrass, this subgroup is actually the whole curve, and its
123 * cardinal is denoted by N.
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100124 *
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200125 * In the case of Short Weierstrass curves, our code requires that N is an odd
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 * prime. (Use odd in mbedtls_ecp_mul() and prime in mbedtls_ecdsa_sign() for blinding.)
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200127 *
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100128 * In the case of Montgomery curves, we don't store A but (A + 2) / 4 which is
Paul Bakker75342a62014-04-08 17:35:40 +0200129 * the quantity actually used in the formulas. Also, nbits is not the size of N
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100130 * but the required size for private keys.
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100131 *
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200132 * If modp is NULL, reduction modulo P is done using a generic algorithm.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 * Otherwise, it must point to a function that takes an mbedtls_mpi in the range
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200134 * 0..2^(2*pbits)-1 and transforms it in-place in an integer of little more
135 * than pbits, so that the integer may be efficiently brought in the 0..P-1
136 * range by a few additions or substractions. It must return 0 on success and
137 * non-zero on failure.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100138 */
139typedef struct
140{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 mbedtls_ecp_group_id id; /*!< internal group identifier */
142 mbedtls_mpi P; /*!< prime modulus of the base field */
143 mbedtls_mpi A; /*!< 1. A in the equation, or 2. (A + 2) / 4 */
144 mbedtls_mpi B; /*!< 1. B in the equation, or 2. unused */
145 mbedtls_ecp_point G; /*!< generator of the (sub)group used */
146 mbedtls_mpi N; /*!< 1. the order of G, or 2. unused */
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +0200147 size_t pbits; /*!< number of bits in P */
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100148 size_t nbits; /*!< number of bits in 1. P, or 2. private keys */
Manuel Pégourié-Gonnard1f82b042013-12-06 12:51:50 +0100149 unsigned int h; /*!< internal: 1 if the constants are static */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 int (*modp)(mbedtls_mpi *); /*!< function for fast reduction mod P */
151 int (*t_pre)(mbedtls_ecp_point *, void *); /*!< unused */
152 int (*t_post)(mbedtls_ecp_point *, void *); /*!< unused */
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100153 void *t_data; /*!< unused */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 mbedtls_ecp_point *T; /*!< pre-computed points for ecp_mul_comb() */
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +0200155 size_t T_size; /*!< number for pre-computed points */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100156}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157mbedtls_ecp_group;
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100158
159/**
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200160 * \brief ECP key pair structure
161 *
162 * A generic key pair that could be used for ECDSA, fixed ECDH, etc.
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200163 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 * \note Members purposefully in the same order as struc mbedtls_ecdsa_context.
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200165 */
166typedef struct
167{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 mbedtls_ecp_group grp; /*!< Elliptic curve and base point */
169 mbedtls_mpi d; /*!< our secret value */
170 mbedtls_ecp_point Q; /*!< our public value */
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200171}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172mbedtls_ecp_keypair;
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200173
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200174#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardb5a50e72017-04-20 16:06:13 +0200175
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200176/**
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +0200177 * \brief Internal restart context for multiplication
178 *
179 * \note Opaque struct
180 */
181typedef struct mbedtls_ecp_restart_mul mbedtls_ecp_restart_mul_ctx;
182
183/**
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200184 * \brief Internal restart context for ecp_muladd()
185 *
186 * \note Opaque struct
187 */
188typedef struct mbedtls_ecp_restart_muladd mbedtls_ecp_restart_muladd_ctx;
189
190/**
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200191 * \brief General context for resuming ECC operations
192 */
193typedef struct
194{
Manuel Pégourié-Gonnard646393b2017-04-20 10:03:45 +0200195 unsigned ops_done; /*!< current ops count */
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +0200196 unsigned depth; /*!< call depth (0 = top-level) */
Manuel Pégourié-Gonnard646393b2017-04-20 10:03:45 +0200197 mbedtls_ecp_restart_mul_ctx *rsm; /*!< ecp_mul_comb() sub-context */
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200198 mbedtls_ecp_restart_muladd_ctx *ma; /*!< ecp_muladd() sub-context */
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200199} mbedtls_ecp_restart_ctx;
Manuel Pégourié-Gonnardb5a50e72017-04-20 16:06:13 +0200200
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +0200201/*
202 * Operation counts for restartable functions
203 */
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200204#define MBEDTLS_ECP_OPS_CHK 3 /*!< basic ops count for ecp_check_pubkey() */
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +0200205#define MBEDTLS_ECP_OPS_DBL 8 /*!< basic ops count for ecp_double_jac() */
206#define MBEDTLS_ECP_OPS_ADD 11 /*!< basic ops count for see ecp_add_mixed() */
207#define MBEDTLS_ECP_OPS_INV 120 /*!< empirical equivalent for mpi_mod_inv() */
208
209/**
210 * \brief Internal; for restartable functions in other modules.
211 * Check and update basic ops budget.
212 *
213 * \param grp Group structure
214 * \param rs_ctx Restart context
215 * \param ops Number of basic ops to do
216 *
217 * \return 0 is doing 'ops' basic ops is still allowed,
218 * MBEDTLS_ERR_ECP_IN_PROGRESS otherwise.
219 */
220int mbedtls_ecp_check_budget( const mbedtls_ecp_group *grp,
221 mbedtls_ecp_restart_ctx *rs_ctx,
222 unsigned ops );
223
224/* Utility macro for checking and updating ops budget */
225#define MBEDTLS_ECP_BUDGET( ops ) MBEDTLS_MPI_CHK( mbedtls_ecp_check_budget( grp, rs_ctx, ops ) );
226
Manuel Pégourié-Gonnardb5a50e72017-04-20 16:06:13 +0200227#else /* MBEDTLS_ECP_RESTARTABLE */
228
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +0200229#define MBEDTLS_ECP_BUDGET( ops ) /* no-op; for compatibility */
230
Manuel Pégourié-Gonnardb5a50e72017-04-20 16:06:13 +0200231/* We want to declare restartable versions of existing functions anyway */
232typedef void mbedtls_ecp_restart_ctx;
233
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200234#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200235
Paul Bakker088c5c52014-04-25 11:11:10 +0200236/**
237 * \name SECTION: Module settings
238 *
239 * The configuration options you can set for this module are in this section.
240 * Either change them in config.h or define them on the compiler command line.
241 * \{
242 */
243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244#if !defined(MBEDTLS_ECP_MAX_BITS)
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200245/**
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200246 * Maximum size of the groups (that is, of N and P)
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100247 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248#define MBEDTLS_ECP_MAX_BITS 521 /**< Maximum bit size of groups */
Paul Bakkere1b665e2013-12-11 16:02:58 +0100249#endif
250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251#define MBEDTLS_ECP_MAX_BYTES ( ( MBEDTLS_ECP_MAX_BITS + 7 ) / 8 )
252#define MBEDTLS_ECP_MAX_PT_LEN ( 2 * MBEDTLS_ECP_MAX_BYTES + 1 )
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254#if !defined(MBEDTLS_ECP_WINDOW_SIZE)
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100255/*
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +0100256 * Maximum "window" size used for point multiplication.
257 * Default: 6.
258 * Minimum value: 2. Maximum value: 7.
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100259 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 * Result is an array of at most ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) )
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +0100261 * points used for point multiplication. This value is directly tied to EC
262 * peak memory usage, so decreasing it by one should roughly cut memory usage
263 * by two (if large curves are in use).
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100264 *
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +0100265 * Reduction in size may reduce speed, but larger curves are impacted first.
266 * Sample performances (in ECDHE handshakes/s, with FIXED_POINT_OPTIM = 1):
267 * w-size: 6 5 4 3 2
268 * 521 145 141 135 120 97
269 * 384 214 209 198 177 146
270 * 256 320 320 303 262 226
271 * 224 475 475 453 398 342
272 * 192 640 640 633 587 476
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100273 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274#define MBEDTLS_ECP_WINDOW_SIZE 6 /**< Maximum window size used */
275#endif /* MBEDTLS_ECP_WINDOW_SIZE */
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +0100276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277#if !defined(MBEDTLS_ECP_FIXED_POINT_OPTIM)
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +0100278/*
279 * Trade memory for speed on fixed-point multiplication.
280 *
281 * This speeds up repeated multiplication of the generator (that is, the
282 * multiplication in ECDSA signatures, and half of the multiplications in
283 * ECDSA verification and ECDHE) by a factor roughly 3 to 4.
284 *
285 * The cost is increasing EC peak memory usage by a factor roughly 2.
286 *
287 * Change this value to 0 to reduce peak memory usage.
288 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289#define MBEDTLS_ECP_FIXED_POINT_OPTIM 1 /**< Enable fixed-point speed-up */
290#endif /* MBEDTLS_ECP_FIXED_POINT_OPTIM */
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100291
Paul Bakker088c5c52014-04-25 11:11:10 +0200292/* \} name SECTION: Module settings */
293
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100294/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100295 * Point formats, from RFC 4492's enum ECPointFormat
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100296 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297#define MBEDTLS_ECP_PF_UNCOMPRESSED 0 /**< Uncompressed point format */
298#define MBEDTLS_ECP_PF_COMPRESSED 1 /**< Compressed point format */
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100299
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100300/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100301 * Some other constants from RFC 4492
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100302 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303#define MBEDTLS_ECP_TLS_NAMED_CURVE 3 /**< ECCurveType's named_curve */
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100304
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200305#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100306/**
307 * \brief Set the maximum number of basic operations done in a row.
308 *
309 * If more operations are needed to complete a computation,
310 * MBEDTLS_ERR_ECP_IN_PROGRESS will be returned by the
Manuel Pégourié-Gonnard8467e682017-04-20 09:47:06 +0200311 * function performing the computation. It is then the
312 * caller's responsibility to either call again with the same
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +0200313 * parameters until it returns 0 or an error code; or to free
Manuel Pégourié-Gonnard8467e682017-04-20 09:47:06 +0200314 * the restart context if the operation is to be aborted.
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100315 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +0200316 * It is strictly required that all input parameters and the
317 * restart context be the same on successive calls for the
318 * same operation, but output parameters need not be the
319 * same; they must not be used until the function finally
320 * returns 0.
321 *
Manuel Pégourié-Gonnard8f28add2017-04-19 10:20:49 +0200322 * This only affects functions that accept a pointer to a
323 * \c mbedtls_ecp_restart_ctx as an argument, and only works
324 * if that pointer valid (in particular, not NULL).
325 *
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100326 * \param max_ops Maximum number of basic operations done in a row.
327 * Default: 0 (unlimited).
328 * Lower (non-zero) values mean ECC functions will block for
329 * a lesser maximum amount of time.
330 *
Manuel Pégourié-Gonnarde6854492017-03-20 14:35:19 +0100331 * \note A "basic operation" is defined as a rough equivalent of a
332 * multiplication in GF(p) for the NIST P-256 curve.
333 * As an indication, with default settings, a scalar
334 * multiplication (full run of \c mbedtls_ecp_mul()) is:
335 * - about 3300 basic operations for P-256
336 * - about 9400 basic operations for P-384
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100337 *
Manuel Pégourié-Gonnarde58f65a2017-03-20 14:59:54 +0100338 * \note Very low values are not always respected: sometimes
Manuel Pégourié-Gonnard1c678e02017-03-20 13:39:39 +0100339 * functions need to block for a minimum number of
340 * operations, and will do so even if max_ops is set to a
341 * lower value. That minimum depends on the curve size, and
342 * can be made lower by decreasing the value of
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +0200343 * \c MBEDTLS_ECP_WINDOW_SIZE. As an indication, here is the
344 * lowest effective value for various curves and values of
345 * that parameter (w for short):
346 * w=6 w=5 w=4 w=3 w=2
347 * P-256 208 208 160 136 124
348 * P-384 682 416 320 272 248
349 * P-521 1364 832 640 544 496
Manuel Pégourié-Gonnarde58f65a2017-03-20 14:59:54 +0100350 *
351 * \note This setting is currently ignored by Curve25519
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100352 */
353void mbedtls_ecp_set_max_ops( unsigned max_ops );
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200354
355/**
356 * \brief Check if restart is enabled (max_ops != 0)
357 *
358 * \return 0 if max_ops == 0 (restart disabled)
359 * 1 otherwise (restart enabled)
360 */
361int mbedtls_ecp_restart_enabled( void );
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200362#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100363
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100364/**
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100365 * \brief Get the list of supported curves in order of preferrence
366 * (full information)
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200367 *
368 * \return A statically allocated array, the last entry is 0.
369 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list( void );
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200371
372/**
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100373 * \brief Get the list of supported curves in order of preferrence
374 * (grp_id only)
375 *
376 * \return A statically allocated array,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 * terminated with MBEDTLS_ECP_DP_NONE.
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100378 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list( void );
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100380
381/**
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200382 * \brief Get curve information from an internal group identifier
383 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 * \param grp_id A MBEDTLS_ECP_DP_XXX value
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200385 *
386 * \return The associated curve information or NULL
387 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388const 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 +0200389
390/**
391 * \brief Get curve information from a TLS NamedCurve value
392 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 * \param tls_id A MBEDTLS_ECP_DP_XXX value
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200394 *
395 * \return The associated curve information or NULL
396 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id( uint16_t tls_id );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200398
399/**
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100400 * \brief Get curve information from a human-readable name
401 *
402 * \param name The name
403 *
404 * \return The associated curve information or NULL
405 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name( const char *name );
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100407
408/**
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100409 * \brief Initialize a point (as zero)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100410 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411void mbedtls_ecp_point_init( mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100412
413/**
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100414 * \brief Initialize a group (to something meaningless)
415 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416void mbedtls_ecp_group_init( mbedtls_ecp_group *grp );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100417
418/**
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200419 * \brief Initialize a key pair (as an invalid one)
420 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421void mbedtls_ecp_keypair_init( mbedtls_ecp_keypair *key );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200422
423/**
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100424 * \brief Free the components of a point
425 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426void mbedtls_ecp_point_free( mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100427
428/**
429 * \brief Free the components of an ECP group
430 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431void mbedtls_ecp_group_free( mbedtls_ecp_group *grp );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100432
433/**
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200434 * \brief Free the components of a key pair
435 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair *key );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200437
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200438#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200439/**
440 * \brief Initialize a restart context
441 */
442void mbedtls_ecp_restart_init( mbedtls_ecp_restart_ctx *ctx );
443
444/**
445 * \brief Free the components of a restart context
446 */
447void mbedtls_ecp_restart_free( mbedtls_ecp_restart_ctx *ctx );
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200448#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200449
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200450/**
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100451 * \brief Copy the contents of point Q into P
452 *
453 * \param P Destination point
454 * \param Q Source point
455 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100456 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200457 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100458 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459int mbedtls_ecp_copy( mbedtls_ecp_point *P, const mbedtls_ecp_point *Q );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100460
461/**
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200462 * \brief Copy the contents of a group object
463 *
464 * \param dst Destination group
465 * \param src Source group
466 *
467 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200468 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200469 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst, const mbedtls_ecp_group *src );
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200471
472/**
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200473 * \brief Set a point to zero
474 *
475 * \param pt Destination point
476 *
477 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200478 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200479 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480int mbedtls_ecp_set_zero( mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200481
482/**
483 * \brief Tell if a point is zero
484 *
485 * \param pt Point to test
486 *
487 * \return 1 if point is zero, 0 otherwise
488 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200490
491/**
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200492 * \brief Compare two points
493 *
494 * \note This assumes the points are normalized. Otherwise,
495 * they may compare as "not equal" even if they are.
496 *
497 * \param P First point to compare
498 * \param Q Second point to compare
499 *
500 * \return 0 if the points are equal,
501 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA otherwise
502 */
503int mbedtls_ecp_point_cmp( const mbedtls_ecp_point *P,
504 const mbedtls_ecp_point *Q );
505
506/**
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100507 * \brief Import a non-zero point from two ASCII strings
508 *
509 * \param P Destination point
510 * \param radix Input numeric base
511 * \param x First affine coordinate as a null-terminated string
512 * \param y Second affine coordinate as a null-terminated string
513 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100515 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516int mbedtls_ecp_point_read_string( mbedtls_ecp_point *P, int radix,
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100517 const char *x, const char *y );
518
519/**
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100520 * \brief Export a point into unsigned binary data
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100521 *
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100522 * \param grp Group to which the point should belong
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100523 * \param P Point to export
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 * \param format Point format, should be a MBEDTLS_ECP_PF_XXX macro
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100525 * \param olen Length of the actual output
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100526 * \param buf Output buffer
527 * \param buflen Length of the output buffer
528 *
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100529 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 * or MBEDTLS_ERR_ECP_BAD_INPUT_DATA
531 * or MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100532 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100534 int format, size_t *olen,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100535 unsigned char *buf, size_t buflen );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100536
537/**
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100538 * \brief Import a point from unsigned binary data
539 *
540 * \param grp Group to which the point should belong
541 * \param P Point to import
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100542 * \param buf Input buffer
543 * \param ilen Actual length of input
544 *
545 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200547 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 * MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the point format
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100549 * is not implemented.
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100550 *
551 * \note This function does NOT check that the point actually
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 * belongs to the given group, see mbedtls_ecp_check_pubkey() for
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100553 * that.
554 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100556 const unsigned char *buf, size_t ilen );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100557
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100558/**
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200559 * \brief Import a point from a TLS ECPoint record
560 *
561 * \param grp ECP group used
562 * \param pt Destination point
563 * \param buf $(Start of input buffer)
564 * \param len Buffer length
565 *
Manuel Pégourié-Gonnard150c4f62014-11-21 09:14:52 +0100566 * \note buf is updated to point right after the ECPoint on exit
567 *
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +0200568 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 * MBEDTLS_ERR_MPI_XXX if initialization failed
570 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200571 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200573 const unsigned char **buf, size_t len );
574
575/**
576 * \brief Export a point as a TLS ECPoint record
577 *
578 * \param grp ECP group used
579 * \param pt Point to export
580 * \param format Export format
581 * \param olen length of data written
582 * \param buf Buffer to write to
583 * \param blen Buffer length
584 *
585 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 * or MBEDTLS_ERR_ECP_BAD_INPUT_DATA
587 * or MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200588 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589int mbedtls_ecp_tls_write_point( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt,
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200590 int format, size_t *olen,
591 unsigned char *buf, size_t blen );
592
593/**
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100594 * \brief Set a group using well-known domain parameters
595 *
596 * \param grp Destination group
597 * \param index Index in the list of well-known domain parameters
598 *
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +0200599 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 * MBEDTLS_ERR_MPI_XXX if initialization failed
601 * MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE for unkownn groups
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100602 *
Manuel Pégourié-Gonnardaff37e52015-05-11 18:11:57 +0200603 * \note Index should be a value of RFC 4492's enum NamedCurve,
604 * usually in the form of a MBEDTLS_ECP_DP_XXX macro.
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100605 */
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200606int mbedtls_ecp_group_load( mbedtls_ecp_group *grp, mbedtls_ecp_group_id index );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100607
608/**
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100609 * \brief Set a group from a TLS ECParameters record
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100610 *
611 * \param grp Destination group
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100612 * \param buf &(Start of input buffer)
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100613 * \param len Buffer length
614 *
Manuel Pégourié-Gonnard150c4f62014-11-21 09:14:52 +0100615 * \note buf is updated to point right after ECParameters on exit
616 *
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +0200617 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 * MBEDTLS_ERR_MPI_XXX if initialization failed
619 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100620 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621int 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 +0100622
623/**
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100624 * \brief Write the TLS ECParameters record for a group
625 *
626 * \param grp ECP group used
627 * \param olen Number of bytes actually written
628 * \param buf Buffer to write to
629 * \param blen Buffer length
630 *
631 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 * or MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100633 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, size_t *olen,
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100635 unsigned char *buf, size_t blen );
636
637/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100638 * \brief Multiplication by an integer: R = m * P
Paul Bakker6838bd12013-09-30 13:56:38 +0200639 * (Not thread-safe to use same group in multiple threads)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100640 *
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200641 * \note In order to prevent timing attacks, this function
642 * executes the exact same sequence of (base field)
643 * operations for any valid m. It avoids any if-branch or
644 * array index depending on the value of m.
645 *
646 * \note If f_rng is not NULL, it is used to randomize intermediate
647 * results in order to prevent potential timing attacks
648 * targeting these results. It is recommended to always
649 * provide a non-NULL f_rng (the overhead is negligible).
650 *
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100651 * \param grp ECP group
652 * \param R Destination point
653 * \param m Integer by which to multiply
654 * \param P Point to multiply
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200655 * \param f_rng RNG function (see notes)
656 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100657 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100658 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 * MBEDTLS_ERR_ECP_INVALID_KEY if m is not a valid privkey
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +0100660 * or P is not a valid pubkey,
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200661 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
662 */
663int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
664 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
665 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
666
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200667/**
668 * \brief Restartable version of \c mbedtls_ecp_mul()
669 *
670 * \note Performs the same job as \c mbedtls_ecp_mul(), but can
671 * return early and restart according to the limit set with
672 * \c mbedtls_ecp_set_max_ops() to reduce blocking.
673 *
674 * \param grp ECP group
675 * \param R Destination point
676 * \param m Integer by which to multiply
677 * \param P Point to multiply
678 * \param f_rng RNG function (see notes)
679 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard8467e682017-04-20 09:47:06 +0200680 * \param rs_ctx Restart context
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200681 *
682 * \return See \c mbedtls_ecp_mul(), or
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100683 * MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnard8467e682017-04-20 09:47:06 +0200684 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100685 */
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200686int mbedtls_ecp_mul_restartable( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200688 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
689 mbedtls_ecp_restart_ctx *rs_ctx );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100690
691/**
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200692 * \brief Multiplication and addition of two points by integers:
693 * R = m * P + n * Q
694 * (Not thread-safe to use same group in multiple threads)
695 *
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +0200696 * \note In contrast to mbedtls_ecp_mul(), this function does not guarantee
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200697 * a constant execution flow and timing.
698 *
699 * \param grp ECP group
700 * \param R Destination point
701 * \param m Integer by which to multiply P
702 * \param P Point to multiply by m
703 * \param n Integer by which to multiply Q
704 * \param Q Point to be multiplied by n
705 *
706 * \return 0 if successful,
707 * MBEDTLS_ERR_ECP_INVALID_KEY if m or n is not a valid privkey
708 * or P or Q is not a valid pubkey,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200709 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200710 */
711int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
712 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
713 const mbedtls_mpi *n, const mbedtls_ecp_point *Q );
714
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200715/**
716 * \brief Restartable version of \c mbedtls_ecp_muladd()
717 *
718 * \note Performs the same job as \c mbedtls_ecp_muladd(), but can
719 * return early and restart according to the limit set with
720 * \c mbedtls_ecp_set_max_ops() to reduce blocking.
721 *
722 * \param grp ECP group
723 * \param R Destination point
724 * \param m Integer by which to multiply P
725 * \param P Point to multiply by m
726 * \param n Integer by which to multiply Q
727 * \param Q Point to be multiplied by n
728 * \param rs_ctx Restart context
729 *
730 * \return See \c mbedtls_ecp_muladd(), or
731 * MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
732 * operations was reached: see \c mbedtls_ecp_set_max_ops().
733 */
734int mbedtls_ecp_muladd_restartable(
735 mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
736 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
737 const mbedtls_mpi *n, const mbedtls_ecp_point *Q,
738 mbedtls_ecp_restart_ctx *rs_ctx );
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200739
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200740/**
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200741 * \brief Check that a point is a valid public key on this curve
742 *
743 * \param grp Curve/group the point should belong to
744 * \param pt Point to check
745 *
746 * \return 0 if point is a valid public key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 * MBEDTLS_ERR_ECP_INVALID_KEY otherwise.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200748 *
749 * \note This function only checks the point is non-zero, has valid
750 * coordinates and lies on the curve, but not that it is
751 * indeed a multiple of G. This is additional check is more
752 * expensive, isn't required by standards, and shouldn't be
753 * necessary if the group used has a small cofactor. In
754 * particular, it is useless for the NIST groups which all
755 * have a cofactor of 1.
756 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 * \note Uses bare components rather than an mbedtls_ecp_keypair structure
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200758 * in order to ease use with other structures such as
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 * mbedtls_ecdh_context of mbedtls_ecdsa_context.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200760 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200762
763/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 * \brief Check that an mbedtls_mpi is a valid private key for this curve
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200765 *
766 * \param grp Group used
767 * \param d Integer to check
768 *
769 * \return 0 if point is a valid private key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 * MBEDTLS_ERR_ECP_INVALID_KEY otherwise.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200771 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 * \note Uses bare components rather than an mbedtls_ecp_keypair structure
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200773 * in order to ease use with other structures such as
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774 * mbedtls_ecdh_context of mbedtls_ecdsa_context.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200775 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp, const mbedtls_mpi *d );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200777
778/**
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +0200779 * \brief Generate a private key
780 *
781 * \param grp ECP group
782 * \param d Destination MPI (secret part)
783 * \param f_rng RNG function
784 * \param p_rng RNG parameter
785 *
786 * \return 0 if successful,
787 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
788 */
789int mbedtls_ecp_gen_privkey( const mbedtls_ecp_group *grp,
790 mbedtls_mpi *d,
791 int (*f_rng)(void *, unsigned char *, size_t),
792 void *p_rng );
793
794/**
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +0200795 * \brief Generate a keypair with configurable base point
796 *
797 * \param grp ECP group
798 * \param G Chosen base point
799 * \param d Destination MPI (secret part)
800 * \param Q Destination point (public part)
801 * \param f_rng RNG function
802 * \param p_rng RNG parameter
803 *
804 * \return 0 if successful,
805 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
806 *
807 * \note Uses bare components rather than an mbedtls_ecp_keypair structure
808 * in order to ease use with other structures such as
809 * mbedtls_ecdh_context of mbedtls_ecdsa_context.
810 */
811int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp,
812 const mbedtls_ecp_point *G,
813 mbedtls_mpi *d, mbedtls_ecp_point *Q,
814 int (*f_rng)(void *, unsigned char *, size_t),
815 void *p_rng );
816
817/**
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100818 * \brief Generate a keypair
819 *
820 * \param grp ECP group
821 * \param d Destination MPI (secret part)
822 * \param Q Destination point (public part)
823 * \param f_rng RNG function
824 * \param p_rng RNG parameter
825 *
826 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200828 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829 * \note Uses bare components rather than an mbedtls_ecp_keypair structure
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200830 * in order to ease use with other structures such as
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 * mbedtls_ecdh_context of mbedtls_ecdsa_context.
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100832 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100834 int (*f_rng)(void *, unsigned char *, size_t),
835 void *p_rng );
836
837/**
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +0100838 * \brief Generate a keypair
839 *
840 * \param grp_id ECP group identifier
841 * \param key Destination keypair
842 * \param f_rng RNG function
843 * \param p_rng RNG parameter
844 *
845 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +0100847 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +0100849 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
850
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +0100851/**
852 * \brief Check a public-private key pair
853 *
854 * \param pub Keypair structure holding a public key
855 * \param prv Keypair structure holding a private (plus public) key
856 *
Manuel Pégourié-Gonnard862d5032015-04-15 11:30:46 +0200857 * \return 0 if successful (keys are valid and match), or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA, or
859 * a MBEDTLS_ERR_ECP_XXX or MBEDTLS_ERR_MPI_XXX code.
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +0100860 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +0100862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863#if defined(MBEDTLS_SELF_TEST)
Janos Follathb0697532016-08-18 12:38:46 +0100864
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +0100865/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100866 * \brief Checkup routine
867 *
Manuel Pégourié-Gonnard13a1ef82014-03-27 20:12:44 +0100868 * \return 0 if successful, or 1 if a test failed
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100869 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870int mbedtls_ecp_self_test( int verbose );
Janos Follathb0697532016-08-18 12:38:46 +0100871
Janos Follath372697b2016-10-28 16:53:11 +0100872#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100873
874#ifdef __cplusplus
875}
876#endif
877
Janos Follathb0697532016-08-18 12:38:46 +0100878#else /* MBEDTLS_ECP_ALT */
879#include "ecp_alt.h"
880#endif /* MBEDTLS_ECP_ALT */
881
Paul Bakker9af723c2014-05-01 13:03:14 +0200882#endif /* ecp.h */