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

randombit / botan / 23225340130

18 Mar 2026 01:53AM UTC coverage: 89.677% (-0.001%) from 89.678%
23225340130

push

github

web-flow
Merge pull request #5456 from randombit/jack/clang-tidy-22

Fix various warnings from clang-tidy 22

104438 of 116460 relevant lines covered (89.68%)

11819947.55 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,
148✔
45
                                                           const std::string& hex_drbg_seed) {
46
#if defined(BOTAN_HAS_SYSTEM_RNG)
47
   if(rng_type == "system" || rng_type.empty()) {
148✔
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") {
120✔
54
      return std::make_shared<Botan::ESDM_RNG>(false);
1✔
55
   }
56
   if(rng_type == "esdm-pr") {
119✔
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") {
118✔
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);
118✔
68

69
#if defined(BOTAN_HAS_AUTO_SEEDING_RNG)
70
   if(rng_type == "auto" || rng_type == "entropy" || rng_type.empty()) {
118✔
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)) {
116✔
92
      auto mac = Botan::MessageAuthenticationCode::create_or_throw("HMAC(SHA-256)");
115✔
93
      auto rng = std::make_shared<Botan::HMAC_DRBG>(std::move(mac));
115✔
94
      rng->add_entropy(drbg_seed.data(), drbg_seed.size());
115✔
95

96
      if(rng->is_seeded() == false) {
115✔
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;
115✔
102
   }
115✔
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
}
118✔
121

122
namespace {
123

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

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

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

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

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

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

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

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

166
BOTAN_REGISTER_COMMAND("rng", RNG);
17✔
167

168
}  // namespace
169

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