]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - SecurityPkg/RandomNumberGenerator/RngDxe/ArmRngDxe.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / SecurityPkg / RandomNumberGenerator / RngDxe / ArmRngDxe.c
... / ...
CommitLineData
1/** @file\r
2 RNG Driver to produce the UEFI Random Number Generator protocol.\r
3\r
4 The driver can use RNDR instruction (through the RngLib and if FEAT_RNG is\r
5 present) to produce random numbers. It also uses the Arm FW-TRNG interface\r
6 to implement EFI_RNG_ALGORITHM_RAW.\r
7\r
8 RNG Algorithms defined in UEFI 2.4:\r
9 - EFI_RNG_ALGORITHM_SP800_90_CTR_256_GUID\r
10 - EFI_RNG_ALGORITHM_RAW\r
11 - EFI_RNG_ALGORITHM_SP800_90_HMAC_256_GUID\r
12 - EFI_RNG_ALGORITHM_SP800_90_HASH_256_GUID\r
13 - EFI_RNG_ALGORITHM_X9_31_3DES_GUID - Unsupported\r
14 - EFI_RNG_ALGORITHM_X9_31_AES_GUID - Unsupported\r
15\r
16 Copyright (c) 2021, NUVIA Inc. All rights reserved.<BR>\r
17 Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>\r
18 (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>\r
19 Copyright (c) 2021 - 2022, Arm Limited. All rights reserved.<BR>\r
20\r
21 SPDX-License-Identifier: BSD-2-Clause-Patent\r
22\r
23**/\r
24\r
25#include <Library/BaseLib.h>\r
26#include <Library/BaseMemoryLib.h>\r
27#include <Library/DebugLib.h>\r
28#include <Library/MemoryAllocationLib.h>\r
29#include <Library/UefiBootServicesTableLib.h>\r
30#include <Library/RngLib.h>\r
31#include <Protocol/Rng.h>\r
32\r
33#include "RngDxeInternals.h"\r
34\r
35/** Free mAvailableAlgoArray.\r
36**/\r
37VOID\r
38EFIAPI\r
39FreeAvailableAlgorithms (\r
40 VOID\r
41 )\r
42{\r
43 FreePool (mAvailableAlgoArray);\r
44 return;\r
45}\r
46\r
47/**\r
48 Produces and returns an RNG value using either the default or specified RNG algorithm.\r
49\r
50 @param[in] This A pointer to the EFI_RNG_PROTOCOL instance.\r
51 @param[in] RNGAlgorithm A pointer to the EFI_RNG_ALGORITHM that identifies the RNG\r
52 algorithm to use. May be NULL in which case the function will\r
53 use its default RNG algorithm.\r
54 @param[in] RNGValueLength The length in bytes of the memory buffer pointed to by\r
55 RNGValue. The driver shall return exactly this numbers of bytes.\r
56 @param[out] RNGValue A caller-allocated memory buffer filled by the driver with the\r
57 resulting RNG value.\r
58\r
59 @retval EFI_SUCCESS The RNG value was returned successfully.\r
60 @retval EFI_UNSUPPORTED The algorithm specified by RNGAlgorithm is not supported by\r
61 this driver.\r
62 @retval EFI_DEVICE_ERROR An RNG value could not be retrieved due to a hardware or\r
63 firmware error.\r
64 @retval EFI_NOT_READY There is not enough random data available to satisfy the length\r
65 requested by RNGValueLength.\r
66 @retval EFI_INVALID_PARAMETER RNGValue is NULL or RNGValueLength is zero.\r
67\r
68**/\r
69EFI_STATUS\r
70EFIAPI\r
71RngGetRNG (\r
72 IN EFI_RNG_PROTOCOL *This,\r
73 IN EFI_RNG_ALGORITHM *RNGAlgorithm OPTIONAL,\r
74 IN UINTN RNGValueLength,\r
75 OUT UINT8 *RNGValue\r
76 )\r
77{\r
78 EFI_STATUS Status;\r
79 UINTN Index;\r
80\r
81 if ((This == NULL) || (RNGValueLength == 0) || (RNGValue == NULL)) {\r
82 return EFI_INVALID_PARAMETER;\r
83 }\r
84\r
85 if (RNGAlgorithm == NULL) {\r
86 //\r
87 // Use the default RNG algorithm if RNGAlgorithm is NULL.\r
88 //\r
89 for (Index = 0; Index < mAvailableAlgoArrayCount; Index++) {\r
90 if (!IsZeroGuid (&mAvailableAlgoArray[Index])) {\r
91 RNGAlgorithm = &mAvailableAlgoArray[Index];\r
92 goto FoundAlgo;\r
93 }\r
94 }\r
95\r
96 if (Index == mAvailableAlgoArrayCount) {\r
97 // No algorithm available.\r
98 ASSERT (Index != mAvailableAlgoArrayCount);\r
99 return EFI_DEVICE_ERROR;\r
100 }\r
101 }\r
102\r
103FoundAlgo:\r
104 if (CompareGuid (RNGAlgorithm, PcdGetPtr (PcdCpuRngSupportedAlgorithm))) {\r
105 Status = RngGetBytes (RNGValueLength, RNGValue);\r
106 return Status;\r
107 }\r
108\r
109 // Raw algorithm (Trng)\r
110 if (CompareGuid (RNGAlgorithm, &gEfiRngAlgorithmRaw)) {\r
111 return GenerateEntropy (RNGValueLength, RNGValue);\r
112 }\r
113\r
114 //\r
115 // Other algorithms are unsupported by this driver.\r
116 //\r
117 return EFI_UNSUPPORTED;\r
118}\r
119\r
120/**\r
121 Returns information about the random number generation implementation.\r
122\r
123 @param[in] This A pointer to the EFI_RNG_PROTOCOL instance.\r
124 @param[in,out] RNGAlgorithmListSize On input, the size in bytes of RNGAlgorithmList.\r
125 On output with a return code of EFI_SUCCESS, the size\r
126 in bytes of the data returned in RNGAlgorithmList. On output\r
127 with a return code of EFI_BUFFER_TOO_SMALL,\r
128 the size of RNGAlgorithmList required to obtain the list.\r
129 @param[out] RNGAlgorithmList A caller-allocated memory buffer filled by the driver\r
130 with one EFI_RNG_ALGORITHM element for each supported\r
131 RNG algorithm. The list must not change across multiple\r
132 calls to the same driver. The first algorithm in the list\r
133 is the default algorithm for the driver.\r
134\r
135 @retval EFI_SUCCESS The RNG algorithm list was returned successfully.\r
136 @retval EFI_UNSUPPORTED The services is not supported by this driver.\r
137 @retval EFI_DEVICE_ERROR The list of algorithms could not be retrieved due to a\r
138 hardware or firmware error.\r
139 @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect.\r
140 @retval EFI_BUFFER_TOO_SMALL The buffer RNGAlgorithmList is too small to hold the result.\r
141\r
142**/\r
143EFI_STATUS\r
144EFIAPI\r
145RngGetInfo (\r
146 IN EFI_RNG_PROTOCOL *This,\r
147 IN OUT UINTN *RNGAlgorithmListSize,\r
148 OUT EFI_RNG_ALGORITHM *RNGAlgorithmList\r
149 )\r
150{\r
151 UINTN RequiredSize;\r
152\r
153 if ((This == NULL) || (RNGAlgorithmListSize == NULL)) {\r
154 return EFI_INVALID_PARAMETER;\r
155 }\r
156\r
157 RequiredSize = mAvailableAlgoArrayCount * sizeof (EFI_RNG_ALGORITHM);\r
158\r
159 if (RequiredSize == 0) {\r
160 // No supported algorithms found.\r
161 return EFI_UNSUPPORTED;\r
162 }\r
163\r
164 if (*RNGAlgorithmListSize < RequiredSize) {\r
165 *RNGAlgorithmListSize = RequiredSize;\r
166 return EFI_BUFFER_TOO_SMALL;\r
167 }\r
168\r
169 if (RNGAlgorithmList == NULL) {\r
170 return EFI_INVALID_PARAMETER;\r
171 }\r
172\r
173 // There is no gap in the array, so copy the block.\r
174 CopyMem (RNGAlgorithmList, mAvailableAlgoArray, RequiredSize);\r
175 *RNGAlgorithmListSize = RequiredSize;\r
176 return EFI_SUCCESS;\r
177}\r