TianoCore EDK2 master
Loading...
Searching...
No Matches
HashLibBaseCryptoRouterPei.c
Go to the documentation of this file.
1
11#include <PiPei.h>
12#include <Library/BaseLib.h>
15#include <Library/DebugLib.h>
17#include <Library/PcdLib.h>
18#include <Library/HobLib.h>
19#include <Library/HashLib.h>
20#include <Guid/ZeroGuid.h>
21
23
24#define HASH_LIB_PEI_ROUTER_GUID \
25 { 0x84681c08, 0x6873, 0x46f3, { 0x8b, 0xb7, 0xab, 0x66, 0x18, 0x95, 0xa1, 0xb3 } }
26
27EFI_GUID mHashLibPeiRouterGuid = HASH_LIB_PEI_ROUTER_GUID;
28
29typedef struct {
30 //
31 // If gZeroGuid, SupportedHashMask is 0 for FIRST module which consumes HashLib
32 // or the hash algorithm bitmap of LAST module which consumes HashLib.
33 // HashInterfaceCount and HashInterface are all 0.
34 // If gEfiCallerIdGuid, HashInterfaceCount, HashInterface and SupportedHashMask
35 // are the hash interface information of CURRENT module which consumes HashLib.
36 //
37 EFI_GUID Identifier;
38 UINTN HashInterfaceCount;
39 HASH_INTERFACE HashInterface[HASH_COUNT];
40 UINT32 SupportedHashMask;
42
52 EFI_GUID *Identifier
53 )
54{
56 HASH_INTERFACE_HOB *HashInterfaceHob;
57
58 Hob.Raw = GetFirstGuidHob (&mHashLibPeiRouterGuid);
59 while (Hob.Raw != NULL) {
60 HashInterfaceHob = GET_GUID_HOB_DATA (Hob);
61 if (CompareGuid (&HashInterfaceHob->Identifier, Identifier)) {
62 //
63 // Found the matched one.
64 //
65 return HashInterfaceHob;
66 }
67
68 Hob.Raw = GET_NEXT_HOB (Hob);
69 Hob.Raw = GetNextGuidHob (&mHashLibPeiRouterGuid, Hob.Raw);
70 }
71
72 return NULL;
73}
74
84 EFI_GUID *Identifier
85 )
86{
87 HASH_INTERFACE_HOB LocalHashInterfaceHob;
88
89 ZeroMem (&LocalHashInterfaceHob, sizeof (LocalHashInterfaceHob));
90 CopyGuid (&LocalHashInterfaceHob.Identifier, Identifier);
91 return BuildGuidDataHob (&mHashLibPeiRouterGuid, &LocalHashInterfaceHob, sizeof (LocalHashInterfaceHob));
92}
93
101VOID
103 IN HASH_INTERFACE_HOB *HashInterfaceHobCurrent
104 )
105{
106 HASH_INTERFACE_HOB *HashInterfaceHobLast;
107
108 HashInterfaceHobLast = InternalGetHashInterfaceHob (&gZeroGuid);
109 ASSERT (HashInterfaceHobLast != NULL);
110
111 if ((HashInterfaceHobLast->SupportedHashMask != 0) &&
112 (HashInterfaceHobCurrent->SupportedHashMask != HashInterfaceHobLast->SupportedHashMask))
113 {
114 DEBUG ((
115 DEBUG_WARN,
116 "WARNING: There is mismatch of supported HashMask (0x%x - 0x%x) between modules\n",
117 HashInterfaceHobCurrent->SupportedHashMask,
118 HashInterfaceHobLast->SupportedHashMask
119 ));
120 DEBUG ((DEBUG_WARN, "that are linking different HashInstanceLib instances!\n"));
121 }
122}
123
133EFIAPI
135 OUT HASH_HANDLE *HashHandle
136 )
137{
138 HASH_INTERFACE_HOB *HashInterfaceHob;
139 HASH_HANDLE *HashCtx;
140 UINTN Index;
141 UINT32 HashMask;
142
143 HashInterfaceHob = InternalGetHashInterfaceHob (&gEfiCallerIdGuid);
144 if (HashInterfaceHob == NULL) {
145 return EFI_UNSUPPORTED;
146 }
147
148 if (HashInterfaceHob->HashInterfaceCount == 0) {
149 return EFI_UNSUPPORTED;
150 }
151
152 CheckSupportedHashMaskMismatch (HashInterfaceHob);
153
154 HashCtx = AllocatePool (sizeof (*HashCtx) * HashInterfaceHob->HashInterfaceCount);
155 ASSERT (HashCtx != NULL);
156
157 for (Index = 0; Index < HashInterfaceHob->HashInterfaceCount; Index++) {
158 HashMask = Tpm2GetHashMaskFromAlgo (&HashInterfaceHob->HashInterface[Index].HashGuid);
159 if ((HashMask & PcdGet32 (PcdTpm2HashMask)) != 0) {
160 HashInterfaceHob->HashInterface[Index].HashInit (&HashCtx[Index]);
161 }
162 }
163
164 *HashHandle = (HASH_HANDLE)HashCtx;
165
166 return EFI_SUCCESS;
167}
168
179EFIAPI
181 IN HASH_HANDLE HashHandle,
182 IN VOID *DataToHash,
183 IN UINTN DataToHashLen
184 )
185{
186 HASH_INTERFACE_HOB *HashInterfaceHob;
187 HASH_HANDLE *HashCtx;
188 UINTN Index;
189 UINT32 HashMask;
190
191 HashInterfaceHob = InternalGetHashInterfaceHob (&gEfiCallerIdGuid);
192 if (HashInterfaceHob == NULL) {
193 return EFI_UNSUPPORTED;
194 }
195
196 if (HashInterfaceHob->HashInterfaceCount == 0) {
197 return EFI_UNSUPPORTED;
198 }
199
200 CheckSupportedHashMaskMismatch (HashInterfaceHob);
201
202 HashCtx = (HASH_HANDLE *)HashHandle;
203
204 for (Index = 0; Index < HashInterfaceHob->HashInterfaceCount; Index++) {
205 HashMask = Tpm2GetHashMaskFromAlgo (&HashInterfaceHob->HashInterface[Index].HashGuid);
206 if ((HashMask & PcdGet32 (PcdTpm2HashMask)) != 0) {
207 HashInterfaceHob->HashInterface[Index].HashUpdate (HashCtx[Index], DataToHash, DataToHashLen);
208 }
209 }
210
211 return EFI_SUCCESS;
212}
213
226EFIAPI
228 IN HASH_HANDLE HashHandle,
229 IN TPMI_DH_PCR PcrIndex,
230 IN VOID *DataToHash,
231 IN UINTN DataToHashLen,
232 OUT TPML_DIGEST_VALUES *DigestList
233 )
234{
235 TPML_DIGEST_VALUES Digest;
236 HASH_INTERFACE_HOB *HashInterfaceHob;
237 HASH_HANDLE *HashCtx;
238 UINTN Index;
239 EFI_STATUS Status;
240 UINT32 HashMask;
241
242 HashInterfaceHob = InternalGetHashInterfaceHob (&gEfiCallerIdGuid);
243 if (HashInterfaceHob == NULL) {
244 return EFI_UNSUPPORTED;
245 }
246
247 if (HashInterfaceHob->HashInterfaceCount == 0) {
248 return EFI_UNSUPPORTED;
249 }
250
251 CheckSupportedHashMaskMismatch (HashInterfaceHob);
252
253 HashCtx = (HASH_HANDLE *)HashHandle;
254 ZeroMem (DigestList, sizeof (*DigestList));
255
256 for (Index = 0; Index < HashInterfaceHob->HashInterfaceCount; Index++) {
257 HashMask = Tpm2GetHashMaskFromAlgo (&HashInterfaceHob->HashInterface[Index].HashGuid);
258 if ((HashMask & PcdGet32 (PcdTpm2HashMask)) != 0) {
259 HashInterfaceHob->HashInterface[Index].HashUpdate (HashCtx[Index], DataToHash, DataToHashLen);
260 HashInterfaceHob->HashInterface[Index].HashFinal (HashCtx[Index], &Digest);
261 Tpm2SetHashToDigestList (DigestList, &Digest);
262 }
263 }
264
265 FreePool (HashCtx);
266
267 Status = Tpm2PcrExtend (
268 PcrIndex,
269 DigestList
270 );
271 return Status;
272}
273
285EFIAPI
287 IN TPMI_DH_PCR PcrIndex,
288 IN VOID *DataToHash,
289 IN UINTN DataToHashLen,
290 OUT TPML_DIGEST_VALUES *DigestList
291 )
292{
293 HASH_INTERFACE_HOB *HashInterfaceHob;
294 HASH_HANDLE HashHandle;
295 EFI_STATUS Status;
296
297 HashInterfaceHob = InternalGetHashInterfaceHob (&gEfiCallerIdGuid);
298 if (HashInterfaceHob == NULL) {
299 return EFI_UNSUPPORTED;
300 }
301
302 if (HashInterfaceHob->HashInterfaceCount == 0) {
303 return EFI_UNSUPPORTED;
304 }
305
306 CheckSupportedHashMaskMismatch (HashInterfaceHob);
307
308 HashStart (&HashHandle);
309 HashUpdate (HashHandle, DataToHash, DataToHashLen);
310 Status = HashCompleteAndExtend (HashHandle, PcrIndex, NULL, 0, DigestList);
311
312 return Status;
313}
314
325EFIAPI
327 IN HASH_INTERFACE *HashInterface
328 )
329{
330 UINTN Index;
331 HASH_INTERFACE_HOB *HashInterfaceHob;
332 UINT32 HashMask;
333 UINT32 Tpm2HashMask;
334 EFI_STATUS Status;
335
336 //
337 // Check allow
338 //
339 HashMask = Tpm2GetHashMaskFromAlgo (&HashInterface->HashGuid);
340 Tpm2HashMask = PcdGet32 (PcdTpm2HashMask);
341
342 if ((Tpm2HashMask != 0) &&
343 ((HashMask & Tpm2HashMask) == 0))
344 {
345 return EFI_UNSUPPORTED;
346 }
347
348 HashInterfaceHob = InternalGetHashInterfaceHob (&gEfiCallerIdGuid);
349 if (HashInterfaceHob == NULL) {
350 HashInterfaceHob = InternalCreateHashInterfaceHob (&gEfiCallerIdGuid);
351 if (HashInterfaceHob == NULL) {
352 return EFI_OUT_OF_RESOURCES;
353 }
354 }
355
356 if (HashInterfaceHob->HashInterfaceCount >= HASH_COUNT) {
357 return EFI_OUT_OF_RESOURCES;
358 }
359
360 //
361 // Check duplication
362 //
363 for (Index = 0; Index < HashInterfaceHob->HashInterfaceCount; Index++) {
364 if (CompareGuid (&HashInterfaceHob->HashInterface[Index].HashGuid, &HashInterface->HashGuid)) {
365 DEBUG ((DEBUG_ERROR, "Hash Interface (%g) has been registered\n", &HashInterface->HashGuid));
366 return EFI_ALREADY_STARTED;
367 }
368 }
369
370 //
371 // Record hash algorithm bitmap of CURRENT module which consumes HashLib.
372 //
373 HashInterfaceHob->SupportedHashMask = PcdGet32 (PcdTcg2HashAlgorithmBitmap) | HashMask;
374 Status = PcdSet32S (PcdTcg2HashAlgorithmBitmap, HashInterfaceHob->SupportedHashMask);
375 ASSERT_EFI_ERROR (Status);
376
377 CopyMem (&HashInterfaceHob->HashInterface[HashInterfaceHob->HashInterfaceCount], HashInterface, sizeof (*HashInterface));
378 HashInterfaceHob->HashInterfaceCount++;
379
380 return EFI_SUCCESS;
381}
382
394EFIAPI
396 IN EFI_PEI_FILE_HANDLE FileHandle,
397 IN CONST EFI_PEI_SERVICES **PeiServices
398 )
399{
400 EFI_STATUS Status;
401 HASH_INTERFACE_HOB *HashInterfaceHob;
402
403 HashInterfaceHob = InternalGetHashInterfaceHob (&gZeroGuid);
404 if (HashInterfaceHob == NULL) {
405 //
406 // No HOB with gZeroGuid Identifier has been created,
407 // this is FIRST module which consumes HashLib.
408 // Create the HOB with gZeroGuid Identifier.
409 //
410 HashInterfaceHob = InternalCreateHashInterfaceHob (&gZeroGuid);
411 if (HashInterfaceHob == NULL) {
412 return EFI_OUT_OF_RESOURCES;
413 }
414 } else {
415 //
416 // Record hash algorithm bitmap of LAST module which also consumes HashLib.
417 //
418 HashInterfaceHob->SupportedHashMask = PcdGet32 (PcdTcg2HashAlgorithmBitmap);
419 }
420
421 HashInterfaceHob = InternalGetHashInterfaceHob (&gEfiCallerIdGuid);
422 if (HashInterfaceHob != NULL) {
423 //
424 // In PEI phase, some modules may call RegisterForShadow and will be
425 // shadowed and executed again after memory is discovered.
426 // This is the second execution of this module, clear the hash interface
427 // information registered at its first execution.
428 //
429 ZeroMem (&HashInterfaceHob->HashInterface, sizeof (HashInterfaceHob->HashInterface));
430 HashInterfaceHob->HashInterfaceCount = 0;
431 HashInterfaceHob->SupportedHashMask = 0;
432 }
433
434 //
435 // Set PcdTcg2HashAlgorithmBitmap to 0 in CONSTRUCTOR for CURRENT module.
436 //
437 Status = PcdSet32S (PcdTcg2HashAlgorithmBitmap, 0);
438 ASSERT_EFI_ERROR (Status);
439
440 return EFI_SUCCESS;
441}
UINT64 UINTN
VOID *EFIAPI GetFirstGuidHob(IN CONST EFI_GUID *Guid)
Definition: HobLib.c:215
VOID *EFIAPI BuildGuidDataHob(IN CONST EFI_GUID *Guid, IN VOID *Data, IN UINTN DataLength)
Definition: HobLib.c:375
VOID *EFIAPI GetNextGuidHob(IN CONST EFI_GUID *Guid, IN CONST VOID *HobStart)
Definition: HobLib.c:176
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
GUID *EFIAPI CopyGuid(OUT GUID *DestinationGuid, IN CONST GUID *SourceGuid)
Definition: MemLibGuid.c:39
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
VOID EFIAPI FreePool(IN VOID *Buffer)
UINT32 EFIAPI Tpm2GetHashMaskFromAlgo(IN EFI_GUID *HashGuid)
VOID EFIAPI Tpm2SetHashToDigestList(IN OUT TPML_DIGEST_VALUES *DigestList, IN TPML_DIGEST_VALUES *Digest)
HASH_INTERFACE_HOB * InternalCreateHashInterfaceHob(EFI_GUID *Identifier)
EFI_STATUS EFIAPI HashStart(OUT HASH_HANDLE *HashHandle)
VOID CheckSupportedHashMaskMismatch(IN HASH_INTERFACE_HOB *HashInterfaceHobCurrent)
EFI_STATUS EFIAPI RegisterHashInterfaceLib(IN HASH_INTERFACE *HashInterface)
HASH_INTERFACE_HOB * InternalGetHashInterfaceHob(EFI_GUID *Identifier)
EFI_STATUS EFIAPI HashUpdate(IN HASH_HANDLE HashHandle, IN VOID *DataToHash, IN UINTN DataToHashLen)
EFI_STATUS EFIAPI HashAndExtend(IN TPMI_DH_PCR PcrIndex, IN VOID *DataToHash, IN UINTN DataToHashLen, OUT TPML_DIGEST_VALUES *DigestList)
EFI_STATUS EFIAPI HashLibBaseCryptoRouterPeiConstructor(IN EFI_PEI_FILE_HANDLE FileHandle, IN CONST EFI_PEI_SERVICES **PeiServices)
EFI_STATUS EFIAPI HashCompleteAndExtend(IN HASH_HANDLE HashHandle, IN TPMI_DH_PCR PcrIndex, IN VOID *DataToHash, IN UINTN DataToHashLen, OUT TPML_DIGEST_VALUES *DigestList)
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define ASSERT_EFI_ERROR(StatusParameter)
Definition: DebugLib.h:462
#define DEBUG(Expression)
Definition: DebugLib.h:434
#define PcdGet32(TokenName)
Definition: PcdLib.h:362
#define PcdSet32S(TokenName, Value)
Definition: PcdLib.h:497
VOID * EFI_PEI_FILE_HANDLE
Definition: PiPeiCis.h:26
VOID *EFIAPI AllocatePool(IN UINTN AllocationSize)
EFI_STATUS EFIAPI Tpm2PcrExtend(IN TPMI_DH_PCR PcrHandle, IN TPML_DIGEST_VALUES *Digests)
Definition: Tpm2Integrity.c:92
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
Definition: Base.h:213