[efi] Use a timer event to generate the currticks() timer
[ipxe.git] / src / 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/profile.h>
30 #include <ipxe/efi/efi.h>
31 #include <ipxe/efi/Protocol/Rng.h>
32
33 /** @file
34 *
35 * EFI entropy source
36 *
37 */
38
39 /** Random number generator protocol */
40 static EFI_RNG_PROTOCOL *efirng;
41 EFI_REQUEST_PROTOCOL ( EFI_RNG_PROTOCOL, &efirng );
42
43 /** Minimum number of bytes to request from RNG
44 *
45 * The UEFI spec states (for no apparently good reason) that "When a
46 * Deterministic Random Bit Generator (DRBG) is used on the output of
47 * a (raw) entropy source, its security level must be at least 256
48 * bits." The EDK2 codebase (mis)interprets this to mean that the
49 * call to GetRNG() should fail if given a buffer less than 32 bytes.
50 *
51 * Incidentally, nothing in the EFI RNG protocol provides any way to
52 * report the actual amount of entropy returned by GetRNG().
53 */
54 #define EFI_ENTROPY_RNG_LEN 32
55
56 /** Time (in 100ns units) to delay waiting for timer tick
57 *
58 * In theory, UEFI allows us to specify a trigger time of zero to
59 * simply wait for the next timer tick. In practice, specifying zero
60 * seems to often return immediately, which produces almost no
61 * entropy. Specify a delay of 1000ns to try to force an existent
62 * delay.
63 */
64 #define EFI_ENTROPY_TRIGGER_TIME 10
65
66 /** Event used to wait for timer tick */
67 static EFI_EVENT tick;
68
69 /**
70 * Enable entropy gathering
71 *
72 * @ret rc Return status code
73 */
74 static int efi_entropy_enable ( void ) {
75 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
76 EFI_STATUS efirc;
77 int rc;
78
79 DBGC ( &tick, "ENTROPY %s RNG protocol\n",
80 ( efirng ? "has" : "has no" ) );
81
82 /* Create timer tick event */
83 if ( ( efirc = bs->CreateEvent ( EVT_TIMER, TPL_NOTIFY, NULL, NULL,
84 &tick ) ) != 0 ) {
85 rc = -EEFI ( efirc );
86 DBGC ( &tick, "ENTROPY could not create event: %s\n",
87 strerror ( rc ) );
88 return rc;
89 }
90
91 return 0;
92 }
93
94 /**
95 * Disable entropy gathering
96 *
97 */
98 static void efi_entropy_disable ( void ) {
99 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
100
101 /* Close timer tick event */
102 bs->CloseEvent ( tick );
103 }
104
105 /**
106 * Wait for a timer tick
107 *
108 * @ret low CPU profiling low-order bits, or negative error
109 */
110 static int efi_entropy_tick ( void ) {
111 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
112 UINTN index;
113 uint16_t low;
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 CPU profiling timestamp low-order bits */
133 low = profile_timestamp();
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 );