2 Ihis library is BaseCrypto router. It will redirect hash request to each individual
3 hash handler registerd, such as SHA1, SHA256.
4 Platform can use PcdTpm2HashMask to mask some hash engines.
6 Copyright (c) 2013 - 2014, Intel Corporation. All rights reserved. <BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include <Library/BaseLib.h>
19 #include <Library/BaseMemoryLib.h>
20 #include <Library/Tpm2CommandLib.h>
21 #include <Library/DebugLib.h>
22 #include <Library/MemoryAllocationLib.h>
23 #include <Library/PcdLib.h>
24 #include <Library/HobLib.h>
25 #include <Library/HashLib.h>
27 #include "HashLibBaseCryptoRouterCommon.h"
29 #define HASH_LIB_PEI_ROUTER_GUID \
30 { 0x84681c08, 0x6873, 0x46f3, { 0x8b, 0xb7, 0xab, 0x66, 0x18, 0x95, 0xa1, 0xb3 } }
32 EFI_GUID mHashLibPeiRouterGuid
= HASH_LIB_PEI_ROUTER_GUID
;
35 UINTN HashInterfaceCount
;
36 HASH_INTERFACE HashInterface
[HASH_COUNT
];
40 This function get hash interface.
42 @retval hash interface.
45 InternalGetHashInterface (
49 EFI_HOB_GUID_TYPE
*Hob
;
51 Hob
= GetFirstGuidHob (&mHashLibPeiRouterGuid
);
55 return (HASH_INTERFACE_HOB
*)(Hob
+ 1);
61 @param HashHandle Hash handle.
63 @retval EFI_SUCCESS Hash sequence start and HandleHandle returned.
64 @retval EFI_OUT_OF_RESOURCES No enough resource to start hash.
69 OUT HASH_HANDLE
*HashHandle
72 HASH_INTERFACE_HOB
*HashInterfaceHob
;
76 HashInterfaceHob
= InternalGetHashInterface ();
77 if (HashInterfaceHob
== NULL
) {
78 return EFI_UNSUPPORTED
;
81 if (HashInterfaceHob
->HashInterfaceCount
== 0) {
82 return EFI_UNSUPPORTED
;
85 HashCtx
= AllocatePool (sizeof(*HashCtx
) * HashInterfaceHob
->HashInterfaceCount
);
86 ASSERT (HashCtx
!= NULL
);
88 for (Index
= 0; Index
< HashInterfaceHob
->HashInterfaceCount
; Index
++) {
89 HashInterfaceHob
->HashInterface
[Index
].HashInit (&HashCtx
[Index
]);
92 *HashHandle
= (HASH_HANDLE
)HashCtx
;
98 Update hash sequence data.
100 @param HashHandle Hash handle.
101 @param DataToHash Data to be hashed.
102 @param DataToHashLen Data size.
104 @retval EFI_SUCCESS Hash sequence updated.
109 IN HASH_HANDLE HashHandle
,
111 IN UINTN DataToHashLen
114 HASH_INTERFACE_HOB
*HashInterfaceHob
;
115 HASH_HANDLE
*HashCtx
;
118 HashInterfaceHob
= InternalGetHashInterface ();
119 if (HashInterfaceHob
== NULL
) {
120 return EFI_UNSUPPORTED
;
123 if (HashInterfaceHob
->HashInterfaceCount
== 0) {
124 return EFI_UNSUPPORTED
;
127 HashCtx
= (HASH_HANDLE
*)HashHandle
;
129 for (Index
= 0; Index
< HashInterfaceHob
->HashInterfaceCount
; Index
++) {
130 HashInterfaceHob
->HashInterface
[Index
].HashUpdate (HashCtx
[Index
], DataToHash
, DataToHashLen
);
137 Hash sequence complete and extend to PCR.
139 @param HashHandle Hash handle.
140 @param PcrIndex PCR to be extended.
141 @param DataToHash Data to be hashed.
142 @param DataToHashLen Data size.
143 @param DigestList Digest list.
145 @retval EFI_SUCCESS Hash sequence complete and DigestList is returned.
149 HashCompleteAndExtend (
150 IN HASH_HANDLE HashHandle
,
151 IN TPMI_DH_PCR PcrIndex
,
153 IN UINTN DataToHashLen
,
154 OUT TPML_DIGEST_VALUES
*DigestList
157 TPML_DIGEST_VALUES Digest
;
158 HASH_INTERFACE_HOB
*HashInterfaceHob
;
159 HASH_HANDLE
*HashCtx
;
163 HashInterfaceHob
= InternalGetHashInterface ();
164 if (HashInterfaceHob
== NULL
) {
165 return EFI_UNSUPPORTED
;
168 if (HashInterfaceHob
->HashInterfaceCount
== 0) {
169 return EFI_UNSUPPORTED
;
172 HashCtx
= (HASH_HANDLE
*)HashHandle
;
173 ZeroMem (DigestList
, sizeof(*DigestList
));
175 for (Index
= 0; Index
< HashInterfaceHob
->HashInterfaceCount
; Index
++) {
176 HashInterfaceHob
->HashInterface
[Index
].HashUpdate (HashCtx
[Index
], DataToHash
, DataToHashLen
);
177 HashInterfaceHob
->HashInterface
[Index
].HashFinal (HashCtx
[Index
], &Digest
);
178 Tpm2SetHashToDigestList (DigestList
, &Digest
);
183 Status
= Tpm2PcrExtend (
191 Hash data and extend to PCR.
193 @param PcrIndex PCR to be extended.
194 @param DataToHash Data to be hashed.
195 @param DataToHashLen Data size.
196 @param DigestList Digest list.
198 @retval EFI_SUCCESS Hash data and DigestList is returned.
203 IN TPMI_DH_PCR PcrIndex
,
205 IN UINTN DataToHashLen
,
206 OUT TPML_DIGEST_VALUES
*DigestList
209 HASH_INTERFACE_HOB
*HashInterfaceHob
;
210 HASH_HANDLE HashHandle
;
213 HashInterfaceHob
= InternalGetHashInterface ();
214 if (HashInterfaceHob
== NULL
) {
215 return EFI_UNSUPPORTED
;
218 if (HashInterfaceHob
->HashInterfaceCount
== 0) {
219 return EFI_UNSUPPORTED
;
222 HashStart (&HashHandle
);
223 HashUpdate (HashHandle
, DataToHash
, DataToHashLen
);
224 Status
= HashCompleteAndExtend (HashHandle
, PcrIndex
, NULL
, 0, DigestList
);
230 This service register Hash.
232 @param HashInterface Hash interface
234 @retval EFI_SUCCESS This hash interface is registered successfully.
235 @retval EFI_UNSUPPORTED System does not support register this interface.
236 @retval EFI_ALREADY_STARTED System already register this interface.
240 RegisterHashInterfaceLib (
241 IN HASH_INTERFACE
*HashInterface
245 HASH_INTERFACE_HOB
*HashInterfaceHob
;
246 HASH_INTERFACE_HOB LocalHashInterfaceHob
;
252 HashMask
= Tpm2GetHashMaskFromAlgo (&HashInterface
->HashGuid
);
253 if ((HashMask
& PcdGet32 (PcdTpm2HashMask
)) == 0) {
254 return EFI_UNSUPPORTED
;
257 HashInterfaceHob
= InternalGetHashInterface ();
258 if (HashInterfaceHob
== NULL
) {
259 ZeroMem (&LocalHashInterfaceHob
, sizeof(LocalHashInterfaceHob
));
260 HashInterfaceHob
= BuildGuidDataHob (&mHashLibPeiRouterGuid
, &LocalHashInterfaceHob
, sizeof(LocalHashInterfaceHob
));
261 if (HashInterfaceHob
== NULL
) {
262 return EFI_OUT_OF_RESOURCES
;
266 if (HashInterfaceHob
->HashInterfaceCount
>= HASH_COUNT
) {
267 return EFI_OUT_OF_RESOURCES
;
273 for (Index
= 0; Index
< HashInterfaceHob
->HashInterfaceCount
; Index
++) {
274 if (CompareGuid (&HashInterfaceHob
->HashInterface
[Index
].HashGuid
, &HashInterface
->HashGuid
)) {
276 // In PEI phase, there will be shadow driver dispatched again.
278 DEBUG ((EFI_D_INFO
, "RegisterHashInterfaceLib - Override\n"));
279 CopyMem (&HashInterfaceHob
->HashInterface
[Index
], HashInterface
, sizeof(*HashInterface
));
284 CopyMem (&HashInterfaceHob
->HashInterface
[HashInterfaceHob
->HashInterfaceCount
], HashInterface
, sizeof(*HashInterface
));
285 HashInterfaceHob
->HashInterfaceCount
++;