]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - SecurityPkg/Library/DxeTpmMeasurementLib/DxeTpmMeasurementLib.c
Add TPM2 implementation.
[mirror_edk2.git] / SecurityPkg / Library / DxeTpmMeasurementLib / DxeTpmMeasurementLib.c
... / ...
CommitLineData
1/** @file\r
2 This library is used by other modules to measure data to TPM.\r
3\r
4Copyright (c) 2012 - 2013, Intel Corporation. All rights reserved. <BR>\r
5This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include <PiDxe.h>\r
16\r
17#include <Protocol/TcgService.h>\r
18#include <Protocol/TrEEProtocol.h>\r
19\r
20#include <Library/BaseMemoryLib.h>\r
21#include <Library/MemoryAllocationLib.h>\r
22#include <Library/UefiBootServicesTableLib.h>\r
23#include <Library/DebugLib.h>\r
24#include <Library/TpmMeasurementLib.h>\r
25\r
26#include <Guid/Acpi.h>\r
27#include <IndustryStandard/Acpi.h>\r
28\r
29\r
30\r
31/**\r
32 Tpm12 measure and log data, and extend the measurement result into a specific PCR.\r
33\r
34 @param[in] PcrIndex PCR Index.\r
35 @param[in] EventType Event type.\r
36 @param[in] EventLog Measurement event log.\r
37 @param[in] LogLen Event log length in bytes.\r
38 @param[in] HashData The start of the data buffer to be hashed, extended.\r
39 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData\r
40\r
41 @retval EFI_SUCCESS Operation completed successfully.\r
42 @retval EFI_UNSUPPORTED TPM device not available.\r
43 @retval EFI_OUT_OF_RESOURCES Out of memory.\r
44 @retval EFI_DEVICE_ERROR The operation was unsuccessful.\r
45**/\r
46EFI_STATUS\r
47Tpm12MeasureAndLogData (\r
48 IN UINT32 PcrIndex,\r
49 IN UINT32 EventType,\r
50 IN VOID *EventLog,\r
51 IN UINT32 LogLen,\r
52 IN VOID *HashData,\r
53 IN UINT64 HashDataLen\r
54 )\r
55{\r
56 EFI_STATUS Status;\r
57 EFI_TCG_PROTOCOL *TcgProtocol;\r
58 TCG_PCR_EVENT *TcgEvent;\r
59 EFI_PHYSICAL_ADDRESS EventLogLastEntry;\r
60 UINT32 EventNumber;\r
61\r
62 TcgEvent = NULL;\r
63\r
64 //\r
65 // Tpm active/deactive state is checked in HashLogExtendEvent\r
66 //\r
67 Status = gBS->LocateProtocol (&gEfiTcgProtocolGuid, NULL, (VOID **) &TcgProtocol);\r
68 if (EFI_ERROR(Status)){\r
69 return Status;\r
70 }\r
71\r
72 TcgEvent = (TCG_PCR_EVENT *)AllocateZeroPool (sizeof (TCG_PCR_EVENT_HDR) + LogLen);\r
73 if(TcgEvent == NULL) {\r
74 return EFI_OUT_OF_RESOURCES;\r
75 }\r
76\r
77 TcgEvent->PCRIndex = PcrIndex;\r
78 TcgEvent->EventType = EventType;\r
79 TcgEvent->EventSize = LogLen;\r
80 CopyMem (&TcgEvent->Event[0], EventLog, LogLen);\r
81 EventNumber = 1;\r
82 Status = TcgProtocol->HashLogExtendEvent (\r
83 TcgProtocol,\r
84 (EFI_PHYSICAL_ADDRESS)(UINTN)HashData,\r
85 HashDataLen,\r
86 TPM_ALG_SHA,\r
87 TcgEvent,\r
88 &EventNumber,\r
89 &EventLogLastEntry\r
90 );\r
91\r
92 FreePool (TcgEvent);\r
93\r
94 return Status;\r
95}\r
96\r
97/**\r
98 Tpm20 measure and log data, and extend the measurement result into a specific PCR.\r
99\r
100 @param[in] PcrIndex PCR Index.\r
101 @param[in] EventType Event type.\r
102 @param[in] EventLog Measurement event log.\r
103 @param[in] LogLen Event log length in bytes.\r
104 @param[in] HashData The start of the data buffer to be hashed, extended.\r
105 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData\r
106\r
107 @retval EFI_SUCCESS Operation completed successfully.\r
108 @retval EFI_UNSUPPORTED TPM device not available.\r
109 @retval EFI_OUT_OF_RESOURCES Out of memory.\r
110 @retval EFI_DEVICE_ERROR The operation was unsuccessful.\r
111**/\r
112EFI_STATUS\r
113Tpm20MeasureAndLogData (\r
114 IN UINT32 PcrIndex,\r
115 IN UINT32 EventType,\r
116 IN VOID *EventLog,\r
117 IN UINT32 LogLen,\r
118 IN VOID *HashData,\r
119 IN UINT64 HashDataLen\r
120 )\r
121{\r
122 EFI_STATUS Status;\r
123 EFI_TREE_PROTOCOL *TreeProtocol;\r
124 TrEE_EVENT *TreeEvent;\r
125\r
126 //\r
127 // TrEEPresentFlag is checked in HashLogExtendEvent\r
128 //\r
129 Status = gBS->LocateProtocol (&gEfiTrEEProtocolGuid, NULL, (VOID **) &TreeProtocol);\r
130 if (EFI_ERROR (Status)) {\r
131 return Status;\r
132 }\r
133\r
134 TreeEvent = (TrEE_EVENT *) AllocateZeroPool (LogLen + sizeof (TrEE_EVENT));\r
135 if(TreeEvent == NULL) {\r
136 return EFI_OUT_OF_RESOURCES;\r
137 }\r
138\r
139 TreeEvent->Size = (UINT32)LogLen + sizeof (TrEE_EVENT) - sizeof(TreeEvent->Event);\r
140 TreeEvent->Header.HeaderSize = sizeof(TrEE_EVENT_HEADER);\r
141 TreeEvent->Header.HeaderVersion = TREE_EVENT_HEADER_VERSION;\r
142 TreeEvent->Header.PCRIndex = PcrIndex;\r
143 TreeEvent->Header.EventType = EventType;\r
144 CopyMem (&TreeEvent->Event[0], EventLog, LogLen);\r
145\r
146 Status = TreeProtocol->HashLogExtendEvent (\r
147 TreeProtocol,\r
148 0,\r
149 (EFI_PHYSICAL_ADDRESS)(UINTN)HashData,\r
150 HashDataLen,\r
151 TreeEvent\r
152 );\r
153 FreePool (TreeEvent);\r
154\r
155 return Status;\r
156}\r
157\r
158/**\r
159 Tpm measure and log data, and extend the measurement result into a specific PCR.\r
160\r
161 @param[in] PcrIndex PCR Index.\r
162 @param[in] EventType Event type.\r
163 @param[in] EventLog Measurement event log.\r
164 @param[in] LogLen Event log length in bytes.\r
165 @param[in] HashData The start of the data buffer to be hashed, extended.\r
166 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData\r
167\r
168 @retval EFI_SUCCESS Operation completed successfully.\r
169 @retval EFI_UNSUPPORTED TPM device not available.\r
170 @retval EFI_OUT_OF_RESOURCES Out of memory.\r
171 @retval EFI_DEVICE_ERROR The operation was unsuccessful.\r
172**/\r
173EFI_STATUS\r
174EFIAPI \r
175TpmMeasureAndLogData (\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\r
186 //\r
187 // Try to measure using Tpm1.2 protocol\r
188 //\r
189 Status = Tpm12MeasureAndLogData(\r
190 PcrIndex,\r
191 EventType,\r
192 EventLog,\r
193 LogLen,\r
194 HashData,\r
195 HashDataLen\r
196 );\r
197 if (EFI_ERROR (Status)) {\r
198 //\r
199 // Try to measure using Tpm20 protocol\r
200 //\r
201 Status = Tpm20MeasureAndLogData(\r
202 PcrIndex,\r
203 EventType,\r
204 EventLog,\r
205 LogLen,\r
206 HashData,\r
207 HashDataLen\r
208 );\r
209 }\r
210\r
211 return Status;\r
212}\r