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

randombit / botan / 23982215831

04 Apr 2026 03:48PM UTC coverage: 89.459% (+0.005%) from 89.454%
23982215831

Pull #5521

github

web-flow
Merge 68cd8aa36 into 417709dd7
Pull Request #5521: Rollup of small fixes

105723 of 118181 relevant lines covered (89.46%)

11537843.46 hits per line

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

92.69
/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
         // Test finish() with non-zero offset
191
         {
2,492✔
192
            const size_t test_offset = 1 + rng.next_byte() % 32;
2,492✔
193
            buf.assign(test_offset, 0xAB);
2,492✔
194
            buf.insert(buf.end(), input.begin(), input.end());
2,492✔
195

196
            mode.start(nonce);
2,492✔
197
            mode.finish(buf, test_offset);
2,492✔
198

199
            for(size_t i = 0; i < test_offset; ++i) {
43,761✔
200
               result.test_u8_eq(direction + " prefix byte", buf[i], 0xAB);
82,538✔
201
            }
202
            result.test_bin_eq(direction + " finish with offset", std::span{buf}.subspan(test_offset), expected);
2,492✔
203
         }
204

205
         // Test update() + finish() with non-zero offset
206
         if(input.size() >= update_granularity + min_final_bytes) {
2,492✔
207
            const size_t test_offset = 1 + rng.next_byte() % 32;
2,167✔
208
            const size_t max_blocks = (input.size() - min_final_bytes) / update_granularity;
2,167✔
209
            const size_t bytes_to_update = max_blocks * update_granularity;
2,167✔
210

211
            buf.assign(test_offset, 0xAB);
2,167✔
212
            buf.insert(buf.end(), input.begin(), input.begin() + bytes_to_update);
2,167✔
213

214
            Botan::secure_vector<uint8_t> final_buf(test_offset, 0xAB);
2,167✔
215
            final_buf.insert(final_buf.end(), input.begin() + bytes_to_update, input.end());
2,167✔
216

217
            mode.start(nonce);
2,167✔
218
            mode.update(buf, test_offset);
2,167✔
219
            mode.finish(final_buf, test_offset);
2,167✔
220

221
            for(size_t i = 0; i < test_offset; ++i) {
37,379✔
222
               result.test_u8_eq(direction + " update offset prefix byte", buf[i], 0xAB);
35,212✔
223
               result.test_u8_eq(direction + " finish offset prefix byte", final_buf[i], 0xAB);
70,424✔
224
            }
225

226
            Botan::secure_vector<uint8_t> combined;
2,167✔
227
            combined.insert(combined.end(), buf.begin() + test_offset, buf.end());
2,167✔
228
            combined.insert(combined.end(), final_buf.begin() + test_offset, final_buf.end());
2,167✔
229
            result.test_bin_eq(direction + " update+finish with offset", combined, expected);
4,334✔
230
         }
4,334✔
231

232
         // additionally test update() and process() if possible
233
         if(input.size() >= update_granularity + min_final_bytes) {
2,492✔
234
            const size_t max_blocks_to_process = (input.size() - min_final_bytes) / update_granularity;
2,167✔
235
            const size_t bytes_to_process = max_blocks_to_process * update_granularity;
2,167✔
236

237
            // test update, 1 block at a time
238
            if(max_blocks_to_process > 1) {
2,167✔
239
               Botan::secure_vector<uint8_t> block(update_granularity);
1,787✔
240
               buf.clear();
1,787✔
241

242
               mode.start(nonce);
1,787✔
243
               for(size_t i = 0; i != max_blocks_to_process; ++i) {
15,607✔
244
                  block.assign(input.data() + i * update_granularity, input.data() + (i + 1) * update_granularity);
13,820✔
245

246
                  mode.update(block);
13,820✔
247
                  buf += block;
13,820✔
248
               }
249

250
               Botan::secure_vector<uint8_t> last_bits(input.data() + bytes_to_process, input.data() + input.size());
1,787✔
251
               mode.finish(last_bits);
1,787✔
252
               buf += last_bits;
1,787✔
253

254
               result.test_bin_eq(direction + " update-1", buf, expected);
3,574✔
255
            }
3,574✔
256

257
            // test update with maximum length input
258
            buf.assign(input.data(), input.data() + bytes_to_process);
2,167✔
259
            Botan::secure_vector<uint8_t> last_bits(input.data() + bytes_to_process, input.data() + input.size());
2,167✔
260

261
            mode.start(nonce);
2,167✔
262
            mode.update(buf);
2,167✔
263
            mode.finish(last_bits);
2,167✔
264

265
            buf += last_bits;
2,167✔
266

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

269
            // test process with maximum length input
270
            mode.start(nonce);
2,167✔
271
            buf.assign(input.begin(), input.end());
2,167✔
272

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

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

277
            mode.finish(buf, bytes_to_process);
2,167✔
278
            result.test_bin_eq(direction + " process", buf, expected);
4,334✔
279
         }
