isaacpittman-hitachi / rpms / openssl

Forked from rpms/openssl 2 years ago
Clone
3da501
From 52b347703ba2b98a0efee86c1a483c2f0f9f73d6 Mon Sep 17 00:00:00 2001
3da501
From: Clemens Lang <cllang@redhat.com>
3da501
Date: Wed, 11 Jan 2023 12:52:59 +0100
3da501
Subject: [PATCH] rsa: Disallow SHAKE in OAEP and PSS in FIPS prov
3da501
3da501
According to FIPS 140-3 IG, section C.C, the SHAKE digest algorithms
3da501
must not be used in higher-level algorithms (such as RSA-OAEP and
3da501
RSASSA-PSS):
3da501
3da501
"To be used in an approved mode of operation, the SHA-3 hash functions
3da501
may be implemented either as part of an approved higher-level algorithm,
3da501
for example, a digital signature algorithm, or as the standalone
3da501
functions. The SHAKE128 and SHAKE256 extendable-output functions may
3da501
only be used as the standalone algorithms."
3da501
3da501
Add a check to prevent their use as message digest in PSS signatures and
3da501
as MGF1 hash function in both OAEP and PSS.
3da501
3da501
Signed-off-by: Clemens Lang <cllang@redhat.com>
3da501
---
3da501
 crypto/rsa/rsa_oaep.c | 28 ++++++++++++++++++++++++++++
3da501
 crypto/rsa/rsa_pss.c  | 16 ++++++++++++++++
3da501
 2 files changed, 44 insertions(+)
3da501
3da501
diff --git a/crypto/rsa/rsa_oaep.c b/crypto/rsa/rsa_oaep.c
3da501
index d9be1a4f98..dfe9c9f0e8 100644
3da501
--- a/crypto/rsa/rsa_oaep.c
3da501
+++ b/crypto/rsa/rsa_oaep.c
3da501
@@ -73,9 +73,23 @@ int ossl_rsa_padding_add_PKCS1_OAEP_mgf1_ex(OSSL_LIB_CTX *libctx,
3da501
         return 0;
3da501
 #endif
3da501
     }
3da501
+
3da501
+#ifdef FIPS_MODULE
3da501
+    if (EVP_MD_is_a(md, "SHAKE-128") || EVP_MD_is_a(md, "SHAKE-256")) {
3da501
+        ERR_raise(ERR_LIB_RSA, RSA_R_DIGEST_NOT_ALLOWED);
3da501
+        return 0;
3da501
+    }
3da501
+#endif
3da501
     if (mgf1md == NULL)
3da501
         mgf1md = md;
3da501
 
3da501
+#ifdef FIPS_MODULE
3da501
+    if (EVP_MD_is_a(mgf1md, "SHAKE-128") || EVP_MD_is_a(mgf1md, "SHAKE-256")) {
3da501
+        ERR_raise(ERR_LIB_RSA, RSA_R_DIGEST_NOT_ALLOWED);
3da501
+        return 0;
3da501
+    }
3da501
+#endif
3da501
+
3da501
     mdlen = EVP_MD_get_size(md);
3da501
     if (mdlen <= 0) {
3da501
         ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_LENGTH);
3da501
@@ -181,9 +195,23 @@ int RSA_padding_check_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
3da501
 #endif
3da501
     }
3da501
 
3da501
+#ifdef FIPS_MODULE
3da501
+    if (EVP_MD_is_a(md, "SHAKE-128") || EVP_MD_is_a(md, "SHAKE-256")) {
3da501
+        ERR_raise(ERR_LIB_RSA, RSA_R_DIGEST_NOT_ALLOWED);
3da501
+        return -1;
3da501
+    }
3da501
+#endif
3da501
+
3da501
     if (mgf1md == NULL)
3da501
         mgf1md = md;
3da501
 
3da501
+#ifdef FIPS_MODULE
3da501
+    if (EVP_MD_is_a(mgf1md, "SHAKE-128") || EVP_MD_is_a(mgf1md, "SHAKE-256")) {
3da501
+        ERR_raise(ERR_LIB_RSA, RSA_R_DIGEST_NOT_ALLOWED);
3da501
+        return -1;
3da501
+    }
3da501
+#endif
3da501
+
3da501
     mdlen = EVP_MD_get_size(md);
3da501
 
3da501
     if (tlen <= 0 || flen <= 0)
3da501
diff --git a/crypto/rsa/rsa_pss.c b/crypto/rsa/rsa_pss.c
3da501
index 33874bfef8..e8681b0351 100644
3da501
--- a/crypto/rsa/rsa_pss.c
3da501
+++ b/crypto/rsa/rsa_pss.c
3da501
@@ -53,6 +53,14 @@ int RSA_verify_PKCS1_PSS_mgf1(RSA *rsa, const unsigned char *mHash,
3da501
     if (mgf1Hash == NULL)
3da501
         mgf1Hash = Hash;
3da501
 
3da501
+#ifdef FIPS_MODULE
3da501
+    if (EVP_MD_is_a(Hash, "SHAKE-128") || EVP_MD_is_a(Hash, "SHAKE-256"))
3da501
+        goto err;
3da501
+
3da501
+    if (EVP_MD_is_a(mgf1Hash, "SHAKE-128") || EVP_MD_is_a(mgf1Hash, "SHAKE-256"))
3da501
+        goto err;
3da501
+#endif
3da501
+
3da501
     hLen = EVP_MD_get_size(Hash);
3da501
     if (hLen < 0)
3da501
         goto err;
3da501
@@ -164,6 +172,14 @@ int RSA_padding_add_PKCS1_PSS_mgf1(RSA *rsa, unsigned char *EM,
3da501
     if (mgf1Hash == NULL)
3da501
         mgf1Hash = Hash;
3da501
 
3da501
+#ifdef FIPS_MODULE
3da501
+    if (EVP_MD_is_a(Hash, "SHAKE-128") || EVP_MD_is_a(Hash, "SHAKE-256"))
3da501
+        goto err;
3da501
+
3da501
+    if (EVP_MD_is_a(mgf1Hash, "SHAKE-128") || EVP_MD_is_a(mgf1Hash, "SHAKE-256"))
3da501
+        goto err;
3da501
+#endif
3da501
+
3da501
     hLen = EVP_MD_get_size(Hash);
3da501
     if (hLen < 0)
3da501
         goto err;
3da501
-- 
3da501
2.39.0
3da501