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

randombit / botan / 11300480493

11 Oct 2024 11:12PM UTC coverage: 91.113% (+0.1%) from 90.981%
11300480493

push

github

web-flow
Merge pull request #4367 from randombit/jack/refactor-speed-pk

Refactor public key related performance test code

90011 of 98791 relevant lines covered (91.11%)

9071963.87 hits per line

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

83.53
/src/cli/speed.cpp
1
/*
2
* (C) 2009,2010,2014,2015,2017,2018,2024 Jack Lloyd
3
* (C) 2015 Simon Warta (Kullo GmbH)
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include "cli.h"
9
#include "perf.h"
10

11
#include <algorithm>
12
#include <chrono>
13
#include <functional>
14
#include <iomanip>
15
#include <map>
16
#include <set>
17
#include <sstream>
18

19
// Always available:
20
#include <botan/version.h>
21
#include <botan/internal/cpuid.h>
22
#include <botan/internal/fmt.h>
23
#include <botan/internal/os_utils.h>
24
#include <botan/internal/stl_util.h>
25
#include <botan/internal/timer.h>
26

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

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

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

39
#if defined(BOTAN_HAS_XOF)
40
   #include <botan/xof.h>
41
#endif
42

43
#if defined(BOTAN_HAS_CIPHER_MODES)
44
   #include <botan/cipher_mode.h>
45
#endif
46

47
#if defined(BOTAN_HAS_MAC)
48
   #include <botan/mac.h>
49
#endif
50

51
#if defined(BOTAN_HAS_ECC_GROUP)
52
   #include <botan/ec_group.h>
53
#endif
54

55
namespace Botan_CLI {
56

57
using Botan::Timer;
58

59
namespace {
60

61
class JSON_Output final {
2✔
62
   public:
63
      void add(const Timer& timer) { m_results.push_back(timer); }
2✔
64

65
      std::string print() const {
1✔
66
         std::ostringstream out;
1✔
67

68
         out << "[\n";
1✔
69

70
         for(size_t i = 0; i != m_results.size(); ++i) {
3✔
71
            const Timer& t = m_results[i];
2✔
72

73
            out << "{"
2✔
74
                << "\"algo\": \"" << t.get_name() << "\", "
2✔
75
                << "\"op\": \"" << t.doing() << "\", "
2✔
76
                << "\"events\": " << t.events() << ", ";
2✔
77

78
            if(t.cycles_consumed() > 0) {
4✔
79
               out << "\"cycles\": " << t.cycles_consumed() << ", ";
4✔
80
            }
81

82
            if(t.buf_size() > 0) {
2✔
83
               out << "\"bps\": " << static_cast<uint64_t>(t.events() / (t.value() / 1000000000.0)) << ", ";
2✔
84
               out << "\"buf_size\": " << t.buf_size() << ", ";
2✔
85
            }
86

87
            out << "\"nanos\": " << t.value() << "}";
2✔
88

89
            if(i != m_results.size() - 1) {
2✔
90
               out << ",";
1✔
91
            }
92

93
            out << "\n";
2✔
94
         }
95
         out << "]\n";
1✔
96

97
         return out.str();
2✔
98
      }
1✔
99

100
   private:
101
      std::vector<Timer> m_results;
102
};
103

104
class Summary final {
1✔
105
   public:
106
      Summary() = default;
1✔
107

108
      void add(const Timer& t) {
2✔
109
         if(t.buf_size() == 0) {
2✔
110
            m_ops_entries.push_back(t);
×
111
         } else {
112
            m_bps_entries[std::make_pair(t.doing(), t.get_name())].push_back(t);
4✔
113
         }
114
      }
2✔
115

116
      std::string print() {
1✔
117
         const size_t name_padding = 35;
1✔
118
         const size_t op_name_padding = 16;
1✔
119
         const size_t op_padding = 16;
1✔
120

121
         std::ostringstream result_ss;
1✔
122
         result_ss << std::fixed;
1✔
123

124
         if(!m_bps_entries.empty()) {
1✔
125
            result_ss << "\n";
1✔
126

127
            // add table header
128
            result_ss << std::setw(name_padding) << std::left << "algo" << std::setw(op_name_padding) << std::left
1✔
129
                      << "operation";
1✔
130

131
            for(const Timer& t : m_bps_entries.begin()->second) {
2✔
132
               result_ss << std::setw(op_padding) << std::right << (std::to_string(t.buf_size()) + " bytes");
2✔
133
            }
134
            result_ss << "\n";
1✔
135

136
            // add table entries
137
            for(const auto& entry : m_bps_entries) {
3✔
138
               if(entry.second.empty()) {
2✔
139
                  continue;
×
140
               }
141

142
               result_ss << std::setw(name_padding) << std::left << (entry.first.second) << std::setw(op_name_padding)
2✔
143
                         << std::left << (entry.first.first);
2✔
144

145
               for(const Timer& t : entry.second) {
4✔
146
                  if(t.events() == 0) {
2✔
147
                     result_ss << std::setw(op_padding) << std::right << "N/A";
×
148
                  } else {
149
                     result_ss << std::setw(op_padding) << std::right << std::setprecision(2)
2✔
150
                               << (t.bytes_per_second() / 1000.0);
2✔
151
                  }
152
               }
153

154
               result_ss << "\n";
2✔
155
            }
156

157
            result_ss << "\n[results are the number of 1000s bytes processed per second]\n";
1✔
158
         }
159

160
         if(!m_ops_entries.empty()) {
1✔
161
            result_ss << std::setprecision(6) << "\n";
×
162

163
            // sort entries
164
            std::sort(m_ops_entries.begin(), m_ops_entries.end());
×
165

166
            // add table header
167
            result_ss << std::setw(name_padding) << std::left << "algo" << std::setw(op_name_padding) << std::left
×
168
                      << "operation" << std::setw(op_padding) << std::right << "sec/op" << std::setw(op_padding)
×
169
                      << std::right << "op/sec"
×
170
                      << "\n";
×
171

172
            // add table entries
173
            for(const Timer& entry : m_ops_entries) {
×
174
               result_ss << std::setw(name_padding) << std::left << entry.get_name() << std::setw(op_name_padding)
×
175
                         << std::left << entry.doing() << std::setw(op_padding) << std::right
×
176
                         << entry.seconds_per_event() << std::setw(op_padding) << std::right
×
177
                         << entry.events_per_second() << "\n";
×
178
            }
179
         }
180

181
         return result_ss.str();
2✔
182
      }
1✔
183

184
   private:
185
      std::map<std::pair<std::string, std::string>, std::vector<Timer>> m_bps_entries;
186
      std::vector<Timer> m_ops_entries;
187
};
188

189
std::vector<size_t> unique_buffer_sizes(const std::string& cmdline_arg) {
30✔
190
   const size_t MAX_BUF_SIZE = 64 * 1024 * 1024;
30✔
191

192
   std::set<size_t> buf;
30✔
193
   for(const std::string& size_str : Command::split_on(cmdline_arg, ',')) {
58✔
194
      size_t x = 0;
31✔
195
      try {
31✔
196
         size_t converted = 0;
31✔
197
         x = static_cast<size_t>(std::stoul(size_str, &converted, 0));
31✔
198

199
         if(converted != size_str.size()) {
30✔
200
            throw CLI_Usage_Error("Invalid integer");
×
201
         }
202
      } catch(std::exception&) {
1✔
203
         throw CLI_Usage_Error("Invalid integer value '" + size_str + "' for option buf-size");
2✔
204
      }
1✔
205

206
      if(x == 0) {
30✔
207
         throw CLI_Usage_Error("Cannot have a zero-sized buffer");
2✔
208
      }
209

210
      if(x > MAX_BUF_SIZE) {
29✔
211
         throw CLI_Usage_Error("Specified buffer size is too large");
2✔
212
      }
213

214
      buf.insert(x);
28✔
215
   }
30✔
216

217
   return std::vector<size_t>(buf.begin(), buf.end());
30✔
218
}
27✔
219

220
}  // namespace
221

222
class Speed final : public Command {
×
223
   public:
224
      Speed() :
31✔
225
            Command(
226
               "speed --msec=500 --format=default --ecc-groups= --provider= --buf-size=1024 --clear-cpuid= --cpu-clock-speed=0 --cpu-clock-ratio=1.0 *algos") {
62✔
227
      }
31✔
228

229
      static std::vector<std::string> default_benchmark_list() {
×
230
         /*
231
         This is not intended to be exhaustive: it just hits the high
232
         points of the most interesting or widely used algorithms.
233
         */
