]> git.proxmox.com Git - mirror_edk2.git/commitdiff
CryptoPkg: BaseCryptLib: Add RSA PSS verify support
authorSachin Agrawal <sachin.agrawal@intel.com>
Fri, 9 Apr 2021 22:14:04 +0000 (06:14 +0800)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Fri, 14 May 2021 03:35:33 +0000 (03:35 +0000)
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3314

This patch uses Openssl's EVP API's to perform RSASSA-PSS verification
of a binary blob.

Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Xiaoyu Lu <xiaoyux.lu@intel.com>
Cc: Guomin Jiang <guomin.jiang@intel.com>
Signed-off-by: Sachin Agrawal <sachin.agrawal@intel.com>
Reviewed-by: Jiewen Yao <jiewen.yao@intel.com>
21 files changed:
CryptoPkg/Include/Library/BaseCryptLib.h
CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf
CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaPss.c [new file with mode: 0644]
CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaPssNull.c [new file with mode: 0644]
CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaPssSign.c [new file with mode: 0644]
CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaPssSignNull.c [new file with mode: 0644]
CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf
CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf
CryptoPkg/Library/BaseCryptLib/UnitTestHostBaseCryptLib.inf
CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf
CryptoPkg/Library/BaseCryptLibNull/Pk/CryptRsaPssNull.c [new file with mode: 0644]
CryptoPkg/Library/BaseCryptLibNull/Pk/CryptRsaPssSignNull.c [new file with mode: 0644]
CryptoPkg/Library/BaseCryptLibOnProtocolPpi/CryptLib.c
CryptoPkg/Private/Protocol/Crypto.h
CryptoPkg/Test/UnitTest/Library/BaseCryptLib/BaseCryptLibUnitTests.c
CryptoPkg/Test/UnitTest/Library/BaseCryptLib/RsaPssTests.c [new file with mode: 0644]
CryptoPkg/Test/UnitTest/Library/BaseCryptLib/RsaTests.c
CryptoPkg/Test/UnitTest/Library/BaseCryptLib/TestBaseCryptLib.h
CryptoPkg/Test/UnitTest/Library/BaseCryptLib/TestBaseCryptLibHost.inf
CryptoPkg/Test/UnitTest/Library/BaseCryptLib/TestBaseCryptLibShell.inf

index 496121e6a4ed50bdc1a62ba716cd8fb95f337a1f..8c7d5922ef96ce6b55943af3586e3396aea805ce 100644 (file)
@@ -1363,6 +1363,80 @@ RsaPkcs1Verify (
   IN  UINTN        SigSize\r
   );\r
 \r
+/**\r
+  Carries out the RSA-SSA signature generation with EMSA-PSS encoding scheme.\r
+\r
+  This function carries out the RSA-SSA signature generation with EMSA-PSS encoding scheme defined in\r
+  RFC 8017.\r
+  Mask generation function is the same as the message digest algorithm.\r
+  If the Signature buffer is too small to hold the contents of signature, FALSE\r
+  is returned and SigSize is set to the required buffer size to obtain the signature.\r
+\r
+  If RsaContext is NULL, then return FALSE.\r
+  If Message is NULL, then return FALSE.\r
+  If MsgSize is zero or > INT_MAX, then return FALSE.\r
+  If DigestLen is NOT 32, 48 or 64, return FALSE.\r
+  If SaltLen is < DigestLen, then return FALSE.\r
+  If SigSize is large enough but Signature is NULL, then return FALSE.\r
+  If this interface is not supported, then return FALSE.\r
+\r
+  @param[in]      RsaContext   Pointer to RSA context for signature generation.\r
+  @param[in]      Message      Pointer to octet message to be signed.\r
+  @param[in]      MsgSize      Size of the message in bytes.\r
+  @param[in]      DigestLen    Length of the digest in bytes to be used for RSA signature operation.\r
+  @param[in]      SaltLen      Length of the salt in bytes to be used for PSS encoding.\r
+  @param[out]     Signature    Pointer to buffer to receive RSA PSS signature.\r
+  @param[in, out] SigSize      On input, the size of Signature buffer in bytes.\r
+                               On output, the size of data returned in Signature buffer in bytes.\r
+\r
+  @retval  TRUE   Signature successfully generated in RSASSA-PSS.\r
+  @retval  FALSE  Signature generation failed.\r
+  @retval  FALSE  SigSize is too small.\r
+  @retval  FALSE  This interface is not supported.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+RsaPssSign (\r
+  IN      VOID         *RsaContext,\r
+  IN      CONST UINT8  *Message,\r
+  IN      UINTN        MsgSize,\r
+  IN      UINT16       DigestLen,\r
+  IN      UINT16       SaltLen,\r
+  OUT     UINT8        *Signature,\r
+  IN OUT  UINTN        *SigSize\r
+  );\r
+\r
+/**\r
+  Verifies the RSA signature with RSASSA-PSS signature scheme defined in RFC 8017.\r
+  Implementation determines salt length automatically from the signature encoding.\r
+  Mask generation function is the same as the message digest algorithm.\r
+  Salt length should atleast be equal to digest length.\r
+\r
+  @param[in]  RsaContext      Pointer to RSA context for signature verification.\r
+  @param[in]  Message         Pointer to octet message to be verified.\r
+  @param[in]  MsgSize         Size of the message in bytes.\r
+  @param[in]  Signature       Pointer to RSASSA-PSS signature to be verified.\r
+  @param[in]  SigSize         Size of signature in bytes.\r
+  @param[in]  DigestLen       Length of digest for RSA operation.\r
+  @param[in]  SaltLen         Salt length for PSS encoding.\r
+\r
+  @retval  TRUE   Valid signature encoded in RSASSA-PSS.\r
+  @retval  FALSE  Invalid signature or invalid RSA context.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+RsaPssVerify (\r
+  IN  VOID         *RsaContext,\r
+  IN  CONST UINT8  *Message,\r
+  IN  UINTN        MsgSize,\r
+  IN  CONST UINT8  *Signature,\r
+  IN  UINTN        SigSize,\r
+  IN  UINT16       DigestLen,\r
+  IN  UINT16       SaltLen\r
+  );\r
+\r
 /**\r
   Retrieve the RSA Private Key from the password-protected PEM key data.\r
 \r
index 4aae2aba95d69a6f130c62908349d367cf035f21..49703fa4c9638b16654af144e6789ef7a32b7ed9 100644 (file)
@@ -49,6 +49,8 @@
   Pk/CryptX509.c\r
   Pk/CryptAuthenticode.c\r
   Pk/CryptTs.c\r
+  Pk/CryptRsaPss.c\r
+  Pk/CryptRsaPssSign.c\r
   Pem/CryptPem.c\r
 \r
   SysCall/CrtWrapper.c\r
index 7509e427302856cd7a5776966b2c44539fda8097..0cab5f3ce36c20891a11505bf24e82c052f483a9 100644 (file)
@@ -55,6 +55,8 @@
   Pk/CryptX509Null.c\r
   Pk/CryptAuthenticodeNull.c\r
   Pk/CryptTsNull.c\r
+  Pk/CryptRsaPss.c\r
+  Pk/CryptRsaPssSignNull.c\r
   Pem/CryptPemNull.c\r
   Rand/CryptRandNull.c\r
 \r
diff --git a/CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaPss.c b/CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaPss.c
new file mode 100644 (file)
index 0000000..4009d37
--- /dev/null
@@ -0,0 +1,151 @@
+/** @file\r
+  RSA Asymmetric Cipher Wrapper Implementation over OpenSSL.\r
+\r
+  This file implements following APIs which provide basic capabilities for RSA:\r
+  1) RsaPssVerify\r
+\r
+Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
+\r
+**/\r
+\r
+#include "InternalCryptLib.h"\r
+\r
+#include <openssl/bn.h>\r
+#include <openssl/rsa.h>\r
+#include <openssl/objects.h>\r
+#include <openssl/evp.h>\r
+\r
+\r
+/**\r
+  Retrieve a pointer to EVP message digest object.\r
+\r
+  @param[in]  DigestLen   Length of the message digest.\r
+\r
+**/\r
+STATIC\r
+const\r
+EVP_MD*\r
+GetEvpMD (\r
+  IN UINT16 DigestLen\r
+  )\r
+{\r
+  switch (DigestLen){\r
+    case SHA256_DIGEST_SIZE:\r
+      return EVP_sha256();\r
+      break;\r
+    case SHA384_DIGEST_SIZE:\r
+      return EVP_sha384();\r
+      break;\r
+    case SHA512_DIGEST_SIZE:\r
+      return EVP_sha512();\r
+      break;\r
+    default:\r
+      return NULL;\r
+  }\r
+}\r
+\r
+\r
+/**\r
+  Verifies the RSA signature with RSASSA-PSS signature scheme defined in RFC 8017.\r
+  Implementation determines salt length automatically from the signature encoding.\r
+  Mask generation function is the same as the message digest algorithm.\r
+  Salt length should atleast be equal to digest length.\r
+\r
+  @param[in]  RsaContext      Pointer to RSA context for signature verification.\r
+  @param[in]  Message         Pointer to octet message to be verified.\r
+  @param[in]  MsgSize         Size of the message in bytes.\r
+  @param[in]  Signature       Pointer to RSASSA-PSS signature to be verified.\r
+  @param[in]  SigSize         Size of signature in bytes.\r
+  @param[in]  DigestLen       Length of digest for RSA operation.\r
+  @param[in]  SaltLen         Salt length for PSS encoding.\r
+\r
+  @retval  TRUE   Valid signature encoded in RSASSA-PSS.\r
+  @retval  FALSE  Invalid signature or invalid RSA context.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+RsaPssVerify (\r
+  IN  VOID         *RsaContext,\r
+  IN  CONST UINT8  *Message,\r
+  IN  UINTN        MsgSize,\r
+  IN  CONST UINT8  *Signature,\r
+  IN  UINTN        SigSize,\r
+  IN  UINT16       DigestLen,\r
+  IN  UINT16       SaltLen\r
+  )\r
+{\r
+  BOOLEAN Result;\r
+  EVP_PKEY *EvpRsaKey;\r
+  EVP_MD_CTX *EvpVerifyCtx;\r
+  EVP_PKEY_CTX *KeyCtx;\r
+  CONST EVP_MD  *HashAlg;\r
+\r
+  EvpRsaKey = NULL;\r
+  EvpVerifyCtx = NULL;\r
+  KeyCtx = NULL;\r
+  HashAlg = NULL;\r
+\r
+  if (RsaContext == NULL) {\r
+    return FALSE;\r
+  }\r
+  if (Message == NULL || MsgSize == 0 || MsgSize > INT_MAX) {\r
+    return FALSE;\r
+  }\r
+  if (Signature == NULL || SigSize == 0 || SigSize > INT_MAX) {\r
+    return FALSE;\r
+  }\r
+  if (SaltLen < DigestLen) {\r
+    return FALSE;\r
+  }\r
+\r
+  HashAlg = GetEvpMD(DigestLen);\r
+\r
+  if (HashAlg == NULL) {\r
+    return FALSE;\r
+  }\r
+\r
+  EvpRsaKey = EVP_PKEY_new();\r
+  if (EvpRsaKey == NULL) {\r
+    goto _Exit;\r
+  }\r
+\r
+  EVP_PKEY_set1_RSA(EvpRsaKey, RsaContext);\r
+\r
+  EvpVerifyCtx = EVP_MD_CTX_create();\r
+  if (EvpVerifyCtx == NULL) {\r
+    goto _Exit;\r
+  }\r
+\r
+  Result = EVP_DigestVerifyInit(EvpVerifyCtx, &KeyCtx, HashAlg, NULL, EvpRsaKey) > 0;\r
+  if (KeyCtx == NULL) {\r
+    goto _Exit;\r
+  }\r
+\r
+  if (Result) {\r
+    Result = EVP_PKEY_CTX_set_rsa_padding(KeyCtx, RSA_PKCS1_PSS_PADDING) > 0;\r
+  }\r
+  if (Result) {\r
+    Result = EVP_PKEY_CTX_set_rsa_pss_saltlen(KeyCtx, SaltLen) > 0;\r
+  }\r
+  if (Result) {\r
+    Result = EVP_PKEY_CTX_set_rsa_mgf1_md(KeyCtx, HashAlg) > 0;\r
+  }\r
+  if (Result) {\r
+    Result = EVP_DigestVerifyUpdate(EvpVerifyCtx, Message, (UINT32)MsgSize) > 0;\r
+  }\r
+  if (Result) {\r
+    Result = EVP_DigestVerifyFinal(EvpVerifyCtx, Signature, (UINT32)SigSize) > 0;\r
+  }\r
+\r
+_Exit :\r
+  if (EvpRsaKey != NULL) {\r
+    EVP_PKEY_free(EvpRsaKey);\r
+  }\r
+  if (EvpVerifyCtx != NULL) {\r
+    EVP_MD_CTX_destroy(EvpVerifyCtx);\r
+  }\r
+\r
+  return Result;\r
+}\r
diff --git a/CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaPssNull.c b/CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaPssNull.c
new file mode 100644 (file)
index 0000000..69c6889
--- /dev/null
@@ -0,0 +1,46 @@
+/** @file\r
+  RSA-PSS Asymmetric Cipher Wrapper Implementation over OpenSSL.\r
+\r
+  This file does not provide real capabilities for following APIs in RSA handling:\r
+  1) RsaPssVerify\r
+\r
+Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
+\r
+**/\r
+\r
+#include "InternalCryptLib.h"\r
+\r
+/**\r
+  Verifies the RSA signature with RSASSA-PSS signature scheme defined in RFC 8017.\r
+  Implementation determines salt length automatically from the signature encoding.\r
+  Mask generation function is the same as the message digest algorithm.\r
+  Salt length should atleast be equal to digest length.\r
+\r
+  @param[in]  RsaContext      Pointer to RSA context for signature verification.\r
+  @param[in]  Message         Pointer to octet message to be verified.\r
+  @param[in]  MsgSize         Size of the message in bytes.\r
+  @param[in]  Signature       Pointer to RSASSA-PSS signature to be verified.\r
+  @param[in]  SigSize         Size of signature in bytes.\r
+  @param[in]  DigestLen       Length of digest for RSA operation.\r
+  @param[in]  SaltLen         Salt length for PSS encoding.\r
+\r
+  @retval  TRUE   Valid signature encoded in RSASSA-PSS.\r
+  @retval  FALSE  Invalid signature or invalid RSA context.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+RsaPssVerify (\r
+  IN  VOID         *RsaContext,\r
+  IN  CONST UINT8  *Message,\r
+  IN  UINTN        MsgSize,\r
+  IN  CONST UINT8  *Signature,\r
+  IN  UINTN        SigSize,\r
+  IN  UINT16       DigestLen,\r
+  IN  UINT16       SaltLen\r
+  )\r
+{\r
+  ASSERT (FALSE);\r
+  return FALSE;\r
+}\r
diff --git a/CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaPssSign.c b/CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaPssSign.c
new file mode 100644 (file)
index 0000000..b66b6f7
--- /dev/null
@@ -0,0 +1,174 @@
+/** @file\r
+  RSA PSS Asymmetric Cipher Wrapper Implementation over OpenSSL.\r
+\r
+  This file implements following APIs which provide basic capabilities for RSA:\r
+  1) RsaPssSign\r
+\r
+Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
+\r
+**/\r
+\r
+#include "InternalCryptLib.h"\r
+\r
+#include <openssl/bn.h>\r
+#include <openssl/rsa.h>\r
+#include <openssl/objects.h>\r
+#include <openssl/evp.h>\r
+\r
+\r
+/**\r
+  Retrieve a pointer to EVP message digest object.\r
+\r
+  @param[in]  DigestLen   Length of the message digest.\r
+\r
+**/\r
+STATIC\r
+const\r
+EVP_MD*\r
+GetEvpMD (\r
+  IN UINT16 DigestLen\r
+  )\r
+{\r
+  switch (DigestLen){\r
+    case SHA256_DIGEST_SIZE:\r
+      return EVP_sha256();\r
+      break;\r
+    case SHA384_DIGEST_SIZE:\r
+      return EVP_sha384();\r
+      break;\r
+    case SHA512_DIGEST_SIZE:\r
+      return EVP_sha512();\r
+      break;\r
+    default:\r
+      return NULL;\r
+  }\r
+}\r
+\r
+\r
+/**\r
+  Carries out the RSA-SSA signature generation with EMSA-PSS encoding scheme.\r
+\r
+  This function carries out the RSA-SSA signature generation with EMSA-PSS encoding scheme defined in\r
+  RFC 8017.\r
+  Mask generation function is the same as the message digest algorithm.\r
+  If the Signature buffer is too small to hold the contents of signature, FALSE\r
+  is returned and SigSize is set to the required buffer size to obtain the signature.\r
+\r
+  If RsaContext is NULL, then return FALSE.\r
+  If Message is NULL, then return FALSE.\r
+  If MsgSize is zero or > INT_MAX, then return FALSE.\r
+  If DigestLen is NOT 32, 48 or 64, return FALSE.\r
+  If SaltLen is < DigestLen, then return FALSE.\r
+  If SigSize is large enough but Signature is NULL, then return FALSE.\r
+  If this interface is not supported, then return FALSE.\r
+\r
+  @param[in]      RsaContext   Pointer to RSA context for signature generation.\r
+  @param[in]      Message      Pointer to octet message to be signed.\r
+  @param[in]      MsgSize      Size of the message in bytes.\r
+  @param[in]      DigestLen    Length of the digest in bytes to be used for RSA signature operation.\r
+  @param[in]      SaltLen      Length of the salt in bytes to be used for PSS encoding.\r
+  @param[out]     Signature    Pointer to buffer to receive RSA PSS signature.\r
+  @param[in, out] SigSize      On input, the size of Signature buffer in bytes.\r
+                               On output, the size of data returned in Signature buffer in bytes.\r
+\r
+  @retval  TRUE   Signature successfully generated in RSASSA-PSS.\r
+  @retval  FALSE  Signature generation failed.\r
+  @retval  FALSE  SigSize is too small.\r
+  @retval  FALSE  This interface is not supported.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+RsaPssSign (\r
+  IN      VOID         *RsaContext,\r
+  IN      CONST UINT8  *Message,\r
+  IN      UINTN        MsgSize,\r
+  IN      UINT16       DigestLen,\r
+  IN      UINT16       SaltLen,\r
+  OUT     UINT8        *Signature,\r
+  IN OUT  UINTN        *SigSize\r
+  )\r
+{\r
+  BOOLEAN               Result;\r
+  UINTN                 RsaSigSize;\r
+  EVP_PKEY              *EvpRsaKey;\r
+  EVP_MD_CTX            *EvpVerifyCtx;\r
+  EVP_PKEY_CTX          *KeyCtx;\r
+  CONST EVP_MD          *HashAlg;\r
+\r
+  EvpRsaKey = NULL;\r
+  EvpVerifyCtx = NULL;\r
+  KeyCtx = NULL;\r
+  HashAlg = NULL;\r
+\r
+  if (RsaContext == NULL) {\r
+    return FALSE;\r
+  }\r
+  if (Message == NULL || MsgSize == 0 || MsgSize > INT_MAX) {\r
+    return FALSE;\r
+  }\r
+\r
+  RsaSigSize = RSA_size (RsaContext);\r
+  if (*SigSize < RsaSigSize) {\r
+    *SigSize = RsaSigSize;\r
+    return FALSE;\r
+  }\r
+\r
+  if (Signature == NULL) {\r
+    return FALSE;\r
+  }\r
+\r
+  if (SaltLen < DigestLen) {\r
+    return FALSE;\r
+  }\r
+\r
+  HashAlg = GetEvpMD(DigestLen);\r
+\r
+  if (HashAlg == NULL) {\r
+    return FALSE;\r
+  }\r
+\r
+  EvpRsaKey = EVP_PKEY_new();\r
+  if (EvpRsaKey == NULL) {\r
+    goto _Exit;\r
+  }\r
+\r
+  EVP_PKEY_set1_RSA(EvpRsaKey, RsaContext);\r
+\r
+  EvpVerifyCtx = EVP_MD_CTX_create();\r
+  if (EvpVerifyCtx == NULL) {\r
+    goto _Exit;\r
+  }\r
+\r
+  Result = EVP_DigestSignInit(EvpVerifyCtx, &KeyCtx, HashAlg, NULL, EvpRsaKey) > 0;\r
+  if (KeyCtx == NULL) {\r
+    goto _Exit;\r
+  }\r
+\r
+  if (Result) {\r
+    Result = EVP_PKEY_CTX_set_rsa_padding(KeyCtx, RSA_PKCS1_PSS_PADDING) > 0;\r
+  }\r
+  if (Result) {\r
+    Result = EVP_PKEY_CTX_set_rsa_pss_saltlen(KeyCtx, SaltLen) > 0;\r
+  }\r
+  if (Result) {\r
+    Result = EVP_PKEY_CTX_set_rsa_mgf1_md(KeyCtx, HashAlg) > 0;\r
+  }\r
+  if (Result) {\r
+    Result = EVP_DigestSignUpdate(EvpVerifyCtx, Message, (UINT32)MsgSize) > 0;\r
+  }\r
+  if (Result) {\r
+    Result = EVP_DigestSignFinal(EvpVerifyCtx, Signature, SigSize) > 0;\r
+  }\r
+\r
+_Exit :\r
+  if (EvpRsaKey != NULL) {\r
+    EVP_PKEY_free(EvpRsaKey);\r
+  }\r
+  if (EvpVerifyCtx != NULL) {\r
+    EVP_MD_CTX_destroy(EvpVerifyCtx);\r
+  }\r
+\r
+  return Result;\r
+}\r
diff --git a/CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaPssSignNull.c b/CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaPssSignNull.c
new file mode 100644 (file)
index 0000000..4ed2dfc
--- /dev/null
@@ -0,0 +1,60 @@
+/** @file\r
+  RSA-PSS Asymmetric Cipher Wrapper Implementation over OpenSSL.\r
+\r
+  This file does not provide real capabilities for following APIs in RSA handling:\r
+  1) RsaPssSign\r
+\r
+Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
+\r
+**/\r
+\r
+#include "InternalCryptLib.h"\r
+\r
+/**\r
+  Carries out the RSA-SSA signature generation with EMSA-PSS encoding scheme.\r
+\r
+  This function carries out the RSA-SSA signature generation with EMSA-PSS encoding scheme defined in\r
+  RFC 8017.\r
+  Mask generation function is the same as the message digest algorithm.\r
+  If the Signature buffer is too small to hold the contents of signature, FALSE\r
+  is returned and SigSize is set to the required buffer size to obtain the signature.\r
+\r
+  If RsaContext is NULL, then return FALSE.\r
+  If Message is NULL, then return FALSE.\r
+  If MsgSize is zero or > INT_MAX, then return FALSE.\r
+  If DigestLen is NOT 32, 48 or 64, return FALSE.\r
+  If SaltLen is < DigestLen, then return FALSE.\r
+  If SigSize is large enough but Signature is NULL, then return FALSE.\r
+  If this interface is not supported, then return FALSE.\r
+\r
+  @param[in]      RsaContext   Pointer to RSA context for signature generation.\r
+  @param[in]      Message      Pointer to octet message to be signed.\r
+  @param[in]      MsgSize      Size of the message in bytes.\r
+  @param[in]      DigestLen    Length of the digest in bytes to be used for RSA signature operation.\r
+  @param[in]      SaltLen      Length of the salt in bytes to be used for PSS encoding.\r
+  @param[out]     Signature    Pointer to buffer to receive RSA PSS signature.\r
+  @param[in, out] SigSize      On input, the size of Signature buffer in bytes.\r
+                               On output, the size of data returned in Signature buffer in bytes.\r
+\r
+  @retval  TRUE   Signature successfully generated in RSASSA-PSS.\r
+  @retval  FALSE  Signature generation failed.\r
+  @retval  FALSE  SigSize is too small.\r
+  @retval  FALSE  This interface is not supported.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+RsaPssSign (\r
+  IN      VOID         *RsaContext,\r
+  IN      CONST UINT8  *Message,\r
+  IN      UINTN        MsgSize,\r
+  IN      UINT16       DigestLen,\r
+  IN      UINT16       SaltLen,\r
+  OUT     UINT8        *Signature,\r
+  IN OUT  UINTN        *SigSize\r
+  )\r
+{\r
+  ASSERT (FALSE);\r
+  return FALSE;\r
+}\r
index 70c985ec93dc2b7556db34582d49f690b3cba803..3d3a6fb94a7786733c7e6dd4df653a2d9b4c50bc 100644 (file)
@@ -55,6 +55,8 @@
   Pk/CryptX509.c\r
   Pk/CryptAuthenticodeNull.c\r
   Pk/CryptTsNull.c\r
+  Pk/CryptRsaPssNull.c\r
+  Pk/CryptRsaPssSignNull.c\r
   Pem/CryptPem.c\r
 \r
   SysCall/CrtWrapper.c\r
index 91ec3e03bf5e7700810c90929c85f295c2a56568..07c376ce04bb712fdba29b5d558af0c5dd3111c9 100644 (file)
@@ -53,6 +53,8 @@
   Pk/CryptX509.c\r
   Pk/CryptAuthenticodeNull.c\r
   Pk/CryptTsNull.c\r
+  Pk/CryptRsaPss.c\r
+  Pk/CryptRsaPssSignNull.c\r
   Pem/CryptPem.c\r
 \r
   SysCall/CrtWrapper.c\r
index db506c32f724b7f62f764490b355ef30954c6590..b98f9635b27b86e4d9d464f08bf84f90d478a190 100644 (file)
@@ -44,6 +44,8 @@
   Pk/CryptAuthenticode.c\r
   Pk/CryptTs.c\r
   Pem/CryptPem.c\r
+  Pk/CryptRsaPss.c\r
+  Pk/CryptRsaPssSign.c\r
 \r
   SysCall/UnitTestHostCrtWrapper.c\r
 \r
index 689af4fedd68e1e4acd87f6dd7fee8fa06684315..faf959827b901fead82757d605a5e3e0dbbb1fd9 100644 (file)
@@ -50,6 +50,8 @@
   Pk/CryptTsNull.c\r
   Pem/CryptPemNull.c\r
   Rand/CryptRandNull.c\r
+  Pk/CryptRsaPssNull.c\r
+  Pk/CryptRsaPssSignNull.c\r
 \r
 [Packages]\r
   MdePkg/MdePkg.dec\r
diff --git a/CryptoPkg/Library/BaseCryptLibNull/Pk/CryptRsaPssNull.c b/CryptoPkg/Library/BaseCryptLibNull/Pk/CryptRsaPssNull.c
new file mode 100644 (file)
index 0000000..69c6889
--- /dev/null
@@ -0,0 +1,46 @@
+/** @file\r
+  RSA-PSS Asymmetric Cipher Wrapper Implementation over OpenSSL.\r
+\r
+  This file does not provide real capabilities for following APIs in RSA handling:\r
+  1) RsaPssVerify\r
+\r
+Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
+\r
+**/\r
+\r
+#include "InternalCryptLib.h"\r
+\r
+/**\r
+  Verifies the RSA signature with RSASSA-PSS signature scheme defined in RFC 8017.\r
+  Implementation determines salt length automatically from the signature encoding.\r
+  Mask generation function is the same as the message digest algorithm.\r
+  Salt length should atleast be equal to digest length.\r
+\r
+  @param[in]  RsaContext      Pointer to RSA context for signature verification.\r
+  @param[in]  Message         Pointer to octet message to be verified.\r
+  @param[in]  MsgSize         Size of the message in bytes.\r
+  @param[in]  Signature       Pointer to RSASSA-PSS signature to be verified.\r
+  @param[in]  SigSize         Size of signature in bytes.\r
+  @param[in]  DigestLen       Length of digest for RSA operation.\r
+  @param[in]  SaltLen         Salt length for PSS encoding.\r
+\r
+  @retval  TRUE   Valid signature encoded in RSASSA-PSS.\r
+  @retval  FALSE  Invalid signature or invalid RSA context.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+RsaPssVerify (\r
+  IN  VOID         *RsaContext,\r
+  IN  CONST UINT8  *Message,\r
+  IN  UINTN        MsgSize,\r
+  IN  CONST UINT8  *Signature,\r
+  IN  UINTN        SigSize,\r
+  IN  UINT16       DigestLen,\r
+  IN  UINT16       SaltLen\r
+  )\r
+{\r
+  ASSERT (FALSE);\r
+  return FALSE;\r
+}\r
diff --git a/CryptoPkg/Library/BaseCryptLibNull/Pk/CryptRsaPssSignNull.c b/CryptoPkg/Library/BaseCryptLibNull/Pk/CryptRsaPssSignNull.c
new file mode 100644 (file)
index 0000000..4ed2dfc
--- /dev/null
@@ -0,0 +1,60 @@
+/** @file\r
+  RSA-PSS Asymmetric Cipher Wrapper Implementation over OpenSSL.\r
+\r
+  This file does not provide real capabilities for following APIs in RSA handling:\r
+  1) RsaPssSign\r
+\r
+Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
+\r
+**/\r
+\r
+#include "InternalCryptLib.h"\r
+\r
+/**\r
+  Carries out the RSA-SSA signature generation with EMSA-PSS encoding scheme.\r
+\r
+  This function carries out the RSA-SSA signature generation with EMSA-PSS encoding scheme defined in\r
+  RFC 8017.\r
+  Mask generation function is the same as the message digest algorithm.\r
+  If the Signature buffer is too small to hold the contents of signature, FALSE\r
+  is returned and SigSize is set to the required buffer size to obtain the signature.\r
+\r
+  If RsaContext is NULL, then return FALSE.\r
+  If Message is NULL, then return FALSE.\r
+  If MsgSize is zero or > INT_MAX, then return FALSE.\r
+  If DigestLen is NOT 32, 48 or 64, return FALSE.\r
+  If SaltLen is < DigestLen, then return FALSE.\r
+  If SigSize is large enough but Signature is NULL, then return FALSE.\r
+  If this interface is not supported, then return FALSE.\r
+\r
+  @param[in]      RsaContext   Pointer to RSA context for signature generation.\r
+  @param[in]      Message      Pointer to octet message to be signed.\r
+  @param[in]      MsgSize      Size of the message in bytes.\r
+  @param[in]      DigestLen    Length of the digest in bytes to be used for RSA signature operation.\r
+  @param[in]      SaltLen      Length of the salt in bytes to be used for PSS encoding.\r
+  @param[out]     Signature    Pointer to buffer to receive RSA PSS signature.\r
+  @param[in, out] SigSize      On input, the size of Signature buffer in bytes.\r
+                               On output, the size of data returned in Signature buffer in bytes.\r
+\r
+  @retval  TRUE   Signature successfully generated in RSASSA-PSS.\r
+  @retval  FALSE  Signature generation failed.\r
+  @retval  FALSE  SigSize is too small.\r
+  @retval  FALSE  This interface is not supported.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+RsaPssSign (\r
+  IN      VOID         *RsaContext,\r
+  IN      CONST UINT8  *Message,\r
+  IN      UINTN        MsgSize,\r
+  IN      UINT16       DigestLen,\r
+  IN      UINT16       SaltLen,\r
+  OUT     UINT8        *Signature,\r
+  IN OUT  UINTN        *SigSize\r
+  )\r
+{\r
+  ASSERT (FALSE);\r
+  return FALSE;\r
+}\r
index 8b43d1363cb938a651cd0081f1ac602d11d971d0..af99ed7f5b427a751215883ef43b744d873227e3 100644 (file)
@@ -1552,6 +1552,76 @@ RsaPkcs1Verify (
   CALL_CRYPTO_SERVICE (RsaPkcs1Verify, (RsaContext, MessageHash, HashSize, Signature, SigSize), FALSE);\r
 }\r
 \r
+/**\r
+  Verifies the RSA signature with RSASSA-PSS signature scheme defined in RFC 8017.\r
+  Implementation determines salt length automatically from the signature encoding.\r
+  Mask generation function is the same as the message digest algorithm.\r
+  Salt length should atleast be equal to digest length.\r
+\r
+  @param[in]  RsaContext      Pointer to RSA context for signature verification.\r
+  @param[in]  Message         Pointer to octet message to be verified.\r
+  @param[in]  MsgSize         Size of the message in bytes.\r
+  @param[in]  Signature       Pointer to RSASSA-PSS signature to be verified.\r
+  @param[in]  SigSize         Size of signature in bytes.\r
+  @param[in]  DigestLen       Length of digest for RSA operation.\r
+  @param[in]  SaltLen         Salt length for PSS encoding.\r
+\r
+  @retval  TRUE   Valid signature encoded in RSASSA-PSS.\r
+  @retval  FALSE  Invalid signature or invalid RSA context.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+RsaPssVerify (\r
+  IN  VOID         *RsaContext,\r
+  IN  CONST UINT8  *Message,\r
+  IN  UINTN        MsgSize,\r
+  IN  CONST UINT8  *Signature,\r
+  IN  UINTN        SigSize,\r
+  IN  UINT16       DigestLen,\r
+  IN  UINT16       SaltLen\r
+  )\r
+{\r
+  CALL_CRYPTO_SERVICE (RsaPssVerify, (RsaContext, Message, MsgSize, Signature, SigSize, DigestLen, SaltLen), FALSE);\r
+}\r
+\r
+/**\r
+  This function carries out the RSA-SSA signature generation with EMSA-PSS encoding scheme defined in\r
+  RFC 8017.\r
+  Mask generation function is the same as the message digest algorithm.\r
+  If the Signature buffer is too small to hold the contents of signature, FALSE\r
+  is returned and SigSize is set to the required buffer size to obtain the signature.\r
+\r
+  @param[in]      RsaContext   Pointer to RSA context for signature generation.\r
+  @param[in]      Message      Pointer to octet message to be signed.\r
+  @param[in]      MsgSize      Size of the message in bytes.\r
+  @param[in]      DigestLen    Length of the digest in bytes to be used for RSA signature operation.\r
+  @param[in]      SaltLen      Length of the salt in bytes to be used for PSS encoding.\r
+  @param[out]     Signature    Pointer to buffer to receive RSA PSS signature.\r
+  @param[in, out] SigSize      On input, the size of Signature buffer in bytes.\r
+                               On output, the size of data returned in Signature buffer in bytes.\r
+\r
+  @retval  TRUE   Signature successfully generated in RSASSA-PSS.\r
+  @retval  FALSE  Signature generation failed.\r
+  @retval  FALSE  SigSize is too small.\r
+  @retval  FALSE  This interface is not supported.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+RsaPssSign (\r
+  IN      VOID         *RsaContext,\r
+  IN      CONST UINT8  *Message,\r
+  IN      UINTN        MsgSize,\r
+  IN      UINT16       DigestLen,\r
+  IN      UINT16       SaltLen,\r
+  OUT     UINT8        *Signature,\r
+  IN OUT  UINTN        *SigSize\r
+  )\r
+{\r
+  CALL_CRYPTO_SERVICE (RsaPssSign, (RsaContext, Message, MsgSize, DigestLen, SaltLen, Signature, SigSize), FALSE);\r
+}\r
+\r
 /**\r
   Retrieve the RSA Private Key from the password-protected PEM key data.\r
 \r
index 17930a77a60e329b99c424496b4117c907436980..e304302c9445289e85fd672d6a52411706bd2a9a 100644 (file)
@@ -3408,6 +3408,81 @@ EFI_STATUS
   IN OUT UINTN                    *DataSize\r
   );\r
 \r
+/**\r
+  Carries out the RSA-SSA signature generation with EMSA-PSS encoding scheme.\r
+\r
+  This function carries out the RSA-SSA signature generation with EMSA-PSS encoding scheme defined in\r
+  RFC 8017.\r
+  Mask generation function is the same as the message digest algorithm.\r
+  If the Signature buffer is too small to hold the contents of signature, FALSE\r
+  is returned and SigSize is set to the required buffer size to obtain the signature.\r
+\r
+  If RsaContext is NULL, then return FALSE.\r
+  If Message is NULL, then return FALSE.\r
+  If MsgSize is zero or > INT_MAX, then return FALSE.\r
+  If DigestLen is NOT 32, 48 or 64, return FALSE.\r
+  If SaltLen is < DigestLen, then return FALSE.\r
+  If SigSize is large enough but Signature is NULL, then return FALSE.\r
+  If this interface is not supported, then return FALSE.\r
+\r
+  @param[in]      RsaContext   Pointer to RSA context for signature generation.\r
+  @param[in]      Message      Pointer to octet message to be signed.\r
+  @param[in]      MsgSize      Size of the message in bytes.\r
+  @param[in]      DigestLen    Length of the digest in bytes to be used for RSA signature operation.\r
+  @param[in]      SaltLen      Length of the salt in bytes to be used for PSS encoding.\r
+  @param[out]     Signature    Pointer to buffer to receive RSA PSS signature.\r
+  @param[in, out] SigSize      On input, the size of Signature buffer in bytes.\r
+                               On output, the size of data returned in Signature buffer in bytes.\r
+\r
+  @retval  TRUE   Signature successfully generated in RSASSA-PSS.\r
+  @retval  FALSE  Signature generation failed.\r
+  @retval  FALSE  SigSize is too small.\r
+  @retval  FALSE  This interface is not supported.\r
+\r
+**/\r
+typedef\r
+BOOLEAN\r
+(EFIAPI* EDKII_CRYPTO_RSA_PSS_SIGN)(\r
+  IN      VOID         *RsaContext,\r
+  IN      CONST UINT8  *Message,\r
+  IN      UINTN        MsgSize,\r
+  IN      UINT16       DigestLen,\r
+  IN      UINT16       SaltLen,\r
+  OUT     UINT8        *Signature,\r
+  IN OUT  UINTN        *SigSize\r
+  );\r
+\r
+/**\r
+  Verifies the RSA signature with RSASSA-PSS signature scheme defined in RFC 8017.\r
+  Implementation determines salt length automatically from the signature encoding.\r
+  Mask generation function is the same as the message digest algorithm.\r
+  Salt length should atleast be equal to digest length.\r
+\r
+  @param[in]  RsaContext      Pointer to RSA context for signature verification.\r
+  @param[in]  Message         Pointer to octet message to be verified.\r
+  @param[in]  MsgSize         Size of the message in bytes.\r
+  @param[in]  Signature       Pointer to RSASSA-PSS signature to be verified.\r
+  @param[in]  SigSize         Size of signature in bytes.\r
+  @param[in]  DigestLen       Length of digest for RSA operation.\r
+  @param[in]  SaltLen         Salt length for PSS encoding.\r
+\r
+  @retval  TRUE   Valid signature encoded in RSASSA-PSS.\r
+  @retval  FALSE  Invalid signature or invalid RSA context.\r
+\r
+**/\r
+typedef\r
+BOOLEAN\r
+(EFIAPI* EDKII_CRYPTO_RSA_PSS_VERIFY)(\r
+  IN  VOID         *RsaContext,\r
+  IN  CONST UINT8  *Message,\r
+  IN  UINTN        MsgSize,\r
+  IN  CONST UINT8  *Signature,\r
+  IN  UINTN        SigSize,\r
+  IN  UINT16       DigestLen,\r
+  IN  UINT16       SaltLen\r
+  );\r
+\r
+\r
 \r
 ///\r
 /// EDK II Crypto Protocol\r
