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 FILE_SECBOOT ( PERMITTED );
26 
27 #include <errno.h>
28 #include <ipxe/entropy.h>
29 #include <ipxe/crc32.h>
30 #include <ipxe/efi/efi.h>
31 #include <ipxe/efi/Protocol/Rng.h>
32 
33 /** @file
34  *
35  * EFI random number generator protocol entropy source
36  *
37  */
38 
39 struct entropy_source efirng_entropy __entropy_source ( ENTROPY_NORMAL );
40 
41 /** Random number generator protocol */
44 
45 /** Minimum number of bytes to request from RNG
46  *
47  * The UEFI spec states (for no apparently good reason) that "When a
48  * Deterministic Random Bit Generator (DRBG) is used on the output of
49  * a (raw) entropy source, its security level must be at least 256
50  * bits." The EDK2 codebase (mis)interprets this to mean that the
51  * call to GetRNG() should fail if given a buffer less than 32 bytes.
52  *
53  * Incidentally, nothing in the EFI RNG protocol provides any way to
54  * report the actual amount of entropy returned by GetRNG().
55  */
56 #define EFIRNG_LEN 32
57 
58 /** Maximum number of times to attempting requesting data from RNG
59  *
60  * The UEFI spec allows GetRNG() to return EFI_NOT_READY, which is not
61  * a particularly helpful error status since there is nothing that can
62  * sensibly be done except to retry immediately. We retry failed
63  * calls to GetRNG() (for any reason) up to this number of times.
64  */
65 #define EFIRNG_MAX_RETRY 16
66 
67 /**
68  * Enable entropy gathering
69  *
70  * @ret rc Return status code
71  */
72 static int efirng_enable ( void ) {
73 
74  /* Check for RNG protocol support */
75  if ( ! efirng ) {
76  DBGC ( &efirng, "EFIRNG has no RNG protocol\n" );
77  return -ENOTSUP;
78  }
79 
80  /* Nothing in the EFI specification provides any clue as to
81  * how much entropy will be returned by GetRNG(). Make a
82  * totally uninformed (and conservative guess) that each
83  * sample will contain at least one bit of entropy.
84  */
85  entropy_init ( &efirng_entropy, MIN_ENTROPY ( 1.0 ) );
86 
87  return 0;
88 }
89 
90 /**
91  * Get noise sample from RNG protocol
92  *
93  * @ret noise Noise sample
94  * @ret rc Return status code
95  */
96 static int efirng_get_noise ( noise_sample_t *noise ) {
97  uint8_t buf[EFIRNG_LEN];
98  unsigned int i;
99  EFI_STATUS efirc;
100  int rc;
101 
102  /* Sanity check */
103  assert ( efirng != NULL );
104 
105  /* Get random bytes, retrying if needed */
106  for ( i = 0 ; i < EFIRNG_MAX_RETRY ; i++ ) {
107 
108  /* Get the minimum allowed number of random bytes */
109  if ( ( efirc = efirng->GetRNG ( efirng, NULL, sizeof ( buf ),
110  buf ) ) != 0 ) {
111  rc = -EEFI ( efirc );
112  continue;
113  }
114 
115  /* Reduce random bytes to a single noise sample. This
116  * seems like overkill, but we have no way of knowing
117  * how much entropy is actually present in the bytes
118  * returned by the RNG protocol.
119  */
120  *noise = crc32_le ( 0, buf, sizeof ( buf ) );
121  return 0;
122  }
123 
124  DBGC ( &efirng, "ENTROPY could not read from RNG: %s\n",
125  strerror ( rc ) );
126  return rc;
127 }
128 
129 /** EFI random number generator protocol entropy source */
130 struct entropy_source efirng_entropy __entropy_source ( ENTROPY_NORMAL ) = {
131  .name = "efirng",
132  .enable = efirng_enable,
133  .get_noise = efirng_get_noise,
134 };
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:175
Error codes.
#define ENTROPY_NORMAL
Normal entropy source.
Definition: entropy.h:181
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:56
The Random Number Generator (RNG) interface provides random bits for use in applications,...
Definition: Rng.h:145
An entropy source.
Definition: entropy.h:117
#define ENOTSUP
Operation not supported.
Definition: errno.h:590
const char * name
Name.
Definition: entropy.h:119
u32 crc32_le(u32 seed, const void *data, size_t len)
Calculate 32-bit little-endian CRC checksum.
Definition: crc32.c:40
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:79
unsigned char uint8_t
Definition: stdint.h:10
#define MIN_ENTROPY(bits)
Construct a min-entropy fixed-point value.
Definition: entropy.h:43
EFI API.
static int efirng_get_noise(noise_sample_t *noise)
Get noise sample from RNG protocol.
Definition: efi_rng.c:96
EFI_RNG_GET_RNG GetRNG
Definition: Rng.h:147
RETURN_STATUS EFI_STATUS
Function return status for EFI API.
Definition: UefiBaseType.h:32
EFI_REQUEST_PROTOCOL(EFI_RNG_PROTOCOL, &efirng)
uint8_t noise_sample_t
A noise sample.
Definition: entropy.h:22
static EFI_RNG_PROTOCOL * efirng
Random number generator protocol.
Definition: efi_rng.c:42
#define EFIRNG_MAX_RETRY
Maximum number of times to attempting requesting data from RNG.
Definition: efi_rng.c:65
FILE_SECBOOT(PERMITTED)
static int efirng_enable(void)
Enable entropy gathering.
Definition: efi_rng.c:72
static void entropy_init(struct entropy_source *source, min_entropy_t min_entropy_per_sample)
Initialise entropy source.
Definition: entropy.h:490
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322
Entropy source.