- Changed the used random function pointer to more flexible format. Renamed havege_rand() to havege_random() to prevent mistakes. Lots of changes as a consequence in library code and programs
diff --git a/tests/suites/helpers.function b/tests/suites/helpers.function
index cfb0253..94d7273 100644
--- a/tests/suites/helpers.function
+++ b/tests/suites/helpers.function
@@ -97,12 +97,17 @@
*
* rng_state shall be NULL.
*/
-static int rnd_std_rand( void *rng_state )
+static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
{
+ size_t i;
+
if( rng_state != NULL )
rng_state = NULL;
- return( rand() );
+ for( i = 0; i < len; ++i )
+ output[i] = rand();
+
+ return( 0 );
}
/**
@@ -110,19 +115,20 @@
*
* rng_state shall be NULL.
*/
-static int rnd_zero_rand( void *rng_state )
+static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
{
if( rng_state != NULL )
rng_state = NULL;
+ memset( output, 0, len );
+
return( 0 );
}
typedef struct
{
unsigned char *buf;
- int length;
- int per_call;
+ size_t length;
} rnd_buf_info;
/**
@@ -136,34 +142,29 @@
*
* After the buffer is empty it will return rand();
*/
-static int rnd_buffer_rand( void *rng_state )
+static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
{
rnd_buf_info *info = (rnd_buf_info *) rng_state;
- int res;
+ size_t use_len;
if( rng_state == NULL )
- return( rand() );
+ return( rnd_std_rand( NULL, output, len ) );
- if( info->per_call > 4 )
- info->per_call = 4;
- else if( info->per_call < 1 )
- info->per_call = 1;
+ use_len = len;
+ if( len > info->length )
+ use_len = info->length;
- res = rand();
-
- if( info->length >= info->per_call )
+ if( use_len )
{
- memcpy( &res, info->buf, info->per_call );
- info->buf += info->per_call;
- info->length -= info->per_call;
- }
- else if( info->length > 0 )
- {
- memcpy( &res, info->buf, info->length );
- info->length = 0;
+ memcpy( output, info->buf, use_len );
+ info->buf += use_len;
+ info->length -= use_len;
}
- return( res );
+ if( len - use_len > 0 )
+ return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
+
+ return( 0 );
}
/**
@@ -187,21 +188,33 @@
*
* rng_state shall be a pointer to a rnd_pseudo_info structure.
*/
-static int rnd_pseudo_rand( void *rng_state )
+static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
{
rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
- uint32_t i, *k, sum = 0, delta=0x9E3779B9;
+ uint32_t i, *k, sum, delta=0x9E3779B9;
if( rng_state == NULL )
- return( rand() );
+ return( rnd_std_rand( NULL, output, len ) );
k = info->key;
- for( i = 0; i < 32; i++ )
+
+ while( len > 0 )
{
- info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
- sum += delta;
- info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
+ size_t use_len = len;
+ sum = 0;
+
+ use_len = 1;
+
+ for( i = 0; i < 32; i++ )
+ {
+ info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
+ sum += delta;
+ info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
+ }
+
+ memcpy( output, &info->v0, use_len );
+ len -= use_len;
}
- return( info->v0 );
+ return( 0 );
}
diff --git a/tests/suites/test_suite_pkcs1_v21.function b/tests/suites/test_suite_pkcs1_v21.function
index 98dea20..b7d3813 100644
--- a/tests/suites/test_suite_pkcs1_v21.function
+++ b/tests/suites/test_suite_pkcs1_v21.function
@@ -26,7 +26,6 @@
info.length = unhexify( rnd_buf, {seed} );
info.buf = rnd_buf;
- info.per_call = 1;
rsa_init( &ctx, RSA_PKCS_V21, {hash} );
memset( message_str, 0x00, 1000 );
@@ -114,7 +113,6 @@
info.length = unhexify( rnd_buf, {salt} );
info.buf = rnd_buf;
- info.per_call = 1;
mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
rsa_init( &ctx, RSA_PKCS_V21, {hash} );
diff --git a/tests/suites/test_suite_rsa.function b/tests/suites/test_suite_rsa.function
index 0d4ba0b..15048cd 100644
--- a/tests/suites/test_suite_rsa.function
+++ b/tests/suites/test_suite_rsa.function
@@ -514,7 +514,7 @@
havege_init( &hs );
rsa_init( &ctx, 0, 0 );
- TEST_ASSERT( rsa_gen_key( &ctx, havege_rand, &hs, {nrbits}, {exponent} ) == {result} );
+ TEST_ASSERT( rsa_gen_key( &ctx, havege_random, &hs, {nrbits}, {exponent} ) == {result} );
if( {result} == 0 )
{
TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );