Compare commits

...

4 Commits

Author SHA1 Message Date
hashlag
9e67a7e4f4 Add DES main algorithm draft implementation
All checks were successful
ChaosTest CI / build-and-test (push) Successful in 38s
2026-01-19 02:30:06 +03:00
hashlag
d63855a505 Add Bitwise::PackUInt64(<...>) utility function 2026-01-15 03:14:45 +03:00
hashlag
51c2da1e48 Explicitly #include <utility> in DesCrypt.hpp (std::pair<> used) 2026-01-14 01:33:35 +03:00
hashlag
5e35462667 Add DES key schedule pre-draft implementation 2026-01-14 01:31:26 +03:00
3 changed files with 650 additions and 0 deletions

View File

@@ -0,0 +1,423 @@
#ifndef CHAOS_CIPHER_DES_DESCRYPT_HPP
#define CHAOS_CIPHER_DES_DESCRYPT_HPP
#include <utility>
#include "Service/ChaosException.hpp"
#include "Service/SeArray.hpp"
namespace Chaos::Cipher::Des::Inner_
{
struct Bitwise
{
template<uint8_t BitsUsed>
static uint8_t GetBit(uint64_t value, int_fast8_t bitNumber)
{
return (value >> (BitsUsed - bitNumber)) & 0b1;
}
template<uint8_t BitsUsed>
static void SetBit(uint64_t & value, int_fast8_t bitNumber)
{
value |= (static_cast<uint64_t>(0b1) << (BitsUsed - bitNumber));
}
template<uint8_t BitsUsedIn, uint8_t BitsUsedOut, typename InputIt>
static uint64_t TableChoice(uint64_t value, InputIt tableBegin, InputIt tableEnd)
{
uint64_t result = 0;
int_fast8_t i = 1;
for (InputIt it = tableBegin; it != tableEnd; ++it, ++i)
{
if (GetBit<BitsUsedIn>(value, *it))
{
SetBit<BitsUsedOut>(result, i);
}
}
return result;
}
template<uint8_t Bits>
static constexpr uint64_t Mask()
{
return (static_cast<uint64_t>(0b1) << Bits) - static_cast<uint64_t>(0b1);
}
template<uint8_t BitsUsed>
static void Rotl(uint64_t & value, int_fast8_t shift)
{
value = ((value << shift) | (value >> (BitsUsed - shift))) & Mask<BitsUsed>();
}
template<uint8_t BitsUsedOut>
static std::pair<uint64_t, uint64_t> Split(uint64_t value)
{
return { value >> BitsUsedOut, value & Mask<BitsUsedOut>() };
}
template<uint8_t BitsUsedIn>
static uint64_t Merge(uint64_t lhs, uint64_t rhs)
{
return (lhs << BitsUsedIn) | rhs;
}
template<typename InputIt>
static uint64_t PackUInt64(InputIt begin, InputIt end)
{
uint64_t result = 0;
int_fast8_t i = 0;
for (InputIt it = begin; i < 8 && it != end; ++i, ++it)
{
result |= static_cast<uint64_t>(*it) << (56 - (i * 8));
}
return result;
}
template<typename OutputIt>
static void CrunchUInt64(OutputIt out, uint64_t value)
{
for (int_fast8_t i = 0; i < 8; ++i)
{
*out++ = (value >> (56 - (i * 8))) & Mask<8>();
}
}
};
using RawKeyArray = Service::SeArray<uint8_t, 8>;
class KeySchedule
{
public:
using Key64 = uint64_t;
using Key56 = uint64_t;
using RoundKey48 = uint64_t;
KeySchedule(const RawKeyArray & rawKeyArray)
{
Key56 key56 = Pc1(Bitwise::PackUInt64(rawKeyArray.Begin(), rawKeyArray.End()));
auto [c28, d28] = Bitwise::Split<28>(key56);
for (int_fast8_t i = 0; i < Schedule_.Size(); ++i)
{
if (i == 0 || i == 1 || i == 8 || i == 15)
{
Bitwise::Rotl<28>(c28, 1);
Bitwise::Rotl<28>(d28, 1);
}
else
{
Bitwise::Rotl<28>(c28, 2);
Bitwise::Rotl<28>(d28, 2);
}
Schedule_[i] = Pc2(Bitwise::Merge<28>(c28, d28));
}
}
RoundKey48 operator[](int_fast8_t i) const
{
return Schedule_[i];
}
private:
Service::SeArray<RoundKey48, 16> Schedule_;
static Key56 Pc1(Key64 key)
{
constexpr int_fast8_t PC1_TABLE[] =
{
57, 49, 41, 33, 25, 17, 9,
1, 58, 50, 42, 34, 26, 18,
10, 2, 59, 51, 43, 35, 27,
19, 11, 3, 60, 52, 44, 36,
63, 55, 47, 39, 31, 23, 15,
7, 62, 54, 46, 38, 30, 22,
14, 6, 61, 53, 45, 37, 29,
21, 13, 5, 28, 20, 12, 4
};
static_assert(std::size(PC1_TABLE) == 56);
return Bitwise::TableChoice<64, 56>(key,
PC1_TABLE,
PC1_TABLE + std::size(PC1_TABLE));
}
static RoundKey48 Pc2(Key56 key)
{
constexpr int_fast8_t PC2_TABLE[] =
{
14, 17, 11, 24, 1, 5,
3, 28, 15, 6, 21, 10,
23, 19, 12, 4, 26, 8,
16, 7, 27, 20, 13, 2,
41, 52, 31, 37, 47, 55,
30, 40, 51, 45, 33, 48,
44, 49, 39, 56, 34, 53,
46, 42, 50, 36, 29, 32
};
static_assert(std::size(PC2_TABLE) == 48);
return Bitwise::TableChoice<56, 48>(key,
PC2_TABLE,
PC2_TABLE + std::size(PC2_TABLE));
}
};
} // namespace Chaos::Cipher::Des::Inner_
namespace Chaos::Cipher::Des
{
class DesCrypt
{
public:
DesCrypt() = delete;
class Key
{
friend class DesCrypt;
public:
template<typename InputIt>
Key(InputIt keyBegin, InputIt keyEnd)
{
int_fast8_t i = 0;
InputIt keyIt = keyBegin;
for (; i < Key_.Size() && keyIt != keyEnd; ++i, ++keyIt)
{
Key_[i] = *keyIt;
}
if (i != Key_.Size() || keyIt != keyEnd)
{
throw Service::ChaosException("DesCrypt::Key: invalid key length "
"(8 bytes required)");
}
}
private:
Inner_::RawKeyArray Key_;
};
template<typename OutputIt, typename InputIt>
static void EncryptBlock(OutputIt out, InputIt inBegin, InputIt inEnd, const Key & key)
{
RawBlockArray block;
int_fast8_t i = 0;
for (InputIt in = inBegin; i < block.Size() && in != inEnd; ++i, ++in)
{
block[i] = *in;
}
Block encrypted = EncryptBlock(Inner_::Bitwise::PackUInt64(block.Begin(), block.End()), key);
Inner_::Bitwise::CrunchUInt64(out, encrypted);
}
private:
using Block = uint64_t;
using BlockHalf = uint32_t;
using RawBlockArray = Service::SeArray<uint8_t, 8>;
using Data48 = uint64_t;
using Data32 = uint32_t;
using Data6 = uint8_t;
using Data4 = uint8_t;
static Data48 E(Data32 value)
{
constexpr int_fast8_t E_TABLE[] =
{
32, 1, 2, 3, 4, 5,
4, 5, 6, 7, 8, 9,
8, 9, 10, 11, 12, 13,
12, 13, 14, 15, 16, 17,
16, 17, 18, 19, 20, 21,
20, 21, 22, 23, 24, 25,
24, 25, 26, 27, 28, 29,
28, 29, 30, 31, 32, 1
};
static_assert(std::size(E_TABLE) == 48);
return Inner_::Bitwise::TableChoice<32, 48>(value,
E_TABLE,
E_TABLE + std::size(E_TABLE));
}
static Data32 SBlock(Data48 value)
{
constexpr Data4 SBOX_TABLES[][64] =
{
{
14, 0, 4, 15, 13, 7, 1, 4, 2, 14, 15, 2, 11, 13, 8, 1,
3, 10, 10, 6, 6, 12, 12, 11, 5, 9, 9, 5, 0, 3, 7, 8,
4, 15, 1, 12, 14, 8, 8, 2, 13, 4, 6, 9, 2, 1, 11, 7,
15, 5, 12, 11, 9, 3, 7, 14, 3, 10, 10, 0, 5, 6, 0, 13
},
{
15, 3, 1, 13, 8, 4, 14, 7, 6, 15, 11, 2, 3, 8, 4, 14,
9, 12, 7, 0, 2, 1, 13, 10, 12, 6, 0, 9, 5, 11, 10, 5,
0, 13, 14, 8, 7, 10, 11, 1, 10, 3, 4, 15, 13, 4, 1, 2,
5, 11, 8, 6, 12, 7, 6, 12, 9, 0, 3, 5, 2, 14, 15, 9
},
{
10, 13, 0, 7, 9, 0, 14, 9, 6, 3, 3, 4, 15, 6, 5, 10,
1, 2, 13, 8, 12, 5, 7, 14, 11, 12, 4, 11, 2, 15, 8, 1,
13, 1, 6, 10, 4, 13, 9, 0, 8, 6, 15, 9, 3, 8, 0, 7,
11, 4, 1, 15, 2, 14, 12, 3, 5, 11, 10, 5, 14, 2, 7, 12
},
{
7, 13, 13, 8, 14, 11, 3, 5, 0, 6, 6, 15, 9, 0, 10, 3,
1, 4, 2, 7, 8, 2, 5, 12, 11, 1, 12, 10, 4, 14, 15, 9,
10, 3, 6, 15, 9, 0, 0, 6, 12, 10, 11, 1, 7, 13, 13, 8,
15, 9, 1, 4, 3, 5, 14, 11, 5, 12, 2, 7, 8, 2, 4, 14
},
{
2, 14, 12, 11, 4, 2, 1, 12, 7, 4, 10, 7, 11, 13, 6, 1,
8, 5, 5, 0, 3, 15, 15, 10, 13, 3, 0, 9, 14, 8, 9, 6,
4, 11, 2, 8, 1, 12, 11, 7, 10, 1, 13, 14, 7, 2, 8, 13,
15, 6, 9, 15, 12, 0, 5, 9, 6, 10, 3, 4, 0, 5, 14, 3
},
{
12, 10, 1, 15, 10, 4, 15, 2, 9, 7, 2, 12, 6, 9, 8, 5,
0, 6, 13, 1, 3, 13, 4, 14, 14, 0, 7, 11, 5, 3, 11, 8,
9, 4, 14, 3, 15, 2, 5, 12, 2, 9, 8, 5, 12, 15, 3, 10,
7, 11, 0, 14, 4, 1, 10, 7, 1, 6, 13, 0, 11, 8, 6, 13
},
{
4, 13, 11, 0, 2, 11, 14, 7, 15, 4, 0, 9, 8, 1, 13, 10,
3, 14, 12, 3, 9, 5, 7, 12, 5, 2, 10, 15, 6, 8, 1, 6,
1, 6, 4, 11, 11, 13, 13, 8, 12, 1, 3, 4, 7, 10, 14, 7,
10, 9, 15, 5, 6, 0, 8, 15, 0, 14, 5, 2, 9, 3, 2, 12
},
{
13, 1, 2, 15, 8, 13, 4, 8, 6, 10, 15, 3, 11, 7, 1, 4,
10, 12, 9, 5, 3, 6, 14, 11, 5, 0, 0, 14, 12, 9, 7, 2,
7, 2, 11, 1, 4, 14, 1, 7, 9, 4, 12, 10, 14, 8, 2, 13,
0, 15, 6, 12, 10, 9, 13, 0, 15, 3, 3, 5, 5, 6, 8, 11
}
};
static_assert(std::size(SBOX_TABLES) == 8);
Data32 result = 0;
for (int_fast8_t i = 0; i < 8; ++i)
{
Data6 input = (value >> (42 - (i * 6))) & Inner_::Bitwise::Mask<6>();
result |= static_cast<Data32>(SBOX_TABLES[i][input]) << (28 - (i * 4));
}
return result;
}
static Data32 P(Data32 value)
{
constexpr int_fast8_t P_TABLE[] =
{
16, 7, 20, 21,
29, 12, 28, 17,
1, 15, 23, 26,
5, 18, 31, 10,
2, 8, 24, 14,
32, 27, 3, 9,
19, 13, 30, 6,
22, 11, 4, 25
};
static_assert(std::size(P_TABLE) == 32);
return Inner_::Bitwise::TableChoice<32, 32>(value,
P_TABLE,
P_TABLE + std::size(P_TABLE));
}
static BlockHalf F(BlockHalf value, Inner_::KeySchedule::RoundKey48 roundKey)
{
Data48 expanded = E(value);
expanded = (expanded ^ roundKey) & Inner_::Bitwise::Mask<48>();
return P(SBlock(expanded));
}
static Block Ip(Block block)
{
constexpr int_fast8_t IP_TABLE[] =
{
58, 50, 42, 34, 26, 18, 10, 2,
60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6,
64, 56, 48, 40, 32, 24, 16, 8,
57, 49, 41, 33, 25, 17, 9, 1,
59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5,
63, 55, 47, 39, 31, 23, 15, 7
};
static_assert(std::size(IP_TABLE) == 64);
return Inner_::Bitwise::TableChoice<64, 64>(block,
IP_TABLE,
IP_TABLE + std::size(IP_TABLE));
}
static Block Fp(Block block)
{
constexpr int_fast8_t FP_TABLE[] =
{
40, 8, 48, 16, 56, 24, 64, 32,
39, 7, 47, 15, 55, 23, 63, 31,
38, 6, 46, 14, 54, 22, 62, 30,
37, 5, 45, 13, 53, 21, 61, 29,
36, 4, 44, 12, 52, 20, 60, 28,
35, 3, 43, 11, 51, 19, 59, 27,
34, 2, 42, 10, 50, 18, 58, 26,
33, 1, 41, 9, 49, 17, 57, 25
};
static_assert(std::size(FP_TABLE) == 64);
return Inner_::Bitwise::TableChoice<64, 64>(block,
FP_TABLE,
FP_TABLE + std::size(FP_TABLE));
}
static Block EncryptBlock(Block block, const Key & key)
{
Inner_::KeySchedule schedule(key.Key_);
block = Ip(block);
uint32_t l32;
uint32_t r32;
{
auto [l, r] = Inner_::Bitwise::Split<32>(block);
l32 = static_cast<uint32_t>(l);
r32 = static_cast<uint32_t>(r);
}
for (int_fast8_t i = 0; i < 16; ++i)
{
uint32_t l32Old = l32;
l32 = r32;
r32 = l32Old ^ F(r32, schedule[i]);
}
return Fp(Inner_::Bitwise::Merge<32>(r32, l32));
}
};
} // namespace Chaos::Cipher::Des
#endif // CHAOS_CIPHER_DES_DESCRYPT_HPP

