Blame SOURCES/gnutls-3.6.8-fix-cfb8-decrypt.patch

1d35f3
From 1c2135506825ae80966fe2797613806916b7e3c0 Mon Sep 17 00:00:00 2001
1d35f3
From: Daiki Ueno <dueno@redhat.com>
1d35f3
Date: Wed, 6 Nov 2019 12:07:24 +0100
1d35f3
Subject: [PATCH 1/2] nettle: backport fixes to cfb8_decrypt
1d35f3
1d35f3
cfb8: don't truncate output IV if input is shorter than block size:
1d35f3
https://git.lysator.liu.se/nettle/nettle/commit/f4a9c842621baf5d71aa9cc3989851f44dc46861
1d35f3
1d35f3
Signed-off-by: Daiki Ueno <dueno@redhat.com>
1d35f3
---
1d35f3
 lib/nettle/backport/cfb8.c | 10 ++++++----
1d35f3
 1 file changed, 6 insertions(+), 4 deletions(-)
1d35f3
1d35f3
diff --git a/lib/nettle/backport/cfb8.c b/lib/nettle/backport/cfb8.c
1d35f3
index e9816feb7..1762192f4 100644
1d35f3
--- a/lib/nettle/backport/cfb8.c
1d35f3
+++ b/lib/nettle/backport/cfb8.c
1d35f3
@@ -110,10 +110,12 @@ cfb8_decrypt(const void *ctx, nettle_cipher_func *f,
1d35f3
       src += i;
1d35f3
       dst += i;
1d35f3
 
1d35f3
-      memcpy(buffer, buffer + block_size, block_size);
1d35f3
-      memcpy(buffer + block_size, src,
1d35f3
-	     length < block_size ? length : block_size);
1d35f3
-
1d35f3
+      if (i == block_size)
1d35f3
+	{
1d35f3
+	  memcpy(buffer, buffer + block_size, block_size);
1d35f3
+	  memcpy(buffer + block_size, src,
1d35f3
+		 length < block_size ? length : block_size);
1d35f3
+	}
1d35f3
     }
1d35f3
 
1d35f3
   memcpy(iv, buffer + i, block_size);
1d35f3
-- 
1d35f3
2.21.0
1d35f3
1d35f3
1d35f3
From cc01347302678719f0bcfb4f3383fe0f1e905ed8 Mon Sep 17 00:00:00 2001
1d35f3
From: =?UTF-8?q?G=C3=BCnther=20Deschner?= <gd@samba.org>
1d35f3
Date: Wed, 6 Nov 2019 13:17:57 +0100
1d35f3
Subject: [PATCH 2/2] crypto-selftests: test CFB8 ciphers with different
1d35f3
 chunksizes
1d35f3
1d35f3
Signed-off-by: Guenther Deschner <gd@samba.org>
1d35f3
Signed-off-by: Daiki Ueno <dueno@redhat.com>
1d35f3
---
1d35f3
 lib/crypto-selftests.c | 124 +++++++++++++++++++++++++++++++++++++++--
1d35f3
 1 file changed, 118 insertions(+), 6 deletions(-)
1d35f3
1d35f3
diff --git a/lib/crypto-selftests.c b/lib/crypto-selftests.c
1d35f3
index 6caf817e8..5f0a4ec8b 100644
1d35f3
--- a/lib/crypto-selftests.c
1d35f3
+++ b/lib/crypto-selftests.c
1d35f3
@@ -710,6 +710,107 @@ static int test_cipher(gnutls_cipher_algorithm_t cipher,
1d35f3
 	return 0;
1d35f3
 }
1d35f3
 
