2 RNG Driver to produce the UEFI Random Number Generator protocol.
4 The driver will use the new RDRAND instruction to produce high-quality, high-performance
5 entropy and random number.
7 RNG Algorithms defined in UEFI 2.4:
8 - EFI_RNG_ALGORITHM_SP800_90_CTR_256_GUID - Supported
9 (RDRAND implements a hardware NIST SP800-90 AES-CTR-256 based DRBG)
10 - EFI_RNG_ALGORITHM_RAW - Supported
11 (Structuring RDRAND invocation can be guaranteed as high-quality entropy source)
12 - EFI_RNG_ALGORITHM_SP800_90_HMAC_256_GUID - Unsupported
13 - EFI_RNG_ALGORITHM_SP800_90_HASH_256_GUID - Unsupported
14 - EFI_RNG_ALGORITHM_X9_31_3DES_GUID - Unsupported
15 - EFI_RNG_ALGORITHM_X9_31_AES_GUID - Unsupported
17 Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
18 (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
19 SPDX-License-Identifier: BSD-2-Clause-Patent
26 // Supported RNG Algorithms list by this driver.
28 EFI_RNG_ALGORITHM mSupportedRngAlgorithms
[] = {
29 EFI_RNG_ALGORITHM_SP800_90_CTR_256_GUID
,
34 Returns information about the random number generation implementation.
36 @param[in] This A pointer to the EFI_RNG_PROTOCOL instance.
37 @param[in,out] RNGAlgorithmListSize On input, the size in bytes of RNGAlgorithmList.
38 On output with a return code of EFI_SUCCESS, the size
39 in bytes of the data returned in RNGAlgorithmList. On output
40 with a return code of EFI_BUFFER_TOO_SMALL,
41 the size of RNGAlgorithmList required to obtain the list.
42 @param[out] RNGAlgorithmList A caller-allocated memory buffer filled by the driver
43 with one EFI_RNG_ALGORITHM element for each supported
44 RNG algorithm. The list must not change across multiple
45 calls to the same driver. The first algorithm in the list
46 is the default algorithm for the driver.
48 @retval EFI_SUCCESS The RNG algorithm list was returned successfully.
49 @retval EFI_UNSUPPORTED The services is not supported by this driver.
50 @retval EFI_DEVICE_ERROR The list of algorithms could not be retrieved due to a
51 hardware or firmware error.
52 @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect.
53 @retval EFI_BUFFER_TOO_SMALL The buffer RNGAlgorithmList is too small to hold the result.
59 IN EFI_RNG_PROTOCOL
*This
,
60 IN OUT UINTN
*RNGAlgorithmListSize
,
61 OUT EFI_RNG_ALGORITHM
*RNGAlgorithmList
67 if ((This
== NULL
) || (RNGAlgorithmListSize
== NULL
)) {
68 return EFI_INVALID_PARAMETER
;
71 RequiredSize
= sizeof (mSupportedRngAlgorithms
);
72 if (*RNGAlgorithmListSize
< RequiredSize
) {
73 Status
= EFI_BUFFER_TOO_SMALL
;
76 // Return algorithm list supported by driver.
78 if (RNGAlgorithmList
!= NULL
) {
79 CopyMem (RNGAlgorithmList
, mSupportedRngAlgorithms
, RequiredSize
);
82 Status
= EFI_INVALID_PARAMETER
;
85 *RNGAlgorithmListSize
= RequiredSize
;
91 Produces and returns an RNG value using either the default or specified RNG algorithm.
93 @param[in] This A pointer to the EFI_RNG_PROTOCOL instance.
94 @param[in] RNGAlgorithm A pointer to the EFI_RNG_ALGORITHM that identifies the RNG
95 algorithm to use. May be NULL in which case the function will
96 use its default RNG algorithm.
97 @param[in] RNGValueLength The length in bytes of the memory buffer pointed to by
98 RNGValue. The driver shall return exactly this numbers of bytes.
99 @param[out] RNGValue A caller-allocated memory buffer filled by the driver with the
102 @retval EFI_SUCCESS The RNG value was returned successfully.
103 @retval EFI_UNSUPPORTED The algorithm specified by RNGAlgorithm is not supported by
105 @retval EFI_DEVICE_ERROR An RNG value could not be retrieved due to a hardware or
107 @retval EFI_NOT_READY There is not enough random data available to satisfy the length
108 requested by RNGValueLength.
109 @retval EFI_INVALID_PARAMETER RNGValue is NULL or RNGValueLength is zero.
115 IN EFI_RNG_PROTOCOL
*This
,
116 IN EFI_RNG_ALGORITHM
*RNGAlgorithm
, OPTIONAL
117 IN UINTN RNGValueLength
,
123 if ((RNGValueLength
== 0) || (RNGValue
== NULL
)) {
124 return EFI_INVALID_PARAMETER
;
127 Status
= EFI_UNSUPPORTED
;
128 if (RNGAlgorithm
== NULL
) {
130 // Use the default RNG algorithm if RNGAlgorithm is NULL.
132 RNGAlgorithm
= &gEfiRngAlgorithmSp80090Ctr256Guid
;
136 // NIST SP800-90-AES-CTR-256 supported by RDRAND
138 if (CompareGuid (RNGAlgorithm
, &gEfiRngAlgorithmSp80090Ctr256Guid
)) {
139 Status
= RdRandGetBytes (RNGValueLength
, RNGValue
);
144 // The "raw" algorithm is intended to provide entropy directly
146 if (CompareGuid (RNGAlgorithm
, &gEfiRngAlgorithmRaw
)) {
148 // When a DRBG is used on the output of a entropy source,
149 // its security level must be at least 256 bits according to UEFI Spec.
151 if (RNGValueLength
< 32) {
152 return EFI_INVALID_PARAMETER
;
155 Status
= RdRandGenerateEntropy (RNGValueLength
, RNGValue
);
160 // Other algorithms were unsupported by this driver.
166 // The Random Number Generator (RNG) protocol
168 EFI_RNG_PROTOCOL mRngRdRand
= {
174 The user Entry Point for the Random Number Generator (RNG) driver.
176 @param[in] ImageHandle The firmware allocated handle for the EFI image.
177 @param[in] SystemTable A pointer to the EFI System Table.
179 @retval EFI_SUCCESS The entry point is executed successfully.
180 @retval EFI_NOT_SUPPORTED Platform does not support RNG.
181 @retval Other Some error occurs when executing this entry point.
187 IN EFI_HANDLE ImageHandle
,
188 IN EFI_SYSTEM_TABLE
*SystemTable
195 // Install UEFI RNG (Random Number Generator) Protocol
198 Status
= gBS
->InstallMultipleProtocolInterfaces (
200 &gEfiRngProtocolGuid
,