public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Zhang, Chao B" <chao.b.zhang@intel.com>
To: "Zeng, Star" <star.zeng@intel.com>,
	"edk2-devel@lists.01.org" <edk2-devel@lists.01.org>
Cc: "Yao, Jiewen" <jiewen.yao@intel.com>
Subject: Re: [PATCH 6/6] SecuriryPkg/TPM2: Extract GetSupportedAndActivePcrs to Tpm2CommandLib
Date: Wed, 21 Sep 2016 06:05:39 +0000	[thread overview]
Message-ID: <FF72C7E4248F3C4E9BDF19D4918E90F2472BB41F@shsmsx102.ccr.corp.intel.com> (raw)
In-Reply-To: <1474368693-180984-7-git-send-email-star.zeng@intel.com>

Serial reviewed by: Chao Zhang <chao.b.zhang@intel.com>




Thanks & Best regards
Chao Zhang

-----Original Message-----
From: Zeng, Star 
Sent: Tuesday, September 20, 2016 6:52 PM
To: edk2-devel@lists.01.org
Cc: Yao, Jiewen; Zhang, Chao B; Zeng, Star
Subject: [PATCH 6/6] SecuriryPkg/TPM2: Extract GetSupportedAndActivePcrs to Tpm2CommandLib

From: Jiewen Yao <jiewen.yao@intel.com>

This patch extracts function Tpm2GetCapabilitySupportedAndActivePcrs()
from drivers and also update Tcg2ExecutePhysicalPresence() to call
Tpm2GetCapabilitySupportedAndActivePcrs() instead of
Tcg2Protocol->GetCapability to query the TPM to determine which
hashing algorithms are supported.

Cc: Chao B Zhang <chao.b.zhang@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Jiewen Yao <jiewen.yao@intel.com>
Signed-off-by: Star Zeng <star.zeng@intel.com>
---
 SecurityPkg/Include/Library/Tpm2CommandLib.h       | 18 +++++
 .../DxeTcg2PhysicalPresenceLib.c                   | 17 ++--
 .../Library/Tpm2CommandLib/Tpm2Capability.c        | 92 ++++++++++++++++++++++
 SecurityPkg/Tcg/Tcg2Dxe/Tcg2Dxe.c                  | 49 +-----------
 4 files changed, 118 insertions(+), 58 deletions(-)

