From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f65.google.com (mail-pj1-f65.google.com [209.85.216.65]) by mx.groups.io with SMTP id smtpd.web11.7062.1597112528869455276 for ; Mon, 10 Aug 2020 19:22:08 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Sxax7Upm; spf=pass (domain: gmail.com, ip: 209.85.216.65, mailfrom: matthewfcarlson@gmail.com) Received: by mail-pj1-f65.google.com with SMTP id ha11so1064898pjb.1 for ; Mon, 10 Aug 2020 19:22:08 -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=JTUHxNr2OvHpiLC8Q2Xhfn5YsnYk6gR3FsOgdaISKLQ=; b=Sxax7UpmkRhixCzy1VfDaDRGUizGxf3RKbHCNW891sLw0sIFibjuWFSXmW1uHweN5H QnJRqpXMLDm0pBnzflbT22dONUiHQ1ZPKUttdbOfdzQmBdIncQPXKzER1y85+NR5f9Je pv7MEQtU7OVrBS4oJRmRPsgGNdExTGSyrDJBgQkC8ozDnnvQNVnxC4pj2FPbVXG7VUX6 b83c8J8jj/W/8rfEvQvGTjQvgHQ91J8pJUvbBDuugIm7jEK4gDCz8lEnRAeL3oZtUD7G ETJwrJOKk9X9enEfqYIaa8u98TO8hmzsqqnFIOZaYSlmSokp/bGi9mIh0HCvaNdJ3m6p T7rQ== 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=JTUHxNr2OvHpiLC8Q2Xhfn5YsnYk6gR3FsOgdaISKLQ=; b=qkrAxzGPL663UEX8aGDfJzZxMFi9C25P8NYCRfLthavdY/rnr5074DrrO/hQZdjl3d j1NG1KbD0cz7lsRFexkAwO9bcL5OwLHZkNi+tctHZ6VjQiChTnnNjkmD5d25RugzkEFp RihHzfcirVMsXrGs1UrcNGapqMmfv/Bv6W0Xs1WQdCm8P6Nc28RggQN9cuXvKIMQ2FX9 aJh5kk+6B7fLWLO9GI/R4Hfystz7WZSXC+UKkQjjSnmVY4EJuVuTBAii9lSe55HuTkNK xZTNqM+/kBqL5Bq1kV4Z1B+R1GVq1+M/yQyciAFhjBwkdTDk2yX2AborBp7KZaxZe6wQ RtGw== X-Gm-Message-State: AOAM530v4rsyiReNIt2YVypuTIF67JLf2xjyyklnF3wRi1/vWFEXfcsC XBNvMWVGPukSA4fdBaWLHGd6UzpYQPQSsA== X-Google-Smtp-Source: ABdhPJxvp6NQSdzGcYfTn5YfLmsPBkaBdlsI/RTyC70yOfB895xveOFi4aIMbYb2De8HnU75mW2Tug== X-Received: by 2002:a17:90b:4acd:: with SMTP id mh13mr2361654pjb.147.1597112528135; Mon, 10 Aug 2020 19:22:08 -0700 (PDT) Return-Path: Received: from tvis-name-05.localdomain ([50.34.40.129]) by smtp.gmail.com with ESMTPSA id r134sm24146774pfc.1.2020.08.10.19.22.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Aug 2020 19:22:07 -0700 (PDT) From: "Matthew Carlson" To: devel@edk2.groups.io Cc: Jiewen Yao , Jian J Wang , Xiaoyu Lu , Matthew Carlson Subject: [PATCH v4 5/5] CryptoPkg: OpensslLib: Use RngLib to generate entropy in rand_pool Date: Mon, 10 Aug 2020 19:22:00 -0700 Message-Id: <20200811022200.1087-6-matthewfcarlson@gmail.com> X-Mailer: git-send-email 2.27.0.windows.1 In-Reply-To: <20200811022200.1087-1-matthewfcarlson@gmail.com> References: <20200811022200.1087-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 | 203 ++---------------= --- CryptoPkg/Library/OpensslLib/rand_pool_noise.c | 29 --- CryptoPkg/Library/OpensslLib/rand_pool_noise_tsc.c | 43 ----- CryptoPkg/CryptoPkg.dsc | 1 + 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..3da92699fef6 100644 --- a/CryptoPkg/Library/OpensslLib/rand_pool.c +++ b/CryptoPkg/Library/OpensslLib/rand_pool.c @@ -11,53 +11,18 @@ 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 a buffer of arbitrary size with random bytes.=0D + This is a shim layer to RngLib.=0D =0D @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 +38,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 +65,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 +84,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 +103,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 +116,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 +149,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..0490eeb7e22f 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 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