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

randombit / botan / 11507839509

24 Oct 2024 09:35PM UTC coverage: 91.139% (+0.006%) from 91.133%
11507839509

push

github

web-flow
Merge pull request #4381 from cr-marcstevens/master

cli speed: also measure keygen for specified duration

91069 of 99923 relevant lines covered (91.14%)

9355539.52 hits per line

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

69.49
/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();
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 key = keygen_timer->run([&] { return Botan::create_private_key(algo, rng, params); });
12✔
51
         while(keygen_timer->under(msec)) {
26✔
52
            key = keygen_timer->run([&] { return Botan::create_private_key(algo, rng, params); });
60✔
53
         }
54

55
         Botan::PK_KEM_Decryptor dec(*key, rng, kdf, provider);
6✔
56
         Botan::PK_KEM_Encryptor enc(*key, kdf, provider);
6✔
57

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

61
         while(kem_enc_timer->under(msec) && kem_dec_timer->under(msec)) {
50✔
62
            Botan::secure_vector<uint8_t> salt = rng.random_vec(16);
44✔
63

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

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

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

82
#endif
83

84
#if defined(BOTAN_HAS_KYBER) || defined(BOTAN_HAS_KYBER_90S)
85

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

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

103
BOTAN_REGISTER_PERF_TEST("Kyber", PerfTest_Kyber);
1✔
104

105
#endif
106

107
#if defined(BOTAN_HAS_ML_KEM)
108

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

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

123
BOTAN_REGISTER_PERF_TEST("ML-KEM", PerfTest_ML_KEM);
×
124

125
#endif
126

127
#if defined(BOTAN_HAS_FRODOKEM)
128

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

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

152
BOTAN_REGISTER_PERF_TEST("FrodoKEM", PerfTest_FrodoKEM);
×
153

154
#endif
155

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