2016-12-31 02:46:03 +00:00
|
|
|
// drbg.h - written and placed in public domain by Jeffrey Walton.
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \file drbg.h
|
|
|
|
/// \brief Classes for NIST DRBGs from SP 800-90A
|
|
|
|
/// \sa <A HREF="http://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-90Ar1.pdf">Recommendation
|
2020-04-02 12:29:45 +00:00
|
|
|
/// for Random Number Generation Using Deterministic Random Bit Generators, Rev 1 (June 2015)</A>
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \since Crypto++ 6.0
|
2016-12-31 02:46:03 +00:00
|
|
|
|
2016-12-31 17:28:41 +00:00
|
|
|
#ifndef CRYPTOPP_NIST_DRBG_H
|
|
|
|
#define CRYPTOPP_NIST_DRBG_H
|
2016-12-31 02:46:03 +00:00
|
|
|
|
|
|
|
#include "cryptlib.h"
|
|
|
|
#include "secblock.h"
|
2017-03-07 09:52:37 +00:00
|
|
|
#include "hmac.h"
|
|
|
|
#include "sha.h"
|
2016-12-31 02:46:03 +00:00
|
|
|
|
|
|
|
NAMESPACE_BEGIN(CryptoPP)
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Interface for NIST DRBGs from SP 800-90A
|
|
|
|
/// \details NIST_DRBG is the base class interface for NIST DRBGs from SP 800-90A Rev 1 (June 2015)
|
2020-04-02 12:29:45 +00:00
|
|
|
/// \details You should reseed the generator after a fork() to avoid multiple generators
|
|
|
|
/// with the same internal state.
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \sa <A HREF="http://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-90Ar1.pdf">Recommendation
|
2020-04-02 12:29:45 +00:00
|
|
|
/// for Random Number Generation Using Deterministic Random Bit Generators, Rev 1 (June 2015)</A>
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \since Crypto++ 6.0
|
2016-12-31 02:46:03 +00:00
|
|
|
class NIST_DRBG : public RandomNumberGenerator
|
|
|
|
{
|
2017-01-01 10:53:55 +00:00
|
|
|
public:
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Exception thrown when a NIST DRBG encounters an error
|
2017-02-07 13:51:58 +00:00
|
|
|
class Err : public Exception
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit Err(const std::string &c, const std::string &m)
|
|
|
|
: Exception(OTHER_ERROR, c + ": " + m) {}
|
|
|
|
};
|
2017-01-01 10:53:55 +00:00
|
|
|
|
2016-12-31 02:46:03 +00:00
|
|
|
public:
|
2017-02-07 13:51:58 +00:00
|
|
|
virtual ~NIST_DRBG() {}
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Determines if a generator can accept additional entropy
|
|
|
|
/// \return true
|
|
|
|
/// \details All NIST_DRBG return true
|
2017-02-07 13:51:58 +00:00
|
|
|
virtual bool CanIncorporateEntropy() const {return true;}
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Update RNG state with additional unpredictable values
|
|
|
|
/// \param input the entropy to add to the generator
|
|
|
|
/// \param length the size of the input buffer
|
2020-12-08 04:35:10 +00:00
|
|
|
/// \throw NIST_DRBG::Err if the generator is reseeded with insufficient entropy
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \details NIST instantiation and reseed requirements demand the generator is constructed
|
2020-04-02 12:29:45 +00:00
|
|
|
/// with at least <tt>MINIMUM_ENTROPY</tt> entropy. The byte array for <tt>input</tt> must
|
|
|
|
/// meet <A HREF ="http://csrc.nist.gov/publications/PubsSPs.html">NIST SP 800-90B or
|
|
|
|
/// SP 800-90C</A> requirements.
|
2017-02-07 13:51:58 +00:00
|
|
|
virtual void IncorporateEntropy(const byte *input, size_t length)=0;
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Update RNG state with additional unpredictable values
|
|
|
|
/// \param entropy the entropy to add to the generator
|
|
|
|
/// \param entropyLength the size of the input buffer
|
|
|
|
/// \param additional additional input to add to the generator
|
|
|
|
/// \param additionaLength the size of the additional input buffer
|
2020-12-08 04:35:10 +00:00
|
|
|
/// \throw NIST_DRBG::Err if the generator is reseeded with insufficient entropy
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \details IncorporateEntropy() is an overload provided to match NIST requirements. NIST
|
2020-04-02 12:29:45 +00:00
|
|
|
/// instantiation and reseed requirements demand the generator is constructed with at least
|
|
|
|
/// <tt>MINIMUM_ENTROPY</tt> entropy. The byte array for <tt>entropy</tt> must meet
|
|
|
|
/// <A HREF ="http://csrc.nist.gov/publications/PubsSPs.html">NIST SP 800-90B or
|
2017-11-29 15:54:33 +00:00
|
|
|
///! SP 800-90C</A> requirements.
|
2017-02-07 13:51:58 +00:00
|
|
|
virtual void IncorporateEntropy(const byte *entropy, size_t entropyLength, const byte* additional, size_t additionaLength)=0;
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Generate random array of bytes
|
|
|
|
/// \param output the byte buffer
|
|
|
|
/// \param size the length of the buffer, in bytes
|
2020-12-08 04:35:10 +00:00
|
|
|
/// \throw NIST_DRBG::Err if a reseed is required
|
|
|
|
/// \throw NIST_DRBG::Err if the size exceeds <tt>MAXIMUM_BYTES_PER_REQUEST</tt>
|
2017-02-07 13:51:58 +00:00
|
|
|
virtual void GenerateBlock(byte *output, size_t size)=0;
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Generate random array of bytes
|
|
|
|
/// \param additional additional input to add to the generator
|
|
|
|
/// \param additionaLength the size of the additional input buffer
|
|
|
|
/// \param output the byte buffer
|
|
|
|
/// \param size the length of the buffer, in bytes
|
2020-12-08 04:35:10 +00:00
|
|
|
/// \throw NIST_DRBG::Err if a reseed is required
|
|
|
|
/// \throw NIST_DRBG::Err if the size exceeds <tt>MAXIMUM_BYTES_PER_REQUEST</tt>
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \details GenerateBlock() is an overload provided to match NIST requirements. The byte
|
2020-04-02 12:29:45 +00:00
|
|
|
/// array for <tt>additional</tt> input is optional. If present the additional randomness
|
|
|
|
/// is mixed before generating the output bytes.
|
2017-02-07 13:51:58 +00:00
|
|
|
virtual void GenerateBlock(const byte* additional, size_t additionaLength, byte *output, size_t size)=0;
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Provides the security strength
|
2020-12-08 04:35:10 +00:00
|
|
|
/// \return The security strength of the generator, in bytes
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \details The equivalent class constant is <tt>SECURITY_STRENGTH</tt>
|
2017-05-29 06:44:55 +00:00
|
|
|
virtual unsigned int SecurityStrength() const=0;
|
2017-02-07 13:51:58 +00:00
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Provides the seed length
|
2020-12-08 04:35:10 +00:00
|
|
|
/// \return The seed size of the generator, in bytes
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \details The equivalent class constant is <tt>SEED_LENGTH</tt>. The size is
|
2020-04-02 12:29:45 +00:00
|
|
|
/// used to maintain internal state of <tt>V</tt> and <tt>C</tt>.
|
2017-05-29 06:44:55 +00:00
|
|
|
virtual unsigned int SeedLength() const=0;
|
2017-02-07 13:51:58 +00:00
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Provides the minimum entropy size
|
2020-12-08 04:35:10 +00:00
|
|
|
/// \return The minimum entropy size required by the generator, in bytes
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \details The equivalent class constant is <tt>MINIMUM_ENTROPY</tt>. All NIST DRBGs must
|
2020-04-02 12:29:45 +00:00
|
|
|
/// be instaniated with at least <tt>MINIMUM_ENTROPY</tt> bytes of entropy. The bytes must
|
|
|
|
/// meet <A HREF="http://csrc.nist.gov/publications/PubsSPs.html">NIST SP 800-90B or
|
|
|
|
/// SP 800-90C</A> requirements.
|
2017-05-29 06:44:55 +00:00
|
|
|
virtual unsigned int MinEntropyLength() const=0;
|
2017-02-07 13:51:58 +00:00
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Provides the maximum entropy size
|
2020-12-08 04:35:10 +00:00
|
|
|
/// \return The maximum entropy size that can be consumed by the generator, in bytes
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \details The equivalent class constant is <tt>MAXIMUM_ENTROPY</tt>. The bytes must
|
2020-04-02 12:29:45 +00:00
|
|
|
/// meet <A HREF="http://csrc.nist.gov/publications/PubsSPs.html">NIST SP 800-90B or
|
|
|
|
/// SP 800-90C</A> requirements. <tt>MAXIMUM_ENTROPY</tt> has been reduced from
|
|
|
|
/// 2<sup>35</sup> to <tt>INT_MAX</tt> to fit the underlying C++ datatype.
|
2017-05-29 06:44:55 +00:00
|
|
|
virtual unsigned int MaxEntropyLength() const=0;
|
2017-02-07 13:51:58 +00:00
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Provides the minimum nonce size
|
2020-12-08 04:35:10 +00:00
|
|
|
/// \return The minimum nonce size recommended for the generator, in bytes
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \details The equivalent class constant is <tt>MINIMUM_NONCE</tt>. If a nonce is not
|
2020-04-02 12:29:45 +00:00
|
|
|
/// required then <tt>MINIMUM_NONCE</tt> is 0. <tt>Hash_DRBG</tt> does not require a
|
|
|
|
/// nonce, while <tt>HMAC_DRBG</tt> and <tt>CTR_DRBG</tt> require a nonce.
|
2017-05-29 06:44:55 +00:00
|
|
|
virtual unsigned int MinNonceLength() const=0;
|
2017-02-07 13:51:58 +00:00
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Provides the maximum nonce size
|
2020-12-08 04:35:10 +00:00
|
|
|
/// \return The maximum nonce that can be consumed by the generator, in bytes
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \details The equivalent class constant is <tt>MAXIMUM_NONCE</tt>. <tt>MAXIMUM_NONCE</tt>
|
2020-04-02 12:29:45 +00:00
|
|
|
/// has been reduced from 2<sup>35</sup> to <tt>INT_MAX</tt> to fit the underlying C++ datatype.
|
|
|
|
/// If a nonce is not required then <tt>MINIMUM_NONCE</tt> is 0. <tt>Hash_DRBG</tt> does not
|
|
|
|
/// require a nonce, while <tt>HMAC_DRBG</tt> and <tt>CTR_DRBG</tt> require a nonce.
|
2017-05-29 06:44:55 +00:00
|
|
|
virtual unsigned int MaxNonceLength() const=0;
|
2017-02-07 13:51:58 +00:00
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Provides the maximum size of a request to GenerateBlock
|
2021-03-05 03:49:25 +00:00
|
|
|
/// \return The maximum size of a request to GenerateBlock(), in bytes
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \details The equivalent class constant is <tt>MAXIMUM_BYTES_PER_REQUEST</tt>
|
2017-05-29 06:44:55 +00:00
|
|
|
virtual unsigned int MaxBytesPerRequest() const=0;
|
2017-02-07 13:51:58 +00:00
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Provides the maximum number of requests before a reseed
|
2021-03-05 03:49:25 +00:00
|
|
|
/// \return The maximum number of requests before a reseed, in bytes
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \details The equivalent class constant is <tt>MAXIMUM_REQUESTS_BEFORE_RESEED</tt>.
|
2020-04-02 12:29:45 +00:00
|
|
|
/// <tt>MAXIMUM_REQUESTS_BEFORE_RESEED</tt> has been reduced from 2<sup>48</sup> to <tt>INT_MAX</tt>
|
|
|
|
/// to fit the underlying C++ datatype.
|
2017-05-29 06:44:55 +00:00
|
|
|
virtual unsigned int MaxRequestBeforeReseed() const=0;
|
2016-12-31 17:28:41 +00:00
|
|
|
|
|
|
|
protected:
|
2017-02-07 13:51:58 +00:00
|
|
|
virtual void DRBG_Instantiate(const byte* entropy, size_t entropyLength,
|
|
|
|
const byte* nonce, size_t nonceLength, const byte* personalization, size_t personalizationLength)=0;
|
2016-12-31 02:46:03 +00:00
|
|
|
|
2017-02-07 13:51:58 +00:00
|
|
|
virtual void DRBG_Reseed(const byte* entropy, size_t entropyLength, const byte* additional, size_t additionaLength)=0;
|
2016-12-31 02:46:03 +00:00
|
|
|
};
|
|
|
|
|
2017-02-07 13:51:58 +00:00
|
|
|
// *************************************************************
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \tparam HASH NIST approved hash derived from HashTransformation
|
|
|
|
/// \tparam STRENGTH security strength, in bytes
|
|
|
|
/// \tparam SEEDLENGTH seed length, in bytes
|
|
|
|
/// \brief Hash_DRBG from SP 800-90A Rev 1 (June 2015)
|
|
|
|
/// \details The NIST Hash DRBG is instantiated with a number of parameters. Two of the parameters,
|
2020-04-02 12:29:45 +00:00
|
|
|
/// Security Strength and Seed Length, depend on the hash and are specified as template parameters.
|
|
|
|
/// The remaining parameters are included in the class. The parameters and their values are listed
|
|
|
|
/// in NIST SP 800-90A Rev. 1, Table 2: Definitions for Hash-Based DRBG Mechanisms (p.38).
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \details Some parameters have been reduce to fit C++ datatypes. For example, NIST allows upto
|
2020-04-02 12:29:45 +00:00
|
|
|
/// 2<sup>48</sup> requests before a reseed. However, Hash_DRBG limits it to <tt>INT_MAX</tt> due
|
|
|
|
/// to the limited data range of an int.
|
|
|
|
/// \details You should reseed the generator after a fork() to avoid multiple generators
|
|
|
|
/// with the same internal state.
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \sa <A HREF="http://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-90Ar1.pdf">Recommendation
|
2020-04-02 12:29:45 +00:00
|
|
|
/// for Random Number Generation Using Deterministic Random Bit Generators, Rev 1 (June 2015)</A>
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \since Crypto++ 6.0
|
2016-12-31 02:46:03 +00:00
|
|
|
template <typename HASH=SHA256, unsigned int STRENGTH=128/8, unsigned int SEEDLENGTH=440/8>
|
2017-02-07 13:51:58 +00:00
|
|
|
class Hash_DRBG : public NIST_DRBG, public NotCopyable
|
2016-12-31 02:46:03 +00:00
|
|
|
{
|
|
|
|
public:
|
2019-10-14 16:30:10 +00:00
|
|
|
CRYPTOPP_CONSTANT(SECURITY_STRENGTH=STRENGTH);
|
|
|
|
CRYPTOPP_CONSTANT(SEED_LENGTH=SEEDLENGTH);
|
|
|
|
CRYPTOPP_CONSTANT(MINIMUM_ENTROPY=STRENGTH);
|
|
|
|
CRYPTOPP_CONSTANT(MINIMUM_NONCE=0);
|
|
|
|
CRYPTOPP_CONSTANT(MINIMUM_ADDITIONAL=0);
|
|
|
|
CRYPTOPP_CONSTANT(MINIMUM_PERSONALIZATION=0);
|
|
|
|
CRYPTOPP_CONSTANT(MAXIMUM_ENTROPY=INT_MAX);
|
|
|
|
CRYPTOPP_CONSTANT(MAXIMUM_NONCE=INT_MAX);
|
|
|
|
CRYPTOPP_CONSTANT(MAXIMUM_ADDITIONAL=INT_MAX);
|
|
|
|
CRYPTOPP_CONSTANT(MAXIMUM_PERSONALIZATION=INT_MAX);
|
|
|
|
CRYPTOPP_CONSTANT(MAXIMUM_BYTES_PER_REQUEST=65536);
|
|
|
|
CRYPTOPP_CONSTANT(MAXIMUM_REQUESTS_BEFORE_RESEED=INT_MAX);
|
2017-02-07 13:51:58 +00:00
|
|
|
|
2017-03-07 09:52:37 +00:00
|
|
|
static std::string StaticAlgorithmName() { return std::string("Hash_DRBG(") + HASH::StaticAlgorithmName() + std::string(")"); }
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Construct a Hash DRBG
|
|
|
|
/// \param entropy the entropy to instantiate the generator
|
|
|
|
/// \param entropyLength the size of the entropy buffer
|
|
|
|
/// \param nonce additional input to instantiate the generator
|
|
|
|
/// \param nonceLength the size of the nonce buffer
|
|
|
|
/// \param personalization additional input to instantiate the generator
|
|
|
|
/// \param personalizationLength the size of the personalization buffer
|
2020-12-08 04:35:10 +00:00
|
|
|
/// \throw NIST_DRBG::Err if the generator is instantiated with insufficient entropy
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \details All NIST DRBGs must be instaniated with at least <tt>MINIMUM_ENTROPY</tt> bytes of entropy.
|
2020-04-02 12:29:45 +00:00
|
|
|
/// The byte array for <tt>entropy</tt> must meet <A HREF ="http://csrc.nist.gov/publications/PubsSPs.html">NIST
|
|
|
|
/// SP 800-90B or SP 800-90C</A> requirements.
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \details The <tt>nonce</tt> and <tt>personalization</tt> are optional byte arrays. If <tt>nonce</tt> is supplied,
|
2020-04-02 12:29:45 +00:00
|
|
|
/// then it should be at least <tt>MINIMUM_NONCE</tt> bytes of entropy.
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \details An example of instantiating a SHA256 generator is shown below.
|
2020-04-02 12:29:45 +00:00
|
|
|
/// The example provides more entropy than required for SHA256. The <tt>NonblockingRng</tt> meets the
|
|
|
|
/// requirements of <A HREF ="http://csrc.nist.gov/publications/PubsSPs.html">NIST SP 800-90B or SP 800-90C</A>.
|
|
|
|
/// RDRAND() and RDSEED() generators would work as well.
|
2017-11-29 15:54:33 +00:00
|
|
|
/// <pre>
|
2020-04-02 12:29:45 +00:00
|
|
|
/// SecByteBlock entropy(48), result(128);
|
|
|
|
/// NonblockingRng prng;
|
|
|
|
/// RandomNumberSource rns(prng, entropy.size(), new ArraySink(entropy, entropy.size()));
|
2017-11-29 15:54:33 +00:00
|
|
|
///
|
2020-04-02 12:29:45 +00:00
|
|
|
/// Hash_DRBG<SHA256, 128/8, 440/8> drbg(entropy, 32, entropy+32, 16);
|
|
|
|
/// drbg.GenerateBlock(result, result.size());
|
2017-11-29 15:54:33 +00:00
|
|
|
/// </pre>
|
2017-03-08 21:59:24 +00:00
|
|
|
Hash_DRBG(const byte* entropy=NULLPTR, size_t entropyLength=STRENGTH, const byte* nonce=NULLPTR,
|
2017-03-01 11:10:06 +00:00
|
|
|
size_t nonceLength=0, const byte* personalization=NULLPTR, size_t personalizationLength=0)
|
2017-03-18 08:51:43 +00:00
|
|
|
: NIST_DRBG(), m_c(SEEDLENGTH), m_v(SEEDLENGTH), m_reseed(0)
|
2017-02-07 13:51:58 +00:00
|
|
|
{
|
2020-07-16 19:37:09 +00:00
|
|
|
if (m_c.data()) // GCC analyzer warning
|
|
|
|
std::memset(m_c.data(), 0x00, m_c.size());
|
|
|
|
if (m_v.data()) // GCC analyzer warning
|
|
|
|
std::memset(m_v.data(), 0x00, m_v.size());
|
2019-05-25 07:41:05 +00:00
|
|
|
|
2017-03-08 21:59:24 +00:00
|
|
|
if (entropy != NULLPTR && entropyLength != 0)
|
|
|
|
DRBG_Instantiate(entropy, entropyLength, nonce, nonceLength, personalization, personalizationLength);
|
2017-02-07 13:51:58 +00:00
|
|
|
}
|
|
|
|
|
2017-05-29 06:44:55 +00:00
|
|
|
unsigned int SecurityStrength() const {return SECURITY_STRENGTH;}
|
|
|
|
unsigned int SeedLength() const {return SEED_LENGTH;}
|
|
|
|
unsigned int MinEntropyLength() const {return MINIMUM_ENTROPY;}
|
|
|
|
unsigned int MaxEntropyLength() const {return MAXIMUM_ENTROPY;}
|
|
|
|
unsigned int MinNonceLength() const {return MINIMUM_NONCE;}
|
|
|
|
unsigned int MaxNonceLength() const {return MAXIMUM_NONCE;}
|
|
|
|
unsigned int MaxBytesPerRequest() const {return MAXIMUM_BYTES_PER_REQUEST;}
|
|
|
|
unsigned int MaxRequestBeforeReseed() const {return MAXIMUM_REQUESTS_BEFORE_RESEED;}
|
2017-02-07 13:51:58 +00:00
|
|
|
|
|
|
|
void IncorporateEntropy(const byte *input, size_t length)
|
2017-03-01 11:10:06 +00:00
|
|
|
{return DRBG_Reseed(input, length, NULLPTR, 0);}
|
2017-02-07 13:51:58 +00:00
|
|
|
|
|
|
|
void IncorporateEntropy(const byte *entropy, size_t entropyLength, const byte* additional, size_t additionaLength)
|
|
|
|
{return DRBG_Reseed(entropy, entropyLength, additional, additionaLength);}
|
|
|
|
|
|
|
|
void GenerateBlock(byte *output, size_t size)
|
2017-03-01 11:10:06 +00:00
|
|
|
{return Hash_Generate(NULLPTR, 0, output, size);}
|
2017-02-07 13:51:58 +00:00
|
|
|
|
|
|
|
void GenerateBlock(const byte* additional, size_t additionaLength, byte *output, size_t size)
|
|
|
|
{return Hash_Generate(additional, additionaLength, output, size);}
|
2016-12-31 02:46:03 +00:00
|
|
|
|
2018-07-06 13:23:37 +00:00
|
|
|
std::string AlgorithmProvider() const
|
|
|
|
{/*Hack*/HASH hash; return hash.AlgorithmProvider();}
|
|
|
|
|
2016-12-31 02:46:03 +00:00
|
|
|
protected:
|
2017-02-07 13:51:58 +00:00
|
|
|
// 10.1.1.2 Instantiation of Hash_DRBG (p.39)
|
|
|
|
void DRBG_Instantiate(const byte* entropy, size_t entropyLength, const byte* nonce, size_t nonceLength,
|
|
|
|
const byte* personalization, size_t personalizationLength);
|
|
|
|
|
|
|
|
// 10.1.1.3 Reseeding a Hash_DRBG Instantiation (p.40)
|
|
|
|
void DRBG_Reseed(const byte* entropy, size_t entropyLength, const byte* additional, size_t additionaLength);
|
|
|
|
|
|
|
|
// 10.1.1.4 Generating Pseudorandom Bits Using Hash_DRBG (p.41)
|
|
|
|
void Hash_Generate(const byte* additional, size_t additionaLength, byte *output, size_t size);
|
|
|
|
|
|
|
|
// 10.3.1 Derivation Function Using a Hash Function (Hash_df) (p.49)
|
|
|
|
void Hash_Update(const byte* input1, size_t inlen1, const byte* input2, size_t inlen2,
|
|
|
|
const byte* input3, size_t inlen3, const byte* input4, size_t inlen4, byte* output, size_t outlen);
|
2016-12-31 02:46:03 +00:00
|
|
|
|
|
|
|
private:
|
2018-08-13 13:54:07 +00:00
|
|
|
HASH m_hash;
|
|
|
|
SecByteBlock m_c, m_v, m_temp;
|
2017-02-07 13:51:58 +00:00
|
|
|
word64 m_reseed;
|
2016-12-31 02:46:03 +00:00
|
|
|
};
|
|
|
|
|
2017-02-07 13:51:58 +00:00
|
|
|
// typedef Hash_DRBG<SHA1, 128/8, 440/8> Hash_SHA1_DRBG;
|
|
|
|
// typedef Hash_DRBG<SHA256, 128/8, 440/8> Hash_SHA256_DRBG;
|
|
|
|
// typedef Hash_DRBG<SHA384, 256/8, 888/8> Hash_SHA384_DRBG;
|
|
|
|
// typedef Hash_DRBG<SHA512, 256/8, 888/8> Hash_SHA512_DRBG;
|
|
|
|
|
|
|
|
// *************************************************************
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \tparam HASH NIST approved hash derived from HashTransformation
|
|
|
|
/// \tparam STRENGTH security strength, in bytes
|
|
|
|
/// \tparam SEEDLENGTH seed length, in bytes
|
|
|
|
/// \brief HMAC_DRBG from SP 800-90A Rev 1 (June 2015)
|
|
|
|
/// \details The NIST HMAC DRBG is instantiated with a number of parameters. Two of the parameters,
|
2020-04-02 12:29:45 +00:00
|
|
|
/// Security Strength and Seed Length, depend on the hash and are specified as template parameters.
|
|
|
|
/// The remaining parameters are included in the class. The parameters and their values are listed
|
|
|
|
/// in NIST SP 800-90A Rev. 1, Table 2: Definitions for Hash-Based DRBG Mechanisms (p.38).
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \details Some parameters have been reduce to fit C++ datatypes. For example, NIST allows upto 2<sup>48</sup> requests
|
2020-04-02 12:29:45 +00:00
|
|
|
/// before a reseed. However, HMAC_DRBG limits it to <tt>INT_MAX</tt> due to the limited data range of an int.
|
|
|
|
/// \details You should reseed the generator after a fork() to avoid multiple generators
|
|
|
|
/// with the same internal state.
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \sa <A HREF="http://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-90Ar1.pdf">Recommendation
|
2020-04-02 12:29:45 +00:00
|
|
|
/// for Random Number Generation Using Deterministic Random Bit Generators, Rev 1 (June 2015)</A>
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \since Crypto++ 6.0
|
2017-02-07 13:51:58 +00:00
|
|
|
template <typename HASH=SHA256, unsigned int STRENGTH=128/8, unsigned int SEEDLENGTH=440/8>
|
|
|
|
class HMAC_DRBG : public NIST_DRBG, public NotCopyable
|
|
|
|
{
|
|
|
|
public:
|
2019-10-14 16:30:10 +00:00
|
|
|
CRYPTOPP_CONSTANT(SECURITY_STRENGTH=STRENGTH);
|
|
|
|
CRYPTOPP_CONSTANT(SEED_LENGTH=SEEDLENGTH);
|
|
|
|
CRYPTOPP_CONSTANT(MINIMUM_ENTROPY=STRENGTH);
|
|
|
|
CRYPTOPP_CONSTANT(MINIMUM_NONCE=0);
|
|
|
|
CRYPTOPP_CONSTANT(MINIMUM_ADDITIONAL=0);
|
|
|
|
CRYPTOPP_CONSTANT(MINIMUM_PERSONALIZATION=0);
|
|
|
|
CRYPTOPP_CONSTANT(MAXIMUM_ENTROPY=INT_MAX);
|
|
|
|
CRYPTOPP_CONSTANT(MAXIMUM_NONCE=INT_MAX);
|
|
|
|
CRYPTOPP_CONSTANT(MAXIMUM_ADDITIONAL=INT_MAX);
|
|
|
|
CRYPTOPP_CONSTANT(MAXIMUM_PERSONALIZATION=INT_MAX);
|
|
|
|
CRYPTOPP_CONSTANT(MAXIMUM_BYTES_PER_REQUEST=65536);
|
|
|
|
CRYPTOPP_CONSTANT(MAXIMUM_REQUESTS_BEFORE_RESEED=INT_MAX);
|
2017-02-07 13:51:58 +00:00
|
|
|
|
2017-03-07 09:52:37 +00:00
|
|
|
static std::string StaticAlgorithmName() { return std::string("HMAC_DRBG(") + HASH::StaticAlgorithmName() + std::string(")"); }
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Construct a HMAC DRBG
|
|
|
|
/// \param entropy the entropy to instantiate the generator
|
|
|
|
/// \param entropyLength the size of the entropy buffer
|
|
|
|
/// \param nonce additional input to instantiate the generator
|
|
|
|
/// \param nonceLength the size of the nonce buffer
|
|
|
|
/// \param personalization additional input to instantiate the generator
|
|
|
|
/// \param personalizationLength the size of the personalization buffer
|
2020-12-08 04:35:10 +00:00
|
|
|
/// \throw NIST_DRBG::Err if the generator is instantiated with insufficient entropy
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \details All NIST DRBGs must be instaniated with at least <tt>MINIMUM_ENTROPY</tt> bytes of entropy.
|
2020-04-02 12:29:45 +00:00
|
|
|
/// The byte array for <tt>entropy</tt> must meet <A HREF ="http://csrc.nist.gov/publications/PubsSPs.html">NIST
|
|
|
|
/// SP 800-90B or SP 800-90C</A> requirements.
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \details The <tt>nonce</tt> and <tt>personalization</tt> are optional byte arrays. If <tt>nonce</tt> is supplied,
|
2020-04-02 12:29:45 +00:00
|
|
|
/// then it should be at least <tt>MINIMUM_NONCE</tt> bytes of entropy.
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \details An example of instantiating a SHA256 generator is shown below.
|
2020-04-02 12:29:45 +00:00
|
|
|
/// The example provides more entropy than required for SHA256. The <tt>NonblockingRng</tt> meets the
|
|
|
|
/// requirements of <A HREF ="http://csrc.nist.gov/publications/PubsSPs.html">NIST SP 800-90B or SP 800-90C</A>.
|
|
|
|
/// RDRAND() and RDSEED() generators would work as well.
|
2017-11-29 15:54:33 +00:00
|
|
|
/// <pre>
|
2020-04-02 12:29:45 +00:00
|
|
|
/// SecByteBlock entropy(48), result(128);
|
|
|
|
/// NonblockingRng prng;
|
|
|
|
/// RandomNumberSource rns(prng, entropy.size(), new ArraySink(entropy, entropy.size()));
|
2017-11-29 15:54:33 +00:00
|
|
|
///
|
2020-04-02 12:29:45 +00:00
|
|
|
/// HMAC_DRBG<SHA256, 128/8, 440/8> drbg(entropy, 32, entropy+32, 16);
|
|
|
|
/// drbg.GenerateBlock(result, result.size());
|
2017-11-29 15:54:33 +00:00
|
|
|
/// </pre>
|
2017-03-08 21:59:24 +00:00
|
|
|
HMAC_DRBG(const byte* entropy=NULLPTR, size_t entropyLength=STRENGTH, const byte* nonce=NULLPTR,
|
2017-03-01 11:10:06 +00:00
|
|
|
size_t nonceLength=0, const byte* personalization=NULLPTR, size_t personalizationLength=0)
|
2017-03-18 08:51:43 +00:00
|
|
|
: NIST_DRBG(), m_k(HASH::DIGESTSIZE), m_v(HASH::DIGESTSIZE), m_reseed(0)
|
2017-02-07 13:51:58 +00:00
|
|
|
{
|
2020-07-16 19:37:09 +00:00
|
|
|
if (m_k.data()) // GCC analyzer warning
|
|
|
|
std::memset(m_k, 0x00, m_k.size());
|
|
|
|
if (m_v.data()) // GCC analyzer warning
|
|
|
|
std::memset(m_v, 0x00, m_v.size());
|
2019-05-25 07:41:05 +00:00
|
|
|
|
2017-03-08 21:59:24 +00:00
|
|
|
if (entropy != NULLPTR && entropyLength != 0)
|
|
|
|
DRBG_Instantiate(entropy, entropyLength, nonce, nonceLength, personalization, personalizationLength);
|
2017-02-07 13:51:58 +00:00
|
|
|
}
|
|
|
|
|
2017-05-29 06:44:55 +00:00
|
|
|
unsigned int SecurityStrength() const {return SECURITY_STRENGTH;}
|
|
|
|
unsigned int SeedLength() const {return SEED_LENGTH;}
|
|
|
|
unsigned int MinEntropyLength() const {return MINIMUM_ENTROPY;}
|
|
|
|
unsigned int MaxEntropyLength() const {return MAXIMUM_ENTROPY;}
|
|
|
|
unsigned int MinNonceLength() const {return MINIMUM_NONCE;}
|
|
|
|
unsigned int MaxNonceLength() const {return MAXIMUM_NONCE;}
|
|
|
|
unsigned int MaxBytesPerRequest() const {return MAXIMUM_BYTES_PER_REQUEST;}
|
|
|
|
unsigned int MaxRequestBeforeReseed() const {return MAXIMUM_REQUESTS_BEFORE_RESEED;}
|
2017-02-07 13:51:58 +00:00
|
|
|
|
|
|
|
void IncorporateEntropy(const byte *input, size_t length)
|
2017-03-01 11:10:06 +00:00
|
|
|
{return DRBG_Reseed(input, length, NULLPTR, 0);}
|
2017-02-07 13:51:58 +00:00
|
|
|
|
|
|
|
void IncorporateEntropy(const byte *entropy, size_t entropyLength, const byte* additional, size_t additionaLength)
|
|
|
|
{return DRBG_Reseed(entropy, entropyLength, additional, additionaLength);}
|
|
|
|
|
|
|
|
void GenerateBlock(byte *output, size_t size)
|
2017-03-01 11:10:06 +00:00
|
|
|
{return HMAC_Generate(NULLPTR, 0, output, size);}
|
2017-02-07 13:51:58 +00:00
|
|
|
|
|
|
|
void GenerateBlock(const byte* additional, size_t additionaLength, byte *output, size_t size)
|
|
|
|
{return HMAC_Generate(additional, additionaLength, output, size);}
|
|
|
|
|
2018-07-06 13:23:37 +00:00
|
|
|
std::string AlgorithmProvider() const
|
|
|
|
{/*Hack*/HASH hash; return hash.AlgorithmProvider();}
|
|
|
|
|
2017-02-07 13:51:58 +00:00
|
|
|
protected:
|
|
|
|
// 10.1.2.3 Instantiation of HMAC_DRBG (p.45)
|
|
|
|
void DRBG_Instantiate(const byte* entropy, size_t entropyLength, const byte* nonce, size_t nonceLength,
|
|
|
|
const byte* personalization, size_t personalizationLength);
|
|
|
|
|
|
|
|
// 10.1.2.4 Reseeding a HMAC_DRBG Instantiation (p.46)
|
|
|
|
void DRBG_Reseed(const byte* entropy, size_t entropyLength, const byte* additional, size_t additionaLength);
|
|
|
|
|
|
|
|
// 10.1.2.5 Generating Pseudorandom Bits Using HMAC_DRBG (p.46)
|
|
|
|
void HMAC_Generate(const byte* additional, size_t additionaLength, byte *output, size_t size);
|
|
|
|
|
|
|
|
// 10.1.2.2 Derivation Function Using a HMAC Function (HMAC_Update) (p.44)
|
|
|
|
void HMAC_Update(const byte* input1, size_t inlen1, const byte* input2, size_t inlen2, const byte* input3, size_t inlen3);
|
|
|
|
|
|
|
|
private:
|
2018-08-13 13:54:07 +00:00
|
|
|
HMAC<HASH> m_hmac;
|
2017-02-07 13:51:58 +00:00
|
|
|
SecByteBlock m_k, m_v;
|
|
|
|
word64 m_reseed;
|
|
|
|
};
|
|
|
|
|
|
|
|
// typedef HMAC_DRBG<SHA1, 128/8, 440/8> HMAC_SHA1_DRBG;
|
|
|
|
// typedef HMAC_DRBG<SHA256, 128/8, 440/8> HMAC_SHA256_DRBG;
|
|
|
|
// typedef HMAC_DRBG<SHA384, 256/8, 888/8> HMAC_SHA384_DRBG;
|
|
|
|
// typedef HMAC_DRBG<SHA512, 256/8, 888/8> HMAC_SHA512_DRBG;
|
|
|
|
|
|
|
|
// *************************************************************
|
|
|
|
|
|
|
|
// 10.1.1.2 Instantiation of Hash_DRBG (p.39)
|
|
|
|
template <typename HASH, unsigned int STRENGTH, unsigned int SEEDLENGTH>
|
|
|
|
void Hash_DRBG<HASH, STRENGTH, SEEDLENGTH>::DRBG_Instantiate(const byte* entropy, size_t entropyLength, const byte* nonce, size_t nonceLength,
|
|
|
|
const byte* personalization, size_t personalizationLength)
|
|
|
|
{
|
|
|
|
// SP 800-90A, 8.6.3: The entropy input shall have entropy that is equal to or greater than the security
|
|
|
|
// strength of the instantiation. Additional entropy may be provided in the nonce or the optional
|
|
|
|
// personalization string during instantiation, or in the additional input during reseeding and generation,
|
|
|
|
// but this is not required and does not increase the "official" security strength of the DRBG
|
|
|
|
// instantiation that is recorded in the internal state.
|
|
|
|
CRYPTOPP_ASSERT(entropyLength >= MINIMUM_ENTROPY);
|
|
|
|
if (entropyLength < MINIMUM_ENTROPY)
|
|
|
|
throw NIST_DRBG::Err("Hash_DRBG", "Insufficient entropy during instantiate");
|
|
|
|
|
|
|
|
// SP 800-90A, Section 9, says we should throw if we have too much entropy, too large a nonce,
|
|
|
|
// or too large a persoanlization string. We warn in Debug builds, but do nothing in Release builds.
|
|
|
|
CRYPTOPP_ASSERT(entropyLength <= MAXIMUM_ENTROPY);
|
|
|
|
CRYPTOPP_ASSERT(nonceLength <= MAXIMUM_NONCE);
|
|
|
|
CRYPTOPP_ASSERT(personalizationLength <= MAXIMUM_PERSONALIZATION);
|
|
|
|
|
|
|
|
const byte zero = 0;
|
|
|
|
SecByteBlock t1(SEEDLENGTH), t2(SEEDLENGTH);
|
2017-03-01 11:10:06 +00:00
|
|
|
Hash_Update(entropy, entropyLength, nonce, nonceLength, personalization, personalizationLength, NULLPTR, 0, t1, t1.size());
|
|
|
|
Hash_Update(&zero, 1, t1, t1.size(), NULLPTR, 0, NULLPTR, 0, t2, t2.size());
|
2017-02-07 13:51:58 +00:00
|
|
|
|
|
|
|
m_v.swap(t1); m_c.swap(t2);
|
|
|
|
m_reseed = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 10.1.1.3 Reseeding a Hash_DRBG Instantiation (p.40)
|
|
|
|
template <typename HASH, unsigned int STRENGTH, unsigned int SEEDLENGTH>
|
|
|
|
void Hash_DRBG<HASH, STRENGTH, SEEDLENGTH>::DRBG_Reseed(const byte* entropy, size_t entropyLength, const byte* additional, size_t additionaLength)
|
|
|
|
{
|
|
|
|
// SP 800-90A, 8.6.3: The entropy input shall have entropy that is equal to or greater than the security
|
|
|
|
// strength of the instantiation. Additional entropy may be provided in the nonce or the optional
|
|
|
|
// personalization string during instantiation, or in the additional input during reseeding and generation,
|
|
|
|
// but this is not required and does not increase the "official" security strength of the DRBG
|
|
|
|
// instantiation that is recorded in the internal state..
|
|
|
|
CRYPTOPP_ASSERT(entropyLength >= MINIMUM_ENTROPY);
|
|
|
|
if (entropyLength < MINIMUM_ENTROPY)
|
|
|
|
throw NIST_DRBG::Err("Hash_DRBG", "Insufficient entropy during reseed");
|
|
|
|
|
|
|
|
// SP 800-90A, Section 9, says we should throw if we have too much entropy, too large a nonce,
|
|
|
|
// or too large a persoanlization string. We warn in Debug builds, but do nothing in Release builds.
|
|
|
|
CRYPTOPP_ASSERT(entropyLength <= MAXIMUM_ENTROPY);
|
|
|
|
CRYPTOPP_ASSERT(additionaLength <= MAXIMUM_ADDITIONAL);
|
|
|
|
|
|
|
|
const byte zero = 0, one = 1;
|
|
|
|
SecByteBlock t1(SEEDLENGTH), t2(SEEDLENGTH);
|
|
|
|
Hash_Update(&one, 1, m_v, m_v.size(), entropy, entropyLength, additional, additionaLength, t1, t1.size());
|
2017-03-01 11:10:06 +00:00
|
|
|
Hash_Update(&zero, 1, t1, t1.size(), NULLPTR, 0, NULLPTR, 0, t2, t2.size());
|
2017-02-07 13:51:58 +00:00
|
|
|
|
|
|
|
m_v.swap(t1); m_c.swap(t2);
|
|
|
|
m_reseed = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 10.1.1.4 Generating Pseudorandom Bits Using Hash_DRBG (p.41)
|
|
|
|
template <typename HASH, unsigned int STRENGTH, unsigned int SEEDLENGTH>
|
|
|
|
void Hash_DRBG<HASH, STRENGTH, SEEDLENGTH>::Hash_Generate(const byte* additional, size_t additionaLength, byte *output, size_t size)
|
|
|
|
{
|
|
|
|
// Step 1
|
2017-05-29 06:44:55 +00:00
|
|
|
if (static_cast<word64>(m_reseed) >= static_cast<word64>(MaxRequestBeforeReseed()))
|
2017-02-07 13:51:58 +00:00
|
|
|
throw NIST_DRBG::Err("Hash_DRBG", "Reseed required");
|
|
|
|
|
2017-05-29 06:44:55 +00:00
|
|
|
if (size > MaxBytesPerRequest())
|
2017-02-07 13:51:58 +00:00
|
|
|
throw NIST_DRBG::Err("Hash_DRBG", "Request size exceeds limit");
|
|
|
|
|
|
|
|
// SP 800-90A, Section 9, says we should throw if we have too much entropy, too large a nonce,
|
|
|
|
// or too large a persoanlization string. We warn in Debug builds, but do nothing in Release builds.
|
|
|
|
CRYPTOPP_ASSERT(additionaLength <= MAXIMUM_ADDITIONAL);
|
|
|
|
|
|
|
|
// Step 2
|
|
|
|
if (additional && additionaLength)
|
|
|
|
{
|
|
|
|
const byte two = 2;
|
2018-08-13 13:54:07 +00:00
|
|
|
m_temp.New(HASH::DIGESTSIZE);
|
2017-02-07 13:51:58 +00:00
|
|
|
|
2018-08-13 13:54:07 +00:00
|
|
|
m_hash.Update(&two, 1);
|
|
|
|
m_hash.Update(m_v, m_v.size());
|
|
|
|
m_hash.Update(additional, additionaLength);
|
|
|
|
m_hash.Final(m_temp);
|
2017-02-07 13:51:58 +00:00
|
|
|
|
|
|
|
CRYPTOPP_ASSERT(SEEDLENGTH >= HASH::DIGESTSIZE);
|
2017-03-11 13:10:32 +00:00
|
|
|
int carry=0, j=HASH::DIGESTSIZE-1, i=SEEDLENGTH-1;
|
|
|
|
while (j>=0)
|
2017-02-07 13:51:58 +00:00
|
|
|
{
|
2018-08-13 13:54:07 +00:00
|
|
|
carry = m_v[i] + m_temp[j] + carry;
|
2017-02-07 13:51:58 +00:00
|
|
|
m_v[i] = static_cast<byte>(carry);
|
2017-03-11 20:55:44 +00:00
|
|
|
i--; j--; carry >>= 8;
|
2017-02-07 13:51:58 +00:00
|
|
|
}
|
2017-03-11 13:10:32 +00:00
|
|
|
while (i>=0)
|
2017-02-07 13:51:58 +00:00
|
|
|
{
|
|
|
|
carry = m_v[i] + carry;
|
|
|
|
m_v[i] = static_cast<byte>(carry);
|
2017-03-11 13:10:32 +00:00
|
|
|
i--; carry >>= 8;
|
2017-02-07 13:51:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 3
|
|
|
|
{
|
2018-08-13 13:54:07 +00:00
|
|
|
m_temp.Assign(m_v);
|
2017-02-07 13:51:58 +00:00
|
|
|
while (size)
|
|
|
|
{
|
2018-08-13 13:54:07 +00:00
|
|
|
m_hash.Update(m_temp, m_temp.size());
|
2017-02-07 13:51:58 +00:00
|
|
|
size_t count = STDMIN(size, (size_t)HASH::DIGESTSIZE);
|
2018-08-13 13:54:07 +00:00
|
|
|
m_hash.TruncatedFinal(output, count);
|
2017-02-07 13:51:58 +00:00
|
|
|
|
2018-08-13 13:54:07 +00:00
|
|
|
IncrementCounterByOne(m_temp, static_cast<unsigned int>(m_temp.size()));
|
2017-03-11 13:10:32 +00:00
|
|
|
size -= count; output += count;
|
2017-02-07 13:51:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Steps 4-7
|
|
|
|
{
|
|
|
|
const byte three = 3;
|
2018-08-13 13:54:07 +00:00
|
|
|
m_temp.New(HASH::DIGESTSIZE);
|
2017-02-07 13:51:58 +00:00
|
|
|
|
2018-08-13 13:54:07 +00:00
|
|
|
m_hash.Update(&three, 1);
|
|
|
|
m_hash.Update(m_v, m_v.size());
|
|
|
|
m_hash.Final(m_temp);
|
2017-02-07 13:51:58 +00:00
|
|
|
|
|
|
|
CRYPTOPP_ASSERT(SEEDLENGTH >= HASH::DIGESTSIZE);
|
|
|
|
CRYPTOPP_ASSERT(HASH::DIGESTSIZE >= sizeof(m_reseed));
|
2017-03-11 13:10:32 +00:00
|
|
|
int carry=0, k=sizeof(m_reseed)-1, j=HASH::DIGESTSIZE-1, i=SEEDLENGTH-1;
|
|
|
|
|
|
|
|
while (k>=0)
|
2017-02-07 13:51:58 +00:00
|
|
|
{
|
2018-08-13 13:54:07 +00:00
|
|
|
carry = m_v[i] + m_c[i] + m_temp[j] + GetByte<word64>(BIG_ENDIAN_ORDER, m_reseed, k) + carry;
|
2017-02-07 13:51:58 +00:00
|
|
|
m_v[i] = static_cast<byte>(carry);
|
2017-03-11 20:55:44 +00:00
|
|
|
i--; j--; k--; carry >>= 8;
|
2017-02-07 13:51:58 +00:00
|
|
|
}
|
2017-03-11 13:10:32 +00:00
|
|
|
|
|
|
|
while (j>=0)
|
2017-02-07 13:51:58 +00:00
|
|
|
{
|
2018-08-13 13:54:07 +00:00
|
|
|
carry = m_v[i] + m_c[i] + m_temp[j] + carry;
|
2017-02-07 13:51:58 +00:00
|
|
|
m_v[i] = static_cast<byte>(carry);
|
2017-03-11 20:55:44 +00:00
|
|
|
i--; j--; carry >>= 8;
|
2017-02-07 13:51:58 +00:00
|
|
|
}
|
2017-03-11 13:10:32 +00:00
|
|
|
|
2017-02-07 13:51:58 +00:00
|
|
|
while (i>=0)
|
|
|
|
{
|
|
|
|
carry = m_v[i] + m_c[i] + carry;
|
|
|
|
m_v[i] = static_cast<byte>(carry);
|
2017-03-11 20:55:44 +00:00
|
|
|
i--; carry >>= 8;
|
2017-02-07 13:51:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_reseed++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 10.3.1 Derivation Function Using a Hash Function (Hash_df) (p.49)
|
|
|
|
template <typename HASH, unsigned int STRENGTH, unsigned int SEEDLENGTH>
|
|
|
|
void Hash_DRBG<HASH, STRENGTH, SEEDLENGTH>::Hash_Update(const byte* input1, size_t inlen1, const byte* input2, size_t inlen2,
|
|
|
|
const byte* input3, size_t inlen3, const byte* input4, size_t inlen4, byte* output, size_t outlen)
|
|
|
|
{
|
|
|
|
byte counter = 1;
|
|
|
|
word32 bits = ConditionalByteReverse(BIG_ENDIAN_ORDER, static_cast<word32>(outlen*8));
|
|
|
|
|
2017-02-07 22:13:27 +00:00
|
|
|
while (outlen)
|
2017-02-07 13:51:58 +00:00
|
|
|
{
|
2018-08-13 13:54:07 +00:00
|
|
|
m_hash.Update(&counter, 1);
|
|
|
|
m_hash.Update(reinterpret_cast<const byte*>(&bits), 4);
|
2017-02-07 13:51:58 +00:00
|
|
|
|
|
|
|
if (input1 && inlen1)
|
2018-08-13 13:54:07 +00:00
|
|
|
m_hash.Update(input1, inlen1);
|
2017-02-07 13:51:58 +00:00
|
|
|
if (input2 && inlen2)
|
2018-08-13 13:54:07 +00:00
|
|
|
m_hash.Update(input2, inlen2);
|
2017-02-07 13:51:58 +00:00
|
|
|
if (input3 && inlen3)
|
2018-08-13 13:54:07 +00:00
|
|
|
m_hash.Update(input3, inlen3);
|
2017-02-07 13:51:58 +00:00
|
|
|
if (input4 && inlen4)
|
2018-08-13 13:54:07 +00:00
|
|
|
m_hash.Update(input4, inlen4);
|
2017-02-07 13:51:58 +00:00
|
|
|
|
2017-02-07 22:13:27 +00:00
|
|
|
size_t count = STDMIN(outlen, (size_t)HASH::DIGESTSIZE);
|
2018-08-13 13:54:07 +00:00
|
|
|
m_hash.TruncatedFinal(output, count);
|
2017-02-07 22:13:27 +00:00
|
|
|
|
2017-03-07 09:52:37 +00:00
|
|
|
output += count; outlen -= count;
|
|
|
|
counter++;
|
2017-02-07 13:51:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// *************************************************************
|
|
|
|
|
|
|
|
// 10.1.2.3 Instantiation of HMAC_DRBG (p.45)
|
|
|
|
template <typename HASH, unsigned int STRENGTH, unsigned int SEEDLENGTH>
|
|
|
|
void HMAC_DRBG<HASH, STRENGTH, SEEDLENGTH>::DRBG_Instantiate(const byte* entropy, size_t entropyLength, const byte* nonce, size_t nonceLength,
|
|
|
|
const byte* personalization, size_t personalizationLength)
|
|
|
|
{
|
|
|
|
// SP 800-90A, 8.6.3: The entropy input shall have entropy that is equal to or greater than the security
|
|
|
|
// strength of the instantiation. Additional entropy may be provided in the nonce or the optional
|
|
|
|
// personalization string during instantiation, or in the additional input during reseeding and generation,
|
|
|
|
// but this is not required and does not increase the "official" security strength of the DRBG
|
|
|
|
// instantiation that is recorded in the internal state.
|
|
|
|
CRYPTOPP_ASSERT(entropyLength >= MINIMUM_ENTROPY);
|
|
|
|
if (entropyLength < MINIMUM_ENTROPY)
|
|
|
|
throw NIST_DRBG::Err("HMAC_DRBG", "Insufficient entropy during instantiate");
|
|
|
|
|
|
|
|
// SP 800-90A, Section 9, says we should throw if we have too much entropy, too large a nonce,
|
|
|
|
// or too large a persoanlization string. We warn in Debug builds, but do nothing in Release builds.
|
|
|
|
CRYPTOPP_ASSERT(entropyLength <= MAXIMUM_ENTROPY);
|
|
|
|
CRYPTOPP_ASSERT(nonceLength <= MAXIMUM_NONCE);
|
|
|
|
CRYPTOPP_ASSERT(personalizationLength <= MAXIMUM_PERSONALIZATION);
|
|
|
|
|
|
|
|
std::fill(m_k.begin(), m_k.begin()+m_k.size(), byte(0));
|
|
|
|
std::fill(m_v.begin(), m_v.begin()+m_v.size(), byte(1));
|
|
|
|
|
|
|
|
HMAC_Update(entropy, entropyLength, nonce, nonceLength, personalization, personalizationLength);
|
|
|
|
m_reseed = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 10.1.2.4 Reseeding a HMAC_DRBG Instantiation (p.46)
|
|
|
|
template <typename HASH, unsigned int STRENGTH, unsigned int SEEDLENGTH>
|
|
|
|
void HMAC_DRBG<HASH, STRENGTH, SEEDLENGTH>::DRBG_Reseed(const byte* entropy, size_t entropyLength, const byte* additional, size_t additionaLength)
|
|
|
|
{
|
|
|
|
// SP 800-90A, 8.6.3: The entropy input shall have entropy that is equal to or greater than the security
|
|
|
|
// strength of the instantiation. Additional entropy may be provided in the nonce or the optional
|
|
|
|
// personalization string during instantiation, or in the additional input during reseeding and generation,
|
|
|
|
// but this is not required and does not increase the "official" security strength of the DRBG
|
|
|
|
// instantiation that is recorded in the internal state..
|
|
|
|
CRYPTOPP_ASSERT(entropyLength >= MINIMUM_ENTROPY);
|
|
|
|
if (entropyLength < MINIMUM_ENTROPY)
|
|
|
|
throw NIST_DRBG::Err("HMAC_DRBG", "Insufficient entropy during reseed");
|
|
|
|
|
|
|
|
// SP 800-90A, Section 9, says we should throw if we have too much entropy, too large a nonce,
|
|
|
|
// or too large a persoanlization string. We warn in Debug builds, but do nothing in Release builds.
|
|
|
|
CRYPTOPP_ASSERT(entropyLength <= MAXIMUM_ENTROPY);
|
|
|
|
CRYPTOPP_ASSERT(additionaLength <= MAXIMUM_ADDITIONAL);
|
|
|
|
|
2017-03-01 11:10:06 +00:00
|
|
|
HMAC_Update(entropy, entropyLength, additional, additionaLength, NULLPTR, 0);
|
2017-02-07 13:51:58 +00:00
|
|
|
m_reseed = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 10.1.2.5 Generating Pseudorandom Bits Using HMAC_DRBG (p.46)
|
|
|
|
template <typename HASH, unsigned int STRENGTH, unsigned int SEEDLENGTH>
|
|
|
|
void HMAC_DRBG<HASH, STRENGTH, SEEDLENGTH>::HMAC_Generate(const byte* additional, size_t additionaLength, byte *output, size_t size)
|
|
|
|
{
|
|
|
|
// Step 1
|
2017-05-29 06:44:55 +00:00
|
|
|
if (static_cast<word64>(m_reseed) >= static_cast<word64>(MaxRequestBeforeReseed()))
|
2017-02-07 13:51:58 +00:00
|
|
|
throw NIST_DRBG::Err("HMAC_DRBG", "Reseed required");
|
|
|
|
|
2017-05-29 06:44:55 +00:00
|
|
|
if (size > MaxBytesPerRequest())
|
2017-02-07 13:51:58 +00:00
|
|
|
throw NIST_DRBG::Err("HMAC_DRBG", "Request size exceeds limit");
|
|
|
|
|
|
|
|
// SP 800-90A, Section 9, says we should throw if we have too much entropy, too large a nonce,
|
|
|
|
// or too large a persoanlization string. We warn in Debug builds, but do nothing in Release builds.
|
|
|
|
CRYPTOPP_ASSERT(additionaLength <= MAXIMUM_ADDITIONAL);
|
|
|
|
|
|
|
|
// Step 2
|
|
|
|
if (additional && additionaLength)
|
2017-03-01 11:10:06 +00:00
|
|
|
HMAC_Update(additional, additionaLength, NULLPTR, 0, NULLPTR, 0);
|
2017-02-07 13:51:58 +00:00
|
|
|
|
|
|
|
// Step 3
|
2018-08-13 13:54:07 +00:00
|
|
|
m_hmac.SetKey(m_k, m_k.size());
|
2017-02-07 13:51:58 +00:00
|
|
|
|
|
|
|
while (size)
|
|
|
|
{
|
2018-08-13 13:54:07 +00:00
|
|
|
m_hmac.Update(m_v, m_v.size());
|
|
|
|
m_hmac.TruncatedFinal(m_v, m_v.size());
|
2017-02-07 13:51:58 +00:00
|
|
|
|
|
|
|
size_t count = STDMIN(size, (size_t)HASH::DIGESTSIZE);
|
|
|
|
memcpy(output, m_v, count);
|
|
|
|
size -= count; output += count;
|
|
|
|
}
|
|
|
|
|
2017-03-01 11:10:06 +00:00
|
|
|
HMAC_Update(additional, additionaLength, NULLPTR, 0, NULLPTR, 0);
|
2017-02-07 13:51:58 +00:00
|
|
|
m_reseed++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 10.1.2.2 Derivation Function Using a HMAC Function (HMAC_Update) (p.44)
|
|
|
|
template <typename HASH, unsigned int STRENGTH, unsigned int SEEDLENGTH>
|
|
|
|
void HMAC_DRBG<HASH, STRENGTH, SEEDLENGTH>::HMAC_Update(const byte* input1, size_t inlen1, const byte* input2, size_t inlen2, const byte* input3, size_t inlen3)
|
|
|
|
{
|
|
|
|
const byte zero = 0, one = 1;
|
|
|
|
|
|
|
|
// Step 1
|
2018-08-13 13:54:07 +00:00
|
|
|
m_hmac.SetKey(m_k, m_k.size());
|
|
|
|
m_hmac.Update(m_v, m_v.size());
|
|
|
|
m_hmac.Update(&zero, 1);
|
2017-02-07 13:51:58 +00:00
|
|
|
|
|
|
|
if (input1 && inlen1)
|
2018-08-13 13:54:07 +00:00
|
|
|
m_hmac.Update(input1, inlen1);
|
2017-02-07 13:51:58 +00:00
|
|
|
if (input2 && inlen2)
|
2018-08-13 13:54:07 +00:00
|
|
|
m_hmac.Update(input2, inlen2);
|
2017-02-07 13:51:58 +00:00
|
|
|
if (input3 && inlen3)
|
2018-08-13 13:54:07 +00:00
|
|
|
m_hmac.Update(input3, inlen3);
|
2017-02-07 13:51:58 +00:00
|
|
|
|
2018-08-13 13:54:07 +00:00
|
|
|
m_hmac.TruncatedFinal(m_k, m_k.size());
|
2017-02-07 13:51:58 +00:00
|
|
|
|
|
|
|
// Step 2
|
2018-08-13 13:54:07 +00:00
|
|
|
m_hmac.SetKey(m_k, m_k.size());
|
|
|
|
m_hmac.Update(m_v, m_v.size());
|
2017-02-07 13:51:58 +00:00
|
|
|
|
2018-08-13 13:54:07 +00:00
|
|
|
m_hmac.TruncatedFinal(m_v, m_v.size());
|
2017-02-07 13:51:58 +00:00
|
|
|
|
|
|
|
// Step 3
|
|
|
|
if ((inlen1 | inlen2 | inlen3) == 0)
|
|
|
|
return;
|
2017-02-07 23:51:44 +00:00
|
|
|
|
2017-02-07 13:51:58 +00:00
|
|
|
// Step 4
|
2018-08-13 13:54:07 +00:00
|
|
|
m_hmac.SetKey(m_k, m_k.size());
|
|
|
|
m_hmac.Update(m_v, m_v.size());
|
|
|
|
m_hmac.Update(&one, 1);
|
2017-02-07 13:51:58 +00:00
|
|
|
|
|
|
|
if (input1 && inlen1)
|
2018-08-13 13:54:07 +00:00
|
|
|
m_hmac.Update(input1, inlen1);
|
2017-02-07 13:51:58 +00:00
|
|
|
if (input2 && inlen2)
|
2018-08-13 13:54:07 +00:00
|
|
|
m_hmac.Update(input2, inlen2);
|
2017-02-07 13:51:58 +00:00
|
|
|
if (input3 && inlen3)
|
2018-08-13 13:54:07 +00:00
|
|
|
m_hmac.Update(input3, inlen3);
|
2017-02-07 13:51:58 +00:00
|
|
|
|
2018-08-13 13:54:07 +00:00
|
|
|
m_hmac.TruncatedFinal(m_k, m_k.size());
|
2017-02-07 13:51:58 +00:00
|
|
|
|
|
|
|
// Step 5
|
2018-08-13 13:54:07 +00:00
|
|
|
m_hmac.SetKey(m_k, m_k.size());
|
|
|
|
m_hmac.Update(m_v, m_v.size());
|
2017-02-07 13:51:58 +00:00
|
|
|
|
2018-08-13 13:54:07 +00:00
|
|
|
m_hmac.TruncatedFinal(m_v, m_v.size());
|
2017-02-07 13:51:58 +00:00
|
|
|
}
|
|
|
|
|
2016-12-31 02:46:03 +00:00
|
|
|
NAMESPACE_END
|
|
|
|
|
2016-12-31 17:28:41 +00:00
|
|
|
#endif // CRYPTOPP_NIST_DRBG_H
|