blob: 64a417147ebbbbbdc41b0893f97d7d627af5c087 [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"
Manuel Pégourié-Gonnardec31f292023-03-14 11:03:37 +01003
4#if defined(MBEDTLS_MD_SOME_PSA)
5#define MD_PSA_INIT() PSA_INIT()
6#define MD_PSA_DONE() PSA_DONE()
7#else /* MBEDTLS_MD_SOME_PSA */
8#define MD_PSA_INIT() ((void) 0)
9#define MD_PSA_DONE() ((void) 0)
10#endif /* MBEDTLS_MD_SOME_PSA */
Paul Bakker33b43f12013-08-20 11:48:36 +020011/* END_HEADER */
Paul Bakker17373852011-01-06 14:20:01 +000012
Paul Bakker33b43f12013-08-20 11:48:36 +020013/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnardb9b630d2023-02-16 19:07:31 +010014 * depends_on:MBEDTLS_MD_LIGHT
Paul Bakker33b43f12013-08-20 11:48:36 +020015 * END_DEPENDENCIES
16 */
Paul Bakker5690efc2011-05-26 13:16:06 +000017
Manuel Pégourié-Gonnardb9b630d2023-02-16 19:07:31 +010018/* BEGIN_CASE depends_on:MBEDTLS_MD_C */
Manuel Pégourié-Gonnardba2412f2023-02-16 18:44:46 +010019void mbedtls_md_list()
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +010020{
21 const int *md_type_ptr;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022 const mbedtls_md_info_t *info;
23 mbedtls_md_context_t ctx;
Manuel Pégourié-Gonnardba2412f2023-02-16 18:44:46 +010024 unsigned char out[MBEDTLS_MD_MAX_SIZE] = { 0 };
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +010025
Manuel Pégourié-Gonnardec31f292023-03-14 11:03:37 +010026 MD_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +010027 mbedtls_md_init(&ctx);
Manuel Pégourié-Gonnardedb242f2014-04-02 17:52:04 +020028
29 /*
Manuel Pégourié-Gonnardba2412f2023-02-16 18:44:46 +010030 * Test that mbedtls_md_list() only returns valid MDs.
Manuel Pégourié-Gonnardedb242f2014-04-02 17:52:04 +020031 */
Gilles Peskine449bd832023-01-11 14:50:10 +010032 for (md_type_ptr = mbedtls_md_list(); *md_type_ptr != 0; md_type_ptr++) {
33 info = mbedtls_md_info_from_type(*md_type_ptr);
34 TEST_ASSERT(info != NULL);
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +010035 TEST_EQUAL(0, mbedtls_md_setup(&ctx, info, 0));
36 TEST_EQUAL(0, mbedtls_md_starts(&ctx));
Manuel Pégourié-Gonnardba2412f2023-02-16 18:44:46 +010037 TEST_EQUAL(0, mbedtls_md_finish(&ctx, out));
Gilles Peskine449bd832023-01-11 14:50:10 +010038 mbedtls_md_free(&ctx);
Manuel Pégourié-Gonnardedb242f2014-04-02 17:52:04 +020039 }
Paul Bakkerbd51b262014-07-10 15:26:12 +020040
41exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010042 mbedtls_md_free(&ctx);
Manuel Pégourié-Gonnardec31f292023-03-14 11:03:37 +010043 MD_PSA_DONE();
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +010044}
45/* END_CASE */
46
47/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010048void md_null_args()
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020049{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050 mbedtls_md_context_t ctx;
Manuel Pégourié-Gonnardb9b630d2023-02-16 19:07:31 +010051#if defined(MBEDTLS_MD_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010052 const mbedtls_md_info_t *info = mbedtls_md_info_from_type(*(mbedtls_md_list()));
Manuel Pégourié-Gonnardb9b630d2023-02-16 19:07:31 +010053#endif
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020054 unsigned char buf[1] = { 0 };
55
Manuel Pégourié-Gonnardec31f292023-03-14 11:03:37 +010056 MD_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +010057 mbedtls_md_init(&ctx);
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020058
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +010059 TEST_EQUAL(0, mbedtls_md_get_size(NULL));
Manuel Pégourié-Gonnardb9b630d2023-02-16 19:07:31 +010060#if defined(MBEDTLS_MD_C)
Manuel Pégourié-Gonnard2189fda2023-02-09 09:18:22 +010061 TEST_EQUAL(mbedtls_md_get_type(NULL), MBEDTLS_MD_NONE);
Gilles Peskine449bd832023-01-11 14:50:10 +010062 TEST_ASSERT(mbedtls_md_get_name(NULL) == NULL);
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020063
Gilles Peskine449bd832023-01-11 14:50:10 +010064 TEST_ASSERT(mbedtls_md_info_from_string(NULL) == NULL);
65 TEST_ASSERT(mbedtls_md_info_from_ctx(NULL) == NULL);
66 TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx) == NULL);
Manuel Pégourié-Gonnardb9b630d2023-02-16 19:07:31 +010067#endif /* MBEDTLS_MD_C */
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020068
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +010069 TEST_EQUAL(mbedtls_md_setup(&ctx, NULL, 0), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
Manuel Pégourié-Gonnardb9b630d2023-02-16 19:07:31 +010070#if defined(MBEDTLS_MD_C)
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +010071 TEST_EQUAL(mbedtls_md_setup(NULL, info, 0), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
Manuel Pégourié-Gonnardb9b630d2023-02-16 19:07:31 +010072#endif
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020073
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +010074 TEST_EQUAL(mbedtls_md_starts(NULL), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
75 TEST_EQUAL(mbedtls_md_starts(&ctx), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020076
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +010077 TEST_EQUAL(mbedtls_md_update(NULL, buf, 1), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
78 TEST_EQUAL(mbedtls_md_update(&ctx, buf, 1), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020079
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +010080 TEST_EQUAL(mbedtls_md_finish(NULL, buf), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
81 TEST_EQUAL(mbedtls_md_finish(&ctx, buf), 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(NULL, buf, 1, buf), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020084
Manuel Pégourié-Gonnardb9b630d2023-02-16 19:07:31 +010085#if defined(MBEDTLS_MD_C)
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +020086#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +010087 TEST_EQUAL(mbedtls_md_file(NULL, "", buf), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +020088#endif
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020089
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +010090 TEST_EQUAL(mbedtls_md_hmac_starts(NULL, buf, 1),
91 MBEDTLS_ERR_MD_BAD_INPUT_DATA);
92 TEST_EQUAL(mbedtls_md_hmac_starts(&ctx, buf, 1),
93 MBEDTLS_ERR_MD_BAD_INPUT_DATA);
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020094
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +010095 TEST_EQUAL(mbedtls_md_hmac_update(NULL, buf, 1),
96 MBEDTLS_ERR_MD_BAD_INPUT_DATA);
97 TEST_EQUAL(mbedtls_md_hmac_update(&ctx, buf, 1),
98 MBEDTLS_ERR_MD_BAD_INPUT_DATA);
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020099
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +0100100 TEST_EQUAL(mbedtls_md_hmac_finish(NULL, buf), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
101 TEST_EQUAL(mbedtls_md_hmac_finish(&ctx, buf), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +0200102
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +0100103 TEST_EQUAL(mbedtls_md_hmac_reset(NULL), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
104 TEST_EQUAL(mbedtls_md_hmac_reset(&ctx), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +0200105
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +0100106 TEST_EQUAL(mbedtls_md_hmac(NULL, buf, 1, buf, 1, buf),
107 MBEDTLS_ERR_MD_BAD_INPUT_DATA);
Manuel Pégourié-Gonnardb9b630d2023-02-16 19:07:31 +0100108#endif /* MBEDTLS_MD_C */
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +0200109
Manuel Pégourié-Gonnard19d644b2015-03-26 12:42:35 +0100110 /* Ok, this is not NULL arg but NULL return... */
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 TEST_ASSERT(mbedtls_md_info_from_type(MBEDTLS_MD_NONE) == NULL);
Manuel Pégourié-Gonnardb9b630d2023-02-16 19:07:31 +0100112#if defined(MBEDTLS_MD_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 TEST_ASSERT(mbedtls_md_info_from_string("no such md") == NULL);
Manuel Pégourié-Gonnardb9b630d2023-02-16 19:07:31 +0100114#endif
Manuel Pégourié-Gonnardec31f292023-03-14 11:03:37 +0100115
116exit:
117 MD_PSA_DONE();
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +0200118}
119/* END_CASE */
120
121/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100122void md_info(int md_type, char *md_name, int md_size)
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +0100123{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardb9b630d2023-02-16 19:07:31 +0100125#if defined(MBEDTLS_MD_C)
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +0100126 const int *md_type_ptr;
Manuel Pégourié-Gonnardb9b630d2023-02-16 19:07:31 +0100127#else
128 (void) md_name;
129#endif
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +0100130
Manuel Pégourié-Gonnardec31f292023-03-14 11:03:37 +0100131 /* Note: PSA Crypto init not needed to info functions */
132
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 md_info = mbedtls_md_info_from_type(md_type);
134 TEST_ASSERT(md_info != NULL);
Manuel Pégourié-Gonnardb9b630d2023-02-16 19:07:31 +0100135#if defined(MBEDTLS_MD_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 TEST_ASSERT(md_info == mbedtls_md_info_from_string(md_name));
Manuel Pégourié-Gonnardb9b630d2023-02-16 19:07:31 +0100137#endif
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +0100138
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +0100139 TEST_EQUAL(mbedtls_md_get_type(md_info), (mbedtls_md_type_t) md_type);
140 TEST_EQUAL(mbedtls_md_get_size(md_info), (unsigned char) md_size);
Manuel Pégourié-Gonnardb9b630d2023-02-16 19:07:31 +0100141#if defined(MBEDTLS_MD_C)
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +0100142 TEST_EQUAL(0, strcmp(mbedtls_md_get_name(md_info), md_name));
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +0100143
Manuel Pégourié-Gonnardb9b630d2023-02-16 19:07:31 +0100144 int found = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 for (md_type_ptr = mbedtls_md_list(); *md_type_ptr != 0; md_type_ptr++) {
146 if (*md_type_ptr == md_type) {
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +0100147 found = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 }
149 }
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +0100150 TEST_EQUAL(found, 1);
Manuel Pégourié-Gonnardb9b630d2023-02-16 19:07:31 +0100151#endif /* MBEDTLS_MD_C */
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +0100152}
153/* END_CASE */
154
155/* BEGIN_CASE */
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100156void md_text(int md_type, char *text_src_string, data_t *hash)
Paul Bakker17373852011-01-06 14:20:01 +0000157{
Manuel Pégourié-Gonnardb707bed2023-02-03 12:32:41 +0100158 unsigned char *src = (unsigned char *) text_src_string;
159 size_t src_len = strlen(text_src_string);
Manuel Pégourié-Gonnardcced3522023-02-06 12:37:02 +0100160 unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 };
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker17373852011-01-06 14:20:01 +0000162
Manuel Pégourié-Gonnardec31f292023-03-14 11:03:37 +0100163 MD_PSA_INIT();
164
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100165 md_info = mbedtls_md_info_from_type(md_type);
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 TEST_ASSERT(md_info != NULL);
Paul Bakker17373852011-01-06 14:20:01 +0000167
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +0100168 TEST_EQUAL(0, mbedtls_md(md_info, src, src_len, output));
Paul Bakker17373852011-01-06 14:20:01 +0000169
Manuel Pégourié-Gonnarda9a1b212023-02-09 09:15:04 +0100170 ASSERT_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len);
Manuel Pégourié-Gonnardec31f292023-03-14 11:03:37 +0100171
172exit:
173 MD_PSA_DONE();
Paul Bakker17373852011-01-06 14:20:01 +0000174}
Paul Bakker33b43f12013-08-20 11:48:36 +0200175/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000176
Paul Bakker33b43f12013-08-20 11:48:36 +0200177/* BEGIN_CASE */
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100178void md_hex(int md_type, data_t *src_str, data_t *hash)
Paul Bakker17373852011-01-06 14:20:01 +0000179{
Manuel Pégourié-Gonnardcced3522023-02-06 12:37:02 +0100180 unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 };
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker17373852011-01-06 14:20:01 +0000182
Manuel Pégourié-Gonnardec31f292023-03-14 11:03:37 +0100183 MD_PSA_INIT();
184
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);
Paul Bakker17373852011-01-06 14:20:01 +0000187
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +0100188 TEST_EQUAL(0, mbedtls_md(md_info, src_str->x, src_str->len, output));
Paul Bakker17373852011-01-06 14:20:01 +0000189
Paul Bakker17373852011-01-06 14:20:01 +0000190
Manuel Pégourié-Gonnarda9a1b212023-02-09 09:15:04 +0100191 ASSERT_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len);
Manuel Pégourié-Gonnardec31f292023-03-14 11:03:37 +0100192
193exit:
194 MD_PSA_DONE();
Paul Bakker17373852011-01-06 14:20:01 +0000195}
Paul Bakker33b43f12013-08-20 11:48:36 +0200196/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000197
Paul Bakker33b43f12013-08-20 11:48:36 +0200198/* BEGIN_CASE */
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100199void md_text_multi(int md_type, char *text_src_string,
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 data_t *hash)
Paul Bakker17373852011-01-06 14:20:01 +0000201{
Manuel Pégourié-Gonnardb707bed2023-02-03 12:32:41 +0100202 unsigned char *src = (unsigned char *) text_src_string;
203 size_t src_len = strlen(text_src_string);
Manuel Pégourié-Gonnardcced3522023-02-06 12:37:02 +0100204 unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 };
Manuel Pégourié-Gonnardb707bed2023-02-03 12:32:41 +0100205 size_t halfway;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker97c53c22016-07-13 17:20:22 +0100208 mbedtls_md_context_t ctx, ctx_copy;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200209
Manuel Pégourié-Gonnardec31f292023-03-14 11:03:37 +0100210 MD_PSA_INIT();
211
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 mbedtls_md_init(&ctx);
213 mbedtls_md_init(&ctx_copy);
Paul Bakker17373852011-01-06 14:20:01 +0000214
Manuel Pégourié-Gonnardb707bed2023-02-03 12:32:41 +0100215 halfway = src_len / 2;
Paul Bakkere35afa22016-07-13 17:09:14 +0100216
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100217 md_info = mbedtls_md_info_from_type(md_type);
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 TEST_ASSERT(md_info != NULL);
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +0100219 TEST_EQUAL(0, mbedtls_md_setup(&ctx, md_info, 0));
220 TEST_EQUAL(0, mbedtls_md_setup(&ctx_copy, md_info, 0));
Manuel Pégourié-Gonnardb9b630d2023-02-16 19:07:31 +0100221#if defined(MBEDTLS_MD_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx) == md_info);
223 TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx_copy) == md_info);
Manuel Pégourié-Gonnardb9b630d2023-02-16 19:07:31 +0100224#endif /* MBEDTLS_MD_C */
Paul Bakker17373852011-01-06 14:20:01 +0000225
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +0100226 TEST_EQUAL(0, mbedtls_md_starts(&ctx));
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 TEST_ASSERT(ctx.md_ctx != NULL);
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +0100228 TEST_EQUAL(0, mbedtls_md_update(&ctx, src, halfway));
229 TEST_EQUAL(0, mbedtls_md_clone(&ctx_copy, &ctx));
Paul Bakker97c53c22016-07-13 17:20:22 +0100230
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +0100231 TEST_EQUAL(0, mbedtls_md_update(&ctx, src + halfway, src_len - halfway));
232 TEST_EQUAL(0, mbedtls_md_finish(&ctx, output));
Manuel Pégourié-Gonnarda9a1b212023-02-09 09:15:04 +0100233 ASSERT_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len);
Paul Bakker17373852011-01-06 14:20:01 +0000234
Paul Bakker97c53c22016-07-13 17:20:22 +0100235 /* Test clone */
Manuel Pégourié-Gonnard4ba98f52023-02-03 12:25:53 +0100236 memset(output, 0x00, sizeof(output));
Paul Bakker97c53c22016-07-13 17:20:22 +0100237
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +0100238 TEST_EQUAL(0, mbedtls_md_update(&ctx_copy, src + halfway, src_len - halfway));
239 TEST_EQUAL(0, mbedtls_md_finish(&ctx_copy, output));
Manuel Pégourié-Gonnarda9a1b212023-02-09 09:15:04 +0100240 ASSERT_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200241
242exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 mbedtls_md_free(&ctx);
244 mbedtls_md_free(&ctx_copy);
Manuel Pégourié-Gonnardec31f292023-03-14 11:03:37 +0100245 MD_PSA_DONE();
Paul Bakker17373852011-01-06 14:20:01 +0000246}
Paul Bakker33b43f12013-08-20 11:48:36 +0200247/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000248
Paul Bakker33b43f12013-08-20 11:48:36 +0200249/* BEGIN_CASE */
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100250void md_hex_multi(int md_type, data_t *src_str, data_t *hash)
Paul Bakker17373852011-01-06 14:20:01 +0000251{
Manuel Pégourié-Gonnardcced3522023-02-06 12:37:02 +0100252 unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 };
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker97c53c22016-07-13 17:20:22 +0100254 mbedtls_md_context_t ctx, ctx_copy;
Azim Khanf1aaec92017-05-30 14:23:15 +0100255 int halfway;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200256
Manuel Pégourié-Gonnardec31f292023-03-14 11:03:37 +0100257 MD_PSA_INIT();
258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 mbedtls_md_init(&ctx);
260 mbedtls_md_init(&ctx_copy);
Paul Bakker17373852011-01-06 14:20:01 +0000261
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100262 md_info = mbedtls_md_info_from_type(md_type);
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 TEST_ASSERT(md_info != NULL);
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +0100264 TEST_EQUAL(0, mbedtls_md_setup(&ctx, md_info, 0));
265 TEST_EQUAL(0, mbedtls_md_setup(&ctx_copy, md_info, 0));
Manuel Pégourié-Gonnardb9b630d2023-02-16 19:07:31 +0100266#if defined(MBEDTLS_MD_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx) == md_info);
268 TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx_copy) == md_info);
Manuel Pégourié-Gonnardb9b630d2023-02-16 19:07:31 +0100269#endif /* MBEDTLS_MD_C */
Paul Bakker17373852011-01-06 14:20:01 +0000270
Azim Khand30ca132017-06-09 04:32:58 +0100271 halfway = src_str->len / 2;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200272
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +0100273 TEST_EQUAL(0, mbedtls_md_starts(&ctx));
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 TEST_ASSERT(ctx.md_ctx != NULL);
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +0100275 TEST_EQUAL(0, mbedtls_md_update(&ctx, src_str->x, halfway));
276 TEST_EQUAL(0, mbedtls_md_clone(&ctx_copy, &ctx));
Paul Bakker97c53c22016-07-13 17:20:22 +0100277
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +0100278 TEST_EQUAL(0, mbedtls_md_update(&ctx, src_str->x + halfway, src_str->len - halfway));
279 TEST_EQUAL(0, mbedtls_md_finish(&ctx, output));
Manuel Pégourié-Gonnarda9a1b212023-02-09 09:15:04 +0100280 ASSERT_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len);
Paul Bakker17373852011-01-06 14:20:01 +0000281
Paul Bakker97c53c22016-07-13 17:20:22 +0100282 /* Test clone */
Manuel Pégourié-Gonnard4ba98f52023-02-03 12:25:53 +0100283 memset(output, 0x00, sizeof(output));
Paul Bakker97c53c22016-07-13 17:20:22 +0100284
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +0100285 TEST_EQUAL(0, mbedtls_md_update(&ctx_copy, src_str->x + halfway, src_str->len - halfway));
286 TEST_EQUAL(0, mbedtls_md_finish(&ctx_copy, output));
Manuel Pégourié-Gonnarda9a1b212023-02-09 09:15:04 +0100287 ASSERT_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200288
289exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 mbedtls_md_free(&ctx);
291 mbedtls_md_free(&ctx_copy);
Manuel Pégourié-Gonnardec31f292023-03-14 11:03:37 +0100292 MD_PSA_DONE();
Paul Bakker17373852011-01-06 14:20:01 +0000293}
Paul Bakker33b43f12013-08-20 11:48:36 +0200294/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000295
Manuel Pégourié-Gonnardb9b630d2023-02-16 19:07:31 +0100296/* BEGIN_CASE depends_on:MBEDTLS_MD_C */
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100297void mbedtls_md_hmac(int md_type, int trunc_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 data_t *key_str, data_t *src_str,
299 data_t *hash)
Paul Bakker17373852011-01-06 14:20:01 +0000300{
Manuel Pégourié-Gonnardcced3522023-02-06 12:37:02 +0100301 unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 };
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker17373852011-01-06 14:20:01 +0000303
Manuel Pégourié-Gonnardec31f292023-03-14 11:03:37 +0100304 MD_PSA_INIT();
305
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100306 md_info = mbedtls_md_info_from_type(md_type);
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 TEST_ASSERT(md_info != NULL);
Paul Bakker17373852011-01-06 14:20:01 +0000308
Paul Bakker17373852011-01-06 14:20:01 +0000309
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +0100310 TEST_EQUAL(0, mbedtls_md_hmac(md_info, key_str->x, key_str->len,
Manuel Pégourié-Gonnardcced3522023-02-06 12:37:02 +0100311 src_str->x, src_str->len, output));
Paul Bakker17373852011-01-06 14:20:01 +0000312
Manuel Pégourié-Gonnarda9a1b212023-02-09 09:15:04 +0100313 ASSERT_COMPARE(output, trunc_size, hash->x, hash->len);
Manuel Pégourié-Gonnardec31f292023-03-14 11:03:37 +0100314
315exit:
316 MD_PSA_DONE();
Paul Bakker17373852011-01-06 14:20:01 +0000317}
Paul Bakker33b43f12013-08-20 11:48:36 +0200318/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000319
Manuel Pégourié-Gonnardb9b630d2023-02-16 19:07:31 +0100320/* BEGIN_CASE depends_on:MBEDTLS_MD_C */
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100321void md_hmac_multi(int md_type, int trunc_size, data_t *key_str,
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 data_t *src_str, data_t *hash)
Paul Bakker17373852011-01-06 14:20:01 +0000323{
Manuel Pégourié-Gonnardcced3522023-02-06 12:37:02 +0100324 unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 };
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 const mbedtls_md_info_t *md_info = NULL;
326 mbedtls_md_context_t ctx;
Azim Khanf1aaec92017-05-30 14:23:15 +0100327 int halfway;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200328
Manuel Pégourié-Gonnardec31f292023-03-14 11:03:37 +0100329 MD_PSA_INIT();
330
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 mbedtls_md_init(&ctx);
Paul Bakker17373852011-01-06 14:20:01 +0000332
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100333 md_info = mbedtls_md_info_from_type(md_type);
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 TEST_ASSERT(md_info != NULL);
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +0100335 TEST_EQUAL(0, mbedtls_md_setup(&ctx, md_info, 1));
Manuel Pégourié-Gonnardb9b630d2023-02-16 19:07:31 +0100336#if defined(MBEDTLS_MD_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx) == md_info);
Manuel Pégourié-Gonnardb9b630d2023-02-16 19:07:31 +0100338#endif
Paul Bakker17373852011-01-06 14:20:01 +0000339
Azim Khand30ca132017-06-09 04:32:58 +0100340 halfway = src_str->len / 2;
Paul Bakker17373852011-01-06 14:20:01 +0000341
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +0100342 TEST_EQUAL(0, mbedtls_md_hmac_starts(&ctx, key_str->x, key_str->len));
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 TEST_ASSERT(ctx.md_ctx != NULL);
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +0100344 TEST_EQUAL(0, mbedtls_md_hmac_update(&ctx, src_str->x, halfway));
345 TEST_EQUAL(0, mbedtls_md_hmac_update(&ctx, src_str->x + halfway, src_str->len - halfway));
346 TEST_EQUAL(0, mbedtls_md_hmac_finish(&ctx, output));
Manuel Pégourié-Gonnard59ba4e92014-03-29 14:43:44 +0100347
Manuel Pégourié-Gonnarda9a1b212023-02-09 09:15:04 +0100348 ASSERT_COMPARE(output, trunc_size, hash->x, hash->len);
Manuel Pégourié-Gonnard59ba4e92014-03-29 14:43:44 +0100349
350 /* Test again, for reset() */
Manuel Pégourié-Gonnard4ba98f52023-02-03 12:25:53 +0100351 memset(output, 0x00, sizeof(output));
Manuel Pégourié-Gonnard59ba4e92014-03-29 14:43:44 +0100352
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +0100353 TEST_EQUAL(0, mbedtls_md_hmac_reset(&ctx));
354 TEST_EQUAL(0, mbedtls_md_hmac_update(&ctx, src_str->x, halfway));
355 TEST_EQUAL(0, mbedtls_md_hmac_update(&ctx, src_str->x + halfway, src_str->len - halfway));
356 TEST_EQUAL(0, mbedtls_md_hmac_finish(&ctx, output));
Paul Bakker33b43f12013-08-20 11:48:36 +0200357
Manuel Pégourié-Gonnarda9a1b212023-02-09 09:15:04 +0100358 ASSERT_COMPARE(output, trunc_size, hash->x, hash->len);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200359
360exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 mbedtls_md_free(&ctx);
Manuel Pégourié-Gonnardec31f292023-03-14 11:03:37 +0100362 MD_PSA_DONE();
Paul Bakker17373852011-01-06 14:20:01 +0000363}
Paul Bakker33b43f12013-08-20 11:48:36 +0200364/* END_CASE */
Paul Bakker428b9ba2013-09-15 15:20:37 +0200365
Manuel Pégourié-Gonnardb9b630d2023-02-16 19:07:31 +0100366/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_MD_C */
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100367void mbedtls_md_file(int md_type, char *filename,
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 data_t *hash)
Paul Bakker17373852011-01-06 14:20:01 +0000369{
Manuel Pégourié-Gonnardcced3522023-02-06 12:37:02 +0100370 unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 };
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker17373852011-01-06 14:20:01 +0000372
Manuel Pégourié-Gonnardec31f292023-03-14 11:03:37 +0100373 MD_PSA_INIT();
374
Manuel Pégourié-Gonnardc90514e2023-02-03 12:13:10 +0100375 md_info = mbedtls_md_info_from_type(md_type);
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 TEST_ASSERT(md_info != NULL);
Paul Bakker17373852011-01-06 14:20:01 +0000377
Manuel Pégourié-Gonnardf5e23312023-02-03 12:51:03 +0100378 TEST_EQUAL(0, mbedtls_md_file(md_info, filename, output));
Paul Bakker17373852011-01-06 14:20:01 +0000379
Manuel Pégourié-Gonnarda9a1b212023-02-09 09:15:04 +0100380 ASSERT_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len);
Manuel Pégourié-Gonnardec31f292023-03-14 11:03:37 +0100381
382exit:
383 MD_PSA_DONE();
Paul Bakker17373852011-01-06 14:20:01 +0000384}
Paul Bakker33b43f12013-08-20 11:48:36 +0200385/* END_CASE */
Manuel Pégourié-Gonnard9b146392023-03-09 15:56:14 +0100386
387/* BEGIN_CASE */
388void md_psa_dynamic_dispatch(int md_type, int pre_psa_ret, int post_psa_engine)
389{
390 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_type);
391 TEST_ASSERT(md_info != NULL);
392 mbedtls_md_context_t ctx1, ctx2;
393
Manuel Pégourié-Gonnardec31f292023-03-14 11:03:37 +0100394 /* Intentionally no PSA init here! (Will be done later.) */
395
Manuel Pégourié-Gonnard9b146392023-03-09 15:56:14 +0100396 mbedtls_md_init(&ctx1);
397 mbedtls_md_init(&ctx2);
398
399 /* Before PSA crypto init */
400 TEST_EQUAL(pre_psa_ret, mbedtls_md_setup(&ctx1, md_info, 0));
401 TEST_EQUAL(pre_psa_ret, mbedtls_md_setup(&ctx2, md_info, 0));
402
403#if defined(MBEDTLS_MD_SOME_PSA)
404 TEST_EQUAL(ctx1.engine, MBEDTLS_MD_ENGINE_LEGACY);
405 TEST_EQUAL(ctx2.engine, MBEDTLS_MD_ENGINE_LEGACY);
406#endif
407
408 /* Reset ctx1 but keep ctx2 for the cloning test */
409 mbedtls_md_free(&ctx1);
410 mbedtls_md_init(&ctx1);
411
Manuel Pégourié-Gonnardec31f292023-03-14 11:03:37 +0100412 /* Now initilize PSA Crypto */
413 MD_PSA_INIT();
414
Manuel Pégourié-Gonnard9b146392023-03-09 15:56:14 +0100415 /* After PSA Crypto init */
Manuel Pégourié-Gonnard9b146392023-03-09 15:56:14 +0100416 TEST_EQUAL(0, mbedtls_md_setup(&ctx1, md_info, 0));
417#if defined(MBEDTLS_MD_SOME_PSA)
418 TEST_EQUAL(ctx1.engine, post_psa_engine);
419#endif
420
421 /* Cloning test */
422 if (pre_psa_ret == 0) {
423 int exp_clone_ret = post_psa_engine == MBEDTLS_MD_ENGINE_PSA
424 ? MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE
425 : 0;
426 TEST_EQUAL(exp_clone_ret, mbedtls_md_clone(&ctx2, &ctx1));
427 }
428
429exit:
430 mbedtls_md_free(&ctx1);
431 mbedtls_md_free(&ctx2);
Manuel Pégourié-Gonnardec31f292023-03-14 11:03:37 +0100432 MD_PSA_DONE();
Manuel Pégourié-Gonnard9b146392023-03-09 15:56:14 +0100433}
434/* END_CASE */