[+] Linux and BSD specific RNG

[*] Improved UNIX rng read check
This commit is contained in:
Reece Wilson 2023-08-18 22:38:17 +01:00
parent ebce7e0048
commit 836edbabdd
3 changed files with 51 additions and 9 deletions

View File

@ -8,6 +8,7 @@
#include <RuntimeInternal.hpp>
#include <sys/syscall.h>
#include <linux/futex.h>
#include <sys/random.h>
namespace Aurora
{
@ -185,4 +186,23 @@ namespace Aurora
{
return syscall(SYS_io_cancel, ctx_id, iocb, result);
}
ssize_t sys_getrandom(void *pBuffer, size_t uLength)
{
static AuUInt32 gShouldNotGetRand {};
ssize_t ret {};
if (gShouldNotGetRand)
{
return ENOSYS;
}
ret = syscall(SYS_getrandom, pBuffer, uLength, 1);
if (ret == ENOSYS)
{
gShouldNotGetRand = 1;
}
return ret;
}
}

View File

@ -7,7 +7,6 @@
***/
#pragma once
#include <linux/aio_abi.h>
struct robust_list_head;
@ -39,7 +38,6 @@ namespace Aurora
int futex_wait(uint32_t *addr, uint32_t expected);
int io_submit(aio_context_t ctx, long nr, struct iocb **iocbpp);
int io_cancel(aio_context_t ctx_id, struct iocb *iocb,
@ -52,4 +50,6 @@ namespace Aurora
int io_getevents(aio_context_t ctx, long min_nr, long max_nr,
struct io_event *events,
struct timespec *timeout);
ssize_t sys_getrandom(void *pBuffer, size_t uLength);
}

View File

@ -18,14 +18,14 @@
#define BCRYPT_RNG_ALG_HANDLE ((BCRYPT_ALG_HANDLE) 0x00000081)
#endif
#define USE_OLD_NTCRYPT
#define AURORA_RNG_HAS_OLD_WINCRYPT
#endif
#if defined(AURORA_IS_MODERNNT_DERIVED)
#include <bcrypt.h>
#endif
#if defined(USE_OLD_NTCRYPT)
#if defined(AURORA_RNG_HAS_OLD_WINCRYPT)
#include <wincrypt.h>
#endif
@ -34,6 +34,11 @@
#include <fcntl.h>
#endif
#if defined(AURORA_IS_BSD_DERIVED)
#include <stdlib.h>
#define AURORA_RND_HAS_ARC4
#endif
#include "AuWELL.hpp"
namespace Aurora::RNG
@ -62,18 +67,24 @@ namespace Aurora::RNG
return 0;
}
return ::read(gDevURand, pBuf, uLen);
int iRet = ::read(gDevURand, pBuf, uLen);
if (iRet <= 0)
{
return 0;
}
return AuUInt32(iRet);
}
#elif defined(AURORA_IS_MODERNNT_DERIVED)
#if defined(USE_OLD_NTCRYPT)
#if defined(AURORA_RNG_HAS_OLD_WINCRYPT)
static HCRYPTPROV gCryptoProv;
#endif
void EntropyInit()
{
#if defined(USE_OLD_NTCRYPT)
#if defined(AURORA_RNG_HAS_OLD_WINCRYPT)
if (pBCryptGenRandom)
{
return;
@ -136,7 +147,7 @@ namespace Aurora::RNG
}
}
#if defined(USE_OLD_NTCRYPT)
#if defined(AURORA_RNG_HAS_OLD_WINCRYPT)
if (gCryptoProv)
{
if (pCryptGenRandom(gCryptoProv, uLen, pBuf))
@ -225,6 +236,17 @@ namespace Aurora::RNG
return x;
}
#elif defined(AURORA_IS_POSIX_DERIVED)
#if defined(AURORA_IS_LINUX_DERIVED)
x = sys_getrandom(pBuffer, uBytes);
if (x > 0)
{
return x;
}
#endif
#if defined(AURORA_RND_HAS_ARC4)
arc4random_buf(pBuffer, uBytes);
return uBytes;
#endif
x = RngUnix(pBuffer, uBytes);
if (x != 0)
{
@ -247,7 +269,7 @@ namespace Aurora::RNG
::close(gDevURand);
gDevURand = -1;
}
#elif defined(AURORA_IS_MODERNNT_DERIVED) && defined(USE_OLD_NTCRYPT)
#elif defined(AURORA_IS_MODERNNT_DERIVED) && defined(AURORA_RNG_HAS_OLD_WINCRYPT)
if (pBCryptGenRandom)
{
return;