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

randombit / botan / 21794448852

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

push

github

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

Reduce header dependencies in tests and cli

102230 of 113507 relevant lines covered (90.06%)

11492365.41 hits per line

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

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

7
#include "perf.h"
8

9
#if defined(BOTAN_HAS_PUBLIC_KEY_CRYPTO)
10
   #include <botan/pk_algs.h>
11
   #include <botan/pubkey.h>
12
   #include <botan/rng.h>
13
   #include <botan/internal/fmt.h>
14
#endif
15

16
namespace Botan_CLI {
17

18
#if defined(BOTAN_HAS_PUBLIC_KEY_CRYPTO)
19

20
class PerfTest_PKSig : public PerfTest {
9✔
21
   public:
22
      virtual std::string algo() const = 0;
23

24
      virtual std::string hash() const { return "SHA-256"; }
4✔
25

26
      virtual std::vector<std::string> keygen_params(const PerfConfig& /*config*/) const { return {""}; }
2✔
27

28
      void go(const PerfConfig& config) override {
9✔
29
         const std::string alg = this->algo();
9✔
30
         const std::string padding = this->hash();
9✔
31

32
         const auto params = this->keygen_params(config);
9✔
33

34
         for(const auto& param : params) {
37✔
35
            const std::string nm = this->format_name(alg, param);
28✔
36
            bench_pk_sig(config, nm, alg, param, padding);
56✔
37
         }
28✔
38
      }
9✔
39

40
      static void bench_pk_sig(const PerfConfig& config,
28✔
41
                               const std::string& nm,
42
                               const std::string& alg,
43
                               const std::string& param,
44
                               const std::string& padding,
45
                               const std::string& provider = "") {
46
         auto& rng = config.rng();
28✔
47
         const auto msec = config.runtime();
28✔
48

49
         auto keygen_timer = config.make_timer(nm, 1, "keygen");
56✔
50

51
         auto sk = keygen_timer->run([&] { return Botan::create_private_key(alg, rng, param); });
56✔
52

53
         if(sk != nullptr) {
28✔
54
            while(keygen_timer->under(msec)) {
75✔
55
               sk = keygen_timer->run([&] { return Botan::create_private_key(alg, rng, param); });
141✔
56
            }
57

58
            config.record_result(*keygen_timer);
28✔
59

60
            auto pk = sk->public_key();
28✔
61

62
            std::vector<uint8_t> message;
28✔
63
            std::vector<uint8_t> signature;
28✔
64
            std::vector<uint8_t> bad_signature;
28✔
65

66
            Botan::PK_Signer sig(*sk, rng, padding, Botan::Signature_Format::Standard, provider);
28✔
67
            Botan::PK_Verifier ver(*pk, padding, Botan::Signature_Format::Standard, provider);
28✔
68

69
            auto sig_timer = config.make_timer(nm, 1, "sign");
56✔
70
            auto ver_timer = config.make_timer(nm, 1, "verify");
56✔
71

72
            size_t invalid_sigs = 0;
28✔
73

74
            while(ver_timer->under(msec) || sig_timer->under(msec)) {
160✔
75
               if(signature.empty() || sig_timer->under(msec)) {
104✔
76
                  /*
77
                  Length here is kind of arbitrary, but 48 bytes fits into a single
78
                  hash block so minimizes hashing overhead versus the PK op itself.
79
                  */
80
                  rng.random_vec(message, 48);
99✔
81

82
                  signature = sig_timer->run([&]() { return sig.sign_message(message, rng); });
198✔
83

84
                  bad_signature = signature;
99✔
85
                  bad_signature[rng.next_byte() % bad_signature.size()] ^= rng.next_nonzero_byte();
99✔
86
               }
87

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

91
                  if(!verified) {
44✔
92
                     invalid_sigs += 1;
×
93
                  }
94

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

97
                  if(verified_bad) {
44✔
98
                     config.error_output() << "Bad signature accepted in " << nm << " signature bench\n";
×
99
                  }
100
               }
101
            }
102

103
            if(invalid_sigs > 0) {
28✔
104
               config.error_output() << invalid_sigs << " generated signatures rejected in " << nm
×
105
                                     << " signature bench\n";
×
106
            }
107
            config.record_result(*sig_timer);
28✔
108
            config.record_result(*ver_timer);
56✔
109
         }
56✔
110
      }
28✔
111
};
112

