Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.4 KiB

  1. // randpool.cpp - written and placed in the public domain by Wei Dai
  2. // RandomPool used to follow the design of randpool in PGP 2.6.x,
  3. // but as of version 5.5 it has been redesigned to reduce the risk
  4. // of reusing random numbers after state rollback (which may occur
  5. // when running in a virtual machine like VMware).
  6. #include "pch.h"
  7. #ifndef CRYPTOPP_IMPORTS
  8. #include "randpool.h"
  9. #include "aes.h"
  10. #include "sha.h"
  11. #include "hrtimer.h"
  12. #include <time.h>
  13. NAMESPACE_BEGIN(CryptoPP)
  14. RandomPool::RandomPool()
  15. : m_pCipher(new AES::Encryption), m_keySet(false)
  16. {
  17. memset(m_key, 0, m_key.SizeInBytes());
  18. memset(m_seed, 0, m_seed.SizeInBytes());
  19. }
  20. void RandomPool::IncorporateEntropy(const byte *input, size_t length)
  21. {
  22. SHA256 hash;
  23. hash.Update(m_key, 32);
  24. hash.Update(input, length);
  25. hash.Final(m_key);
  26. m_keySet = false;
  27. }
  28. void RandomPool::GenerateIntoBufferedTransformation(BufferedTransformation &target, const std::string &channel, lword size)
  29. {
  30. if (size > 0)
  31. {
  32. if (!m_keySet)
  33. m_pCipher->SetKey(m_key, 32);
  34. Timer timer;
  35. TimerWord tw = timer.GetCurrentTimerValue();
  36. CRYPTOPP_COMPILE_ASSERT(sizeof(tw) <= 16);
  37. *(TimerWord *)m_seed.data() += tw;
  38. time_t t = time(NULL);
  39. CRYPTOPP_COMPILE_ASSERT(sizeof(t) <= 8);
  40. *(time_t *)(m_seed.data()+8) += t;
  41. do
  42. {
  43. m_pCipher->ProcessBlock(m_seed);
  44. size_t len = UnsignedMin(16, size);
  45. target.ChannelPut(channel, m_seed, len);
  46. size -= len;
  47. } while (size > 0);
  48. }
  49. }
  50. NAMESPACE_END
  51. #endif