/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 | | { |
28 | | public: |
29 | | /** |
30 | | * Add padding bytes to buffer. |
31 | | * @param buffer data to pad |
32 | | * @param final_block_bytes size of the final block in bytes |
33 | | * @param block_size size of each block in bytes |
34 | | */ |
35 | | virtual void add_padding(secure_vector<uint8_t>& buffer, |
36 | | size_t final_block_bytes, |
37 | | size_t block_size) const = 0; |
38 | | |
39 | | /** |
40 | | * Remove padding bytes from block |
41 | | * @param block the last block |
42 | | * @param len the size of the block in bytes |
43 | | * @return number of data bytes, or if the padding is invalid returns len |
44 | | */ |
45 | | virtual size_t unpad(const uint8_t block[], size_t len) const = 0; |
46 | | |
47 | | /** |
48 | | * @param block_size of the cipher |
49 | | * @return valid block size for this padding mode |
50 | | */ |
51 | | virtual bool valid_blocksize(size_t block_size) const = 0; |
52 | | |
53 | | /** |
54 | | * @return name of the mode |
55 | | */ |
56 | | virtual std::string name() const = 0; |
57 | | |
58 | | /** |
59 | | * virtual destructor |
60 | | */ |
61 | 1.03k | virtual ~BlockCipherModePaddingMethod() = default; |
62 | | }; |
63 | | |
64 | | /** |
65 | | * PKCS#7 Padding |
66 | | */ |
67 | | class BOTAN_TEST_API PKCS7_Padding final : public BlockCipherModePaddingMethod |
68 | | { |
69 | | public: |
70 | | void add_padding(secure_vector<uint8_t>& buffer, |
71 | | size_t final_block_bytes, |
72 | | size_t block_size) const override; |
73 | | |
74 | | size_t unpad(const uint8_t[], size_t) const override; |
75 | | |
76 | 386 | 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 | | { |
86 | | public: |
87 | | void add_padding(secure_vector<uint8_t>& buffer, |
88 | | size_t final_block_bytes, |
89 | | size_t block_size) const override; |
90 | | |
91 | | size_t unpad(const uint8_t[], size_t) const override; |
92 | | |
93 | 386 | bool valid_blocksize(size_t bs) const override { return (bs > 2 && bs < 256); } |
94 | | |
95 | 0 | std::string name() const override { return "X9.23"; } |
96 | | }; |
97 | | |
98 | | /** |
99 | | * One And Zeros Padding (ISO/IEC 9797-1, padding method 2) |
100 | | */ |
101 | | class BOTAN_TEST_API OneAndZeros_Padding final : public BlockCipherModePaddingMethod |
102 | | { |
103 | | public: |
104 | | void add_padding(secure_vector<uint8_t>& buffer, |
105 | | size_t final_block_bytes, |
106 | | size_t block_size) const override; |
107 | | |
108 | | size_t unpad(const uint8_t[], size_t) const override; |
109 | | |
110 | 442 | bool valid_blocksize(size_t bs) const override { return (bs > 2); } |
111 | | |
112 | 0 | std::string name() const override { return "OneAndZeros"; } |
113 | | }; |
114 | | |
115 | | /** |
116 | | * ESP Padding (RFC 4304) |
117 | | */ |
118 | | class BOTAN_TEST_API ESP_Padding final : public BlockCipherModePaddingMethod |
119 | | { |
120 | | public: |
121 | | void add_padding(secure_vector<uint8_t>& buffer, |
122 | | size_t final_block_bytes, |
123 | | size_t block_size) const override; |
124 | | |
125 | | size_t unpad(const uint8_t[], size_t) const override; |
126 | | |
127 | 386 | bool valid_blocksize(size_t bs) const override { return (bs > 2 && bs < 256); } |
128 | | |
129 | 0 | std::string name() const override { return "ESP"; } |
130 | | }; |
131 | | |
132 | | /** |
133 | | * Null Padding |
134 | | */ |
135 | | class Null_Padding final : public BlockCipherModePaddingMethod |
136 | | { |
137 | | public: |
138 | | void add_padding(secure_vector<uint8_t>&, size_t, size_t) const override |
139 | 0 | { |
140 | | /* no padding */ |
141 | 0 | } |
142 | | |
143 | 0 | size_t unpad(const uint8_t[], size_t size) const override { return size; } |
144 | | |
145 | 1.02k | bool valid_blocksize(size_t) const override { return true; } |
146 | | |
147 | 0 | std::string name() const override { return "NoPadding"; } |
148 | | }; |
149 | | |
150 | | /** |
151 | | * Get a block cipher padding mode by name (eg "NoPadding" or "PKCS7") |
152 | | * @param algo_spec block cipher padding mode name |
153 | | */ |
154 | | BOTAN_TEST_API BlockCipherModePaddingMethod* get_bc_pad(const std::string& algo_spec); |
155 | | |
156 | | } |
157 | | |
158 | | #endif |