2014-05-30 08:38:18 +00:00
|
|
|
/* BEGIN_HEADER */
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/entropy.h"
|
2014-05-30 09:42:01 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Number of calls made to entropy_dummy_source()
|
|
|
|
*/
|
|
|
|
static size_t entropy_dummy_calls;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Dummy entropy source
|
|
|
|
*
|
|
|
|
* If data is NULL, write exactly the requested length.
|
|
|
|
* Otherwise, write the length indicated by data or error if negative
|
|
|
|
*/
|
|
|
|
static int entropy_dummy_source( void *data, unsigned char *output,
|
|
|
|
size_t len, size_t *olen )
|
|
|
|
{
|
|
|
|
entropy_dummy_calls++;
|
|
|
|
|
|
|
|
if( data == NULL )
|
|
|
|
*olen = len;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int *d = (int *) data;
|
|
|
|
|
|
|
|
if( *d < 0 )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
|
2014-05-30 09:42:01 +00:00
|
|
|
else
|
|
|
|
*olen = *d;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset( output, 0x2a, *olen );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
2014-05-30 08:38:18 +00:00
|
|
|
/* END_HEADER */
|
|
|
|
|
|
|
|
/* BEGIN_DEPENDENCIES
|
2015-04-08 10:49:31 +00:00
|
|
|
* depends_on:MBEDTLS_ENTROPY_C
|
2014-05-30 08:38:18 +00:00
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
|
2014-05-30 08:38:18 +00:00
|
|
|
void entropy_seed_file( char *path, int ret )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_entropy_context ctx;
|
2014-05-30 08:38:18 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_entropy_init( &ctx );
|
2014-05-30 08:38:18 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_entropy_write_seed_file( &ctx, path ) == ret );
|
|
|
|
TEST_ASSERT( mbedtls_entropy_update_seed_file( &ctx, path ) == ret );
|
2014-05-30 08:38:18 +00:00
|
|
|
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_entropy_free( &ctx );
|
2014-05-30 08:38:18 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2014-05-30 09:42:01 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void entropy_too_many_sources( )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_entropy_context ctx;
|
2014-05-30 09:42:01 +00:00
|
|
|
size_t i;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_entropy_init( &ctx );
|
2014-05-30 09:42:01 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* It's hard to tell precisely when the error will occur,
|
|
|
|
* since we don't know how many sources were automatically added.
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
for( i = 0; i < MBEDTLS_ENTROPY_MAX_SOURCES; i++ )
|
2015-06-19 08:26:32 +00:00
|
|
|
(void) mbedtls_entropy_add_source( &ctx, entropy_dummy_source, NULL,
|
|
|
|
16, MBEDTLS_ENTROPY_SOURCE_WEAK );
|
2014-05-30 09:42:01 +00:00
|
|
|
|
2015-06-19 08:26:32 +00:00
|
|
|
TEST_ASSERT( mbedtls_entropy_add_source( &ctx, entropy_dummy_source, NULL,
|
|
|
|
16, MBEDTLS_ENTROPY_SOURCE_WEAK )
|
2015-04-08 10:49:31 +00:00
|
|
|
== MBEDTLS_ERR_ENTROPY_MAX_SOURCES );
|
2014-05-30 09:42:01 +00:00
|
|
|
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_entropy_free( &ctx );
|
2014-05-30 09:42:01 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void entropy_func_len( int len, int ret )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_entropy_context ctx;
|
|
|
|
unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE + 10] = { 0 };
|
|
|
|
unsigned char acc[MBEDTLS_ENTROPY_BLOCK_SIZE + 10] = { 0 };
|
2014-05-30 09:42:01 +00:00
|
|
|
size_t i, j;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_entropy_init( &ctx );
|
2014-05-30 09:42:01 +00:00
|
|
|
|
|
|
|
/*
|
2015-04-08 10:49:31 +00:00
|
|
|
* See comments in mbedtls_entropy_self_test()
|
2014-05-30 09:42:01 +00:00
|
|
|
*/
|
|
|
|
for( i = 0; i < 8; i++ )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_entropy_func( &ctx, buf, len ) == ret );
|
2014-05-30 09:42:01 +00:00
|
|
|
for( j = 0; j < sizeof( buf ); j++ )
|
|
|
|
acc[j] |= buf[j];
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ret == 0 )
|
|
|
|
for( j = 0; j < (size_t) len; j++ )
|
|
|
|
TEST_ASSERT( acc[j] != 0 );
|
|
|
|
|
|
|
|
for( j = len; j < sizeof( buf ); j++ )
|
|
|
|
TEST_ASSERT( acc[j] == 0 );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void entropy_source_fail( char *path )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_entropy_context ctx;
|
2014-05-30 09:42:01 +00:00
|
|
|
int fail = -1;
|
|
|
|
unsigned char buf[16];
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_entropy_init( &ctx );
|
2014-05-30 09:42:01 +00:00
|
|
|
|
2015-06-19 08:26:32 +00:00
|
|
|
TEST_ASSERT( mbedtls_entropy_add_source( &ctx, entropy_dummy_source,
|
|
|
|
&fail, 16,
|
|
|
|
MBEDTLS_ENTROPY_SOURCE_WEAK )
|
2014-05-30 09:42:01 +00:00
|
|
|
== 0 );
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_entropy_func( &ctx, buf, sizeof( buf ) )
|
|
|
|
== MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
|
|
|
|
TEST_ASSERT( mbedtls_entropy_gather( &ctx )
|
|
|
|
== MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
|
|
|
|
#if defined(MBEDTLS_FS_IO)
|
|
|
|
TEST_ASSERT( mbedtls_entropy_write_seed_file( &ctx, path )
|
|
|
|
== MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
|
|
|
|
TEST_ASSERT( mbedtls_entropy_update_seed_file( &ctx, path )
|
|
|
|
== MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
|
2014-05-30 09:42:01 +00:00
|
|
|
#else
|
|
|
|
((void) path);
|
|
|
|
#endif
|
|
|
|
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_entropy_free( &ctx );
|
2014-05-30 09:42:01 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void entropy_threshold( int threshold, int chunk_size, int result )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_entropy_context ctx;
|
|
|
|
unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
|
2014-05-30 09:42:01 +00:00
|
|
|
int ret;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_entropy_init( &ctx );
|
2014-05-30 09:42:01 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_entropy_add_source( &ctx, entropy_dummy_source,
|
2015-06-19 08:26:32 +00:00
|
|
|
&chunk_size, threshold,
|
|
|
|
MBEDTLS_ENTROPY_SOURCE_WEAK ) == 0 );
|
2014-05-30 09:42:01 +00:00
|
|
|
|
|
|
|
entropy_dummy_calls = 0;
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_entropy_func( &ctx, buf, sizeof( buf ) );
|
2014-05-30 09:42:01 +00:00
|
|
|
|
|
|
|
if( result >= 0 )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( ret == 0 );
|
|
|
|
TEST_ASSERT( entropy_dummy_calls == (size_t) result );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_ASSERT( ret == result );
|
|
|
|
}
|
|
|
|
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_entropy_free( &ctx );
|
2014-05-30 09:42:01 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
|
2014-05-30 08:38:18 +00:00
|
|
|
void entropy_selftest( )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_entropy_self_test( 0 ) == 0 );
|
2014-05-30 08:38:18 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|