Linux/macOS: simplify logic of handling /dev/random reading failure (proposed by @Lavode in #1187)

Fixes #1187
This commit is contained in:
Mounir IDRASSI 2023-09-08 09:49:07 +02:00
parent f15052e68d
commit a903049858
No known key found for this signature in database
GPG Key ID: FC1B00364B3FE937
2 changed files with 11 additions and 16 deletions

View File

@ -54,22 +54,20 @@ namespace VeraCrypt
{ {
int rndCount = read (random, buffer, buffer.Size()); int rndCount = read (random, buffer, buffer.Size());
throw_sys_sub_if ((rndCount == -1) && errno != EAGAIN && errno != ERESTART && errno != EINTR, L"/dev/random"); throw_sys_sub_if ((rndCount == -1) && errno != EAGAIN && errno != ERESTART && errno != EINTR, L"/dev/random");
if (rndCount == -1 && (!DevRandomSucceeded || (DevRandomBytesCount < 32))) if (rndCount != -1) {
{ // We count returned bytes until 32-bytes threshold reached
if (DevRandomBytesCount < 32)
DevRandomBytesCount += rndCount;
break;
}
else if (DevRandomBytesCount >= 32) {
// allow /dev/random to fail gracefully since we have enough bytes
break;
}
else {
// wait 250ms before querying /dev/random again // wait 250ms before querying /dev/random again
::usleep (250 * 1000); ::usleep (250 * 1000);
} }
else
{
if (rndCount != -1)
{
// We count returned bytes untill 32-bytes treshold reached
if (DevRandomBytesCount < 32)
DevRandomBytesCount += rndCount;
DevRandomSucceeded = true;
}
break;
}
} }
AddToPool (buffer); AddToPool (buffer);
@ -253,7 +251,6 @@ namespace VeraCrypt
EnrichedByUser = false; EnrichedByUser = false;
Running = false; Running = false;
DevRandomSucceeded = false;
DevRandomBytesCount = 0; DevRandomBytesCount = 0;
} }
@ -292,6 +289,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;
int RandomNumberGenerator::DevRandomBytesCount = 0; int RandomNumberGenerator::DevRandomBytesCount = 0;
} }

View File

@ -55,7 +55,6 @@ 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;
static int DevRandomBytesCount; static int DevRandomBytesCount;
}; };
} }