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
48 #define strcasecmp _stricmp
51 static int supported_init = 0;
58 if( ! supported_init )
63 while( def->
type != 0 )
64 *type++ = (*def++).type;
79 if( def->
type == cipher_type )
89 if( NULL == cipher_name )
93 if( ! strcasecmp( def->
info->
name, cipher_name ) )
116 if( NULL == cipher_info || NULL == ctx )
126 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
130 #if defined(POLARSSL_CIPHER_PADDING_PKCS7)
181 const unsigned char *iv,
size_t iv_len )
183 size_t actual_iv_size;
185 if( NULL == ctx || NULL == ctx->
cipher_info || NULL == iv )
189 actual_iv_size = iv_len;
193 memcpy( ctx->
iv, iv, actual_iv_size );
209 #if defined(POLARSSL_CIPHER_MODE_AEAD)
211 const unsigned char *ad,
size_t ad_len )
216 #if defined(POLARSSL_GCM_C)
229 unsigned char *output,
size_t *olen )
235 if( NULL == ctx || NULL == ctx->
cipher_info || NULL == olen )
256 #if defined(POLARSSL_GCM_C)
265 if( input == output &&
271 #if defined(POLARSSL_CIPHER_MODE_CBC)
350 #if defined(POLARSSL_CIPHER_MODE_CFB)
366 #if defined(POLARSSL_CIPHER_MODE_CTR)
382 #if defined(POLARSSL_CIPHER_MODE_STREAM)
386 ilen, input, output ) ) )
400 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
401 #if defined(POLARSSL_CIPHER_PADDING_PKCS7)
405 static void add_pkcs_padding(
unsigned char *output,
size_t output_len,
408 size_t padding_len = output_len - data_len;
411 for( i = 0; i < padding_len; i++ )
412 output[data_len + i] = (
unsigned char) padding_len;
415 static int get_pkcs_padding(
unsigned char *input,
size_t input_len,
418 size_t i, padding_len = 0;
420 if( NULL == input || NULL == data_len )
423 padding_len = input[input_len - 1];
425 if( padding_len > input_len || padding_len == 0 )
428 for( i = input_len - padding_len; i < input_len; i++ )
429 if( input[i] != padding_len )
432 *data_len = input_len - padding_len;
438 #if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS)
442 static void add_one_and_zeros_padding(
unsigned char *output,
443 size_t output_len,
size_t data_len )
445 size_t padding_len = output_len - data_len;
448 output[data_len] = 0x80;
449 for( i = 1; i < padding_len; i++ )
450 output[data_len + i] = 0x00;
453 static int get_one_and_zeros_padding(
unsigned char *input,
size_t input_len,
456 unsigned char *p = input + input_len - 1;
458 if( NULL == input || NULL == data_len )
461 while( *p == 0x00 && p > input )
467 *data_len = p - input;
473 #if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN)
477 static void add_zeros_and_len_padding(
unsigned char *output,
478 size_t output_len,
size_t data_len )
480 size_t padding_len = output_len - data_len;
483 for( i = 1; i < padding_len; i++ )
484 output[data_len + i - 1] = 0x00;
485 output[output_len - 1] = (
unsigned char) padding_len;
488 static int get_zeros_and_len_padding(
unsigned char *input,
size_t input_len,
491 size_t i, padding_len = 0;
493 if( NULL == input || NULL == data_len )
496 padding_len = input[input_len - 1];
498 if( padding_len > input_len || padding_len == 0 )
501 for( i = input_len - padding_len; i < input_len - 1; i++ )
502 if( input[i] != 0x00 )
505 *data_len = input_len - padding_len;
511 #if defined(POLARSSL_CIPHER_PADDING_ZEROS)
515 static void add_zeros_padding(
unsigned char *output,
516 size_t output_len,
size_t data_len )
520 for( i = data_len; i < output_len; i++ )
524 static int get_zeros_padding(
unsigned char *input,
size_t input_len,
527 unsigned char *p = input + input_len - 1;
528 if( NULL == input || NULL == data_len )
531 while( *p == 0x00 && p > input )
534 *data_len = *p == 0x00 ? 0 : p - input + 1;
546 static int get_no_padding(
unsigned char *input,
size_t input_len,
549 if( NULL == input || NULL == data_len )
552 *data_len = input_len;
559 unsigned char *output,
size_t *olen )
561 if( NULL == ctx || NULL == ctx->
cipher_info || NULL == olen )
582 #if defined(POLARSSL_CIPHER_MODE_CBC)
637 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
648 #if defined(POLARSSL_CIPHER_PADDING_PKCS7)
654 #if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS)
660 #if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN)
666 #if defined(POLARSSL_CIPHER_PADDING_ZEROS)
685 #if defined(POLARSSL_CIPHER_MODE_AEAD)
687 unsigned char *tag,
size_t tag_len )
689 if( NULL == ctx || NULL == ctx->
cipher_info || NULL == tag )
695 #if defined(POLARSSL_GCM_C)
704 const unsigned char *tag,
size_t tag_len )
714 #if defined(POLARSSL_GCM_C)
717 unsigned char check_tag[16];
721 if( tag_len >
sizeof( check_tag ) )
725 check_tag, tag_len ) ) )
731 for( diff = 0, i = 0; i < tag_len; i++ )
732 diff |= tag[i] ^ check_tag[i];
745 #if defined(POLARSSL_SELF_TEST)
749 #define ASSERT(x) if (!(x)) { \
750 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.
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_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.
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.
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