Przeglądaj źródła

[efi] Use the EFI_RNG_PROTOCOL as an entropy source if available

Entropy gathering via timer ticks is slow under UEFI (of the order of
20-30 seconds on some machines).  Use the EFI_RNG_PROTOCOL if
available, to speed up the process of entropy gathering.

Note that some implementations (including EDK2) will fail if we
request fewer than 32 random bytes at a time, and that the RNG
protocol provides no guarantees about the amount of entropy provided
by a call to GetRNG().  We take the (hopefully pessimistic) view that
a 32-byte block returned by GetRNG() will contain at least the 1.3
bits of entropy claimed by min_entropy_per_sample().

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 9 lat temu
rodzic
commit
7ca801d637

+ 76
- 3
src/arch/x86/interface/efi/efi_entropy.c Wyświetl plik

@@ -25,7 +25,9 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 25
 
26 26
 #include <errno.h>
27 27
 #include <ipxe/entropy.h>
28
+#include <ipxe/crc32.h>
28 29
 #include <ipxe/efi/efi.h>
30
+#include <ipxe/efi/Protocol/Rng.h>
29 31
 
30 32
 /** @file
31 33
  *
@@ -33,6 +35,23 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
33 35
  *
34 36
  */
35 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
+
36 55
 /** Time (in 100ns units) to delay waiting for timer tick
37 56
  *
38 57
  * In theory, UEFI allows us to specify a trigger time of zero to
@@ -56,6 +75,9 @@ static int efi_entropy_enable ( void ) {
56 75
 	EFI_STATUS efirc;
57 76
 	int rc;
58 77
 
78
+	DBGC ( &tick, "ENTROPY %s RNG protocol\n",
79
+	       ( efirng ? "has" : "has no" ) );
80
+
59 81
 	/* Create timer tick event */
