mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-12-15 23:37:46 +00:00
refactor: cryptography component
This commit is contained in:
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();
|
||||
}
|
||||
Reference in New Issue
Block a user