PolarSSL v1.1.4
|
00001 00030 #ifndef POLARSSL_CIPHER_H 00031 #define POLARSSL_CIPHER_H 00032 00033 #include <string.h> 00034 00035 #if defined(_MSC_VER) && !defined(inline) 00036 #define inline _inline 00037 #else 00038 #if defined(__ARMCC_VERSION) && !defined(inline) 00039 #define inline __inline 00040 #endif /* __ARMCC_VERSION */ 00041 #endif /*_MSC_VER */ 00042 00043 #define POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE -0x6080 00044 #define POLARSSL_ERR_CIPHER_BAD_INPUT_DATA -0x6100 00045 #define POLARSSL_ERR_CIPHER_ALLOC_FAILED -0x6180 00046 #define POLARSSL_ERR_CIPHER_INVALID_PADDING -0x6200 00047 #define POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED -0x6280 00049 typedef enum { 00050 POLARSSL_CIPHER_ID_NONE = 0, 00051 POLARSSL_CIPHER_ID_AES, 00052 POLARSSL_CIPHER_ID_DES, 00053 POLARSSL_CIPHER_ID_3DES, 00054 POLARSSL_CIPHER_ID_CAMELLIA, 00055 } cipher_id_t; 00056 00057 typedef enum { 00058 POLARSSL_CIPHER_NONE = 0, 00059 POLARSSL_CIPHER_AES_128_CBC, 00060 POLARSSL_CIPHER_AES_192_CBC, 00061 POLARSSL_CIPHER_AES_256_CBC, 00062 POLARSSL_CIPHER_AES_128_CFB128, 00063 POLARSSL_CIPHER_AES_192_CFB128, 00064 POLARSSL_CIPHER_AES_256_CFB128, 00065 POLARSSL_CIPHER_AES_128_CTR, 00066 POLARSSL_CIPHER_AES_192_CTR, 00067 POLARSSL_CIPHER_AES_256_CTR, 00068 POLARSSL_CIPHER_CAMELLIA_128_CBC, 00069 POLARSSL_CIPHER_CAMELLIA_192_CBC, 00070 POLARSSL_CIPHER_CAMELLIA_256_CBC, 00071 POLARSSL_CIPHER_CAMELLIA_128_CFB128, 00072 POLARSSL_CIPHER_CAMELLIA_192_CFB128, 00073 POLARSSL_CIPHER_CAMELLIA_256_CFB128, 00074 POLARSSL_CIPHER_CAMELLIA_128_CTR, 00075 POLARSSL_CIPHER_CAMELLIA_192_CTR, 00076 POLARSSL_CIPHER_CAMELLIA_256_CTR, 00077 POLARSSL_CIPHER_DES_CBC, 00078 POLARSSL_CIPHER_DES_EDE_CBC, 00079 POLARSSL_CIPHER_DES_EDE3_CBC 00080 } cipher_type_t; 00081 00082 typedef enum { 00083 POLARSSL_MODE_NONE = 0, 00084 POLARSSL_MODE_CBC, 00085 POLARSSL_MODE_CFB128, 00086 POLARSSL_MODE_OFB, 00087 POLARSSL_MODE_CTR, 00088 } cipher_mode_t; 00089 00090 typedef enum { 00091 POLARSSL_OPERATION_NONE = -1, 00092 POLARSSL_DECRYPT = 0, 00093 POLARSSL_ENCRYPT, 00094 } operation_t; 00095 00096 enum { 00098 POLARSSL_KEY_LENGTH_NONE = 0, 00100 POLARSSL_KEY_LENGTH_DES = 64, 00102 POLARSSL_KEY_LENGTH_DES_EDE = 128, 00104 POLARSSL_KEY_LENGTH_DES_EDE3 = 192, 00106 POLARSSL_MAX_IV_LENGTH = 16, 00107 }; 00108 00112 typedef struct { 00113 00115 cipher_id_t cipher; 00116 00118 int (*cbc_func)( void *ctx, operation_t mode, size_t length, unsigned char *iv, 00119 const unsigned char *input, unsigned char *output ); 00120 00122 int (*cfb128_func)( void *ctx, operation_t mode, size_t length, size_t *iv_off, 00123 unsigned char *iv, const unsigned char *input, unsigned char *output ); 00124 00126 int (*ctr_func)( void *ctx, size_t length, size_t *nc_off, unsigned char *nonce_counter, 00127 unsigned char *stream_block, const unsigned char *input, unsigned char *output ); 00128 00130 int (*setkey_enc_func)( void *ctx, const unsigned char *key, unsigned int key_length); 00131 00133 int (*setkey_dec_func)( void *ctx, const unsigned char *key, unsigned int key_length); 00134 00136 void * (*ctx_alloc_func)( void ); 00137 00139 void (*ctx_free_func)( void *ctx ); 00140 00141 } cipher_base_t; 00142 00146 typedef struct { 00148 cipher_type_t type; 00149 00151 cipher_mode_t mode; 00152 00155 unsigned int key_length; 00156 00158 const char * name; 00159 00161 unsigned int iv_size; 00162 00164 unsigned int block_size; 00165 00167 const cipher_base_t *base; 00168 00169 } cipher_info_t; 00170 00174 typedef struct { 00176 const cipher_info_t *cipher_info; 00177 00179 int key_length; 00180 00182 operation_t operation; 00183 00185 unsigned char unprocessed_data[POLARSSL_MAX_IV_LENGTH]; 00186 00188 size_t unprocessed_len; 00189 00191 unsigned char iv[POLARSSL_MAX_IV_LENGTH]; 00192 00194 void *cipher_ctx; 00195 } cipher_context_t; 00196 00197 #ifdef __cplusplus 00198 extern "C" { 00199 #endif 00200 00207 const int *cipher_list( void ); 00208 00218 const cipher_info_t *cipher_info_from_string( const char *cipher_name ); 00219 00229 const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type ); 00230 00243 int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info ); 00244 00254 int cipher_free_ctx( cipher_context_t *ctx ); 00255 00264 static inline unsigned int cipher_get_block_size( const cipher_context_t *ctx ) 00265 { 00266 if( NULL == ctx || NULL == ctx->cipher_info ) 00267 return 0; 00268 00269 return ctx->cipher_info->block_size; 00270 } 00271 00281 static inline cipher_mode_t cipher_get_cipher_mode( const cipher_context_t *ctx ) 00282 { 00283 if( NULL == ctx || NULL == ctx->cipher_info ) 00284 return POLARSSL_MODE_NONE; 00285 00286 return ctx->cipher_info->mode; 00287 } 00288 00297 static inline int cipher_get_iv_size( const cipher_context_t *ctx ) 00298 { 00299 if( NULL == ctx || NULL == ctx->cipher_info ) 00300 return 0; 00301 00302 return ctx->cipher_info->iv_size; 00303 } 00304 00313 static inline cipher_type_t cipher_get_type( const cipher_context_t *ctx ) 00314 { 00315 if( NULL == ctx || NULL == ctx->cipher_info ) 00316 return 0; 00317 00318 return ctx->cipher_info->type; 00319 } 00320 00328 static inline const char *cipher_get_name( const cipher_context_t *ctx ) 00329 { 00330 if( NULL == ctx || NULL == ctx->cipher_info ) 00331 return 0; 00332 00333 return ctx->cipher_info->name; 00334 } 00335 00345 static inline int cipher_get_key_size ( const cipher_context_t *ctx ) 00346 { 00347 if( NULL == ctx ) 00348 return POLARSSL_KEY_LENGTH_NONE; 00349 00350 return ctx->key_length; 00351 } 00352 00362 static inline operation_t cipher_get_operation( const cipher_context_t *ctx ) 00363 { 00364 if( NULL == ctx || NULL == ctx->cipher_info ) 00365 return POLARSSL_OPERATION_NONE; 00366 00367 return ctx->operation; 00368 } 00369 00385 int cipher_setkey( cipher_context_t *ctx, const unsigned char *key, int key_length, 00386 const operation_t operation ); 00387 00397 int cipher_reset( cipher_context_t *ctx, const unsigned char *iv ); 00398 00422 int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ilen, 00423 unsigned char *output, size_t *olen ); 00424 00442 int cipher_finish( cipher_context_t *ctx, unsigned char *output, size_t *olen); 00443 00444 00450 int cipher_self_test( int verbose ); 00451 00452 #ifdef __cplusplus 00453 } 00454 #endif 00455 00456 #endif /* POLARSSL_MD_H */