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

randombit / botan / 12805544433

16 Jan 2025 09:08AM UTC coverage: 90.876% (-0.4%) from 91.245%
12805544433

Pull #4540

github

web-flow
Merge cc1ceff51 into 9b798efbb
Pull Request #4540: PKCS #11 Version 3.2 Support

93425 of 102805 relevant lines covered (90.88%)

11409241.89 hits per line

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

85.0
/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_ESDM_RNG)
22
   #include <botan/esdm_rng.h>
23
#endif
24

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

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

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

37
namespace Botan_CLI {
38

39
std::shared_ptr<Botan::RandomNumberGenerator> cli_make_rng(const std::string& rng_type,
154✔
40
                                                           const std::string& hex_drbg_seed) {
41
#if defined(BOTAN_HAS_SYSTEM_RNG)
42
   if(rng_type == "system" || rng_type.empty()) {
154✔
43
      return std::make_shared<Botan::System_RNG>();
28✔
44
   }
45
#endif
46

47
#if defined(BOTAN_HAS_ESDM_RNG)
48
   if(rng_type == "esdm-full") {
126✔
49
      return std::make_shared<Botan::ESDM_RNG>(false);
1✔
50
   }
51
   if(rng_type == "esdm-pr") {
125✔
52
      return std::make_shared<Botan::ESDM_RNG>(true);
1✔
53
   }
54
#endif
55

56
   const std::vector<uint8_t> drbg_seed = Botan::hex_decode(hex_drbg_seed);
124✔
57

58
#if defined(BOTAN_HAS_AUTO_SEEDING_RNG)
59
   if(rng_type == "auto" || rng_type == "entropy" || rng_type.empty()) {
124✔
60
      std::shared_ptr<Botan::RandomNumberGenerator> rng;
2✔
61

62
      if(rng_type == "entropy") {
2✔
63
   #if defined(BOTAN_HAS_ENTROPY_SOURCE)
64
         rng = std::make_shared<Botan::AutoSeeded_RNG>(Botan::Entropy_Sources::global_sources());
1✔
65
   #else
66
         throw CLI_Error_Unsupported("Entropy sources not included in this build");
67
   #endif
68
      } else {
69
         rng = std::make_shared<Botan::AutoSeeded_RNG>();
1✔
70
      }
71

72
      if(!drbg_seed.empty()) {
2✔
73
         rng->add_entropy(drbg_seed.data(), drbg_seed.size());
×
74
      }
75
      return rng;
2✔
76
   }
2✔
77
#endif
78

79
#if defined(BOTAN_HAS_HMAC_DRBG) && defined(BOTAN_HAS_SHA2_32)
80
   if(rng_type == "drbg" || (rng_type.empty() && drbg_seed.empty() == false)) {
122✔
81
      auto mac = Botan::MessageAuthenticationCode::create_or_throw("HMAC(SHA-256)");
121✔
82
      auto rng = std::make_shared<Botan::HMAC_DRBG>(std::move(mac));
121✔
83
      rng->add_entropy(drbg_seed.data(), drbg_seed.size());
121✔
84

85
      if(rng->is_seeded() == false) {
121✔
86
         throw CLI_Error("For " + rng->name() + " a seed of at least " + std::to_string(rng->security_level() / 8) +
×
87
                         " bytes must be provided");
×
88
      }
89

90
      return rng;
121✔
91
   }
121✔
92
#endif
93

94
#if defined(BOTAN_HAS_PROCESSOR_RNG)
95
   if(rng_type == "rdrand" || rng_type == "cpu" || rng_type.empty()) {
1✔
96
      if(Botan::Processor_RNG::available()) {
1✔
97
         return std::make_shared<Botan::Processor_RNG>();
1✔
98
      } else if(rng_type.empty() == false) {
×
99
         throw CLI_Error("RNG instruction not supported on this processor");
×
100
      }
101
   }
102
#endif
103

104
   if(rng_type.empty()) {
×
105
      throw CLI_Error_Unsupported("No random number generator seems to be available in the current build");
×
106
   } else {
107
      throw CLI_Error_Unsupported("RNG", rng_type);
×
108
   }
109
}
124✔
110

111
class RNG final : public Command {
112
   public:
113
      RNG() :
18✔
114
            Command(
115
               "rng --format=hex --system --esdm-full --esdm-pr --rdrand --auto --entropy --drbg --drbg-seed= *bytes") {
18✔
116
      }
18✔
117

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

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

122
      void go() override {
17✔
123
         const std::string format = get_arg("format");
17✔
124
         std::string type = get_arg("rng-type");
17✔
125

126
         if(type.empty()) {
17✔
127
            for(std::string flag : {"system", "rdrand", "auto", "entropy", "drbg", "esdm-full", "esdm-pr"}) {
23✔
128
               if(flag_set(flag)) {
23✔
129
                  type = flag;
6✔
130
                  break;
6✔
131
               }
132
            }
23✔
133
         }
134

135
         const std::string drbg_seed = get_arg("drbg-seed");
17✔
136
         auto rng = cli_make_rng(type, drbg_seed);
17✔
137

138
         for(const std::string& req : get_arg_list("bytes")) {
36✔
139
            const size_t req_len = Botan::to_u32bit(req);
19✔
140
            const auto blob = rng->random_vec(req_len);
19✔
141

142
            if(format == "binary" || format == "raw") {
19✔
143
               write_output(blob);
×
144
            } else {
145
               output() << format_blob(format, blob) << "\n";
38✔
146
            }
147
         }
36✔
148
      }
17✔
149
};
150

151
BOTAN_REGISTER_COMMAND("rng", RNG);
18✔
152

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