SecurityPkg HashLibBaseCryptoRouterDxe/Tpm2DeviceLibRouterPei: Fixed GCC build failur...
[mirror_edk2.git] / SecurityPkg / Library / HashLibBaseCryptoRouter / HashLibBaseCryptoRouterDxe.c
CommitLineData
c1d93242
JY
1/** @file\r
2 Ihis library is BaseCrypto router. It will redirect hash request to each individual\r
3 hash handler registerd, such as SHA1, SHA256.\r
4 Platform can use PcdTpm2HashMask to mask some hash engines.\r
5\r
859b0db4 6Copyright (c) 2013 - 2014, Intel Corporation. All rights reserved. <BR>\r
c1d93242
JY
7This program and the accompanying materials\r
8are licensed and made available under the terms and conditions of the BSD License\r
9which accompanies this distribution. The full text of the license may be found at\r
10http://opensource.org/licenses/bsd-license.php\r
11\r
12THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
13WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14\r
15**/\r
16\r
17#include <PiPei.h>\r
18#include <Library/BaseLib.h>\r
19#include <Library/BaseMemoryLib.h>\r
20#include <Library/Tpm2CommandLib.h>\r
21#include <Library/DebugLib.h>\r
22#include <Library/MemoryAllocationLib.h>\r
23#include <Library/PcdLib.h>\r
24#include <Library/HashLib.h>\r
25\r
26#include "HashLibBaseCryptoRouterCommon.h"\r
27\r
859b0db4 28HASH_INTERFACE mHashInterface[HASH_COUNT] = {{{0}, NULL, NULL, NULL}};\r
c1d93242
JY
29UINTN mHashInterfaceCount = 0;\r
30\r
31/**\r
32 Start hash sequence.\r
33\r
34 @param HashHandle Hash handle.\r
35\r
36 @retval EFI_SUCCESS Hash sequence start and HandleHandle returned.\r
37 @retval EFI_OUT_OF_RESOURCES No enough resource to start hash.\r
38**/\r
39EFI_STATUS\r
40EFIAPI\r
41HashStart (\r
42 OUT HASH_HANDLE *HashHandle\r
43 )\r
44{\r
45 HASH_HANDLE *HashCtx;\r
46 UINTN Index;\r
47\r
48 if (mHashInterfaceCount == 0) {\r
49 return EFI_UNSUPPORTED;\r
50 }\r
51\r
52 HashCtx = AllocatePool (sizeof(*HashCtx) * mHashInterfaceCount);\r
53 ASSERT (HashCtx != NULL);\r
54\r
55 for (Index = 0; Index < mHashInterfaceCount; Index++) {\r
56 mHashInterface[Index].HashInit (&HashCtx[Index]);\r
57 }\r
58\r
59 *HashHandle = (HASH_HANDLE)HashCtx;\r
60\r
61 return EFI_SUCCESS;\r
62}\r
63\r
64/**\r
65 Update hash sequence data.\r
66\r
67 @param HashHandle Hash handle.\r
68 @param DataToHash Data to be hashed.\r
69 @param DataToHashLen Data size.\r
70\r
71 @retval EFI_SUCCESS Hash sequence updated.\r
72**/\r
73EFI_STATUS\r
74EFIAPI\r
75HashUpdate (\r
76 IN HASH_HANDLE HashHandle,\r
77 IN VOID *DataToHash,\r
78 IN UINTN DataToHashLen\r
79 )\r
80{\r
81 HASH_HANDLE *HashCtx;\r
82 UINTN Index;\r
83\r
84 if (mHashInterfaceCount == 0) {\r
85 return EFI_UNSUPPORTED;\r
86 }\r
87\r
88 HashCtx = (HASH_HANDLE *)HashHandle;\r
89\r
90 for (Index = 0; Index < mHashInterfaceCount; Index++) {\r
91 mHashInterface[Index].HashUpdate (HashCtx[Index], DataToHash, DataToHashLen);\r
92 }\r
93\r
94 return EFI_SUCCESS;\r
95}\r
96\r
97/**\r
98 Hash sequence complete and extend to PCR.\r
99\r
100 @param HashHandle Hash handle.\r
101 @param PcrIndex PCR to be extended.\r
102 @param DataToHash Data to be hashed.\r
103 @param DataToHashLen Data size.\r
104 @param DigestList Digest list.\r
105\r
106 @retval EFI_SUCCESS Hash sequence complete and DigestList is returned.\r
107**/\r
108EFI_STATUS\r
109EFIAPI\r
110HashCompleteAndExtend (\r
111 IN HASH_HANDLE HashHandle,\r
112 IN TPMI_DH_PCR PcrIndex,\r
113 IN VOID *DataToHash,\r
114 IN UINTN DataToHashLen,\r
115 OUT TPML_DIGEST_VALUES *DigestList\r
116 )\r
117{\r
118 TPML_DIGEST_VALUES Digest;\r
119 HASH_HANDLE *HashCtx;\r
120 UINTN Index;\r
121 EFI_STATUS Status;\r
122\r
123 if (mHashInterfaceCount == 0) {\r
124 return EFI_UNSUPPORTED;\r
125 }\r
126\r
127 HashCtx = (HASH_HANDLE *)HashHandle;\r
128 ZeroMem (DigestList, sizeof(*DigestList));\r
129\r
130 for (Index = 0; Index < mHashInterfaceCount; Index++) {\r
131 mHashInterface[Index].HashUpdate (HashCtx[Index], DataToHash, DataToHashLen);\r
132 mHashInterface[Index].HashFinal (HashCtx[Index], &Digest);\r
133 Tpm2SetHashToDigestList (DigestList, &Digest);\r
134 }\r
135\r
136 FreePool (HashCtx);\r
137\r
138 Status = Tpm2PcrExtend (\r
139 PcrIndex,\r
140 DigestList\r
141 );\r
142 return Status;\r
143}\r
144\r
145/**\r
146 Hash data and extend to PCR.\r
147\r
148 @param PcrIndex PCR to be extended.\r
149 @param DataToHash Data to be hashed.\r
150 @param DataToHashLen Data size.\r
151 @param DigestList Digest list.\r
152\r
153 @retval EFI_SUCCESS Hash data and DigestList is returned.\r
154**/\r
155EFI_STATUS\r
156EFIAPI\r
157HashAndExtend (\r
158 IN TPMI_DH_PCR PcrIndex,\r
159 IN VOID *DataToHash,\r
160 IN UINTN DataToHashLen,\r
161 OUT TPML_DIGEST_VALUES *DigestList\r
162 )\r
163{\r
164 HASH_HANDLE HashHandle;\r
165 EFI_STATUS Status;\r
166\r
167 if (mHashInterfaceCount == 0) {\r
168 return EFI_UNSUPPORTED;\r
169 }\r
170\r
171 HashStart (&HashHandle);\r
172 HashUpdate (HashHandle, DataToHash, DataToHashLen);\r
173 Status = HashCompleteAndExtend (HashHandle, PcrIndex, NULL, 0, DigestList);\r
174\r
175 return Status;\r
176}\r
177\r
178/**\r
179 This service register Hash.\r
180\r
181 @param HashInterface Hash interface\r
182\r
183 @retval EFI_SUCCESS This hash interface is registered successfully.\r
184 @retval EFI_UNSUPPORTED System does not support register this interface.\r
185 @retval EFI_ALREADY_STARTED System already register this interface.\r
186**/\r
187EFI_STATUS\r
188EFIAPI\r
189RegisterHashInterfaceLib (\r
190 IN HASH_INTERFACE *HashInterface\r
191 )\r
192{\r
193 UINTN Index;\r
194 UINT32 HashMask;\r
195\r
196 //\r
197 // Check allow\r
198 //\r
199 HashMask = Tpm2GetHashMaskFromAlgo (&HashInterface->HashGuid);\r
200 if ((HashMask & PcdGet32 (PcdTpm2HashMask)) == 0) {\r
201 return EFI_UNSUPPORTED;\r
202 }\r
203\r
204 if (mHashInterfaceCount >= sizeof(mHashInterface)/sizeof(mHashInterface[0])) {\r
205 return EFI_OUT_OF_RESOURCES;\r
206 }\r
207\r
208 //\r
209 // Check duplication\r
210 //\r
211 for (Index = 0; Index < mHashInterfaceCount; Index++) {\r
212 if (CompareGuid (&mHashInterface[Index].HashGuid, &HashInterface->HashGuid)) {\r
213 return EFI_ALREADY_STARTED;\r
214 }\r
215 }\r
216\r
217 CopyMem (&mHashInterface[mHashInterfaceCount], HashInterface, sizeof(*HashInterface));\r
218 mHashInterfaceCount ++;\r
219 \r
220 return EFI_SUCCESS;\r
221}