aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/test_pad.cpp
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2016-11-15 20:08:22 -0500
committerJack Lloyd <[email protected]>2016-11-15 20:08:22 -0500
commitdb3a868cb8c477dbd909bed53879f2124a443306 (patch)
tree78bed218f47d93f28685075a8f8540d40d00360f /src/tests/test_pad.cpp
parentca972385dd259f1a80d37b153a504fc14b370795 (diff)
Add negative tests for cipher mode padding
Diffstat (limited to 'src/tests/test_pad.cpp')
-rw-r--r--src/tests/test_pad.cpp64
1 files changed, 38 insertions, 26 deletions
diff --git a/src/tests/test_pad.cpp b/src/tests/test_pad.cpp
index 0eb14beb8..bf9e64c0d 100644
--- a/src/tests/test_pad.cpp
+++ b/src/tests/test_pad.cpp
@@ -18,15 +18,25 @@ class Cipher_Mode_Padding_Tests : public Text_Based_Test
{
public:
Cipher_Mode_Padding_Tests() :
- Text_Based_Test("pad.vec", {"In", "Out", "Blocksize"})
+ Text_Based_Test("", "pad.vec", {"In", "Blocksize"},{"Out"})
{}
- Test::Result run_one_test(const std::string& algo, const VarMap& vars) override
+ Test::Result run_one_test(const std::string& header, const VarMap& vars) override
{
const std::vector<uint8_t> input = get_req_bin(vars, "In");
- const std::vector<uint8_t> expected = get_req_bin(vars, "Out");
+ const std::vector<uint8_t> expected = get_opt_bin(vars, "Out");
const size_t block_size = get_req_sz(vars, "Blocksize");
+ std::string algo = header;
+
+ auto underscore = algo.find('_');
+ if(underscore != std::string::npos)
+ {
+ if(algo.substr(underscore+1,std::string::npos) != "Invalid")
+ throw Test_Error("Unexpected padding header " + header);
+ algo = algo.substr(0, underscore);
+ }
+
Test::Result result(algo);
std::unique_ptr<Botan::BlockCipherModePaddingMethod> pad(Botan::get_bc_pad(algo));
@@ -37,34 +47,36 @@ class Cipher_Mode_Padding_Tests : public Text_Based_Test
return result;
}
- Botan::secure_vector<uint8_t> buf(input.begin(), input.end());
- pad->add_padding(buf, input.size() % block_size, block_size);
- result.test_eq("pad", buf, expected);
+ if(expected.empty())
+ {
+ // This is an unpad an invalid input and ensure we reject
+ try
+ {
+ pad->unpad(input.data(), block_size);
+ result.test_failure("Did not reject invalid padding", Botan::hex_encode(input));
+ }
+ catch(Botan::Decoding_Error&)
+ {
+ result.test_success("Rejected invalid padding");
+ }
+ }
+ else
+ {
+ // This is a pad plaintext and unpad valid padding round trip test
+ Botan::secure_vector<uint8_t> buf(input.begin(), input.end());
+ pad->add_padding(buf, input.size() % block_size, block_size);
+ result.test_eq("pad", buf, expected);
- buf.assign(expected.begin(), expected.end());
+ buf.assign(expected.begin(), expected.end());
- const size_t last_block = ( buf.size() < block_size ) ? 0 : buf.size() - block_size;
- const size_t pad_bytes = block_size - pad->unpad(&buf[last_block], block_size);
- buf.resize(buf.size() - pad_bytes); // remove padding
- result.test_eq("unpad", buf, input);
+ const size_t last_block = ( buf.size() < block_size ) ? 0 : buf.size() - block_size;
+ const size_t pad_bytes = block_size - pad->unpad(&buf[last_block], block_size);
+ buf.resize(buf.size() - pad_bytes); // remove padding
+ result.test_eq("unpad", buf, input);
+ }
return result;
}
-
- std::vector<Test::Result> run_final_tests()
- {
- Test::Result result("ESP negative tests");
-
- std::vector<uint8_t> invalid1 { 0xFF, 0x01, 0x02, 0x02 };
- result.test_throws("ESP invalid last pad", [&invalid1]()
- { Botan::ESP_Padding().unpad(invalid1.data(), invalid1.size()); } );
-
- std::vector<uint8_t> invalid2 { 0xFF, 0x01, 0x02, 0x04 };
- result.test_throws("ESP invalid pad", [&invalid2]()
- { Botan::ESP_Padding().unpad(invalid2.data(), invalid2.size()); } );
-
- return {result};
- }
};
BOTAN_REGISTER_TEST("bc_pad", Cipher_Mode_Padding_Tests);