Botan  2.15.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 
16 
17 namespace Botan {
18 
19 /**
20 * Block Cipher Mode Padding Method
21 * This class is pretty limited, it cannot deal well with
22 * randomized padding methods, or any padding method that
23 * wants to add more than one block. For instance, it should
24 * be possible to define cipher text stealing mode as simply
25 * a padding mode for CBC, which happens to consume the last
26 * two block (and requires use of the block cipher).
27 */
29  {
30  public:
31  /**
32  * Add padding bytes to buffer.
33  * @param buffer data to pad
34  * @param final_block_bytes size of the final block in bytes
35  * @param block_size size of each block in bytes
36  */
37  virtual void add_padding(secure_vector<uint8_t>& buffer,
38  size_t final_block_bytes,
39  size_t block_size) const = 0;
40 
41  /**
42  * Remove padding bytes from block
43  * @param block the last block
44  * @param len the size of the block in bytes
45  * @return number of data bytes, or if the padding is invalid returns len
46  */
47  virtual size_t unpad(const uint8_t block[], size_t len) const = 0;
48 
49  /**
50  * @param block_size of the cipher
51  * @return valid block size for this padding mode
52  */
53  virtual bool valid_blocksize(size_t block_size) const = 0;
54 
55  /**
56  * @return name of the mode
57  */
58  virtual std::string name() const = 0;
59 
60  /**
61  * virtual destructor
62  */
63  virtual ~BlockCipherModePaddingMethod() = default;
64  };
65 
66 /**
67 * PKCS#7 Padding
68 */
70  {
71  public:
73  size_t final_block_bytes,
74  size_t block_size) const override;
75 
76  size_t unpad(const uint8_t[], size_t) const override;
77 
78  bool valid_blocksize(size_t bs) const override { return (bs > 2 && bs < 256); }
79 
80  std::string name() const override { return "PKCS7"; }
81  };
82 
83 /**
84 * ANSI X9.23 Padding
85 */
87  {
88  public:
90  size_t final_block_bytes,
91  size_t block_size) const override;
92 
93  size_t unpad(const uint8_t[], size_t) const override;
94 
95  bool valid_blocksize(size_t bs) const override { return (bs > 2 && bs < 256); }
96 
97  std::string name() const override { return "X9.23"; }
98  };
99 
100 /**
101 * One And Zeros Padding (ISO/IEC 9797-1, padding method 2)
102 */
104  {
105  public:
106  void add_padding(secure_vector<uint8_t>& buffer,
107  size_t final_block_bytes,
108  size_t block_size) const override;
109 
110  size_t unpad(const uint8_t[], size_t) const override;
111 
112  bool valid_blocksize(size_t bs) const override { return (bs > 2); }
113 
114  std::string name() const override { return "OneAndZeros"; }
115  };
116 
117 /**
118 * ESP Padding (RFC 4304)
119 */
121  {
122  public:
123  void add_padding(secure_vector<uint8_t>& buffer,
124  size_t final_block_bytes,
125  size_t block_size) const override;
126 
127  size_t unpad(const uint8_t[], size_t) const override;
128 
129  bool valid_blocksize(size_t bs) const override { return (bs > 2 && bs < 256); }
130 
131  std::string name() const override { return "ESP"; }
132  };
133 
134 /**
135 * Null Padding
136 */
138  {
139  public:
140  void add_padding(secure_vector<uint8_t>&, size_t, size_t) const override
141  {
142  /* no padding */
143  }
144 
145  size_t unpad(const uint8_t[], size_t size) const override { return size; }
146 
147  bool valid_blocksize(size_t) const override { return true; }
148 
149  std::string name() const override { return "NoPadding"; }
150  };
151 
152 /**
153 * Get a block cipher padding mode by name (eg "NoPadding" or "PKCS7")
154 * @param algo_spec block cipher padding mode name
155 */
156 BOTAN_PUBLIC_API(2,0) BlockCipherModePaddingMethod* get_bc_pad(const std::string& algo_spec);
157 
158 }
159 
160 #endif
bool valid_blocksize(size_t bs) const override
Definition: mode_pad.h:129
bool valid_blocksize(size_t bs) const override
Definition: mode_pad.h:95
virtual size_t unpad(const uint8_t block[], size_t len) const =0
int(* final)(unsigned char *, CTX *)
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:31
Definition: bigint.h:1142
std::string name() const override
Definition: mode_pad.h:97
bool valid_blocksize(size_t) const override
Definition: mode_pad.h:147
std::string name
std::string name() const override
Definition: mode_pad.h:114
std::string name() const override
Definition: mode_pad.h:149
bool valid_blocksize(size_t bs) const override
Definition: mode_pad.h:78
Definition: alg_id.cpp:13
bool valid_blocksize(size_t bs) const override
Definition: mode_pad.h:112
virtual void add_padding(secure_vector< uint8_t > &buffer, size_t final_block_bytes, size_t block_size) const =0
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:65
void add_padding(secure_vector< uint8_t > &, size_t, size_t) const override
Definition: mode_pad.h:140
std::string name() const override
Definition: mode_pad.h:131
#define BOTAN_FUTURE_INTERNAL_HEADER(hdr)
Definition: compiler.h:145
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:145
std::string name() const override
Definition: mode_pad.h:80