Merge branch 'block-cipher'
All checks were successful
Chaos Ci / test-and-benchmark (push) Successful in 1m43s
All checks were successful
Chaos Ci / test-and-benchmark (push) Successful in 1m43s
Introduce fixed CRTP interfaces for block cipher encryption and decryption.
This commit is contained in:
40
Chaos/Cipher/Block/Decryptor.hpp
Normal file
40
Chaos/Cipher/Block/Decryptor.hpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#ifndef CHAOS_CIPHER_BLOCK_DECRYPTOR_HPP
|
||||
#define CHAOS_CIPHER_BLOCK_DECRYPTOR_HPP
|
||||
|
||||
namespace Chaos::Cipher::Block
|
||||
{
|
||||
|
||||
template<typename T>
|
||||
class Decryptor
|
||||
{
|
||||
public:
|
||||
template<typename OutputIt, typename InputIt>
|
||||
void DecryptBlock(OutputIt out, InputIt inBegin, InputIt inEnd)
|
||||
{
|
||||
Impl().DecryptBlock(out, inBegin, inEnd);
|
||||
}
|
||||
|
||||
template<typename Block>
|
||||
auto DecryptBlock(Block block)
|
||||
{
|
||||
return Impl().DecryptBlock(block);
|
||||
}
|
||||
|
||||
protected:
|
||||
Decryptor() = default;
|
||||
|
||||
private:
|
||||
const T & Impl() const
|
||||
{
|
||||
return static_cast<const T &>(*this);
|
||||
}
|
||||
|
||||
T & Impl()
|
||||
{
|
||||
return static_cast<T &>(*this);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace Chaos::Cipher::Block
|
||||
|
||||
#endif // CHAOS_CIPHER_BLOCK_DECRYPTOR_HPP
|
||||
@@ -1,5 +1,5 @@
|
||||
#ifndef CHAOS_CIPHER_DES_DESCRYPT_HPP
|
||||
#define CHAOS_CIPHER_DES_DESCRYPT_HPP
|
||||
#ifndef CHAOS_CIPHER_BLOCK_DES_DESCRYPT_HPP
|
||||
#define CHAOS_CIPHER_BLOCK_DES_DESCRYPT_HPP
|
||||
|
||||
#include <algorithm>
|
||||
#include <utility>
|
||||
@@ -7,7 +7,10 @@
|
||||
#include "Service/ChaosException.hpp"
|
||||
#include "Service/SeArray.hpp"
|
||||
|
||||
namespace Chaos::Cipher::Des::Inner_
|
||||
#include "Cipher/Block/Encryptor.hpp"
|
||||
#include "Cipher/Block/Decryptor.hpp"
|
||||
|
||||
namespace Chaos::Cipher::Block::Des::Inner_
|
||||
{
|
||||
|
||||
struct Bitwise
|
||||
@@ -184,9 +187,9 @@ private:
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace Chaos::Cipher::Des::Inner_
|
||||
} // namespace Chaos::Cipher::Block::Des::Inner_
|
||||
|
||||
namespace Chaos::Cipher::Des
|
||||
namespace Chaos::Cipher::Block::Des
|
||||
{
|
||||
|
||||
class DesCrypt
|
||||
@@ -221,7 +224,7 @@ public:
|
||||
Inner_::RawKey Key_;
|
||||
};
|
||||
|
||||
class DesEncryptor
|
||||
class DesEncryptor : public Encryptor<DesEncryptor>
|
||||
{
|
||||
public:
|
||||
DesEncryptor(const Key & key)
|
||||
@@ -256,7 +259,7 @@ public:
|
||||
Inner_::KeySchedule Schedule_;
|
||||
};
|
||||
|
||||
class DesDecryptor
|
||||
class DesDecryptor : public Decryptor<DesDecryptor>
|
||||
{
|
||||
public:
|
||||
DesDecryptor(const Key & key)
|
||||
@@ -483,6 +486,6 @@ private:
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace Chaos::Cipher::Des
|
||||
} // namespace Chaos::Cipher::Block::Des
|
||||
|
||||
#endif // CHAOS_CIPHER_DES_DESCRYPT_HPP
|
||||
#endif // CHAOS_CIPHER_BLOCK_DES_DESCRYPT_HPP
|
||||
40
Chaos/Cipher/Block/Encryptor.hpp
Normal file
40
Chaos/Cipher/Block/Encryptor.hpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#ifndef CHAOS_CIPHER_BLOCK_ENCRYPTOR_HPP
|
||||
#define CHAOS_CIPHER_BLOCK_ENCRYPTOR_HPP
|
||||
|
||||
namespace Chaos::Cipher::Block
|
||||
{
|
||||
|
||||
template<typename T>
|
||||
class Encryptor
|
||||
{
|
||||
public:
|
||||
template<typename OutputIt, typename InputIt>
|
||||
void EncryptBlock(OutputIt out, InputIt inBegin, InputIt inEnd)
|
||||
{
|
||||
Impl().EncryptBlock(out, inBegin, inEnd);
|
||||
}
|
||||
|
||||
template<typename Block>
|
||||
auto EncryptBlock(Block block)
|
||||
{
|
||||
return Impl().EncryptBlock(block);
|
||||
}
|
||||
|
||||
protected:
|
||||
Encryptor() = default;
|
||||
|
||||
private:
|
||||
const T & Impl() const
|
||||
{
|
||||
return static_cast<const T &>(*this);
|
||||
}
|
||||
|
||||
T & Impl()
|
||||
{
|
||||
return static_cast<T &>(*this);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace Chaos::Cipher::Block
|
||||
|
||||
#endif // CHAOS_CIPHER_BLOCK_ENCRYPTOR_HPP
|
||||
@@ -1,8 +1,10 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "Cipher/Des/DesCrypt.hpp"
|
||||
#include "Cipher/Block/Des/DesCrypt.hpp"
|
||||
#include "Cipher/Block/Encryptor.hpp"
|
||||
|
||||
using namespace Chaos::Cipher::Des;
|
||||
using namespace Chaos::Cipher::Block::Des;
|
||||
using namespace Chaos::Cipher::Block;
|
||||
|
||||
TEST(DesCryptTests, KeyScheduleTest)
|
||||
{
|
||||
@@ -494,3 +496,85 @@ TEST(DesCryptTests, OutIteratorUsageDecryptTest)
|
||||
ASSERT_EQ(8, incrementCalls);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Impl, typename InputIt>
|
||||
static std::array<uint8_t, 8> EncryptThroughBase(Encryptor<Impl> & enc,
|
||||
InputIt begin, InputIt end)
|
||||
{
|
||||
std::array<uint8_t, 8> result;
|
||||
enc.EncryptBlock(result.begin(), begin, end);
|
||||
return result;
|
||||
}
|
||||
|
||||
TEST(DesCryptTests, EncryptThroughBaseTest)
|
||||
{
|
||||
std::array<uint8_t, 8> key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 };
|
||||
|
||||
std::array<uint8_t, 8> data = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
|
||||
std::array<uint8_t, 8> expected = { 0x85, 0xe8, 0x13, 0x54, 0x0f, 0x0a, 0xb4, 0x05 };
|
||||
|
||||
DesCrypt::Key desKey(key.begin(), key.end());
|
||||
DesCrypt::DesEncryptor enc(desKey);
|
||||
|
||||
ASSERT_EQ(expected, EncryptThroughBase(enc, data.begin(), data.end()));
|
||||
}
|
||||
|
||||
template<typename Impl>
|
||||
static uint64_t EncryptUInt64BlockThroughBase(Encryptor<Impl> & enc, uint64_t block)
|
||||
{
|
||||
return enc.EncryptBlock(block);
|
||||
}
|
||||
|
||||
TEST(DesCryptTests, EncryptUInt64BlockThroughBaseTest)
|
||||
{
|
||||
std::array<uint8_t, 8> key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 };
|
||||
|
||||
uint64_t data = 0x0123456789abcdef;
|
||||
uint64_t expected = 0x85e813540f0ab405;
|
||||
|
||||
DesCrypt::Key desKey(key.begin(), key.end());
|
||||
DesCrypt::DesEncryptor enc(desKey);
|
||||
|
||||
ASSERT_EQ(expected, EncryptUInt64BlockThroughBase(enc, data));
|
||||
}
|
||||
|
||||
template<typename Impl, typename InputIt>
|
||||
static std::array<uint8_t, 8> DecryptThroughBase(Decryptor<Impl> & dec,
|
||||
InputIt begin, InputIt end)
|
||||
{
|
||||
std::array<uint8_t, 8> result;
|
||||
dec.DecryptBlock(result.begin(), begin, end);
|
||||
return result;
|
||||
}
|
||||
|
||||
TEST(DesCryptTests, DecryptThroughBaseTest)
|
||||
{
|
||||
std::array<uint8_t, 8> key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 };
|
||||
|
||||
std::array<uint8_t, 8> data = { 0x85, 0xe8, 0x13, 0x54, 0x0f, 0x0a, 0xb4, 0x05 };
|
||||
std::array<uint8_t, 8> expected = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
|
||||
|
||||
DesCrypt::Key desKey(key.begin(), key.end());
|
||||
DesCrypt::DesDecryptor dec(desKey);
|
||||
|
||||
ASSERT_EQ(expected, DecryptThroughBase(dec, data.begin(), data.end()));
|
||||
}
|
||||
|
||||
template<typename Impl>
|
||||
static uint64_t DecryptUInt64BlockThroughBase(Decryptor<Impl> & dec, uint64_t block)
|
||||
{
|
||||
return dec.DecryptBlock(block);
|
||||
}
|
||||
|
||||
TEST(DesCryptTests, DecryptUInt64BlockThroughBaseTest)
|
||||
{
|
||||
std::array<uint8_t, 8> key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 };
|
||||
|
||||
uint64_t data = 0x85e813540f0ab405;
|
||||
uint64_t expected = 0x0123456789abcdef;
|
||||
|
||||
DesCrypt::Key desKey(key.begin(), key.end());
|
||||
DesCrypt::DesDecryptor dec(desKey);
|
||||
|
||||
ASSERT_EQ(expected, DecryptUInt64BlockThroughBase(dec, data));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user