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

randombit / botan / 11331948318

14 Oct 2024 05:01PM UTC coverage: 91.128% (+0.008%) from 91.12%
11331948318

Pull #3893

github

web-flow
Merge 22a97fa1d into ed74c9542
Pull Request #3893: PQC: ML-KEM

90438 of 99243 relevant lines covered (91.13%)

8992513.48 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
      virtual std::string format_name(const std::string& alg, const std::string& param) const {
9✔
28
         return param.empty() ? alg : Botan::fmt("{}-{}", alg, param);
9✔
29
      }
30

31
      void go(const PerfConfig& config) override {
1✔
32
         const std::string alg = this->algo();
1✔
33

34
         const auto params = this->keygen_params(config);
1✔
35

36
         for(const auto& param : params) {
10✔
37
            const std::string nm = this->format_name(alg, param);
9✔
38
            bench_pk_kem(config, nm, alg, param);
18✔
39
         }
9✔
40
      }
1✔
41

42
      void bench_pk_kem(const PerfConfig& config,
9✔
43
                        const std::string& nm,
44
                        const std::string& algo,
45
                        const std::string& params,
46
                        const std::string& provider = "") {
47
         const auto msec = config.runtime();
9✔
48
         auto& rng = config.rng();
9✔
49

50
         const std::string kdf = "KDF2(SHA-256)";  // arbitrary choice
9✔
51

52
         auto keygen_timer = config.make_timer(nm, 1, "keygen");
18✔
53

54
         auto key = keygen_timer->run([&] { return Botan::create_private_key(algo, rng, params); });
18✔
55

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

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

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

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

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

73
            if(kem_result.shared_key() != dec_shared_key) {
57✔
74
               config.error_output() << "KEM mismatch in PK bench\n";
×
75
            }
76
         }
114✔
77

78
         config.record_result(*kem_enc_timer);
9✔
79
         config.record_result(*kem_dec_timer);
9✔
80
      }
18✔
81
};
82

83
#endif
84

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

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

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

107
BOTAN_REGISTER_PERF_TEST("Kyber", PerfTest_Kyber);
1✔
108

109
#endif
110

111
#if defined(BOTAN_HAS_ML_KEM)
112

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

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

127
BOTAN_REGISTER_PERF_TEST("ML-KEM", PerfTest_ML_KEM);
×
128

129
#endif
130

131
#if defined(BOTAN_HAS_FRODOKEM)
132

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

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

156
BOTAN_REGISTER_PERF_TEST("FrodoKEM", PerfTest_FrodoKEM);
×
157

158
#endif
159

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