2,167✔
280

281
         mode.clear();
2,492✔
282
         result.test_is_false("key is not set", mode.has_keying_material());
2,492✔
283

284
         result.test_throws<Botan::Invalid_State>("Unkeyed object throws after clear", [&]() {
2,492✔
285
            Botan::secure_vector<uint8_t> bad(min_final_bytes);
2,492✔
286
            mode.finish(bad);
2,492✔
287
         });
×
288
      }
9,525✔
289
};
290

291
BOTAN_REGISTER_SMOKE_TEST("modes", "cipher_modes", Cipher_Mode_Tests);
292

293
class Cipher_Mode_IV_Carry_Tests final : public Test {
1✔
294
   public:
295
      std::vector<Test::Result> run() override {
1✔
296
         std::vector<Test::Result> results;
1✔
297
         results.push_back(test_cbc_iv_carry());
2✔
298
         results.push_back(test_cfb_iv_carry());
2✔
299
         results.push_back(test_ctr_iv_carry());
2✔
300
         return results;
1✔
301
      }
×
302

303
   private:
304
      static Test::Result test_cbc_iv_carry() {
1✔
305
         Test::Result result("CBC IV carry");
1✔
306

307
   #if defined(BOTAN_HAS_MODE_CBC) && defined(BOTAN_HAS_AES)
308
         std::unique_ptr<Botan::Cipher_Mode> enc(
1✔
309
            Botan::Cipher_Mode::create("AES-128/CBC/PKCS7", Botan::Cipher_Dir::Encryption));
1✔
310
         std::unique_ptr<Botan::Cipher_Mode> dec(
1✔
311
            Botan::Cipher_Mode::create("AES-128/CBC/PKCS7", Botan::Cipher_Dir::Decryption));
1✔
312

313
         const std::vector<uint8_t> key(16, 0xAA);
1✔
314
         const std::vector<uint8_t> iv(16, 0xAA);
1✔
315

316
         Botan::secure_vector<uint8_t> msg1 =
1✔
317
            Botan::hex_decode_locked("446F6E27742075736520706C61696E20434243206D6F6465");
1✔
318
         Botan::secure_vector<uint8_t> msg2 = Botan::hex_decode_locked("49562063617272796F766572");
1✔
319
         Botan::secure_vector<uint8_t> msg3 = Botan::hex_decode_locked("49562063617272796F76657232");
1✔
320

321
         enc->set_key(key);
1✔
322
         dec->set_key(key);
1✔
323

324
         enc->start(iv);
1✔
325
         enc->finish(msg1);
1✔
326
         result.test_bin_eq(
1✔
327
            "First ciphertext", msg1, "9BDD7300E0CB61CA71FFF957A71605DB6836159C36781246A1ADF50982757F4B");
328

329
         enc->start();
1✔
330
         enc->finish(msg2);
1✔
331

332
         result.test_bin_eq("Second ciphertext", msg2, "AA8D682958A4A044735DAC502B274DB2");
1✔
333

334
         enc->start();
1✔
335
         enc->finish(msg3);
1✔
336

337
         result.test_bin_eq("Third ciphertext", msg3, "1241B9976F73051BCF809525D6E86C25");
1✔
338

339
         dec->start(iv);
1✔
340
         dec->finish(msg1);
1✔
341

342
         dec->start();
1✔
343
         dec->finish(msg2);
1✔
344

345
         dec->start();
1✔
346
         dec->finish(msg3);
1✔
347
         result.test_bin_eq("Third plaintext", msg3, "49562063617272796F76657232");
1✔
348

349
   #endif
350
         return result;
1✔
351
      }
7✔
352

353
      static Test::Result test_cfb_iv_carry() {
1✔
354
         Test::Result result("CFB IV carry");
1✔
355
   #if defined(BOTAN_HAS_MODE_CFB) && defined(BOTAN_HAS_AES)
356
         std::unique_ptr<Botan::Cipher_Mode> enc(
1✔
357
            Botan::Cipher_Mode::create("AES-128/CFB(8)", Botan::Cipher_Dir::Encryption));
1✔
358
         std::unique_ptr<Botan::Cipher_Mode> dec(
1✔
359
            Botan::Cipher_Mode::create("AES-128/CFB(8)", Botan::Cipher_Dir::Decryption));
1✔
360

361
         const std::vector<uint8_t> key(16, 0xAA);
1✔
362
         const std::vector<uint8_t> iv(16, 0xAB);
1✔
363

364
         Botan::secure_vector<uint8_t> msg1 = Botan::hex_decode_locked("ABCDEF01234567");
1✔
365
         Botan::secure_vector<uint8_t> msg2 = Botan::hex_decode_locked("0000123456ABCDEF");
1✔
366
         Botan::secure_vector<uint8_t> msg3 = Botan::hex_decode_locked("012345");
1✔
367

368
         enc->set_key(key);
1✔
369
         dec->set_key(key);
1✔
370

371
         enc->start(iv);
1✔
372
         enc->finish(msg1);
1✔
373
         result.test_bin_eq("First ciphertext", msg1, "a51522387c4c9b");
1✔
374

375
         enc->start();
1✔
376
         enc->finish(msg2);
1✔
377

378
         result.test_bin_eq("Second ciphertext", msg2, "105457dc2e0649d4");
1✔
379

380
         enc->start();
1✔
381
         enc->finish(msg3);
1✔
382

383
         result.test_bin_eq("Third ciphertext", msg3, "53bd65");
1✔
384

385
         dec->start(iv);
1✔
386
         dec->finish(msg1);
1✔
387
         result.test_bin_eq("First plaintext", msg1, "ABCDEF01234567");
1✔
388

389
         dec->start();
1✔
390
         dec->finish(msg2);
1✔
391
         result.test_bin_eq("Second plaintext", msg2, "0000123456ABCDEF");
1✔
392

393
         dec->start();
1✔
394
         dec->finish(msg3);
1✔
395
         result.test_bin_eq("Third plaintext", msg3, "012345");
1✔
396
   #endif
397
         return result;
1✔
398
      }
7✔
399

400
      static Test::Result test_ctr_iv_carry() {
1✔
401
         Test::Result result("CTR IV carry");
1✔
402
   #if defined(BOTAN_HAS_CTR_BE) && defined(BOTAN_HAS_AES)
403

404
         std::unique_ptr<Botan::Cipher_Mode> enc(
1✔
405
            Botan::Cipher_Mode::create("AES-128/CTR-BE", Botan::Cipher_Dir::Encryption));
1✔
406
         std::unique_ptr<Botan::Cipher_Mode> dec(
1✔
407
            Botan::Cipher_Mode::create("AES-128/CTR-BE", Botan::Cipher_Dir::Decryption));
1✔
408

409
         const std::vector<uint8_t> key = Botan::hex_decode("2B7E151628AED2A6ABF7158809CF4F3C");
1✔
410
         const std::vector<uint8_t> iv = Botan::hex_decode("F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF");
1✔
411

412
         enc->set_key(key);
1✔
413
         dec->set_key(key);
1✔
414

415
         const std::vector<std::string> exp_ciphertext = {
1✔
416
            "EC",
417
            "8CDF",
418
            "739860",
419
            "7CB0F2D2",
420
            "1675EA9EA1",
421
            "E4362B7C3C67",
422
            "73516318A077D7",
423
            "FC5073AE6A2CC378",
424
            "7889374FBEB4C81B17",
425
            "BA6C44E89C399FF0F198C",
426
         };
1✔
427

428
         for(size_t i = 1; i != 10; ++i) {
10✔
429
            if(i == 1) {
9✔
430
               enc->start(iv);
1✔
431
               dec->start(iv);
1✔
432
            } else {
433
               enc->start();
8✔
434
               dec->start();
8✔
435
            }
436

437
            Botan::secure_vector<uint8_t> msg(i, 0);
9✔
438
            enc->finish(msg);
9✔
439

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

442
            dec->finish(msg);
9✔
443

444
            for(const uint8_t b : msg) {
54✔
445
               result.test_u8_eq("Plaintext zeros", b, 0);
45✔
446
            }
447
         }
9✔
448
   #endif
449
         return result;
2✔
450
      }
5✔
451
};
452

453
BOTAN_REGISTER_TEST("modes", "iv_carryover", Cipher_Mode_IV_Carry_Tests);
454

455
#endif
456

457
}  // namespace
458

459
}  // 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