AuroraRuntime/Source/RNG/AuRNG.cpp

64 lines
1.4 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuRNG.cpp
Date: 2021-6-11
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "AuRNG.hpp"
#include "AuRNGEntropy.hpp"
#include "AuWELL.hpp"
namespace Aurora::RNG
{
static WELLRand gWellRand;
RandomUnique_t gFastDevice;
AUKN_SYM void ReadSecureRNG(void *pBuffer, AuUInt32 uBytes)
{
AuUInt32 offset;
AuUInt8 *headPtr;
SysAssert(pBuffer, "Null RNG out buffer");
headPtr = reinterpret_cast<AuUInt8 *>(pBuffer);
offset = 0;
while (offset != uBytes)
{
auto req = uBytes - offset;
auto bytes = RngGetBytes(headPtr + offset, req);
SysAssertExp(bytes, "Couldn't consume {} RNG bytes", req);
offset += bytes;
}
}
AUKN_SYM void ReadFastRNG(void *pBuffer, AuUInt32 uBytes)
{
SysAssert(pBuffer, "Null RNG out buffer");
WELL_NextBytes(&gWellRand, pBuffer, uBytes);
}
static void InitFastRng()
{
AuArray<AuUInt8, 64> maxEntropy;
RngFillArray<false, AuUInt8>(maxEntropy.data(), 64);
gWellRand = WELL_SeedRandBig64(maxEntropy);
RandomDef fast;
fast.SetQuickRng();
gFastDevice = RandomUnique(fast);
}
void Init()
{
EntropyInit();
InitFastRng();
}
void Release()
{
EntropyDeinit();
}
}