]>
Commit | Line | Data |
---|---|---|
a332cfd3 | 1 | /** @file\r |
8c06c53b MX |
2 | This library is used by other modules to measure data to TPM and Confidential\r |
3 | Computing (CC) measure registers.\r | |
a332cfd3 | 4 | \r |
b3548d32 | 5 | Copyright (c) 2012 - 2018, Intel Corporation. All rights reserved. <BR>\r |
289b714b | 6 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
a332cfd3 | 7 | \r |
8 | **/\r | |
9 | \r | |
10 | #include <PiDxe.h>\r | |
11 | \r | |
12 | #include <Protocol/TcgService.h>\r | |
1abfa4ce | 13 | #include <Protocol/Tcg2Protocol.h>\r |
a332cfd3 | 14 | \r |
15 | #include <Library/BaseMemoryLib.h>\r | |
16 | #include <Library/MemoryAllocationLib.h>\r | |
17 | #include <Library/UefiBootServicesTableLib.h>\r | |
18 | #include <Library/DebugLib.h>\r | |
19 | #include <Library/TpmMeasurementLib.h>\r | |
20 | \r | |
21 | #include <Guid/Acpi.h>\r | |
22 | #include <IndustryStandard/Acpi.h>\r | |
8c06c53b | 23 | #include <Protocol/CcMeasurement.h>\r |
a332cfd3 | 24 | \r |
a332cfd3 | 25 | /**\r |
26 | Tpm12 measure and log data, and extend the measurement result into a specific PCR.\r | |
27 | \r | |
28 | @param[in] PcrIndex PCR Index.\r | |
29 | @param[in] EventType Event type.\r | |
30 | @param[in] EventLog Measurement event log.\r | |
31 | @param[in] LogLen Event log length in bytes.\r | |
32 | @param[in] HashData The start of the data buffer to be hashed, extended.\r | |
33 | @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData\r | |
34 | \r | |
35 | @retval EFI_SUCCESS Operation completed successfully.\r | |
36 | @retval EFI_UNSUPPORTED TPM device not available.\r | |
37 | @retval EFI_OUT_OF_RESOURCES Out of memory.\r | |
38 | @retval EFI_DEVICE_ERROR The operation was unsuccessful.\r | |
39 | **/\r | |
8c06c53b | 40 | STATIC\r |
a332cfd3 | 41 | EFI_STATUS\r |
42 | Tpm12MeasureAndLogData (\r | |
c411b485 MK |
43 | IN UINT32 PcrIndex,\r |
44 | IN UINT32 EventType,\r | |
45 | IN VOID *EventLog,\r | |
46 | IN UINT32 LogLen,\r | |
47 | IN VOID *HashData,\r | |
48 | IN UINT64 HashDataLen\r | |
a332cfd3 | 49 | )\r |
50 | {\r | |
c411b485 MK |
51 | EFI_STATUS Status;\r |
52 | EFI_TCG_PROTOCOL *TcgProtocol;\r | |
53 | TCG_PCR_EVENT *TcgEvent;\r | |
54 | EFI_PHYSICAL_ADDRESS EventLogLastEntry;\r | |
55 | UINT32 EventNumber;\r | |
a332cfd3 | 56 | \r |
57 | TcgEvent = NULL;\r | |
58 | \r | |
59 | //\r | |
d6b926e7 | 60 | // Tpm activation state is checked in HashLogExtendEvent\r |
a332cfd3 | 61 | //\r |
c411b485 MK |
62 | Status = gBS->LocateProtocol (&gEfiTcgProtocolGuid, NULL, (VOID **)&TcgProtocol);\r |
63 | if (EFI_ERROR (Status)) {\r | |
a332cfd3 | 64 | return Status;\r |
65 | }\r | |
66 | \r | |
67 | TcgEvent = (TCG_PCR_EVENT *)AllocateZeroPool (sizeof (TCG_PCR_EVENT_HDR) + LogLen);\r | |
c411b485 | 68 | if (TcgEvent == NULL) {\r |
a332cfd3 | 69 | return EFI_OUT_OF_RESOURCES;\r |
70 | }\r | |
71 | \r | |
72 | TcgEvent->PCRIndex = PcrIndex;\r | |
73 | TcgEvent->EventType = EventType;\r | |
74 | TcgEvent->EventSize = LogLen;\r | |
75 | CopyMem (&TcgEvent->Event[0], EventLog, LogLen);\r | |
76 | EventNumber = 1;\r | |
c411b485 MK |
77 | Status = TcgProtocol->HashLogExtendEvent (\r |
78 | TcgProtocol,\r | |
79 | (EFI_PHYSICAL_ADDRESS)(UINTN)HashData,\r | |
80 | HashDataLen,\r | |
81 | TPM_ALG_SHA,\r | |
82 | TcgEvent,\r | |
83 | &EventNumber,\r | |
84 | &EventLogLastEntry\r | |
85 | );\r | |
a332cfd3 | 86 | \r |
87 | FreePool (TcgEvent);\r | |
88 | \r | |
89 | return Status;\r | |
90 | }\r | |
91 | \r | |
c1d93242 JY |
92 | /**\r |
93 | Tpm20 measure and log data, and extend the measurement result into a specific PCR.\r | |
94 | \r | |
95 | @param[in] PcrIndex PCR Index.\r | |
96 | @param[in] EventType Event type.\r | |
97 | @param[in] EventLog Measurement event log.\r | |
98 | @param[in] LogLen Event log length in bytes.\r | |
99 | @param[in] HashData The start of the data buffer to be hashed, extended.\r | |
100 | @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData\r | |
101 | \r | |
102 | @retval EFI_SUCCESS Operation completed successfully.\r | |
103 | @retval EFI_UNSUPPORTED TPM device not available.\r | |
104 | @retval EFI_OUT_OF_RESOURCES Out of memory.\r | |
105 | @retval EFI_DEVICE_ERROR The operation was unsuccessful.\r | |
106 | **/\r | |
8c06c53b | 107 | STATIC\r |
c1d93242 JY |
108 | EFI_STATUS\r |
109 | Tpm20MeasureAndLogData (\r | |
c411b485 MK |
110 | IN UINT32 PcrIndex,\r |
111 | IN UINT32 EventType,\r | |
112 | IN VOID *EventLog,\r | |
113 | IN UINT32 LogLen,\r | |
114 | IN VOID *HashData,\r | |
115 | IN UINT64 HashDataLen\r | |
c1d93242 JY |
116 | )\r |
117 | {\r | |
c411b485 MK |
118 | EFI_STATUS Status;\r |
119 | EFI_TCG2_PROTOCOL *Tcg2Protocol;\r | |
120 | EFI_TCG2_EVENT *Tcg2Event;\r | |
c1d93242 JY |
121 | \r |
122 | //\r | |
1abfa4ce | 123 | // TPMPresentFlag is checked in HashLogExtendEvent\r |
c1d93242 | 124 | //\r |
c411b485 | 125 | Status = gBS->LocateProtocol (&gEfiTcg2ProtocolGuid, NULL, (VOID **)&Tcg2Protocol);\r |
c1d93242 JY |
126 | if (EFI_ERROR (Status)) {\r |
127 | return Status;\r | |
128 | }\r | |
129 | \r | |
c411b485 MK |
130 | Tcg2Event = (EFI_TCG2_EVENT *)AllocateZeroPool (LogLen + sizeof (EFI_TCG2_EVENT));\r |
131 | if (Tcg2Event == NULL) {\r | |
c1d93242 JY |
132 | return EFI_OUT_OF_RESOURCES;\r |
133 | }\r | |
134 | \r | |
c411b485 MK |
135 | Tcg2Event->Size = (UINT32)LogLen + sizeof (EFI_TCG2_EVENT) - sizeof (Tcg2Event->Event);\r |
136 | Tcg2Event->Header.HeaderSize = sizeof (EFI_TCG2_EVENT_HEADER);\r | |
1abfa4ce JY |
137 | Tcg2Event->Header.HeaderVersion = EFI_TCG2_EVENT_HEADER_VERSION;\r |
138 | Tcg2Event->Header.PCRIndex = PcrIndex;\r | |
139 | Tcg2Event->Header.EventType = EventType;\r | |
140 | CopyMem (&Tcg2Event->Event[0], EventLog, LogLen);\r | |
c1d93242 | 141 | \r |
1abfa4ce JY |
142 | Status = Tcg2Protocol->HashLogExtendEvent (\r |
143 | Tcg2Protocol,\r | |
c1d93242 JY |
144 | 0,\r |
145 | (EFI_PHYSICAL_ADDRESS)(UINTN)HashData,\r | |
146 | HashDataLen,\r | |
1abfa4ce | 147 | Tcg2Event\r |
c1d93242 | 148 | );\r |
1abfa4ce | 149 | FreePool (Tcg2Event);\r |
c1d93242 JY |
150 | \r |
151 | return Status;\r | |
152 | }\r | |
153 | \r | |
8c06c53b MX |
154 | /**\r |
155 | Cc measure and log data, and extend the measurement result into a\r | |
156 | specific CC MR.\r | |
157 | \r | |
158 | @param[in] CcProtocol Instance of CC measurement protocol\r | |
159 | @param[in] PcrIndex PCR Index.\r | |
160 | @param[in] EventType Event type.\r | |
161 | @param[in] EventLog Measurement event log.\r | |
162 | @param[in] LogLen Event log length in bytes.\r | |
163 | @param[in] HashData The start of the data buffer to be hashed, extended.\r | |
164 | @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData\r | |
165 | \r | |
166 | @retval EFI_SUCCESS Operation completed successfully.\r | |
167 | @retval EFI_UNSUPPORTED CC guest not available.\r | |
168 | @retval EFI_OUT_OF_RESOURCES Out of memory.\r | |
169 | @retval EFI_DEVICE_ERROR The operation was unsuccessful.\r | |
170 | @retval EFI_INVALID_PARAMETER The input parameter is invalid.\r | |
171 | **/\r | |
172 | STATIC\r | |
173 | EFI_STATUS\r | |
174 | CcMeasureAndLogData (\r | |
175 | IN EFI_CC_MEASUREMENT_PROTOCOL *CcProtocol,\r | |
176 | IN UINT32 PcrIndex,\r | |
177 | IN UINT32 EventType,\r | |
178 | IN VOID *EventLog,\r | |
179 | IN UINT32 LogLen,\r | |
180 | IN VOID *HashData,\r | |
181 | IN UINT64 HashDataLen\r | |
182 | )\r | |
183 | {\r | |
184 | EFI_STATUS Status;\r | |
185 | EFI_CC_EVENT *EfiCcEvent;\r | |
186 | EFI_CC_MR_INDEX MrIndex;\r | |
187 | \r | |
188 | if (CcProtocol == NULL) {\r | |
189 | return EFI_INVALID_PARAMETER;\r | |
190 | }\r | |
191 | \r | |
192 | Status = CcProtocol->MapPcrToMrIndex (CcProtocol, PcrIndex, &MrIndex);\r | |
193 | if (EFI_ERROR (Status)) {\r | |
194 | return Status;\r | |
195 | }\r | |
196 | \r | |
197 | EfiCcEvent = (EFI_CC_EVENT *)AllocateZeroPool (LogLen + sizeof (EFI_CC_EVENT));\r | |
198 | if (EfiCcEvent == NULL) {\r | |
199 | return EFI_OUT_OF_RESOURCES;\r | |
200 | }\r | |
201 | \r | |
202 | EfiCcEvent->Size = (UINT32)LogLen + sizeof (EFI_CC_EVENT) - sizeof (EfiCcEvent->Event);\r | |
203 | EfiCcEvent->Header.HeaderSize = sizeof (EFI_CC_EVENT_HEADER);\r | |
204 | EfiCcEvent->Header.HeaderVersion = EFI_CC_EVENT_HEADER_VERSION;\r | |
205 | EfiCcEvent->Header.MrIndex = MrIndex;\r | |
206 | EfiCcEvent->Header.EventType = EventType;\r | |
207 | CopyMem (&EfiCcEvent->Event[0], EventLog, LogLen);\r | |
208 | \r | |
209 | Status = CcProtocol->HashLogExtendEvent (\r | |
210 | CcProtocol,\r | |
211 | 0,\r | |
212 | (EFI_PHYSICAL_ADDRESS)(UINTN)HashData,\r | |
213 | HashDataLen,\r | |
214 | EfiCcEvent\r | |
215 | );\r | |
216 | FreePool (EfiCcEvent);\r | |
217 | \r | |
218 | return Status;\r | |
219 | }\r | |
220 | \r | |
a332cfd3 | 221 | /**\r |
222 | Tpm measure and log data, and extend the measurement result into a specific PCR.\r | |
223 | \r | |
224 | @param[in] PcrIndex PCR Index.\r | |
225 | @param[in] EventType Event type.\r | |
226 | @param[in] EventLog Measurement event log.\r | |
227 | @param[in] LogLen Event log length in bytes.\r | |
228 | @param[in] HashData The start of the data buffer to be hashed, extended.\r | |
229 | @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData\r | |
230 | \r | |
231 | @retval EFI_SUCCESS Operation completed successfully.\r | |
232 | @retval EFI_UNSUPPORTED TPM device not available.\r | |
233 | @retval EFI_OUT_OF_RESOURCES Out of memory.\r | |
234 | @retval EFI_DEVICE_ERROR The operation was unsuccessful.\r | |
235 | **/\r | |
a332cfd3 | 236 | EFI_STATUS\r |
b3548d32 | 237 | EFIAPI\r |
a332cfd3 | 238 | TpmMeasureAndLogData (\r |
c411b485 MK |
239 | IN UINT32 PcrIndex,\r |
240 | IN UINT32 EventType,\r | |
241 | IN VOID *EventLog,\r | |
242 | IN UINT32 LogLen,\r | |
243 | IN VOID *HashData,\r | |
244 | IN UINT64 HashDataLen\r | |
a332cfd3 | 245 | )\r |
246 | {\r | |
8c06c53b MX |
247 | EFI_STATUS Status;\r |
248 | EFI_CC_MEASUREMENT_PROTOCOL *CcProtocol;\r | |
d9e206d4 | 249 | \r |
8c06c53b MX |
250 | Status = gBS->LocateProtocol (&gEfiCcMeasurementProtocolGuid, NULL, (VOID **)&CcProtocol);\r |
251 | if (!EFI_ERROR (Status)) {\r | |
c1d93242 | 252 | //\r |
8c06c53b | 253 | // Try to measure using Cc measurement protocol\r |
c1d93242 | 254 | //\r |
8c06c53b MX |
255 | Status = CcMeasureAndLogData (\r |
256 | CcProtocol,\r | |
c1d93242 JY |
257 | PcrIndex,\r |
258 | EventType,\r | |
259 | EventLog,\r | |
260 | LogLen,\r | |
261 | HashData,\r | |
262 | HashDataLen\r | |
263 | );\r | |
8c06c53b MX |
264 | } else {\r |
265 | //\r | |
266 | // Try to measure using Tpm20 protocol\r | |
267 | //\r | |
268 | Status = Tpm20MeasureAndLogData (\r | |
269 | PcrIndex,\r | |
270 | EventType,\r | |
271 | EventLog,\r | |
272 | LogLen,\r | |
273 | HashData,\r | |
274 | HashDataLen\r | |
275 | );\r | |
276 | \r | |
277 | if (EFI_ERROR (Status)) {\r | |
278 | //\r | |
279 | // Try to measure using Tpm1.2 protocol\r | |
280 | //\r | |
281 | Status = Tpm12MeasureAndLogData (\r | |
282 | PcrIndex,\r | |
283 | EventType,\r | |
284 | EventLog,\r | |
285 | LogLen,\r | |
286 | HashData,\r | |
287 | HashDataLen\r | |
288 | );\r | |
289 | }\r | |
c1d93242 | 290 | }\r |
a332cfd3 | 291 | \r |
292 | return Status;\r | |
293 | }\r |