32 #if defined(POLARSSL_CIPHER_C)
39 #if defined _MSC_VER && !defined strcasecmp
40 #define strcasecmp _stricmp
43 static const int supported_ciphers[] = {
45 #if defined(POLARSSL_AES_C)
50 #if defined(POLARSSL_CIPHER_MODE_CFB)
56 #if defined(POLARSSL_CIPHER_MODE_CTR)
64 #if defined(POLARSSL_CAMELLIA_C)
69 #if defined(POLARSSL_CIPHER_MODE_CFB)
75 #if defined(POLARSSL_CIPHER_MODE_CTR)
83 #if defined(POLARSSL_DES_C)
89 #if defined(POLARSSL_BLOWFISH_C)
92 #if defined(POLARSSL_CIPHER_MODE_CFB)
96 #if defined(POLARSSL_CIPHER_MODE_CTR)
102 #if defined(POLARSSL_CIPHER_NULL_CIPHER)
111 return supported_ciphers;
117 switch ( cipher_type )
119 #if defined(POLARSSL_AES_C)
127 #if defined(POLARSSL_CIPHER_MODE_CFB)
136 #if defined(POLARSSL_CIPHER_MODE_CTR)
147 #if defined(POLARSSL_CAMELLIA_C)
155 #if defined(POLARSSL_CIPHER_MODE_CFB)
164 #if defined(POLARSSL_CIPHER_MODE_CTR)
175 #if defined(POLARSSL_DES_C)
184 #if defined(POLARSSL_BLOWFISH_C)
188 #if defined(POLARSSL_CIPHER_MODE_CFB)
193 #if defined(POLARSSL_CIPHER_MODE_CTR)
200 #if defined(POLARSSL_CIPHER_NULL_CIPHER)
202 return &null_cipher_info;
212 if( NULL == cipher_name )
216 #if defined(POLARSSL_CAMELLIA_C)
217 if( !strcasecmp(
"CAMELLIA-128-CBC", cipher_name ) )
219 if( !strcasecmp(
"CAMELLIA-192-CBC", cipher_name ) )
221 if( !strcasecmp(
"CAMELLIA-256-CBC", cipher_name ) )
224 #if defined(POLARSSL_CIPHER_MODE_CFB)
225 if( !strcasecmp(
"CAMELLIA-128-CFB128", cipher_name ) )
227 if( !strcasecmp(
"CAMELLIA-192-CFB128", cipher_name ) )
229 if( !strcasecmp(
"CAMELLIA-256-CFB128", cipher_name ) )
233 #if defined(POLARSSL_CIPHER_MODE_CTR)
234 if( !strcasecmp(
"CAMELLIA-128-CTR", cipher_name ) )
236 if( !strcasecmp(
"CAMELLIA-192-CTR", cipher_name ) )
238 if( !strcasecmp(
"CAMELLIA-256-CTR", cipher_name ) )
243 #if defined(POLARSSL_AES_C)
244 if( !strcasecmp(
"AES-128-CBC", cipher_name ) )
246 if( !strcasecmp(
"AES-192-CBC", cipher_name ) )
248 if( !strcasecmp(
"AES-256-CBC", cipher_name ) )
251 #if defined(POLARSSL_CIPHER_MODE_CFB)
252 if( !strcasecmp(
"AES-128-CFB128", cipher_name ) )
254 if( !strcasecmp(
"AES-192-CFB128", cipher_name ) )
256 if( !strcasecmp(
"AES-256-CFB128", cipher_name ) )
260 #if defined(POLARSSL_CIPHER_MODE_CTR)
261 if( !strcasecmp(
"AES-128-CTR", cipher_name ) )
263 if( !strcasecmp(
"AES-192-CTR", cipher_name ) )
265 if( !strcasecmp(
"AES-256-CTR", cipher_name ) )
270 #if defined(POLARSSL_DES_C)
271 if( !strcasecmp(
"DES-CBC", cipher_name ) )
273 if( !strcasecmp(
"DES-EDE-CBC", cipher_name ) )
275 if( !strcasecmp(
"DES-EDE3-CBC", cipher_name ) )
279 #if defined(POLARSSL_BLOWFISH_C)
280 if( !strcasecmp(
"BLOWFISH-CBC", cipher_name ) )
283 #if defined(POLARSSL_CIPHER_MODE_CFB)
284 if( !strcasecmp(
"BLOWFISH-CFB64", cipher_name ) )
288 #if defined(POLARSSL_CIPHER_MODE_CTR)
289 if( !strcasecmp(
"BLOWFISH-CTR", cipher_name ) )
294 #if defined(POLARSSL_CIPHER_NULL_CIPHER)
295 if( !strcasecmp(
"NULL", cipher_name ) )
304 if( NULL == cipher_info || NULL == ctx )
336 #if defined(POLARSSL_CIPHER_NULL_CIPHER)
361 if( NULL == ctx || NULL == ctx->
cipher_info || NULL == iv )
372 unsigned char *output,
size_t *olen )
377 if( NULL == ctx || NULL == ctx->
cipher_info || NULL == olen ||
385 #if defined(POLARSSL_CIPHER_NULL_CIPHER)
388 memcpy( output, input, ilen );
499 static void add_pkcs_padding(
unsigned char *output,
size_t output_len,
502 size_t padding_len = output_len - data_len;
505 for( i = 0; i < padding_len; i++ )
506 output[data_len + i] = (
unsigned char) padding_len;
509 static int get_pkcs_padding(
unsigned char *input,
unsigned int input_len,
512 unsigned int i, padding_len = 0;
514 if( NULL == input || NULL == data_len )
517 padding_len = input[input_len - 1];
519 if( padding_len > input_len )
522 for( i = input_len - padding_len; i < input_len; i++ )
523 if( input[i] != padding_len )
526 *data_len = input_len - padding_len;
535 if( NULL == ctx || NULL == ctx->
cipher_info || NULL == olen )
580 #if defined(POLARSSL_SELF_TEST)
584 #define ASSERT(x) if (!(x)) { \
585 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