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