2022-09-19 01:34:57 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
2022-11-17 07:46:07 +00:00
|
|
|
File: AuRNGEntropy.cpp
|
2022-09-19 01:34:57 +00:00
|
|
|
Date: 2022-9-17
|
|
|
|
File: RNG.cpp
|
|
|
|
Date: 2021-6-11
|
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#include <Source/RuntimeInternal.hpp>
|
2022-11-17 07:46:07 +00:00
|
|
|
#include "AuRNG.hpp"
|
|
|
|
#include "AuRNGEntropy.hpp"
|
2022-09-19 01:34:57 +00:00
|
|
|
|
|
|
|
#if defined(AURORA_PLATFORM_WIN32)
|
|
|
|
#include <bcrypt.h>
|
2023-07-09 10:26:17 +00:00
|
|
|
|
|
|
|
#if !defined(BCRYPT_RNG_ALG_HANDLE)
|
|
|
|
#define BCRYPT_RNG_ALG_HANDLE ((BCRYPT_ALG_HANDLE) 0x00000081)
|
|
|
|
#endif
|
|
|
|
|
2023-08-18 21:38:17 +00:00
|
|
|
#define AURORA_RNG_HAS_OLD_WINCRYPT
|
2023-07-09 10:26:17 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(AURORA_IS_MODERNNT_DERIVED)
|
2022-09-19 01:34:57 +00:00
|
|
|
#include <bcrypt.h>
|
|
|
|
#endif
|
|
|
|
|
2023-08-18 21:38:17 +00:00
|
|
|
#if defined(AURORA_RNG_HAS_OLD_WINCRYPT)
|
2023-07-09 10:26:17 +00:00
|
|
|
#include <wincrypt.h>
|
|
|
|
#endif
|
|
|
|
|
2022-09-19 01:34:57 +00:00
|
|
|
#if defined(AURORA_IS_POSIX_DERIVED)
|
|
|
|
#include <cstdio>
|
2022-12-16 00:41:01 +00:00
|
|
|
#include <fcntl.h>
|
2022-09-19 01:34:57 +00:00
|
|
|
#endif
|
|
|
|
|
2023-08-18 21:38:17 +00:00
|
|
|
#if defined(AURORA_IS_BSD_DERIVED)
|
|
|
|
#include <stdlib.h>
|
|
|
|
#define AURORA_RND_HAS_ARC4
|
|
|
|
#endif
|
|
|
|
|
2022-11-17 07:46:07 +00:00
|
|
|
#include "AuWELL.hpp"
|
2022-09-19 01:34:57 +00:00
|
|
|
|
|
|
|
namespace Aurora::RNG
|
|
|
|
{
|
|
|
|
#if defined(AURORA_IS_POSIX_DERIVED)
|
2023-07-10 19:56:45 +00:00
|
|
|
static int gDevURand { -1 };
|
2022-09-19 01:34:57 +00:00
|
|
|
|
|
|
|
void EntropyInit()
|
|
|
|
{
|
2022-12-16 03:48:04 +00:00
|
|
|
gDevURand = ::open("/dev/urandom", O_RDONLY | O_CLOEXEC);
|
2022-09-19 01:34:57 +00:00
|
|
|
if (gDevURand <= 0)
|
|
|
|
{
|
2022-12-16 03:48:04 +00:00
|
|
|
gDevURand = ::open("/dev/random", O_RDONLY | O_CLOEXEC);
|
2022-09-19 01:34:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (gDevURand <= 0)
|
|
|
|
{
|
|
|
|
gDevURand = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-17 07:46:07 +00:00
|
|
|
static AuUInt32 RngUnix(AuUInt8 *pBuf, AuUInt32 uLen)
|
2022-09-19 01:34:57 +00:00
|
|
|
{
|
|
|
|
if (gDevURand == -1)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-08-18 21:38:17 +00:00
|
|
|
int iRet = ::read(gDevURand, pBuf, uLen);
|
|
|
|
if (iRet <= 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return AuUInt32(iRet);
|
2022-09-19 01:34:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(AURORA_IS_MODERNNT_DERIVED)
|
|
|
|
|
2023-08-18 21:38:17 +00:00
|
|
|
#if defined(AURORA_RNG_HAS_OLD_WINCRYPT)
|
2022-09-19 01:34:57 +00:00
|
|
|
static HCRYPTPROV gCryptoProv;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void EntropyInit()
|
|
|
|
{
|
2023-08-18 21:38:17 +00:00
|
|
|
#if defined(AURORA_RNG_HAS_OLD_WINCRYPT)
|
2023-07-09 10:26:17 +00:00
|
|
|
if (pBCryptGenRandom)
|
2022-09-19 01:34:57 +00:00
|
|
|
{
|
2023-07-09 10:26:17 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-11 16:58:20 +00:00
|
|
|
if (pRtlGenRandom)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-09 10:26:17 +00:00
|
|
|
if (pCryptAcquireContextW)
|
|
|
|
{
|
|
|
|
if (!pCryptAcquireContextW(&gCryptoProv, NULL,
|
|
|
|
MS_DEF_PROV_W, PROV_RSA_FULL,
|
|
|
|
(CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET)))
|
2022-09-19 01:34:57 +00:00
|
|
|
{
|
2023-07-09 10:26:17 +00:00
|
|
|
if (!pCryptAcquireContextW(&gCryptoProv, NULL,
|
|
|
|
MS_DEF_PROV_W, PROV_RSA_FULL,
|
|
|
|
CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET | CRYPT_NEWKEYSET))
|
|
|
|
{
|
|
|
|
gCryptoProv = 0;
|
|
|
|
}
|
2022-09-19 01:34:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-11-17 07:46:07 +00:00
|
|
|
static AuUInt32 RngWin32(AuUInt8 *pBuf, AuUInt32 uLen)
|
2022-09-19 01:34:57 +00:00
|
|
|
{
|
2023-07-09 10:26:17 +00:00
|
|
|
if (pBCryptGenRandom)
|
2022-09-19 01:34:57 +00:00
|
|
|
{
|
2023-07-11 16:58:20 +00:00
|
|
|
if (AuSwInfo::IsWindows10OrGreater() ||
|
|
|
|
AuBuild::kCurrentPlatform != AuBuild::EPlatform::ePlatformWin32)
|
2022-09-19 01:34:57 +00:00
|
|
|
{
|
2023-07-11 16:58:20 +00:00
|
|
|
if (pBCryptGenRandom(BCRYPT_RNG_ALG_HANDLE, reinterpret_cast<PUCHAR>(pBuf), uLen, 0) == 0)
|
|
|
|
{
|
|
|
|
return uLen;
|
|
|
|
}
|
2022-09-19 01:34:57 +00:00
|
|
|
}
|
|
|
|
|
2023-10-21 06:23:56 +00:00
|
|
|
if (pRtlGenRandom &&
|
|
|
|
pRtlGenRandom(pBuf, uLen))
|
2023-10-08 13:51:12 +00:00
|
|
|
{
|
|
|
|
return uLen;
|
|
|
|
}
|
|
|
|
|
2023-07-09 10:26:17 +00:00
|
|
|
if (pBCryptGenRandom(NULL, reinterpret_cast<PUCHAR>(pBuf), uLen, BCRYPT_USE_SYSTEM_PREFERRED_RNG) == 0)
|
|
|
|
{
|
|
|
|
return uLen;
|
|
|
|
}
|
|
|
|
}
|
2023-10-08 13:51:12 +00:00
|
|
|
else if (pRtlGenRandom &&
|
|
|
|
pRtlGenRandom(pBuf, uLen))
|
|
|
|
{
|
|
|
|
return uLen;
|
|
|
|
}
|
2023-07-09 10:26:17 +00:00
|
|
|
|
2023-08-18 21:38:17 +00:00
|
|
|
#if defined(AURORA_RNG_HAS_OLD_WINCRYPT)
|
2023-07-09 10:26:17 +00:00
|
|
|
if (gCryptoProv)
|
2022-09-19 01:34:57 +00:00
|
|
|
{
|
2023-07-09 10:26:17 +00:00
|
|
|
if (pCryptGenRandom(gCryptoProv, uLen, pBuf))
|
|
|
|
{
|
|
|
|
return uLen;
|
|
|
|
}
|
2022-09-19 01:34:57 +00:00
|
|
|
}
|
2023-07-09 10:26:17 +00:00
|
|
|
#endif
|
2022-09-19 01:34:57 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
void EntropyInit()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2023-07-11 16:58:20 +00:00
|
|
|
static AuUInt64 RngTimeClock()
|
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
return clock(); // [...] resolution of 1 posix clock() tick (this is usually 1,000,000 == CLOCKS_PER_SEC per posix, but it can be 1k)
|
|
|
|
#else
|
2023-10-07 13:50:32 +00:00
|
|
|
static AuUInt64 gTimeHolder;
|
|
|
|
static AuUInt64 gTime = (gTimeHolder = AuTime::SteadyClockJiffies()) >= 10'000'000ull ?
|
|
|
|
1 :
|
2023-10-08 13:51:12 +00:00
|
|
|
AuPageRoundUp<AuUInt64>(10'000'000ull, 1024) / gTimeHolder;
|
2023-10-07 13:50:32 +00:00
|
|
|
return AuTime::SteadyClockNS() * gTime / 10'000ull;
|
2023-07-11 16:58:20 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static AuUInt32 RngTimeBased(AuUInt8 *pBuf, AuUInt32 uLen)
|
2022-09-19 01:34:57 +00:00
|
|
|
{
|
2023-07-11 16:58:20 +00:00
|
|
|
AuUInt64 t1;
|
2023-07-13 04:58:53 +00:00
|
|
|
int acc, bits, a, b, c;
|
2022-09-19 01:34:57 +00:00
|
|
|
|
2023-07-11 16:58:20 +00:00
|
|
|
acc = a = b = c = 0;
|
2022-09-19 01:34:57 +00:00
|
|
|
bits = 8;
|
|
|
|
|
2023-08-11 15:51:42 +00:00
|
|
|
void *pASLRSeed = (void *)&RngTimeBased;
|
2023-07-11 16:58:20 +00:00
|
|
|
|
2023-07-13 04:58:53 +00:00
|
|
|
for (AU_ITERATE_N(uOffsetInByteStream, uLen))
|
2022-09-19 01:34:57 +00:00
|
|
|
{
|
2023-07-13 04:58:53 +00:00
|
|
|
for (AU_ITERATE_N(uMultiplePassesForTheFunOfIt, 3))
|
2022-09-19 01:34:57 +00:00
|
|
|
{
|
2023-07-13 04:58:53 +00:00
|
|
|
while (bits--) // for each bit in byte
|
2022-09-19 01:34:57 +00:00
|
|
|
{
|
2023-07-13 04:58:53 +00:00
|
|
|
do
|
2022-09-19 01:34:57 +00:00
|
|
|
{
|
2023-07-13 04:58:53 +00:00
|
|
|
t1 = RngTimeClock();
|
2023-09-09 12:50:26 +00:00
|
|
|
while (t1 == RngTimeClock()) // spin within 1 microseconds (*)
|
2023-07-13 04:58:53 +00:00
|
|
|
{
|
|
|
|
a ^= 1; // flip
|
|
|
|
}
|
|
|
|
|
|
|
|
t1 = RngTimeClock();
|
|
|
|
while (t1 == RngTimeClock()) // spin within 1 microseconds
|
|
|
|
{
|
|
|
|
b ^= 1; // flip
|
|
|
|
}
|
2023-09-09 12:50:26 +00:00
|
|
|
|
|
|
|
// Update 2023/Sept: Now x10 because we dont have enough timer res on some platforms.
|
|
|
|
// This is was generally never a CPU limitation or API limitation until Windows XP. Check software or motherboard...
|
|
|
|
// ...but still, let's continue.
|
|
|
|
// Deadlocking preprocess init, because for example the motherboard is failing or a VM configuration changed, is pretty dumb.
|
|
|
|
// (ask me how i know)
|
2023-10-07 13:50:32 +00:00
|
|
|
// Update oct: it scales with whatever frequency user-land clocks can be trusted with, per the respective kernels' HAL (viso, kshared user data, etc).
|
2022-09-19 01:34:57 +00:00
|
|
|
}
|
2023-07-13 04:58:53 +00:00
|
|
|
while (a == b); // ensure theres enough entropy for a deviation to occur
|
|
|
|
acc = (acc << 1) | a; // push the first bit state
|
2023-07-11 16:58:20 +00:00
|
|
|
}
|
2022-09-19 01:34:57 +00:00
|
|
|
|
2023-08-18 14:53:38 +00:00
|
|
|
c = AuHashCode(acc) ^
|
|
|
|
(AuHashCode(c) * kFnv1MagicPrime32) ^
|
|
|
|
(a ? AuHashCode(pASLRSeed) : 0);
|
2023-08-29 00:07:59 +00:00
|
|
|
|
|
|
|
acc = 0;
|
|
|
|
bits = 8;
|
2023-07-13 04:58:53 +00:00
|
|
|
}
|
2022-09-19 01:34:57 +00:00
|
|
|
|
2023-07-13 04:58:53 +00:00
|
|
|
*pBuf++ = AuUInt8(c);
|
2022-09-19 01:34:57 +00:00
|
|
|
}
|
|
|
|
|
2023-07-13 04:58:53 +00:00
|
|
|
return uLen;
|
2022-09-19 01:34:57 +00:00
|
|
|
}
|
|
|
|
|
2022-11-17 07:46:07 +00:00
|
|
|
AuUInt32 RngGetBytes(AuUInt8 *pBuffer, AuUInt32 uBytes)
|
2022-09-19 01:34:57 +00:00
|
|
|
{
|
|
|
|
AuUInt32 x;
|
|
|
|
#if defined(AURORA_IS_MODERNNT_DERIVED)
|
2022-11-17 07:46:07 +00:00
|
|
|
x = RngWin32(pBuffer, uBytes);
|
2022-09-19 01:34:57 +00:00
|
|
|
if (x != 0)
|
|
|
|
{
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
#elif defined(AURORA_IS_POSIX_DERIVED)
|
2023-08-18 21:38:17 +00:00
|
|
|
#if defined(AURORA_IS_LINUX_DERIVED)
|
|
|
|
x = sys_getrandom(pBuffer, uBytes);
|
|
|
|
if (x > 0)
|
|
|
|
{
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if defined(AURORA_RND_HAS_ARC4)
|
|
|
|
arc4random_buf(pBuffer, uBytes);
|
|
|
|
return uBytes;
|
|
|
|
#endif
|
2022-12-16 00:41:01 +00:00
|
|
|
x = RngUnix(pBuffer, uBytes);
|
2022-09-19 01:34:57 +00:00
|
|
|
if (x != 0)
|
|
|
|
{
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
#endif
|
2023-07-11 16:58:20 +00:00
|
|
|
x = RngTimeBased(pBuffer, uBytes);
|
2022-09-19 01:34:57 +00:00
|
|
|
if (x != 0)
|
|
|
|
{
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EntropyDeinit()
|
|
|
|
{
|
|
|
|
#if defined(AURORA_IS_POSIX_DERIVED)
|
2022-12-16 17:18:12 +00:00
|
|
|
if (gDevURand > 0)
|
2022-09-19 01:34:57 +00:00
|
|
|
{
|
|
|
|
::close(gDevURand);
|
2023-07-10 19:56:45 +00:00
|
|
|
gDevURand = -1;
|
2022-09-19 01:34:57 +00:00
|
|
|
}
|
2023-08-18 21:38:17 +00:00
|
|
|
#elif defined(AURORA_IS_MODERNNT_DERIVED) && defined(AURORA_RNG_HAS_OLD_WINCRYPT)
|
2023-07-09 10:26:17 +00:00
|
|
|
if (pBCryptGenRandom)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-11 16:58:20 +00:00
|
|
|
if (pRtlGenRandom)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-09 10:26:17 +00:00
|
|
|
if (pCryptReleaseContext)
|
|
|
|
{
|
|
|
|
pCryptReleaseContext(gCryptoProv, 0);
|
|
|
|
}
|
2022-09-19 01:34:57 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|