mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-06-26 14:21:49 +00:00
Import code from previous AssetBuilder version
This commit is contained in:
19
src/Crypto/Crypto.cpp
Normal file
19
src/Crypto/Crypto.cpp
Normal file
@ -0,0 +1,19 @@
|
||||
#include "Crypto.h"
|
||||
#include "Impl/AlgorithmRSA.h"
|
||||
#include "Impl/AlgorithmSHA1.h"
|
||||
#include "Impl/AlgorithmSalsa20.h"
|
||||
|
||||
IHashFunction* Crypto::CreateSHA1()
|
||||
{
|
||||
return new AlgorithmSHA1();
|
||||
}
|
||||
|
||||
IStreamCipher* Crypto::CreateSalsa20(const uint8_t* keyBytes, const size_t keySize)
|
||||
{
|
||||
return new AlgorithmSalsa20(keyBytes, keySize);
|
||||
}
|
||||
|
||||
IPublicKeyAlgorithm* Crypto::CreateRSA(const IPublicKeyAlgorithm::HashingAlgorithm hashingAlgorithm, const RSAPaddingMode paddingMode)
|
||||
{
|
||||
return new AlgorithmRSA(hashingAlgorithm, paddingMode);
|
||||
}
|
20
src/Crypto/Crypto.h
Normal file
20
src/Crypto/Crypto.h
Normal file
@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
#include "IHashFunction.h"
|
||||
#include "IStreamCipher.h"
|
||||
#include "IPublicKeyAlgorithm.h"
|
||||
|
||||
class Crypto
|
||||
{
|
||||
public:
|
||||
enum RSAPaddingMode
|
||||
{
|
||||
RSA_PADDING_PKS1,
|
||||
RSA_PADDING_PSS,
|
||||
};
|
||||
|
||||
static IHashFunction* CreateSHA1();
|
||||
|
||||
static IStreamCipher* CreateSalsa20(const uint8_t* keyBytes, size_t keySize);
|
||||
|
||||
static IPublicKeyAlgorithm* CreateRSA(IPublicKeyAlgorithm::HashingAlgorithm hashingAlgorithm, RSAPaddingMode paddingMode);
|
||||
};
|
204
src/Crypto/Crypto.vcxproj
Normal file
204
src/Crypto/Crypto.vcxproj
Normal file
@ -0,0 +1,204 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|Win32">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Crypto.h" />
|
||||
<ClInclude Include="IHashFunction.h" />
|
||||
<ClInclude Include="Impl\AlgorithmRSA.h" />
|
||||
<ClInclude Include="Impl\AlgorithmSalsa20.h" />
|
||||
<ClInclude Include="Impl\AlgorithmSHA1.h" />
|
||||
<ClInclude Include="Impl\CryptoLibrary.h" />
|
||||
<ClInclude Include="IPublicKeyAlgorithm.h" />
|
||||
<ClInclude Include="IStreamCipher.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="Crypto.cpp" />
|
||||
<ClCompile Include="Impl\AlgorithmRSA.cpp" />
|
||||
<ClCompile Include="Impl\AlgorithmSalsa20.cpp" />
|
||||
<ClCompile Include="Impl\AlgorithmSHA1.cpp" />
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<VCProjectVersion>15.0</VCProjectVersion>
|
||||
<ProjectGuid>{8D511FF0-0294-47EE-9AC4-BD9FE5DCC681}</ProjectGuid>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<RootNamespace>Crypto</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0.17763.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>StaticLibrary</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v141</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>StaticLibrary</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v141</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>StaticLibrary</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v141</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>StaticLibrary</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v141</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="Shared">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<LinkIncremental>true</LinkIncremental>
|
||||
<IntDir>$(SolutionDir)obj\$(ProjectName)\$(Configuration)_$(Platform)\</IntDir>
|
||||
<OutDir>$(SolutionDir)lib\$(Configuration)_$(Platform)\</OutDir>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<LinkIncremental>true</LinkIncremental>
|
||||
<IntDir>$(SolutionDir)obj\$(ProjectName)\$(Configuration)_$(Platform)\</IntDir>
|
||||
<OutDir>$(SolutionDir)lib\$(Configuration)_$(Platform)\</OutDir>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<LinkIncremental>false</LinkIncremental>
|
||||
<IntDir>$(SolutionDir)obj\$(ProjectName)\$(Configuration)_$(Platform)\</IntDir>
|
||||
<OutDir>$(SolutionDir)lib\$(Configuration)_$(Platform)\</OutDir>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<LinkIncremental>false</LinkIncremental>
|
||||
<IntDir>$(SolutionDir)obj\$(ProjectName)\$(Configuration)_$(Platform)\</IntDir>
|
||||
<OutDir>$(SolutionDir)lib\$(Configuration)_$(Platform)\</OutDir>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>Disabled</Optimization>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<AdditionalIncludeDirectories>$(ProjectDir);$(SolutionDir)thirdparty\libtomcrypt\headers;$(SolutionDir)thirdparty\salsa20;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<TreatSpecificWarningsAsErrors>4715;%(TreatSpecificWarningsAsErrors)</TreatSpecificWarningsAsErrors>
|
||||
<DisableSpecificWarnings>4221;%(DisableSpecificWarnings)</DisableSpecificWarnings>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Windows</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
<Lib>
|
||||
<AdditionalDependencies>libtomcrypt.lib;libtommath.lib;salsa20.lib</AdditionalDependencies>
|
||||
<AdditionalLibraryDirectories>$(SolutionDir)lib\$(Configuration)_$(Platform)\;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
<AdditionalOptions>/ignore:4221</AdditionalOptions>
|
||||
</Lib>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>Disabled</Optimization>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<AdditionalIncludeDirectories>$(ProjectDir);$(SolutionDir)thirdparty\libtomcrypt\headers;$(SolutionDir)thirdparty\salsa20;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<TreatSpecificWarningsAsErrors>4715;%(TreatSpecificWarningsAsErrors)</TreatSpecificWarningsAsErrors>
|
||||
<DisableSpecificWarnings>4221;%(DisableSpecificWarnings)</DisableSpecificWarnings>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Windows</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
<Lib>
|
||||
<AdditionalDependencies>libtomcrypt.lib;libtommath.lib;salsa20.lib</AdditionalDependencies>
|
||||
<AdditionalLibraryDirectories>$(SolutionDir)lib\$(Configuration)_$(Platform)\;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
<AdditionalOptions>/ignore:4221</AdditionalOptions>
|
||||
</Lib>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>MaxSpeed</Optimization>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<AdditionalIncludeDirectories>$(ProjectDir);$(SolutionDir)thirdparty\libtomcrypt\headers;$(SolutionDir)thirdparty\salsa20;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<TreatSpecificWarningsAsErrors>4715;%(TreatSpecificWarningsAsErrors)</TreatSpecificWarningsAsErrors>
|
||||
<DisableSpecificWarnings>4221;%(DisableSpecificWarnings)</DisableSpecificWarnings>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Windows</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
<Lib>
|
||||
<AdditionalDependencies>libtomcrypt.lib;libtommath.lib;salsa20.lib</AdditionalDependencies>
|
||||
<AdditionalLibraryDirectories>$(SolutionDir)lib\$(Configuration)_$(Platform)\;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
<AdditionalOptions>/ignore:4221</AdditionalOptions>
|
||||
</Lib>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>MaxSpeed</Optimization>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<AdditionalIncludeDirectories>$(ProjectDir);$(SolutionDir)thirdparty\libtomcrypt\headers;$(SolutionDir)thirdparty\salsa20;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<TreatSpecificWarningsAsErrors>4715;%(TreatSpecificWarningsAsErrors)</TreatSpecificWarningsAsErrors>
|
||||
<DisableSpecificWarnings>4221;%(DisableSpecificWarnings)</DisableSpecificWarnings>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Windows</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
<Lib>
|
||||
<AdditionalDependencies>libtomcrypt.lib;libtommath.lib;salsa20.lib</AdditionalDependencies>
|
||||
<AdditionalLibraryDirectories>$(SolutionDir)lib\$(Configuration)_$(Platform)\;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
<AdditionalOptions>/ignore:4221</AdditionalOptions>
|
||||
</Lib>
|
||||
</ItemDefinitionGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
12
src/Crypto/IHashFunction.h
Normal file
12
src/Crypto/IHashFunction.h
Normal file
@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
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;
|
||||
};
|
21
src/Crypto/IPublicKeyAlgorithm.h
Normal file
21
src/Crypto/IPublicKeyAlgorithm.h
Normal file
@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
class IPublicKeyAlgorithm
|
||||
{
|
||||
public:
|
||||
enum 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;
|
||||
};
|
12
src/Crypto/IStreamCipher.h
Normal file
12
src/Crypto/IStreamCipher.h
Normal file
@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
#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;
|
||||
};
|
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