60 82
 	if ( ( efirc = bs->CreateEvent ( EVT_TIMER, TPL_NOTIFY, NULL, NULL,
61 83
 					 &tick ) ) != 0 ) {
@@ -80,7 +102,7 @@ static void efi_entropy_disable ( void ) {
80 102
 }
81 103
 
82 104
 /**
83
- * Wait for an RTC tick
105
+ * Wait for a timer tick
84 106
  *
85 107
  * @ret low		TSC low-order bits, or negative error
86 108
  */
@@ -114,12 +136,12 @@ static int efi_entropy_tick ( void ) {
114 136
 }
115 137
 
116 138
 /**
117
- * Get noise sample
139
+ * Get noise sample from timer ticks
118 140
  *
119 141
  * @ret noise		Noise sample
120 142
  * @ret rc		Return status code
121 143
  */
122
-static int efi_get_noise ( noise_sample_t *noise ) {
144
+static int efi_get_noise_ticks ( noise_sample_t *noise ) {
123 145
 	int before;
124 146
 	int after;
125 147
 	int rc;
@@ -144,6 +166,57 @@ static int efi_get_noise ( noise_sample_t *noise ) {
144 166
 	return 0;
145 167
 }
146 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
+
147 220
 PROVIDE_ENTROPY_INLINE ( efi, min_entropy_per_sample );
148 221
 PROVIDE_ENTROPY ( efi, entropy_enable, efi_entropy_enable );
149 222
 PROVIDE_ENTROPY ( efi, entropy_disable, efi_entropy_disable );

+ 158
- 0
src/include/ipxe/efi/Protocol/Rng.h Wyświetl plik

@@ -0,0 +1,158 @@
1
+/** @file
2
+  EFI_RNG_PROTOCOL as defined in UEFI 2.4.
3
+  The UEFI Random Number Generator Protocol is used to provide random bits for use
4
+  in applications, or entropy for seeding other random number generators.
5
+
6
+Copyright (c) 2013, Intel Corporation. All rights reserved.<BR>
7
+This program and the accompanying materials are licensed and made available under
8
+the terms and conditions of the BSD License that accompanies this distribution.
9
+The full text of the license may be found at
10
+http://opensource.org/licenses/bsd-license.php.
11
+
12
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14
+
15
+**/
16
+
17
+#ifndef __EFI_RNG_PROTOCOL_H__
18
+#define __EFI_RNG_PROTOCOL_H__
19
+
20
+FILE_LICENCE ( BSD3 );
21
+
22
+///
23
+/// Global ID for the Random Number Generator Protocol
24
+///
25
+#define EFI_RNG_PROTOCOL_GUID \
26
+  { \
27
+    0x3152bca5, 0xeade, 0x433d, {0x86, 0x2e, 0xc0, 0x1c, 0xdc, 0x29, 0x1f, 0x44 } \
28
+  }
29
+
30
+typedef struct _EFI_RNG_PROTOCOL EFI_RNG_PROTOCOL;
31
+
32
+///
33
+/// A selection of EFI_RNG_PROTOCOL algorithms.
34
+/// The algorithms listed are optional, not meant to be exhaustive and be argmented by
35
+/// vendors or other industry standards.
36
+///
37
+
38
+typedef EFI_GUID EFI_RNG_ALGORITHM;
39
+
40
+///
41
+/// The algorithms corresponds to SP800-90 as defined in
42
+/// NIST SP 800-90, "Recommendation for Random Number Generation Using Deterministic Random
43
+/// Bit Generators", March 2007.
44
+///
45
+#define EFI_RNG_ALGORITHM_SP800_90_HASH_256_GUID \
46
+  { \
47
+    0xa7af67cb, 0x603b, 0x4d42, {0xba, 0x21, 0x70, 0xbf, 0xb6, 0x29, 0x3f, 0x96 } \
48
+  }
49
+#define EFI_RNG_ALGORITHM_SP800_90_HMAC_256_GUID \
50
+  { \
51
+    0xc5149b43, 0xae85, 0x4f53, {0x99, 0x82, 0xb9, 0x43, 0x35, 0xd3, 0xa9, 0xe7 } \
52
+  }
53
+#define EFI_RNG_ALGORITHM_SP800_90_CTR_256_GUID \
54
+  { \
55
+    0x44f0de6e, 0x4d8c, 0x4045, {0xa8, 0xc7, 0x4d, 0xd1, 0x68, 0x85, 0x6b, 0x9e } \
56
+  }
57
+///
58
+/// The algorithms correspond to X9.31 as defined in
59
+/// NIST, "Recommended Random Number Generator Based on ANSI X9.31 Appendix A.2.4 Using
60
+/// the 3-Key Triple DES and AES Algorithm", January 2005.
61
+///
62
+#define EFI_RNG_ALGORITHM_X9_31_3DES_GUID \
63
+  { \
64
+    0x63c4785a, 0xca34, 0x4012, {0xa3, 0xc8, 0x0b, 0x6a, 0x32, 0x4f, 0x55, 0x46 } \
65
+  }
66
+#define EFI_RNG_ALGORITHM_X9_31_AES_GUID \
67
+  { \
68
+    0xacd03321, 0x777e, 0x4d3d, {0xb1, 0xc8, 0x20, 0xcf, 0xd8, 0x88, 0x20, 0xc9 } \
69
+  }
70
+///
71
+/// The "raw" algorithm, when supported, is intended to provide entropy directly from
72
+/// the source, without it going through some deterministic random bit generator.
73
+///
74
+#define EFI_RNG_ALGORITHM_RAW \
75
+  { \
76
+    0xe43176d7, 0xb6e8, 0x4827, {0xb7, 0x84, 0x7f, 0xfd, 0xc4, 0xb6, 0x85, 0x61 } \
77
+  }
78
+
79
+/**
80
+  Returns information about the random number generation implementation.
81
+
82
+  @param[in]     This                 A pointer to the EFI_RNG_PROTOCOL instance.
83
+  @param[in,out] RNGAlgorithmListSize On input, the size in bytes of RNGAlgorithmList.
84
+                                      On output with a return code of EFI_SUCCESS, the size
85
+                                      in bytes of the data returned in RNGAlgorithmList. On output
86
+                                      with a return code of EFI_BUFFER_TOO_SMALL,
87
+                                      the size of RNGAlgorithmList required to obtain the list.
88
+  @param[out] RNGAlgorithmList        A caller-allocated memory buffer filled by the driver
89
+                                      with one EFI_RNG_ALGORITHM element for each supported
90
+                                      RNG algorithm. The list must not change across multiple
91
+                                      calls to the same driver. The first algorithm in the list
92
+                                      is the default algorithm for the driver.
93
+
94
+  @retval EFI_SUCCESS                 The RNG algorithm list was returned successfully.
95
+  @retval EFI_UNSUPPORTED             The services is not supported by this driver.
96
+  @retval EFI_DEVICE_ERROR            The list of algorithms could not be retrieved due to a
97
+                                      hardware or firmware error.
98
+  @retval EFI_INVALID_PARAMETER       One or more of the parameters are incorrect.
99
+  @retval EFI_BUFFER_TOO_SMALL        The buffer RNGAlgorithmList is too small to hold the result.
100
+
101
+**/
102
+typedef
103
+EFI_STATUS
104
+(EFIAPI *EFI_RNG_GET_INFO) (
105
+  IN EFI_RNG_PROTOCOL             *This,
106
+  IN OUT UINTN                    *RNGAlgorithmListSize,
107
+  OUT EFI_RNG_ALGORITHM           *RNGAlgorithmList
108
+  );
109
+
110
+/**
111
+  Produces and returns an RNG value using either the default or specified RNG algorithm.
112
+
113
+  @param[in]  This                    A pointer to the EFI_RNG_PROTOCOL instance.
114
+  @param[in]  RNGAlgorithm            A pointer to the EFI_RNG_ALGORITHM that identifies the RNG
115
+                                      algorithm to use. May be NULL in which case the function will
116
+                                      use its default RNG algorithm.
117
+  @param[in]  RNGValueLength          The length in bytes of the memory buffer pointed to by
118
+                                      RNGValue. The driver shall return exactly this numbers of bytes.
119
+  @param[out] RNGValue                A caller-allocated memory buffer filled by the driver with the
120
+                                      resulting RNG value.
121
+
122
+  @retval EFI_SUCCESS                 The RNG value was returned successfully.
123
+  @retval EFI_UNSUPPORTED             The algorithm specified by RNGAlgorithm is not supported by
124
+                                      this driver.
125
+  @retval EFI_DEVICE_ERROR            An RNG value could not be retrieved due to a hardware or
126
+                                      firmware error.
127
+  @retval EFI_NOT_READY               There is not enough random data available to satisfy the length
128
+                                      requested by RNGValueLength.
129
+  @retval EFI_INVALID_PARAMETER       RNGValue is NULL or RNGValueLength is zero.
130
+
131
+**/
132
+typedef
133
+EFI_STATUS
134
+(EFIAPI *EFI_RNG_GET_RNG) (
135
+  IN EFI_RNG_PROTOCOL            *This,
136
+  IN EFI_RNG_ALGORITHM           *RNGAlgorithm, OPTIONAL
137
+  IN UINTN                       RNGValueLength,
138
+  OUT UINT8                      *RNGValue
139
+  );
140
+
141
+///
142
+/// The Random Number Generator (RNG) protocol provides random bits for use in
143
+/// applications, or entropy for seeding other random number generators.
144
+///
145
+struct _EFI_RNG_PROTOCOL {
146
+  EFI_RNG_GET_INFO                GetInfo;
147
+  EFI_RNG_GET_RNG                 GetRNG;
148
+};
149
+
150
+extern EFI_GUID gEfiRngProtocolGuid;
151
+extern EFI_GUID gEfiRngAlgorithmSp80090Hash256Guid;
152
+extern EFI_GUID gEfiRngAlgorithmSp80090Hmac256Guid;
153
+extern EFI_GUID gEfiRngAlgorithmSp80090Ctr256Guid;
154
+extern EFI_GUID gEfiRngAlgorithmX9313DesGuid;
155
+extern EFI_GUID gEfiRngAlgorithmX931AesGuid;
156
+extern EFI_GUID gEfiRngAlgorithmRaw;
157
+
158
+#endif

Ładowanie…
Anuluj
Zapisz