10 #ifndef CRYPTOPP_NIST_DRBG_H 11 #define CRYPTOPP_NIST_DRBG_H 31 explicit Err(
const std::string &
c,
const std::string &m)
50 virtual void IncorporateEntropy(
const byte *input,
size_t length)=0;
62 virtual void IncorporateEntropy(
const byte *entropy,
size_t entropyLength,
const byte* additional,
size_t additionaLength)=0;
69 virtual void GenerateBlock(
byte *output,
size_t size)=0;
80 virtual void GenerateBlock(
const byte* additional,
size_t additionaLength,
byte *output,
size_t size)=0;
85 virtual unsigned int GetSecurityStrength()
const=0;
91 virtual unsigned int GetSeedLength()
const=0;
98 virtual unsigned int GetMinEntropy()
const=0;
105 virtual unsigned int GetMaxEntropy()
const=0;
112 virtual unsigned int GetMinNonce()
const=0;
120 virtual unsigned int GetMaxNonce()
const=0;
125 virtual unsigned int GetMaxBytesPerRequest()
const=0;
132 virtual unsigned int GetMaxRequestBeforeReseed()
const=0;
135 virtual void DRBG_Instantiate(
const byte* entropy,
size_t entropyLength,
136 const byte* nonce,
size_t nonceLength,
const byte* personalization,
size_t personalizationLength)=0;
138 virtual void DRBG_Reseed(
const byte* entropy,
size_t entropyLength,
const byte* additional,
size_t additionaLength)=0;
155 template <
typename HASH=
SHA256,
unsigned int STRENGTH=128/8,
unsigned int SEEDLENGTH=440/8>
173 Hash_DRBG(
const byte* entropy,
size_t entropyLength=STRENGTH,
const byte* nonce=NULL,
198 size_t nonceLength=0,
const byte* personalization=NULL,
size_t personalizationLength=0)
199 :
NIST_DRBG(), m_c(SEEDLENGTH), m_v(SEEDLENGTH)
201 DRBG_Instantiate(entropy, entropyLength, nonce, nonceLength, personalization, personalizationLength);
214 {
return DRBG_Reseed(input, length, NULL, 0);}
217 {
return DRBG_Reseed(entropy, entropyLength, additional, additionaLength);}
220 {
return Hash_Generate(NULL, 0, output, size);}
223 {
return Hash_Generate(additional, additionaLength, output, size);}
228 const byte* input3,
size_t inlen3,
const byte* input4,
size_t inlen4,
byte* output,
size_t outlen)
235 for (count=0; outlen; outlen -=
count, output +=
count, counter++)
237 hash.Update(&counter, 1);
238 hash.Update(reinterpret_cast<const byte*>(&bits), 4);
240 if (input1 && inlen1)
241 hash.Update(input1, inlen1);
242 if (input2 && inlen2)
243 hash.Update(input2, inlen2);
244 if (input3 && inlen3)
245 hash.Update(input3, inlen3);
246 if (input4 && inlen4)
247 hash.Update(input4, inlen4);
249 count =
STDMIN(outlen, (
size_t)HASH::DIGESTSIZE);
250 hash.TruncatedFinal(output, count);
256 const byte* personalization,
size_t personalizationLength)
264 if (entropyLength < MINIMUM_ENTROPY)
265 throw NIST_DRBG::Err(
"Hash_DRBG",
"Insufficient entropy during instantiate");
275 Hash_df(entropy, entropyLength, nonce, nonceLength, personalization, personalizationLength, NULL, 0,
t1,
t1.size());
276 Hash_df(&zero, 1,
t1,
t1.size(), NULL, 0, NULL, 0,
t2, t2.size());
278 m_v.swap(
t1); m_c.swap(t2);
283 void DRBG_Reseed(
const byte* entropy,
size_t entropyLength,
const byte* additional,
size_t additionaLength)
291 if (entropyLength < MINIMUM_ENTROPY)
292 throw NIST_DRBG::Err(
"Hash_DRBG",
"Insufficient entropy during reseed");
299 const byte zero = 0, one = 1;
301 Hash_df(&one, 1, m_v, m_v.size(), entropy, entropyLength, additional, additionaLength,
t1, t1.size());
302 Hash_df(&zero, 1, t1, t1.size(), NULL, 0, NULL, 0,
t2, t2.size());
304 m_v.swap(t1); m_c.swap(t2);
312 if (static_cast<word64>(m_reseed) >= static_cast<word64>(GetMaxRequestBeforeReseed()))
315 if (size > GetMaxBytesPerRequest())
323 if (additional && additionaLength)
329 hash.Update(&two, 1);
330 hash.Update(m_v, m_v.size());
331 hash.Update(additional, additionaLength);
335 int carry=0, i=SEEDLENGTH-1, j=HASH::DIGESTSIZE-1;
338 carry = m_v[i] + w[j] + carry;
339 m_v[i] =
static_cast<byte>(carry);
340 carry >>= 8; i--; j--;
342 while (carry && i>=0)
344 carry = m_v[i] + carry;
345 m_v[i] =
static_cast<byte>(carry);
358 hash.Update(data, data.
size());
359 count =
STDMIN(size, (
size_t)HASH::DIGESTSIZE);
360 hash.TruncatedFinal(output, count);
369 const byte three = 3;
372 hash.Update(&three, 1);
373 hash.Update(m_v, m_v.size());
378 int carry=0, i=SEEDLENGTH-1, j=HASH::DIGESTSIZE-1, k=
sizeof(m_reseed)-1;
379 while(i>=0 && j>=0 && k>=0)
381 carry = m_v[i] + m_c[i] + h[j] + GetByte<word64>(
BIG_ENDIAN_ORDER, m_reseed, k) + carry;
382 m_v[i] =
static_cast<byte>(carry);
383 carry >>= 8; i--; j--; k--;
387 carry = m_v[i] + m_c[i] + h[j] + carry;
388 m_v[i] =
static_cast<byte>(carry);
389 carry >>= 8; i--; j--;
393 carry = m_v[i] + m_c[i] + carry;
394 m_v[i] =
static_cast<byte>(carry);
409 #endif // CRYPTOPP_NIST_DRBG_H Base class for all exceptions thrown by the library.
void Hash_df(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)
void IncrementCounterByOne(byte *inout, unsigned int size)
Performs an addition with carry on a block of bytes.
void IncorporateEntropy(const byte *entropy, size_t entropyLength, const byte *additional, size_t additionaLength)
Update RNG state with additional unpredictable values.
#define NAMESPACE_BEGIN(x)
Abstract base classes that provide a uniform interface to this library.
size_type size() const
Provides the count of elements in the SecBlock.
unsigned int GetMinNonce() const
Provides the minimum nonce size.
Interface for random number generators.
unsigned int GetMinEntropy() const
Provides the minimum entropy size.
void DRBG_Instantiate(const byte *entropy, size_t entropyLength, const byte *nonce, size_t nonceLength, const byte *personalization, size_t personalizationLength)
Err(const std::string &c, const std::string &m)
Classes and functions for secure memory allocations.
void IncorporateEntropy(const byte *input, size_t length)
Update RNG state with additional unpredictable values.
Exception thrown when a NIST DRBG encounters an error.
void GenerateBlock(const byte *additional, size_t additionaLength, byte *output, size_t size)
Generate random array of bytes.
T ConditionalByteReverse(ByteOrder order, T value)
Reverses bytes in a value depending upon endianness.
unsigned int GetSecurityStrength() const
Provides the security strength.
void GenerateBlock(byte *output, size_t size)
Generate random array of bytes.
unsigned long long word64
unsigned int GetMaxNonce() const
Provides the maximum nonce size.
#define CRYPTOPP_CONSTANT(x)
unsigned int GetMaxBytesPerRequest() const
Provides the maximum size of a request to GenerateBlock.
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
#define CRYPTOPP_ASSERT(exp)
void Hash_Generate(const byte *additional, size_t additionaLength, byte *output, size_t size)
Hash_DRBG from SP 800-90A Rev 1 (June 2015)
Interface for NIST DRBGs from SP 800-90A.
uint8_t const size_t const size
unsigned int GetSeedLength() const
Provides the seed length.
virtual bool CanIncorporateEntropy() const
Determines if a generator can accept additional entropy.
unsigned int GetMaxRequestBeforeReseed() const
Provides the maximum number of requests before a reseed.
unsigned int GetMaxEntropy() const
Provides the maximum entropy size.
void DRBG_Reseed(const byte *entropy, size_t entropyLength, const byte *additional, size_t additionaLength)