2 This 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 - 2018, 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
] = {{{0}, NULL
, NULL
, NULL
}};
23 UINTN mHashInterfaceCount
= 0;
25 UINT32 mSupportedHashMaskLast
= 0;
26 UINT32 mSupportedHashMaskCurrent
= 0;
29 Check mismatch of supported HashMask between modules
30 that may link different HashInstanceLib instances.
34 CheckSupportedHashMaskMismatch (
38 if (mSupportedHashMaskCurrent
!= mSupportedHashMaskLast
) {
41 "WARNING: There is mismatch of supported HashMask (0x%x - 0x%x) between modules\n",
42 mSupportedHashMaskCurrent
,
43 mSupportedHashMaskLast
45 DEBUG ((DEBUG_WARN
, "that are linking different HashInstanceLib instances!\n"));
52 @param HashHandle Hash handle.
54 @retval EFI_SUCCESS Hash sequence start and HandleHandle returned.
55 @retval EFI_OUT_OF_RESOURCES No enough resource to start hash.
60 OUT HASH_HANDLE
*HashHandle
67 if (mHashInterfaceCount
== 0) {
68 return EFI_UNSUPPORTED
;
71 CheckSupportedHashMaskMismatch ();
73 HashCtx
= AllocatePool (sizeof(*HashCtx
) * mHashInterfaceCount
);
74 ASSERT (HashCtx
!= NULL
);
76 for (Index
= 0; Index
< mHashInterfaceCount
; Index
++) {
77 HashMask
= Tpm2GetHashMaskFromAlgo (&mHashInterface
[Index
].HashGuid
);
78 if ((HashMask
& PcdGet32 (PcdTpm2HashMask
)) != 0) {
79 mHashInterface
[Index
].HashInit (&HashCtx
[Index
]);
83 *HashHandle
= (HASH_HANDLE
)HashCtx
;
89 Update hash sequence data.
91 @param HashHandle Hash handle.
92 @param DataToHash Data to be hashed.
93 @param DataToHashLen Data size.
95 @retval EFI_SUCCESS Hash sequence updated.
100 IN HASH_HANDLE HashHandle
,
102 IN UINTN DataToHashLen
105 HASH_HANDLE
*HashCtx
;
109 if (mHashInterfaceCount
== 0) {
110 return EFI_UNSUPPORTED
;
113 CheckSupportedHashMaskMismatch ();
115 HashCtx
= (HASH_HANDLE
*)HashHandle
;
117 for (Index
= 0; Index
< mHashInterfaceCount
; Index
++) {
118 HashMask
= Tpm2GetHashMaskFromAlgo (&mHashInterface
[Index
].HashGuid
);
119 if ((HashMask
& PcdGet32 (PcdTpm2HashMask
)) != 0) {
120 mHashInterface
[Index
].HashUpdate (HashCtx
[Index
], DataToHash
, DataToHashLen
);
128 Hash sequence complete and extend to PCR.
130 @param HashHandle Hash handle.
131 @param PcrIndex PCR to be extended.
132 @param DataToHash Data to be hashed.
133 @param DataToHashLen Data size.
134 @param DigestList Digest list.
136 @retval EFI_SUCCESS Hash sequence complete and DigestList is returned.
140 HashCompleteAndExtend (
141 IN HASH_HANDLE HashHandle
,
142 IN TPMI_DH_PCR PcrIndex
,
144 IN UINTN DataToHashLen
,
145 OUT TPML_DIGEST_VALUES
*DigestList
148 TPML_DIGEST_VALUES Digest
;
149 HASH_HANDLE
*HashCtx
;
154 if (mHashInterfaceCount
== 0) {
155 return EFI_UNSUPPORTED
;
158 CheckSupportedHashMaskMismatch ();
160 HashCtx
= (HASH_HANDLE
*)HashHandle
;
161 ZeroMem (DigestList
, sizeof(*DigestList
));
163 for (Index
= 0; Index
< mHashInterfaceCount
; Index
++) {
164 HashMask
= Tpm2GetHashMaskFromAlgo (&mHashInterface
[Index
].HashGuid
);
165 if ((HashMask
& PcdGet32 (PcdTpm2HashMask
)) != 0) {
166 mHashInterface
[Index
].HashUpdate (HashCtx
[Index
], DataToHash
, DataToHashLen
);
167 mHashInterface
[Index
].HashFinal (HashCtx
[Index
], &Digest
);
168 Tpm2SetHashToDigestList (DigestList
, &Digest
);
174 Status
= Tpm2PcrExtend (
182 Hash data and extend to PCR.
184 @param PcrIndex PCR to be extended.
185 @param DataToHash Data to be hashed.
186 @param DataToHashLen Data size.
187 @param DigestList Digest list.
189 @retval EFI_SUCCESS Hash data and DigestList is returned.
194 IN TPMI_DH_PCR PcrIndex
,
196 IN UINTN DataToHashLen
,
197 OUT TPML_DIGEST_VALUES
*DigestList
200 HASH_HANDLE HashHandle
;
203 if (mHashInterfaceCount
== 0) {
204 return EFI_UNSUPPORTED
;
207 CheckSupportedHashMaskMismatch ();
209 HashStart (&HashHandle
);
210 HashUpdate (HashHandle
, DataToHash
, DataToHashLen
);
211 Status
= HashCompleteAndExtend (HashHandle
, PcrIndex
, NULL
, 0, DigestList
);
217 This service register Hash.
219 @param HashInterface Hash interface
221 @retval EFI_SUCCESS This hash interface is registered successfully.
222 @retval EFI_UNSUPPORTED System does not support register this interface.
223 @retval EFI_ALREADY_STARTED System already register this interface.
227 RegisterHashInterfaceLib (
228 IN HASH_INTERFACE
*HashInterface
238 HashMask
= Tpm2GetHashMaskFromAlgo (&HashInterface
->HashGuid
);
239 if ((HashMask
& PcdGet32 (PcdTpm2HashMask
)) == 0) {
240 return EFI_UNSUPPORTED
;
243 if (mHashInterfaceCount
>= sizeof(mHashInterface
)/sizeof(mHashInterface
[0])) {
244 return EFI_OUT_OF_RESOURCES
;
250 for (Index
= 0; Index
< mHashInterfaceCount
; Index
++) {
251 if (CompareGuid (&mHashInterface
[Index
].HashGuid
, &HashInterface
->HashGuid
)) {
252 DEBUG ((DEBUG_ERROR
, "Hash Interface (%g) has been registered\n", &HashInterface
->HashGuid
));
253 return EFI_ALREADY_STARTED
;
258 // Record hash algorithm bitmap of CURRENT module which consumes HashLib.
260 mSupportedHashMaskCurrent
= PcdGet32 (PcdTcg2HashAlgorithmBitmap
) | HashMask
;
261 Status
= PcdSet32S (PcdTcg2HashAlgorithmBitmap
, mSupportedHashMaskCurrent
);
262 ASSERT_EFI_ERROR (Status
);
264 CopyMem (&mHashInterface
[mHashInterfaceCount
], HashInterface
, sizeof(*HashInterface
));
265 mHashInterfaceCount
++;
271 The constructor function of HashLibBaseCryptoRouterDxe.
273 @param ImageHandle The firmware allocated handle for the EFI image.
274 @param SystemTable A pointer to the EFI System Table.
276 @retval EFI_SUCCESS The constructor executed correctly.
281 HashLibBaseCryptoRouterDxeConstructor (
282 IN EFI_HANDLE ImageHandle
,
283 IN EFI_SYSTEM_TABLE
*SystemTable
289 // Record hash algorithm bitmap of LAST module which also consumes HashLib.
291 mSupportedHashMaskLast
= PcdGet32 (PcdTcg2HashAlgorithmBitmap
);
294 // Set PcdTcg2HashAlgorithmBitmap to 0 in CONSTRUCTOR for CURRENT module.
296 Status
= PcdSet32S (PcdTcg2HashAlgorithmBitmap
, 0);
297 ASSERT_EFI_ERROR (Status
);