aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/rng/x931_rng/x931_rng.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/rng/x931_rng/x931_rng.cpp')
-rw-r--r--src/lib/rng/x931_rng/x931_rng.cpp102
1 files changed, 34 insertions, 68 deletions
diff --git a/src/lib/rng/x931_rng/x931_rng.cpp b/src/lib/rng/x931_rng/x931_rng.cpp
index b36f87106..dbf09b367 100644
--- a/src/lib/rng/x931_rng/x931_rng.cpp
+++ b/src/lib/rng/x931_rng/x931_rng.cpp
@@ -1,6 +1,6 @@
/*
* ANSI X9.31 RNG
-* (C) 1999-2009 Jack Lloyd
+* (C) 1999-2009,2014 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
@@ -11,9 +11,6 @@
namespace Botan {
-/*
-* Generate a buffer of random bytes
-*/
void ANSI_X931_RNG::randomize(byte out[], size_t length)
{
if(!is_seeded())
@@ -21,15 +18,15 @@ void ANSI_X931_RNG::randomize(byte out[], size_t length)
while(length)
{
- if(position == R.size())
+ if(m_R_pos == m_R.size())
update_buffer();
- const size_t copied = std::min<size_t>(length, R.size() - position);
+ const size_t copied = std::min<size_t>(length, m_R.size() - m_R_pos);
- copy_mem(out, &R[position], copied);
+ copy_mem(out, &m_R[m_R_pos], copied);
out += copied;
length -= copied;
- position += copied;
+ m_R_pos += copied;
}
}
@@ -38,18 +35,18 @@ void ANSI_X931_RNG::randomize(byte out[], size_t length)
*/
void ANSI_X931_RNG::update_buffer()
{
- const size_t BLOCK_SIZE = cipher->block_size();
+ const size_t BLOCK_SIZE = m_cipher->block_size();
- secure_vector<byte> DT = prng->random_vec(BLOCK_SIZE);
- cipher->encrypt(DT);
+ secure_vector<byte> DT = m_prng->random_vec(BLOCK_SIZE);
+ m_cipher->encrypt(DT);
- xor_buf(&R[0], &V[0], &DT[0], BLOCK_SIZE);
- cipher->encrypt(R);
+ xor_buf(&m_R[0], &m_V[0], &DT[0], BLOCK_SIZE);
+ m_cipher->encrypt(m_R);
- xor_buf(&V[0], &R[0], &DT[0], BLOCK_SIZE);
- cipher->encrypt(V);
+ xor_buf(&m_V[0], &m_R[0], &DT[0], BLOCK_SIZE);
+ m_cipher->encrypt(m_V);
- position = 0;
+ m_R_pos = 0;
}
/*
@@ -57,90 +54,59 @@ void ANSI_X931_RNG::update_buffer()
*/
void ANSI_X931_RNG::rekey()
{
- const size_t BLOCK_SIZE = cipher->block_size();
+ const size_t BLOCK_SIZE = m_cipher->block_size();
- if(prng->is_seeded())
+ if(m_prng->is_seeded())
{
- cipher->set_key(prng->random_vec(cipher->maximum_keylength()));
+ m_cipher->set_key(m_prng->random_vec(m_cipher->maximum_keylength()));
- if(V.size() != BLOCK_SIZE)
- V.resize(BLOCK_SIZE);
- prng->randomize(&V[0], V.size());
+ if(m_V.size() != BLOCK_SIZE)
+ m_V.resize(BLOCK_SIZE);
+ m_prng->randomize(&m_V[0], m_V.size());
update_buffer();
}
}
-/*
-* Reseed the internal state
-*/
void ANSI_X931_RNG::reseed(size_t poll_bits)
{
- prng->reseed(poll_bits);
+ m_prng->reseed(poll_bits);
rekey();
}
-/*
-* Add some entropy to the underlying PRNG
-*/
void ANSI_X931_RNG::add_entropy(const byte input[], size_t length)
{
- prng->add_entropy(input, length);
+ m_prng->add_entropy(input, length);
rekey();
}
-/*
-* Check if the the PRNG is seeded
-*/
bool ANSI_X931_RNG::is_seeded() const
{
- return (V.size() > 0);
+ return (m_V.size() > 0);
}
-/*
-* Clear memory of sensitive data
-*/
void ANSI_X931_RNG::clear()
{
- cipher->clear();
- prng->clear();
- zeroise(R);
- V.clear();
+ m_cipher->clear();
+ m_prng->clear();
+ zeroise(m_R);
+ m_V.clear();
- position = 0;
+ m_R_pos = 0;
}
-/*
-* 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::ANSI_X931_RNG(BlockCipher* cipher_in,
- RandomNumberGenerator* prng_in)
- {
- if(!prng_in || !cipher_in)
- throw Invalid_Argument("ANSI_X931_RNG constructor: NULL arguments");
-
- cipher = cipher_in;
- prng = prng_in;
-
- R.resize(cipher->block_size());
- position = 0;
+ return "X9.31(" + m_cipher->name() + ")";
}
-/*
-* ANSI X931 RNG Destructor
-*/
-ANSI_X931_RNG::~ANSI_X931_RNG()
+ANSI_X931_RNG::ANSI_X931_RNG(BlockCipher* cipher,
+ RandomNumberGenerator* prng) :
+ m_cipher(cipher),
+ m_prng(prng),
+ m_R(m_cipher->block_size()),
+ m_R_pos(0)
{
- delete cipher;
- delete prng;
}
}