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

randombit / botan / 11507839509

24 Oct 2024 09:35PM UTC coverage: 91.139% (+0.006%) from 91.133%
11507839509

push

github

web-flow
Merge pull request #4381 from cr-marcstevens/master

cli speed: also measure keygen for specified duration

91069 of 99923 relevant lines covered (91.14%)

9355539.52 hits per line

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

79.59
/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();
49

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

52
         auto key = keygen_timer->run([&] { return Botan::create_private_key(alg, rng, param); });
102✔
53
         while(keygen_timer->under(msec)) {
88✔
54
            key = keygen_timer->run([&] { return Botan::create_private_key(alg, rng, param); });
111✔
55
         }
56

57
         if(key != nullptr) {
51✔
58
            config.record_result(*keygen_timer);
51✔
59

60
            std::vector<uint8_t> message, signature, bad_signature;
51✔
61

62
            Botan::PK_Signer sig(*key, rng, padding, Botan::Signature_Format::Standard, provider);
51✔
63
            Botan::PK_Verifier ver(*key, padding, Botan::Signature_Format::Standard, provider);
51✔
64

65
            auto sig_timer = config.make_timer(nm, 1, "sign");
102✔
66
            auto ver_timer = config.make_timer(nm, 1, "verify");
102✔
67

68
            size_t invalid_sigs = 0;
51✔
69

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

78
                  signature = sig_timer->run([&]() { return sig.sign_message(message, rng); });
226✔
79

80
                  bad_signature = signature;
113✔
81
                  bad_signature[rng.next_byte() % bad_signature.size()] ^= rng.next_nonzero_byte();
113✔
82
               }
83

84
               if(ver_timer->under(msec)) {
171✔
85
                  const bool verified = ver_timer->run([&] { return ver.verify_message(message, signature); });
250✔
86

87
                  if(!verified) {
125✔
88
                     invalid_sigs += 1;
×
89
                  }
90

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

93
                  if(verified_bad) {
125✔
94
                     config.error_output() << "Bad signature accepted in " << nm << " signature bench\n";
×
95
                  }
96
               }
97
            }
98

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

109
#endif
110

111
#if defined(BOTAN_HAS_DSA)
112

