]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/RandomNumberGenerator/RngDxe/ArmRngDxe.c
SecurityPkg/RngDxe: Add Arm support of RngDxe
[mirror_edk2.git] / SecurityPkg / RandomNumberGenerator / RngDxe / ArmRngDxe.c
CommitLineData
4e5ecdba
RC
1/** @file\r
2 RNG Driver to produce the UEFI Random Number Generator protocol.\r
3\r
863fe9e1
SM
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
4e5ecdba
RC
7\r
8 RNG Algorithms defined in UEFI 2.4:\r
9 - EFI_RNG_ALGORITHM_SP800_90_CTR_256_GUID\r
863fe9e1 10 - EFI_RNG_ALGORITHM_RAW\r
4e5ecdba
RC
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
922bf317 19 Copyright (c) 2021 - 2022, Arm Limited. All rights reserved.<BR>\r
4e5ecdba
RC
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
4b3e9d80
PG
27#include <Library/DebugLib.h>\r
28#include <Library/MemoryAllocationLib.h>\r
4e5ecdba 29#include <Library/UefiBootServicesTableLib.h>\r
4b3e9d80 30#include <Library/RngLib.h>\r
4e5ecdba
RC
31#include <Protocol/Rng.h>\r
32\r
33#include "RngDxeInternals.h"\r
34\r
4b3e9d80
PG
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
4e5ecdba
RC
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
c411b485
MK
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
4e5ecdba
RC
76 )\r
77{\r
c411b485 78 EFI_STATUS Status;\r
4b3e9d80 79 UINTN Index;\r
4e5ecdba 80\r
199031b2 81 if ((This == NULL) || (RNGValueLength == 0) || (RNGValue == NULL)) {\r
4e5ecdba
RC
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
4b3e9d80
PG
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
4e5ecdba
RC
101 }\r
102\r
4b3e9d80 103FoundAlgo:\r
4e5ecdba
RC
104 if (CompareGuid (RNGAlgorithm, PcdGetPtr (PcdCpuRngSupportedAlgorithm))) {\r
105 Status = RngGetBytes (RNGValueLength, RNGValue);\r
106 return Status;\r
107 }\r
108\r
863fe9e1
SM
109 // Raw algorithm (Trng)\r
110 if (CompareGuid (RNGAlgorithm, &gEfiRngAlgorithmRaw)) {\r
111 return GenerateEntropy (RNGValueLength, RNGValue);\r
112 }\r
113\r
4e5ecdba
RC
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
922bf317 123 @param[in] This A pointer to the EFI_RNG_PROTOCOL instance.\r
4e5ecdba
RC
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
922bf317
PG
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
4e5ecdba
RC
140 @retval EFI_BUFFER_TOO_SMALL The buffer RNGAlgorithmList is too small to hold the result.\r
141\r
142**/\r
922bf317 143EFI_STATUS\r
4e5ecdba 144EFIAPI\r
922bf317
PG
145RngGetInfo (\r
146 IN EFI_RNG_PROTOCOL *This,\r
147 IN OUT UINTN *RNGAlgorithmListSize,\r
148 OUT EFI_RNG_ALGORITHM *RNGAlgorithmList\r
4e5ecdba
RC
149 )\r
150{\r
4b3e9d80 151 UINTN RequiredSize;\r
4e5ecdba 152\r
922bf317
PG
153 if ((This == NULL) || (RNGAlgorithmListSize == NULL)) {\r
154 return EFI_INVALID_PARAMETER;\r
155 }\r
156\r
4b3e9d80
PG
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
4e5ecdba
RC
164 if (*RNGAlgorithmListSize < RequiredSize) {\r
165 *RNGAlgorithmListSize = RequiredSize;\r
166 return EFI_BUFFER_TOO_SMALL;\r
167 }\r
168\r
4b3e9d80
PG
169 if (RNGAlgorithmList == NULL) {\r
170 return EFI_INVALID_PARAMETER;\r
171 }\r
4e5ecdba 172\r
4b3e9d80
PG
173 // There is no gap in the array, so copy the block.\r
174 CopyMem (RNGAlgorithmList, mAvailableAlgoArray, RequiredSize);\r
4e5ecdba
RC
175 *RNGAlgorithmListSize = RequiredSize;\r
176 return EFI_SUCCESS;\r
177}\r