Small optimization

This commit is contained in:
momo5502 2016-06-14 19:49:27 +02:00
parent ef58a174e1
commit 4428c063d7
6 changed files with 149 additions and 2 deletions

View File

@ -8,7 +8,7 @@ namespace Main
{
Main::EntryPointHook.Uninstall();
Utils::Cryptography::Rand::Initialize();
Utils::Cryptography::Initialize();
Components::Loader::Initialize();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)

View File

@ -6,6 +6,11 @@ namespace Utils
{
namespace Cryptography
{
void Initialize()
{
TDES::Initialize();
Rand::Initialize();
}
#pragma region Rand
@ -117,6 +122,69 @@ namespace Utils
#pragma endregion
#pragma region TDES
void TDES::Initialize()
{
register_cipher(&des3_desc);
}
std::string TDES::Encrypt(std::string text, std::string iv, std::string key)
{
std::string encData;
encData.resize(text.size());
symmetric_CBC cbc;
int des3 = find_cipher("3des");
cbc_start(des3, reinterpret_cast<const uint8_t*>(iv.data()), reinterpret_cast<const uint8_t*>(key.data()), key.size(), 0, &cbc);
cbc_encrypt(reinterpret_cast<const uint8_t*>(text.data()), reinterpret_cast<uint8_t*>(const_cast<char*>(encData.data())), text.size(), &cbc);
cbc_done(&cbc);
return encData;
}
std::string TDES::Decrpyt(std::string data, std::string iv, std::string key)
{
std::string decData;
decData.resize(data.size());
symmetric_CBC cbc;
int des3 = find_cipher("3des");
cbc_start(des3, reinterpret_cast<const uint8_t*>(iv.data()), reinterpret_cast<const uint8_t*>(key.data()), key.size(), 0, &cbc);
cbc_decrypt(reinterpret_cast<const uint8_t*>(data.data()), reinterpret_cast<uint8_t*>(const_cast<char*>(decData.data())), data.size(), &cbc);
cbc_done(&cbc);
return decData;
}
#pragma endregion
#pragma region Tiger
std::string Tiger::Compute(std::string data, bool hex)
{
return Tiger::Compute(reinterpret_cast<const uint8_t*>(data.data()), data.size(), hex);
}
std::string Tiger::Compute(const uint8_t* data, size_t length, bool hex)
{
uint8_t buffer[24] = { 0 };
hash_state state;
tiger_init(&state);
tiger_process(&state, data, length);
tiger_done(&state, buffer);
std::string hash(reinterpret_cast<char*>(buffer), sizeof(buffer));
if (!hex) return hash;
return Utils::DumpHex(hash, "");
}
#pragma endregion
#pragma region SHA256
std::string SHA256::Compute(std::string data, bool hex)

View File

@ -2,6 +2,8 @@ namespace Utils
{
namespace Cryptography
{
void Initialize();
class Token
{
public:
@ -283,6 +285,21 @@ namespace Utils
static bool VerifyMessage(Key key, std::string message, std::string signature);
};
class TDES
{
public:
static void Initialize();
static std::string Encrypt(std::string text, std::string iv, std::string key);
static std::string Decrpyt(std::string text, std::string iv, std::string key);
};
class Tiger
{
public:
static std::string Compute(std::string data, bool hex = false);
static std::string Compute(const uint8_t* data, size_t length, bool hex = false);
};
class SHA256
{
public:

View File

@ -5,6 +5,45 @@ namespace Utils
std::map<void*, void*> Hook::Interceptor::IReturn;
std::map<void*, void(*)()> Hook::Interceptor::ICallbacks;
void Hook::Signature::Process()
{
if (Hook::Signature::Signatures.empty()) return;
char* start = reinterpret_cast<char*>(Hook::Signature::Start);
unsigned int sigCount = Hook::Signature::Signatures.size();
Hook::Signature::Container* containers = Hook::Signature::Signatures.data();
for (size_t i = 0; i < Hook::Signature::Length; ++i)
{
char* address = start + i;
for (unsigned int k = 0; k < sigCount; ++k)
{
Hook::Signature::Container* container = &containers[k];
unsigned int j = 0;
for (j = 0; j < strlen(container->Mask); ++j)
{
if (container->Mask[j] != '?' &&container->Signature[j] != address[j])
{
break;
}
}
if (j == strlen(container->Mask))
{
container->Callback(address);
}
}
}
}
void Hook::Signature::Add(Hook::Signature::Container& container)
{
Hook::Signature::Signatures.push_back(container);
}
void Hook::Interceptor::Install(void* place, void(*stub)())
{
return Hook::Interceptor::Install(reinterpret_cast<void**>(place), stub);

View File

@ -8,6 +8,29 @@ namespace Utils
class Hook
{
public:
class Signature
{
public:
struct Container
{
const char* Signature;
const char* Mask;
std::function<void(char*)> Callback;
};
Signature(void* start, size_t length) : Start(start), Length(length) {}
Signature(DWORD start, size_t length) : Signature(reinterpret_cast<void*>(start), length) {}
Signature() : Signature(0x400000, 0x800000) {}
void Process();
void Add(Container& container);
private:
void* Start;
size_t Length;
std::vector<Container> Signatures;
};
class Interceptor
{
public:

View File

@ -268,4 +268,4 @@ namespace Utils
this->KeyValuePairs[KeyValues[i]] = KeyValues[i + 1];
}
}
}
}