[PATCH v7 07/19] MdePkg/TrngLib: Add NULL instance of TRNG Library


PierreGondois
 

From: Sami Mujawar <sami.mujawar@...>

Bugzilla: 3668 (https://bugzilla.tianocore.org/show_bug.cgi?id=3D3668)

The True Random Number Generator (TRNG) library defines an
interface to access the entropy source on a platform. On
platforms that do not have access to an entropy source, a
NULL instance of the TRNG library may be useful to satisfy
the build dependency.

Therefore, add a NULL instance of the TRNG library.

Signed-off-by: Pierre Gondois <pierre.gondois@...>
---
.../Library/BaseTrngLibNull/BaseTrngLibNull.c | 117 ++++++++++++++++++
.../BaseTrngLibNull/BaseTrngLibNull.inf | 30 +++++
.../BaseTrngLibNull/BaseTrngLibNull.uni | 12 ++
MdePkg/MdePkg.dsc | 1 +
4 files changed, 160 insertions(+)
create mode 100644 MdePkg/Library/BaseTrngLibNull/BaseTrngLibNull.c
create mode 100644 MdePkg/Library/BaseTrngLibNull/BaseTrngLibNull.inf
create mode 100644 MdePkg/Library/BaseTrngLibNull/BaseTrngLibNull.uni

diff --git a/MdePkg/Library/BaseTrngLibNull/BaseTrngLibNull.c b/MdePkg/Li=
brary/BaseTrngLibNull/BaseTrngLibNull.c
new file mode 100644
index 000000000000..60774b33dd58
--- /dev/null
+++ b/MdePkg/Library/BaseTrngLibNull/BaseTrngLibNull.c
@@ -0,0 +1,117 @@
+/** @file
+ Null version of TRNG (True Random Number Generator) services.
+
+ Copyright (c) 2021 - 2022, Arm Limited. All rights reserved.<BR>
+
+ SPDX-License-Identifier: BSD-2-Clause-Patent
+
+ @par Reference(s):
+ - [1] NIST Special Publication 800-90B, Recommendation for the Entropy
+ Sources Used for Random Bit Generation.
+ (https://csrc.nist.gov/publications/detail/sp/800-90b/final)
+
+ @par Glossary:
+ - TRNG - True Random Number Generator
+**/
+
+#include <Library/DebugLib.h>
+#include <Library/TrngLib.h>
+
+/** Get the version of the TRNG backend.
+
+ A TRNG may be implemented by the system firmware, in which case this
+ function shall return the version of the TRNG backend.
+ The implementation must return NOT_SUPPORTED if a Back end is not pres=
ent.
+
+ @param [out] MajorRevision Major revision.
+ @param [out] MinorRevision Minor revision.
+
+ @retval RETURN_SUCCESS The function completed successfully=
.
+ @retval RETURN_INVALID_PARAMETER Invalid parameter.
+ @retval RETURN_UNSUPPORTED Backend not present.
+**/
+RETURN_STATUS
+EFIAPI
+GetTrngVersion (
+ OUT UINT16 *MajorRevision,
+ OUT UINT16 *MinorRevision
+ )
+{
+ ASSERT (FALSE);
+ return RETURN_UNSUPPORTED;
+}
+
+/** Get the UUID of the TRNG backend.
+
+ A TRNG may be implemented by the system firmware, in which case this
+ function shall return the UUID of the TRNG backend.
+ Returning the TRNG UUID is optional and if not implemented, RETURN_UNS=
UPPORTED
+ shall be returned.
+
+ Note: The caller must not rely on the returned UUID as a trustworthy T=
RNG
+ Back end identity
+
+ @param [out] Guid UUID of the TRNG backend.
+
+ @retval RETURN_SUCCESS The function completed successfully=
.
+ @retval RETURN_INVALID_PARAMETER Invalid parameter.
+ @retval RETURN_UNSUPPORTED Function not implemented.
+**/
+RETURN_STATUS
+EFIAPI
+GetTrngUuid (
+ OUT GUID *Guid
+ )
+{
+ ASSERT (FALSE);
+ return RETURN_UNSUPPORTED;
+}
+
+/** Returns maximum number of entropy bits that can be returned in a sin=
gle
+ call.
+
+ @return Returns the maximum number of Entropy bits that can be returne=
d
+ in a single call to GetTrngEntropy().
+**/
+UINTN
+EFIAPI
+GetTrngMaxSupportedEntropyBits (
+ VOID
+ )
+{
+ ASSERT (FALSE);
+ return 0;
+}
+
+/** Returns N bits of conditioned entropy.
+
+ See [1] Section 2.3.1 GetEntropy: An Interface to the Entropy Source
+ GetEntropy
+ Input:
+ bits_of_entropy: the requested amount of entropy
+ Output:
+ entropy_bitstring: The string that provides the requested entrop=
y.
+ status: A Boolean value that is TRUE if the request has been satis=
fied,
+ and is FALSE otherwise.
+
+ @param [in] EntropyBits Number of entropy bits requested.
+ @param [in] BufferSize Size of the Buffer in bytes.
+ @param [out] Buffer Buffer to return the entropy bits.
+
+ @retval RETURN_SUCCESS The function completed successfully=
.
+ @retval RETURN_INVALID_PARAMETER Invalid parameter.
+ @retval RETURN_UNSUPPORTED Function not implemented.
+ @retval RETURN_BAD_BUFFER_SIZE Buffer size is too small.
+ @retval RETURN_NOT_READY No Entropy available.
+**/
+RETURN_STATUS
+EFIAPI
+GetTrngEntropy (
+ IN UINTN EntropyBits,
+ IN UINTN BufferSize,
+ OUT UINT8 *Buffer
+ )
+{
+ ASSERT (FALSE);
+ return RETURN_UNSUPPORTED;
+}
diff --git a/MdePkg/Library/BaseTrngLibNull/BaseTrngLibNull.inf b/MdePkg/=
Library/BaseTrngLibNull/BaseTrngLibNull.inf
new file mode 100644
index 000000000000..54a4fd3db8cd
--- /dev/null
+++ b/MdePkg/Library/BaseTrngLibNull/BaseTrngLibNull.inf
@@ -0,0 +1,30 @@
+## @file
+# Null instance of TRNG (True Random Number Generator) Library.
+#
+# Copyright (c) 2021 - 2022, Arm Limited. All rights reserved.<BR>
+#
+# SPDX-License-Identifier: BSD-2-Clause-Patent
+#
+##
+
+[Defines]
+ INF_VERSION =3D 1.29
+ BASE_NAME =3D BaseTrngLibNull
+ MODULE_UNI_FILE =3D BaseTrngLibNull.uni
+ FILE_GUID =3D ABDE1C87-4F50-4B82-9133-7A79E13F69A=
B
+ MODULE_TYPE =3D BASE
+ VERSION_STRING =3D 1.0
+ LIBRARY_CLASS =3D TrngLib
+
+#
+# VALID_ARCHITECTURES =3D IA32 X64 ARM AARCH64 RISCV64
+#
+
+[Sources]
+ BaseTrngLibNull.c
+
+[Packages]
+ MdePkg/MdePkg.dec
+
+[LibraryClasses]
+ DebugLib
diff --git a/MdePkg/Library/BaseTrngLibNull/BaseTrngLibNull.uni b/MdePkg/=
Library/BaseTrngLibNull/BaseTrngLibNull.uni
new file mode 100644
index 000000000000..952e4354c0cc
--- /dev/null
+++ b/MdePkg/Library/BaseTrngLibNull/BaseTrngLibNull.uni
@@ -0,0 +1,12 @@
+// /** @file
+// Null Instance of TRNG (True Random Number Generator) Library.
+//
+// Copyright (c) 2021 - 2022, Arm Limited. All rights reserved.<BR>
+//
+// SPDX-License-Identifier: BSD-2-Clause-Patent
+//
+// **/
+
+#string STR_MODULE_ABSTRACT #language en-US "Null instance o=
f TRNG Library"
+
+#string STR_MODULE_DESCRIPTION #language en-US "This library in=
stance should be used with modules that inherit an (indirect) dependency =
on the TrngLib class, but never actually call TrngLib APIs for consuming =
Entropy."
diff --git a/MdePkg/MdePkg.dsc b/MdePkg/MdePkg.dsc
index cc1ac196a931..1a3a3c3243f5 100644
--- a/MdePkg/MdePkg.dsc
+++ b/MdePkg/MdePkg.dsc
@@ -67,6 +67,7 @@ [Components]
MdePkg/Library/DxeRngLib/DxeRngLib.inf
MdePkg/Library/BaseRngLibNull/BaseRngLibNull.inf
MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.inf
+ MdePkg/Library/BaseTrngLibNull/BaseTrngLibNull.inf
=20
MdePkg/Library/BaseSerialPortLibNull/BaseSerialPortLibNull.inf
MdePkg/Library/BaseSynchronizationLib/BaseSynchronizationLib.inf
--=20
2.25.1