early-access version 1503

This commit is contained in:
pineappleEA
2021-03-06 01:41:47 +01:00
parent a37fdd48d5
commit 3fd627d0ba
558 changed files with 55823 additions and 15727 deletions

View File

@@ -1,14 +1,97 @@
/* BEGIN_HEADER */
#include "mbedtls/entropy.h"
#include "mbedtls/ctr_drbg.h"
#include "string.h"
static int test_offset_idx;
/* Modes for ctr_drbg_validate */
enum reseed_mode
{
RESEED_NEVER, /* never reseed */
RESEED_FIRST, /* instantiate, reseed, generate, generate */
RESEED_SECOND, /* instantiate, generate, reseed, generate */
RESEED_ALWAYS /* prediction resistance, no explicit reseed */
};
static size_t test_offset_idx = 0;
static size_t test_max_idx = 0;
static int mbedtls_test_entropy_func( void *data, unsigned char *buf, size_t len )
{
const unsigned char *p = (unsigned char *) data;
if( test_offset_idx + len > test_max_idx )
return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
memcpy( buf, p + test_offset_idx, len );
test_offset_idx += len;
return( 0 );
}
static void ctr_drbg_validate_internal( int reseed_mode, data_t * nonce,
int entropy_len_arg, data_t * entropy,
data_t * reseed,
data_t * add1, data_t * add2,
data_t * result )
{
mbedtls_ctr_drbg_context ctx;
unsigned char buf[64];
size_t entropy_chunk_len = (size_t) entropy_len_arg;
TEST_ASSERT( entropy_chunk_len <= sizeof( buf ) );
test_offset_idx = 0;
mbedtls_ctr_drbg_init( &ctx );
test_max_idx = entropy->len;
/* CTR_DRBG_Instantiate(entropy[:entropy->len], nonce, perso, <ignored>)
* where nonce||perso = nonce[nonce->len] */
mbedtls_ctr_drbg_set_entropy_len( &ctx, entropy_chunk_len );
TEST_ASSERT( mbedtls_ctr_drbg_seed(
&ctx,
mbedtls_test_entropy_func, entropy->x,
nonce->x, nonce->len ) == 0 );
if( reseed_mode == RESEED_ALWAYS )
mbedtls_ctr_drbg_set_prediction_resistance(
&ctx,
MBEDTLS_CTR_DRBG_PR_ON );
if( reseed_mode == RESEED_FIRST )
{
/* CTR_DRBG_Reseed(entropy[idx:idx+entropy->len],
* reseed[:reseed->len]) */
TEST_ASSERT( mbedtls_ctr_drbg_reseed(
&ctx,
reseed->x, reseed->len ) == 0 );
}
/* CTR_DRBG_Generate(result->len * 8 bits, add1[:add1->len]) -> buf */
/* Then reseed if prediction resistance is enabled. */
TEST_ASSERT( mbedtls_ctr_drbg_random_with_add(
&ctx,
buf, result->len,
add1->x, add1->len ) == 0 );
if( reseed_mode == RESEED_SECOND )
{
/* CTR_DRBG_Reseed(entropy[idx:idx+entropy->len],
* reseed[:reseed->len]) */
TEST_ASSERT( mbedtls_ctr_drbg_reseed(
&ctx,
reseed->x, reseed->len ) == 0 );
}
/* CTR_DRBG_Generate(result->len * 8 bits, add2->x[:add2->len]) -> buf */
/* Then reseed if prediction resistance is enabled. */
TEST_ASSERT( mbedtls_ctr_drbg_random_with_add(
&ctx,
buf, result->len,
add2->x, add2->len ) == 0 );
TEST_ASSERT( memcmp( buf, result->x, result->len ) == 0 );
exit:
mbedtls_ctr_drbg_free( &ctx );
}
/* END_HEADER */
/* BEGIN_DEPENDENCIES
@@ -49,103 +132,90 @@ exit:
}
/* END_CASE */
/* BEGIN_CASE */
void ctr_drbg_validate_pr( char *add_init_string, char *entropy_string,
char *add1_string, char *add2_string,
char *result_str )
void ctr_drbg_validate_no_reseed( data_t * add_init, data_t * entropy,
data_t * add1, data_t * add2,
data_t * result_string )
{
unsigned char entropy[512];
unsigned char add_init[512];
unsigned char add1[512];
unsigned char add2[512];
mbedtls_ctr_drbg_context ctx;
unsigned char buf[512];
unsigned char output_str[512];
int add_init_len, add1_len, add2_len;
mbedtls_ctr_drbg_init( &ctx );
memset( output_str, 0, 512 );
unhexify( entropy, entropy_string );
add_init_len = unhexify( add_init, add_init_string );
add1_len = unhexify( add1, add1_string );
add2_len = unhexify( add2, add2_string );
test_offset_idx = 0;
TEST_ASSERT( mbedtls_ctr_drbg_seed_entropy_len( &ctx, mbedtls_test_entropy_func, entropy, add_init, add_init_len, 32 ) == 0 );
mbedtls_ctr_drbg_set_prediction_resistance( &ctx, MBEDTLS_CTR_DRBG_PR_ON );
TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, buf, 16, add1, add1_len ) == 0 );
TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, buf, 16, add2, add2_len ) == 0 );
hexify( output_str, buf, 16 );
TEST_ASSERT( strcmp( (char *) output_str, result_str ) == 0 );
exit:
mbedtls_ctr_drbg_free( &ctx );
data_t empty = { 0, 0 };
ctr_drbg_validate_internal( RESEED_NEVER, add_init,
entropy->len, entropy,
&empty, add1, add2,
result_string );
goto exit; // goto is needed to avoid warning ( no test assertions in func)
}
/* END_CASE */
/* BEGIN_CASE */
void ctr_drbg_validate_nopr( char *add_init_string, char *entropy_string,
char *add1_string, char *add_reseed_string,
char *add2_string, char *result_str )
void ctr_drbg_validate_pr( data_t * add_init, data_t * entropy,
data_t * add1, data_t * add2,
data_t * result_string )
{
unsigned char entropy[512];
unsigned char add_init[512];
unsigned char add1[512];
unsigned char add_reseed[512];
unsigned char add2[512];
mbedtls_ctr_drbg_context ctx;
unsigned char buf[512];
unsigned char output_str[512];
int add_init_len, add1_len, add_reseed_len, add2_len;
mbedtls_ctr_drbg_init( &ctx );
memset( output_str, 0, 512 );
unhexify( entropy, entropy_string );
add_init_len = unhexify( add_init, add_init_string );
add1_len = unhexify( add1, add1_string );
add_reseed_len = unhexify( add_reseed, add_reseed_string );
add2_len = unhexify( add2, add2_string );
test_offset_idx = 0;
TEST_ASSERT( mbedtls_ctr_drbg_seed_entropy_len( &ctx, mbedtls_test_entropy_func, entropy, add_init, add_init_len, 32 ) == 0 );
TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, buf, 16, add1, add1_len ) == 0 );
TEST_ASSERT( mbedtls_ctr_drbg_reseed( &ctx, add_reseed, add_reseed_len ) == 0 );
TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, buf, 16, add2, add2_len ) == 0 );
hexify( output_str, buf, 16 );
TEST_ASSERT( strcmp( (char *) output_str, result_str ) == 0 );
exit:
mbedtls_ctr_drbg_free( &ctx );
data_t empty = { 0, 0 };
ctr_drbg_validate_internal( RESEED_ALWAYS, add_init,
entropy->len / 3, entropy,
&empty, add1, add2,
result_string );
goto exit; // goto is needed to avoid warning ( no test assertions in func)
}
/* END_CASE */
/* BEGIN_CASE */
void ctr_drbg_entropy_usage( )
void ctr_drbg_validate_reseed_between( data_t * add_init, data_t * entropy,
data_t * add1, data_t * add_reseed,
data_t * add2, data_t * result_string )
{
ctr_drbg_validate_internal( RESEED_SECOND, add_init,
entropy->len / 2, entropy,
add_reseed, add1, add2,
result_string );
goto exit; // goto is needed to avoid warning ( no test assertions in func)
}
/* END_CASE */
/* BEGIN_CASE */
void ctr_drbg_validate_reseed_first( data_t * add_init, data_t * entropy,
data_t * add1, data_t * add_reseed,
data_t * add2, data_t * result_string )
{
ctr_drbg_validate_internal( RESEED_FIRST, add_init,
entropy->len / 2, entropy,
add_reseed, add1, add2,
result_string );
goto exit; // goto is needed to avoid warning ( no test assertions in func)
}
/* END_CASE */
/* BEGIN_CASE */
void ctr_drbg_entropy_usage( )
{
unsigned char out[16];
unsigned char add[16];
unsigned char entropy[1024];
mbedtls_ctr_drbg_context ctx;
size_t i, reps = 10;
int last_idx;
size_t last_idx;
mbedtls_ctr_drbg_init( &ctx );
test_offset_idx = 0;
test_max_idx = sizeof( entropy );
memset( entropy, 0, sizeof( entropy ) );
memset( out, 0, sizeof( out ) );
memset( add, 0, sizeof( add ) );
/* Set reseed interval before seed */
mbedtls_ctr_drbg_set_reseed_interval( &ctx, 2 * reps );
/* Init must use entropy */
last_idx = test_offset_idx;
TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctx, mbedtls_test_entropy_func, entropy, NULL, 0 ) == 0 );
TEST_ASSERT( last_idx < test_offset_idx );
/* By default, PR is off and reseed_interval is large,
* so the next few calls should not use entropy */
/* By default, PR is off, and reseed interval was set to
* 2 * reps so the next few calls should not use entropy */
last_idx = test_offset_idx;
for( i = 0; i < reps; i++ )
{
@@ -161,15 +231,16 @@ void ctr_drbg_entropy_usage( )
TEST_ASSERT( out[sizeof( out ) - 2] == 0 );
TEST_ASSERT( out[sizeof( out ) - 1] == 0 );
/* Set reseed_interval to the number of calls done,
* so the next call should reseed */
mbedtls_ctr_drbg_set_reseed_interval( &ctx, 2 * reps );
/* There have been 2 * reps calls to random. The next call should reseed */
TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
TEST_ASSERT( last_idx < test_offset_idx );
/* The new few calls should not reseed */
/* Set reseed interval after seed */
mbedtls_ctr_drbg_set_reseed_interval( &ctx, 4 * reps + 1 );
/* The next few calls should not reseed */
last_idx = test_offset_idx;
for( i = 0; i < reps / 2; i++ )
for( i = 0; i < (2 * reps); i++ )
{
TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, out, sizeof( out ) ,
@@ -177,9 +248,11 @@ void ctr_drbg_entropy_usage( )
}
TEST_ASSERT( last_idx == test_offset_idx );
/* Call update with too much data (sizeof entropy > MAX(_SEED)_INPUT)
* (just make sure it doesn't cause memory corruption) */
mbedtls_ctr_drbg_update( &ctx, entropy, sizeof( entropy ) );
/* Call update with too much data (sizeof entropy > MAX(_SEED)_INPUT).
* Make sure it's detected as an error and doesn't cause memory
* corruption. */
TEST_ASSERT( mbedtls_ctr_drbg_update_ret(
&ctx, entropy, sizeof( entropy ) ) != 0 );
/* Now enable PR, so the next few calls should all reseed */
mbedtls_ctr_drbg_set_prediction_resistance( &ctx, MBEDTLS_CTR_DRBG_PR_ON );
@@ -203,7 +276,7 @@ exit:
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
void ctr_drbg_seed_file( char *path, int ret )
void ctr_drbg_seed_file( char * path, int ret )
{
mbedtls_ctr_drbg_context ctx;
@@ -219,7 +292,7 @@ exit:
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
void ctr_drbg_selftest( )
void ctr_drbg_selftest( )
{
TEST_ASSERT( mbedtls_ctr_drbg_self_test( 1 ) == 0 );
}