DxeTpmMeasurementLib.c 6.2 KB

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