1
0
mirror of https://github.com/Mbed-TLS/mbedtls.git synced 2025-08-08 17:42:09 +03:00

Merged new cipher layer enhancements

This commit is contained in:
Paul Bakker
2013-09-04 16:12:55 +02:00
12 changed files with 449 additions and 95 deletions

View File

@@ -67,6 +67,12 @@ static const int supported_ciphers[] = {
POLARSSL_CIPHER_AES_256_CTR,
#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
#if defined(POLARSSL_GCM_C)
POLARSSL_CIPHER_AES_128_GCM,
POLARSSL_CIPHER_AES_192_GCM,
POLARSSL_CIPHER_AES_256_GCM,
#endif /* defined(POLARSSL_GCM_C) */
#endif /* defined(POLARSSL_AES_C) */
#if defined(POLARSSL_ARC4_C)
@@ -157,6 +163,8 @@ const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type )
#if defined(POLARSSL_GCM_C)
case POLARSSL_CIPHER_AES_128_GCM:
return &aes_128_gcm_info;
case POLARSSL_CIPHER_AES_192_GCM:
return &aes_192_gcm_info;
case POLARSSL_CIPHER_AES_256_GCM:
return &aes_256_gcm_info;
#endif /* defined(POLARSSL_GCM_C) */
@@ -293,6 +301,8 @@ const cipher_info_t *cipher_info_from_string( const char *cipher_name )
#if defined(POLARSSL_GCM_C)
if( !strcasecmp( "AES-128-GCM", cipher_name ) )
return cipher_info_from_type( POLARSSL_CIPHER_AES_128_GCM );
if( !strcasecmp( "AES-192-GCM", cipher_name ) )
return cipher_info_from_type( POLARSSL_CIPHER_AES_192_GCM );
if( !strcasecmp( "AES-256-GCM", cipher_name ) )
return cipher_info_from_type( POLARSSL_CIPHER_AES_256_GCM );
#endif
@@ -396,31 +406,60 @@ int cipher_setkey( cipher_context_t *ctx, const unsigned char *key,
return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
}
int cipher_reset( cipher_context_t *ctx,
const unsigned char *iv, size_t iv_len,
const unsigned char *ad, size_t ad_len )
int cipher_set_iv( cipher_context_t *ctx,
const unsigned char *iv, size_t iv_len )
{
size_t actual_iv_size;
if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
if( ctx->cipher_info->accepts_variable_iv_size )
actual_iv_size = iv_len;
else
actual_iv_size = ctx->cipher_info->iv_size;
memcpy( ctx->iv, iv, actual_iv_size );
ctx->iv_size = actual_iv_size;
return 0;
}
int cipher_reset( cipher_context_t *ctx )
{
if( NULL == ctx || NULL == ctx->cipher_info )
return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
ctx->unprocessed_len = 0;
return 0;
}
#if defined(POLARSSL_CIPHER_MODE_AEAD)
int cipher_update_ad( cipher_context_t *ctx,
const unsigned char *ad, size_t ad_len )
{
if( NULL == ctx || NULL == ctx->cipher_info )
return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
#if defined(POLARSSL_GCM_C)
if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
{
return gcm_starts( ctx->cipher_ctx, ctx->operation,
iv, iv_len, ad, ad_len );
}
#else
((void) ad);
((void) ad_len);
((void) iv_len);
#endif
/* Make sure we're called right after cipher_reset() */
if( ((gcm_context *) ctx->cipher_ctx)->len != 0 ||
((gcm_context *) ctx->cipher_ctx)->add_len != 0 )
{
return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
}
memcpy( ctx->iv, iv, cipher_get_iv_size( ctx ) );
return gcm_starts( ctx->cipher_ctx, ctx->operation,
ctx->iv, ctx->iv_size, ad, ad_len );
}
#endif
return 0;
}
#endif /* POLARSSL_CIPHER_MODE_AEAD */
int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ilen,
unsigned char *output, size_t *olen )
@@ -747,8 +786,7 @@ static int get_no_padding( unsigned char *input, size_t input_len,
}
int cipher_finish( cipher_context_t *ctx,
unsigned char *output, size_t *olen,
unsigned char *tag, size_t tag_len )
unsigned char *output, size_t *olen )
{
int ret = 0;
@@ -767,10 +805,6 @@ int cipher_finish( cipher_context_t *ctx,
#if defined(POLARSSL_GCM_C)
if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
{
unsigned char check_tag[16];
size_t i;
int diff;
if( 0 != ( ret = gcm_update( ctx->cipher_ctx,
ctx->unprocessed_len, ctx->unprocessed_data,
output ) ) )
@@ -780,29 +814,8 @@ int cipher_finish( cipher_context_t *ctx,
*olen += ctx->unprocessed_len;
if( 0 != ( ret = gcm_finish( ctx->cipher_ctx, check_tag, tag_len ) ) )
return( ret );
/* On encryption, write the tag */
if( POLARSSL_ENCRYPT == ctx->operation )
{
if( tag_len != 0 )
memcpy( tag, check_tag, tag_len );
return( 0 );
}
/* On decryption, check the tag (in "constant-time") */
for( diff = 0, i = 0; i < tag_len; i++ )
diff |= tag[i] ^ check_tag[i];
if( diff != 0 )
return( POLARSSL_ERR_GCM_AUTH_FAILED );
return( 0 );
}
#else
((void) tag);
((void) tag_len);
#endif
if( POLARSSL_MODE_CBC == ctx->cipher_info->mode )
@@ -900,6 +913,63 @@ int cipher_set_padding_mode( cipher_context_t *ctx, cipher_padding_t mode )
return 0;
}
#if defined(POLARSSL_CIPHER_MODE_AEAD)
int cipher_write_tag( cipher_context_t *ctx,
unsigned char *tag, size_t tag_len )
{
if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
if( POLARSSL_ENCRYPT != ctx->operation )
return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
#if defined(POLARSSL_GCM_C)
if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
return gcm_finish( ctx->cipher_ctx, tag, tag_len );
#endif
return 0;
}
int cipher_check_tag( cipher_context_t *ctx,
const unsigned char *tag, size_t tag_len )
{
int ret;
if( NULL == ctx || NULL == ctx->cipher_info ||
POLARSSL_DECRYPT != ctx->operation )
{
return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
}
#if defined(POLARSSL_GCM_C)
if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
{
unsigned char check_tag[16];
size_t i;
int diff;
if( tag_len > sizeof( check_tag ) )
return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
if( 0 != ( ret = gcm_finish( ctx->cipher_ctx, check_tag, tag_len ) ) )
return( ret );
/* Check the tag in "constant-time" */
for( diff = 0, i = 0; i < tag_len; i++ )
diff |= tag[i] ^ check_tag[i];
if( diff != 0 )
return( POLARSSL_ERR_GCM_AUTH_FAILED );
return( 0 );
}
#endif
return( 0 );
}
#endif /* POLARSSL_CIPHER_MODE_AEAD */
#if defined(POLARSSL_SELF_TEST)
#include <stdio.h>

View File

@@ -150,6 +150,7 @@ const cipher_info_t aes_128_cbc_info = {
128,
"AES-128-CBC",
16,
0,
16,
&aes_info
};
@@ -160,6 +161,7 @@ const cipher_info_t aes_192_cbc_info = {
192,
"AES-192-CBC",
16,
0,
16,
&aes_info
};
@@ -170,6 +172,7 @@ const cipher_info_t aes_256_cbc_info = {
256,
"AES-256-CBC",
16,
0,
16,
&aes_info
};
@@ -181,6 +184,7 @@ const cipher_info_t aes_128_cfb128_info = {
128,
"AES-128-CFB128",
16,
0,
16,
&aes_info
};
@@ -191,6 +195,7 @@ const cipher_info_t aes_192_cfb128_info = {
192,
"AES-192-CFB128",
16,
0,
16,
&aes_info
};
@@ -201,6 +206,7 @@ const cipher_info_t aes_256_cfb128_info = {
256,
"AES-256-CFB128",
16,
0,
16,
&aes_info
};
@@ -213,6 +219,7 @@ const cipher_info_t aes_128_ctr_info = {
128,
"AES-128-CTR",
16,
0,
16,
&aes_info
};
@@ -223,6 +230,7 @@ const cipher_info_t aes_192_ctr_info = {
192,
"AES-192-CTR",
16,
0,
16,
&aes_info
};
@@ -233,6 +241,7 @@ const cipher_info_t aes_256_ctr_info = {
256,
"AES-256-CTR",
16,
0,
16,
&aes_info
};
@@ -271,7 +280,19 @@ const cipher_info_t aes_128_gcm_info = {
POLARSSL_MODE_GCM,
128,
"AES-128-GCM",
0,
12,
1,
16,
&gcm_aes_info
};
const cipher_info_t aes_192_gcm_info = {
POLARSSL_CIPHER_AES_192_GCM,
POLARSSL_MODE_GCM,
192,
"AES-192-GCM",
12,
1,
16,
&gcm_aes_info
};
@@ -281,7 +302,8 @@ const cipher_info_t aes_256_gcm_info = {
POLARSSL_MODE_GCM,
256,
"AES-256-GCM",
0,
12,
1,
16,
&gcm_aes_info
};
@@ -373,6 +395,7 @@ const cipher_info_t camellia_128_cbc_info = {
128,
"CAMELLIA-128-CBC",
16,
0,
16,
&camellia_info
};
@@ -383,6 +406,7 @@ const cipher_info_t camellia_192_cbc_info = {
192,
"CAMELLIA-192-CBC",
16,
0,
16,
&camellia_info
};
@@ -393,6 +417,7 @@ const cipher_info_t camellia_256_cbc_info = {
256,
"CAMELLIA-256-CBC",
16,
0,
16,
&camellia_info
};
@@ -404,6 +429,7 @@ const cipher_info_t camellia_128_cfb128_info = {
128,
"CAMELLIA-128-CFB128",
16,
0,
16,
&camellia_info
};
@@ -414,6 +440,7 @@ const cipher_info_t camellia_192_cfb128_info = {
192,
"CAMELLIA-192-CFB128",
16,
0,
16,
&camellia_info
};
@@ -424,6 +451,7 @@ const cipher_info_t camellia_256_cfb128_info = {
256,
"CAMELLIA-256-CFB128",
16,
0,
16,
&camellia_info
};
@@ -436,6 +464,7 @@ const cipher_info_t camellia_128_ctr_info = {
128,
"CAMELLIA-128-CTR",
16,
0,
16,
&camellia_info
};
@@ -446,6 +475,7 @@ const cipher_info_t camellia_192_ctr_info = {
192,
"CAMELLIA-192-CTR",
16,
0,
16,
&camellia_info
};
@@ -456,6 +486,7 @@ const cipher_info_t camellia_256_ctr_info = {
256,
"CAMELLIA-256-CTR",
16,
0,
16,
&camellia_info
};
@@ -581,6 +612,7 @@ const cipher_info_t des_cbc_info = {
POLARSSL_KEY_LENGTH_DES,
"DES-CBC",
8,
0,
8,
&des_info
};
@@ -603,6 +635,7 @@ const cipher_info_t des_ede_cbc_info = {
POLARSSL_KEY_LENGTH_DES_EDE,
"DES-EDE-CBC",
8,
0,
8,
&des_ede_info
};
@@ -625,6 +658,7 @@ const cipher_info_t des_ede3_cbc_info = {
POLARSSL_KEY_LENGTH_DES_EDE3,
"DES-EDE3-CBC",
8,
0,
8,
&des_ede3_info
};
@@ -709,6 +743,7 @@ const cipher_info_t blowfish_cbc_info = {
128,
"BLOWFISH-CBC",
8,
0,
8,
&blowfish_info
};
@@ -720,6 +755,7 @@ const cipher_info_t blowfish_cfb64_info = {
128,
"BLOWFISH-CFB64",
8,
0,
8,
&blowfish_info
};
@@ -732,6 +768,7 @@ const cipher_info_t blowfish_ctr_info = {
128,
"BLOWFISH-CTR",
8,
0,
8,
&blowfish_info
};
@@ -749,7 +786,11 @@ static int arc4_crypt_stream_wrap( void *ctx, size_t length,
static int arc4_setkey_wrap( void *ctx, const unsigned char *key,
unsigned int key_length )
{
arc4_setup( (arc4_context *) ctx, key, key_length );
/* we get key_length in bits, arc4 expects it in bytes */
if( key_length % 8 != 0)
return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
arc4_setup( (arc4_context *) ctx, key, key_length / 8 );
return( 0 );
}
@@ -781,6 +822,7 @@ const cipher_info_t arc4_128_info = {
128,
"ARC4-128",
0,
0,
1,
&arc4_base_info
};
@@ -834,6 +876,7 @@ const cipher_info_t null_cipher_info = {
0,
"NULL",
0,
0,
1,
&null_base_info
};

View File

@@ -184,7 +184,10 @@ int pkcs12_pbe( asn1_buf *pbe_params, int mode,
if( ( ret = cipher_setkey( &cipher_ctx, key, keylen, mode ) ) != 0 )
goto exit;
if( ( ret = cipher_reset( &cipher_ctx, iv, 0, NULL, 0 ) ) != 0 )
if( ( ret = cipher_set_iv( &cipher_ctx, iv, cipher_info->iv_size ) ) != 0 )
goto exit;
if( ( ret = cipher_reset( &cipher_ctx ) ) != 0 )
goto exit;
if( ( ret = cipher_update( &cipher_ctx, data, len,
@@ -193,11 +196,8 @@ int pkcs12_pbe( asn1_buf *pbe_params, int mode,
goto exit;
}
if( ( ret = cipher_finish( &cipher_ctx, output + olen, &olen, NULL, 0 ) )
!= 0 )
{
if( ( ret = cipher_finish( &cipher_ctx, output + olen, &olen ) ) != 0 )
ret = POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH;
}
exit:
cipher_free_ctx( &cipher_ctx );

View File

@@ -187,7 +187,10 @@ int pkcs5_pbes2( asn1_buf *pbe_params, int mode,
if( ( ret = cipher_setkey( &cipher_ctx, key, keylen, mode ) ) != 0 )
goto exit;
if( ( ret = cipher_reset( &cipher_ctx, iv, 0, NULL, 0 ) ) != 0 )
if( ( ret = cipher_set_iv( &cipher_ctx, iv, enc_scheme_params.len ) ) != 0 )
goto exit;
if( ( ret = cipher_reset( &cipher_ctx ) ) != 0 )
goto exit;
if( ( ret = cipher_update( &cipher_ctx, data, datalen,
@@ -196,11 +199,8 @@ int pkcs5_pbes2( asn1_buf *pbe_params, int mode,
goto exit;
}
if( ( ret = cipher_finish( &cipher_ctx, output + olen, &olen, NULL, 0 ) )
!= 0 )
{
if( ( ret = cipher_finish( &cipher_ctx, output + olen, &olen ) ) != 0 )
ret = POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH;
}
exit:
md_free_ctx( &md_ctx );