Add tests for ChaChaPoly parameter validation

Parameter validation was previously performed and tested unconditionally
for the ChaCha/Poly modules. This commit therefore only needs go guard the
existing tests accordingly and use the appropriate test macros for parameter
validation.
This commit is contained in:
Hanno Becker 2018-12-11 15:14:02 +00:00
parent 305e4e4f32
commit ae2ff02ff1
3 changed files with 123 additions and 145 deletions

View File

@ -82,7 +82,7 @@ void chacha20_crypt( char *hex_key_string,
}
/* END_CASE */
/* BEGIN_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
void chacha20_bad_params()
{
unsigned char key[32];
@ -93,42 +93,37 @@ void chacha20_bad_params()
size_t len = sizeof( src );
mbedtls_chacha20_context ctx;
mbedtls_chacha20_init( NULL );
mbedtls_chacha20_free( NULL );
TEST_INVALID_PARAM( mbedtls_chacha20_init( NULL ) );
mbedtls_chacha20_init( &ctx );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA,
mbedtls_chacha20_setkey( NULL, key ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA,
mbedtls_chacha20_setkey( &ctx, NULL ) );
TEST_ASSERT( mbedtls_chacha20_setkey( NULL, key )
== MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA );
TEST_ASSERT( mbedtls_chacha20_setkey( &ctx, NULL )
== MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA,
mbedtls_chacha20_starts( NULL, nonce, counter ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA,
mbedtls_chacha20_starts( &ctx, NULL, counter ) );
TEST_ASSERT( mbedtls_chacha20_starts( NULL, nonce, counter )
== MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA );
TEST_ASSERT( mbedtls_chacha20_starts( &ctx, NULL, counter )
== MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA,
mbedtls_chacha20_update( NULL, 0, src, dst ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA,
mbedtls_chacha20_update( &ctx, len, NULL, dst ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA,
mbedtls_chacha20_update( &ctx, len, src, NULL ) );
TEST_ASSERT( mbedtls_chacha20_update( NULL, 0, src, dst )
== MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA );
TEST_ASSERT( mbedtls_chacha20_update( &ctx, len, NULL, dst )
== MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA );
TEST_ASSERT( mbedtls_chacha20_update( &ctx, len, src, NULL )
== MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA );
TEST_ASSERT( mbedtls_chacha20_update( &ctx, 0, NULL, NULL )
== 0 );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA,
mbedtls_chacha20_crypt( NULL, nonce, counter, 0, src, dst ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA,
mbedtls_chacha20_crypt( key, NULL, counter, 0, src, dst ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA,
mbedtls_chacha20_crypt( key, nonce, counter, len, NULL, dst ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA,
mbedtls_chacha20_crypt( key, nonce, counter, len, src, NULL ) );
mbedtls_chacha20_free( &ctx );
exit:
return;
TEST_ASSERT( mbedtls_chacha20_crypt( NULL, nonce, counter, 0, src, dst )
== MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA );
TEST_ASSERT( mbedtls_chacha20_crypt( key, NULL, counter, 0, src, dst )
== MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA );
TEST_ASSERT( mbedtls_chacha20_crypt( key, nonce, counter, len, NULL, dst )
== MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA );
TEST_ASSERT( mbedtls_chacha20_crypt( key, nonce, counter, len, src, NULL )
== MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA );
TEST_ASSERT( mbedtls_chacha20_crypt( key, nonce, counter, 0, NULL, NULL )
== 0 );
}
/* END_CASE */

View File

