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

randombit / botan / 13089344942

01 Feb 2025 02:06PM UTC coverage: 91.224% (-0.003%) from 91.227%
13089344942

push

github

web-flow
Merge pull request #4622 from randombit/jack/cli-perf-keygen-fix

In public key perf tests exit early if keygen fails

94169 of 103228 relevant lines covered (91.22%)

11304027.04 hits per line

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

79.87
/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
#endif
13

14
namespace Botan_CLI {
15

16
#if defined(BOTAN_HAS_PUBLIC_KEY_CRYPTO)
17

18
class PerfTest_PKSig : public PerfTest {
12✔
19
   public:
20
      virtual std::string algo() const = 0;
21

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

24
      virtual std::vector<std::string> keygen_params(const PerfConfig& config) const {
2✔
25
         BOTAN_UNUSED(config);
2✔
26
         return {""};
2✔
27
      }
28

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

33
         const auto params = this->keygen_params(config);
12✔
34

35
         for(const auto& param : params) {
63✔
36
            const std::string nm = this->format_name(alg, param);
51✔
37
            bench_pk_sig(config, nm, alg, param, padding);
102✔
38
         }
51✔
39
      }
12✔
40

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

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

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

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

59
            config.record_result(*keygen_timer);
51✔
60

61
            auto pk = sk->public_key();
51✔
62

63
            std::vector<uint8_t> message, signature, bad_signature;
51✔
64

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

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

71
            size_t invalid_sigs = 0;
51✔
72

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

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

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

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

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

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

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

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

112
#endif
113

114
#if defined(BOTAN_HAS_DSA)
115

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

120
      std::vector<std::string> keygen_params(const PerfConfig& config) const override {
1✔
121
         BOTAN_UNUSED(config);
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 {
1✔
137
   public:
138
      std::string algo() const override { return "RSA"; }
1✔
139

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

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

148
BOTAN_REGISTER_PERF_TEST("RSA", PerfTest_RSA);
1✔
149

150
#endif
151

152
#if defined(BOTAN_HAS_ECDSA)
153

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

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

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

163
#endif
164

165
#if defined(BOTAN_HAS_ECKCDSA)
166

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

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

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

176
#endif
177

178
#if defined(BOTAN_HAS_ECGDSA)
179

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

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

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

189
#endif
190

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

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

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

199
      std::vector<std::string> keygen_params(const PerfConfig& config) const override {
1✔
200
         BOTAN_UNUSED(config);
1✔
201
         return {"gost_256A"};
1✔
202
      }
203
};
204

205
BOTAN_REGISTER_PERF_TEST("GOST-34.10", PerfTest_Gost3410);
1✔
206

207
#endif
208

209
#if defined(BOTAN_HAS_SM2) && defined(BOTAN_HAS_SM3)
210

211
class PerfTest_SM2 final : public PerfTest_PKSig {
1✔
212
   public:
213
      std::string algo() const override { return "SM2"; }
1✔
214

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

217
      std::vector<std::string> keygen_params(const PerfConfig& config) const override {
1✔
218
         BOTAN_UNUSED(config);
1✔
219
         return {"sm2p256v1"};
1✔
220
      }
221
};
222

223
BOTAN_REGISTER_PERF_TEST("SM2", PerfTest_SM2);
1✔
224

225
#endif
226

227
#if defined(BOTAN_HAS_ED25519)
228

229
class PerfTest_Ed25519 final : public PerfTest_PKSig {
1✔
230
   public:
231
      std::string algo() const override { return "Ed25519"; }
1✔
232

233
      std::string hash() const override { return "Pure"; }
1✔
234
};
235

236
BOTAN_REGISTER_PERF_TEST("Ed25519", PerfTest_Ed25519);
1✔
237

238
#endif
239

240
#if defined(BOTAN_HAS_ED448)
241

242
class PerfTest_Ed448 final : public PerfTest_PKSig {
1✔
243
   public:
244
      std::string algo() const override { return "Ed448"; }
1✔
245

246
      std::string hash() const override { return "Pure"; }
1✔
247
};
248

249
BOTAN_REGISTER_PERF_TEST("Ed448", PerfTest_Ed448);
1✔
250

251
#endif
252

253
#if defined(BOTAN_HAS_XMSS_RFC8391)
254

255
class PerfTest_XMSS final : public PerfTest_PKSig {
1✔
256
   public:
257
      std::string algo() const override { return "XMSS"; }
1✔
258

259
      std::string hash() const override { return ""; }
1✔
260

261
      std::vector<std::string> keygen_params(const PerfConfig& config) const override {
1✔
262
         BOTAN_UNUSED(config);
1✔
263

264
         /*
265
         We only test H10 signatures here since already they are quite slow (a
266
         few seconds per signature). On a fast machine, H16 signatures take 1-2
267
         minutes to generate and H20 signatures take 5-10 minutes to generate
268
         */
269
         return {
1✔
270
            "XMSS-SHA2_10_256",
271
            "XMSS-SHAKE_10_256",
272
            "XMSS-SHA2_10_512",
273
            "XMSS-SHAKE_10_512",
274
         };
1✔
275
      }
276
};
277

278
BOTAN_REGISTER_PERF_TEST("XMSS", PerfTest_XMSS);
1✔
279

280
#endif
281

282
#if defined(BOTAN_HAS_HSS_LMS)
283

284
class PerfTest_HSS_LMS final : public PerfTest_PKSig {
×
285
   public:
286
      std::string algo() const override { return "HSS-LMS"; }
×
287

288
      std::string hash() const override { return ""; }
×
289

290
      std::vector<std::string> keygen_params(const PerfConfig& config) const override {
×
291
         BOTAN_UNUSED(config);
×
292

293
         // At first we compare instances with multiple hash functions. LMS trees with
294
         // height 10 are suitable, since they can be used for enough signatures and are
295
         // fast enough for speed testing.
296
         // Afterward, setups with multiple HSS layers are tested
297
         return {"SHA-256,HW(10,1)",
×
298
                 "SHAKE-256(256),HW(10,1)",
299
                 "SHAKE-256(192),HW(10,1)",
300
                 "Truncated(SHA-256,192),HW(10,1)",
301
                 "SHA-256,HW(10,1),HW(10,1)",
302
                 "SHA-256,HW(10,1),HW(10,1),HW(10,1)"};
×
303
      }
304
};
305

306
BOTAN_REGISTER_PERF_TEST("HSS-LMS", PerfTest_HSS_LMS);
×
307

308
#endif
309

310
#if defined(BOTAN_HAS_SPHINCS_PLUS_WITH_SHA2) || defined(BOTAN_HAS_SPHINCS_PLUS_WITH_SHAKE)
311

312
class PerfTest_SPHINCSp final : public PerfTest_PKSig {
×
313
   public:
314
      std::string algo() const override { return "SPHINCS+"; }
×
315

316
      std::string hash() const override { return ""; }
×
317

318
      std::string format_name(const std::string& alg, const std::string& param) const override {
×
319
         return alg + param.substr(11);
×
320
      }
321

322
      std::vector<std::string> keygen_params(const PerfConfig& config) const override {
×
323
         BOTAN_UNUSED(config);
×
324

325
         return {"SphincsPlus-sha2-128s-r3.1",
×
326
                 "SphincsPlus-sha2-128f-r3.1",
327
                 "SphincsPlus-sha2-192s-r3.1",
328
                 "SphincsPlus-sha2-192f-r3.1",
329
                 "SphincsPlus-sha2-256s-r3.1",
330
                 "SphincsPlus-sha2-256f-r3.1",
331
                 "SphincsPlus-shake-128s-r3.1",
332
                 "SphincsPlus-shake-128f-r3.1",
333
                 "SphincsPlus-shake-192s-r3.1",
334
                 "SphincsPlus-shake-192f-r3.1",
335
                 "SphincsPlus-shake-256s-r3.1",
336
                 "SphincsPlus-shake-256f-r3.1"};
×
337
      }
338
};
339

340
BOTAN_REGISTER_PERF_TEST("SPHINCS+", PerfTest_SPHINCSp);
×
341

342
#endif
343

344
#if defined(BOTAN_HAS_SLH_DSA_WITH_SHA2) || defined(BOTAN_HAS_SLH_DSA_WITH_SHAKE)
345

346
class PerfTest_SLH_DSA final : public PerfTest_PKSig {
1✔
347
   public:
348
      std::string algo() const override { return "SLH-DSA"; }
1✔
349

350
      std::string hash() const override { return ""; }
1✔
351

352
      std::vector<std::string> keygen_params(const PerfConfig& config) const override {
1✔
353
         BOTAN_UNUSED(config);
1✔
354

355
         return {"SLH-DSA-SHA2-128s",
1✔
356
                 "SLH-DSA-SHA2-128f",
357
                 "SLH-DSA-SHA2-192s",
358
                 "SLH-DSA-SHA2-192f",
359
                 "SLH-DSA-SHA2-256s",
360
                 "SLH-DSA-SHA2-256f",
361
                 "SLH-DSA-SHAKE-128s",
362
                 "SLH-DSA-SHAKE-128f",
363
                 "SLH-DSA-SHAKE-192s",
364
                 "SLH-DSA-SHAKE-192f",
365
                 "SLH-DSA-SHAKE-256s",
366
                 "SLH-DSA-SHAKE-256f"};
1✔
367
      }
368
};
369

370
BOTAN_REGISTER_PERF_TEST("SLH-DSA", PerfTest_SLH_DSA);
1✔
371

372
#endif
373

374
#if defined(BOTAN_HAS_DILITHIUM) || defined(BOTAN_HAS_DILITHIUM_AES)
375

376
class PerfTest_Dilithium final : public PerfTest_PKSig {
1✔
377
   public:
378
      std::string algo() const override { return "Dilithium"; }
1✔
379

380
      std::string hash() const override { return ""; }
1✔
381

382
      std::vector<std::string> keygen_params(const PerfConfig& config) const override {
1✔
383
         BOTAN_UNUSED(config);
1✔
384

385
         return {
1✔
386
            "Dilithium-4x4-r3",
387
            "Dilithium-4x4-AES-r3",
388
            "Dilithium-6x5-r3",
389
            "Dilithium-6x5-AES-r3",
390
            "Dilithium-8x7-r3",
391
            "Dilithium-8x7-AES-r3",
392
         };
1✔
393
      }
394
};
395

396
BOTAN_REGISTER_PERF_TEST("Dilithium", PerfTest_Dilithium);
1✔
397

398
#endif
399

400
#if defined(BOTAN_HAS_ML_DSA)
401

402
class PerfTest_ML_DSA final : public PerfTest_PKSig {
×
403
   public:
404
      std::string algo() const override { return "ML-DSA"; }
×
405

406
      std::string hash() const override { return ""; }
×
407

408
      std::vector<std::string> keygen_params(const PerfConfig& config) const override {
×
409
         BOTAN_UNUSED(config);
×
410

411
         return {
×
412
            "ML-DSA-4x4",
413
            "ML-DSA-6x5",
414
            "ML-DSA-8x7",
415
         };
×
416
      }
417
};
418

419
BOTAN_REGISTER_PERF_TEST("ML-DSA", PerfTest_ML_DSA);
×
420

421
#endif
422

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