From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pg1-f196.google.com (mail-pg1-f196.google.com [209.85.215.196]) by mx.groups.io with SMTP id smtpd.web11.3384.1595901197570374873 for ; Mon, 27 Jul 2020 18:53:17 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@gmail.com header.s=20161025 header.b=s3Btix2W; spf=pass (domain: gmail.com, ip: 209.85.215.196, mailfrom: matthewfcarlson@gmail.com) Received: by mail-pg1-f196.google.com with SMTP id e8so10989262pgc.5 for ; Mon, 27 Jul 2020 18:53:17 -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=tDODF8OydlPrxtKSg3Vpa0f9iXKBCcOV9XYW7511b2c=; b=s3Btix2W2rcBk7/SR81R9xAOpy+7qpt3OmoT78vXlubY2LH+q7hLsQLCtFb3x6BtC8 VfPlwZiZa+R4+qj1I1RaYLxOA0XBCVTydkXpAHRPX1xAeU5zM4QFsd7/+Ont0z+x2uyG tfKRD+lihQi4+6ir7FjPEGUGyOQBvPUKqz/rA1fZSFlbrjhjjCBgd9LnYzgqXJ23h81F Gur8+pIu14a/oL21YadjvoX6POU8rxapjndfcSK9LbSHNf1wtTb5sfYVl7zlUgQQVTaE CcurSGBH+7k8XRIJq6hoUCYhbdsZTx1IXJEGP6shkAG7LSSJ3TxLQuU6RT+IEJ8Mbkrk IkqQ== 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=tDODF8OydlPrxtKSg3Vpa0f9iXKBCcOV9XYW7511b2c=; b=deD9xAFH5UktaLGT6TSjNHNlV3m+d5jT1W5FrVWYAe4+MeAwhpY40yqw/87P7OxwUO IYO3VpKh+MJXJ6NaNXeX0DwFytE+3e/ENVp2swADEUGjPhxoXPF8Qrr1MZ7tdmcsTjvS 5hh03KNwUJL+77y3xPss0MsEQXZOgzuI5hRu/iNriyG+SshUrA2zT36xhw7zS6JfevUW FuNo24lHauZjqI4sdbOeT1jyVDCRJfs25V3RAetxBKwGPzlhMJVP+82x5AvsHwRk5wcG 4lpuKwvgACMeISwBgMK7K9kdeliIImti1TjsqmmUEfl0mDBzpFBVP4OTGY90TXDzIzgk 8HTg== X-Gm-Message-State: AOAM531yLyoeURMTeZ1CObvga+BZjXxJGovqleEN22pG2B7aYlTP8bKF 9lp55hxtYgCyALYCsdQ+NR2gX30gitWiug== X-Google-Smtp-Source: ABdhPJxa2DeRk8naFSwnC7ey9khC5H26GSR/M4xrOuKaUeTqoebhTwWaDV7dmMOAsaFwIE7soLRL3w== X-Received: by 2002:a63:3d44:: with SMTP id k65mr580071pga.17.1595901196852; Mon, 27 Jul 2020 18:53:16 -0700 (PDT) Return-Path: Received: from tvis-name-05.localdomain ([50.34.40.129]) by smtp.gmail.com with ESMTPSA id a184sm9579151pfa.83.2020.07.27.18.53.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Jul 2020 18:53:16 -0700 (PDT) From: "Matthew Carlson" To: devel@edk2.groups.io Cc: Michael D Kinney , Liming Gao , Zhiguang Liu , Matthew Carlson Subject: [PATCH v1 2/2] MdePkg: TimerRngLib: Added RngLib that uses TimerLib Date: Mon, 27 Jul 2020 18:53:12 -0700 Message-Id: <20200728015312.1023-3-matthewfcarlson@gmail.com> X-Mailer: git-send-email 2.27.0.windows.1 In-Reply-To: <20200728015312.1023-1-matthewfcarlson@gmail.com> References: <20200728015312.1023-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/TimerRngLib/TimerRng.c | 153 ++++++++++++++++++++ MdePkg/Library/TimerRngLib/TimerRngLib.inf | 37 +++++ MdePkg/MdePkg.dsc | 2 + 3 files changed, 192 insertions(+) diff --git a/MdePkg/Library/TimerRngLib/TimerRng.c b/MdePkg/Library/TimerRn= gLib/TimerRng.c new file mode 100644 index 000000000000..1b0f7f04c01d --- /dev/null +++ b/MdePkg/Library/TimerRngLib/TimerRng.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/TimerRngLib/TimerRngLib.inf b/MdePkg/Library/Ti= merRngLib/TimerRngLib.inf new file mode 100644 index 000000000000..a80a89b77e72 --- /dev/null +++ b/MdePkg/Library/TimerRngLib/TimerRngLib.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 BaseRngLib=0D + MODULE_UNI_FILE =3D BaseRngLib.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 + TimerRng.c=0D +=0D +[Packages]=0D + MdePkg/MdePkg.dec=0D +=0D +[LibraryClasses]=0D + BaseLib=0D + DebugLib=0D + TimerLib=0D diff --git a/MdePkg/MdePkg.dsc b/MdePkg/MdePkg.dsc index 472fa3777412..c46a95e52e15 100644 --- a/MdePkg/MdePkg.dsc +++ b/MdePkg/MdePkg.dsc @@ -120,6 +120,8 @@ MdePkg/Library/SmmLibNull/SmmLibNull.inf=0D MdePkg/Library/BaseExtractGuidedSectionLib/BaseExtractGuidedSectionLib.i= nf=0D =0D + MdePkg/Library/TimerRngLib/TimerRngLib.inf=0D +=0D MdePkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDriverEntryPoint= .inf=0D MdePkg/Library/StandaloneMmServicesTableLib/StandaloneMmServicesTableLib= .inf=0D =0D --=20 2.27.0.windows.1