2 * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
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.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
27 #include <ipxe/entropy.h>
28 #include <ipxe/crc32.h>
29 #include <ipxe/efi/efi.h>
30 #include <ipxe/efi/Protocol/Rng.h>
38 /** Random number generator protocol */
39 static EFI_RNG_PROTOCOL *efirng;
40 EFI_REQUEST_PROTOCOL ( EFI_RNG_PROTOCOL, &efirng );
42 /** Minimum number of bytes to request from RNG
44 * The UEFI spec states (for no apparently good reason) that "When a
45 * Deterministic Random Bit Generator (DRBG) is used on the output of
46 * a (raw) entropy source, its security level must be at least 256
47 * bits." The EDK2 codebase (mis)interprets this to mean that the
48 * call to GetRNG() should fail if given a buffer less than 32 bytes.
50 * Incidentally, nothing in the EFI RNG protocol provides any way to
51 * report the actual amount of entropy returned by GetRNG().
53 #define EFI_ENTROPY_RNG_LEN 32
55 /** Time (in 100ns units) to delay waiting for timer tick
57 * In theory, UEFI allows us to specify a trigger time of zero to
58 * simply wait for the next timer tick. In practice, specifying zero
59 * seems to often return immediately, which produces almost no
60 * entropy. Specify a delay of 1000ns to try to force an existent
63 #define EFI_ENTROPY_TRIGGER_TIME 10
65 /** Event used to wait for timer tick */
66 static EFI_EVENT tick;
69 * Enable entropy gathering
71 * @ret rc Return status code
73 static int efi_entropy_enable ( void ) {
74 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
78 DBGC ( &tick, "ENTROPY %s RNG protocol\n",
79 ( efirng ? "has" : "has no" ) );
81 /* Create timer tick event */
82 if ( ( efirc = bs->CreateEvent ( EVT_TIMER, TPL_NOTIFY, NULL, NULL,
85 DBGC ( &tick, "ENTROPY could not create event: %s\n",
94 * Disable entropy gathering
97 static void efi_entropy_disable ( void ) {
98 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
100 /* Close timer tick event */
101 bs->CloseEvent ( tick );
105 * Wait for a timer tick
107 * @ret low TSC low-order bits, or negative error
109 static int efi_entropy_tick ( void ) {
110 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
117 /* Wait for next timer tick */
118 if ( ( efirc = bs->SetTimer ( tick, TimerRelative,
119 EFI_ENTROPY_TRIGGER_TIME ) ) != 0 ) {
120 rc = -EEFI ( efirc );
121 DBGC ( &tick, "ENTROPY could not set timer: %s\n",
125 if ( ( efirc = bs->WaitForEvent ( 1, &tick, &index ) ) != 0 ) {
126 rc = -EEFI ( efirc );
127 DBGC ( &tick, "ENTROPY could not wait for timer tick: %s\n",
132 /* Get current TSC low-order bits */
133 __asm__ __volatile__ ( "rdtsc" : "=a" ( low ), "=d" ( discard_d ) );
139 * Get noise sample from timer ticks
141 * @ret noise Noise sample
142 * @ret rc Return status code
144 static int efi_get_noise_ticks ( noise_sample_t *noise ) {
149 /* Wait for a timer tick */
150 before = efi_entropy_tick();
156 /* Wait for another timer tick */
157 after = efi_entropy_tick();
163 /* Use TSC delta as noise sample */
164 *noise = ( after - before );
170 * Get noise sample from RNG protocol
172 * @ret noise Noise sample
173 * @ret rc Return status code
175 static int efi_get_noise_rng ( noise_sample_t *noise ) {
176 uint8_t buf[EFI_ENTROPY_RNG_LEN];
180 /* Fail if we have no EFI RNG protocol */
184 /* Get the minimum allowed number of random bytes */
185 if ( ( efirc = efirng->GetRNG ( efirng, NULL, EFI_ENTROPY_RNG_LEN,
187 rc = -EEFI ( efirc );
188 DBGC ( &tick, "ENTROPY could not read from RNG: %s\n",
193 /* Reduce random bytes to a single noise sample. This seems
194 * like overkill, but we have no way of knowing how much
195 * entropy is actually present in the bytes returned by the
198 *noise = crc32_le ( 0, buf, sizeof ( buf ) );
206 * @ret noise Noise sample
207 * @ret rc Return status code
209 static int efi_get_noise ( noise_sample_t *noise ) {
212 /* Try RNG first, falling back to timer ticks */
213 if ( ( ( rc = efi_get_noise_rng ( noise ) ) != 0 ) &&
214 ( ( rc = efi_get_noise_ticks ( noise ) ) != 0 ) )
220 PROVIDE_ENTROPY_INLINE ( efi, min_entropy_per_sample );
221 PROVIDE_ENTROPY ( efi, entropy_enable, efi_entropy_enable );
222 PROVIDE_ENTROPY ( efi, entropy_disable, efi_entropy_disable );
223 PROVIDE_ENTROPY ( efi, get_noise, efi_get_noise );