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

randombit / botan / 12999101703

27 Jan 2025 02:24PM UTC coverage: 91.243% (-0.01%) from 91.254%
12999101703

push

github

web-flow
Merge pull request #4592 from randombit/jack/split-key-prework

Some initial refactoring for splitting public and private keys

93963 of 102981 relevant lines covered (91.24%)

11538552.33 hits per line

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

63.24
/src/cli/perf_pk_kem.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_PK_KEM : 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) {
7✔
33
            const std::string nm = this->format_name(alg, param);
6✔
34
            bench_pk_kem(config, nm, alg, param);
12✔
35
         }
6✔
36
      }
1✔
37

38
      void bench_pk_kem(const PerfConfig& config,
6✔
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();
6✔
44
         auto& rng = config.rng();
6✔
45

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

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

50
         auto sk = keygen_timer->run([&] { return Botan::create_private_key(algo, rng, params); });
12✔
51
         while(keygen_timer->under(msec)) {
27✔
52
            sk = keygen_timer->run([&] { return Botan::create_private_key(algo, rng, params); });
63✔
53
         }
54

55
         auto pk = sk->public_key();
6✔
56

57
         Botan::PK_KEM_Decryptor dec(*sk, rng, kdf, provider);
6✔
58
         Botan::PK_KEM_Encryptor enc(*pk, kdf, provider);
6✔
59

60
         auto kem_enc_timer = config.make_timer(nm, 1, "KEM encrypt");
12✔
61
         auto kem_dec_timer = config.make_timer(nm, 1, "KEM decrypt");
12✔
62

63
         while(kem_enc_timer->under(msec) && kem_dec_timer->under(msec)) {
51✔
64
            Botan::secure_vector<uint8_t> salt = rng.random_vec(16);
45✔
65

66
            kem_enc_timer->start();
45✔
67
            const auto kem_result = enc.encrypt(rng, 64, salt);
45✔
68
            kem_enc_timer->stop();
45✔
69

70
            kem_dec_timer->start();
45✔
71
            Botan::secure_vector<uint8_t> dec_shared_key = dec.decrypt(kem_result.encapsulated_shared_key(), 64, salt);
45✔
72
            kem_dec_timer->stop();
45✔
73

74
            if(kem_result.shared_key() != dec_shared_key) {
45✔
75
               config.error_output() << "KEM mismatch in PK bench\n";
×
76
            }
77
         }
90✔
78
         config.record_result(*keygen_timer);
6✔
79
         config.record_result(*kem_enc_timer);
6✔
80
         config.record_result(*kem_dec_timer);
12✔
81
      }
18✔
82
};
83

84
#endif
85

86
#if defined(BOTAN_HAS_KYBER) || defined(BOTAN_HAS_KYBER_90S)
87

88
class PerfTest_Kyber final : public PerfTest_PK_KEM {
1✔
89
   public:
90
      std::string algo() const override { return "Kyber"; }
1✔
91

92
      std::vector<std::string> keygen_params(const PerfConfig& config) const override {
1✔
93
         BOTAN_UNUSED(config);
1✔
94
         return {
1✔
95
            "Kyber-512-r3",
96
            "Kyber-512-90s-r3",
97
            "Kyber-768-r3",
98
            "Kyber-768-90s-r3",
99
            "Kyber-1024-r3",
100
            "Kyber-1024-90s-r3",
101
         };
1✔
102
      }
103
};
104

105
BOTAN_REGISTER_PERF_TEST("Kyber", PerfTest_Kyber);
1✔
106

107
#endif
108

109
#if defined(BOTAN_HAS_ML_KEM)
110

111
class PerfTest_ML_KEM final : public PerfTest_PK_KEM {
×
112
   public:
113
      std::string algo() const override { return "ML-KEM"; }
×
114

115
      std::vector<std::string> keygen_params(const PerfConfig& config) const override {
×
116
         BOTAN_UNUSED(config);
×
117
         return {
×
118
            "ML-KEM-512",
119
            "ML-KEM-768",
120
            "ML-KEM-1024",
121
         };
×
122
      }
123
};
124

125
BOTAN_REGISTER_PERF_TEST("ML-KEM", PerfTest_ML_KEM);
×
126

127
#endif
128

129
#if defined(BOTAN_HAS_FRODOKEM)
130

131
class PerfTest_FrodoKEM final : public PerfTest_PK_KEM {
×
132
   public:
133
      std::string algo() const override { return "FrodoKEM"; }
×
134

135
      std::vector<std::string> keygen_params(const PerfConfig& config) const override {
×
136
         BOTAN_UNUSED(config);
×
137
         return {
×
138
            "FrodoKEM-640-SHAKE",
139
            "FrodoKEM-640-AES",
140
            "eFrodoKEM-640-SHAKE",
141
            "eFrodoKEM-640-AES",
142
            "FrodoKEM-976-SHAKE",
143
            "FrodoKEM-976-AES",
144
            "eFrodoKEM-976-SHAKE",
145
            "eFrodoKEM-976-AES",
146
            "FrodoKEM-1344-SHAKE",
147
            "FrodoKEM-1344-AES",
148
            "eFrodoKEM-1344-SHAKE",
149
            "eFrodoKEM-1344-AES",
150
         };
×
151
      }
152
};
153

154
BOTAN_REGISTER_PERF_TEST("FrodoKEM", PerfTest_FrodoKEM);
×
155

156
#endif
157

158
#if defined(BOTAN_HAS_CLASSICMCELIECE)
159

160
class PerfTest_Classic_McEliece final : public PerfTest_PK_KEM {
×
161
   public:
162
      std::string algo() const override { return "ClassicMcEliece"; }
×
163

164
      std::vector<std::string> keygen_params(const PerfConfig& config) const override {
×
165
         BOTAN_UNUSED(config);
×
166
         return {
×
167
            "mceliece348864",
168
            "mceliece348864f",
169
            "mceliece460896",
170
            "mceliece460896f",
171
            "mceliece6688128",
172
            "mceliece6688128f",
173
            "mceliece6688128pc",
174
            "mceliece6688128pcf",
175
            "mceliece6960119",
176
            "mceliece6960119f",
177
            "mceliece6960119pc",
178
            "mceliece6960119pcf",
179
            "mceliece8192128",
180
            "mceliece8192128f",
181
            "mceliece8192128pc",
182
            "mceliece8192128pcf",
183
         };
×
184
      }
185
};
186

187
BOTAN_REGISTER_PERF_TEST("ClassicMcEliece", PerfTest_Classic_McEliece);
×
188

189
#endif
190

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