From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by spool.mail.gandi.net (Postfix) with ESMTPS id 1DF26AC16A2 for ; Tue, 28 Nov 2023 17:18:17 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=6T2DyYzZKtu6bA7AKbaCSWFB/TtMfa5Gzwaj2zictac=; c=relaxed/simple; d=groups.io; h=DKIM-Filter:From:To:Cc:Subject:Date:Message-ID:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding; s=20140610; t=1701191896; v=1; b=MKf3nbNyayrtCPDLOqulU4GYXuhp//m9R2ktLiqyhKDDMxPPW0uy1BBpQpm77cY7QpCt6LBC hy8RJO807Dei2tRnvbYvNw50HkTNxhL5oZIPRFUgp/KMMN9s0nZjkXG9eS5TVotojcN0I9Nw5jw mRwaz/JxOoZv973YSByqWVsY= X-Received: by 127.0.0.2 with SMTP id 7wfqYY7687511xsiycg0olnM; Tue, 28 Nov 2023 09:18:16 -0800 X-Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by mx.groups.io with SMTP id smtpd.web10.38963.1701191895800730458 for ; Tue, 28 Nov 2023 09:18:15 -0800 X-Received: from localhost.localdomain (unknown [47.201.241.198]) by linux.microsoft.com (Postfix) with ESMTPSA id CC33F20B74C0; Tue, 28 Nov 2023 09:18:14 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com CC33F20B74C0 From: "Michael Kubacki" To: devel@edk2.groups.io Cc: Jiewen Yao , Rahul Kumar Subject: [edk2-devel] [PATCH v1 1/1] SecurityPkg/Tpm2CommandLib: Add new digest list copy and size functions Date: Tue, 28 Nov 2023 12:17:44 -0500 Message-ID: <20231128171744.1384-1-mikuback@linux.microsoft.com> MIME-Version: 1.0 Precedence: Bulk List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,mikuback@linux.microsoft.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: BsJZyaq5YLoBd7d5pTT2TSfCx7686176AA= Content-Transfer-Encoding: quoted-printable X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20140610 header.b=MKf3nbNy; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=linux.microsoft.com (policy=none); spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io From: Michael Kubacki REF:https://bugzilla.tianocore.org/show_bug.cgi?id=3D3267 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/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..b450cae0defd 100644 --- a/SecurityPkg/Library/Tpm2CommandLib/Tpm2Help.c +++ b/SecurityPkg/Library/Tpm2CommandLib/Tpm2Help.c @@ -290,6 +290,67 @@ CopyDigestListToBuffer ( return Buffer; } =20 +/** + Copy a buffer into a TPML_DIGEST_VALUES structure. + + @param[in] Buffer Buffer to hold TPML_DIGEST_VALUES co= mpact binary. + @param[in] BufferSize Size of Buffer. + @param[out] DigestList TPML_DIGEST_VALUES. + + @retval EFI_SUCCESS Buffer was succesfully copied to Dig= est 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_V= ALUES +**/ +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 =3D EFI_INVALID_PARAMETER; + + if ((Buffer =3D=3D NULL) || (DigestList =3D=3D NULL) || (BufferSize > = sizeof (TPML_DIGEST_VALUES))) { + return EFI_INVALID_PARAMETER; + } + + DigestList->count =3D SwapBytes32 (ReadUnaligned32 ((CONST UINT32 *)Bu= ffer)); + if (DigestList->count > HASH_COUNT) { + return EFI_INVALID_PARAMETER; + } + + BufferPtr =3D (CONST UINT8 *)Buffer + sizeof (UINT32); + for (Index =3D 0; Index < DigestList->count; Index++) { + if (BufferPtr - (CONST UINT8 *)Buffer + sizeof (UINT16) > BufferSize= ) { + Status =3D EFI_BAD_BUFFER_SIZE; + break; + } else { + DigestList->digests[Index].hashAlg =3D SwapBytes16 (ReadUnaligned1= 6 ((CONST UINT16 *)BufferPtr)); + } + + BufferPtr +=3D sizeof (UINT16); + DigestSize =3D GetHashSizeFromAlgo (DigestList->digests[Index].hashA= lg); + if (BufferPtr - (CONST UINT8 *)Buffer + (UINTN)DigestSize > BufferSi= ze) { + Status =3D EFI_BAD_BUFFER_SIZE; + break; + } else { + CopyMem (&DigestList->digests[Index].digest, BufferPtr, DigestSize= ); + } + + BufferPtr +=3D DigestSize; + Status =3D EFI_SUCCESS; + } + + return Status; +} + /** Get TPML_DIGEST_VALUES data size. =20 @@ -316,6 +377,32 @@ GetDigestListSize ( return TotalSize; } =20 +/** + 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 =3D sizeof (UINT32); + for (Index =3D 0; Index < ARRAY_SIZE (mHashInfo); Index++) { + if (mHashInfo[Index].HashMask & HashAlgorithmMask) { + TotalSize +=3D sizeof (TPMI_ALG_HASH) + mHashInfo[Index].HashSize; + } + } + + return TotalSize; +} + /** This function get digest from digest list. =20 diff --git a/SecurityPkg/Include/Library/Tpm2CommandLib.h b/SecurityPkg/I= nclude/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 ); =20 +/** + Copy a buffer into A TPML_DIGEST_VALUES structure. + + @param[in] Buffer Buffer to hold TPML_DIGEST_VALUES co= mpact binary. + @param[in] BufferSize Size of Buffer. + @param[out] DigestList TPML_DIGEST_VALUES. + + @retval EFI_SUCCESS Buffer was successfully copied to Di= gest 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_V= ALUES +**/ +EFI_STATUS +EFIAPI +CopyBufferToDigestList ( + IN CONST VOID *Buffer, + IN UINTN BufferSize, + OUT TPML_DIGEST_VALUES *DigestList + ); + /** Get TPML_DIGEST_VALUES data size. =20 @@ -1095,6 +1115,19 @@ GetDigestListSize ( IN TPML_DIGEST_VALUES *DigestList ); =20 +/** + 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. =20 --=20 2.42.0.windows.2 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#111817): https://edk2.groups.io/g/devel/message/111817 Mute This Topic: https://groups.io/mt/102854693/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-