summaryrefslogtreecommitdiffstats
path: root/CryptoPP/randpool.cpp
diff options
context:
space:
mode:
authormadmaxoft@gmail.com <madmaxoft@gmail.com@0a769ca7-a7f5-676a-18bf-c427514a06d6>2012-08-30 23:06:13 +0200
committermadmaxoft@gmail.com <madmaxoft@gmail.com@0a769ca7-a7f5-676a-18bf-c427514a06d6>2012-08-30 23:06:13 +0200
commit539364846a89987ac2679988653f50332cb91d26 (patch)
treef1695473c1f493a19c5fbdb70f7f1faccf99d7f3 /CryptoPP/randpool.cpp
parentUpdated to V6 - "Stop" and "Progress report" functionality (diff)
downloadcuberite-539364846a89987ac2679988653f50332cb91d26.tar
cuberite-539364846a89987ac2679988653f50332cb91d26.tar.gz
cuberite-539364846a89987ac2679988653f50332cb91d26.tar.bz2
cuberite-539364846a89987ac2679988653f50332cb91d26.tar.lz
cuberite-539364846a89987ac2679988653f50332cb91d26.tar.xz
cuberite-539364846a89987ac2679988653f50332cb91d26.tar.zst
cuberite-539364846a89987ac2679988653f50332cb91d26.zip
Diffstat (limited to 'CryptoPP/randpool.cpp')
-rw-r--r--CryptoPP/randpool.cpp63
1 files changed, 63 insertions, 0 deletions
diff --git a/CryptoPP/randpool.cpp b/CryptoPP/randpool.cpp
new file mode 100644
index 000000000..a063c8996
--- /dev/null
+++ b/CryptoPP/randpool.cpp
@@ -0,0 +1,63 @@
+// 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);
+ *(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