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