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