Skip to content

Commit

Permalink
tests: Remove usage of mbedtls_test_hexify for comparison
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
ronald-cron-arm committed Jul 2, 2020
1 parent ac6ae35 commit 55d97f2
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 95 deletions.
10 changes: 3 additions & 7 deletions tests/suites/test_suite_aes.function
Original file line number Diff line number Diff line change
Expand Up @@ -321,17 +321,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 ) );
Expand All @@ -346,12 +344,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 )
Expand Down
62 changes: 20 additions & 42 deletions tests/suites/test_suite_aria.function
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand All @@ -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:
Expand All @@ -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 );

Expand All @@ -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:
Expand All @@ -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 );

Expand All @@ -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:
Expand All @@ -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 );

Expand All @@ -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:
Expand All @@ -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 );

Expand All @@ -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 );
Expand All @@ -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 );

Expand All @@ -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 );
Expand All @@ -385,26 +369,23 @@ 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 );

mbedtls_aria_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
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 );
Expand All @@ -413,26 +394,23 @@ 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 );

mbedtls_aria_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
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 );
Expand Down
25 changes: 6 additions & 19 deletions tests/suites/test_suite_chacha20.function
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 );
}
Expand Down
16 changes: 1 addition & 15 deletions tests/suites/test_suite_hkdf.function
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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 */

Expand Down
18 changes: 6 additions & 12 deletions tests/suites/test_suite_poly1305.function
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,20 @@
*/

/* 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
*/
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
Expand All @@ -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
Expand All @@ -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 ) );
}

/*
Expand All @@ -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 );
Expand Down

0 comments on commit 55d97f2

Please sign in to comment.