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

randombit / botan / 5123321399

30 May 2023 04:06PM UTC coverage: 92.213% (+0.004%) from 92.209%
5123321399

Pull #3558

github

web-flow
Merge dd72f7389 into 057bcbc35
Pull Request #3558: Add braces around all if/else statements

75602 of 81986 relevant lines covered (92.21%)

11859779.3 hits per line

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

77.78
/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
#include <botan/entropy_src.h>
9
#include <botan/hex.h>
10
#include <botan/rng.h>
11
#include <botan/internal/parsing.h>
12

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

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

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

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

29
namespace Botan_CLI {
30

31
std::shared_ptr<Botan::RandomNumberGenerator> cli_make_rng(const std::string& rng_type,
146✔
32
                                                           const std::string& hex_drbg_seed) {
33
#if defined(BOTAN_HAS_SYSTEM_RNG)
34
   if(rng_type == "system" || rng_type.empty()) {
146✔
35
      return std::make_shared<Botan::System_RNG>();
26✔
36
   }
37
#endif
38

39
   const std::vector<uint8_t> drbg_seed = Botan::hex_decode(hex_drbg_seed);
120✔
40

41
#if defined(BOTAN_HAS_AUTO_SEEDING_RNG)
42
   if(rng_type == "auto" || rng_type == "entropy" || rng_type.empty()) {
120✔
43
      std::shared_ptr<Botan::RandomNumberGenerator> rng;
2✔
44

45
      if(rng_type == "entropy") {
2✔
46
         rng = std::make_shared<Botan::AutoSeeded_RNG>(Botan::Entropy_Sources::global_sources());
1✔
47
      } else {
48
         rng = std::make_shared<Botan::AutoSeeded_RNG>();
1✔
49
      }
50

51
      if(!drbg_seed.empty()) {
2✔
52
         rng->add_entropy(drbg_seed.data(), drbg_seed.size());
×
53
      }
54
      return rng;
2✔
55
   }
2✔
56
#endif
57

58
#if defined(BOTAN_HAS_HMAC_DRBG) && defined(BOTAN_HAS_SHA2_32)
59
   if(rng_type == "drbg" || (rng_type.empty() && drbg_seed.empty() == false)) {
118✔
60
      auto mac = Botan::MessageAuthenticationCode::create_or_throw("HMAC(SHA-256)");
118✔
61
      auto rng = std::make_shared<Botan::HMAC_DRBG>(std::move(mac));
118✔
62
      rng->add_entropy(drbg_seed.data(), drbg_seed.size());
118✔
63

64
      if(rng->is_seeded() == false) {
118✔
65
         throw CLI_Error("For " + rng->name() + " a seed of at least " + std::to_string(rng->security_level() / 8) +
×
66
                         " bytes must be provided");
×
67
      }
68

69
      return rng;
118✔
70
   }
118✔
71
#endif
72

73
#if defined(BOTAN_HAS_PROCESSOR_RNG)
74
   if(rng_type == "rdrand" || rng_type == "cpu" || rng_type.empty()) {
×
75
      if(Botan::Processor_RNG::available()) {
×
76
         return std::make_shared<Botan::Processor_RNG>();
×
77
      } else if(rng_type.empty() == false) {
×
78
         throw CLI_Error("RNG instruction not supported on this processor");
×
79
      }
80
   }
81
#endif
82

83
   if(rng_type.empty()) {
×
84
      throw CLI_Error_Unsupported("No random number generator seems to be available in the current build");
×
85
   } else {
86
      throw CLI_Error_Unsupported("RNG", rng_type);
×
87
   }
88
}
146✔
89

90
class RNG final : public Command {
91
   public:
92
      RNG() : Command("rng --format=hex --system --rdrand --auto --entropy --drbg --drbg-seed= *bytes") {}
30✔
93

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

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

98
      void go() override {
14✔
99
         const std::string format = get_arg("format");
14✔
100
         std::string type = get_arg("rng-type");
14✔
101

102
         if(type.empty()) {
14✔
103
            for(std::string flag : {"system", "rdrand", "auto", "entropy", "drbg"}) {
8✔
104
               if(flag_set(flag)) {
8✔
105
                  type = flag;
3✔
106
                  break;
3✔
107
               }
108
            }
8✔
109
         }
110

111
         const std::string drbg_seed = get_arg("drbg-seed");
14✔
112
         auto rng = cli_make_rng(type, drbg_seed);
14✔
113

114
         for(const std::string& req : get_arg_list("bytes")) {
30✔
115
            const size_t req_len = Botan::to_u32bit(req);
16✔
116
            const auto blob = rng->random_vec(req_len);
16✔
117

118
            if(format == "binary" || format == "raw") {
16✔
119
               output().write(reinterpret_cast<const char*>(blob.data()), blob.size());
×
120
            } else {
121
               output() << format_blob(format, blob) << "\n";
32✔
122
            }
123
         }
30✔
124
      }
25✔
125
};
126

127
BOTAN_REGISTER_COMMAND("rng", RNG);
15✔
128

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