2 This library is HashLib for Tdx.
4 Copyright (c) 2021 - 2022, Intel Corporation. All rights reserved. <BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include <Library/BaseLib.h>
11 #include <Library/BaseMemoryLib.h>
12 #include <Library/DebugLib.h>
13 #include <Library/PcdLib.h>
14 #include <Library/HashLib.h>
15 #include <Library/TdxLib.h>
16 #include <Protocol/CcMeasurement.h>
18 EFI_GUID mSha384Guid
= HASH_ALGORITHM_SHA384_GUID
;
21 // Currently TDX supports SHA384.
23 HASH_INTERFACE mHashInterface
= {
24 { 0 }, NULL
, NULL
, NULL
27 UINTN mHashInterfaceCount
= 0;
32 @param HashHandle Hash handle.
34 @retval EFI_SUCCESS Hash sequence start and HandleHandle returned.
35 @retval EFI_OUT_OF_RESOURCES No enough resource to start hash.
40 OUT HASH_HANDLE
*HashHandle
45 if (mHashInterfaceCount
== 0) {
47 return EFI_UNSUPPORTED
;
51 mHashInterface
.HashInit (&HashCtx
);
53 *HashHandle
= HashCtx
;
59 Update hash sequence data.
61 @param HashHandle Hash handle.
62 @param DataToHash Data to be hashed.
63 @param DataToHashLen Data size.
65 @retval EFI_SUCCESS Hash sequence updated.
70 IN HASH_HANDLE HashHandle
,
72 IN UINTN DataToHashLen
75 if (mHashInterfaceCount
== 0) {
77 return EFI_UNSUPPORTED
;
80 mHashInterface
.HashUpdate (HashHandle
, DataToHash
, DataToHashLen
);
86 Hash sequence complete and extend to PCR.
88 @param HashHandle Hash handle.
89 @param PcrIndex PCR to be extended.
90 @param DataToHash Data to be hashed.
91 @param DataToHashLen Data size.
92 @param DigestList Digest list.
94 @retval EFI_SUCCESS Hash sequence complete and DigestList is returned.
98 HashCompleteAndExtend (
99 IN HASH_HANDLE HashHandle
,
100 IN TPMI_DH_PCR PcrIndex
,
102 IN UINTN DataToHashLen
,
103 OUT TPML_DIGEST_VALUES
*DigestList
106 TPML_DIGEST_VALUES Digest
;
109 if (mHashInterfaceCount
== 0) {
111 return EFI_UNSUPPORTED
;
114 ZeroMem (DigestList
, sizeof (*DigestList
));
116 mHashInterface
.HashUpdate (HashHandle
, DataToHash
, DataToHashLen
);
117 mHashInterface
.HashFinal (HashHandle
, &Digest
);
120 &DigestList
->digests
[0],
122 sizeof (Digest
.digests
[0])
126 ASSERT (DigestList
->count
== 1 && DigestList
->digests
[0].hashAlg
== TPM_ALG_SHA384
);
128 Status
= TdExtendRtmr (
129 (UINT32
*)DigestList
->digests
[0].digest
.sha384
,
134 ASSERT (!EFI_ERROR (Status
));
139 Hash data and extend to RTMR.
141 @param PcrIndex PCR to be extended.
142 @param DataToHash Data to be hashed.
143 @param DataToHashLen Data size.
144 @param DigestList Digest list.
146 @retval EFI_SUCCESS Hash data and DigestList is returned.
151 IN TPMI_DH_PCR PcrIndex
,
153 IN UINTN DataToHashLen
,
154 OUT TPML_DIGEST_VALUES
*DigestList
157 HASH_HANDLE HashHandle
;
160 if (mHashInterfaceCount
== 0) {
162 return EFI_UNSUPPORTED
;
165 ASSERT (TdIsEnabled ());
167 HashStart (&HashHandle
);
168 HashUpdate (HashHandle
, DataToHash
, DataToHashLen
);
169 Status
= HashCompleteAndExtend (HashHandle
, PcrIndex
, NULL
, 0, DigestList
);
175 This service register Hash.
177 @param HashInterface Hash interface
179 @retval EFI_SUCCESS This hash interface is registered successfully.
180 @retval EFI_UNSUPPORTED System does not support register this interface.
181 @retval EFI_ALREADY_STARTED System already register this interface.
185 RegisterHashInterfaceLib (
186 IN HASH_INTERFACE
*HashInterface
189 ASSERT (TdIsEnabled ());
192 // Only SHA384 is allowed.
194 if (!CompareGuid (&mSha384Guid
, &HashInterface
->HashGuid
)) {
195 return EFI_UNSUPPORTED
;
198 if (mHashInterfaceCount
!= 0) {
200 return EFI_OUT_OF_RESOURCES
;
203 CopyMem (&mHashInterface
, HashInterface
, sizeof (*HashInterface
));
204 mHashInterfaceCount
++;