2 RNG Driver to produce the UEFI Random Number Generator protocol.
4 The driver will use the RNDR instruction to produce random numbers.
6 RNG Algorithms defined in UEFI 2.4:
7 - EFI_RNG_ALGORITHM_SP800_90_CTR_256_GUID
8 - EFI_RNG_ALGORITHM_RAW - Unsupported
9 - EFI_RNG_ALGORITHM_SP800_90_HMAC_256_GUID
10 - EFI_RNG_ALGORITHM_SP800_90_HASH_256_GUID
11 - EFI_RNG_ALGORITHM_X9_31_3DES_GUID - Unsupported
12 - EFI_RNG_ALGORITHM_X9_31_AES_GUID - Unsupported
14 Copyright (c) 2021, NUVIA Inc. All rights reserved.<BR>
15 Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
16 (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
17 Copyright (c) 2021 - 2022, Arm Limited. All rights reserved.<BR>
19 SPDX-License-Identifier: BSD-2-Clause-Patent
23 #include <Library/BaseLib.h>
24 #include <Library/BaseMemoryLib.h>
25 #include <Library/UefiBootServicesTableLib.h>
26 #include <Protocol/Rng.h>
28 #include "RngDxeInternals.h"
31 Produces and returns an RNG value using either the default or specified RNG algorithm.
33 @param[in] This A pointer to the EFI_RNG_PROTOCOL instance.
34 @param[in] RNGAlgorithm A pointer to the EFI_RNG_ALGORITHM that identifies the RNG
35 algorithm to use. May be NULL in which case the function will
36 use its default RNG algorithm.
37 @param[in] RNGValueLength The length in bytes of the memory buffer pointed to by
38 RNGValue. The driver shall return exactly this numbers of bytes.
39 @param[out] RNGValue A caller-allocated memory buffer filled by the driver with the
42 @retval EFI_SUCCESS The RNG value was returned successfully.
43 @retval EFI_UNSUPPORTED The algorithm specified by RNGAlgorithm is not supported by
45 @retval EFI_DEVICE_ERROR An RNG value could not be retrieved due to a hardware or
47 @retval EFI_NOT_READY There is not enough random data available to satisfy the length
48 requested by RNGValueLength.
49 @retval EFI_INVALID_PARAMETER RNGValue is NULL or RNGValueLength is zero.
55 IN EFI_RNG_PROTOCOL
*This
,
56 IN EFI_RNG_ALGORITHM
*RNGAlgorithm OPTIONAL
,
57 IN UINTN RNGValueLength
,
63 if ((This
== NULL
) || (RNGValueLength
== 0) || (RNGValue
== NULL
)) {
64 return EFI_INVALID_PARAMETER
;
67 if (RNGAlgorithm
== NULL
) {
69 // Use the default RNG algorithm if RNGAlgorithm is NULL.
71 RNGAlgorithm
= PcdGetPtr (PcdCpuRngSupportedAlgorithm
);
74 if (CompareGuid (RNGAlgorithm
, PcdGetPtr (PcdCpuRngSupportedAlgorithm
))) {
75 Status
= RngGetBytes (RNGValueLength
, RNGValue
);
80 // Other algorithms are unsupported by this driver.
82 return EFI_UNSUPPORTED
;
86 Returns information about the random number generation implementation.
88 @param[in] This A pointer to the EFI_RNG_PROTOCOL instance.
89 @param[in,out] RNGAlgorithmListSize On input, the size in bytes of RNGAlgorithmList.
90 On output with a return code of EFI_SUCCESS, the size
91 in bytes of the data returned in RNGAlgorithmList. On output
92 with a return code of EFI_BUFFER_TOO_SMALL,
93 the size of RNGAlgorithmList required to obtain the list.
94 @param[out] RNGAlgorithmList A caller-allocated memory buffer filled by the driver
95 with one EFI_RNG_ALGORITHM element for each supported
96 RNG algorithm. The list must not change across multiple
97 calls to the same driver. The first algorithm in the list
98 is the default algorithm for the driver.
100 @retval EFI_SUCCESS The RNG algorithm list was returned successfully.
101 @retval EFI_UNSUPPORTED The services is not supported by this driver.
102 @retval EFI_DEVICE_ERROR The list of algorithms could not be retrieved due to a
103 hardware or firmware error.
104 @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect.
105 @retval EFI_BUFFER_TOO_SMALL The buffer RNGAlgorithmList is too small to hold the result.
111 IN EFI_RNG_PROTOCOL
*This
,
112 IN OUT UINTN
*RNGAlgorithmListSize
,
113 OUT EFI_RNG_ALGORITHM
*RNGAlgorithmList
117 EFI_RNG_ALGORITHM
*CpuRngSupportedAlgorithm
;
119 RequiredSize
= sizeof (EFI_RNG_ALGORITHM
);
121 if ((This
== NULL
) || (RNGAlgorithmListSize
== NULL
)) {
122 return EFI_INVALID_PARAMETER
;
125 if (*RNGAlgorithmListSize
< RequiredSize
) {
126 *RNGAlgorithmListSize
= RequiredSize
;
127 return EFI_BUFFER_TOO_SMALL
;
130 CpuRngSupportedAlgorithm
= PcdGetPtr (PcdCpuRngSupportedAlgorithm
);
132 CopyMem (&RNGAlgorithmList
[0], CpuRngSupportedAlgorithm
, sizeof (EFI_RNG_ALGORITHM
));
134 *RNGAlgorithmListSize
= RequiredSize
;