Botan  1.10.9
Public Member Functions | Static Public Member Functions | List of all members
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

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.

References Botan::BlockCipher::block_size(), Botan::Buffered_Computation::output_length(), Botan::MemoryRegion< T >::resize(), and Botan::SymmetricAlgorithm::valid_keylength().

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  }
void resize(size_t n)
Definition: secmem.h:211
bool valid_keylength(size_t length) const
Definition: sym_algo.h:51
virtual size_t block_size() const =0
virtual size_t output_length() const =0
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

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.

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

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  }
SecureVector< byte > process(const byte in[], size_t length)
Definition: buf_comp.h:101
void xor_buf(byte out[], const byte in[], size_t length)
Definition: xor_buf.h:21
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  }
void Botan::Randpool::clear ( )
virtual

Clear all internally held values of this RNG.

Implements Botan::RandomNumberGenerator.

Definition at line 148 of file randpool.cpp.

References Botan::Algorithm::clear(), and Botan::zeroise().

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

Referenced by randomize().

25 { return seeded; }
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  }
std::string Botan::Randpool::name ( ) const
virtual

Return the name of this object

Implements Botan::RandomNumberGenerator.

Definition at line 161 of file randpool.cpp.

References Botan::Algorithm::name(), and Botan::MessageAuthenticationCode::name().

Referenced by randomize().

162  {
163  return "Randpool(" + cipher->name() + "," + mac->name() + ")";
164  }
virtual std::string name() const =0
virtual std::string name() const =0
byte Botan::RandomNumberGenerator::next_byte ( )
inherited

Return a random byte

Returns
random byte

Definition at line 19 of file rng.cpp.

References Botan::RandomNumberGenerator::randomize().

Referenced by Botan::random_prime().

20  {
21  byte out;
22  this->randomize(&out, 1);
23  return out;
24  }
virtual void randomize(byte output[], size_t length)=0
unsigned char byte
Definition: types.h:22
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.

References 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().

41  {
42  SecureVector<byte> output(bytes);
43  randomize(&output[0], output.size());
44  return output;
45  }
virtual void randomize(byte output[], size_t length)=0
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.

References Botan::copy_mem(), is_seeded(), name(), and Botan::MemoryRegion< T >::size().

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
size_t size() const
Definition: secmem.h:29
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:22
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.

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

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  }
void final(byte out[])
Definition: buf_comp.h:80
void xor_buf(byte out[], const byte in[], size_t length)
Definition: xor_buf.h:21

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