From nobody Thu Apr 25 23:04:57 2024 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+63475+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+63475+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1596129686; cv=none; d=zohomail.com; s=zohoarc; b=b6RBMnmyxNfkOEsRqJvSrek21g5wsX736ZsSxSlDaWNrhlKM2W6UaN1btDZDqrYor1PXbOIZ3bz2w0ZYQBzmCZmrwJq9tCAB/g3CQG2hv+sgRhd+BnbarhXBrjkZWd8xL8bNcW/Q6u5raPMWn2IuKKpwxw/e6utId6Lkhxz7Oi8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1596129686; 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=jPg+OpRavo6iymg8ob7uKDVjPSXgzwtfBZSXf2J/W8s=; b=eQ+xktg0Gc4KwjJz7+JOFE0S0LDKO51rT/Sh5eEy5MWlng0qJeAUQrdoS+k5ZNGbF9soMjFsAZiNSWQHdNAmtK7av7/NA+LRQc7w1rZT5ClryDmocbddlhlAEDwMPClz4ZV+O9ZLZaDgh14sMaCdAZ1ZpYS4NfaYAEfwJDawvHA= 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+63475+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 1596129686279591.5081819474145; Thu, 30 Jul 2020 10:21:26 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id L0siYY1788612xSVeuH4LUfU; Thu, 30 Jul 2020 10:21:25 -0700 X-Received: from mail-pj1-f47.google.com (mail-pj1-f47.google.com [209.85.216.47]) by mx.groups.io with SMTP id smtpd.web10.1551.1596129685188490899 for ; Thu, 30 Jul 2020 10:21:25 -0700 X-Received: by mail-pj1-f47.google.com with SMTP id k1so4774594pjt.5 for ; Thu, 30 Jul 2020 10:21:25 -0700 (PDT) X-Gm-Message-State: xd88MnbNDr9p9cJ2GM2TBEZ5x1787277AA= X-Google-Smtp-Source: ABdhPJzdTuIyXqYEXxrnMt8lwHSBcIfV90/PceLYJ8ejcYsqOUSMOeZnWKidm0s8FKPSOXY/4esF5Q== X-Received: by 2002:a62:7958:: with SMTP id u85mr25665pfc.248.1596129684427; Thu, 30 Jul 2020 10:21:24 -0700 (PDT) X-Received: from tvis-name-05.localdomain ([50.34.40.129]) by smtp.gmail.com with ESMTPSA id mp3sm21562914pjb.0.2020.07.30.10.21.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Jul 2020 10:21:23 -0700 (PDT) From: "Matthew Carlson" To: devel@edk2.groups.io Cc: Jiewen Yao , Jian J Wang , Xiaoyu Lu , Matthew Carlson Subject: [edk2-devel] [Patch v2 1/2] CryptoPkg: OpensslLib: Use RngLib to generate entropy in rand_pool Date: Thu, 30 Jul 2020 10:21:16 -0700 Message-Id: <20200730172117.1558-2-matthewfcarlson@gmail.com> In-Reply-To: <20200730172117.1558-1-matthewfcarlson@gmail.com> References: <20200730172117.1558-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=1596129685; bh=r4GXLbsIgL2zDE+baMghKf3RiMqQ6VYMUp0mFndU3Is=; h=Cc:Date:From:Reply-To:Subject:To; b=rJvigeQlikbrKsA4eyqpc8UadL6EnZb0u3MVLHL+jq7Dmw/qRBCX+pvh0fLGiV+4WNf G5XvIfhF5O8w0PYwYXWatX8oTrDuCHqjiiGT2vCvQ1bxoLcLFgm8CR5bvJGZLlcve9yHc Uvvowj0HY33JwiZtiDeLmmjL9ogPg/Q1vog= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Type: text/plain; charset="utf-8" From: Matthew Carlson Changes OpenSSL to no longer depend on TimerLib and instead use RngLib. This allows platforms to decide for themsevles what sort of entropy source they provide to OpenSSL and TlsLib. Cc: Jiewen Yao Cc: Jian J Wang Cc: Xiaoyu Lu Signed-off-by: Matthew Carlson --- CryptoPkg/Library/OpensslLib/rand_pool.c | 202 ++---------------= --- CryptoPkg/Library/OpensslLib/rand_pool_noise.c | 29 --- CryptoPkg/Library/OpensslLib/rand_pool_noise_tsc.c | 43 ----- CryptoPkg/CryptoPkg.dsc | 2 + CryptoPkg/Library/OpensslLib/OpensslLib.inf | 15 +- CryptoPkg/Library/OpensslLib/OpensslLibCrypto.inf | 15 +- CryptoPkg/Library/OpensslLib/rand_pool_noise.h | 29 --- 7 files changed, 22 insertions(+), 313 deletions(-) diff --git a/CryptoPkg/Library/OpensslLib/rand_pool.c b/CryptoPkg/Library/O= pensslLib/rand_pool.c index 9e0179b03490..b3ce71543bab 100644 --- a/CryptoPkg/Library/OpensslLib/rand_pool.c +++ b/CryptoPkg/Library/OpensslLib/rand_pool.c @@ -11,43 +11,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent #include =20 #include -#include - -#include "rand_pool_noise.h" - -/** - Get some randomness from low-order bits of GetPerformanceCounter results. - And combine them to the 64-bit value - - @param[out] Rand Buffer pointer to store the 64-bit random value. - - @retval TRUE Random number generated successfully. - @retval FALSE Failed to generate. -**/ -STATIC -BOOLEAN -EFIAPI -GetRandNoise64FromPerformanceCounter( - OUT UINT64 *Rand - ) -{ - UINT32 Index; - UINT32 *RandPtr; - - if (NULL =3D=3D Rand) { - return FALSE; - } - - RandPtr =3D (UINT32 *) Rand; - - for (Index =3D 0; Index < 2; Index ++) { - *RandPtr =3D (UINT32) (GetPerformanceCounter () & 0xFF); - MicroSecondDelay (10); - RandPtr++; - } - - return TRUE; -} +#include =20 /** Calls RandomNumber64 to fill @@ -56,8 +20,8 @@ GetRandNoise64FromPerformanceCounter( @param[in] Length Size of the buffer, in bytes, to fill with. @param[out] RandBuffer Pointer to the buffer to store the random res= ult. =20 - @retval EFI_SUCCESS Random bytes generation succeeded. - @retval EFI_NOT_READY Failed to request random bytes. + @retval True Random bytes generation succeeded. + @retval False Failed to request random bytes. =20 **/ STATIC @@ -73,17 +37,17 @@ RandGetBytes ( =20 Ret =3D FALSE; =20 + if (RandBuffer =3D=3D NULL) { + DEBUG((DEBUG_ERROR, "[OPENSSL_RAND_POOL] NULL RandBuffer. No random nu= mbers are generated and your system is not secure\n")); + ASSERT(FALSE); // Since we can't generate random numbers, we should as= sert. Otherwise we will just blow up later. + return Ret; + } + + while (Length > 0) { - // - // Get random noise from platform. - // If it failed, fallback to PerformanceCounter - // If you really care about security, you must override - // GetRandomNoise64FromPlatform. - // - Ret =3D GetRandomNoise64 (&TempRand); - if (Ret =3D=3D FALSE) { - Ret =3D GetRandNoise64FromPerformanceCounter (&TempRand); - } + // Use RngLib to get random number + Ret =3D GetRandomNumber64(&TempRand); + if (!Ret) { return Ret; } @@ -100,125 +64,6 @@ RandGetBytes ( return Ret; } =20 -/** - Creates a 128bit random value that is fully forward and backward predict= ion resistant, - suitable for seeding a NIST SP800-90 Compliant. - This function takes multiple random numbers from PerformanceCounter to e= nsure reseeding - and performs AES-CBC-MAC over the data to compute the seed value. - - @param[out] SeedBuffer Pointer to a 128bit buffer to store the rando= m seed. - - @retval TRUE Random seed generation succeeded. - @retval FALSE Failed to request random bytes. - -**/ -STATIC -BOOLEAN -EFIAPI -RandGetSeed128 ( - OUT UINT8 *SeedBuffer - ) -{ - BOOLEAN Ret; - UINT8 RandByte[16]; - UINT8 Key[16]; - UINT8 Ffv[16]; - UINT8 Xored[16]; - UINT32 Index; - UINT32 Index2; - AES_KEY AESKey; - - // - // Chose an arbitrary key and zero the feed_forward_value (FFV) - // - for (Index =3D 0; Index < 16; Index++) { - Key[Index] =3D (UINT8) Index; - Ffv[Index] =3D 0; - } - - AES_set_encrypt_key (Key, 16 * 8, &AESKey); - - // - // Perform CBC_MAC over 32 * 128 bit values, with 10us gaps between 128 = bit value - // The 10us gaps will ensure multiple reseeds within the system time wit= h a large - // design margin. - // - for (Index =3D 0; Index < 32; Index++) { - MicroSecondDelay (10); - Ret =3D RandGetBytes (16, RandByte); - if (!Ret) { - return Ret; - } - - // - // Perform XOR operations on two 128-bit value. - // - for (Index2 =3D 0; Index2 < 16; Index2++) { - Xored[Index2] =3D RandByte[Index2] ^ Ffv[Index2]; - } - - AES_encrypt (Xored, Ffv, &AESKey); - } - - for (Index =3D 0; Index < 16; Index++) { - SeedBuffer[Index] =3D Ffv[Index]; - } - - return Ret; -} - -/** - Generate high-quality entropy source. - - @param[in] Length Size of the buffer, in bytes, to fill with. - @param[out] Entropy Pointer to the buffer to store the entropy da= ta. - - @retval EFI_SUCCESS Entropy generation succeeded. - @retval EFI_NOT_READY Failed to request random data. - -**/ -STATIC -BOOLEAN -EFIAPI -RandGenerateEntropy ( - IN UINTN Length, - OUT UINT8 *Entropy - ) -{ - BOOLEAN Ret; - UINTN BlockCount; - UINT8 Seed[16]; - UINT8 *Ptr; - - BlockCount =3D Length / 16; - Ptr =3D (UINT8 *) Entropy; - - // - // Generate high-quality seed for DRBG Entropy - // - while (BlockCount > 0) { - Ret =3D RandGetSeed128 (Seed); - if (!Ret) { - return Ret; - } - CopyMem (Ptr, Seed, 16); - - BlockCount--; - Ptr =3D Ptr + 16; - } - - // - // Populate the remained data as request. - // - Ret =3D RandGetSeed128 (Seed); - if (!Ret) { - return Ret; - } - CopyMem (Ptr, Seed, (Length % 16)); - - return Ret; -} - /* * Add random bytes to the pool to acquire requested amount of entropy * @@ -238,7 +83,7 @@ size_t rand_pool_acquire_entropy(RAND_POOL *pool) buffer =3D rand_pool_add_begin(pool, bytes_needed); =20 if (buffer !=3D NULL) { - Ret =3D RandGenerateEntropy(bytes_needed, buffer); + Ret =3D RandGetBytes(bytes_needed, buffer); if (FALSE =3D=3D Ret) { rand_pool_add_end(pool, 0, 0); } else { @@ -257,13 +102,8 @@ size_t rand_pool_acquire_entropy(RAND_POOL *pool) */ int rand_pool_add_nonce_data(RAND_POOL *pool) { - struct { - UINT64 Rand; - UINT64 TimerValue; - } data =3D { 0 }; - - RandGetBytes(8, (UINT8 *)&(data.Rand)); - data.TimerValue =3D GetPerformanceCounter(); + UINT8 data[16]; + RandGetBytes(sizeof(data), data); =20 return rand_pool_add(pool, (unsigned char*)&data, sizeof(data), 0); } @@ -275,13 +115,8 @@ int rand_pool_add_nonce_data(RAND_POOL *pool) */ int rand_pool_add_additional_data(RAND_POOL *pool) { - struct { - UINT64 Rand; - UINT64 TimerValue; - } data =3D { 0 }; - - RandGetBytes(8, (UINT8 *)&(data.Rand)); - data.TimerValue =3D GetPerformanceCounter(); + UINT8 data[16]; + RandGetBytes(sizeof(data), data); =20 return rand_pool_add(pool, (unsigned char*)&data, sizeof(data), 0); } @@ -313,4 +148,3 @@ void rand_pool_cleanup(void) void rand_pool_keep_random_devices_open(int keep) { } - diff --git a/CryptoPkg/Library/OpensslLib/rand_pool_noise.c b/CryptoPkg/Lib= rary/OpensslLib/rand_pool_noise.c deleted file mode 100644 index 212834e27acc..000000000000 --- a/CryptoPkg/Library/OpensslLib/rand_pool_noise.c +++ /dev/null @@ -1,29 +0,0 @@ -/** @file - Provide rand noise source. - -Copyright (c) 2019, Intel Corporation. All rights reserved.
-SPDX-License-Identifier: BSD-2-Clause-Patent - -**/ - -#include - -/** - Get 64-bit noise source - - @param[out] Rand Buffer pointer to store 64-bit noise source - - @retval FALSE Failed to generate -**/ -BOOLEAN -EFIAPI -GetRandomNoise64 ( - OUT UINT64 *Rand - ) -{ - // - // Return FALSE will fallback to use PerformanceCounter to - // generate noise. - // - return FALSE; -} diff --git a/CryptoPkg/Library/OpensslLib/rand_pool_noise_tsc.c b/CryptoPkg= /Library/OpensslLib/rand_pool_noise_tsc.c deleted file mode 100644 index 4158106231fd..000000000000 --- a/CryptoPkg/Library/OpensslLib/rand_pool_noise_tsc.c +++ /dev/null @@ -1,43 +0,0 @@ -/** @file - Provide rand noise source. - -Copyright (c) 2019, Intel Corporation. All rights reserved.
-SPDX-License-Identifier: BSD-2-Clause-Patent - -**/ - -#include -#include -#include - -/** - Get 64-bit noise source - - @param[out] Rand Buffer pointer to store 64-bit noise source - - @retval TRUE Get randomness successfully. - @retval FALSE Failed to generate -**/ -BOOLEAN -EFIAPI -GetRandomNoise64 ( - OUT UINT64 *Rand - ) -{ - UINT32 Index; - UINT32 *RandPtr; - - if (NULL =3D=3D Rand) { - return FALSE; - } - - RandPtr =3D (UINT32 *)Rand; - - for (Index =3D 0; Index < 2; Index ++) { - *RandPtr =3D (UINT32) ((AsmReadTsc ()) & 0xFF); - RandPtr++; - MicroSecondDelay (10); - } - - return TRUE; -} diff --git a/CryptoPkg/CryptoPkg.dsc b/CryptoPkg/CryptoPkg.dsc index 1af78468a19c..446a89a477e1 100644 --- a/CryptoPkg/CryptoPkg.dsc +++ b/CryptoPkg/CryptoPkg.dsc @@ -60,6 +60,7 @@ BaseCryptLib|CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf TlsLib|CryptoPkg/Library/TlsLibNull/TlsLibNull.inf HashApiLib|CryptoPkg/Library/BaseHashApiLib/BaseHashApiLib.inf + RngLib|MdePkg/Library/BaseRngLibNull/BaseRngLibNull.inf =20 [LibraryClasses.ARM, LibraryClasses.AARCH64] # @@ -119,6 +120,7 @@ ReportStatusCodeLib|MdeModulePkg/Library/DxeReportStatusCodeLib/DxeRepor= tStatusCodeLib.inf BaseCryptLib|CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf TlsLib|CryptoPkg/Library/TlsLib/TlsLib.inf + RngLib|SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf =20 [LibraryClasses.common.DXE_SMM_DRIVER] ReportStatusCodeLib|MdeModulePkg/Library/SmmReportStatusCodeLib/SmmRepor= tStatusCodeLib.inf diff --git a/CryptoPkg/Library/OpensslLib/OpensslLib.inf b/CryptoPkg/Librar= y/OpensslLib/OpensslLib.inf index dbbe5386a10c..4baad565564c 100644 --- a/CryptoPkg/Library/OpensslLib/OpensslLib.inf +++ b/CryptoPkg/Library/OpensslLib/OpensslLib.inf @@ -571,22 +571,9 @@ $(OPENSSL_PATH)/ssl/statem/statem_local.h # Autogenerated files list ends here buildinf.h - rand_pool_noise.h ossl_store.c rand_pool.c =20 -[Sources.Ia32] - rand_pool_noise_tsc.c - -[Sources.X64] - rand_pool_noise_tsc.c - -[Sources.ARM] - rand_pool_noise.c - -[Sources.AARCH64] - rand_pool_noise.c - [Packages] MdePkg/MdePkg.dec CryptoPkg/CryptoPkg.dec @@ -594,7 +581,7 @@ [LibraryClasses] BaseLib DebugLib - TimerLib + RngLib PrintLib =20 [LibraryClasses.ARM] diff --git a/CryptoPkg/Library/OpensslLib/OpensslLibCrypto.inf b/CryptoPkg/= Library/OpensslLib/OpensslLibCrypto.inf index 616ccd9f62d1..3557711bd85a 100644 --- a/CryptoPkg/Library/OpensslLib/OpensslLibCrypto.inf +++ b/CryptoPkg/Library/OpensslLib/OpensslLibCrypto.inf @@ -520,22 +520,9 @@ $(OPENSSL_PATH)/crypto/x509v3/v3_admis.h # Autogenerated files list ends here buildinf.h - rand_pool_noise.h ossl_store.c rand_pool.c =20 -[Sources.Ia32] - rand_pool_noise_tsc.c - -[Sources.X64] - rand_pool_noise_tsc.c - -[Sources.ARM] - rand_pool_noise.c - -[Sources.AARCH64] - rand_pool_noise.c - [Packages] MdePkg/MdePkg.dec CryptoPkg/CryptoPkg.dec @@ -543,7 +530,7 @@ [LibraryClasses] BaseLib DebugLib - TimerLib + RngLib PrintLib =20 [LibraryClasses.ARM] diff --git a/CryptoPkg/Library/OpensslLib/rand_pool_noise.h b/CryptoPkg/Lib= rary/OpensslLib/rand_pool_noise.h deleted file mode 100644 index 75acc686a9f1..000000000000 --- a/CryptoPkg/Library/OpensslLib/rand_pool_noise.h +++ /dev/null @@ -1,29 +0,0 @@ -/** @file - Provide rand noise source. - -Copyright (c) 2019, Intel Corporation. All rights reserved.
-SPDX-License-Identifier: BSD-2-Clause-Patent - -**/ - -#ifndef __RAND_POOL_NOISE_H__ -#define __RAND_POOL_NOISE_H__ - -#include - -/** - Get 64-bit noise source. - - @param[out] Rand Buffer pointer to store 64-bit noise source - - @retval TRUE Get randomness successfully. - @retval FALSE Failed to generate -**/ -BOOLEAN -EFIAPI -GetRandomNoise64 ( - OUT UINT64 *Rand - ); - - -#endif // __RAND_POOL_NOISE_H__ --=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 (#63475): https://edk2.groups.io/g/devel/message/63475 Mute This Topic: https://groups.io/mt/75890827/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- From nobody Thu Apr 25 23:04:57 2024 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+63476+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+63476+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1596129688; cv=none; d=zohomail.com; s=zohoarc; b=RVRsb662v6/UxsHbm9bD620HqnxUai3kM9nlT/lYv4hJTeRszkF+3+VkU84HM3xf8RObAczNpolYhE++DiX6FiXjAfBqhDJPlhxqI8h5jp493RIsrDhP9Ltnz6LMEX+qXVVMZY5XsHsb2roC2WON2Lr66sS5khZKBuzva8oGkLY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1596129688; 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=CrkGAz9kaSF/sqzdpJsMiibDw54WdFkvcKO3uEwaQRY=; b=RDWqdf3QEcjMlEQz1Y2GTNMBqmm30mMdTh+93GS3Z29thFsmRIO41u4zKd5SIDGGPFybCQrcpQUcdfwuZh4U1iM4H8MEJTJ37xNBvbGgZXbKjimhmhxZTgfw5Teb1HFRbXVeyVOLPTzybIFzJcI+akzGFOu1fAmbD40TtB+d59c= 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+63476+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 159612968809089.36219363713724; Thu, 30 Jul 2020 10:21:28 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id VMHXYY1788612x4IwVdp4pZ2; Thu, 30 Jul 2020 10:21:26 -0700 X-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 X-Received: by mail-pj1-f49.google.com with SMTP id lx9so5152097pjb.2 for ; Thu, 30 Jul 2020 10:21:26 -0700 (PDT) X-Gm-Message-State: rlg08X87WT1ofYAjeqnEZAvox1787277AA= 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) X-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: [edk2-devel] [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> In-Reply-To: <20200730172117.1558-1-matthewfcarlson@gmail.com> References: <20200730172117.1558-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=1596129686; bh=BOj9+SUxPOatR6/wzU179JSxtqisUTJNDzuvqfeloYI=; h=Cc:Date:From:Reply-To:Subject:To; b=m8kraUHjWreWR2B8I7+a/YFBisJFih9OCGe65Xh/Fv0OdldxREpkvw9OgcMKN2fErCI HACnsmZHIsjc2McfdjVEJ86nP/hRt086OfUp0rGestbpgE5K4XyL9s5nSaKVtqz4L1QPW 9s4HCDbk6NGiJLsyuVsz+H2+LdmvY8VUTMg= 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/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 + 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 + for (Index =3D 0; Index < 2; Index ++) { + *RandPtr =3D (UINT8) (GetPerformanceCounter () & 0xFF); + MicroSecondDelay (4); + 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 + for (Index =3D 0; Index < 4; Index ++) { + *RandPtr =3D (UINT8) (GetPerformanceCounter () & 0xFF); + MicroSecondDelay (2); + 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 + for (Index =3D 0; Index < 8; Index ++) { + *RandPtr =3D (UINT8) (GetPerformanceCounter () & 0xFF); + MicroSecondDelay (1); + 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); + + // + // Read first 64 bits + // + if (!GetRandomNumber64 (Rand)) { + return FALSE; + } + + // + // Read second 64 bits + // + return GetRandomNumber64 (++Rand); +} 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 +# 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. +# +# Copyright (c) Microsoft Corporation. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +# +## + +[Defines] + INF_VERSION =3D 0x00010005 + BASE_NAME =3D BaseRngLibTimer + MODULE_UNI_FILE =3D BaseRngLibTimer.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 + DebugLib + TimerLib 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 +// Instance of RNG (Random Number Generator) Library. +// +// BaseRng Library that uses CPU RdRand instruction access to provide +// high-quality 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-quality random numbers" + 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 MdePkg/Library/BasePrintLib/BasePrintLib.inf MdePkg/Library/BaseReportStatusCodeLibNull/BaseReportStatusCodeLibNull.i= nf + MdePkg/Library/BaseRngLibTimer/BaseRngLibTimer.inf MdePkg/Library/BaseSerialPortLibNull/BaseSerialPortLibNull.inf MdePkg/Library/BaseSynchronizationLib/BaseSynchronizationLib.inf MdePkg/Library/BaseTimerLibNullTemplate/BaseTimerLibNullTemplate.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 (#63476): https://edk2.groups.io/g/devel/message/63476 Mute This Topic: https://groups.io/mt/75890830/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-