2018-11-19 10:53:55 +01:00
|
|
|
/* BEGIN_HEADER */
|
|
|
|
#include <stdint.h>
|
2020-11-25 00:16:10 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <psa/crypto.h>
|
2018-11-19 10:53:55 +01:00
|
|
|
|
|
|
|
#include "mbedtls/entropy.h"
|
2021-03-09 20:11:19 +01:00
|
|
|
#include "entropy_poll.h"
|
2018-11-19 10:53:55 +01:00
|
|
|
|
2020-11-24 18:44:58 +01:00
|
|
|
/* Calculating the minimum allowed entropy size in bytes */
|
2023-01-11 14:50:10 +01:00
|
|
|
#define MBEDTLS_PSA_INJECT_ENTROPY_MIN_SIZE MAX(MBEDTLS_ENTROPY_MIN_PLATFORM, \
|
|
|
|
MBEDTLS_ENTROPY_BLOCK_SIZE)
|
2020-11-24 18:44:58 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
|
|
|
|
|
2019-02-25 11:04:06 +01:00
|
|
|
#if defined(MBEDTLS_PSA_ITS_FILE_C)
|
|
|
|
#include <stdio.h>
|
|
|
|
#else
|
|
|
|
#include <psa/internal_trusted_storage.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Remove the entropy seed file. Since the library does not expose a way
|
|
|
|
* to do this (it would be a security risk if such a function was ever
|
|
|
|
* accessible in production), implement this functionality in a white-box
|
|
|
|
* manner. */
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_status_t remove_seed_file(void)
|
2019-02-25 11:04:06 +01:00
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_PSA_ITS_FILE_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (remove("00000000ffffff52.psa_its") == 0) {
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
} else {
|
|
|
|
return PSA_ERROR_DOES_NOT_EXIST;
|
|
|
|
}
|
2019-02-25 11:04:06 +01:00
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
return psa_its_remove(PSA_CRYPTO_ITS_RANDOM_SEED_UID);
|
2019-02-25 11:04:06 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-11-24 18:44:58 +01:00
|
|
|
#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
|
2018-11-19 10:53:55 +01:00
|
|
|
|
2020-11-24 18:44:58 +01:00
|
|
|
/* END_HEADER */
|
2018-11-19 10:53:55 +01:00
|
|
|
|
2020-11-25 00:16:10 +01:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
2023-01-11 14:50:10 +01:00
|
|
|
void external_rng_failure_generate()
|
2020-11-25 00:16:10 +01:00
|
|
|
{
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
|
|
|
|
psa_set_key_bits(&attributes, 128);
|
2020-11-25 00:16:10 +01:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
uint8_t output[1];
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_crypto_init());
|
2020-11-25 00:16:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_generate_random(output, sizeof(output)));
|
|
|
|
PSA_ASSERT(psa_generate_key(&attributes, &key));
|
|
|
|
PSA_ASSERT(psa_destroy_key(key));
|
2020-11-25 00:16:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_test_disable_insecure_external_rng();
|
|
|
|
TEST_EQUAL(PSA_ERROR_INSUFFICIENT_ENTROPY,
|
|
|
|
psa_generate_random(output, sizeof(output)));
|
|
|
|
TEST_EQUAL(PSA_ERROR_INSUFFICIENT_ENTROPY,
|
|
|
|
psa_generate_key(&attributes, &key));
|
2020-11-25 00:16:10 +01:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_destroy_key(key);
|
|
|
|
PSA_DONE();
|
2020-11-25 00:16:10 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
2023-01-11 14:50:10 +01:00
|
|
|
void external_rng_failure_sign(int key_type, data_t *key_data, int alg,
|
|
|
|
int input_size_arg)
|
2020-11-25 00:16:10 +01:00
|
|
|
{
|
|
|
|
/* This test case is only expected to pass if the signature mechanism
|
|
|
|
* requires randomness, either because it is a randomized signature
|
|
|
|
* or because the implementation uses blinding. */
|
|
|
|
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_set_key_type(&attributes, key_type);
|
|
|
|
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
|
|
|
|
psa_set_key_algorithm(&attributes, alg);
|
2020-11-25 00:16:10 +01:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
size_t input_size = input_size_arg;
|
2021-01-06 20:16:26 +01:00
|
|
|
uint8_t *input = NULL;
|
|
|
|
uint8_t *signature = NULL;
|
|
|
|
size_t signature_size = PSA_SIGNATURE_MAX_SIZE;
|
2020-11-25 00:16:10 +01:00
|
|
|
size_t signature_length;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_ALLOC(input, input_size);
|
|
|
|
ASSERT_ALLOC(signature, signature_size);
|
2020-11-25 00:16:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_crypto_init());
|
|
|
|
PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
|
|
|
|
&key));
|
|
|
|
PSA_ASSERT(psa_sign_hash(key, alg,
|
|
|
|
input, input_size,
|
|
|
|
signature, signature_size,
|
|
|
|
&signature_length));
|
|
|
|
PSA_ASSERT(psa_destroy_key(key));
|
2020-11-25 00:16:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_test_disable_insecure_external_rng();
|
2020-11-25 00:16:10 +01:00
|
|
|
/* Import the key again, because for RSA Mbed TLS caches blinding values
|
|
|
|
* in the key object and this could perturb the test. */
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
|
|
|
|
&key));
|
|
|
|
TEST_EQUAL(PSA_ERROR_INSUFFICIENT_ENTROPY,
|
|
|
|
psa_sign_hash(key, alg,
|
|
|
|
input, input_size,
|
|
|
|
signature, signature_size,
|
|
|
|
&signature_length));
|
|
|
|
PSA_ASSERT(psa_destroy_key(key));
|
2020-11-25 00:16:10 +01:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_destroy_key(key);
|
|
|
|
PSA_DONE();
|
|
|
|
mbedtls_free(input);
|
|
|
|
mbedtls_free(signature);
|
2020-11-25 00:16:10 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2020-11-24 18:44:58 +01:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_PSA_INJECT_ENTROPY */
|
2023-01-11 14:50:10 +01:00
|
|
|
void validate_entropy_seed_injection(int seed_length_a,
|
|
|
|
int expected_status_a,
|
|
|
|
int seed_length_b,
|
|
|
|
int expected_status_b)
|
2018-11-19 10:53:55 +01:00
|
|
|
{
|
|
|
|
psa_status_t status;
|
|
|
|
uint8_t output[32] = { 0 };
|
|
|
|
uint8_t zeros[32] = { 0 };
|
|
|
|
uint8_t *seed = NULL;
|
|
|
|
int i;
|
|
|
|
int seed_size;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (seed_length_a > seed_length_b) {
|
2018-11-19 10:53:55 +01:00
|
|
|
seed_size = seed_length_a;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2018-11-19 10:53:55 +01:00
|
|
|
seed_size = seed_length_b;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_ALLOC(seed, seed_size);
|
2018-11-20 15:51:49 +01:00
|
|
|
/* fill seed with some data */
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; i < seed_size; ++i) {
|
2018-11-19 10:53:55 +01:00
|
|
|
seed[i] = i;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
status = remove_seed_file();
|
|
|
|
TEST_ASSERT((status == PSA_SUCCESS) ||
|
|
|
|
(status == PSA_ERROR_DOES_NOT_EXIST));
|
|
|
|
status = mbedtls_psa_inject_entropy(seed, seed_length_a);
|
|
|
|
TEST_EQUAL(status, expected_status_a);
|
|
|
|
status = mbedtls_psa_inject_entropy(seed, seed_length_b);
|
|
|
|
TEST_EQUAL(status, expected_status_b);
|
|
|
|
PSA_ASSERT(psa_crypto_init());
|
|
|
|
PSA_ASSERT(psa_generate_random(output,
|
|
|
|
sizeof(output)));
|
|
|
|
TEST_ASSERT(memcmp(output, zeros, sizeof(output)) != 0);
|
2018-11-19 10:53:55 +01:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(seed);
|
|
|
|
remove_seed_file();
|
|
|
|
PSA_DONE();
|
2018-11-19 10:53:55 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2020-11-24 18:44:58 +01:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_PSA_INJECT_ENTROPY */
|
2023-01-11 14:50:10 +01:00
|
|
|
void run_entropy_inject_with_crypto_init()
|
2018-11-19 10:53:55 +01:00
|
|
|
{
|
|
|
|
psa_status_t status;
|
2019-02-25 11:04:06 +01:00
|
|
|
size_t i;
|
2018-11-20 15:51:49 +01:00
|
|
|
uint8_t seed[MBEDTLS_PSA_INJECT_ENTROPY_MIN_SIZE] = { 0 };
|
|
|
|
/* fill seed with some data */
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; i < sizeof(seed); ++i) {
|
2018-11-19 10:53:55 +01:00
|
|
|
seed[i] = i;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
status = remove_seed_file();
|
|
|
|
TEST_ASSERT((status == PSA_SUCCESS) ||
|
|
|
|
(status == PSA_ERROR_DOES_NOT_EXIST));
|
|
|
|
status = mbedtls_psa_inject_entropy(seed, sizeof(seed));
|
|
|
|
PSA_ASSERT(status);
|
|
|
|
status = remove_seed_file();
|
|
|
|
TEST_EQUAL(status, PSA_SUCCESS);
|
|
|
|
status = psa_crypto_init();
|
|
|
|
TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_ENTROPY);
|
|
|
|
status = mbedtls_psa_inject_entropy(seed, sizeof(seed));
|
|
|
|
PSA_ASSERT(status);
|
|
|
|
status = psa_crypto_init();
|
|
|
|
PSA_ASSERT(status);
|
|
|
|
PSA_DONE();
|
2018-11-19 10:53:55 +01:00
|
|
|
/* The seed is written by nv_seed callback functions therefore the injection will fail */
|
2023-01-11 14:50:10 +01:00
|
|
|
status = mbedtls_psa_inject_entropy(seed, sizeof(seed));
|
|
|
|
TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
|
2018-11-19 10:53:55 +01:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
remove_seed_file();
|
|
|
|
PSA_DONE();
|
2018-11-19 10:53:55 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|