diff options
Diffstat (limited to 'src/Core/RandomNumberGenerator.cpp')
-rw-r--r-- | src/Core/RandomNumberGenerator.cpp | 46 |
1 files changed, 27 insertions, 19 deletions
diff --git a/src/Core/RandomNumberGenerator.cpp b/src/Core/RandomNumberGenerator.cpp index 4451348e..3fb6062a 100644 --- a/src/Core/RandomNumberGenerator.cpp +++ b/src/Core/RandomNumberGenerator.cpp @@ -54,22 +54,20 @@ namespace VeraCrypt { int rndCount = read (random, buffer, buffer.Size()); throw_sys_sub_if ((rndCount == -1) && errno != EAGAIN && errno != ERESTART && errno != EINTR, L"/dev/random"); - if (rndCount == -1 && (!DevRandomSucceeded || (DevRandomBytesCount < 32))) - { - // wait 250ms before querying /dev/random again - ::usleep (250 * 1000); + if (rndCount != -1) { + // We count returned bytes until 32-bytes threshold reached + if (DevRandomBytesCount < 32) + DevRandomBytesCount += rndCount; + break; } - else - { - if (rndCount != -1) - { - // We count returned bytes untill 32-bytes treshold reached - if (DevRandomBytesCount < 32) - DevRandomBytesCount += rndCount; - DevRandomSucceeded = true; - } + else if (DevRandomBytesCount >= 32) { + // allow /dev/random to fail gracefully since we have enough bytes break; } + else { + // wait 250ms before querying /dev/random again + ::usleep (250 * 1000); + } } AddToPool (buffer); @@ -253,14 +251,17 @@ namespace VeraCrypt EnrichedByUser = false; Running = false; - DevRandomSucceeded = false; DevRandomBytesCount = 0; } void RandomNumberGenerator::Test () { shared_ptr <Hash> origPoolHash = PoolHash; - PoolHash.reset (new Blake2s()); + #ifndef WOLFCRYPT_BACKEND + PoolHash.reset (new Blake2s()); + #else + PoolHash.reset (new Sha256()); + #endif Pool.Zero(); Buffer buffer (1); @@ -270,15 +271,23 @@ namespace VeraCrypt AddToPool (buffer); } + #ifndef WOLFCRYPT_BACKEND if (Crc32::ProcessBuffer (Pool) != 0x9c743238) - throw TestFailed (SRC_POS); + #else + if (Crc32::ProcessBuffer (Pool) != 0xac95ac1a) + #endif + throw TestFailed (SRC_POS); buffer.Allocate (PoolSize); buffer.CopyFrom (PeekPool()); AddToPool (buffer); - if (Crc32::ProcessBuffer (Pool) != 0xd2d09c8d) - throw TestFailed (SRC_POS); + #ifndef WOLFCRYPT_BACKEND + if (Crc32::ProcessBuffer (Pool) != 0xd2d09c8d) + #else + if (Crc32::ProcessBuffer (Pool) != 0xb79f3c12) + #endif + throw TestFailed (SRC_POS); PoolHash = origPoolHash; } @@ -292,6 +301,5 @@ namespace VeraCrypt bool RandomNumberGenerator::Running = false; size_t RandomNumberGenerator::WriteOffset; struct rand_data *RandomNumberGenerator::JitterRngCtx = NULL; - bool RandomNumberGenerator::DevRandomSucceeded = false; int RandomNumberGenerator::DevRandomBytesCount = 0; } |