From e55e103bfe31855aa2a35212f4c670371b384b2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Tue, 18 Dec 2018 12:09:02 +0100 Subject: [PATCH] Fix off-by-one in iv_off check and add tests --- library/aes.c | 4 ++-- tests/suites/test_suite_aes.function | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/library/aes.c b/library/aes.c index 1c743f95d..0543cd781 100644 --- a/library/aes.c +++ b/library/aes.c @@ -1298,7 +1298,7 @@ int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx, n = *iv_off; - if( n > 16 ) + if( n > 15 ) return( MBEDTLS_ERR_AES_BAD_INPUT_DATA ); if( mode == MBEDTLS_AES_DECRYPT ) @@ -1394,7 +1394,7 @@ int mbedtls_aes_crypt_ofb( mbedtls_aes_context *ctx, n = *iv_off; - if( n > 16 ) + if( n > 15 ) return( MBEDTLS_ERR_AES_BAD_INPUT_DATA ); while( length-- ) diff --git a/tests/suites/test_suite_aes.function b/tests/suites/test_suite_aes.function index 3762ba4ec..f74183d8e 100644 --- a/tests/suites/test_suite_aes.function +++ b/tests/suites/test_suite_aes.function @@ -569,6 +569,7 @@ void aes_misc_params( ) #endif const unsigned char in[16] = { 0 }; unsigned char out[16]; + size_t size; /* These calls accept NULL */ TEST_VALID_PARAM( mbedtls_aes_free( NULL ) ); @@ -597,6 +598,19 @@ void aes_misc_params( ) in, in, out ) == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH ); #endif + +#if defined(MBEDTLS_CIPHER_MODE_CFB) + size = 16; + TEST_ASSERT( mbedtls_aes_crypt_cfb128( &aes_ctx, MBEDTLS_AES_ENCRYPT, 16, + &size, out, in, out ) + == MBEDTLS_ERR_AES_BAD_INPUT_DATA ); +#endif + +#if defined(MBEDTLS_CIPHER_MODE_OFB) + size = 16; + TEST_ASSERT( mbedtls_aes_crypt_ofb( &aes_ctx, 16, &size, out, in, out ) + == MBEDTLS_ERR_AES_BAD_INPUT_DATA ); +#endif } /* END_CASE */