2 RNG Driver to produce the UEFI Random Number Generator protocol.
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.
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
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>
21 SPDX-License-Identifier: BSD-2-Clause-Patent
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>
35 #include "RngDxeInternals.h"
37 // Maximum number of Rng algorithms.
38 #define RNG_AVAILABLE_ALGO_MAX 2
40 /** Allocate and initialize mAvailableAlgoArray with the available
41 Rng algorithms. Also update mAvailableAlgoArrayCount.
43 @retval EFI_SUCCESS The function completed successfully.
44 @retval EFI_OUT_OF_RESOURCES Could not allocate memory.
48 GetAvailableAlgorithms (
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
;
62 // Check RngGetBytes() before advertising PcdCpuRngSupportedAlgorithm.
63 if (!EFI_ERROR (RngGetBytes (sizeof (DummyRand
), (UINT8
*)&DummyRand
))) {
65 &mAvailableAlgoArray
[mAvailableAlgoArrayCount
],
66 PcdGetPtr (PcdCpuRngSupportedAlgorithm
),
67 sizeof (EFI_RNG_ALGORITHM
)
69 mAvailableAlgoArrayCount
++;
72 if (IsZeroGuid (PcdGetPtr (PcdCpuRngSupportedAlgorithm
))) {
75 "PcdCpuRngSupportedAlgorithm should be a non-zero GUID\n"
82 // Raw algorithm (Trng)
83 if (!EFI_ERROR (GetArmTrngVersion (&MajorRevision
, &MinorRevision
))) {
85 &mAvailableAlgoArray
[mAvailableAlgoArrayCount
],
87 sizeof (EFI_RNG_ALGORITHM
)
89 mAvailableAlgoArrayCount
++;
95 /** Free mAvailableAlgoArray.
99 FreeAvailableAlgorithms (
103 FreePool (mAvailableAlgoArray
);
108 Produces and returns an RNG value using either the default or specified RNG algorithm.
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
119 @retval EFI_SUCCESS The RNG value was returned successfully.
120 @retval EFI_UNSUPPORTED The algorithm specified by RNGAlgorithm is not supported by
122 @retval EFI_DEVICE_ERROR An RNG value could not be retrieved due to a hardware or
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.
132 IN EFI_RNG_PROTOCOL
*This
,
133 IN EFI_RNG_ALGORITHM
*RNGAlgorithm OPTIONAL
,
134 IN UINTN RNGValueLength
,
141 if ((This
== NULL
) || (RNGValueLength
== 0) || (RNGValue
== NULL
)) {
142 return EFI_INVALID_PARAMETER
;
145 if (RNGAlgorithm
== NULL
) {
147 // Use the default RNG algorithm if RNGAlgorithm is NULL.
149 for (Index
= 0; Index
< mAvailableAlgoArrayCount
; Index
++) {
150 if (!IsZeroGuid (&mAvailableAlgoArray
[Index
])) {
151 RNGAlgorithm
= &mAvailableAlgoArray
[Index
];
156 if (Index
== mAvailableAlgoArrayCount
) {
157 // No algorithm available.
158 ASSERT (Index
!= mAvailableAlgoArrayCount
);
159 return EFI_DEVICE_ERROR
;
164 if (CompareGuid (RNGAlgorithm
, PcdGetPtr (PcdCpuRngSupportedAlgorithm
))) {
165 Status
= RngGetBytes (RNGValueLength
, RNGValue
);
169 // Raw algorithm (Trng)
170 if (CompareGuid (RNGAlgorithm
, &gEfiRngAlgorithmRaw
)) {
171 return GenerateEntropy (RNGValueLength
, RNGValue
);
175 // Other algorithms are unsupported by this driver.
177 return EFI_UNSUPPORTED
;
181 Returns information about the random number generation implementation.
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.
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.
206 IN EFI_RNG_PROTOCOL
*This
,
207 IN OUT UINTN
*RNGAlgorithmListSize
,
208 OUT EFI_RNG_ALGORITHM
*RNGAlgorithmList
213 if ((This
== NULL
) || (RNGAlgorithmListSize
== NULL
)) {
214 return EFI_INVALID_PARAMETER
;
217 RequiredSize
= mAvailableAlgoArrayCount
* sizeof (EFI_RNG_ALGORITHM
);
219 if (RequiredSize
== 0) {
220 // No supported algorithms found.
221 return EFI_UNSUPPORTED
;
224 if (*RNGAlgorithmListSize
< RequiredSize
) {
225 *RNGAlgorithmListSize
= RequiredSize
;
226 return EFI_BUFFER_TOO_SMALL
;
229 if (RNGAlgorithmList
== NULL
) {
230 return EFI_INVALID_PARAMETER
;
233 // There is no gap in the array, so copy the block.
234 CopyMem (RNGAlgorithmList
, mAvailableAlgoArray
, RequiredSize
);
235 *RNGAlgorithmListSize
= RequiredSize
;