1 /**
2 * \file ecp.h
3 *
4 * \brief This file provides an API for Elliptic Curves over GF(P) (ECP).
5 *
6 * 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 The Mbed TLS Contributors
19 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
20 */
21
22 #ifndef MBEDTLS_ECP_H
23 #define MBEDTLS_ECP_H
24 #include "mbedtls/private_access.h"
25
26 #include "mbedtls/build_info.h"
27
28 #include "mbedtls/bignum.h"
29
30 /*
31 * ECP error codes
32 */
33 /** Bad input parameters to function. */
34 #define MBEDTLS_ERR_ECP_BAD_INPUT_DATA -0x4F80
35 /** The buffer is too small to write to. */
36 #define MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL -0x4F00
37 /** The requested feature is not available, for example, the requested curve is not supported. */
38 #define MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE -0x4E80
39 /** The signature is not valid. */
40 #define MBEDTLS_ERR_ECP_VERIFY_FAILED -0x4E00
41 /** Memory allocation failed. */
42 #define MBEDTLS_ERR_ECP_ALLOC_FAILED -0x4D80
43 /** Generation of random value, such as ephemeral key, failed. */
44 #define MBEDTLS_ERR_ECP_RANDOM_FAILED -0x4D00
45 /** Invalid private or public key. */
46 #define MBEDTLS_ERR_ECP_INVALID_KEY -0x4C80
47 /** The buffer contains a valid signature followed by more data. */
48 #define MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH -0x4C00
49 /** Operation in progress, call again with the same parameters to continue. */
50 #define MBEDTLS_ERR_ECP_IN_PROGRESS -0x4B00
51
52 /* Flags indicating whether to include code that is specific to certain
53 * types of curves. These flags are for internal library use only. */
54 #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || \
55 defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || \
56 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || \
57 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || \
58 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || \
59 defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || \
60 defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || \
61 defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || \
62 defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || \
63 defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || \
64 defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
65 #define MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED
66 #endif
67 #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || \
68 defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
69 #define MBEDTLS_ECP_MONTGOMERY_ENABLED
70 #endif
71
72 #ifdef __cplusplus
73 extern "C" {
74 #endif
75
76 /**
77 * Domain-parameter identifiers: curve, subgroup, and generator.
78 *
79 * \note Only curves over prime fields are supported.
80 *
81 * \warning This library does not support validation of arbitrary domain
82 * parameters. Therefore, only standardized domain parameters from trusted
83 * sources should be used. See mbedtls_ecp_group_load().
84 */
85 /* Note: when adding a new curve:
86 * - Add it at the end of this enum, otherwise you'll break the ABI by
87 * changing the numerical value for existing curves.
88 * - Increment MBEDTLS_ECP_DP_MAX below if needed.
89 * - Update the calculation of MBEDTLS_ECP_MAX_BITS below.
90 * - Add the corresponding MBEDTLS_ECP_DP_xxx_ENABLED macro definition to
91 * mbedtls_config.h.
92 * - List the curve as a dependency of MBEDTLS_ECP_C and
93 * MBEDTLS_ECDSA_C if supported in check_config.h.
94 * - Add the curve to the appropriate curve type macro
95 * MBEDTLS_ECP_yyy_ENABLED above.
96 * - Add the necessary definitions to ecp_curves.c.
97 * - Add the curve to the ecp_supported_curves array in ecp.c.
98 * - Add the curve to applicable profiles in x509_crt.c.
99 * - Add the curve to applicable presets in ssl_tls.c.
100 */
101 typedef enum {
102 MBEDTLS_ECP_DP_NONE = 0, /*!< Curve not defined. */
103 MBEDTLS_ECP_DP_SECP192R1, /*!< Domain parameters for the 192-bit curve defined by FIPS 186-4 and SEC1. */
104 MBEDTLS_ECP_DP_SECP224R1, /*!< Domain parameters for the 224-bit curve defined by FIPS 186-4 and SEC1. */
105 MBEDTLS_ECP_DP_SECP256R1, /*!< Domain parameters for the 256-bit curve defined by FIPS 186-4 and SEC1. */
106 MBEDTLS_ECP_DP_SECP384R1, /*!< Domain parameters for the 384-bit curve defined by FIPS 186-4 and SEC1. */
107 MBEDTLS_ECP_DP_SECP521R1, /*!< Domain parameters for the 521-bit curve defined by FIPS 186-4 and SEC1. */
108 MBEDTLS_ECP_DP_BP256R1, /*!< Domain parameters for 256-bit Brainpool curve. */
109 MBEDTLS_ECP_DP_BP384R1, /*!< Domain parameters for 384-bit Brainpool curve. */
110 MBEDTLS_ECP_DP_BP512R1, /*!< Domain parameters for 512-bit Brainpool curve. */
111 MBEDTLS_ECP_DP_CURVE25519, /*!< Domain parameters for Curve25519. */
112 MBEDTLS_ECP_DP_SECP192K1, /*!< Domain parameters for 192-bit "Koblitz" curve. */
113 MBEDTLS_ECP_DP_SECP224K1, /*!< Domain parameters for 224-bit "Koblitz" curve. */
114 MBEDTLS_ECP_DP_SECP256K1, /*!< Domain parameters for 256-bit "Koblitz" curve. */
115 MBEDTLS_ECP_DP_CURVE448, /*!< Domain parameters for Curve448. */
116 } mbedtls_ecp_group_id;
117
118 /**
119 * The number of supported curves, plus one for #MBEDTLS_ECP_DP_NONE.
120 */
121 #define MBEDTLS_ECP_DP_MAX 14
122
123 /*
124 * Curve types
125 */
126 typedef enum {
127 MBEDTLS_ECP_TYPE_NONE = 0,
128 MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS, /* y^2 = x^3 + a x + b */
129 MBEDTLS_ECP_TYPE_MONTGOMERY, /* y^2 = x^3 + a x^2 + x */
130 } mbedtls_ecp_curve_type;
131
132 /**
133 * Curve information, for use by other modules.
134 *
135 * The fields of this structure are part of the public API and can be
136 * accessed directly by applications. Future versions of the library may
137 * add extra fields or reorder existing fields.
138 */
139 typedef struct mbedtls_ecp_curve_info {
140 mbedtls_ecp_group_id grp_id; /*!< An internal identifier. */
141 uint16_t tls_id; /*!< The TLS NamedCurve identifier. */
142 uint16_t bit_size; /*!< The curve size in bits. */
143 const char *name; /*!< A human-friendly name. */
144 } mbedtls_ecp_curve_info;
145
146 /**
147 * \brief The ECP point structure, in Jacobian coordinates.
148 *
149 * \note All functions expect and return points satisfying
150 * the following condition: <code>Z == 0</code> or
151 * <code>Z == 1</code>. Other values of \p Z are
152 * used only by internal functions.
153 * The point is zero, or "at infinity", if <code>Z == 0</code>.
154 * Otherwise, \p X and \p Y are its standard (affine)
155 * coordinates.
156 */
157 typedef struct mbedtls_ecp_point {
158 mbedtls_mpi MBEDTLS_PRIVATE(X); /*!< The X coordinate of the ECP point. */
159 mbedtls_mpi MBEDTLS_PRIVATE(Y); /*!< The Y coordinate of the ECP point. */
160 mbedtls_mpi MBEDTLS_PRIVATE(Z); /*!< The Z coordinate of the ECP point. */
161 }
162 mbedtls_ecp_point;
163
164 #if !defined(MBEDTLS_ECP_ALT)
165 /*
166 * default Mbed TLS elliptic curve arithmetic implementation
167 *
168 * (in case MBEDTLS_ECP_ALT is defined then the developer has to provide an
169 * alternative implementation for the whole module and it will replace this
170 * one.)
171 */
172
173 /**
174 * \brief The ECP group structure.
175 *
176 * We consider two types of curve equations:
177 * <ul><li>Short Weierstrass: <code>y^2 = x^3 + A x + B mod P</code>
178 * (SEC1 + RFC-4492)</li>
179 * <li>Montgomery: <code>y^2 = x^3 + A x^2 + x mod P</code> (Curve25519,
180 * Curve448)</li></ul>
181 * In both cases, the generator (\p G) for a prime-order subgroup is fixed.
182 *
183 * For Short Weierstrass, this subgroup is the whole curve, and its
184 * cardinality is denoted by \p N. Our code requires that \p N is an
185 * odd prime as mbedtls_ecp_mul() requires an odd number, and
186 * mbedtls_ecdsa_sign() requires that it is prime for blinding purposes.
187 *
188 * The default implementation only initializes \p A without setting it to the
189 * authentic value for curves with <code>A = -3</code>(SECP256R1, etc), in which
190 * case you need to load \p A by yourself when using domain parameters directly,
191 * for example:
192 * \code
193 * mbedtls_mpi_init(&A);
194 * mbedtls_ecp_group_init(&grp);
195 * CHECK_RETURN(mbedtls_ecp_group_load(&grp, grp_id));
196 * if (mbedtls_ecp_group_a_is_minus_3(&grp)) {
197 * CHECK_RETURN(mbedtls_mpi_sub_int(&A, &grp.P, 3));
198 * } else {
199 * CHECK_RETURN(mbedtls_mpi_copy(&A, &grp.A));
200 * }
201 *
202 * do_something_with_a(&A);
203 *
204 * cleanup:
205 * mbedtls_mpi_free(&A);
206 * mbedtls_ecp_group_free(&grp);
207 * \endcode
208 *
209 * For Montgomery curves, we do not store \p A, but <code>(A + 2) / 4</code>,
210 * which is the quantity used in the formulas. Additionally, \p nbits is
211 * not the size of \p N but the required size for private keys.
212 *
213 * If \p modp is NULL, reduction modulo \p P is done using a generic algorithm.
214 * Otherwise, \p modp must point to a function that takes an \p mbedtls_mpi in the
215 * range of <code>0..2^(2*pbits)-1</code>, and transforms it in-place to an integer
216 * which is congruent mod \p P to the given MPI, and is close enough to \p pbits
217 * in size, so that it may be efficiently brought in the 0..P-1 range by a few
218 * additions or subtractions. Therefore, it is only an approximative modular
219 * reduction. It must return 0 on success and non-zero on failure.
220 *
221 * \note Alternative implementations of the ECP module must obey the
222 * following constraints.
223 * * Group IDs must be distinct: if two group structures have
224 * the same ID, then they must be identical.
225 * * The fields \c id, \c P, \c A, \c B, \c G, \c N,
226 * \c pbits and \c nbits must have the same type and semantics
227 * as in the built-in implementation.
228 * They must be available for reading, but direct modification
229 * of these fields does not need to be supported.
230 * They do not need to be at the same offset in the structure.
231 */
232 typedef struct mbedtls_ecp_group {
233 mbedtls_ecp_group_id id; /*!< An internal group identifier. */
234 mbedtls_mpi P; /*!< The prime modulus of the base field. */
235 mbedtls_mpi A; /*!< For Short Weierstrass: \p A in the equation. Note that
236 \p A is not set to the authentic value in some cases.
237 Refer to detailed description of ::mbedtls_ecp_group if
238 using domain parameters in the structure.
239 For Montgomery curves: <code>(A + 2) / 4</code>. */
240 mbedtls_mpi B; /*!< For Short Weierstrass: \p B in the equation.
241 For Montgomery curves: unused. */
242 mbedtls_ecp_point G; /*!< The generator of the subgroup used. */
243 mbedtls_mpi N; /*!< The order of \p G. */
244 size_t pbits; /*!< The number of bits in \p P.*/
245 size_t nbits; /*!< For Short Weierstrass: The number of bits in \p P.
246 For Montgomery curves: the number of bits in the
247 private keys. */
248 /* End of public fields */
249
250 unsigned int MBEDTLS_PRIVATE(h); /*!< \internal 1 if the constants are static. */
251 int(*MBEDTLS_PRIVATE(modp))(mbedtls_mpi *); /*!< The function for fast pseudo-reduction
252 mod \p P (see above).*/
253 int(*MBEDTLS_PRIVATE(t_pre))(mbedtls_ecp_point *, void *); /*!< Unused. */
254 int(*MBEDTLS_PRIVATE(t_post))(mbedtls_ecp_point *, void *); /*!< Unused. */
255 void *MBEDTLS_PRIVATE(t_data); /*!< Unused. */
256 mbedtls_ecp_point *MBEDTLS_PRIVATE(T); /*!< Pre-computed points for ecp_mul_comb(). */
257 size_t MBEDTLS_PRIVATE(T_size); /*!< The number of dynamic allocated pre-computed points. */
258 }
259 mbedtls_ecp_group;
260
261 /**
262 * \name SECTION: Module settings
263 *
264 * The configuration options you can set for this module are in this section.
265 * Either change them in mbedtls_config.h, or define them using the compiler command line.
266 * \{
267 */
268
269 #if !defined(MBEDTLS_ECP_WINDOW_SIZE)
270 /*
271 * Maximum "window" size used for point multiplication.
272 * Default: a point where higher memory usage yields diminishing performance
273 * returns.
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
287 * 224 475 475 453 398 342
288 * 192 640 640 633 587 476
289 */
290 #define MBEDTLS_ECP_WINDOW_SIZE 4 /**< The maximum window size used. */
291 #endif /* MBEDTLS_ECP_WINDOW_SIZE */
292
293 #if !defined(MBEDTLS_ECP_FIXED_POINT_OPTIM)
294 /*
295 * Trade code size 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 * For each n-bit Short Weierstrass curve that is enabled, this adds 4n bytes
302 * of code size if n < 384 and 8n otherwise.
303 *
304 * Change this value to 0 to reduce code size.
305 */
306 #define MBEDTLS_ECP_FIXED_POINT_OPTIM 1 /**< Enable fixed-point speed-up. */
307 #endif /* MBEDTLS_ECP_FIXED_POINT_OPTIM */
308
309 /** \} name SECTION: Module settings */
310
311 #else /* MBEDTLS_ECP_ALT */
312 #include "ecp_alt.h"
313 #endif /* MBEDTLS_ECP_ALT */
314
315 /**
316 * The maximum size of the groups, that is, of \c N and \c P.
317 */
318 #if !defined(MBEDTLS_ECP_LIGHT)
319 /* Dummy definition to help code that has optional ECP support and
320 * defines an MBEDTLS_ECP_MAX_BYTES-sized array unconditionally. */
321 #define MBEDTLS_ECP_MAX_BITS 1
322 /* Note: the curves must be listed in DECREASING size! */
323 #elif defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
324 #define MBEDTLS_ECP_MAX_BITS 521
325 #elif defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
326 #define MBEDTLS_ECP_MAX_BITS 512
327 #elif defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
328 #define MBEDTLS_ECP_MAX_BITS 448
329 #elif defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
330 #define MBEDTLS_ECP_MAX_BITS 384
331 #elif defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
332 #define MBEDTLS_ECP_MAX_BITS 384
333 #elif defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
334 #define MBEDTLS_ECP_MAX_BITS 256
335 #elif defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
336 #define MBEDTLS_ECP_MAX_BITS 256
337 #elif defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
338 #define MBEDTLS_ECP_MAX_BITS 256
339 #elif defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
340 #define MBEDTLS_ECP_MAX_BITS 255
341 #elif defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
342 #define MBEDTLS_ECP_MAX_BITS 225 // n is slightly above 2^224
343 #elif defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
344 #define MBEDTLS_ECP_MAX_BITS 224
345 #elif defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
346 #define MBEDTLS_ECP_MAX_BITS 192
347 #elif defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
348 #define MBEDTLS_ECP_MAX_BITS 192
349 #else /* !MBEDTLS_ECP_LIGHT */
350 #error "Missing definition of MBEDTLS_ECP_MAX_BITS"
351 #endif /* !MBEDTLS_ECP_LIGHT */
352
353 #define MBEDTLS_ECP_MAX_BYTES ((MBEDTLS_ECP_MAX_BITS + 7) / 8)
354 #define MBEDTLS_ECP_MAX_PT_LEN (2 * MBEDTLS_ECP_MAX_BYTES + 1)
355
356 #if defined(MBEDTLS_ECP_RESTARTABLE)
357
358 /**
359 * \brief Internal restart context for multiplication
360 *
361 * \note Opaque struct
362 */
363 typedef struct mbedtls_ecp_restart_mul mbedtls_ecp_restart_mul_ctx;
364
365 /**
366 * \brief Internal restart context for ecp_muladd()
367 *
368 * \note Opaque struct
369 */
370 typedef struct mbedtls_ecp_restart_muladd mbedtls_ecp_restart_muladd_ctx;
371
372 /**
373 * \brief General context for resuming ECC operations
374 */
375 typedef struct {
376 unsigned MBEDTLS_PRIVATE(ops_done); /*!< current ops count */
377 unsigned MBEDTLS_PRIVATE(depth); /*!< call depth (0 = top-level) */
378 mbedtls_ecp_restart_mul_ctx *MBEDTLS_PRIVATE(rsm); /*!< ecp_mul_comb() sub-context */
379 mbedtls_ecp_restart_muladd_ctx *MBEDTLS_PRIVATE(ma); /*!< ecp_muladd() sub-context */
380 } mbedtls_ecp_restart_ctx;
381
382 /*
383 * Operation counts for restartable functions
384 */
385 #define MBEDTLS_ECP_OPS_CHK 3 /*!< basic ops count for ecp_check_pubkey() */
386 #define MBEDTLS_ECP_OPS_DBL 8 /*!< basic ops count for ecp_double_jac() */
387 #define MBEDTLS_ECP_OPS_ADD 11 /*!< basic ops count for see ecp_add_mixed() */
388 #define MBEDTLS_ECP_OPS_INV 120 /*!< empirical equivalent for mpi_mod_inv() */
389
390 /**
391 * \brief Internal; for restartable functions in other modules.
392 * Check and update basic ops budget.
393 *
394 * \param grp Group structure
395 * \param rs_ctx Restart context
396 * \param ops Number of basic ops to do
397 *
398 * \return \c 0 if doing \p ops basic ops is still allowed,
399 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS otherwise.
400 */
401 int mbedtls_ecp_check_budget(const mbedtls_ecp_group *grp,
402 mbedtls_ecp_restart_ctx *rs_ctx,
403 unsigned ops);
404
405 /* Utility macro for checking and updating ops budget */
406 #define MBEDTLS_ECP_BUDGET(ops) \
407 MBEDTLS_MPI_CHK(mbedtls_ecp_check_budget(grp, rs_ctx, \
408 (unsigned) (ops)));
409
410 #else /* MBEDTLS_ECP_RESTARTABLE */
411
412 #define MBEDTLS_ECP_BUDGET(ops) /* no-op; for compatibility */
413
414 /* We want to declare restartable versions of existing functions anyway */
415 typedef void mbedtls_ecp_restart_ctx;
416
417 #endif /* MBEDTLS_ECP_RESTARTABLE */
418
419 /**
420 * \brief The ECP key-pair structure.
421 *
422 * A generic key-pair that may be used for ECDSA and fixed ECDH, for example.
423 *
424 * \note Members are deliberately in the same order as in the
425 * ::mbedtls_ecdsa_context structure.
426 */
427 typedef struct mbedtls_ecp_keypair {
428 mbedtls_ecp_group MBEDTLS_PRIVATE(grp); /*!< Elliptic curve and base point */
429 mbedtls_mpi MBEDTLS_PRIVATE(d); /*!< our secret value */
430 mbedtls_ecp_point MBEDTLS_PRIVATE(Q); /*!< our public value */
431 }
432 mbedtls_ecp_keypair;
433
434 /**
435 * The uncompressed point format for Short Weierstrass curves
436 * (MBEDTLS_ECP_DP_SECP_XXX and MBEDTLS_ECP_DP_BP_XXX).
437 */
438 #define MBEDTLS_ECP_PF_UNCOMPRESSED 0
439 /**
440 * The compressed point format for Short Weierstrass curves
441 * (MBEDTLS_ECP_DP_SECP_XXX and MBEDTLS_ECP_DP_BP_XXX).
442 *
443 * \warning While this format is supported for all concerned curves for
444 * writing, when it comes to parsing, it is not supported for all
445 * curves. Specifically, parsing compressed points on
446 * MBEDTLS_ECP_DP_SECP224R1 and MBEDTLS_ECP_DP_SECP224K1 is not
447 * supported.
448 */
449 #define MBEDTLS_ECP_PF_COMPRESSED 1
450
451 /*
452 * Some other constants from RFC 4492
453 */
454 #define MBEDTLS_ECP_TLS_NAMED_CURVE 3 /**< The named_curve of ECCurveType. */
455
456 #if defined(MBEDTLS_ECP_RESTARTABLE)
457 /**
458 * \brief Set the maximum number of basic operations done in a row.
459 *
460 * If more operations are needed to complete a computation,
461 * #MBEDTLS_ERR_ECP_IN_PROGRESS will be returned by the
462 * function performing the computation. It is then the
463 * caller's responsibility to either call again with the same
464 * parameters until it returns 0 or an error code; or to free
465 * the restart context if the operation is to be aborted.
466 *
467 * It is strictly required that all input parameters and the
468 * restart context be the same on successive calls for the
469 * same operation, but output parameters need not be the
470 * same; they must not be used until the function finally
471 * returns 0.
472 *
473 * This only applies to functions whose documentation
474 * mentions they may return #MBEDTLS_ERR_ECP_IN_PROGRESS (or
475 * #MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS for functions in the
476 * SSL module). For functions that accept a "restart context"
477 * argument, passing NULL disables restart and makes the
478 * function equivalent to the function with the same name
479 * with \c _restartable removed. For functions in the ECDH
480 * module, restart is disabled unless the function accepts
481 * an "ECDH context" argument and
482 * mbedtls_ecdh_enable_restart() was previously called on
483 * that context. For function in the SSL module, restart is
484 * only enabled for specific sides and key exchanges
485 * (currently only for clients and ECDHE-ECDSA).
486 *
487 * \warning Using the PSA interruptible interfaces with keys in local
488 * storage and no accelerator driver will also call this
489 * function to set the values specified via those interfaces,
490 * overwriting values previously set. Care should be taken if
491 * mixing these two interfaces.
492 *
493 * \param max_ops Maximum number of basic operations done in a row.
494 * Default: 0 (unlimited).
495 * Lower (non-zero) values mean ECC functions will block for
496 * a lesser maximum amount of time.
497 *
498 * \note A "basic operation" is defined as a rough equivalent of a
499 * multiplication in GF(p) for the NIST P-256 curve.
500 * As an indication, with default settings, a scalar
501 * multiplication (full run of \c mbedtls_ecp_mul()) is:
502 * - about 3300 basic operations for P-256
503 * - about 9400 basic operations for P-384
504 *
505 * \note Very low values are not always respected: sometimes
506 * functions need to block for a minimum number of
507 * operations, and will do so even if max_ops is set to a
508 * lower value. That minimum depends on the curve size, and
509 * can be made lower by decreasing the value of
510 * \c MBEDTLS_ECP_WINDOW_SIZE. As an indication, here is the
511 * lowest effective value for various curves and values of
512 * that parameter (w for short):
513 * w=6 w=5 w=4 w=3 w=2
514 * P-256 208 208 160 136 124
515 * P-384 682 416 320 272 248
516 * P-521 1364 832 640 544 496
517 *
518 * \note This setting is currently ignored by Curve25519.
519 */
520 void mbedtls_ecp_set_max_ops(unsigned max_ops);
521
522 /**
523 * \brief Check if restart is enabled (max_ops != 0)
524 *
525 * \return \c 0 if \c max_ops == 0 (restart disabled)
526 * \return \c 1 otherwise (restart enabled)
527 */
528 int mbedtls_ecp_restart_is_enabled(void);
529 #endif /* MBEDTLS_ECP_RESTARTABLE */
530
531 /*
532 * Get the type of a curve
533 */
534 mbedtls_ecp_curve_type mbedtls_ecp_get_type(const mbedtls_ecp_group *grp);
535
536 /**
537 * \brief This function retrieves the information defined in
538 * mbedtls_ecp_curve_info() for all supported curves.
539 *
540 * \note This function returns information about all curves
541 * supported by the library. Some curves may not be
542 * supported for all algorithms. Call mbedtls_ecdh_can_do()
543 * or mbedtls_ecdsa_can_do() to check if a curve is
544 * supported for ECDH or ECDSA.
545 *
546 * \return A statically allocated array. The last entry is 0.
547 */
548 const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list(void);
549
550 /**
551 * \brief This function retrieves the list of internal group
552 * identifiers of all supported curves in the order of
553 * preference.
554 *
555 * \note This function returns information about all curves
556 * supported by the library. Some curves may not be
557 * supported for all algorithms. Call mbedtls_ecdh_can_do()
558 * or mbedtls_ecdsa_can_do() to check if a curve is
559 * supported for ECDH or ECDSA.
560 *
561 * \return A statically allocated array,
562 * terminated with MBEDTLS_ECP_DP_NONE.
563 */
564 const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list(void);
565
566 /**
567 * \brief This function retrieves curve information from an internal
568 * group identifier.
569 *
570 * \param grp_id An \c MBEDTLS_ECP_DP_XXX value.
571 *
572 * \return The associated curve information on success.
573 * \return NULL on failure.
574 */
575 const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_grp_id(mbedtls_ecp_group_id grp_id);
576
577 /**
578 * \brief This function retrieves curve information from a TLS
579 * NamedCurve value.
580 *
581 * \param tls_id An \c MBEDTLS_ECP_DP_XXX value.
582 *
583 * \return The associated curve information on success.
584 * \return NULL on failure.
585 */
586 const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id(uint16_t tls_id);
587
588 /**
589 * \brief This function retrieves curve information from a
590 * human-readable name.
591 *
592 * \param name The human-readable name.
593 *
594 * \return The associated curve information on success.
595 * \return NULL on failure.
596 */
597 const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name(const char *name);
598
599 /**
600 * \brief This function initializes a point as zero.
601 *
602 * \param pt The point to initialize.
603 */
604 void mbedtls_ecp_point_init(mbedtls_ecp_point *pt);
605
606 /**
607 * \brief This function initializes an ECP group context
608 * without loading any domain parameters.
609 *
610 * \note After this function is called, domain parameters
611 * for various ECP groups can be loaded through the
612 * mbedtls_ecp_group_load() or mbedtls_ecp_tls_read_group()
613 * functions.
614 */
615 void mbedtls_ecp_group_init(mbedtls_ecp_group *grp);
616
617 /**
618 * \brief This function initializes a key pair as an invalid one.
619 *
620 * \param key The key pair to initialize.
621 */
622 void mbedtls_ecp_keypair_init(mbedtls_ecp_keypair *key);
623
624 /**
625 * \brief This function frees the components of a point.
626 *
627 * \param pt The point to free.
628 */
629 void mbedtls_ecp_point_free(mbedtls_ecp_point *pt);
630
631 /**
632 * \brief This function frees the components of an ECP group.
633 *
634 * \param grp The group to free. This may be \c NULL, in which
635 * case this function returns immediately. If it is not
636 * \c NULL, it must point to an initialized ECP group.
637 */
638 void mbedtls_ecp_group_free(mbedtls_ecp_group *grp);
639
640 /**
641 * \brief This function frees the components of a key pair.
642 *
643 * \param key The key pair to free. This may be \c NULL, in which
644 * case this function returns immediately. If it is not
645 * \c NULL, it must point to an initialized ECP key pair.
646 */
647 void mbedtls_ecp_keypair_free(mbedtls_ecp_keypair *key);
648
649 #if defined(MBEDTLS_ECP_RESTARTABLE)
650 /**
651 * \brief Initialize a restart context.
652 *
653 * \param ctx The restart context to initialize. This must
654 * not be \c NULL.
655 */
656 void mbedtls_ecp_restart_init(mbedtls_ecp_restart_ctx *ctx);
657
658 /**
659 * \brief Free the components of a restart context.
660 *
661 * \param ctx The restart context to free. This may be \c NULL, in which
662 * case this function returns immediately. If it is not
663 * \c NULL, it must point to an initialized restart context.
664 */
665 void mbedtls_ecp_restart_free(mbedtls_ecp_restart_ctx *ctx);
666 #endif /* MBEDTLS_ECP_RESTARTABLE */
667
668 /**
669 * \brief This function copies the contents of point \p Q into
670 * point \p P.
671 *
672 * \param P The destination point. This must be initialized.
673 * \param Q The source point. This must be initialized.
674 *
675 * \return \c 0 on success.
676 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
677 * \return Another negative error code for other kinds of failure.
678 */
679 int mbedtls_ecp_copy(mbedtls_ecp_point *P, const mbedtls_ecp_point *Q);
680
681 /**
682 * \brief This function copies the contents of group \p src into
683 * group \p dst.
684 *
685 * \param dst The destination group. This must be initialized.
686 * \param src The source group. This must be initialized.
687 *
688 * \return \c 0 on success.
689 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
690 * \return Another negative error code on other kinds of failure.
691 */
692 int mbedtls_ecp_group_copy(mbedtls_ecp_group *dst,
693 const mbedtls_ecp_group *src);
694
695 /**
696 * \brief This function sets a point to the point at infinity.
697 *
698 * \param pt The point to set. This must be initialized.
699 *
700 * \return \c 0 on success.
701 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
702 * \return Another negative error code on other kinds of failure.
703 */
704 int mbedtls_ecp_set_zero(mbedtls_ecp_point *pt);
705
706 /**
707 * \brief This function checks if a point is the point at infinity.
708 *
709 * \param pt The point to test. This must be initialized.
710 *
711 * \return \c 1 if the point is zero.
712 * \return \c 0 if the point is non-zero.
713 * \return A negative error code on failure.
714 */
715 int mbedtls_ecp_is_zero(mbedtls_ecp_point *pt);
716
717 /**
718 * \brief This function compares two points.
719 *
720 * \note This assumes that the points are normalized. Otherwise,
721 * they may compare as "not equal" even if they are.
722 *
723 * \param P The first point to compare. This must be initialized.
724 * \param Q The second point to compare. This must be initialized.
725 *
726 * \return \c 0 if the points are equal.
727 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the points are not equal.
728 */
729 int mbedtls_ecp_point_cmp(const mbedtls_ecp_point *P,
730 const mbedtls_ecp_point *Q);
731
732 /**
733 * \brief This function imports a non-zero point from two ASCII
734 * strings.
735 *
736 * \param P The destination point. This must be initialized.
737 * \param radix The numeric base of the input.
738 * \param x The first affine coordinate, as a null-terminated string.
739 * \param y The second affine coordinate, as a null-terminated string.
740 *
741 * \return \c 0 on success.
742 * \return An \c MBEDTLS_ERR_MPI_XXX error code on failure.
743 */
744 int mbedtls_ecp_point_read_string(mbedtls_ecp_point *P, int radix,
745 const char *x, const char *y);
746
747 /**
748 * \brief This function exports a point into unsigned binary data.
749 *
750 * \param grp The group to which the point should belong.
751 * This must be initialized and have group parameters
752 * set, for example through mbedtls_ecp_group_load().
753 * \param P The point to export. This must be initialized.
754 * \param format The point format. This must be either
755 * #MBEDTLS_ECP_PF_COMPRESSED or #MBEDTLS_ECP_PF_UNCOMPRESSED.
756 * (For groups without these formats, this parameter is
757 * ignored. But it still has to be either of the above
758 * values.)
759 * \param olen The address at which to store the length of
760 * the output in Bytes. This must not be \c NULL.
761 * \param buf The output buffer. This must be a writable buffer
762 * of length \p buflen Bytes.
763 * \param buflen The length of the output buffer \p buf in Bytes.
764 *
765 * \return \c 0 on success.
766 * \return #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the output buffer
767 * is too small to hold the point.
768 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the point format
769 * or the export for the given group is not implemented.
770 * \return Another negative error code on other kinds of failure.
771 */
772 int mbedtls_ecp_point_write_binary(const mbedtls_ecp_group *grp,
773 const mbedtls_ecp_point *P,
774 int format, size_t *olen,
775 unsigned char *buf, size_t buflen);
776
777 /**
778 * \brief This function imports a point from unsigned binary data.
779 *
780 * \note This function does not check that the point actually
781 * belongs to the given group, see mbedtls_ecp_check_pubkey()
782 * for that.
783 *
784 * \note For compressed points, see #MBEDTLS_ECP_PF_COMPRESSED for
785 * limitations.
786 *
787 * \param grp The group to which the point should belong.
788 * This must be initialized and have group parameters
789 * set, for example through mbedtls_ecp_group_load().
790 * \param P The destination context to import the point to.
791 * This must be initialized.
792 * \param buf The input buffer. This must be a readable buffer
793 * of length \p ilen Bytes.
794 * \param ilen The length of the input buffer \p buf in Bytes.
795 *
796 * \return \c 0 on success.
797 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the input is invalid.
798 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
799 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the import for the
800 * given group is not implemented.
801 */
802 int mbedtls_ecp_point_read_binary(const mbedtls_ecp_group *grp,
803 mbedtls_ecp_point *P,
804 const unsigned char *buf, size_t ilen);
805
806 /**
807 * \brief This function imports a point from a TLS ECPoint record.
808 *
809 * \note On function return, \p *buf is updated to point immediately
810 * after the ECPoint record.
811 *
812 * \param grp The ECP group to use.
813 * This must be initialized and have group parameters
814 * set, for example through mbedtls_ecp_group_load().
815 * \param pt The destination point.
816 * \param buf The address of the pointer to the start of the input buffer.
817 * \param len The length of the buffer.
818 *
819 * \return \c 0 on success.
820 * \return An \c MBEDTLS_ERR_MPI_XXX error code on initialization
821 * failure.
822 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid.
823 */
824 int mbedtls_ecp_tls_read_point(const mbedtls_ecp_group *grp,
825 mbedtls_ecp_point *pt,
826 const unsigned char **buf, size_t len);
827
828 /**
829 * \brief This function exports a point as a TLS ECPoint record
830 * defined in RFC 4492, Section 5.4.
831 *
832 * \param grp The ECP group to use.
833 * This must be initialized and have group parameters
834 * set, for example through mbedtls_ecp_group_load().
835 * \param pt The point to be exported. This must be initialized.
836 * \param format The point format to use. This must be either
837 * #MBEDTLS_ECP_PF_COMPRESSED or #MBEDTLS_ECP_PF_UNCOMPRESSED.
838 * \param olen The address at which to store the length in Bytes
839 * of the data written.
840 * \param buf The target buffer. This must be a writable buffer of
841 * length \p blen Bytes.
842 * \param blen The length of the target buffer \p buf in Bytes.
843 *
844 * \return \c 0 on success.
845 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the input is invalid.
846 * \return #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the target buffer
847 * is too small to hold the exported point.
848 * \return Another negative error code on other kinds of failure.
849 */
850 int mbedtls_ecp_tls_write_point(const mbedtls_ecp_group *grp,
851 const mbedtls_ecp_point *pt,
852 int format, size_t *olen,
853 unsigned char *buf, size_t blen);
854
855 /**
856 * \brief This function sets up an ECP group context
857 * from a standardized set of domain parameters.
858 *
859 * \note The index should be a value of the NamedCurve enum,
860 * as defined in <em>RFC-4492: Elliptic Curve Cryptography
861 * (ECC) Cipher Suites for Transport Layer Security (TLS)</em>,
862 * usually in the form of an \c MBEDTLS_ECP_DP_XXX macro.
863 *
864 * \param grp The group context to setup. This must be initialized.
865 * \param id The identifier of the domain parameter set to load.
866 *
867 * \return \c 0 on success.
868 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if \p id doesn't
869 * correspond to a known group.
870 * \return Another negative error code on other kinds of failure.
871 */
872 int mbedtls_ecp_group_load(mbedtls_ecp_group *grp, mbedtls_ecp_group_id id);
873
874 /**
875 * \brief This function sets up an ECP group context from a TLS
876 * ECParameters record as defined in RFC 4492, Section 5.4.
877 *
878 * \note The read pointer \p buf is updated to point right after
879 * the ECParameters record on exit.
880 *
881 * \param grp The group context to setup. This must be initialized.
882 * \param buf The address of the pointer to the start of the input buffer.
883 * \param len The length of the input buffer \c *buf in Bytes.
884 *
885 * \return \c 0 on success.
886 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid.
887 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the group is not
888 * recognized.
889 * \return Another negative error code on other kinds of failure.
890 */
891 int mbedtls_ecp_tls_read_group(mbedtls_ecp_group *grp,
892 const unsigned char **buf, size_t len);
893
894 /**
895 * \brief This function extracts an elliptic curve group ID from a
896 * TLS ECParameters record as defined in RFC 4492, Section 5.4.
897 *
898 * \note The read pointer \p buf is updated to point right after
899 * the ECParameters record on exit.
900 *
901 * \param grp The address at which to store the group id.
902 * This must not be \c NULL.
903 * \param buf The address of the pointer to the start of the input buffer.
904 * \param len The length of the input buffer \c *buf in Bytes.
905 *
906 * \return \c 0 on success.
907 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid.
908 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the group is not
909 * recognized.
910 * \return Another negative error code on other kinds of failure.
911 */
912 int mbedtls_ecp_tls_read_group_id(mbedtls_ecp_group_id *grp,
913 const unsigned char **buf,
914 size_t len);
915 /**
916 * \brief This function exports an elliptic curve as a TLS
917 * ECParameters record as defined in RFC 4492, Section 5.4.
918 *
919 * \param grp The ECP group to be exported.
920 * This must be initialized and have group parameters
921 * set, for example through mbedtls_ecp_group_load().
922 * \param olen The address at which to store the number of Bytes written.
923 * This must not be \c NULL.
924 * \param buf The buffer to write to. This must be a writable buffer
925 * of length \p blen Bytes.
926 * \param blen The length of the output buffer \p buf in Bytes.
927 *
928 * \return \c 0 on success.
929 * \return #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the output
930 * buffer is too small to hold the exported group.
931 * \return Another negative error code on other kinds of failure.
932 */
933 int mbedtls_ecp_tls_write_group(const mbedtls_ecp_group *grp,
934 size_t *olen,
935 unsigned char *buf, size_t blen);
936
937 /**
938 * \brief This function performs a scalar multiplication of a point
939 * by an integer: \p R = \p m * \p P.
940 *
941 * It is not thread-safe to use same group in multiple threads.
942 *
943 * \note To prevent timing attacks, this function
944 * executes the exact same sequence of base-field
945 * operations for any valid \p m. It avoids any if-branch or
946 * array index depending on the value of \p m. It also uses
947 * \p f_rng to randomize some intermediate results.
948 *
949 * \param grp The ECP group to use.
950 * This must be initialized and have group parameters
951 * set, for example through mbedtls_ecp_group_load().
952 * \param R The point in which to store the result of the calculation.
953 * This must be initialized.
954 * \param m The integer by which to multiply. This must be initialized.
955 * \param P The point to multiply. This must be initialized.
956 * \param f_rng The RNG function. This must not be \c NULL.
957 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c
958 * NULL if \p f_rng doesn't need a context.
959 *
960 * \return \c 0 on success.
961 * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m is not a valid private
962 * key, or \p P is not a valid public key.
963 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
964 * \return Another negative error code on other kinds of failure.
965 */
966 int mbedtls_ecp_mul(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
967 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
968 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
969
970 /**
971 * \brief This function performs multiplication of a point by
972 * an integer: \p R = \p m * \p P in a restartable way.
973 *
974 * \see mbedtls_ecp_mul()
975 *
976 * \note This function does the same as \c mbedtls_ecp_mul(), but
977 * it can return early and restart according to the limit set
978 * with \c mbedtls_ecp_set_max_ops() to reduce blocking.
979 *
980 * \param grp The ECP group to use.
981 * This must be initialized and have group parameters
982 * set, for example through mbedtls_ecp_group_load().
983 * \param R The point in which to store the result of the calculation.
984 * This must be initialized.
985 * \param m The integer by which to multiply. This must be initialized.
986 * \param P The point to multiply. This must be initialized.
987 * \param f_rng The RNG function. This must not be \c NULL.
988 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c
989 * NULL if \p f_rng doesn't need a context.
990 * \param rs_ctx The restart context (NULL disables restart).
991 *
992 * \return \c 0 on success.
993 * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m is not a valid private
994 * key, or \p P is not a valid public key.
995 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
996 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
997 * operations was reached: see \c mbedtls_ecp_set_max_ops().
998 * \return Another negative error code on other kinds of failure.
999 */
1000 int mbedtls_ecp_mul_restartable(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
1001 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
1002 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1003 mbedtls_ecp_restart_ctx *rs_ctx);
1004
1005 #if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
1006 /**
1007 * \brief This function checks if domain parameter A of the curve is
1008 * \c -3.
1009 *
1010 * \note This function is only defined for short Weierstrass curves.
1011 * It may not be included in builds without any short
1012 * Weierstrass curve.
1013 *
1014 * \param grp The ECP group to use.
1015 * This must be initialized and have group parameters
1016 * set, for example through mbedtls_ecp_group_load().
1017 *
1018 * \return \c 1 if <code>A = -3</code>.
1019 * \return \c 0 Otherwise.
1020 */
mbedtls_ecp_group_a_is_minus_3(const mbedtls_ecp_group * grp)1021 static inline int mbedtls_ecp_group_a_is_minus_3(const mbedtls_ecp_group *grp)
1022 {
1023 return grp->A.MBEDTLS_PRIVATE(p) == NULL;
1024 }
1025
1026 /**
1027 * \brief This function performs multiplication and addition of two
1028 * points by integers: \p R = \p m * \p P + \p n * \p Q
1029 *
1030 * It is not thread-safe to use same group in multiple threads.
1031 *
1032 * \note In contrast to mbedtls_ecp_mul(), this function does not
1033 * guarantee a constant execution flow and timing.
1034 *
1035 * \note This function is only defined for short Weierstrass curves.
1036 * It may not be included in builds without any short
1037 * Weierstrass curve.
1038 *
1039 * \param grp The ECP group to use.
1040 * This must be initialized and have group parameters
1041 * set, for example through mbedtls_ecp_group_load().
1042 * \param R The point in which to store the result of the calculation.
1043 * This must be initialized.
1044 * \param m The integer by which to multiply \p P.
1045 * This must be initialized.
1046 * \param P The point to multiply by \p m. This must be initialized.
1047 * \param n The integer by which to multiply \p Q.
1048 * This must be initialized.
1049 * \param Q The point to be multiplied by \p n.
1050 * This must be initialized.
1051 *
1052 * \return \c 0 on success.
1053 * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m or \p n are not
1054 * valid private keys, or \p P or \p Q are not valid public
1055 * keys.
1056 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
1057 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if \p grp does not
1058 * designate a short Weierstrass curve.
1059 * \return Another negative error code on other kinds of failure.
1060 */
1061 int mbedtls_ecp_muladd(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
1062 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
1063 const mbedtls_mpi *n, const mbedtls_ecp_point *Q);
1064
1065 /**
1066 * \brief This function performs multiplication and addition of two
1067 * points by integers: \p R = \p m * \p P + \p n * \p Q in a
1068 * restartable way.
1069 *
1070 * \see \c mbedtls_ecp_muladd()
1071 *
1072 * \note This function works the same as \c mbedtls_ecp_muladd(),
1073 * but it can return early and restart according to the limit
1074 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
1075 *
1076 * \note This function is only defined for short Weierstrass curves.
1077 * It may not be included in builds without any short
1078 * Weierstrass curve.
1079 *
1080 * \param grp The ECP group to use.
1081 * This must be initialized and have group parameters
1082 * set, for example through mbedtls_ecp_group_load().
1083 * \param R The point in which to store the result of the calculation.
1084 * This must be initialized.
1085 * \param m The integer by which to multiply \p P.
1086 * This must be initialized.
1087 * \param P The point to multiply by \p m. This must be initialized.
1088 * \param n The integer by which to multiply \p Q.
1089 * This must be initialized.
1090 * \param Q The point to be multiplied by \p n.
1091 * This must be initialized.
1092 * \param rs_ctx The restart context (NULL disables restart).
1093 *
1094 * \return \c 0 on success.
1095 * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m or \p n are not
1096 * valid private keys, or \p P or \p Q are not valid public
1097 * keys.
1098 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
1099 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if \p grp does not
1100 * designate a short Weierstrass curve.
1101 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
1102 * operations was reached: see \c mbedtls_ecp_set_max_ops().
1103 * \return Another negative error code on other kinds of failure.
1104 */
1105 int mbedtls_ecp_muladd_restartable(
1106 mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
1107 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
1108 const mbedtls_mpi *n, const mbedtls_ecp_point *Q,
1109 mbedtls_ecp_restart_ctx *rs_ctx);
1110 #endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
1111
1112 /**
1113 * \brief This function checks that a point is a valid public key
1114 * on this curve.
1115 *
1116 * It only checks that the point is non-zero, has
1117 * valid coordinates and lies on the curve. It does not verify
1118 * that it is indeed a multiple of \c G. This additional
1119 * check is computationally more expensive, is not required
1120 * by standards, and should not be necessary if the group
1121 * used has a small cofactor. In particular, it is useless for
1122 * the NIST groups which all have a cofactor of 1.
1123 *
1124 * \note This function uses bare components rather than an
1125 * ::mbedtls_ecp_keypair structure, to ease use with other
1126 * structures, such as ::mbedtls_ecdh_context or
1127 * ::mbedtls_ecdsa_context.
1128 *
1129 * \param grp The ECP group the point should belong to.
1130 * This must be initialized and have group parameters
1131 * set, for example through mbedtls_ecp_group_load().
1132 * \param pt The point to check. This must be initialized.
1133 *
1134 * \return \c 0 if the point is a valid public key.
1135 * \return #MBEDTLS_ERR_ECP_INVALID_KEY if the point is not
1136 * a valid public key for the given curve.
1137 * \return Another negative error code on other kinds of failure.
1138 */
1139 int mbedtls_ecp_check_pubkey(const mbedtls_ecp_group *grp,
1140 const mbedtls_ecp_point *pt);
1141
1142 /**
1143 * \brief This function checks that an \c mbedtls_mpi is a
1144 * valid private key for this curve.
1145 *
1146 * \note This function uses bare components rather than an
1147 * ::mbedtls_ecp_keypair structure to ease use with other
1148 * structures, such as ::mbedtls_ecdh_context or
1149 * ::mbedtls_ecdsa_context.
1150 *
1151 * \param grp The ECP group the private key should belong to.
1152 * This must be initialized and have group parameters
1153 * set, for example through mbedtls_ecp_group_load().
1154 * \param d The integer to check. This must be initialized.
1155 *
1156 * \return \c 0 if the point is a valid private key.
1157 * \return #MBEDTLS_ERR_ECP_INVALID_KEY if the point is not a valid
1158 * private key for the given curve.
1159 * \return Another negative error code on other kinds of failure.
1160 */
1161 int mbedtls_ecp_check_privkey(const mbedtls_ecp_group *grp,
1162 const mbedtls_mpi *d);
1163
1164 /**
1165 * \brief This function generates a private key.
1166 *
1167 * \param grp The ECP group to generate a private key for.
1168 * This must be initialized and have group parameters
1169 * set, for example through mbedtls_ecp_group_load().
1170 * \param d The destination MPI (secret part). This must be initialized.
1171 * \param f_rng The RNG function. This must not be \c NULL.
1172 * \param p_rng The RNG parameter to be passed to \p f_rng. This may be
1173 * \c NULL if \p f_rng doesn't need a context argument.
1174 *
1175 * \return \c 0 on success.
1176 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
1177 * on failure.
1178 */
1179 int mbedtls_ecp_gen_privkey(const mbedtls_ecp_group *grp,
1180 mbedtls_mpi *d,
1181 int (*f_rng)(void *, unsigned char *, size_t),
1182 void *p_rng);
1183
1184 /**
1185 * \brief This function generates a keypair with a configurable base
1186 * point.
1187 *
1188 * \note This function uses bare components rather than an
1189 * ::mbedtls_ecp_keypair structure to ease use with other
1190 * structures, such as ::mbedtls_ecdh_context or
1191 * ::mbedtls_ecdsa_context.
1192 *
1193 * \param grp The ECP group to generate a key pair for.
1194 * This must be initialized and have group parameters
1195 * set, for example through mbedtls_ecp_group_load().
1196 * \param G The base point to use. This must be initialized
1197 * and belong to \p grp. It replaces the default base
1198 * point \c grp->G used by mbedtls_ecp_gen_keypair().
1199 * \param d The destination MPI (secret part).
1200 * This must be initialized.
1201 * \param Q The destination point (public part).
1202 * This must be initialized.
1203 * \param f_rng The RNG function. This must not be \c NULL.
1204 * \param p_rng The RNG context to be passed to \p f_rng. This may
1205 * be \c NULL if \p f_rng doesn't need a context argument.
1206 *
1207 * \return \c 0 on success.
1208 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
1209 * on failure.
1210 */
1211 int mbedtls_ecp_gen_keypair_base(mbedtls_ecp_group *grp,
1212 const mbedtls_ecp_point *G,
1213 mbedtls_mpi *d, mbedtls_ecp_point *Q,
1214 int (*f_rng)(void *, unsigned char *, size_t),
1215 void *p_rng);
1216
1217 /**
1218 * \brief This function generates an ECP keypair.
1219 *
1220 * \note This function uses bare components rather than an
1221 * ::mbedtls_ecp_keypair structure to ease use with other
1222 * structures, such as ::mbedtls_ecdh_context or
1223 * ::mbedtls_ecdsa_context.
1224 *
1225 * \param grp The ECP group to generate a key pair for.
1226 * This must be initialized and have group parameters
1227 * set, for example through mbedtls_ecp_group_load().
1228 * \param d The destination MPI (secret part).
1229 * This must be initialized.
1230 * \param Q The destination point (public part).
1231 * This must be initialized.
1232 * \param f_rng The RNG function. This must not be \c NULL.
1233 * \param p_rng The RNG context to be passed to \p f_rng. This may
1234 * be \c NULL if \p f_rng doesn't need a context argument.
1235 *
1236 * \return \c 0 on success.
1237 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
1238 * on failure.
1239 */
1240 int mbedtls_ecp_gen_keypair(mbedtls_ecp_group *grp, mbedtls_mpi *d,
1241 mbedtls_ecp_point *Q,
1242 int (*f_rng)(void *, unsigned char *, size_t),
1243 void *p_rng);
1244
1245 /**
1246 * \brief This function generates an ECP key.
1247 *
1248 * \param grp_id The ECP group identifier.
1249 * \param key The destination key. This must be initialized.
1250 * \param f_rng The RNG function to use. This must not be \c NULL.
1251 * \param p_rng The RNG context to be passed to \p f_rng. This may
1252 * be \c NULL if \p f_rng doesn't need a context argument.
1253 *
1254 * \return \c 0 on success.
1255 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
1256 * on failure.
1257 */
1258 int mbedtls_ecp_gen_key(mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
1259 int (*f_rng)(void *, unsigned char *, size_t),
1260 void *p_rng);
1261
1262 /**
1263 * \brief This function reads an elliptic curve private key.
1264 *
1265 * \param grp_id The ECP group identifier.
1266 * \param key The destination key.
1267 * \param buf The buffer containing the binary representation of the
1268 * key. (Big endian integer for Weierstrass curves, byte
1269 * string for Montgomery curves.)
1270 * \param buflen The length of the buffer in bytes.
1271 *
1272 * \return \c 0 on success.
1273 * \return #MBEDTLS_ERR_ECP_INVALID_KEY error if the key is
1274 * invalid.
1275 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
1276 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the operation for
1277 * the group is not implemented.
1278 * \return Another negative error code on different kinds of failure.
1279 */
1280 int mbedtls_ecp_read_key(mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
1281 const unsigned char *buf, size_t buflen);
1282
1283 /**
1284 * \brief This function exports an elliptic curve private key.
1285 *
1286 * \param key The private key.
1287 * \param buf The output buffer for containing the binary representation
1288 * of the key. (Big endian integer for Weierstrass curves, byte
1289 * string for Montgomery curves.)
1290 * \param buflen The total length of the buffer in bytes.
1291 *
1292 * \return \c 0 on success.
1293 * \return #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the \p key
1294 representation is larger than the available space in \p buf.
1295 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the operation for
1296 * the group is not implemented.
1297 * \return Another negative error code on different kinds of failure.
1298 */
1299 int mbedtls_ecp_write_key(mbedtls_ecp_keypair *key,
1300 unsigned char *buf, size_t buflen);
1301
1302 /**
1303 * \brief This function checks that the keypair objects
1304 * \p pub and \p prv have the same group and the
1305 * same public point, and that the private key in
1306 * \p prv is consistent with the public key.
1307 *
1308 * \param pub The keypair structure holding the public key. This
1309 * must be initialized. If it contains a private key, that
1310 * part is ignored.
1311 * \param prv The keypair structure holding the full keypair.
1312 * This must be initialized.
1313 * \param f_rng The RNG function. This must not be \c NULL.
1314 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c
1315 * NULL if \p f_rng doesn't need a context.
1316 *
1317 * \return \c 0 on success, meaning that the keys are valid and match.
1318 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the keys are invalid or do not match.
1319 * \return An \c MBEDTLS_ERR_ECP_XXX or an \c MBEDTLS_ERR_MPI_XXX
1320 * error code on calculation failure.
1321 */
1322 int mbedtls_ecp_check_pub_priv(
1323 const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv,
1324 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
1325
1326 /**
1327 * \brief This function exports generic key-pair parameters.
1328 *
1329 * \param key The key pair to export from.
1330 * \param grp Slot for exported ECP group.
1331 * It must point to an initialized ECP group.
1332 * \param d Slot for the exported secret value.
1333 * It must point to an initialized mpi.
1334 * \param Q Slot for the exported public value.
1335 * It must point to an initialized ECP point.
1336 *
1337 * \return \c 0 on success,
1338 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
1339 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if key id doesn't
1340 * correspond to a known group.
1341 * \return Another negative error code on other kinds of failure.
1342 */
1343 int mbedtls_ecp_export(const mbedtls_ecp_keypair *key, mbedtls_ecp_group *grp,
1344 mbedtls_mpi *d, mbedtls_ecp_point *Q);
1345
1346 #if defined(MBEDTLS_SELF_TEST)
1347
1348 /**
1349 * \brief The ECP checkup routine.
1350 *
1351 * \return \c 0 on success.
1352 * \return \c 1 on failure.
1353 */
1354 int mbedtls_ecp_self_test(int verbose);
1355
1356 #endif /* MBEDTLS_SELF_TEST */
1357
1358 #ifdef __cplusplus
1359 }
1360 #endif
1361
1362 #endif /* ecp.h */
1363