From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-1.mimecast.com (us-smtp-1.mimecast.com [205.139.110.120]) by mx.groups.io with SMTP id smtpd.web12.2750.1573730170111352371 for ; Thu, 14 Nov 2019 03:16:10 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=IHw33FFS; spf=pass (domain: redhat.com, ip: 205.139.110.120, mailfrom: lersek@redhat.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1573730169; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Yuxbyel3yiHHxOM1UKbPGMo7UGMJluHnkLDaweEZ1os=; b=IHw33FFSNBKGrPV3S8hFptwoGQcNcz3XpWvnPyEY73E9RREB+qUMLKtmGCBGFFf/eef9oD /uf6ooAmVifPKVa28sd3M8WRBdXBLOxuag6jPQqneBinh+0zVjtqjxi2plFZpR5k/sjBKb yQ/Nrp+L0LsntzvBMv1H7gxVhiwXLtQ= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-96-w2tg4RzfM1O_eDKIUTHM_w-1; Thu, 14 Nov 2019 06:16:05 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 21839189DB01; Thu, 14 Nov 2019 11:16:03 +0000 (UTC) Received: from lacos-laptop-7.usersys.redhat.com (ovpn-117-80.ams2.redhat.com [10.36.117.80]) by smtp.corp.redhat.com (Postfix) with ESMTP id 572C760BFB; Thu, 14 Nov 2019 11:16:00 +0000 (UTC) Subject: Re: [edk2-devel] [PATCH 06/11] SecurityPkg/DxeRngLibRngProtocol: add RNG protocol version of RngLib To: devel@edk2.groups.io, jian.j.wang@intel.com Cc: Jiewen Yao , Chao Zhang , Ard Biesheuvel , Matthew Carlson , Sean Brogan , Bret Barkelew , Liming Gao , Ray Ni References: <20191114021743.3876-1-jian.j.wang@intel.com> <20191114021743.3876-7-jian.j.wang@intel.com> From: "Laszlo Ersek" Message-ID: <014e7809-4c92-e61f-b6a8-4a08151bbcdd@redhat.com> Date: Thu, 14 Nov 2019 12:15:59 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 In-Reply-To: <20191114021743.3876-7-jian.j.wang@intel.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-MC-Unique: w2tg4RzfM1O_eDKIUTHM_w-1 X-Mimecast-Spam-Score: 0 Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On 11/14/19 03:17, Wang, Jian J wrote: > From: Matthew Carlson >=20 > 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. I'd like to slightly correct the UEFI spec reference. In UEFI-2.8, section "37.5.1 EFI RNG Algorithm Definitions" writes, [...] The algorithms listed are optional, not meant to be exhaustive and may be augmented by vendors or other industry standards. [...] Meaning that an EFI_RNG_PROTOCOL instance is not "required", per spec, to support any one of the - gEfiRngAlgorithmSp80090Ctr256Guid - gEfiRngAlgorithmSp80090Hmac256Guid - gEfiRngAlgorithmSp80090Hash256Guid algorithms. It's of course fine to require one of those algorithms, as a policy decision, in this particular library instance -- but, as far as I can tell, that decision does not follow from the spec. Thanks Laszlo >=20 > 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/DxeRngLibRngProtoco= l/DxeRngLibRngProtocol.c > create mode 100644 SecurityPkg/RandomNumberGenerator/DxeRngLibRngProtoco= l/DxeRngLibRngProtocol.inf > create mode 100644 SecurityPkg/RandomNumberGenerator/DxeRngLibRngProtoco= l/DxeRngLibRngProtocol.uni >=20 > diff --git a/SecurityPkg/RandomNumberGenerator/DxeRngLibRngProtocol/DxeRn= gLibRngProtocol.c b/SecurityPkg/RandomNumberGenerator/DxeRngLibRngProtocol/= DxeRngLibRngProtocol.c > new file mode 100644 > index 0000000000..8ce4a7050d > --- /dev/null > +++ b/SecurityPkg/RandomNumberGenerator/DxeRngLibRngProtocol/DxeRngLibRng= Protocol.c > @@ -0,0 +1,200 @@ > +/** @file > + Provides an implementation of the library class RngLib that uses the Rn= g 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. C= alled too soon.\n", __FUNCTION__)); > + return EFI_LOAD_ERROR; > + } > + > + Status =3D gBS->LocateProtocol (&gEfiRngProtocolGuid, NULL, (VOID **)&= RngProtocol); > + 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, &gEfiRngAlgorithmSp80090C= tr256Guid, 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, &gEfiRngAlgorithmSp80090H= mac256Guid, 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, &gEfiRngAlgorithmSp80090H= ash256Guid, 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/DxeRn= gLibRngProtocol.inf b/SecurityPkg/RandomNumberGenerator/DxeRngLibRngProtoco= l/DxeRngLibRngProtocol.inf > new file mode 100644 > index 0000000000..d47fe3be53 > --- /dev/null > +++ b/SecurityPkg/RandomNumberGenerator/DxeRngLibRngProtocol/DxeRngLibRng= Protocol.inf > @@ -0,0 +1,42 @@ > +# @file > +# Provides implementation of the library class RngLib that uses the RngP= rotocol > +# > +# @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_DRIVE= R 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/DxeRn= gLibRngProtocol.uni b/SecurityPkg/RandomNumberGenerator/DxeRngLibRngProtoco= l/DxeRngLibRngProtocol.uni > new file mode 100644 > index 0000000000..09af056bd3 > --- /dev/null > +++ b/SecurityPkg/RandomNumberGenerator/DxeRngLibRngProtocol/DxeRngLibRng= Protocol.uni > @@ -0,0 +1,14 @@ > +// /** @file > +// Instance of RNG (Random Number Generator) Library Based on EFI_RNG_PR= OTOCOL. > +// > +// 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= Library Based on EFI_RNG_PROTOCOL." > + > +#string STR_MODULE_DESCRIPTION #language en-US "This version of= RNG library makes use of EFI_RNG_PROTOCOL to generate random number compli= ant with NIST 800-9A." > + >=20