TianoCore EDK2 master
Loading...
Searching...
No Matches
HashLibTdx.c
Go to the documentation of this file.
1
9#include <PiPei.h>
10#include <Library/BaseLib.h>
12#include <Library/DebugLib.h>
13#include <Library/PcdLib.h>
14#include <Library/HashLib.h>
15#include <Library/TdxLib.h>
17
18EFI_GUID mSha384Guid = HASH_ALGORITHM_SHA384_GUID;
19
20//
21// Currently TDX supports SHA384.
22//
23HASH_INTERFACE mHashInterface = {
24 { 0 }, NULL, NULL, NULL
25};
26
27UINTN mHashInterfaceCount = 0;
28
38EFIAPI
40 OUT HASH_HANDLE *HashHandle
41 )
42{
43 HASH_HANDLE HashCtx;
44
45 if (mHashInterfaceCount == 0) {
46 ASSERT (FALSE);
47 return EFI_UNSUPPORTED;
48 }
49
50 HashCtx = 0;
51 mHashInterface.HashInit (&HashCtx);
52
53 *HashHandle = HashCtx;
54
55 return EFI_SUCCESS;
56}
57
68EFIAPI
70 IN HASH_HANDLE HashHandle,
71 IN VOID *DataToHash,
72 IN UINTN DataToHashLen
73 )
74{
75 if (mHashInterfaceCount == 0) {
76 ASSERT (FALSE);
77 return EFI_UNSUPPORTED;
78 }
79
80 mHashInterface.HashUpdate (HashHandle, DataToHash, DataToHashLen);
81
82 return EFI_SUCCESS;
83}
84
97EFIAPI
99 IN HASH_HANDLE HashHandle,
100 IN TPMI_DH_PCR PcrIndex,
101 IN VOID *DataToHash,
102 IN UINTN DataToHashLen,
103 OUT TPML_DIGEST_VALUES *DigestList
104 )
105{
106 TPML_DIGEST_VALUES Digest;
107 EFI_STATUS Status;
108
109 if (mHashInterfaceCount == 0) {
110 ASSERT (FALSE);
111 return EFI_UNSUPPORTED;
112 }
113
114 ZeroMem (DigestList, sizeof (*DigestList));
115
116 mHashInterface.HashUpdate (HashHandle, DataToHash, DataToHashLen);
117 mHashInterface.HashFinal (HashHandle, &Digest);
118
119 CopyMem (
120 &DigestList->digests[0],
121 &Digest.digests[0],
122 sizeof (Digest.digests[0])
123 );
124 DigestList->count++;
125
126 ASSERT (DigestList->count == 1 && DigestList->digests[0].hashAlg == TPM_ALG_SHA384);
127
128 Status = TdExtendRtmr (
129 (UINT32 *)DigestList->digests[0].digest.sha384,
131 (UINT8)PcrIndex
132 );
133
134 ASSERT (!EFI_ERROR (Status));
135 return Status;
136}
137
149EFIAPI
151 IN TPMI_DH_PCR PcrIndex,
152 IN VOID *DataToHash,
153 IN UINTN DataToHashLen,
154 OUT TPML_DIGEST_VALUES *DigestList
155 )
156{
157 HASH_HANDLE HashHandle;
158 EFI_STATUS Status;
159
160 if (mHashInterfaceCount == 0) {
161 ASSERT (FALSE);
162 return EFI_UNSUPPORTED;
163 }
164
165 ASSERT (TdIsEnabled ());
166
167 HashStart (&HashHandle);
168 HashUpdate (HashHandle, DataToHash, DataToHashLen);
169 Status = HashCompleteAndExtend (HashHandle, PcrIndex, NULL, 0, DigestList);
170
171 return Status;
172}
173
184EFIAPI
186 IN HASH_INTERFACE *HashInterface
187 )
188{
189 //
190 // HashLibTdx is designed for Tdx guest. So if it is not Tdx guest,
191 // return EFI_UNSUPPORTED.
192 //
193 if (!TdIsEnabled ()) {
194 return EFI_UNSUPPORTED;
195 }
196
197 //
198 // Only SHA384 is allowed.
199 //
200 if (!CompareGuid (&mSha384Guid, &HashInterface->HashGuid)) {
201 return EFI_UNSUPPORTED;
202 }
203
204 if (mHashInterfaceCount != 0) {
205 ASSERT (FALSE);
206 return EFI_OUT_OF_RESOURCES;
207 }
208
209 CopyMem (&mHashInterface, HashInterface, sizeof (*HashInterface));
210 mHashInterfaceCount++;
211
212 return EFI_SUCCESS;
213}
UINT64 UINTN
#define SHA384_DIGEST_SIZE
Definition: BaseCryptLib.h:49
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
BOOLEAN EFIAPI CompareGuid(IN CONST GUID *Guid1, IN CONST GUID *Guid2)
Definition: MemLibGuid.c:73
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
EFI_STATUS EFIAPI HashStart(OUT HASH_HANDLE *HashHandle)
Definition: HashLibTdx.c:39
EFI_STATUS EFIAPI RegisterHashInterfaceLib(IN HASH_INTERFACE *HashInterface)
Definition: HashLibTdx.c:185
EFI_STATUS EFIAPI HashUpdate(IN HASH_HANDLE HashHandle, IN VOID *DataToHash, IN UINTN DataToHashLen)
Definition: HashLibTdx.c:69
EFI_STATUS EFIAPI HashAndExtend(IN TPMI_DH_PCR PcrIndex, IN VOID *DataToHash, IN UINTN DataToHashLen, OUT TPML_DIGEST_VALUES *DigestList)
Definition: HashLibTdx.c:150
EFI_STATUS EFIAPI HashCompleteAndExtend(IN HASH_HANDLE HashHandle, IN TPMI_DH_PCR PcrIndex, IN VOID *DataToHash, IN UINTN DataToHashLen, OUT TPML_DIGEST_VALUES *DigestList)
Definition: HashLibTdx.c:98
#define NULL
Definition: Base.h:319
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
BOOLEAN EFIAPI TdIsEnabled()
Definition: IntelTdxNull.c:79
EFI_STATUS EFIAPI TdExtendRtmr(IN UINT32 *Data, IN UINT32 DataLen, IN UINT8 Index)
Definition: Rtmr.c:40
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
Definition: Base.h:213