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