From c0208dc79e71a4b01fb59a1580b1c2af23382804 Mon Sep 17 00:00:00 2001 From: rdiaz Date: Tue, 21 Apr 2026 19:16:27 +0000 Subject: [PATCH] SecurityPkg: Update Tpm2Help.c Functions Update the Tpm2Help.c functions to become wrappers for the functions in Tpm2HelpLib. This prevents platforms from breaking due to the updated prefix naming but will still allow us to keep one instance of the function implementations. Signed-off-by: Raymond Diaz --- SecurityPkg/Library/Tpm2CommandLib/Tpm2Help.c | 263 +----------------- 1 file changed, 11 insertions(+), 252 deletions(-) diff --git a/SecurityPkg/Library/Tpm2CommandLib/Tpm2Help.c b/SecurityPkg/Library/Tpm2CommandLib/Tpm2Help.c index ac802733d3..69d8b47736 100644 --- a/SecurityPkg/Library/Tpm2CommandLib/Tpm2Help.c +++ b/SecurityPkg/Library/Tpm2CommandLib/Tpm2Help.c @@ -9,24 +9,11 @@ SPDX-License-Identifier: BSD-2-Clause-Patent #include #include #include +#include #include #include #include -typedef struct { - TPMI_ALG_HASH HashAlgo; - UINT16 HashSize; - UINT32 HashMask; -} INTERNAL_HASH_INFO; - -STATIC INTERNAL_HASH_INFO mHashInfo[] = { - { TPM_ALG_SHA1, SHA1_DIGEST_SIZE, HASH_ALG_SHA1 }, - { TPM_ALG_SHA256, SHA256_DIGEST_SIZE, HASH_ALG_SHA256 }, - { TPM_ALG_SM3_256, SM3_256_DIGEST_SIZE, HASH_ALG_SM3_256 }, - { TPM_ALG_SHA384, SHA384_DIGEST_SIZE, HASH_ALG_SHA384 }, - { TPM_ALG_SHA512, SHA512_DIGEST_SIZE, HASH_ALG_SHA512 }, -}; - /** Return size of digest. @@ -40,15 +27,7 @@ GetHashSizeFromAlgo ( IN TPMI_ALG_HASH HashAlgo ) { - UINTN Index; - - for (Index = 0; Index < sizeof (mHashInfo)/sizeof (mHashInfo[0]); Index++) { - if (mHashInfo[Index].HashAlgo == HashAlgo) { - return mHashInfo[Index].HashSize; - } - } - - return 0; + return Tpm2GetHashSizeFromAlgo (HashAlgo); } /** @@ -64,15 +43,7 @@ GetHashMaskFromAlgo ( IN TPMI_ALG_HASH HashAlgo ) { - UINTN Index; - - for (Index = 0; Index < sizeof (mHashInfo)/sizeof (mHashInfo[0]); Index++) { - if (mHashInfo[Index].HashAlgo == HashAlgo) { - return mHashInfo[Index].HashMask; - } - } - - return 0; + return Tpm2GetHashMaskFromAlgo (HashAlgo); } /** @@ -90,54 +61,7 @@ CopyAuthSessionCommand ( OUT UINT8 *AuthSessionOut ) { - UINT8 *Buffer; - - Buffer = (UINT8 *)AuthSessionOut; - - // - // Add in Auth session - // - if (AuthSessionIn != NULL) { - // sessionHandle - WriteUnaligned32 ((UINT32 *)Buffer, SwapBytes32 (AuthSessionIn->sessionHandle)); - Buffer += sizeof (UINT32); - - // nonce - WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (AuthSessionIn->nonce.size)); - Buffer += sizeof (UINT16); - - CopyMem (Buffer, AuthSessionIn->nonce.buffer, AuthSessionIn->nonce.size); - Buffer += AuthSessionIn->nonce.size; - - // sessionAttributes - *(UINT8 *)Buffer = *(UINT8 *)&AuthSessionIn->sessionAttributes; - Buffer++; - - // hmac - WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (AuthSessionIn->hmac.size)); - Buffer += sizeof (UINT16); - - CopyMem (Buffer, AuthSessionIn->hmac.buffer, AuthSessionIn->hmac.size); - Buffer += AuthSessionIn->hmac.size; - } else { - // sessionHandle - WriteUnaligned32 ((UINT32 *)Buffer, SwapBytes32 (TPM_RS_PW)); - Buffer += sizeof (UINT32); - - // nonce = nullNonce - WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (0)); - Buffer += sizeof (UINT16); - - // sessionAttributes = 0 - *(UINT8 *)Buffer = 0x00; - Buffer++; - - // hmac = nullAuth - WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (0)); - Buffer += sizeof (UINT16); - } - - return (UINT32)((UINTN)Buffer - (UINTN)AuthSessionOut); + return Tpm2CopyAuthSessionCommand (AuthSessionIn, AuthSessionOut); } /** @@ -156,42 +80,7 @@ CopyAuthSessionResponse ( OUT TPMS_AUTH_RESPONSE *AuthSessionOut OPTIONAL ) { - UINT8 *Buffer; - TPMS_AUTH_RESPONSE LocalAuthSessionOut; - - if (AuthSessionOut == NULL) { - AuthSessionOut = &LocalAuthSessionOut; - } - - Buffer = (UINT8 *)AuthSessionIn; - - // nonce - AuthSessionOut->nonce.size = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer)); - Buffer += sizeof (UINT16); - if (AuthSessionOut->nonce.size > sizeof (TPMU_HA)) { - DEBUG ((DEBUG_ERROR, "CopyAuthSessionResponse - nonce.size error %x\n", AuthSessionOut->nonce.size)); - return 0; - } - - CopyMem (AuthSessionOut->nonce.buffer, Buffer, AuthSessionOut->nonce.size); - Buffer += AuthSessionOut->nonce.size; - - // sessionAttributes - *(UINT8 *) &AuthSessionOut->sessionAttributes = *(UINT8 *)Buffer; - Buffer++; - - // hmac - AuthSessionOut->hmac.size = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer)); - Buffer += sizeof (UINT16); - if (AuthSessionOut->hmac.size > sizeof (TPMU_HA)) { - DEBUG ((DEBUG_ERROR, "CopyAuthSessionResponse - hmac.size error %x\n", AuthSessionOut->hmac.size)); - return 0; - } - - CopyMem (AuthSessionOut->hmac.buffer, Buffer, AuthSessionOut->hmac.size); - Buffer += AuthSessionOut->hmac.size; - - return (UINT32)((UINTN)Buffer - (UINTN)AuthSessionIn); + return Tpm2CopyAuthSessionResponse (AuthSessionIn, AuthSessionOut); } /** @@ -210,40 +99,7 @@ IsHashAlgSupportedInHashAlgorithmMask ( IN UINT32 HashAlgorithmMask ) { - switch (HashAlg) { - case TPM_ALG_SHA1: - if ((HashAlgorithmMask & HASH_ALG_SHA1) != 0) { - return TRUE; - } - - break; - case TPM_ALG_SHA256: - if ((HashAlgorithmMask & HASH_ALG_SHA256) != 0) { - return TRUE; - } - - break; - case TPM_ALG_SHA384: - if ((HashAlgorithmMask & HASH_ALG_SHA384) != 0) { - return TRUE; - } - - break; - case TPM_ALG_SHA512: - if ((HashAlgorithmMask & HASH_ALG_SHA512) != 0) { - return TRUE; - } - - break; - case TPM_ALG_SM3_256: - if ((HashAlgorithmMask & HASH_ALG_SM3_256) != 0) { - return TRUE; - } - - break; - } - - return FALSE; + return Tpm2IsHashAlgSupportedInHashAlgorithmMask (HashAlg, HashAlgorithmMask); } /** @@ -263,31 +119,7 @@ CopyDigestListToBuffer ( IN UINT32 HashAlgorithmMask ) { - UINTN Index; - UINT16 DigestSize; - UINT32 DigestListCount; - UINT32 *DigestListCountPtr; - - DigestListCountPtr = (UINT32 *)Buffer; - DigestListCount = 0; - Buffer = (UINT8 *)Buffer + sizeof (DigestList->count); - for (Index = 0; Index < DigestList->count; Index++) { - if (!IsHashAlgSupportedInHashAlgorithmMask (DigestList->digests[Index].hashAlg, HashAlgorithmMask)) { - DEBUG ((DEBUG_ERROR, "WARNING: TPM2 Event log has HashAlg unsupported by PCR bank (0x%x)\n", DigestList->digests[Index].hashAlg)); - continue; - } - - CopyMem (Buffer, &DigestList->digests[Index].hashAlg, sizeof (DigestList->digests[Index].hashAlg)); - Buffer = (UINT8 *)Buffer + sizeof (DigestList->digests[Index].hashAlg); - DigestSize = GetHashSizeFromAlgo (DigestList->digests[Index].hashAlg); - CopyMem (Buffer, &DigestList->digests[Index].digest, DigestSize); - Buffer = (UINT8 *)Buffer + DigestSize; - DigestListCount++; - } - - WriteUnaligned32 (DigestListCountPtr, DigestListCount); - - return Buffer; + return Tpm2CopyDigestListToBuffer (Buffer, DigestList, HashAlgorithmMask); } /** @@ -310,45 +142,7 @@ CopyBufferToDigestList ( OUT TPML_DIGEST_VALUES *DigestList ) { - EFI_STATUS Status; - UINTN Index; - UINT16 DigestSize; - CONST UINT8 *BufferPtr; - - Status = EFI_INVALID_PARAMETER; - - if ((Buffer == NULL) || (DigestList == NULL) || (BufferSize > sizeof (TPML_DIGEST_VALUES))) { - return EFI_INVALID_PARAMETER; - } - - DigestList->count = SwapBytes32 (ReadUnaligned32 ((CONST UINT32 *)Buffer)); - if (DigestList->count > HASH_COUNT) { - return EFI_INVALID_PARAMETER; - } - - BufferPtr = (CONST UINT8 *)Buffer + sizeof (UINT32); - for (Index = 0; Index < DigestList->count; Index++) { - if (BufferPtr - (CONST UINT8 *)Buffer + sizeof (UINT16) > BufferSize) { - Status = EFI_BAD_BUFFER_SIZE; - break; - } else { - DigestList->digests[Index].hashAlg = SwapBytes16 (ReadUnaligned16 ((CONST UINT16 *)BufferPtr)); - } - - BufferPtr += sizeof (UINT16); - DigestSize = GetHashSizeFromAlgo (DigestList->digests[Index].hashAlg); - if (BufferPtr - (CONST UINT8 *)Buffer + (UINTN)DigestSize > BufferSize) { - Status = EFI_BAD_BUFFER_SIZE; - break; - } else { - CopyMem (&DigestList->digests[Index].digest, BufferPtr, DigestSize); - } - - BufferPtr += DigestSize; - Status = EFI_SUCCESS; - } - - return Status; + return Tpm2CopyBufferToDigestList (Buffer, BufferSize, DigestList); } /** @@ -364,17 +158,7 @@ GetDigestListSize ( IN TPML_DIGEST_VALUES *DigestList ) { - UINTN Index; - UINT16 DigestSize; - UINT32 TotalSize; - - TotalSize = sizeof (DigestList->count); - for (Index = 0; Index < DigestList->count; Index++) { - DigestSize = GetHashSizeFromAlgo (DigestList->digests[Index].hashAlg); - TotalSize += sizeof (DigestList->digests[Index].hashAlg) + DigestSize; - } - - return TotalSize; + return Tpm2GetDigestListSize (DigestList); } /** @@ -390,17 +174,7 @@ GetDigestListSizeFromHashAlgorithmMask ( IN UINT32 HashAlgorithmMask ) { - UINTN Index; - UINT32 TotalSize; - - TotalSize = sizeof (UINT32); - for (Index = 0; Index < ARRAY_SIZE (mHashInfo); Index++) { - if ((mHashInfo[Index].HashMask & HashAlgorithmMask) != 0) { - TotalSize += sizeof (TPMI_ALG_HASH) + mHashInfo[Index].HashSize; - } - } - - return TotalSize; + return Tpm2GetDigestListSizeFromHashAlgorithmMask (HashAlgorithmMask); } /** @@ -421,20 +195,5 @@ GetDigestFromDigestList ( OUT VOID *Digest ) { - UINTN Index; - UINT16 DigestSize; - - DigestSize = GetHashSizeFromAlgo (HashAlg); - for (Index = 0; Index < DigestList->count; Index++) { - if (DigestList->digests[Index].hashAlg == HashAlg) { - CopyMem ( - Digest, - &DigestList->digests[Index].digest, - DigestSize - ); - return EFI_SUCCESS; - } - } - - return EFI_NOT_FOUND; + return Tpm2GetDigestFromDigestList (HashAlg, DigestList, Digest); }