From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail04.groups.io (mail04.groups.io [45.79.224.9]) by spool.mail.gandi.net (Postfix) with ESMTPS id 8CA1874003E for ; Tue, 16 Apr 2024 07:51:31 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=loDmGh5PpYk/e6084dvjsMuj+Z5MAdAsbZy6qVl4i48=; c=relaxed/simple; d=groups.io; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Resent-Date:Resent-From:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding; s=20240206; t=1713253890; v=1; b=DA6YOT9WSbwrCVTYHZRRherSlxl/HiTgN7m7q0GnTblsjzT+MnwbcN0KsOSfONP20+Lisxg9 uA3YS79scM4wutC3+Wt1LU7RDCo5rtTPtYHV1p6hrpruyl/DVm7ExvB//mb2m39NyxDoaGMAz9E QyNvsII1WyaGqcaWSpAFchdik7C9IqOto/aHDUWTcqauqU7a1LD8MCFwKqdMLXacjtcUx9iXs4y IjxfV6KX3p5AqwZvW5nNnDGLCF0SNa7WzOT4BMCxNIkQhu+MNTEFq5oBuhTIw3TESV+bNLuW9zy /QwuXmNDEJFD1/ei99W9KihlWE6JFxVnUrnBRxpspTdTQ== X-Received: by 127.0.0.2 with SMTP id R6HrYY7687511xA4HRDUlJe5; Tue, 16 Apr 2024 00:51:30 -0700 X-Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) by mx.groups.io with SMTP id smtpd.web10.15023.1713253889614547084 for ; Tue, 16 Apr 2024 00:51:29 -0700 X-CSE-ConnectionGUID: 99CiVriwTsuaYHnh1DYRFw== X-CSE-MsgGUID: Mc2RkIsBT7OW2ZG/REMF3A== X-IronPort-AV: E=McAfee;i="6600,9927,11045"; a="8537733" X-IronPort-AV: E=Sophos;i="6.07,205,1708416000"; d="scan'208";a="8537733" X-Received: from fmviesa008.fm.intel.com ([10.60.135.148]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Apr 2024 00:51:24 -0700 X-CSE-ConnectionGUID: k7Mzx+J2TQ6y0W1qrDTGQQ== X-CSE-MsgGUID: SVc8hx7/TTCptHW4GRgjCg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,205,1708416000"; d="scan'208";a="22261247" X-Received: from shwdejointd777.ccr.corp.intel.com ([10.239.58.116]) by fmviesa008.fm.intel.com with ESMTP; 16 Apr 2024 00:51:22 -0700 From: "Wenxing Hou" To: devel@edk2.groups.io Cc: Jiewen Yao , Yi Li Subject: [edk2-devel] [PATCH 2/9] CryptoPkg: Add rand function for BaseCryptLibMbedTls Date: Tue, 16 Apr 2024 15:51:11 +0800 Message-Id: <20240416075118.4799-3-wenxing.hou@intel.com> In-Reply-To: <20240416075118.4799-1-wenxing.hou@intel.com> References: <20240416075118.4799-1-wenxing.hou@intel.com> MIME-Version: 1.0 Precedence: Bulk List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Resent-Date: Tue, 16 Apr 2024 00:51:29 -0700 Resent-From: wenxing.hou@intel.com Reply-To: devel@edk2.groups.io,wenxing.hou@intel.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: Bk5sDKWbTRebl0HpBupcR9Yix7686176AA= Content-Transfer-Encoding: quoted-printable X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20240206 header.b=DA6YOT9W; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=intel.com (policy=none); spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 45.79.224.9 as permitted sender) smtp.mailfrom=bounce@groups.io REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3D4177 Add rand function for BaseCryptLibMbedTls. Cc: Jiewen Yao Cc: Yi Li Signed-off-by: Wenxing Hou --- CryptoPkg/CryptoPkg.ci.yaml | 1 + .../BaseCryptLibMbedTls/InternalCryptLib.h | 16 +++ .../BaseCryptLibMbedTls/Rand/CryptRand.c | 105 ++++++++++++++++++ .../BaseCryptLibMbedTls/Rand/CryptRandTsc.c | 105 ++++++++++++++++++ 4 files changed, 227 insertions(+) create mode 100644 CryptoPkg/Library/BaseCryptLibMbedTls/Rand/CryptRand.c create mode 100644 CryptoPkg/Library/BaseCryptLibMbedTls/Rand/CryptRandTsc= .c diff --git a/CryptoPkg/CryptoPkg.ci.yaml b/CryptoPkg/CryptoPkg.ci.yaml index b601bcf85c..2fbc021c84 100644 --- a/CryptoPkg/CryptoPkg.ci.yaml +++ b/CryptoPkg/CryptoPkg.ci.yaml @@ -45,6 +45,7 @@ "Library/Include/stdint.h",=0D "Library/Include/stubs-32.h",=0D "Library/Include/inttypes.h",=0D + "Library/BaseCryptLibMbedTls/InternalCryptLib.h",=0D # These directories contain auto-generated OpenSSL content=0D "Library/OpensslLib",=0D "Library/IntrinsicLib",=0D diff --git a/CryptoPkg/Library/BaseCryptLibMbedTls/InternalCryptLib.h b/Cry= ptoPkg/Library/BaseCryptLibMbedTls/InternalCryptLib.h index 039aa32028..e2c7e42ecb 100644 --- a/CryptoPkg/Library/BaseCryptLibMbedTls/InternalCryptLib.h +++ b/CryptoPkg/Library/BaseCryptLibMbedTls/InternalCryptLib.h @@ -22,4 +22,20 @@ SPDX-License-Identifier: BSD-2-Clause-Patent //=0D #include =0D =0D +/**=0D + The MbedTLS function f_rng, which MbedtlsRand implements.=0D +=0D + @param[in] rng_state Not used, just for compatibility with mbedlts.=0D + @param[out] output Pointer to buffer to receive random value.=0D + @param[in] len Size of random bytes to generate.=0D +=0D + @retval 0 Pseudorandom byte stream generated successfully.=0D + @retval Non-0 Pseudorandom number generator fails to generate due to la= ck of entropy.=0D +**/=0D +INTN=0D +MbedtlsRand (=0D + VOID *rng_state,=0D + UINT8 *output,=0D + UINTN len=0D + );=0D #endif=0D diff --git a/CryptoPkg/Library/BaseCryptLibMbedTls/Rand/CryptRand.c b/Crypt= oPkg/Library/BaseCryptLibMbedTls/Rand/CryptRand.c new file mode 100644 index 0000000000..081b413740 --- /dev/null +++ b/CryptoPkg/Library/BaseCryptLibMbedTls/Rand/CryptRand.c @@ -0,0 +1,105 @@ +/** @file=0D + Pseudorandom Number Generator Wrapper Implementation over MbedTLS.=0D +=0D +Copyright (c) 2024, Intel Corporation. All rights reserved.
=0D +SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#include "InternalCryptLib.h"=0D +#include =0D +=0D +/**=0D + Sets up the seed value for the pseudorandom number generator.=0D +=0D + This function sets up the seed value for the pseudorandom number generat= or.=0D + If Seed is not NULL, then the seed passed in is used.=0D + If Seed is NULL, then default seed is used.=0D +=0D + @param[in] Seed Pointer to seed value.=0D + If NULL, default seed is used.=0D + @param[in] SeedSize Size of seed value.=0D + If Seed is NULL, this parameter is ignored.=0D +=0D + @retval TRUE Pseudorandom number generator has enough entropy for rand= om generation.=0D + @retval FALSE Pseudorandom number generator does not have enough entrop= y for random generation.=0D +=0D +**/=0D +BOOLEAN=0D +EFIAPI=0D +RandomSeed (=0D + IN CONST UINT8 *Seed OPTIONAL,=0D + IN UINTN SeedSize=0D + )=0D +{=0D + return TRUE;=0D +}=0D +=0D +/**=0D + Generates a pseudorandom byte stream of the specified size.=0D +=0D + If Output is NULL, then return FALSE.=0D +=0D + @param[out] Output Pointer to buffer to receive random value.=0D + @param[in] Size Size of random bytes to generate.=0D +=0D + @retval TRUE Pseudorandom byte stream generated successfully.=0D + @retval FALSE Pseudorandom number generator fails to generate due to la= ck of entropy.=0D +=0D +**/=0D +BOOLEAN=0D +EFIAPI=0D +RandomBytes (=0D + OUT UINT8 *Output,=0D + IN UINTN Size=0D + )=0D +{=0D + BOOLEAN Ret;=0D + UINT64 TempRand;=0D +=0D + Ret =3D FALSE;=0D +=0D + while (Size > 0) {=0D + // Use RngLib to get random number=0D + Ret =3D GetRandomNumber64 (&TempRand);=0D +=0D + if (!Ret) {=0D + return Ret;=0D + }=0D +=0D + if (Size >=3D sizeof (TempRand)) {=0D + *((UINT64 *)Output) =3D TempRand;=0D + Output +=3D sizeof (UINT64);=0D + Size -=3D sizeof (TempRand);=0D + } else {=0D + CopyMem (Output, &TempRand, Size);=0D + Size =3D 0;=0D + }=0D + }=0D +=0D + return Ret;=0D +}=0D +=0D +/**=0D + The MbedTLS function f_rng, which MbedtlsRand implements.=0D +=0D + @param[in] rng_state Not used, just for compatibility with mbedlts.=0D + @param[out] output Pointer to buffer to receive random value.=0D + @param[in] len Size of random bytes to generate.=0D +=0D + @retval 0 Pseudorandom byte stream generated successfully.=0D + @retval Non-0 Pseudorandom number generator fails to generate due to la= ck of entropy.=0D +**/=0D +INTN=0D +MbedtlsRand (=0D + VOID *rng_state,=0D + UINT8 *output,=0D + UINTN len=0D + )=0D +{=0D + BOOLEAN Result;=0D +=0D + Result =3D RandomBytes (output, len);=0D +=0D + return Result ? 0 : -1;=0D +}=0D diff --git a/CryptoPkg/Library/BaseCryptLibMbedTls/Rand/CryptRandTsc.c b/Cr= yptoPkg/Library/BaseCryptLibMbedTls/Rand/CryptRandTsc.c new file mode 100644 index 0000000000..081b413740 --- /dev/null +++ b/CryptoPkg/Library/BaseCryptLibMbedTls/Rand/CryptRandTsc.c @@ -0,0 +1,105 @@ +/** @file=0D + Pseudorandom Number Generator Wrapper Implementation over MbedTLS.=0D +=0D +Copyright (c) 2024, Intel Corporation. All rights reserved.
=0D +SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#include "InternalCryptLib.h"=0D +#include =0D +=0D +/**=0D + Sets up the seed value for the pseudorandom number generator.=0D +=0D + This function sets up the seed value for the pseudorandom number generat= or.=0D + If Seed is not NULL, then the seed passed in is used.=0D + If Seed is NULL, then default seed is used.=0D +=0D + @param[in] Seed Pointer to seed value.=0D + If NULL, default seed is used.=0D + @param[in] SeedSize Size of seed value.=0D + If Seed is NULL, this parameter is ignored.=0D +=0D + @retval TRUE Pseudorandom number generator has enough entropy for rand= om generation.=0D + @retval FALSE Pseudorandom number generator does not have enough entrop= y for random generation.=0D +=0D +**/=0D +BOOLEAN=0D +EFIAPI=0D +RandomSeed (=0D + IN CONST UINT8 *Seed OPTIONAL,=0D + IN UINTN SeedSize=0D + )=0D +{=0D + return TRUE;=0D +}=0D +=0D +/**=0D + Generates a pseudorandom byte stream of the specified size.=0D +=0D + If Output is NULL, then return FALSE.=0D +=0D + @param[out] Output Pointer to buffer to receive random value.=0D + @param[in] Size Size of random bytes to generate.=0D +=0D + @retval TRUE Pseudorandom byte stream generated successfully.=0D + @retval FALSE Pseudorandom number generator fails to generate due to la= ck of entropy.=0D +=0D +**/=0D +BOOLEAN=0D +EFIAPI=0D +RandomBytes (=0D + OUT UINT8 *Output,=0D + IN UINTN Size=0D + )=0D +{=0D + BOOLEAN Ret;=0D + UINT64 TempRand;=0D +=0D + Ret =3D FALSE;=0D +=0D + while (Size > 0) {=0D + // Use RngLib to get random number=0D + Ret =3D GetRandomNumber64 (&TempRand);=0D +=0D + if (!Ret) {=0D + return Ret;=0D + }=0D +=0D + if (Size >=3D sizeof (TempRand)) {=0D + *((UINT64 *)Output) =3D TempRand;=0D + Output +=3D sizeof (UINT64);=0D + Size -=3D sizeof (TempRand);=0D + } else {=0D + CopyMem (Output, &TempRand, Size);=0D + Size =3D 0;=0D + }=0D + }=0D +=0D + return Ret;=0D +}=0D +=0D +/**=0D + The MbedTLS function f_rng, which MbedtlsRand implements.=0D +=0D + @param[in] rng_state Not used, just for compatibility with mbedlts.=0D + @param[out] output Pointer to buffer to receive random value.=0D + @param[in] len Size of random bytes to generate.=0D +=0D + @retval 0 Pseudorandom byte stream generated successfully.=0D + @retval Non-0 Pseudorandom number generator fails to generate due to la= ck of entropy.=0D +**/=0D +INTN=0D +MbedtlsRand (=0D + VOID *rng_state,=0D + UINT8 *output,=0D + UINTN len=0D + )=0D +{=0D + BOOLEAN Result;=0D +=0D + Result =3D RandomBytes (output, len);=0D +=0D + return Result ? 0 : -1;=0D +}=0D --=20 2.26.2.windows.1 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#117859): https://edk2.groups.io/g/devel/message/117859 Mute This Topic: https://groups.io/mt/105552832/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-