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

randombit / botan / 11375854747

17 Oct 2024 12:10AM UTC coverage: 91.119%. Remained the same
11375854747

push

github

web-flow
Merge pull request #4378 from randombit/jack/speed-cleanup

Cleanups after refactoring of speed cli

90986 of 99854 relevant lines covered (91.12%)

9328544.88 hits per line

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

67.86
/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

52
         Botan::PK_KEM_Decryptor dec(*key, rng, kdf, provider);
6✔
53
         Botan::PK_KEM_Encryptor enc(*key, kdf, provider);
6✔
54

55
         auto kem_enc_timer = config.make_timer(nm, 1, "KEM encrypt");
12✔
56
         auto kem_dec_timer = config.make_timer(nm, 1, "KEM decrypt");
12✔
57

58
         while(kem_enc_timer->under(msec) && kem_dec_timer->under(msec)) {
44✔
59
            Botan::secure_vector<uint8_t> salt = rng.random_vec(16);
38✔
60

61
            kem_enc_timer->start();
38✔
62
            const auto kem_result = enc.encrypt(rng, 64, salt);
38✔
63
            kem_enc_timer->stop();
38✔
64

65
            kem_dec_timer->start();
38✔
66
            Botan::secure_vector<uint8_t> dec_shared_key = dec.decrypt(kem_result.encapsulated_shared_key(), 64, salt);
38✔
67
            kem_dec_timer->stop();
38✔
68

69
            if(kem_result.shared_key() != dec_shared_key) {
38✔
70
               config.error_output() << "KEM mismatch in PK bench\n";
×
71
            }
72
         }
76✔
73

74
         config.record_result(*kem_enc_timer);
6✔
75
         config.record_result(*kem_dec_timer);
6✔
76
      }
12✔
77
};
78

79
#endif
80

81
#if defined(BOTAN_HAS_KYBER) || defined(BOTAN_HAS_KYBER_90S)
82

83
class PerfTest_Kyber final : public PerfTest_PK_KEM {
1✔
84
   public:
85
      std::string algo() const override { return "Kyber"; }
1✔
86

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

100
BOTAN_REGISTER_PERF_TEST("Kyber", PerfTest_Kyber);
1✔
101

102
#endif
103

104
#if defined(BOTAN_HAS_ML_KEM)
105

106
class PerfTest_ML_KEM final : public PerfTest_PK_KEM {
×
107
   public:
108
      std::string algo() const override { return "ML-KEM"; }
×
109

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

120
BOTAN_REGISTER_PERF_TEST("ML-KEM", PerfTest_ML_KEM);
×
121

122
#endif
123

124
#if defined(BOTAN_HAS_FRODOKEM)
125

126
class PerfTest_FrodoKEM final : public PerfTest_PK_KEM {
×
127
   public:
128
      std::string algo() const override { return "FrodoKEM"; }
×
129

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

149
BOTAN_REGISTER_PERF_TEST("FrodoKEM", PerfTest_FrodoKEM);
×
150

151
#endif
152

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