234
         // clang-format off
235
         return {
×
236
            /* Block ciphers */
237
            "AES-128",
238
            "AES-192",
239
            "AES-256",
240
            "ARIA-128",
241
            "ARIA-192",
242
            "ARIA-256",
243
            "Blowfish",
244
            "CAST-128",
245
            "Camellia-128",
246
            "Camellia-192",
247
            "Camellia-256",
248
            "DES",
249
            "TripleDES",
250
            "GOST-28147-89",
251
            "IDEA",
252
            "Noekeon",
253
            "SHACAL2",
254
            "SM4",
255
            "Serpent",
256
            "Threefish-512",
257
            "Twofish",
258

259
            /* Cipher modes */
260
            "AES-128/CBC",
261
            "AES-128/CTR-BE",
262
            "AES-128/EAX",
263
            "AES-128/OCB",
264
            "AES-128/GCM",
265
            "AES-128/XTS",
266
            "AES-128/SIV",
267

268
            "Serpent/CBC",
269
            "Serpent/CTR-BE",
270
            "Serpent/EAX",
271
            "Serpent/OCB",
272
            "Serpent/GCM",
273
            "Serpent/XTS",
274
            "Serpent/SIV",
275

276
            "ChaCha20Poly1305",
277

278
            /* Stream ciphers */
279
            "RC4",
280
            "Salsa20",
281
            "ChaCha20",
282

283
            /* Hashes */
284
            "SHA-1",
285
            "SHA-256",
286
            "SHA-512",
287
            "SHA-3(256)",
288
            "SHA-3(512)",
289
            "RIPEMD-160",
290
            "Skein-512",
291
            "Blake2b",
292
            "Whirlpool",
293

294
            /* XOFs */
295
            "SHAKE-128",
296
            "SHAKE-256",
297

298
            /* MACs */
299
            "CMAC(AES-128)",
300
            "HMAC(SHA-256)",
301

302
            /* pubkey */
303
            "RSA",
304
            "DH",
305
            "ECDH",
306
            "ECDSA",
307
            "Ed25519",
308
            "Ed448",
309
            "X25519",
310
            "X448",
311
            "Kyber",
312
            "SPHINCS+",
313
            "FrodoKEM",
314
            "HSS-LMS",
315
         };
