mirror of
https://github.com/yuzu-emu/mbedtls.git
synced 2024-11-22 19:45:40 +01:00
ctr_drbg tests: make input sizes flexible
The ctr_drbg_validate_xxx test functions had hard-coded sizes for the entropy and the output size. Generalize the sizes. Keep track of the current entropy size. Unhexify the expected output and compare with the actual output, rather than hexifying the actual output and comparing the hex.
This commit is contained in:
parent
ed7da59798
commit
ef0624f791
@ -1,11 +1,15 @@
|
||||
/* BEGIN_HEADER */
|
||||
#include "mbedtls/entropy.h"
|
||||
#include "mbedtls/ctr_drbg.h"
|
||||
#include "string.h"
|
||||
|
||||
static int test_offset_idx;
|
||||
static size_t test_offset_idx;
|
||||
static size_t test_max_idx;
|
||||
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 );
|
||||
@ -53,24 +57,32 @@ exit:
|
||||
/* BEGIN_CASE */
|
||||
void ctr_drbg_validate_pr( data_t * add_init, data_t * entropy,
|
||||
data_t * add1, data_t * add2,
|
||||
data_t * result_str )
|
||||
char *result_string )
|
||||
{
|
||||
mbedtls_ctr_drbg_context ctx;
|
||||
unsigned char buf[512];
|
||||
unsigned char result[512];
|
||||
size_t entropy_len, add_init_len, add1_len, add2_len, result_len;
|
||||
|
||||
mbedtls_ctr_drbg_init( &ctx );
|
||||
|
||||
entropy_len = unhexify( entropy, entropy_string );
|
||||
result_len = unhexify( result, result_string );
|
||||
|
||||
test_offset_idx = 0;
|
||||
test_max_idx = entropy_len;
|
||||
/* CTR_DRBG_Instantiate(entropy[:entropy_len/3], nonce, perso, <ignored>)
|
||||
* where nonce||perso = add_init[add_init_len] */
|
||||
TEST_ASSERT( mbedtls_ctr_drbg_seed_entropy_len( &ctx, mbedtls_test_entropy_func, entropy, add_init, add_init_len, entropy_len / 3 ) == 0 );
|
||||
mbedtls_ctr_drbg_set_prediction_resistance( &ctx, MBEDTLS_CTR_DRBG_PR_ON );
|
||||
|
||||
/* CTR_DRBG_Generate(result_len * 8 bits, add1[:add1_len]) -> buf */
|
||||
/* Then reseed because of prediction resistance. */
|
||||
TEST_ASSERT( hexcmp( buf, result_str->x, 16, result_str->len ) == 0 );
|
||||
TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, buf, result_len, add1, add1_len ) == 0 );
|
||||
/* CTR_DRBG_Generate(result_len * 8 bits, add2[:add2_len]) -> buf */
|
||||
/* Then reseed because of prediction resistance. */
|
||||
TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, buf, result_len, add2, add2_len ) == 0 );
|
||||
TEST_ASSERT( memcmp( buf, result, result_len ) == 0 );
|
||||
|
||||
exit:
|
||||
mbedtls_ctr_drbg_free( &ctx );
|
||||
@ -80,23 +92,31 @@ exit:
|
||||
/* BEGIN_CASE */
|
||||
void ctr_drbg_validate_nopr( data_t * add_init, data_t * entropy,
|
||||
data_t * add1, data_t * add_reseed,
|
||||
data_t * add2, data_t * result_str )
|
||||
char *add2_string, char *result_string )
|
||||
{
|
||||
mbedtls_ctr_drbg_context ctx;
|
||||
unsigned char buf[512];
|
||||
unsigned char result[512];
|
||||
size_t entropy_len, add_init_len, add1_len, add_reseed_len, add2_len, result_len;
|
||||
|
||||
mbedtls_ctr_drbg_init( &ctx );
|
||||
|
||||
entropy_len = unhexify( entropy, entropy_string );
|
||||
result_len = unhexify( result, result_string );
|
||||
|
||||
test_offset_idx = 0;
|
||||
test_max_idx = entropy_len;
|
||||
/* CTR_DRBG_Instantiate(entropy[:entropy_len/2], nonce, perso, <ignored>)
|
||||
* where nonce||perso = add_init[add_init_len] */
|
||||
TEST_ASSERT( mbedtls_ctr_drbg_seed_entropy_len( &ctx, mbedtls_test_entropy_func, entropy, add_init, add_init_len, entropy_len / 2 ) == 0 );
|
||||
|
||||
/* CTR_DRBG_Generate(16 * 8 bits, add1[:add1_len]) -> buf */
|
||||
TEST_ASSERT( mbedtls_ctr_drbg_reseed( &ctx, add_reseed->x, add_reseed->len ) == 0 );
|
||||
TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, buf, result_len, add1, add1_len ) == 0 );
|
||||
/* CTR_DRBG_Reseed(entropy[entropy_len/2:entropy_len], add_reseed[:add_reseed_len]) */
|
||||
TEST_ASSERT( hexcmp( buf, result_str->x, 16, result_str->len ) == 0 );
|
||||
/* CTR_DRBG_Generate(16 * 8 bits, add2[:add2_len]) -> buf */
|
||||
TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, buf, result_len, add2, add2_len ) == 0 );
|
||||
TEST_ASSERT( memcmp( buf, result, result_len ) == 0 );
|
||||
|
||||
exit:
|
||||
mbedtls_ctr_drbg_free( &ctx );
|
||||
@ -111,10 +131,11 @@ void ctr_drbg_entropy_usage( )
|
||||
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 ) );
|
||||
|
Loading…
Reference in New Issue
Block a user