File: test_suite_shax.function

package info (click to toggle)
mbedtls 3.6.5-0.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 51,488 kB
  • sloc: ansic: 164,842; sh: 25,443; python: 15,512; makefile: 3,131; perl: 1,043; tcl: 4
file content (313 lines) | stat: -rw-r--r-- 8,558 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/* BEGIN_HEADER */
#include "mbedtls/sha1.h"
#include "mbedtls/sha256.h"
#include "mbedtls/sha512.h"
#include "mbedtls/sha3.h"
/* END_HEADER */

/* BEGIN_CASE depends_on:MBEDTLS_SHA1_C */
void mbedtls_sha1(data_t *src_str, data_t *hash)
{
    unsigned char output[41];

    memset(output, 0x00, 41);


    TEST_ASSERT(mbedtls_sha1(src_str->x, src_str->len, output) == 0);

    TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x, 20, hash->len) == 0);
}
/* END_CASE */

/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
void sha256_invalid_param()
{
    mbedtls_sha256_context ctx;
    unsigned char buf[64] = { 0 };
    size_t const buflen = sizeof(buf);
    int invalid_type = 42;

    TEST_EQUAL(MBEDTLS_ERR_SHA256_BAD_INPUT_DATA,
               mbedtls_sha256_starts(&ctx, invalid_type));

    TEST_EQUAL(MBEDTLS_ERR_SHA256_BAD_INPUT_DATA,
               mbedtls_sha256(buf, buflen,
                              buf, invalid_type));

exit:
    return;
}
/* END_CASE */

/* BEGIN_CASE depends_on:MBEDTLS_SHA224_C */
void sha224(data_t *src_str, data_t *hash)
{
    unsigned char output[57];

    memset(output, 0x00, 57);


    TEST_EQUAL(mbedtls_sha256(src_str->x, src_str->len, output, 1), 0);

    TEST_EQUAL(mbedtls_test_hexcmp(output, hash->x, 28, hash->len), 0);
}
/* END_CASE */

/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
void mbedtls_sha256(data_t *src_str, data_t *hash)
{
    unsigned char output[65];

    memset(output, 0x00, 65);


    TEST_EQUAL(mbedtls_sha256(src_str->x, src_str->len, output, 0), 0);

    TEST_EQUAL(mbedtls_test_hexcmp(output, hash->x, 32, hash->len), 0);
}
/* END_CASE */

/* BEGIN_CASE depends_on:MBEDTLS_SHA512_C */
void sha512_invalid_param()
{
    mbedtls_sha512_context ctx;
    unsigned char buf[64] = { 0 };
    size_t const buflen = sizeof(buf);
    int invalid_type = 42;

    TEST_EQUAL(MBEDTLS_ERR_SHA512_BAD_INPUT_DATA,
               mbedtls_sha512_starts(&ctx, invalid_type));

    TEST_EQUAL(MBEDTLS_ERR_SHA512_BAD_INPUT_DATA,
               mbedtls_sha512(buf, buflen,
                              buf, invalid_type));

exit:
    return;
}
/* END_CASE */

/* BEGIN_CASE depends_on:MBEDTLS_SHA384_C */
void sha384(data_t *src_str, data_t *hash)
{
    unsigned char output[97];

    memset(output, 0x00, 97);


    TEST_EQUAL(mbedtls_sha512(src_str->x, src_str->len, output, 1), 0);

    TEST_EQUAL(mbedtls_test_hexcmp(output, hash->x, 48, hash->len), 0);
}
/* END_CASE */

/* BEGIN_CASE depends_on:MBEDTLS_SHA512_C */
void mbedtls_sha512(data_t *src_str, data_t *hash)
{
    unsigned char output[129];

    memset(output, 0x00, 129);


    TEST_EQUAL(mbedtls_sha512(src_str->x, src_str->len, output, 0), 0);

    TEST_EQUAL(mbedtls_test_hexcmp(output, hash->x, 64, hash->len), 0);
}
/* END_CASE */

