From 53502519779470236af938911736e912ade8bda3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Thu, 10 May 2018 11:42:07 +0200 Subject: [PATCH] Simplify selftest functions using macros This reduces clutter, making the functions more readable. Also, it makes lcov see each line as covered. This is not cheating, as the lines that were previously seen as not covered are not supposed to be reached anyway (failing branches of the selftests). Thanks to this and previous test suite enhancements, lcov now sees chacha20.c and poly1305.c at 100% line coverage, and for chachapoly.c only two lines are not covered (error returns from lower-level module that should never happen except perhaps if an alternative implementation returns an unexpected error). --- library/chacha20.c | 45 +++++++++++++------------------ library/chachapoly.c | 64 ++++++++++++++++---------------------------- library/poly1305.c | 45 +++++++++++++------------------ 3 files changed, 60 insertions(+), 94 deletions(-) diff --git a/library/chacha20.c b/library/chacha20.c index d89000da2..5a753ebaa 100644 --- a/library/chacha20.c +++ b/library/chacha20.c @@ -519,18 +519,29 @@ static const size_t test_lengths[2] = 375U }; +#define ASSERT( cond, args ) \ + do \ + { \ + if( ! ( cond ) ) \ + { \ + if( verbose != 0 ) \ + mbedtls_printf args; \ + \ + return( -1 ); \ + } \ + } \ + while( 0 ) + int mbedtls_chacha20_self_test( int verbose ) { unsigned char output[381]; unsigned i; int result; - for ( i = 0U; i < 2U; i++ ) + for( i = 0U; i < 2U; i++ ) { - if ( verbose != 0 ) - { + if( verbose != 0 ) mbedtls_printf( " ChaCha20 test %u ", i ); - } result = mbedtls_chacha20_crypt( test_keys[i], test_nonces[i], @@ -538,36 +549,18 @@ int mbedtls_chacha20_self_test( int verbose ) test_lengths[i], test_input[i], output ); - if ( result != 0) - { - if ( verbose != 0 ) - { - mbedtls_printf( "error code: %i\n", result ); - } - return( -1 ); - } + ASSERT( 0 == result, ( "error code: %i\n", result ) ); - if ( 0 != memcmp( output, test_output[i], test_lengths[i] ) ) - { - if ( verbose != 0 ) - { - mbedtls_printf( "failed\n" ); - } + ASSERT( 0 == memcmp( output, test_output[i], test_lengths[i] ), + ( "failed (output)\n" ) ); - return( -1 ); - } - - if ( verbose != 0 ) - { + if( verbose != 0 ) mbedtls_printf( "passed\n" ); - } } if( verbose != 0 ) - { mbedtls_printf( "\n" ); - } return( 0 ); } diff --git a/library/chachapoly.c b/library/chachapoly.c index fd05886fb..ebf25bbbb 100644 --- a/library/chachapoly.c +++ b/library/chachapoly.c @@ -452,6 +452,19 @@ static const unsigned char test_mac[1][16] = } }; +#define ASSERT( cond, args ) \ + do \ + { \ + if( ! ( cond ) ) \ + { \ + if( verbose != 0 ) \ + mbedtls_printf args; \ + \ + return( -1 ); \ + } \ + } \ + while( 0 ) + int mbedtls_chachapoly_self_test( int verbose ) { mbedtls_chachapoly_context ctx; @@ -460,24 +473,15 @@ int mbedtls_chachapoly_self_test( int verbose ) unsigned char output[200]; unsigned char mac[16]; - for ( i = 0U; i < 1U; i++ ) + for( i = 0U; i < 1U; i++ ) { - if ( verbose != 0 ) - { + if( verbose != 0 ) mbedtls_printf( " ChaCha20-Poly1305 test %u ", i ); - } mbedtls_chachapoly_init( &ctx ); result = mbedtls_chachapoly_setkey( &ctx, test_key[i] ); - if ( result != 0 ) - { - if ( verbose != 0 ) - { - mbedtls_printf( "setkey() error code: %i\n", result ); - } - return( -1 ); - } + ASSERT( 0 == result, ( "setkey() error code: %i\n", result ) ); result = mbedtls_chachapoly_crypt_and_tag( &ctx, MBEDTLS_CHACHAPOLY_ENCRYPT, @@ -488,45 +492,23 @@ int mbedtls_chachapoly_self_test( int verbose ) test_input[i], output, mac ); - if ( result != 0 ) - { - if ( verbose != 0 ) - { - mbedtls_printf( "crypt_and_tag() error code: %i\n", result ); - } - return( -1 ); - } - if ( memcmp( output, test_output[i], test_input_len[i] ) != 0 ) - { - if ( verbose != 0 ) - { - mbedtls_printf( "failure (wrong output)\n" ); - } - return( -1 ); - } + ASSERT( 0 == result, ( "crypt_and_tag() error code: %i\n", result ) ); - if ( memcmp( mac, test_mac[i], 16U ) != 0 ) - { - if ( verbose != 0 ) - { - mbedtls_printf( "failure (wrong MAC)\n" ); - } - return( -1 ); - } + ASSERT( 0 == memcmp( output, test_output[i], test_input_len[i] ), + ( "failure (wrong output)\n" ) ); + + ASSERT( 0 == memcmp( mac, test_mac[i], 16U ), + ( "failure (wrong MAC)\n" ) ); mbedtls_chachapoly_free( &ctx ); - if ( verbose != 0 ) - { + if( verbose != 0 ) mbedtls_printf( "passed\n" ); - } } if( verbose != 0 ) - { mbedtls_printf( "\n" ); - } return( 0 ); } diff --git a/library/poly1305.c b/library/poly1305.c index 0aa453356..a9fff4757 100644 --- a/library/poly1305.c +++ b/library/poly1305.c @@ -487,53 +487,44 @@ static const unsigned char test_mac[2][16] = } }; +#define ASSERT( cond, args ) \ + do \ + { \ + if( ! ( cond ) ) \ + { \ + if( verbose != 0 ) \ + mbedtls_printf args; \ + \ + return( -1 ); \ + } \ + } \ + while( 0 ) + int mbedtls_poly1305_self_test( int verbose ) { unsigned char mac[16]; unsigned i; int result; - for ( i = 0U; i < 2U; i++ ) + for( i = 0U; i < 2U; i++ ) { - if ( verbose != 0 ) - { + if( verbose != 0 ) mbedtls_printf( " Poly1305 test %u ", i ); - } result = mbedtls_poly1305_mac( test_keys[i], test_data[i], test_data_len[i], mac ); - if ( result != 0 ) - { - if ( verbose != 0 ) - { - mbedtls_printf( "error code: %i\n", result ); - } + ASSERT( 0 == result, ( "error code: %i\n", result ) ); - return( -1 ); - } + ASSERT( 0 == memcmp( mac, test_mac[i], 16U ), ( "failed (mac)\n" ) ); - if ( memcmp( mac, test_mac[i], 16U ) != 0 ) - { - if ( verbose != 0 ) - { - mbedtls_printf( "failed\n" ); - } - - return( -1 ); - } - - if ( verbose != 0 ) - { + if( verbose != 0 ) mbedtls_printf( "passed\n" ); - } } if( verbose != 0 ) - { mbedtls_printf( "\n" ); - } return( 0 ); }