Add ISO/IEC 7816-4 padding implementation.
Chaos Ci / test-and-benchmark (push) Successful in 2m8s

This commit is contained in:
hashlag
2026-06-18 01:28:38 +03:00
parent 5a16d154a2
commit eb3da5ff75
3 changed files with 165 additions and 0 deletions
+33
View File
@@ -0,0 +1,33 @@
#ifndef CHAOS_PADDING_PADDERISO7816_HPP
#define CHAOS_PADDING_PADDERISO7816_HPP
#include <cstdint>
#include "Padding/Padder.hpp"
namespace Chaos::Padding
{
class PadderIso7816 : public Padder<PadderIso7816>
{
public:
template<typename OutputIt>
static void Pad(OutputIt begin, OutputIt end)
{
OutputIt it = begin;
if (it != end)
{
*it++ = static_cast<uint8_t>(0x80);
for (; it != end; ++it)
{
*it = 0;
}
}
}
};
} // namespace Chaos::Padding
#endif // CHAOS_PADDING_PADDERISO7816_HPP
+1
View File
@@ -19,6 +19,7 @@ set(ChaosTests_SOURCE Hash/Md4HasherTests.cpp
Cipher/Arc4CryptTests.cpp
Cipher/DesCryptTests.cpp
Padding/PadderPkcs7Tests.cpp
Padding/PadderIso7816Tests.cpp
Service/SeArrayTests.cpp
Service/ChaosExceptionTests.cpp)
+131
View File
@@ -0,0 +1,131 @@
#include <gtest/gtest.h>
#include <array>
#include <cstdint>
#include <vector>
#include "Padding/PadderIso7816.hpp"
using namespace Chaos::Padding;
TEST(PadIso7816Tests, PadTest)
{
{
std::array<uint8_t, 1> fact = {};
std::array<uint8_t, 1> expected = { 0x80 };
PadderIso7816::Pad(fact.begin(), fact.end());
ASSERT_EQ(expected, fact);
}
{
std::array<uint8_t, 7> fact = {};
std::array<uint8_t, 7> expected =
{
0x80, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00
};
PadderIso7816::Pad(fact.begin(), fact.end());
ASSERT_EQ(expected, fact);
}
{
std::array<uint8_t, 10> fact = {};
std::array<uint8_t, 10> expected =
{
0x80, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00
};
PadderIso7816::Pad(fact.begin(), fact.end());
ASSERT_EQ(expected, fact);
}
for (int i = 0; i < 256; ++i)
{
std::vector<uint8_t> fact(i, 0xff);
PadderIso7816::Pad(fact.begin(), fact.end());
for (int j = 0; j < i; ++j)
{
ASSERT_EQ(j == 0 ? 0x80 : 0x00, fact[j]);
}
}
}
TEST(PadIso7816Tests, PadOutIteratorUsageTest)
{
{
std::array<uint8_t, 28> fact;
fact.fill(0xff);
std::array<uint8_t, 28> expected =
{
0xff, 0xff, 0xff,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xff, 0xff, 0xff
};
PadderIso7816::Pad(fact.begin() + 3, fact.end() - 3);
ASSERT_EQ(expected, fact);
}
{
std::array<uint8_t, 39> fact;
fact.fill(0xff);
std::array<uint8_t, 39> expected =
{
0xff, 0xff, 0xff,
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,
0xff, 0xff, 0xff
};
PadderIso7816::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
};
PadderIso7816::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(PadIso7816Tests, PadThroughBaseTest)
{
{
std::array<uint8_t, 5> fact;
fact.fill(0xff);
std::array<uint8_t, 5> expected =
{
0x80, 0x00, 0x00, 0x00, 0x00
};
const PadderIso7816 padder;
PadThroughBase(padder, fact.begin(), fact.end());
ASSERT_EQ(expected, fact);
}
}