113
class PerfTest_DSA final : public PerfTest_PKSig {
1✔
114
   public:
115
      std::string algo() const override { return "DSA"; }
1✔
116

117
      std::vector<std::string> keygen_params(const PerfConfig& config) const override {
1✔
118
         BOTAN_UNUSED(config);
1✔
119
         return {"dsa/jce/1024", "dsa/botan/2048", "dsa/botan/3072"};
1✔
120
      }
121

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

127
BOTAN_REGISTER_PERF_TEST("DSA", PerfTest_DSA);
1✔
128

129
#endif
130

131
#if defined(BOTAN_HAS_RSA)
132

133
class PerfTest_RSA final : public PerfTest_PKSig {
1✔
134
   public:
135
      std::string algo() const override { return "RSA"; }
1✔
136

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

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

145
BOTAN_REGISTER_PERF_TEST("RSA", PerfTest_RSA);
1✔
146

147
#endif
148

149
#if defined(BOTAN_HAS_ECDSA)
150

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

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

158
BOTAN_REGISTER_PERF_TEST("ECDSA", PerfTest_ECDSA);
1✔
159

160
#endif
161

162
#if defined(BOTAN_HAS_ECKCDSA)
163

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

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

171
BOTAN_REGISTER_PERF_TEST("ECKCDSA", PerfTest_ECKCDSA);
1✔
172

173
#endif
174

175
#if defined(BOTAN_HAS_ECGDSA)
176

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

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

184
BOTAN_REGISTER_PERF_TEST("ECGDSA", PerfTest_ECGDSA);
1✔
185

186
#endif
187

188
#if defined(BOTAN_HAS_GOST_34_10_2001) && defined(BOTAN_HAS_GOST_34_11)
189

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

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

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

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

204
#endif
205

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

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

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

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

220
BOTAN_REGISTER_PERF_TEST("SM2", PerfTest_SM2);
1✔
221

222
#endif
223

224
#if defined(BOTAN_HAS_ED25519)
225

226
class PerfTest_Ed25519 final : public PerfTest_PKSig {
1✔
227
   public:
228
      std::string algo() const override { return "Ed25519"; }
1✔
229

230
      std::string hash() const override { return "Pure"; }
1✔
231
};
232

233
BOTAN_REGISTER_PERF_TEST("Ed25519", PerfTest_Ed25519);
1✔
234

235
#endif
236

237
#if defined(BOTAN_HAS_ED448)
238

239
class PerfTest_Ed448 final : public PerfTest_PKSig {
1✔
240
   public:
241
      std::string algo() const override { return "Ed448"; }
1✔
242

243
      std::string hash() const override { return "Pure"; }
1✔
244
};
245

246
BOTAN_REGISTER_PERF_TEST("Ed448", PerfTest_Ed448);
1✔
247

248
#endif
249

250
#if defined(BOTAN_HAS_XMSS_RFC8391)
251

252
class PerfTest_XMSS final : public PerfTest_PKSig {
1✔
253
   public:
254
      std::string algo() const override { return "XMSS"; }
1✔
255

256
      std::string hash() const override { return ""; }
1✔
257

258
      std::vector<std::string> keygen_params(const PerfConfig& config) const override {
1✔
259
         BOTAN_UNUSED(config);
1✔
260

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

275
BOTAN_REGISTER_PERF_TEST("XMSS", PerfTest_XMSS);
1✔
276

277
#endif
278

279
#if defined(BOTAN_HAS_HSS_LMS)
280

281
class PerfTest_HSS_LMS final : public PerfTest_PKSig {
×
282
   public:
283
      std::string algo() const override { return "HSS-LMS"; }
×
284

285
      std::string hash() const override { return ""; }
×
286

287
      std::vector<std::string> keygen_params(const PerfConfig& config) const override {
×
288
         BOTAN_UNUSED(config);
×
289

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

303
BOTAN_REGISTER_PERF_TEST("HSS-LMS", PerfTest_HSS_LMS);
×
304

305
#endif
306

307
#if defined(BOTAN_HAS_SPHINCS_PLUS_WITH_SHA2) || defined(BOTAN_HAS_SPHINCS_PLUS_WITH_SHAKE)
308

309
class PerfTest_SPHINCSp final : public PerfTest_PKSig {
×
310
   public:
311
      std::string algo() const override { return "SPHINCS+"; }
×
312

313
      std::string hash() const override { return ""; }
×
314

315
      std::string format_name(const std::string& alg, const std::string& param) const override {
×
316
         return alg + param.substr(11);
×
317
      }
318

319
      std::vector<std::string> keygen_params(const PerfConfig& config) const override {
×
320
         BOTAN_UNUSED(config);
×
321

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

337
BOTAN_REGISTER_PERF_TEST("SPHINCS+", PerfTest_SPHINCSp);
×
338

339
#endif
340

341
#if defined(BOTAN_HAS_SLH_DSA_WITH_SHA2) || defined(BOTAN_HAS_SLH_DSA_WITH_SHAKE)
342

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

347
      std::string hash() const override { return ""; }
1✔
348

349
      std::vector<std::string> keygen_params(const PerfConfig& config) const override {
1✔
350
         BOTAN_UNUSED(config);
1✔
351

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

367
BOTAN_REGISTER_PERF_TEST("SLH-DSA", PerfTest_SLH_DSA);
1✔
368

369
#endif
370

371
#if defined(BOTAN_HAS_DILITHIUM) || defined(BOTAN_HAS_DILITHIUM_AES)
372

373
class PerfTest_Dilithium final : public PerfTest_PKSig {
1✔
374
   public:
375
      std::string algo() const override { return "Dilithium"; }
1✔
376

377
      std::string hash() const override { return ""; }
1✔
378

379
      std::vector<std::string> keygen_params(const PerfConfig& config) const override {
1✔
380
         BOTAN_UNUSED(config);
1✔
381

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

393
BOTAN_REGISTER_PERF_TEST("Dilithium", PerfTest_Dilithium);
1✔
394

395
#endif
396

397
#if defined(BOTAN_HAS_ML_DSA)
398

399
class PerfTest_ML_DSA final : public PerfTest_PKSig {
×
400
   public:
401
      std::string algo() const override { return "ML-DSA"; }
×
402

403
      std::string hash() const override { return ""; }
×
404

405
      std::vector<std::string> keygen_params(const PerfConfig& config) const override {
×
406
         BOTAN_UNUSED(config);
×
407

408
         return {
×
409
            "ML-DSA-4x4",
410
            "ML-DSA-6x5",
411
            "ML-DSA-8x7",
412
         };
×
413
      }
414
};
415

416
BOTAN_REGISTER_PERF_TEST("ML-DSA", PerfTest_ML_DSA);
×
417

418
#endif
419

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