Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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:
39 CBC_Mode(BlockCipher* cipher, BlockCipherModePaddingMethod* padding);
40
41 const BlockCipher& cipher() const { return *m_cipher; }
42
43 const BlockCipherModePaddingMethod& padding() const
44 {
45 BOTAN_ASSERT_NONNULL(m_padding);
46 return *m_padding;
47 }
48
49 secure_vector<uint8_t>& state() { return m_state; }
50
51 uint8_t* state_ptr() { return m_state.data(); }
52
53 private:
54 void start_msg(const uint8_t nonce[], size_t nonce_len) override;
55
56 void key_schedule(const uint8_t key[], size_t length) override;
57
58 std::unique_ptr<BlockCipher> m_cipher;
59 std::unique_ptr<BlockCipherModePaddingMethod> m_padding;
60 secure_vector<uint8_t> 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 */
73 CBC_Encryption(BlockCipher* cipher, BlockCipherModePaddingMethod* padding) :
74 CBC_Mode(cipher, padding) {}
75
76 size_t process(uint8_t buf[], size_t size) override;
77
78 void finish(secure_vector<uint8_t>& 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<uint8_t>& 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 */
115 CBC_Decryption(BlockCipher* cipher, BlockCipherModePaddingMethod* padding) :
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<uint8_t>& 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<uint8_t> 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<uint8_t>& 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
Botan::CBC_Mode::state
secure_vector< uint8_t > & state()
Definition: cbc.h:49
Botan::Key_Length_Specification
Definition: key_spec.h:18
Botan::CBC_Mode::padding
const BlockCipherModePaddingMethod & padding() const
Definition: cbc.h:43
Botan::CTS_Decryption
Definition: cbc.h:135
Botan::CTS_Encryption
Definition: cbc.h:88
Botan::CBC_Decryption
Definition: cbc.h:108
Botan::CBC_Encryption::CBC_Encryption
CBC_Encryption(BlockCipher *cipher, BlockCipherModePaddingMethod *padding)
Definition: cbc.h:73
Botan::CBC_Encryption
Definition: cbc.h:66
Botan::secure_vector
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:121
BOTAN_ASSERT_NONNULL
#define BOTAN_ASSERT_NONNULL(ptr)
Definition: assert.h:79
Botan::CBC_Mode::state_ptr
uint8_t * state_ptr()
Definition: cbc.h:51
Botan
Definition: alg_id.cpp:13
Botan::BlockCipherModePaddingMethod
Definition: mode_pad.h:26
Botan::CBC_Decryption::CBC_Decryption
CBC_Decryption(BlockCipher *cipher, BlockCipherModePaddingMethod *padding)
Definition: cbc.h:115
Botan::CTS_Encryption::CTS_Encryption
CTS_Encryption(BlockCipher *cipher)
Definition: cbc.h:94
Botan::CTS_Decryption::CTS_Decryption
CTS_Decryption(BlockCipher *cipher)
Definition: cbc.h:141
Botan::Cipher_Mode
Definition: cipher_mode.h:23
Botan::CBC_Mode::cipher
const BlockCipher & cipher() const
Definition: cbc.h:41
Botan::CBC_Mode
Definition: cbc.h:21
Botan::BlockCipher
Definition: block_cipher.h:19