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