Compare commits
26 Commits
a97826da88
...
pad
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
797a5428cc | ||
|
|
5baede0a1d | ||
|
|
2a3185406b | ||
|
|
0647a7c3dc | ||
|
|
7c86d704b7 | ||
|
|
9f6265395d | ||
|
|
151c93560c | ||
|
|
3059bd4c66 | ||
|
|
ccf1397595 | ||
|
|
6a09d81ae2 | ||
|
|
b4f015f501 | ||
|
|
95e74db6ec | ||
|
|
ed22d29af0 | ||
|
|
65eb51c133 | ||
|
|
b34fda75ef | ||
|
|
15d841893c | ||
|
|
5fd27cd7b5 | ||
|
|
1bf91a90d0 | ||
|
|
2d64389d66 | ||
|
|
ccdf14d31a | ||
|
|
9734f9a517 | ||
|
|
3de69d3298 | ||
|
|
a0f17ea127 | ||
|
|
67709e5361 | ||
|
|
d194fef1af | ||
|
|
cdcab2f4f7 |
@@ -9,18 +9,19 @@ class Decryptor
|
||||
{
|
||||
public:
|
||||
template<typename OutputIt, typename InputIt>
|
||||
void DecryptBlock(OutputIt out, InputIt inBegin, InputIt inEnd)
|
||||
void DecryptBlock(OutputIt outBegin, OutputIt outEnd,
|
||||
InputIt inBegin, InputIt inEnd) const
|
||||
{
|
||||
Impl().DecryptBlock(out, inBegin, inEnd);
|
||||
Impl().DecryptBlock(outBegin, outEnd, inBegin, inEnd);
|
||||
}
|
||||
|
||||
template<typename Block>
|
||||
auto DecryptBlock(Block block)
|
||||
auto DecryptBlock(Block block) const
|
||||
{
|
||||
return Impl().DecryptBlock(block);
|
||||
}
|
||||
|
||||
auto GetBlockSize()
|
||||
auto GetBlockSize() const
|
||||
{
|
||||
return Impl().GetBlockSize();
|
||||
}
|
||||
|
||||
@@ -83,11 +83,12 @@ struct Bitwise
|
||||
}
|
||||
|
||||
template<typename OutputIt>
|
||||
static void CrunchUInt64(OutputIt out, uint64_t value)
|
||||
static void CrunchUInt64(OutputIt outBegin, OutputIt outEnd, uint64_t value)
|
||||
{
|
||||
for (int_fast8_t i = 0; i < 8; ++i)
|
||||
int_fast8_t i = 0;
|
||||
for (OutputIt out = outBegin; i < 8 && out != outEnd; ++i, ++out)
|
||||
{
|
||||
*out++ = (value >> (56 - (i * 8))) & Mask<8>();
|
||||
*out = (value >> (56 - (i * 8))) & Mask<8>();
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -197,8 +198,10 @@ class DesCrypt
|
||||
public:
|
||||
using Block = uint64_t;
|
||||
static constexpr size_t BlockSize = 8;
|
||||
static constexpr size_t KeySize = 8;
|
||||
|
||||
static_assert(BlockSize == sizeof(Block));
|
||||
static_assert(KeySize == Inner_::RawKey::Size());
|
||||
|
||||
DesCrypt() = delete;
|
||||
|
||||
@@ -230,14 +233,17 @@ public:
|
||||
class DesEncryptor : public Encryptor<DesEncryptor>
|
||||
{
|
||||
public:
|
||||
using Key = DesCrypt::Key;
|
||||
static constexpr size_t BlockSize = DesCrypt::BlockSize;
|
||||
static constexpr size_t KeySize = DesCrypt::KeySize;
|
||||
|
||||
DesEncryptor(const Key & key)
|
||||
: Schedule_(Inner_::KeySchedule::Direction::Encrypt, key.Key_)
|
||||
{ }
|
||||
|
||||
template<typename OutputIt, typename InputIt>
|
||||
void EncryptBlock(OutputIt out, InputIt inBegin, InputIt inEnd)
|
||||
void EncryptBlock(OutputIt outBegin, OutputIt outEnd,
|
||||
InputIt inBegin, InputIt inEnd) const
|
||||
{
|
||||
RawBlockArray block;
|
||||
|
||||
@@ -252,15 +258,15 @@ public:
|
||||
block.End()),
|
||||
Schedule_);
|
||||
|
||||
Inner_::Bitwise::CrunchUInt64(out, encrypted);
|
||||
Inner_::Bitwise::CrunchUInt64(outBegin, outEnd, encrypted);
|
||||
}
|
||||
|
||||
Block EncryptBlock(Block block)
|
||||
Block EncryptBlock(Block block) const
|
||||
{
|
||||
return DesCrypt::ProcessBlock(block, Schedule_);
|
||||
}
|
||||
|
||||
constexpr size_t GetBlockSize()
|
||||
constexpr size_t GetBlockSize() const
|
||||
{
|
||||
return BlockSize;
|
||||
}
|
||||
@@ -272,14 +278,17 @@ public:
|
||||
class DesDecryptor : public Decryptor<DesDecryptor>
|
||||
{
|
||||
public:
|
||||
using Key = DesCrypt::Key;
|
||||
static constexpr size_t BlockSize = DesCrypt::BlockSize;
|
||||
static constexpr size_t KeySize = DesCrypt::KeySize;
|
||||
|
||||
DesDecryptor(const Key & key)
|
||||
: Schedule_(Inner_::KeySchedule::Direction::Decrypt, key.Key_)
|
||||
{ }
|
||||
|
||||
template<typename OutputIt, typename InputIt>
|
||||
void DecryptBlock(OutputIt out, InputIt inBegin, InputIt inEnd)
|
||||
void DecryptBlock(OutputIt outBegin, OutputIt outEnd,
|
||||
InputIt inBegin, InputIt inEnd) const
|
||||
{
|
||||
RawBlockArray block;
|
||||
|
||||
@@ -294,15 +303,15 @@ public:
|
||||
block.End()),
|
||||
Schedule_);
|
||||
|
||||
Inner_::Bitwise::CrunchUInt64(out, decrypted);
|
||||
Inner_::Bitwise::CrunchUInt64(outBegin, outEnd, decrypted);
|
||||
}
|
||||
|
||||
Block DecryptBlock(Block block)
|
||||
Block DecryptBlock(Block block) const
|
||||
{
|
||||
return DesCrypt::ProcessBlock(block, Schedule_);
|
||||
}
|
||||
|
||||
constexpr size_t GetBlockSize()
|
||||
constexpr size_t GetBlockSize() const
|
||||
{
|
||||
return BlockSize;
|
||||
}
|
||||
|
||||
@@ -9,18 +9,19 @@ class Encryptor
|
||||
{
|
||||
public:
|
||||
template<typename OutputIt, typename InputIt>
|
||||
void EncryptBlock(OutputIt out, InputIt inBegin, InputIt inEnd)
|
||||
void EncryptBlock(OutputIt outBegin, OutputIt outEnd,
|
||||
InputIt inBegin, InputIt inEnd) const
|
||||
{
|
||||
Impl().EncryptBlock(out, inBegin, inEnd);
|
||||
Impl().EncryptBlock(outBegin, outEnd, inBegin, inEnd);
|
||||
}
|
||||
|
||||
template<typename Block>
|
||||
auto EncryptBlock(Block block)
|
||||
auto EncryptBlock(Block block) const
|
||||
{
|
||||
return Impl().EncryptBlock(block);
|
||||
}
|
||||
|
||||
auto GetBlockSize()
|
||||
auto GetBlockSize() const
|
||||
{
|
||||
return Impl().GetBlockSize();
|
||||
}
|
||||
|
||||
34
Chaos/Padding/Padder.hpp
Normal file
34
Chaos/Padding/Padder.hpp
Normal file
@@ -0,0 +1,34 @@
|
||||
#ifndef CHAOS_PADDING_PADDER_HPP
|
||||
#define CHAOS_PADDING_PADDER_HPP
|
||||
|
||||
namespace Chaos::Padding
|
||||
{
|
||||
|
||||
template<typename T>
|
||||
class Padder
|
||||
{
|
||||
public:
|
||||
template<typename OutputIt>
|
||||
void Pad(OutputIt begin, OutputIt end) const
|
||||
{
|
||||
Impl().Pad(begin, end);
|
||||
}
|
||||
|
||||
protected:
|
||||
Padder() = default;
|
||||
|
||||
private:
|
||||
const T & Impl() const
|
||||
{
|
||||
return static_cast<const T &>(*this);
|
||||
}
|
||||
|
||||
T & Impl()
|
||||
{
|
||||
return static_cast<T &>(*this);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace Chaos::Padding
|
||||
|
||||
#endif // CHAOS_PADDING_PADDER_HPP
|
||||
38
Chaos/Padding/PadderPkcs7.hpp
Normal file
38
Chaos/Padding/PadderPkcs7.hpp
Normal file
@@ -0,0 +1,38 @@
|
||||
#ifndef CHAOS_PADDING_PADDERPKCS7_HPP
|
||||
#define CHAOS_PADDING_PADDERPKCS7_HPP
|
||||
|
||||
#include <cstdint>
|
||||
#include <iterator>
|
||||
#include <limits>
|
||||
|
||||
#include "Padding/Padder.hpp"
|
||||
#include "Service/ChaosException.hpp"
|
||||
|
||||
namespace Chaos::Padding
|
||||
{
|
||||
|
||||
class PadderPkcs7 : public Padder<PadderPkcs7>
|
||||
{
|
||||
public:
|
||||
template<typename OutputIt>
|
||||
static void Pad(OutputIt begin, OutputIt end)
|
||||
{
|
||||
auto dist = std::distance(begin, end);
|
||||
|
||||
if (dist >= 0 && dist <= std::numeric_limits<uint8_t>::max())
|
||||
{
|
||||
for (OutputIt it = begin; it != end; ++it)
|
||||
{
|
||||
*it = static_cast<uint8_t>(dist);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw Service::ChaosException("PadPkcs7::Pad(): invalid range");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace Chaos::Padding
|
||||
|
||||
#endif // CHAOS_PADDING_PADDERPKCS7_HPP
|
||||
@@ -58,11 +58,6 @@ public:
|
||||
return Storage_.data() + Storage_.size();
|
||||
}
|
||||
|
||||
constexpr size_t Size() const noexcept
|
||||
{
|
||||
return Storage_.size();
|
||||
}
|
||||
|
||||
void Fill(const T & value)
|
||||
{
|
||||
Storage_.fill(value);
|
||||
@@ -73,6 +68,11 @@ public:
|
||||
EraseImpl();
|
||||
}
|
||||
|
||||
static constexpr size_t Size() noexcept
|
||||
{
|
||||
return S;
|
||||
}
|
||||
|
||||
private:
|
||||
std::array<T, S> Storage_;
|
||||
|
||||
|
||||
@@ -18,7 +18,9 @@ set(ChaosTests_SOURCE Hash/Md4HasherTests.cpp
|
||||
Cipher/Arc4GenTests.cpp
|
||||
Cipher/Arc4CryptTests.cpp
|
||||
Cipher/DesCryptTests.cpp
|
||||
Service/SeArrayTests.cpp)
|
||||
Padding/PadderPkcs7Tests.cpp
|
||||
Service/SeArrayTests.cpp
|
||||
Service/ChaosExceptionTests.cpp)
|
||||
|
||||
add_executable(ChaosTests ${ChaosTests_SOURCE})
|
||||
target_link_libraries(ChaosTests gtest gtest_main)
|
||||
@@ -26,6 +28,8 @@ target_include_directories(ChaosTests PRIVATE
|
||||
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/Chaos>
|
||||
)
|
||||
|
||||
target_compile_options(ChaosTests PRIVATE -Wunused -Werror=unused)
|
||||
|
||||
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||
target_compile_options(ChaosTests PRIVATE --coverage)
|
||||
target_link_options(ChaosTests PRIVATE --coverage)
|
||||
|
||||
@@ -58,11 +58,8 @@ TEST(Arc4CryptTests, UninitializedArc4CryptTest)
|
||||
Arc4Crypt arc4;
|
||||
|
||||
{
|
||||
std::array<uint8_t, 10> in;
|
||||
in.fill(0);
|
||||
|
||||
std::array<uint8_t, 10> out;
|
||||
out.fill(0);
|
||||
std::array<uint8_t, 10> in = {};
|
||||
std::array<uint8_t, 10> out = {};
|
||||
|
||||
ASSERT_THROW(arc4.Encrypt(out.begin(), in.begin(), in.size()), Chaos::Service::ChaosException);
|
||||
ASSERT_THROW(arc4.Decrypt(out.begin(), in.begin(), in.size()), Chaos::Service::ChaosException);
|
||||
@@ -107,3 +104,115 @@ TEST(Arc4CryptTests, RekeyTest)
|
||||
ciphertext);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Arc4CryptTests, EncryptOutIteratorUsageTest)
|
||||
{
|
||||
const std::vector<uint8_t> data = StrToU8Vec("The quick brown fox jumps over the lazy dog.");
|
||||
|
||||
{
|
||||
std::array<uint8_t, 5> key = { 0x01, 0x02, 0x03, 0x04, 0x05 };
|
||||
|
||||
Arc4Crypt crypt(key.begin(), key.end());
|
||||
|
||||
std::array<uint8_t, 47> out = {};
|
||||
|
||||
std::array<uint8_t, 47> expected =
|
||||
{
|
||||
0x00, 0x00, 0x00,
|
||||
0xe6, 0x51, 0x06, 0x25, 0x81, 0x48, 0xa9, 0x44, 0xa7, 0xe3, 0x30,
|
||||
0x38, 0x65, 0x66, 0x76, 0x88, 0x0f, 0xed, 0xec, 0x6f, 0x72, 0x89,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||
};
|
||||
|
||||
crypt.Encrypt(out.begin() + 3, data.begin(), 22);
|
||||
|
||||
ASSERT_EQ(expected, out);
|
||||
}
|
||||
|
||||
{
|
||||
std::array<uint8_t, 5> key = { 0x01, 0x02, 0x03, 0x04, 0x05 };
|
||||
|
||||
Arc4Crypt crypt(key.begin(), key.end());
|
||||
|
||||
std::array<uint8_t, 47> out = {};
|
||||
|
||||
std::array<uint8_t, 47> expected =
|
||||
{
|
||||
0x00, 0x00, 0x00,
|
||||
0xe6, 0x51, 0x06, 0x25, 0x81, 0x48, 0xa9, 0x44, 0xa7, 0xe3, 0x30,
|
||||
0x38, 0x65, 0x66, 0x76, 0x88, 0x0f, 0xed, 0xec, 0x6f, 0x72, 0x89,
|
||||
0xef, 0xa5, 0xfa, 0xe4, 0x6c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||
};
|
||||
|
||||
crypt.Encrypt(out.begin() + 3, data.begin(), 27);
|
||||
|
||||
ASSERT_EQ(expected, out);
|
||||
}
|
||||
|
||||
{
|
||||
std::array<uint8_t, 5> key = { 0x01, 0x02, 0x03, 0x04, 0x05 };
|
||||
|
||||
Arc4Crypt crypt(key.begin(), key.end());
|
||||
|
||||
std::array<uint8_t, 44> out = {};
|
||||
std::array<uint8_t, 44> expected = {};
|
||||
|
||||
crypt.Encrypt(out.begin() + 3, data.begin(), 0);
|
||||
|
||||
ASSERT_EQ(expected, out);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Arc4CryptTests, DecryptOutIteratorUsageTest)
|
||||
{
|
||||
const std::array<uint8_t, 14> data = { 0x45, 0xA0, 0x1F, 0x64, 0x5F, 0xC3, 0x5B,
|
||||
0x38, 0x35, 0x52, 0x54, 0x4B, 0x9B, 0xF5 };
|
||||
const std::vector<uint8_t> key = StrToU8Vec("Secret");
|
||||
|
||||
{
|
||||
Arc4Crypt crypt(key.begin(), key.end());
|
||||
|
||||
std::array<uint8_t, 17> out = {};
|
||||
|
||||
std::array<uint8_t, 17> expected =
|
||||
{
|
||||
0x00, 0x00, 0x00,
|
||||
'A', 't', 't', 'a', 'c', 'k', ' ', 'a', 't', ' ', 'd', 'a',
|
||||
0x00, 0x00
|
||||
};
|
||||
|
||||
crypt.Decrypt(out.begin() + 3, data.begin(), 12);
|
||||
|
||||
ASSERT_EQ(expected, out);
|
||||
}
|
||||
|
||||
{
|
||||
Arc4Crypt crypt(key.begin(), key.end());
|
||||
|
||||
std::array<uint8_t, 17> out = {};
|
||||
|
||||
std::array<uint8_t, 17> expected =
|
||||
{
|
||||
0x00, 0x00, 0x00,
|
||||
'A', 't', 't', 'a', 'c', 'k', ' ',
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||
};
|
||||
|
||||
crypt.Decrypt(out.begin() + 3, data.begin(), 7);
|
||||
|
||||
ASSERT_EQ(expected, out);
|
||||
}
|
||||
|
||||
{
|
||||
Arc4Crypt crypt(key.begin(), key.end());
|
||||
|
||||
std::array<uint8_t, 14> out = {};
|
||||
std::array<uint8_t, 14> expected = {};
|
||||
|
||||
crypt.Decrypt(out.begin() + 3, data.begin(), 0);
|
||||
|
||||
ASSERT_EQ(expected, out);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -353,3 +353,56 @@ TEST(Arc4GenTests, UninitializedGenTest)
|
||||
ASSERT_THROW(gen.Drop(256), Chaos::Service::ChaosException);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Arc4GenTests, GenerateOutIteratorUsageTest)
|
||||
{
|
||||
{
|
||||
uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
|
||||
Arc4Gen gen(key, key + std::size(key));
|
||||
|
||||
std::array<uint8_t, 23> out = {};
|
||||
|
||||
std::array<uint8_t, 23> expected =
|
||||
{
|
||||
0x00, 0x00, 0x00,
|
||||
0xb2, 0x39, 0x63, 0x05, 0xf0, 0x3d, 0xc0, 0x27,
|
||||
0xcc, 0xc3, 0x52, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00
|
||||
};
|
||||
|
||||
gen.Generate(out.begin() + 3, 11);
|
||||
|
||||
ASSERT_EQ(expected, out);
|
||||
}
|
||||
|
||||
{
|
||||
uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
|
||||
Arc4Gen gen(key, key + std::size(key));
|
||||
|
||||
std::array<uint8_t, 23> out = {};
|
||||
|
||||
std::array<uint8_t, 23> expected =
|
||||
{
|
||||
0x00, 0x00, 0x00,
|
||||
0xb2, 0x39, 0x63, 0x05, 0xf0, 0x3d, 0xc0, 0x27,
|
||||
0xcc, 0xc3, 0x52, 0x4a, 0x0a, 0x11, 0x18, 0xa8,
|
||||
0x69, 0x82, 0x00, 0x00
|
||||
};
|
||||
|
||||
gen.Generate(out.begin() + 3, 18);
|
||||
|
||||
ASSERT_EQ(expected, out);
|
||||
}
|
||||
|
||||
{
|
||||
uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
|
||||
Arc4Gen gen(key, key + std::size(key));
|
||||
|
||||
std::array<uint8_t, 20> out = {};
|
||||
std::array<uint8_t, 20> expected = {};
|
||||
|
||||
gen.Generate(out.begin() + 3, 0);
|
||||
|
||||
ASSERT_EQ(expected, out);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,14 +44,13 @@ TEST(DesCryptTests, EncryptTest)
|
||||
struct Helper
|
||||
{
|
||||
std::array<uint8_t, DesCrypt::BlockSize> operator()(const std::array<uint8_t, DesCrypt::BlockSize> & data,
|
||||
const std::array<uint8_t, 8> & key) const
|
||||
const std::array<uint8_t, DesCrypt::KeySize> & key) const
|
||||
{
|
||||
std::array<uint8_t, DesCrypt::BlockSize> result;
|
||||
result.fill(0);
|
||||
std::array<uint8_t, DesCrypt::BlockSize> result = {};
|
||||
|
||||
DesCrypt::Key desKey(key.begin(), key.end());
|
||||
DesCrypt::DesEncryptor enc(desKey);
|
||||
enc.EncryptBlock(result.begin(), data.begin(), data.end());
|
||||
enc.EncryptBlock(result.begin(), result.end(), data.begin(), data.end());
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -61,7 +60,7 @@ TEST(DesCryptTests, EncryptTest)
|
||||
|
||||
{
|
||||
std::array<uint8_t, DesCrypt::BlockSize> 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, DesCrypt::KeySize> key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 };
|
||||
|
||||
std::array<uint8_t, DesCrypt::BlockSize> expected = { 0x85, 0xe8, 0x13, 0x54, 0x0f, 0x0a, 0xb4, 0x05 };
|
||||
|
||||
@@ -70,7 +69,7 @@ TEST(DesCryptTests, EncryptTest)
|
||||
|
||||
{
|
||||
std::array<uint8_t, DesCrypt::BlockSize> 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, DesCrypt::KeySize> key = { 0x44, 0xbf, 0x32, 0x19, 0x99, 0x25, 0x81, 0x51 };
|
||||
|
||||
std::array<uint8_t, DesCrypt::BlockSize> expected = { 0x07, 0xe8, 0x7f, 0xaa, 0xb3, 0x17, 0x13, 0x18 };
|
||||
|
||||
@@ -79,7 +78,7 @@ TEST(DesCryptTests, EncryptTest)
|
||||
|
||||
{
|
||||
std::array<uint8_t, DesCrypt::BlockSize> 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, DesCrypt::KeySize> key = { 0xda, 0xec, 0x68, 0xae, 0x83, 0xe0, 0x1e, 0xab };
|
||||
|
||||
std::array<uint8_t, DesCrypt::BlockSize> expected = { 0x42, 0x27, 0x88, 0xa6, 0x7b, 0x6c, 0x18, 0xed };
|
||||
|
||||
@@ -92,7 +91,7 @@ TEST(DesCryptTests, EncryptUInt64BlockTest)
|
||||
struct Helper
|
||||
{
|
||||
uint64_t operator()(uint64_t data,
|
||||
const std::array<uint8_t, 8> & key) const
|
||||
const std::array<uint8_t, DesCrypt::KeySize> & key) const
|
||||
{
|
||||
DesCrypt::Key desKey(key.begin(), key.end());
|
||||
DesCrypt::DesEncryptor enc(desKey);
|
||||
@@ -105,7 +104,7 @@ TEST(DesCryptTests, EncryptUInt64BlockTest)
|
||||
|
||||
{
|
||||
uint64_t data = 0x0123456789abcdef;
|
||||
std::array<uint8_t, 8> key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 };
|
||||
std::array<uint8_t, DesCrypt::KeySize> key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 };
|
||||
|
||||
uint64_t expected = 0x85e813540f0ab405;
|
||||
|
||||
@@ -114,7 +113,7 @@ TEST(DesCryptTests, EncryptUInt64BlockTest)
|
||||
|
||||
{
|
||||
uint64_t data = 0xaaf383162d2e6bcb;
|
||||
std::array<uint8_t, 8> key = { 0x44, 0xbf, 0x32, 0x19, 0x99, 0x25, 0x81, 0x51 };
|
||||
std::array<uint8_t, DesCrypt::KeySize> key = { 0x44, 0xbf, 0x32, 0x19, 0x99, 0x25, 0x81, 0x51 };
|
||||
|
||||
uint64_t expected = 0x07e87faab3171318;
|
||||
|
||||
@@ -123,7 +122,7 @@ TEST(DesCryptTests, EncryptUInt64BlockTest)
|
||||
|
||||
{
|
||||
uint64_t data = 0xe51a9fd419a79344;
|
||||
std::array<uint8_t, 8> key = { 0xda, 0xec, 0x68, 0xae, 0x83, 0xe0, 0x1e, 0xab };
|
||||
std::array<uint8_t, DesCrypt::KeySize> key = { 0xda, 0xec, 0x68, 0xae, 0x83, 0xe0, 0x1e, 0xab };
|
||||
|
||||
uint64_t expected = 0x422788a67b6c18ed;
|
||||
|
||||
@@ -143,7 +142,7 @@ TEST(DesCryptTests, EncryptShortDataTest)
|
||||
|
||||
DesCrypt::Key desKey(key.begin(), key.end());
|
||||
DesCrypt::DesEncryptor enc(desKey);
|
||||
enc.EncryptBlock(result.begin(), data.begin(), data.end());
|
||||
enc.EncryptBlock(result.begin(), result.end(), data.begin(), data.end());
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -177,7 +176,7 @@ TEST(DesCryptTests, EncryptLongDataTest)
|
||||
|
||||
DesCrypt::Key desKey(key.begin(), key.end());
|
||||
DesCrypt::DesEncryptor enc(desKey);
|
||||
enc.EncryptBlock(result.begin(), data.begin(), data.end());
|
||||
enc.EncryptBlock(result.begin(), result.end(), data.begin(), data.end());
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -204,14 +203,13 @@ TEST(DesCryptTests, DecryptTest)
|
||||
struct Helper
|
||||
{
|
||||
std::array<uint8_t, DesCrypt::BlockSize> operator()(const std::array<uint8_t, DesCrypt::BlockSize> & data,
|
||||
const std::array<uint8_t, 8> & key) const
|
||||
const std::array<uint8_t, DesCrypt::KeySize> & key) const
|
||||
{
|
||||
std::array<uint8_t, DesCrypt::BlockSize> result;
|
||||
result.fill(0);
|
||||
std::array<uint8_t, DesCrypt::BlockSize> result = {};
|
||||
|
||||
DesCrypt::Key desKey(key.begin(), key.end());
|
||||
DesCrypt::DesDecryptor dec(desKey);
|
||||
dec.DecryptBlock(result.begin(), data.begin(), data.end());
|
||||
dec.DecryptBlock(result.begin(), result.end(), data.begin(), data.end());
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -221,7 +219,7 @@ TEST(DesCryptTests, DecryptTest)
|
||||
|
||||
{
|
||||
std::array<uint8_t, DesCrypt::BlockSize> 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, DesCrypt::KeySize> key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 };
|
||||
|
||||
std::array<uint8_t, DesCrypt::BlockSize> expected = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
|
||||
|
||||
@@ -230,7 +228,7 @@ TEST(DesCryptTests, DecryptTest)
|
||||
|
||||
{
|
||||
std::array<uint8_t, DesCrypt::BlockSize> 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, DesCrypt::KeySize> key = { 0x44, 0xbf, 0x32, 0x19, 0x99, 0x25, 0x81, 0x51 };
|
||||
|
||||
std::array<uint8_t, DesCrypt::BlockSize> expected = { 0xaa, 0xf3, 0x83, 0x16, 0x2d, 0x2e, 0x6b, 0xcb };
|
||||
|
||||
@@ -239,7 +237,7 @@ TEST(DesCryptTests, DecryptTest)
|
||||
|
||||
{
|
||||
std::array<uint8_t, DesCrypt::BlockSize> 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, DesCrypt::KeySize> key = { 0xda, 0xec, 0x68, 0xae, 0x83, 0xe0, 0x1e, 0xab };
|
||||
|
||||
std::array<uint8_t, DesCrypt::BlockSize> expected = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44 };
|
||||
|
||||
@@ -252,7 +250,7 @@ TEST(DesCryptTests, DecryptUInt64BlockTest)
|
||||
struct Helper
|
||||
{
|
||||
uint64_t operator()(uint64_t data,
|
||||
const std::array<uint8_t, 8> & key) const
|
||||
const std::array<uint8_t, DesCrypt::KeySize> & key) const
|
||||
{
|
||||
DesCrypt::Key desKey(key.begin(), key.end());
|
||||
DesCrypt::DesDecryptor dec(desKey);
|
||||
@@ -265,7 +263,7 @@ TEST(DesCryptTests, DecryptUInt64BlockTest)
|
||||
|
||||
{
|
||||
uint64_t data = 0x85e813540f0ab405;
|
||||
std::array<uint8_t, 8> key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 };
|
||||
std::array<uint8_t, DesCrypt::KeySize> key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 };
|
||||
|
||||
uint64_t expected = 0x0123456789abcdef;
|
||||
|
||||
@@ -274,7 +272,7 @@ TEST(DesCryptTests, DecryptUInt64BlockTest)
|
||||
|
||||
{
|
||||
uint64_t data = 0x07e87faab3171318;
|
||||
std::array<uint8_t, 8> key = { 0x44, 0xbf, 0x32, 0x19, 0x99, 0x25, 0x81, 0x51 };
|
||||
std::array<uint8_t, DesCrypt::KeySize> key = { 0x44, 0xbf, 0x32, 0x19, 0x99, 0x25, 0x81, 0x51 };
|
||||
|
||||
uint64_t expected = 0xaaf383162d2e6bcb;
|
||||
|
||||
@@ -283,7 +281,7 @@ TEST(DesCryptTests, DecryptUInt64BlockTest)
|
||||
|
||||
{
|
||||
uint64_t data = 0x422788a67b6c18ed;
|
||||
std::array<uint8_t, 8> key = { 0xda, 0xec, 0x68, 0xae, 0x83, 0xe0, 0x1e, 0xab };
|
||||
std::array<uint8_t, DesCrypt::KeySize> key = { 0xda, 0xec, 0x68, 0xae, 0x83, 0xe0, 0x1e, 0xab };
|
||||
|
||||
uint64_t expected = 0xe51a9fd419a79344;
|
||||
|
||||
@@ -303,7 +301,7 @@ TEST(DesCryptTests, DecryptShortDataTest)
|
||||
|
||||
DesCrypt::Key desKey(key.begin(), key.end());
|
||||
DesCrypt::DesDecryptor dec(desKey);
|
||||
dec.DecryptBlock(result.begin(), data.begin(), data.end());
|
||||
dec.DecryptBlock(result.begin(), result.end(), data.begin(), data.end());
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -337,7 +335,7 @@ TEST(DesCryptTests, DecryptLongDataTest)
|
||||
|
||||
DesCrypt::Key desKey(key.begin(), key.end());
|
||||
DesCrypt::DesDecryptor dec(desKey);
|
||||
dec.DecryptBlock(result.begin(), data.begin(), data.end());
|
||||
dec.DecryptBlock(result.begin(), result.end(), data.begin(), data.end());
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -362,7 +360,7 @@ TEST(DesCryptTests, DecryptLongDataTest)
|
||||
TEST(DesCryptTests, ShortKeyTest)
|
||||
{
|
||||
{
|
||||
std::array<uint8_t, 7> key = {};
|
||||
std::array<uint8_t, DesCrypt::KeySize - 1> key = {};
|
||||
ASSERT_THROW(DesCrypt::Key(key.begin(), key.end()), Chaos::Service::ChaosException);
|
||||
}
|
||||
}
|
||||
@@ -370,147 +368,119 @@ TEST(DesCryptTests, ShortKeyTest)
|
||||
TEST(DesCryptTests, LongKeyTest)
|
||||
{
|
||||
{
|
||||
std::array<uint8_t, 9> key = {};
|
||||
std::array<uint8_t, DesCrypt::KeySize + 1> key = {};
|
||||
ASSERT_THROW(DesCrypt::Key(key.begin(), key.end()), Chaos::Service::ChaosException);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(DesCryptTests, OutIteratorUsageEncryptTest)
|
||||
{
|
||||
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, DesCrypt::BlockSize> 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, DesCrypt::KeySize> key = { 0xda, 0xec, 0x68, 0xae, 0x83, 0xe0, 0x1e, 0xab };
|
||||
|
||||
size_t asteriskCalls = 0;
|
||||
size_t incrementCalls = 0;
|
||||
OutputItMock it(asteriskCalls, incrementCalls);
|
||||
std::array<uint8_t, 11> fact = {};
|
||||
// First and last 3 bytes should be untouched.
|
||||
std::array<uint8_t, 11> expected = { 0x00, 0x00, 0x00, 0x42, 0x27, 0x88, 0xa6, 0x7b, 0x00, 0x00, 0x00 };
|
||||
|
||||
DesCrypt::Key desKey(key.begin(), key.end());
|
||||
DesCrypt::DesEncryptor enc(desKey);
|
||||
enc.EncryptBlock(it, data.begin(), data.end());
|
||||
enc.EncryptBlock(fact.begin() + 3, fact.end() - 3, data.begin(), data.end());
|
||||
|
||||
ASSERT_EQ(8, asteriskCalls);
|
||||
ASSERT_EQ(8, incrementCalls);
|
||||
ASSERT_EQ(expected, fact);
|
||||
}
|
||||
|
||||
{
|
||||
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 };
|
||||
std::array<uint8_t, DesCrypt::BlockSize + 2> data = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44, 0x44, 0x44 };
|
||||
std::array<uint8_t, DesCrypt::KeySize> key = { 0xda, 0xec, 0x68, 0xae, 0x83, 0xe0, 0x1e, 0xab };
|
||||
|
||||
size_t asteriskCalls = 0;
|
||||
size_t incrementCalls = 0;
|
||||
OutputItMock it(asteriskCalls, incrementCalls);
|
||||
std::array<uint8_t, 12> fact = {};
|
||||
// First and last 4 bytes should be untouched.
|
||||
std::array<uint8_t, 12> expected = { 0x00, 0x00, 0x00, 0x00, 0x42, 0x27, 0x88, 0xa6, 0x00, 0x00, 0x00, 0x00 };
|
||||
|
||||
DesCrypt::Key desKey(key.begin(), key.end());
|
||||
DesCrypt::DesEncryptor enc(desKey);
|
||||
enc.EncryptBlock(it, data.begin(), data.end());
|
||||
enc.EncryptBlock(fact.begin() + 4, fact.end() - 4, data.begin(), data.end());
|
||||
|
||||
ASSERT_EQ(8, asteriskCalls);
|
||||
ASSERT_EQ(8, incrementCalls);
|
||||
ASSERT_EQ(expected, fact);
|
||||
}
|
||||
|
||||
{
|
||||
std::array<uint8_t, DesCrypt::BlockSize + 2> data = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44, 0x44, 0x44 };
|
||||
std::array<uint8_t, DesCrypt::KeySize> key = { 0xda, 0xec, 0x68, 0xae, 0x83, 0xe0, 0x1e, 0xab };
|
||||
|
||||
std::array<uint8_t, 12> fact = {};
|
||||
std::array<uint8_t, 12> expected = {};
|
||||
|
||||
DesCrypt::Key desKey(key.begin(), key.end());
|
||||
DesCrypt::DesEncryptor enc(desKey);
|
||||
enc.EncryptBlock(fact.begin() + 3, fact.begin() + 3, data.begin(), data.end());
|
||||
|
||||
ASSERT_EQ(expected, fact);
|
||||
}
|
||||
}
|
||||
|
||||
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, DesCrypt::BlockSize> 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, DesCrypt::KeySize> key = { 0xda, 0xec, 0x68, 0xae, 0x83, 0xe0, 0x1e, 0xab };
|
||||
|
||||
size_t asteriskCalls = 0;
|
||||
size_t incrementCalls = 0;
|
||||
OutputItMock it(asteriskCalls, incrementCalls);
|
||||
std::array<uint8_t, 11> fact = {};
|
||||
// First and last 3 bytes should be untouched.
|
||||
std::array<uint8_t, 11> expected = { 0x00, 0x00, 0x00, 0x45, 0x69, 0x71, 0x17, 0x13, 0x00, 0x00, 0x00 };
|
||||
|
||||
DesCrypt::Key desKey(key.begin(), key.end());
|
||||
DesCrypt::DesDecryptor dec(desKey);
|
||||
dec.DecryptBlock(it, data.begin(), data.end());
|
||||
dec.DecryptBlock(fact.begin() + 3, fact.end() - 3, data.begin(), data.end());
|
||||
|
||||
ASSERT_EQ(8, asteriskCalls);
|
||||
ASSERT_EQ(8, incrementCalls);
|
||||
ASSERT_EQ(expected, fact);
|
||||
}
|
||||
|
||||
{
|
||||
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 };
|
||||
std::array<uint8_t, DesCrypt::BlockSize + 2> data = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44, 0x44, 0x44 };
|
||||
std::array<uint8_t, DesCrypt::KeySize> key = { 0xda, 0xec, 0x68, 0xae, 0x83, 0xe0, 0x1e, 0xab };
|
||||
|
||||
size_t asteriskCalls = 0;
|
||||
size_t incrementCalls = 0;
|
||||
OutputItMock it(asteriskCalls, incrementCalls);
|
||||
std::array<uint8_t, 12> fact = {};
|
||||
// First and last 4 bytes should be untouched.
|
||||
std::array<uint8_t, 12> expected = { 0x00, 0x00, 0x00, 0x00, 0x45, 0x69, 0x71, 0x17, 0x00, 0x00, 0x00, 0x00 };
|
||||
|
||||
DesCrypt::Key desKey(key.begin(), key.end());
|
||||
DesCrypt::DesDecryptor dec(desKey);
|
||||
dec.DecryptBlock(it, data.begin(), data.end());
|
||||
dec.DecryptBlock(fact.begin() + 4, fact.end() - 4, data.begin(), data.end());
|
||||
|
||||
ASSERT_EQ(8, asteriskCalls);
|
||||
ASSERT_EQ(8, incrementCalls);
|
||||
ASSERT_EQ(expected, fact);
|
||||
}
|
||||
|
||||
{
|
||||
std::array<uint8_t, DesCrypt::BlockSize + 2> data = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44, 0x44, 0x44 };
|
||||
std::array<uint8_t, DesCrypt::KeySize> key = { 0xda, 0xec, 0x68, 0xae, 0x83, 0xe0, 0x1e, 0xab };
|
||||
|
||||
std::array<uint8_t, 12> fact = {};
|
||||
std::array<uint8_t, 12> expected = {};
|
||||
|
||||
DesCrypt::Key desKey(key.begin(), key.end());
|
||||
DesCrypt::DesDecryptor dec(desKey);
|
||||
dec.DecryptBlock(fact.begin() + 3, fact.begin() + 3, data.begin(), data.end());
|
||||
|
||||
ASSERT_EQ(expected, fact);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Impl, typename InputIt>
|
||||
static std::vector<uint8_t> EncryptThroughBase(Encryptor<Impl> & enc,
|
||||
static std::vector<uint8_t> EncryptThroughBase(const Encryptor<Impl> & enc,
|
||||
InputIt begin, InputIt end)
|
||||
{
|
||||
std::vector<uint8_t> result;
|
||||
result.resize(enc.GetBlockSize(), 0);
|
||||
|
||||
enc.EncryptBlock(result.begin(), begin, end);
|
||||
enc.EncryptBlock(result.begin(), result.end(), begin, end);
|
||||
return result;
|
||||
}
|
||||
|
||||
TEST(DesCryptTests, EncryptThroughBaseTest)
|
||||
{
|
||||
std::array<uint8_t, 8> key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 };
|
||||
std::array<uint8_t, DesCrypt::KeySize> key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 };
|
||||
|
||||
std::vector<uint8_t> data = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
|
||||
std::vector<uint8_t> expected = { 0x85, 0xe8, 0x13, 0x54, 0x0f, 0x0a, 0xb4, 0x05 };
|
||||
@@ -522,14 +492,14 @@ TEST(DesCryptTests, EncryptThroughBaseTest)
|
||||
}
|
||||
|
||||
template<typename Impl>
|
||||
static uint64_t EncryptUInt64BlockThroughBase(Encryptor<Impl> & enc, uint64_t block)
|
||||
static uint64_t EncryptUInt64BlockThroughBase(const Encryptor<Impl> & enc, uint64_t block)
|
||||
{
|
||||
return enc.EncryptBlock(block);
|
||||
}
|
||||
|
||||
TEST(DesCryptTests, EncryptUInt64BlockThroughBaseTest)
|
||||
{
|
||||
std::array<uint8_t, 8> key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 };
|
||||
std::array<uint8_t, DesCrypt::KeySize> key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 };
|
||||
|
||||
uint64_t data = 0x0123456789abcdef;
|
||||
uint64_t expected = 0x85e813540f0ab405;
|
||||
@@ -541,19 +511,19 @@ TEST(DesCryptTests, EncryptUInt64BlockThroughBaseTest)
|
||||
}
|
||||
|
||||
template<typename Impl, typename InputIt>
|
||||
static std::vector<uint8_t> DecryptThroughBase(Decryptor<Impl> & dec,
|
||||
static std::vector<uint8_t> DecryptThroughBase(const Decryptor<Impl> & dec,
|
||||
InputIt begin, InputIt end)
|
||||
{
|
||||
std::vector<uint8_t> result;
|
||||
result.resize(dec.GetBlockSize(), 0);
|
||||
|
||||
dec.DecryptBlock(result.begin(), begin, end);
|
||||
dec.DecryptBlock(result.begin(), result.end(), begin, end);
|
||||
return result;
|
||||
}
|
||||
|
||||
TEST(DesCryptTests, DecryptThroughBaseTest)
|
||||
{
|
||||
std::array<uint8_t, 8> key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 };
|
||||
std::array<uint8_t, DesCrypt::KeySize> key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 };
|
||||
|
||||
std::vector<uint8_t> data = { 0x85, 0xe8, 0x13, 0x54, 0x0f, 0x0a, 0xb4, 0x05 };
|
||||
std::vector<uint8_t> expected = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
|
||||
@@ -565,14 +535,14 @@ TEST(DesCryptTests, DecryptThroughBaseTest)
|
||||
}
|
||||
|
||||
template<typename Impl>
|
||||
static uint64_t DecryptUInt64BlockThroughBase(Decryptor<Impl> & dec, uint64_t block)
|
||||
static uint64_t DecryptUInt64BlockThroughBase(const Decryptor<Impl> & dec, uint64_t block)
|
||||
{
|
||||
return dec.DecryptBlock(block);
|
||||
}
|
||||
|
||||
TEST(DesCryptTests, DecryptUInt64BlockThroughBaseTest)
|
||||
{
|
||||
std::array<uint8_t, 8> key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 };
|
||||
std::array<uint8_t, DesCrypt::KeySize> key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 };
|
||||
|
||||
uint64_t data = 0x85e813540f0ab405;
|
||||
uint64_t expected = 0x0123456789abcdef;
|
||||
|
||||
@@ -99,8 +99,7 @@ TEST(HmacTests, LongKeyTest)
|
||||
|
||||
TEST(HmacTests, UninitializedHmacTest)
|
||||
{
|
||||
std::array<uint8_t, 10> in;
|
||||
in.fill(0);
|
||||
std::array<uint8_t, 10> in = {};
|
||||
|
||||
{
|
||||
Hmac<Md5Hasher> hmac;
|
||||
|
||||
143
ChaosTests/Padding/PadderPkcs7Tests.cpp
Normal file
143
ChaosTests/Padding/PadderPkcs7Tests.cpp
Normal file
@@ -0,0 +1,143 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
#include "Padding/PadderPkcs7.hpp"
|
||||
#include "Service/ChaosException.hpp"
|
||||
|
||||
using namespace Chaos::Padding;
|
||||
|
||||
TEST(PadPkcs7Tests, PadTest)
|
||||
{
|
||||
{
|
||||
std::array<uint8_t, 1> fact = {};
|
||||
std::array<uint8_t, 1> expected = { 0x01 };
|
||||
|
||||
PadderPkcs7::Pad(fact.begin(), fact.end());
|
||||
ASSERT_EQ(expected, fact);
|
||||
}
|
||||
|
||||
{
|
||||
std::array<uint8_t, 7> fact = {};
|
||||
std::array<uint8_t, 7> expected =
|
||||
{
|
||||
0x07, 0x07, 0x07, 0x07, 0x07,
|
||||
0x07, 0x07
|
||||
};
|
||||
|
||||
PadderPkcs7::Pad(fact.begin(), fact.end());
|
||||
ASSERT_EQ(expected, fact);
|
||||
}
|
||||
|
||||
{
|
||||
std::array<uint8_t, 10> fact = {};
|
||||
std::array<uint8_t, 10> expected =
|
||||
{
|
||||
0x0a, 0x0a, 0x0a, 0x0a, 0x0a,
|
||||
0x0a, 0x0a, 0x0a, 0x0a, 0x0a
|
||||
};
|
||||
|
||||
PadderPkcs7::Pad(fact.begin(), fact.end());
|
||||
ASSERT_EQ(expected, fact);
|
||||
}
|
||||
|
||||
for (int i = 0; i < 256; ++i)
|
||||
{
|
||||
std::vector<uint8_t> fact(i, 0x00);
|
||||
|
||||
PadderPkcs7::Pad(fact.begin(), fact.end());
|
||||
ASSERT_EQ(std::vector<uint8_t>(i, i), fact);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(PadPkcs7Tests, PadInvalidRangeTest)
|
||||
{
|
||||
{
|
||||
std::array<uint8_t, 256> out = {};
|
||||
|
||||
ASSERT_THROW(PadderPkcs7::Pad(out.begin(), out.end()), Chaos::Service::ChaosException);
|
||||
}
|
||||
|
||||
{
|
||||
std::array<uint8_t, 500> out = {};
|
||||
|
||||
ASSERT_THROW(PadderPkcs7::Pad(out.begin(), out.end()), Chaos::Service::ChaosException);
|
||||
}
|
||||
|
||||
{
|
||||
std::array<uint8_t, 50> out = {};
|
||||
|
||||
ASSERT_THROW(PadderPkcs7::Pad(out.end(), out.begin()), Chaos::Service::ChaosException);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(PadPkcs7Tests, PadOutIteratorUsageTest)
|
||||
{
|
||||
{
|
||||
std::array<uint8_t, 28> fact = {};
|
||||
std::array<uint8_t, 28> expected =
|
||||
{
|
||||
0x00, 0x00, 0x00,
|
||||
0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16,
|
||||
0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16,
|
||||
0x00, 0x00, 0x00
|
||||
};
|
||||
|
||||
PadderPkcs7::Pad(fact.begin() + 3, fact.end() - 3);
|
||||
ASSERT_EQ(expected, fact);
|
||||
}
|
||||
|
||||
{
|
||||
std::array<uint8_t, 39> fact = {};
|
||||
std::array<uint8_t, 39> expected =
|
||||
{
|
||||
0x00, 0x00, 0x00,
|
||||
0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21,
|
||||
0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21,
|
||||
0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21,
|
||||
0x00, 0x00, 0x00
|
||||
};
|
||||
|
||||
PadderPkcs7::Pad(fact.begin() + 3, fact.end() - 3);
|
||||
ASSERT_EQ(expected, fact);
|
||||
}
|
||||
|
||||
{
|
||||
std::array<uint8_t, 10> fact =
|
||||
{
|
||||
0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
|
||||
0xbb, 0xbb, 0xbb, 0xbb, 0xbb
|
||||
};
|
||||
std::array<uint8_t, 10> expected =
|
||||
{
|
||||
0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
|
||||
0xbb, 0xbb, 0xbb, 0xbb, 0xbb
|
||||
};
|
||||
|
||||
PadderPkcs7::Pad(fact.begin() + 5, fact.begin() + 5);
|
||||
ASSERT_EQ(expected, fact);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Impl, typename OutputIt>
|
||||
void PadThroughBase(const Padder<Impl> & padder, OutputIt begin, OutputIt end)
|
||||
{
|
||||
padder.Pad(begin, end);
|
||||
}
|
||||
|
||||
TEST(PadPkcs7Tests, PadThroughBaseTest)
|
||||
{
|
||||
{
|
||||
std::array<uint8_t, 5> fact = {};
|
||||
std::array<uint8_t, 5> expected =
|
||||
{
|
||||
0x05, 0x05, 0x05, 0x05, 0x05
|
||||
};
|
||||
|
||||
const PadderPkcs7 padder;
|
||||
PadThroughBase(padder, fact.begin(), fact.end());
|
||||
|
||||
ASSERT_EQ(expected, fact);
|
||||
}
|
||||
}
|
||||
32
ChaosTests/Service/ChaosExceptionTests.cpp
Normal file
32
ChaosTests/Service/ChaosExceptionTests.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <string>
|
||||
|
||||
#include "Service/ChaosException.hpp"
|
||||
|
||||
using namespace Chaos::Service;
|
||||
|
||||
TEST(ChaosExceptionTests, RvalueRefCtorTest)
|
||||
{
|
||||
try
|
||||
{
|
||||
throw ChaosException("everything's alright :D");
|
||||
}
|
||||
catch (const ChaosException & ex)
|
||||
{
|
||||
ASSERT_EQ("everything's alright :D", ex.GetMessage());
|
||||
}
|
||||
}
|
||||
|
||||
TEST(ChaosExceptionTests, ConstLvalueRefCtorTest)
|
||||
{
|
||||
const std::string message = "everything's alright :D";
|
||||
|
||||
try
|
||||
{
|
||||
throw ChaosException(message);
|
||||
}
|
||||
catch (const ChaosException & ex)
|
||||
{
|
||||
ASSERT_EQ("everything's alright :D", ex.GetMessage());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user