113
#endif
114

115
#if defined(BOTAN_HAS_DSA)
116

117
class PerfTest_DSA final : public PerfTest_PKSig {
1✔
118
   public:
119
      std::string algo() const override { return "DSA"; }
1✔
120

121
      std::vector<std::string> keygen_params(const PerfConfig& /*config*/) const override {
1✔
122
         return {"dsa/jce/1024", "dsa/botan/2048", "dsa/botan/3072"};
1✔
123
      }
124

125
      std::string format_name(const std::string& alg, const std::string& param) const override {
3✔
126
         return Botan::fmt("{}-{}", alg, param.substr(param.find_last_of('/') + 1));
6✔
127
      }
128
};
129

130
BOTAN_REGISTER_PERF_TEST("DSA", PerfTest_DSA);
1✔
131

132
#endif
133

134
#if defined(BOTAN_HAS_RSA)
135

136
class PerfTest_RSA final : public PerfTest_PKSig {
×
137
   public:
138
      std::string algo() const override { return "RSA"; }
×
139

140
      std::string hash() const override { return "PKCS1v15(SHA-256)"; }
×
141

142
      std::vector<std::string> keygen_params(const PerfConfig& /*config*/) const override {
×
143
         return {"1024", "2048", "3072", "4096"};
×
144
      }
145
};
146

147
BOTAN_REGISTER_PERF_TEST("RSA", PerfTest_RSA);
×
148

149
#endif
150

151
#if defined(BOTAN_HAS_ECDSA)
152

153
class PerfTest_ECDSA final : public PerfTest_PKSig {
1✔
154
   public:
155
      std::string algo() const override { return "ECDSA"; }
1✔
156

157
      std::vector<std::string> keygen_params(const PerfConfig& config) const override { return config.ecc_groups(); }
1✔
158
};
159

160
BOTAN_REGISTER_PERF_TEST("ECDSA", PerfTest_ECDSA);
1✔
161

162
#endif
163

164
#if defined(BOTAN_HAS_ECKCDSA)
165

166
class PerfTest_ECKCDSA final : public PerfTest_PKSig {
1✔
167
   public:
168
      std::string algo() const override { return "ECKCDSA"; }
1✔
169

170
      std::vector<std::string> keygen_params(const PerfConfig& config) const override { return config.ecc_groups(); }
1✔
171
};
172

173
BOTAN_REGISTER_PERF_TEST("ECKCDSA", PerfTest_ECKCDSA);
1✔
174

175
#endif
176

177
#if defined(BOTAN_HAS_ECGDSA)
178

179
class PerfTest_ECGDSA final : public PerfTest_PKSig {
1✔
180
   public:
181
      std::string algo() const override { return "ECGDSA"; }
1✔
182

183
      std::vector<std::string> keygen_params(const PerfConfig& config) const override { return config.ecc_groups(); }
1✔
184
};
185

186
BOTAN_REGISTER_PERF_TEST("ECGDSA", PerfTest_ECGDSA);
1✔
187

188
#endif
189

190
#if defined(BOTAN_HAS_GOST_34_10_2001) && defined(BOTAN_HAS_GOST_34_11)
191

192
class PerfTest_Gost3410 final : public PerfTest_PKSig {
1✔
193
   public:
194
      std::string algo() const override { return "GOST-34.10"; }
1✔
195

196
      std::string hash() const override { return "GOST-34.11"; }
1✔
197

198
      std::vector<std::string> keygen_params(const PerfConfig& /*config*/) const override { return {"gost_256A"}; }
1✔
199
};
200

201
BOTAN_REGISTER_PERF_TEST("GOST-34.10", PerfTest_Gost3410);
1✔
202

203
#endif
204

205
#if defined(BOTAN_HAS_SM2) && defined(BOTAN_HAS_SM3)
206

