]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RngDxe.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / SecurityPkg / RandomNumberGenerator / RngDxe / Rand / RngDxe.c
1 /** @file
2 RNG Driver to produce the UEFI Random Number Generator protocol.
3
4 The driver will use the new RDRAND instruction to produce high-quality, high-performance
5 entropy and random number.
6
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
16
17 Copyright (c) 2021 - 2022, Arm Limited. All rights reserved.<BR>
18 Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
19 (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
20 SPDX-License-Identifier: BSD-2-Clause-Patent
21
22 **/
23
24 #include <Library/BaseLib.h>
25 #include <Library/BaseMemoryLib.h>
26
27 #include "RngDxeInternals.h"
28
29 /** Allocate and initialize mAvailableAlgoArray with the available
30 Rng algorithms. Also update mAvailableAlgoArrayCount.
31
32 @retval EFI_SUCCESS The function completed successfully.
33 @retval EFI_OUT_OF_RESOURCES Could not allocate memory.
34 **/
35 EFI_STATUS
36 EFIAPI
37 GetAvailableAlgorithms (
38 VOID
39 )
40 {
41 return EFI_SUCCESS;
42 }
43
44 /** Free mAvailableAlgoArray.
45 **/
46 VOID
47 EFIAPI
48 FreeAvailableAlgorithms (
49 VOID
50 )
51 {
52 return;
53 }
54
55 /**
56 Produces and returns an RNG value using either the default or specified RNG algorithm.
57
58 @param[in] This A pointer to the EFI_RNG_PROTOCOL instance.
59 @param[in] RNGAlgorithm A pointer to the EFI_RNG_ALGORITHM that identifies the RNG
60 algorithm to use. May be NULL in which case the function will
61 use its default RNG algorithm.
62 @param[in] RNGValueLength The length in bytes of the memory buffer pointed to by
63 RNGValue. The driver shall return exactly this numbers of bytes.
64 @param[out] RNGValue A caller-allocated memory buffer filled by the driver with the
65 resulting RNG value.
66
67 @retval EFI_SUCCESS The RNG value was returned successfully.
68 @retval EFI_UNSUPPORTED The algorithm specified by RNGAlgorithm is not supported by
69 this driver.
70 @retval EFI_DEVICE_ERROR An RNG value could not be retrieved due to a hardware or
71 firmware error.
72 @retval EFI_NOT_READY There is not enough random data available to satisfy the length
73 requested by RNGValueLength.
74 @retval EFI_INVALID_PARAMETER RNGValue is NULL or RNGValueLength is zero.
75
76 **/
77 EFI_STATUS
78 EFIAPI
79 RngGetRNG (
80 IN EFI_RNG_PROTOCOL *This,
81 IN EFI_RNG_ALGORITHM *RNGAlgorithm OPTIONAL,
82 IN UINTN RNGValueLength,
83 OUT UINT8 *RNGValue
84 )
85 {
86 EFI_STATUS Status;
87
88 if ((This == NULL) || (RNGValueLength == 0) || (RNGValue == NULL)) {
89 return EFI_INVALID_PARAMETER;
90 }
91
92 Status = EFI_UNSUPPORTED;
93 if (RNGAlgorithm == NULL) {
94 //
95 // Use the default RNG algorithm if RNGAlgorithm is NULL.
96 //
97 RNGAlgorithm = &gEfiRngAlgorithmSp80090Ctr256Guid;
98 }
99
100 //
101 // NIST SP800-90-AES-CTR-256 supported by RDRAND
102 //
103 if (CompareGuid (RNGAlgorithm, &gEfiRngAlgorithmSp80090Ctr256Guid)) {
104 Status = RngGetBytes (RNGValueLength, RNGValue);
105 return Status;
106 }
107
108 //
109 // The "raw" algorithm is intended to provide entropy directly
110 //
111 if (CompareGuid (RNGAlgorithm, &gEfiRngAlgorithmRaw)) {
112 //
113 // When a DRBG is used on the output of a entropy source,
114 // its security level must be at least 256 bits according to UEFI Spec.
115 //
116 if (RNGValueLength < 32) {
117 return EFI_INVALID_PARAMETER;
118 }
119
120 Status = GenerateEntropy (RNGValueLength, RNGValue);
121 return Status;
122 }
123
124 //
125 // Other algorithms were unsupported by this driver.
126 //
127 return Status;
128 }
129
130 /**
131 Returns information about the random number generation implementation.
132
133 @param[in] This A pointer to the EFI_RNG_PROTOCOL instance.
134 @param[in,out] RNGAlgorithmListSize On input, the size in bytes of RNGAlgorithmList.
135 On output with a return code of EFI_SUCCESS, the size
136 in bytes of the data returned in RNGAlgorithmList. On output
137 with a return code of EFI_BUFFER_TOO_SMALL,
138 the size of RNGAlgorithmList required to obtain the list.
139 @param[out] RNGAlgorithmList A caller-allocated memory buffer filled by the driver
140 with one EFI_RNG_ALGORITHM element for each supported
141 RNG algorithm. The list must not change across multiple
142 calls to the same driver. The first algorithm in the list
143 is the default algorithm for the driver.
144
145 @retval EFI_SUCCESS The RNG algorithm list was returned successfully.
146 @retval EFI_UNSUPPORTED No supported algorithms found.
147 @retval EFI_DEVICE_ERROR The list of algorithms could not be retrieved due to a
148 hardware or firmware error.
149 @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect.
150 @retval EFI_BUFFER_TOO_SMALL The buffer RNGAlgorithmList is too small to hold the result.
151
152 **/
153 EFI_STATUS
154 EFIAPI
155 RngGetInfo (
156 IN EFI_RNG_PROTOCOL *This,
157 IN OUT UINTN *RNGAlgorithmListSize,
158 OUT EFI_RNG_ALGORITHM *RNGAlgorithmList
159 )
160 {
161 UINTN RequiredSize;
162
163 if ((This == NULL) || (RNGAlgorithmListSize == NULL)) {
164 return EFI_INVALID_PARAMETER;
165 }
166
167 RequiredSize = 2 * sizeof (EFI_RNG_ALGORITHM);
168
169 if (*RNGAlgorithmListSize < RequiredSize) {
170 *RNGAlgorithmListSize = RequiredSize;
171 return EFI_BUFFER_TOO_SMALL;
172 }
173
174 if (RNGAlgorithmList == NULL) {
175 return EFI_INVALID_PARAMETER;
176 }
177
178 CopyMem (&RNGAlgorithmList[0], &gEfiRngAlgorithmSp80090Ctr256Guid, sizeof (EFI_RNG_ALGORITHM));
179
180 // x86 platforms also support EFI_RNG_ALGORITHM_RAW via RDSEED
181 CopyMem (&RNGAlgorithmList[1], &gEfiRngAlgorithmRaw, sizeof (EFI_RNG_ALGORITHM));
182
183 *RNGAlgorithmListSize = RequiredSize;
184 return EFI_SUCCESS;
185 }