Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
797a5428cc | ||
|
|
5baede0a1d | ||
|
|
2a3185406b |
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
|
||||||
@@ -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)
|
||||||
|
|
||||||
|
|||||||
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