iPXE
efi_entropy.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/profile.h>
29 #include <ipxe/efi/efi.h>
30 
31 /** @file
32  *
33  * EFI entropy source
34  *
35  */
36 
37 struct entropy_source efitick_entropy __entropy_source ( ENTROPY_FALLBACK );
38 
39 /** Time (in 100ns units) to delay waiting for timer tick
40  *
41  * In theory, UEFI allows us to specify a trigger time of zero to
42  * simply wait for the next timer tick. In practice, specifying zero
43  * seems to often return immediately, which produces almost no
44  * entropy. Specify a delay of 1000ns to try to force an existent
45  * delay.
46  */
47 #define EFI_ENTROPY_TRIGGER_TIME 10
48 
49 /** Event used to wait for timer tick */
50 static EFI_EVENT tick;
51 
52 /**
53  * Enable entropy gathering
54  *
55  * @ret rc Return status code
56  */
57 static int efi_entropy_enable ( void ) {
59  EFI_STATUS efirc;
60  int rc;
61 
62  /* Drop to external TPL to allow timer tick event to take place */
64 
65  /* Create timer tick event */
66  if ( ( efirc = bs->CreateEvent ( EVT_TIMER, TPL_NOTIFY, NULL, NULL,
67  &tick ) ) != 0 ) {
68  rc = -EEFI ( efirc );
69  DBGC ( &tick, "ENTROPY could not create event: %s\n",
70  strerror ( rc ) );
71  return rc;
72  }
73 
74  /* We use essentially the same mechanism as for the BIOS
75  * RTC-based entropy source, and so assume the same
76  * min-entropy per sample.
77  */
78  entropy_init ( &efitick_entropy, MIN_ENTROPY ( 1.3 ) );
79 
80  return 0;
81 }
82 
83 /**
84  * Disable entropy gathering
85  *
86  */
87 static void efi_entropy_disable ( void ) {
89 
90  /* Close timer tick event */
91  bs->CloseEvent ( tick );
92 
93  /* Return to internal TPL */
94  bs->RaiseTPL ( efi_internal_tpl );
95 }
96 
97 /**
98  * Wait for a timer tick
99  *
100  * @ret low CPU profiling low-order bits, or negative error
101  */
102 static int efi_entropy_tick ( void ) {
104  UINTN index;
105  uint16_t low;
106  EFI_STATUS efirc;
107  int rc;
108 
109  /* Wait for next timer tick */
110  if ( ( efirc = bs->SetTimer ( tick, TimerRelative,
111  EFI_ENTROPY_TRIGGER_TIME ) ) != 0 ) {
112  rc = -EEFI ( efirc );
113  DBGC ( &tick, "ENTROPY could not set timer: %s\n",
114  strerror ( rc ) );
115  return rc;
116  }
117  if ( ( efirc = bs->WaitForEvent ( 1, &tick, &index ) ) != 0 ) {
118  rc = -EEFI ( efirc );
119  DBGC ( &tick, "ENTROPY could not wait for timer tick: %s\n",
120  strerror ( rc ) );
121  return rc;
122  }
123 
124  /* Get current CPU profiling timestamp low-order bits */
125  low = profile_timestamp();
126 
127  return low;
128 }
129 
130 /**
131  * Get noise sample from timer ticks
132  *
133  * @ret noise Noise sample
134  * @ret rc Return status code
135  */
136 static int efi_get_noise ( noise_sample_t *noise ) {
137  int before;
138  int after;
139  int rc;
140 
141  /* Wait for a timer tick */
143  if ( before < 0 ) {
144  rc = before;
145  return rc;
146  }
147 
148  /* Wait for another timer tick */
150  if ( after < 0 ) {
151  rc = after;
152  return rc;
153  }
154 
155  /* Use TSC delta as noise sample */
156  *noise = ( after - before );
157 
158  return 0;
159 }
160 
161 /** EFI entropy source */
162 struct entropy_source efitick_entropy __entropy_source ( ENTROPY_FALLBACK ) = {
163  .name = "efitick",
164  .enable = efi_entropy_enable,
165  .disable = efi_entropy_disable,
166  .get_noise = efi_get_noise,
167 };
EFI_BOOT_SERVICES * BootServices
A pointer to the EFI Boot Services Table.
Definition: UefiSpec.h:2081
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
unsigned short uint16_t
Definition: stdint.h:11
static int efi_entropy_tick(void)
Wait for a timer tick.
Definition: efi_entropy.c:102
uint32_t low
Low 16 bits of address.
Definition: myson.h:19
#define EEFI(efirc)
Convert an EFI status code to an iPXE status code.
Definition: efi.h:171
EFI_RAISE_TPL RaiseTPL
Definition: UefiSpec.h:1926
An event is to be signaled once at a specified interval from the current time.
Definition: UefiSpec.h:541
Error codes.
static EFI_EVENT tick
Event used to wait for timer tick.
Definition: efi_entropy.c:50
VOID * EFI_EVENT
Handle to an event structure.
Definition: UefiBaseType.h:39
static int efi_get_noise(noise_sample_t *noise)
Get noise sample from timer ticks.
Definition: efi_entropy.c:136
#define DBGC(...)
Definition: compiler.h:505
int32_t before
Initial microcode version.
Definition: ucode.h:16
EFI_CLOSE_EVENT CloseEvent
Definition: UefiSpec.h:1945
EFI_SET_TIMER SetTimer
Definition: UefiSpec.h:1942
#define EFI_ENTROPY_TRIGGER_TIME
Time (in 100ns units) to delay waiting for timer tick.
Definition: efi_entropy.c:47
struct entropy_source efitick_entropy __entropy_source(ENTROPY_FALLBACK)
EFI entropy source.
static void efi_entropy_disable(void)
Disable entropy gathering.
Definition: efi_entropy.c:87
EFI_TPL efi_internal_tpl
Internal task priority level.
Definition: efi_init.c:52
An entropy source.
Definition: entropy.h:116
#define TPL_NOTIFY
Definition: UefiSpec.h:639
const char * name
Name.
Definition: entropy.h:118
static int efi_entropy_enable(void)
Enable entropy gathering.
Definition: efi_entropy.c:57
EFI_CREATE_EVENT CreateEvent
Definition: UefiSpec.h:1941
Profiling.
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
EFI Boot Services Table.
Definition: UefiSpec.h:1917
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
UINT64 UINTN
Unsigned value of native width.
EFI_WAIT_FOR_EVENT WaitForEvent
Definition: UefiSpec.h:1943
#define MIN_ENTROPY(bits)
Construct a min-entropy fixed-point value.
Definition: entropy.h:42
EFI API.
#define ENTROPY_FALLBACK
Fallback entropy source.
Definition: entropy.h:181
int32_t after
Final microcode version.
Definition: ucode.h:18
RETURN_STATUS EFI_STATUS
Function return status for EFI API.
Definition: UefiBaseType.h:31
uint8_t noise_sample_t
A noise sample.
Definition: entropy.h:21
EFI_SYSTEM_TABLE * efi_systab
uint64_t index
Index of the first segment within the content.
Definition: pccrc.h:21
EFI_RESTORE_TPL RestoreTPL
Definition: UefiSpec.h:1927
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.
#define EVT_TIMER
Definition: UefiSpec.h:440
EFI_TPL efi_external_tpl
External task priority level.
Definition: efi_init.c:55