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

randombit / botan / 44860445550

20 Nov 2026 12:17PM UTC coverage: 92.497% (+0.007%) from 92.49%
44860445550

push

github

web-flow
Merge pull request #6145 from randombit/jack/fuzzer-work

Fix various bugs and issues in the fuzzers

145799 of 157625 relevant lines covered (92.5%)

8640547.93 hits per line

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

48.39
/src/cli/perf_misc.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
#include <algorithm>
9
#include <cstring>
10

11
// Always available:
12
#include <botan/assert.h>
13
#include <botan/hex.h>
14
#include <botan/rng.h>
15
#include <botan/internal/fmt.h>
16

17
#if defined(BOTAN_HAS_BASE32_CODEC)
18
   #include <botan/base32.h>
19
#endif
20

21
#if defined(BOTAN_HAS_BASE58_CODEC)
22
   #include <botan/base58.h>
23
#endif
24

25
#if defined(BOTAN_HAS_BASE64_CODEC)
26
   #include <botan/base64.h>
27
#endif
28

29
#if defined(BOTAN_HAS_FPE_FE1)
30
   #include <botan/fpe_fe1.h>
31
#endif
32

33
#if defined(BOTAN_HAS_RFC3394_KEYWRAP)
34
   #include <botan/rfc3394.h>
35
#endif
36

37
#if defined(BOTAN_HAS_ZFEC)
38
   #include <botan/zfec.h>
39
#endif
40

