• 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.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
#endif
40

41
namespace Botan_CLI {
42

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

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

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

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

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

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

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

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

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

100
      return rng;
120✔
101
   }
120✔
102
#endif
103

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

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

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

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

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

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

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

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

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

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

163
BOTAN_REGISTER_COMMAND("rng", RNG);
18✔
164

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