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

randombit / botan / 5123321399

30 May 2023 04:06PM UTC coverage: 92.213% (+0.004%) from 92.209%
5123321399

Pull #3558

github

web-flow
Merge dd72f7389 into 057bcbc35
Pull Request #3558: Add braces around all if/else statements

75602 of 81986 relevant lines covered (92.21%)

11859779.3 hits per line

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

95.22
/src/cli/speed.cpp
1
/*
2
* (C) 2009,2010,2014,2015,2017,2018 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 "../tests/test_rng.h"  // FIXME
9
#include "cli.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/entropy_src.h>
21
#include <botan/version.h>
22
#include <botan/internal/cpuid.h>
23
#include <botan/internal/os_utils.h>
24
#include <botan/internal/timer.h>
25

26
#if defined(BOTAN_HAS_BIGINT)
27
   #include <botan/bigint.h>
28
   #include <botan/internal/divide.h>
29
#endif
30

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

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

39
#if defined(BOTAN_HAS_HASH)
40
   #include <botan/hash.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_AUTO_SEEDING_RNG)
52
   #include <botan/auto_rng.h>
53
#endif
54

55
#if defined(BOTAN_HAS_SYSTEM_RNG)
56
   #include <botan/system_rng.h>
57
#endif
58

59
#if defined(BOTAN_HAS_HMAC_DRBG)
60
   #include <botan/hmac_drbg.h>
61
#endif
62

63
#if defined(BOTAN_HAS_PROCESSOR_RNG)
64
   #include <botan/processor_rng.h>
65
#endif
66

67
#if defined(BOTAN_HAS_CHACHA_RNG)
68
   #include <botan/chacha_rng.h>
69
#endif
70

71
#if defined(BOTAN_HAS_FPE_FE1)
72
   #include <botan/fpe_fe1.h>
73
#endif
74

75
#if defined(BOTAN_HAS_RFC3394_KEYWRAP)
76
   #include <botan/rfc3394.h>
77
#endif
78

79
#if defined(BOTAN_HAS_COMPRESSION)
80
   #include <botan/compression.h>
81
#endif
82

83
#if defined(BOTAN_HAS_POLY_DBL)
84
   #include <botan/internal/poly_dbl.h>
85
#endif
86

87
#if defined(BOTAN_HAS_PUBLIC_KEY_CRYPTO)
88
   #include <botan/pk_algs.h>
89
   #include <botan/pkcs8.h>
90
   #include <botan/pubkey.h>
91
   #include <botan/x509_key.h>
92
   #include <botan/internal/workfactor.h>
93
#endif
94

95
#if defined(BOTAN_HAS_NUMBERTHEORY)
96
   #include <botan/numthry.h>
97
   #include <botan/reducer.h>
98
   #include <botan/internal/curve_nistp.h>
99
   #include <botan/internal/primality.h>
100
#endif
101

102
#if defined(BOTAN_HAS_ECC_GROUP)
103
   #include <botan/ec_group.h>
104
#endif
105

106
#if defined(BOTAN_HAS_DL_GROUP)
107
   #include <botan/dl_group.h>
108
#endif
109

110
#if defined(BOTAN_HAS_MCELIECE)
111
   #include <botan/mceliece.h>
112
#endif
113

114
#if defined(BOTAN_HAS_KYBER) || defined(BOTAN_HAS_KYBER_90S)
115
   #include <botan/kyber.h>
116
#endif
117

118
#if defined(BOTAN_HAS_DILITHIUM) || defined(BOTAN_HAS_DILITHIUM_AES)
119
   #include <botan/dilithium.h>
120
#endif
121

122
#if defined(BOTAN_HAS_ECDSA)
123
   #include <botan/ecdsa.h>
124
#endif
125

126
#if defined(BOTAN_HAS_BCRYPT)
127
   #include <botan/bcrypt.h>
128
#endif
129

130
#if defined(BOTAN_HAS_PASSHASH9)
131
   #include <botan/passhash9.h>
132
#endif
133

134
#if defined(BOTAN_HAS_PASSWORD_HASHING)
135
   #include <botan/pwdhash.h>
136
#endif
137

138
#if defined(BOTAN_HAS_ZFEC)
139
   #include <botan/zfec.h>
140
#endif
141

142
namespace Botan_CLI {
143

144
using Botan::Timer;
145

146
namespace {
147

148
class JSON_Output final {
2✔
149
   public:
150
      void add(const Timer& timer) { m_results.push_back(timer); }
2✔
151

152
      std::string print() const {
1✔
153
         std::ostringstream out;
1✔
154

155
         out << "[\n";
1✔
156

157
         for(size_t i = 0; i != m_results.size(); ++i) {
3✔
158
            const Timer& t = m_results[i];
2✔
159

160
            out << "{"
2✔
161
                << "\"algo\": \"" << t.get_name() << "\", "
2✔
162
                << "\"op\": \"" << t.doing() << "\", "
4✔
163
                << "\"events\": " << t.events() << ", ";
6✔
164

165
            if(t.cycles_consumed() > 0) {
4✔
166
               out << "\"cycles\": " << t.cycles_consumed() << ", ";
4✔
167
            }
168

169
            if(t.buf_size() > 0) {
2✔
170
               out << "\"bps\": " << static_cast<uint64_t>(t.events() / (t.value() / 1000000000.0)) << ", ";
2✔
171
               out << "\"buf_size\": " << t.buf_size() << ", ";
2✔
172
            }
173

174
            out << "\"nanos\": " << t.value() << "}";
2✔
175

176
            if(i != m_results.size() - 1) {
2✔
177
               out << ",";
1✔
178
            }
179

180
            out << "\n";
2✔
181
         }
182
         out << "]\n";
1✔
183

184
         return out.str();
2✔
185
      }
1✔
186

187
   private:
188
      std::vector<Timer> m_results;
189
};
190

191
class Summary final {
1✔
192
   public:
193
      Summary() = default;
1✔
194

195
      void add(const Timer& t) {
2✔
196
         if(t.buf_size() == 0) {
2✔
197
            m_ops_entries.push_back(t);
×
198
         } else {
199
            m_bps_entries[std::make_pair(t.doing(), t.get_name())].push_back(t);
4✔
200
         }
201
      }
2✔
202

203
      std::string print() {
1✔
204
         const size_t name_padding = 35;
1✔
205
         const size_t op_name_padding = 16;
1✔
206
         const size_t op_padding = 16;
1✔
207

208
         std::ostringstream result_ss;
1✔
209
         result_ss << std::fixed;
1✔
210

211
         if(!m_bps_entries.empty()) {
1✔
212
            result_ss << "\n";
1✔
213

214
            // add table header
215
            result_ss << std::setw(name_padding) << std::left << "algo" << std::setw(op_name_padding) << std::left
1✔
216
                      << "operation";
1✔
217

218
            for(const Timer& t : m_bps_entries.begin()->second) {
2✔
219
               result_ss << std::setw(op_padding) << std::right << (std::to_string(t.buf_size()) + " bytes");
2✔
220
            }
221
            result_ss << "\n";
1✔
222

223
            // add table entries
224
            for(const auto& entry : m_bps_entries) {
3✔
225
               if(entry.second.empty()) {
2✔
226
                  continue;
×
227
               }
228

229
               result_ss << std::setw(name_padding) << std::left << (entry.first.second) << std::setw(op_name_padding)
2✔
230
                         << std::left << (entry.first.first);
2✔
231

232
               for(const Timer& t : entry.second) {
4✔
233
                  if(t.events() == 0) {
2✔
234
                     result_ss << std::setw(op_padding) << std::right << "N/A";
×
235
                  } else {
236
                     result_ss << std::setw(op_padding) << std::right << std::setprecision(2)
2✔
237
                               << (t.bytes_per_second() / 1000.0);
2✔
238
                  }
239
               }
240

241
               result_ss << "\n";
2✔
242
            }
243

244
            result_ss << "\n[results are the number of 1000s bytes processed per second]\n";
1✔
245
         }
246

247
         if(!m_ops_entries.empty()) {
1✔
248
            result_ss << std::setprecision(6) << "\n";
×
249

250
            // sort entries
251
            std::sort(m_ops_entries.begin(), m_ops_entries.end());
×
252

253
            // add table header
254
            result_ss << std::setw(name_padding) << std::left << "algo" << std::setw(op_name_padding) << std::left
×
255
                      << "operation" << std::setw(op_padding) << std::right << "sec/op" << std::setw(op_padding)
×
256
                      << std::right << "op/sec"
×
257
                      << "\n";
×
258

259
            // add table entries
260
            for(const Timer& entry : m_ops_entries) {
×
261
               result_ss << std::setw(name_padding) << std::left << entry.get_name() << std::setw(op_name_padding)
×
262
                         << std::left << entry.doing() << std::setw(op_padding) << std::right
×
263
                         << entry.seconds_per_event() << std::setw(op_padding) << std::right
×
264
                         << entry.events_per_second() << "\n";
×
265
            }
266
         }
267

268
         return result_ss.str();
2✔
269
      }
1✔
270

271
   private:
272
      std::map<std::pair<std::string, std::string>, std::vector<Timer>> m_bps_entries;
273
      std::vector<Timer> m_ops_entries;
274
};
275

276
std::vector<size_t> unique_buffer_sizes(const std::string& cmdline_arg) {
34✔
277
   const size_t MAX_BUF_SIZE = 64 * 1024 * 1024;
34✔
278

279
   std::set<size_t> buf;
34✔
280
   for(const std::string& size_str : Command::split_on(cmdline_arg, ',')) {
66✔
281
      size_t x = 0;
35✔
282
      try {
35✔
283
         size_t converted = 0;
35✔
284
         x = static_cast<size_t>(std::stoul(size_str, &converted, 0));
35✔
285

286
         if(converted != size_str.size()) {
34✔
287
            throw CLI_Usage_Error("Invalid integer");
×
288
         }
289
      } catch(std::exception&) {
1✔
290
         throw CLI_Usage_Error("Invalid integer value '" + size_str + "' for option buf-size");
3✔
291
      }
1✔
292

293
      if(x == 0) {
34✔
294
         throw CLI_Usage_Error("Cannot have a zero-sized buffer");
3✔
295
      }
296

297
      if(x > MAX_BUF_SIZE) {
33✔
298
         throw CLI_Usage_Error("Specified buffer size is too large");
3✔
299
      }
300

301
      buf.insert(x);
32✔
302
   }
34✔
303

304
   return std::vector<size_t>(buf.begin(), buf.end());
34✔
305
}
31✔
306

307
}  // namespace
308

309
class Speed final : public Command {
×
310
   public:
311
      Speed() :
35✔
312
            Command(
313
               "speed --msec=500 --format=default --ecc-groups= --provider= --buf-size=1024 --clear-cpuid= --cpu-clock-speed=0 --cpu-clock-ratio=1.0 *algos") {
70✔
314
      }
35✔
315

316
      static std::vector<std::string> default_benchmark_list() {
×
317
         /*
318
         This is not intended to be exhaustive: it just hits the high
319
         points of the most interesting or widely used algorithms.
320
         */
321

322
         return {
×
323
            /* Block ciphers */
324
            "AES-128",
325
            "AES-192",
326
            "AES-256",
327
            "ARIA-128",
328
            "ARIA-192",
329
            "ARIA-256",
330
            "Blowfish",
331
            "CAST-128",
332
            "Camellia-128",
333
            "Camellia-192",
334
            "Camellia-256",
335
            "DES",
336
            "TripleDES",
337
            "GOST-28147-89",
338
            "IDEA",
339
            "Noekeon",
340
            "SHACAL2",
341
            "SM4",
342
            "Serpent",
343
            "Threefish-512",
344
            "Twofish",
345

346
            /* Cipher modes */
347
            "AES-128/CBC",
348
            "AES-128/CTR-BE",
349
            "AES-128/EAX",
350
            "AES-128/OCB",
351
            "AES-128/GCM",
352
            "AES-128/XTS",
353
            "AES-128/SIV",
354

355
            "Serpent/CBC",
356
            "Serpent/CTR-BE",
357
            "Serpent/EAX",
358
            "Serpent/OCB",
359
            "Serpent/GCM",
360
            "Serpent/XTS",
361
            "Serpent/SIV",
362

363
            "ChaCha20Poly1305",
364

365
            /* Stream ciphers */
366
            "RC4",
367
            "Salsa20",
368
            "ChaCha20",
369

370
            /* Hashes */
371
            "SHA-1",
372
            "SHA-256",
373
            "SHA-512",
374
            "SHA-3(256)",
375
            "SHA-3(512)",
376
            "RIPEMD-160",
377
            "Skein-512",
378
            "Blake2b",
379
            "Whirlpool",
380

381
            /* MACs */
382
            "CMAC(AES-128)",
383
            "HMAC(SHA-256)",
384

385
            /* pubkey */
386
            "RSA",
387
            "DH",
388
            "ECDH",
389
            "ECDSA",
390
            "Ed25519",
391
            "Curve25519",
392
            "McEliece",
393
            "Kyber",
394
         };
