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

randombit / botan / 5111374265

29 May 2023 11:19AM UTC coverage: 92.227% (+0.5%) from 91.723%
5111374265

push

github

randombit
Next release will be 3.1.0. Update release notes

75588 of 81959 relevant lines covered (92.23%)

11886470.91 hits per line

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

98.15
/src/lib/entropy/entropy_srcs.cpp
1
/*
2
* Entropy Source Polling
3
* (C) 2008-2010,2015 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include <botan/entropy_src.h>
9

10
#include <botan/rng.h>
11

12
#if defined(BOTAN_HAS_SYSTEM_RNG)
13
   #include <botan/system_rng.h>
14
#endif
15

16
#if defined(BOTAN_HAS_PROCESSOR_RNG)
17
   #include <botan/processor_rng.h>
18
#endif
19

20
#if defined(BOTAN_HAS_ENTROPY_SRC_RDSEED)
21
   #include <botan/internal/rdseed.h>
22
#endif
23

24
#if defined(BOTAN_HAS_ENTROPY_SRC_WIN32)
25
   #include <botan/internal/es_win32.h>
26
#endif
27

28
#if defined(BOTAN_HAS_ENTROPY_SRC_GETENTROPY)
29
   #include <botan/internal/getentropy.h>
30
#endif
31

32
namespace Botan {
33

34
namespace {
35

36
#if defined(BOTAN_HAS_SYSTEM_RNG)
37

38
class System_RNG_EntropySource final : public Entropy_Source {
4✔
39
   public:
40
      size_t poll(RandomNumberGenerator& rng) override {
5✔
41
         const size_t poll_bits = BOTAN_RNG_RESEED_POLL_BITS;
5✔
42
         rng.reseed_from_rng(system_rng(), poll_bits);
5✔
43
         return poll_bits;
5✔
44
      }
45

46
      std::string name() const override { return "system_rng"; }
8✔
47
};
48

49
#endif
50

51
#if defined(BOTAN_HAS_PROCESSOR_RNG)
52

53
class Processor_RNG_EntropySource final : public Entropy_Source {
4✔
54
   public:
55
      size_t poll(RandomNumberGenerator& rng) override {
6✔
56
         /*
57
         * Intel's documentation for RDRAND at
58
         * https://software.intel.com/en-us/articles/intel-digital-random-number-generator-drng-software-implementation-guide
59
         * claims that software can guarantee a reseed event by polling enough data:
60
         * "There is an upper bound of 511 samples per seed in the implementation
61
         * where samples are 128 bits in size and can provide two 64-bit random
62
         * numbers each."
63
         *
64
         * By requesting 65536 bits we are asking for 512 samples and thus are assured
65
         * that at some point in producing the output, at least one reseed of the
66
         * internal state will occur.
67
         *
68
         * The reseeding conditions of the POWER and ARM processor RNGs are not known
69
         * but probably work in a somewhat similar manner. The exact amount requested
70
         * may be tweaked if and when such conditions become publically known.
71
         */
72
         const size_t poll_bits = 65536;
6✔
73
         rng.reseed_from_rng(m_hwrng, poll_bits);
6✔
74
         // Avoid trusting a black box, don't count this as contributing entropy:
75
         return 0;
6✔
76
      }
77

78
      std::string name() const override { return m_hwrng.name(); }
18✔
79

80
   private:
81
      Processor_RNG m_hwrng;
82
};
83

84
#endif
85

86
}  // namespace
87

88
std::unique_ptr<Entropy_Source> Entropy_Source::create(std::string_view name) {
20✔
89
#if defined(BOTAN_HAS_SYSTEM_RNG)
90
   if(name == "system_rng") {
24✔
91
      return std::make_unique<System_RNG_EntropySource>();
4✔
92
   }
93
#endif
94

95
#if defined(BOTAN_HAS_PROCESSOR_RNG)
96
   if(name == "hwrng") {
16✔
97
      if(Processor_RNG::available()) {
4✔
98
         return std::make_unique<Processor_RNG_EntropySource>();
4✔
99
      }
100
   }
101
#endif
102

103
#if defined(BOTAN_HAS_ENTROPY_SRC_RDSEED)
104
   if(name == "rdseed") {
12✔
105
      return std::make_unique<Intel_Rdseed>();
4✔
106
   }
107
#endif
108

109
#if defined(BOTAN_HAS_ENTROPY_SRC_GETENTROPY)
110
   if(name == "getentropy") {
8✔
111
      return std::make_unique<Getentropy>();
4✔
112
   }
113
#endif
114

115
#if defined(BOTAN_HAS_ENTROPY_SRC_WIN32)
116
   if(name == "system_stats") {
117
      return std::make_unique<Win32_EntropySource>();
118
   }
119
#endif
120

121
   BOTAN_UNUSED(name);
4✔
122
   return nullptr;
4✔
123
}
124

125
void Entropy_Sources::add_source(std::unique_ptr<Entropy_Source> src) {
26✔
126
   if(src) {
26✔
127
      m_srcs.push_back(std::move(src));
22✔
128
   }
129
}
26✔
130

131
std::vector<std::string> Entropy_Sources::enabled_sources() const {
3✔
132
   std::vector<std::string> sources;
3✔
133
   sources.reserve(m_srcs.size());
3✔
134
   for(const auto& src : m_srcs) {
15✔
135
      sources.push_back(src->name());
24✔
136
   }
137
   return sources;
3✔
138
}
×
139

140
size_t Entropy_Sources::poll(RandomNumberGenerator& rng, size_t poll_bits, std::chrono::milliseconds timeout) {
9✔
141
   typedef std::chrono::system_clock clock;
9✔
142

143
   auto deadline = clock::now() + timeout;
9✔
144

145
   size_t bits_collected = 0;
9✔
146

147
   for(auto& src : m_srcs) {
13✔
148
      bits_collected += src->poll(rng);
9✔
149

150
      if(bits_collected >= poll_bits || clock::now() > deadline)
9✔
151
         break;
152
   }
153

154
   return bits_collected;
5✔
155
}
156

157
size_t Entropy_Sources::poll_just(RandomNumberGenerator& rng, std::string_view the_src) {
20✔
158
   for(auto& src : m_srcs) {
50✔
159
      if(src->name() == the_src) {
80✔
160
         return src->poll(rng);
20✔
161
      }
162
   }
163

164
   return 0;
165
}
166

167
Entropy_Sources::Entropy_Sources(const std::vector<std::string>& sources) {
4✔
168
   for(auto&& src_name : sources) {
24✔
169
      add_source(Entropy_Source::create(src_name));
40✔
170
   }
171
}
4✔
172

173
Entropy_Sources& Entropy_Sources::global_sources() {
9✔
174
   static Entropy_Sources global_entropy_sources(BOTAN_ENTROPY_DEFAULT_SOURCES);
29✔
175

176
   return global_entropy_sources;
9✔
177
}
178

179
}  // namespace Botan
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