00001
00027 #ifndef POLARSSL_ECP_H
00028 #define POLARSSL_ECP_H
00029
00030 #include "bignum.h"
00031
00032
00033
00034
00035 #define POLARSSL_ERR_ECP_BAD_INPUT_DATA -0x4F80
00036 #define POLARSSL_ERR_ECP_BUFFER_TOO_SMALL -0x4F00
00037 #define POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE -0x4E80
00038 #define POLARSSL_ERR_ECP_VERIFY_FAILED -0x4E00
00039 #define POLARSSL_ERR_ECP_MALLOC_FAILED -0x4D80
00040 #define POLARSSL_ERR_ECP_RANDOM_FAILED -0x4D00
00041 #define POLARSSL_ERR_ECP_INVALID_KEY -0x4C80
00043 #ifdef __cplusplus
00044 extern "C" {
00045 #endif
00046
00056 typedef enum
00057 {
00058 POLARSSL_ECP_DP_NONE = 0,
00059 POLARSSL_ECP_DP_SECP192R1,
00060 POLARSSL_ECP_DP_SECP224R1,
00061 POLARSSL_ECP_DP_SECP256R1,
00062 POLARSSL_ECP_DP_SECP384R1,
00063 POLARSSL_ECP_DP_SECP521R1,
00064 POLARSSL_ECP_DP_BP256R1,
00065 POLARSSL_ECP_DP_BP384R1,
00066 POLARSSL_ECP_DP_BP512R1,
00067 } ecp_group_id;
00068
00072 #define POLARSSL_ECP_DP_MAX 9
00073
00077 typedef struct
00078 {
00079 ecp_group_id grp_id;
00080 uint16_t tls_id;
00081 uint16_t size;
00082 const char *name;
00083 } ecp_curve_info;
00084
00094 typedef struct
00095 {
00096 mpi X;
00097 mpi Y;
00098 mpi Z;
00099 }
00100 ecp_point;
00101
00117 typedef struct
00118 {
00119 ecp_group_id id;
00120 mpi P;
00121 mpi A;
00122 mpi B;
00123 ecp_point G;
00124 mpi N;
00125 size_t pbits;
00126 size_t nbits;
00127 unsigned int h;
00128 int (*modp)(mpi *);
00129 int (*t_pre)(ecp_point *, void *);
00130 int (*t_post)(ecp_point *, void *);
00131 void *t_data;
00132 ecp_point *T;
00133 size_t T_size;
00134 }
00135 ecp_group;
00136
00144 typedef struct
00145 {
00146 ecp_group grp;
00147 mpi d;
00148 ecp_point Q;
00149 }
00150 ecp_keypair;
00151
00155 #define POLARSSL_ECP_MAX_BITS 521
00156 #define POLARSSL_ECP_MAX_BYTES ( ( POLARSSL_ECP_MAX_BITS + 7 ) / 8 )
00157 #define POLARSSL_ECP_MAX_PT_LEN ( 2 * POLARSSL_ECP_MAX_BYTES + 1 )
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169 #define POLARSSL_ECP_WINDOW_SIZE 8
00171
00172
00173
00174 #define POLARSSL_ECP_PF_UNCOMPRESSED 0
00175 #define POLARSSL_ECP_PF_COMPRESSED 1
00177
00178
00179
00180 #define POLARSSL_ECP_TLS_NAMED_CURVE 3
00187 const ecp_curve_info *ecp_curve_list( void );
00188
00196 const ecp_curve_info *ecp_curve_info_from_grp_id( ecp_group_id grp_id );
00197
00205 const ecp_curve_info *ecp_curve_info_from_tls_id( uint16_t tls_id );
00206
00210 void ecp_point_init( ecp_point *pt );
00211
00215 void ecp_group_init( ecp_group *grp );
00216
00220 void ecp_keypair_init( ecp_keypair *key );
00221
00225 void ecp_point_free( ecp_point *pt );
00226
00230 void ecp_group_free( ecp_group *grp );
00231
00235 void ecp_keypair_free( ecp_keypair *key );
00236
00246 int ecp_copy( ecp_point *P, const ecp_point *Q );
00247
00257 int ecp_group_copy( ecp_group *dst, const ecp_group *src );
00258
00267 int ecp_set_zero( ecp_point *pt );
00268
00276 int ecp_is_zero( ecp_point *pt );
00277
00288 int ecp_point_read_string( ecp_point *P, int radix,
00289 const char *x, const char *y );
00290
00305 int ecp_point_write_binary( const ecp_group *grp, const ecp_point *P,
00306 int format, size_t *olen,
00307 unsigned char *buf, size_t buflen );
00308
00325 int ecp_point_read_binary( const ecp_group *grp, ecp_point *P,
00326 const unsigned char *buf, size_t ilen );
00327
00340 int ecp_tls_read_point( const ecp_group *grp, ecp_point *pt,
00341 const unsigned char **buf, size_t len );
00342
00357 int ecp_tls_write_point( const ecp_group *grp, const ecp_point *pt,
00358 int format, size_t *olen,
00359 unsigned char *buf, size_t blen );
00360
00376 int ecp_group_read_string( ecp_group *grp, int radix,
00377 const char *p, const char *b,
00378 const char *gx, const char *gy, const char *n);
00379
00393 int ecp_use_known_dp( ecp_group *grp, ecp_group_id index );
00394
00406 int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len );
00407
00419 int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
00420 unsigned char *buf, size_t blen );
00421
00433 int ecp_add( const ecp_group *grp, ecp_point *R,
00434 const ecp_point *P, const ecp_point *Q );
00435
00447 int ecp_sub( const ecp_group *grp, ecp_point *R,
00448 const ecp_point *P, const ecp_point *Q );
00449
00479 int ecp_mul( ecp_group *grp, ecp_point *R,
00480 const mpi *m, const ecp_point *P,
00481 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
00482
00483
00505 int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt );
00506
00520 int ecp_check_privkey( const ecp_group *grp, const mpi *d );
00521
00538 int ecp_gen_keypair( ecp_group *grp, mpi *d, ecp_point *Q,
00539 int (*f_rng)(void *, unsigned char *, size_t),
00540 void *p_rng );
00541
00547 int ecp_self_test( int verbose );
00548
00549 #ifdef __cplusplus
00550 }
00551 #endif
00552
00553 #endif