Skip to content

Commit a60f7a7

Browse files
onikyannnPirulax
andauthored
Add support aes128 encryption to encodeString/decodeString (#2235)
* add support aes128 encryption to encodeString/decodeString * change encryption alhorytm to CTR and make some small fixes * add iv generator to Aes128encode * fix whitespaces * add test * add cryptopp to loader project premake5 * Update Shared/sdk/SharedUtil.Crypto.h Co-authored-by: Pirulax <patrikjankovics7@gmail.com> * use key and iv as string directly * Update Shared/sdk/SharedUtil.Crypto.h Add missing sIv.resize(AES::BLOCKSIZE); to aes128decode * Update Shared/sdk/SharedUtil.Crypto.h - Addendum to previous Co-authored-by: Pirulax <patrikjankovics7@gmail.com>
1 parent 13226ed commit a60f7a7

File tree

7 files changed

+207
-1
lines changed

7 files changed

+207
-1
lines changed

Client/loader/premake5.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ project "Loader"
1818
"unrar", "d3d9",
1919
"detours", "Imagehlp",
2020
"../../vendor/nvapi/x86/nvapi.lib",
21+
"cryptopp",
2122
}
2223

2324
pchheader "StdInc.h"

Shared/mods/deathmatch/logic/Enums.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ IMPLEMENT_ENUM_CLASS_END("password-hash-function")
5454

5555
IMPLEMENT_ENUM_CLASS_BEGIN(StringEncryptFunction)
5656
ADD_ENUM(StringEncryptFunction::TEA, "tea")
57+
ADD_ENUM(StringEncryptFunction::AES128, "aes128")
5758
IMPLEMENT_ENUM_CLASS_END("string-encrypt-function")
5859

5960
IMPLEMENT_ENUM_BEGIN(ePacketID)