@ -118,7 +118,7 @@ exit:
}
/* END_CASE */
/* BEGIN_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
void chachapoly_bad_params()
{
unsigned char key[32];
@ -138,124 +138,113 @@ void chachapoly_bad_params()
memset( output, 0x00, sizeof( output ) );
memset( mac, 0x00, sizeof( mac ) );
mbedtls_chachapoly_init( NULL );
mbedtls_chachapoly_free( NULL );
TEST_INVALID_PARAM( mbedtls_chachapoly_init( NULL ) );
mbedtls_chachapoly_init( &ctx );
/* setkey */
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
mbedtls_chachapoly_setkey( NULL, key ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
mbedtls_chachapoly_setkey( &ctx, NULL ) );
TEST_ASSERT( mbedtls_chachapoly_setkey( NULL, key )
== MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA );
TEST_ASSERT( mbedtls_chachapoly_setkey( &ctx, NULL )
== MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA );
TEST_ASSERT( mbedtls_chachapoly_encrypt_and_tag( NULL,
/* encrypt_and_tag */
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
mbedtls_chachapoly_encrypt_and_tag( NULL,
0, nonce,
aad, 0,
input, output, mac )
== MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA );
TEST_ASSERT( mbedtls_chachapoly_encrypt_and_tag( &ctx,
input, output, mac ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
mbedtls_chachapoly_encrypt_and_tag( &ctx,
0, NULL,
aad, 0,
input, output, mac )
== MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA );
TEST_ASSERT( mbedtls_chachapoly_encrypt_and_tag( &ctx,
input, output, mac ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
mbedtls_chachapoly_encrypt_and_tag( &ctx,
0, nonce,
NULL, aad_len,
input, output, mac )
== MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA );
TEST_ASSERT( mbedtls_chachapoly_encrypt_and_tag( &ctx,
input, output, mac ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
mbedtls_chachapoly_encrypt_and_tag( &ctx,
input_len, nonce,
aad, 0,
NULL, output, mac )
== MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA );
TEST_ASSERT( mbedtls_chachapoly_encrypt_and_tag( &ctx,
NULL, output, mac ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
mbedtls_chachapoly_encrypt_and_tag( &ctx,
input_len, nonce,
aad, 0,
input, NULL, mac )
== MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA );
TEST_ASSERT( mbedtls_chachapoly_encrypt_and_tag( &ctx,
input, NULL, mac ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
mbedtls_chachapoly_encrypt_and_tag( &ctx,
0, nonce,
aad, 0,
input, output, NULL )
== MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA );
input, output, NULL ) );
TEST_ASSERT( mbedtls_chachapoly_auth_decrypt( NULL,
/* auth_decrypt */
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
mbedtls_chachapoly_auth_decrypt( NULL,
0, nonce,
aad, 0,
mac, input, output )
== MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA );
TEST_ASSERT( mbedtls_chachapoly_auth_decrypt( &ctx,
mac, input, output ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
mbedtls_chachapoly_auth_decrypt( &ctx,
0, NULL,
aad, 0,
mac, input, output )
== MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA );
TEST_ASSERT( mbedtls_chachapoly_auth_decrypt( &ctx,
mac, input, output ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
mbedtls_chachapoly_auth_decrypt( &ctx,
0, nonce,
NULL, aad_len,
mac, input, output )
== MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA );
TEST_ASSERT( mbedtls_chachapoly_auth_decrypt( &ctx,
mac, input, output ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
mbedtls_chachapoly_auth_decrypt( &ctx,
0, nonce,
aad, 0,
NULL, input, output )
== MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA );
TEST_ASSERT( mbedtls_chachapoly_auth_decrypt( &ctx,
NULL, input, output ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
mbedtls_chachapoly_auth_decrypt( &ctx,
input_len, nonce,
aad, 0,
mac, NULL, output )
== MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA );
TEST_ASSERT( mbedtls_chachapoly_auth_decrypt( &ctx,
mac, NULL, output ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
mbedtls_chachapoly_auth_decrypt( &ctx,
input_len, nonce,
aad, 0,
mac, input, NULL )
== MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA );
mac, input, NULL ) );
TEST_ASSERT( mbedtls_chachapoly_encrypt_and_tag( &ctx,
0, nonce,
aad, aad_len,
NULL, NULL, mac )
== 0 );
TEST_ASSERT( mbedtls_chachapoly_auth_decrypt( &ctx,
0, nonce,
aad, aad_len,
mac, NULL, NULL )
== 0 );
/* starts */
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
mbedtls_chachapoly_starts( NULL, nonce,
MBEDTLS_CHACHAPOLY_ENCRYPT ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
mbedtls_chachapoly_starts( &ctx, NULL,
MBEDTLS_CHACHAPOLY_ENCRYPT ) );
TEST_ASSERT( mbedtls_chachapoly_encrypt_and_tag( &ctx,
input_len, nonce,
NULL, 0,
input, output, mac )
== 0 );
TEST_ASSERT( mbedtls_chachapoly_auth_decrypt( &ctx,
input_len, nonce,
NULL, 0,
mac, input, output )
== 0 );
/* update_aad */
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
mbedtls_chachapoly_update_aad( NULL, aad,
aad_len ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
mbedtls_chachapoly_update_aad( &ctx, NULL,
aad_len ) );
TEST_ASSERT( mbedtls_chachapoly_starts( NULL, nonce, MBEDTLS_CHACHAPOLY_ENCRYPT )
== MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA );
TEST_ASSERT( mbedtls_chachapoly_starts( &ctx, NULL, MBEDTLS_CHACHAPOLY_ENCRYPT )
== MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA );
/* update */
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
mbedtls_chachapoly_update( NULL, input_len,
input, output ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
mbedtls_chachapoly_update( &ctx, input_len,
NULL, output ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
mbedtls_chachapoly_update( &ctx, input_len,
input, NULL ) );
TEST_ASSERT( mbedtls_chachapoly_update_aad( NULL, aad, aad_len )
== MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA );
TEST_ASSERT( mbedtls_chachapoly_update_aad( &ctx, NULL, aad_len )
== MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA );
TEST_ASSERT( mbedtls_chachapoly_update( NULL, input_len, input, output )
== MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA );
TEST_ASSERT( mbedtls_chachapoly_update( &ctx, input_len, NULL, output )
== MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA );
TEST_ASSERT( mbedtls_chachapoly_update( &ctx, input_len, input, NULL )
== MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA );
TEST_ASSERT( mbedtls_chachapoly_finish( NULL, mac )
== MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA );
TEST_ASSERT( mbedtls_chachapoly_finish( &ctx, NULL )
== MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA );
/* finish */
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
mbedtls_chachapoly_finish( NULL, mac ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
mbedtls_chachapoly_finish( &ctx, NULL ) );
exit:
mbedtls_chachapoly_free( &ctx );
return;
}
/* END_CASE */

View File

@ -88,7 +88,7 @@ void mbedtls_poly1305( char *hex_key_string, char *hex_mac_string, char *hex_src
}
/* END_CASE */
/* BEGIN_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
void poly1305_bad_params()
{
unsigned char src[1];
@ -97,38 +97,32 @@ void poly1305_bad_params()
size_t src_len = sizeof( src );
mbedtls_poly1305_context ctx;
mbedtls_poly1305_init( NULL );
mbedtls_poly1305_free( NULL );
TEST_INVALID_PARAM( mbedtls_poly1305_init( NULL ) );
mbedtls_poly1305_init( &ctx );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
mbedtls_poly1305_starts( NULL, key ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
mbedtls_poly1305_starts( &ctx, NULL ) );
TEST_ASSERT( mbedtls_poly1305_starts( NULL, key )
== MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA );
TEST_ASSERT( mbedtls_poly1305_starts( &ctx, NULL )
== MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
mbedtls_poly1305_update( NULL, src, 0 ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
mbedtls_poly1305_update( &ctx, NULL, src_len ) );
TEST_ASSERT( mbedtls_poly1305_update( NULL, src, 0 )
== MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA );
TEST_ASSERT( mbedtls_poly1305_update( &ctx, NULL, src_len )
== MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA );
TEST_ASSERT( mbedtls_poly1305_update( &ctx, NULL, 0 )
== 0 );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
mbedtls_poly1305_finish( NULL, mac ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
mbedtls_poly1305_finish( &ctx, NULL ) );
TEST_ASSERT( mbedtls_poly1305_finish( NULL, mac )
== MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA );
TEST_ASSERT( mbedtls_poly1305_finish( &ctx, NULL )
== MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
mbedtls_poly1305_mac( NULL, src, 0, mac ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
mbedtls_poly1305_mac( key, NULL, src_len, mac ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
mbedtls_poly1305_mac( key, src, 0, NULL ) );
TEST_ASSERT( mbedtls_poly1305_mac( NULL, src, 0, mac )
== MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA );
TEST_ASSERT( mbedtls_poly1305_mac( key, NULL, src_len, mac )
== MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA );
TEST_ASSERT( mbedtls_poly1305_mac( key, src, 0, NULL )
== MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA );
TEST_ASSERT( mbedtls_poly1305_mac( key, NULL, 0, mac )
== 0 );
mbedtls_poly1305_free( &ctx );
exit:
return;
}
/* END_CASE */