• 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

92.0
/src/cli/perf_pk_enc.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_PKEnc : public PerfTest {
1✔
19
   public:
20
      virtual std::string algo() const = 0;
21

22
      virtual std::vector<std::string> keygen_params(const PerfConfig& config) const {
×
23
         BOTAN_UNUSED(config);
×
24
         return {""};
×
25
      }
26

27
      void go(const PerfConfig& config) override {
1✔
28
         const std::string alg = this->algo();
1✔
29

30
         const auto params = this->keygen_params(config);
1✔
31

32
         for(const auto& param : params) {
5✔
33
            const std::string nm = this->format_name(alg, param);
4✔
34
            bench_pk_ka(config, nm, alg, param);
8✔
35
         }
4✔
36
      }
1✔
37

38
      void bench_pk_ka(const PerfConfig& config,
4✔
39
                       const std::string& nm,
40
                       const std::string& algo,
41
                       const std::string& params,
42
                       const std::string& provider = "") {
43
         auto& rng = config.rng();
4✔
44
         const auto msec = config.runtime();
4✔
45

46
         std::vector<uint8_t> plaintext, ciphertext;
4✔
47

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

50
         auto sk = keygen_timer->run([&] { return Botan::create_private_key(algo, rng, params); });
8✔
51

52
         if(sk) {
4✔
53
            while(keygen_timer->under(msec)) {
7✔
54
               sk = keygen_timer->run([&] { return Botan::create_private_key(algo, rng, params); });
9✔
55
            }
56

57
            auto pk = sk->public_key();
4✔
58

59
            // TODO this would have to be generalized for anything but RSA/ElGamal
60
            const std::string padding = "PKCS1v15";
4✔
61

62
            Botan::PK_Encryptor_EME enc(*pk, rng, padding, provider);
4✔
63
            Botan::PK_Decryptor_EME dec(*sk, rng, padding, provider);
4✔
64

65
            auto enc_timer = config.make_timer(nm + " " + padding, 1, "encrypt");
8✔
66
            auto dec_timer = config.make_timer(nm + " " + padding, 1, "decrypt");
8✔
67

68
            while(enc_timer->under(msec) || dec_timer->under(msec)) {
12✔
69
               // Generate a new random ciphertext to decrypt
70
               if(ciphertext.empty() || enc_timer->under(msec)) {
4✔
71
                  rng.random_vec(plaintext, enc.maximum_input_size());
4✔
72
                  ciphertext = enc_timer->run([&]() { return enc.encrypt(plaintext, rng); });
16✔
73
               }
74

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

78
                  // sanity check
79
                  if(!(Botan::unlock(dec_pt) == plaintext)) {
8✔
80
                     config.error_output() << "Bad roundtrip in PK encrypt/decrypt bench\n";
×
81
                  }
82
               }
4✔
83
            }
84

85
            config.record_result(*keygen_timer);
4✔
86
            config.record_result(*enc_timer);
4✔
87
            config.record_result(*dec_timer);
8✔
88
         }
8✔
89
      }
12✔
90
};
91

92
#endif
93

94
#if defined(BOTAN_HAS_ELGAMAL)
95

96
class PerfTest_ElGamal final : public PerfTest_PKEnc {
1✔
97
   public:
98
      std::string algo() const override { return "ElGamal"; }
1✔
99

100
      std::vector<std::string> keygen_params(const PerfConfig& config) const override {
1✔
101
         BOTAN_UNUSED(config);
1✔
102
         return {
1✔
103
            "modp/ietf/1024",
104
            "modp/ietf/2048",
105
            "modp/ietf/3072",
106
            "modp/ietf/4096",
107
         };
1✔
108
      }
109

110
      std::string format_name(const std::string& alg, const std::string& param) const override {
4✔
111
         return Botan::fmt("{}-{}", alg, param.substr(param.find_last_of('/') + 1));
8✔
112
      }
113
};
114

115
BOTAN_REGISTER_PERF_TEST("ElGamal", PerfTest_ElGamal);
1✔
116

117
#endif
118

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