Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:55:08

0001 // randpool.h - originally written and placed in the public domain by Wei Dai

0002 //              OldRandPool added by JW in August, 2017.

0003 
0004 /// \file randpool.h

0005 /// \brief Class file for Randomness Pool

0006 /// \details RandomPool can be used to generate cryptographic quality pseudorandom bytes

0007 ///  after seeding the pool with IncorporateEntropy(). Internally, the generator uses

0008 ///  AES-256 to produce the stream. Entropy is stirred in using SHA-256.

0009 /// \details RandomPool used to follow the design of randpool in PGP 2.6.x. At version 5.5

0010 ///  RandomPool was redesigned to reduce the risk of reusing random numbers after state

0011 ///  rollback (which may occur when running in a virtual machine like VMware or a hosted

0012 ///  environment).

0013 /// \details If you need the pre-Crypto++ 5.5 generator then use OldRandomPool class. You

0014 ///  should migrate away from OldRandomPool at the earliest opportunity. Use RandomPool

0015 ///  or AutoSeededRandomPool instead.

0016 /// \since Crypto++ 4.0 (PGP 2.6.x style), Crypto++ 5.5 (AES-256 based)

0017 
0018 #ifndef CRYPTOPP_RANDPOOL_H
0019 #define CRYPTOPP_RANDPOOL_H
0020 
0021 #include "cryptlib.h"
0022 #include "filters.h"
0023 #include "secblock.h"
0024 #include "smartptr.h"
0025 #include "aes.h"
0026 
0027 NAMESPACE_BEGIN(CryptoPP)
0028 
0029 /// \brief Randomness Pool based on AES-256

0030 /// \details RandomPool can be used to generate cryptographic quality pseudorandom bytes

0031 ///  after seeding the pool with IncorporateEntropy(). Internally, the generator uses

0032 ///  AES-256 to produce the stream. Entropy is stirred in using SHA-256.

0033 /// \details RandomPool used to follow the design of randpool in PGP 2.6.x. At version 5.5

0034 ///  RandomPool was redesigned to reduce the risk of reusing random numbers after state

0035 ///  rollback, which may occur when running in a virtual machine like VMware or a hosted

0036 ///  environment.

0037 /// \details You should reseed the generator after a fork() to avoid multiple generators

0038 ///  with the same internal state.

0039 /// \details If you need the pre-Crypto++ 5.5 generator then use OldRandomPool class. You

0040 ///  should migrate away from OldRandomPool at the earliest opportunity.

0041 /// \sa OldRandomPool

0042 /// \since Crypto++ 4.0 (PGP 2.6.x style), Crypto++ 5.5 (AES-256 based)

0043 class CRYPTOPP_DLL RandomPool : public RandomNumberGenerator, public NotCopyable
0044 {
0045 public:
0046     /// \brief Construct a RandomPool

0047     RandomPool();
0048 
0049     bool CanIncorporateEntropy() const {return true;}
0050     void IncorporateEntropy(const byte *input, size_t length);
0051     void GenerateIntoBufferedTransformation(BufferedTransformation &target, const std::string &channel, lword size);
0052 
0053 private:
0054     FixedSizeAlignedSecBlock<byte, 16, true> m_seed;
0055     FixedSizeAlignedSecBlock<byte, 32> m_key;
0056     member_ptr<BlockCipher> m_pCipher;
0057     bool m_keySet;
0058 };
0059 
0060 /// \brief Randomness Pool based on PGP 2.6.x with MDC

0061 /// \details If you need the pre-Crypto++ 5.5 generator then use OldRandomPool class. The

0062 ///  OldRandomPool also provides the modern interface, including <tt>CanIncorporateEntropy</tt>,

0063 ///  <tt>IncorporateEntropy</tt> and <tt>GenerateIntoBufferedTransformation</tt>.

0064 /// \details You should reseed the generator after a fork() to avoid multiple generators

0065 ///  with the same internal state.

0066 /// \details You should migrate away from OldRandomPool at the earliest opportunity. Use a

0067 ///  modern random number generator or key derivation function, like AutoSeededRandomPool or

0068 ///  HKDF.

0069 /// \warning This class uses an old style PGP 2.6.x with MDC. The generator risks reusing

0070 ///  random numbers after state rollback. You should migrate away from OldRandomPool at

0071 ///  the earliest opportunity.

0072 /// \sa RandomPool, AutoSeededRandomPool, HKDF, P1363_KDF2, PKCS12_PBKDF, PKCS5_PBKDF2_HMAC

0073 /// \since Crypto++ 6.0

0074 class CRYPTOPP_DLL OldRandomPool : public RandomNumberGenerator
0075 {
0076 public:
0077     /// \brief Construct an OldRandomPool

0078     /// \param poolSize internal pool size of the generator

0079     /// \details poolSize must be greater than 16

0080     OldRandomPool(unsigned int poolSize=384);
0081 
0082     // RandomNumberGenerator interface (Crypto++ 5.5 and above)

0083     bool CanIncorporateEntropy() const {return true;}
0084     void IncorporateEntropy(const byte *input, size_t length);
0085     void GenerateIntoBufferedTransformation(BufferedTransformation &target, const std::string &channel, lword size);
0086 
0087     byte GenerateByte();
0088     void GenerateBlock(byte *output, size_t size);
0089 
0090     // GenerateWord32 is overridden and provides Crypto++ 5.4 behavior.

0091     // Taken from RandomNumberSource::GenerateWord32 in cryptlib.cpp.

0092     word32 GenerateWord32 (word32 min=0, word32 max=0xffffffffUL);
0093 
0094 protected:
0095     void Stir();
0096 
0097 private:
0098     SecByteBlock pool, key;
0099     size_t addPos, getPos;
0100 };
0101 
0102 NAMESPACE_END
0103 
0104 #endif