From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by spool.mail.gandi.net (Postfix) with ESMTPS id 5370BAC1272 for ; Thu, 28 Sep 2023 15:34:35 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=mJsg4cmiN+H2sq/ZfyX30peVBwi5tTU6AZ/zgUxYF78=; 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:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding; s=20140610; t=1695915273; v=1; b=Q1v5GSE4kC28mWdSF7f1qe54lxjmjxKaHU6/vKz3N8cLOaY8YFP8o8RQwSSgLW4vWP0pRWBO qSuVq25LwEdKUYOQrP+n84D1H1oFQoX881dyGOGxY3uDvWpvBF2I607pV4LIBAfFqwim2zG7Ado ZgmWgNV3kp7oDFeZ/3wZxywA= X-Received: by 127.0.0.2 with SMTP id OVLMYY7687511xurKPcgwwk5; Thu, 28 Sep 2023 08:34:33 -0700 X-Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.9]) by mx.groups.io with SMTP id smtpd.web10.16607.1695915264250602914 for ; Thu, 28 Sep 2023 08:34:33 -0700 X-IronPort-AV: E=McAfee;i="6600,9927,10847"; a="703991" X-IronPort-AV: E=Sophos;i="6.03,184,1694761200"; d="scan'208";a="703991" X-Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orvoesa101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Sep 2023 08:34:33 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10847"; a="778943250" X-IronPort-AV: E=Sophos;i="6.03,184,1694761200"; d="scan'208";a="778943250" X-Received: from shwdejointd777.ccr.corp.intel.com ([10.239.58.116]) by orsmga008.jf.intel.com with ESMTP; 28 Sep 2023 08:34:31 -0700 From: "Wenxing Hou" To: devel@edk2.groups.io Cc: Jiewen Yao , Yi Li , Xiaoyu Lu , Guomin Jiang Subject: [edk2-devel] [PATCH v3 05/11] CryptoPkg: Add RSA functions based on Mbedtls Date: Thu, 28 Sep 2023 23:34:10 +0800 Message-Id: <20230928153416.537-6-wenxing.hou@intel.com> In-Reply-To: <20230928153416.537-1-wenxing.hou@intel.com> References: <20230928153416.537-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 Reply-To: devel@edk2.groups.io,wenxing.hou@intel.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: cuFVcyGxGb7DKV57fLamzlXQx7686176AA= Content-Transfer-Encoding: quoted-printable X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20140610 header.b=Q1v5GSE4; 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 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io Add RSA APIs. REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3D4177 Cc: Jiewen Yao Cc: Yi Li Cc: Xiaoyu Lu Cc: Guomin Jiang Signed-off-by: Wenxing Hou --- .../BaseCryptLibMbedTls/InternalCryptLib.h | 25 ++ .../BaseCryptLibMbedTls/Pk/CryptRsaBasic.c | 278 ++++++++++++++++++ .../Pk/CryptRsaBasicNull.c | 121 ++++++++ .../BaseCryptLibMbedTls/Pk/CryptRsaExtNull.c | 117 ++++++++ .../BaseCryptLibMbedTls/Pk/CryptRsaPss.c | 174 +++++++++++ .../BaseCryptLibMbedTls/Pk/CryptRsaPssNull.c | 46 +++ .../Pk/CryptRsaPssSignNull.c | 60 ++++ .../UnitTest/Library/BaseCryptLib/RsaTests.c | 10 + 8 files changed, 831 insertions(+) create mode 100644 CryptoPkg/Library/BaseCryptLibMbedTls/InternalCryptLib.h create mode 100644 CryptoPkg/Library/BaseCryptLibMbedTls/Pk/CryptRsaBasic.c create mode 100644 CryptoPkg/Library/BaseCryptLibMbedTls/Pk/CryptRsaBasicN= ull.c create mode 100644 CryptoPkg/Library/BaseCryptLibMbedTls/Pk/CryptRsaExtNul= l.c create mode 100644 CryptoPkg/Library/BaseCryptLibMbedTls/Pk/CryptRsaPss.c create mode 100644 CryptoPkg/Library/BaseCryptLibMbedTls/Pk/CryptRsaPssNul= l.c create mode 100644 CryptoPkg/Library/BaseCryptLibMbedTls/Pk/CryptRsaPssSig= nNull.c diff --git a/CryptoPkg/Library/BaseCryptLibMbedTls/InternalCryptLib.h b/Cry= ptoPkg/Library/BaseCryptLibMbedTls/InternalCryptLib.h new file mode 100644 index 0000000000..039aa32028 --- /dev/null +++ b/CryptoPkg/Library/BaseCryptLibMbedTls/InternalCryptLib.h @@ -0,0 +1,25 @@ +/** @file=0D + Internal include file for BaseCryptLib.=0D +=0D +Copyright (c) 2023, Intel Corporation. All rights reserved.
=0D +SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#ifndef INTERNAL_CRYPT_LIB_H_=0D +#define INTERNAL_CRYPT_LIB_H_=0D +=0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +=0D +//=0D +// We should alwasy add mbedtls/config.h here=0D +// to ensure the config override takes effect.=0D +//=0D +#include =0D +=0D +#endif=0D diff --git a/CryptoPkg/Library/BaseCryptLibMbedTls/Pk/CryptRsaBasic.c b/Cry= ptoPkg/Library/BaseCryptLibMbedTls/Pk/CryptRsaBasic.c new file mode 100644 index 0000000000..8b61ae02ec --- /dev/null +++ b/CryptoPkg/Library/BaseCryptLibMbedTls/Pk/CryptRsaBasic.c @@ -0,0 +1,278 @@ +/** @file=0D + RSA Asymmetric Cipher Wrapper Implementation over MbedTLS.=0D +=0D + This file implements following APIs which provide basic capabilities for= RSA:=0D + 1) RsaNew=0D + 2) RsaFree=0D + 3) RsaSetKey=0D + 4) RsaPkcs1Verify=0D +=0D + RFC 8017 - PKCS #1: RSA Cryptography Specifications Version 2.2=0D +=0D +Copyright (c) 2023, Intel Corporation. All rights reserved.
=0D +SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#include "InternalCryptLib.h"=0D +=0D +#include =0D +=0D +/**=0D + Allocates and initializes one RSA context for subsequent use.=0D +=0D + @return Pointer to the RSA context that has been initialized.=0D + If the allocations fails, RsaNew() returns NULL.=0D +=0D +**/=0D +VOID *=0D +EFIAPI=0D +RsaNew (=0D + VOID=0D + )=0D +{=0D + VOID *RsaContext;=0D +=0D + RsaContext =3D AllocateZeroPool (sizeof (mbedtls_rsa_context));=0D + if (RsaContext =3D=3D NULL) {=0D + return RsaContext;=0D + }=0D +=0D + mbedtls_rsa_init (RsaContext);=0D + if (mbedtls_rsa_set_padding (RsaContext, MBEDTLS_RSA_PKCS_V15, MBEDTLS_M= D_NONE) !=3D 0) {=0D + return NULL;=0D + }=0D +=0D + return RsaContext;=0D +}=0D +=0D +/**=0D + Release the specified RSA context.=0D +=0D + @param[in] RsaContext Pointer to the RSA context to be released.=0D +=0D +**/=0D +VOID=0D +EFIAPI=0D +RsaFree (=0D + IN VOID *RsaContext=0D + )=0D +{=0D + mbedtls_rsa_free (RsaContext);=0D + if (RsaContext !=3D NULL) {=0D + FreePool (RsaContext);=0D + }=0D +}=0D +=0D +/**=0D + Sets the tag-designated key component into the established RSA context.= =0D +=0D + This function sets the tag-designated RSA key component into the establi= shed=0D + RSA context from the user-specified non-negative integer (octet string f= ormat=0D + represented in RSA PKCS#1).=0D + If BigNumber is NULL, then the specified key component in RSA context is= cleared.=0D +=0D + If RsaContext is NULL, then return FALSE.=0D +=0D + @param[in, out] RsaContext Pointer to RSA context being set.=0D + @param[in] KeyTag Tag of RSA key component being set.=0D + @param[in] BigNumber Pointer to octet integer buffer.=0D + If NULL, then the specified key component i= n RSA=0D + context is cleared.=0D + @param[in] BnSize Size of big number buffer in bytes.=0D + If BigNumber is NULL, then it is ignored.=0D +=0D + @retval TRUE RSA key component was set successfully.=0D + @retval FALSE Invalid RSA key component tag.=0D +=0D +**/=0D +BOOLEAN=0D +EFIAPI=0D +RsaSetKey (=0D + IN OUT VOID *RsaContext,=0D + IN RSA_KEY_TAG KeyTag,=0D + IN CONST UINT8 *BigNumber,=0D + IN UINTN BnSize=0D + )=0D +{=0D + mbedtls_rsa_context *RsaKey;=0D + INT32 Ret;=0D + mbedtls_mpi Value;=0D +=0D + //=0D + // Check input parameters.=0D + //=0D + if ((RsaContext =3D=3D NULL) || (BnSize > INT_MAX)) {=0D + return FALSE;=0D + }=0D +=0D + mbedtls_mpi_init (&Value);=0D +=0D + RsaKey =3D (mbedtls_rsa_context *)RsaContext;=0D +=0D + // if BigNumber is Null clear=0D + if (BigNumber !=3D NULL) {=0D + Ret =3D mbedtls_mpi_read_binary (&Value, BigNumber, BnSize);=0D + if (Ret !=3D 0) {=0D + mbedtls_mpi_free (&Value);=0D + return FALSE;=0D + }=0D + }=0D +=0D + switch (KeyTag) {=0D + case RsaKeyN:=0D + Ret =3D mbedtls_rsa_import (=0D + RsaKey,=0D + &Value,=0D + NULL,=0D + NULL,=0D + NULL,=0D + NULL=0D + );=0D + break;=0D + case RsaKeyE:=0D + Ret =3D mbedtls_rsa_import (=0D + RsaKey,=0D + NULL,=0D + NULL,=0D + NULL,=0D + NULL,=0D + &Value=0D + );=0D + break;=0D + case RsaKeyD:=0D + Ret =3D mbedtls_rsa_import (=0D + RsaKey,=0D + NULL,=0D + NULL,=0D + NULL,=0D + &Value,=0D + NULL=0D + );=0D + break;=0D + case RsaKeyQ:=0D + Ret =3D mbedtls_rsa_import (=0D + RsaKey,=0D + NULL,=0D + NULL,=0D + &Value,=0D + NULL,=0D + NULL=0D + );=0D + break;=0D + case RsaKeyP:=0D + Ret =3D mbedtls_rsa_import (=0D + RsaKey,=0D + NULL,=0D + &Value,=0D + NULL,=0D + NULL,=0D + NULL=0D + );=0D + break;=0D + case RsaKeyDp:=0D + case RsaKeyDq:=0D + case RsaKeyQInv:=0D + default:=0D + Ret =3D -1;=0D + break;=0D + }=0D +=0D + mbedtls_mpi_free (&Value);=0D + return Ret =3D=3D 0;=0D +}=0D +=0D +/**=0D + Verifies the RSA-SSA signature with EMSA-PKCS1-v1_5 encoding scheme defi= ned in=0D + RSA PKCS#1.=0D +=0D + If RsaContext is NULL, then return FALSE.=0D + If MessageHash is NULL, then return FALSE.=0D + If Signature is NULL, then return FALSE.=0D + If HashSize is not equal to the size of MD5, SHA-1, SHA-256, SHA-384 or = SHA-512 digest, then return FALSE.=0D +=0D + @param[in] RsaContext Pointer to RSA context for signature verificati= on.=0D + @param[in] MessageHash Pointer to octet message hash to be checked.=0D + @param[in] HashSize Size of the message hash in bytes.=0D + @param[in] Signature Pointer to RSA PKCS1-v1_5 signature to be verif= ied.=0D + @param[in] SigSize Size of signature in bytes.=0D +=0D + @retval TRUE Valid signature encoded in PKCS1-v1_5.=0D + @retval FALSE Invalid signature or invalid RSA context.=0D +=0D +**/=0D +BOOLEAN=0D +EFIAPI=0D +RsaPkcs1Verify (=0D + IN VOID *RsaContext,=0D + IN CONST UINT8 *MessageHash,=0D + IN UINTN HashSize,=0D + IN CONST UINT8 *Signature,=0D + IN UINTN SigSize=0D + )=0D +{=0D + INT32 Ret;=0D + mbedtls_md_type_t md_alg;=0D + mbedtls_rsa_context *RsaKey;=0D +=0D + if ((RsaContext =3D=3D NULL) || (MessageHash =3D=3D NULL) || (Signature = =3D=3D NULL)) {=0D + return FALSE;=0D + }=0D +=0D + if ((SigSize > INT_MAX) || (SigSize =3D=3D 0)) {=0D + return FALSE;=0D + }=0D +=0D + RsaKey =3D (mbedtls_rsa_context *)RsaContext;=0D + if (mbedtls_rsa_complete (RsaKey) !=3D 0) {=0D + return FALSE;=0D + }=0D +=0D + switch (HashSize) {=0D + #ifdef ENABLE_MD5_DEPRECATED_INTERFACES=0D + case MD5_DIGEST_SIZE:=0D + md_alg =3D MBEDTLS_MD_MD5;=0D + break;=0D + #endif=0D +=0D + #ifndef DISABLE_SHA1_DEPRECATED_INTERFACES=0D + case SHA1_DIGEST_SIZE:=0D + md_alg =3D MBEDTLS_MD_SHA1;=0D + break;=0D + #endif=0D +=0D + case SHA256_DIGEST_SIZE:=0D + md_alg =3D MBEDTLS_MD_SHA256;=0D + break;=0D +=0D + case SHA384_DIGEST_SIZE:=0D + md_alg =3D MBEDTLS_MD_SHA384;=0D + break;=0D +=0D + case SHA512_DIGEST_SIZE:=0D + md_alg =3D MBEDTLS_MD_SHA512;=0D + break;=0D +=0D + default:=0D + return FALSE;=0D + }=0D +=0D + if (mbedtls_rsa_get_len (RsaContext) !=3D SigSize) {=0D + return FALSE;=0D + }=0D +=0D + mbedtls_rsa_set_padding (RsaContext, MBEDTLS_RSA_PKCS_V15, md_alg);=0D +=0D + Ret =3D mbedtls_rsa_pkcs1_verify (=0D + RsaContext,=0D + md_alg,=0D + (UINT32)HashSize,=0D + MessageHash,=0D + Signature=0D + );=0D + if (Ret !=3D 0) {=0D + return FALSE;=0D + }=0D +=0D + return TRUE;=0D +}=0D diff --git a/CryptoPkg/Library/BaseCryptLibMbedTls/Pk/CryptRsaBasicNull.c b= /CryptoPkg/Library/BaseCryptLibMbedTls/Pk/CryptRsaBasicNull.c new file mode 100644 index 0000000000..3e643509fd --- /dev/null +++ b/CryptoPkg/Library/BaseCryptLibMbedTls/Pk/CryptRsaBasicNull.c @@ -0,0 +1,121 @@ +/** @file=0D + RSA Asymmetric Cipher Wrapper Null Implementation.=0D +=0D + This file implements following APIs which provide basic capabilities for= RSA:=0D + 1) RsaNew=0D + 2) RsaFree=0D + 3) RsaSetKey=0D + 4) RsaPkcs1Verify=0D +=0D +Copyright (c) 2023, Intel Corporation. All rights reserved.
=0D +SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#include "InternalCryptLib.h"=0D +=0D +/**=0D + Allocates and initializes one RSA context for subsequent use.=0D +=0D + @return Pointer to the RSA context that has been initialized.=0D + If the allocations fails, RsaNew() returns NULL.=0D +=0D +**/=0D +VOID *=0D +EFIAPI=0D +RsaNew (=0D + VOID=0D + )=0D +{=0D + //=0D + // Allocates & Initializes RSA Context=0D + //=0D + ASSERT (FALSE);=0D + return NULL;=0D +}=0D +=0D +/**=0D + Release the specified RSA context.=0D +=0D + @param[in] RsaContext Pointer to the RSA context to be released.=0D +=0D +**/=0D +VOID=0D +EFIAPI=0D +RsaFree (=0D + IN VOID *RsaContext=0D + )=0D +{=0D + //=0D + // Free RSA Context=0D + //=0D + ASSERT (FALSE);=0D +}=0D +=0D +/**=0D + Sets the tag-designated key component into the established RSA context.= =0D +=0D + This function sets the tag-designated RSA key component into the establi= shed=0D + RSA context from the user-specified non-negative integer (octet string f= ormat=0D + represented in RSA PKCS#1).=0D + If BigNumber is NULL, then the specified key component in RSA context is= cleared.=0D +=0D + If RsaContext is NULL, then return FALSE.=0D +=0D + @param[in, out] RsaContext Pointer to RSA context being set.=0D + @param[in] KeyTag Tag of RSA key component being set.=0D + @param[in] BigNumber Pointer to octet integer buffer.=0D + If NULL, then the specified key component i= n RSA=0D + context is cleared.=0D + @param[in] BnSize Size of big number buffer in bytes.=0D + If BigNumber is NULL, then it is ignored.=0D +=0D + @retval TRUE RSA key component was set successfully.=0D + @retval FALSE Invalid RSA key component tag.=0D +=0D +**/=0D +BOOLEAN=0D +EFIAPI=0D +RsaSetKey (=0D + IN OUT VOID *RsaContext,=0D + IN RSA_KEY_TAG KeyTag,=0D + IN CONST UINT8 *BigNumber,=0D + IN UINTN BnSize=0D + )=0D +{=0D + ASSERT (FALSE);=0D + return FALSE;=0D +}=0D +=0D +/**=0D + Verifies the RSA-SSA signature with EMSA-PKCS1-v1_5 encoding scheme defi= ned in=0D + RSA PKCS#1.=0D +=0D + If RsaContext is NULL, then return FALSE.=0D + If MessageHash is NULL, then return FALSE.=0D + If Signature is NULL, then return FALSE.=0D + If HashSize is not equal to the size of MD5, SHA-1 or SHA-256 digest, th= en return FALSE.=0D +=0D + @param[in] RsaContext Pointer to RSA context for signature verificati= on.=0D + @param[in] MessageHash Pointer to octet message hash to be checked.=0D + @param[in] HashSize Size of the message hash in bytes.=0D + @param[in] Signature Pointer to RSA PKCS1-v1_5 signature to be verif= ied.=0D + @param[in] SigSize Size of signature in bytes.=0D +=0D + @retval TRUE Valid signature encoded in PKCS1-v1_5.=0D + @retval FALSE Invalid signature or invalid RSA context.=0D +=0D +**/=0D +BOOLEAN=0D +EFIAPI=0D +RsaPkcs1Verify (=0D + IN VOID *RsaContext,=0D + IN CONST UINT8 *MessageHash,=0D + IN UINTN HashSize,=0D + IN CONST UINT8 *Signature,=0D + IN UINTN SigSize=0D + )=0D +{=0D + ASSERT (FALSE);=0D + return FALSE;=0D +}=0D diff --git a/CryptoPkg/Library/BaseCryptLibMbedTls/Pk/CryptRsaExtNull.c b/C= ryptoPkg/Library/BaseCryptLibMbedTls/Pk/CryptRsaExtNull.c new file mode 100644 index 0000000000..be810fb8ca --- /dev/null +++ b/CryptoPkg/Library/BaseCryptLibMbedTls/Pk/CryptRsaExtNull.c @@ -0,0 +1,117 @@ +/** @file=0D + RSA Asymmetric Cipher Wrapper Implementation over MbedTLS.=0D +=0D + This file does not provide real capabilities for following APIs in RSA h= andling:=0D + 1) RsaGetKey=0D + 2) RsaGenerateKey=0D + 3) RsaCheckKey=0D + 4) RsaPkcs1Sign=0D +=0D +Copyright (c) 2023, Intel Corporation. All rights reserved.
=0D +SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#include "InternalCryptLib.h"=0D +=0D +/**=0D + Gets the tag-designated RSA key component from the established RSA conte= xt.=0D +=0D + Return FALSE to indicate this interface is not supported.=0D +=0D + @param[in, out] RsaContext Pointer to RSA context being set.=0D + @param[in] KeyTag Tag of RSA key component being set.=0D + @param[out] BigNumber Pointer to octet integer buffer.=0D + @param[in, out] BnSize On input, the size of big number buffer in = bytes.=0D + On output, the size of data returned in big= number buffer in bytes.=0D +=0D + @retval FALSE This interface is not supported.=0D +=0D +**/=0D +BOOLEAN=0D +EFIAPI=0D +RsaGetKey (=0D + IN OUT VOID *RsaContext,=0D + IN RSA_KEY_TAG KeyTag,=0D + OUT UINT8 *BigNumber,=0D + IN OUT UINTN *BnSize=0D + )=0D +{=0D + ASSERT (FALSE);=0D + return FALSE;=0D +}=0D +=0D +/**=0D + Generates RSA key components.=0D +=0D + Return FALSE to indicate this interface is not supported.=0D +=0D + @param[in, out] RsaContext Pointer to RSA context being set.= =0D + @param[in] ModulusLength Length of RSA modulus N in bits.=0D + @param[in] PublicExponent Pointer to RSA public exponent.=0D + @param[in] PublicExponentSize Size of RSA public exponent buffer= in bytes.=0D +=0D + @retval FALSE This interface is not supported.=0D +=0D +**/=0D +BOOLEAN=0D +EFIAPI=0D +RsaGenerateKey (=0D + IN OUT VOID *RsaContext,=0D + IN UINTN ModulusLength,=0D + IN CONST UINT8 *PublicExponent,=0D + IN UINTN PublicExponentSize=0D + )=0D +{=0D + ASSERT (FALSE);=0D + return FALSE;=0D +}=0D +=0D +/**=0D + Validates key components of RSA context.=0D +=0D + Return FALSE to indicate this interface is not supported.=0D +=0D + @param[in] RsaContext Pointer to RSA context to check.=0D +=0D + @retval FALSE This interface is not supported.=0D +=0D +**/=0D +BOOLEAN=0D +EFIAPI=0D +RsaCheckKey (=0D + IN VOID *RsaContext=0D + )=0D +{=0D + ASSERT (FALSE);=0D + return FALSE;=0D +}=0D +=0D +/**=0D + Carries out the RSA-SSA signature generation with EMSA-PKCS1-v1_5 encodi= ng scheme.=0D +=0D + Return FALSE to indicate this interface is not supported.=0D +=0D + @param[in] RsaContext Pointer to RSA context for signature gener= ation.=0D + @param[in] MessageHash Pointer to octet message hash to be signed= .=0D + @param[in] HashSize Size of the message hash in bytes.=0D + @param[out] Signature Pointer to buffer to receive RSA PKCS1-v1_= 5 signature.=0D + @param[in, out] SigSize On input, the size of Signature buffer in = bytes.=0D + On output, the size of data returned in Si= gnature buffer in bytes.=0D +=0D + @retval FALSE This interface is not supported.=0D +=0D +**/=0D +BOOLEAN=0D +EFIAPI=0D +RsaPkcs1Sign (=0D + IN VOID *RsaContext,=0D + IN CONST UINT8 *MessageHash,=0D + IN UINTN HashSize,=0D + OUT UINT8 *Signature,=0D + IN OUT UINTN *SigSize=0D + )=0D +{=0D + ASSERT (FALSE);=0D + return FALSE;=0D +}=0D diff --git a/CryptoPkg/Library/BaseCryptLibMbedTls/Pk/CryptRsaPss.c b/Crypt= oPkg/Library/BaseCryptLibMbedTls/Pk/CryptRsaPss.c new file mode 100644 index 0000000000..7927c34ae9 --- /dev/null +++ b/CryptoPkg/Library/BaseCryptLibMbedTls/Pk/CryptRsaPss.c @@ -0,0 +1,174 @@ +/** @file=0D + RSA Asymmetric Cipher Wrapper Implementation over MbedTLS.=0D +=0D + This file implements following APIs which provide basic capabilities for= RSA:=0D + 1) RsaPssVerify=0D +=0D +Copyright (c) 2023, 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 + Verifies the RSA signature with RSASSA-PSS signature scheme defined in R= FC 8017.=0D + Implementation determines salt length automatically from the signature e= ncoding.=0D + Mask generation function is the same as the message digest algorithm.=0D + Salt length should be equal to digest length.=0D +=0D + @param[in] RsaContext Pointer to RSA context for signature verific= ation.=0D + @param[in] Message Pointer to octet message to be verified.=0D + @param[in] MsgSize Size of the message in bytes.=0D + @param[in] Signature Pointer to RSASSA-PSS signature to be verifi= ed.=0D + @param[in] SigSize Size of signature in bytes.=0D + @param[in] DigestLen Length of digest for RSA operation.=0D + @param[in] SaltLen Salt length for PSS encoding.=0D +=0D + @retval TRUE Valid signature encoded in RSASSA-PSS.=0D + @retval FALSE Invalid signature or invalid RSA context.=0D +=0D +**/=0D +BOOLEAN=0D +EFIAPI=0D +RsaPssVerify (=0D + IN VOID *RsaContext,=0D + IN CONST UINT8 *Message,=0D + IN UINTN MsgSize,=0D + IN CONST UINT8 *Signature,=0D + IN UINTN SigSize,=0D + IN UINT16 DigestLen,=0D + IN UINT16 SaltLen=0D + )=0D +{=0D + INT32 Ret;=0D + mbedtls_md_type_t md_alg;=0D + UINT8 HashValue[SHA512_DIGEST_SIZE];=0D + BOOLEAN Status;=0D + UINTN ShaCtxSize;=0D + VOID *ShaCtx;=0D + mbedtls_rsa_context *RsaKey;=0D +=0D + if (RsaContext =3D=3D NULL) {=0D + return FALSE;=0D + }=0D +=0D + if ((Message =3D=3D NULL) || (MsgSize =3D=3D 0) || (MsgSize > INT_MAX)) = {=0D + return FALSE;=0D + }=0D +=0D + if (SaltLen !=3D DigestLen) {=0D + return FALSE;=0D + }=0D +=0D + if ((Signature =3D=3D NULL) || (SigSize =3D=3D 0) || (SigSize > INT_MAX)= ) {=0D + return FALSE;=0D + }=0D +=0D + RsaKey =3D (mbedtls_rsa_context *)RsaContext;=0D + if (mbedtls_rsa_complete (RsaKey) !=3D 0) {=0D + return FALSE;=0D + }=0D +=0D + ZeroMem (HashValue, DigestLen);=0D +=0D + switch (DigestLen) {=0D + case SHA256_DIGEST_SIZE:=0D + md_alg =3D MBEDTLS_MD_SHA256;=0D + ShaCtxSize =3D Sha256GetContextSize ();=0D + ShaCtx =3D AllocateZeroPool (ShaCtxSize);=0D +=0D + Status =3D Sha256Init (ShaCtx);=0D + if (!Status) {=0D + return FALSE;=0D + }=0D +=0D + Status =3D Sha256Update (ShaCtx, Message, MsgSize);=0D + if (!Status) {=0D + FreePool (ShaCtx);=0D + return FALSE;=0D + }=0D +=0D + Status =3D Sha256Final (ShaCtx, HashValue);=0D + if (!Status) {=0D + FreePool (ShaCtx);=0D + return FALSE;=0D + }=0D +=0D + FreePool (ShaCtx);=0D + break;=0D +=0D + case SHA384_DIGEST_SIZE:=0D + md_alg =3D MBEDTLS_MD_SHA384;=0D + ShaCtxSize =3D Sha384GetContextSize ();=0D + ShaCtx =3D AllocateZeroPool (ShaCtxSize);=0D +=0D + Status =3D Sha384Init (ShaCtx);=0D + if (!Status) {=0D + return FALSE;=0D + }=0D +=0D + Status =3D Sha384Update (ShaCtx, Message, MsgSize);=0D + if (!Status) {=0D + FreePool (ShaCtx);=0D + return FALSE;=0D + }=0D +=0D + Status =3D Sha384Final (ShaCtx, HashValue);=0D + if (!Status) {=0D + FreePool (ShaCtx);=0D + return FALSE;=0D + }=0D +=0D + FreePool (ShaCtx);=0D + break;=0D +=0D + case SHA512_DIGEST_SIZE:=0D + md_alg =3D MBEDTLS_MD_SHA512;=0D + ShaCtxSize =3D Sha512GetContextSize ();=0D + ShaCtx =3D AllocateZeroPool (ShaCtxSize);=0D +=0D + Status =3D Sha512Init (ShaCtx);=0D + if (!Status) {=0D + return FALSE;=0D + }=0D +=0D + Status =3D Sha512Update (ShaCtx, Message, MsgSize);=0D + if (!Status) {=0D + FreePool (ShaCtx);=0D + return FALSE;=0D + }=0D +=0D + Status =3D Sha512Final (ShaCtx, HashValue);=0D + if (!Status) {=0D + FreePool (ShaCtx);=0D + return FALSE;=0D + }=0D +=0D + FreePool (ShaCtx);=0D + break;=0D +=0D + default:=0D + return FALSE;=0D + }=0D +=0D + if (mbedtls_rsa_get_len (RsaContext) !=3D SigSize) {=0D + return FALSE;=0D + }=0D +=0D + mbedtls_rsa_set_padding (RsaContext, MBEDTLS_RSA_PKCS_V21, md_alg);=0D +=0D + Ret =3D mbedtls_rsa_rsassa_pss_verify (=0D + RsaContext,=0D + md_alg,=0D + (UINT32)DigestLen,=0D + HashValue,=0D + Signature=0D + );=0D + if (Ret !=3D 0) {=0D + return FALSE;=0D + }=0D +=0D + return TRUE;=0D +}=0D diff --git a/CryptoPkg/Library/BaseCryptLibMbedTls/Pk/CryptRsaPssNull.c b/C= ryptoPkg/Library/BaseCryptLibMbedTls/Pk/CryptRsaPssNull.c new file mode 100644 index 0000000000..75ad71a922 --- /dev/null +++ b/CryptoPkg/Library/BaseCryptLibMbedTls/Pk/CryptRsaPssNull.c @@ -0,0 +1,46 @@ +/** @file=0D + RSA-PSS Asymmetric Cipher Wrapper Implementation over MbedTLS.=0D +=0D + This file does not provide real capabilities for following APIs in RSA h= andling:=0D + 1) RsaPssVerify=0D +=0D +Copyright (c) 2023, Intel Corporation. All rights reserved.
=0D +SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#include "InternalCryptLib.h"=0D +=0D +/**=0D + Verifies the RSA signature with RSASSA-PSS signature scheme defined in R= FC 8017.=0D + Implementation determines salt length automatically from the signature e= ncoding.=0D + Mask generation function is the same as the message digest algorithm.=0D + Salt length should be equal to digest length.=0D +=0D + @param[in] RsaContext Pointer to RSA context for signature verific= ation.=0D + @param[in] Message Pointer to octet message to be verified.=0D + @param[in] MsgSize Size of the message in bytes.=0D + @param[in] Signature Pointer to RSASSA-PSS signature to be verifi= ed.=0D + @param[in] SigSize Size of signature in bytes.=0D + @param[in] DigestLen Length of digest for RSA operation.=0D + @param[in] SaltLen Salt length for PSS encoding.=0D +=0D + @retval TRUE Valid signature encoded in RSASSA-PSS.=0D + @retval FALSE Invalid signature or invalid RSA context.=0D +=0D +**/=0D +BOOLEAN=0D +EFIAPI=0D +RsaPssVerify (=0D + IN VOID *RsaContext,=0D + IN CONST UINT8 *Message,=0D + IN UINTN MsgSize,=0D + IN CONST UINT8 *Signature,=0D + IN UINTN SigSize,=0D + IN UINT16 DigestLen,=0D + IN UINT16 SaltLen=0D + )=0D +{=0D + ASSERT (FALSE);=0D + return FALSE;=0D +}=0D diff --git a/CryptoPkg/Library/BaseCryptLibMbedTls/Pk/CryptRsaPssSignNull.c= b/CryptoPkg/Library/BaseCryptLibMbedTls/Pk/CryptRsaPssSignNull.c new file mode 100644 index 0000000000..10687bd38e --- /dev/null +++ b/CryptoPkg/Library/BaseCryptLibMbedTls/Pk/CryptRsaPssSignNull.c @@ -0,0 +1,60 @@ +/** @file=0D + RSA-PSS Asymmetric Cipher Wrapper Implementation over MbedTLS.=0D +=0D + This file does not provide real capabilities for following APIs in RSA h= andling:=0D + 1) RsaPssSign=0D +=0D +Copyright (c) 2023, Intel Corporation. All rights reserved.
=0D +SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#include "InternalCryptLib.h"=0D +=0D +/**=0D + Carries out the RSA-SSA signature generation with EMSA-PSS encoding sche= me.=0D +=0D + This function carries out the RSA-SSA signature generation with EMSA-PSS= encoding scheme defined in=0D + RFC 8017.=0D + Mask generation function is the same as the message digest algorithm.=0D + If the Signature buffer is too small to hold the contents of signature, = FALSE=0D + is returned and SigSize is set to the required buffer size to obtain the= signature.=0D +=0D + If RsaContext is NULL, then return FALSE.=0D + If Message is NULL, then return FALSE.=0D + If MsgSize is zero or > INT_MAX, then return FALSE.=0D + If DigestLen is NOT 32, 48 or 64, return FALSE.=0D + If SaltLen is not equal to DigestLen, then return FALSE.=0D + If SigSize is large enough but Signature is NULL, then return FALSE.=0D + If this interface is not supported, then return FALSE.=0D +=0D + @param[in] RsaContext Pointer to RSA context for signature genera= tion.=0D + @param[in] Message Pointer to octet message to be signed.=0D + @param[in] MsgSize Size of the message in bytes.=0D + @param[in] DigestLen Length of the digest in bytes to be used fo= r RSA signature operation.=0D + @param[in] SaltLen Length of the salt in bytes to be used for = PSS encoding.=0D + @param[out] Signature Pointer to buffer to receive RSA PSS signat= ure.=0D + @param[in, out] SigSize On input, the size of Signature buffer in b= ytes.=0D + On output, the size of data returned in Sig= nature buffer in bytes.=0D +=0D + @retval TRUE Signature successfully generated in RSASSA-PSS.=0D + @retval FALSE Signature generation failed.=0D + @retval FALSE SigSize is too small.=0D + @retval FALSE This interface is not supported.=0D +=0D +**/=0D +BOOLEAN=0D +EFIAPI=0D +RsaPssSign (=0D + IN VOID *RsaContext,=0D + IN CONST UINT8 *Message,=0D + IN UINTN MsgSize,=0D + IN UINT16 DigestLen,=0D + IN UINT16 SaltLen,=0D + OUT UINT8 *Signature,=0D + IN OUT UINTN *SigSize=0D + )=0D +{=0D + ASSERT (FALSE);=0D + return FALSE;=0D +}=0D diff --git a/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/RsaTests.c b/Cryp= toPkg/Test/UnitTest/Library/BaseCryptLib/RsaTests.c index 3f06e89b3c..c61f2da73d 100644 --- a/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/RsaTests.c +++ b/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/RsaTests.c @@ -194,6 +194,9 @@ TestVerifyRsaGenerateKeyComponents ( BOOLEAN Status;=0D UINTN KeySize;=0D UINT8 *KeyBuffer;=0D + UINT8 TestPublicExponent1[] =3D { 0x03 };=0D + UINT8 TestPublicExponent2[] =3D { 0x01, 0x01 };=0D + UINT8 TestPublicExponent3[] =3D { 0x01, 0x00, 0x01 };=0D =0D //=0D // Generate RSA Key Components=0D @@ -202,6 +205,13 @@ TestVerifyRsaGenerateKeyComponents ( Status =3D RsaGenerateKey (mRsa, RSA_MODULUS_LENGTH, NULL, 0);=0D UT_ASSERT_TRUE (Status);=0D =0D + Status =3D RsaGenerateKey (mRsa, RSA_MODULUS_LENGTH, TestPublicExponent1= , sizeof (TestPublicExponent1));=0D + UT_ASSERT_TRUE (Status);=0D + Status =3D RsaGenerateKey (mRsa, RSA_MODULUS_LENGTH, TestPublicExponent2= , sizeof (TestPublicExponent2));=0D + UT_ASSERT_TRUE (Status);=0D + Status =3D RsaGenerateKey (mRsa, RSA_MODULUS_LENGTH, TestPublicExponent3= , sizeof (TestPublicExponent3));=0D + UT_ASSERT_TRUE (Status);=0D +=0D KeySize =3D RSA_MODULUS_LENGTH / 8;=0D KeyBuffer =3D AllocatePool (KeySize);=0D Status =3D RsaGetKey (mRsa, RsaKeyE, KeyBuffer, &KeySize);=0D --=20 2.26.2.windows.1 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#109157): https://edk2.groups.io/g/devel/message/109157 Mute This Topic: https://groups.io/mt/101639980/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-