2017-08-19 19:41:45 +00:00
|
|
|
// 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"
|
|
|
|
|
2017-08-20 08:09:19 +00:00
|
|
|
// The Padlock Security Engine RNG has a few items to be aware of. You can
|
|
|
|
// find copies of the Programmer's manual, Cryptography Research Inc audit
|
|
|
|
// report, and other goodies at http://www.cryptopp.com/wiki/VIA_Padlock.
|
|
|
|
|
2018-01-21 21:25:49 +00:00
|
|
|
#if CRYPTOPP_MSC_VERSION
|
|
|
|
# pragma warning(disable: 4702)
|
|
|
|
#endif
|
|
|
|
|
2017-08-19 19:41:45 +00:00
|
|
|
NAMESPACE_BEGIN(CryptoPP)
|
|
|
|
|
2018-07-06 13:23:37 +00:00
|
|
|
std::string PadlockRNG::AlgorithmProvider() const
|
|
|
|
{
|
|
|
|
return "Padlock";
|
|
|
|
}
|
|
|
|
|
2017-08-20 08:09:19 +00:00
|
|
|
PadlockRNG::PadlockRNG(word32 divisor)
|
2018-07-27 15:21:54 +00:00
|
|
|
: m_divisor(DivisorHelper(divisor)), m_msr(0)
|
2017-08-19 19:41:45 +00:00
|
|
|
{
|
2017-08-20 08:40:57 +00:00
|
|
|
#if defined(CRYPTOPP_X86_ASM_AVAILABLE)
|
2017-08-20 08:09:19 +00:00
|
|
|
if (!HasPadlockRNG())
|
2017-08-19 19:41:45 +00:00
|
|
|
#endif
|
2018-07-27 15:21:54 +00:00
|
|
|
throw PadlockRNG_Err("PadlockRNG", "PadlockRNG generator not available");
|
2017-08-19 19:41:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PadlockRNG::GenerateBlock(byte *output, size_t size)
|
|
|
|
{
|
|
|
|
CRYPTOPP_UNUSED(output); CRYPTOPP_UNUSED(size);
|
2017-08-20 08:09:19 +00:00
|
|
|
#if defined(CRYPTOPP_X86_ASM_AVAILABLE) && defined(__GNUC__)
|
2017-08-19 19:41:45 +00:00
|
|
|
while (size)
|
|
|
|
{
|
|
|
|
__asm__ __volatile__
|
|
|
|
(
|
2017-08-20 15:19:32 +00:00
|
|
|
#if (CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64)
|
|
|
|
"mov %1, %%rdi ;\n"
|
2017-08-20 08:21:24 +00:00
|
|
|
"movl %2, %%edx ;\n"
|
2017-08-20 15:19:32 +00:00
|
|
|
#else
|
|
|
|
"mov %1, %%edi ;\n"
|
|
|
|
"movl %2, %%edx ;\n"
|
|
|
|
#endif
|
|
|
|
|
2020-07-27 15:41:28 +00:00
|
|
|
// xstore-rng
|
2017-08-19 19:41:45 +00:00
|
|
|
".byte 0x0f, 0xa7, 0xc0 ;\n"
|
|
|
|
"movl %%eax, %0 ;\n"
|
|
|
|
|
2017-08-20 08:09:19 +00:00
|
|
|
: "=g" (m_msr) : "g" (m_buffer.data()), "g" (m_divisor)
|
2017-08-25 00:24:32 +00:00
|
|
|
#if (CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64)
|
2018-07-27 20:51:32 +00:00
|
|
|
: "rax", "rdx", "rdi", "cc"
|
2017-08-20 15:19:32 +00:00
|
|
|
#else
|
2018-07-27 20:51:32 +00:00
|
|
|
: "eax", "edx", "edi", "cc"
|
2017-08-20 15:19:32 +00:00
|
|
|
#endif
|
2017-08-19 19:41:45 +00:00
|
|
|
);
|
|
|
|
|
2020-07-27 23:39:34 +00:00
|
|
|
const size_t ret = m_msr & 0x1f;
|
|
|
|
const size_t rem = STDMIN<size_t>(ret, STDMIN<size_t>(size, 16U /*buffer size*/));
|
2017-08-19 19:41:45 +00:00
|
|
|
std::memcpy(output, m_buffer, rem);
|
|
|
|
size -= rem; output += rem;
|
2017-08-20 08:09:19 +00:00
|
|
|
}
|
2017-08-20 15:19:32 +00:00
|
|
|
#elif defined(CRYPTOPP_X86_ASM_AVAILABLE) && defined(_MSC_VER) && defined(_M_IX86)
|
2017-08-20 08:09:19 +00:00
|
|
|
while (size)
|
|
|
|
{
|
|
|
|
word32 result, divisor = m_divisor;
|
|
|
|
byte *buffer = reinterpret_cast<byte*>(m_buffer.data());
|
2017-08-19 19:41:45 +00:00
|
|
|
__asm {
|
|
|
|
mov edi, buffer
|
2017-08-20 08:09:19 +00:00
|
|
|
mov edx, divisor
|
2017-08-19 19:41:45 +00:00
|
|
|
_emit 0x0f
|
|
|
|
_emit 0xa7
|
|
|
|
_emit 0xc0
|
|
|
|
mov result, eax
|
|
|
|
}
|
|
|
|
|
2020-07-27 23:39:34 +00:00
|
|
|
const size_t ret = (m_msr = result) & 0x1f;
|
|
|
|
const size_t rem = STDMIN<size_t>(ret, STDMIN<size_t>(size, 16U /*buffer size*/));
|
2017-08-20 08:09:19 +00:00
|
|
|
std::memcpy(output, buffer, rem);
|
2017-08-19 19:41:45 +00:00
|
|
|
size -= rem; output += rem;
|
|
|
|
}
|
2017-08-20 08:09:19 +00:00
|
|
|
#else
|
|
|
|
throw PadlockRNG_Err("GenerateBlock", "PadlockRNG generator not available");
|
|
|
|
#endif // CRYPTOPP_X86_ASM_AVAILABLE
|
2017-08-19 19:41:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PadlockRNG::DiscardBytes(size_t n)
|
|
|
|
{
|
2017-08-20 08:09:19 +00:00
|
|
|
FixedSizeSecBlock<word32, 4> discard;
|
|
|
|
n = RoundUpToMultipleOf(n, sizeof(word32));
|
2017-08-19 19:41:45 +00:00
|
|
|
|
2017-08-20 08:09:19 +00:00
|
|
|
size_t count = STDMIN(n, discard.SizeInBytes());
|
|
|
|
while (count)
|
|
|
|
{
|
|
|
|
GenerateBlock(discard.BytePtr(), count);
|
|
|
|
n -= count;
|
|
|
|
count = STDMIN(n, discard.SizeInBytes());
|
|
|
|
}
|
2017-08-19 19:41:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NAMESPACE_END
|