Unify RNG initialization that must be unified

mpi_core_random_basic and mpi_random_values must generate the same random
sequences in order to get the expected test coverage (where we know we'll
hit certain numbers of retries). Facilitate this by defining the RNG seed
only once.

Fix the seed to explicitly list all 16 words of the key. This isn't strictly
required (missing initializer fields get the value zero), but it's clearer.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This commit is contained in:
Gilles Peskine 2022-12-15 15:10:36 +01:00
parent 6b7ce968d2
commit acdefdd51a

View File

@ -16,6 +16,15 @@ static int sign_is_valid( const mbedtls_mpi *X )
return( X->s == 1 );
}
/* A common initializer for test functions that should generate the same
* sequences for reproducibility and good coverage. */
const mbedtls_test_rnd_pseudo_info rnd_pseudo_seed = {
/* 16-word key */
{'T', 'h', 'i', 's', ' ', 'i', 's', ' ',
'a', ' ', 's', 'e', 'e', 'd', '!', 0},
/* 2-word initial state, should be zero */
0, 0};
/* Test whether bytes represents (in big-endian base 256) a number b that
* is significantly above a power of 2. That is, b must not have a long run
* of unset bits after the most significant bit.
@ -71,10 +80,7 @@ static int is_significantly_above_a_power_of_2( data_t *bytes )
void mpi_core_random_basic( int min, char *bound_bytes, int expected_ret )
{
/* Same RNG as in mpi_random_values */
mbedtls_test_rnd_pseudo_info rnd = {
{'T', 'h', 'i', 's', ' ', 'i', ',', 'a',
's', 'e', 'e', 'd', '!', 0},
0, 0};
mbedtls_test_rnd_pseudo_info rnd = rnd_pseudo_seed;
size_t limbs;
mbedtls_mpi_uint *lower_bound = NULL;
mbedtls_mpi_uint *upper_bound = NULL;
@ -107,10 +113,7 @@ exit:
void mpi_random_values( int min, char *max_hex )
{
/* Same RNG as in mpi_core_random_basic */
mbedtls_test_rnd_pseudo_info rnd_core = {
{'T', 'h', 'i', 's', ' ', 'i', ',', 'a',
's', 'e', 'e', 'd', '!', 0},
0, 0};
mbedtls_test_rnd_pseudo_info rnd_core = rnd_pseudo_seed;
mbedtls_test_rnd_pseudo_info rnd_legacy;
memcpy( &rnd_legacy, &rnd_core, sizeof( rnd_core ) );
mbedtls_mpi max_legacy;