Tier2: move random generator from tier0 to tier2

Moved into cryptutils.cpp.
This commit is contained in:
Kawe Mazidjatari 2024-04-10 15:28:47 +02:00
parent 73aff08819
commit 90e4711425
5 changed files with 21 additions and 22 deletions

View File

@ -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;

View File

@ -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

View File

@ -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];

View File

@ -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, ...)

View File

@ -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;