Debian Codegraph @

Source/Core/Common/Random.cpp

raw symbols SCIP
1 // Copyright 2018 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "Common/Random.h"
5
6 #include <mbedtls/entropy.h>
7 #include <mbedtls/hmac_drbg.h>
8
9 #include "Common/Assert.h"
10 #include "Common/CommonTypes.h"
11
12 namespace Common::Random
13 {
14 class EntropySeededPRNG final
15 {
16 public:
17 EntropySeededPRNG()
18 {
19 mbedtls_entropy_init(&m_entropy);
20 mbedtls_hmac_drbg_init(&m_context);
21 const int ret = mbedtls_hmac_drbg_seed(&m_context, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
22 mbedtls_entropy_func, &m_entropy, nullptr, 0);
23 ASSERT(ret == 0);
24 }
25
26 ~EntropySeededPRNG()
27 {
28 mbedtls_hmac_drbg_free(&m_context);
29 mbedtls_entropy_free(&m_entropy);
30 }
31
32 void Generate(void* buffer, std::size_t size)
33 {
34 const int ret = mbedtls_hmac_drbg_random(&m_context, static_cast<u8*>(buffer), size);
35 ASSERT(ret == 0);
36 }
37
38 private:
39 mbedtls_entropy_context m_entropy;
40 mbedtls_hmac_drbg_context m_context;
41 };
42
43 static thread_local EntropySeededPRNG s_esprng;
44
45 void Generate(void* buffer, std::size_t size)
46 {
47 s_esprng.Generate(buffer, size);
48 }
49 } // namespace Common::Random