1
0
mirror of https://github.com/veracrypt/VeraCrypt.git synced 2025-11-11 11:08:02 -06:00

Linux/MacOSX: Don't always ignore /dev/random failure by making sure that it has returned random bytes successfully at least once during the lifetime of RandomNumberGenerator

This commit is contained in:
Mounir IDRASSI
2020-06-19 03:30:05 +02:00
parent f765860dfb
commit 4f1de9666a
2 changed files with 21 additions and 1 deletions

View File

@@ -44,7 +44,24 @@ 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); });
throw_sys_sub_if (read (random, buffer, buffer.Size()) == -1 && errno != EAGAIN, L"/dev/random"); // ensure that we have read /dev/random successfully at least once before continuing
while (true)
{
int rndCount = read (random, buffer, buffer.Size());
throw_sys_sub_if ((rndCount == -1) && errno != EAGAIN, L"/dev/random");
if (rndCount == -1 && !DevRandomSucceeded)
{
// wait 250ms before querying /dev/random again
::usleep (250 * 1000);
}
else
{
if (rndCount != -1)
DevRandomSucceeded = true;
break;
}
}
AddToPool (buffer); AddToPool (buffer);
/* use JitterEntropy library to get good quality random bytes based on CPU timing jitter */ /* use JitterEntropy library to get good quality random bytes based on CPU timing jitter */
@@ -218,6 +235,7 @@ namespace VeraCrypt
EnrichedByUser = false; EnrichedByUser = false;
Running = false; Running = false;
DevRandomSucceeded = false;
} }
void RandomNumberGenerator::Test () void RandomNumberGenerator::Test ()
@@ -255,4 +273,5 @@ namespace VeraCrypt
bool RandomNumberGenerator::Running = false; bool RandomNumberGenerator::Running = false;
size_t RandomNumberGenerator::WriteOffset; size_t RandomNumberGenerator::WriteOffset;
struct rand_data *RandomNumberGenerator::JitterRngCtx = NULL; struct rand_data *RandomNumberGenerator::JitterRngCtx = NULL;
bool RandomNumberGenerator::DevRandomSucceeded = false;
} }

View File

@@ -55,6 +55,7 @@ namespace VeraCrypt
static bool Running; static bool Running;
static size_t WriteOffset; static size_t WriteOffset;
static struct rand_data *JitterRngCtx; static struct rand_data *JitterRngCtx;
static bool DevRandomSucceeded;
}; };
} }