From 7ae63335ac1a6179807a73f4794b7f279c05e85c Mon Sep 17 00:00:00 2001 From: Mounir IDRASSI Date: Sat, 27 May 2023 17:24:11 +0200 Subject: Linux/macOS: Make RNG implementation match documentation and the Windows implementation --- src/Core/RandomNumberGenerator.cpp | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/Core/RandomNumberGenerator.cpp b/src/Core/RandomNumberGenerator.cpp index 2473ef2f..4451348e 100644 --- a/src/Core/RandomNumberGenerator.cpp +++ b/src/Core/RandomNumberGenerator.cpp @@ -187,19 +187,26 @@ namespace VeraCrypt void RandomNumberGenerator::HashMixPool () { BytesAddedSincePoolHashMix = 0; - - for (size_t poolPos = 0; poolPos < Pool.Size(); ) + size_t digestSize = PoolHash->GetDigestSize(); + size_t poolSize = Pool.Size(); + // pool size must be multiple of digest size + // this is always the case with default pool size value (320 bytes) + if (poolSize % digestSize) + throw AssertionFailed (SRC_POS); + + for (size_t poolPos = 0; poolPos < poolSize; poolPos += digestSize) { // Compute the message digest of the entire pool using the selected hash function - SecureBuffer digest (PoolHash->GetDigestSize()); + SecureBuffer digest (digestSize); PoolHash->Init(); PoolHash->ProcessData (Pool); PoolHash->GetDigest (digest); - // Add the message digest to the pool - for (size_t digestPos = 0; digestPos < digest.Size() && poolPos < Pool.Size(); ++digestPos) + /* XOR the resultant message digest to the pool at the poolIndex position. */ + /* this matches the documentation: https://veracrypt.fr/en/Random%20Number%20Generator.html */ + for (size_t digestIndex = 0; digestIndex < digestSize; digestIndex++) { - Pool[poolPos++] += digest[digestPos]; + Pool [poolPos + digestIndex] ^= digest [digestIndex]; } } } @@ -263,14 +270,14 @@ namespace VeraCrypt AddToPool (buffer); } - if (Crc32::ProcessBuffer (Pool) != 0x21CED8B7) + if (Crc32::ProcessBuffer (Pool) != 0x9c743238) throw TestFailed (SRC_POS); buffer.Allocate (PoolSize); buffer.CopyFrom (PeekPool()); AddToPool (buffer); - if (Crc32::ProcessBuffer (Pool) != 0xDCFD0A83) + if (Crc32::ProcessBuffer (Pool) != 0xd2d09c8d) throw TestFailed (SRC_POS); PoolHash = origPoolHash; -- cgit v1.2.3