mirror of
https://github.com/yuzu-emu/mbedtls.git
synced 2025-06-20 05:27:49 +00:00
Entropy tests: support multiple dummy sources
Always pass a context object to entropy_dummy_source. This lets us write tests that register more than one source and keep track of how many times each one is called.
This commit is contained in:
parent
c34b839d85
commit
ed04a676ee
|
@ -3,10 +3,19 @@
|
||||||
#include "mbedtls/entropy_poll.h"
|
#include "mbedtls/entropy_poll.h"
|
||||||
#include "string.h"
|
#include "string.h"
|
||||||
|
|
||||||
/*
|
typedef enum
|
||||||
* Number of calls made to entropy_dummy_source()
|
{
|
||||||
*/
|
DUMMY_CONSTANT_LENGTH, /* Output context->length bytes */
|
||||||
static size_t entropy_dummy_calls;
|
DUMMY_REQUESTED_LENGTH, /* Output whatever length was requested */
|
||||||
|
DUMMY_FAIL, /* Return an error code */
|
||||||
|
} entropy_dummy_instruction;
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
entropy_dummy_instruction instruction;
|
||||||
|
size_t length; /* Length to return for DUMMY_CONSTANT_LENGTH */
|
||||||
|
size_t calls; /* Incremented at each call */
|
||||||
|
} entropy_dummy_context;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Dummy entropy source
|
* Dummy entropy source
|
||||||
|
@ -14,25 +23,25 @@ static size_t entropy_dummy_calls;
|
||||||
* If data is NULL, write exactly the requested length.
|
* If data is NULL, write exactly the requested length.
|
||||||
* Otherwise, write the length indicated by data or error if negative
|
* Otherwise, write the length indicated by data or error if negative
|
||||||
*/
|
*/
|
||||||
static int entropy_dummy_source( void *data, unsigned char *output,
|
static int entropy_dummy_source( void *arg, unsigned char *output,
|
||||||
size_t len, size_t *olen )
|
size_t len, size_t *olen )
|
||||||
{
|
{
|
||||||
entropy_dummy_calls++;
|
entropy_dummy_context *context = arg;
|
||||||
|
++context->calls;
|
||||||
|
|
||||||
if( data == NULL )
|
switch( context->instruction )
|
||||||
*olen = len;
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
int *d = (int *) data;
|
case DUMMY_CONSTANT_LENGTH:
|
||||||
|
*olen = context->length;
|
||||||
if( *d < 0 )
|
break;
|
||||||
|
case DUMMY_REQUESTED_LENGTH:
|
||||||
|
*olen = len;
|
||||||
|
break;
|
||||||
|
case DUMMY_FAIL:
|
||||||
return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
|
return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
|
||||||
else
|
|
||||||
*olen = *d;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
memset( output, 0x2a, *olen );
|
memset( output, 0x2a, *olen );
|
||||||
|
|
||||||
return( 0 );
|
return( 0 );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -144,6 +153,7 @@ void entropy_too_many_sources( )
|
||||||
{
|
{
|
||||||
mbedtls_entropy_context ctx;
|
mbedtls_entropy_context ctx;
|
||||||
size_t i;
|
size_t i;
|
||||||
|
entropy_dummy_context dummy = {DUMMY_REQUESTED_LENGTH, 0, 0};
|
||||||
|
|
||||||
mbedtls_entropy_init( &ctx );
|
mbedtls_entropy_init( &ctx );
|
||||||
|
|
||||||
|
@ -152,10 +162,10 @@ void entropy_too_many_sources( )
|
||||||
* since we don't know how many sources were automatically added.
|
* since we don't know how many sources were automatically added.
|
||||||
*/
|
*/
|
||||||
for( i = 0; i < MBEDTLS_ENTROPY_MAX_SOURCES; i++ )
|
for( i = 0; i < MBEDTLS_ENTROPY_MAX_SOURCES; i++ )
|
||||||
(void) mbedtls_entropy_add_source( &ctx, entropy_dummy_source, NULL,
|
(void) mbedtls_entropy_add_source( &ctx, entropy_dummy_source, &dummy,
|
||||||
16, MBEDTLS_ENTROPY_SOURCE_WEAK );
|
16, MBEDTLS_ENTROPY_SOURCE_WEAK );
|
||||||
|
|
||||||
TEST_ASSERT( mbedtls_entropy_add_source( &ctx, entropy_dummy_source, NULL,
|
TEST_ASSERT( mbedtls_entropy_add_source( &ctx, entropy_dummy_source, &dummy,
|
||||||
16, MBEDTLS_ENTROPY_SOURCE_WEAK )
|
16, MBEDTLS_ENTROPY_SOURCE_WEAK )
|
||||||
== MBEDTLS_ERR_ENTROPY_MAX_SOURCES );
|
== MBEDTLS_ERR_ENTROPY_MAX_SOURCES );
|
||||||
|
|
||||||
|
@ -197,13 +207,13 @@ void entropy_func_len( int len, int ret )
|
||||||
void entropy_source_fail( char * path )
|
void entropy_source_fail( char * path )
|
||||||
{
|
{
|
||||||
mbedtls_entropy_context ctx;
|
mbedtls_entropy_context ctx;
|
||||||
int fail = -1;
|
|
||||||
unsigned char buf[16];
|
unsigned char buf[16];
|
||||||
|
entropy_dummy_context dummy = {DUMMY_FAIL, 0, 0};
|
||||||
|
|
||||||
mbedtls_entropy_init( &ctx );
|
mbedtls_entropy_init( &ctx );
|
||||||
|
|
||||||
TEST_ASSERT( mbedtls_entropy_add_source( &ctx, entropy_dummy_source,
|
TEST_ASSERT( mbedtls_entropy_add_source( &ctx, entropy_dummy_source,
|
||||||
&fail, 16,
|
&dummy, 16,
|
||||||
MBEDTLS_ENTROPY_SOURCE_WEAK )
|
MBEDTLS_ENTROPY_SOURCE_WEAK )
|
||||||
== 0 );
|
== 0 );
|
||||||
|
|
||||||
|
@ -229,16 +239,16 @@ exit:
|
||||||
void entropy_threshold( int threshold, int chunk_size, int result )
|
void entropy_threshold( int threshold, int chunk_size, int result )
|
||||||
{
|
{
|
||||||
mbedtls_entropy_context ctx;
|
mbedtls_entropy_context ctx;
|
||||||
|
entropy_dummy_context dummy = {DUMMY_CONSTANT_LENGTH, chunk_size, 0};
|
||||||
unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
|
unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
mbedtls_entropy_init( &ctx );
|
mbedtls_entropy_init( &ctx );
|
||||||
|
|
||||||
TEST_ASSERT( mbedtls_entropy_add_source( &ctx, entropy_dummy_source,
|
TEST_ASSERT( mbedtls_entropy_add_source( &ctx, entropy_dummy_source,
|
||||||
&chunk_size, threshold,
|
&dummy, threshold,
|
||||||
MBEDTLS_ENTROPY_SOURCE_WEAK ) == 0 );
|
MBEDTLS_ENTROPY_SOURCE_WEAK ) == 0 );
|
||||||
|
|
||||||
entropy_dummy_calls = 0;
|
|
||||||
ret = mbedtls_entropy_func( &ctx, buf, sizeof( buf ) );
|
ret = mbedtls_entropy_func( &ctx, buf, sizeof( buf ) );
|
||||||
|
|
||||||
if( result >= 0 )
|
if( result >= 0 )
|
||||||
|
@ -248,7 +258,7 @@ void entropy_threshold( int threshold, int chunk_size, int result )
|
||||||
// Two times as much calls due to the NV seed update
|
// Two times as much calls due to the NV seed update
|
||||||
result *= 2;
|
result *= 2;
|
||||||
#endif
|
#endif
|
||||||
TEST_ASSERT( entropy_dummy_calls == (size_t) result );
|
TEST_ASSERT( dummy.calls == (size_t) result );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue