Tweak device random stuff for Linux
This commit is contained in:
parent
eec812814a
commit
60fede7678
|
@ -1241,37 +1241,34 @@ cHashedKey192::~cHashedKey192()
|
||||||
RandomizeBytes(mKey, KEYLEN);
|
RandomizeBytes(mKey, KEYLEN);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
#if HAVE_DEVICE_RANDOM
|
||||||
|
|
||||||
static bool randomize_by_device(const char* device_name, int8* destbuf, int len)
|
static bool randomize_by_device(const char* device_name, int8* destbuf, int len)
|
||||||
{
|
{
|
||||||
int dev_random = open("/dev/random", O_RDONLY|O_NONBLOCK);
|
int rng_device = open(device_name, O_RDONLY|O_NONBLOCK);
|
||||||
if (dev_random >= 0)
|
if (rng_device >= 0)
|
||||||
{
|
{
|
||||||
int bytes_read = read(dev_random, destbuf, len);
|
int bytes_read = read(rng_device, destbuf, len);
|
||||||
close(dev_random);
|
close(rng_device);
|
||||||
if (bytes_read == len)
|
if (bytes_read == len)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
static bool gRandomizeBytesSeeded = false;
|
||||||
|
#endif
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
// void RandomizeBytes(byte* destbuf, int len) -- Fill a buffer with random bytes
|
// void RandomizeBytes(byte* destbuf, int len) -- Fill a buffer with random bytes
|
||||||
|
|
||||||
static bool gRandomizeBytesSeeded = false;
|
|
||||||
|
|
||||||
void RandomizeBytes(int8* destbuf, int len)
|
void RandomizeBytes(int8* destbuf, int len)
|
||||||
{
|
{
|
||||||
#if HAVE_DEVICE_RANDOM
|
#if HAVE_DEVICE_RANDOM
|
||||||
|
|
||||||
#if HAVE_DEV_RANDOM
|
|
||||||
if (randomize_by_device("/dev/random", destbuf, len))
|
|
||||||
return;
|
|
||||||
|
|
||||||
iUserNotify::GetInstance()->Notify( iUserNotify::V_NORMAL, "Could not read from /dev/random, falling back to /dev/urandom");
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if HAVE_DEV_URANDOM
|
#if HAVE_DEV_URANDOM
|
||||||
if (randomize_by_device("/dev/urandom", destbuf, len))
|
if (randomize_by_device("/dev/urandom", destbuf, len))
|
||||||
return;
|
return;
|
||||||
|
@ -1282,6 +1279,11 @@ void RandomizeBytes(int8* destbuf, int len)
|
||||||
return;
|
return;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if HAVE_DEV_RANDOM
|
||||||
|
if (randomize_by_device("/dev/random", destbuf, len))
|
||||||
|
return;
|
||||||
|
#endif
|
||||||
|
|
||||||
ThrowAndAssert(eInternal(_T("Failed to read from any RNG devices")));
|
ThrowAndAssert(eInternal(_T("Failed to read from any RNG devices")));
|
||||||
|
|
||||||
// TODO: OpenSSL or other impls that are better than the default one
|
// TODO: OpenSSL or other impls that are better than the default one
|
||||||
|
|
Loading…
Reference in New Issue