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

randombit / botan / 20160266155

12 Dec 2025 08:01AM UTC coverage: 90.357% (-0.001%) from 90.358%
20160266155

push

github

web-flow
Merge pull request #5172 from randombit/jack/fix-clang-tidy-misc-const-correctness

Fix and enable clang-tidy warning misc-const-correctness

100951 of 111725 relevant lines covered (90.36%)

12817908.54 hits per line

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

84.39
/src/tests/test_rng_behavior.cpp
1
/*
2
* (C) 2014,2015,2017 Jack Lloyd
3
* (C) 2016 René Korthaus, Rohde & Schwarz Cybersecurity
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include "test_rng.h"
9
#include "tests.h"
10

11
#include <botan/internal/target_info.h>
12

13
#if defined(BOTAN_HAS_STATEFUL_RNG)
14
   #include <botan/stateful_rng.h>
15
#endif
16

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

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

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

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

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

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

41
#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
42
   #include <sys/wait.h>
43
   #include <unistd.h>
44
#endif
45

46
namespace Botan_Tests {
47

48
namespace {
49

50
#if defined(BOTAN_HAS_STATEFUL_RNG)
51

52
class Stateful_RNG_Tests : public Test {
2✔
53
   public:
54
      std::vector<Test::Result> run() override {
2✔
55
         std::vector<Test::Result> results;
2✔
56
         results.push_back(test_reseed_kat());
4✔
57
         results.push_back(test_reseed());
4✔
58
         results.push_back(test_reseed_interval_limits());
4✔
59
         results.push_back(test_max_number_of_bytes_per_request());
4✔
60
         results.push_back(test_broken_entropy_input());
4✔
61
         results.push_back(test_check_nonce());
4✔
62
         results.push_back(test_prediction_resistance());
4✔
63
         results.push_back(test_randomize_with_ts_input());
4✔
64
         results.push_back(test_security_level());
4✔
65
         results.push_back(test_input_output_edge_cases());
4✔
66

67
         /*
68
         * This test uses the library in both parent and child processes. But
69
         * this causes a race with other threads, where if any other test thread
70
         * is holding the mlock pool mutex, it is killed after the fork. Then,
71
         * in the child, any attempt to allocate or free memory will cause a
72
         * deadlock.
73
         */
74
         if(Test::options().test_threads() == 1) {
2✔
75
            results.push_back(test_fork_safety());
×
76
         }
77

78
         return results;
2✔
79
      }
×
80

81
   protected:
82
      virtual std::string rng_name() const = 0;
83

84
      virtual std::unique_ptr<Botan::Stateful_RNG> create_rng(Botan::RandomNumberGenerator* underlying_rng,
85
                                                              Botan::Entropy_Sources* underlying_es,
86
                                                              size_t reseed_interval) = 0;
87

88
      std::unique_ptr<Botan::Stateful_RNG> make_rng(Botan::RandomNumberGenerator& underlying_rng,
14✔
89
                                                    size_t reseed_interval = 1024) {
90
         return create_rng(&underlying_rng, nullptr, reseed_interval);
14✔
91
      }
92

93
      std::unique_ptr<Botan::Stateful_RNG> make_rng(Botan::Entropy_Sources& underlying_srcs,
4✔
94
                                                    size_t reseed_interval = 1024) {
95
         return create_rng(nullptr, &underlying_srcs, reseed_interval);
4✔
96
      }
97

98
      std::unique_ptr<Botan::Stateful_RNG> make_rng(Botan::RandomNumberGenerator& underlying_rng,
6✔
99
                                                    Botan::Entropy_Sources& underlying_srcs,
100
                                                    size_t reseed_interval = 1024) {
101
         return create_rng(&underlying_rng, &underlying_srcs, reseed_interval);
6✔
102
      }
103

104
      virtual Test::Result test_reseed_kat() = 0;
105

106
      virtual Test::Result test_security_level() = 0;
107

108
      virtual Test::Result test_max_number_of_bytes_per_request() = 0;
109

110
      virtual Test::Result test_reseed_interval_limits() = 0;
111

112
   private:
113
      Test::Result test_reseed() {
2✔
114
         Test::Result result(rng_name() + " Reseed");
4✔
115

116
         // test reseed_interval is enforced
117
         Request_Counting_RNG counting_rng;
2✔
118

119
         auto rng = make_rng(counting_rng, 2);
2✔
120

121
         rng->random_vec(7);
2✔
122
         result.test_eq("initial seeding", counting_rng.randomize_count(), 1);
2✔
123
         rng->random_vec(9);
2✔
124
         result.test_eq("still initial seed", counting_rng.randomize_count(), 1);
2✔
125

126
         rng->random_vec(1);
2✔
127
         result.test_eq("first reseed", counting_rng.randomize_count(), 2);
2✔
128
         rng->random_vec(15);
2✔
129
         result.test_eq("still first reseed", counting_rng.randomize_count(), 2);
2✔
130

131
         rng->random_vec(15);
2✔
132
         result.test_eq("second reseed", counting_rng.randomize_count(), 3);
2✔
133
         rng->random_vec(1);
2✔
134
         result.test_eq("still second reseed", counting_rng.randomize_count(), 3);
2✔
135

136
         if(rng->max_number_of_bytes_per_request() > 0) {
2✔
137
            // request > max_number_of_bytes_per_request, do reseeds occur?
138
            rng->random_vec(64 * 1024 + 1);
1✔
139
            result.test_eq("request exceeds output limit", counting_rng.randomize_count(), 4);
1✔
140

141
            rng->random_vec(9 * 64 * 1024 + 1);
1✔
142
            result.test_eq("request exceeds output limit", counting_rng.randomize_count(), 9);
2✔
143
         }
144

145
         return result;
4✔
146
      }
