mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2026-01-30 11:03:31 +00:00
Yellow squiggly lines begone! Done automatically on .cpp files through `run-clang-tidy`, with manual corrections to the mistakes. If an import is directly used, but is technically unnecessary since it's recursively imported by something else, it is *not* removed. The tool doesn't touch .h files, so I did some of them by hand while fixing errors due to old recursive imports. Not everything is removed, but the cleanup should be substantial enough. Because this done on Linux, code that isn't used on it is mostly untouched. (Hopefully no open PR is depending on these imports...)
50 lines
1.1 KiB
C++
50 lines
1.1 KiB
C++
// Copyright 2018 Dolphin Emulator Project
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
#include "Common/Random.h"
|
|
|
|
#include <mbedtls/entropy.h>
|
|
#include <mbedtls/hmac_drbg.h>
|
|
|
|
#include "Common/Assert.h"
|
|
#include "Common/CommonTypes.h"
|
|
|
|
namespace Common::Random
|
|
{
|
|
class EntropySeededPRNG final
|
|
{
|
|
public:
|
|
EntropySeededPRNG()
|
|
{
|
|
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);
|
|
}
|
|
|
|
~EntropySeededPRNG()
|
|
{
|
|
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;
|
|
};
|
|
|
|
static thread_local EntropySeededPRNG s_esprng;
|
|
|
|
void Generate(void* buffer, std::size_t size)
|
|
{
|
|
s_esprng.Generate(buffer, size);
|
|
}
|
|
} // namespace Common::Random
|