From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga06.intel.com (mga06.intel.com [134.134.136.31]) by mx.groups.io with SMTP id smtpd.web10.7118.1635397215257585054 for ; Wed, 27 Oct 2021 22:00:15 -0700 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: intel.com, ip: 134.134.136.31, mailfrom: min.m.xu@intel.com) X-IronPort-AV: E=McAfee;i="6200,9189,10150"; a="291160696" X-IronPort-AV: E=Sophos;i="5.87,188,1631602800"; d="scan'208";a="291160696" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Oct 2021 22:00:14 -0700 X-IronPort-AV: E=Sophos;i="5.87,188,1631602800"; d="scan'208";a="498214407" Received: from mxu9-mobl1.ccr.corp.intel.com ([10.238.4.37]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Oct 2021 22:00:12 -0700 From: "Min Xu" To: devel@edk2.groups.io Cc: Min Xu , Michael D Kinney , Liming Gao , Zhiguang Liu , Jiewen Yao , Jian J Wang , Sami Mujawar Subject: [PATCH V3 3/3] SecurityPkg: Support TeeMeasurementProtocol in DxeTpmMeasurementLib Date: Thu, 28 Oct 2021 12:59:29 +0800 Message-Id: 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 TeeMeasurementProtocol is introduced, TD based measurement needs to be supported in DxeTpmMeasurementLib as well. In TpmMeasureAndLogData, TEE based measurement will be first called. If it failed, TPM based measurement will be called sequentially. Currently there is an assumption that TEE 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 Cc: Sami Mujawar Signed-off-by: Min Xu --- .../DxeTpmMeasurementLib.c | 88 ++++++++++++++++++- .../DxeTpmMeasurementLib.inf | 5 +- 2 files changed, 88 insertions(+), 5 deletions(-) diff --git a/SecurityPkg/Library/DxeTpmMeasurementLib/DxeTpmMeasurementLib.c b/SecurityPkg/Library/DxeTpmMeasurementLib/DxeTpmMeasurementLib.c index 061136ee7860..1b856cfc7a0d 100644 --- a/SecurityPkg/Library/DxeTpmMeasurementLib/DxeTpmMeasurementLib.c +++ b/SecurityPkg/Library/DxeTpmMeasurementLib/DxeTpmMeasurementLib.c @@ -19,7 +19,8 @@ SPDX-License-Identifier: BSD-2-Clause-Patent #include #include - +#include +#include /** @@ -149,6 +150,73 @@ Tpm20MeasureAndLogData ( return Status; } +/** + Tee measure and log data, and extend the measurement result into a + specific TEE MR. + + @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 +TeeMeasureAndLogData ( + IN UINT32 PcrIndex, + IN UINT32 EventType, + IN VOID *EventLog, + IN UINT32 LogLen, + IN VOID *HashData, + IN UINT64 HashDataLen + ) +{ + EFI_STATUS Status; + EFI_TEE_MEASUREMENT_PROTOCOL *TeeProtocol; + EFI_TEE_EVENT *EfiTeeEvent; + UINT32 MrIndex; + + Status = gBS->LocateProtocol (&gEfiTeeMeasurementProtocolGuid, NULL, (VOID **) &TeeProtocol); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = TeeProtocol->MapPcrToMrIndex (TeeProtocol, PcrIndex, &MrIndex); + if (EFI_ERROR (Status)) { + return EFI_INVALID_PARAMETER; + } + + EfiTeeEvent = (EFI_TEE_EVENT *) AllocateZeroPool (LogLen + sizeof (EFI_TEE_EVENT)); + if(EfiTeeEvent == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + EfiTeeEvent->Size = (UINT32) LogLen + sizeof (EFI_TEE_EVENT) - sizeof (EfiTeeEvent->Event); + EfiTeeEvent->Header.HeaderSize = sizeof (EFI_TEE_EVENT_HEADER); + EfiTeeEvent->Header.HeaderVersion = EFI_TEE_EVENT_HEADER_VERSION; + EfiTeeEvent->Header.MrIndex = MrIndex; + EfiTeeEvent->Header.EventType = EventType; + CopyMem (&EfiTeeEvent->Event[0], EventLog, LogLen); + + Status = TeeProtocol->HashLogExtendEvent ( + TeeProtocol, + 0, + (EFI_PHYSICAL_ADDRESS) (UINTN) HashData, + HashDataLen, + EfiTeeEvent + ); + FreePool (EfiTeeEvent); + + return Status; +} + + /** Tpm measure and log data, and extend the measurement result into a specific PCR. @@ -178,9 +246,9 @@ TpmMeasureAndLogData ( EFI_STATUS Status; // - // Try to measure using Tpm20 protocol + // Try to measure using Tee measurement protocol // - Status = Tpm20MeasureAndLogData( + Status = TeeMeasureAndLogData ( PcrIndex, EventType, EventLog, @@ -189,6 +257,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..501d5d66e0fb 100644 --- a/SecurityPkg/Library/DxeTpmMeasurementLib/DxeTpmMeasurementLib.inf +++ b/SecurityPkg/Library/DxeTpmMeasurementLib/DxeTpmMeasurementLib.inf @@ -40,5 +40,6 @@ UefiBootServicesTableLib [Protocols] - gEfiTcgProtocolGuid ## SOMETIMES_CONSUMES - gEfiTcg2ProtocolGuid ## SOMETIMES_CONSUMES + gEfiTcgProtocolGuid ## SOMETIMES_CONSUMES + gEfiTcg2ProtocolGuid ## SOMETIMES_CONSUMES + gEfiTeeMeasurementProtocolGuid ## SOMETIMES_CONSUMES -- 2.29.2.windows.2