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 - 2015, 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
;
77 HashInterfaceHob
= InternalGetHashInterface ();
78 if (HashInterfaceHob
== NULL
) {
79 return EFI_UNSUPPORTED
;
82 if (HashInterfaceHob
->HashInterfaceCount
== 0) {
83 return EFI_UNSUPPORTED
;
86 HashCtx
= AllocatePool (sizeof(*HashCtx
) * HashInterfaceHob
->HashInterfaceCount
);
87 ASSERT (HashCtx
!= NULL
);
89 for (Index
= 0; Index
< HashInterfaceHob
->HashInterfaceCount
; Index
++) {
90 HashMask
= Tpm2GetHashMaskFromAlgo (&HashInterfaceHob
->HashInterface
[Index
].HashGuid
);
91 if ((HashMask
& PcdGet32 (PcdTpm2HashMask
)) != 0) {
92 HashInterfaceHob
->HashInterface
[Index
].HashInit (&HashCtx
[Index
]);
96 *HashHandle
= (HASH_HANDLE
)HashCtx
;
102 Update hash sequence data.
104 @param HashHandle Hash handle.
105 @param DataToHash Data to be hashed.
106 @param DataToHashLen Data size.
108 @retval EFI_SUCCESS Hash sequence updated.
113 IN HASH_HANDLE HashHandle
,
115 IN UINTN DataToHashLen
118 HASH_INTERFACE_HOB
*HashInterfaceHob
;
119 HASH_HANDLE
*HashCtx
;
123 HashInterfaceHob
= InternalGetHashInterface ();
124 if (HashInterfaceHob
== NULL
) {
125 return EFI_UNSUPPORTED
;
128 if (HashInterfaceHob
->HashInterfaceCount
== 0) {
129 return EFI_UNSUPPORTED
;
132 HashCtx
= (HASH_HANDLE
*)HashHandle
;
134 for (Index
= 0; Index
< HashInterfaceHob
->HashInterfaceCount
; Index
++) {
135 HashMask
= Tpm2GetHashMaskFromAlgo (&HashInterfaceHob
->HashInterface
[Index
].HashGuid
);
136 if ((HashMask
& PcdGet32 (PcdTpm2HashMask
)) != 0) {
137 HashInterfaceHob
->HashInterface
[Index
].HashUpdate (HashCtx
[Index
], DataToHash
, DataToHashLen
);
145 Hash sequence complete and extend to PCR.
147 @param HashHandle Hash handle.
148 @param PcrIndex PCR to be extended.
149 @param DataToHash Data to be hashed.
150 @param DataToHashLen Data size.
151 @param DigestList Digest list.
153 @retval EFI_SUCCESS Hash sequence complete and DigestList is returned.
157 HashCompleteAndExtend (
158 IN HASH_HANDLE HashHandle
,
159 IN TPMI_DH_PCR PcrIndex
,
161 IN UINTN DataToHashLen
,
162 OUT TPML_DIGEST_VALUES
*DigestList
165 TPML_DIGEST_VALUES Digest
;
166 HASH_INTERFACE_HOB
*HashInterfaceHob
;
167 HASH_HANDLE
*HashCtx
;
172 HashInterfaceHob
= InternalGetHashInterface ();
173 if (HashInterfaceHob
== NULL
) {
174 return EFI_UNSUPPORTED
;
177 if (HashInterfaceHob
->HashInterfaceCount
== 0) {
178 return EFI_UNSUPPORTED
;
181 HashCtx
= (HASH_HANDLE
*)HashHandle
;
182 ZeroMem (DigestList
, sizeof(*DigestList
));
184 for (Index
= 0; Index
< HashInterfaceHob
->HashInterfaceCount
; Index
++) {
185 HashMask
= Tpm2GetHashMaskFromAlgo (&HashInterfaceHob
->HashInterface
[Index
].HashGuid
);
186 if ((HashMask
& PcdGet32 (PcdTpm2HashMask
)) != 0) {
187 HashInterfaceHob
->HashInterface
[Index
].HashUpdate (HashCtx
[Index
], DataToHash
, DataToHashLen
);
188 HashInterfaceHob
->HashInterface
[Index
].HashFinal (HashCtx
[Index
], &Digest
);
189 Tpm2SetHashToDigestList (DigestList
, &Digest
);
195 Status
= Tpm2PcrExtend (
203 Hash data and extend to PCR.
205 @param PcrIndex PCR to be extended.
206 @param DataToHash Data to be hashed.
207 @param DataToHashLen Data size.
208 @param DigestList Digest list.
210 @retval EFI_SUCCESS Hash data and DigestList is returned.
215 IN TPMI_DH_PCR PcrIndex
,
217 IN UINTN DataToHashLen
,
218 OUT TPML_DIGEST_VALUES
*DigestList
221 HASH_INTERFACE_HOB
*HashInterfaceHob
;
222 HASH_HANDLE HashHandle
;
225 HashInterfaceHob
= InternalGetHashInterface ();
226 if (HashInterfaceHob
== NULL
) {
227 return EFI_UNSUPPORTED
;
230 if (HashInterfaceHob
->HashInterfaceCount
== 0) {
231 return EFI_UNSUPPORTED
;
234 HashStart (&HashHandle
);
235 HashUpdate (HashHandle
, DataToHash
, DataToHashLen
);
236 Status
= HashCompleteAndExtend (HashHandle
, PcrIndex
, NULL
, 0, DigestList
);
242 This service register Hash.
244 @param HashInterface Hash interface
246 @retval EFI_SUCCESS This hash interface is registered successfully.
247 @retval EFI_UNSUPPORTED System does not support register this interface.
248 @retval EFI_ALREADY_STARTED System already register this interface.
252 RegisterHashInterfaceLib (
253 IN HASH_INTERFACE
*HashInterface
257 HASH_INTERFACE_HOB
*HashInterfaceHob
;
258 HASH_INTERFACE_HOB LocalHashInterfaceHob
;
260 UINT32 BiosSupportedHashMask
;
265 HashMask
= Tpm2GetHashMaskFromAlgo (&HashInterface
->HashGuid
);
266 if ((HashMask
& PcdGet32 (PcdTpm2HashMask
)) == 0) {
267 return EFI_UNSUPPORTED
;
270 HashInterfaceHob
= InternalGetHashInterface ();
271 if (HashInterfaceHob
== NULL
) {
272 ZeroMem (&LocalHashInterfaceHob
, sizeof(LocalHashInterfaceHob
));
273 HashInterfaceHob
= BuildGuidDataHob (&mHashLibPeiRouterGuid
, &LocalHashInterfaceHob
, sizeof(LocalHashInterfaceHob
));
274 if (HashInterfaceHob
== NULL
) {
275 return EFI_OUT_OF_RESOURCES
;
279 if (HashInterfaceHob
->HashInterfaceCount
>= HASH_COUNT
) {
280 return EFI_OUT_OF_RESOURCES
;
282 BiosSupportedHashMask
= PcdGet32 (PcdTcg2HashAlgorithmBitmap
);
283 PcdSet32 (PcdTcg2HashAlgorithmBitmap
, BiosSupportedHashMask
| HashMask
);
288 for (Index
= 0; Index
< HashInterfaceHob
->HashInterfaceCount
; Index
++) {
289 if (CompareGuid (&HashInterfaceHob
->HashInterface
[Index
].HashGuid
, &HashInterface
->HashGuid
)) {
291 // In PEI phase, there will be shadow driver dispatched again.
293 DEBUG ((EFI_D_INFO
, "RegisterHashInterfaceLib - Override\n"));
294 CopyMem (&HashInterfaceHob
->HashInterface
[Index
], HashInterface
, sizeof(*HashInterface
));
299 CopyMem (&HashInterfaceHob
->HashInterface
[HashInterfaceHob
->HashInterfaceCount
], HashInterface
, sizeof(*HashInterface
));
300 HashInterfaceHob
->HashInterfaceCount
++;