1 /**
2  * \file ecp.h
3  *
4  * \brief Elliptic curves over GF(p)
5  *
6  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
7  *  SPDX-License-Identifier: Apache-2.0
8  *
9  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
10  *  not use this file except in compliance with the License.
11  *  You may obtain a copy of the License at
12  *
13  *  http://www.apache.org/licenses/LICENSE-2.0
14  *
15  *  Unless required by applicable law or agreed to in writing, software
16  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  *  See the License for the specific language governing permissions and
19  *  limitations under the License.
20  *
21  *  This file is part of mbed TLS (https://tls.mbed.org)
22  */
23 #ifndef MBEDTLS_ECP_H
24 #define MBEDTLS_ECP_H
25 
26 #include "bignum.h"
27 
28 /*
29  * ECP error codes
30  */
31 #define MBEDTLS_ERR_ECP_BAD_INPUT_DATA                    -0x4F80  /**< Bad input parameters to function. */
32 #define MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL                  -0x4F00  /**< The buffer is too small to write to. */
33 #define MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE               -0x4E80  /**< Requested curve not available. */
34 #define MBEDTLS_ERR_ECP_VERIFY_FAILED                     -0x4E00  /**< The signature is not valid. */
35 #define MBEDTLS_ERR_ECP_ALLOC_FAILED                      -0x4D80  /**< Memory allocation failed. */
36 #define MBEDTLS_ERR_ECP_RANDOM_FAILED                     -0x4D00  /**< Generation of random value, such as (ephemeral) key, failed. */
37 #define MBEDTLS_ERR_ECP_INVALID_KEY                       -0x4C80  /**< Invalid private or public key. */
38 #define MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH                  -0x4C00  /**< Signature is valid but shorter than the user-supplied length. */
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 /**
45  * Domain parameters (curve, subgroup and generator) identifiers.
46  *
47  * Only curves over prime fields are supported.
48  *
49  * \warning This library does not support validation of arbitrary domain
50  * parameters. Therefore, only well-known domain parameters from trusted
51  * sources should be used. See mbedtls_ecp_group_load().
52  */
53 typedef enum
54 {
55     MBEDTLS_ECP_DP_NONE = 0,
56     MBEDTLS_ECP_DP_SECP192R1,      /*!< 192-bits NIST curve  */
57     MBEDTLS_ECP_DP_SECP224R1,      /*!< 224-bits NIST curve  */
58     MBEDTLS_ECP_DP_SECP256R1,      /*!< 256-bits NIST curve  */
59     MBEDTLS_ECP_DP_SECP384R1,      /*!< 384-bits NIST curve  */
60     MBEDTLS_ECP_DP_SECP521R1,      /*!< 521-bits NIST curve  */
61     MBEDTLS_ECP_DP_BP256R1,        /*!< 256-bits Brainpool curve */
62     MBEDTLS_ECP_DP_BP384R1,        /*!< 384-bits Brainpool curve */
63     MBEDTLS_ECP_DP_BP512R1,        /*!< 512-bits Brainpool curve */
64     MBEDTLS_ECP_DP_CURVE25519,           /*!< Curve25519               */
65     MBEDTLS_ECP_DP_SECP192K1,      /*!< 192-bits "Koblitz" curve */
66     MBEDTLS_ECP_DP_SECP224K1,      /*!< 224-bits "Koblitz" curve */
67     MBEDTLS_ECP_DP_SECP256K1,      /*!< 256-bits "Koblitz" curve */
68 } mbedtls_ecp_group_id;
69 
70 /**
71  * Number of supported curves (plus one for NONE).
72  *
73  * (Montgomery curves excluded for now.)
74  */
75 #define MBEDTLS_ECP_DP_MAX     12
76 
77 /**
78  * Curve information for use by other modules
79  */
80 typedef struct
81 {
82     mbedtls_ecp_group_id grp_id;    /*!< Internal identifier        */
83     uint16_t tls_id;                /*!< TLS NamedCurve identifier  */
84     uint16_t bit_size;              /*!< Curve size in bits         */
85     const char *name;               /*!< Human-friendly name        */
86 } mbedtls_ecp_curve_info;
87 
88 /**
89  * \brief           ECP point structure (jacobian coordinates)
90  *
91  * \note            All functions expect and return points satisfying
92  *                  the following condition: Z == 0 or Z == 1. (Other
93  *                  values of Z are used by internal functions only.)
94  *                  The point is zero, or "at infinity", if Z == 0.
95  *                  Otherwise, X and Y are its standard (affine) coordinates.
96  */
97 typedef struct
98 {
99     mbedtls_mpi X;          /*!<  the point's X coordinate  */
100     mbedtls_mpi Y;          /*!<  the point's Y coordinate  */
101     mbedtls_mpi Z;          /*!<  the point's Z coordinate  */
102 }
103 mbedtls_ecp_point;
104 
105 /**
106  * \brief           ECP group structure
107  *
108  * We consider two types of curves equations:
109  * 1. Short Weierstrass y^2 = x^3 + A x + B     mod P   (SEC1 + RFC 4492)
110  * 2. Montgomery,       y^2 = x^3 + A x^2 + x   mod P   (Curve25519 + draft)
111  * In both cases, a generator G for a prime-order subgroup is fixed. In the
112  * short weierstrass, this subgroup is actually the whole curve, and its
113  * cardinal is denoted by N.
114  *
115  * In the case of Short Weierstrass curves, our code requires that N is an odd
116  * prime. (Use odd in mbedtls_ecp_mul() and prime in mbedtls_ecdsa_sign() for blinding.)
117  *
118  * In the case of Montgomery curves, we don't store A but (A + 2) / 4 which is
119  * the quantity actually used in the formulas. Also, nbits is not the size of N
120  * but the required size for private keys.
121  *
122  * If modp is NULL, reduction modulo P is done using a generic algorithm.
123  * Otherwise, it must point to a function that takes an mbedtls_mpi in the range
124  * 0..2^(2*pbits)-1 and transforms it in-place in an integer of little more
125  * than pbits, so that the integer may be efficiently brought in the 0..P-1
126  * range by a few additions or substractions. It must return 0 on success and
127  * non-zero on failure.
128  */
129 typedef struct
130 {
131     mbedtls_ecp_group_id id;    /*!<  internal group identifier                     */
132     mbedtls_mpi P;              /*!<  prime modulus of the base field               */
133     mbedtls_mpi A;              /*!<  1. A in the equation, or 2. (A + 2) / 4       */
134     mbedtls_mpi B;              /*!<  1. B in the equation, or 2. unused            */
135     mbedtls_ecp_point G;        /*!<  generator of the (sub)group used              */
136     mbedtls_mpi N;              /*!<  1. the order of G, or 2. unused               */
137     size_t pbits;       /*!<  number of bits in P                           */
138     size_t nbits;       /*!<  number of bits in 1. P, or 2. private keys    */
139     unsigned int h;     /*!<  internal: 1 if the constants are static       */
140     int (*modp)(mbedtls_mpi *); /*!<  function for fast reduction mod P             */
141     int (*t_pre)(mbedtls_ecp_point *, void *);  /*!< unused                         */
142     int (*t_post)(mbedtls_ecp_point *, void *); /*!< unused                         */
143     void *t_data;                       /*!< unused                         */
144     mbedtls_ecp_point *T;       /*!<  pre-computed points for ecp_mul_comb()        */
145     size_t T_size;      /*!<  number for pre-computed points                */
146 }
147 mbedtls_ecp_group;
148 
149 /**
150  * \brief           ECP key pair structure
151  *
152  * A generic key pair that could be used for ECDSA, fixed ECDH, etc.
153  *
154  * \note Members purposefully in the same order as struc mbedtls_ecdsa_context.
155  */
156 typedef struct
157 {
158     mbedtls_ecp_group grp;      /*!<  Elliptic curve and base point     */
159     mbedtls_mpi d;              /*!<  our secret value                  */
160     mbedtls_ecp_point Q;        /*!<  our public value                  */
161 }
162 mbedtls_ecp_keypair;
163 
164 /**
165  * \name SECTION: Module settings
166  *
167  * The configuration options you can set for this module are in this section.
168  * Either change them in config.h or define them on the compiler command line.
169  * \{
170  */
171 
172 #if !defined(MBEDTLS_ECP_MAX_BITS)
173 /**
174  * Maximum size of the groups (that is, of N and P)
175  */
176 #define MBEDTLS_ECP_MAX_BITS     521   /**< Maximum bit size of groups */
177 #endif
178 
179 #define MBEDTLS_ECP_MAX_BYTES    ( ( MBEDTLS_ECP_MAX_BITS + 7 ) / 8 )
180 #define MBEDTLS_ECP_MAX_PT_LEN   ( 2 * MBEDTLS_ECP_MAX_BYTES + 1 )
181 
182 #if !defined(MBEDTLS_ECP_WINDOW_SIZE)
183 /*
184  * Maximum "window" size used for point multiplication.
185  * Default: 6.
186  * Minimum value: 2. Maximum value: 7.
187  *
188  * Result is an array of at most ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) )
189  * points used for point multiplication. This value is directly tied to EC
190  * peak memory usage, so decreasing it by one should roughly cut memory usage
191  * by two (if large curves are in use).
192  *
193  * Reduction in size may reduce speed, but larger curves are impacted first.
194  * Sample performances (in ECDHE handshakes/s, with FIXED_POINT_OPTIM = 1):
195  *      w-size:     6       5       4       3       2
196  *      521       145     141     135     120      97
197  *      384       214     209     198     177     146
198  *      256       320     320     303     262     226
199 
200  *      224       475     475     453     398     342
201  *      192       640     640     633     587     476
202  */
203 #define MBEDTLS_ECP_WINDOW_SIZE    6   /**< Maximum window size used */
204 #endif /* MBEDTLS_ECP_WINDOW_SIZE */
205 
206 #if !defined(MBEDTLS_ECP_FIXED_POINT_OPTIM)
207 /*
208  * Trade memory for speed on fixed-point multiplication.
209  *
210  * This speeds up repeated multiplication of the generator (that is, the
211  * multiplication in ECDSA signatures, and half of the multiplications in
212  * ECDSA verification and ECDHE) by a factor roughly 3 to 4.
213  *
214  * The cost is increasing EC peak memory usage by a factor roughly 2.
215  *
216  * Change this value to 0 to reduce peak memory usage.
217  */
218 #define MBEDTLS_ECP_FIXED_POINT_OPTIM  1   /**< Enable fixed-point speed-up */
219 #endif /* MBEDTLS_ECP_FIXED_POINT_OPTIM */
220 
221 /* \} name SECTION: Module settings */
222 
223 /*
224  * Point formats, from RFC 4492's enum ECPointFormat
225  */
226 #define MBEDTLS_ECP_PF_UNCOMPRESSED    0   /**< Uncompressed point format */
227 #define MBEDTLS_ECP_PF_COMPRESSED      1   /**< Compressed point format */
228 
229 /*
230  * Some other constants from RFC 4492
231  */
232 #define MBEDTLS_ECP_TLS_NAMED_CURVE    3   /**< ECCurveType's named_curve */
233 
234 /**
235  * \brief           Get the list of supported curves in order of preferrence
236  *                  (full information)
237  *
238  * \return          A statically allocated array, the last entry is 0.
239  */
240 const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list( void );
241 
242 /**
243  * \brief           Get the list of supported curves in order of preferrence
244  *                  (grp_id only)
245  *
246  * \return          A statically allocated array,
247  *                  terminated with MBEDTLS_ECP_DP_NONE.
248  */
249 const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list( void );
250 
251 /**
252  * \brief           Get curve information from an internal group identifier
253  *
254  * \param grp_id    A MBEDTLS_ECP_DP_XXX value
255  *
256  * \return          The associated curve information or NULL
257  */
258 const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_grp_id( mbedtls_ecp_group_id grp_id );
259 
260 /**
261  * \brief           Get curve information from a TLS NamedCurve value
262  *
263  * \param tls_id    A MBEDTLS_ECP_DP_XXX value
264  *
265  * \return          The associated curve information or NULL
266  */
267 const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id( uint16_t tls_id );
268 
269 /**
270  * \brief           Get curve information from a human-readable name
271  *
272  * \param name      The name
273  *
274  * \return          The associated curve information or NULL
275  */
276 const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name( const char *name );
277 
278 /**
279  * \brief           Initialize a point (as zero)
280  */
281 void mbedtls_ecp_point_init( mbedtls_ecp_point *pt );
282 
283 /**
284  * \brief           Initialize a group (to something meaningless)
285  */
286 void mbedtls_ecp_group_init( mbedtls_ecp_group *grp );
287 
288 /**
289  * \brief           Initialize a key pair (as an invalid one)
290  */
291 void mbedtls_ecp_keypair_init( mbedtls_ecp_keypair *key );
292 
293 /**
294  * \brief           Free the components of a point
295  */
296 void mbedtls_ecp_point_free( mbedtls_ecp_point *pt );
297 
298 /**
299  * \brief           Free the components of an ECP group
300  */
301 void mbedtls_ecp_group_free( mbedtls_ecp_group *grp );
302 
303 /**
304  * \brief           Free the components of a key pair
305  */
306 void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair *key );
307 
308 /**
309  * \brief           Copy the contents of point Q into P
310  *
311  * \param P         Destination point
312  * \param Q         Source point
313  *
314  * \return          0 if successful,
315  *                  MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
316  */
317 int mbedtls_ecp_copy( mbedtls_ecp_point *P, const mbedtls_ecp_point *Q );
318 
319 /**
320  * \brief           Copy the contents of a group object
321  *
322  * \param dst       Destination group
323  * \param src       Source group
324  *
325  * \return          0 if successful,
326  *                  MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
327  */
328 int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst, const mbedtls_ecp_group *src );
329 
330 /**
331  * \brief           Set a point to zero
332  *
333  * \param pt        Destination point
334  *
335  * \return          0 if successful,
336  *                  MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
337  */
338 int mbedtls_ecp_set_zero( mbedtls_ecp_point *pt );
339 
340 /**
341  * \brief           Tell if a point is zero
342  *
343  * \param pt        Point to test
344  *
345  * \return          1 if point is zero, 0 otherwise
346  */
347 int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt );
348 
349 /**
350  * \brief           Compare two points
351  *
352  * \note            This assumes the points are normalized. Otherwise,
353  *                  they may compare as "not equal" even if they are.
354  *
355  * \param P         First point to compare
356  * \param Q         Second point to compare
357  *
358  * \return          0 if the points are equal,
359  *                  MBEDTLS_ERR_ECP_BAD_INPUT_DATA otherwise
360  */
361 int mbedtls_ecp_point_cmp( const mbedtls_ecp_point *P,
362                            const mbedtls_ecp_point *Q );
363 
364 /**
365  * \brief           Import a non-zero point from two ASCII strings
366  *
367  * \param P         Destination point
368  * \param radix     Input numeric base
369  * \param x         First affine coordinate as a null-terminated string
370  * \param y         Second affine coordinate as a null-terminated string
371  *
372  * \return          0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
373  */
374 int mbedtls_ecp_point_read_string( mbedtls_ecp_point *P, int radix,
375                            const char *x, const char *y );
376 
377 /**
378  * \brief           Export a point into unsigned binary data
379  *
380  * \param grp       Group to which the point should belong
381  * \param P         Point to export
382  * \param format    Point format, should be a MBEDTLS_ECP_PF_XXX macro
383  * \param olen      Length of the actual output
384  * \param buf       Output buffer
385  * \param buflen    Length of the output buffer
386  *
387  * \return          0 if successful,
388  *                  or MBEDTLS_ERR_ECP_BAD_INPUT_DATA
389  *                  or MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL
390  */
391 int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *P,
392                             int format, size_t *olen,
393                             unsigned char *buf, size_t buflen );
394 
395 /**
396  * \brief           Import a point from unsigned binary data
397  *
398  * \param grp       Group to which the point should belong
399  * \param P         Point to import
400  * \param buf       Input buffer
401  * \param ilen      Actual length of input
402  *
403  * \return          0 if successful,
404  *                  MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid,
405  *                  MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
406  *                  MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the point format
407  *                  is not implemented.
408  *
409  * \note            This function does NOT check that the point actually
410  *                  belongs to the given group, see mbedtls_ecp_check_pubkey() for
411  *                  that.
412  */
413 int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P,
414                            const unsigned char *buf, size_t ilen );
415 
416 /**
417  * \brief           Import a point from a TLS ECPoint record
418  *
419  * \param grp       ECP group used
420  * \param pt        Destination point
421  * \param buf       $(Start of input buffer)
422  * \param len       Buffer length
423  *
424  * \note            buf is updated to point right after the ECPoint on exit
425  *
426  * \return          0 if successful,
427  *                  MBEDTLS_ERR_MPI_XXX if initialization failed
428  *                  MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid
429  */
430 int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
431                         const unsigned char **buf, size_t len );
432 
433 /**
434  * \brief           Export a point as a TLS ECPoint record
435  *
436  * \param grp       ECP group used
437  * \param pt        Point to export
438  * \param format    Export format
439  * \param olen      length of data written
440  * \param buf       Buffer to write to
441  * \param blen      Buffer length
442  *
443  * \return          0 if successful,
444  *                  or MBEDTLS_ERR_ECP_BAD_INPUT_DATA
445  *                  or MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL
446  */
447 int mbedtls_ecp_tls_write_point( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt,
448                          int format, size_t *olen,
449                          unsigned char *buf, size_t blen );
450 
451 /**
452  * \brief           Set a group using well-known domain parameters
453  *
454  * \param grp       Destination group
455  * \param index     Index in the list of well-known domain parameters
456  *
457  * \return          0 if successful,
458  *                  MBEDTLS_ERR_MPI_XXX if initialization failed
459  *                  MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE for unkownn groups
460  *
461  * \note            Index should be a value of RFC 4492's enum NamedCurve,
462  *                  usually in the form of a MBEDTLS_ECP_DP_XXX macro.
463  */
464 int mbedtls_ecp_group_load( mbedtls_ecp_group *grp, mbedtls_ecp_group_id index );
465 
466 /**
467  * \brief           Set a group from a TLS ECParameters record
468  *
469  * \param grp       Destination group
470  * \param buf       &(Start of input buffer)
471  * \param len       Buffer length
472  *
473  * \note            buf is updated to point right after ECParameters on exit
474  *
475  * \return          0 if successful,
476  *                  MBEDTLS_ERR_MPI_XXX if initialization failed
477  *                  MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid
478  */
479 int mbedtls_ecp_tls_read_group( mbedtls_ecp_group *grp, const unsigned char **buf, size_t len );
480 
481 /**
482  * \brief           Write the TLS ECParameters record for a group
483  *
484  * \param grp       ECP group used
485  * \param olen      Number of bytes actually written
486  * \param buf       Buffer to write to
487  * \param blen      Buffer length
488  *
489  * \return          0 if successful,
490  *                  or MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL
491  */
492 int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, size_t *olen,
493                          unsigned char *buf, size_t blen );
494 
495 /**
496  * \brief           Multiplication by an integer: R = m * P
497  *                  (Not thread-safe to use same group in multiple threads)
498  *
499  * \note            In order to prevent timing attacks, this function
500  *                  executes the exact same sequence of (base field)
501  *                  operations for any valid m. It avoids any if-branch or
502  *                  array index depending on the value of m.
503  *
504  * \note            If f_rng is not NULL, it is used to randomize intermediate
505  *                  results in order to prevent potential timing attacks
506  *                  targeting these results. It is recommended to always
507  *                  provide a non-NULL f_rng (the overhead is negligible).
508  *
509  * \param grp       ECP group
510  * \param R         Destination point
511  * \param m         Integer by which to multiply
512  * \param P         Point to multiply
513  * \param f_rng     RNG function (see notes)
514  * \param p_rng     RNG parameter
515  *
516  * \return          0 if successful,
517  *                  MBEDTLS_ERR_ECP_INVALID_KEY if m is not a valid privkey
518  *                  or P is not a valid pubkey,
519  *                  MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
520  */
521 int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
522              const mbedtls_mpi *m, const mbedtls_ecp_point *P,
523              int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
524 
525 /**
526  * \brief           Multiplication and addition of two points by integers:
527  *                  R = m * P + n * Q
528  *                  (Not thread-safe to use same group in multiple threads)
529  *
530  * \note            In contrast to mbedtls_ecp_mul(), this function does not guarantee
531  *                  a constant execution flow and timing.
532  *
533  * \param grp       ECP group
534  * \param R         Destination point
535  * \param m         Integer by which to multiply P
536  * \param P         Point to multiply by m
537  * \param n         Integer by which to multiply Q
538  * \param Q         Point to be multiplied by n
539  *
540  * \return          0 if successful,
541  *                  MBEDTLS_ERR_ECP_INVALID_KEY if m or n is not a valid privkey
542  *                  or P or Q is not a valid pubkey,
543  *                  MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
544  */
545 int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
546              const mbedtls_mpi *m, const mbedtls_ecp_point *P,
547              const mbedtls_mpi *n, const mbedtls_ecp_point *Q );
548 
549 /**
550  * \brief           Check that a point is a valid public key on this curve
551  *
552  * \param grp       Curve/group the point should belong to
553  * \param pt        Point to check
554  *
555  * \return          0 if point is a valid public key,
556  *                  MBEDTLS_ERR_ECP_INVALID_KEY otherwise.
557  *
558  * \note            This function only checks the point is non-zero, has valid
559  *                  coordinates and lies on the curve, but not that it is
560  *                  indeed a multiple of G. This is additional check is more
561  *                  expensive, isn't required by standards, and shouldn't be
562  *                  necessary if the group used has a small cofactor. In
563  *                  particular, it is useless for the NIST groups which all
564  *                  have a cofactor of 1.
565  *
566  * \note            Uses bare components rather than an mbedtls_ecp_keypair structure
567  *                  in order to ease use with other structures such as
568  *                  mbedtls_ecdh_context of mbedtls_ecdsa_context.
569  */
570 int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt );
571 
572 /**
573  * \brief           Check that an mbedtls_mpi is a valid private key for this curve
574  *
575  * \param grp       Group used
576  * \param d         Integer to check
577  *
578  * \return          0 if point is a valid private key,
579  *                  MBEDTLS_ERR_ECP_INVALID_KEY otherwise.
580  *
581  * \note            Uses bare components rather than an mbedtls_ecp_keypair structure
582  *                  in order to ease use with other structures such as
583  *                  mbedtls_ecdh_context of mbedtls_ecdsa_context.
584  */
585 int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp, const mbedtls_mpi *d );
586 
587 /**
588  * \brief           Generate a keypair with configurable base point
589  *
590  * \param grp       ECP group
591  * \param G         Chosen base point
592  * \param d         Destination MPI (secret part)
593  * \param Q         Destination point (public part)
594  * \param f_rng     RNG function
595  * \param p_rng     RNG parameter
596  *
597  * \return          0 if successful,
598  *                  or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
599  *
600  * \note            Uses bare components rather than an mbedtls_ecp_keypair structure
601  *                  in order to ease use with other structures such as
602  *                  mbedtls_ecdh_context of mbedtls_ecdsa_context.
603  */
604 int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp,
605                      const mbedtls_ecp_point *G,
606                      mbedtls_mpi *d, mbedtls_ecp_point *Q,
607                      int (*f_rng)(void *, unsigned char *, size_t),
608                      void *p_rng );
609 
610 /**
611  * \brief           Generate a keypair
612  *
613  * \param grp       ECP group
614  * \param d         Destination MPI (secret part)
615  * \param Q         Destination point (public part)
616  * \param f_rng     RNG function
617  * \param p_rng     RNG parameter
618  *
619  * \return          0 if successful,
620  *                  or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
621  *
622  * \note            Uses bare components rather than an mbedtls_ecp_keypair structure
623  *                  in order to ease use with other structures such as
624  *                  mbedtls_ecdh_context of mbedtls_ecdsa_context.
625  */
626 int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
627                      int (*f_rng)(void *, unsigned char *, size_t),
628                      void *p_rng );
629 
630 /**
631  * \brief           Generate a keypair
632  *
633  * \param grp_id    ECP group identifier
634  * \param key       Destination keypair
635  * \param f_rng     RNG function
636  * \param p_rng     RNG parameter
637  *
638  * \return          0 if successful,
639  *                  or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
640  */
641 int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
642                 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
643 
644 /**
645  * \brief           Check a public-private key pair
646  *
647  * \param pub       Keypair structure holding a public key
648  * \param prv       Keypair structure holding a private (plus public) key
649  *
650  * \return          0 if successful (keys are valid and match), or
651  *                  MBEDTLS_ERR_ECP_BAD_INPUT_DATA, or
652  *                  a MBEDTLS_ERR_ECP_XXX or MBEDTLS_ERR_MPI_XXX code.
653  */
654 int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv );
655 
656 #if defined(MBEDTLS_SELF_TEST)
657 /**
658  * \brief          Checkup routine
659  *
660  * \return         0 if successful, or 1 if a test failed
661  */
662 int mbedtls_ecp_self_test( int verbose );
663 #endif
664 
665 #ifdef __cplusplus
666 }
667 #endif
668 
669 #endif /* ecp.h */
670