Blame SOURCES/gnutls-3.7.6-fips-rsa-pss-saltlen.patch

a74aed
From 3bd42dc88ff062bf9ede2b593e1ad1afa6f68f62 Mon Sep 17 00:00:00 2001
a74aed
From: Daiki Ueno <ueno@gnu.org>
a74aed
Date: Wed, 16 Nov 2022 23:02:13 +0900
a74aed
Subject: [PATCH] nettle: mark non-compliant RSA-PSS salt length to be
a74aed
 not-approved
a74aed
a74aed
According to FIPS 186-5 5.4, the salt length must be in the range
a74aed
between 0 and the hash length inclusive.  While the use of those salt
a74aed
lengths is still allowed for compatibility, it is reported as
a74aed
non-approved operation through FIPS service indicator.
a74aed
a74aed
Signed-off-by: Daiki Ueno <ueno@gnu.org>
a74aed
---
a74aed
 lib/nettle/pk.c     |  9 ++++++++
a74aed
 tests/rsa-rsa-pss.c | 54 ++++++++++++++++++++++++++++++++++++---------
a74aed
 2 files changed, 53 insertions(+), 10 deletions(-)
a74aed
a74aed
diff --git a/lib/nettle/pk.c b/lib/nettle/pk.c
a74aed
index c098e2aa45..7732e90542 100644
a74aed
--- a/lib/nettle/pk.c
a74aed
+++ b/lib/nettle/pk.c
a74aed
@@ -1316,6 +1316,15 @@ _wrap_nettle_pk_sign(gnutls_pk_algorithm_t algo,
a74aed
 
a74aed
 			mpz_init(s);
a74aed
 
a74aed
+			me = hash_to_entry(sign_params->rsa_pss_dig);
a74aed
+
a74aed
+			/* According to FIPS 186-5 5.4, the salt length must be
a74aed
+			 * in the range between 0 and the hash length inclusive.
a74aed
+			 */
a74aed
+			if (sign_params->salt_size > _gnutls_mac_get_algo_len(me)) {
a74aed
+				not_approved = true;
a74aed
+			}
a74aed
+
a74aed
 			ret =
a74aed
 			    _rsa_pss_sign_digest_tr(sign_params->rsa_pss_dig,
a74aed
 						    &pub, &priv,
a74aed
diff --git a/tests/rsa-rsa-pss.c b/tests/rsa-rsa-pss.c
a74aed
index 19a175b722..d7799c1961 100644
a74aed
--- a/tests/rsa-rsa-pss.c
a74aed
+++ b/tests/rsa-rsa-pss.c
a74aed
@@ -46,6 +46,8 @@ const gnutls_datum_t raw_data = {
a74aed
 	11
a74aed
 };
a74aed
 
a74aed
+static gnutls_fips140_context_t fips_context;
a74aed
+
a74aed
 static void inv_sign_check(unsigned sigalgo,
a74aed
 			   gnutls_privkey_t privkey, int exp_error)
a74aed
 {
a74aed
@@ -86,13 +88,16 @@ static void inv_encryption_check(gnutls_pk_algorithm_t algorithm,
a74aed
 
a74aed
 static void sign_verify_data(unsigned sigalgo, gnutls_privkey_t privkey,
a74aed
 			     unsigned int sign_flags, unsigned int verify_flags,
a74aed
-			     int sign_exp_error, int verify_exp_error)
a74aed
+			     int sign_exp_error, int verify_exp_error,
a74aed
+			     gnutls_fips140_operation_state_t sign_exp_state)
a74aed
 {
a74aed
 	int ret;
a74aed
 	gnutls_datum_t signature = { NULL, 0 };
a74aed
 
a74aed
+	fips_push_context(fips_context);
a74aed
 	ret = gnutls_privkey_sign_data2(privkey, sigalgo, sign_flags,
a74aed
 					&raw_data, &signature);
a74aed
+	fips_pop_context(fips_context, sign_exp_state);
a74aed
 	if (ret != sign_exp_error)
a74aed
 		fail("gnutls_x509_privkey_sign_data returned unexpected error: %s\n",
a74aed
 		     gnutls_strerror(ret));
a74aed
@@ -180,11 +185,16 @@ void doit(void)
a74aed
 	if (debug)
a74aed
 		gnutls_global_set_log_level(4711);
a74aed
 
a74aed
+	assert(gnutls_fips140_context_init(&fips_context) >= 0);
a74aed
+
a74aed
 	prepare_keys(&pkey_rsa_pss, &pkey_rsa, GNUTLS_DIG_SHA256, 32);
a74aed
 
a74aed
-	sign_verify_data(GNUTLS_SIGN_RSA_PSS_SHA256, pkey_rsa_pss, 0, 0, 0, 0);
a74aed
-	sign_verify_data(GNUTLS_SIGN_RSA_PSS_RSAE_SHA256, pkey_rsa, 0, 0, 0, 0);
a74aed
-	sign_verify_data(GNUTLS_SIGN_RSA_PSS_SHA256, pkey_rsa, 0, 0, 0, 0);
a74aed
+	sign_verify_data(GNUTLS_SIGN_RSA_PSS_SHA256, pkey_rsa_pss, 0, 0, 0, 0,
a74aed
+			 GNUTLS_FIPS140_OP_APPROVED);
a74aed
+	sign_verify_data(GNUTLS_SIGN_RSA_PSS_RSAE_SHA256, pkey_rsa, 0, 0, 0, 0,
a74aed
+			 GNUTLS_FIPS140_OP_APPROVED);
a74aed
+	sign_verify_data(GNUTLS_SIGN_RSA_PSS_SHA256, pkey_rsa, 0, 0, 0, 0,
a74aed
+			 GNUTLS_FIPS140_OP_APPROVED);
a74aed
 
a74aed
 	if (debug)
a74aed
 		success("success signing with RSA-PSS-SHA256\n");
a74aed
@@ -213,41 +223,65 @@ void doit(void)
a74aed
 	gnutls_privkey_deinit(pkey_rsa_pss);
a74aed
 	gnutls_privkey_deinit(pkey_rsa);
a74aed
 
a74aed
+	/* Restrict key to use salt length larger than hash output
a74aed
+	 * length (not approved in FIPS).
a74aed
+	 */
a74aed
+	prepare_keys(&pkey_rsa_pss, &pkey_rsa, GNUTLS_DIG_SHA256, 33);
a74aed
+
a74aed
+	sign_verify_data(GNUTLS_SIGN_RSA_PSS_SHA256, pkey_rsa_pss, 0, 0, 0, 0,
a74aed
+			 GNUTLS_FIPS140_OP_NOT_APPROVED);
a74aed
+
a74aed
+	gnutls_privkey_deinit(pkey_rsa_pss);
a74aed
+	gnutls_privkey_deinit(pkey_rsa);
a74aed
+
a74aed
 	/* Use the mismatched salt length with the digest length */
a74aed
 	prepare_keys(&pkey_rsa_pss, &pkey_rsa, GNUTLS_DIG_SHA256, 48);
a74aed
 
a74aed
 	sign_verify_data(GNUTLS_SIGN_RSA_PSS_RSAE_SHA256, pkey_rsa_pss,
a74aed
-			 0, 0, 0, 0);
a74aed
+			 0, 0, 0, 0, GNUTLS_FIPS140_OP_NOT_APPROVED);
a74aed
 	sign_verify_data(GNUTLS_SIGN_RSA_PSS_SHA256, pkey_rsa_pss,
a74aed
 			 GNUTLS_PRIVKEY_FLAG_RSA_PSS_FIXED_SALT_LENGTH,
a74aed
 			 0,
a74aed
 			 GNUTLS_E_CONSTRAINT_ERROR,
a74aed
-			 0);
a74aed
+			 0,
a74aed
+			 /* The error is caught before calling the actual
a74aed
+			  * signing operation.
a74aed
+			  */
a74aed
+			 GNUTLS_FIPS140_OP_INITIAL);
a74aed
 	sign_verify_data(GNUTLS_SIGN_RSA_PSS_SHA256, pkey_rsa_pss,
a74aed
 			 0,
a74aed
 			 GNUTLS_VERIFY_RSA_PSS_FIXED_SALT_LENGTH,
a74aed
 			 0,
a74aed
-			 GNUTLS_E_PK_SIG_VERIFY_FAILED);
a74aed
+			 GNUTLS_E_PK_SIG_VERIFY_FAILED,
a74aed
+			 GNUTLS_FIPS140_OP_NOT_APPROVED);
a74aed
 
a74aed
 	assert(gnutls_x509_spki_init(&spki)>=0);
a74aed
 	gnutls_x509_spki_set_rsa_pss_params(spki, GNUTLS_DIG_SHA256, 48);
a74aed
 	assert(gnutls_privkey_set_spki(pkey_rsa, spki, 0)>=0);
a74aed
 
a74aed
-	sign_verify_data(GNUTLS_SIGN_RSA_PSS_RSAE_SHA256, pkey_rsa, 0, 0, 0, 0);
a74aed
+	sign_verify_data(GNUTLS_SIGN_RSA_PSS_RSAE_SHA256, pkey_rsa, 0, 0, 0, 0,
a74aed
+			 GNUTLS_FIPS140_OP_NOT_APPROVED);
a74aed
 	sign_verify_data(GNUTLS_SIGN_RSA_PSS_RSAE_SHA256, pkey_rsa,
a74aed
 			 GNUTLS_PRIVKEY_FLAG_RSA_PSS_FIXED_SALT_LENGTH,
a74aed
 			 0,
a74aed
 			 GNUTLS_E_CONSTRAINT_ERROR,
a74aed
-			 0);
a74aed
+			 0,
a74aed
+			 /* The error is caught before calling the actual
a74aed
+			  * signing operation.
a74aed
+			  */
a74aed
+			 GNUTLS_FIPS140_OP_INITIAL);
a74aed
 	sign_verify_data(GNUTLS_SIGN_RSA_PSS_RSAE_SHA256, pkey_rsa,
a74aed
 			 0,
a74aed
 			 GNUTLS_VERIFY_RSA_PSS_FIXED_SALT_LENGTH,
a74aed
 			 0,
a74aed
-			 GNUTLS_E_PK_SIG_VERIFY_FAILED);
a74aed
+			 GNUTLS_E_PK_SIG_VERIFY_FAILED,
a74aed
+			 GNUTLS_FIPS140_OP_NOT_APPROVED);
a74aed
 
a74aed
 	gnutls_privkey_deinit(pkey_rsa_pss);
a74aed
 	gnutls_privkey_deinit(pkey_rsa);
a74aed
 	gnutls_x509_spki_deinit(spki);
a74aed
 
a74aed
+	gnutls_fips140_context_deinit(fips_context);
a74aed
+
a74aed
 	gnutls_global_deinit();
a74aed
 }
a74aed
-- 
a74aed
2.38.1
a74aed