2018-11-19 10:53:55 +01:00
|
|
|
/* BEGIN_HEADER */
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include "mbedtls/entropy.h"
|
|
|
|
#include "mbedtls/entropy_poll.h"
|
|
|
|
|
2020-05-27 09:22:32 +02:00
|
|
|
#include "test/psa_crypto_helpers.h"
|
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
|
|
|
|
|
2018-11-20 15:51:49 +01:00
|
|
|
/* Calculating the minimum allowed entropy size in bytes */
|
|
|
|
#define MBEDTLS_PSA_INJECT_ENTROPY_MIN_SIZE MAX(MBEDTLS_ENTROPY_MIN_PLATFORM, MBEDTLS_ENTROPY_BLOCK_SIZE)
|
|
|
|
|
2019-02-25 11:04:06 +01:00
|
|
|
/* 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. */
|
|
|
|
psa_status_t remove_seed_file( void )
|
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_PSA_ITS_FILE_C)
|
|
|
|
if( remove( "00000000ffffff52.psa_its" ) == 0 )
|
|
|
|
return( PSA_SUCCESS );
|
|
|
|
else
|
|
|
|
return( PSA_ERROR_DOES_NOT_EXIST );
|
|
|
|
#else
|
|
|
|
return( psa_its_remove( PSA_CRYPTO_ITS_RANDOM_SEED_UID ) );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-11-19 10:53:55 +01:00
|
|
|
/* END_HEADER */
|
|
|
|
|
|
|
|
/* BEGIN_DEPENDENCIES
|
2019-02-25 11:04:06 +01:00
|
|
|
* depends_on:MBEDTLS_PSA_INJECT_ENTROPY
|
2018-11-19 10:53:55 +01:00
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void validate_entropy_seed_injection( int seed_length_a,
|
|
|
|
int expected_status_a,
|
|
|
|
int seed_length_b,
|
|
|
|
int expected_status_b )
|
|
|
|
{
|
|
|
|
psa_status_t status;
|
|
|
|
uint8_t output[32] = { 0 };
|
|
|
|
uint8_t zeros[32] = { 0 };
|
|
|
|
uint8_t *seed = NULL;
|
|
|
|
int i;
|
|
|
|
int seed_size;
|
2018-11-20 15:51:49 +01:00
|
|
|
if( seed_length_a > seed_length_b )
|
2018-11-19 10:53:55 +01:00
|
|
|
{
|
|
|
|
seed_size = seed_length_a;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
seed_size = seed_length_b;
|
|
|
|
}
|
|
|
|
ASSERT_ALLOC( seed, seed_size );
|
2018-11-20 15:51:49 +01:00
|
|
|
/* fill seed with some data */
|
|
|
|
for( i = 0; i < seed_size; ++i )
|
2018-11-19 10:53:55 +01:00
|
|
|
{
|
|
|
|
seed[i] = i;
|
|
|
|
}
|
2019-02-25 11:04:06 +01:00
|
|
|
status = remove_seed_file( );
|
2019-02-18 12:56:26 +01:00
|
|
|
TEST_ASSERT( ( status == PSA_SUCCESS ) ||
|
|
|
|
( status == PSA_ERROR_DOES_NOT_EXIST ) );
|
2018-11-19 10:53:55 +01:00
|
|
|
status = mbedtls_psa_inject_entropy( seed, seed_length_a );
|
2018-12-18 00:24:04 +01:00
|
|
|
TEST_EQUAL( status, expected_status_a );
|
2018-11-19 10:53:55 +01:00
|
|
|
status = mbedtls_psa_inject_entropy( seed, seed_length_b );
|
2018-12-18 00:24:04 +01:00
|
|
|
TEST_EQUAL( status, expected_status_b );
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
PSA_ASSERT( psa_generate_random( output,
|
|
|
|
sizeof( output ) ) );
|
2018-11-20 15:51:49 +01:00
|
|
|
TEST_ASSERT( memcmp( output, zeros, sizeof( output ) ) != 0 );
|
2018-11-19 10:53:55 +01:00
|
|
|
exit:
|
|
|
|
mbedtls_free( seed );
|
2019-02-25 11:04:06 +01:00
|
|
|
remove_seed_file( );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-11-19 10:53:55 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void run_entropy_inject_with_crypto_init( )
|
|
|
|
{
|
|
|
|
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 */
|
|
|
|
for( i = 0; i < sizeof( seed ); ++i )
|
2018-11-19 10:53:55 +01:00
|
|
|
{
|
|
|
|
seed[i] = i;
|
|
|
|
}
|
2019-02-25 11:04:06 +01:00
|
|
|
status = remove_seed_file( );
|
2019-02-18 12:56:26 +01:00
|
|
|
TEST_ASSERT( ( status == PSA_SUCCESS ) ||
|
|
|
|
( status == PSA_ERROR_DOES_NOT_EXIST ) );
|
2018-11-20 15:51:49 +01:00
|
|
|
status = mbedtls_psa_inject_entropy( seed, sizeof( seed ) );
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( status );
|
2019-02-25 11:04:06 +01:00
|
|
|
status = remove_seed_file( );
|
2019-02-18 12:56:26 +01:00
|
|
|
TEST_EQUAL( status, PSA_SUCCESS );
|
2018-11-20 15:51:49 +01:00
|
|
|
status = psa_crypto_init( );
|
2018-12-18 00:24:04 +01:00
|
|
|
TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_ENTROPY );
|
2018-11-20 15:51:49 +01:00
|
|
|
status = mbedtls_psa_inject_entropy( seed, sizeof( seed ) );
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( status );
|
2018-11-20 15:51:49 +01:00
|
|
|
status = psa_crypto_init( );
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( status );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-11-19 10:53:55 +01:00
|
|
|
/* The seed is written by nv_seed callback functions therefore the injection will fail */
|
2018-11-20 15:51:49 +01:00
|
|
|
status = mbedtls_psa_inject_entropy( seed, sizeof( seed ) );
|
2018-12-18 00:24:04 +01:00
|
|
|
TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
|
2018-11-19 10:53:55 +01:00
|
|
|
exit:
|
2019-02-25 11:04:06 +01:00
|
|
|
remove_seed_file( );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-11-19 10:53:55 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|