]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c
SecurityPkg/RngDxe: Add debug warning for NULL PcdCpuRngSupportedAlgorithm
[mirror_edk2.git] / SecurityPkg / RandomNumberGenerator / RngDxe / AArch64 / RngDxe.c
1 /** @file
2 RNG Driver to produce the UEFI Random Number Generator protocol.
3
4 The driver can use RNDR instruction (through the RngLib and if FEAT_RNG is
5 present) to produce random numbers. It also uses the Arm FW-TRNG interface
6 to implement EFI_RNG_ALGORITHM_RAW.
7
8 RNG Algorithms defined in UEFI 2.4:
9 - EFI_RNG_ALGORITHM_SP800_90_CTR_256_GUID
10 - EFI_RNG_ALGORITHM_RAW
11 - EFI_RNG_ALGORITHM_SP800_90_HMAC_256_GUID
12 - EFI_RNG_ALGORITHM_SP800_90_HASH_256_GUID
13 - EFI_RNG_ALGORITHM_X9_31_3DES_GUID - Unsupported
14 - EFI_RNG_ALGORITHM_X9_31_AES_GUID - Unsupported
15
16 Copyright (c) 2021, NUVIA Inc. All rights reserved.<BR>
17 Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
18 (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
19 Copyright (c) 2021 - 2022, Arm Limited. All rights reserved.<BR>
20
21 SPDX-License-Identifier: BSD-2-Clause-Patent
22
23 **/
24
25 #include <Library/BaseLib.h>
26 #include <Library/BaseMemoryLib.h>
27 #include <Library/DebugLib.h>
28 #include <Library/MemoryAllocationLib.h>
29 #include <Library/UefiBootServicesTableLib.h>
30 #include <Library/RngLib.h>
31 #include <Library/DebugLib.h>
32 #include <Library/ArmTrngLib.h>
33 #include <Protocol/Rng.h>
34
35 #include "RngDxeInternals.h"
36
37 // Maximum number of Rng algorithms.
38 #define RNG_AVAILABLE_ALGO_MAX 2
39
40 /** Allocate and initialize mAvailableAlgoArray with the available
41 Rng algorithms. Also update mAvailableAlgoArrayCount.
42
43 @retval EFI_SUCCESS The function completed successfully.
44 @retval EFI_OUT_OF_RESOURCES Could not allocate memory.
45 **/
46 EFI_STATUS
47 EFIAPI
48 GetAvailableAlgorithms (
49 VOID
50 )
51 {
52 UINT64 DummyRand;
53 UINT16 MajorRevision;
54 UINT16 MinorRevision;
55
56 // Rng algorithms 2 times, one for the allocation, one to populate.
57 mAvailableAlgoArray = AllocateZeroPool (RNG_AVAILABLE_ALGO_MAX);
58 if (mAvailableAlgoArray == NULL) {
59 return EFI_OUT_OF_RESOURCES;
60 }
61
62 // Check RngGetBytes() before advertising PcdCpuRngSupportedAlgorithm.
63 if (!EFI_ERROR (RngGetBytes (sizeof (DummyRand), (UINT8 *)&DummyRand))) {
64 CopyMem (
65 &mAvailableAlgoArray[mAvailableAlgoArrayCount],
66 PcdGetPtr (PcdCpuRngSupportedAlgorithm),
67 sizeof (EFI_RNG_ALGORITHM)
68 );
69 mAvailableAlgoArrayCount++;
70
71 DEBUG_CODE_BEGIN ();
72 if (IsZeroGuid (PcdGetPtr (PcdCpuRngSupportedAlgorithm))) {
73 DEBUG ((
74 DEBUG_WARN,
75 "PcdCpuRngSupportedAlgorithm should be a non-zero GUID\n"
76 ));
77 }
78
79 DEBUG_CODE_END ();
80 }
81
82 // Raw algorithm (Trng)
83 if (!EFI_ERROR (GetArmTrngVersion (&MajorRevision, &MinorRevision))) {
84 CopyMem (
85 &mAvailableAlgoArray[mAvailableAlgoArrayCount],
86 &gEfiRngAlgorithmRaw,
87 sizeof (EFI_RNG_ALGORITHM)
88 );
89 mAvailableAlgoArrayCount++;
90 }
91
92 return EFI_SUCCESS;
93 }
94
95 /** Free mAvailableAlgoArray.
96 **/
97 VOID
98 EFIAPI
99 FreeAvailableAlgorithms (
100 VOID
101 )
102 {
103 FreePool (mAvailableAlgoArray);
104 return;
105 }
106
107 /**
108 Produces and returns an RNG value using either the default or specified RNG algorithm.
109
110 @param[in] This A pointer to the EFI_RNG_PROTOCOL instance.
111 @param[in] RNGAlgorithm A pointer to the EFI_RNG_ALGORITHM that identifies the RNG
112 algorithm to use. May be NULL in which case the function will
113 use its default RNG algorithm.
114 @param[in] RNGValueLength The length in bytes of the memory buffer pointed to by
115 RNGValue. The driver shall return exactly this numbers of bytes.
116 @param[out] RNGValue A caller-allocated memory buffer filled by the driver with the
117 resulting RNG value.
118
119 @retval EFI_SUCCESS The RNG value was returned successfully.
120 @retval EFI_UNSUPPORTED The algorithm specified by RNGAlgorithm is not supported by
121 this driver.
122 @retval EFI_DEVICE_ERROR An RNG value could not be retrieved due to a hardware or
123 firmware error.
124 @retval EFI_NOT_READY There is not enough random data available to satisfy the length
125 requested by RNGValueLength.
126 @retval EFI_INVALID_PARAMETER RNGValue is NULL or RNGValueLength is zero.
127
128 **/
129 EFI_STATUS
130 EFIAPI
131 RngGetRNG (
132 IN EFI_RNG_PROTOCOL *This,
133 IN EFI_RNG_ALGORITHM *RNGAlgorithm OPTIONAL,
134 IN UINTN RNGValueLength,
135 OUT UINT8 *RNGValue
136 )
137 {
138 EFI_STATUS Status;
139 UINTN Index;
140
141 if ((This == NULL) || (RNGValueLength == 0) || (RNGValue == NULL)) {
142 return EFI_INVALID_PARAMETER;
143 }
144
145 if (RNGAlgorithm == NULL) {
146 //
147 // Use the default RNG algorithm if RNGAlgorithm is NULL.
148 //
149 for (Index = 0; Index < mAvailableAlgoArrayCount; Index++) {
150 if (!IsZeroGuid (&mAvailableAlgoArray[Index])) {
151 RNGAlgorithm = &mAvailableAlgoArray[Index];
152 goto FoundAlgo;
153 }
154 }
155
156 if (Index == mAvailableAlgoArrayCount) {
157 // No algorithm available.
158 ASSERT (Index != mAvailableAlgoArrayCount);
159 return EFI_DEVICE_ERROR;
160 }
161 }
162
163 FoundAlgo:
164 if (CompareGuid (RNGAlgorithm, PcdGetPtr (PcdCpuRngSupportedAlgorithm))) {
165 Status = RngGetBytes (RNGValueLength, RNGValue);
166 return Status;
167 }
168
169 // Raw algorithm (Trng)
170 if (CompareGuid (RNGAlgorithm, &gEfiRngAlgorithmRaw)) {
171 return GenerateEntropy (RNGValueLength, RNGValue);
172 }
173
174 //
175 // Other algorithms are unsupported by this driver.
176 //
177 return EFI_UNSUPPORTED;
178 }
179
180 /**
181 Returns information about the random number generation implementation.
182
183 @param[in] This A pointer to the EFI_RNG_PROTOCOL instance.
184 @param[in,out] RNGAlgorithmListSize On input, the size in bytes of RNGAlgorithmList.
185 On output with a return code of EFI_SUCCESS, the size
186 in bytes of the data returned in RNGAlgorithmList. On output
187 with a return code of EFI_BUFFER_TOO_SMALL,
188 the size of RNGAlgorithmList required to obtain the list.
189 @param[out] RNGAlgorithmList A caller-allocated memory buffer filled by the driver
190 with one EFI_RNG_ALGORITHM element for each supported
191 RNG algorithm. The list must not change across multiple
192 calls to the same driver. The first algorithm in the list
193 is the default algorithm for the driver.
194
195 @retval EFI_SUCCESS The RNG algorithm list was returned successfully.
196 @retval EFI_UNSUPPORTED The services is not supported by this driver.
197 @retval EFI_DEVICE_ERROR The list of algorithms could not be retrieved due to a
198 hardware or firmware error.
199 @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect.
200 @retval EFI_BUFFER_TOO_SMALL The buffer RNGAlgorithmList is too small to hold the result.
201
202 **/
203 EFI_STATUS
204 EFIAPI
205 RngGetInfo (
206 IN EFI_RNG_PROTOCOL *This,
207 IN OUT UINTN *RNGAlgorithmListSize,
208 OUT EFI_RNG_ALGORITHM *RNGAlgorithmList
209 )
210 {
211 UINTN RequiredSize;
212
213 if ((This == NULL) || (RNGAlgorithmListSize == NULL)) {
214 return EFI_INVALID_PARAMETER;
215 }
216
217 RequiredSize = mAvailableAlgoArrayCount * sizeof (EFI_RNG_ALGORITHM);
218
219 if (RequiredSize == 0) {
220 // No supported algorithms found.
221 return EFI_UNSUPPORTED;
222 }
223
224 if (*RNGAlgorithmListSize < RequiredSize) {
225 *RNGAlgorithmListSize = RequiredSize;
226 return EFI_BUFFER_TOO_SMALL;
227 }
228
229 if (RNGAlgorithmList == NULL) {
230 return EFI_INVALID_PARAMETER;
231 }
232
233 // There is no gap in the array, so copy the block.
234 CopyMem (RNGAlgorithmList, mAvailableAlgoArray, RequiredSize);
235 *RNGAlgorithmListSize = RequiredSize;
236 return EFI_SUCCESS;
237 }