From nobody Sun Feb 8 20:59:51 2026 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+64654+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+64654+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1598475309; cv=none; d=zohomail.com; s=zohoarc; b=KtUNnULdZshwoVj7L9kk1cfMTasyBylc+/Rzd21DaFSWQfz0EfM9smcNInYK2nXh5RHOdjHYGauZ12crAiGTyCjrkMR62hmMqLadSYWOwzaKF/l6cZPOVPUYpmcS20mV2m7XZvussjTSMsoq+czqr8H+CmOvUTU3oJp4Q7gvsFw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1598475309; 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=6Nk7URqC3sOBfAmmSeSUo5zGdK8FZPYjbwkTdNfWpFg=; b=ePMpQNO6HQ38Ryx2G8tW3C84O2OnO1HuKzP/947ccfy5QE423j2C7OOjzfkN6sd2oieMPq6Gg79jl4+CHsi2MMPDrQMe02VWOiit44fpbOwgCYRYGFMqMA+A4wLBYn7cGsk0MKBxFWcRBE3H9e57xPjGI/1T0jJ+4sAIYvQSVbI= 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+64654+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 1598475309604517.9186190528502; Wed, 26 Aug 2020 13:55:09 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id UeZAYY1788612x3UALaqyFbt; Wed, 26 Aug 2020 13:55:09 -0700 X-Received: from mail-pj1-f49.google.com (mail-pj1-f49.google.com [209.85.216.49]) by mx.groups.io with SMTP id smtpd.web10.4549.1598475308665557739 for ; Wed, 26 Aug 2020 13:55:08 -0700 X-Received: by mail-pj1-f49.google.com with SMTP id s2so845228pjr.4 for ; Wed, 26 Aug 2020 13:55:08 -0700 (PDT) X-Gm-Message-State: kYgbsiCvHgMWQdmbPWVZ6dF7x1787277AA= X-Google-Smtp-Source: ABdhPJxsTSgYEV3Q4nfl9jDdDIscQOshQKmUyIwkOgL3NrqwoC4nDcymXQIeRK2U3Fpr+xQZefFymg== X-Received: by 2002:a17:90a:858a:: with SMTP id m10mr1627742pjn.185.1598475307676; Wed, 26 Aug 2020 13:55:07 -0700 (PDT) X-Received: from tvis-name-05.localdomain ([50.34.40.129]) by smtp.gmail.com with ESMTPSA id s67sm68284pfs.117.2020.08.26.13.55.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Aug 2020 13:55:07 -0700 (PDT) From: "Matthew Carlson" To: devel@edk2.groups.io Cc: Ard Biesheuvel , Michael D Kinney , Liming Gao , Zhiguang Liu , Matthew Carlson Subject: [edk2-devel] [PATCH v9 2/5] MdePkg: BaseRngLibDxe: Add RngLib that uses RngDxe Date: Wed, 26 Aug 2020 13:54:58 -0700 Message-Id: <20200826205501.1124-3-matthewfcarlson@gmail.com> In-Reply-To: <20200826205501.1124-1-matthewfcarlson@gmail.com> References: <20200826205501.1124-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=1598475309; bh=wyCX1BTNHXURGKJuEud3JKGiE8BBPj6VeLzoTuSgQQI=; h=Cc:Date:From:Reply-To:Subject:To; b=fAPG8UZIbtwXxt/dypFRNPcICh5pY2WNJjN3aBJdLtJbKv4/vcR/nu9yB7AT+k7SKnQ PNvYSrXnqQkwYcFpFEE/Xmfdmlzfyq230s1Yel0MDKXLpM9RoJ+rOi2RvRZZnenBX66zG 2WVJqIFbXyRWK7O4JcIxxezRnZCC9LOBXAc= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Type: text/plain; charset="utf-8" From: Matthew Carlson 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=3D1871 Cc: Ard Biesheuvel Cc: Michael D Kinney Cc: Liming Gao Cc: Zhiguang Liu Signed-off-by: Matthew Carlson --- MdePkg/Library/DxeRngLib/DxeRngLib.c | 199 ++++++++++++++++++++ MdePkg/Library/DxeRngLib/DxeRngLib.inf | 38 ++++ MdePkg/Library/DxeRngLib/DxeRngLib.uni | 15 ++ MdePkg/MdePkg.dsc | 4 +- 4 files changed, 255 insertions(+), 1 deletion(-) diff --git a/MdePkg/Library/DxeRngLib/DxeRngLib.c b/MdePkg/Library/DxeRngLi= b/DxeRngLib.c new file mode 100644 index 000000000000..8d4e05e52d57 --- /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 =3D NULL; + + if (Buffer =3D=3D NULL) { + DEBUG((DEBUG_ERROR, "%a: Buffer =3D=3D NULL.\n", __FUNCTION__)); + return EFI_INVALID_PARAMETER; + } + + Status =3D gBS->LocateProtocol (&gEfiRngProtocolGuid, NULL, (VOID **)&Rn= gProtocol); + if (EFI_ERROR (Status) || RngProtocol =3D=3D NULL) { + DEBUG((DEBUG_ERROR, "%a: Could not locate RNG prototocol, Status =3D= %r\n", __FUNCTION__, Status)); + return Status; + } + + Status =3D RngProtocol->GetRNG (RngProtocol, &gEfiRngAlgorithmSp80090Ctr= 256Guid, BufferSize, Buffer); + DEBUG((DEBUG_INFO, "%a: GetRNG algorithm CTR-256 - Status =3D %r\n", __F= UNCTION__, Status)); + if (!EFI_ERROR (Status)) { + return Status; + } + + Status =3D RngProtocol->GetRNG (RngProtocol, &gEfiRngAlgorithmSp80090Hma= c256Guid, BufferSize, Buffer); + DEBUG((DEBUG_INFO, "%a: GetRNG algorithm HMAC-256 - Status =3D %r\n", __= FUNCTION__, Status)); + if (!EFI_ERROR (Status)) { + return Status; + } + + Status =3D RngProtocol->GetRNG (RngProtocol, &gEfiRngAlgorithmSp80090Has= h256Guid, BufferSize, Buffer); + DEBUG((DEBUG_INFO, "%a: GetRNG algorithm Hash-256 - Status =3D %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 =3D RngProtocol->GetRNG (RngProtocol, NULL, BufferSize, Buffer); + DEBUG((DEBUG_INFO, "%a: GetRNG algorithm Hash-256 - Status =3D %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 =3D %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 =3D=3D NULL) + { + return FALSE; + } + + Status =3D GenerateRandomNumberViaNist800Algorithm ((UINT8 *)Rand, 2); + 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 =3D=3D NULL) { + return FALSE; + } + + Status =3D GenerateRandomNumberViaNist800Algorithm ((UINT8*)Rand, 4); + 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 =3D=3D NULL) { + return FALSE; + } + + Status =3D GenerateRandomNumberViaNist800Algorithm ((UINT8*)Rand, 8); + 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 =3D=3D NULL) { + return FALSE; + } + + Status =3D GenerateRandomNumberViaNist800Algorithm ((UINT8*)Rand, 16); + if (EFI_ERROR (Status)) { + return FALSE; + } + return TRUE; +} diff --git a/MdePkg/Library/DxeRngLib/DxeRngLib.inf b/MdePkg/Library/DxeRng= Lib/DxeRngLib.inf new file mode 100644 index 000000000000..68554ad21146 --- /dev/null +++ b/MdePkg/Library/DxeRngLib/DxeRngLib.inf @@ -0,0 +1,38 @@ +# @file +# Provides implementation of the library class RngLib that uses the RngPro= tocol +# +# @copyright +# Copyright (c) Microsoft Corporation. All rights reserved. +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION =3D 1.27 + BASE_NAME =3D DxeRngLib + MODULE_UNI_FILE =3D DxeRngLib.uni + FILE_GUID =3D FF9F84C5-A33E-44E3-9BB5-0D654B2D4149 + MODULE_TYPE =3D DXE_DRIVER + VERSION_STRING =3D 1.0 + LIBRARY_CLASS =3D RngLib|DXE_DRIVER UEFI_APPLICATION UEFI_DRIVER + +[Packages] + MdePkg/MdePkg.dec + +[Sources] + DxeRngLib.c + +[LibraryClasses] + DebugLib + UefiBootServicesTableLib + +[Protocols] + gEfiRngProtocolGuid ## CONSUMES + +[Depex] + gEfiRngProtocolGuid + +[Guids] + gEfiRngAlgorithmSp80090Ctr256Guid + gEfiRngAlgorithmSp80090Hash256Guid + gEfiRngAlgorithmSp80090Hmac256Guid diff --git a/MdePkg/Library/DxeRngLib/DxeRngLib.uni b/MdePkg/Library/DxeRng= Lib/DxeRngLib.uni new file mode 100644 index 000000000000..c904e54b6fb0 --- /dev/null +++ b/MdePkg/Library/DxeRngLib/DxeRngLib.uni @@ -0,0 +1,15 @@ +// @file +// Instance of RNG (Random Number Generator) Library. +// +// RngLib that uses the Rng Protocol to provide random numbers. +// +// Copyright (c) Microsoft Corporation. +// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// + + +#string STR_MODULE_ABSTRACT #language en-US "Instance of RNG Library" + +#string STR_MODULE_DESCRIPTION #language en-US "BaseRng Library that uses= the Rng Protocol to provide random numbers" + diff --git a/MdePkg/MdePkg.dsc b/MdePkg/MdePkg.dsc index d7ba3a730909..2c3b7966b086 100644 --- a/MdePkg/MdePkg.dsc +++ b/MdePkg/MdePkg.dsc @@ -62,8 +62,10 @@ MdePkg/Library/BasePostCodeLibPort80/BasePostCodeLibPort80.inf MdePkg/Library/BasePrintLib/BasePrintLib.inf MdePkg/Library/BaseReportStatusCodeLibNull/BaseReportStatusCodeLibNull.i= nf - MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.inf + MdePkg/Library/DxeRngLib/DxeRngLib.inf MdePkg/Library/BaseRngLibNull/BaseRngLibNull.inf + MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.inf + MdePkg/Library/BaseSerialPortLibNull/BaseSerialPortLibNull.inf MdePkg/Library/BaseSynchronizationLib/BaseSynchronizationLib.inf MdePkg/Library/BaseTimerLibNullTemplate/BaseTimerLibNullTemplate.inf --=20 2.28.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 (#64654): https://edk2.groups.io/g/devel/message/64654 Mute This Topic: https://groups.io/mt/76437902/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-