]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/RandomNumberGenerator/RngDxe/ArmRngDxe.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / SecurityPkg / RandomNumberGenerator / RngDxe / ArmRngDxe.c
1 /** @file
2 RNG Driver to produce the UEFI Random Number Generator protocol.
3
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.
7
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
15
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>
20
21 SPDX-License-Identifier: BSD-2-Clause-Patent
22
23 **/
24
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>
32
33 #include "RngDxeInternals.h"
34
35 /** Free mAvailableAlgoArray.
36 **/
37 VOID
38 EFIAPI
39 FreeAvailableAlgorithms (
40 VOID
41 )
42 {
43 FreePool (mAvailableAlgoArray);
44 return;
45 }
46
47 /**
48 Produces and returns an RNG value using either the default or specified RNG algorithm.
49
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
57 resulting RNG value.
58
59 @retval EFI_SUCCESS The RNG value was returned successfully.
60 @retval EFI_UNSUPPORTED The algorithm specified by RNGAlgorithm is not supported by
61 this driver.
62 @retval EFI_DEVICE_ERROR An RNG value could not be retrieved due to a hardware or
63 firmware error.
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.
67
68 **/
69 EFI_STATUS
70 EFIAPI
71 RngGetRNG (
72 IN EFI_RNG_PROTOCOL *This,
73 IN EFI_RNG_ALGORITHM *RNGAlgorithm OPTIONAL,
74 IN UINTN RNGValueLength,
75 OUT UINT8 *RNGValue
76 )
77 {
78 EFI_STATUS Status;
79 UINTN Index;
80
81 if ((This == NULL) || (RNGValueLength == 0) || (RNGValue == NULL)) {
82 return EFI_INVALID_PARAMETER;
83 }
84
85 if (RNGAlgorithm == NULL) {
86 //
87 // Use the default RNG algorithm if RNGAlgorithm is NULL.
88 //
89 for (Index = 0; Index < mAvailableAlgoArrayCount; Index++) {
90 if (!IsZeroGuid (&mAvailableAlgoArray[Index])) {
91 RNGAlgorithm = &mAvailableAlgoArray[Index];
92 goto FoundAlgo;
93 }
94 }
95
96 if (Index == mAvailableAlgoArrayCount) {
97 // No algorithm available.
98 ASSERT (Index != mAvailableAlgoArrayCount);
99 return EFI_DEVICE_ERROR;
100 }
101 }
102
103 FoundAlgo:
104 if (CompareGuid (RNGAlgorithm, PcdGetPtr (PcdCpuRngSupportedAlgorithm))) {
105 Status = RngGetBytes (RNGValueLength, RNGValue);
106 return Status;
107 }
108
109 // Raw algorithm (Trng)
110 if (CompareGuid (RNGAlgorithm, &gEfiRngAlgorithmRaw)) {
111 return GenerateEntropy (RNGValueLength, RNGValue);
112 }
113
114 //
115 // Other algorithms are unsupported by this driver.
116 //
117 return EFI_UNSUPPORTED;
118 }
119
120 /**
121 Returns information about the random number generation implementation.
122
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.
134
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.
141
142 **/
143 EFI_STATUS
144 EFIAPI
145 RngGetInfo (
146 IN EFI_RNG_PROTOCOL *This,
147 IN OUT UINTN *RNGAlgorithmListSize,
148 OUT EFI_RNG_ALGORITHM *RNGAlgorithmList
149 )
150 {
151 UINTN RequiredSize;
152
153 if ((This == NULL) || (RNGAlgorithmListSize == NULL)) {
154 return EFI_INVALID_PARAMETER;
155 }
156
157 RequiredSize = mAvailableAlgoArrayCount * sizeof (EFI_RNG_ALGORITHM);
158
159 if (RequiredSize == 0) {
160 // No supported algorithms found.
161 return EFI_UNSUPPORTED;
162 }
163
164 if (*RNGAlgorithmListSize < RequiredSize) {
165 *RNGAlgorithmListSize = RequiredSize;
166 return EFI_BUFFER_TOO_SMALL;
167 }
168
169 if (RNGAlgorithmList == NULL) {
170 return EFI_INVALID_PARAMETER;
171 }
172
173 // There is no gap in the array, so copy the block.
174 CopyMem (RNGAlgorithmList, mAvailableAlgoArray, RequiredSize);
175 *RNGAlgorithmListSize = RequiredSize;
176 return EFI_SUCCESS;
177 }