Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
282 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -191,6 +191,8 @@ oids.h | |
osrng.cpp | ||
osrng.h | ||
ossig.h | ||
padlkrng.cpp | ||
padlkrng.h | ||
panama.cpp | ||
panama.h | ||
pch.cpp | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// via-rng.cpp - written and placed in public domain by Jeffrey Walton and Uri Blumenthal. | ||
|
||
#include "pch.h" | ||
#include "config.h" | ||
#include "cryptlib.h" | ||
#include "secblock.h" | ||
#include "padlkrng.h" | ||
#include "cpu.h" | ||
|
||
NAMESPACE_BEGIN(CryptoPP) | ||
|
||
PadlockRNG::PadlockRNG() | ||
{ | ||
#if CRYPTOPP_BOOL_X86 | ||
if (!HasPadlockRNG()) | ||
throw PadlockRNG_Err("HasPadlockRNG"); | ||
#else | ||
throw PadlockRNG_Err("HasPadlockRNG"); | ||
#endif | ||
} | ||
|
||
void PadlockRNG::GenerateBlock(byte *output, size_t size) | ||
{ | ||
CRYPTOPP_UNUSED(output); CRYPTOPP_UNUSED(size); | ||
#if CRYPTOPP_BOOL_X86 | ||
while (size) | ||
{ | ||
# if defined(__GNUC__) | ||
|
||
word32 result; | ||
__asm__ __volatile__ | ||
( | ||
"movl %1, %%edi ;\n" | ||
"movl $1, %%edx ;\n" | ||
".byte 0x0f, 0xa7, 0xc0 ;\n" | ||
"andl $31, %%eax ;\n" | ||
"movl %%eax, %0 ;\n" | ||
|
||
: "=g" (result) : "g" (m_buffer.begin()) : "eax", "edx", "edi", "cc" | ||
); | ||
|
||
const size_t rem = STDMIN(result, STDMIN(size, m_buffer.SizeInBytes())); | ||
std::memcpy(output, m_buffer, rem); | ||
size -= rem; output += rem; | ||
|
||
# elif defined(_MSC_VER) | ||
|
||
word32 result; | ||
byte* buffer = reinterpret_cast<byte*>(m_buffer.begin()); | ||
|
||
__asm { | ||
mov edi, buffer | ||
mov edx, 0x01 | ||
_emit 0x0f | ||
_emit 0xa7 | ||
_emit 0xc0 | ||
and eax, 31 | ||
mov result, eax | ||
} | ||
|
||
const size_t rem = STDMIN(result, STDMIN(size, m_buffer.SizeInBytes())); | ||
std::memcpy(output, m_buffer, rem); | ||
size -= rem; output += rem; | ||
|
||
# else | ||
throw NotImplemented("PadlockRNG::GenerateBlock"); | ||
# endif | ||
} | ||
#endif // CRYPTOPP_BOOL_X86 | ||
} | ||
|
||
void PadlockRNG::DiscardBytes(size_t n) | ||
{ | ||
FixedSizeSecBlock<word32, 4> discard; | ||
n = RoundUpToMultipleOf(n, sizeof(word32)); | ||
|
||
size_t count = STDMIN(n, discard.SizeInBytes()); | ||
while (count) | ||
{ | ||
GenerateBlock(discard.BytePtr(), count); | ||
n -= count; | ||
count = STDMIN(n, discard.SizeInBytes()); | ||
} | ||
} | ||
|
||
NAMESPACE_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7fb5953
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also see VIA Padlock on the Crypto++ wiki.