/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/rng.h> |
12 | | #include <botan/secmem.h> |
13 | | #include <chrono> |
14 | | #include <memory> |
15 | | #include <string> |
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 | | public: |
27 | | /** |
28 | | * Return a new entropy source of a particular type, or null |
29 | | * Each entropy source may require substantial resources (eg, a file handle |
30 | | * or socket instance), so try to share them among multiple RNGs, or just |
31 | | * use the preconfigured global list accessed by Entropy_Sources::global_sources() |
32 | | */ |
33 | | static std::unique_ptr<Entropy_Source> create(std::string_view type); |
34 | | |
35 | | /** |
36 | | * @return name identifying this entropy source |
37 | | */ |
38 | | virtual std::string name() const = 0; |
39 | | |
40 | | /** |
41 | | * Perform an entropy gathering poll |
42 | | * @param rng will be provided with entropy via calls to add_entropy |
43 | | * @return conservative estimate of actual entropy added to rng during poll |
44 | | */ |
45 | | virtual size_t poll(RandomNumberGenerator& rng) = 0; |
46 | | |
47 | 0 | Entropy_Source() = default; |
48 | | Entropy_Source(const Entropy_Source& other) = delete; |
49 | | Entropy_Source(Entropy_Source&& other) = delete; |
50 | | Entropy_Source& operator=(const Entropy_Source& other) = delete; |
51 | | |
52 | 0 | virtual ~Entropy_Source() = default; |
53 | | }; |
54 | | |
55 | | class BOTAN_PUBLIC_API(2, 0) Entropy_Sources final { |
56 | | public: |
57 | | static Entropy_Sources& global_sources(); |
58 | | |
59 | | void add_source(std::unique_ptr<Entropy_Source> src); |
60 | | |
61 | | std::vector<std::string> enabled_sources() const; |
62 | | |
63 | | size_t poll(RandomNumberGenerator& rng, size_t bits, std::chrono::milliseconds timeout); |
64 | | |
65 | | /** |
66 | | * Poll just a single named source. Ordinally only used for testing |
67 | | */ |
68 | | size_t poll_just(RandomNumberGenerator& rng, std::string_view src); |
69 | | |
70 | | Entropy_Sources() = default; |
71 | | explicit Entropy_Sources(const std::vector<std::string>& sources); |
72 | | |
73 | | Entropy_Sources(const Entropy_Sources& other) = delete; |
74 | | Entropy_Sources(Entropy_Sources&& other) = delete; |
75 | | Entropy_Sources& operator=(const Entropy_Sources& other) = delete; |
76 | | |
77 | | private: |
78 | | std::vector<std::unique_ptr<Entropy_Source>> m_srcs; |
79 | | }; |
80 | | |
81 | | } // namespace Botan |
82 | | |
83 | | #endif |