From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail05.groups.io (mail05.groups.io [45.79.224.7]) by spool.mail.gandi.net (Postfix) with ESMTPS id AD87194128A for ; Thu, 9 May 2024 06:27:15 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=iMvdnBf87XDs9F2fNB8CX7Gfn3r0LmydmRqkK8XdVks=; 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=1715236034; v=1; b=ZugjrlEELGPZBmNSOFs2w92rPqkODRZx7lfzFgAzUJfjHaNvla/lCMpdbBx0lh2CHfmuawVs lFn2ouUQTqAnby5Qn37cUl7yLX6cTv83Pz1g11imOfiN3GHGt2ymbOZhUHn6Cbn+65lxwN4E0Y2 6GmnOmkYdpKD/kiqqu0ljFX/KBfEE4RXG4Kq1E//0S4Lv2m6NufAXu+pZIMKD4IYzADmmJZU7fA 7RoUALO+AYoP3f8d4RP+2ew+o80nZcA0C6TgtVmGUZjy32YawE9xWdgvMA3VpDck2KLUhJWpLms EDt/ryaI3uM7Y0JuFL01AgnuYQL4D8GEiOABbajGfQwJA== X-Received: by 127.0.0.2 with SMTP id i61WYY7687511xFNNFulfyHx; Wed, 08 May 2024 23:27:14 -0700 X-Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.12]) by mx.groups.io with SMTP id smtpd.web10.3998.1715236024155799791 for ; Wed, 08 May 2024 23:27:12 -0700 X-CSE-ConnectionGUID: GLBbBRyAS6GPiK0X18MG3Q== X-CSE-MsgGUID: Lci1qnnQQ9qUq/nFvrY1mQ== X-IronPort-AV: E=McAfee;i="6600,9927,11067"; a="14946415" X-IronPort-AV: E=Sophos;i="6.08,147,1712646000"; d="scan'208";a="14946415" X-Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by fmvoesa106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 May 2024 23:27:13 -0700 X-CSE-ConnectionGUID: BeamgrDkTQq8DLVtaereig== X-CSE-MsgGUID: /CL32iMaRZmU/uRec9PC7g== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,147,1712646000"; d="scan'208";a="60305920" X-Received: from shwdejointd777.ccr.corp.intel.com ([10.239.58.116]) by fmviesa001.fm.intel.com with ESMTP; 08 May 2024 23:27:11 -0700 From: "Wenxing Hou" To: devel@edk2.groups.io Cc: Jiewen Yao , Yi Li Subject: [edk2-devel] [PATCH v3 07/11] CryptoPkg: Add more RSA related functions based on Mbedtls Date: Thu, 9 May 2024 14:26:56 +0800 Message-Id: <20240509062700.2062-8-wenxing.hou@intel.com> In-Reply-To: <20240509062700.2062-1-wenxing.hou@intel.com> References: <20240509062700.2062-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: Wed, 08 May 2024 23:27:12 -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: IpecAc1uNVcQhPdwrSHCtMtlx7686176AA= 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=ZugjrlEE; 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.7 as permitted sender) smtp.mailfrom=bounce@groups.io REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3D4177 Implement more RSA functions such as RsaPkcs1Sign based Mbedlts. Cc: Jiewen Yao Cc: Yi Li Signed-off-by: Wenxing Hou --- .../BaseCryptLibMbedTls/Pk/CryptPkcs1Oaep.c | 278 ++++++++++++++ .../BaseCryptLibMbedTls/Pk/CryptRsaExt.c | 352 ++++++++++++++++++ .../BaseCryptLibMbedTls/Pk/CryptRsaPssSign.c | 140 +++++++ 3 files changed, 770 insertions(+) create mode 100644 CryptoPkg/Library/BaseCryptLibMbedTls/Pk/CryptPkcs1Oaep= .c create mode 100644 CryptoPkg/Library/BaseCryptLibMbedTls/Pk/CryptRsaExt.c create mode 100644 CryptoPkg/Library/BaseCryptLibMbedTls/Pk/CryptRsaPssSig= n.c diff --git a/CryptoPkg/Library/BaseCryptLibMbedTls/Pk/CryptPkcs1Oaep.c b/Cr= yptoPkg/Library/BaseCryptLibMbedTls/Pk/CryptPkcs1Oaep.c new file mode 100644 index 0000000000..61ccdd78e6 --- /dev/null +++ b/CryptoPkg/Library/BaseCryptLibMbedTls/Pk/CryptPkcs1Oaep.c @@ -0,0 +1,278 @@ +/** @file=0D + This file contains UEFI wrapper functions for RSA PKCS1v2 OAEP encryptio= n routines.=0D +=0D + SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D + Copyright (c) 2024, Intel Corporation. All rights reserved.
=0D +=0D +**/=0D +=0D +#include "InternalCryptLib.h"=0D +#include =0D +#include =0D +#include =0D +=0D +/**=0D + Encrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will retu= rn the=0D + encrypted message in a newly allocated buffer.=0D +=0D + Things that can cause a failure include:=0D + - X509 key size does not match any known key size.=0D + - Fail to parse X509 certificate.=0D + - Fail to allocate an intermediate buffer.=0D + - Null pointer provided for a non-optional parameter.=0D + - Data size is too large for the provided key size (max size is a functi= on of key size=0D + and hash digest size).=0D +=0D + @param[in] PublicKey A pointer to the DER-encoded X509 certif= icate that=0D + will be used to encrypt the data.=0D + @param[in] PublicKeySize Size of the X509 cert buffer.=0D + @param[in] InData Data to be encrypted.=0D + @param[in] InDataSize Size of the data buffer.=0D + @param[in] PrngSeed [Optional] If provided, a pointer to a r= andom seed buffer=0D + to be used when initializing the PRNG. N= ULL otherwise.=0D + @param[in] PrngSeedSize [Optional] If provided, size of the rand= om seed buffer.=0D + 0 otherwise.=0D + @param[out] EncryptedData Pointer to an allocated buffer containin= g the encrypted=0D + message.=0D + @param[out] EncryptedDataSize Size of the encrypted message buffer.=0D +=0D + @retval TRUE Encryption was successful.=0D + @retval FALSE Encryption failed.=0D +=0D +**/=0D +BOOLEAN=0D +EFIAPI=0D +Pkcs1v2Encrypt (=0D + IN CONST UINT8 *PublicKey,=0D + IN UINTN PublicKeySize,=0D + IN UINT8 *InData,=0D + IN UINTN InDataSize,=0D + IN CONST UINT8 *PrngSeed OPTIONAL,=0D + IN UINTN PrngSeedSize OPTIONAL,=0D + OUT UINT8 **EncryptedData,=0D + OUT UINTN *EncryptedDataSize=0D + )=0D +{=0D + BOOLEAN Result;=0D + UINT32 Ret;=0D + UINT8 *OutData;=0D + mbedtls_x509_crt CertContext;=0D + mbedtls_rsa_context RsaContext;=0D +=0D + //=0D + // Check input parameters.=0D + //=0D + if ((PublicKey =3D=3D NULL) || (InData =3D=3D NULL) ||=0D + (EncryptedData =3D=3D NULL) || (EncryptedDataSize =3D=3D NULL))=0D + {=0D + return FALSE;=0D + }=0D +=0D + //=0D + // Check public key size.=0D + //=0D + if (PublicKeySize > UINT_MAX) {=0D + //=0D + // Public key size is too large for implementation.=0D + //=0D + return FALSE;=0D + }=0D +=0D + *EncryptedData =3D NULL;=0D + *EncryptedDataSize =3D 0;=0D + Result =3D FALSE;=0D + OutData =3D NULL;=0D +=0D + mbedtls_x509_crt_init (&CertContext);=0D +=0D + if (mbedtls_x509_crt_parse_der (&CertContext, PublicKey, (UINT32)PublicK= eySize) !=3D 0) {=0D + goto _Exit;=0D + }=0D +=0D + if (mbedtls_pk_get_type (&CertContext.pk) !=3D MBEDTLS_PK_RSA) {=0D + goto _Exit;=0D + }=0D +=0D + mbedtls_rsa_init (&RsaContext);=0D + if (mbedtls_rsa_set_padding (&RsaContext, MBEDTLS_RSA_PKCS_V21, MBEDTLS_= MD_NONE) !=3D 0) {=0D + goto _Exit;=0D + }=0D +=0D + Ret =3D mbedtls_rsa_copy (&RsaContext, mbedtls_pk_rsa (CertContext.pk));= =0D + if (Ret !=3D 0) {=0D + goto _Exit;=0D + }=0D +=0D + *EncryptedDataSize =3D RsaContext.len;=0D +=0D + //=0D + // Allocate a buffer for the output data.=0D + //=0D + OutData =3D AllocateZeroPool (*EncryptedDataSize);=0D + if (OutData =3D=3D NULL) {=0D + //=0D + // Fail to allocate the output buffer.=0D + //=0D + goto _Exit;=0D + }=0D +=0D + Ret =3D mbedtls_rsa_pkcs1_encrypt (=0D + &RsaContext,=0D + MbedtlsRand,=0D + NULL,=0D + InDataSize,=0D + InData,=0D + OutData=0D + );=0D + if (Ret !=3D 0) {=0D + FreePool (OutData);=0D + OutData =3D NULL;=0D + goto _Exit;=0D + }=0D +=0D + *EncryptedData =3D OutData;=0D + Result =3D TRUE;=0D +=0D +_Exit:=0D + //=0D + // Release Resources=0D + //=0D + if (&CertContext !=3D NULL) {=0D + mbedtls_x509_crt_free (&CertContext);=0D + }=0D +=0D + if (&RsaContext !=3D NULL) {=0D + mbedtls_rsa_free (&RsaContext);=0D + }=0D +=0D + return Result;=0D +}=0D +=0D +/**=0D + Encrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will retu= rn the=0D + encrypted message in a newly allocated buffer.=0D +=0D + Things that can cause a failure include:=0D + - X509 key size does not match any known key size.=0D + - Fail to allocate an intermediate buffer.=0D + - Null pointer provided for a non-optional parameter.=0D + - Data size is too large for the provided key size (max size is a functi= on of key size=0D + and hash digest size).=0D +=0D + @param[in] RsaContext A pointer to an RSA context created by R= saNew() and=0D + provisioned with a public key using RsaS= etKey().=0D + @param[in] InData Data to be encrypted.=0D + @param[in] InDataSize Size of the data buffer.=0D + @param[in] PrngSeed [Optional] If provided, a pointer to a r= andom seed buffer=0D + to be used when initializing the PRNG. N= ULL otherwise.=0D + @param[in] PrngSeedSize [Optional] If provided, size of the rand= om seed buffer.=0D + 0 otherwise.=0D + @param[in] DigestLen [Optional] If provided, size of the hash= used:=0D + SHA1_DIGEST_SIZE=0D + SHA256_DIGEST_SIZE=0D + SHA384_DIGEST_SIZE=0D + SHA512_DIGEST_SIZE=0D + 0 to use default (SHA1)=0D + @param[out] EncryptedData Pointer to an allocated buffer containin= g the encrypted=0D + message.=0D + @param[out] EncryptedDataSize Size of the encrypted message buffer.=0D +=0D + @retval TRUE Encryption was successful.=0D + @retval FALSE Encryption failed.=0D +=0D +**/=0D +BOOLEAN=0D +EFIAPI=0D +RsaOaepEncrypt (=0D + IN VOID *RsaContext,=0D + IN UINT8 *InData,=0D + IN UINTN InDataSize,=0D + IN CONST UINT8 *PrngSeed OPTIONAL,=0D + IN UINTN PrngSeedSize OPTIONAL,=0D + IN UINT16 DigestLen OPTIONAL,=0D + OUT UINT8 **EncryptedData,=0D + OUT UINTN *EncryptedDataSize=0D + )=0D +{=0D + ASSERT (FALSE);=0D + return FALSE;=0D +}=0D +=0D +/**=0D + Decrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will retu= rn the=0D + decrypted message in a newly allocated buffer.=0D +=0D + Things that can cause a failure include:=0D + - Fail to parse private key.=0D + - Fail to allocate an intermediate buffer.=0D + - Null pointer provided for a non-optional parameter.=0D +=0D + @param[in] PrivateKey A pointer to the DER-encoded private key= .=0D + @param[in] PrivateKeySize Size of the private key buffer.=0D + @param[in] EncryptedData Data to be decrypted.=0D + @param[in] EncryptedDataSize Size of the encrypted buffer.=0D + @param[out] OutData Pointer to an allocated buffer containin= g the encrypted=0D + message.=0D + @param[out] OutDataSize Size of the encrypted message buffer.=0D +=0D + @retval TRUE Encryption was successful.=0D + @retval FALSE Encryption failed.=0D +=0D +**/=0D +BOOLEAN=0D +EFIAPI=0D +Pkcs1v2Decrypt (=0D + IN CONST UINT8 *PrivateKey,=0D + IN UINTN PrivateKeySize,=0D + IN UINT8 *EncryptedData,=0D + IN UINTN EncryptedDataSize,=0D + OUT UINT8 **OutData,=0D + OUT UINTN *OutDataSize=0D + )=0D +{=0D + ASSERT (FALSE);=0D + return FALSE;=0D +}=0D +=0D +/**=0D + Decrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will retu= rn the=0D + decrypted message in a newly allocated buffer.=0D +=0D + Things that can cause a failure include:=0D + - Fail to parse private key.=0D + - Fail to allocate an intermediate buffer.=0D + - Null pointer provided for a non-optional parameter.=0D +=0D + @param[in] RsaContext A pointer to an RSA context created by R= saNew() and=0D + provisioned with a private key using Rsa= SetKey().=0D + @param[in] EncryptedData Data to be decrypted.=0D + @param[in] EncryptedDataSize Size of the encrypted buffer.=0D + @param[in] DigestLen [Optional] If provided, size of the hash= used:=0D + SHA1_DIGEST_SIZE=0D + SHA256_DIGEST_SIZE=0D + SHA384_DIGEST_SIZE=0D + SHA512_DIGEST_SIZE=0D + 0 to use default (SHA1)=0D + @param[out] OutData Pointer to an allocated buffer containin= g the encrypted=0D + message.=0D + @param[out] OutDataSize Size of the encrypted message buffer.=0D +=0D + @retval TRUE Encryption was successful.=0D + @retval FALSE Encryption failed.=0D +=0D +**/=0D +BOOLEAN=0D +EFIAPI=0D +RsaOaepDecrypt (=0D + IN VOID *RsaContext,=0D + IN UINT8 *EncryptedData,=0D + IN UINTN EncryptedDataSize,=0D + IN UINT16 DigestLen OPTIONAL,=0D + OUT UINT8 **OutData,=0D + OUT UINTN *OutDataSize=0D + )=0D +{=0D + ASSERT (FALSE);=0D + return FALSE;=0D +}=0D diff --git a/CryptoPkg/Library/BaseCryptLibMbedTls/Pk/CryptRsaExt.c b/Crypt= oPkg/Library/BaseCryptLibMbedTls/Pk/CryptRsaExt.c new file mode 100644 index 0000000000..5fe76f146d --- /dev/null +++ b/CryptoPkg/Library/BaseCryptLibMbedTls/Pk/CryptRsaExt.c @@ -0,0 +1,352 @@ +/** @file=0D + RSA Asymmetric Cipher Wrapper Implementation over MbedTLS.=0D +=0D + This file implements following APIs which provide more capabilities for = RSA:=0D + 1) RsaGetKey=0D + 2) RsaGenerateKey=0D + 3) RsaCheckKey=0D + 4) RsaPkcs1Sign=0D +=0D + RFC 8017 - PKCS #1: RSA Cryptography Specifications Version 2.2=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 + Gets the tag-designated RSA key component from the established RSA conte= xt.=0D +=0D + This function retrieves the tag-designated RSA key component from the=0D + established RSA context as a non-negative integer (octet string format=0D + represented in RSA PKCS#1).=0D + If specified key component has not been set or has been cleared, then re= turned=0D + BnSize is set to 0.=0D + If the BigNumber buffer is too small to hold the contents of the key, FA= LSE=0D + is returned and BnSize is set to the required buffer size to obtain the = key.=0D +=0D + If RsaContext is NULL, then return FALSE.=0D + If BnSize is NULL, then return FALSE.=0D + If BnSize is large enough but BigNumber 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[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 TRUE RSA key component was retrieved successfully.=0D + @retval FALSE Invalid RSA key component tag.=0D + @retval FALSE BnSize is too small.=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 + mbedtls_rsa_context *RsaKey;=0D + INT32 Ret;=0D + mbedtls_mpi Value;=0D + UINTN Size;=0D +=0D + //=0D + // Check input parameters.=0D + //=0D + if ((RsaContext =3D=3D NULL) || (*BnSize > INT_MAX)) {=0D + return FALSE;=0D + }=0D +=0D + //=0D + // Init mbedtls_mpi=0D + //=0D + mbedtls_mpi_init (&Value);=0D + Size =3D *BnSize;=0D + *BnSize =3D 0;=0D +=0D + RsaKey =3D (mbedtls_rsa_context *)RsaContext;=0D +=0D + switch (KeyTag) {=0D + case RsaKeyN:=0D + Ret =3D mbedtls_rsa_export (RsaKey, &Value, NULL, NULL, NULL, NULL);= =0D + break;=0D + case RsaKeyE:=0D + Ret =3D mbedtls_rsa_export (RsaKey, NULL, NULL, NULL, NULL, &Value);= =0D + break;=0D + case RsaKeyD:=0D + Ret =3D mbedtls_rsa_export (RsaKey, NULL, NULL, NULL, &Value, NULL);= =0D + break;=0D + case RsaKeyQ:=0D + Ret =3D mbedtls_rsa_export (RsaKey, NULL, NULL, &Value, NULL, NULL);= =0D + break;=0D + case RsaKeyP:=0D + Ret =3D mbedtls_rsa_export (RsaKey, NULL, &Value, NULL, NULL, NULL);= =0D + break;=0D + case RsaKeyDp:=0D + case RsaKeyDq:=0D + case RsaKeyQInv:=0D + default:=0D + Ret =3D -1;=0D + break;=0D + }=0D +=0D + if (Ret !=3D 0) {=0D + goto End;=0D + }=0D +=0D + if (mbedtls_mpi_size (&Value) =3D=3D 0) {=0D + Ret =3D 0;=0D + goto End;=0D + }=0D +=0D + *BnSize =3D Size;=0D +=0D + Size =3D mbedtls_mpi_size (&Value);=0D + if (*BnSize < Size) {=0D + Ret =3D 1;=0D + *BnSize =3D Size;=0D + goto End;=0D + }=0D +=0D + if (BigNumber =3D=3D NULL) {=0D + Ret =3D 0;=0D + *BnSize =3D Size;=0D + goto End;=0D + }=0D +=0D + if ((BigNumber !=3D NULL) && (Ret =3D=3D 0)) {=0D + Ret =3D mbedtls_mpi_write_binary (&Value, BigNumber, Size);=0D + *BnSize =3D Size;=0D + }=0D +=0D +End:=0D + mbedtls_mpi_free (&Value);=0D + return Ret =3D=3D 0;=0D +}=0D +=0D +/**=0D + Generates RSA key components.=0D +=0D + This function generates RSA key components. It takes RSA public exponent= Pe and=0D + length in bits of RSA modulus N as input, and generates all key componen= ts.=0D + If PublicExponent is NULL, the default RSA public exponent (0x10001) wil= l be used.=0D +=0D + Before this function can be invoked, pseudorandom number generator must = be correctly=0D + initialized by RandomSeed().=0D +=0D + If RsaContext is NULL, then return FALSE.=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 TRUE RSA key component was generated successfully.=0D + @retval FALSE Invalid RSA key component tag.=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 + INT32 Ret;=0D + mbedtls_rsa_context *Rsa;=0D + INT32 Pe;=0D +=0D + //=0D + // Check input parameters.=0D + //=0D + if ((RsaContext =3D=3D NULL) || (ModulusLength > INT_MAX) || (PublicExpo= nentSize > INT_MAX)) {=0D + return FALSE;=0D + }=0D +=0D + Rsa =3D (mbedtls_rsa_context *)RsaContext;=0D +=0D + if (PublicExponent =3D=3D NULL) {=0D + Pe =3D 0x10001;=0D + } else {=0D + if (PublicExponentSize =3D=3D 0) {=0D + return FALSE;=0D + }=0D +=0D + switch (PublicExponentSize) {=0D + case 1:=0D + Pe =3D PublicExponent[0];=0D + break;=0D + case 2:=0D + Pe =3D PublicExponent[0] << 8 | PublicExponent[1];=0D + break;=0D + case 3:=0D + Pe =3D PublicExponent[0] << 16 | PublicExponent[1] << 8 |=0D + PublicExponent[2];=0D + break;=0D + case 4:=0D + Pe =3D PublicExponent[0] << 24 | PublicExponent[1] << 16 |=0D + PublicExponent[2] << 8 | PublicExponent[3];=0D + break;=0D + default:=0D + return FALSE;=0D + }=0D + }=0D +=0D + Ret =3D mbedtls_rsa_gen_key (=0D + Rsa,=0D + MbedtlsRand,=0D + NULL,=0D + (UINT32)ModulusLength,=0D + Pe=0D + );=0D +=0D + return Ret =3D=3D 0;=0D +}=0D +=0D +/**=0D + Validates key components of RSA context.=0D + NOTE: This function performs integrity checks on all the RSA key materia= l, so=0D + the RSA key structure must contain all the private key data.=0D +=0D + This function validates key components of RSA context in following aspec= ts:=0D + - Whether p is a prime=0D + - Whether q is a prime=0D + - Whether n =3D p * q=0D + - Whether d*e =3D 1 mod lcm(p-1,q-1)=0D +=0D + If RsaContext is NULL, then return FALSE.=0D +=0D + @param[in] RsaContext Pointer to RSA context to check.=0D +=0D + @retval TRUE RSA key components are valid.=0D + @retval FALSE RSA key components are not valid.=0D +=0D +**/=0D +BOOLEAN=0D +EFIAPI=0D +RsaCheckKey (=0D + IN VOID *RsaContext=0D + )=0D +{=0D + if (RsaContext =3D=3D NULL) {=0D + return FALSE;=0D + }=0D +=0D + UINT32 Ret;=0D +=0D + Ret =3D mbedtls_rsa_complete (RsaContext);=0D + if (Ret =3D=3D 0) {=0D + Ret =3D mbedtls_rsa_check_privkey (RsaContext);=0D + }=0D +=0D + return Ret =3D=3D 0;=0D +}=0D +=0D +/**=0D + Carries out the RSA-SSA signature generation with EMSA-PKCS1-v1_5 encodi= ng scheme.=0D +=0D + This function carries out the RSA-SSA signature generation with EMSA-PKC= S1-v1_5 encoding scheme defined in=0D + RSA PKCS#1.=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 MessageHash 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 + If SigSize is large enough but Signature is NULL, then return FALSE.=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 TRUE Signature successfully generated in PKCS1-v1_5.=0D + @retval FALSE Signature generation failed.=0D + @retval FALSE SigSize is too small.=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 + INT32 Ret;=0D + mbedtls_md_type_t MdAlg;=0D +=0D + if ((RsaContext =3D=3D NULL) || (MessageHash =3D=3D NULL)) {=0D + return FALSE;=0D + }=0D +=0D + if (mbedtls_rsa_complete ((mbedtls_rsa_context *)RsaContext) !=3D 0) {=0D + return FALSE;=0D + }=0D +=0D + switch (HashSize) {=0D + #ifndef DISABLE_SHA1_DEPRECATED_INTERFACES=0D + case SHA1_DIGEST_SIZE:=0D + MdAlg =3D MBEDTLS_MD_SHA1;=0D + break;=0D + #endif=0D +=0D + case SHA256_DIGEST_SIZE:=0D + MdAlg =3D MBEDTLS_MD_SHA256;=0D + break;=0D +=0D + case SHA384_DIGEST_SIZE:=0D + MdAlg =3D MBEDTLS_MD_SHA384;=0D + break;=0D +=0D + case SHA512_DIGEST_SIZE:=0D + MdAlg =3D MBEDTLS_MD_SHA512;=0D + break;=0D +=0D + default:=0D + return FALSE;=0D + }=0D +=0D + if (mbedtls_rsa_get_len (RsaContext) > *SigSize) {=0D + *SigSize =3D mbedtls_rsa_get_len (RsaContext);=0D + return FALSE;=0D + }=0D +=0D + if (Signature =3D=3D NULL) {=0D + return FALSE;=0D + }=0D +=0D + Ret =3D mbedtls_rsa_set_padding (RsaContext, MBEDTLS_RSA_PKCS_V15, MdAlg= );=0D + if (Ret !=3D 0) {=0D + return FALSE;=0D + }=0D +=0D + Ret =3D mbedtls_rsa_pkcs1_sign (=0D + RsaContext,=0D + MbedtlsRand,=0D + NULL,=0D + MdAlg,=0D + (UINT32)HashSize,=0D + MessageHash,=0D + Signature=0D + );=0D + if (Ret !=3D 0) {=0D + return FALSE;=0D + }=0D +=0D + *SigSize =3D mbedtls_rsa_get_len (RsaContext);=0D + return TRUE;=0D +}=0D diff --git a/CryptoPkg/Library/BaseCryptLibMbedTls/Pk/CryptRsaPssSign.c b/C= ryptoPkg/Library/BaseCryptLibMbedTls/Pk/CryptRsaPssSign.c new file mode 100644 index 0000000000..5555f9261e --- /dev/null +++ b/CryptoPkg/Library/BaseCryptLibMbedTls/Pk/CryptRsaPssSign.c @@ -0,0 +1,140 @@ +/** @file=0D + RSA PSS Asymmetric Cipher Wrapper Implementation over MbedTLS.=0D +=0D + This file implements following APIs which provide basic capabilities for= RSA:=0D + 1) RsaPssSign=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 +#include =0D +#include =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 + INT32 Ret;=0D + mbedtls_md_type_t MdAlg;=0D + UINT8 HashValue[SHA512_DIGEST_SIZE];=0D +=0D + if (RsaContext =3D=3D NULL) {=0D + return FALSE;=0D + }=0D +=0D + if (mbedtls_rsa_complete ((mbedtls_rsa_context *)RsaContext) !=3D 0) {=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 + ZeroMem (HashValue, DigestLen);=0D +=0D + switch (DigestLen) {=0D + case SHA256_DIGEST_SIZE:=0D + MdAlg =3D MBEDTLS_MD_SHA256;=0D + if (mbedtls_sha256 (Message, MsgSize, HashValue, FALSE) !=3D 0) {=0D + return FALSE;=0D + }=0D +=0D + break;=0D +=0D + case SHA384_DIGEST_SIZE:=0D + MdAlg =3D MBEDTLS_MD_SHA384;=0D + if (mbedtls_sha512 (Message, MsgSize, HashValue, TRUE) !=3D 0) {=0D + return FALSE;=0D + }=0D +=0D + break;=0D +=0D + case SHA512_DIGEST_SIZE:=0D + MdAlg =3D MBEDTLS_MD_SHA512;=0D + if (mbedtls_sha512 (Message, MsgSize, HashValue, FALSE) !=3D 0) {=0D + return FALSE;=0D + }=0D +=0D + break;=0D +=0D + default:=0D + return FALSE;=0D + }=0D +=0D + if (Signature =3D=3D NULL) {=0D + //=0D + // If Signature is NULL, return safe SignatureSize=0D + //=0D + *SigSize =3D MBEDTLS_MPI_MAX_SIZE;=0D + return FALSE;=0D + }=0D +=0D + Ret =3D mbedtls_rsa_set_padding (RsaContext, MBEDTLS_RSA_PKCS_V21, MdAlg= );=0D + if (Ret !=3D 0) {=0D + return FALSE;=0D + }=0D +=0D + Ret =3D mbedtls_rsa_rsassa_pss_sign (=0D + RsaContext,=0D + MbedtlsRand,=0D + NULL,=0D + MdAlg,=0D + (UINT32)DigestLen,=0D + HashValue,=0D + Signature=0D + );=0D + if (Ret !=3D 0) {=0D + return FALSE;=0D + }=0D +=0D + *SigSize =3D ((mbedtls_rsa_context *)RsaContext)->len;=0D + return TRUE;=0D +}=0D --=20 2.26.2.windows.1 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#118738): https://edk2.groups.io/g/devel/message/118738 Mute This Topic: https://groups.io/mt/105996833/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-