blob: 2fb1af49a2ef7c1aa668d8863f45cf6f2adecfa7 [file] [log] [blame]
Fabio Utzigba05f2a2017-12-05 11:00:41 -02001/**
2 * \file ecp.h
3 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -02004 * \brief This file provides an API for Elliptic Curves over GF(P) (ECP).
Fabio Utzigba05f2a2017-12-05 11:00:41 -02005 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -02006 * The use of ECP in cryptography and TLS is defined in
7 * <em>Standards for Efficient Cryptography Group (SECG): SEC1
8 * Elliptic Curve Cryptography</em> and
9 * <em>RFC-4492: Elliptic Curve Cryptography (ECC) Cipher Suites
10 * for Transport Layer Security (TLS)</em>.
11 *
12 * <em>RFC-2409: The Internet Key Exchange (IKE)</em> defines ECP
13 * group types.
14 *
15 */
16
17/*
18 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
Fabio Utzigba05f2a2017-12-05 11:00:41 -020019 * SPDX-License-Identifier: Apache-2.0
20 *
21 * Licensed under the Apache License, Version 2.0 (the "License"); you may
22 * not use this file except in compliance with the License.
23 * You may obtain a copy of the License at
24 *
25 * http://www.apache.org/licenses/LICENSE-2.0
26 *
27 * Unless required by applicable law or agreed to in writing, software
28 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
29 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30 * See the License for the specific language governing permissions and
31 * limitations under the License.
32 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -020033 * This file is part of Mbed TLS (https://tls.mbed.org)
Fabio Utzigba05f2a2017-12-05 11:00:41 -020034 */
Fabio Utzig3ac36ea2018-12-27 12:35:39 -020035
Fabio Utzigba05f2a2017-12-05 11:00:41 -020036#ifndef MBEDTLS_ECP_H
37#define MBEDTLS_ECP_H
38
39#include "bignum.h"
40
41/*
42 * ECP error codes
43 */
44#define MBEDTLS_ERR_ECP_BAD_INPUT_DATA -0x4F80 /**< Bad input parameters to function. */
45#define MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL -0x4F00 /**< The buffer is too small to write to. */
Fabio Utzig3ac36ea2018-12-27 12:35:39 -020046#define MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE -0x4E80 /**< The requested feature is not available, for example, the requested curve is not supported. */
Fabio Utzigba05f2a2017-12-05 11:00:41 -020047#define MBEDTLS_ERR_ECP_VERIFY_FAILED -0x4E00 /**< The signature is not valid. */
48#define MBEDTLS_ERR_ECP_ALLOC_FAILED -0x4D80 /**< Memory allocation failed. */
Fabio Utzig3ac36ea2018-12-27 12:35:39 -020049#define MBEDTLS_ERR_ECP_RANDOM_FAILED -0x4D00 /**< Generation of random value, such as ephemeral key, failed. */
Fabio Utzigba05f2a2017-12-05 11:00:41 -020050#define MBEDTLS_ERR_ECP_INVALID_KEY -0x4C80 /**< Invalid private or public key. */
Fabio Utzig3ac36ea2018-12-27 12:35:39 -020051#define MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH -0x4C00 /**< The buffer contains a valid signature followed by more data. */
52
53/* MBEDTLS_ERR_ECP_HW_ACCEL_FAILED is deprecated and should not be used. */
54#define MBEDTLS_ERR_ECP_HW_ACCEL_FAILED -0x4B80 /**< The ECP hardware accelerator failed. */
55
56#define MBEDTLS_ERR_ECP_IN_PROGRESS -0x4B00 /**< Operation in progress, call again with the same parameters to continue. */
57
58#ifdef __cplusplus
59extern "C" {
60#endif
61
62/**
63 * Domain-parameter identifiers: curve, subgroup, and generator.
64 *
65 * \note Only curves over prime fields are supported.
66 *
67 * \warning This library does not support validation of arbitrary domain
68 * parameters. Therefore, only standardized domain parameters from trusted
69 * sources should be used. See mbedtls_ecp_group_load().
70 */
71typedef enum
72{
73 MBEDTLS_ECP_DP_NONE = 0, /*!< Curve not defined. */
74 MBEDTLS_ECP_DP_SECP192R1, /*!< Domain parameters for the 192-bit curve defined by FIPS 186-4 and SEC1. */
75 MBEDTLS_ECP_DP_SECP224R1, /*!< Domain parameters for the 224-bit curve defined by FIPS 186-4 and SEC1. */
76 MBEDTLS_ECP_DP_SECP256R1, /*!< Domain parameters for the 256-bit curve defined by FIPS 186-4 and SEC1. */
77 MBEDTLS_ECP_DP_SECP384R1, /*!< Domain parameters for the 384-bit curve defined by FIPS 186-4 and SEC1. */
78 MBEDTLS_ECP_DP_SECP521R1, /*!< Domain parameters for the 521-bit curve defined by FIPS 186-4 and SEC1. */
79 MBEDTLS_ECP_DP_BP256R1, /*!< Domain parameters for 256-bit Brainpool curve. */
80 MBEDTLS_ECP_DP_BP384R1, /*!< Domain parameters for 384-bit Brainpool curve. */
81 MBEDTLS_ECP_DP_BP512R1, /*!< Domain parameters for 512-bit Brainpool curve. */
82 MBEDTLS_ECP_DP_CURVE25519, /*!< Domain parameters for Curve25519. */
83 MBEDTLS_ECP_DP_SECP192K1, /*!< Domain parameters for 192-bit "Koblitz" curve. */
84 MBEDTLS_ECP_DP_SECP224K1, /*!< Domain parameters for 224-bit "Koblitz" curve. */
85 MBEDTLS_ECP_DP_SECP256K1, /*!< Domain parameters for 256-bit "Koblitz" curve. */
86 MBEDTLS_ECP_DP_CURVE448, /*!< Domain parameters for Curve448. */
87} mbedtls_ecp_group_id;
88
89/**
90 * The number of supported curves, plus one for #MBEDTLS_ECP_DP_NONE.
91 *
92 * \note Montgomery curves are currently excluded.
93 */
94#define MBEDTLS_ECP_DP_MAX 12
95
96/**
97 * Curve information, for use by other modules.
98 */
99typedef struct mbedtls_ecp_curve_info
100{
101 mbedtls_ecp_group_id grp_id; /*!< An internal identifier. */
102 uint16_t tls_id; /*!< The TLS NamedCurve identifier. */
103 uint16_t bit_size; /*!< The curve size in bits. */
104 const char *name; /*!< A human-friendly name. */
105} mbedtls_ecp_curve_info;
106
107/**
108 * \brief The ECP point structure, in Jacobian coordinates.
109 *
110 * \note All functions expect and return points satisfying
111 * the following condition: <code>Z == 0</code> or
112 * <code>Z == 1</code>. Other values of \p Z are
113 * used only by internal functions.
114 * The point is zero, or "at infinity", if <code>Z == 0</code>.
115 * Otherwise, \p X and \p Y are its standard (affine)
116 * coordinates.
117 */
118typedef struct mbedtls_ecp_point
119{
120 mbedtls_mpi X; /*!< The X coordinate of the ECP point. */
121 mbedtls_mpi Y; /*!< The Y coordinate of the ECP point. */
122 mbedtls_mpi Z; /*!< The Z coordinate of the ECP point. */
123}
124mbedtls_ecp_point;
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200125
126#if !defined(MBEDTLS_ECP_ALT)
127/*
128 * default mbed TLS elliptic curve arithmetic implementation
129 *
130 * (in case MBEDTLS_ECP_ALT is defined then the developer has to provide an
131 * alternative implementation for the whole module and it will replace this
132 * one.)
133 */
134
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200135/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200136 * \brief The ECP group structure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200137 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200138 * We consider two types of curve equations:
139 * <ul><li>Short Weierstrass: <code>y^2 = x^3 + A x + B mod P</code>
140 * (SEC1 + RFC-4492)</li>
141 * <li>Montgomery: <code>y^2 = x^3 + A x^2 + x mod P</code> (Curve25519,
142 * Curve448)</li></ul>
143 * In both cases, the generator (\p G) for a prime-order subgroup is fixed.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200144 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200145 * For Short Weierstrass, this subgroup is the whole curve, and its
146 * cardinality is denoted by \p N. Our code requires that \p N is an
147 * odd prime as mbedtls_ecp_mul() requires an odd number, and
148 * mbedtls_ecdsa_sign() requires that it is prime for blinding purposes.
149 *
150 * For Montgomery curves, we do not store \p A, but <code>(A + 2) / 4</code>,
151 * which is the quantity used in the formulas. Additionally, \p nbits is
152 * not the size of \p N but the required size for private keys.
153 *
154 * If \p modp is NULL, reduction modulo \p P is done using a generic algorithm.
155 * Otherwise, \p modp must point to a function that takes an \p mbedtls_mpi in the
156 * range of <code>0..2^(2*pbits)-1</code>, and transforms it in-place to an integer
157 * which is congruent mod \p P to the given MPI, and is close enough to \p pbits
158 * in size, so that it may be efficiently brought in the 0..P-1 range by a few
159 * additions or subtractions. Therefore, it is only an approximative modular
160 * reduction. It must return 0 on success and non-zero on failure.
161 *
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200162 */
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200163typedef struct mbedtls_ecp_group
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200164{
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200165 mbedtls_ecp_group_id id; /*!< An internal group identifier. */
166 mbedtls_mpi P; /*!< The prime modulus of the base field. */
167 mbedtls_mpi A; /*!< For Short Weierstrass: \p A in the equation. For
168 Montgomery curves: <code>(A + 2) / 4</code>. */
169 mbedtls_mpi B; /*!< For Short Weierstrass: \p B in the equation.
170 For Montgomery curves: unused. */
171 mbedtls_ecp_point G; /*!< The generator of the subgroup used. */
172 mbedtls_mpi N; /*!< The order of \p G. */
173 size_t pbits; /*!< The number of bits in \p P.*/
174 size_t nbits; /*!< For Short Weierstrass: The number of bits in \p P.
175 For Montgomery curves: the number of bits in the
176 private keys. */
177 unsigned int h; /*!< \internal 1 if the constants are static. */
178 int (*modp)(mbedtls_mpi *); /*!< The function for fast pseudo-reduction
179 mod \p P (see above).*/
180 int (*t_pre)(mbedtls_ecp_point *, void *); /*!< Unused. */
181 int (*t_post)(mbedtls_ecp_point *, void *); /*!< Unused. */
182 void *t_data; /*!< Unused. */
183 mbedtls_ecp_point *T; /*!< Pre-computed points for ecp_mul_comb(). */
184 size_t T_size; /*!< The number of pre-computed points. */
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200185}
186mbedtls_ecp_group;
187
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200188#if defined(MBEDTLS_ECP_RESTARTABLE)
189
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200190/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200191 * \brief Internal restart context for multiplication
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200192 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200193 * \note Opaque struct
194 */
195typedef struct mbedtls_ecp_restart_mul mbedtls_ecp_restart_mul_ctx;
196
197/**
198 * \brief Internal restart context for ecp_muladd()
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200199 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200200 * \note Opaque struct
201 */
202typedef struct mbedtls_ecp_restart_muladd mbedtls_ecp_restart_muladd_ctx;
203
204/**
205 * \brief General context for resuming ECC operations
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200206 */
207typedef struct
208{
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200209 unsigned ops_done; /*!< current ops count */
210 unsigned depth; /*!< call depth (0 = top-level) */
211 mbedtls_ecp_restart_mul_ctx *rsm; /*!< ecp_mul_comb() sub-context */
212 mbedtls_ecp_restart_muladd_ctx *ma; /*!< ecp_muladd() sub-context */
213} mbedtls_ecp_restart_ctx;
214
215/*
216 * Operation counts for restartable functions
217 */
218#define MBEDTLS_ECP_OPS_CHK 3 /*!< basic ops count for ecp_check_pubkey() */
219#define MBEDTLS_ECP_OPS_DBL 8 /*!< basic ops count for ecp_double_jac() */
220#define MBEDTLS_ECP_OPS_ADD 11 /*!< basic ops count for see ecp_add_mixed() */
221#define MBEDTLS_ECP_OPS_INV 120 /*!< empirical equivalent for mpi_mod_inv() */
222
223/**
224 * \brief Internal; for restartable functions in other modules.
225 * Check and update basic ops budget.
226 *
227 * \param grp Group structure
228 * \param rs_ctx Restart context
229 * \param ops Number of basic ops to do
230 *
231 * \return \c 0 if doing \p ops basic ops is still allowed,
232 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS otherwise.
233 */
234int mbedtls_ecp_check_budget( const mbedtls_ecp_group *grp,
235 mbedtls_ecp_restart_ctx *rs_ctx,
236 unsigned ops );
237
238/* Utility macro for checking and updating ops budget */
239#define MBEDTLS_ECP_BUDGET( ops ) \
240 MBEDTLS_MPI_CHK( mbedtls_ecp_check_budget( grp, rs_ctx, \
241 (unsigned) (ops) ) );
242
243#else /* MBEDTLS_ECP_RESTARTABLE */
244
245#define MBEDTLS_ECP_BUDGET( ops ) /* no-op; for compatibility */
246
247/* We want to declare restartable versions of existing functions anyway */
248typedef void mbedtls_ecp_restart_ctx;
249
250#endif /* MBEDTLS_ECP_RESTARTABLE */
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200251
252/**
253 * \name SECTION: Module settings
254 *
255 * The configuration options you can set for this module are in this section.
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200256 * Either change them in config.h, or define them using the compiler command line.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200257 * \{
258 */
259
260#if !defined(MBEDTLS_ECP_MAX_BITS)
261/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200262 * The maximum size of the groups, that is, of \c N and \c P.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200263 */
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200264#define MBEDTLS_ECP_MAX_BITS 521 /**< The maximum size of groups, in bits. */
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200265#endif
266
267#define MBEDTLS_ECP_MAX_BYTES ( ( MBEDTLS_ECP_MAX_BITS + 7 ) / 8 )
268#define MBEDTLS_ECP_MAX_PT_LEN ( 2 * MBEDTLS_ECP_MAX_BYTES + 1 )
269
270#if !defined(MBEDTLS_ECP_WINDOW_SIZE)
271/*
272 * Maximum "window" size used for point multiplication.
273 * Default: 6.
274 * Minimum value: 2. Maximum value: 7.
275 *
276 * Result is an array of at most ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) )
277 * points used for point multiplication. This value is directly tied to EC
278 * peak memory usage, so decreasing it by one should roughly cut memory usage
279 * by two (if large curves are in use).
280 *
281 * Reduction in size may reduce speed, but larger curves are impacted first.
282 * Sample performances (in ECDHE handshakes/s, with FIXED_POINT_OPTIM = 1):
283 * w-size: 6 5 4 3 2
284 * 521 145 141 135 120 97
285 * 384 214 209 198 177 146
286 * 256 320 320 303 262 226
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200287 * 224 475 475 453 398 342
288 * 192 640 640 633 587 476
289 */
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200290#define MBEDTLS_ECP_WINDOW_SIZE 6 /**< The maximum window size used. */
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200291#endif /* MBEDTLS_ECP_WINDOW_SIZE */
292
293#if !defined(MBEDTLS_ECP_FIXED_POINT_OPTIM)
294/*
295 * Trade memory for speed on fixed-point multiplication.
296 *
297 * This speeds up repeated multiplication of the generator (that is, the
298 * multiplication in ECDSA signatures, and half of the multiplications in
299 * ECDSA verification and ECDHE) by a factor roughly 3 to 4.
300 *
301 * The cost is increasing EC peak memory usage by a factor roughly 2.
302 *
303 * Change this value to 0 to reduce peak memory usage.
304 */
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200305#define MBEDTLS_ECP_FIXED_POINT_OPTIM 1 /**< Enable fixed-point speed-up. */
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200306#endif /* MBEDTLS_ECP_FIXED_POINT_OPTIM */
307
308/* \} name SECTION: Module settings */
309
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200310#else /* MBEDTLS_ECP_ALT */
311#include "ecp_alt.h"
312#endif /* MBEDTLS_ECP_ALT */
313
314/**
315 * \brief The ECP key-pair structure.
316 *
317 * A generic key-pair that may be used for ECDSA and fixed ECDH, for example.
318 *
319 * \note Members are deliberately in the same order as in the
320 * ::mbedtls_ecdsa_context structure.
321 */
322typedef struct mbedtls_ecp_keypair
323{
324 mbedtls_ecp_group grp; /*!< Elliptic curve and base point */
325 mbedtls_mpi d; /*!< our secret value */
326 mbedtls_ecp_point Q; /*!< our public value */
327}
328mbedtls_ecp_keypair;
329
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200330/*
331 * Point formats, from RFC 4492's enum ECPointFormat
332 */
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200333#define MBEDTLS_ECP_PF_UNCOMPRESSED 0 /**< Uncompressed point format. */
334#define MBEDTLS_ECP_PF_COMPRESSED 1 /**< Compressed point format. */
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200335
336/*
337 * Some other constants from RFC 4492
338 */
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200339#define MBEDTLS_ECP_TLS_NAMED_CURVE 3 /**< The named_curve of ECCurveType. */
340
341#if defined(MBEDTLS_ECP_RESTARTABLE)
342/**
343 * \brief Set the maximum number of basic operations done in a row.
344 *
345 * If more operations are needed to complete a computation,
346 * #MBEDTLS_ERR_ECP_IN_PROGRESS will be returned by the
347 * function performing the computation. It is then the
348 * caller's responsibility to either call again with the same
349 * parameters until it returns 0 or an error code; or to free
350 * the restart context if the operation is to be aborted.
351 *
352 * It is strictly required that all input parameters and the
353 * restart context be the same on successive calls for the
354 * same operation, but output parameters need not be the
355 * same; they must not be used until the function finally
356 * returns 0.
357 *
358 * This only applies to functions whose documentation
359 * mentions they may return #MBEDTLS_ERR_ECP_IN_PROGRESS (or
360 * #MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS for functions in the
361 * SSL module). For functions that accept a "restart context"
362 * argument, passing NULL disables restart and makes the
363 * function equivalent to the function with the same name
364 * with \c _restartable removed. For functions in the ECDH
365 * module, restart is disabled unless the function accepts
366 * an "ECDH context" argument and
367 * mbedtls_ecdh_enable_restart() was previously called on
368 * that context. For function in the SSL module, restart is
369 * only enabled for specific sides and key exchanges
370 * (currently only for clients and ECDHE-ECDSA).
371 *
372 * \param max_ops Maximum number of basic operations done in a row.
373 * Default: 0 (unlimited).
374 * Lower (non-zero) values mean ECC functions will block for
375 * a lesser maximum amount of time.
376 *
377 * \note A "basic operation" is defined as a rough equivalent of a
378 * multiplication in GF(p) for the NIST P-256 curve.
379 * As an indication, with default settings, a scalar
380 * multiplication (full run of \c mbedtls_ecp_mul()) is:
381 * - about 3300 basic operations for P-256
382 * - about 9400 basic operations for P-384
383 *
384 * \note Very low values are not always respected: sometimes
385 * functions need to block for a minimum number of
386 * operations, and will do so even if max_ops is set to a
387 * lower value. That minimum depends on the curve size, and
388 * can be made lower by decreasing the value of
389 * \c MBEDTLS_ECP_WINDOW_SIZE. As an indication, here is the
390 * lowest effective value for various curves and values of
391 * that parameter (w for short):
392 * w=6 w=5 w=4 w=3 w=2
393 * P-256 208 208 160 136 124
394 * P-384 682 416 320 272 248
395 * P-521 1364 832 640 544 496
396 *
397 * \note This setting is currently ignored by Curve25519.
398 */
399void mbedtls_ecp_set_max_ops( unsigned max_ops );
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200400
401/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200402 * \brief Check if restart is enabled (max_ops != 0)
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200403 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200404 * \return \c 0 if \c max_ops == 0 (restart disabled)
405 * \return \c 1 otherwise (restart enabled)
406 */
407int mbedtls_ecp_restart_is_enabled( void );
408#endif /* MBEDTLS_ECP_RESTARTABLE */
409
410/**
411 * \brief This function retrieves the information defined in
412 * mbedtls_ecp_curve_info() for all supported curves in order
413 * of preference.
414 *
415 * \return A statically allocated array. The last entry is 0.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200416 */
417const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list( void );
418
419/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200420 * \brief This function retrieves the list of internal group
421 * identifiers of all supported curves in the order of
422 * preference.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200423 *
424 * \return A statically allocated array,
425 * terminated with MBEDTLS_ECP_DP_NONE.
426 */
427const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list( void );
428
429/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200430 * \brief This function retrieves curve information from an internal
431 * group identifier.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200432 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200433 * \param grp_id An \c MBEDTLS_ECP_DP_XXX value.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200434 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200435 * \return The associated curve information on success.
436 * \return NULL on failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200437 */
438const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_grp_id( mbedtls_ecp_group_id grp_id );
439
440/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200441 * \brief This function retrieves curve information from a TLS
442 * NamedCurve value.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200443 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200444 * \param tls_id An \c MBEDTLS_ECP_DP_XXX value.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200445 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200446 * \return The associated curve information on success.
447 * \return NULL on failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200448 */
449const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id( uint16_t tls_id );
450
451/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200452 * \brief This function retrieves curve information from a
453 * human-readable name.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200454 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200455 * \param name The human-readable name.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200456 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200457 * \return The associated curve information on success.
458 * \return NULL on failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200459 */
460const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name( const char *name );
461
462/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200463 * \brief This function initializes a point as zero.
464 *
465 * \param pt The point to initialize.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200466 */
467void mbedtls_ecp_point_init( mbedtls_ecp_point *pt );
468
469/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200470 * \brief This function initializes an ECP group context
471 * without loading any domain parameters.
472 *
473 * \note After this function is called, domain parameters
474 * for various ECP groups can be loaded through the
475 * mbedtls_ecp_load() or mbedtls_ecp_tls_read_group()
476 * functions.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200477 */
478void mbedtls_ecp_group_init( mbedtls_ecp_group *grp );
479
480/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200481 * \brief This function initializes a key pair as an invalid one.
482 *
483 * \param key The key pair to initialize.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200484 */
485void mbedtls_ecp_keypair_init( mbedtls_ecp_keypair *key );
486
487/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200488 * \brief This function frees the components of a point.
489 *
490 * \param pt The point to free.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200491 */
492void mbedtls_ecp_point_free( mbedtls_ecp_point *pt );
493
494/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200495 * \brief This function frees the components of an ECP group.
496 * \param grp The group to free.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200497 */
498void mbedtls_ecp_group_free( mbedtls_ecp_group *grp );
499
500/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200501 * \brief This function frees the components of a key pair.
502 * \param key The key pair to free.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200503 */
504void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair *key );
505
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200506#if defined(MBEDTLS_ECP_RESTARTABLE)
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200507/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200508 * \brief Initialize a restart context
509 */
510void mbedtls_ecp_restart_init( mbedtls_ecp_restart_ctx *ctx );
511
512/**
513 * \brief Free the components of a restart context
514 */
515void mbedtls_ecp_restart_free( mbedtls_ecp_restart_ctx *ctx );
516#endif /* MBEDTLS_ECP_RESTARTABLE */
517
518/**
519 * \brief This function copies the contents of point \p Q into
520 * point \p P.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200521 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200522 * \param P The destination point.
523 * \param Q The source point.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200524 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200525 * \return \c 0 on success.
526 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200527 */
528int mbedtls_ecp_copy( mbedtls_ecp_point *P, const mbedtls_ecp_point *Q );
529
530/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200531 * \brief This function copies the contents of group \p src into
532 * group \p dst.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200533 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200534 * \param dst The destination group.
535 * \param src The source group.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200536 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200537 * \return \c 0 on success.
538 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200539 */
540int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst, const mbedtls_ecp_group *src );
541
542/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200543 * \brief This function sets a point to zero.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200544 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200545 * \param pt The point to set.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200546 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200547 * \return \c 0 on success.
548 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200549 */
550int mbedtls_ecp_set_zero( mbedtls_ecp_point *pt );
551
552/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200553 * \brief This function checks if a point is zero.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200554 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200555 * \param pt The point to test.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200556 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200557 * \return \c 1 if the point is zero.
558 * \return \c 0 if the point is non-zero.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200559 */
560int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt );
561
562/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200563 * \brief This function compares two points.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200564 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200565 * \note This assumes that the points are normalized. Otherwise,
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200566 * they may compare as "not equal" even if they are.
567 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200568 * \param P The first point to compare.
569 * \param Q The second point to compare.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200570 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200571 * \return \c 0 if the points are equal.
572 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the points are not equal.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200573 */
574int mbedtls_ecp_point_cmp( const mbedtls_ecp_point *P,
575 const mbedtls_ecp_point *Q );
576
577/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200578 * \brief This function imports a non-zero point from two ASCII
579 * strings.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200580 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200581 * \param P The destination point.
582 * \param radix The numeric base of the input.
583 * \param x The first affine coordinate, as a null-terminated string.
584 * \param y The second affine coordinate, as a null-terminated string.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200585 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200586 * \return \c 0 on success.
587 * \return An \c MBEDTLS_ERR_MPI_XXX error code on failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200588 */
589int mbedtls_ecp_point_read_string( mbedtls_ecp_point *P, int radix,
590 const char *x, const char *y );
591
592/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200593 * \brief This function exports a point into unsigned binary data.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200594 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200595 * \param grp The group to which the point should belong.
596 * \param P The point to export.
597 * \param format The point format. Should be an \c MBEDTLS_ECP_PF_XXX macro.
598 * \param olen The length of the output.
599 * \param buf The output buffer.
600 * \param buflen The length of the output buffer.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200601 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200602 * \return \c 0 on success.
603 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA
604 * or #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL on failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200605 */
606int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *P,
607 int format, size_t *olen,
608 unsigned char *buf, size_t buflen );
609
610/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200611 * \brief This function imports a point from unsigned binary data.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200612 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200613 * \note This function does not check that the point actually
614 * belongs to the given group, see mbedtls_ecp_check_pubkey()
615 * for that.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200616 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200617 * \param grp The group to which the point should belong.
618 * \param P The point to import.
619 * \param buf The input buffer.
620 * \param ilen The length of the input.
621 *
622 * \return \c 0 on success.
623 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid.
624 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
625 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the point format
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200626 * is not implemented.
627 *
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200628 */
629int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P,
630 const unsigned char *buf, size_t ilen );
631
632/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200633 * \brief This function imports a point from a TLS ECPoint record.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200634 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200635 * \note On function return, \p buf is updated to point to immediately
636 * after the ECPoint record.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200637 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200638 * \param grp The ECP group used.
639 * \param pt The destination point.
640 * \param buf The address of the pointer to the start of the input buffer.
641 * \param len The length of the buffer.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200642 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200643 * \return \c 0 on success.
644 * \return An \c MBEDTLS_ERR_MPI_XXX error code on initialization failure.
645 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200646 */
647int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
648 const unsigned char **buf, size_t len );
649
650/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200651 * \brief This function exports a point as a TLS ECPoint record.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200652 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200653 * \param grp The ECP group used.
654 * \param pt The point format to export to. The point format is an
655 * \c MBEDTLS_ECP_PF_XXX constant.
656 * \param format The export format.
657 * \param olen The length of the data written.
658 * \param buf The buffer to write to.
659 * \param blen The length of the buffer.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200660 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200661 * \return \c 0 on success.
662 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA or
663 * #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL on failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200664 */
665int mbedtls_ecp_tls_write_point( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt,
666 int format, size_t *olen,
667 unsigned char *buf, size_t blen );
668
669/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200670 * \brief This function sets a group using standardized domain parameters.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200671 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200672 * \note The index should be a value of the NamedCurve enum,
673 * as defined in <em>RFC-4492: Elliptic Curve Cryptography
674 * (ECC) Cipher Suites for Transport Layer Security (TLS)</em>,
675 * usually in the form of an \c MBEDTLS_ECP_DP_XXX macro.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200676 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200677 * \param grp The destination group.
678 * \param id The identifier of the domain parameter set to load.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200679 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200680 * \return \c 0 on success,
681 * \return An \c MBEDTLS_ERR_MPI_XXX error code on initialization failure.
682 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE for unkownn groups.
683
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200684 */
685int mbedtls_ecp_group_load( mbedtls_ecp_group *grp, mbedtls_ecp_group_id id );
686
687/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200688 * \brief This function sets a group from a TLS ECParameters record.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200689 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200690 * \note \p buf is updated to point right after the ECParameters record
691 * on exit.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200692 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200693 * \param grp The destination group.
694 * \param buf The address of the pointer to the start of the input buffer.
695 * \param len The length of the buffer.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200696 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200697 * \return \c 0 on success.
698 * \return An \c MBEDTLS_ERR_MPI_XXX error code on initialization failure.
699 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200700 */
701int mbedtls_ecp_tls_read_group( mbedtls_ecp_group *grp, const unsigned char **buf, size_t len );
702
703/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200704 * \brief This function writes the TLS ECParameters record for a group.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200705 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200706 * \param grp The ECP group used.
707 * \param olen The number of Bytes written.
708 * \param buf The buffer to write to.
709 * \param blen The length of the buffer.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200710 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200711 * \return \c 0 on success.
712 * \return #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL on failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200713 */
714int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, size_t *olen,
715 unsigned char *buf, size_t blen );
716
717/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200718 * \brief This function performs multiplication of a point by
719 * an integer: \p R = \p m * \p P.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200720 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200721 * It is not thread-safe to use same group in multiple threads.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200722 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200723 * \note To prevent timing attacks, this function
724 * executes the exact same sequence of base-field
725 * operations for any valid \p m. It avoids any if-branch or
726 * array index depending on the value of \p m.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200727 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200728 * \note If \p f_rng is not NULL, it is used to randomize
729 * intermediate results to prevent potential timing attacks
730 * targeting these results. We recommend always providing
731 * a non-NULL \p f_rng. The overhead is negligible.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200732 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200733 * \param grp The ECP group.
734 * \param R The destination point.
735 * \param m The integer by which to multiply.
736 * \param P The point to multiply.
737 * \param f_rng The RNG function.
738 * \param p_rng The RNG context.
739 *
740 * \return \c 0 on success.
741 * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m is not a valid private
742 * key, or \p P is not a valid public key.
743 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200744 */
745int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
746 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
747 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
748
749/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200750 * \brief This function performs multiplication of a point by
751 * an integer: \p R = \p m * \p P in a restartable way.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200752 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200753 * \see mbedtls_ecp_mul()
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200754 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200755 * \note This function does the same as \c mbedtls_ecp_mul(), but
756 * it can return early and restart according to the limit set
757 * with \c mbedtls_ecp_set_max_ops() to reduce blocking.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200758 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200759 * \param grp The ECP group.
760 * \param R The destination point.
761 * \param m The integer by which to multiply.
762 * \param P The point to multiply.
763 * \param f_rng The RNG function.
764 * \param p_rng The RNG context.
765 * \param rs_ctx The restart context (NULL disables restart).
766 *
767 * \return \c 0 on success.
768 * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m is not a valid private
769 * key, or \p P is not a valid public key.
770 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
771 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
772 * operations was reached: see \c mbedtls_ecp_set_max_ops().
773 */
774int mbedtls_ecp_mul_restartable( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
775 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
776 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
777 mbedtls_ecp_restart_ctx *rs_ctx );
778
779/**
780 * \brief This function performs multiplication and addition of two
781 * points by integers: \p R = \p m * \p P + \p n * \p Q
782 *
783 * It is not thread-safe to use same group in multiple threads.
784 *
785 * \note In contrast to mbedtls_ecp_mul(), this function does not
786 * guarantee a constant execution flow and timing.
787 *
788 * \param grp The ECP group.
789 * \param R The destination point.
790 * \param m The integer by which to multiply \p P.
791 * \param P The point to multiply by \p m.
792 * \param n The integer by which to multiply \p Q.
793 * \param Q The point to be multiplied by \p n.
794 *
795 * \return \c 0 on success.
796 * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m or \p n are not
797 * valid private keys, or \p P or \p Q are not valid public
798 * keys.
799 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200800 */
801int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
802 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
803 const mbedtls_mpi *n, const mbedtls_ecp_point *Q );
804
805/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200806 * \brief This function performs multiplication and addition of two
807 * points by integers: \p R = \p m * \p P + \p n * \p Q in a
808 * restartable way.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200809 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200810 * \see \c mbedtls_ecp_muladd()
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200811 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200812 * \note This function works the same as \c mbedtls_ecp_muladd(),
813 * but it can return early and restart according to the limit
814 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200815 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200816 * \param grp The ECP group.
817 * \param R The destination point.
818 * \param m The integer by which to multiply \p P.
819 * \param P The point to multiply by \p m.
820 * \param n The integer by which to multiply \p Q.
821 * \param Q The point to be multiplied by \p n.
822 * \param rs_ctx The restart context (NULL disables restart).
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200823 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200824 * \return \c 0 on success.
825 * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m or \p n are not
826 * valid private keys, or \p P or \p Q are not valid public
827 * keys.
828 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
829 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
830 * operations was reached: see \c mbedtls_ecp_set_max_ops().
831 */
832int mbedtls_ecp_muladd_restartable(
833 mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
834 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
835 const mbedtls_mpi *n, const mbedtls_ecp_point *Q,
836 mbedtls_ecp_restart_ctx *rs_ctx );
837
838/**
839 * \brief This function checks that a point is a valid public key
840 * on this curve.
841 *
842 * It only checks that the point is non-zero, has
843 * valid coordinates and lies on the curve. It does not verify
844 * that it is indeed a multiple of \p G. This additional
845 * check is computationally more expensive, is not required
846 * by standards, and should not be necessary if the group
847 * used has a small cofactor. In particular, it is useless for
848 * the NIST groups which all have a cofactor of 1.
849 *
850 * \note This function uses bare components rather than an
851 * ::mbedtls_ecp_keypair structure, to ease use with other
852 * structures, such as ::mbedtls_ecdh_context or
853 * ::mbedtls_ecdsa_context.
854 *
855 * \param grp The curve the point should lie on.
856 * \param pt The point to check.
857 *
858 * \return \c 0 if the point is a valid public key.
859 * \return #MBEDTLS_ERR_ECP_INVALID_KEY on failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200860 */
861int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt );
862
863/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200864 * \brief This function checks that an \p mbedtls_mpi is a valid private
865 * key for this curve.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200866 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200867 * \note This function uses bare components rather than an
868 * ::mbedtls_ecp_keypair structure to ease use with other
869 * structures, such as ::mbedtls_ecdh_context or
870 * ::mbedtls_ecdsa_context.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200871 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200872 * \param grp The group used.
873 * \param d The integer to check.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200874 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200875 * \return \c 0 if the point is a valid private key.
876 * \return #MBEDTLS_ERR_ECP_INVALID_KEY on failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200877 */
878int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp, const mbedtls_mpi *d );
879
880/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200881 * \brief This function generates a private key.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200882 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200883 * \param grp The ECP group.
884 * \param d The destination MPI (secret part).
885 * \param f_rng The RNG function.
886 * \param p_rng The RNG parameter.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200887 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200888 * \return \c 0 on success.
889 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
890 * on failure.
891 */
892int mbedtls_ecp_gen_privkey( const mbedtls_ecp_group *grp,
893 mbedtls_mpi *d,
894 int (*f_rng)(void *, unsigned char *, size_t),
895 void *p_rng );
896
897/**
898 * \brief This function generates a keypair with a configurable base
899 * point.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200900 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200901 * \note This function uses bare components rather than an
902 * ::mbedtls_ecp_keypair structure to ease use with other
903 * structures, such as ::mbedtls_ecdh_context or
904 * ::mbedtls_ecdsa_context.
905 *
906 * \param grp The ECP group.
907 * \param G The chosen base point.
908 * \param d The destination MPI (secret part).
909 * \param Q The destination point (public part).
910 * \param f_rng The RNG function.
911 * \param p_rng The RNG context.
912 *
913 * \return \c 0 on success.
914 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
915 * on failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200916 */
917int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp,
918 const mbedtls_ecp_point *G,
919 mbedtls_mpi *d, mbedtls_ecp_point *Q,
920 int (*f_rng)(void *, unsigned char *, size_t),
921 void *p_rng );
922
923/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200924 * \brief This function generates an ECP keypair.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200925 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200926 * \note This function uses bare components rather than an
927 * ::mbedtls_ecp_keypair structure to ease use with other
928 * structures, such as ::mbedtls_ecdh_context or
929 * ::mbedtls_ecdsa_context.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200930 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200931 * \param grp The ECP group.
932 * \param d The destination MPI (secret part).
933 * \param Q The destination point (public part).
934 * \param f_rng The RNG function.
935 * \param p_rng The RNG context.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200936 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200937 * \return \c 0 on success.
938 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
939 * on failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200940 */
941int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
942 int (*f_rng)(void *, unsigned char *, size_t),
943 void *p_rng );
944
945/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200946 * \brief This function generates an ECP key.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200947 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200948 * \param grp_id The ECP group identifier.
949 * \param key The destination key.
950 * \param f_rng The RNG function.
951 * \param p_rng The RNG context.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200952 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200953 * \return \c 0 on success.
954 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
955 * on failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200956 */
957int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
958 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
959
960/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200961 * \brief This function checks that the keypair objects
962 * \p pub and \p prv have the same group and the
963 * same public point, and that the private key in
964 * \p prv is consistent with the public key.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200965 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200966 * \param pub The keypair structure holding the public key.
967 * If it contains a private key, that part is ignored.
968 * \param prv The keypair structure holding the full keypair.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200969 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200970 * \return \c 0 on success, meaning that the keys are valid and match.
971 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the keys are invalid or do not match.
972 * \return An \c MBEDTLS_ERR_ECP_XXX or an \c MBEDTLS_ERR_MPI_XXX
973 * error code on calculation failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200974 */
975int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv );
976
977#if defined(MBEDTLS_SELF_TEST)
978
979/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200980 * \brief The ECP checkup routine.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200981 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200982 * \return \c 0 on success.
983 * \return \c 1 on failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200984 */
985int mbedtls_ecp_self_test( int verbose );
986
987#endif /* MBEDTLS_SELF_TEST */
988
989#ifdef __cplusplus
990}
991#endif
992
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200993#endif /* ecp.h */