mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-05-06 20:44:57 +00:00
refactor: cryptography component
This commit is contained in:
parent
60f5c1a18f
commit
5635470b6e
@ -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()
|
||||
|
@ -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<IHashFunction> Crypto::CreateMD5()
|
||||
{
|
||||
return std::make_unique<AlgorithmMD5>();
|
||||
}
|
||||
|
||||
std::unique_ptr<IHashFunction> Crypto::CreateSHA1()
|
||||
{
|
||||
return std::make_unique<AlgorithmSHA1>();
|
||||
}
|
||||
|
||||
std::unique_ptr<IHashFunction> Crypto::CreateSHA256()
|
||||
{
|
||||
return std::make_unique<AlgorithmSHA256>();
|
||||
}
|
||||
|
||||
std::unique_ptr<IStreamCipher> Crypto::CreateSalsa20(const uint8_t* keyBytes, const size_t keySize)
|
||||
{
|
||||
return std::make_unique<AlgorithmSalsa20>(keyBytes, keySize);
|
||||
}
|
||||
|
||||
std::unique_ptr<IPublicKeyAlgorithm> Crypto::CreateRSA(const IPublicKeyAlgorithm::HashingAlgorithm hashingAlgorithm, const RSAPaddingMode paddingMode)
|
||||
{
|
||||
return std::make_unique<AlgorithmRSA>(hashingAlgorithm, paddingMode);
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "IHashFunction.h"
|
||||
#include "IPublicKeyAlgorithm.h"
|
||||
#include "IStreamCipher.h"
|
||||
|
||||
#include <cstddef>
|
||||
#include <memory>
|
||||
|
||||
class Crypto
|
||||
{
|
||||
public:
|
||||
enum class RSAPaddingMode
|
||||
{
|
||||
RSA_PADDING_PKS1,
|
||||
RSA_PADDING_PSS,
|
||||
};
|
||||
|
||||
static std::unique_ptr<IHashFunction> CreateMD5();
|
||||
|
||||
static std::unique_ptr<IHashFunction> CreateSHA1();
|
||||
static std::unique_ptr<IHashFunction> CreateSHA256();
|
||||
|
||||
static std::unique_ptr<IStreamCipher> CreateSalsa20(const uint8_t* keyBytes, size_t keySize);
|
||||
|
||||
static std::unique_ptr<IPublicKeyAlgorithm> CreateRSA(IPublicKeyAlgorithm::HashingAlgorithm hashingAlgorithm, RSAPaddingMode paddingMode);
|
||||
};
|
@ -1,14 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
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;
|
||||
};
|
@ -1,22 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
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;
|
||||
};
|
@ -1,14 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
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;
|
||||
};
|
@ -1,64 +0,0 @@
|
||||
#include "AlgorithmMD5.h"
|
||||
|
||||
#include "CryptoLibrary.h"
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
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<const uint8_t*>(input), inputSize);
|
||||
}
|
||||
|
||||
void Finish(void* hashBuffer)
|
||||
{
|
||||
md5_done(&m_state, static_cast<uint8_t*>(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);
|
||||
}
|
@ -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;
|
||||
};
|
@ -1,119 +0,0 @@
|
||||
#include "AlgorithmRSA.h"
|
||||
|
||||
#include "CryptoLibrary.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
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);
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
#pragma once
|
||||
#include "Crypto.h"
|
||||
#include "IPublicKeyAlgorithm.h"
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
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;
|
||||
};
|
@ -1,64 +0,0 @@
|
||||
#include "AlgorithmSHA1.h"
|
||||
|
||||
#include "CryptoLibrary.h"
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
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<const uint8_t*>(input), inputSize);
|
||||
}
|
||||
|
||||
void Finish(void* hashBuffer)
|
||||
{
|
||||
sha1_done(&m_state, static_cast<uint8_t*>(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);
|
||||
}
|
@ -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;
|
||||
};
|
@ -1,64 +0,0 @@
|
||||
#include "AlgorithmSHA256.h"
|
||||
|
||||
#include "CryptoLibrary.h"
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
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<const uint8_t*>(input), inputSize);
|
||||
}
|
||||
|
||||
void Finish(void* hashBuffer)
|
||||
{
|
||||
sha256_done(&m_state, static_cast<uint8_t*>(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);
|
||||
}
|
@ -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;
|
||||
};
|
@ -1,89 +0,0 @@
|
||||
#include "AlgorithmSalsa20.h"
|
||||
|
||||
#include "salsa20.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <stdexcept>
|
||||
|
||||
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<const uint8_t*>(plainText), static_cast<uint8_t*>(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);
|
||||
}
|
@ -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;
|
||||
};
|
@ -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;
|
||||
}
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#define LTC_NO_PROTOTYPES
|
||||
#include "tomcrypt.h"
|
||||
|
||||
class CryptoLibrary
|
||||
{
|
||||
public:
|
||||
static void Init();
|
||||
};
|
@ -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)
|
54
src/Cryptography/Algorithms/AlgorithmMd5.cpp
Normal file
54
src/Cryptography/Algorithms/AlgorithmMd5.cpp
Normal file
@ -0,0 +1,54 @@
|
||||
#include "AlgorithmMd5.h"
|
||||
|
||||
#include "Internal/CryptoLibrary.h"
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
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<const uint8_t*>(input), static_cast<unsigned long>(inputSize));
|
||||
}
|
||||
|
||||
void Finish(void* hashBuffer) override
|
||||
{
|
||||
md5_done(&m_state, static_cast<uint8_t*>(hashBuffer));
|
||||
}
|
||||
|
||||
private:
|
||||
hash_state m_state{};
|
||||
};
|
||||
} // namespace
|
||||
|
||||
namespace cryptography
|
||||
{
|
||||
std::unique_ptr<IHashFunction> CreateMd5()
|
||||
{
|
||||
return std::make_unique<AlgorithmMd5>();
|
||||
}
|
||||
} // namespace cryptography
|
10
src/Cryptography/Algorithms/AlgorithmMd5.h
Normal file
10
src/Cryptography/Algorithms/AlgorithmMd5.h
Normal file
@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include "IHashFunction.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace cryptography
|
||||
{
|
||||
std::unique_ptr<IHashFunction> CreateMd5();
|
||||
}
|
84
src/Cryptography/Algorithms/AlgorithmRsa.cpp
Normal file
84
src/Cryptography/Algorithms/AlgorithmRsa.cpp
Normal file
@ -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<unsigned long>(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<unsigned long>(signatureSize),
|
||||
signedData,
|
||||
static_cast<unsigned long>(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<IPublicKeyAlgorithm> CreateRsa(const HashingAlgorithm hashingAlgorithm, const RsaPaddingMode paddingMode)
|
||||
{
|
||||
return std::make_unique<AlgorithmRsa>(hashingAlgorithm, paddingMode);
|
||||
}
|
||||
} // namespace cryptography
|
17
src/Cryptography/Algorithms/AlgorithmRsa.h
Normal file
17
src/Cryptography/Algorithms/AlgorithmRsa.h
Normal file
@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include "IPublicKeyAlgorithm.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
|
||||
namespace cryptography
|
||||
{
|
||||
enum class RsaPaddingMode : std::uint8_t
|
||||
{
|
||||
RSA_PADDING_PKS1,
|
||||
RSA_PADDING_PSS,
|
||||
};
|
||||
|
||||
std::unique_ptr<IPublicKeyAlgorithm> CreateRsa(HashingAlgorithm hashingAlgorithm, RsaPaddingMode paddingMode);
|
||||
} // namespace cryptography
|
46
src/Cryptography/Algorithms/AlgorithmSalsa20.cpp
Normal file
46
src/Cryptography/Algorithms/AlgorithmSalsa20.cpp
Normal file
@ -0,0 +1,46 @@
|
||||
#include "AlgorithmSalsa20.h"
|
||||
|
||||
#include "salsa20.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <stdexcept>
|
||||
|
||||
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<uint32_t>(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<const uint8_t*>(plainText), static_cast<uint8_t*>(cipherText), static_cast<uint32_t>(amount));
|
||||
}
|
||||
|
||||
private:
|
||||
salsa20_ctx m_context{};
|
||||
};
|
||||
} // namespace
|
||||
|
||||
namespace cryptography
|
||||
{
|
||||
std::unique_ptr<IStreamCipher> CreateSalsa20(const uint8_t* keyBytes, const size_t keySize)
|
||||
{
|
||||
return std::make_unique<AlgorithmSalsa20>(keyBytes, keySize);
|
||||
}
|
||||
} // namespace cryptography
|
10
src/Cryptography/Algorithms/AlgorithmSalsa20.h
Normal file
10
src/Cryptography/Algorithms/AlgorithmSalsa20.h
Normal file
@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include "IStreamCipher.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace cryptography
|
||||
{
|
||||
std::unique_ptr<IStreamCipher> CreateSalsa20(const uint8_t* keyBytes, size_t keySize);
|
||||
}
|
54
src/Cryptography/Algorithms/AlgorithmSha1.cpp
Normal file
54
src/Cryptography/Algorithms/AlgorithmSha1.cpp
Normal file
@ -0,0 +1,54 @@
|
||||
#include "AlgorithmSha1.h"
|
||||
|
||||
#include "Internal/CryptoLibrary.h"
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
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<const uint8_t*>(input), static_cast<unsigned long>(inputSize));
|
||||
}
|
||||
|
||||
void Finish(void* hashBuffer) override
|
||||
{
|
||||
sha1_done(&m_state, static_cast<uint8_t*>(hashBuffer));
|
||||
}
|
||||
|
||||
private:
|
||||
hash_state m_state{};
|
||||
};
|
||||
} // namespace
|
||||
|
||||
namespace cryptography
|
||||
{
|
||||
std::unique_ptr<IHashFunction> CreateSha1()
|
||||
{
|
||||
return std::make_unique<AlgorithmSha1>();
|
||||
}
|
||||
} // namespace cryptography
|
10
src/Cryptography/Algorithms/AlgorithmSha1.h
Normal file
10
src/Cryptography/Algorithms/AlgorithmSha1.h
Normal file
@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include "IHashFunction.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace cryptography
|
||||
{
|
||||
std::unique_ptr<IHashFunction> CreateSha1();
|
||||
}
|
54
src/Cryptography/Algorithms/AlgorithmSha256.cpp
Normal file
54
src/Cryptography/Algorithms/AlgorithmSha256.cpp
Normal file
@ -0,0 +1,54 @@
|
||||
#include "AlgorithmSha256.h"
|
||||
|
||||
#include "Internal/CryptoLibrary.h"
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
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<const uint8_t*>(input), static_cast<unsigned long>(inputSize));
|
||||
}
|
||||
|
||||
void Finish(void* hashBuffer) override
|
||||
{
|
||||
sha256_done(&m_state, static_cast<uint8_t*>(hashBuffer));
|
||||
}
|
||||
|
||||
private:
|
||||
hash_state m_state{};
|
||||
};
|
||||
} // namespace
|
||||
|
||||
namespace cryptography
|
||||
{
|
||||
std::unique_ptr<IHashFunction> CreateSha256()
|
||||
{
|
||||
return std::make_unique<AlgorithmSha256>();
|
||||
}
|
||||
} // namespace cryptography
|
10
src/Cryptography/Algorithms/AlgorithmSha256.h
Normal file
10
src/Cryptography/Algorithms/AlgorithmSha256.h
Normal file
@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include "IHashFunction.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace cryptography
|
||||
{
|
||||
std::unique_ptr<IHashFunction> CreateSha256();
|
||||
}
|
@ -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<const unsigned char*>(inputData), inputLength, static_cast<char*>(outputBuffer), &outLength);
|
||||
unsigned long outLength = static_cast<unsigned long>(outputBufferSize);
|
||||
const auto result =
|
||||
base64_encode(static_cast<const unsigned char*>(inputData), static_cast<unsigned long>(inputLength), static_cast<char*>(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<unsigned long>(GetBase64DecodeOutputLength(base64Data, inputLength));
|
||||
assert(outLength <= outputBufferSize);
|
||||
if (outLength > outputBufferSize)
|
||||
return 0u;
|
||||
|
||||
const auto result = base64_decode(static_cast<const char*>(base64Data), inputLength, static_cast<unsigned char*>(outputBuffer), &outLength);
|
||||
const auto result =
|
||||
base64_decode(static_cast<const char*>(base64Data), static_cast<unsigned long>(inputLength), static_cast<unsigned char*>(outputBuffer), &outLength);
|
||||
assert(result == CRYPT_OK);
|
||||
|
||||
return static_cast<size_t>(outLength);
|
@ -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
|
7
src/Cryptography/Cryptography.h
Normal file
7
src/Cryptography/Cryptography.h
Normal file
@ -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"
|
22
src/Cryptography/IHashFunction.h
Normal file
22
src/Cryptography/IHashFunction.h
Normal file
@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdlib>
|
||||
|
||||
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
|
30
src/Cryptography/IPublicKeyAlgorithm.h
Normal file
30
src/Cryptography/IPublicKeyAlgorithm.h
Normal file
@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
|
||||
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
|
21
src/Cryptography/IStreamCipher.h
Normal file
21
src/Cryptography/IStreamCipher.h
Normal file
@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
|
||||
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
|
18
src/Cryptography/Internal/CryptoLibrary.cpp
Normal file
18
src/Cryptography/Internal/CryptoLibrary.cpp
Normal file
@ -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
|
13
src/Cryptography/Internal/CryptoLibrary.h
Normal file
13
src/Cryptography/Internal/CryptoLibrary.h
Normal file
@ -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
|
@ -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)
|
||||
|
@ -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 <format>
|
||||
#include <iostream>
|
||||
|
@ -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);
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include "GltfBuffer.h"
|
||||
|
||||
#include "Impl/Base64.h"
|
||||
#include "Base64.h"
|
||||
#include "XModel/Gltf/GltfConstants.h"
|
||||
|
||||
#include <cassert>
|
||||
|
@ -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 <iomanip>
|
||||
|
@ -6,7 +6,7 @@
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#define LTC_NO_PROTOTYPES
|
||||
#include "Impl/Base64.h"
|
||||
#include "Base64.h"
|
||||
|
||||
#include <tomcrypt.h>
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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<IStreamCipher> m_salsa20;
|
||||
std::unique_ptr<IHashFunction> m_sha1;
|
||||
std::unique_ptr<cryptography::IStreamCipher> m_salsa20;
|
||||
std::unique_ptr<cryptography::IHashFunction> m_sha1;
|
||||
};
|
||||
|
||||
int m_stream_count;
|
||||
|
@ -1,7 +1,7 @@
|
||||
#include "XChunkProcessorSalsa20Decryption.h"
|
||||
|
||||
#include "AbstractSalsa20Processor.h"
|
||||
#include "Crypto.h"
|
||||
#include "Cryptography.h"
|
||||
|
||||
#include <cassert>
|
||||
|
||||
@ -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
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
@ -91,11 +91,11 @@ namespace
|
||||
#undef XBLOCK_DEF
|
||||
}
|
||||
|
||||
std::unique_ptr<IPublicKeyAlgorithm> SetupRSA(const bool isOfficial)
|
||||
std::unique_ptr<cryptography::IPublicKeyAlgorithm> 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<StepVerifyMagic>(ZoneConstants::MAGIC_AUTH_HEADER));
|
||||
zoneLoader.AddLoadingStep(std::make_unique<StepSkipBytes>(4)); // Skip reserved
|
||||
@ -147,8 +147,7 @@ namespace
|
||||
auto* masterBlockHashesPtr = masterBlockHashes.get();
|
||||
zoneLoader.AddLoadingStep(std::move(masterBlockHashes));
|
||||
|
||||
zoneLoader.AddLoadingStep(
|
||||
std::make_unique<StepVerifyHash>(std::unique_ptr<IHashFunction>(Crypto::CreateSHA256()), 0, subHeaderHashPtr, subHeaderCapturePtr));
|
||||
zoneLoader.AddLoadingStep(std::make_unique<StepVerifyHash>(cryptography::CreateSha256(), 0, subHeaderHashPtr, subHeaderCapturePtr));
|
||||
zoneLoader.AddLoadingStep(std::make_unique<StepRemoveProcessor>(subHeaderCapturePtr));
|
||||
|
||||
// Skip the rest of the first chunk
|
||||
@ -158,7 +157,7 @@ namespace
|
||||
std::make_unique<StepAddProcessor>(std::make_unique<ProcessorAuthedBlocks>(ZoneConstants::AUTHED_CHUNK_COUNT_PER_GROUP,
|
||||
ZoneConstants::AUTHED_CHUNK_SIZE,
|
||||
std::extent_v<decltype(DB_AuthSubHeader::masterBlockHashes)>,
|
||||
Crypto::CreateSHA256(),
|
||||
cryptography::CreateSha256(),
|
||||
masterBlockHashesPtr)));
|
||||
}
|
||||
} // namespace
|
||||
|
@ -75,11 +75,11 @@ namespace
|
||||
#undef XBLOCK_DEF
|
||||
}
|
||||
|
||||
std::unique_ptr<IPublicKeyAlgorithm> SetupRSA(const bool isOfficial)
|
||||
std::unique_ptr<cryptography::IPublicKeyAlgorithm> 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<StepVerifyMagic>(ZoneConstants::MAGIC_AUTH_HEADER));
|
||||
zoneLoader.AddLoadingStep(std::make_unique<StepSkipBytes>(4)); // Skip reserved
|
||||
@ -131,8 +131,7 @@ namespace
|
||||
auto* masterBlockHashesPtr = masterBlockHashes.get();
|
||||
zoneLoader.AddLoadingStep(std::move(masterBlockHashes));
|
||||
|
||||
zoneLoader.AddLoadingStep(
|
||||
std::make_unique<StepVerifyHash>(std::unique_ptr<IHashFunction>(Crypto::CreateSHA256()), 0, subHeaderHashPtr, subHeaderCapturePtr));
|
||||
zoneLoader.AddLoadingStep(std::make_unique<StepVerifyHash>(cryptography::CreateSha256(), 0, subHeaderHashPtr, subHeaderCapturePtr));
|
||||
zoneLoader.AddLoadingStep(std::make_unique<StepRemoveProcessor>(subHeaderCapturePtr));
|
||||
|
||||
// Skip the rest of the first chunk
|
||||
@ -142,7 +141,7 @@ namespace
|
||||
std::make_unique<StepAddProcessor>(std::make_unique<ProcessorAuthedBlocks>(ZoneConstants::AUTHED_CHUNK_COUNT_PER_GROUP,
|
||||
ZoneConstants::AUTHED_CHUNK_SIZE,
|
||||
std::extent_v<decltype(DB_AuthSubHeader::masterBlockHashes)>,
|
||||
Crypto::CreateSHA256(),
|
||||
cryptography::CreateSha256(),
|
||||
masterBlockHashesPtr)));
|
||||
}
|
||||
} // namespace
|
||||
|
@ -105,11 +105,11 @@ namespace
|
||||
#undef XBLOCK_DEF
|
||||
}
|
||||
|
||||
std::unique_ptr<IPublicKeyAlgorithm> SetupRSA(const bool isOfficial)
|
||||
std::unique_ptr<cryptography::IPublicKeyAlgorithm> 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<ZoneLoader> 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);
|
||||
|
@ -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;
|
||||
|
@ -17,7 +17,7 @@ class ProcessorAuthedBlocks::Impl
|
||||
const size_t m_chunk_size;
|
||||
const unsigned m_max_master_block_count;
|
||||
|
||||
const std::unique_ptr<IHashFunction> m_hash_function;
|
||||
const std::unique_ptr<cryptography::IHashFunction> m_hash_function;
|
||||
IHashProvider* const m_master_block_hash_provider;
|
||||
const std::unique_ptr<uint8_t[]> m_chunk_hashes_buffer;
|
||||
const std::unique_ptr<uint8_t[]> m_current_chunk_hash_buffer;
|
||||
@ -34,7 +34,7 @@ public:
|
||||
const unsigned authedChunkCount,
|
||||
const size_t chunkSize,
|
||||
const unsigned maxMasterBlockCount,
|
||||
std::unique_ptr<IHashFunction> hashFunction,
|
||||
std::unique_ptr<cryptography::IHashFunction> 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<IHashFunction> hashFunction,
|
||||
std::unique_ptr<cryptography::IHashFunction> hashFunction,
|
||||
IHashProvider* masterBlockHashProvider)
|
||||
: m_impl(new Impl(this, authedChunkCount, chunkSize, maxMasterBlockCount, std::move(hashFunction), masterBlockHashProvider))
|
||||
{
|
||||
|
@ -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<IHashFunction> hashFunction,
|
||||
std::unique_ptr<cryptography::IHashFunction> hashFunction,
|
||||
IHashProvider* masterBlockHashProvider);
|
||||
~ProcessorAuthedBlocks() override;
|
||||
ProcessorAuthedBlocks(const ProcessorAuthedBlocks& other) = delete;
|
||||
|
@ -1,14 +1,8 @@
|
||||
#include "ProcessorStreamCipher.h"
|
||||
|
||||
ProcessorStreamCipher::ProcessorStreamCipher(IStreamCipher* cipher)
|
||||
ProcessorStreamCipher::ProcessorStreamCipher(std::unique_ptr<cryptography::IStreamCipher> 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)
|
||||
|
@ -1,14 +1,17 @@
|
||||
#pragma once
|
||||
#include "Crypto.h"
|
||||
|
||||
#include "Cryptography.h"
|
||||
#include "Loading/StreamProcessor.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
class ProcessorStreamCipher final : public StreamProcessor
|
||||
{
|
||||
IStreamCipher* m_cipher;
|
||||
|
||||
public:
|
||||
explicit ProcessorStreamCipher(IStreamCipher* cipher);
|
||||
~ProcessorStreamCipher() override;
|
||||
explicit ProcessorStreamCipher(std::unique_ptr<cryptography::IStreamCipher> cipher);
|
||||
|
||||
size_t Load(void* buffer, size_t length) override;
|
||||
|
||||
private:
|
||||
std::unique_ptr<cryptography::IStreamCipher> m_cipher;
|
||||
};
|
||||
|
@ -5,7 +5,7 @@
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
|
||||
StepVerifyHash::StepVerifyHash(std::unique_ptr<IHashFunction> hashFunction,
|
||||
StepVerifyHash::StepVerifyHash(std::unique_ptr<cryptography::IHashFunction> hashFunction,
|
||||
const unsigned hashIndex,
|
||||
IHashProvider* hashProvider,
|
||||
ICapturedDataProvider* dataProvider)
|
||||
|
@ -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<IHashFunction> m_hash_function;
|
||||
std::unique_ptr<cryptography::IHashFunction> m_hash_function;
|
||||
unsigned m_hash_index;
|
||||
IHashProvider* m_hash_provider;
|
||||
ICapturedDataProvider* m_data_provider;
|
||||
|
||||
public:
|
||||
StepVerifyHash(std::unique_ptr<IHashFunction> hashFunction, unsigned hashIndex, IHashProvider* hashProvider, ICapturedDataProvider* dataProvider);
|
||||
StepVerifyHash(std::unique_ptr<cryptography::IHashFunction> hashFunction,
|
||||
unsigned hashIndex,
|
||||
IHashProvider* hashProvider,
|
||||
ICapturedDataProvider* dataProvider);
|
||||
~StepVerifyHash();
|
||||
StepVerifyHash(const StepVerifyHash& other) = delete;
|
||||
StepVerifyHash(StepVerifyHash&& other) noexcept = default;
|
||||
|
@ -4,7 +4,7 @@
|
||||
|
||||
#include <cassert>
|
||||
|
||||
StepVerifySignature::StepVerifySignature(std::unique_ptr<IPublicKeyAlgorithm> signatureAlgorithm,
|
||||
StepVerifySignature::StepVerifySignature(std::unique_ptr<cryptography::IPublicKeyAlgorithm> signatureAlgorithm,
|
||||
ISignatureProvider* signatureProvider,
|
||||
ICapturedDataProvider* signatureDataProvider)
|
||||
: m_algorithm(std::move(signatureAlgorithm)),
|
||||
|
@ -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<IPublicKeyAlgorithm> m_algorithm;
|
||||
std::unique_ptr<cryptography::IPublicKeyAlgorithm> m_algorithm;
|
||||
ISignatureProvider* m_signature_provider;
|
||||
ICapturedDataProvider* m_signature_data_provider;
|
||||
|
||||
public:
|
||||
StepVerifySignature(std::unique_ptr<IPublicKeyAlgorithm> signatureAlgorithm,
|
||||
StepVerifySignature(std::unique_ptr<cryptography::IPublicKeyAlgorithm> signatureAlgorithm,
|
||||
ISignatureProvider* signatureProvider,
|
||||
ICapturedDataProvider* signatureDataProvider);
|
||||
~StepVerifySignature() override = default;
|
||||
|
@ -4,11 +4,11 @@
|
||||
|
||||
class StreamProcessor : public ILoadingStream
|
||||
{
|
||||
protected:
|
||||
ILoadingStream* m_base_stream;
|
||||
|
||||
public:
|
||||
StreamProcessor();
|
||||
|
||||
void SetBaseStream(ILoadingStream* baseStream);
|
||||
|
||||
protected:
|
||||
ILoadingStream* m_base_stream;
|
||||
};
|
||||
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user