32 #if defined(POLARSSL_CIPHER_C)
37 #if defined(POLARSSL_GCM_C)
43 #if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
44 #define POLARSSL_CIPHER_MODE_STREAM
47 #if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
49 #define strcasecmp _stricmp
52 static int supported_init = 0;
59 if( ! supported_init )
64 while( def->
type != 0 )
65 *type++ = (*def++).type;
80 if( def->
type == cipher_type )
90 if( NULL == cipher_name )
94 if( ! strcasecmp( def->
info->
name, cipher_name ) )
117 if( NULL == cipher_info || NULL == ctx )
127 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
131 #if defined(POLARSSL_CIPHER_PADDING_PKCS7)
182 const unsigned char *iv,
size_t iv_len )
184 size_t actual_iv_size;
186 if( NULL == ctx || NULL == ctx->
cipher_info || NULL == iv )
194 actual_iv_size = iv_len;
200 if( actual_iv_size > iv_len )
204 memcpy( ctx->
iv, iv, actual_iv_size );
220 #if defined(POLARSSL_CIPHER_MODE_AEAD)
222 const unsigned char *ad,
size_t ad_len )
227 #if defined(POLARSSL_GCM_C)
240 unsigned char *output,
size_t *olen )
244 if( NULL == ctx || NULL == ctx->
cipher_info || NULL == olen )
267 #if defined(POLARSSL_GCM_C)
276 if( input == output &&
282 #if defined(POLARSSL_CIPHER_MODE_CBC)
361 #if defined(POLARSSL_CIPHER_MODE_CFB)
377 #if defined(POLARSSL_CIPHER_MODE_CTR)
393 #if defined(POLARSSL_CIPHER_MODE_STREAM)
397 ilen, input, output ) ) )
411 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
412 #if defined(POLARSSL_CIPHER_PADDING_PKCS7)
416 static void add_pkcs_padding(
unsigned char *output,
size_t output_len,
419 size_t padding_len = output_len - data_len;
422 for( i = 0; i < padding_len; i++ )
423 output[data_len + i] = (
unsigned char) padding_len;
426 static int get_pkcs_padding(
unsigned char *input,
size_t input_len,
430 unsigned char padding_len, bad = 0;
432 if( NULL == input || NULL == data_len )
435 padding_len = input[input_len - 1];
436 *data_len = input_len - padding_len;
439 bad |= padding_len > input_len;
440 bad |= padding_len == 0;
444 pad_idx = input_len - padding_len;
445 for( i = 0; i < input_len; i++ )
446 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
452 #if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS)
456 static void add_one_and_zeros_padding(
unsigned char *output,
457 size_t output_len,
size_t data_len )
459 size_t padding_len = output_len - data_len;
462 output[data_len] = 0x80;
463 for( i = 1; i < padding_len; i++ )
464 output[data_len + i] = 0x00;
467 static int get_one_and_zeros_padding(
unsigned char *input,
size_t input_len,
471 unsigned char done = 0, prev_done, bad;
473 if( NULL == input || NULL == data_len )
478 for( i = input_len; i > 0; i-- )
481 done |= ( input[i-1] != 0 );
482 *data_len |= ( i - 1 ) * ( done != prev_done );
483 bad &= ( input[i-1] ^ 0x80 ) | ( done == prev_done );
491 #if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN)
495 static void add_zeros_and_len_padding(
unsigned char *output,
496 size_t output_len,
size_t data_len )
498 size_t padding_len = output_len - data_len;
501 for( i = 1; i < padding_len; i++ )
502 output[data_len + i - 1] = 0x00;
503 output[output_len - 1] = (
unsigned char) padding_len;
506 static int get_zeros_and_len_padding(
unsigned char *input,
size_t input_len,
510 unsigned char padding_len, bad = 0;
512 if( NULL == input || NULL == data_len )
515 padding_len = input[input_len - 1];
516 *data_len = input_len - padding_len;
519 bad |= padding_len > input_len;
520 bad |= padding_len == 0;
523 pad_idx = input_len - padding_len;
524 for( i = 0; i < input_len - 1; i++ )
525 bad |= input[i] * ( i >= pad_idx );
531 #if defined(POLARSSL_CIPHER_PADDING_ZEROS)
535 static void add_zeros_padding(
unsigned char *output,
536 size_t output_len,
size_t data_len )
540 for( i = data_len; i < output_len; i++ )
544 static int get_zeros_padding(
unsigned char *input,
size_t input_len,
548 unsigned char done = 0, prev_done;
550 if( NULL == input || NULL == data_len )
554 for( i = input_len; i > 0; i-- )
557 done |= ( input[i-1] != 0 );
558 *data_len |= i * ( done != prev_done );
571 static int get_no_padding(
unsigned char *input,
size_t input_len,
574 if( NULL == input || NULL == data_len )
577 *data_len = input_len;
584 unsigned char *output,
size_t *olen )
586 if( NULL == ctx || NULL == ctx->
cipher_info || NULL == olen )
607 #if defined(POLARSSL_CIPHER_MODE_CBC)
662 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
673 #if defined(POLARSSL_CIPHER_PADDING_PKCS7)
679 #if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS)
685 #if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN)
691 #if defined(POLARSSL_CIPHER_PADDING_ZEROS)
710 #if defined(POLARSSL_CIPHER_MODE_AEAD)
712 unsigned char *tag,
size_t tag_len )
714 if( NULL == ctx || NULL == ctx->
cipher_info || NULL == tag )
720 #if defined(POLARSSL_GCM_C)
729 const unsigned char *tag,
size_t tag_len )
739 #if defined(POLARSSL_GCM_C)
742 unsigned char check_tag[16];
746 if( tag_len >
sizeof( check_tag ) )
750 check_tag, tag_len ) ) )
756 for( diff = 0, i = 0; i < tag_len; i++ )
757 diff |= tag[i] ^ check_tag[i];
770 #if defined(POLARSSL_SELF_TEST)
774 #define ASSERT(x) if (!(x)) { \
775 printf( "failed with %i at %s\n", value, (#x) ); \
int key_length
Key length to use.
#define POLARSSL_ERR_CIPHER_BAD_INPUT_DATA
Bad input parameters to function.
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/NONCE in bytes.
#define POLARSSL_ERR_CIPHER_ALLOC_FAILED
Failed to allocate memory.
int cipher_write_tag(cipher_context_t *ctx, unsigned char *tag, size_t tag_len)
Write tag for AEAD ciphers.
zero padding (not reversible!)
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.
int(* get_padding)(unsigned char *input, size_t ilen, size_t *data_len)
Configuration options (set of defines)
void(* ctx_free_func)(void *ctx)
Free the given context.
#define POLARSSL_ERR_CIPHER_INVALID_PADDING
Input data contains invalid padding and is rejected.
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_definition_t cipher_definitions[]
int(* ecb_func)(void *ctx, operation_t mode, const unsigned char *input, unsigned char *output)
Encrypt using ECB.
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)
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.
size_t unprocessed_len
Number of bytes that still need processing.
int cipher_free_ctx(cipher_context_t *ctx)
Free the cipher-specific context of ctx.
int cipher_update_ad(cipher_context_t *ctx, const unsigned char *ad, size_t ad_len)
Add additional data (for AEAD ciphers).
unsigned int key_length
Cipher key length, in bits (default length for variable sized ciphers) (Includes parity bits for ciph...
int cipher_set_iv(cipher_context_t *ctx, const unsigned char *iv, size_t iv_len)
Set the initialization vector (IV) or nonce.
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.
unsigned char unprocessed_data[POLARSSL_MAX_BLOCK_LENGTH]
Buffer for data that hasn't been encrypted yet.
int(* stream_func)(void *ctx, size_t length, const unsigned char *input, unsigned char *output)
Encrypt using STREAM.
#define POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
Decryption of block requires a full block.
const char * name
Name of the cipher.
#define POLARSSL_MAX_IV_LENGTH
Maximum length of any IV, in bytes.
int cipher_reset(cipher_context_t *ctx)
Finish preparation of the given context.
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_id_t cipher
Base Cipher type (e.g.
int cipher_set_padding_mode(cipher_context_t *ctx, cipher_padding_t mode)
Set padding mode, for cipher modes that use padding.
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.
int gcm_update(gcm_context *ctx, size_t length, const unsigned char *input, unsigned char *output)
Generic GCM update function.
int gcm_starts(gcm_context *ctx, int mode, const unsigned char *iv, size_t iv_len, const unsigned char *add, size_t add_len)
Generic GCM stream start function.
never pad (full blocks only)
Galois/Counter mode for 128-bit block ciphers.
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.
int gcm_finish(gcm_context *ctx, unsigned char *tag, size_t tag_len)
Generic GCM finalisation function.
void * cipher_ctx
Cipher-specific context.
int cipher_self_test(int verbose)
Checkup routine.
void(* add_padding)(unsigned char *output, size_t olen, size_t data_len)
Padding functions to use, if relevant for cipher mode.
size_t iv_size
IV size in bytes (for ciphers with variable-length IVs)
int accepts_variable_iv_size
Flag for ciphers that accept many sizes of IV/NONCE.
int cipher_check_tag(cipher_context_t *ctx, const unsigned char *tag, size_t tag_len)
Check tag for AEAD ciphers.
unsigned int iv_size
IV/NONCE size, in bytes.
#define POLARSSL_ERR_CIPHER_AUTH_FAILED
Authentication failed (for AEAD modes).
const cipher_info_t * cipher_info_from_values(const cipher_id_t cipher_id, int key_length, const cipher_mode_t mode)
Returns the cipher information structure associated with the given cipher id, key size and mode...
const cipher_info_t * info