PadderIso7816: Do not allow to pad empty ranges.
Chaos Ci / test (push) Successful in 2m39s
Chaos Ci / benchmark (push) Successful in 1m50s

This commit is contained in:
hashlag
2026-08-13 01:22:14 +03:00
parent ac122f0f9f
commit 8adc867a2e
2 changed files with 21 additions and 17 deletions
+5
View File
@@ -7,6 +7,7 @@
#include "Padding/Padder.hpp" #include "Padding/Padder.hpp"
#include "Service/Branchless.hpp" #include "Service/Branchless.hpp"
#include "Service/ChaosException.hpp"
namespace Chaos::Padding namespace Chaos::Padding
{ {
@@ -28,6 +29,10 @@ public:
*it = 0; *it = 0;
} }
} }
else
{
throw Service::ChaosException("PadderIso7816::Pad(): invalid range");
}
} }
struct ComputeUnpadResult struct ComputeUnpadResult
+16 -17
View File
@@ -1,5 +1,6 @@
#include <algorithm> #include <algorithm>
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "TestHelpers/AssertThrowEx.hpp"
#include <array> #include <array>
#include <cstdint> #include <cstdint>
@@ -8,6 +9,7 @@
#include "Padding/PadderIso7816.hpp" #include "Padding/PadderIso7816.hpp"
#include "Padding/Padder.hpp" #include "Padding/Padder.hpp"
#include "Service/ChaosException.hpp"
using namespace Chaos::Padding; using namespace Chaos::Padding;
@@ -45,7 +47,7 @@ TEST(PadIso7816Tests, PadTest)
ASSERT_EQ(expected, fact); ASSERT_EQ(expected, fact);
} }
for (int i = 0; i < 256; ++i) for (int i = 1; i < 256; ++i)
{ {
std::vector<uint8_t> fact(i, 0xff); std::vector<uint8_t> fact(i, 0xff);
@@ -58,6 +60,19 @@ TEST(PadIso7816Tests, PadTest)
} }
} }
TEST(PadIso7816Tests, PadInvalidRangeTest)
{
{
std::array<uint8_t, 3> out = {};
ASSERT_THROW_EX(PadderIso7816::Pad(out.begin(), out.begin()),
Chaos::Service::ChaosException,
{
ASSERT_EQ("PadderIso7816::Pad(): invalid range", ex.GetMessage());
});
}
}
TEST(PadIso7816Tests, PadOutIteratorUsageTest) TEST(PadIso7816Tests, PadOutIteratorUsageTest)
{ {
{ {
@@ -92,22 +107,6 @@ TEST(PadIso7816Tests, PadOutIteratorUsageTest)
PadderIso7816::Pad(fact.begin() + 3, fact.end() - 3); PadderIso7816::Pad(fact.begin() + 3, fact.end() - 3);
ASSERT_EQ(expected, fact); 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> template<typename Impl, typename OutputIt>