@ -46,7 +46,7 @@
# define TEST 0
# define TEST 0
# endif
# endif
static int read_random ( uint32 _t * dst , const char * file )
static int read_random ( uint8 _t * dst , size_t len , const char * file )
{
{
# if HAVE_UNISTD_H
# if HAVE_UNISTD_H
FILE * fp = avpriv_fopen_utf8 ( file , " r " ) ;
FILE * fp = avpriv_fopen_utf8 ( file , " r " ) ;
@ -54,10 +54,10 @@ static int read_random(uint32_t *dst, const char *file)
if ( ! fp )
if ( ! fp )
return AVERROR_UNKNOWN ;
return AVERROR_UNKNOWN ;
err = fread ( dst , 1 , sizeof ( * dst ) , fp ) ;
err = fread ( dst , 1 , len , fp ) ;
fclose ( fp ) ;
fclose ( fp ) ;
if ( err ! = sizeof ( * dst ) )
if ( err ! = len )
return AVERROR_UNKNOWN ;
return AVERROR_UNKNOWN ;
return 0 ;
return 0 ;
@ -121,27 +121,38 @@ static uint32_t get_generic_seed(void)
return AV_RB32 ( digest ) + AV_RB32 ( digest + 16 ) ;
return AV_RB32 ( digest ) + AV_RB32 ( digest + 16 ) ;
}
}
uint32_t av_get_random_seed ( void )
int av_random_bytes ( uint8_t * buf , size_t len )
{
{
uint32_t seed ;
int err ;
# if HAVE_BCRYPT
# if HAVE_BCRYPT
BCRYPT_ALG_HANDLE algo_handle ;
BCRYPT_ALG_HANDLE algo_handle ;
NTSTATUS ret = BCryptOpenAlgorithmProvider ( & algo_handle , BCRYPT_RNG_ALGORITHM ,
NTSTATUS ret = BCryptOpenAlgorithmProvider ( & algo_handle , BCRYPT_RNG_ALGORITHM ,
MS_PRIMITIVE_PROVIDER , 0 ) ;
MS_PRIMITIVE_PROVIDER , 0 ) ;
if ( BCRYPT_SUCCESS ( ret ) ) {
if ( BCRYPT_SUCCESS ( ret ) ) {
NTSTATUS ret = BCryptGenRandom ( algo_handle , ( UCHAR * ) & seed , sizeof ( seed ) , 0 ) ;
NTSTATUS ret = BCryptGenRandom ( algo_handle , ( PUCHAR ) buf , len , 0 ) ;
BCryptCloseAlgorithmProvider ( algo_handle , 0 ) ;
BCryptCloseAlgorithmProvider ( algo_handle , 0 ) ;
if ( BCRYPT_SUCCESS ( ret ) )
if ( BCRYPT_SUCCESS ( ret ) )
return seed ;
return 0 ;
}
}
# endif
# endif
# if HAVE_ARC4RANDOM
# if HAVE_ARC4RANDOM
return arc4random ( ) ;
arc4random_buf ( buf , len ) ;
return 0 ;
# endif
# endif
if ( ! read_random ( & seed , " /dev/urandom " ) )
err = read_random ( buf , len , " /dev/urandom " ) ;
return seed ;
return get_generic_seed ( ) ;
return err ;
}
uint32_t av_get_random_seed ( void )
{
uint32_t seed ;
if ( av_random_bytes ( ( uint8_t * ) & seed , sizeof ( seed ) ) < 0 )
return get_generic_seed ( ) ;
return seed ;
}
}