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