/src/botan/build/include/botan/internal/mode_pad.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * CBC Padding Methods |
3 | | * (C) 1999-2008,2013 Jack Lloyd |
4 | | * (C) 2016 René Korthaus, Rohde & Schwarz Cybersecurity |
5 | | * |
6 | | * Botan is released under the Simplified BSD License (see license.txt) |
7 | | */ |
8 | | |
9 | | #ifndef BOTAN_MODE_PADDING_H_ |
10 | | #define BOTAN_MODE_PADDING_H_ |
11 | | |
12 | | #include <botan/secmem.h> |
13 | | #include <string> |
14 | | |
15 | | namespace Botan { |
16 | | |
17 | | /** |
18 | | * Block Cipher Mode Padding Method |
19 | | * This class is pretty limited, it cannot deal well with |
20 | | * randomized padding methods, or any padding method that |
21 | | * wants to add more than one block. For instance, it should |
22 | | * be possible to define cipher text stealing mode as simply |
23 | | * a padding mode for CBC, which happens to consume the last |
24 | | * two block (and requires use of the block cipher). |
25 | | */ |
26 | | class BOTAN_TEST_API BlockCipherModePaddingMethod { |
27 | | public: |
28 | | /** |
29 | | * Get a block cipher padding mode by name (eg "NoPadding" or "PKCS7") |
30 | | * @param algo_spec block cipher padding mode name |
31 | | */ |
32 | | static std::unique_ptr<BlockCipherModePaddingMethod> create(std::string_view algo_spec); |
33 | | |
34 | | /** |
35 | | * Add padding bytes to buffer. |
36 | | * @param buffer data to pad |
37 | | * @param final_block_bytes size of the final block in bytes |
38 | | * @param block_size size of each block in bytes |
39 | | */ |
40 | | virtual void add_padding(secure_vector<uint8_t>& buffer, size_t final_block_bytes, size_t block_size) const = 0; |
41 | | |
42 | | /** |
43 | | * Remove padding bytes from block |
44 | | * @param block the last block |
45 | | * @param len the size of the block in bytes |
46 | | * @return number of data bytes, or if the padding is invalid returns len |
47 | | */ |
48 | | virtual size_t unpad(const uint8_t block[], size_t len) const = 0; |
49 | | |
50 | | /** |
51 | | * @param block_size of the cipher |
52 | | * @return valid block size for this padding mode |
53 | | */ |
54 | | virtual bool valid_blocksize(size_t block_size) const = 0; |
55 | | |
56 | | /** |
57 | | * @return name of the mode |
58 | | */ |
59 | | virtual std::string name() const = 0; |
60 | | |
61 | | /** |
62 | | * virtual destructor |
63 | | */ |
64 | 0 | virtual ~BlockCipherModePaddingMethod() = default; |
65 | | }; |
66 | | |
67 | | /** |
68 | | * PKCS#7 Padding |
69 | | */ |
70 | | class BOTAN_TEST_API PKCS7_Padding final : public BlockCipherModePaddingMethod { |
71 | | public: |
72 | | void add_padding(secure_vector<uint8_t>& buffer, size_t final_block_bytes, size_t block_size) const override; |
73 | | |
74 | | size_t unpad(const uint8_t[], size_t) const override; |
75 | | |
76 | 0 | bool valid_blocksize(size_t bs) const override { return (bs > 2 && bs < 256); } |
77 | | |
78 | 0 | std::string name() const override { return "PKCS7"; } |
79 | | }; |
80 | | |
81 | | /** |
82 | | * ANSI X9.23 Padding |
83 | | */ |
84 | | class BOTAN_TEST_API ANSI_X923_Padding final : public BlockCipherModePaddingMethod { |
85 | | public: |
86 | | void add_padding(secure_vector<uint8_t>& buffer, size_t final_block_bytes, size_t block_size) const override; |
87 | | |
88 | | size_t unpad(const uint8_t[], size_t) const override; |
89 | | |
90 | 0 | bool valid_blocksize(size_t bs) const override { return (bs > 2 && bs < 256); } |
91 | | |
92 | 0 | std::string name() const override { return "X9.23"; } |
93 | | }; |
94 | | |
95 | | /** |
96 | | * One And Zeros Padding (ISO/IEC 9797-1, padding method 2) |
97 | | */ |
98 | | class BOTAN_TEST_API OneAndZeros_Padding final : public BlockCipherModePaddingMethod { |
99 | | public: |
100 | | void add_padding(secure_vector<uint8_t>& buffer, size_t final_block_bytes, size_t block_size) const override; |
101 | | |
102 | | size_t unpad(const uint8_t[], size_t) const override; |
103 | | |
104 | 0 | bool valid_blocksize(size_t bs) const override { return (bs > 2); } |
105 | | |
106 | 0 | std::string name() const override { return "OneAndZeros"; } |
107 | | }; |
108 | | |
109 | | /** |
110 | | * ESP Padding (RFC 4304) |
111 | | */ |
112 | | class BOTAN_TEST_API ESP_Padding final : public BlockCipherModePaddingMethod { |
113 | | public: |
114 | | void add_padding(secure_vector<uint8_t>& buffer, size_t final_block_bytes, size_t block_size) const override; |
115 | | |
116 | | size_t unpad(const uint8_t[], size_t) const override; |
117 | | |
118 | 0 | bool valid_blocksize(size_t bs) const override { return (bs > 2 && bs < 256); } |
119 | | |
120 | 0 | std::string name() const override { return "ESP"; } |
121 | | }; |
122 | | |
123 | | /** |
124 | | * Null Padding |
125 | | */ |
126 | | class Null_Padding final : public BlockCipherModePaddingMethod { |
127 | | public: |
128 | 0 | void add_padding(secure_vector<uint8_t>&, size_t, size_t) const override { /* no padding */ |
129 | 0 | } |
130 | | |
131 | 0 | size_t unpad(const uint8_t[], size_t size) const override { return size; } |
132 | | |
133 | 0 | bool valid_blocksize(size_t) const override { return true; } |
134 | | |
135 | 0 | std::string name() const override { return "NoPadding"; } |
136 | | }; |
137 | | |
138 | | } // namespace Botan |
139 | | |
140 | | #endif |