2018-05-21 13:48:17 +00:00
|
|
|
// Copyright 2018 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include "Common/Random.h"
|
|
|
|
|
|
|
|
#include <mbedtls/entropy.h>
|
|
|
|
#include <mbedtls/hmac_drbg.h>
|
|
|
|
|
|
|
|
#include "Common/Assert.h"
|
|
|
|
|
|
|
|
namespace Common::Random
|
|
|
|
{
|
2021-01-13 13:54:19 +00:00
|
|
|
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
|
2018-05-21 13:48:17 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-01-13 13:54:19 +00:00
|
|
|
EntropySeededPRNG()
|
2018-05-21 13:48:17 +00:00
|
|
|
{
|
|
|
|
mbedtls_entropy_init(&m_entropy);
|
|
|
|
mbedtls_hmac_drbg_init(&m_context);
|
|
|
|
const int ret = mbedtls_hmac_drbg_seed(&m_context, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
|
|
|
|
mbedtls_entropy_func, &m_entropy, nullptr, 0);
|
|
|
|
ASSERT(ret == 0);
|
|
|
|
}
|
|
|
|
|
2021-01-13 13:54:19 +00:00
|
|
|
~EntropySeededPRNG()
|
2018-05-21 13:48:17 +00:00
|
|
|
{
|
|
|
|
mbedtls_hmac_drbg_free(&m_context);
|
|
|
|
mbedtls_entropy_free(&m_entropy);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
mbedtls_entropy_context m_entropy;
|
|
|
|
mbedtls_hmac_drbg_context m_context;
|
|
|
|
};
|
|
|
|
|
2021-01-13 13:54:19 +00:00
|
|
|
static thread_local EntropySeededPRNG s_esprng;
|
2018-05-21 13:48:17 +00:00
|
|
|
|
|
|
|
void Generate(void* buffer, std::size_t size)
|
|
|
|
{
|
2021-01-13 13:54:19 +00:00
|
|
|
s_esprng.Generate(buffer, size);
|
2018-05-21 13:48:17 +00:00
|
|
|
}
|
|
|
|
} // namespace Common::Random
|