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

randombit / botan / 11374557528

16 Oct 2024 10:03PM UTC coverage: 91.123% (+0.004%) from 91.119%
11374557528

Pull #4378

github

web-flow
Merge 8d74fef7c into 102c9d75c
Pull Request #4378: Cleanups after refactoring of speed cli

90990 of 99854 relevant lines covered (91.12%)

9473429.03 hits per line

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

90.91
/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

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

55
         Botan::PK_Encryptor_EME enc(*key, rng, padding, provider);
4✔
56
         Botan::PK_Decryptor_EME dec(*key, rng, padding, provider);
4✔
57

58
         auto enc_timer = config.make_timer(nm + " " + padding, 1, "encrypt");
8✔
59
         auto dec_timer = config.make_timer(nm + " " + padding, 1, "decrypt");
8✔
60

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

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

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

78
         config.record_result(*enc_timer);
4✔
79
         config.record_result(*dec_timer);
4✔
80
      }
16✔
81
};
82

83
#endif
84

85
#if defined(BOTAN_HAS_ELGAMAL)
86

87
class PerfTest_ElGamal final : public PerfTest_PKEnc {
1✔
88
   public:
89
      std::string algo() const override { return "ElGamal"; }
1✔
90

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

101
      std::string format_name(const std::string& alg, const std::string& param) const override {
4✔
102
         return Botan::fmt("{}-{}", alg, param.substr(param.find_last_of('/') + 1));
8✔
103
      }
104
};
105

106
BOTAN_REGISTER_PERF_TEST("ElGamal", PerfTest_ElGamal);
1✔
107

108
#endif
109

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