×
395
      }
396

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

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

401
      void go() override {
34✔
402
         std::chrono::milliseconds msec(get_arg_sz("msec"));
34✔
403
         const std::string provider = get_arg("provider");
34✔
404
         std::vector<std::string> ecc_groups = Command::split_on(get_arg("ecc-groups"), ',');
71✔
405
         const std::string format = get_arg("format");
34✔
406
         const std::string clock_ratio = get_arg("cpu-clock-ratio");
37✔
407
         m_clock_speed = get_arg_sz("cpu-clock-speed");
34✔
408

409
         m_clock_cycle_ratio = std::strtod(clock_ratio.c_str(), nullptr);
34✔
410

411
         /*
412
         * This argument is intended to be the ratio between the cycle counter
413
         * and the actual machine cycles. It is extremely unlikely that there is
414
         * any machine where the cycle counter increments faster than the actual
415
         * clock.
416
         */
417
         if(m_clock_cycle_ratio < 0.0 || m_clock_cycle_ratio > 1.0) {
34✔
418
            throw CLI_Usage_Error("Unlikely CPU clock ratio of " + clock_ratio);
×
419
         }
420

421
         m_clock_cycle_ratio = 1.0 / m_clock_cycle_ratio;
34✔
422

423
         if(m_clock_speed != 0 && Botan::OS::get_cpu_cycle_counter() != 0) {
34✔
424
            error_output() << "The --cpu-clock-speed option is only intended to be used on "
×
425
                              "platforms without access to a cycle counter.\n"
426
                              "Expected incorrect results\n\n";
×
427
         }
428

429
         if(format == "table") {
34✔
430
            m_summary = std::make_unique<Summary>();
1✔
431
         } else if(format == "json") {
33✔
432
            m_json = std::make_unique<JSON_Output>();
1✔
433
         } else if(format != "default") {
32✔
434
            throw CLI_Usage_Error("Unknown --format type '" + format + "'");
×
435
         }
436

437
#if defined(BOTAN_HAS_ECC_GROUP)
438
         if(ecc_groups.empty()) {
34✔
439
            ecc_groups = {"secp256r1", "brainpool256r1", "secp384r1", "brainpool384r1", "secp521r1", "brainpool512r1"};
272✔
440
         } else if(ecc_groups.size() == 1 && ecc_groups[0] == "all") {
×
441
            auto all = Botan::EC_Group::known_named_groups();
×
442
            ecc_groups.assign(all.begin(), all.end());
×
443
         }
×
444
#endif
445

446
         std::vector<std::string> algos = get_arg_list("algos");
37✔
447

448
         const std::vector<size_t> buf_sizes = unique_buffer_sizes(get_arg("buf-size"));
68✔
449

450
         for(const std::string& cpuid_to_clear : Command::split_on(get_arg("clear-cpuid"), ',')) {
32✔
451
            auto bits = Botan::CPUID::bit_from_string(cpuid_to_clear);
1✔
452
            if(bits.empty()) {
1✔
453
               error_output() << "Warning don't know CPUID flag '" << cpuid_to_clear << "'\n";
1✔
454
            }
455

456
            for(auto bit : bits) {
1✔
457
               Botan::CPUID::clear_cpuid_bit(bit);
×
458
            }
459
         }
32✔
460

461
         if(verbose() || m_summary) {
31✔
462
            output() << Botan::version_string() << "\n"
2✔
463
                     << "CPUID: " << Botan::CPUID::to_string() << "\n\n";
3✔
464
         }
465

466
         const bool using_defaults = (algos.empty());
31✔
467
         if(using_defaults) {
31✔
468
            algos = default_benchmark_list();
×
469
         }
470

471
         for(const auto& algo : algos) {
81✔
472
            using namespace std::placeholders;
50✔
473

474
            if(false) {
50✔
475
               // Since everything might be disabled, need a block to else if from
476
            }
477
#if defined(BOTAN_HAS_HASH)
478
            else if(!Botan::HashFunction::providers(algo).empty()) {
50✔
479
               bench_providers_of<Botan::HashFunction>(
1✔
480
                  algo, provider, msec, buf_sizes, std::bind(&Speed::bench_hash, this, _1, _2, _3, _4));
2✔
481
            }
482
#endif
483
#if defined(BOTAN_HAS_BLOCK_CIPHER)
484
            else if(!Botan::BlockCipher::providers(algo).empty()) {
49✔
485
               bench_providers_of<Botan::BlockCipher>(
4✔
486
                  algo, provider, msec, buf_sizes, std::bind(&Speed::bench_block_cipher, this, _1, _2, _3, _4));
8✔
487
            }
488
#endif
489
#if defined(BOTAN_HAS_STREAM_CIPHER)
490
            else if(!Botan::StreamCipher::providers(algo).empty()) {
45✔
491
               bench_providers_of<Botan::StreamCipher>(
1✔
492
                  algo, provider, msec, buf_sizes, std::bind(&Speed::bench_stream_cipher, this, _1, _2, _3, _4));
2✔
493
            }
494
#endif
495
#if defined(BOTAN_HAS_CIPHER_MODES)
496
            else if(auto enc = Botan::Cipher_Mode::create(algo, Botan::Cipher_Dir::Encryption, provider)) {
44✔
497
               auto dec = Botan::Cipher_Mode::create_or_throw(algo, Botan::Cipher_Dir::Decryption, provider);
1✔
498
               bench_cipher_mode(*enc, *dec, msec, buf_sizes);
1✔
499
            }
1✔
500
#endif
501
#if defined(BOTAN_HAS_MAC)
502
            else if(!Botan::MessageAuthenticationCode::providers(algo).empty()) {
43✔
503
               bench_providers_of<Botan::MessageAuthenticationCode>(
1✔
504
                  algo, provider, msec, buf_sizes, std::bind(&Speed::bench_mac, this, _1, _2, _3, _4));
2✔
505
            }
506
#endif
507
#if defined(BOTAN_HAS_RSA)
508
            else if(algo == "RSA") {
42✔
509
               bench_rsa(provider, msec);
1✔
510
            } else if(algo == "RSA_keygen") {
41✔
511
               bench_rsa_keygen(provider, msec);
1✔
512
            }
513
#endif
514
#if defined(BOTAN_HAS_ECDSA)
515
            else if(algo == "ECDSA") {
40✔
516
               bench_ecdsa(ecc_groups, provider, msec);
1✔
517
            } else if(algo == "ecdsa_recovery") {
39✔
518
               bench_ecdsa_recovery(ecc_groups, provider, msec);
1✔
519
            }
520
#endif
521
#if defined(BOTAN_HAS_SM2)
522
            else if(algo == "SM2") {
38✔
523
               bench_sm2(ecc_groups, provider, msec);
1✔
524
            }
525
#endif
526
#if defined(BOTAN_HAS_ECKCDSA)
527
            else if(algo == "ECKCDSA") {
37✔
528
               bench_eckcdsa(ecc_groups, provider, msec);
1✔
529
            }
530
#endif
531
#if defined(BOTAN_HAS_GOST_34_10_2001)
532
            else if(algo == "GOST-34.10") {
36✔
533
               bench_gost_3410(provider, msec);
1✔
534
            }
535
#endif
536
#if defined(BOTAN_HAS_ECGDSA)
537
            else if(algo == "ECGDSA") {
35✔
538
               bench_ecgdsa(ecc_groups, provider, msec);
1✔
539
            }
540
#endif
541
#if defined(BOTAN_HAS_ED25519)
542
            else if(algo == "Ed25519") {
34✔
543
               bench_ed25519(provider, msec);
1✔
544
            }
545
#endif
546
#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
547
            else if(algo == "DH") {
33✔
548
               bench_dh(provider, msec);
1✔
549
            }
550
#endif
551
#if defined(BOTAN_HAS_DSA)
552
            else if(algo == "DSA") {
32✔
553
               bench_dsa(provider, msec);
1✔
554
            }
555
#endif
556
#if defined(BOTAN_HAS_ELGAMAL)
557
            else if(algo == "ElGamal") {
31✔
558
               bench_elgamal(provider, msec);
1✔
559
            }
560
#endif
561
#if defined(BOTAN_HAS_ECDH)
562
            else if(algo == "ECDH") {
30✔
563
               bench_ecdh(ecc_groups, provider, msec);
1✔
564
            }
565
#endif
566
#if defined(BOTAN_HAS_CURVE_25519)
567
            else if(algo == "Curve25519") {
29✔
568
               bench_curve25519(provider, msec);
1✔
569
            }
570
#endif
571
#if defined(BOTAN_HAS_MCELIECE)
572
            else if(algo == "McEliece") {
28✔
573
               bench_mceliece(provider, msec);
1✔
574
            }
575
#endif
576
#if defined(BOTAN_HAS_KYBER) || defined(BOTAN_HAS_KYBER_90S)
577
            else if(algo == "Kyber") {
27✔
578
               bench_kyber(provider, msec);
1✔
579
            }
580
#endif
581
#if defined(BOTAN_HAS_DILITHIUM) || defined(BOTAN_HAS_DILITHIUM_AES)
582
            else if(algo == "Dilithium") {
26✔
583
               bench_dilithium(provider, msec);
1✔
584
            }
585
#endif
586
#if defined(BOTAN_HAS_XMSS_RFC8391)
587
            else if(algo == "XMSS") {
25✔
588
               bench_xmss(provider, msec);
1✔
589
            }
590
#endif
591
#if defined(BOTAN_HAS_SCRYPT)
592
            else if(algo == "scrypt") {
24✔
593
               bench_scrypt(provider, msec);
1✔
594
            }
595
#endif
596
#if defined(BOTAN_HAS_ARGON2)
597
            else if(algo == "argon2") {
23✔
598
               bench_argon2(provider, msec);
1✔
599
            }
600
#endif
601
#if defined(BOTAN_HAS_BCRYPT)
602
            else if(algo == "bcrypt") {
22✔
603
               bench_bcrypt();
1✔
604
            }
605
#endif
606
#if defined(BOTAN_HAS_PASSHASH9)
607
            else if(algo == "passhash9") {
21✔
608
               bench_passhash9();
1✔
609
            }
610
#endif
611
#if defined(BOTAN_HAS_ZFEC)
612
            else if(algo == "zfec") {
20✔
613
               bench_zfec(msec);
1✔
614
            }
615
#endif
616
#if defined(BOTAN_HAS_POLY_DBL)
617
            else if(algo == "poly_dbl") {
19✔
618
               bench_poly_dbl(msec);
1✔
619
            }
620
#endif
621

622
#if defined(BOTAN_HAS_DL_GROUP)
623
            else if(algo == "modexp") {
18✔
624
               bench_modexp(msec);
1✔
625
            }
626
#endif
627

628
#if defined(BOTAN_HAS_BIGINT)
629
            else if(algo == "mp_mul") {
17✔
630
               bench_mp_mul(msec);
1✔
631
            } else if(algo == "mp_div") {
16✔
632
               bench_mp_div(msec);
1✔
633
            } else if(algo == "mp_div10") {
15✔
634
               bench_mp_div10(msec);
1✔
635
            }
636
#endif
637

638
#if defined(BOTAN_HAS_NUMBERTHEORY)
639
            else if(algo == "primality_test") {
14✔
640
               bench_primality_tests(msec);
1✔
641
            } else if(algo == "random_prime") {
13✔
642
               bench_random_prime(msec);
1✔
643
            } else if(algo == "inverse_mod") {
12✔
644
               bench_inverse_mod(msec);
1✔
645
            } else if(algo == "bn_redc") {
11✔
646
               bench_bn_redc(msec);
1✔
647
            } else if(algo == "nistp_redc") {
10✔
648
               bench_nistp_redc(msec);
1✔
649
            }
650
#endif
651

652
#if defined(BOTAN_HAS_FPE_FE1)
653
            else if(algo == "fpe_fe1") {
9✔
654
               bench_fpe_fe1(msec);
1✔
655
            }
656
#endif
657

658
#if defined(BOTAN_HAS_RFC3394_KEYWRAP)
659
            else if(algo == "rfc3394") {
8✔
660
               bench_rfc3394(msec);
1✔
661
            }
662
#endif
663

664
#if defined(BOTAN_HAS_ECC_GROUP)
665
            else if(algo == "ecc_mult") {
7✔
666
               bench_ecc_mult(ecc_groups, msec);
1✔
667
            } else if(algo == "ecc_ops") {
6✔
668
               bench_ecc_ops(ecc_groups, msec);
1✔
669
            } else if(algo == "ecc_init") {
5✔
670
               bench_ecc_init(ecc_groups, msec);
1✔
671
            } else if(algo == "os2ecp") {
4✔
672
               bench_os2ecp(ecc_groups, msec);
1✔
673
            }
674
#endif
675
#if defined(BOTAN_HAS_EC_HASH_TO_CURVE)
676
            else if(algo == "ec_h2c") {
3✔
677
               bench_ec_h2c(msec);
1✔
678
            }
679
#endif
680
            else if(algo == "RNG") {
2✔
681
#if defined(BOTAN_HAS_AUTO_SEEDING_RNG)
682
               Botan::AutoSeeded_RNG auto_rng;
1✔
683
               bench_rng(auto_rng, "AutoSeeded_RNG (with reseed)", msec, buf_sizes);
1✔
684
#endif
685

686
#if defined(BOTAN_HAS_SYSTEM_RNG)
687
               bench_rng(Botan::system_rng(), "System_RNG", msec, buf_sizes);
1✔
688
#endif
689

690
#if defined(BOTAN_HAS_PROCESSOR_RNG)
691
               if(Botan::Processor_RNG::available()) {
1✔
692
                  Botan::Processor_RNG hwrng;
1✔
693
                  bench_rng(hwrng, "Processor_RNG", msec, buf_sizes);
2✔
694
               }
1✔
695
#endif
696

697
#if defined(BOTAN_HAS_HMAC_DRBG)
698
               for(std::string hash : {"SHA-256", "SHA-384", "SHA-512"}) {
4✔
699
                  Botan::HMAC_DRBG hmac_drbg(hash);
3✔
700
                  bench_rng(hmac_drbg, hmac_drbg.name(), msec, buf_sizes);
3✔
701
               }
3✔
702
#endif
703

704
#if defined(BOTAN_HAS_CHACHA_RNG)
705
               // Provide a dummy seed
706
               Botan::ChaCha_RNG chacha_rng(Botan::secure_vector<uint8_t>(32));
1✔
707
               bench_rng(chacha_rng, "ChaCha_RNG", msec, buf_sizes);
1✔
708
#endif
709

710
            } else if(algo == "entropy") {
2✔
711
               bench_entropy_sources(msec);
1✔
712
            } else {
713
               if(verbose() || !using_defaults) {
×
714
                  error_output() << "Unknown algorithm '" << algo << "'\n";
×
715
               }
716
            }
44✔
717
         }
718

719
         if(m_json) {
31✔
720
            output() << m_json->print();
3✔
721
         }
722
         if(m_summary) {
31✔
723
            output() << m_summary->print() << "\n";
3✔
724
         }
725

726
         if(verbose() && m_clock_speed == 0 && m_cycles_consumed > 0 && m_ns_taken > 0) {
31✔
727
            const double seconds = static_cast<double>(m_ns_taken) / 1000000000;
×
728
            const double Hz = static_cast<double>(m_cycles_consumed) / seconds;
×
729
            const double MHz = Hz / 1000000;
×
730
            output() << "\nEstimated clock speed " << MHz << " MHz\n";
×
731
         }
732
      }
