• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/* BEGIN_HEADER */
2#include "mbedtls/md.h"
3/* END_HEADER */
4
5/* BEGIN_DEPENDENCIES
6 * depends_on:MBEDTLS_MD_C
7 * END_DEPENDENCIES
8 */
9
10/* BEGIN_CASE */
11void mbedtls_md_process()
12{
13    const int *md_type_ptr;
14    const mbedtls_md_info_t *info;
15    mbedtls_md_context_t ctx;
16    unsigned char buf[150];
17
18    mbedtls_md_init(&ctx);
19    memset(buf, 0, sizeof(buf));
20
21    /*
22     * Very minimal testing of mbedtls_md_process, just make sure the various
23     * xxx_process_wrap() function pointers are valid. (Testing that they
24     * indeed do the right thing would require messing with the internal
25     * state of the underlying mbedtls_md/sha context.)
26     *
27     * Also tests that mbedtls_md_list() only returns valid MDs.
28     */
29    for (md_type_ptr = mbedtls_md_list(); *md_type_ptr != 0; md_type_ptr++) {
30        info = mbedtls_md_info_from_type(*md_type_ptr);
31        TEST_ASSERT(info != NULL);
32        TEST_EQUAL(0, mbedtls_md_setup(&ctx, info, 0));
33        TEST_EQUAL(0, mbedtls_md_starts(&ctx));
34        TEST_EQUAL(0, mbedtls_md_process(&ctx, buf));
35        mbedtls_md_free(&ctx);
36    }
37
38exit:
39    mbedtls_md_free(&ctx);
40}
41/* END_CASE */
42
43/* BEGIN_CASE */
44void md_null_args()
45{
46    mbedtls_md_context_t ctx;
47    const mbedtls_md_info_t *info = mbedtls_md_info_from_type(*(mbedtls_md_list()));
48    unsigned char buf[1] = { 0 };
49
50    mbedtls_md_init(&ctx);
51
52    TEST_EQUAL(0, mbedtls_md_get_size(NULL));
53    TEST_EQUAL(mbedtls_md_get_type(NULL), MBEDTLS_MD_NONE);
54    TEST_ASSERT(mbedtls_md_get_name(NULL) == NULL);
55
56    TEST_ASSERT(mbedtls_md_info_from_string(NULL) == NULL);
57
58    TEST_EQUAL(mbedtls_md_setup(&ctx, NULL, 0), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
59    TEST_EQUAL(mbedtls_md_setup(NULL, info, 0), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
60
61    TEST_EQUAL(mbedtls_md_starts(NULL), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
62    TEST_EQUAL(mbedtls_md_starts(&ctx), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
63
64    TEST_EQUAL(mbedtls_md_update(NULL, buf, 1), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
65    TEST_EQUAL(mbedtls_md_update(&ctx, buf, 1), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
66
67    TEST_EQUAL(mbedtls_md_finish(NULL, buf), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
68    TEST_EQUAL(mbedtls_md_finish(&ctx, buf), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
69
70    TEST_EQUAL(mbedtls_md(NULL, buf, 1, buf), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
71
72#if defined(MBEDTLS_FS_IO)
73    TEST_EQUAL(mbedtls_md_file(NULL, "", buf), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
74#endif
75
76    TEST_EQUAL(mbedtls_md_hmac_starts(NULL, buf, 1),
77               MBEDTLS_ERR_MD_BAD_INPUT_DATA);
78    TEST_EQUAL(mbedtls_md_hmac_starts(&ctx, buf, 1),
79               MBEDTLS_ERR_MD_BAD_INPUT_DATA);
80
81    TEST_EQUAL(mbedtls_md_hmac_update(NULL, buf, 1),
82               MBEDTLS_ERR_MD_BAD_INPUT_DATA);
83    TEST_EQUAL(mbedtls_md_hmac_update(&ctx, buf, 1),
84               MBEDTLS_ERR_MD_BAD_INPUT_DATA);
85
86    TEST_EQUAL(mbedtls_md_hmac_finish(NULL, buf), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
87    TEST_EQUAL(mbedtls_md_hmac_finish(&ctx, buf), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
88
89    TEST_EQUAL(mbedtls_md_hmac_reset(NULL), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
90    TEST_EQUAL(mbedtls_md_hmac_reset(&ctx), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
91
92    TEST_EQUAL(mbedtls_md_hmac(NULL, buf, 1, buf, 1, buf),
93               MBEDTLS_ERR_MD_BAD_INPUT_DATA);
94
95    TEST_EQUAL(mbedtls_md_process(NULL, buf), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
96    TEST_EQUAL(mbedtls_md_process(&ctx, buf), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
97
98    /* Ok, this is not NULL arg but NULL return... */
99    TEST_ASSERT(mbedtls_md_info_from_type(MBEDTLS_MD_NONE) == NULL);
100    TEST_ASSERT(mbedtls_md_info_from_string("no such md") == NULL);
101}
102/* END_CASE */
103
104/* BEGIN_CASE */
105void md_info(int md_type, char *md_name, int md_size)
106{
107    const mbedtls_md_info_t *md_info;
108    const int *md_type_ptr;
109    int found;
110
111    md_info = mbedtls_md_info_from_type(md_type);
112    TEST_ASSERT(md_info != NULL);
113    TEST_ASSERT(md_info == mbedtls_md_info_from_string(md_name));
114
115    TEST_EQUAL(mbedtls_md_get_type(md_info), (mbedtls_md_type_t) md_type);
116    TEST_EQUAL(mbedtls_md_get_size(md_info), (unsigned char) md_size);
117    TEST_EQUAL(0, strcmp(mbedtls_md_get_name(md_info), md_name));
118
119    found = 0;
120    for (md_type_ptr = mbedtls_md_list(); *md_type_ptr != 0; md_type_ptr++) {
121        if (*md_type_ptr == md_type) {
122            found = 1;
123        }
124    }
125    TEST_EQUAL(found, 1);
126}
127/* END_CASE */
128
129/* BEGIN_CASE */
130void md_text(int md_type, char *text_src_string, data_t *hash)
131{
132    unsigned char *src = (unsigned char *) text_src_string;
133    size_t src_len = strlen(text_src_string);
134    unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 };
135    const mbedtls_md_info_t *md_info = NULL;
136
137    md_info = mbedtls_md_info_from_type(md_type);
138    TEST_ASSERT(md_info != NULL);
139
140    TEST_EQUAL(0, mbedtls_md(md_info, src, src_len, output));
141
142    TEST_MEMORY_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len);
143}
144/* END_CASE */
145
146/* BEGIN_CASE */
147void md_hex(int md_type, data_t *src_str, data_t *hash)
148{
149    unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 };
150    const mbedtls_md_info_t *md_info = NULL;
151
152    md_info = mbedtls_md_info_from_type(md_type);
153    TEST_ASSERT(md_info != NULL);
154
155    TEST_EQUAL(0, mbedtls_md(md_info, src_str->x, src_str->len, output));
156
157
158    TEST_MEMORY_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len);
159}
160/* END_CASE */
161
162/* BEGIN_CASE */
163void md_text_multi(int md_type, char *text_src_string,
164                   data_t *hash)
165{
166    unsigned char *src = (unsigned char *) text_src_string;
167    size_t src_len = strlen(text_src_string);
168    unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 };
169    size_t halfway;
170
171    const mbedtls_md_info_t *md_info = NULL;
172    mbedtls_md_context_t ctx, ctx_copy;
173
174    mbedtls_md_init(&ctx);
175    mbedtls_md_init(&ctx_copy);
176
177    halfway = src_len / 2;
178
179    md_info = mbedtls_md_info_from_type(md_type);
180    TEST_ASSERT(md_info != NULL);
181    TEST_EQUAL(0, mbedtls_md_setup(&ctx, md_info, 0));
182    TEST_EQUAL(0, mbedtls_md_setup(&ctx_copy, md_info, 0));
183
184    TEST_EQUAL(0, mbedtls_md_starts(&ctx));
185    TEST_ASSERT(ctx.md_ctx != NULL);
186    TEST_EQUAL(0, mbedtls_md_update(&ctx, src, halfway));
187    TEST_EQUAL(0, mbedtls_md_clone(&ctx_copy, &ctx));
188
189    TEST_EQUAL(0, mbedtls_md_update(&ctx, src + halfway, src_len - halfway));
190    TEST_EQUAL(0, mbedtls_md_finish(&ctx, output));
191    TEST_MEMORY_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len);
192
193    /* Test clone */
194    memset(output, 0x00, sizeof(output));
195
196    TEST_EQUAL(0, mbedtls_md_update(&ctx_copy, src + halfway, src_len - halfway));
197    TEST_EQUAL(0, mbedtls_md_finish(&ctx_copy, output));
198    TEST_MEMORY_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len);
199
200exit:
201    mbedtls_md_free(&ctx);
202    mbedtls_md_free(&ctx_copy);
203}
204/* END_CASE */
205
206/* BEGIN_CASE */
207void md_hex_multi(int md_type, data_t *src_str, data_t *hash)
208{
209    unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 };
210    const mbedtls_md_info_t *md_info = NULL;
211    mbedtls_md_context_t ctx, ctx_copy;
212    int halfway;
213
214    mbedtls_md_init(&ctx);
215    mbedtls_md_init(&ctx_copy);
216
217    md_info = mbedtls_md_info_from_type(md_type);
218    TEST_ASSERT(md_info != NULL);
219    TEST_EQUAL(0, mbedtls_md_setup(&ctx, md_info, 0));
220    TEST_EQUAL(0, mbedtls_md_setup(&ctx_copy, md_info, 0));
221
222    halfway = src_str->len / 2;
223
224    TEST_EQUAL(0, mbedtls_md_starts(&ctx));
225    TEST_ASSERT(ctx.md_ctx != NULL);
226    TEST_EQUAL(0, mbedtls_md_update(&ctx, src_str->x, halfway));
227    TEST_EQUAL(0, mbedtls_md_clone(&ctx_copy, &ctx));
228
229    TEST_EQUAL(0, mbedtls_md_update(&ctx, src_str->x + halfway, src_str->len - halfway));
230    TEST_EQUAL(0, mbedtls_md_finish(&ctx, output));
231    TEST_MEMORY_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len);
232
233    /* Test clone */
234    memset(output, 0x00, sizeof(output));
235
236    TEST_EQUAL(0, mbedtls_md_update(&ctx_copy, src_str->x + halfway, src_str->len - halfway));
237    TEST_EQUAL(0, mbedtls_md_finish(&ctx_copy, output));
238    TEST_MEMORY_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len);
239
240exit:
241    mbedtls_md_free(&ctx);
242    mbedtls_md_free(&ctx_copy);
243}
244/* END_CASE */
245
246/* BEGIN_CASE */
247void mbedtls_md_hmac(int md_type, int trunc_size,
248                     data_t *key_str, data_t *src_str,
249                     data_t *hash)
250{
251    unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 };
252    const mbedtls_md_info_t *md_info = NULL;
253
254    md_info = mbedtls_md_info_from_type(md_type);
255    TEST_ASSERT(md_info != NULL);
256
257
258    TEST_EQUAL(0, mbedtls_md_hmac(md_info, key_str->x, key_str->len,
259                                  src_str->x, src_str->len, output));
260
261    TEST_MEMORY_COMPARE(output, trunc_size, hash->x, hash->len);
262}
263/* END_CASE */
264
265/* BEGIN_CASE */
266void md_hmac_multi(int md_type, int trunc_size, data_t *key_str,
267                   data_t *src_str, data_t *hash)
268{
269    unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 };
270    const mbedtls_md_info_t *md_info = NULL;
271    mbedtls_md_context_t ctx;
272    int halfway;
273
274    mbedtls_md_init(&ctx);
275
276    md_info = mbedtls_md_info_from_type(md_type);
277    TEST_ASSERT(md_info != NULL);
278    TEST_EQUAL(0, mbedtls_md_setup(&ctx, md_info, 1));
279
280    halfway = src_str->len / 2;
281
282    TEST_EQUAL(0, mbedtls_md_hmac_starts(&ctx, key_str->x, key_str->len));
283    TEST_ASSERT(ctx.md_ctx != NULL);
284    TEST_EQUAL(0, mbedtls_md_hmac_update(&ctx, src_str->x, halfway));
285    TEST_EQUAL(0, mbedtls_md_hmac_update(&ctx, src_str->x + halfway, src_str->len - halfway));
286    TEST_EQUAL(0, mbedtls_md_hmac_finish(&ctx, output));
287
288    TEST_MEMORY_COMPARE(output, trunc_size, hash->x, hash->len);
289
290    /* Test again, for reset() */
291    memset(output, 0x00, sizeof(output));
292
293    TEST_EQUAL(0, mbedtls_md_hmac_reset(&ctx));
294    TEST_EQUAL(0, mbedtls_md_hmac_update(&ctx, src_str->x, halfway));
295    TEST_EQUAL(0, mbedtls_md_hmac_update(&ctx, src_str->x + halfway, src_str->len - halfway));
296    TEST_EQUAL(0, mbedtls_md_hmac_finish(&ctx, output));
297
298    TEST_MEMORY_COMPARE(output, trunc_size, hash->x, hash->len);
299
300exit:
301    mbedtls_md_free(&ctx);
302}
303/* END_CASE */
304
305/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
306void mbedtls_md_file(int md_type, char *filename,
307                     data_t *hash)
308{
309    unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 };
310    const mbedtls_md_info_t *md_info = NULL;
311
312    md_info = mbedtls_md_info_from_type(md_type);
313    TEST_ASSERT(md_info != NULL);
314
315    TEST_EQUAL(0, mbedtls_md_file(md_info, filename, output));
316
317    TEST_MEMORY_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len);
318}
319/* END_CASE */
320