xref: /reactos/sdk/include/reactos/libs/mbedtls/ecp.h (revision 103a79ce)
1 /**
2  * \file ecp.h
3  *
4  * \brief This file provides an API for Elliptic Curves over GF(P) (ECP).
5  *
6  * The use of ECP in cryptography and TLS is defined in
7  * <em>Standards for Efficient Cryptography Group (SECG): SEC1
8  * Elliptic Curve Cryptography</em> and
9  * <em>RFC-4492: Elliptic Curve Cryptography (ECC) Cipher Suites
10  * for Transport Layer Security (TLS)</em>.
11  *
12  * <em>RFC-2409: The Internet Key Exchange (IKE)</em> defines ECP
13  * group types.
14  *
15  */
16 
17 /*
18  *  Copyright The Mbed TLS Contributors
19  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
20  *
21  *  This file is provided under the Apache License 2.0, or the
22  *  GNU General Public License v2.0 or later.
23  *
24  *  **********
25  *  Apache License 2.0:
26  *
27  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
28  *  not use this file except in compliance with the License.
29  *  You may obtain a copy of the License at
30  *
31  *  http://www.apache.org/licenses/LICENSE-2.0
32  *
33  *  Unless required by applicable law or agreed to in writing, software
34  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
35  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
36  *  See the License for the specific language governing permissions and
37  *  limitations under the License.
38  *
39  *  **********
40  *
41  *  **********
42  *  GNU General Public License v2.0 or later:
43  *
44  *  This program is free software; you can redistribute it and/or modify
45  *  it under the terms of the GNU General Public License as published by
46  *  the Free Software Foundation; either version 2 of the License, or
47  *  (at your option) any later version.
48  *
49  *  This program is distributed in the hope that it will be useful,
50  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
51  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
52  *  GNU General Public License for more details.
53  *
54  *  You should have received a copy of the GNU General Public License along
55  *  with this program; if not, write to the Free Software Foundation, Inc.,
56  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
57  *
58  *  **********
59  */
60 
61 #ifndef MBEDTLS_ECP_H
62 #define MBEDTLS_ECP_H
63 
64 #if !defined(MBEDTLS_CONFIG_FILE)
65 #include "config.h"
66 #else
67 #include MBEDTLS_CONFIG_FILE
68 #endif
69 
70 #include "bignum.h"
71 
72 /*
73  * ECP error codes
74  */
75 #define MBEDTLS_ERR_ECP_BAD_INPUT_DATA                    -0x4F80  /**< Bad input parameters to function. */
76 #define MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL                  -0x4F00  /**< The buffer is too small to write to. */
77 #define MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE               -0x4E80  /**< The requested feature is not available, for example, the requested curve is not supported. */
78 #define MBEDTLS_ERR_ECP_VERIFY_FAILED                     -0x4E00  /**< The signature is not valid. */
79 #define MBEDTLS_ERR_ECP_ALLOC_FAILED                      -0x4D80  /**< Memory allocation failed. */
80 #define MBEDTLS_ERR_ECP_RANDOM_FAILED                     -0x4D00  /**< Generation of random value, such as ephemeral key, failed. */
81 #define MBEDTLS_ERR_ECP_INVALID_KEY                       -0x4C80  /**< Invalid private or public key. */
82 #define MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH                  -0x4C00  /**< The buffer contains a valid signature followed by more data. */
83 
84 /* MBEDTLS_ERR_ECP_HW_ACCEL_FAILED is deprecated and should not be used. */
85 #define MBEDTLS_ERR_ECP_HW_ACCEL_FAILED                   -0x4B80  /**< The ECP hardware accelerator failed. */
86 
87 #define MBEDTLS_ERR_ECP_IN_PROGRESS                       -0x4B00  /**< Operation in progress, call again with the same parameters to continue. */
88 
89 #ifdef __cplusplus
90 extern "C" {
91 #endif
92 
93 /**
94  * Domain-parameter identifiers: curve, subgroup, and generator.
95  *
96  * \note Only curves over prime fields are supported.
97  *
98  * \warning This library does not support validation of arbitrary domain
99  * parameters. Therefore, only standardized domain parameters from trusted
100  * sources should be used. See mbedtls_ecp_group_load().
101  */
102 typedef enum
103 {
104     MBEDTLS_ECP_DP_NONE = 0,       /*!< Curve not defined. */
105     MBEDTLS_ECP_DP_SECP192R1,      /*!< Domain parameters for the 192-bit curve defined by FIPS 186-4 and SEC1. */
106     MBEDTLS_ECP_DP_SECP224R1,      /*!< Domain parameters for the 224-bit curve defined by FIPS 186-4 and SEC1. */
107     MBEDTLS_ECP_DP_SECP256R1,      /*!< Domain parameters for the 256-bit curve defined by FIPS 186-4 and SEC1. */
108     MBEDTLS_ECP_DP_SECP384R1,      /*!< Domain parameters for the 384-bit curve defined by FIPS 186-4 and SEC1. */
109     MBEDTLS_ECP_DP_SECP521R1,      /*!< Domain parameters for the 521-bit curve defined by FIPS 186-4 and SEC1. */
110     MBEDTLS_ECP_DP_BP256R1,        /*!< Domain parameters for 256-bit Brainpool curve. */
111     MBEDTLS_ECP_DP_BP384R1,        /*!< Domain parameters for 384-bit Brainpool curve. */
112     MBEDTLS_ECP_DP_BP512R1,        /*!< Domain parameters for 512-bit Brainpool curve. */
113     MBEDTLS_ECP_DP_CURVE25519,     /*!< Domain parameters for Curve25519. */
114     MBEDTLS_ECP_DP_SECP192K1,      /*!< Domain parameters for 192-bit "Koblitz" curve. */
115     MBEDTLS_ECP_DP_SECP224K1,      /*!< Domain parameters for 224-bit "Koblitz" curve. */
116     MBEDTLS_ECP_DP_SECP256K1,      /*!< Domain parameters for 256-bit "Koblitz" curve. */
117     MBEDTLS_ECP_DP_CURVE448,       /*!< Domain parameters for Curve448. */
118 } mbedtls_ecp_group_id;
119 
120 /**
121  * The number of supported curves, plus one for #MBEDTLS_ECP_DP_NONE.
122  *
123  * \note Montgomery curves are currently excluded.
124  */
125 #define MBEDTLS_ECP_DP_MAX     12
126 
127 /**
128  * Curve information, for use by other modules.
129  */
130 typedef struct mbedtls_ecp_curve_info
131 {
132     mbedtls_ecp_group_id grp_id;    /*!< An internal identifier. */
133     uint16_t tls_id;                /*!< The TLS NamedCurve identifier. */
134     uint16_t bit_size;              /*!< The curve size in bits. */
135     const char *name;               /*!< A human-friendly name. */
136 } mbedtls_ecp_curve_info;
137 
138 /**
139  * \brief           The ECP point structure, in Jacobian coordinates.
140  *
141  * \note            All functions expect and return points satisfying
142  *                  the following condition: <code>Z == 0</code> or
143  *                  <code>Z == 1</code>. Other values of \p Z are
144  *                  used only by internal functions.
145  *                  The point is zero, or "at infinity", if <code>Z == 0</code>.
146  *                  Otherwise, \p X and \p Y are its standard (affine)
147  *                  coordinates.
148  */
149 typedef struct mbedtls_ecp_point
150 {
151     mbedtls_mpi X;          /*!< The X coordinate of the ECP point. */
152     mbedtls_mpi Y;          /*!< The Y coordinate of the ECP point. */
153     mbedtls_mpi Z;          /*!< The Z coordinate of the ECP point. */
154 }
155 mbedtls_ecp_point;
156 
157 /* Determine the minimum safe value of MBEDTLS_ECP_MAX_BITS. */
158 #if !defined(MBEDTLS_ECP_C)
159 #define MBEDTLS_ECP_MAX_BITS_MIN 0
160 /* Note: the curves must be listed in DECREASING size! */
161 #elif defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
162 #define MBEDTLS_ECP_MAX_BITS_MIN 521
163 #elif defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
164 #define MBEDTLS_ECP_MAX_BITS_MIN 512
165 #elif defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
166 #define MBEDTLS_ECP_MAX_BITS_MIN 448
167 #elif defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
168 #define MBEDTLS_ECP_MAX_BITS_MIN 384
169 #elif defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
170 #define MBEDTLS_ECP_MAX_BITS_MIN 384
171 #elif defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
172 #define MBEDTLS_ECP_MAX_BITS_MIN 256
173 #elif defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
174 #define MBEDTLS_ECP_MAX_BITS_MIN 256
175 #elif defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
176 #define MBEDTLS_ECP_MAX_BITS_MIN 256
177 #elif defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
178 #define MBEDTLS_ECP_MAX_BITS_MIN 255
179 #elif defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
180 #define MBEDTLS_ECP_MAX_BITS_MIN 225 // n is slightly above 2^224
181 #elif defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
182 #define MBEDTLS_ECP_MAX_BITS_MIN 224
183 #elif defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
184 #define MBEDTLS_ECP_MAX_BITS_MIN 192
185 #elif defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
186 #define MBEDTLS_ECP_MAX_BITS_MIN 192
187 #else
188 #error "MBEDTLS_ECP_C enabled, but no curve?"
189 #endif
190 
191 #if !defined(MBEDTLS_ECP_ALT)
192 /*
193  * default mbed TLS elliptic curve arithmetic implementation
194  *
195  * (in case MBEDTLS_ECP_ALT is defined then the developer has to provide an
196  * alternative implementation for the whole module and it will replace this
197  * one.)
198  */
199 
200 /**
201  * \brief           The ECP group structure.
202  *
203  * We consider two types of curve equations:
204  * <ul><li>Short Weierstrass: <code>y^2 = x^3 + A x + B mod P</code>
205  * (SEC1 + RFC-4492)</li>
206  * <li>Montgomery: <code>y^2 = x^3 + A x^2 + x mod P</code> (Curve25519,
207  * Curve448)</li></ul>
208  * In both cases, the generator (\p G) for a prime-order subgroup is fixed.
209  *
210  * For Short Weierstrass, this subgroup is the whole curve, and its
211  * cardinality is denoted by \p N. Our code requires that \p N is an
212  * odd prime as mbedtls_ecp_mul() requires an odd number, and
213  * mbedtls_ecdsa_sign() requires that it is prime for blinding purposes.
214  *
215  * For Montgomery curves, we do not store \p A, but <code>(A + 2) / 4</code>,
216  * which is the quantity used in the formulas. Additionally, \p nbits is
217  * not the size of \p N but the required size for private keys.
218  *
219  * If \p modp is NULL, reduction modulo \p P is done using a generic algorithm.
220  * Otherwise, \p modp must point to a function that takes an \p mbedtls_mpi in the
221  * range of <code>0..2^(2*pbits)-1</code>, and transforms it in-place to an integer
222  * which is congruent mod \p P to the given MPI, and is close enough to \p pbits
223  * in size, so that it may be efficiently brought in the 0..P-1 range by a few
224  * additions or subtractions. Therefore, it is only an approximative modular
225  * reduction. It must return 0 on success and non-zero on failure.
226  *
227  * \note        Alternative implementations must keep the group IDs distinct. If
228  *              two group structures have the same ID, then they must be
229  *              identical.
230  *
231  */
232 typedef struct mbedtls_ecp_group
233 {
234     mbedtls_ecp_group_id id;    /*!< An internal group identifier. */
235     mbedtls_mpi P;              /*!< The prime modulus of the base field. */
236     mbedtls_mpi A;              /*!< For Short Weierstrass: \p A in the equation. For
237                                      Montgomery curves: <code>(A + 2) / 4</code>. */
238     mbedtls_mpi B;              /*!< For Short Weierstrass: \p B in the equation.
239                                      For Montgomery curves: unused. */
240     mbedtls_ecp_point G;        /*!< The generator of the subgroup used. */
241     mbedtls_mpi N;              /*!< The order of \p G. */
242     size_t pbits;               /*!< The number of bits in \p P.*/
243     size_t nbits;               /*!< For Short Weierstrass: The number of bits in \p P.
244                                      For Montgomery curves: the number of bits in the
245                                      private keys. */
246     unsigned int h;             /*!< \internal 1 if the constants are static. */
247     int (*modp)(mbedtls_mpi *); /*!< The function for fast pseudo-reduction
248                                      mod \p P (see above).*/
249     int (*t_pre)(mbedtls_ecp_point *, void *);  /*!< Unused. */
250     int (*t_post)(mbedtls_ecp_point *, void *); /*!< Unused. */
251     void *t_data;               /*!< Unused. */
252     mbedtls_ecp_point *T;       /*!< Pre-computed points for ecp_mul_comb(). */
253     size_t T_size;              /*!< The number of pre-computed points. */
254 }
255 mbedtls_ecp_group;
256 
257 /**
258  * \name SECTION: Module settings
259  *
260  * The configuration options you can set for this module are in this section.
261  * Either change them in config.h, or define them using the compiler command line.
262  * \{
263  */
264 
265 #if defined(MBEDTLS_ECP_MAX_BITS)
266 
267 #if MBEDTLS_ECP_MAX_BITS < MBEDTLS_ECP_MAX_BITS_MIN
268 #error "MBEDTLS_ECP_MAX_BITS is smaller than the largest supported curve"
269 #endif
270 
271 #else
272 /**
273  * The maximum size of the groups, that is, of \c N and \c P.
274  */
275 #define MBEDTLS_ECP_MAX_BITS     521   /**< The maximum size of groups, in bits. */
276 #endif
277 
278 #define MBEDTLS_ECP_MAX_BYTES    ( ( MBEDTLS_ECP_MAX_BITS + 7 ) / 8 )
279 #define MBEDTLS_ECP_MAX_PT_LEN   ( 2 * MBEDTLS_ECP_MAX_BYTES + 1 )
280 
281 #if !defined(MBEDTLS_ECP_WINDOW_SIZE)
282 /*
283  * Maximum "window" size used for point multiplication.
284  * Default: 6.
285  * Minimum value: 2. Maximum value: 7.
286  *
287  * Result is an array of at most ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) )
288  * points used for point multiplication. This value is directly tied to EC
289  * peak memory usage, so decreasing it by one should roughly cut memory usage
290  * by two (if large curves are in use).
291  *
292  * Reduction in size may reduce speed, but larger curves are impacted first.
293  * Sample performances (in ECDHE handshakes/s, with FIXED_POINT_OPTIM = 1):
294  *      w-size:     6       5       4       3       2
295  *      521       145     141     135     120      97
296  *      384       214     209     198     177     146
297  *      256       320     320     303     262     226
298  *      224       475     475     453     398     342
299  *      192       640     640     633     587     476
300  */
301 #define MBEDTLS_ECP_WINDOW_SIZE    6   /**< The maximum window size used. */
302 #endif /* MBEDTLS_ECP_WINDOW_SIZE */
303 
304 #if !defined(MBEDTLS_ECP_FIXED_POINT_OPTIM)
305 /*
306  * Trade memory for speed on fixed-point multiplication.
307  *
308  * This speeds up repeated multiplication of the generator (that is, the
309  * multiplication in ECDSA signatures, and half of the multiplications in
310  * ECDSA verification and ECDHE) by a factor roughly 3 to 4.
311  *
312  * The cost is increasing EC peak memory usage by a factor roughly 2.
313  *
314  * Change this value to 0 to reduce peak memory usage.
315  */
316 #define MBEDTLS_ECP_FIXED_POINT_OPTIM  1   /**< Enable fixed-point speed-up. */
317 #endif /* MBEDTLS_ECP_FIXED_POINT_OPTIM */
318 
319 /* \} name SECTION: Module settings */
320 
321 #else  /* MBEDTLS_ECP_ALT */
322 #include "ecp_alt.h"
323 #endif /* MBEDTLS_ECP_ALT */
324 
325 #if defined(MBEDTLS_ECP_RESTARTABLE)
326 
327 /**
328  * \brief           Internal restart context for multiplication
329  *
330  * \note            Opaque struct
331  */
332 typedef struct mbedtls_ecp_restart_mul mbedtls_ecp_restart_mul_ctx;
333 
334 /**
335  * \brief           Internal restart context for ecp_muladd()
336  *
337  * \note            Opaque struct
338  */
339 typedef struct mbedtls_ecp_restart_muladd mbedtls_ecp_restart_muladd_ctx;
340 
341 /**
342  * \brief           General context for resuming ECC operations
343  */
344 typedef struct
345 {
346     unsigned ops_done;                  /*!<  current ops count             */
347     unsigned depth;                     /*!<  call depth (0 = top-level)    */
348     mbedtls_ecp_restart_mul_ctx *rsm;   /*!<  ecp_mul_comb() sub-context    */
349     mbedtls_ecp_restart_muladd_ctx *ma; /*!<  ecp_muladd() sub-context      */
350 } mbedtls_ecp_restart_ctx;
351 
352 /*
353  * Operation counts for restartable functions
354  */
355 #define MBEDTLS_ECP_OPS_CHK   3 /*!< basic ops count for ecp_check_pubkey()  */
356 #define MBEDTLS_ECP_OPS_DBL   8 /*!< basic ops count for ecp_double_jac()    */
357 #define MBEDTLS_ECP_OPS_ADD  11 /*!< basic ops count for see ecp_add_mixed() */
358 #define MBEDTLS_ECP_OPS_INV 120 /*!< empirical equivalent for mpi_mod_inv()  */
359 
360 /**
361  * \brief           Internal; for restartable functions in other modules.
362  *                  Check and update basic ops budget.
363  *
364  * \param grp       Group structure
365  * \param rs_ctx    Restart context
366  * \param ops       Number of basic ops to do
367  *
368  * \return          \c 0 if doing \p ops basic ops is still allowed,
369  * \return          #MBEDTLS_ERR_ECP_IN_PROGRESS otherwise.
370  */
371 int mbedtls_ecp_check_budget( const mbedtls_ecp_group *grp,
372                               mbedtls_ecp_restart_ctx *rs_ctx,
373                               unsigned ops );
374 
375 /* Utility macro for checking and updating ops budget */
376 #define MBEDTLS_ECP_BUDGET( ops )   \
377     MBEDTLS_MPI_CHK( mbedtls_ecp_check_budget( grp, rs_ctx, \
378                                                (unsigned) (ops) ) );
379 
380 #else /* MBEDTLS_ECP_RESTARTABLE */
381 
382 #define MBEDTLS_ECP_BUDGET( ops )   /* no-op; for compatibility */
383 
384 /* We want to declare restartable versions of existing functions anyway */
385 typedef void mbedtls_ecp_restart_ctx;
386 
387 #endif /* MBEDTLS_ECP_RESTARTABLE */
388 
389 /**
390  * \brief    The ECP key-pair structure.
391  *
392  * A generic key-pair that may be used for ECDSA and fixed ECDH, for example.
393  *
394  * \note    Members are deliberately in the same order as in the
395  *          ::mbedtls_ecdsa_context structure.
396  */
397 typedef struct mbedtls_ecp_keypair
398 {
399     mbedtls_ecp_group grp;      /*!<  Elliptic curve and base point     */
400     mbedtls_mpi d;              /*!<  our secret value                  */
401     mbedtls_ecp_point Q;        /*!<  our public value                  */
402 }
403 mbedtls_ecp_keypair;
404 
405 /*
406  * Point formats, from RFC 4492's enum ECPointFormat
407  */
408 #define MBEDTLS_ECP_PF_UNCOMPRESSED    0   /**< Uncompressed point format. */
409 #define MBEDTLS_ECP_PF_COMPRESSED      1   /**< Compressed point format. */
410 
411 /*
412  * Some other constants from RFC 4492
413  */
414 #define MBEDTLS_ECP_TLS_NAMED_CURVE    3   /**< The named_curve of ECCurveType. */
415 
416 #if defined(MBEDTLS_ECP_RESTARTABLE)
417 /**
418  * \brief           Set the maximum number of basic operations done in a row.
419  *
420  *                  If more operations are needed to complete a computation,
421  *                  #MBEDTLS_ERR_ECP_IN_PROGRESS will be returned by the
422  *                  function performing the computation. It is then the
423  *                  caller's responsibility to either call again with the same
424  *                  parameters until it returns 0 or an error code; or to free
425  *                  the restart context if the operation is to be aborted.
426  *
427  *                  It is strictly required that all input parameters and the
428  *                  restart context be the same on successive calls for the
429  *                  same operation, but output parameters need not be the
430  *                  same; they must not be used until the function finally
431  *                  returns 0.
432  *
433  *                  This only applies to functions whose documentation
434  *                  mentions they may return #MBEDTLS_ERR_ECP_IN_PROGRESS (or
435  *                  #MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS for functions in the
436  *                  SSL module). For functions that accept a "restart context"
437  *                  argument, passing NULL disables restart and makes the
438  *                  function equivalent to the function with the same name
439  *                  with \c _restartable removed. For functions in the ECDH
440  *                  module, restart is disabled unless the function accepts
441  *                  an "ECDH context" argument and
442  *                  mbedtls_ecdh_enable_restart() was previously called on
443  *                  that context. For function in the SSL module, restart is
444  *                  only enabled for specific sides and key exchanges
445  *                  (currently only for clients and ECDHE-ECDSA).
446  *
447  * \param max_ops   Maximum number of basic operations done in a row.
448  *                  Default: 0 (unlimited).
449  *                  Lower (non-zero) values mean ECC functions will block for
450  *                  a lesser maximum amount of time.
451  *
452  * \note            A "basic operation" is defined as a rough equivalent of a
453  *                  multiplication in GF(p) for the NIST P-256 curve.
454  *                  As an indication, with default settings, a scalar
455  *                  multiplication (full run of \c mbedtls_ecp_mul()) is:
456  *                  - about 3300 basic operations for P-256
457  *                  - about 9400 basic operations for P-384
458  *
459  * \note            Very low values are not always respected: sometimes
460  *                  functions need to block for a minimum number of
461  *                  operations, and will do so even if max_ops is set to a
462  *                  lower value.  That minimum depends on the curve size, and
463  *                  can be made lower by decreasing the value of
464  *                  \c MBEDTLS_ECP_WINDOW_SIZE.  As an indication, here is the
465  *                  lowest effective value for various curves and values of
466  *                  that parameter (w for short):
467  *                          w=6     w=5     w=4     w=3     w=2
468  *                  P-256   208     208     160     136     124
469  *                  P-384   682     416     320     272     248
470  *                  P-521  1364     832     640     544     496
471  *
472  * \note            This setting is currently ignored by Curve25519.
473  */
474 void mbedtls_ecp_set_max_ops( unsigned max_ops );
475 
476 /**
477  * \brief           Check if restart is enabled (max_ops != 0)
478  *
479  * \return          \c 0 if \c max_ops == 0 (restart disabled)
480  * \return          \c 1 otherwise (restart enabled)
481  */
482 int mbedtls_ecp_restart_is_enabled( void );
483 #endif /* MBEDTLS_ECP_RESTARTABLE */
484 
485 /**
486  * \brief           This function retrieves the information defined in
487  *                  mbedtls_ecp_curve_info() for all supported curves in order
488  *                  of preference.
489  *
490  * \return          A statically allocated array. The last entry is 0.
491  */
492 const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list( void );
493 
494 /**
495  * \brief           This function retrieves the list of internal group
496  *                  identifiers of all supported curves in the order of
497  *                  preference.
498  *
499  * \return          A statically allocated array,
500  *                  terminated with MBEDTLS_ECP_DP_NONE.
501  */
502 const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list( void );
503 
504 /**
505  * \brief           This function retrieves curve information from an internal
506  *                  group identifier.
507  *
508  * \param grp_id    An \c MBEDTLS_ECP_DP_XXX value.
509  *
510  * \return          The associated curve information on success.
511  * \return          NULL on failure.
512  */
513 const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_grp_id( mbedtls_ecp_group_id grp_id );
514 
515 /**
516  * \brief           This function retrieves curve information from a TLS
517  *                  NamedCurve value.
518  *
519  * \param tls_id    An \c MBEDTLS_ECP_DP_XXX value.
520  *
521  * \return          The associated curve information on success.
522  * \return          NULL on failure.
523  */
524 const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id( uint16_t tls_id );
525 
526 /**
527  * \brief           This function retrieves curve information from a
528  *                  human-readable name.
529  *
530  * \param name      The human-readable name.
531  *
532  * \return          The associated curve information on success.
533  * \return          NULL on failure.
534  */
535 const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name( const char *name );
536 
537 /**
538  * \brief           This function initializes a point as zero.
539  *
540  * \param pt        The point to initialize.
541  */
542 void mbedtls_ecp_point_init( mbedtls_ecp_point *pt );
543 
544 /**
545  * \brief           This function initializes an ECP group context
546  *                  without loading any domain parameters.
547  *
548  * \note            After this function is called, domain parameters
549  *                  for various ECP groups can be loaded through the
550  *                  mbedtls_ecp_group_load() or mbedtls_ecp_tls_read_group()
551  *                  functions.
552  */
553 void mbedtls_ecp_group_init( mbedtls_ecp_group *grp );
554 
555 /**
556  * \brief           This function initializes a key pair as an invalid one.
557  *
558  * \param key       The key pair to initialize.
559  */
560 void mbedtls_ecp_keypair_init( mbedtls_ecp_keypair *key );
561 
562 /**
563  * \brief           This function frees the components of a point.
564  *
565  * \param pt        The point to free.
566  */
567 void mbedtls_ecp_point_free( mbedtls_ecp_point *pt );
568 
569 /**
570  * \brief           This function frees the components of an ECP group.
571  *
572  * \param grp       The group to free. This may be \c NULL, in which
573  *                  case this function returns immediately. If it is not
574  *                  \c NULL, it must point to an initialized ECP group.
575  */
576 void mbedtls_ecp_group_free( mbedtls_ecp_group *grp );
577 
578 /**
579  * \brief           This function frees the components of a key pair.
580  *
581  * \param key       The key pair to free. This may be \c NULL, in which
582  *                  case this function returns immediately. If it is not
583  *                  \c NULL, it must point to an initialized ECP key pair.
584  */
585 void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair *key );
586 
587 #if defined(MBEDTLS_ECP_RESTARTABLE)
588 /**
589  * \brief           Initialize a restart context.
590  *
591  * \param ctx       The restart context to initialize. This must
592  *                  not be \c NULL.
593  */
594 void mbedtls_ecp_restart_init( mbedtls_ecp_restart_ctx *ctx );
595 
596 /**
597  * \brief           Free the components of a restart context.
598  *
599  * \param ctx       The restart context to free. This may be \c NULL, in which
600  *                  case this function returns immediately. If it is not
601  *                  \c NULL, it must point to an initialized restart context.
602  */
603 void mbedtls_ecp_restart_free( mbedtls_ecp_restart_ctx *ctx );
604 #endif /* MBEDTLS_ECP_RESTARTABLE */
605 
606 /**
607  * \brief           This function copies the contents of point \p Q into
608  *                  point \p P.
609  *
610  * \param P         The destination point. This must be initialized.
611  * \param Q         The source point. This must be initialized.
612  *
613  * \return          \c 0 on success.
614  * \return          #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
615  * \return          Another negative error code for other kinds of failure.
616  */
617 int mbedtls_ecp_copy( mbedtls_ecp_point *P, const mbedtls_ecp_point *Q );
618 
619 /**
620  * \brief           This function copies the contents of group \p src into
621  *                  group \p dst.
622  *
623  * \param dst       The destination group. This must be initialized.
624  * \param src       The source group. This must be initialized.
625  *
626  * \return          \c 0 on success.
627  * \return          #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
628  * \return          Another negative error code on other kinds of failure.
629  */
630 int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst,
631                             const mbedtls_ecp_group *src );
632 
633 /**
634  * \brief           This function sets a point to the point at infinity.
635  *
636  * \param pt        The point to set. This must be initialized.
637  *
638  * \return          \c 0 on success.
639  * \return          #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
640  * \return          Another negative error code on other kinds of failure.
641  */
642 int mbedtls_ecp_set_zero( mbedtls_ecp_point *pt );
643 
644 /**
645  * \brief           This function checks if a point is the point at infinity.
646  *
647  * \param pt        The point to test. This must be initialized.
648  *
649  * \return          \c 1 if the point is zero.
650  * \return          \c 0 if the point is non-zero.
651  * \return          A negative error code on failure.
652  */
653 int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt );
654 
655 /**
656  * \brief           This function compares two points.
657  *
658  * \note            This assumes that the points are normalized. Otherwise,
659  *                  they may compare as "not equal" even if they are.
660  *
661  * \param P         The first point to compare. This must be initialized.
662  * \param Q         The second point to compare. This must be initialized.
663  *
664  * \return          \c 0 if the points are equal.
665  * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the points are not equal.
666  */
667 int mbedtls_ecp_point_cmp( const mbedtls_ecp_point *P,
668                            const mbedtls_ecp_point *Q );
669 
670 /**
671  * \brief           This function imports a non-zero point from two ASCII
672  *                  strings.
673  *
674  * \param P         The destination point. This must be initialized.
675  * \param radix     The numeric base of the input.
676  * \param x         The first affine coordinate, as a null-terminated string.
677  * \param y         The second affine coordinate, as a null-terminated string.
678  *
679  * \return          \c 0 on success.
680  * \return          An \c MBEDTLS_ERR_MPI_XXX error code on failure.
681  */
682 int mbedtls_ecp_point_read_string( mbedtls_ecp_point *P, int radix,
683                            const char *x, const char *y );
684 
685 /**
686  * \brief           This function exports a point into unsigned binary data.
687  *
688  * \param grp       The group to which the point should belong.
689  *                  This must be initialized and have group parameters
690  *                  set, for example through mbedtls_ecp_group_load().
691  * \param P         The point to export. This must be initialized.
692  * \param format    The point format. This must be either
693  *                  #MBEDTLS_ECP_PF_COMPRESSED or #MBEDTLS_ECP_PF_UNCOMPRESSED.
694  * \param olen      The address at which to store the length of
695  *                  the output in Bytes. This must not be \c NULL.
696  * \param buf       The output buffer. This must be a writable buffer
697  *                  of length \p buflen Bytes.
698  * \param buflen    The length of the output buffer \p buf in Bytes.
699  *
700  * \return          \c 0 on success.
701  * \return          #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the output buffer
702  *                  is too small to hold the point.
703  * \return          Another negative error code on other kinds of failure.
704  */
705 int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp, 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 point format
729  *                  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 /**
942  * \brief           This function performs multiplication and addition of two
943  *                  points by integers: \p R = \p m * \p P + \p n * \p Q
944  *
945  *                  It is not thread-safe to use same group in multiple threads.
946  *
947  * \note            In contrast to mbedtls_ecp_mul(), this function does not
948  *                  guarantee a constant execution flow and timing.
949  *
950  * \param grp       The ECP group to use.
951  *                  This must be initialized and have group parameters
952  *                  set, for example through mbedtls_ecp_group_load().
953  * \param R         The point in which to store the result of the calculation.
954  *                  This must be initialized.
955  * \param m         The integer by which to multiply \p P.
956  *                  This must be initialized.
957  * \param P         The point to multiply by \p m. This must be initialized.
958  * \param n         The integer by which to multiply \p Q.
959  *                  This must be initialized.
960  * \param Q         The point to be multiplied by \p n.
961  *                  This must be initialized.
962  *
963  * \return          \c 0 on success.
964  * \return          #MBEDTLS_ERR_ECP_INVALID_KEY if \p m or \p n are not
965  *                  valid private keys, or \p P or \p Q are not valid public
966  *                  keys.
967  * \return          #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
968  * \return          Another negative error code on other kinds of failure.
969  */
970 int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
971              const mbedtls_mpi *m, const mbedtls_ecp_point *P,
972              const mbedtls_mpi *n, const mbedtls_ecp_point *Q );
973 
974 /**
975  * \brief           This function performs multiplication and addition of two
976  *                  points by integers: \p R = \p m * \p P + \p n * \p Q in a
977  *                  restartable way.
978  *
979  * \see             \c mbedtls_ecp_muladd()
980  *
981  * \note            This function works the same as \c mbedtls_ecp_muladd(),
982  *                  but it can return early and restart according to the limit
983  *                  set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
984  *
985  * \param grp       The ECP group to use.
986  *                  This must be initialized and have group parameters
987  *                  set, for example through mbedtls_ecp_group_load().
988  * \param R         The point in which to store the result of the calculation.
989  *                  This must be initialized.
990  * \param m         The integer by which to multiply \p P.
991  *                  This must be initialized.
992  * \param P         The point to multiply by \p m. This must be initialized.
993  * \param n         The integer by which to multiply \p Q.
994  *                  This must be initialized.
995  * \param Q         The point to be multiplied by \p n.
996  *                  This must be initialized.
997  * \param rs_ctx    The restart context (NULL disables restart).
998  *
999  * \return          \c 0 on success.
1000  * \return          #MBEDTLS_ERR_ECP_INVALID_KEY if \p m or \p n are not
1001  *                  valid private keys, or \p P or \p Q are not valid public
1002  *                  keys.
1003  * \return          #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
1004  * \return          #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
1005  *                  operations was reached: see \c mbedtls_ecp_set_max_ops().
1006  * \return          Another negative error code on other kinds of failure.
1007  */
1008 int mbedtls_ecp_muladd_restartable(
1009              mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
1010              const mbedtls_mpi *m, const mbedtls_ecp_point *P,
1011              const mbedtls_mpi *n, const mbedtls_ecp_point *Q,
1012              mbedtls_ecp_restart_ctx *rs_ctx );
1013 
1014 /**
1015  * \brief           This function checks that a point is a valid public key
1016  *                  on this curve.
1017  *
1018  *                  It only checks that the point is non-zero, has
1019  *                  valid coordinates and lies on the curve. It does not verify
1020  *                  that it is indeed a multiple of \p G. This additional
1021  *                  check is computationally more expensive, is not required
1022  *                  by standards, and should not be necessary if the group
1023  *                  used has a small cofactor. In particular, it is useless for
1024  *                  the NIST groups which all have a cofactor of 1.
1025  *
1026  * \note            This function uses bare components rather than an
1027  *                  ::mbedtls_ecp_keypair structure, to ease use with other
1028  *                  structures, such as ::mbedtls_ecdh_context or
1029  *                  ::mbedtls_ecdsa_context.
1030  *
1031  * \param grp       The ECP group the point should belong to.
1032  *                  This must be initialized and have group parameters
1033  *                  set, for example through mbedtls_ecp_group_load().
1034  * \param pt        The point to check. This must be initialized.
1035  *
1036  * \return          \c 0 if the point is a valid public key.
1037  * \return          #MBEDTLS_ERR_ECP_INVALID_KEY if the point is not
1038  *                  a valid public key for the given curve.
1039  * \return          Another negative error code on other kinds of failure.
1040  */
1041 int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp,
1042                               const mbedtls_ecp_point *pt );
1043 
1044 /**
1045  * \brief           This function checks that an \p mbedtls_mpi is a
1046  *                  valid private key for this curve.
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 the private key should belong to.
1054  *                  This must be initialized and have group parameters
1055  *                  set, for example through mbedtls_ecp_group_load().
1056  * \param d         The integer to check. This must be initialized.
1057  *
1058  * \return          \c 0 if the point is a valid private key.
1059  * \return          #MBEDTLS_ERR_ECP_INVALID_KEY if the point is not a valid
1060  *                  private key for the given curve.
1061  * \return          Another negative error code on other kinds of failure.
1062  */
1063 int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp,
1064                                const mbedtls_mpi *d );
1065 
1066 /**
1067  * \brief           This function generates a private key.
1068  *
1069  * \param grp       The ECP group to generate a private key for.
1070  *                  This must be initialized and have group parameters
1071  *                  set, for example through mbedtls_ecp_group_load().
1072  * \param d         The destination MPI (secret part). This must be initialized.
1073  * \param f_rng     The RNG function. This must not be \c NULL.
1074  * \param p_rng     The RNG parameter to be passed to \p f_rng. This may be
1075  *                  \c NULL if \p f_rng doesn't need a context argument.
1076  *
1077  * \return          \c 0 on success.
1078  * \return          An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
1079  *                  on failure.
1080  */
1081 int mbedtls_ecp_gen_privkey( const mbedtls_ecp_group *grp,
1082                      mbedtls_mpi *d,
1083                      int (*f_rng)(void *, unsigned char *, size_t),
1084                      void *p_rng );
1085 
1086 /**
1087  * \brief           This function generates a keypair with a configurable base
1088  *                  point.
1089  *
1090  * \note            This function uses bare components rather than an
1091  *                  ::mbedtls_ecp_keypair structure to ease use with other
1092  *                  structures, such as ::mbedtls_ecdh_context or
1093  *                  ::mbedtls_ecdsa_context.
1094  *
1095  * \param grp       The ECP group to generate a key pair for.
1096  *                  This must be initialized and have group parameters
1097  *                  set, for example through mbedtls_ecp_group_load().
1098  * \param G         The base point to use. This must be initialized
1099  *                  and belong to \p grp. It replaces the default base
1100  *                  point \c grp->G used by mbedtls_ecp_gen_keypair().
1101  * \param d         The destination MPI (secret part).
1102  *                  This must be initialized.
1103  * \param Q         The destination point (public part).
1104  *                  This must be initialized.
1105  * \param f_rng     The RNG function. This must not be \c NULL.
1106  * \param p_rng     The RNG context to be passed to \p f_rng. This may
1107  *                  be \c NULL if \p f_rng doesn't need a context argument.
1108  *
1109  * \return          \c 0 on success.
1110  * \return          An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
1111  *                  on failure.
1112  */
1113 int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp,
1114                                   const mbedtls_ecp_point *G,
1115                                   mbedtls_mpi *d, mbedtls_ecp_point *Q,
1116                                   int (*f_rng)(void *, unsigned char *, size_t),
1117                                   void *p_rng );
1118 
1119 /**
1120  * \brief           This function generates an ECP keypair.
1121  *
1122  * \note            This function uses bare components rather than an
1123  *                  ::mbedtls_ecp_keypair structure to ease use with other
1124  *                  structures, such as ::mbedtls_ecdh_context or
1125  *                  ::mbedtls_ecdsa_context.
1126  *
1127  * \param grp       The ECP group to generate a key pair for.
1128  *                  This must be initialized and have group parameters
1129  *                  set, for example through mbedtls_ecp_group_load().
1130  * \param d         The destination MPI (secret part).
1131  *                  This must be initialized.
1132  * \param Q         The destination point (public part).
1133  *                  This must be initialized.
1134  * \param f_rng     The RNG function. This must not be \c NULL.
1135  * \param p_rng     The RNG context to be passed to \p f_rng. This may
1136  *                  be \c NULL if \p f_rng doesn't need a context argument.
1137  *
1138  * \return          \c 0 on success.
1139  * \return          An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
1140  *                  on failure.
1141  */
1142 int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp, mbedtls_mpi *d,
1143                              mbedtls_ecp_point *Q,
1144                              int (*f_rng)(void *, unsigned char *, size_t),
1145                              void *p_rng );
1146 
1147 /**
1148  * \brief           This function generates an ECP key.
1149  *
1150  * \param grp_id    The ECP group identifier.
1151  * \param key       The destination key. This must be initialized.
1152  * \param f_rng     The RNG function to use. This must not be \c NULL.
1153  * \param p_rng     The RNG context to be passed to \p f_rng. This may
1154  *                  be \c NULL if \p f_rng doesn't need a context argument.
1155  *
1156  * \return          \c 0 on success.
1157  * \return          An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
1158  *                  on failure.
1159  */
1160 int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
1161                          int (*f_rng)(void *, unsigned char *, size_t),
1162                          void *p_rng );
1163 
1164 /**
1165  * \brief           This function checks that the keypair objects
1166  *                  \p pub and \p prv have the same group and the
1167  *                  same public point, and that the private key in
1168  *                  \p prv is consistent with the public key.
1169  *
1170  * \param pub       The keypair structure holding the public key. This
1171  *                  must be initialized. If it contains a private key, that
1172  *                  part is ignored.
1173  * \param prv       The keypair structure holding the full keypair.
1174  *                  This must be initialized.
1175  *
1176  * \return          \c 0 on success, meaning that the keys are valid and match.
1177  * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the keys are invalid or do not match.
1178  * \return          An \c MBEDTLS_ERR_ECP_XXX or an \c MBEDTLS_ERR_MPI_XXX
1179  *                  error code on calculation failure.
1180  */
1181 int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub,
1182                                 const mbedtls_ecp_keypair *prv );
1183 
1184 #if defined(MBEDTLS_SELF_TEST)
1185 
1186 /**
1187  * \brief          The ECP checkup routine.
1188  *
1189  * \return         \c 0 on success.
1190  * \return         \c 1 on failure.
1191  */
1192 int mbedtls_ecp_self_test( int verbose );
1193 
1194 #endif /* MBEDTLS_SELF_TEST */
1195 
1196 #ifdef __cplusplus
1197 }
1198 #endif
1199 
1200 #endif /* ecp.h */
1201