1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
/*
* EntropySource
* (C) 2008-2009,2014 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#ifndef BOTAN_ENTROPY_SOURCE_BASE_H__
#define BOTAN_ENTROPY_SOURCE_BASE_H__
#include <botan/secmem.h>
#include <string>
#include <functional>
namespace Botan {
/**
* Class used to accumulate the poll results of EntropySources
*/
class BOTAN_DLL Entropy_Accumulator
{
public:
/**
* Initialize an Entropy_Accumulator
* @param goal is how many bits we would like to collect
*/
Entropy_Accumulator(std::function<bool (const byte[], size_t, double)> accum) :
m_accum_fn(accum), m_done(false) {}
virtual ~Entropy_Accumulator() {}
/**
* Get a cached I/O buffer (purely for minimizing allocation
* overhead to polls)
*
* @param size requested size for the I/O buffer
* @return cached I/O buffer for repeated polls
*/
secure_vector<byte>& get_io_buffer(size_t size)
{
m_io_buffer.clear();
m_io_buffer.resize(size);
return m_io_buffer;
}
/**
* @return if our polling goal has been achieved
*/
bool polling_goal_achieved() const { return m_done; }
/**
* Add entropy to the accumulator
* @param bytes the input bytes
* @param length specifies how many bytes the input is
* @param entropy_bits_per_byte is a best guess at how much
* entropy per byte is in this input
*/
void add(const void* bytes, size_t length, double entropy_bits_per_byte)
{
m_done = m_accum_fn(reinterpret_cast<const byte*>(bytes),
length, entropy_bits_per_byte * length);
}
/**
* Add entropy to the accumulator
* @param v is some value
* @param entropy_bits_per_byte is a best guess at how much
* entropy per byte is in this input
*/
template<typename T>
void add(const T& v, double entropy_bits_per_byte)
{
add(&v, sizeof(T), entropy_bits_per_byte);
}
private:
std::function<bool (const byte[], size_t, double)> m_accum_fn;
bool m_done;
secure_vector<byte> m_io_buffer;
};
/**
* Abstract interface to a source of entropy
*/
class BOTAN_DLL EntropySource
{
public:
/**
* @return name identifying this entropy source
*/
virtual std::string name() const = 0;
/**
* Perform an entropy gathering poll
* @param accum is an accumulator object that will be given entropy
*/
virtual void poll(Entropy_Accumulator& accum) = 0;
virtual ~EntropySource() {}
};
}
#endif
|