--- /dev/null
+/** @file\r
+ Implement TPM2 help.\r
+\r
+Copyright (c) 2013, 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
+http://opensource.org/licenses/bsd-license.php\r
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+#include <IndustryStandard/UefiTcgPlatform.h>\r
+#include <Library/Tpm2CommandLib.h>\r
+#include <Library/Tpm2DeviceLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/BaseLib.h>\r
+#include <Library/DebugLib.h>\r
+\r
+typedef struct {\r
+ TPMI_ALG_HASH HashAlgo;\r
+ UINT16 HashSize;\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
+};\r
+\r
+/**\r
+ Return size of digest.\r
+\r
+ @param[in] HashAlgo Hash algorithm\r
+\r
+ @return size of digest\r
+**/\r
+UINT16\r
+EFIAPI\r
+GetHashSizeFromAlgo (\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].HashSize;\r
+ }\r
+ }\r
+ return 0;\r
+}\r
+\r
+/**\r
+ Copy AuthSessionIn to TPM2 command buffer.\r
+\r
+ @param [in] AuthSessionIn Input AuthSession data\r
+ @param [out] AuthSessionOut Output AuthSession data in TPM2 command buffer\r
+\r
+ @return AuthSession size\r
+**/\r
+UINT32\r
+EFIAPI\r
+CopyAuthSessionCommand (\r
+ IN TPMS_AUTH_COMMAND *AuthSessionIn, OPTIONAL\r
+ OUT UINT8 *AuthSessionOut\r
+ )\r
+{\r
+ UINT8 *Buffer;\r
+\r
+ Buffer = (UINT8 *)AuthSessionOut;\r
+ \r
+ //\r
+ // Add in Auth session\r
+ //\r
+ if (AuthSessionIn != NULL) {\r
+ // sessionHandle\r
+ WriteUnaligned32 ((UINT32 *)Buffer, SwapBytes32(AuthSessionIn->sessionHandle));\r
+ Buffer += sizeof(UINT32);\r
+\r
+ // nonce\r
+ WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (AuthSessionIn->nonce.size));\r
+ Buffer += sizeof(UINT16);\r
+\r
+ CopyMem (Buffer, AuthSessionIn->nonce.buffer, AuthSessionIn->nonce.size);\r
+ Buffer += AuthSessionIn->nonce.size;\r
+\r
+ // sessionAttributes\r
+ *(UINT8 *)Buffer = *(UINT8 *)&AuthSessionIn->sessionAttributes;\r
+ Buffer += sizeof(UINT8);\r
+\r
+ // hmac\r
+ WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (AuthSessionIn->hmac.size));\r
+ Buffer += sizeof(UINT16);\r
+\r
+ CopyMem (Buffer, AuthSessionIn->hmac.buffer, AuthSessionIn->hmac.size);\r
+ Buffer += AuthSessionIn->hmac.size;\r
+ } else {\r
+ // sessionHandle\r
+ WriteUnaligned32 ((UINT32 *)Buffer, SwapBytes32(TPM_RS_PW));\r
+ Buffer += sizeof(UINT32);\r
+\r
+ // nonce = nullNonce\r
+ WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16(0));\r
+ Buffer += sizeof(UINT16);\r
+\r
+ // sessionAttributes = 0\r
+ *(UINT8 *)Buffer = 0x00;\r
+ Buffer += sizeof(UINT8);\r
+\r
+ // hmac = nullAuth\r
+ WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16(0));\r
+ Buffer += sizeof(UINT16);\r
+ }\r
+\r
+ return (UINT32)(UINTN)(Buffer - (UINT8 *)AuthSessionOut);\r
+}\r
+\r
+/**\r
+ Copy AuthSessionIn from TPM2 response buffer.\r
+\r
+ @param [in] AuthSessionIn Input AuthSession data in TPM2 response buffer\r
+ @param [out] AuthSessionOut Output AuthSession data\r
+\r
+ @return AuthSession size\r
+**/\r
+UINT32\r
+EFIAPI\r
+CopyAuthSessionResponse (\r
+ IN UINT8 *AuthSessionIn,\r
+ OUT TPMS_AUTH_RESPONSE *AuthSessionOut OPTIONAL\r
+ )\r
+{\r
+ UINT8 *Buffer;\r
+ TPMS_AUTH_RESPONSE LocalAuthSessionOut;\r
+\r
+ if (AuthSessionOut == NULL) {\r
+ AuthSessionOut = &LocalAuthSessionOut;\r
+ }\r
+\r
+ Buffer = (UINT8 *)AuthSessionIn;\r
+\r
+ // nonce\r
+ AuthSessionOut->nonce.size = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));\r
+ Buffer += sizeof(UINT16);\r
+\r
+ CopyMem (AuthSessionOut->nonce.buffer, Buffer, AuthSessionOut->nonce.size);\r
+ Buffer += AuthSessionOut->nonce.size;\r
+\r
+ // sessionAttributes\r
+ *(UINT8 *)&AuthSessionOut->sessionAttributes = *(UINT8 *)Buffer;\r
+ Buffer += sizeof(UINT8);\r
+\r
+ // hmac\r
+ AuthSessionOut->hmac.size = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));\r
+ Buffer += sizeof(UINT16);\r
+\r
+ CopyMem (AuthSessionOut->hmac.buffer, Buffer, AuthSessionOut->hmac.size);\r
+ Buffer += AuthSessionOut->hmac.size;\r
+\r
+ return (UINT32)(UINTN)(Buffer - (UINT8 *)AuthSessionIn);\r
+}\r