• 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

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
#endif
13

14
namespace Botan_CLI {
15

16
#if defined(BOTAN_HAS_PUBLIC_KEY_CRYPTO)
17

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

83
#endif
84

85
#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
86

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

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

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

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

108
#endif
109

110
#if defined(BOTAN_HAS_ECDH)
111

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

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

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

121
#endif
122

123
#if defined(BOTAN_HAS_X25519)
124

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

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

132
#endif
133

134
#if defined(BOTAN_HAS_X448)
135

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

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

143
#endif
144

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