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 /**
58  * Enable entropy gathering
59  *
60  * @ret rc Return status code
61  */
62 static int efirng_enable ( void ) {
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 }
79 
80 /**
81  * Get noise sample from RNG protocol
82  *
83  * @ret noise Noise sample
84  * @ret rc Return status code
85  */
86 static int efirng_get_noise ( noise_sample_t *noise ) {
87  uint8_t buf[EFIRNG_LEN];
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 }
112 
113 /** EFI random number generator protocol entropy source */
114 struct entropy_source efirng_entropy __entropy_source ( ENTROPY_NORMAL ) = {
115  .name = "efirng",
116  .enable = efirng_enable,
117  .get_noise = efirng_get_noise,
118 };
EFI_RNG_PROTOCOL as defined in UEFI 2.4.
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
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) protocol provides random bits for use in applications,...
Definition: Rng.h:148
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)
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:86
RETURN_STATUS EFI_STATUS
Function return status for EFI API.
Definition: UefiBaseType.h:31
EFI_RNG_GET_RNG GetRNG
Definition: Rng.h:150
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
static int efirng_enable(void)
Enable entropy gathering.
Definition: efi_rng.c:62
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.