[efi] Use a timer event to generate the currticks() timer
[ipxe.git] / src / arch / x86 / interface / efi / efi_entropy.c
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 entropy source
35 *
36 */
37
38 /** Random number generator protocol */
39 static EFI_RNG_PROTOCOL *efirng;
40 EFI_REQUEST_PROTOCOL ( EFI_RNG_PROTOCOL, &efirng );
41
42 /** Minimum number of bytes to request from RNG
43 *
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.
49 *
50 * Incidentally, nothing in the EFI RNG protocol provides any way to
51 * report the actual amount of entropy returned by GetRNG().
52 */
53 #define EFI_ENTROPY_RNG_LEN 32
54
55 /** Time (in 100ns units) to delay waiting for timer tick
56 *
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
61 * delay.
62 */
63 #define EFI_ENTROPY_TRIGGER_TIME 10
64
65 /** Event used to wait for timer tick */
66 static EFI_EVENT tick;
67
68 /**
69 * Enable entropy gathering
70 *
71 * @ret rc Return status code
72 */
73 static int efi_entropy_enable ( void ) {
74 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
75 EFI_STATUS efirc;
76 int rc;
77
78 DBGC ( &tick, "ENTROPY %s RNG protocol\n",
79 ( efirng ? "has" : "has no" ) );
80
81 /* Create timer tick event */
82 if ( ( efirc = bs->CreateEvent ( EVT_TIMER, TPL_NOTIFY, NULL, NULL,
83 &tick ) ) != 0 ) {
84 rc = -EEFI ( efirc );
85 DBGC ( &tick, "ENTROPY could not create event: %s\n",
86 strerror ( rc ) );
87 return rc;
88 }
89
90 return 0;
91 }
92
93 /**
94 * Disable entropy gathering
95 *
96 */
97 static void efi_entropy_disable ( void ) {
98 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
99
100 /* Close timer tick event */
101 bs->CloseEvent ( tick );
102 }
103
104 /**
105 * Wait for a timer tick
106 *
107 * @ret low TSC low-order bits, or negative error
108 */
109 static int efi_entropy_tick ( void ) {
110 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
111 UINTN index;
112 uint16_t low;
113 uint32_t discard_d;
114 EFI_STATUS efirc;
115 int rc;
116
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",
122 strerror ( rc ) );
123 return rc;
124 }
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",
128 strerror ( rc ) );
129 return rc;
130 }
131
132 /* Get current TSC low-order bits */
133 __asm__ __volatile__ ( "rdtsc" : "=a" ( low ), "=d" ( discard_d ) );
134
135 return low;
136 }
137
138 /**
139 * Get noise sample from timer ticks
140 *
141 * @ret noise Noise sample
142 * @ret rc Return status code
143 */
144 static int efi_get_noise_ticks ( noise_sample_t *noise ) {
145 int before;
146 int after;
147 int rc;
148
149 /* Wait for a timer tick */
150 before = efi_entropy_tick();
151 if ( before < 0 ) {
152 rc = before;
153 return rc;
154 }
155
156 /* Wait for another timer tick */
157 after = efi_entropy_tick();
158 if ( after < 0 ) {
159 rc = after;
160 return rc;
161 }
162
163 /* Use TSC delta as noise sample */
164 *noise = ( after - before );
165
166 return 0;
167 }
168
169 /**
170 * Get noise sample from RNG protocol
171 *
172 * @ret noise Noise sample
173 * @ret rc Return status code
174 */
175 static int efi_get_noise_rng ( noise_sample_t *noise ) {
176 uint8_t buf[EFI_ENTROPY_RNG_LEN];
177 EFI_STATUS efirc;
178 int rc;
179
180 /* Fail if we have no EFI RNG protocol */
181 if ( ! efirng )
182 return -ENOTSUP;
183
184 /* Get the minimum allowed number of random bytes */
185 if ( ( efirc = efirng->GetRNG ( efirng, NULL, EFI_ENTROPY_RNG_LEN,
186 buf ) ) != 0 ) {
187 rc = -EEFI ( efirc );
188 DBGC ( &tick, "ENTROPY could not read from RNG: %s\n",
189 strerror ( rc ) );
190 return rc;
191 }
192
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
196 * RNG protocol.
197 */
198 *noise = crc32_le ( 0, buf, sizeof ( buf ) );
199
200 return 0;
201 }
202
203 /**
204 * Get noise sample
205 *
206 * @ret noise Noise sample
207 * @ret rc Return status code
208 */
209 static int efi_get_noise ( noise_sample_t *noise ) {
210 int rc;
211
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 ) )
215 return rc;
216
217 return 0;
218 }
219
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 );