Cleared Valgrind warnings on uninitialized reads (Issue 105)

This commit is contained in:
Jeffrey Walton 2016-01-10 14:25:47 -05:00
parent dce2317195
commit 76b2f9387d
2 changed files with 36 additions and 28 deletions

56
rng.cpp
View File

@ -59,25 +59,33 @@ void LC_RNG::GenerateBlock(byte *output, size_t size)
#ifndef CRYPTOPP_IMPORTS
X917RNG::X917RNG(BlockTransformation *c, const byte *seed, const byte *deterministicTimeVector)
: cipher(c),
S(cipher->BlockSize()),
dtbuf(S),
randseed(seed, S),
m_lastBlock(S),
m_deterministicTimeVector(deterministicTimeVector, deterministicTimeVector ? S : 0)
: m_cipher(c),
m_size(m_cipher->BlockSize()),
m_datetime(m_size),
m_randseed(seed, m_size),
m_lastBlock(m_size),
m_deterministicTimeVector(deterministicTimeVector, deterministicTimeVector ? m_size : 0)
{
// Valgrind finding, http://github.com/weidai11/cryptopp/issues/105
// Garbage in the tail creates a non-conforming X9.17 or X9.31 generator.
if (m_size > 8)
{
memset(m_datetime, 0x00, m_size);
memset(m_lastBlock, 0x00, m_size);
}
if (!deterministicTimeVector)
{
time_t tstamp1 = time(0);
xorbuf(dtbuf, (byte *)&tstamp1, UnsignedMin(sizeof(tstamp1), S));
cipher->ProcessBlock(dtbuf);
xorbuf(m_datetime, (byte *)&tstamp1, UnsignedMin(sizeof(tstamp1), m_size));
m_cipher->ProcessBlock(m_datetime);
clock_t tstamp2 = clock();
xorbuf(dtbuf, (byte *)&tstamp2, UnsignedMin(sizeof(tstamp2), S));
cipher->ProcessBlock(dtbuf);
xorbuf(m_datetime, (byte *)&tstamp2, UnsignedMin(sizeof(tstamp2), m_size));
m_cipher->ProcessBlock(m_datetime);
}
// for FIPS 140-2
GenerateBlock(m_lastBlock, S);
GenerateBlock(m_lastBlock, m_size);
}
void X917RNG::GenerateIntoBufferedTransformation(BufferedTransformation &target, const std::string &channel, lword size)
@ -87,35 +95,35 @@ void X917RNG::GenerateIntoBufferedTransformation(BufferedTransformation &target,
// calculate new enciphered timestamp
if (m_deterministicTimeVector.size())
{
cipher->ProcessBlock(m_deterministicTimeVector, dtbuf);
IncrementCounterByOne(m_deterministicTimeVector, S);
m_cipher->ProcessBlock(m_deterministicTimeVector, m_datetime);
IncrementCounterByOne(m_deterministicTimeVector, m_size);
}
else
{
clock_t c = clock();
xorbuf(dtbuf, (byte *)&c, UnsignedMin(sizeof(c), S));
xorbuf(m_datetime, (byte *)&c, UnsignedMin(sizeof(c), m_size));
time_t t = time(NULL);
xorbuf(dtbuf+S-UnsignedMin(sizeof(t), S), (byte *)&t, UnsignedMin(sizeof(t), S));
cipher->ProcessBlock(dtbuf);
xorbuf(m_datetime+m_size-UnsignedMin(sizeof(t), m_size), (byte *)&t, UnsignedMin(sizeof(t), m_size));
m_cipher->ProcessBlock(m_datetime);
}
// combine enciphered timestamp with seed
xorbuf(randseed, dtbuf, S);
xorbuf(m_randseed, m_datetime, m_size);
// generate a new block of random bytes
cipher->ProcessBlock(randseed);
if (memcmp(m_lastBlock, randseed, S) == 0)
m_cipher->ProcessBlock(m_randseed);
if (memcmp(m_lastBlock, m_randseed, m_size) == 0)
throw SelfTestFailure("X917RNG: Continuous random number generator test failed.");
// output random bytes
size_t len = UnsignedMin(S, size);
target.ChannelPut(channel, randseed, len);
size_t len = UnsignedMin(m_size, size);
target.ChannelPut(channel, m_randseed, len);
size -= len;
// compute new seed vector
memcpy(m_lastBlock, randseed, S);
xorbuf(randseed, dtbuf, S);
cipher->ProcessBlock(randseed);
memcpy(m_lastBlock, m_randseed, m_size);
xorbuf(m_randseed, m_datetime, m_size);
m_cipher->ProcessBlock(m_randseed);
}
}

8
rng.h
View File

@ -69,10 +69,10 @@ public:
void GenerateIntoBufferedTransformation(BufferedTransformation &target, const std::string &channel, lword size);
private:
member_ptr<BlockTransformation> cipher;
const unsigned int S; // blocksize of cipher
SecByteBlock dtbuf; // buffer for enciphered timestamp
SecByteBlock randseed, m_lastBlock, m_deterministicTimeVector;
member_ptr<BlockTransformation> m_cipher;
const unsigned int m_size; // S, blocksize of cipher
SecByteBlock m_datetime; // DT, buffer for enciphered timestamp
SecByteBlock m_randseed, m_lastBlock, m_deterministicTimeVector;
};
//! \class MaurerRandomnessTest