iPXE
efi_rng.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 #include <errno.h>
27 #include <ipxe/entropy.h>
28 #include <ipxe/crc32.h>
29 #include <ipxe/efi/efi.h>
30 #include <ipxe/efi/Protocol/Rng.h>
31 
32 /** @file
33  *
34  * EFI random number generator protocol entropy source
35  *
36  */
37 
38 struct entropy_source efirng_entropy __entropy_source ( ENTROPY_NORMAL );
39 
40 /** Random number generator protocol */
43 
44 /** Minimum number of bytes to request from RNG
45  *
46  * The UEFI spec states (for no apparently good reason) that "When a
47  * Deterministic Random Bit Generator (DRBG) is used on the output of
48  * a (raw) entropy source, its security level must be at least 256
49  * bits." The EDK2 codebase (mis)interprets this to mean that the
50  * call to GetRNG() should fail if given a buffer less than 32 bytes.
51  *
52  * Incidentally, nothing in the EFI RNG protocol provides any way to
53  * report the actual amount of entropy returned by GetRNG().
54  */
55 #define EFIRNG_LEN 32
56 
57 /** Maximum number of times to attempting requesting data from RNG
58  *
59  * The UEFI spec allows GetRNG() to return EFI_NOT_READY, which is not
60  * a particularly helpful error status since there is nothing that can
61  * sensibly be done except to retry immediately. We retry failed
62  * calls to GetRNG() (for any reason) up to this number of times.
63  */
64 #define EFIRNG_MAX_RETRY 16
65 
66 /**
67  * Enable entropy gathering
68  *
69  * @ret rc Return status code
70  */
71 static int efirng_enable ( void ) {
72 
73  /* Check for RNG protocol support */
74  if ( ! efirng ) {
75  DBGC ( &efirng, "EFIRNG has no RNG protocol\n" );
76  return -ENOTSUP;
77  }
78 
79  /* Nothing in the EFI specification provides any clue as to
80  * how much entropy will be returned by GetRNG(). Make a
81  * totally uninformed (and conservative guess) that each
82  * sample will contain at least one bit of entropy.
83  */
84  entropy_init ( &efirng_entropy, MIN_ENTROPY ( 1.0 ) );
85 
86  return 0;
87 }
88 
89 /**
90  * Get noise sample from RNG protocol
91  *
92  * @ret noise Noise sample
93  * @ret rc Return status code
94  */
95 static int efirng_get_noise ( noise_sample_t *noise ) {
96  uint8_t buf[EFIRNG_LEN];
97  unsigned int i;
98  EFI_STATUS efirc;
99  int rc;
100 
101  /* Sanity check */
102  assert ( efirng != NULL );
103 
104  /* Get random bytes, retrying if needed */
105  for ( i = 0 ; i < EFIRNG_MAX_RETRY ; i++ ) {
106 
107  /* Get the minimum allowed number of random bytes */
108  if ( ( efirc = efirng->GetRNG ( efirng, NULL, sizeof ( buf ),
109  buf ) ) != 0 ) {
110  rc = -EEFI ( efirc );
111  continue;
112  }
113 
114  /* Reduce random bytes to a single noise sample. This
115  * seems like overkill, but we have no way of knowing
116  * how much entropy is actually present in the bytes
117  * returned by the RNG protocol.
118  */
119  *noise = crc32_le ( 0, buf, sizeof ( buf ) );
120  return 0;
121  }
122 
123  DBGC ( &efirng, "ENTROPY could not read from RNG: %s\n",
124  strerror ( rc ) );
125  return rc;
126 }
127 
128 /** EFI random number generator protocol entropy source */
129 struct entropy_source efirng_entropy __entropy_source ( ENTROPY_NORMAL ) = {
130  .name = "efirng",
131  .enable = efirng_enable,
132  .get_noise = efirng_get_noise,
133 };
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:174
Error codes.
#define ENTROPY_NORMAL
Normal entropy source.
Definition: entropy.h:180
struct entropy_source efirng_entropy __entropy_source(ENTROPY_NORMAL)
EFI random number generator protocol entropy source.
#define DBGC(...)
Definition: compiler.h:505
#define EFIRNG_LEN
Minimum number of bytes to request from RNG.
Definition: efi_rng.c:55
The Random Number Generator (RNG) interface provides random bits for use in applications,...
Definition: Rng.h:144
An entropy source.
Definition: entropy.h:116
#define ENOTSUP
Operation not supported.
Definition: errno.h:589
const char * name
Name.
Definition: entropy.h:118
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)
EFI_RNG_PROTOCOL as defined in UEFI 2.4.
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
unsigned char uint8_t
Definition: stdint.h:10
#define MIN_ENTROPY(bits)
Construct a min-entropy fixed-point value.
Definition: entropy.h:42
EFI API.
static int efirng_get_noise(noise_sample_t *noise)
Get noise sample from RNG protocol.
Definition: efi_rng.c:95
EFI_RNG_GET_RNG GetRNG
Definition: Rng.h:146
RETURN_STATUS EFI_STATUS
Function return status for EFI API.
Definition: UefiBaseType.h:31
EFI_REQUEST_PROTOCOL(EFI_RNG_PROTOCOL, &efirng)
uint8_t noise_sample_t
A noise sample.
Definition: entropy.h:21
static EFI_RNG_PROTOCOL * efirng
Random number generator protocol.
Definition: efi_rng.c:41
#define EFIRNG_MAX_RETRY
Maximum number of times to attempting requesting data from RNG.
Definition: efi_rng.c:64
static int efirng_enable(void)
Enable entropy gathering.
Definition: efi_rng.c:71
static void entropy_init(struct entropy_source *source, min_entropy_t min_entropy_per_sample)
Initialise entropy source.
Definition: entropy.h:489
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
Entropy source.