mbed TLS v2.3.0
ecp.h
Go to the documentation of this file.
1 
25 #ifndef MBEDTLS_ECP_H
26 #define MBEDTLS_ECP_H
27 
28 #include "bignum.h"
29 
30 /*
31  * ECP error codes
32  */
33 #define MBEDTLS_ERR_ECP_BAD_INPUT_DATA -0x4F80
34 #define MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL -0x4F00
35 #define MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE -0x4E80
36 #define MBEDTLS_ERR_ECP_VERIFY_FAILED -0x4E00
37 #define MBEDTLS_ERR_ECP_ALLOC_FAILED -0x4D80
38 #define MBEDTLS_ERR_ECP_RANDOM_FAILED -0x4D00
39 #define MBEDTLS_ERR_ECP_INVALID_KEY -0x4C80
40 #define MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH -0x4C00
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
55 typedef enum
56 {
71 
77 #define MBEDTLS_ECP_DP_MAX 12
78 
82 typedef struct
83 {
85  uint16_t tls_id;
86  uint16_t bit_size;
87  const char *name;
89 
99 typedef struct
100 {
104 }
106 
131 typedef struct
132 {
139  size_t pbits;
140  size_t nbits;
141  unsigned int h;
142  int (*modp)(mbedtls_mpi *);
143  int (*t_pre)(mbedtls_ecp_point *, void *);
144  int (*t_post)(mbedtls_ecp_point *, void *);
145  void *t_data;
147  size_t T_size;
148 }
150 
158 typedef struct
159 {
163 }
165 
174 #if !defined(MBEDTLS_ECP_MAX_BITS)
175 
178 #define MBEDTLS_ECP_MAX_BITS 521
179 #endif
180 
181 #define MBEDTLS_ECP_MAX_BYTES ( ( MBEDTLS_ECP_MAX_BITS + 7 ) / 8 )
182 #define MBEDTLS_ECP_MAX_PT_LEN ( 2 * MBEDTLS_ECP_MAX_BYTES + 1 )
183 
184 #if !defined(MBEDTLS_ECP_WINDOW_SIZE)
185 /*
186  * Maximum "window" size used for point multiplication.
187  * Default: 6.
188  * Minimum value: 2. Maximum value: 7.
189  *
190  * Result is an array of at most ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) )
191  * points used for point multiplication. This value is directly tied to EC
192  * peak memory usage, so decreasing it by one should roughly cut memory usage
193  * by two (if large curves are in use).
194  *
195  * Reduction in size may reduce speed, but larger curves are impacted first.
196  * Sample performances (in ECDHE handshakes/s, with FIXED_POINT_OPTIM = 1):
197  * w-size: 6 5 4 3 2
198  * 521 145 141 135 120 97
199  * 384 214 209 198 177 146
200  * 256 320 320 303 262 226
201 
202  * 224 475 475 453 398 342
203  * 192 640 640 633 587 476
204  */
205 #define MBEDTLS_ECP_WINDOW_SIZE 6
206 #endif /* MBEDTLS_ECP_WINDOW_SIZE */
207 
208 #if !defined(MBEDTLS_ECP_FIXED_POINT_OPTIM)
209 /*
210  * Trade memory for speed on fixed-point multiplication.
211  *
212  * This speeds up repeated multiplication of the generator (that is, the
213  * multiplication in ECDSA signatures, and half of the multiplications in
214  * ECDSA verification and ECDHE) by a factor roughly 3 to 4.
215  *
216  * The cost is increasing EC peak memory usage by a factor roughly 2.
217  *
218  * Change this value to 0 to reduce peak memory usage.
219  */
220 #define MBEDTLS_ECP_FIXED_POINT_OPTIM 1
221 #endif /* MBEDTLS_ECP_FIXED_POINT_OPTIM */
222 
223 /* \} name SECTION: Module settings */
224 
225 /*
226  * Point formats, from RFC 4492's enum ECPointFormat
227  */
228 #define MBEDTLS_ECP_PF_UNCOMPRESSED 0
229 #define MBEDTLS_ECP_PF_COMPRESSED 1
231 /*
232  * Some other constants from RFC 4492
233  */
234 #define MBEDTLS_ECP_TLS_NAMED_CURVE 3
242 const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list( void );
243 
252 
261 
270 
279 
284 
289 
294 
299 
304 
309 
320 
331 
341 
350 
364  const mbedtls_ecp_point *Q );
365 
377  const char *x, const char *y );
378 
394  int format, size_t *olen,
395  unsigned char *buf, size_t buflen );
396 
416  const unsigned char *buf, size_t ilen );
417 
433  const unsigned char **buf, size_t len );
434 
450  int format, size_t *olen,
451  unsigned char *buf, size_t blen );
452 
467 
481 int mbedtls_ecp_tls_read_group( mbedtls_ecp_group *grp, const unsigned char **buf, size_t len );
482 
494 int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, size_t *olen,
495  unsigned char *buf, size_t blen );
496 
524  const mbedtls_mpi *m, const mbedtls_ecp_point *P,
525  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
526 
548  const mbedtls_mpi *m, const mbedtls_ecp_point *P,
549  const mbedtls_mpi *n, const mbedtls_ecp_point *Q );
550 
573 
587 int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp, const mbedtls_mpi *d );
588 
607  const mbedtls_ecp_point *G,
609  int (*f_rng)(void *, unsigned char *, size_t),
610  void *p_rng );
611 
629  int (*f_rng)(void *, unsigned char *, size_t),
630  void *p_rng );
631 
644  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
645 
657 
658 #if defined(MBEDTLS_SELF_TEST)
659 
664 int mbedtls_ecp_self_test( int verbose );
665 #endif
666 
667 #ifdef __cplusplus
668 }
669 #endif
670 
671 #endif /* ecp.h */
uint16_t tls_id
Definition: ecp.h:85
int mbedtls_ecp_is_zero(mbedtls_ecp_point *pt)
Tell if a point is zero.
mbedtls_mpi N
Definition: ecp.h:138
mbedtls_mpi Z
Definition: ecp.h:103
int mbedtls_ecp_muladd(mbedtls_ecp_group *grp, mbedtls_ecp_point *R, const mbedtls_mpi *m, const mbedtls_ecp_point *P, const mbedtls_mpi *n, const mbedtls_ecp_point *Q)
Multiplication and addition of two points by integers: R = m * P + n * Q (Not thread-safe to use same...
int mbedtls_ecp_check_pub_priv(const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv)
Check a public-private key pair.
int mbedtls_ecp_point_read_binary(const mbedtls_ecp_group *grp, mbedtls_ecp_point *P, const unsigned char *buf, size_t ilen)
Import a point from unsigned binary data.
mbedtls_mpi Y
Definition: ecp.h:102
mbedtls_ecp_group grp
Definition: ecp.h:160
int mbedtls_ecp_group_load(mbedtls_ecp_group *grp, mbedtls_ecp_group_id index)
Set a group using well-known domain parameters.
ECP key pair structure.
Definition: ecp.h:158
int mbedtls_ecp_set_zero(mbedtls_ecp_point *pt)
Set a point to zero.
int mbedtls_ecp_copy(mbedtls_ecp_point *P, const mbedtls_ecp_point *Q)
Copy the contents of point Q into P.
const mbedtls_ecp_group_id * mbedtls_ecp_grp_id_list(void)
Get the list of supported curves in order of preferrence (grp_id only)
int mbedtls_ecp_group_copy(mbedtls_ecp_group *dst, const mbedtls_ecp_group *src)
Copy the contents of a group object.
size_t nbits
Definition: ecp.h:140
int mbedtls_ecp_gen_keypair(mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Generate a keypair.
void mbedtls_ecp_point_free(mbedtls_ecp_point *pt)
Free the components of a point.
void mbedtls_ecp_keypair_init(mbedtls_ecp_keypair *key)
Initialize a key pair (as an invalid one)
size_t pbits
Definition: ecp.h:139
mbedtls_mpi X
Definition: ecp.h:101
Curve information for use by other modules.
Definition: ecp.h:82
Multi-precision integer library.
int mbedtls_ecp_gen_key(mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Generate a keypair.
ECP group structure.
Definition: ecp.h:131
int mbedtls_ecp_check_privkey(const mbedtls_ecp_group *grp, const mbedtls_mpi *d)
Check that an mbedtls_mpi is a valid private key for this curve.
mbedtls_ecp_group_id id
Definition: ecp.h:133
const mbedtls_ecp_curve_info * mbedtls_ecp_curve_info_from_grp_id(mbedtls_ecp_group_id grp_id)
Get curve information from an internal group identifier.
int mbedtls_ecp_tls_write_group(const mbedtls_ecp_group *grp, size_t *olen, unsigned char *buf, size_t blen)
Write the TLS ECParameters record for a group.
void mbedtls_ecp_group_free(mbedtls_ecp_group *grp)
Free the components of an ECP group.
int mbedtls_ecp_mul(mbedtls_ecp_group *grp, mbedtls_ecp_point *R, const mbedtls_mpi *m, const mbedtls_ecp_point *P, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Multiplication by an integer: R = m * P (Not thread-safe to use same group in multiple threads) ...
int mbedtls_ecp_check_pubkey(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt)
Check that a point is a valid public key on this curve.
mbedtls_mpi A
Definition: ecp.h:135
mbedtls_mpi P
Definition: ecp.h:134
void mbedtls_ecp_keypair_free(mbedtls_ecp_keypair *key)
Free the components of a key pair.
int mbedtls_ecp_tls_read_group(mbedtls_ecp_group *grp, const unsigned char **buf, size_t len)
Set a group from a TLS ECParameters record.
mbedtls_ecp_point Q
Definition: ecp.h:162
void * t_data
Definition: ecp.h:145
void mbedtls_ecp_point_init(mbedtls_ecp_point *pt)
Initialize a point (as zero)
mbedtls_ecp_group_id
Domain parameters (curve, subgroup and generator) identifiers.
Definition: ecp.h:55
int mbedtls_ecp_point_read_string(mbedtls_ecp_point *P, int radix, const char *x, const char *y)
Import a non-zero point from two ASCII strings.
int mbedtls_ecp_self_test(int verbose)
Checkup routine.
int mbedtls_ecp_point_write_binary(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *P, int format, size_t *olen, unsigned char *buf, size_t buflen)
Export a point into unsigned binary data.
const mbedtls_ecp_curve_info * mbedtls_ecp_curve_info_from_tls_id(uint16_t tls_id)
Get curve information from a TLS NamedCurve value.
int mbedtls_ecp_tls_read_point(const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt, const unsigned char **buf, size_t len)
Import a point from a TLS ECPoint record.
mbedtls_ecp_group_id grp_id
Definition: ecp.h:84
int mbedtls_ecp_gen_keypair_base(mbedtls_ecp_group *grp, const mbedtls_ecp_point *G, mbedtls_mpi *d, mbedtls_ecp_point *Q, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Generate a keypair with configurable base point.
mbedtls_mpi d
Definition: ecp.h:161
unsigned int h
Definition: ecp.h:141
int mbedtls_ecp_point_cmp(const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q)
Compare two points.
int mbedtls_ecp_tls_write_point(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt, int format, size_t *olen, unsigned char *buf, size_t blen)
Export a point as a TLS ECPoint record.
size_t T_size
Definition: ecp.h:147
mbedtls_ecp_point * T
Definition: ecp.h:146
mbedtls_ecp_point G
Definition: ecp.h:137
MPI structure.
Definition: bignum.h:146
ECP point structure (jacobian coordinates)
Definition: ecp.h:99
const mbedtls_ecp_curve_info * mbedtls_ecp_curve_info_from_name(const char *name)
Get curve information from a human-readable name.
const char * name
Definition: ecp.h:87
uint16_t bit_size
Definition: ecp.h:86
mbedtls_mpi B
Definition: ecp.h:136
void mbedtls_ecp_group_init(mbedtls_ecp_group *grp)
Initialize a group (to something meaningless)