Blame SOURCES/0085-FIPS-RSA-disable-shake.patch

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