[+] AuRNG::SetSecureRNGBackendOverload

[+] AuRNG::GetSecureRNGBackendOverload
[+] AuRNG::GetSecureRNGBackend
This commit is contained in:
Reece Wilson 2024-02-25 19:41:59 +00:00
parent 8c101cea6f
commit e199172428
2 changed files with 60 additions and 27 deletions

View File

@ -13,30 +13,37 @@
namespace Aurora::RNG
{
AUKN_SYM void ReadSecureRNG(Memory::MemoryViewWrite writeView);
AUKN_SYM void ReadFastRNG (Memory::MemoryViewWrite writeView);
AUKN_SYM AuString ReadString(AuUInt32 uLength, ERngStringCharacters type = ERngStringCharacters::eAlphaCharacters);
AUKN_SYM void RngString(char *pString, AuUInt32 uLength, ERngStringCharacters type = ERngStringCharacters::eAlphaCharacters);
// May return any length between 1 and uBytes
using RngGetBytesProvider_f = AuUInt32(*)(AuUInt8 *pBuffer, AuUInt32 uBytes);
AUKN_SYM AuUInt8 RngByte();
AUKN_SYM bool RngBoolean();
AUKN_SYM AuUInt32 RngU32();
AUKN_SYM AuUInt32 RngU32Range(AuUInt32 uMin, AuUInt32 uMax);
AUKN_SYM AuUInt64 RngU64();
AUKN_SYM AuInt32 RngI32Range(AuInt32 iMin, AuInt32 iMax);
AUKN_SYM double RngDecimal();
AUKN_SYM double RngNumber(double dMin, double dMax);
AUKN_SYM AuUInt32 RngIndex(AuUInt32 uCount /* = max + 1*/);
AUKN_SYM uuids::uuid RngUUID();
AUKN_SYM AuList<AuInt32> RngArrayI32Range(AuUInt32 uCount, AuInt32 iMin, AuInt32 iMax);
AUKN_SYM AuList<AuUInt32> RngArrayU32Range(AuUInt32 uCount, AuUInt32 uMin, AuUInt32 uMax);
AUKN_SYM AuList<double> RngArrayDoubleRange(AuUInt32 uCount, double dMin, double dMax);
AUKN_SYM AuList<AuInt32> RngArrayI32(AuUInt32 uCount);
AUKN_SYM AuList<AuUInt32> RngArrayU32(AuUInt32 uCount);
AUKN_SYM AuList<double> RngArrayDouble(AuUInt32 uCount);
AUKN_SYM AuList<double> RngArrayDecimals(AuUInt32 uCount);
AUKN_SYM AuList<uuids::uuid> RngArrayUUIDs(AuUInt32 uCount);
AUKN_SYM void SetSecureRNGBackendOverload(RngGetBytesProvider_f pSecureBackend);
AUKN_SYM RngGetBytesProvider_f GetSecureRNGBackendOverload();
AUKN_SYM RngGetBytesProvider_f GetSecureRNGBackend();
AUKN_SYM void ReadSecureRNG(Memory::MemoryViewWrite writeView);
AUKN_SYM void ReadFastRNG (Memory::MemoryViewWrite writeView);
AUKN_SYM AuString ReadString(AuUInt32 uLength, ERngStringCharacters type = ERngStringCharacters::eAlphaCharacters);
AUKN_SYM void RngString(char *pString, AuUInt32 uLength, ERngStringCharacters type = ERngStringCharacters::eAlphaCharacters);
AUKN_SYM AuUInt8 RngByte();
AUKN_SYM bool RngBoolean();
AUKN_SYM AuUInt32 RngU32();
AUKN_SYM AuUInt32 RngU32Range(AuUInt32 uMin, AuUInt32 uMax);
AUKN_SYM AuUInt64 RngU64();
AUKN_SYM AuInt32 RngI32Range(AuInt32 iMin, AuInt32 iMax);
AUKN_SYM double RngDecimal();
AUKN_SYM double RngNumber(double dMin, double dMax);
AUKN_SYM AuUInt32 RngIndex(AuUInt32 uCount /* = max + 1*/);
AUKN_SYM uuids::uuid RngUUID();
AUKN_SYM AuList<AuInt32> RngArrayI32Range(AuUInt32 uCount, AuInt32 iMin, AuInt32 iMax);
AUKN_SYM AuList<AuUInt32> RngArrayU32Range(AuUInt32 uCount, AuUInt32 uMin, AuUInt32 uMax);
AUKN_SYM AuList<double> RngArrayDoubleRange(AuUInt32 uCount, double dMin, double dMax);
AUKN_SYM AuList<AuInt32> RngArrayI32(AuUInt32 uCount);
AUKN_SYM AuList<AuUInt32> RngArrayU32(AuUInt32 uCount);
AUKN_SYM AuList<double> RngArrayDouble(AuUInt32 uCount);
AUKN_SYM AuList<double> RngArrayDecimals(AuUInt32 uCount);
AUKN_SYM AuList<uuids::uuid> RngArrayUUIDs(AuUInt32 uCount);
// Note: it is conceivable that someone may want the following templates for some cryptographic purpose
template<bool fast = true, typename T, int N>

View File

@ -43,6 +43,8 @@
namespace Aurora::RNG
{
static RngGetBytesProvider_f gSecureBackend;
#if defined(AURORA_IS_POSIX_DERIVED)
static int gDevURand { -1 };
@ -232,9 +234,33 @@ namespace Aurora::RNG
return uLen;
}
AUKN_SYM void SetSecureRNGBackendOverload(RngGetBytesProvider_f pSecureBackend)
{
gSecureBackend = pSecureBackend;
}
AUKN_SYM RngGetBytesProvider_f GetSecureRNGBackendOverload()
{
return gSecureBackend;
}
AUKN_SYM RngGetBytesProvider_f GetSecureRNGBackend()
{
return gSecureBackend ? gSecureBackend : RngGetBytes;
}
AuUInt32 RngGetBytes(AuUInt8 *pBuffer, AuUInt32 uBytes)
{
AuUInt32 x;
if (gSecureBackend)
{
x = gSecureBackend(pBuffer, uBytes);
if (x != 0)
{
return x;
}
}
#if defined(AURORA_IS_MODERNNT_DERIVED)
x = RngWin32(pBuffer, uBytes);
if (x != 0)
@ -242,6 +268,10 @@ namespace Aurora::RNG
return x;
}
#elif defined(AURORA_IS_POSIX_DERIVED)
#if defined(AURORA_RND_HAS_ARC4)
arc4random_buf(pBuffer, uBytes);
return uBytes;
#endif
#if defined(AURORA_IS_LINUX_DERIVED)
x = sys_getrandom(pBuffer, uBytes);
if (x > 0)
@ -249,10 +279,6 @@ namespace Aurora::RNG
return x;
}
#endif
#if defined(AURORA_RND_HAS_ARC4)
arc4random_buf(pBuffer, uBytes);
return uBytes;
#endif
x = RngUnix(pBuffer, uBytes);
if (x != 0)
{