Botan 1.10.17

Public Member Functions | Static Public Member Functions | List of all members
Botan::Randpool Class Reference

`#include <randpool.h>`

Inheritance diagram for Botan::Randpool:

## 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< byte > | random_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 RandomNumberGenerator * | make_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

  cipher | a block cipher to use
  mac | a message authentication code to use
  pool_blocks | how many cipher blocks to use for the pool
  iterations_before_reseed | how 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 }
Botan::MemoryRegion::resize
void resize(size_t n)
Definition: secmem.h:217
Botan::SymmetricAlgorithm::valid_keylength
bool valid_keylength(size_t length) const
Definition: sym_algo.h:51
Botan::BlockCipher::block_size
virtual size_t block_size() const =0
Botan::Buffered_Computation::output_length
virtual size_t output_length() const =0

## ◆ ~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

  in | a byte array containg the entropy to be added
  length | the 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 }
Botan::Buffered_Computation::process
SecureVector< byte > process(const byte in[], size_t length)
Definition: buf_comp.h:101
Botan::xor_buf
void xor_buf(byte out[], const byte in[], size_t length)
Definition: xor_buf.h:21

## ◆ add_entropy_source()

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

Add this entropy source to the RNG object

Parameters

  source | the 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.

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 }
Botan::Algorithm::clear
virtual void clear()=0
Botan::zeroise
void zeroise(MemoryRegion< T > &vec)
Definition: secmem.h:428

## ◆ is_seeded()

bool Botan::Randpool::is_seeded | ( |  | ) | const
| inline virtual

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; }

## ◆ make_rng()

RandomNumberGenerator * Botan::RandomNumberGenerator::make_rng | ( |  | ) |
| static inherited

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 }

## ◆ 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.

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

Referenced by randomize().

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

## ◆ next_byte()

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

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

## ◆ random_vec()

SecureVector<byte> Botan::RandomNumberGenerator::random_vec | ( | size_t | bytes | ) |
| inline inherited

Return a random vector

Parameters

  bytes | number of bytes in the result

Returns
  randomized vector of length bytes

Definition at line 40 of file rng.h.

References Botan::MemoryRegion< T >::size().

41 {
42 SecureVector<byte> output(bytes);
43 randomize(&output[0], output.size());
44 return output;
45 }
Botan::RandomNumberGenerator::randomize
virtual void randomize(byte output[], size_t length)=0

## ◆ randomize()

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

Randomize a byte array.

Parameters

  output | the byte array to hold the random output.
  length | the 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 }
Botan::Randpool::name
std::string name() const
Definition: randpool.cpp:161
Botan::copy_mem
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:22
Botan::MemoryRegion::size
size_t size() const
Definition: secmem.h:29
Botan::Randpool::is_seeded
bool is_seeded() const
Definition: randpool.h:25

## ◆ reseed()

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

Seed this RNG using the entropy sources it contains.

Parameters

  bits_to_collect | is 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 }
Botan::Buffered_Computation::final
void final(byte out[])
Definition: buf_comp.h:80
Botan::xor_buf
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:

- src/rng/randpool/randpool.h
- src/rng/randpool/randpool.cpp