mirror of
https://github.com/RPCS3/rpcs3.git
synced 2025-03-16 07:20:59 +00:00
sys_ss: use BCryptGenRandom on Win32
This commit is contained in:
parent
08f3460365
commit
a84077f174
@ -5,33 +5,11 @@
|
|||||||
#include "Emu/IdManager.h"
|
#include "Emu/IdManager.h"
|
||||||
#include "Emu/Cell/PPUThread.h"
|
#include "Emu/Cell/PPUThread.h"
|
||||||
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <Windows.h>
|
#include <Windows.h>
|
||||||
#include <wincrypt.h>
|
#include <bcrypt.h>
|
||||||
|
|
||||||
const HCRYPTPROV s_crypto_provider = []() -> HCRYPTPROV
|
|
||||||
{
|
|
||||||
HCRYPTPROV result;
|
|
||||||
|
|
||||||
if (!CryptAcquireContextW(&result, nullptr, nullptr, PROV_RSA_FULL, 0) && !CryptAcquireContextW(&result, nullptr, nullptr, PROV_RSA_FULL, CRYPT_NEWKEYSET))
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
::atexit([]()
|
|
||||||
{
|
|
||||||
if (s_crypto_provider)
|
|
||||||
{
|
|
||||||
CryptReleaseContext(s_crypto_provider, 0);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}();
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
void fmt_class_string<sys_ss_rng_error>::format(std::string& out, u64 arg)
|
void fmt_class_string<sys_ss_rng_error>::format(std::string& out, u64 arg)
|
||||||
{
|
{
|
||||||
@ -81,15 +59,18 @@ error_code sys_ss_random_number_generator(u64 pkg_id, vm::ptr<void> buf, u64 siz
|
|||||||
std::unique_ptr<u8[]> temp(new u8[size]);
|
std::unique_ptr<u8[]> temp(new u8[size]);
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
if (!s_crypto_provider || !CryptGenRandom(s_crypto_provider, size, temp.get()))
|
if (auto ret = BCryptGenRandom(nullptr, temp.get(), size, BCRYPT_USE_SYSTEM_PREFERRED_RNG))
|
||||||
|
{
|
||||||
|
fmt::throw_exception("sys_ss_random_number_generator(): BCryptGenRandom failed (0x%08x)" HERE, ret);
|
||||||
|
}
|
||||||
#else
|
#else
|
||||||
fs::file rnd{"/dev/urandom"};
|
fs::file rnd{"/dev/urandom"};
|
||||||
|
|
||||||
if (!rnd || rnd.read(temp.get(), size) != size)
|
if (!rnd || rnd.read(temp.get(), size) != size)
|
||||||
#endif
|
|
||||||
{
|
{
|
||||||
fmt::throw_exception("sys_ss_random_number_generator(): Failed to generate pseudo-random numbers" HERE);
|
fmt::throw_exception("sys_ss_random_number_generator(): Failed to generate pseudo-random numbers" HERE);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
std::memcpy(buf.get_ptr(), temp.get(), size);
|
std::memcpy(buf.get_ptr(), temp.get(), size);
|
||||||
return CELL_OK;
|
return CELL_OK;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user