diff options
| author | Jeffrey Walton <noloader@gmail.com> | 2015-11-05 01:59:46 -0500 |
|---|---|---|
| committer | Jeffrey Walton <noloader@gmail.com> | 2015-11-05 01:59:46 -0500 |
| commit | 48809d4e85c125814425c621d8d0d89f95405924 (patch) | |
| tree | 1010fd16c4b1199f3d27dd726dda241a2bd29f83 /randpool.cpp | |
| parent | 025337a94aceb75d188149db70c2094673772816 (diff) | |
| download | cryptopp-git-48809d4e85c125814425c621d8d0d89f95405924.tar.gz | |
CRYPTOPP 5.6.3 RC6 checkin
Diffstat (limited to 'randpool.cpp')
| -rw-r--r-- | randpool.cpp | 142 |
1 files changed, 74 insertions, 68 deletions
diff --git a/randpool.cpp b/randpool.cpp index ea7db55a..a9480cca 100644 --- a/randpool.cpp +++ b/randpool.cpp @@ -1,68 +1,74 @@ -// randpool.cpp - written and placed in the public domain by Wei Dai -// 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). - -#include "pch.h" - -#ifndef CRYPTOPP_IMPORTS - -#include "randpool.h" -#include "aes.h" -#include "sha.h" -#include "hrtimer.h" -#include <time.h> - -#if GCC_DIAGNOSTIC_AWARE -# pragma GCC diagnostic ignored "-Wunused-value" -# pragma GCC diagnostic ignored "-Wunused-variable" -#endif - -NAMESPACE_BEGIN(CryptoPP) - -RandomPool::RandomPool() - : m_pCipher(new AES::Encryption), m_keySet(false) -{ - memset(m_key, 0, m_key.SizeInBytes()); - memset(m_seed, 0, m_seed.SizeInBytes()); -} - -void RandomPool::IncorporateEntropy(const byte *input, size_t length) -{ - SHA256 hash; - hash.Update(m_key, 32); - hash.Update(input, length); - hash.Final(m_key); - m_keySet = false; -} - -void RandomPool::GenerateIntoBufferedTransformation(BufferedTransformation &target, const std::string &channel, lword size) -{ - if (size > 0) - { - 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); - } -} - -NAMESPACE_END - -#endif +// randpool.cpp - written and placed in the public domain by Wei Dai
+// 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).
+
+#include "pch.h"
+
+#ifndef CRYPTOPP_IMPORTS
+
+#include "randpool.h"
+#include "aes.h"
+#include "sha.h"
+#include "hrtimer.h"
+#include <time.h>
+
+NAMESPACE_BEGIN(CryptoPP)
+
+RandomPool::RandomPool()
+ : m_pCipher(new AES::Encryption), m_keySet(false)
+{
+ memset(m_key, 0, m_key.SizeInBytes());
+ memset(m_seed, 0, m_seed.SizeInBytes());
+}
+
+void RandomPool::IncorporateEntropy(const byte *input, size_t length)
+{
+ SHA256 hash;
+ hash.Update(m_key, 32);
+ hash.Update(input, length);
+ hash.Final(m_key);
+ m_keySet = false;
+}
+
+void RandomPool::GenerateIntoBufferedTransformation(BufferedTransformation &target, const std::string &channel, lword size)
+{
+ if (size > 0)
+ {
+ 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);
+
+ // UBsan finding: signed integer overflow: 1876017710 + 1446085457 cannot be represented in type 'long int'
+ // *(time_t *)(m_seed.data()+8) += t;
+ assert(m_seed.size() >= 16);
+ word64 tt1, tt2 = (word64)t;
+ memcpy(&tt1, m_seed.data()+8, 8);
+ memcpy(m_seed.data()+8, &(tt2 += tt1), 8);
+
+ // Wipe the intermediates
+ *((volatile TimerWord*)&tw) = 0;
+ *((volatile word64*)&tt1) = 0;
+ *((volatile word64*)&tt2) = 0;
+
+ do
+ {
+ m_pCipher->ProcessBlock(m_seed);
+ size_t len = UnsignedMin(16, size);
+ target.ChannelPut(channel, m_seed, len);
+ size -= len;
+ } while (size > 0);
+ }
+}
+
+NAMESPACE_END
+
+#endif
|
