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

randombit / botan / 16395825001

19 Jul 2025 11:30PM UTC coverage: 90.635% (-0.07%) from 90.708%
16395825001

push

github

web-flow
Merge pull request #4998 from randombit/jack/fix-clang-tidy-readability-isolate-declaration

Enable and fix clang-tidy warning readability-isolate-declaration

99940 of 110266 relevant lines covered (90.64%)

12341110.13 hits per line

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

95.83
/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 { return {""}; }
×
23

24
      void go(const PerfConfig& config) override {
1✔
25
         const std::string alg = this->algo();
1✔
26

27
         const auto params = this->keygen_params(config);
1✔
28

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

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

43
         std::vector<uint8_t> plaintext;
4✔
44
         std::vector<uint8_t> ciphertext;
4✔
45

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

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

50
         if(sk) {
4✔
51
            while(keygen_timer->under(msec)) {
8✔
52
               sk = keygen_timer->run([&] { return Botan::create_private_key(algo, rng, params); });
12✔
53
            }
54

55
            auto pk = sk->public_key();
4✔
56

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

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

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

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

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

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

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

90
#endif
91

92
#if defined(BOTAN_HAS_ELGAMAL)
93

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

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

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

112
BOTAN_REGISTER_PERF_TEST("ElGamal", PerfTest_ElGamal);
1✔
113

114
#endif
115

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