1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
|
/** @file
A UEFI shell application for testing TPM 2.0 Physical Presence Interface
operations, including querying and configuring PCR banks.
Copyright (C) Microsoft Corporation.
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include <Uefi.h>
#include <Protocol/Tcg2Protocol.h>
#include <IndustryStandard/Tpm20.h>
#include <IndustryStandard/UefiTcgPlatform.h>
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/BaseCryptLib.h>
#include <Library/ShellLib.h>
#include <Library/UefiApplicationEntryPoint.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiLib.h>
// Largest supported digest size (SHA-512).
#define MAX_DIGEST_SIZE SHA512_DIGEST_SIZE
// TPM2_PCR_Read command size (tag + size + code + pcrSelectionIn).
#define TPM2_PCR_READ_CMD_SIZE 20
// Offset to the first digest byte in a TPM2_PCR_Read response.
#define TPM2_PCR_READ_RSP_DIGEST_OFFSET 30
typedef struct {
UINT32 HashMask;
UINT16 AlgId;
CONST CHAR8 *Name;
} HASH_ALG_INFO;
STATIC CONST HASH_ALG_INFO mHashAlgTable[] = {
{ EFI_TCG2_BOOT_HASH_ALG_SHA1, TPM_ALG_SHA1, "SHA1" },
{ EFI_TCG2_BOOT_HASH_ALG_SHA256, TPM_ALG_SHA256, "SHA256" },
{ EFI_TCG2_BOOT_HASH_ALG_SHA384, TPM_ALG_SHA384, "SHA384" },
{ EFI_TCG2_BOOT_HASH_ALG_SHA512, TPM_ALG_SHA512, "SHA512" },
{ EFI_TCG2_BOOT_HASH_ALG_SM3_256, TPM_ALG_SM3_256, "SM3_256" },
};
// Event type name lookup table.
typedef struct {
UINT32 EventType;
CONST CHAR8 *Name;
} EVENT_TYPE_INFO;
STATIC CONST EVENT_TYPE_INFO mEventTypeTable[] = {
{ EV_POST_CODE, "EV_POST_CODE" },
{ EV_NO_ACTION, "EV_NO_ACTION" },
{ EV_SEPARATOR, "EV_SEPARATOR" },
{ EV_ACTION, "EV_ACTION" },
{ EV_S_CRTM_CONTENTS, "EV_S_CRTM_CONTENTS" },
{ EV_S_CRTM_VERSION, "EV_S_CRTM_VERSION" },
{ EV_TABLE_OF_DEVICES, "EV_TABLE_OF_DEVICES" },
{ EV_EFI_VARIABLE_DRIVER_CONFIG, "EV_EFI_VARIABLE_DRIVER_CONFIG" },
{ EV_EFI_VARIABLE_BOOT, "EV_EFI_VARIABLE_BOOT" },
{ EV_EFI_BOOT_SERVICES_APPLICATION, "EV_EFI_BOOT_SERVICES_APPLICATION" },
{ EV_EFI_BOOT_SERVICES_DRIVER, "EV_EFI_BOOT_SERVICES_DRIVER" },
{ EV_EFI_RUNTIME_SERVICES_DRIVER, "EV_EFI_RUNTIME_SERVICES_DRIVER" },
{ EV_EFI_GPT_EVENT, "EV_EFI_GPT_EVENT" },
{ EV_EFI_ACTION, "EV_EFI_ACTION" },
{ EV_EFI_PLATFORM_FIRMWARE_BLOB, "EV_EFI_PLATFORM_FIRMWARE_BLOB" },
{ EV_EFI_HANDOFF_TABLES, "EV_EFI_HANDOFF_TABLES" },
{ EV_EFI_PLATFORM_FIRMWARE_BLOB2, "EV_EFI_PLATFORM_FIRMWARE_BLOB2" },
{ EV_EFI_HANDOFF_TABLES2, "EV_EFI_HANDOFF_TABLES2" },
{ EV_EFI_VARIABLE_AUTHORITY, "EV_EFI_VARIABLE_AUTHORITY" },
};
/**
Print a bitmask of hash algorithms as human-readable names.
@param[in] AlgBitmap Bitmask of HASH_ALG_* values.
**/
STATIC
VOID
PrintAlgorithms (
IN UINT32 AlgBitmap
)
{
UINTN Index;
for (Index = 0; Index < ARRAY_SIZE (mHashAlgTable); Index++) {
if ((AlgBitmap & mHashAlgTable[Index].HashMask) != 0) {
Print (L" * %a\n", mHashAlgTable[Index].Name);
}
}
}
/**
Query and display TPM PCR bank information via TCG2 Protocol.
Retrieves the supported and currently active PCR banks from the
TCG2 Protocol capability structure.
@param[in] Tcg2Protocol Pointer to the TCG2 Protocol instance.
@retval EFI_SUCCESS Information retrieved and displayed.
@retval Others Error querying the protocol.
**/
STATIC
EFI_STATUS
ShowPcrBanks (
IN EFI_TCG2_PROTOCOL *Tcg2Protocol
)
{
EFI_STATUS Status;
EFI_TCG2_BOOT_SERVICE_CAPABILITY Capability;
Capability.Size = sizeof (Capability);
Status = Tcg2Protocol->GetCapability (Tcg2Protocol, &Capability);
if (EFI_ERROR (Status)) {
Print (L"GetCapability failed - %r\n", Status);
return Status;
}
Print (L"Supported PCR banks bitmap: 0x%x\n", Capability.HashAlgorithmBitmap);
PrintAlgorithms (Capability.HashAlgorithmBitmap);
Print (L"\n");
Print (L"Active PCR banks: 0x%x\n", Capability.ActivePcrBanks);
PrintAlgorithms (Capability.ActivePcrBanks);
return EFI_SUCCESS;
}
/**
Submit a request to set specific PCR banks via TCG2 Protocol.
This calls TCG2 Protocol SetActivePcrBanks which submits a Physical
Presence request internally. The change will take effect on the next
reboot.
@param[in] Tcg2Protocol Pointer to the TCG2 Protocol instance.
@param[in] DesiredBanks Bitmask of desired PCR banks
(EFI_TCG2_BOOT_HASH_ALG_* values).
@retval EFI_SUCCESS Request submitted successfully.
@retval Others Error submitting the request.
**/
STATIC
EFI_STATUS
RequestSetPcrBanks (
IN EFI_TCG2_PROTOCOL *Tcg2Protocol,
IN UINT32 DesiredBanks
)
{
EFI_STATUS Status;
Print (L"Submitting SetActivePcrBanks with parameter 0x%x\n", DesiredBanks);
Status = Tcg2Protocol->SetActivePcrBanks (Tcg2Protocol, DesiredBanks);
Print (L"Status: %r\n", Status);
if (EFI_ERROR (Status)) {
Print (L"SetActivePcrBanks failed.\n");
return Status;
}
Print (L"Request submitted. Changes will take effect after reboot.\n");
return EFI_SUCCESS;
}
/**
Submit a request to enable all supported PCR banks via TCG2 Protocol.
@param[in] Tcg2Protocol Pointer to the TCG2 Protocol instance.
@retval EFI_SUCCESS Request submitted successfully.
@retval Others Error submitting the request.
**/
STATIC
EFI_STATUS
RequestEnableAllPcrBanks (
IN EFI_TCG2_PROTOCOL *Tcg2Protocol
)
{
EFI_STATUS Status;
EFI_TCG2_BOOT_SERVICE_CAPABILITY Capability;
Capability.Size = sizeof (Capability);
Status = Tcg2Protocol->GetCapability (Tcg2Protocol, &Capability);
if (EFI_ERROR (Status)) {
Print (L"GetCapability failed - %r\n", Status);
return Status;
}
Print (L"Requesting all supported PCR banks: 0x%x\n", Capability.HashAlgorithmBitmap);
Status = Tcg2Protocol->SetActivePcrBanks (Tcg2Protocol, Capability.HashAlgorithmBitmap);
Print (L"Status: %r\n", Status);
if (EFI_ERROR (Status)) {
Print (L"SetActivePcrBanks failed.\n");
return Status;
}
Print (L"Request submitted. All supported PCR banks will be enabled after reboot.\n");
return EFI_SUCCESS;
}
/**
Return a human-readable name for a TPM algorithm ID.
@param[in] AlgId TPM_ALG_ID value.
@return Static string name, or "UNKNOWN" if not recognized.
**/
STATIC
CONST CHAR8 *
GetAlgName (
IN UINT16 AlgId
)
{
UINTN Index;
for (Index = 0; Index < ARRAY_SIZE (mHashAlgTable); Index++) {
if (mHashAlgTable[Index].AlgId == AlgId) {
return mHashAlgTable[Index].Name;
}
}
return "UNKNOWN";
}
/**
Return a human-readable name for a TCG event type.
@param[in] EventType TCG_EVENTTYPE value.
@return Static string name, or "UNKNOWN" if not recognized.
**/
STATIC
CONST CHAR8 *
GetEventTypeName (
IN UINT32 EventType
)
{
UINTN Index;
for (Index = 0; Index < ARRAY_SIZE (mEventTypeTable); Index++) {
if (mEventTypeTable[Index].EventType == EventType) {
return mEventTypeTable[Index].Name;
}
}
return "UNKNOWN";
}
/**
Print a byte buffer as a hexadecimal string.
@param[in] Data Pointer to the byte buffer.
@param[in] Size Number of bytes to print.
**/
STATIC
VOID
PrintHexDump (
IN UINT8 *Data,
IN UINT16 Size
)
{
UINT16 Index;
for (Index = 0; Index < Size; Index++) {
Print (L"%02x ", Data[Index]);
}
}
/**
Retrieve and parse the TCG2 crypto-agile event log header.
Calls GetEventLog(), validates the Spec ID Event, and extracts the
algorithm list. On success the caller receives pointers to the first
TCG_PCR_EVENT2 entry and the last entry, plus the algorithm table.
@param[in] Tcg2Protocol Pointer to the TCG2 Protocol instance.
@param[out] FirstEvent Receives pointer to the first TCG_PCR_EVENT2.
@param[out] LastEvent Receives pointer to the last TCG_PCR_EVENT2.
@param[out] AlgList Receives the algorithm/digest-size table.
@param[out] NumberOfAlgorithms Receives the number of algorithms.
@param[out] WasTruncated Receives TRUE if the log was truncated.
@retval EFI_SUCCESS Event log parsed successfully.
@retval EFI_NOT_FOUND Event log is empty.
@retval EFI_UNSUPPORTED Event log format is not recognized.
@retval Others Error from GetEventLog.
**/
STATIC
EFI_STATUS
GetParsedEventLog (
IN EFI_TCG2_PROTOCOL *Tcg2Protocol,
OUT UINT8 **FirstEvent,
OUT UINT8 **LastEvent,
OUT TCG_EfiSpecIdEventAlgorithmSize *AlgList,
OUT UINT32 *NumberOfAlgorithms,
OUT BOOLEAN *WasTruncated
)
{
EFI_STATUS Status;
EFI_PHYSICAL_ADDRESS LogLocation;
EFI_PHYSICAL_ADDRESS LogLastEntry;
TCG_PCR_EVENT_HDR *SpecIdHdr;
TCG_EfiSpecIDEventStruct *SpecId;
UINT8 *LogData;
UINT32 AlgIndex;
TCG_EfiSpecIdEventAlgorithmSize *AlgEntry;
Status = Tcg2Protocol->GetEventLog (
Tcg2Protocol,
EFI_TCG2_EVENT_LOG_FORMAT_TCG_2,
&LogLocation,
&LogLastEntry,
WasTruncated
);
if (EFI_ERROR (Status)) {
Print (L"GetEventLog failed - %r\n", Status);
return Status;
}
if ((LogLocation == 0) || (LogLastEntry == 0)) {
Print (L"Event log is empty.\n");
return EFI_NOT_FOUND;
}
// The first entry is a v1 TCG_PCR_EVENT containing the Spec ID Event.
SpecIdHdr = (TCG_PCR_EVENT_HDR *)(UINTN)LogLocation;
if (SpecIdHdr->EventType != EV_NO_ACTION) {
Print (L"First event is not a Spec ID Event (type 0x%x).\n", SpecIdHdr->EventType);
return EFI_UNSUPPORTED;
}
SpecId = (TCG_EfiSpecIDEventStruct *)((UINT8 *)SpecIdHdr + sizeof (TCG_PCR_EVENT_HDR));
if (AsciiStrCmp (
(CONST CHAR8 *)SpecId->signature,
TCG_EfiSpecIDEventStruct_SIGNATURE_03
) != 0)
{
Print (L"Unsupported event log format (expected crypto-agile).\n");
return EFI_UNSUPPORTED;
}
// Read the algorithm list from the variable-length portion of the Spec ID Event.
LogData = (UINT8 *)SpecId + sizeof (TCG_EfiSpecIDEventStruct);
*NumberOfAlgorithms = *(UINT32 *)LogData;
LogData += sizeof (UINT32);
if ((*NumberOfAlgorithms == 0) || (*NumberOfAlgorithms > ARRAY_SIZE (mHashAlgTable))) {
Print (L"Invalid algorithm count in Spec ID Event (%u).\n", *NumberOfAlgorithms);
return EFI_UNSUPPORTED;
}
for (AlgIndex = 0; AlgIndex < *NumberOfAlgorithms; AlgIndex++) {
AlgEntry = (TCG_EfiSpecIdEventAlgorithmSize *)LogData;
AlgList[AlgIndex] = *AlgEntry;
LogData += sizeof (TCG_EfiSpecIdEventAlgorithmSize);
}
*FirstEvent = (UINT8 *)(UINTN)LogLocation + sizeof (TCG_PCR_EVENT_HDR) + SpecIdHdr->EventSize;
*LastEvent = (UINT8 *)(UINTN)LogLastEntry;
return EFI_SUCCESS;
}
/**
Retrieve, parse, and display the TCG2 crypto-agile event log.
Retrieves the event log via GetParsedEventLog(), then walks each
subsequent TCG_PCR_EVENT2 entry displaying the PCR index, event
type, and digest values.
@param[in] Tcg2Protocol Pointer to the TCG2 Protocol instance.
@retval EFI_SUCCESS Event log retrieved and displayed.
@retval EFI_NOT_FOUND Event log is empty.
@retval EFI_UNSUPPORTED Event log format is not recognized.
@retval EFI_ABORTED Event log entry is malformed.
@retval Others Error from GetEventLog.
**/
STATIC
EFI_STATUS
DumpEventLog (
IN EFI_TCG2_PROTOCOL *Tcg2Protocol
)
{
EFI_STATUS Status;
BOOLEAN Truncated;
UINT32 NumberOfAlgorithms;
TCG_EfiSpecIdEventAlgorithmSize AlgList[ARRAY_SIZE (mHashAlgTable)];
UINT32 AlgIndex;
UINT8 *CurrentEvent;
UINT8 *LastEvent;
UINTN EventNum;
UINT32 PcrIndex;
UINT32 EventType;
UINT32 DigestCount;
UINT16 AlgId;
UINT16 DigestSize;
UINT32 EventDataSize;
UINT32 DigestIndex;
UINT32 MatchIndex;
UINT8 *Digest;
Status = GetParsedEventLog (
Tcg2Protocol,
&CurrentEvent,
&LastEvent,
AlgList,
&NumberOfAlgorithms,
&Truncated
);
if (EFI_ERROR (Status)) {
return Status;
}
if (Truncated) {
Print (L"Warning: Event log was truncated.\n\n");
}
Print (L"Spec ID Event: %u algorithm(s)\n", NumberOfAlgorithms);
for (AlgIndex = 0; AlgIndex < NumberOfAlgorithms; AlgIndex++) {
Print (
L" %a (alg 0x%x, digest %u bytes)\n",
GetAlgName (AlgList[AlgIndex].algorithmId),
AlgList[AlgIndex].algorithmId,
AlgList[AlgIndex].digestSize
);
}
Print (L"\n");
EventNum = 0;
while (CurrentEvent <= LastEvent) {
EventNum++;
// Read fixed fields: PCRIndex, EventType, DigestCount.
PcrIndex = *(UINT32 *)CurrentEvent;
EventType = *(UINT32 *)(CurrentEvent + sizeof (UINT32));
DigestCount = *(UINT32 *)(CurrentEvent + 2 * sizeof (UINT32));
Print (
L"Event %u: PCR %u %a (0x%x)\n",
EventNum,
PcrIndex,
GetEventTypeName (EventType),
EventType
);
if (DigestCount > NumberOfAlgorithms) {
Print (L" ERROR: DigestCount (%u) exceeds algorithm count. Stopping.\n", DigestCount);
return EFI_ABORTED;
}
// Walk the digest list: each digest has a UINT16 AlgId followed by digest bytes.
Digest = CurrentEvent + 3 * sizeof (UINT32);
for (DigestIndex = 0; DigestIndex < DigestCount; DigestIndex++) {
AlgId = *(UINT16 *)Digest;
Digest += sizeof (UINT16);
// Look up the digest size from the Spec ID Event algorithm list.
DigestSize = 0;
for (MatchIndex = 0; MatchIndex < NumberOfAlgorithms; MatchIndex++) {
if (AlgList[MatchIndex].algorithmId == AlgId) {
DigestSize = AlgList[MatchIndex].digestSize;
break;
}
}
if (DigestSize == 0) {
Print (L" ERROR: Unknown algorithm 0x%x. Stopping.\n", AlgId);
return EFI_ABORTED;
}
Print (L" %a: ", GetAlgName (AlgId));
PrintHexDump (Digest, DigestSize);
Print (L"\n");
Digest += DigestSize;
}
// Read EventSize and advance past the event data.
EventDataSize = *(UINT32 *)Digest;
Print (L" Event data: %u bytes\n", EventDataSize);
CurrentEvent = Digest + sizeof (UINT32) + EventDataSize;
}
Print (L"\nTotal: %u event(s)\n", EventNum);
return EFI_SUCCESS;
}
/**
Extend a PCR accumulator with a new digest.
Computes NewPcr = Hash(CurrentPcr || Digest) using the hash algorithm
identified by AlgId. CurrentPcr and NewPcr may point to the same buffer.
@param[in] AlgId TPM_ALG_ID identifying the hash algorithm.
@param[in] CurrentPcr Current PCR value (DigestSize bytes).
@param[in] Digest Digest to extend with (DigestSize bytes).
@param[in] DigestSize Size of CurrentPcr and Digest in bytes.
@param[out] NewPcr Receives the extended PCR value (DigestSize bytes).
@retval EFI_SUCCESS Extension computed.
@retval EFI_UNSUPPORTED Algorithm not supported for replay.
@retval EFI_DEVICE_ERROR Hash computation failed.
**/
STATIC
EFI_STATUS
HashExtend (
IN UINT16 AlgId,
IN UINT8 *CurrentPcr,
IN UINT8 *Digest,
IN UINT16 DigestSize,
OUT UINT8 *NewPcr
)
{
UINT8 Buffer[MAX_DIGEST_SIZE * 2];
BOOLEAN Result;
CopyMem (Buffer, CurrentPcr, DigestSize);
CopyMem (Buffer + DigestSize, Digest, DigestSize);
switch (AlgId) {
case TPM_ALG_SHA1:
Result = Sha1HashAll (Buffer, (UINTN)DigestSize * 2, NewPcr);
break;
case TPM_ALG_SHA256:
Result = Sha256HashAll (Buffer, (UINTN)DigestSize * 2, NewPcr);
break;
case TPM_ALG_SHA384:
Result = Sha384HashAll (Buffer, (UINTN)DigestSize * 2, NewPcr);
break;
case TPM_ALG_SHA512:
Result = Sha512HashAll (Buffer, (UINTN)DigestSize * 2, NewPcr);
break;
default:
return EFI_UNSUPPORTED;
}
return Result ? EFI_SUCCESS : EFI_DEVICE_ERROR;
}
/**
Read a single PCR value from the TPM via SubmitCommand.
Builds a TPM2_PCR_Read command and sends it through
Tcg2Protocol->SubmitCommand(), avoiding any dependency on
Tpm2CommandLib.
@param[in] Tcg2Protocol Pointer to the TCG2 Protocol instance.
@param[in] AlgId TPM_ALG_ID for the PCR bank to read.
@param[in] PcrIndex PCR index (0..23).
@param[in] DigestSize Expected digest size in bytes.
@param[out] PcrValue Receives the PCR digest (DigestSize bytes).
@retval EFI_SUCCESS PCR value read successfully.
@retval EFI_INVALID_PARAMETER PcrIndex out of range.
@retval EFI_DEVICE_ERROR TPM returned a non-zero response code.
@retval Others Error from SubmitCommand.
**/
STATIC
EFI_STATUS
ReadPcrValue (
IN EFI_TCG2_PROTOCOL *Tcg2Protocol,
IN UINT16 AlgId,
IN UINT32 PcrIndex,
IN UINT16 DigestSize,
OUT UINT8 *PcrValue
)
{
EFI_STATUS Status;
UINT8 CmdBuffer[TPM2_PCR_READ_CMD_SIZE];
UINT8 RspBuffer[TPM2_PCR_READ_RSP_DIGEST_OFFSET + MAX_DIGEST_SIZE];
UINT32 RspSize;
UINT32 ResponseCode;
UINT8 *Ptr;
if (PcrIndex > MAX_PCR_INDEX) {
return EFI_INVALID_PARAMETER;
}
// Build TPM2_PCR_Read command (big-endian wire format).
// Layout (20 bytes total):
// [0-1] tag = TPM_ST_NO_SESSIONS
// [2-5] commandSize = 20
// [6-9] commandCode = TPM_CC_PCR_Read
// [10-13] count = 1
// [14-15] hash = AlgId
// [16] sizeofSelect = 3
// [17-19] pcrSelect[3] = bit for PcrIndex
Ptr = CmdBuffer;
*(UINT16 *)Ptr = SwapBytes16 (TPM_ST_NO_SESSIONS);
Ptr += sizeof (UINT16);
*(UINT32 *)Ptr = SwapBytes32 ((UINT32)TPM2_PCR_READ_CMD_SIZE);
Ptr += sizeof (UINT32);
*(UINT32 *)Ptr = SwapBytes32 (TPM_CC_PCR_Read);
Ptr += sizeof (UINT32);
*(UINT32 *)Ptr = SwapBytes32 (1);
Ptr += sizeof (UINT32);
*(UINT16 *)Ptr = SwapBytes16 (AlgId);
Ptr += sizeof (UINT16);
*Ptr = 3;
Ptr++;
Ptr[0] = 0;
Ptr[1] = 0;
Ptr[2] = 0;
Ptr[PcrIndex / 8] = (UINT8)(1 << (PcrIndex % 8));
// Send command and read response.
RspSize = sizeof (RspBuffer);
Status = Tcg2Protocol->SubmitCommand (
Tcg2Protocol,
TPM2_PCR_READ_CMD_SIZE,
CmdBuffer,
RspSize,
RspBuffer
);
if (EFI_ERROR (Status)) {
return Status;
}
// Response layout (for a single PCR):
// [0-1] tag
// [2-5] responseSize
// [6-9] responseCode
// [10-13] pcrUpdateCounter
// [14-17] pcrSelectionOut.count
// [18-19] hash
// [20] sizeofSelect
// [21-23] pcrSelect[3]
// [24-27] digests.count
// [28-29] digests[0].size
// [30+] digests[0].buffer
ResponseCode = SwapBytes32 (*(UINT32 *)(RspBuffer + 6));
if (ResponseCode != 0) {
return EFI_DEVICE_ERROR;
}
CopyMem (PcrValue, RspBuffer + TPM2_PCR_READ_RSP_DIGEST_OFFSET, DigestSize);
return EFI_SUCCESS;
}
/**
Replay the TCG2 event log and verify against actual TPM PCR values.
Retrieves the event log, replays all extend operations locally to
compute expected PCR values, reads the actual PCR values from the
TPM via SubmitCommand, and compares them.
@param[in] Tcg2Protocol Pointer to the TCG2 Protocol instance.
@retval EFI_SUCCESS All replayed PCRs match the TPM.
@retval EFI_NOT_FOUND Event log is empty.
@retval EFI_UNSUPPORTED Event log format not recognized.
@retval EFI_ABORTED Replay completed but one or more PCRs did not match.
@retval Others Error from GetEventLog or SubmitCommand.
**/
STATIC
EFI_STATUS
ReplayAndVerifyEventLog (
IN EFI_TCG2_PROTOCOL *Tcg2Protocol
)
{
EFI_STATUS Status;
BOOLEAN Truncated;
UINT32 NumberOfAlgorithms;
TCG_EfiSpecIdEventAlgorithmSize AlgList[ARRAY_SIZE (mHashAlgTable)];
BOOLEAN AlgReplayable[ARRAY_SIZE (mHashAlgTable)];
UINT32 AlgIndex;
UINT8 *CurrentEvent;
UINT8 *LastEvent;
UINTN EventNum;
UINT32 PcrIndex;
UINT32 DigestCount;
UINT16 AlgId;
UINT16 DigestSize;
UINT32 EventDataSize;
UINT32 DigestIndex;
UINT32 MatchIndex;
UINT8 ReplayedPcrs[ARRAY_SIZE (mHashAlgTable)][MAX_PCR_INDEX + 1][MAX_DIGEST_SIZE];
BOOLEAN PcrUsed[MAX_PCR_INDEX + 1];
UINT8 TpmPcrValue[MAX_DIGEST_SIZE];
UINTN PcrIdx;
UINTN PassCount;
UINTN FailCount;
UINTN VerifiedCount;
BOOLEAN Match;
UINT8 *Digest;
Status = GetParsedEventLog (
Tcg2Protocol,
&CurrentEvent,
&LastEvent,
AlgList,
&NumberOfAlgorithms,
&Truncated
);
if (EFI_ERROR (Status)) {
return Status;
}
if (Truncated) {
Print (L"Warning: Event log was truncated. Replay may not match.\n\n");
}
for (AlgIndex = 0; AlgIndex < NumberOfAlgorithms; AlgIndex++) {
AlgReplayable[AlgIndex] = TRUE;
}
// Initialize PCR accumulators to zero.
ZeroMem (ReplayedPcrs, sizeof (ReplayedPcrs));
ZeroMem (PcrUsed, sizeof (PcrUsed));
// Walk events and replay PCR extensions.
EventNum = 0;
while (CurrentEvent <= LastEvent) {
EventNum++;
PcrIndex = *(UINT32 *)CurrentEvent;
DigestCount = *(UINT32 *)(CurrentEvent + 2 * sizeof (UINT32));
if (DigestCount > NumberOfAlgorithms) {
Print (L"Event %u: DigestCount (%u) exceeds algorithm count. Stopping.\n", EventNum, DigestCount);
return EFI_ABORTED;
}
if (PcrIndex <= MAX_PCR_INDEX) {
PcrUsed[PcrIndex] = TRUE;
}
Digest = CurrentEvent + 3 * sizeof (UINT32);
for (DigestIndex = 0; DigestIndex < DigestCount; DigestIndex++) {
AlgId = *(UINT16 *)Digest;
Digest += sizeof (UINT16);
DigestSize = 0;
MatchIndex = 0;
for (MatchIndex = 0; MatchIndex < NumberOfAlgorithms; MatchIndex++) {
if (AlgList[MatchIndex].algorithmId == AlgId) {
DigestSize = AlgList[MatchIndex].digestSize;
break;
}
}
if (DigestSize == 0) {
Print (L"Event %u: Unknown algorithm 0x%x. Stopping.\n", EventNum, AlgId);
return EFI_ABORTED;
}
if ((PcrIndex <= MAX_PCR_INDEX) && AlgReplayable[MatchIndex]) {
Status = HashExtend (
AlgId,
ReplayedPcrs[MatchIndex][PcrIndex],
Digest,
DigestSize,
ReplayedPcrs[MatchIndex][PcrIndex]
);
if (Status == EFI_UNSUPPORTED) {
AlgReplayable[MatchIndex] = FALSE;
} else if (EFI_ERROR (Status)) {
Print (L"Event %u: Hash extend failed for %a - %r\n", EventNum, GetAlgName (AlgId), Status);
return Status;
}
}
Digest += DigestSize;
}
EventDataSize = *(UINT32 *)Digest;
CurrentEvent = Digest + sizeof (UINT32) + EventDataSize;
}
Print (L"Replayed %u event(s) across %u algorithm(s).\n\n", EventNum, NumberOfAlgorithms);
// Verify replayed PCRs against actual TPM values.
PassCount = 0;
FailCount = 0;
VerifiedCount = 0;
for (PcrIdx = 0; PcrIdx <= MAX_PCR_INDEX; PcrIdx++) {
if (!PcrUsed[PcrIdx]) {
continue;
}
Print (L"PCR %u:\n", PcrIdx);
for (AlgIndex = 0; AlgIndex < NumberOfAlgorithms; AlgIndex++) {
AlgId = AlgList[AlgIndex].algorithmId;
DigestSize = AlgList[AlgIndex].digestSize;
if (!AlgReplayable[AlgIndex]) {
Print (L" %a: (replay not supported, skipped)\n", GetAlgName (AlgId));
continue;
}
Print (L" %a Replayed: ", GetAlgName (AlgId));
PrintHexDump (ReplayedPcrs[AlgIndex][PcrIdx], DigestSize);
Print (L"\n");
Status = ReadPcrValue (Tcg2Protocol, AlgId, (UINT32)PcrIdx, DigestSize, TpmPcrValue);
if (EFI_ERROR (Status)) {
Print (L" %a Actual: (read failed - %r)\n", GetAlgName (AlgId), Status);
FailCount++;
VerifiedCount++;
continue;
}
Print (L" %a Actual: ", GetAlgName (AlgId));
PrintHexDump (TpmPcrValue, DigestSize);
Print (L"\n");
Match = (CompareMem (ReplayedPcrs[AlgIndex][PcrIdx], TpmPcrValue, DigestSize) == 0);
Print (L" Result: %a\n", Match ? "PASS" : "FAIL");
if (Match) {
PassCount++;
} else {
FailCount++;
}
VerifiedCount++;
}
Print (L"\n");
}
Print (L"Summary: %u PCR bank(s) verified, %u PASS, %u FAIL\n", VerifiedCount, PassCount, FailCount);
return (FailCount > 0) ? EFI_ABORTED : EFI_SUCCESS;
}
/**
Print usage information.
**/
STATIC
VOID
PrintUsage (
VOID
)
{
Print (L"TpmShellApp - TPM 2.0 Physical Presence Test Utility\n");
Print (L"\n");
Print (L"Usage:\n");
Print (L" TpmShellApp help - Show usage\n");
Print (L" TpmShellApp get_active_pcr_banks - Display active PCR banks\n");
Print (L" TpmShellApp set_active_pcr_banks <mask> - Request active PCR bank change (hex bitmask)\n");
Print (L" TpmShellApp enable_all_pcr_banks - Enable all supported PCR banks\n");
Print (L" TpmShellApp dump_event_log - Dump the TCG2 event log\n");
Print (L" TpmShellApp replay_event_log - Replay event log and verify PCRs\n");
Print (L" TpmShellApp get_last_result - Show last SetActivePcrBanks result\n");
Print (L"\n");
Print (L"PCR bank bitmask values:\n");
Print (L" 0x00000001 = SHA1\n");
Print (L" 0x00000002 = SHA256\n");
Print (L" 0x00000004 = SHA384\n");
Print (L" 0x00000008 = SHA512\n");
Print (L" 0x00000010 = SM3_256\n");
Print (L"\n");
Print (L"Example: TpmShellApp set_active_pcr_banks 0x2 (enable SHA256 only)\n");
Print (L"Example: TpmShellApp set_active_pcr_banks 0x6 (enable SHA256 + SHA384)\n");
}
/**
Entry point for the TPM Test UEFI Shell Application.
@param[in] ImageHandle The firmware allocated handle for the EFI image.
@param[in] SystemTable A pointer to the EFI System Table.
@retval EFI_SUCCESS Application executed successfully.
@retval Others An error occurred.
**/
EFI_STATUS
EFIAPI
TpmShellAppEntry (
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
{
EFI_STATUS Status;
UINTN Argc;
LIST_ENTRY *ParamPackage;
CONST CHAR16 *Command;
CONST CHAR16 *PcrMaskStr;
UINT64 PcrMaskVal;
EFI_TCG2_PROTOCOL *Tcg2Protocol;
UINT32 OperationPresent;
UINT32 Response;
// Initialize the Shell library.
Status = ShellInitialize ();
if (EFI_ERROR (Status)) {
Print (L"ShellInitialize failed - %r\n", Status);
return Status;
}
// Parse command line.
Status = ShellCommandLineParse (EmptyParamList, &ParamPackage, NULL, FALSE);
if (EFI_ERROR (Status)) {
return Status;
}
// Validate number of input parameters.
Argc = ShellCommandLineGetCount (ParamPackage);
if (Argc < 2) {
Print (L"\n[Invalid Usage]\n");
Print (L" Use 'TpmShellApp help' for usage.\n");
Status = EFI_INVALID_PARAMETER;
goto Exit;
}
// Acquire the command value.
Command = ShellCommandLineGetRawValue (ParamPackage, 1);
if (Command == NULL) {
Print (L"\n[Invalid Usage]\n");
Print (L" Use 'TpmShellApp help' for usage.\n");
Status = EFI_INVALID_PARAMETER;
goto Exit;
}
// Locate TCG2 Protocol it is required for this test app to function
Status = gBS->LocateProtocol (&gEfiTcg2ProtocolGuid, NULL, (VOID **)&Tcg2Protocol);
if (EFI_ERROR (Status)) {
Print (L"TCG2 Protocol not found - %r\n", Status);
Print (L" TPM 2.0 may not be enabled on this platform.\n");
goto Exit;
}
// Dispatch based on command.
if (StrCmp (Command, L"help") == 0) {
Print (L"\n[TPM 2.0 Help]\n\n");
PrintUsage ();
goto Exit;
} else if (StrCmp (Command, L"get_active_pcr_banks") == 0) {
Print (L"\n[TPM 2.0 Information]\n\n");
ShowPcrBanks (Tcg2Protocol);
} else if (StrCmp (Command, L"set_active_pcr_banks") == 0) {
Print (L"\n[Set PCR Banks]\n\n");
if (Argc < 3) {
Print (L"set_active_pcr_banks requires a hex bitmask parameter.\n");
Print (L" Example: TpmShellApp set_active_pcr_banks 0x2\n");
Status = EFI_INVALID_PARAMETER;
goto Exit;
}
PcrMaskStr = ShellCommandLineGetRawValue (ParamPackage, 2);
Status = ShellConvertStringToUint64 (
PcrMaskStr,
&PcrMaskVal,
TRUE,
FALSE
);
if (EFI_ERROR (Status)) {
Print (L"Invalid hex value.\n");
goto Exit;
}
Status = RequestSetPcrBanks (Tcg2Protocol, (UINT32)PcrMaskVal);
} else if (StrCmp (Command, L"enable_all_pcr_banks") == 0) {
Print (L"\n[Enable All PCR Banks]\n\n");
Status = RequestEnableAllPcrBanks (Tcg2Protocol);
} else if (StrCmp (Command, L"dump_event_log") == 0) {
Print (L"\n[TCG2 Event Log]\n\n");
Status = DumpEventLog (Tcg2Protocol);
} else if (StrCmp (Command, L"replay_event_log") == 0) {
Print (L"\n[Event Log Replay]\n\n");
Status = ReplayAndVerifyEventLog (Tcg2Protocol);
} else if (StrCmp (Command, L"get_last_result") == 0) {
Print (L"\n[Last SetActivePcrBanks Result]\n\n");
Status = Tcg2Protocol->GetResultOfSetActivePcrBanks (Tcg2Protocol, &OperationPresent, &Response);
if (EFI_ERROR (Status)) {
Print (L" GetResultOfSetActivePcrBanks failed - %r\n", Status);
goto Exit;
}
Print (L" Operation present: %a\n", OperationPresent ? "YES" : "NO");
Print (L" Response code: %u\n", Response);
} else {
Print (L"\n[Invalid Input Command]\n\n");
Print (L" Unknown command '%s'\n", Command);
Print (L" Use 'TpmShellApp help' for usage.\n");
Status = EFI_INVALID_PARAMETER;
}
Exit:
Print (L"\n");
ShellCommandLineFreeVarList (ParamPackage);
return Status;
}
|