Compare commits
8 Commits
ccf1397595
...
pad
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
797a5428cc | ||
|
|
5baede0a1d | ||
|
|
2a3185406b | ||
|
|
0647a7c3dc | ||
|
|
7c86d704b7 | ||
|
|
9f6265395d | ||
|
|
151c93560c | ||
|
|
3059bd4c66 |
@@ -198,8 +198,10 @@ class DesCrypt
|
|||||||
public:
|
public:
|
||||||
using Block = uint64_t;
|
using Block = uint64_t;
|
||||||
static constexpr size_t BlockSize = 8;
|
static constexpr size_t BlockSize = 8;
|
||||||
|
static constexpr size_t KeySize = 8;
|
||||||
|
|
||||||
static_assert(BlockSize == sizeof(Block));
|
static_assert(BlockSize == sizeof(Block));
|
||||||
|
static_assert(KeySize == Inner_::RawKey::Size());
|
||||||
|
|
||||||
DesCrypt() = delete;
|
DesCrypt() = delete;
|
||||||
|
|
||||||
@@ -231,7 +233,9 @@ public:
|
|||||||
class DesEncryptor : public Encryptor<DesEncryptor>
|
class DesEncryptor : public Encryptor<DesEncryptor>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
using Key = DesCrypt::Key;
|
||||||
static constexpr size_t BlockSize = DesCrypt::BlockSize;
|
static constexpr size_t BlockSize = DesCrypt::BlockSize;
|
||||||
|
static constexpr size_t KeySize = DesCrypt::KeySize;
|
||||||
|
|
||||||
DesEncryptor(const Key & key)
|
DesEncryptor(const Key & key)
|
||||||
: Schedule_(Inner_::KeySchedule::Direction::Encrypt, key.Key_)
|
: Schedule_(Inner_::KeySchedule::Direction::Encrypt, key.Key_)
|
||||||
@@ -274,7 +278,9 @@ public:
|
|||||||
class DesDecryptor : public Decryptor<DesDecryptor>
|
class DesDecryptor : public Decryptor<DesDecryptor>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
using Key = DesCrypt::Key;
|
||||||
static constexpr size_t BlockSize = DesCrypt::BlockSize;
|
static constexpr size_t BlockSize = DesCrypt::BlockSize;
|
||||||
|
static constexpr size_t KeySize = DesCrypt::KeySize;
|
||||||
|
|
||||||
DesDecryptor(const Key & key)
|
DesDecryptor(const Key & key)
|
||||||
: Schedule_(Inner_::KeySchedule::Direction::Decrypt, key.Key_)
|
: Schedule_(Inner_::KeySchedule::Direction::Decrypt, key.Key_)
|
||||||
|
|||||||
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();
|
return Storage_.data() + Storage_.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr size_t Size() const noexcept
|
|
||||||
{
|
|
||||||
return Storage_.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Fill(const T & value)
|
void Fill(const T & value)
|
||||||
{
|
{
|
||||||
Storage_.fill(value);
|
Storage_.fill(value);
|
||||||
@@ -73,6 +68,11 @@ public:
|
|||||||
EraseImpl();
|
EraseImpl();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static constexpr size_t Size() noexcept
|
||||||
|
{
|
||||||
|
return S;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::array<T, S> Storage_;
|
std::array<T, S> Storage_;
|
||||||
|
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ set(ChaosTests_SOURCE Hash/Md4HasherTests.cpp
|
|||||||
Cipher/Arc4GenTests.cpp
|
Cipher/Arc4GenTests.cpp
|
||||||
Cipher/Arc4CryptTests.cpp
|
Cipher/Arc4CryptTests.cpp
|
||||||
Cipher/DesCryptTests.cpp
|
Cipher/DesCryptTests.cpp
|
||||||
|
Padding/PadderPkcs7Tests.cpp
|
||||||
Service/SeArrayTests.cpp
|
Service/SeArrayTests.cpp
|
||||||
Service/ChaosExceptionTests.cpp)
|
Service/ChaosExceptionTests.cpp)
|
||||||
|
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ TEST(DesCryptTests, EncryptTest)
|
|||||||
struct Helper
|
struct Helper
|
||||||
{
|
{
|
||||||
std::array<uint8_t, DesCrypt::BlockSize> operator()(const std::array<uint8_t, DesCrypt::BlockSize> & data,
|
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 = {};
|
std::array<uint8_t, DesCrypt::BlockSize> result = {};
|
||||||
|
|
||||||
@@ -60,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, 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 };
|
std::array<uint8_t, DesCrypt::BlockSize> expected = { 0x85, 0xe8, 0x13, 0x54, 0x0f, 0x0a, 0xb4, 0x05 };
|
||||||
|
|
||||||
@@ -69,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, 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 };
|
std::array<uint8_t, DesCrypt::BlockSize> expected = { 0x07, 0xe8, 0x7f, 0xaa, 0xb3, 0x17, 0x13, 0x18 };
|
||||||
|
|
||||||
@@ -78,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, 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 };
|
std::array<uint8_t, DesCrypt::BlockSize> expected = { 0x42, 0x27, 0x88, 0xa6, 0x7b, 0x6c, 0x18, 0xed };
|
||||||
|
|
||||||
@@ -91,7 +91,7 @@ TEST(DesCryptTests, EncryptUInt64BlockTest)
|
|||||||
struct Helper
|
struct Helper
|
||||||
{
|
{
|
||||||
uint64_t operator()(uint64_t data,
|
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::Key desKey(key.begin(), key.end());
|
||||||
DesCrypt::DesEncryptor enc(desKey);
|
DesCrypt::DesEncryptor enc(desKey);
|
||||||
@@ -104,7 +104,7 @@ TEST(DesCryptTests, EncryptUInt64BlockTest)
|
|||||||
|
|
||||||
{
|
{
|
||||||
uint64_t data = 0x0123456789abcdef;
|
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;
|
uint64_t expected = 0x85e813540f0ab405;
|
||||||
|
|
||||||
@@ -113,7 +113,7 @@ TEST(DesCryptTests, EncryptUInt64BlockTest)
|
|||||||
|
|
||||||
{
|
{
|
||||||
uint64_t data = 0xaaf383162d2e6bcb;
|
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;
|
uint64_t expected = 0x07e87faab3171318;
|
||||||
|
|
||||||
@@ -122,7 +122,7 @@ TEST(DesCryptTests, EncryptUInt64BlockTest)
|
|||||||
|
|
||||||
{
|
{
|
||||||
uint64_t data = 0xe51a9fd419a79344;
|
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;
|
uint64_t expected = 0x422788a67b6c18ed;
|
||||||
|
|
||||||
@@ -203,7 +203,7 @@ TEST(DesCryptTests, DecryptTest)
|
|||||||
struct Helper
|
struct Helper
|
||||||
{
|
{
|
||||||
std::array<uint8_t, DesCrypt::BlockSize> operator()(const std::array<uint8_t, DesCrypt::BlockSize> & data,
|
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 = {};
|
std::array<uint8_t, DesCrypt::BlockSize> result = {};
|
||||||
|
|
||||||
@@ -219,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, 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 };
|
std::array<uint8_t, DesCrypt::BlockSize> expected = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
|
||||||
|
|
||||||
@@ -228,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, 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 };
|
std::array<uint8_t, DesCrypt::BlockSize> expected = { 0xaa, 0xf3, 0x83, 0x16, 0x2d, 0x2e, 0x6b, 0xcb };
|
||||||
|
|
||||||
@@ -237,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, 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 };
|
std::array<uint8_t, DesCrypt::BlockSize> expected = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44 };
|
||||||
|
|
||||||
@@ -250,7 +250,7 @@ TEST(DesCryptTests, DecryptUInt64BlockTest)
|
|||||||
struct Helper
|
struct Helper
|
||||||
{
|
{
|
||||||
uint64_t operator()(uint64_t data,
|
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::Key desKey(key.begin(), key.end());
|
||||||
DesCrypt::DesDecryptor dec(desKey);
|
DesCrypt::DesDecryptor dec(desKey);
|
||||||
@@ -263,7 +263,7 @@ TEST(DesCryptTests, DecryptUInt64BlockTest)
|
|||||||
|
|
||||||
{
|
{
|
||||||
uint64_t data = 0x85e813540f0ab405;
|
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;
|
uint64_t expected = 0x0123456789abcdef;
|
||||||
|
|
||||||
@@ -272,7 +272,7 @@ TEST(DesCryptTests, DecryptUInt64BlockTest)
|
|||||||
|
|
||||||
{
|
{
|
||||||
uint64_t data = 0x07e87faab3171318;
|
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;
|
uint64_t expected = 0xaaf383162d2e6bcb;
|
||||||
|
|
||||||
@@ -281,7 +281,7 @@ TEST(DesCryptTests, DecryptUInt64BlockTest)
|
|||||||
|
|
||||||
{
|
{
|
||||||
uint64_t data = 0x422788a67b6c18ed;
|
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;
|
uint64_t expected = 0xe51a9fd419a79344;
|
||||||
|
|
||||||
@@ -360,7 +360,7 @@ TEST(DesCryptTests, DecryptLongDataTest)
|
|||||||
TEST(DesCryptTests, ShortKeyTest)
|
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);
|
ASSERT_THROW(DesCrypt::Key(key.begin(), key.end()), Chaos::Service::ChaosException);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -368,7 +368,7 @@ TEST(DesCryptTests, ShortKeyTest)
|
|||||||
TEST(DesCryptTests, LongKeyTest)
|
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);
|
ASSERT_THROW(DesCrypt::Key(key.begin(), key.end()), Chaos::Service::ChaosException);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -377,7 +377,7 @@ TEST(DesCryptTests, OutIteratorUsageEncryptTest)
|
|||||||
{
|
{
|
||||||
{
|
{
|
||||||
std::array<uint8_t, DesCrypt::BlockSize> data = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44 };
|
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, 11> fact = {};
|
std::array<uint8_t, 11> fact = {};
|
||||||
// First and last 3 bytes should be untouched.
|
// First and last 3 bytes should be untouched.
|
||||||
@@ -392,7 +392,7 @@ TEST(DesCryptTests, OutIteratorUsageEncryptTest)
|
|||||||
|
|
||||||
{
|
{
|
||||||
std::array<uint8_t, DesCrypt::BlockSize + 2> data = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44, 0x44, 0x44 };
|
std::array<uint8_t, DesCrypt::BlockSize + 2> data = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44, 0x44, 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, 12> fact = {};
|
std::array<uint8_t, 12> fact = {};
|
||||||
// First and last 4 bytes should be untouched.
|
// First and last 4 bytes should be untouched.
|
||||||
@@ -407,7 +407,7 @@ TEST(DesCryptTests, OutIteratorUsageEncryptTest)
|
|||||||
|
|
||||||
{
|
{
|
||||||
std::array<uint8_t, DesCrypt::BlockSize + 2> data = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44, 0x44, 0x44 };
|
std::array<uint8_t, DesCrypt::BlockSize + 2> data = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44, 0x44, 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, 12> fact = {};
|
std::array<uint8_t, 12> fact = {};
|
||||||
std::array<uint8_t, 12> expected = {};
|
std::array<uint8_t, 12> expected = {};
|
||||||
@@ -424,7 +424,7 @@ TEST(DesCryptTests, OutIteratorUsageDecryptTest)
|
|||||||
{
|
{
|
||||||
{
|
{
|
||||||
std::array<uint8_t, DesCrypt::BlockSize> data = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44 };
|
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, 11> fact = {};
|
std::array<uint8_t, 11> fact = {};
|
||||||
// First and last 3 bytes should be untouched.
|
// First and last 3 bytes should be untouched.
|
||||||
@@ -439,7 +439,7 @@ TEST(DesCryptTests, OutIteratorUsageDecryptTest)
|
|||||||
|
|
||||||
{
|
{
|
||||||
std::array<uint8_t, DesCrypt::BlockSize + 2> data = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44, 0x44, 0x44 };
|
std::array<uint8_t, DesCrypt::BlockSize + 2> data = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44, 0x44, 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, 12> fact = {};
|
std::array<uint8_t, 12> fact = {};
|
||||||
// First and last 4 bytes should be untouched.
|
// First and last 4 bytes should be untouched.
|
||||||
@@ -454,7 +454,7 @@ TEST(DesCryptTests, OutIteratorUsageDecryptTest)
|
|||||||
|
|
||||||
{
|
{
|
||||||
std::array<uint8_t, DesCrypt::BlockSize + 2> data = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44, 0x44, 0x44 };
|
std::array<uint8_t, DesCrypt::BlockSize + 2> data = { 0xe5, 0x1a, 0x9f, 0xd4, 0x19, 0xa7, 0x93, 0x44, 0x44, 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, 12> fact = {};
|
std::array<uint8_t, 12> fact = {};
|
||||||
std::array<uint8_t, 12> expected = {};
|
std::array<uint8_t, 12> expected = {};
|
||||||
@@ -480,7 +480,7 @@ static std::vector<uint8_t> EncryptThroughBase(const Encryptor<Impl> & enc,
|
|||||||
|
|
||||||
TEST(DesCryptTests, EncryptThroughBaseTest)
|
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> data = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
|
||||||
std::vector<uint8_t> expected = { 0x85, 0xe8, 0x13, 0x54, 0x0f, 0x0a, 0xb4, 0x05 };
|
std::vector<uint8_t> expected = { 0x85, 0xe8, 0x13, 0x54, 0x0f, 0x0a, 0xb4, 0x05 };
|
||||||
@@ -499,7 +499,7 @@ static uint64_t EncryptUInt64BlockThroughBase(const Encryptor<Impl> & enc, uint6
|
|||||||
|
|
||||||
TEST(DesCryptTests, EncryptUInt64BlockThroughBaseTest)
|
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 data = 0x0123456789abcdef;
|
||||||
uint64_t expected = 0x85e813540f0ab405;
|
uint64_t expected = 0x85e813540f0ab405;
|
||||||
@@ -523,7 +523,7 @@ static std::vector<uint8_t> DecryptThroughBase(const Decryptor<Impl> & dec,
|
|||||||
|
|
||||||
TEST(DesCryptTests, DecryptThroughBaseTest)
|
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> data = { 0x85, 0xe8, 0x13, 0x54, 0x0f, 0x0a, 0xb4, 0x05 };
|
||||||
std::vector<uint8_t> expected = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
|
std::vector<uint8_t> expected = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
|
||||||
@@ -542,7 +542,7 @@ static uint64_t DecryptUInt64BlockThroughBase(const Decryptor<Impl> & dec, uint6
|
|||||||
|
|
||||||
TEST(DesCryptTests, DecryptUInt64BlockThroughBaseTest)
|
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 data = 0x85e813540f0ab405;
|
||||||
uint64_t expected = 0x0123456789abcdef;
|
uint64_t expected = 0x0123456789abcdef;
|
||||||
|
|||||||
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user