Compare commits

..

6 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
hashlag
7f9430e0e6 Update HmacTests: add Sha1HmacTest
All checks were successful
ChaosTest CI / build-and-test (pull_request) Successful in 30s
ChaosTest CI / build-and-test (push) Successful in 31s
2026-01-06 04:30:07 +03:00
hashlag
fc2f100f0c Add SHA1 draft implementation
All checks were successful
ChaosTest CI / build-and-test (push) Successful in 34s
2026-01-06 04:13:31 +03:00
6 changed files with 1174 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

297
Chaos/Hash/Sha1.hpp Normal file
View File

@@ -0,0 +1,297 @@
#ifndef CHAOS_HASH_SHA1_HPP
#define CHAOS_HASH_SHA1_HPP
#include <cstdint>
#include <array>
#include <string>
#include "Hash.hpp"
#include "Hasher.hpp"
namespace Chaos::Hash::Sha1::Inner_
{
struct Buffer
{
uint32_t Regs_[5] = { 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0 };
};
using Block = std::array<uint32_t, 16>;
struct Algorithm
{
public:
static void UpdateBuffer(Buffer & buffer, const Block & block)
{
static_assert(std::tuple_size_v<ScheduledBlock> == 80);
ScheduledBlock scheduled;
ScheduleBlock(scheduled, block);
uint32_t a = buffer.Regs_[0];
uint32_t b = buffer.Regs_[1];
uint32_t c = buffer.Regs_[2];
uint32_t d = buffer.Regs_[3];
uint32_t e = buffer.Regs_[4];
for (int_fast8_t i = 0; i < 20; ++i)
{
PerformRound(a, b, c, d, e, F0, scheduled[i], 0x5a827999);
}
for (int_fast8_t i = 20; i < 40; ++i)
{
PerformRound(a, b, c, d, e, F20, scheduled[i], 0x6ed9eba1);
}
for (int_fast8_t i = 40; i < 60; ++i)
{
PerformRound(a, b, c, d, e, F40, scheduled[i], 0x8f1bbcdc);
}
for (int_fast8_t i = 60; i < 80; ++i)
{
PerformRound(a, b, c, d, e, F60, scheduled[i], 0xca62c1d6);
}
buffer.Regs_[0] += a;
buffer.Regs_[1] += b;
buffer.Regs_[2] += c;
buffer.Regs_[3] += d;
buffer.Regs_[4] += e;
}
private:
using ScheduledBlock = std::array<uint32_t, 80>;
using RoundFunction = uint32_t (*)(uint32_t b, uint32_t c, uint32_t d);
static uint32_t Rotl(uint32_t v, int_fast8_t s)
{
return (v << s) | (v >> (32 - s));
}
static uint32_t F0(uint32_t b, uint32_t c, uint32_t d)
{
return (b & c) | ((~b) & d);
}
static uint32_t F20(uint32_t b, uint32_t c, uint32_t d)
{
return b ^ c ^ d;
}
static uint32_t F40(uint32_t b, uint32_t c, uint32_t d)
{
return (b & c) | (b & d) | (c & d);
}
static uint32_t F60(uint32_t b, uint32_t c, uint32_t d)
{
return b ^ c ^ d;
}
static void ScheduleBlock(ScheduledBlock & result, const Block & block)
{
static_assert(std::tuple_size_v<Block> == 16);
static_assert(std::tuple_size_v<ScheduledBlock> == 80);
std::copy(block.begin(), block.end(), result.begin());
for (int_fast8_t t = 16; t < 80; ++t)
{
result[t] = Rotl(result[t - 3] ^
result[t - 8] ^
result[t - 14] ^
result[t - 16], 1);
}
}
static void PerformRound(uint32_t & a, uint32_t & b, uint32_t & c,
uint32_t & d, uint32_t & e,
RoundFunction func, uint32_t data, uint32_t k)
{
const uint32_t temp = Rotl(a, 5) + func(b, c, d) + e + data + k;
e = d;
d = c;
c = Rotl(b, 30);
b = a;
a = temp;
}
};
} // namespace Chaos::Hash::Sha1::Inner_
namespace Chaos::Hash::Sha1
{
struct Sha1Hash : public Hash<Sha1Hash>
{
std::array<uint8_t, 20> GetRawDigest() const
{
return RawDigest_;
}
std::string ToHexString() const
{
char buf[41];
std::sprintf(buf,
"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"
"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
RawDigest_[ 0], RawDigest_[ 1], RawDigest_[ 2], RawDigest_[ 3],
RawDigest_[ 4], RawDigest_[ 5], RawDigest_[ 6], RawDigest_[ 7],
RawDigest_[ 8], RawDigest_[ 9], RawDigest_[10], RawDigest_[11],
RawDigest_[12], RawDigest_[13], RawDigest_[14], RawDigest_[15],
RawDigest_[16], RawDigest_[17], RawDigest_[18], RawDigest_[19]);
return std::string(buf, buf + 40);
}
std::array<uint8_t, 20> RawDigest_;
};
class Sha1Hasher : public Hasher<Sha1Hasher>
{
public:
using HashType = Sha1Hash;
static constexpr size_t BLOCK_SIZE_BYTES = 64;
Sha1Hasher()
{
ResetImpl();
}
void Reset()
{
ResetImpl();
}
template<typename InputIt>
void Update(InputIt begin, InputIt end)
{
MessageSizeBytes_ += UpdateImpl(begin, end);
}
HashType Finish()
{
uint64_t messageSizeBytesMod64 = MessageSizeBytes_ % 64;
int_fast8_t paddingNeededBytes;
if (messageSizeBytesMod64 < 56)
{
paddingNeededBytes = 56 - messageSizeBytesMod64;
}
else if (messageSizeBytesMod64 > 56)
{
paddingNeededBytes = 120 - messageSizeBytesMod64;
}
else
{
paddingNeededBytes = 64;
}
UpdateImpl(PAD_, PAD_ + paddingNeededBytes);
const uint64_t messageSizeBits = MessageSizeBytes_ * 8;
uint8_t encodedMessageSizeBits[] =
{
static_cast<uint8_t>((messageSizeBits >> 56) & 0xFF),
static_cast<uint8_t>((messageSizeBits >> 48) & 0xFF),
static_cast<uint8_t>((messageSizeBits >> 40) & 0xFF),
static_cast<uint8_t>((messageSizeBits >> 32) & 0xFF),
static_cast<uint8_t>((messageSizeBits >> 24) & 0xFF),
static_cast<uint8_t>((messageSizeBits >> 16) & 0xFF),
static_cast<uint8_t>((messageSizeBits >> 8) & 0xFF),
static_cast<uint8_t>((messageSizeBits >> 0) & 0xFF),
};
static_assert(std::size(encodedMessageSizeBits) == 8);
UpdateImpl(encodedMessageSizeBits,
encodedMessageSizeBits + std::size(encodedMessageSizeBits));
HashType result;
int_fast8_t i = 0;
for (int_fast8_t reg = 0; reg < 5; ++reg)
{
for (int_fast8_t shift = 0; shift < 32; shift += 8)
{
result.RawDigest_[i++] = (Buffer_.Regs_[reg] >> (24 - shift)) & 0xFF;
}
}
return result;
}
private:
static constexpr uint8_t PAD_[] =
{
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
static_assert(std::size(PAD_) == 64);
Inner_::Buffer Buffer_;
Inner_::Block Block_;
int_fast8_t BlockSize_;
uint32_t Word_;
int_fast8_t WordBytesPacked_;
uint64_t MessageSizeBytes_;
void ResetImpl()
{
Buffer_ = Inner_::Buffer();
Block_.fill(0);
BlockSize_ = 0;
Word_ = 0;
WordBytesPacked_ = 0;
MessageSizeBytes_ = 0;
}
template<typename InputIt>
uint64_t UpdateImpl(InputIt begin, InputIt end)
{
uint64_t written = 0;
for (InputIt it = begin; it != end; ++it, ++written)
{
Word_ |= (static_cast<uint32_t>(*it) << (24 - (WordBytesPacked_ * 8)));
++WordBytesPacked_;
if (WordBytesPacked_ == 4)
{
Block_[BlockSize_++] = Word_;
WordBytesPacked_ = 0;
Word_ = 0;
if (BlockSize_ == 16)
{
Inner_::Algorithm::UpdateBuffer(Buffer_, Block_);
BlockSize_ = 0;
}
}
}
return written;
}
};
} // namespace Chaos::Hash::Sha1
#endif // CHAOS_HASH_SHA1_HPP

View File

@@ -13,9 +13,11 @@ FetchContent_MakeAvailable(googletest)
set(ChaosTests_SOURCE Hash/Md4HasherTests.cpp
Hash/Md5HasherTests.cpp
Hash/Sha1HasherTests.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);
}
}

