• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 13274522654

11 Feb 2025 11:26PM UTC coverage: 91.645% (-0.007%) from 91.652%
13274522654

push

github

web-flow
Merge pull request #4647 from randombit/jack/internal-assert-and-mem-ops

Avoid using mem_ops.h or assert.h in public headers

94854 of 103501 relevant lines covered (91.65%)

11334975.77 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.02
/src/lib/modes/cbc/cbc.cpp
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) :
3,414✔
20
      m_cipher(std::move(cipher)), m_padding(std::move(padding)), m_block_size(m_cipher->block_size()) {
3,414✔
21
   if(m_padding && !m_padding->valid_blocksize(m_block_size)) {
3,414✔
22
      throw Invalid_Argument(fmt("Padding {} cannot be used with {} in CBC mode", m_padding->name(), m_cipher->name()));
×
23
   }
24
}
3,414✔
25

26
void CBC_Mode::clear() {
660✔
27
   m_cipher->clear();
660✔
28
   reset();
660✔
29
}
660✔
30

31
void CBC_Mode::reset() {
1,976✔
32
   m_state.clear();
989✔
33
}
989✔
34

35
std::string CBC_Mode::name() const {
1,318✔
36
   if(m_padding) {
1,318✔
37
      return fmt("{}/CBC/{}", cipher().name(), padding().name());
1,126✔
38
   } else {
39
      return fmt("{}/CBC/CTS", cipher().name());
192✔
40
   }
41
}
42

43
size_t CBC_Mode::update_granularity() const {
2,308✔
44
   return cipher().block_size();
2,308✔
45
}
46

47
size_t CBC_Mode::ideal_granularity() const {
3,528✔
48
   return cipher().parallel_bytes();
3,528✔
49
}
50

51
Key_Length_Specification CBC_Mode::key_spec() const {
6,053✔
52
   return cipher().key_spec();
6,053✔
53
}
54

55
size_t CBC_Mode::default_nonce_length() const {
1,179✔
56
   return block_size();
1,179✔
57
}
58

59
bool CBC_Mode::valid_nonce_length(size_t n) const {
8,683✔
60
   return (n == 0 || n == block_size());
8,683✔
61
}
62

63
bool CBC_Mode::has_keying_material() const {
1,974✔
64
   return m_cipher->has_keying_material();
1,974✔
65
}
66

67
void CBC_Mode::key_schedule(std::span<const uint8_t> key) {
3,967✔
68
   m_cipher->set_key(key);
3,967✔
69
   m_state.clear();
3,967✔
70
}
3,967✔
71

72
void CBC_Mode::start_msg(const uint8_t nonce[], size_t nonce_len) {
8,233✔
73
   if(!valid_nonce_length(nonce_len)) {
8,233✔
74
      throw Invalid_IV_Length(name(), nonce_len);
1,316✔
75
   }
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
   if(nonce_len) {
7,575✔
83
      m_state.assign(nonce, nonce + nonce_len);
7,555✔
84
   } else if(m_state.empty()) {
20✔
85
      m_state.resize(m_cipher->block_size());
14✔
86
   }
87
   // else leave the state alone
88
}
7,575✔
89

90
size_t CBC_Encryption::minimum_final_size() const {
284✔
91
   return 0;
284✔
92
}
93

94
size_t CBC_Encryption::output_length(size_t input_length) const {
283✔
95
   if(input_length == 0) {
283✔
96
      return block_size();
4✔
97
   } else {
98
      return round_up(input_length, block_size());
279✔
99
   }
100
}
101

102
size_t CBC_Encryption::process_msg(uint8_t buf[], size_t sz) {
5,645✔
103
   BOTAN_STATE_CHECK(state().empty() == false);
5,645✔
104
   const size_t BS = block_size();
4,658✔
105

106
   BOTAN_ARG_CHECK(sz % BS == 0, "CBC input is not full blocks");
4,658✔
107
   const size_t blocks = sz / BS;
4,658✔
108

109
   if(blocks > 0) {
4,658✔
110
      xor_buf(&buf[0], state_ptr(), BS);
4,502✔
111
      cipher().encrypt(&buf[0]);
4,502✔
112

113
      for(size_t i = 1; i != blocks; ++i) {
203,095✔
114
         xor_buf(&buf[BS * i], &buf[BS * (i - 1)], BS);
198,593✔
115
         cipher().encrypt(&buf[BS * i]);
198,593✔
116
      }
117

118
      state().assign(&buf[BS * (blocks - 1)], &buf[BS * blocks]);
4,502✔
119
   }
120

121
   return sz;
4,658✔
122
}
123

