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

randombit / botan / 4855062618

01 May 2023 10:04PM UTC coverage: 92.147% (+0.004%) from 92.143%
4855062618

push

github

77604 of 84218 relevant lines covered (92.15%)

11834916.63 hits per line

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

77.19
/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/rng.h>
9
#include <botan/entropy_src.h>
10
#include <botan/hex.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>
32
cli_make_rng(const std::string& rng_type, const std::string& hex_drbg_seed)
144✔
33
   {
34
#if defined(BOTAN_HAS_SYSTEM_RNG)
35
   if(rng_type == "system" || rng_type.empty())
144✔
36
      {
37
      return std::make_shared<Botan::System_RNG>();
24✔
38
      }
39
#endif
40

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

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

48
      if(rng_type == "entropy")
2✔
49
         rng = std::make_shared<Botan::AutoSeeded_RNG>(Botan::Entropy_Sources::global_sources());
1✔
50
      else
51
         rng = std::make_shared<Botan::AutoSeeded_RNG>();
1✔
52

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

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

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

71
      return rng;
118✔
72
      }
118✔
73
#endif
74

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

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

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

96
      std::string group() const override
1✔
97
         {
98
         return "misc";
1✔
99
         }
100

101
      std::string description() const override
1✔
102
         {
103
         return "Sample random bytes from the specified rng";
1✔
104
         }
105

106
      void go() override
14✔
107
         {
108
         const std::string format = get_arg("format");
14✔
109
         std::string type = get_arg("rng-type");
14✔
110

111
         if(type.empty())
14✔
112
            {
113
            for(std::string flag : { "system", "rdrand", "auto", "entropy", "drbg" })
8✔
114
               {
115
               if(flag_set(flag))
8✔
116
                  {
117
                  type = flag;
3✔
118
                  break;
3✔
119
                  }
120
               }
8✔
121
            }
122

123
         const std::string drbg_seed = get_arg("drbg-seed");
14✔
124
         auto rng = cli_make_rng(type, drbg_seed);
14✔
125

126
         for(const std::string& req : get_arg_list("bytes"))
30✔
127
            {
128
            const size_t req_len = Botan::to_u32bit(req);
16✔
129
            const auto blob = rng->random_vec(req_len);
16✔
130

131
            if(format == "binary" || format == "raw")
16✔
132
               {
133
               output().write(reinterpret_cast<const char*>(blob.data()), blob.size());
×
134
               }
135
            else
136
               {
137
               output() << format_blob(format, blob) << "\n";
32✔
138
               }
139
            }
30✔
140
         }
25✔
141
   };
142

143
BOTAN_REGISTER_COMMAND("rng", RNG);
15✔
144

145
}
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