2✔
147

148
      Test::Result test_broken_entropy_input() {
2✔
149
         Test::Result result(rng_name() + " Broken Entropy Input");
4✔
150

151
   #if defined(BOTAN_HAS_ENTROPY_SOURCE)
152
         class Broken_Entropy_Source final : public Botan::Entropy_Source {
2✔
153
            public:
154
               std::string name() const override { return "Broken Entropy Source"; }
×
155

156
               size_t poll(Botan::RandomNumberGenerator& /*rng*/) override {
4✔
157
                  throw Botan::Not_Implemented("polling not available");
4✔
158
               }
159
         };
160

161
         class Insufficient_Entropy_Source final : public Botan::Entropy_Source {
2✔
162
            public:
163
               std::string name() const override { return "Insufficient Entropy Source"; }
×
164

165
               size_t poll(Botan::RandomNumberGenerator& /*rng*/) override { return 0; }
2✔
166
         };
167
   #endif
168

169
         // make sure no output is generated when the entropy input source is broken
170

171
         // underlying_rng throws exception
172
         Botan::Null_RNG broken_entropy_input_rng;
2✔
173
         result.test_eq("Null_RNG not seeded", broken_entropy_input_rng.is_seeded(), false);
2✔
174
         auto rng_with_broken_rng = make_rng(broken_entropy_input_rng);
2✔
175

176
         result.test_throws("broken underlying rng", [&rng_with_broken_rng]() { rng_with_broken_rng->random_vec(16); });
6✔
177

178
   #if defined(BOTAN_HAS_ENTROPY_SOURCE)
179

180
         // entropy_sources throw exception
181
         auto broken_entropy_source_1 = std::make_unique<Broken_Entropy_Source>();
2✔
182
         auto broken_entropy_source_2 = std::make_unique<Broken_Entropy_Source>();
2✔
183

184
         Botan::Entropy_Sources broken_entropy_sources;
2✔
185
         broken_entropy_sources.add_source(std::move(broken_entropy_source_1));
2✔
186
         broken_entropy_sources.add_source(std::move(broken_entropy_source_2));
2✔
187

188
         auto rng_with_broken_es = make_rng(broken_entropy_sources);
2✔
189
         result.test_throws("broken entropy sources", [&rng_with_broken_es]() { rng_with_broken_es->random_vec(16); });
6✔
190

191
         // entropy source returns insufficient entropy
192
         Botan::Entropy_Sources insufficient_entropy_sources;
2✔
193
         auto insufficient_entropy_source = std::make_unique<Insufficient_Entropy_Source>();
2✔
194
         insufficient_entropy_sources.add_source(std::move(insufficient_entropy_source));
2✔
195

196
         auto rng_with_insufficient_es = make_rng(insufficient_entropy_sources);
2✔
197
         result.test_throws("insufficient entropy source",
4✔
198
                            [&rng_with_insufficient_es]() { rng_with_insufficient_es->random_vec(16); });
4✔
199

200
         // one of or both underlying_rng and entropy_sources throw exception
201

202
         auto rng_with_broken_rng_and_good_es =
2✔
203
            make_rng(broken_entropy_input_rng, Botan::Entropy_Sources::global_sources());
2✔
204

205
         result.test_throws("broken underlying rng but good entropy sources",
4✔
206
                            [&rng_with_broken_rng_and_good_es]() { rng_with_broken_rng_and_good_es->random_vec(16); });
4✔
207

208
         auto rng_with_good_rng_and_broken_es = make_rng(this->rng(), broken_entropy_sources);
2✔
209

210
         result.test_throws("good underlying rng but broken entropy sources",
4✔
211
                            [&rng_with_good_rng_and_broken_es]() { rng_with_good_rng_and_broken_es->random_vec(16); });
4✔
212

213
         auto rng_with_broken_rng_and_broken_es = make_rng(broken_entropy_input_rng, broken_entropy_sources);
2✔
214

215
         result.test_throws("underlying rng and entropy sources broken", [&rng_with_broken_rng_and_broken_es]() {
4✔
216
            rng_with_broken_rng_and_broken_es->random_vec(16);
2✔
217
         });
×
218
   #endif
219

220
         return result;
4✔
221
      }
12✔
222

223
      Test::Result test_check_nonce() {
2✔
224
         Test::Result result(rng_name() + " Nonce Check");
4✔
225

226
         // make sure the nonce has at least security_strength bits
227
         auto rng = create_rng(nullptr, nullptr, 0);
2✔
228

229
         for(const size_t nonce_size : {0, 4, 8, 16, 31, 32, 34, 64}) {
18✔
230
            rng->clear();
16✔
231
            result.test_eq("not seeded", rng->is_seeded(), false);
16✔
232

233
            const std::vector<uint8_t> nonce(nonce_size);
16✔
234
            rng->initialize_with(nonce.data(), nonce.size());
16✔
235

236
            if(nonce_size < rng->security_level() / 8) {
16✔
237
               result.test_eq("not seeded", rng->is_seeded(), false);
10✔
238
               result.test_throws("invalid nonce size", [&rng]() { rng->random_vec(32); });
40✔
239
            } else {
240
               result.test_eq("is seeded", rng->is_seeded(), true);
6✔
241
               rng->random_vec(32);
12✔
242
            }
243
         }
16✔
244

245
         return result;
2✔
246
      }
2✔
247

248
      Test::Result test_prediction_resistance() {
2✔
249
         Test::Result result(rng_name() + " Prediction Resistance");
4✔
250

251
         // set reseed_interval = 1, forcing a reseed for every RNG request
252
         Request_Counting_RNG counting_rng;
2✔
253
         auto rng = make_rng(counting_rng, 1);
2✔
254

255
         rng->random_vec(16);
2✔
256
         result.test_eq("first request", counting_rng.randomize_count(), size_t(1));
2✔
257

258
         rng->random_vec(16);
2✔
259
         result.test_eq("second request", counting_rng.randomize_count(), size_t(2));
2✔
260

261
         rng->random_vec(16);
2✔
262
         result.test_eq("third request", counting_rng.randomize_count(), size_t(3));
2✔
263

264
         return result;
4✔
265
      }
2✔
266

267
      Test::Result test_fork_safety() {
×
268
         Test::Result result(rng_name() + " Fork Safety");
×
269

270
   #if defined(BOTAN_TARGET_OS_HAS_POSIX1)
271
         const size_t reseed_interval = 1024;
×
272

273
         // make sure rng is reseeded after every fork
274
         Request_Counting_RNG counting_rng;
×
275
         auto rng = make_rng(counting_rng, reseed_interval);
×
276

277
         rng->random_vec(16);
×
278
         result.test_eq("first request", counting_rng.randomize_count(), size_t(1));
×
279

280
         // fork and request from parent and child, both should output different sequences
281
         size_t count = counting_rng.randomize_count();
×
282
         Botan::secure_vector<uint8_t> parent_bytes(16);
×
283
         Botan::secure_vector<uint8_t> child_bytes(16);
×
284
         int fd[2];
×
285
         const int rc = ::pipe(fd);
×
286
         if(rc != 0) {
×
287
            result.test_failure("failed to create pipe");
×
288
         }
289

290
         const pid_t pid = ::fork();
×
291
         if(pid == -1) {
×
292
      #if defined(BOTAN_TARGET_OS_IS_EMSCRIPTEN)
293
            result.test_note("failed to fork process");
294
      #else
295
            result.test_failure("failed to fork process");
×
296
      #endif
297
            return result;
×
298
         } else if(pid != 0) {
×
299
            // parent process, wait for randomize_count from child's rng
300
            ::close(fd[1]);  // close write end in parent
×
301
            ssize_t got = ::read(fd[0], &count, sizeof(count));
×
302

303
            if(got > 0) {
×
304
               result.test_eq("expected bytes from child", got, sizeof(count));
×
305
               result.test_eq("parent not reseeded", counting_rng.randomize_count(), 1);
×
306
               result.test_eq("child reseed occurred", count, 2);
×
307
            } else {
308
               result.test_failure("Failed to read count size from child process");
×
309
            }
310

311
            parent_bytes = rng->random_vec(16);
×
312
            got = ::read(fd[0], child_bytes.data(), child_bytes.size());
×
313

314
            if(got > 0) {
×
315
               result.test_eq("expected bytes from child", got, child_bytes.size());
×
316
               result.test_ne("parent and child output sequences differ", parent_bytes, child_bytes);
×
317
            } else {
318
               result.test_failure("Failed to read RNG bytes from child process");
×
319
            }
320
            ::close(fd[0]);  // close read end in parent
×
321

322
            // wait for the child to exit
323
            int status = 0;
×
324
            ::waitpid(pid, &status, 0);
×
325
         } else {
326
            // child process, send randomize_count and first output sequence back to parent
327
            ::close(fd[0]);  // close read end in child
×
328
            rng->randomize(child_bytes.data(), child_bytes.size());
×
329
            count = counting_rng.randomize_count();
×
330
            ssize_t written = ::write(fd[1], &count, sizeof(count));
×
331
            BOTAN_UNUSED(written);
×
332
            try {
×
333
               rng->randomize(child_bytes.data(), child_bytes.size());
×
334
            } catch(std::exception& e) {
×
335
               static_cast<void>(fprintf(stderr, "%s", e.what()));  // NOLINT(*-vararg)
×
336
            }
×
337
            written = ::write(fd[1], child_bytes.data(), child_bytes.size());
×
338
            BOTAN_UNUSED(written);
×
339
            ::close(fd[1]);  // close write end in child
×
340

341
            /*
342
            * We can't call exit because it causes the mlock pool to be freed (#602)
343
            * We can't call _exit because it makes valgrind think we leaked memory.
344
            * So instead we execute something that will return 0 for us.
345
            */
346
            ::execl("/bin/true", "true", NULL);  // NOLINT(*-vararg)
×
347
            ::_exit(0);                          // just in case /bin/true isn't available (sandbox?)
×
348
         }
349
   #endif
350
         return result;
×
351
      }
