Implement unpad for ISO 7816 padding.
This commit is contained in:
@@ -1,9 +1,12 @@
|
|||||||
#ifndef CHAOS_PADDING_PADDERISO7816_HPP
|
#ifndef CHAOS_PADDING_PADDERISO7816_HPP
|
||||||
#define CHAOS_PADDING_PADDERISO7816_HPP
|
#define CHAOS_PADDING_PADDERISO7816_HPP
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <limits>
|
||||||
|
|
||||||
#include "Padding/Padder.hpp"
|
#include "Padding/Padder.hpp"
|
||||||
|
#include "Service/Branchless.hpp"
|
||||||
|
|
||||||
namespace Chaos::Padding
|
namespace Chaos::Padding
|
||||||
{
|
{
|
||||||
@@ -26,6 +29,52 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct ComputeUnpadResult
|
||||||
|
{
|
||||||
|
bool IsOkay_;
|
||||||
|
uint8_t PadSize_;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename InputIt>
|
||||||
|
static ComputeUnpadResult ComputeUnpad(InputIt begin, InputIt end) noexcept
|
||||||
|
{
|
||||||
|
if (begin == end)
|
||||||
|
{
|
||||||
|
return { .IsOkay_ = false, .PadSize_ = 0 };
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t padSizeIncrement = 1;
|
||||||
|
uint8_t padSize = 0;
|
||||||
|
|
||||||
|
uint8_t encountered0x80 = 0x00;
|
||||||
|
uint8_t onlyZerosPast0x80 = 0xFF;
|
||||||
|
|
||||||
|
InputIt it = end;
|
||||||
|
while (it != begin && padSize < std::numeric_limits<uint8_t>::max())
|
||||||
|
{
|
||||||
|
--it;
|
||||||
|
|
||||||
|
const uint8_t byte = *it;
|
||||||
|
|
||||||
|
padSizeIncrement = Branchless::Sel<uint8_t>(encountered0x80, 0, 1);
|
||||||
|
encountered0x80 |= Branchless::Eq<uint8_t, uint8_t>(byte, 0x80);
|
||||||
|
onlyZerosPast0x80 &= Branchless::IsZero<uint8_t, uint8_t>(byte) | encountered0x80;
|
||||||
|
|
||||||
|
padSize += padSizeIncrement;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t isOkay = encountered0x80 & onlyZerosPast0x80;
|
||||||
|
|
||||||
|
return
|
||||||
|
{
|
||||||
|
.IsOkay_ = static_cast<bool>(isOkay),
|
||||||
|
.PadSize_ = Branchless::Sel<uint8_t>(isOkay, padSize, 0)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
using Branchless = Service::Branchless;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Chaos::Padding
|
} // namespace Chaos::Padding
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
|
#include <algorithm>
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <iterator>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "Padding/PadderIso7816.hpp"
|
#include "Padding/PadderIso7816.hpp"
|
||||||
@@ -131,3 +133,213 @@ TEST(PadIso7816Tests, PadThroughBaseTest)
|
|||||||
ASSERT_EQ(expected, fact);
|
ASSERT_EQ(expected, fact);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(PadIso7816Tests, UnpadTest)
|
||||||
|
{
|
||||||
|
{
|
||||||
|
std::array<uint8_t, 1> data = { 0x80 };
|
||||||
|
|
||||||
|
auto result = PadderIso7816::ComputeUnpad(data.begin(), data.end());
|
||||||
|
|
||||||
|
ASSERT_TRUE(result.IsOkay_);
|
||||||
|
ASSERT_EQ(1, result.PadSize_);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
std::array<uint8_t, 4> data = { 0xaa, 0xbb, 0x80, 0x00 };
|
||||||
|
|
||||||
|
auto result = PadderIso7816::ComputeUnpad(data.begin(), data.end());
|
||||||
|
|
||||||
|
ASSERT_TRUE(result.IsOkay_);
|
||||||
|
ASSERT_EQ(2, result.PadSize_);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
std::array<uint8_t, 10> data = { 0xaa, 0xbb, 0xcc, 0x80, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00 };
|
||||||
|
|
||||||
|
auto result = PadderIso7816::ComputeUnpad(data.begin(), data.end());
|
||||||
|
|
||||||
|
ASSERT_TRUE(result.IsOkay_);
|
||||||
|
ASSERT_EQ(7, result.PadSize_);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
std::array<uint8_t, 10> data = { 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5,
|
||||||
|
0xa6, 0xa7, 0xa8, 0x80 };
|
||||||
|
|
||||||
|
auto result = PadderIso7816::ComputeUnpad(data.begin(), data.end());
|
||||||
|
|
||||||
|
ASSERT_TRUE(result.IsOkay_);
|
||||||
|
ASSERT_EQ(1, result.PadSize_);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
std::array<uint8_t, 10> data = { 0xa0, 0xa1, 0xa2, 0x80, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00 };
|
||||||
|
|
||||||
|
auto result = PadderIso7816::ComputeUnpad(data.begin(), data.end());
|
||||||
|
|
||||||
|
ASSERT_TRUE(result.IsOkay_);
|
||||||
|
ASSERT_EQ(7, result.PadSize_);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
std::array<uint8_t, 10> data = { 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00 };
|
||||||
|
|
||||||
|
auto result = PadderIso7816::ComputeUnpad(data.begin(), data.end());
|
||||||
|
|
||||||
|
ASSERT_TRUE(result.IsOkay_);
|
||||||
|
ASSERT_EQ(10, result.PadSize_);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
std::array<uint8_t, 4> data = { 0x80, 0x80, 0x00, 0x00 };
|
||||||
|
|
||||||
|
auto result = PadderIso7816::ComputeUnpad(data.begin(), data.end());
|
||||||
|
|
||||||
|
ASSERT_TRUE(result.IsOkay_);
|
||||||
|
ASSERT_EQ(3, result.PadSize_);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
std::array<uint8_t, 200> data;
|
||||||
|
data.fill(0x41);
|
||||||
|
*std::prev(data.end()) = 0x80;
|
||||||
|
|
||||||
|
auto result = PadderIso7816::ComputeUnpad(data.begin(), data.end());
|
||||||
|
|
||||||
|
ASSERT_TRUE(result.IsOkay_);
|
||||||
|
ASSERT_EQ(1, result.PadSize_);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
std::array<uint8_t, 200> data;
|
||||||
|
data.fill(0x41);
|
||||||
|
*std::prev(data.end(), 100) = 0x80;
|
||||||
|
std::fill(data.end() - 99, data.end(), 0);
|
||||||
|
|
||||||
|
auto result = PadderIso7816::ComputeUnpad(data.begin(), data.end());
|
||||||
|
|
||||||
|
ASSERT_TRUE(result.IsOkay_);
|
||||||
|
ASSERT_EQ(100, result.PadSize_);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
std::array<uint8_t, 255> data;
|
||||||
|
data.fill(0);
|
||||||
|
*data.begin() = 0x80;
|
||||||
|
|
||||||
|
auto result = PadderIso7816::ComputeUnpad(data.begin(), data.end());
|
||||||
|
|
||||||
|
ASSERT_TRUE(result.IsOkay_);
|
||||||
|
ASSERT_EQ(255, result.PadSize_);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
std::array<uint8_t, 381> data;
|
||||||
|
data.fill(0x9a);
|
||||||
|
*std::prev(data.end(), 255) = 0x80;
|
||||||
|
std::fill(data.end() - 254, data.end(), 0);
|
||||||
|
|
||||||
|
auto result = PadderIso7816::ComputeUnpad(data.begin(), data.end());
|
||||||
|
|
||||||
|
ASSERT_TRUE(result.IsOkay_);
|
||||||
|
ASSERT_EQ(255, result.PadSize_);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PadIso7816Tests, UnpadErrorTest)
|
||||||
|
{
|
||||||
|
{
|
||||||
|
std::array<uint8_t, 0> data = { };
|
||||||
|
|
||||||
|
auto result = PadderIso7816::ComputeUnpad(data.begin(), data.end());
|
||||||
|
|
||||||
|
ASSERT_FALSE(result.IsOkay_);
|
||||||
|
ASSERT_EQ(0, result.PadSize_);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
std::array<uint8_t, 1> data = { 0x00 };
|
||||||
|
|
||||||
|
auto result = PadderIso7816::ComputeUnpad(data.begin(), data.end());
|
||||||
|
|
||||||
|
ASSERT_FALSE(result.IsOkay_);
|
||||||
|
ASSERT_EQ(0, result.PadSize_);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
std::array<uint8_t, 5> data = { 0xa0, 0xa1, 0xa2, 0x80, 0xff };
|
||||||
|
|
||||||
|
auto result = PadderIso7816::ComputeUnpad(data.begin(), data.end());
|
||||||
|
|
||||||
|
ASSERT_FALSE(result.IsOkay_);
|
||||||
|
ASSERT_EQ(0, result.PadSize_);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
std::array<uint8_t, 5> data = { 0xa0, 0xa1, 0x00, 0x00, 0x00 };
|
||||||
|
|
||||||
|
auto result = PadderIso7816::ComputeUnpad(data.begin(), data.end());
|
||||||
|
|
||||||
|
ASSERT_FALSE(result.IsOkay_);
|
||||||
|
ASSERT_EQ(0, result.PadSize_);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
std::array<uint8_t, 5> data = { 0x80, 0x00, 0xa2, 0x00, 0x00 };
|
||||||
|
|
||||||
|
auto result = PadderIso7816::ComputeUnpad(data.begin(), data.end());
|
||||||
|
|
||||||
|
ASSERT_FALSE(result.IsOkay_);
|
||||||
|
ASSERT_EQ(0, result.PadSize_);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
std::array<uint8_t, 5> data = { 0xa0, 0x80, 0x03, 0x00, 0x00 };
|
||||||
|
|
||||||
|
auto result = PadderIso7816::ComputeUnpad(data.begin(), data.end());
|
||||||
|
|
||||||
|
ASSERT_FALSE(result.IsOkay_);
|
||||||
|
ASSERT_EQ(0, result.PadSize_);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
std::array<uint8_t, 1> data = { 0xff };
|
||||||
|
|
||||||
|
auto result = PadderIso7816::ComputeUnpad(data.begin(), data.end());
|
||||||
|
|
||||||
|
ASSERT_FALSE(result.IsOkay_);
|
||||||
|
ASSERT_EQ(0, result.PadSize_);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
std::array<uint8_t, 2> data = { 0x00, 0x00 };
|
||||||
|
|
||||||
|
auto result = PadderIso7816::ComputeUnpad(data.begin(), data.end());
|
||||||
|
|
||||||
|
ASSERT_FALSE(result.IsOkay_);
|
||||||
|
ASSERT_EQ(0, result.PadSize_);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Impl, typename OutputIt>
|
||||||
|
auto ComputeUnpadThroughBase(const Padder<Impl> & padder, OutputIt begin, OutputIt end)
|
||||||
|
{
|
||||||
|
return padder.ComputeUnpad(begin, end);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PadIso7816Tests, ComputeUnpadThroughBaseTest)
|
||||||
|
{
|
||||||
|
{
|
||||||
|
std::array<uint8_t, 5> data = { 0x80, 0x00, 0x00, 0x00, 0x00 };
|
||||||
|
|
||||||
|
const PadderIso7816 padder;
|
||||||
|
auto result = ComputeUnpadThroughBase(padder, data.begin(), data.end());
|
||||||
|
|
||||||
|
ASSERT_TRUE(result.IsOkay_);
|
||||||
|
ASSERT_EQ(5, result.PadSize_);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user