×
316
         // clang-format on
317
      }
318

319
      std::string group() const override { return "misc"; }
1✔
320

321
      std::string description() const override { return "Measures the speed of algorithms"; }
1✔
322

323
      void go() override {
30✔
324
         std::chrono::milliseconds msec(get_arg_sz("msec"));
30✔
325
         const std::string provider = get_arg("provider");
30✔
326
         std::vector<std::string> ecc_groups = Command::split_on(get_arg("ecc-groups"), ',');
63✔
327
         const std::string format = get_arg("format");
30✔
328
         const std::string clock_ratio = get_arg("cpu-clock-ratio");
33✔
329
         m_clock_speed = get_arg_sz("cpu-clock-speed");
30✔
330

331
         m_clock_cycle_ratio = std::strtod(clock_ratio.c_str(), nullptr);
30✔
332

333
         /*
334
         * This argument is intended to be the ratio between the cycle counter
335
         * and the actual machine cycles. It is extremely unlikely that there is
336
         * any machine where the cycle counter increments faster than the actual
337
         * clock.
338
         */
339
         if(m_clock_cycle_ratio < 0.0 || m_clock_cycle_ratio > 1.0) {
30✔
340
            throw CLI_Usage_Error("Unlikely CPU clock ratio of " + clock_ratio);
×
341
         }
342

343
         m_clock_cycle_ratio = 1.0 / m_clock_cycle_ratio;
30✔
344

345
         if(m_clock_speed != 0 && Botan::OS::get_cpu_cycle_counter() != 0) {
30✔
346
            error_output() << "The --cpu-clock-speed option is only intended to be used on "
×
347
                              "platforms without access to a cycle counter.\n"
348
                              "Expected incorrect results\n\n";
×
349
         }
350

351
         if(format == "table") {
30✔
352
            m_summary = std::make_unique<Summary>();
1✔
353
         } else if(format == "json") {
29✔
354
            m_json = std::make_unique<JSON_Output>();
1✔
355
         } else if(format != "default") {
28✔
356
            throw CLI_Usage_Error("Unknown --format type '" + format + "'");
×
357
         }
358

359
#if defined(BOTAN_HAS_ECC_GROUP)
360
         if(ecc_groups.empty()) {
30✔
361
            ecc_groups = {"secp256r1", "secp384r1", "secp521r1", "brainpool256r1", "brainpool384r1", "brainpool512r1"};
240✔
362
         } else if(ecc_groups.size() == 1 && ecc_groups[0] == "all") {
×
363
            auto all = Botan::EC_Group::known_named_groups();
×
364
            ecc_groups.assign(all.begin(), all.end());
×
365
         }
×
366
#endif
367

368
         std::vector<std::string> algos = get_arg_list("algos");
33✔
369

370
         const std::vector<size_t> buf_sizes = unique_buffer_sizes(get_arg("buf-size"));
63✔
371

372
         for(const std::string& cpuid_to_clear : Command::split_on(get_arg("clear-cpuid"), ',')) {
28✔
373
            auto bits = Botan::CPUID::bit_from_string(cpuid_to_clear);
1✔
374
            if(bits.empty()) {
1✔
375
               error_output() << "Warning don't know CPUID flag '" << cpuid_to_clear << "'\n";
1✔
376
            }
377

378
            for(auto bit : bits) {
1✔
379
               Botan::CPUID::clear_cpuid_bit(bit);
×
380
            }
381
         }
28✔
382

383
         if(verbose() || m_summary) {
27✔
384
            output() << Botan::version_string() << "\n"
2✔
385
                     << "CPUID: " << Botan::CPUID::to_string() << "\n\n";
3✔
386
         }
387

388
         const bool using_defaults = (algos.empty());
27✔
389
         if(using_defaults) {
27✔
390
            algos = default_benchmark_list();
×
391
         }
392

393
         class PerfConfig_Cli final : public PerfConfig {
27✔
394
            public:
395
               PerfConfig_Cli(std::chrono::milliseconds runtime,
27✔
396
                              const std::vector<std::string>& ecc_groups,
397
                              const std::vector<size_t>& buffer_sizes,
398
                              Speed* speed) :
27✔
399
                     m_runtime(runtime), m_ecc_groups(ecc_groups), m_buffer_sizes(buffer_sizes), m_speed(speed) {}
27✔
400

401
               const std::vector<size_t>& buffer_sizes() const override { return m_buffer_sizes; }
7✔
402

403
               const std::vector<std::string>& ecc_groups() const override { return m_ecc_groups; }
6✔
404

405
               std::chrono::milliseconds runtime() const override { return m_runtime; }
119✔
406

407
               std::ostream& error_output() const override { return m_speed->error_output(); }
×
408

409
               Botan::RandomNumberGenerator& rng() const override { return m_speed->rng(); }
10,741✔
410

411
               void record_result(const Botan::Timer& timer) const override { m_speed->record_result(timer); }
439✔
412

413
               std::unique_ptr<Botan::Timer> make_timer(const std::string& alg,
455✔
414
                                                        uint64_t event_mult,
415
                                                        const std::string& what,
416
                                                        const std::string& provider,
417
                                                        size_t buf_size) const override {
418
                  return m_speed->make_timer(alg, event_mult, what, provider, buf_size);
910✔
419
               }
420

421
            private:
422
               std::chrono::milliseconds m_runtime;
423
               std::vector<std::string> m_ecc_groups;
424
               std::vector<size_t> m_buffer_sizes;
425
               Speed* m_speed;
426
         };
427

428
         PerfConfig_Cli perf_config(msec, ecc_groups, buf_sizes, this);
27✔
429

430
         for(const auto& algo : algos) {
74✔
431
            using namespace std::placeholders;
47✔
432

433
            if(auto perf = PerfTest::get(algo)) {
47✔
434
               perf->go(perf_config);
39✔
435
            }
436
#if defined(BOTAN_HAS_HASH)
437
            else if(!Botan::HashFunction::providers(algo).empty()) {
8✔
438
               bench_providers_of<Botan::HashFunction>(
1✔
439
                  algo, provider, msec, buf_sizes, std::bind(&Speed::bench_hash, this, _1, _2, _3, _4));
2✔
440
            }
441
#endif
442
#if defined(BOTAN_HAS_XOF)
443
            else if(!Botan::XOF::providers(algo).empty()) {
7✔
444
               bench_providers_of<Botan::XOF>(
×
445
                  algo, provider, msec, buf_sizes, std::bind(&Speed::bench_xof, this, _1, _2, _3, _4));
×
446
            }
447
#endif
448
#if defined(BOTAN_HAS_BLOCK_CIPHER)
449
            else if(!Botan::BlockCipher::providers(algo).empty()) {
7✔
450
               bench_providers_of<Botan::BlockCipher>(
4✔
451
                  algo, provider, msec, buf_sizes, std::bind(&Speed::bench_block_cipher, this, _1, _2, _3, _4));
8✔
452
            }
453
#endif
454
#if defined(BOTAN_HAS_STREAM_CIPHER)
455
            else if(!Botan::StreamCipher::providers(algo).empty()) {
3✔
456
               bench_providers_of<Botan::StreamCipher>(
1✔
457
                  algo, provider, msec, buf_sizes, std::bind(&Speed::bench_stream_cipher, this, _1, _2, _3, _4));
2✔
458
            }
459
#endif
460
#if defined(BOTAN_HAS_CIPHER_MODES)
461
            else if(auto enc = Botan::Cipher_Mode::create(algo, Botan::Cipher_Dir::Encryption, provider)) {
2✔
462
               auto dec = Botan::Cipher_Mode::create_or_throw(algo, Botan::Cipher_Dir::Decryption, provider);
1✔
463
               bench_cipher_mode(*enc, *dec, msec, buf_sizes);
1✔
464
            }
1✔
465
#endif
466
#if defined(BOTAN_HAS_MAC)
467
            else if(!Botan::MessageAuthenticationCode::providers(algo).empty()) {
1✔
468
               bench_providers_of<Botan::MessageAuthenticationCode>(
1✔
469
                  algo, provider, msec, buf_sizes, std::bind(&Speed::bench_mac, this, _1, _2, _3, _4));
2✔
470
            }
471
#endif
472
            else {
473
               if(verbose() || !using_defaults) {
×
474
                  error_output() << "Unknown algorithm '" << algo << "'\n";
×
475
               }
476
            }
49✔
477
         }
478

479
         if(m_json) {
27✔
480
            output() << m_json->print();
2✔
481
         }
482
         if(m_summary) {
27✔
483
            output() << m_summary->print() << "\n";
3✔
484
         }
485

486
         if(verbose() && m_clock_speed == 0 && m_cycles_consumed > 0 && m_ns_taken > 0) {
27✔
487
            const double seconds = static_cast<double>(m_ns_taken) / 1000000000;
×
488
            const double Hz = static_cast<double>(m_cycles_consumed) / seconds;
×
489
            const double MHz = Hz / 1000000;
×
490
            output() << "\nEstimated clock speed " << MHz << " MHz\n";
×
491
         }
492
      }