×
352

353
      Test::Result test_randomize_with_ts_input() {
2✔
354
         Test::Result result(rng_name() + " Randomize With Timestamp Input");
4✔
355

356
         const size_t request_bytes = 64;
2✔
357
         const std::vector<uint8_t> seed(128);
2✔
358

359
         // check that randomize_with_ts_input() creates different output based on a timestamp
360
         // and possibly additional data, such as process id even with identical seeds
361
         Fixed_Output_RNG fixed_output_rng1(seed);
2✔
362
         Fixed_Output_RNG fixed_output_rng2(seed);
2✔
363

364
         auto rng1 = make_rng(fixed_output_rng1);
2✔
365
         auto rng2 = make_rng(fixed_output_rng2);
2✔
366

367
         Botan::secure_vector<uint8_t> output1(request_bytes);
2✔
368
         Botan::secure_vector<uint8_t> output2(request_bytes);
2✔
369

370
         rng1->randomize(output1.data(), output1.size());
2✔
371
         rng2->randomize(output2.data(), output2.size());
2✔
372

373
         result.test_eq("equal output due to same seed", output1, output2);
4✔
374

375
         rng1->randomize_with_ts_input(output1.data(), output1.size());
2✔
376
         rng2->randomize_with_ts_input(output2.data(), output2.size());
2✔
377

378
         result.test_ne("output differs due to different timestamp", output1, output2);
4✔
379

380
         return result;
2✔
381
      }
10✔
382

383
      Test::Result test_input_output_edge_cases() {
2✔
384
         Test::Result result(rng_name() + " randomize");
4✔
385

386
         const std::vector<uint8_t> seed(128);
2✔
387
         Fixed_Output_RNG fixed_output_rng(seed);
2✔
388

389
         auto rng = make_rng(fixed_output_rng);
2✔
390

391
         for(size_t i = 0; i != 4096; ++i) {
8,194✔
392
            std::vector<uint8_t> buf(i);
8,192✔
393
            rng->randomize(buf.data(), buf.size());
8,192✔
394
            rng->add_entropy(buf.data(), buf.size());
8,192✔
395

396
            result.test_success("RNG accepted input and output length");
16,384✔
397
         }
8,192✔
398

399
         return result;
2✔
400
      }
4✔
401
};
402

403
#endif
404

405
#if defined(BOTAN_HAS_HMAC_DRBG) && defined(BOTAN_HAS_SHA2_32)
406

