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

randombit / botan / 11507839509

24 Oct 2024 09:35PM UTC coverage: 91.139% (+0.006%) from 91.133%
11507839509

push

github

web-flow
Merge pull request #4381 from cr-marcstevens/master

cli speed: also measure keygen for specified duration

91069 of 99923 relevant lines covered (91.14%)

9355539.52 hits per line

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

89.36
/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();
8✔
45

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

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

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

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

58
         Botan::PK_Encryptor_EME enc(*key, rng, padding, provider);
4✔
59
         Botan::PK_Decryptor_EME dec(*key, rng, padding, provider);
4✔
60

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

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

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

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

81
         config.record_result(*keygen_timer);
4✔
82
         config.record_result(*enc_timer);
4✔
83
         config.record_result(*dec_timer);
4✔
84
      }
16✔
85
};
86

87
#endif
88

89
#if defined(BOTAN_HAS_ELGAMAL)
90

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

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

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

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

112
#endif
113

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