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

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 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
      if(!drbg_seed.empty())
2✔
51
         rng->add_entropy(drbg_seed.data(), drbg_seed.size());
×
52
      return rng;
2✔
53
   }
2✔
54
#endif
55

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

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

66
      return rng;
118✔
67
   }
118✔
68
#endif
69

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

79
   if(rng_type.empty())
×
80
      throw CLI_Error_Unsupported("No random number generator seems to be available in the current build");
×
81
   else
82
      throw CLI_Error_Unsupported("RNG", rng_type);
×
83
}
146✔
84

85
class RNG final : public Command {
86
   public:
87
      RNG() : Command("rng --format=hex --system --rdrand --auto --entropy --drbg --drbg-seed= *bytes") {}
30✔
88

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

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

93
      void go() override {
14✔
94
         const std::string format = get_arg("format");
14✔
95
         std::string type = get_arg("rng-type");
14✔
96

97
         if(type.empty()) {
14✔
98
            for(std::string flag : {"system", "rdrand", "auto", "entropy", "drbg"}) {
8✔
99
               if(flag_set(flag)) {
8✔
100
                  type = flag;
3✔
101
                  break;
3✔
102
               }
103
            }
8✔
104
         }
105

106
         const std::string drbg_seed = get_arg("drbg-seed");
14✔
107
         auto rng = cli_make_rng(type, drbg_seed);
14✔
108

109
         for(const std::string& req : get_arg_list("bytes")) {
30✔
110
            const size_t req_len = Botan::to_u32bit(req);
16✔
111
            const auto blob = rng->random_vec(req_len);
16✔
112

113
            if(format == "binary" || format == "raw") {
16✔
114
               output().write(reinterpret_cast<const char*>(blob.data()), blob.size());
×
115
            } else {
116
               output() << format_blob(format, blob) << "\n";
32✔
117
            }
118
         }
30✔
119
      }
25✔
120
};
121

122
BOTAN_REGISTER_COMMAND("rng", RNG);
15✔
123

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