aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/entropy/entropy_srcs.cpp
blob: 81076b0e9d9784a85556aa00464e765089d0271d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
* Entropy Source Polling
* (C) 2008-2010,2015 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include <botan/entropy_src.h>
#include <botan/rng.h>

#if defined(BOTAN_HAS_ENTROPY_SRC_HIGH_RESOLUTION_TIMER)
  #include <botan/internal/hres_timer.h>
#endif

#if defined(BOTAN_HAS_ENTROPY_SRC_RDRAND)
  #include <botan/internal/rdrand.h>
#endif

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

#if defined(BOTAN_HAS_ENTROPY_SRC_DEV_RANDOM)
  #include <botan/internal/dev_random.h>
#endif

#if defined(BOTAN_HAS_ENTROPY_SRC_EGD)
  #include <botan/internal/es_egd.h>
#endif

#if defined(BOTAN_HAS_ENTROPY_SRC_UNIX_PROCESS_RUNNER)
  #include <botan/internal/unix_procs.h>
#endif

#if defined(BOTAN_HAS_ENTROPY_SRC_BEOS)
  #include <botan/internal/es_beos.h>
#endif

#if defined(BOTAN_HAS_ENTROPY_SRC_CAPI)
  #include <botan/internal/es_capi.h>
#endif

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

#if defined(BOTAN_HAS_ENTROPY_SRC_PROC_WALKER)
  #include <botan/internal/proc_walk.h>
#endif

#if defined(BOTAN_HAS_ENTROPY_SRC_DARWIN_SECRANDOM)
  #include <botan/internal/darwin_secrandom.h>
#endif

namespace Botan {

std::unique_ptr<Entropy_Source> Entropy_Source::create(const std::string& name)
   {
   if(name == "timestamp")
      {
#if defined(BOTAN_HAS_ENTROPY_SRC_HIGH_RESOLUTION_TIMER)
   return std::unique_ptr<Entropy_Source>(new High_Resolution_Timestamp);
#endif
      }

   if(name == "rdrand")
      {
#if defined(BOTAN_HAS_ENTROPY_SRC_RDRAND)
      return std::unique_ptr<Entropy_Source>(new Intel_Rdrand);
#endif
      }
      
   if(name == "rdseed")
      {
#if defined(BOTAN_HAS_ENTROPY_SRC_RDSEED)
      return std::unique_ptr<Entropy_Source>(new Intel_Rdseed);
#endif
      }

   if(name == "proc_info")
      {
#if defined(BOTAN_HAS_ENTROPY_SRC_UNIX_PROCESS_RUNNER)
   return std::unique_ptr<Entropy_Source>(new UnixProcessInfo_EntropySource);
#endif
      }

   if(name == "darwin_secrandom")
      {
#if defined(BOTAN_HAS_ENTROPY_SRC_DARWIN_SECRANDOM)
      return std::unique_ptr<Entropy_Source>(new Darwin_SecRandom);
#endif
      }

   if(name == "dev_random")
      {
#if defined(BOTAN_HAS_ENTROPY_SRC_DEV_RANDOM)
      return std::unique_ptr<Entropy_Source>(new Device_EntropySource(BOTAN_SYSTEM_RNG_POLL_DEVICES));
      }

   if(name == "win32_cryptoapi")
      {
#elif defined(BOTAN_HAS_ENTROPY_SRC_CAPI)
      return std::unique_ptr<Entropy_Source>(new Win32_CAPI_EntropySource);
#endif
      }

   if(name == "proc_walk")
      {
#if defined(BOTAN_HAS_ENTROPY_SRC_PROC_WALKER)
      return std::unique_ptr<Entropy_Source>(new ProcWalking_EntropySource("/proc"));
#endif
      }

   if(name == "system_stats")
      {
#if defined(BOTAN_HAS_ENTROPY_SRC_WIN32)
      return std::unique_ptr<Entropy_Source>(new Win32_EntropySource);
#elif defined(BOTAN_HAS_ENTROPY_SRC_BEOS)
   return std::unique_ptr<Entropy_Source>(new BeOS_EntropySource);
#endif
      }

   if(name == "unix_procs")
      {
#if defined(BOTAN_HAS_ENTROPY_SRC_UNIX_PROCESS_RUNNER)
      return std::unique_ptr<Entropy_Source>(new Unix_EntropySource(BOTAN_ENTROPY_SAFE_PATHS));
#endif
      }

   if(name == "egd")
      {
#if defined(BOTAN_HAS_ENTROPY_SRC_EGD)
      return std::unique_ptr<Entropy_Source>(new EGD_EntropySource(BOTAN_ENTROPY_EGD_PATHS));
#endif
      }

   return std::unique_ptr<Entropy_Source>();
   }

void Entropy_Sources::add_source(std::unique_ptr<Entropy_Source> src)
   {
   if(src.get())
      {
      m_srcs.push_back(src.release());
      }
   }

std::vector<std::string> Entropy_Sources::enabled_sources() const
   {
   std::vector<std::string> sources;
   for(size_t i = 0; i != m_srcs.size(); ++i)
      {
      sources.push_back(m_srcs[i]->name());
      }
   return sources;
   }

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

   auto deadline = clock::now() + timeout;

   double bits_collected = 0;

   Entropy_Accumulator accum([&](const byte in[], size_t in_len, double entropy_estimate) {
      rng.add_entropy(in, in_len);
      bits_collected += entropy_estimate;
      return (bits_collected >= poll_bits || clock::now() > deadline);
      });

   for(size_t i = 0; i != m_srcs.size(); ++i)
      {
      m_srcs[i]->poll(accum);
      if(accum.polling_goal_achieved())
         break;
      }

   return static_cast<size_t>(bits_collected);
   }

bool Entropy_Sources::poll_just(Entropy_Accumulator& accum, const std::string& the_src)
   {
   for(size_t i = 0; i != m_srcs.size(); ++i)
      {
      if(m_srcs[i]->name() == the_src)
         {
         m_srcs[i]->poll(accum);
         return true;
         }
      }

   return false;
   }

Entropy_Sources::Entropy_Sources(const std::vector<std::string>& sources)
   {
   for(auto&& src_name : sources)
      {
      add_source(Entropy_Source::create(src_name));
      }
   }

Entropy_Sources::~Entropy_Sources()
   {
   for(size_t i = 0; i != m_srcs.size(); ++i)
      {
      delete m_srcs[i];
      m_srcs[i] = nullptr;
      }
   m_srcs.clear();
   }

Entropy_Sources& Entropy_Sources::global_sources()
   {
   static Entropy_Sources global_entropy_sources(BOTAN_ENTROPY_DEFAULT_SOURCES);

   return global_entropy_sources;
   }

}