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

randombit / botan / 10109696569

26 Jul 2024 10:20AM UTC coverage: 91.648% (+0.004%) from 91.644%
10109696569

push

github

web-flow
Merge pull request #4256 from randombit/jack/ec-group-debt

Define some deprecated EC_Group functions in terms of the new APIs

87247 of 95198 relevant lines covered (91.65%)

9303720.63 hits per line

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

91.73
/src/cli/timing_tests.cpp
1
/*
2
* Timing Analysis Tests
3
*
4
* These tests are not for performance, but verifying that two inputs are not handled
5
* in a way that is vulnerable to simple timing attacks.
6
*
7
* Produces output which can be analyzed with the Mona reporting library
8
*
9
* $ git clone https://github.com/seecurity/mona-timing-report.git
10
* $ cd mona-timing-report && ant
11
* $ java -jar ReportingTool.jar --lowerBound=0.4 --upperBound=0.5 --inputFile=$file --name=$file
12
*
13
* (C) 2016 Juraj Somorovsky - juraj.somorovsky@hackmanit.de
14
* (C) 2017 Neverhub
15
* (C) 2017,2018,2019 Jack Lloyd
16
*
17
* Botan is released under the Simplified BSD License (see license.txt)
18
*/
19

20
#include "cli.h"
21

22
#include <botan/hex.h>
23
#include <botan/rng.h>
24
#include <botan/internal/filesystem.h>
25
#include <botan/internal/fmt.h>
26
#include <botan/internal/loadstor.h>
27
#include <botan/internal/os_utils.h>
28
#include <botan/internal/parsing.h>
29
#include <fstream>
30
#include <sstream>
31

32
#if defined(BOTAN_HAS_BIGINT)
33
   #include <botan/bigint.h>
34
#endif
35

36
#if defined(BOTAN_HAS_NUMBERTHEORY)
37
   #include <botan/numthry.h>
38
#endif
39

40
#if defined(BOTAN_HAS_ECC_GROUP)
41
   #include <botan/ec_group.h>
42
#endif
43

44
#if defined(BOTAN_HAS_DL_GROUP)
45
   #include <botan/dl_group.h>
46
#endif
47

48
#if defined(BOTAN_HAS_PUBLIC_KEY_CRYPTO)
49
   #include <botan/pkcs8.h>
50
   #include <botan/pubkey.h>
51
#endif
52

53
#if defined(BOTAN_HAS_RSA)
54
   #include <botan/rsa.h>
55
#endif
56

57
#if defined(BOTAN_HAS_TLS_CBC)
58
   #include <botan/tls_exceptn.h>
59
   #include <botan/internal/tls_cbc.h>
60
#endif
61

62
#if defined(BOTAN_HAS_ECDSA)
63
   #include <botan/ecdsa.h>
64
#endif
65

