From mboxrd@z Thu Jan 1 00:00:00 1970 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 Authentication-Results: mx.groups.io; dkim=pass header.i=@gmail.com header.s=20161025 header.b=XnKAk7+V; spf=pass (domain: gmail.com, ip: 209.85.216.47, mailfrom: matthewfcarlson@gmail.com) Received: by mail-pj1-f47.google.com with SMTP id k1so4774594pjt.5 for ; Thu, 30 Jul 2020 10:21:25 -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=Jqu0KI0ycYexHRiVUF/RfCpFc2U71lkPfk8JN6URQI8=; b=XnKAk7+VfEZkJysILQZ1gO3Aj6Mjjz79q7Cx+JN424MdvyfKuxJMcWW6wS8V25tWxa k6PnZE4dtpRC1s8vdo+Q3Po/6/liB3kcr8n3tzAwoXwq5qJzxbPle/Xu9kCRudd8gvup XDq/ynNhboUzqzT97R3rejayi3luycR2MAe5xkXWAUW5b9Fcp639TKhxs8zRMG52jBvF wvkTCtLDz3m8JnUGwCZJzdXRwubi/8ckOHwJ2sJw0BdJwVmGPCc5va5GpsmBFhHKyBYB UgJ8LTEsk5wNpinO3NdHwI8g9cB3cWfeZxQbd2Wckr+wP1Kob6SgYDsSafDeE7aFD+fV Kt0w== 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=Jqu0KI0ycYexHRiVUF/RfCpFc2U71lkPfk8JN6URQI8=; b=ofIKPGVXCwVxJDYiptoBsEZZVYUI31LFrbdj86zttwJvvZ99WckdjoxpHpIzEMR0si 8Ag2MBvXa9rIH0E+9g4x8wStie/KryGkQpOXUYCU/G8TeIAw285/bC19n579JymFwbHQ I7nG3q3zhwKtV0FCrWrhHFR5mbVjQ1m8eJIgtyNeILmcCOzDlkrHsQChOraykuifnjjY E+QgexKTOyh03fAI3Jxb7wjxlO0J3mhTC2SvGUNMN9vlgctPAR1SkM2CyPIQwKz37wkv EWnj0N7rExE7bYssQqXFYgK8VuPC+/WZGDCevdFYzIKb1yIz7uF5bTiK/v/JpNwDgYA5 8K3w== X-Gm-Message-State: AOAM530hufEFYkD80Ohrq/VTEOAiyRx61FBooV+swQd6exZOlXeWg+Hc NBQrn9HZzY6MSlaYTiO9TB7Z3eZsC0U= 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) 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.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: [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> 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 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 =0D =0D #include =0D -#include =0D -=0D -#include "rand_pool_noise.h"=0D -=0D -/**=0D - Get some randomness from low-order bits of GetPerformanceCounter results= .=0D - And combine them to the 64-bit value=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.=0D -**/=0D -STATIC=0D -BOOLEAN=0D -EFIAPI=0D -GetRandNoise64FromPerformanceCounter(=0D - OUT UINT64 *Rand=0D - )=0D -{=0D - UINT32 Index;=0D - UINT32 *RandPtr;=0D -=0D - if (NULL =3D=3D Rand) {=0D - return FALSE;=0D - }=0D -=0D - RandPtr =3D (UINT32 *) Rand;=0D -=0D - for (Index =3D 0; Index < 2; Index ++) {=0D - *RandPtr =3D (UINT32) (GetPerformanceCounter () & 0xFF);=0D - MicroSecondDelay (10);=0D - RandPtr++;=0D - }=0D -=0D - return TRUE;=0D -}=0D +#include =0D =0D /**=0D Calls RandomNumber64 to fill=0D @@ -56,8 +20,8 @@ GetRandNoise64FromPerformanceCounter( @param[in] Length Size of the buffer, in bytes, to fill with.= =0D @param[out] RandBuffer Pointer to the buffer to store the random res= ult.=0D =0D - @retval EFI_SUCCESS Random bytes generation succeeded.=0D - @retval EFI_NOT_READY Failed to request random bytes.=0D + @retval True Random bytes generation succeeded.=0D + @retval False Failed to request random bytes.=0D =0D **/=0D STATIC=0D @@ -73,17 +37,17 @@ RandGetBytes ( =0D Ret =3D FALSE;=0D =0D + if (RandBuffer =3D=3D NULL) {=0D + DEBUG((DEBUG_ERROR, "[OPENSSL_RAND_POOL] NULL RandBuffer. No random nu= mbers are generated and your system is not secure\n"));=0D + ASSERT(FALSE); // Since we can't generate random numbers, we should as= sert. Otherwise we will just blow up later.=0D + return Ret;=0D + }=0D +=0D +=0D while (Length > 0) {=0D - //=0D - // Get random noise from platform.=0D - // If it failed, fallback to PerformanceCounter=0D - // If you really care about security, you must override=0D - // GetRandomNoise64FromPlatform.=0D - //=0D - Ret =3D GetRandomNoise64 (&TempRand);=0D - if (Ret =3D=3D FALSE) {=0D - Ret =3D GetRandNoise64FromPerformanceCounter (&TempRand);=0D - }=0D + // Use RngLib to get random number=0D + Ret =3D GetRandomNumber64(&TempRand);=0D +=0D if (!Ret) {=0D return Ret;=0D }=0D @@ -100,125 +64,6 @@ RandGetBytes ( return Ret;=0D }=0D =0D -/**=0D - Creates a 128bit random value that is fully forward and backward predict= ion resistant,=0D - suitable for seeding a NIST SP800-90 Compliant.=0D - This function takes multiple random numbers from PerformanceCounter to e= nsure reseeding=0D - and performs AES-CBC-MAC over the data to compute the seed value.=0D -=0D - @param[out] SeedBuffer Pointer to a 128bit buffer to store the rando= m seed.=0D -=0D - @retval TRUE Random seed generation succeeded.=0D - @retval FALSE Failed to request random bytes.=0D -=0D -**/=0D -STATIC=0D -BOOLEAN=0D -EFIAPI=0D -RandGetSeed128 (=0D - OUT UINT8 *SeedBuffer=0D - )=0D -{=0D - BOOLEAN Ret;=0D - UINT8 RandByte[16];=0D - UINT8 Key[16];=0D - UINT8 Ffv[16];=0D - UINT8 Xored[16];=0D - UINT32 Index;=0D - UINT32 Index2;=0D - AES_KEY AESKey;=0D -=0D - //=0D - // Chose an arbitrary key and zero the feed_forward_value (FFV)=0D - //=0D - for (Index =3D 0; Index < 16; Index++) {=0D - Key[Index] =3D (UINT8) Index;=0D - Ffv[Index] =3D 0;=0D - }=0D -=0D - AES_set_encrypt_key (Key, 16 * 8, &AESKey);=0D -=0D - //=0D - // Perform CBC_MAC over 32 * 128 bit values, with 10us gaps between 128 = bit value=0D - // The 10us gaps will ensure multiple reseeds within the system time wit= h a large=0D - // design margin.=0D - //=0D - for (Index =3D 0; Index < 32; Index++) {=0D - MicroSecondDelay (10);=0D - Ret =3D RandGetBytes (16, RandByte);=0D - if (!Ret) {=0D - return Ret;=0D - }=0D -=0D - //=0D - // Perform XOR operations on two 128-bit value.=0D - //=0D - for (Index2 =3D 0; Index2 < 16; Index2++) {=0D - Xored[Index2] =3D RandByte[Index2] ^ Ffv[Index2];=0D - }=0D -=0D - AES_encrypt (Xored, Ffv, &AESKey);=0D - }=0D -=0D - for (Index =3D 0; Index < 16; Index++) {=0D - SeedBuffer[Index] =3D Ffv[Index];=0D - }=0D -=0D - return Ret;=0D -}=0D -=0D -/**=0D - Generate high-quality entropy source.=0D -=0D - @param[in] Length Size of the buffer, in bytes, to fill with.=0D - @param[out] Entropy Pointer to the buffer to store the entropy da= ta.=0D -=0D - @retval EFI_SUCCESS Entropy generation succeeded.=0D - @retval EFI_NOT_READY Failed to request random data.=0D -=0D -**/=0D -STATIC=0D -BOOLEAN=0D -EFIAPI=0D -RandGenerateEntropy (=0D - IN UINTN Length,=0D - OUT UINT8 *Entropy=0D - )=0D -{=0D - BOOLEAN Ret;=0D - UINTN BlockCount;=0D - UINT8 Seed[16];=0D - UINT8 *Ptr;=0D -=0D - BlockCount =3D Length / 16;=0D - Ptr =3D (UINT8 *) Entropy;=0D -=0D - //=0D - // Generate high-quality seed for DRBG Entropy=0D - //=0D - while (BlockCount > 0) {=0D - Ret =3D RandGetSeed128 (Seed);=0D - if (!Ret) {=0D - return Ret;=0D - }=0D - CopyMem (Ptr, Seed, 16);=0D -=0D - BlockCount--;=0D - Ptr =3D Ptr + 16;=0D - }=0D -=0D - //=0D - // Populate the remained data as request.=0D - //=0D - Ret =3D RandGetSeed128 (Seed);=0D - if (!Ret) {=0D - return Ret;=0D - }=0D - CopyMem (Ptr, Seed, (Length % 16));=0D -=0D - return Ret;=0D -}=0D -=0D /*=0D * Add random bytes to the pool to acquire requested amount of entropy=0D *=0D @@ -238,7 +83,7 @@ size_t rand_pool_acquire_entropy(RAND_POOL *pool) buffer =3D rand_pool_add_begin(pool, bytes_needed);=0D =0D if (buffer !=3D NULL) {=0D - Ret =3D RandGenerateEntropy(bytes_needed, buffer);=0D + Ret =3D RandGetBytes(bytes_needed, buffer);=0D if (FALSE =3D=3D Ret) {=0D rand_pool_add_end(pool, 0, 0);=0D } else {=0D @@ -257,13 +102,8 @@ size_t rand_pool_acquire_entropy(RAND_POOL *pool) */=0D int rand_pool_add_nonce_data(RAND_POOL *pool)=0D {=0D - struct {=0D - UINT64 Rand;=0D - UINT64 TimerValue;=0D - } data =3D { 0 };=0D -=0D - RandGetBytes(8, (UINT8 *)&(data.Rand));=0D - data.TimerValue =3D GetPerformanceCounter();=0D + UINT8 data[16];=0D + RandGetBytes(sizeof(data), data);=0D =0D return rand_pool_add(pool, (unsigned char*)&data, sizeof(data), 0);=0D }=0D @@ -275,13 +115,8 @@ int rand_pool_add_nonce_data(RAND_POOL *pool) */=0D int rand_pool_add_additional_data(RAND_POOL *pool)=0D {=0D - struct {=0D - UINT64 Rand;=0D - UINT64 TimerValue;=0D - } data =3D { 0 };=0D -=0D - RandGetBytes(8, (UINT8 *)&(data.Rand));=0D - data.TimerValue =3D GetPerformanceCounter();=0D + UINT8 data[16];=0D + RandGetBytes(sizeof(data), data);=0D =0D return rand_pool_add(pool, (unsigned char*)&data, sizeof(data), 0);=0D }=0D @@ -313,4 +148,3 @@ void rand_pool_cleanup(void) void rand_pool_keep_random_devices_open(int keep)=0D {=0D }=0D -=0D 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=0D - Provide rand noise source.=0D -=0D -Copyright (c) 2019, Intel Corporation. All rights reserved.
=0D -SPDX-License-Identifier: BSD-2-Clause-Patent=0D -=0D -**/=0D -=0D -#include =0D -=0D -/**=0D - Get 64-bit noise source=0D -=0D - @param[out] Rand Buffer pointer to store 64-bit noise source=0D -=0D - @retval FALSE Failed to generate=0D -**/=0D -BOOLEAN=0D -EFIAPI=0D -GetRandomNoise64 (=0D - OUT UINT64 *Rand=0D - )=0D -{=0D - //=0D - // Return FALSE will fallback to use PerformanceCounter to=0D - // generate noise.=0D - //=0D - return FALSE;=0D -}=0D 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=0D - Provide rand noise source.=0D -=0D -Copyright (c) 2019, Intel Corporation. All rights reserved.
=0D -SPDX-License-Identifier: BSD-2-Clause-Patent=0D -=0D -**/=0D -=0D -#include =0D -#include =0D -#include =0D -=0D -/**=0D - Get 64-bit noise source=0D -=0D - @param[out] Rand Buffer pointer to store 64-bit noise source=0D -=0D - @retval TRUE Get randomness successfully.=0D - @retval FALSE Failed to generate=0D -**/=0D -BOOLEAN=0D -EFIAPI=0D -GetRandomNoise64 (=0D - OUT UINT64 *Rand=0D - )=0D -{=0D - UINT32 Index;=0D - UINT32 *RandPtr;=0D -=0D - if (NULL =3D=3D Rand) {=0D - return FALSE;=0D - }=0D -=0D - RandPtr =3D (UINT32 *)Rand;=0D -=0D - for (Index =3D 0; Index < 2; Index ++) {=0D - *RandPtr =3D (UINT32) ((AsmReadTsc ()) & 0xFF);=0D - RandPtr++;=0D - MicroSecondDelay (10);=0D - }=0D -=0D - return TRUE;=0D -}=0D 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=0D TlsLib|CryptoPkg/Library/TlsLibNull/TlsLibNull.inf=0D HashApiLib|CryptoPkg/Library/BaseHashApiLib/BaseHashApiLib.inf=0D + RngLib|MdePkg/Library/BaseRngLibNull/BaseRngLibNull.inf=0D =0D [LibraryClasses.ARM, LibraryClasses.AARCH64]=0D #=0D @@ -119,6 +120,7 @@ ReportStatusCodeLib|MdeModulePkg/Library/DxeReportStatusCodeLib/DxeRepor= tStatusCodeLib.inf=0D BaseCryptLib|CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf=0D TlsLib|CryptoPkg/Library/TlsLib/TlsLib.inf=0D + RngLib|SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf=0D =0D [LibraryClasses.common.DXE_SMM_DRIVER]=0D ReportStatusCodeLib|MdeModulePkg/Library/SmmReportStatusCodeLib/SmmRepor= tStatusCodeLib.inf=0D 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=0D # Autogenerated files list ends here=0D buildinf.h=0D - rand_pool_noise.h=0D ossl_store.c=0D rand_pool.c=0D =0D -[Sources.Ia32]=0D - rand_pool_noise_tsc.c=0D -=0D -[Sources.X64]=0D - rand_pool_noise_tsc.c=0D -=0D -[Sources.ARM]=0D - rand_pool_noise.c=0D -=0D -[Sources.AARCH64]=0D - rand_pool_noise.c=0D -=0D [Packages]=0D MdePkg/MdePkg.dec=0D CryptoPkg/CryptoPkg.dec=0D @@ -594,7 +581,7 @@ [LibraryClasses]=0D BaseLib=0D DebugLib=0D - TimerLib=0D + RngLib=0D PrintLib=0D =0D [LibraryClasses.ARM]=0D 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=0D # Autogenerated files list ends here=0D buildinf.h=0D - rand_pool_noise.h=0D ossl_store.c=0D rand_pool.c=0D =0D -[Sources.Ia32]=0D - rand_pool_noise_tsc.c=0D -=0D -[Sources.X64]=0D - rand_pool_noise_tsc.c=0D -=0D -[Sources.ARM]=0D - rand_pool_noise.c=0D -=0D -[Sources.AARCH64]=0D - rand_pool_noise.c=0D -=0D [Packages]=0D MdePkg/MdePkg.dec=0D CryptoPkg/CryptoPkg.dec=0D @@ -543,7 +530,7 @@ [LibraryClasses]=0D BaseLib=0D DebugLib=0D - TimerLib=0D + RngLib=0D PrintLib=0D =0D [LibraryClasses.ARM]=0D 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=0D - Provide rand noise source.=0D -=0D -Copyright (c) 2019, Intel Corporation. All rights reserved.
=0D -SPDX-License-Identifier: BSD-2-Clause-Patent=0D -=0D -**/=0D -=0D -#ifndef __RAND_POOL_NOISE_H__=0D -#define __RAND_POOL_NOISE_H__=0D -=0D -#include =0D -=0D -/**=0D - Get 64-bit noise source.=0D -=0D - @param[out] Rand Buffer pointer to store 64-bit noise source=0D -=0D - @retval TRUE Get randomness successfully.=0D - @retval FALSE Failed to generate=0D -**/=0D -BOOLEAN=0D -EFIAPI=0D -GetRandomNoise64 (=0D - OUT UINT64 *Rand=0D - );=0D -=0D -=0D -#endif // __RAND_POOL_NOISE_H__=0D --=20 2.27.0.windows.1