mirror of
https://github.com/veracrypt/VeraCrypt.git
synced 2025-11-11 19:08:26 -06:00
Linux/MacOSX: Read at least 32 bytes from /dev/random before allowing it to fail gracefully
This commit is contained in:
@@ -44,12 +44,12 @@ namespace VeraCrypt
|
|||||||
throw_sys_sub_if (random == -1, L"/dev/random");
|
throw_sys_sub_if (random == -1, L"/dev/random");
|
||||||
finally_do_arg (int, random, { close (finally_arg); });
|
finally_do_arg (int, random, { close (finally_arg); });
|
||||||
|
|
||||||
// ensure that we have read /dev/random successfully at least once before continuing
|
// ensure that we have read at least 32 bytes from /dev/random before allowing it to fail gracefully
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
int rndCount = read (random, buffer, buffer.Size());
|
int rndCount = read (random, buffer, buffer.Size());
|
||||||
throw_sys_sub_if ((rndCount == -1) && errno != EAGAIN, L"/dev/random");
|
throw_sys_sub_if ((rndCount == -1) && errno != EAGAIN && errno != ERESTART && errno != EINTR, L"/dev/random");
|
||||||
if (rndCount == -1 && !DevRandomSucceeded)
|
if (rndCount == -1 && (!DevRandomSucceeded || (DevRandomBytesCount < 32)))
|
||||||
{
|
{
|
||||||
// wait 250ms before querying /dev/random again
|
// wait 250ms before querying /dev/random again
|
||||||
::usleep (250 * 1000);
|
::usleep (250 * 1000);
|
||||||
@@ -57,7 +57,12 @@ namespace VeraCrypt
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (rndCount != -1)
|
if (rndCount != -1)
|
||||||
|
{
|
||||||
|
// We count returned bytes untill 32-bytes treshold reached
|
||||||
|
if (DevRandomBytesCount < 32)
|
||||||
|
DevRandomBytesCount += rndCount;
|
||||||
DevRandomSucceeded = true;
|
DevRandomSucceeded = true;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -236,6 +241,7 @@ namespace VeraCrypt
|
|||||||
EnrichedByUser = false;
|
EnrichedByUser = false;
|
||||||
Running = false;
|
Running = false;
|
||||||
DevRandomSucceeded = false;
|
DevRandomSucceeded = false;
|
||||||
|
DevRandomBytesCount = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void RandomNumberGenerator::Test ()
|
void RandomNumberGenerator::Test ()
|
||||||
@@ -274,4 +280,5 @@ namespace VeraCrypt
|
|||||||
size_t RandomNumberGenerator::WriteOffset;
|
size_t RandomNumberGenerator::WriteOffset;
|
||||||
struct rand_data *RandomNumberGenerator::JitterRngCtx = NULL;
|
struct rand_data *RandomNumberGenerator::JitterRngCtx = NULL;
|
||||||
bool RandomNumberGenerator::DevRandomSucceeded = false;
|
bool RandomNumberGenerator::DevRandomSucceeded = false;
|
||||||
|
int RandomNumberGenerator::DevRandomBytesCount = 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,6 +56,7 @@ namespace VeraCrypt
|
|||||||
static size_t WriteOffset;
|
static size_t WriteOffset;
|
||||||
static struct rand_data *JitterRngCtx;
|
static struct rand_data *JitterRngCtx;
|
||||||
static bool DevRandomSucceeded;
|
static bool DevRandomSucceeded;
|
||||||
|
static int DevRandomBytesCount;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user