From nobody Fri Apr 26 15:33:02 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+63372+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+63372+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1595901198; cv=none; d=zohomail.com; s=zohoarc; b=mQNXMM/VGWe/SycJxIYYiQ2hwT3wX+lpkWfmFGxfGQ8F2aKcCKc743p2Rn4UZR0N2qZm6xfzWML860kKAFubIJb/0HxRxtm0j+8ykXPXShi5UfIut8Hcc5NjDY+NxwQRcgJaNBl1HPT4iZt6DoNUGvR4uAsbkNJLVC1ssaXzDYs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1595901198; 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=q3PX7Ubmg14vISlih3nXWc987bNIAN72AN478a//crc=; b=C0zs7L+NIzeFujP2ZrV56hjgOYANArEExZP0VhVGReYYtuPI9YzlLOerGtPzIN7ZQtCdnaFiT6yL4eyNXKo0fOIzw9rx6r8VJ/MkWs3BSA4YX7NOY7NH5g9fVyO5DJuFIpH/SVl5lIj3c+QaSzgCJ7tCAF+zmJmM5T05cQDSEto= 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+63372+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 1595901198283799.2384785748152; Mon, 27 Jul 2020 18:53:18 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id VBGXYY1788612x4zncsHG2cM; Mon, 27 Jul 2020 18:53:17 -0700 X-Received: from mail-pf1-f193.google.com (mail-pf1-f193.google.com [209.85.210.193]) by mx.groups.io with SMTP id smtpd.web12.3455.1595901196676725297 for ; Mon, 27 Jul 2020 18:53:16 -0700 X-Received: by mail-pf1-f193.google.com with SMTP id l2so3740702pff.0 for ; Mon, 27 Jul 2020 18:53:16 -0700 (PDT) X-Gm-Message-State: Su8XZpG1Kg8ysHrxAvjLSu5sx1787277AA= X-Google-Smtp-Source: ABdhPJwxz9G7L3Q6pNyJRflwsezflHXbuvwsWsVUgtjKFcOjS86eztemx85VYrx8FIcJKaJF+1EJ2A== X-Received: by 2002:a63:1007:: with SMTP id f7mr16763010pgl.147.1595901195996; Mon, 27 Jul 2020 18:53:15 -0700 (PDT) X-Received: from tvis-name-05.localdomain ([50.34.40.129]) by smtp.gmail.com with ESMTPSA id a184sm9579151pfa.83.2020.07.27.18.53.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Jul 2020 18:53:15 -0700 (PDT) From: "Matthew Carlson" To: devel@edk2.groups.io Cc: Jiewen Yao , Jian J Wang , Xiaoyu Lu , Matthew Carlson Subject: [edk2-devel] [PATCH v1 1/2] CryptoPkg: OpensslLib: Use RngLib to generate entropy in rand_pool Date: Mon, 27 Jul 2020 18:53:11 -0700 Message-Id: <20200728015312.1023-2-matthewfcarlson@gmail.com> In-Reply-To: <20200728015312.1023-1-matthewfcarlson@gmail.com> References: <20200728015312.1023-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=1595901197; bh=tes444ITyw1u9kDGDX1oQxiefOH/BZFWb/mnnEn6a/s=; h=Cc:Date:From:Reply-To:Subject:To; b=ZjOr3pdOoS8P4ROEdFByzNeJv1CeRiBxzPVWN9y8OZYs7mfEhoCkrzekVBA2HYFdPlr +Ux3PMz7V2C0xHd1GVuYi9gKqs3CWR2uJZbsq0xvDC39ldAurLJ375IgEiKv5mZyK8Og6 A5WuDUFp1OnoBI0Eit5mOxWdjChXXuWDLXQ= 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 | 200 ++---------------= --- CryptoPkg/Library/OpensslLib/rand_pool_noise.c | 29 --- CryptoPkg/Library/OpensslLib/rand_pool_noise_tsc.c | 43 ----- CryptoPkg/Library/OpensslLib/OpensslLib.inf | 15 +- CryptoPkg/Library/OpensslLib/OpensslLibCrypto.inf | 15 +- CryptoPkg/Library/OpensslLib/rand_pool_noise.h | 29 --- 6 files changed, 20 insertions(+), 311 deletions(-) diff --git a/CryptoPkg/Library/OpensslLib/rand_pool.c b/CryptoPkg/Library/O= pensslLib/rand_pool.c index 9e0179b03490..55bf6c9c6950 100644 --- a/CryptoPkg/Library/OpensslLib/rand_pool.c +++ b/CryptoPkg/Library/OpensslLib/rand_pool.c @@ -11,44 +11,10 @@ SPDX-License-Identifier: BSD-2-Clause-Patent #include =20 #include -#include +#include =20 #include "rand_pool_noise.h" =20 -/** - 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; -} - /** Calls RandomNumber64 to fill a buffer of arbitrary size with random bytes. @@ -56,8 +22,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 +39,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 +66,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 +85,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 +104,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 +117,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 +150,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/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 (#63372): https://edk2.groups.io/g/devel/message/63372 Mute This Topic: https://groups.io/mt/75836597/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 Fri Apr 26 15:33:02 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+63373+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+63373+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1595901199; cv=none; d=zohomail.com; s=zohoarc; b=WGuJ5YP48wsBG9wIkb/iW98lBM6gFkEuIpLHvmK0efSFXWiAq90lhpoq8T/HNdFkA04Hg5XAxWXZBsGmhJj7Rjla5NE3AW3cK0NjO4Y//2esh1TtNmcz752PGwTEGhFgXf/PiED8Iv+hWETw5v0XPfZVmS1K0UQy4CGNONwTAbg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1595901199; 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=Wb4uguc76lNZnw2SEO+9LNRonaS0wFjp3Ftt0I4LixM=; b=CLkRG6lmmDM+oW1TTmZi3Bifd4GsdfchizRk9GrYVtmy0/VgU5m3P10BMcMVxGWvk2ht7fNAQVYzqVgv64EtAFfY2xUqYCxEh83d/mePIhV6J8q+wXbjMgJvZxyhINtjCzCvfuDfQ2bkAxY4zN1L5eZnPnokhgUvLlDybo06f40= 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+63373+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 1595901199363588.8441379575095; Mon, 27 Jul 2020 18:53:19 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id rqUkYY1788612xqRMTfQTckt; Mon, 27 Jul 2020 18:53:19 -0700 X-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 X-Received: by mail-pg1-f196.google.com with SMTP id e8so10989262pgc.5 for ; Mon, 27 Jul 2020 18:53:17 -0700 (PDT) X-Gm-Message-State: Ll7N1AkGsNwudRyQ3JpXpvCex1787277AA= 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) X-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: [edk2-devel] [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> In-Reply-To: <20200728015312.1023-1-matthewfcarlson@gmail.com> References: <20200728015312.1023-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=1595901199; bh=u6lS8Ryr6Mh0D45I+BpcFYYpj2mOo/Jc+MWTm1b/2+M=; h=Cc:Date:From:Reply-To:Subject:To; b=Z1E49V/IiK6uVhzLSf5sF2O5Y/S8Xb2dM4lHLUsb3zOqIUDV9X6culgANqdraR/leX9 6Bw2MwsD5sorfzRjHFW+rWP7ek4meGZJ/5csW3vbgVQSsQA/x8L1MZ6U8txnyRKLKxrJa 680lPnGDsxalueZ8K1qQcK2UKYQsnJFuBgA= 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/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 + 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/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 +# 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 BaseRngLib + MODULE_UNI_FILE =3D BaseRngLib.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] + TimerRng.c + +[Packages] + MdePkg/MdePkg.dec + +[LibraryClasses] + BaseLib + DebugLib + TimerLib 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 MdePkg/Library/BaseExtractGuidedSectionLib/BaseExtractGuidedSectionLib.i= nf =20 + MdePkg/Library/TimerRngLib/TimerRngLib.inf + MdePkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDriverEntryPoint= .inf MdePkg/Library/StandaloneMmServicesTableLib/StandaloneMmServicesTableLib= .inf =20 --=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 (#63373): https://edk2.groups.io/g/devel/message/63373 Mute This Topic: https://groups.io/mt/75836598/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-