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