Split the unconditional and conditional parameter validation tests

This commit is contained in:
k-stachowiak 2018-12-19 18:06:35 +01:00
parent 516897a44a
commit a85edd9415
2 changed files with 127 additions and 63 deletions

View file

@ -21,10 +21,129 @@ void mbedtls_cipher_list( )
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
void cipher_invalid_param( )
/* BEGIN_CASE */
void cipher_invalid_param_unconditional( )
{
mbedtls_cipher_context_t valid_ctx;
mbedtls_cipher_context_t invalid_ctx;
mbedtls_operation_t valid_operation = MBEDTLS_ENCRYPT;
mbedtls_cipher_padding_t valid_mode = MBEDTLS_PADDING_ZEROS;
unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
int valid_size = sizeof(valid_buffer);
int valid_bitlen = valid_size * 8;
const mbedtls_cipher_info_t *valid_info = mbedtls_cipher_info_from_type(
*( mbedtls_cipher_list() ) );
size_t size_t_var;
mbedtls_cipher_init( &valid_ctx );
mbedtls_cipher_setup( &valid_ctx, valid_info );
mbedtls_cipher_init( &invalid_ctx );
/* mbedtls_cipher_setup() */
TEST_ASSERT( mbedtls_cipher_setup( &valid_ctx, NULL ) ==
MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
/* mbedtls_cipher_get_block_size() */
TEST_ASSERT( mbedtls_cipher_get_block_size( &invalid_ctx ) == 0 );
/* mbedtls_cipher_get_cipher_mode() */
TEST_ASSERT( mbedtls_cipher_get_cipher_mode( &invalid_ctx ) ==
MBEDTLS_MODE_NONE );
/* mbedtls_cipher_get_iv_size() */
TEST_ASSERT( mbedtls_cipher_get_iv_size( &invalid_ctx ) == 0 );
/* mbedtls_cipher_get_type() */
TEST_ASSERT(
mbedtls_cipher_get_type( &invalid_ctx ) ==
MBEDTLS_CIPHER_NONE);
/* mbedtls_cipher_get_name() */
TEST_ASSERT( mbedtls_cipher_get_name( &invalid_ctx ) == 0 );
/* mbedtls_cipher_get_key_bitlen() */
TEST_ASSERT( mbedtls_cipher_get_key_bitlen( &invalid_ctx ) ==
MBEDTLS_KEY_LENGTH_NONE );
/* mbedtls_cipher_get_operation() */
TEST_ASSERT( mbedtls_cipher_get_operation( &invalid_ctx ) ==
MBEDTLS_OPERATION_NONE );
/* mbedtls_cipher_setkey() */
TEST_ASSERT(
mbedtls_cipher_setkey( &invalid_ctx,
valid_buffer,
valid_bitlen,
valid_operation ) ==
MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
/* mbedtls_cipher_set_iv() */
TEST_ASSERT(
mbedtls_cipher_set_iv( &invalid_ctx,
valid_buffer,
valid_size ) ==
MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
/* mbedtls_cipher_reset() */
TEST_ASSERT( mbedtls_cipher_reset( &invalid_ctx ) ==
MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
/* mbedtls_cipher_update_ad() */
TEST_ASSERT(
mbedtls_cipher_update_ad( &invalid_ctx,
valid_buffer,
valid_size ) ==
MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
#endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
/* mbedtls_cipher_set_padding_mode() */
TEST_ASSERT( mbedtls_cipher_set_padding_mode( &invalid_ctx, valid_mode ) ==
MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
#endif
/* mbedtls_cipher_update() */
TEST_ASSERT(
mbedtls_cipher_update( &invalid_ctx,
valid_buffer,
valid_size,
valid_buffer,
&size_t_var ) ==
MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
/* mbedtls_cipher_finish() */
TEST_ASSERT(
mbedtls_cipher_finish( &invalid_ctx,
valid_buffer,
&size_t_var ) ==
MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
/* mbedtls_cipher_write_tag() */
TEST_ASSERT(
mbedtls_cipher_write_tag( &invalid_ctx,
valid_buffer,
valid_size ) ==
MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
/* mbedtls_cipher_check_tag() */
TEST_ASSERT(
mbedtls_cipher_check_tag( &invalid_ctx,
valid_buffer,
valid_size ) ==
MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
#endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
exit:
mbedtls_cipher_free( &invalid_ctx );
mbedtls_cipher_free( &valid_ctx );
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
void cipher_invalid_param_conditional( )
{
mbedtls_cipher_context_t valid_ctx;
mbedtls_operation_t valid_operation = MBEDTLS_ENCRYPT;
@ -39,58 +158,43 @@ void cipher_invalid_param( )
size_t size_t_var;
/* mbedtls_cipher_init() */
TEST_VALID_PARAM( mbedtls_cipher_init( &invalid_ctx ) );
TEST_VALID_PARAM( mbedtls_cipher_init( &valid_ctx ) );
TEST_INVALID_PARAM( mbedtls_cipher_init( NULL ) );
/* mbedtls_cipher_setup() */
TEST_VALID_PARAM( mbedtls_cipher_setup( &valid_ctx, valid_info ) );
TEST_INVALID_PARAM_RET(
MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
mbedtls_cipher_setup( NULL, valid_info ) );
TEST_ASSERT( mbedtls_cipher_setup( &valid_ctx, NULL ) ==
MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
/* mbedtls_cipher_get_block_size() */
TEST_INVALID_PARAM_RET( 0, mbedtls_cipher_get_block_size( NULL ) );
TEST_ASSERT( mbedtls_cipher_get_block_size( &invalid_ctx ) == 0 );
/* mbedtls_cipher_get_cipher_mode() */
TEST_INVALID_PARAM_RET(
MBEDTLS_MODE_NONE,
mbedtls_cipher_get_cipher_mode( NULL ) );
TEST_ASSERT( mbedtls_cipher_get_cipher_mode( &invalid_ctx ) ==
MBEDTLS_MODE_NONE );
/* mbedtls_cipher_get_iv_size() */
TEST_INVALID_PARAM_RET( 0, mbedtls_cipher_get_iv_size( NULL ) );
TEST_ASSERT( mbedtls_cipher_get_iv_size( &invalid_ctx ) == 0 );
/* mbedtls_cipher_get_type() */
TEST_INVALID_PARAM_RET(
MBEDTLS_CIPHER_NONE,
mbedtls_cipher_get_type( NULL ) );
TEST_ASSERT(
mbedtls_cipher_get_type( &invalid_ctx ) ==
MBEDTLS_CIPHER_NONE);
/* mbedtls_cipher_get_name() */
TEST_INVALID_PARAM_RET( 0, mbedtls_cipher_get_name( NULL ) );
TEST_ASSERT( mbedtls_cipher_get_name( &invalid_ctx ) == 0 );
/* mbedtls_cipher_get_key_bitlen() */
TEST_INVALID_PARAM_RET(
MBEDTLS_KEY_LENGTH_NONE,
mbedtls_cipher_get_key_bitlen( NULL ) );
TEST_ASSERT( mbedtls_cipher_get_key_bitlen( &invalid_ctx ) ==
MBEDTLS_KEY_LENGTH_NONE );
/* mbedtls_cipher_get_operation() */
TEST_INVALID_PARAM_RET(
MBEDTLS_OPERATION_NONE,
mbedtls_cipher_get_operation( NULL ) );
TEST_ASSERT( mbedtls_cipher_get_operation( &invalid_ctx ) ==
MBEDTLS_OPERATION_NONE );
/* mbedtls_cipher_setkey() */
TEST_INVALID_PARAM_RET(
@ -99,12 +203,6 @@ void cipher_invalid_param( )
valid_buffer,
valid_bitlen,
valid_operation ) );
TEST_ASSERT(
mbedtls_cipher_setkey( &invalid_ctx,
valid_buffer,
valid_bitlen,
valid_operation ) ==
MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
TEST_INVALID_PARAM_RET(
MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
mbedtls_cipher_setkey( &valid_ctx,
@ -124,11 +222,6 @@ void cipher_invalid_param( )
mbedtls_cipher_set_iv( NULL,
valid_buffer,
valid_size ) );
TEST_ASSERT(
mbedtls_cipher_set_iv( &invalid_ctx,
valid_buffer,
valid_size ) ==
MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
TEST_INVALID_PARAM_RET(
MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
mbedtls_cipher_set_iv( &valid_ctx,
@ -138,8 +231,6 @@ void cipher_invalid_param( )
/* mbedtls_cipher_reset() */
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
mbedtls_cipher_reset( NULL ) );
TEST_ASSERT( mbedtls_cipher_reset( &invalid_ctx ) ==
MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
/* mbedtls_cipher_update_ad() */
@ -148,11 +239,6 @@ void cipher_invalid_param( )
mbedtls_cipher_update_ad( NULL,
valid_buffer,
valid_size ) );
TEST_ASSERT(
mbedtls_cipher_update_ad( &invalid_ctx,
valid_buffer,
valid_size ) ==
MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
TEST_INVALID_PARAM_RET(
MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
mbedtls_cipher_update_ad( &valid_ctx,
@ -164,8 +250,6 @@ void cipher_invalid_param( )
/* mbedtls_cipher_set_padding_mode() */
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
mbedtls_cipher_set_padding_mode( NULL, valid_mode ) );
TEST_ASSERT( mbedtls_cipher_set_padding_mode( &invalid_ctx, valid_mode ) ==
MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
#endif
/* mbedtls_cipher_update() */
@ -176,13 +260,6 @@ void cipher_invalid_param( )
valid_size,
valid_buffer,
&size_t_var ) );
TEST_ASSERT(
mbedtls_cipher_update( &invalid_ctx,
valid_buffer,
valid_size,
valid_buffer,
&size_t_var ) ==
MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
TEST_INVALID_PARAM_RET(
MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
mbedtls_cipher_update( &valid_ctx,
@ -208,11 +285,6 @@ void cipher_invalid_param( )
mbedtls_cipher_finish( NULL,
valid_buffer,
&size_t_var ) );
TEST_ASSERT(
mbedtls_cipher_finish( &invalid_ctx,
valid_buffer,
&size_t_var ) ==
MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
TEST_INVALID_PARAM_RET(
MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
mbedtls_cipher_finish( &valid_ctx,
@ -231,11 +303,6 @@ void cipher_invalid_param( )
mbedtls_cipher_write_tag( NULL,
valid_buffer,
valid_size ) );
TEST_ASSERT(
mbedtls_cipher_write_tag( &invalid_ctx,
valid_buffer,
valid_size ) ==
MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
TEST_INVALID_PARAM_RET(
MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
mbedtls_cipher_write_tag( &valid_ctx,
@ -248,11 +315,6 @@ void cipher_invalid_param( )
mbedtls_cipher_check_tag( NULL,
valid_buffer,
valid_size ) );
TEST_ASSERT(
mbedtls_cipher_check_tag( &invalid_ctx,
valid_buffer,
valid_size ) ==
MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
TEST_INVALID_PARAM_RET(
MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
mbedtls_cipher_check_tag( &valid_ctx,
@ -413,7 +475,6 @@ void cipher_invalid_param( )
/* mbedtls_cipher_free() */
TEST_VALID_PARAM( mbedtls_cipher_free( NULL ) );
exit:
TEST_VALID_PARAM( mbedtls_cipher_free( &invalid_ctx ) );
TEST_VALID_PARAM( mbedtls_cipher_free( &valid_ctx ) );
}
/* END_CASE */

View file

@ -1,2 +1,5 @@
CIPHER - Invalid parameters
cipher_invalid_param:
CIPHER - Conditional invalid parameter checks
cipher_invalid_param_conditional:
CIPHER - Unconditional invalid parameter checks
cipher_invalid_param_unconditional: