2017-01-27 12:05:45 +00:00
|
|
|
// randpool.h - originally written and placed in the public domain by Wei Dai
|
2017-08-01 22:53:31 +00:00
|
|
|
// OldRandPool added by JW in August, 2017.
|
2016-01-25 04:09:28 +00:00
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \file randpool.h
|
|
|
|
/// \brief Class file for Randomness Pool
|
|
|
|
/// \details RandomPool can be used to generate cryptographic quality pseudorandom bytes
|
|
|
|
/// after seeding the pool with IncorporateEntropy(). Internally, the generator uses
|
|
|
|
/// AES-256 to produce the stream. Entropy is stirred in using SHA-256.
|
|
|
|
/// \details RandomPool used to follow the design of randpool in PGP 2.6.x. At version 5.5
|
|
|
|
/// RandomPool was redesigned to reduce the risk of reusing random numbers after state
|
|
|
|
/// rollback (which may occur when running in a virtual machine like VMware or a hosted
|
|
|
|
/// environment).
|
|
|
|
/// \details If you need the pre-Crypto++ 5.5 generator then use OldRandomPool class. You
|
|
|
|
/// should migrate away from OldRandomPool at the earliest opportunity. Use RandomPool
|
|
|
|
/// or AutoSeededRandomPool instead.
|
|
|
|
/// \since Crypto++ 4.0 (PGP 2.6.x style), Crypto++ 5.5 (AES-256 based)
|
2016-01-25 04:09:28 +00:00
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
#ifndef CRYPTOPP_RANDPOOL_H
|
|
|
|
#define CRYPTOPP_RANDPOOL_H
|
|
|
|
|
|
|
|
#include "cryptlib.h"
|
|
|
|
#include "filters.h"
|
|
|
|
#include "secblock.h"
|
|
|
|
#include "smartptr.h"
|
|
|
|
#include "aes.h"
|
|
|
|
|
|
|
|
NAMESPACE_BEGIN(CryptoPP)
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \class RandomPool
|
|
|
|
/// \brief Randomness Pool based on AES-256
|
|
|
|
/// \details RandomPool can be used to generate cryptographic quality pseudorandom bytes
|
|
|
|
/// after seeding the pool with IncorporateEntropy(). Internally, the generator uses
|
|
|
|
/// AES-256 to produce the stream. Entropy is stirred in using SHA-256.
|
|
|
|
/// \details RandomPool used to follow the design of randpool in PGP 2.6.x. At version 5.5
|
|
|
|
/// RandomPool was redesigned to reduce the risk of reusing random numbers after state
|
|
|
|
/// rollback, which may occur when running in a virtual machine like VMware or a hosted
|
|
|
|
/// environment.
|
|
|
|
/// \details If you need the pre-Crypto++ 5.5 generator then use OldRandomPool class. You
|
|
|
|
/// should migrate away from OldRandomPool at the earliest opportunity.
|
|
|
|
/// \sa OldRandomPool
|
|
|
|
/// \since Crypto++ 4.0 (PGP 2.6.x style), Crypto++ 5.5 (AES-256 based)
|
2015-11-05 06:59:46 +00:00
|
|
|
class CRYPTOPP_DLL RandomPool : public RandomNumberGenerator, public NotCopyable
|
|
|
|
{
|
|
|
|
public:
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Construct a RandomPool
|
2015-11-05 06:59:46 +00:00
|
|
|
RandomPool();
|
|
|
|
|
|
|
|
bool CanIncorporateEntropy() const {return true;}
|
|
|
|
void IncorporateEntropy(const byte *input, size_t length);
|
|
|
|
void GenerateIntoBufferedTransformation(BufferedTransformation &target, const std::string &channel, lword size);
|
|
|
|
|
|
|
|
private:
|
2016-01-25 04:09:28 +00:00
|
|
|
FixedSizeAlignedSecBlock<byte, 16, true> m_seed;
|
2016-01-08 16:02:03 +00:00
|
|
|
FixedSizeAlignedSecBlock<byte, 32> m_key;
|
2015-11-05 06:59:46 +00:00
|
|
|
member_ptr<BlockCipher> m_pCipher;
|
|
|
|
bool m_keySet;
|
|
|
|
};
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \class OldRandomPool
|
|
|
|
/// \brief Randomness Pool based on PGP 2.6.x with MDC
|
|
|
|
/// \details If you need the pre-Crypto++ 5.5 generator then use OldRandomPool class. The
|
|
|
|
/// OldRandomPool class is always available so you dont need to define
|
|
|
|
/// CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY. OldRandomPool also provides the modern
|
|
|
|
/// interface, including <tt>CanIncorporateEntropy</tt>, <tt>IncorporateEntropy</tt> and
|
|
|
|
/// <tt>GenerateIntoBufferedTransformation</tt>.
|
|
|
|
/// \details You should migrate away from OldRandomPool at the earliest opportunity. Use a
|
|
|
|
/// modern random number generator or key derivation function, like AutoSeededRandomPool or
|
|
|
|
/// HKDF.
|
|
|
|
/// \deprecated This class uses an old style PGP 2.6.x with MDC. The generator risks reusing
|
|
|
|
/// random random numbers after state rollback. You should migrate away from OldRandomPool
|
|
|
|
/// at the earliest opportunity.
|
|
|
|
/// \sa RandomPool, AutoSeededRandomPool, HKDF, P1363_KDF2, PKCS12_PBKDF, PKCS5_PBKDF2_HMAC
|
|
|
|
/// \since Crypto++ 6.0 (PGP 2.6.x style)
|
2017-08-02 23:43:56 +00:00
|
|
|
class CRYPTOPP_DLL OldRandomPool : public RandomNumberGenerator
|
2017-08-01 22:53:31 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Construct an OldRandomPool
|
|
|
|
/// \param poolSize internal pool size of the generator
|
|
|
|
/// \details poolSize must be greater than 16
|
2017-08-01 23:58:08 +00:00
|
|
|
OldRandomPool(unsigned int poolSize=384);
|
2017-08-01 22:53:31 +00:00
|
|
|
|
2017-08-02 22:55:10 +00:00
|
|
|
// RandomNumberGenerator interface (Crypto++ 5.5 and above)
|
|
|
|
bool CanIncorporateEntropy() const {return true;}
|
|
|
|
void IncorporateEntropy(const byte *input, size_t length);
|
2017-08-02 23:43:56 +00:00
|
|
|
void GenerateIntoBufferedTransformation(BufferedTransformation &target, const std::string &channel, lword size);
|
2017-08-01 22:53:31 +00:00
|
|
|
|
2017-08-01 23:58:08 +00:00
|
|
|
byte GenerateByte();
|
|
|
|
void GenerateBlock(byte *output, size_t size);
|
2017-08-01 22:53:31 +00:00
|
|
|
|
|
|
|
protected:
|
2017-08-01 23:58:08 +00:00
|
|
|
void Stir();
|
2017-08-01 22:53:31 +00:00
|
|
|
|
|
|
|
private:
|
2017-08-01 23:58:08 +00:00
|
|
|
SecByteBlock pool, key;
|
|
|
|
size_t addPos, getPos;
|
2017-08-01 22:53:31 +00:00
|
|
|
};
|
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
NAMESPACE_END
|
|
|
|
|
|
|
|
#endif
|