1d35f3
+static int test_cipher_all_block_sizes(gnutls_cipher_algorithm_t cipher,
1d35f3
+				       const struct cipher_vectors_st *vectors,
1d35f3
+				       size_t vectors_size, unsigned flags)
1d35f3
+{
1d35f3
+	gnutls_cipher_hd_t hd;
1d35f3
+	int ret;
1d35f3
+	unsigned int i;
1d35f3
+	uint8_t tmp[384];
1d35f3
+	gnutls_datum_t key, iv = {NULL, 0};
1d35f3
+	size_t block;
1d35f3
+	size_t offset;
1d35f3
+
1d35f3
+	for (i = 0; i < vectors_size; i++) {
1d35f3
+		for (block = 1; block <= vectors[i].plaintext_size; block++) {
1d35f3
+			key.data = (void *) vectors[i].key;
1d35f3
+			key.size = vectors[i].key_size;
1d35f3
+
1d35f3
+			iv.data = (void *) vectors[i].iv;
1d35f3
+			iv.size = gnutls_cipher_get_iv_size(cipher);
1d35f3
+
1d35f3
+			if (iv.size != vectors[i].iv_size)
1d35f3
+				return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
1d35f3
+
1d35f3
+			ret = gnutls_cipher_init(&hd, cipher, &key, &iv;;
1d35f3
+			if (ret < 0) {
1d35f3
+				_gnutls_debug_log("error initializing: %s\n",
1d35f3
+						  gnutls_cipher_get_name(cipher));
1d35f3
+				return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
1d35f3
+			}
1d35f3
+
1d35f3
+			for (offset = 0;
1d35f3
+			     offset < vectors[i].plaintext_size;
1d35f3
+			     offset += block) {
1d35f3
+				ret =
1d35f3
+				    gnutls_cipher_encrypt2(hd,
1d35f3
+							   vectors[i].plaintext + offset,
1d35f3
+							   MIN(block, vectors[i].plaintext_size - offset),
1d35f3
+							   tmp + offset,
1d35f3
+							   sizeof(tmp) - offset);
1d35f3
+				if (ret < 0)
1d35f3
+					return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
1d35f3
+			}
1d35f3
+
1d35f3
+			if (memcmp
1d35f3
+			    (tmp, vectors[i].ciphertext,
1d35f3
+			     vectors[i].plaintext_size) != 0) {
1d35f3
+				_gnutls_debug_log("%s encryption of test vector %d failed with block size %d/%d!\n",
1d35f3
+						  gnutls_cipher_get_name(cipher),
1d35f3
+						  i, (int)block, (int)vectors[i].plaintext_size);
1d35f3
+				return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
1d35f3
+			}
1d35f3
+
1d35f3
+			gnutls_cipher_deinit(hd);
1d35f3
+		}
1d35f3
+	}
1d35f3
+
1d35f3
+	for (i = 0; i < vectors_size; i++) {
1d35f3
+		for (block = 1; block <= vectors[i].plaintext_size; block++) {
1d35f3
+			key.data = (void *) vectors[i].key;
1d35f3
+			key.size = vectors[i].key_size;
1d35f3
+
1d35f3
+			iv.data = (void *) vectors[i].iv;
1d35f3
+			iv.size = gnutls_cipher_get_iv_size(cipher);
1d35f3
+
1d35f3
+			ret = gnutls_cipher_init(&hd, cipher, &key, &iv;;
1d35f3
+			if (ret < 0)
1d35f3
+				return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
1d35f3
+
1d35f3
+			for (offset = 0;
1d35f3
+			     offset + block <= vectors[i].plaintext_size;
1d35f3
+			     offset += block) {
1d35f3
+				ret =
1d35f3
+				    gnutls_cipher_decrypt2(hd,
1d35f3
+							   vectors[i].ciphertext + offset,
1d35f3
+							   MIN(block, vectors[i].plaintext_size - offset),
1d35f3
+							   tmp + offset,
1d35f3
+							   sizeof(tmp) - offset);
1d35f3
+				if (ret < 0)
1d35f3
+					return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
1d35f3
+			}
1d35f3
+
1d35f3
+			if (memcmp
1d35f3
+			    (tmp, vectors[i].plaintext,
1d35f3
+			     vectors[i].plaintext_size) != 0) {
1d35f3
+				_gnutls_debug_log("%s decryption of test vector %d failed with block size %d!\n",
1d35f3
+						  gnutls_cipher_get_name(cipher),
1d35f3
+						  i, (int)block);
1d35f3
+				return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
1d35f3
+			}
1d35f3
+
1d35f3
+			gnutls_cipher_deinit(hd);
1d35f3
+		}
1d35f3
+	}
1d35f3
+
1d35f3
+	_gnutls_debug_log
1d35f3
+	    ("%s self check succeeded\n",
1d35f3
+	     gnutls_cipher_get_name(cipher));
1d35f3
+
1d35f3
+	return 0;
1d35f3
+}
1d35f3
+
1d35f3
 /* AEAD modes (compat APIs) */
1d35f3
 static int test_cipher_aead_compat(gnutls_cipher_algorithm_t cipher,
1d35f3
 			    const struct cipher_aead_vectors_st *vectors,
1d35f3
@@ -1721,6 +1822,14 @@ static int test_mac(gnutls_mac_algorithm_t mac,
1d35f3
 			if (!(flags & GNUTLS_SELF_TEST_FLAG_ALL) || ret < 0) \
1d35f3
 				return ret
1d35f3
 
1d35f3
+#define CASE2(x, func, func2, vectors) case x:	  \
1d35f3
+			ret = func(x, V(vectors), flags); \
1d35f3
+			if (!(flags & GNUTLS_SELF_TEST_FLAG_ALL) || ret < 0) \
1d35f3
+				return ret; \
1d35f3
+			ret = func2(x, V(vectors), flags); \
1d35f3
+			if (!(flags & GNUTLS_SELF_TEST_FLAG_ALL) || ret < 0) \
1d35f3
+				return ret
1d35f3
+
1d35f3
 #define NON_FIPS_CASE(x, func, vectors) case x: \
1d35f3
 			if (_gnutls_fips_mode_enabled() == 0) { \
1d35f3
 				ret = func(x, V(vectors), flags); \
1d35f3
@@ -1786,14 +1895,17 @@ int gnutls_cipher_self_test(unsigned flags, gnutls_cipher_algorithm_t cipher)
1d35f3
 		NON_FIPS_CASE(GNUTLS_CIPHER_CHACHA20_POLY1305, test_cipher_aead,
1d35f3
 		     chacha_poly1305_vectors);
1d35f3
 		FALLTHROUGH;
1d35f3
-		CASE(GNUTLS_CIPHER_AES_128_CFB8, test_cipher,
1d35f3
-		     aes128_cfb8_vectors);
1d35f3
+		CASE2(GNUTLS_CIPHER_AES_128_CFB8, test_cipher,
1d35f3
+		      test_cipher_all_block_sizes,
1d35f3
+		      aes128_cfb8_vectors);
1d35f3
 		FALLTHROUGH;
1d35f3
-		CASE(GNUTLS_CIPHER_AES_192_CFB8, test_cipher,
1d35f3
-		     aes192_cfb8_vectors);
1d35f3
+		CASE2(GNUTLS_CIPHER_AES_192_CFB8, test_cipher,
1d35f3
+		      test_cipher_all_block_sizes,
1d35f3
+		      aes192_cfb8_vectors);
1d35f3
 		FALLTHROUGH;
1d35f3
-		CASE(GNUTLS_CIPHER_AES_256_CFB8, test_cipher,
1d35f3
-		     aes256_cfb8_vectors);
1d35f3
+		CASE2(GNUTLS_CIPHER_AES_256_CFB8, test_cipher,
1d35f3
+		      test_cipher_all_block_sizes,
1d35f3
+		      aes256_cfb8_vectors);
1d35f3
 		FALLTHROUGH;
1d35f3
 		CASE(GNUTLS_CIPHER_AES_128_XTS, test_cipher,
1d35f3
 		     aes128_xts_vectors);
1d35f3
-- 
1d35f3
2.21.0
1d35f3