From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail.byosoft.com.cn (mail.byosoft.com.cn [58.240.74.243]) by mx.groups.io with SMTP id smtpd.web10.9385.1598491740217234786 for ; Wed, 26 Aug 2020 18:29:01 -0700 Authentication-Results: mx.groups.io; dkim=missing; spf=none, err=permanent DNS error (domain: byosoft.com.cn, ip: 58.240.74.243, mailfrom: gaoliming@byosoft.com.cn) Received: from DESKTOPS6D0PVI ([58.246.60.130]) (envelope-sender ) by 192.168.6.13 with ESMTP for ; Thu, 27 Aug 2020 09:28:53 +0800 X-WM-Sender: gaoliming@byosoft.com.cn X-WM-AuthFlag: YES X-WM-AuthUser: gaoliming@byosoft.com.cn From: "gaoliming" To: , Cc: "'Ard Biesheuvel'" , "'Michael D Kinney'" , "'Liming Gao'" , "'Zhiguang Liu'" References: <20200826205501.1124-1-matthewfcarlson@gmail.com> <20200826205501.1124-3-matthewfcarlson@gmail.com> In-Reply-To: <20200826205501.1124-3-matthewfcarlson@gmail.com> Subject: =?UTF-8?B?5Zue5aSNOiBbZWRrMi1kZXZlbF0gW1BBVENIIHY5IDIvNV0gTWRlUGtnOiBCYXNlUm5nTGliRHhlOiBBZGQgUm5nTGliIHRoYXQgdXNlcyBSbmdEeGU=?= Date: Thu, 27 Aug 2020 09:28:55 +0800 Message-ID: <002f01d67c11$6e54d570$4afe8050$@byosoft.com.cn> MIME-Version: 1.0 X-Mailer: Microsoft Outlook 16.0 Thread-Index: AQHyfPnFr5/2IqzimbBvrQYaUqkljwGQdnqIqQakwEA= Content-Type: text/plain; charset="gb2312" Content-Transfer-Encoding: quoted-printable Content-Language: zh-cn Matthew: > -----=D3=CA=BC=FE=D4=AD=BC=FE----- > =B7=A2=BC=FE=C8=CB: bounce+27952+64654+4905953+8761045@groups.io > =B4=FA=B1=ED Matthew > Carlson > =B7=A2=CB=CD=CA=B1=BC=E4: 2020=C4=EA8=D4=C227=C8=D5 4:55 > =CA=D5=BC=FE=C8=CB: devel@edk2.groups.io > =B3=AD=CB=CD: Ard Biesheuvel ; Michael D = Kinney > ; Liming Gao ; > Zhiguang Liu ; Matthew Carlson > > =D6=F7=CC=E2: [edk2-devel] [PATCH v9 2/5] MdePkg: BaseRngLibDxe: Add = RngLib that > uses RngDxe >=20 > From: Matthew Carlson >=20 > This adds a RngLib that uses the RngProtocol to provide randomness. > This means that the RngLib is meant to be used with DXE_DRIVERS. >=20 > Ref: https://github.com/tianocore/edk2/pull/845 > Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=3D1871 >=20 > 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(-) >=20 > diff --git a/MdePkg/Library/DxeRngLib/DxeRngLib.c > b/MdePkg/Library/DxeRngLib/DxeRngLib.c > new file mode 100644 > index 000000000000..8d4e05e52d57 > --- /dev/null > +++ b/MdePkg/Library/DxeRngLib/DxeRngLib.c > @@ -0,0 +1,199 @@ > +/** @file >=20 > + Provides an implementation of the library class RngLib that uses the = Rng > protocol. >=20 > + >=20 > + Copyright (c) Microsoft Corporation. All rights reserved. >=20 > + SPDX-License-Identifier: BSD-2-Clause-Patent >=20 > + >=20 > +**/ >=20 > +#include >=20 > +#include >=20 > +#include >=20 > +#include >=20 > +#include >=20 > + >=20 > +/** >=20 > + Routine Description: >=20 > + >=20 > + Generates a random number via the NIST >=20 > + 800-9A algorithm. Refer to >=20 > + http://csrc.nist.gov/groups/STM/cavp/documents/drbg/DRBGVS.pdf >=20 > + for more information. >=20 > + >=20 > + @param[out] Buffer Buffer to receive the random number. >=20 > + @param[in] BufferSize Number of bytes in Buffer. >=20 > + >=20 > + @retval EFI_SUCCESS or underlying failure code. >=20 > +**/ >=20 > +STATIC >=20 > +EFI_STATUS >=20 > +GenerateRandomNumberViaNist800Algorithm ( >=20 > + OUT UINT8 *Buffer, >=20 > + IN UINTN BufferSize >=20 > + ) >=20 > +{ >=20 > + EFI_STATUS Status; >=20 > + EFI_RNG_PROTOCOL *RngProtocol; >=20 > + >=20 > + RngProtocol =3D NULL; >=20 > + >=20 > + if (Buffer =3D=3D NULL) { >=20 > + DEBUG((DEBUG_ERROR, "%a: Buffer =3D=3D NULL.\n", > __FUNCTION__)); >=20 > + return EFI_INVALID_PARAMETER; >=20 > + } >=20 > + >=20 > + Status =3D gBS->LocateProtocol (&gEfiRngProtocolGuid, NULL, (VOID > **)&RngProtocol); >=20 > + if (EFI_ERROR (Status) || RngProtocol =3D=3D NULL) { >=20 > + DEBUG((DEBUG_ERROR, "%a: Could not locate RNG prototocol, > Status =3D %r\n", __FUNCTION__, Status)); >=20 > + return Status; >=20 > + } >=20 > + >=20 > + Status =3D RngProtocol->GetRNG (RngProtocol, > &gEfiRngAlgorithmSp80090Ctr256Guid, BufferSize, Buffer); >=20 > + DEBUG((DEBUG_INFO, "%a: GetRNG algorithm CTR-256 - Status =3D = %r\n", > __FUNCTION__, Status)); >=20 > + if (!EFI_ERROR (Status)) { >=20 > + return Status; >=20 > + } >=20 > + >=20 > + Status =3D RngProtocol->GetRNG (RngProtocol, > &gEfiRngAlgorithmSp80090Hmac256Guid, BufferSize, Buffer); >=20 > + DEBUG((DEBUG_INFO, "%a: GetRNG algorithm HMAC-256 - Status > =3D %r\n", __FUNCTION__, Status)); >=20 > + if (!EFI_ERROR (Status)) { >=20 > + return Status; >=20 > + } >=20 > + >=20 > + Status =3D RngProtocol->GetRNG (RngProtocol, > &gEfiRngAlgorithmSp80090Hash256Guid, BufferSize, Buffer); >=20 > + DEBUG((DEBUG_INFO, "%a: GetRNG algorithm Hash-256 - Status =3D = %r\n", > __FUNCTION__, Status)); >=20 > + if (!EFI_ERROR (Status)) { >=20 > + return Status; >=20 > + } >=20 > + // If all the other methods have failed, use the default method = from the > RngProtocol >=20 > + Status =3D RngProtocol->GetRNG (RngProtocol, NULL, BufferSize, = Buffer); >=20 > + DEBUG((DEBUG_INFO, "%a: GetRNG algorithm Hash-256 - Status =3D = %r\n", > __FUNCTION__, Status)); >=20 > + if (!EFI_ERROR (Status)) { >=20 > + return Status; >=20 > + } >=20 > + // If we get to this point, we have failed >=20 > + DEBUG((DEBUG_ERROR, "%a: GetRNG() failed, staus =3D %r\n", > __FUNCTION__, Status)); >=20 > + >=20 > + return Status; >=20 > +}// GenerateRandomNumberViaNist800Algorithm() >=20 > + >=20 > + >=20 > +/** >=20 > + Generates a 16-bit random number. >=20 > + >=20 > + if Rand is NULL, return FALSE. >=20 > + >=20 > + @param[out] Rand Buffer pointer to store the 16-bit random = value. >=20 > + >=20 > + @retval TRUE Random number generated successfully. >=20 > + @retval FALSE Failed to generate the random number. >=20 > + >=20 > +**/ >=20 > +BOOLEAN >=20 > +EFIAPI >=20 > +GetRandomNumber16 ( >=20 > + OUT UINT16 *Rand >=20 > + ) >=20 > +{ >=20 > + EFI_STATUS Status; >=20 > + >=20 > + if (Rand =3D=3D NULL) >=20 > + { >=20 > + return FALSE; >=20 > + } >=20 > + >=20 > + Status =3D GenerateRandomNumberViaNist800Algorithm ((UINT8 *)Rand, > 2); Here, how about use sizeof (UINT16) to replace hardcode 2? It will be meaningful.=20 The same comments are for the following 4, 8, 16. 16 is 2 * sizeof = (UINT64). Thanks Liming >=20 > + if (EFI_ERROR (Status)) { >=20 > + return FALSE; >=20 > + } >=20 > + return TRUE; >=20 > +} >=20 > + >=20 > +/** >=20 > + Generates a 32-bit random number. >=20 > + >=20 > + if Rand is NULL, return FALSE. >=20 > + >=20 > + @param[out] Rand Buffer pointer to store the 32-bit random = value. >=20 > + >=20 > + @retval TRUE Random number generated successfully. >=20 > + @retval FALSE Failed to generate the random number. >=20 > + >=20 > +**/ >=20 > +BOOLEAN >=20 > +EFIAPI >=20 > +GetRandomNumber32 ( >=20 > + OUT UINT32 *Rand >=20 > + ) >=20 > +{ >=20 > + EFI_STATUS Status; >=20 > + >=20 > + if (Rand =3D=3D NULL) { >=20 > + return FALSE; >=20 > + } >=20 > + >=20 > + Status =3D GenerateRandomNumberViaNist800Algorithm ((UINT8*)Rand, > 4); >=20 > + if (EFI_ERROR (Status)) { >=20 > + return FALSE; >=20 > + } >=20 > + return TRUE; >=20 > +} >=20 > + >=20 > +/** >=20 > + Generates a 64-bit random number. >=20 > + >=20 > + if Rand is NULL, return FALSE. >=20 > + >=20 > + @param[out] Rand Buffer pointer to store the 64-bit random = value. >=20 > + >=20 > + @retval TRUE Random number generated successfully. >=20 > + @retval FALSE Failed to generate the random number. >=20 > + >=20 > +**/ >=20 > +BOOLEAN >=20 > +EFIAPI >=20 > +GetRandomNumber64 ( >=20 > + OUT UINT64 *Rand >=20 > + ) >=20 > +{ >=20 > + EFI_STATUS Status; >=20 > + >=20 > + if (Rand =3D=3D NULL) { >=20 > + return FALSE; >=20 > + } >=20 > + >=20 > + Status =3D GenerateRandomNumberViaNist800Algorithm ((UINT8*)Rand, > 8); >=20 > + if (EFI_ERROR (Status)) { >=20 > + return FALSE; >=20 > + } >=20 > + return TRUE; >=20 > +} >=20 > + >=20 > +/** >=20 > + Generates a 128-bit random number. >=20 > + >=20 > + if Rand is NULL, return FALSE. >=20 > + >=20 > + @param[out] Rand Buffer pointer to store the 128-bit random > value. >=20 > + >=20 > + @retval TRUE Random number generated successfully. >=20 > + @retval FALSE Failed to generate the random number. >=20 > + >=20 > +**/ >=20 > +BOOLEAN >=20 > +EFIAPI >=20 > +GetRandomNumber128 ( >=20 > + OUT UINT64 *Rand >=20 > + ) >=20 > +{ >=20 > + EFI_STATUS Status; >=20 > + >=20 > + if (Rand =3D=3D NULL) { >=20 > + return FALSE; >=20 > + } >=20 > + >=20 > + Status =3D GenerateRandomNumberViaNist800Algorithm ((UINT8*)Rand, > 16); >=20 > + if (EFI_ERROR (Status)) { >=20 > + return FALSE; >=20 > + } >=20 > + return TRUE; >=20 > +} >=20 > diff --git a/MdePkg/Library/DxeRngLib/DxeRngLib.inf > b/MdePkg/Library/DxeRngLib/DxeRngLib.inf > new file mode 100644 > index 000000000000..68554ad21146 > --- /dev/null > +++ b/MdePkg/Library/DxeRngLib/DxeRngLib.inf > @@ -0,0 +1,38 @@ > +# @file >=20 > +# Provides implementation of the library class RngLib that uses the > RngProtocol >=20 > +# >=20 > +# @copyright >=20 > +# Copyright (c) Microsoft Corporation. All rights reserved. >=20 > +# SPDX-License-Identifier: BSD-2-Clause-Patent >=20 > +# >=20 > +## >=20 > + >=20 > +[Defines] >=20 > + INF_VERSION =3D 1.27 >=20 > + BASE_NAME =3D DxeRngLib >=20 > + MODULE_UNI_FILE =3D DxeRngLib.uni >=20 > + FILE_GUID =3D FF9F84C5-A33E-44E3-9BB5-0D654B2D4149 >=20 > + MODULE_TYPE =3D DXE_DRIVER >=20 > + VERSION_STRING =3D 1.0 >=20 > + LIBRARY_CLASS =3D RngLib|DXE_DRIVER UEFI_APPLICATION > UEFI_DRIVER >=20 > + >=20 > +[Packages] >=20 > + MdePkg/MdePkg.dec >=20 > + >=20 > +[Sources] >=20 > + DxeRngLib.c >=20 > + >=20 > +[LibraryClasses] >=20 > + DebugLib >=20 > + UefiBootServicesTableLib >=20 > + >=20 > +[Protocols] >=20 > + gEfiRngProtocolGuid ## CONSUMES >=20 > + >=20 > +[Depex] >=20 > + gEfiRngProtocolGuid >=20 > + >=20 > +[Guids] >=20 > + gEfiRngAlgorithmSp80090Ctr256Guid >=20 > + gEfiRngAlgorithmSp80090Hash256Guid >=20 > + gEfiRngAlgorithmSp80090Hmac256Guid >=20 > diff --git a/MdePkg/Library/DxeRngLib/DxeRngLib.uni > b/MdePkg/Library/DxeRngLib/DxeRngLib.uni > new file mode 100644 > index 000000000000..c904e54b6fb0 > --- /dev/null > +++ b/MdePkg/Library/DxeRngLib/DxeRngLib.uni > @@ -0,0 +1,15 @@ > +// @file >=20 > +// Instance of RNG (Random Number Generator) Library. >=20 > +// >=20 > +// RngLib that uses the Rng Protocol to provide random numbers. >=20 > +// >=20 > +// Copyright (c) Microsoft Corporation. >=20 > +// >=20 > +// SPDX-License-Identifier: BSD-2-Clause-Patent >=20 > +// >=20 > + >=20 > + >=20 > +#string STR_MODULE_ABSTRACT #language en-US "Instance of RNG > Library" >=20 > + >=20 > +#string STR_MODULE_DESCRIPTION #language en-US "BaseRng Library > that uses the Rng Protocol to provide random numbers" >=20 > + >=20 > 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 >=20 > MdePkg/Library/BasePrintLib/BasePrintLib.inf >=20 >=20 > MdePkg/Library/BaseReportStatusCodeLibNull/BaseReportStatusCodeLibNull > .inf >=20 > - MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.inf >=20 > + MdePkg/Library/DxeRngLib/DxeRngLib.inf >=20 > MdePkg/Library/BaseRngLibNull/BaseRngLibNull.inf >=20 > + MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.inf >=20 > + >=20 > MdePkg/Library/BaseSerialPortLibNull/BaseSerialPortLibNull.inf >=20 > MdePkg/Library/BaseSynchronizationLib/BaseSynchronizationLib.inf >=20 >=20 > MdePkg/Library/BaseTimerLibNullTemplate/BaseTimerLibNullTemplate.inf >=20 > -- > 2.28.0.windows.1 >=20 >=20 > -=3D-=3D-=3D-=3D-=3D-=3D > Groups.io Links: You receive all messages sent to this group. >=20 > View/Reply Online (#64654): = https://edk2.groups.io/g/devel/message/64654 > Mute This Topic: https://groups.io/mt/76437902/4905953 > Group Owner: devel+owner@edk2.groups.io > Unsubscribe: https://edk2.groups.io/g/devel/unsub > [gaoliming@byosoft.com.cn] > -=3D-=3D-=3D-=3D-=3D-=3D