Botan  2.1.0
Crypto and TLS for C++11
entropy_src.h
Go to the documentation of this file.
1 /*
2 * EntropySource
3 * (C) 2008,2009,2014,2015,2016 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #ifndef BOTAN_ENTROPY_H__
9 #define BOTAN_ENTROPY_H__
10 
11 #include <botan/secmem.h>
12 #include <botan/rng.h>
13 #include <string>
14 #include <chrono>
15 #include <memory>
16 #include <vector>
17 
18 namespace Botan {
19 
20 class RandomNumberGenerator;
21 
22 /**
23 * Abstract interface to a source of entropy
24 */
25 class BOTAN_DLL Entropy_Source
26  {
27  public:
28  /**
29  * Return a new entropy source of a particular type, or null
30  * Each entropy source may require substantial resources (eg, a file handle
31  * or socket instance), so try to share them among multiple RNGs, or just
32  * use the preconfigured global list accessed by Entropy_Sources::global_sources()
33  */
34  static std::unique_ptr<Entropy_Source> create(const std::string& type);
35 
36  /**
37  * @return name identifying this entropy source
38  */
39  virtual std::string name() const = 0;
40 
41  /**
42  * Perform an entropy gathering poll
43  * @param rng will be provided with entropy via calls to add_entropy
44  * @return conservative estimate of actual entropy added to rng during poll
45  */
46  virtual size_t poll(RandomNumberGenerator& rng) = 0;
47 
48  virtual ~Entropy_Source() {}
49  };
50 
51 class BOTAN_DLL Entropy_Sources final
52  {
53  public:
54  static Entropy_Sources& global_sources();
55 
56  void add_source(std::unique_ptr<Entropy_Source> src);
57 
58  std::vector<std::string> enabled_sources() const;
59 
60  size_t poll(RandomNumberGenerator& rng,
61  size_t bits,
62  std::chrono::milliseconds timeout);
63 
64  /**
65  * Poll just a single named source. Ordinally only used for testing
66  */
67  size_t poll_just(RandomNumberGenerator& rng, const std::string& src);
68 
70  explicit Entropy_Sources(const std::vector<std::string>& sources);
71 
72  ~Entropy_Sources();
73  private:
74  std::vector<Entropy_Source*> m_srcs;
75  };
76 
77 }
78 
79 #endif
MechanismType type
Definition: alg_id.cpp:13
virtual ~Entropy_Source()
Definition: entropy_src.h:48