From ed0dce7d5466b6b22ff9e0923f3a3e885540bbfc Mon Sep 17 00:00:00 2001 From: Matthew Carlson Date: Fri, 31 Jul 2020 13:07:17 -0700 Subject: MdePkg: BaseRngLibDxe: Add RngLib that uses RngDxe This adds a RngLib that uses the RngProtocol to provide randomness. This means that the RngLib is meant to be used with DXE_DRIVERS. Ref: https://github.com/tianocore/edk2/pull/845 Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=1871 Cc: Ard Biesheuvel Cc: Michael D Kinney Cc: Liming Gao Cc: Zhiguang Liu Reviewed-by: Liming Gao Signed-off-by: Matthew Carlson --- MdePkg/Library/DxeRngLib/DxeRngLib.c | 199 +++++++++++++++++++++++++++++++++++ 1 file changed, 199 insertions(+) create mode 100644 MdePkg/Library/DxeRngLib/DxeRngLib.c (limited to 'MdePkg/Library/DxeRngLib/DxeRngLib.c') diff --git a/MdePkg/Library/DxeRngLib/DxeRngLib.c b/MdePkg/Library/DxeRngLib/DxeRngLib.c new file mode 100644 index 0000000000..9c3d67b5a6 --- /dev/null +++ b/MdePkg/Library/DxeRngLib/DxeRngLib.c @@ -0,0 +1,199 @@ +/** @file + Provides an implementation of the library class RngLib that uses the Rng protocol. + + Copyright (c) Microsoft Corporation. All rights reserved. + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ +#include +#include +#include +#include +#include + +/** + Routine Description: + + Generates a random number via the NIST + 800-9A algorithm. Refer to + http://csrc.nist.gov/groups/STM/cavp/documents/drbg/DRBGVS.pdf + for more information. + + @param[out] Buffer Buffer to receive the random number. + @param[in] BufferSize Number of bytes in Buffer. + + @retval EFI_SUCCESS or underlying failure code. +**/ +STATIC +EFI_STATUS +GenerateRandomNumberViaNist800Algorithm ( + OUT UINT8 *Buffer, + IN UINTN BufferSize + ) +{ + EFI_STATUS Status; + EFI_RNG_PROTOCOL *RngProtocol; + + RngProtocol = NULL; + + if (Buffer == NULL) { + DEBUG((DEBUG_ERROR, "%a: Buffer == NULL.\n", __FUNCTION__)); + return EFI_INVALID_PARAMETER; + } + + Status = gBS->LocateProtocol (&gEfiRngProtocolGuid, NULL, (VOID **)&RngProtocol); + if (EFI_ERROR (Status) || RngProtocol == NULL) { + DEBUG((DEBUG_ERROR, "%a: Could not locate RNG prototocol, Status = %r\n", __FUNCTION__, Status)); + return Status; + } + + Status = RngProtocol->GetRNG (RngProtocol, &gEfiRngAlgorithmSp80090Ctr256Guid, BufferSize, Buffer); + DEBUG((DEBUG_INFO, "%a: GetRNG algorithm CTR-256 - Status = %r\n", __FUNCTION__, Status)); + if (!EFI_ERROR (Status)) { + return Status; + } + + Status = RngProtocol->GetRNG (RngProtocol, &gEfiRngAlgorithmSp80090Hmac256Guid, BufferSize, Buffer); + DEBUG((DEBUG_INFO, "%a: GetRNG algorithm HMAC-256 - Status = %r\n", __FUNCTION__, Status)); + if (!EFI_ERROR (Status)) { + return Status; + } + + Status = RngProtocol->GetRNG (RngProtocol, &gEfiRngAlgorithmSp80090Hash256Guid, BufferSize, Buffer); + DEBUG((DEBUG_INFO, "%a: GetRNG algorithm Hash-256 - Status = %r\n", __FUNCTION__, Status)); + if (!EFI_ERROR (Status)) { + return Status; + } + // If all the other methods have failed, use the default method from the RngProtocol + Status = RngProtocol->GetRNG (RngProtocol, NULL, BufferSize, Buffer); + DEBUG((DEBUG_INFO, "%a: GetRNG algorithm Hash-256 - Status = %r\n", __FUNCTION__, Status)); + if (!EFI_ERROR (Status)) { + return Status; + } + // If we get to this point, we have failed + DEBUG((DEBUG_ERROR, "%a: GetRNG() failed, staus = %r\n", __FUNCTION__, Status)); + + return Status; +}// GenerateRandomNumberViaNist800Algorithm() + + +/** + Generates a 16-bit random number. + + if Rand is NULL, return FALSE. + + @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 + ) +{ + EFI_STATUS Status; + + if (Rand == NULL) + { + return FALSE; + } + + Status = GenerateRandomNumberViaNist800Algorithm ((UINT8 *)Rand, sizeof(UINT16)); + if (EFI_ERROR (Status)) { + return FALSE; + } + return TRUE; +} + +/** + Generates a 32-bit random number. + + if Rand is NULL, return FALSE. + + @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 + ) +{ + EFI_STATUS Status; + + if (Rand == NULL) { + return FALSE; + } + + Status = GenerateRandomNumberViaNist800Algorithm ((UINT8*)Rand, sizeof(UINT32)); + if (EFI_ERROR (Status)) { + return FALSE; + } + return TRUE; +} + +/** + Generates a 64-bit random number. + + if Rand is NULL, return FALSE. + + @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 + ) +{ + EFI_STATUS Status; + + if (Rand == NULL) { + return FALSE; + } + + Status = GenerateRandomNumberViaNist800Algorithm ((UINT8*)Rand, sizeof(UINT64)); + if (EFI_ERROR (Status)) { + return FALSE; + } + return TRUE; +} + +/** + Generates a 128-bit random number. + + if Rand is NULL, return FALSE. + + @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 + ) +{ + EFI_STATUS Status; + + if (Rand == NULL) { + return FALSE; + } + + Status = GenerateRandomNumberViaNist800Algorithm ((UINT8*)Rand, 2 * sizeof(UINT64)); + if (EFI_ERROR (Status)) { + return FALSE; + } + return TRUE; +} -- cgit v1.2.3