Coverage Report

Created: 2025-04-11 06:45

/src/botan/build/include/public/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/api.h>
12
#include <chrono>
13
#include <memory>
14
#include <string>
15
#include <string_view>
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
      /**
64
      * Poll all sources to collect @p bits of entropy with a @p timeout.
65
      * Entropy collection is aborted as soon as either the requested number of
66
      * bits are obtained or the timeout runs out. If the target system does not
67
      * provide a clock, the timeout is ignored.
68
      *
69
      * Note that the timeout is cooperative. If the poll() method of an entropy
70
      * source blocks forever, this invocation will potentially also block.
71
      *
72
      * @returns the number of bits collected from the entropy sources
73
      */
74
      size_t poll(RandomNumberGenerator& rng, size_t bits, std::chrono::milliseconds timeout);
75
76
      /**
77
      * Poll just a single named source. Ordinally only used for testing
78
      */
79
      size_t poll_just(RandomNumberGenerator& rng, std::string_view src);
80
81
      Entropy_Sources() = default;
82
      explicit Entropy_Sources(const std::vector<std::string>& sources);
83
84
      Entropy_Sources(const Entropy_Sources& other) = delete;
85
      Entropy_Sources(Entropy_Sources&& other) = delete;
86
      Entropy_Sources& operator=(const Entropy_Sources& other) = delete;
87
88
   private:
89
      std::vector<std::unique_ptr<Entropy_Source>> m_srcs;
90
};
91
92
}  // namespace Botan
93
94
#endif