mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-08-08 17:42:09 +03:00
- 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
This commit is contained in:
@@ -97,12 +97,17 @@ void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
|
||||
*
|
||||
* 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 @@ static int rnd_std_rand( void *rng_state )
|
||||
*
|
||||
* 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 @@ typedef struct
|
||||
*
|
||||
* 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 @@ typedef struct
|
||||
*
|
||||
* 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 );
|
||||
}
|
||||
|
Reference in New Issue
Block a user