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

randombit / botan / 20579846577

29 Dec 2025 06:24PM UTC coverage: 90.415% (+0.2%) from 90.243%
20579846577

push

github

web-flow
Merge pull request #5167 from randombit/jack/src-size-reductions

Changes to reduce unnecessary inclusions

101523 of 112285 relevant lines covered (90.42%)

12817276.56 hits per line

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

91.77
/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/rng.h>
14
#endif
15

16
namespace Botan_Tests {
17

18
#if defined(BOTAN_HAS_CIPHER_MODES)
19

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

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

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

34
         Test::Result result(algo);
2,490✔
35

36
         const std::vector<std::string> providers = possible_providers(algo);
1,245✔
37

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

43
         for(auto&& provider_ask : providers) {
2,490✔
44
            auto enc = Botan::Cipher_Mode::create(algo, Botan::Cipher_Dir::Encryption, provider_ask);
1,245✔
45

46
            auto dec = Botan::Cipher_Mode::create(algo, Botan::Cipher_Dir::Decryption, provider_ask);
1,245✔
47

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

59
            result.test_eq("enc and dec granularity is the same", enc->update_granularity(), dec->update_granularity());
1,245✔
60

61
            result.test_gt("update granularity is non-zero", enc->update_granularity(), 0);
1,245✔
62

63
            result.test_eq(
1,245✔
64
               "enc and dec ideal granularity is the same", enc->ideal_granularity(), dec->ideal_granularity());
1,245✔
65

66
            result.test_gt(
1,245✔
67
               "ideal granularity is at least update granularity", enc->ideal_granularity(), enc->update_granularity());
1,245✔
68

69
            result.confirm("ideal granularity is a multiple of update granularity",
2,490✔
70
                           enc->ideal_granularity() % enc->update_granularity() == 0);
1,245✔
71

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

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

86
         return result;
87
      }
6,225✔
88

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

103
         result.test_eq("name", mode.name(), algo);
4,980✔
104

105
         // Some modes report base even if got from another provider
106
         if(mode.provider() != "base") {
2,490✔
107
            result.test_eq("provider", mode.provider(), provider);
×
108
         }
109

110
         result.test_eq("mode not authenticated", mode.authenticated(), false);
2,490✔
111

112
         const size_t update_granularity = mode.update_granularity();
2,490✔
113
         const size_t min_final_bytes = mode.minimum_final_size();
2,490✔
114

115
         // FFI currently requires this, so assure it is true for all modes
116
         result.test_gt("buffer sizes ok", mode.ideal_granularity(), min_final_bytes);
2,490✔
117

118
         result.test_eq("key not set", mode.has_keying_material(), false);
2,490✔
119

120
         result.test_throws<Botan::Invalid_State>("Unkeyed object throws", [&]() {
4,980✔
121
            Botan::secure_vector<uint8_t> bad(min_final_bytes);
2,490✔
122
            mode.finish(bad);
2,490✔
123
         });
×
124

125
         if(is_cbc) {
2,490✔
126
            // can't test equal due to CBC padding
127

128
            if(direction == "encryption") {
658✔
129
               result.test_lte("output_length", mode.output_length(input.size()), expected.size());
658✔
130
            } else {
131
               result.test_gte("output_length", mode.output_length(input.size()), expected.size());
658✔
132
            }
133
         } else {
134
            // assume all other modes are not expanding (currently true)
135
            result.test_eq("output_length", mode.output_length(input.size()), expected.size());
3,664✔
136
         }
137

138
         result.confirm("default nonce size is allowed", mode.valid_nonce_length(mode.default_nonce_length()));
4,980✔
139

140
         // Test that disallowed nonce sizes result in an exception
141
         static constexpr size_t large_nonce_size = 65000;
2,490✔
142
         result.test_eq("Large nonce not allowed", mode.valid_nonce_length(large_nonce_size), false);
2,490✔
143
         result.test_throws<Botan::Invalid_Argument>("Large nonce causes exception",
4,980✔
144
                                                     [&mode]() { mode.start(nullptr, large_nonce_size); });
4,980✔
145

146
         Botan::secure_vector<uint8_t> garbage = rng.random_vec(update_granularity);
2,490✔
147
         Botan::secure_vector<uint8_t> ultimate_garbage = rng.random_vec(min_final_bytes);
2,490✔
148

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

155
         mode.set_key(mutate_vec(key, rng));
2,490✔
156

157
         if(!is_ctr) {
2,490✔
158
            result.test_throws<Botan::Invalid_State>("Cannot process data until nonce is set",
7,458✔
159
                                                     [&]() { mode.update(garbage); });
4,972✔
160
         }
161

162
         mode.start(mutate_vec(nonce, rng));
2,490✔
163
         mode.reset();
2,490✔
164

165
         if(!is_ctr) {
2,490✔
166
            result.test_throws<Botan::Invalid_State>("Cannot process data until nonce is set (after start/reset)",
7,458✔
167
                                                     [&]() { mode.update(garbage); });
4,972✔
168
         }
169

170
         mode.start(mutate_vec(nonce, rng));
2,490✔
171
         mode.update(garbage);
2,490✔
172

173
         mode.reset();
2,490✔
174

175
         mode.set_key(key);
2,490✔
176
         result.test_eq("key is set", mode.has_keying_material(), true);
2,490✔
177
         mode.start(nonce);
2,490✔
178

179
         Botan::secure_vector<uint8_t> buf;
2,490✔
180

181
         buf.assign(input.begin(), input.end());
2,490✔
182
         mode.finish(buf);
2,490✔
183
         result.test_eq(direction + " all-in-one", buf, expected);
4,980✔
184

185
         // additionally test update() and process() if possible
186
         if(input.size() >= update_granularity + min_final_bytes) {
2,490✔
187
            const size_t max_blocks_to_process = (input.size() - min_final_bytes) / update_granularity;
2,165✔
188
            const size_t bytes_to_process = max_blocks_to_process * update_granularity;
2,165✔
189

190
            // test update, 1 block at a time
191
            if(max_blocks_to_process > 1) {
2,165✔
192
               Botan::secure_vector<uint8_t> block(update_granularity);
1,785✔
193
               buf.clear();
1,785✔
194

195
               mode.start(nonce);
1,785✔
196
               for(size_t i = 0; i != max_blocks_to_process; ++i) {
15,425✔
197
                  block.assign(input.data() + i * update_granularity, input.data() + (i + 1) * update_granularity);
13,640✔
198

199
                  mode.update(block);
13,640✔
200
                  buf += block;
13,640✔
201
               }
202

203
               Botan::secure_vector<uint8_t> last_bits(input.data() + bytes_to_process, input.data() + input.size());
1,785✔
204
               mode.finish(last_bits);
1,785✔
205
               buf += last_bits;
1,785✔
206

207
               result.test_eq(direction + " update-1", buf, expected);
3,570✔
208
            }
3,570✔
209

210
            // test update with maximum length input
211
            buf.assign(input.data(), input.data() + bytes_to_process);
2,165✔
212
            Botan::secure_vector<uint8_t> last_bits(input.data() + bytes_to_process, input.data() + input.size());
2,165✔
213

214
            mode.start(nonce);
2,165✔
215
            mode.update(buf);
2,165✔
216
            mode.finish(last_bits);
2,165✔
217

218
            buf += last_bits;
2,165✔
219

220
            result.test_eq(direction + " update-all", buf, expected);
4,330✔
221

222
            // test process with maximum length input
223
            mode.start(nonce);
2,165✔
224
            buf.assign(input.begin(), input.end());
2,165✔
225

226
            const size_t bytes_written = mode.process(buf.data(), bytes_to_process);
2,165✔
227

228
            result.test_eq("correct number of bytes processed", bytes_written, bytes_to_process);
2,165✔
229

230
            mode.finish(buf, bytes_to_process);
2,165✔
231
            result.test_eq(direction + " process", buf, expected);
4,330✔
232
         }
2,165✔
233

234
         mode.clear();
2,490✔
235
         result.test_eq("key is not set", mode.has_keying_material(), false);
2,490✔
236

237
         result.test_throws<Botan::Invalid_State>("Unkeyed object throws after clear", [&]() {
7,470✔
238
            Botan::secure_vector<uint8_t> bad(min_final_bytes);
2,490✔
239
            mode.finish(bad);
2,490✔
240
         });
×
241
      }
7,027✔
242
};
243

244
BOTAN_REGISTER_SMOKE_TEST("modes", "cipher_modes", Cipher_Mode_Tests);
245

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

256
   private:
257
      static Test::Result test_cbc_iv_carry() {
1✔
258
         Test::Result result("CBC IV carry");
1✔
259

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

266
         const std::vector<uint8_t> key(16, 0xAA);
1✔
267
         const std::vector<uint8_t> iv(16, 0xAA);
1✔
268

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

274
         enc->set_key(key);
1✔
275
         dec->set_key(key);
1✔
276

277
         enc->start(iv);
1✔
278
         enc->finish(msg1);
1✔
279
         result.test_eq("First ciphertext", msg1, "9BDD7300E0CB61CA71FFF957A71605DB6836159C36781246A1ADF50982757F4B");
1✔
280

281
         enc->start();
1✔
282
         enc->finish(msg2);
1✔
283

284
         result.test_eq("Second ciphertext", msg2, "AA8D682958A4A044735DAC502B274DB2");
1✔
285

286
         enc->start();
1✔
287
         enc->finish(msg3);
1✔
288

289
         result.test_eq("Third ciphertext", msg3, "1241B9976F73051BCF809525D6E86C25");
1✔
290

291
         dec->start(iv);
1✔
292
         dec->finish(msg1);
1✔
293

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

297
         dec->start();
1✔
298
         dec->finish(msg3);
1✔
299
         result.test_eq("Third plaintext", msg3, "49562063617272796F76657232");
1✔
300

301
   #endif
302
         return result;
1✔
303
      }
