Botan 1.10.17
Botan::Randpool Class Reference

#include <randpool.h>

Inheritance diagram for Botan::Randpool:
Botan::RandomNumberGenerator

Public Member Functions

void add_entropy (const byte input[], size_t length)
void add_entropy_source (EntropySource *es)
void clear ()
bool is_seeded () const
std::string name () const
byte next_byte ()
SecureVector< byterandom_vec (size_t bytes)
void randomize (byte[], size_t)
 Randpool (BlockCipher *cipher, MessageAuthenticationCode *mac, size_t pool_blocks=32, size_t iterations_before_reseed=128)
void reseed (size_t bits_to_collect)
 ~Randpool ()

Static Public Member Functions

static RandomNumberGeneratormake_rng ()

Detailed Description

Randpool

Definition at line 21 of file randpool.h.

Constructor & Destructor Documentation

◆ Randpool()

Botan::Randpool::Randpool ( BlockCipher * cipher,
MessageAuthenticationCode * mac,
size_t pool_blocks = 32,
size_t iterations_before_reseed = 128 )
Parameters
ciphera block cipher to use
maca message authentication code to use
pool_blockshow many cipher blocks to use for the pool
iterations_before_reseedhow many times we'll use the internal state to generate output before reseeding

Definition at line 169 of file randpool.cpp.

172 :
173 ITERATIONS_BEFORE_RESEED(iter_before_reseed),
174 POOL_BLOCKS(pool_blocks),
175 cipher(cipher_in),
176 mac(mac_in)
177 {
178 const size_t BLOCK_SIZE = cipher->block_size();
179 const size_t OUTPUT_LENGTH = mac->output_length();
180
181 if(OUTPUT_LENGTH < BLOCK_SIZE ||
182 !cipher->valid_keylength(OUTPUT_LENGTH) ||
183 !mac->valid_keylength(OUTPUT_LENGTH))
184 {
185 delete cipher;
186 delete mac;
187 throw Internal_Error("Randpool: Invalid algorithm combination");
188 }
189
190 buffer.resize(BLOCK_SIZE);
191 pool.resize(POOL_BLOCKS * BLOCK_SIZE);
192 counter.resize(12);
193 seeded = false;
194 }
Internal_Error(const std::string &err)
Definition exceptn.h:47

◆ ~Randpool()

Botan::Randpool::~Randpool ( )

Definition at line 199 of file randpool.cpp.

200 {
201 delete cipher;
202 delete mac;
203
204 std::for_each(entropy_sources.begin(), entropy_sources.end(),
205 del_fun<EntropySource>());
206 }

Member Function Documentation

◆ add_entropy()

void Botan::Randpool::add_entropy ( const byte in[],
size_t length )
virtual

Add entropy to this RNG.

Parameters
ina byte array containg the entropy to be added
lengththe length of the byte array in

Implements Botan::RandomNumberGenerator.

Definition at line 127 of file randpool.cpp.

128 {
129 SecureVector<byte> mac_val = mac->process(input, length);
130 xor_buf(pool, mac_val, mac_val.size());
131 mix_pool();
132
133 if(length)
134 seeded = true;
135 }
void xor_buf(byte out[], const byte in[], size_t length)
Definition xor_buf.h:21

References Botan::MemoryRegion< T >::size(), and Botan::xor_buf().

◆ add_entropy_source()

void Botan::Randpool::add_entropy_source ( EntropySource * source)
virtual

Add this entropy source to the RNG object

Parameters
sourcethe entropy source which will be retained and used by RNG

Implements Botan::RandomNumberGenerator.

Definition at line 140 of file randpool.cpp.

141 {
142 entropy_sources.push_back(src);
143 }

◆ clear()

void Botan::Randpool::clear ( )
virtual

Clear all internally held values of this RNG.

Implements Botan::RandomNumberGenerator.

Definition at line 148 of file randpool.cpp.

149 {
150 cipher->clear();
151 mac->clear();
152 zeroise(pool);
153 zeroise(buffer);
154 zeroise(counter);
155 seeded = false;
156 }
void zeroise(MemoryRegion< T > &vec)
Definition secmem.h:428

References Botan::zeroise().

◆ is_seeded()

bool Botan::Randpool::is_seeded ( ) const
inlinevirtual

Check whether this RNG is seeded.

Returns
true if this RNG was already seeded, false otherwise.

Reimplemented from Botan::RandomNumberGenerator.

Definition at line 25 of file randpool.h.

25{ return seeded; }

Referenced by randomize().