407
class HMAC_DRBG_Unit_Tests final : public Stateful_RNG_Tests {
×
408
   public:
409
      std::string rng_name() const override { return "HMAC_DRBG"; }
6✔
410

411
      std::unique_ptr<Botan::Stateful_RNG> create_rng(Botan::RandomNumberGenerator* underlying_rng,
13✔
412
                                                      Botan::Entropy_Sources* underlying_es,
413
                                                      size_t reseed_interval) override {
414
         std::unique_ptr<Botan::MessageAuthenticationCode> mac =
13✔
415
            Botan::MessageAuthenticationCode::create("HMAC(SHA-256)");
13✔
416

417
         if(underlying_rng != nullptr && underlying_es != nullptr) {
13✔
418
            return std::make_unique<Botan::HMAC_DRBG>(std::move(mac), *underlying_rng, *underlying_es, reseed_interval);
3✔
419
         } else if(underlying_rng != nullptr) {
10✔
420
            return std::make_unique<Botan::HMAC_DRBG>(std::move(mac), *underlying_rng, reseed_interval);
7✔
421
         } else if(underlying_es != nullptr) {
3✔
422
            return std::make_unique<Botan::HMAC_DRBG>(std::move(mac), *underlying_es, reseed_interval);
2✔
423
         } else if(reseed_interval == 0) {
1✔
424
            return std::make_unique<Botan::HMAC_DRBG>(std::move(mac));
1✔
425
         } else {
426
            throw Test_Error("Invalid reseed interval in HMAC_DRBG unit test");
×
427
         }
428
      }
13✔
429

430
      Test::Result test_max_number_of_bytes_per_request() override {
1✔
431
         Test::Result result("HMAC_DRBG max_number_of_bytes_per_request");
1✔
432

433
         const std::string mac_string = "HMAC(SHA-256)";
1✔
434

435
         Request_Counting_RNG counting_rng;
1✔
436

437
         result.test_throws("HMAC_DRBG does not accept 0 for max_number_of_bytes_per_request",
2✔
438
                            [&mac_string, &counting_rng]() {
1✔
439
                               const Botan::HMAC_DRBG failing_rng(
1✔
440
                                  Botan::MessageAuthenticationCode::create(mac_string), counting_rng, 2, 0);
1✔
441
                            });
×
442

443
         result.test_throws("HMAC_DRBG does not accept values higher than 64KB for max_number_of_bytes_per_request",
2✔
444
                            [&mac_string, &counting_rng]() {
1✔
445
                               const Botan::HMAC_DRBG failing_rng(
1✔
446
                                  Botan::MessageAuthenticationCode::create(mac_string), counting_rng, 2, 64 * 1024 + 1);
1✔
447
                            });
×
448

449
         // set reseed_interval to 1 so we can test that a long request is split
450
         // into multiple, max_number_of_bytes_per_request long requests
451
         // for each smaller request, reseed_check() calls counting_rng::randomize(),
452
         // which we can compare with
453
         Botan::HMAC_DRBG rng(Botan::MessageAuthenticationCode::create(mac_string), counting_rng, 1, 64);
1✔
454

455
         rng.random_vec(63);
1✔
456
         result.test_eq("one request", counting_rng.randomize_count(), 1);
1✔
457

458
         rng.clear();
1✔
459
         counting_rng.clear();
1✔
460

461
         rng.random_vec(64);
1✔
462
         result.test_eq("one request", counting_rng.randomize_count(), 1);
1✔
463

464
         rng.clear();
1✔
465
         counting_rng.clear();
1✔
466

467
         rng.random_vec(65);
1✔
468
         result.test_eq("two requests", counting_rng.randomize_count(), 2);
1✔
469

470
         rng.clear();
1✔
471
         counting_rng.clear();
1✔
472

473
         rng.random_vec(1025);
1✔
474
         result.test_eq("17 requests", counting_rng.randomize_count(), 17);
1✔
475

476
         return result;
2✔
477
      }
1✔
478

479
      Test::Result test_reseed_interval_limits() override {
1✔
480
         Test::Result result("HMAC_DRBG reseed_interval limits");
1✔
481

482
         const std::string mac_string = "HMAC(SHA-256)";
1✔
483

484
         Request_Counting_RNG counting_rng;
1✔
485

486
         result.test_throws("HMAC_DRBG does not accept 0 for reseed_interval", [&mac_string, &counting_rng]() {
2✔
487
            const Botan::HMAC_DRBG failing_rng(Botan::MessageAuthenticationCode::create(mac_string), counting_rng, 0);
1✔
488
         });
×
489

490
         result.test_throws("HMAC_DRBG does not accept values higher than 2^24 for reseed_interval",
2✔
491
                            [&mac_string, &counting_rng]() {
1✔
492
                               const Botan::HMAC_DRBG failing_rng(Botan::MessageAuthenticationCode::create(mac_string),
2✔
493
                                                                  counting_rng,
1✔
494
                                                                  (static_cast<size_t>(1) << 24) + 1);
1✔
495
                            });
×
496

497
         return result;
1✔
498
      }
1✔
499

500
      Test::Result test_security_level() override {
1✔
501
         Test::Result result("HMAC_DRBG Security Level");
1✔
502

503
         std::vector<std::string> approved_hash_fns{"SHA-1", "SHA-224", "SHA-256", "SHA-512/256", "SHA-384", "SHA-512"};
7✔
504
         std::vector<uint32_t> security_strengths{128, 192, 256, 256, 256, 256};
2✔
505

506
         for(size_t i = 0; i < approved_hash_fns.size(); ++i) {
7✔
507
            const auto& hash_fn = approved_hash_fns[i];
6✔
508
            const size_t expected_security_level = security_strengths[i];
6✔
509

510
            const std::string mac_name = "HMAC(" + hash_fn + ")";
12✔
511
            auto mac = Botan::MessageAuthenticationCode::create(mac_name);
6✔
512
            if(!mac) {
6✔
513
               result.note_missing(mac_name);
1✔
514
               continue;
1✔
515
            }
516

517
            const Botan::HMAC_DRBG rng(std::move(mac));
5✔
518
            result.test_eq(hash_fn + " security level", rng.security_level(), expected_security_level);
5✔
519
         }
6✔
520

521
         return result;
1✔
522
      }
3✔
523

524
      Test::Result test_reseed_kat() override {
1✔
525
         Test::Result result("HMAC_DRBG Reseed KAT");
1✔
526

527
         Request_Counting_RNG counting_rng;
1✔
528
         auto rng = make_rng(counting_rng, 2);
1✔
529

530
         const Botan::secure_vector<uint8_t> seed_input(
1✔
531
            {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
532
             0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF});
1✔
533

534
         result.test_eq("is_seeded", rng->is_seeded(), false);
1✔
535

536
         rng->initialize_with(seed_input.data(), seed_input.size());
1✔
537

538
         Botan::secure_vector<uint8_t> out(32);
1✔
539

540
         rng->randomize(out.data(), out.size());
1✔
541
         result.test_eq("underlying RNG calls", counting_rng.randomize_count(), size_t(0));
1✔
542
         result.test_eq("out before reseed", out, "48D3B45AAB65EF92CCFCB9427EF20C90297065ECC1B8A525BFE4DC6FF36D0E38");
1✔
543

544
         // reseed must happen here
545
         rng->randomize(out.data(), out.size());
1✔
546
         result.test_eq("underlying RNG calls", counting_rng.randomize_count(), size_t(1));
1✔
547
         result.test_eq("out after reseed", out, "2F8FCA696832C984781123FD64F4B20C7379A25C87AB29A21C9BF468B0081CE2");
1✔
548

549
         return result;
2✔
550
      }
3✔
551
};
552

