]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/Library/SecTpmMeasurementLib/SecTpmMeasurementLibTdx.c
38887b172dc0f31e40a1a04541ab469981b9b404
[mirror_edk2.git] / SecurityPkg / Library / SecTpmMeasurementLib / SecTpmMeasurementLibTdx.c
1 /** @file
2 This library is used by other modules to measure data to TPM.
3
4 Copyright (c) 2020, Intel Corporation. All rights reserved. <BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include <PiPei.h>
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>
20
21 #pragma pack(1)
22
23 typedef struct {
24 UINT32 Count;
25 TPMI_ALG_HASH HashAlg;
26 BYTE Sha384[SHA384_DIGEST_SIZE];
27 } TDX_DIGEST_VALUE;
28
29 #pragma pack()
30
31 #define INVALID_PCR2MR_INDEX 0xFF
32
33 /**
34 Get the mapped RTMR index based on the input PCRIndex.
35 RTMR[0] => PCR[1,7]
36 RTMR[1] => PCR[2,3,4,5]
37 RTMR[2] => PCR[8~15]
38 RTMR[3] => NA
39 Note:
40 PCR[0] is mapped to MRTD and should not appear here.
41 PCR[6] is reserved for OEM. It is not used.
42
43 @param[in] PCRIndex The input PCR index
44
45 @retval UINT8 The mapped RTMR index.
46 **/
47 UINT8
48 GetMappedRtmrIndex (
49 IN UINT32 PCRIndex
50 )
51 {
52 UINT8 RtmrIndex;
53
54 if ((PCRIndex == 6) || (PCRIndex == 0) || (PCRIndex > 15)) {
55 DEBUG ((DEBUG_ERROR, "Invalid PCRIndex(%d) map to MR Index.\n", PCRIndex));
56 ASSERT (FALSE);
57 return INVALID_PCR2MR_INDEX;
58 }
59
60 RtmrIndex = 0;
61 if ((PCRIndex == 1) || (PCRIndex == 7)) {
62 RtmrIndex = 0;
63 } else if ((PCRIndex >= 2) && (PCRIndex < 6)) {
64 RtmrIndex = 1;
65 } else if ((PCRIndex >= 8) && (PCRIndex <= 15)) {
66 RtmrIndex = 2;
67 }
68
69 return RtmrIndex;
70 }
71
72 /**
73 Tpm measure and log data, and extend the measurement result into a specific PCR.
74
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
81
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.
86 **/
87 EFI_STATUS
88 EFIAPI
89 TpmMeasureAndLogData (
90 IN UINT32 PcrIndex,
91 IN UINT32 EventType,
92 IN VOID *EventLog,
93 IN UINT32 LogLen,
94 IN VOID *HashData,
95 IN UINT64 HashDataLen
96 )
97 {
98 EFI_STATUS Status;
99 UINT32 RtmrIndex;
100 VOID *EventHobData;
101 TCG_PCR_EVENT2 *TcgPcrEvent2;
102 UINT8 *DigestBuffer;
103 TDX_DIGEST_VALUE *TdxDigest;
104 TPML_DIGEST_VALUES DigestList;
105 UINT8 *Ptr;
106
107 if (!TdIsEnabled ()) {
108 return EFI_UNSUPPORTED;
109 }
110
111 RtmrIndex = GetMappedRtmrIndex (PcrIndex);
112 if (RtmrIndex == INVALID_PCR2MR_INDEX) {
113 return EFI_INVALID_PARAMETER;
114 }
115
116 DEBUG ((DEBUG_INFO, "Creating TdTcg2PcrEvent PCR[%d]/RTMR[%d] EventType 0x%x\n", PcrIndex, RtmrIndex, EventType));
117
118 Status = HashAndExtend (
119 RtmrIndex,
120 (VOID *)HashData,
121 HashDataLen,
122 &DigestList
123 );
124
125 if (EFI_ERROR (Status)) {
126 DEBUG ((DEBUG_INFO, "Failed to HashAndExtend. %r\n", Status));
127 return Status;
128 }
129
130 //
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
134 //
135 EventHobData = BuildGuidHob (
136 &gCcEventEntryHobGuid,
137 sizeof (TcgPcrEvent2->PCRIndex) + sizeof (TcgPcrEvent2->EventType) +
138 sizeof (TDX_DIGEST_VALUE) +
139 sizeof (TcgPcrEvent2->EventSize) + LogLen
140 );
141
142 if (EventHobData == NULL) {
143 return EFI_OUT_OF_RESOURCES;
144 }
145
146 Ptr = (UINT8 *)EventHobData;
147 //
148 // Initialize PcrEvent data now
149 //
150 RtmrIndex++;
151 CopyMem (Ptr, &RtmrIndex, sizeof (UINT32));
152 Ptr += sizeof (UINT32);
153 CopyMem (Ptr, &EventType, sizeof (TCG_EVENTTYPE));
154 Ptr += sizeof (TCG_EVENTTYPE);
155
156 DigestBuffer = Ptr;
157
158 TdxDigest = (TDX_DIGEST_VALUE *)DigestBuffer;
159 TdxDigest->Count = 1;
160 TdxDigest->HashAlg = TPM_ALG_SHA384;
161 CopyMem (
162 TdxDigest->Sha384,
163 DigestList.digests[0].digest.sha384,
164 SHA384_DIGEST_SIZE
165 );
166
167 Ptr += sizeof (TDX_DIGEST_VALUE);
168
169 CopyMem (Ptr, &LogLen, sizeof (UINT32));
170 Ptr += sizeof (UINT32);
171 CopyMem (Ptr, EventLog, LogLen);
172 Ptr += LogLen;
173
174 Status = EFI_SUCCESS;
175 return Status;
176 }