◆ make_rng()

RandomNumberGenerator * Botan::RandomNumberGenerator::make_rng ( )
staticinherited

Create a seeded and active RNG object for general application use

Definition at line 29 of file rng.cpp.

30 {
31#if defined(BOTAN_HAS_AUTO_SEEDING_RNG)
32 return new AutoSeeded_RNG;
33#endif
34
35 throw Algorithm_Not_Found("RandomNumberGenerator::make_rng - no RNG found");
36 }
Algorithm_Not_Found(const std::string &name)
Definition exceptn.h:111

References Botan::Algorithm_Not_Found::Algorithm_Not_Found(), and RandomNumberGenerator().

◆ name()

std::string Botan::Randpool::name ( ) const
virtual

Return the name of this object

Implements Botan::RandomNumberGenerator.

Definition at line 161 of file randpool.cpp.

162 {
163 return "Randpool(" + cipher->name() + "," + mac->name() + ")";
164 }

Referenced by randomize().

◆ next_byte()

byte Botan::RandomNumberGenerator::next_byte ( )
inherited

Return a random byte

Returns
random byte

Definition at line 19 of file rng.cpp.

20 {
21 byte out;
22 this->randomize(&out, 1);
23 return out;
24 }
virtual void randomize(byte output[], size_t length)=0

References randomize().

Referenced by Botan::random_prime().

◆ random_vec()

SecureVector< byte > Botan::RandomNumberGenerator::random_vec ( size_t bytes)
inlineinherited

Return a random vector

Parameters
bytesnumber of bytes in the result
Returns
randomized vector of length bytes

Definition at line 40 of file rng.h.

41 {
42 SecureVector<byte> output(bytes);
43 randomize(&output[0], output.size());
44 return output;
45 }

References randomize(), and Botan::MemoryRegion< T >::size().

Referenced by Botan::Client_Hello::Client_Hello(), Botan::Client_Key_Exchange::Client_Key_Exchange(), Botan::KeyPair::encryption_consistency_check(), Botan::generate_bcrypt(), Botan::generate_dsa_primes(), Botan::OctetString::OctetString(), Botan::Client_Key_Exchange::pre_master_secret(), Botan::BigInt::randomize(), Botan::Server_Hello::Server_Hello(), and Botan::KeyPair::signature_consistency_check().

◆ randomize()

void Botan::Randpool::randomize ( byte output[],
size_t length )
virtual

Randomize a byte array.

Parameters
outputthe byte array to hold the random output.
lengththe length of the byte array output.

Implements Botan::RandomNumberGenerator.

Definition at line 32 of file randpool.cpp.

33 {
34 if(!is_seeded())
35 throw PRNG_Unseeded(name());
36
37 update_buffer();
38 while(length)
39 {
40 const size_t copied = std::min<size_t>(length, buffer.size());
41 copy_mem(out, &buffer[0], copied);
42 out += copied;
43 length -= copied;
44 update_buffer();
45 }
46 }
std::string name() const
Definition randpool.cpp:161
bool is_seeded() const
Definition randpool.h:25
void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:22
PRNG_Unseeded(const std::string &algo)
Definition exceptn.h:91

References Botan::copy_mem(), is_seeded(), name(), and Botan::PRNG_Unseeded::PRNG_Unseeded().

◆ reseed()

void Botan::Randpool::reseed ( size_t bits_to_collect)
virtual

Seed this RNG using the entropy sources it contains.

Parameters
bits_to_collectis the number of bits of entropy to attempt to gather from the entropy sources

Implements Botan::RandomNumberGenerator.

Definition at line 100 of file randpool.cpp.

101 {
102 Entropy_Accumulator_BufferedComputation accum(*mac, poll_bits);
103
104 if(!entropy_sources.empty())
105 {
106 size_t poll_attempt = 0;
107
108 while(!accum.polling_goal_achieved() && poll_attempt < poll_bits)
109 {
110 entropy_sources[poll_attempt % entropy_sources.size()]->poll(accum);
111 ++poll_attempt;
112 }
113 }
114
115 SecureVector<byte> mac_val = mac->final();
116
117 xor_buf(pool, mac_val, mac_val.size());
118 mix_pool();
119
120 if(accum.bits_collected() >= poll_bits)
121 seeded = true;
122 }

References Botan::Entropy_Accumulator::bits_collected(), Botan::Entropy_Accumulator::polling_goal_achieved(), Botan::MemoryRegion< T >::size(), and Botan::xor_buf().


The documentation for this class was generated from the following files: