More fixes and optimizations

This commit is contained in:
momo5502
2016-07-11 17:37:36 +02:00
parent 9562285ed7
commit 2ea490382b
16 changed files with 934 additions and 927 deletions

View File

@ -35,7 +35,9 @@ namespace Utils
}
int ret = 0;
uint8_t* dest = Utils::Memory::AllocateArray<uint8_t>(CHUNK);
Utils::Memory::Allocator allocator;
uint8_t* dest = allocator.AllocateArray<uint8_t>(CHUNK);
const char* dataPtr = data.data();
do
@ -52,7 +54,6 @@ namespace Utils
if (ret == Z_STREAM_ERROR)
{
inflateEnd(&stream);
Utils::Memory::Free(dest);
return "";
}
@ -63,9 +64,6 @@ namespace Utils
} while (ret != Z_STREAM_END);
inflateEnd(&stream);
Utils::Memory::Free(dest);
return buffer;
}
};

View File

@ -1,324 +1,324 @@
namespace Utils
{
namespace Cryptography
{
void Initialize();
class Token
{
public:
Token() { this->TokenString.clear(); };
Token(const Token& obj) : TokenString(obj.TokenString) { };
Token(std::string token) : TokenString(token.begin(), token.end()) { };
Token(std::basic_string<uint8_t> token) : TokenString(token.begin(), token.end()) { };
Token& operator++ ()
{
if (this->TokenString.empty())
{
this->TokenString.append(reinterpret_cast<uint8_t*>("\0"), 1);
}
else
{
for (unsigned int i = (this->TokenString.size() - 1); i >= 0; i--)
{
if (this->TokenString[i] == 0xFF)
{
this->TokenString[i] = 0;
if (!i)
{
// Prepend here, as /dev/urandom says so ;) https://github.com/IW4x/iw4x-client-node/wikis/technical-information#incrementing-the-token
this->TokenString = std::basic_string<uint8_t>(reinterpret_cast<uint8_t*>("\0"), 1) + this->TokenString;
break;
}
}
else
{
++this->TokenString[i];
break;
}
}
}
return *this;
}
Token operator++ (int)
{
Token result = *this;
this->operator++();
return result;
}
bool operator==(const Token& token) const
{
return (this->ToString() == token.ToString());
}
bool operator!=(const Token& token) const
{
return !(*this == token);
}
bool operator<(const Token& token) const
{
if (*this == token)
{
return false;
}
else if (this->ToString().size() < token.ToString().size())
{
return true;
}
else if (this->ToString().size() > token.ToString().size())
{
return false;
}
else
{
auto lStr = this->ToString();
auto rStr = token.ToString();
for (unsigned int i = 0; i < lStr.size(); ++i)
{
if (lStr[i] < rStr[i])
{
return true;
}
}
}
return false;
}
bool operator>(const Token& token) const
{
return (token < *this && *this != token);
}
bool operator<=(const Token& token) const
{
return !(*this > token);
}
bool operator>=(const Token& token) const
{
return !(*this < token);
}
std::string ToString()
{
return std::string(this->TokenString.begin(), this->TokenString.end());
}
const std::string ToString() const
{
return std::string(this->TokenString.begin(), this->TokenString.end());
}
std::basic_string<uint8_t> ToUnsignedString()
{
return this->TokenString;
}
void Clear()
{
this->TokenString.clear();
}
private:
std::basic_string<uint8_t> TokenString;
};
class Rand
{
public:
static uint32_t GenerateInt();
static void Initialize();
private:
static prng_state State;
};
class ECC
{
public:
class Key
{
public:
Key() : KeyStorage(new ecc_key)
{
ZeroMemory(this->GetKeyPtr(), sizeof(*this->GetKeyPtr()));
};
Key(ecc_key* key) : Key() { if(key) std::memmove(this->GetKeyPtr(), key, sizeof(*key)); };
Key(ecc_key key) : Key(&key) {};
~Key()
{
if (this->KeyStorage.use_count() <= 1)
{
this->Free();
}
};
bool IsValid()
{
return (!Utils::MemIsSet(this->GetKeyPtr(), 0, sizeof(*this->GetKeyPtr())));
}
ecc_key* GetKeyPtr()
{
return this->KeyStorage.get();
}
std::string GetPublicKey()
{
uint8_t buffer[512] = { 0 };
DWORD length = sizeof(buffer);
if (ecc_ansi_x963_export(this->GetKeyPtr(), buffer, &length) == CRYPT_OK)
{
return std::string(reinterpret_cast<char*>(buffer), length);
}
return "";
}
void Set(std::string pubKeyBuffer)
{
this->Free();
if (ecc_ansi_x963_import(reinterpret_cast<const uint8_t*>(pubKeyBuffer.data()), pubKeyBuffer.size(), this->GetKeyPtr()) != CRYPT_OK)
{
ZeroMemory(this->KeyStorage.get(), sizeof(*this->GetKeyPtr()));
}
}
void Import(std::string key, int type = PK_PRIVATE)
{
this->Free();
if (ecc_import(reinterpret_cast<const uint8_t*>(key.data()), key.size(), this->GetKeyPtr()) != CRYPT_OK)
{
ZeroMemory(this->KeyStorage.get(), sizeof(*this->GetKeyPtr()));
}
}
std::string Export(int type = PK_PRIVATE)
{
uint8_t buffer[4096] = { 0 };
DWORD length = sizeof(buffer);
if (ecc_export(buffer, &length, type, this->GetKeyPtr()) == CRYPT_OK)
{
return std::string(reinterpret_cast<char*>(buffer), length);
}
return "";
}
void Free()
{
if (this->IsValid())
{
ecc_free(this->GetKeyPtr());
}
ZeroMemory(this->GetKeyPtr(), sizeof(*this->GetKeyPtr()));
}
private:
std::shared_ptr<ecc_key> KeyStorage;
};
static Key GenerateKey(int bits);
static std::string SignMessage(Key key, std::string message);
static bool VerifyMessage(Key key, std::string message, std::string signature);
};
class RSA
{
public:
class Key
{
public:
Key() : KeyStorage(new rsa_key)
{
ZeroMemory(this->GetKeyPtr(), sizeof(*this->GetKeyPtr()));
};
Key(rsa_key* key) : Key() { if (key) std::memmove(this->GetKeyPtr(), key, sizeof(*key)); };
Key(rsa_key key) : Key(&key) {};
~Key()
{
if (this->KeyStorage.use_count() <= 1)
{
this->Free();
}
};
rsa_key* GetKeyPtr()
{
return this->KeyStorage.get();
}
bool IsValid()
{
return (!Utils::MemIsSet(this->GetKeyPtr(), 0, sizeof(*this->GetKeyPtr())));
}
void Free()
{
if (this->IsValid())
{
rsa_free(this->GetKeyPtr());
}
ZeroMemory(this->GetKeyPtr(), sizeof(*this->GetKeyPtr()));
}
private:
std::shared_ptr<rsa_key> KeyStorage;
};
static Key GenerateKey(int bits);
static std::string SignMessage(Key key, std::string message);
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:
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 SHA512
{
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 JenkinsOneAtATime
{
public:
static unsigned int Compute(std::string data);
static unsigned int Compute(const char *key, size_t len);
};
}
}
namespace Utils
{
namespace Cryptography
{
void Initialize();
class Token
{
public:
Token() { this->TokenString.clear(); };
Token(const Token& obj) : TokenString(obj.TokenString) { };
Token(std::string token) : TokenString(token.begin(), token.end()) { };
Token(std::basic_string<uint8_t> token) : TokenString(token.begin(), token.end()) { };
Token& operator++ ()
{
if (this->TokenString.empty())
{
this->TokenString.append(reinterpret_cast<uint8_t*>("\0"), 1);
}
else
{
for (unsigned int i = (this->TokenString.size() - 1); i >= 0; i--)
{
if (this->TokenString[i] == 0xFF)
{
this->TokenString[i] = 0;
if (!i)
{
// Prepend here, as /dev/urandom says so ;) https://github.com/IW4x/iw4x-client-node/wikis/technical-information#incrementing-the-token
this->TokenString = std::basic_string<uint8_t>(reinterpret_cast<uint8_t*>("\0"), 1) + this->TokenString;
break;
}
}
else
{
++this->TokenString[i];
break;
}
}
}
return *this;
}
Token operator++ (int)
{
Token result = *this;
this->operator++();
return result;
}
bool operator==(const Token& token) const
{
return (this->ToString() == token.ToString());
}
bool operator!=(const Token& token) const
{
return !(*this == token);
}
bool operator<(const Token& token) const
{
if (*this == token)
{
return false;
}
else if (this->ToString().size() < token.ToString().size())
{
return true;
}
else if (this->ToString().size() > token.ToString().size())
{
return false;
}
else
{
auto lStr = this->ToString();
auto rStr = token.ToString();
for (unsigned int i = 0; i < lStr.size(); ++i)
{
if (lStr[i] < rStr[i])
{
return true;
}
}
}
return false;
}
bool operator>(const Token& token) const
{
return (token < *this && *this != token);
}
bool operator<=(const Token& token) const
{
return !(*this > token);
}
bool operator>=(const Token& token) const
{
return !(*this < token);
}
std::string ToString()
{
return std::string(this->TokenString.begin(), this->TokenString.end());
}
const std::string ToString() const
{
return std::string(this->TokenString.begin(), this->TokenString.end());
}
std::basic_string<uint8_t> ToUnsignedString()
{
return this->TokenString;
}
void Clear()
{
this->TokenString.clear();
}
private:
std::basic_string<uint8_t> TokenString;
};
class Rand
{
public:
static uint32_t GenerateInt();
static void Initialize();
private:
static prng_state State;
};
class ECC
{
public:
class Key
{
public:
Key() : KeyStorage(new ecc_key)
{
ZeroMemory(this->GetKeyPtr(), sizeof(*this->GetKeyPtr()));
};
Key(ecc_key* key) : Key() { if(key) std::memmove(this->GetKeyPtr(), key, sizeof(*key)); };
Key(ecc_key key) : Key(&key) {};
~Key()
{
if (this->KeyStorage.use_count() <= 1)
{
this->Free();
}
};
bool IsValid()
{
return (!Utils::Memory::IsSet(this->GetKeyPtr(), 0, sizeof(*this->GetKeyPtr())));
}
ecc_key* GetKeyPtr()
{
return this->KeyStorage.get();
}
std::string GetPublicKey()
{
uint8_t buffer[512] = { 0 };
DWORD length = sizeof(buffer);
if (ecc_ansi_x963_export(this->GetKeyPtr(), buffer, &length) == CRYPT_OK)
{
return std::string(reinterpret_cast<char*>(buffer), length);
}
return "";
}
void Set(std::string pubKeyBuffer)
{
this->Free();
if (ecc_ansi_x963_import(reinterpret_cast<const uint8_t*>(pubKeyBuffer.data()), pubKeyBuffer.size(), this->GetKeyPtr()) != CRYPT_OK)
{
ZeroMemory(this->KeyStorage.get(), sizeof(*this->GetKeyPtr()));
}
}
void Import(std::string key, int type = PK_PRIVATE)
{
this->Free();
if (ecc_import(reinterpret_cast<const uint8_t*>(key.data()), key.size(), this->GetKeyPtr()) != CRYPT_OK)
{
ZeroMemory(this->KeyStorage.get(), sizeof(*this->GetKeyPtr()));
}
}
std::string Export(int type = PK_PRIVATE)
{
uint8_t buffer[4096] = { 0 };
DWORD length = sizeof(buffer);
if (ecc_export(buffer, &length, type, this->GetKeyPtr()) == CRYPT_OK)
{
return std::string(reinterpret_cast<char*>(buffer), length);
}
return "";
}
void Free()
{
if (this->IsValid())
{
ecc_free(this->GetKeyPtr());
}
ZeroMemory(this->GetKeyPtr(), sizeof(*this->GetKeyPtr()));
}
private:
std::shared_ptr<ecc_key> KeyStorage;
};
static Key GenerateKey(int bits);
static std::string SignMessage(Key key, std::string message);
static bool VerifyMessage(Key key, std::string message, std::string signature);
};
class RSA
{
public:
class Key
{
public:
Key() : KeyStorage(new rsa_key)
{
ZeroMemory(this->GetKeyPtr(), sizeof(*this->GetKeyPtr()));
};
Key(rsa_key* key) : Key() { if (key) std::memmove(this->GetKeyPtr(), key, sizeof(*key)); };
Key(rsa_key key) : Key(&key) {};
~Key()
{
if (this->KeyStorage.use_count() <= 1)
{
this->Free();
}
};
rsa_key* GetKeyPtr()
{
return this->KeyStorage.get();
}
bool IsValid()
{
return (!Utils::Memory::IsSet(this->GetKeyPtr(), 0, sizeof(*this->GetKeyPtr())));
}
void Free()
{
if (this->IsValid())
{
rsa_free(this->GetKeyPtr());
}
ZeroMemory(this->GetKeyPtr(), sizeof(*this->GetKeyPtr()));
}
private:
std::shared_ptr<rsa_key> KeyStorage;
};
static Key GenerateKey(int bits);
static std::string SignMessage(Key key, std::string message);
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:
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 SHA512
{
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 JenkinsOneAtATime
{
public:
static unsigned int Compute(std::string data);
static unsigned int Compute(const char *key, size_t len);
};
}
}

View File

@ -1,38 +1,54 @@
#include "STDInclude.hpp"
namespace Utils
{
void* Memory::Allocate(size_t length)
{
void* data = new char[length];
assert(data != nullptr);
if (data)
{
ZeroMemory(data, length);
}
return data;
}
char* Memory::DuplicateString(std::string string)
{
char* newString = Memory::AllocateArray<char>(string.size() + 1);
std::memcpy(newString, string.data(), string.size());
return newString;
}
void Memory::Free(void* data)
{
if (data)
{
delete[] data;
}
}
void Memory::Free(const void* data)
{
Memory::Free(const_cast<void*>(data));
}
}
#include "STDInclude.hpp"
namespace Utils
{
void* Memory::Allocate(size_t length)
{
void* data = new char[length];
assert(data != nullptr);
if (data)
{
ZeroMemory(data, length);
}
return data;
}
char* Memory::DuplicateString(std::string string)
{
char* newString = Memory::AllocateArray<char>(string.size() + 1);
std::memcpy(newString, string.data(), string.size());
return newString;
}
void Memory::Free(void* data)
{
if (data)
{
delete[] data;
}
}
void Memory::Free(const void* data)
{
Memory::Free(const_cast<void*>(data));
}
// Complementary function for memset, which checks if memory is filled with a char
bool Memory::IsSet(void* mem, char chr, size_t length)
{
char* memArr = reinterpret_cast<char*>(mem);
for (size_t i = 0; i < length; ++i)
{
if (memArr[i] != chr)
{
return false;
}
}
return true;
}
}

View File

@ -1,80 +1,90 @@
namespace Utils
{
class Memory
{
public:
class Allocator
{
public:
typedef void(*FreeCallback)(void*);
Allocator()
{
this->Pool.clear();
this->RefMemory.clear();
}
~Allocator()
{
this->Free();
}
void Free()
{
for (auto i = this->RefMemory.begin(); i != this->RefMemory.end(); ++i)
{
if (i->first && i->second)
{
i->second(i->first);
}
}
this->RefMemory.clear();
for (auto data : this->Pool)
{
Memory::Free(data);
}
this->Pool.clear();
}
void Reference(void* memory, FreeCallback callback)
{
this->RefMemory[memory] = callback;
}
void* Allocate(size_t length)
{
void* data = Memory::Allocate(length);
this->Pool.push_back(data);
return data;
}
template <typename T> T* AllocateArray(size_t count = 1)
{
return static_cast<T*>(this->Allocate(count * sizeof(T)));
}
char* DuplicateString(std::string string)
{
char* data = Memory::DuplicateString(string);
this->Pool.push_back(data);
return data;
}
private:
std::vector<void*> Pool;
std::map<void*, FreeCallback> RefMemory;
};
static void* Allocate(size_t length);
template <typename T> static T* AllocateArray(size_t count = 1)
{
return static_cast<T*>(Allocate(count * sizeof(T)));
}
static char* DuplicateString(std::string string);
static void Free(void* data);
static void Free(const void* data);
};
}
namespace Utils
{
class Memory
{
public:
class Allocator
{
public:
typedef void(*FreeCallback)(void*);
Allocator()
{
this->Pool.clear();
this->RefMemory.clear();
}
~Allocator()
{
this->Free();
}
void Free()
{
for (auto i = this->RefMemory.begin(); i != this->RefMemory.end(); ++i)
{
if (i->first && i->second)
{
i->second(i->first);
}
}
this->RefMemory.clear();
for (auto data : this->Pool)
{
Memory::Free(data);
}
this->Pool.clear();
}
void Reference(void* memory, FreeCallback callback)
{
this->RefMemory[memory] = callback;
}
void* Allocate(size_t length)
{
void* data = Memory::Allocate(length);
this->Pool.push_back(data);
return data;
}
template <typename T> T* Allocate()
{
return this->AllocateArray<T>(1);
}
template <typename T> T* AllocateArray(size_t count = 1)
{
return static_cast<T*>(this->Allocate(count * sizeof(T)));
}
char* DuplicateString(std::string string)
{
char* data = Memory::DuplicateString(string);
this->Pool.push_back(data);
return data;
}
private:
std::vector<void*> Pool;
std::map<void*, FreeCallback> RefMemory;
};
static void* Allocate(size_t length);
template <typename T> static T* Allocate()
{
return AllocateArray<T>(1);
}
template <typename T> static T* AllocateArray(size_t count = 1)
{
return static_cast<T*>(Allocate(count * sizeof(T)));
}
static char* DuplicateString(std::string string);
static void Free(void* data);
static void Free(const void* data);
static bool IsSet(void* mem, char chr, size_t length);
};
}

View File

@ -16,22 +16,6 @@ namespace Utils
return "application/octet-stream";
}
// Complementary function for memset, which checks if a memory is set
bool MemIsSet(void* mem, char chr, size_t length)
{
char* memArr = reinterpret_cast<char*>(mem);
for (size_t i = 0; i < length; ++i)
{
if (memArr[i] != chr)
{
return false;
}
}
return true;
}
std::string ParseChallenge(std::string data)
{
auto pos = data.find_first_of("\n ");

View File

@ -2,7 +2,6 @@ namespace Utils
{
std::string GetMimeType(std::string url);
std::string ParseChallenge(std::string data);
bool MemIsSet(void* mem, char chr, size_t length);
template <typename T> void Merge(std::vector<T>* target, T* source, size_t length)
{