From a84077f1743693acb56a0dc393d8f6c77fe88f30 Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Sat, 29 Feb 2020 12:09:39 +0300 Subject: [PATCH] sys_ss: use BCryptGenRandom on Win32 --- rpcs3/Emu/Cell/lv2/sys_ss.cpp | 31 ++++++------------------------- 1 file changed, 6 insertions(+), 25 deletions(-) diff --git a/rpcs3/Emu/Cell/lv2/sys_ss.cpp b/rpcs3/Emu/Cell/lv2/sys_ss.cpp index 7543141fdd..e5407d67ad 100644 --- a/rpcs3/Emu/Cell/lv2/sys_ss.cpp +++ b/rpcs3/Emu/Cell/lv2/sys_ss.cpp @@ -5,33 +5,11 @@ #include "Emu/IdManager.h" #include "Emu/Cell/PPUThread.h" - #ifdef _WIN32 #include -#include - -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; -}(); +#include #endif - template<> void fmt_class_string::format(std::string& out, u64 arg) { @@ -81,15 +59,18 @@ error_code sys_ss_random_number_generator(u64 pkg_id, vm::ptr buf, u64 siz std::unique_ptr temp(new u8[size]); #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 fs::file rnd{"/dev/urandom"}; if (!rnd || rnd.read(temp.get(), size) != size) -#endif { fmt::throw_exception("sys_ss_random_number_generator(): Failed to generate pseudo-random numbers" HERE); } +#endif std::memcpy(buf.get_ptr(), temp.get(), size); return CELL_OK;