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

randombit / botan / 23225340130

18 Mar 2026 01:53AM UTC coverage: 89.677% (-0.001%) from 89.678%
23225340130

push

github

web-flow
Merge pull request #5456 from randombit/jack/clang-tidy-22

Fix various warnings from clang-tidy 22

104438 of 116460 relevant lines covered (89.68%)

11819947.55 hits per line

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

91.81
/src/tests/test_modes.cpp
1
/*
2
* (C) 2014,2015,2017 Jack Lloyd
3
* (C) 2016 Daniel Neus, Rohde & Schwarz Cybersecurity
4
* (C) 2018 Ribose Inc
5
*
6
* Botan is released under the Simplified BSD License (see license.txt)
7
*/
8

9
#include "tests.h"
10

11
#if defined(BOTAN_HAS_CIPHER_MODES)
12
   #include <botan/cipher_mode.h>
13
   #include <botan/exceptn.h>
14
   #include <botan/hex.h>
15
   #include <botan/rng.h>
16
#endif
17

18
namespace Botan_Tests {
19

20
namespace {
21

22
#if defined(BOTAN_HAS_CIPHER_MODES)
23

24
class Cipher_Mode_Tests final : public Text_Based_Test {
×
25
   public:
26
      Cipher_Mode_Tests() : Text_Based_Test("modes", "Key,Nonce,In,Out") {}
2✔
27

28
      std::vector<std::string> possible_providers(const std::string& algo) override {
1,246✔
29
         return provider_filter(Botan::Cipher_Mode::providers(algo));
1,246✔
30
      }
31

32
      Test::Result run_one_test(const std::string& algo, const VarMap& vars) override {
1,246✔
33
         const std::vector<uint8_t> key = vars.get_req_bin("Key");
1,246✔
34
         const std::vector<uint8_t> nonce = vars.get_req_bin("Nonce");
1,246✔
35
         const std::vector<uint8_t> input = vars.get_req_bin("In");
1,246✔
36
         const std::vector<uint8_t> expected = vars.get_req_bin("Out");
1,246✔
37

38
         Test::Result result(algo);
1,246✔
39

40
         const std::vector<std::string> providers = possible_providers(algo);
1,246✔
41

42
         if(providers.empty()) {
1,246✔
43
            result.note_missing("cipher mode " + algo);
×
44
            return result;
×
45
         }
46

47
         for(auto&& provider_ask : providers) {
2,492✔
48
            auto enc = Botan::Cipher_Mode::create(algo, Botan::Cipher_Dir::Encryption, provider_ask);
1,246✔
49

50
            auto dec = Botan::Cipher_Mode::create(algo, Botan::Cipher_Dir::Decryption, provider_ask);
1,246✔
51

52
            if(!enc || !dec) {
1,246✔
53
               if(enc) {
×
54
                  result.test_failure("Provider " + provider_ask + " has encrypt but not decrypt");
×
55
               }
56
               if(dec) {
×
57
                  result.test_failure("Provider " + provider_ask + " has decrypt but not encrypt");
×
58
               }
59
               result.note_missing(algo);
×
60
               return result;
×
61
            }
62

63
            result.test_sz_eq(
1,246✔
64
               "enc and dec granularity is the same", enc->update_granularity(), dec->update_granularity());
1,246✔
65

66
            result.test_sz_gt("update granularity is non-zero", enc->update_granularity(), 0);
1,246✔
67

68
            result.test_sz_eq(
1,246✔
69
               "enc and dec ideal granularity is the same", enc->ideal_granularity(), dec->ideal_granularity());
1,246✔
70

71
            result.test_sz_gt(
1,246✔
72
               "ideal granularity is at least update granularity", enc->ideal_granularity(), enc->update_granularity());
1,246✔
73

74
            result.test_is_true("ideal granularity is a multiple of update granularity",
1,246✔
75
                                enc->ideal_granularity() % enc->update_granularity() == 0);
1,246✔
76

77
            try {
1,246✔
78
               test_mode(result, algo, provider_ask, "encryption", *enc, key, nonce, input, expected, this->rng());
2,492✔
79
            } catch(Botan::Exception& e) {
×
80
               result.test_failure("Encryption tests failed", e.what());
×
81
            }
×
82

83
            try {
1,246✔
84
               // NOLINTNEXTLINE(*-suspicious-call-argument) intentionally swapping ptext and ctext arguments here
85
               test_mode(result, algo, provider_ask, "decryption", *dec, key, nonce, expected, input, this->rng());
2,492✔
86
            } catch(Botan::Exception& e) {
×
87
               result.test_failure("Decryption tests failed", e.what());
×
88
            }
×
89
         }
2,492✔
90

91
         return result;
92
      }
6,230✔
93

94
   private:
95
      static void test_mode(Test::Result& result,
2,492✔
96
                            const std::string& algo,
97
                            const std::string& provider,
98
                            const std::string& direction,
99
                            Botan::Cipher_Mode& mode,
100
                            const std::vector<uint8_t>& key,
101
                            const std::vector<uint8_t>& nonce,
102
                            const std::vector<uint8_t>& input,
103
                            const std::vector<uint8_t>& expected,
104
                            Botan::RandomNumberGenerator& rng) {
105
         const bool is_cbc = (algo.find("/CBC") != std::string::npos);
2,492✔
106
         const bool is_ctr = (algo.find("CTR") != std::string::npos);
2,492✔
107

108
         result.test_str_eq("name", mode.name(), algo);
2,492✔
109

110
         // Some modes report base even if got from another provider
111
         if(mode.provider() != "base") {
2,492✔
112
            result.test_str_eq("provider", mode.provider(), provider);
×
113
         }
114

115
         result.test_is_false("mode not authenticated", mode.authenticated());
2,492✔
116

117
         const size_t update_granularity = mode.update_granularity();
2,492✔
118
         const size_t min_final_bytes = mode.minimum_final_size();
2,492✔
119

120
         // FFI currently requires this, so assure it is true for all modes
121
         result.test_sz_gt("buffer sizes ok", mode.ideal_granularity(), min_final_bytes);
2,492✔
122

123
         result.test_is_false("key not set", mode.has_keying_material());
2,492✔
124

125
         result.test_throws<Botan::Invalid_State>("Unkeyed object throws", [&]() {
2,492✔
126
            Botan::secure_vector<uint8_t> bad(min_final_bytes);
2,492✔
127
            mode.finish(bad);
2,492✔
128
         });
×
129

130
         if(is_cbc) {
2,492✔
131
            // can't test equal due to CBC padding
132

133
            if(direction == "encryption") {
658✔
134
               result.test_sz_lte("output_length", mode.output_length(input.size()), expected.size());
329✔
135
            } else {
136
               result.test_sz_gte("output_length", mode.output_length(input.size()), expected.size());
329✔
137
            }
138
         } else {
139
            // assume all other modes are not expanding (currently true)
140
            result.test_sz_eq("output_length", mode.output_length(input.size()), expected.size());
1,834✔
141
         }
142

143
         result.test_is_true("default nonce size is allowed", mode.valid_nonce_length(mode.default_nonce_length()));
2,492✔
144

145
         // Test that disallowed nonce sizes result in an exception
146
         static constexpr size_t large_nonce_size = 65000;
2,492✔
147
         result.test_is_false("Large nonce not allowed", mode.valid_nonce_length(large_nonce_size));
2,492✔
148
         result.test_throws<Botan::Invalid_Argument>("Large nonce causes exception",
2,492✔
149
                                                     [&mode]() { mode.start(nullptr, large_nonce_size); });
4,984✔
150

151
         Botan::secure_vector<uint8_t> garbage = rng.random_vec(update_granularity);
2,492✔
152
         Botan::secure_vector<uint8_t> ultimate_garbage = rng.random_vec(min_final_bytes);
2,492✔
153

154
         // Test to make sure reset() resets what we need it to
155
         result.test_throws<Botan::Invalid_State>("Cannot process data (update) until key is set",
2,492✔
156
                                                  [&]() { mode.update(garbage); });
4,984✔
157
         result.test_throws<Botan::Invalid_State>("Cannot process data (finish) until key is set",
2,492✔
158
                                                  [&]() { mode.finish(ultimate_garbage); });
4,984✔
159

160
         mode.set_key(mutate_vec(key, rng));
2,492✔
161

162
         if(!is_ctr) {
2,492✔
163
            result.test_throws<Botan::Invalid_State>("Cannot process data until nonce is set",
2,488✔
164
                                                     [&]() { mode.update(garbage); });
4,976✔
165
         }
166

167
         mode.start(mutate_vec(nonce, rng));
2,492✔
168
         mode.reset();
2,492✔
169

170
         if(!is_ctr) {
2,492✔
171
            result.test_throws<Botan::Invalid_State>("Cannot process data until nonce is set (after start/reset)",
2,488✔
172
                                                     [&]() { mode.update(garbage); });
4,976✔
173
         }
174

175
         mode.start(mutate_vec(nonce, rng));
2,492✔
176
         mode.update(garbage);
2,492✔
177

178
         mode.reset();
2,492✔
179

180
         mode.set_key(key);
2,492✔
181
         result.test_is_true("key is set", mode.has_keying_material());
2,492✔
182
         mode.start(nonce);
2,492✔
183

184
         Botan::secure_vector<uint8_t> buf;
2,492✔
185

186
         buf.assign(input.begin(), input.end());
2,492✔
187
         mode.finish(buf);
2,492✔
188
         result.test_bin_eq(direction + " all-in-one", buf, expected);
2,492✔
189

190
         // additionally test update() and process() if possible
191
         if(input.size() >= update_granularity + min_final_bytes) {
2,492✔
192
            const size_t max_blocks_to_process = (input.size() - min_final_bytes) / update_granularity;
2,167✔
193
            const size_t bytes_to_process = max_blocks_to_process * update_granularity;
2,167✔
194

195
            // test update, 1 block at a time
196
            if(max_blocks_to_process > 1) {
2,167✔
197
               Botan::secure_vector<uint8_t> block(update_granularity);
1,787✔
198
               buf.clear();
1,787✔
199

200
               mode.start(nonce);
1,787✔
201
               for(size_t i = 0; i != max_blocks_to_process; ++i) {
15,607✔
202
                  block.assign(input.data() + i * update_granularity, input.data() + (i + 1) * update_granularity);
13,820✔
203

204
                  mode.update(block);
13,820✔
205
                  buf += block;
13,820✔
206
               }
207

208
               Botan::secure_vector<uint8_t> last_bits(input.data() + bytes_to_process, input.data() + input.size());
1,787✔
209
               mode.finish(last_bits);
1,787✔
210
               buf += last_bits;
1,787✔
211

212
               result.test_bin_eq(direction + " update-1", buf, expected);
3,574✔
213
            }
3,574✔
214

215
            // test update with maximum length input
216
            buf.assign(input.data(), input.data() + bytes_to_process);
2,167✔
217
            Botan::secure_vector<uint8_t> last_bits(input.data() + bytes_to_process, input.data() + input.size());
2,167✔
218

219
            mode.start(nonce);
2,167✔
220
            mode.update(buf);
2,167✔
221
            mode.finish(last_bits);
2,167✔
222

223
            buf += last_bits;
2,167✔
224

225
            result.test_bin_eq(direction + " update-all", buf, expected);
2,167✔
226

227
            // test process with maximum length input
228
            mode.start(nonce);
2,167✔
229
            buf.assign(input.begin(), input.end());
2,167✔
230

231
            const size_t bytes_written = mode.process(buf.data(), bytes_to_process);
2,167✔
232

233
            result.test_sz_eq("correct number of bytes processed", bytes_written, bytes_to_process);
2,167✔
234

235
            mode.finish(buf, bytes_to_process);
2,167✔
236
            result.test_bin_eq(direction + " process", buf, expected);
4,334✔
237
         }
2,167✔
238

239
         mode.clear();
2,492✔
240
         result.test_is_false("key is not set", mode.has_keying_material());
2,492✔
241

242
         result.test_throws<Botan::Invalid_State>("Unkeyed object throws after clear", [&]() {
2,492✔
243
            Botan::secure_vector<uint8_t> bad(min_final_bytes);
2,492✔
244
            mode.finish(bad);
2,492✔
245
         });
×
246
      }
7,033✔
247
};
248

249
BOTAN_REGISTER_SMOKE_TEST("modes", "cipher_modes", Cipher_Mode_Tests);
250

251
class Cipher_Mode_IV_Carry_Tests final : public Test {
1✔
252
   public:
253
      std::vector<Test::Result> run() override {
1✔
254
         std::vector<Test::Result> results;
1✔
255
         results.push_back(test_cbc_iv_carry());
2✔
256
         results.push_back(test_cfb_iv_carry());
2✔
257
         results.push_back(test_ctr_iv_carry());
2✔
258
         return results;
1✔
259
      }
×
260

261
   private:
262
      static Test::Result test_cbc_iv_carry() {
1✔
263
         Test::Result result("CBC IV carry");
1✔
264

265
   #if defined(BOTAN_HAS_MODE_CBC) && defined(BOTAN_HAS_AES)
266
         std::unique_ptr<Botan::Cipher_Mode> enc(
1✔
267
            Botan::Cipher_Mode::create("AES-128/CBC/PKCS7", Botan::Cipher_Dir::Encryption));
1✔
268
         std::unique_ptr<Botan::Cipher_Mode> dec(
1✔
269
            Botan::Cipher_Mode::create("AES-128/CBC/PKCS7", Botan::Cipher_Dir::Decryption));
1✔
270

271
         const std::vector<uint8_t> key(16, 0xAA);
1✔
272
         const std::vector<uint8_t> iv(16, 0xAA);
1✔
273

274
         Botan::secure_vector<uint8_t> msg1 =
1✔
275
            Botan::hex_decode_locked("446F6E27742075736520706C61696E20434243206D6F6465");
1✔
276
         Botan::secure_vector<uint8_t> msg2 = Botan::hex_decode_locked("49562063617272796F766572");
1✔
277
         Botan::secure_vector<uint8_t> msg3 = Botan::hex_decode_locked("49562063617272796F76657232");
1✔
278

279
         enc->set_key(key);
1✔
280
         dec->set_key(key);
1✔
281

282
         enc->start(iv);
1✔
283
         enc->finish(msg1);
1✔
284
         result.test_bin_eq(
1✔
285
            "First ciphertext", msg1, "9BDD7300E0CB61CA71FFF957A71605DB6836159C36781246A1ADF50982757F4B");
286

287
         enc->start();
1✔
288
         enc->finish(msg2);
1✔
289

290
         result.test_bin_eq("Second ciphertext", msg2, "AA8D682958A4A044735DAC502B274DB2");
1✔
291

292
         enc->start();
1✔
293
         enc->finish(msg3);
1✔
294

295
         result.test_bin_eq("Third ciphertext", msg3, "1241B9976F73051BCF809525D6E86C25");
1✔
296

297
         dec->start(iv);
1✔
298
         dec->finish(msg1);
1✔
299

300
         dec->start();
1✔
301
         dec->finish(msg2);
1✔
302

303
         dec->start();
1✔
304
         dec->finish(msg3);
1✔
305
         result.test_bin_eq("Third plaintext", msg3, "49562063617272796F76657232");
1✔
306

307
   #endif
308
         return result;
1✔
309
      }
7✔
310

311
      static Test::Result test_cfb_iv_carry() {
1✔
312
         Test::Result result("CFB IV carry");
1✔
313
   #if defined(BOTAN_HAS_MODE_CFB) && defined(BOTAN_HAS_AES)
314
         std::unique_ptr<Botan::Cipher_Mode> enc(
1✔
315
            Botan::Cipher_Mode::create("AES-128/CFB(8)", Botan::Cipher_Dir::Encryption));
1✔
316
         std::unique_ptr<Botan::Cipher_Mode> dec(
1✔
317
            Botan::Cipher_Mode::create("AES-128/CFB(8)", Botan::Cipher_Dir::Decryption));
1✔
318

319
         const std::vector<uint8_t> key(16, 0xAA);
1✔
320
         const std::vector<uint8_t> iv(16, 0xAB);
1✔
321

322
         Botan::secure_vector<uint8_t> msg1 = Botan::hex_decode_locked("ABCDEF01234567");
1✔
323
         Botan::secure_vector<uint8_t> msg2 = Botan::hex_decode_locked("0000123456ABCDEF");
1✔
324
         Botan::secure_vector<uint8_t> msg3 = Botan::hex_decode_locked("012345");
1✔
325

326
         enc->set_key(key);
1✔
327
         dec->set_key(key);
1✔
328

329
         enc->start(iv);
1✔
330
         enc->finish(msg1);
1✔
331
         result.test_bin_eq("First ciphertext", msg1, "a51522387c4c9b");
1✔
332

333
         enc->start();
1✔
334
         enc->finish(msg2);
1✔
335

336
         result.test_bin_eq("Second ciphertext", msg2, "105457dc2e0649d4");
1✔
337

338
         enc->start();
1✔
339
         enc->finish(msg3);
1✔
340

341
         result.test_bin_eq("Third ciphertext", msg3, "53bd65");
1✔
342

343
         dec->start(iv);
1✔
344
         dec->finish(msg1);
1✔
345
         result.test_bin_eq("First plaintext", msg1, "ABCDEF01234567");
1✔
346

347
         dec->start();
1✔
348
         dec->finish(msg2);
1✔
349
         result.test_bin_eq("Second plaintext", msg2, "0000123456ABCDEF");
1✔
350

351
         dec->start();
1✔
352
         dec->finish(msg3);
1✔
353
         result.test_bin_eq("Third plaintext", msg3, "012345");
1✔
354
   #endif
355
         return result;
1✔
356
      }
7✔
357

358
      static Test::Result test_ctr_iv_carry() {
1✔
359
         Test::Result result("CTR IV carry");
1✔
360
   #if defined(BOTAN_HAS_CTR_BE) && defined(BOTAN_HAS_AES)
361

362
         std::unique_ptr<Botan::Cipher_Mode> enc(
1✔
363
            Botan::Cipher_Mode::create("AES-128/CTR-BE", Botan::Cipher_Dir::Encryption));
1✔
364
         std::unique_ptr<Botan::Cipher_Mode> dec(
1✔
365
            Botan::Cipher_Mode::create("AES-128/CTR-BE", Botan::Cipher_Dir::Decryption));
1✔
366

367
         const std::vector<uint8_t> key = Botan::hex_decode("2B7E151628AED2A6ABF7158809CF4F3C");
1✔
368
         const std::vector<uint8_t> iv = Botan::hex_decode("F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF");
1✔
369

370
         enc->set_key(key);
1✔
371
         dec->set_key(key);
1✔
372

373
         const std::vector<std::string> exp_ciphertext = {
1✔
374
            "EC",
375
            "8CDF",
376
            "739860",
377
            "7CB0F2D2",
378
            "1675EA9EA1",
379
            "E4362B7C3C67",
380
            "73516318A077D7",
381
            "FC5073AE6A2CC378",
382
            "7889374FBEB4C81B17",
383
            "BA6C44E89C399FF0F198C",
384
         };
1✔
385

386
         for(size_t i = 1; i != 10; ++i) {
10✔
387
            if(i == 1) {
9✔
388
               enc->start(iv);
1✔
389
               dec->start(iv);
1✔
390
            } else {
391
               enc->start();
8✔
392
               dec->start();
8✔
393
            }
394

395
            Botan::secure_vector<uint8_t> msg(i, 0);
9✔
396
            enc->finish(msg);
9✔
397

398
            result.test_bin_eq("Ciphertext", msg, exp_ciphertext[i - 1]);
9✔
399

400
            dec->finish(msg);
9✔
401

402
            for(const uint8_t b : msg) {
54✔
403
               result.test_u8_eq("Plaintext zeros", b, 0);
45✔
404
            }
405
         }
9✔
406
   #endif
407
         return result;
2✔
408
      }
5✔
409
};
410

411
BOTAN_REGISTER_TEST("modes", "iv_carryover", Cipher_Mode_IV_Carry_Tests);
412

413
#endif
414

415
}  // namespace
416

417
}  // namespace Botan_Tests
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