/src/botan/build/include/botan/entropy_src.h
Line | Count | Source (jump to first uncovered line) |
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_PUBLIC_API(2,0) 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 | 0 | Entropy_Source() = default; |
49 | | Entropy_Source(const Entropy_Source& other) = delete; |
50 | | Entropy_Source(Entropy_Source&& other) = delete; |
51 | | Entropy_Source& operator=(const Entropy_Source& other) = delete; |
52 | | |
53 | 0 | virtual ~Entropy_Source() = default; |
54 | | }; |
55 | | |
56 | | class BOTAN_PUBLIC_API(2,0) Entropy_Sources final |
57 | | { |
58 | | public: |
59 | | static Entropy_Sources& global_sources(); |
60 | | |
61 | | void add_source(std::unique_ptr<Entropy_Source> src); |
62 | | |
63 | | std::vector<std::string> enabled_sources() const; |
64 | | |
65 | | size_t poll(RandomNumberGenerator& rng, |
66 | | size_t bits, |
67 | | std::chrono::milliseconds timeout); |
68 | | |
69 | | /** |
70 | | * Poll just a single named source. Ordinally only used for testing |
71 | | */ |
72 | | size_t poll_just(RandomNumberGenerator& rng, const std::string& src); |
73 | | |
74 | | Entropy_Sources() = default; |
75 | | explicit Entropy_Sources(const std::vector<std::string>& sources); |
76 | | |
77 | | Entropy_Sources(const Entropy_Sources& other) = delete; |
78 | | Entropy_Sources(Entropy_Sources&& other) = delete; |
79 | | Entropy_Sources& operator=(const Entropy_Sources& other) = delete; |
80 | | |
81 | | private: |
82 | | std::vector<std::unique_ptr<Entropy_Source>> m_srcs; |
83 | | }; |
84 | | |
85 | | } |
86 | | |
87 | | #endif |