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

randombit / botan / 5454043422

04 Jul 2023 11:13AM UTC coverage: 91.659% (-0.07%) from 91.732%
5454043422

Pull #3609

github

web-flow
Merge 5741e183c into cf8d8a6ca
Pull Request #3609: [TLS 1.3] Hybrid PQ/T key establishment

78635 of 85791 relevant lines covered (91.66%)

12300857.61 hits per line

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

95.02
/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/fmt.h>
24
#include <botan/internal/os_utils.h>
25
#include <botan/internal/timer.h>
26

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

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

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

40
#if defined(BOTAN_HAS_HASH)
41
   #include <botan/hash.h>
42
#endif
43

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

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

52
#if defined(BOTAN_HAS_AUTO_SEEDING_RNG)
53
   #include <botan/auto_rng.h>
54
#endif
55

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

123
#if defined(BOTAN_HAS_SPHINCS_PLUS_WITH_SHA2) || defined(BOTAN_HAS_SPHINCS_PLUS_WITH_SHAKE)
124
   #include <botan/sphincsplus.h>
125
#endif
126

127
#if defined(BOTAN_HAS_ECDSA)
128
   #include <botan/ecdsa.h>
129
#endif
130

131
#if defined(BOTAN_HAS_BCRYPT)
132
   #include <botan/bcrypt.h>
133
#endif
134

135
#if defined(BOTAN_HAS_PASSHASH9)
136
   #include <botan/passhash9.h>
137
#endif
138

139
#if defined(BOTAN_HAS_PASSWORD_HASHING)
140
   #include <botan/pwdhash.h>
141
#endif
142

143
#if defined(BOTAN_HAS_ZFEC)
144
   #include <botan/zfec.h>
145
#endif
146

