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