2016-02-01 04:23:06 -05:00
|
|
|
namespace Utils
|
|
|
|
{
|
|
|
|
namespace Cryptography
|
|
|
|
{
|
2016-02-21 18:01:20 -05:00
|
|
|
class Token
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Token() { this->TokenString.clear(); };
|
2016-02-22 07:37:13 -05:00
|
|
|
Token(const Token& obj) : TokenString(obj.TokenString) { };
|
2016-02-21 18:01:20 -05:00
|
|
|
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);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ToString()
|
|
|
|
{
|
2016-02-22 07:37:13 -05:00
|
|
|
return std::string(this->TokenString.begin(), this->TokenString.end());
|
2016-02-21 18:01:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
std::basic_string<uint8_t> ToUnsignedString()
|
|
|
|
{
|
|
|
|
return this->TokenString;
|
|
|
|
}
|
|
|
|
|
2016-02-22 07:37:13 -05:00
|
|
|
void Clear()
|
|
|
|
{
|
|
|
|
this->TokenString.clear();
|
|
|
|
}
|
|
|
|
|
2016-02-21 18:01:20 -05:00
|
|
|
private:
|
|
|
|
std::basic_string<uint8_t> TokenString;
|
|
|
|
};
|
|
|
|
|
2016-02-08 08:27:15 -05:00
|
|
|
class Rand
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static uint32_t GenerateInt();
|
2016-02-19 17:57:06 -05:00
|
|
|
static void Initialize();
|
|
|
|
|
|
|
|
private:
|
|
|
|
static prng_state State;
|
2016-02-08 08:27:15 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
class ECDSA
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
class Key
|
|
|
|
{
|
|
|
|
public:
|
2016-02-08 12:43:31 -05:00
|
|
|
Key() : KeyStorage(new ecc_key)
|
|
|
|
{
|
2016-02-21 13:57:56 -05:00
|
|
|
ZeroMemory(this->KeyStorage.get(), sizeof(*this->GetKeyPtr()));
|
2016-02-08 12:43:31 -05:00
|
|
|
};
|
2016-02-21 13:57:56 -05:00
|
|
|
Key(ecc_key* key) : Key() { if(key) std::memmove(this->GetKeyPtr(), key, sizeof(*key)); };
|
2016-02-08 12:43:31 -05:00
|
|
|
Key(ecc_key key) : Key(&key) {};
|
|
|
|
~Key()
|
|
|
|
{
|
|
|
|
if (this->KeyStorage.use_count() <= 1)
|
|
|
|
{
|
|
|
|
this->Free();
|
|
|
|
}
|
|
|
|
};
|
2016-02-08 08:27:15 -05:00
|
|
|
|
2016-02-08 12:43:31 -05:00
|
|
|
bool IsValid()
|
|
|
|
{
|
2016-02-21 13:57:56 -05:00
|
|
|
return (!Utils::MemIsSet(this->GetKeyPtr(), 0, sizeof(*this->GetKeyPtr())));
|
2016-02-08 12:43:31 -05:00
|
|
|
}
|
2016-02-08 08:27:15 -05:00
|
|
|
|
|
|
|
ecc_key* GetKeyPtr()
|
|
|
|
{
|
2016-02-08 12:43:31 -05:00
|
|
|
return this->KeyStorage.get();
|
2016-02-08 08:27:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string GetPublicKey()
|
|
|
|
{
|
2016-02-08 12:43:31 -05:00
|
|
|
uint8_t buffer[512] = { 0 };
|
2016-02-08 08:27:15 -05:00
|
|
|
DWORD length = sizeof(buffer);
|
|
|
|
|
|
|
|
if (ecc_ansi_x963_export(this->GetKeyPtr(), buffer, &length) == CRYPT_OK)
|
|
|
|
{
|
|
|
|
return std::string(reinterpret_cast<char*>(buffer), length);
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2016-02-08 12:43:31 -05:00
|
|
|
void Set(std::string pubKeyBuffer)
|
|
|
|
{
|
|
|
|
this->Free();
|
|
|
|
|
2016-02-21 13:57:56 -05:00
|
|
|
if (ecc_ansi_x963_import(reinterpret_cast<const uint8_t*>(pubKeyBuffer.data()), pubKeyBuffer.size(), this->GetKeyPtr()) != CRYPT_OK)
|
2016-02-08 12:43:31 -05:00
|
|
|
{
|
2016-02-21 13:57:56 -05:00
|
|
|
ZeroMemory(this->KeyStorage.get(), sizeof(*this->GetKeyPtr()));
|
2016-02-08 12:43:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-21 13:57:56 -05:00
|
|
|
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 "";
|
|
|
|
}
|
|
|
|
|
2016-02-08 12:43:31 -05:00
|
|
|
void Free()
|
|
|
|
{
|
|
|
|
if (this->IsValid())
|
|
|
|
{
|
2016-02-21 13:57:56 -05:00
|
|
|
ecc_free(this->GetKeyPtr());
|
2016-02-08 12:43:31 -05:00
|
|
|
}
|
|
|
|
|
2016-02-21 13:57:56 -05:00
|
|
|
ZeroMemory(this->GetKeyPtr(), sizeof(*this->GetKeyPtr()));
|
2016-02-08 12:43:31 -05:00
|
|
|
}
|
|
|
|
|
2016-02-08 08:27:15 -05:00
|
|
|
private:
|
2016-02-08 12:43:31 -05:00
|
|
|
std::shared_ptr<ecc_key> KeyStorage;
|
2016-02-08 08:27:15 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2016-02-01 04:23:06 -05:00
|
|
|
class RSA
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
class Key
|
|
|
|
{
|
|
|
|
public:
|
2016-02-08 12:43:31 -05:00
|
|
|
Key() : KeyStorage(new rsa_key)
|
|
|
|
{
|
2016-02-21 13:57:56 -05:00
|
|
|
ZeroMemory(this->KeyStorage.get(), sizeof(*this->GetKeyPtr()));
|
2016-02-08 12:43:31 -05:00
|
|
|
};
|
2016-02-21 13:57:56 -05:00
|
|
|
Key(rsa_key* key) : Key() { if (key) std::memmove(this->GetKeyPtr(), key, sizeof(*key)); };
|
2016-02-08 12:43:31 -05:00
|
|
|
Key(rsa_key key) : Key(&key) {};
|
|
|
|
~Key()
|
|
|
|
{
|
|
|
|
if (this->KeyStorage.use_count() <= 1)
|
|
|
|
{
|
|
|
|
this->Free();
|
|
|
|
}
|
|
|
|
};
|
2016-02-01 04:23:06 -05:00
|
|
|
|
|
|
|
rsa_key* GetKeyPtr()
|
|
|
|
{
|
2016-02-08 12:43:31 -05:00
|
|
|
return this->KeyStorage.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsValid()
|
|
|
|
{
|
2016-02-21 13:57:56 -05:00
|
|
|
return (!Utils::MemIsSet(this->GetKeyPtr(), 0, sizeof(*this->GetKeyPtr())));
|
2016-02-08 12:43:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void Free()
|
|
|
|
{
|
|
|
|
if (this->IsValid())
|
|
|
|
{
|
2016-02-21 13:57:56 -05:00
|
|
|
rsa_free(this->GetKeyPtr());
|
2016-02-08 12:43:31 -05:00
|
|
|
}
|
|
|
|
|
2016-02-21 13:57:56 -05:00
|
|
|
ZeroMemory(this->GetKeyPtr(), sizeof(*this->GetKeyPtr()));
|
2016-02-01 04:23:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-02-08 12:43:31 -05:00
|
|
|
std::shared_ptr<rsa_key> KeyStorage;
|
2016-02-01 04:23:06 -05:00
|
|
|
};
|
|
|
|
|
2016-02-08 08:27:15 -05:00
|
|
|
static Key GenerateKey(int bits);
|
2016-02-01 04:23:06 -05:00
|
|
|
static std::string SignMessage(Key key, std::string message);
|
|
|
|
static bool VerifyMessage(Key key, std::string message, std::string signature);
|
|
|
|
};
|
2016-02-22 17:35:53 -05:00
|
|
|
|
|
|
|
class SHA256
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static std::string Compute(std::string data, bool hex = false);
|
2016-02-29 08:47:21 -05:00
|
|
|
static std::string Compute(const uint8_t* data, size_t length, bool hex = false);
|
2016-02-22 17:35:53 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
class SHA512
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static std::string Compute(std::string data, bool hex = false);
|
2016-02-29 08:47:21 -05:00
|
|
|
static std::string Compute(const uint8_t* data, size_t length, bool hex = false);
|
2016-02-22 17:35:53 -05:00
|
|
|
};
|
2016-02-01 04:23:06 -05:00
|
|
|
}
|
|
|
|
}
|