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

randombit / botan / 16245106037

13 Jul 2025 03:53AM UTC coverage: 90.576% (+0.004%) from 90.572%
16245106037

push

github

web-flow
Merge pull request #4981 from randombit/jack/clang-tidy-suspicious-call-argument

Enable and fix clang-tidy warning suspcious-call-argument

99102 of 109413 relevant lines covered (90.58%)

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

85
         return result;
86
      }
6,225✔
87

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

217
            buf += last_bits;
2,165✔
218

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

406
#endif
407

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