Botan 2.13.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 BOTAN_FUTURE_INTERNAL_HEADER(cbc.h)
17
18 namespace Botan {
19
20 /**
21 * CBC Mode
22 */
23 class BOTAN_PUBLIC_API(2,0) CBC_Mode : public Cipher_Mode
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:
41 CBC_Mode(BlockCipher* cipher, BlockCipherModePaddingMethod* padding);
42
43 const BlockCipher& cipher() const { return *m_cipher; }
44
45 const BlockCipherModePaddingMethod& padding() const
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 */
71 class BOTAN_PUBLIC_API(2,0) CBC_Encryption : public CBC_Mode
72 {
73 public:
74 /**
75 * @param cipher block cipher to use
76 * @param padding padding method to use
77 */
78 CBC_Encryption(BlockCipher* cipher, BlockCipherModePaddingMethod* padding) :
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 */
93 class BOTAN_PUBLIC_API(2,0) CTS_Encryption final : public CBC_Encryption
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 */
113 class BOTAN_PUBLIC_API(2,0) CBC_Decryption : public CBC_Mode
114 {
115 public:
116 /**
117 * @param cipher block cipher to use
118 * @param padding padding method to use
119 */
120 CBC_Decryption(BlockCipher* cipher, BlockCipherModePaddingMethod* padding) :
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 */
140 class BOTAN_PUBLIC_API(2,0) CTS_Decryption final : public CBC_Decryption
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
Botan::CBC_Mode::state
secure_vector< uint8_t > & state()
Definition: cbc.h:53
Botan::CBC_Decryption::finish
void finish(secure_vector< uint8_t > &final_block, size_t offset=0) override
Definition: cbc.cpp:242
m_cipher
CCCryptorRef m_cipher
Definition: commoncrypto_mode.cpp:50
Botan::CBC_Mode::valid_nonce_length
bool valid_nonce_length(size_t n) const override
Definition: cbc.cpp:61
Botan::Key_Length_Specification
Definition: key_spec.h:18
final
int(* final)(unsigned char *, CTX *)
Definition: commoncrypto_hash.cpp:28
Botan::Cipher_Mode::update_granularity
virtual size_t update_granularity() const =0
BOTAN_PUBLIC_API
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:31
Botan::CBC_Mode::padding
const BlockCipherModePaddingMethod & padding() const
Definition: cbc.h:45
Botan::Cipher_Mode::start_msg
virtual void start_msg(const uint8_t nonce[], size_t nonce_len)=0
Botan::CTS_Decryption
Definition: cbc.h:140
Botan::Cipher_Mode::reset
virtual void reset()=0
Botan::CTS_Encryption
Definition: cbc.h:93
Botan::CBC_Decryption
Definition: cbc.h:113
Botan::Cipher_Mode::minimum_final_size
virtual size_t minimum_final_size() const =0
Botan::CBC_Encryption::CBC_Encryption
CBC_Encryption(BlockCipher *cipher, BlockCipherModePaddingMethod *padding)
Definition: cbc.h:78
Botan::Cipher_Mode::default_nonce_length
virtual size_t default_nonce_length() const =0
Botan::CBC_Decryption::minimum_final_size
size_t minimum_final_size() const override
Definition: cbc.cpp:209
Botan::CBC_Encryption
Definition: cbc.h:71
Botan::Cipher_Mode::output_length
virtual size_t output_length(size_t input_length) const =0
Botan::secure_vector
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:65
Botan::CBC_Mode::block_size
size_t block_size() const
Definition: cbc.h:51
Botan::SymmetricAlgorithm::name
virtual std::string name() const =0
BOTAN_ASSERT_NONNULL
#define BOTAN_ASSERT_NONNULL(ptr)
Definition: assert.h:107
Botan::CBC_Mode::reset
void reset() override
Definition: cbc.cpp:33
Botan::CBC_Mode::state_ptr
uint8_t * state_ptr()
Definition: cbc.h:55
Botan::SymmetricAlgorithm::clear
virtual void clear()=0
Botan
Definition: alg_id.cpp:13
Botan::BlockCipherModePaddingMethod
Definition: mode_pad.h:28
Botan::CBC_Decryption::CBC_Decryption
CBC_Decryption(BlockCipher *cipher, BlockCipherModePaddingMethod *padding)
Definition: cbc.h:120
Botan::CTS_Encryption::CTS_Encryption
CTS_Encryption(BlockCipher *cipher)
Definition: cbc.h:99
Botan::CTS_Decryption::CTS_Decryption
CTS_Decryption(BlockCipher *cipher)
Definition: cbc.h:146
Botan::SymmetricAlgorithm::key_spec
virtual Key_Length_Specification key_spec() const =0
Botan::Cipher_Mode
Definition: cipher_mode.h:28
Botan::Cipher_Mode::process
virtual size_t process(uint8_t msg[], size_t msg_len)=0
Botan::CBC_Mode::cipher
const BlockCipher & cipher() const
Definition: cbc.h:43
Botan::CBC_Encryption::finish
void finish(secure_vector< uint8_t > &final_block, size_t offset=0) override
Definition: cbc.cpp:127
Botan::CBC_Encryption::minimum_final_size
size_t minimum_final_size() const override
Definition: cbc.cpp:89
m_block_size
size_t m_block_size
Definition: openssl_mode.cpp:47
BOTAN_FUTURE_INTERNAL_HEADER
#define BOTAN_FUTURE_INTERNAL_HEADER(hdr)
Definition: compiler.h:139
Botan::CBC_Encryption::output_length
size_t output_length(size_t input_length) const override
Definition: cbc.cpp:94
Botan::Cipher_Mode::valid_nonce_length
virtual bool valid_nonce_length(size_t nonce_len) const =0
Botan::Cipher_Mode::finish
virtual void finish(secure_vector< uint8_t > &final_block, size_t offset=0)=0
Botan::CBC_Mode
Definition: cbc.h:23
Botan::BlockCipher
Definition: block_cipher.h:21