Skip to content

Commit ec25ac9

Browse files
Updated Pkcs7 padding test
1 parent f6545c7 commit ec25ac9

File tree

1 file changed

+35
-22
lines changed

1 file changed

+35
-22
lines changed

source/obsidian-test/tests/test_encodings.cpp

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "source/include/obsidian/encoding.hpp"
44
#include "source/include/obsidian/random.hpp"
5+
56
using namespace obsidian;
67

78
const std::string& QBF_TEST_STRING =
@@ -65,6 +66,9 @@ bool pkcs7_validator(const std::vector<uint8_t>& bytes,
6566
const uint8_t& last_byte = *bytes.cend();
6667

6768
if(last_byte != correct_difference) {
69+
std::cout << "Difference is " << static_cast<uint32_t>(last_byte)
70+
<< " should have been " << correct_difference << "\n";
71+
6872
return false;
6973
}
7074

@@ -74,33 +78,42 @@ bool pkcs7_validator(const std::vector<uint8_t>& bytes,
7478
[&](const auto& byte) -> bool { return byte == last_byte; });
7579

7680
if(!valid_padding) {
81+
for(const auto& byte : bytes) {
82+
std::cout << std::hex << std::setw(2) << std::setfill('0')
83+
<< static_cast<uint32_t>(byte) << ", ";
84+
}
85+
86+
std::cout << '\n';
87+
7788
return false;
7889
}
7990

8091
return true;
8192
}
8293

83-
TEST(Encodings, Pkcs7KnownPad)
94+
TEST(Encodings, Pkcs7Padding)
8495
{
85-
const std::vector<uint8_t>& sample_15b {
86-
0xFF,
87-
0xFF,
88-
0xFF,
89-
0xFF,
90-
0xFF,
91-
0xFF,
92-
0xFF,
93-
0xFF,
94-
0xFF,
95-
0xFF,
96-
0xFF,
97-
0xFF,
98-
0xFF,
99-
0xFF,
100-
0xFF,
101-
};
102-
103-
const auto& padded = encoding::apply_pkcs7_padding(sample_15b, 16);
104-
105-
ASSERT_EQ(pkcs7_validator(sample_15b, 1), true);
96+
const auto& padding_factor = 16;
97+
const auto& fill_byte = 0xFF;
98+
99+
std::vector<uint8_t> sample(64, fill_byte);
100+
101+
for(uint32_t i = 0; i < 64; ++i) {
102+
const auto& padded = encoding::apply_pkcs7_padding(
103+
{sample.begin(), sample.end() - i}, padding_factor);
104+
105+
const auto& last_byte = padded[padded.size() - 1];
106+
107+
if(i % padding_factor) {
108+
const auto& delta = i % padding_factor;
109+
110+
ASSERT_EQ(last_byte, i % padding_factor);
111+
112+
for(auto i = 0; i < delta; ++i) {
113+
ASSERT_EQ(padded[padded.size() - 1 - i], delta);
114+
}
115+
} else {
116+
ASSERT_EQ(last_byte, fill_byte);
117+
}
118+
}
106119
}

0 commit comments

Comments
 (0)