Botan  2.8.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 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  virtual ~BlockCipherModePaddingMethod() = default;
62  };
63 
64 /**
65 * PKCS#7 Padding
66 */
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  bool valid_blocksize(size_t bs) const override { return (bs > 2 && bs < 256); }
77 
78  std::string name() const override { return "PKCS7"; }
79  };
80 
81 /**
82 * ANSI X9.23 Padding
83 */
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  bool valid_blocksize(size_t bs) const override { return (bs > 2 && bs < 256); }
94 
95  std::string name() const override { return "X9.23"; }
96  };
97 
98 /**
99 * One And Zeros Padding (ISO/IEC 9797-1, padding method 2)
100 */
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  bool valid_blocksize(size_t bs) const override { return (bs > 2); }
111 
112  std::string name() const override { return "OneAndZeros"; }
113  };
114 
115 /**
116 * ESP Padding (RFC 4304)
117 */
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  bool valid_blocksize(size_t bs) const override { return (bs > 2 && bs < 256); }
128 
129  std::string name() const override { return "ESP"; }
130  };
131 
132 /**
133 * Null Padding
134 */
136  {
137  public:
138  void add_padding(secure_vector<uint8_t>&, size_t, size_t) const override
139  {
140  /* no padding */
141  }
142 
143  size_t unpad(const uint8_t[], size_t size) const override { return size; }
144 
145  bool valid_blocksize(size_t) const override { return true; }
146 
147  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_PUBLIC_API(2,0) BlockCipherModePaddingMethod* get_bc_pad(const std::string& algo_spec);
155 
156 }
157 
158 #endif
bool valid_blocksize(size_t bs) const override
Definition: mode_pad.h:127
bool valid_blocksize(size_t bs) const override
Definition: mode_pad.h:93
int(* final)(unsigned char *, CTX *)
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:27
Definition: bigint.h:891
std::string name() const override
Definition: mode_pad.h:95
bool valid_blocksize(size_t) const override
Definition: mode_pad.h:145
std::string name
std::string name() const override
Definition: mode_pad.h:112
std::string name() const override
Definition: mode_pad.h:147
bool valid_blocksize(size_t bs) const override
Definition: mode_pad.h:76
Definition: alg_id.cpp:13
bool valid_blocksize(size_t bs) const override
Definition: mode_pad.h:110
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:138
std::string name() const override
Definition: mode_pad.h:129
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:143
std::string name() const override
Definition: mode_pad.h:78