Botan 1.10.17

entropy_src.h
Go to the documentation of this file.
1 /*
2 * EntropySource
3 * (C) 2008-2009 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #ifndef BOTAN_ENTROPY_SOURCE_BASE_H__
9 #define BOTAN_ENTROPY_SOURCE_BASE_H__
10
11 #include <botan/buf_comp.h>
12 #include <string>
13
14 namespace Botan {
15
16 /**
17 * Class used to accumulate the poll results of EntropySources
18 */
19 class BOTAN_DLL Entropy_Accumulator
20 {
21 public:
22 /**
23 * Initialize an Entropy_Accumulator
24 * @param goal is how many bits we would like to collect
25 */
26 Entropy_Accumulator(size_t goal) :
27 entropy_goal(goal), collected_bits(0) {}
28
29 virtual ~Entropy_Accumulator() {}
30
31 /**
32 * Get a cached I/O buffer (purely for minimizing allocation
33 * overhead to polls)
34 *
35 * @param size requested size for the I/O buffer
36 * @return cached I/O buffer for repeated polls
37 */
38 MemoryRegion<byte>& get_io_buffer(size_t size)
39 { io_buffer.resize(size); return io_buffer; }
40
41 /**
42 * @return number of bits collected so far
43 */
44 size_t bits_collected() const
45 { return static_cast<size_t>(collected_bits); }
46
47 /**
48 * @return if our polling goal has been achieved
49 */
50 bool polling_goal_achieved() const
51 { return (collected_bits >= entropy_goal); }
52
53 /**
54 * @return how many bits we need to reach our polling goal
55 */
56 size_t desired_remaining_bits() const
57 {
58 if(collected_bits >= entropy_goal)
59 return 0;
60 return static_cast<size_t>(entropy_goal - collected_bits);
61 }
62
63 /**
64 * Add entropy to the accumulator
65 * @param bytes the input bytes
66 * @param length specifies how many bytes the input is
67 * @param entropy_bits_per_byte is a best guess at how much
68 * entropy per byte is in this input
69 */
70 void add(const void* bytes, size_t length, double entropy_bits_per_byte)
71 {
72 add_bytes(reinterpret_cast<const byte*>(bytes), length);
73 collected_bits += entropy_bits_per_byte * length;
74 }
75
76 /**
77 * Add entropy to the accumulator
78 * @param v is some value
79 * @param entropy_bits_per_byte is a best guess at how much
80 * entropy per byte is in this input
81 */
82 template<typename T>
83 void add(const T& v, double entropy_bits_per_byte)
84 {
85 add(&v, sizeof(T), entropy_bits_per_byte);
86 }
87 private:
88 virtual void add_bytes(const byte bytes[], size_t length) = 0;
89
90 SecureVector<byte> io_buffer;
91 size_t entropy_goal;
92 double collected_bits;
93 };
94
95 /**
96 * Entropy accumulator that puts the input into a Buffered_Computation
97 */
98 class BOTAN_DLL Entropy_Accumulator_BufferedComputation :
99 public Entropy_Accumulator
100 {
101 public:
102 /**
103 * @param sink the hash or MAC we are feeding the poll data into
104 * @param goal is how many bits we want to collect in this poll
105 */
106 Entropy_Accumulator_BufferedComputation(Buffered_Computation& sink,
107 size_t goal) :
108 Entropy_Accumulator(goal), entropy_sink(sink) {}
109
110 private:
111 virtual void add_bytes(const byte bytes[], size_t length)
112 {
113 entropy_sink.update(bytes, length);
114 }
115
116 Buffered_Computation& entropy_sink;
117 };
118
119 /**
120 * Abstract interface to a source of (hopefully unpredictable) system entropy
121 */
122 class BOTAN_DLL EntropySource
123 {
124 public:
125 /**
126 * @return name identifying this entropy source
127 */
128 virtual std::string name() const = 0;
129
130 /**
131 * Perform an entropy gathering poll
132 * @param accum is an accumulator object that will be given entropy
133 */
134 virtual void poll(Entropy_Accumulator& accum) = 0;
135
136 virtual ~EntropySource() {}
137 };
138
139 }
140
141 #endif
Botan::MemoryRegion::resize
void resize(size_t n)
Definition: secmem.h:217
Botan::Entropy_Accumulator::~Entropy_Accumulator
virtual ~Entropy_Accumulator()
Definition: entropy_src.h:29
Botan::Entropy_Accumulator::desired_remaining_bits
size_t desired_remaining_bits() const
Definition: entropy_src.h:56
Botan::Entropy_Accumulator::get_io_buffer
MemoryRegion< byte > & get_io_buffer(size_t size)
Definition: entropy_src.h:38
Botan::Entropy_Accumulator_BufferedComputation
Definition: entropy_src.h:98
Botan::Entropy_Accumulator::add
void add(const void *bytes, size_t length, double entropy_bits_per_byte)
Definition: entropy_src.h:70
Botan::Buffered_Computation
Definition: buf_comp.h:20
Botan::EntropySource
Definition: entropy_src.h:122
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::MemoryRegion< byte >
Botan
Definition: algo_base.h:14
Botan::Entropy_Accumulator::Entropy_Accumulator
Entropy_Accumulator(size_t goal)
Definition: entropy_src.h:26
Botan::Entropy_Accumulator::add
void add(const T &v, double entropy_bits_per_byte)
Definition: entropy_src.h:83
Botan::Entropy_Accumulator::polling_goal_achieved
bool polling_goal_achieved() const
Definition: entropy_src.h:50
Botan::Entropy_Accumulator_BufferedComputation::Entropy_Accumulator_BufferedComputation
Entropy_Accumulator_BufferedComputation(Buffered_Computation &sink, size_t goal)
Definition: entropy_src.h:106
Botan::SecureVector< byte >
Botan::Entropy_Accumulator::bits_collected
size_t bits_collected() const
Definition: entropy_src.h:44
Botan::EntropySource::~EntropySource
virtual ~EntropySource()
Definition: entropy_src.h:136
Botan::Entropy_Accumulator
Definition: entropy_src.h:19