From nobody Tue Nov 26 16:18:39 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of groups.io designates 66.175.222.12 as permitted sender) client-ip=66.175.222.12; envelope-from=bounce+27952+50612+1787277+3901457@groups.io; helo=web01.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zoho.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+50612+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1573697880; cv=none; d=zoho.com; s=zohoarc; b=nCyUlQ6cx4U4AeED7XjG/ALir4z1BswtDrW4QkkWl4MT6sqmTOuQnxOrw2RquGZtehUXY9eMV1atcdv5KOoWNcxkUdi8aCSdLyebbkq9nLYZiCK6qfHZkRJI90uh6BuoFVv8hkhlTzlyPb5/BEmzVqL6zdQSmVlzouXUi4gU7n4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1573697880; h=Cc:Date:From:In-Reply-To:List-Id:List-Unsubscribe:Message-ID:Reply-To:References:Sender:Subject:To; bh=pjSogBCds4KTrcqL8qEHduFS+c1QBaZnhy8o2XtNXFI=; b=RGYrztU3zPciHlBpvXU+/qLOtUmAEmg3WOjXPxFSh477DEeKx/tTV8PBXRdunkri+T9KumDKDD7Abszc4+jvERKjjuQZHGKZ8849tSeR0ZpL7klU5Q27hRzkOFopdIHdDzX35nMpLDALgwqHVd77PdLa1xXOAki0SsvrFHLErAY= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=pass; spf=pass (zoho.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+50612+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 1573697880158871.6153204575626; Wed, 13 Nov 2019 18:18:00 -0800 (PST) Return-Path: X-Received: by 127.0.0.2 with SMTP id 5Z5UYY1788612xr5sycQZ4UF; Wed, 13 Nov 2019 18:17:57 -0800 X-Received: from mga07.intel.com (mga07.intel.com []) by mx.groups.io with SMTP id smtpd.web10.830.1573697867801385708 for ; Wed, 13 Nov 2019 18:17:56 -0800 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga105.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 13 Nov 2019 18:17:56 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.68,302,1569308400"; d="scan'208";a="235500541" X-Received: from shwdeopenpsi777.ccr.corp.intel.com ([10.239.158.59]) by fmsmga002.fm.intel.com with ESMTP; 13 Nov 2019 18:17:54 -0800 From: "Wang, Jian J" To: devel@edk2.groups.io Cc: Jiewen Yao , Chao Zhang , Laszlo Ersek , Ard Biesheuvel , Matthew Carlson , Sean Brogan , Bret Barkelew , Liming Gao , Ray Ni Subject: [edk2-devel] [PATCH 06/11] SecurityPkg/DxeRngLibRngProtocol: add RNG protocol version of RngLib Date: Thu, 14 Nov 2019 10:17:38 +0800 Message-Id: <20191114021743.3876-7-jian.j.wang@intel.com> In-Reply-To: <20191114021743.3876-1-jian.j.wang@intel.com> References: <20191114021743.3876-1-jian.j.wang@intel.com> 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,jian.j.wang@intel.com X-Gm-Message-State: ZJPzw0HM2bCvxwTvhnw7yFByx1787277AA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1573697877; bh=eUrZ2UxMLefc+ekOwpjhrWGBRpapM1G2Pqng8SBY7FY=; h=Cc:Date:From:Reply-To:Subject:To; b=p/OgDeUY4JzXcKNYQSwQuIsDz5IjxO8i2yv56i7p2LyheraLG6d9FUZVVZLIILtjsGY rEQHUyAYR4/ocJ3tzbQgYhQg1YWozXl1k42oXotcrx4ppULBMTzMLqmU0dtYCp7dhzak+ ssUC4Z5vrgWZT/cszx1NfvYP4lApHs6+ZCk= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Matthew Carlson This version of RngLib makes use of EFI_RNG_PROTOCOL to provide random number. According to UEFI spec, EFI_RNG_PROTOCOL should meet NIST SP 800-90 and/or ANSI X9.31 standards. Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=3D1871 Cc: Jiewen Yao Cc: Chao Zhang Cc: Laszlo Ersek Cc: Ard Biesheuvel Cc: Matthew Carlson Cc: Sean Brogan Cc: Bret Barkelew Cc: Liming Gao Cc: Ray Ni Signed-off-by: Jian J Wang --- .../DxeRngLibRngProtocol.c | 200 ++++++++++++++++++ .../DxeRngLibRngProtocol.inf | 42 ++++ .../DxeRngLibRngProtocol.uni | 14 ++ 3 files changed, 256 insertions(+) create mode 100644 SecurityPkg/RandomNumberGenerator/DxeRngLibRngProtocol/= DxeRngLibRngProtocol.c create mode 100644 SecurityPkg/RandomNumberGenerator/DxeRngLibRngProtocol/= DxeRngLibRngProtocol.inf create mode 100644 SecurityPkg/RandomNumberGenerator/DxeRngLibRngProtocol/= DxeRngLibRngProtocol.uni diff --git a/SecurityPkg/RandomNumberGenerator/DxeRngLibRngProtocol/DxeRngL= ibRngProtocol.c b/SecurityPkg/RandomNumberGenerator/DxeRngLibRngProtocol/Dx= eRngLibRngProtocol.c new file mode 100644 index 0000000000..8ce4a7050d --- /dev/null +++ b/SecurityPkg/RandomNumberGenerator/DxeRngLibRngProtocol/DxeRngLibRngPr= otocol.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. +Copyright (c) 2019, Intel Corporation. All rights reserved. +SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ +#include +#include +#include +#include +#include + +/** + 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 Random data generated successfully. + @retval Others Failed to generate the random number. + +**/ +STATIC +EFI_STATUS +GenerateRandomNumberViaNist800Algorithm( + OUT UINT8* Buffer, + IN UINTN BufferSize + ) +{ + EFI_STATUS Status; + EFI_RNG_PROTOCOL* RngProtocol; + + Status =3D EFI_UNSUPPORTED; + RngProtocol =3D NULL; + + if (Buffer =3D=3D NULL) { + DEBUG ((DEBUG_ERROR, "[%a] Buffer =3D=3D NULL.\n", __FUNCTION__)); + return EFI_INVALID_PARAMETER; + } + + if (gBS =3D=3D NULL) { + DEBUG ((DEBUG_ERROR, "[%a] GenerateRandomNumber, gBS =3D=3D NULL. Cal= led too soon.\n", __FUNCTION__)); + return EFI_LOAD_ERROR; + } + + 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_VERBOSE, "[%a] GetRNG algorithm CTR-256 - Status =3D %r\n"= , __FUNCTION__, Status)); + if (!EFI_ERROR (Status)) { + return Status; + } + + Status =3D RngProtocol->GetRNG (RngProtocol, &gEfiRngAlgorithmSp80090Hma= c256Guid, BufferSize, Buffer); + DEBUG ((DEBUG_VERBOSE, "[%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_VERBOSE, "[%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; +} + + +/** + 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/SecurityPkg/RandomNumberGenerator/DxeRngLibRngProtocol/DxeRngL= ibRngProtocol.inf b/SecurityPkg/RandomNumberGenerator/DxeRngLibRngProtocol/= DxeRngLibRngProtocol.inf new file mode 100644 index 0000000000..d47fe3be53 --- /dev/null +++ b/SecurityPkg/RandomNumberGenerator/DxeRngLibRngProtocol/DxeRngLibRngPr= otocol.inf @@ -0,0 +1,42 @@ +# @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 0x00010029 + BASE_NAME =3D DxeRngLibRngProtocol + MODULE_UNI_FILE =3D DxeRngLibRngProtocol.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 DXE_RUNTIME_DRIVER DXE_SMM_DRIVER = UEFI_APPLICATION UEFI_DRIVER + +# +# VALID_ARCHITECTURES =3D IA32 X64 EBC ARM AARCH64 +# + +[Packages] + MdePkg/MdePkg.dec + +[Sources] + DxeRngLibRngProtocol.c + +[LibraryClasses] + DebugLib + UefiBootServicesTableLib + +[Protocols] + gEfiRngProtocolGuid ## CONSUMES + +[Depex] + gEfiRngProtocolGuid + +[Guids] + gEfiRngAlgorithmSp80090Ctr256Guid ## CONSUMES + gEfiRngAlgorithmSp80090Hash256Guid ## CONSUMES + gEfiRngAlgorithmSp80090Hmac256Guid ## CONSUMES diff --git a/SecurityPkg/RandomNumberGenerator/DxeRngLibRngProtocol/DxeRngL= ibRngProtocol.uni b/SecurityPkg/RandomNumberGenerator/DxeRngLibRngProtocol/= DxeRngLibRngProtocol.uni new file mode 100644 index 0000000000..09af056bd3 --- /dev/null +++ b/SecurityPkg/RandomNumberGenerator/DxeRngLibRngProtocol/DxeRngLibRngPr= otocol.uni @@ -0,0 +1,14 @@ +// /** @file +// Instance of RNG (Random Number Generator) Library Based on EFI_RNG_PROT= OCOL. +// +// Copyright (c) 2019, Intel Corporation. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + + +#string STR_MODULE_ABSTRACT #language en-US "Instance of RNG L= ibrary Based on EFI_RNG_PROTOCOL." + +#string STR_MODULE_DESCRIPTION #language en-US "This version of R= NG library makes use of EFI_RNG_PROTOCOL to generate random number complian= t with NIST 800-9A." + --=20 2.17.1.windows.2 -=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 (#50612): https://edk2.groups.io/g/devel/message/50612 Mute This Topic: https://groups.io/mt/56714115/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-