32 #if defined(POLARSSL_CIPHER_C)
39 #if defined _MSC_VER && !defined strcasecmp
40 #define strcasecmp _stricmp
44 static void polarssl_zeroize(
void *v,
size_t n ) {
45 volatile unsigned char *p = v;
while( n-- ) *p++ = 0;
48 static const int supported_ciphers[] = {
50 #if defined(POLARSSL_AES_C)
55 #if defined(POLARSSL_CIPHER_MODE_CFB)
61 #if defined(POLARSSL_CIPHER_MODE_CTR)
69 #if defined(POLARSSL_CAMELLIA_C)
74 #if defined(POLARSSL_CIPHER_MODE_CFB)
80 #if defined(POLARSSL_CIPHER_MODE_CTR)
88 #if defined(POLARSSL_DES_C)
94 #if defined(POLARSSL_BLOWFISH_C)
97 #if defined(POLARSSL_CIPHER_MODE_CFB)
101 #if defined(POLARSSL_CIPHER_MODE_CTR)
107 #if defined(POLARSSL_CIPHER_NULL_CIPHER)
116 return supported_ciphers;
122 switch ( cipher_type )
124 #if defined(POLARSSL_AES_C)
132 #if defined(POLARSSL_CIPHER_MODE_CFB)
141 #if defined(POLARSSL_CIPHER_MODE_CTR)
152 #if defined(POLARSSL_CAMELLIA_C)
160 #if defined(POLARSSL_CIPHER_MODE_CFB)
169 #if defined(POLARSSL_CIPHER_MODE_CTR)
180 #if defined(POLARSSL_DES_C)
189 #if defined(POLARSSL_BLOWFISH_C)
193 #if defined(POLARSSL_CIPHER_MODE_CFB)
198 #if defined(POLARSSL_CIPHER_MODE_CTR)
205 #if defined(POLARSSL_CIPHER_NULL_CIPHER)
207 return &null_cipher_info;
217 if( NULL == cipher_name )
221 #if defined(POLARSSL_CAMELLIA_C)
222 if( !strcasecmp(
"CAMELLIA-128-CBC", cipher_name ) )
224 if( !strcasecmp(
"CAMELLIA-192-CBC", cipher_name ) )
226 if( !strcasecmp(
"CAMELLIA-256-CBC", cipher_name ) )
229 #if defined(POLARSSL_CIPHER_MODE_CFB)
230 if( !strcasecmp(
"CAMELLIA-128-CFB128", cipher_name ) )
232 if( !strcasecmp(
"CAMELLIA-192-CFB128", cipher_name ) )
234 if( !strcasecmp(
"CAMELLIA-256-CFB128", cipher_name ) )
238 #if defined(POLARSSL_CIPHER_MODE_CTR)
239 if( !strcasecmp(
"CAMELLIA-128-CTR", cipher_name ) )
241 if( !strcasecmp(
"CAMELLIA-192-CTR", cipher_name ) )
243 if( !strcasecmp(
"CAMELLIA-256-CTR", cipher_name ) )
248 #if defined(POLARSSL_AES_C)
249 if( !strcasecmp(
"AES-128-CBC", cipher_name ) )
251 if( !strcasecmp(
"AES-192-CBC", cipher_name ) )
253 if( !strcasecmp(
"AES-256-CBC", cipher_name ) )
256 #if defined(POLARSSL_CIPHER_MODE_CFB)
257 if( !strcasecmp(
"AES-128-CFB128", cipher_name ) )
259 if( !strcasecmp(
"AES-192-CFB128", cipher_name ) )
261 if( !strcasecmp(
"AES-256-CFB128", cipher_name ) )
265 #if defined(POLARSSL_CIPHER_MODE_CTR)
266 if( !strcasecmp(
"AES-128-CTR", cipher_name ) )
268 if( !strcasecmp(
"AES-192-CTR", cipher_name ) )
270 if( !strcasecmp(
"AES-256-CTR", cipher_name ) )
275 #if defined(POLARSSL_DES_C)
276 if( !strcasecmp(
"DES-CBC", cipher_name ) )
278 if( !strcasecmp(
"DES-EDE-CBC", cipher_name ) )
280 if( !strcasecmp(
"DES-EDE3-CBC", cipher_name ) )
284 #if defined(POLARSSL_BLOWFISH_C)
285 if( !strcasecmp(
"BLOWFISH-CBC", cipher_name ) )
288 #if defined(POLARSSL_CIPHER_MODE_CFB)
289 if( !strcasecmp(
"BLOWFISH-CFB64", cipher_name ) )
293 #if defined(POLARSSL_CIPHER_MODE_CTR)
294 if( !strcasecmp(
"BLOWFISH-CTR", cipher_name ) )
299 #if defined(POLARSSL_CIPHER_NULL_CIPHER)
300 if( !strcasecmp(
"NULL", cipher_name ) )
309 if( NULL == cipher_info || NULL == ctx )
342 #if defined(POLARSSL_CIPHER_NULL_CIPHER)
367 if( NULL == ctx || NULL == ctx->
cipher_info || NULL == iv )
378 unsigned char *output,
size_t *olen )
383 if( NULL == ctx || NULL == ctx->
cipher_info || NULL == olen ||
391 #if defined(POLARSSL_CIPHER_NULL_CIPHER)
394 memcpy( output, input, ilen );
505 static void add_pkcs_padding(
unsigned char *output,
size_t output_len,
508 size_t padding_len = output_len - data_len;
511 for( i = 0; i < padding_len; i++ )
512 output[data_len + i] = (
unsigned char) padding_len;
515 static int get_pkcs_padding(
unsigned char *input,
unsigned int input_len,
518 unsigned int i, pad_idx;
519 unsigned char padding_len, bad = 0;
521 if( NULL == input || NULL == data_len )
524 padding_len = input[input_len - 1];
525 *data_len = input_len - padding_len;
528 bad |= padding_len > input_len;
529 bad |= padding_len == 0;
533 pad_idx = input_len - padding_len;
534 for( i = 0; i < input_len; i++ )
535 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
544 if( NULL == ctx || NULL == ctx->
cipher_info || NULL == olen )
589 #if defined(POLARSSL_SELF_TEST)
593 #define ASSERT(x) if (!(x)) { \
594 printf( "failed with %i at %s\n", value, (#x) ); \
int key_length
Key length to use.
const cipher_info_t blowfish_ctr_info
#define POLARSSL_ERR_CIPHER_BAD_INPUT_DATA
Bad input parameters to function.
const cipher_info_t blowfish_cbc_info
int cipher_finish(cipher_context_t *ctx, unsigned char *output, size_t *olen)
Generic cipher finalisation function.
#define POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE
The selected feature is not available.
static int cipher_get_iv_size(const cipher_context_t *ctx)
Returns the size of the cipher's IV.
const cipher_info_t camellia_192_ctr_info
const cipher_info_t aes_128_ctr_info
const cipher_info_t camellia_192_cbc_info
const cipher_info_t aes_256_ctr_info
#define POLARSSL_ERR_CIPHER_ALLOC_FAILED
Failed to allocate memory.
const cipher_info_t * cipher_info_from_type(const cipher_type_t cipher_type)
Returns the cipher information structure associated with the given cipher type.
static unsigned int cipher_get_block_size(const cipher_context_t *ctx)
Returns the block size of the given cipher.
const cipher_info_t * cipher_info_from_string(const char *cipher_name)
Returns the cipher information structure associated with the given cipher name.
const cipher_info_t aes_128_cfb128_info
const cipher_info_t des_cbc_info
Configuration options (set of defines)
const cipher_info_t aes_256_cbc_info
const cipher_info_t camellia_256_ctr_info
void(* ctx_free_func)(void *ctx)
Free the given context.
const cipher_info_t aes_192_ctr_info
#define POLARSSL_ERR_CIPHER_INVALID_PADDING
Input data contains invalid padding and is rejected.
const cipher_info_t camellia_128_ctr_info
const cipher_info_t blowfish_cfb64_info
int(* cbc_func)(void *ctx, operation_t mode, size_t length, unsigned char *iv, const unsigned char *input, unsigned char *output)
Encrypt using CBC.
const cipher_info_t des_ede3_cbc_info
unsigned char iv[POLARSSL_MAX_IV_LENGTH]
Current IV or NONCE_COUNTER for CTR-mode.
const cipher_info_t * cipher_info
Information about the associated cipher.
int(* cfb_func)(void *ctx, operation_t mode, size_t length, size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output)
Encrypt using CFB (Full length)
const cipher_info_t aes_128_cbc_info
int(* ctr_func)(void *ctx, size_t length, size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block, const unsigned char *input, unsigned char *output)
Encrypt using CTR.
operation_t operation
Operation that the context's key has been initialised for.
const cipher_info_t des_ede_cbc_info
size_t unprocessed_len
Number of bytes that still need processing.
unsigned char unprocessed_data[POLARSSL_MAX_IV_LENGTH]
Buffer for data that hasn't been encrypted yet.
int cipher_free_ctx(cipher_context_t *ctx)
Free the cipher-specific context of ctx.
int cipher_update(cipher_context_t *ctx, const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen)
Generic cipher update function.
int(* setkey_dec_func)(void *ctx, const unsigned char *key, unsigned int key_length)
Set key for decryption purposes.
const cipher_info_t camellia_256_cfb128_info
#define POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
Decryption of block requires a full block.
const cipher_info_t camellia_256_cbc_info
const cipher_info_t camellia_128_cbc_info
int cipher_reset(cipher_context_t *ctx, const unsigned char *iv)
Reset the given context, setting the IV to iv.
const cipher_info_t aes_192_cbc_info
int(* setkey_enc_func)(void *ctx, const unsigned char *key, unsigned int key_length)
Set key for encryption purposes.
void *(* ctx_alloc_func)(void)
Allocate a new context.
cipher_mode_t mode
Cipher mode (e.g.
int cipher_init_ctx(cipher_context_t *ctx, const cipher_info_t *cipher_info)
Initialises and fills the cipher context structure with the appropriate values.
int cipher_setkey(cipher_context_t *ctx, const unsigned char *key, int key_length, const operation_t operation)
Set the key to use with the given context.
const cipher_info_t aes_256_cfb128_info
const cipher_info_t aes_192_cfb128_info
const cipher_base_t * base
Base cipher information and functions.
const int * cipher_list(void)
Returns the list of ciphers supported by the generic cipher module.
void * cipher_ctx
Cipher-specific context.
int cipher_self_test(int verbose)
Checkup routine.
const cipher_info_t camellia_192_cfb128_info
const cipher_info_t camellia_128_cfb128_info