2002-10-04 17:31:41 +00:00
|
|
|
// randpool.cpp - written and placed in the public domain by Wei Dai
|
2007-05-04 15:37:46 +00:00
|
|
|
// RandomPool used to follow the design of randpool in PGP 2.6.x,
|
|
|
|
// but as of version 5.5 it has been redesigned to reduce the risk
|
|
|
|
// of reusing random numbers after state rollback (which may occur
|
|
|
|
// when running in a virtual machine like VMware).
|
2002-10-04 17:31:41 +00:00
|
|
|
|
|
|
|
#include "pch.h"
|
2003-07-04 00:17:37 +00:00
|
|
|
|
|
|
|
#ifndef CRYPTOPP_IMPORTS
|
|
|
|
|
2002-10-04 17:31:41 +00:00
|
|
|
#include "randpool.h"
|
2007-05-04 15:37:46 +00:00
|
|
|
#include "aes.h"
|
2002-10-04 17:31:41 +00:00
|
|
|
#include "sha.h"
|
2007-05-04 15:37:46 +00:00
|
|
|
#include "hrtimer.h"
|
|
|
|
#include <time.h>
|
2002-10-04 17:31:41 +00:00
|
|
|
|
|
|
|
NAMESPACE_BEGIN(CryptoPP)
|
|
|
|
|
2007-05-04 15:37:46 +00:00
|
|
|
RandomPool::RandomPool()
|
|
|
|
: m_pCipher(new AES::Encryption), m_keySet(false)
|
2002-10-04 17:31:41 +00:00
|
|
|
{
|
2009-09-17 01:45:26 +00:00
|
|
|
memset(m_key, 0, m_key.SizeInBytes());
|
|
|
|
memset(m_seed, 0, m_seed.SizeInBytes());
|
2002-10-04 17:31:41 +00:00
|
|
|
}
|
|
|
|
|
2007-05-04 15:37:46 +00:00
|
|
|
void RandomPool::IncorporateEntropy(const byte *input, size_t length)
|
2002-10-04 17:31:41 +00:00
|
|
|
{
|
2007-05-04 15:37:46 +00:00
|
|
|
SHA256 hash;
|
|
|
|
hash.Update(m_key, 32);
|
|
|
|
hash.Update(input, length);
|
|
|
|
hash.Final(m_key);
|
|
|
|
m_keySet = false;
|
2002-10-04 17:31:41 +00:00
|
|
|
}
|
|
|
|
|
2007-05-04 15:37:46 +00:00
|
|
|
void RandomPool::GenerateIntoBufferedTransformation(BufferedTransformation &target, const std::string &channel, lword size)
|
2002-10-04 17:31:41 +00:00
|
|
|
{
|
2007-05-04 15:37:46 +00:00
|
|
|
if (size > 0)
|
2002-10-04 17:31:41 +00:00
|
|
|
{
|
2007-05-04 15:37:46 +00:00
|
|
|
if (!m_keySet)
|
|
|
|
m_pCipher->SetKey(m_key, 32);
|
|
|
|
|
|
|
|
Timer timer;
|
|
|
|
TimerWord tw = timer.GetCurrentTimerValue();
|
|
|
|
CRYPTOPP_COMPILE_ASSERT(sizeof(tw) <= 16);
|
|
|
|
*(TimerWord *)m_seed.data() += tw;
|
|
|
|
|
|
|
|
time_t t = time(NULL);
|
|
|
|
CRYPTOPP_COMPILE_ASSERT(sizeof(t) <= 8);
|
|
|
|
*(time_t *)(m_seed.data()+8) += t;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
m_pCipher->ProcessBlock(m_seed);
|
|
|
|
size_t len = UnsignedMin(16, size);
|
|
|
|
target.ChannelPut(channel, m_seed, len);
|
|
|
|
size -= len;
|
|
|
|
} while (size > 0);
|
2002-10-04 17:31:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NAMESPACE_END
|
2003-07-04 00:17:37 +00:00
|
|
|
|
|
|
|
#endif
|