123✔
493

494
   private:
495
      size_t m_clock_speed = 0;
496
      double m_clock_cycle_ratio = 0.0;
497
      uint64_t m_cycles_consumed = 0;
498
      uint64_t m_ns_taken = 0;
499
      std::unique_ptr<Summary> m_summary;
500
      std::unique_ptr<JSON_Output> m_json;
501

502
      void record_result(const Timer& t) {
455✔
503
         m_ns_taken += t.value();
455✔
504
         m_cycles_consumed += t.cycles_consumed();
455✔
505
         if(m_json) {
455✔
506
            m_json->add(t);
2✔
507
         } else {
508
            output() << t.to_string() << std::flush;
906✔
509
            if(m_summary) {
453✔
510
               m_summary->add(t);
2✔
511
            }
512
         }
513
      }
455✔
514

515
      void record_result(const std::unique_ptr<Timer>& t) { record_result(*t); }
16✔
516

517
      template <typename T>
518
      using bench_fn = std::function<void(T&, std::string, std::chrono::milliseconds, const std::vector<size_t>&)>;
519

520
      template <typename T>
521
      void bench_providers_of(const std::string& algo,
7✔
522
                              const std::string& provider, /* user request, if any */
523
                              const std::chrono::milliseconds runtime,
524
                              const std::vector<size_t>& buf_sizes,
525
                              bench_fn<T> bench_one) {
526
         for(const auto& prov : T::providers(algo)) {
14✔
527
            if(provider.empty() || provider == prov) {
7✔
528
               auto p = T::create(algo, prov);
7✔
529

530
               if(p) {
7✔
531
                  bench_one(*p, prov, runtime, buf_sizes);
14✔
532
               }
533
            }
7✔
534
         }
535
      }
