1/* BEGIN_HEADER */
2#include "mbedtls/sha1.h"
3#include "mbedtls/sha256.h"
4#include "mbedtls/sha512.h"
5#include "mbedtls/sha3.h"
6/* END_HEADER */
7
8/* BEGIN_CASE depends_on:MBEDTLS_SHA1_C */
9void mbedtls_sha1(data_t *src_str, data_t *hash)
10{
11    unsigned char output[41];
12
13    memset(output, 0x00, 41);
14
15
16    TEST_ASSERT(mbedtls_sha1(src_str->x, src_str->len, output) == 0);
17
18    TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x, 20, hash->len) == 0);
19}
20/* END_CASE */
21
22/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
23void sha256_invalid_param()
24{
25    mbedtls_sha256_context ctx;
26    unsigned char buf[64] = { 0 };
27    size_t const buflen = sizeof(buf);
28    int invalid_type = 42;
29
30    TEST_EQUAL(MBEDTLS_ERR_SHA256_BAD_INPUT_DATA,
31               mbedtls_sha256_starts(&ctx, invalid_type));
32
33    TEST_EQUAL(MBEDTLS_ERR_SHA256_BAD_INPUT_DATA,
34               mbedtls_sha256(buf, buflen,
35                              buf, invalid_type));
36
37exit:
38    return;
39}
40/* END_CASE */
41
42/* BEGIN_CASE depends_on:MBEDTLS_SHA224_C */
43void sha224(data_t *src_str, data_t *hash)
44{
45    unsigned char output[57];
46
47    memset(output, 0x00, 57);
48
49
50    TEST_EQUAL(mbedtls_sha256(src_str->x, src_str->len, output, 1), 0);
51
52    TEST_EQUAL(mbedtls_test_hexcmp(output, hash->x, 28, hash->len), 0);
53}
54/* END_CASE */
55
56/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
57void mbedtls_sha256(data_t *src_str, data_t *hash)
58{
59    unsigned char output[65];
60
61    memset(output, 0x00, 65);
62
63
64    TEST_EQUAL(mbedtls_sha256(src_str->x, src_str->len, output, 0), 0);
65
66    TEST_EQUAL(mbedtls_test_hexcmp(output, hash->x, 32, hash->len), 0);
67}
68/* END_CASE */
69
70/* BEGIN_CASE depends_on:MBEDTLS_SHA512_C */
71void sha512_invalid_param()
72{
73    mbedtls_sha512_context ctx;
74    unsigned char buf[64] = { 0 };
75    size_t const buflen = sizeof(buf);
76    int invalid_type = 42;
77
78    TEST_EQUAL(MBEDTLS_ERR_SHA512_BAD_INPUT_DATA,
79               mbedtls_sha512_starts(&ctx, invalid_type));
80
81    TEST_EQUAL(MBEDTLS_ERR_SHA512_BAD_INPUT_DATA,
82               mbedtls_sha512(buf, buflen,
83                              buf, invalid_type));
84
85exit:
86    return;
87}
88/* END_CASE */
89
90/* BEGIN_CASE depends_on:MBEDTLS_SHA384_C */
91void sha384(data_t *src_str, data_t *hash)
92{
93    unsigned char output[97];
94
95    memset(output, 0x00, 97);
96
97
98    TEST_EQUAL(mbedtls_sha512(src_str->x, src_str->len, output, 1), 0);
99
100    TEST_EQUAL(mbedtls_test_hexcmp(output, hash->x, 48, hash->len), 0);
101}
102/* END_CASE */
103
104/* BEGIN_CASE depends_on:MBEDTLS_SHA512_C */
105void mbedtls_sha512(data_t *src_str, data_t *hash)
106{
107    unsigned char output[129];
108
109    memset(output, 0x00, 129);
110
111
112    TEST_EQUAL(mbedtls_sha512(src_str->x, src_str->len, output, 0), 0);
113
114    TEST_EQUAL(mbedtls_test_hexcmp(output, hash->x, 64, hash->len), 0);
115}
116/* END_CASE */
117
118/* BEGIN_CASE depends_on:MBEDTLS_SHA1_C:MBEDTLS_SELF_TEST */
119void sha1_selftest()
120{
121    TEST_ASSERT(mbedtls_sha1_self_test(1) == 0);
122}
123/* END_CASE */
124
125/* BEGIN_CASE depends_on:MBEDTLS_SHA224_C:MBEDTLS_SELF_TEST */
126void sha224_selftest()
127{
128    TEST_EQUAL(mbedtls_sha224_self_test(1), 0);
129}
130/* END_CASE */
131
132/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C:MBEDTLS_SELF_TEST */
133void sha256_selftest()
134{
135    TEST_EQUAL(mbedtls_sha256_self_test(1), 0);
136}
137/* END_CASE */
138
139/* BEGIN_CASE depends_on:MBEDTLS_SHA384_C:MBEDTLS_SELF_TEST */
140void sha384_selftest()
141{
142    TEST_EQUAL(mbedtls_sha384_self_test(1), 0);
143}
144/* END_CASE */
145
146/* BEGIN_CASE depends_on:MBEDTLS_SHA512_C:MBEDTLS_SELF_TEST */
147void sha512_selftest()
148{
149    TEST_EQUAL(mbedtls_sha512_self_test(1), 0);
150}
151/* END_CASE */
152
153/* BEGIN_CASE depends_on:MBEDTLS_SHA3_C */
154void mbedtls_sha3(int family, data_t *in, data_t *hash)
155{
156    unsigned char *output = NULL;
157
158    TEST_CALLOC(output, hash->len);
159
160    TEST_ASSERT(mbedtls_sha3(family, in->x, in->len, output, hash->len) == 0);
161
162    TEST_MEMORY_COMPARE(output, hash->len, hash->x, hash->len);
163
164exit:
165    mbedtls_free(output);
166}
167/* END_CASE */
168
169/* BEGIN_CASE depends_on:MBEDTLS_SHA3_C */
170void sha3_invalid_param()
171{
172    unsigned char output[32];
173    mbedtls_sha3_context ctx;
174
175    mbedtls_sha3_init(&ctx);
176    TEST_EQUAL(mbedtls_sha3_starts(&ctx, MBEDTLS_SHA3_NONE), MBEDTLS_ERR_SHA3_BAD_INPUT_DATA);
177
178    TEST_EQUAL(mbedtls_sha3_starts(&ctx, MBEDTLS_SHA3_256), 0);
179    TEST_EQUAL(mbedtls_sha3_finish(&ctx, output, 0), MBEDTLS_ERR_SHA3_BAD_INPUT_DATA);
180
181    TEST_EQUAL(mbedtls_sha3_starts(&ctx, MBEDTLS_SHA3_256), 0);
182    TEST_EQUAL(mbedtls_sha3_finish(&ctx, output, 31), MBEDTLS_ERR_SHA3_BAD_INPUT_DATA);
183
184    TEST_EQUAL(mbedtls_sha3_starts(&ctx, MBEDTLS_SHA3_256), 0);
185    TEST_EQUAL(mbedtls_sha3_finish(&ctx, output, 32), 0);
186
187exit:
188    return;
189}
190/* END_CASE */
191
192/* BEGIN_CASE depends_on:MBEDTLS_SHA3_C */
193void mbedtls_sha3_multi(int family, data_t *in, data_t *hash)
194{
195    unsigned char *output = NULL;
196    mbedtls_sha3_context ctx;
197    const unsigned int block_size = 256;
198
199    TEST_CALLOC(output, hash->len);
200
201    mbedtls_sha3_init(&ctx);
202    mbedtls_sha3_starts(&ctx, family);
203
204    for (size_t l = 0; l < in->len; l += block_size) {
205        TEST_ASSERT(mbedtls_sha3_update(&ctx, in->x + l, MIN(in->len - l, block_size)) == 0);
206    }
207
208    TEST_ASSERT(mbedtls_sha3_finish(&ctx, output, hash->len) == 0);
209
210    TEST_MEMORY_COMPARE(output, hash->len, hash->x, hash->len);
211
212exit:
213    mbedtls_free(output);
214}
215/* END_CASE */
216
217/* BEGIN_CASE depends_on:MBEDTLS_SHA3_C */
218void sha3_streaming(int type, data_t *input)
219{
220    mbedtls_sha3_context ctx;
221    unsigned char reference_hash[64];
222    unsigned char hash[64];
223    size_t chunk_size;
224    size_t hash_length = (type == MBEDTLS_SHA3_224 ? 28 :
225                          type == MBEDTLS_SHA3_256 ? 32 :
226                          type == MBEDTLS_SHA3_384 ? 48 :
227                          type == MBEDTLS_SHA3_512 ? 64 :
228                          0);
229
230    mbedtls_sha3_init(&ctx);
231    memset(reference_hash, 0, sizeof(reference_hash));
232    memset(hash, 0, sizeof(hash));
233    TEST_ASSERT(hash_length != 0);
234
235    /* Generate a reference hash */
236    mbedtls_sha3(type, input->x, input->len, reference_hash, hash_length);
237
238    /* Repeat each test with increasingly-sized data chunks
239     * E.g. start by processing bytes individual bytes, then 2-byte chunks,
240     * then 3-byte chunks, and so on...
241     * At each test ensure that the same hash is generated.
242     */
243    for (chunk_size = 1; chunk_size < input->len; chunk_size++) {
244        size_t i;
245        size_t remaining = input->len;
246
247        mbedtls_sha3_init(&ctx);
248        TEST_ASSERT(mbedtls_sha3_starts(&ctx, type) == 0);
249
250        for (i = 0; i < input->len; i += chunk_size) {
251            size_t len = remaining >= chunk_size ? chunk_size : remaining;
252            TEST_ASSERT(mbedtls_sha3_update(&ctx, input->x + i, len) == 0);
253            remaining -= len;
254        }
255
256        mbedtls_sha3_finish(&ctx, hash, hash_length);
257        mbedtls_sha3_free(&ctx);
258
259        TEST_MEMORY_COMPARE(hash, hash_length, reference_hash, hash_length);
260    }
261
262exit:
263    mbedtls_sha3_free(&ctx);
264}
265/* END_CASE */
266
267/* BEGIN_CASE depends_on:MBEDTLS_SHA3_C */
268void sha3_reuse(data_t *input1, data_t *hash1,
269                data_t *input2, data_t *hash2)
270{
271    unsigned char output[64];
272    mbedtls_sha3_context ctx;
273    mbedtls_sha3_id type1, type2;
274
275    mbedtls_sha3_init(&ctx);
276    switch (hash1->len) {
277        case 28: type1 = MBEDTLS_SHA3_224; break;
278        case 32: type1 = MBEDTLS_SHA3_256; break;
279        case 48: type1 = MBEDTLS_SHA3_384; break;
280        case 64: type1 = MBEDTLS_SHA3_512; break;
281        default: TEST_FAIL("hash1->len validity"); break;
282    }
283    switch (hash2->len) {
284        case 28: type2 = MBEDTLS_SHA3_224; break;
285        case 32: type2 = MBEDTLS_SHA3_256; break;
286        case 48: type2 = MBEDTLS_SHA3_384; break;
287        case 64: type2 = MBEDTLS_SHA3_512; break;
288        default: TEST_FAIL("hash2->len validity"); break;
289    }
290
291    /* Round 1 */
292    TEST_ASSERT(mbedtls_sha3_starts(&ctx, type1) == 0);
293    TEST_ASSERT(mbedtls_sha3_update(&ctx, input1->x, input1->len) == 0);
294    TEST_ASSERT(mbedtls_sha3_finish(&ctx, output, sizeof(output)) == 0);
295    TEST_MEMORY_COMPARE(output, hash1->len, hash1->x, hash1->len);
296
297    /* Round 2 */
298    TEST_ASSERT(mbedtls_sha3_starts(&ctx, type2) == 0);
299    TEST_ASSERT(mbedtls_sha3_update(&ctx, input2->x, input2->len) == 0);
300    TEST_ASSERT(mbedtls_sha3_finish(&ctx, output, sizeof(output)) == 0);
301    TEST_MEMORY_COMPARE(output, hash2->len, hash2->x, hash2->len);
302
303exit:
304    mbedtls_sha3_free(&ctx);
305}
306/* END_CASE */
307
308/* BEGIN_CASE depends_on:MBEDTLS_SHA3_C:MBEDTLS_SELF_TEST */
309void sha3_selftest()
310{
311    TEST_ASSERT(mbedtls_sha3_self_test(0) == 0);
312}
313/* END_CASE */
314