2 This library is BaseCrypto router. It will redirect hash request to each individual
3 hash handler registered, such as SHA1, SHA256.
4 Platform can use PcdTpm2HashMask to mask some hash engines.
6 Copyright (c) 2013 - 2021, Intel Corporation. All rights reserved. <BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
12 #include <Library/BaseLib.h>
13 #include <Library/BaseMemoryLib.h>
14 #include <Library/Tpm2CommandLib.h>
15 #include <Library/DebugLib.h>
16 #include <Library/MemoryAllocationLib.h>
17 #include <Library/PcdLib.h>
18 #include <Library/HashLib.h>
20 #include "HashLibBaseCryptoRouterCommon.h"
22 HASH_INTERFACE mHashInterface
[HASH_COUNT
] = {
24 { 0 }, NULL
, NULL
, NULL
27 UINTN mHashInterfaceCount
= 0;
29 UINT32 mSupportedHashMaskLast
= 0;
30 UINT32 mSupportedHashMaskCurrent
= 0;
33 Check mismatch of supported HashMask between modules
34 that may link different HashInstanceLib instances.
38 CheckSupportedHashMaskMismatch (
42 if (mSupportedHashMaskCurrent
!= mSupportedHashMaskLast
) {
45 "WARNING: There is mismatch of supported HashMask (0x%x - 0x%x) between modules\n",
46 mSupportedHashMaskCurrent
,
47 mSupportedHashMaskLast
49 DEBUG ((DEBUG_WARN
, "that are linking different HashInstanceLib instances!\n"));
56 @param HashHandle Hash handle.
58 @retval EFI_SUCCESS Hash sequence start and HandleHandle returned.
59 @retval EFI_OUT_OF_RESOURCES No enough resource to start hash.
64 OUT HASH_HANDLE
*HashHandle
71 if (mHashInterfaceCount
== 0) {
72 return EFI_UNSUPPORTED
;
75 CheckSupportedHashMaskMismatch ();
77 HashCtx
= AllocatePool (sizeof (*HashCtx
) * mHashInterfaceCount
);
78 ASSERT (HashCtx
!= NULL
);
80 for (Index
= 0; Index
< mHashInterfaceCount
; Index
++) {
81 HashMask
= Tpm2GetHashMaskFromAlgo (&mHashInterface
[Index
].HashGuid
);
82 if ((HashMask
& PcdGet32 (PcdTpm2HashMask
)) != 0) {
83 mHashInterface
[Index
].HashInit (&HashCtx
[Index
]);
87 *HashHandle
= (HASH_HANDLE
)HashCtx
;
93 Update hash sequence data.
95 @param HashHandle Hash handle.
96 @param DataToHash Data to be hashed.
97 @param DataToHashLen Data size.
99 @retval EFI_SUCCESS Hash sequence updated.
104 IN HASH_HANDLE HashHandle
,
106 IN UINTN DataToHashLen
109 HASH_HANDLE
*HashCtx
;
113 if (mHashInterfaceCount
== 0) {
114 return EFI_UNSUPPORTED
;
117 CheckSupportedHashMaskMismatch ();
119 HashCtx
= (HASH_HANDLE
*)HashHandle
;
121 for (Index
= 0; Index
< mHashInterfaceCount
; Index
++) {
122 HashMask
= Tpm2GetHashMaskFromAlgo (&mHashInterface
[Index
].HashGuid
);
123 if ((HashMask
& PcdGet32 (PcdTpm2HashMask
)) != 0) {
124 mHashInterface
[Index
].HashUpdate (HashCtx
[Index
], DataToHash
, DataToHashLen
);
132 Hash sequence complete and extend to PCR.
134 @param HashHandle Hash handle.
135 @param PcrIndex PCR to be extended.
136 @param DataToHash Data to be hashed.
137 @param DataToHashLen Data size.
138 @param DigestList Digest list.
140 @retval EFI_SUCCESS Hash sequence complete and DigestList is returned.
144 HashCompleteAndExtend (
145 IN HASH_HANDLE HashHandle
,
146 IN TPMI_DH_PCR PcrIndex
,
148 IN UINTN DataToHashLen
,
149 OUT TPML_DIGEST_VALUES
*DigestList
152 TPML_DIGEST_VALUES Digest
;
153 HASH_HANDLE
*HashCtx
;
158 if (mHashInterfaceCount
== 0) {
159 return EFI_UNSUPPORTED
;
162 CheckSupportedHashMaskMismatch ();
164 HashCtx
= (HASH_HANDLE
*)HashHandle
;
165 ZeroMem (DigestList
, sizeof (*DigestList
));
167 for (Index
= 0; Index
< mHashInterfaceCount
; Index
++) {
168 HashMask
= Tpm2GetHashMaskFromAlgo (&mHashInterface
[Index
].HashGuid
);
169 if ((HashMask
& PcdGet32 (PcdTpm2HashMask
)) != 0) {
170 mHashInterface
[Index
].HashUpdate (HashCtx
[Index
], DataToHash
, DataToHashLen
);
171 mHashInterface
[Index
].HashFinal (HashCtx
[Index
], &Digest
);
172 Tpm2SetHashToDigestList (DigestList
, &Digest
);
178 Status
= Tpm2PcrExtend (
186 Hash data and extend to PCR.
188 @param PcrIndex PCR to be extended.
189 @param DataToHash Data to be hashed.
190 @param DataToHashLen Data size.
191 @param DigestList Digest list.
193 @retval EFI_SUCCESS Hash data and DigestList is returned.
198 IN TPMI_DH_PCR PcrIndex
,
200 IN UINTN DataToHashLen
,
201 OUT TPML_DIGEST_VALUES
*DigestList
204 HASH_HANDLE HashHandle
;
207 if (mHashInterfaceCount
== 0) {
208 return EFI_UNSUPPORTED
;
211 CheckSupportedHashMaskMismatch ();
213 HashStart (&HashHandle
);
214 HashUpdate (HashHandle
, DataToHash
, DataToHashLen
);
215 Status
= HashCompleteAndExtend (HashHandle
, PcrIndex
, NULL
, 0, DigestList
);
221 This service register Hash.
223 @param HashInterface Hash interface
225 @retval EFI_SUCCESS This hash interface is registered successfully.
226 @retval EFI_UNSUPPORTED System does not support register this interface.
227 @retval EFI_ALREADY_STARTED System already register this interface.
231 RegisterHashInterfaceLib (
232 IN HASH_INTERFACE
*HashInterface
243 HashMask
= Tpm2GetHashMaskFromAlgo (&HashInterface
->HashGuid
);
244 Tpm2HashMask
= PcdGet32 (PcdTpm2HashMask
);
246 if ((Tpm2HashMask
!= 0) &&
247 ((HashMask
& Tpm2HashMask
) == 0))
249 return EFI_UNSUPPORTED
;
252 if (mHashInterfaceCount
>= sizeof (mHashInterface
)/sizeof (mHashInterface
[0])) {
253 return EFI_OUT_OF_RESOURCES
;
259 for (Index
= 0; Index
< mHashInterfaceCount
; Index
++) {
260 if (CompareGuid (&mHashInterface
[Index
].HashGuid
, &HashInterface
->HashGuid
)) {
261 DEBUG ((DEBUG_ERROR
, "Hash Interface (%g) has been registered\n", &HashInterface
->HashGuid
));
262 return EFI_ALREADY_STARTED
;
267 // Record hash algorithm bitmap of CURRENT module which consumes HashLib.
269 mSupportedHashMaskCurrent
= PcdGet32 (PcdTcg2HashAlgorithmBitmap
) | HashMask
;
270 Status
= PcdSet32S (PcdTcg2HashAlgorithmBitmap
, mSupportedHashMaskCurrent
);
271 ASSERT_EFI_ERROR (Status
);
273 CopyMem (&mHashInterface
[mHashInterfaceCount
], HashInterface
, sizeof (*HashInterface
));
274 mHashInterfaceCount
++;
280 The constructor function of HashLibBaseCryptoRouterDxe.
282 @param ImageHandle The firmware allocated handle for the EFI image.
283 @param SystemTable A pointer to the EFI System Table.
285 @retval EFI_SUCCESS The constructor executed correctly.
290 HashLibBaseCryptoRouterDxeConstructor (
291 IN EFI_HANDLE ImageHandle
,
292 IN EFI_SYSTEM_TABLE
*SystemTable
298 // Record hash algorithm bitmap of LAST module which also consumes HashLib.
300 mSupportedHashMaskLast
= PcdGet32 (PcdTcg2HashAlgorithmBitmap
);
303 // Set PcdTcg2HashAlgorithmBitmap to 0 in CONSTRUCTOR for CURRENT module.
305 Status
= PcdSet32S (PcdTcg2HashAlgorithmBitmap
, 0);
306 ASSERT_EFI_ERROR (Status
);