blob: d0f08e15940875baf916fc2198e566870c03dbc5 [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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Case0e7791f2021-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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +010039 mbedtls_md_free(&ctx);
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +010040}
41/* END_CASE */
42
43/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +010050 mbedtls_md_init(&ctx);
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020051
Gilles Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +010056 TEST_ASSERT(mbedtls_md_info_from_string(NULL) == NULL);
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020057
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010058 TEST_ASSERT(mbedtls_md_setup(&ctx, NULL, 0) == MBEDTLS_ERR_MD_BAD_INPUT_DATA);
59 TEST_ASSERT(mbedtls_md_setup(NULL, info, 0) == MBEDTLS_ERR_MD_BAD_INPUT_DATA);
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020060
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010061 TEST_ASSERT(mbedtls_md_starts(NULL) == MBEDTLS_ERR_MD_BAD_INPUT_DATA);
62 TEST_ASSERT(mbedtls_md_starts(&ctx) == MBEDTLS_ERR_MD_BAD_INPUT_DATA);
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020063
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010064 TEST_ASSERT(mbedtls_md_update(NULL, buf, 1) == MBEDTLS_ERR_MD_BAD_INPUT_DATA);
65 TEST_ASSERT(mbedtls_md_update(&ctx, buf, 1) == MBEDTLS_ERR_MD_BAD_INPUT_DATA);
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020066
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010067 TEST_ASSERT(mbedtls_md_finish(NULL, buf) == MBEDTLS_ERR_MD_BAD_INPUT_DATA);
68 TEST_ASSERT(mbedtls_md_finish(&ctx, buf) == MBEDTLS_ERR_MD_BAD_INPUT_DATA);
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020069
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010070 TEST_ASSERT(mbedtls_md(NULL, buf, 1, buf) == MBEDTLS_ERR_MD_BAD_INPUT_DATA);
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020071
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +020072#if defined(MBEDTLS_FS_IO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010073 TEST_ASSERT(mbedtls_md_file(NULL, "", buf) == MBEDTLS_ERR_MD_BAD_INPUT_DATA);
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +020074#endif
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020075
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010076 TEST_ASSERT(mbedtls_md_hmac_starts(NULL, buf, 1)
77 == MBEDTLS_ERR_MD_BAD_INPUT_DATA);
78 TEST_ASSERT(mbedtls_md_hmac_starts(&ctx, buf, 1)
79 == MBEDTLS_ERR_MD_BAD_INPUT_DATA);
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020080
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010081 TEST_ASSERT(mbedtls_md_hmac_update(NULL, buf, 1)
82 == MBEDTLS_ERR_MD_BAD_INPUT_DATA);
83 TEST_ASSERT(mbedtls_md_hmac_update(&ctx, buf, 1)
84 == MBEDTLS_ERR_MD_BAD_INPUT_DATA);
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020085
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010086 TEST_ASSERT(mbedtls_md_hmac_finish(NULL, buf)
87 == MBEDTLS_ERR_MD_BAD_INPUT_DATA);
88 TEST_ASSERT(mbedtls_md_hmac_finish(&ctx, buf)
89 == MBEDTLS_ERR_MD_BAD_INPUT_DATA);
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020090
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010091 TEST_ASSERT(mbedtls_md_hmac_reset(NULL) == MBEDTLS_ERR_MD_BAD_INPUT_DATA);
92 TEST_ASSERT(mbedtls_md_hmac_reset(&ctx) == MBEDTLS_ERR_MD_BAD_INPUT_DATA);
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020093
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010094 TEST_ASSERT(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
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010097 TEST_ASSERT(mbedtls_md_process(NULL, buf) == MBEDTLS_ERR_MD_BAD_INPUT_DATA);
98 TEST_ASSERT(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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100117 TEST_ASSERT(mbedtls_md_get_type(md_info) == (mbedtls_md_type_t) md_type);
118 TEST_ASSERT(mbedtls_md_get_size(md_info) == (unsigned char) md_size);
119 TEST_ASSERT(strcmp(mbedtls_md_get_name(md_info), md_name) == 0);
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +0100120
121 found = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +0100125 }
126 }
127 TEST_ASSERT(found == 1);
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +0100128}
129/* END_CASE */
130
131/* BEGIN_CASE */
Manuel Pégourié-Gonnarda876bd22023-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{
Paul Bakker17373852011-01-06 14:20:01 +0000134 unsigned char src_str[1000];
Manuel Pégourié-Gonnard2a5e2132023-02-03 12:25:53 +0100135 unsigned char output[MBEDTLS_MD_MAX_SIZE] = {0};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker17373852011-01-06 14:20:01 +0000137
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100138 memset(src_str, 0x00, 1000);
Paul Bakker17373852011-01-06 14:20:01 +0000139
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100140 strncpy((char *) src_str, text_src_string, sizeof(src_str) - 1);
Manuel Pégourié-Gonnarda876bd22023-02-03 12:13:10 +0100141 md_info = mbedtls_md_info_from_type(md_type);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100142 TEST_ASSERT(md_info != NULL);
Paul Bakker17373852011-01-06 14:20:01 +0000143
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100144 TEST_ASSERT(0 == mbedtls_md(md_info, src_str, strlen((char *) src_str), output));
Paul Bakker17373852011-01-06 14:20:01 +0000145
Gilles Peskine1b6c09a2023-01-11 14:52:35 +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é-Gonnarda876bd22023-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é-Gonnard2a5e2132023-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é-Gonnarda876bd22023-02-03 12:13:10 +0100158 md_info = mbedtls_md_info_from_type(md_type);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100159 TEST_ASSERT(md_info != NULL);
Paul Bakker17373852011-01-06 14:20:01 +0000160
Gilles Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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é-Gonnarda876bd22023-02-03 12:13:10 +0100171void md_text_multi(int md_type, char *text_src_string,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100172 data_t *hash)
Paul Bakker17373852011-01-06 14:20:01 +0000173{
Paul Bakker17373852011-01-06 14:20:01 +0000174 unsigned char src_str[1000];
Manuel Pégourié-Gonnard2a5e2132023-02-03 12:25:53 +0100175 unsigned char output[MBEDTLS_MD_MAX_SIZE] = {0};
Paul Bakkere35afa22016-07-13 17:09:14 +0100176 int halfway, len;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker97c53c22016-07-13 17:20:22 +0100179 mbedtls_md_context_t ctx, ctx_copy;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200180
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100181 mbedtls_md_init(&ctx);
182 mbedtls_md_init(&ctx_copy);
Paul Bakker17373852011-01-06 14:20:01 +0000183
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100184 memset(src_str, 0x00, 1000);
Paul Bakker17373852011-01-06 14:20:01 +0000185
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100186 strncpy((char *) src_str, text_src_string, sizeof(src_str) - 1);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100187 len = strlen((char *) src_str);
Paul Bakkere35afa22016-07-13 17:09:14 +0100188 halfway = len / 2;
189
Manuel Pégourié-Gonnarda876bd22023-02-03 12:13:10 +0100190 md_info = mbedtls_md_info_from_type(md_type);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100191 TEST_ASSERT(md_info != NULL);
192 TEST_ASSERT(0 == mbedtls_md_setup(&ctx, md_info, 0));
193 TEST_ASSERT(0 == mbedtls_md_setup(&ctx_copy, md_info, 0));
Paul Bakker17373852011-01-06 14:20:01 +0000194
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100195 TEST_ASSERT(0 == mbedtls_md_starts(&ctx));
196 TEST_ASSERT(ctx.md_ctx != NULL);
197 TEST_ASSERT(0 == mbedtls_md_update(&ctx, src_str, halfway));
198 TEST_ASSERT(0 == mbedtls_md_clone(&ctx_copy, &ctx));
Paul Bakker97c53c22016-07-13 17:20:22 +0100199
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100200 TEST_ASSERT(0 == mbedtls_md_update(&ctx, src_str + halfway, len - halfway));
201 TEST_ASSERT(0 == mbedtls_md_finish(&ctx, output));
202 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
203 mbedtls_md_get_size(md_info),
204 hash->len) == 0);
Paul Bakker17373852011-01-06 14:20:01 +0000205
Paul Bakker97c53c22016-07-13 17:20:22 +0100206 /* Test clone */
Manuel Pégourié-Gonnard2a5e2132023-02-03 12:25:53 +0100207 memset(output, 0x00, sizeof(output));
Paul Bakker97c53c22016-07-13 17:20:22 +0100208
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100209 TEST_ASSERT(0 == mbedtls_md_update(&ctx_copy, src_str + halfway, len - halfway));
210 TEST_ASSERT(0 == mbedtls_md_finish(&ctx_copy, output));
211 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
212 mbedtls_md_get_size(md_info),
213 hash->len) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200214
215exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100216 mbedtls_md_free(&ctx);
217 mbedtls_md_free(&ctx_copy);
Paul Bakker17373852011-01-06 14:20:01 +0000218}
Paul Bakker33b43f12013-08-20 11:48:36 +0200219/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000220
Paul Bakker33b43f12013-08-20 11:48:36 +0200221/* BEGIN_CASE */
Manuel Pégourié-Gonnarda876bd22023-02-03 12:13:10 +0100222void md_hex_multi(int md_type, data_t *src_str, data_t *hash)
Paul Bakker17373852011-01-06 14:20:01 +0000223{
Manuel Pégourié-Gonnard2a5e2132023-02-03 12:25:53 +0100224 unsigned char output[MBEDTLS_MD_MAX_SIZE] = {0};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker97c53c22016-07-13 17:20:22 +0100226 mbedtls_md_context_t ctx, ctx_copy;
Azim Khanf1aaec92017-05-30 14:23:15 +0100227 int halfway;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200228
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100229 mbedtls_md_init(&ctx);
230 mbedtls_md_init(&ctx_copy);
Paul Bakker17373852011-01-06 14:20:01 +0000231
Manuel Pégourié-Gonnarda876bd22023-02-03 12:13:10 +0100232 md_info = mbedtls_md_info_from_type(md_type);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100233 TEST_ASSERT(md_info != NULL);
234 TEST_ASSERT(0 == mbedtls_md_setup(&ctx, md_info, 0));
235 TEST_ASSERT(0 == mbedtls_md_setup(&ctx_copy, md_info, 0));
Paul Bakker17373852011-01-06 14:20:01 +0000236
Azim Khand30ca132017-06-09 04:32:58 +0100237 halfway = src_str->len / 2;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200238
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100239 TEST_ASSERT(0 == mbedtls_md_starts(&ctx));
240 TEST_ASSERT(ctx.md_ctx != NULL);
241 TEST_ASSERT(0 == mbedtls_md_update(&ctx, src_str->x, halfway));
242 TEST_ASSERT(0 == mbedtls_md_clone(&ctx_copy, &ctx));
Paul Bakker97c53c22016-07-13 17:20:22 +0100243
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100244 TEST_ASSERT(0 == mbedtls_md_update(&ctx, src_str->x + halfway, src_str->len - halfway));
245 TEST_ASSERT(0 == mbedtls_md_finish(&ctx, output));
246 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
247 mbedtls_md_get_size(md_info),
248 hash->len) == 0);
Paul Bakker17373852011-01-06 14:20:01 +0000249
Paul Bakker97c53c22016-07-13 17:20:22 +0100250 /* Test clone */
Manuel Pégourié-Gonnard2a5e2132023-02-03 12:25:53 +0100251 memset(output, 0x00, sizeof(output));
Paul Bakker97c53c22016-07-13 17:20:22 +0100252
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100253 TEST_ASSERT(0 == mbedtls_md_update(&ctx_copy, src_str->x + halfway, src_str->len - halfway));
254 TEST_ASSERT(0 == mbedtls_md_finish(&ctx_copy, output));
255 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
256 mbedtls_md_get_size(md_info),
257 hash->len) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200258
259exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100260 mbedtls_md_free(&ctx);
261 mbedtls_md_free(&ctx_copy);
Paul Bakker17373852011-01-06 14:20:01 +0000262}
Paul Bakker33b43f12013-08-20 11:48:36 +0200263/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000264
Paul Bakker33b43f12013-08-20 11:48:36 +0200265/* BEGIN_CASE */
Manuel Pégourié-Gonnarda876bd22023-02-03 12:13:10 +0100266void mbedtls_md_hmac(int md_type, int trunc_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100267 data_t *key_str, data_t *src_str,
268 data_t *hash)
Paul Bakker17373852011-01-06 14:20:01 +0000269{
Manuel Pégourié-Gonnard2a5e2132023-02-03 12:25:53 +0100270 unsigned char output[MBEDTLS_MD_MAX_SIZE] = {0};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker17373852011-01-06 14:20:01 +0000272
Manuel Pégourié-Gonnarda876bd22023-02-03 12:13:10 +0100273 md_info = mbedtls_md_info_from_type(md_type);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100274 TEST_ASSERT(md_info != NULL);
Paul Bakker17373852011-01-06 14:20:01 +0000275
Paul Bakker17373852011-01-06 14:20:01 +0000276
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100277 TEST_ASSERT(mbedtls_md_hmac(md_info, key_str->x, key_str->len, src_str->x, src_str->len,
278 output) == 0);
Paul Bakker17373852011-01-06 14:20:01 +0000279
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100280 TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
281 trunc_size, hash->len) == 0);
Paul Bakker17373852011-01-06 14:20:01 +0000282}
Paul Bakker33b43f12013-08-20 11:48:36 +0200283/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000284
Paul Bakker33b43f12013-08-20 11:48:36 +0200285/* BEGIN_CASE */
Manuel Pégourié-Gonnarda876bd22023-02-03 12:13:10 +0100286void md_hmac_multi(int md_type, int trunc_size, data_t *key_str,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100287 data_t *src_str, data_t *hash)
Paul Bakker17373852011-01-06 14:20:01 +0000288{
Manuel Pégourié-Gonnard2a5e2132023-02-03 12:25:53 +0100289 unsigned char output[MBEDTLS_MD_MAX_SIZE] = {0};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 const mbedtls_md_info_t *md_info = NULL;
291 mbedtls_md_context_t ctx;
Azim Khanf1aaec92017-05-30 14:23:15 +0100292 int halfway;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200293
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100294 mbedtls_md_init(&ctx);
Paul Bakker17373852011-01-06 14:20:01 +0000295
Manuel Pégourié-Gonnarda876bd22023-02-03 12:13:10 +0100296 md_info = mbedtls_md_info_from_type(md_type);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100297 TEST_ASSERT(md_info != NULL);
298 TEST_ASSERT(0 == mbedtls_md_setup(&ctx, md_info, 1));
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
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100302 TEST_ASSERT(0 == mbedtls_md_hmac_starts(&ctx, key_str->x, key_str->len));
303 TEST_ASSERT(ctx.md_ctx != NULL);
304 TEST_ASSERT(0 == mbedtls_md_hmac_update(&ctx, src_str->x, halfway));
305 TEST_ASSERT(0 == mbedtls_md_hmac_update(&ctx, src_str->x + halfway, src_str->len - halfway));
306 TEST_ASSERT(0 == mbedtls_md_hmac_finish(&ctx, output));
Manuel Pégourié-Gonnard59ba4e92014-03-29 14:43:44 +0100307
Gilles Peskine1b6c09a2023-01-11 14:52:35 +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é-Gonnard2a5e2132023-02-03 12:25:53 +0100312 memset(output, 0x00, sizeof(output));
Manuel Pégourié-Gonnard59ba4e92014-03-29 14:43:44 +0100313
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100314 TEST_ASSERT(0 == mbedtls_md_hmac_reset(&ctx));
315 TEST_ASSERT(0 == mbedtls_md_hmac_update(&ctx, src_str->x, halfway));
316 TEST_ASSERT(0 == mbedtls_md_hmac_update(&ctx, src_str->x + halfway, src_str->len - halfway));
317 TEST_ASSERT(0 == mbedtls_md_hmac_finish(&ctx, output));
Paul Bakker33b43f12013-08-20 11:48:36 +0200318
Gilles Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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é-Gonnarda876bd22023-02-03 12:13:10 +0100328void mbedtls_md_file(int md_type, char *filename,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100329 data_t *hash)
Paul Bakker17373852011-01-06 14:20:01 +0000330{
Manuel Pégourié-Gonnard2a5e2132023-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é-Gonnarda876bd22023-02-03 12:13:10 +0100334 md_info = mbedtls_md_info_from_type(md_type);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100335 TEST_ASSERT(md_info != NULL);
Paul Bakker17373852011-01-06 14:20:01 +0000336
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100337 TEST_ASSERT(mbedtls_md_file(md_info, filename, output) == 0);
Paul Bakker17373852011-01-06 14:20:01 +0000338
Gilles Peskine1b6c09a2023-01-11 14:52:35 +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 */