Shared/mods/deathmatch/logic/luadefs/CLuaCryptDefs.cpp

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,75 @@ int CLuaCryptDefs::EncodeString(lua_State* luaVM)
294294
}
295295
return 1;
296296
}
297+
case StringEncryptFunction::AES128:
298+
{
299+
SString& key = options["key"];
300+
301+
if (key.size() != CryptoPP::AES::DEFAULT_KEYLENGTH)
302+
{
303+
m_pScriptDebugging->LogCustom(luaVM, "Invalid key length (must be 16 characters long)");
304+
lua_pushboolean(luaVM, false);
305+
return 1;
306+
}
307+
308+
// Async
309+
if (VERIFY_FUNCTION(luaFunctionRef))
310+
{
311+
CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM);
312+
if (pLuaMain)
313+
{
314+
CLuaShared::GetAsyncTaskScheduler()->PushTask<std::pair<SString, SString>>(
315+
[data, key] {
316+
std::pair<SString, SString> result;
317+
try
318+
{
319+
result = SharedUtil::Aes128encode(data, key);
320+
}
321+
catch (const CryptoPP::Exception&)
322+
{
323+
324+
}
325+
return result;
326+
},
327+
[luaFunctionRef](const std::pair<SString, SString> result) {
328+
CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaFunctionRef.GetLuaVM());
329+
if (pLuaMain)
330+
{
331+
CLuaArguments arguments;
332+
if (result.first.empty())
333+
{
334+
arguments.PushBoolean(false);
335+
arguments.Call(pLuaMain, luaFunctionRef);
336+
}
337+
else
338+
{
339+
arguments.PushString(result.first);
340+
arguments.PushString(result.second);
341+
arguments.Call(pLuaMain, luaFunctionRef);
342+
}
343+
}
344+
});
345+
346+
lua_pushboolean(luaVM, true);
347+
}
348+
}
349+
else // Sync
350+
{
351+
std::pair<SString, SString> result;
352+
try
353+
{
354+
result = SharedUtil::Aes128encode(data, key);
355+
}
356+
catch (const CryptoPP::Exception&)
357+
{
358+
lua_pushboolean(luaVM, false);
359+
return 1;
360+
}
361+
lua_pushlstring(luaVM, result.first, result.first.length());
362+
lua_pushlstring(luaVM, result.second, result.second.length());
363+
}
364+
return 2;
365+
}
297366
default:
298367
{
299368
m_pScriptDebugging->LogCustom(luaVM, "Unknown encryption algorithm");
@@ -373,6 +442,82 @@ int CLuaCryptDefs::DecodeString(lua_State* luaVM)
373442
}
374443
return 1;
375444
}
445+
case StringEncryptFunction::AES128:
446+
{
447+
SString& key = options["key"];
448+
SString& iv = options["iv"];
449+
450+
if (key.size() != CryptoPP::AES::DEFAULT_KEYLENGTH)
451+
{
452+
m_pScriptDebugging->LogCustom(luaVM, "Invalid key length (must be 16 characters long)");
453+
lua_pushboolean(luaVM, false);
454+
return 1;
455+
}
456+
457+
if (iv.size() != CryptoPP::AES::BLOCKSIZE)
458+
{
459+
m_pScriptDebugging->LogCustom(luaVM, "Invalid iv length (must be 16 characters long)");
460+
lua_pushboolean(luaVM, false);
461+
return 1;
462+
}
463+
464+
// Async
465+
if (VERIFY_FUNCTION(luaFunctionRef))
466+
{
467+
CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM);
468+
if (pLuaMain)
469+
{
470+
CLuaShared::GetAsyncTaskScheduler()->PushTask<SString>(
471+
[data, key, iv] {
472+
// Execute time-consuming task
473+
SString result;
474+
try
475+
{
476+
result = SharedUtil::Aes128decode(data, key, iv);
477+
}
478+
catch (const CryptoPP::Exception&)
479+
{
480+
481+
}
482+
return result;
483+
},
484+
[luaFunctionRef](const SString& result) {
485+
CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaFunctionRef.GetLuaVM());
486+
if (pLuaMain)
487+
{
488+
CLuaArguments arguments;
489+
if (result.empty())
490+
{
491+
arguments.PushBoolean(false);
492+
arguments.Call(pLuaMain, luaFunctionRef);
493+
}
494+
else
495+
{
496+
arguments.PushString(result);
497+
arguments.Call(pLuaMain, luaFunctionRef);
498+
}
499+
}
500+
});
501+
502+
lua_pushboolean(luaVM, true);
503+
}
504+
}
505+
else // Sync
506+
{
507+
SString result;
508+
try
509+
{
510+
result = SharedUtil::Aes128decode(data, key, iv);
511+
}
512+
catch (const CryptoPP::Exception&)
513+
{
514+
lua_pushboolean(luaVM, false);
515+
return 1;
516+
}
517+
lua_pushlstring(luaVM, result, result.length());
518+
}
519+
return 1;
520+
}
376521
default:
377522
{
378523
m_pScriptDebugging->LogCustom(luaVM, "Unknown encryption algorithm");

Shared/sdk/SharedUtil.Crypto.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
*****************************************************************************/
1010
#pragma once
1111
#include <cryptopp/base64.h>
12+
#include <cryptopp/aes.h>
13+
#include <cryptopp/modes.h>
14+
#include <cryptopp/osrng.h>
1215
#include "SString.h"
1316

1417
namespace SharedUtil
@@ -28,4 +31,39 @@ namespace SharedUtil
2831

2932
return result;
3033
}
34+
35+
inline std::pair<SString, SString> Aes128encode(const SString& sData, const SString& sKey)
36+
{
37+
using namespace CryptoPP;
38+
using CryptoPP::byte;
39+
40+
AutoSeededRandomPool rnd;
41+
42+
SString result;
43+
SString sIv;
44+
45+
sIv.resize(AES::BLOCKSIZE);
46+
rnd.GenerateBlock((byte*)sIv.data(), sIv.size());
47+
48+
CTR_Mode<AES>::Encryption aesEncryption;
49+
aesEncryption.SetKeyWithIV((byte*)sKey.data(), sKey.size(), (byte*)sIv.data());
50+
StringSource ss(sData, true, new StreamTransformationFilter(aesEncryption, new StringSink(result)));
51+
52+
return {result, sIv};
53+
}
54+
55+
inline SString Aes128decode(const SString& sData, const SString& sKey, SString sIv)
56+
{
57+
using namespace CryptoPP;
58+
using CryptoPP::byte;
59+
60+
sIv.resize(AES::BLOCKSIZE);
61+
SString result;
62+
63+
CTR_Mode<AES>::Decryption aesDecryption;
64+
aesDecryption.SetKeyWithIV((byte*)sKey.data(), sKey.size(), (byte*)sIv.data());
65+
StringSource ss(sData, true, new StreamTransformationFilter(aesDecryption, new StringSink(result)));
66+
67+
return result;
68+
}
3169
} // namespace SharedUtil

Shared/sdk/SharedUtil.Hash.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ enum class PasswordHashFunction
3434

3535
enum class StringEncryptFunction
3636
{
37-
TEA
37+
TEA,
38+
AES128,
3839
};
3940

4041
namespace SharedUtil

Shared/sdk/SharedUtil.Tests.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,25 @@ void SharedUtil_Hash_Tests()
782782
TEST_END
783783
}
784784

785+
// Aes128encode/Aes128decode
786+
{
787+
TEST_FUNCTION
788+
std::pair<std::string, std::string> res = Aes128encode(data, key);
789+
assert(!res.first.empty());
790+
assert(!res.second.empty());
791+
SString strDecoded = Aes128decode(res.first, key, res.second);
792+
assert(data == strDecoded);
793+
TEST_VARS
794+
const SString key;
795+
const SString data;
796+
TEST_DATA = {
797+
{"5347123412340000", "AB12£$_ "},
798+
{"gHtySkGerYnhDxAs", "78111E998C42243285635E39AFDD614B\0 AB12£$_ "},
799+
{"!@#$%^&*()_+|:<>", "Hello thereHello there"},
800+
};
801+
TEST_END
802+
}
803+
785804
#define szTempFilename "hash_""\xD0""\x98""_test"
786805

787806
// MD5

Shared/sdk/SharedUtil.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#include "SharedUtil.Math.h"
5555
#include "SharedUtil.ClassIdent.h"
5656
#include "SharedUtil.Hash.h"
57+
#include "SharedUtil.Crypto.h"
5758
#if defined(SHARED_UTIL_WITH_SYS_INFO)
5859
#include "SharedUtil.SysInfo.h"
5960
#endif

0 commit comments

Comments
 (0)