2018-11-20 20:56:14 +01:00
|
|
|
/* BEGIN_HEADER */
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2018-11-20 21:59:56 +01:00
|
|
|
/* Some tests in this module configure entropy sources. */
|
|
|
|
#include "psa_crypto_invasive.h"
|
|
|
|
|
|
|
|
#include "mbedtls/entropy.h"
|
2021-03-09 20:11:19 +01:00
|
|
|
#include "entropy_poll.h"
|
2018-11-20 21:59:56 +01:00
|
|
|
|
2018-11-20 23:21:01 +01:00
|
|
|
#define ENTROPY_MIN_NV_SEED_SIZE \
|
|
|
|
MAX(MBEDTLS_ENTROPY_MIN_PLATFORM, MBEDTLS_ENTROPY_BLOCK_SIZE)
|
2018-11-20 22:41:50 +01:00
|
|
|
|
2020-12-14 16:43:58 +01:00
|
|
|
#include "psa_crypto_random_impl.h"
|
2020-11-13 21:31:17 +01:00
|
|
|
#if defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
|
|
|
|
/* PSA crypto uses the HMAC_DRBG module. It reads from the entropy source twice:
|
|
|
|
* once for the initial entropy and once for a nonce. The nonce length is
|
|
|
|
* half the entropy length. For SHA-256, SHA-384 or SHA-512, the
|
|
|
|
* entropy length is 256 per the documentation of mbedtls_hmac_drbg_seed(),
|
|
|
|
* and PSA crypto doesn't support other hashes for HMAC_DRBG. */
|
2023-01-11 14:50:10 +01:00
|
|
|
#define ENTROPY_NONCE_LEN (256 / 2)
|
2020-11-13 21:31:17 +01:00
|
|
|
#else
|
2019-10-23 19:39:36 +02:00
|
|
|
/* PSA crypto uses the CTR_DRBG module. In some configurations, it needs
|
|
|
|
* to read from the entropy source twice: once for the initial entropy
|
|
|
|
* and once for a nonce. */
|
|
|
|
#include "mbedtls/ctr_drbg.h"
|
|
|
|
#define ENTROPY_NONCE_LEN MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN
|
2020-11-13 21:31:17 +01:00
|
|
|
#endif
|
2019-10-23 19:39:36 +02:00
|
|
|
|
2020-11-13 18:47:18 +01:00
|
|
|
#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
typedef struct {
|
2018-11-20 22:41:50 +01:00
|
|
|
size_t threshold; /* Minimum bytes to make mbedtls_entropy_func happy */
|
|
|
|
size_t max_steps;
|
|
|
|
size_t *length_sequence;
|
|
|
|
size_t step;
|
|
|
|
} fake_entropy_state_t;
|
2023-01-11 14:50:10 +01:00
|
|
|
static int fake_entropy_source(void *state_arg,
|
|
|
|
unsigned char *output, size_t len,
|
|
|
|
size_t *olen)
|
2018-11-20 22:41:50 +01:00
|
|
|
{
|
|
|
|
fake_entropy_state_t *state = state_arg;
|
|
|
|
size_t i;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (state->step >= state->max_steps) {
|
|
|
|
return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
|
|
|
|
}
|
2018-11-20 22:41:50 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
*olen = MIN(len, state->length_sequence[state->step]);
|
|
|
|
for (i = 0; i < *olen; i++) {
|
2018-11-20 22:41:50 +01:00
|
|
|
output[i] = i;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2018-11-20 22:41:50 +01:00
|
|
|
++state->step;
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2018-12-04 12:25:40 +01:00
|
|
|
}
|
2018-11-20 22:41:50 +01:00
|
|
|
|
2018-11-20 21:59:56 +01:00
|
|
|
#define ENTROPY_SOURCE_PLATFORM 0x00000001
|
|
|
|
#define ENTROPY_SOURCE_TIMING 0x00000002
|
2021-01-29 16:46:31 +01:00
|
|
|
#define ENTROPY_SOURCE_HARDWARE 0x00000004
|
|
|
|
#define ENTROPY_SOURCE_NV_SEED 0x00000008
|
2018-11-20 22:41:50 +01:00
|
|
|
#define ENTROPY_SOURCE_FAKE 0x40000000
|
|
|
|
|
2018-11-20 21:59:56 +01:00
|
|
|
static uint32_t custom_entropy_sources_mask;
|
2018-11-20 22:41:50 +01:00
|
|
|
static fake_entropy_state_t fake_entropy_state;
|
2018-11-20 21:59:56 +01:00
|
|
|
|
|
|
|
/* This is a modified version of mbedtls_entropy_init() from entropy.c
|
|
|
|
* which chooses entropy sources dynamically. */
|
2023-01-11 14:50:10 +01:00
|
|
|
static void custom_entropy_init(mbedtls_entropy_context *ctx)
|
2018-11-20 21:59:56 +01:00
|
|
|
{
|
|
|
|
ctx->source_count = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(ctx->source, 0, sizeof(ctx->source));
|
2018-11-20 21:59:56 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mutex_init(&ctx->mutex);
|
2018-11-20 21:59:56 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
ctx->accumulator_started = 0;
|
2023-02-02 13:14:59 +01:00
|
|
|
mbedtls_md_init(&ctx->accumulator);
|
2018-11-20 21:59:56 +01:00
|
|
|
|
|
|
|
#if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (custom_entropy_sources_mask & ENTROPY_SOURCE_PLATFORM) {
|
|
|
|
mbedtls_entropy_add_source(ctx, mbedtls_platform_entropy_poll, NULL,
|
|
|
|
MBEDTLS_ENTROPY_MIN_PLATFORM,
|
|
|
|
MBEDTLS_ENTROPY_SOURCE_STRONG);
|
|
|
|
}
|
2018-11-20 21:59:56 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (custom_entropy_sources_mask & ENTROPY_SOURCE_HARDWARE) {
|
|
|
|
mbedtls_entropy_add_source(ctx, mbedtls_hardware_poll, NULL,
|
|
|
|
MBEDTLS_ENTROPY_MIN_HARDWARE,
|
|
|
|
MBEDTLS_ENTROPY_SOURCE_STRONG);
|
|
|
|
}
|
2018-11-20 21:59:56 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_ENTROPY_NV_SEED)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (custom_entropy_sources_mask & ENTROPY_SOURCE_NV_SEED) {
|
|
|
|
mbedtls_entropy_add_source(ctx, mbedtls_nv_seed_poll, NULL,
|
|
|
|
MBEDTLS_ENTROPY_BLOCK_SIZE,
|
|
|
|
MBEDTLS_ENTROPY_SOURCE_STRONG);
|
2018-11-20 23:09:54 +01:00
|
|
|
ctx->initial_entropy_run = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2018-11-20 23:09:54 +01:00
|
|
|
/* Skip the NV seed even though it's compiled in. */
|
|
|
|
ctx->initial_entropy_run = 1;
|
|
|
|
}
|
2018-11-20 21:59:56 +01:00
|
|
|
#endif
|
2018-11-20 22:41:50 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (custom_entropy_sources_mask & ENTROPY_SOURCE_FAKE) {
|
|
|
|
mbedtls_entropy_add_source(ctx,
|
|
|
|
fake_entropy_source, &fake_entropy_state,
|
|
|
|
fake_entropy_state.threshold,
|
|
|
|
MBEDTLS_ENTROPY_SOURCE_STRONG);
|
|
|
|
}
|
2018-11-20 21:59:56 +01:00
|
|
|
}
|
|
|
|
|
2020-11-13 18:47:18 +01:00
|
|
|
#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
|
|
|
|
|
2018-11-20 20:56:14 +01:00
|
|
|
/* END_HEADER */
|
|
|
|
|
|
|
|
/* BEGIN_DEPENDENCIES
|
|
|
|
* depends_on:MBEDTLS_PSA_CRYPTO_C
|
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
|
|
|
|
2020-11-13 18:47:18 +01:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:!MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
2023-01-11 14:50:10 +01:00
|
|
|
void create_nv_seed()
|
2018-11-20 23:21:37 +01:00
|
|
|
{
|
|
|
|
static unsigned char seed[ENTROPY_MIN_NV_SEED_SIZE];
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_nv_seed_write(seed, sizeof(seed)) >= 0);
|
2018-11-20 23:21:37 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2018-11-20 20:56:14 +01:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void init_deinit(int count)
|
2018-11-20 20:56:14 +01:00
|
|
|
{
|
|
|
|
psa_status_t status;
|
|
|
|
int i;
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
status = psa_crypto_init();
|
|
|
|
PSA_ASSERT(status);
|
|
|
|
status = psa_crypto_init();
|
|
|
|
PSA_ASSERT(status);
|
|
|
|
PSA_DONE();
|
2018-11-20 20:56:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2018-11-20 21:42:52 +01:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void deinit_without_init(int count)
|
2018-11-20 21:42:52 +01:00
|
|
|
{
|
|
|
|
int i;
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
PSA_ASSERT(psa_crypto_init());
|
|
|
|
PSA_DONE();
|
2018-11-20 21:42:52 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_DONE();
|
2018-11-20 21:42:52 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2018-11-20 20:56:14 +01:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void validate_module_init_generate_random(int count)
|
2018-11-20 20:56:14 +01:00
|
|
|
{
|
|
|
|
psa_status_t status;
|
|
|
|
uint8_t random[10] = { 0 };
|
2018-11-20 21:00:42 +01:00
|
|
|
int i;
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
status = psa_crypto_init();
|
|
|
|
PSA_ASSERT(status);
|
|
|
|
PSA_DONE();
|
2018-11-20 21:00:42 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_generate_random(random, sizeof(random));
|
|
|
|
TEST_EQUAL(status, PSA_ERROR_BAD_STATE);
|
2018-11-20 20:56:14 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void validate_module_init_key_based(int count)
|
2018-11-20 20:56:14 +01:00
|
|
|
{
|
|
|
|
psa_status_t status;
|
|
|
|
uint8_t data[10] = { 0 };
|
2019-04-19 19:58:20 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_svc_key_id_t key = mbedtls_svc_key_id_make(0xdead, 0xdead);
|
2018-11-20 21:00:42 +01:00
|
|
|
int i;
|
2019-04-19 19:58:20 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
status = psa_crypto_init();
|
|
|
|
PSA_ASSERT(status);
|
|
|
|
PSA_DONE();
|
2018-11-20 21:00:42 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_set_key_type(&attributes, PSA_KEY_TYPE_RAW_DATA);
|
|
|
|
status = psa_import_key(&attributes, data, sizeof(data), &key);
|
|
|
|
TEST_EQUAL(status, PSA_ERROR_BAD_STATE);
|
|
|
|
TEST_ASSERT(mbedtls_svc_key_id_is_null(key));
|
2018-11-20 20:56:14 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2018-11-20 21:59:56 +01:00
|
|
|
|
2020-11-13 18:47:18 +01:00
|
|
|
/* BEGIN_CASE depends_on:!MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
2023-01-11 14:50:10 +01:00
|
|
|
void custom_entropy_sources(int sources_arg, int expected_init_status_arg)
|
2018-11-20 21:59:56 +01:00
|
|
|
{
|
|
|
|
psa_status_t expected_init_status = expected_init_status_arg;
|
|
|
|
uint8_t random[10] = { 0 };
|
|
|
|
|
|
|
|
custom_entropy_sources_mask = sources_arg;
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(mbedtls_psa_crypto_configure_entropy_sources(
|
|
|
|
custom_entropy_init, mbedtls_entropy_free));
|
2018-11-20 21:59:56 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(psa_crypto_init(), expected_init_status);
|
|
|
|
if (expected_init_status != PSA_SUCCESS) {
|
2018-11-20 21:59:56 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2018-11-20 21:59:56 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_generate_random(random, sizeof(random)));
|
2018-11-20 21:59:56 +01:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_DONE();
|
2018-11-20 22:41:50 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2020-11-13 18:47:18 +01:00
|
|
|
/* BEGIN_CASE depends_on:!MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
2023-01-11 14:50:10 +01:00
|
|
|
void fake_entropy_source(int threshold,
|
|
|
|
int amount1,
|
|
|
|
int amount2,
|
|
|
|
int amount3,
|
|
|
|
int amount4,
|
|
|
|
int expected_init_status_arg)
|
2018-11-20 22:41:50 +01:00
|
|
|
{
|
|
|
|
psa_status_t expected_init_status = expected_init_status_arg;
|
|
|
|
uint8_t random[10] = { 0 };
|
|
|
|
size_t lengths[4];
|
|
|
|
|
|
|
|
fake_entropy_state.threshold = threshold;
|
|
|
|
fake_entropy_state.step = 0;
|
|
|
|
fake_entropy_state.max_steps = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (amount1 >= 0) {
|
2018-11-20 22:41:50 +01:00
|
|
|
lengths[fake_entropy_state.max_steps++] = amount1;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
if (amount2 >= 0) {
|
2018-11-20 22:41:50 +01:00
|
|
|
lengths[fake_entropy_state.max_steps++] = amount2;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
if (amount3 >= 0) {
|
2018-11-20 22:41:50 +01:00
|
|
|
lengths[fake_entropy_state.max_steps++] = amount3;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
if (amount4 >= 0) {
|
2018-11-20 22:41:50 +01:00
|
|
|
lengths[fake_entropy_state.max_steps++] = amount4;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2018-11-20 22:41:50 +01:00
|
|
|
fake_entropy_state.length_sequence = lengths;
|
|
|
|
|
|
|
|
custom_entropy_sources_mask = ENTROPY_SOURCE_FAKE;
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(mbedtls_psa_crypto_configure_entropy_sources(
|
|
|
|
custom_entropy_init, mbedtls_entropy_free));
|
2018-11-20 22:41:50 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(psa_crypto_init(), expected_init_status);
|
|
|
|
if (expected_init_status != PSA_SUCCESS) {
|
2018-11-20 22:41:50 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2018-11-20 22:41:50 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_generate_random(random, sizeof(random)));
|
2018-11-20 22:41:50 +01:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_DONE();
|
2018-11-20 21:59:56 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2018-11-20 23:21:01 +01:00
|
|
|
|
2020-11-13 18:47:18 +01:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:!MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
2023-01-11 14:50:10 +01:00
|
|
|
void entropy_from_nv_seed(int seed_size_arg,
|
|
|
|
int expected_init_status_arg)
|
2018-11-20 23:21:01 +01:00
|
|
|
{
|
|
|
|
psa_status_t expected_init_status = expected_init_status_arg;
|
|
|
|
uint8_t random[10] = { 0 };
|
|
|
|
uint8_t *seed = NULL;
|
|
|
|
size_t seed_size = seed_size_arg;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_ALLOC(seed, seed_size);
|
|
|
|
TEST_ASSERT(mbedtls_nv_seed_write(seed, seed_size) >= 0);
|
2018-11-20 23:21:01 +01:00
|
|
|
|
|
|
|
custom_entropy_sources_mask = ENTROPY_SOURCE_NV_SEED;
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(mbedtls_psa_crypto_configure_entropy_sources(
|
|
|
|
custom_entropy_init, mbedtls_entropy_free));
|
2018-11-20 23:21:01 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(psa_crypto_init(), expected_init_status);
|
|
|
|
if (expected_init_status != PSA_SUCCESS) {
|
2018-11-20 23:21:01 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2018-11-20 23:21:01 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_generate_random(random, sizeof(random)));
|
2018-11-20 23:21:01 +01:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(seed);
|
|
|
|
PSA_DONE();
|
2018-11-20 23:21:01 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|