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
24 #include "RngDxeInternals.h"
27 Produces and returns an RNG value using either the default or specified RNG algorithm.
29 @param[in] This A pointer to the EFI_RNG_PROTOCOL instance.
30 @param[in] RNGAlgorithm A pointer to the EFI_RNG_ALGORITHM that identifies the RNG
31 algorithm to use. May be NULL in which case the function will
32 use its default RNG algorithm.
33 @param[in] RNGValueLength The length in bytes of the memory buffer pointed to by
34 RNGValue. The driver shall return exactly this numbers of bytes.
35 @param[out] RNGValue A caller-allocated memory buffer filled by the driver with the
38 @retval EFI_SUCCESS The RNG value was returned successfully.
39 @retval EFI_UNSUPPORTED The algorithm specified by RNGAlgorithm is not supported by
41 @retval EFI_DEVICE_ERROR An RNG value could not be retrieved due to a hardware or
43 @retval EFI_NOT_READY There is not enough random data available to satisfy the length
44 requested by RNGValueLength.
45 @retval EFI_INVALID_PARAMETER RNGValue is NULL or RNGValueLength is zero.
51 IN EFI_RNG_PROTOCOL
*This
,
52 IN EFI_RNG_ALGORITHM
*RNGAlgorithm OPTIONAL
,
53 IN UINTN RNGValueLength
,
59 if ((RNGValueLength
== 0) || (RNGValue
== NULL
)) {
60 return EFI_INVALID_PARAMETER
;
63 Status
= EFI_UNSUPPORTED
;
64 if (RNGAlgorithm
== NULL
) {
66 // Use the default RNG algorithm if RNGAlgorithm is NULL.
68 RNGAlgorithm
= &gEfiRngAlgorithmSp80090Ctr256Guid
;
72 // NIST SP800-90-AES-CTR-256 supported by RDRAND
74 if (CompareGuid (RNGAlgorithm
, &gEfiRngAlgorithmSp80090Ctr256Guid
)) {
75 Status
= RngGetBytes (RNGValueLength
, RNGValue
);
80 // The "raw" algorithm is intended to provide entropy directly
82 if (CompareGuid (RNGAlgorithm
, &gEfiRngAlgorithmRaw
)) {
84 // When a DRBG is used on the output of a entropy source,
85 // its security level must be at least 256 bits according to UEFI Spec.
87 if (RNGValueLength
< 32) {
88 return EFI_INVALID_PARAMETER
;
91 Status
= RdRandGenerateEntropy (RNGValueLength
, RNGValue
);
96 // Other algorithms were unsupported by this driver.
102 Returns information about the random number generation implementation.
104 @param[in,out] RNGAlgorithmListSize On input, the size in bytes of RNGAlgorithmList.
105 On output with a return code of EFI_SUCCESS, the size
106 in bytes of the data returned in RNGAlgorithmList. On output
107 with a return code of EFI_BUFFER_TOO_SMALL,
108 the size of RNGAlgorithmList required to obtain the list.
109 @param[out] RNGAlgorithmList A caller-allocated memory buffer filled by the driver
110 with one EFI_RNG_ALGORITHM element for each supported
111 RNG algorithm. The list must not change across multiple
112 calls to the same driver. The first algorithm in the list
113 is the default algorithm for the driver.
115 @retval EFI_SUCCESS The RNG algorithm list was returned successfully.
116 @retval EFI_BUFFER_TOO_SMALL The buffer RNGAlgorithmList is too small to hold the result.
121 ArchGetSupportedRngAlgorithms (
122 IN OUT UINTN
*RNGAlgorithmListSize
,
123 OUT EFI_RNG_ALGORITHM
*RNGAlgorithmList
127 EFI_RNG_ALGORITHM
*CpuRngSupportedAlgorithm
;
129 RequiredSize
= 2 * sizeof (EFI_RNG_ALGORITHM
);
131 if (*RNGAlgorithmListSize
< RequiredSize
) {
132 *RNGAlgorithmListSize
= RequiredSize
;
133 return EFI_BUFFER_TOO_SMALL
;
136 CpuRngSupportedAlgorithm
= PcdGetPtr (PcdCpuRngSupportedAlgorithm
);
138 CopyMem(&RNGAlgorithmList
[0], CpuRngSupportedAlgorithm
, sizeof (EFI_RNG_ALGORITHM
));
140 // x86 platforms also support EFI_RNG_ALGORITHM_RAW via RDSEED
141 CopyMem(&RNGAlgorithmList
[1], &gEfiRngAlgorithmRaw
, sizeof (EFI_RNG_ALGORITHM
));
143 *RNGAlgorithmListSize
= RequiredSize
;