summaryrefslogtreecommitdiff
path: root/padlkrng.h
diff options
context:
space:
mode:
authorJeffrey Walton <noloader@gmail.com>2017-08-19 15:41:45 -0400
committerJeffrey Walton <noloader@gmail.com>2017-08-19 15:41:45 -0400
commit7fb5953055d14307a9d4ae95fd6499f3a48f8b95 (patch)
tree8373b41085ae414dd5c06f410f8a7a430d366ba5 /padlkrng.h
parent65a96fe983d28e6e51612e3d2361716d7cdf9453 (diff)
downloadcryptopp-git-7fb5953055d14307a9d4ae95fd6499f3a48f8b95.tar.gz
Add VIA Padlock RNG
Diffstat (limited to 'padlkrng.h')
-rw-r--r--padlkrng.h70
1 files changed, 70 insertions, 0 deletions
diff --git a/padlkrng.h b/padlkrng.h
new file mode 100644
index 00000000..9a70fd87
--- /dev/null
+++ b/padlkrng.h
@@ -0,0 +1,70 @@
+// via-rng.h - written and placed in public domain by Jeffrey Walton
+
+//! \file PadlockRNG.h
+//! \brief Class for VIA Padlock RNG
+//! \since Crypto++ 6.0
+
+#ifndef CRYPTOPP_PADLOCK_RNG_H
+#define CRYPTOPP_PADLOCK_RNG_H
+
+#include "cryptlib.h"
+#include "secblock.h"
+
+NAMESPACE_BEGIN(CryptoPP)
+
+//! \brief Exception thrown when a PadlockRNG generator encounters
+//! a generator related error.
+//! \since Crypto++ 6.0
+class PadlockRNG_Err : public Exception
+{
+public:
+ PadlockRNG_Err(const std::string &operation)
+ : Exception(OTHER_ERROR, "PadlockRNG: " + operation + " operation failed") {}
+};
+
+//! \brief Hardware generated random numbers using PadlockRNG instruction
+//! \sa MaurerRandomnessTest() for random bit generators
+//! \since Crypto++ 6.0
+class PadlockRNG : public RandomNumberGenerator
+{
+public:
+ CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() { return "PadlockRNG"; }
+
+ virtual ~PadlockRNG() {}
+
+ //! \brief Construct a PadlockRNG generator
+ //! \details According to DJ of Intel, the Intel PadlockRNG circuit does not underflow.
+ //! If it did hypothetically underflow, then it would return 0 for the random value.
+ //! AMD's PadlockRNG implementation appears to provide the same behavior.
+ //! \throws PadlockRNG_Err if the random number generator is not available
+ PadlockRNG();
+
+ //! \brief Generate random array of bytes
+ //! \param output the byte buffer
+ //! \param size the length of the buffer, in bytes
+ virtual void GenerateBlock(byte *output, size_t size);
+
+ //! \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.
+ virtual void DiscardBytes(size_t n);
+
+ //! \brief 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.
+ CRYPTOPP_UNUSED(input); CRYPTOPP_UNUSED(length);
+ }
+
+private:
+ FixedSizeAlignedSecBlock<word32, 1, true> m_buffer;
+};
+
+NAMESPACE_END
+
+#endif // CRYPTOPP_PADLOCK_RNG_H