Botan  2.8.0
Crypto and TLS for C++11
cbc.h
Go to the documentation of this file.
1 /*
2 * CBC mode
3 * (C) 1999-2007,2013 Jack Lloyd
4 * (C) 2016 Daniel Neus, Rohde & Schwarz Cybersecurity
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 */
8 
9 #ifndef BOTAN_MODE_CBC_H_
10 #define BOTAN_MODE_CBC_H_
11 
12 #include <botan/cipher_mode.h>
13 #include <botan/block_cipher.h>
14 #include <botan/mode_pad.h>
15 
16 namespace Botan {
17 
18 /**
19 * CBC Mode
20 */
22  {
23  public:
24  std::string name() const override;
25 
26  size_t update_granularity() const override;
27 
28  Key_Length_Specification key_spec() const override;
29 
30  size_t default_nonce_length() const override;
31 
32  bool valid_nonce_length(size_t n) const override;
33 
34  void clear() override;
35 
36  void reset() override;
37 
38  protected:
40 
41  const BlockCipher& cipher() const { return *m_cipher; }
42 
44  {
45  BOTAN_ASSERT_NONNULL(m_padding);
46  return *m_padding;
47  }
48 
49  size_t block_size() const { return m_block_size; }
50 
51  secure_vector<uint8_t>& state() { return m_state; }
52 
53  uint8_t* state_ptr() { return m_state.data(); }
54 
55  private:
56  void start_msg(const uint8_t nonce[], size_t nonce_len) override;
57 
58  void key_schedule(const uint8_t key[], size_t length) override;
59 
60  std::unique_ptr<BlockCipher> m_cipher;
61  std::unique_ptr<BlockCipherModePaddingMethod> m_padding;
62  secure_vector<uint8_t> m_state;
63  size_t m_block_size;
64  };
65 
66 /**
67 * CBC Encryption
68 */
70  {
71  public:
72  /**
73  * @param cipher block cipher to use
74  * @param padding padding method to use
75  */
77  CBC_Mode(cipher, padding) {}
78 
79  size_t process(uint8_t buf[], size_t size) override;
80 
81  void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
82 
83  size_t output_length(size_t input_length) const override;
84 
85  size_t minimum_final_size() const override;
86  };
87 
88 /**
89 * CBC Encryption with ciphertext stealing (CBC-CS3 variant)
90 */
92  {
93  public:
94  /**
95  * @param cipher block cipher to use
96  */
97  explicit CTS_Encryption(BlockCipher* cipher) : CBC_Encryption(cipher, nullptr) {}
98 
99  size_t output_length(size_t input_length) const override;
100 
101  void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
102 
103  size_t minimum_final_size() const override;
104 
105  bool valid_nonce_length(size_t n) const override;
106  };
107 
108 /**
109 * CBC Decryption
110 */
112  {
113  public:
114  /**
115  * @param cipher block cipher to use
116  * @param padding padding method to use
117  */
119  CBC_Mode(cipher, padding), m_tempbuf(update_granularity()) {}
120 
121  size_t process(uint8_t buf[], size_t size) override;
122 
123  void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
124 
125  size_t output_length(size_t input_length) const override;
126 
127  size_t minimum_final_size() const override;
128 
129  void reset() override;
130 
131  private:
132  secure_vector<uint8_t> m_tempbuf;
133  };
134 
135 /**
136 * CBC Decryption with ciphertext stealing (CBC-CS3 variant)
137 */
139  {
140  public:
141  /**
142  * @param cipher block cipher to use
143  */
144  explicit CTS_Decryption(BlockCipher* cipher) : CBC_Decryption(cipher, nullptr) {}
145 
146  void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
147 
148  size_t minimum_final_size() const override;
149 
150  bool valid_nonce_length(size_t n) const override;
151  };
152 
153 }
154 
155 #endif
secure_vector< uint8_t > & state()
Definition: cbc.h:51
const BlockCipherModePaddingMethod & padding() const
Definition: cbc.h:43
int(* final)(unsigned char *, CTX *)
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:27
CBC_Encryption(BlockCipher *cipher, BlockCipherModePaddingMethod *padding)
Definition: cbc.h:76
std::string name
#define BOTAN_ASSERT_NONNULL(ptr)
Definition: assert.h:107
uint8_t * state_ptr()
Definition: cbc.h:53
Definition: alg_id.cpp:13
size_t block_size() const
Definition: cbc.h:49
CBC_Decryption(BlockCipher *cipher, BlockCipherModePaddingMethod *padding)
Definition: cbc.h:118
CTS_Encryption(BlockCipher *cipher)
Definition: cbc.h:97
CTS_Decryption(BlockCipher *cipher)
Definition: cbc.h:144
const BlockCipher & cipher() const
Definition: cbc.h:41
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:88