147
namespace Botan_CLI {
148

149
using Botan::Timer;
150

151
namespace {
152

153
class JSON_Output final {
2✔
154
   public:
155
      void add(const Timer& timer) { m_results.push_back(timer); }
2✔
156

157
      std::string print() const {
1✔
158
         std::ostringstream out;
1✔
159

160
         out << "[\n";
1✔
161

162
         for(size_t i = 0; i != m_results.size(); ++i) {
3✔
163
            const Timer& t = m_results[i];
2✔
164

165
            out << "{"
2✔
166
                << "\"algo\": \"" << t.get_name() << "\", "
2✔
167
                << "\"op\": \"" << t.doing() << "\", "
4✔
168
                << "\"events\": " << t.events() << ", ";
6✔
169

170
            if(t.cycles_consumed() > 0) {
4✔
171
               out << "\"cycles\": " << t.cycles_consumed() << ", ";
4✔
172
            }
173

174
            if(t.buf_size() > 0) {
2✔
175
               out << "\"bps\": " << static_cast<uint64_t>(t.events() / (t.value() / 1000000000.0)) << ", ";
2✔
176
               out << "\"buf_size\": " << t.buf_size() << ", ";
2✔
177
            }
178

179
            out << "\"nanos\": " << t.value() << "}";
2✔
180

181
            if(i != m_results.size() - 1) {
2✔
182
               out << ",";
1✔
183
            }
184

185
            out << "\n";
2✔
186
         }
187
         out << "]\n";
1✔
188

189
         return out.str();
2✔
190
      }
1✔
191

192
   private:
193
      std::vector<Timer> m_results;
194
};
195

196
class Summary final {
1✔
197
   public:
198
      Summary() = default;
1✔
199

200
      void add(const Timer& t) {
2✔
201
         if(t.buf_size() == 0) {
2✔
202
            m_ops_entries.push_back(t);
×
203
         } else {
204
            m_bps_entries[std::make_pair(t.doing(), t.get_name())].push_back(t);
4✔
205
         }
206
      }
2✔
207

208
      std::string print() {
1✔
209
         const size_t name_padding = 35;
1✔
210
         const size_t op_name_padding = 16;
1✔
211
         const size_t op_padding = 16;
1✔
212

213
         std::ostringstream result_ss;
1✔
214
         result_ss << std::fixed;
1✔
215

216
         if(!m_bps_entries.empty()) {
1✔
217
            result_ss << "\n";
1✔
218

219
            // add table header
220
            result_ss << std::setw(name_padding) << std::left << "algo" << std::setw(op_name_padding) << std::left
1✔
221
                      << "operation";
1✔
222

223
            for(const Timer& t : m_bps_entries.begin()->second) {
2✔
224
               result_ss << std::setw(op_padding) << std::right << (std::to_string(t.buf_size()) + " bytes");
2✔
225
            }
226
            result_ss << "\n";
1✔
227

228
            // add table entries
229
            for(const auto& entry : m_bps_entries) {
3✔
230
               if(entry.second.empty()) {
2✔
231
                  continue;
×
232
               }
233

234
               result_ss << std::setw(name_padding) << std::left << (entry.first.second) << std::setw(op_name_padding)
2✔
235
                         << std::left << (entry.first.first);
2✔
236

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

246
               result_ss << "\n";
2✔
247
            }
248

249
            result_ss << "\n[results are the number of 1000s bytes processed per second]\n";
1✔
250
         }
251

252
         if(!m_ops_entries.empty()) {
1✔
253
            result_ss << std::setprecision(6) << "\n";
×
254

255
            // sort entries
256
            std::sort(m_ops_entries.begin(), m_ops_entries.end());
×
257

258
            // add table header
259
            result_ss << std::setw(name_padding) << std::left << "algo" << std::setw(op_name_padding) << std::left
×
260
                      << "operation" << std::setw(op_padding) << std::right << "sec/op" << std::setw(op_padding)
×
261
                      << std::right << "op/sec"
×
262
                      << "\n";
×
263

264
            // add table entries
265
            for(const Timer& entry : m_ops_entries) {
×
266
               result_ss << std::setw(name_padding) << std::left << entry.get_name() << std::setw(op_name_padding)
×
267
                         << std::left << entry.doing() << std::setw(op_padding) << std::right
×
268
                         << entry.seconds_per_event() << std::setw(op_padding) << std::right
×
269
                         << entry.events_per_second() << "\n";
×
270
            }
271
         }
272

273
         return result_ss.str();
2✔
274
      }
1✔
275

276
   private:
277
      std::map<std::pair<std::string, std::string>, std::vector<Timer>> m_bps_entries;
278
      std::vector<Timer> m_ops_entries;
279
};
280

281
std::vector<size_t> unique_buffer_sizes(const std::string& cmdline_arg) {
34✔
282
   const size_t MAX_BUF_SIZE = 64 * 1024 * 1024;
34✔
283

284
   std::set<size_t> buf;
34✔
285
   for(const std::string& size_str : Command::split_on(cmdline_arg, ',')) {
66✔
286
      size_t x = 0;
35✔
287
      try {
35✔
288
         size_t converted = 0;
35✔
289
         x = static_cast<size_t>(std::stoul(size_str, &converted, 0));
35✔
290

291
         if(converted != size_str.size()) {
34✔
292
            throw CLI_Usage_Error("Invalid integer");
×
293
         }
294
      } catch(std::exception&) {
1✔
295
         throw CLI_Usage_Error("Invalid integer value '" + size_str + "' for option buf-size");
3✔
296
      }
1✔
297

298
      if(x == 0) {
34✔
299
         throw CLI_Usage_Error("Cannot have a zero-sized buffer");
3✔
300
      }
301

302
      if(x > MAX_BUF_SIZE) {
33✔
303
         throw CLI_Usage_Error("Specified buffer size is too large");
3✔
304
      }
305

306
      buf.insert(x);
32✔
307
   }
34✔
308

309
   return std::vector<size_t>(buf.begin(), buf.end());
34✔
310
}
31✔
311

312
}  // namespace
313

314
class Speed final : public Command {
×
315
   public:
316
      Speed() :
35✔
317
            Command(
318
               "speed --msec=500 --format=default --ecc-groups= --provider= --buf-size=1024 --clear-cpuid= --cpu-clock-speed=0 --cpu-clock-ratio=1.0 *algos") {
70✔
319
      }
35✔
320

321
      static std::vector<std::string> default_benchmark_list() {
×
322
         /*
323
         This is not intended to be exhaustive: it just hits the high
324
         points of the most interesting or widely used algorithms.
325
         */
326
         // clang-format off
327
         return {
×
328
            /* Block ciphers */
329
            "AES-128",
330
            "AES-192",
331
            "AES-256",
332
            "ARIA-128",
333
            "ARIA-192",
334
            "ARIA-256",
335
            "Blowfish",
336
            "CAST-128",
337
            "Camellia-128",
338
            "Camellia-192",
339
            "Camellia-256",
340
            "DES",
341
            "TripleDES",
342
            "GOST-28147-89",
343
            "IDEA",
344
            "Noekeon",
345
            "SHACAL2",
346
            "SM4",
347
            "Serpent",
348
            "Threefish-512",
349
            "Twofish",
350

351
            /* Cipher modes */
352
            "AES-128/CBC",
353
            "AES-128/CTR-BE",
354
            "AES-128/EAX",
355
            "AES-128/OCB",
356
            "AES-128/GCM",
357
            "AES-128/XTS",
358
            "AES-128/SIV",
359

360
            "Serpent/CBC",
361
            "Serpent/CTR-BE",
362
            "Serpent/EAX",
363
            "Serpent/OCB",
364
            "Serpent/GCM",
365
            "Serpent/XTS",
366
            "Serpent/SIV",
367

368
            "ChaCha20Poly1305",
369

370
            /* Stream ciphers */
371
            "RC4",
372
            "Salsa20",
373
            "ChaCha20",
374

375
            /* Hashes */
376
            "SHA-1",
377
            "SHA-256",
378
            "SHA-512",
379
            "SHA-3(256)",
380
            "SHA-3(512)",
381
            "RIPEMD-160",
382
            "Skein-512",
383
            "Blake2b",
384
            "Whirlpool",
385

386
            /* MACs */
387
            "CMAC(AES-128)",
388
            "HMAC(SHA-256)",
389

390
            /* pubkey */
391
            "RSA",
392
            "DH",
393
            "ECDH",
394
            "ECDSA",
395
            "Ed25519",
396
            "Curve25519",
397
            "McEliece",
398
            "Kyber",
399
            "SPHINCS+"
400
         };
×
401
         // clang-format on
402
      }
403

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

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

408
      void go() override {
34✔
409
         std::chrono::milliseconds msec(get_arg_sz("msec"));
34✔
410
         const std::string provider = get_arg("provider");
34✔
411
         std::vector<std::string> ecc_groups = Command::split_on(get_arg("ecc-groups"), ',');
71✔
412
         const std::string format = get_arg("format");
34✔
413
         const std::string clock_ratio = get_arg("cpu-clock-ratio");
37✔
414
         m_clock_speed = get_arg_sz("cpu-clock-speed");
34✔
415

416
         m_clock_cycle_ratio = std::strtod(clock_ratio.c_str(), nullptr);
34✔
417

418
         /*
419
         * This argument is intended to be the ratio between the cycle counter
420
         * and the actual machine cycles. It is extremely unlikely that there is
421
         * any machine where the cycle counter increments faster than the actual
422
         * clock.
423
         */
424
         if(m_clock_cycle_ratio < 0.0 || m_clock_cycle_ratio > 1.0) {
34✔
425
            throw CLI_Usage_Error("Unlikely CPU clock ratio of " + clock_ratio);
×
426
         }
427

428
         m_clock_cycle_ratio = 1.0 / m_clock_cycle_ratio;
34✔
429

430
         if(m_clock_speed != 0 && Botan::OS::get_cpu_cycle_counter() != 0) {
34✔
431
            error_output() << "The --cpu-clock-speed option is only intended to be used on "
×
432
                              "platforms without access to a cycle counter.\n"
433
                              "Expected incorrect results\n\n";
×
434
         }
435

436
         if(format == "table") {
34✔
437
            m_summary = std::make_unique<Summary>();
1✔
438
         } else if(format == "json") {
33✔
439
            m_json = std::make_unique<JSON_Output>();
1✔
440
         } else if(format != "default") {
32✔
441
            throw CLI_Usage_Error("Unknown --format type '" + format + "'");
×
442
         }
443

444
#if defined(BOTAN_HAS_ECC_GROUP)
445
         if(ecc_groups.empty()) {
34✔
446
            ecc_groups = {"secp256r1", "brainpool256r1", "secp384r1", "brainpool384r1", "secp521r1", "brainpool512r1"};
272✔
447
         } else if(ecc_groups.size() == 1 && ecc_groups[0] == "all") {
×
448
            auto all = Botan::EC_Group::known_named_groups();
×
449
            ecc_groups.assign(all.begin(), all.end());
×
450
         }
×
451
#endif
452

453
         std::vector<std::string> algos = get_arg_list("algos");
37✔
454

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

457
         for(const std::string& cpuid_to_clear : Command::split_on(get_arg("clear-cpuid"), ',')) {
32✔
458
            auto bits = Botan::CPUID::bit_from_string(cpuid_to_clear);
1✔
459
            if(bits.empty()) {
1✔
460
               error_output() << "Warning don't know CPUID flag '" << cpuid_to_clear << "'\n";
1✔
461
            }
462

463
            for(auto bit : bits) {
1✔
464
               Botan::CPUID::clear_cpuid_bit(bit);
×
465
            }
466
         }
32✔
467

468
         if(verbose() || m_summary) {
31✔
469
            output() << Botan::version_string() << "\n"
2✔
470
                     << "CPUID: " << Botan::CPUID::to_string() << "\n\n";
3✔
471
         }
472

473
         const bool using_defaults = (algos.empty());
31✔
474
         if(using_defaults) {
31✔
475
            algos = default_benchmark_list();
×
476
         }
477

478
         for(const auto& algo : algos) {
82✔
479
            using namespace std::placeholders;
51✔
480

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

634
#if defined(BOTAN_HAS_DL_GROUP)
635
            else if(algo == "modexp") {
18✔
636
               bench_modexp(msec);
1✔
637
            }
638
#endif
639

640
#if defined(BOTAN_HAS_BIGINT)
641
            else if(algo == "mp_mul") {
17✔
642
               bench_mp_mul(msec);
1✔
643
            } else if(algo == "mp_div") {
16✔
644
               bench_mp_div(msec);
1✔
645
            } else if(algo == "mp_div10") {
15✔
646
               bench_mp_div10(msec);
1✔
647
            }
648
#endif
649

650
#if defined(BOTAN_HAS_NUMBERTHEORY)
651
            else if(algo == "primality_test") {
14✔
652
               bench_primality_tests(msec);
1✔
653
            } else if(algo == "random_prime") {
13✔
654
               bench_random_prime(msec);
1✔
655
            } else if(algo == "inverse_mod") {
12✔
656
               bench_inverse_mod(msec);
1✔
657
            } else if(algo == "bn_redc") {
11✔
658
               bench_bn_redc(msec);
1✔
659
            } else if(algo == "nistp_redc") {
10✔
660
               bench_nistp_redc(msec);
1✔
661
            }
662
#endif
663

664
#if defined(BOTAN_HAS_FPE_FE1)
665
            else if(algo == "fpe_fe1") {
9✔
666
               bench_fpe_fe1(msec);
1✔
667
            }
668
#endif
669

670
#if defined(BOTAN_HAS_RFC3394_KEYWRAP)
671
            else if(algo == "rfc3394") {
8✔
672
               bench_rfc3394(msec);
1✔
673
            }
674
#endif
675

676
#if defined(BOTAN_HAS_ECC_GROUP)
677
            else if(algo == "ecc_mult") {
7✔
678
               bench_ecc_mult(ecc_groups, msec);
1✔
679
            } else if(algo == "ecc_ops") {
6✔
680
               bench_ecc_ops(ecc_groups, msec);
1✔
681
            } else if(algo == "ecc_init") {
5✔
682
               bench_ecc_init(ecc_groups, msec);
1✔
683
            } else if(algo == "os2ecp") {
4✔
684
               bench_os2ecp(ecc_groups, msec);
1✔
685
            }
686
#endif
687
#if defined(BOTAN_HAS_EC_HASH_TO_CURVE)
688
            else if(algo == "ec_h2c") {
3✔
689
               bench_ec_h2c(msec);
1✔
690
            }
691
#endif
692
            else if(algo == "RNG") {
2✔
693
#if defined(BOTAN_HAS_AUTO_SEEDING_RNG)
694
               Botan::AutoSeeded_RNG auto_rng;
1✔
695
               bench_rng(auto_rng, "AutoSeeded_RNG (with reseed)", msec, buf_sizes);
1✔
696
#endif
697

698
#if defined(BOTAN_HAS_SYSTEM_RNG)
699
               bench_rng(Botan::system_rng(), "System_RNG", msec, buf_sizes);
1✔
700
#endif
701

702
#if defined(BOTAN_HAS_PROCESSOR_RNG)
703
               if(Botan::Processor_RNG::available()) {
1✔
704
                  Botan::Processor_RNG hwrng;
1✔
705
                  bench_rng(hwrng, "Processor_RNG", msec, buf_sizes);
2✔
706
               }
1✔
707
#endif
708

709
#if defined(BOTAN_HAS_HMAC_DRBG)
710
               for(std::string hash : {"SHA-256", "SHA-384", "SHA-512"}) {
4✔
711
                  Botan::HMAC_DRBG hmac_drbg(hash);
3✔
712
                  bench_rng(hmac_drbg, hmac_drbg.name(), msec, buf_sizes);
3✔
713
               }
3✔
714
#endif
715

716
#if defined(BOTAN_HAS_CHACHA_RNG)
717
               // Provide a dummy seed
718
               Botan::ChaCha_RNG chacha_rng(Botan::secure_vector<uint8_t>(32));
1✔
719
               bench_rng(chacha_rng, "ChaCha_RNG", msec, buf_sizes);
1✔
720
#endif
721

722
            } else if(algo == "entropy") {
2✔
723
               bench_entropy_sources(msec);
1✔
724
            } else {
725
               if(verbose() || !using_defaults) {
×
726
                  error_output() << "Unknown algorithm '" << algo << "'\n";
×
727
               }
728
            }
45✔
729
         }
730

731
         if(m_json) {
31✔
732
            output() << m_json->print();
3✔
733
         }
734
         if(m_summary) {
31✔
735
            output() << m_summary->print() << "\n";
3✔
736
         }
737

738
         if(verbose() && m_clock_speed == 0 && m_cycles_consumed > 0 && m_ns_taken > 0) {
31✔
739
            const double seconds = static_cast<double>(m_ns_taken) / 1000000000;
×
740
            const double Hz = static_cast<double>(m_cycles_consumed) / seconds;
×
741
            const double MHz = Hz / 1000000;
×
742
            output() << "\nEstimated clock speed " << MHz << " MHz\n";
×
743
         }
744
      }
40✔
745

746
   private:
747
      size_t m_clock_speed = 0;
748
      double m_clock_cycle_ratio = 0.0;
749
      uint64_t m_cycles_consumed = 0;
750
      uint64_t m_ns_taken = 0;
751
      std::unique_ptr<Summary> m_summary;
752
      std::unique_ptr<JSON_Output> m_json;
753

754
      void record_result(const std::unique_ptr<Timer>& t) {
470✔
755
         m_ns_taken += t->value();
470✔
756
         m_cycles_consumed += t->cycles_consumed();
470✔
757
         if(m_json) {
470✔
758
            m_json->add(*t);
2✔
759
         } else {
760
            output() << t->to_string() << std::flush;
468✔
761
            if(m_summary) {
468✔
762
               m_summary->add(*t);
2✔
763
            }
764
         }
765
      }
470✔
766

767
      template <typename T>
768
      using bench_fn = std::function<void(T&, std::string, std::chrono::milliseconds, const std::vector<size_t>&)>;
769

770
      template <typename T>
771
      void bench_providers_of(const std::string& algo,
7✔
772
                              const std::string& provider, /* user request, if any */
773
                              const std::chrono::milliseconds runtime,
774
                              const std::vector<size_t>& buf_sizes,
775
                              bench_fn<T> bench_one) {
776
         for(const auto& prov : T::providers(algo)) {
14✔
777
            if(provider.empty() || provider == prov) {
7✔
778
               auto p = T::create(algo, prov);
7✔
779

780
               if(p) {
7✔
781
                  bench_one(*p, prov, runtime, buf_sizes);
14✔
782
               }
783
            }
7✔
784
         }
785
      }
7✔
786

787
      std::unique_ptr<Timer> make_timer(const std::string& name,
474✔
788
                                        uint64_t event_mult = 1,
789
                                        const std::string& what = "",
790
                                        const std::string& provider = "",
791
                                        size_t buf_size = 0) {
792
         return std::make_unique<Timer>(name, provider, what, event_mult, buf_size, m_clock_cycle_ratio, m_clock_speed);
252✔
793
      }
794

795
      std::unique_ptr<Timer> make_timer(const std::string& algo, const std::string& provider, const std::string& what) {
215✔
796
         return make_timer(algo, 1, what, provider, 0);
215✔
797
      }
798

799
#if defined(BOTAN_HAS_BLOCK_CIPHER)
800
      void bench_block_cipher(Botan::BlockCipher& cipher,
4✔
801
                              const std::string& provider,
802
                              std::chrono::milliseconds runtime,
803
                              const std::vector<size_t>& buf_sizes) {
804
         auto ks_timer = make_timer(cipher.name(), provider, "key schedule");
8✔
805

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

809
         const size_t bs = cipher.block_size();
4✔
810
         std::set<size_t> buf_sizes_in_blocks;
4✔
811
         for(size_t buf_size : buf_sizes) {
9✔
812
            if(buf_size % bs == 0) {
5✔
813
               buf_sizes_in_blocks.insert(buf_size);
10✔
814
            } else {
815
               buf_sizes_in_blocks.insert(buf_size + bs - (buf_size % bs));
×
816
            }
817
         }
818

819
         for(size_t buf_size : buf_sizes_in_blocks) {
9✔
820
            std::vector<uint8_t> buffer(buf_size);
5✔
821
            const size_t blocks = buf_size / bs;
5✔
822

823
            auto encrypt_timer = make_timer(cipher.name(), buffer.size(), "encrypt", provider, buf_size);
10✔
824
            auto decrypt_timer = make_timer(cipher.name(), buffer.size(), "decrypt", provider, buf_size);
10✔
825

826
            encrypt_timer->run_until_elapsed(runtime, [&]() { cipher.encrypt_n(&buffer[0], &buffer[0], blocks); });
2,065✔
827
            record_result(encrypt_timer);
5✔
828

829
            decrypt_timer->run_until_elapsed(runtime, [&]() { cipher.decrypt_n(&buffer[0], &buffer[0], blocks); });
2,002✔
830
            record_result(decrypt_timer);
5✔
831
         }
10✔
832
      }
8✔
833
#endif
834

835
#if defined(BOTAN_HAS_STREAM_CIPHER)
836
      void bench_stream_cipher(Botan::StreamCipher& cipher,
1✔
837
                               const std::string& provider,
838
                               const std::chrono::milliseconds runtime,
839
                               const std::vector<size_t>& buf_sizes) {
840
         for(auto buf_size : buf_sizes) {
2✔
841
            Botan::secure_vector<uint8_t> buffer = rng().random_vec(buf_size);
1✔
842

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

845
            const Botan::SymmetricKey key(rng(), cipher.maximum_keylength());
1✔
846
            cipher.set_key(key);
1✔
847

848
            if(cipher.valid_iv_length(12)) {
1✔
849
               const Botan::InitializationVector iv(rng(), 12);
1✔
850
               cipher.set_iv(iv.begin(), iv.size());
1✔
851
            }
1✔
852

853
            while(encrypt_timer->under(runtime)) {
622✔
854
               encrypt_timer->run([&]() { cipher.encipher(buffer); });
1,242✔
855
            }
856

857
            record_result(encrypt_timer);
1✔
858

859
            if(verbose()) {
1✔
860
               auto ks_timer = make_timer(cipher.name(), buffer.size(), "write_keystream", provider, buf_size);
×
861

862
               while(ks_timer->under(runtime)) {
×
863
                  ks_timer->run([&]() { cipher.write_keystream(buffer.data(), buffer.size()); });
×
864
               }
865
               record_result(ks_timer);
×
866
            }
×
867
         }
2✔
868
      }
1✔
869
#endif
870

871
#if defined(BOTAN_HAS_HASH)
872
      void bench_hash(Botan::HashFunction& hash,
1✔
873
                      const std::string& provider,
874
                      const std::chrono::milliseconds runtime,
875
                      const std::vector<size_t>& buf_sizes) {
876
         std::vector<uint8_t> output(hash.output_length());
1✔
877

878
         for(auto buf_size : buf_sizes) {
2✔
879
            Botan::secure_vector<uint8_t> buffer = rng().random_vec(buf_size);
1✔
880

881
            auto timer = make_timer(hash.name(), buffer.size(), "hash", provider, buf_size);
2✔
882
            timer->run_until_elapsed(runtime, [&]() {
1✔
883
               hash.update(buffer);
106✔
884
               hash.final(output.data());
106✔
885
            });
106✔
886
            record_result(timer);
1✔
887
         }
2✔
888
      }
1✔
889
#endif
890

891
#if defined(BOTAN_HAS_MAC)
892
      void bench_mac(Botan::MessageAuthenticationCode& mac,
1✔
893
                     const std::string& provider,
894
                     const std::chrono::milliseconds runtime,
895
                     const std::vector<size_t>& buf_sizes) {
896
         std::vector<uint8_t> output(mac.output_length());
1✔
897

898
         for(auto buf_size : buf_sizes) {
2✔
899
            Botan::secure_vector<uint8_t> buffer = rng().random_vec(buf_size);
1✔
900

901
            const Botan::SymmetricKey key(rng(), mac.maximum_keylength());
1✔
902
            mac.set_key(key);
1✔
903
            mac.start(nullptr, 0);
1✔
904

905
            auto timer = make_timer(mac.name(), buffer.size(), "mac", provider, buf_size);
2✔
906
            timer->run_until_elapsed(runtime, [&]() { mac.update(buffer); });
117✔
907
            timer->run([&]() { mac.final(output.data()); });
2✔
908
            record_result(timer);
1✔
909
         }
3✔
910
      }
1✔
911
#endif
912

913
#if defined(BOTAN_HAS_CIPHER_MODES)
914
      void bench_cipher_mode(Botan::Cipher_Mode& enc,
1✔
915
                             Botan::Cipher_Mode& dec,
916
                             const std::chrono::milliseconds runtime,
917
                             const std::vector<size_t>& buf_sizes) {
918
         auto ks_timer = make_timer(enc.name(), enc.provider(), "key schedule");
2✔
919

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

922
         ks_timer->run([&]() { enc.set_key(key); });
2✔
923
         ks_timer->run([&]() { dec.set_key(key); });
2✔
924

925
         record_result(ks_timer);
1✔
926

927
         for(auto buf_size : buf_sizes) {
2✔
928
            Botan::secure_vector<uint8_t> buffer = rng().random_vec(buf_size);
1✔
929

930
            auto encrypt_timer = make_timer(enc.name(), buffer.size(), "encrypt", enc.provider(), buf_size);
2✔
931
            auto decrypt_timer = make_timer(dec.name(), buffer.size(), "decrypt", dec.provider(), buf_size);
2✔
932

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

935
            if(buf_size >= enc.minimum_final_size()) {
1✔
936
               while(encrypt_timer->under(runtime) && decrypt_timer->under(runtime)) {
67✔
937
                  // Must run in this order, or AEADs will reject the ciphertext
938
                  encrypt_timer->run([&]() {
66✔
939
                     enc.start(iv);
66✔
940
                     enc.finish(buffer);
66✔
941
                  });
66✔
942

943
                  decrypt_timer->run([&]() {
66✔
944
                     dec.start(iv);
66✔
945
                     dec.finish(buffer);
66✔
946
                  });
66✔
947

948
                  if(!iv.empty()) {
66✔
949
                     iv[iv.size() - 1] += 1;
66✔
950
                  }
951
               }
952
            }
953

954
            record_result(encrypt_timer);
1✔
955
            record_result(decrypt_timer);
1✔
956
         }
2✔
957
      }
1✔
958
#endif
959

960
      void bench_rng(Botan::RandomNumberGenerator& rng,
7✔
961
                     const std::string& rng_name,
962
                     const std::chrono::milliseconds runtime,
963
                     const std::vector<size_t>& buf_sizes) {
964
         for(auto buf_size : buf_sizes) {
14✔
965
            Botan::secure_vector<uint8_t> buffer(buf_size);
7✔
966

967
#if defined(BOTAN_HAS_SYSTEM_RNG)
968
            rng.reseed_from_rng(Botan::system_rng(), 256);
7✔
969
#endif
970

971
            auto timer = make_timer(rng_name, buffer.size(), "generate", "", buf_size);
7✔
972
            timer->run_until_elapsed(runtime, [&]() { rng.randomize(buffer.data(), buffer.size()); });
741✔
973
            record_result(timer);
7✔
974
         }
14✔
975
      }
7✔
976

977
      void bench_entropy_sources(const std::chrono::milliseconds /*unused*/) {
1✔
978
         Botan::Entropy_Sources& srcs = Botan::Entropy_Sources::global_sources();
1✔
979

980
         for(auto src : srcs.enabled_sources()) {
5✔
981
            size_t entropy_bits = 0;
4✔
982
            Botan_Tests::SeedCapturing_RNG rng;
4✔
983

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

987
            size_t compressed_size = 0;
4✔
988

989
#if defined(BOTAN_HAS_ZLIB)
990
            auto comp = Botan::Compression_Algorithm::create("zlib");
4✔
991

992
            if(comp) {
4✔
993
               Botan::secure_vector<uint8_t> compressed;
4✔
994
               compressed.assign(rng.seed_material().begin(), rng.seed_material().end());
4✔
995
               comp->start(9);
4✔
996
               comp->finish(compressed);
4✔
997

998
               compressed_size = compressed.size();
4✔
999
            }
4✔
1000
#endif
1001

1002
            std::ostringstream msg;
4✔
1003

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

1007
            if(compressed_size > 0) {
4✔
1008
               msg << " output compressed to " << compressed_size << " bytes";
4✔
1009
            }
1010

1011
            msg << " total samples " << rng.samples() << "\n";
4✔
1012

1013
            timer->set_custom_msg(msg.str());
8✔
1014

1015
            record_result(timer);
4✔
1016
         }
13✔
1017
      }
1✔
1018

1019
#if defined(BOTAN_HAS_ECC_GROUP)
1020
      void bench_ecc_ops(const std::vector<std::string>& groups, const std::chrono::milliseconds runtime) {
1✔
1021
         for(const std::string& group_name : groups) {
7✔
1022
            const Botan::EC_Group ec_group(group_name);
6✔
1023

1024
            auto add_timer = make_timer(group_name + " add");
15✔
1025
            auto addf_timer = make_timer(group_name + " addf");
15✔
1026
            auto dbl_timer = make_timer(group_name + " dbl");
15✔
1027

1028
            const Botan::EC_Point& base_point = ec_group.get_base_point();
6✔
1029

1030
            // create a non-affine point
1031
            const auto random_k = Botan::BigInt::from_u64(0x4E6F537465707E);
6✔
1032
            Botan::EC_Point non_affine_pt = ec_group.get_base_point() * random_k;
6✔
1033
            Botan::EC_Point pt = ec_group.get_base_point();
6✔
1034

1035
            std::vector<Botan::BigInt> ws(Botan::EC_Point::WORKSPACE_SIZE);
6✔
1036

1037
            while(add_timer->under(runtime) && addf_timer->under(runtime) && dbl_timer->under(runtime)) {
688✔
1038
               dbl_timer->run([&]() { pt.mult2(ws); });
1,364✔
1039
               add_timer->run([&]() { pt.add(non_affine_pt, ws); });
1,364✔
1040
               addf_timer->run([&]() { pt.add_affine(base_point, ws); });
1,364✔
1041
            }
1042

1043
            record_result(dbl_timer);
6✔
1044
            record_result(add_timer);
6✔
1045
            record_result(addf_timer);
6✔
1046
         }
12✔
1047
      }
1✔
1048

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

1053
            while(timer->under(runtime)) {
12✔
1054
               Botan::EC_Group::clear_registered_curve_data();
6✔
1055
               timer->run([&]() { Botan::EC_Group group(group_name); });
12✔
1056
            }
1057

1058
            record_result(timer);
6✔
1059
         }
6✔
1060
      }
1✔
1061

1062
      void bench_ecc_mult(const std::vector<std::string>& groups, const std::chrono::milliseconds runtime) {
1✔
1063
         for(const std::string& group_name : groups) {
7✔
1064
            const Botan::EC_Group ec_group(group_name);
6✔
1065

1066
            auto mult_timer = make_timer(group_name + " Montgomery ladder");
18✔
1067
            auto blinded_mult_timer = make_timer(group_name + " blinded comb");
18✔
1068
            auto blinded_var_mult_timer = make_timer(group_name + " blinded window");
18✔
1069

1070
            const Botan::EC_Point& base_point = ec_group.get_base_point();
6✔
1071

1072
            std::vector<Botan::BigInt> ws;
6✔
1073

1074
            while(mult_timer->under(runtime) && blinded_mult_timer->under(runtime) &&
12✔
1075
                  blinded_var_mult_timer->under(runtime)) {
6✔
1076
               const Botan::BigInt scalar(rng(), ec_group.get_p_bits());
6✔
1077

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

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

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

1086
               BOTAN_ASSERT_EQUAL(r1, r2, "Same point computed by Montgomery and comb");
6✔
1087
               BOTAN_ASSERT_EQUAL(r1, r3, "Same point computed by Montgomery and window");
6✔
1088
            }
12✔
1089

1090
            record_result(mult_timer);
6✔
1091
            record_result(blinded_mult_timer);
6✔
1092
            record_result(blinded_var_mult_timer);
6✔
1093
         }
6✔
1094
      }
1✔
1095

1096
      void bench_os2ecp(const std::vector<std::string>& groups, const std::chrono::milliseconds runtime) {
1✔
1097
         for(const std::string& group_name : groups) {
7✔
1098
            auto uncmp_timer = make_timer("OS2ECP uncompressed " + group_name);
18✔
1099
            auto cmp_timer = make_timer("OS2ECP compressed " + group_name);
18✔
1100

1101
            const Botan::EC_Group ec_group(group_name);
6✔
1102

1103
            while(uncmp_timer->under(runtime) && cmp_timer->under(runtime)) {
12✔
1104
               const Botan::BigInt k(rng(), 256);
6✔
1105
               const Botan::EC_Point p = ec_group.get_base_point() * k;
6✔
1106
               const std::vector<uint8_t> os_cmp = p.encode(Botan::EC_Point_Format::Compressed);
6✔
1107
               const std::vector<uint8_t> os_uncmp = p.encode(Botan::EC_Point_Format::Uncompressed);
6✔
1108

1109
               uncmp_timer->run([&]() { ec_group.OS2ECP(os_uncmp); });
12✔
1110
               cmp_timer->run([&]() { ec_group.OS2ECP(os_cmp); });
12✔
1111
            }
18✔
1112

1113
            record_result(uncmp_timer);
6✔
1114
            record_result(cmp_timer);
6✔
1115
         }
6✔
1116
      }
1✔
1117

1118
#endif
1119

1120
#if defined(BOTAN_HAS_EC_HASH_TO_CURVE)
1121
      void bench_ec_h2c(const std::chrono::milliseconds runtime) {
1✔
1122
         for(std::string group_name : {"secp256r1", "secp384r1", "secp521r1"}) {
4✔
1123
            auto h2c_ro_timer = make_timer(group_name + "-RO", "", "hash to curve");
6✔
1124
            auto h2c_nu_timer = make_timer(group_name + "-NU", "", "hash to curve");
6✔
1125

1126
            const Botan::EC_Group group(group_name);
3✔
1127

1128
            while(h2c_ro_timer->under(runtime)) {
6✔
1129
               std::vector<uint8_t> input(32);
3✔
1130

1131
               rng().randomize(input.data(), input.size());
3✔
1132

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

1136
               BOTAN_ASSERT_NOMSG(p1.on_the_curve());
3✔
1137

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

1141
               BOTAN_ASSERT_NOMSG(p2.on_the_curve());
3✔
1142
            }
6✔
1143

1144
            record_result(h2c_ro_timer);
3✔
1145
            record_result(h2c_nu_timer);
3✔
1146
         }
3✔
1147
      }
1✔
1148
#endif
1149

1150
#if defined(BOTAN_HAS_FPE_FE1)
1151

1152
      void bench_fpe_fe1(const std::chrono::milliseconds runtime) {
1✔
1153
         const auto n = Botan::BigInt::from_u64(1000000000000000);
1✔
1154

1155
         auto enc_timer = make_timer("FPE_FE1 encrypt");
2✔
1156
         auto dec_timer = make_timer("FPE_FE1 decrypt");
2✔
1157

1158
         const Botan::SymmetricKey key(rng(), 32);
1✔
1159
         const std::vector<uint8_t> tweak(8);  // 8 zeros
1✔
1160

1161
         auto x = Botan::BigInt::one();
1✔
1162

1163
         Botan::FPE_FE1 fpe_fe1(n);
1✔
1164
         fpe_fe1.set_key(key);
1✔
1165

1166
         while(enc_timer->under(runtime)) {
3✔
1167
            enc_timer->start();
2✔
1168
            x = fpe_fe1.encrypt(x, tweak.data(), tweak.size());
2✔
1169
            enc_timer->stop();
2✔
1170
         }
1171

1172
         for(size_t i = 0; i != enc_timer->events(); ++i) {
3✔
1173
            dec_timer->start();
2✔
1174
            x = fpe_fe1.decrypt(x, tweak.data(), tweak.size());
2✔
1175
            dec_timer->stop();
2✔
1176
         }
1177

1178
         BOTAN_ASSERT(x == 1, "FPE works");
1✔
1179

1180
         record_result(enc_timer);
1✔
1181
         record_result(dec_timer);
1✔
1182
      }
5✔
1183
#endif
1184

1185
#if defined(BOTAN_HAS_RFC3394_KEYWRAP)
1186

1187
      void bench_rfc3394(const std::chrono::milliseconds runtime) {
1✔
1188
         auto wrap_timer = make_timer("RFC3394 AES-256 key wrap");
3✔
1189
         auto unwrap_timer = make_timer("RFC3394 AES-256 key unwrap");
3✔
1190

1191
         const Botan::SymmetricKey kek(rng(), 32);
1✔
1192
         Botan::secure_vector<uint8_t> key(64, 0);
1✔
1193

1194
         while(wrap_timer->under(runtime)) {
31✔
1195
            wrap_timer->start();
30✔
1196
            key = Botan::rfc3394_keywrap(key, kek);
60✔
1197
            wrap_timer->stop();
30✔
1198

1199
            unwrap_timer->start();
30✔
1200
            key = Botan::rfc3394_keyunwrap(key, kek);
60✔
1201
            unwrap_timer->stop();
30✔
1202

1203
            key[0] += 1;
30✔
1204
         }
1205

1206
         record_result(wrap_timer);
1✔
1207
         record_result(unwrap_timer);
1✔
1208
      }
2✔
1209
#endif
1210

1211
#if defined(BOTAN_HAS_BIGINT)
1212

1213
      void bench_mp_mul(const std::chrono::milliseconds runtime) {
1✔
1214
         std::chrono::milliseconds runtime_per_size = runtime;
1✔
1215
         for(size_t bits : {256, 384, 512, 768, 1024, 1536, 2048, 3072, 4096}) {
10✔
1216
            auto mul_timer = make_timer("BigInt mul " + std::to_string(bits));
18✔
1217
            auto sqr_timer = make_timer("BigInt sqr " + std::to_string(bits));
18✔
1218

1219
            const Botan::BigInt y(rng(), bits);
9✔
1220
            Botan::secure_vector<Botan::word> ws;
9✔
1221

1222
            while(mul_timer->under(runtime_per_size)) {
3,889✔
1223
               Botan::BigInt x(rng(), bits);
3,880✔
1224

1225
               sqr_timer->start();
3,880✔
1226
               x.square(ws);
3,880✔
1227
               sqr_timer->stop();
3,880✔
1228

1229
               x.mask_bits(bits);
3,880✔
1230

1231
               mul_timer->start();
3,880✔
1232
               x.mul(y, ws);
3,880✔
1233
               mul_timer->stop();
3,880✔
1234
            }
3,880✔
1235

1236
            record_result(mul_timer);
9✔
1237
            record_result(sqr_timer);
9✔
1238
         }
18✔
1239
      }
1✔
1240

1241
      void bench_mp_div(const std::chrono::milliseconds runtime) {
1✔
1242
         std::chrono::milliseconds runtime_per_size = runtime;
1✔
1243

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

1248
            auto div_timer = make_timer("BigInt div " + bit_descr);
27✔
1249
            auto ct_div_timer = make_timer("BigInt ct_div " + bit_descr);
27✔
1250

1251
            Botan::BigInt y;
9✔
1252
            Botan::BigInt x;
9✔
1253
            Botan::secure_vector<Botan::word> ws;
9✔
1254

1255
            Botan::BigInt q1, r1, q2, r2;
9✔
1256

1257
            while(ct_div_timer->under(runtime_per_size)) {
42✔
1258
               x.randomize(rng(), n_bits);
33✔
1259
               y.randomize(rng(), q_bits);
33✔
1260

1261
               div_timer->start();
33✔
1262
               Botan::vartime_divide(x, y, q1, r1);
33✔
1263
               div_timer->stop();
33✔
1264

1265
               ct_div_timer->start();
33✔
1266
               Botan::ct_divide(x, y, q2, r2);
33✔
1267
               ct_div_timer->stop();
33✔
1268

1269
               BOTAN_ASSERT_EQUAL(q1, q2, "Quotient ok");
33✔
1270
               BOTAN_ASSERT_EQUAL(r1, r2, "Remainder ok");
75✔
1271
            }
1272

1273
            record_result(div_timer);
9✔
1274
            record_result(ct_div_timer);
9✔
1275
         }
54✔
1276
      }
1✔
1277

1278
      void bench_mp_div10(const std::chrono::milliseconds runtime) {
1✔
1279
         std::chrono::milliseconds runtime_per_size = runtime;
1✔
1280

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

1284
            auto div_timer = make_timer("BigInt div " + bit_descr);
27✔
1285
            auto ct_div_timer = make_timer("BigInt ct_div " + bit_descr);
27✔
1286

1287
            Botan::BigInt x;
9✔
1288
            Botan::secure_vector<Botan::word> ws;
9✔
1289

1290
            const auto ten = Botan::BigInt::from_word(10);
9✔
1291
            Botan::BigInt q1, r1, q2;
9✔
1292
            Botan::word r2;
1293

1294
            while(ct_div_timer->under(runtime_per_size)) {
229✔
1295
               x.randomize(rng(), n_bits);
220✔
1296

1297
               div_timer->start();
220✔
1298
               Botan::vartime_divide(x, ten, q1, r1);
220✔
1299
               div_timer->stop();
220✔
1300

1301
               ct_div_timer->start();
220✔
1302
               Botan::ct_divide_word(x, 10, q2, r2);
220✔
1303
               ct_div_timer->stop();
220✔
1304

1305
               BOTAN_ASSERT_EQUAL(q1, q2, "Quotient ok");
220✔
1306
               BOTAN_ASSERT_EQUAL(r1, r2, "Remainder ok");
449✔
1307
            }
1308

1309
            record_result(div_timer);
9✔
1310
            record_result(ct_div_timer);
9✔
1311
         }
45✔
1312
      }
1✔
1313

1314
#endif
1315

1316
#if defined(BOTAN_HAS_DL_GROUP)
1317

1318
      void bench_modexp(const std::chrono::milliseconds runtime) {
1✔
1319
         for(size_t group_bits : {1024, 1536, 2048, 3072, 4096}) {
6✔
1320
            const std::string group_bits_str = std::to_string(group_bits);
5✔
1321
            const Botan::DL_Group group("modp/srp/" + group_bits_str);
5✔
1322

1323
            const size_t e_bits = Botan::dl_exponent_size(group_bits);
5✔
1324
            const size_t f_bits = group_bits - 1;
5✔
1325

1326
            const Botan::BigInt random_e(rng(), e_bits);
5✔
1327
            const Botan::BigInt random_f(rng(), f_bits);
5✔
1328

1329
            auto e_timer = make_timer(group_bits_str + " short exponent");
15✔
1330
            auto f_timer = make_timer(group_bits_str + "  full exponent");
15✔
1331

1332
            while(f_timer->under(runtime)) {
10✔
1333
               e_timer->run([&]() { group.power_g_p(random_e); });
10✔
1334
               f_timer->run([&]() { group.power_g_p(random_f); });
10✔
1335
            }
1336

1337
            record_result(e_timer);
5✔
1338
            record_result(f_timer);
5✔
1339
         }
20✔
1340
      }
1✔
1341
#endif
1342

1343
#if defined(BOTAN_HAS_NUMBERTHEORY)
1344
      void bench_nistp_redc(const std::chrono::milliseconds runtime) {
1✔
1345
         Botan::secure_vector<Botan::word> ws;
1✔
1346

1347
         auto p192_timer = make_timer("P-192 redc");
2✔
1348
         Botan::BigInt r192(rng(), 192 * 2 - 1);
1✔
1349
         while(p192_timer->under(runtime)) {
2,264✔
1350
            Botan::BigInt r = r192;
2,263✔
1351
            p192_timer->run([&]() { Botan::redc_p192(r, ws); });
4,526✔
1352
            r192 += 1;
2,263✔
1353
         }
2,263✔
1354
         record_result(p192_timer);
1✔
1355

1356
         auto p224_timer = make_timer("P-224 redc");
2✔
1357
         Botan::BigInt r224(rng(), 224 * 2 - 1);
1✔
1358
         while(p224_timer->under(runtime)) {
2,149✔
1359
            Botan::BigInt r = r224;
2,148✔
1360
            p224_timer->run([&]() { Botan::redc_p224(r, ws); });
4,296✔
1361
            r224 += 1;
2,148✔
1362
         }
2,148✔
1363
         record_result(p224_timer);
1✔
1364

1365
         auto p256_timer = make_timer("P-256 redc");
2✔
1366
         Botan::BigInt r256(rng(), 256 * 2 - 1);
1✔
1367
         while(p256_timer->under(runtime)) {
1,979✔
1368
            Botan::BigInt r = r256;
1,978✔
1369
            p256_timer->run([&]() { Botan::redc_p256(r, ws); });
3,956✔
1370
            r256 += 1;
1,978✔
1371
         }
1,978✔
1372
         record_result(p256_timer);
1✔
1373

1374
         auto p384_timer = make_timer("P-384 redc");
2✔
1375
         Botan::BigInt r384(rng(), 384 * 2 - 1);
1✔
1376
         while(p384_timer->under(runtime)) {
1,921✔
1377
            Botan::BigInt r = r384;
1,920✔
1378
            p384_timer->run([&]() { Botan::redc_p384(r384, ws); });
3,840✔
1379
            r384 += 1;
1,920✔
1380
         }
1,920✔
1381
         record_result(p384_timer);
1✔
1382

1383
         auto p521_timer = make_timer("P-521 redc");
2✔
1384
         Botan::BigInt r521(rng(), 521 * 2 - 1);
1✔
1385
         while(p521_timer->under(runtime)) {
1,402✔
1386
            Botan::BigInt r = r521;
1,401✔
1387
            p521_timer->run([&]() { Botan::redc_p521(r521, ws); });
2,802✔
1388
            r521 += 1;
1,401✔
1389
         }
1,401✔
1390
         record_result(p521_timer);
1✔
1391
      }
6✔
1392

1393
      void bench_bn_redc(const std::chrono::milliseconds runtime) {
1✔
1394
         for(size_t bitsize : {512, 1024, 2048, 4096}) {
5✔
1395
            Botan::BigInt p(rng(), bitsize);
4✔
1396

1397
            std::string bit_str = std::to_string(bitsize);
4✔
1398
            auto barrett_timer = make_timer("Barrett-" + bit_str);
8✔
1399
            auto schoolbook_timer = make_timer("Schoolbook-" + bit_str);
8✔
1400

1401
            Botan::Modular_Reducer mod_p(p);
4✔
1402

1403
            while(schoolbook_timer->under(runtime)) {
48✔
1404
               const Botan::BigInt x(rng(), p.bits() * 2 - 2);
44✔
1405

1406
               const Botan::BigInt r1 = barrett_timer->run([&] { return mod_p.reduce(x); });
88✔
1407
               const Botan::BigInt r2 = schoolbook_timer->run([&] { return x % p; });
88✔
1408

1409
               BOTAN_ASSERT(r1 == r2, "Computed different results");
44✔
1410
            }
132✔
1411

1412
            record_result(barrett_timer);
4✔
1413
            record_result(schoolbook_timer);
4✔
1414
         }
8✔
1415
      }
1✔
1416

1417
      void bench_inverse_mod(const std::chrono::milliseconds runtime) {
1✔
1418
         for(size_t bits : {256, 384, 512, 1024, 2048}) {
6✔
1419
            const std::string bit_str = std::to_string(bits);
5✔
1420

1421
            auto timer = make_timer("inverse_mod-" + bit_str);
12✔
1422
            auto gcd_timer = make_timer("gcd-" + bit_str);
10✔
1423

1424
            while(timer->under(runtime) && gcd_timer->under(runtime)) {
11✔
1425
               const Botan::BigInt x(rng(), bits - 1);
6✔
1426
               Botan::BigInt mod(rng(), bits);
6✔
1427

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

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

1432
               if(x_inv == 0) {
6✔
1433
                  BOTAN_ASSERT(g != 1, "Inversion only fails if gcd(x, mod) > 1");
2✔
1434
               } else {
1435
                  BOTAN_ASSERT(g == 1, "Inversion succeeds only if gcd != 1");
4✔
1436
                  const Botan::BigInt check = (x_inv * x) % mod;
4✔
1437
                  BOTAN_ASSERT_EQUAL(check, 1, "Const time inversion correct");
4✔
1438
               }
4✔
1439
            }
23✔
1440

1441
            record_result(timer);
5✔
1442
            record_result(gcd_timer);
5✔
1443
         }
5✔
1444
      }
1✔
1445

1446
      void bench_primality_tests(const std::chrono::milliseconds runtime) {
1✔
1447
         for(size_t bits : {256, 512, 1024}) {
4✔
1448
            auto mr_timer = make_timer("Miller-Rabin-" + std::to_string(bits));
9✔
1449
            auto bpsw_timer = make_timer("Bailie-PSW-" + std::to_string(bits));
6✔
1450
            auto lucas_timer = make_timer("Lucas-" + std::to_string(bits));
6✔
1451

1452
            Botan::BigInt n = Botan::random_prime(rng(), bits);
3✔
1453

1454
            while(lucas_timer->under(runtime)) {
6✔
1455
               Botan::Modular_Reducer mod_n(n);
3✔
1456

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

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

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

1463
               n += 2;
3✔
1464
            }
3✔
1465

1466
            record_result(mr_timer);
3✔
1467
            record_result(bpsw_timer);
3✔
1468
            record_result(lucas_timer);
3✔
1469
         }
3✔
1470
      }
1✔
1471

1472
      void bench_random_prime(const std::chrono::milliseconds runtime) {
1✔
1473
         const auto coprime = Botan::BigInt::from_word(0x10001);
1✔
1474

1475
         for(size_t bits : {256, 384, 512, 768, 1024, 1536}) {
7✔
1476
            auto genprime_timer = make_timer("random_prime " + std::to_string(bits));
18✔
1477
            auto gensafe_timer = make_timer("random_safe_prime " + std::to_string(bits));
18✔
1478
            auto is_prime_timer = make_timer("is_prime " + std::to_string(bits));
12✔
1479

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

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

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

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

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

1497
               // Now test p+2, p+4, ... which may or may not be prime
1498
               for(size_t i = 2; i <= 64; i += 2) {
198✔
1499
                  is_prime_timer->run([&]() { Botan::is_prime(p + i, rng(), 64, true); });
576✔
1500
               }
1501
            }
12✔
1502

1503
            record_result(genprime_timer);
6✔
1504
            record_result(gensafe_timer);
6✔
1505
            record_result(is_prime_timer);
6✔
1506
         }
6✔
1507
      }
1✔
1508
#endif
1509

1510
#if defined(BOTAN_HAS_PUBLIC_KEY_CRYPTO)
1511
      void bench_pk_enc(const Botan::Private_Key& key,
4✔
1512
                        const std::string& nm,
1513
                        const std::string& provider,
1514
                        const std::string& padding,
1515
                        std::chrono::milliseconds msec) {
1516
         std::vector<uint8_t> plaintext, ciphertext;
4✔
1517

1518
         Botan::PK_Encryptor_EME enc(key, rng(), padding, provider);
4✔
1519
         Botan::PK_Decryptor_EME dec(key, rng(), padding, provider);
4✔
1520

1521
         auto enc_timer = make_timer(nm + " " + padding, provider, "encrypt");
12✔
1522
         auto dec_timer = make_timer(nm + " " + padding, provider, "decrypt");
12✔
1523

1524
         while(enc_timer->under(msec) || dec_timer->under(msec)) {
12✔
1525
            // Generate a new random ciphertext to decrypt
1526
            if(ciphertext.empty() || enc_timer->under(msec)) {
4✔
1527
               rng().random_vec(plaintext, enc.maximum_input_size());
4✔
1528
               ciphertext = enc_timer->run([&]() { return enc.encrypt(plaintext, rng()); });
12✔
1529
            }
1530

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

1534
               if(!(Botan::unlock(dec_pt) == plaintext))  // sanity check
8✔
1535
               {
1536
                  error_output() << "Bad roundtrip in PK encrypt/decrypt bench\n";
×
1537
               }
1538
            }
4✔
1539
         }
1540

1541
         record_result(enc_timer);
4✔
1542
         record_result(dec_timer);
4✔
1543
      }
12✔
1544

1545
      void bench_pk_ka(const std::string& algo,
14✔
1546
                       const std::string& nm,
1547
                       const std::string& params,
1548
                       const std::string& provider,
1549
                       std::chrono::milliseconds msec) {
1550
         const std::string kdf = "KDF2(SHA-256)";  // arbitrary choice
14✔
1551

1552
         auto keygen_timer = make_timer(nm, provider, "keygen");
28✔
1553

1554
         std::unique_ptr<Botan::Private_Key> key1(
14✔
1555
            keygen_timer->run([&] { return Botan::create_private_key(algo, rng(), params); }));
28✔
1556
         std::unique_ptr<Botan::Private_Key> key2(
14✔
1557
            keygen_timer->run([&] { return Botan::create_private_key(algo, rng(), params); }));
28✔
1558

1559
         record_result(keygen_timer);
14✔
1560

1561
         const Botan::PK_Key_Agreement_Key& ka_key1 = dynamic_cast<const Botan::PK_Key_Agreement_Key&>(*key1);
14✔
1562
         const Botan::PK_Key_Agreement_Key& ka_key2 = dynamic_cast<const Botan::PK_Key_Agreement_Key&>(*key2);
14✔
1563

1564
         Botan::PK_Key_Agreement ka1(ka_key1, rng(), kdf, provider);
14✔
1565
         Botan::PK_Key_Agreement ka2(ka_key2, rng(), kdf, provider);
14✔
1566

1567
         const std::vector<uint8_t> ka1_pub = ka_key1.public_value();
14✔
1568
         const std::vector<uint8_t> ka2_pub = ka_key2.public_value();
14✔
1569

1570
         auto ka_timer = make_timer(nm, provider, "key agreements");
28✔
1571

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

1576
            if(symkey1 != symkey2) {
17✔
1577
               error_output() << "Key agreement mismatch in PK bench\n";
×
1578
            }
1579
         }
34✔
1580

1581
         record_result(ka_timer);
14✔
1582
      }
70✔
1583

1584
      void bench_pk_kem(const Botan::Private_Key& key,
11✔
1585
                        const std::string& nm,
1586
                        const std::string& provider,
1587
                        const std::string& kdf,
1588
                        std::chrono::milliseconds msec) {
1589
         Botan::PK_KEM_Decryptor dec(key, rng(), kdf, provider);
11✔
1590
         Botan::PK_KEM_Encryptor enc(key, kdf, provider);
11✔
1591

1592
         auto kem_enc_timer = make_timer(nm, provider, "KEM encrypt");
22✔
1593
         auto kem_dec_timer = make_timer(nm, provider, "KEM decrypt");
22✔
1594

1595
         while(kem_enc_timer->under(msec) && kem_dec_timer->under(msec)) {
32✔
1596
            Botan::secure_vector<uint8_t> salt = rng().random_vec(16);
21✔
1597

1598
            kem_enc_timer->start();
21✔
1599
            const auto kem_result = enc.encrypt(rng(), 64, salt);
21✔
1600
            kem_enc_timer->stop();
21✔
1601

1602
            kem_dec_timer->start();
21✔
1603
            Botan::secure_vector<uint8_t> dec_shared_key = dec.decrypt(kem_result.encapsulated_shared_key(), 64, salt);
21✔
1604
            kem_dec_timer->stop();
21✔
1605

1606
            if(kem_result.shared_key() != dec_shared_key) {
21✔
1607
               error_output() << "KEM mismatch in PK bench\n";
×
1608
            }
1609
         }
42✔
1610

1611
         record_result(kem_enc_timer);
11✔
1612
         record_result(kem_dec_timer);
11✔
1613
      }
11✔
1614

1615
      void bench_pk_sig_ecc(const std::string& algo,
6✔
1616
                            const std::string& emsa,
1617
                            const std::string& provider,
1618
                            const std::vector<std::string>& params,
1619
                            std::chrono::milliseconds msec) {
1620
         for(std::string grp : params) {
32✔
1621
            const std::string nm = grp.empty() ? algo : Botan::fmt("{}-{}", algo, grp);
26✔
1622

1623
            auto keygen_timer = make_timer(nm, provider, "keygen");
52✔
1624

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

1628
            record_result(keygen_timer);
26✔
1629
            bench_pk_sig(*key, nm, provider, emsa, msec);
26✔
1630
         }
48✔
1631
      }
6✔
1632

1633
      size_t bench_pk_sig(const Botan::Private_Key& key,
41✔
1634
                          const std::string& nm,
1635
                          const std::string& provider,
1636
                          const std::string& padding,
1637
                          std::chrono::milliseconds msec) {
1638
         std::vector<uint8_t> message, signature, bad_signature;
41✔
1639

1640
         Botan::PK_Signer sig(key, rng(), padding, Botan::Signature_Format::Standard, provider);
41✔
1641
         Botan::PK_Verifier ver(key, padding, Botan::Signature_Format::Standard, provider);
41✔
1642

1643
         auto sig_timer = make_timer(nm + " " + padding, provider, "sign");
122✔
1644
         auto ver_timer = make_timer(nm + " " + padding, provider, "verify");
122✔
1645

1646
         size_t invalid_sigs = 0;
41✔
1647

1648
         while(ver_timer->under(msec) || sig_timer->under(msec)) {
160✔
1649
            if(signature.empty() || sig_timer->under(msec)) {
78✔
1650
               /*
1651
               Length here is kind of arbitrary, but 48 bytes fits into a single
1652
               hash block so minimizes hashing overhead versus the PK op itself.
1653
               */
1654
               rng().random_vec(message, 48);
51✔
1655

1656
               signature = sig_timer->run([&]() { return sig.sign_message(message, rng()); });
153✔
1657

1658
               bad_signature = signature;
51✔
1659
               bad_signature[rng().next_byte() % bad_signature.size()] ^= rng().next_nonzero_byte();
51✔
1660
            }
1661

1662
            if(ver_timer->under(msec)) {
78✔
1663
               const bool verified = ver_timer->run([&] { return ver.verify_message(message, signature); });
138✔
1664

1665
               if(!verified) {
69✔
1666
                  invalid_sigs += 1;
×
1667
               }
1668

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

1671
               if(verified_bad) {
69✔
1672
                  error_output() << "Bad signature accepted in " << nm << " signature bench\n";
×
1673
               }
1674
            }
1675
         }
1676

1677
         if(invalid_sigs > 0) {
41✔
1678
            error_output() << invalid_sigs << " generated signatures rejected in " << nm << " signature bench\n";
×
1679
         }
1680

1681
         const size_t events = static_cast<size_t>(std::min(sig_timer->events(), ver_timer->events()));
41✔
1682

1683
         record_result(sig_timer);
41✔
1684
         record_result(ver_timer);
41✔
1685

1686
         return events;
82✔
1687
      }
164✔
1688
#endif
1689

1690
#if defined(BOTAN_HAS_RSA)
1691
      void bench_rsa_keygen(const std::string& provider, std::chrono::milliseconds msec) {
1✔
1692
         for(size_t keylen : {1024, 2048, 3072, 4096}) {
5✔
1693
            const std::string nm = "RSA-" + std::to_string(keylen);
4✔
1694
            auto keygen_timer = make_timer(nm, provider, "keygen");
8✔
1695

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

1700
               BOTAN_ASSERT(key->check_key(rng(), true), "Key is ok");
4✔
1701
            }
4✔
1702

1703
            record_result(keygen_timer);
4✔
1704
         }
4✔
1705
      }
1✔
1706

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

1711
            auto keygen_timer = make_timer(nm, provider, "keygen");
8✔
1712

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

1716
            record_result(keygen_timer);
4✔
1717

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

1721
            //bench_pk_sig(*key, nm, provider, "PSSR(SHA-256)", msec);
1722
            //bench_pk_enc(*key, nm, provider, "EME-PKCS1-v1_5", msec);
1723
            //bench_pk_enc(*key, nm, provider, "OAEP(SHA-1)", msec);
1724
         }
4✔
1725
      }
1✔
1726
#endif
1727

1728
#if defined(BOTAN_HAS_ECDSA)
1729
      void bench_ecdsa(const std::vector<std::string>& groups,
1✔
1730
                       const std::string& provider,
1731
                       std::chrono::milliseconds msec) {
1732
         return bench_pk_sig_ecc("ECDSA", "SHA-256", provider, groups, msec);
2✔
1733
      }
1734

1735
      void bench_ecdsa_recovery(const std::vector<std::string>& groups,
1✔
1736
                                const std::string& /*unused*/,
1737
                                std::chrono::milliseconds msec) {
1738
         for(const std::string& group_name : groups) {
7✔
1739
            Botan::EC_Group group(group_name);
6✔
1740
            auto recovery_timer = make_timer("ECDSA recovery " + group_name);
18✔
1741

1742
            while(recovery_timer->under(msec)) {
12✔
1743
               Botan::ECDSA_PrivateKey key(rng(), group);
6✔
1744

1745
               std::vector<uint8_t> message(group.get_order_bits() / 8);
6✔
1746
               rng().randomize(message.data(), message.size());
6✔
1747

1748
               Botan::PK_Signer signer(key, rng(), "Raw");
6✔
1749
               signer.update(message);
6✔
1750
               std::vector<uint8_t> signature = signer.signature(rng());
6✔
1751

1752
               Botan::PK_Verifier verifier(key, "Raw", Botan::Signature_Format::Standard, "base");
6✔
1753
               verifier.update(message);
6✔
1754
               BOTAN_ASSERT(verifier.check_signature(signature), "Valid signature");
6✔
1755

1756
               Botan::BigInt r(signature.data(), signature.size() / 2);
6✔
1757
               Botan::BigInt s(signature.data() + signature.size() / 2, signature.size() / 2);
6✔
1758

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

1761
               recovery_timer->run([&]() {
6✔
1762
                  Botan::ECDSA_PublicKey pubkey(group, message, r, s, v);
6✔
1763
                  BOTAN_ASSERT(pubkey.public_point() == key.public_point(), "Recovered public key");
6✔
1764
               });
6✔
1765
            }
24✔
1766

1767
            record_result(recovery_timer);
6✔
1768
         }
6✔
1769
      }
1✔
1770

1771
#endif
1772

1773
#if defined(BOTAN_HAS_ECKCDSA)
1774
      void bench_eckcdsa(const std::vector<std::string>& groups,
1✔
1775
                         const std::string& provider,
1776
                         std::chrono::milliseconds msec) {
1777
         return bench_pk_sig_ecc("ECKCDSA", "SHA-256", provider, groups, msec);
2✔
1778
      }
1779
#endif
1780

1781
#if defined(BOTAN_HAS_GOST_34_10_2001)
1782
      void bench_gost_3410(const std::string& provider, std::chrono::milliseconds msec) {
1✔
1783
         return bench_pk_sig_ecc("GOST-34.10", "GOST-34.11", provider, {"gost_256A"}, msec);
3✔
1784
      }
1785
#endif
1786

1787
#if defined(BOTAN_HAS_SM2)
1788
      void bench_sm2(const std::vector<std::string>& groups,
1✔
1789
                     const std::string& provider,
1790
                     std::chrono::milliseconds msec) {
1791
         return bench_pk_sig_ecc("SM2_Sig", "SM3", provider, groups, msec);
2✔
1792
      }
1793
#endif
1794

1795
#if defined(BOTAN_HAS_ECGDSA)
1796
      void bench_ecgdsa(const std::vector<std::string>& groups,
1✔
1797
                        const std::string& provider,
1798
                        std::chrono::milliseconds msec) {
1799
         return bench_pk_sig_ecc("ECGDSA", "SHA-256", provider, groups, msec);
2✔
1800
      }
1801
#endif
1802

1803
#if defined(BOTAN_HAS_ED25519)
1804
      void bench_ed25519(const std::string& provider, std::chrono::milliseconds msec) {
1✔
1805
         return bench_pk_sig_ecc("Ed25519", "Pure", provider, std::vector<std::string>{""}, msec);
3✔
1806
      }
1807
#endif
1808

1809
#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
1810
      void bench_dh(const std::string& provider, std::chrono::milliseconds msec) {
1✔
1811
         for(size_t bits : {1024, 1536, 2048, 3072, 4096, 6144, 8192}) {
8✔
1812
            bench_pk_ka("DH", "DH-" + std::to_string(bits), "modp/ietf/" + std::to_string(bits), provider, msec);
14✔
1813
         }
1814
      }
1✔
1815
#endif
1816

1817
#if defined(BOTAN_HAS_DSA)
1818
      void bench_dsa(const std::string& provider, std::chrono::milliseconds msec) {
1✔
1819
         for(size_t bits : {1024, 2048, 3072}) {
4✔
1820
            const std::string nm = "DSA-" + std::to_string(bits);
3✔
1821

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

1824
            auto keygen_timer = make_timer(nm, provider, "keygen");
6✔
1825

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

1829
            record_result(keygen_timer);
3✔
1830

1831
            bench_pk_sig(*key, nm, provider, "SHA-256", msec);
6✔
1832
         }
3✔
1833
      }
1✔
1834
#endif
1835

1836
#if defined(BOTAN_HAS_ELGAMAL)
1837
      void bench_elgamal(const std::string& provider, std::chrono::milliseconds msec) {
1✔
1838
         for(size_t keylen : {1024, 2048, 3072, 4096}) {
5✔
1839
            const std::string nm = "ElGamal-" + std::to_string(keylen);
4✔
1840

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

1843
            auto keygen_timer = make_timer(nm, provider, "keygen");
8✔
1844

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

1848
            record_result(keygen_timer);
4✔
1849

1850
            bench_pk_enc(*key, nm, provider, "EME-PKCS1-v1_5", msec);
8✔
1851
         }
4✔
1852
      }
1✔
1853
#endif
1854

1855
#if defined(BOTAN_HAS_ECDH)
1856
      void bench_ecdh(const std::vector<std::string>& groups,
1✔
1857
                      const std::string& provider,
1858
                      std::chrono::milliseconds msec) {
1859
         for(const std::string& grp : groups) {
7✔
1860
            bench_pk_ka("ECDH", "ECDH-" + grp, grp, provider, msec);
15✔
1861
         }
1862
      }
1✔
1863
#endif
1864

1865
#if defined(BOTAN_HAS_CURVE_25519)
1866
      void bench_curve25519(const std::string& provider, std::chrono::milliseconds msec) {
1✔
1867
         bench_pk_ka("Curve25519", "Curve25519", "", provider, msec);
2✔
1868
      }
1✔
1869
#endif
1870

1871
#if defined(BOTAN_HAS_MCELIECE)
1872
      void bench_mceliece(const std::string& provider, std::chrono::milliseconds msec) {
1✔
1873
         /*
1874
         SL=80 n=1632 t=33 - 59 KB pubkey 140 KB privkey
1875
         SL=107 n=2480 t=45 - 128 KB pubkey 300 KB privkey
1876
         SL=128 n=2960 t=57 - 195 KB pubkey 459 KB privkey
1877
         SL=147 n=3408 t=67 - 265 KB pubkey 622 KB privkey
1878
         SL=191 n=4624 t=95 - 516 KB pubkey 1234 KB privkey
1879
         SL=256 n=6624 t=115 - 942 KB pubkey 2184 KB privkey
1880
         */
1881

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

1885
         for(auto params : mce_params) {
6✔
1886
            size_t n = params.first;
5✔
1887
            size_t t = params.second;
5✔
1888

1889
            const std::string nm = "McEliece-" + std::to_string(n) + "," + std::to_string(t) +
10✔
1890
                                   " (WF=" + std::to_string(Botan::mceliece_work_factor(n, t)) + ")";
15✔
1891

1892
            auto keygen_timer = make_timer(nm, provider, "keygen");
10✔
1893

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

1897
            record_result(keygen_timer);
5✔
1898
            bench_pk_kem(*key, nm, provider, "KDF2(SHA-256)", msec);
10✔
1899
         }
10✔
1900
      }
1✔
1901
#endif
1902

1903
#if defined(BOTAN_HAS_KYBER) || defined(BOTAN_HAS_KYBER_90S)
1904
      void bench_kyber(const std::string& provider, std::chrono::milliseconds msec) {
1✔
1905
         const Botan::KyberMode::Mode all_modes[] = {
1✔
1906
            Botan::KyberMode::Kyber512,
1907
            Botan::KyberMode::Kyber512_90s,
1908
            Botan::KyberMode::Kyber768,
1909
            Botan::KyberMode::Kyber768_90s,
1910
            Botan::KyberMode::Kyber1024,
1911
            Botan::KyberMode::Kyber1024_90s,
1912
         };
1913

1914
         for(auto modet : all_modes) {
7✔
1915
            Botan::KyberMode mode(modet);
6✔
1916

1917
   #if !defined(BOTAN_HAS_KYBER)
1918
            if(mode.is_modern())
1919
               continue;
1920
   #endif
1921

1922
   #if !defined(BOTAN_HAS_KYBER_90S)
1923
            if(mode.is_90s())
1924
               continue;
1925
   #endif
1926

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

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

1931
            record_result(keygen_timer);
6✔
1932

1933
            bench_pk_kem(key, mode.to_string(), provider, "KDF2(SHA-256)", msec);
12✔
1934
         }
6✔
1935
      }
1✔
1936
#endif
1937

