1/* BEGIN_HEADER */
2#include "mbedtls/hkdf.h"
3#include "md_wrap.h"
4/* END_HEADER */
5
6/* BEGIN_DEPENDENCIES
7 * depends_on:MBEDTLS_HKDF_C
8 * END_DEPENDENCIES
9 */
10
11/* BEGIN_CASE */
12void test_hkdf(int md_alg, data_t *ikm, data_t *salt, data_t *info,
13               data_t *expected_okm)
14{
15    int ret;
16    unsigned char okm[128] = { '\0' };
17
18    MD_PSA_INIT();
19
20    const mbedtls_md_info_t *md = mbedtls_md_info_from_type(md_alg);
21    TEST_ASSERT(md != NULL);
22
23    TEST_ASSERT(expected_okm->len <= sizeof(okm));
24
25    ret = mbedtls_hkdf(md, salt->x, salt->len, ikm->x, ikm->len,
26                       info->x, info->len, okm, expected_okm->len);
27    TEST_ASSERT(ret == 0);
28
29    TEST_MEMORY_COMPARE(okm, expected_okm->len,
30                        expected_okm->x, expected_okm->len);
31
32exit:
33    MD_PSA_DONE();
34}
35/* END_CASE */
36
37/* BEGIN_CASE */
38void test_hkdf_extract(int md_alg,
39                       data_t *ikm,
40                       data_t *salt,
41                       data_t *prk)
42{
43    int ret;
44    unsigned char *output_prk = NULL;
45    size_t output_prk_len;
46
47    MD_PSA_INIT();
48
49    const mbedtls_md_info_t *md = mbedtls_md_info_from_type(md_alg);
50    TEST_ASSERT(md != NULL);
51
52    output_prk_len = mbedtls_md_get_size(md);
53    TEST_CALLOC(output_prk, output_prk_len);
54
55    ret = mbedtls_hkdf_extract(md, salt->x, salt->len,
56                               ikm->x, ikm->len, output_prk);
57    TEST_ASSERT(ret == 0);
58
59    TEST_MEMORY_COMPARE(output_prk, output_prk_len, prk->x, prk->len);
60
61exit:
62    mbedtls_free(output_prk);
63    MD_PSA_DONE();
64}
65/* END_CASE */
66
67/* BEGIN_CASE */
68void test_hkdf_expand(int md_alg,
69                      data_t *info,
70                      data_t *prk,
71                      data_t *okm)
72{
73    enum { OKM_LEN  = 1024 };
74    int ret;
75    unsigned char *output_okm = NULL;
76
77    MD_PSA_INIT();
78
79    const mbedtls_md_info_t *md = mbedtls_md_info_from_type(md_alg);
80    TEST_ASSERT(md != NULL);
81
82    TEST_CALLOC(output_okm, OKM_LEN);
83
84    TEST_ASSERT(prk->len == mbedtls_md_get_size(md));
85    TEST_ASSERT(okm->len < OKM_LEN);
86
87    ret = mbedtls_hkdf_expand(md, prk->x, prk->len,
88                              info->x, info->len,
89                              output_okm, OKM_LEN);
90    TEST_ASSERT(ret == 0);
91    TEST_MEMORY_COMPARE(output_okm, okm->len, okm->x, okm->len);
92
93exit:
94    mbedtls_free(output_okm);
95    MD_PSA_DONE();
96}
97/* END_CASE */
98
99/* BEGIN_CASE */
100void test_hkdf_extract_ret(int hash_len, int ret)
101{
102    int output_ret;
103    unsigned char *salt = NULL;
104    unsigned char *ikm = NULL;
105    unsigned char *prk = NULL;
106    size_t salt_len, ikm_len;
107    struct mbedtls_md_info_t fake_md_info;
108
109    memset(&fake_md_info, 0, sizeof(fake_md_info));
110    fake_md_info.type = MBEDTLS_MD_NONE;
111    fake_md_info.size = hash_len;
112
113    TEST_CALLOC(prk, MBEDTLS_MD_MAX_SIZE);
114    salt_len = 0;
115    ikm_len = 0;
116
117    output_ret = mbedtls_hkdf_extract(&fake_md_info, salt, salt_len,
118                                      ikm, ikm_len, prk);
119    TEST_ASSERT(output_ret == ret);
120
121exit:
122    mbedtls_free(prk);
123}
124/* END_CASE */
125
126/* BEGIN_CASE */
127void test_hkdf_expand_ret(int hash_len, int prk_len, int okm_len, int ret)
128{
129    int output_ret;
130    unsigned char *info = NULL;
131    unsigned char *prk = NULL;
132    unsigned char *okm = NULL;
133    size_t info_len;
134    struct mbedtls_md_info_t fake_md_info;
135
136    memset(&fake_md_info, 0, sizeof(fake_md_info));
137    fake_md_info.type = MBEDTLS_MD_NONE;
138    fake_md_info.size = hash_len;
139
140    info_len = 0;
141
142    if (prk_len > 0) {
143        TEST_CALLOC(prk, prk_len);
144    }
145
146    if (okm_len > 0) {
147        TEST_CALLOC(okm, okm_len);
148    }
149
150    output_ret = mbedtls_hkdf_expand(&fake_md_info, prk, prk_len,
151                                     info, info_len, okm, okm_len);
152    TEST_ASSERT(output_ret == ret);
153
154exit:
155    mbedtls_free(prk);
156    mbedtls_free(okm);
157}
158/* END_CASE */
159