7✔
536

537
      std::unique_ptr<Timer> make_timer(const std::string& name,
475✔
538
                                        uint64_t event_mult = 1,
539
                                        const std::string& what = "",
540
                                        const std::string& provider = "",
541
                                        size_t buf_size = 0) {
542
         return std::make_unique<Timer>(name, provider, what, event_mult, buf_size, m_clock_cycle_ratio, m_clock_speed);
470✔
543
      }
544

545
      std::unique_ptr<Timer> make_timer(const std::string& algo, const std::string& provider, const std::string& what) {
5✔
546
         return make_timer(algo, 1, what, provider, 0);
5✔
547
      }
548

549
#if defined(BOTAN_HAS_BLOCK_CIPHER)
550
      void bench_block_cipher(Botan::BlockCipher& cipher,
4✔
551
                              const std::string& provider,
552
                              std::chrono::milliseconds runtime,
553
                              const std::vector<size_t>& buf_sizes) {
554
         auto ks_timer = make_timer(cipher.name(), provider, "key schedule");
8✔
555

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

559
         const size_t bs = cipher.block_size();
4✔
560
         std::set<size_t> buf_sizes_in_blocks;
4✔
561
         for(size_t buf_size : buf_sizes) {
9✔
562
            if(buf_size % bs == 0) {
5✔
563
               buf_sizes_in_blocks.insert(buf_size);
5✔
564
            } else {
565
               buf_sizes_in_blocks.insert(buf_size + bs - (buf_size % bs));
×
566
            }
567
         }
568

569
         for(size_t buf_size : buf_sizes_in_blocks) {
9✔
570
            std::vector<uint8_t> buffer(buf_size);
5✔
571
            const size_t mult = std::max<size_t>(1, 65536 / buf_size);
5✔
572
            const size_t blocks = buf_size / bs;
5✔
573

574
            auto encrypt_timer = make_timer(cipher.name(), mult * buffer.size(), "encrypt", provider, buf_size);
10✔
575
            auto decrypt_timer = make_timer(cipher.name(), mult * buffer.size(), "decrypt", provider, buf_size);
10✔
576

577
            encrypt_timer->run_until_elapsed(runtime, [&]() {
5✔
578
               for(size_t i = 0; i != mult; ++i) {
15,953✔
579
                  cipher.encrypt_n(&buffer[0], &buffer[0], blocks);
15,872✔
580
               }
581
            });
81✔
582
            record_result(encrypt_timer);
5✔
583

584
            decrypt_timer->run_until_elapsed(runtime, [&]() {
5✔
585
               for(size_t i = 0; i != mult; ++i) {
16,913✔
586
                  cipher.decrypt_n(&buffer[0], &buffer[0], blocks);
16,832✔
587
               }
588
            });
81✔
589
            record_result(decrypt_timer);
5✔
590
         }
10✔
591
      }
