diff --git a/src/Main.cpp b/src/Main.cpp index b59ffb47..63886f6c 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -14,11 +14,6 @@ namespace Main { Components::Loader::Uninitialize(); } - - void PreInit() - { - ltc_mp = ltm_desc; - } } BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) @@ -47,7 +42,21 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReser })->Install(); - Main::PreInit(); +// auto key = Utils::Cryptography::RSA::GenerateKey(2048); +// std::string message = "ZOB1234543253253452345"; +// std::string signature = Utils::Cryptography::RSA::SignMessage(key, message); +// +// // Invalidate the signature +// //signature[0] ^= 0xFF; +// +// if (Utils::Cryptography::RSA::VerifyMessage(key, message, signature)) +// { +// MessageBoxA(0, "Valid", 0, 0); +// } +// else +// { +// MessageBoxA(0, "Invalid!", 0, 0); +// } } else if (ul_reason_for_call == DLL_PROCESS_DETACH) { diff --git a/src/STDInclude.hpp b/src/STDInclude.hpp index 0f548037..e7be1ea1 100644 --- a/src/STDInclude.hpp +++ b/src/STDInclude.hpp @@ -70,6 +70,7 @@ #include "Utils\Memory.hpp" #include "Utils\Hooking.hpp" #include "Utils\Compression.hpp" +#include "Utils\Cryptography.hpp" #include "Steam\Steam.hpp" diff --git a/src/Utils/Cryptography.cpp b/src/Utils/Cryptography.cpp new file mode 100644 index 00000000..09188000 --- /dev/null +++ b/src/Utils/Cryptography.cpp @@ -0,0 +1,48 @@ +#include "STDInclude.hpp" + +/// http://www.opensource.apple.com/source/CommonCrypto/CommonCrypto-55010/Source/libtomcrypt/doc/libTomCryptDoc.pdf + +namespace Utils +{ + namespace Cryptography + { + RSA::Key RSA::GenerateKey(int bits) + { + RSA::Key key; + + register_prng(&sprng_desc); + register_hash(&sha1_desc); + + ltc_mp = ltm_desc; + + rsa_make_key(NULL, find_prng("sprng"), bits / 8, 65537, key.GetKeyPtr()); + + return key; + } + + std::string RSA::SignMessage(RSA::Key key, std::string message) + { + uint8_t buffer[0x200]; // Default size is 512 + DWORD length = sizeof(buffer); + + register_prng(&sprng_desc); + register_hash(&sha1_desc); + + ltc_mp = ltm_desc; + + rsa_sign_hash(reinterpret_cast(message.data()), message.size(), buffer, &length, NULL, find_prng("sprng"), find_hash("sha1"), 0, key.GetKeyPtr()); + + return std::string(reinterpret_cast(buffer), length); + } + + bool RSA::VerifyMessage(Key key, std::string message, std::string signature) + { + register_hash(&sha1_desc); + + ltc_mp = ltm_desc; + + int result = 0; + return (rsa_verify_hash(reinterpret_cast(signature.data()), signature.size(), reinterpret_cast(message.data()), message.size(), find_hash("sha1"), 0, &result, key.GetKeyPtr()) == CRYPT_OK && result != 0); + } + } +} diff --git a/src/Utils/Cryptography.hpp b/src/Utils/Cryptography.hpp new file mode 100644 index 00000000..2cb620d3 --- /dev/null +++ b/src/Utils/Cryptography.hpp @@ -0,0 +1,32 @@ +namespace Utils +{ + namespace Cryptography + { + class RSA + { + public: + class Key + { + public: + Key() { ZeroMemory(&this->KeyStorage, sizeof(this->KeyStorage)); }; + Key(rsa_key* key) : Key(*key) {}; + Key(rsa_key key) : KeyStorage(key) {}; + Key(const Key& obj) : KeyStorage(obj.KeyStorage) {}; + + ~Key() {} + + rsa_key* GetKeyPtr() + { + return &this->KeyStorage; + } + + private: + rsa_key KeyStorage; + }; + + static RSA::Key GenerateKey(int bits); + static std::string SignMessage(Key key, std::string message); + static bool VerifyMessage(Key key, std::string message, std::string signature); + }; + } +}