From 2d0aa8be97bad9e8d65276716833f1e6d117c5b2 Mon Sep 17 00:00:00 2001 From: Andres Amaya Garcia Date: Fri, 21 Jul 2017 14:57:26 +0100 Subject: [PATCH] Fix MD selftest to use correct type and expose ret --- library/md2.c | 20 ++++++++++++++------ library/md4.c | 20 ++++++++++++++------ library/md5.c | 15 +++++++++------ library/ripemd160.c | 31 +++++++++++++++++-------------- library/sha1.c | 2 +- library/sha256.c | 2 +- library/sha512.c | 2 +- 7 files changed, 57 insertions(+), 35 deletions(-) diff --git a/library/md2.c b/library/md2.c index 8d887a102..06d6ac288 100644 --- a/library/md2.c +++ b/library/md2.c @@ -248,7 +248,7 @@ exit: /* * RFC 1319 test vectors */ -static const char md2_test_str[7][81] = +static const unsigned char md2_test_str[7][81] = { { "" }, { "a" }, @@ -256,10 +256,15 @@ static const char md2_test_str[7][81] = { "message digest" }, { "abcdefghijklmnopqrstuvwxyz" }, { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" }, - { "12345678901234567890123456789012345678901234567890123456789012" \ + { "12345678901234567890123456789012345678901234567890123456789012" "345678901234567890" } }; +static const size_t md2_test_strlen[7] = +{ + 0, 1, 3, 14, 26, 62, 80 +}; + static const unsigned char md2_test_sum[7][16] = { { 0x83, 0x50, 0xE5, 0xA3, 0xE2, 0x4C, 0x15, 0x3D, @@ -283,7 +288,7 @@ static const unsigned char md2_test_sum[7][16] = */ int mbedtls_md2_self_test( int verbose ) { - int i; + int i, ret = 0; unsigned char md2sum[16]; for( i = 0; i < 7; i++ ) @@ -291,12 +296,15 @@ int mbedtls_md2_self_test( int verbose ) if( verbose != 0 ) mbedtls_printf( " MD2 test #%d: ", i + 1 ); - if( mbedtls_md2_ext( (unsigned char *)md2_test_str[i], - strlen( md2_test_str[i] ), md2sum ) != 0 ) + ret = mbedtls_md2_ext( md2_test_str[i], md2_test_strlen[i], md2sum ); + if( ret != 0 ) goto fail; if( memcmp( md2sum, md2_test_sum[i], 16 ) != 0 ) + { + ret = 1; goto fail; + } if( verbose != 0 ) mbedtls_printf( "passed\n" ); @@ -311,7 +319,7 @@ fail: if( verbose != 0 ) mbedtls_printf( "failed\n" ); - return( 1 ); + return( ret ); } #endif /* MBEDTLS_SELF_TEST */ diff --git a/library/md4.c b/library/md4.c index 1121fd190..f5972eb63 100644 --- a/library/md4.c +++ b/library/md4.c @@ -352,7 +352,7 @@ exit: /* * RFC 1320 test vectors */ -static const char md4_test_str[7][81] = +static const unsigned char md4_test_str[7][81] = { { "" }, { "a" }, @@ -360,10 +360,15 @@ static const char md4_test_str[7][81] = { "message digest" }, { "abcdefghijklmnopqrstuvwxyz" }, { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" }, - { "12345678901234567890123456789012345678901234567890123456789012" \ + { "12345678901234567890123456789012345678901234567890123456789012" "345678901234567890" } }; +static const size_t md4_test_strlen[7] = +{ + 0, 1, 3, 14, 26, 62, 80 +}; + static const unsigned char md4_test_sum[7][16] = { { 0x31, 0xD6, 0xCF, 0xE0, 0xD1, 0x6A, 0xE9, 0x31, @@ -387,7 +392,7 @@ static const unsigned char md4_test_sum[7][16] = */ int mbedtls_md4_self_test( int verbose ) { - int i; + int i, ret = 0; unsigned char md4sum[16]; for( i = 0; i < 7; i++ ) @@ -395,12 +400,15 @@ int mbedtls_md4_self_test( int verbose ) if( verbose != 0 ) mbedtls_printf( " MD4 test #%d: ", i + 1 ); - if( mbedtls_md4_ext( (unsigned char *) md4_test_str[i], - strlen( md4_test_str[i] ), md4sum ) != 0 ) + ret = mbedtls_md4_ext( md4_test_str[i], md4_test_strlen[i], md4sum ); + if( ret != 0 ) goto fail; if( memcmp( md4sum, md4_test_sum[i], 16 ) != 0 ) + { + ret = 1; goto fail; + } if( verbose != 0 ) mbedtls_printf( "passed\n" ); @@ -415,7 +423,7 @@ fail: if( verbose != 0 ) mbedtls_printf( "failed\n" ); - return( 1 ); + return( ret ); } #endif /* MBEDTLS_SELF_TEST */ diff --git a/library/md5.c b/library/md5.c index 93f6434a1..68a112ab7 100644 --- a/library/md5.c +++ b/library/md5.c @@ -373,11 +373,11 @@ static const unsigned char md5_test_buf[7][81] = { "message digest" }, { "abcdefghijklmnopqrstuvwxyz" }, { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" }, - { "12345678901234567890123456789012345678901234567890123456789012" \ + { "12345678901234567890123456789012345678901234567890123456789012" "345678901234567890" } }; -static const int md5_test_buflen[7] = +static const size_t md5_test_buflen[7] = { 0, 1, 3, 14, 26, 62, 80 }; @@ -405,7 +405,7 @@ static const unsigned char md5_test_sum[7][16] = */ int mbedtls_md5_self_test( int verbose ) { - int i; + int i, ret = 0; unsigned char md5sum[16]; for( i = 0; i < 7; i++ ) @@ -413,12 +413,15 @@ int mbedtls_md5_self_test( int verbose ) if( verbose != 0 ) mbedtls_printf( " MD5 test #%d: ", i + 1 ); - if( mbedtls_md5_ext( md5_test_buf[i], - md5_test_buflen[i], md5sum ) != 0 ) + ret = mbedtls_md5_ext( md5_test_buf[i], md5_test_buflen[i], md5sum ); + if( ret != 0 ) goto fail; if( memcmp( md5sum, md5_test_sum[i], 16 ) != 0 ) + { + ret = 1; goto fail; + } if( verbose != 0 ) mbedtls_printf( "passed\n" ); @@ -433,7 +436,7 @@ fail: if( verbose != 0 ) mbedtls_printf( "failed\n" ); - return( 1 ); + return( ret ); } #endif /* MBEDTLS_SELF_TEST */ diff --git a/library/ripemd160.c b/library/ripemd160.c index bf5058fe9..274a7c9c7 100644 --- a/library/ripemd160.c +++ b/library/ripemd160.c @@ -426,18 +426,22 @@ exit: * http://homes.esat.kuleuven.be/~bosselae/mbedtls_ripemd160.html#HMAC */ #define TESTS 8 -#define KEYS 2 -static const char *ripemd160_test_input[TESTS] = +static const unsigned char ripemd160_test_str[TESTS][81] = { - "", - "a", - "abc", - "message digest", - "abcdefghijklmnopqrstuvwxyz", - "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", - "1234567890123456789012345678901234567890" - "1234567890123456789012345678901234567890", + { "" }, + { "a" }, + { "abc" }, + { "message digest" }, + { "abcdefghijklmnopqrstuvwxyz" }, + { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" }, + { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" }, + { "12345678901234567890123456789012345678901234567890123456789012" + "345678901234567890" }, +}; + +static const size_t ripemd160_test_strlen[TESTS] = +{ + 0, 1, 3, 14, 26, 56, 62, 80 }; static const unsigned char ripemd160_test_md[TESTS][20] = @@ -475,9 +479,8 @@ int mbedtls_ripemd160_self_test( int verbose ) if( verbose != 0 ) mbedtls_printf( " RIPEMD-160 test #%d: ", i + 1 ); - ret = mbedtls_ripemd160_ext( - (const unsigned char *)ripemd160_test_input[i], - strlen( ripemd160_test_input[i] ), output ); + ret = mbedtls_ripemd160_ext( ripemd160_test_str[i], + ripemd160_test_strlen[i], output ); if( ret != 0 ) goto fail; diff --git a/library/sha1.c b/library/sha1.c index 42f3d6cd5..8d3895035 100644 --- a/library/sha1.c +++ b/library/sha1.c @@ -405,7 +405,7 @@ static const unsigned char sha1_test_buf[3][57] = { "" } }; -static const int sha1_test_buflen[3] = +static const size_t sha1_test_buflen[3] = { 3, 56, 1000 }; diff --git a/library/sha256.c b/library/sha256.c index 0e24d6982..b76569792 100644 --- a/library/sha256.c +++ b/library/sha256.c @@ -380,7 +380,7 @@ static const unsigned char sha256_test_buf[3][57] = { "" } }; -static const int sha256_test_buflen[3] = +static const size_t sha256_test_buflen[3] = { 3, 56, 1000 }; diff --git a/library/sha512.c b/library/sha512.c index b1947f1ea..d0faba941 100644 --- a/library/sha512.c +++ b/library/sha512.c @@ -418,7 +418,7 @@ static const unsigned char sha512_test_buf[3][113] = { "" } }; -static const int sha512_test_buflen[3] = +static const size_t sha512_test_buflen[3] = { 3, 112, 1000 };