Coverage Report

Created: 2026-06-16 07:05

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