207
class PerfTest_SM2 final : public PerfTest_PKSig {
1✔
208
   public:
209
      std::string algo() const override { return "SM2"; }
1✔
210

211
      std::string hash() const override { return "SM3"; }
1✔
212

213
      std::vector<std::string> keygen_params(const PerfConfig& /*config*/) const override { return {"sm2p256v1"}; }
1✔
214
};
215

216
BOTAN_REGISTER_PERF_TEST("SM2", PerfTest_SM2);
1✔
217

218
#endif
219

220
#if defined(BOTAN_HAS_ED25519)
221

222
class PerfTest_Ed25519 final : public PerfTest_PKSig {
1✔
223
   public:
224
      std::string algo() const override { return "Ed25519"; }
1✔
225

226
      std::string hash() const override { return "Pure"; }
1✔
227
};
228

229
BOTAN_REGISTER_PERF_TEST("Ed25519", PerfTest_Ed25519);
1✔
230

231
#endif
232

233
#if defined(BOTAN_HAS_ED448)
234

235
class PerfTest_Ed448 final : public PerfTest_PKSig {
1✔
236
   public:
237
      std::string algo() const override { return "Ed448"; }
1✔
238

239
      std::string hash() const override { return "Pure"; }
1✔
240
};
241

242
BOTAN_REGISTER_PERF_TEST("Ed448", PerfTest_Ed448);
1✔
243

244
#endif
245

246
#if defined(BOTAN_HAS_XMSS_RFC8391)
247

248
class PerfTest_XMSS final : public PerfTest_PKSig {
×
249
   public:
250
      std::string algo() const override { return "XMSS"; }
×
251

252
      std::string hash() const override { return ""; }
×
253

254
      std::vector<std::string> keygen_params(const PerfConfig& /*config*/) const override {
×
255
         /*
256
         We only test H10 signatures here since already they are quite slow (a
257
         few seconds per signature). On a fast machine, H16 signatures take 1-2
258
         minutes to generate and H20 signatures take 5-10 minutes to generate
259
         */
260
         return {
×
261
            "XMSS-SHA2_10_256",
262
            "XMSS-SHAKE_10_256",
263
            "XMSS-SHA2_10_512",
264
            "XMSS-SHAKE_10_512",
265
         };
×
266
      }
267
};
268

269
BOTAN_REGISTER_PERF_TEST("XMSS", PerfTest_XMSS);
×
270

271
#endif
272

273
#if defined(BOTAN_HAS_HSS_LMS)
274

275
class PerfTest_HSS_LMS final : public PerfTest_PKSig {
×
276
   public:
277
      std::string algo() const override { return "HSS-LMS"; }
×
278

279
      std::string hash() const override { return ""; }
×
280

281
      std::vector<std::string> keygen_params(const PerfConfig& /*config*/) const override {
×
282
         // At first we compare instances with multiple hash functions. LMS trees with
283
         // height 10 are suitable, since they can be used for enough signatures and are
284
         // fast enough for speed testing.
285
         // Afterward, setups with multiple HSS layers are tested
286
         return {"SHA-256,HW(10,1)",
×
287
                 "SHAKE-256(256),HW(10,1)",
288
                 "SHAKE-256(192),HW(10,1)",
289
                 "Truncated(SHA-256,192),HW(10,1)",
290
                 "SHA-256,HW(10,1),HW(10,1)",
291
                 "SHA-256,HW(10,1),HW(10,1),HW(10,1)"};
×
292
      }
293
};
294

295
BOTAN_REGISTER_PERF_TEST("HSS-LMS", PerfTest_HSS_LMS);
×
296

297
#endif
298

299
#if defined(BOTAN_HAS_SPHINCS_PLUS_WITH_SHA2) || defined(BOTAN_HAS_SPHINCS_PLUS_WITH_SHAKE)
300

