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

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

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

96.75
/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/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) :
2,836✔
19
      m_cipher(std::move(cipher)), m_padding(std::move(padding)), m_block_size(m_cipher->block_size()) {
2,836✔
20
   if(m_padding && !m_padding->valid_blocksize(m_block_size)) {
2,836✔
21
      throw Invalid_Argument(fmt("Padding {} cannot be used with {} in CBC mode", m_padding->name(), m_cipher->name()));
×
22
   }
23
}
2,836✔
24

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

30
void CBC_Mode::reset() { m_state.clear(); }
989✔
31

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

39
size_t CBC_Mode::update_granularity() const { return cipher().block_size(); }
2,308✔
40

41
size_t CBC_Mode::ideal_granularity() const { return cipher().parallel_bytes(); }
3,228✔
42

43
Key_Length_Specification CBC_Mode::key_spec() const { return cipher().key_spec(); }
3,887✔
44

45
size_t CBC_Mode::default_nonce_length() const { return block_size(); }
903✔
46

47
bool CBC_Mode::valid_nonce_length(size_t n) const { return (n == 0 || n == block_size()); }
8,060✔
48

49
bool CBC_Mode::has_keying_material() const { return m_cipher->has_keying_material(); }
1,974✔
50

51
void CBC_Mode::key_schedule(const uint8_t key[], size_t length) {
3,389✔
52
   m_cipher->set_key(key, length);
3,389✔
53
   m_state.clear();
3,389✔
54
}
3,389✔
55

56
void CBC_Mode::start_msg(const uint8_t nonce[], size_t nonce_len) {
7,610✔
57
   if(!valid_nonce_length(nonce_len))
7,610✔
58
      throw Invalid_IV_Length(name(), nonce_len);
1,316✔
59

60
   /*
61
   * A nonce of zero length means carry the last ciphertext value over
62
   * as the new IV, as unfortunately some protocols require this. If
63
   * this is the first message then we use an IV of all zeros.
64
   */
65
   if(nonce_len)
6,952✔
66
      m_state.assign(nonce, nonce + nonce_len);
6,932✔
67
   else if(m_state.empty())
20✔
68
      m_state.resize(m_cipher->block_size());
14✔
69
   // else leave the state alone
70
}
6,952✔
71

72
size_t CBC_Encryption::minimum_final_size() const { return 0; }
284✔
73

74
size_t CBC_Encryption::output_length(size_t input_length) const {
283✔
75
   if(input_length == 0)
283✔
76
      return block_size();
4✔
77
   else
78
      return round_up(input_length, block_size());
279✔
79
}
80

81
size_t CBC_Encryption::process_msg(uint8_t buf[], size_t sz) {
5,332✔
82
   BOTAN_STATE_CHECK(state().empty() == false);
5,332✔
83
   const size_t BS = block_size();
4,345✔
84

85
   BOTAN_ARG_CHECK(sz % BS == 0, "CBC input is not full blocks");
4,345✔
86
   const size_t blocks = sz / BS;
4,345✔
87

88
   if(blocks > 0) {
4,345✔
89
      xor_buf(&buf[0], state_ptr(), BS);
4,189✔
90
      cipher().encrypt(&buf[0]);
4,189✔
91

92
      for(size_t i = 1; i != blocks; ++i) {
142,986✔
93
         xor_buf(&buf[BS * i], &buf[BS * (i - 1)], BS);
138,797✔
94
         cipher().encrypt(&buf[BS * i]);
138,797✔
95
      }
96

97
      state().assign(&buf[BS * (blocks - 1)], &buf[BS * blocks]);
4,189✔
98
   }
99

100
   return sz;
4,345✔
101
}
102

103
void CBC_Encryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset) {
2,400✔
104
   BOTAN_STATE_CHECK(state().empty() == false);
2,400✔
105
   BOTAN_ARG_CHECK(buffer.size() >= offset, "Offset is out of range");
1,557✔
106

107
   const size_t BS = block_size();
1,557✔
108

109
   const size_t bytes_in_final_block = (buffer.size() - offset) % BS;
1,557✔
110

111
   padding().add_padding(buffer, bytes_in_final_block, BS);
1,557✔
112

113
   BOTAN_ASSERT_EQUAL(buffer.size() % BS, offset % BS, "Padded to block boundary");
1,557✔
114

115
   update(buffer, offset);
1,557✔
116
}
1,557✔
117

118
bool CTS_Encryption::valid_nonce_length(size_t n) const { return (n == block_size()); }
437✔
119

120
size_t CTS_Encryption::minimum_final_size() const { return block_size() + 1; }
48✔
121

122
size_t CTS_Encryption::output_length(size_t input_length) const {
48✔
123
   return input_length;  // no ciphertext expansion in CTS
48✔
124
}
125

126
void CTS_Encryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset) {
341✔
127
   BOTAN_STATE_CHECK(state().empty() == false);
341✔
128
   BOTAN_ARG_CHECK(buffer.size() >= offset, "Offset is out of range");
197✔
129
   uint8_t* buf = buffer.data() + offset;
197✔
130
   const size_t sz = buffer.size() - offset;
197✔
131

132
   const size_t BS = block_size();
197✔
133

134
   if(sz < BS + 1)
197✔
135
      throw Encoding_Error(name() + ": insufficient data to encrypt");
×
136

137
   if(sz % BS == 0) {
197✔
138
      update(buffer, offset);
32✔
139

140
      // swap last two blocks
141
      for(size_t i = 0; i != BS; ++i)
376✔
142
         std::swap(buffer[buffer.size() - BS + i], buffer[buffer.size() - 2 * BS + i]);
344✔
143
   } else {
144
      const size_t full_blocks = ((sz / BS) - 1) * BS;
165✔
145
      const size_t final_bytes = sz - full_blocks;
165✔
146
      BOTAN_ASSERT(final_bytes > BS && final_bytes < 2 * BS, "Left over size in expected range");
165✔
147

148
      secure_vector<uint8_t> last(buf + full_blocks, buf + full_blocks + final_bytes);
165✔
149
      buffer.resize(full_blocks + offset);
165✔
150
      update(buffer, offset);
165✔
151

152
      xor_buf(last.data(), state_ptr(), BS);
165✔
153
      cipher().encrypt(last.data());
165✔
154

155
      for(size_t i = 0; i != final_bytes - BS; ++i) {
860✔
156
         last[i] ^= last[i + BS];
695✔
157
         last[i + BS] ^= last[i];
695✔
158
      }
159

160
      cipher().encrypt(last.data());
165✔
161

162
      buffer += last;
165✔
163
   }
165✔
164
}
197✔
165

166
size_t CBC_Decryption::output_length(size_t input_length) const {
331✔
167
   return input_length;  // precise for CTS, worst case otherwise
331✔
168
}
169

170
size_t CBC_Decryption::minimum_final_size() const { return block_size(); }
285✔
171

172
size_t CBC_Decryption::process_msg(uint8_t buf[], size_t sz) {
5,552✔
173
   BOTAN_STATE_CHECK(state().empty() == false);
5,552✔
174

175
   const size_t BS = block_size();
4,565✔
176

177
   BOTAN_ARG_CHECK(sz % BS == 0, "Input is not full blocks");
4,565✔
178
   size_t blocks = sz / BS;
4,565✔
179

180
   while(blocks) {
16,723✔
181
      const size_t to_proc = std::min(BS * blocks, m_tempbuf.size());
12,158✔
182

183
      cipher().decrypt_n(buf, m_tempbuf.data(), to_proc / BS);
12,158✔
184

185
      xor_buf(m_tempbuf.data(), state_ptr(), BS);
12,158✔
186
      xor_buf(&m_tempbuf[BS], buf, to_proc - BS);
12,158✔
187
      copy_mem(state_ptr(), buf + (to_proc - BS), BS);
12,158✔
188

189
      copy_mem(buf, m_tempbuf.data(), to_proc);
12,158✔
190

191
      buf += to_proc;
12,158✔
192
      blocks -= to_proc / BS;
12,158✔
193
   }
194

195
   return sz;
4,565✔
196
}
197

198
void CBC_Decryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset) {
2,348✔
199
   BOTAN_STATE_CHECK(state().empty() == false);
2,348✔
200
   BOTAN_ARG_CHECK(buffer.size() >= offset, "Offset is out of range");
1,505✔
201
   const size_t sz = buffer.size() - offset;
1,505✔
202

203
   const size_t BS = block_size();
1,505✔
204

205
   if(sz == 0 || sz % BS)
1,505✔
206
      throw Decoding_Error(name() + ": Ciphertext not a multiple of block size");
×
207

208
   update(buffer, offset);
1,505✔
209

210
   const size_t pad_bytes = BS - padding().unpad(&buffer[buffer.size() - BS], BS);
1,505✔
211
   buffer.resize(buffer.size() - pad_bytes);  // remove padding
1,505✔
212
   if(pad_bytes == 0 && padding().name() != "NoPadding") {
1,591✔
213
      throw Decoding_Error("Invalid CBC padding");
×
214
   }
215
}
1,505✔
216

217
void CBC_Decryption::reset() {
987✔
218
   CBC_Mode::reset();
987✔
219
   zeroise(m_tempbuf);
987✔
220
}
987✔
221

222
bool CTS_Decryption::valid_nonce_length(size_t n) const { return (n == block_size()); }
437✔
223

224
size_t CTS_Decryption::minimum_final_size() const { return block_size() + 1; }
48✔
225

226
void CTS_Decryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset) {
341✔
227
   BOTAN_STATE_CHECK(state().empty() == false);
341✔
228
   BOTAN_ARG_CHECK(buffer.size() >= offset, "Offset is out of range");
197✔
229
   const size_t sz = buffer.size() - offset;
197✔
230
   uint8_t* buf = buffer.data() + offset;
197✔
231

232
   const size_t BS = block_size();
197✔
233

234
   if(sz < BS + 1)
197✔
235
      throw Encoding_Error(name() + ": insufficient data to decrypt");
×
236

237
   if(sz % BS == 0) {
197✔
238
      // swap last two blocks
239

240
      for(size_t i = 0; i != BS; ++i)
376✔
241
         std::swap(buffer[buffer.size() - BS + i], buffer[buffer.size() - 2 * BS + i]);
344✔
242

243
      update(buffer, offset);
32✔
244
   } else {
245
      const size_t full_blocks = ((sz / BS) - 1) * BS;
165✔
246
      const size_t final_bytes = sz - full_blocks;
165✔
247
      BOTAN_ASSERT(final_bytes > BS && final_bytes < 2 * BS, "Left over size in expected range");
165✔
248

249
      secure_vector<uint8_t> last(buf + full_blocks, buf + full_blocks + final_bytes);
165✔
250
      buffer.resize(full_blocks + offset);
165✔
251
      update(buffer, offset);
165✔
252

253
      cipher().decrypt(last.data());
165✔
254

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

257
      for(size_t i = 0; i != final_bytes - BS; ++i)
860✔
258
         std::swap(last[i], last[i + BS]);
695✔
259

260
      cipher().decrypt(last.data());
165✔
261
      xor_buf(last.data(), state_ptr(), BS);
165✔
262

263
      buffer += last;
165✔
264
   }
165✔
265
}
197✔
266

267
}
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