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

randombit / botan / 16238531931

12 Jul 2025 01:33PM UTC coverage: 90.574% (+0.002%) from 90.572%
16238531931

push

github

web-flow
Merge pull request #4970 from reneme/rene/throw_as_improvement

Test: Improve AEAD and cipher mode error condition checks

99085 of 109397 relevant lines covered (90.57%)

12435022.98 hits per line

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

91.34
/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
#endif
14

15
namespace Botan_Tests {
16

17
#if defined(BOTAN_HAS_CIPHER_MODES)
18

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

77
            try {
1,245✔
78
               test_mode(result, algo, provider_ask, "decryption", *dec, key, nonce, expected, input, this->rng());
2,490✔
79
            } catch(Botan::Exception& e) {
×
80
               result.test_failure("Decryption tests failed", e.what());
×
81
            }
×
82
         }
2,490✔
83

84
         return result;
85
      }
6,225✔
86

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

101
         result.test_eq("name", mode.name(), algo);
4,980✔
102

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

108
         result.test_eq("mode not authenticated", mode.authenticated(), false);
2,490✔
109

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

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

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

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

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

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

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

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

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

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

153
         mode.set_key(mutate_vec(key, rng));
2,490✔
154

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

160
         mode.start(mutate_vec(nonce, rng));
2,490✔
161
         mode.reset();
2,490✔
162

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

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

171
         mode.reset();
2,490✔
172

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

177
         Botan::secure_vector<uint8_t> buf;
2,490✔
178

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

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

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

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

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

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

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

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

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

216
            buf += last_bits;
2,165✔
217

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

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

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

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

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

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

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

242
BOTAN_REGISTER_SMOKE_TEST("modes", "cipher_modes", Cipher_Mode_Tests);
243

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

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

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

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

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

272
         enc->set_key(key);
1✔
273
         dec->set_key(key);
1✔
274

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

279
         enc->start();
1✔
280
         enc->finish(msg2);
1✔
281

282
         result.test_eq("Second ciphertext", msg2, "AA8D682958A4A044735DAC502B274DB2");
1✔
283

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

287
         result.test_eq("Third ciphertext", msg3, "1241B9976F73051BCF809525D6E86C25");
1✔
288

289
         dec->start(iv);
1✔
290
         dec->finish(msg1);
1✔
291

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

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

299
   #endif
300
         return result;
1✔
301
      }
7✔
302

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

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

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

318
         enc->set_key(key);
1✔
319
         dec->set_key(key);
1✔
320

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

325
         enc->start();
1✔
326
         enc->finish(msg2);
1✔
327

328
         result.test_eq("Second ciphertext", msg2, "105457dc2e0649d4");
1✔
329

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

333
         result.test_eq("Third ciphertext", msg3, "53bd65");
1✔
334

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

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

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

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

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

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

362
         enc->set_key(key);
1✔
363
         dec->set_key(key);
1✔
364

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

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

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

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

392
            dec->finish(msg);
9✔
393

394
            for(size_t j = 0; j != msg.size(); ++j) {
54✔
395
               result.test_eq("Plaintext zeros", static_cast<size_t>(msg[j]), 0);
90✔
396
            }
397
         }
9✔
398
   #endif
399
         return result;
2✔
400
      }
5✔
401
};
402

403
BOTAN_REGISTER_TEST("modes", "iv_carryover", Cipher_Mode_IV_Carry_Tests);
404

405
#endif
406

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