tests: hkdf: Prepare to char* to data_t* type change

In preparation of changing the type of some parameters
of test_hkdf() from `char *` to `data_t` to get rid of the
calls to mbedtls_test_unhexify():

- Align naming of variables related to the expected okm
- Rename `okm_hex[]` to `okm_string[]`
- Added TEST_ASSERT( expected_okm_len <= sizeof( okm ) ) to check
  that the okm[] buffer is large enough for the okm output.

Signed-off-by: Ronald Cron <ronald.cron@arm.com>
This commit is contained in:
Ronald Cron 2020-06-25 10:26:42 +02:00
parent 6d1f0ad7b6
commit d7a4df8341

View file

@ -10,20 +10,20 @@
/* BEGIN_CASE */ /* BEGIN_CASE */
void test_hkdf( int md_alg, char *hex_ikm_string, char *hex_salt_string, void test_hkdf( int md_alg, char *hex_ikm_string, char *hex_salt_string,
char *hex_info_string, char *hex_okm_string ) char *hex_info_string, char *hex_expected_okm_string )
{ {
int ret; int ret;
size_t ikm_len, salt_len, info_len, okm_len; size_t ikm_len, salt_len, info_len, expected_okm_len;
unsigned char ikm[128] = { '\0' }; unsigned char ikm[128] = { '\0' };
unsigned char salt[128] = { '\0' }; unsigned char salt[128] = { '\0' };
unsigned char info[128] = { '\0' }; unsigned char info[128] = { '\0' };
unsigned char expected_okm[128] = { '\0' }; unsigned char expected_okm[128] = { '\0' };
unsigned char okm[128] = { '\0' }; unsigned char okm[128] = { '\0' };
/* /*
* okm_hex is the string representation of okm, * okm_string is the ASCII string representation of okm,
* so its size is twice the size of okm, and an extra null-termination. * so its size is twice the size of okm, and an extra null-termination.
*/ */
unsigned char okm_hex[257] = { '\0' }; unsigned char okm_string[257] = { '\0' };
const mbedtls_md_info_t *md = mbedtls_md_info_from_type( md_alg ); const mbedtls_md_info_t *md = mbedtls_md_info_from_type( md_alg );
TEST_ASSERT( md != NULL ); TEST_ASSERT( md != NULL );
@ -31,18 +31,21 @@ void test_hkdf( int md_alg, char *hex_ikm_string, char *hex_salt_string,
ikm_len = mbedtls_test_unhexify( ikm, hex_ikm_string ); ikm_len = mbedtls_test_unhexify( ikm, hex_ikm_string );
salt_len = mbedtls_test_unhexify( salt, hex_salt_string ); salt_len = mbedtls_test_unhexify( salt, hex_salt_string );
info_len = mbedtls_test_unhexify( info, hex_info_string ); info_len = mbedtls_test_unhexify( info, hex_info_string );
okm_len = mbedtls_test_unhexify( expected_okm, hex_okm_string ); expected_okm_len = mbedtls_test_unhexify( expected_okm,
hex_expected_okm_string );
TEST_ASSERT( expected_okm_len <= sizeof( okm ) );
ret = mbedtls_hkdf( md, salt, salt_len, ikm, ikm_len, info, info_len, okm, ret = mbedtls_hkdf( md, salt, salt_len, ikm, ikm_len, info, info_len, okm,
okm_len); expected_okm_len);
TEST_ASSERT( ret == 0 ); TEST_ASSERT( ret == 0 );
/* /*
* Run mbedtls_test_hexify on it so that it looks nicer if the assertion * Run mbedtls_test_hexify on it so that it looks nicer if the assertion
* fails. * fails.
*/ */
mbedtls_test_hexify( okm_hex, okm, okm_len ); mbedtls_test_hexify( okm_string, okm, expected_okm_len );
TEST_ASSERT( !strcmp( (char *)okm_hex, hex_okm_string ) ); TEST_ASSERT( !strcmp( (char *)okm_string, hex_expected_okm_string ) );
} }
/* END_CASE */ /* END_CASE */