@ -69,6 +69,7 @@
# include <boost/program_options/detail/config_file.hpp>
# include <boost/program_options/detail/config_file.hpp>
# include <boost/program_options/parsers.hpp>
# include <boost/program_options/parsers.hpp>
# include <openssl/crypto.h>
# include <openssl/crypto.h>
# include <openssl/err.h>
# include <openssl/rand.h>
# include <openssl/rand.h>
// Work around clang compilation problem in Boost 1.46:
// Work around clang compilation problem in Boost 1.46:
@ -141,12 +142,14 @@ public:
}
}
instance_of_cinit ;
instance_of_cinit ;
bool GetRandBytes ( unsigned char * buf , int num )
{
if ( RAND_bytes ( buf , num ) = = 0 ) {
LogPrint ( " rand " , " %s : OpenSSL RAND_bytes() failed with error: %s \n " , __func__ , ERR_error_string ( ERR_get_error ( ) , NULL ) ) ;
return false ;
}
return true ;
}
void RandAddSeed ( )
void RandAddSeed ( )
{
{
@ -207,9 +210,9 @@ uint64_t GetRand(uint64_t nMax)
// to give every possible output value an equal possibility
// to give every possible output value an equal possibility
uint64_t nRange = ( std : : numeric_limits < uint64_t > : : max ( ) / nMax ) * nMax ;
uint64_t nRange = ( std : : numeric_limits < uint64_t > : : max ( ) / nMax ) * nMax ;
uint64_t nRand = 0 ;
uint64_t nRand = 0 ;
do
do {
RAND_b ytes( ( unsigned char * ) & nRand , sizeof ( nRand ) ) ;
GetRandB ytes( ( unsigned char * ) & nRand , sizeof ( nRand ) ) ;
while ( nRand > = nRange ) ;
} while ( nRand > = nRange ) ;
return ( nRand % nMax ) ;
return ( nRand % nMax ) ;
}
}
@ -221,7 +224,7 @@ int GetRandInt(int nMax)
uint256 GetRandHash ( )
uint256 GetRandHash ( )
{
{
uint256 hash ;
uint256 hash ;
RAND_b ytes( ( unsigned char * ) & hash , sizeof ( hash ) ) ;
GetRandB ytes( ( unsigned char * ) & hash , sizeof ( hash ) ) ;
return hash ;
return hash ;
}
}
@ -1196,18 +1199,18 @@ uint32_t insecure_rand_Rz = 11;
uint32_t insecure_rand_Rw = 11 ;
uint32_t insecure_rand_Rw = 11 ;
void seed_insecure_rand ( bool fDeterministic )
void seed_insecure_rand ( bool fDeterministic )
{
{
//The seed values have some unlikely fixed points which we avoid.
// The seed values have some unlikely fixed points which we avoid.
if ( fDeterministic )
if ( fDeterministic )
{
{
insecure_rand_Rz = insecure_rand_Rw = 11 ;
insecure_rand_Rz = insecure_rand_Rw = 11 ;
} else {
} else {
uint32_t tmp ;
uint32_t tmp ;
do {
do {
RAND_b ytes( ( unsigned char * ) & tmp , 4 ) ;
GetRandB ytes( ( unsigned char * ) & tmp , 4 ) ;
} while ( tmp = = 0 | | tmp = = 0x9068ffffU ) ;
} while ( tmp = = 0 | | tmp = = 0x9068ffffU ) ;
insecure_rand_Rz = tmp ;
insecure_rand_Rz = tmp ;
do {
do {
RAND_b ytes( ( unsigned char * ) & tmp , 4 ) ;
GetRandB ytes( ( unsigned char * ) & tmp , 4 ) ;
} while ( tmp = = 0 | | tmp = = 0x464fffffU ) ;
} while ( tmp = = 0 | | tmp = = 0x464fffffU ) ;
insecure_rand_Rw = tmp ;
insecure_rand_Rw = tmp ;
}
}