blob: 65aad13fcc5ec35badfd0895a4819e2426958e61 [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);
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +010032 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));
Gilles Peskine449bd832023-01-11 14:50:10 +010035 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
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +010052 TEST_EQUAL(0, mbedtls_md_get_size(NULL));
Gilles Peskine449bd832023-01-11 14:50:10 +010053 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
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +010060 TEST_EQUAL(mbedtls_md_setup(&ctx, NULL, 0), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
61 TEST_EQUAL(mbedtls_md_setup(NULL, info, 0), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020062
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +010063 TEST_EQUAL(mbedtls_md_starts(NULL), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
64 TEST_EQUAL(mbedtls_md_starts(&ctx), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020065
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +010066 TEST_EQUAL(mbedtls_md_update(NULL, buf, 1), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
67 TEST_EQUAL(mbedtls_md_update(&ctx, buf, 1), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020068
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +010069 TEST_EQUAL(mbedtls_md_finish(NULL, buf), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
70 TEST_EQUAL(mbedtls_md_finish(&ctx, buf), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020071
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +010072 TEST_EQUAL(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)
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +010075 TEST_EQUAL(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
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +010078 TEST_EQUAL(mbedtls_md_hmac_starts(NULL, buf, 1),
79 MBEDTLS_ERR_MD_BAD_INPUT_DATA);
80 TEST_EQUAL(mbedtls_md_hmac_starts(&ctx, buf, 1),
81 MBEDTLS_ERR_MD_BAD_INPUT_DATA);
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020082
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +010083 TEST_EQUAL(mbedtls_md_hmac_update(NULL, buf, 1),
84 MBEDTLS_ERR_MD_BAD_INPUT_DATA);
85 TEST_EQUAL(mbedtls_md_hmac_update(&ctx, buf, 1),
86 MBEDTLS_ERR_MD_BAD_INPUT_DATA);
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020087
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +010088 TEST_EQUAL(mbedtls_md_hmac_finish(NULL, buf), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
89 TEST_EQUAL(mbedtls_md_hmac_finish(&ctx, buf), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020090
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +010091 TEST_EQUAL(mbedtls_md_hmac_reset(NULL), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
92 TEST_EQUAL(mbedtls_md_hmac_reset(&ctx), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020093
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +010094 TEST_EQUAL(mbedtls_md_hmac(NULL, buf, 1, buf, 1, buf),
95 MBEDTLS_ERR_MD_BAD_INPUT_DATA);
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020096
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +010097 TEST_EQUAL(mbedtls_md_process(NULL, buf), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
98 TEST_EQUAL(mbedtls_md_process(&ctx, buf), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard19d644b2015-03-26 12:42:35 +010099
100 /* Ok, this is not NULL arg but NULL return... */
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 TEST_ASSERT(mbedtls_md_info_from_type(MBEDTLS_MD_NONE) == NULL);
102 TEST_ASSERT(mbedtls_md_info_from_string("no such md") == NULL);
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +0200103}
104/* END_CASE */
105
106/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100107void md_info(int md_type, char *md_name, int md_size)
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +0100108{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +0100110 const int *md_type_ptr;
111 int found;
112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 md_info = mbedtls_md_info_from_type(md_type);
114 TEST_ASSERT(md_info != NULL);
115 TEST_ASSERT(md_info == mbedtls_md_info_from_string(md_name));
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +0100116
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +0100117 TEST_EQUAL(mbedtls_md_get_type(md_info), (mbedtls_md_type_t) md_type);
118 TEST_EQUAL(mbedtls_md_get_size(md_info), (unsigned char) md_size);
119 TEST_EQUAL(0, strcmp(mbedtls_md_get_name(md_info), md_name));
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +0100120
121 found = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 for (md_type_ptr = mbedtls_md_list(); *md_type_ptr != 0; md_type_ptr++) {
123 if (*md_type_ptr == md_type) {
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +0100124 found = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 }
126 }
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +0100127 TEST_EQUAL(found, 1);
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +0100128}
129/* END_CASE */
130
131/* BEGIN_CASE */
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100132void md_text(int md_type, char *text_src_string, data_t *hash)
Paul Bakker17373852011-01-06 14:20:01 +0000133{
Manuel Pégourié-Gonnardb707bed2023-02-03 12:32:41 +0100134 unsigned char *src = (unsigned char *) text_src_string;
135 size_t src_len = strlen(text_src_string);
Manuel Pégourié-Gonnard4ba98f52023-02-03 12:25:53 +0100136 unsigned char output[MBEDTLS_MD_MAX_SIZE] = {0};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker17373852011-01-06 14:20:01 +0000138
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100139 md_info = mbedtls_md_info_from_type(md_type);
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 TEST_ASSERT(md_info != NULL);
Paul Bakker17373852011-01-06 14:20:01 +0000141
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +0100142 TEST_EQUAL(0, mbedtls_md(md_info, src, src_len, output));
Paul Bakker17373852011-01-06 14:20:01 +0000143
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
145 mbedtls_md_get_size(md_info),
146 hash->len) == 0);
Paul Bakker17373852011-01-06 14:20:01 +0000147}
Paul Bakker33b43f12013-08-20 11:48:36 +0200148/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000149
Paul Bakker33b43f12013-08-20 11:48:36 +0200150/* BEGIN_CASE */
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100151void md_hex(int md_type, data_t *src_str, data_t *hash)
Paul Bakker17373852011-01-06 14:20:01 +0000152{
Manuel Pégourié-Gonnard4ba98f52023-02-03 12:25:53 +0100153 unsigned char output[MBEDTLS_MD_MAX_SIZE] = {0};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker17373852011-01-06 14:20:01 +0000155
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100156 md_info = mbedtls_md_info_from_type(md_type);
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 TEST_ASSERT(md_info != NULL);
Paul Bakker17373852011-01-06 14:20:01 +0000158
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +0100159 TEST_EQUAL(0, mbedtls_md(md_info, src_str->x, src_str->len, output));
Paul Bakker17373852011-01-06 14:20:01 +0000160
Paul Bakker17373852011-01-06 14:20:01 +0000161
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
163 mbedtls_md_get_size(md_info),
164 hash->len) == 0);
Paul Bakker17373852011-01-06 14:20:01 +0000165}
Paul Bakker33b43f12013-08-20 11:48:36 +0200166/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000167
Paul Bakker33b43f12013-08-20 11:48:36 +0200168/* BEGIN_CASE */
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100169void md_text_multi(int md_type, char *text_src_string,
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 data_t *hash)
Paul Bakker17373852011-01-06 14:20:01 +0000171{
Manuel Pégourié-Gonnardb707bed2023-02-03 12:32:41 +0100172 unsigned char *src = (unsigned char *) text_src_string;
173 size_t src_len = strlen(text_src_string);
Manuel Pégourié-Gonnard4ba98f52023-02-03 12:25:53 +0100174 unsigned char output[MBEDTLS_MD_MAX_SIZE] = {0};
Manuel Pégourié-Gonnardb707bed2023-02-03 12:32:41 +0100175 size_t halfway;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker97c53c22016-07-13 17:20:22 +0100178 mbedtls_md_context_t ctx, ctx_copy;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 mbedtls_md_init(&ctx);
181 mbedtls_md_init(&ctx_copy);
Paul Bakker17373852011-01-06 14:20:01 +0000182
Manuel Pégourié-Gonnardb707bed2023-02-03 12:32:41 +0100183 halfway = src_len / 2;
Paul Bakkere35afa22016-07-13 17:09:14 +0100184
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100185 md_info = mbedtls_md_info_from_type(md_type);
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 TEST_ASSERT(md_info != NULL);
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +0100187 TEST_EQUAL(0, mbedtls_md_setup(&ctx, md_info, 0));
188 TEST_EQUAL(0, mbedtls_md_setup(&ctx_copy, md_info, 0));
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx) == md_info);
190 TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx_copy) == md_info);
Paul Bakker17373852011-01-06 14:20:01 +0000191
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +0100192 TEST_EQUAL(0, mbedtls_md_starts(&ctx));
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 TEST_ASSERT(ctx.md_ctx != NULL);
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +0100194 TEST_EQUAL(0, mbedtls_md_update(&ctx, src, halfway));
195 TEST_EQUAL(0, mbedtls_md_clone(&ctx_copy, &ctx));
Paul Bakker97c53c22016-07-13 17:20:22 +0100196
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +0100197 TEST_EQUAL(0, mbedtls_md_update(&ctx, src + halfway, src_len - halfway));
198 TEST_EQUAL(0, mbedtls_md_finish(&ctx, output));
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
200 mbedtls_md_get_size(md_info),
201 hash->len) == 0);
Paul Bakker17373852011-01-06 14:20:01 +0000202
Paul Bakker97c53c22016-07-13 17:20:22 +0100203 /* Test clone */
Manuel Pégourié-Gonnard4ba98f52023-02-03 12:25:53 +0100204 memset(output, 0x00, sizeof(output));
Paul Bakker97c53c22016-07-13 17:20:22 +0100205
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +0100206 TEST_EQUAL(0, mbedtls_md_update(&ctx_copy, src + halfway, src_len - halfway));
207 TEST_EQUAL(0, mbedtls_md_finish(&ctx_copy, output));
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
209 mbedtls_md_get_size(md_info),
210 hash->len) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200211
212exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 mbedtls_md_free(&ctx);
214 mbedtls_md_free(&ctx_copy);
Paul Bakker17373852011-01-06 14:20:01 +0000215}
Paul Bakker33b43f12013-08-20 11:48:36 +0200216/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000217
Paul Bakker33b43f12013-08-20 11:48:36 +0200218/* BEGIN_CASE */
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100219void md_hex_multi(int md_type, data_t *src_str, data_t *hash)
Paul Bakker17373852011-01-06 14:20:01 +0000220{
Manuel Pégourié-Gonnard4ba98f52023-02-03 12:25:53 +0100221 unsigned char output[MBEDTLS_MD_MAX_SIZE] = {0};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker97c53c22016-07-13 17:20:22 +0100223 mbedtls_md_context_t ctx, ctx_copy;
Azim Khanf1aaec92017-05-30 14:23:15 +0100224 int halfway;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200225
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 mbedtls_md_init(&ctx);
227 mbedtls_md_init(&ctx_copy);
Paul Bakker17373852011-01-06 14:20:01 +0000228
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100229 md_info = mbedtls_md_info_from_type(md_type);
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 TEST_ASSERT(md_info != NULL);
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +0100231 TEST_EQUAL(0, mbedtls_md_setup(&ctx, md_info, 0));
232 TEST_EQUAL(0, mbedtls_md_setup(&ctx_copy, md_info, 0));
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx) == md_info);
234 TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx_copy) == md_info);
Paul Bakker17373852011-01-06 14:20:01 +0000235
Azim Khand30ca132017-06-09 04:32:58 +0100236 halfway = src_str->len / 2;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200237
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +0100238 TEST_EQUAL(0, mbedtls_md_starts(&ctx));
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 TEST_ASSERT(ctx.md_ctx != NULL);
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +0100240 TEST_EQUAL(0, mbedtls_md_update(&ctx, src_str->x, halfway));
241 TEST_EQUAL(0, mbedtls_md_clone(&ctx_copy, &ctx));
Paul Bakker97c53c22016-07-13 17:20:22 +0100242
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +0100243 TEST_EQUAL(0, mbedtls_md_update(&ctx, src_str->x + halfway, src_str->len - halfway));
244 TEST_EQUAL(0, mbedtls_md_finish(&ctx, output));
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
246 mbedtls_md_get_size(md_info),
247 hash->len) == 0);
Paul Bakker17373852011-01-06 14:20:01 +0000248
Paul Bakker97c53c22016-07-13 17:20:22 +0100249 /* Test clone */
Manuel Pégourié-Gonnard4ba98f52023-02-03 12:25:53 +0100250 memset(output, 0x00, sizeof(output));
Paul Bakker97c53c22016-07-13 17:20:22 +0100251
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +0100252 TEST_EQUAL(0, mbedtls_md_update(&ctx_copy, src_str->x + halfway, src_str->len - halfway));
253 TEST_EQUAL(0, mbedtls_md_finish(&ctx_copy, output));
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
255 mbedtls_md_get_size(md_info),
256 hash->len) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200257
258exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 mbedtls_md_free(&ctx);
260 mbedtls_md_free(&ctx_copy);
Paul Bakker17373852011-01-06 14:20:01 +0000261}
Paul Bakker33b43f12013-08-20 11:48:36 +0200262/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000263
Paul Bakker33b43f12013-08-20 11:48:36 +0200264/* BEGIN_CASE */
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100265void mbedtls_md_hmac(int md_type, int trunc_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 data_t *key_str, data_t *src_str,
267 data_t *hash)
Paul Bakker17373852011-01-06 14:20:01 +0000268{
Manuel Pégourié-Gonnard4ba98f52023-02-03 12:25:53 +0100269 unsigned char output[MBEDTLS_MD_MAX_SIZE] = {0};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker17373852011-01-06 14:20:01 +0000271
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100272 md_info = mbedtls_md_info_from_type(md_type);
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 TEST_ASSERT(md_info != NULL);
Paul Bakker17373852011-01-06 14:20:01 +0000274
Paul Bakker17373852011-01-06 14:20:01 +0000275
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +0100276 TEST_EQUAL(0, mbedtls_md_hmac(md_info, key_str->x, key_str->len,
277 src_str->x, src_str->len, output));
Paul Bakker17373852011-01-06 14:20:01 +0000278
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
280 trunc_size, hash->len) == 0);
Paul Bakker17373852011-01-06 14:20:01 +0000281}
Paul Bakker33b43f12013-08-20 11:48:36 +0200282/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000283
Paul Bakker33b43f12013-08-20 11:48:36 +0200284/* BEGIN_CASE */
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100285void md_hmac_multi(int md_type, int trunc_size, data_t *key_str,
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 data_t *src_str, data_t *hash)
Paul Bakker17373852011-01-06 14:20:01 +0000287{
Manuel Pégourié-Gonnard4ba98f52023-02-03 12:25:53 +0100288 unsigned char output[MBEDTLS_MD_MAX_SIZE] = {0};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 const mbedtls_md_info_t *md_info = NULL;
290 mbedtls_md_context_t ctx;
Azim Khanf1aaec92017-05-30 14:23:15 +0100291 int halfway;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200292
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 mbedtls_md_init(&ctx);
Paul Bakker17373852011-01-06 14:20:01 +0000294
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100295 md_info = mbedtls_md_info_from_type(md_type);
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 TEST_ASSERT(md_info != NULL);
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +0100297 TEST_EQUAL(0, mbedtls_md_setup(&ctx, md_info, 1));
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx) == md_info);
Paul Bakker17373852011-01-06 14:20:01 +0000299
Azim Khand30ca132017-06-09 04:32:58 +0100300 halfway = src_str->len / 2;
Paul Bakker17373852011-01-06 14:20:01 +0000301
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +0100302 TEST_EQUAL(0, mbedtls_md_hmac_starts(&ctx, key_str->x, key_str->len));
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 TEST_ASSERT(ctx.md_ctx != NULL);
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +0100304 TEST_EQUAL(0, mbedtls_md_hmac_update(&ctx, src_str->x, halfway));
305 TEST_EQUAL(0, mbedtls_md_hmac_update(&ctx, src_str->x + halfway, src_str->len - halfway));
306 TEST_EQUAL(0, mbedtls_md_hmac_finish(&ctx, output));
Manuel Pégourié-Gonnard59ba4e92014-03-29 14:43:44 +0100307
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
309 trunc_size, hash->len) == 0);
Manuel Pégourié-Gonnard59ba4e92014-03-29 14:43:44 +0100310
311 /* Test again, for reset() */
Manuel Pégourié-Gonnard4ba98f52023-02-03 12:25:53 +0100312 memset(output, 0x00, sizeof(output));
Manuel Pégourié-Gonnard59ba4e92014-03-29 14:43:44 +0100313
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +0100314 TEST_EQUAL(0, mbedtls_md_hmac_reset(&ctx));
315 TEST_EQUAL(0, mbedtls_md_hmac_update(&ctx, src_str->x, halfway));
316 TEST_EQUAL(0, mbedtls_md_hmac_update(&ctx, src_str->x + halfway, src_str->len - halfway));
317 TEST_EQUAL(0, mbedtls_md_hmac_finish(&ctx, output));
Paul Bakker33b43f12013-08-20 11:48:36 +0200318
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
320 trunc_size, hash->len) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200321
322exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 mbedtls_md_free(&ctx);
Paul Bakker17373852011-01-06 14:20:01 +0000324}
Paul Bakker33b43f12013-08-20 11:48:36 +0200325/* END_CASE */
Paul Bakker428b9ba2013-09-15 15:20:37 +0200326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100328void mbedtls_md_file(int md_type, char *filename,
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 data_t *hash)
Paul Bakker17373852011-01-06 14:20:01 +0000330{
Manuel Pégourié-Gonnard4ba98f52023-02-03 12:25:53 +0100331 unsigned char output[MBEDTLS_MD_MAX_SIZE] = {0};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker17373852011-01-06 14:20:01 +0000333
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100334 md_info = mbedtls_md_info_from_type(md_type);
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 TEST_ASSERT(md_info != NULL);
Paul Bakker17373852011-01-06 14:20:01 +0000336
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +0100337 TEST_EQUAL(0, mbedtls_md_file(md_info, filename, output));
Paul Bakker17373852011-01-06 14:20:01 +0000338
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
340 mbedtls_md_get_size(md_info),
341 hash->len) == 0);
Paul Bakker17373852011-01-06 14:20:01 +0000342}
Paul Bakker33b43f12013-08-20 11:48:36 +0200343/* END_CASE */