[PATCH v3 1/3] CryptoPkg: OpensslLib: Use RngLib to generate entropy in rand_pool


Matthew Carlson
 

From: Matthew Carlson <macarl@...>

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 <jiewen.yao@...>
Cc: Jian J Wang <jian.j.wang@...>
Cc: Xiaoyu Lu <xiaoyux.lu@...>
Signed-off-by: Matthew Carlson <matthewfcarlson@...>
---
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..b3ff03b2aa13 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 <openssl/aes.h>=0D
=0D
#include <Uefi.h>=0D
-#include <Library/TimerLib.h>=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 <Library/RngLib.h>=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.<BR>=0D
-SPDX-License-Identifier: BSD-2-Clause-Patent=0D
-=0D
-**/=0D
-=0D
-#include <Library/BaseLib.h>=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.<BR>=0D
-SPDX-License-Identifier: BSD-2-Clause-Patent=0D
-=0D
-**/=0D
-=0D
-#include <Library/BaseLib.h>=0D
-#include <Library/DebugLib.h>=0D
-#include <Library/TimerLib.h>=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.<BR>=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 <Uefi/UefiBaseType.h>=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

Join devel@edk2.groups.io to automatically receive all group messages.