iPXE
Macros | Functions | Variables
efi_rng.c File Reference

EFI random number generator protocol entropy source. More...

#include <errno.h>
#include <ipxe/entropy.h>
#include <ipxe/crc32.h>
#include <ipxe/efi/efi.h>
#include <ipxe/efi/Protocol/Rng.h>

Go to the source code of this file.

Macros

#define EFIRNG_LEN   32
 Minimum number of bytes to request from RNG. More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
struct entropy_source efirng_entropy __entropy_source (ENTROPY_NORMAL)
 EFI random number generator protocol entropy source. More...
 
 EFI_REQUEST_PROTOCOL (EFI_RNG_PROTOCOL, &efirng)
 
static int efirng_enable (void)
 Enable entropy gathering. More...
 
static int efirng_get_noise (noise_sample_t *noise)
 Get noise sample from RNG protocol. More...
 

Variables

static EFI_RNG_PROTOCOLefirng
 Random number generator protocol. More...
 

Detailed Description

EFI random number generator protocol entropy source.

Definition in file efi_rng.c.

Macro Definition Documentation

◆ EFIRNG_LEN

#define EFIRNG_LEN   32

Minimum number of bytes to request from RNG.

The UEFI spec states (for no apparently good reason) that "When a Deterministic Random Bit Generator (DRBG) is used on the output of a (raw) entropy source, its security level must be at least 256 bits." The EDK2 codebase (mis)interprets this to mean that the call to GetRNG() should fail if given a buffer less than 32 bytes.

Incidentally, nothing in the EFI RNG protocol provides any way to report the actual amount of entropy returned by GetRNG().

Definition at line 55 of file efi_rng.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ __entropy_source()

struct entropy_source rtc_entropy __entropy_source ( ENTROPY_NORMAL  )
Initial value:
= {
.name = "efirng",
.enable = efirng_enable,
.get_noise = efirng_get_noise,
}
static int efirng_get_noise(noise_sample_t *noise)
Get noise sample from RNG protocol.
Definition: efi_rng.c:86
static int efirng_enable(void)
Enable entropy gathering.
Definition: efi_rng.c:62

EFI random number generator protocol entropy source.

RTC entropy source.

◆ EFI_REQUEST_PROTOCOL()

EFI_REQUEST_PROTOCOL ( EFI_RNG_PROTOCOL  ,
efirng 
)

◆ efirng_enable()

static int efirng_enable ( void  )
static

Enable entropy gathering.

Return values
rcReturn status code

Definition at line 62 of file efi_rng.c.

62  {
63 
64  /* Check for RNG protocol support */
65  if ( ! efirng ) {
66  DBGC ( &efirng, "EFIRNG has no RNG protocol\n" );
67  return -ENOTSUP;
68  }
69 
70  /* Nothing in the EFI specification provides any clue as to
71  * how much entropy will be returned by GetRNG(). Make a
72  * totally uninformed (and conservative guess) that each
73  * sample will contain at least one bit of entropy.
74  */
75  entropy_init ( &efirng_entropy, MIN_ENTROPY ( 1.0 ) );
76 
77  return 0;
78 }
#define DBGC(...)
Definition: compiler.h:505
#define ENOTSUP
Operation not supported.
Definition: errno.h:589
#define MIN_ENTROPY(bits)
Construct a min-entropy fixed-point value.
Definition: entropy.h:42
static EFI_RNG_PROTOCOL * efirng
Random number generator protocol.
Definition: efi_rng.c:41
static void entropy_init(struct entropy_source *source, min_entropy_t min_entropy_per_sample)
Initialise entropy source.
Definition: entropy.h:489

References DBGC, efirng, ENOTSUP, entropy_init(), and MIN_ENTROPY.

◆ efirng_get_noise()

static int efirng_get_noise ( noise_sample_t noise)
static

Get noise sample from RNG protocol.

Return values
noiseNoise sample
rcReturn status code

Definition at line 86 of file efi_rng.c.

86  {
88  EFI_STATUS efirc;
89  int rc;
90 
91  /* Sanity check */
92  assert ( efirng != NULL );
93 
94  /* Get the minimum allowed number of random bytes */
95  if ( ( efirc = efirng->GetRNG ( efirng, NULL, sizeof ( buf ),
96  buf ) ) != 0 ) {
97  rc = -EEFI ( efirc );
98  DBGC ( &efirng, "ENTROPY could not read from RNG: %s\n",
99  strerror ( rc ) );
100  return rc;
101  }
102 
103  /* Reduce random bytes to a single noise sample. This seems
104  * like overkill, but we have no way of knowing how much
105  * entropy is actually present in the bytes returned by the
106  * RNG protocol.
107  */
108  *noise = crc32_le ( 0, buf, sizeof ( buf ) );
109 
110  return 0;
111 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define EEFI(efirc)
Convert an EFI status code to an iPXE status code.
Definition: efi.h:171
#define DBGC(...)
Definition: compiler.h:505
struct xfer_buffer buf
Data transfer buffer.
Definition: efi_pxe.c:94
#define EFIRNG_LEN
Minimum number of bytes to request from RNG.
Definition: efi_rng.c:55
u32 crc32_le(u32 seed, const void *data, size_t len)
Calculate 32-bit little-endian CRC checksum.
Definition: crc32.c:39
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
unsigned char uint8_t
Definition: stdint.h:10
RETURN_STATUS EFI_STATUS
Function return status for EFI API.
Definition: UefiBaseType.h:31
EFI_RNG_GET_RNG GetRNG
Definition: Rng.h:150
static EFI_RNG_PROTOCOL * efirng
Random number generator protocol.
Definition: efi_rng.c:41
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References assert(), crc32_le(), DBGC, EEFI, efirng, EFIRNG_LEN, _EFI_RNG_PROTOCOL::GetRNG, NULL, rc, and strerror().

Variable Documentation

◆ efirng

EFI_RNG_PROTOCOL* efirng
static

Random number generator protocol.

Definition at line 41 of file efi_rng.c.

Referenced by efirng_enable(), and efirng_get_noise().