blob: 988cd39176638a23e25a9d3352cccdee73b71db8 [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{
Paul Bakker17373852011-01-06 14:20:01 +0000136 unsigned char src_str[1000];
Paul Bakker17373852011-01-06 14:20:01 +0000137 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker17373852011-01-06 14:20:01 +0000139
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 memset(src_str, 0x00, 1000);
141 memset(output, 0x00, 100);
Paul Bakker17373852011-01-06 14:20:01 +0000142
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 strncpy((char *) src_str, text_src_string, sizeof(src_str) - 1);
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100144 md_info = mbedtls_md_info_from_type(md_type);
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 TEST_ASSERT(md_info != NULL);
Paul Bakker17373852011-01-06 14:20:01 +0000146
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 TEST_ASSERT(0 == mbedtls_md(md_info, src_str, strlen((char *) src_str), output));
Paul Bakker17373852011-01-06 14:20:01 +0000148
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
150 mbedtls_md_get_size(md_info),
151 hash->len) == 0);
Paul Bakker17373852011-01-06 14:20:01 +0000152}
Paul Bakker33b43f12013-08-20 11:48:36 +0200153/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000154
Paul Bakker33b43f12013-08-20 11:48:36 +0200155/* BEGIN_CASE */
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100156void md_hex(int md_type, data_t *src_str, data_t *hash)
Paul Bakker17373852011-01-06 14:20:01 +0000157{
Paul Bakker17373852011-01-06 14:20:01 +0000158 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker17373852011-01-06 14:20:01 +0000160
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 memset(output, 0x00, 100);
Paul Bakker17373852011-01-06 14:20:01 +0000162
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100163 md_info = mbedtls_md_info_from_type(md_type);
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 TEST_ASSERT(md_info != NULL);
Paul Bakker17373852011-01-06 14:20:01 +0000165
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 TEST_ASSERT(0 == mbedtls_md(md_info, src_str->x, src_str->len, output));
Paul Bakker17373852011-01-06 14:20:01 +0000167
Paul Bakker17373852011-01-06 14:20:01 +0000168
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
170 mbedtls_md_get_size(md_info),
171 hash->len) == 0);
Paul Bakker17373852011-01-06 14:20:01 +0000172}
Paul Bakker33b43f12013-08-20 11:48:36 +0200173/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000174
Paul Bakker33b43f12013-08-20 11:48:36 +0200175/* BEGIN_CASE */
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100176void md_text_multi(int md_type, char *text_src_string,
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 data_t *hash)
Paul Bakker17373852011-01-06 14:20:01 +0000178{
Paul Bakker17373852011-01-06 14:20:01 +0000179 unsigned char src_str[1000];
Paul Bakker17373852011-01-06 14:20:01 +0000180 unsigned char output[100];
Paul Bakkere35afa22016-07-13 17:09:14 +0100181 int halfway, len;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker97c53c22016-07-13 17:20:22 +0100184 mbedtls_md_context_t ctx, ctx_copy;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200185
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 mbedtls_md_init(&ctx);
187 mbedtls_md_init(&ctx_copy);
Paul Bakker17373852011-01-06 14:20:01 +0000188
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 memset(src_str, 0x00, 1000);
190 memset(output, 0x00, 100);
Paul Bakker17373852011-01-06 14:20:01 +0000191
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 strncpy((char *) src_str, text_src_string, sizeof(src_str) - 1);
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 len = strlen((char *) src_str);
Paul Bakkere35afa22016-07-13 17:09:14 +0100194 halfway = len / 2;
195
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100196 md_info = mbedtls_md_info_from_type(md_type);
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 TEST_ASSERT(md_info != NULL);
198 TEST_ASSERT(0 == mbedtls_md_setup(&ctx, md_info, 0));
199 TEST_ASSERT(0 == mbedtls_md_setup(&ctx_copy, md_info, 0));
200 TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx) == md_info);
201 TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx_copy) == md_info);
Paul Bakker17373852011-01-06 14:20:01 +0000202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 TEST_ASSERT(0 == mbedtls_md_starts(&ctx));
204 TEST_ASSERT(ctx.md_ctx != NULL);
205 TEST_ASSERT(0 == mbedtls_md_update(&ctx, src_str, halfway));
206 TEST_ASSERT(0 == mbedtls_md_clone(&ctx_copy, &ctx));
Paul Bakker97c53c22016-07-13 17:20:22 +0100207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 TEST_ASSERT(0 == mbedtls_md_update(&ctx, src_str + halfway, len - halfway));
209 TEST_ASSERT(0 == mbedtls_md_finish(&ctx, output));
210 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
211 mbedtls_md_get_size(md_info),
212 hash->len) == 0);
Paul Bakker17373852011-01-06 14:20:01 +0000213
Paul Bakker97c53c22016-07-13 17:20:22 +0100214 /* Test clone */
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 memset(output, 0x00, 100);
Paul Bakker97c53c22016-07-13 17:20:22 +0100216
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 TEST_ASSERT(0 == mbedtls_md_update(&ctx_copy, src_str + halfway, len - halfway));
218 TEST_ASSERT(0 == mbedtls_md_finish(&ctx_copy, output));
219 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
220 mbedtls_md_get_size(md_info),
221 hash->len) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200222
223exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 mbedtls_md_free(&ctx);
225 mbedtls_md_free(&ctx_copy);
Paul Bakker17373852011-01-06 14:20:01 +0000226}
Paul Bakker33b43f12013-08-20 11:48:36 +0200227/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000228
Paul Bakker33b43f12013-08-20 11:48:36 +0200229/* BEGIN_CASE */
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100230void md_hex_multi(int md_type, data_t *src_str, data_t *hash)
Paul Bakker17373852011-01-06 14:20:01 +0000231{
Paul Bakker17373852011-01-06 14:20:01 +0000232 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker97c53c22016-07-13 17:20:22 +0100234 mbedtls_md_context_t ctx, ctx_copy;
Azim Khanf1aaec92017-05-30 14:23:15 +0100235 int halfway;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200236
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 mbedtls_md_init(&ctx);
238 mbedtls_md_init(&ctx_copy);
Paul Bakker17373852011-01-06 14:20:01 +0000239
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 memset(output, 0x00, 100);
Paul Bakker17373852011-01-06 14:20:01 +0000241
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100242 md_info = mbedtls_md_info_from_type(md_type);
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 TEST_ASSERT(md_info != NULL);
244 TEST_ASSERT(0 == mbedtls_md_setup(&ctx, md_info, 0));
245 TEST_ASSERT(0 == mbedtls_md_setup(&ctx_copy, md_info, 0));
246 TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx) == md_info);
247 TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx_copy) == md_info);
Paul Bakker17373852011-01-06 14:20:01 +0000248
Azim Khand30ca132017-06-09 04:32:58 +0100249 halfway = src_str->len / 2;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200250
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 TEST_ASSERT(0 == mbedtls_md_starts(&ctx));
252 TEST_ASSERT(ctx.md_ctx != NULL);
253 TEST_ASSERT(0 == mbedtls_md_update(&ctx, src_str->x, halfway));
254 TEST_ASSERT(0 == mbedtls_md_clone(&ctx_copy, &ctx));
Paul Bakker97c53c22016-07-13 17:20:22 +0100255
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 TEST_ASSERT(0 == mbedtls_md_update(&ctx, src_str->x + halfway, src_str->len - halfway));
257 TEST_ASSERT(0 == mbedtls_md_finish(&ctx, output));
258 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
259 mbedtls_md_get_size(md_info),
260 hash->len) == 0);
Paul Bakker17373852011-01-06 14:20:01 +0000261
Paul Bakker97c53c22016-07-13 17:20:22 +0100262 /* Test clone */
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 memset(output, 0x00, 100);
Paul Bakker97c53c22016-07-13 17:20:22 +0100264
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 TEST_ASSERT(0 == mbedtls_md_update(&ctx_copy, src_str->x + halfway, src_str->len - halfway));
266 TEST_ASSERT(0 == mbedtls_md_finish(&ctx_copy, output));
267 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
268 mbedtls_md_get_size(md_info),
269 hash->len) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200270
271exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 mbedtls_md_free(&ctx);
273 mbedtls_md_free(&ctx_copy);
Paul Bakker17373852011-01-06 14:20:01 +0000274}
Paul Bakker33b43f12013-08-20 11:48:36 +0200275/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000276
Paul Bakker33b43f12013-08-20 11:48:36 +0200277/* BEGIN_CASE */
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100278void mbedtls_md_hmac(int md_type, int trunc_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 data_t *key_str, data_t *src_str,
280 data_t *hash)
Paul Bakker17373852011-01-06 14:20:01 +0000281{
Paul Bakker17373852011-01-06 14:20:01 +0000282 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker17373852011-01-06 14:20:01 +0000284
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 memset(output, 0x00, 100);
Paul Bakker17373852011-01-06 14:20:01 +0000286
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100287 md_info = mbedtls_md_info_from_type(md_type);
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 TEST_ASSERT(md_info != NULL);
Paul Bakker17373852011-01-06 14:20:01 +0000289
Paul Bakker17373852011-01-06 14:20:01 +0000290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 TEST_ASSERT(mbedtls_md_hmac(md_info, key_str->x, key_str->len, src_str->x, src_str->len,
292 output) == 0);
Paul Bakker17373852011-01-06 14:20:01 +0000293
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
295 trunc_size, hash->len) == 0);
Paul Bakker17373852011-01-06 14:20:01 +0000296}
Paul Bakker33b43f12013-08-20 11:48:36 +0200297/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000298
Paul Bakker33b43f12013-08-20 11:48:36 +0200299/* BEGIN_CASE */
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100300void md_hmac_multi(int md_type, int trunc_size, data_t *key_str,
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 data_t *src_str, data_t *hash)
Paul Bakker17373852011-01-06 14:20:01 +0000302{
Paul Bakker17373852011-01-06 14:20:01 +0000303 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 const mbedtls_md_info_t *md_info = NULL;
305 mbedtls_md_context_t ctx;
Azim Khanf1aaec92017-05-30 14:23:15 +0100306 int halfway;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200307
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 mbedtls_md_init(&ctx);
Paul Bakker17373852011-01-06 14:20:01 +0000309
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 memset(output, 0x00, 100);
Paul Bakker17373852011-01-06 14:20:01 +0000311
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100312 md_info = mbedtls_md_info_from_type(md_type);
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 TEST_ASSERT(md_info != NULL);
314 TEST_ASSERT(0 == mbedtls_md_setup(&ctx, md_info, 1));
315 TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx) == md_info);
Paul Bakker17373852011-01-06 14:20:01 +0000316
Azim Khand30ca132017-06-09 04:32:58 +0100317 halfway = src_str->len / 2;
Paul Bakker17373852011-01-06 14:20:01 +0000318
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 TEST_ASSERT(0 == mbedtls_md_hmac_starts(&ctx, key_str->x, key_str->len));
320 TEST_ASSERT(ctx.md_ctx != NULL);
321 TEST_ASSERT(0 == mbedtls_md_hmac_update(&ctx, src_str->x, halfway));
322 TEST_ASSERT(0 == mbedtls_md_hmac_update(&ctx, src_str->x + halfway, src_str->len - halfway));
323 TEST_ASSERT(0 == mbedtls_md_hmac_finish(&ctx, output));
Manuel Pégourié-Gonnard59ba4e92014-03-29 14:43:44 +0100324
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
326 trunc_size, hash->len) == 0);
Manuel Pégourié-Gonnard59ba4e92014-03-29 14:43:44 +0100327
328 /* Test again, for reset() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard59ba4e92014-03-29 14:43:44 +0100330
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 TEST_ASSERT(0 == mbedtls_md_hmac_reset(&ctx));
332 TEST_ASSERT(0 == mbedtls_md_hmac_update(&ctx, src_str->x, halfway));
333 TEST_ASSERT(0 == mbedtls_md_hmac_update(&ctx, src_str->x + halfway, src_str->len - halfway));
334 TEST_ASSERT(0 == mbedtls_md_hmac_finish(&ctx, output));
Paul Bakker33b43f12013-08-20 11:48:36 +0200335
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
337 trunc_size, hash->len) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200338
339exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 mbedtls_md_free(&ctx);
Paul Bakker17373852011-01-06 14:20:01 +0000341}
Paul Bakker33b43f12013-08-20 11:48:36 +0200342/* END_CASE */
Paul Bakker428b9ba2013-09-15 15:20:37 +0200343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100345void mbedtls_md_file(int md_type, char *filename,
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 data_t *hash)
Paul Bakker17373852011-01-06 14:20:01 +0000347{
Paul Bakker17373852011-01-06 14:20:01 +0000348 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker17373852011-01-06 14:20:01 +0000350
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 memset(output, 0x00, 100);
Paul Bakker17373852011-01-06 14:20:01 +0000352
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100353 md_info = mbedtls_md_info_from_type(md_type);
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 TEST_ASSERT(md_info != NULL);
Paul Bakker17373852011-01-06 14:20:01 +0000355
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 TEST_ASSERT(mbedtls_md_file(md_info, filename, output) == 0);
Paul Bakker17373852011-01-06 14:20:01 +0000357
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
359 mbedtls_md_get_size(md_info),
360 hash->len) == 0);
Paul Bakker17373852011-01-06 14:20:01 +0000361}
Paul Bakker33b43f12013-08-20 11:48:36 +0200362/* END_CASE */