From 617e061830f6218ef985b599b981bf0ee53ad7da Mon Sep 17 00:00:00 2001 From: Michael Kubacki Date: Tue, 28 Nov 2023 11:31:24 -0500 Subject: [PATCH] SecurityPkg/Tpm2CommandLib: Add new digest list copy and size functions REF:https://bugzilla.tianocore.org/show_bug.cgi?id=3267 Adds two new helper functions. Currently, a function exists in Tpm2CommandLib to copy a digest list to a buffer. A function to perform the inverse operation - copying from a buffer to a digest list is added. A function is also added to compute the total digest size for a given hash algorithm mask. Cc: Jiewen Yao Cc: Rahul Kumar Signed-off-by: Michael Kubacki --- SecurityPkg/Include/Library/Tpm2CommandLib.h | 34 ++++++++ SecurityPkg/Library/Tpm2CommandLib/Tpm2Help.c | 87 +++++++++++++++++++ 2 files changed, 121 insertions(+) diff --git a/SecurityPkg/Include/Library/Tpm2CommandLib.h b/SecurityPkg/Include/Library/Tpm2CommandLib.h index 70eec84c90..7bda027068 100644 --- a/SecurityPkg/Include/Library/Tpm2CommandLib.h +++ b/SecurityPkg/Include/Library/Tpm2CommandLib.h @@ -1103,6 +1103,27 @@ CopyDigestListToBuffer ( IN UINT32 HashAlgorithmMask ); +/** + Copy a buffer into a TPML_DIGEST_VALUES structure. + + @param[in] Buffer Buffer to hold TPML_DIGEST_VALUES compact binary. + @param[in] BufferSize Size of Buffer. + @param[out] DigestList TPML_DIGEST_VALUES. + + @retval EFI_SUCCESS Buffer was succesfully copied to DigestList. + @retval EFI_BAD_BUFFER_SIZE A bad buffer size passed to the function. + @retval EFI_INVALID_PARAMETER An invalid parameter passed to the function: NULL pointer or + BufferSize bigger than TPML_DIGEST_VALUES. + +**/ +EFI_STATUS +EFIAPI +CopyBufferToDigestList ( + IN CONST VOID *Buffer, + IN UINTN BufferSize, + OUT TPML_DIGEST_VALUES *DigestList + ); + /** Get TPML_DIGEST_VALUES data size. @@ -1116,6 +1137,19 @@ GetDigestListSize ( IN TPML_DIGEST_VALUES *DigestList ); +/** + Get the total digest size from a hash algorithm mask. + + @param[in] HashAlgorithmMask. + + @return Digest size in bytes. +**/ +UINT32 +EFIAPI +GetDigestListSizeFromHashAlgorithmMask ( + IN UINT32 HashAlgorithmMask + ); + /** This function get digest from digest list. diff --git a/SecurityPkg/Library/Tpm2CommandLib/Tpm2Help.c b/SecurityPkg/Library/Tpm2CommandLib/Tpm2Help.c index e7f30b673f..ac802733d3 100644 --- a/SecurityPkg/Library/Tpm2CommandLib/Tpm2Help.c +++ b/SecurityPkg/Library/Tpm2CommandLib/Tpm2Help.c @@ -290,6 +290,67 @@ CopyDigestListToBuffer ( return Buffer; } +/** + Copy a buffer into a TPML_DIGEST_VALUES structure. + + @param[in] Buffer Buffer to hold TPML_DIGEST_VALUES compact binary. + @param[in] BufferSize Size of Buffer. + @param[out] DigestList TPML_DIGEST_VALUES. + + @retval EFI_SUCCESS Buffer was succesfully copied to DigestList. + @retval EFI_BAD_BUFFER_SIZE A bad buffer size passed to the function. + @retval EFI_INVALID_PARAMETER An invalid parameter passed to the function: NULL pointer or + BufferSize bigger than TPML_DIGEST_VALUES. +**/ +EFI_STATUS +EFIAPI +CopyBufferToDigestList ( + IN CONST VOID *Buffer, + IN UINTN BufferSize, + 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; +} + /** Get TPML_DIGEST_VALUES data size. @@ -316,6 +377,32 @@ GetDigestListSize ( return TotalSize; } +/** + Get the total digest size from a hash algorithm mask. + + @param[in] HashAlgorithmMask. + + @return Digest size in bytes. +**/ +UINT32 +EFIAPI +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; +} + /** This function get digest from digest list.