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

randombit / botan / 13089344942

01 Feb 2025 02:06PM UTC coverage: 91.224% (-0.003%) from 91.227%
13089344942

push

github

web-flow
Merge pull request #4622 from randombit/jack/cli-perf-keygen-fix

In public key perf tests exit early if keygen fails

94169 of 103228 relevant lines covered (91.22%)

11304027.04 hits per line

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

98.28
/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 {
2✔
23
         BOTAN_UNUSED(config);
2✔
24
         return {""};
2✔
25
      }
26

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

30
         const auto params = this->keygen_params(config);
4✔
31

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

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

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

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

49
         auto& rng = config.rng();
26✔
50

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

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

59
            config.record_result(*keygen_timer);
13✔
60

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

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

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

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

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

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

81
            config.record_result(*ka_timer);
26✔
82
         }
39✔
83
      }
26✔
84
};
85

86
#endif
87

88
#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
89

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

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

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

110
BOTAN_REGISTER_PERF_TEST("DH", PerfTest_DH);
1✔
111

112
#endif
113

114
#if defined(BOTAN_HAS_ECDH)
115

116
class PerfTest_ECDH final : public PerfTest_PKKa {
1✔
117
   public:
118
      std::string algo() const override { return "ECDH"; }
1✔
119

120
      std::vector<std::string> keygen_params(const PerfConfig& config) const override { return config.ecc_groups(); }
1✔
121
};
122

123
BOTAN_REGISTER_PERF_TEST("ECDH", PerfTest_ECDH);
1✔
124

125
#endif
126

127
#if defined(BOTAN_HAS_X25519)
128

129
class PerfTest_X25519 final : public PerfTest_PKKa {
1✔
130
   public:
131
      std::string algo() const override { return "X25519"; }
1✔
132
};
133

134
BOTAN_REGISTER_PERF_TEST("X25519", PerfTest_X25519);
1✔
135

136
#endif
137

138
#if defined(BOTAN_HAS_X448)
139

140
class PerfTest_X448 final : public PerfTest_PKKa {
1✔
141
   public:
142
      std::string algo() const override { return "X448"; }
1✔
143
};
144

145
BOTAN_REGISTER_PERF_TEST("X448", PerfTest_X448);
1✔
146

147
#endif
148

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