From 90e4711425e673c10175e603d1dd42ba04ad91ef Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Wed, 10 Apr 2024 15:28:47 +0200 Subject: [PATCH] Tier2: move random generator from tier0 to tier2 Moved into cryptutils.cpp. --- r5dev/engine/shared/base_rcon.cpp | 2 +- r5dev/public/tier0/utility.h | 1 - r5dev/public/tier2/cryptutils.h | 2 ++ r5dev/tier0/utility.cpp | 20 -------------------- r5dev/tier2/cryptutils.cpp | 18 ++++++++++++++++++ 5 files changed, 21 insertions(+), 22 deletions(-) diff --git a/r5dev/engine/shared/base_rcon.cpp b/r5dev/engine/shared/base_rcon.cpp index 2b677e79..5008737e 100644 --- a/r5dev/engine/shared/base_rcon.cpp +++ b/r5dev/engine/shared/base_rcon.cpp @@ -74,7 +74,7 @@ void CNetConBase::SetKey(const char* pBase64NetKey, const bool bUseDefaultOnFail { const char* errorMsg = nullptr; - if (!CryptoGenRandom(m_NetKey, sizeof(m_NetKey), errorMsg)) + if (!Plat_GenerateRandom(m_NetKey, sizeof(m_NetKey), errorMsg)) { Error(eDLL_T::ENGINE, NO_ERROR, "RCON Key: generate error (%s)\n", errorMsg); useDefaultKey = true; diff --git a/r5dev/public/tier0/utility.h b/r5dev/public/tier0/utility.h index ec7f321d..f245e34e 100644 --- a/r5dev/public/tier0/utility.h +++ b/r5dev/public/tier0/utility.h @@ -8,7 +8,6 @@ int CreateDirHierarchy(const char* filePath); bool IsDirectory(const char* path); bool FileEmpty(ifstream& pFile); MODULEINFO GetModuleInfo(const char* szModule); -bool CryptoGenRandom(unsigned char* pData, const uint32_t nDataLen, const char*& outMsg); ///////////////////////////////////////////////////////////////////////////// // Debug diff --git a/r5dev/public/tier2/cryptutils.h b/r5dev/public/tier2/cryptutils.h index 7bf7e822..048ac786 100644 --- a/r5dev/public/tier2/cryptutils.h +++ b/r5dev/public/tier2/cryptutils.h @@ -1,6 +1,8 @@ #ifndef TIER2_CRYPTUTILS_H #define TIER2_CRYPTUTILS_H +bool Plat_GenerateRandom(unsigned char* pBuffer, const uint32_t nBufLen, const char*& errorMsg); + typedef unsigned char CryptoIV_t[16]; typedef unsigned char CryptoKey_t[16]; diff --git a/r5dev/tier0/utility.cpp b/r5dev/tier0/utility.cpp index 42503c1e..0c55dfd1 100644 --- a/r5dev/tier0/utility.cpp +++ b/r5dev/tier0/utility.cpp @@ -121,26 +121,6 @@ MODULEINFO GetModuleInfo(const char* szModule) return modinfo; } -/////////////////////////////////////////////////////////////////////////////// -// For generating random data. -static BCRYPT_ALG_HANDLE s_bcryptAlgorithmProvider; -bool CryptoGenRandom(unsigned char* pData, const uint32_t nDataLen, const char*& outMsg) -{ - if (!s_bcryptAlgorithmProvider && (BCryptOpenAlgorithmProvider(&s_bcryptAlgorithmProvider, L"RNG", 0, 0) < 0)) - { - outMsg = "Failed to open rng algorithm"; - return false; - } - - if (BCryptGenRandom(s_bcryptAlgorithmProvider, pData, nDataLen, 0) < 0) - { - outMsg = "Failed to generate random data"; - return false; - } - - return true; -} - /////////////////////////////////////////////////////////////////////////////// // For printing output to the debugger. void DbgPrint(LPCSTR sFormat, ...) diff --git a/r5dev/tier2/cryptutils.cpp b/r5dev/tier2/cryptutils.cpp index bc4b1c90..18b28ed4 100644 --- a/r5dev/tier2/cryptutils.cpp +++ b/r5dev/tier2/cryptutils.cpp @@ -11,6 +11,24 @@ #include "tier2/cryptutils.h" +static BCRYPT_ALG_HANDLE s_algorithmProvider; +bool Plat_GenerateRandom(unsigned char* buffer, const uint32_t bufferSize, const char*& errorMsg) +{ + if (!s_algorithmProvider && (BCryptOpenAlgorithmProvider(&s_algorithmProvider, L"RNG", 0, 0) < 0)) + { + errorMsg = "Failed to open rng algorithm"; + return false; + } + + if (BCryptGenRandom(s_algorithmProvider, buffer, bufferSize, 0) < 0) + { + errorMsg = "Failed to generate random data"; + return false; + } + + return true; +} + bool Crypto_GenerateIV(CryptoContext_s& ctx, const unsigned char* const data, const size_t size) { mbedtls_entropy_context entropy;