[+] 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,6 +13,13 @@
namespace Aurora::RNG namespace Aurora::RNG
{ {
// May return any length between 1 and uBytes
using RngGetBytesProvider_f = AuUInt32(*)(AuUInt8 *pBuffer, AuUInt32 uBytes);
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 ReadSecureRNG(Memory::MemoryViewWrite writeView);
AUKN_SYM void ReadFastRNG (Memory::MemoryViewWrite writeView); AUKN_SYM void ReadFastRNG (Memory::MemoryViewWrite writeView);

View File

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