iPXE
Macros | Functions
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. More...
 
#define colour   &rdrand_entropy
 Colour for debug messages. More...
 

Functions

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

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 40 of file rdrand.c.

◆ colour

#define colour   &rdrand_entropy

Colour for debug messages.

Definition at line 43 of file rdrand.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ __entropy_source()

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

Hardware random number generator entropy source.

◆ rdrand_entropy_enable()

static int rdrand_entropy_enable ( void  )
static

Enable entropy gathering.

Return values
rcReturn status code

Definition at line 50 of file rdrand.c.

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

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

◆ rdrand_get_noise()

static int rdrand_get_noise ( noise_sample_t noise)
static

Get noise sample.

Return values
noiseNoise sample
rcReturn status code

Definition at line 77 of file rdrand.c.

77  {
78  unsigned int result;
79  unsigned int discard_c;
80  unsigned int ok;
81 
82  /* Issue RDRAND, retrying until CF is set */
83  __asm__ ( "\n1:\n\t"
84  "rdrand %0\n\t"
85  "sbb %1, %1\n\t"
86  "loopz 1b\n\t"
87  : "=r" ( result ), "=r" ( ok ), "=c" ( discard_c )
88  : "2" ( RDRAND_RETRY_COUNT ) );
89  if ( ! ok ) {
90  DBGC ( colour, "RDRAND failed to become ready\n" );
91  return -EBUSY;
92  }
93 
94  *noise = result;
95  return 0;
96 }
static const void const void void * result
Definition: crypto.h:335
#define EBUSY
Device or resource busy.
Definition: errno.h:338
#define RDRAND_RETRY_COUNT
Number of times to retry RDRAND instruction.
Definition: rdrand.c:40
#define colour
Colour for debug messages.
Definition: rdrand.c:43
#define DBGC(...)
Definition: compiler.h:505
__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")
long discard_c
Definition: bigint.h:32
#define ok(success)
Definition: test.h:46

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