From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by mx.groups.io with SMTP id smtpd.web10.5168.1633670487432256251 for ; Thu, 07 Oct 2021 22:21:33 -0700 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: intel.com, ip: 134.134.136.100, mailfrom: min.m.xu@intel.com) X-IronPort-AV: E=McAfee;i="6200,9189,10130"; a="289934969" X-IronPort-AV: E=Sophos;i="5.85,356,1624345200"; d="scan'208";a="289934969" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Oct 2021 22:21:33 -0700 X-IronPort-AV: E=Sophos;i="5.85,356,1624345200"; d="scan'208";a="478844444" Received: from mxu9-mobl1.ccr.corp.intel.com ([10.238.4.37]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Oct 2021 22:21:31 -0700 From: "Min Xu" To: devel@edk2.groups.io Cc: Min Xu , Michael D Kinney , Liming Gao , Zhiguang Liu , Jiewen Yao , Jian J Wang Subject: [PATCH V2 3/3] SecurityPkg: Support TdProtocol in DxeTpmMeasurementLib Date: Fri, 8 Oct 2021 13:21:14 +0800 Message-Id: <844e9fe333a90c76c923c541e8439716a2d949d5.1633661591.git.min.m.xu@intel.com> X-Mailer: git-send-email 2.29.2.windows.2 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3625 DxeTpmMeasurementLib supports TPM based measurement in DXE phase. After Td protocol is introduced, TD based measurement needs to be supported in DxeTpmMeasurementLib as well. In TpmMeasureAndLogData, TD based measurement will be first called. If it failed, TPM based measurement will be called sequentially. Currently there is an assumption that TD based measurement and TPM based measurement won't be exist at the same time.If the assumption is not true in the future, we will revisit here then. Cc: Michael D Kinney Cc: Liming Gao Cc: Zhiguang Liu Cc: Jiewen Yao Cc: Jian J Wang Signed-off-by: Min Xu --- .../DxeTpmMeasurementLib.c | 87 ++++++++++++++++++- .../DxeTpmMeasurementLib.inf | 1 + 2 files changed, 85 insertions(+), 3 deletions(-) diff --git a/SecurityPkg/Library/DxeTpmMeasurementLib/DxeTpmMeasurementLib.c b/SecurityPkg/Library/DxeTpmMeasurementLib/DxeTpmMeasurementLib.c index 061136ee7860..f8cd289ba62c 100644 --- a/SecurityPkg/Library/DxeTpmMeasurementLib/DxeTpmMeasurementLib.c +++ b/SecurityPkg/Library/DxeTpmMeasurementLib/DxeTpmMeasurementLib.c @@ -19,7 +19,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent #include #include - +#include /** @@ -149,6 +149,73 @@ Tpm20MeasureAndLogData ( return Status; } +/** + Tdx measure and log data, and extend the measurement result into a + specific TDX RTMR. + + @param[in] PcrIndex PCR Index. + @param[in] EventType Event type. + @param[in] EventLog Measurement event log. + @param[in] LogLen Event log length in bytes. + @param[in] HashData The start of the data buffer to be hashed, extended. + @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData + + @retval EFI_SUCCESS Operation completed successfully. + @retval EFI_UNSUPPORTED Tdx device not available. + @retval EFI_OUT_OF_RESOURCES Out of memory. + @retval EFI_DEVICE_ERROR The operation was unsuccessful. +**/ +EFI_STATUS +EFIAPI +TdxMeasureAndLogData ( + IN UINT32 PcrIndex, + IN UINT32 EventType, + IN VOID *EventLog, + IN UINT32 LogLen, + IN VOID *HashData, + IN UINT64 HashDataLen + ) +{ + EFI_STATUS Status; + EFI_TD_PROTOCOL *TdProtocol; + EFI_TD_EVENT *TdEvent; + UINT32 MrIndex; + + Status = gBS->LocateProtocol (&gEfiTdProtocolGuid, NULL, (VOID **) &TdProtocol); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = TdProtocol->MapPcrToMrIndex (TdProtocol, PcrIndex, &MrIndex); + if (EFI_ERROR (Status)) { + return EFI_INVALID_PARAMETER; + } + + TdEvent = (EFI_TD_EVENT *) AllocateZeroPool (LogLen + sizeof (EFI_TD_EVENT)); + if(TdEvent == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + TdEvent->Size = (UINT32) LogLen + sizeof (EFI_TD_EVENT) - sizeof (TdEvent->Event); + TdEvent->Header.HeaderSize = sizeof (EFI_TD_EVENT_HEADER); + TdEvent->Header.HeaderVersion = EFI_TD_EVENT_HEADER_VERSION; + TdEvent->Header.MrIndex = MrIndex; + TdEvent->Header.EventType = EventType; + CopyMem (&TdEvent->Event[0], EventLog, LogLen); + + Status = TdProtocol->HashLogExtendEvent ( + TdProtocol, + 0, + (EFI_PHYSICAL_ADDRESS) (UINTN) HashData, + HashDataLen, + TdEvent + ); + FreePool (TdEvent); + + return Status; +} + + /** Tpm measure and log data, and extend the measurement result into a specific PCR. @@ -178,9 +245,9 @@ TpmMeasureAndLogData ( EFI_STATUS Status; // - // Try to measure using Tpm20 protocol + // Try to measure using Td protocol // - Status = Tpm20MeasureAndLogData( + Status = TdxMeasureAndLogData ( PcrIndex, EventType, EventLog, @@ -189,6 +256,20 @@ TpmMeasureAndLogData ( HashDataLen ); + if (EFI_ERROR (Status)) { + // + // Try to measure using Tpm20 protocol + // + Status = Tpm20MeasureAndLogData( + PcrIndex, + EventType, + EventLog, + LogLen, + HashData, + HashDataLen + ); + } + if (EFI_ERROR (Status)) { // // Try to measure using Tpm1.2 protocol diff --git a/SecurityPkg/Library/DxeTpmMeasurementLib/DxeTpmMeasurementLib.inf b/SecurityPkg/Library/DxeTpmMeasurementLib/DxeTpmMeasurementLib.inf index 7d41bc41f95d..b919771d5a9e 100644 --- a/SecurityPkg/Library/DxeTpmMeasurementLib/DxeTpmMeasurementLib.inf +++ b/SecurityPkg/Library/DxeTpmMeasurementLib/DxeTpmMeasurementLib.inf @@ -42,3 +42,4 @@ [Protocols] gEfiTcgProtocolGuid ## SOMETIMES_CONSUMES gEfiTcg2ProtocolGuid ## SOMETIMES_CONSUMES + gEfiTdProtocolGuid ## SOMETIMES_CONSUMES -- 2.29.2.windows.2