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 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/HashLib.h>
26 #include "HashLibBaseCryptoRouterCommon.h"
28 HASH_INTERFACE mHashInterface
[HASH_COUNT
] = {{{0}, NULL
, NULL
, NULL
}};
29 UINTN mHashInterfaceCount
= 0;
31 UINT32 mSupportedHashMaskLast
= 0;
32 UINT32 mSupportedHashMaskCurrent
= 0;
35 Check mismatch of supported HashMask between modules
36 that may link different HashInstanceLib instances.
40 CheckSupportedHashMaskMismatch (
44 if (mSupportedHashMaskCurrent
!= mSupportedHashMaskLast
) {
47 "WARNING: There is mismatch of supported HashMask (0x%x - 0x%x) between modules\n",
48 mSupportedHashMaskCurrent
,
49 mSupportedHashMaskLast
51 DEBUG ((DEBUG_WARN
, "that are linking different HashInstanceLib instances!\n"));
58 @param HashHandle Hash handle.
60 @retval EFI_SUCCESS Hash sequence start and HandleHandle returned.
61 @retval EFI_OUT_OF_RESOURCES No enough resource to start hash.
66 OUT HASH_HANDLE
*HashHandle
73 if (mHashInterfaceCount
== 0) {
74 return EFI_UNSUPPORTED
;
77 CheckSupportedHashMaskMismatch ();
79 HashCtx
= AllocatePool (sizeof(*HashCtx
) * mHashInterfaceCount
);
80 ASSERT (HashCtx
!= NULL
);
82 for (Index
= 0; Index
< mHashInterfaceCount
; Index
++) {
83 HashMask
= Tpm2GetHashMaskFromAlgo (&mHashInterface
[Index
].HashGuid
);
84 if ((HashMask
& PcdGet32 (PcdTpm2HashMask
)) != 0) {
85 mHashInterface
[Index
].HashInit (&HashCtx
[Index
]);
89 *HashHandle
= (HASH_HANDLE
)HashCtx
;
95 Update hash sequence data.
97 @param HashHandle Hash handle.
98 @param DataToHash Data to be hashed.
99 @param DataToHashLen Data size.
101 @retval EFI_SUCCESS Hash sequence updated.
106 IN HASH_HANDLE HashHandle
,
108 IN UINTN DataToHashLen
111 HASH_HANDLE
*HashCtx
;
115 if (mHashInterfaceCount
== 0) {
116 return EFI_UNSUPPORTED
;
119 CheckSupportedHashMaskMismatch ();
121 HashCtx
= (HASH_HANDLE
*)HashHandle
;
123 for (Index
= 0; Index
< mHashInterfaceCount
; Index
++) {
124 HashMask
= Tpm2GetHashMaskFromAlgo (&mHashInterface
[Index
].HashGuid
);
125 if ((HashMask
& PcdGet32 (PcdTpm2HashMask
)) != 0) {
126 mHashInterface
[Index
].HashUpdate (HashCtx
[Index
], DataToHash
, DataToHashLen
);
134 Hash sequence complete and extend to PCR.
136 @param HashHandle Hash handle.
137 @param PcrIndex PCR to be extended.
138 @param DataToHash Data to be hashed.
139 @param DataToHashLen Data size.
140 @param DigestList Digest list.
142 @retval EFI_SUCCESS Hash sequence complete and DigestList is returned.
146 HashCompleteAndExtend (
147 IN HASH_HANDLE HashHandle
,
148 IN TPMI_DH_PCR PcrIndex
,
150 IN UINTN DataToHashLen
,
151 OUT TPML_DIGEST_VALUES
*DigestList
154 TPML_DIGEST_VALUES Digest
;
155 HASH_HANDLE
*HashCtx
;
160 if (mHashInterfaceCount
== 0) {
161 return EFI_UNSUPPORTED
;
164 CheckSupportedHashMaskMismatch ();
166 HashCtx
= (HASH_HANDLE
*)HashHandle
;
167 ZeroMem (DigestList
, sizeof(*DigestList
));
169 for (Index
= 0; Index
< mHashInterfaceCount
; Index
++) {
170 HashMask
= Tpm2GetHashMaskFromAlgo (&mHashInterface
[Index
].HashGuid
);
171 if ((HashMask
& PcdGet32 (PcdTpm2HashMask
)) != 0) {
172 mHashInterface
[Index
].HashUpdate (HashCtx
[Index
], DataToHash
, DataToHashLen
);
173 mHashInterface
[Index
].HashFinal (HashCtx
[Index
], &Digest
);
174 Tpm2SetHashToDigestList (DigestList
, &Digest
);
180 Status
= Tpm2PcrExtend (
188 Hash data and extend to PCR.
190 @param PcrIndex PCR to be extended.
191 @param DataToHash Data to be hashed.
192 @param DataToHashLen Data size.
193 @param DigestList Digest list.
195 @retval EFI_SUCCESS Hash data and DigestList is returned.
200 IN TPMI_DH_PCR PcrIndex
,
202 IN UINTN DataToHashLen
,
203 OUT TPML_DIGEST_VALUES
*DigestList
206 HASH_HANDLE HashHandle
;
209 if (mHashInterfaceCount
== 0) {
210 return EFI_UNSUPPORTED
;
213 CheckSupportedHashMaskMismatch ();
215 HashStart (&HashHandle
);
216 HashUpdate (HashHandle
, DataToHash
, DataToHashLen
);
217 Status
= HashCompleteAndExtend (HashHandle
, PcrIndex
, NULL
, 0, DigestList
);
223 This service register Hash.
225 @param HashInterface Hash interface
227 @retval EFI_SUCCESS This hash interface is registered successfully.
228 @retval EFI_UNSUPPORTED System does not support register this interface.
229 @retval EFI_ALREADY_STARTED System already register this interface.
233 RegisterHashInterfaceLib (
234 IN HASH_INTERFACE
*HashInterface
244 HashMask
= Tpm2GetHashMaskFromAlgo (&HashInterface
->HashGuid
);
245 if ((HashMask
& PcdGet32 (PcdTpm2HashMask
)) == 0) {
246 return EFI_UNSUPPORTED
;
249 if (mHashInterfaceCount
>= sizeof(mHashInterface
)/sizeof(mHashInterface
[0])) {
250 return EFI_OUT_OF_RESOURCES
;
256 for (Index
= 0; Index
< mHashInterfaceCount
; Index
++) {
257 if (CompareGuid (&mHashInterface
[Index
].HashGuid
, &HashInterface
->HashGuid
)) {
258 DEBUG ((DEBUG_ERROR
, "Hash Interface (%g) has been registered\n", &HashInterface
->HashGuid
));
259 return EFI_ALREADY_STARTED
;
264 // Record hash algorithm bitmap of CURRENT module which consumes HashLib.
266 mSupportedHashMaskCurrent
= PcdGet32 (PcdTcg2HashAlgorithmBitmap
) | HashMask
;
267 Status
= PcdSet32S (PcdTcg2HashAlgorithmBitmap
, mSupportedHashMaskCurrent
);
268 ASSERT_EFI_ERROR (Status
);
270 CopyMem (&mHashInterface
[mHashInterfaceCount
], HashInterface
, sizeof(*HashInterface
));
271 mHashInterfaceCount
++;
277 The constructor function of HashLibBaseCryptoRouterDxe.
279 @param ImageHandle The firmware allocated handle for the EFI image.
280 @param SystemTable A pointer to the EFI System Table.
282 @retval EFI_SUCCESS The constructor executed correctly.
287 HashLibBaseCryptoRouterDxeConstructor (
288 IN EFI_HANDLE ImageHandle
,
289 IN EFI_SYSTEM_TABLE
*SystemTable
295 // Record hash algorithm bitmap of LAST module which also consumes HashLib.
297 mSupportedHashMaskLast
= PcdGet32 (PcdTcg2HashAlgorithmBitmap
);
300 // Set PcdTcg2HashAlgorithmBitmap to 0 in CONSTRUCTOR for CURRENT module.
302 Status
= PcdSet32S (PcdTcg2HashAlgorithmBitmap
, 0);
303 ASSERT_EFI_ERROR (Status
);