]>
Commit | Line | Data |
---|---|---|
4e5ecdba RC |
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 | |
8a897478 | 17 | Copyright (c) 2021 - 2022, Arm Limited. All rights reserved.<BR>\r |
4e5ecdba RC |
18 | Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>\r |
19 | (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>\r | |
20 | SPDX-License-Identifier: BSD-2-Clause-Patent\r | |
21 | \r | |
22 | **/\r | |
23 | \r | |
8a897478 SM |
24 | #include <Library/BaseLib.h>\r |
25 | #include <Library/BaseMemoryLib.h>\r | |
26 | \r | |
4e5ecdba RC |
27 | #include "RngDxeInternals.h"\r |
28 | \r | |
4b3e9d80 PG |
29 | /** Allocate and initialize mAvailableAlgoArray with the available\r |
30 | Rng algorithms. Also update mAvailableAlgoArrayCount.\r | |
31 | \r | |
32 | @retval EFI_SUCCESS The function completed successfully.\r | |
33 | @retval EFI_OUT_OF_RESOURCES Could not allocate memory.\r | |
34 | **/\r | |
35 | EFI_STATUS\r | |
36 | EFIAPI\r | |
37 | GetAvailableAlgorithms (\r | |
38 | VOID\r | |
39 | )\r | |
40 | {\r | |
41 | return EFI_SUCCESS;\r | |
42 | }\r | |
43 | \r | |
44 | /** Free mAvailableAlgoArray.\r | |
45 | **/\r | |
46 | VOID\r | |
47 | EFIAPI\r | |
48 | FreeAvailableAlgorithms (\r | |
49 | VOID\r | |
50 | )\r | |
51 | {\r | |
52 | return;\r | |
53 | }\r | |
54 | \r | |
4e5ecdba RC |
55 | /**\r |
56 | Produces and returns an RNG value using either the default or specified RNG algorithm.\r | |
57 | \r | |
58 | @param[in] This A pointer to the EFI_RNG_PROTOCOL instance.\r | |
59 | @param[in] RNGAlgorithm A pointer to the EFI_RNG_ALGORITHM that identifies the RNG\r | |
60 | algorithm to use. May be NULL in which case the function will\r | |
61 | use its default RNG algorithm.\r | |
62 | @param[in] RNGValueLength The length in bytes of the memory buffer pointed to by\r | |
63 | RNGValue. The driver shall return exactly this numbers of bytes.\r | |
64 | @param[out] RNGValue A caller-allocated memory buffer filled by the driver with the\r | |
65 | resulting RNG value.\r | |
66 | \r | |
67 | @retval EFI_SUCCESS The RNG value was returned successfully.\r | |
68 | @retval EFI_UNSUPPORTED The algorithm specified by RNGAlgorithm is not supported by\r | |
69 | this driver.\r | |
70 | @retval EFI_DEVICE_ERROR An RNG value could not be retrieved due to a hardware or\r | |
71 | firmware error.\r | |
72 | @retval EFI_NOT_READY There is not enough random data available to satisfy the length\r | |
73 | requested by RNGValueLength.\r | |
74 | @retval EFI_INVALID_PARAMETER RNGValue is NULL or RNGValueLength is zero.\r | |
75 | \r | |
76 | **/\r | |
77 | EFI_STATUS\r | |
78 | EFIAPI\r | |
79 | RngGetRNG (\r | |
c411b485 MK |
80 | IN EFI_RNG_PROTOCOL *This,\r |
81 | IN EFI_RNG_ALGORITHM *RNGAlgorithm OPTIONAL,\r | |
82 | IN UINTN RNGValueLength,\r | |
83 | OUT UINT8 *RNGValue\r | |
4e5ecdba RC |
84 | )\r |
85 | {\r | |
c411b485 | 86 | EFI_STATUS Status;\r |
4e5ecdba | 87 | \r |
199031b2 | 88 | if ((This == NULL) || (RNGValueLength == 0) || (RNGValue == NULL)) {\r |
4e5ecdba RC |
89 | return EFI_INVALID_PARAMETER;\r |
90 | }\r | |
91 | \r | |
92 | Status = EFI_UNSUPPORTED;\r | |
93 | if (RNGAlgorithm == NULL) {\r | |
94 | //\r | |
95 | // Use the default RNG algorithm if RNGAlgorithm is NULL.\r | |
96 | //\r | |
97 | RNGAlgorithm = &gEfiRngAlgorithmSp80090Ctr256Guid;\r | |
98 | }\r | |
99 | \r | |
100 | //\r | |
101 | // NIST SP800-90-AES-CTR-256 supported by RDRAND\r | |
102 | //\r | |
103 | if (CompareGuid (RNGAlgorithm, &gEfiRngAlgorithmSp80090Ctr256Guid)) {\r | |
104 | Status = RngGetBytes (RNGValueLength, RNGValue);\r | |
105 | return Status;\r | |
106 | }\r | |
107 | \r | |
108 | //\r | |
109 | // The "raw" algorithm is intended to provide entropy directly\r | |
110 | //\r | |
111 | if (CompareGuid (RNGAlgorithm, &gEfiRngAlgorithmRaw)) {\r | |
112 | //\r | |
113 | // When a DRBG is used on the output of a entropy source,\r | |
114 | // its security level must be at least 256 bits according to UEFI Spec.\r | |
115 | //\r | |
116 | if (RNGValueLength < 32) {\r | |
117 | return EFI_INVALID_PARAMETER;\r | |
118 | }\r | |
119 | \r | |
8a897478 | 120 | Status = GenerateEntropy (RNGValueLength, RNGValue);\r |
4e5ecdba RC |
121 | return Status;\r |
122 | }\r | |
123 | \r | |
124 | //\r | |
125 | // Other algorithms were unsupported by this driver.\r | |
126 | //\r | |
127 | return Status;\r | |
128 | }\r | |
129 | \r | |
130 | /**\r | |
131 | Returns information about the random number generation implementation.\r | |
132 | \r | |
922bf317 | 133 | @param[in] This A pointer to the EFI_RNG_PROTOCOL instance.\r |
4e5ecdba RC |
134 | @param[in,out] RNGAlgorithmListSize On input, the size in bytes of RNGAlgorithmList.\r |
135 | On output with a return code of EFI_SUCCESS, the size\r | |
136 | in bytes of the data returned in RNGAlgorithmList. On output\r | |
137 | with a return code of EFI_BUFFER_TOO_SMALL,\r | |
138 | the size of RNGAlgorithmList required to obtain the list.\r | |
139 | @param[out] RNGAlgorithmList A caller-allocated memory buffer filled by the driver\r | |
140 | with one EFI_RNG_ALGORITHM element for each supported\r | |
141 | RNG algorithm. The list must not change across multiple\r | |
142 | calls to the same driver. The first algorithm in the list\r | |
143 | is the default algorithm for the driver.\r | |
144 | \r | |
145 | @retval EFI_SUCCESS The RNG algorithm list was returned successfully.\r | |
922bf317 PG |
146 | @retval EFI_UNSUPPORTED No supported algorithms found.\r |
147 | @retval EFI_DEVICE_ERROR The list of algorithms could not be retrieved due to a\r | |
148 | hardware or firmware error.\r | |
149 | @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect.\r | |
4e5ecdba RC |
150 | @retval EFI_BUFFER_TOO_SMALL The buffer RNGAlgorithmList is too small to hold the result.\r |
151 | \r | |
152 | **/\r | |
922bf317 | 153 | EFI_STATUS\r |
4e5ecdba | 154 | EFIAPI\r |
922bf317 PG |
155 | RngGetInfo (\r |
156 | IN EFI_RNG_PROTOCOL *This,\r | |
157 | IN OUT UINTN *RNGAlgorithmListSize,\r | |
158 | OUT EFI_RNG_ALGORITHM *RNGAlgorithmList\r | |
4e5ecdba RC |
159 | )\r |
160 | {\r | |
aade3b93 | 161 | UINTN RequiredSize;\r |
4e5ecdba | 162 | \r |
922bf317 PG |
163 | if ((This == NULL) || (RNGAlgorithmListSize == NULL)) {\r |
164 | return EFI_INVALID_PARAMETER;\r | |
165 | }\r | |
166 | \r | |
4e5ecdba RC |
167 | RequiredSize = 2 * sizeof (EFI_RNG_ALGORITHM);\r |
168 | \r | |
169 | if (*RNGAlgorithmListSize < RequiredSize) {\r | |
170 | *RNGAlgorithmListSize = RequiredSize;\r | |
171 | return EFI_BUFFER_TOO_SMALL;\r | |
172 | }\r | |
173 | \r | |
922bf317 PG |
174 | if (RNGAlgorithmList == NULL) {\r |
175 | return EFI_INVALID_PARAMETER;\r | |
176 | }\r | |
177 | \r | |
aade3b93 | 178 | CopyMem (&RNGAlgorithmList[0], &gEfiRngAlgorithmSp80090Ctr256Guid, sizeof (EFI_RNG_ALGORITHM));\r |
4e5ecdba RC |
179 | \r |
180 | // x86 platforms also support EFI_RNG_ALGORITHM_RAW via RDSEED\r | |
c411b485 | 181 | CopyMem (&RNGAlgorithmList[1], &gEfiRngAlgorithmRaw, sizeof (EFI_RNG_ALGORITHM));\r |
4e5ecdba RC |
182 | \r |
183 | *RNGAlgorithmListSize = RequiredSize;\r | |
184 | return EFI_SUCCESS;\r | |
185 | }\r |