From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) by mx.groups.io with SMTP id smtpd.web10.3508.1596227237939594931 for ; Fri, 31 Jul 2020 13:27:18 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@gmail.com header.s=20161025 header.b=RGcmHvql; spf=pass (domain: gmail.com, ip: 209.85.214.170, mailfrom: matthewfcarlson@gmail.com) Received: by mail-pl1-f170.google.com with SMTP id u10so8456127plr.7 for ; Fri, 31 Jul 2020 13:27:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=AsX5qTQ67698YTTdja5bozq9JbS2pHMtUbX4rFYxY4Y=; b=RGcmHvqlFkpIA7LgdT9RmoabaL8F1fBYCKXNskjSb1aTn2Sl3A07RPHkqHnH2Jo/UU uo9S55CphnbKK6sBIPViEnx0BPZ0nYIldJ+cqSGfRI+QKZe9JiL9duP6PGJ6/aC8aIqM X64SWcpLY99p7BdPBvlLpfxH9vRY1DFP1f2B3iiCJdByinNw5ZyS7J4z3uEAS+re0ic7 GFztMthIPm94h91I4aeHgwAdjmpFx3rq1ysPDmw0XBsz3J0D24tiR+YvBzNcNAHGoPfQ 5GeKalp7bfzP8PpWWJ0hKBJyUU1PRcP4rQnaCy/k9GDVXr/2IGMRDRQvJrNX0k8PC/vQ jz0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=AsX5qTQ67698YTTdja5bozq9JbS2pHMtUbX4rFYxY4Y=; b=AFLxW8a+tf16p3x6mSKApkiZNwsr0cqr+Lp7jnr9WOnFKAxf4qbyIJJXHJhXOD9dw5 mBXDZ66XMN5lZOKHuC1c4z6rothb7A+DzT7td+JZmK9PZPQ1GMTJpKc5xHlrx8t520rE 3HTsUpI++4K+/Vqu3FCEKZ1GaSOSF09xy0JsoIy1Gzm0W4MFCJFs+VzXvd8gX+0kWbA0 OPhZxu+k8Gbdws4pNvCr5OCVp48VxsZRI7LXxXqQIHkfDAvYcWk/JL4/S+dxF7yMXhbS 541zniERV0MpBiuP0nrk/L06Z29SJoLxIvbIUmhIRavh9rIEJR+e1hnMxnPDWR7Vn40Y rS+w== X-Gm-Message-State: AOAM533T+SyrUDg8gY2HJdXI+jK16lRQqg7UPqaZR9bjaHkAmmPlYbDi FothVuTg4zTRpfodONxZd0RTaF79I0U= X-Google-Smtp-Source: ABdhPJy6A2kgam4zqdJISzE+bHN0Gk87n0sWoU0rnPPdrc059+y0tT2PI6OYk3wK+rQ7rPRhwQWKsQ== X-Received: by 2002:a17:902:a504:: with SMTP id s4mr4987278plq.177.1596227236992; Fri, 31 Jul 2020 13:27:16 -0700 (PDT) Return-Path: Received: from tvis-name-05.localdomain ([50.34.40.129]) by smtp.gmail.com with ESMTPSA id m26sm11098594pff.84.2020.07.31.13.27.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 31 Jul 2020 13:27:16 -0700 (PDT) From: "Matthew Carlson" To: devel@edk2.groups.io Cc: Michael D Kinney , Liming Gao , Zhiguang Liu , Matthew Carlson Subject: [PATCH v3 3/3] MdePkg: BaseRngLibDxe: Add RngLib that uses RngDxe Date: Fri, 31 Jul 2020 13:27:12 -0700 Message-Id: <20200731202712.1759-4-matthewfcarlson@gmail.com> X-Mailer: git-send-email 2.27.0.windows.1 In-Reply-To: <20200731202712.1759-1-matthewfcarlson@gmail.com> References: <20200731202712.1759-1-matthewfcarlson@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Matthew Carlson Cc: Michael D Kinney Cc: Liming Gao Cc: Zhiguang Liu Signed-off-by: Matthew Carlson --- MdePkg/Library/BaseRngLibDxe/RngDxeLib.c | 216 ++++++++++++++++++++ MdePkg/Library/BaseRngLibDxe/BaseRngLibDxe.inf | 38 ++++ MdePkg/MdePkg.dsc | 4 +- 3 files changed, 257 insertions(+), 1 deletion(-) diff --git a/MdePkg/Library/BaseRngLibDxe/RngDxeLib.c b/MdePkg/Library/Base= RngLibDxe/RngDxeLib.c new file mode 100644 index 000000000000..45bd6bad8f7a --- /dev/null +++ b/MdePkg/Library/BaseRngLibDxe/RngDxeLib.c @@ -0,0 +1,216 @@ +/** @file=0D + Provides an implementation of the library class RngLib that uses the Rng = protocol.=0D +=0D +Copyright (c) Microsoft Corporation. All rights reserved.=0D +SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +MU_CHANGE: New file=0D +=0D +**/=0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +=0D +/**=0D +Routine Description:=0D +=0D + Generates a random number via the NIST=0D + 800-9A algorithm. Refer to=0D + http://csrc.nist.gov/groups/STM/cavp/documents/drbg/DRBGVS.pdf=0D + for more information.=0D +=0D + Arguments:=0D +=0D + Buffer -- Buffer to receive the random number.=0D + BufferSize -- Number of bytes in Buffer.=0D +=0D +Return Value:=0D +=0D + EFI_SUCCESS or underlying failure code.=0D +=0D +**/=0D +EFI_STATUS=0D +EFIAPI=0D +GenerateRandomNumberViaNist800Algorithm(=0D + OUT UINT8* Buffer,=0D + IN UINTN BufferSize=0D + )=0D +{=0D + EFI_STATUS Status =3D EFI_SUCCESS;=0D + EFI_RNG_PROTOCOL* RngProtocol =3D NULL;=0D +=0D + if (Buffer =3D=3D NULL)=0D + {=0D + DEBUG((DEBUG_ERROR, "[%a] Buffer =3D=3D NULL.\n", __FUNCTION__));=0D + return EFI_INVALID_PARAMETER;=0D + }=0D +=0D + if (gBS =3D=3D NULL)=0D + {=0D + DEBUG((DEBUG_ERROR, "[%a] GenerateRandomNumber, gBS =3D=3D NULL. Ca= lled too soon.\n", __FUNCTION__));=0D + return EFI_LOAD_ERROR;=0D + }=0D +=0D + Status =3D gBS->LocateProtocol(&gEfiRngProtocolGuid, NULL, (VOID **)&Rng= Protocol);=0D + if (EFI_ERROR(Status) || RngProtocol =3D=3D NULL)=0D + {=0D + DEBUG((DEBUG_ERROR, "[%a] Could not locate RNG prototocol, Status = =3D %r\n", __FUNCTION__, Status));=0D + return Status;=0D + }=0D +=0D + Status =3D RngProtocol->GetRNG(RngProtocol, &gEfiRngAlgorithmSp80090Ctr2= 56Guid, BufferSize, Buffer);=0D + DEBUG((DEBUG_INFO, "[%a] GetRNG algorithm CTR-256 - Status =3D %r\n", __= FUNCTION__, Status));=0D + if(!EFI_ERROR(Status))=0D + {=0D + return Status;=0D + }=0D +=0D + Status =3D RngProtocol->GetRNG(RngProtocol, &gEfiRngAlgorithmSp80090Hmac= 256Guid, BufferSize, Buffer);=0D + DEBUG((DEBUG_INFO, "[%a] GetRNG algorithm HMAC-256 - Status =3D %r\n", _= _FUNCTION__, Status));=0D + if(!EFI_ERROR(Status))=0D + {=0D + return Status;=0D + }=0D +=0D + Status =3D RngProtocol->GetRNG(RngProtocol, &gEfiRngAlgorithmSp80090Hash= 256Guid, BufferSize, Buffer);=0D + DEBUG((DEBUG_INFO, "[%a] GetRNG algorithm Hash-256 - Status =3D %r\n", _= _FUNCTION__, Status));=0D + if(!EFI_ERROR(Status))=0D + {=0D + return Status;=0D + }=0D + // If we get to this point, we have failed=0D + DEBUG((DEBUG_ERROR, "[%a] GetRNG() failed, staus =3D %r\n", __FUNCTION__= , Status));=0D +=0D + return Status;=0D +}// GenerateRandomNumberViaNist800Algorithm()=0D +=0D +=0D +/**=0D + Generates a 16-bit random number.=0D +=0D + if Rand is NULL, return FALSE.=0D +=0D + @param[out] Rand Buffer pointer to store the 16-bit random value.=0D +=0D + @retval TRUE Random number generated successfully.=0D + @retval FALSE Failed to generate the random number.=0D +=0D +**/=0D +BOOLEAN=0D +EFIAPI=0D +GetRandomNumber16 (=0D + OUT UINT16 *Rand=0D + )=0D +{=0D + EFI_STATUS Status;=0D +=0D + if (Rand =3D=3D NULL)=0D + {=0D + return FALSE;=0D + }=0D +=0D + Status =3D GenerateRandomNumberViaNist800Algorithm ((UINT8 *)Rand, 2);=0D + if (EFI_ERROR(Status))=0D + {=0D + return FALSE;=0D + }=0D + return TRUE;=0D +}=0D +=0D +/**=0D + Generates a 32-bit random number.=0D +=0D + if Rand is NULL, return FALSE.=0D +=0D + @param[out] Rand Buffer pointer to store the 32-bit random value.=0D +=0D + @retval TRUE Random number generated successfully.=0D + @retval FALSE Failed to generate the random number.=0D +=0D +**/=0D +BOOLEAN=0D +EFIAPI=0D +GetRandomNumber32 (=0D + OUT UINT32 *Rand=0D + )=0D +{=0D + EFI_STATUS Status;=0D +=0D + if (Rand =3D=3D NULL)=0D + {=0D + return FALSE;=0D + }=0D +=0D + Status =3D GenerateRandomNumberViaNist800Algorithm ((UINT8 *)Rand, 4);=0D + if (EFI_ERROR(Status))=0D + {=0D + return FALSE;=0D + }=0D + return TRUE;=0D +}=0D +=0D +/**=0D + Generates a 64-bit random number.=0D +=0D + if Rand is NULL, return FALSE.=0D +=0D + @param[out] Rand Buffer pointer to store the 64-bit random value.=0D +=0D + @retval TRUE Random number generated successfully.=0D + @retval FALSE Failed to generate the random number.=0D +=0D +**/=0D +BOOLEAN=0D +EFIAPI=0D +GetRandomNumber64 (=0D + OUT UINT64 *Rand=0D + )=0D +{=0D + EFI_STATUS Status;=0D +=0D + if (Rand =3D=3D NULL)=0D + {=0D + return FALSE;=0D + }=0D +=0D + Status =3D GenerateRandomNumberViaNist800Algorithm ((UINT8 *)Rand, 8);=0D + if (EFI_ERROR(Status))=0D + {=0D + return FALSE;=0D + }=0D + return TRUE;=0D +}=0D +=0D +/**=0D + Generates a 128-bit random number.=0D +=0D + if Rand is NULL, return FALSE.=0D +=0D + @param[out] Rand Buffer pointer to store the 128-bit random value.=0D +=0D + @retval TRUE Random number generated successfully.=0D + @retval FALSE Failed to generate the random number.=0D +=0D +**/=0D +BOOLEAN=0D +EFIAPI=0D +GetRandomNumber128 (=0D + OUT UINT64 *Rand=0D + )=0D +{=0D + EFI_STATUS Status;=0D +=0D + if (Rand =3D=3D NULL)=0D + {=0D + return FALSE;=0D + }=0D +=0D + Status =3D GenerateRandomNumberViaNist800Algorithm ((UINT8 *)Rand, 16);= =0D + if (EFI_ERROR(Status))=0D + {=0D + return FALSE;=0D + }=0D + return TRUE;=0D +}=0D 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=0D +# Provides implementation of the library class RngLib that uses the RngPro= tocol=0D +#=0D +# @copyright=0D +# Copyright (c) Microsoft Corporation. All rights reserved.=0D +# SPDX-License-Identifier: BSD-2-Clause-Patent=0D +#=0D +# MU_CHANGE: New file=0D +##=0D +=0D +[Defines]=0D + INF_VERSION =3D 0x00010017=0D + BASE_NAME =3D BaseRngLibDxe=0D + FILE_GUID =3D FF9F84C5-A33E-44E3-9BB5-0D654B2D4149=0D + MODULE_TYPE =3D DXE_DRIVER=0D + VERSION_STRING =3D 1.0=0D + LIBRARY_CLASS =3D RngLib|DXE_DRIVER UEFI_APPLICATION UEFI_DRIVER=0D +=0D +[Packages]=0D + MdePkg/MdePkg.dec=0D +=0D +[Sources]=0D + RngDxeLib.c=0D +=0D +[LibraryClasses]=0D + DebugLib=0D + UefiBootServicesTableLib=0D +=0D +[Protocols]=0D + gEfiRngProtocolGuid ## CONSUMES=0D +=0D +[Depex]=0D + gEfiRngProtocolGuid=0D +=0D +[Guids]=0D + gEfiRngAlgorithmSp80090Ctr256Guid ## CONSUMES=0D + gEfiRngAlgorithmSp80090Hash256Guid ## CONSUMES=0D + gEfiRngAlgorithmSp80090Hmac256Guid ## CONSUMES=0D 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=0D MdePkg/Library/BasePrintLib/BasePrintLib.inf=0D MdePkg/Library/BaseReportStatusCodeLibNull/BaseReportStatusCodeLibNull.i= nf=0D - MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.inf=0D + MdePkg/Library/BaseRngLibDxe/BaseRngLibDxe.inf=0D MdePkg/Library/BaseRngLibNull/BaseRngLibNull.inf=0D + MdePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.inf=0D +=0D MdePkg/Library/BaseSerialPortLibNull/BaseSerialPortLibNull.inf=0D MdePkg/Library/BaseSynchronizationLib/BaseSynchronizationLib.inf=0D MdePkg/Library/BaseTimerLibNullTemplate/BaseTimerLibNullTemplate.inf=0D --=20 2.27.0.windows.1