2 This library is used by other modules to measure data to TPM.
4 Copyright (c) 2020, Intel Corporation. All rights reserved. <BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include <Guid/CcEventHob.h>
11 #include <Library/BaseLib.h>
12 #include <Library/BaseMemoryLib.h>
13 #include <Library/DebugLib.h>
14 #include <Library/HashLib.h>
15 #include <Library/HobLib.h>
16 #include <Library/PrintLib.h>
17 #include <IndustryStandard/Tpm20.h>
18 #include <Protocol/CcMeasurement.h>
19 #include <Library/TpmMeasurementLib.h>
25 TPMI_ALG_HASH HashAlg
;
26 BYTE Sha384
[SHA384_DIGEST_SIZE
];
31 #define INVALID_PCR2MR_INDEX 0xFF
34 Get the mapped RTMR index based on the input PCRIndex.
36 RTMR[1] => PCR[2,3,4,5]
40 PCR[0] is mapped to MRTD and should not appear here.
41 PCR[6] is reserved for OEM. It is not used.
43 @param[in] PCRIndex The input PCR index
45 @retval UINT8 The mapped RTMR index.
54 if ((PCRIndex
== 6) || (PCRIndex
== 0) || (PCRIndex
> 15)) {
55 DEBUG ((DEBUG_ERROR
, "Invalid PCRIndex(%d) map to MR Index.\n", PCRIndex
));
57 return INVALID_PCR2MR_INDEX
;
61 if ((PCRIndex
== 1) || (PCRIndex
== 7)) {
63 } else if ((PCRIndex
>= 2) && (PCRIndex
< 6)) {
65 } else if ((PCRIndex
>= 8) && (PCRIndex
<= 15)) {
73 Tpm measure and log data, and extend the measurement result into a specific PCR.
75 @param[in] PcrIndex PCR Index.
76 @param[in] EventType Event type.
77 @param[in] EventLog Measurement event log.
78 @param[in] LogLen Event log length in bytes.
79 @param[in] HashData The start of the data buffer to be hashed, extended.
80 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData
82 @retval EFI_SUCCESS Operation completed successfully.
83 @retval EFI_UNSUPPORTED TPM device not available.
84 @retval EFI_OUT_OF_RESOURCES Out of memory.
85 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
89 TpmMeasureAndLogData (
101 TCG_PCR_EVENT2
*TcgPcrEvent2
;
103 TDX_DIGEST_VALUE
*TdxDigest
;
104 TPML_DIGEST_VALUES DigestList
;
107 if (!TdIsEnabled ()) {
108 return EFI_UNSUPPORTED
;
111 RtmrIndex
= GetMappedRtmrIndex (PcrIndex
);
112 if (RtmrIndex
== INVALID_PCR2MR_INDEX
) {
113 return EFI_INVALID_PARAMETER
;
116 DEBUG ((DEBUG_INFO
, "Creating TdTcg2PcrEvent PCR[%d]/RTMR[%d] EventType 0x%x\n", PcrIndex
, RtmrIndex
, EventType
));
118 Status
= HashAndExtend (
125 if (EFI_ERROR (Status
)) {
126 DEBUG ((DEBUG_INFO
, "Failed to HashAndExtend. %r\n", Status
));
131 // Use TDX_DIGEST_VALUE in the GUID HOB DataLength calculation
132 // to reserve enough buffer to hold TPML_DIGEST_VALUES compact binary
133 // which is limited to a SHA384 digest list
135 EventHobData
= BuildGuidHob (
136 &gCcEventEntryHobGuid
,
137 sizeof (TcgPcrEvent2
->PCRIndex
) + sizeof (TcgPcrEvent2
->EventType
) +
138 sizeof (TDX_DIGEST_VALUE
) +
139 sizeof (TcgPcrEvent2
->EventSize
) + LogLen
142 if (EventHobData
== NULL
) {
143 return EFI_OUT_OF_RESOURCES
;
146 Ptr
= (UINT8
*)EventHobData
;
148 // Initialize PcrEvent data now
151 CopyMem (Ptr
, &RtmrIndex
, sizeof (UINT32
));
152 Ptr
+= sizeof (UINT32
);
153 CopyMem (Ptr
, &EventType
, sizeof (TCG_EVENTTYPE
));
154 Ptr
+= sizeof (TCG_EVENTTYPE
);
158 TdxDigest
= (TDX_DIGEST_VALUE
*)DigestBuffer
;
159 TdxDigest
->Count
= 1;
160 TdxDigest
->HashAlg
= TPM_ALG_SHA384
;
163 DigestList
.digests
[0].digest
.sha384
,
167 Ptr
+= sizeof (TDX_DIGEST_VALUE
);
169 CopyMem (Ptr
, &LogLen
, sizeof (UINT32
));
170 Ptr
+= sizeof (UINT32
);
171 CopyMem (Ptr
, EventLog
, LogLen
);
174 Status
= EFI_SUCCESS
;