mirror of
https://github.com/yuzu-emu/mbedtls.git
synced 2024-11-25 16:45:40 +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
0450eecfae
commit
756b3f2c25
@ -301,10 +301,19 @@ void entropy_nv_seed_std_io( )
|
||||
}
|
||||
/* 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( data_t * read_seed )
|
||||
{
|
||||
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;
|
||||
int (*original_mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len ) =
|
||||
mbedtls_nv_seed_read;
|
||||
@ -320,17 +329,14 @@ void entropy_nv_seed( data_t * read_seed )
|
||||
|
||||
memset( entropy, 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( check_seed, 2, MBEDTLS_ENTROPY_BLOCK_SIZE );
|
||||
memset( check_entropy, 3, MBEDTLS_ENTROPY_BLOCK_SIZE );
|
||||
|
||||
// Set the initial NV seed to read
|
||||
memcpy( buffer_seed, read_seed->x, read_seed->len );
|
||||
|
||||
// Make sure we read/write NV seed from our buffers
|
||||
mbedtls_platform_set_nv_seed( buffer_nv_seed_read, buffer_nv_seed_write );
|
||||
|
||||
mbedtls_md_init( &accumulator );
|
||||
mbedtls_entropy_init( &ctx );
|
||||
entropy_clear_sources( &ctx );
|
||||
|
||||
@ -338,45 +344,54 @@ void entropy_nv_seed( data_t * read_seed )
|
||||
MBEDTLS_ENTROPY_BLOCK_SIZE,
|
||||
MBEDTLS_ENTROPY_SOURCE_STRONG ) == 0 );
|
||||
|
||||
// Set the initial NV seed to read
|
||||
TEST_ASSERT( read_seed->len >= MBEDTLS_ENTROPY_BLOCK_SIZE );
|
||||
memcpy( buffer_seed, read_seed->x, MBEDTLS_ENTROPY_BLOCK_SIZE );
|
||||
|
||||
// Do an entropy run
|
||||
TEST_ASSERT( mbedtls_entropy_func( &ctx, entropy, sizeof( entropy ) ) == 0 );
|
||||
|
||||
// Determine what should have happened with manual entropy internal logic
|
||||
// Only use the SHA-512 version to check
|
||||
|
||||
// Init accumulator
|
||||
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
|
||||
header[0] = 0;
|
||||
mbedtls_sha512_update( &accumulator, header, 2 );
|
||||
mbedtls_sha512_update( &accumulator, read_seed->x, read_seed->len );
|
||||
mbedtls_sha512_finish( &accumulator, buf );
|
||||
TEST_ASSERT( mbedtls_md_starts( &accumulator ) == 0 );
|
||||
TEST_ASSERT( mbedtls_md_update( &accumulator, header, 2 ) == 0 );
|
||||
TEST_ASSERT( mbedtls_md_update( &accumulator,
|
||||
read_seed->x, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
|
||||
TEST_ASSERT( mbedtls_md_finish( &accumulator, buf ) == 0 );
|
||||
|
||||
memset( &accumulator, 0, sizeof( mbedtls_sha512_context ) );
|
||||
mbedtls_sha512_starts( &accumulator, 0 );
|
||||
mbedtls_sha512_update( &accumulator, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
|
||||
TEST_ASSERT( mbedtls_md_starts( &accumulator ) == 0 );
|
||||
TEST_ASSERT( mbedtls_md_update( &accumulator,
|
||||
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)
|
||||
header[0] = MBEDTLS_ENTROPY_SOURCE_MANUAL;
|
||||
mbedtls_sha512_update( &accumulator, header, 2 );
|
||||
mbedtls_sha512_update( &accumulator, empty, MBEDTLS_ENTROPY_BLOCK_SIZE );
|
||||
TEST_ASSERT( mbedtls_md_update( &accumulator, header, 2 ) == 0 );
|
||||
TEST_ASSERT( mbedtls_md_update( &accumulator,
|
||||
empty, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
|
||||
|
||||
header[0] = 0;
|
||||
mbedtls_sha512_update( &accumulator, header, 2 );
|
||||
mbedtls_sha512_update( &accumulator, check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE );
|
||||
mbedtls_sha512_finish( &accumulator, buf );
|
||||
TEST_ASSERT( mbedtls_md_update( &accumulator, header, 2 ) == 0 );
|
||||
TEST_ASSERT( mbedtls_md_update( &accumulator,
|
||||
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
|
||||
TEST_ASSERT( memcmp( check_seed, buffer_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
|
||||
TEST_ASSERT( memcmp( check_entropy, entropy, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
|
||||
|
||||
exit:
|
||||
mbedtls_md_free( &accumulator );
|
||||
mbedtls_entropy_free( &ctx );
|
||||
mbedtls_nv_seed_read = original_mbedtls_nv_seed_read;
|
||||
mbedtls_nv_seed_write = original_mbedtls_nv_seed_write;
|
||||
|
Loading…
Reference in New Issue
Block a user