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

randombit / botan / 21794448852

08 Feb 2026 12:09AM UTC coverage: 90.065% (-0.008%) from 90.073%
21794448852

push

github

web-flow
Merge pull request #5295 from randombit/jack/header-patrol-3

Reduce header dependencies in tests and cli

102230 of 113507 relevant lines covered (90.06%)

11492365.41 hits per line

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

87.5
/src/cli/perf_sym.cpp
1
/*
2
* (C) 2024 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6

7
#include "perf.h"
8

9
#include <botan/assert.h>
10
#include <set>
11

12
#include <botan/rng.h>
13
#include <botan/symkey.h>
14

15
#if defined(BOTAN_HAS_BLOCK_CIPHER)
16
   #include <botan/block_cipher.h>
17
#endif
18

19
#if defined(BOTAN_HAS_CIPHER_MODES)
20
   #include <botan/cipher_mode.h>
21
#endif
22

23
#if defined(BOTAN_HAS_STREAM_CIPHER)
24
   #include <botan/stream_cipher.h>
25
#endif
26

27
#if defined(BOTAN_HAS_HASH)
28
   #include <botan/hash.h>
29
#endif
30

31
#if defined(BOTAN_HAS_MAC)
32
   #include <botan/mac.h>
33
#endif
34

35
#if defined(BOTAN_HAS_XOF)
36
   #include <botan/xof.h>
37
#endif
38

39
namespace Botan_CLI {
40

41
#if defined(BOTAN_HAS_BLOCK_CIPHER)
42
class PerfTest_BlockCipher final : public PerfTest {
43
   public:
44
      explicit PerfTest_BlockCipher(std::string_view alg) : m_alg(alg) {}
4✔
45

46
      void go(const PerfConfig& config) override {
4✔
47
         for(const auto& provider : Botan::BlockCipher::providers(m_alg)) {
8✔
48
            if(auto cipher = Botan::BlockCipher::create(m_alg, provider)) {
4✔
49
               bench_stream_cipher(config, *cipher);
4✔
50
            }
4✔
51
         }
4✔
52
      }
4✔
53

54
      static bool has_impl_for(std::string_view alg) { return !Botan::BlockCipher::providers(alg).empty(); }
7✔
55

56
   private:
57
      static void bench_stream_cipher(const PerfConfig& config, Botan::BlockCipher& cipher) {
4✔
58
         auto& rng = config.rng();
4✔
59
         const auto runtime = config.runtime();
4✔
60
         const auto provider = cipher.provider();
4✔
61

62
         auto ks_timer = config.make_timer(cipher.name(), 1, "key schedule", provider);
8✔
63

64
         const Botan::SymmetricKey key(rng, cipher.maximum_keylength());
4✔
65
         ks_timer->run([&]() { cipher.set_key(key); });
8✔
66

67
         const size_t bs = cipher.block_size();
4✔
68
         std::set<size_t> buf_sizes_in_blocks;
4✔
69
         for(const size_t buf_size : config.buffer_sizes()) {
9✔
70
            if(buf_size % bs == 0) {
5✔
71
               buf_sizes_in_blocks.insert(buf_size);
5✔
72
            } else {
73
               buf_sizes_in_blocks.insert(buf_size + bs - (buf_size % bs));
×
74
            }
75
         }
76

77
         for(const size_t buf_size : buf_sizes_in_blocks) {
9✔
78
            std::vector<uint8_t> buffer(buf_size);
5✔
79
            const size_t mult = std::max<size_t>(1, 65536 / buf_size);
5✔
80
            const size_t blocks = buf_size / bs;
5✔
81

82
            auto encrypt_timer = config.make_timer(cipher.name(), mult * buffer.size(), "encrypt", provider, buf_size);
10✔
83
            auto decrypt_timer = config.make_timer(cipher.name(), mult * buffer.size(), "decrypt", provider, buf_size);
10✔
84

85
            encrypt_timer->run_until_elapsed(runtime, [&]() {
5✔
86
               for(size_t i = 0; i != mult; ++i) {
18,068✔
87
                  cipher.encrypt_n(buffer.data(), buffer.data(), blocks);
17,984✔
88
               }
89
            });
84✔
90
            config.record_result(*encrypt_timer);
5✔
91

92
            decrypt_timer->run_until_elapsed(runtime, [&]() {
5✔
93
               for(size_t i = 0; i != mult; ++i) {
16,848✔
94
                  cipher.decrypt_n(buffer.data(), buffer.data(), blocks);
16,768✔
95
               }
96
            });
80✔
97
            config.record_result(*decrypt_timer);
10✔
98
         }
10✔
99
      }
8✔
100

101
      std::string m_alg;
102
};
103
#endif
104

105
#if defined(BOTAN_HAS_CIPHER_MODES)
106
class PerfTest_CipherMode final : public PerfTest {
107
   public:
108
      explicit PerfTest_CipherMode(std::string_view alg) : m_alg(alg) {}
1✔
109

110
      void go(const PerfConfig& config) override {
1✔
111
         for(const auto& provider : Botan::Cipher_Mode::providers(m_alg)) {
2✔
112
            if(auto enc = Botan::Cipher_Mode::create(m_alg, Botan::Cipher_Dir::Encryption, provider)) {
1✔
113
               auto dec = Botan::Cipher_Mode::create_or_throw(m_alg, Botan::Cipher_Dir::Decryption, provider);
1✔
114
               bench_cipher_mode(config, *enc, *dec);
1✔
115
            }
2✔
116
         }
1✔
117
      }
1✔
118

119
      static bool has_impl_for(std::string_view alg) { return !Botan::Cipher_Mode::providers(alg).empty(); }
3✔
120

121
   private:
122
      static void bench_cipher_mode(const PerfConfig& config, Botan::Cipher_Mode& enc, Botan::Cipher_Mode& dec) {
1✔
123
         auto& rng = config.rng();
1✔
124
         const auto runtime = config.runtime();
1✔
125
         const auto provider = enc.provider();
1✔
126

127
         auto ks_timer = config.make_timer(enc.name(), 1, "key schedule", provider);
2✔
128

129
         const Botan::SymmetricKey key(config.rng(), enc.key_spec().maximum_keylength());
1✔
130

131
         ks_timer->run([&]() { enc.set_key(key); });
2✔
132
         ks_timer->run([&]() { dec.set_key(key); });
2✔
133

134
         config.record_result(*ks_timer);
1✔
135

136
         for(auto buf_size : config.buffer_sizes()) {
2✔
137
            Botan::secure_vector<uint8_t> buffer = rng.random_vec(buf_size);
1✔
138
            const size_t mult = std::max<size_t>(1, 65536 / buf_size);
1✔
139

140
            auto encrypt_timer = config.make_timer(enc.name(), mult * buffer.size(), "encrypt", provider, buf_size);
2✔
141
            auto decrypt_timer = config.make_timer(dec.name(), mult * buffer.size(), "decrypt", provider, buf_size);
2✔
142

143
            Botan::secure_vector<uint8_t> iv = rng.random_vec(enc.default_nonce_length());
1✔
144

145
            if(buf_size >= enc.minimum_final_size()) {
1✔
146
               encrypt_timer->run_until_elapsed(runtime, [&]() {
2✔
147
                  for(size_t i = 0; i != mult; ++i) {
260✔
148
                     enc.start(iv);
256✔
149
                     enc.finish(buffer);
256✔
150
                     buffer.resize(buf_size);  // remove any tag or padding
256✔
151
                  }
152
               });
4✔
153

154
               while(decrypt_timer->under(runtime)) {
5✔
155
                  if(!iv.empty()) {
4✔
156
                     iv[iv.size() - 1] += 1;
4✔
157
                  }
158

159
                  // Create a valid ciphertext/tag for decryption to run on
160
                  buffer.resize(buf_size);
4✔
161
                  enc.start(iv);
4✔
162
                  enc.finish(buffer);
4✔
163

164
                  Botan::secure_vector<uint8_t> dbuffer;
4✔
165

166
                  decrypt_timer->run([&]() {
4✔
167
                     for(size_t i = 0; i != mult; ++i) {
260✔
168
                        dbuffer = buffer;
256✔
169
                        dec.start(iv);
256✔
170
                        dec.finish(dbuffer);
256✔
171
                     }
172
                  });
4✔
173
               }
4✔
174
            }
175

176
            config.record_result(*encrypt_timer);
1✔
177
            config.record_result(*decrypt_timer);
2✔
178
         }
4✔
179
      }
1✔
180

181
      std::string m_alg;
182
};
183
#endif
184

185
#if defined(BOTAN_HAS_STREAM_CIPHER)
186
class PerfTest_StreamCipher final : public PerfTest {
187
   public:
188
      explicit PerfTest_StreamCipher(std::string_view alg) : m_alg(alg) {}
1✔
189

190
      void go(const PerfConfig& config) override {
1✔
191
         for(const auto& provider : Botan::StreamCipher::providers(m_alg)) {
2✔
192
            if(auto cipher = Botan::StreamCipher::create(m_alg, provider)) {
1✔
193
               bench_stream_cipher(config, *cipher);
1✔
194
            }
1✔
195
         }
1✔
196
      }
1✔
197

198
      static bool has_impl_for(std::string_view alg) { return !Botan::StreamCipher::providers(alg).empty(); }
8✔
199

200
   private:
201
      static void bench_stream_cipher(const PerfConfig& config, Botan::StreamCipher& cipher) {
1✔
202
         auto& rng = config.rng();
1✔
203
         const auto runtime = config.runtime();
1✔
204
         const auto provider = cipher.provider();
1✔
205

206
         for(auto buf_size : config.buffer_sizes()) {
2✔
207
            const Botan::SymmetricKey key(rng, cipher.maximum_keylength());
1✔
208
            cipher.set_key(key);
1✔
209

210
            if(cipher.valid_iv_length(12)) {
1✔
211
               const Botan::InitializationVector iv(rng, 12);
1✔
212
               cipher.set_iv(iv.begin(), iv.size());
1✔
213
            }
1✔
214

215
            auto buffer = rng.random_vec(buf_size);
1✔
216

217
            const size_t mult = std::max<size_t>(1, 65536 / buf_size);
1✔
218

219
            auto encrypt_timer = config.make_timer(cipher.name(), mult * buffer.size(), "encrypt", provider, buf_size);
2✔
220

221
            encrypt_timer->run_until_elapsed(runtime, [&]() {
1✔
222
               for(size_t i = 0; i != mult; ++i) {
910✔
223
                  cipher.encipher(buffer);
896✔
224
               }
225
            });
14✔
226

227
            config.record_result(*encrypt_timer);
1✔
228

229
            auto ks_timer =
1✔
230
               config.make_timer(cipher.name(), mult * buffer.size(), "write_keystream", provider, buf_size);
2✔
231

232
            while(ks_timer->under(runtime)) {
27✔
233
               ks_timer->run([&]() {
26✔
234
                  for(size_t i = 0; i != mult; ++i) {
1,690✔
235
                     cipher.write_keystream(buffer.data(), buffer.size());
1,664✔
236
                  }
237
               });
26✔
238
            }
239

240
            config.record_result(*ks_timer);
2✔
241
         }
3✔
242
      }
1✔
243

244
      std::string m_alg;
245
};
246
#endif
247

248
#if defined(BOTAN_HAS_HASH)
249
class PerfTest_HashFunction final : public PerfTest {
250
   public:
251
      explicit PerfTest_HashFunction(std::string_view alg) : m_alg(alg) {}
1✔
252

253
      void go(const PerfConfig& config) override {
1✔
254
         for(const auto& provider : Botan::HashFunction::providers(m_alg)) {
2✔
255
            if(auto hash = Botan::HashFunction::create(m_alg, provider)) {
1✔
256
               bench_hash_fn(config, *hash);
1✔
257
            }
1✔
258
         }
1✔
259
      }
1✔
260

261
      static bool has_impl_for(std::string_view alg) { return !Botan::HashFunction::providers(alg).empty(); }
2✔
262

263
   private:
264
      static void bench_hash_fn(const PerfConfig& config, Botan::HashFunction& hash) {
1✔
265
         std::vector<uint8_t> output(hash.output_length());
1✔
266
         const auto provider = hash.provider();
1✔
267
         const auto runtime = config.runtime();
1✔
268

269
         for(auto buf_size : config.buffer_sizes()) {
2✔
270
            const auto buffer = config.rng().random_vec(buf_size);
1✔
271

272
            const size_t mult = std::max<size_t>(1, 65536 / buf_size);
1✔
273

274
            auto timer = config.make_timer(hash.name(), mult * buffer.size(), "hash", provider, buf_size);
2✔
275
            timer->run_until_elapsed(runtime, [&]() {
1✔
276
               for(size_t i = 0; i != mult; ++i) {
325✔
277
                  hash.update(buffer);
320✔
278
                  hash.final(output.data());
640✔
279
               }
280
            });
5✔
281
            config.record_result(*timer);
2✔
282
         }
2✔
283
      }
2✔
284

285
      std::string m_alg;
286
};
287
#endif
288

289
#if defined(BOTAN_HAS_MAC)
290
class PerfTest_MessageAuthenticationCode final : public PerfTest {
291
   public:
292
      explicit PerfTest_MessageAuthenticationCode(std::string_view alg) : m_alg(alg) {}
1✔
293

294
      void go(const PerfConfig& config) override {
1✔
295
         for(const auto& provider : Botan::MessageAuthenticationCode::providers(m_alg)) {
2✔
296
            if(auto mac = Botan::MessageAuthenticationCode::create(m_alg, provider)) {
1✔
297
               bench_mac_fn(config, *mac);
1✔
298
            }
1✔
299
         }
1✔
300
      }
1✔
301

302
      static bool has_impl_for(std::string_view alg) {
1✔
303
         return !Botan::MessageAuthenticationCode::providers(alg).empty();
1✔
304
      }
305

306
   private:
307
      static void bench_mac_fn(const PerfConfig& config, Botan::MessageAuthenticationCode& mac) {
1✔
308
         std::vector<uint8_t> output(mac.output_length());
1✔
309
         const auto provider = mac.provider();
1✔
310
         const auto runtime = config.runtime();
1✔
311
         auto& rng = config.rng();
1✔
312

313
         for(auto buf_size : config.buffer_sizes()) {
2✔
314
            Botan::secure_vector<uint8_t> buffer = rng.random_vec(buf_size);
1✔
315
            const size_t mult = std::max<size_t>(1, 65536 / buf_size);
1✔
316

317
            const Botan::SymmetricKey key(rng, mac.maximum_keylength());
1✔
318
            mac.set_key(key);
1✔
319

320
            auto timer = config.make_timer(mac.name(), mult * buffer.size(), "mac", provider, buf_size);
2✔
321
            timer->run_until_elapsed(runtime, [&]() {
1✔
322
               for(size_t i = 0; i != mult; ++i) {
260✔
323
                  if(mac.fresh_key_required_per_message()) {
256✔
324
                     mac.set_key(key);
×
325
                  }
326
                  mac.start(nullptr, 0);
256✔
327
                  mac.update(buffer);
256✔
328
                  mac.final(output.data());
512✔
329
               }
330
            });
4✔
331

332
            config.record_result(*timer);
2✔
333
         }
3✔
334
      }
2✔
335

336
      std::string m_alg;
337
};
338
#endif
339

340
#if defined(BOTAN_HAS_XOF)
341
class PerfTest_XOF final : public PerfTest {
342
   public:
343
      explicit PerfTest_XOF(std::string_view alg) : m_alg(alg) {}
×
344

345
      void go(const PerfConfig& config) override {
×
346
         for(const auto& provider : Botan::XOF::providers(m_alg)) {
×
347
            if(auto xof = Botan::XOF::create(m_alg, provider)) {
×
348
               bench_xof_fn(config, *xof);
×
349
            }
×
350
         }
×
351
      }
×
352

353
      static bool has_impl_for(std::string_view alg) { return !Botan::XOF::providers(alg).empty(); }
8✔
354

355
   private:
356
      static void bench_xof_fn(const PerfConfig& config, Botan::XOF& xof) {
×
357
         const auto runtime = config.runtime();
×
358
         const auto provider = xof.provider();
×
359

360
         for(const size_t buf_size : config.buffer_sizes()) {
×
361
            auto in = config.rng().random_vec(buf_size);
×
362
            Botan::secure_vector<uint8_t> out(buf_size);
×
363

364
            auto in_timer = config.make_timer(xof.name(), in.size(), "input", provider, buf_size);
×
365
            in_timer->run_until_elapsed(runtime / 2, [&]() { xof.update(in); });
×
366

367
            auto out_timer = config.make_timer(xof.name(), out.size(), "output", provider, buf_size);
×
368
            out_timer->run_until_elapsed(runtime / 2, [&] { xof.output(out); });
×
369

370
            config.record_result(*in_timer);
×
371
            config.record_result(*out_timer);
×
372

373
            // Our XOFs don't want to consume inputs after producing output, so reset the state
374
            xof.clear();
×
375
         }
×
376
      }
×
377

378
      std::string m_alg;
379
};
380
#endif
381

382
//static
383
std::unique_ptr<PerfTest> PerfTest::get_sym(const std::string& alg) {
8✔
384
#if defined(BOTAN_HAS_XOF)
385
   if(PerfTest_XOF::has_impl_for(alg)) {
8✔
386
      return std::make_unique<PerfTest_XOF>(alg);
×
387
   }
388
#endif
389

390
#if defined(BOTAN_HAS_STREAM_CIPHER)
391
   if(PerfTest_StreamCipher::has_impl_for(alg)) {
8✔
392
      return std::make_unique<PerfTest_StreamCipher>(alg);
1✔
393
   }
394
#endif
395

396
#if defined(BOTAN_HAS_BLOCK_CIPHER)
397
   if(PerfTest_BlockCipher::has_impl_for(alg)) {
7✔
398
      return std::make_unique<PerfTest_BlockCipher>(alg);
4✔
399
   }
400
#endif
401

402
#if defined(BOTAN_HAS_CIPHER_MODES)
403
   if(PerfTest_CipherMode::has_impl_for(alg)) {
3✔
404
      return std::make_unique<PerfTest_CipherMode>(alg);
1✔
405
   }
406
#endif
407

408
#if defined(BOTAN_HAS_HASH)
409
   if(PerfTest_HashFunction::has_impl_for(alg)) {
2✔
410
      return std::make_unique<PerfTest_HashFunction>(alg);
1✔
411
   }
412
#endif
413

414
#if defined(BOTAN_HAS_MAC)
415
   if(PerfTest_MessageAuthenticationCode::has_impl_for(alg)) {
1✔
416
      return std::make_unique<PerfTest_MessageAuthenticationCode>(alg);
1✔
417
   }
418
#endif
419

420
   BOTAN_UNUSED(alg);
×
421
   return {};
×
422
}
423

424
}  // namespace Botan_CLI
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