From 115ee1a9ec494b10d9d6c5c995cc36305383d7b1 Mon Sep 17 00:00:00 2001 From: Jamie Reece Wilson Date: Thu, 13 Jul 2023 05:58:53 +0100 Subject: [PATCH] [*] Improve time based RNG failover implementation --- Source/RNG/AuRNGEntropy.cpp | 50 +++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/Source/RNG/AuRNGEntropy.cpp b/Source/RNG/AuRNGEntropy.cpp index ae4b0759..1d106313 100644 --- a/Source/RNG/AuRNGEntropy.cpp +++ b/Source/RNG/AuRNGEntropy.cpp @@ -170,47 +170,49 @@ namespace Aurora::RNG static AuUInt32 RngTimeBased(AuUInt8 *pBuf, AuUInt32 uLen) { AuUInt64 t1; - int l, acc, bits, a, b, c; + int acc, bits, a, b, c; - l = uLen; acc = a = b = c = 0; bits = 8; void *pASLRSeed = &RngTimeBased; - while (uLen--) + for (AU_ITERATE_N(uOffsetInByteStream, uLen)) { - while (bits--) // for each bit in byte + for (AU_ITERATE_N(uMultiplePassesForTheFunOfIt, 3)) { - do + while (bits--) // for each bit in byte { - t1 = RngTimeClock(); - while (t1 == RngTimeClock()) // spin within 1 microseconds + do { - a ^= 1; // flip - } + t1 = RngTimeClock(); + while (t1 == RngTimeClock()) // spin within 1 microseconds + { + a ^= 1; // flip + } - t1 = RngTimeClock(); - while (t1 == RngTimeClock()) // spin within 1 microseconds - { - b ^= 1; // flip + t1 = RngTimeClock(); + while (t1 == RngTimeClock()) // spin within 1 microseconds + { + b ^= 1; // flip + } } + while (a == b); // ensure theres enough entropy for a deviation to occur + acc = (acc << 1) | a; // push the first bit state } - while (a == b); // ensure theres enough entropy for a deviation to occur - acc = (acc << 1) | a; // push the first bit state + + acc = 0; + bits = 8; + + c = AuFnv1a32Runtime(&acc, sizeof(acc)) ^ + (AuFnv1a32Runtime(&c, sizeof(c)) * kFnv1MagicPrime32) ^ + (a ? AuFnv1a32Runtime(&pASLRSeed, sizeof(pASLRSeed)) : 0); } - *pBuf = AuFnv1a32Runtime(&acc, sizeof(acc)) ^ - AuFnv1a32Runtime(&c, sizeof(c)) ^ - AuFnv1a32Runtime(&pASLRSeed, sizeof(pASLRSeed)); - - c = *pBuf++; - - acc = 0; - bits = 8; + *pBuf++ = AuUInt8(c); } - return l; + return uLen; } AuUInt32 RngGetBytes(AuUInt8 *pBuffer, AuUInt32 uBytes)