Compare commits

..

5 Commits

Author SHA1 Message Date
hashlag
15dd7398d2 Implement DES decryption
All checks were successful
ChaosTest CI / build-and-test (push) Successful in 41s
2026-01-23 22:22:28 +03:00
hashlag
5fd92e0c9d Fix long statement formatting 2026-01-23 21:32:57 +03:00
hashlag
da5eaff182 Introduce stateful DesCrypt::Encryptor class, remove static DesCrypt::EncryptBlock() 2026-01-23 21:31:04 +03:00
hashlag
3a2a665031 Fix a layering issue: private EncryptBlock() should use a private key representation 2026-01-22 22:36:14 +03:00
hashlag
fa042e7abf Rename RawKeyArray --> RawKey since it's not an 'array of raw keys' 2026-01-21 23:36:15 +03:00
2 changed files with 260 additions and 24 deletions

View File

@@ -1,6 +1,7 @@
#ifndef CHAOS_CIPHER_DES_DESCRYPT_HPP #ifndef CHAOS_CIPHER_DES_DESCRYPT_HPP
#define CHAOS_CIPHER_DES_DESCRYPT_HPP #define CHAOS_CIPHER_DES_DESCRYPT_HPP
#include <algorithm>
#include <utility> #include <utility>
#include "Service/ChaosException.hpp" #include "Service/ChaosException.hpp"
@@ -88,7 +89,7 @@ struct Bitwise
} }
}; };
using RawKeyArray = Service::SeArray<uint8_t, 8>; using RawKey = Service::SeArray<uint8_t, 8>;
class KeySchedule class KeySchedule
{ {
@@ -98,9 +99,15 @@ public:
using RoundKey48 = uint64_t; using RoundKey48 = uint64_t;
KeySchedule(const RawKeyArray & rawKeyArray) enum class Direction
{ {
Key56 key56 = Pc1(Bitwise::PackUInt64(rawKeyArray.Begin(), rawKeyArray.End())); Encrypt,
Decrypt
};
KeySchedule(Direction direction, const RawKey & rawKey)
{
Key56 key56 = Pc1(Bitwise::PackUInt64(rawKey.Begin(), rawKey.End()));
auto [c28, d28] = Bitwise::Split<28>(key56); auto [c28, d28] = Bitwise::Split<28>(key56);
@@ -119,6 +126,11 @@ public:
Schedule_[i] = Pc2(Bitwise::Merge<28>(c28, d28)); Schedule_[i] = Pc2(Bitwise::Merge<28>(c28, d28));
} }
if (direction == Direction::Decrypt)
{
std::reverse(Schedule_.Begin(), Schedule_.End());
}
} }
RoundKey48 operator[](int_fast8_t i) const RoundKey48 operator[](int_fast8_t i) const
@@ -204,24 +216,68 @@ public:
} }
private: private:
Inner_::RawKeyArray Key_; Inner_::RawKey Key_;
}; };
template<typename OutputIt, typename InputIt> class Encryptor
static void EncryptBlock(OutputIt out, InputIt inBegin, InputIt inEnd, const Key & key)
{ {
RawBlockArray block; public:
Encryptor(const Key & key)
: Schedule_(Inner_::KeySchedule::Direction::Encrypt, key.Key_)
{ }
int_fast8_t i = 0; template<typename OutputIt, typename InputIt>
for (InputIt in = inBegin; i < block.Size() && in != inEnd; ++i, ++in) void EncryptBlock(OutputIt out, InputIt inBegin, InputIt inEnd)
{ {
block[i] = *in; RawBlockArray block;
int_fast8_t i = 0;
for (InputIt in = inBegin; i < block.Size() && in != inEnd; ++i, ++in)
{
block[i] = *in;
}
Block encrypted
= DesCrypt::ProcessBlock(Inner_::Bitwise::PackUInt64(block.Begin(),
block.End()),
Schedule_);
Inner_::Bitwise::CrunchUInt64(out, encrypted);
} }
Block encrypted = EncryptBlock(Inner_::Bitwise::PackUInt64(block.Begin(), block.End()), key); private:
Inner_::KeySchedule Schedule_;
};
Inner_::Bitwise::CrunchUInt64(out, encrypted); class Decryptor
} {
public:
Decryptor(const Key & key)
: Schedule_(Inner_::KeySchedule::Direction::Decrypt, key.Key_)
{ }
template<typename OutputIt, typename InputIt>
void DecryptBlock(OutputIt out, InputIt inBegin, InputIt inEnd)
{
RawBlockArray block;
int_fast8_t i = 0;
for (InputIt in = inBegin; i < block.Size() && in != inEnd; ++i, ++in)
{
block[i] = *in;
}
Block decrypted
= DesCrypt::ProcessBlock(Inner_::Bitwise::PackUInt64(block.Begin(),
block.End()),
Schedule_);
Inner_::Bitwise::CrunchUInt64(out, decrypted);
}
private:
Inner_::KeySchedule Schedule_;
};
private: private:
using Block = uint64_t; using Block = uint64_t;
@@ -391,10 +447,8 @@ private:
FP_TABLE + std::size(FP_TABLE)); FP_TABLE + std::size(FP_TABLE));
} }
static Block EncryptBlock(Block block, const Key & key) static Block ProcessBlock(Block block, const Inner_::KeySchedule & schedule)
{ {
Inner_::KeySchedule schedule(key.Key_);
block = Ip(block); block = Ip(block);
uint32_t l32; uint32_t l32;

View File

@@ -6,7 +6,7 @@ using namespace Chaos::Cipher::Des;
TEST(DesCryptTests, KeyScheduleTest) TEST(DesCryptTests, KeyScheduleTest)
{ {
Inner_::RawKeyArray key; Inner_::RawKey key;
key[0] = 0b00010011; key[0] = 0b00010011;
key[1] = 0b00110100; key[1] = 0b00110100;
@@ -17,7 +17,7 @@ TEST(DesCryptTests, KeyScheduleTest)
key[6] = 0b11011111; key[6] = 0b11011111;
key[7] = 0b11110001; key[7] = 0b11110001;
Inner_::KeySchedule schedule(key); Inner_::KeySchedule schedule(Inner_::KeySchedule::Direction::Encrypt, key);
ASSERT_EQ(0b000110110000001011101111111111000111000001110010ULL, schedule[0]); ASSERT_EQ(0b000110110000001011101111111111000111000001110010ULL, schedule[0]);
ASSERT_EQ(0b011110011010111011011001110110111100100111100101ULL, schedule[1]); ASSERT_EQ(0b011110011010111011011001110110111100100111100101ULL, schedule[1]);
@@ -48,7 +48,8 @@ TEST(DesCryptTests, EncryptTest)
result.fill(0); result.fill(0);
DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::Key desKey(key.begin(), key.end());
DesCrypt::EncryptBlock(result.begin(), data.begin(), data.end(), desKey); DesCrypt::Encryptor enc(desKey);
enc.EncryptBlock(result.begin(), data.begin(), data.end());
return result; return result;
} }
@@ -95,7 +96,8 @@ TEST(DesCryptTests, EncryptShortDataTest)
result.resize(8, 0); result.resize(8, 0);
DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::Key desKey(key.begin(), key.end());
DesCrypt::EncryptBlock(result.begin(), data.begin(), data.end(), desKey); DesCrypt::Encryptor enc(desKey);
enc.EncryptBlock(result.begin(), data.begin(), data.end());
return result; return result;
} }
@@ -128,7 +130,8 @@ TEST(DesCryptTests, EncryptLongDataTest)
result.resize(8, 0); result.resize(8, 0);
DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::Key desKey(key.begin(), key.end());
DesCrypt::EncryptBlock(result.begin(), data.begin(), data.end(), desKey); DesCrypt::Encryptor enc(desKey);
enc.EncryptBlock(result.begin(), data.begin(), data.end());
return result; return result;
} }
@@ -150,6 +153,122 @@ TEST(DesCryptTests, EncryptLongDataTest)
} }
} }
TEST(DesCryptTests, DecryptTest)
{
struct Helper
{
std::array<uint8_t, 8> operator()(const std::array<uint8_t, 8> & data,
const std::array<uint8_t, 8> & key) const
{
std::array<uint8_t, 8> result;
result.fill(0);
DesCrypt::Key desKey(key.begin(), key.end());
DesCrypt::Decryptor dec(desKey);
dec.DecryptBlock(result.begin(), data.begin(), data.end());
return result;
}
};
Helper des;
{
std::array<uint8_t, 8> data = { 0x85, 0xe8, 0x13, 0x54, 0x0f, 0x0a, 0xb4, 0x05 };
std::array<uint8_t, 8> key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 };
std::array<uint8_t, 8> expected = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
ASSERT_EQ(expected, des(data, key));
}
{
std::array<uint8_t, 8> data = { 0x07, 0xe8, 0x7f, 0xaa, 0xb3, 0x17, 0x13, 0x18 };
std::array<uint8_t, 8> key = { 0x44, 0xbf, 0x32, 0x19, 0x99, 0x25, 0x81, 0x51 };
std::array<uint8_t, 8> expected = { 0xaa, 0xf3, 0x83, 0x16, 0x2d, 0x2e, 0x6b, 0xcb };
ASSERT_EQ(expected, des(data, key));
}
{
std::array<uint8_t, 8> data = { 0x42, 0x27, 0x88, 0xa6, 0x7b, 0x6c, 0x18, 0xed };
std::array<uint8_t, 8> key = { 0xda, 0xec, 0x68, 0xae, 0x83, 0xe0, 0x1e, 0xab };
std::array<uint8_t, 8> expected = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44 };
ASSERT_EQ(expected, des(data, key));
}
}
TEST(DesCryptTests, DecryptShortDataTest)
{
struct Helper
{
std::vector<uint8_t> operator()(const std::vector<uint8_t> & data,
const std::vector<uint8_t> & key)
{
std::vector<uint8_t> result;
result.resize(8, 0);
DesCrypt::Key desKey(key.begin(), key.end());
DesCrypt::Decryptor dec(desKey);
dec.DecryptBlock(result.begin(), data.begin(), data.end());
return result;
}
};
Helper des;
{
// treated as { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0x00, 0x00, 0x00 }
std::vector<uint8_t> dataShort = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19 };
std::vector<uint8_t> data = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0x00, 0x00, 0x00 };
std::vector<uint8_t> key = { 0xda, 0xec, 0x68, 0xae, 0x83, 0xe0, 0x1e, 0xab };
std::vector<uint8_t> expected = { 0x90, 0xe5, 0xf6, 0x5b, 0xe1, 0xd3, 0x8a, 0x64 };
ASSERT_EQ(expected, des(data, key));
ASSERT_EQ(expected, des(dataShort, key));
}
}
TEST(DesCryptTests, DecryptLongDataTest)
{
struct Helper
{
std::vector<uint8_t> operator()(const std::vector<uint8_t> & data,
const std::vector<uint8_t> & key)
{
std::vector<uint8_t> result;
result.resize(8, 0);
DesCrypt::Key desKey(key.begin(), key.end());
DesCrypt::Decryptor dec(desKey);
dec.DecryptBlock(result.begin(), data.begin(), data.end());
return result;
}
};
Helper des;
{
// treated as { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44 }
std::vector<uint8_t> dataLong = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44, 0xaa, 0xbb };
std::vector<uint8_t> data = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44 };
std::vector<uint8_t> key = { 0xda, 0xec, 0x68, 0xae, 0x83, 0xe0, 0x1e, 0xab };
std::vector<uint8_t> expected = { 0x45, 0x69, 0x71, 0x17, 0x13, 0xfb, 0x3e, 0xee };
ASSERT_EQ(expected, des(data, key));
ASSERT_EQ(expected, des(dataLong, key));
}
}
TEST(DesCryptTests, ShortKeyTest) TEST(DesCryptTests, ShortKeyTest)
{ {
{ {
@@ -166,7 +285,7 @@ TEST(DesCryptTests, LongKeyTest)
} }
} }
TEST(DesCryptTests, OutIteratorUsageTest) TEST(DesCryptTests, OutIteratorUsageEncryptTest)
{ {
struct OutputItMock struct OutputItMock
{ {
@@ -203,7 +322,8 @@ TEST(DesCryptTests, OutIteratorUsageTest)
OutputItMock it(asteriskCalls, incrementCalls); OutputItMock it(asteriskCalls, incrementCalls);
DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::Key desKey(key.begin(), key.end());
DesCrypt::EncryptBlock(it, data.begin(), data.end(), desKey); DesCrypt::Encryptor enc(desKey);
enc.EncryptBlock(it, data.begin(), data.end());
ASSERT_EQ(8, asteriskCalls); ASSERT_EQ(8, asteriskCalls);
ASSERT_EQ(8, incrementCalls); ASSERT_EQ(8, incrementCalls);
@@ -218,7 +338,69 @@ TEST(DesCryptTests, OutIteratorUsageTest)
OutputItMock it(asteriskCalls, incrementCalls); OutputItMock it(asteriskCalls, incrementCalls);
DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::Key desKey(key.begin(), key.end());
DesCrypt::EncryptBlock(it, data.begin(), data.end(), desKey); DesCrypt::Encryptor enc(desKey);
enc.EncryptBlock(it, data.begin(), data.end());
ASSERT_EQ(8, asteriskCalls);
ASSERT_EQ(8, incrementCalls);
}
}
TEST(DesCryptTests, OutIteratorUsageDecryptTest)
{
struct OutputItMock
{
OutputItMock(size_t & asteriskCalls, size_t & incrementCalls)
: AsteriskCalls_(asteriskCalls)
, IncrementCalls_(incrementCalls)
{ }
uint8_t & operator*()
{
++AsteriskCalls_;
static uint8_t dummy = 0;
return dummy;
}
OutputItMock operator++(int)
{
++IncrementCalls_;
return *this;
}
size_t & AsteriskCalls_;
size_t & IncrementCalls_;
};
{
std::array<uint8_t, 8> data = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44 };
std::array<uint8_t, 8> key = { 0xda, 0xec, 0x68, 0xae, 0x83, 0xe0, 0x1e, 0xab };
size_t asteriskCalls = 0;
size_t incrementCalls = 0;
OutputItMock it(asteriskCalls, incrementCalls);
DesCrypt::Key desKey(key.begin(), key.end());
DesCrypt::Decryptor dec(desKey);
dec.DecryptBlock(it, data.begin(), data.end());
ASSERT_EQ(8, asteriskCalls);
ASSERT_EQ(8, incrementCalls);
}
{
std::array<uint8_t, 11> data = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f };
std::array<uint8_t, 8> key = { 0xda, 0xec, 0x68, 0xae, 0x83, 0xe0, 0x1e, 0xab };
size_t asteriskCalls = 0;
size_t incrementCalls = 0;
OutputItMock it(asteriskCalls, incrementCalls);
DesCrypt::Key desKey(key.begin(), key.end());
DesCrypt::Decryptor dec(desKey);
dec.DecryptBlock(it, data.begin(), data.end());
ASSERT_EQ(8, asteriskCalls); ASSERT_EQ(8, asteriskCalls);
ASSERT_EQ(8, incrementCalls); ASSERT_EQ(8, incrementCalls);