diff --git a/premake5.lua b/premake5.lua index e0be302a..53f7714d 100644 --- a/premake5.lua +++ b/premake5.lua @@ -117,7 +117,7 @@ group "" -- Projects -- ======================== include "src/Common.lua" -include "src/Crypto.lua" +include "src/Cryptography.lua" include "src/ImageConverter.lua" include "src/Linker.lua" include "src/Parser.lua" @@ -141,7 +141,7 @@ include "tools/scripts/raw.lua" -- Components group: All projects assist or are part of a tool group "Components" Common:project() - Crypto:project() + Cryptography:project() Parser:project() Utils:project() ZoneCode:project() diff --git a/src/Crypto/Crypto.cpp b/src/Crypto/Crypto.cpp deleted file mode 100644 index 3dc07fd7..00000000 --- a/src/Crypto/Crypto.cpp +++ /dev/null @@ -1,32 +0,0 @@ -#include "Crypto.h" - -#include "Impl/AlgorithmMD5.h" -#include "Impl/AlgorithmRSA.h" -#include "Impl/AlgorithmSHA1.h" -#include "Impl/AlgorithmSHA256.h" -#include "Impl/AlgorithmSalsa20.h" - -std::unique_ptr Crypto::CreateMD5() -{ - return std::make_unique(); -} - -std::unique_ptr Crypto::CreateSHA1() -{ - return std::make_unique(); -} - -std::unique_ptr Crypto::CreateSHA256() -{ - return std::make_unique(); -} - -std::unique_ptr Crypto::CreateSalsa20(const uint8_t* keyBytes, const size_t keySize) -{ - return std::make_unique(keyBytes, keySize); -} - -std::unique_ptr Crypto::CreateRSA(const IPublicKeyAlgorithm::HashingAlgorithm hashingAlgorithm, const RSAPaddingMode paddingMode) -{ - return std::make_unique(hashingAlgorithm, paddingMode); -} diff --git a/src/Crypto/Crypto.h b/src/Crypto/Crypto.h deleted file mode 100644 index e7e5edb8..00000000 --- a/src/Crypto/Crypto.h +++ /dev/null @@ -1,27 +0,0 @@ -#pragma once - -#include "IHashFunction.h" -#include "IPublicKeyAlgorithm.h" -#include "IStreamCipher.h" - -#include -#include - -class Crypto -{ -public: - enum class RSAPaddingMode - { - RSA_PADDING_PKS1, - RSA_PADDING_PSS, - }; - - static std::unique_ptr CreateMD5(); - - static std::unique_ptr CreateSHA1(); - static std::unique_ptr CreateSHA256(); - - static std::unique_ptr CreateSalsa20(const uint8_t* keyBytes, size_t keySize); - - static std::unique_ptr CreateRSA(IPublicKeyAlgorithm::HashingAlgorithm hashingAlgorithm, RSAPaddingMode paddingMode); -}; diff --git a/src/Crypto/IHashFunction.h b/src/Crypto/IHashFunction.h deleted file mode 100644 index 81dbec51..00000000 --- a/src/Crypto/IHashFunction.h +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once - -#include - -class IHashFunction -{ -public: - virtual ~IHashFunction() = default; - - virtual size_t GetHashSize() = 0; - virtual void Init() = 0; - virtual void Process(const void* input, size_t inputSize) = 0; - virtual void Finish(void* hashBuffer) = 0; -}; diff --git a/src/Crypto/IPublicKeyAlgorithm.h b/src/Crypto/IPublicKeyAlgorithm.h deleted file mode 100644 index 9764e0a0..00000000 --- a/src/Crypto/IPublicKeyAlgorithm.h +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once - -#include -#include - -class IPublicKeyAlgorithm -{ -public: - enum class HashingAlgorithm - { - RSA_HASH_SHA256, - RSA_HASH_SHA512 - }; - - virtual ~IPublicKeyAlgorithm() = default; - - virtual bool SetKey(const uint8_t* keyData, size_t keySize) = 0; - - // If needed add a signing method - - virtual bool Verify(const uint8_t* signedData, size_t signedDataSize, const uint8_t* signature, size_t signatureSize) = 0; -}; diff --git a/src/Crypto/IStreamCipher.h b/src/Crypto/IStreamCipher.h deleted file mode 100644 index 076f549c..00000000 --- a/src/Crypto/IStreamCipher.h +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once - -#include -#include - -class IStreamCipher -{ -public: - virtual ~IStreamCipher() = default; - - virtual void SetIV(const uint8_t* iv, size_t ivSize) = 0; - - virtual void Process(const void* plainText, void* cipherText, size_t amount) = 0; -}; diff --git a/src/Crypto/Impl/AlgorithmMD5.cpp b/src/Crypto/Impl/AlgorithmMD5.cpp deleted file mode 100644 index 3e14e74b..00000000 --- a/src/Crypto/Impl/AlgorithmMD5.cpp +++ /dev/null @@ -1,64 +0,0 @@ -#include "AlgorithmMD5.h" - -#include "CryptoLibrary.h" - -#include - -class AlgorithmMD5::AlgorithmMD5Impl -{ - hash_state m_state{}; - -public: - AlgorithmMD5Impl() - { - CryptoLibrary::Init(); - - Init(); - } - - void Init() - { - md5_init(&m_state); - } - - void Process(const void* input, const size_t inputSize) - { - md5_process(&m_state, static_cast(input), inputSize); - } - - void Finish(void* hashBuffer) - { - md5_done(&m_state, static_cast(hashBuffer)); - } -}; - -AlgorithmMD5::AlgorithmMD5() -{ - m_impl = new AlgorithmMD5Impl(); -} - -AlgorithmMD5::~AlgorithmMD5() -{ - delete m_impl; - m_impl = nullptr; -} - -size_t AlgorithmMD5::GetHashSize() -{ - return HASH_SIZE; -} - -void AlgorithmMD5::Init() -{ - m_impl->Init(); -} - -void AlgorithmMD5::Process(const void* input, const size_t inputSize) -{ - m_impl->Process(input, inputSize); -} - -void AlgorithmMD5::Finish(void* hashBuffer) -{ - m_impl->Finish(hashBuffer); -} diff --git a/src/Crypto/Impl/AlgorithmMD5.h b/src/Crypto/Impl/AlgorithmMD5.h deleted file mode 100644 index 4687572a..00000000 --- a/src/Crypto/Impl/AlgorithmMD5.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once -#include "IHashFunction.h" - -class AlgorithmMD5 : public IHashFunction -{ - class AlgorithmMD5Impl; - AlgorithmMD5Impl* m_impl; - -public: - static const int HASH_SIZE = 16; - - AlgorithmMD5(); - ~AlgorithmMD5() override; - - size_t GetHashSize() override; - - void Init() override; - void Process(const void* input, size_t inputSize) override; - void Finish(void* hashBuffer) override; -}; diff --git a/src/Crypto/Impl/AlgorithmRSA.cpp b/src/Crypto/Impl/AlgorithmRSA.cpp deleted file mode 100644 index ac65adb9..00000000 --- a/src/Crypto/Impl/AlgorithmRSA.cpp +++ /dev/null @@ -1,119 +0,0 @@ -#include "AlgorithmRSA.h" - -#include "CryptoLibrary.h" - -#include - -class AlgorithmRSA::AlgorithmRSAImpl -{ - rsa_key m_key{}; - HashingAlgorithm m_hash; - Crypto::RSAPaddingMode m_padding; - - const ltc_hash_descriptor* GetHashDescriptor() const - { - switch (m_hash) - { - case HashingAlgorithm::RSA_HASH_SHA256: - return &sha256_desc; - - default: - case HashingAlgorithm::RSA_HASH_SHA512: - return &sha512_desc; - } - } - - int GetPaddingMode() const - { - switch (m_padding) - { - case Crypto::RSAPaddingMode::RSA_PADDING_PKS1: - return LTC_PKCS_1_V1_5; - - default: - case Crypto::RSAPaddingMode::RSA_PADDING_PSS: - return LTC_PKCS_1_PSS; - } - } - -public: - AlgorithmRSAImpl(const HashingAlgorithm hash, const Crypto::RSAPaddingMode padding) - { - m_hash = hash; - m_padding = padding; - - CryptoLibrary::Init(); - } - - ~AlgorithmRSAImpl() = default; - - AlgorithmRSAImpl(AlgorithmRSAImpl& other) = default; - AlgorithmRSAImpl(AlgorithmRSAImpl&& other) = delete; - - AlgorithmRSAImpl& operator=(AlgorithmRSAImpl const& other) = default; - AlgorithmRSAImpl& operator=(AlgorithmRSAImpl&& other) = delete; - - bool SetKey(const uint8_t* keyData, const size_t keySize) - { - return rsa_import(keyData, keySize, &m_key) == CRYPT_OK; - } - - bool Verify(const uint8_t* signedData, const size_t signedDataSize, const uint8_t* signature, const size_t signatureSize) - { - const ltc_hash_descriptor* hashDesc = GetHashDescriptor(); - const int hashId = register_hash(hashDesc); - const int padding = GetPaddingMode(); - - int result; - rsa_verify_hash_ex(signature, signatureSize, signedData, signedDataSize, padding, hashId, 8, &result, &m_key); - - return result == 1; - } -}; - -AlgorithmRSA::AlgorithmRSA(const HashingAlgorithm hash, const Crypto::RSAPaddingMode padding) -{ - m_impl = new AlgorithmRSAImpl(hash, padding); -} - -AlgorithmRSA::~AlgorithmRSA() -{ - delete m_impl; - m_impl = nullptr; -} - -AlgorithmRSA::AlgorithmRSA(AlgorithmRSA& other) -{ - m_impl = new AlgorithmRSAImpl(*other.m_impl); -} - -AlgorithmRSA::AlgorithmRSA(AlgorithmRSA&& other) noexcept -{ - m_impl = other.m_impl; - other.m_impl = nullptr; -} - -AlgorithmRSA& AlgorithmRSA::operator=(AlgorithmRSA const& other) -{ - m_impl = new AlgorithmRSAImpl(*other.m_impl); - - return *this; -} - -AlgorithmRSA& AlgorithmRSA::operator=(AlgorithmRSA&& other) noexcept -{ - m_impl = other.m_impl; - other.m_impl = nullptr; - - return *this; -} - -bool AlgorithmRSA::SetKey(const uint8_t* keyData, size_t keySize) -{ - return m_impl->SetKey(keyData, keySize); -} - -bool AlgorithmRSA::Verify(const uint8_t* signedData, const size_t signedDataSize, const uint8_t* signature, const size_t signatureSize) -{ - return m_impl->Verify(signedData, signedDataSize, signature, signatureSize); -} diff --git a/src/Crypto/Impl/AlgorithmRSA.h b/src/Crypto/Impl/AlgorithmRSA.h deleted file mode 100644 index f84cee0e..00000000 --- a/src/Crypto/Impl/AlgorithmRSA.h +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once -#include "Crypto.h" -#include "IPublicKeyAlgorithm.h" - -#include - -class AlgorithmRSA final : public IPublicKeyAlgorithm -{ - class AlgorithmRSAImpl; - AlgorithmRSAImpl* m_impl; - -public: - AlgorithmRSA(HashingAlgorithm hash, Crypto::RSAPaddingMode padding); - ~AlgorithmRSA() override; - - AlgorithmRSA(AlgorithmRSA& other); - AlgorithmRSA(AlgorithmRSA&& other) noexcept; - - AlgorithmRSA& operator=(AlgorithmRSA const& other); - AlgorithmRSA& operator=(AlgorithmRSA&& other) noexcept; - - bool SetKey(const uint8_t* keyData, size_t keySize) override; - - bool Verify(const uint8_t* signedData, size_t signedDataSize, const uint8_t* signature, size_t signatureSize) override; -}; diff --git a/src/Crypto/Impl/AlgorithmSHA1.cpp b/src/Crypto/Impl/AlgorithmSHA1.cpp deleted file mode 100644 index 21558383..00000000 --- a/src/Crypto/Impl/AlgorithmSHA1.cpp +++ /dev/null @@ -1,64 +0,0 @@ -#include "AlgorithmSHA1.h" - -#include "CryptoLibrary.h" - -#include - -class AlgorithmSHA1::AlgorithmSHA1Impl -{ - hash_state m_state{}; - -public: - AlgorithmSHA1Impl() - { - CryptoLibrary::Init(); - - Init(); - } - - void Init() - { - sha1_init(&m_state); - } - - void Process(const void* input, const size_t inputSize) - { - sha1_process(&m_state, static_cast(input), inputSize); - } - - void Finish(void* hashBuffer) - { - sha1_done(&m_state, static_cast(hashBuffer)); - } -}; - -AlgorithmSHA1::AlgorithmSHA1() -{ - m_impl = new AlgorithmSHA1Impl(); -} - -AlgorithmSHA1::~AlgorithmSHA1() -{ - delete m_impl; - m_impl = nullptr; -} - -size_t AlgorithmSHA1::GetHashSize() -{ - return HASH_SIZE; -} - -void AlgorithmSHA1::Init() -{ - m_impl->Init(); -} - -void AlgorithmSHA1::Process(const void* input, const size_t inputSize) -{ - m_impl->Process(input, inputSize); -} - -void AlgorithmSHA1::Finish(void* hashBuffer) -{ - m_impl->Finish(hashBuffer); -} diff --git a/src/Crypto/Impl/AlgorithmSHA1.h b/src/Crypto/Impl/AlgorithmSHA1.h deleted file mode 100644 index f9ae0514..00000000 --- a/src/Crypto/Impl/AlgorithmSHA1.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once -#include "IHashFunction.h" - -class AlgorithmSHA1 : public IHashFunction -{ - class AlgorithmSHA1Impl; - AlgorithmSHA1Impl* m_impl; - -public: - static const int HASH_SIZE = 20; - - AlgorithmSHA1(); - ~AlgorithmSHA1() override; - - size_t GetHashSize() override; - - void Init() override; - void Process(const void* input, size_t inputSize) override; - void Finish(void* hashBuffer) override; -}; diff --git a/src/Crypto/Impl/AlgorithmSHA256.cpp b/src/Crypto/Impl/AlgorithmSHA256.cpp deleted file mode 100644 index a1bf1964..00000000 --- a/src/Crypto/Impl/AlgorithmSHA256.cpp +++ /dev/null @@ -1,64 +0,0 @@ -#include "AlgorithmSHA256.h" - -#include "CryptoLibrary.h" - -#include - -class AlgorithmSHA256::Impl -{ - hash_state m_state{}; - -public: - Impl() - { - CryptoLibrary::Init(); - - Init(); - } - - void Init() - { - sha256_init(&m_state); - } - - void Process(const void* input, const size_t inputSize) - { - sha256_process(&m_state, static_cast(input), inputSize); - } - - void Finish(void* hashBuffer) - { - sha256_done(&m_state, static_cast(hashBuffer)); - } -}; - -AlgorithmSHA256::AlgorithmSHA256() -{ - m_impl = new Impl(); -} - -AlgorithmSHA256::~AlgorithmSHA256() -{ - delete m_impl; - m_impl = nullptr; -} - -size_t AlgorithmSHA256::GetHashSize() -{ - return HASH_SIZE; -} - -void AlgorithmSHA256::Init() -{ - m_impl->Init(); -} - -void AlgorithmSHA256::Process(const void* input, const size_t inputSize) -{ - m_impl->Process(input, inputSize); -} - -void AlgorithmSHA256::Finish(void* hashBuffer) -{ - m_impl->Finish(hashBuffer); -} diff --git a/src/Crypto/Impl/AlgorithmSHA256.h b/src/Crypto/Impl/AlgorithmSHA256.h deleted file mode 100644 index 54875a2a..00000000 --- a/src/Crypto/Impl/AlgorithmSHA256.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once -#include "IHashFunction.h" - -class AlgorithmSHA256 : public IHashFunction -{ - class Impl; - Impl* m_impl; - -public: - static const int HASH_SIZE = 32; - - AlgorithmSHA256(); - ~AlgorithmSHA256() override; - - size_t GetHashSize() override; - - void Init() override; - void Process(const void* input, size_t inputSize) override; - void Finish(void* hashBuffer) override; -}; diff --git a/src/Crypto/Impl/AlgorithmSalsa20.cpp b/src/Crypto/Impl/AlgorithmSalsa20.cpp deleted file mode 100644 index 2c5459d6..00000000 --- a/src/Crypto/Impl/AlgorithmSalsa20.cpp +++ /dev/null @@ -1,89 +0,0 @@ -#include "AlgorithmSalsa20.h" - -#include "salsa20.h" - -#include -#include - -class AlgorithmSalsa20::AlgorithmSalsa20Impl -{ - salsa20_ctx m_context{}; - -public: - AlgorithmSalsa20Impl(const uint8_t* keyBytes, const size_t keySize) - { - Salsa20_KeySetup(&m_context, keyBytes, keySize * 8); - } - - ~AlgorithmSalsa20Impl() = default; - - AlgorithmSalsa20Impl(AlgorithmSalsa20Impl& other) = default; - AlgorithmSalsa20Impl(AlgorithmSalsa20Impl&& other) = delete; - - AlgorithmSalsa20Impl& operator=(AlgorithmSalsa20Impl const& other) = default; - AlgorithmSalsa20Impl& operator=(AlgorithmSalsa20Impl&& other) = delete; - - void SetIV(const uint8_t* iv, const size_t ivSize) - { - assert(ivSize == 8); - - if (ivSize != 8) - { - throw std::invalid_argument("Salsa20 IV size must be 8"); - } - - Salsa20_IVSetup(&m_context, iv); - } - - void Process(const void* plainText, void* cipherText, const size_t amount) - { - Salsa20_Encrypt_Bytes(&m_context, static_cast(plainText), static_cast(cipherText), amount); - } -}; - -AlgorithmSalsa20::AlgorithmSalsa20(const uint8_t* keyBytes, const size_t keySize) -{ - m_impl = new AlgorithmSalsa20Impl(keyBytes, keySize); -} - -AlgorithmSalsa20::~AlgorithmSalsa20() -{ - delete m_impl; - m_impl = nullptr; -} - -AlgorithmSalsa20::AlgorithmSalsa20(AlgorithmSalsa20& other) -{ - m_impl = new AlgorithmSalsa20Impl(*other.m_impl); -} - -AlgorithmSalsa20::AlgorithmSalsa20(AlgorithmSalsa20&& other) noexcept -{ - m_impl = other.m_impl; - other.m_impl = nullptr; -} - -AlgorithmSalsa20& AlgorithmSalsa20::operator=(AlgorithmSalsa20 const& other) -{ - m_impl = new AlgorithmSalsa20Impl(*other.m_impl); - - return *this; -} - -AlgorithmSalsa20& AlgorithmSalsa20::operator=(AlgorithmSalsa20&& other) noexcept -{ - m_impl = other.m_impl; - other.m_impl = nullptr; - - return *this; -} - -void AlgorithmSalsa20::SetIV(const uint8_t* iv, const size_t ivSize) -{ - m_impl->SetIV(iv, ivSize); -} - -void AlgorithmSalsa20::Process(const void* plainText, void* cipherText, const size_t amount) -{ - m_impl->Process(plainText, cipherText, amount); -} diff --git a/src/Crypto/Impl/AlgorithmSalsa20.h b/src/Crypto/Impl/AlgorithmSalsa20.h deleted file mode 100644 index 88c0d6ca..00000000 --- a/src/Crypto/Impl/AlgorithmSalsa20.h +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once -#include "IStreamCipher.h" - -class AlgorithmSalsa20 final : public IStreamCipher -{ - class AlgorithmSalsa20Impl; - AlgorithmSalsa20Impl* m_impl; - -public: - AlgorithmSalsa20(const uint8_t* keyBytes, size_t keySize); - ~AlgorithmSalsa20() override; - - AlgorithmSalsa20(AlgorithmSalsa20& other); - AlgorithmSalsa20(AlgorithmSalsa20&& other) noexcept; - - AlgorithmSalsa20& operator=(AlgorithmSalsa20 const& other); - AlgorithmSalsa20& operator=(AlgorithmSalsa20&& other) noexcept; - - void SetIV(const uint8_t* iv, size_t ivSize) override; - void Process(const void* plainText, void* cipherText, size_t amount) override; -}; diff --git a/src/Crypto/Impl/CryptoLibrary.cpp b/src/Crypto/Impl/CryptoLibrary.cpp deleted file mode 100644 index 3db79799..00000000 --- a/src/Crypto/Impl/CryptoLibrary.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#include "CryptoLibrary.h" - -#include "tommath.h" - -void CryptoLibrary::Init() -{ - static bool initialized = false; - - if (!initialized) - { - initialized = true; - - ltc_mp = ltm_desc; - } -} diff --git a/src/Crypto/Impl/CryptoLibrary.h b/src/Crypto/Impl/CryptoLibrary.h deleted file mode 100644 index 68829c41..00000000 --- a/src/Crypto/Impl/CryptoLibrary.h +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once - -#define LTC_NO_PROTOTYPES -#include "tomcrypt.h" - -class CryptoLibrary -{ -public: - static void Init(); -}; diff --git a/src/Crypto.lua b/src/Cryptography.lua similarity index 61% rename from src/Crypto.lua rename to src/Cryptography.lua index 96cd1670..a3740ddc 100644 --- a/src/Crypto.lua +++ b/src/Cryptography.lua @@ -1,29 +1,29 @@ -Crypto = {} +Cryptography = {} -function Crypto:include(includes) +function Cryptography:include(includes) if includes:handle(self:name()) then includedirs { - path.join(ProjectFolder(), "Crypto") + path.join(ProjectFolder(), "Cryptography") } end end -function Crypto:link(links) +function Cryptography:link(links) links:add(self:name()) links:linkto(libtomcrypt) links:linkto(libtommath) links:linkto(salsa20) end -function Crypto:use() +function Cryptography:use() end -function Crypto:name() - return "Crypto" +function Cryptography:name() + return "Cryptography" end -function Crypto:project() +function Cryptography:project() local folder = ProjectFolder() local includes = Includes:create() @@ -34,8 +34,8 @@ function Crypto:project() language "C++" files { - path.join(folder, "Crypto/**.h"), - path.join(folder, "Crypto/**.cpp") + path.join(folder, "Cryptography/**.h"), + path.join(folder, "Cryptography/**.cpp") } self:include(includes) diff --git a/src/Cryptography/Algorithms/AlgorithmMd5.cpp b/src/Cryptography/Algorithms/AlgorithmMd5.cpp new file mode 100644 index 00000000..501bc20e --- /dev/null +++ b/src/Cryptography/Algorithms/AlgorithmMd5.cpp @@ -0,0 +1,54 @@ +#include "AlgorithmMd5.h" + +#include "Internal/CryptoLibrary.h" + +#include + +using namespace cryptography; + +namespace +{ + constexpr int HASH_SIZE = 16; + + class AlgorithmMd5 final : public IHashFunction + { + public: + AlgorithmMd5() + { + internal::CryptoLibrary::Init(); + + Init(); + } + + size_t GetHashSize() override + { + return HASH_SIZE; + } + + void Init() override + { + md5_init(&m_state); + } + + void Process(const void* input, const size_t inputSize) override + { + md5_process(&m_state, static_cast(input), static_cast(inputSize)); + } + + void Finish(void* hashBuffer) override + { + md5_done(&m_state, static_cast(hashBuffer)); + } + + private: + hash_state m_state{}; + }; +} // namespace + +namespace cryptography +{ + std::unique_ptr CreateMd5() + { + return std::make_unique(); + } +} // namespace cryptography diff --git a/src/Cryptography/Algorithms/AlgorithmMd5.h b/src/Cryptography/Algorithms/AlgorithmMd5.h new file mode 100644 index 00000000..d321408e --- /dev/null +++ b/src/Cryptography/Algorithms/AlgorithmMd5.h @@ -0,0 +1,10 @@ +#pragma once + +#include "IHashFunction.h" + +#include + +namespace cryptography +{ + std::unique_ptr CreateMd5(); +} diff --git a/src/Cryptography/Algorithms/AlgorithmRsa.cpp b/src/Cryptography/Algorithms/AlgorithmRsa.cpp new file mode 100644 index 00000000..450fdf71 --- /dev/null +++ b/src/Cryptography/Algorithms/AlgorithmRsa.cpp @@ -0,0 +1,84 @@ +#include "AlgorithmRsa.h" + +#include "Internal/CryptoLibrary.h" + +using namespace cryptography; + +namespace +{ + class AlgorithmRsa final : public IPublicKeyAlgorithm + { + public: + AlgorithmRsa(const HashingAlgorithm hash, const RsaPaddingMode padding) + { + m_hash = hash; + m_padding = padding; + + internal::CryptoLibrary::Init(); + } + + bool SetKey(const uint8_t* keyData, const size_t keySize) override + { + return rsa_import(keyData, static_cast(keySize), &m_key) == CRYPT_OK; + } + + bool Verify(const uint8_t* signedData, const size_t signedDataSize, const uint8_t* signature, const size_t signatureSize) override + { + const ltc_hash_descriptor* hashDesc = GetHashDescriptor(); + const int hashId = register_hash(hashDesc); + const int padding = GetPaddingMode(); + + int result; + rsa_verify_hash_ex(signature, + static_cast(signatureSize), + signedData, + static_cast(signedDataSize), + padding, + hashId, + 8, + &result, + &m_key); + + return result == 1; + } + + private: + [[nodiscard]] const ltc_hash_descriptor* GetHashDescriptor() const + { + switch (m_hash) + { + case HashingAlgorithm::RSA_HASH_SHA256: + return &sha256_desc; + + default: + case HashingAlgorithm::RSA_HASH_SHA512: + return &sha512_desc; + } + } + + [[nodiscard]] int GetPaddingMode() const + { + switch (m_padding) + { + case RsaPaddingMode::RSA_PADDING_PKS1: + return LTC_PKCS_1_V1_5; + + default: + case RsaPaddingMode::RSA_PADDING_PSS: + return LTC_PKCS_1_PSS; + } + } + + rsa_key m_key{}; + HashingAlgorithm m_hash; + RsaPaddingMode m_padding; + }; +} // namespace + +namespace cryptography +{ + std::unique_ptr CreateRsa(const HashingAlgorithm hashingAlgorithm, const RsaPaddingMode paddingMode) + { + return std::make_unique(hashingAlgorithm, paddingMode); + } +} // namespace cryptography diff --git a/src/Cryptography/Algorithms/AlgorithmRsa.h b/src/Cryptography/Algorithms/AlgorithmRsa.h new file mode 100644 index 00000000..5a682bc3 --- /dev/null +++ b/src/Cryptography/Algorithms/AlgorithmRsa.h @@ -0,0 +1,17 @@ +#pragma once + +#include "IPublicKeyAlgorithm.h" + +#include +#include + +namespace cryptography +{ + enum class RsaPaddingMode : std::uint8_t + { + RSA_PADDING_PKS1, + RSA_PADDING_PSS, + }; + + std::unique_ptr CreateRsa(HashingAlgorithm hashingAlgorithm, RsaPaddingMode paddingMode); +} // namespace cryptography diff --git a/src/Cryptography/Algorithms/AlgorithmSalsa20.cpp b/src/Cryptography/Algorithms/AlgorithmSalsa20.cpp new file mode 100644 index 00000000..9a58e8d6 --- /dev/null +++ b/src/Cryptography/Algorithms/AlgorithmSalsa20.cpp @@ -0,0 +1,46 @@ +#include "AlgorithmSalsa20.h" + +#include "salsa20.h" + +#include +#include + +using namespace cryptography; + +namespace +{ + class AlgorithmSalsa20 final : public IStreamCipher + { + public: + AlgorithmSalsa20(const uint8_t* keyBytes, const size_t keySize) + { + Salsa20_KeySetup(&m_context, keyBytes, static_cast(keySize * 8uz)); + } + + void SetIv(const uint8_t* iv, const size_t ivSize) override + { + assert(ivSize == 8); + + if (ivSize != 8) + throw std::invalid_argument("Salsa20 IV size must be 8"); + + Salsa20_IVSetup(&m_context, iv); + } + + void Process(const void* plainText, void* cipherText, const size_t amount) override + { + Salsa20_Encrypt_Bytes(&m_context, static_cast(plainText), static_cast(cipherText), static_cast(amount)); + } + + private: + salsa20_ctx m_context{}; + }; +} // namespace + +namespace cryptography +{ + std::unique_ptr CreateSalsa20(const uint8_t* keyBytes, const size_t keySize) + { + return std::make_unique(keyBytes, keySize); + } +} // namespace cryptography diff --git a/src/Cryptography/Algorithms/AlgorithmSalsa20.h b/src/Cryptography/Algorithms/AlgorithmSalsa20.h new file mode 100644 index 00000000..1e5c95be --- /dev/null +++ b/src/Cryptography/Algorithms/AlgorithmSalsa20.h @@ -0,0 +1,10 @@ +#pragma once + +#include "IStreamCipher.h" + +#include + +namespace cryptography +{ + std::unique_ptr CreateSalsa20(const uint8_t* keyBytes, size_t keySize); +} diff --git a/src/Cryptography/Algorithms/AlgorithmSha1.cpp b/src/Cryptography/Algorithms/AlgorithmSha1.cpp new file mode 100644 index 00000000..4e394a50 --- /dev/null +++ b/src/Cryptography/Algorithms/AlgorithmSha1.cpp @@ -0,0 +1,54 @@ +#include "AlgorithmSha1.h" + +#include "Internal/CryptoLibrary.h" + +#include + +using namespace cryptography; + +namespace +{ + constexpr int HASH_SIZE = 20; + + class AlgorithmSha1 final : public IHashFunction + { + public: + AlgorithmSha1() + { + internal::CryptoLibrary::Init(); + + Init(); + } + + size_t GetHashSize() override + { + return HASH_SIZE; + } + + void Init() override + { + sha1_init(&m_state); + } + + void Process(const void* input, const size_t inputSize) override + { + sha1_process(&m_state, static_cast(input), static_cast(inputSize)); + } + + void Finish(void* hashBuffer) override + { + sha1_done(&m_state, static_cast(hashBuffer)); + } + + private: + hash_state m_state{}; + }; +} // namespace + +namespace cryptography +{ + std::unique_ptr CreateSha1() + { + return std::make_unique(); + } +} // namespace cryptography diff --git a/src/Cryptography/Algorithms/AlgorithmSha1.h b/src/Cryptography/Algorithms/AlgorithmSha1.h new file mode 100644 index 00000000..41e11ac8 --- /dev/null +++ b/src/Cryptography/Algorithms/AlgorithmSha1.h @@ -0,0 +1,10 @@ +#pragma once + +#include "IHashFunction.h" + +#include + +namespace cryptography +{ + std::unique_ptr CreateSha1(); +} diff --git a/src/Cryptography/Algorithms/AlgorithmSha256.cpp b/src/Cryptography/Algorithms/AlgorithmSha256.cpp new file mode 100644 index 00000000..662c8e4a --- /dev/null +++ b/src/Cryptography/Algorithms/AlgorithmSha256.cpp @@ -0,0 +1,54 @@ +#include "AlgorithmSha256.h" + +#include "Internal/CryptoLibrary.h" + +#include + +using namespace cryptography; + +namespace +{ + constexpr int HASH_SIZE = 32; + + class AlgorithmSha256 final : public IHashFunction + { + public: + AlgorithmSha256() + { + internal::CryptoLibrary::Init(); + + Init(); + } + + size_t GetHashSize() override + { + return HASH_SIZE; + } + + void Init() override + { + sha256_init(&m_state); + } + + void Process(const void* input, const size_t inputSize) override + { + sha256_process(&m_state, static_cast(input), static_cast(inputSize)); + } + + void Finish(void* hashBuffer) override + { + sha256_done(&m_state, static_cast(hashBuffer)); + } + + private: + hash_state m_state{}; + }; +} // namespace + +namespace cryptography +{ + std::unique_ptr CreateSha256() + { + return std::make_unique(); + } +} // namespace cryptography diff --git a/src/Cryptography/Algorithms/AlgorithmSha256.h b/src/Cryptography/Algorithms/AlgorithmSha256.h new file mode 100644 index 00000000..22bb1372 --- /dev/null +++ b/src/Cryptography/Algorithms/AlgorithmSha256.h @@ -0,0 +1,10 @@ +#pragma once + +#include "IHashFunction.h" + +#include + +namespace cryptography +{ + std::unique_ptr CreateSha256(); +} diff --git a/src/Crypto/Impl/Base64.cpp b/src/Cryptography/Base64.cpp similarity index 76% rename from src/Crypto/Impl/Base64.cpp rename to src/Cryptography/Base64.cpp index 77d1717f..135faa70 100644 --- a/src/Crypto/Impl/Base64.cpp +++ b/src/Cryptography/Base64.cpp @@ -20,8 +20,9 @@ namespace base64 bool EncodeBase64(const void* inputData, const size_t inputLength, void* outputBuffer, const size_t outputBufferSize) { - unsigned long outLength = outputBufferSize; - const auto result = base64_encode(static_cast(inputData), inputLength, static_cast(outputBuffer), &outLength); + unsigned long outLength = static_cast(outputBufferSize); + const auto result = + base64_encode(static_cast(inputData), static_cast(inputLength), static_cast(outputBuffer), &outLength); return result == CRYPT_OK; } @@ -32,12 +33,13 @@ namespace base64 size_t DecodeBase64(const void* base64Data, const size_t inputLength, void* outputBuffer, const size_t outputBufferSize) { - unsigned long outLength = GetBase64DecodeOutputLength(base64Data, inputLength); + auto outLength = static_cast(GetBase64DecodeOutputLength(base64Data, inputLength)); assert(outLength <= outputBufferSize); if (outLength > outputBufferSize) return 0u; - const auto result = base64_decode(static_cast(base64Data), inputLength, static_cast(outputBuffer), &outLength); + const auto result = + base64_decode(static_cast(base64Data), static_cast(inputLength), static_cast(outputBuffer), &outLength); assert(result == CRYPT_OK); return static_cast(outLength); diff --git a/src/Crypto/Impl/Base64.h b/src/Cryptography/Base64.h similarity index 84% rename from src/Crypto/Impl/Base64.h rename to src/Cryptography/Base64.h index 8264a8c3..5c28685b 100644 --- a/src/Crypto/Impl/Base64.h +++ b/src/Cryptography/Base64.h @@ -8,6 +8,6 @@ namespace base64 size_t GetBase64EncodeOutputLength(size_t inputLength); size_t DecodeBase64(const void* base64Data, size_t inputLength, void* outputBuffer, size_t outputBufferSize); - size_t GetBase64DecodeOutputLength(const void* base64Data, const size_t inputLength); + size_t GetBase64DecodeOutputLength(const void* base64Data, size_t inputLength); size_t GetBase64DecodeOutputLength(size_t inputLength); } // namespace base64 diff --git a/src/Cryptography/Cryptography.h b/src/Cryptography/Cryptography.h new file mode 100644 index 00000000..8aff466f --- /dev/null +++ b/src/Cryptography/Cryptography.h @@ -0,0 +1,7 @@ +#pragma once + +#include "Algorithms/AlgorithmMd5.h" +#include "Algorithms/AlgorithmRsa.h" +#include "Algorithms/AlgorithmSalsa20.h" +#include "Algorithms/AlgorithmSha1.h" +#include "Algorithms/AlgorithmSha256.h" diff --git a/src/Cryptography/IHashFunction.h b/src/Cryptography/IHashFunction.h new file mode 100644 index 00000000..a7519b3c --- /dev/null +++ b/src/Cryptography/IHashFunction.h @@ -0,0 +1,22 @@ +#pragma once + +#include + +namespace cryptography +{ + class IHashFunction + { + public: + IHashFunction() = default; + virtual ~IHashFunction() = default; + IHashFunction(const IHashFunction& other) = default; + IHashFunction(IHashFunction&& other) noexcept = default; + IHashFunction& operator=(const IHashFunction& other) = default; + IHashFunction& operator=(IHashFunction&& other) noexcept = default; + + virtual size_t GetHashSize() = 0; + virtual void Init() = 0; + virtual void Process(const void* input, size_t inputSize) = 0; + virtual void Finish(void* hashBuffer) = 0; + }; +} // namespace cryptography diff --git a/src/Cryptography/IPublicKeyAlgorithm.h b/src/Cryptography/IPublicKeyAlgorithm.h new file mode 100644 index 00000000..475409df --- /dev/null +++ b/src/Cryptography/IPublicKeyAlgorithm.h @@ -0,0 +1,30 @@ +#pragma once + +#include +#include + +namespace cryptography +{ + enum class HashingAlgorithm : std::uint8_t + { + RSA_HASH_SHA256, + RSA_HASH_SHA512 + }; + + class IPublicKeyAlgorithm + { + public: + IPublicKeyAlgorithm() = default; + virtual ~IPublicKeyAlgorithm() = default; + IPublicKeyAlgorithm(const IPublicKeyAlgorithm& other) = default; + IPublicKeyAlgorithm(IPublicKeyAlgorithm&& other) noexcept = default; + IPublicKeyAlgorithm& operator=(const IPublicKeyAlgorithm& other) = default; + IPublicKeyAlgorithm& operator=(IPublicKeyAlgorithm&& other) noexcept = default; + + virtual bool SetKey(const uint8_t* keyData, size_t keySize) = 0; + + // If needed, add a signing method + + virtual bool Verify(const uint8_t* signedData, size_t signedDataSize, const uint8_t* signature, size_t signatureSize) = 0; + }; +} // namespace cryptography diff --git a/src/Cryptography/IStreamCipher.h b/src/Cryptography/IStreamCipher.h new file mode 100644 index 00000000..7c2da861 --- /dev/null +++ b/src/Cryptography/IStreamCipher.h @@ -0,0 +1,21 @@ +#pragma once + +#include +#include + +namespace cryptography +{ + class IStreamCipher + { + public: + IStreamCipher() = default; + virtual ~IStreamCipher() = default; + IStreamCipher(const IStreamCipher& other) = default; + IStreamCipher(IStreamCipher&& other) noexcept = default; + IStreamCipher& operator=(const IStreamCipher& other) = default; + IStreamCipher& operator=(IStreamCipher&& other) noexcept = default; + + virtual void SetIv(const uint8_t* iv, size_t ivSize) = 0; + virtual void Process(const void* plainText, void* cipherText, size_t amount) = 0; + }; +} // namespace cryptography diff --git a/src/Cryptography/Internal/CryptoLibrary.cpp b/src/Cryptography/Internal/CryptoLibrary.cpp new file mode 100644 index 00000000..150ecd5e --- /dev/null +++ b/src/Cryptography/Internal/CryptoLibrary.cpp @@ -0,0 +1,18 @@ +#include "CryptoLibrary.h" + +#include "tommath.h" + +namespace cryptography::internal +{ + void CryptoLibrary::Init() + { + static bool initialized = false; + + if (!initialized) + { + initialized = true; + + ltc_mp = ltm_desc; + } + } +} // namespace cryptography::internal diff --git a/src/Cryptography/Internal/CryptoLibrary.h b/src/Cryptography/Internal/CryptoLibrary.h new file mode 100644 index 00000000..566a1917 --- /dev/null +++ b/src/Cryptography/Internal/CryptoLibrary.h @@ -0,0 +1,13 @@ +#pragma once + +#define LTC_NO_PROTOTYPES +#include "tomcrypt.h" + +namespace cryptography::internal +{ + class CryptoLibrary + { + public: + static void Init(); + }; +} // namespace cryptography::internal diff --git a/src/ObjLoading.lua b/src/ObjLoading.lua index 712ad146..70e945df 100644 --- a/src/ObjLoading.lua +++ b/src/ObjLoading.lua @@ -55,7 +55,7 @@ function ObjLoading:project() useSourceTemplating("ObjLoading") self:include(includes) - Crypto:include(includes) + Cryptography:include(includes) Utils:include(includes) minilzo:include(includes) minizip:include(includes) diff --git a/src/ObjLoading/Game/IW5/Material/JsonMaterialLoader.cpp b/src/ObjLoading/Game/IW5/Material/JsonMaterialLoader.cpp index 3742f848..55380c30 100644 --- a/src/ObjLoading/Game/IW5/Material/JsonMaterialLoader.cpp +++ b/src/ObjLoading/Game/IW5/Material/JsonMaterialLoader.cpp @@ -1,8 +1,8 @@ #include "JsonMaterialLoader.h" +#include "Base64.h" #include "Game/IW5/CommonIW5.h" #include "Game/IW5/Material/JsonMaterial.h" -#include "Impl/Base64.h" #include #include diff --git a/src/ObjLoading/ObjContainer/SoundBank/SoundBankWriter.cpp b/src/ObjLoading/ObjContainer/SoundBank/SoundBankWriter.cpp index 4910f628..fa31a407 100644 --- a/src/ObjLoading/ObjContainer/SoundBank/SoundBankWriter.cpp +++ b/src/ObjLoading/ObjContainer/SoundBank/SoundBankWriter.cpp @@ -1,6 +1,6 @@ #include "SoundBankWriter.h" -#include "Crypto.h" +#include "Cryptography.h" #include "ObjContainer/SoundBank/SoundBankTypes.h" #include "Sound/FlacDecoder.h" #include "Sound/WavTypes.h" @@ -251,7 +251,7 @@ public: SoundAssetBankChecksum checksum{}; - const auto md5Crypt = Crypto::CreateMD5(); + const auto md5Crypt = cryptography::CreateMd5(); md5Crypt->Process(soundData.get(), soundSize); md5Crypt->Finish(checksum.checksumBytes); diff --git a/src/ObjLoading/XModel/Gltf/Internal/GltfBuffer.cpp b/src/ObjLoading/XModel/Gltf/Internal/GltfBuffer.cpp index fcca640b..d7bd6d3a 100644 --- a/src/ObjLoading/XModel/Gltf/Internal/GltfBuffer.cpp +++ b/src/ObjLoading/XModel/Gltf/Internal/GltfBuffer.cpp @@ -1,6 +1,6 @@ #include "GltfBuffer.h" -#include "Impl/Base64.h" +#include "Base64.h" #include "XModel/Gltf/GltfConstants.h" #include diff --git a/src/ObjWriting/Game/IW5/Material/JsonMaterialWriter.cpp b/src/ObjWriting/Game/IW5/Material/JsonMaterialWriter.cpp index 66ec64b4..ff36505a 100644 --- a/src/ObjWriting/Game/IW5/Material/JsonMaterialWriter.cpp +++ b/src/ObjWriting/Game/IW5/Material/JsonMaterialWriter.cpp @@ -1,8 +1,8 @@ #include "JsonMaterialWriter.h" +#include "Base64.h" #include "Game/IW5/CommonIW5.h" #include "Game/IW5/Material/JsonMaterial.h" -#include "Impl/Base64.h" #include "MaterialConstantZoneState.h" #include diff --git a/src/ObjWriting/XModel/Gltf/GltfTextOutput.cpp b/src/ObjWriting/XModel/Gltf/GltfTextOutput.cpp index 49afebd6..c80a72f9 100644 --- a/src/ObjWriting/XModel/Gltf/GltfTextOutput.cpp +++ b/src/ObjWriting/XModel/Gltf/GltfTextOutput.cpp @@ -6,7 +6,7 @@ #include #define LTC_NO_PROTOTYPES -#include "Impl/Base64.h" +#include "Base64.h" #include diff --git a/src/ZoneCommon.lua b/src/ZoneCommon.lua index fa9fd969..3f005f49 100644 --- a/src/ZoneCommon.lua +++ b/src/ZoneCommon.lua @@ -9,14 +9,14 @@ function ZoneCommon:include(includes) Common:include(includes) ObjCommon:include(includes) Parser:include(includes) - Crypto:include(includes) + Cryptography:include(includes) end end function ZoneCommon:link(links) links:add(self:name()) links:linkto(Common) - links:linkto(Crypto) + links:linkto(Cryptography) links:linkto(ObjCommon) links:linkto(Parser) links:linkto(Utils) diff --git a/src/ZoneCommon/Zone/XChunk/AbstractSalsa20Processor.cpp b/src/ZoneCommon/Zone/XChunk/AbstractSalsa20Processor.cpp index a89f6bc7..377a74be 100644 --- a/src/ZoneCommon/Zone/XChunk/AbstractSalsa20Processor.cpp +++ b/src/ZoneCommon/Zone/XChunk/AbstractSalsa20Processor.cpp @@ -40,8 +40,8 @@ void AbstractSalsa20Processor::InitStreams(const std::string& zoneName, const ui { m_stream_block_indices[stream] = 0; - m_stream_contexts[stream].m_salsa20 = Crypto::CreateSalsa20(salsa20Key, keySize); - m_stream_contexts[stream].m_sha1 = Crypto::CreateSHA1(); + m_stream_contexts[stream].m_salsa20 = cryptography::CreateSalsa20(salsa20Key, keySize); + m_stream_contexts[stream].m_sha1 = cryptography::CreateSha1(); } } diff --git a/src/ZoneCommon/Zone/XChunk/AbstractSalsa20Processor.h b/src/ZoneCommon/Zone/XChunk/AbstractSalsa20Processor.h index 38ed4aad..01d56a63 100644 --- a/src/ZoneCommon/Zone/XChunk/AbstractSalsa20Processor.h +++ b/src/ZoneCommon/Zone/XChunk/AbstractSalsa20Processor.h @@ -1,5 +1,6 @@ #pragma once -#include "Crypto.h" + +#include "Cryptography.h" #include "Utils/ClassUtils.h" #include "Utils/ICapturedDataProvider.h" @@ -17,8 +18,8 @@ protected: class StreamContext { public: - std::unique_ptr m_salsa20; - std::unique_ptr m_sha1; + std::unique_ptr m_salsa20; + std::unique_ptr m_sha1; }; int m_stream_count; diff --git a/src/ZoneCommon/Zone/XChunk/XChunkProcessorSalsa20Decryption.cpp b/src/ZoneCommon/Zone/XChunk/XChunkProcessorSalsa20Decryption.cpp index 57018c62..cf1bfb05 100644 --- a/src/ZoneCommon/Zone/XChunk/XChunkProcessorSalsa20Decryption.cpp +++ b/src/ZoneCommon/Zone/XChunk/XChunkProcessorSalsa20Decryption.cpp @@ -1,7 +1,7 @@ #include "XChunkProcessorSalsa20Decryption.h" #include "AbstractSalsa20Processor.h" -#include "Crypto.h" +#include "Cryptography.h" #include @@ -21,10 +21,10 @@ size_t XChunkProcessorSalsa20Decryption::Process( assert(output != nullptr); assert(inputLength <= outputBufferSize); - auto& streamContext = m_stream_contexts[streamNumber]; + const auto& streamContext = m_stream_contexts[streamNumber]; // Initialize Salsa20 with an IV of the first 8 bytes of the current hash block - streamContext.m_salsa20->SetIV(GetHashBlock(streamNumber), SALSA20_IV_SIZE); + streamContext.m_salsa20->SetIv(GetHashBlock(streamNumber), SALSA20_IV_SIZE); streamContext.m_salsa20->Process(input, output, inputLength); // Hash decrypted XChunk diff --git a/src/ZoneCommon/Zone/XChunk/XChunkProcessorSalsa20Encryption.cpp b/src/ZoneCommon/Zone/XChunk/XChunkProcessorSalsa20Encryption.cpp index 3f36b12d..80ca8928 100644 --- a/src/ZoneCommon/Zone/XChunk/XChunkProcessorSalsa20Encryption.cpp +++ b/src/ZoneCommon/Zone/XChunk/XChunkProcessorSalsa20Encryption.cpp @@ -27,7 +27,7 @@ size_t XChunkProcessorSalsa20Encryption::Process( streamContext.m_sha1->Finish(&blockSha1Hash); // Initialize Salsa20 with an IV of the first 8 bytes of the current hash block - streamContext.m_salsa20->SetIV(GetHashBlock(streamNumber), SALSA20_IV_SIZE); + streamContext.m_salsa20->SetIv(GetHashBlock(streamNumber), SALSA20_IV_SIZE); streamContext.m_salsa20->Process(input, output, inputLength); // Advance index to next hash block diff --git a/src/ZoneLoading.lua b/src/ZoneLoading.lua index 4cc3c0fa..0d555bca 100644 --- a/src/ZoneLoading.lua +++ b/src/ZoneLoading.lua @@ -11,7 +11,7 @@ end function ZoneLoading:link(links) links:add(self:name()) - links:linkto(Crypto) + links:linkto(Cryptography) links:linkto(Utils) links:linkto(ZoneCommon) links:linkto(zlib) @@ -53,7 +53,7 @@ function ZoneLoading:project() } self:include(includes) - Crypto:include(includes) + Cryptography:include(includes) Utils:include(includes) zlib:include(includes) ZoneCode:include(includes) diff --git a/src/ZoneLoading/Game/IW4/ZoneLoaderFactoryIW4.cpp b/src/ZoneLoading/Game/IW4/ZoneLoaderFactoryIW4.cpp index a90f8993..e2b4bbc0 100644 --- a/src/ZoneLoading/Game/IW4/ZoneLoaderFactoryIW4.cpp +++ b/src/ZoneLoading/Game/IW4/ZoneLoaderFactoryIW4.cpp @@ -91,11 +91,11 @@ namespace #undef XBLOCK_DEF } - std::unique_ptr SetupRSA(const bool isOfficial) + std::unique_ptr SetupRsa(const bool isOfficial) { if (isOfficial) { - auto rsa = Crypto::CreateRSA(IPublicKeyAlgorithm::HashingAlgorithm::RSA_HASH_SHA256, Crypto::RSAPaddingMode::RSA_PADDING_PSS); + auto rsa = cryptography::CreateRsa(cryptography::HashingAlgorithm::RSA_HASH_SHA256, cryptography::RsaPaddingMode::RSA_PADDING_PSS); if (!rsa->SetKey(ZoneConstants::RSA_PUBLIC_KEY_INFINITY_WARD, sizeof(ZoneConstants::RSA_PUBLIC_KEY_INFINITY_WARD))) { @@ -121,7 +121,7 @@ namespace return; // If file is signed setup a RSA instance. - auto rsa = SetupRSA(isOfficial); + auto rsa = SetupRsa(isOfficial); zoneLoader.AddLoadingStep(std::make_unique(ZoneConstants::MAGIC_AUTH_HEADER)); zoneLoader.AddLoadingStep(std::make_unique(4)); // Skip reserved @@ -147,8 +147,7 @@ namespace auto* masterBlockHashesPtr = masterBlockHashes.get(); zoneLoader.AddLoadingStep(std::move(masterBlockHashes)); - zoneLoader.AddLoadingStep( - std::make_unique(std::unique_ptr(Crypto::CreateSHA256()), 0, subHeaderHashPtr, subHeaderCapturePtr)); + zoneLoader.AddLoadingStep(std::make_unique(cryptography::CreateSha256(), 0, subHeaderHashPtr, subHeaderCapturePtr)); zoneLoader.AddLoadingStep(std::make_unique(subHeaderCapturePtr)); // Skip the rest of the first chunk @@ -158,7 +157,7 @@ namespace std::make_unique(std::make_unique(ZoneConstants::AUTHED_CHUNK_COUNT_PER_GROUP, ZoneConstants::AUTHED_CHUNK_SIZE, std::extent_v, - Crypto::CreateSHA256(), + cryptography::CreateSha256(), masterBlockHashesPtr))); } } // namespace diff --git a/src/ZoneLoading/Game/IW5/ZoneLoaderFactoryIW5.cpp b/src/ZoneLoading/Game/IW5/ZoneLoaderFactoryIW5.cpp index a5dc37bc..f2a18c53 100644 --- a/src/ZoneLoading/Game/IW5/ZoneLoaderFactoryIW5.cpp +++ b/src/ZoneLoading/Game/IW5/ZoneLoaderFactoryIW5.cpp @@ -75,11 +75,11 @@ namespace #undef XBLOCK_DEF } - std::unique_ptr SetupRSA(const bool isOfficial) + std::unique_ptr SetupRsa(const bool isOfficial) { if (isOfficial) { - auto rsa = Crypto::CreateRSA(IPublicKeyAlgorithm::HashingAlgorithm::RSA_HASH_SHA256, Crypto::RSAPaddingMode::RSA_PADDING_PSS); + auto rsa = cryptography::CreateRsa(cryptography::HashingAlgorithm::RSA_HASH_SHA256, cryptography::RsaPaddingMode::RSA_PADDING_PSS); if (!rsa->SetKey(ZoneConstants::RSA_PUBLIC_KEY_INFINITY_WARD, sizeof(ZoneConstants::RSA_PUBLIC_KEY_INFINITY_WARD))) { @@ -105,7 +105,7 @@ namespace return; // If file is signed setup a RSA instance. - auto rsa = SetupRSA(isOfficial); + auto rsa = SetupRsa(isOfficial); zoneLoader.AddLoadingStep(std::make_unique(ZoneConstants::MAGIC_AUTH_HEADER)); zoneLoader.AddLoadingStep(std::make_unique(4)); // Skip reserved @@ -131,8 +131,7 @@ namespace auto* masterBlockHashesPtr = masterBlockHashes.get(); zoneLoader.AddLoadingStep(std::move(masterBlockHashes)); - zoneLoader.AddLoadingStep( - std::make_unique(std::unique_ptr(Crypto::CreateSHA256()), 0, subHeaderHashPtr, subHeaderCapturePtr)); + zoneLoader.AddLoadingStep(std::make_unique(cryptography::CreateSha256(), 0, subHeaderHashPtr, subHeaderCapturePtr)); zoneLoader.AddLoadingStep(std::make_unique(subHeaderCapturePtr)); // Skip the rest of the first chunk @@ -142,7 +141,7 @@ namespace std::make_unique(std::make_unique(ZoneConstants::AUTHED_CHUNK_COUNT_PER_GROUP, ZoneConstants::AUTHED_CHUNK_SIZE, std::extent_v, - Crypto::CreateSHA256(), + cryptography::CreateSha256(), masterBlockHashesPtr))); } } // namespace diff --git a/src/ZoneLoading/Game/T6/ZoneLoaderFactoryT6.cpp b/src/ZoneLoading/Game/T6/ZoneLoaderFactoryT6.cpp index 6ebb70f6..09642fd6 100644 --- a/src/ZoneLoading/Game/T6/ZoneLoaderFactoryT6.cpp +++ b/src/ZoneLoading/Game/T6/ZoneLoaderFactoryT6.cpp @@ -105,11 +105,11 @@ namespace #undef XBLOCK_DEF } - std::unique_ptr SetupRSA(const bool isOfficial) + std::unique_ptr SetupRsa(const bool isOfficial) { if (isOfficial) { - auto rsa = Crypto::CreateRSA(IPublicKeyAlgorithm::HashingAlgorithm::RSA_HASH_SHA256, Crypto::RSAPaddingMode::RSA_PADDING_PSS); + auto rsa = cryptography::CreateRsa(cryptography::HashingAlgorithm::RSA_HASH_SHA256, cryptography::RsaPaddingMode::RSA_PADDING_PSS); if (!rsa->SetKey(ZoneConstants::RSA_PUBLIC_KEY_TREYARCH, sizeof(ZoneConstants::RSA_PUBLIC_KEY_TREYARCH))) { @@ -190,7 +190,7 @@ std::unique_ptr ZoneLoaderFactory::CreateLoaderForHeader(ZoneHeader& SetupBlock(*zoneLoader); // If file is signed setup a RSA instance. - auto rsa = isSecure ? SetupRSA(isOfficial) : nullptr; + auto rsa = isSecure ? SetupRsa(isOfficial) : nullptr; // Add steps for loading the auth header which also contain the signature of the zone if it is signed. ISignatureProvider* signatureProvider = AddAuthHeaderSteps(isSecure, *zoneLoader, fileName); diff --git a/src/ZoneLoading/Loading/ILoadingStream.h b/src/ZoneLoading/Loading/ILoadingStream.h index aade82e1..d9512718 100644 --- a/src/ZoneLoading/Loading/ILoadingStream.h +++ b/src/ZoneLoading/Loading/ILoadingStream.h @@ -6,7 +6,12 @@ class ILoadingStream { public: + ILoadingStream() = default; virtual ~ILoadingStream() = default; + ILoadingStream(const ILoadingStream& other) = default; + ILoadingStream(ILoadingStream&& other) noexcept = default; + ILoadingStream& operator=(const ILoadingStream& other) = default; + ILoadingStream& operator=(ILoadingStream&& other) noexcept = default; virtual size_t Load(void* buffer, size_t length) = 0; virtual int64_t Pos() = 0; diff --git a/src/ZoneLoading/Loading/Processor/ProcessorAuthedBlocks.cpp b/src/ZoneLoading/Loading/Processor/ProcessorAuthedBlocks.cpp index aeb86e5a..cc5c8f39 100644 --- a/src/ZoneLoading/Loading/Processor/ProcessorAuthedBlocks.cpp +++ b/src/ZoneLoading/Loading/Processor/ProcessorAuthedBlocks.cpp @@ -17,7 +17,7 @@ class ProcessorAuthedBlocks::Impl const size_t m_chunk_size; const unsigned m_max_master_block_count; - const std::unique_ptr m_hash_function; + const std::unique_ptr m_hash_function; IHashProvider* const m_master_block_hash_provider; const std::unique_ptr m_chunk_hashes_buffer; const std::unique_ptr m_current_chunk_hash_buffer; @@ -34,7 +34,7 @@ public: const unsigned authedChunkCount, const size_t chunkSize, const unsigned maxMasterBlockCount, - std::unique_ptr hashFunction, + std::unique_ptr hashFunction, IHashProvider* masterBlockHashProvider) : m_base(base), m_authed_chunk_count(authedChunkCount), @@ -141,7 +141,7 @@ public: ProcessorAuthedBlocks::ProcessorAuthedBlocks(const unsigned authedChunkCount, const size_t chunkSize, const unsigned maxMasterBlockCount, - std::unique_ptr hashFunction, + std::unique_ptr hashFunction, IHashProvider* masterBlockHashProvider) : m_impl(new Impl(this, authedChunkCount, chunkSize, maxMasterBlockCount, std::move(hashFunction), masterBlockHashProvider)) { diff --git a/src/ZoneLoading/Loading/Processor/ProcessorAuthedBlocks.h b/src/ZoneLoading/Loading/Processor/ProcessorAuthedBlocks.h index 22a82e6e..5bcf4a19 100644 --- a/src/ZoneLoading/Loading/Processor/ProcessorAuthedBlocks.h +++ b/src/ZoneLoading/Loading/Processor/ProcessorAuthedBlocks.h @@ -1,5 +1,6 @@ #pragma once -#include "Crypto.h" + +#include "Cryptography.h" #include "Loading/IHashProvider.h" #include "Loading/StreamProcessor.h" @@ -14,7 +15,7 @@ public: ProcessorAuthedBlocks(unsigned authedChunkCount, size_t chunkSize, unsigned maxMasterBlockCount, - std::unique_ptr hashFunction, + std::unique_ptr hashFunction, IHashProvider* masterBlockHashProvider); ~ProcessorAuthedBlocks() override; ProcessorAuthedBlocks(const ProcessorAuthedBlocks& other) = delete; diff --git a/src/ZoneLoading/Loading/Processor/ProcessorStreamCipher.cpp b/src/ZoneLoading/Loading/Processor/ProcessorStreamCipher.cpp index 5a03ace9..73279921 100644 --- a/src/ZoneLoading/Loading/Processor/ProcessorStreamCipher.cpp +++ b/src/ZoneLoading/Loading/Processor/ProcessorStreamCipher.cpp @@ -1,14 +1,8 @@ #include "ProcessorStreamCipher.h" -ProcessorStreamCipher::ProcessorStreamCipher(IStreamCipher* cipher) +ProcessorStreamCipher::ProcessorStreamCipher(std::unique_ptr cipher) + : m_cipher(std::move(cipher)) { - m_cipher = cipher; -} - -ProcessorStreamCipher::~ProcessorStreamCipher() -{ - delete m_cipher; - m_cipher = nullptr; } size_t ProcessorStreamCipher::Load(void* buffer, const size_t length) diff --git a/src/ZoneLoading/Loading/Processor/ProcessorStreamCipher.h b/src/ZoneLoading/Loading/Processor/ProcessorStreamCipher.h index ebcc3b24..3b40f4a5 100644 --- a/src/ZoneLoading/Loading/Processor/ProcessorStreamCipher.h +++ b/src/ZoneLoading/Loading/Processor/ProcessorStreamCipher.h @@ -1,14 +1,17 @@ #pragma once -#include "Crypto.h" + +#include "Cryptography.h" #include "Loading/StreamProcessor.h" +#include + class ProcessorStreamCipher final : public StreamProcessor { - IStreamCipher* m_cipher; - public: - explicit ProcessorStreamCipher(IStreamCipher* cipher); - ~ProcessorStreamCipher() override; + explicit ProcessorStreamCipher(std::unique_ptr cipher); size_t Load(void* buffer, size_t length) override; + +private: + std::unique_ptr m_cipher; }; diff --git a/src/ZoneLoading/Loading/Steps/StepVerifyHash.cpp b/src/ZoneLoading/Loading/Steps/StepVerifyHash.cpp index 7264000d..dcbacf8f 100644 --- a/src/ZoneLoading/Loading/Steps/StepVerifyHash.cpp +++ b/src/ZoneLoading/Loading/Steps/StepVerifyHash.cpp @@ -5,7 +5,7 @@ #include #include -StepVerifyHash::StepVerifyHash(std::unique_ptr hashFunction, +StepVerifyHash::StepVerifyHash(std::unique_ptr hashFunction, const unsigned hashIndex, IHashProvider* hashProvider, ICapturedDataProvider* dataProvider) diff --git a/src/ZoneLoading/Loading/Steps/StepVerifyHash.h b/src/ZoneLoading/Loading/Steps/StepVerifyHash.h index b02b4078..b85fb3d7 100644 --- a/src/ZoneLoading/Loading/Steps/StepVerifyHash.h +++ b/src/ZoneLoading/Loading/Steps/StepVerifyHash.h @@ -1,6 +1,6 @@ #pragma once -#include "Crypto.h" +#include "Cryptography.h" #include "Loading/IHashProvider.h" #include "Loading/ILoadingStep.h" #include "Utils/ICapturedDataProvider.h" @@ -9,13 +9,16 @@ class StepVerifyHash final : public ILoadingStep { - std::unique_ptr m_hash_function; + std::unique_ptr m_hash_function; unsigned m_hash_index; IHashProvider* m_hash_provider; ICapturedDataProvider* m_data_provider; public: - StepVerifyHash(std::unique_ptr hashFunction, unsigned hashIndex, IHashProvider* hashProvider, ICapturedDataProvider* dataProvider); + StepVerifyHash(std::unique_ptr hashFunction, + unsigned hashIndex, + IHashProvider* hashProvider, + ICapturedDataProvider* dataProvider); ~StepVerifyHash(); StepVerifyHash(const StepVerifyHash& other) = delete; StepVerifyHash(StepVerifyHash&& other) noexcept = default; diff --git a/src/ZoneLoading/Loading/Steps/StepVerifySignature.cpp b/src/ZoneLoading/Loading/Steps/StepVerifySignature.cpp index c5f2e74e..00152fd6 100644 --- a/src/ZoneLoading/Loading/Steps/StepVerifySignature.cpp +++ b/src/ZoneLoading/Loading/Steps/StepVerifySignature.cpp @@ -4,7 +4,7 @@ #include -StepVerifySignature::StepVerifySignature(std::unique_ptr signatureAlgorithm, +StepVerifySignature::StepVerifySignature(std::unique_ptr signatureAlgorithm, ISignatureProvider* signatureProvider, ICapturedDataProvider* signatureDataProvider) : m_algorithm(std::move(signatureAlgorithm)), diff --git a/src/ZoneLoading/Loading/Steps/StepVerifySignature.h b/src/ZoneLoading/Loading/Steps/StepVerifySignature.h index f415ebea..3ce85bfe 100644 --- a/src/ZoneLoading/Loading/Steps/StepVerifySignature.h +++ b/src/ZoneLoading/Loading/Steps/StepVerifySignature.h @@ -1,18 +1,18 @@ #pragma once -#include "Crypto.h" +#include "Cryptography.h" #include "Loading/ILoadingStep.h" #include "Loading/ISignatureProvider.h" #include "Utils/ICapturedDataProvider.h" class StepVerifySignature final : public ILoadingStep { - std::unique_ptr m_algorithm; + std::unique_ptr m_algorithm; ISignatureProvider* m_signature_provider; ICapturedDataProvider* m_signature_data_provider; public: - StepVerifySignature(std::unique_ptr signatureAlgorithm, + StepVerifySignature(std::unique_ptr signatureAlgorithm, ISignatureProvider* signatureProvider, ICapturedDataProvider* signatureDataProvider); ~StepVerifySignature() override = default; diff --git a/src/ZoneLoading/Loading/StreamProcessor.h b/src/ZoneLoading/Loading/StreamProcessor.h index d3702777..4a718276 100644 --- a/src/ZoneLoading/Loading/StreamProcessor.h +++ b/src/ZoneLoading/Loading/StreamProcessor.h @@ -4,11 +4,11 @@ class StreamProcessor : public ILoadingStream { -protected: - ILoadingStream* m_base_stream; - public: StreamProcessor(); void SetBaseStream(ILoadingStream* baseStream); + +protected: + ILoadingStream* m_base_stream; }; diff --git a/src/ZoneWriting.lua b/src/ZoneWriting.lua index 693662e4..a55b33dd 100644 --- a/src/ZoneWriting.lua +++ b/src/ZoneWriting.lua @@ -11,7 +11,7 @@ end function ZoneWriting:link(links) links:add(self:name()) - links:linkto(Crypto) + links:linkto(Cryptography) links:linkto(Utils) links:linkto(ZoneCommon) links:linkto(zlib) @@ -49,7 +49,7 @@ function ZoneWriting:project() } self:include(includes) - Crypto:include(includes) + Cryptography:include(includes) Utils:include(includes) zlib:include(includes) ZoneCode:include(includes)