124
void CBC_Encryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset) {
2,676✔
125
   BOTAN_STATE_CHECK(state().empty() == false);
2,676✔
126
   BOTAN_ARG_CHECK(buffer.size() >= offset, "Offset is out of range");
1,833✔
127

128
   const size_t BS = block_size();
1,833✔
129

130
   const size_t bytes_in_final_block = (buffer.size() - offset) % BS;
1,833✔
131

132
   padding().add_padding(buffer, bytes_in_final_block, BS);
1,833✔
133

134
   BOTAN_ASSERT_EQUAL(buffer.size() % BS, offset % BS, "Padded to block boundary");
1,833✔
135

136
   update(buffer, offset);
1,833✔
137
}
1,833✔
138

139
bool CTS_Encryption::valid_nonce_length(size_t n) const {
437✔
140
   return (n == block_size());
437✔
141
}
142

143
size_t CTS_Encryption::minimum_final_size() const {
48✔
144
   return block_size() + 1;
48✔
145
}
146

147
size_t CTS_Encryption::output_length(size_t input_length) const {
48✔
148
   return input_length;  // no ciphertext expansion in CTS
48✔
149
}
150

151
void CTS_Encryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset) {
341✔
152
   BOTAN_STATE_CHECK(state().empty() == false);
341✔
153
   BOTAN_ARG_CHECK(buffer.size() >= offset, "Offset is out of range");
197✔
154
   uint8_t* buf = buffer.data() + offset;
197✔
155
   const size_t sz = buffer.size() - offset;
197✔
156

157
   const size_t BS = block_size();
197✔
158

159
   if(sz < BS + 1) {
197✔
160
      throw Encoding_Error(name() + ": insufficient data to encrypt");
×
161
   }
162

163
   if(sz % BS == 0) {
197✔
164
      update(buffer, offset);
32✔
165

166
      // swap last two blocks
167
      for(size_t i = 0; i != BS; ++i) {
376✔
168
         std::swap(buffer[buffer.size() - BS + i], buffer[buffer.size() - 2 * BS + i]);
344✔
169
      }
170
   } else {
171
      const size_t full_blocks = ((sz / BS) - 1) * BS;
165✔
172
      const size_t final_bytes = sz - full_blocks;
165✔
173
      BOTAN_ASSERT(final_bytes > BS && final_bytes < 2 * BS, "Left over size in expected range");
165✔
174

175
      secure_vector<uint8_t> last(buf + full_blocks, buf + full_blocks + final_bytes);
165✔
176
      buffer.resize(full_blocks + offset);
165✔
177
      update(buffer, offset);
165✔
178

179
      xor_buf(last.data(), state_ptr(), BS);
165✔
180
      cipher().encrypt(last.data());
165✔
181

182
      for(size_t i = 0; i != final_bytes - BS; ++i) {
860✔
183
         last[i] ^= last[i + BS];
695✔
184
         last[i + BS] ^= last[i];
695✔
185
      }
186

187
      cipher().encrypt(last.data());
165✔
188

189
      buffer += last;
165✔
190
   }
165✔
191
}
197✔
192

193
size_t CBC_Decryption::output_length(size_t input_length) const {
331✔
194
   return input_length;  // precise for CTS, worst case otherwise
331✔
195
}
196

197
size_t CBC_Decryption::minimum_final_size() const {
285✔
198
   return block_size();
285✔
199
}
200

