randpool.cpp

00001 // randpool.cpp - written and placed in the public domain by Wei Dai
00002 // RandomPool used to follow the design of randpool in PGP 2.6.x,
00003 // but as of version 5.5 it has been redesigned to reduce the risk
00004 // of reusing random numbers after state rollback (which may occur
00005 // when running in a virtual machine like VMware).
00006 
00007 #include "pch.h"
00008 
00009 #ifndef CRYPTOPP_IMPORTS
00010 
00011 #include "randpool.h"
00012 #include "aes.h"
00013 #include "sha.h"
00014 #include "hrtimer.h"
00015 #include <time.h>
00016 
00017 NAMESPACE_BEGIN(CryptoPP)
00018 
00019 RandomPool::RandomPool()
00020         : m_pCipher(new AES::Encryption), m_keySet(false)
00021 {
00022 }
00023 
00024 void RandomPool::IncorporateEntropy(const byte *input, size_t length)
00025 {
00026         SHA256 hash;
00027         hash.Update(m_key, 32);
00028         hash.Update(input, length);
00029         hash.Final(m_key);
00030         m_keySet = false;
00031 }
00032 
00033 void RandomPool::GenerateIntoBufferedTransformation(BufferedTransformation &target, const std::string &channel, lword size)
00034 {
00035         if (size > 0)
00036         {
00037                 if (!m_keySet)
00038                         m_pCipher->SetKey(m_key, 32);
00039 
00040                 Timer timer;
00041                 TimerWord tw = timer.GetCurrentTimerValue();
00042                 CRYPTOPP_COMPILE_ASSERT(sizeof(tw) <= 16);
00043                 *(TimerWord *)m_seed.data() += tw;
00044 
00045                 time_t t = time(NULL);
00046                 CRYPTOPP_COMPILE_ASSERT(sizeof(t) <= 8);
00047                 *(time_t *)(m_seed.data()+8) += t;
00048 
00049                 do
00050                 {
00051                         m_pCipher->ProcessBlock(m_seed);
00052                         size_t len = UnsignedMin(16, size);
00053                         target.ChannelPut(channel, m_seed, len);
00054                         size -= len;
00055                 } while (size > 0);
00056         }
00057 }
00058 
00059 NAMESPACE_END
00060 
00061 #endif

Generated on Fri Jun 1 11:11:23 2007 for Crypto++ by  doxygen 1.5.2