maint(utils): clean up crypto utils

This commit is contained in:
Diavolo 2023-06-21 20:47:09 +02:00
parent 0da8167a0a
commit e26eed4c11
No known key found for this signature in database
GPG Key ID: FA77F074E98D98A5
2 changed files with 61 additions and 63 deletions

View File

@ -61,7 +61,7 @@ namespace Utils
{ {
if (!key.isValid()) return {}; if (!key.isValid()) return {};
std::uint8_t buffer[512]; std::uint8_t buffer[512]{};
unsigned long length = sizeof(buffer); unsigned long length = sizeof(buffer);
ltc_mp = ltm_desc; ltc_mp = ltm_desc;
@ -145,9 +145,9 @@ namespace Utils
encData.resize(text.size()); encData.resize(text.size());
symmetric_CBC cbc; symmetric_CBC cbc;
int des3 = find_cipher("3des"); const auto des3 = find_cipher("3des");
cbc_start(des3, reinterpret_cast<const std::uint8_t*>(iv.data()), reinterpret_cast<const std::uint8_t*>(key.data()), key.size(), 0, &cbc); cbc_start(des3, reinterpret_cast<const std::uint8_t*>(iv.data()), reinterpret_cast<const std::uint8_t*>(key.data()), static_cast<int>(key.size()), 0, &cbc);
cbc_encrypt(reinterpret_cast<const std::uint8_t*>(text.data()), reinterpret_cast<uint8_t*>(encData.data()), text.size(), &cbc); cbc_encrypt(reinterpret_cast<const std::uint8_t*>(text.data()), reinterpret_cast<uint8_t*>(encData.data()), text.size(), &cbc);
cbc_done(&cbc); cbc_done(&cbc);
@ -160,9 +160,9 @@ namespace Utils
decData.resize(data.size()); decData.resize(data.size());
symmetric_CBC cbc; symmetric_CBC cbc;
int des3 = find_cipher("3des"); const auto des3 = find_cipher("3des");
cbc_start(des3, reinterpret_cast<const std::uint8_t*>(iv.data()), reinterpret_cast<const std::uint8_t*>(key.data()), key.size(), 0, &cbc); cbc_start(des3, reinterpret_cast<const std::uint8_t*>(iv.data()), reinterpret_cast<const std::uint8_t*>(key.data()), static_cast<int>(key.size()), 0, &cbc);
cbc_decrypt(reinterpret_cast<const std::uint8_t*>(data.data()), reinterpret_cast<std::uint8_t*>(decData.data()), data.size(), &cbc); cbc_decrypt(reinterpret_cast<const std::uint8_t*>(data.data()), reinterpret_cast<std::uint8_t*>(decData.data()), data.size(), &cbc);
cbc_done(&cbc); cbc_done(&cbc);
@ -269,20 +269,21 @@ namespace Utils
#pragma region JenkinsOneAtATime #pragma region JenkinsOneAtATime
unsigned int JenkinsOneAtATime::Compute(const std::string& data) std::size_t JenkinsOneAtATime::Compute(const std::string& data)
{ {
return Compute(data.data(), data.size()); return Compute(data.data(), data.size());
} }
unsigned int JenkinsOneAtATime::Compute(const char* key, std::size_t len) std::size_t JenkinsOneAtATime::Compute(const char* key, const std::size_t len)
{ {
unsigned int hash, i; std::size_t hash, i;
for (hash = i = 0; i < len; ++i) for (hash = i = 0; i < len; ++i)
{ {
hash += key[i]; hash += key[i];
hash += (hash << 10); hash += (hash << 10);
hash ^= (hash >> 6); hash ^= (hash >> 6);
} }
hash += (hash << 3); hash += (hash << 3);
hash ^= (hash >> 11); hash ^= (hash >> 11);
hash += (hash << 15); hash += (hash << 15);

View File

@ -9,16 +9,16 @@ namespace Utils
class Token class Token
{ {
public: public:
Token() { this->tokenString.clear(); }; Token() { this->tokenString.clear(); }
Token(const Token& obj) : tokenString(obj.tokenString) { }; Token(const Token& obj) : tokenString(obj.tokenString) { }
Token(const std::string& token) : tokenString(token.begin(), token.end()) { }; Token(const std::string& token) : tokenString(token.begin(), token.end()) { }
Token(const std::basic_string<uint8_t>& token) : tokenString(token.begin(), token.end()) { }; Token(const std::basic_string<std::uint8_t>& token) : tokenString(token.begin(), token.end()) { }
Token& operator++ () Token& operator++ ()
{ {
if (this->tokenString.empty()) if (this->tokenString.empty())
{ {
this->tokenString.append(reinterpret_cast<uint8_t*>(const_cast<char *>("\0")), 1); this->tokenString.append(reinterpret_cast<std::uint8_t*>(const_cast<char *>("\0")), 1);
} }
else else
{ {
@ -30,7 +30,7 @@ namespace Utils
if (!i) if (!i)
{ {
this->tokenString = std::basic_string<std::uint8_t>(reinterpret_cast<std::uint8_t*>(const_cast<char*>("\0")), 1) + this->tokenString; this->tokenString = std::basic_string{ reinterpret_cast<std::uint8_t*>(const_cast<char*>("\0")), 1 } + this->tokenString;
break; break;
} }
} }
@ -68,27 +68,27 @@ namespace Utils
{ {
return false; return false;
} }
else if (this->toString().size() < token.toString().size())
if (this->toString().size() < token.toString().size())
{ {
return true; return true;
} }
else if (this->toString().size() > token.toString().size())
if (this->toString().size() > token.toString().size())
{ {
return false; return false;
} }
else
{
auto lStr = this->toString(); auto lStr = this->toString();
auto rStr = token.toString(); auto rStr = token.toString();
for (unsigned int i = 0; i < lStr.size(); ++i) for (std::size_t i = 0; i < lStr.size(); ++i)
{ {
if (lStr[i] < rStr[i]) if (lStr[i] < rStr[i])
{ {
return true; return true;
} }
} }
}
return false; return false;
} }
@ -108,17 +108,12 @@ namespace Utils
return !(*this < token); return !(*this < token);
} }
std::string toString() [[nodiscard]] std::string toString() const
{ {
return std::string(this->tokenString.begin(), this->tokenString.end()); return std::string{ this->tokenString.begin(), this->tokenString.end() };
} }
std::string toString() const [[nodiscard]] std::basic_string<std::uint8_t> toUnsignedString() const
{
return std::string(this->tokenString.begin(), this->tokenString.end());
}
std::basic_string<uint8_t> toUnsignedString()
{ {
return this->tokenString; return this->tokenString;
} }
@ -136,7 +131,7 @@ namespace Utils
{ {
public: public:
static std::string GenerateChallenge(); static std::string GenerateChallenge();
static uint32_t GenerateInt(); static std::uint32_t GenerateInt();
static void Initialize(); static void Initialize();
private: private:
@ -152,45 +147,47 @@ namespace Utils
Key() : keyStorage(new ecc_key) Key() : keyStorage(new ecc_key)
{ {
ZeroMemory(this->getKeyPtr(), sizeof(*this->getKeyPtr())); 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(ecc_key* key) : Key() { if (key) std::memmove(this->getKeyPtr(), key, sizeof(*key)); }
Key(ecc_key key) : Key(&key) {}
~Key() ~Key()
{ {
if (this->keyStorage.use_count() <= 1) if (this->keyStorage.use_count() <= 1)
{ {
this->free(); this->free();
} }
};
bool isValid()
{
return (!Utils::Memory::IsSet(this->getKeyPtr(), 0, sizeof(*this->getKeyPtr())));
} }
ecc_key* getKeyPtr() [[nodiscard]] bool isValid()
{
return (!Memory::IsSet(this->getKeyPtr(), 0, sizeof(*this->getKeyPtr())));
}
[[nodiscard]] ecc_key* getKeyPtr()
{ {
return this->keyStorage.get(); return this->keyStorage.get();
} }
std::string getPublicKey() [[nodiscard]] std::string getPublicKey()
{ {
uint8_t buffer[512] = { 0 }; std::uint8_t buffer[512]{};
DWORD length = sizeof(buffer); DWORD length = sizeof(buffer);
if (ecc_ansi_x963_export(this->getKeyPtr(), buffer, &length) == CRYPT_OK) if (ecc_ansi_x963_export(this->getKeyPtr(), buffer, &length) == CRYPT_OK)
{ {
return std::string(reinterpret_cast<char*>(buffer), length); return std::string{ reinterpret_cast<char*>(buffer), length };
} }
return ""; return std::string{};
} }
void set(const std::string& pubKeyBuffer) void set(const std::string& pubKeyBuffer)
{ {
this->free(); this->free();
if (ecc_ansi_x963_import(reinterpret_cast<const uint8_t*>(pubKeyBuffer.data()), pubKeyBuffer.size(), this->getKeyPtr()) != CRYPT_OK) if (ecc_ansi_x963_import(reinterpret_cast<const std::uint8_t*>(pubKeyBuffer.data()), pubKeyBuffer.size(), this->getKeyPtr()) != CRYPT_OK)
{ {
ZeroMemory(this->getKeyPtr(), sizeof(*this->getKeyPtr())); ZeroMemory(this->getKeyPtr(), sizeof(*this->getKeyPtr()));
} }
@ -200,23 +197,23 @@ namespace Utils
{ {
this->free(); this->free();
if (ecc_import(reinterpret_cast<const uint8_t*>(key.data()), key.size(), this->getKeyPtr()) != CRYPT_OK) if (ecc_import(reinterpret_cast<const std::uint8_t*>(key.data()), key.size(), this->getKeyPtr()) != CRYPT_OK)
{ {
ZeroMemory(this->getKeyPtr(), sizeof(*this->getKeyPtr())); ZeroMemory(this->getKeyPtr(), sizeof(*this->getKeyPtr()));
} }
} }
std::string serialize(int type = PK_PRIVATE) [[nodiscard]] std::string serialize(int type = PK_PRIVATE)
{ {
uint8_t buffer[4096] = { 0 }; std::uint8_t buffer[4096]{};
DWORD length = sizeof(buffer); DWORD length = sizeof(buffer);
if (ecc_export(buffer, &length, type, this->getKeyPtr()) == CRYPT_OK) if (ecc_export(buffer, &length, type, this->getKeyPtr()) == CRYPT_OK)
{ {
return std::string(reinterpret_cast<char*>(buffer), length); return std::string{ reinterpret_cast<char*>(buffer), length };
} }
return ""; return std::string{};
} }
void free() void free()
@ -265,16 +262,16 @@ namespace Utils
} }
} }
rsa_key* getKeyPtr() [[nodiscard]] bool isValid()
{
return this->keyStorage.get();
}
bool isValid()
{ {
return (!Memory::IsSet(this->getKeyPtr(), 0, sizeof(*this->getKeyPtr()))); return (!Memory::IsSet(this->getKeyPtr(), 0, sizeof(*this->getKeyPtr())));
} }
[[nodiscard]] rsa_key* getKeyPtr()
{
return this->keyStorage.get();
}
void free() void free()
{ {
if (this->isValid()) if (this->isValid())
@ -333,8 +330,8 @@ namespace Utils
class JenkinsOneAtATime class JenkinsOneAtATime
{ {
public: public:
static unsigned int Compute(const std::string& data); static std::size_t Compute(const std::string& data);
static unsigned int Compute(const char* key, std::size_t len); static std::size_t Compute(const char* key, std::size_t len);
}; };
} }
} }