From nobody Tue Feb 10 02:49:55 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+109157+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+109157+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1695915274; cv=none; d=zohomail.com; s=zohoarc; b=C8sE6wm1LNcR5A0dlUtk6xzFrA/NX2Lp7gSMuvNgpGfij5Ez0l7Lx9WbNzlidnDZWY+DAUABck+nR1RGi56DbxFePoo0cgE+VJ5Rpj/dO2VNrD/dn+chjiuc80mcpgx63xfE6m94cNao3rMcnnQI5WHaVK79Bbr13xkJk7ENpBM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1695915274; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=UFN0qpW9xZO4pDN5PN29GKf/WukcoKhFr9Q8zYkL/2w=; b=eWToH0ZggEYf1sGi8hkLTvobqZHnLWIVhX3z2X/2vgKVs9VvwriGiA0kgb7dLD93BdSEZ4xxmOGMQ2ZfloAw7z5CMXW960tkQbNIZrUpjJG8fFDn+9Oh/P0Es+qeIE+MpLwULzy7M20XCihfpqBoMatbMDCeH7Dj5DM1o+UoyBQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+109157+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1695915274239873.3579921340771; Thu, 28 Sep 2023 08:34:34 -0700 (PDT) Return-Path: DKIM-Signature: a=rsa-sha256; bh=/nbYIOXZGGXylXkZHf77lsBd+7AMQFpAwOkKIUq24OA=; 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=G6wzZy9EjndBIXMg5zmg6ThHeTDPWldJjolAivSTZsrQnSpdEyKcqSbkQb4jBQJQb6iSBam5 RAWip44wpcEQxVsgc65RO0kVec2E9/Qq640puXCS+lLzofoPfc/1UyfmzPQiMcjNmZNKCIhI9o7 nnR/5Uc1NRUW5CrAUtjXR7w0= X-Received: by 127.0.0.2 with SMTP id Fk0KYY1788612x56PVjqgRyv; 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: OVIufIsyCaudWaRXBEC2ikyFx1787277AA= Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1695915274644100012 Content-Type: text/plain; charset="utf-8" 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 + Internal include file for BaseCryptLib. + +Copyright (c) 2023, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef INTERNAL_CRYPT_LIB_H_ +#define INTERNAL_CRYPT_LIB_H_ + +#include +#include +#include +#include +#include +#include + +// +// We should alwasy add mbedtls/config.h here +// to ensure the config override takes effect. +// +#include + +#endif 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 + RSA Asymmetric Cipher Wrapper Implementation over MbedTLS. + + This file implements following APIs which provide basic capabilities for= RSA: + 1) RsaNew + 2) RsaFree + 3) RsaSetKey + 4) RsaPkcs1Verify + + RFC 8017 - PKCS #1: RSA Cryptography Specifications Version 2.2 + +Copyright (c) 2023, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "InternalCryptLib.h" + +#include + +/** + Allocates and initializes one RSA context for subsequent use. + + @return Pointer to the RSA context that has been initialized. + If the allocations fails, RsaNew() returns NULL. + +**/ +VOID * +EFIAPI +RsaNew ( + VOID + ) +{ + VOID *RsaContext; + + RsaContext =3D AllocateZeroPool (sizeof (mbedtls_rsa_context)); + if (RsaContext =3D=3D NULL) { + return RsaContext; + } + + mbedtls_rsa_init (RsaContext); + if (mbedtls_rsa_set_padding (RsaContext, MBEDTLS_RSA_PKCS_V15, MBEDTLS_M= D_NONE) !=3D 0) { + return NULL; + } + + return RsaContext; +} + +/** + Release the specified RSA context. + + @param[in] RsaContext Pointer to the RSA context to be released. + +**/ +VOID +EFIAPI +RsaFree ( + IN VOID *RsaContext + ) +{ + mbedtls_rsa_free (RsaContext); + if (RsaContext !=3D NULL) { + FreePool (RsaContext); + } +} + +/** + Sets the tag-designated key component into the established RSA context. + + This function sets the tag-designated RSA key component into the establi= shed + RSA context from the user-specified non-negative integer (octet string f= ormat + represented in RSA PKCS#1). + If BigNumber is NULL, then the specified key component in RSA context is= cleared. + + If RsaContext is NULL, then return FALSE. + + @param[in, out] RsaContext Pointer to RSA context being set. + @param[in] KeyTag Tag of RSA key component being set. + @param[in] BigNumber Pointer to octet integer buffer. + If NULL, then the specified key component i= n RSA + context is cleared. + @param[in] BnSize Size of big number buffer in bytes. + If BigNumber is NULL, then it is ignored. + + @retval TRUE RSA key component was set successfully. + @retval FALSE Invalid RSA key component tag. + +**/ +BOOLEAN +EFIAPI +RsaSetKey ( + IN OUT VOID *RsaContext, + IN RSA_KEY_TAG KeyTag, + IN CONST UINT8 *BigNumber, + IN UINTN BnSize + ) +{ + mbedtls_rsa_context *RsaKey; + INT32 Ret; + mbedtls_mpi Value; + + // + // Check input parameters. + // + if ((RsaContext =3D=3D NULL) || (BnSize > INT_MAX)) { + return FALSE; + } + + mbedtls_mpi_init (&Value); + + RsaKey =3D (mbedtls_rsa_context *)RsaContext; + + // if BigNumber is Null clear + if (BigNumber !=3D NULL) { + Ret =3D mbedtls_mpi_read_binary (&Value, BigNumber, BnSize); + if (Ret !=3D 0) { + mbedtls_mpi_free (&Value); + return FALSE; + } + } + + switch (KeyTag) { + case RsaKeyN: + Ret =3D mbedtls_rsa_import ( + RsaKey, + &Value, + NULL, + NULL, + NULL, + NULL + ); + break; + case RsaKeyE: + Ret =3D mbedtls_rsa_import ( + RsaKey, + NULL, + NULL, + NULL, + NULL, + &Value + ); + break; + case RsaKeyD: + Ret =3D mbedtls_rsa_import ( + RsaKey, + NULL, + NULL, + NULL, + &Value, + NULL + ); + break; + case RsaKeyQ: + Ret =3D mbedtls_rsa_import ( + RsaKey, + NULL, + NULL, + &Value, + NULL, + NULL + ); + break; + case RsaKeyP: + Ret =3D mbedtls_rsa_import ( + RsaKey, + NULL, + &Value, + NULL, + NULL, + NULL + ); + break; + case RsaKeyDp: + case RsaKeyDq: + case RsaKeyQInv: + default: + Ret =3D -1; + break; + } + + mbedtls_mpi_free (&Value); + return Ret =3D=3D 0; +} + +/** + Verifies the RSA-SSA signature with EMSA-PKCS1-v1_5 encoding scheme defi= ned in + RSA PKCS#1. + + If RsaContext is NULL, then return FALSE. + If MessageHash is NULL, then return FALSE. + If Signature is NULL, then return FALSE. + If HashSize is not equal to the size of MD5, SHA-1, SHA-256, SHA-384 or = SHA-512 digest, then return FALSE. + + @param[in] RsaContext Pointer to RSA context for signature verificati= on. + @param[in] MessageHash Pointer to octet message hash to be checked. + @param[in] HashSize Size of the message hash in bytes. + @param[in] Signature Pointer to RSA PKCS1-v1_5 signature to be verif= ied. + @param[in] SigSize Size of signature in bytes. + + @retval TRUE Valid signature encoded in PKCS1-v1_5. + @retval FALSE Invalid signature or invalid RSA context. + +**/ +BOOLEAN +EFIAPI +RsaPkcs1Verify ( + IN VOID *RsaContext, + IN CONST UINT8 *MessageHash, + IN UINTN HashSize, + IN CONST UINT8 *Signature, + IN UINTN SigSize + ) +{ + INT32 Ret; + mbedtls_md_type_t md_alg; + mbedtls_rsa_context *RsaKey; + + if ((RsaContext =3D=3D NULL) || (MessageHash =3D=3D NULL) || (Signature = =3D=3D NULL)) { + return FALSE; + } + + if ((SigSize > INT_MAX) || (SigSize =3D=3D 0)) { + return FALSE; + } + + RsaKey =3D (mbedtls_rsa_context *)RsaContext; + if (mbedtls_rsa_complete (RsaKey) !=3D 0) { + return FALSE; + } + + switch (HashSize) { + #ifdef ENABLE_MD5_DEPRECATED_INTERFACES + case MD5_DIGEST_SIZE: + md_alg =3D MBEDTLS_MD_MD5; + break; + #endif + + #ifndef DISABLE_SHA1_DEPRECATED_INTERFACES + case SHA1_DIGEST_SIZE: + md_alg =3D MBEDTLS_MD_SHA1; + break; + #endif + + case SHA256_DIGEST_SIZE: + md_alg =3D MBEDTLS_MD_SHA256; + break; + + case SHA384_DIGEST_SIZE: + md_alg =3D MBEDTLS_MD_SHA384; + break; + + case SHA512_DIGEST_SIZE: + md_alg =3D MBEDTLS_MD_SHA512; + break; + + default: + return FALSE; + } + + if (mbedtls_rsa_get_len (RsaContext) !=3D SigSize) { + return FALSE; + } + + mbedtls_rsa_set_padding (RsaContext, MBEDTLS_RSA_PKCS_V15, md_alg); + + Ret =3D mbedtls_rsa_pkcs1_verify ( + RsaContext, + md_alg, + (UINT32)HashSize, + MessageHash, + Signature + ); + if (Ret !=3D 0) { + return FALSE; + } + + return TRUE; +} 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 + RSA Asymmetric Cipher Wrapper Null Implementation. + + This file implements following APIs which provide basic capabilities for= RSA: + 1) RsaNew + 2) RsaFree + 3) RsaSetKey + 4) RsaPkcs1Verify + +Copyright (c) 2023, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "InternalCryptLib.h" + +/** + Allocates and initializes one RSA context for subsequent use. + + @return Pointer to the RSA context that has been initialized. + If the allocations fails, RsaNew() returns NULL. + +**/ +VOID * +EFIAPI +RsaNew ( + VOID + ) +{ + // + // Allocates & Initializes RSA Context + // + ASSERT (FALSE); + return NULL; +} + +/** + Release the specified RSA context. + + @param[in] RsaContext Pointer to the RSA context to be released. + +**/ +VOID +EFIAPI +RsaFree ( + IN VOID *RsaContext + ) +{ + // + // Free RSA Context + // + ASSERT (FALSE); +} + +/** + Sets the tag-designated key component into the established RSA context. + + This function sets the tag-designated RSA key component into the establi= shed + RSA context from the user-specified non-negative integer (octet string f= ormat + represented in RSA PKCS#1). + If BigNumber is NULL, then the specified key component in RSA context is= cleared. + + If RsaContext is NULL, then return FALSE. + + @param[in, out] RsaContext Pointer to RSA context being set. + @param[in] KeyTag Tag of RSA key component being set. + @param[in] BigNumber Pointer to octet integer buffer. + If NULL, then the specified key component i= n RSA + context is cleared. + @param[in] BnSize Size of big number buffer in bytes. + If BigNumber is NULL, then it is ignored. + + @retval TRUE RSA key component was set successfully. + @retval FALSE Invalid RSA key component tag. + +**/ +BOOLEAN +EFIAPI +RsaSetKey ( + IN OUT VOID *RsaContext, + IN RSA_KEY_TAG KeyTag, + IN CONST UINT8 *BigNumber, + IN UINTN BnSize + ) +{ + ASSERT (FALSE); + return FALSE; +} + +/** + Verifies the RSA-SSA signature with EMSA-PKCS1-v1_5 encoding scheme defi= ned in + RSA PKCS#1. + + If RsaContext is NULL, then return FALSE. + If MessageHash is NULL, then return FALSE. + If Signature is NULL, then return FALSE. + If HashSize is not equal to the size of MD5, SHA-1 or SHA-256 digest, th= en return FALSE. + + @param[in] RsaContext Pointer to RSA context for signature verificati= on. + @param[in] MessageHash Pointer to octet message hash to be checked. + @param[in] HashSize Size of the message hash in bytes. + @param[in] Signature Pointer to RSA PKCS1-v1_5 signature to be verif= ied. + @param[in] SigSize Size of signature in bytes. + + @retval TRUE Valid signature encoded in PKCS1-v1_5. + @retval FALSE Invalid signature or invalid RSA context. + +**/ +BOOLEAN +EFIAPI +RsaPkcs1Verify ( + IN VOID *RsaContext, + IN CONST UINT8 *MessageHash, + IN UINTN HashSize, + IN CONST UINT8 *Signature, + IN UINTN SigSize + ) +{ + ASSERT (FALSE); + return FALSE; +} 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 + RSA Asymmetric Cipher Wrapper Implementation over MbedTLS. + + This file does not provide real capabilities for following APIs in RSA h= andling: + 1) RsaGetKey + 2) RsaGenerateKey + 3) RsaCheckKey + 4) RsaPkcs1Sign + +Copyright (c) 2023, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "InternalCryptLib.h" + +/** + Gets the tag-designated RSA key component from the established RSA conte= xt. + + Return FALSE to indicate this interface is not supported. + + @param[in, out] RsaContext Pointer to RSA context being set. + @param[in] KeyTag Tag of RSA key component being set. + @param[out] BigNumber Pointer to octet integer buffer. + @param[in, out] BnSize On input, the size of big number buffer in = bytes. + On output, the size of data returned in big= number buffer in bytes. + + @retval FALSE This interface is not supported. + +**/ +BOOLEAN +EFIAPI +RsaGetKey ( + IN OUT VOID *RsaContext, + IN RSA_KEY_TAG KeyTag, + OUT UINT8 *BigNumber, + IN OUT UINTN *BnSize + ) +{ + ASSERT (FALSE); + return FALSE; +} + +/** + Generates RSA key components. + + Return FALSE to indicate this interface is not supported. + + @param[in, out] RsaContext Pointer to RSA context being set. + @param[in] ModulusLength Length of RSA modulus N in bits. + @param[in] PublicExponent Pointer to RSA public exponent. + @param[in] PublicExponentSize Size of RSA public exponent buffer= in bytes. + + @retval FALSE This interface is not supported. + +**/ +BOOLEAN +EFIAPI +RsaGenerateKey ( + IN OUT VOID *RsaContext, + IN UINTN ModulusLength, + IN CONST UINT8 *PublicExponent, + IN UINTN PublicExponentSize + ) +{ + ASSERT (FALSE); + return FALSE; +} + +/** + Validates key components of RSA context. + + Return FALSE to indicate this interface is not supported. + + @param[in] RsaContext Pointer to RSA context to check. + + @retval FALSE This interface is not supported. + +**/ +BOOLEAN +EFIAPI +RsaCheckKey ( + IN VOID *RsaContext + ) +{ + ASSERT (FALSE); + return FALSE; +} + +/** + Carries out the RSA-SSA signature generation with EMSA-PKCS1-v1_5 encodi= ng scheme. + + Return FALSE to indicate this interface is not supported. + + @param[in] RsaContext Pointer to RSA context for signature gener= ation. + @param[in] MessageHash Pointer to octet message hash to be signed. + @param[in] HashSize Size of the message hash in bytes. + @param[out] Signature Pointer to buffer to receive RSA PKCS1-v1_= 5 signature. + @param[in, out] SigSize On input, the size of Signature buffer in = bytes. + On output, the size of data returned in Si= gnature buffer in bytes. + + @retval FALSE This interface is not supported. + +**/ +BOOLEAN +EFIAPI +RsaPkcs1Sign ( + IN VOID *RsaContext, + IN CONST UINT8 *MessageHash, + IN UINTN HashSize, + OUT UINT8 *Signature, + IN OUT UINTN *SigSize + ) +{ + ASSERT (FALSE); + return FALSE; +} 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 + RSA Asymmetric Cipher Wrapper Implementation over MbedTLS. + + This file implements following APIs which provide basic capabilities for= RSA: + 1) RsaPssVerify + +Copyright (c) 2023, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "InternalCryptLib.h" +#include + +/** + Verifies the RSA signature with RSASSA-PSS signature scheme defined in R= FC 8017. + Implementation determines salt length automatically from the signature e= ncoding. + Mask generation function is the same as the message digest algorithm. + Salt length should be equal to digest length. + + @param[in] RsaContext Pointer to RSA context for signature verific= ation. + @param[in] Message Pointer to octet message to be verified. + @param[in] MsgSize Size of the message in bytes. + @param[in] Signature Pointer to RSASSA-PSS signature to be verifi= ed. + @param[in] SigSize Size of signature in bytes. + @param[in] DigestLen Length of digest for RSA operation. + @param[in] SaltLen Salt length for PSS encoding. + + @retval TRUE Valid signature encoded in RSASSA-PSS. + @retval FALSE Invalid signature or invalid RSA context. + +**/ +BOOLEAN +EFIAPI +RsaPssVerify ( + IN VOID *RsaContext, + IN CONST UINT8 *Message, + IN UINTN MsgSize, + IN CONST UINT8 *Signature, + IN UINTN SigSize, + IN UINT16 DigestLen, + IN UINT16 SaltLen + ) +{ + INT32 Ret; + mbedtls_md_type_t md_alg; + UINT8 HashValue[SHA512_DIGEST_SIZE]; + BOOLEAN Status; + UINTN ShaCtxSize; + VOID *ShaCtx; + mbedtls_rsa_context *RsaKey; + + if (RsaContext =3D=3D NULL) { + return FALSE; + } + + if ((Message =3D=3D NULL) || (MsgSize =3D=3D 0) || (MsgSize > INT_MAX)) { + return FALSE; + } + + if (SaltLen !=3D DigestLen) { + return FALSE; + } + + if ((Signature =3D=3D NULL) || (SigSize =3D=3D 0) || (SigSize > INT_MAX)= ) { + return FALSE; + } + + RsaKey =3D (mbedtls_rsa_context *)RsaContext; + if (mbedtls_rsa_complete (RsaKey) !=3D 0) { + return FALSE; + } + + ZeroMem (HashValue, DigestLen); + + switch (DigestLen) { + case SHA256_DIGEST_SIZE: + md_alg =3D MBEDTLS_MD_SHA256; + ShaCtxSize =3D Sha256GetContextSize (); + ShaCtx =3D AllocateZeroPool (ShaCtxSize); + + Status =3D Sha256Init (ShaCtx); + if (!Status) { + return FALSE; + } + + Status =3D Sha256Update (ShaCtx, Message, MsgSize); + if (!Status) { + FreePool (ShaCtx); + return FALSE; + } + + Status =3D Sha256Final (ShaCtx, HashValue); + if (!Status) { + FreePool (ShaCtx); + return FALSE; + } + + FreePool (ShaCtx); + break; + + case SHA384_DIGEST_SIZE: + md_alg =3D MBEDTLS_MD_SHA384; + ShaCtxSize =3D Sha384GetContextSize (); + ShaCtx =3D AllocateZeroPool (ShaCtxSize); + + Status =3D Sha384Init (ShaCtx); + if (!Status) { + return FALSE; + } + + Status =3D Sha384Update (ShaCtx, Message, MsgSize); + if (!Status) { + FreePool (ShaCtx); + return FALSE; + } + + Status =3D Sha384Final (ShaCtx, HashValue); + if (!Status) { + FreePool (ShaCtx); + return FALSE; + } + + FreePool (ShaCtx); + break; + + case SHA512_DIGEST_SIZE: + md_alg =3D MBEDTLS_MD_SHA512; + ShaCtxSize =3D Sha512GetContextSize (); + ShaCtx =3D AllocateZeroPool (ShaCtxSize); + + Status =3D Sha512Init (ShaCtx); + if (!Status) { + return FALSE; + } + + Status =3D Sha512Update (ShaCtx, Message, MsgSize); + if (!Status) { + FreePool (ShaCtx); + return FALSE; + } + + Status =3D Sha512Final (ShaCtx, HashValue); + if (!Status) { + FreePool (ShaCtx); + return FALSE; + } + + FreePool (ShaCtx); + break; + + default: + return FALSE; + } + + if (mbedtls_rsa_get_len (RsaContext) !=3D SigSize) { + return FALSE; + } + + mbedtls_rsa_set_padding (RsaContext, MBEDTLS_RSA_PKCS_V21, md_alg); + + Ret =3D mbedtls_rsa_rsassa_pss_verify ( + RsaContext, + md_alg, + (UINT32)DigestLen, + HashValue, + Signature + ); + if (Ret !=3D 0) { + return FALSE; + } + + return TRUE; +} 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 + RSA-PSS Asymmetric Cipher Wrapper Implementation over MbedTLS. + + This file does not provide real capabilities for following APIs in RSA h= andling: + 1) RsaPssVerify + +Copyright (c) 2023, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "InternalCryptLib.h" + +/** + Verifies the RSA signature with RSASSA-PSS signature scheme defined in R= FC 8017. + Implementation determines salt length automatically from the signature e= ncoding. + Mask generation function is the same as the message digest algorithm. + Salt length should be equal to digest length. + + @param[in] RsaContext Pointer to RSA context for signature verific= ation. + @param[in] Message Pointer to octet message to be verified. + @param[in] MsgSize Size of the message in bytes. + @param[in] Signature Pointer to RSASSA-PSS signature to be verifi= ed. + @param[in] SigSize Size of signature in bytes. + @param[in] DigestLen Length of digest for RSA operation. + @param[in] SaltLen Salt length for PSS encoding. + + @retval TRUE Valid signature encoded in RSASSA-PSS. + @retval FALSE Invalid signature or invalid RSA context. + +**/ +BOOLEAN +EFIAPI +RsaPssVerify ( + IN VOID *RsaContext, + IN CONST UINT8 *Message, + IN UINTN MsgSize, + IN CONST UINT8 *Signature, + IN UINTN SigSize, + IN UINT16 DigestLen, + IN UINT16 SaltLen + ) +{ + ASSERT (FALSE); + return FALSE; +} 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 + RSA-PSS Asymmetric Cipher Wrapper Implementation over MbedTLS. + + This file does not provide real capabilities for following APIs in RSA h= andling: + 1) RsaPssSign + +Copyright (c) 2023, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "InternalCryptLib.h" + +/** + Carries out the RSA-SSA signature generation with EMSA-PSS encoding sche= me. + + This function carries out the RSA-SSA signature generation with EMSA-PSS= encoding scheme defined in + RFC 8017. + Mask generation function is the same as the message digest algorithm. + If the Signature buffer is too small to hold the contents of signature, = FALSE + is returned and SigSize is set to the required buffer size to obtain the= signature. + + If RsaContext is NULL, then return FALSE. + If Message is NULL, then return FALSE. + If MsgSize is zero or > INT_MAX, then return FALSE. + If DigestLen is NOT 32, 48 or 64, return FALSE. + If SaltLen is not equal to DigestLen, then return FALSE. + If SigSize is large enough but Signature is NULL, then return FALSE. + If this interface is not supported, then return FALSE. + + @param[in] RsaContext Pointer to RSA context for signature genera= tion. + @param[in] Message Pointer to octet message to be signed. + @param[in] MsgSize Size of the message in bytes. + @param[in] DigestLen Length of the digest in bytes to be used fo= r RSA signature operation. + @param[in] SaltLen Length of the salt in bytes to be used for = PSS encoding. + @param[out] Signature Pointer to buffer to receive RSA PSS signat= ure. + @param[in, out] SigSize On input, the size of Signature buffer in b= ytes. + On output, the size of data returned in Sig= nature buffer in bytes. + + @retval TRUE Signature successfully generated in RSASSA-PSS. + @retval FALSE Signature generation failed. + @retval FALSE SigSize is too small. + @retval FALSE This interface is not supported. + +**/ +BOOLEAN +EFIAPI +RsaPssSign ( + IN VOID *RsaContext, + IN CONST UINT8 *Message, + IN UINTN MsgSize, + IN UINT16 DigestLen, + IN UINT16 SaltLen, + OUT UINT8 *Signature, + IN OUT UINTN *SigSize + ) +{ + ASSERT (FALSE); + return FALSE; +} 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; UINTN KeySize; UINT8 *KeyBuffer; + UINT8 TestPublicExponent1[] =3D { 0x03 }; + UINT8 TestPublicExponent2[] =3D { 0x01, 0x01 }; + UINT8 TestPublicExponent3[] =3D { 0x01, 0x00, 0x01 }; =20 // // Generate RSA Key Components @@ -202,6 +205,13 @@ TestVerifyRsaGenerateKeyComponents ( Status =3D RsaGenerateKey (mRsa, RSA_MODULUS_LENGTH, NULL, 0); UT_ASSERT_TRUE (Status); =20 + Status =3D RsaGenerateKey (mRsa, RSA_MODULUS_LENGTH, TestPublicExponent1= , sizeof (TestPublicExponent1)); + UT_ASSERT_TRUE (Status); + Status =3D RsaGenerateKey (mRsa, RSA_MODULUS_LENGTH, TestPublicExponent2= , sizeof (TestPublicExponent2)); + UT_ASSERT_TRUE (Status); + Status =3D RsaGenerateKey (mRsa, RSA_MODULUS_LENGTH, TestPublicExponent3= , sizeof (TestPublicExponent3)); + UT_ASSERT_TRUE (Status); + KeySize =3D RSA_MODULUS_LENGTH / 8; KeyBuffer =3D AllocatePool (KeySize); Status =3D RsaGetKey (mRsa, RsaKeyE, KeyBuffer, &KeySize); --=20 2.26.2.windows.1 -=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 (#109157): https://edk2.groups.io/g/devel/message/109157 Mute This Topic: https://groups.io/mt/101639980/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-