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