Botan  1.10.9
mode_pad.h
Go to the documentation of this file.
1 /*
2 * CBC Padding Methods
3 * (C) 1999-2008 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7 
8 #ifndef BOTAN_CBC_PADDING_H__
9 #define BOTAN_CBC_PADDING_H__
10 
11 #include <botan/types.h>
12 #include <string>
13 
14 namespace Botan {
15 
16 /**
17 * Block Cipher Mode Padding Method
18 * This class is pretty limited, it cannot deal well with
19 * randomized padding methods, or any padding method that
20 * wants to add more than one block. For instance, it should
21 * be possible to define cipher text stealing mode as simply
22 * a padding mode for CBC, which happens to consume the last
23 * two block (and requires use of the block cipher).
24 */
26  {
27  public:
28  /**
29  * @param block output buffer
30  * @param size of the block
31  * @param current_position in the last block
32  */
33  virtual void pad(byte block[],
34  size_t size,
35  size_t current_position) const = 0;
36 
37  /**
38  * @param block the last block
39  * @param size the of the block
40  */
41  virtual size_t unpad(const byte block[],
42  size_t size) const = 0;
43 
44  /**
45  * @param block_size of the cipher
46  * @param position in the current block
47  * @return number of padding bytes that will be appended
48  */
49  virtual size_t pad_bytes(size_t block_size,
50  size_t position) const;
51 
52  /**
53  * @param block_size of the cipher
54  * @return valid block size for this padding mode
55  */
56  virtual bool valid_blocksize(size_t block_size) const = 0;
57 
58  /**
59  * @return name of the mode
60  */
61  virtual std::string name() const = 0;
62 
63  /**
64  * virtual destructor
65  */
67  };
68 
69 /**
70 * PKCS#7 Padding
71 */
73  {
74  public:
75  void pad(byte[], size_t, size_t) const;
76  size_t unpad(const byte[], size_t) const;
77  bool valid_blocksize(size_t) const;
78  std::string name() const { return "PKCS7"; }
79  };
80 
81 /**
82 * ANSI X9.23 Padding
83 */
85  {
86  public:
87  void pad(byte[], size_t, size_t) const;
88  size_t unpad(const byte[], size_t) const;
89  bool valid_blocksize(size_t) const;
90  std::string name() const { return "X9.23"; }
91  };
92 
93 /**
94 * One And Zeros Padding
95 */
97  {
98  public:
99  void pad(byte[], size_t, size_t) const;
100  size_t unpad(const byte[], size_t) const;
101  bool valid_blocksize(size_t) const;
102  std::string name() const { return "OneAndZeros"; }
103  };
104 
105 /**
106 * Null Padding
107 */
109  {
110  public:
111  void pad(byte[], size_t, size_t) const { return; }
112  size_t unpad(const byte[], size_t size) const { return size; }
113  size_t pad_bytes(size_t, size_t) const { return 0; }
114  bool valid_blocksize(size_t) const { return true; }
115  std::string name() const { return "NoPadding"; }
116  };
117 
118 }
119 
120 #endif
std::string name() const
Definition: mode_pad.h:90
std::string name() const
Definition: mode_pad.h:115
size_t block_size
Definition: ossl_md.cpp:41
void pad(byte[], size_t, size_t) const
Definition: mode_pad.h:111
unsigned char byte
Definition: types.h:22
std::string name() const
Definition: mode_pad.h:102
std::string name() const
Definition: mode_pad.h:78
bool valid_blocksize(size_t) const
Definition: mode_pad.h:114
size_t pad_bytes(size_t, size_t) const
Definition: mode_pad.h:113
size_t unpad(const byte[], size_t size) const
Definition: mode_pad.h:112