AuroraRuntime/Source/RNG/AuRNG.cpp
Reece Wilson 124038df62 [*] Refactor public headers
[+] AuConsole::Commands::RemoveCommand
[+] EExtendedUsage::eServerAuth
[+] SysPanic2 for SysSafeLine hints (backtrace may contain optimized SysPanics, making tracing the true origin difficult)
[*] Reworked SysAssertions
[*] AuParse::EncodeHex now takes AuMemoryViewRead
[*] AuRng::ReadSecureRNG now takes AuMemoryViewWrite
[*] AuRng::ReadFastRNG now takes AuMemoryViewWrite
2023-01-15 06:05:22 +00:00

69 lines
1.6 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(Memory::MemoryViewWrite writeView)
{
AuUInt32 offset;
AuUInt8 *headPtr;
auto pBuffer = writeView.ptr;
auto uBytes = writeView.length;
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(Memory::MemoryViewWrite writeView)
{
auto pBuffer = writeView.ptr;
auto uBytes = writeView.length;
SysAssert(pBuffer, "Null fast 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();
}
}