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

randombit / botan / 12991133942

27 Jan 2025 02:24PM UTC coverage: 91.246% (-0.008%) from 91.254%
12991133942

push

github

web-flow
Merge pull request #4592 from randombit/jack/split-key-prework

Some initial refactoring for splitting public and private keys

93966 of 102981 relevant lines covered (91.25%)

11558329.57 hits per line

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

89.58
/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
         while(keygen_timer->under(msec)) {
4✔
52
            sk = keygen_timer->run([&] { return Botan::create_private_key(algo, rng, params); });
×
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)) {
12✔
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)) {
4✔
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
      }
20✔
87
};
88

89
#endif
90

91
#if defined(BOTAN_HAS_ELGAMAL)
92

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

97
      std::vector<std::string> keygen_params(const PerfConfig& config) const override {
1✔
98
         BOTAN_UNUSED(config);
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