]> git.proxmox.com Git - mirror_edk2.git/blobdiff - SecurityPkg/Library/HashLibBaseCryptoRouter/HashLibBaseCryptoRouterDxe.c
SecurityPkg HashLibRouter: Avoid incorrect PcdTcg2HashAlgorithmBitmap
[mirror_edk2.git] / SecurityPkg / Library / HashLibBaseCryptoRouter / HashLibBaseCryptoRouterDxe.c
index 3250c3a01a0c6c8abcfe9d6090f7b75378963fda..7bb5087550577764f1bcf6c9ef21629da39514ad 100644 (file)
@@ -3,7 +3,7 @@
   hash handler registerd, such as SHA1, SHA256.\r
   Platform can use PcdTpm2HashMask to mask some hash engines.\r
 \r
-Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved. <BR>\r
+Copyright (c) 2013 - 2017, Intel Corporation. All rights reserved. <BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
@@ -28,6 +28,30 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 HASH_INTERFACE   mHashInterface[HASH_COUNT] = {{{0}, NULL, NULL, NULL}};\r
 UINTN            mHashInterfaceCount = 0;\r
 \r
+UINT32           mSupportedHashMaskLast = 0;\r
+UINT32           mSupportedHashMaskCurrent = 0;\r
+\r
+/**\r
+  Check mismatch of supported HashMask between modules\r
+  that may link different HashInstanceLib instances.\r
+\r
+**/\r
+VOID\r
+CheckSupportedHashMaskMismatch (\r
+  VOID\r
+  )\r
+{\r
+  if (mSupportedHashMaskCurrent != mSupportedHashMaskLast) {\r
+    DEBUG ((\r
+      DEBUG_WARN,\r
+      "WARNING: There is mismatch of supported HashMask (0x%x - 0x%x) between modules\n",\r
+      mSupportedHashMaskCurrent,\r
+      mSupportedHashMaskLast\r
+      ));\r
+    DEBUG ((DEBUG_WARN, "that are linking different HashInstanceLib instances!\n"));\r
+  }\r
+}\r
+\r
 /**\r
   Start hash sequence.\r
 \r
@@ -50,6 +74,8 @@ HashStart (
     return EFI_UNSUPPORTED;\r
   }\r
 \r
+  CheckSupportedHashMaskMismatch ();\r
+\r
   HashCtx = AllocatePool (sizeof(*HashCtx) * mHashInterfaceCount);\r
   ASSERT (HashCtx != NULL);\r
 \r
@@ -90,6 +116,8 @@ HashUpdate (
     return EFI_UNSUPPORTED;\r
   }\r
 \r
+  CheckSupportedHashMaskMismatch ();\r
+\r
   HashCtx = (HASH_HANDLE *)HashHandle;\r
 \r
   for (Index = 0; Index < mHashInterfaceCount; Index++) {\r
@@ -133,6 +161,8 @@ HashCompleteAndExtend (
     return EFI_UNSUPPORTED;\r
   }\r
 \r
+  CheckSupportedHashMaskMismatch ();\r
+\r
   HashCtx = (HASH_HANDLE *)HashHandle;\r
   ZeroMem (DigestList, sizeof(*DigestList));\r
 \r
@@ -180,6 +210,8 @@ HashAndExtend (
     return EFI_UNSUPPORTED;\r
   }\r
 \r
+  CheckSupportedHashMaskMismatch ();\r
+\r
   HashStart (&HashHandle);\r
   HashUpdate (HashHandle, DataToHash, DataToHashLen);\r
   Status = HashCompleteAndExtend (HashHandle, PcrIndex, NULL, 0, DigestList);\r
@@ -204,7 +236,6 @@ RegisterHashInterfaceLib (
 {\r
   UINTN              Index;\r
   UINT32             HashMask;\r
-  UINT32             BiosSupportedHashMask;\r
   EFI_STATUS         Status;\r
 \r
   //\r
@@ -218,21 +249,58 @@ RegisterHashInterfaceLib (
   if (mHashInterfaceCount >= sizeof(mHashInterface)/sizeof(mHashInterface[0])) {\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
-  BiosSupportedHashMask = PcdGet32 (PcdTcg2HashAlgorithmBitmap);\r
-  Status = PcdSet32S (PcdTcg2HashAlgorithmBitmap, BiosSupportedHashMask | HashMask);\r
-  ASSERT_EFI_ERROR (Status);\r
 \r
   //\r
   // Check duplication\r
   //\r
   for (Index = 0; Index < mHashInterfaceCount; Index++) {\r
     if (CompareGuid (&mHashInterface[Index].HashGuid, &HashInterface->HashGuid)) {\r
+      DEBUG ((DEBUG_ERROR, "Hash Interface (%g) has been registered\n", &HashInterface->HashGuid));\r
       return EFI_ALREADY_STARTED;\r
     }\r
   }\r
 \r
+  //\r
+  // Record hash algorithm bitmap of CURRENT module which consumes HashLib.\r
+  //\r
+  mSupportedHashMaskCurrent = PcdGet32 (PcdTcg2HashAlgorithmBitmap) | HashMask;\r
+  Status = PcdSet32S (PcdTcg2HashAlgorithmBitmap, mSupportedHashMaskCurrent);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
   CopyMem (&mHashInterface[mHashInterfaceCount], HashInterface, sizeof(*HashInterface));\r
   mHashInterfaceCount ++;\r
   \r
   return EFI_SUCCESS;\r
-}
\ No newline at end of file
+}\r
+\r
+/**\r
+  The constructor function of HashLibBaseCryptoRouterDxe.\r
+  \r
+  @param  ImageHandle   The firmware allocated handle for the EFI image.\r
+  @param  SystemTable   A pointer to the EFI System Table.\r
+  \r
+  @retval EFI_SUCCESS   The constructor executed correctly.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+HashLibBaseCryptoRouterDxeConstructor (\r
+  IN EFI_HANDLE        ImageHandle,\r
+  IN EFI_SYSTEM_TABLE  *SystemTable\r
+  )\r
+{\r
+  EFI_STATUS    Status;\r
+\r
+  //\r
+  // Record hash algorithm bitmap of LAST module which also consumes HashLib.\r
+  //\r
+  mSupportedHashMaskLast = PcdGet32 (PcdTcg2HashAlgorithmBitmap);\r
+\r
+  //\r
+  // Set PcdTcg2HashAlgorithmBitmap to 0 in CONSTRUCTOR for CURRENT module.\r
+  //\r
+  Status = PcdSet32S (PcdTcg2HashAlgorithmBitmap, 0);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  return EFI_SUCCESS;\r
+}\r