From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.groups.io with SMTP id smtpd.web09.22357.1628776125864366475 for ; Thu, 12 Aug 2021 06:48:46 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=DQBgJZMO; spf=pass (domain: redhat.com, ip: 216.205.24.124, mailfrom: mlureau@redhat.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1628776124; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=+GmDQJFFUi8Afi9RNwozM17itTQyQPo9m64qqnVOsqA=; b=DQBgJZMOKsqgOgLD+vTR9nuu6OWk2KDo4YFoHtFc2zxtt2Uj28NuB/ehswCZStTn3xoC/M 5MDoZXtSb6e1WgDaXmvMgRPDrq1F+bSXtB4+HoaRQb/egZ9KukG/kkqHoBSXyqFTo+G7Bp bf2BiZQNhmnNN6l3GEgn39MJ59+lLvc= Received: from mail-pl1-f197.google.com (mail-pl1-f197.google.com [209.85.214.197]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-127-qZvl8n4POeSj5wAH2PhNWw-1; Thu, 12 Aug 2021 09:48:42 -0400 X-MC-Unique: qZvl8n4POeSj5wAH2PhNWw-1 Received: by mail-pl1-f197.google.com with SMTP id w5-20020a170902e885b029012cf3ddd763so3777798plg.17 for ; Thu, 12 Aug 2021 06:48:42 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=+GmDQJFFUi8Afi9RNwozM17itTQyQPo9m64qqnVOsqA=; b=PFF23oDp5uqMBRLPJ1thrS8FCUMa/Q23EsiYm6ydFagO0RFtjiQbAmJbtsuKFKn35B OTV5awxURhJGrQxQnUmdZ4BjD7Zg4G262nydHXFr95UIvQZRAIw/FZru/oj38QMh4pAy EOtu3gJChekIPNtqIq+lDlyvPyfxuH4MCzF3oIehCwGlBFi7m09T0npuuJ9g1acWa+KW ZcNZxdEmPZyc/Rou3s8gl8po2VODbAvZynHNo5VPOvZ4Zh/Wm/q6cOTi20zzbrarU+bo ixcdAitPezyumEkj/JbqUHGWDKXfzQBP1PF38vowED79KaX4xPQdRalKPQ/n7txl1Pz4 Dozg== X-Gm-Message-State: AOAM531l3UqMG/luQp8E3yiQ+N+PrUKyzK6G7NOG3iBnthpaCaWNaIpy QWbK+A0uRPZkeAUXSjdpC9ovE1gzQSVarLIY0m1iXY/oDxdRh6dRFhs405kUfc8hEqrUbj7zeHj ARo3hf4Q/1AibDJV3CRa4l7N3noLH3Q== X-Received: by 2002:a17:90a:116:: with SMTP id b22mr4315707pjb.97.1628776121317; Thu, 12 Aug 2021 06:48:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyL0XvYNwrTD7n6skrMDazVa3np6+L92PWGYS08tkcCgMDX8Wt98priveHvDd6tgALYjiTa9CMB2iTnjLWnmtc= X-Received: by 2002:a17:90a:116:: with SMTP id b22mr4315683pjb.97.1628776121061; Thu, 12 Aug 2021 06:48:41 -0700 (PDT) MIME-Version: 1.0 References: <20210810172029.4166819-1-stefanb@linux.vnet.ibm.com> <20210810172029.4166819-2-stefanb@linux.vnet.ibm.com> In-Reply-To: <20210810172029.4166819-2-stefanb@linux.vnet.ibm.com> From: =?UTF-8?B?TWFyYy1BbmRyw6kgTHVyZWF1?= Date: Thu, 12 Aug 2021 17:48:29 +0400 Message-ID: Subject: Re: [PATCH v3 1/6] OvmfPkg/TPM: Import PeiDxeTpmPlatformHierarchyLib.c from edk2-platforms To: Stefan Berger Cc: edk2-devel-groups-io , jiewen.yao@intel.com, Laszlo Ersek , dick_wilkins@phoenix.com, James.Bottomley@hansenpartnership.com, Stefan Berger Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=mlureau@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: multipart/alternative; boundary="0000000000001d6bce05c95cfe5e" --0000000000001d6bce05c95cfe5e Content-Type: text/plain; charset="UTF-8" Hi On Tue, Aug 10, 2021 at 9:22 PM Stefan Berger wrote: > Import PeiDxeTpmPlatformHierarchyLib.c from edk2-platforms. Modify it so > that ConfigureTpmPlatformHierarchy() is the only public function provided > by this file. > > Signed-off-by: Stefan Berger > --- > .../Include/Library/TpmPlatformHierarchyLib.h | 27 +++ > .../PeiDxeTpmPlatformHierarchyLib.c | 210 ++++++++++++++++++ > .../PeiDxeTpmPlatformHierarchyLib.inf | 40 ++++ > 3 files changed, 277 insertions(+) > create mode 100644 OvmfPkg/Include/Library/TpmPlatformHierarchyLib.h > create mode 100644 > OvmfPkg/Library/PeiDxeTpmPlatformHierarchyLib/PeiDxeTpmPlatformHierarchyLib.c > create mode 100644 > OvmfPkg/Library/PeiDxeTpmPlatformHierarchyLib/PeiDxeTpmPlatformHierarchyLib.inf > > diff --git a/OvmfPkg/Include/Library/TpmPlatformHierarchyLib.h > b/OvmfPkg/Include/Library/TpmPlatformHierarchyLib.h > new file mode 100644 > index 0000000000..a872fa09dc > --- /dev/null > +++ b/OvmfPkg/Include/Library/TpmPlatformHierarchyLib.h > @@ -0,0 +1,27 @@ > +/** @file > + TPM Platform Hierarchy configuration library. > + > + This library provides functions for customizing the TPM's Platform > Hierarchy > + Authorization Value (platformAuth) and Platform Hierarchy > Authorization > + Policy (platformPolicy) can be defined through this function. > + > +Copyright (c) 2019, Intel Corporation. All rights reserved.
> +Copyright (c) Microsoft Corporation.
> +SPDX-License-Identifier: BSD-2-Clause-Patent > + > +**/ > + > +#ifndef _TPM_PLATFORM_HIERARCHY_LIB_H_ > +#define _TPM_PLATFORM_HIERARCHY_LIB_H_ > + > +/** > + This service will perform the TPM Platform Hierarchy configuration at > the SmmReadyToLock event. > + > +**/ > +VOID > +EFIAPI > +ConfigureTpmPlatformHierarchy ( > + VOID > + ); > + > +#endif > diff --git > a/OvmfPkg/Library/PeiDxeTpmPlatformHierarchyLib/PeiDxeTpmPlatformHierarchyLib.c > b/OvmfPkg/Library/PeiDxeTpmPlatformHierarchyLib/PeiDxeTpmPlatformHierarchyLib.c > new file mode 100644 > index 0000000000..ba2d99bb53 > --- /dev/null > +++ > b/OvmfPkg/Library/PeiDxeTpmPlatformHierarchyLib/PeiDxeTpmPlatformHierarchyLib.c > @@ -0,0 +1,210 @@ > +/** @file > + TPM Platform Hierarchy configuration library. > + > + This library provides functions for customizing the TPM's Platform > Hierarchy > + Authorization Value (platformAuth) and Platform Hierarchy > Authorization > + Policy (platformPolicy) can be defined through this function. > + > + Copyright (c) 2019, Intel Corporation. All rights reserved.
> + Copyright (c) Microsoft Corporation.
> + SPDX-License-Identifier: BSD-2-Clause-Patent > + > + @par Specification Reference: > + > https://trustedcomputinggroup.org/resource/tcg-tpm-v2-0-provisioning-guidance/ > +**/ > + > +#include > + > +#include > +#include > +#include > +#include > +#include > +#include > + > +// > +// The authorization value may be no larger than the digest produced by > the hash > +// algorithm used for context integrity. > +// > +#define MAX_NEW_AUTHORIZATION_SIZE SHA512_DIGEST_SIZE > + > +UINT16 mAuthSize; > + > +/** > + Generate high-quality entropy source through RDRAND. > + > + @param[in] Length Size of the buffer, in bytes, to fill with. > + @param[out] Entropy Pointer to the buffer to store the entropy > data. > + > + @retval EFI_SUCCESS Entropy generation succeeded. > + @retval EFI_NOT_READY Failed to request random data. > + > +**/ > +EFI_STATUS > +EFIAPI > +RdRandGenerateEntropy ( > + IN UINTN Length, > + OUT UINT8 *Entropy > + ) > +{ > + EFI_STATUS Status; > + UINTN BlockCount; > + UINT64 Seed[2]; > + UINT8 *Ptr; > + > + Status = EFI_NOT_READY; > + BlockCount = Length / 64; > + Ptr = (UINT8 *)Entropy; > + > + // > + // Generate high-quality seed for DRBG Entropy > + // > + while (BlockCount > 0) { > + Status = GetRandomNumber128 (Seed); > + if (EFI_ERROR (Status)) { > + return Status; > + } > + CopyMem (Ptr, Seed, 64); > This looks like it's copying past the Seed buffer, which is 2 * sizeof(u64) = 16. + > + BlockCount--; > + Ptr = Ptr + 64; > + } > + > + // > + // Populate the remained data as request. > + // > + Status = GetRandomNumber128 (Seed); > + if (EFI_ERROR (Status)) { > + return Status; > + } > + CopyMem (Ptr, Seed, (Length % 64)); > And then again. Isn't there a better way to fill a buffer with random data in edk2? + > + return Status; > +} > + > +/** > + This function returns the maximum size of TPM2B_AUTH; this structure is > used for an authorization value > + and limits an authValue to being no larger than the largest digest > produced by a TPM. > + > + @param[out] AuthSize Tpm2 Auth size > + > + @retval EFI_SUCCESS Auth size returned. > + @retval EFI_DEVICE_ERROR Can not return platform auth due > to device error. > + > +**/ > +EFI_STATUS > +EFIAPI > +GetAuthSize ( > + OUT UINT16 *AuthSize > + ) > +{ > + EFI_STATUS Status; > + TPML_PCR_SELECTION Pcrs; > + UINTN Index; > + UINT16 DigestSize; > + > + Status = EFI_SUCCESS; > + > + while (mAuthSize == 0) { > This is a bit odd, but ok. + > + mAuthSize = SHA1_DIGEST_SIZE; > + ZeroMem (&Pcrs, sizeof (TPML_PCR_SELECTION)); > + Status = Tpm2GetCapabilityPcrs (&Pcrs); > + > + if (EFI_ERROR (Status)) { > + DEBUG ((DEBUG_ERROR, "Tpm2GetCapabilityPcrs fail!\n")); > + break; > + } > + > + DEBUG ((DEBUG_ERROR, "Tpm2GetCapabilityPcrs - %08x\n", Pcrs.count)); > + > + for (Index = 0; Index < Pcrs.count; Index++) { > + DEBUG ((DEBUG_ERROR, "alg - %x\n", Pcrs.pcrSelections[Index].hash)); > + > + switch (Pcrs.pcrSelections[Index].hash) { > + case TPM_ALG_SHA1: > + DigestSize = SHA1_DIGEST_SIZE; > + break; > + case TPM_ALG_SHA256: > + DigestSize = SHA256_DIGEST_SIZE; > + break; > + case TPM_ALG_SHA384: > + DigestSize = SHA384_DIGEST_SIZE; > + break; > + case TPM_ALG_SHA512: > + DigestSize = SHA512_DIGEST_SIZE; > + break; > + case TPM_ALG_SM3_256: > + DigestSize = SM3_256_DIGEST_SIZE; > + break; > + default: > + DigestSize = SHA1_DIGEST_SIZE; > + break; > + } > + > + if (DigestSize > mAuthSize) { > + mAuthSize = DigestSize; > + } > + } > + break; > + } > + > + *AuthSize = mAuthSize; > + return Status; > +} > + > +/** > + Set PlatformAuth to random value. > +**/ > +VOID > +RandomizePlatformAuth ( > + VOID > + ) > +{ > + EFI_STATUS Status; > + UINT16 AuthSize; > + UINT8 *Rand; > + UINTN RandSize; > + TPM2B_AUTH NewPlatformAuth; > + > + // > + // Send Tpm2HierarchyChange Auth with random value to avoid > PlatformAuth being null > + // > + > + GetAuthSize (&AuthSize); > + > + ZeroMem (NewPlatformAuth.buffer, AuthSize); > + NewPlatformAuth.size = AuthSize; > + > + // > + // Allocate one buffer to store random data. > + // > + RandSize = MAX_NEW_AUTHORIZATION_SIZE; > + Rand = AllocatePool (RandSize); > + > + RdRandGenerateEntropy (RandSize, Rand); > + CopyMem (NewPlatformAuth.buffer, Rand, AuthSize); > Why generate random data for MAX_NEW_AUTHORIZATION to only copy a subset after? + > + FreePool (Rand); > + > + // > + // Send Tpm2HierarchyChangeAuth command with the new Auth value > + // > + Status = Tpm2HierarchyChangeAuth (TPM_RH_PLATFORM, NULL, > &NewPlatformAuth); > + DEBUG ((DEBUG_INFO, "Tpm2HierarchyChangeAuth Result: - %r\n", Status)); > + ZeroMem (NewPlatformAuth.buffer, AuthSize); > + ZeroMem (Rand, RandSize); > Isn't Rand free at this point? +} > + > +/** > + This service defines the configuration of the Platform Hierarchy > Authorization Value (platformAuth) > + and Platform Hierarchy Authorization Policy (platformPolicy) > + > +**/ > +VOID > +EFIAPI > +ConfigureTpmPlatformHierarchy ( > + ) > +{ > + RandomizePlatformAuth (); > +} > diff --git > a/OvmfPkg/Library/PeiDxeTpmPlatformHierarchyLib/PeiDxeTpmPlatformHierarchyLib.inf > b/OvmfPkg/Library/PeiDxeTpmPlatformHierarchyLib/PeiDxeTpmPlatformHierarchyLib.inf > new file mode 100644 > index 0000000000..a413e02302 > --- /dev/null > +++ > b/OvmfPkg/Library/PeiDxeTpmPlatformHierarchyLib/PeiDxeTpmPlatformHierarchyLib.inf > @@ -0,0 +1,40 @@ > +### @file > +# > +# TPM Platform Hierarchy configuration library. > +# > +# This library provides functions for customizing the TPM's Platform > Hierarchy > +# Authorization Value (platformAuth) and Platform Hierarchy > Authorization > +# Policy (platformPolicy) can be defined through this function. > +# > +# Copyright (c) 2019, Intel Corporation. All rights reserved.
> +# Copyright (c) Microsoft Corporation.
> +# > +# SPDX-License-Identifier: BSD-2-Clause-Patent > +# > +### > + > +[Defines] > + INF_VERSION = 0x00010005 > + BASE_NAME = PeiDxeTpmPlatformHierarchyLib > + FILE_GUID = 7794F92C-4E8E-4E57-9E4A-49A0764C7D73 > + MODULE_TYPE = PEIM > + VERSION_STRING = 1.0 > + LIBRARY_CLASS = TpmPlatformHierarchyLib|PEIM DXE_DRIVER > + > +[LibraryClasses] > + BaseLib > + BaseMemoryLib > + DebugLib > + MemoryAllocationLib > + RngLib > + Tpm2CommandLib > + Tpm2DeviceLib > + > +[Packages] > + MdePkg/MdePkg.dec > + MdeModulePkg/MdeModulePkg.dec > + SecurityPkg/SecurityPkg.dec > + CryptoPkg/CryptoPkg.dec > + > +[Sources] > + PeiDxeTpmPlatformHierarchyLib.c > -- > 2.31.1 > > --0000000000001d6bce05c95cfe5e Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi

On Tue, Aug 10, 2021 at 9:22 PM Ste= fan Berger <stefanb@linux.= vnet.ibm.com> wrote:
Import PeiDxeTpmPlatformHierarchyLib.c from edk2-platforms. Mod= ify it so
that ConfigureTpmPlatformHierarchy() is the only public function provided by this file.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
---
=C2=A0.../Include/Library/TpmPlatformHierarchyLib.h |=C2=A0 27 +++
=C2=A0.../PeiDxeTpmPlatformHierarchyLib.c=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0| 210 ++++++++++++++++++
=C2=A0.../PeiDxeTpmPlatformHierarchyLib.inf=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0|=C2=A0 40 ++++
=C2=A03 files changed, 277 insertions(+)
=C2=A0create mode 100644 OvmfPkg/Include/Library/TpmPlatformHierarchyLib.h<= br> =C2=A0create mode 100644 OvmfPkg/Library/PeiDxeTpmPlatformHierarchyLib/PeiD= xeTpmPlatformHierarchyLib.c
=C2=A0create mode 100644 OvmfPkg/Library/PeiDxeTpmPlatformHierarchyLib/PeiD= xeTpmPlatformHierarchyLib.inf

diff --git a/OvmfPkg/Include/Library/TpmPlatformHierarchyLib.h b/OvmfPkg/In= clude/Library/TpmPlatformHierarchyLib.h
new file mode 100644
index 0000000000..a872fa09dc
--- /dev/null
+++ b/OvmfPkg/Include/Library/TpmPlatformHierarchyLib.h
@@ -0,0 +1,27 @@
+/** @file
+=C2=A0 =C2=A0 TPM Platform Hierarchy configuration library.
+
+=C2=A0 =C2=A0 This library provides functions for customizing the TPM'= s Platform Hierarchy
+=C2=A0 =C2=A0 Authorization Value (platformAuth) and Platform Hierarchy Au= thorization
+=C2=A0 =C2=A0 Policy (platformPolicy) can be defined through this function= .
+
+Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
+Copyright (c) Microsoft Corporation.<BR>
+SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#ifndef _TPM_PLATFORM_HIERARCHY_LIB_H_
+#define _TPM_PLATFORM_HIERARCHY_LIB_H_
+
+/**
+=C2=A0 =C2=A0This service will perform the TPM Platform Hierarchy configur= ation at the SmmReadyToLock event.
+
+**/
+VOID
+EFIAPI
+ConfigureTpmPlatformHierarchy (
+=C2=A0 VOID
+=C2=A0 );
+
+#endif
diff --git a/OvmfPkg/Library/PeiDxeTpmPlatformHierarchyLib/PeiDxeTpmPlatfor= mHierarchyLib.c b/OvmfPkg/Library/PeiDxeTpmPlatformHierarchyLib/PeiDxeTpmPl= atformHierarchyLib.c
new file mode 100644
index 0000000000..ba2d99bb53
--- /dev/null
+++ b/OvmfPkg/Library/PeiDxeTpmPlatformHierarchyLib/PeiDxeTpmPlatformHierar= chyLib.c
@@ -0,0 +1,210 @@
+/** @file
+=C2=A0 =C2=A0 TPM Platform Hierarchy configuration library.
+
+=C2=A0 =C2=A0 This library provides functions for customizing the TPM'= s Platform Hierarchy
+=C2=A0 =C2=A0 Authorization Value (platformAuth) and Platform Hierarchy Au= thorization
+=C2=A0 =C2=A0 Policy (platformPolicy) can be defined through this function= .
+
+=C2=A0 =C2=A0 Copyright (c) 2019, Intel Corporation. All rights reserved.&= lt;BR>
+=C2=A0 =C2=A0 Copyright (c) Microsoft Corporation.<BR>
+=C2=A0 =C2=A0 SPDX-License-Identifier: BSD-2-Clause-Patent
+
+=C2=A0 =C2=A0 @par Specification Reference:
+=C2=A0 =C2=A0 https:/= /trustedcomputinggroup.org/resource/tcg-tpm-v2-0-provisioning-guidance/=
+**/
+
+#include <Uefi.h>
+
+#include <Library/BaseMemoryLib.h>
+#include <Library/DebugLib.h>
+#include <Library/MemoryAllocationLib.h>
+#include <Library/RngLib.h>
+#include <Library/Tpm2CommandLib.h>
+#include <Library/Tpm2DeviceLib.h>
+
+//
+// The authorization value may be no larger than the digest produced by th= e hash
+//=C2=A0 =C2=A0algorithm used for context integrity.
+//
+#define=C2=A0 =C2=A0 =C2=A0 MAX_NEW_AUTHORIZATION_SIZE SHA512_DIGEST_SIZE<= br> +
+UINT16=C2=A0 =C2=A0 =C2=A0 =C2=A0mAuthSize;
+
+/**
+=C2=A0 Generate high-quality entropy source through RDRAND.
+
+=C2=A0 @param[in]=C2=A0 =C2=A0Length=C2=A0 =C2=A0 =C2=A0 =C2=A0 Size of th= e buffer, in bytes, to fill with.
+=C2=A0 @param[out]=C2=A0 Entropy=C2=A0 =C2=A0 =C2=A0 =C2=A0Pointer to the = buffer to store the entropy data.
+
+=C2=A0 @retval EFI_SUCCESS=C2=A0 =C2=A0 =C2=A0 =C2=A0 Entropy generation s= ucceeded.
+=C2=A0 @retval EFI_NOT_READY=C2=A0 =C2=A0 =C2=A0 Failed to request random = data.
+
+**/
+EFI_STATUS
+EFIAPI
+RdRandGenerateEntropy (
+=C2=A0 IN UINTN=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Length,
+=C2=A0 OUT UINT8=C2=A0 =C2=A0 =C2=A0 =C2=A0 *Entropy
+=C2=A0 )
+{
+=C2=A0 EFI_STATUS=C2=A0 Status;
+=C2=A0 UINTN=C2=A0 =C2=A0 =C2=A0 =C2=A0BlockCount;
+=C2=A0 UINT64=C2=A0 =C2=A0 =C2=A0 Seed[2];
+=C2=A0 UINT8=C2=A0 =C2=A0 =C2=A0 =C2=A0*Ptr;
+
+=C2=A0 Status =3D EFI_NOT_READY;
+=C2=A0 BlockCount =3D Length / 64;
+=C2=A0 Ptr =3D (UINT8 *)Entropy;
+
+=C2=A0 //
+=C2=A0 // Generate high-quality seed for DRBG Entropy
+=C2=A0 //
+=C2=A0 while (BlockCount > 0) {
+=C2=A0 =C2=A0 Status =3D GetRandomNumber128 (Seed);
+=C2=A0 =C2=A0 if (EFI_ERROR (Status)) {
+=C2=A0 =C2=A0 =C2=A0 return Status;
+=C2=A0 =C2=A0 }
+=C2=A0 =C2=A0 CopyMem (Ptr, Seed, 64);

This looks like it's copying past the Seed buffer, which is 2 * sizeof= (u64) =3D 16.

+
+=C2=A0 =C2=A0 BlockCount--;
+=C2=A0 =C2=A0 Ptr =3D Ptr + 64;
+=C2=A0 }
+
+=C2=A0 //
+=C2=A0 // Populate the remained data as request.
+=C2=A0 //
+=C2=A0 Status =3D GetRandomNumber128 (Seed);
+=C2=A0 if (EFI_ERROR (Status)) {
+=C2=A0 =C2=A0 return Status;
+=C2=A0 }
+=C2=A0 CopyMem (Ptr, Seed, (Length % 64));

=
And then again.

Isn't there a better way = to fill a buffer with random data in edk2?

+
+=C2=A0 return Status;
+}
+
+/**
+=C2=A0 This function returns the maximum size of TPM2B_AUTH; this structur= e is used for an authorization value
+=C2=A0 and limits an authValue to being no larger than the largest digest = produced by a TPM.
+
+=C2=A0 @param[out] AuthSize=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0Tpm2 Auth size
+
+=C2=A0 @retval EFI_SUCCESS=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 Auth size returned.
+=C2=A0 @retval EFI_DEVICE_ERROR=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0Can not return platform auth due to device error.
+
+**/
+EFI_STATUS
+EFIAPI
+GetAuthSize (
+=C2=A0 OUT UINT16=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 *AuthSize
+=C2=A0 )
+{
+=C2=A0 EFI_STATUS=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 Status;
+=C2=A0 TPML_PCR_SELECTION=C2=A0 =C2=A0 Pcrs;
+=C2=A0 UINTN=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= Index;
+=C2=A0 UINT16=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 Diges= tSize;
+
+=C2=A0 Status =3D EFI_SUCCESS;
+
+=C2=A0 while (mAuthSize =3D=3D 0) {

Th= is is a bit odd, but ok.

+
+=C2=A0 =C2=A0 mAuthSize =3D SHA1_DIGEST_SIZE;
+=C2=A0 =C2=A0 ZeroMem (&Pcrs, sizeof (TPML_PCR_SELECTION));
+=C2=A0 =C2=A0 Status =3D Tpm2GetCapabilityPcrs (&Pcrs);
+
+=C2=A0 =C2=A0 if (EFI_ERROR (Status)) {
+=C2=A0 =C2=A0 =C2=A0 DEBUG ((DEBUG_ERROR, "Tpm2GetCapabilityPcrs fail= !\n"));
+=C2=A0 =C2=A0 =C2=A0 break;
+=C2=A0 =C2=A0 }
+
+=C2=A0 =C2=A0 DEBUG ((DEBUG_ERROR, "Tpm2GetCapabilityPcrs - %08x\n&qu= ot;, Pcrs.count));
+
+=C2=A0 =C2=A0 for (Index =3D 0; Index < Pcrs.count; Index++) {
+=C2=A0 =C2=A0 =C2=A0 DEBUG ((DEBUG_ERROR, "alg - %x\n", Pcrs.pcr= Selections[Index].hash));
+
+=C2=A0 =C2=A0 =C2=A0 switch (Pcrs.pcrSelections[Index].hash) {
+=C2=A0 =C2=A0 =C2=A0 case TPM_ALG_SHA1:
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 DigestSize =3D SHA1_DIGEST_SIZE;
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 break;
+=C2=A0 =C2=A0 =C2=A0 case TPM_ALG_SHA256:
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 DigestSize =3D SHA256_DIGEST_SIZE;
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 break;
+=C2=A0 =C2=A0 =C2=A0 case TPM_ALG_SHA384:
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 DigestSize =3D SHA384_DIGEST_SIZE;
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 break;
+=C2=A0 =C2=A0 =C2=A0 case TPM_ALG_SHA512:
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 DigestSize =3D SHA512_DIGEST_SIZE;
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 break;
+=C2=A0 =C2=A0 =C2=A0 case TPM_ALG_SM3_256:
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 DigestSize =3D SM3_256_DIGEST_SIZE;
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 break;
+=C2=A0 =C2=A0 =C2=A0 default:
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 DigestSize =3D SHA1_DIGEST_SIZE;
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 break;
+=C2=A0 =C2=A0 =C2=A0 }
+
+=C2=A0 =C2=A0 =C2=A0 if (DigestSize > mAuthSize) {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 mAuthSize =3D DigestSize;
+=C2=A0 =C2=A0 =C2=A0 }
+=C2=A0 =C2=A0 }
+=C2=A0 =C2=A0 break;
+=C2=A0 }
+
+=C2=A0 *AuthSize =3D mAuthSize;
+=C2=A0 return Status;
+}
+
+/**
+=C2=A0 Set PlatformAuth to random value.
+**/
+VOID
+RandomizePlatformAuth (
+=C2=A0 VOID
+=C2=A0 )
+{
+=C2=A0 EFI_STATUS=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 Status;
+=C2=A0 UINT16=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 AuthSize;
+=C2=A0 UINT8=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0*Rand;
+=C2=A0 UINTN=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0RandSize;
+=C2=A0 TPM2B_AUTH=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 NewPlatformAuth;
+
+=C2=A0 //
+=C2=A0 // Send Tpm2HierarchyChange Auth with random value to avoid Platfor= mAuth being null
+=C2=A0 //
+
+=C2=A0 GetAuthSize (&AuthSize);
+
+=C2=A0 ZeroMem (NewPlatformAuth.buffer, AuthSize);
+=C2=A0 NewPlatformAuth.size =3D AuthSize;
+
+=C2=A0 //
+=C2=A0 // Allocate one buffer to store random data.
+=C2=A0 //
+=C2=A0 RandSize =3D MAX_NEW_AUTHORIZATION_SIZE;
+=C2=A0 Rand =3D AllocatePool (RandSize);
+
+=C2=A0 RdRandGenerateEntropy (RandSize, Rand);
+=C2=A0 CopyMem (NewPlatformAuth.buffer, Rand, AuthSize);
<= div>
Why generate random data for MAX_NEW_AUTHORIZATION to on= ly copy a subset after?

+
+=C2=A0 FreePool (Rand);
+
+=C2=A0 //
+=C2=A0 // Send Tpm2HierarchyChangeAuth command with the new Auth value
+=C2=A0 //
+=C2=A0 Status =3D Tpm2HierarchyChangeAuth (TPM_RH_PLATFORM, NULL, &New= PlatformAuth);
+=C2=A0 DEBUG ((DEBUG_INFO, "Tpm2HierarchyChangeAuth Result: - %r\n&qu= ot;, Status));
+=C2=A0 ZeroMem (NewPlatformAuth.buffer, AuthSize);
+=C2=A0 ZeroMem (Rand, RandSize);

Isn&#= 39;t Rand free at this point?

+}
+
+/**
+=C2=A0 =C2=A0This service defines the configuration of the Platform Hierar= chy Authorization Value (platformAuth)
+=C2=A0 =C2=A0and Platform Hierarchy Authorization Policy (platformPolicy)<= br> +
+**/
+VOID
+EFIAPI
+ConfigureTpmPlatformHierarchy (
+=C2=A0 )
+{
+=C2=A0 RandomizePlatformAuth ();
+}
diff --git a/OvmfPkg/Library/PeiDxeTpmPlatformHierarchyLib/PeiDxeTpmPlatfor= mHierarchyLib.inf b/OvmfPkg/Library/PeiDxeTpmPlatformHierarchyLib/PeiDxeTpm= PlatformHierarchyLib.inf
new file mode 100644
index 0000000000..a413e02302
--- /dev/null
+++ b/OvmfPkg/Library/PeiDxeTpmPlatformHierarchyLib/PeiDxeTpmPlatformHierar= chyLib.inf
@@ -0,0 +1,40 @@
+### @file
+#
+#=C2=A0 =C2=A0TPM Platform Hierarchy configuration library.
+#
+#=C2=A0 =C2=A0This library provides functions for customizing the TPM'= s Platform Hierarchy
+#=C2=A0 =C2=A0Authorization Value (platformAuth) and Platform Hierarchy Au= thorization
+#=C2=A0 =C2=A0Policy (platformPolicy) can be defined through this function= .
+#
+# Copyright (c) 2019, Intel Corporation. All rights reserved.<BR> +# Copyright (c) Microsoft Corporation.<BR>
+#
+# SPDX-License-Identifier: BSD-2-Clause-Patent
+#
+###
+
+[Defines]
+=C2=A0 INF_VERSION=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =3D 0x00010005
+=C2=A0 BASE_NAME=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =3D PeiDxeTpmPlatformHierarchyLib
+=C2=A0 FILE_GUID=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =3D 7794F92C-4E8E-4E57-9E4A-49A0764C7D73
+=C2=A0 MODULE_TYPE=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =3D PEIM
+=C2=A0 VERSION_STRING=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0=3D 1.0
+=C2=A0 LIBRARY_CLASS=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =3D TpmPlatformHierarchyLib|PEIM DXE_DRIVER
+
+[LibraryClasses]
+=C2=A0 BaseLib
+=C2=A0 BaseMemoryLib
+=C2=A0 DebugLib
+=C2=A0 MemoryAllocationLib
+=C2=A0 RngLib
+=C2=A0 Tpm2CommandLib
+=C2=A0 Tpm2DeviceLib
+
+[Packages]
+=C2=A0 MdePkg/MdePkg.dec
+=C2=A0 MdeModulePkg/MdeModulePkg.dec
+=C2=A0 SecurityPkg/SecurityPkg.dec
+=C2=A0 CryptoPkg/CryptoPkg.dec
+
+[Sources]
+=C2=A0 PeiDxeTpmPlatformHierarchyLib.c
--
2.31.1

--0000000000001d6bce05c95cfe5e--