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

randombit / botan / 21786344715

07 Feb 2026 08:25PM UTC coverage: 90.068% (-0.005%) from 90.073%
21786344715

Pull #5295

github

web-flow
Merge 8d5fc3b23 into ebf8f0044
Pull Request #5295: Reduce header dependencies in tests and cli

102234 of 113507 relevant lines covered (90.07%)

11372278.81 hits per line

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

98.18
/src/cli/perf_pk_ka.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
   #include <botan/internal/fmt.h>
13
#endif
14

15
namespace Botan_CLI {
16

17
#if defined(BOTAN_HAS_PUBLIC_KEY_CRYPTO)
18

19
class PerfTest_PKKa : public PerfTest {
4✔
20
   public:
21
      virtual std::string algo() const = 0;
22

23
      virtual std::vector<std::string> keygen_params(const PerfConfig& /*config*/) const { return {""}; }
2✔
24

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

28
         const auto params = this->keygen_params(config);
4✔
29

30
         for(const auto& param : params) {
17✔
31
            const std::string nm = this->format_name(alg, param);
13✔
32
            bench_pk_ka(config, nm, alg, param);
26✔
33
         }
13✔
34
      }
4✔
35

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

43
         const std::string kdf = "KDF2(SHA-256)";  // arbitrary choice
13✔
44

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

47
         auto& rng = config.rng();
13✔
48

49
         auto key1 = keygen_timer->run([&] { return Botan::create_private_key(algo, rng, params); });
26✔
50
         auto key2 = keygen_timer->run([&] { return Botan::create_private_key(algo, rng, params); });
26✔
51

52
         if(key1 && key2) {
13✔
53
            while(keygen_timer->under(msec)) {
34✔
54
               key2 = keygen_timer->run([&] { return Botan::create_private_key(algo, rng, params); });
63✔
55
            }
56

57
            config.record_result(*keygen_timer);
13✔
58

59
            const Botan::PK_Key_Agreement_Key& ka_key1 = dynamic_cast<const Botan::PK_Key_Agreement_Key&>(*key1);
13✔
60
            const Botan::PK_Key_Agreement_Key& ka_key2 = dynamic_cast<const Botan::PK_Key_Agreement_Key&>(*key2);
13✔
61

62
            Botan::PK_Key_Agreement ka1(ka_key1, rng, kdf, provider);
13✔
63
            Botan::PK_Key_Agreement ka2(ka_key2, rng, kdf, provider);
13✔
64

65
            const std::vector<uint8_t> ka1_pub = ka_key1.public_value();
13✔
66
            const std::vector<uint8_t> ka2_pub = ka_key2.public_value();
13✔
67

68
            auto ka_timer = config.make_timer(nm, 1, "key agreements");
26✔
69

70
            while(ka_timer->under(msec)) {
32✔
71
               auto k1 = ka_timer->run([&]() { return ka1.derive_key(32, ka2_pub); });
38✔
72
               auto k2 = ka_timer->run([&]() { return ka2.derive_key(32, ka1_pub); });
38✔
73

74
               if(k1 != k2) {
19✔
75
                  config.error_output() << "Key agreement mismatch in PK bench\n";
×
76
               }
77
            }
38✔
78

79
            config.record_result(*ka_timer);
26✔
80
         }
39✔
81
      }
26✔
82
};
83

84
#endif
85

86
#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
87

88
class PerfTest_DH final : public PerfTest_PKKa {
1✔
89
   public:
90
      std::string algo() const override { return "DH"; }
1✔
91

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

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

107
BOTAN_REGISTER_PERF_TEST("DH", PerfTest_DH);
1✔
108

109
#endif
110

111
#if defined(BOTAN_HAS_ECDH)
112

113
class PerfTest_ECDH final : public PerfTest_PKKa {
1✔
114
   public:
115
      std::string algo() const override { return "ECDH"; }
1✔
116

117
      std::vector<std::string> keygen_params(const PerfConfig& config) const override { return config.ecc_groups(); }
1✔
118
};
119

120
BOTAN_REGISTER_PERF_TEST("ECDH", PerfTest_ECDH);
1✔
121

122
#endif
123

124
#if defined(BOTAN_HAS_X25519)
125

126
class PerfTest_X25519 final : public PerfTest_PKKa {
1✔
127
   public:
128
      std::string algo() const override { return "X25519"; }
1✔
129
};
130

131
BOTAN_REGISTER_PERF_TEST("X25519", PerfTest_X25519);
1✔
132

133
#endif
134

135
#if defined(BOTAN_HAS_X448)
136

137
class PerfTest_X448 final : public PerfTest_PKKa {
1✔
138
   public:
139
      std::string algo() const override { return "X448"; }
1✔
140
};
141

142
BOTAN_REGISTER_PERF_TEST("X448", PerfTest_X448);
1✔
143

144
#endif
145

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