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