-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Return an error from mbedtls_cipher_set_iv
for an invalid IV length with ChaCha20 and ChaCha20+Poly
#5253
Return an error from mbedtls_cipher_set_iv
for an invalid IV length with ChaCha20 and ChaCha20+Poly
#5253
Changes from all commits
33ca6af
63439ed
8be8e4a
f2d4e27
b9fbc11
ad2b8b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Default behavior changes | ||
* mbedtls_cipher_set_iv will now fail with ChaCha20 and ChaCha20+Poly1305 | ||
for IV lengths other than 12. The library was silently overwriting this | ||
length with 12, but did not inform the caller about it. Fixes #4301. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -442,6 +442,9 @@ void enc_dec_buf( int cipher_id, char * cipher_string, int key_len, | |
if( NULL != strstr( cipher_info->name, "CCM*-NO-TAG") ) | ||
iv_len = 13; /* For CCM, IV length is expected to be between 7 and 13 bytes. | ||
* For CCM*-NO-TAG, IV length must be exactly 13 bytes long. */ | ||
else if( cipher_info->type == MBEDTLS_CIPHER_CHACHA20 || | ||
cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 ) | ||
iv_len = 12; | ||
else | ||
iv_len = sizeof(iv); | ||
|
||
|
@@ -568,7 +571,9 @@ void dec_empty_buf( int cipher, | |
int expected_finish_ret ) | ||
{ | ||
unsigned char key[32]; | ||
unsigned char iv[16]; | ||
|
||
unsigned char *iv = NULL; | ||
size_t iv_len = 16; | ||
|
||
mbedtls_cipher_context_t ctx_dec; | ||
const mbedtls_cipher_info_t *cipher_info; | ||
|
@@ -579,7 +584,6 @@ void dec_empty_buf( int cipher, | |
size_t outlen = 0; | ||
|
||
memset( key, 0, 32 ); | ||
memset( iv , 0, 16 ); | ||
|
||
mbedtls_cipher_init( &ctx_dec ); | ||
|
||
|
@@ -589,6 +593,14 @@ void dec_empty_buf( int cipher, | |
/* Initialise context */ | ||
cipher_info = mbedtls_cipher_info_from_type( cipher ); | ||
TEST_ASSERT( NULL != cipher_info); | ||
|
||
if( cipher_info->type == MBEDTLS_CIPHER_CHACHA20 || | ||
cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 ) | ||
iv_len = 12; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand that 16 byes for iv is default (and max) length and for those two cipher types we are only limiting the length, but maybe we could first determine the iv length and then allocate memory? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done that :) It has an additional advantage as described in my comment below. |
||
|
||
ASSERT_ALLOC( iv, iv_len ); | ||
gabor-mezei-arm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
memset( iv , 0, iv_len ); | ||
|
||
TEST_ASSERT( sizeof(key) * 8 >= cipher_info->key_bitlen ); | ||
|
||
TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) ); | ||
|
@@ -597,7 +609,7 @@ void dec_empty_buf( int cipher, | |
key, cipher_info->key_bitlen, | ||
MBEDTLS_DECRYPT ) ); | ||
|
||
TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, 16 ) ); | ||
TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, iv_len ) ); | ||
|
||
TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) ); | ||
|
||
|
@@ -627,6 +639,7 @@ void dec_empty_buf( int cipher, | |
TEST_ASSERT( 0 == outlen ); | ||
|
||
exit: | ||
mbedtls_free( iv ); | ||
mbedtls_cipher_free( &ctx_dec ); | ||
} | ||
/* END_CASE */ | ||
|
@@ -689,6 +702,9 @@ void enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val, | |
if( NULL != strstr( cipher_info->name, "CCM*-NO-TAG") ) | ||
iv_len = 13; /* For CCM, IV length is expected to be between 7 and 13 bytes. | ||
* For CCM*-NO-TAG, IV length must be exactly 13 bytes long. */ | ||
else if( cipher_info->type == MBEDTLS_CIPHER_CHACHA20 || | ||
cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 ) | ||
iv_len = 12; | ||
else | ||
iv_len = sizeof(iv); | ||
|
||
|
@@ -1130,3 +1146,40 @@ void check_padding( int pad_mode, data_t * input, int ret, int dlen_check | |
TEST_ASSERT( dlen == (size_t) dlen_check ); | ||
} | ||
/* END_CASE */ | ||
|
||
/* BEGIN_CASE */ | ||
void check_iv( int cipher_id, char * cipher_string, | ||
int iv_len_val, int ret ) | ||
{ | ||
size_t iv_len = iv_len_val; | ||
unsigned char iv[16]; | ||
|
||
const mbedtls_cipher_info_t *cipher_info; | ||
mbedtls_cipher_context_t ctx_dec; | ||
mbedtls_cipher_context_t ctx_enc; | ||
|
||
/* | ||
* Prepare contexts | ||
*/ | ||
mbedtls_cipher_init( &ctx_dec ); | ||
mbedtls_cipher_init( &ctx_enc ); | ||
|
||
/* Check and get info structures */ | ||
cipher_info = mbedtls_cipher_info_from_type( cipher_id ); | ||
TEST_ASSERT( NULL != cipher_info ); | ||
TEST_ASSERT( mbedtls_cipher_info_from_string( cipher_string ) == cipher_info ); | ||
TEST_ASSERT( strcmp( mbedtls_cipher_info_get_name( cipher_info ), | ||
cipher_string ) == 0 ); | ||
|
||
/* Initialise enc and dec contexts */ | ||
TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) ); | ||
TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) ); | ||
|
||
TEST_ASSERT( ret == mbedtls_cipher_set_iv( &ctx_dec, iv, iv_len ) ); | ||
TEST_ASSERT( ret == mbedtls_cipher_set_iv( &ctx_enc, iv, iv_len ) ); | ||
|
||
exit: | ||
mbedtls_cipher_free( &ctx_dec ); | ||
mbedtls_cipher_free( &ctx_enc ); | ||
} | ||
/* END_CASE */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use macro also on success.