PolarSSL v1.3.9
ecp.h
Go to the documentation of this file.
1
27#ifndef POLARSSL_ECP_H
28#define POLARSSL_ECP_H
29
30#include "bignum.h"
31
32/*
33 * ECP error codes
34 */
35#define POLARSSL_ERR_ECP_BAD_INPUT_DATA -0x4F80
36#define POLARSSL_ERR_ECP_BUFFER_TOO_SMALL -0x4F00
37#define POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE -0x4E80
38#define POLARSSL_ERR_ECP_VERIFY_FAILED -0x4E00
39#define POLARSSL_ERR_ECP_MALLOC_FAILED -0x4D80
40#define POLARSSL_ERR_ECP_RANDOM_FAILED -0x4D00
41#define POLARSSL_ERR_ECP_INVALID_KEY -0x4C80
42#define POLARSSL_ERR_ECP_SIG_LEN_MISMATCH -0x4C00
44#ifdef __cplusplus
45extern "C" {
46#endif
47
76
82#define POLARSSL_ECP_DP_MAX 12
83
87typedef struct
88{
90 uint16_t tls_id;
91 uint16_t size;
92 const char *name;
94
104typedef struct
105{
109}
111
136typedef struct
137{
144 size_t pbits;
145 size_t nbits;
146 unsigned int h;
147 int (*modp)(mpi *);
148 int (*t_pre)(ecp_point *, void *);
149 int (*t_post)(ecp_point *, void *);
150 void *t_data;
152 size_t T_size;
153}
155
163typedef struct
164{
168}
170
179#if !defined(POLARSSL_ECP_MAX_BITS)
183#define POLARSSL_ECP_MAX_BITS 521
184#endif
185
186#define POLARSSL_ECP_MAX_BYTES ( ( POLARSSL_ECP_MAX_BITS + 7 ) / 8 )
187#define POLARSSL_ECP_MAX_PT_LEN ( 2 * POLARSSL_ECP_MAX_BYTES + 1 )
188
189#if !defined(POLARSSL_ECP_WINDOW_SIZE)
190/*
191 * Maximum "window" size used for point multiplication.
192 * Default: 6.
193 * Minimum value: 2. Maximum value: 7.
194 *
195 * Result is an array of at most ( 1 << ( POLARSSL_ECP_WINDOW_SIZE - 1 ) )
196 * points used for point multiplication. This value is directly tied to EC
197 * peak memory usage, so decreasing it by one should roughly cut memory usage
198 * by two (if large curves are in use).
199 *
200 * Reduction in size may reduce speed, but larger curves are impacted first.
201 * Sample performances (in ECDHE handshakes/s, with FIXED_POINT_OPTIM = 1):
202 * w-size: 6 5 4 3 2
203 * 521 145 141 135 120 97
204 * 384 214 209 198 177 146
205 * 256 320 320 303 262 226
206
207 * 224 475 475 453 398 342
208 * 192 640 640 633 587 476
209 */
210#define POLARSSL_ECP_WINDOW_SIZE 6
211#endif /* POLARSSL_ECP_WINDOW_SIZE */
212
213#if !defined(POLARSSL_ECP_FIXED_POINT_OPTIM)
214/*
215 * Trade memory for speed on fixed-point multiplication.
216 *
217 * This speeds up repeated multiplication of the generator (that is, the
218 * multiplication in ECDSA signatures, and half of the multiplications in
219 * ECDSA verification and ECDHE) by a factor roughly 3 to 4.
220 *
221 * The cost is increasing EC peak memory usage by a factor roughly 2.
222 *
223 * Change this value to 0 to reduce peak memory usage.
224 */
225#define POLARSSL_ECP_FIXED_POINT_OPTIM 1
226#endif /* POLARSSL_ECP_FIXED_POINT_OPTIM */
227
228/* \} name SECTION: Module settings */
229
230/*
231 * Point formats, from RFC 4492's enum ECPointFormat
232 */
233#define POLARSSL_ECP_PF_UNCOMPRESSED 0
234#define POLARSSL_ECP_PF_COMPRESSED 1
236/*
237 * Some other constants from RFC 4492
238 */
239#define POLARSSL_ECP_TLS_NAMED_CURVE 3
248
257
266
275
283const ecp_curve_info *ecp_curve_info_from_name( const char *name );
284
289
294
299
304
309
314
324int ecp_copy( ecp_point *P, const ecp_point *Q );
325
335int ecp_group_copy( ecp_group *dst, const ecp_group *src );
336
346
355
367 const char *x, const char *y );
368
384 int format, size_t *olen,
385 unsigned char *buf, size_t buflen );
386
406 const unsigned char *buf, size_t ilen );
407
421 const unsigned char **buf, size_t len );
422
437int ecp_tls_write_point( const ecp_group *grp, const ecp_point *pt,
438 int format, size_t *olen,
439 unsigned char *buf, size_t blen );
440
456int ecp_group_read_string( ecp_group *grp, int radix,
457 const char *p, const char *b,
458 const char *gx, const char *gy, const char *n);
459
474
486int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len );
487
499int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
500 unsigned char *buf, size_t blen );
501
516int ecp_add( const ecp_group *grp, ecp_point *R,
517 const ecp_point *P, const ecp_point *Q );
518
533int ecp_sub( const ecp_group *grp, ecp_point *R,
534 const ecp_point *P, const ecp_point *Q );
535
563 const mpi *m, const ecp_point *P,
564 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
565
587int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt );
588
602int ecp_check_privkey( const ecp_group *grp, const mpi *d );
603
621 int (*f_rng)(void *, unsigned char *, size_t),
622 void *p_rng );
623
636 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
637
638#if defined(POLARSSL_SELF_TEST)
644int ecp_self_test( int verbose );
645#endif
646
647#ifdef __cplusplus
648}
649#endif
650
651#endif /* ecp.h */
Multi-precision integer library.
int ecp_sub(const ecp_group *grp, ecp_point *R, const ecp_point *P, const ecp_point *Q)
Subtraction: R = P - Q.
const ecp_curve_info * ecp_curve_info_from_grp_id(ecp_group_id grp_id)
Get curve information from an internal group identifier.
int ecp_tls_read_group(ecp_group *grp, const unsigned char **buf, size_t len)
Set a group from a TLS ECParameters record.
int ecp_check_pubkey(const ecp_group *grp, const ecp_point *pt)
Check that a point is a valid public key on this curve.
int ecp_self_test(int verbose)
Checkup routine.
int ecp_mul(ecp_group *grp, ecp_point *R, const mpi *m, const 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 ecp_set_zero(ecp_point *pt)
Set a point to zero.
int ecp_point_read_binary(const ecp_group *grp, ecp_point *P, const unsigned char *buf, size_t ilen)
Import a point from unsigned binary data.
int ecp_point_read_string(ecp_point *P, int radix, const char *x, const char *y)
Import a non-zero point from two ASCII strings.
int ecp_copy(ecp_point *P, const ecp_point *Q)
Copy the contents of point Q into P.
int ecp_add(const ecp_group *grp, ecp_point *R, const ecp_point *P, const ecp_point *Q)
Addition: R = P + Q.
int ecp_tls_read_point(const ecp_group *grp, ecp_point *pt, const unsigned char **buf, size_t len)
Import a point from a TLS ECPoint record.
void ecp_point_free(ecp_point *pt)
Free the components of a point.
ecp_group_id
Domain parameters (curve, subgroup and generator) identifiers.
Definition ecp.h:58
@ POLARSSL_ECP_DP_SECP192K1
Definition ecp.h:72
@ POLARSSL_ECP_DP_SECP521R1
Definition ecp.h:64
@ POLARSSL_ECP_DP_SECP256R1
Definition ecp.h:62
@ POLARSSL_ECP_DP_BP384R1
Definition ecp.h:66
@ POLARSSL_ECP_DP_NONE
Definition ecp.h:59
@ POLARSSL_ECP_DP_M255
Definition ecp.h:69
@ POLARSSL_ECP_DP_SECP224K1
Definition ecp.h:73
@ POLARSSL_ECP_DP_SECP384R1
Definition ecp.h:63
@ POLARSSL_ECP_DP_SECP256K1
Definition ecp.h:74
@ POLARSSL_ECP_DP_M383
Definition ecp.h:70
@ POLARSSL_ECP_DP_SECP224R1
Definition ecp.h:61
@ POLARSSL_ECP_DP_M221
Definition ecp.h:68
@ POLARSSL_ECP_DP_SECP192R1
Definition ecp.h:60
@ POLARSSL_ECP_DP_BP256R1
Definition ecp.h:65
@ POLARSSL_ECP_DP_BP512R1
Definition ecp.h:67
@ POLARSSL_ECP_DP_M511
Definition ecp.h:71
const ecp_curve_info * ecp_curve_info_from_tls_id(uint16_t tls_id)
Get curve information from a TLS NamedCurve value.
void ecp_point_init(ecp_point *pt)
Initialize a point (as zero)
void ecp_keypair_free(ecp_keypair *key)
Free the components of a key pair.
int ecp_check_privkey(const ecp_group *grp, const mpi *d)
Check that an mpi is a valid private key for this curve.
void ecp_group_init(ecp_group *grp)
Initialize a group (to something meaningless)
int ecp_tls_write_group(const ecp_group *grp, size_t *olen, unsigned char *buf, size_t blen)
Write the TLS ECParameters record for a group.
int ecp_group_read_string(ecp_group *grp, int radix, const char *p, const char *b, const char *gx, const char *gy, const char *n)
Import an ECP group from null-terminated ASCII strings.
int ecp_group_copy(ecp_group *dst, const ecp_group *src)
Copy the contents of a group object.
int ecp_gen_keypair(ecp_group *grp, mpi *d, ecp_point *Q, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Generate a keypair.
int ecp_is_zero(ecp_point *pt)
Tell if a point is zero.
const ecp_curve_info * ecp_curve_info_from_name(const char *name)
Get curve information from a human-readable name.
void ecp_group_free(ecp_group *grp)
Free the components of an ECP group.
int ecp_gen_key(ecp_group_id grp_id, ecp_keypair *key, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Generate a keypair.
void ecp_keypair_init(ecp_keypair *key)
Initialize a key pair (as an invalid one)
int ecp_tls_write_point(const ecp_group *grp, const ecp_point *pt, int format, size_t *olen, unsigned char *buf, size_t blen)
Export a point as a TLS ECPoint record.
const ecp_group_id * ecp_grp_id_list(void)
Get the list of supported curves in order of preferrence (grp_id only)
int ecp_use_known_dp(ecp_group *grp, ecp_group_id index)
Set a group using well-known domain parameters.
int ecp_point_write_binary(const ecp_group *grp, const ecp_point *P, int format, size_t *olen, unsigned char *buf, size_t buflen)
Export a point into unsigned binary data.
const ecp_curve_info * ecp_curve_list(void)
Get the list of supported curves in order of preferrence (full information)
Curve information for use by other modules.
Definition ecp.h:88
const char * name
Definition ecp.h:92
uint16_t tls_id
Definition ecp.h:90
ecp_group_id grp_id
Definition ecp.h:89
uint16_t size
Definition ecp.h:91
ECP group structure.
Definition ecp.h:137
mpi A
Definition ecp.h:140
ecp_point * T
Definition ecp.h:151
size_t nbits
Definition ecp.h:145
size_t pbits
Definition ecp.h:144
mpi N
Definition ecp.h:143
mpi B
Definition ecp.h:141
ecp_group_id id
Definition ecp.h:138
size_t T_size
Definition ecp.h:152
mpi P
Definition ecp.h:139
ecp_point G
Definition ecp.h:142
void * t_data
Definition ecp.h:150
unsigned int h
Definition ecp.h:146
ECP key pair structure.
Definition ecp.h:164
ecp_point Q
Definition ecp.h:167
mpi d
Definition ecp.h:166
ecp_group grp
Definition ecp.h:165
ECP point structure (jacobian coordinates)
Definition ecp.h:105
mpi Y
Definition ecp.h:107
mpi Z
Definition ecp.h:108
mpi X
Definition ecp.h:106
MPI structure.
Definition bignum.h:183