Botan 3.13.0
Crypto and TLS for C&
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/api.h>
12#include <chrono>
13#include <memory>
14#include <string>
15#include <string_view>
16#include <vector>
17
18namespace Botan {
19
21
22/**
23* Abstract interface to a source of entropy
24*/
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 a free-form string 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 * Any implementation of this function should be thread safe; it may be
46 * called concurrently in multiple threads if multiple stateful RNGs reseed
47 * across different threads.
48 *
49 * TODO(Botan4) make this member function const
50 */
51 virtual size_t poll(RandomNumberGenerator& rng) = 0;
52
53 /**
54 * Default constructor
55 */
56 Entropy_Source() = default;
57 Entropy_Source(const Entropy_Source& other) = delete;
58 Entropy_Source(Entropy_Source&& other) = delete;
59 Entropy_Source& operator=(const Entropy_Source& other) = delete;
61
62 virtual ~Entropy_Source() = default;
63};
64
65/**
66* A collection of entropy sources which can be polled together
67*/
69 public:
70 /**
71 * Access the process-wide set of entropy sources used by default
72 * @warning This object is not synchronized. For general usage (eg polling)
73 * this is fine. However if you use global_sources().add_source() concurrently
74 * with a poll, likely a race leading to memory corruption will occur; only
75 * add a new entropy source at the start of main before RNG objects are created.
76 */
78
79 /**
80 * Add an entropy source to this collection
81 * @param src the source to add
82 */
83 void add_source(std::unique_ptr<Entropy_Source> src);
84
85 /**
86 * List the entropy sources in this collection
87 * @return the names of the enabled sources
88 */
89 std::vector<std::string> enabled_sources() const;
90
91 /**
92 * Poll all sources to collect @p bits of entropy with a @p timeout.
93 * Entropy collection is aborted as soon as either the requested number of
94 * bits are obtained or the timeout runs out. If the target system does not
95 * provide a clock, the timeout is ignored.
96 *
97 * Note that the timeout is cooperative. If the poll() method of an entropy
98 * source blocks forever, this invocation will potentially also block.
99 *
100 * @returns the number of bits collected from the entropy sources
101 *
102 * TODO(Botan4) remove this variant, and the <chrono> include above
103 */
104 BOTAN_DEPRECATED("Use version without a timeout argument")
105 size_t poll(RandomNumberGenerator& rng, size_t bits, std::chrono::milliseconds timeout);
106
107 /**
108 * Poll all sources to collect @p bits of entropy. Entropy collection is
109 * aborted as soon as the requested number of bits are obtained or the
110 * timeout runs out.
111 *
112 * @returns the number of bits collected from the entropy sources
113 */
114 size_t poll(RandomNumberGenerator& rng, size_t bits);
115
116 /**
117 * Poll just a single named source. Ordinally only used for testing
118 */
119 size_t poll_just(RandomNumberGenerator& rng, std::string_view src);
120
121 /**
122 * Create an empty collection of entropy sources
123 */
124 Entropy_Sources() = default;
125 /**
126 * Create a collection containing the named entropy sources
127 * @param sources the names of the sources to enable
128 */
129 explicit Entropy_Sources(const std::vector<std::string>& sources);
130
131 Entropy_Sources(const Entropy_Sources& other) = delete;
135 ~Entropy_Sources() = default;
136
137 private:
138 std::vector<std::unique_ptr<Entropy_Source>> m_srcs;
139};
140
141} // namespace Botan
142
143#endif
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
#define BOTAN_DEPRECATED(msg)
Definition api.h:73
virtual std::string name() const =0
static std::unique_ptr< Entropy_Source > create(std::string_view type)
Entropy_Source(const Entropy_Source &other)=delete
virtual size_t poll(RandomNumberGenerator &rng)=0
Entropy_Source(Entropy_Source &&other)=delete
virtual ~Entropy_Source()=default
Entropy_Source & operator=(Entropy_Source &&other)=delete
Entropy_Source & operator=(const Entropy_Source &other)=delete
static Entropy_Sources & global_sources()
size_t poll_just(RandomNumberGenerator &rng, std::string_view src)
Entropy_Sources(Entropy_Sources &&other)=delete
Entropy_Sources & operator=(Entropy_Sources &&other)=delete
void add_source(std::unique_ptr< Entropy_Source > src)
size_t poll(RandomNumberGenerator &rng, size_t bits, std::chrono::milliseconds timeout)
Entropy_Sources(const Entropy_Sources &other)=delete
Entropy_Sources & operator=(const Entropy_Sources &other)=delete
std::vector< std::string > enabled_sources() const