/src/botan/src/lib/modes/cbc/cbc.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * CBC Mode |
3 | | * (C) 1999-2007,2013,2017 Jack Lloyd |
4 | | * (C) 2016 Daniel Neus, Rohde & Schwarz Cybersecurity |
5 | | * (C) 2018 Ribose Inc |
6 | | * |
7 | | * Botan is released under the Simplified BSD License (see license.txt) |
8 | | */ |
9 | | |
10 | | #include <botan/internal/cbc.h> |
11 | | |
12 | | #include <botan/internal/fmt.h> |
13 | | #include <botan/internal/mode_pad.h> |
14 | | #include <botan/internal/rounding.h> |
15 | | |
16 | | namespace Botan { |
17 | | |
18 | | CBC_Mode::CBC_Mode(std::unique_ptr<BlockCipher> cipher, std::unique_ptr<BlockCipherModePaddingMethod> padding) : |
19 | 0 | m_cipher(std::move(cipher)), m_padding(std::move(padding)), m_block_size(m_cipher->block_size()) { |
20 | 0 | if(m_padding && !m_padding->valid_blocksize(m_block_size)) { |
21 | 0 | throw Invalid_Argument(fmt("Padding {} cannot be used with {} in CBC mode", m_padding->name(), m_cipher->name())); |
22 | 0 | } |
23 | 0 | } |
24 | | |
25 | 0 | void CBC_Mode::clear() { |
26 | 0 | m_cipher->clear(); |
27 | 0 | reset(); |
28 | 0 | } |
29 | | |
30 | 0 | void CBC_Mode::reset() { |
31 | 0 | m_state.clear(); |
32 | 0 | } |
33 | | |
34 | 0 | std::string CBC_Mode::name() const { |
35 | 0 | if(m_padding) { |
36 | 0 | return fmt("{}/CBC/{}", cipher().name(), padding().name()); |
37 | 0 | } else { |
38 | 0 | return fmt("{}/CBC/CTS", cipher().name()); |
39 | 0 | } |
40 | 0 | } |
41 | | |
42 | 0 | size_t CBC_Mode::update_granularity() const { |
43 | 0 | return cipher().block_size(); |
44 | 0 | } |
45 | | |
46 | 0 | size_t CBC_Mode::ideal_granularity() const { |
47 | 0 | return cipher().parallel_bytes(); |
48 | 0 | } |
49 | | |
50 | 0 | Key_Length_Specification CBC_Mode::key_spec() const { |
51 | 0 | return cipher().key_spec(); |
52 | 0 | } |
53 | | |
54 | 0 | size_t CBC_Mode::default_nonce_length() const { |
55 | 0 | return block_size(); |
56 | 0 | } |
57 | | |
58 | 0 | bool CBC_Mode::valid_nonce_length(size_t n) const { |
59 | 0 | return (n == 0 || n == block_size()); |
60 | 0 | } |
61 | | |
62 | 0 | bool CBC_Mode::has_keying_material() const { |
63 | 0 | return m_cipher->has_keying_material(); |
64 | 0 | } |
65 | | |
66 | 0 | void CBC_Mode::key_schedule(std::span<const uint8_t> key) { |
67 | 0 | m_cipher->set_key(key); |
68 | 0 | m_state.clear(); |
69 | 0 | } |
70 | | |
71 | 0 | void CBC_Mode::start_msg(const uint8_t nonce[], size_t nonce_len) { |
72 | 0 | if(!valid_nonce_length(nonce_len)) { |
73 | 0 | throw Invalid_IV_Length(name(), nonce_len); |
74 | 0 | } |
75 | | |
76 | | /* |
77 | | * A nonce of zero length means carry the last ciphertext value over |
78 | | * as the new IV, as unfortunately some protocols require this. If |
79 | | * this is the first message then we use an IV of all zeros. |
80 | | */ |
81 | 0 | if(nonce_len) { |
82 | 0 | m_state.assign(nonce, nonce + nonce_len); |
83 | 0 | } else if(m_state.empty()) { |
84 | 0 | m_state.resize(m_cipher->block_size()); |
85 | 0 | } |
86 | | // else leave the state alone |
87 | 0 | } |
88 | | |
89 | 0 | size_t CBC_Encryption::minimum_final_size() const { |
90 | 0 | return 0; |
91 | 0 | } |
92 | | |
93 | 0 | size_t CBC_Encryption::output_length(size_t input_length) const { |
94 | 0 | if(input_length == 0) { |
95 | 0 | return block_size(); |
96 | 0 | } else { |
97 | 0 | return round_up(input_length, block_size()); |
98 | 0 | } |
99 | 0 | } |
100 | | |
101 | 0 | size_t CBC_Encryption::process_msg(uint8_t buf[], size_t sz) { |
102 | 0 | BOTAN_STATE_CHECK(state().empty() == false); |
103 | 0 | const size_t BS = block_size(); |
104 | |
|
105 | 0 | BOTAN_ARG_CHECK(sz % BS == 0, "CBC input is not full blocks"); |
106 | 0 | const size_t blocks = sz / BS; |
107 | |
|
108 | 0 | if(blocks > 0) { |
109 | 0 | xor_buf(&buf[0], state_ptr(), BS); |
110 | 0 | cipher().encrypt(&buf[0]); |
111 | |
|
112 | 0 | for(size_t i = 1; i != blocks; ++i) { |
113 | 0 | xor_buf(&buf[BS * i], &buf[BS * (i - 1)], BS); |
114 | 0 | cipher().encrypt(&buf[BS * i]); |
115 | 0 | } |
116 | |
|
117 | 0 | state().assign(&buf[BS * (blocks - 1)], &buf[BS * blocks]); |
118 | 0 | } |
119 | |
|
120 | 0 | return sz; |
121 | 0 | } |
122 | | |
123 | 0 | void CBC_Encryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset) { |
124 | 0 | BOTAN_STATE_CHECK(state().empty() == false); |
125 | 0 | BOTAN_ARG_CHECK(buffer.size() >= offset, "Offset is out of range"); |
126 | |
|
127 | 0 | const size_t BS = block_size(); |
128 | |
|
129 | 0 | const size_t bytes_in_final_block = (buffer.size() - offset) % BS; |
130 | |
|
131 | 0 | padding().add_padding(buffer, bytes_in_final_block, BS); |
132 | |
|
133 | 0 | BOTAN_ASSERT_EQUAL(buffer.size() % BS, offset % BS, "Padded to block boundary"); |
134 | |
|
135 | 0 | update(buffer, offset); |
136 | 0 | } |
137 | | |
138 | 0 | bool CTS_Encryption::valid_nonce_length(size_t n) const { |
139 | 0 | return (n == block_size()); |
140 | 0 | } |
141 | | |
142 | 0 | size_t CTS_Encryption::minimum_final_size() const { |
143 | 0 | return block_size() + 1; |
144 | 0 | } |
145 | | |
146 | 0 | size_t CTS_Encryption::output_length(size_t input_length) const { |
147 | 0 | return input_length; // no ciphertext expansion in CTS |
148 | 0 | } |
149 | | |
150 | 0 | void CTS_Encryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset) { |
151 | 0 | BOTAN_STATE_CHECK(state().empty() == false); |
152 | 0 | BOTAN_ARG_CHECK(buffer.size() >= offset, "Offset is out of range"); |
153 | 0 | uint8_t* buf = buffer.data() + offset; |
154 | 0 | const size_t sz = buffer.size() - offset; |
155 | |
|
156 | 0 | const size_t BS = block_size(); |
157 | |
|
158 | 0 | if(sz < BS + 1) { |
159 | 0 | throw Encoding_Error(name() + ": insufficient data to encrypt"); |
160 | 0 | } |
161 | | |
162 | 0 | if(sz % BS == 0) { |
163 | 0 | update(buffer, offset); |
164 | | |
165 | | // swap last two blocks |
166 | 0 | for(size_t i = 0; i != BS; ++i) { |
167 | 0 | std::swap(buffer[buffer.size() - BS + i], buffer[buffer.size() - 2 * BS + i]); |
168 | 0 | } |
169 | 0 | } else { |
170 | 0 | const size_t full_blocks = ((sz / BS) - 1) * BS; |
171 | 0 | const size_t final_bytes = sz - full_blocks; |
172 | 0 | BOTAN_ASSERT(final_bytes > BS && final_bytes < 2 * BS, "Left over size in expected range"); |
173 | |
|
174 | 0 | secure_vector<uint8_t> last(buf + full_blocks, buf + full_blocks + final_bytes); |
175 | 0 | buffer.resize(full_blocks + offset); |
176 | 0 | update(buffer, offset); |
177 | |
|
178 | 0 | xor_buf(last.data(), state_ptr(), BS); |
179 | 0 | cipher().encrypt(last.data()); |
180 | |
|
181 | 0 | for(size_t i = 0; i != final_bytes - BS; ++i) { |
182 | 0 | last[i] ^= last[i + BS]; |
183 | 0 | last[i + BS] ^= last[i]; |
184 | 0 | } |
185 | |
|
186 | 0 | cipher().encrypt(last.data()); |
187 | |
|
188 | 0 | buffer += last; |
189 | 0 | } |
190 | 0 | } |
191 | | |
192 | 0 | size_t CBC_Decryption::output_length(size_t input_length) const { |
193 | 0 | return input_length; // precise for CTS, worst case otherwise |
194 | 0 | } |
195 | | |
196 | 0 | size_t CBC_Decryption::minimum_final_size() const { |
197 | 0 | return block_size(); |
198 | 0 | } |
199 | | |
200 | 0 | size_t CBC_Decryption::process_msg(uint8_t buf[], size_t sz) { |
201 | 0 | BOTAN_STATE_CHECK(state().empty() == false); |
202 | |
|
203 | 0 | const size_t BS = block_size(); |
204 | |
|
205 | 0 | BOTAN_ARG_CHECK(sz % BS == 0, "Input is not full blocks"); |
206 | 0 | size_t blocks = sz / BS; |
207 | |
|
208 | 0 | while(blocks) { |
209 | 0 | const size_t to_proc = std::min(BS * blocks, m_tempbuf.size()); |
210 | |
|
211 | 0 | cipher().decrypt_n(buf, m_tempbuf.data(), to_proc / BS); |
212 | |
|
213 | 0 | xor_buf(m_tempbuf.data(), state_ptr(), BS); |
214 | 0 | xor_buf(&m_tempbuf[BS], buf, to_proc - BS); |
215 | 0 | copy_mem(state_ptr(), buf + (to_proc - BS), BS); |
216 | |
|
217 | 0 | copy_mem(buf, m_tempbuf.data(), to_proc); |
218 | |
|
219 | 0 | buf += to_proc; |
220 | 0 | blocks -= to_proc / BS; |
221 | 0 | } |
222 | |
|
223 | 0 | return sz; |
224 | 0 | } |
225 | | |
226 | 0 | void CBC_Decryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset) { |
227 | 0 | BOTAN_STATE_CHECK(state().empty() == false); |
228 | 0 | BOTAN_ARG_CHECK(buffer.size() >= offset, "Offset is out of range"); |
229 | 0 | const size_t sz = buffer.size() - offset; |
230 | |
|
231 | 0 | const size_t BS = block_size(); |
232 | |
|
233 | 0 | if(sz == 0 || sz % BS) { |
234 | 0 | throw Decoding_Error(name() + ": Ciphertext not a multiple of block size"); |
235 | 0 | } |
236 | | |
237 | 0 | update(buffer, offset); |
238 | |
|
239 | 0 | const size_t pad_bytes = BS - padding().unpad(&buffer[buffer.size() - BS], BS); |
240 | 0 | buffer.resize(buffer.size() - pad_bytes); // remove padding |
241 | 0 | if(pad_bytes == 0 && padding().name() != "NoPadding") { |
242 | 0 | throw Decoding_Error("Invalid CBC padding"); |
243 | 0 | } |
244 | 0 | } |
245 | | |
246 | 0 | void CBC_Decryption::reset() { |
247 | 0 | CBC_Mode::reset(); |
248 | 0 | zeroise(m_tempbuf); |
249 | 0 | } |
250 | | |
251 | 0 | bool CTS_Decryption::valid_nonce_length(size_t n) const { |
252 | 0 | return (n == block_size()); |
253 | 0 | } |
254 | | |
255 | 0 | size_t CTS_Decryption::minimum_final_size() const { |
256 | 0 | return block_size() + 1; |
257 | 0 | } |
258 | | |
259 | 0 | void CTS_Decryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset) { |
260 | 0 | BOTAN_STATE_CHECK(state().empty() == false); |
261 | 0 | BOTAN_ARG_CHECK(buffer.size() >= offset, "Offset is out of range"); |
262 | 0 | const size_t sz = buffer.size() - offset; |
263 | 0 | uint8_t* buf = buffer.data() + offset; |
264 | |
|
265 | 0 | const size_t BS = block_size(); |
266 | |
|
267 | 0 | if(sz < BS + 1) { |
268 | 0 | throw Encoding_Error(name() + ": insufficient data to decrypt"); |
269 | 0 | } |
270 | | |
271 | 0 | if(sz % BS == 0) { |
272 | | // swap last two blocks |
273 | |
|
274 | 0 | for(size_t i = 0; i != BS; ++i) { |
275 | 0 | std::swap(buffer[buffer.size() - BS + i], buffer[buffer.size() - 2 * BS + i]); |
276 | 0 | } |
277 | |
|
278 | 0 | update(buffer, offset); |
279 | 0 | } else { |
280 | 0 | const size_t full_blocks = ((sz / BS) - 1) * BS; |
281 | 0 | const size_t final_bytes = sz - full_blocks; |
282 | 0 | BOTAN_ASSERT(final_bytes > BS && final_bytes < 2 * BS, "Left over size in expected range"); |
283 | |
|
284 | 0 | secure_vector<uint8_t> last(buf + full_blocks, buf + full_blocks + final_bytes); |
285 | 0 | buffer.resize(full_blocks + offset); |
286 | 0 | update(buffer, offset); |
287 | |
|
288 | 0 | cipher().decrypt(last.data()); |
289 | |
|
290 | 0 | xor_buf(last.data(), &last[BS], final_bytes - BS); |
291 | |
|
292 | 0 | for(size_t i = 0; i != final_bytes - BS; ++i) { |
293 | 0 | std::swap(last[i], last[i + BS]); |
294 | 0 | } |
295 | |
|
296 | 0 | cipher().decrypt(last.data()); |
297 | 0 | xor_buf(last.data(), state_ptr(), BS); |
298 | |
|
299 | 0 | buffer += last; |
300 | 0 | } |
301 | 0 | } |
302 | | |
303 | | } // namespace Botan |