Botan  1.11.4
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 ()
 
secure_vector< 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::Algorithm::name(), Botan::MessageAuthenticationCode::name(), Botan::Buffered_Computation::output_length(), and Botan::SymmetricAlgorithm::valid_keylength().

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

Definition at line 200 of file randpool.cpp.

201  {
202  delete cipher;
203  delete mac;
204 
205  for(auto i = entropy_sources.begin(); i != entropy_sources.end(); ++i)
206  delete *i;
207  }

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(), and Botan::xor_buf().

128  {
129  secure_vector<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 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  }
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  }
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  }
secure_vector<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.

Referenced by Botan::TLS::Client_Key_Exchange::Client_Key_Exchange(), Botan::CryptoBox::encrypt(), Botan::KeyPair::encryption_consistency_check(), Botan::generate_bcrypt(), Botan::OctetString::OctetString(), Botan::BigInt::randomize(), Botan::TLS::Session_Manager_SQLite::Session_Manager_SQLite(), and Botan::KeyPair::signature_consistency_check().

41  {
42  secure_vector<byte> output(bytes);
43  randomize(&output[0], output.size());
44  return output;
45  }
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(), and name().

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  }
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(), 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  secure_vector<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  }

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