553
std::vector<Test::Result> hmac_drbg_multiple_requests() {
1✔
554
   auto null_rng = Botan::Null_RNG();
1✔
555
   constexpr auto rng_max_output = 1024;
1✔
556
   const auto seed = Botan::hex_decode("deadbeefbaadcafedeadbeefbaadcafedeadbeefbaadcafedeadbeefbaadcafe");
1✔
557

558
   auto make_seeded_rng = [&](size_t reseed_interval) {
5✔
559
      auto rng = std::make_unique<Botan::HMAC_DRBG>(Botan::MessageAuthenticationCode::create("HMAC(SHA-256)"),
×
560
                                                    null_rng,
561
                                                    reseed_interval + 1 /* off by one */,
8✔
562
                                                    rng_max_output);
4✔
563
      rng->add_entropy(seed);
4✔
564
      return rng;
4✔
565
   };
×
566

567
   return {CHECK("bulk and split output without input",
1✔
568
                 [&](auto& result) {
1✔
569
                    auto rng1 = make_seeded_rng(2);
1✔
570
                    auto rng2 = make_seeded_rng(2);
1✔
571

572
                    result.confirm("RNG 1 is seeded and ready to go", rng1->is_seeded());
2✔
573
                    result.confirm("RNG 2 is seeded and ready to go", rng2->is_seeded());
2✔
574

575
                    auto bulk = rng1->random_vec<std::vector<uint8_t>>(2 * rng_max_output);
1✔
576

577
                    auto split1 = rng2->random_vec<std::vector<uint8_t>>(rng_max_output);
1✔
578
                    auto split2 = rng2->random_vec<std::vector<uint8_t>>(rng_max_output);
1✔
579
                    split1.insert(split1.end(), split2.begin(), split2.end());
1✔
580

581
                    result.test_eq("Output is equal, regardless bulk request", bulk, split1);
2✔
582

583
                    return result;
2✔
584
                 }),
3✔
585

586
           CHECK("bulk and split output with input", [&](auto& result) {
1✔
587
              auto rng1 = make_seeded_rng(3);
1✔
588
              auto rng2 = make_seeded_rng(3);
1✔
589

590
              result.confirm("RNG 1 is seeded and ready to go", rng1->is_seeded());
2✔
591
              result.confirm("RNG 2 is seeded and ready to go", rng2->is_seeded());
2✔
592

593
              std::vector<uint8_t> bulk(3 * rng_max_output);
1✔
594
              rng1->randomize_with_input(bulk, seed);
2✔
595

596
              std::vector<uint8_t> split(3 * rng_max_output);
1✔
597
              std::span<uint8_t> const split_span(split);
1✔
598
              rng2->randomize_with_input(split_span.subspan(0, rng_max_output), seed);
2✔
599
              rng2->randomize_with_input(split_span.subspan(rng_max_output, rng_max_output), {});
2✔
600
              rng2->randomize_with_input(split_span.subspan(2 * rng_max_output), {});
2✔
601

602
              result.test_eq("Output is equal, regardless bulk request", bulk, split);
2✔
603

604
              return result;
2✔
605
           })};
6✔
606
}
2✔
607

608
BOTAN_REGISTER_TEST("rng", "hmac_drbg_unit", HMAC_DRBG_Unit_Tests);
609
BOTAN_REGISTER_TEST_FN("rng", "hmac_drbg_multi_request", hmac_drbg_multiple_requests);
610

611
#endif
612

613
#if defined(BOTAN_HAS_CHACHA_RNG)
614

615
class ChaCha_RNG_Unit_Tests final : public Stateful_RNG_Tests {
×
616
   public:
617
      std::string rng_name() const override { return "ChaCha_RNG"; }
6✔
618

619
      std::unique_ptr<Botan::Stateful_RNG> create_rng(Botan::RandomNumberGenerator* underlying_rng,
13✔
620
                                                      Botan::Entropy_Sources* underlying_es,
621
                                                      size_t reseed_interval) override {
622
         if(underlying_rng != nullptr && underlying_es != nullptr) {
13✔
623
            return std::make_unique<Botan::ChaCha_RNG>(*underlying_rng, *underlying_es, reseed_interval);
3✔
624
         } else if(underlying_rng != nullptr) {
10✔
625
            return std::make_unique<Botan::ChaCha_RNG>(*underlying_rng, reseed_interval);
7✔
626
         } else if(underlying_es != nullptr) {
3✔
627
            return std::make_unique<Botan::ChaCha_RNG>(*underlying_es, reseed_interval);
2✔
628
         } else if(reseed_interval == 0) {
1✔
629
            return std::make_unique<Botan::ChaCha_RNG>();
1✔
630
         } else {
631
            throw Test_Error("Invalid reseed interval in ChaCha_RNG unit test");
×
632
         }
633
      }
634

635
      Test::Result test_security_level() override {
1✔
636
         Test::Result result("ChaCha_RNG Security Level");
1✔
637
         const Botan::ChaCha_RNG rng;
1✔
638
         result.test_eq("Expected security level", rng.security_level(), size_t(256));
1✔
639
         return result;
1✔
640
      }
1✔
641

642
      Test::Result test_max_number_of_bytes_per_request() override {
1✔
643
         Test::Result result("ChaCha_RNG max_number_of_bytes_per_request");
1✔
644
         // ChaCha_RNG doesn't have this notion
645
         return result;
1✔
646
      }
647

648
      Test::Result test_reseed_interval_limits() override {
1✔
649
         Test::Result result("ChaCha_RNG reseed_interval limits");
1✔
650
         // ChaCha_RNG doesn't apply any limits to reseed_interval
651
         return result;
1✔
652
      }
653

654
      Test::Result test_reseed_kat() override {
1✔
655
         Test::Result result("ChaCha_RNG Reseed KAT");
1✔
656

657
         Request_Counting_RNG counting_rng;
1✔
658
         auto rng = make_rng(counting_rng, 2);
1✔
659

660
         const Botan::secure_vector<uint8_t> seed_input(32);
1✔
661

662
         result.test_eq("is_seeded", rng->is_seeded(), false);
1✔
663

664
         rng->initialize_with(seed_input.data(), seed_input.size());
1✔
665

666
         Botan::secure_vector<uint8_t> out(32);
1✔
667

668
         rng->randomize(out.data(), out.size());
1✔
669
         result.test_eq("underlying RNG calls", counting_rng.randomize_count(), size_t(0));
1✔
670
         result.test_eq("out before reseed", out, "1F0E6F13429D5073B59C057C37CBE9587740A0A894D247E2596C393CE91DDC6F");
1✔
671

672
         // reseed must happen here
673
         rng->randomize(out.data(), out.size());
1✔
674
         result.test_eq("underlying RNG calls", counting_rng.randomize_count(), size_t(1));
1✔
675
         result.test_eq("out after reseed", out, "F2CAE73F22684D5D773290B48FDCDA0E6C0661EBA0A854AFEC922832BDBB9C49");
1✔
676

677
         return result;
2✔
678
      }
3✔
679
};
680

