From 5e7693f6ba36a68a20feb5cf55226961eee96360 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Fri, 13 Jun 2014 16:08:07 +0200 Subject: [PATCH] Add tests for bad arguments to cipher functions --- tests/suites/test_suite_cipher.function | 70 +++++++++++++++++++++ tests/suites/test_suite_cipher.padding.data | 3 + 2 files changed, 73 insertions(+) diff --git a/tests/suites/test_suite_cipher.function b/tests/suites/test_suite_cipher.function index 03be2b818f..09ae2e08a7 100644 --- a/tests/suites/test_suite_cipher.function +++ b/tests/suites/test_suite_cipher.function @@ -21,6 +21,76 @@ void cipher_list( ) } /* END_CASE */ +/* BEGIN_CASE */ +void cipher_null_args( ) +{ + cipher_context_t ctx; + const cipher_info_t *info = cipher_info_from_type( *( cipher_list() ) ); + unsigned char buf[1] = { 0 }; + size_t olen; + + memset( &ctx, 0, sizeof( cipher_context_t ) ); + + TEST_ASSERT( cipher_get_block_size( NULL ) == 0 ); + TEST_ASSERT( cipher_get_block_size( &ctx ) == 0 ); + + TEST_ASSERT( cipher_get_cipher_mode( NULL ) == POLARSSL_MODE_NONE ); + TEST_ASSERT( cipher_get_cipher_mode( &ctx ) == POLARSSL_MODE_NONE ); + + TEST_ASSERT( cipher_get_iv_size( NULL ) == 0 ); + TEST_ASSERT( cipher_get_iv_size( &ctx ) == 0 ); + + TEST_ASSERT( cipher_info_from_string( NULL ) == NULL ); + + TEST_ASSERT( cipher_init_ctx( &ctx, NULL ) + == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA ); + TEST_ASSERT( cipher_init_ctx( NULL, info ) + == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA ); + + TEST_ASSERT( cipher_setkey( NULL, buf, 0, POLARSSL_ENCRYPT ) + == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA ); + TEST_ASSERT( cipher_setkey( &ctx, buf, 0, POLARSSL_ENCRYPT ) + == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA ); + + TEST_ASSERT( cipher_set_iv( NULL, buf, 0 ) + == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA ); + TEST_ASSERT( cipher_set_iv( &ctx, buf, 0 ) + == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA ); + + TEST_ASSERT( cipher_reset( NULL ) == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA ); + TEST_ASSERT( cipher_reset( &ctx ) == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA ); + +#if defined(POLARSSL_CIPHER_MODE_AEAD) + TEST_ASSERT( cipher_update_ad( NULL, buf, 0 ) + == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA ); + TEST_ASSERT( cipher_update_ad( &ctx, buf, 0 ) + == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA ); +#endif + + TEST_ASSERT( cipher_update( NULL, buf, 0, buf, &olen ) + == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA ); + TEST_ASSERT( cipher_update( &ctx, buf, 0, buf, &olen ) + == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA ); + + TEST_ASSERT( cipher_finish( NULL, buf, &olen ) + == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA ); + TEST_ASSERT( cipher_finish( &ctx, buf, &olen ) + == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA ); + +#if defined(POLARSSL_CIPHER_MODE_AEAD) + TEST_ASSERT( cipher_write_tag( NULL, buf, olen ) + == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA ); + TEST_ASSERT( cipher_write_tag( &ctx, buf, olen ) + == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA ); + + TEST_ASSERT( cipher_check_tag( NULL, buf, olen ) + == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA ); + TEST_ASSERT( cipher_check_tag( &ctx, buf, olen ) + == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA ); +#endif +} +/* END_CASE */ + /* BEGIN_CASE */ void enc_dec_buf( int cipher_id, char *cipher_string, int key_len, int length_val, int pad_mode ) diff --git a/tests/suites/test_suite_cipher.padding.data b/tests/suites/test_suite_cipher.padding.data index 702c88ff7f..9b5f290dda 100644 --- a/tests/suites/test_suite_cipher.padding.data +++ b/tests/suites/test_suite_cipher.padding.data @@ -1,6 +1,9 @@ Cipher list cipher_list: +Cipher null/uninitialised arguments +cipher_null_args: + Set padding with AES-CBC depends_on:POLARSSL_AES_C:POLARSSL_CIPHER_MODE_CBC:POLARSSL_CIPHER_PADDING_PKCS7 set_padding:POLARSSL_CIPHER_AES_128_CBC:POLARSSL_PADDING_PKCS7:0