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
|
/*************************************************
* ANSI X9.31 RNG Source File *
* (C) 1999-2008 Jack Lloyd *
*************************************************/
#include <botan/x931_rng.h>
#include <botan/xor_buf.h>
#include <algorithm>
namespace Botan {
/*************************************************
* Generate a buffer of random bytes *
*************************************************/
void ANSI_X931_RNG::randomize(byte out[], u32bit length)
{
if(!is_seeded())
throw PRNG_Unseeded(name());
while(length)
{
if(position == R.size())
update_buffer();
const u32bit copied = std::min(length, R.size() - position);
copy_mem(out, R + position, copied);
out += copied;
length -= copied;
position += copied;
}
}
/*************************************************
* Refill the internal state *
*************************************************/
void ANSI_X931_RNG::update_buffer()
{
SecureVector<byte> DT(cipher->BLOCK_SIZE);
prng->randomize(DT, DT.size());
cipher->encrypt(DT);
xor_buf(R, V, DT, cipher->BLOCK_SIZE);
cipher->encrypt(R);
xor_buf(V, R, DT, cipher->BLOCK_SIZE);
cipher->encrypt(V);
position = 0;
}
/*************************************************
* Reseed the internal state *
*************************************************/
void ANSI_X931_RNG::reseed(u32bit poll_bits)
{
prng->reseed(poll_bits);
if(prng->is_seeded())
{
SecureVector<byte> key(cipher->MAXIMUM_KEYLENGTH);
prng->randomize(key, key.size());
cipher->set_key(key, key.size());
if(V.size() != cipher->BLOCK_SIZE)
V.create(cipher->BLOCK_SIZE);
prng->randomize(V, V.size());
update_buffer();
}
}
/*************************************************
* Add a entropy source to the underlying PRNG *
*************************************************/
void ANSI_X931_RNG::add_entropy_source(EntropySource* src)
{
prng->add_entropy_source(src);
}
/*************************************************
* Add some entropy to the underlying PRNG *
*************************************************/
void ANSI_X931_RNG::add_entropy(const byte input[], u32bit length)
{
prng->add_entropy(input, length);
}
/*************************************************
* Check if the the PRNG is seeded *
*************************************************/
bool ANSI_X931_RNG::is_seeded() const
{
return V.has_items();
}
/*************************************************
* Clear memory of sensitive data *
*************************************************/
void ANSI_X931_RNG::clear() throw()
{
cipher->clear();
prng->clear();
R.clear();
V.destroy();
position = 0;
}
/*************************************************
* Return the name of this type *
*************************************************/
std::string ANSI_X931_RNG::name() const
{
return "X9.31(" + cipher->name() + ")";
}
/*************************************************
* ANSI X931 RNG Constructor *
*************************************************/
ANSI_X931_RNG::ANSI_X931_RNG(BlockCipher* cipher_in,
RandomNumberGenerator* prng_in)
{
if(!prng_in || !cipher_in)
throw Invalid_Argument("ANSI_X931_RNG constructor: NULL arguments");
cipher = cipher_in;
prng = prng_in;
R.create(cipher->BLOCK_SIZE);
position = 0;
}
/*************************************************
* ANSI X931 RNG Destructor *
*************************************************/
ANSI_X931_RNG::~ANSI_X931_RNG()
{
delete cipher;
delete prng;
}
}
|