2016-07-17 09:51:22 +02:00
|
|
|
/* BEGIN_HEADER */
|
|
|
|
#include "mbedtls/hkdf.h"
|
2021-03-09 18:03:29 +01:00
|
|
|
#include "md_wrap.h"
|
2016-07-17 09:51:22 +02:00
|
|
|
/* END_HEADER */
|
|
|
|
|
|
|
|
/* BEGIN_DEPENDENCIES
|
|
|
|
* depends_on:MBEDTLS_HKDF_C
|
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2020-06-25 09:03:34 +02:00
|
|
|
void test_hkdf( int md_alg, data_t *ikm, data_t *salt, data_t *info,
|
|
|
|
data_t *expected_okm )
|
2016-07-17 09:51:22 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2018-11-14 19:22:03 +01:00
|
|
|
unsigned char okm[128] = { '\0' };
|
2016-07-17 09:51:22 +02:00
|
|
|
|
|
|
|
const mbedtls_md_info_t *md = mbedtls_md_info_from_type( md_alg );
|
|
|
|
TEST_ASSERT( md != NULL );
|
|
|
|
|
2020-06-25 09:03:34 +02:00
|
|
|
TEST_ASSERT( expected_okm->len <= sizeof( okm ) );
|
2020-06-25 10:26:42 +02:00
|
|
|
|
2020-06-25 09:03:34 +02:00
|
|
|
ret = mbedtls_hkdf( md, salt->x, salt->len, ikm->x, ikm->len,
|
|
|
|
info->x, info->len, okm, expected_okm->len );
|
2016-07-17 09:51:22 +02:00
|
|
|
TEST_ASSERT( ret == 0 );
|
|
|
|
|
2020-07-30 14:18:02 +02:00
|
|
|
ASSERT_COMPARE( okm , expected_okm->len,
|
|
|
|
expected_okm->x, expected_okm->len );
|
2016-07-17 09:51:22 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2018-05-23 19:55:47 +02:00
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2022-03-16 16:53:23 +01:00
|
|
|
void test_hkdf_extract( int md_alg,
|
|
|
|
data_t *ikm,
|
|
|
|
data_t *salt,
|
|
|
|
data_t *prk )
|
2018-05-23 19:55:47 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
unsigned char *output_prk = NULL;
|
2022-03-16 16:53:23 +01:00
|
|
|
size_t output_prk_len;
|
2018-05-23 19:55:47 +02:00
|
|
|
|
|
|
|
const mbedtls_md_info_t *md = mbedtls_md_info_from_type( md_alg );
|
|
|
|
TEST_ASSERT( md != NULL );
|
|
|
|
|
|
|
|
output_prk_len = mbedtls_md_get_size( md );
|
2022-02-07 18:21:39 +01:00
|
|
|
ASSERT_ALLOC( output_prk, output_prk_len );
|
2018-05-23 19:55:47 +02:00
|
|
|
|
2022-03-16 16:53:23 +01:00
|
|
|
ret = mbedtls_hkdf_extract( md, salt->x, salt->len,
|
|
|
|
ikm->x, ikm->len, output_prk );
|
2018-05-23 19:55:47 +02:00
|
|
|
TEST_ASSERT( ret == 0 );
|
|
|
|
|
2022-03-16 16:53:23 +01:00
|
|
|
ASSERT_COMPARE( output_prk, output_prk_len, prk->x, prk->len );
|
2018-05-23 19:55:47 +02:00
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_free(output_prk);
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2022-03-16 16:53:23 +01:00
|
|
|
void test_hkdf_expand( int md_alg,
|
|
|
|
data_t *info,
|
|
|
|
data_t *prk,
|
|
|
|
data_t *okm )
|
2018-05-23 19:55:47 +02:00
|
|
|
{
|
|
|
|
enum { OKM_LEN = 1024 };
|
|
|
|
int ret;
|
|
|
|
unsigned char *output_okm = NULL;
|
|
|
|
|
|
|
|
const mbedtls_md_info_t *md = mbedtls_md_info_from_type( md_alg );
|
|
|
|
TEST_ASSERT( md != NULL );
|
|
|
|
|
2022-02-09 16:31:48 +01:00
|
|
|
ASSERT_ALLOC( output_okm, OKM_LEN );
|
2018-05-23 19:55:47 +02:00
|
|
|
|
2022-03-16 16:53:23 +01:00
|
|
|
TEST_ASSERT( prk->len == mbedtls_md_get_size( md ) );
|
|
|
|
TEST_ASSERT( okm->len < OKM_LEN );
|
2018-05-23 19:55:47 +02:00
|
|
|
|
2022-03-16 16:53:23 +01:00
|
|
|
ret = mbedtls_hkdf_expand( md, prk->x, prk->len,
|
|
|
|
info->x, info->len,
|
2018-05-23 19:55:47 +02:00
|
|
|
output_okm, OKM_LEN );
|
|
|
|
TEST_ASSERT( ret == 0 );
|
2022-03-16 16:53:23 +01:00
|
|
|
ASSERT_COMPARE( output_okm, okm->len, okm->x, okm->len );
|
2018-05-23 19:55:47 +02:00
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_free(output_okm);
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
2018-05-23 20:44:26 +02:00
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void test_hkdf_extract_ret( int hash_len, int ret )
|
|
|
|
{
|
|
|
|
int output_ret;
|
|
|
|
unsigned char *salt = NULL;
|
|
|
|
unsigned char *ikm = NULL;
|
|
|
|
unsigned char *prk = NULL;
|
|
|
|
size_t salt_len, ikm_len;
|
|
|
|
struct mbedtls_md_info_t fake_md_info;
|
|
|
|
|
|
|
|
memset( &fake_md_info, 0, sizeof( fake_md_info ) );
|
|
|
|
fake_md_info.type = MBEDTLS_MD_NONE;
|
|
|
|
fake_md_info.size = hash_len;
|
|
|
|
|
2022-02-07 18:21:39 +01:00
|
|
|
ASSERT_ALLOC( prk, MBEDTLS_MD_MAX_SIZE);
|
2018-05-23 20:44:26 +02:00
|
|
|
salt_len = 0;
|
|
|
|
ikm_len = 0;
|
|
|
|
|
|
|
|
output_ret = mbedtls_hkdf_extract( &fake_md_info, salt, salt_len,
|
|
|
|
ikm, ikm_len, prk );
|
|
|
|
TEST_ASSERT( output_ret == ret );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_free(prk);
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void test_hkdf_expand_ret( int hash_len, int prk_len, int okm_len, int ret )
|
|
|
|
{
|
|
|
|
int output_ret;
|
|
|
|
unsigned char *info = NULL;
|
|
|
|
unsigned char *prk = NULL;
|
|
|
|
unsigned char *okm = NULL;
|
|
|
|
size_t info_len;
|
|
|
|
struct mbedtls_md_info_t fake_md_info;
|
|
|
|
|
|
|
|
memset( &fake_md_info, 0, sizeof( fake_md_info ) );
|
|
|
|
fake_md_info.type = MBEDTLS_MD_NONE;
|
|
|
|
fake_md_info.size = hash_len;
|
|
|
|
|
|
|
|
info_len = 0;
|
|
|
|
|
|
|
|
if (prk_len > 0)
|
2022-02-09 16:31:48 +01:00
|
|
|
ASSERT_ALLOC( prk, prk_len );
|
2018-05-23 20:44:26 +02:00
|
|
|
|
|
|
|
if (okm_len > 0)
|
2022-02-09 16:31:48 +01:00
|
|
|
ASSERT_ALLOC( okm, okm_len );
|
2018-05-23 20:44:26 +02:00
|
|
|
|
|
|
|
output_ret = mbedtls_hkdf_expand( &fake_md_info, prk, prk_len,
|
|
|
|
info, info_len, okm, okm_len );
|
|
|
|
TEST_ASSERT( output_ret == ret );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_free(prk);
|
|
|
|
mbedtls_free(okm);
|
|
|
|
}
|
|
|
|
/* END_CASE */
|