00001
00028 #ifndef POLARSSL_PK_H
00029 #define POLARSSL_PK_H
00030
00031 #include "config.h"
00032
00033 #include "md.h"
00034
00035 #if defined(POLARSSL_RSA_C)
00036 #include "rsa.h"
00037 #endif
00038
00039 #if defined(POLARSSL_ECP_C)
00040 #include "ecp.h"
00041 #endif
00042
00043 #if defined(POLARSSL_ECDSA_C)
00044 #include "ecdsa.h"
00045 #endif
00046
00047 #define POLARSSL_ERR_PK_MALLOC_FAILED -0x2F80
00048 #define POLARSSL_ERR_PK_TYPE_MISMATCH -0x2F00
00049 #define POLARSSL_ERR_PK_BAD_INPUT_DATA -0x2E80
00050 #define POLARSSL_ERR_PK_FILE_IO_ERROR -0x2E00
00051 #define POLARSSL_ERR_PK_KEY_INVALID_VERSION -0x2D80
00052 #define POLARSSL_ERR_PK_KEY_INVALID_FORMAT -0x2D00
00053 #define POLARSSL_ERR_PK_UNKNOWN_PK_ALG -0x2C80
00054 #define POLARSSL_ERR_PK_PASSWORD_REQUIRED -0x2C00
00055 #define POLARSSL_ERR_PK_PASSWORD_MISMATCH -0x2B80
00056 #define POLARSSL_ERR_PK_INVALID_PUBKEY -0x2B00
00057 #define POLARSSL_ERR_PK_INVALID_ALG -0x2A80
00058 #define POLARSSL_ERR_PK_UNKNOWN_NAMED_CURVE -0x2A00
00059 #define POLARSSL_ERR_PK_FEATURE_UNAVAILABLE -0x2980
00062 #if defined(POLARSSL_RSA_C)
00063
00069 #define pk_rsa( pk ) ( (rsa_context *) (pk).pk_ctx )
00070 #endif
00071
00072 #if defined(POLARSSL_ECP_C)
00073
00079 #define pk_ec( pk ) ( (ecp_keypair *) (pk).pk_ctx )
00080 #endif
00081
00082
00083 #ifdef __cplusplus
00084 extern "C" {
00085 #endif
00086
00090 typedef enum {
00091 POLARSSL_PK_NONE=0,
00092 POLARSSL_PK_RSA,
00093 POLARSSL_PK_ECKEY,
00094 POLARSSL_PK_ECKEY_DH,
00095 POLARSSL_PK_ECDSA,
00096 POLARSSL_PK_RSA_ALT,
00097 } pk_type_t;
00098
00102 typedef enum
00103 {
00104 POLARSSL_PK_DEBUG_NONE = 0,
00105 POLARSSL_PK_DEBUG_MPI,
00106 POLARSSL_PK_DEBUG_ECP,
00107 } pk_debug_type;
00108
00112 typedef struct
00113 {
00114 pk_debug_type type;
00115 const char *name;
00116 void *value;
00117 } pk_debug_item;
00118
00120 #define POLARSSL_PK_DEBUG_MAX_ITEMS 3
00121
00125 typedef struct
00126 {
00128 pk_type_t type;
00129
00131 const char *name;
00132
00134 size_t (*get_size)( const void * );
00135
00137 int (*can_do)( pk_type_t type );
00138
00140 int (*verify_func)( void *ctx, md_type_t md_alg,
00141 const unsigned char *hash, size_t hash_len,
00142 const unsigned char *sig, size_t sig_len );
00143
00145 int (*sign_func)( void *ctx, md_type_t md_alg,
00146 const unsigned char *hash, size_t hash_len,
00147 unsigned char *sig, size_t *sig_len,
00148 int (*f_rng)(void *, unsigned char *, size_t),
00149 void *p_rng );
00150
00152 int (*decrypt_func)( void *ctx, const unsigned char *input, size_t ilen,
00153 unsigned char *output, size_t *olen, size_t osize,
00154 int (*f_rng)(void *, unsigned char *, size_t),
00155 void *p_rng );
00156
00158 int (*encrypt_func)( void *ctx, const unsigned char *input, size_t ilen,
00159 unsigned char *output, size_t *olen, size_t osize,
00160 int (*f_rng)(void *, unsigned char *, size_t),
00161 void *p_rng );
00162
00164 void * (*ctx_alloc_func)( void );
00165
00167 void (*ctx_free_func)( void *ctx );
00168
00170 void (*debug_func)( const void *ctx, pk_debug_item *items );
00171
00172 } pk_info_t;
00173
00177 typedef struct
00178 {
00179 const pk_info_t * pk_info;
00180 void * pk_ctx;
00181 } pk_context;
00182
00186 typedef int (*pk_rsa_alt_decrypt_func)( void *ctx, int mode, size_t *olen,
00187 const unsigned char *input, unsigned char *output,
00188 size_t output_max_len );
00189 typedef int (*pk_rsa_alt_sign_func)( void *ctx,
00190 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
00191 int mode, int hash_id, unsigned int hashlen,
00192 const unsigned char *hash, unsigned char *sig );
00193 typedef size_t (*pk_rsa_alt_key_len_func)( void *ctx );
00194
00202 const pk_info_t *pk_info_from_type( pk_type_t pk_type );
00203
00207 void pk_init( pk_context *ctx );
00208
00212 void pk_free( pk_context *ctx );
00213
00228 int pk_init_ctx( pk_context *ctx, const pk_info_t *info );
00229
00244 int pk_init_ctx_rsa_alt( pk_context *ctx, void * key,
00245 pk_rsa_alt_decrypt_func decrypt_func,
00246 pk_rsa_alt_sign_func sign_func,
00247 pk_rsa_alt_key_len_func key_len_func );
00248
00256 size_t pk_get_size( const pk_context *ctx );
00257
00264 static inline size_t pk_get_len( const pk_context *ctx )
00265 {
00266 return( ( pk_get_size( ctx ) + 7 ) / 8 );
00267 }
00268
00278 int pk_can_do( pk_context *ctx, pk_type_t type );
00279
00298 int pk_verify( pk_context *ctx, md_type_t md_alg,
00299 const unsigned char *hash, size_t hash_len,
00300 const unsigned char *sig, size_t sig_len );
00301
00321 int pk_sign( pk_context *ctx, md_type_t md_alg,
00322 const unsigned char *hash, size_t hash_len,
00323 unsigned char *sig, size_t *sig_len,
00324 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
00325
00340 int pk_decrypt( pk_context *ctx,
00341 const unsigned char *input, size_t ilen,
00342 unsigned char *output, size_t *olen, size_t osize,
00343 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
00344
00359 int pk_encrypt( pk_context *ctx,
00360 const unsigned char *input, size_t ilen,
00361 unsigned char *output, size_t *olen, size_t osize,
00362 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
00363
00372 int pk_debug( const pk_context *ctx, pk_debug_item *items );
00373
00381 const char * pk_get_name( const pk_context *ctx );
00382
00390 pk_type_t pk_get_type( const pk_context *ctx );
00391
00392 #if defined(POLARSSL_PK_PARSE_C)
00393
00405 int pk_parse_key( pk_context *ctx,
00406 const unsigned char *key, size_t keylen,
00407 const unsigned char *pwd, size_t pwdlen );
00408
00419 int pk_parse_public_key( pk_context *ctx,
00420 const unsigned char *key, size_t keylen );
00421
00422 #if defined(POLARSSL_FS_IO)
00423
00433 int pk_parse_keyfile( pk_context *ctx,
00434 const char *path, const char *password );
00435
00445 int pk_parse_public_keyfile( pk_context *ctx, const char *path );
00446 #endif
00447 #endif
00448
00449 #if defined(POLARSSL_PK_WRITE_C)
00450
00463 int pk_write_key_der( pk_context *pk, unsigned char *buf, size_t size );
00464
00478 int pk_write_pubkey_der( pk_context *key, unsigned char *buf, size_t size );
00479
00480 #if defined(POLARSSL_PEM_WRITE_C)
00481
00490 int pk_write_pubkey_pem( pk_context *key, unsigned char *buf, size_t size );
00491
00501 int pk_write_key_pem( pk_context *key, unsigned char *buf, size_t size );
00502 #endif
00503 #endif
00504
00505
00506
00507
00508
00509
00510 #if defined(POLARSSL_PK_PARSE_C)
00511
00520 int pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
00521 pk_context *pk );
00522 #endif
00523
00524 #if defined(POLARSSL_PK_WRITE_C)
00525
00535 int pk_write_pubkey( unsigned char **p, unsigned char *start,
00536 const pk_context *key );
00537 #endif
00538
00539 #ifdef __cplusplus
00540 }
00541 #endif
00542
00543 #endif