@@ -3593,6 +3668,9 @@ struct _EDKII_CRYPTO_PROTOCOL {
   EDKII_CRYPTO_TLS_GET_HOST_PUBLIC_CERT           TlsGetHostPublicCert;\r
   EDKII_CRYPTO_TLS_GET_HOST_PRIVATE_KEY           TlsGetHostPrivateKey;\r
   EDKII_CRYPTO_TLS_GET_CERT_REVOCATION_LIST       TlsGetCertRevocationList;\r
+  /// RSA PSS\r
+  EDKII_CRYPTO_RSA_PSS_SIGN                       RsaPssSign;\r
+  EDKII_CRYPTO_RSA_PSS_VERIFY                     RsaPssVerify;\r
 };\r
 \r
 extern GUID gEdkiiCryptoProtocolGuid;\r
index b7fcea3ff7e4b3ea0596d8a6c077cd985ae77055..3873de9730646dc53a9675a3194f546c209e6695 100644 (file)
@@ -16,6 +16,7 @@ SUITE_DESC  mSuiteDesc[] = {
     {"HMAC verify tests",           "CryptoPkg.BaseCryptLib", NULL, NULL, &mHmacTestNum,           mHmacTest},\r
     {"BlockCipher verify tests",    "CryptoPkg.BaseCryptLib", NULL, NULL, &mBlockCipherTestNum,    mBlockCipherTest},\r
     {"RSA verify tests",            "CryptoPkg.BaseCryptLib", NULL, NULL, &mRsaTestNum,            mRsaTest},\r
+    {"RSA PSS verify tests",        "CryptoPkg.BaseCryptLib", NULL, NULL, &mRsaPssTestNum,         mRsaPssTest},\r
     {"RSACert verify tests",        "CryptoPkg.BaseCryptLib", NULL, NULL, &mRsaCertTestNum,        mRsaCertTest},\r
     {"PKCS7 verify tests",          "CryptoPkg.BaseCryptLib", NULL, NULL, &mPkcs7TestNum,          mPkcs7Test},\r
     {"PKCS5 verify tests",          "CryptoPkg.BaseCryptLib", NULL, NULL, &mPkcs5TestNum,          mPkcs5Test},\r
diff --git a/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/RsaPssTests.c b/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/RsaPssTests.c
new file mode 100644 (file)
index 0000000..7970523
--- /dev/null
@@ -0,0 +1,191 @@
+/** @file\r
+  Application for RSA PSS Primitives Validation.\r
+\r
+Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
+\r
+**/\r
+\r
+#include "TestBaseCryptLib.h"\r
+\r
+//\r
+// RSA PSS test vectors from NIST FIPS 186-3 RSA files\r
+//\r
+\r
+//\r
+// Public Modulus of RSA Key\r
+//\r
+UINT8 RsaPssN[]={\r
+    0xa4, 0x7d, 0x04, 0xe7, 0xca, 0xcd, 0xba, 0x4e, 0xa2, 0x6e, 0xca, 0x8a, 0x4c, 0x6e, 0x14, 0x56,\r
+    0x3c, 0x2c, 0xe0, 0x3b, 0x62, 0x3b, 0x76, 0x8c, 0x0d, 0x49, 0x86, 0x8a, 0x57, 0x12, 0x13, 0x01,\r
+    0xdb, 0xf7, 0x83, 0xd8, 0x2f, 0x4c, 0x05, 0x5e, 0x73, 0x96, 0x0e, 0x70, 0x55, 0x01, 0x87, 0xd0,\r
+    0xaf, 0x62, 0xac, 0x34, 0x96, 0xf0, 0xa3, 0xd9, 0x10, 0x3c, 0x2e, 0xb7, 0x91, 0x9a, 0x72, 0x75,\r
+    0x2f, 0xa7, 0xce, 0x8c, 0x68, 0x8d, 0x81, 0xe3, 0xae, 0xe9, 0x94, 0x68, 0x88, 0x7a, 0x15, 0x28,\r
+    0x8a, 0xfb, 0xb7, 0xac, 0xb8, 0x45, 0xb7, 0xc5, 0x22, 0xb5, 0xc6, 0x4e, 0x67, 0x8f, 0xcd, 0x3d,\r
+    0x22, 0xfe, 0xb8, 0x4b, 0x44, 0x27, 0x27, 0x00, 0xbe, 0x52, 0x7d, 0x2b, 0x20, 0x25, 0xa3, 0xf8,\r
+    0x3c, 0x23, 0x83, 0xbf, 0x6a, 0x39, 0xcf, 0x5b, 0x4e, 0x48, 0xb3, 0xcf, 0x2f, 0x56, 0xee, 0xf0,\r
+    0xdf, 0xff, 0x18, 0x55, 0x5e, 0x31, 0x03, 0x7b, 0x91, 0x52, 0x48, 0x69, 0x48, 0x76, 0xf3, 0x04,\r
+    0x78, 0x14, 0x41, 0x51, 0x64, 0xf2, 0xc6, 0x60, 0x88, 0x1e, 0x69, 0x4b, 0x58, 0xc2, 0x80, 0x38,\r
+    0xa0, 0x32, 0xad, 0x25, 0x63, 0x4a, 0xad, 0x7b, 0x39, 0x17, 0x1d, 0xee, 0x36, 0x8e, 0x3d, 0x59,\r
+    0xbf, 0xb7, 0x29, 0x9e, 0x46, 0x01, 0xd4, 0x58, 0x7e, 0x68, 0xca, 0xaf, 0x8d, 0xb4, 0x57, 0xb7,\r
+    0x5a, 0xf4, 0x2f, 0xc0, 0xcf, 0x1a, 0xe7, 0xca, 0xce, 0xd2, 0x86, 0xd7, 0x7f, 0xac, 0x6c, 0xed,\r
+    0xb0, 0x3a, 0xd9, 0x4f, 0x14, 0x33, 0xd2, 0xc9, 0x4d, 0x08, 0xe6, 0x0b, 0xc1, 0xfd, 0xef, 0x05,\r
+    0x43, 0xcd, 0x29, 0x51, 0xe7, 0x65, 0xb3, 0x82, 0x30, 0xfd, 0xd1, 0x8d, 0xe5, 0xd2, 0xca, 0x62,\r
+    0x7d, 0xdc, 0x03, 0x2f, 0xe0, 0x5b, 0xbd, 0x2f, 0xf2, 0x1e, 0x2d, 0xb1, 0xc2, 0xf9, 0x4d, 0x8b,\r
+    };\r
+\r
+//\r
+// Public Exponent of RSA Key\r
+//\r
+UINT8 RsaPssE[]={ 0x10, 0xe4, 0x3f };\r
+\r
+//\r
+// Private Exponent of RSA Key\r
+//\r
+UINT8 RsaPssD[]={\r
+    0x11, 0xa0, 0xdd, 0x28, 0x5f, 0x66, 0x47, 0x1a, 0x8d, 0xa3, 0x0b, 0xcb, 0x8c, 0x24, 0xa1, 0xd5,\r
+    0xc8, 0xdb, 0x94, 0x2f, 0xc9, 0x92, 0x07, 0x97, 0xca, 0x44, 0x24, 0x60, 0xa8, 0x00, 0xb7, 0x5b,\r
+    0xbc, 0x73, 0x8b, 0xeb, 0x8e, 0xe0, 0xe8, 0x74, 0xb0, 0x53, 0xe6, 0x47, 0x07, 0xdf, 0x4c, 0xfc,\r
+    0x78, 0x37, 0xc4, 0x0e, 0x5b, 0xe6, 0x8b, 0x8a, 0x8e, 0x1d, 0x01, 0x45, 0x16, 0x9c, 0xa6, 0x27,\r
+    0x1d, 0x81, 0x88, 0x7e, 0x19, 0xa1, 0xcd, 0x95, 0xb2, 0xfd, 0x0d, 0xe0, 0xdb, 0xa3, 0x47, 0xfe,\r
+    0x63, 0x7b, 0xcc, 0x6c, 0xdc, 0x24, 0xee, 0xbe, 0x03, 0xc2, 0x4d, 0x4c, 0xf3, 0xa5, 0xc6, 0x15,\r
+    0x4d, 0x78, 0xf1, 0x41, 0xfe, 0x34, 0x16, 0x99, 0x24, 0xd0, 0xf8, 0x95, 0x33, 0x65, 0x8e, 0xac,\r
+    0xfd, 0xea, 0xe9, 0x9c, 0xe1, 0xa8, 0x80, 0x27, 0xc1, 0x8f, 0xf9, 0x26, 0x53, 0xa8, 0x35, 0xaa,\r
+    0x38, 0x91, 0xbf, 0xff, 0xcd, 0x38, 0x8f, 0xfc, 0x23, 0x88, 0xce, 0x2b, 0x10, 0x56, 0x85, 0x43,\r
+    0x75, 0x05, 0x02, 0xcc, 0xbc, 0x69, 0xc0, 0x08, 0x8f, 0x1d, 0x69, 0x0e, 0x97, 0xa5, 0xf5, 0xbd,\r
+    0xd1, 0x88, 0x8c, 0xd2, 0xfa, 0xa4, 0x3c, 0x04, 0xae, 0x24, 0x53, 0x95, 0x22, 0xdd, 0xe2, 0xd9,\r
+    0xc2, 0x02, 0xf6, 0x55, 0xfc, 0x55, 0x75, 0x44, 0x40, 0xb5, 0x3a, 0x15, 0x32, 0xaa, 0xb4, 0x78,\r
+    0x51, 0xf6, 0x0b, 0x7a, 0x06, 0x7e, 0x24, 0x0b, 0x73, 0x8e, 0x1b, 0x1d, 0xaa, 0xe6, 0xca, 0x0d,\r
+    0x59, 0xee, 0xae, 0x27, 0x68, 0x6c, 0xd8, 0x88, 0x57, 0xe9, 0xad, 0xad, 0xc2, 0xd4, 0xb8, 0x2b,\r
+    0x07, 0xa6, 0x1a, 0x35, 0x84, 0x56, 0xaa, 0xf8, 0x07, 0x66, 0x96, 0x93, 0xff, 0xb1, 0x3c, 0x99,\r
+    0x64, 0xa6, 0x36, 0x54, 0xca, 0xdc, 0x81, 0xee, 0x59, 0xdf, 0x51, 0x1c, 0xa3, 0xa4, 0xbd, 0x67,\r
+    };\r
+\r
+//\r
+// Binary message to be signed and verified\r
+//\r
+UINT8 PssMessage[]={\r
+    0xe0, 0x02, 0x37, 0x7a, 0xff, 0xb0, 0x4f, 0x0f, 0xe4, 0x59, 0x8d, 0xe9, 0xd9, 0x2d, 0x31, 0xd6,\r
+    0xc7, 0x86, 0x04, 0x0d, 0x57, 0x76, 0x97, 0x65, 0x56, 0xa2, 0xcf, 0xc5, 0x5e, 0x54, 0xa1, 0xdc,\r
+    0xb3, 0xcb, 0x1b, 0x12, 0x6b, 0xd6, 0xa4, 0xbe, 0xd2, 0xa1, 0x84, 0x99, 0x0c, 0xce, 0xa7, 0x73,\r
+    0xfc, 0xc7, 0x9d, 0x24, 0x65, 0x53, 0xe6, 0xc6, 0x4f, 0x68, 0x6d, 0x21, 0xad, 0x41, 0x52, 0x67,\r
+    0x3c, 0xaf, 0xec, 0x22, 0xae, 0xb4, 0x0f, 0x6a, 0x08, 0x4e, 0x8a, 0x5b, 0x49, 0x91, 0xf4, 0xc6,\r
+    0x4c, 0xf8, 0xa9, 0x27, 0xef, 0xfd, 0x0f, 0xd7, 0x75, 0xe7, 0x1e, 0x83, 0x29, 0xe4, 0x1f, 0xdd,\r
+    0x44, 0x57, 0xb3, 0x91, 0x11, 0x73, 0x18, 0x7b, 0x4f, 0x09, 0xa8, 0x17, 0xd7, 0x9e, 0xa2, 0x39,\r
+    0x7f, 0xc1, 0x2d, 0xfe, 0x3d, 0x9c, 0x9a, 0x02, 0x90, 0xc8, 0xea, 0xd3, 0x1b, 0x66, 0x90, 0xa6,\r
+    };\r
+\r
+//\r
+// Binary message to be signed and verified\r
+//\r
+UINT8 PssSalt[]={\r
+    0xd6, 0x6f, 0x72, 0xf1, 0x0b, 0x69, 0x00, 0x1a, 0x5b, 0x59, 0xcf, 0x10, 0x92, 0xad, 0x27, 0x4d,\r
+    0x50, 0x56, 0xc4, 0xe9, 0x5c, 0xcc, 0xcf, 0xbe, 0x3b, 0x53, 0x0d, 0xcb, 0x02, 0x7e, 0x57, 0xd6\r
+    };\r
+\r
+//\r
+// RSASSA-PSS Signature over above message using above keys, salt and SHA256 digest(and MGF1) algo.\r
+//\r
+UINT8 TestVectorSignature[]={\r
+    0x4f, 0x9b, 0x42, 0x5c, 0x20, 0x58, 0x46, 0x0e, 0x4a, 0xb2, 0xf5, 0xc9, 0x63, 0x84, 0xda, 0x23,\r
+    0x27, 0xfd, 0x29, 0x15, 0x0f, 0x01, 0x95, 0x5a, 0x76, 0xb4, 0xef, 0xe9, 0x56, 0xaf, 0x06, 0xdc,\r
+    0x08, 0x77, 0x9a, 0x37, 0x4e, 0xe4, 0x60, 0x7e, 0xab, 0x61, 0xa9, 0x3a, 0xdc, 0x56, 0x08, 0xf4,\r
+    0xec, 0x36, 0xe4, 0x7f, 0x2a, 0x0f, 0x75, 0x4e, 0x8f, 0xf8, 0x39, 0xa8, 0xa1, 0x9b, 0x1d, 0xb1,\r
+    0xe8, 0x84, 0xea, 0x4c, 0xf3, 0x48, 0xcd, 0x45, 0x50, 0x69, 0xeb, 0x87, 0xaf, 0xd5, 0x36, 0x45,\r
+    0xb4, 0x4e, 0x28, 0xa0, 0xa5, 0x68, 0x08, 0xf5, 0x03, 0x1d, 0xa5, 0xba, 0x91, 0x12, 0x76, 0x8d,\r
+    0xfb, 0xfc, 0xa4, 0x4e, 0xbe, 0x63, 0xa0, 0xc0, 0x57, 0x2b, 0x73, 0x1d, 0x66, 0x12, 0x2f, 0xb7,\r
+    0x16, 0x09, 0xbe, 0x14, 0x80, 0xfa, 0xa4, 0xe4, 0xf7, 0x5e, 0x43, 0x95, 0x51, 0x59, 0xd7, 0x0f,\r
+    0x08, 0x1e, 0x2a, 0x32, 0xfb, 0xb1, 0x9a, 0x48, 0xb9, 0xf1, 0x62, 0xcf, 0x6b, 0x2f, 0xb4, 0x45,\r
+    0xd2, 0xd6, 0x99, 0x4b, 0xc5, 0x89, 0x10, 0xa2, 0x6b, 0x59, 0x43, 0x47, 0x78, 0x03, 0xcd, 0xaa,\r
+    0xa1, 0xbd, 0x74, 0xb0, 0xda, 0x0a, 0x5d, 0x05, 0x3d, 0x8b, 0x1d, 0xc5, 0x93, 0x09, 0x1d, 0xb5,\r
+    0x38, 0x83, 0x83, 0xc2, 0x60, 0x79, 0xf3, 0x44, 0xe2, 0xae, 0xa6, 0x00, 0xd0, 0xe3, 0x24, 0x16,\r
+    0x4b, 0x45, 0x0f, 0x7b, 0x9b, 0x46, 0x51, 0x11, 0xb7, 0x26, 0x5f, 0x3b, 0x1b, 0x06, 0x30, 0x89,\r
+    0xae, 0x7e, 0x26, 0x23, 0xfc, 0x0f, 0xda, 0x80, 0x52, 0xcf, 0x4b, 0xf3, 0x37, 0x91, 0x02, 0xfb,\r
+    0xf7, 0x1d, 0x7c, 0x98, 0xe8, 0x25, 0x86, 0x64, 0xce, 0xed, 0x63, 0x7d, 0x20, 0xf9, 0x5f, 0xf0,\r
+    0x11, 0x18, 0x81, 0xe6, 0x50, 0xce, 0x61, 0xf2, 0x51, 0xd9, 0xc3, 0xa6, 0x29, 0xef, 0x22, 0x2d,\r
+    };\r
+\r
+\r
+STATIC VOID     *mRsa;\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestVerifyRsaPssPreReq (\r
+  UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  mRsa = RsaNew ();\r
+\r
+  if (mRsa == NULL) {\r
+    return UNIT_TEST_ERROR_TEST_FAILED;\r
+  }\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+VOID\r
+EFIAPI\r
+TestVerifyRsaPssCleanUp (\r
+  UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  if (mRsa != NULL) {\r
+    RsaFree (mRsa);\r
+    mRsa = NULL;\r
+  }\r
+}\r
+\r
+\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+TestVerifyRsaPssSignVerify (\r
+  IN UNIT_TEST_CONTEXT           Context\r
+  )\r
+{\r
+  UINT8    *Signature;\r
+  UINTN    SigSize;\r
+  BOOLEAN  Status;\r
+\r
+  Status = RsaSetKey (mRsa, RsaKeyN, RsaPssN, sizeof (RsaPssN));\r
+  UT_ASSERT_TRUE (Status);\r
+\r
+  Status = RsaSetKey (mRsa, RsaKeyE, RsaPssE, sizeof (RsaPssE));\r
+  UT_ASSERT_TRUE (Status);\r
+\r
+  Status = RsaSetKey (mRsa, RsaKeyD, RsaPssD, sizeof (RsaPssD));\r
+  UT_ASSERT_TRUE (Status);\r
+\r
+  SigSize = 0;\r
+  Status = RsaPssSign (mRsa, PssMessage, sizeof(PssMessage), SHA256_DIGEST_SIZE, SHA256_DIGEST_SIZE, NULL, &SigSize);\r
+  UT_ASSERT_FALSE (Status);\r
+  UT_ASSERT_NOT_EQUAL (SigSize, 0);\r
+\r
+  Signature = AllocatePool (SigSize);\r
+  Status = RsaPssSign (mRsa, PssMessage, sizeof(PssMessage), SHA256_DIGEST_SIZE, SHA256_DIGEST_SIZE, Signature, &SigSize);\r
+  UT_ASSERT_TRUE (Status);\r
+\r
+  //\r
+  // Verify RSA PSS encoded Signature generated in above step\r
+  //\r
+  Status = RsaPssVerify (mRsa, PssMessage, sizeof(PssMessage), Signature, SigSize, SHA256_DIGEST_SIZE, SHA256_DIGEST_SIZE);\r
+  UT_ASSERT_TRUE (Status);\r
+\r
+  //\r
+  // Verify NIST FIPS 186-3 RSA test vector signature\r
+  //\r
+  Status = RsaPssVerify (mRsa, PssMessage, sizeof(PssMessage), TestVectorSignature, sizeof(TestVectorSignature), SHA256_DIGEST_SIZE, SHA256_DIGEST_SIZE);\r
+  UT_ASSERT_TRUE (Status);\r
+\r
+  FreePool(Signature);\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+\r
+TEST_DESC mRsaPssTest[] = {\r
+    //\r
+    // -----Description--------------------------------------Class----------------------Function---------------------------------Pre---------------------Post---------Context\r
+    //\r
+    {"TestVerifyRsaPssSignVerify()",           "CryptoPkg.BaseCryptLib.Rsa",   TestVerifyRsaPssSignVerify,           TestVerifyRsaPssPreReq, TestVerifyRsaPssCleanUp, NULL},\r
+};\r
+\r
+UINTN mRsaPssTestNum = ARRAY_SIZE(mRsaPssTest);\r
index 7ce20d2e778f3b7697dbae13234f9902ca0174d3..0969b6aea66045a1e06bcc124ebdceea5a608f22 100644 (file)
@@ -295,6 +295,8 @@ TestVerifyRsaPkcs1SignVerify (
   Status = RsaPkcs1Verify (mRsa, HashValue, HashSize, Signature, SigSize);\r
   UT_ASSERT_TRUE (Status);\r
 \r
+  FreePool(Signature);\r
+\r
   return UNIT_TEST_PASSED;\r
 }\r
 \r
index 9d1cb150a113cf77f160a2085c7207a02a294a2f..25c1379f1a777923ab65d93c78a2717da47b370b 100644 (file)
@@ -83,6 +83,9 @@ extern TEST_DESC mPrngTest[];
 extern UINTN mOaepTestNum;\r
 extern TEST_DESC mOaepTest[];\r
 \r
+extern UINTN mRsaPssTestNum;\r
+extern TEST_DESC mRsaPssTest[];\r
+\r
 /** Creates a framework you can use */\r
 EFI_STATUS\r
 EFIAPI\r
index 300b98e40b33a3741e78cfd967052a70b5749f3b..00c869265080240ceffb83943fc48b2227e5a6a7 100644 (file)
@@ -34,6 +34,7 @@
   RandTests.c\r
   Pkcs7EkuTests.c\r
   OaepEncryptTests.c\r
+  RsaPssTests.c\r
 \r
 [Packages]\r
   MdePkg/MdePkg.dec\r
index d5e7e0d01446f41c10b3c68cc171b92fff2794aa..ca789aa6ada3b6f5097f6deb9cb927b1cde103da 100644 (file)
@@ -35,6 +35,7 @@
   RandTests.c\r
   Pkcs7EkuTests.c\r
   OaepEncryptTests.c\r
+  RsaPssTests.c\r
 \r
 [Packages]\r
   MdePkg/MdePkg.dec\r