randpool.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // randpool.h - originally written and placed in the public domain by Wei Dai
  2. // OldRandPool added by JW in August, 2017.
  3. /// \file randpool.h
  4. /// \brief Class file for Randomness Pool
  5. /// \details RandomPool can be used to generate cryptographic quality pseudorandom bytes
  6. /// after seeding the pool with IncorporateEntropy(). Internally, the generator uses
  7. /// AES-256 to produce the stream. Entropy is stirred in using SHA-256.
  8. /// \details RandomPool used to follow the design of randpool in PGP 2.6.x. At version 5.5
  9. /// RandomPool was redesigned to reduce the risk of reusing random numbers after state
  10. /// rollback (which may occur when running in a virtual machine like VMware or a hosted
  11. /// environment).
  12. /// \details If you need the pre-Crypto++ 5.5 generator then use OldRandomPool class. You
  13. /// should migrate away from OldRandomPool at the earliest opportunity. Use RandomPool
  14. /// or AutoSeededRandomPool instead.
  15. /// \since Crypto++ 4.0 (PGP 2.6.x style), Crypto++ 5.5 (AES-256 based)
  16. #ifndef CRYPTOPP_RANDPOOL_H
  17. #define CRYPTOPP_RANDPOOL_H
  18. #include "cryptlib.h"
  19. #include "filters.h"
  20. #include "secblock.h"
  21. #include "smartptr.h"
  22. #include "aes.h"
  23. NAMESPACE_BEGIN(CryptoPP)
  24. /// \brief Randomness Pool based on AES-256
  25. /// \details RandomPool can be used to generate cryptographic quality pseudorandom bytes
  26. /// after seeding the pool with IncorporateEntropy(). Internally, the generator uses
  27. /// AES-256 to produce the stream. Entropy is stirred in using SHA-256.
  28. /// \details RandomPool used to follow the design of randpool in PGP 2.6.x. At version 5.5
  29. /// RandomPool was redesigned to reduce the risk of reusing random numbers after state
  30. /// rollback, which may occur when running in a virtual machine like VMware or a hosted
  31. /// environment.
  32. /// \details You should reseed the generator after a fork() to avoid multiple generators
  33. /// with the same internal state.
  34. /// \details If you need the pre-Crypto++ 5.5 generator then use OldRandomPool class. You
  35. /// should migrate away from OldRandomPool at the earliest opportunity.
  36. /// \sa OldRandomPool
  37. /// \since Crypto++ 4.0 (PGP 2.6.x style), Crypto++ 5.5 (AES-256 based)
  38. class CRYPTOPP_DLL RandomPool : public RandomNumberGenerator, public NotCopyable
  39. {
  40. public:
  41. /// \brief Construct a RandomPool
  42. RandomPool();
  43. bool CanIncorporateEntropy() const {return true;}
  44. void IncorporateEntropy(const byte *input, size_t length);
  45. void GenerateIntoBufferedTransformation(BufferedTransformation &target, const std::string &channel, lword size);
  46. private:
  47. FixedSizeAlignedSecBlock<byte, 16, true> m_seed;
  48. FixedSizeAlignedSecBlock<byte, 32> m_key;
  49. member_ptr<BlockCipher> m_pCipher;
  50. bool m_keySet;
  51. };
  52. /// \brief Randomness Pool based on PGP 2.6.x with MDC
  53. /// \details If you need the pre-Crypto++ 5.5 generator then use OldRandomPool class. The
  54. /// OldRandomPool also provides the modern interface, including <tt>CanIncorporateEntropy</tt>,
  55. /// <tt>IncorporateEntropy</tt> and <tt>GenerateIntoBufferedTransformation</tt>.
  56. /// \details You should reseed the generator after a fork() to avoid multiple generators
  57. /// with the same internal state.
  58. /// \details You should migrate away from OldRandomPool at the earliest opportunity. Use a
  59. /// modern random number generator or key derivation function, like AutoSeededRandomPool or
  60. /// HKDF.
  61. /// \warning This class uses an old style PGP 2.6.x with MDC. The generator risks reusing
  62. /// random numbers after state rollback. You should migrate away from OldRandomPool at
  63. /// the earliest opportunity.
  64. /// \sa RandomPool, AutoSeededRandomPool, HKDF, P1363_KDF2, PKCS12_PBKDF, PKCS5_PBKDF2_HMAC
  65. /// \since Crypto++ 6.0
  66. class CRYPTOPP_DLL OldRandomPool : public RandomNumberGenerator
  67. {
  68. public:
  69. /// \brief Construct an OldRandomPool
  70. /// \param poolSize internal pool size of the generator
  71. /// \details poolSize must be greater than 16
  72. OldRandomPool(unsigned int poolSize=384);
  73. // RandomNumberGenerator interface (Crypto++ 5.5 and above)
  74. bool CanIncorporateEntropy() const {return true;}
  75. void IncorporateEntropy(const byte *input, size_t length);
  76. void GenerateIntoBufferedTransformation(BufferedTransformation &target, const std::string &channel, lword size);
  77. byte GenerateByte();
  78. void GenerateBlock(byte *output, size_t size);
  79. // GenerateWord32 is overridden and provides Crypto++ 5.4 behavior.
  80. // Taken from RandomNumberSource::GenerateWord32 in cryptlib.cpp.
  81. word32 GenerateWord32 (word32 min=0, word32 max=0xffffffffUL);
  82. protected:
  83. void Stir();
  84. private:
  85. SecByteBlock pool, key;
  86. size_t addPos, getPos;
  87. };
  88. NAMESPACE_END
  89. #endif