cipher_auth_xxcrypt(): fix some null pointer handling
Make sure that if a buffer is allowed to be empty, a null pointer is
accepted if the buffer length is 0. This was already the case for most
but not all arguments to mbedtls_cipher_auth_{en,de}crypt{,_ext}.
Make sure to pass NULL for an empty buffer in the tests.
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
diff --git a/tests/suites/test_suite_cipher.function b/tests/suites/test_suite_cipher.function
index 47a763c..ffe3284 100644
--- a/tests/suites/test_suite_cipher.function
+++ b/tests/suites/test_suite_cipher.function
@@ -1155,6 +1155,16 @@
unsigned char *tag_buf = NULL;
#endif /* !MBEDTLS_DEPRECATED_WARNING && !MBEDTLS_DEPRECATED_REMOVED */
+ /* Null pointers are documented as valid for inputs of length 0.
+ * The test framework passes non-null pointers, so set them to NULL.
+ * key, cipher and tag can't be empty. */
+ if( iv->len == 0 )
+ iv->x = NULL;
+ if( ad->len == 0 )
+ ad->x = NULL;
+ if( clear->len == 0 )
+ clear->x = NULL;
+
mbedtls_cipher_init( &ctx );
/* Initialize PSA Crypto */
@@ -1345,8 +1355,7 @@
* Authenticate and decrypt, and check result
*/
- /* We can't pass a NULL output buffer to this function */
- ASSERT_ALLOC( decrypt_buf, cipher->len ? cipher->len : 1 );
+ ASSERT_ALLOC( decrypt_buf, cipher->len );
outlen = 0;
ret = mbedtls_cipher_auth_decrypt( &ctx, iv->x, iv->len, ad->x, ad->len,
tmp_cipher, cipher->len, decrypt_buf, &outlen,
@@ -1397,8 +1406,7 @@
else
#endif /* MBEDTLS_USE_PSA_CRYPTO */
{
- /* can't pass a NULL output buffer to this function */
- ASSERT_ALLOC( encrypt_buf, cipher->len ? cipher->len : 1 );
+ ASSERT_ALLOC( encrypt_buf, cipher->len );
ASSERT_ALLOC( tag_buf, tag->len );
tmp_cipher = encrypt_buf;
tmp_tag = tag_buf;
@@ -1421,7 +1429,8 @@
TEST_ASSERT( ret == 0 );
TEST_ASSERT( outlen == cipher->len );
- TEST_ASSERT( memcmp( tmp_cipher, cipher->x, cipher->len ) == 0 );
+ if( cipher->len != 0 )
+ TEST_ASSERT( memcmp( tmp_cipher, cipher->x, cipher->len ) == 0 );
TEST_ASSERT( memcmp( tmp_tag, tag->x, tag->len ) == 0 );
}
}