Random: Add seeded PRNG

This commit is contained in:
MerryMage 2021-01-13 13:54:19 +00:00
parent b22073ef59
commit f65c1df094
2 changed files with 62 additions and 5 deletions

View File

@ -11,10 +11,42 @@
namespace Common::Random namespace Common::Random
{ {
class CSPRNG final struct PRNG::Impl
{
Impl(void* seed, std::size_t size)
{
mbedtls_hmac_drbg_init(&m_context);
const int ret = mbedtls_hmac_drbg_seed_buf(
&m_context, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), static_cast<u8*>(seed), size);
ASSERT(ret == 0);
}
~Impl() { mbedtls_hmac_drbg_free(&m_context); }
void Generate(void* buffer, std::size_t size)
{
const int ret = mbedtls_hmac_drbg_random(&m_context, static_cast<u8*>(buffer), size);
ASSERT(ret == 0);
}
mbedtls_hmac_drbg_context m_context;
};
PRNG::PRNG(void* seed, std::size_t size) : m_impl(std::make_unique<Impl>(seed, size))
{
}
PRNG::~PRNG() = default;
void PRNG::Generate(void* buffer, std::size_t size)
{
m_impl->Generate(buffer, size);
}
class EntropySeededPRNG final
{ {
public: public:
CSPRNG() EntropySeededPRNG()
{ {
mbedtls_entropy_init(&m_entropy); mbedtls_entropy_init(&m_entropy);
mbedtls_hmac_drbg_init(&m_context); mbedtls_hmac_drbg_init(&m_context);
@ -23,7 +55,7 @@ public:
ASSERT(ret == 0); ASSERT(ret == 0);
} }
~CSPRNG() ~EntropySeededPRNG()
{ {
mbedtls_hmac_drbg_free(&m_context); mbedtls_hmac_drbg_free(&m_context);
mbedtls_entropy_free(&m_entropy); mbedtls_entropy_free(&m_entropy);
@ -40,10 +72,10 @@ private:
mbedtls_hmac_drbg_context m_context; mbedtls_hmac_drbg_context m_context;
}; };
static thread_local CSPRNG s_csprng; static thread_local EntropySeededPRNG s_esprng;
void Generate(void* buffer, std::size_t size) void Generate(void* buffer, std::size_t size)
{ {
s_csprng.Generate(buffer, size); s_esprng.Generate(buffer, size);
} }
} // namespace Common::Random } // namespace Common::Random

View File

@ -5,12 +5,37 @@
#pragma once #pragma once
#include <cstddef> #include <cstddef>
#include <memory>
#include <type_traits> #include <type_traits>
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
namespace Common::Random namespace Common::Random
{ {
/// Cryptographically secure pseudo-random number generator, with explicit seed.
class PRNG final
{
public:
explicit PRNG(u64 seed) : PRNG(&seed, sizeof(u64)) {}
PRNG(void* seed, std::size_t size);
~PRNG();
void Generate(void* buffer, std::size_t size);
template <typename T>
T GenerateValue()
{
static_assert(std::is_arithmetic<T>(), "T must be an arithmetic type in GenerateValue.");
T value;
Generate(&value, sizeof(value));
return value;
}
private:
struct Impl;
std::unique_ptr<Impl> m_impl;
};
/// Fill `buffer` with random bytes using a cryptographically secure pseudo-random number generator. /// Fill `buffer` with random bytes using a cryptographically secure pseudo-random number generator.
void Generate(void* buffer, std::size_t size); void Generate(void* buffer, std::size_t size);