From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f49.google.com (mail-pj1-f49.google.com [209.85.216.49]) by mx.groups.io with SMTP id smtpd.web12.1560.1596129686312741170 for ; Thu, 30 Jul 2020 10:21:26 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@gmail.com header.s=20161025 header.b=W7F34Rno; spf=pass (domain: gmail.com, ip: 209.85.216.49, mailfrom: matthewfcarlson@gmail.com) Received: by mail-pj1-f49.google.com with SMTP id lx9so5152097pjb.2 for ; Thu, 30 Jul 2020 10:21:26 -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=WlfQWVS9WjX9vDX4P+lMimFxrLxJSHFxRSkIWcNwJas=; b=W7F34RnoP+AgjsQS/8PVfsEn0/7OOTVYzKnaAS5SYmMaygf/1o4HD/zgAKrPTe5Dbq DZtkXy0V78Wv15FlVUEDjgRROO9VB5jBTL3mshtfpjsoN5oRoZZaUe3m7QW25f3BrtTp IZC+1SckyaPbjUM+i3Xri7pi/q8qK+DwNpGMBRsdN3L6SZ3LZjh0pSqu8BLyUY+tFDCC WODOYeYOJXeOte+Drx34qvTPVZksUa1RcMKCNCBqyOKXoCXiioUD5Zz291w4JWF5ewzn Voasb2mTysUvig7c9bhFM+1xL7C3dtkjTr4lH60/97DR0uMoxTWrWqXz99WFZ6GHdp3r cP/Q== 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=WlfQWVS9WjX9vDX4P+lMimFxrLxJSHFxRSkIWcNwJas=; b=XyASF4fiD8Db/viuRtkksBZoL2oJ6po38jX1yAyHtCeT7AnL8PDQfv9dnmoe16VS2X OTWCrc3rnWfWEfuqNNCRr4t3kBRtmkoY0UZyQpmbikNN5Ns2yHDuVty39xjdLXC4H4cw kMdHCSwEB4Z8bJi6ufunr4DeLYUchrGwWid3WovzLz+L5GDqcLuctLqUSIaPX4+kvv93 rrxeu9jAjjjTL9ZFAYhGO6YZHC1xja0UCHCUgBIB44X0Mula8lMQ6j1vlIBYlo85Vtf0 o4h2bbVJsTZLKPA3ArRiVOaWPVqijMxgJshcA3QNFvqH1Wr9SEzVt4T+8EY7MGUNP79S xXcw== X-Gm-Message-State: AOAM5307x/Qw+4LsYnKz1ojbbF5NWURV43wAxHoqtvjerl5zBGQmSmnn 3JlMEbj7iztJFKUIG9KeXqG88zl0JWc= X-Google-Smtp-Source: ABdhPJzGI7j0w9lIAB9jaR3do4MaRTK5Ozikl0nef9ybQEVkZxDwJBSCvC2rmD+64k11gcmfkWT49w== X-Received: by 2002:a17:902:bd83:: with SMTP id q3mr173460pls.295.1596129685669; Thu, 30 Jul 2020 10:21:25 -0700 (PDT) Return-Path: Received: from tvis-name-05.localdomain ([50.34.40.129]) by smtp.gmail.com with ESMTPSA id mp3sm21562914pjb.0.2020.07.30.10.21.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Jul 2020 10:21:25 -0700 (PDT) From: "Matthew Carlson" To: devel@edk2.groups.io Cc: Michael D Kinney , Liming Gao , Zhiguang Liu , Matthew Carlson Subject: [Patch v2 2/2] MdePkg: TimerRngLib: Added RngLib that uses TimerLib Date: Thu, 30 Jul 2020 10:21:17 -0700 Message-Id: <20200730172117.1558-3-matthewfcarlson@gmail.com> X-Mailer: git-send-email 2.27.0.windows.1 In-Reply-To: <20200730172117.1558-1-matthewfcarlson@gmail.com> References: <20200730172117.1558-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. Cc: Michael D Kinney Cc: Liming Gao Cc: Zhiguang Liu Signed-off-by: Matthew Carlson --- MdePkg/Library/BaseRngLibTimer/RngLibTimer.c | 153 +++++++++++++++++= +++ MdePkg/Library/BaseRngLibTimer/BaseRngLibTimer.inf | 37 +++++ MdePkg/Library/BaseRngLibTimer/BaseRngLibTimer.uni | 17 +++ MdePkg/MdePkg.dsc | 1 + 4 files changed, 208 insertions(+) diff --git a/MdePkg/Library/BaseRngLibTimer/RngLibTimer.c b/MdePkg/Library/= BaseRngLibTimer/RngLibTimer.c new file mode 100644 index 000000000000..1b0f7f04c01d --- /dev/null +++ b/MdePkg/Library/BaseRngLibTimer/RngLibTimer.c @@ -0,0 +1,153 @@ +/** @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 +/**=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 +=0D + ASSERT (Rand !=3D NULL);=0D +=0D + if (NULL =3D=3D Rand) {=0D + return FALSE;=0D + }=0D +=0D + RandPtr =3D (UINT8 *) Rand;=0D + // Get 2 bytes of random ish data=0D + for (Index =3D 0; Index < 2; Index ++) {=0D + *RandPtr =3D (UINT8) (GetPerformanceCounter () & 0xFF);=0D + MicroSecondDelay (4);=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 +=0D + ASSERT (Rand !=3D NULL);=0D +=0D + if (NULL =3D=3D Rand) {=0D + return FALSE;=0D + }=0D +=0D + RandPtr =3D (UINT8 *) Rand;=0D + // Get 4 bytes of random ish data=0D + for (Index =3D 0; Index < 4; Index ++) {=0D + *RandPtr =3D (UINT8) (GetPerformanceCounter () & 0xFF);=0D + MicroSecondDelay (2);=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 +=0D + ASSERT (Rand !=3D NULL);=0D +=0D + if (NULL =3D=3D Rand) {=0D + return FALSE;=0D + }=0D +=0D + RandPtr =3D (UINT8 *) Rand;=0D + // Get 8 bytes of random ish data=0D + for (Index =3D 0; Index < 8; Index ++) {=0D + *RandPtr =3D (UINT8) (GetPerformanceCounter () & 0xFF);=0D + MicroSecondDelay (1);=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 +=0D + //=0D + // Read first 64 bits=0D + //=0D + if (!GetRandomNumber64 (Rand)) {=0D + return FALSE;=0D + }=0D +=0D + //=0D + // Read second 64 bits=0D + //=0D + return GetRandomNumber64 (++Rand);=0D +}=0D diff --git a/MdePkg/Library/BaseRngLibTimer/BaseRngLibTimer.inf b/MdePkg/Li= brary/BaseRngLibTimer/BaseRngLibTimer.inf new file mode 100644 index 000000000000..c5561fe04c53 --- /dev/null +++ b/MdePkg/Library/BaseRngLibTimer/BaseRngLibTimer.inf @@ -0,0 +1,37 @@ +## @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.=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 0x00010005=0D + BASE_NAME =3D BaseRngLibTimer=0D + MODULE_UNI_FILE =3D BaseRngLibTimer.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 + CONSTRUCTOR =3D BaseRngLibConstructor=0D +=0D +#=0D +# VALID_ARCHITECTURES =3D IA32 X64=0D +#=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/BaseRngLibTimer/BaseRngLibTimer.uni b/MdePkg/Li= brary/BaseRngLibTimer/BaseRngLibTimer.uni new file mode 100644 index 000000000000..e2939f441f4a --- /dev/null +++ b/MdePkg/Library/BaseRngLibTimer/BaseRngLibTimer.uni @@ -0,0 +1,17 @@ +// /** @file=0D +// Instance of RNG (Random Number Generator) Library.=0D +//=0D +// BaseRng Library that uses CPU RdRand instruction access to provide=0D +// high-quality random numbers.=0D +//=0D +// Copyright (c) Microsoft Corporation=0D +//=0D +// SPDX-License-Identifier: BSD-2-Clause-Patent=0D +//=0D +// **/=0D +=0D +=0D +#string STR_MODULE_ABSTRACT #language en-US "Instance of RNG L= ibrary"=0D +=0D +#string STR_MODULE_DESCRIPTION #language en-US "BaseRng Library t= hat uses the TimerLib to provide low-quality random numbers"=0D +=0D diff --git a/MdePkg/MdePkg.dsc b/MdePkg/MdePkg.dsc index 472fa3777412..e1151cf2e0b9 100644 --- a/MdePkg/MdePkg.dsc +++ b/MdePkg/MdePkg.dsc @@ -62,6 +62,7 @@ MdePkg/Library/BasePostCodeLibPort80/BasePostCodeLibPort80.inf=0D MdePkg/Library/BasePrintLib/BasePrintLib.inf=0D MdePkg/Library/BaseReportStatusCodeLibNull/BaseReportStatusCodeLibNull.i= nf=0D + MdePkg/Library/BaseRngLibTimer/BaseRngLibTimer.inf=0D MdePkg/Library/BaseSerialPortLibNull/BaseSerialPortLibNull.inf=0D MdePkg/Library/BaseSynchronizationLib/BaseSynchronizationLib.inf=0D MdePkg/Library/BaseTimerLibNullTemplate/BaseTimerLibNullTemplate.inf=0D --=20 2.27.0.windows.1