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

randombit / botan / 22020273955

14 Feb 2026 04:02PM UTC coverage: 90.059% (-0.005%) from 90.064%
22020273955

push

github

web-flow
Merge pull request #5328 from randombit/jack/test-predicates

Change Test::Result integer and bool predicates to be specifically named

102238 of 113523 relevant lines covered (90.06%)

11567860.88 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
#if defined(BOTAN_HAS_CIPHER_MODES)
21

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

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

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

36
         Test::Result result(algo);
1,246✔
37

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

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

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

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

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

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

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

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

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

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

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

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

89
         return result;
90
      }
6,230✔
91

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

106
         result.test_eq("name", mode.name(), algo);
2,492✔
107

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

113
         result.test_is_false("mode not authenticated", mode.authenticated());
2,492✔
114

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

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

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

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

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

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

141
         result.confirm("default nonce size is allowed", mode.valid_nonce_length(mode.default_nonce_length()));
2,492✔
142

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

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

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

158
         mode.set_key(mutate_vec(key, rng));
2,492✔
159

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

165
         mode.start(mutate_vec(nonce, rng));
2,492✔
166
         mode.reset();
2,492✔
167

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

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

176
         mode.reset();
2,492✔
177

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

182
         Botan::secure_vector<uint8_t> buf;
2,492✔
183

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

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

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

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

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

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

210
               result.test_eq(direction + " update-1", buf, expected);
1,787✔
211
            }
3,574✔
212

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

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

221
            buf += last_bits;
2,167✔
222

223
            result.test_eq(direction + " update-all", buf, expected);
2,167✔
224

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

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

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

233
            mode.finish(buf, bytes_to_process);
2,167✔
234
            result.test_eq(direction + " process", buf, expected);
2,167✔
235
         }
2,167✔
236

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

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

247
BOTAN_REGISTER_SMOKE_TEST("modes", "cipher_modes", Cipher_Mode_Tests);
248

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

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

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

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

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

277
         enc->set_key(key);
1✔
278
         dec->set_key(key);
1✔
279

280
         enc->start(iv);
1✔
281
         enc->finish(msg1);
1✔
282
         result.test_eq("First ciphertext", msg1, "9BDD7300E0CB61CA71FFF957A71605DB6836159C36781246A1ADF50982757F4B");
1✔
283

284
         enc->start();
1✔
285
         enc->finish(msg2);
1✔
286

287
         result.test_eq("Second ciphertext", msg2, "AA8D682958A4A044735DAC502B274DB2");
1✔
288

289
         enc->start();
1✔
290
         enc->finish(msg3);
1✔
291

292
         result.test_eq("Third ciphertext", msg3, "1241B9976F73051BCF809525D6E86C25");
1✔
293

294
         dec->start(iv);
1✔
295
         dec->finish(msg1);
1✔
296

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

300
         dec->start();
1✔
301
         dec->finish(msg3);
1✔
302
         result.test_eq("Third plaintext", msg3, "49562063617272796F76657232");
1✔
303

304
   #endif
305
         return result;
1✔
306
      }
7✔
307

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

316
         const std::vector<uint8_t> key(16, 0xAA);
1✔
317
         const std::vector<uint8_t> iv(16, 0xAB);
1✔
318

319
         Botan::secure_vector<uint8_t> msg1 = Botan::hex_decode_locked("ABCDEF01234567");
1✔
320
         Botan::secure_vector<uint8_t> msg2 = Botan::hex_decode_locked("0000123456ABCDEF");
1✔
321
         Botan::secure_vector<uint8_t> msg3 = Botan::hex_decode_locked("012345");
1✔
322

323
         enc->set_key(key);
1✔
324
         dec->set_key(key);
1✔
325

326
         enc->start(iv);
1✔
327
         enc->finish(msg1);
1✔
328
         result.test_eq("First ciphertext", msg1, "a51522387c4c9b");
1✔
329

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

333
         result.test_eq("Second ciphertext", msg2, "105457dc2e0649d4");
1✔
334

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

338
         result.test_eq("Third ciphertext", msg3, "53bd65");
1✔
339

340
         dec->start(iv);
1✔
341
         dec->finish(msg1);
1✔
342
         result.test_eq("First plaintext", msg1, "ABCDEF01234567");
1✔
343

344
         dec->start();
1✔
345
         dec->finish(msg2);
1✔
346
         result.test_eq("Second plaintext", msg2, "0000123456ABCDEF");
1✔
347

348
         dec->start();
1✔
349
         dec->finish(msg3);
1✔
350
         result.test_eq("Third plaintext", msg3, "012345");
1✔
351
   #endif
352
         return result;
1✔
353
      }
7✔
354

355
      static Test::Result test_ctr_iv_carry() {
1✔
356
         Test::Result result("CTR IV carry");
1✔
357
   #if defined(BOTAN_HAS_CTR_BE) && defined(BOTAN_HAS_AES)
358

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

364
         const std::vector<uint8_t> key = Botan::hex_decode("2B7E151628AED2A6ABF7158809CF4F3C");
1✔
365
         const std::vector<uint8_t> iv = Botan::hex_decode("F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF");
1✔
366

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

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

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

392
            Botan::secure_vector<uint8_t> msg(i, 0);
9✔
393
            enc->finish(msg);
9✔
394

395
            result.test_eq("Ciphertext", msg, exp_ciphertext[i - 1].c_str());
9✔
396

397
            dec->finish(msg);
9✔
398

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

408
BOTAN_REGISTER_TEST("modes", "iv_carryover", Cipher_Mode_IV_Carry_Tests);
409

410
#endif
411

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