Security Scol plugin
rdrand.h
Go to the documentation of this file.
1// rdrand.h - written and placed in public domain by Jeffrey Walton and Uri Blumenthal.
2
6
7#ifndef CRYPTOPP_RDRAND_H
8#define CRYPTOPP_RDRAND_H
9
10#include "cryptlib.h"
11
12// This class file provides both RDRAND and RDSEED. They were added at
13// Crypto++ 5.6.3. At compile time, it uses CRYPTOPP_BOOL_{X86|X32|X64}
14// to select an implementation or "throw NotImplemented". At runtime the
15// constructor will throw RDRAND_Err or RDSEED_Err if a generator is
16// is not available.
17// The original classes accepted a retry count. Retries were superfluous for
18// RDRAND, and RDSEED encountered a failure about 1 in 256 bytes depending
19// on the processor. Retries were removed at Crypto++ 6.0 because
20// GenerateBlock unconditionally retries and always fulfills the request.
21
22// Throughput varies wildly depending on processor and manufacturer. A Core i5 or
23// Core i7 RDRAND can generate at over 200 MiB/s. It is below theroetical
24// maximum, but it takes about 5 instructions to generate, retry and store a
25// result. A low-end Celeron may perform RDRAND at about 7 MiB/s. RDSEED
26// performs at about 1/4 to 1/2 the rate of RDRAND. AMD RDRAND performed poorly
27// during testing with Athlon X4 845. The Bulldozer v4 only performed at 1 MiB/s.
28
29// Microsoft added RDRAND in August 2012, VS2012; RDSEED in October 2013, VS2013.
30// GCC added RDRAND in December 2010, GCC 4.6. LLVM added RDRAND in July 2012,
31// Clang 3.2. Intel added RDRAND in September 2011, ICC 12.1.
32
33NAMESPACE_BEGIN(CryptoPP)
34
35
38class RDRAND_Err : public Exception
39{
40public:
41 RDRAND_Err(const std::string &operation)
42 : Exception(OTHER_ERROR, "RDRAND: " + operation + " operation failed") {}
43};
44
49{
50public:
51 CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() { return "RDRAND"; }
52
53 virtual ~RDRAND() {}
54
60 RDRAND();
61
65 virtual void GenerateBlock(byte *output, size_t size);
66
72 virtual void DiscardBytes(size_t n);
73
78 virtual void IncorporateEntropy(const byte *input, size_t length)
79 {
80 // Override to avoid the base class' throw.
81 CRYPTOPP_UNUSED(input); CRYPTOPP_UNUSED(length);
82 }
83
84 std::string AlgorithmProvider() const {
85 return "RDRAND";
86 }
87};
88
92class RDSEED_Err : public Exception
93{
94public:
95 RDSEED_Err(const std::string &operation)
96 : Exception(OTHER_ERROR, "RDSEED: " + operation + " operation failed") {}
97};
98
103{
104public:
105 CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() { return "RDSEED"; }
106
107 virtual ~RDSEED() {}
108
114 RDSEED();
115
119 virtual void GenerateBlock(byte *output, size_t size);
120
126 virtual void DiscardBytes(size_t n);
127
132 virtual void IncorporateEntropy(const byte *input, size_t length)
133 {
134 // Override to avoid the base class' throw.
135 CRYPTOPP_UNUSED(input); CRYPTOPP_UNUSED(length);
136 }
137
138 std::string AlgorithmProvider() const {
139 return "RDSEED";
140 }
141};
142
143NAMESPACE_END
144
145#endif // CRYPTOPP_RDRAND_H
Base class for all exceptions thrown by the library.
Definition cryptlib.h:159
@ OTHER_ERROR
Some other error occurred not belonging to other categories.
Definition cryptlib.h:177
Exception thrown when a RDRAND generator encounters a generator related error.
Definition rdrand.h:39
Hardware generated random numbers using RDRAND instruction.
Definition rdrand.h:49
RDRAND()
Construct a RDRAND generator.
Definition rdrand.cpp:287
virtual void GenerateBlock(byte *output, size_t size)
Generate random array of bytes.
Definition rdrand.cpp:292
virtual void DiscardBytes(size_t n)
Generate and discard n bytes.
Definition rdrand.cpp:298
std::string AlgorithmProvider() const
Retrieve the provider of this algorithm.
Definition rdrand.h:84
virtual void IncorporateEntropy(const byte *input, size_t length)
Update RNG state with additional unpredictable values.
Definition rdrand.h:78
Exception thrown when a RDSEED generator encounters a generator related error.
Definition rdrand.h:93
Hardware generated random numbers using RDSEED instruction.
Definition rdrand.h:103
RDSEED()
Construct a RDSEED generator.
Definition rdrand.cpp:308
virtual void IncorporateEntropy(const byte *input, size_t length)
Update RNG state with additional unpredictable values.
Definition rdrand.h:132
virtual void GenerateBlock(byte *output, size_t size)
Generate random array of bytes.
Definition rdrand.cpp:313
virtual void DiscardBytes(size_t n)
Generate and discard n bytes.
Definition rdrand.cpp:319
std::string AlgorithmProvider() const
Retrieve the provider of this algorithm.
Definition rdrand.h:138
Interface for random number generators.
Definition cryptlib.h:1435
Abstract base classes that provide a uniform interface to this library.