From: Michael Kubacki <michael.kuba...@microsoft.com> 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 <jiewen....@intel.com> Cc: Rahul Kumar <rahul1.ku...@intel.com> Signed-off-by: Michael Kubacki <michael.kuba...@microsoft.com> --- Notes: v2 change: - ECC non-functional fix in GetDigestListSizeFromHashAlgorithmMask(). SecurityPkg/Library/Tpm2CommandLib/Tpm2Help.c | 87 ++++++++++++++++++++ SecurityPkg/Include/Library/Tpm2CommandLib.h | 33 ++++++++ 2 files changed, 120 insertions(+) diff --git a/SecurityPkg/Library/Tpm2CommandLib/Tpm2Help.c b/SecurityPkg/Library/Tpm2CommandLib/Tpm2Help.c index e7f30b673f0e..796971bb1599 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 Digest List. + @retval EFI_BAD_BUFFER_SIZE Bad buffer size passed to function. + @retval EFI_INVALID_PARAMETER Invalid parameter passed to 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. diff --git a/SecurityPkg/Include/Library/Tpm2CommandLib.h b/SecurityPkg/Include/Library/Tpm2CommandLib.h index a2fb97f18dfe..8b0fe8328516 100644 --- a/SecurityPkg/Include/Library/Tpm2CommandLib.h +++ b/SecurityPkg/Include/Library/Tpm2CommandLib.h @@ -1082,6 +1082,26 @@ 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 successfully copied to Digest List. + @retval EFI_BAD_BUFFER_SIZE Bad buffer size passed to function. + @retval EFI_INVALID_PARAMETER Invalid parameter passed to 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. @@ -1095,6 +1115,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. -- 2.42.0.windows.2 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#111826): https://edk2.groups.io/g/devel/message/111826 Mute This Topic: https://groups.io/mt/102858120/21656 Group Owner: devel+ow...@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com] -=-=-=-=-=-=-=-=-=-=-=-