Crypto++ 8.1 Free C++ class library of cryptographic schemes

randpool.h
Go to the documentation of this file.
1 // randpool.h - originally written and placed in the public domain by Wei Dai
2 // OldRandPool added by JW in August, 2017.
3
4 /// \file randpool.h
5 /// \brief Class file for Randomness Pool
6 /// \details RandomPool can be used to generate cryptographic quality pseudorandom bytes
7 /// after seeding the pool with IncorporateEntropy(). Internally, the generator uses
8 /// AES-256 to produce the stream. Entropy is stirred in using SHA-256.
9 /// \details RandomPool used to follow the design of randpool in PGP 2.6.x. At version 5.5
10 /// RandomPool was redesigned to reduce the risk of reusing random numbers after state
11 /// rollback (which may occur when running in a virtual machine like VMware or a hosted
12 /// environment).
13 /// \details If you need the pre-Crypto++ 5.5 generator then use OldRandomPool class. You
14 /// should migrate away from OldRandomPool at the earliest opportunity. Use RandomPool
15 /// or AutoSeededRandomPool instead.
16 /// \since Crypto++ 4.0 (PGP 2.6.x style), Crypto++ 5.5 (AES-256 based)
17
18 #ifndef CRYPTOPP_RANDPOOL_H
19 #define CRYPTOPP_RANDPOOL_H
20
21 #include "cryptlib.h"
22 #include "filters.h"
23 #include "secblock.h"
24 #include "smartptr.h"
25 #include "aes.h"
26
27 NAMESPACE_BEGIN(CryptoPP)
28
29 /// \brief Randomness Pool based on AES-256
30 /// \details RandomPool can be used to generate cryptographic quality pseudorandom bytes
31 /// after seeding the pool with IncorporateEntropy(). Internally, the generator uses
32 /// AES-256 to produce the stream. Entropy is stirred in using SHA-256.
33 /// \details RandomPool used to follow the design of randpool in PGP 2.6.x. At version 5.5
34 /// RandomPool was redesigned to reduce the risk of reusing random numbers after state
35 /// rollback, which may occur when running in a virtual machine like VMware or a hosted
36 /// environment.
37 /// \details If you need the pre-Crypto++ 5.5 generator then use OldRandomPool class. You
38 /// should migrate away from OldRandomPool at the earliest opportunity.
39 /// \sa OldRandomPool
40 /// \since Crypto++ 4.0 (PGP 2.6.x style), Crypto++ 5.5 (AES-256 based)
41 class CRYPTOPP_DLL RandomPool : public RandomNumberGenerator, public NotCopyable
42 {
43 public:
44 /// \brief Construct a RandomPool
45 RandomPool();
46
47 bool CanIncorporateEntropy() const {return true;}
48 void IncorporateEntropy(const byte *input, size_t length);
49 void GenerateIntoBufferedTransformation(BufferedTransformation &target, const std::string &channel, lword size);
50
51 private:
52 FixedSizeAlignedSecBlock<byte, 16, true> m_seed;
53 FixedSizeAlignedSecBlock<byte, 32> m_key;
54 member_ptr<BlockCipher> m_pCipher;
55 bool m_keySet;
56 };
57
58 /// \brief Randomness Pool based on PGP 2.6.x with MDC
59 /// \details If you need the pre-Crypto++ 5.5 generator then use OldRandomPool class. The
60 /// OldRandomPool class is always available so you dont need to define
61 /// CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY. OldRandomPool also provides the modern
62 /// interface, including <tt>CanIncorporateEntropy</tt>, <tt>IncorporateEntropy</tt> and
63 /// <tt>GenerateIntoBufferedTransformation</tt>.
64 /// \details You should migrate away from OldRandomPool at the earliest opportunity. Use a
65 /// modern random number generator or key derivation function, like AutoSeededRandomPool or
66 /// HKDF.
67 /// \warning This class uses an old style PGP 2.6.x with MDC. The generator risks reusing
68 /// random random numbers after state rollback. You should migrate away from OldRandomPool
69 /// at the earliest opportunity.
70 /// \sa RandomPool, AutoSeededRandomPool, HKDF, P1363_KDF2, PKCS12_PBKDF, PKCS5_PBKDF2_HMAC
71 /// \since Crypto++ 6.0 (PGP 2.6.x style)
72 class CRYPTOPP_DLL OldRandomPool : public RandomNumberGenerator
73 {
74 public:
75 /// \brief Construct an OldRandomPool
76 /// \param poolSize internal pool size of the generator
77 /// \details poolSize must be greater than 16
78 OldRandomPool(unsigned int poolSize=384);
79
80 // RandomNumberGenerator interface (Crypto++ 5.5 and above)
81 bool CanIncorporateEntropy() const {return true;}
82 void IncorporateEntropy(const byte *input, size_t length);
83 void GenerateIntoBufferedTransformation(BufferedTransformation &target, const std::string &channel, lword size);
84
85 byte GenerateByte();
86 void GenerateBlock(byte *output, size_t size);
87
88 protected:
89 void Stir();
90
91 private:
92 SecByteBlock pool, key;
93 size_t addPos, getPos;
94 };
95
96 NAMESPACE_END
97
98 #endif
RandomPool
Randomness Pool based on AES-256.
Definition: randpool.h:41
RandomNumberGenerator::GenerateBlock
virtual void GenerateBlock(byte *output, size_t size)
Generate random array of bytes.
Definition: cryptlib.cpp:311
OldRandomPool
Randomness Pool based on PGP 2.6.x with MDC.
Definition: randpool.h:72
RandomPool::CanIncorporateEntropy
bool CanIncorporateEntropy() const
Determines if a generator can accept additional entropy.
Definition: randpool.h:47
cryptlib.h
Abstract base classes that provide a uniform interface to this library.
smartptr.h
Classes for automatic resource management.
RandomNumberGenerator
Interface for random number generators.
Definition: cryptlib.h:1383
SecByteBlock
SecBlock<byte> typedef.
Definition: secblock.h:1058
BufferedTransformation
Interface for buffered transformations.
Definition: cryptlib.h:1598
member_ptr< BlockCipher >
secblock.h
Classes and functions for secure memory allocations.
OldRandomPool::CanIncorporateEntropy
bool CanIncorporateEntropy() const
Determines if a generator can accept additional entropy.
Definition: randpool.h:81
aes.h
Class file for the AES cipher (Rijndael)
FixedSizeAlignedSecBlock< byte, 16, true >
RandomNumberGenerator::GenerateByte
virtual byte GenerateByte()
Generate new random byte and return it.
Definition: cryptlib.cpp:276
filters.h
Implementation of BufferedTransformation&#39;s attachment interface.
CryptoPP
Crypto++ library namespace.
NotCopyable
Ensures an object is not copyable.
Definition: misc.h:200
RandomNumberGenerator::GenerateIntoBufferedTransformation
virtual void GenerateIntoBufferedTransformation(BufferedTransformation &target, const std::string &channel, lword length)
Generate random bytes into a BufferedTransformation.
Definition: cryptlib.cpp:324
RandomNumberGenerator::IncorporateEntropy
virtual void IncorporateEntropy(const byte *input, size_t length)
Update RNG state with additional unpredictable values.
Definition: cryptlib.h:1396