From nobody Mon Feb 9 05:41:41 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+64467+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+64467+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1597865840; cv=none; d=zohomail.com; s=zohoarc; b=GyW4IPBVPOA+EQ/8cm8Vx9m/OUEfm1rL+jPl11oFTETDrEHSngFyg/DgT1+y57YlkSGXiHJSGDQNBm3xf1/QjzREBRkMm1Wndb8qSf4L3RYOx3KP93f8nCIx3izLJetZ6iYyBvGKRrBZ63Wj3e3qgwIx4eVz3n9l+ZzVpG2dmtk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1597865840; 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=ty+xQOiww+i5VH9fe7ADYCjh1jfdYu77Mbqe8nzAlQY=; b=AaN6mwE8ULKj5BbeUIV5OBAhbqwmkPnYR1FihF5GoNokbw5QfoBOrIPHPfP0z7BYjlBBhH7/EfIaEIdOHElTA5GPEPcciXPkWBGwGmuu3Ni5CGWMBh83LRfiRXu0W3Nxc6aUr0u2xzTuH5UmTSUjwmEopn6vZO8fTvKKKET44sI= 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+64467+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 1597865840868745.5861422926102; Wed, 19 Aug 2020 12:37:20 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id VuwtYY1788612xx4bKI2Uwh3; Wed, 19 Aug 2020 12:37:20 -0700 X-Received: from mail-pf1-f177.google.com (mail-pf1-f177.google.com [209.85.210.177]) by mx.groups.io with SMTP id smtpd.web10.96295.1597865838045145614 for ; Wed, 19 Aug 2020 12:37:18 -0700 X-Received: by mail-pf1-f177.google.com with SMTP id y206so12173004pfb.10 for ; Wed, 19 Aug 2020 12:37:18 -0700 (PDT) X-Gm-Message-State: 8o5EyDp5JDRBdQjjLFljgr5rx1787277AA= X-Google-Smtp-Source: ABdhPJxt93rMRHTY2C88crNk6AHsjUh6CfzqZ6LNFiiCrTD/nti7H2ifXda+GDV2WWu8rN1g7+fl+w== X-Received: by 2002:aa7:838d:: with SMTP id u13mr1539293pfm.158.1597865837335; Wed, 19 Aug 2020 12:37:17 -0700 (PDT) X-Received: from tvis-name-05.localdomain ([50.34.40.129]) by smtp.gmail.com with ESMTPSA id u191sm24480265pgu.56.2020.08.19.12.37.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Aug 2020 12:37:16 -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 v8 2/5] MdePkg: BaseRngLibDxe: Add RngLib that uses RngDxe Date: Wed, 19 Aug 2020 12:37:09 -0700 Message-Id: <20200819193712.1629-3-matthewfcarlson@gmail.com> In-Reply-To: <20200819193712.1629-1-matthewfcarlson@gmail.com> References: <20200819193712.1629-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=1597865840; bh=rTBpVlr0oNOnVSi1sTS2fMuQfvXQwNzhDg1q16wVHKk=; h=Cc:Date:From:Reply-To:Subject:To; b=SBzlCi91UW5URId3u7B/EG6clZHJpRDHVzNdHCovA4WVJVnx9eLwtBN9ldjZHJIM0fz EiZnmxQXSdi4UhjOSD+CZNI5R9Fg+PqMeMacK4pt4WCKsHsNY1y9EiZoZUfAlUMxcpAwI 991po0kyXAwQpoKKIAH9xjvt9zAoUT2RfT8= 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 | 206 ++++++++++++++++++++ MdePkg/Library/DxeRngLib/DxeRngLib.inf | 38 ++++ MdePkg/Library/DxeRngLib/DxeRngLib.uni | 15 ++ MdePkg/MdePkg.dsc | 4 +- 4 files changed, 262 insertions(+), 1 deletion(-) diff --git a/MdePkg/Library/DxeRngLib/DxeRngLib.c b/MdePkg/Library/DxeRngLi= b/DxeRngLib.c new file mode 100644 index 000000000000..0bd6585357b5 --- /dev/null +++ b/MdePkg/Library/DxeRngLib/DxeRngLib.c @@ -0,0 +1,206 @@ +/** @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. + +**/ +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..f4838aa05b7e --- /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 0x00010017 + 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 (#64467): https://edk2.groups.io/g/devel/message/64467 Mute This Topic: https://groups.io/mt/76294213/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-