2014-05-30 10:38:18 +02:00
|
|
|
/* BEGIN_HEADER */
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/entropy.h"
|
2021-03-09 20:11:19 +01:00
|
|
|
#include "entropy_poll.h"
|
2020-04-14 21:28:42 +02:00
|
|
|
#include "mbedtls/md.h"
|
2017-04-06 12:55:43 +02:00
|
|
|
#include "string.h"
|
2014-05-30 11:42:01 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
typedef enum {
|
2019-10-08 14:37:27 +02:00
|
|
|
DUMMY_CONSTANT_LENGTH, /* Output context->length bytes */
|
|
|
|
DUMMY_REQUESTED_LENGTH, /* Output whatever length was requested */
|
|
|
|
DUMMY_FAIL, /* Return an error code */
|
|
|
|
} entropy_dummy_instruction;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
typedef struct {
|
2019-10-08 14:37:27 +02:00
|
|
|
entropy_dummy_instruction instruction;
|
|
|
|
size_t length; /* Length to return for DUMMY_CONSTANT_LENGTH */
|
|
|
|
size_t calls; /* Incremented at each call */
|
|
|
|
} entropy_dummy_context;
|
2014-05-30 11:42:01 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Dummy entropy source
|
|
|
|
*
|
|
|
|
* If data is NULL, write exactly the requested length.
|
|
|
|
* Otherwise, write the length indicated by data or error if negative
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int entropy_dummy_source(void *arg, unsigned char *output,
|
|
|
|
size_t len, size_t *olen)
|
2014-05-30 11:42:01 +02:00
|
|
|
{
|
2019-10-08 14:37:27 +02:00
|
|
|
entropy_dummy_context *context = arg;
|
|
|
|
++context->calls;
|
2014-05-30 11:42:01 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (context->instruction) {
|
2019-10-08 14:37:27 +02:00
|
|
|
case DUMMY_CONSTANT_LENGTH:
|
|
|
|
*olen = context->length;
|
|
|
|
break;
|
|
|
|
case DUMMY_REQUESTED_LENGTH:
|
|
|
|
*olen = len;
|
|
|
|
break;
|
|
|
|
case DUMMY_FAIL:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
|
2014-05-30 11:42:01 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(output, 0x2a, *olen);
|
|
|
|
return 0;
|
2014-05-30 11:42:01 +02:00
|
|
|
}
|
2016-06-01 16:36:18 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Ability to clear entropy sources to allow testing with just predefined
|
|
|
|
* entropy sources. This function or tests depending on it might break if there
|
|
|
|
* are internal changes to how entropy sources are registered.
|
|
|
|
*
|
|
|
|
* To be called immediately after mbedtls_entropy_init().
|
|
|
|
*
|
|
|
|
* Just resetting the counter. New sources will overwrite existing ones.
|
|
|
|
* This might break memory checks in the future if sources need 'free-ing' then
|
|
|
|
* as well.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static void entropy_clear_sources(mbedtls_entropy_context *ctx)
|
2016-06-01 16:36:18 +02:00
|
|
|
{
|
|
|
|
ctx->source_count = 0;
|
|
|
|
}
|
|
|
|
|
2019-10-08 14:51:49 +02:00
|
|
|
#if defined(MBEDTLS_ENTROPY_NV_SEED)
|
2016-06-01 16:36:18 +02:00
|
|
|
/*
|
|
|
|
* NV seed read/write functions that use a buffer instead of a file
|
|
|
|
*/
|
|
|
|
static unsigned char buffer_seed[MBEDTLS_ENTROPY_BLOCK_SIZE];
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int buffer_nv_seed_read(unsigned char *buf, size_t buf_len)
|
2016-06-01 16:36:18 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (buf_len != MBEDTLS_ENTROPY_BLOCK_SIZE) {
|
|
|
|
return -1;
|
|
|
|
}
|
2016-06-01 16:36:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(buf, buffer_seed, MBEDTLS_ENTROPY_BLOCK_SIZE);
|
|
|
|
return 0;
|
2016-06-01 16:36:18 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int buffer_nv_seed_write(unsigned char *buf, size_t buf_len)
|
2016-06-01 16:36:18 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (buf_len != MBEDTLS_ENTROPY_BLOCK_SIZE) {
|
|
|
|
return -1;
|
|
|
|
}
|
2016-06-01 16:36:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(buffer_seed, buf, MBEDTLS_ENTROPY_BLOCK_SIZE);
|
|
|
|
return 0;
|
2016-06-01 16:36:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NV seed read/write helpers that fill the base seedfile
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int write_nv_seed(unsigned char *buf, size_t buf_len)
|
2016-06-01 16:36:18 +02:00
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (buf_len != MBEDTLS_ENTROPY_BLOCK_SIZE) {
|
|
|
|
return -1;
|
|
|
|
}
|
2016-06-01 16:36:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((f = fopen(MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w")) == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
2016-06-01 16:36:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (fwrite(buf, 1, MBEDTLS_ENTROPY_BLOCK_SIZE, f) !=
|
|
|
|
MBEDTLS_ENTROPY_BLOCK_SIZE) {
|
2023-10-17 18:08:24 +02:00
|
|
|
fclose(f);
|
2023-01-11 14:50:10 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2016-06-01 16:36:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
fclose(f);
|
2016-06-01 16:36:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2016-06-01 16:36:18 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int read_nv_seed(unsigned char *buf, size_t buf_len)
|
2016-06-01 16:36:18 +02:00
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (buf_len != MBEDTLS_ENTROPY_BLOCK_SIZE) {
|
|
|
|
return -1;
|
|
|
|
}
|
2016-06-01 16:36:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((f = fopen(MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb")) == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
2016-06-01 16:36:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (fread(buf, 1, MBEDTLS_ENTROPY_BLOCK_SIZE, f) !=
|
|
|
|
MBEDTLS_ENTROPY_BLOCK_SIZE) {
|
2023-10-17 18:08:24 +02:00
|
|
|
fclose(f);
|
2023-01-11 14:50:10 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2016-06-01 16:36:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
fclose(f);
|
2016-06-01 16:36:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2016-06-01 16:36:18 +02:00
|
|
|
}
|
2016-06-01 17:34:25 +02:00
|
|
|
#endif /* MBEDTLS_ENTROPY_NV_SEED */
|
2014-05-30 10:38:18 +02:00
|
|
|
/* END_HEADER */
|
|
|
|
|
|
|
|
/* BEGIN_DEPENDENCIES
|
2023-04-28 23:41:38 +02:00
|
|
|
* depends_on:MBEDTLS_ENTROPY_C:!MBEDTLS_PSA_INJECT_ENTROPY
|
2014-05-30 10:38:18 +02:00
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
|
|
|
|
2021-02-22 21:24:02 +01:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void entropy_init_free(int reinit)
|
2021-02-22 21:24:02 +01:00
|
|
|
{
|
|
|
|
mbedtls_entropy_context ctx;
|
|
|
|
|
|
|
|
/* Double free is not explicitly documented to work, but it is convenient
|
|
|
|
* to call mbedtls_entropy_free() unconditionally on an error path without
|
|
|
|
* checking whether it has already been called in the success path. */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_entropy_init(&ctx);
|
|
|
|
mbedtls_entropy_free(&ctx);
|
2021-02-22 21:24:02 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (reinit) {
|
|
|
|
mbedtls_entropy_init(&ctx);
|
|
|
|
}
|
|
|
|
mbedtls_entropy_free(&ctx);
|
2021-02-22 21:24:02 +01:00
|
|
|
|
|
|
|
/* This test case always succeeds, functionally speaking. A plausible
|
|
|
|
* bug might trigger an invalid pointer dereference or a memory leak. */
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2016-09-15 19:42:26 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
|
2023-01-11 14:50:10 +01:00
|
|
|
void entropy_seed_file(char *path, int ret)
|
2014-05-30 10:38:18 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_entropy_context ctx;
|
2023-07-26 18:43:39 +02:00
|
|
|
mbedtls_entropy_init(&ctx);
|
2014-05-30 10:38:18 +02:00
|
|
|
|
2023-02-02 13:14:59 +01:00
|
|
|
MD_PSA_INIT();
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_entropy_write_seed_file(&ctx, path) == ret);
|
|
|
|
TEST_ASSERT(mbedtls_entropy_update_seed_file(&ctx, path) == ret);
|
2014-05-30 10:38:18 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_entropy_free(&ctx);
|
2023-02-02 13:14:59 +01:00
|
|
|
MD_PSA_DONE();
|
2014-05-30 10:38:18 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2020-08-29 21:54:37 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
|
2023-01-11 14:50:10 +01:00
|
|
|
void entropy_write_base_seed_file(int ret)
|
2020-08-29 21:54:37 +02:00
|
|
|
{
|
|
|
|
mbedtls_entropy_context ctx;
|
2023-07-26 18:43:39 +02:00
|
|
|
mbedtls_entropy_init(&ctx);
|
2020-08-29 21:54:37 +02:00
|
|
|
|
2023-02-02 13:14:59 +01:00
|
|
|
MD_PSA_INIT();
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_entropy_write_seed_file(&ctx, MBEDTLS_PLATFORM_STD_NV_SEED_FILE) == ret);
|
|
|
|
TEST_ASSERT(mbedtls_entropy_update_seed_file(&ctx, MBEDTLS_PLATFORM_STD_NV_SEED_FILE) == ret);
|
2020-08-29 21:54:37 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_entropy_free(&ctx);
|
2023-02-02 13:14:59 +01:00
|
|
|
MD_PSA_DONE();
|
2020-08-29 21:54:37 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2019-10-08 14:51:49 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void entropy_no_sources()
|
2019-10-08 14:51:49 +02:00
|
|
|
{
|
|
|
|
mbedtls_entropy_context ctx;
|
|
|
|
unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_entropy_init(&ctx);
|
|
|
|
entropy_clear_sources(&ctx);
|
|
|
|
TEST_EQUAL(mbedtls_entropy_func(&ctx, buf, sizeof(buf)),
|
|
|
|
MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED);
|
2019-10-08 14:51:49 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_entropy_free(&ctx);
|
2019-10-08 14:51:49 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2014-05-30 11:42:01 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void entropy_too_many_sources()
|
2014-05-30 11:42:01 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_entropy_context ctx;
|
2014-05-30 11:42:01 +02:00
|
|
|
size_t i;
|
2023-01-11 14:50:10 +01:00
|
|
|
entropy_dummy_context dummy = { DUMMY_REQUESTED_LENGTH, 0, 0 };
|
2014-05-30 11:42:01 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_entropy_init(&ctx);
|
2014-05-30 11:42:01 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* It's hard to tell precisely when the error will occur,
|
|
|
|
* since we don't know how many sources were automatically added.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; i < MBEDTLS_ENTROPY_MAX_SOURCES; i++) {
|
|
|
|
(void) mbedtls_entropy_add_source(&ctx, entropy_dummy_source, &dummy,
|
|
|
|
16, MBEDTLS_ENTROPY_SOURCE_WEAK);
|
|
|
|
}
|
2014-05-30 11:42:01 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_entropy_add_source(&ctx, entropy_dummy_source, &dummy,
|
|
|
|
16, MBEDTLS_ENTROPY_SOURCE_WEAK)
|
|
|
|
== MBEDTLS_ERR_ENTROPY_MAX_SOURCES);
|
2014-05-30 11:42:01 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_entropy_free(&ctx);
|
2014-05-30 11:42:01 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2017-09-07 09:09:33 +02:00
|
|
|
/* BEGIN_CASE depends_on:ENTROPY_HAVE_STRONG */
|
2023-01-11 14:50:10 +01:00
|
|
|
void entropy_func_len(int len, int ret)
|
2014-05-30 11:42:01 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_entropy_context ctx;
|
|
|
|
unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE + 10] = { 0 };
|
|
|
|
unsigned char acc[MBEDTLS_ENTROPY_BLOCK_SIZE + 10] = { 0 };
|
2014-05-30 11:42:01 +02:00
|
|
|
size_t i, j;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_entropy_init(&ctx);
|
2014-05-30 11:42:01 +02:00
|
|
|
|
2023-07-26 18:43:39 +02:00
|
|
|
MD_PSA_INIT();
|
|
|
|
|
2014-05-30 11:42:01 +02:00
|
|
|
/*
|
2015-04-08 12:49:31 +02:00
|
|
|
* See comments in mbedtls_entropy_self_test()
|
2014-05-30 11:42:01 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; i < 8; i++) {
|
|
|
|
TEST_ASSERT(mbedtls_entropy_func(&ctx, buf, len) == ret);
|
|
|
|
for (j = 0; j < sizeof(buf); j++) {
|
2014-05-30 11:42:01 +02:00
|
|
|
acc[j] |= buf[j];
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2014-05-30 11:42:01 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == 0) {
|
|
|
|
for (j = 0; j < (size_t) len; j++) {
|
|
|
|
TEST_ASSERT(acc[j] != 0);
|
|
|
|
}
|
|
|
|
}
|
2014-05-30 11:42:01 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (j = len; j < sizeof(buf); j++) {
|
|
|
|
TEST_ASSERT(acc[j] == 0);
|
|
|
|
}
|
2021-01-31 00:07:11 +01:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_entropy_free(&ctx);
|
2023-02-02 13:14:59 +01:00
|
|
|
MD_PSA_DONE();
|
2014-05-30 11:42:01 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void entropy_source_fail(char *path)
|
2014-05-30 11:42:01 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_entropy_context ctx;
|
2014-05-30 11:42:01 +02:00
|
|
|
unsigned char buf[16];
|
2023-01-11 14:50:10 +01:00
|
|
|
entropy_dummy_context dummy = { DUMMY_FAIL, 0, 0 };
|
2014-05-30 11:42:01 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_entropy_init(&ctx);
|
2014-05-30 11:42:01 +02:00
|
|
|
|
2023-07-26 18:43:39 +02:00
|
|
|
MD_PSA_INIT();
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_entropy_add_source(&ctx, entropy_dummy_source,
|
|
|
|
&dummy, 16,
|
|
|
|
MBEDTLS_ENTROPY_SOURCE_WEAK)
|
|
|
|
== 0);
|
2014-05-30 11:42:01 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_entropy_func(&ctx, buf, sizeof(buf))
|
|
|
|
== MBEDTLS_ERR_ENTROPY_SOURCE_FAILED);
|
|
|
|
TEST_ASSERT(mbedtls_entropy_gather(&ctx)
|
|
|
|
== MBEDTLS_ERR_ENTROPY_SOURCE_FAILED);
|
2016-09-15 19:42:26 +02:00
|
|
|
#if defined(MBEDTLS_FS_IO) && defined(MBEDTLS_ENTROPY_NV_SEED)
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_entropy_write_seed_file(&ctx, path)
|
|
|
|
== MBEDTLS_ERR_ENTROPY_SOURCE_FAILED);
|
|
|
|
TEST_ASSERT(mbedtls_entropy_update_seed_file(&ctx, path)
|
|
|
|
== MBEDTLS_ERR_ENTROPY_SOURCE_FAILED);
|
2014-05-30 11:42:01 +02:00
|
|
|
#else
|
|
|
|
((void) path);
|
|
|
|
#endif
|
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_entropy_free(&ctx);
|
2023-02-02 13:14:59 +01:00
|
|
|
MD_PSA_DONE();
|
2014-05-30 11:42:01 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2019-11-25 19:50:54 +01:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void entropy_threshold(int threshold, int chunk_size, int result)
|
2014-05-30 11:42:01 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_entropy_context ctx;
|
2019-11-25 19:50:54 +01:00
|
|
|
entropy_dummy_context strong =
|
2023-01-11 14:50:10 +01:00
|
|
|
{ DUMMY_CONSTANT_LENGTH, MBEDTLS_ENTROPY_BLOCK_SIZE, 0 };
|
|
|
|
entropy_dummy_context weak = { DUMMY_CONSTANT_LENGTH, chunk_size, 0 };
|
2015-04-08 12:49:31 +02:00
|
|
|
unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
|
2014-05-30 11:42:01 +02:00
|
|
|
int ret;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_entropy_init(&ctx);
|
|
|
|
entropy_clear_sources(&ctx);
|
2014-05-30 11:42:01 +02:00
|
|
|
|
2023-07-26 18:43:39 +02:00
|
|
|
MD_PSA_INIT();
|
|
|
|
|
2019-11-25 19:50:54 +01:00
|
|
|
/* Set strong source that reaches its threshold immediately and
|
|
|
|
* a weak source whose threshold is a test parameter. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_entropy_add_source(&ctx, entropy_dummy_source,
|
|
|
|
&strong, 1,
|
|
|
|
MBEDTLS_ENTROPY_SOURCE_STRONG) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_entropy_add_source(&ctx, entropy_dummy_source,
|
|
|
|
&weak, threshold,
|
|
|
|
MBEDTLS_ENTROPY_SOURCE_WEAK) == 0);
|
|
|
|
|
|
|
|
ret = mbedtls_entropy_func(&ctx, buf, sizeof(buf));
|
|
|
|
|
|
|
|
if (result >= 0) {
|
|
|
|
TEST_ASSERT(ret == 0);
|
2016-06-01 16:36:18 +02:00
|
|
|
#if defined(MBEDTLS_ENTROPY_NV_SEED)
|
2019-11-25 18:26:23 +01:00
|
|
|
/* If the NV seed functionality is enabled, there are two entropy
|
|
|
|
* updates: before and after updating the NV seed. */
|
2016-06-01 16:36:18 +02:00
|
|
|
result *= 2;
|
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(weak.calls == (size_t) result);
|
|
|
|
} else {
|
|
|
|
TEST_ASSERT(ret == result);
|
2019-10-08 15:01:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_entropy_free(&ctx);
|
2023-02-02 13:14:59 +01:00
|
|
|
MD_PSA_DONE();
|
2019-10-08 15:01:34 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void entropy_calls(int strength1, int strength2,
|
|
|
|
int threshold, int chunk_size,
|
|
|
|
int result)
|
2019-10-08 15:01:34 +02:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* if result >= 0: result = expected number of calls to source 1
|
|
|
|
* if result < 0: result = expected return code from mbedtls_entropy_func()
|
|
|
|
*/
|
|
|
|
|
|
|
|
mbedtls_entropy_context ctx;
|
2023-01-11 14:50:10 +01:00
|
|
|
entropy_dummy_context dummy1 = { DUMMY_CONSTANT_LENGTH, chunk_size, 0 };
|
|
|
|
entropy_dummy_context dummy2 = { DUMMY_CONSTANT_LENGTH, chunk_size, 0 };
|
2019-10-08 15:01:34 +02:00
|
|
|
unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
|
|
|
|
int ret;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_entropy_init(&ctx);
|
|
|
|
entropy_clear_sources(&ctx);
|
2019-10-08 15:01:34 +02:00
|
|
|
|
2023-07-26 18:43:39 +02:00
|
|
|
MD_PSA_INIT();
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_entropy_add_source(&ctx, entropy_dummy_source,
|
|
|
|
&dummy1, threshold,
|
|
|
|
strength1) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_entropy_add_source(&ctx, entropy_dummy_source,
|
|
|
|
&dummy2, threshold,
|
|
|
|
strength2) == 0);
|
2019-10-08 15:01:34 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_entropy_func(&ctx, buf, sizeof(buf));
|
2019-10-08 15:01:34 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (result >= 0) {
|
|
|
|
TEST_ASSERT(ret == 0);
|
2019-11-25 18:26:23 +01:00
|
|
|
#if defined(MBEDTLS_ENTROPY_NV_SEED)
|
|
|
|
/* If the NV seed functionality is enabled, there are two entropy
|
|
|
|
* updates: before and after updating the NV seed. */
|
|
|
|
result *= 2;
|
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(dummy1.calls == (size_t) result);
|
|
|
|
} else {
|
|
|
|
TEST_ASSERT(ret == result);
|
2014-05-30 11:42:01 +02:00
|
|
|
}
|
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_entropy_free(&ctx);
|
2023-02-02 13:14:59 +01:00
|
|
|
MD_PSA_DONE();
|
2014-05-30 11:42:01 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2016-06-01 16:36:18 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
|
2023-01-11 14:50:10 +01:00
|
|
|
void nv_seed_file_create()
|
2016-06-01 16:36:18 +02:00
|
|
|
{
|
|
|
|
unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
|
2016-06-01 16:36:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(write_nv_seed(buf, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0);
|
2016-06-01 16:36:18 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2016-06-01 17:57:11 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO:MBEDTLS_PLATFORM_NV_SEED_ALT */
|
2023-01-11 14:50:10 +01:00
|
|
|
void entropy_nv_seed_std_io()
|
2016-06-01 16:36:18 +02:00
|
|
|
{
|
|
|
|
unsigned char io_seed[MBEDTLS_ENTROPY_BLOCK_SIZE];
|
|
|
|
unsigned char check_seed[MBEDTLS_ENTROPY_BLOCK_SIZE];
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(io_seed, 1, MBEDTLS_ENTROPY_BLOCK_SIZE);
|
|
|
|
memset(check_seed, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
|
2016-06-01 16:36:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_set_nv_seed(mbedtls_platform_std_nv_seed_read,
|
|
|
|
mbedtls_platform_std_nv_seed_write);
|
2016-06-01 16:36:18 +02:00
|
|
|
|
|
|
|
/* Check if platform NV read and write manipulate the same data */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(write_nv_seed(io_seed, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_nv_seed_read(check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE) ==
|
|
|
|
MBEDTLS_ENTROPY_BLOCK_SIZE);
|
2016-06-01 16:36:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(memcmp(io_seed, check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0);
|
2016-06-01 16:36:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(check_seed, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
|
2016-06-01 16:36:18 +02:00
|
|
|
|
|
|
|
/* Check if platform NV write and raw read manipulate the same data */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_nv_seed_write(io_seed, MBEDTLS_ENTROPY_BLOCK_SIZE) ==
|
|
|
|
MBEDTLS_ENTROPY_BLOCK_SIZE);
|
|
|
|
TEST_ASSERT(read_nv_seed(check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0);
|
2016-06-01 16:36:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(memcmp(io_seed, check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0);
|
2016-06-01 16:36:18 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2019-06-12 19:33:42 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_MD_C:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_PLATFORM_NV_SEED_ALT */
|
2023-01-11 14:50:10 +01:00
|
|
|
void entropy_nv_seed(data_t *read_seed)
|
2016-06-01 16:36:18 +02:00
|
|
|
{
|
2019-06-12 19:33:42 +02:00
|
|
|
#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
|
|
|
|
const mbedtls_md_info_t *md_info =
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_md_info_from_type(MBEDTLS_MD_SHA512);
|
2019-06-12 19:33:42 +02:00
|
|
|
#elif defined(MBEDTLS_ENTROPY_SHA256_ACCUMULATOR)
|
|
|
|
const mbedtls_md_info_t *md_info =
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
|
2019-06-12 19:33:42 +02:00
|
|
|
#else
|
|
|
|
#error "Unsupported entropy accumulator"
|
|
|
|
#endif
|
|
|
|
mbedtls_md_context_t accumulator;
|
2016-06-01 16:36:18 +02:00
|
|
|
mbedtls_entropy_context ctx;
|
2023-01-11 14:50:10 +01:00
|
|
|
int (*original_mbedtls_nv_seed_read)(unsigned char *buf, size_t buf_len) =
|
2019-06-12 19:31:29 +02:00
|
|
|
mbedtls_nv_seed_read;
|
2023-01-11 14:50:10 +01:00
|
|
|
int (*original_mbedtls_nv_seed_write)(unsigned char *buf, size_t buf_len) =
|
2019-06-12 19:31:29 +02:00
|
|
|
mbedtls_nv_seed_write;
|
2016-06-01 16:36:18 +02:00
|
|
|
|
|
|
|
unsigned char header[2];
|
|
|
|
unsigned char entropy[MBEDTLS_ENTROPY_BLOCK_SIZE];
|
|
|
|
unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
|
|
|
|
unsigned char empty[MBEDTLS_ENTROPY_BLOCK_SIZE];
|
|
|
|
unsigned char check_seed[MBEDTLS_ENTROPY_BLOCK_SIZE];
|
|
|
|
unsigned char check_entropy[MBEDTLS_ENTROPY_BLOCK_SIZE];
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(entropy, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
|
|
|
|
memset(buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
|
|
|
|
memset(empty, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
|
|
|
|
memset(check_seed, 2, MBEDTLS_ENTROPY_BLOCK_SIZE);
|
|
|
|
memset(check_entropy, 3, MBEDTLS_ENTROPY_BLOCK_SIZE);
|
2016-06-01 16:36:18 +02:00
|
|
|
|
|
|
|
// Make sure we read/write NV seed from our buffers
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_set_nv_seed(buffer_nv_seed_read, buffer_nv_seed_write);
|
2016-06-01 16:36:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_md_init(&accumulator);
|
|
|
|
mbedtls_entropy_init(&ctx);
|
|
|
|
entropy_clear_sources(&ctx);
|
2016-06-01 16:36:18 +02:00
|
|
|
|
2023-07-26 18:43:39 +02:00
|
|
|
MD_PSA_INIT();
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_entropy_add_source(&ctx, mbedtls_nv_seed_poll, NULL,
|
|
|
|
MBEDTLS_ENTROPY_BLOCK_SIZE,
|
|
|
|
MBEDTLS_ENTROPY_SOURCE_STRONG) == 0);
|
2016-06-01 16:36:18 +02:00
|
|
|
|
2019-06-12 19:33:42 +02:00
|
|
|
// Set the initial NV seed to read
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(read_seed->len >= MBEDTLS_ENTROPY_BLOCK_SIZE);
|
|
|
|
memcpy(buffer_seed, read_seed->x, MBEDTLS_ENTROPY_BLOCK_SIZE);
|
2019-06-12 19:33:42 +02:00
|
|
|
|
2016-06-01 16:36:18 +02:00
|
|
|
// Do an entropy run
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_entropy_func(&ctx, entropy, sizeof(entropy)) == 0);
|
2016-06-01 16:36:18 +02:00
|
|
|
// Determine what should have happened with manual entropy internal logic
|
|
|
|
|
|
|
|
// Init accumulator
|
|
|
|
header[1] = MBEDTLS_ENTROPY_BLOCK_SIZE;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_md_setup(&accumulator, md_info, 0) == 0);
|
2016-06-01 16:36:18 +02:00
|
|
|
|
|
|
|
// First run for updating write_seed
|
|
|
|
header[0] = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_md_starts(&accumulator) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_md_update(&accumulator, header, 2) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_md_update(&accumulator,
|
|
|
|
read_seed->x, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_md_finish(&accumulator, buf) == 0);
|
2016-06-01 16:36:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_md_starts(&accumulator) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_md_update(&accumulator,
|
|
|
|
buf, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0);
|
2016-06-01 16:36:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_md(md_info, buf, MBEDTLS_ENTROPY_BLOCK_SIZE,
|
|
|
|
check_seed) == 0);
|
2016-06-01 16:36:18 +02:00
|
|
|
|
|
|
|
// Second run for actual entropy (triggers mbedtls_entropy_update_nv_seed)
|
|
|
|
header[0] = MBEDTLS_ENTROPY_SOURCE_MANUAL;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_md_update(&accumulator, header, 2) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_md_update(&accumulator,
|
|
|
|
empty, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0);
|
2016-06-01 16:36:18 +02:00
|
|
|
|
|
|
|
header[0] = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_md_update(&accumulator, header, 2) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_md_update(&accumulator,
|
|
|
|
check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_md_finish(&accumulator, buf) == 0);
|
2016-06-01 16:36:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_md(md_info, buf, MBEDTLS_ENTROPY_BLOCK_SIZE,
|
|
|
|
check_entropy) == 0);
|
2016-06-01 16:36:18 +02:00
|
|
|
|
|
|
|
// Check result of both NV file and entropy received with the manual calculations
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(memcmp(check_seed, buffer_seed, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0);
|
|
|
|
TEST_ASSERT(memcmp(check_entropy, entropy, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0);
|
2016-06-01 16:36:18 +02:00
|
|
|
|
2019-06-12 19:31:29 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_md_free(&accumulator);
|
|
|
|
mbedtls_entropy_free(&ctx);
|
2019-06-12 19:31:29 +02:00
|
|
|
mbedtls_nv_seed_read = original_mbedtls_nv_seed_read;
|
|
|
|
mbedtls_nv_seed_write = original_mbedtls_nv_seed_write;
|
2023-02-02 13:14:59 +01:00
|
|
|
MD_PSA_DONE();
|
2016-06-01 16:36:18 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2017-09-07 09:09:33 +02:00
|
|
|
/* BEGIN_CASE depends_on:ENTROPY_HAVE_STRONG:MBEDTLS_SELF_TEST */
|
2023-01-11 14:50:10 +01:00
|
|
|
void entropy_selftest(int result)
|
2014-05-30 10:38:18 +02:00
|
|
|
{
|
2023-02-02 13:14:59 +01:00
|
|
|
MD_PSA_INIT();
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_entropy_self_test(1) == result);
|
2023-02-02 13:14:59 +01:00
|
|
|
|
|
|
|
exit:
|
|
|
|
MD_PSA_DONE();
|
2014-05-30 10:38:18 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|