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

randombit / botan / 13274522654

11 Feb 2025 11:26PM UTC coverage: 91.645% (-0.007%) from 91.652%
13274522654

push

github

web-flow
Merge pull request #4647 from randombit/jack/internal-assert-and-mem-ops

Avoid using mem_ops.h or assert.h in public headers

94854 of 103501 relevant lines covered (91.65%)

11334975.77 hits per line

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

95.74
/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, ciphertext;
4✔
44

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

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

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

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

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

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

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

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

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

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

82
            config.record_result(*keygen_timer);
4✔
83
            config.record_result(*enc_timer);
4✔
84
            config.record_result(*dec_timer);
8✔
85
         }
8✔
86
      }
12✔
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
         return {
1✔
99
            "modp/ietf/1024",
100
            "modp/ietf/2048",
101
            "modp/ietf/3072",
102
            "modp/ietf/4096",
103
         };
1✔
104
      }
105

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

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

113
#endif
114

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