From nobody Mon Feb 9 07:23:40 2026 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+63594+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+63594+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1596227238; cv=none; d=zohomail.com; s=zohoarc; b=Z18Zajvu0XmcakRTU6XEBZMyu3emiOA+Lbyanq+IjsF5lqfEwyhWZEEsDs65KlMb96A53/ZukcZW/aPVnz8ALcdhDmcvIVGL/iew2Y9UknVcplrH7Qpu1gI+7h3bpKq9MfM7vjmr5oD7yVIPZ31ZYI0P0XIoRuq6qHkmIP8JtPA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1596227238; 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=AO6jLStnDsBPsQ+mZ8+3clCBubrjwxTGP7FmKdgTsKE=; b=Jj2zYNPHPJIlb3OVCP1wd5FgMlY6nqA3Fam5kh7ZA2Pxo4+fsVqTwDxX/+USTokhXTB4lnlIXy1gZKHSehNDOCt/5xiDsce1JI4rFio6Wfyo3nLSAJXjbPui8IdIE7Hrz9/jg5/n4fAkxlTOs4dWLvvf7uAiatfeENHL+DlF5e8= 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+63594+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 1596227238447881.5108650822378; Fri, 31 Jul 2020 13:27:18 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id wCXKYY1788612xSv6U9SG5iR; Fri, 31 Jul 2020 13:27:18 -0700 X-Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) by mx.groups.io with SMTP id smtpd.web12.3430.1596227236925581938 for ; Fri, 31 Jul 2020 13:27:17 -0700 X-Received: by mail-pl1-f172.google.com with SMTP id b9so17946607plx.6 for ; Fri, 31 Jul 2020 13:27:16 -0700 (PDT) X-Gm-Message-State: quetWxfCeQBNsPkg2s656owox1787277AA= X-Google-Smtp-Source: ABdhPJym8YRgR+P4BsMtPZca88X0yj1TMGI6LVOC2uOtZpkxJDaZFJDP5KGprq+ATqFi/9XRGh4vpA== X-Received: by 2002:a17:902:302:: with SMTP id 2mr5188673pld.169.1596227236187; Fri, 31 Jul 2020 13:27:16 -0700 (PDT) X-Received: from tvis-name-05.localdomain ([50.34.40.129]) by smtp.gmail.com with ESMTPSA id m26sm11098594pff.84.2020.07.31.13.27.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 31 Jul 2020 13:27:15 -0700 (PDT) From: "Matthew Carlson" To: devel@edk2.groups.io Cc: Michael D Kinney , Liming Gao , Zhiguang Liu , Matthew Carlson Subject: [edk2-devel] [PATCH v3 2/3] MdePkg: TimerRngLib: Added RngLib that uses TimerLib Date: Fri, 31 Jul 2020 13:27:11 -0700 Message-Id: <20200731202712.1759-3-matthewfcarlson@gmail.com> In-Reply-To: <20200731202712.1759-1-matthewfcarlson@gmail.com> References: <20200731202712.1759-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=1596227238; bh=+prkjDygmuTiY4LUXfvLvurXZ9qZsh9L0xIo2Rriulg=; h=Cc:Date:From:Reply-To:Subject:To; b=eA0XLROILabF4q5JeHuH4EWw8NvT74DYcg6qQLLrXBYJQkwcklFXb4aKqshrIxQdmys xtk6DWgXjph+F0DdLaND4pAaG4VqcYvA03Q6jQ9taVwziksm5CmJcxv3xFXX4Hm91qt4Q 7YKEB0pHXaGmeibqjha0XmrEApk2ZnKolBE= 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. Cc: Michael D Kinney Cc: Liming Gao Cc: Zhiguang Liu Signed-off-by: Matthew Carlson --- MdePkg/Library/BaseRngLibTimerLib/RngLibTimer.c | 154 +++++++++++= +++++++++ MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.inf | 38 +++++ MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.uni | 17 +++ MdePkg/MdePkg.dsc | 3 +- 4 files changed, 211 insertions(+), 1 deletion(-) diff --git a/MdePkg/Library/BaseRngLibTimerLib/RngLibTimer.c b/MdePkg/Libra= ry/BaseRngLibTimerLib/RngLibTimer.c new file mode 100644 index 000000000000..8244a740555c --- /dev/null +++ b/MdePkg/Library/BaseRngLibTimerLib/RngLibTimer.c @@ -0,0 +1,154 @@ +/** @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 + + +/** + 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 10ms + for (Index =3D 0; Index < 2; Index ++) { + *RandPtr =3D (UINT8) (GetPerformanceCounter () & 0xFF); + MicroSecondDelay (5); // delay to give chance for performance counter = to catch 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); + MicroSecondDelay (5); // delay to give chance for performance counter = to catch 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); + MicroSecondDelay (5); // delay to give chance for performance counter = to catch 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..0d20fd13c1da --- /dev/null +++ b/MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.inf @@ -0,0 +1,38 @@ +## @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 have a weak = random +# algorithm. +# +# 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 (#63594): https://edk2.groups.io/g/devel/message/63594 Mute This Topic: https://groups.io/mt/75915447/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-