aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/test_pad.cpp
blob: 592ae660b4b63518fce7a3008aecf1560a3bdb44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
* (C) 2016 René Korthaus, Rohde & Schwarz Cybersecurity
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include "tests.h"

#if defined(BOTAN_HAS_CIPHER_MODE_PADDING)
   #include <botan/mode_pad.h>
#endif

namespace Botan_Tests {

#if defined(BOTAN_HAS_CIPHER_MODE_PADDING)

class Cipher_Mode_Padding_Tests : public Text_Based_Test
   {
   public:
      Cipher_Mode_Padding_Tests() : Text_Based_Test("pad.vec", "In,Blocksize", "Out") {}

      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_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));

         if(!pad)
            {
            result.test_failure("Invalid padding method: " + algo);
            return result;
            }

         if(expected.empty())
            {
            // This is an unpad an invalid input and ensure we reject
            if(pad->unpad(input.data(), block_size) != block_size)
               {
               result.test_failure("Did not reject invalid padding", Botan::hex_encode(input));
               }
            else
               {
               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());

            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;
         }
   };

BOTAN_REGISTER_TEST("bc_pad", Cipher_Mode_Padding_Tests);

#endif

}