2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_HEADER */
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/md.h"
|
2023-03-14 11:03:37 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_MD_SOME_PSA)
|
|
|
|
#define MD_PSA_INIT() PSA_INIT()
|
|
|
|
#define MD_PSA_DONE() PSA_DONE()
|
|
|
|
#else /* MBEDTLS_MD_SOME_PSA */
|
|
|
|
#define MD_PSA_INIT() ((void) 0)
|
|
|
|
#define MD_PSA_DONE() ((void) 0)
|
|
|
|
#endif /* MBEDTLS_MD_SOME_PSA */
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_HEADER */
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_DEPENDENCIES
|
2023-02-16 19:07:31 +01:00
|
|
|
* depends_on:MBEDTLS_MD_LIGHT
|
2013-08-20 11:48:36 +02:00
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
2011-05-26 15:16:06 +02:00
|
|
|
|
2023-02-16 19:07:31 +01:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_MD_C */
|
2023-02-16 18:44:46 +01:00
|
|
|
void mbedtls_md_list()
|
2014-03-29 15:54:50 +01:00
|
|
|
{
|
|
|
|
const int *md_type_ptr;
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_md_info_t *info;
|
|
|
|
mbedtls_md_context_t ctx;
|
2023-02-16 18:44:46 +01:00
|
|
|
unsigned char out[MBEDTLS_MD_MAX_SIZE] = { 0 };
|
2014-04-02 17:52:04 +02:00
|
|
|
|
2023-03-14 11:03:37 +01:00
|
|
|
MD_PSA_INIT();
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_md_init(&ctx);
|
2014-04-02 17:52:04 +02:00
|
|
|
|
|
|
|
/*
|
2023-02-16 18:44:46 +01:00
|
|
|
* Test that mbedtls_md_list() only returns valid MDs.
|
2014-04-02 17:52:04 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
for (md_type_ptr = mbedtls_md_list(); *md_type_ptr != 0; md_type_ptr++) {
|
|
|
|
info = mbedtls_md_info_from_type(*md_type_ptr);
|
|
|
|
TEST_ASSERT(info != NULL);
|
2023-02-03 12:51:03 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_md_setup(&ctx, info, 0));
|
|
|
|
TEST_EQUAL(0, mbedtls_md_starts(&ctx));
|
2023-02-16 18:44:46 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_md_finish(&ctx, out));
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_md_free(&ctx);
|
2014-04-02 17:52:04 +02:00
|
|
|
}
|
2014-07-10 15:26:12 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_md_free(&ctx);
|
2023-03-14 11:03:37 +01:00
|
|
|
MD_PSA_DONE();
|
2014-03-29 15:54:50 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2014-06-13 16:34:30 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void md_null_args()
|
2014-06-13 16:34:30 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_md_context_t ctx;
|
2023-02-16 19:07:31 +01:00
|
|
|
#if defined(MBEDTLS_MD_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
const mbedtls_md_info_t *info = mbedtls_md_info_from_type(*(mbedtls_md_list()));
|
2023-02-16 19:07:31 +01:00
|
|
|
#endif
|
2014-06-13 16:34:30 +02:00
|
|
|
unsigned char buf[1] = { 0 };
|
|
|
|
|
2023-03-14 11:03:37 +01:00
|
|
|
MD_PSA_INIT();
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_md_init(&ctx);
|
2014-06-13 16:34:30 +02:00
|
|
|
|
2023-02-03 12:51:03 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_md_get_size(NULL));
|
2023-02-16 19:07:31 +01:00
|
|
|
#if defined(MBEDTLS_MD_C)
|
2023-02-09 09:18:22 +01:00
|
|
|
TEST_EQUAL(mbedtls_md_get_type(NULL), MBEDTLS_MD_NONE);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_md_get_name(NULL) == NULL);
|
2014-06-13 16:34:30 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_md_info_from_string(NULL) == NULL);
|
|
|
|
TEST_ASSERT(mbedtls_md_info_from_ctx(NULL) == NULL);
|
|
|
|
TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx) == NULL);
|
2023-02-16 19:07:31 +01:00
|
|
|
#endif /* MBEDTLS_MD_C */
|
2014-06-13 16:34:30 +02:00
|
|
|
|
2023-02-03 12:51:03 +01:00
|
|
|
TEST_EQUAL(mbedtls_md_setup(&ctx, NULL, 0), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
|
2023-02-16 19:07:31 +01:00
|
|
|
#if defined(MBEDTLS_MD_C)
|
2023-02-03 12:51:03 +01:00
|
|
|
TEST_EQUAL(mbedtls_md_setup(NULL, info, 0), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
|
2023-02-16 19:07:31 +01:00
|
|
|
#endif
|
2014-06-13 16:34:30 +02:00
|
|
|
|
2023-02-03 12:51:03 +01:00
|
|
|
TEST_EQUAL(mbedtls_md_starts(NULL), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
|
|
|
|
TEST_EQUAL(mbedtls_md_starts(&ctx), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
|
2014-06-13 16:34:30 +02:00
|
|
|
|
2023-02-03 12:51:03 +01:00
|
|
|
TEST_EQUAL(mbedtls_md_update(NULL, buf, 1), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
|
|
|
|
TEST_EQUAL(mbedtls_md_update(&ctx, buf, 1), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
|
2014-06-13 16:34:30 +02:00
|
|
|
|
2023-02-03 12:51:03 +01:00
|
|
|
TEST_EQUAL(mbedtls_md_finish(NULL, buf), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
|
|
|
|
TEST_EQUAL(mbedtls_md_finish(&ctx, buf), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
|
2014-06-13 16:34:30 +02:00
|
|
|
|
2023-02-03 12:51:03 +01:00
|
|
|
TEST_EQUAL(mbedtls_md(NULL, buf, 1, buf), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
|
2014-06-13 16:34:30 +02:00
|
|
|
|
2023-02-16 19:07:31 +01:00
|
|
|
#if defined(MBEDTLS_MD_C)
|
2015-05-28 14:44:00 +02:00
|
|
|
#if defined(MBEDTLS_FS_IO)
|
2023-02-03 12:51:03 +01:00
|
|
|
TEST_EQUAL(mbedtls_md_file(NULL, "", buf), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
|
2015-05-28 14:44:00 +02:00
|
|
|
#endif
|
2014-06-13 16:34:30 +02:00
|
|
|
|
2023-02-03 12:51:03 +01:00
|
|
|
TEST_EQUAL(mbedtls_md_hmac_starts(NULL, buf, 1),
|
|
|
|
MBEDTLS_ERR_MD_BAD_INPUT_DATA);
|
|
|
|
TEST_EQUAL(mbedtls_md_hmac_starts(&ctx, buf, 1),
|
|
|
|
MBEDTLS_ERR_MD_BAD_INPUT_DATA);
|
2014-06-13 16:34:30 +02:00
|
|
|
|
2023-02-03 12:51:03 +01:00
|
|
|
TEST_EQUAL(mbedtls_md_hmac_update(NULL, buf, 1),
|
|
|
|
MBEDTLS_ERR_MD_BAD_INPUT_DATA);
|
|
|
|
TEST_EQUAL(mbedtls_md_hmac_update(&ctx, buf, 1),
|
|
|
|
MBEDTLS_ERR_MD_BAD_INPUT_DATA);
|
2014-06-13 16:34:30 +02:00
|
|
|
|
2023-02-03 12:51:03 +01:00
|
|
|
TEST_EQUAL(mbedtls_md_hmac_finish(NULL, buf), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
|
|
|
|
TEST_EQUAL(mbedtls_md_hmac_finish(&ctx, buf), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
|
2014-06-13 16:34:30 +02:00
|
|
|
|
2023-02-03 12:51:03 +01:00
|
|
|
TEST_EQUAL(mbedtls_md_hmac_reset(NULL), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
|
|
|
|
TEST_EQUAL(mbedtls_md_hmac_reset(&ctx), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
|
2014-06-13 16:34:30 +02:00
|
|
|
|
2023-02-03 12:51:03 +01:00
|
|
|
TEST_EQUAL(mbedtls_md_hmac(NULL, buf, 1, buf, 1, buf),
|
|
|
|
MBEDTLS_ERR_MD_BAD_INPUT_DATA);
|
2023-02-16 19:07:31 +01:00
|
|
|
#endif /* MBEDTLS_MD_C */
|
2014-06-13 16:34:30 +02:00
|
|
|
|
2015-03-26 12:42:35 +01:00
|
|
|
/* Ok, this is not NULL arg but NULL return... */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_md_info_from_type(MBEDTLS_MD_NONE) == NULL);
|
2023-02-16 19:07:31 +01:00
|
|
|
#if defined(MBEDTLS_MD_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_md_info_from_string("no such md") == NULL);
|
2023-02-16 19:07:31 +01:00
|
|
|
#endif
|
2023-03-14 11:03:37 +01:00
|
|
|
|
|
|
|
exit:
|
|
|
|
MD_PSA_DONE();
|
2014-06-13 16:34:30 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2014-03-29 15:54:50 +01:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void md_info(int md_type, char *md_name, int md_size)
|
2014-03-29 15:54:50 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_md_info_t *md_info;
|
2023-02-16 19:07:31 +01:00
|
|
|
#if defined(MBEDTLS_MD_C)
|
2014-03-29 15:54:50 +01:00
|
|
|
const int *md_type_ptr;
|
2023-02-16 19:07:31 +01:00
|
|
|
#else
|
|
|
|
(void) md_name;
|
|
|
|
#endif
|
2014-03-29 15:54:50 +01:00
|
|
|
|
2023-03-14 11:03:37 +01:00
|
|
|
/* Note: PSA Crypto init not needed to info functions */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
md_info = mbedtls_md_info_from_type(md_type);
|
|
|
|
TEST_ASSERT(md_info != NULL);
|
2023-02-16 19:07:31 +01:00
|
|
|
#if defined(MBEDTLS_MD_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(md_info == mbedtls_md_info_from_string(md_name));
|
2023-02-16 19:07:31 +01:00
|
|
|
#endif
|
2014-03-29 15:54:50 +01:00
|
|
|
|
2023-02-03 12:51:03 +01:00
|
|
|
TEST_EQUAL(mbedtls_md_get_type(md_info), (mbedtls_md_type_t) md_type);
|
|
|
|
TEST_EQUAL(mbedtls_md_get_size(md_info), (unsigned char) md_size);
|
2023-02-16 19:07:31 +01:00
|
|
|
#if defined(MBEDTLS_MD_C)
|
2023-02-03 12:51:03 +01:00
|
|
|
TEST_EQUAL(0, strcmp(mbedtls_md_get_name(md_info), md_name));
|
2014-03-29 15:54:50 +01:00
|
|
|
|
2023-02-16 19:07:31 +01:00
|
|
|
int found = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
for (md_type_ptr = mbedtls_md_list(); *md_type_ptr != 0; md_type_ptr++) {
|
|
|
|
if (*md_type_ptr == md_type) {
|
2014-03-29 15:54:50 +01:00
|
|
|
found = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
}
|
2023-02-03 12:51:03 +01:00
|
|
|
TEST_EQUAL(found, 1);
|
2023-02-16 19:07:31 +01:00
|
|
|
#endif /* MBEDTLS_MD_C */
|
2014-03-29 15:54:50 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-02-03 12:13:10 +01:00
|
|
|
void md_text(int md_type, char *text_src_string, data_t *hash)
|
2011-01-06 15:20:01 +01:00
|
|
|
{
|
2023-02-03 12:32:41 +01:00
|
|
|
unsigned char *src = (unsigned char *) text_src_string;
|
|
|
|
size_t src_len = strlen(text_src_string);
|
2023-02-06 12:37:02 +01:00
|
|
|
unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 };
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_md_info_t *md_info = NULL;
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2023-03-14 11:03:37 +01:00
|
|
|
MD_PSA_INIT();
|
|
|
|
|
2023-02-03 12:13:10 +01:00
|
|
|
md_info = mbedtls_md_info_from_type(md_type);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(md_info != NULL);
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2023-02-03 12:51:03 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_md(md_info, src, src_len, output));
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2023-02-09 09:15:04 +01:00
|
|
|
ASSERT_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len);
|
2023-03-14 11:03:37 +01:00
|
|
|
|
|
|
|
exit:
|
|
|
|
MD_PSA_DONE();
|
2011-01-06 15:20:01 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-02-03 12:13:10 +01:00
|
|
|
void md_hex(int md_type, data_t *src_str, data_t *hash)
|
2011-01-06 15:20:01 +01:00
|
|
|
{
|
2023-02-06 12:37:02 +01:00
|
|
|
unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 };
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_md_info_t *md_info = NULL;
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2023-03-14 11:03:37 +01:00
|
|
|
MD_PSA_INIT();
|
|
|
|
|
2023-02-03 12:13:10 +01:00
|
|
|
md_info = mbedtls_md_info_from_type(md_type);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(md_info != NULL);
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2023-02-03 12:51:03 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_md(md_info, src_str->x, src_str->len, output));
|
2011-01-06 15:20:01 +01:00
|
|
|
|
|
|
|
|
2023-02-09 09:15:04 +01:00
|
|
|
ASSERT_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len);
|
2023-03-14 11:03:37 +01:00
|
|
|
|
|
|
|
exit:
|
|
|
|
MD_PSA_DONE();
|
2011-01-06 15:20:01 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-02-03 12:13:10 +01:00
|
|
|
void md_text_multi(int md_type, char *text_src_string,
|
2023-01-11 14:50:10 +01:00
|
|
|
data_t *hash)
|
2011-01-06 15:20:01 +01:00
|
|
|
{
|
2023-02-03 12:32:41 +01:00
|
|
|
unsigned char *src = (unsigned char *) text_src_string;
|
|
|
|
size_t src_len = strlen(text_src_string);
|
2023-02-06 12:37:02 +01:00
|
|
|
unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 };
|
2023-02-03 12:32:41 +01:00
|
|
|
size_t halfway;
|
2014-07-01 15:45:49 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_md_info_t *md_info = NULL;
|
2016-07-13 18:20:22 +02:00
|
|
|
mbedtls_md_context_t ctx, ctx_copy;
|
2014-07-01 15:45:49 +02:00
|
|
|
|
2023-03-14 11:03:37 +01:00
|
|
|
MD_PSA_INIT();
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_md_init(&ctx);
|
|
|
|
mbedtls_md_init(&ctx_copy);
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2023-02-03 12:32:41 +01:00
|
|
|
halfway = src_len / 2;
|
2016-07-13 18:09:14 +02:00
|
|
|
|
2023-02-03 12:13:10 +01:00
|
|
|
md_info = mbedtls_md_info_from_type(md_type);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(md_info != NULL);
|
2023-02-03 12:51:03 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_md_setup(&ctx, md_info, 0));
|
|
|
|
TEST_EQUAL(0, mbedtls_md_setup(&ctx_copy, md_info, 0));
|
2023-02-16 19:07:31 +01:00
|
|
|
#if defined(MBEDTLS_MD_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx) == md_info);
|
|
|
|
TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx_copy) == md_info);
|
2023-02-16 19:07:31 +01:00
|
|
|
#endif /* MBEDTLS_MD_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
2023-02-03 12:51:03 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_md_starts(&ctx));
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(ctx.md_ctx != NULL);
|
2023-02-03 12:51:03 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_md_update(&ctx, src, halfway));
|
|
|
|
TEST_EQUAL(0, mbedtls_md_clone(&ctx_copy, &ctx));
|
2023-01-11 14:50:10 +01:00
|
|
|
|
2023-02-03 12:51:03 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_md_update(&ctx, src + halfway, src_len - halfway));
|
|
|
|
TEST_EQUAL(0, mbedtls_md_finish(&ctx, output));
|
2023-02-09 09:15:04 +01:00
|
|
|
ASSERT_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len);
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2016-07-13 18:20:22 +02:00
|
|
|
/* Test clone */
|
2023-02-03 12:25:53 +01:00
|
|
|
memset(output, 0x00, sizeof(output));
|
2016-07-13 18:20:22 +02:00
|
|
|
|
2023-02-03 12:51:03 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_md_update(&ctx_copy, src + halfway, src_len - halfway));
|
|
|
|
TEST_EQUAL(0, mbedtls_md_finish(&ctx_copy, output));
|
2023-02-09 09:15:04 +01:00
|
|
|
ASSERT_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len);
|
2014-07-10 15:26:12 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_md_free(&ctx);
|
|
|
|
mbedtls_md_free(&ctx_copy);
|
2023-03-14 11:03:37 +01:00
|
|
|
MD_PSA_DONE();
|
2011-01-06 15:20:01 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-02-03 12:13:10 +01:00
|
|
|
void md_hex_multi(int md_type, data_t *src_str, data_t *hash)
|
2011-01-06 15:20:01 +01:00
|
|
|
{
|
2023-02-06 12:37:02 +01:00
|
|
|
unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 };
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_md_info_t *md_info = NULL;
|
2016-07-13 18:20:22 +02:00
|
|
|
mbedtls_md_context_t ctx, ctx_copy;
|
2017-05-30 15:23:15 +02:00
|
|
|
int halfway;
|
2014-07-01 15:45:49 +02:00
|
|
|
|
2023-03-14 11:03:37 +01:00
|
|
|
MD_PSA_INIT();
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_md_init(&ctx);
|
|
|
|
mbedtls_md_init(&ctx_copy);
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2023-02-03 12:13:10 +01:00
|
|
|
md_info = mbedtls_md_info_from_type(md_type);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(md_info != NULL);
|
2023-02-03 12:51:03 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_md_setup(&ctx, md_info, 0));
|
|
|
|
TEST_EQUAL(0, mbedtls_md_setup(&ctx_copy, md_info, 0));
|
2023-02-16 19:07:31 +01:00
|
|
|
#if defined(MBEDTLS_MD_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx) == md_info);
|
|
|
|
TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx_copy) == md_info);
|
2023-02-16 19:07:31 +01:00
|
|
|
#endif /* MBEDTLS_MD_C */
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
halfway = src_str->len / 2;
|
2014-07-01 15:45:49 +02:00
|
|
|
|
2023-02-03 12:51:03 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_md_starts(&ctx));
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(ctx.md_ctx != NULL);
|
2023-02-03 12:51:03 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_md_update(&ctx, src_str->x, halfway));
|
|
|
|
TEST_EQUAL(0, mbedtls_md_clone(&ctx_copy, &ctx));
|
2016-07-13 18:20:22 +02:00
|
|
|
|
2023-02-03 12:51:03 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_md_update(&ctx, src_str->x + halfway, src_str->len - halfway));
|
|
|
|
TEST_EQUAL(0, mbedtls_md_finish(&ctx, output));
|
2023-02-09 09:15:04 +01:00
|
|
|
ASSERT_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len);
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2016-07-13 18:20:22 +02:00
|
|
|
/* Test clone */
|
2023-02-03 12:25:53 +01:00
|
|
|
memset(output, 0x00, sizeof(output));
|
2016-07-13 18:20:22 +02:00
|
|
|
|
2023-02-03 12:51:03 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_md_update(&ctx_copy, src_str->x + halfway, src_str->len - halfway));
|
|
|
|
TEST_EQUAL(0, mbedtls_md_finish(&ctx_copy, output));
|
2023-02-09 09:15:04 +01:00
|
|
|
ASSERT_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len);
|
2014-07-10 15:26:12 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_md_free(&ctx);
|
|
|
|
mbedtls_md_free(&ctx_copy);
|
2023-03-14 11:03:37 +01:00
|
|
|
MD_PSA_DONE();
|
2011-01-06 15:20:01 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2023-02-16 19:07:31 +01:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_MD_C */
|
2023-02-03 12:13:10 +01:00
|
|
|
void mbedtls_md_hmac(int md_type, int trunc_size,
|
2023-01-11 14:50:10 +01:00
|
|
|
data_t *key_str, data_t *src_str,
|
|
|
|
data_t *hash)
|
2011-01-06 15:20:01 +01:00
|
|
|
{
|
2023-02-06 12:37:02 +01:00
|
|
|
unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 };
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_md_info_t *md_info = NULL;
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2023-03-14 11:03:37 +01:00
|
|
|
MD_PSA_INIT();
|
|
|
|
|
2023-02-03 12:13:10 +01:00
|
|
|
md_info = mbedtls_md_info_from_type(md_type);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(md_info != NULL);
|
2011-01-06 15:20:01 +01:00
|
|
|
|
|
|
|
|
2023-02-03 12:51:03 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_md_hmac(md_info, key_str->x, key_str->len,
|
2023-02-06 12:37:02 +01:00
|
|
|
src_str->x, src_str->len, output));
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2023-02-09 09:15:04 +01:00
|
|
|
ASSERT_COMPARE(output, trunc_size, hash->x, hash->len);
|
2023-03-14 11:03:37 +01:00
|
|
|
|
|
|
|
exit:
|
|
|
|
MD_PSA_DONE();
|
2011-01-06 15:20:01 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2023-02-16 19:07:31 +01:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_MD_C */
|
2023-02-03 12:13:10 +01:00
|
|
|
void md_hmac_multi(int md_type, int trunc_size, data_t *key_str,
|
2023-01-11 14:50:10 +01:00
|
|
|
data_t *src_str, data_t *hash)
|
2011-01-06 15:20:01 +01:00
|
|
|
{
|
2023-02-06 12:37:02 +01:00
|
|
|
unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 };
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_md_info_t *md_info = NULL;
|
|
|
|
mbedtls_md_context_t ctx;
|
2017-05-30 15:23:15 +02:00
|
|
|
int halfway;
|
2014-07-01 15:45:49 +02:00
|
|
|
|
2023-03-14 11:03:37 +01:00
|
|
|
MD_PSA_INIT();
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_md_init(&ctx);
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2023-02-03 12:13:10 +01:00
|
|
|
md_info = mbedtls_md_info_from_type(md_type);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(md_info != NULL);
|
2023-02-03 12:51:03 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_md_setup(&ctx, md_info, 1));
|
2023-02-16 19:07:31 +01:00
|
|
|
#if defined(MBEDTLS_MD_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx) == md_info);
|
2023-02-16 19:07:31 +01:00
|
|
|
#endif
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
halfway = src_str->len / 2;
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2023-02-03 12:51:03 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_md_hmac_starts(&ctx, key_str->x, key_str->len));
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(ctx.md_ctx != NULL);
|
2023-02-03 12:51:03 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_md_hmac_update(&ctx, src_str->x, halfway));
|
|
|
|
TEST_EQUAL(0, mbedtls_md_hmac_update(&ctx, src_str->x + halfway, src_str->len - halfway));
|
|
|
|
TEST_EQUAL(0, mbedtls_md_hmac_finish(&ctx, output));
|
2013-08-20 11:48:36 +02:00
|
|
|
|
2023-02-09 09:15:04 +01:00
|
|
|
ASSERT_COMPARE(output, trunc_size, hash->x, hash->len);
|
2014-03-29 14:43:44 +01:00
|
|
|
|
|
|
|
/* Test again, for reset() */
|
2023-02-03 12:25:53 +01:00
|
|
|
memset(output, 0x00, sizeof(output));
|
2014-03-29 14:43:44 +01:00
|
|
|
|
2023-02-03 12:51:03 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_md_hmac_reset(&ctx));
|
|
|
|
TEST_EQUAL(0, mbedtls_md_hmac_update(&ctx, src_str->x, halfway));
|
|
|
|
TEST_EQUAL(0, mbedtls_md_hmac_update(&ctx, src_str->x + halfway, src_str->len - halfway));
|
|
|
|
TEST_EQUAL(0, mbedtls_md_hmac_finish(&ctx, output));
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2023-02-09 09:15:04 +01:00
|
|
|
ASSERT_COMPARE(output, trunc_size, hash->x, hash->len);
|
2014-07-10 15:26:12 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_md_free(&ctx);
|
2023-03-14 11:03:37 +01:00
|
|
|
MD_PSA_DONE();
|
2011-01-06 15:20:01 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2013-09-15 15:20:37 +02:00
|
|
|
|
2023-02-16 19:07:31 +01:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_MD_C */
|
2023-02-03 12:13:10 +01:00
|
|
|
void mbedtls_md_file(int md_type, char *filename,
|
2023-01-11 14:50:10 +01:00
|
|
|
data_t *hash)
|
2011-01-06 15:20:01 +01:00
|
|
|
{
|
2023-02-06 12:37:02 +01:00
|
|
|
unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 };
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_md_info_t *md_info = NULL;
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2023-03-14 11:03:37 +01:00
|
|
|
MD_PSA_INIT();
|
|
|
|
|
2023-02-03 12:13:10 +01:00
|
|
|
md_info = mbedtls_md_info_from_type(md_type);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(md_info != NULL);
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2023-02-03 12:51:03 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_md_file(md_info, filename, output));
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2023-02-09 09:15:04 +01:00
|
|
|
ASSERT_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len);
|
2023-03-14 11:03:37 +01:00
|
|
|
|
|
|
|
exit:
|
|
|
|
MD_PSA_DONE();
|
2011-01-06 15:20:01 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2023-03-09 15:56:14 +01:00
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void md_psa_dynamic_dispatch(int md_type, int pre_psa_ret, int post_psa_engine)
|
|
|
|
{
|
|
|
|
const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_type);
|
|
|
|
TEST_ASSERT(md_info != NULL);
|
|
|
|
mbedtls_md_context_t ctx1, ctx2;
|
|
|
|
|
2023-03-14 11:03:37 +01:00
|
|
|
/* Intentionally no PSA init here! (Will be done later.) */
|
|
|
|
|
2023-03-09 15:56:14 +01:00
|
|
|
mbedtls_md_init(&ctx1);
|
|
|
|
mbedtls_md_init(&ctx2);
|
|
|
|
|
|
|
|
/* Before PSA crypto init */
|
|
|
|
TEST_EQUAL(pre_psa_ret, mbedtls_md_setup(&ctx1, md_info, 0));
|
|
|
|
TEST_EQUAL(pre_psa_ret, mbedtls_md_setup(&ctx2, md_info, 0));
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_MD_SOME_PSA)
|
|
|
|
TEST_EQUAL(ctx1.engine, MBEDTLS_MD_ENGINE_LEGACY);
|
|
|
|
TEST_EQUAL(ctx2.engine, MBEDTLS_MD_ENGINE_LEGACY);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Reset ctx1 but keep ctx2 for the cloning test */
|
|
|
|
mbedtls_md_free(&ctx1);
|
|
|
|
mbedtls_md_init(&ctx1);
|
|
|
|
|
2023-03-14 11:03:37 +01:00
|
|
|
/* Now initilize PSA Crypto */
|
|
|
|
MD_PSA_INIT();
|
|
|
|
|
2023-03-09 15:56:14 +01:00
|
|
|
/* After PSA Crypto init */
|
|
|
|
TEST_EQUAL(0, mbedtls_md_setup(&ctx1, md_info, 0));
|
|
|
|
#if defined(MBEDTLS_MD_SOME_PSA)
|
|
|
|
TEST_EQUAL(ctx1.engine, post_psa_engine);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Cloning test */
|
|
|
|
if (pre_psa_ret == 0) {
|
|
|
|
int exp_clone_ret = post_psa_engine == MBEDTLS_MD_ENGINE_PSA
|
|
|
|
? MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE
|
|
|
|
: 0;
|
|
|
|
TEST_EQUAL(exp_clone_ret, mbedtls_md_clone(&ctx2, &ctx1));
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_md_free(&ctx1);
|
|
|
|
mbedtls_md_free(&ctx2);
|
2023-03-14 11:03:37 +01:00
|
|
|
MD_PSA_DONE();
|
2023-03-09 15:56:14 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|