1938
#if defined(BOTAN_HAS_DILITHIUM) || defined(BOTAN_HAS_DILITHIUM_AES)
1939
      void bench_dilithium(const std::string& provider, std::chrono::milliseconds msec) {
1✔
1940
         const Botan::DilithiumMode::Mode all_modes[] = {Botan::DilithiumMode::Dilithium4x4,
1✔
1941
                                                         Botan::DilithiumMode::Dilithium4x4_AES,
1942
                                                         Botan::DilithiumMode::Dilithium6x5,
1943
                                                         Botan::DilithiumMode::Dilithium6x5_AES,
1944
                                                         Botan::DilithiumMode::Dilithium8x7,
1945
                                                         Botan::DilithiumMode::Dilithium8x7_AES};
1946

1947
         for(auto modet : all_modes) {
7✔
1948
            Botan::DilithiumMode mode(modet);
6✔
1949

1950
   #if !defined(BOTAN_HAS_DILITHIUM)
1951
            if(mode.is_modern())
1952
               continue;
1953
   #endif
1954

1955
   #if !defined(BOTAN_HAS_DILITHIUM_AES)
1956
            if(mode.is_aes())
1957
               continue;
1958
   #endif
1959

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

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

1964
            record_result(keygen_timer);
6✔
1965

1966
            bench_pk_sig(key, mode.to_string(), provider, "", msec);
12✔
1967
         }
6✔
1968
      }
1✔
1969
#endif
1970

1971
#if defined(BOTAN_HAS_SPHINCS_PLUS_WITH_SHA2) || defined(BOTAN_HAS_SPHINCS_PLUS_WITH_SHAKE)
1972
      void bench_sphincs_plus(const std::string& provider, std::chrono::milliseconds msec) {
1✔
1973
         // Sphincs_Parameter_Set set, Sphincs_Hash_Type hash
1974
         std::vector<std::string> sphincs_params{"SphincsPlus-sha2-128s-r3.1",
1✔
1975
                                                 "SphincsPlus-sha2-128f-r3.1",
1976
                                                 "SphincsPlus-sha2-192s-r3.1",
1977
                                                 "SphincsPlus-sha2-192f-r3.1",
1978
                                                 "SphincsPlus-sha2-256s-r3.1",
1979
                                                 "SphincsPlus-sha2-256f-r3.1",
1980
                                                 "SphincsPlus-shake-128s-r3.1",
1981
                                                 "SphincsPlus-shake-128f-r3.1",
1982
                                                 "SphincsPlus-shake-192s-r3.1",
1983
                                                 "SphincsPlus-shake-192f-r3.1",
1984
                                                 "SphincsPlus-shake-256s-r3.1",
1985
                                                 "SphincsPlus-shake-256f-r3.1"};
13✔
1986

1987
         for(auto params : sphincs_params) {
1✔
1988
            try {
1✔
1989
               auto keygen_timer = make_timer(params, provider, "keygen");
2✔
1990

1991
               std::unique_ptr<Botan::Private_Key> key(
1✔
1992
                  keygen_timer->run([&] { return Botan::create_private_key("SPHINCS+", rng(), params); }));
2✔
1993

1994
               record_result(keygen_timer);
1✔
1995
               if(bench_pk_sig(*key, params, provider, "", msec) == 1) {
1✔
1996
                  break;
1997
               }
1998
            } catch(Botan::Not_Implemented&) {
1✔
1999
               continue;
×
2000
            }
×
2001
         }
×
2002
      }
1✔
2003
#endif
2004

2005
#if defined(BOTAN_HAS_XMSS_RFC8391)
2006
      void bench_xmss(const std::string& provider, std::chrono::milliseconds msec) {
1✔
2007
         /*
2008
         We only test H10 signatures here since already they are quite slow (a
2009
         few seconds per signature). On a fast machine, H16 signatures take 1-2
2010
         minutes to generate and H20 signatures take 5-10 minutes to generate
2011
         */
2012
         std::vector<std::string> xmss_params{
1✔
2013
            "XMSS-SHA2_10_256",
2014
            "XMSS-SHAKE_10_256",
2015
            "XMSS-SHA2_10_512",
2016
            "XMSS-SHAKE_10_512",
2017
         };
5✔
2018

2019
         for(std::string params : xmss_params) {
1✔
2020
            auto keygen_timer = make_timer(params, provider, "keygen");
2✔
2021

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

2025
            record_result(keygen_timer);
1✔
2026
            if(bench_pk_sig(*key, params, provider, "", msec) == 1) {
1✔
2027
               break;
2028
            }
2029
         }
2✔
2030
      }
1✔
2031
#endif
2032

2033
#if defined(BOTAN_HAS_ZFEC)
2034
      void bench_zfec(std::chrono::milliseconds msec) {
1✔
2035
         const size_t k = 4;
1✔
2036
         const size_t n = 16;
1✔
2037

2038
         Botan::ZFEC zfec(k, n);
1✔
2039

2040
         const size_t share_size = 256 * 1024;
1✔
2041

2042
         std::vector<uint8_t> input(share_size * k);
1✔
2043
         rng().randomize(input.data(), input.size());
1✔
2044

2045
         std::vector<uint8_t> output(share_size * n);
1✔
2046

2047
         auto enc_fn = [&](size_t share, const uint8_t buf[], size_t len) {
17✔
2048
            std::memcpy(&output[share * share_size], buf, len);
16✔
2049
         };
1✔
2050

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

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

2056
         record_result(enc_timer);
1✔
2057

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

2061
         std::map<size_t, const uint8_t*> shares;
1✔
2062
         for(size_t i = 0; i != n; ++i) {
17✔
2063
            shares[i] = &output[share_size * i];
16✔
2064
         }
2065

2066
         // remove data shares to make decoding maximally expensive:
2067
         while(shares.size() != k) {
13✔
2068
            shares.erase(shares.begin());
12✔
2069
         }
2070

2071
         std::vector<uint8_t> recovered(share_size * k);
1✔
2072

2073
         auto dec_fn = [&](size_t share, const uint8_t buf[], size_t len) {
5✔
2074
            std::memcpy(&recovered[share * share_size], buf, len);
4✔
2075
         };
1✔
2076

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

2079
         record_result(dec_timer);
1✔
2080

2081
         if(recovered != input) {
1✔
2082
            error_output() << "ZFEC recovery failed\n";
×
2083
         }
2084
      }
4✔
2085

2086
#endif
2087

2088
#if defined(BOTAN_HAS_POLY_DBL)
2089
      void bench_poly_dbl(std::chrono::milliseconds msec) {
1✔
2090
         for(size_t sz : {8, 16, 24, 32, 64, 128}) {
7✔
2091
            auto be_timer = make_timer("poly_dbl_be_" + std::to_string(sz));
12✔
2092
            auto le_timer = make_timer("poly_dbl_le_" + std::to_string(sz));
12✔
2093

2094
            std::vector<uint8_t> buf(sz);
6✔
2095
            rng().randomize(buf.data(), sz);
6✔
2096

2097
            be_timer->run_until_elapsed(msec, [&]() { Botan::poly_double_n(buf.data(), buf.data(), sz); });
12,556✔
2098
            le_timer->run_until_elapsed(msec, [&]() { Botan::poly_double_n_le(buf.data(), buf.data(), sz); });
12,082✔
2099

2100
            record_result(be_timer);
6✔
2101
            record_result(le_timer);
6✔
2102
         }
6✔
2103
      }
1✔
2104
#endif
2105

2106
#if defined(BOTAN_HAS_BCRYPT)
2107

2108
      void bench_bcrypt() {
1✔
2109
         const std::string password = "not a very good password";
1✔
2110

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

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

2116
            record_result(timer);
11✔
2117
         }
11✔
2118
      }
1✔
2119
#endif
2120

2121
#if defined(BOTAN_HAS_PASSHASH9)
2122

2123
      void bench_passhash9() {
1✔
2124
         const std::string password = "not a very good password";
1✔
2125

2126
         for(uint8_t alg = 0; alg <= 4; ++alg) {
6✔
2127
            if(Botan::is_passhash9_alg_supported(alg) == false) {
5✔
2128
               continue;
×
2129
            }
2130

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

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

2136
               record_result(timer);
10✔
2137
            }
10✔
2138
         }
2139
      }
1✔
2140
#endif
2141

2142
#if defined(BOTAN_HAS_SCRYPT)
2143

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

2147
         for(size_t N : {8192, 16384, 32768, 65536}) {
5✔
2148
            for(size_t r : {1, 8, 16}) {
16✔
2149
               for(size_t p : {1}) {
12✔
2150
                  auto pwdhash = pwdhash_fam->from_params(N, r, p);
12✔
2151

2152
                  auto scrypt_timer =
12✔
2153
                     make_timer("scrypt-" + std::to_string(N) + "-" + std::to_string(r) + "-" + std::to_string(p) +
24✔
2154
                                " (" + std::to_string(pwdhash->total_memory_usage() / (1024 * 1024)) + " MiB)");
60✔
2155

2156
                  uint8_t out[64];
12✔
2157
                  uint8_t salt[8];
12✔
2158
                  rng().randomize(salt, sizeof(salt));
12✔
2159

2160
                  while(scrypt_timer->under(msec)) {
24✔
2161
                     scrypt_timer->run([&] {
12✔
2162
                        pwdhash->derive_key(out, sizeof(out), "password", 8, salt, sizeof(salt));
12✔
2163

2164
                        Botan::copy_mem(salt, out, 8);
12✔
2165
                     });
12✔
2166
                  }
2167

2168
                  record_result(scrypt_timer);
12✔
2169

2170
                  if(scrypt_timer->events() == 1) {
12✔
2171
                     break;
2172
                  }
2173
               }
24✔
2174
            }
2175
         }
2176
      }
1✔
2177

2178
#endif
2179

2180
#if defined(BOTAN_HAS_ARGON2)
2181

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

2185
         for(size_t M : {8 * 1024, 64 * 1024, 256 * 1024}) {
4✔
2186
            for(size_t t : {1, 4}) {
9✔
2187
               for(size_t p : {1, 4}) {
18✔
2188
                  auto pwhash = pwhash_fam->from_params(M, t, p);
12✔
2189
                  auto timer = make_timer(pwhash->to_string());
36✔
2190

2191
                  uint8_t out[64];
12✔
2192
                  uint8_t salt[16];
12✔
2193
                  rng().randomize(salt, sizeof(salt));
12✔
2194

2195
                  while(timer->under(msec)) {
24✔
2196
                     timer->run([&] { pwhash->derive_key(out, sizeof(out), "password", 8, salt, sizeof(salt)); });
24✔
2197
                  }
2198

2199
                  record_result(timer);
12✔
2200
               }
24✔
2201
            }
2202
         }
2203
      }
1✔
2204

2205
#endif
2206
};
2207

2208
BOTAN_REGISTER_COMMAND("speed", Speed);
35✔
2209

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