Blame SOURCES/gnutls-3.6.8-fips-rsa-random-selftests.patch

cde47b
From fbb6dd2a65c6fc7a2e9bd82fe66fde54f6cf2952 Mon Sep 17 00:00:00 2001
cde47b
From: Daiki Ueno <dueno@redhat.com>
cde47b
Date: Fri, 16 Aug 2019 17:01:05 +0200
cde47b
Subject: [PATCH] nettle: disable RSA blinding in FIPS selftests
cde47b
cde47b
Nettle's RSA signing, encryption and decryption functions still
cde47b
require randomness for blinding, so fallback to use a fixed buffer in
cde47b
selftests where entropy might not be available.
cde47b
cde47b
Signed-off-by: Daiki Ueno <dueno@redhat.com>
cde47b
---
cde47b
 lib/nettle/pk.c | 37 +++++++++++++++++++++++++++++++++----
cde47b
 1 file changed, 33 insertions(+), 4 deletions(-)
cde47b
cde47b
diff --git a/lib/nettle/pk.c b/lib/nettle/pk.c
cde47b
index b2d27cf74..772fcdc21 100644
cde47b
--- a/lib/nettle/pk.c
cde47b
+++ b/lib/nettle/pk.c
cde47b
@@ -94,6 +94,15 @@ static void rnd_mpz_func(void *_ctx, size_t length, uint8_t * data)
cde47b
 	nettle_mpz_get_str_256 (length, data, *k);
cde47b
 }
cde47b
 
cde47b
+static void rnd_nonce_func_fallback(void *_ctx, size_t length, uint8_t * data)
cde47b
+{
cde47b
+	if (unlikely(_gnutls_get_lib_state() != LIB_STATE_SELFTEST)) {
cde47b
+		_gnutls_switch_lib_state(LIB_STATE_ERROR);
cde47b
+	}
cde47b
+
cde47b
+	memset(data, 0xAA, length);
cde47b
+}
cde47b
+
cde47b
 static void
cde47b
 ecc_scalar_zclear (struct ecc_scalar *s)
cde47b
 {
cde47b
@@ -435,6 +444,7 @@ _wrap_nettle_pk_encrypt(gnutls_pk_algorithm_t algo,
cde47b
 	case GNUTLS_PK_RSA:
cde47b
 		{
cde47b
 			struct rsa_public_key pub;
cde47b
+			nettle_random_func *random_func;
cde47b
 
cde47b
 			ret = _rsa_params_to_pubkey(pk_params, &pub;;
cde47b
 			if (ret < 0) {
cde47b
@@ -442,8 +452,12 @@ _wrap_nettle_pk_encrypt(gnutls_pk_algorithm_t algo,
cde47b
 				goto cleanup;
cde47b
 			}
cde47b
 
cde47b
+			if (_gnutls_get_lib_state() == LIB_STATE_SELFTEST)
cde47b
+				random_func = rnd_nonce_func_fallback;
cde47b
+			else
cde47b
+				random_func = rnd_nonce_func;
cde47b
 			ret =
cde47b
-			    rsa_encrypt(&pub, NULL, rnd_nonce_func,
cde47b
+			    rsa_encrypt(&pub, NULL, random_func,
cde47b
 					plaintext->size, plaintext->data,
cde47b
 					p);
cde47b
 			if (ret == 0 || HAVE_LIB_ERROR()) {
cde47b
@@ -496,6 +510,7 @@ _wrap_nettle_pk_decrypt(gnutls_pk_algorithm_t algo,
cde47b
 			struct rsa_public_key pub;
cde47b
 			size_t length;
cde47b
 			bigint_t c;
cde47b
+			nettle_random_func *random_func;
cde47b
 
cde47b
 			_rsa_params_to_privkey(pk_params, &priv;;
cde47b
 			ret = _rsa_params_to_pubkey(pk_params, &pub;;
cde47b
@@ -526,8 +541,12 @@ _wrap_nettle_pk_decrypt(gnutls_pk_algorithm_t algo,
cde47b
 				goto cleanup;
cde47b
 			}
cde47b
 
cde47b
+			if (_gnutls_get_lib_state() == LIB_STATE_SELFTEST)
cde47b
+				random_func = rnd_nonce_func_fallback;
cde47b
+			else
cde47b
+				random_func = rnd_nonce_func;
cde47b
 			ret =
cde47b
-			    rsa_decrypt_tr(&pub, &priv, NULL, rnd_nonce_func,
cde47b
+			    rsa_decrypt_tr(&pub, &priv, NULL, random_func,
cde47b
 					   &length, plaintext->data,
cde47b
 					   TOMPZ(c));
cde47b
 			_gnutls_mpi_release(&c);
cde47b
@@ -573,6 +592,7 @@ _wrap_nettle_pk_decrypt2(gnutls_pk_algorithm_t algo,
cde47b
 	bigint_t c;
cde47b
 	uint32_t is_err;
cde47b
 	int ret;
cde47b
+	nettle_random_func *random_func;
cde47b
 
cde47b
 	if (algo != GNUTLS_PK_RSA || plaintext == NULL) {
cde47b
 		gnutls_assert();
cde47b
@@ -592,7 +612,11 @@ _wrap_nettle_pk_decrypt2(gnutls_pk_algorithm_t algo,
cde47b
 		return gnutls_assert_val (GNUTLS_E_MPI_SCAN_FAILED);
cde47b
 	}
cde47b
 
cde47b
-	ret = rsa_sec_decrypt(&pub, &priv, NULL, rnd_nonce_func,
cde47b
+	if (_gnutls_get_lib_state() == LIB_STATE_SELFTEST)
cde47b
+		random_func = rnd_nonce_func_fallback;
cde47b
+	else
cde47b
+		random_func = rnd_nonce_func;
cde47b
+	ret = rsa_sec_decrypt(&pub, &priv, NULL, random_func,
cde47b
 			     plaintext_size, plaintext, TOMPZ(c));
cde47b
 	/* after this point, any conditional on failure that cause differences
cde47b
 	 * in execution may create a timing or cache access pattern side
cde47b
@@ -942,6 +966,7 @@ _wrap_nettle_pk_sign(gnutls_pk_algorithm_t algo,
cde47b
 		{
cde47b
 			struct rsa_private_key priv;
cde47b
 			struct rsa_public_key pub;
cde47b
+			nettle_random_func *random_func;
cde47b
 			mpz_t s;
cde47b
 
cde47b
 			_rsa_params_to_privkey(pk_params, &priv;;
cde47b
@@ -952,8 +977,12 @@ _wrap_nettle_pk_sign(gnutls_pk_algorithm_t algo,
cde47b
 
cde47b
 			mpz_init(s);
cde47b
 
cde47b
+			if (_gnutls_get_lib_state() == LIB_STATE_SELFTEST)
cde47b
+				random_func = rnd_nonce_func_fallback;
cde47b
+			else
cde47b
+				random_func = rnd_nonce_func;
cde47b
 			ret =
cde47b
-			    rsa_pkcs1_sign_tr(&pub, &priv, NULL, rnd_nonce_func,
cde47b
+			    rsa_pkcs1_sign_tr(&pub, &priv, NULL, random_func,
cde47b
 					      vdata->size, vdata->data, s);
cde47b
 			if (ret == 0 || HAVE_LIB_ERROR()) {
cde47b
 				gnutls_assert();
cde47b
-- 
cde47b
2.21.0
cde47b