/* BEGIN_CASE depends_on:MBEDTLS_SHA1_C:MBEDTLS_SELF_TEST */
void sha1_selftest()
{
    TEST_ASSERT(mbedtls_sha1_self_test(1) == 0);
}
/* END_CASE */

/* BEGIN_CASE depends_on:MBEDTLS_SHA224_C:MBEDTLS_SELF_TEST */
void sha224_selftest()
{
    TEST_EQUAL(mbedtls_sha224_self_test(1), 0);
}
/* END_CASE */

/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C:MBEDTLS_SELF_TEST */
void sha256_selftest()
{
    TEST_EQUAL(mbedtls_sha256_self_test(1), 0);
}
/* END_CASE */

/* BEGIN_CASE depends_on:MBEDTLS_SHA384_C:MBEDTLS_SELF_TEST */
void sha384_selftest()
{
    TEST_EQUAL(mbedtls_sha384_self_test(1), 0);
}
/* END_CASE */

/* BEGIN_CASE depends_on:MBEDTLS_SHA512_C:MBEDTLS_SELF_TEST */
void sha512_selftest()
{
    TEST_EQUAL(mbedtls_sha512_self_test(1), 0);
}
/* END_CASE */

/* BEGIN_CASE depends_on:MBEDTLS_SHA3_C */
void mbedtls_sha3(int family, data_t *in, data_t *hash)
{
    unsigned char *output = NULL;

    TEST_CALLOC(output, hash->len);

    TEST_ASSERT(mbedtls_sha3(family, in->x, in->len, output, hash->len) == 0);

    TEST_MEMORY_COMPARE(output, hash->len, hash->x, hash->len);

exit:
    mbedtls_free(output);
}
/* END_CASE */

/* BEGIN_CASE depends_on:MBEDTLS_SHA3_C */
void sha3_invalid_param()
{
    unsigned char output[32];
    mbedtls_sha3_context ctx;

    mbedtls_sha3_init(&ctx);
    TEST_EQUAL(mbedtls_sha3_starts(&ctx, MBEDTLS_SHA3_NONE), MBEDTLS_ERR_SHA3_BAD_INPUT_DATA);

    TEST_EQUAL(mbedtls_sha3_starts(&ctx, MBEDTLS_SHA3_256), 0);
    TEST_EQUAL(mbedtls_sha3_finish(&ctx, output, 0), MBEDTLS_ERR_SHA3_BAD_INPUT_DATA);

    TEST_EQUAL(mbedtls_sha3_starts(&ctx, MBEDTLS_SHA3_256), 0);
    TEST_EQUAL(mbedtls_sha3_finish(&ctx, output, 31), MBEDTLS_ERR_SHA3_BAD_INPUT_DATA);

    TEST_EQUAL(mbedtls_sha3_starts(&ctx, MBEDTLS_SHA3_256), 0);
    TEST_EQUAL(mbedtls_sha3_finish(&ctx, output, 32), 0);

exit:
    return;
}
/* END_CASE */

/* BEGIN_CASE depends_on:MBEDTLS_SHA3_C */
void mbedtls_sha3_multi(int family, data_t *in, data_t *hash)
{
    unsigned char *output = NULL;
    mbedtls_sha3_context ctx;
    const unsigned int block_size = 256;

    TEST_CALLOC(output, hash->len);

    mbedtls_sha3_init(&ctx);
    mbedtls_sha3_starts(&ctx, family);

    for (size_t l = 0; l < in->len; l += block_size) {
        TEST_ASSERT(mbedtls_sha3_update(&ctx, in->x + l, MIN(in->len - l, block_size)) == 0);
    }

    TEST_ASSERT(mbedtls_sha3_finish(&ctx, output, hash->len) == 0);

    TEST_MEMORY_COMPARE(output, hash->len, hash->x, hash->len);

exit:
    mbedtls_free(output);
}
/* END_CASE */