201
size_t CBC_Decryption::process_msg(uint8_t buf[], size_t sz) {
5,858✔
202
   BOTAN_STATE_CHECK(state().empty() == false);
5,858✔
203

204
   const size_t BS = block_size();
4,871✔
205

206
   BOTAN_ARG_CHECK(sz % BS == 0, "Input is not full blocks");
4,871✔
207
   size_t blocks = sz / BS;
4,871✔
208

209
   while(blocks) {
16,729✔
210
      const size_t to_proc = std::min(BS * blocks, m_tempbuf.size());
11,858✔
211

212
      cipher().decrypt_n(buf, m_tempbuf.data(), to_proc / BS);
11,858✔
213

214
      xor_buf(m_tempbuf.data(), state_ptr(), BS);
11,858✔
215
      xor_buf(&m_tempbuf[BS], buf, to_proc - BS);
11,858✔
216
      copy_mem(state_ptr(), buf + (to_proc - BS), BS);
11,858✔
217

218
      copy_mem(buf, m_tempbuf.data(), to_proc);
11,858✔
219

220
      buf += to_proc;
11,858✔
221
      blocks -= to_proc / BS;
11,858✔
222
   }
223

224
   return sz;
4,871✔
225
}
226

227
void CBC_Decryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset) {
2,624✔
228
   BOTAN_STATE_CHECK(state().empty() == false);
2,624✔
229
   BOTAN_ARG_CHECK(buffer.size() >= offset, "Offset is out of range");
1,781✔
230
   const size_t sz = buffer.size() - offset;
1,781✔
231

232
   const size_t BS = block_size();
1,781✔
233

234
   if(sz == 0 || sz % BS) {
1,781✔
235
      throw Decoding_Error(name() + ": Ciphertext not a multiple of block size");
×
236
   }
237

238
   update(buffer, offset);
1,781✔
239

240
   const size_t pad_bytes = BS - padding().unpad(&buffer[buffer.size() - BS], BS);
1,781✔
241
   buffer.resize(buffer.size() - pad_bytes);  // remove padding
1,781✔
242
   if(pad_bytes == 0 && padding().name() != "NoPadding") {
1,867✔
243
      throw Decoding_Error("Invalid CBC padding");
×
244
   }
245
}
1,781✔
246

247
void CBC_Decryption::reset() {
987✔
248
   CBC_Mode::reset();
987✔
249
   zeroise(m_tempbuf);
987✔
250
}
987✔
251

252
bool CTS_Decryption::valid_nonce_length(size_t n) const {
437✔
253
   return (n == block_size());
437✔
254
}
255

256
size_t CTS_Decryption::minimum_final_size() const {
48✔
257
   return block_size() + 1;
48✔
258
}
259

260
void CTS_Decryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset) {
341✔
261
   BOTAN_STATE_CHECK(state().empty() == false);
341✔
262
   BOTAN_ARG_CHECK(buffer.size() >= offset, "Offset is out of range");
197✔
263
   const size_t sz = buffer.size() - offset;
197✔
264
   uint8_t* buf = buffer.data() + offset;
197✔
265

266
   const size_t BS = block_size();
197✔
267

268
   if(sz < BS + 1) {
197✔
269
      throw Encoding_Error(name() + ": insufficient data to decrypt");
×
270
   }
271

272
   if(sz % BS == 0) {
197✔
273
      // swap last two blocks
274

275
      for(size_t i = 0; i != BS; ++i) {
376✔
276
         std::swap(buffer[buffer.size() - BS + i], buffer[buffer.size() - 2 * BS + i]);
344✔
277
      }
278

279
      update(buffer, offset);
32✔
280
   } else {
281
      const size_t full_blocks = ((sz / BS) - 1) * BS;
165✔
282
      const size_t final_bytes = sz - full_blocks;
165✔
283
      BOTAN_ASSERT(final_bytes > BS && final_bytes < 2 * BS, "Left over size in expected range");
165✔
284

285
      secure_vector<uint8_t> last(buf + full_blocks, buf + full_blocks + final_bytes);
165✔
286
      buffer.resize(full_blocks + offset);
165✔
287
      update(buffer, offset);
165✔
288

289
      cipher().decrypt(last.data());
165✔
290

291
      xor_buf(last.data(), &last[BS], final_bytes - BS);
165✔
292

293
      for(size_t i = 0; i != final_bytes - BS; ++i) {
860✔
294
         std::swap(last[i], last[i + BS]);
695✔
295
      }
296

297
      cipher().decrypt(last.data());
165✔
298
      xor_buf(last.data(), state_ptr(), BS);
165✔
299

300
      buffer += last;
165✔
301
   }
165✔
302
}
197✔
303

304
}  // namespace Botan
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc