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

randombit / botan / 5312210723

19 Jun 2023 01:02PM UTC coverage: 91.712% (+0.002%) from 91.71%
5312210723

Pull #3549

github

web-flow
Merge 3275783d5 into 5288e84e1
Pull Request #3549: SPHINCS+

78132 of 85193 relevant lines covered (91.71%)

11884653.99 hits per line

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

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

8
#include "../tests/test_rng.h"  // FIXME
9
#include "cli.h"
10

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

146
namespace Botan_CLI {
147

148
using Botan::Timer;
149

150
namespace {
151

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

311
}  // namespace
312

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

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

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

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

367
            "ChaCha20Poly1305",
368

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

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

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

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

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

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

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

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

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

427
         m_clock_cycle_ratio = 1.0 / m_clock_cycle_ratio;
34✔
428

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

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

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

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

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

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

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

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

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

477
         for(const auto& algo : algos) {
81✔
478
            using namespace std::placeholders;
50✔
479

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

856
            record_result(encrypt_timer);
1✔
857

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

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

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

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

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

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

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

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

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

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

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

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

924
         record_result(ks_timer);
1✔
925

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

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

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

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

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

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

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

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

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

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

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

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

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

986
            size_t compressed_size = 0;
4✔
987

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

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

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

1001
            std::ostringstream msg;
4✔
1002

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1117
#endif
1118

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

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

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

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

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

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

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

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

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

1149
#if defined(BOTAN_HAS_FPE_FE1)
1150

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

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

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

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

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

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

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

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

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

1184
#if defined(BOTAN_HAS_RFC3394_KEYWRAP)
1185

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

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

1193
         while(wrap_timer->under(runtime)) {
43✔
1194
            wrap_timer->start();
42✔
1195
            key = Botan::rfc3394_keywrap(key, kek);
84✔
1196
            wrap_timer->stop();
42✔
1197

1198
            unwrap_timer->start();
42✔
1199
            key = Botan::rfc3394_keyunwrap(key, kek);
84✔
1200
            unwrap_timer->stop();
42✔
1201

1202
            key[0] += 1;
42✔
1203
         }
1204

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

1210
#if defined(BOTAN_HAS_BIGINT)
1211

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

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

1221
            while(mul_timer->under(runtime_per_size)) {
4,474✔
1222
               Botan::BigInt x(rng(), bits);
4,465✔
1223

1224
               sqr_timer->start();
4,465✔
1225
               x.square(ws);
4,465✔
1226
               sqr_timer->stop();
4,465✔
1227

1228
               x.mask_bits(bits);
4,465✔
1229

1230
               mul_timer->start();
4,465✔
1231
               x.mul(y, ws);
4,465✔
1232
               mul_timer->stop();
4,465✔
1233
            }
4,465✔
1234

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

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

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

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

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

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

1256
            while(ct_div_timer->under(runtime_per_size)) {
46✔
1257
               x.randomize(rng(), n_bits);
37✔
1258
               y.randomize(rng(), q_bits);
37✔
1259

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

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

1268
               BOTAN_ASSERT_EQUAL(q1, q2, "Quotient ok");
37✔
1269
               BOTAN_ASSERT_EQUAL(r1, r2, "Remainder ok");
83✔
1270
            }
1271

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

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

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

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

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

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

1293
            while(ct_div_timer->under(runtime_per_size)) {
265✔
1294
               x.randomize(rng(), n_bits);
256✔
1295

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

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

1304
               BOTAN_ASSERT_EQUAL(q1, q2, "Quotient ok");
256✔
1305
               BOTAN_ASSERT_EQUAL(r1, r2, "Remainder ok");
521✔
1306
            }
1307

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

1313
#endif
1314

1315
#if defined(BOTAN_HAS_DL_GROUP)
1316

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1408
               BOTAN_ASSERT(r1 == r2, "Computed different results");
49✔
1409
            }
147✔
1410

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1558
         record_result(keygen_timer);
14✔
1559

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

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

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

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

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

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

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

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

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

1594
         while(kem_enc_timer->under(msec) && kem_dec_timer->under(msec)) {
35✔
1595
            Botan::secure_vector<uint8_t> encap_key, enc_shared_key;
24✔
1596
            Botan::secure_vector<uint8_t> salt = rng().random_vec(16);
24✔
1597

1598
            kem_enc_timer->start();
24✔
1599
            enc.encrypt(encap_key, enc_shared_key, 64, rng(), salt);
24✔
1600
            kem_enc_timer->stop();
24✔
1601

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

1606
            if(enc_shared_key != dec_shared_key) {
24✔
1607
               error_output() << "KEM mismatch in PK bench\n";
×
1608
            }
1609
         }
96✔
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 : (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,
40✔
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;
40✔
1639

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

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

1646
         size_t invalid_sigs = 0;
40✔
1647

1648
         while(ver_timer->under(msec) || sig_timer->under(msec)) {
168✔
1649
            if(signature.empty() || sig_timer->under(msec)) {
88✔
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);
55✔
1655

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

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

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

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

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

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

1677
         if(invalid_sigs > 0) {
40✔
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()));
40✔
1682

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

1686
         return events;
80✔
1687
      }
160✔
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) {
×
1973
         // Sphincs_Parameter_Set set, Sphincs_Hash_Type hash
1974
         std::vector<std::string> sphincs_params{"SphincsPlus-sha2-128s-r3.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"};
×
1986

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2055
         record_result(enc_timer);
1✔
2056

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

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

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

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

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

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

2078
         record_result(dec_timer);
1✔
2079

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

2085
#endif
2086

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

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

2096
            be_timer->run_until_elapsed(msec, [&]() { Botan::poly_double_n(buf.data(), buf.data(), sz); });
18,263✔
2097
            le_timer->run_until_elapsed(msec, [&]() { Botan::poly_double_n_le(buf.data(), buf.data(), sz); });
20,498✔
2098

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

2105
#if defined(BOTAN_HAS_BCRYPT)
2106

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

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

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

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

2120
#if defined(BOTAN_HAS_PASSHASH9)
2121

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

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

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

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

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

2141
#if defined(BOTAN_HAS_SCRYPT)
2142

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

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

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

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

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

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

2167
                  record_result(scrypt_timer);
12✔
2168

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

2177
#endif
2178

2179
#if defined(BOTAN_HAS_ARGON2)
2180

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

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

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

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

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

2204
#endif
2205
};
2206

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

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