Ensure MD self_test ret codes are not hidden

Also fix a potential memory leak and an incorrect goto statement in
sha1.c self_test
This commit is contained in:
Andres Amaya Garcia 2017-07-20 14:18:54 +01:00
parent a21247ead7
commit 6a3f30514a
4 changed files with 42 additions and 25 deletions

View File

@ -464,7 +464,7 @@ static const unsigned char ripemd160_test_md[TESTS][20] =
*/ */
int mbedtls_ripemd160_self_test( int verbose ) int mbedtls_ripemd160_self_test( int verbose )
{ {
int i, ret; int i, ret = 0;
unsigned char output[20]; unsigned char output[20];
memset( output, 0, sizeof output ); memset( output, 0, sizeof output );
@ -481,7 +481,10 @@ int mbedtls_ripemd160_self_test( int verbose )
goto fail; goto fail;
if( memcmp( output, ripemd160_test_md[i], 20 ) != 0 ) if( memcmp( output, ripemd160_test_md[i], 20 ) != 0 )
{
ret = 1;
goto fail; goto fail;
}
if( verbose != 0 ) if( verbose != 0 )
mbedtls_printf( "passed\n" ); mbedtls_printf( "passed\n" );
@ -496,7 +499,7 @@ fail:
if( verbose != 0 ) if( verbose != 0 )
mbedtls_printf( "failed\n" ); mbedtls_printf( "failed\n" );
return( 1 ); return( ret );
} }
#endif /* MBEDTLS_SELF_TEST */ #endif /* MBEDTLS_SELF_TEST */

View File

@ -439,7 +439,7 @@ int mbedtls_sha1_self_test( int verbose )
if( verbose != 0 ) if( verbose != 0 )
mbedtls_printf( " SHA-1 test #%d: ", i + 1 ); mbedtls_printf( " SHA-1 test #%d: ", i + 1 );
if( mbedtls_sha1_starts_ext( &ctx ) != 0 ) if( ( ret = mbedtls_sha1_starts_ext( &ctx ) ) != 0 )
goto fail; goto fail;
if( i == 2 ) if( i == 2 )
@ -448,21 +448,27 @@ int mbedtls_sha1_self_test( int verbose )
for( j = 0; j < 1000; j++ ) for( j = 0; j < 1000; j++ )
{ {
if( mbedtls_sha1_update_ext( &ctx, buf, buflen ) != 0 ) ret = mbedtls_sha1_update_ext( &ctx, buf, buflen );
if( ret != 0 )
goto fail; goto fail;
} }
} }
else else
{ {
if( mbedtls_sha1_update_ext( &ctx, sha1_test_buf[i], ret = mbedtls_sha1_update_ext( &ctx, sha1_test_buf[i],
sha1_test_buflen[i] ) != 0 ) sha1_test_buflen[i] );
if( ret != 0 )
goto fail; goto fail;
} }
mbedtls_sha1_finish_ext( &ctx, sha1sum ); if( ( ret = mbedtls_sha1_finish_ext( &ctx, sha1sum ) ) != 0 )
goto fail;
if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 ) if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 )
goto exit; {
ret = 1;
goto fail;
}
if( verbose != 0 ) if( verbose != 0 )
mbedtls_printf( "passed\n" ); mbedtls_printf( "passed\n" );
@ -477,8 +483,6 @@ fail:
if( verbose != 0 ) if( verbose != 0 )
mbedtls_printf( "failed\n" ); mbedtls_printf( "failed\n" );
ret = 1;
exit: exit:
mbedtls_sha1_free( &ctx ); mbedtls_sha1_free( &ctx );

View File

