mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-07-05 02:57:52 -05:00
Import code from previous AssetBuilder version
This commit is contained in:
118
src/Crypto/Impl/AlgorithmRSA.cpp
Normal file
118
src/Crypto/Impl/AlgorithmRSA.cpp
Normal file
@ -0,0 +1,118 @@
|
||||
#include "AlgorithmRSA.h"
|
||||
#include <cstring>
|
||||
|
||||
#include "CryptoLibrary.h"
|
||||
|
||||
class AlgorithmRSA::AlgorithmRSAImpl
|
||||
{
|
||||
rsa_key m_key{};
|
||||
HashingAlgorithm m_hash;
|
||||
Crypto::RSAPaddingMode m_padding;
|
||||
|
||||
const ltc_hash_descriptor* GetHashDescriptor() const
|
||||
{
|
||||
switch(m_hash)
|
||||
{
|
||||
case RSA_HASH_SHA256:
|
||||
return &sha256_desc;
|
||||
|
||||
default:
|
||||
case RSA_HASH_SHA512:
|
||||
return &sha512_desc;
|
||||
}
|
||||
}
|
||||
|
||||
int GetPaddingMode() const
|
||||
{
|
||||
switch(m_padding)
|
||||
{
|
||||
case Crypto::RSA_PADDING_PKS1:
|
||||
return LTC_PKCS_1_V1_5;
|
||||
|
||||
default:
|
||||
case Crypto::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);
|
||||
}
|
24
src/Crypto/Impl/AlgorithmRSA.h
Normal file
24
src/Crypto/Impl/AlgorithmRSA.h
Normal file
@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
#include "IPublicKeyAlgorithm.h"
|
||||
#include "Crypto.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;
|
||||
};
|
63
src/Crypto/Impl/AlgorithmSHA1.cpp
Normal file
63
src/Crypto/Impl/AlgorithmSHA1.cpp
Normal file
@ -0,0 +1,63 @@
|
||||
#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);
|
||||
}
|
20
src/Crypto/Impl/AlgorithmSHA1.h
Normal file
20
src/Crypto/Impl/AlgorithmSHA1.h
Normal file
@ -0,0 +1,20 @@
|
||||
#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;
|
||||
};
|
87
src/Crypto/Impl/AlgorithmSalsa20.cpp
Normal file
87
src/Crypto/Impl/AlgorithmSalsa20.cpp
Normal file
@ -0,0 +1,87 @@
|
||||
#include "AlgorithmSalsa20.h"
|
||||
#include "salsa20.h"
|
||||
#include <cassert>
|
||||
#include <exception>
|
||||
|
||||
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::exception("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);
|
||||
}
|
21
src/Crypto/Impl/AlgorithmSalsa20.h
Normal file
21
src/Crypto/Impl/AlgorithmSalsa20.h
Normal file
@ -0,0 +1,21 @@
|
||||
#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;
|
||||
};
|
20
src/Crypto/Impl/CryptoLibrary.h
Normal file
20
src/Crypto/Impl/CryptoLibrary.h
Normal file
@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#define LTC_NO_PROTOTYPES
|
||||
#include "tomcrypt.h"
|
||||
|
||||
class CryptoLibrary
|
||||
{
|
||||
public:
|
||||
static void Init()
|
||||
{
|
||||
static bool initialized = false;
|
||||
|
||||
if(!initialized)
|
||||
{
|
||||
initialized = true;
|
||||
|
||||
ltc_mp = ltm_desc;
|
||||
}
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user