From nobody Mon Feb 9 12:10:31 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+64036+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+64036+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1597198894; cv=none; d=zohomail.com; s=zohoarc; b=SToOMWVjcWYmQGgoVPbbopV8azSGviTO1TKEVrI65gWym1NHNRLbrx4yG6v9lPPyFDuNKUOBg1hGg5Yoz47w7NniFFHwSxwh0yKzuVfAEe6oVfutqYAXIZRg7fw0FTAsBGKpC4CDa6uB+MERdDHJFVUHPHnCsR0ER1Hd+cdFpP8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1597198894; 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=y+oXGPkiGqNoUeTAqhVhFjgAuZugIplTmcSnmI7tvMY=; b=AbpnjADhr+l5/K+br5WysRGnGrKd/57GkyHr1PMBbYo7Ef+yjgWYvasRXCiLFLpkkGuEKY0zYae6FiO/mVfPDWp7WLjf7AV0ywT5TXvfuNKhMn8cUqM2auEAuoYCFYj6KOxuPBtQxX5JQHnovuEKcZgMrHrL8347lr4lebUjkWA= 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+64036+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 1597198894880643.3731097063688; Tue, 11 Aug 2020 19:21:34 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id 4yhsYY1788612xtQrZ7WhaPF; Tue, 11 Aug 2020 19:21:34 -0700 X-Received: from mail-pg1-f175.google.com (mail-pg1-f175.google.com [209.85.215.175]) by mx.groups.io with SMTP id smtpd.web11.7009.1597198892703645031 for ; Tue, 11 Aug 2020 19:21:32 -0700 X-Received: by mail-pg1-f175.google.com with SMTP id m34so236697pgl.11 for ; Tue, 11 Aug 2020 19:21:32 -0700 (PDT) X-Gm-Message-State: RF4wwSA3iw4OBM75mxLXutHNx1787277AA= X-Google-Smtp-Source: ABdhPJyb/s+xDQuLOMWsgY3V0Bm+AqjO2F0wNRQepojZJIYqQ0Zc83/X+KiwjDdhQedtdTZMTDaqrA== X-Received: by 2002:a63:29c8:: with SMTP id p191mr3309980pgp.333.1597198892041; Tue, 11 Aug 2020 19:21:32 -0700 (PDT) X-Received: from tvis-name-05.localdomain ([50.34.40.129]) by smtp.gmail.com with ESMTPSA id q17sm415952pfh.32.2020.08.11.19.21.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Aug 2020 19:21:31 -0700 (PDT) From: "Matthew Carlson" To: devel@edk2.groups.io Cc: Michael D Kinney , Liming Gao , Zhiguang Liu , Matthew Carlson Subject: [edk2-devel] [PATCH v5 2/5] MdePkg: BaseRngLibDxe: Add RngLib that uses RngDxe Date: Tue, 11 Aug 2020 19:21:24 -0700 Message-Id: <20200812022127.1205-3-matthewfcarlson@gmail.com> In-Reply-To: <20200812022127.1205-1-matthewfcarlson@gmail.com> References: <20200812022127.1205-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=1597198894; bh=46++WJi0sIkBlvS9uPZHFv1uF12hYBjPZ2b6ElYEAg0=; h=Cc:Date:From:Reply-To:Subject:To; b=RZMshvEcx+0YvPcu5GDxS5KY/jc4DlYx83Yyc78S6lgDKf47u/tqd7qd8jyqFi+KzzY 14MQC0QsSxcWGcHQIhwfwApLBOs5mKWDUuWdeyM5w1n6YMnDHY3rHUIFiIWCOzXluMzpX GQB5ARsDRcVRSRk0OQxJ9n/rT5rvNDUrmN0= 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: Michael D Kinney Cc: Liming Gao Cc: Zhiguang Liu Signed-off-by: Matthew Carlson --- MdePkg/Library/BaseRngLibDxe/RngDxeLib.c | 200 ++++++++++++++++++++ MdePkg/Library/BaseRngLibDxe/BaseRngLibDxe.inf | 38 ++++ MdePkg/MdePkg.dsc | 4 +- 3 files changed, 241 insertions(+), 1 deletion(-) diff --git a/MdePkg/Library/BaseRngLibDxe/RngDxeLib.c b/MdePkg/Library/Base= RngLibDxe/RngDxeLib.c new file mode 100644 index 000000000000..8ee29329de13 --- /dev/null +++ b/MdePkg/Library/BaseRngLibDxe/RngDxeLib.c @@ -0,0 +1,200 @@ +/** @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. + + Arguments: + + Buffer -- Buffer to receive the random number. + BufferSize -- Number of bytes in Buffer. + +Return Value: + + EFI_SUCCESS or underlying failure code. + +**/ +EFI_STATUS +EFIAPI +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 **)&Rng= Protocol); + 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, &gEfiRngAlgorithmSp80090Ctr2= 56Guid, BufferSize, Buffer); + DEBUG((DEBUG_INFO, "[%a] GetRNG algorithm CTR-256 - Status =3D %r\n", __= FUNCTION__, Status)); + if(!EFI_ERROR(Status)) { + return Status; + } + + Status =3D RngProtocol->GetRNG(RngProtocol, &gEfiRngAlgorithmSp80090Hmac= 256Guid, 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, &gEfiRngAlgorithmSp80090Hash= 256Guid, 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/BaseRngLibDxe/BaseRngLibDxe.inf b/MdePkg/Librar= y/BaseRngLibDxe/BaseRngLibDxe.inf new file mode 100644 index 000000000000..819a106b1376 --- /dev/null +++ b/MdePkg/Library/BaseRngLibDxe/BaseRngLibDxe.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 +# +# MU_CHANGE: New file +## + +[Defines] + INF_VERSION =3D 0x00010017 + BASE_NAME =3D BaseRngLibDxe + 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] + RngDxeLib.c + +[LibraryClasses] + DebugLib + UefiBootServicesTableLib + +[Protocols] + gEfiRngProtocolGuid ## CONSUMES + +[Depex] + gEfiRngProtocolGuid + +[Guids] + gEfiRngAlgorithmSp80090Ctr256Guid ## CONSUMES + gEfiRngAlgorithmSp80090Hash256Guid ## CONSUMES + gEfiRngAlgorithmSp80090Hmac256Guid ## CONSUMES diff --git a/MdePkg/MdePkg.dsc b/MdePkg/MdePkg.dsc index d7ba3a730909..837a0047400e 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/BaseRngLibDxe/BaseRngLibDxe.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.27.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 (#64036): https://edk2.groups.io/g/devel/message/64036 Mute This Topic: https://groups.io/mt/76140304/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-