681
BOTAN_REGISTER_TEST("rng", "chacha_rng_unit", ChaCha_RNG_Unit_Tests);
682

683
#endif
684

685
#if defined(BOTAN_HAS_AUTO_RNG)
686

687
class AutoSeeded_RNG_Tests final : public Test {
×
688
   private:
689
      static Test::Result auto_rng_tests() {
1✔
690
         Test::Result result("AutoSeeded_RNG");
1✔
691

692
         Botan::Null_RNG null_rng;
1✔
693

694
         result.test_eq("Null_RNG is null", null_rng.is_seeded(), false);
1✔
695

696
         try {
1✔
697
            const Botan::AutoSeeded_RNG rng(null_rng);
1✔
698
         } catch(Botan::PRNG_Unseeded&) {
1✔
699
            result.test_success("AutoSeeded_RNG rejected useless RNG");
1✔
700
         }
1✔
701

702
   #if defined(BOTAN_HAS_ENTROPY_SOURCE)
703
         Botan::Entropy_Sources no_entropy_for_you;
1✔
704

705
         try {
1✔
706
            const Botan::AutoSeeded_RNG rng(no_entropy_for_you);
1✔
707
            result.test_failure("AutoSeeded_RNG should have rejected useless entropy source");
×
708
         } catch(Botan::PRNG_Unseeded&) {
1✔
709
            result.test_success("AutoSeeded_RNG rejected empty entropy source");
1✔
710
         }
1✔
711

712
         try {
1✔
713
            const Botan::AutoSeeded_RNG rng(null_rng, no_entropy_for_you);
1✔
714
         } catch(Botan::PRNG_Unseeded&) {
1✔
715
            result.test_success("AutoSeeded_RNG rejected useless RNG+entropy sources");
1✔
716
         }
1✔
717
   #endif
718

719
         Botan::AutoSeeded_RNG rng;
1✔
720

721
         result.confirm("AutoSeeded_RNG::name", rng.name().starts_with("HMAC_DRBG(HMAC(SHA-"));
3✔
722

723
         result.confirm("AutoSeeded_RNG starts seeded", rng.is_seeded());
2✔
724
         rng.random_vec(16);  // generate and discard output
1✔
725
         rng.clear();
1✔
726
         result.test_eq("AutoSeeded_RNG unseeded after calling clear", rng.is_seeded(), false);
1✔
727

728
         // AutoSeeded_RNG automatically reseeds as required:
729
         rng.random_vec(16);
1✔
730
         result.confirm("AutoSeeded_RNG can be reseeded", rng.is_seeded());
2✔
731

732
         result.confirm("AutoSeeded_RNG ", rng.is_seeded());
2✔
733
         rng.random_vec(16);  // generate and discard output
1✔
734
         rng.clear();
1✔
735
         result.test_eq("AutoSeeded_RNG unseeded after calling clear", rng.is_seeded(), false);
1✔
736

737
   #if defined(BOTAN_HAS_ENTROPY_SOURCE)
738
         const size_t no_entropy_bits = rng.reseed(no_entropy_for_you, 256, std::chrono::milliseconds(300));
1✔
739
         result.test_eq("AutoSeeded_RNG can't reseed from nothing", no_entropy_bits, 0);
1✔
740
         result.test_eq("AutoSeeded_RNG still unseeded", rng.is_seeded(), false);
1✔
741
   #endif
742

743
         rng.random_vec(16);  // generate and discard output
1✔
744
         result.confirm("AutoSeeded_RNG can be reseeded", rng.is_seeded());
2✔
745

746
         for(size_t i = 0; i != 4096; ++i) {
4,097✔
747
            std::vector<uint8_t> buf(i);
4,096✔
748
            rng.randomize(buf.data(), buf.size());
4,096✔
749
            rng.add_entropy(buf.data(), buf.size());
4,096✔
750

751
            result.test_success("AutoSeeded_RNG accepted input and output length");
8,192✔
752
         }
4,096✔
753

754
         rng.clear();
1✔
755

756
         return result;
1✔
757
      }
1✔
758

759
   public:
760
      std::vector<Test::Result> run() override {
1✔
761
         std::vector<Test::Result> results;
1✔
762
         results.push_back(auto_rng_tests());
2✔
763
         return results;
1✔
764
      }
×
765
};
766