40✔
733

734
   private:
735
      size_t m_clock_speed = 0;
736
      double m_clock_cycle_ratio = 0.0;
737
      uint64_t m_cycles_consumed = 0;
738
      uint64_t m_ns_taken = 0;
739
      std::unique_ptr<Summary> m_summary;
740
      std::unique_ptr<JSON_Output> m_json;
741

742
      void record_result(const std::unique_ptr<Timer>& t) {
467✔
743
         m_ns_taken += t->value();
467✔
744
         m_cycles_consumed += t->cycles_consumed();
467✔
745
         if(m_json) {
467✔
746
            m_json->add(*t);
2✔
747
         } else {
748
            output() << t->to_string() << std::flush;
465✔
749
            if(m_summary) {
465✔
750
               m_summary->add(*t);
2✔
751
            }
752
         }
753
      }
467✔
754

755
      template <typename T>
756
      using bench_fn = std::function<void(T&, std::string, std::chrono::milliseconds, const std::vector<size_t>&)>;
757

758
      template <typename T>
759
      void bench_providers_of(const std::string& algo,
7✔
760
                              const std::string& provider, /* user request, if any */
761
                              const std::chrono::milliseconds runtime,
762
                              const std::vector<size_t>& buf_sizes,
763
                              bench_fn<T> bench_one) {
764
         for(const auto& prov : T::providers(algo)) {
14✔
765
            if(provider.empty() || provider == prov) {
7✔
766
               auto p = T::create(algo, prov);
7✔
767

768
               if(p) {
7✔
769
                  bench_one(*p, prov, runtime, buf_sizes);
14✔
770
               }
771
            }
7✔
772
         }
773
      }
7✔
774

775
      std::unique_ptr<Timer> make_timer(const std::string& name,
471✔
776
                                        uint64_t event_mult = 1,
777
                                        const std::string& what = "",
778
                                        const std::string& provider = "",
779
                                        size_t buf_size = 0) {
780
         return std::make_unique<Timer>(name, provider, what, event_mult, buf_size, m_clock_cycle_ratio, m_clock_speed);
252✔
781
      }
782

783
      std::unique_ptr<Timer> make_timer(const std::string& algo, const std::string& provider, const std::string& what) {
212✔
784
         return make_timer(algo, 1, what, provider, 0);
212✔
785
      }
786

787
#if defined(BOTAN_HAS_BLOCK_CIPHER)
788
      void bench_block_cipher(Botan::BlockCipher& cipher,
4✔
789
                              const std::string& provider,
790
                              std::chrono::milliseconds runtime,
791
                              const std::vector<size_t>& buf_sizes) {
792
         auto ks_timer = make_timer(cipher.name(), provider, "key schedule");
8✔
793

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

797
         const size_t bs = cipher.block_size();
4✔
798
         std::set<size_t> buf_sizes_in_blocks;
4✔
799
         for(size_t buf_size : buf_sizes) {
9✔
800
            if(buf_size % bs == 0) {
5✔
801
               buf_sizes_in_blocks.insert(buf_size);
10✔
802
            } else {
803
               buf_sizes_in_blocks.insert(buf_size + bs - (buf_size % bs));
×
804
            }
805
         }
806

807
         for(size_t buf_size : buf_sizes_in_blocks) {
9✔
808
            std::vector<uint8_t> buffer(buf_size);
5✔
809
            const size_t blocks = buf_size / bs;
5✔
810

811
            auto encrypt_timer = make_timer(cipher.name(), buffer.size(), "encrypt", provider, buf_size);
10✔
812
            auto decrypt_timer = make_timer(cipher.name(), buffer.size(), "decrypt", provider, buf_size);
10✔
813

814
            encrypt_timer->run_until_elapsed(runtime, [&]() { cipher.encrypt_n(&buffer[0], &buffer[0], blocks); });
2,021✔
815
            record_result(encrypt_timer);
5✔
816

817
            decrypt_timer->run_until_elapsed(runtime, [&]() { cipher.decrypt_n(&buffer[0], &buffer[0], blocks); });
2,008✔
818
            record_result(decrypt_timer);
5✔
819
         }
10✔
820
      }
8✔
821
#endif
822

823
#if defined(BOTAN_HAS_STREAM_CIPHER)
824
      void bench_stream_cipher(Botan::StreamCipher& cipher,
1✔
825
                               const std::string& provider,
826
                               const std::chrono::milliseconds runtime,
827
                               const std::vector<size_t>& buf_sizes) {
828
         for(auto buf_size : buf_sizes) {
2✔
829
            Botan::secure_vector<uint8_t> buffer = rng().random_vec(buf_size);
1✔
830

831
            auto encrypt_timer = make_timer(cipher.name(), buffer.size(), "encrypt", provider, buf_size);
2✔
832

833
            const Botan::SymmetricKey key(rng(), cipher.maximum_keylength());
1✔
834
            cipher.set_key(key);
1✔
835

836
            if(cipher.valid_iv_length(12)) {
1✔
837
               const Botan::InitializationVector iv(rng(), 12);
1✔
838
               cipher.set_iv(iv.begin(), iv.size());
1✔
839
            }
1✔
840

841
            while(encrypt_timer->under(runtime)) {
587✔
842
               encrypt_timer->run([&]() { cipher.encipher(buffer); });
1,172✔
843
            }
844

845
            record_result(encrypt_timer);
1✔
846

847
            if(verbose()) {
1✔
848
               auto ks_timer = make_timer(cipher.name(), buffer.size(), "write_keystream", provider, buf_size);
×
849

850
               while(ks_timer->under(runtime)) {
×
851
                  ks_timer->run([&]() { cipher.write_keystream(buffer.data(), buffer.size()); });
×
852
               }
853
               record_result(ks_timer);
×
854
            }
×
855
         }
2✔
856
      }
1✔
857
#endif
858

859
#if defined(BOTAN_HAS_HASH)
860
      void bench_hash(Botan::HashFunction& hash,
1✔
861
                      const std::string& provider,
862
                      const std::chrono::milliseconds runtime,
863
                      const std::vector<size_t>& buf_sizes) {
864
         std::vector<uint8_t> output(hash.output_length());
1✔
865

866
         for(auto buf_size : buf_sizes) {
2✔
867
            Botan::secure_vector<uint8_t> buffer = rng().random_vec(buf_size);
1✔
868

869
            auto timer = make_timer(hash.name(), buffer.size(), "hash", provider, buf_size);
2✔
870
            timer->run_until_elapsed(runtime, [&]() {
1✔
871
               hash.update(buffer);
109✔
872
               hash.final(output.data());
109✔
873
            });
109✔
874
            record_result(timer);
1✔
875
         }
2✔
876
      }
1✔
877
#endif
878

879
#if defined(BOTAN_HAS_MAC)
880
      void bench_mac(Botan::MessageAuthenticationCode& mac,
1✔
881
                     const std::string& provider,
882
                     const std::chrono::milliseconds runtime,
883
                     const std::vector<size_t>& buf_sizes) {
884
         std::vector<uint8_t> output(mac.output_length());
1✔
885

886
         for(auto buf_size : buf_sizes) {
2✔
887
            Botan::secure_vector<uint8_t> buffer = rng().random_vec(buf_size);
1✔
888

889
            const Botan::SymmetricKey key(rng(), mac.maximum_keylength());
1✔
890
            mac.set_key(key);
1✔
891
            mac.start(nullptr, 0);
1✔
892

893
            auto timer = make_timer(mac.name(), buffer.size(), "mac", provider, buf_size);
2✔
894
            timer->run_until_elapsed(runtime, [&]() { mac.update(buffer); });
120✔
895
            timer->run([&]() { mac.final(output.data()); });
2✔
896
            record_result(timer);
1✔
897
         }
3✔
898
      }
1✔
899
#endif
900

901
#if defined(BOTAN_HAS_CIPHER_MODES)
902
      void bench_cipher_mode(Botan::Cipher_Mode& enc,
1✔
903
                             Botan::Cipher_Mode& dec,
904
                             const std::chrono::milliseconds runtime,
905
                             const std::vector<size_t>& buf_sizes) {
906
         auto ks_timer = make_timer(enc.name(), enc.provider(), "key schedule");
2✔
907

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

910
         ks_timer->run([&]() { enc.set_key(key); });
2✔
911
         ks_timer->run([&]() { dec.set_key(key); });
2✔
912

913
         record_result(ks_timer);
1✔
914

915
         for(auto buf_size : buf_sizes) {
2✔
916
            Botan::secure_vector<uint8_t> buffer = rng().random_vec(buf_size);
1✔
917

918
            auto encrypt_timer = make_timer(enc.name(), buffer.size(), "encrypt", enc.provider(), buf_size);
2✔
919
            auto decrypt_timer = make_timer(dec.name(), buffer.size(), "decrypt", dec.provider(), buf_size);
2✔
920

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

923
            if(buf_size >= enc.minimum_final_size()) {
1✔
924
               while(encrypt_timer->under(runtime) && decrypt_timer->under(runtime)) {
64✔
925
                  // Must run in this order, or AEADs will reject the ciphertext
926
                  encrypt_timer->run([&]() {
63✔
927
                     enc.start(iv);
63✔
928
                     enc.finish(buffer);
63✔
929
                  });
63✔
930

931
                  decrypt_timer->run([&]() {
63✔
932
                     dec.start(iv);
63✔
933
                     dec.finish(buffer);
63✔
934
                  });
63✔
935

936
                  if(!iv.empty()) {
63✔
937
                     iv[iv.size() - 1] += 1;
63✔
938
                  }
939
               }
940
            }
941

942
            record_result(encrypt_timer);
1✔
943
            record_result(decrypt_timer);
1✔
944
         }
2✔
945
      }
1✔
946
#endif
947

948
      void bench_rng(Botan::RandomNumberGenerator& rng,
7✔
949
                     const std::string& rng_name,
950
                     const std::chrono::milliseconds runtime,
951
                     const std::vector<size_t>& buf_sizes) {
952
         for(auto buf_size : buf_sizes) {
14✔
953
            Botan::secure_vector<uint8_t> buffer(buf_size);
7✔
954

955
#if defined(BOTAN_HAS_SYSTEM_RNG)
956
            rng.reseed_from_rng(Botan::system_rng(), 256);
7✔
957
#endif
958

959
            auto timer = make_timer(rng_name, buffer.size(), "generate", "", buf_size);
7✔
960
            timer->run_until_elapsed(runtime, [&]() { rng.randomize(buffer.data(), buffer.size()); });
735✔
961
            record_result(timer);
7✔
962
         }
14✔
963
      }
7✔
964

965
      void bench_entropy_sources(const std::chrono::milliseconds /*unused*/) {
1✔
966
         Botan::Entropy_Sources& srcs = Botan::Entropy_Sources::global_sources();
1✔
967

968
         for(auto src : srcs.enabled_sources()) {
5✔
969
            size_t entropy_bits = 0;
4✔
970
            Botan_Tests::SeedCapturing_RNG rng;
4✔
971

972
            auto timer = make_timer(src, "", "bytes");
8✔
973
            timer->run([&]() { entropy_bits = srcs.poll_just(rng, src); });
8✔
974

975
            size_t compressed_size = 0;
4✔
976

977
#if defined(BOTAN_HAS_ZLIB)
978
            auto comp = Botan::Compression_Algorithm::create("zlib");
4✔
979

980
            if(comp) {
4✔
981
               Botan::secure_vector<uint8_t> compressed;
4✔
982
               compressed.assign(rng.seed_material().begin(), rng.seed_material().end());
4✔
983
               comp->start(9);
4✔
984
               comp->finish(compressed);
4✔
985

986
               compressed_size = compressed.size();
4✔
987
            }
4✔
988
#endif
989

990
            std::ostringstream msg;
4✔
991

992
            msg << "Entropy source " << src << " output " << rng.seed_material().size() << " bytes"
4✔
993
                << " estimated entropy " << entropy_bits << " in " << timer->milliseconds() << " ms";
4✔
994

995
            if(compressed_size > 0) {
4✔
996
               msg << " output compressed to " << compressed_size << " bytes";
4✔
997
            }
998

999
            msg << " total samples " << rng.samples() << "\n";
4✔
1000

1001
            timer->set_custom_msg(msg.str());
8✔
1002

1003
            record_result(timer);
4✔
1004
         }
13✔
1005
      }
1✔
1006

1007
#if defined(BOTAN_HAS_ECC_GROUP)
1008
      void bench_ecc_ops(const std::vector<std::string>& groups, const std::chrono::milliseconds runtime) {
1✔
1009
         for(const std::string& group_name : groups) {
7✔
1010
            const Botan::EC_Group ec_group(group_name);
6✔
1011

1012
            auto add_timer = make_timer(group_name + " add");
15✔
1013
            auto addf_timer = make_timer(group_name + " addf");
15✔
1014
            auto dbl_timer = make_timer(group_name + " dbl");
15✔
1015

1016
            const Botan::EC_Point& base_point = ec_group.get_base_point();
6✔
1017

1018
            // create a non-affine point
1019
            const auto random_k = Botan::BigInt::from_u64(0x4E6F537465707E);
6✔
1020
            Botan::EC_Point non_affine_pt = ec_group.get_base_point() * random_k;
6✔
1021
            Botan::EC_Point pt = ec_group.get_base_point();
6✔
1022

1023
            std::vector<Botan::BigInt> ws(Botan::EC_Point::WORKSPACE_SIZE);
6✔
1024

1025
            while(add_timer->under(runtime) && addf_timer->under(runtime) && dbl_timer->under(runtime)) {
605✔
1026
               dbl_timer->run([&]() { pt.mult2(ws); });
1,198✔
1027
               add_timer->run([&]() { pt.add(non_affine_pt, ws); });
1,198✔
1028
               addf_timer->run([&]() { pt.add_affine(base_point, ws); });
1,198✔
1029
            }
1030

1031
            record_result(dbl_timer);
6✔
1032
            record_result(add_timer);
6✔
1033
            record_result(addf_timer);
6✔
1034
         }
12✔
1035
      }
1✔
1036

1037
      void bench_ecc_init(const std::vector<std::string>& groups, const std::chrono::milliseconds runtime) {
1✔
1038
         for(std::string group_name : groups) {
7✔
1039
            auto timer = make_timer(group_name + " initialization");
18✔
1040

1041
            while(timer->under(runtime)) {
12✔
1042
               Botan::EC_Group::clear_registered_curve_data();
6✔
1043
               timer->run([&]() { Botan::EC_Group group(group_name); });
12✔
1044
            }
1045

1046
            record_result(timer);
6✔
1047
         }
6✔
1048
      }
1✔
1049

1050
      void bench_ecc_mult(const std::vector<std::string>& groups, const std::chrono::milliseconds runtime) {
1✔
1051
         for(const std::string& group_name : groups) {
7✔
1052
            const Botan::EC_Group ec_group(group_name);
6✔
1053

1054
            auto mult_timer = make_timer(group_name + " Montgomery ladder");
18✔
1055
            auto blinded_mult_timer = make_timer(group_name + " blinded comb");
18✔
1056
            auto blinded_var_mult_timer = make_timer(group_name + " blinded window");
18✔
1057

1058
            const Botan::EC_Point& base_point = ec_group.get_base_point();
6✔
1059

1060
            std::vector<Botan::BigInt> ws;
6✔
1061

1062
            while(mult_timer->under(runtime) && blinded_mult_timer->under(runtime) &&
12✔
1063
                  blinded_var_mult_timer->under(runtime)) {
6✔
1064
               const Botan::BigInt scalar(rng(), ec_group.get_p_bits());
6✔
1065

1066
               const Botan::EC_Point r1 = mult_timer->run([&]() { return base_point * scalar; });
18✔
1067

1068
               const Botan::EC_Point r2 =
6✔
1069
                  blinded_mult_timer->run([&]() { return ec_group.blinded_base_point_multiply(scalar, rng(), ws); });
12✔
1070

1071
               const Botan::EC_Point r3 = blinded_var_mult_timer->run(
6✔
1072
                  [&]() { return ec_group.blinded_var_point_multiply(base_point, scalar, rng(), ws); });
12✔
1073

1074
               BOTAN_ASSERT_EQUAL(r1, r2, "Same point computed by Montgomery and comb");
6✔
1075
               BOTAN_ASSERT_EQUAL(r1, r3, "Same point computed by Montgomery and window");
6✔
1076
            }
12✔
1077

1078
            record_result(mult_timer);
6✔
1079
            record_result(blinded_mult_timer);
6✔
1080
            record_result(blinded_var_mult_timer);
6✔
1081
         }
6✔
1082
      }
1✔
1083

1084
      void bench_os2ecp(const std::vector<std::string>& groups, const std::chrono::milliseconds runtime) {
1✔
1085
         for(const std::string& group_name : groups) {
7✔
1086
            auto uncmp_timer = make_timer("OS2ECP uncompressed " + group_name);
18✔
1087
            auto cmp_timer = make_timer("OS2ECP compressed " + group_name);
18✔
1088

1089
            const Botan::EC_Group ec_group(group_name);
6✔
1090

1091
            while(uncmp_timer->under(runtime) && cmp_timer->under(runtime)) {
12✔
1092
               const Botan::BigInt k(rng(), 256);
6✔
1093
               const Botan::EC_Point p = ec_group.get_base_point() * k;
6✔
1094
               const std::vector<uint8_t> os_cmp = p.encode(Botan::EC_Point_Format::Compressed);
6✔
1095
               const std::vector<uint8_t> os_uncmp = p.encode(Botan::EC_Point_Format::Uncompressed);
6✔
1096

1097
               uncmp_timer->run([&]() { ec_group.OS2ECP(os_uncmp); });
12✔
1098
               cmp_timer->run([&]() { ec_group.OS2ECP(os_cmp); });
12✔
1099
            }
18✔
1100

1101
            record_result(uncmp_timer);
6✔
1102
            record_result(cmp_timer);
6✔
1103
         }
6✔
1104
      }
1✔
1105

1106
#endif
1107

1108
#if defined(BOTAN_HAS_EC_HASH_TO_CURVE)
1109
      void bench_ec_h2c(const std::chrono::milliseconds runtime) {
1✔
1110
         for(std::string group_name : {"secp256r1", "secp384r1", "secp521r1"}) {
4✔
1111
            auto h2c_ro_timer = make_timer(group_name + "-RO", "", "hash to curve");
6✔
1112
            auto h2c_nu_timer = make_timer(group_name + "-NU", "", "hash to curve");
6✔
1113

1114
            const Botan::EC_Group group(group_name);
3✔
1115

1116
            while(h2c_ro_timer->under(runtime)) {
6✔
1117
               std::vector<uint8_t> input(32);
3✔
1118

1119
               rng().randomize(input.data(), input.size());
3✔
1120

1121
               const Botan::EC_Point p1 = h2c_ro_timer->run(
3✔
1122
                  [&]() { return group.hash_to_curve("SHA-256", input.data(), input.size(), nullptr, 0, true); });
6✔
1123

1124
               BOTAN_ASSERT_NOMSG(p1.on_the_curve());
3✔
1125

1126
               const Botan::EC_Point p2 = h2c_nu_timer->run(
3✔
1127
                  [&]() { return group.hash_to_curve("SHA-256", input.data(), input.size(), nullptr, 0, false); });
6✔
1128

1129
               BOTAN_ASSERT_NOMSG(p2.on_the_curve());
3✔
1130
            }
6✔
1131

1132
            record_result(h2c_ro_timer);
3✔
1133
            record_result(h2c_nu_timer);
3✔
1134
         }
3✔
1135
      }
1✔
1136
#endif
1137

1138
#if defined(BOTAN_HAS_FPE_FE1)
1139

1140
      void bench_fpe_fe1(const std::chrono::milliseconds runtime) {
1✔
1141
         const auto n = Botan::BigInt::from_u64(1000000000000000);
1✔
1142

1143
         auto enc_timer = make_timer("FPE_FE1 encrypt");
2✔
1144
         auto dec_timer = make_timer("FPE_FE1 decrypt");
2✔
1145

1146
         const Botan::SymmetricKey key(rng(), 32);
1✔
1147
         const std::vector<uint8_t> tweak(8);  // 8 zeros
1✔
1148

1149
         auto x = Botan::BigInt::one();
1✔
1150

1151
         Botan::FPE_FE1 fpe_fe1(n);
1✔
1152
         fpe_fe1.set_key(key);
1✔
1153

1154
         while(enc_timer->under(runtime)) {
3✔
1155
            enc_timer->start();
2✔
1156
            x = fpe_fe1.encrypt(x, tweak.data(), tweak.size());
2✔
1157
            enc_timer->stop();
2✔
1158
         }
1159

1160
         for(size_t i = 0; i != enc_timer->events(); ++i) {
3✔
1161
            dec_timer->start();
2✔
1162
            x = fpe_fe1.decrypt(x, tweak.data(), tweak.size());
2✔
1163
            dec_timer->stop();
2✔
1164
         }
1165

1166
         BOTAN_ASSERT(x == 1, "FPE works");
1✔
1167

1168
         record_result(enc_timer);
1✔
1169
         record_result(dec_timer);
1✔
1170
      }
5✔
1171
#endif
1172

1173
#if defined(BOTAN_HAS_RFC3394_KEYWRAP)
1174

1175
      void bench_rfc3394(const std::chrono::milliseconds runtime) {
1✔
1176
         auto wrap_timer = make_timer("RFC3394 AES-256 key wrap");
3✔
1177
         auto unwrap_timer = make_timer("RFC3394 AES-256 key unwrap");
3✔
1178

1179
         const Botan::SymmetricKey kek(rng(), 32);
1✔
1180
         Botan::secure_vector<uint8_t> key(64, 0);
1✔
1181

1182
         while(wrap_timer->under(runtime)) {
33✔
1183
            wrap_timer->start();
32✔
1184
            key = Botan::rfc3394_keywrap(key, kek);
64✔
1185
            wrap_timer->stop();
32✔
1186

1187
            unwrap_timer->start();
32✔
1188
            key = Botan::rfc3394_keyunwrap(key, kek);
64✔
1189
            unwrap_timer->stop();
32✔
1190

1191
            key[0] += 1;
32✔
1192
         }
1193

1194
         record_result(wrap_timer);
1✔
1195
         record_result(unwrap_timer);
1✔
1196
      }
2✔
1197
#endif
1198

1199
#if defined(BOTAN_HAS_BIGINT)
1200

1201
      void bench_mp_mul(const std::chrono::milliseconds runtime) {
1✔
1202
         std::chrono::milliseconds runtime_per_size = runtime;
1✔
1203
         for(size_t bits : {256, 384, 512, 768, 1024, 1536, 2048, 3072, 4096}) {
10✔
1204
            auto mul_timer = make_timer("BigInt mul " + std::to_string(bits));
18✔
1205
            auto sqr_timer = make_timer("BigInt sqr " + std::to_string(bits));
18✔
1206

1207
            const Botan::BigInt y(rng(), bits);
9✔
1208
            Botan::secure_vector<Botan::word> ws;
9✔
1209

1210
            while(mul_timer->under(runtime_per_size)) {
3,714✔
1211
               Botan::BigInt x(rng(), bits);
3,705✔
1212

1213
               sqr_timer->start();
3,705✔
1214
               x.square(ws);
3,705✔
1215
               sqr_timer->stop();
3,705✔
1216

1217
               x.mask_bits(bits);
3,705✔
1218

1219
               mul_timer->start();
3,705✔
1220
               x.mul(y, ws);
3,705✔
1221
               mul_timer->stop();
3,705✔
1222
            }
3,705✔
1223

1224
            record_result(mul_timer);
9✔
1225
            record_result(sqr_timer);
9✔
1226
         }
18✔
1227
      }
1✔
1228

1229
      void bench_mp_div(const std::chrono::milliseconds runtime) {
1✔
1230
         std::chrono::milliseconds runtime_per_size = runtime;
1✔
1231

1232
         for(size_t n_bits : {256, 384, 512, 768, 1024, 1536, 2048, 3072, 4096}) {
10✔
1233
            const size_t q_bits = n_bits / 2;
9✔
1234
            const std::string bit_descr = std::to_string(n_bits) + "/" + std::to_string(q_bits);
18✔
1235

1236
            auto div_timer = make_timer("BigInt div " + bit_descr);
27✔
1237
            auto ct_div_timer = make_timer("BigInt ct_div " + bit_descr);
27✔
1238

1239
            Botan::BigInt y;
9✔
1240
            Botan::BigInt x;
9✔
1241
            Botan::secure_vector<Botan::word> ws;
9✔
1242

1243
            Botan::BigInt q1, r1, q2, r2;
9✔
1244

1245
            while(ct_div_timer->under(runtime_per_size)) {
39✔
1246
               x.randomize(rng(), n_bits);
30✔
1247
               y.randomize(rng(), q_bits);
30✔
1248

1249
               div_timer->start();
30✔
1250
               Botan::vartime_divide(x, y, q1, r1);
30✔
1251
               div_timer->stop();
30✔
1252

1253
               ct_div_timer->start();
30✔
1254
               Botan::ct_divide(x, y, q2, r2);
30✔
1255
               ct_div_timer->stop();
30✔
1256

1257
               BOTAN_ASSERT_EQUAL(q1, q2, "Quotient ok");
30✔
1258
               BOTAN_ASSERT_EQUAL(r1, r2, "Remainder ok");
69✔
1259
            }
1260

1261
            record_result(div_timer);
9✔
1262
            record_result(ct_div_timer);
9✔
1263
         }
54✔
1264
      }
1✔
1265

1266
      void bench_mp_div10(const std::chrono::milliseconds runtime) {
1✔
1267
         std::chrono::milliseconds runtime_per_size = runtime;
1✔
1268

1269
         for(size_t n_bits : {256, 384, 512, 768, 1024, 1536, 2048, 3072, 4096}) {
10✔
1270
            const std::string bit_descr = std::to_string(n_bits) + "/10";
9✔
1271

1272
            auto div_timer = make_timer("BigInt div " + bit_descr);
27✔
1273
            auto ct_div_timer = make_timer("BigInt ct_div " + bit_descr);
27✔
1274

1275
            Botan::BigInt x;
9✔
1276
            Botan::secure_vector<Botan::word> ws;
9✔
1277

1278
            const auto ten = Botan::BigInt::from_word(10);
9✔
1279
            Botan::BigInt q1, r1, q2;
9✔
1280
            Botan::word r2;
1281

1282
            while(ct_div_timer->under(runtime_per_size)) {
191✔
1283
               x.randomize(rng(), n_bits);
182✔
1284

1285
               div_timer->start();
182✔
1286
               Botan::vartime_divide(x, ten, q1, r1);
182✔
1287
               div_timer->stop();
182✔
1288

1289
               ct_div_timer->start();
182✔
1290
               Botan::ct_divide_word(x, 10, q2, r2);
182✔
1291
               ct_div_timer->stop();
182✔
1292

1293
               BOTAN_ASSERT_EQUAL(q1, q2, "Quotient ok");
182✔
1294
               BOTAN_ASSERT_EQUAL(r1, r2, "Remainder ok");
373✔
1295
            }
1296

1297
            record_result(div_timer);
9✔
1298
            record_result(ct_div_timer);
9✔
1299
         }
45✔
1300
      }
1✔
1301

1302
#endif
1303

1304
#if defined(BOTAN_HAS_DL_GROUP)
1305

1306
      void bench_modexp(const std::chrono::milliseconds runtime) {
1✔
1307
         for(size_t group_bits : {1024, 1536, 2048, 3072, 4096}) {
6✔
1308
            const std::string group_bits_str = std::to_string(group_bits);
5✔
1309
            const Botan::DL_Group group("modp/srp/" + group_bits_str);
5✔
1310

1311
            const size_t e_bits = Botan::dl_exponent_size(group_bits);
5✔
1312
            const size_t f_bits = group_bits - 1;
5✔
1313

1314
            const Botan::BigInt random_e(rng(), e_bits);
5✔
1315
            const Botan::BigInt random_f(rng(), f_bits);
5✔
1316

1317
            auto e_timer = make_timer(group_bits_str + " short exponent");
15✔
1318
            auto f_timer = make_timer(group_bits_str + "  full exponent");
15✔
1319

1320
            while(f_timer->under(runtime)) {
10✔
1321
               e_timer->run([&]() { group.power_g_p(random_e); });
10✔
1322
               f_timer->run([&]() { group.power_g_p(random_f); });
10✔
1323
            }
1324

1325
            record_result(e_timer);
5✔
1326
            record_result(f_timer);
5✔
1327
         }
20✔
1328
      }
1✔
1329
#endif
1330

1331
#if defined(BOTAN_HAS_NUMBERTHEORY)
1332
      void bench_nistp_redc(const std::chrono::milliseconds runtime) {
1✔
1333
         Botan::secure_vector<Botan::word> ws;
1✔
1334

1335
         auto p192_timer = make_timer("P-192 redc");
2✔
1336
         Botan::BigInt r192(rng(), 192 * 2 - 1);
1✔
1337
         while(p192_timer->under(runtime)) {
2,359✔
1338
            Botan::BigInt r = r192;
2,358✔
1339
            p192_timer->run([&]() { Botan::redc_p192(r, ws); });
4,716✔
1340
            r192 += 1;
2,358✔
1341
         }
2,358✔
1342
         record_result(p192_timer);
1✔
1343

1344
         auto p224_timer = make_timer("P-224 redc");
2✔
1345
         Botan::BigInt r224(rng(), 224 * 2 - 1);
1✔
1346
         while(p224_timer->under(runtime)) {
1,835✔
1347
            Botan::BigInt r = r224;
1,834✔
1348
            p224_timer->run([&]() { Botan::redc_p224(r, ws); });
3,668✔
1349
            r224 += 1;
1,834✔
1350
         }
1,834✔
1351
         record_result(p224_timer);
1✔
1352

1353
         auto p256_timer = make_timer("P-256 redc");
2✔
1354
         Botan::BigInt r256(rng(), 256 * 2 - 1);
1✔
1355
         while(p256_timer->under(runtime)) {
2,075✔
1356
            Botan::BigInt r = r256;
2,074✔
1357
            p256_timer->run([&]() { Botan::redc_p256(r, ws); });
4,148✔
1358
            r256 += 1;
2,074✔
1359
         }
2,074✔
1360
         record_result(p256_timer);
1✔
1361

1362
         auto p384_timer = make_timer("P-384 redc");
2✔
1363
         Botan::BigInt r384(rng(), 384 * 2 - 1);
1✔
1364
         while(p384_timer->under(runtime)) {
1,698✔
1365
            Botan::BigInt r = r384;
1,697✔
1366
            p384_timer->run([&]() { Botan::redc_p384(r384, ws); });
3,394✔
1367
            r384 += 1;
1,697✔
1368
         }
1,697✔
1369
         record_result(p384_timer);
1✔
1370

1371
         auto p521_timer = make_timer("P-521 redc");
2✔
1372
         Botan::BigInt r521(rng(), 521 * 2 - 1);
1✔
1373
         while(p521_timer->under(runtime)) {
1,112✔
1374
            Botan::BigInt r = r521;
1,111✔
1375
            p521_timer->run([&]() { Botan::redc_p521(r521, ws); });
2,222✔
1376
            r521 += 1;
1,111✔
1377
         }
1,111✔
1378
         record_result(p521_timer);
1✔
1379
      }
6✔
1380

1381
      void bench_bn_redc(const std::chrono::milliseconds runtime) {
1✔
1382
         for(size_t bitsize : {512, 1024, 2048, 4096}) {
5✔
1383
            Botan::BigInt p(rng(), bitsize);
4✔
1384

1385
            std::string bit_str = std::to_string(bitsize);
4✔
1386
            auto barrett_timer = make_timer("Barrett-" + bit_str);
8✔
1387
            auto schoolbook_timer = make_timer("Schoolbook-" + bit_str);
8✔
1388

1389
            Botan::Modular_Reducer mod_p(p);
4✔
1390

1391
            while(schoolbook_timer->under(runtime)) {
45✔
1392
               const Botan::BigInt x(rng(), p.bits() * 2 - 2);
41✔
1393

1394
               const Botan::BigInt r1 = barrett_timer->run([&] { return mod_p.reduce(x); });
82✔
1395
               const Botan::BigInt r2 = schoolbook_timer->run([&] { return x % p; });
82✔
1396

1397
               BOTAN_ASSERT(r1 == r2, "Computed different results");
41✔
1398
            }
123✔
1399

1400
            record_result(barrett_timer);
4✔
1401
            record_result(schoolbook_timer);
4✔
1402
         }
8✔
1403
      }
1✔
1404

1405
      void bench_inverse_mod(const std::chrono::milliseconds runtime) {
1✔
1406
         for(size_t bits : {256, 384, 512, 1024, 2048}) {
6✔
1407
            const std::string bit_str = std::to_string(bits);
5✔
1408

1409
            auto timer = make_timer("inverse_mod-" + bit_str);
12✔
1410
            auto gcd_timer = make_timer("gcd-" + bit_str);
10✔
1411

1412
            while(timer->under(runtime) && gcd_timer->under(runtime)) {
11✔
1413
               const Botan::BigInt x(rng(), bits - 1);
6✔
1414
               Botan::BigInt mod(rng(), bits);
6✔
1415

1416
               const Botan::BigInt x_inv = timer->run([&] { return Botan::inverse_mod(x, mod); });
12✔
1417

1418
               const Botan::BigInt g = gcd_timer->run([&] { return gcd(x, mod); });
12✔
1419

1420
               if(x_inv == 0) {
6✔
1421
                  BOTAN_ASSERT(g != 1, "Inversion only fails if gcd(x, mod) > 1");
2✔
1422
               } else {
1423
                  BOTAN_ASSERT(g == 1, "Inversion succeeds only if gcd != 1");
4✔
1424
                  const Botan::BigInt check = (x_inv * x) % mod;
4✔
1425
                  BOTAN_ASSERT_EQUAL(check, 1, "Const time inversion correct");
4✔
1426
               }
4✔
1427
            }
23✔
1428

1429
            record_result(timer);
5✔
1430
            record_result(gcd_timer);
5✔
1431
         }
5✔
1432
      }
1✔
1433

1434
      void bench_primality_tests(const std::chrono::milliseconds runtime) {
1✔
1435
         for(size_t bits : {256, 512, 1024}) {
4✔
1436
            auto mr_timer = make_timer("Miller-Rabin-" + std::to_string(bits));
9✔
1437
            auto bpsw_timer = make_timer("Bailie-PSW-" + std::to_string(bits));
6✔
1438
            auto lucas_timer = make_timer("Lucas-" + std::to_string(bits));
6✔
1439

1440
            Botan::BigInt n = Botan::random_prime(rng(), bits);
3✔
1441

1442
            while(lucas_timer->under(runtime)) {
6✔
1443
               Botan::Modular_Reducer mod_n(n);
3✔
1444

1445
               mr_timer->run([&]() { return Botan::is_miller_rabin_probable_prime(n, mod_n, rng(), 2); });
6✔
1446

1447
               bpsw_timer->run([&]() { return Botan::is_bailie_psw_probable_prime(n, mod_n); });
6✔
1448

1449
               lucas_timer->run([&]() { return Botan::is_lucas_probable_prime(n, mod_n); });
6✔
1450

1451
               n += 2;
3✔
1452
            }
3✔
1453

1454
            record_result(mr_timer);
3✔
1455
            record_result(bpsw_timer);
3✔
1456
            record_result(lucas_timer);
3✔
1457
         }
3✔
1458
      }
1✔
1459

1460
      void bench_random_prime(const std::chrono::milliseconds runtime) {
1✔
1461
         const auto coprime = Botan::BigInt::from_word(0x10001);
1✔
1462

1463
         for(size_t bits : {256, 384, 512, 768, 1024, 1536}) {
7✔
1464
            auto genprime_timer = make_timer("random_prime " + std::to_string(bits));
18✔
1465
            auto gensafe_timer = make_timer("random_safe_prime " + std::to_string(bits));
18✔
1466
            auto is_prime_timer = make_timer("is_prime " + std::to_string(bits));
12✔
1467

1468
            while(gensafe_timer->under(runtime)) {
12✔
1469
               const Botan::BigInt p = genprime_timer->run([&] { return Botan::random_prime(rng(), bits, coprime); });
12✔
1470

1471
               if(!is_prime_timer->run([&] { return Botan::is_prime(p, rng(), 64, true); })) {
12✔
1472
                  error_output() << "Generated prime " << p << " which failed a primality test";
×
1473
               }
1474

1475
               const Botan::BigInt sg = gensafe_timer->run([&] { return Botan::random_safe_prime(rng(), bits); });
12✔
1476

1477
               if(!is_prime_timer->run([&] { return Botan::is_prime(sg, rng(), 64, true); })) {
12✔
1478
                  error_output() << "Generated safe prime " << sg << " which failed a primality test";
×
1479
               }
1480

1481
               if(!is_prime_timer->run([&] { return Botan::is_prime(sg / 2, rng(), 64, true); })) {
18✔
1482
                  error_output() << "Generated prime " << sg / 2 << " which failed a primality test";
×
1483
               }
1484

1485
               // Now test p+2, p+4, ... which may or may not be prime
1486
               for(size_t i = 2; i <= 64; i += 2) {
198✔
1487
                  is_prime_timer->run([&]() { Botan::is_prime(p + i, rng(), 64, true); });
576✔
1488
               }
1489
            }
12✔
1490

1491
            record_result(genprime_timer);
6✔
1492
            record_result(gensafe_timer);
6✔
1493
            record_result(is_prime_timer);
6✔
1494
         }
6✔
1495
      }
1✔
1496
#endif
1497

1498
#if defined(BOTAN_HAS_PUBLIC_KEY_CRYPTO)
1499
      void bench_pk_enc(const Botan::Private_Key& key,
4✔
1500
                        const std::string& nm,
1501
                        const std::string& provider,
1502
                        const std::string& padding,
1503
                        std::chrono::milliseconds msec) {
1504
         std::vector<uint8_t> plaintext, ciphertext;
4✔
1505

1506
         Botan::PK_Encryptor_EME enc(key, rng(), padding, provider);
4✔
1507
         Botan::PK_Decryptor_EME dec(key, rng(), padding, provider);
4✔
1508

1509
         auto enc_timer = make_timer(nm + " " + padding, provider, "encrypt");
12✔
1510
         auto dec_timer = make_timer(nm + " " + padding, provider, "decrypt");
12✔
1511

1512
         while(enc_timer->under(msec) || dec_timer->under(msec)) {
12✔
1513
            // Generate a new random ciphertext to decrypt
1514
            if(ciphertext.empty() || enc_timer->under(msec)) {
4✔
1515
               rng().random_vec(plaintext, enc.maximum_input_size());
4✔
1516
               ciphertext = enc_timer->run([&]() { return enc.encrypt(plaintext, rng()); });
12✔
1517
            }
1518

1519
            if(dec_timer->under(msec)) {
4✔
1520
               const auto dec_pt = dec_timer->run([&]() { return dec.decrypt(ciphertext); });
12✔
1521

1522
               if(!(Botan::unlock(dec_pt) == plaintext))  // sanity check
8✔
1523
               {
1524
                  error_output() << "Bad roundtrip in PK encrypt/decrypt bench\n";
×
1525
               }
1526
            }
4✔
1527
         }
1528

1529
         record_result(enc_timer);
4✔
1530
         record_result(dec_timer);
4✔
1531
      }
12✔
1532

1533
      void bench_pk_ka(const std::string& algo,
14✔
1534
                       const std::string& nm,
1535
                       const std::string& params,
1536
                       const std::string& provider,
1537
                       std::chrono::milliseconds msec) {
1538
         const std::string kdf = "KDF2(SHA-256)";  // arbitrary choice
14✔
1539

1540
         auto keygen_timer = make_timer(nm, provider, "keygen");
28✔
1541

1542
         std::unique_ptr<Botan::Private_Key> key1(
14✔
1543
            keygen_timer->run([&] { return Botan::create_private_key(algo, rng(), params); }));
28✔
1544
         std::unique_ptr<Botan::Private_Key> key2(
14✔
1545
            keygen_timer->run([&] { return Botan::create_private_key(algo, rng(), params); }));
28✔
1546

1547
         record_result(keygen_timer);
14✔
1548

1549
         const Botan::PK_Key_Agreement_Key& ka_key1 = dynamic_cast<const Botan::PK_Key_Agreement_Key&>(*key1);
14✔
1550
         const Botan::PK_Key_Agreement_Key& ka_key2 = dynamic_cast<const Botan::PK_Key_Agreement_Key&>(*key2);
14✔
1551

1552
         Botan::PK_Key_Agreement ka1(ka_key1, rng(), kdf, provider);
14✔
1553
         Botan::PK_Key_Agreement ka2(ka_key2, rng(), kdf, provider);
14✔
1554

1555
         const std::vector<uint8_t> ka1_pub = ka_key1.public_value();
14✔
1556
         const std::vector<uint8_t> ka2_pub = ka_key2.public_value();
14✔
1557

1558
         auto ka_timer = make_timer(nm, provider, "key agreements");
28✔
1559

1560
         while(ka_timer->under(msec)) {
31✔
1561
            Botan::SymmetricKey symkey1 = ka_timer->run([&]() { return ka1.derive_key(32, ka2_pub); });
34✔
1562
            Botan::SymmetricKey symkey2 = ka_timer->run([&]() { return ka2.derive_key(32, ka1_pub); });
34✔
1563

1564
            if(symkey1 != symkey2) {
17✔
1565
               error_output() << "Key agreement mismatch in PK bench\n";
×
1566
            }
1567
         }
34✔
1568

1569
         record_result(ka_timer);
14✔
1570
      }
70✔
1571

1572
      void bench_pk_kem(const Botan::Private_Key& key,
11✔
1573
                        const std::string& nm,
1574
                        const std::string& provider,
1575
                        const std::string& kdf,
1576
                        std::chrono::milliseconds msec) {
1577
         Botan::PK_KEM_Decryptor dec(key, rng(), kdf, provider);
11✔
1578
         Botan::PK_KEM_Encryptor enc(key, kdf, provider);
11✔
1579

1580
         auto kem_enc_timer = make_timer(nm, provider, "KEM encrypt");
22✔
1581
         auto kem_dec_timer = make_timer(nm, provider, "KEM decrypt");
22✔
1582

1583
         while(kem_enc_timer->under(msec) && kem_dec_timer->under(msec)) {
31✔
1584
            Botan::secure_vector<uint8_t> encap_key, enc_shared_key;
20✔
1585
            Botan::secure_vector<uint8_t> salt = rng().random_vec(16);
20✔
1586

1587
            kem_enc_timer->start();
20✔
1588
            enc.encrypt(encap_key, enc_shared_key, 64, rng(), salt);
20✔
1589
            kem_enc_timer->stop();
20✔
1590

1591
            kem_dec_timer->start();
20✔
1592
            Botan::secure_vector<uint8_t> dec_shared_key = dec.decrypt(encap_key, 64, salt);
20✔
1593
            kem_dec_timer->stop();
20✔
1594

1595
            if(enc_shared_key != dec_shared_key) {
20✔
1596
               error_output() << "KEM mismatch in PK bench\n";
×
1597
            }
1598
         }
80✔
1599

1600
         record_result(kem_enc_timer);
11✔
1601
         record_result(kem_dec_timer);
11✔
1602
      }
11✔
1603

1604
      void bench_pk_sig_ecc(const std::string& algo,
6✔
1605
                            const std::string& emsa,
1606
                            const std::string& provider,
1607
                            const std::vector<std::string>& params,
1608
                            std::chrono::milliseconds msec) {
1609
         for(std::string grp : params) {
32✔
1610
            const std::string nm = grp.empty() ? algo : (algo + "-" + grp);
26✔
1611

1612
            auto keygen_timer = make_timer(nm, provider, "keygen");
52✔
1613

1614
            std::unique_ptr<Botan::Private_Key> key(
26✔
1615
               keygen_timer->run([&] { return Botan::create_private_key(algo, rng(), grp); }));
52✔
1616

1617
            record_result(keygen_timer);
26✔
1618
            bench_pk_sig(*key, nm, provider, emsa, msec);
26✔
1619
         }
48✔
1620
      }
6✔
1621

1622
      size_t bench_pk_sig(const Botan::Private_Key& key,
40✔
1623
                          const std::string& nm,
1624
                          const std::string& provider,
1625
                          const std::string& padding,
1626
                          std::chrono::milliseconds msec) {
1627
         std::vector<uint8_t> message, signature, bad_signature;
40✔
1628

1629
         Botan::PK_Signer sig(key, rng(), padding, Botan::Signature_Format::Standard, provider);
40✔
1630
         Botan::PK_Verifier ver(key, padding, Botan::Signature_Format::Standard, provider);
40✔
1631

1632
         auto sig_timer = make_timer(nm + " " + padding, provider, "sign");
119✔
1633
         auto ver_timer = make_timer(nm + " " + padding, provider, "verify");
119✔
1634

1635
         size_t invalid_sigs = 0;
40✔
1636

1637
         while(ver_timer->under(msec) || sig_timer->under(msec)) {
155✔
1638
            if(signature.empty() || sig_timer->under(msec)) {
75✔
1639
               /*
1640
               Length here is kind of arbitrary, but 48 bytes fits into a single
1641
               hash block so minimizes hashing overhead versus the PK op itself.
1642
               */
1643
               rng().random_vec(message, 48);
49✔
1644

1645
               signature = sig_timer->run([&]() { return sig.sign_message(message, rng()); });
147✔
1646

1647
               bad_signature = signature;
49✔
1648
               bad_signature[rng().next_byte() % bad_signature.size()] ^= rng().next_nonzero_byte();
49✔
1649
            }
1650

1651
            if(ver_timer->under(msec)) {
75✔
1652
               const bool verified = ver_timer->run([&] { return ver.verify_message(message, signature); });
132✔
1653

1654
               if(!verified) {
66✔
1655
                  invalid_sigs += 1;
×
1656
               }
1657

1658
               const bool verified_bad = ver_timer->run([&] { return ver.verify_message(message, bad_signature); });
132✔
1659

1660
               if(verified_bad) {
66✔
1661
                  error_output() << "Bad signature accepted in " << nm << " signature bench\n";
×
1662
               }
1663
            }
1664
         }
1665

1666
         if(invalid_sigs > 0) {
40✔
1667
            error_output() << invalid_sigs << " generated signatures rejected in " << nm << " signature bench\n";
×
1668
         }
1669

1670
         const size_t events = static_cast<size_t>(std::min(sig_timer->events(), ver_timer->events()));
40✔
1671

1672
         record_result(sig_timer);
40✔
1673
         record_result(ver_timer);
40✔
1674

1675
         return events;
80✔
1676
      }
160✔
1677
#endif
1678

1679
#if defined(BOTAN_HAS_RSA)
1680
      void bench_rsa_keygen(const std::string& provider, std::chrono::milliseconds msec) {
1✔
1681
         for(size_t keylen : {1024, 2048, 3072, 4096}) {
5✔
1682
            const std::string nm = "RSA-" + std::to_string(keylen);
4✔
1683
            auto keygen_timer = make_timer(nm, provider, "keygen");
8✔
1684

1685
            while(keygen_timer->under(msec)) {
8✔
1686
               std::unique_ptr<Botan::Private_Key> key(
4✔
1687
                  keygen_timer->run([&] { return Botan::create_private_key("RSA", rng(), std::to_string(keylen)); }));
8✔
1688

1689
               BOTAN_ASSERT(key->check_key(rng(), true), "Key is ok");
4✔
1690
            }
4✔
1691

1692
            record_result(keygen_timer);
4✔
1693
         }
4✔
1694
      }
1✔
1695

1696
      void bench_rsa(const std::string& provider, std::chrono::milliseconds msec) {
1✔
1697
         for(size_t keylen : {1024, 2048, 3072, 4096}) {
5✔
1698
            const std::string nm = "RSA-" + std::to_string(keylen);
4✔
1699

1700
            auto keygen_timer = make_timer(nm, provider, "keygen");
8✔
1701

1702
            std::unique_ptr<Botan::Private_Key> key(
4✔
1703
               keygen_timer->run([&] { return Botan::create_private_key("RSA", rng(), std::to_string(keylen)); }));
8✔
1704

1705
            record_result(keygen_timer);
4✔
1706

1707
            // Using PKCS #1 padding so OpenSSL provider can play along
1708
            bench_pk_sig(*key, nm, provider, "EMSA-PKCS1-v1_5(SHA-256)", msec);
8✔
1709

1710
            //bench_pk_sig(*key, nm, provider, "PSSR(SHA-256)", msec);
1711
            //bench_pk_enc(*key, nm, provider, "EME-PKCS1-v1_5", msec);
1712
            //bench_pk_enc(*key, nm, provider, "OAEP(SHA-1)", msec);
1713
         }
4✔
1714
      }
1✔
1715
#endif
1716

1717
#if defined(BOTAN_HAS_ECDSA)
1718
      void bench_ecdsa(const std::vector<std::string>& groups,
1✔
1719
                       const std::string& provider,
1720
                       std::chrono::milliseconds msec) {
1721
         return bench_pk_sig_ecc("ECDSA", "SHA-256", provider, groups, msec);
2✔
1722
      }
1723

1724
      void bench_ecdsa_recovery(const std::vector<std::string>& groups,
1✔
1725
                                const std::string& /*unused*/,
1726
                                std::chrono::milliseconds msec) {
1727
         for(const std::string& group_name : groups) {
7✔
1728
            Botan::EC_Group group(group_name);
6✔
1729
            auto recovery_timer = make_timer("ECDSA recovery " + group_name);
18✔
1730

1731
            while(recovery_timer->under(msec)) {
12✔
1732
               Botan::ECDSA_PrivateKey key(rng(), group);
6✔
1733

1734
               std::vector<uint8_t> message(group.get_order_bits() / 8);
6✔
1735
               rng().randomize(message.data(), message.size());
6✔
1736

1737
               Botan::PK_Signer signer(key, rng(), "Raw");
6✔
1738
               signer.update(message);
6✔
1739
               std::vector<uint8_t> signature = signer.signature(rng());
6✔
1740

1741
               Botan::PK_Verifier verifier(key, "Raw", Botan::Signature_Format::Standard, "base");
6✔
1742
               verifier.update(message);
6✔
1743
               BOTAN_ASSERT(verifier.check_signature(signature), "Valid signature");
6✔
1744

1745
               Botan::BigInt r(signature.data(), signature.size() / 2);
6✔
1746
               Botan::BigInt s(signature.data() + signature.size() / 2, signature.size() / 2);
6✔
1747

1748
               const uint8_t v = key.recovery_param(message, r, s);
6✔
1749

1750
               recovery_timer->run([&]() {
6✔
1751
                  Botan::ECDSA_PublicKey pubkey(group, message, r, s, v);
6✔
1752
                  BOTAN_ASSERT(pubkey.public_point() == key.public_point(), "Recovered public key");
6✔
1753
               });
6✔
1754
            }
24✔
1755

1756
            record_result(recovery_timer);
6✔
1757
         }
6✔
1758
      }
1✔
1759

1760
#endif
1761

1762
#if defined(BOTAN_HAS_ECKCDSA)
1763
      void bench_eckcdsa(const std::vector<std::string>& groups,
1✔
1764
                         const std::string& provider,
1765
                         std::chrono::milliseconds msec) {
1766
         return bench_pk_sig_ecc("ECKCDSA", "SHA-256", provider, groups, msec);
2✔
1767
      }
1768
#endif
1769

1770
#if defined(BOTAN_HAS_GOST_34_10_2001)
1771
      void bench_gost_3410(const std::string& provider, std::chrono::milliseconds msec) {
1✔
1772
         return bench_pk_sig_ecc("GOST-34.10", "GOST-34.11", provider, {"gost_256A"}, msec);
3✔
1773
      }
1774
#endif
1775

1776
#if defined(BOTAN_HAS_SM2)
1777
      void bench_sm2(const std::vector<std::string>& groups,
1✔
1778
                     const std::string& provider,
1779
                     std::chrono::milliseconds msec) {
1780
         return bench_pk_sig_ecc("SM2_Sig", "SM3", provider, groups, msec);
2✔
1781
      }
1782
#endif
1783

1784
#if defined(BOTAN_HAS_ECGDSA)
1785
      void bench_ecgdsa(const std::vector<std::string>& groups,
1✔
1786
                        const std::string& provider,
1787
                        std::chrono::milliseconds msec) {
1788
         return bench_pk_sig_ecc("ECGDSA", "SHA-256", provider, groups, msec);
2✔
1789
      }
1790
#endif
1791

1792
#if defined(BOTAN_HAS_ED25519)
1793
      void bench_ed25519(const std::string& provider, std::chrono::milliseconds msec) {
1✔
1794
         return bench_pk_sig_ecc("Ed25519", "Pure", provider, std::vector<std::string>{""}, msec);
3✔
1795
      }
1796
#endif
1797

1798
#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
1799
      void bench_dh(const std::string& provider, std::chrono::milliseconds msec) {
1✔
1800
         for(size_t bits : {1024, 1536, 2048, 3072, 4096, 6144, 8192}) {
8✔
1801
            bench_pk_ka("DH", "DH-" + std::to_string(bits), "modp/ietf/" + std::to_string(bits), provider, msec);
14✔
1802
         }
1803
      }
1✔
1804
#endif
1805

1806
#if defined(BOTAN_HAS_DSA)
1807
      void bench_dsa(const std::string& provider, std::chrono::milliseconds msec) {
1✔
1808
         for(size_t bits : {1024, 2048, 3072}) {
4✔
1809
            const std::string nm = "DSA-" + std::to_string(bits);
3✔
1810

1811
            const std::string params = (bits == 1024) ? "dsa/jce/1024" : ("dsa/botan/" + std::to_string(bits));
3✔
1812

1813
            auto keygen_timer = make_timer(nm, provider, "keygen");
6✔
1814

1815
            std::unique_ptr<Botan::Private_Key> key(
3✔
1816
               keygen_timer->run([&] { return Botan::create_private_key("DSA", rng(), params); }));
6✔
1817

1818
            record_result(keygen_timer);
3✔
1819

1820
            bench_pk_sig(*key, nm, provider, "SHA-256", msec);
6✔
1821
         }
3✔
1822
      }
1✔
1823
#endif
1824

1825
#if defined(BOTAN_HAS_ELGAMAL)
1826
      void bench_elgamal(const std::string& provider, std::chrono::milliseconds msec) {
1✔
1827
         for(size_t keylen : {1024, 2048, 3072, 4096}) {
5✔
1828
            const std::string nm = "ElGamal-" + std::to_string(keylen);
4✔
1829

1830
            const std::string params = "modp/ietf/" + std::to_string(keylen);
4✔
1831

1832
            auto keygen_timer = make_timer(nm, provider, "keygen");
8✔
1833

1834
            std::unique_ptr<Botan::Private_Key> key(
4✔
1835
               keygen_timer->run([&] { return Botan::create_private_key("ElGamal", rng(), params); }));
8✔
1836

1837
            record_result(keygen_timer);
4✔
1838

1839
            bench_pk_enc(*key, nm, provider, "EME-PKCS1-v1_5", msec);
8✔
1840
         }
4✔
1841
      }
1✔
1842
#endif
1843

1844
#if defined(BOTAN_HAS_ECDH)
1845
      void bench_ecdh(const std::vector<std::string>& groups,
1✔
1846
                      const std::string& provider,
1847
                      std::chrono::milliseconds msec) {
1848
         for(const std::string& grp : groups) {
7✔
1849
            bench_pk_ka("ECDH", "ECDH-" + grp, grp, provider, msec);
15✔
1850
         }
1851
      }
1✔
1852
#endif
1853

1854
#if defined(BOTAN_HAS_CURVE_25519)
1855
      void bench_curve25519(const std::string& provider, std::chrono::milliseconds msec) {
1✔
1856
         bench_pk_ka("Curve25519", "Curve25519", "", provider, msec);
2✔
1857
      }
1✔
1858
#endif
1859

1860
#if defined(BOTAN_HAS_MCELIECE)
1861
      void bench_mceliece(const std::string& provider, std::chrono::milliseconds msec) {
1✔
1862
         /*
1863
         SL=80 n=1632 t=33 - 59 KB pubkey 140 KB privkey
1864
         SL=107 n=2480 t=45 - 128 KB pubkey 300 KB privkey
1865
         SL=128 n=2960 t=57 - 195 KB pubkey 459 KB privkey
1866
         SL=147 n=3408 t=67 - 265 KB pubkey 622 KB privkey
1867
         SL=191 n=4624 t=95 - 516 KB pubkey 1234 KB privkey
1868
         SL=256 n=6624 t=115 - 942 KB pubkey 2184 KB privkey
1869
         */
1870

1871
         const std::vector<std::pair<size_t, size_t>> mce_params = {
1✔
1872
            {2480, 45}, {2960, 57}, {3408, 67}, {4624, 95}, {6624, 115}};
1✔
1873

1874
         for(auto params : mce_params) {
6✔
1875
            size_t n = params.first;
5✔
1876
            size_t t = params.second;
5✔
1877

1878
            const std::string nm = "McEliece-" + std::to_string(n) + "," + std::to_string(t) +
10✔
1879
                                   " (WF=" + std::to_string(Botan::mceliece_work_factor(n, t)) + ")";
15✔
1880

1881
            auto keygen_timer = make_timer(nm, provider, "keygen");
10✔
1882

1883
            std::unique_ptr<Botan::Private_Key> key =
5✔
1884
               keygen_timer->run([&] { return std::make_unique<Botan::McEliece_PrivateKey>(rng(), n, t); });
10✔
1885

1886
            record_result(keygen_timer);
5✔
1887
            bench_pk_kem(*key, nm, provider, "KDF2(SHA-256)", msec);
10✔
1888
         }
10✔
1889
      }
1✔
1890
#endif
1891

1892
#if defined(BOTAN_HAS_KYBER) || defined(BOTAN_HAS_KYBER_90S)
1893
      void bench_kyber(const std::string& provider, std::chrono::milliseconds msec) {
1✔
1894
         const Botan::KyberMode::Mode all_modes[] = {
1✔
1895
            Botan::KyberMode::Kyber512,
1896
            Botan::KyberMode::Kyber512_90s,
1897
            Botan::KyberMode::Kyber768,
1898
            Botan::KyberMode::Kyber768_90s,
1899
            Botan::KyberMode::Kyber1024,
1900
            Botan::KyberMode::Kyber1024_90s,
1901
         };
1902

1903
         for(auto modet : all_modes) {
7✔
1904
            Botan::KyberMode mode(modet);
6✔
1905

1906
   #if !defined(BOTAN_HAS_KYBER)
1907
            if(mode.is_modern())
1908
               continue;
1909
   #endif
1910

1911
   #if !defined(BOTAN_HAS_KYBER_90S)
1912
            if(mode.is_90s())
1913
               continue;
1914
   #endif
1915

1916
            auto keygen_timer = make_timer(mode.to_string(), provider, "keygen");
15✔
1917

1918
            auto key = keygen_timer->run([&] { return Botan::Kyber_PrivateKey(rng(), mode); });
12✔
1919

1920
            record_result(keygen_timer);
6✔
1921

1922
            bench_pk_kem(key, mode.to_string(), provider, "KDF2(SHA-256)", msec);
12✔
1923
         }
6✔
1924
      }
1✔
1925
#endif
1926

1927
#if defined(BOTAN_HAS_DILITHIUM) || defined(BOTAN_HAS_DILITHIUM_AES)
1928
      void bench_dilithium(const std::string& provider, std::chrono::milliseconds msec) {
1✔
1929
         const Botan::DilithiumMode::Mode all_modes[] = {Botan::DilithiumMode::Dilithium4x4,
1✔
1930
                                                         Botan::DilithiumMode::Dilithium4x4_AES,
1931
                                                         Botan::DilithiumMode::Dilithium6x5,
1932
                                                         Botan::DilithiumMode::Dilithium6x5_AES,
1933
                                                         Botan::DilithiumMode::Dilithium8x7,
1934
                                                         Botan::DilithiumMode::Dilithium8x7_AES};
1935

1936
         for(auto modet : all_modes) {
7✔
1937
            Botan::DilithiumMode mode(modet);
6✔
1938

1939
   #if !defined(BOTAN_HAS_DILITHIUM)
1940
            if(mode.is_modern())
1941
               continue;
1942
   #endif
1943

1944
   #if !defined(BOTAN_HAS_DILITHIUM_AES)
1945
            if(mode.is_aes())
1946
               continue;
1947
   #endif
1948

1949
            auto keygen_timer = make_timer(mode.to_string(), provider, "keygen");
18✔
1950

1951
            auto key = keygen_timer->run([&] { return Botan::Dilithium_PrivateKey(rng(), mode); });
12✔
1952

1953
            record_result(keygen_timer);
6✔
1954

1955
            bench_pk_sig(key, mode.to_string(), provider, "", msec);
12✔
1956
         }
6✔
1957
      }
1✔
1958
#endif
1959

1960
#if defined(BOTAN_HAS_XMSS_RFC8391)
1961
      void bench_xmss(const std::string& provider, std::chrono::milliseconds msec) {
1✔
1962
         /*
1963
         We only test H10 signatures here since already they are quite slow (a
1964
         few seconds per signature). On a fast machine, H16 signatures take 1-2
1965
         minutes to generate and H20 signatures take 5-10 minutes to generate
1966
         */
1967
         std::vector<std::string> xmss_params{
1✔
1968
            "XMSS-SHA2_10_256",
1969
            "XMSS-SHAKE_10_256",
1970
            "XMSS-SHA2_10_512",
1971
            "XMSS-SHAKE_10_512",
1972
         };
5✔
1973

1974
         for(std::string params : xmss_params) {
1✔
1975
            auto keygen_timer = make_timer(params, provider, "keygen");
2✔
1976

1977
            std::unique_ptr<Botan::Private_Key> key(
1✔
1978
               keygen_timer->run([&] { return Botan::create_private_key("XMSS", rng(), params); }));
2✔
1979

1980
            record_result(keygen_timer);
1✔
1981
            if(bench_pk_sig(*key, params, provider, "", msec) == 1) {
1✔
1982
               break;
1983
            }
1984
         }
2✔
1985
      }
1✔
1986
#endif
1987

1988
#if defined(BOTAN_HAS_ZFEC)
1989
      void bench_zfec(std::chrono::milliseconds msec) {
1✔
1990
         const size_t k = 4;
1✔
1991
         const size_t n = 16;
1✔
1992

1993
         Botan::ZFEC zfec(k, n);
1✔
1994

1995
         const size_t share_size = 256 * 1024;
1✔
1996

1997
         std::vector<uint8_t> input(share_size * k);
1✔
1998
         rng().randomize(input.data(), input.size());
1✔
1999

2000
         std::vector<uint8_t> output(share_size * n);
1✔
2001

2002
         auto enc_fn = [&](size_t share, const uint8_t buf[], size_t len) {
17✔
2003
            std::memcpy(&output[share * share_size], buf, len);
16✔
2004
         };
1✔
2005

2006
         auto enc_timer =
1✔
2007
            make_timer("zfec " + std::to_string(k) + "/" + std::to_string(n), input.size(), "encode", "", input.size());
2✔
2008

2009
         enc_timer->run_until_elapsed(msec, [&]() { zfec.encode(input.data(), input.size(), enc_fn); });
3✔
2010

2011
         record_result(enc_timer);
1✔
2012

2013
         auto dec_timer =
1✔
2014
            make_timer("zfec " + std::to_string(k) + "/" + std::to_string(n), input.size(), "decode", "", input.size());
2✔
2015

2016
         std::map<size_t, const uint8_t*> shares;
1✔
2017
         for(size_t i = 0; i != n; ++i) {
17✔
2018
            shares[i] = &output[share_size * i];
16✔
2019
         }
2020

2021
         // remove data shares to make decoding maximally expensive:
2022
         while(shares.size() != k) {
13✔
2023
            shares.erase(shares.begin());
12✔
2024
         }
2025

2026
         std::vector<uint8_t> recovered(share_size * k);
1✔
2027

2028
         auto dec_fn = [&](size_t share, const uint8_t buf[], size_t len) {
5✔
2029
            std::memcpy(&recovered[share * share_size], buf, len);
4✔
2030
         };
1✔
2031

2032
         dec_timer->run_until_elapsed(msec, [&]() { zfec.decode_shares(shares, share_size, dec_fn); });
3✔
2033

2034
         record_result(dec_timer);
1✔
2035

2036
         if(recovered != input) {
1✔
2037
            error_output() << "ZFEC recovery failed\n";
×
2038
         }
2039
      }
4✔
2040

2041
#endif
2042

2043
#if defined(BOTAN_HAS_POLY_DBL)
2044
      void bench_poly_dbl(std::chrono::milliseconds msec) {
1✔
2045
         for(size_t sz : {8, 16, 24, 32, 64, 128}) {
7✔
2046
            auto be_timer = make_timer("poly_dbl_be_" + std::to_string(sz));
12✔
2047
            auto le_timer = make_timer("poly_dbl_le_" + std::to_string(sz));
12✔
2048

2049
            std::vector<uint8_t> buf(sz);
6✔
2050
            rng().randomize(buf.data(), sz);
6✔
2051

2052
            be_timer->run_until_elapsed(msec, [&]() { Botan::poly_double_n(buf.data(), buf.data(), sz); });
14,463✔
2053
            le_timer->run_until_elapsed(msec, [&]() { Botan::poly_double_n_le(buf.data(), buf.data(), sz); });
16,656✔
2054

2055
            record_result(be_timer);
6✔
2056
            record_result(le_timer);
6✔
2057
         }
6✔
2058
      }
1✔
2059
#endif
2060

2061
#if defined(BOTAN_HAS_BCRYPT)
2062

2063
      void bench_bcrypt() {
1✔
2064
         const std::string password = "not a very good password";
1✔
2065

2066
         for(uint8_t work_factor = 4; work_factor <= 14; ++work_factor) {
12✔
2067
            auto timer = make_timer("bcrypt wf=" + std::to_string(work_factor));
22✔
2068

2069
            timer->run([&] { Botan::generate_bcrypt(password, rng(), work_factor); });
22✔
2070

2071
            record_result(timer);
11✔
2072
         }
11✔
2073
      }
1✔
2074
#endif
2075

2076
#if defined(BOTAN_HAS_PASSHASH9)
2077

2078
      void bench_passhash9() {
1✔
2079
         const std::string password = "not a very good password";
1✔
2080

2081
         for(uint8_t alg = 0; alg <= 4; ++alg) {
6✔
2082
            if(Botan::is_passhash9_alg_supported(alg) == false) {
5✔
2083
               continue;
×
2084
            }
2085

2086
            for(auto work_factor : {10, 15}) {
15✔
2087
               auto timer = make_timer("passhash9 alg=" + std::to_string(alg) + " wf=" + std::to_string(work_factor));
30✔
2088

2089
               timer->run([&] { Botan::generate_passhash9(password, rng(), static_cast<uint8_t>(work_factor), alg); });
20✔
2090

2091
               record_result(timer);
10✔
2092
            }
10✔
2093
         }
2094
      }
1✔
2095
#endif
2096

2097
#if defined(BOTAN_HAS_SCRYPT)
2098

2099
      void bench_scrypt(const std::string& /*provider*/, std::chrono::milliseconds msec) {
1✔
2100
         auto pwdhash_fam = Botan::PasswordHashFamily::create_or_throw("Scrypt");
1✔
2101

2102
         for(size_t N : {8192, 16384, 32768, 65536}) {
5✔
2103
            for(size_t r : {1, 8, 16}) {
16✔
2104
               for(size_t p : {1}) {
12✔
2105
                  auto pwdhash = pwdhash_fam->from_params(N, r, p);
12✔
2106

2107
                  auto scrypt_timer =
12✔
2108
                     make_timer("scrypt-" + std::to_string(N) + "-" + std::to_string(r) + "-" + std::to_string(p) +
24✔
2109
                                " (" + std::to_string(pwdhash->total_memory_usage() / (1024 * 1024)) + " MiB)");
60✔
2110

2111
                  uint8_t out[64];
12✔
2112
                  uint8_t salt[8];
12✔
2113
                  rng().randomize(salt, sizeof(salt));
12✔
2114

2115
                  while(scrypt_timer->under(msec)) {
24✔
2116
                     scrypt_timer->run([&] {
12✔
2117
                        pwdhash->derive_key(out, sizeof(out), "password", 8, salt, sizeof(salt));
12✔
2118

2119
                        Botan::copy_mem(salt, out, 8);
12✔
2120
                     });
12✔
2121
                  }
2122

2123
                  record_result(scrypt_timer);
12✔
2124

2125
                  if(scrypt_timer->events() == 1) {
12✔
2126
                     break;
2127
                  }
2128
               }
24✔
2129
            }
2130
         }
2131
      }
1✔
2132

2133
#endif
2134

2135
#if defined(BOTAN_HAS_ARGON2)
2136

2137
      void bench_argon2(const std::string& /*provider*/, std::chrono::milliseconds msec) {
1✔
2138
         auto pwhash_fam = Botan::PasswordHashFamily::create_or_throw("Argon2id");
1✔
2139

2140
         for(size_t M : {8 * 1024, 64 * 1024, 256 * 1024}) {
4✔
2141
            for(size_t t : {1, 4}) {
9✔
2142
               for(size_t p : {1, 4}) {
18✔
2143
                  auto pwhash = pwhash_fam->from_params(M, t, p);
12✔
2144
                  auto timer = make_timer(pwhash->to_string());
36✔
2145

2146
                  uint8_t out[64];
12✔
2147
                  uint8_t salt[16];
12✔
2148
                  rng().randomize(salt, sizeof(salt));
12✔
2149

2150
                  while(timer->under(msec)) {
24✔
2151
                     timer->run([&] { pwhash->derive_key(out, sizeof(out), "password", 8, salt, sizeof(salt)); });
24✔
2152
                  }
2153

2154
                  record_result(timer);
12✔
2155
               }
24✔
2156
            }
2157
         }
2158
      }
1✔
2159

2160
#endif
2161
};
2162

2163
BOTAN_REGISTER_COMMAND("speed", Speed);
35✔
2164

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