]>
Commit | Line | Data |
---|---|---|
3aa8dc6c LQ |
1 | /** @file \r |
2 | RNG Driver to produce the UEFI Random Number Generator protocol.\r | |
3 | \r | |
4 | The driver will use the new RDRAND instruction to produce high-quality, high-performance \r | |
5 | entropy and random number.\r | |
6 | \r | |
7 | RNG Algoritnms defined in UEFI 2.4:\r | |
8 | - EFI_RNG_ALGORITHM_SP800_90_CTR_256_GUID - Supported\r | |
9 | (RDRAND implements a hardware NIST SP800-90 AES-CTR-256 based DRBG)\r | |
10 | - EFI_RNG_ALGORITHM_RAW - Supported\r | |
11 | (Structuring RDRAND invocation can be guaranteed as high-quality entropy source)\r | |
12 | - EFI_RNG_ALGORITHM_SP800_90_HMAC_256_GUID - Unsupported\r | |
13 | - EFI_RNG_ALGORITHM_SP800_90_HASH_256_GUID - Unsupported\r | |
14 | - EFI_RNG_ALGORITHM_X9_31_3DES_GUID - Unsupported\r | |
15 | - EFI_RNG_ALGORITHM_X9_31_AES_GUID - Unsupported\r | |
16 | \r | |
17 | Copyright (c) 2013, Intel Corporation. All rights reserved.<BR>\r | |
18 | This program and the accompanying materials \r | |
19 | are licensed and made available under the terms and conditions of the BSD License \r | |
20 | which accompanies this distribution. The full text of the license may be found at \r | |
21 | http://opensource.org/licenses/bsd-license.php\r | |
22 | \r | |
23 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r | |
24 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
25 | \r | |
26 | **/\r | |
27 | \r | |
28 | #include "RdRand.h"\r | |
29 | \r | |
30 | //\r | |
31 | // Supported RNG Algorithms list by this driver.\r | |
32 | //\r | |
33 | EFI_RNG_ALGORITHM mSupportedRngAlgorithms[] = {\r | |
34 | EFI_RNG_ALGORITHM_SP800_90_CTR_256_GUID,\r | |
35 | EFI_RNG_ALGORITHM_RAW\r | |
36 | };\r | |
37 | \r | |
38 | /**\r | |
39 | Returns information about the random number generation implementation.\r | |
40 | \r | |
41 | @param[in] This A pointer to the EFI_RNG_PROTOCOL instance.\r | |
42 | @param[in,out] RNGAlgorithmListSize On input, the size in bytes of RNGAlgorithmList. \r | |
43 | On output with a return code of EFI_SUCCESS, the size\r | |
44 | in bytes of the data returned in RNGAlgorithmList. On output\r | |
45 | with a return code of EFI_BUFFER_TOO_SMALL,\r | |
46 | the size of RNGAlgorithmList required to obtain the list.\r | |
47 | @param[out] RNGAlgorithmList A caller-allocated memory buffer filled by the driver\r | |
48 | with one EFI_RNG_ALGORITHM element for each supported\r | |
49 | RNG algorithm. The list must not change across multiple\r | |
50 | calls to the same driver. The first algorithm in the list\r | |
51 | is the default algorithm for the driver.\r | |
52 | \r | |
53 | @retval EFI_SUCCESS The RNG algorithm list was returned successfully.\r | |
54 | @retval EFI_UNSUPPORTED The services is not supported by this driver.\r | |
55 | @retval EFI_DEVICE_ERROR The list of algorithms could not be retrieved due to a\r | |
56 | hardware or firmware error.\r | |
57 | @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect.\r | |
58 | @retval EFI_BUFFER_TOO_SMALL The buffer RNGAlgorithmList is too small to hold the result.\r | |
59 | \r | |
60 | **/\r | |
61 | EFI_STATUS\r | |
62 | EFIAPI\r | |
63 | RngGetInfo (\r | |
64 | IN EFI_RNG_PROTOCOL *This,\r | |
65 | IN OUT UINTN *RNGAlgorithmListSize,\r | |
66 | OUT EFI_RNG_ALGORITHM *RNGAlgorithmList\r | |
67 | )\r | |
68 | {\r | |
69 | EFI_STATUS Status;\r | |
70 | UINTN RequiredSize;\r | |
71 | \r | |
72 | if ((This == NULL) || (RNGAlgorithmListSize == NULL)) {\r | |
73 | return EFI_INVALID_PARAMETER;\r | |
74 | }\r | |
75 | \r | |
76 | RequiredSize = sizeof (mSupportedRngAlgorithms);\r | |
77 | if (*RNGAlgorithmListSize < RequiredSize) {\r | |
78 | Status = EFI_BUFFER_TOO_SMALL;\r | |
79 | } else {\r | |
80 | //\r | |
81 | // Return algorithm list supported by driver.\r | |
82 | //\r | |
83 | if (RNGAlgorithmList != NULL) {\r | |
84 | CopyMem (RNGAlgorithmList, mSupportedRngAlgorithms, RequiredSize);\r | |
85 | Status = EFI_SUCCESS;\r | |
86 | } else {\r | |
87 | Status = EFI_INVALID_PARAMETER;\r | |
88 | }\r | |
89 | }\r | |
90 | *RNGAlgorithmListSize = RequiredSize;\r | |
91 | \r | |
92 | return Status;\r | |
93 | }\r | |
94 | \r | |
95 | /**\r | |
96 | Produces and returns an RNG value using either the default or specified RNG algorithm.\r | |
97 | \r | |
98 | @param[in] This A pointer to the EFI_RNG_PROTOCOL instance.\r | |
99 | @param[in] RNGAlgorithm A pointer to the EFI_RNG_ALGORITHM that identifies the RNG\r | |
100 | algorithm to use. May be NULL in which case the function will\r | |
101 | use its default RNG algorithm.\r | |
102 | @param[in] RNGValueLength The length in bytes of the memory buffer pointed to by\r | |
103 | RNGValue. The driver shall return exactly this numbers of bytes.\r | |
104 | @param[out] RNGValue A caller-allocated memory buffer filled by the driver with the\r | |
105 | resulting RNG value.\r | |
106 | \r | |
107 | @retval EFI_SUCCESS The RNG value was returned successfully.\r | |
108 | @retval EFI_UNSUPPORTED The algorithm specified by RNGAlgorithm is not supported by\r | |
109 | this driver.\r | |
110 | @retval EFI_DEVICE_ERROR An RNG value could not be retrieved due to a hardware or\r | |
111 | firmware error.\r | |
112 | @retval EFI_NOT_READY There is not enough random data available to satisfy the length\r | |
113 | requested by RNGValueLength.\r | |
114 | @retval EFI_INVALID_PARAMETER RNGValue is NULL or RNGValueLength is zero.\r | |
115 | \r | |
116 | **/\r | |
117 | EFI_STATUS\r | |
118 | EFIAPI\r | |
119 | RngGetRNG (\r | |
120 | IN EFI_RNG_PROTOCOL *This,\r | |
121 | IN EFI_RNG_ALGORITHM *RNGAlgorithm, OPTIONAL\r | |
122 | IN UINTN RNGValueLength,\r | |
123 | OUT UINT8 *RNGValue\r | |
124 | )\r | |
125 | {\r | |
126 | EFI_STATUS Status;\r | |
127 | \r | |
128 | if ((RNGValueLength == 0) || (RNGValue == NULL)) {\r | |
129 | return EFI_INVALID_PARAMETER;\r | |
130 | }\r | |
131 | \r | |
132 | Status = EFI_UNSUPPORTED;\r | |
133 | if (RNGAlgorithm == NULL) {\r | |
134 | //\r | |
135 | // Use the default RNG algorithm if RNGAlgorithm is NULL.\r | |
136 | //\r | |
137 | RNGAlgorithm = &gEfiRngAlgorithmSp80090Ctr256Guid;\r | |
138 | }\r | |
139 | \r | |
140 | //\r | |
141 | // NIST SP800-90-AES-CTR-256 supported by RDRAND\r | |
142 | //\r | |
143 | if (CompareGuid (RNGAlgorithm, &gEfiRngAlgorithmSp80090Ctr256Guid)) {\r | |
144 | Status = RdRandGetBytes (RNGValueLength, RNGValue);\r | |
145 | return Status;\r | |
146 | }\r | |
147 | \r | |
148 | //\r | |
149 | // The "raw" algorithm is intended to provide entropy directly\r | |
150 | //\r | |
151 | if (CompareGuid (RNGAlgorithm, &gEfiRngAlgorithmRaw)) {\r | |
152 | //\r | |
153 | // When a DRBG is used on the output of a entropy source, \r | |
154 | // its security level must be at least 256 bits according to UEFI Spec.\r | |
155 | //\r | |
156 | if (RNGValueLength < 32) {\r | |
157 | return EFI_INVALID_PARAMETER;\r | |
158 | }\r | |
159 | \r | |
160 | Status = RdRandGenerateEntropy (RNGValueLength, RNGValue);\r | |
161 | return Status;\r | |
162 | }\r | |
163 | \r | |
164 | //\r | |
165 | // Other algorithms were unsupported by this driver.\r | |
166 | //\r | |
167 | return Status;\r | |
168 | }\r | |
169 | \r | |
170 | //\r | |
171 | // The Random Number Generator (RNG) protocol\r | |
172 | //\r | |
173 | EFI_RNG_PROTOCOL mRngRdRand = {\r | |
174 | RngGetInfo,\r | |
175 | RngGetRNG\r | |
176 | };\r | |
177 | \r | |
178 | /**\r | |
179 | The user Entry Point for the Random Number Generator (RNG) driver.\r | |
180 | \r | |
181 | @param[in] ImageHandle The firmware allocated handle for the EFI image.\r | |
182 | @param[in] SystemTable A pointer to the EFI System Table.\r | |
183 | \r | |
184 | @retval EFI_SUCCESS The entry point is executed successfully.\r | |
185 | @retval EFI_NOT_SUPPORTED Platform does not support RNG.\r | |
186 | @retval Other Some error occurs when executing this entry point.\r | |
187 | \r | |
188 | **/\r | |
189 | EFI_STATUS\r | |
190 | EFIAPI\r | |
191 | RngDriverEntry (\r | |
192 | IN EFI_HANDLE ImageHandle,\r | |
193 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
194 | )\r | |
195 | {\r | |
196 | EFI_STATUS Status;\r | |
197 | EFI_HANDLE Handle;\r | |
198 | \r | |
199 | //\r | |
200 | // Verify RdRand support on Platform.\r | |
201 | //\r | |
202 | Status = IsRdRandSupported ();\r | |
203 | if (EFI_ERROR (Status)) {\r | |
204 | return Status;\r | |
205 | }\r | |
206 | \r | |
207 | //\r | |
208 | // Install UEFI RNG (Random Number Generator) Protocol\r | |
209 | //\r | |
210 | Handle = NULL;\r | |
211 | Status = gBS->InstallMultipleProtocolInterfaces (\r | |
212 | &Handle,\r | |
213 | &gEfiRngProtocolGuid,\r | |
214 | &mRngRdRand,\r | |
215 | NULL\r | |
216 | );\r | |
217 | \r | |
218 | return Status;\r | |
219 | }\r |