Botan  2.4.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  secure_vector<uint8_t>& state() { return m_state; }
50 
51  size_t block_size() const { return m_state.size(); }
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  };
64 
65 /**
66 * CBC Encryption
67 */
69  {
70  public:
71  /**
72  * @param cipher block cipher to use
73  * @param padding padding method to use
74  */
76  CBC_Mode(cipher, padding) {}
77 
78  size_t process(uint8_t buf[], size_t size) override;
79 
80  void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
81 
82  size_t output_length(size_t input_length) const override;
83 
84  size_t minimum_final_size() const override;
85  };
86 
87 /**
88 * CBC Encryption with ciphertext stealing (CBC-CS3 variant)
89 */
91  {
92  public:
93  /**
94  * @param cipher block cipher to use
95  */
96  explicit CTS_Encryption(BlockCipher* cipher) : CBC_Encryption(cipher, nullptr) {}
97 
98  size_t output_length(size_t input_length) const override;
99 
100  void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
101 
102  size_t minimum_final_size() const override;
103 
104  bool valid_nonce_length(size_t n) const override;
105  };
106 
107 /**
108 * CBC Decryption
109 */
111  {
112  public:
113  /**
114  * @param cipher block cipher to use
115  * @param padding padding method to use
116  */
118  CBC_Mode(cipher, padding), m_tempbuf(update_granularity()) {}
119 
120  size_t process(uint8_t buf[], size_t size) override;
121 
122  void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
123 
124  size_t output_length(size_t input_length) const override;
125 
126  size_t minimum_final_size() const override;
127 
128  void reset() override;
129 
130  private:
131  secure_vector<uint8_t> m_tempbuf;
132  };
133 
134 /**
135 * CBC Decryption with ciphertext stealing (CBC-CS3 variant)
136 */
138  {
139  public:
140  /**
141  * @param cipher block cipher to use
142  */
143  explicit CTS_Decryption(BlockCipher* cipher) : CBC_Decryption(cipher, nullptr) {}
144 
145  void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
146 
147  size_t minimum_final_size() const override;
148 
149  bool valid_nonce_length(size_t n) const override;
150  };
151 
152 }
153 
154 #endif
secure_vector< uint8_t > & state()
Definition: cbc.h:49
const BlockCipherModePaddingMethod & padding() const
Definition: cbc.h:43
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:27
CBC_Encryption(BlockCipher *cipher, BlockCipherModePaddingMethod *padding)
Definition: cbc.h:75
#define BOTAN_ASSERT_NONNULL(ptr)
Definition: assert.h:81
uint8_t * state_ptr()
Definition: cbc.h:53
Definition: alg_id.cpp:13
size_t block_size() const
Definition: cbc.h:51
CBC_Decryption(BlockCipher *cipher, BlockCipherModePaddingMethod *padding)
Definition: cbc.h:117
CTS_Encryption(BlockCipher *cipher)
Definition: cbc.h:96
CTS_Decryption(BlockCipher *cipher)
Definition: cbc.h:143
const BlockCipher & cipher() const
Definition: cbc.h:41
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:88