From 4bdc13ff09033459786df534679746e7ac6e8f3b Mon Sep 17 00:00:00 2001 From: Ronald Cron Date: Fri, 26 Jun 2020 17:00:30 +0200 Subject: [PATCH] tests: Remove usage of mbedtls_test_hexify for comparison Do not hexify binary data to compare them, do compare them directly. That simplifies the check code and save memory. Signed-off-by: Ronald Cron --- tests/suites/test_suite_aes.function | 10 ++-- tests/suites/test_suite_aria.function | 62 ++++++++--------------- tests/suites/test_suite_chacha20.function | 25 +++------ tests/suites/test_suite_hkdf.function | 16 +----- tests/suites/test_suite_poly1305.function | 18 +++---- 5 files changed, 36 insertions(+), 95 deletions(-) diff --git a/tests/suites/test_suite_aes.function b/tests/suites/test_suite_aes.function index c71959654..23bf83d09 100644 --- a/tests/suites/test_suite_aes.function +++ b/tests/suites/test_suite_aes.function @@ -319,17 +319,15 @@ exit: /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_OFB */ void aes_encrypt_ofb( int fragment_size, data_t *key_str, data_t *iv_str, data_t *src_str, - char *expected_output_string) + data_t *expected_output ) { unsigned char output[32]; - unsigned char output_string[65]; mbedtls_aes_context ctx; size_t iv_offset = 0; int in_buffer_len; unsigned char* src_str_next; memset( output, 0x00, sizeof( output ) ); - memset( output_string, 0x00, sizeof( output_string ) ); mbedtls_aes_init( &ctx ); TEST_ASSERT( (size_t)fragment_size < sizeof( output ) ); @@ -344,12 +342,10 @@ void aes_encrypt_ofb( int fragment_size, data_t *key_str, TEST_ASSERT( mbedtls_aes_crypt_ofb( &ctx, fragment_size, &iv_offset, iv_str->x, src_str_next, output ) == 0 ); - mbedtls_test_hexify( output_string, output, fragment_size ); - TEST_ASSERT( strncmp( (char *) output_string, expected_output_string, - ( 2 * fragment_size ) ) == 0 ); + TEST_ASSERT( memcmp( output, expected_output->x, fragment_size ) == 0 ); in_buffer_len -= fragment_size; - expected_output_string += ( fragment_size * 2 ); + expected_output->x += fragment_size; src_str_next += fragment_size; if( in_buffer_len < fragment_size ) diff --git a/tests/suites/test_suite_aria.function b/tests/suites/test_suite_aria.function index d08c39dc6..3cc683bf0 100644 --- a/tests/suites/test_suite_aria.function +++ b/tests/suites/test_suite_aria.function @@ -207,14 +207,12 @@ exit: /* BEGIN_CASE */ void aria_encrypt_ecb( data_t *key_str, data_t *src_str, - char *hex_dst_string, int setkey_result ) + data_t *expected_output, int setkey_result ) { - unsigned char dst_str[ARIA_MAX_DATA_STR]; unsigned char output[ARIA_MAX_DATASIZE]; mbedtls_aria_context ctx; size_t i; - memset( dst_str, 0x00, sizeof( dst_str ) ); memset( output, 0x00, sizeof( output ) ); mbedtls_aria_init( &ctx ); @@ -227,9 +225,9 @@ void aria_encrypt_ecb( data_t *key_str, data_t *src_str, TEST_ASSERT( mbedtls_aria_crypt_ecb( &ctx, src_str->x + i, output + i ) == 0 ); } - mbedtls_test_hexify( dst_str, output, src_str->len ); - TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 ); + TEST_ASSERT( !memcmp( output, + expected_output->x, expected_output->len) ); } exit: @@ -239,14 +237,12 @@ exit: /* BEGIN_CASE */ void aria_decrypt_ecb( data_t *key_str, data_t *src_str, - char *hex_dst_string, int setkey_result ) + data_t *expected_output, int setkey_result ) { - unsigned char dst_str[ARIA_MAX_DATA_STR]; unsigned char output[ARIA_MAX_DATASIZE]; mbedtls_aria_context ctx; size_t i; - memset( dst_str, 0x00, sizeof( dst_str ) ); memset( output, 0x00, sizeof( output ) ); mbedtls_aria_init( &ctx ); @@ -259,9 +255,9 @@ void aria_decrypt_ecb( data_t *key_str, data_t *src_str, TEST_ASSERT( mbedtls_aria_crypt_ecb( &ctx, src_str->x + i, output + i ) == 0 ); } - mbedtls_test_hexify( dst_str, output, src_str->len ); - TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 ); + TEST_ASSERT( !memcmp( output, + expected_output->x, expected_output->len) ); } exit: @@ -271,14 +267,12 @@ exit: /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */ void aria_encrypt_cbc( data_t *key_str, data_t *iv_str, - data_t *src_str, char *hex_dst_string, + data_t *src_str, data_t *expected_output, int cbc_result ) { - unsigned char dst_str[ARIA_MAX_DATA_STR]; unsigned char output[ARIA_MAX_DATASIZE]; mbedtls_aria_context ctx; - memset( dst_str, 0x00, sizeof( dst_str ) ); memset( output, 0x00, sizeof( output ) ); mbedtls_aria_init( &ctx ); @@ -288,9 +282,8 @@ void aria_encrypt_cbc( data_t *key_str, data_t *iv_str, output ) == cbc_result ); if( cbc_result == 0 ) { - mbedtls_test_hexify( dst_str, output, src_str->len ); - - TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 ); + TEST_ASSERT( !memcmp( output, + expected_output->x, expected_output->len) ); } exit: @@ -300,14 +293,12 @@ exit: /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */ void aria_decrypt_cbc( data_t *key_str, data_t *iv_str, - data_t *src_str, char *hex_dst_string, + data_t *src_str, data_t *expected_output, int cbc_result ) { - unsigned char dst_str[ARIA_MAX_DATA_STR]; unsigned char output[ARIA_MAX_DATASIZE]; mbedtls_aria_context ctx; - memset( dst_str, 0x00, sizeof( dst_str ) ); memset( output, 0x00, sizeof( output ) ); mbedtls_aria_init( &ctx ); @@ -317,9 +308,8 @@ void aria_decrypt_cbc( data_t *key_str, data_t *iv_str, output ) == cbc_result ); if( cbc_result == 0 ) { - mbedtls_test_hexify( dst_str, output, src_str->len ); - - TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 ); + TEST_ASSERT( !memcmp( output, + expected_output->x, expected_output->len) ); } exit: @@ -329,15 +319,13 @@ exit: /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ void aria_encrypt_cfb128( data_t *key_str, data_t *iv_str, - data_t *src_str, char *hex_dst_string, + data_t *src_str, data_t *expected_output, int result ) { - unsigned char dst_str[ARIA_MAX_DATA_STR]; unsigned char output[ARIA_MAX_DATASIZE]; mbedtls_aria_context ctx; size_t iv_offset = 0; - memset( dst_str, 0x00, sizeof( dst_str ) ); memset( output, 0x00, sizeof( output ) ); mbedtls_aria_init( &ctx ); @@ -346,9 +334,8 @@ void aria_encrypt_cfb128( data_t *key_str, data_t *iv_str, src_str->len, &iv_offset, iv_str->x, src_str->x, output ) == result ); - mbedtls_test_hexify( dst_str, output, src_str->len ); - TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 ); + TEST_ASSERT( !memcmp( output, expected_output->x, expected_output->len) ); exit: mbedtls_aria_free( &ctx ); @@ -357,15 +344,13 @@ exit: /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ void aria_decrypt_cfb128( data_t *key_str, data_t *iv_str, - data_t *src_str, char *hex_dst_string, + data_t *src_str, data_t *expected_output, int result ) { - unsigned char dst_str[ARIA_MAX_DATA_STR]; unsigned char output[ARIA_MAX_DATASIZE]; mbedtls_aria_context ctx; size_t iv_offset = 0; - memset( dst_str, 0x00, sizeof( dst_str ) ); memset( output, 0x00, sizeof( output ) ); mbedtls_aria_init( &ctx ); @@ -374,9 +359,8 @@ void aria_decrypt_cfb128( data_t *key_str, data_t *iv_str, src_str->len, &iv_offset, iv_str->x, src_str->x, output ) == result ); - mbedtls_test_hexify( dst_str, output, src_str->len ); - TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 ); + TEST_ASSERT( !memcmp( output, expected_output->x, expected_output->len) ); exit: mbedtls_aria_free( &ctx ); @@ -385,16 +369,14 @@ exit: /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CTR */ void aria_encrypt_ctr( data_t *key_str, data_t *iv_str, - data_t *src_str, char *hex_dst_string, + data_t *src_str, data_t *expected_output, int result ) { - unsigned char dst_str[ARIA_MAX_DATA_STR]; unsigned char output[ARIA_MAX_DATASIZE]; unsigned char blk[MBEDTLS_ARIA_BLOCKSIZE]; mbedtls_aria_context ctx; size_t iv_offset = 0; - memset( dst_str, 0x00, sizeof( dst_str ) ); memset( output, 0x00, sizeof( output ) ); mbedtls_aria_init( &ctx ); @@ -402,9 +384,8 @@ void aria_encrypt_ctr( data_t *key_str, data_t *iv_str, TEST_ASSERT( mbedtls_aria_crypt_ctr( &ctx, src_str->len, &iv_offset, iv_str->x, blk, src_str->x, output ) == result ); - mbedtls_test_hexify( dst_str, output, src_str->len ); - TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 ); + TEST_ASSERT( !memcmp( output, expected_output->x, expected_output->len) ); exit: mbedtls_aria_free( &ctx ); @@ -413,16 +394,14 @@ exit: /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CTR */ void aria_decrypt_ctr( data_t *key_str, data_t *iv_str, - data_t *src_str, char *hex_dst_string, + data_t *src_str, data_t *expected_output, int result ) { - unsigned char dst_str[ARIA_MAX_DATA_STR]; unsigned char output[ARIA_MAX_DATASIZE]; unsigned char blk[MBEDTLS_ARIA_BLOCKSIZE]; mbedtls_aria_context ctx; size_t iv_offset = 0; - memset( dst_str, 0x00, sizeof( dst_str ) ); memset( output, 0x00, sizeof( output ) ); mbedtls_aria_init( &ctx ); @@ -430,9 +409,8 @@ void aria_decrypt_ctr( data_t *key_str, data_t *iv_str, TEST_ASSERT( mbedtls_aria_crypt_ctr( &ctx, src_str->len, &iv_offset, iv_str->x, blk, src_str->x, output ) == result ); - mbedtls_test_hexify( dst_str, output, src_str->len ); - TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 ); + TEST_ASSERT( !memcmp( output, expected_output->x, expected_output->len) ); exit: mbedtls_aria_free( &ctx ); diff --git a/tests/suites/test_suite_chacha20.function b/tests/suites/test_suite_chacha20.function index afe24183a..968a9e9b9 100644 --- a/tests/suites/test_suite_chacha20.function +++ b/tests/suites/test_suite_chacha20.function @@ -17,13 +17,6 @@ void chacha20_crypt( data_t *key_str, unsigned char output[375]; mbedtls_chacha20_context ctx; - /* - * Buffers to store the ASCII string representation of output and - * expected_output_str. - */ - unsigned char output_string[751] = { '\0' }; - unsigned char expected_output_string[751] = { '\0' }; - memset( output, 0x00, sizeof( output ) ); TEST_ASSERT( src_str->len == expected_output_str->len ); @@ -35,12 +28,8 @@ void chacha20_crypt( data_t *key_str, */ TEST_ASSERT( mbedtls_chacha20_crypt( key_str->x, nonce_str->x, counter, src_str->len, src_str->x, output ) == 0 ); - mbedtls_test_hexify( expected_output_string, - expected_output_str->x, - expected_output_str->len); - mbedtls_test_hexify( output_string, output, src_str->len ); - TEST_ASSERT( strcmp( (char *)output_string, - (char *)expected_output_string ) == 0 ); + TEST_ASSERT( !memcmp( output, expected_output_str->x, + expected_output_str->len ) ); /* * Test the streaming API @@ -54,9 +43,8 @@ void chacha20_crypt( data_t *key_str, memset( output, 0x00, sizeof( output ) ); TEST_ASSERT( mbedtls_chacha20_update( &ctx, src_str->len, src_str->x, output ) == 0 ); - mbedtls_test_hexify( output_string, output, src_str->len ); - TEST_ASSERT( strcmp( (char *)output_string, - (char *)expected_output_string ) == 0 ); + TEST_ASSERT( !memcmp( output, expected_output_str->x, + expected_output_str->len ) ); /* * Test the streaming API again, piecewise @@ -71,9 +59,8 @@ void chacha20_crypt( data_t *key_str, TEST_ASSERT( mbedtls_chacha20_update( &ctx, src_str->len - 1, src_str->x + 1, output + 1 ) == 0 ); - mbedtls_test_hexify( output_string, output, src_str->len ); - TEST_ASSERT( strcmp( (char *)output_string, - (char *)expected_output_string ) == 0 ); + TEST_ASSERT( !memcmp( output, expected_output_str->x, + expected_output_str->len ) ); mbedtls_chacha20_free( &ctx ); } diff --git a/tests/suites/test_suite_hkdf.function b/tests/suites/test_suite_hkdf.function index ddd64fae4..eca10afd9 100644 --- a/tests/suites/test_suite_hkdf.function +++ b/tests/suites/test_suite_hkdf.function @@ -14,13 +14,6 @@ void test_hkdf( int md_alg, data_t *ikm, data_t *salt, data_t *info, { int ret; unsigned char okm[128] = { '\0' }; - /* - * okm_string and expected_okm_string are the ASCII string representations - * of km and expected_okm, so their size should be twice the size of - * okm and expected_okm, and an extra null-termination. - */ - unsigned char okm_string[257] = { '\0' }; - unsigned char expected_okm_string[257] = { '\0' }; const mbedtls_md_info_t *md = mbedtls_md_info_from_type( md_alg ); TEST_ASSERT( md != NULL ); @@ -31,14 +24,7 @@ void test_hkdf( int md_alg, data_t *ikm, data_t *salt, data_t *info, info->x, info->len, okm, expected_okm->len ); TEST_ASSERT( ret == 0 ); - /* - * Run mbedtls_test_hexify on okm and expected_okm so that it looks nicer - * if the assertion fails. - */ - mbedtls_test_hexify( okm_string, okm, expected_okm->len ); - mbedtls_test_hexify( expected_okm_string, - expected_okm->x, expected_okm->len ); - TEST_ASSERT( !strcmp( (char *)okm_string, (char *)expected_okm_string ) ); + TEST_ASSERT( !memcmp( okm, expected_okm->x, expected_okm->len ) ); } /* END_CASE */ diff --git a/tests/suites/test_suite_poly1305.function b/tests/suites/test_suite_poly1305.function index 44617d98e..0fc1f6058 100644 --- a/tests/suites/test_suite_poly1305.function +++ b/tests/suites/test_suite_poly1305.function @@ -9,14 +9,12 @@ */ /* BEGIN_CASE */ -void mbedtls_poly1305( data_t *key, char *hex_mac_string, data_t *src_str ) +void mbedtls_poly1305( data_t *key, data_t *expected_mac, data_t *src_str ) { unsigned char mac[16]; /* size set by the standard */ - unsigned char mac_str[33]; /* hex expansion of the above */ mbedtls_poly1305_context ctx; - memset( mac_str, 0x00, sizeof( mac_str ) ); - memset( mac, 0x00, sizeof( mac ) ); + memset( mac, 0x00, sizeof( mac ) ); /* * Test the integrated API @@ -24,8 +22,7 @@ void mbedtls_poly1305( data_t *key, char *hex_mac_string, data_t *src_str ) TEST_ASSERT( mbedtls_poly1305_mac( key->x, src_str->x, src_str->len, mac ) == 0 ); - mbedtls_test_hexify( mac_str, mac, 16 ); - TEST_ASSERT( strcmp( (char *) mac_str, hex_mac_string ) == 0 ); + TEST_ASSERT( !memcmp( mac, expected_mac->x, expected_mac->len ) ); /* * Test the streaming API @@ -38,8 +35,7 @@ void mbedtls_poly1305( data_t *key, char *hex_mac_string, data_t *src_str ) TEST_ASSERT( mbedtls_poly1305_finish( &ctx, mac ) == 0 ); - mbedtls_test_hexify( mac_str, mac, 16 ); - TEST_ASSERT( strcmp( (char *) mac_str, hex_mac_string ) == 0 ); + TEST_ASSERT( !memcmp( mac, expected_mac->x, expected_mac->len ) ); /* * Test the streaming API again, piecewise @@ -56,8 +52,7 @@ void mbedtls_poly1305( data_t *key, char *hex_mac_string, data_t *src_str ) TEST_ASSERT( mbedtls_poly1305_finish( &ctx, mac ) == 0 ); - mbedtls_test_hexify( mac_str, mac, 16 ); - TEST_ASSERT( strcmp( (char *) mac_str, hex_mac_string ) == 0 ); + TEST_ASSERT( !memcmp( mac, expected_mac->x, expected_mac->len ) ); } /* @@ -73,8 +68,7 @@ void mbedtls_poly1305( data_t *key, char *hex_mac_string, data_t *src_str ) TEST_ASSERT( mbedtls_poly1305_finish( &ctx, mac ) == 0 ); - mbedtls_test_hexify( mac_str, mac, 16 ); - TEST_ASSERT( strcmp( (char *) mac_str, hex_mac_string ) == 0 ); + TEST_ASSERT( !memcmp( mac, expected_mac->x, expected_mac->len ) ); } mbedtls_poly1305_free( &ctx );