iPXE
rdrand.c File Reference

Hardware random number generator. More...

#include <errno.h>
#include <ipxe/cpuid.h>
#include <ipxe/entropy.h>
#include <ipxe/drbg.h>

Go to the source code of this file.

Macros

#define RDRAND_RETRY_COUNT   16
 Number of times to retry RDRAND instruction.
#define colour   &rdrand_entropy
 Colour for debug messages.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 FILE_SECBOOT (PERMITTED)
struct entropy_source rdrand_entropy __entropy_source (ENTROPY_PREFERRED)
 Hardware random number generator entropy source.
static int rdrand_entropy_enable (void)
 Enable entropy gathering.
static int rdrand_get_noise (noise_sample_t *noise)
 Get noise sample.

Detailed Description

Hardware random number generator.

Definition in file rdrand.c.

Macro Definition Documentation

◆ RDRAND_RETRY_COUNT

#define RDRAND_RETRY_COUNT   16

Number of times to retry RDRAND instruction.

Definition at line 41 of file rdrand.c.

Referenced by rdrand_get_noise().

◆ colour

#define colour   &rdrand_entropy

Colour for debug messages.

Definition at line 44 of file rdrand.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ __entropy_source()

struct entropy_source rdrand_entropy __entropy_source ( ENTROPY_PREFERRED )
Initial value:
= {
.name = "rdrand",
.get_noise = rdrand_get_noise,
}
static int rdrand_get_noise(noise_sample_t *noise)
Get noise sample.
Definition rdrand.c:78
static int rdrand_entropy_enable(void)
Enable entropy gathering.
Definition rdrand.c:51

Hardware random number generator entropy source.

References __entropy_source, and ENTROPY_PREFERRED.

◆ rdrand_entropy_enable()

int rdrand_entropy_enable ( void )
static

Enable entropy gathering.

Return values
rcReturn status code

Definition at line 51 of file rdrand.c.

51 {
53
54 /* Check that RDRAND is supported */
56 if ( ! ( features.intel.ecx & CPUID_FEATURES_INTEL_ECX_RDRAND ) ) {
57 DBGC ( colour, "RDRAND not supported\n" );
58 return -ENOTSUP;
59 }
60
61 /* Data returned by RDRAND is theoretically full entropy, up
62 * to a security strength of 128 bits, so assume that each
63 * sample contains exactly 8 bits of entropy.
64 */
65 if ( DRBG_SECURITY_STRENGTH > 128 )
66 return -ENOTSUP;
67 entropy_init ( &rdrand_entropy, MIN_ENTROPY ( 8.0 ) );
68
69 return 0;
70}
#define colour
Colour for debug messages.
Definition acpi.c:42
void x86_features(struct x86_features *features)
Get x86 CPU features.
Definition cpuid.c:164
#define CPUID_FEATURES_INTEL_ECX_RDRAND
RDRAND instruction is supported.
Definition cpuid.h:47
#define DRBG_SECURITY_STRENGTH
Security strength.
Definition drbg.h:31
uint32_t features
Supported features.
Definition ena.h:5
#define DBGC(...)
Definition compiler.h:505
#define ENOTSUP
Operation not supported.
Definition errno.h:590
static void entropy_init(struct entropy_source *source, min_entropy_t min_entropy_per_sample)
Initialise entropy source.
Definition entropy.h:490
#define MIN_ENTROPY(bits)
Construct a min-entropy fixed-point value.
Definition entropy.h:43
x86 CPU features
Definition cpuid.h:24

References colour, CPUID_FEATURES_INTEL_ECX_RDRAND, DBGC, DRBG_SECURITY_STRENGTH, ENOTSUP, entropy_init(), features, MIN_ENTROPY, and x86_features().

◆ rdrand_get_noise()

int rdrand_get_noise ( noise_sample_t * noise)
static

Get noise sample.

Return values
noiseNoise sample
rcReturn status code

Definition at line 78 of file rdrand.c.

78 {
79 unsigned int result;
80 unsigned int discard_c;
81 unsigned int ok;
82
83 /* Issue RDRAND, retrying until CF is set */
84 __asm__ ( "\n1:\n\t"
85 "rdrand %0\n\t"
86 "sbb %1, %1\n\t"
87 "loopz 1b\n\t"
88 : "=r" ( result ), "=r" ( ok ), "=c" ( discard_c )
89 : "2" ( RDRAND_RETRY_COUNT ) );
90 if ( ! ok ) {
91 DBGC ( colour, "RDRAND failed to become ready\n" );
92 return -EBUSY;
93 }
94
95 *noise = result;
96 return 0;
97}
uint16_t result
Definition hyperv.h:33
long discard_c
Definition bigint.h:33
#define EBUSY
Device or resource busy.
Definition errno.h:339
__asm__(".section \".rodata\", \"a\", " PROGBITS "\n\t" "\nprivate_key_data:\n\t" ".size private_key_data, ( . - private_key_data )\n\t" ".equ private_key_len, ( . - private_key_data )\n\t" ".previous\n\t")
#define RDRAND_RETRY_COUNT
Number of times to retry RDRAND instruction.
Definition rdrand.c:41
#define ok(success)
Definition test.h:46

References __asm__(), colour, DBGC, discard_c, EBUSY, ok, RDRAND_RETRY_COUNT, and result.