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 AC071740034 for ; Tue, 28 Nov 2023 19:53:06 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=bcKx1uTRY06R+le0gxXrzo1CI5scOv6RduO7+/8vQXA=; 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=1701201185; v=1; b=i8YURSK0T6UUw2Uo32FQhiVfKsqt+8zdUUXUUAapUDn6LiQXIZJ+20LJb2zE1pPTWpMFAxHM +j7jcEw7PXBkX80ESbn6e8AQUZ+q8YnCsZ73naP5QFL6QL2VX6kgsNd4fCkmJbqOzCcz/nSEVwd P07lL/Zgp9bNgtSrmrFCC9uY= X-Received: by 127.0.0.2 with SMTP id nxpFYY7687511xRsBI3V4xPe; Tue, 28 Nov 2023 11:53:05 -0800 X-Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by mx.groups.io with SMTP id smtpd.web10.2228.1701201184634026098 for ; Tue, 28 Nov 2023 11:53:04 -0800 X-Received: from localhost.localdomain (unknown [47.201.241.198]) by linux.microsoft.com (Postfix) with ESMTPSA id C4FA620B74C0; Tue, 28 Nov 2023 11:53:03 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com C4FA620B74C0 From: "Michael Kubacki" To: devel@edk2.groups.io Cc: Jiewen Yao , Rahul Kumar Subject: [edk2-devel] [PATCH v2 1/1] SecurityPkg/Tpm2CommandLib: Add new digest list copy and size functions Date: Tue, 28 Nov 2023 14:52:45 -0500 Message-ID: <20231128195245.1515-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: DuxOl2kc8vTdXchi7pX6zdgjx7686176AA= 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=i8YURSK0; 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 --- Notes: v2 change: =20 - 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; } =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) !=3D 0) { + 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 (#111826): https://edk2.groups.io/g/devel/message/111826 Mute This Topic: https://groups.io/mt/102858120/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-