summaryrefslogtreecommitdiff
path: root/rdrand.h
diff options
context:
space:
mode:
authorJeffrey Walton <noloader@gmail.com>2015-11-18 15:19:02 -0500
committerJeffrey Walton <noloader@gmail.com>2015-11-18 15:19:02 -0500
commitd2fda9bd4231a7dfcb44e59150f11246d992843f (patch)
treeae2afc3dd818e907f3035d783f039abf9fc83078 /rdrand.h
parent1993a8b7b9f60020ce1524860ce76c01081f3e79 (diff)
downloadcryptopp-git-d2fda9bd4231a7dfcb44e59150f11246d992843f.tar.gz
Cleared issues 11,12,13 (Clang integrated assembler), 58 (RC rollup), 66 (Coverity rollup)
Diffstat (limited to 'rdrand.h')
-rw-r--r--rdrand.h141
1 files changed, 80 insertions, 61 deletions
diff --git a/rdrand.h b/rdrand.h
index 6f02165b..8d5b7380 100644
--- a/rdrand.h
+++ b/rdrand.h
@@ -1,21 +1,30 @@
// rdrand.h - written and placed in public domain by Jeffrey Walton and Uri Blumenthal.
// Copyright assigned to Crypto++ project.
+//! \file
+//! \headerfile rdrand.h
+//! \brief Classes for RDRAND and RDSEED
+
#ifndef CRYPTOPP_RDRAND_H
#define CRYPTOPP_RDRAND_H
#include "cryptlib.h"
+// 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
+// available. A lazy throw strategy is used in case the CPU does not support
+// the instruction. I.e., the throw is deferred until GenerateBlock is called.
+
// 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.
-// Visual Studio 2015 (CL version 1900) is missing _rdseed{16|32|64}_step
-#if (CRYPTOPP_MSC_VERSION <= 1900)
-# define MSC_RDSEED_INTRINSIC_AVAILABLE 0
-#endif
-
NAMESPACE_BEGIN(CryptoPP)
+//! \brief Exception thrown when a RDRAND generator encounters
+//! a generator related error.
class RDRAND_Err : public Exception
{
public:
@@ -23,61 +32,65 @@ public:
: Exception(OTHER_ERROR, "RDRAND: " + operation + " operation failed") {}
};
-//! \brief Read hardware generated random numbers.
-
-//! 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
-//! available. A lazy throw strategy is used in case the CPU does not support
-//! the instruction. I.e., the throw is deferred until GenerateBlock is called.
-class RDRAND : public RandomNumberGenerator, public DeviceState
+//! \brief Hardware generated random numbers using RDRAND instruction
+//! \sa MaurerRandomnessTest() for random bit generators
+class RDRAND : public RandomNumberGenerator
{
public:
std::string AlgorithmName() const {return "RDRAND";}
- //! construct a RDRAND generator with a maximum number of retires for failed generation attempts
+ //! \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.
RDRAND(unsigned int retries = 8) : m_retries(retries) {}
virtual ~RDRAND() {}
- //! returns true if RDRAND is present or available according to CPUID, false otherwise
- bool Available() const;
-
- //! returns true if RDRAND is present or available according to CPUID, false otherwise. There is no exended information available.
- bool Available(word64& extendedInfo) const;
-
- //! returns true if RDRAND is online/ready to produce random numbers, false otherwise
- bool Ready() const;
-
- //! returns true if RDRAND is online/ready to produce random numbers, false otherwise. There is no exended information available.
- bool Ready(word64& extendedInfo) const;
-
+ //! \brief Retrieve the number of retries used by the generator
//! returns the number of times GenerateBlock will attempt to recover from a failed generation
unsigned int GetRetries() const
{
return m_retries;
}
- //! sets the number of times GenerateBlock will attempt to recover from a failed generation
+ //! \brief Set the number of retries used by the generator
+ //! \param the number of times GenerateBlock will attempt to recover from a failed generation
void SetRetries(unsigned int retries)
{
m_retries = retries;
}
- //! generate random array of bytes
+ //! \brief Generate random array of bytes
//! \param output the byte buffer
//! \param size the length of the buffer, in bytes
+#if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64)
virtual void GenerateBlock(byte *output, size_t size);
+#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
- //! generate and discard n bytes.
- //! \param n the number of bytes to discard
+ //! \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)
virtual void DiscardBytes(size_t n);
+#else
+ virtual void DiscardBytes(size_t n) {
+ CRYPTOPP_UNUSED(n);
+ throw NotImplemented("RDRAND: rdrand is not available on this platform");
+ }
+#endif
- //! update RNG state with additional unpredictable values. The operation is a nop for this generator.
+ //! Update RNG state with additional unpredictable values
//! \param input unused
//! \param length unused
+ //! \details The operation is a nop for this generator.
virtual void IncorporateEntropy(const byte *input, size_t length)
{
// Override to avoid the base class' throw.
@@ -89,6 +102,8 @@ private:
unsigned int m_retries;
};
+//! \brief Exception thrown when a RDSEED generator encounters
+//! a generator related error.
class RDSEED_Err : public Exception
{
public:
@@ -96,61 +111,65 @@ public:
: Exception(OTHER_ERROR, "RDSEED: " + operation + " operation failed") {}
};
-//! \brief Read hardware generated random numbers.
-
-//! 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
-//! available. A lazy throw strategy is used in case the CPU does not support
-//! the instruction. I.e., the throw is deferred until GenerateBlock is called.
-class RDSEED : public RandomNumberGenerator, public DeviceState
+//! \brief Hardware generated random numbers using RDSEED instruction
+//! \sa MaurerRandomnessTest() for random bit generators
+class RDSEED : public RandomNumberGenerator
{
public:
std::string AlgorithmName() const {return "RDSEED";}
- //! construct a RDSEED generator with a maximum number of retires for failed generation attempts
+ //! \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.
RDSEED(unsigned int retries = 8) : m_retries(retries) {}
virtual ~RDSEED() {}
- //! returns true if RDSEED is present or available according to CPUID, false otherwise
- bool Available() const;
-
- //! returns true if RDSEED is present or available according to CPUID, false otherwise. There is no exended information available.
- bool Available(word64& extendedInfo) const;
-
- //! returns true if RDSEED is online/ready to produce random numbers, false otherwise
- bool Ready() const;
-
- //! returns true if RDSEED is online/ready to produce random numbers, false otherwise. There is no exended information available.
- bool Ready(word64& extendedInfo) const;
-
+ //! \brief Retrieve the number of retries used by the generator
//! returns the number of times GenerateBlock will attempt to recover from a failed generation
unsigned int GetRetries() const
{
return m_retries;
}
- //! sets the number of times GenerateBlock will attempt to recover from a failed generation
+ //! \brief Set the number of retries used by the generator
+ //! \param the number of times GenerateBlock will attempt to recover from a failed generation
void SetRetries(unsigned int retries)
{
m_retries = retries;
}
- //! generate random array of bytes
+ //! \brief Generate random array of bytes
//! \param output the byte buffer
//! \param size the length of the buffer, in bytes
+#if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64)
virtual void GenerateBlock(byte *output, size_t size);
+#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
- //! generate and discard n bytes.
- //! \param n the number of bytes to discard
+ //! \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)
virtual void DiscardBytes(size_t n);
+#else
+ virtual void DiscardBytes(size_t n) {
+ CRYPTOPP_UNUSED(n);
+ throw NotImplemented("RDSEED: rdseed is not available on this platform");
+ }
+#endif
- //! update RNG state with additional unpredictable values. The operation is a nop for this generator.
+ //! Update RNG state with additional unpredictable values
//! \param input unused
//! \param length unused
+ //! \details The operation is a nop for this generator.
virtual void IncorporateEntropy(const byte *input, size_t length)
{
// Override to avoid the base class' throw.