767
BOTAN_REGISTER_TEST("rng", "auto_rng_unit", AutoSeeded_RNG_Tests);
768

769
#endif
770

771
#if defined(BOTAN_HAS_SYSTEM_RNG)
772

773
class System_RNG_Tests final : public Test {
×
774
   public:
775
      std::vector<Test::Result> run() override {
1✔
776
         Test::Result result("System_RNG");
1✔
777

778
         Botan::System_RNG rng;
1✔
779

780
         result.test_gte("Some non-empty name is returned", rng.name().size(), 1);
3✔
781

782
         result.confirm("System RNG always seeded", rng.is_seeded());
3✔
783
         rng.clear();  // clear is a noop for system rng
1✔
784
         result.confirm("System RNG always seeded", rng.is_seeded());
3✔
785

786
   #if defined(BOTAN_HAS_ENTROPY_SOURCE)
787
         rng.reseed(Botan::Entropy_Sources::global_sources(), 256, std::chrono::milliseconds(100));
1✔
788
   #endif
789

790
         for(size_t i = 0; i != 128; ++i) {
129✔
791
            std::vector<uint8_t> out_buf(i);
128✔
792
            rng.randomize(out_buf.data(), out_buf.size());
128✔
793
            rng.add_entropy(out_buf.data(), out_buf.size());
128✔
794
         }
128✔
795

796
         if(Test::run_long_tests() && Test::run_memory_intensive_tests() && (sizeof(size_t) > 4)) {
1✔
797
            // Pass buffer with a size greater than 32bit
798
            const size_t size32BitsMax = std::numeric_limits<uint32_t>::max();
1✔
799
            const size_t checkSize = 1024;
1✔
800
            std::vector<uint8_t> large_buf(size32BitsMax + checkSize);
1✔
801
            std::memset(large_buf.data() + size32BitsMax, 0xFE, checkSize);
1✔
802

803
            rng.randomize(large_buf.data(), large_buf.size());
1✔
804

805
            std::vector<uint8_t> check_buf(checkSize, 0xFE);
1✔
806

807
            result.confirm("System RNG failed to write after 4GB boundary",
2✔
808
                           std::memcmp(large_buf.data() + size32BitsMax, check_buf.data(), checkSize) != 0);
1✔
809
         }
2✔
810

811
         return std::vector<Test::Result>{result};
2✔
812
      }
2✔
813
};
814

815
BOTAN_REGISTER_TEST("rng", "system_rng", System_RNG_Tests);
816

817
#endif
818

819
#if defined(BOTAN_HAS_PROCESSOR_RNG)
820

821
class Processor_RNG_Tests final : public Test {
×
822
   public:
823
      std::vector<Test::Result> run() override {
1✔
824
         Test::Result result("Processor_RNG");
1✔
825

826
         if(Botan::Processor_RNG::available()) {
1✔
827
            Botan::Processor_RNG rng;
1✔
828

829
            result.test_ne("Has a name", rng.name(), "");
2✔
830
            result.confirm("CPU RNG always seeded", rng.is_seeded());
2✔
831
            rng.clear();  // clear is a noop for rdrand
1✔
832
            result.confirm("CPU RNG always seeded", rng.is_seeded());
2✔
833

834
   #if defined(BOTAN_HAS_ENTROPY_SOURCE)
835
            const size_t reseed_bits =
1✔
836
               rng.reseed(Botan::Entropy_Sources::global_sources(), 256, std::chrono::seconds(1));
1✔
837
            result.test_eq("CPU RNG cannot consume inputs", reseed_bits, size_t(0));
1✔
838
   #endif
839

840
            /*
841
            Processor_RNG ignores add_entropy calls - confirm this by passing
842
            an invalid ptr/length field to add_entropy. If it examined its
843
            arguments, it would crash...
844
            */
845
            // NOLINTNEXTLINE(*-no-int-to-ptr)
846
            const uint8_t* invalid_ptr = reinterpret_cast<const uint8_t*>(static_cast<uintptr_t>(0xDEADC0DE));
1✔
847
            const size_t invalid_ptr_len = 64 * 1024;
1✔
848
            rng.add_entropy(invalid_ptr, invalid_ptr_len);
1✔
849

850
            for(size_t i = 0; i != 128; ++i) {
129✔
851
               std::vector<uint8_t> out_buf(i);
128✔
852
               rng.randomize(out_buf.data(), out_buf.size());
128✔
853
            }
128✔
854
         } else {
1✔
855
            result.test_throws("Processor_RNG throws if instruction not available",
×
856
                               []() { const Botan::Processor_RNG rng; });
×
857
         }
858

859
         return std::vector<Test::Result>{result};
3✔
860
      }
2✔
861
};
862

863
BOTAN_REGISTER_TEST("rng", "processor_rng", Processor_RNG_Tests);
864

865
#endif
866

867
}  // namespace
868

869
}  // namespace Botan_Tests
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

© 2026 Coveralls, Inc