diff --git a/SecurityPkg/Include/Library/Tpm2CommandLib.h b/SecurityPkg/Include/Library/Tpm2CommandLib.h
index 825ffc37a466..9a1dd8d8aceb 100644
--- a/SecurityPkg/Include/Library/Tpm2CommandLib.h
+++ b/SecurityPkg/Include/Library/Tpm2CommandLib.h
@@ -763,6 +763,24 @@ Tpm2GetCapabilityPcrs (
   );
 
 /**
+  This function will query the TPM to determine which hashing algorithms
+  are supported and which PCR banks are currently active.
+
+  @param[out]  TpmHashAlgorithmBitmap A bitmask containing the algorithms supported by the TPM.
+  @param[out]  ActivePcrBanks         A bitmask containing the PCRs currently allocated.
+
+  @retval     EFI_SUCCESS   TPM was successfully queried and return values can be trusted.
+  @retval     Others        An error occurred, likely in communication with the TPM.
+
+**/
+EFI_STATUS
+EFIAPI
+Tpm2GetCapabilitySupportedAndActivePcrs(
+  OUT UINT32                            *TpmHashAlgorithmBitmap,
+  OUT UINT32                            *ActivePcrBanks
+  );
+
+/**
   This command returns the information of TPM AlgorithmSet.
 
   This function parse the value got from TPM2_GetCapability and return the AlgorithmSet.
diff --git a/SecurityPkg/Library/DxeTcg2PhysicalPresenceLib/DxeTcg2PhysicalPresenceLib.c b/SecurityPkg/Library/DxeTcg2PhysicalPresenceLib/DxeTcg2PhysicalPresenceLib.c
index d1ed7e83c7ae..4cec0f75278b 100644
--- a/SecurityPkg/Library/DxeTcg2PhysicalPresenceLib/DxeTcg2PhysicalPresenceLib.c
+++ b/SecurityPkg/Library/DxeTcg2PhysicalPresenceLib/DxeTcg2PhysicalPresenceLib.c
@@ -160,17 +160,10 @@ Tcg2ExecutePhysicalPresence (
   )
 {
   EFI_STATUS                        Status;
-  EFI_TCG2_PROTOCOL                 *Tcg2Protocol;
-  EFI_TCG2_BOOT_SERVICE_CAPABILITY  ProtocolCapability;
-
-  Status = gBS->LocateProtocol (&gEfiTcg2ProtocolGuid, NULL, (VOID **) &Tcg2Protocol);
-  ASSERT_EFI_ERROR (Status);
+  EFI_TCG2_EVENT_ALGORITHM_BITMAP   TpmHashAlgorithmBitmap;
+  UINT32                            ActivePcrBanks;
 
-  ProtocolCapability.Size = sizeof(ProtocolCapability);
-  Status = Tcg2Protocol->GetCapability (
-                           Tcg2Protocol,
-                           &ProtocolCapability
-                           );
+  Status = Tpm2GetCapabilitySupportedAndActivePcrs (&TpmHashAlgorithmBitmap, &ActivePcrBanks);
   ASSERT_EFI_ERROR (Status);
 
   switch (CommandCode) {
@@ -194,7 +187,7 @@ Tcg2ExecutePhysicalPresence (
       return TCG_PP_OPERATION_RESPONSE_SUCCESS;
 
     case TCG2_PHYSICAL_PRESENCE_SET_PCR_BANKS:
-      Status = Tpm2PcrAllocateBanks (PlatformAuth, ProtocolCapability.HashAlgorithmBitmap, CommandParameter);
+      Status = Tpm2PcrAllocateBanks (PlatformAuth, TpmHashAlgorithmBitmap, CommandParameter);
       if (EFI_ERROR (Status)) {
         return TCG_PP_OPERATION_RESPONSE_BIOS_FAILURE;
       } else {
@@ -210,7 +203,7 @@ Tcg2ExecutePhysicalPresence (
       }
 
     case TCG2_PHYSICAL_PRESENCE_LOG_ALL_DIGESTS:
-      Status = Tpm2PcrAllocateBanks (PlatformAuth, ProtocolCapability.HashAlgorithmBitmap, ProtocolCapability.HashAlgorithmBitmap);
+      Status = Tpm2PcrAllocateBanks (PlatformAuth, TpmHashAlgorithmBitmap, TpmHashAlgorithmBitmap);
       if (EFI_ERROR (Status)) {
         return TCG_PP_OPERATION_RESPONSE_BIOS_FAILURE;
       } else {
diff --git a/SecurityPkg/Library/Tpm2CommandLib/Tpm2Capability.c b/SecurityPkg/Library/Tpm2CommandLib/Tpm2Capability.c
index 0fe2c367d269..9aab17f9345f 100644
--- a/SecurityPkg/Library/Tpm2CommandLib/Tpm2Capability.c
+++ b/SecurityPkg/Library/Tpm2CommandLib/Tpm2Capability.c
@@ -486,6 +486,98 @@ Tpm2GetCapabilityPcrs (
 }
 
 /**
+  This function will query the TPM to determine which hashing algorithms
+  are supported and which PCR banks are currently active.
+
+  @param[out]  TpmHashAlgorithmBitmap A bitmask containing the algorithms supported by the TPM.
+  @param[out]  ActivePcrBanks         A bitmask containing the PCRs currently allocated.
+
+  @retval     EFI_SUCCESS   TPM was successfully queried and return values can be trusted.
+  @retval     Others        An error occurred, likely in communication with the TPM.
+
+**/
+EFI_STATUS
+EFIAPI
+Tpm2GetCapabilitySupportedAndActivePcrs (
+  OUT UINT32                            *TpmHashAlgorithmBitmap,
+  OUT UINT32                            *ActivePcrBanks
+  )
+{
+  EFI_STATUS            Status;
+  TPML_PCR_SELECTION    Pcrs;
+  UINTN                 Index;
+
+  //
+  // Get supported PCR and current Active PCRs.
+  //
+  Status = Tpm2GetCapabilityPcrs (&Pcrs);
+
+  //
+  // If error, assume that we have at least SHA-1 (and return the error.)
+  //
+  if (EFI_ERROR (Status)) {
+    DEBUG ((EFI_D_ERROR, "GetSupportedAndActivePcrs - Tpm2GetCapabilityPcrs fail!\n"));
+    *TpmHashAlgorithmBitmap = HASH_ALG_SHA1;
+    *ActivePcrBanks         = HASH_ALG_SHA1;
+  }
+  //
+  // Otherwise, process the return data to determine what algorithms are supported
+  // and currently allocated.
+  //
+  else {
+    DEBUG ((EFI_D_INFO, "GetSupportedAndActivePcrs - Count = %08x\n", Pcrs.count));
+    *TpmHashAlgorithmBitmap = 0;
+    *ActivePcrBanks         = 0;
+    for (Index = 0; Index < Pcrs.count; Index++) {
+      switch (Pcrs.pcrSelections[Index].hash) {
+      case TPM_ALG_SHA1:
+        DEBUG ((EFI_D_VERBOSE, "GetSupportedAndActivePcrs - HASH_ALG_SHA1 present.\n"));
+        *TpmHashAlgorithmBitmap |= HASH_ALG_SHA1;
+        if (!IsZeroBuffer (Pcrs.pcrSelections[Index].pcrSelect, Pcrs.pcrSelections[Index].sizeofSelect)) {
+          DEBUG ((EFI_D_VERBOSE, "GetSupportedAndActivePcrs - HASH_ALG_SHA1 active.\n"));
+          *ActivePcrBanks |= HASH_ALG_SHA1;
+        }
+        break;
+      case TPM_ALG_SHA256:
+        DEBUG ((EFI_D_VERBOSE, "GetSupportedAndActivePcrs - HASH_ALG_SHA256 present.\n"));
+        *TpmHashAlgorithmBitmap |= HASH_ALG_SHA256;
+        if (!IsZeroBuffer (Pcrs.pcrSelections[Index].pcrSelect, Pcrs.pcrSelections[Index].sizeofSelect)) {
+          DEBUG ((EFI_D_VERBOSE, "GetSupportedAndActivePcrs - HASH_ALG_SHA256 active.\n"));
+          *ActivePcrBanks |= HASH_ALG_SHA256;
+        }
+        break;
+      case TPM_ALG_SHA384:
+        DEBUG ((EFI_D_VERBOSE, "GetSupportedAndActivePcrs - HASH_ALG_SHA384 present.\n"));
+        *TpmHashAlgorithmBitmap |= HASH_ALG_SHA384;
+        if (!IsZeroBuffer (Pcrs.pcrSelections[Index].pcrSelect, Pcrs.pcrSelections[Index].sizeofSelect)) {
+          DEBUG ((EFI_D_VERBOSE, "GetSupportedAndActivePcrs - HASH_ALG_SHA384 active.\n"));
+          *ActivePcrBanks |= HASH_ALG_SHA384;
+        }
+        break;
+      case TPM_ALG_SHA512:
+        DEBUG ((EFI_D_VERBOSE, "GetSupportedAndActivePcrs - HASH_ALG_SHA512 present.\n"));
+        *TpmHashAlgorithmBitmap |= HASH_ALG_SHA512;
+        if (!IsZeroBuffer (Pcrs.pcrSelections[Index].pcrSelect, Pcrs.pcrSelections[Index].sizeofSelect)) {
+          DEBUG ((EFI_D_VERBOSE, "GetSupportedAndActivePcrs - HASH_ALG_SHA512 active.\n"));
+          *ActivePcrBanks |= HASH_ALG_SHA512;
+        }
+        break;
+      case TPM_ALG_SM3_256:
+        DEBUG ((EFI_D_VERBOSE, "GetSupportedAndActivePcrs - HASH_ALG_SM3_256 present.\n"));
+        *TpmHashAlgorithmBitmap |= HASH_ALG_SM3_256;
+        if (!IsZeroBuffer (Pcrs.pcrSelections[Index].pcrSelect, Pcrs.pcrSelections[Index].sizeofSelect)) {
+          DEBUG ((EFI_D_VERBOSE, "GetSupportedAndActivePcrs - HASH_ALG_SM3_256 active.\n"));
+          *ActivePcrBanks |= HASH_ALG_SM3_256;
+        }
+        break;
+      }
+    }
+  }
+
+  return Status;
+}
+
+/**
   This command returns the information of TPM AlgorithmSet.
 
   This function parse the value got from TPM2_GetCapability and return the AlgorithmSet.
diff --git a/SecurityPkg/Tcg/Tcg2Dxe/Tcg2Dxe.c b/SecurityPkg/Tcg/Tcg2Dxe/Tcg2Dxe.c
index f3cc47796448..db8d662f80dc 100644
--- a/SecurityPkg/Tcg/Tcg2Dxe/Tcg2Dxe.c
+++ b/SecurityPkg/Tcg/Tcg2Dxe/Tcg2Dxe.c
@@ -2222,7 +2222,6 @@ DriverEntry (
   VOID                              *Registration;
   UINT32                            MaxCommandSize;
   UINT32                            MaxResponseSize;
-  TPML_PCR_SELECTION                Pcrs;
   UINTN                             Index;
   EFI_TCG2_EVENT_ALGORITHM_BITMAP   TpmHashAlgorithmBitmap;
   UINT32                            ActivePCRBanks;
@@ -2292,51 +2291,9 @@ DriverEntry (
   //
   // Get supported PCR and current Active PCRs
   //
-  Status = Tpm2GetCapabilityPcrs (&Pcrs);
-  if (EFI_ERROR (Status)) {
-    DEBUG ((EFI_D_ERROR, "Tpm2GetCapabilityPcrs fail!\n"));
-    TpmHashAlgorithmBitmap = EFI_TCG2_BOOT_HASH_ALG_SHA1;
-    ActivePCRBanks = EFI_TCG2_BOOT_HASH_ALG_SHA1;
-  } else {
-    DEBUG ((EFI_D_INFO, "Tpm2GetCapabilityPcrs Count - %08x\n", Pcrs.count));
-    TpmHashAlgorithmBitmap = 0;
-    ActivePCRBanks = 0;
-    for (Index = 0; Index < Pcrs.count; Index++) {
-      DEBUG ((EFI_D_INFO, "hash - %x\n", Pcrs.pcrSelections[Index].hash));
-      switch (Pcrs.pcrSelections[Index].hash) {
-      case TPM_ALG_SHA1:
-        TpmHashAlgorithmBitmap |= EFI_TCG2_BOOT_HASH_ALG_SHA1;
-        if (!IsZeroBuffer (Pcrs.pcrSelections[Index].pcrSelect, Pcrs.pcrSelections[Index].sizeofSelect)) {
-          ActivePCRBanks |= EFI_TCG2_BOOT_HASH_ALG_SHA1;
-        }        
-        break;
-      case TPM_ALG_SHA256:
-        TpmHashAlgorithmBitmap |= EFI_TCG2_BOOT_HASH_ALG_SHA256;
-        if (!IsZeroBuffer (Pcrs.pcrSelections[Index].pcrSelect, Pcrs.pcrSelections[Index].sizeofSelect)) {
-          ActivePCRBanks |= EFI_TCG2_BOOT_HASH_ALG_SHA256;
-        }
-        break;
-      case TPM_ALG_SHA384:
-        TpmHashAlgorithmBitmap |= EFI_TCG2_BOOT_HASH_ALG_SHA384;
-        if (!IsZeroBuffer (Pcrs.pcrSelections[Index].pcrSelect, Pcrs.pcrSelections[Index].sizeofSelect)) {
-          ActivePCRBanks |= EFI_TCG2_BOOT_HASH_ALG_SHA384;
-        }
-        break;
-      case TPM_ALG_SHA512:
-        TpmHashAlgorithmBitmap |= EFI_TCG2_BOOT_HASH_ALG_SHA512;
-        if (!IsZeroBuffer (Pcrs.pcrSelections[Index].pcrSelect, Pcrs.pcrSelections[Index].sizeofSelect)) {
-          ActivePCRBanks |= EFI_TCG2_BOOT_HASH_ALG_SHA512;
-        }
-        break;
-      case TPM_ALG_SM3_256:
-        TpmHashAlgorithmBitmap |= EFI_TCG2_BOOT_HASH_ALG_SM3_256;
-        if (!IsZeroBuffer (Pcrs.pcrSelections[Index].pcrSelect, Pcrs.pcrSelections[Index].sizeofSelect)) {
-          ActivePCRBanks |= EFI_TCG2_BOOT_HASH_ALG_SM3_256;
-        }
-        break;
-      }
-    }
-  }
+  Status = Tpm2GetCapabilitySupportedAndActivePcrs (&TpmHashAlgorithmBitmap, &ActivePCRBanks);
+  ASSERT_EFI_ERROR (Status);
+
   mTcgDxeData.BsCap.HashAlgorithmBitmap = TpmHashAlgorithmBitmap & PcdGet32 (PcdTcg2HashAlgorithmBitmap);
   mTcgDxeData.BsCap.ActivePcrBanks = ActivePCRBanks & PcdGet32 (PcdTcg2HashAlgorithmBitmap);
 
-- 
2.7.0.windows.1



  reply	other threads:[~2016-09-21  6:07 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-20 10:51 [PATCH 0/6] Move/Extract generic duplicated code to Tpm2CommandLib Star Zeng
2016-09-20 10:51 ` [PATCH 1/6] SecuriryPkg/TPM2: Move Tpm2PcrAllocateBanks() " Star Zeng
2016-09-20 10:51 ` [PATCH 2/6] SecuriryPkg/TPM2: Move GetDigestFromDigestList() " Star Zeng
2016-09-20 10:51 ` [PATCH 3/6] SecuriryPkg/TPM2: Update function header of GetDigestFromDigestList() Star Zeng
2016-09-20 10:51 ` [PATCH 4/6] SecuriryPkg/TPM2: Move GetDigestListSize() to Tpm2CommandLib Star Zeng
2016-09-20 10:51 ` [PATCH 5/6] SecuriryPkg/TPM2: Move CopyDigestListToBuffer() " Star Zeng
2016-09-20 10:51 ` [PATCH 6/6] SecuriryPkg/TPM2: Extract GetSupportedAndActivePcrs " Star Zeng
2016-09-21  6:05   ` Zhang, Chao B [this message]
2016-09-20 10:58 ` [PATCH 0/6] Move/Extract generic duplicated code " Zeng, Star
2016-09-20 14:47 ` Yao, Jiewen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=FF72C7E4248F3C4E9BDF19D4918E90F2472BB41F@shsmsx102.ccr.corp.intel.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox