From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f169.google.com (mail-pf1-f169.google.com [209.85.210.169]) by mx.groups.io with SMTP id smtpd.web10.1596.1598981629736472385 for ; Tue, 01 Sep 2020 10:33:49 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@gmail.com header.s=20161025 header.b=RluK3SR7; spf=pass (domain: gmail.com, ip: 209.85.210.169, mailfrom: matthewfcarlson@gmail.com) Received: by mail-pf1-f169.google.com with SMTP id f18so1195347pfa.10 for ; Tue, 01 Sep 2020 10:33:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=PLsCBi8vd7cYpKmJ7rpGzARPPQVyAF9V6PVxI+OL07Y=; b=RluK3SR7yPRpnfuOAQ7SIJr4tZaQGojSlOxWZAIAAgwrb1LvWyiwvwKHg4msOMT/mn rJV1Nko++N/8Gy02Qd9cYNoF2jjjPEhR+Ii2Hn4KDjaHD+Gd7SHneZVFHpY8o6lC8Cm3 HUop5dcQQbTFbtEbvoRlODyfRyVR2qhCmuoXia5mkVQFrI6slmzRvq9TrTA5Iq9E85dC G7YnNixFEZl9vZU8L0rILRDLI/ozMvFP6jfEgzRjbX8BgedXw5ZhMI2laNosx7DEEYdT lMbQAUokVpU1OUImWl4Ml15yBEN1YTNQ1sk2fpN6gh1VmUbmz2FqNz8J4S9mxI54p/Pt ghiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=PLsCBi8vd7cYpKmJ7rpGzARPPQVyAF9V6PVxI+OL07Y=; b=MvcNefJ4P3tXWryIPsIxwzu3Pd7HxvuuLnjkwi+D5SLMdTKOsiMQC2/vhZaLXv5WxH yBFL39aNUzoRc0dqFADXCJIpcrncX7zvIKQX9H+TACCsdxAwbxGM6KF1n7kNEAPc8Ry1 B85o/GFmtu8skJ6++Z9eauUlJ4d66fAQ1H2I3H+oRfPp6f9rJvOp/xpS3oqrePIrAxB/ 4V7zDfOMN5tFS3gSCotClyV7zl+6z8KGZz9jfIaVfLstlInCeqcPmz60LuuKk3gr/gEQ 8O5mp7NWtsdv+6XoluiqPquupIVoVWYjym/l+PMNZUpdDDTrMBjw0IMPeADEhsGHqRRd nLjg== X-Gm-Message-State: AOAM532w9ehqUsr8Y+e/1p0A9pis4tOGX03MP7QG+fSHu4hToxOyn8IW 08p5SXxUn05oNBe0MRODkY8gTPKFA/4Zvw== X-Google-Smtp-Source: ABdhPJz5yVxMHE3SiKTbNBJKSOqrh1WNVyUKWsw4MVZOUIp4SIFZ3JGHfBaQ/nVhG6atfFqZxVYPzw== X-Received: by 2002:a63:344f:: with SMTP id b76mr2302833pga.388.1598981629035; Tue, 01 Sep 2020 10:33:49 -0700 (PDT) Return-Path: Received: from tvis-name-05.localdomain ([50.34.58.90]) by smtp.gmail.com with ESMTPSA id 205sm2729842pfz.14.2020.09.01.10.33.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Sep 2020 10:33:48 -0700 (PDT) From: "Matthew Carlson" To: devel@edk2.groups.io Cc: Ard Biesheuvel , Michael D Kinney , Liming Gao , Zhiguang Liu , Matthew Carlson Subject: [PATCH v10 1/5] MdePkg: TimerRngLib: Added RngLib that uses TimerLib Date: Tue, 1 Sep 2020 10:33:42 -0700 Message-Id: <20200901173346.676-2-matthewfcarlson@gmail.com> X-Mailer: git-send-email 2.28.0.windows.1 In-Reply-To: <20200901173346.676-1-matthewfcarlson@gmail.com> References: <20200901173346.676-1-matthewfcarlson@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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 | 189 +++++++++++= +++++++++ MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.inf | 36 ++++ MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.uni | 15 ++ MdePkg/MdePkg.dsc | 3 +- 4 files changed, 242 insertions(+), 1 deletion(-) diff --git a/MdePkg/Library/BaseRngLibTimerLib/RngLibTimer.c b/MdePkg/Libra= ry/BaseRngLibTimerLib/RngLibTimer.c new file mode 100644 index 000000000000..54d29d96f3d3 --- /dev/null +++ b/MdePkg/Library/BaseRngLibTimerLib/RngLibTimer.c @@ -0,0 +1,189 @@ +/** @file=0D + BaseRng Library that uses the TimerLib to provide reasonably random numb= ers.=0D + Do not use this on a production system.=0D +=0D + Copyright (c) Microsoft Corporation.=0D + SPDX-License-Identifier: BSD-2-Clause-Patent=0D +**/=0D +=0D +#include =0D +#include =0D +#include =0D +#include =0D +=0D +#define DEFAULT_DELAY_TIME_IN_MICROSECONDS 10=0D +=0D +/**=0D + Using the TimerLib GetPerformanceCounterProperties() we delay=0D + for enough time for the PerformanceCounter to increment.=0D +=0D + If the return value from GetPerformanceCounterProperties (TimerLib)=0D + is zero, this function will return 10 and attempt to assert.=0D + **/=0D +STATIC=0D +UINT32=0D +CalculateMinimumDecentDelayInMicroseconds (=0D + VOID=0D + )=0D +{=0D + UINT64 CounterHz;=0D +=0D + // Get the counter properties=0D + CounterHz =3D GetPerformanceCounterProperties (NULL, NULL);=0D + // Make sure we won't divide by zero=0D + if (CounterHz =3D=3D 0) {=0D + ASSERT(CounterHz !=3D 0); // Assert so the developer knows something i= s wrong=0D + return DEFAULT_DELAY_TIME_IN_MICROSECONDS;=0D + }=0D + // Calculate the minimum delay based on 1.5 microseconds divided by the = hertz.=0D + // We calculate the length of a cycle (1/CounterHz) and multiply it by 1= .5 microseconds=0D + // This ensures that the performance counter has increased by at least o= ne=0D + return (UINT32)(MAX (DivU64x64Remainder (1500000,CounterHz, NULL), 1));= =0D +}=0D +=0D +=0D +/**=0D + Generates a 16-bit random number.=0D +=0D + if Rand is NULL, then ASSERT().=0D +=0D + @param[out] Rand Buffer pointer to store the 16-bit random value.=0D +=0D + @retval TRUE Random number generated successfully.=0D + @retval FALSE Failed to generate the random number.=0D +=0D +**/=0D +BOOLEAN=0D +EFIAPI=0D +GetRandomNumber16 (=0D + OUT UINT16 *Rand=0D + )=0D +{=0D + UINT32 Index;=0D + UINT8 *RandPtr;=0D + UINT32 DelayInMicroSeconds;=0D +=0D + ASSERT (Rand !=3D NULL);=0D +=0D + if (Rand =3D=3D NULL) {=0D + return FALSE;=0D + }=0D + DelayInMicroSeconds =3D CalculateMinimumDecentDelayInMicroseconds ();=0D + RandPtr =3D (UINT8*)Rand;=0D + // Get 2 bytes of random ish data=0D + for (Index =3D 0; Index < sizeof(UINT16); Index ++) {=0D + *RandPtr =3D (UINT8)(GetPerformanceCounter () & 0xFF);=0D + // Delay to give the performance counter a chance to change=0D + MicroSecondDelay (DelayInMicroSeconds);=0D + RandPtr++;=0D + }=0D + return TRUE;=0D +}=0D +=0D +/**=0D + Generates a 32-bit random number.=0D +=0D + if Rand is NULL, then ASSERT().=0D +=0D + @param[out] Rand Buffer pointer to store the 32-bit random value.=0D +=0D + @retval TRUE Random number generated successfully.=0D + @retval FALSE Failed to generate the random number.=0D +=0D +**/=0D +BOOLEAN=0D +EFIAPI=0D +GetRandomNumber32 (=0D + OUT UINT32 *Rand=0D + )=0D +{=0D + UINT32 Index;=0D + UINT8 *RandPtr;=0D + UINT32 DelayInMicroSeconds;=0D +=0D + ASSERT (Rand !=3D NULL);=0D +=0D + if (NULL =3D=3D Rand) {=0D + return FALSE;=0D + }=0D +=0D + RandPtr =3D (UINT8 *) Rand;=0D + DelayInMicroSeconds =3D CalculateMinimumDecentDelayInMicroseconds ();=0D + // Get 4 bytes of random ish data=0D + for (Index =3D 0; Index < sizeof(UINT32); Index ++) {=0D + *RandPtr =3D (UINT8)(GetPerformanceCounter () & 0xFF);=0D + // Delay to give the performance counter a chance to change=0D + MicroSecondDelay (DelayInMicroSeconds);=0D + RandPtr++;=0D + }=0D + return TRUE;=0D +}=0D +=0D +/**=0D + Generates a 64-bit random number.=0D +=0D + if Rand is NULL, then ASSERT().=0D +=0D + @param[out] Rand Buffer pointer to store the 64-bit random value.=0D +=0D + @retval TRUE Random number generated successfully.=0D + @retval FALSE Failed to generate the random number.=0D +=0D +**/=0D +BOOLEAN=0D +EFIAPI=0D +GetRandomNumber64 (=0D + OUT UINT64 *Rand=0D + )=0D +{=0D + UINT32 Index;=0D + UINT8 *RandPtr;=0D + UINT32 DelayInMicroSeconds;=0D +=0D + ASSERT (Rand !=3D NULL);=0D +=0D + if (NULL =3D=3D Rand) {=0D + return FALSE;=0D + }=0D +=0D + RandPtr =3D (UINT8 *)Rand;=0D + DelayInMicroSeconds =3D CalculateMinimumDecentDelayInMicroseconds ();=0D + // Get 8 bytes of random ish data=0D + for (Index =3D 0; Index < sizeof(UINT64); Index ++) {=0D + *RandPtr =3D (UINT8)(GetPerformanceCounter () & 0xFF);=0D + // Delay to give the performance counter a chance to change=0D + MicroSecondDelay (DelayInMicroSeconds);=0D + RandPtr++;=0D + }=0D +=0D + return TRUE;=0D +}=0D +=0D +/**=0D + Generates a 128-bit random number.=0D +=0D + if Rand is NULL, then ASSERT().=0D +=0D + @param[out] Rand Buffer pointer to store the 128-bit random value.=0D +=0D + @retval TRUE Random number generated successfully.=0D + @retval FALSE Failed to generate the random number.=0D +=0D +**/=0D +BOOLEAN=0D +EFIAPI=0D +GetRandomNumber128 (=0D + OUT UINT64 *Rand=0D + )=0D +{=0D + ASSERT (Rand !=3D NULL);=0D + // This should take around 80ms=0D +=0D + // Read first 64 bits=0D + if (!GetRandomNumber64 (Rand)) {=0D + return FALSE;=0D + }=0D +=0D + // Read second 64 bits=0D + return GetRandomNumber64 (++Rand);=0D +}=0D diff --git a/MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.inf b/Mde= Pkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.inf new file mode 100644 index 000000000000..f857290e823b --- /dev/null +++ b/MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.inf @@ -0,0 +1,36 @@ +## @file=0D +# Instance of RNG (Random Number Generator) Library.=0D +#=0D +# BaseRng Library that uses the TimerLib to provide reasonably random num= bers.=0D +# Do NOT use this on a production system as this uses the system performa= nce=0D +# counter rather than a true source of random in addition to having a wea= k=0D +# random algorithm. This is provided primarily as a source of entropy for= =0D +# OpenSSL for platforms that do not have a good built in RngLib as this=0D +# emulates what was done before (though it isn't perfect).=0D +#=0D +# Copyright (c) Microsoft Corporation. All rights reserved.
=0D +#=0D +# SPDX-License-Identifier: BSD-2-Clause-Patent=0D +#=0D +#=0D +##=0D +=0D +[Defines]=0D + INF_VERSION =3D 1.27=0D + BASE_NAME =3D BaseRngLibTimerLib=0D + MODULE_UNI_FILE =3D BaseRngLibTimerLib.uni=0D + FILE_GUID =3D 74950C45-10FC-4AB5-B114-49C87C17409B= =0D + MODULE_TYPE =3D BASE=0D + VERSION_STRING =3D 1.0=0D + LIBRARY_CLASS =3D RngLib=0D +=0D +[Sources]=0D + RngLibTimer.c=0D +=0D +[Packages]=0D + MdePkg/MdePkg.dec=0D +=0D +[LibraryClasses]=0D + BaseLib=0D + DebugLib=0D + TimerLib=0D diff --git a/MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.uni b/Mde= Pkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.uni new file mode 100644 index 000000000000..fde24b9f0107 --- /dev/null +++ b/MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.uni @@ -0,0 +1,15 @@ +// @file=0D +// Instance of RNG (Random Number Generator) Library.=0D +//=0D +// RngLib that uses TimerLib's performance counter to provide random numbe= rs.=0D +//=0D +// Copyright (c) Microsoft Corporation.=0D +//=0D +// SPDX-License-Identifier: BSD-2-Clause-Patent=0D +//=0D +=0D +=0D +#string STR_MODULE_ABSTRACT #language en-US "Instance of RNG Library"= =0D +=0D +#string STR_MODULE_DESCRIPTION #language en-US "BaseRng Library that uses= the TimerLib to provide low-entropy random numbers"=0D +=0D 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=0D MdePkg/Library/BasePrintLib/BasePrintLib.inf=0D MdePkg/Library/BaseReportStatusCodeLibNull/BaseReportStatusCodeLibNull.i= nf=0D + MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.inf=0D + MdePkg/Library/BaseRngLibNull/BaseRngLibNull.inf=0D MdePkg/Library/BaseSerialPortLibNull/BaseSerialPortLibNull.inf=0D MdePkg/Library/BaseSynchronizationLib/BaseSynchronizationLib.inf=0D MdePkg/Library/BaseTimerLibNullTemplate/BaseTimerLibNullTemplate.inf=0D @@ -69,7 +71,6 @@ MdePkg/Library/BaseUefiDecompressLib/BaseUefiTianoCustomDecompressLib.in= f=0D MdePkg/Library/BaseSmbusLibNull/BaseSmbusLibNull.inf=0D MdePkg/Library/BaseSafeIntLib/BaseSafeIntLib.inf=0D - MdePkg/Library/BaseRngLibNull/BaseRngLibNull.inf=0D =0D MdePkg/Library/DxeCoreEntryPoint/DxeCoreEntryPoint.inf=0D MdePkg/Library/DxeCoreHobLib/DxeCoreHobLib.inf=0D --=20 2.28.0.windows.1