Botan 3.6.1
Crypto and TLS for C&
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/block_cipher.h>
13#include <botan/cipher_mode.h>
14#include <botan/internal/mode_pad.h>
15
16namespace Botan {
17
18/**
19* CBC Mode
20*/
21class CBC_Mode : public Cipher_Mode {
22 public:
23 std::string name() const final;
24
25 size_t update_granularity() const final;
26
27 size_t ideal_granularity() const final;
28
30
31 size_t default_nonce_length() const final;
32
33 bool valid_nonce_length(size_t n) const override;
34
35 void clear() final;
36
37 void reset() override;
38
39 bool has_keying_material() const final;
40
41 protected:
42 CBC_Mode(std::unique_ptr<BlockCipher> cipher, std::unique_ptr<BlockCipherModePaddingMethod> padding);
43
44 const BlockCipher& cipher() const { return *m_cipher; }
45
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(std::span<const uint8_t> key) override;
61
62 std::unique_ptr<BlockCipher> m_cipher;
63 std::unique_ptr<BlockCipherModePaddingMethod> m_padding;
65 size_t m_block_size;
66};
67
68/**
69* CBC Encryption
70*/
71class CBC_Encryption : public CBC_Mode {
72 public:
73 /**
74 * @param cipher block cipher to use
75 * @param padding padding method to use
76 */
77 CBC_Encryption(std::unique_ptr<BlockCipher> cipher, std::unique_ptr<BlockCipherModePaddingMethod> padding) :
78 CBC_Mode(std::move(cipher), std::move(padding)) {}
79
80 size_t output_length(size_t input_length) const override;
81
82 size_t minimum_final_size() const override;
83
84 private:
85 size_t process_msg(uint8_t buf[], size_t size) override;
86 void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
87};
88
89/**
90* CBC Encryption with ciphertext stealing (CBC-CS3 variant)
91*/
93 public:
94 /**
95 * @param cipher block cipher to use
96 */
97 explicit CTS_Encryption(std::unique_ptr<BlockCipher> cipher) : CBC_Encryption(std::move(cipher), nullptr) {}
98
99 size_t output_length(size_t input_length) const override;
100
101 size_t minimum_final_size() const override;
102
103 bool valid_nonce_length(size_t n) const override;
104
105 private:
106 void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
107};
108
109/**
110* CBC Decryption
111*/
112class CBC_Decryption : public CBC_Mode {
113 public:
114 /**
115 * @param cipher block cipher to use
116 * @param padding padding method to use
117 */
118 CBC_Decryption(std::unique_ptr<BlockCipher> cipher, std::unique_ptr<BlockCipherModePaddingMethod> padding) :
119 CBC_Mode(std::move(cipher), std::move(padding)), m_tempbuf(ideal_granularity()) {}
120
121 size_t output_length(size_t input_length) const override;
122
123 size_t minimum_final_size() const override;
124
125 void reset() override;
126
127 private:
128 size_t process_msg(uint8_t buf[], size_t size) override;
129 void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
130
131 secure_vector<uint8_t> m_tempbuf;
132};
133
134/**
135* CBC Decryption with ciphertext stealing (CBC-CS3 variant)
136*/
138 public:
139 /**
140 * @param cipher block cipher to use
141 */
142 explicit CTS_Decryption(std::unique_ptr<BlockCipher> cipher) : CBC_Decryption(std::move(cipher), nullptr) {}
143
144 size_t minimum_final_size() const override;
145
146 bool valid_nonce_length(size_t n) const override;
147
148 private:
149 void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
150};
151
152} // namespace Botan
153
154#endif
#define BOTAN_ASSERT_NONNULL(ptr)
Definition assert.h:86
size_t minimum_final_size() const override
Definition cbc.cpp:196
CBC_Decryption(std::unique_ptr< BlockCipher > cipher, std::unique_ptr< BlockCipherModePaddingMethod > padding)
Definition cbc.h:118
size_t output_length(size_t input_length) const override
Definition cbc.cpp:192
void reset() override
Definition cbc.cpp:246
size_t minimum_final_size() const override
Definition cbc.cpp:89
size_t output_length(size_t input_length) const override
Definition cbc.cpp:93
CBC_Encryption(std::unique_ptr< BlockCipher > cipher, std::unique_ptr< BlockCipherModePaddingMethod > padding)
Definition cbc.h:77
std::string name() const final
Definition cbc.cpp:34
size_t update_granularity() const final
Definition cbc.cpp:42
size_t ideal_granularity() const final
Definition cbc.cpp:46
const BlockCipherModePaddingMethod & padding() const
Definition cbc.h:46
size_t block_size() const
Definition cbc.h:51
bool valid_nonce_length(size_t n) const override
Definition cbc.cpp:58
CBC_Mode(std::unique_ptr< BlockCipher > cipher, std::unique_ptr< BlockCipherModePaddingMethod > padding)
Definition cbc.cpp:18
void reset() override
Definition cbc.cpp:30
size_t default_nonce_length() const final
Definition cbc.cpp:54
const BlockCipher & cipher() const
Definition cbc.h:44
void clear() final
Definition cbc.cpp:25
secure_vector< uint8_t > & state()
Definition cbc.h:53
uint8_t * state_ptr()
Definition cbc.h:55
Key_Length_Specification key_spec() const final
Definition cbc.cpp:50
bool has_keying_material() const final
Definition cbc.cpp:62
CTS_Decryption(std::unique_ptr< BlockCipher > cipher)
Definition cbc.h:142
bool valid_nonce_length(size_t n) const override
Definition cbc.cpp:251
size_t minimum_final_size() const override
Definition cbc.cpp:255
CTS_Encryption(std::unique_ptr< BlockCipher > cipher)
Definition cbc.h:97
size_t output_length(size_t input_length) const override
Definition cbc.cpp:146
size_t minimum_final_size() const override
Definition cbc.cpp:142
bool valid_nonce_length(size_t n) const override
Definition cbc.cpp:138
int(* final)(unsigned char *, CTX *)
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61