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 <Protocol/Rng.h>
33 #include "RngDxeInternals.h"
35 /** Free mAvailableAlgoArray.
39 FreeAvailableAlgorithms (
43 FreePool (mAvailableAlgoArray
);
48 Produces and returns an RNG value using either the default or specified RNG algorithm.
50 @param[in] This A pointer to the EFI_RNG_PROTOCOL instance.
51 @param[in] RNGAlgorithm A pointer to the EFI_RNG_ALGORITHM that identifies the RNG
52 algorithm to use. May be NULL in which case the function will
53 use its default RNG algorithm.
54 @param[in] RNGValueLength The length in bytes of the memory buffer pointed to by
55 RNGValue. The driver shall return exactly this numbers of bytes.
56 @param[out] RNGValue A caller-allocated memory buffer filled by the driver with the
59 @retval EFI_SUCCESS The RNG value was returned successfully.
60 @retval EFI_UNSUPPORTED The algorithm specified by RNGAlgorithm is not supported by
62 @retval EFI_DEVICE_ERROR An RNG value could not be retrieved due to a hardware or
64 @retval EFI_NOT_READY There is not enough random data available to satisfy the length
65 requested by RNGValueLength.
66 @retval EFI_INVALID_PARAMETER RNGValue is NULL or RNGValueLength is zero.
72 IN EFI_RNG_PROTOCOL
*This
,
73 IN EFI_RNG_ALGORITHM
*RNGAlgorithm OPTIONAL
,
74 IN UINTN RNGValueLength
,
81 if ((This
== NULL
) || (RNGValueLength
== 0) || (RNGValue
== NULL
)) {
82 return EFI_INVALID_PARAMETER
;
85 if (RNGAlgorithm
== NULL
) {
87 // Use the default RNG algorithm if RNGAlgorithm is NULL.
89 for (Index
= 0; Index
< mAvailableAlgoArrayCount
; Index
++) {
90 if (!IsZeroGuid (&mAvailableAlgoArray
[Index
])) {
91 RNGAlgorithm
= &mAvailableAlgoArray
[Index
];
96 if (Index
== mAvailableAlgoArrayCount
) {
97 // No algorithm available.
98 ASSERT (Index
!= mAvailableAlgoArrayCount
);
99 return EFI_DEVICE_ERROR
;
104 if (CompareGuid (RNGAlgorithm
, PcdGetPtr (PcdCpuRngSupportedAlgorithm
))) {
105 Status
= RngGetBytes (RNGValueLength
, RNGValue
);
109 // Raw algorithm (Trng)
110 if (CompareGuid (RNGAlgorithm
, &gEfiRngAlgorithmRaw
)) {
111 return GenerateEntropy (RNGValueLength
, RNGValue
);
115 // Other algorithms are unsupported by this driver.
117 return EFI_UNSUPPORTED
;
121 Returns information about the random number generation implementation.
123 @param[in] This A pointer to the EFI_RNG_PROTOCOL instance.
124 @param[in,out] RNGAlgorithmListSize On input, the size in bytes of RNGAlgorithmList.
125 On output with a return code of EFI_SUCCESS, the size
126 in bytes of the data returned in RNGAlgorithmList. On output
127 with a return code of EFI_BUFFER_TOO_SMALL,
128 the size of RNGAlgorithmList required to obtain the list.
129 @param[out] RNGAlgorithmList A caller-allocated memory buffer filled by the driver
130 with one EFI_RNG_ALGORITHM element for each supported
131 RNG algorithm. The list must not change across multiple
132 calls to the same driver. The first algorithm in the list
133 is the default algorithm for the driver.
135 @retval EFI_SUCCESS The RNG algorithm list was returned successfully.
136 @retval EFI_UNSUPPORTED The services is not supported by this driver.
137 @retval EFI_DEVICE_ERROR The list of algorithms could not be retrieved due to a
138 hardware or firmware error.
139 @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect.
140 @retval EFI_BUFFER_TOO_SMALL The buffer RNGAlgorithmList is too small to hold the result.
146 IN EFI_RNG_PROTOCOL
*This
,
147 IN OUT UINTN
*RNGAlgorithmListSize
,
148 OUT EFI_RNG_ALGORITHM
*RNGAlgorithmList
153 if ((This
== NULL
) || (RNGAlgorithmListSize
== NULL
)) {
154 return EFI_INVALID_PARAMETER
;
157 RequiredSize
= mAvailableAlgoArrayCount
* sizeof (EFI_RNG_ALGORITHM
);
159 if (RequiredSize
== 0) {
160 // No supported algorithms found.
161 return EFI_UNSUPPORTED
;
164 if (*RNGAlgorithmListSize
< RequiredSize
) {
165 *RNGAlgorithmListSize
= RequiredSize
;
166 return EFI_BUFFER_TOO_SMALL
;
169 if (RNGAlgorithmList
== NULL
) {
170 return EFI_INVALID_PARAMETER
;
173 // There is no gap in the array, so copy the block.
174 CopyMem (RNGAlgorithmList
, mAvailableAlgoArray
, RequiredSize
);
175 *RNGAlgorithmListSize
= RequiredSize
;