PolarSSL v1.3.1
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
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
56 typedef enum
57 {
67 } ecp_group_id;
68 
72 #define POLARSSL_ECP_DP_MAX 9
73 
77 typedef struct
78 {
80  uint16_t tls_id;
81  uint16_t size;
82  const char *name;
84 
94 typedef struct
95 {
96  mpi X;
97  mpi Y;
98  mpi Z;
99 }
100 ecp_point;
101 
117 typedef struct
118 {
120  mpi P;
121  mpi A;
122  mpi B;
124  mpi N;
125  size_t pbits;
126  size_t nbits;
127  unsigned int h;
128  int (*modp)(mpi *);
129  int (*t_pre)(ecp_point *, void *);
130  int (*t_post)(ecp_point *, void *);
131  void *t_data;
133  size_t T_size;
134 }
135 ecp_group;
136 
144 typedef struct
145 {
147  mpi d;
149 }
151 
155 #define POLARSSL_ECP_MAX_BITS 521
156 #define POLARSSL_ECP_MAX_BYTES ( ( POLARSSL_ECP_MAX_BITS + 7 ) / 8 )
157 #define POLARSSL_ECP_MAX_PT_LEN ( 2 * POLARSSL_ECP_MAX_BYTES + 1 )
158 
159 /*
160  * Maximum window size (actually, NAF width) used for point multipliation.
161  * Default: 8.
162  * Minimum value: 2. Maximum value: 8.
163  *
164  * Result is an array of at most ( 1 << ( POLARSSL_ECP_WINDOW_SIZE - 1 ) )
165  * points used for point multiplication.
166  *
167  * Reduction in size may reduce speed for big curves.
168  */
169 #define POLARSSL_ECP_WINDOW_SIZE 8
171 /*
172  * Point formats, from RFC 4492's enum ECPointFormat
173  */
174 #define POLARSSL_ECP_PF_UNCOMPRESSED 0
175 #define POLARSSL_ECP_PF_COMPRESSED 1
177 /*
178  * Some other constants from RFC 4492
179  */
180 #define POLARSSL_ECP_TLS_NAMED_CURVE 3
187 const ecp_curve_info *ecp_curve_list( void );
188 
192 void ecp_point_init( ecp_point *pt );
193 
197 void ecp_group_init( ecp_group *grp );
198 
202 void ecp_keypair_init( ecp_keypair *key );
203 
207 void ecp_point_free( ecp_point *pt );
208 
212 void ecp_group_free( ecp_group *grp );
213 
217 void ecp_keypair_free( ecp_keypair *key );
218 
227 int ecp_set_zero( ecp_point *pt );
228 
236 int ecp_is_zero( ecp_point *pt );
237 
247 int ecp_copy( ecp_point *P, const ecp_point *Q );
248 
258 int ecp_group_copy( ecp_group *dst, const ecp_group *src );
259 
270 int ecp_point_read_string( ecp_point *P, int radix,
271  const char *x, const char *y );
272 
288 int ecp_group_read_string( ecp_group *grp, int radix,
289  const char *p, const char *b,
290  const char *gx, const char *gy, const char *n);
291 
306 int ecp_point_write_binary( const ecp_group *grp, const ecp_point *P,
307  int format, size_t *olen,
308  unsigned char *buf, size_t buflen );
309 
326 int ecp_point_read_binary( const ecp_group *grp, ecp_point *P,
327  const unsigned char *buf, size_t ilen );
328 
342 int ecp_use_known_dp( ecp_group *grp, ecp_group_id index );
343 
355 int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len );
356 
368 int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
369  unsigned char *buf, size_t blen );
370 
379 
387 const ecp_curve_info *ecp_curve_info_from_tls_id( uint16_t tls_id );
388 
401 int ecp_tls_read_point( const ecp_group *grp, ecp_point *pt,
402  const unsigned char **buf, size_t len );
403 
418 int ecp_tls_write_point( const ecp_group *grp, const ecp_point *pt,
419  int format, size_t *olen,
420  unsigned char *buf, size_t blen );
421 
433 int ecp_add( const ecp_group *grp, ecp_point *R,
434  const ecp_point *P, const ecp_point *Q );
435 
447 int ecp_sub( const ecp_group *grp, ecp_point *R,
448  const ecp_point *P, const ecp_point *Q );
449 
479 int ecp_mul( ecp_group *grp, ecp_point *R,
480  const mpi *m, const ecp_point *P,
481  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
482 
483 
505 int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt );
506 
520 int ecp_check_privkey( const ecp_group *grp, const mpi *d );
521 
538 int ecp_gen_keypair( ecp_group *grp, mpi *d, ecp_point *Q,
539  int (*f_rng)(void *, unsigned char *, size_t),
540  void *p_rng );
541 
547 int ecp_self_test( int verbose );
548 
549 #ifdef __cplusplus
550 }
551 #endif
552 
553 #endif