From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pg1-f179.google.com (mail-pg1-f179.google.com [209.85.215.179]) by mx.groups.io with SMTP id smtpd.web11.1408.1597272247682902720 for ; Wed, 12 Aug 2020 15:44:07 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@gmail.com header.s=20161025 header.b=aJFT25Kb; spf=pass (domain: gmail.com, ip: 209.85.215.179, mailfrom: matthewfcarlson@gmail.com) Received: by mail-pg1-f179.google.com with SMTP id m34so1771084pgl.11 for ; Wed, 12 Aug 2020 15:44:07 -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=Cl+KZ2qz8w/h2Y+8n99FywuFJ0mIhHfdyx++6uoi9ws=; b=aJFT25KbKtfe8zOhF/FVGsJZRF59PMCJBfVPrscOLfdnMQ9+y4kiz5bbtBwCpZYIpp vrDqW8UvhlN2feZnyheXGg8CkG3dOsogoCVTFME1PNAEBqSAf4/7kcQIPmhTjZXj6uN3 X/7QVae7gg5h4UNUMDB/PXwax13tJpzNCkWOYbIiaYnc+PjKs+eoXj77SyEyq2VtygGW 26Y1aXy5jx6Cffbjmi+EQ5h2CC7AhlPCUGemBNiThywbTxSU8DjAPRI6XB5QmeX+4mVu iA4PjwjK9hbbAl4Tt0nkOEk3styp8XrGTbiLDgii8tGjweJXhQHwvL5b5fs4Qa7S8fin wSNA== 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=Cl+KZ2qz8w/h2Y+8n99FywuFJ0mIhHfdyx++6uoi9ws=; b=NBg/oumQ4j+LKyxWTVLVbunZZvAGZ+By8Xt1NQVW2/pStTCNiRP314Sm8k0XGr5lO8 iDFB08LhXhTB2IGNvk41+3j7s0uK8ZbxnTQjClN3QbL04OYCMxZMjkNd+byy/pBoi4UY i55f7X+ME4i6fOqs3N5UvB8+QKDvuHcWUrmLGpyBj4fTkEkJnjEGNyqeDJpqJAoGJpRn mtLPCJvuYPss7fGnFJnUVO2GgXaPcHo5WN9oNwGB1mRXZaXUpIv9zl+wu/Q+z8ZG7ScZ 3CUEIbOhGE7ogjFPhL0+Y8Ywl41hM6LXFbNIsbN5tS39eOanO3TUXn8cjEVD3vhye9m7 Yjkw== X-Gm-Message-State: AOAM531rtU0HtEwmwUFsl+W+PfmoQnHHupQeGYoiHFmqX2sgdzaJo05b 0k95JeJlwGo8W0gYGKOnZZ3sqbeVlP+rZA== X-Google-Smtp-Source: ABdhPJzSVDnhA4CVSEbh6eAXG9/zQZErRJ0q3x/EYctDFF0W0RiSeDCJNn3uYJVgygnTfI4JVUcG5A== X-Received: by 2002:a63:cd54:: with SMTP id a20mr1190860pgj.228.1597272246898; Wed, 12 Aug 2020 15:44:06 -0700 (PDT) Return-Path: Received: from tvis-name-05.localdomain ([50.34.40.129]) by smtp.gmail.com with ESMTPSA id l63sm3252943pgl.24.2020.08.12.15.44.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Aug 2020 15:44:06 -0700 (PDT) From: "Matthew Carlson" To: devel@edk2.groups.io Cc: Ard Biesheuvel , Jiewen Yao , Jian J Wang , Xiaoyu Lu , Matthew Carlson Subject: [PATCH v6 5/5] CryptoPkg: OpensslLib: Use RngLib to generate entropy in rand_pool Date: Wed, 12 Aug 2020 15:43:38 -0700 Message-Id: <20200812224338.287-6-matthewfcarlson@gmail.com> X-Mailer: git-send-email 2.27.0.windows.1 In-Reply-To: <20200812224338.287-1-matthewfcarlson@gmail.com> References: <20200812224338.287-1-matthewfcarlson@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Matthew Carlson Ref: https://github.com/tianocore/edk2/pull/845 Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=3D1871 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: Ard Biesheuvel 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