66
namespace Botan_CLI {
67

68
typedef uint64_t ticks;
69

70
class Timing_Test {
71
   public:
72
      Timing_Test() {
10✔
73
         /*
74
         A constant seed is ok here since the timing test rng just needs to be
75
         "random" but not cryptographically secure - even std::rand() would be ok.
76
         */
77
         const std::string drbg_seed(64, 'A');
10✔
78
         m_rng = cli_make_rng("", drbg_seed);  // throws if it can't find anything to use
10✔
79
      }
10✔
80

81
      virtual ~Timing_Test() = default;
×
82

83
      Timing_Test(const Timing_Test& other) = delete;
84
      Timing_Test(Timing_Test&& other) = delete;
85
      Timing_Test& operator=(const Timing_Test& other) = delete;
86
      Timing_Test& operator=(Timing_Test&& other) = delete;
87

88
      std::vector<std::vector<ticks>> execute_evaluation(const std::vector<std::string>& inputs,
89
                                                         size_t warmup_runs,
90
                                                         size_t measurement_runs);
91

92
      virtual std::vector<uint8_t> prepare_input(const std::string& input) { return Botan::hex_decode(input); }
9✔
93

94
      virtual ticks measure_critical_function(const std::vector<uint8_t>& input) = 0;
95

96
   protected:
97
      static ticks get_ticks() {
1,026✔
98
         // Returns CPU counter or best approximation (monotonic clock of some kind)
99
         //return Botan::OS::get_high_resolution_clock();
100
         return Botan::OS::get_system_timestamp_ns();
1,026✔
101
      }
102

103
      Botan::RandomNumberGenerator& timing_test_rng() { return (*m_rng); }
3✔
104

105
   private:
106
      std::shared_ptr<Botan::RandomNumberGenerator> m_rng;
107
};
108

109
#if defined(BOTAN_HAS_RSA) && defined(BOTAN_HAS_EME_PKCS1) && defined(BOTAN_HAS_EME_RAW)
110

111
class Bleichenbacker_Timing_Test final : public Timing_Test {
×
112
   public:
113
      explicit Bleichenbacker_Timing_Test(size_t keysize) :
1✔
114
            m_privkey(timing_test_rng(), keysize),
1✔
115
            m_pubkey(m_privkey),
1✔
116
            m_enc(m_pubkey, timing_test_rng(), "Raw"),
1✔
117
            m_dec(m_privkey, timing_test_rng(), "PKCS1v15") {}
2✔
118

119
      std::vector<uint8_t> prepare_input(const std::string& input) override {
4✔
120
         const std::vector<uint8_t> input_vector = Botan::hex_decode(input);
4✔
121
         return m_enc.encrypt(input_vector, timing_test_rng());
4✔
122
      }
4✔
123

124
      ticks measure_critical_function(const std::vector<uint8_t>& input) override {
76✔
125
         const ticks start = get_ticks();
76✔
126
         m_dec.decrypt_or_random(input.data(), m_ctext_length, m_expected_content_size, timing_test_rng());
76✔
127
         const ticks end = get_ticks();
76✔
128
         return (end - start);
76✔
129
      }
130

131
   private:
132
      const size_t m_expected_content_size = 48;
133
      const size_t m_ctext_length = 256;
134
      Botan::RSA_PrivateKey m_privkey;
135
      Botan::RSA_PublicKey m_pubkey;
136
      Botan::PK_Encryptor_EME m_enc;
137
      Botan::PK_Decryptor_EME m_dec;
138
};
139

140
#endif
141

142
#if defined(BOTAN_HAS_RSA) && defined(BOTAN_HAS_EME_OAEP) && defined(BOTAN_HAS_EME_RAW)
143

144
/*
145
* Test Manger OAEP side channel
146
*
147
* "A Chosen Ciphertext Attack on RSA Optimal Asymmetric Encryption
148
* Padding (OAEP) as Standardized in PKCS #1 v2.0" James Manger
149
* http://archiv.infsec.ethz.ch/education/fs08/secsem/Manger01.pdf
150
*/
151
class Manger_Timing_Test final : public Timing_Test {
×
152
   public:
153
      explicit Manger_Timing_Test(size_t keysize) :
1✔
154
            m_privkey(timing_test_rng(), keysize),
1✔
155
            m_pubkey(m_privkey),
1✔
156
            m_enc(m_pubkey, timing_test_rng(), m_encrypt_padding),
1✔
157
            m_dec(m_privkey, timing_test_rng(), m_decrypt_padding) {}
2✔
158

159
      std::vector<uint8_t> prepare_input(const std::string& input) override {
2✔
160
         const std::vector<uint8_t> input_vector = Botan::hex_decode(input);
2✔
161
         return m_enc.encrypt(input_vector, timing_test_rng());
2✔
162
      }
2✔
163

164
      ticks measure_critical_function(const std::vector<uint8_t>& input) override {
38✔
165
         ticks start = get_ticks();
38✔
166
         try {
38✔
167
            m_dec.decrypt(input.data(), m_ctext_length);
38✔
168
         } catch(Botan::Decoding_Error&) {}
38✔
169
         ticks end = get_ticks();
38✔
170

171
         return (end - start);
38✔
172
      }
173

174
   private:
175
      const std::string m_encrypt_padding = "Raw";
176
      const std::string m_decrypt_padding = "EME1(SHA-256)";
177
      const size_t m_ctext_length = 256;
178
      Botan::RSA_PrivateKey m_privkey;
179
      Botan::RSA_PublicKey m_pubkey;
180
      Botan::PK_Encryptor_EME m_enc;
181
      Botan::PK_Decryptor_EME m_dec;
182
};
183

184
#endif
185

186
#if defined(BOTAN_HAS_TLS_CBC)
187

188
/*
189
* Test handling of countermeasure to the Lucky13 attack
190
*/
191
class Lucky13_Timing_Test final : public Timing_Test {
×
192
   public:
193
      Lucky13_Timing_Test(const std::string& mac_name, size_t mac_keylen) :
4✔
194
            m_mac_algo(mac_name),
8✔
195
            m_mac_keylen(mac_keylen),
4✔
196
            m_dec(Botan::BlockCipher::create_or_throw("AES-128"),
12✔
197
                  Botan::MessageAuthenticationCode::create_or_throw("HMAC(" + m_mac_algo + ")"),
12✔
198
                  16,
199
                  m_mac_keylen,
4✔
200
                  Botan::TLS::Protocol_Version::TLS_V12,
201
                  false) {}
8✔
202

203
      std::vector<uint8_t> prepare_input(const std::string& input) override;
204
      ticks measure_critical_function(const std::vector<uint8_t>& input) override;
205

206
   private:
207
      const std::string m_mac_algo;
208
      const size_t m_mac_keylen;
209
      Botan::TLS::TLS_CBC_HMAC_AEAD_Decryption m_dec;
210
};
211

212
std::vector<uint8_t> Lucky13_Timing_Test::prepare_input(const std::string& input) {
12✔
213
   const std::vector<uint8_t> input_vector = Botan::hex_decode(input);
12✔
214
   const std::vector<uint8_t> key(16);
12✔
215
   const std::vector<uint8_t> iv(16);
12✔
216

217
   auto enc = Botan::Cipher_Mode::create("AES-128/CBC/NoPadding", Botan::Cipher_Dir::Encryption);
12✔
218
   enc->set_key(key);
12✔
219
   enc->start(iv);
12✔
220
   Botan::secure_vector<uint8_t> buf(input_vector.begin(), input_vector.end());
12✔
221
   enc->finish(buf);
12✔
222

223
   return unlock(buf);
12✔
224
}
60✔
225

226
ticks Lucky13_Timing_Test::measure_critical_function(const std::vector<uint8_t>& input) {
228✔
227
   Botan::secure_vector<uint8_t> data(input.begin(), input.end());
228✔
228
   Botan::secure_vector<uint8_t> aad(13);
228✔
229
   const Botan::secure_vector<uint8_t> iv(16);
228✔
230
   Botan::secure_vector<uint8_t> key(16 + m_mac_keylen);
228✔
231

232
   m_dec.set_key(unlock(key));
228✔
233
   m_dec.set_associated_data(aad);
228✔
234
   m_dec.start(unlock(iv));
228✔
235

236
   ticks start = get_ticks();
228✔
237
   try {
228✔
238
      m_dec.finish(data);
228✔
239
   } catch(Botan::TLS::TLS_Exception&) {}
228✔
240
   ticks end = get_ticks();
228✔
241
   return (end - start);
228✔
242
}
912✔
243

244
#endif
245

246
#if defined(BOTAN_HAS_ECDSA)
247

248
class ECDSA_Timing_Test final : public Timing_Test {
×
249
   public:
250
      explicit ECDSA_Timing_Test(const std::string& ecgroup);
251

252
      ticks measure_critical_function(const std::vector<uint8_t>& input) override;
253

254
   private:
255
      const Botan::EC_Group m_group;
256
      const Botan::ECDSA_PrivateKey m_privkey;
257
      const Botan::EC_Scalar m_x;
258
      Botan::EC_Scalar m_b;
259
      Botan::EC_Scalar m_b_inv;
260
      std::vector<Botan::BigInt> m_ws;
261
};
262

263
ECDSA_Timing_Test::ECDSA_Timing_Test(const std::string& ecgroup) :
1✔
264
      m_group(Botan::EC_Group::from_name(ecgroup)),
1✔
265
      m_privkey(timing_test_rng(), m_group),
1✔
266
      m_x(m_privkey._private_key()),
1✔
267
      m_b(Botan::EC_Scalar::random(m_group, timing_test_rng())),
1✔
268
      m_b_inv(m_b.invert()) {}
2✔
269

270
ticks ECDSA_Timing_Test::measure_critical_function(const std::vector<uint8_t>& input) {
38✔
271
   const auto k = Botan::EC_Scalar::from_bytes_with_trunc(m_group, input);
38✔
272
   // fixed message to minimize noise
273
   const auto m = Botan::EC_Scalar::from_bytes_with_trunc(m_group, std::vector<uint8_t>{5});
38✔
274

275
   ticks start = get_ticks();
38✔
276

277
   // the following ECDSA operations involve and should not leak any information about k
278
   const auto r = Botan::EC_Scalar::gk_x_mod_order(k, timing_test_rng(), m_ws);
38✔
279
   const auto k_inv = k.invert();
38✔
280
   m_b.square_self();
38✔
281
   m_b_inv.square_self();
38✔
282
   const auto xr_m = ((m_x * m_b) * r) + (m * m_b);
38✔
283
   const auto s = (k_inv * xr_m) * m_b_inv;
38✔
284
   BOTAN_UNUSED(r, s);
38✔
285

286
   ticks end = get_ticks();
38✔
287

288
   return (end - start);
38✔
289
}
38✔
290

291
#endif
292

293
#if defined(BOTAN_HAS_ECC_GROUP)
294

295
class ECC_Mul_Timing_Test final : public Timing_Test {
×
296
   public:
297
      explicit ECC_Mul_Timing_Test(std::string_view ecgroup) : m_group(Botan::EC_Group::from_name(ecgroup)) {}
1✔
298

299
      ticks measure_critical_function(const std::vector<uint8_t>& input) override;
300

301
   private:
302
      const Botan::EC_Group m_group;
303
      std::vector<Botan::BigInt> m_ws;
304
};
305

306
ticks ECC_Mul_Timing_Test::measure_critical_function(const std::vector<uint8_t>& input) {
38✔
307
   const auto k = Botan::EC_Scalar::from_bytes_with_trunc(m_group, input);
38✔
308

309
   ticks start = get_ticks();
38✔
310
   const auto kG = Botan::EC_AffinePoint::g_mul(k, timing_test_rng(), m_ws);
38✔
311
   ticks end = get_ticks();
38✔
312

313
   return (end - start);
38✔
314
}
38✔
315

316
#endif
317

318
#if defined(BOTAN_HAS_DL_GROUP)
319

320
class Powmod_Timing_Test final : public Timing_Test {
×
321
   public:
322
      explicit Powmod_Timing_Test(std::string_view dl_group) : m_group(dl_group) {}
1✔
323

324
      ticks measure_critical_function(const std::vector<uint8_t>& input) override;
325

326
   private:
327
      Botan::DL_Group m_group;
328
};
329

330
ticks Powmod_Timing_Test::measure_critical_function(const std::vector<uint8_t>& input) {
57✔
331
   const Botan::BigInt x(input.data(), input.size());
57✔
332
   const size_t max_x_bits = m_group.p_bits();
57✔
333

334
   ticks start = get_ticks();
57✔
335

336
   const Botan::BigInt g_x_p = m_group.power_g_p(x, max_x_bits);
57✔
337

338
   ticks end = get_ticks();
57✔
339

340
   return (end - start);
57✔
341
}
114✔
342

343
#endif
344

345
#if defined(BOTAN_HAS_NUMBERTHEORY)
346

347
class Invmod_Timing_Test final : public Timing_Test {
×
348
   public:
349
      explicit Invmod_Timing_Test(size_t p_bits) { m_p = Botan::random_prime(timing_test_rng(), p_bits); }
2✔
350

351
      ticks measure_critical_function(const std::vector<uint8_t>& input) override;
352

353
   private:
354
      Botan::BigInt m_p;
355
};
356

357
ticks Invmod_Timing_Test::measure_critical_function(const std::vector<uint8_t>& input) {
38✔
358
   const Botan::BigInt k(input.data(), input.size());
38✔
359

360
   ticks start = get_ticks();
38✔
361

362
   const Botan::BigInt inv = inverse_mod(k, m_p);
38✔
363

364
   ticks end = get_ticks();
38✔
365

366
   return (end - start);
38✔
367
}
76✔
368

369
#endif
370

371
std::vector<std::vector<ticks>> Timing_Test::execute_evaluation(const std::vector<std::string>& raw_inputs,
10✔
372
                                                                size_t warmup_runs,
373
                                                                size_t measurement_runs) {
374
   std::vector<std::vector<ticks>> all_results(raw_inputs.size());
10✔
375
   std::vector<std::vector<uint8_t>> inputs(raw_inputs.size());
10✔
376

377
   for(auto& result : all_results) {
37✔
378
      result.reserve(measurement_runs);
27✔
379
   }
380

381
   for(size_t i = 0; i != inputs.size(); ++i) {
37✔
382
      inputs[i] = prepare_input(raw_inputs[i]);
27✔
383
   }
384

385
   // arbitrary upper bounds of 1 and 10 million resp
386
   if(warmup_runs > 1000000 || measurement_runs > 100000000) {
10✔
387
      throw CLI_Error("Requested execution counts too large, rejecting");
×
388
   }
389

390
   size_t total_runs = 0;
10✔
391
   std::vector<ticks> results(inputs.size());
10✔
392

393
   while(total_runs < (warmup_runs + measurement_runs)) {
200✔
394
      for(size_t i = 0; i != inputs.size(); ++i) {
703✔
395
         results[i] = measure_critical_function(inputs[i]);
513✔
396
      }
397

398
      total_runs++;
190✔
399

400
      if(total_runs > warmup_runs) {
190✔
401
         for(size_t i = 0; i != results.size(); ++i) {
592✔
402
            all_results[i].push_back(results[i]);
432✔
403
         }
404
      }
405
   }
406

407
   return all_results;
10✔
408
}
10✔
409

410
class Timing_Test_Command final : public Command {
411
   public:
412
      Timing_Test_Command() :
11✔
413
            Command(
414
               "timing_test test_type --test-data-file= --test-data-dir=src/tests/data/timing "
415
               "--warmup-runs=5000 --measurement-runs=50000") {}
22✔
416

417
      std::string group() const override { return "testing"; }
1✔
418

419
      std::string description() const override { return "Run various timing side channel tests"; }
1✔
420

421
      void go() override {
10✔
422
         const std::string test_type = get_arg("test_type");
10✔
423
         const size_t warmup_runs = get_arg_sz("warmup-runs");
10✔
424
         const size_t measurement_runs = get_arg_sz("measurement-runs");
10✔
425

426
         std::unique_ptr<Timing_Test> test = lookup_timing_test(test_type);
10✔
427

428
         if(!test) {
10✔
429
            throw CLI_Error("Unknown or unavailable test type '" + test_type + "'");
×
430
         }
431

432
         std::string filename = get_arg_or("test-data-file", "");
20✔
433

434
         if(filename.empty()) {
10✔
435
            const std::string test_data_dir = get_arg("test-data-dir");
10✔
436
            filename = test_data_dir + "/" + test_type + ".vec";
20✔
437
         }
10✔
438

439
         std::vector<std::string> lines = read_testdata(filename);
10✔
440

441
         std::vector<std::vector<ticks>> results = test->execute_evaluation(lines, warmup_runs, measurement_runs);
10✔
442

443
         size_t unique_id = 0;
10✔
444
         std::ostringstream oss;
10✔
445
         for(size_t secret_id = 0; secret_id != results.size(); ++secret_id) {
37✔
446
            for(size_t i = 0; i != results[secret_id].size(); ++i) {
459✔
447
               oss << unique_id++ << ";" << secret_id << ";" << results[secret_id][i] << "\n";
432✔
448
            }
449
         }
450

451
         output() << oss.str();
10✔
452
      }
20✔
453

454
   private:
455
      static std::vector<std::string> read_testdata(const std::string& filename) {
10✔
456
         std::vector<std::string> lines;
10✔
457
         std::ifstream infile(filename);
10✔
458
         if(infile.good() == false) {
10✔
459
            throw CLI_Error("Error reading test data from '" + filename + "'");
×
460
         }
461
         std::string line;
10✔
462
         while(std::getline(infile, line)) {
70✔
463
            if(!line.empty() && line.at(0) != '#') {
60✔
464
               lines.push_back(line);
27✔
465
            }
466
         }
467
         return lines;
20✔
468
      }
10✔
469

470
      static std::unique_ptr<Timing_Test> lookup_timing_test(std::string_view test_type);
471

472
      std::string help_text() const override {
×
473
         // TODO check feature macros
474
         return (Command::help_text() +
×
475
                 "\ntest_type can take on values "
476
                 "bleichenbacher "
477
                 "manger "
478
                 "ecdsa "
479
                 "ecc_mul "
480
                 "inverse_mod "
481
                 "pow_mod "
482
                 "lucky13sec3 "
483
                 "lucky13sec4sha1 "
484
                 "lucky13sec4sha256 "
485
                 "lucky13sec4sha384 ");
×
486
      }
487
};
488

489
std::unique_ptr<Timing_Test> Timing_Test_Command::lookup_timing_test(std::string_view test_type) {
10✔
490
#if defined(BOTAN_HAS_RSA) && defined(BOTAN_HAS_EME_PKCS1) && defined(BOTAN_HAS_EME_RAW)
491
   if(test_type == "bleichenbacher") {
10✔
492
      return std::make_unique<Bleichenbacker_Timing_Test>(2048);
1✔
493
   }
494
#endif
495

496
#if defined(BOTAN_HAS_RSA) && defined(BOTAN_HAS_EME_OAEP) && defined(BOTAN_HAS_EME_RAW)
497
   if(test_type == "manger") {
9✔
498
      return std::make_unique<Manger_Timing_Test>(2048);
1✔
499
   }
500
#endif
501

502
#if defined(BOTAN_HAS_ECDSA)
503
   if(test_type == "ecdsa") {
8✔
504
      return std::make_unique<ECDSA_Timing_Test>("secp384r1");
1✔
505
   }
506
#endif
507

508
#if defined(BOTAN_HAS_ECC_GROUP)
509
   if(test_type == "ecc_mul") {
7✔
510
      return std::make_unique<ECC_Mul_Timing_Test>("brainpool512r1");
1✔
511
   }
512
#endif
513

514
#if defined(BOTAN_HAS_NUMBERTHEORY)
515
   if(test_type == "inverse_mod") {
6✔
516
      return std::make_unique<Invmod_Timing_Test>(512);
1✔
517
   }
518
#endif
519

520
#if defined(BOTAN_HAS_DL_GROUP)
521
   if(test_type == "pow_mod") {
5✔
522
      return std::make_unique<Powmod_Timing_Test>("modp/ietf/1024");
1✔
523
   }
524
#endif
525

526
#if defined(BOTAN_HAS_TLS_CBC)
527
   if(test_type == "lucky13sec3" || test_type == "lucky13sec4sha1") {
6✔
528
      return std::make_unique<Lucky13_Timing_Test>("SHA-1", 20);
2✔
529
   }
530
   if(test_type == "lucky13sec4sha256") {
2✔
531
      return std::make_unique<Lucky13_Timing_Test>("SHA-256", 32);
1✔
532
   }
533
   if(test_type == "lucky13sec4sha384") {
1✔
534
      return std::make_unique<Lucky13_Timing_Test>("SHA-384", 48);
1✔
535
   }
536
#endif
537

538
   BOTAN_UNUSED(test_type);
×
539

540
   return nullptr;
×
541
}
542

543
BOTAN_REGISTER_COMMAND("timing_test", Timing_Test_Command);
11✔
544

545
#if defined(BOTAN_HAS_RSA) && defined(BOTAN_HAS_EME_PKCS1)
546

547
class MARVIN_Test_Command final : public Command {
548
   public:
549
      MARVIN_Test_Command() : Command("marvin_test key_file ctext_dir --runs=10 --output-nsec --expect-pt-len=0") {}
4✔
550

551
      std::string group() const override { return "testing"; }
1✔
552

553
      std::string description() const override { return "Run a test for MARVIN attack"; }
1✔
554

555
      void go() override {
1✔
556
         const std::string key_file = get_arg("key_file");
1✔
557
         const std::string ctext_dir = get_arg("ctext_dir");
1✔
558
         const size_t measurement_runs = get_arg_sz("runs");
1✔
559
         const size_t expect_pt_len = get_arg_sz("expect-pt-len");
1✔
560
         const bool output_nsec = flag_set("output-nsec");
1✔
561

562
         Botan::DataSource_Stream key_src(key_file);
1✔
563
         const auto key = Botan::PKCS8::load_key(key_src);
1✔
564

565
         if(key->algo_name() != "RSA") {
1✔
566
            throw CLI_Usage_Error("Unexpected key type for MARVIN test");
×
567
         }
568

569
         const size_t modulus_bytes = (key->key_length() + 7) / 8;
1✔
570

571
         std::vector<std::string> names;
1✔
572
         std::vector<uint8_t> ciphertext_data;
1✔
573

574
         for(const auto& filename : Botan::get_files_recursive(ctext_dir)) {
5✔
575
            const auto contents = this->slurp_file(filename);
4✔
576

577
            if(contents.size() != modulus_bytes) {
4✔
578
               throw CLI_Usage_Error(
×
579
                  Botan::fmt("The ciphertext file {} had different size ({}) than the RSA modulus ({})",
×
580
                             filename,
581
                             contents.size(),
×
582
                             modulus_bytes));
×
583
            }
584

585
            const auto parts = Botan::split_on(filename, '/');
4✔
586

587
            names.push_back(parts[parts.size() - 1]);
4✔
588
            ciphertext_data.insert(ciphertext_data.end(), contents.begin(), contents.end());
4✔
589
         }
9✔
590

591
         if(names.empty()) {
1✔
592
            throw CLI_Usage_Error("Empty ciphertext directory for MARVIN test");
×
593
         }
594

595
         Botan::PK_Decryptor_EME op(*key, rng(), "PKCS1v15");
1✔
596

597
         std::vector<size_t> indexes;
1✔
598
         for(size_t i = 0; i != names.size(); ++i) {
5✔
599
            indexes.push_back(i);
4✔
600
         }
601

602
         std::vector<std::vector<uint64_t>> measurements(names.size());
1✔
603
         for(auto& m : measurements) {
5✔
604
            m.reserve(measurement_runs);
4✔
605
         }
606

607
         for(size_t r = 0; r != measurement_runs; ++r) {
33✔
608
            shuffle(indexes, rng());
32✔
609

610
            std::vector<uint8_t> ciphertext(modulus_bytes);
32✔
611
            for(size_t i = 0; i != indexes.size(); ++i) {
160✔
612
               const size_t testcase = indexes[i];
128✔
613

614
               // FIXME should this load be constant time?
615
               Botan::copy_mem(&ciphertext[0], &ciphertext_data[testcase * modulus_bytes], modulus_bytes);
128✔
616

617
               const uint64_t start = Botan::OS::get_system_timestamp_ns();
128✔
618

619
               op.decrypt_or_random(ciphertext.data(), modulus_bytes, expect_pt_len, rng());
128✔
620

621
               const uint64_t duration = Botan::OS::get_system_timestamp_ns() - start;
128✔
622
               BOTAN_ASSERT_NOMSG(measurements[testcase].size() == r);
128✔
623
               measurements[testcase].push_back(duration);
128✔
624
            }
625
         }
32✔
626

627
         for(size_t t = 0; t != names.size(); ++t) {
5✔
628
            if(t > 0) {
4✔
629
               output() << ",";
3✔
630
            }
631
            output() << names[t];
4✔
632
         }
633
         output() << "\n";
1✔
634

635
         for(size_t r = 0; r != measurement_runs; ++r) {
33✔
636
            for(size_t t = 0; t != names.size(); ++t) {
160✔
637
               if(t > 0) {
128✔
638
                  output() << ",";
96✔
639
               }
640

641
               const uint64_t dur_nsec = measurements[t][r];
128✔
642
               if(output_nsec) {
128✔
643
                  output() << dur_nsec;
×
644
               } else {
645
                  const double dur_s = static_cast<double>(dur_nsec) / 1000000000.0;
128✔
646
                  output() << dur_s;
128✔
647
               }
648
            }
649
            output() << "\n";
32✔
650
         }
651
      }
3✔
652

653
      template <typename T>
654
      void shuffle(std::vector<T>& vec, Botan::RandomNumberGenerator& rng) {
32✔
655
         const size_t n = vec.size();
32✔
656
         for(size_t i = 0; i != n; ++i) {
160✔
657
            uint8_t jb[sizeof(uint64_t)];
658
            rng.randomize(jb, sizeof(jb));
128✔
659
            uint64_t j8 = Botan::load_le<uint64_t>(jb, 0);
128✔
660
            size_t j = i + static_cast<size_t>(j8) % (n - i);
128✔
661
            std::swap(vec[i], vec[j]);
128✔
662
         }
663
      }
32✔
664
};
665

666
BOTAN_REGISTER_COMMAND("marvin_test", MARVIN_Test_Command);
2✔
667

668
#endif
669

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