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

randombit / botan / 21753596263

06 Feb 2026 02:13PM UTC coverage: 90.063% (-0.01%) from 90.073%
21753596263

Pull #5289

github

web-flow
Merge 587099284 into 8ea0ca252
Pull Request #5289: Further misc header reductions, forward declarations, etc

102237 of 113517 relevant lines covered (90.06%)

11402137.11 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/hex.h>
14
   #include <botan/rng.h>
15
#endif
16

17
namespace Botan_Tests {
18

19
#if defined(BOTAN_HAS_CIPHER_MODES)
20

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

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

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

35
         Test::Result result(algo);
2,492✔
36

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

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

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

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

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

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

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

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

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

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

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

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

87
         return result;
88
      }
6,230✔
89

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

104
         result.test_eq("name", mode.name(), algo);
4,984✔
105

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

111
         result.test_eq("mode not authenticated", mode.authenticated(), false);
2,492✔
112

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

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

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

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

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

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

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

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

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

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

156
         mode.set_key(mutate_vec(key, rng));
2,492✔
157

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

163
         mode.start(mutate_vec(nonce, rng));
2,492✔
164
         mode.reset();
2,492✔
165

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

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

174
         mode.reset();
2,492✔
175

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

180
         Botan::secure_vector<uint8_t> buf;
2,492✔
181

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

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

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

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

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

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

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

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

215
            mode.start(nonce);
4,334✔
216
            mode.update(buf);
2,167✔
217
            mode.finish(last_bits);
2,167✔
218

219
            buf += last_bits;
2,167✔
220

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

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

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

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

231
            mode.finish(buf, bytes_to_process);
2,167✔
232
            result.test_eq(direction + " process", buf, expected);
4,334✔
233
         }
2,167✔
234

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

317
         Botan::secure_vector<uint8_t> msg1 = Botan::hex_decode_locked("ABCDEF01234567");
1✔
318
         Botan::secure_vector<uint8_t> msg2 = Botan::hex_decode_locked("0000123456ABCDEF");
1✔
319
         Botan::secure_vector<uint8_t> msg3 = Botan::hex_decode_locked("012345");
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_eq("First ciphertext", msg1, "a51522387c4c9b");
1✔
327

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

408
#endif
409

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