View File

@@ -17,6 +17,7 @@ set(ChaosTests_SOURCE Hash/Md4HasherTests.cpp
Mac/HmacTests.cpp
Cipher/Arc4GenTests.cpp
Cipher/Arc4CryptTests.cpp
Cipher/DesCryptTests.cpp
Service/SeArrayTests.cpp)
add_executable(ChaosTests ${ChaosTests_SOURCE})

View File

@@ -0,0 +1,226 @@
#include <gtest/gtest.h>
#include "Cipher/Des/DesCrypt.hpp"
using namespace Chaos::Cipher::Des;
TEST(DesCryptTests, KeyScheduleTest)
{
Inner_::RawKeyArray key;
key[0] = 0b00010011;
key[1] = 0b00110100;
key[2] = 0b01010111;
key[3] = 0b01111001;
key[4] = 0b10011011;
key[5] = 0b10111100;
key[6] = 0b11011111;
key[7] = 0b11110001;
Inner_::KeySchedule schedule(key);
ASSERT_EQ(0b000110110000001011101111111111000111000001110010ULL, schedule[0]);
ASSERT_EQ(0b011110011010111011011001110110111100100111100101ULL, schedule[1]);
ASSERT_EQ(0b010101011111110010001010010000101100111110011001ULL, schedule[2]);
ASSERT_EQ(0b011100101010110111010110110110110011010100011101ULL, schedule[3]);
ASSERT_EQ(0b011111001110110000000111111010110101001110101000ULL, schedule[4]);
ASSERT_EQ(0b011000111010010100111110010100000111101100101111ULL, schedule[5]);
ASSERT_EQ(0b111011001000010010110111111101100001100010111100ULL, schedule[6]);
ASSERT_EQ(0b111101111000101000111010110000010011101111111011ULL, schedule[7]);
ASSERT_EQ(0b111000001101101111101011111011011110011110000001ULL, schedule[8]);
ASSERT_EQ(0b101100011111001101000111101110100100011001001111ULL, schedule[9]);
ASSERT_EQ(0b001000010101111111010011110111101101001110000110ULL, schedule[10]);
ASSERT_EQ(0b011101010111000111110101100101000110011111101001ULL, schedule[11]);
ASSERT_EQ(0b100101111100010111010001111110101011101001000001ULL, schedule[12]);
ASSERT_EQ(0b010111110100001110110111111100101110011100111010ULL, schedule[13]);
ASSERT_EQ(0b101111111001000110001101001111010011111100001010ULL, schedule[14]);
ASSERT_EQ(0b110010110011110110001011000011100001011111110101ULL, schedule[15]);
}
TEST(DesCryptTests, EncryptTest)
{
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::EncryptBlock(result.begin(), data.begin(), data.end(), desKey);
return result;
}
};
Helper des;
{
std::array<uint8_t, 8> data = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
std::array<uint8_t, 8> key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 };
std::array<uint8_t, 8> expected = { 0x85, 0xe8, 0x13, 0x54, 0x0f, 0x0a, 0xb4, 0x05 };
ASSERT_EQ(expected, des(data, key));
}
{
std::array<uint8_t, 8> data = { 0xaa, 0xf3, 0x83, 0x16, 0x2d, 0x2e, 0x6b, 0xcb };
std::array<uint8_t, 8> key = { 0x44, 0xbf, 0x32, 0x19, 0x99, 0x25, 0x81, 0x51 };
std::array<uint8_t, 8> expected = { 0x07, 0xe8, 0x7f, 0xaa, 0xb3, 0x17, 0x13, 0x18 };
ASSERT_EQ(expected, des(data, key));
}
{
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 };
std::array<uint8_t, 8> expected = { 0x42, 0x27, 0x88, 0xa6, 0x7b, 0x6c, 0x18, 0xed };
ASSERT_EQ(expected, des(data, key));
}
}
TEST(DesCryptTests, EncryptShortDataTest)
{
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::EncryptBlock(result.begin(), data.begin(), data.end(), desKey);
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 = { 0xd8, 0xa8, 0xb8, 0xb4, 0xc0, 0x9b, 0x04, 0x09 };
ASSERT_EQ(expected, des(data, key));
ASSERT_EQ(expected, des(dataShort, key));
}
}
TEST(DesCryptTests, EncryptLongDataTest)
{
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::EncryptBlock(result.begin(), data.begin(), data.end(), desKey);
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 = { 0x42, 0x27, 0x88, 0xa6, 0x7b, 0x6c, 0x18, 0xed };
ASSERT_EQ(expected, des(data, key));
ASSERT_EQ(expected, des(dataLong, key));
}
}
TEST(DesCryptTests, ShortKeyTest)
{
{
std::array<uint8_t, 7> key = {};
ASSERT_THROW(DesCrypt::Key(key.begin(), key.end()), Chaos::Service::ChaosException);
}
}
TEST(DesCryptTests, LongKeyTest)
{
{
std::array<uint8_t, 9> key = {};
ASSERT_THROW(DesCrypt::Key(key.begin(), key.end()), Chaos::Service::ChaosException);
}
}
TEST(DesCryptTests, OutIteratorUsageTest)
{
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::EncryptBlock(it, data.begin(), data.end(), desKey);
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::EncryptBlock(it, data.begin(), data.end(), desKey);
ASSERT_EQ(8, asteriskCalls);
ASSERT_EQ(8, incrementCalls);
}
}