/* BEGIN_CASE depends_on:MBEDTLS_SHA3_C */
void sha3_streaming(int type, data_t *input)
{
    mbedtls_sha3_context ctx;
    unsigned char reference_hash[64];
    unsigned char hash[64];
    size_t chunk_size;
    size_t hash_length = (type == MBEDTLS_SHA3_224 ? 28 :
                          type == MBEDTLS_SHA3_256 ? 32 :
                          type == MBEDTLS_SHA3_384 ? 48 :
                          type == MBEDTLS_SHA3_512 ? 64 :
                          0);

    mbedtls_sha3_init(&ctx);
    memset(reference_hash, 0, sizeof(reference_hash));
    memset(hash, 0, sizeof(hash));
    TEST_ASSERT(hash_length != 0);

    /* Generate a reference hash */
    mbedtls_sha3(type, input->x, input->len, reference_hash, hash_length);

    /* Repeat each test with increasingly-sized data chunks
     * E.g. start by processing bytes individual bytes, then 2-byte chunks,
     * then 3-byte chunks, and so on...
     * At each test ensure that the same hash is generated.
     */
    for (chunk_size = 1; chunk_size < input->len; chunk_size++) {
        size_t i;
        size_t remaining = input->len;

        mbedtls_sha3_init(&ctx);
        TEST_ASSERT(mbedtls_sha3_starts(&ctx, type) == 0);

        for (i = 0; i < input->len; i += chunk_size) {
            size_t len = remaining >= chunk_size ? chunk_size : remaining;
            TEST_ASSERT(mbedtls_sha3_update(&ctx, input->x + i, len) == 0);
            remaining -= len;
        }

        mbedtls_sha3_finish(&ctx, hash, hash_length);
        mbedtls_sha3_free(&ctx);

        TEST_MEMORY_COMPARE(hash, hash_length, reference_hash, hash_length);
    }

exit:
    mbedtls_sha3_free(&ctx);
}
/* END_CASE */

/* BEGIN_CASE depends_on:MBEDTLS_SHA3_C */
void sha3_reuse(data_t *input1, data_t *hash1,
                data_t *input2, data_t *hash2)
{
    unsigned char output[64];
    mbedtls_sha3_context ctx;
    mbedtls_sha3_id type1, type2;

    mbedtls_sha3_init(&ctx);
    switch (hash1->len) {
        case 28: type1 = MBEDTLS_SHA3_224; break;
        case 32: type1 = MBEDTLS_SHA3_256; break;
        case 48: type1 = MBEDTLS_SHA3_384; break;
        case 64: type1 = MBEDTLS_SHA3_512; break;
        default: TEST_FAIL("hash1->len validity"); break;
    }
    switch (hash2->len) {
        case 28: type2 = MBEDTLS_SHA3_224; break;
        case 32: type2 = MBEDTLS_SHA3_256; break;
        case 48: type2 = MBEDTLS_SHA3_384; break;
        case 64: type2 = MBEDTLS_SHA3_512; break;
        default: TEST_FAIL("hash2->len validity"); break;
    }

    /* Round 1 */
    TEST_ASSERT(mbedtls_sha3_starts(&ctx, type1) == 0);
    TEST_ASSERT(mbedtls_sha3_update(&ctx, input1->x, input1->len) == 0);
    TEST_ASSERT(mbedtls_sha3_finish(&ctx, output, sizeof(output)) == 0);
    TEST_MEMORY_COMPARE(output, hash1->len, hash1->x, hash1->len);

    /* Round 2 */
    TEST_ASSERT(mbedtls_sha3_starts(&ctx, type2) == 0);
    TEST_ASSERT(mbedtls_sha3_update(&ctx, input2->x, input2->len) == 0);
    TEST_ASSERT(mbedtls_sha3_finish(&ctx, output, sizeof(output)) == 0);
    TEST_MEMORY_COMPARE(output, hash2->len, hash2->x, hash2->len);

exit:
    mbedtls_sha3_free(&ctx);
}
/* END_CASE */

/* BEGIN_CASE depends_on:MBEDTLS_SHA3_C:MBEDTLS_SELF_TEST */
void sha3_selftest()
{
    TEST_ASSERT(mbedtls_sha3_self_test(0) == 0);
}
/* END_CASE */