]> git.proxmox.com Git - mirror_edk2.git/blobdiff - SecurityPkg/Library/Tpm2CommandLib/Tpm2Help.c
SecurityPkg: Refine type cast for pointer subtraction
[mirror_edk2.git] / SecurityPkg / Library / Tpm2CommandLib / Tpm2Help.c
index 95d4f7c84ce965dc4f3091f6fe0a8cf926ca0da3..180f36083096f86e76011bb195e0aa9c89fb8387 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Implement TPM2 help.\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
@@ -22,14 +22,15 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 typedef struct {\r
   TPMI_ALG_HASH              HashAlgo;\r
   UINT16                     HashSize;\r
+  UINT32                     HashMask;\r
 } INTERNAL_HASH_INFO;\r
 \r
 STATIC INTERNAL_HASH_INFO mHashInfo[] = {\r
-  {TPM_ALG_SHA1,          SHA1_DIGEST_SIZE},\r
-  {TPM_ALG_SHA256,        SHA256_DIGEST_SIZE},\r
-  {TPM_ALG_SM3_256,       SM3_256_DIGEST_SIZE},\r
-  {TPM_ALG_SHA384,        SHA384_DIGEST_SIZE},\r
-  {TPM_ALG_SHA512,        SHA512_DIGEST_SIZE},\r
+  {TPM_ALG_SHA1,          SHA1_DIGEST_SIZE,     HASH_ALG_SHA1},\r
+  {TPM_ALG_SHA256,        SHA256_DIGEST_SIZE,   HASH_ALG_SHA256},\r
+  {TPM_ALG_SM3_256,       SM3_256_DIGEST_SIZE,  HASH_ALG_SM3_256},\r
+  {TPM_ALG_SHA384,        SHA384_DIGEST_SIZE,   HASH_ALG_SHA384},\r
+  {TPM_ALG_SHA512,        SHA512_DIGEST_SIZE,   HASH_ALG_SHA512},\r
 };\r
 \r
 /**\r
@@ -55,6 +56,29 @@ GetHashSizeFromAlgo (
   return 0;\r
 }\r
 \r
+/**\r
+  Get hash mask from algorithm.\r
+\r
+  @param[in] HashAlgo   Hash algorithm\r
+\r
+  @return Hash mask\r
+**/\r
+UINT32\r
+EFIAPI\r
+GetHashMaskFromAlgo (\r
+  IN TPMI_ALG_HASH     HashAlgo\r
+  )\r
+{\r
+  UINTN  Index;\r
+\r
+  for (Index = 0; Index < sizeof(mHashInfo)/sizeof(mHashInfo[0]); Index++) {\r
+    if (mHashInfo[Index].HashAlgo == HashAlgo) {\r
+      return mHashInfo[Index].HashMask;\r
+    }\r
+  }\r
+  return 0;\r
+}\r
+\r
 /**\r
   Copy AuthSessionIn to TPM2 command buffer.\r
 \r
@@ -117,7 +141,7 @@ CopyAuthSessionCommand (
     Buffer += sizeof(UINT16);\r
   }\r
 \r
-  return (UINT32)(UINTN)(Buffer - (UINT8 *)AuthSessionOut);\r
+  return (UINT32)((UINTN)Buffer - (UINTN)AuthSessionOut);\r
 }\r
 \r
 /**\r
@@ -162,7 +186,7 @@ CopyAuthSessionResponse (
   CopyMem (AuthSessionOut->hmac.buffer, Buffer, AuthSessionOut->hmac.size);\r
   Buffer += AuthSessionOut->hmac.size;\r
 \r
-  return (UINT32)(UINTN)(Buffer - (UINT8 *)AuthSessionIn);\r
+  return (UINT32)((UINTN)Buffer - (UINTN)AuthSessionIn);\r
 }\r
 \r
 /**\r
@@ -215,7 +239,7 @@ IsHashAlgSupportedInHashAlgorithmMask(
 /**\r
   Copy TPML_DIGEST_VALUES into a buffer\r
 \r
-  @param[in,out] Buffer             Buffer to hold TPML_DIGEST_VALUES.\r
+  @param[in,out] Buffer             Buffer to hold copied TPML_DIGEST_VALUES compact binary.\r
   @param[in]     DigestList         TPML_DIGEST_VALUES to be copied.\r
   @param[in]     HashAlgorithmMask  HASH bits corresponding to the desired digests to copy.\r
 \r