Botan  2.7.0
Crypto and TLS for C++11
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 */
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 */
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 */
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 */
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 */
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 */
137  {
138  public:
139  void add_padding(secure_vector<uint8_t>&, size_t, size_t) const override
140  {
141  /* no padding */
142  }
143 
144  size_t unpad(const uint8_t[], size_t size) const override { return size; }
145 
146  bool valid_blocksize(size_t) const override { return true; }
147 
148  std::string name() const override { return "NoPadding"; }
149  };
150 
151 /**
152 * Get a block cipher padding mode by name (eg "NoPadding" or "PKCS7")
153 * @param algo_spec block cipher padding mode name
154 */
155 BOTAN_PUBLIC_API(2,0) BlockCipherModePaddingMethod* get_bc_pad(const std::string& algo_spec);
156 
157 }
158 
159 #endif
bool valid_blocksize(size_t bs) const override
Definition: mode_pad.h:128
bool valid_blocksize(size_t bs) const override
Definition: mode_pad.h:94
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:27
Definition: bigint.h:796
std::string name() const override
Definition: mode_pad.h:96
bool valid_blocksize(size_t) const override
Definition: mode_pad.h:146
std::string name() const override
Definition: mode_pad.h:113
std::string name() const override
Definition: mode_pad.h:148
bool valid_blocksize(size_t bs) const override
Definition: mode_pad.h:77
Definition: alg_id.cpp:13
bool valid_blocksize(size_t bs) const override
Definition: mode_pad.h:111
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:88
void add_padding(secure_vector< uint8_t > &, size_t, size_t) const override
Definition: mode_pad.h:139
std::string name() const override
Definition: mode_pad.h:130
BlockCipherModePaddingMethod * get_bc_pad(const std::string &algo_spec)
Definition: mode_pad.cpp:18
size_t unpad(const uint8_t[], size_t size) const override
Definition: mode_pad.h:144
std::string name() const override
Definition: mode_pad.h:79