Skip to content

CryptoPkg: Use RngLib to get Random number. #11198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

kanagavels97
Copy link
Contributor

REF: #11002

Use RngLib to get the Random number instead of passing the Default seed when the seed value is NULL and Seed size is zero.

Description

Add an implementation to use the RngLib to get the RandomNumber instead of passing the predictable default seed value when the RandomSeed() is called with seed value NULL and Seed size zero.

REF: tianocore#11002

Use RngLib to get the Random number instead of passing the Default seed
when the seed value is NULL and Seed size is zero.

Signed-off-by: Kanagavel S <kanagavels@ami.com>
@kanagavels97 kanagavels97 marked this pull request as ready for review June 17, 2025 04:57
@pierregondois
Copy link
Contributor

The patch looks good to me. As a side comment, shouldn't we have a valid implementation of RandomSeed() in CryptoPkg/Library/BaseCryptLibMbedTls/Rand ?

BOOLEAN
EFIAPI
RandomSeed (
  IN  CONST  UINT8  *Seed  OPTIONAL,
  IN  UINTN         SeedSize
  )
{
  return TRUE;
}

@kanagavels97
Copy link
Contributor Author

The patch looks good to me. As a side comment, shouldn't we have a valid implementation of RandomSeed() in CryptoPkg/Library/BaseCryptLibMbedTls/Rand ?

BOOLEAN
EFIAPI
RandomSeed (
  IN  CONST  UINT8  *Seed  OPTIONAL,
  IN  UINTN         SeedSize
  )
{
  return TRUE;
}

Do you want the same implementation for MbedTls library as well?

@pierregondois
Copy link
Contributor

The patch looks good to me. As a side comment, shouldn't we have a valid implementation of RandomSeed() in CryptoPkg/Library/BaseCryptLibMbedTls/Rand ?

BOOLEAN
EFIAPI
RandomSeed (
  IN  CONST  UINT8  *Seed  OPTIONAL,
  IN  UINTN         SeedSize
  )
{
  return TRUE;
}

Do you want the same implementation for MbedTls library as well?

It would be a good think IMO.
Another thing is that adding a dependency of CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf over the RngLib might break some platform builds. For instance:
Platform/Sophgo/SG2042_EVB_Board/SG2042.dsc
will break.

Note that the build seems already failing. I had to to the following to see the unresolved dependency:

diff --git a/Platform/Sophgo/SG2042_EVB_Board/SG2042.dsc b/Platform/Sophgo/SG2042_EVB_Board/SG2042.dsc
index 5f15e3a621a1..03dd084fdb9b 100644
--- a/Platform/Sophgo/SG2042_EVB_Board/SG2042.dsc
+++ b/Platform/Sophgo/SG2042_EVB_Board/SG2042.dsc
@@ -29,7 +29,7 @@
   # Defines for default states.  These can be changed on the command line.
   # -D FLAG=VALUE
   #
-  DEFINE SECURE_BOOT_ENABLE      = FALSE
+  DEFINE SECURE_BOOT_ENABLE      = TRUE
   DEFINE DEBUG_ON_SERIAL_PORT    = TRUE
 
   #
@@ -99,7 +99,7 @@
   SortLib|MdeModulePkg/Library/BaseSortLib/BaseSortLib.inf
   ShellLib|ShellPkg/Library/UefiShellLib/UefiShellLib.inf
   UefiBootManagerLib|MdeModulePkg/Library/UefiBootManagerLib/UefiBootManagerLib.inf
-  FdtLib|EmbeddedPkg/Library/FdtLib/FdtLib.inf
+  FdtLib|MdePkg/Library/BaseFdtLib/BaseFdtLib.inf
   VariableFlashInfoLib|MdeModulePkg/Library/BaseVariableFlashInfoLib/BaseVariableFlashInfoLib.inf
   VariablePolicyHelperLib|MdeModulePkg/Library/VariablePolicyHelperLib/VariablePolicyHelperLib.inf
   ImagePropertiesRecordLib|MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLib.inf
@@ -153,7 +153,7 @@
   RealTimeClockLib|EmbeddedPkg//Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.inf
 
   # Flattened Device Tree (FDT) access library
-  FdtLib|EmbeddedPkg/Library/FdtLib/FdtLib.inf
+  FdtLib|MdePkg/Library/BaseFdtLib/BaseFdtLib.inf
 
 [LibraryClasses.common.SEC]
 !ifdef $(DEBUG_ON_SERIAL_PORT)

and the command:
build -p Platform/Sophgo/SG2042_EVB_Board/SG2042.dsc -t GCC5 -a RISCV64

@@ -47,17 +48,12 @@ RandomSeed (
if (Seed != NULL) {
RAND_seed (Seed, (UINT32)SeedSize);
} else {
//
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we don't need to keep two files with exactly the same content. CryptRanTsc.c can be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@liyi77 You mean we can use CryptRand.c file for IA32, X64, ARM, and AARCH64?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@liyi77 You mean we can use CryptRand.c file for IA32, X64, ARM, and AARCH64?

Yes, they are same things anyway

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok @liyi77 , I will remove CryptRandTsc.c file

@liyi77
Copy link
Contributor

liyi77 commented Jun 20, 2025

It would be a good think IMO. Another thing is that adding a dependency of CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf over the RngLib might break some platform builds. For instance: Platform/Sophgo/SG2042_EVB_Board/SG2042.dsc will break.

Sounds strange, RngLib is a dependency of OpensslLib:

[LibraryClasses]
BaseLib
DebugLib
RngLib

Generally as long as you use Openssl CryptoLib, this dependency is already satisfied.

@pierregondois
Copy link
Contributor

Sounds strange, RngLib is a dependency of OpensslLib:

[LibraryClasses]
BaseLib
DebugLib
RngLib

Generally as long as you use Openssl CryptoLib, this dependency is already satisfied.

Yes right, the RngLib dependency is already broken for the platform.
While checking some other platforms, it seems that the RngLib is present for all of them. However I didn't try to build all of them.

@kanagavels97
Copy link
Contributor Author

Sounds strange, RngLib is a dependency of OpensslLib:

[LibraryClasses]
BaseLib
DebugLib
RngLib

Generally as long as you use Openssl CryptoLib, this dependency is already satisfied.

Yes right, the RngLib dependency is already broken for the platform. While checking some other platforms, it seems that the RngLib is present for all of them. However I didn't try to build all of them.

@pierregondois

Could you please confirm whether the RngLib dependency is still required for BaseCryptLib?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants