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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
/*************************************************
* Randpool Source File *
* (C) 1999-2007 Jack Lloyd *
*************************************************/
#include <botan/randpool.h>
#include <botan/lookup.h>
#include <botan/loadstor.h>
#include <botan/bit_ops.h>
#include <botan/util.h>
#include <algorithm>
namespace Botan {
namespace {
/*************************************************
* PRF based on a MAC *
*************************************************/
enum RANDPOOL_PRF_TAG {
USER_INPUT = 0,
CIPHER_KEY = 1,
MAC_KEY = 2,
GEN_OUTPUT = 3
};
SecureVector<byte> randpool_prf(MessageAuthenticationCode* mac,
RANDPOOL_PRF_TAG tag,
const byte in[], u32bit length)
{
mac->update(static_cast<byte>(tag));
mac->update(in, length);
return mac->final();
}
}
/*************************************************
* Generate a buffer of random bytes *
*************************************************/
void Randpool::randomize(byte out[], u32bit length)
{
if(!is_seeded())
throw PRNG_Unseeded(name());
update_buffer();
while(length)
{
const u32bit copied = std::min(length, buffer.size());
copy_mem(out, buffer.begin(), copied);
out += copied;
length -= copied;
update_buffer();
}
}
/*************************************************
* Refill the output buffer *
*************************************************/
void Randpool::update_buffer()
{
const u64bit timestamp = system_time();
for(u32bit j = 0; j != counter.size(); ++j)
if(++counter[j])
break;
store_be(timestamp, counter + 4);
SecureVector<byte> mac_val = randpool_prf(mac, GEN_OUTPUT,
counter, counter.size());
for(u32bit j = 0; j != mac_val.size(); ++j)
buffer[j % buffer.size()] ^= mac_val[j];
cipher->encrypt(buffer);
if(counter[0] % ITERATIONS_BEFORE_RESEED == 0)
{
mix_pool();
update_buffer();
}
}
/*************************************************
* Mix the entropy pool *
*************************************************/
void Randpool::mix_pool()
{
const u32bit BLOCK_SIZE = cipher->BLOCK_SIZE;
mac->set_key(randpool_prf(mac, MAC_KEY, pool, pool.size()));
cipher->set_key(randpool_prf(mac, CIPHER_KEY, pool, pool.size()));
xor_buf(pool, buffer, BLOCK_SIZE);
cipher->encrypt(pool);
for(u32bit j = 1; j != POOL_BLOCKS; ++j)
{
const byte* previous_block = pool + BLOCK_SIZE*(j-1);
byte* this_block = pool + BLOCK_SIZE*j;
xor_buf(this_block, previous_block, BLOCK_SIZE);
cipher->encrypt(this_block);
}
}
/*************************************************
* Add entropy to the internal state *
*************************************************/
void Randpool::add_randomness(const byte data[], u32bit length)
{
u32bit this_entropy = entropy_estimate(data, length);
entropy += std::min(this_entropy, 8*mac->OUTPUT_LENGTH);
entropy = std::min(entropy, 8 * pool.size());
SecureVector<byte> mac_val = randpool_prf(mac, USER_INPUT, data, length);
xor_buf(pool, mac_val, mac_val.size());
mix_pool();
}
/*************************************************
* Check if the the pool is seeded *
*************************************************/
bool Randpool::is_seeded() const
{
return (entropy >= 256);
}
/*************************************************
* Clear memory of sensitive data *
*************************************************/
void Randpool::clear() throw()
{
cipher->clear();
mac->clear();
pool.clear();
buffer.clear();
counter.clear();
entropy = 0;
}
/*************************************************
* Return the name of this type *
*************************************************/
std::string Randpool::name() const
{
return "Randpool(" + cipher->name() + "," + mac->name() + ")";
}
/*************************************************
* Randpool Constructor *
*************************************************/
Randpool::Randpool(const std::string& cipher_name,
const std::string& mac_name) :
ITERATIONS_BEFORE_RESEED(8), POOL_BLOCKS(32)
{
cipher = get_block_cipher(cipher_name);
mac = get_mac(mac_name);
const u32bit BLOCK_SIZE = cipher->BLOCK_SIZE;
const u32bit OUTPUT_LENGTH = mac->OUTPUT_LENGTH;
if(OUTPUT_LENGTH < BLOCK_SIZE ||
!cipher->valid_keylength(OUTPUT_LENGTH) ||
!mac->valid_keylength(OUTPUT_LENGTH))
{
delete cipher;
delete mac;
throw Internal_Error("Randpool: Invalid algorithm combination " +
cipher_name + "/" + mac_name);
}
buffer.create(BLOCK_SIZE);
pool.create(POOL_BLOCKS * BLOCK_SIZE);
counter.create(12);
entropy = 0;
mix_pool();
}
/*************************************************
* Randpool Destructor *
*************************************************/
Randpool::~Randpool()
{
delete cipher;
delete mac;
entropy = 0;
}
}
|