diff --git a/library/cipher.c b/library/cipher.c index da5520651..d7acf34ee 100644 --- a/library/cipher.c +++ b/library/cipher.c @@ -242,6 +242,9 @@ int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx, else if( NULL == iv && iv_len != 0 ) return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); + if( NULL == iv && iv_len == 0 ) + ctx->iv_size = 0; + /* avoid buffer overflow in ctx->iv */ if( iv_len > MBEDTLS_MAX_IV_LENGTH ) return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE ); @@ -269,7 +272,7 @@ int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx, } #endif - if ( actual_iv_size ) + if ( actual_iv_size != 0 ) { memcpy( ctx->iv, iv, actual_iv_size ); ctx->iv_size = actual_iv_size; diff --git a/tests/suites/test_suite_cipher.function b/tests/suites/test_suite_cipher.function index c0aa831ea..1536c9d31 100644 --- a/tests/suites/test_suite_cipher.function +++ b/tests/suites/test_suite_cipher.function @@ -106,7 +106,7 @@ void cipher_special_behaviours( ) mbedtls_cipher_init( &ctx ); memset( input, 0, sizeof( input ) ); memset( output, 0, sizeof( output ) ); -#if defined (MBEDTLS_CIPHER_MODE_CBC) +#if defined(MBEDTLS_CIPHER_MODE_CBC) memset( iv, 0, sizeof( iv ) ); /* Check and get info structures */ @@ -124,6 +124,7 @@ void cipher_special_behaviours( ) == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); mbedtls_cipher_free( &ctx ); + mbedtls_cipher_init( &ctx ); #endif /* MBEDTLS_CIPHER_MODE_CBC */ cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_ECB ); TEST_ASSERT( NULL != cipher_info );