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