Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

mode_pad.h
Go to the documentation of this file.
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_DLL 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 size the size of the block in bytes
43 * @return number of padding bytes
44 */
45 virtual size_t unpad(const uint8_t block[],
46 size_t size) const = 0;
47
48 /**
49 * @param block_size of the cipher
50 * @return valid block size for this padding mode
51 */
52 virtual bool valid_blocksize(size_t block_size) const = 0;
53
54 /**
55 * @return name of the mode
56 */
57 virtual std::string name() const = 0;
58
59 /**
60 * virtual destructor
61 */
62 virtual ~BlockCipherModePaddingMethod() = default;
63 };
64
65 /**
66 * PKCS#7 Padding
67 */
68 class BOTAN_DLL PKCS7_Padding final : public BlockCipherModePaddingMethod
69 {
70 public:
71 void add_padding(secure_vector<uint8_t>& buffer,
72 size_t final_block_bytes,
73 size_t block_size) const override;
74
75 size_t unpad(const uint8_t[], size_t) const override;
76
77 bool valid_blocksize(size_t bs) const override { return (bs > 0 && bs < 256); }
78
79 std::string name() const override { return "PKCS7"; }
80 };
81
82 /**
83 * ANSI X9.23 Padding
84 */
85 class BOTAN_DLL ANSI_X923_Padding final : public BlockCipherModePaddingMethod
86 {
87 public:
88 void add_padding(secure_vector<uint8_t>& buffer,
89 size_t final_block_bytes,
90 size_t block_size) const override;
91
92 size_t unpad(const uint8_t[], size_t) const override;
93
94 bool valid_blocksize(size_t bs) const override { return (bs > 0 && bs < 256); }
95
96 std::string name() const override { return "X9.23"; }
97 };
98
99 /**
100 * One And Zeros Padding (ISO/IEC 9797-1, padding method 2)
101 */
102 class BOTAN_DLL OneAndZeros_Padding final : public BlockCipherModePaddingMethod
103 {
104 public:
105 void add_padding(secure_vector<uint8_t>& buffer,
106 size_t final_block_bytes,
107 size_t block_size) const override;
108
109 size_t unpad(const uint8_t[], size_t) const override;
110
111 bool valid_blocksize(size_t bs) const override { return (bs > 0); }
112
113 std::string name() const override { return "OneAndZeros"; }
114 };
115
116 /**
117 * ESP Padding (RFC 4304)
118 */
119 class BOTAN_DLL ESP_Padding final : public BlockCipherModePaddingMethod
120 {
121 public:
122 void add_padding(secure_vector<uint8_t>& buffer,
123 size_t final_block_bytes,
124 size_t block_size) const override;
125
126 size_t unpad(const uint8_t[], size_t) const override;
127
128 bool valid_blocksize(size_t bs) const override { return (bs > 0); }
129
130 std::string name() const override { return "ESP"; }
131 };
132
133 /**
134 * Null Padding
135 */
136 class BOTAN_DLL Null_Padding final : public BlockCipherModePaddingMethod
137 {
138 public:
139 void add_padding(secure_vector<uint8_t>&, size_t, size_t) const override {}
140
141 size_t unpad(const uint8_t[], size_t size) const override { return size; }
142
143 bool valid_blocksize(size_t) const override { return true; }
144
145 std::string name() const override { return "NoPadding"; }
146 };
147
148 /**
149 * Get a block cipher padding mode by name (eg "NoPadding" or "PKCS7")
150 * @param algo_spec block cipher padding mode name
151 */
152 BOTAN_DLL BlockCipherModePaddingMethod* get_bc_pad(const std::string& algo_spec);
153
154 }
155
156 #endif
Botan::ESP_Padding::valid_blocksize
bool valid_blocksize(size_t bs) const override
Definition: mode_pad.h:128
Botan::ANSI_X923_Padding::valid_blocksize
bool valid_blocksize(size_t bs) const override
Definition: mode_pad.h:94
Botan::get_bc_pad
BlockCipherModePaddingMethod * get_bc_pad(const std::string &algo_spec)
Definition: mode_pad.cpp:18
Botan::ANSI_X923_Padding::name
std::string name() const override
Definition: mode_pad.h:96
Botan::Null_Padding
Definition: mode_pad.h:136
Botan::Null_Padding::valid_blocksize
bool valid_blocksize(size_t) const override
Definition: mode_pad.h:143
Botan::secure_vector
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:121
Botan::OneAndZeros_Padding::name
std::string name() const override
Definition: mode_pad.h:113
Botan::ESP_Padding
Definition: mode_pad.h:119
Botan::Null_Padding::name
std::string name() const override
Definition: mode_pad.h:145
Botan::PKCS7_Padding::valid_blocksize
bool valid_blocksize(size_t bs) const override
Definition: mode_pad.h:77
Botan
Definition: alg_id.cpp:13
Botan::OneAndZeros_Padding
Definition: mode_pad.h:102
Botan::BlockCipherModePaddingMethod
Definition: mode_pad.h:26
Botan::ANSI_X923_Padding
Definition: mode_pad.h:85
Botan::OneAndZeros_Padding::valid_blocksize
bool valid_blocksize(size_t bs) const override
Definition: mode_pad.h:111
Botan::PKCS7_Padding
Definition: mode_pad.h:68
Botan::Null_Padding::add_padding
void add_padding(secure_vector< uint8_t > &, size_t, size_t) const override
Definition: mode_pad.h:139
Botan::ESP_Padding::name
std::string name() const override
Definition: mode_pad.h:130
Botan::Null_Padding::unpad
size_t unpad(const uint8_t[], size_t size) const override
Definition: mode_pad.h:141
Botan::PKCS7_Padding::name
std::string name() const override
Definition: mode_pad.h:79