From nobody Sat Apr 27 23:08:45 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) client-ip=66.175.222.12; envelope-from=bounce+27952+64126+1787277+3901457@groups.io; helo=web01.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+64126+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1597272224; cv=none; d=zohomail.com; s=zohoarc; b=cZniy89LKtNcSwC8CorXBjbnLHkCfGjkAzIUjI9HHKWaKDoYHKlXPq2mCEzVHCwfZY4sOt5ZOSb5BkrM6MDt/6IhvJnkPNofMAWOgTOmL/C9++6jYG+z0hXp88qyBMvsY2qr6ai33py4Rc18xyujNnbPBnRLDUp7bsIkWG2of6g= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1597272224; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Id:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=yyTaqSMwspjAzg0eMdM3zuKJoQwfES9PVqbhP5ILZ80=; b=ityQFru4DQ/dVTMmYctrO6UdAL2qo5Z3uoRko8BR7dSdOE8QIAqFrDrlqK+FCfz1mgCrn6X0G0orjq21ehb+Ry9bB7xSvyIVt0VfYNAmHX6k1gx9iW0vZ/E5ybKH9m9ueKrh9odFC227g8ILYp/w+2mtpP00ITduv+UTRGg+t5c= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+64126+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) header.from= Received: from web01.groups.io (web01.groups.io [66.175.222.12]) by mx.zohomail.com with SMTPS id 1597272224695640.7401483204856; Wed, 12 Aug 2020 15:43:44 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id dXYyYY1788612xADQrZFvClS; Wed, 12 Aug 2020 15:43:43 -0700 X-Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) by mx.groups.io with SMTP id smtpd.web12.1316.1597272222417614485 for ; Wed, 12 Aug 2020 15:43:42 -0700 X-Received: by mail-pl1-f177.google.com with SMTP id y6so1761056plt.3 for ; Wed, 12 Aug 2020 15:43:42 -0700 (PDT) X-Gm-Message-State: 7hEEelqKp5hCQRurGmGlM2pOx1787277AA= X-Google-Smtp-Source: ABdhPJx+EwlZvwJRPz2iQWnKNnyMI/czHf9a4ur89gM2EiY3OFup9AZXEdSVBmc3ZxrQomashMg7RA== X-Received: by 2002:a17:90a:a794:: with SMTP id f20mr2252557pjq.74.1597272221727; Wed, 12 Aug 2020 15:43:41 -0700 (PDT) X-Received: from tvis-name-05.localdomain ([50.34.40.129]) by smtp.gmail.com with ESMTPSA id l63sm3252943pgl.24.2020.08.12.15.43.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Aug 2020 15:43:41 -0700 (PDT) From: "Matthew Carlson" To: devel@edk2.groups.io Cc: Ard Biesheuvel , Michael D Kinney , Liming Gao , Zhiguang Liu , Matthew Carlson Subject: [edk2-devel] [PATCH v6 1/5] MdePkg: TimerRngLib: Added RngLib that uses TimerLib Date: Wed, 12 Aug 2020 15:43:34 -0700 Message-Id: <20200812224338.287-2-matthewfcarlson@gmail.com> In-Reply-To: <20200812224338.287-1-matthewfcarlson@gmail.com> References: <20200812224338.287-1-matthewfcarlson@gmail.com> MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: 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,matthewfcarlson@gmail.com Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1597272223; bh=nXtUE3VhkBFmsv6RwD7dMvQbfbIrChfNakdtcEzs1So=; h=Cc:Date:From:Reply-To:Subject:To; b=Sqt7USDgbYLJYwqAJ7gSoy/aEiwWj/gfr9i071xNqTyxIVsM7z3bquflE0Z173+rHZq Il2jy6UhTUEq8KViVLkB8pKG2oRccCsA13WQQzn1jMR25xaFkQMvBE6OdKAYyeEf1wCnF oPhH5h6U5L0dfGIFO31T9kil5fZYWBd4c+w= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Type: text/plain; charset="utf-8" From: Matthew Carlson Added a new RngLib that provides random numbers from the TimerLib using the performance counter. This is meant to be used for OpenSSL to replicate past behavior. This should not be used in production as a real source of entropy. Ref: https://github.com/tianocore/edk2/pull/845 Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=3D1871 Cc: Ard Biesheuvel Cc: Michael D Kinney Cc: Liming Gao Cc: Zhiguang Liu Signed-off-by: Matthew Carlson --- MdePkg/Library/BaseRngLibTimerLib/RngLibTimer.c | 187 +++++++++++= +++++++++ MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.inf | 40 +++++ MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.uni | 17 ++ MdePkg/MdePkg.dsc | 3 +- 4 files changed, 246 insertions(+), 1 deletion(-) diff --git a/MdePkg/Library/BaseRngLibTimerLib/RngLibTimer.c b/MdePkg/Libra= ry/BaseRngLibTimerLib/RngLibTimer.c new file mode 100644 index 000000000000..915382fb9278 --- /dev/null +++ b/MdePkg/Library/BaseRngLibTimerLib/RngLibTimer.c @@ -0,0 +1,187 @@ +/** @file + BaseRng Library that uses the TimerLib to provide reasonably random numb= ers. + Do not use this on a production system. + + Copyright (c) Microsoft Corporation. + SPDX-License-Identifier: BSD-2-Clause-Patent +**/ + +#include +#include +#include +#include + +/** + * Using the TimerLib GetPerformanceCounterProperties() we delay + * for enough time for the PerformanceCounter to increment. + * Depending on your system + * + * If the return value from GetPerformanceCounterProperties (TimerLib) + * is zero, this function will not delay and attempt to assert. + */ +VOID +EFIAPI +DecentDelay( + VOID + ) +{ + UINT64 StartValue; + UINT64 EndValue; + UINT64 CounterHz; + UINT64 MinumumDelayInMicroSeconds; + // Get the counter properties + CounterHz =3D GetPerformanceCounterProperties(&StartValue, &EndValue); + // Make sure we won't divide by zero + if (CounterHz =3D=3D 0) { + ASSERT(FALSE); // Assert so the developer knows something is wrong + return; + } + // Calculate the minimum delay based on 1.5 microseconds divided by the = hertz. + // We calculate the length of a cycle (1/CounterHz) and multiply it by 1= .5 microseconds + // This ensures that the performance counter has increased by at least o= ne + MinumumDelayInMicroSeconds =3D 1500000 / CounterHz; + + MicroSecondDelay(MinumumDelayInMicroSeconds); +} + + +/** + Generates a 16-bit random number. + + if Rand is NULL, then ASSERT(). + + @param[out] Rand Buffer pointer to store the 16-bit random value. + + @retval TRUE Random number generated successfully. + @retval FALSE Failed to generate the random number. + +**/ +BOOLEAN +EFIAPI +GetRandomNumber16 ( + OUT UINT16 *Rand + ) +{ + UINT32 Index; + UINT8* RandPtr; + + ASSERT (Rand !=3D NULL); + + if (NULL =3D=3D Rand) { + return FALSE; + } + + RandPtr =3D (UINT8 *) Rand; + // Get 2 bytes of random ish data + // This should take around 10us + for (Index =3D 0; Index < 2; Index ++) { + *RandPtr =3D (UINT8) (GetPerformanceCounter () & 0xFF); + DecentDelay (); // delay to give chance for performance counter to cat= ch up + RandPtr++; + } + return TRUE; +} + +/** + Generates a 32-bit random number. + + if Rand is NULL, then ASSERT(). + + @param[out] Rand Buffer pointer to store the 32-bit random value. + + @retval TRUE Random number generated successfully. + @retval FALSE Failed to generate the random number. + +**/ +BOOLEAN +EFIAPI +GetRandomNumber32 ( + OUT UINT32 *Rand + ) +{ + UINT32 Index; + UINT8* RandPtr; + + ASSERT (Rand !=3D NULL); + + if (NULL =3D=3D Rand) { + return FALSE; + } + + RandPtr =3D (UINT8 *) Rand; + // Get 4 bytes of random ish data + // This should take around 20ms + for (Index =3D 0; Index < 4; Index ++) { + *RandPtr =3D (UINT8) (GetPerformanceCounter () & 0xFF); + DecentDelay (); // delay to give chance for performance counter to cat= ch up + RandPtr++; + } + return TRUE; +} + +/** + Generates a 64-bit random number. + + if Rand is NULL, then ASSERT(). + + @param[out] Rand Buffer pointer to store the 64-bit random value. + + @retval TRUE Random number generated successfully. + @retval FALSE Failed to generate the random number. + +**/ +BOOLEAN +EFIAPI +GetRandomNumber64 ( + OUT UINT64 *Rand + ) +{ + UINT32 Index; + UINT8* RandPtr; + + ASSERT (Rand !=3D NULL); + + if (NULL =3D=3D Rand) { + return FALSE; + } + + RandPtr =3D (UINT8 *) Rand; + // Get 8 bytes of random ish data + // This should take around 40ms + for (Index =3D 0; Index < 8; Index ++) { + *RandPtr =3D (UINT8) (GetPerformanceCounter () & 0xFF); + DecentDelay (); // delay to give chance for performance counter to cat= ch up + RandPtr++; + } + + return TRUE; +} + +/** + Generates a 128-bit random number. + + if Rand is NULL, then ASSERT(). + + @param[out] Rand Buffer pointer to store the 128-bit random value. + + @retval TRUE Random number generated successfully. + @retval FALSE Failed to generate the random number. + +**/ +BOOLEAN +EFIAPI +GetRandomNumber128 ( + OUT UINT64 *Rand + ) +{ + ASSERT (Rand !=3D NULL); + // This should take around 80ms + + // Read first 64 bits + if (!GetRandomNumber64 (Rand)) { + return FALSE; + } + + // Read second 64 bits + return GetRandomNumber64 (++Rand); +} diff --git a/MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.inf b/Mde= Pkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.inf new file mode 100644 index 000000000000..34dea0152497 --- /dev/null +++ b/MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.inf @@ -0,0 +1,40 @@ +## @file +# Instance of RNG (Random Number Generator) Library. +# +# BaseRng Library that uses the TimerLib to provide reasonably random num= bers. +# Do NOT use this on a production system as this uses the system performa= nce +# counter rather than a true source of random in addition to having a weak +# random algorithm. This is provided primarily as a source of entropy for +# OpenSSL for platforms that do not have a good built in RngLib as this +# emulates what was done before (though it isn't perfect). +# +# Copyright (c) Microsoft Corporation. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +# +## + +[Defines] + INF_VERSION =3D 0x00010005 + BASE_NAME =3D BaseRngLibTimerLib + MODULE_UNI_FILE =3D BaseRngLibTimerLib.uni + FILE_GUID =3D 74950C45-10FC-4AB5-B114-49C87C17409B + MODULE_TYPE =3D BASE + VERSION_STRING =3D 1.0 + LIBRARY_CLASS =3D RngLib + CONSTRUCTOR =3D BaseRngLibConstructor + +# +# VALID_ARCHITECTURES =3D IA32 X64 +# + +[Sources] + RngLibTimer.c + +[Packages] + MdePkg/MdePkg.dec + +[LibraryClasses] + BaseLib + TimerLib diff --git a/MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.uni b/Mde= Pkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.uni new file mode 100644 index 000000000000..766a8e0ddf97 --- /dev/null +++ b/MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.uni @@ -0,0 +1,17 @@ +// /** @file +// Instance of RNG (Random Number Generator) Library. +// +// BaseRng Library that uses TimerLib's performance counter +// to provide random numbers. +// +// Copyright (c) Microsoft Corporation +// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + + +#string STR_MODULE_ABSTRACT #language en-US "Instance of RNG L= ibrary" + +#string STR_MODULE_DESCRIPTION #language en-US "BaseRng Library t= hat uses the TimerLib to provide low-entropy random numbers" + diff --git a/MdePkg/MdePkg.dsc b/MdePkg/MdePkg.dsc index 472fa3777412..d7ba3a730909 100644 --- a/MdePkg/MdePkg.dsc +++ b/MdePkg/MdePkg.dsc @@ -62,6 +62,8 @@ MdePkg/Library/BasePostCodeLibPort80/BasePostCodeLibPort80.inf MdePkg/Library/BasePrintLib/BasePrintLib.inf MdePkg/Library/BaseReportStatusCodeLibNull/BaseReportStatusCodeLibNull.i= nf + MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.inf + MdePkg/Library/BaseRngLibNull/BaseRngLibNull.inf MdePkg/Library/BaseSerialPortLibNull/BaseSerialPortLibNull.inf MdePkg/Library/BaseSynchronizationLib/BaseSynchronizationLib.inf MdePkg/Library/BaseTimerLibNullTemplate/BaseTimerLibNullTemplate.inf @@ -69,7 +71,6 @@ MdePkg/Library/BaseUefiDecompressLib/BaseUefiTianoCustomDecompressLib.inf MdePkg/Library/BaseSmbusLibNull/BaseSmbusLibNull.inf MdePkg/Library/BaseSafeIntLib/BaseSafeIntLib.inf - MdePkg/Library/BaseRngLibNull/BaseRngLibNull.inf =20 MdePkg/Library/DxeCoreEntryPoint/DxeCoreEntryPoint.inf MdePkg/Library/DxeCoreHobLib/DxeCoreHobLib.inf --=20 2.27.0.windows.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#64126): https://edk2.groups.io/g/devel/message/64126 Mute This Topic: https://groups.io/mt/76157313/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- From nobody Sat Apr 27 23:08:45 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) client-ip=66.175.222.12; envelope-from=bounce+27952+64127+1787277+3901457@groups.io; helo=web01.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+64127+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1597272227; cv=none; d=zohomail.com; s=zohoarc; b=BGwOZFx28MEh7YiMz1w8T7QFC3KdD+MCtmrebCfHb6FlwU/ky7WkFQbxW8YC+Q1Rd8r8SPuqCjB+AtD5Zd0PgEKpGzh6/MAtU1Lyq37ObTJoehHH8uxIm5lQ0B55GILazYudQInOGKLTA1Xz1rz1Kk4/lzBam3Mqf1pLRK+Adkw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1597272227; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Id:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=XNTKpWh5EPm6ybwkh6sUYqPsZnZKk1vhukyYkF7uBeM=; b=NKaIyDMTEEayWr8zfFwAQ07BICxeKrKfP+8bvpF6BvhZ1xgaGTkBAidG6YMhIgaFUI4f6uHmh3o6/zm4ZJ1Ctg7N+R33xFE5MURexVwJF+YPOhH60G26naFTcDN2+rWbnC5bFTkacDZRamrPAx1KlgCzd/1jEiIHO8bNn9pEN4Y= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+64127+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) header.from= Received: from web01.groups.io (web01.groups.io [66.175.222.12]) by mx.zohomail.com with SMTPS id 1597272227787470.27783475357126; Wed, 12 Aug 2020 15:43:47 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id HRyXYY1788612xtBAUufEehg; Wed, 12 Aug 2020 15:43:46 -0700 X-Received: from mail-pg1-f169.google.com (mail-pg1-f169.google.com [209.85.215.169]) by mx.groups.io with SMTP id smtpd.web10.1324.1597272223366086929 for ; Wed, 12 Aug 2020 15:43:43 -0700 X-Received: by mail-pg1-f169.google.com with SMTP id 128so1787569pgd.5 for ; Wed, 12 Aug 2020 15:43:43 -0700 (PDT) X-Gm-Message-State: dgRqgfOLSWcsYbIzcbe2sf4Mx1787277AA= X-Google-Smtp-Source: ABdhPJztXQgbcHeDvAf8N19haUckh0oCe+bT098BvZZ4mUDVJfNeEPsDdaeX2KdbwxgRFEwmlVMTkw== X-Received: by 2002:aa7:9712:: with SMTP id a18mr1494881pfg.197.1597272222625; Wed, 12 Aug 2020 15:43:42 -0700 (PDT) X-Received: from tvis-name-05.localdomain ([50.34.40.129]) by smtp.gmail.com with ESMTPSA id l63sm3252943pgl.24.2020.08.12.15.43.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Aug 2020 15:43:42 -0700 (PDT) From: "Matthew Carlson" To: devel@edk2.groups.io Cc: Ard Biesheuvel , Michael D Kinney , Liming Gao , Zhiguang Liu , Matthew Carlson Subject: [edk2-devel] [PATCH v6 2/5] MdePkg: BaseRngLibDxe: Add RngLib that uses RngDxe Date: Wed, 12 Aug 2020 15:43:35 -0700 Message-Id: <20200812224338.287-3-matthewfcarlson@gmail.com> In-Reply-To: <20200812224338.287-1-matthewfcarlson@gmail.com> References: <20200812224338.287-1-matthewfcarlson@gmail.com> MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: 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,matthewfcarlson@gmail.com Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1597272226; bh=vw0yxJCksjRjrE4Np8J+fmOCs+EoZzB7qqh5172fets=; h=Cc:Date:From:Reply-To:Subject:To; b=l/uZAyryQsJwOPcZ9Tdjpp1F1H4T8QIQTBHToKzlvH+bTIeQbMwAzbgz4HDdKnSqrEc 1yfwEW3wBW4/KUTwp+p5ADNsjqmal0KA32DLH9V7OUvh/SjuueidrGs6cszanJzlXKHxr kdspVPkDggdXG4B/nlxX93z1AA8SuJmZr7E= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Type: text/plain; charset="utf-8" From: Matthew Carlson This adds a RngLib that uses the RngProtocol to provide randomness. This means that the RngLib is meant to be used with DXE_DRIVERS. Ref: https://github.com/tianocore/edk2/pull/845 Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=3D1871 Cc: Ard Biesheuvel Cc: Michael D Kinney Cc: Liming Gao Cc: Zhiguang Liu Signed-off-by: Matthew Carlson --- MdePkg/Library/BaseRngLibDxe/RngDxeLib.c | 200 ++++++++++++++++++++ MdePkg/Library/BaseRngLibDxe/BaseRngLibDxe.inf | 38 ++++ MdePkg/MdePkg.dsc | 4 +- 3 files changed, 241 insertions(+), 1 deletion(-) diff --git a/MdePkg/Library/BaseRngLibDxe/RngDxeLib.c b/MdePkg/Library/Base= RngLibDxe/RngDxeLib.c new file mode 100644 index 000000000000..8ee29329de13 --- /dev/null +++ b/MdePkg/Library/BaseRngLibDxe/RngDxeLib.c @@ -0,0 +1,200 @@ +/** @file + Provides an implementation of the library class RngLib that uses the Rng = protocol. + +Copyright (c) Microsoft Corporation. All rights reserved. +SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ +#include +#include +#include +#include +#include + +/** +Routine Description: + + Generates a random number via the NIST + 800-9A algorithm. Refer to + http://csrc.nist.gov/groups/STM/cavp/documents/drbg/DRBGVS.pdf + for more information. + + Arguments: + + Buffer -- Buffer to receive the random number. + BufferSize -- Number of bytes in Buffer. + +Return Value: + + EFI_SUCCESS or underlying failure code. + +**/ +EFI_STATUS +EFIAPI +GenerateRandomNumberViaNist800Algorithm( + OUT UINT8* Buffer, + IN UINTN BufferSize + ) +{ + EFI_STATUS Status; + EFI_RNG_PROTOCOL* RngProtocol; + + RngProtocol =3D NULL; + + if (Buffer =3D=3D NULL) { + DEBUG((DEBUG_ERROR, "[%a] Buffer =3D=3D NULL.\n", __FUNCTION__)); + return EFI_INVALID_PARAMETER; + } + + Status =3D gBS->LocateProtocol(&gEfiRngProtocolGuid, NULL, (VOID **)&Rng= Protocol); + if (EFI_ERROR(Status) || RngProtocol =3D=3D NULL) { + DEBUG((DEBUG_ERROR, "[%a] Could not locate RNG prototocol, Status = =3D %r\n", __FUNCTION__, Status)); + return Status; + } + + Status =3D RngProtocol->GetRNG(RngProtocol, &gEfiRngAlgorithmSp80090Ctr2= 56Guid, BufferSize, Buffer); + DEBUG((DEBUG_INFO, "[%a] GetRNG algorithm CTR-256 - Status =3D %r\n", __= FUNCTION__, Status)); + if(!EFI_ERROR(Status)) { + return Status; + } + + Status =3D RngProtocol->GetRNG(RngProtocol, &gEfiRngAlgorithmSp80090Hmac= 256Guid, BufferSize, Buffer); + DEBUG((DEBUG_INFO, "[%a] GetRNG algorithm HMAC-256 - Status =3D %r\n", _= _FUNCTION__, Status)); + if(!EFI_ERROR(Status)) { + return Status; + } + + Status =3D RngProtocol->GetRNG(RngProtocol, &gEfiRngAlgorithmSp80090Hash= 256Guid, BufferSize, Buffer); + DEBUG((DEBUG_INFO, "[%a] GetRNG algorithm Hash-256 - Status =3D %r\n", _= _FUNCTION__, Status)); + if(!EFI_ERROR(Status)) { + return Status; + } + // If we get to this point, we have failed + DEBUG((DEBUG_ERROR, "[%a] GetRNG() failed, staus =3D %r\n", __FUNCTION__= , Status)); + + return Status; +}// GenerateRandomNumberViaNist800Algorithm() + + +/** + Generates a 16-bit random number. + + if Rand is NULL, return FALSE. + + @param[out] Rand Buffer pointer to store the 16-bit random value. + + @retval TRUE Random number generated successfully. + @retval FALSE Failed to generate the random number. + +**/ +BOOLEAN +EFIAPI +GetRandomNumber16 ( + OUT UINT16 *Rand + ) +{ + EFI_STATUS Status; + + if (Rand =3D=3D NULL) + { + return FALSE; + } + + Status =3D GenerateRandomNumberViaNist800Algorithm ((UINT8 *)Rand, 2); + if (EFI_ERROR(Status)) + { + return FALSE; + } + return TRUE; +} + +/** + Generates a 32-bit random number. + + if Rand is NULL, return FALSE. + + @param[out] Rand Buffer pointer to store the 32-bit random value. + + @retval TRUE Random number generated successfully. + @retval FALSE Failed to generate the random number. + +**/ +BOOLEAN +EFIAPI +GetRandomNumber32 ( + OUT UINT32 *Rand + ) +{ + EFI_STATUS Status; + + if (Rand =3D=3D NULL) { + return FALSE; + } + + Status =3D GenerateRandomNumberViaNist800Algorithm ((UINT8 *)Rand, 4); + if (EFI_ERROR(Status)) { + return FALSE; + } + return TRUE; +} + +/** + Generates a 64-bit random number. + + if Rand is NULL, return FALSE. + + @param[out] Rand Buffer pointer to store the 64-bit random value. + + @retval TRUE Random number generated successfully. + @retval FALSE Failed to generate the random number. + +**/ +BOOLEAN +EFIAPI +GetRandomNumber64 ( + OUT UINT64 *Rand + ) +{ + EFI_STATUS Status; + + if (Rand =3D=3D NULL) + { + return FALSE; + } + + Status =3D GenerateRandomNumberViaNist800Algorithm ((UINT8 *)Rand, 8); + if (EFI_ERROR(Status)) { + return FALSE; + } + return TRUE; +} + +/** + Generates a 128-bit random number. + + if Rand is NULL, return FALSE. + + @param[out] Rand Buffer pointer to store the 128-bit random value. + + @retval TRUE Random number generated successfully. + @retval FALSE Failed to generate the random number. + +**/ +BOOLEAN +EFIAPI +GetRandomNumber128 ( + OUT UINT64 *Rand + ) +{ + EFI_STATUS Status; + + if (Rand =3D=3D NULL) { + return FALSE; + } + + Status =3D GenerateRandomNumberViaNist800Algorithm ((UINT8 *)Rand, 16); + if (EFI_ERROR(Status)) { + return FALSE; + } + return TRUE; +} diff --git a/MdePkg/Library/BaseRngLibDxe/BaseRngLibDxe.inf b/MdePkg/Librar= y/BaseRngLibDxe/BaseRngLibDxe.inf new file mode 100644 index 000000000000..819a106b1376 --- /dev/null +++ b/MdePkg/Library/BaseRngLibDxe/BaseRngLibDxe.inf @@ -0,0 +1,38 @@ +# @file +# Provides implementation of the library class RngLib that uses the RngPro= tocol +# +# @copyright +# Copyright (c) Microsoft Corporation. All rights reserved. +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +# MU_CHANGE: New file +## + +[Defines] + INF_VERSION =3D 0x00010017 + BASE_NAME =3D BaseRngLibDxe + FILE_GUID =3D FF9F84C5-A33E-44E3-9BB5-0D654B2D4149 + MODULE_TYPE =3D DXE_DRIVER + VERSION_STRING =3D 1.0 + LIBRARY_CLASS =3D RngLib|DXE_DRIVER UEFI_APPLICATION UEFI_DRIVER + +[Packages] + MdePkg/MdePkg.dec + +[Sources] + RngDxeLib.c + +[LibraryClasses] + DebugLib + UefiBootServicesTableLib + +[Protocols] + gEfiRngProtocolGuid ## CONSUMES + +[Depex] + gEfiRngProtocolGuid + +[Guids] + gEfiRngAlgorithmSp80090Ctr256Guid ## CONSUMES + gEfiRngAlgorithmSp80090Hash256Guid ## CONSUMES + gEfiRngAlgorithmSp80090Hmac256Guid ## CONSUMES diff --git a/MdePkg/MdePkg.dsc b/MdePkg/MdePkg.dsc index d7ba3a730909..837a0047400e 100644 --- a/MdePkg/MdePkg.dsc +++ b/MdePkg/MdePkg.dsc @@ -62,8 +62,10 @@ MdePkg/Library/BasePostCodeLibPort80/BasePostCodeLibPort80.inf MdePkg/Library/BasePrintLib/BasePrintLib.inf MdePkg/Library/BaseReportStatusCodeLibNull/BaseReportStatusCodeLibNull.i= nf - MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.inf + MdePkg/Library/BaseRngLibDxe/BaseRngLibDxe.inf MdePkg/Library/BaseRngLibNull/BaseRngLibNull.inf + MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.inf + MdePkg/Library/BaseSerialPortLibNull/BaseSerialPortLibNull.inf MdePkg/Library/BaseSynchronizationLib/BaseSynchronizationLib.inf MdePkg/Library/BaseTimerLibNullTemplate/BaseTimerLibNullTemplate.inf --=20 2.27.0.windows.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#64127): https://edk2.groups.io/g/devel/message/64127 Mute This Topic: https://groups.io/mt/76157314/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- From nobody Sat Apr 27 23:08:45 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) client-ip=66.175.222.12; envelope-from=bounce+27952+64128+1787277+3901457@groups.io; helo=web01.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+64128+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1597272246; cv=none; d=zohomail.com; s=zohoarc; b=UShUNC+QcHYyPVHYWDllf8Kl75oY3Np0BYe9uaKUyYp0ul6cRoBMiRgeLhMLxcfQg51HYAdlDjpYku24Hjo23WdGRrc36tj9KupasI7NoKrs6vDVRqAPXXXV1jDrkWZQoVcSD5+lxL7vZvNP+/shR99Ar0a2KJrhg5Ie0ZvPXNE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1597272246; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Id:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=j+NdCBGtXbpeVUomRp4aVNvIXN1+Ppvk9PmDFtvTcfc=; b=S+yDIeukEu9Emo9iLQHky+ocH8Bpo7tmdoKTplQ335edWRlIBZQy+sjyOVqjYIbDSvOb0xXnGzn+I7wBDhaR0ac+6STEiTotcebbaQlx9LmLCuJn6h8UNZrQfGRiPT++w837B9/N4xQN4Ce5HzF5HslKVcXJncijXy6YOeHCxAE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+64128+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) header.from= Received: from web01.groups.io (web01.groups.io [66.175.222.12]) by mx.zohomail.com with SMTPS id 1597272246662451.2893250608158; Wed, 12 Aug 2020 15:44:06 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id 6XOMYY1788612xAnghVhWwF5; Wed, 12 Aug 2020 15:44:06 -0700 X-Received: from mail-pj1-f54.google.com (mail-pj1-f54.google.com [209.85.216.54]) by mx.groups.io with SMTP id smtpd.web12.1322.1597272245734006578 for ; Wed, 12 Aug 2020 15:44:05 -0700 X-Received: by mail-pj1-f54.google.com with SMTP id mw10so1844846pjb.2 for ; Wed, 12 Aug 2020 15:44:05 -0700 (PDT) X-Gm-Message-State: 1BG11wzTyZULwAyAmA9r4C6Gx1787277AA= X-Google-Smtp-Source: ABdhPJxZ4S1X3WudFrgstFK9L8gs61v8jyhVr/LQguNYFk1f14bTdkB6qu/00um6blinaId7EASkFA== X-Received: by 2002:a17:902:50d:: with SMTP id 13mr1352313plf.311.1597272245084; Wed, 12 Aug 2020 15:44:05 -0700 (PDT) X-Received: from tvis-name-05.localdomain ([50.34.40.129]) by smtp.gmail.com with ESMTPSA id l63sm3252943pgl.24.2020.08.12.15.44.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Aug 2020 15:44:04 -0700 (PDT) From: "Matthew Carlson" To: devel@edk2.groups.io Cc: Jordan Justen , Laszlo Ersek , Ard Biesheuvel , Anthony Perard , Julien Grall , Matthew Carlson Subject: [edk2-devel] [PATCH v6 3/5] OvmfPkg: Add RngLib based on TimerLib for Crypto Date: Wed, 12 Aug 2020 15:43:36 -0700 Message-Id: <20200812224338.287-4-matthewfcarlson@gmail.com> In-Reply-To: <20200812224338.287-1-matthewfcarlson@gmail.com> References: <20200812224338.287-1-matthewfcarlson@gmail.com> MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: 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,matthewfcarlson@gmail.com Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1597272246; bh=73HqoyKK6iL/Wv1F9M93jj/CbC9bdwBl4DzGd57PpI4=; h=Cc:Date:From:Reply-To:Subject:To; b=XMRbOMgKXfo14Z/cJdXWq2BjLdd2pEUDV09JxvMbGR2HJfxaoAB2UgsFBLL1fiSKV3m ua3iP2fR2BtjU0aQibbNrUX6zTLOa+6JqHH9jj1rX1bXc6mbv84UYTqwjf0lPjHGPHAjY lQ+AJBSZ7iQI6b93TRDlBi56zZUZ4bAy74Y= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Type: text/plain; charset="utf-8" From: Matthew Carlson Updates the DSC's for Ovmf based platforms to add a RngLib that uses the TimerLib. This is due to a later change that adds TimerLib as a dependency for OpenSSL. The TimerLib based RngLib mimics the behavior of OpenSSL previously and it is recommended to switch to a better source of entropy than the system's performance counter. Ref: https://github.com/tianocore/edk2/pull/845 Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=3D1871 Cc: Jordan Justen Cc: Laszlo Ersek Cc: Ard Biesheuvel Cc: Anthony Perard Cc: Julien Grall Signed-off-by: Matthew Carlson Reviewed-by: Laszlo Ersek Reviewed-by: Laszlo Ersek --- OvmfPkg/Bhyve/BhyvePkgX64.dsc | 1 + OvmfPkg/OvmfPkgIa32.dsc | 1 + OvmfPkg/OvmfPkgIa32X64.dsc | 1 + OvmfPkg/OvmfPkgX64.dsc | 1 + OvmfPkg/OvmfXen.dsc | 1 + 5 files changed, 5 insertions(+) diff --git a/OvmfPkg/Bhyve/BhyvePkgX64.dsc b/OvmfPkg/Bhyve/BhyvePkgX64.dsc index 8eba48d109a3..4b1fbb361b28 100644 --- a/OvmfPkg/Bhyve/BhyvePkgX64.dsc +++ b/OvmfPkg/Bhyve/BhyvePkgX64.dsc @@ -185,6 +185,7 @@ !else OpensslLib|CryptoPkg/Library/OpensslLib/OpensslLibCrypto.inf !endif + RngLib|MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.inf =20 !if $(SECURE_BOOT_ENABLE) =3D=3D TRUE PlatformSecureLib|OvmfPkg/Bhyve/Library/PlatformSecureLib/PlatformSecure= Lib.inf diff --git a/OvmfPkg/OvmfPkgIa32.dsc b/OvmfPkg/OvmfPkgIa32.dsc index 9178ffeb71cb..fc1c8014eba1 100644 --- a/OvmfPkg/OvmfPkgIa32.dsc +++ b/OvmfPkg/OvmfPkgIa32.dsc @@ -189,6 +189,7 @@ !else OpensslLib|CryptoPkg/Library/OpensslLib/OpensslLibCrypto.inf !endif + RngLib|MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.inf =20 !if $(SECURE_BOOT_ENABLE) =3D=3D TRUE PlatformSecureLib|OvmfPkg/Library/PlatformSecureLib/PlatformSecureLib.inf diff --git a/OvmfPkg/OvmfPkgIa32X64.dsc b/OvmfPkg/OvmfPkgIa32X64.dsc index a665f78f0dc7..2365dc2fa98d 100644 --- a/OvmfPkg/OvmfPkgIa32X64.dsc +++ b/OvmfPkg/OvmfPkgIa32X64.dsc @@ -193,6 +193,7 @@ !else OpensslLib|CryptoPkg/Library/OpensslLib/OpensslLibCrypto.inf !endif + RngLib|MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.inf =20 !if $(SECURE_BOOT_ENABLE) =3D=3D TRUE PlatformSecureLib|OvmfPkg/Library/PlatformSecureLib/PlatformSecureLib.inf diff --git a/OvmfPkg/OvmfPkgX64.dsc b/OvmfPkg/OvmfPkgX64.dsc index 17f345acf4ee..6992cfd98b70 100644 --- a/OvmfPkg/OvmfPkgX64.dsc +++ b/OvmfPkg/OvmfPkgX64.dsc @@ -193,6 +193,7 @@ !else OpensslLib|CryptoPkg/Library/OpensslLib/OpensslLibCrypto.inf !endif + RngLib|MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.inf =20 !if $(SECURE_BOOT_ENABLE) =3D=3D TRUE PlatformSecureLib|OvmfPkg/Library/PlatformSecureLib/PlatformSecureLib.inf diff --git a/OvmfPkg/OvmfXen.dsc b/OvmfPkg/OvmfXen.dsc index 782803cb2787..416f81f06a04 100644 --- a/OvmfPkg/OvmfXen.dsc +++ b/OvmfPkg/OvmfXen.dsc @@ -179,6 +179,7 @@ !else OpensslLib|CryptoPkg/Library/OpensslLib/OpensslLibCrypto.inf !endif + RngLib|MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.inf =20 AuthVariableLib|MdeModulePkg/Library/AuthVariableLibNull/AuthVariableLib= Null.inf VarCheckLib|MdeModulePkg/Library/VarCheckLib/VarCheckLib.inf --=20 2.27.0.windows.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#64128): https://edk2.groups.io/g/devel/message/64128 Mute This Topic: https://groups.io/mt/76157321/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- From nobody Sat Apr 27 23:08:45 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) client-ip=66.175.222.12; envelope-from=bounce+27952+64129+1787277+3901457@groups.io; helo=web01.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+64129+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1597272247; cv=none; d=zohomail.com; s=zohoarc; b=k6cJJsA2VB1eckrUmr/tWlvgLGyvHDh81tEJnRWaUS+fZikTn+YDsKOh+O2p/QFYEKAKI9FyN18Ttvuehf/Mkx0IMfvYUBVflJtWLl4x2+Ayw/WyOLyH2eR0C/d8NCzg9rs309WN4CEnrGJ4k0muawP4JvtMq16ThstFySYMO5Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1597272247; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Id:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=7lQmbEpbmbFiYVKtDPoVZ+DeK7JcJSNKeNqLpQTcH8g=; b=JZWA88d1J2C4Pyl14E9B/++qHF6oQvH+9ycRvNT8uieOQax0sn48WS80XNWxNGM7d7L0unCMv3eGCEnQGDgoHB3tXnL+DumVnWzbzsl8j7tI/Dxm1dYHjvcBZ6K8NjtZsPY4V0W0GRs4bDh7BNPAu5XlCmVRJMZI+KRlMJ61z/8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+64129+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) header.from= Received: from web01.groups.io (web01.groups.io [66.175.222.12]) by mx.zohomail.com with SMTPS id 1597272247565551.4550304077001; Wed, 12 Aug 2020 15:44:07 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id z0ReYY1788612xEOlKhLFA0j; Wed, 12 Aug 2020 15:44:07 -0700 X-Received: from mail-pf1-f170.google.com (mail-pf1-f170.google.com [209.85.210.170]) by mx.groups.io with SMTP id smtpd.web10.1328.1597272246624203340 for ; Wed, 12 Aug 2020 15:44:06 -0700 X-Received: by mail-pf1-f170.google.com with SMTP id m8so1767445pfh.3 for ; Wed, 12 Aug 2020 15:44:06 -0700 (PDT) X-Gm-Message-State: SE4OhgIOK1rxtVPqZfSItf4Ux1787277AA= X-Google-Smtp-Source: ABdhPJwcVP7XcifGolePNzZnhamS1vBTLhNJbbc/XWncm62YilusfzR9DwDp0PHccNRslFM5R7s7TA== X-Received: by 2002:a63:a84b:: with SMTP id i11mr1199719pgp.79.1597272245971; Wed, 12 Aug 2020 15:44:05 -0700 (PDT) X-Received: from tvis-name-05.localdomain ([50.34.40.129]) by smtp.gmail.com with ESMTPSA id l63sm3252943pgl.24.2020.08.12.15.44.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Aug 2020 15:44:05 -0700 (PDT) From: "Matthew Carlson" To: devel@edk2.groups.io Cc: Laszlo Ersek , Ard Biesheuvel , Leif Lindholm , Matthew Carlson Subject: [edk2-devel] [PATCH v6 4/5] ArmVirtPkg: Add RngLib based on TimerLib for CryptoPkg Date: Wed, 12 Aug 2020 15:43:37 -0700 Message-Id: <20200812224338.287-5-matthewfcarlson@gmail.com> In-Reply-To: <20200812224338.287-1-matthewfcarlson@gmail.com> References: <20200812224338.287-1-matthewfcarlson@gmail.com> MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: 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,matthewfcarlson@gmail.com Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1597272247; bh=iLeQZ7Kr095gLCLO55RStXNd6wpOpn8NUXAGSqk325I=; h=Cc:Date:From:Reply-To:Subject:To; b=P/zhSS/MRJL0sWziCJ4H1FCpPqwm6ixevuGgm8MgTGezXxge6IChi2y2amxM7iJ/3HV 4gIcn0boYhqqAZS0+7I881xxG1YuZpYtJwt0kGTkK2vKjIeb6WYXX6dFiKedbwUpw2/Lk 8QNznSKM83JQ9BKEvzc9R3Py6fyh+orD+rw= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Type: text/plain; charset="utf-8" From: Matthew Carlson Updates the DSC for the ArmVirtPkg platform to add a RngLib that uses the TimerLib. This is due to a later change that adds TimerLib as a dependency for OpenSSL. The TimerLib based RngLib mimics the behavior of OpenSSL previously and it is recommended to switch to a better source of entropy than the system's performance counter. Ref: https://github.com/tianocore/edk2/pull/845 Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=3D1871 Cc: Laszlo Ersek Cc: Ard Biesheuvel Cc: Leif Lindholm Signed-off-by: Matthew Carlson Reviewed-by: Laszlo Ersek --- ArmVirtPkg/ArmVirt.dsc.inc | 1 + 1 file changed, 1 insertion(+) diff --git a/ArmVirtPkg/ArmVirt.dsc.inc b/ArmVirtPkg/ArmVirt.dsc.inc index cf44fc73890b..cb3845d2bd37 100644 --- a/ArmVirtPkg/ArmVirt.dsc.inc +++ b/ArmVirtPkg/ArmVirt.dsc.inc @@ -160,6 +160,7 @@ OpensslLib|CryptoPkg/Library/OpensslLib/OpensslLibCrypto.inf !endif BaseCryptLib|CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf + RngLib|MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.inf =20 # # Secure Boot dependencies --=20 2.27.0.windows.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#64129): https://edk2.groups.io/g/devel/message/64129 Mute This Topic: https://groups.io/mt/76157322/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- From nobody Sat Apr 27 23:08:45 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) client-ip=66.175.222.12; envelope-from=bounce+27952+64130+1787277+3901457@groups.io; helo=web01.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+64130+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1597272248; cv=none; d=zohomail.com; s=zohoarc; b=QnI2gjqy4hkon4/7m8ni+fSZceEYxlq405OjrgzlwrazvcDtxgCLL0oCuyfovbNBadnC3k9zKVzYccySpTvoHHj+4+2MizStwxIkI76AgLUU1aYBKouOQMcci8HFaGaAZyzpu7OajtEkxCrgxpJN/fHwSzkx7fUWZJYM9dnpnvc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1597272248; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Id:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=s5MrFruAE35/5Lip8PmmMgG4LxFP0CXRXQvSUkYB9Ek=; b=bSaGFoqmRFZVpvNd36LEYUkPoDaOfg0KDkR3/zCWV2XnXOPAy/OIZ5YjJfPWqM/yi2d2rOe0xQ/9WuzFG4A6u+AW6Y2SVN/aUfeQcAUUOyCDD4XHVhC9bCI9l23+N4v9lYoSra5e0ukUQcEAsDOa4Wf9FDG3cZ6MAdyL/jBCTeU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+64130+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) header.from= Received: from web01.groups.io (web01.groups.io [66.175.222.12]) by mx.zohomail.com with SMTPS id 1597272248710388.10842307829466; Wed, 12 Aug 2020 15:44:08 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id BnRxYY1788612xMABThiRnYZ; Wed, 12 Aug 2020 15:44:08 -0700 X-Received: from mail-pg1-f179.google.com (mail-pg1-f179.google.com [209.85.215.179]) by mx.groups.io with SMTP id smtpd.web11.1408.1597272247682902720 for ; Wed, 12 Aug 2020 15:44:07 -0700 X-Received: by mail-pg1-f179.google.com with SMTP id m34so1771084pgl.11 for ; Wed, 12 Aug 2020 15:44:07 -0700 (PDT) X-Gm-Message-State: ParKLStRTza2ozL0D2Hrfexox1787277AA= X-Google-Smtp-Source: ABdhPJzSVDnhA4CVSEbh6eAXG9/zQZErRJ0q3x/EYctDFF0W0RiSeDCJNn3uYJVgygnTfI4JVUcG5A== X-Received: by 2002:a63:cd54:: with SMTP id a20mr1190860pgj.228.1597272246898; Wed, 12 Aug 2020 15:44:06 -0700 (PDT) X-Received: from tvis-name-05.localdomain ([50.34.40.129]) by smtp.gmail.com with ESMTPSA id l63sm3252943pgl.24.2020.08.12.15.44.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Aug 2020 15:44:06 -0700 (PDT) From: "Matthew Carlson" To: devel@edk2.groups.io Cc: Ard Biesheuvel , Jiewen Yao , Jian J Wang , Xiaoyu Lu , Matthew Carlson Subject: [edk2-devel] [PATCH v6 5/5] CryptoPkg: OpensslLib: Use RngLib to generate entropy in rand_pool Date: Wed, 12 Aug 2020 15:43:38 -0700 Message-Id: <20200812224338.287-6-matthewfcarlson@gmail.com> In-Reply-To: <20200812224338.287-1-matthewfcarlson@gmail.com> References: <20200812224338.287-1-matthewfcarlson@gmail.com> MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: 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,matthewfcarlson@gmail.com Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1597272248; bh=edVaurAi9uPGOejhFXjP87xdeh9bYV0syuXC2BS8O0s=; h=Cc:Date:From:Reply-To:Subject:To; b=HPX2A/rSZmSQ/xQmEK94ikWL+dFD8KQMJO2wKKN8YtLvQOc7f1FbTcetp4+RUd8rV3o tzrbsR4DQPNHHbdr5SkZSD1d9aqX2g9FKuH2099e0Pwm8DS92+SRfXjCh7A2xFjJ8joZo ecnxrnM0s2smpZHYmOjnCo4iayTrbC8ypqk= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Type: text/plain; charset="utf-8" From: Matthew Carlson Ref: https://github.com/tianocore/edk2/pull/845 Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=3D1871 Changes OpenSSL to no longer depend on TimerLib and instead use RngLib. This allows platforms to decide for themsevles what sort of entropy source they provide to OpenSSL and TlsLib. Cc: Ard Biesheuvel Cc: Jiewen Yao Cc: Jian J Wang Cc: Xiaoyu Lu Signed-off-by: Matthew Carlson Acked-by: Ard Biesheuvel --- CryptoPkg/Library/OpensslLib/rand_pool.c | 203 ++---------------= --- CryptoPkg/Library/OpensslLib/rand_pool_noise.c | 29 --- CryptoPkg/Library/OpensslLib/rand_pool_noise_tsc.c | 43 ----- CryptoPkg/CryptoPkg.dsc | 1 + CryptoPkg/Library/OpensslLib/OpensslLib.inf | 15 +- CryptoPkg/Library/OpensslLib/OpensslLibCrypto.inf | 15 +- CryptoPkg/Library/OpensslLib/rand_pool_noise.h | 29 --- 7 files changed, 22 insertions(+), 313 deletions(-) diff --git a/CryptoPkg/Library/OpensslLib/rand_pool.c b/CryptoPkg/Library/O= pensslLib/rand_pool.c index 9e0179b03490..3da92699fef6 100644 --- a/CryptoPkg/Library/OpensslLib/rand_pool.c +++ b/CryptoPkg/Library/OpensslLib/rand_pool.c @@ -11,53 +11,18 @@ SPDX-License-Identifier: BSD-2-Clause-Patent #include =20 #include -#include - -#include "rand_pool_noise.h" - -/** - Get some randomness from low-order bits of GetPerformanceCounter results. - And combine them to the 64-bit value - - @param[out] Rand Buffer pointer to store the 64-bit random value. - - @retval TRUE Random number generated successfully. - @retval FALSE Failed to generate. -**/ -STATIC -BOOLEAN -EFIAPI -GetRandNoise64FromPerformanceCounter( - OUT UINT64 *Rand - ) -{ - UINT32 Index; - UINT32 *RandPtr; - - if (NULL =3D=3D Rand) { - return FALSE; - } - - RandPtr =3D (UINT32 *) Rand; - - for (Index =3D 0; Index < 2; Index ++) { - *RandPtr =3D (UINT32) (GetPerformanceCounter () & 0xFF); - MicroSecondDelay (10); - RandPtr++; - } - - return TRUE; -} +#include =20 /** Calls RandomNumber64 to fill a buffer of arbitrary size with random bytes. + This is a shim layer to RngLib. =20 @param[in] Length Size of the buffer, in bytes, to fill with. @param[out] RandBuffer Pointer to the buffer to store the random res= ult. =20 - @retval EFI_SUCCESS Random bytes generation succeeded. - @retval EFI_NOT_READY Failed to request random bytes. + @retval TRUE Random bytes generation succeeded. + @retval FALSE Failed to request random bytes. =20 **/ STATIC @@ -73,17 +38,17 @@ RandGetBytes ( =20 Ret =3D FALSE; =20 + if (RandBuffer =3D=3D NULL) { + DEBUG((DEBUG_ERROR, "[OPENSSL_RAND_POOL] NULL RandBuffer. No random nu= mbers are generated and your system is not secure\n")); + ASSERT(FALSE); // Since we can't generate random numbers, we should as= sert. Otherwise we will just blow up later. + return Ret; + } + + while (Length > 0) { - // - // Get random noise from platform. - // If it failed, fallback to PerformanceCounter - // If you really care about security, you must override - // GetRandomNoise64FromPlatform. - // - Ret =3D GetRandomNoise64 (&TempRand); - if (Ret =3D=3D FALSE) { - Ret =3D GetRandNoise64FromPerformanceCounter (&TempRand); - } + // Use RngLib to get random number + Ret =3D GetRandomNumber64(&TempRand); + if (!Ret) { return Ret; } @@ -100,125 +65,6 @@ RandGetBytes ( return Ret; } =20 -/** - Creates a 128bit random value that is fully forward and backward predict= ion resistant, - suitable for seeding a NIST SP800-90 Compliant. - This function takes multiple random numbers from PerformanceCounter to e= nsure reseeding - and performs AES-CBC-MAC over the data to compute the seed value. - - @param[out] SeedBuffer Pointer to a 128bit buffer to store the rando= m seed. - - @retval TRUE Random seed generation succeeded. - @retval FALSE Failed to request random bytes. - -**/ -STATIC -BOOLEAN -EFIAPI -RandGetSeed128 ( - OUT UINT8 *SeedBuffer - ) -{ - BOOLEAN Ret; - UINT8 RandByte[16]; - UINT8 Key[16]; - UINT8 Ffv[16]; - UINT8 Xored[16]; - UINT32 Index; - UINT32 Index2; - AES_KEY AESKey; - - // - // Chose an arbitrary key and zero the feed_forward_value (FFV) - // - for (Index =3D 0; Index < 16; Index++) { - Key[Index] =3D (UINT8) Index; - Ffv[Index] =3D 0; - } - - AES_set_encrypt_key (Key, 16 * 8, &AESKey); - - // - // Perform CBC_MAC over 32 * 128 bit values, with 10us gaps between 128 = bit value - // The 10us gaps will ensure multiple reseeds within the system time wit= h a large - // design margin. - // - for (Index =3D 0; Index < 32; Index++) { - MicroSecondDelay (10); - Ret =3D RandGetBytes (16, RandByte); - if (!Ret) { - return Ret; - } - - // - // Perform XOR operations on two 128-bit value. - // - for (Index2 =3D 0; Index2 < 16; Index2++) { - Xored[Index2] =3D RandByte[Index2] ^ Ffv[Index2]; - } - - AES_encrypt (Xored, Ffv, &AESKey); - } - - for (Index =3D 0; Index < 16; Index++) { - SeedBuffer[Index] =3D Ffv[Index]; - } - - return Ret; -} - -/** - Generate high-quality entropy source. - - @param[in] Length Size of the buffer, in bytes, to fill with. - @param[out] Entropy Pointer to the buffer to store the entropy da= ta. - - @retval EFI_SUCCESS Entropy generation succeeded. - @retval EFI_NOT_READY Failed to request random data. - -**/ -STATIC -BOOLEAN -EFIAPI -RandGenerateEntropy ( - IN UINTN Length, - OUT UINT8 *Entropy - ) -{ - BOOLEAN Ret; - UINTN BlockCount; - UINT8 Seed[16]; - UINT8 *Ptr; - - BlockCount =3D Length / 16; - Ptr =3D (UINT8 *) Entropy; - - // - // Generate high-quality seed for DRBG Entropy - // - while (BlockCount > 0) { - Ret =3D RandGetSeed128 (Seed); - if (!Ret) { - return Ret; - } - CopyMem (Ptr, Seed, 16); - - BlockCount--; - Ptr =3D Ptr + 16; - } - - // - // Populate the remained data as request. - // - Ret =3D RandGetSeed128 (Seed); - if (!Ret) { - return Ret; - } - CopyMem (Ptr, Seed, (Length % 16)); - - return Ret; -} - /* * Add random bytes to the pool to acquire requested amount of entropy * @@ -238,7 +84,7 @@ size_t rand_pool_acquire_entropy(RAND_POOL *pool) buffer =3D rand_pool_add_begin(pool, bytes_needed); =20 if (buffer !=3D NULL) { - Ret =3D RandGenerateEntropy(bytes_needed, buffer); + Ret =3D RandGetBytes(bytes_needed, buffer); if (FALSE =3D=3D Ret) { rand_pool_add_end(pool, 0, 0); } else { @@ -257,13 +103,8 @@ size_t rand_pool_acquire_entropy(RAND_POOL *pool) */ int rand_pool_add_nonce_data(RAND_POOL *pool) { - struct { - UINT64 Rand; - UINT64 TimerValue; - } data =3D { 0 }; - - RandGetBytes(8, (UINT8 *)&(data.Rand)); - data.TimerValue =3D GetPerformanceCounter(); + UINT8 data[16]; + RandGetBytes(sizeof(data), data); =20 return rand_pool_add(pool, (unsigned char*)&data, sizeof(data), 0); } @@ -275,13 +116,8 @@ int rand_pool_add_nonce_data(RAND_POOL *pool) */ int rand_pool_add_additional_data(RAND_POOL *pool) { - struct { - UINT64 Rand; - UINT64 TimerValue; - } data =3D { 0 }; - - RandGetBytes(8, (UINT8 *)&(data.Rand)); - data.TimerValue =3D GetPerformanceCounter(); + UINT8 data[16]; + RandGetBytes(sizeof(data), data); =20 return rand_pool_add(pool, (unsigned char*)&data, sizeof(data), 0); } @@ -313,4 +149,3 @@ void rand_pool_cleanup(void) void rand_pool_keep_random_devices_open(int keep) { } - diff --git a/CryptoPkg/Library/OpensslLib/rand_pool_noise.c b/CryptoPkg/Lib= rary/OpensslLib/rand_pool_noise.c deleted file mode 100644 index 212834e27acc..000000000000 --- a/CryptoPkg/Library/OpensslLib/rand_pool_noise.c +++ /dev/null @@ -1,29 +0,0 @@ -/** @file - Provide rand noise source. - -Copyright (c) 2019, Intel Corporation. All rights reserved.
-SPDX-License-Identifier: BSD-2-Clause-Patent - -**/ - -#include - -/** - Get 64-bit noise source - - @param[out] Rand Buffer pointer to store 64-bit noise source - - @retval FALSE Failed to generate -**/ -BOOLEAN -EFIAPI -GetRandomNoise64 ( - OUT UINT64 *Rand - ) -{ - // - // Return FALSE will fallback to use PerformanceCounter to - // generate noise. - // - return FALSE; -} diff --git a/CryptoPkg/Library/OpensslLib/rand_pool_noise_tsc.c b/CryptoPkg= /Library/OpensslLib/rand_pool_noise_tsc.c deleted file mode 100644 index 4158106231fd..000000000000 --- a/CryptoPkg/Library/OpensslLib/rand_pool_noise_tsc.c +++ /dev/null @@ -1,43 +0,0 @@ -/** @file - Provide rand noise source. - -Copyright (c) 2019, Intel Corporation. All rights reserved.
-SPDX-License-Identifier: BSD-2-Clause-Patent - -**/ - -#include -#include -#include - -/** - Get 64-bit noise source - - @param[out] Rand Buffer pointer to store 64-bit noise source - - @retval TRUE Get randomness successfully. - @retval FALSE Failed to generate -**/ -BOOLEAN -EFIAPI -GetRandomNoise64 ( - OUT UINT64 *Rand - ) -{ - UINT32 Index; - UINT32 *RandPtr; - - if (NULL =3D=3D Rand) { - return FALSE; - } - - RandPtr =3D (UINT32 *)Rand; - - for (Index =3D 0; Index < 2; Index ++) { - *RandPtr =3D (UINT32) ((AsmReadTsc ()) & 0xFF); - RandPtr++; - MicroSecondDelay (10); - } - - return TRUE; -} diff --git a/CryptoPkg/CryptoPkg.dsc b/CryptoPkg/CryptoPkg.dsc index 1af78468a19c..0490eeb7e22f 100644 --- a/CryptoPkg/CryptoPkg.dsc +++ b/CryptoPkg/CryptoPkg.dsc @@ -60,6 +60,7 @@ BaseCryptLib|CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf TlsLib|CryptoPkg/Library/TlsLibNull/TlsLibNull.inf HashApiLib|CryptoPkg/Library/BaseHashApiLib/BaseHashApiLib.inf + RngLib|MdePkg/Library/BaseRngLibNull/BaseRngLibNull.inf =20 [LibraryClasses.ARM, LibraryClasses.AARCH64] # diff --git a/CryptoPkg/Library/OpensslLib/OpensslLib.inf b/CryptoPkg/Librar= y/OpensslLib/OpensslLib.inf index dbbe5386a10c..4baad565564c 100644 --- a/CryptoPkg/Library/OpensslLib/OpensslLib.inf +++ b/CryptoPkg/Library/OpensslLib/OpensslLib.inf @@ -571,22 +571,9 @@ $(OPENSSL_PATH)/ssl/statem/statem_local.h # Autogenerated files list ends here buildinf.h - rand_pool_noise.h ossl_store.c rand_pool.c =20 -[Sources.Ia32] - rand_pool_noise_tsc.c - -[Sources.X64] - rand_pool_noise_tsc.c - -[Sources.ARM] - rand_pool_noise.c - -[Sources.AARCH64] - rand_pool_noise.c - [Packages] MdePkg/MdePkg.dec CryptoPkg/CryptoPkg.dec @@ -594,7 +581,7 @@ [LibraryClasses] BaseLib DebugLib - TimerLib + RngLib PrintLib =20 [LibraryClasses.ARM] diff --git a/CryptoPkg/Library/OpensslLib/OpensslLibCrypto.inf b/CryptoPkg/= Library/OpensslLib/OpensslLibCrypto.inf index 616ccd9f62d1..3557711bd85a 100644 --- a/CryptoPkg/Library/OpensslLib/OpensslLibCrypto.inf +++ b/CryptoPkg/Library/OpensslLib/OpensslLibCrypto.inf @@ -520,22 +520,9 @@ $(OPENSSL_PATH)/crypto/x509v3/v3_admis.h # Autogenerated files list ends here buildinf.h - rand_pool_noise.h ossl_store.c rand_pool.c =20 -[Sources.Ia32] - rand_pool_noise_tsc.c - -[Sources.X64] - rand_pool_noise_tsc.c - -[Sources.ARM] - rand_pool_noise.c - -[Sources.AARCH64] - rand_pool_noise.c - [Packages] MdePkg/MdePkg.dec CryptoPkg/CryptoPkg.dec @@ -543,7 +530,7 @@ [LibraryClasses] BaseLib DebugLib - TimerLib + RngLib PrintLib =20 [LibraryClasses.ARM] diff --git a/CryptoPkg/Library/OpensslLib/rand_pool_noise.h b/CryptoPkg/Lib= rary/OpensslLib/rand_pool_noise.h deleted file mode 100644 index 75acc686a9f1..000000000000 --- a/CryptoPkg/Library/OpensslLib/rand_pool_noise.h +++ /dev/null @@ -1,29 +0,0 @@ -/** @file - Provide rand noise source. - -Copyright (c) 2019, Intel Corporation. All rights reserved.
-SPDX-License-Identifier: BSD-2-Clause-Patent - -**/ - -#ifndef __RAND_POOL_NOISE_H__ -#define __RAND_POOL_NOISE_H__ - -#include - -/** - Get 64-bit noise source. - - @param[out] Rand Buffer pointer to store 64-bit noise source - - @retval TRUE Get randomness successfully. - @retval FALSE Failed to generate -**/ -BOOLEAN -EFIAPI -GetRandomNoise64 ( - OUT UINT64 *Rand - ); - - -#endif // __RAND_POOL_NOISE_H__ --=20 2.27.0.windows.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#64130): https://edk2.groups.io/g/devel/message/64130 Mute This Topic: https://groups.io/mt/76157323/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-