]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/Library/DxeTpmMeasurementLib/DxeTpmMeasurementLib.c
d014ea4aec081392deabbb4e665867ed75ac84ad
[mirror_edk2.git] / SecurityPkg / Library / DxeTpmMeasurementLib / DxeTpmMeasurementLib.c
1 /** @file
2 This library is used by other modules to measure data to TPM.
3
4 Copyright (c) 2012 - 2018, Intel Corporation. All rights reserved. <BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include <PiDxe.h>
10
11 #include <Protocol/TcgService.h>
12 #include <Protocol/Tcg2Protocol.h>
13
14 #include <Library/BaseMemoryLib.h>
15 #include <Library/MemoryAllocationLib.h>
16 #include <Library/UefiBootServicesTableLib.h>
17 #include <Library/DebugLib.h>
18 #include <Library/TpmMeasurementLib.h>
19
20 #include <Guid/Acpi.h>
21 #include <IndustryStandard/Acpi.h>
22
23 /**
24 Tpm12 measure and log data, and extend the measurement result into a specific PCR.
25
26 @param[in] PcrIndex PCR Index.
27 @param[in] EventType Event type.
28 @param[in] EventLog Measurement event log.
29 @param[in] LogLen Event log length in bytes.
30 @param[in] HashData The start of the data buffer to be hashed, extended.
31 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData
32
33 @retval EFI_SUCCESS Operation completed successfully.
34 @retval EFI_UNSUPPORTED TPM device not available.
35 @retval EFI_OUT_OF_RESOURCES Out of memory.
36 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
37 **/
38 EFI_STATUS
39 Tpm12MeasureAndLogData (
40 IN UINT32 PcrIndex,
41 IN UINT32 EventType,
42 IN VOID *EventLog,
43 IN UINT32 LogLen,
44 IN VOID *HashData,
45 IN UINT64 HashDataLen
46 )
47 {
48 EFI_STATUS Status;
49 EFI_TCG_PROTOCOL *TcgProtocol;
50 TCG_PCR_EVENT *TcgEvent;
51 EFI_PHYSICAL_ADDRESS EventLogLastEntry;
52 UINT32 EventNumber;
53
54 TcgEvent = NULL;
55
56 //
57 // Tpm activation state is checked in HashLogExtendEvent
58 //
59 Status = gBS->LocateProtocol (&gEfiTcgProtocolGuid, NULL, (VOID **)&TcgProtocol);
60 if (EFI_ERROR (Status)) {
61 return Status;
62 }
63
64 TcgEvent = (TCG_PCR_EVENT *)AllocateZeroPool (sizeof (TCG_PCR_EVENT_HDR) + LogLen);
65 if (TcgEvent == NULL) {
66 return EFI_OUT_OF_RESOURCES;
67 }
68
69 TcgEvent->PCRIndex = PcrIndex;
70 TcgEvent->EventType = EventType;
71 TcgEvent->EventSize = LogLen;
72 CopyMem (&TcgEvent->Event[0], EventLog, LogLen);
73 EventNumber = 1;
74 Status = TcgProtocol->HashLogExtendEvent (
75 TcgProtocol,
76 (EFI_PHYSICAL_ADDRESS)(UINTN)HashData,
77 HashDataLen,
78 TPM_ALG_SHA,
79 TcgEvent,
80 &EventNumber,
81 &EventLogLastEntry
82 );
83
84 FreePool (TcgEvent);
85
86 return Status;
87 }
88
89 /**
90 Tpm20 measure and log data, and extend the measurement result into a specific PCR.
91
92 @param[in] PcrIndex PCR Index.
93 @param[in] EventType Event type.
94 @param[in] EventLog Measurement event log.
95 @param[in] LogLen Event log length in bytes.
96 @param[in] HashData The start of the data buffer to be hashed, extended.
97 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData
98
99 @retval EFI_SUCCESS Operation completed successfully.
100 @retval EFI_UNSUPPORTED TPM device not available.
101 @retval EFI_OUT_OF_RESOURCES Out of memory.
102 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
103 **/
104 EFI_STATUS
105 Tpm20MeasureAndLogData (
106 IN UINT32 PcrIndex,
107 IN UINT32 EventType,
108 IN VOID *EventLog,
109 IN UINT32 LogLen,
110 IN VOID *HashData,
111 IN UINT64 HashDataLen
112 )
113 {
114 EFI_STATUS Status;
115 EFI_TCG2_PROTOCOL *Tcg2Protocol;
116 EFI_TCG2_EVENT *Tcg2Event;
117
118 //
119 // TPMPresentFlag is checked in HashLogExtendEvent
120 //
121 Status = gBS->LocateProtocol (&gEfiTcg2ProtocolGuid, NULL, (VOID **)&Tcg2Protocol);
122 if (EFI_ERROR (Status)) {
123 return Status;
124 }
125
126 Tcg2Event = (EFI_TCG2_EVENT *)AllocateZeroPool (LogLen + sizeof (EFI_TCG2_EVENT));
127 if (Tcg2Event == NULL) {
128 return EFI_OUT_OF_RESOURCES;
129 }
130
131 Tcg2Event->Size = (UINT32)LogLen + sizeof (EFI_TCG2_EVENT) - sizeof (Tcg2Event->Event);
132 Tcg2Event->Header.HeaderSize = sizeof (EFI_TCG2_EVENT_HEADER);
133 Tcg2Event->Header.HeaderVersion = EFI_TCG2_EVENT_HEADER_VERSION;
134 Tcg2Event->Header.PCRIndex = PcrIndex;
135 Tcg2Event->Header.EventType = EventType;
136 CopyMem (&Tcg2Event->Event[0], EventLog, LogLen);
137
138 Status = Tcg2Protocol->HashLogExtendEvent (
139 Tcg2Protocol,
140 0,
141 (EFI_PHYSICAL_ADDRESS)(UINTN)HashData,
142 HashDataLen,
143 Tcg2Event
144 );
145 FreePool (Tcg2Event);
146
147 return Status;
148 }
149
150 /**
151 Tpm measure and log data, and extend the measurement result into a specific PCR.
152
153 @param[in] PcrIndex PCR Index.
154 @param[in] EventType Event type.
155 @param[in] EventLog Measurement event log.
156 @param[in] LogLen Event log length in bytes.
157 @param[in] HashData The start of the data buffer to be hashed, extended.
158 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData
159
160 @retval EFI_SUCCESS Operation completed successfully.
161 @retval EFI_UNSUPPORTED TPM device not available.
162 @retval EFI_OUT_OF_RESOURCES Out of memory.
163 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
164 **/
165 EFI_STATUS
166 EFIAPI
167 TpmMeasureAndLogData (
168 IN UINT32 PcrIndex,
169 IN UINT32 EventType,
170 IN VOID *EventLog,
171 IN UINT32 LogLen,
172 IN VOID *HashData,
173 IN UINT64 HashDataLen
174 )
175 {
176 EFI_STATUS Status;
177
178 //
179 // Try to measure using Tpm20 protocol
180 //
181 Status = Tpm20MeasureAndLogData (
182 PcrIndex,
183 EventType,
184 EventLog,
185 LogLen,
186 HashData,
187 HashDataLen
188 );
189
190 if (EFI_ERROR (Status)) {
191 //
192 // Try to measure using Tpm1.2 protocol
193 //
194 Status = Tpm12MeasureAndLogData (
195 PcrIndex,
196 EventType,
197 EventLog,
198 LogLen,
199 HashData,
200 HashDataLen
201 );
202 }
203
204 return Status;
205 }