Compare commits
5 Commits
9e67a7e4f4
...
15dd7398d2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
15dd7398d2 | ||
|
|
5fd92e0c9d | ||
|
|
da5eaff182 | ||
|
|
3a2a665031 | ||
|
|
fa042e7abf |
@@ -1,6 +1,7 @@
|
||||
#ifndef CHAOS_CIPHER_DES_DESCRYPT_HPP
|
||||
#define CHAOS_CIPHER_DES_DESCRYPT_HPP
|
||||
|
||||
#include <algorithm>
|
||||
#include <utility>
|
||||
|
||||
#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
|
||||
{
|
||||
@@ -98,9 +99,15 @@ public:
|
||||
|
||||
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);
|
||||
|
||||
@@ -119,6 +126,11 @@ public:
|
||||
|
||||
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
|
||||
@@ -204,11 +216,18 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
Inner_::RawKeyArray Key_;
|
||||
Inner_::RawKey Key_;
|
||||
};
|
||||
|
||||
class Encryptor
|
||||
{
|
||||
public:
|
||||
Encryptor(const Key & key)
|
||||
: Schedule_(Inner_::KeySchedule::Direction::Encrypt, key.Key_)
|
||||
{ }
|
||||
|
||||
template<typename OutputIt, typename InputIt>
|
||||
static void EncryptBlock(OutputIt out, InputIt inBegin, InputIt inEnd, const Key & key)
|
||||
void EncryptBlock(OutputIt out, InputIt inBegin, InputIt inEnd)
|
||||
{
|
||||
RawBlockArray block;
|
||||
|
||||
@@ -218,11 +237,48 @@ public:
|
||||
block[i] = *in;
|
||||
}
|
||||
|
||||
Block encrypted = EncryptBlock(Inner_::Bitwise::PackUInt64(block.Begin(), block.End()), key);
|
||||
Block encrypted
|
||||
= DesCrypt::ProcessBlock(Inner_::Bitwise::PackUInt64(block.Begin(),
|
||||
block.End()),
|
||||
Schedule_);
|
||||
|
||||
Inner_::Bitwise::CrunchUInt64(out, encrypted);
|
||||
}
|
||||
|
||||
private:
|
||||
Inner_::KeySchedule Schedule_;
|
||||
};
|
||||
|
||||
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:
|
||||
using Block = uint64_t;
|
||||
using BlockHalf = uint32_t;
|
||||
@@ -391,10 +447,8 @@ private:
|
||||
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);
|
||||
|
||||
uint32_t l32;
|
||||
|
||||
@@ -6,7 +6,7 @@ using namespace Chaos::Cipher::Des;
|
||||
|
||||
TEST(DesCryptTests, KeyScheduleTest)
|
||||
{
|
||||
Inner_::RawKeyArray key;
|
||||
Inner_::RawKey key;
|
||||
|
||||
key[0] = 0b00010011;
|
||||
key[1] = 0b00110100;
|
||||
@@ -17,7 +17,7 @@ TEST(DesCryptTests, KeyScheduleTest)
|
||||
key[6] = 0b11011111;
|
||||
key[7] = 0b11110001;
|
||||
|
||||
Inner_::KeySchedule schedule(key);
|
||||
Inner_::KeySchedule schedule(Inner_::KeySchedule::Direction::Encrypt, key);
|
||||
|
||||
ASSERT_EQ(0b000110110000001011101111111111000111000001110010ULL, schedule[0]);
|
||||
ASSERT_EQ(0b011110011010111011011001110110111100100111100101ULL, schedule[1]);
|
||||
@@ -48,7 +48,8 @@ TEST(DesCryptTests, EncryptTest)
|
||||
result.fill(0);
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -95,7 +96,8 @@ TEST(DesCryptTests, EncryptShortDataTest)
|
||||
result.resize(8, 0);
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -128,7 +130,8 @@ TEST(DesCryptTests, EncryptLongDataTest)
|
||||
result.resize(8, 0);
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -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)
|
||||
{
|
||||
{
|
||||
@@ -166,7 +285,7 @@ TEST(DesCryptTests, LongKeyTest)
|
||||
}
|
||||
}
|
||||
|
||||
TEST(DesCryptTests, OutIteratorUsageTest)
|
||||
TEST(DesCryptTests, OutIteratorUsageEncryptTest)
|
||||
{
|
||||
struct OutputItMock
|
||||
{
|
||||
@@ -203,7 +322,8 @@ TEST(DesCryptTests, OutIteratorUsageTest)
|
||||
OutputItMock it(asteriskCalls, incrementCalls);
|
||||
|
||||
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);
|
||||
@@ -218,7 +338,69 @@ TEST(DesCryptTests, OutIteratorUsageTest)
|
||||
OutputItMock it(asteriskCalls, incrementCalls);
|
||||
|
||||
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, incrementCalls);
|
||||
|
||||
Reference in New Issue
Block a user