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