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