diff --git a/Chaos/Cipher/Block/Decryptor.hpp b/Chaos/Cipher/Block/Decryptor.hpp new file mode 100644 index 0000000..ea90e7b --- /dev/null +++ b/Chaos/Cipher/Block/Decryptor.hpp @@ -0,0 +1,40 @@ +#ifndef CHAOS_CIPHER_BLOCK_DECRYPTOR_HPP +#define CHAOS_CIPHER_BLOCK_DECRYPTOR_HPP + +namespace Chaos::Cipher::Block +{ + +template +class Decryptor +{ +public: + template + void DecryptBlock(OutputIt out, InputIt inBegin, InputIt inEnd) + { + Impl().DecryptBlock(out, inBegin, inEnd); + } + + template + auto DecryptBlock(Block block) + { + return Impl().DecryptBlock(block); + } + +protected: + Decryptor() = default; + +private: + const T & Impl() const + { + return static_cast(*this); + } + + T & Impl() + { + return static_cast(*this); + } +}; + +} // namespace Chaos::Cipher::Block + +#endif // CHAOS_CIPHER_BLOCK_DECRYPTOR_HPP diff --git a/Chaos/Cipher/Des/DesCrypt.hpp b/Chaos/Cipher/Block/Des/DesCrypt.hpp similarity index 96% rename from Chaos/Cipher/Des/DesCrypt.hpp rename to Chaos/Cipher/Block/Des/DesCrypt.hpp index 2424d47..50b540d 100644 --- a/Chaos/Cipher/Des/DesCrypt.hpp +++ b/Chaos/Cipher/Block/Des/DesCrypt.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 #include @@ -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 { public: DesEncryptor(const Key & key) @@ -256,7 +259,7 @@ public: Inner_::KeySchedule Schedule_; }; - class DesDecryptor + class DesDecryptor : public Decryptor { 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 diff --git a/Chaos/Cipher/Block/Encryptor.hpp b/Chaos/Cipher/Block/Encryptor.hpp new file mode 100644 index 0000000..bf15e5c --- /dev/null +++ b/Chaos/Cipher/Block/Encryptor.hpp @@ -0,0 +1,40 @@ +#ifndef CHAOS_CIPHER_BLOCK_ENCRYPTOR_HPP +#define CHAOS_CIPHER_BLOCK_ENCRYPTOR_HPP + +namespace Chaos::Cipher::Block +{ + +template +class Encryptor +{ +public: + template + void EncryptBlock(OutputIt out, InputIt inBegin, InputIt inEnd) + { + Impl().EncryptBlock(out, inBegin, inEnd); + } + + template + auto EncryptBlock(Block block) + { + return Impl().EncryptBlock(block); + } + +protected: + Encryptor() = default; + +private: + const T & Impl() const + { + return static_cast(*this); + } + + T & Impl() + { + return static_cast(*this); + } +}; + +} // namespace Chaos::Cipher::Block + +#endif // CHAOS_CIPHER_BLOCK_ENCRYPTOR_HPP diff --git a/ChaosTests/Cipher/DesCryptTests.cpp b/ChaosTests/Cipher/DesCryptTests.cpp index dc431af..f7e4139 100644 --- a/ChaosTests/Cipher/DesCryptTests.cpp +++ b/ChaosTests/Cipher/DesCryptTests.cpp @@ -1,8 +1,10 @@ #include -#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 +static std::array EncryptThroughBase(Encryptor & enc, + InputIt begin, InputIt end) +{ + std::array result; + enc.EncryptBlock(result.begin(), begin, end); + return result; +} + +TEST(DesCryptTests, EncryptThroughBaseTest) +{ + std::array key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 }; + + std::array data = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef }; + std::array 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 +static uint64_t EncryptUInt64BlockThroughBase(Encryptor & enc, uint64_t block) +{ + return enc.EncryptBlock(block); +} + +TEST(DesCryptTests, EncryptUInt64BlockThroughBaseTest) +{ + std::array 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 +static std::array DecryptThroughBase(Decryptor & dec, + InputIt begin, InputIt end) +{ + std::array result; + dec.DecryptBlock(result.begin(), begin, end); + return result; +} + +TEST(DesCryptTests, DecryptThroughBaseTest) +{ + std::array key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 }; + + std::array data = { 0x85, 0xe8, 0x13, 0x54, 0x0f, 0x0a, 0xb4, 0x05 }; + std::array 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 +static uint64_t DecryptUInt64BlockThroughBase(Decryptor & dec, uint64_t block) +{ + return dec.DecryptBlock(block); +} + +TEST(DesCryptTests, DecryptUInt64BlockThroughBaseTest) +{ + std::array 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)); +}