@ -448,7 +448,7 @@ int mbedtls_sha256_self_test( int verbose )
if( verbose != 0 ) if( verbose != 0 )
mbedtls_printf( " SHA-%d test #%d: ", 256 - k * 32, j + 1 ); mbedtls_printf( " SHA-%d test #%d: ", 256 - k * 32, j + 1 );
if( mbedtls_sha256_starts_ext( &ctx, k ) != 0 ) if( ( ret = mbedtls_sha256_starts_ext( &ctx, k ) ) != 0 )
goto fail; goto fail;
if( j == 2 ) if( j == 2 )
@ -456,23 +456,30 @@ int mbedtls_sha256_self_test( int verbose )
memset( buf, 'a', buflen = 1000 ); memset( buf, 'a', buflen = 1000 );
for( j = 0; j < 1000; j++ ) for( j = 0; j < 1000; j++ )
if( mbedtls_sha256_update_ext( &ctx, buf, buflen ) != 0 ) {
ret = mbedtls_sha256_update_ext( &ctx, buf, buflen );
if( ret != 0 )
goto fail; goto fail;
}
} }
else else
{ {
if( mbedtls_sha256_update_ext( &ctx, sha256_test_buf[j], ret = mbedtls_sha256_update_ext( &ctx, sha256_test_buf[j],
sha256_test_buflen[j] ) != 0 ) sha256_test_buflen[j] );
if( ret != 0 )
goto fail; goto fail;
} }
if( mbedtls_sha256_finish_ext( &ctx, sha256sum ) != 0 ) if( ( ret = mbedtls_sha256_finish_ext( &ctx, sha256sum ) ) != 0 )
goto fail; goto fail;
if( memcmp( sha256sum, sha256_test_sum[i], 32 - k * 4 ) != 0 ) if( memcmp( sha256sum, sha256_test_sum[i], 32 - k * 4 ) != 0 )
{
ret = 1;
goto fail; goto fail;
}
if( verbose != 0 ) if( verbose != 0 )
mbedtls_printf( "passed\n" ); mbedtls_printf( "passed\n" );
@ -487,8 +494,6 @@ fail:
if( verbose != 0 ) if( verbose != 0 )
mbedtls_printf( "failed\n" ); mbedtls_printf( "failed\n" );
ret = 1;
exit: exit:
mbedtls_sha256_free( &ctx ); mbedtls_sha256_free( &ctx );
mbedtls_free( buf ); mbedtls_free( buf );

View File

@ -504,7 +504,7 @@ int mbedtls_sha512_self_test( int verbose )
if( verbose != 0 ) if( verbose != 0 )
mbedtls_printf( " SHA-%d test #%d: ", 512 - k * 128, j + 1 ); mbedtls_printf( " SHA-%d test #%d: ", 512 - k * 128, j + 1 );
if( mbedtls_sha512_starts_ext( &ctx, k ) != 0 ) if( ( ret = mbedtls_sha512_starts_ext( &ctx, k ) ) != 0 )
goto fail; goto fail;
if( j == 2 ) if( j == 2 )
@ -512,21 +512,28 @@ int mbedtls_sha512_self_test( int verbose )
memset( buf, 'a', buflen = 1000 ); memset( buf, 'a', buflen = 1000 );
for( j = 0; j < 1000; j++ ) for( j = 0; j < 1000; j++ )
if( mbedtls_sha512_update_ext( &ctx, buf, buflen ) != 0 ) {
ret = mbedtls_sha512_update_ext( &ctx, buf, buflen );
if( ret != 0 )
goto fail; goto fail;
} }
}
else else
{ {
if( mbedtls_sha512_update_ext( &ctx, sha512_test_buf[j], ret = mbedtls_sha512_update_ext( &ctx, sha512_test_buf[j],
sha512_test_buflen[j] ) != 0 ) sha512_test_buflen[j] );
if( ret != 0 )
goto fail; goto fail;
} }
if( mbedtls_sha512_finish_ext( &ctx, sha512sum ) != 0 ) if( ( ret = mbedtls_sha512_finish_ext( &ctx, sha512sum ) ) != 0 )
goto fail; goto fail;
if( memcmp( sha512sum, sha512_test_sum[i], 64 - k * 16 ) != 0 ) if( memcmp( sha512sum, sha512_test_sum[i], 64 - k * 16 ) != 0 )
{
ret = 1;
goto fail; goto fail;
}
if( verbose != 0 ) if( verbose != 0 )
mbedtls_printf( "passed\n" ); mbedtls_printf( "passed\n" );
@ -541,8 +548,6 @@ fail:
if( verbose != 0 ) if( verbose != 0 )
mbedtls_printf( "failed\n" ); mbedtls_printf( "failed\n" );
ret = 1;
exit: exit:
mbedtls_sha512_free( &ctx ); mbedtls_sha512_free( &ctx );
mbedtls_free( buf ); mbedtls_free( buf );