8✔
592
#endif
593

594
#if defined(BOTAN_HAS_STREAM_CIPHER)
595
      void bench_stream_cipher(Botan::StreamCipher& cipher,
1✔
596
                               const std::string& provider,
597
                               const std::chrono::milliseconds runtime,
598
                               const std::vector<size_t>& buf_sizes) {
599
         for(auto buf_size : buf_sizes) {
2✔
600
            const Botan::SymmetricKey key(rng(), cipher.maximum_keylength());
1✔
601
            cipher.set_key(key);
1✔
602

603
            if(cipher.valid_iv_length(12)) {
1✔
604
               const Botan::InitializationVector iv(rng(), 12);
1✔
605
               cipher.set_iv(iv.begin(), iv.size());
1✔
606
            }
1✔
607

608
            Botan::secure_vector<uint8_t> buffer = rng().random_vec(buf_size);
1✔
609

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

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

614
            encrypt_timer->run_until_elapsed(runtime, [&]() {
1✔
615
               for(size_t i = 0; i != mult; ++i) {
910✔
616
                  cipher.encipher(buffer);
896✔
617
               }
618
            });
14✔
619

620
            record_result(encrypt_timer);
1✔
621

622
            if(verbose()) {
1✔
623
               auto ks_timer = make_timer(cipher.name(), buffer.size(), "write_keystream", provider, buf_size);
×
624

625
               while(ks_timer->under(runtime)) {
×
626
                  ks_timer->run([&]() { cipher.write_keystream(buffer.data(), buffer.size()); });
×
627
               }
628
               record_result(ks_timer);
×
629
            }
×
630
         }
3✔
631
      }
1✔
632
#endif
633

634
#if defined(BOTAN_HAS_HASH)
635
      void bench_hash(Botan::HashFunction& hash,
1✔
636
                      const std::string& provider,
637
                      const std::chrono::milliseconds runtime,
638
                      const std::vector<size_t>& buf_sizes) {
639
         std::vector<uint8_t> output(hash.output_length());
1✔
640

641
         for(auto buf_size : buf_sizes) {
2✔
642
            Botan::secure_vector<uint8_t> buffer = rng().random_vec(buf_size);
1✔
643

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

646
            auto timer = make_timer(hash.name(), mult * buffer.size(), "hash", provider, buf_size);
2✔
647
            timer->run_until_elapsed(runtime, [&]() {
1✔
648
               for(size_t i = 0; i != mult; ++i) {
260✔
649
                  hash.update(buffer);
256✔
650
                  hash.final(output.data());
512✔
651
               }
652
            });
4✔
653
            record_result(timer);
1✔
654
         }
2✔
655
      }
1✔
656
#endif
657

658
#if defined(BOTAN_HAS_XOF)
659
      void bench_xof(Botan::XOF& xof,
×
660
                     const std::string& provider,
661
                     const std::chrono::milliseconds runtime,
662
                     const std::vector<size_t>& buf_sizes) {
663
         for(auto buf_size : buf_sizes) {
×
664
            Botan::secure_vector<uint8_t> in = rng().random_vec(buf_size);
×
665
            Botan::secure_vector<uint8_t> out(buf_size);
×
666

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

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

673
            record_result(in_timer);
×
674
            record_result(out_timer);
×
675
         }
×
676
      }
×
677
#endif
678

