Botan  2.12.1
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 
17 
18 namespace Botan {
19 
20 /**
21 * CBC Mode
22 */
24  {
25  public:
26  std::string name() const override;
27 
28  size_t update_granularity() const override;
29 
30  Key_Length_Specification key_spec() const override;
31 
32  size_t default_nonce_length() const override;
33 
34  bool valid_nonce_length(size_t n) const override;
35 
36  void clear() override;
37 
38  void reset() override;
39 
40  protected:
42 
43  const BlockCipher& cipher() const { return *m_cipher; }
44 
46  {
47  BOTAN_ASSERT_NONNULL(m_padding);
48  return *m_padding;
49  }
50 
51  size_t block_size() const { return m_block_size; }
52 
53  secure_vector<uint8_t>& state() { return m_state; }
54 
55  uint8_t* state_ptr() { return m_state.data(); }
56 
57  private:
58  void start_msg(const uint8_t nonce[], size_t nonce_len) override;
59 
60  void key_schedule(const uint8_t key[], size_t length) override;
61 
62  std::unique_ptr<BlockCipher> m_cipher;
63  std::unique_ptr<BlockCipherModePaddingMethod> m_padding;
64  secure_vector<uint8_t> m_state;
65  size_t m_block_size;
66  };
67 
68 /**
69 * CBC Encryption
70 */
72  {
73  public:
74  /**
75  * @param cipher block cipher to use
76  * @param padding padding method to use
77  */
79  CBC_Mode(cipher, padding) {}
80 
81  size_t process(uint8_t buf[], size_t size) override;
82 
83  void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
84 
85  size_t output_length(size_t input_length) const override;
86 
87  size_t minimum_final_size() const override;
88  };
89 
90 /**
91 * CBC Encryption with ciphertext stealing (CBC-CS3 variant)
92 */
94  {
95  public:
96  /**
97  * @param cipher block cipher to use
98  */
99  explicit CTS_Encryption(BlockCipher* cipher) : CBC_Encryption(cipher, nullptr) {}
100 
101  size_t output_length(size_t input_length) const override;
102 
103  void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
104 
105  size_t minimum_final_size() const override;
106 
107  bool valid_nonce_length(size_t n) const override;
108  };
109 
110 /**
111 * CBC Decryption
112 */
114  {
115  public:
116  /**
117  * @param cipher block cipher to use
118  * @param padding padding method to use
119  */
121  CBC_Mode(cipher, padding), m_tempbuf(update_granularity()) {}
122 
123  size_t process(uint8_t buf[], size_t size) override;
124 
125  void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
126 
127  size_t output_length(size_t input_length) const override;
128 
129  size_t minimum_final_size() const override;
130 
131  void reset() override;
132 
133  private:
134  secure_vector<uint8_t> m_tempbuf;
135  };
136 
137 /**
138 * CBC Decryption with ciphertext stealing (CBC-CS3 variant)
139 */
141  {
142  public:
143  /**
144  * @param cipher block cipher to use
145  */
146  explicit CTS_Decryption(BlockCipher* cipher) : CBC_Decryption(cipher, nullptr) {}
147 
148  void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
149 
150  size_t minimum_final_size() const override;
151 
152  bool valid_nonce_length(size_t n) const override;
153  };
154 
155 }
156 
157 #endif
secure_vector< uint8_t > & state()
Definition: cbc.h:53
void finish(secure_vector< uint8_t > &final_block, size_t offset=0) override
Definition: cbc.cpp:242
const BlockCipherModePaddingMethod & padding() const
Definition: cbc.h:45
bool valid_nonce_length(size_t n) const override
Definition: cbc.cpp:61
int(* final)(unsigned char *, CTX *)
virtual size_t update_granularity() const =0
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:31
virtual void start_msg(const uint8_t nonce[], size_t nonce_len)=0
virtual void reset()=0
virtual size_t minimum_final_size() const =0
CBC_Encryption(BlockCipher *cipher, BlockCipherModePaddingMethod *padding)
Definition: cbc.h:78
virtual size_t default_nonce_length() const =0
size_t minimum_final_size() const override
Definition: cbc.cpp:209
virtual size_t output_length(size_t input_length) const =0
virtual std::string name() const =0
#define BOTAN_ASSERT_NONNULL(ptr)
Definition: assert.h:107
void reset() override
Definition: cbc.cpp:33
uint8_t * state_ptr()
Definition: cbc.h:55
virtual void clear()=0
Definition: alg_id.cpp:13
size_t block_size() const
Definition: cbc.h:51
CBC_Decryption(BlockCipher *cipher, BlockCipherModePaddingMethod *padding)
Definition: cbc.h:120
CTS_Encryption(BlockCipher *cipher)
Definition: cbc.h:99
CTS_Decryption(BlockCipher *cipher)
Definition: cbc.h:146
virtual Key_Length_Specification key_spec() const =0
virtual size_t process(uint8_t msg[], size_t msg_len)=0
void finish(secure_vector< uint8_t > &final_block, size_t offset=0) override
Definition: cbc.cpp:127
size_t minimum_final_size() const override
Definition: cbc.cpp:89
const BlockCipher & cipher() const
Definition: cbc.h:43
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:65
#define BOTAN_FUTURE_INTERNAL_HEADER(hdr)
Definition: compiler.h:139
size_t output_length(size_t input_length) const override
Definition: cbc.cpp:94
virtual bool valid_nonce_length(size_t nonce_len) const =0
virtual void finish(secure_vector< uint8_t > &final_block, size_t offset=0)=0