blob: 6b4d09a6f5c9914ba78ebf4653e969ba8883a85c [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/md.h"
Paul Bakker33b43f12013-08-20 11:48:36 +02003/* END_HEADER */
Paul Bakker17373852011-01-06 14:20:01 +00004
Paul Bakker33b43f12013-08-20 11:48:36 +02005/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006 * depends_on:MBEDTLS_MD_C
Paul Bakker33b43f12013-08-20 11:48:36 +02007 * END_DEPENDENCIES
8 */
Paul Bakker5690efc2011-05-26 13:16:06 +00009
Paul Bakker33b43f12013-08-20 11:48:36 +020010/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010011void mbedtls_md_process()
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +010012{
13 const int *md_type_ptr;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014 const mbedtls_md_info_t *info;
15 mbedtls_md_context_t ctx;
Manuel Pégourié-Gonnardedb242f2014-04-02 17:52:04 +020016 unsigned char buf[150];
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +010017
Gilles Peskine449bd832023-01-11 14:50:10 +010018 mbedtls_md_init(&ctx);
19 memset(buf, 0, sizeof(buf));
Manuel Pégourié-Gonnardedb242f2014-04-02 17:52:04 +020020
21 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022 * Very minimal testing of mbedtls_md_process, just make sure the various
Manuel Pégourié-Gonnardedb242f2014-04-02 17:52:04 +020023 * xxx_process_wrap() function pointers are valid. (Testing that they
Shaun Case8b0ecbc2021-12-20 21:14:10 -080024 * indeed do the right thing would require messing with the internal
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025 * state of the underlying mbedtls_md/sha context.)
Manuel Pégourié-Gonnardedb242f2014-04-02 17:52:04 +020026 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027 * Also tests that mbedtls_md_list() only returns valid MDs.
Manuel Pégourié-Gonnardedb242f2014-04-02 17:52:04 +020028 */
Gilles Peskine449bd832023-01-11 14:50:10 +010029 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_ASSERT(mbedtls_md_setup(&ctx, info, 0) == 0);
33 TEST_ASSERT(mbedtls_md_starts(&ctx) == 0);
34 TEST_ASSERT(mbedtls_md_process(&ctx, buf) == 0);
35 mbedtls_md_free(&ctx);
Manuel Pégourié-Gonnardedb242f2014-04-02 17:52:04 +020036 }
Paul Bakkerbd51b262014-07-10 15:26:12 +020037
38exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010039 mbedtls_md_free(&ctx);
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +010040}
41/* END_CASE */
42
43/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010044void md_null_args()
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020045{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046 mbedtls_md_context_t ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +010047 const mbedtls_md_info_t *info = mbedtls_md_info_from_type(*(mbedtls_md_list()));
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020048 unsigned char buf[1] = { 0 };
49
Gilles Peskine449bd832023-01-11 14:50:10 +010050 mbedtls_md_init(&ctx);
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020051
Gilles Peskine449bd832023-01-11 14:50:10 +010052 TEST_ASSERT(mbedtls_md_get_size(NULL) == 0);
53 TEST_ASSERT(mbedtls_md_get_type(NULL) == MBEDTLS_MD_NONE);
54 TEST_ASSERT(mbedtls_md_get_name(NULL) == NULL);
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020055
Gilles Peskine449bd832023-01-11 14:50:10 +010056 TEST_ASSERT(mbedtls_md_info_from_string(NULL) == NULL);
57 TEST_ASSERT(mbedtls_md_info_from_ctx(NULL) == NULL);
58 TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx) == NULL);
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020059
Gilles Peskine449bd832023-01-11 14:50:10 +010060 TEST_ASSERT(mbedtls_md_setup(&ctx, NULL, 0) == MBEDTLS_ERR_MD_BAD_INPUT_DATA);
61 TEST_ASSERT(mbedtls_md_setup(NULL, info, 0) == MBEDTLS_ERR_MD_BAD_INPUT_DATA);
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020062
Gilles Peskine449bd832023-01-11 14:50:10 +010063 TEST_ASSERT(mbedtls_md_starts(NULL) == MBEDTLS_ERR_MD_BAD_INPUT_DATA);
64 TEST_ASSERT(mbedtls_md_starts(&ctx) == MBEDTLS_ERR_MD_BAD_INPUT_DATA);
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020065
Gilles Peskine449bd832023-01-11 14:50:10 +010066 TEST_ASSERT(mbedtls_md_update(NULL, buf, 1) == MBEDTLS_ERR_MD_BAD_INPUT_DATA);
67 TEST_ASSERT(mbedtls_md_update(&ctx, buf, 1) == MBEDTLS_ERR_MD_BAD_INPUT_DATA);
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020068
Gilles Peskine449bd832023-01-11 14:50:10 +010069 TEST_ASSERT(mbedtls_md_finish(NULL, buf) == MBEDTLS_ERR_MD_BAD_INPUT_DATA);
70 TEST_ASSERT(mbedtls_md_finish(&ctx, buf) == MBEDTLS_ERR_MD_BAD_INPUT_DATA);
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020071
Gilles Peskine449bd832023-01-11 14:50:10 +010072 TEST_ASSERT(mbedtls_md(NULL, buf, 1, buf) == MBEDTLS_ERR_MD_BAD_INPUT_DATA);
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020073
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +020074#if defined(MBEDTLS_FS_IO)
Gilles Peskine449bd832023-01-11 14:50:10 +010075 TEST_ASSERT(mbedtls_md_file(NULL, "", buf) == MBEDTLS_ERR_MD_BAD_INPUT_DATA);
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +020076#endif
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020077
Gilles Peskine449bd832023-01-11 14:50:10 +010078 TEST_ASSERT(mbedtls_md_hmac_starts(NULL, buf, 1)
79 == MBEDTLS_ERR_MD_BAD_INPUT_DATA);
80 TEST_ASSERT(mbedtls_md_hmac_starts(&ctx, buf, 1)
81 == MBEDTLS_ERR_MD_BAD_INPUT_DATA);
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020082
Gilles Peskine449bd832023-01-11 14:50:10 +010083 TEST_ASSERT(mbedtls_md_hmac_update(NULL, buf, 1)
84 == MBEDTLS_ERR_MD_BAD_INPUT_DATA);
85 TEST_ASSERT(mbedtls_md_hmac_update(&ctx, buf, 1)
86 == MBEDTLS_ERR_MD_BAD_INPUT_DATA);
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020087
Gilles Peskine449bd832023-01-11 14:50:10 +010088 TEST_ASSERT(mbedtls_md_hmac_finish(NULL, buf)
89 == MBEDTLS_ERR_MD_BAD_INPUT_DATA);
90 TEST_ASSERT(mbedtls_md_hmac_finish(&ctx, buf)
91 == MBEDTLS_ERR_MD_BAD_INPUT_DATA);
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020092
Gilles Peskine449bd832023-01-11 14:50:10 +010093 TEST_ASSERT(mbedtls_md_hmac_reset(NULL) == MBEDTLS_ERR_MD_BAD_INPUT_DATA);
94 TEST_ASSERT(mbedtls_md_hmac_reset(&ctx) == MBEDTLS_ERR_MD_BAD_INPUT_DATA);
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020095
Gilles Peskine449bd832023-01-11 14:50:10 +010096 TEST_ASSERT(mbedtls_md_hmac(NULL, buf, 1, buf, 1, buf)
97 == MBEDTLS_ERR_MD_BAD_INPUT_DATA);
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020098
Gilles Peskine449bd832023-01-11 14:50:10 +010099 TEST_ASSERT(mbedtls_md_process(NULL, buf) == MBEDTLS_ERR_MD_BAD_INPUT_DATA);
100 TEST_ASSERT(mbedtls_md_process(&ctx, buf) == MBEDTLS_ERR_MD_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard19d644b2015-03-26 12:42:35 +0100101
102 /* Ok, this is not NULL arg but NULL return... */
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 TEST_ASSERT(mbedtls_md_info_from_type(MBEDTLS_MD_NONE) == NULL);
104 TEST_ASSERT(mbedtls_md_info_from_string("no such md") == NULL);
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +0200105}
106/* END_CASE */
107
108/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100109void md_info(int md_type, char *md_name, int md_size)
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +0100110{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +0100112 const int *md_type_ptr;
113 int found;
114
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 md_info = mbedtls_md_info_from_type(md_type);
116 TEST_ASSERT(md_info != NULL);
117 TEST_ASSERT(md_info == mbedtls_md_info_from_string(md_name));
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +0100118
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 TEST_ASSERT(mbedtls_md_get_type(md_info) == (mbedtls_md_type_t) md_type);
120 TEST_ASSERT(mbedtls_md_get_size(md_info) == (unsigned char) md_size);
121 TEST_ASSERT(strcmp(mbedtls_md_get_name(md_info), md_name) == 0);
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +0100122
123 found = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 for (md_type_ptr = mbedtls_md_list(); *md_type_ptr != 0; md_type_ptr++) {
125 if (*md_type_ptr == md_type) {
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +0100126 found = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 }
128 }
129 TEST_ASSERT(found == 1);
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +0100130}
131/* END_CASE */
132
133/* BEGIN_CASE */
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100134void md_text(int md_type, char *text_src_string, data_t *hash)
Paul Bakker17373852011-01-06 14:20:01 +0000135{
Manuel Pégourié-Gonnardb707bed2023-02-03 12:32:41 +0100136 unsigned char *src = (unsigned char *) text_src_string;
137 size_t src_len = strlen(text_src_string);
Manuel Pégourié-Gonnard4ba98f52023-02-03 12:25:53 +0100138 unsigned char output[MBEDTLS_MD_MAX_SIZE] = {0};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker17373852011-01-06 14:20:01 +0000140
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100141 md_info = mbedtls_md_info_from_type(md_type);
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 TEST_ASSERT(md_info != NULL);
Paul Bakker17373852011-01-06 14:20:01 +0000143
Manuel Pégourié-Gonnardb707bed2023-02-03 12:32:41 +0100144 TEST_ASSERT(0 == mbedtls_md(md_info, src, src_len, output));
Paul Bakker17373852011-01-06 14:20:01 +0000145
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
147 mbedtls_md_get_size(md_info),
148 hash->len) == 0);
Paul Bakker17373852011-01-06 14:20:01 +0000149}
Paul Bakker33b43f12013-08-20 11:48:36 +0200150/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000151
Paul Bakker33b43f12013-08-20 11:48:36 +0200152/* BEGIN_CASE */
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100153void md_hex(int md_type, data_t *src_str, data_t *hash)
Paul Bakker17373852011-01-06 14:20:01 +0000154{
Manuel Pégourié-Gonnard4ba98f52023-02-03 12:25:53 +0100155 unsigned char output[MBEDTLS_MD_MAX_SIZE] = {0};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker17373852011-01-06 14:20:01 +0000157
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100158 md_info = mbedtls_md_info_from_type(md_type);
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 TEST_ASSERT(md_info != NULL);
Paul Bakker17373852011-01-06 14:20:01 +0000160
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 TEST_ASSERT(0 == mbedtls_md(md_info, src_str->x, src_str->len, output));
Paul Bakker17373852011-01-06 14:20:01 +0000162
Paul Bakker17373852011-01-06 14:20:01 +0000163
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
165 mbedtls_md_get_size(md_info),
166 hash->len) == 0);
Paul Bakker17373852011-01-06 14:20:01 +0000167}
Paul Bakker33b43f12013-08-20 11:48:36 +0200168/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000169
Paul Bakker33b43f12013-08-20 11:48:36 +0200170/* BEGIN_CASE */
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100171void md_text_multi(int md_type, char *text_src_string,
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 data_t *hash)
Paul Bakker17373852011-01-06 14:20:01 +0000173{
Manuel Pégourié-Gonnardb707bed2023-02-03 12:32:41 +0100174 unsigned char *src = (unsigned char *) text_src_string;
175 size_t src_len = strlen(text_src_string);
Manuel Pégourié-Gonnard4ba98f52023-02-03 12:25:53 +0100176 unsigned char output[MBEDTLS_MD_MAX_SIZE] = {0};
Manuel Pégourié-Gonnardb707bed2023-02-03 12:32:41 +0100177 size_t halfway;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker97c53c22016-07-13 17:20:22 +0100180 mbedtls_md_context_t ctx, ctx_copy;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200181
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 mbedtls_md_init(&ctx);
183 mbedtls_md_init(&ctx_copy);
Paul Bakker17373852011-01-06 14:20:01 +0000184
Manuel Pégourié-Gonnardb707bed2023-02-03 12:32:41 +0100185 halfway = src_len / 2;
Paul Bakkere35afa22016-07-13 17:09:14 +0100186
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100187 md_info = mbedtls_md_info_from_type(md_type);
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 TEST_ASSERT(md_info != NULL);
189 TEST_ASSERT(0 == mbedtls_md_setup(&ctx, md_info, 0));
190 TEST_ASSERT(0 == mbedtls_md_setup(&ctx_copy, md_info, 0));
191 TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx) == md_info);
192 TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx_copy) == md_info);
Paul Bakker17373852011-01-06 14:20:01 +0000193
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 TEST_ASSERT(0 == mbedtls_md_starts(&ctx));
195 TEST_ASSERT(ctx.md_ctx != NULL);
Manuel Pégourié-Gonnardb707bed2023-02-03 12:32:41 +0100196 TEST_ASSERT(0 == mbedtls_md_update(&ctx, src, halfway));
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 TEST_ASSERT(0 == mbedtls_md_clone(&ctx_copy, &ctx));
Paul Bakker97c53c22016-07-13 17:20:22 +0100198
Manuel Pégourié-Gonnardb707bed2023-02-03 12:32:41 +0100199 TEST_ASSERT(0 == mbedtls_md_update(&ctx, src + halfway, src_len - halfway));
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 TEST_ASSERT(0 == mbedtls_md_finish(&ctx, output));
201 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
202 mbedtls_md_get_size(md_info),
203 hash->len) == 0);
Paul Bakker17373852011-01-06 14:20:01 +0000204
Paul Bakker97c53c22016-07-13 17:20:22 +0100205 /* Test clone */
Manuel Pégourié-Gonnard4ba98f52023-02-03 12:25:53 +0100206 memset(output, 0x00, sizeof(output));
Paul Bakker97c53c22016-07-13 17:20:22 +0100207
Manuel Pégourié-Gonnardb707bed2023-02-03 12:32:41 +0100208 TEST_ASSERT(0 == mbedtls_md_update(&ctx_copy, src + halfway, src_len - halfway));
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 TEST_ASSERT(0 == mbedtls_md_finish(&ctx_copy, output));
210 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
211 mbedtls_md_get_size(md_info),
212 hash->len) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200213
214exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 mbedtls_md_free(&ctx);
216 mbedtls_md_free(&ctx_copy);
Paul Bakker17373852011-01-06 14:20:01 +0000217}
Paul Bakker33b43f12013-08-20 11:48:36 +0200218/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000219
Paul Bakker33b43f12013-08-20 11:48:36 +0200220/* BEGIN_CASE */
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100221void md_hex_multi(int md_type, data_t *src_str, data_t *hash)
Paul Bakker17373852011-01-06 14:20:01 +0000222{
Manuel Pégourié-Gonnard4ba98f52023-02-03 12:25:53 +0100223 unsigned char output[MBEDTLS_MD_MAX_SIZE] = {0};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker97c53c22016-07-13 17:20:22 +0100225 mbedtls_md_context_t ctx, ctx_copy;
Azim Khanf1aaec92017-05-30 14:23:15 +0100226 int halfway;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 mbedtls_md_init(&ctx);
229 mbedtls_md_init(&ctx_copy);
Paul Bakker17373852011-01-06 14:20:01 +0000230
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100231 md_info = mbedtls_md_info_from_type(md_type);
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 TEST_ASSERT(md_info != NULL);
233 TEST_ASSERT(0 == mbedtls_md_setup(&ctx, md_info, 0));
234 TEST_ASSERT(0 == mbedtls_md_setup(&ctx_copy, md_info, 0));
235 TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx) == md_info);
236 TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx_copy) == md_info);
Paul Bakker17373852011-01-06 14:20:01 +0000237
Azim Khand30ca132017-06-09 04:32:58 +0100238 halfway = src_str->len / 2;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200239
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 TEST_ASSERT(0 == mbedtls_md_starts(&ctx));
241 TEST_ASSERT(ctx.md_ctx != NULL);
242 TEST_ASSERT(0 == mbedtls_md_update(&ctx, src_str->x, halfway));
243 TEST_ASSERT(0 == mbedtls_md_clone(&ctx_copy, &ctx));
Paul Bakker97c53c22016-07-13 17:20:22 +0100244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 TEST_ASSERT(0 == mbedtls_md_update(&ctx, src_str->x + halfway, src_str->len - halfway));
246 TEST_ASSERT(0 == mbedtls_md_finish(&ctx, output));
247 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
248 mbedtls_md_get_size(md_info),
249 hash->len) == 0);
Paul Bakker17373852011-01-06 14:20:01 +0000250
Paul Bakker97c53c22016-07-13 17:20:22 +0100251 /* Test clone */
Manuel Pégourié-Gonnard4ba98f52023-02-03 12:25:53 +0100252 memset(output, 0x00, sizeof(output));
Paul Bakker97c53c22016-07-13 17:20:22 +0100253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 TEST_ASSERT(0 == mbedtls_md_update(&ctx_copy, src_str->x + halfway, src_str->len - halfway));
255 TEST_ASSERT(0 == mbedtls_md_finish(&ctx_copy, output));
256 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
257 mbedtls_md_get_size(md_info),
258 hash->len) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200259
260exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 mbedtls_md_free(&ctx);
262 mbedtls_md_free(&ctx_copy);
Paul Bakker17373852011-01-06 14:20:01 +0000263}
Paul Bakker33b43f12013-08-20 11:48:36 +0200264/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000265
Paul Bakker33b43f12013-08-20 11:48:36 +0200266/* BEGIN_CASE */
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100267void mbedtls_md_hmac(int md_type, int trunc_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 data_t *key_str, data_t *src_str,
269 data_t *hash)
Paul Bakker17373852011-01-06 14:20:01 +0000270{
Manuel Pégourié-Gonnard4ba98f52023-02-03 12:25:53 +0100271 unsigned char output[MBEDTLS_MD_MAX_SIZE] = {0};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker17373852011-01-06 14:20:01 +0000273
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100274 md_info = mbedtls_md_info_from_type(md_type);
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 TEST_ASSERT(md_info != NULL);
Paul Bakker17373852011-01-06 14:20:01 +0000276
Paul Bakker17373852011-01-06 14:20:01 +0000277
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 TEST_ASSERT(mbedtls_md_hmac(md_info, key_str->x, key_str->len, src_str->x, src_str->len,
279 output) == 0);
Paul Bakker17373852011-01-06 14:20:01 +0000280
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
282 trunc_size, hash->len) == 0);
Paul Bakker17373852011-01-06 14:20:01 +0000283}
Paul Bakker33b43f12013-08-20 11:48:36 +0200284/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000285
Paul Bakker33b43f12013-08-20 11:48:36 +0200286/* BEGIN_CASE */
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100287void md_hmac_multi(int md_type, int trunc_size, data_t *key_str,
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 data_t *src_str, data_t *hash)
Paul Bakker17373852011-01-06 14:20:01 +0000289{
Manuel Pégourié-Gonnard4ba98f52023-02-03 12:25:53 +0100290 unsigned char output[MBEDTLS_MD_MAX_SIZE] = {0};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 const mbedtls_md_info_t *md_info = NULL;
292 mbedtls_md_context_t ctx;
Azim Khanf1aaec92017-05-30 14:23:15 +0100293 int halfway;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200294
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 mbedtls_md_init(&ctx);
Paul Bakker17373852011-01-06 14:20:01 +0000296
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100297 md_info = mbedtls_md_info_from_type(md_type);
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 TEST_ASSERT(md_info != NULL);
299 TEST_ASSERT(0 == mbedtls_md_setup(&ctx, md_info, 1));
300 TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx) == md_info);
Paul Bakker17373852011-01-06 14:20:01 +0000301
Azim Khand30ca132017-06-09 04:32:58 +0100302 halfway = src_str->len / 2;
Paul Bakker17373852011-01-06 14:20:01 +0000303
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 TEST_ASSERT(0 == mbedtls_md_hmac_starts(&ctx, key_str->x, key_str->len));
305 TEST_ASSERT(ctx.md_ctx != NULL);
306 TEST_ASSERT(0 == mbedtls_md_hmac_update(&ctx, src_str->x, halfway));
307 TEST_ASSERT(0 == mbedtls_md_hmac_update(&ctx, src_str->x + halfway, src_str->len - halfway));
308 TEST_ASSERT(0 == mbedtls_md_hmac_finish(&ctx, output));
Manuel Pégourié-Gonnard59ba4e92014-03-29 14:43:44 +0100309
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
311 trunc_size, hash->len) == 0);
Manuel Pégourié-Gonnard59ba4e92014-03-29 14:43:44 +0100312
313 /* Test again, for reset() */
Manuel Pégourié-Gonnard4ba98f52023-02-03 12:25:53 +0100314 memset(output, 0x00, sizeof(output));
Manuel Pégourié-Gonnard59ba4e92014-03-29 14:43:44 +0100315
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 TEST_ASSERT(0 == mbedtls_md_hmac_reset(&ctx));
317 TEST_ASSERT(0 == mbedtls_md_hmac_update(&ctx, src_str->x, halfway));
318 TEST_ASSERT(0 == mbedtls_md_hmac_update(&ctx, src_str->x + halfway, src_str->len - halfway));
319 TEST_ASSERT(0 == mbedtls_md_hmac_finish(&ctx, output));
Paul Bakker33b43f12013-08-20 11:48:36 +0200320
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
322 trunc_size, hash->len) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200323
324exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 mbedtls_md_free(&ctx);
Paul Bakker17373852011-01-06 14:20:01 +0000326}
Paul Bakker33b43f12013-08-20 11:48:36 +0200327/* END_CASE */
Paul Bakker428b9ba2013-09-15 15:20:37 +0200328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100330void mbedtls_md_file(int md_type, char *filename,
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 data_t *hash)
Paul Bakker17373852011-01-06 14:20:01 +0000332{
Manuel Pégourié-Gonnard4ba98f52023-02-03 12:25:53 +0100333 unsigned char output[MBEDTLS_MD_MAX_SIZE] = {0};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker17373852011-01-06 14:20:01 +0000335
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100336 md_info = mbedtls_md_info_from_type(md_type);
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 TEST_ASSERT(md_info != NULL);
Paul Bakker17373852011-01-06 14:20:01 +0000338
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 TEST_ASSERT(mbedtls_md_file(md_info, filename, output) == 0);
Paul Bakker17373852011-01-06 14:20:01 +0000340
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
342 mbedtls_md_get_size(md_info),
343 hash->len) == 0);
Paul Bakker17373852011-01-06 14:20:01 +0000344}
Paul Bakker33b43f12013-08-20 11:48:36 +0200345/* END_CASE */