Botan  1.11.11
cbc.h
Go to the documentation of this file.
1 /*
2 * CBC mode
3 * (C) 1999-2007,2013 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7 
8 #ifndef BOTAN_MODE_CBC_H__
9 #define BOTAN_MODE_CBC_H__
10 
11 #include <botan/cipher_mode.h>
12 #include <botan/block_cipher.h>
13 #include <botan/mode_pad.h>
14 
15 namespace Botan {
16 
17 /**
18 * CBC Mode
19 */
20 class BOTAN_DLL CBC_Mode : public Cipher_Mode
21  {
22  public:
23  std::string name() const override;
24 
25  size_t update_granularity() const override;
26 
27  Key_Length_Specification key_spec() const override;
28 
29  size_t default_nonce_length() const override;
30 
31  bool valid_nonce_length(size_t n) const override;
32 
33  void clear() override;
34  protected:
36 
37  const BlockCipher& cipher() const { return *m_cipher; }
38 
40  {
41  BOTAN_ASSERT_NONNULL(m_padding);
42  return *m_padding;
43  }
44 
45  secure_vector<byte>& state() { return m_state; }
46 
47  byte* state_ptr() { return &m_state[0]; }
48 
49  private:
50  secure_vector<byte> start_raw(const byte nonce[], size_t nonce_len) override;
51 
52  void key_schedule(const byte key[], size_t length) override;
53 
54  std::unique_ptr<BlockCipher> m_cipher;
55  std::unique_ptr<BlockCipherModePaddingMethod> m_padding;
56  secure_vector<byte> m_state;
57  };
58 
59 /**
60 * CBC Encryption
61 */
62 class BOTAN_DLL CBC_Encryption : public CBC_Mode
63  {
64  public:
66  CBC_Mode(cipher, padding) {}
67 
68  void update(secure_vector<byte>& blocks, size_t offset = 0) override;
69 
70  void finish(secure_vector<byte>& final_block, size_t offset = 0) override;
71 
72  size_t output_length(size_t input_length) const override;
73 
74  size_t minimum_final_size() const override;
75  };
76 
77 /**
78 * CBC Encryption with ciphertext stealing (CBC-CS3 variant)
79 */
80 class BOTAN_DLL CTS_Encryption : public CBC_Encryption
81  {
82  public:
83  CTS_Encryption(BlockCipher* cipher) : CBC_Encryption(cipher, nullptr) {}
84 
85  size_t output_length(size_t input_length) const override;
86 
87  void finish(secure_vector<byte>& final_block, size_t offset = 0) override;
88 
89  size_t minimum_final_size() const override;
90 
91  bool valid_nonce_length(size_t n) const;
92  };
93 
94 /**
95 * CBC Decryption
96 */
97 class BOTAN_DLL CBC_Decryption : public CBC_Mode
98  {
99  public:
101  CBC_Mode(cipher, padding), m_tempbuf(update_granularity()) {}
102 
103  void update(secure_vector<byte>& blocks, size_t offset = 0) override;
104 
105  void finish(secure_vector<byte>& final_block, size_t offset = 0) override;
106 
107  size_t output_length(size_t input_length) const override;
108 
109  size_t minimum_final_size() const override;
110  private:
111  secure_vector<byte> m_tempbuf;
112  };
113 
114 /**
115 * CBC Decryption with ciphertext stealing (CBC-CS3 variant)
116 */
117 class BOTAN_DLL CTS_Decryption : public CBC_Decryption
118  {
119  public:
120  CTS_Decryption(BlockCipher* cipher) : CBC_Decryption(cipher, nullptr) {}
121 
122  void finish(secure_vector<byte>& final_block, size_t offset = 0) override;
123 
124  size_t minimum_final_size() const override;
125 
126  bool valid_nonce_length(size_t n) const;
127  };
128 
129 }
130 
131 #endif
byte * state_ptr()
Definition: cbc.h:47
const BlockCipherModePaddingMethod & padding() const
Definition: cbc.h:39
CBC_Encryption(BlockCipher *cipher, BlockCipherModePaddingMethod *padding)
Definition: cbc.h:65
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:92
#define BOTAN_ASSERT_NONNULL(ptr)
Definition: assert.h:66
uint8_t byte
Definition: types.h:30
secure_vector< byte > & state()
Definition: cbc.h:45
Definition: buf_comp.h:15
CBC_Decryption(BlockCipher *cipher, BlockCipherModePaddingMethod *padding)
Definition: cbc.h:100
CTS_Encryption(BlockCipher *cipher)
Definition: cbc.h:83
CTS_Decryption(BlockCipher *cipher)
Definition: cbc.h:120
const BlockCipher & cipher() const
Definition: cbc.h:37