301
class PerfTest_SPHINCSp final : public PerfTest_PKSig {
×
302
   public:
303
      std::string algo() const override { return "SPHINCS+"; }
×
304

305
      std::string hash() const override { return ""; }
×
306

307
      std::string format_name(const std::string& alg, const std::string& param) const override {
×
308
         return alg + param.substr(11);
×
309
      }
310

311
      std::vector<std::string> keygen_params(const PerfConfig& /*config*/) const override {
×
312
         return {"SphincsPlus-sha2-128s-r3.1",
×
313
                 "SphincsPlus-sha2-128f-r3.1",
314
                 "SphincsPlus-sha2-192s-r3.1",
315
                 "SphincsPlus-sha2-192f-r3.1",
316
                 "SphincsPlus-sha2-256s-r3.1",
317
                 "SphincsPlus-sha2-256f-r3.1",
318
                 "SphincsPlus-shake-128s-r3.1",
319
                 "SphincsPlus-shake-128f-r3.1",
320
                 "SphincsPlus-shake-192s-r3.1",
321
                 "SphincsPlus-shake-192f-r3.1",
322
                 "SphincsPlus-shake-256s-r3.1",
323
                 "SphincsPlus-shake-256f-r3.1"};
×
324
      }
325
};
326

327
BOTAN_REGISTER_PERF_TEST("SPHINCS+", PerfTest_SPHINCSp);
×
328

329
#endif
330

331
#if defined(BOTAN_HAS_SLH_DSA_WITH_SHA2) || defined(BOTAN_HAS_SLH_DSA_WITH_SHAKE)
332

333
class PerfTest_SLH_DSA final : public PerfTest_PKSig {
×
334
   public:
335
      std::string algo() const override { return "SLH-DSA"; }
×
336

337
      std::string hash() const override { return ""; }
×
338

339
      std::vector<std::string> keygen_params(const PerfConfig& /*config*/) const override {
×
340
         return {"SLH-DSA-SHA2-128s",
×
341
                 "SLH-DSA-SHA2-128f",
342
                 "SLH-DSA-SHA2-192s",
343
                 "SLH-DSA-SHA2-192f",
344
                 "SLH-DSA-SHA2-256s",
345
                 "SLH-DSA-SHA2-256f",
346
                 "SLH-DSA-SHAKE-128s",
347
                 "SLH-DSA-SHAKE-128f",
348
                 "SLH-DSA-SHAKE-192s",
349
                 "SLH-DSA-SHAKE-192f",
350
                 "SLH-DSA-SHAKE-256s",
351
                 "SLH-DSA-SHAKE-256f"};
×
352
      }
353
};
354

355
BOTAN_REGISTER_PERF_TEST("SLH-DSA", PerfTest_SLH_DSA);
×
356

357
#endif
358

359
#if defined(BOTAN_HAS_DILITHIUM) || defined(BOTAN_HAS_DILITHIUM_AES)
360

361
class PerfTest_Dilithium final : public PerfTest_PKSig {
×
362
   public:
363
      std::string algo() const override { return "Dilithium"; }
×
364

365
      std::string hash() const override { return ""; }
×
366

367
      std::vector<std::string> keygen_params(const PerfConfig& /*config*/) const override {
×
368
         return {
×
369
            "Dilithium-4x4-r3",
370
            "Dilithium-4x4-AES-r3",
371
            "Dilithium-6x5-r3",
372
            "Dilithium-6x5-AES-r3",
373
            "Dilithium-8x7-r3",
374
            "Dilithium-8x7-AES-r3",
375
         };
×
376
      }
377
};
378

379
BOTAN_REGISTER_PERF_TEST("Dilithium", PerfTest_Dilithium);
×
380

381
#endif
382

383
#if defined(BOTAN_HAS_ML_DSA)
384

385
class PerfTest_ML_DSA final : public PerfTest_PKSig {
1✔
386
   public:
387
      std::string algo() const override { return "ML-DSA"; }
1✔
388

389
      std::string hash() const override { return ""; }
1✔
390

391
      std::vector<std::string> keygen_params(const PerfConfig& /*config*/) const override {
1✔
392
         return {
1✔
393
            "ML-DSA-4x4",
394
            "ML-DSA-6x5",
395
            "ML-DSA-8x7",
396
         };
1✔
397
      }
398
};
399

400
BOTAN_REGISTER_PERF_TEST("ML-DSA", PerfTest_ML_DSA);
1✔
401

402
#endif
403

404
}  // namespace Botan_CLI
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc