2015-11-05 06:59:46 +00:00
|
|
|
// rdrand.h - written and placed in public domain by Jeffrey Walton and Uri Blumenthal.
|
|
|
|
// Copyright assigned to Crypto++ project.
|
|
|
|
|
2015-11-29 19:43:12 +00:00
|
|
|
//! \file rdrand.h
|
2015-11-18 20:19:02 +00:00
|
|
|
//! \brief Classes for RDRAND and RDSEED
|
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
#ifndef CRYPTOPP_RDRAND_H
|
|
|
|
#define CRYPTOPP_RDRAND_H
|
|
|
|
|
|
|
|
#include "cryptlib.h"
|
|
|
|
|
2015-11-18 20:19:02 +00:00
|
|
|
// This file (and friends) provides both RDRAND and RDSEED, but its somewhat
|
|
|
|
// experimental. They were added at Crypto++ 5.6.3. At compile time, it
|
|
|
|
// indirectly uses CRYPTOPP_BOOL_{X86|X32|X64} (via CRYPTOPP_CPUID_AVAILABLE)
|
|
|
|
// to select an implementation or "throw NotImplemented". At runtime, the
|
|
|
|
// class uses the result of CPUID to determine if RDRAND or RDSEED are
|
2015-11-29 19:43:12 +00:00
|
|
|
// available. If not available, a lazy throw strategy is used. I.e., the
|
|
|
|
// throw is deferred until GenerateBlock() is called.
|
2015-11-18 20:19:02 +00:00
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
// Microsoft added RDRAND in August 2012, VS2012. GCC added RDRAND in December 2010, GCC 4.6.
|
|
|
|
// Clang added RDRAND in July 2012, Clang 3.2. Intel added RDRAND in September 2011, ICC 12.1.
|
|
|
|
|
|
|
|
NAMESPACE_BEGIN(CryptoPP)
|
|
|
|
|
2015-11-18 20:19:02 +00:00
|
|
|
//! \brief Exception thrown when a RDRAND generator encounters
|
|
|
|
//! a generator related error.
|
2015-11-05 06:59:46 +00:00
|
|
|
class RDRAND_Err : public Exception
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RDRAND_Err(const std::string &operation)
|
|
|
|
: Exception(OTHER_ERROR, "RDRAND: " + operation + " operation failed") {}
|
|
|
|
};
|
|
|
|
|
2015-11-18 20:19:02 +00:00
|
|
|
//! \brief Hardware generated random numbers using RDRAND instruction
|
|
|
|
//! \sa MaurerRandomnessTest() for random bit generators
|
|
|
|
class RDRAND : public RandomNumberGenerator
|
2015-11-05 06:59:46 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
std::string AlgorithmName() const {return "RDRAND";}
|
|
|
|
|
2015-11-18 20:19:02 +00:00
|
|
|
//! \brief Construct a RDRAND generator
|
|
|
|
//! \param retries the number of retries for failed calls to the hardware
|
|
|
|
//! \details RDRAND() constructs a generator with a maximum number of retires
|
|
|
|
//! for failed generation attempts.
|
2015-11-29 19:43:12 +00:00
|
|
|
//! \details Empirical testing under a 6th generaton i7 (6200U) shows RDSEED fails
|
|
|
|
//! to fulfill requests at about 6 to 8 times the rate of RDRAND. The default
|
|
|
|
//! retries reflects the difference.
|
|
|
|
RDRAND(unsigned int retries = 12) : m_retries(retries) {}
|
2015-11-05 06:59:46 +00:00
|
|
|
|
|
|
|
virtual ~RDRAND() {}
|
|
|
|
|
2015-11-18 20:19:02 +00:00
|
|
|
//! \brief Retrieve the number of retries used by the generator
|
2015-11-23 00:17:15 +00:00
|
|
|
//! \returns the number of times GenerateBlock() will attempt to recover from a failed generation
|
2015-11-05 06:59:46 +00:00
|
|
|
unsigned int GetRetries() const
|
|
|
|
{
|
|
|
|
return m_retries;
|
|
|
|
}
|
|
|
|
|
2015-11-18 20:19:02 +00:00
|
|
|
//! \brief Set the number of retries used by the generator
|
2015-11-19 18:09:33 +00:00
|
|
|
//! \param retries number of times GenerateBlock() will attempt to recover from a failed generation
|
2015-11-05 06:59:46 +00:00
|
|
|
void SetRetries(unsigned int retries)
|
|
|
|
{
|
|
|
|
m_retries = retries;
|
|
|
|
}
|
|
|
|
|
2015-11-18 20:19:02 +00:00
|
|
|
//! \brief Generate random array of bytes
|
2015-11-05 06:59:46 +00:00
|
|
|
//! \param output the byte buffer
|
|
|
|
//! \param size the length of the buffer, in bytes
|
2015-11-18 20:19:02 +00:00
|
|
|
#if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64)
|
2015-11-05 06:59:46 +00:00
|
|
|
virtual void GenerateBlock(byte *output, size_t size);
|
2015-11-18 20:19:02 +00:00
|
|
|
#else
|
|
|
|
virtual void GenerateBlock(byte *output, size_t size) {
|
|
|
|
CRYPTOPP_UNUSED(output), CRYPTOPP_UNUSED(size);
|
|
|
|
throw NotImplemented("RDRAND: rdrand is not available on this platform");
|
|
|
|
}
|
|
|
|
#endif
|
2015-11-05 06:59:46 +00:00
|
|
|
|
2015-11-18 20:19:02 +00:00
|
|
|
//! \brief Generate and discard n bytes
|
|
|
|
//! \param n the number of bytes to generate and discard
|
|
|
|
//! \details the RDSEED generator discards words, not bytes. If n is
|
|
|
|
//! not a multiple of a machine word, then it is rounded up to
|
|
|
|
//! that size.
|
|
|
|
#if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64)
|
2015-11-05 06:59:46 +00:00
|
|
|
virtual void DiscardBytes(size_t n);
|
2015-11-18 20:19:02 +00:00
|
|
|
#else
|
|
|
|
virtual void DiscardBytes(size_t n) {
|
|
|
|
CRYPTOPP_UNUSED(n);
|
|
|
|
throw NotImplemented("RDRAND: rdrand is not available on this platform");
|
|
|
|
}
|
|
|
|
#endif
|
2015-11-05 06:59:46 +00:00
|
|
|
|
2015-12-31 09:42:21 +00:00
|
|
|
//! \brief Update RNG state with additional unpredictable values
|
2015-11-05 06:59:46 +00:00
|
|
|
//! \param input unused
|
|
|
|
//! \param length unused
|
2015-11-18 20:19:02 +00:00
|
|
|
//! \details The operation is a nop for this generator.
|
2015-11-05 06:59:46 +00:00
|
|
|
virtual void IncorporateEntropy(const byte *input, size_t length)
|
|
|
|
{
|
|
|
|
// Override to avoid the base class' throw.
|
|
|
|
CRYPTOPP_UNUSED(input); CRYPTOPP_UNUSED(length);
|
2015-12-31 09:50:20 +00:00
|
|
|
// assert(0); // warn in debug builds
|
2015-11-05 06:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
unsigned int m_retries;
|
|
|
|
};
|
|
|
|
|
2015-11-18 20:19:02 +00:00
|
|
|
//! \brief Exception thrown when a RDSEED generator encounters
|
|
|
|
//! a generator related error.
|
2015-11-05 06:59:46 +00:00
|
|
|
class RDSEED_Err : public Exception
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RDSEED_Err(const std::string &operation)
|
|
|
|
: Exception(OTHER_ERROR, "RDSEED: " + operation + " operation failed") {}
|
|
|
|
};
|
|
|
|
|
2015-11-18 20:19:02 +00:00
|
|
|
//! \brief Hardware generated random numbers using RDSEED instruction
|
|
|
|
//! \sa MaurerRandomnessTest() for random bit generators
|
|
|
|
class RDSEED : public RandomNumberGenerator
|
2015-11-05 06:59:46 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
std::string AlgorithmName() const {return "RDSEED";}
|
|
|
|
|
2015-11-18 20:19:02 +00:00
|
|
|
//! \brief Construct a RDSEED generator
|
|
|
|
//! \param retries the number of retries for failed calls to the hardware
|
|
|
|
//! \details RDSEED() constructs a generator with a maximum number of retires
|
|
|
|
//! for failed generation attempts.
|
2015-11-29 19:43:12 +00:00
|
|
|
//! \details Empirical testing under a 6th generaton i7 (6200U) shows RDSEED fails
|
|
|
|
//! to fulfill requests at about 6 to 8 times the rate of RDRAND. The default
|
|
|
|
//! retries reflects the difference.
|
|
|
|
RDSEED(unsigned int retries = 64) : m_retries(retries) {}
|
2015-11-05 06:59:46 +00:00
|
|
|
|
|
|
|
virtual ~RDSEED() {}
|
|
|
|
|
2015-11-18 20:19:02 +00:00
|
|
|
//! \brief Retrieve the number of retries used by the generator
|
2015-11-23 00:17:15 +00:00
|
|
|
//! \returns the number of times GenerateBlock() will attempt to recover from a failed generation
|
2015-11-05 06:59:46 +00:00
|
|
|
unsigned int GetRetries() const
|
|
|
|
{
|
|
|
|
return m_retries;
|
|
|
|
}
|
|
|
|
|
2015-11-18 20:19:02 +00:00
|
|
|
//! \brief Set the number of retries used by the generator
|
2015-11-19 18:09:33 +00:00
|
|
|
//! \param retries number of times GenerateBlock() will attempt to recover from a failed generation
|
2015-11-05 06:59:46 +00:00
|
|
|
void SetRetries(unsigned int retries)
|
|
|
|
{
|
|
|
|
m_retries = retries;
|
|
|
|
}
|
|
|
|
|
2015-11-18 20:19:02 +00:00
|
|
|
//! \brief Generate random array of bytes
|
2015-11-05 06:59:46 +00:00
|
|
|
//! \param output the byte buffer
|
|
|
|
//! \param size the length of the buffer, in bytes
|
2015-11-18 20:19:02 +00:00
|
|
|
#if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64)
|
2015-11-05 06:59:46 +00:00
|
|
|
virtual void GenerateBlock(byte *output, size_t size);
|
2015-11-18 20:19:02 +00:00
|
|
|
#else
|
|
|
|
virtual void GenerateBlock(byte *output, size_t size) {
|
|
|
|
CRYPTOPP_UNUSED(output), CRYPTOPP_UNUSED(size);
|
|
|
|
throw NotImplemented("RDSEED: rdseed is not available on this platform");
|
|
|
|
}
|
|
|
|
#endif
|
2015-11-05 06:59:46 +00:00
|
|
|
|
2015-11-18 20:19:02 +00:00
|
|
|
//! \brief Generate and discard n bytes
|
|
|
|
//! \param n the number of bytes to generate and discard
|
|
|
|
//! \details the RDSEED generator discards words, not bytes. If n is
|
|
|
|
//! not a multiple of a machine word, then it is rounded up to
|
|
|
|
//! that size.
|
|
|
|
#if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64)
|
2015-11-05 06:59:46 +00:00
|
|
|
virtual void DiscardBytes(size_t n);
|
2015-11-18 20:19:02 +00:00
|
|
|
#else
|
|
|
|
virtual void DiscardBytes(size_t n) {
|
|
|
|
CRYPTOPP_UNUSED(n);
|
|
|
|
throw NotImplemented("RDSEED: rdseed is not available on this platform");
|
|
|
|
}
|
|
|
|
#endif
|
2015-11-05 06:59:46 +00:00
|
|
|
|
2015-12-31 09:42:21 +00:00
|
|
|
//! \brief Update RNG state with additional unpredictable values
|
2015-11-05 06:59:46 +00:00
|
|
|
//! \param input unused
|
|
|
|
//! \param length unused
|
2015-11-18 20:19:02 +00:00
|
|
|
//! \details The operation is a nop for this generator.
|
2015-11-05 06:59:46 +00:00
|
|
|
virtual void IncorporateEntropy(const byte *input, size_t length)
|
|
|
|
{
|
|
|
|
// Override to avoid the base class' throw.
|
|
|
|
CRYPTOPP_UNUSED(input); CRYPTOPP_UNUSED(length);
|
2015-12-31 09:50:20 +00:00
|
|
|
// assert(0); // warn in debug builds
|
2015-11-05 06:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
unsigned int m_retries;
|
|
|
|
};
|
|
|
|
|
|
|
|
NAMESPACE_END
|
|
|
|
|
|
|
|
#endif // CRYPTOPP_RDRAND_H
|