7✔
304

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

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

316
         Botan::secure_vector<uint8_t> msg1 = Botan::hex_decode_locked("ABCDEF01234567");
1✔
317
         Botan::secure_vector<uint8_t> msg2 = Botan::hex_decode_locked("0000123456ABCDEF");
1✔
318
         Botan::secure_vector<uint8_t> msg3 = Botan::hex_decode_locked("012345");
1✔
319

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

323
         enc->start(iv);
1✔
324
         enc->finish(msg1);
1✔
325
         result.test_eq("First ciphertext", msg1, "a51522387c4c9b");
1✔
326

327
         enc->start();
1✔
328
         enc->finish(msg2);
1✔
329

330
         result.test_eq("Second ciphertext", msg2, "105457dc2e0649d4");
1✔
331

332
         enc->start();
1✔
333
         enc->finish(msg3);
1✔
334

335
         result.test_eq("Third ciphertext", msg3, "53bd65");
1✔
336

337
         dec->start(iv);
1✔
338
         dec->finish(msg1);
1✔
339
         result.test_eq("First plaintext", msg1, "ABCDEF01234567");
1✔
340

341
         dec->start();
1✔
342
         dec->finish(msg2);
1✔
343
         result.test_eq("Second plaintext", msg2, "0000123456ABCDEF");
1✔
344

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

352
      static Test::Result test_ctr_iv_carry() {
1✔
353
         Test::Result result("CTR IV carry");
1✔
354
   #if defined(BOTAN_HAS_CTR_BE) && defined(BOTAN_HAS_AES)
355

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

361
         const std::vector<uint8_t> key = Botan::hex_decode("2B7E151628AED2A6ABF7158809CF4F3C");
1✔
362
         const std::vector<uint8_t> iv = Botan::hex_decode("F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF");
1✔
363

364
         enc->set_key(key);
1✔
365
         dec->set_key(key);
1✔
366

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

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

389
            Botan::secure_vector<uint8_t> msg(i, 0);
9✔
390
            enc->finish(msg);
9✔
391

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

394
            dec->finish(msg);
9✔
395

396
            for(const uint8_t b : msg) {
54✔
397
               result.test_eq("Plaintext zeros", static_cast<size_t>(b), 0);
90✔
398
            }
399
         }
9✔
400
   #endif
401
         return result;
2✔
402
      }
5✔
403
};
404

405
BOTAN_REGISTER_TEST("modes", "iv_carryover", Cipher_Mode_IV_Carry_Tests);
406

407
#endif
408

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