View File

@@ -0,0 +1,202 @@
#include <gtest/gtest.h>
#include "Hash/Sha1.hpp"
using namespace Chaos::Hash::Sha1;
TEST(Sha1Tests, RfcTest)
{
struct Helper
{
std::string operator()(const char * in) const
{
Sha1Hasher hasher;
hasher.Update(in, in + strlen(in));
return hasher.Finish().ToHexString();
}
};
Helper hash;
ASSERT_EQ("da39a3ee5e6b4b0d3255bfef95601890afd80709", hash(""));
ASSERT_EQ("86f7e437faa5a7fce15d1ddcb9eaeaea377667b8", hash("a"));
ASSERT_EQ("a9993e364706816aba3e25717850c26c9cd0d89d", hash("abc"));
ASSERT_EQ("84983e441c3bd26ebaae4aa1f95129e5e54670f1", hash("abcdbcdecdefdefgefghfghighijhi"
"jkijkljklmklmnlmnomnopnopq"));
ASSERT_EQ("e0c094e867ef46c350ef54a7f59dd60bed92ae83", hash("01234567012345670123456701234567"
"01234567012345670123456701234567"));
}
TEST(Sha1Tests, PartialUpdateTest)
{
{
// "a"
Sha1Hasher hasher;
{
const char * in = "a";
hasher.Update(in, in + strlen(in));
}
{
const char * in = "";
hasher.Update(in, in + strlen(in));
}
ASSERT_EQ("86f7e437faa5a7fce15d1ddcb9eaeaea377667b8", hasher.Finish().ToHexString());
}
{
// "abc"
Sha1Hasher hasher;
{
const char * in = "ab";
hasher.Update(in, in + strlen(in));
}
{
const char * in = "c";
hasher.Update(in, in + strlen(in));
}
ASSERT_EQ("a9993e364706816aba3e25717850c26c9cd0d89d", hasher.Finish().ToHexString());
}
{
// "message digest"
Sha1Hasher hasher;
{
const char * in = "me";
hasher.Update(in, in + strlen(in));
}
{
const char * in = "ssage ";
hasher.Update(in, in + strlen(in));
}
{
const char * in = "diges";
hasher.Update(in, in + strlen(in));
}
{
const char * in = "t";
hasher.Update(in, in + strlen(in));
}
ASSERT_EQ("c12252ceda8be8994d5fa0290a47231c1d16aae3", hasher.Finish().ToHexString());
}
{
// "12345678901234567890123456789012345678901234567890123456789012345678901234567890"
Sha1Hasher hasher;
{
const char * in = "12345678901234567890";
hasher.Update(in, in + strlen(in));
}
{
const char * in = "12345678901234567890";
hasher.Update(in, in + strlen(in));
}
{
const char * in = "12345678901234567890";
hasher.Update(in, in + strlen(in));
}
{
const char * in = "12345678901234567890";
hasher.Update(in, in + strlen(in));
}
ASSERT_EQ("50abf5706a150990a08b2c5ea40fa0e585554732", hasher.Finish().ToHexString());
}
}
TEST(Sha1Tests, LongInputTest)
{
struct Helper
{
std::string operator()(const char * in) const
{
Sha1Hasher hasher;
hasher.Update(in, in + strlen(in));
return hasher.Finish().ToHexString();
}
};
Helper hash;
// 2500 zeros ('0').
ASSERT_EQ("79e7958997241a7ffe484e14cbe1a41a088aa70b", hash(std::string(2500, '0').c_str()));
// 1000 'a' followed by 1000 'b'.
ASSERT_EQ("246f7ca16d5edebf7a5df7ddeab7c044745942ec", hash((std::string(1000, 'a') +
std::string(1000, 'b')).c_str()));
}
TEST(Sha1Tests, LongInputPartialUpdateTest)
{
{
// 2500 zeros ('0').
Sha1Hasher hasher;
std::string in(750, '0');
hasher.Update(in.begin(), in.begin() + 250);
hasher.Update(in.begin(), in.begin() + 500);
hasher.Update(in.begin(), in.begin() + 500);
hasher.Update(in.begin(), in.begin() + 750);
hasher.Update(in.begin(), in.begin() + 333);
hasher.Update(in.begin(), in.begin() + 167);
ASSERT_EQ("79e7958997241a7ffe484e14cbe1a41a088aa70b", hasher.Finish().ToHexString());
}
{
// 1000 'a' followed by 1000 'b'.
Sha1Hasher hasher;
std::string inA(1000, 'a');
std::string inB(1000, 'b');
hasher.Update(inA.begin(), inA.begin() + 100);
hasher.Update(inA.begin(), inA.begin() + 255);
hasher.Update(inA.begin(), inA.begin() + 645);
hasher.Update(inB.begin(), inB.begin() + 33);
hasher.Update(inB.begin(), inB.begin() + 701);
hasher.Update(inB.begin(), inB.begin() + 266);
ASSERT_EQ("246f7ca16d5edebf7a5df7ddeab7c044745942ec", hasher.Finish().ToHexString());
}
}
TEST(Sha1Tests, ResetTest)
{
Sha1Hasher hasher;
{
const char * in = "abc";
hasher.Update(in, in + strlen(in));
}
ASSERT_EQ("a9993e364706816aba3e25717850c26c9cd0d89d", hasher.Finish().ToHexString());
hasher.Reset();
{
const char * in = "message digest";
hasher.Update(in, in + strlen(in));
}
ASSERT_EQ("c12252ceda8be8994d5fa0290a47231c1d16aae3", hasher.Finish().ToHexString());
hasher.Reset();
ASSERT_EQ("da39a3ee5e6b4b0d3255bfef95601890afd80709", hasher.Finish().ToHexString());
}

View File

@@ -1,10 +1,12 @@
#include <gtest/gtest.h>
#include "Hash/Md5.hpp"
#include "Hash/Sha1.hpp"
#include "Mac/Hmac.hpp"
using namespace Chaos::Mac::Hmac;
using namespace Chaos::Hash::Md5;
using namespace Chaos::Hash::Sha1;
TEST(HmacTests, RfcTest)
{
@@ -51,6 +53,28 @@ TEST(HmacTests, RfcTest)
}
}
TEST(HmacTests, Sha1HmacTest)
{
struct Helper
{
std::string operator()(const char * key, const char * data) const
{
Hmac<Sha1Hasher> hmac(key, key + strlen(key));
hmac.Update(data, data + strlen(data));
return hmac.Finish().ToHexString();
}
};
Helper hmacSha1;
{
const char * key = "Two Generals'";
const char * data = "Attack at dawn.";
ASSERT_EQ("20ccda1c4de0e206f3a47056f2abd40f731ff3db", hmacSha1(key, data));
}
}
TEST(HmacTests, LongKeyTest)
{
struct Helper