From mboxrd@z Thu Jan 1 00:00:00 1970 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 Authentication-Results: mx.groups.io; dkim=pass header.i=@gmail.com header.s=20161025 header.b=K0spmPqA; spf=pass (domain: gmail.com, ip: 209.85.214.172, mailfrom: matthewfcarlson@gmail.com) Received: by mail-pl1-f172.google.com with SMTP id b9so17946607plx.6 for ; Fri, 31 Jul 2020 13:27:16 -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=yaLBOS5ZFcwIL6oYkJYrl20LNpMM7v7IpnDdFjVYXP0=; b=K0spmPqAddh5tDpXjKpAFxeOMvH6PPn24FdXLRYNZCJKD98L+dLxrEeJYdqcJ4d1zx R9LY0F0WnHKwOfsWgfKvU+Tneyz5FytnUwsbPKaZzJlOzpHXM7majnvCUqGMndU6nMH/ A9SF0xYlHxGO20L0Hf3uf0KOte/iyxW+NAP1cPKTT36rYGPasC8NsVN4QrQpy8FcA4oW tZ6stbJO7EYzAeEKao6FTdhkeNYaMhNp5gWf2WSZzOYtP17y6+bec81UvFReY/GnmLxp nZzJZ3Ya456y5a7ZHmFlM6CjYgTofI//MKXg93095kLzD+bmtn0dNJt+esSnWnCkWYRD jLWA== 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=yaLBOS5ZFcwIL6oYkJYrl20LNpMM7v7IpnDdFjVYXP0=; b=HVaU55KDbxT19zsnYawshfvqVibHw8SSAtgMbgfJL9Uy+Or7zkK2YsA0XXJo26JXad 7MQDb6zaSfHDaPiMwwStwizBokOZOPnBDOjDK8oxD15Vq4TcCJqj/0RGUxPLITu7Fj7v kEB8tQZKBizURWvQ0fd8HtDYwqSsroaKDxkV+E9iJyNksHbo2DOaLIJ8TJ0nU3504fiH q1xLFACBpK+LSd7/qEMR+QzPbKuNMg+uF7Cbc/CnAxKPxc0EWsJmw35lXtfFZHLCzDdk d1pJmE/WyUe5SUClUMb0duq6Tq/fhg+i1we6kDiBgu7JmLiOd4+5dxpT9LoNtuAZPVgc Okpg== X-Gm-Message-State: AOAM531qfqg09iYR/hQkYAoo5ESAqisbnBS1amqhGDfTPguFWWmKbgIJ yAczPYjRUxjxFOjHAaNNPWJqpyPxYK8= 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) Return-Path: 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: [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> X-Mailer: git-send-email 2.27.0.windows.1 In-Reply-To: <20200731202712.1759-1-matthewfcarlson@gmail.com> References: <20200731202712.1759-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/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=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 +/**=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 + // This should take around 10ms=0D + for (Index =3D 0; Index < 2; Index ++) {=0D + *RandPtr =3D (UINT8) (GetPerformanceCounter () & 0xFF);=0D + MicroSecondDelay (5); // delay to give chance for performance counter = to catch up=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 + // This should take around 20ms=0D + for (Index =3D 0; Index < 4; Index ++) {=0D + *RandPtr =3D (UINT8) (GetPerformanceCounter () & 0xFF);=0D + MicroSecondDelay (5); // delay to give chance for performance counter = to catch up=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 + // This should take around 40ms=0D + for (Index =3D 0; Index < 8; Index ++) {=0D + *RandPtr =3D (UINT8) (GetPerformanceCounter () & 0xFF);=0D + MicroSecondDelay (5); // delay to give chance for performance counter = to catch up=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..0d20fd13c1da --- /dev/null +++ b/MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.inf @@ -0,0 +1,38 @@ +## @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 have a weak = random=0D +# algorithm.=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 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 + 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 + TimerLib=0D 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=0D +// Instance of RNG (Random Number Generator) Library.=0D +//=0D +// BaseRng Library that uses TimerLib's performance counter=0D +// to provide 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-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.27.0.windows.1