41
namespace Botan_CLI {
42

43
namespace {
44

45
class PerfTest_Hex final : public PerfTest {
×
46
   public:
47
      void go(const PerfConfig& config) override {
×
48
         for(const size_t buf_size : config.buffer_sizes()) {
×
49
            std::vector<uint8_t> ibuf(buf_size);
×
50
            std::vector<uint8_t> rbuf(buf_size);
×
51
            const size_t olen = 2 * buf_size;
×
52

53
            auto enc_timer = config.make_timer("hex", ibuf.size(), "encode", "", ibuf.size());
×
54

55
            auto dec_timer = config.make_timer("hex", olen, "decode", "", olen);
×
56

57
            const auto msec = config.runtime();
×
58

59
            while(enc_timer->under(msec) && dec_timer->under(msec)) {
×
60
               config.rng().randomize(ibuf);
×
61

62
               std::string hex = enc_timer->run([&]() { return Botan::hex_encode(ibuf); });
×
63

64
               dec_timer->run([&]() { Botan::hex_decode(rbuf.data(), hex); });
×
65
               BOTAN_ASSERT(rbuf == ibuf, "Encode/decode round trip ok");
×
66
            }
×
67

68
            config.record_result(*enc_timer);
×
69
            config.record_result(*dec_timer);
×
70
         }
×
71
      }
×
72
};
73

74
BOTAN_REGISTER_PERF_TEST("hex", PerfTest_Hex);
×
75

76
#if defined(BOTAN_HAS_BASE32_CODEC)
77
class PerfTest_Base32 final : public PerfTest {
×
78
   public:
79
      void go(const PerfConfig& config) override {
×
80
         for(const size_t buf_size : config.buffer_sizes()) {
×
81
            std::vector<uint8_t> ibuf(buf_size);
×
82
            const size_t olen = Botan::base32_encode_max_output(ibuf.size());
×
83
            std::vector<uint8_t> rbuf(Botan::base32_decode_max_output(olen));
×
84

85
            auto enc_timer = config.make_timer("base32", ibuf.size(), "encode", "", ibuf.size());
×
86

87
            auto dec_timer = config.make_timer("base32", olen, "decode", "", olen);
×
88

89
            const auto msec = config.runtime();
×
90

91
            while(enc_timer->under(msec) && dec_timer->under(msec)) {
×
92
               config.rng().randomize(ibuf);
×
93

94
               std::string b32 = enc_timer->run([&]() { return Botan::base32_encode(ibuf); });
×
95

96
               const size_t rlen = dec_timer->run([&]() { return Botan::base32_decode(rbuf.data(), b32); });
×
97
               BOTAN_ASSERT(rlen == ibuf.size() && std::equal(ibuf.begin(), ibuf.end(), rbuf.begin()),
×
98
                            "Encode/decode round trip ok");
99
            }
×
100

101
            config.record_result(*enc_timer);
×
102
            config.record_result(*dec_timer);
×
103
         }
×
104
      }
×
105
};
106

107
BOTAN_REGISTER_PERF_TEST("base32", PerfTest_Base32);
×
108

109
#endif
110

111
#if defined(BOTAN_HAS_BASE64_CODEC)
112
class PerfTest_Base64 final : public PerfTest {
×
113
   public:
114
      void go(const PerfConfig& config) override {
×
115
         for(const size_t buf_size : config.buffer_sizes()) {
×
116
            std::vector<uint8_t> ibuf(buf_size);
×
117
            const size_t olen = Botan::base64_encode_max_output(ibuf.size());
×
118
            std::vector<uint8_t> rbuf(Botan::base64_decode_max_output(olen));
×
119

120
            auto enc_timer = config.make_timer("base64", ibuf.size(), "encode", "", ibuf.size());
×
121

122
            auto dec_timer = config.make_timer("base64", olen, "decode", "", olen);
×
123

124
            const auto msec = config.runtime();
×
125

126
            config.rng().randomize(ibuf);
×
127

128
            // Check the roundtrip once, outside of the timed loop
129
            const size_t rlen = Botan::base64_decode(rbuf.data(), Botan::base64_encode(ibuf));
×
130
            BOTAN_ASSERT(rlen == ibuf.size() && std::equal(ibuf.begin(), ibuf.end(), rbuf.begin()),
×
131
                         "Encode/decode round trip ok");
132

133
            while(enc_timer->under(msec) && dec_timer->under(msec)) {
×
134
               const std::string b64 = enc_timer->run([&]() { return Botan::base64_encode(ibuf); });
×
135

136
               dec_timer->run([&]() { Botan::base64_decode(rbuf.data(), b64); });
×
137
            }
×
138

139
            config.record_result(*enc_timer);
×
140
            config.record_result(*dec_timer);
×
141
         }
×
142
      }
×
143
};
144

145
BOTAN_REGISTER_PERF_TEST("base64", PerfTest_Base64);
×
146

147
#endif
148

149
#if defined(BOTAN_HAS_BASE58_CODEC)
150
class PerfTest_Base58 final : public PerfTest {
×
151
   public:
152
      void go(const PerfConfig& config) override {
×
153
         for(const size_t buf_size : config.buffer_sizes()) {
×
154
            std::vector<uint8_t> ibuf(buf_size);
×
155

156
            auto enc_timer = config.make_timer("base58", ibuf.size(), "encode", "", ibuf.size());
×
157
            auto dec_timer = config.make_timer("base58", ibuf.size(), "decode", "", ibuf.size());
×
158

159
            const auto msec = config.runtime();
×
160

161
            while(enc_timer->under(msec) && dec_timer->under(msec)) {
×
162
               config.rng().randomize(ibuf);
×
163

164
               const std::string b58 = enc_timer->run([&]() { return Botan::base58_encode(ibuf); });
×
165
               const auto rbuf = dec_timer->run([&] { return Botan::base58_decode(b58); });
×
166
               BOTAN_ASSERT(rbuf == ibuf, "Encode/decode round trip ok");
×
167
            }
×
168

169
            config.record_result(*enc_timer);
×
170
            config.record_result(*dec_timer);
×
171
         }
×
172
      }
×
173
};
174

175
BOTAN_REGISTER_PERF_TEST("base58", PerfTest_Base58);
×
176

177
#endif
178

179
#if defined(BOTAN_HAS_FPE_FE1)
180

181
class PerfTest_FpeFe1 final : public PerfTest {
1 ✔
182
   public:
183
      void go(const PerfConfig& config) override {
1 ✔
184
         const auto n = Botan::BigInt::from_u64(1000000000000000);
1 ✔
185

186
         auto enc_timer = config.make_timer("FPE_FE1 encrypt");
2 ✔
187
         auto dec_timer = config.make_timer("FPE_FE1 decrypt");
2 ✔
188

189
         const Botan::SymmetricKey key(config.rng(), 32);
1 ✔
190
         const std::vector<uint8_t> tweak(8);  // 8 zeros
1 ✔
191

192
         auto x = Botan::BigInt::one();
1 ✔
193

194
         Botan::FPE_FE1 fpe_fe1(n);
1 ✔
195
         fpe_fe1.set_key(key);
1 ✔
196

197
         const auto runtime = config.runtime();
1 ✔
198

199
         while(enc_timer->under(runtime)) {
10 ✔
200
            enc_timer->start();
9 ✔
201
            x = fpe_fe1.encrypt(x, tweak.data(), tweak.size());
9 ✔
202
            enc_timer->stop();
9 ✔
203
         }
204
         config.record_result(*enc_timer);
1 ✔
205

206
         for(size_t i = 0; i != enc_timer->events(); ++i) {
10 ✔
207
            dec_timer->start();
9 ✔
208
            x = fpe_fe1.decrypt(x, tweak.data(), tweak.size());
9 ✔
209
            dec_timer->stop();
9 ✔
210
         }
211
         config.record_result(*dec_timer);
1 ✔
212

213
         BOTAN_ASSERT(x == 1, "FPE works");
1 ✔
214
      }
4 ✔
215
};
216

217
BOTAN_REGISTER_PERF_TEST("fpe_fe1", PerfTest_FpeFe1);
1 ✔
218

219
#endif
220

221
#if defined(BOTAN_HAS_RFC3394_KEYWRAP)
222
class PerfTest_Rfc3394 final : public PerfTest {
1 ✔
223
      void go(const PerfConfig& config) override {
1 ✔
224
         auto wrap_timer = config.make_timer("RFC3394 AES-256 key wrap");
2 ✔
225
         auto unwrap_timer = config.make_timer("RFC3394 AES-256 key unwrap");
2 ✔
226

227
         const Botan::SymmetricKey kek(config.rng(), 32);
1 ✔
228
         Botan::secure_vector<uint8_t> key(64, 0);
1 ✔
229

230
         const auto runtime = config.runtime();
1 ✔
231

232
         while(wrap_timer->under(runtime)) {
27 ✔
233
            wrap_timer->start();
26 ✔
234
            key = Botan::rfc3394_keywrap(key, kek);
52 ✔
235
            wrap_timer->stop();
26 ✔
236

237
            unwrap_timer->start();
26 ✔
238
            key = Botan::rfc3394_keyunwrap(key, kek);
52 ✔
239
            unwrap_timer->stop();
26 ✔
240

241
            key[0] += 1;
26 ✔
242
         }
243

244
         config.record_result(*wrap_timer);
1 ✔
245
         config.record_result(*unwrap_timer);
2 ✔
246
      }
3 ✔
247
};
248

249
BOTAN_REGISTER_PERF_TEST("rfc3394", PerfTest_Rfc3394);
1 ✔
250

251
#endif
252

253
#if defined(BOTAN_HAS_ZFEC)
254

255
class PerfTest_Zfec final : public PerfTest {
1 ✔
256
   public:
257
      void go(const PerfConfig& config) override {
1 ✔
258
         const size_t k = 4;
1 ✔
259
         const size_t n = 16;
1 ✔
260

261
         Botan::ZFEC zfec(k, n);
1 ✔
262

263
         const size_t share_size = 256 * 1024;
1 ✔
264

265
         std::vector<uint8_t> input(share_size * k);
1 ✔
266
         config.rng().randomize(input.data(), input.size());
1 ✔
267

268
         std::vector<uint8_t> output(share_size * n);
1 ✔
269

270
         auto enc_fn = [&](size_t share, const uint8_t buf[], size_t len) {
17 ✔
271
            std::memcpy(&output[share * share_size], buf, len);
16 ✔
272
         };
17 ✔
273

274
         const auto msec = config.runtime();
1 ✔
275

276
         const std::string alg = Botan::fmt("zfec {}/{}", k, n);
1 ✔
277

278
         auto enc_timer = config.make_timer(alg, input.size(), "encode", "", input.size());
1 ✔
279

280
         enc_timer->run_until_elapsed(msec, [&]() { zfec.encode(input.data(), input.size(), enc_fn); });
3 ✔
281

282
         config.record_result(*enc_timer);
1 ✔
283

284
         auto dec_timer = config.make_timer(alg, input.size(), "decode", "", input.size());
1 ✔
285

286
         std::map<size_t, const uint8_t*> shares;
1 ✔
287
         for(size_t i = 0; i != n; ++i) {
17 ✔
288
            shares[i] = &output[share_size * i];
16 ✔
289
         }
290

291
         // remove data shares to make decoding maximally expensive:
292
         while(shares.size() != k) {
13 ✔
293
            shares.erase(shares.begin());
12 ✔
294
         }
295

296
         std::vector<uint8_t> recovered(share_size * k);
1 ✔
297

298
         auto dec_fn = [&](size_t share, const uint8_t buf[], size_t len) {
13 ✔
299
            std::memcpy(&recovered[share * share_size], buf, len);
12 ✔
300
         };
13 ✔
301

302
         dec_timer->run_until_elapsed(msec, [&]() { zfec.decode_shares(shares, share_size, dec_fn); });
7 ✔
303

304
         config.record_result(*dec_timer);
1 ✔
305

306
         if(recovered != input) {
1 ✔
307
            config.error_output() << "ZFEC recovery failed\n";
×
308
         }
309
      }
4 ✔
310
};
311

312
BOTAN_REGISTER_PERF_TEST("zfec", PerfTest_Zfec);
1 ✔
313

314
#endif
315

316
}  // namespace
317

318
}  // namespace Botan_CLI
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc