blob: be1eab06fe0ef79d6c1f7740cd2c34af4f88ccec [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 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100134void md_text(char *text_md_name, char *text_src_string,
135 data_t *hash)
Paul Bakker17373852011-01-06 14:20:01 +0000136{
137 char md_name[100];
138 unsigned char src_str[1000];
Paul Bakker17373852011-01-06 14:20:01 +0000139 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker17373852011-01-06 14:20:01 +0000141
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 memset(md_name, 0x00, 100);
143 memset(src_str, 0x00, 1000);
144 memset(output, 0x00, 100);
Paul Bakker17373852011-01-06 14:20:01 +0000145
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 strncpy((char *) src_str, text_src_string, sizeof(src_str) - 1);
147 strncpy((char *) md_name, text_md_name, sizeof(md_name) - 1);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 md_info = mbedtls_md_info_from_string(md_name);
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 TEST_ASSERT(md_info != NULL);
Paul Bakker17373852011-01-06 14:20:01 +0000150
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 TEST_ASSERT(0 == mbedtls_md(md_info, src_str, strlen((char *) src_str), output));
Paul Bakker17373852011-01-06 14:20:01 +0000152
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
154 mbedtls_md_get_size(md_info),
155 hash->len) == 0);
Paul Bakker17373852011-01-06 14:20:01 +0000156}
Paul Bakker33b43f12013-08-20 11:48:36 +0200157/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000158
Paul Bakker33b43f12013-08-20 11:48:36 +0200159/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100160void md_hex(char *text_md_name, data_t *src_str, data_t *hash)
Paul Bakker17373852011-01-06 14:20:01 +0000161{
162 char md_name[100];
Paul Bakker17373852011-01-06 14:20:01 +0000163 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker17373852011-01-06 14:20:01 +0000165
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 memset(md_name, 0x00, 100);
167 memset(output, 0x00, 100);
Paul Bakker17373852011-01-06 14:20:01 +0000168
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 strncpy((char *) md_name, text_md_name, sizeof(md_name) - 1);
170 md_info = mbedtls_md_info_from_string(md_name);
171 TEST_ASSERT(md_info != NULL);
Paul Bakker17373852011-01-06 14:20:01 +0000172
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 TEST_ASSERT(0 == mbedtls_md(md_info, src_str->x, src_str->len, output));
Paul Bakker17373852011-01-06 14:20:01 +0000174
Paul Bakker17373852011-01-06 14:20:01 +0000175
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
177 mbedtls_md_get_size(md_info),
178 hash->len) == 0);
Paul Bakker17373852011-01-06 14:20:01 +0000179}
Paul Bakker33b43f12013-08-20 11:48:36 +0200180/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000181
Paul Bakker33b43f12013-08-20 11:48:36 +0200182/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100183void md_text_multi(char *text_md_name, char *text_src_string,
184 data_t *hash)
Paul Bakker17373852011-01-06 14:20:01 +0000185{
186 char md_name[100];
187 unsigned char src_str[1000];
Paul Bakker17373852011-01-06 14:20:01 +0000188 unsigned char output[100];
Paul Bakkere35afa22016-07-13 17:09:14 +0100189 int halfway, len;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker97c53c22016-07-13 17:20:22 +0100192 mbedtls_md_context_t ctx, ctx_copy;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200193
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 mbedtls_md_init(&ctx);
195 mbedtls_md_init(&ctx_copy);
Paul Bakker17373852011-01-06 14:20:01 +0000196
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 memset(md_name, 0x00, 100);
198 memset(src_str, 0x00, 1000);
199 memset(output, 0x00, 100);
Paul Bakker17373852011-01-06 14:20:01 +0000200
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 strncpy((char *) src_str, text_src_string, sizeof(src_str) - 1);
202 strncpy((char *) md_name, text_md_name, sizeof(md_name) - 1);
203 len = strlen((char *) src_str);
Paul Bakkere35afa22016-07-13 17:09:14 +0100204 halfway = len / 2;
205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 md_info = mbedtls_md_info_from_string(md_name);
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 TEST_ASSERT(md_info != NULL);
208 TEST_ASSERT(0 == mbedtls_md_setup(&ctx, md_info, 0));
209 TEST_ASSERT(0 == mbedtls_md_setup(&ctx_copy, md_info, 0));
210 TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx) == md_info);
211 TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx_copy) == md_info);
Paul Bakker17373852011-01-06 14:20:01 +0000212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 TEST_ASSERT(0 == mbedtls_md_starts(&ctx));
214 TEST_ASSERT(ctx.md_ctx != NULL);
215 TEST_ASSERT(0 == mbedtls_md_update(&ctx, src_str, halfway));
216 TEST_ASSERT(0 == mbedtls_md_clone(&ctx_copy, &ctx));
Paul Bakker97c53c22016-07-13 17:20:22 +0100217
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 TEST_ASSERT(0 == mbedtls_md_update(&ctx, src_str + halfway, len - halfway));
219 TEST_ASSERT(0 == mbedtls_md_finish(&ctx, output));
220 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
221 mbedtls_md_get_size(md_info),
222 hash->len) == 0);
Paul Bakker17373852011-01-06 14:20:01 +0000223
Paul Bakker97c53c22016-07-13 17:20:22 +0100224 /* Test clone */
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 memset(output, 0x00, 100);
Paul Bakker97c53c22016-07-13 17:20:22 +0100226
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 TEST_ASSERT(0 == mbedtls_md_update(&ctx_copy, src_str + halfway, len - halfway));
228 TEST_ASSERT(0 == mbedtls_md_finish(&ctx_copy, output));
229 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
230 mbedtls_md_get_size(md_info),
231 hash->len) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200232
233exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 mbedtls_md_free(&ctx);
235 mbedtls_md_free(&ctx_copy);
Paul Bakker17373852011-01-06 14:20:01 +0000236}
Paul Bakker33b43f12013-08-20 11:48:36 +0200237/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000238
Paul Bakker33b43f12013-08-20 11:48:36 +0200239/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100240void md_hex_multi(char *text_md_name, data_t *src_str, data_t *hash)
Paul Bakker17373852011-01-06 14:20:01 +0000241{
242 char md_name[100];
Paul Bakker17373852011-01-06 14:20:01 +0000243 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker97c53c22016-07-13 17:20:22 +0100245 mbedtls_md_context_t ctx, ctx_copy;
Azim Khanf1aaec92017-05-30 14:23:15 +0100246 int halfway;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200247
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 mbedtls_md_init(&ctx);
249 mbedtls_md_init(&ctx_copy);
Paul Bakker17373852011-01-06 14:20:01 +0000250
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 memset(md_name, 0x00, 100);
252 memset(output, 0x00, 100);
Paul Bakker17373852011-01-06 14:20:01 +0000253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 strncpy((char *) md_name, text_md_name, sizeof(md_name) - 1);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 md_info = mbedtls_md_info_from_string(md_name);
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 TEST_ASSERT(md_info != NULL);
257 TEST_ASSERT(0 == mbedtls_md_setup(&ctx, md_info, 0));
258 TEST_ASSERT(0 == mbedtls_md_setup(&ctx_copy, md_info, 0));
259 TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx) == md_info);
260 TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx_copy) == md_info);
Paul Bakker17373852011-01-06 14:20:01 +0000261
Azim Khand30ca132017-06-09 04:32:58 +0100262 halfway = src_str->len / 2;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200263
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 TEST_ASSERT(0 == mbedtls_md_starts(&ctx));
265 TEST_ASSERT(ctx.md_ctx != NULL);
266 TEST_ASSERT(0 == mbedtls_md_update(&ctx, src_str->x, halfway));
267 TEST_ASSERT(0 == mbedtls_md_clone(&ctx_copy, &ctx));
Paul Bakker97c53c22016-07-13 17:20:22 +0100268
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 TEST_ASSERT(0 == mbedtls_md_update(&ctx, src_str->x + halfway, src_str->len - halfway));
270 TEST_ASSERT(0 == mbedtls_md_finish(&ctx, output));
271 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
272 mbedtls_md_get_size(md_info),
273 hash->len) == 0);
Paul Bakker17373852011-01-06 14:20:01 +0000274
Paul Bakker97c53c22016-07-13 17:20:22 +0100275 /* Test clone */
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 memset(output, 0x00, 100);
Paul Bakker97c53c22016-07-13 17:20:22 +0100277
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 TEST_ASSERT(0 == mbedtls_md_update(&ctx_copy, src_str->x + halfway, src_str->len - halfway));
279 TEST_ASSERT(0 == mbedtls_md_finish(&ctx_copy, output));
280 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
281 mbedtls_md_get_size(md_info),
282 hash->len) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200283
284exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 mbedtls_md_free(&ctx);
286 mbedtls_md_free(&ctx_copy);
Paul Bakker17373852011-01-06 14:20:01 +0000287}
Paul Bakker33b43f12013-08-20 11:48:36 +0200288/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000289
Paul Bakker33b43f12013-08-20 11:48:36 +0200290/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100291void mbedtls_md_hmac(char *text_md_name, int trunc_size,
292 data_t *key_str, data_t *src_str,
293 data_t *hash)
Paul Bakker17373852011-01-06 14:20:01 +0000294{
295 char md_name[100];
Paul Bakker17373852011-01-06 14:20:01 +0000296 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker17373852011-01-06 14:20:01 +0000298
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 memset(md_name, 0x00, 100);
300 memset(output, 0x00, 100);
Paul Bakker17373852011-01-06 14:20:01 +0000301
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 strncpy((char *) md_name, text_md_name, sizeof(md_name) - 1);
303 md_info = mbedtls_md_info_from_string(md_name);
304 TEST_ASSERT(md_info != NULL);
Paul Bakker17373852011-01-06 14:20:01 +0000305
Paul Bakker17373852011-01-06 14:20:01 +0000306
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 TEST_ASSERT(mbedtls_md_hmac(md_info, key_str->x, key_str->len, src_str->x, src_str->len,
308 output) == 0);
Paul Bakker17373852011-01-06 14:20:01 +0000309
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
311 trunc_size, hash->len) == 0);
Paul Bakker17373852011-01-06 14:20:01 +0000312}
Paul Bakker33b43f12013-08-20 11:48:36 +0200313/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000314
Paul Bakker33b43f12013-08-20 11:48:36 +0200315/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100316void md_hmac_multi(char *text_md_name, int trunc_size, data_t *key_str,
317 data_t *src_str, data_t *hash)
Paul Bakker17373852011-01-06 14:20:01 +0000318{
319 char md_name[100];
Paul Bakker17373852011-01-06 14:20:01 +0000320 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 const mbedtls_md_info_t *md_info = NULL;
322 mbedtls_md_context_t ctx;
Azim Khanf1aaec92017-05-30 14:23:15 +0100323 int halfway;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200324
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 mbedtls_md_init(&ctx);
Paul Bakker17373852011-01-06 14:20:01 +0000326
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 memset(md_name, 0x00, 100);
328 memset(output, 0x00, 100);
Paul Bakker17373852011-01-06 14:20:01 +0000329
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 strncpy((char *) md_name, text_md_name, sizeof(md_name) - 1);
331 md_info = mbedtls_md_info_from_string(md_name);
332 TEST_ASSERT(md_info != NULL);
333 TEST_ASSERT(0 == mbedtls_md_setup(&ctx, md_info, 1));
334 TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx) == md_info);
Paul Bakker17373852011-01-06 14:20:01 +0000335
Azim Khand30ca132017-06-09 04:32:58 +0100336 halfway = src_str->len / 2;
Paul Bakker17373852011-01-06 14:20:01 +0000337
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 TEST_ASSERT(0 == mbedtls_md_hmac_starts(&ctx, key_str->x, key_str->len));
339 TEST_ASSERT(ctx.md_ctx != NULL);
340 TEST_ASSERT(0 == mbedtls_md_hmac_update(&ctx, src_str->x, halfway));
341 TEST_ASSERT(0 == mbedtls_md_hmac_update(&ctx, src_str->x + halfway, src_str->len - halfway));
342 TEST_ASSERT(0 == mbedtls_md_hmac_finish(&ctx, output));
Manuel Pégourié-Gonnard59ba4e92014-03-29 14:43:44 +0100343
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
345 trunc_size, hash->len) == 0);
Manuel Pégourié-Gonnard59ba4e92014-03-29 14:43:44 +0100346
347 /* Test again, for reset() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard59ba4e92014-03-29 14:43:44 +0100349
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 TEST_ASSERT(0 == mbedtls_md_hmac_reset(&ctx));
351 TEST_ASSERT(0 == mbedtls_md_hmac_update(&ctx, src_str->x, halfway));
352 TEST_ASSERT(0 == mbedtls_md_hmac_update(&ctx, src_str->x + halfway, src_str->len - halfway));
353 TEST_ASSERT(0 == mbedtls_md_hmac_finish(&ctx, output));
Paul Bakker33b43f12013-08-20 11:48:36 +0200354
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
356 trunc_size, hash->len) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200357
358exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 mbedtls_md_free(&ctx);
Paul Bakker17373852011-01-06 14:20:01 +0000360}
Paul Bakker33b43f12013-08-20 11:48:36 +0200361/* END_CASE */
Paul Bakker428b9ba2013-09-15 15:20:37 +0200362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100364void mbedtls_md_file(char *text_md_name, char *filename,
365 data_t *hash)
Paul Bakker17373852011-01-06 14:20:01 +0000366{
367 char md_name[100];
Paul Bakker17373852011-01-06 14:20:01 +0000368 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker17373852011-01-06 14:20:01 +0000370
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 memset(md_name, 0x00, 100);
372 memset(output, 0x00, 100);
Paul Bakker17373852011-01-06 14:20:01 +0000373
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 strncpy((char *) md_name, text_md_name, sizeof(md_name) - 1);
375 md_info = mbedtls_md_info_from_string(md_name);
376 TEST_ASSERT(md_info != NULL);
Paul Bakker17373852011-01-06 14:20:01 +0000377
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 TEST_ASSERT(mbedtls_md_file(md_info, filename, output) == 0);
Paul Bakker17373852011-01-06 14:20:01 +0000379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
381 mbedtls_md_get_size(md_info),
382 hash->len) == 0);
Paul Bakker17373852011-01-06 14:20:01 +0000383}
Paul Bakker33b43f12013-08-20 11:48:36 +0200384/* END_CASE */