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

randombit / botan / 11376677396

17 Oct 2024 12:10AM UTC coverage: 91.125% (+0.006%) from 91.119%
11376677396

push

github

web-flow
Merge pull request #4378 from randombit/jack/speed-cleanup

Cleanups after refactoring of speed cli

90992 of 99854 relevant lines covered (91.13%)

9363281.76 hits per line

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

79.45
/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
         ;
51✔
54

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

58
            std::vector<uint8_t> message, signature, bad_signature;
51✔
59

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

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

66
            size_t invalid_sigs = 0;
51✔
67

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

76
                  signature = sig_timer->run([&]() { return sig.sign_message(message, rng); });
232✔
77

78
                  bad_signature = signature;
116✔
79
                  bad_signature[rng.next_byte() % bad_signature.size()] ^= rng.next_nonzero_byte();
116✔
80
               }
81

82
               if(ver_timer->under(msec)) {
174✔
83
                  const bool verified = ver_timer->run([&] { return ver.verify_message(message, signature); });
256✔
84

85
                  if(!verified) {
128✔
86
                     invalid_sigs += 1;
×
87
                  }
88

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

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

97
            if(invalid_sigs > 0) {
51✔
98
               config.error_output() << invalid_sigs << " generated signatures rejected in " << nm
×
99
                                     << " signature bench\n";
×
100
            }
101

102
            config.record_result(*sig_timer);
51✔
103
            config.record_result(*ver_timer);
51✔
104
         }
51✔
105
      }
51✔
106
};
107

108
#endif
109

110
#if defined(BOTAN_HAS_DSA)
111

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

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

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

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

128
#endif
129

130
#if defined(BOTAN_HAS_RSA)
131

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

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

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

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

146
#endif
147

148
#if defined(BOTAN_HAS_ECDSA)
149

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

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

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

159
#endif
160

161
#if defined(BOTAN_HAS_ECKCDSA)
162

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

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

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

172
#endif
173

174
#if defined(BOTAN_HAS_ECGDSA)
175

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

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

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

185
#endif
186

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

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

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

195
      std::vector<std::string> keygen_params(const PerfConfig& config) const override {
1✔
196
         BOTAN_UNUSED(config);
1✔
197
         return {"gost_256A"};
1✔
198
      }
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 {
1✔
214
         BOTAN_UNUSED(config);
1✔
215
         return {"sm2p256v1"};
1✔
216
      }
217
};
218

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

221
#endif
222

223
#if defined(BOTAN_HAS_ED25519)
224

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

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

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

234
#endif
235

236
#if defined(BOTAN_HAS_ED448)
237

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

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

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

247
#endif
248

249
#if defined(BOTAN_HAS_XMSS_RFC8391)
250

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

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

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

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

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

276
#endif
277

278
#if defined(BOTAN_HAS_HSS_LMS)
279

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

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

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

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

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

304
#endif
305

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

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

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

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

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

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

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

338
#endif
339

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

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

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

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

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

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

368
#endif
369

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

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

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

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

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

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

394
#endif
395

396
#if defined(BOTAN_HAS_ML_DSA)
397

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

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

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

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

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

417
#endif
418

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