Crypto++ 8.2
Free C&
padlkrng.cpp
1// via-rng.cpp - written and placed in public domain by Jeffrey Walton and Uri Blumenthal.
2
3#include "pch.h"
4#include "config.h"
5#include "cryptlib.h"
6#include "secblock.h"
7#include "padlkrng.h"
8#include "cpu.h"
9
10// The Padlock Security Engine RNG has a few items to be aware of. You can
11// find copies of the Programmer's manual, Cryptography Research Inc audit
12// report, and other goodies at http://www.cryptopp.com/wiki/VIA_Padlock.
13
14#if CRYPTOPP_MSC_VERSION
15# pragma warning(disable: 4702)
16#endif
17
18NAMESPACE_BEGIN(CryptoPP)
19
20std::string PadlockRNG::AlgorithmProvider() const
21{
22 return "Padlock";
23}
24
26 : m_divisor(DivisorHelper(divisor)), m_msr(0)
27{
28#if defined(CRYPTOPP_X86_ASM_AVAILABLE)
29 if (!HasPadlockRNG())
30#endif
31 throw PadlockRNG_Err("PadlockRNG", "PadlockRNG generator not available");
32}
33
34void PadlockRNG::GenerateBlock(byte *output, size_t size)
35{
36 CRYPTOPP_UNUSED(output); CRYPTOPP_UNUSED(size);
37#if defined(CRYPTOPP_X86_ASM_AVAILABLE) && defined(__GNUC__)
38 while (size)
39 {
40 __asm__ __volatile__
41 (
42#if (CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64)
43 "mov %1, %%rdi ;\n"
44 "movl %2, %%edx ;\n"
45#else
46 "mov %1, %%edi ;\n"
47 "movl %2, %%edx ;\n"
48#endif
49
50 ".byte 0x0f, 0xa7, 0xc0 ;\n"
51 "movl %%eax, %0 ;\n"
52
53 : "=g" (m_msr) : "g" (m_buffer.data()), "g" (m_divisor)
54#if (CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64)
55 : "rax", "rdx", "rdi", "cc"
56#else
57 : "eax", "edx", "edi", "cc"
58#endif
59 );
60
61 const size_t ret = m_msr & 0x1f;
62 const size_t rem = STDMIN<size_t>(ret, STDMIN<size_t>(size, 16U /*buffer size*/));
63 std::memcpy(output, m_buffer, rem);
64 size -= rem; output += rem;
65 }
66#elif defined(CRYPTOPP_X86_ASM_AVAILABLE) && defined(_MSC_VER) && defined(_M_IX86)
67 while (size)
68 {
69 word32 result, divisor = m_divisor;
70 byte *buffer = reinterpret_cast<byte*>(m_buffer.data());
71 __asm {
72 mov edi, buffer
73 mov edx, divisor
74 _emit 0x0f
75 _emit 0xa7
76 _emit 0xc0
77 mov result, eax
78 }
79
80 const size_t ret = (m_msr = result) & 0x1f;
81 const size_t rem = STDMIN<size_t>(ret, STDMIN<size_t>(size, 16U /*buffer size*/));
82 std::memcpy(output, buffer, rem);
83 size -= rem; output += rem;
84 }
85#else
86 throw PadlockRNG_Err("GenerateBlock", "PadlockRNG generator not available");
87#endif // CRYPTOPP_X86_ASM_AVAILABLE
88}
89
91{
93 n = RoundUpToMultipleOf(n, sizeof(word32));
94
95 size_t count = STDMIN(n, discard.SizeInBytes());
96 while (count)
97 {
98 GenerateBlock(discard.BytePtr(), count);
99 n -= count;
100 count = STDMIN(n, discard.SizeInBytes());
101 }
102}
103
104NAMESPACE_END
Fixed size stack-based SecBlock.
Definition: secblock.h:1078
Exception thrown when a PadlockRNG generator encounters a generator related error.
Definition: padlkrng.h:21
Hardware generated random numbers using VIA XSTORE.
Definition: padlkrng.h:51
PadlockRNG(word32 divisor=1)
Construct a PadlockRNG generator.
Definition: padlkrng.cpp:25
virtual void DiscardBytes(size_t n)
Generate and discard n bytes.
Definition: padlkrng.cpp:90
virtual void GenerateBlock(byte *output, size_t size)
Generate random array of bytes.
Definition: padlkrng.cpp:34
A::pointer data()
Provides a pointer to the first element in the memory block.
Definition: secblock.h:789
size_type SizeInBytes() const
Provides the number of bytes in the SecBlock.
Definition: secblock.h:811
byte * BytePtr()
Provides a byte pointer to the first element in the memory block.
Definition: secblock.h:804
Library configuration file.
Functions for CPU features and intrinsics.
bool HasPadlockRNG()
Determines Padlock RNG availability.
Definition: cpu.h:269
Abstract base classes that provide a uniform interface to this library.
T1 RoundUpToMultipleOf(const T1 &n, const T2 &m)
Rounds a value up to a multiple of a second value.
Definition: misc.h:1085
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
Definition: misc.h:567
Crypto++ library namespace.
Classes for VIA Padlock RNG.
Precompiled header file.
Classes and functions for secure memory allocations.