679
#if defined(BOTAN_HAS_MAC)
680
      void bench_mac(Botan::MessageAuthenticationCode& mac,
1✔
681
                     const std::string& provider,
682
                     const std::chrono::milliseconds runtime,
683
                     const std::vector<size_t>& buf_sizes) {
684
         std::vector<uint8_t> output(mac.output_length());
1✔
685

686
         for(auto buf_size : buf_sizes) {
2✔
687
            Botan::secure_vector<uint8_t> buffer = rng().random_vec(buf_size);
1✔
688
            const size_t mult = std::max<size_t>(1, 65536 / buf_size);
1✔
689

690
            const Botan::SymmetricKey key(rng(), mac.maximum_keylength());
1✔
691
            mac.set_key(key);
1✔
692

693
            auto timer = make_timer(mac.name(), mult * buffer.size(), "mac", provider, buf_size);
2✔
694
            timer->run_until_elapsed(runtime, [&]() {
1✔
695
               for(size_t i = 0; i != mult; ++i) {
195✔
696
                  if(mac.fresh_key_required_per_message()) {
192✔
697
                     mac.set_key(key);
×
698
                  }
699
                  mac.start(nullptr, 0);
192✔
700
                  mac.update(buffer);
192✔
701
                  mac.final(output.data());
384✔
702
               }
703
            });
3✔
704

705
            record_result(timer);
1✔
706
         }
3✔
707
      }
1✔
708
#endif
709

710
#if defined(BOTAN_HAS_CIPHER_MODES)
711
      void bench_cipher_mode(Botan::Cipher_Mode& enc,
1✔
712
                             Botan::Cipher_Mode& dec,
713
                             const std::chrono::milliseconds runtime,
714
                             const std::vector<size_t>& buf_sizes) {
715
         auto ks_timer = make_timer(enc.name(), enc.provider(), "key schedule");
2✔
716

717
         const Botan::SymmetricKey key(rng(), enc.key_spec().maximum_keylength());
1✔
718

719
         ks_timer->run([&]() { enc.set_key(key); });
2✔
720
         ks_timer->run([&]() { dec.set_key(key); });
2✔
721

722
         record_result(ks_timer);
1✔
723

724
         for(auto buf_size : buf_sizes) {
2✔
725
            Botan::secure_vector<uint8_t> buffer = rng().random_vec(buf_size);
1✔
726
            const size_t mult = std::max<size_t>(1, 65536 / buf_size);
1✔
727

728
            auto encrypt_timer = make_timer(enc.name(), mult * buffer.size(), "encrypt", enc.provider(), buf_size);
2✔
729
            auto decrypt_timer = make_timer(dec.name(), mult * buffer.size(), "decrypt", dec.provider(), buf_size);
2✔
730

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

733
            if(buf_size >= enc.minimum_final_size()) {
1✔
734
               encrypt_timer->run_until_elapsed(runtime, [&]() {
2✔
735
                  for(size_t i = 0; i != mult; ++i) {
260✔
736
                     enc.start(iv);
256✔
737
                     enc.finish(buffer);
256✔
738
                     buffer.resize(buf_size);  // remove any tag or padding
256✔
739
                  }
740
               });
4✔
741

742
               while(decrypt_timer->under(runtime)) {
5✔
743
                  if(!iv.empty()) {
4✔
744
                     iv[iv.size() - 1] += 1;
4✔
745
                  }
746

747
                  // Create a valid ciphertext/tag for decryption to run on
748
                  buffer.resize(buf_size);
4✔
749
                  enc.start(iv);
4✔
750
                  enc.finish(buffer);
4✔
751

752
                  Botan::secure_vector<uint8_t> dbuffer;
4✔
753

754
                  decrypt_timer->run([&]() {
4✔
755
                     for(size_t i = 0; i != mult; ++i) {
260✔
756
                        dbuffer = buffer;
256✔
757
                        dec.start(iv);
256✔
758
                        dec.finish(dbuffer);
256✔
759
                     }
760
                  });
4✔
761
               }
4✔
762
            }
763
            record_result(encrypt_timer);
1✔
764
            record_result(decrypt_timer);
1✔
765
         }
2✔
766
      }
1✔
767
#endif
768
};
769

770
BOTAN_REGISTER_COMMAND("speed", Speed);
31✔
771

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

© 2025 Coveralls, Inc