mirror of
https://github.com/yuzu-emu/mbedtls.git
synced 2024-11-27 01:14:14 +01:00
entropy_nv_seed: cope with SHA-256
This test case was only executed if the SHA-512 module was enabled and MBEDTLS_ENTROPY_FORCE_SHA256 was not enabled, so "config.pl full" didn't have a chance to reach it even if that enabled MBEDTLS_PLATFORM_NV_SEED_ALT. Now all it takes to enable this test is MBEDTLS_PLATFORM_NV_SEED_ALT and its requirements, and the near-ubiquitous MD module.
This commit is contained in:
parent
923f7f9637
commit
d56ca658ab
@ -300,10 +300,19 @@ void entropy_nv_seed_std_io()
|
|||||||
}
|
}
|
||||||
/* END_CASE */
|
/* END_CASE */
|
||||||
|
|
||||||
/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_PLATFORM_NV_SEED_ALT:MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
|
/* BEGIN_CASE depends_on:MBEDTLS_MD_C:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_PLATFORM_NV_SEED_ALT */
|
||||||
void entropy_nv_seed( char *read_seed_str )
|
void entropy_nv_seed( char *read_seed_str )
|
||||||
{
|
{
|
||||||
mbedtls_sha512_context accumulator;
|
#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
|
||||||
|
const mbedtls_md_info_t *md_info =
|
||||||
|
mbedtls_md_info_from_type( MBEDTLS_MD_SHA512 );
|
||||||
|
#elif defined(MBEDTLS_ENTROPY_SHA256_ACCUMULATOR)
|
||||||
|
const mbedtls_md_info_t *md_info =
|
||||||
|
mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 );
|
||||||
|
#else
|
||||||
|
#error "Unsupported entropy accumulator"
|
||||||
|
#endif
|
||||||
|
mbedtls_md_context_t accumulator;
|
||||||
mbedtls_entropy_context ctx;
|
mbedtls_entropy_context ctx;
|
||||||
int (*original_mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len ) =
|
int (*original_mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len ) =
|
||||||
mbedtls_nv_seed_read;
|
mbedtls_nv_seed_read;
|
||||||
@ -320,18 +329,14 @@ void entropy_nv_seed( char *read_seed_str )
|
|||||||
|
|
||||||
memset( entropy, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
|
memset( entropy, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
|
||||||
memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
|
memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
|
||||||
memset( buffer_seed, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
|
|
||||||
memset( empty, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
|
memset( empty, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
|
||||||
memset( check_seed, 2, MBEDTLS_ENTROPY_BLOCK_SIZE );
|
memset( check_seed, 2, MBEDTLS_ENTROPY_BLOCK_SIZE );
|
||||||
memset( check_entropy, 3, MBEDTLS_ENTROPY_BLOCK_SIZE );
|
memset( check_entropy, 3, MBEDTLS_ENTROPY_BLOCK_SIZE );
|
||||||
|
|
||||||
// Set the initial NV seed to read
|
|
||||||
unhexify( read_seed, read_seed_str );
|
|
||||||
memcpy( buffer_seed, read_seed, MBEDTLS_ENTROPY_BLOCK_SIZE );
|
|
||||||
|
|
||||||
// Make sure we read/write NV seed from our buffers
|
// Make sure we read/write NV seed from our buffers
|
||||||
mbedtls_platform_set_nv_seed( buffer_nv_seed_read, buffer_nv_seed_write );
|
mbedtls_platform_set_nv_seed( buffer_nv_seed_read, buffer_nv_seed_write );
|
||||||
|
|
||||||
|
mbedtls_md_init( &accumulator );
|
||||||
mbedtls_entropy_init( &ctx );
|
mbedtls_entropy_init( &ctx );
|
||||||
entropy_clear_sources( &ctx );
|
entropy_clear_sources( &ctx );
|
||||||
|
|
||||||
@ -339,45 +344,57 @@ void entropy_nv_seed( char *read_seed_str )
|
|||||||
MBEDTLS_ENTROPY_BLOCK_SIZE,
|
MBEDTLS_ENTROPY_BLOCK_SIZE,
|
||||||
MBEDTLS_ENTROPY_SOURCE_STRONG ) == 0 );
|
MBEDTLS_ENTROPY_SOURCE_STRONG ) == 0 );
|
||||||
|
|
||||||
|
// Set the initial NV seed to read.
|
||||||
|
// Get exactly MBEDTLS_ENTROPY_BLOCK_SIZE bytes from read_str.
|
||||||
|
TEST_ASSERT( strlen( read_seed ) / 2 >= MBEDTLS_ENTROPY_BLOCK_SIZE );
|
||||||
|
read_seed_str[MBEDTLS_ENTROPY_BLOCK_SIZE * 2] = '\0';
|
||||||
|
unhexify( read_seed, read_seed_str );
|
||||||
|
memcpy( buffer_seed, read_seed, MBEDTLS_ENTROPY_BLOCK_SIZE );
|
||||||
|
|
||||||
// Do an entropy run
|
// Do an entropy run
|
||||||
TEST_ASSERT( mbedtls_entropy_func( &ctx, entropy, sizeof( entropy ) ) == 0 );
|
TEST_ASSERT( mbedtls_entropy_func( &ctx, entropy, sizeof( entropy ) ) == 0 );
|
||||||
|
|
||||||
// Determine what should have happened with manual entropy internal logic
|
// Determine what should have happened with manual entropy internal logic
|
||||||
// Only use the SHA-512 version to check
|
|
||||||
|
|
||||||
// Init accumulator
|
// Init accumulator
|
||||||
header[1] = MBEDTLS_ENTROPY_BLOCK_SIZE;
|
header[1] = MBEDTLS_ENTROPY_BLOCK_SIZE;
|
||||||
mbedtls_sha512_starts( &accumulator, 0 );
|
TEST_ASSERT( mbedtls_md_setup( &accumulator, md_info, 0 ) == 0 );
|
||||||
|
|
||||||
// First run for updating write_seed
|
// First run for updating write_seed
|
||||||
header[0] = 0;
|
header[0] = 0;
|
||||||
mbedtls_sha512_update( &accumulator, header, 2 );
|
TEST_ASSERT( mbedtls_md_starts( &accumulator ) == 0 );
|
||||||
mbedtls_sha512_update( &accumulator, read_seed, MBEDTLS_ENTROPY_BLOCK_SIZE );
|
TEST_ASSERT( mbedtls_md_update( &accumulator, header, 2 ) == 0 );
|
||||||
mbedtls_sha512_finish( &accumulator, buf );
|
TEST_ASSERT( mbedtls_md_update( &accumulator,
|
||||||
|
read_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
|
||||||
|
TEST_ASSERT( mbedtls_md_finish( &accumulator, buf ) == 0 );
|
||||||
|
|
||||||
memset( &accumulator, 0, sizeof( mbedtls_sha512_context ) );
|
TEST_ASSERT( mbedtls_md_starts( &accumulator ) == 0 );
|
||||||
mbedtls_sha512_starts( &accumulator, 0 );
|
TEST_ASSERT( mbedtls_md_update( &accumulator,
|
||||||
mbedtls_sha512_update( &accumulator, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
|
buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
|
||||||
|
|
||||||
mbedtls_sha512( buf, MBEDTLS_ENTROPY_BLOCK_SIZE, check_seed, 0 );
|
TEST_ASSERT( mbedtls_md( md_info, buf, MBEDTLS_ENTROPY_BLOCK_SIZE,
|
||||||
|
check_seed ) == 0 );
|
||||||
|
|
||||||
// Second run for actual entropy (triggers mbedtls_entropy_update_nv_seed)
|
// Second run for actual entropy (triggers mbedtls_entropy_update_nv_seed)
|
||||||
header[0] = MBEDTLS_ENTROPY_SOURCE_MANUAL;
|
header[0] = MBEDTLS_ENTROPY_SOURCE_MANUAL;
|
||||||
mbedtls_sha512_update( &accumulator, header, 2 );
|
TEST_ASSERT( mbedtls_md_update( &accumulator, header, 2 ) == 0 );
|
||||||
mbedtls_sha512_update( &accumulator, empty, MBEDTLS_ENTROPY_BLOCK_SIZE );
|
TEST_ASSERT( mbedtls_md_update( &accumulator,
|
||||||
|
empty, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
|
||||||
|
|
||||||
header[0] = 0;
|
header[0] = 0;
|
||||||
mbedtls_sha512_update( &accumulator, header, 2 );
|
TEST_ASSERT( mbedtls_md_update( &accumulator, header, 2 ) == 0 );
|
||||||
mbedtls_sha512_update( &accumulator, check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE );
|
TEST_ASSERT( mbedtls_md_update( &accumulator,
|
||||||
mbedtls_sha512_finish( &accumulator, buf );
|
check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
|
||||||
|
TEST_ASSERT( mbedtls_md_finish( &accumulator, buf ) == 0 );
|
||||||
|
|
||||||
mbedtls_sha512( buf, MBEDTLS_ENTROPY_BLOCK_SIZE, check_entropy, 0 );
|
TEST_ASSERT( mbedtls_md( md_info, buf, MBEDTLS_ENTROPY_BLOCK_SIZE,
|
||||||
|
check_entropy ) == 0 );
|
||||||
|
|
||||||
// Check result of both NV file and entropy received with the manual calculations
|
// Check result of both NV file and entropy received with the manual calculations
|
||||||
TEST_ASSERT( memcmp( check_seed, buffer_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
|
TEST_ASSERT( memcmp( check_seed, buffer_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
|
||||||
TEST_ASSERT( memcmp( check_entropy, entropy, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
|
TEST_ASSERT( memcmp( check_entropy, entropy, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
|
||||||
|
|
||||||
exit:
|
exit:
|
||||||
|
mbedtls_md_free( &accumulator );
|
||||||
mbedtls_entropy_free( &ctx );
|
mbedtls_entropy_free( &ctx );
|
||||||
mbedtls_nv_seed_read = original_mbedtls_nv_seed_read;
|
mbedtls_nv_seed_read = original_mbedtls_nv_seed_read;
|
||||||
mbedtls_nv_seed_write = original_mbedtls_nv_seed_write;
|
mbedtls_nv_seed_write = original_mbedtls_nv_seed_write;
|
||||||
|
Loading…
Reference in New Issue
Block a user