Merge pull request #21333 from Chocobo1/random

Improve PRNG functions
This commit is contained in:
Chocobo1 2024-09-16 17:56:19 +08:00 committed by GitHub
commit 23f7275bd5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -30,28 +30,26 @@
#include <random> #include <random>
#include <QtLogging>
#include <QtSystemDetection> #include <QtSystemDetection>
#ifdef Q_OS_WIN #if defined(Q_OS_WIN)
#include <windows.h> #include <windows.h>
#include <ntsecapi.h> #include "base/global.h"
#else // Q_OS_WIN #include "base/utils/os.h"
#elif defined(Q_OS_LINUX)
#include <cerrno>
#include <cstring>
#include <sys/random.h>
#else
#include <cerrno> #include <cerrno>
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
#endif #endif
#include <QString>
#include "base/global.h"
#ifdef Q_OS_WIN
#include "base/utils/os.h"
#endif
namespace namespace
{ {
#ifdef Q_OS_WIN #if defined(Q_OS_WIN)
class RandomLayer class RandomLayer
{ {
// need to satisfy UniformRandomBitGenerator requirements // need to satisfy UniformRandomBitGenerator requirements
@ -59,10 +57,10 @@ namespace
using result_type = uint32_t; using result_type = uint32_t;
RandomLayer() RandomLayer()
: m_rtlGenRandom {Utils::OS::loadWinAPI<PRTLGENRANDOM>(u"Advapi32.dll"_s, "SystemFunction036")} : m_processPrng {Utils::OS::loadWinAPI<PPROCESSPRNG>(u"BCryptPrimitives.dll"_s, "ProcessPrng")}
{ {
if (!m_rtlGenRandom) if (!m_processPrng)
qFatal("Failed to load RtlGenRandom()"); qFatal("Failed to load ProcessPrng().");
} }
static constexpr result_type min() static constexpr result_type min()
@ -78,18 +76,57 @@ namespace
result_type operator()() result_type operator()()
{ {
result_type buf = 0; result_type buf = 0;
const bool result = m_rtlGenRandom(&buf, sizeof(buf)); const bool result = m_processPrng(reinterpret_cast<PBYTE>(&buf), sizeof(buf));
if (!result) if (!result)
qFatal("RtlGenRandom() failed"); qFatal("ProcessPrng() failed.");
return buf; return buf;
} }
private: private:
using PRTLGENRANDOM = BOOLEAN (WINAPI *)(PVOID, ULONG); using PPROCESSPRNG = BOOL (WINAPI *)(PBYTE, SIZE_T);
const PRTLGENRANDOM m_rtlGenRandom; const PPROCESSPRNG m_processPrng;
}; };
#else // Q_OS_WIN #elif defined(Q_OS_LINUX)
class RandomLayer
{
// need to satisfy UniformRandomBitGenerator requirements
public:
using result_type = uint32_t;
RandomLayer()
{
}
static constexpr result_type min()
{
return std::numeric_limits<result_type>::min();
}
static constexpr result_type max()
{
return std::numeric_limits<result_type>::max();
}
result_type operator()()
{
const int RETRY_MAX = 3;
for (int i = 0; i < RETRY_MAX; ++i)
{
result_type buf = 0;
const ssize_t result = ::getrandom(&buf, sizeof(buf), 0);
if (result == sizeof(buf)) // success
return buf;
if (result < 0)
qFatal("getrandom() error. Reason: %s. Error code: %d.", std::strerror(errno), errno);
}
qFatal("getrandom() failed. Reason: too many retries.");
}
};
#else
class RandomLayer class RandomLayer
{ {
// need to satisfy UniformRandomBitGenerator requirements // need to satisfy UniformRandomBitGenerator requirements
@ -100,7 +137,7 @@ namespace
: m_randDev {fopen("/dev/urandom", "rb")} : m_randDev {fopen("/dev/urandom", "rb")}
{ {
if (!m_randDev) if (!m_randDev)
qFatal("Failed to open /dev/urandom. Reason: %s. Error code: %d.\n", std::strerror(errno), errno); qFatal("Failed to open /dev/urandom. Reason: %s. Error code: %d.", std::strerror(errno), errno);
} }
~RandomLayer() ~RandomLayer()
@ -122,7 +159,7 @@ namespace
{ {
result_type buf = 0; result_type buf = 0;
if (fread(&buf, sizeof(buf), 1, m_randDev) != 1) if (fread(&buf, sizeof(buf), 1, m_randDev) != 1)
qFatal("Read /dev/urandom error. Reason: %s. Error code: %d.\n", std::strerror(errno), errno); qFatal("Read /dev/urandom error. Reason: %s. Error code: %d.", std::strerror(errno), errno);
return buf; return buf;
} }