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

randombit / botan / 21753596263

06 Feb 2026 02:13PM UTC coverage: 90.063% (-0.01%) from 90.073%
21753596263

Pull #5289

github

web-flow
Merge 587099284 into 8ea0ca252
Pull Request #5289: Further misc header reductions, forward declarations, etc

102237 of 113517 relevant lines covered (90.06%)

11402137.11 hits per line

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

84.13
/src/cli/cli_rng.cpp
1
/*
2
* (C) 2015,2017 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6

7
#include "cli.h"
8

9
#include <botan/hex.h>
10
#include <botan/rng.h>
11
#include <botan/internal/parsing.h>
12

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

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

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

25
#if defined(BOTAN_HAS_ESDM_RNG)
26
   #include <botan/esdm_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_HMAC_DRBG)
38
   #include <botan/hmac_drbg.h>
39
   #include <botan/mac.h>
40
#endif
41

42
namespace Botan_CLI {
43

44
std::shared_ptr<Botan::RandomNumberGenerator> cli_make_rng(const std::string& rng_type,
132✔
45
                                                           const std::string& hex_drbg_seed) {
46
#if defined(BOTAN_HAS_SYSTEM_RNG)
47
   if(rng_type == "system" || rng_type.empty()) {
132✔
48
      return std::make_shared<Botan::System_RNG>();
28✔
49
   }
50
#endif
51

52
#if defined(BOTAN_HAS_ESDM_RNG)
53
   if(rng_type == "esdm-full") {
104✔
54
      return std::make_shared<Botan::ESDM_RNG>(false);
1✔
55
   }
56
   if(rng_type == "esdm-pr") {
103✔
57
      return std::make_shared<Botan::ESDM_RNG>(true);
1✔
58
   }
59
#endif
60

61
#if defined(BOTAN_HAS_JITTER_RNG)
62
   if(rng_type == "jitter") {
102✔
63
      return std::make_shared<Botan::Jitter_RNG>();
×
64
   }
65
#endif
66

67
   const std::vector<uint8_t> drbg_seed = Botan::hex_decode(hex_drbg_seed);
102✔
68

69
#if defined(BOTAN_HAS_AUTO_SEEDING_RNG)
70
   if(rng_type == "auto" || rng_type == "entropy" || rng_type.empty()) {
102✔
71
      std::shared_ptr<Botan::RandomNumberGenerator> rng;
2✔
72

73
      if(rng_type == "entropy") {
2✔
74
   #if defined(BOTAN_HAS_ENTROPY_SOURCE)
75
         rng = std::make_shared<Botan::AutoSeeded_RNG>(Botan::Entropy_Sources::global_sources());
1✔
76
   #else
77
         throw CLI_Error_Unsupported("Entropy sources not included in this build");
78
   #endif
79
      } else {
80
         rng = std::make_shared<Botan::AutoSeeded_RNG>();
1✔
81
      }
82

83
      if(!drbg_seed.empty()) {
2✔
84
         rng->add_entropy(drbg_seed.data(), drbg_seed.size());
×
85
      }
86
      return rng;
2✔
87
   }
2✔
88
#endif
89

90
#if defined(BOTAN_HAS_HMAC_DRBG) && defined(BOTAN_HAS_SHA2_32)
91
   if(rng_type == "drbg" || (rng_type.empty() && drbg_seed.empty() == false)) {
100✔
92
      auto mac = Botan::MessageAuthenticationCode::create_or_throw("HMAC(SHA-256)");
99✔
93
      auto rng = std::make_shared<Botan::HMAC_DRBG>(std::move(mac));
99✔
94
      rng->add_entropy(drbg_seed.data(), drbg_seed.size());
99✔
95

96
      if(rng->is_seeded() == false) {
99✔
97
         throw CLI_Error("For " + rng->name() + " a seed of at least " + std::to_string(rng->security_level() / 8) +
×
98
                         " bytes must be provided");
×
99
      }
100

101
      return rng;
99✔
102
   }
99✔
103
#endif
104

105
#if defined(BOTAN_HAS_PROCESSOR_RNG)
106
   if(rng_type == "rdrand" || rng_type == "cpu" || rng_type.empty()) {
1✔
107
      if(Botan::Processor_RNG::available()) {
1✔
108
         return std::make_shared<Botan::Processor_RNG>();
1✔
109
      } else if(rng_type.empty() == false) {
×
110
         throw CLI_Error("RNG instruction not supported on this processor");
×
111
      }
112
   }
113
#endif
114

115
   if(rng_type.empty()) {
×
116
      throw CLI_Error_Unsupported("No random number generator seems to be available in the current build");
×
117
   } else {
118
      throw CLI_Error_Unsupported("RNG", rng_type);
×
119
   }
120
}
102✔
121

122
class RNG final : public Command {
123
   public:
124
      RNG() :
17✔
125
            Command(
126
               "rng --format=hex --system --esdm-full --esdm-pr --jitter --rdrand --auto --entropy --drbg --drbg-seed= *bytes") {
17✔
127
      }
17✔
128

129
      std::string group() const override { return "misc"; }
1✔
130

131
      std::string description() const override { return "Sample random bytes from the specified rng"; }
1✔
132

133
      void go() override {
16✔
134
         const std::string format = get_arg("format");
16✔
135
         std::string type = get_arg("rng-type");
16✔
136

137
         if(type.empty()) {
16✔
138
            const std::vector<std::string> known_rng_types = {
6✔
139
               "system", "rdrand", "auto", "entropy", "drbg", "esdm-full", "esdm-pr", "jitter"};
6✔
140
            for(const auto& flag : known_rng_types) {
23✔
141
               if(flag_set(flag)) {
23✔
142
                  type = flag;
6✔
143
                  break;
144
               }
145
            }
146
         }
6✔
147

148
         const std::string drbg_seed = get_arg("drbg-seed");
16✔
149
         auto rng = cli_make_rng(type, drbg_seed);
16✔
150

151
         for(const std::string& req : get_arg_list("bytes")) {
35✔
152
            const size_t req_len = Botan::to_u32bit(req);
19✔
153
            const auto blob = rng->random_vec(req_len);
19✔
154

155
            if(format == "binary" || format == "raw") {
19✔
156
               write_output(blob);
×
157
            } else {
158
               output() << format_blob(format, blob) << "\n";
38✔
159
            }
160
         }
35✔
161
      }
16✔
162
};
163

164
BOTAN_REGISTER_COMMAND("rng", RNG);
17✔
165

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

© 2026 Coveralls, Inc