/src/botan/src/lib/modes/cbc/cbc.cpp
Line | Count | Source |
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/exceptn.h> |
13 | | #include <botan/mem_ops.h> |
14 | | #include <botan/internal/fmt.h> |
15 | | #include <botan/internal/int_utils.h> |
16 | | #include <botan/internal/mode_pad.h> |
17 | | |
18 | | namespace Botan { |
19 | | |
20 | | CBC_Mode::CBC_Mode(std::unique_ptr<BlockCipher> cipher, std::unique_ptr<BlockCipherModePaddingMethod> padding) : |
21 | 292 | m_cipher(std::move(cipher)), m_padding(std::move(padding)), m_block_size(m_cipher->block_size()) { |
22 | 292 | if(m_padding && !m_padding->valid_blocksize(m_block_size)) { |
23 | 0 | throw Invalid_Argument(fmt("Padding {} cannot be used with {} in CBC mode", m_padding->name(), m_cipher->name())); |
24 | 0 | } |
25 | 292 | } |
26 | | |
27 | 0 | void CBC_Mode::clear() { |
28 | 0 | m_cipher->clear(); |
29 | 0 | reset(); |
30 | 0 | } |
31 | | |
32 | 0 | void CBC_Mode::reset() { |
33 | 0 | m_state.clear(); |
34 | 0 | } |
35 | | |
36 | 0 | std::string CBC_Mode::name() const { |
37 | 0 | if(m_padding) { |
38 | 0 | return fmt("{}/CBC/{}", cipher().name(), padding().name()); |
39 | 0 | } else { |
40 | 0 | return fmt("{}/CBC/CTS", cipher().name()); |
41 | 0 | } |
42 | 0 | } |
43 | | |
44 | 0 | size_t CBC_Mode::update_granularity() const { |
45 | 0 | return cipher().block_size(); |
46 | 0 | } |
47 | | |
48 | 198 | size_t CBC_Mode::ideal_granularity() const { |
49 | 198 | return cipher().parallel_bytes(); |
50 | 198 | } |
51 | | |
52 | 292 | Key_Length_Specification CBC_Mode::key_spec() const { |
53 | 292 | return cipher().key_spec(); |
54 | 292 | } |
55 | | |
56 | 0 | size_t CBC_Mode::default_nonce_length() const { |
57 | 0 | return block_size(); |
58 | 0 | } |
59 | | |
60 | 280 | bool CBC_Mode::valid_nonce_length(size_t n) const { |
61 | 280 | return (n == 0 || n == block_size()); |
62 | 280 | } |
63 | | |
64 | 0 | bool CBC_Mode::has_keying_material() const { |
65 | 0 | return m_cipher->has_keying_material(); |
66 | 0 | } |
67 | | |
68 | 292 | void CBC_Mode::key_schedule(std::span<const uint8_t> key) { |
69 | 292 | m_cipher->set_key(key); |
70 | 292 | m_state.clear(); |
71 | 292 | } |
72 | | |
73 | 280 | void CBC_Mode::start_msg(const uint8_t nonce[], size_t nonce_len) { |
74 | 280 | if(!valid_nonce_length(nonce_len)) { |
75 | 0 | throw Invalid_IV_Length(name(), nonce_len); |
76 | 0 | } |
77 | | |
78 | | /* |
79 | | * A nonce of zero length means carry the last ciphertext value over |
80 | | * as the new IV, as unfortunately some protocols require this. If |
81 | | * this is the first message then we use an IV of all zeros. |
82 | | */ |
83 | 280 | if(nonce_len > 0) { |
84 | 280 | m_state.assign(nonce, nonce + nonce_len); |
85 | 280 | } else if(m_state.empty()) { |
86 | 0 | m_state.resize(m_cipher->block_size()); |
87 | 0 | } |
88 | | // else leave the state alone |
89 | 280 | } |
90 | | |
91 | 0 | size_t CBC_Encryption::minimum_final_size() const { |
92 | 0 | return 0; |
93 | 0 | } |
94 | | |
95 | 0 | size_t CBC_Encryption::output_length(size_t input_length) const { |
96 | 0 | return padding().output_length(input_length, block_size()); |
97 | 0 | } |
98 | | |
99 | 170 | size_t CBC_Encryption::process_msg(uint8_t buf[], size_t sz) { |
100 | 170 | BOTAN_STATE_CHECK(state().empty() == false); |
101 | 170 | const size_t BS = block_size(); |
102 | | |
103 | 170 | BOTAN_ARG_CHECK(sz % BS == 0, "CBC input is not full blocks"); |
104 | 170 | const size_t blocks = sz / BS; |
105 | | |
106 | 170 | if(blocks > 0) { |
107 | 170 | xor_buf(&buf[0], state_ptr(), BS); |
108 | 170 | cipher().encrypt(&buf[0]); |
109 | | |
110 | 544 | for(size_t i = 1; i != blocks; ++i) { |
111 | 374 | xor_buf(&buf[BS * i], &buf[BS * (i - 1)], BS); |
112 | 374 | cipher().encrypt(&buf[BS * i]); |
113 | 374 | } |
114 | | |
115 | 170 | state().assign(&buf[BS * (blocks - 1)], &buf[BS * blocks]); |
116 | 170 | } |
117 | | |
118 | 170 | return sz; |
119 | 170 | } |
120 | | |
121 | 0 | void CBC_Encryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset) { |
122 | 0 | BOTAN_STATE_CHECK(state().empty() == false); |
123 | 0 | BOTAN_ARG_CHECK(buffer.size() >= offset, "Offset is out of range"); |
124 | |
|
125 | 0 | const size_t BS = block_size(); |
126 | |
|
127 | 0 | const size_t output_bytes = |
128 | 0 | add_or_throw(offset, padding().output_length(buffer.size() - offset, BS), "CBC input too large"); |
129 | 0 | const size_t bytes_in_final_block = (buffer.size() - offset) % BS; |
130 | 0 | buffer.resize(output_bytes); |
131 | 0 | padding().add_padding(std::span(buffer).subspan(offset), 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 | 110 | size_t CBC_Decryption::process_msg(uint8_t buf[], size_t sz) { |
201 | 110 | BOTAN_STATE_CHECK(state().empty() == false); |
202 | | |
203 | 110 | const size_t BS = block_size(); |
204 | | |
205 | 110 | BOTAN_ARG_CHECK(sz % BS == 0, "Input is not full blocks"); |
206 | 110 | size_t blocks = sz / BS; |
207 | | |
208 | 546 | while(blocks > 0) { |
209 | 436 | const size_t to_proc = std::min(BS * blocks, m_tempbuf.size()); |
210 | | |
211 | 436 | cipher().decrypt_n(buf, m_tempbuf.data(), to_proc / BS); |
212 | | |
213 | 436 | xor_buf(m_tempbuf.data(), state_ptr(), BS); |
214 | 436 | xor_buf(&m_tempbuf[BS], buf, to_proc - BS); |
215 | 436 | copy_mem(state_ptr(), buf + (to_proc - BS), BS); |
216 | | |
217 | 436 | copy_mem(buf, m_tempbuf.data(), to_proc); |
218 | | |
219 | 436 | buf += to_proc; |
220 | 436 | blocks -= to_proc / BS; |
221 | 436 | } |
222 | | |
223 | 110 | return sz; |
224 | 110 | } |
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 != 0) { |
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(std::span{buffer}.last(BS)); |
240 | 0 | buffer.resize(buffer.size() - pad_bytes); // remove padding |
241 | 0 | if(pad_bytes == 0 && padding().name() != "NoPadding") { |
242 | 0 | clear_mem(std::span{buffer}.subspan(offset)); |
243 | 0 | throw Decoding_Error("Invalid CBC padding"); |
244 | 0 | } |
245 | 0 | } |
246 | | |
247 | 0 | void CBC_Decryption::reset() { |
248 | 0 | CBC_Mode::reset(); |
249 | 0 | zeroise(m_tempbuf); |
250 | 0 | } |
251 | | |
252 | 0 | bool CTS_Decryption::valid_nonce_length(size_t n) const { |
253 | 0 | return (n == block_size()); |
254 | 0 | } |
255 | | |
256 | 0 | size_t CTS_Decryption::minimum_final_size() const { |
257 | 0 | return block_size() + 1; |
258 | 0 | } |
259 | | |
260 | 0 | void CTS_Decryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset) { |
261 | 0 | BOTAN_STATE_CHECK(state().empty() == false); |
262 | 0 | BOTAN_ARG_CHECK(buffer.size() >= offset, "Offset is out of range"); |
263 | 0 | const size_t sz = buffer.size() - offset; |
264 | 0 | uint8_t* buf = buffer.data() + offset; |
265 | |
|
266 | 0 | const size_t BS = block_size(); |
267 | |
|
268 | 0 | if(sz < BS + 1) { |
269 | 0 | throw Encoding_Error(name() + ": insufficient data to decrypt"); |
270 | 0 | } |
271 | | |
272 | 0 | if(sz % BS == 0) { |
273 | | // swap last two blocks |
274 | |
|
275 | 0 | for(size_t i = 0; i != BS; ++i) { |
276 | 0 | std::swap(buffer[buffer.size() - BS + i], buffer[buffer.size() - 2 * BS + i]); |
277 | 0 | } |
278 | |
|
279 | 0 | update(buffer, offset); |
280 | 0 | } else { |
281 | 0 | const size_t full_blocks = ((sz / BS) - 1) * BS; |
282 | 0 | const size_t final_bytes = sz - full_blocks; |
283 | 0 | BOTAN_ASSERT(final_bytes > BS && final_bytes < 2 * BS, "Left over size in expected range"); |
284 | |
|
285 | 0 | secure_vector<uint8_t> last(buf + full_blocks, buf + full_blocks + final_bytes); |
286 | 0 | buffer.resize(full_blocks + offset); |
287 | 0 | update(buffer, offset); |
288 | |
|
289 | 0 | cipher().decrypt(last.data()); |
290 | |
|
291 | 0 | xor_buf(last.data(), &last[BS], final_bytes - BS); |
292 | |
|
293 | 0 | for(size_t i = 0; i != final_bytes - BS; ++i) { |
294 | 0 | std::swap(last[i], last[i + BS]); |
295 | 0 | } |
296 | |
|
297 | 0 | cipher().decrypt(last.data()); |
298 | 0 | xor_buf(last.data(), state_ptr(), BS); |
299 | |
|
300 | 0 | buffer += last; |
301 | 0 | } |
302 | 0 | } |
303 | | |
304 | | } // namespace Botan |