aboutsummaryrefslogtreecommitdiffstats
path: root/src/rng
diff options
context:
space:
mode:
Diffstat (limited to 'src/rng')
-rw-r--r--src/rng/hmac_rng/hmac_rng.cpp21
-rw-r--r--src/rng/hmac_rng/hmac_rng.h4
-rw-r--r--src/rng/randpool/randpool.cpp24
-rw-r--r--src/rng/randpool/randpool.h4
-rw-r--r--src/rng/x931_rng/x931_rng.cpp82
-rw-r--r--src/rng/x931_rng/x931_rng.h15
6 files changed, 69 insertions, 81 deletions
diff --git a/src/rng/hmac_rng/hmac_rng.cpp b/src/rng/hmac_rng/hmac_rng.cpp
index ffdfdc60d..458118e11 100644
--- a/src/rng/hmac_rng/hmac_rng.cpp
+++ b/src/rng/hmac_rng/hmac_rng.cpp
@@ -69,7 +69,7 @@ void HMAC_RNG::reseed_with_input(u32bit poll_bits,
feedback of the current PRK value, into the extractor function.
*/
- Entropy_Accumulator accum(*extractor, poll_bits);
+ Entropy_Accumulator_BufferedComputation accum(*extractor, poll_bits);
for(u32bit i = 0; i < entropy_sources.size(); ++i)
{
@@ -112,9 +112,8 @@ void HMAC_RNG::reseed_with_input(u32bit poll_bits,
K.clear();
counter = 0;
- // Upper bound entropy estimate at the extractor output size
- entropy = std::min<u32bit>(entropy + accum.bits_collected(),
- 8 * extractor->OUTPUT_LENGTH);
+ if(input_length || accum.bits_collected() >= poll_bits)
+ seeded = true;
}
/**
@@ -142,14 +141,6 @@ void HMAC_RNG::add_entropy_source(EntropySource* src)
entropy_sources.push_back(src);
}
-/**
-* Check if the the pool is seeded
-*/
-bool HMAC_RNG::is_seeded() const
- {
- return (entropy >= 8 * prf->OUTPUT_LENGTH);
- }
-
/*
* Clear memory of sensitive data
*/
@@ -158,8 +149,8 @@ void HMAC_RNG::clear() throw()
extractor->clear();
prf->clear();
K.clear();
- entropy = 0;
counter = 0;
+ seeded = false;
}
/**
@@ -177,11 +168,10 @@ HMAC_RNG::HMAC_RNG(MessageAuthenticationCode* extractor_mac,
MessageAuthenticationCode* prf_mac) :
extractor(extractor_mac), prf(prf_mac)
{
- entropy = 0;
-
// First PRF inputs are all zero, as specified in section 2
K.create(prf->OUTPUT_LENGTH);
counter = 0;
+ seeded = false;
/*
Normally we want to feedback PRF output into the input to the
@@ -223,7 +213,6 @@ HMAC_RNG::~HMAC_RNG()
std::for_each(entropy_sources.begin(), entropy_sources.end(),
del_fun<EntropySource>());
- entropy = 0;
counter = 0;
}
diff --git a/src/rng/hmac_rng/hmac_rng.h b/src/rng/hmac_rng/hmac_rng.h
index fbfa8df19..16adddd60 100644
--- a/src/rng/hmac_rng/hmac_rng.h
+++ b/src/rng/hmac_rng/hmac_rng.h
@@ -26,7 +26,7 @@ class BOTAN_DLL HMAC_RNG : public RandomNumberGenerator
{
public:
void randomize(byte buf[], u32bit len);
- bool is_seeded() const;
+ bool is_seeded() const { return seeded; }
void clear() throw();
std::string name() const;
@@ -46,7 +46,7 @@ class BOTAN_DLL HMAC_RNG : public RandomNumberGenerator
MessageAuthenticationCode* prf;
std::vector<EntropySource*> entropy_sources;
- u32bit entropy;
+ bool seeded;
SecureVector<byte> K, io_buffer;
u32bit counter, source_index;
diff --git a/src/rng/randpool/randpool.cpp b/src/rng/randpool/randpool.cpp
index 41a8ca23a..af36c335f 100644
--- a/src/rng/randpool/randpool.cpp
+++ b/src/rng/randpool/randpool.cpp
@@ -101,7 +101,7 @@ void Randpool::mix_pool()
*/
void Randpool::reseed(u32bit poll_bits)
{
- Entropy_Accumulator accum(*mac, poll_bits);
+ Entropy_Accumulator_BufferedComputation accum(*mac, poll_bits);
for(u32bit i = 0; i != entropy_sources.size(); ++i)
{
@@ -116,8 +116,8 @@ void Randpool::reseed(u32bit poll_bits)
xor_buf(pool, mac_val, mac_val.size());
mix_pool();
- entropy = std::min<u32bit>(entropy + accum.bits_collected(),
- 8 * mac_val.size());
+ if(accum.bits_collected() >= poll_bits)
+ seeded = true;
}
/**
@@ -129,8 +129,8 @@ void Randpool::add_entropy(const byte input[], u32bit length)
xor_buf(pool, mac_val, mac_val.size());
mix_pool();
- // Assume 1 bit conditional entropy per byte of input
- entropy = std::min<u32bit>(entropy + length, 8 * mac_val.size());
+ if(length)
+ seeded = true;
}
/**
@@ -142,14 +142,6 @@ void Randpool::add_entropy_source(EntropySource* src)
}
/**
-* Check if the the pool is seeded
-*/
-bool Randpool::is_seeded() const
- {
- return (entropy >= 7 * mac->OUTPUT_LENGTH);
- }
-
-/**
* Clear memory of sensitive data
*/
void Randpool::clear() throw()
@@ -159,7 +151,7 @@ void Randpool::clear() throw()
pool.clear();
buffer.clear();
counter.clear();
- entropy = 0;
+ seeded = false;
}
/**
@@ -198,7 +190,7 @@ Randpool::Randpool(BlockCipher* cipher_in,
buffer.create(BLOCK_SIZE);
pool.create(POOL_BLOCKS * BLOCK_SIZE);
counter.create(12);
- entropy = 0;
+ seeded = false;
}
/**
@@ -211,8 +203,6 @@ Randpool::~Randpool()
std::for_each(entropy_sources.begin(), entropy_sources.end(),
del_fun<EntropySource>());
-
- entropy = 0;
}
}
diff --git a/src/rng/randpool/randpool.h b/src/rng/randpool/randpool.h
index 46683934e..f44527609 100644
--- a/src/rng/randpool/randpool.h
+++ b/src/rng/randpool/randpool.h
@@ -20,7 +20,7 @@ class BOTAN_DLL Randpool : public RandomNumberGenerator
{
public:
void randomize(byte[], u32bit);
- bool is_seeded() const;
+ bool is_seeded() const { return seeded; }
void clear() throw();
std::string name() const;
@@ -43,7 +43,7 @@ class BOTAN_DLL Randpool : public RandomNumberGenerator
std::vector<EntropySource*> entropy_sources;
SecureVector<byte> pool, buffer, counter;
- u32bit entropy;
+ bool seeded;
};
}
diff --git a/src/rng/x931_rng/x931_rng.cpp b/src/rng/x931_rng/x931_rng.cpp
index 4b33f4c5e..e77f04ae4 100644
--- a/src/rng/x931_rng/x931_rng.cpp
+++ b/src/rng/x931_rng/x931_rng.cpp
@@ -1,7 +1,7 @@
-/*************************************************
-* ANSI X9.31 RNG Source File *
-* (C) 1999-2008 Jack Lloyd *
-*************************************************/
+/*
+* ANSI X9.31 RNG Source File
+* (C) 1999-2009 Jack Lloyd
+*/
#include <botan/x931_rng.h>
#include <botan/xor_buf.h>
@@ -9,9 +9,9 @@
namespace Botan {
-/*************************************************
-* Generate a buffer of random bytes *
-*************************************************/
+/**
+* Generate a buffer of random bytes
+*/
void ANSI_X931_RNG::randomize(byte out[], u32bit length)
{
if(!is_seeded())
@@ -31,9 +31,9 @@ void ANSI_X931_RNG::randomize(byte out[], u32bit length)
}
}
-/*************************************************
-* Refill the internal state *
-*************************************************/
+/**
+* Refill the internal state
+*/
void ANSI_X931_RNG::update_buffer()
{
SecureVector<byte> DT(cipher->BLOCK_SIZE);
@@ -50,13 +50,11 @@ void ANSI_X931_RNG::update_buffer()
position = 0;
}
-/*************************************************
-* Reseed the internal state *
-*************************************************/
-void ANSI_X931_RNG::reseed(u32bit poll_bits)
+/**
+* Reset V and the cipher key with new values
+*/
+void ANSI_X931_RNG::rekey()
{
- prng->reseed(poll_bits);
-
if(prng->is_seeded())
{
SecureVector<byte> key(cipher->MAXIMUM_KEYLENGTH);
@@ -71,33 +69,43 @@ void ANSI_X931_RNG::reseed(u32bit poll_bits)
}
}
-/*************************************************
-* Add a entropy source to the underlying PRNG *
-*************************************************/
+/**
+* Reseed the internal state
+*/
+void ANSI_X931_RNG::reseed(u32bit poll_bits)
+ {
+ prng->reseed(poll_bits);
+ rekey();
+ }
+
+/**
+* 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 *
-*************************************************/
+/**
+* Add some entropy to the underlying PRNG
+*/
void ANSI_X931_RNG::add_entropy(const byte input[], u32bit length)
{
prng->add_entropy(input, length);
+ rekey();
}
-/*************************************************
-* Check if the the PRNG is seeded *
-*************************************************/
+/**
+* Check if the the PRNG is seeded
+*/
bool ANSI_X931_RNG::is_seeded() const
{
return V.has_items();
}
-/*************************************************
-* Clear memory of sensitive data *
-*************************************************/
+/**
+* Clear memory of sensitive data
+*/
void ANSI_X931_RNG::clear() throw()
{
cipher->clear();
@@ -108,17 +116,17 @@ void ANSI_X931_RNG::clear() throw()
position = 0;
}
-/*************************************************
-* Return the name of this type *
-*************************************************/
+/**
+* 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 Constructor
+*/
ANSI_X931_RNG::ANSI_X931_RNG(BlockCipher* cipher_in,
RandomNumberGenerator* prng_in)
{
@@ -132,9 +140,9 @@ ANSI_X931_RNG::ANSI_X931_RNG(BlockCipher* cipher_in,
position = 0;
}
-/*************************************************
-* ANSI X931 RNG Destructor *
-*************************************************/
+/**
+* ANSI X931 RNG Destructor
+*/
ANSI_X931_RNG::~ANSI_X931_RNG()
{
delete cipher;
diff --git a/src/rng/x931_rng/x931_rng.h b/src/rng/x931_rng/x931_rng.h
index 2c68b9cb4..b1cef8df3 100644
--- a/src/rng/x931_rng/x931_rng.h
+++ b/src/rng/x931_rng/x931_rng.h
@@ -1,7 +1,7 @@
-/*************************************************
-* ANSI X9.31 RNG Header File *
-* (C) 1999-2008 Jack Lloyd *
-*************************************************/
+/*
+* ANSI X9.31 RNG Header File
+* (C) 1999-2009 Jack Lloyd
+*/
#ifndef BOTAN_ANSI_X931_RNG_H__
#define BOTAN_ANSI_X931_RNG_H__
@@ -11,9 +11,9 @@
namespace Botan {
-/*************************************************
-* ANSI X9.31 RNG *
-*************************************************/
+/**
+* ANSI X9.31 RNG
+*/
class BOTAN_DLL ANSI_X931_RNG : public RandomNumberGenerator
{
public:
@@ -29,6 +29,7 @@ class BOTAN_DLL ANSI_X931_RNG : public RandomNumberGenerator
ANSI_X931_RNG(BlockCipher*, RandomNumberGenerator*);
~ANSI_X931_RNG();
private:
+ void rekey();
void update_buffer();
BlockCipher* cipher;