Blame SOURCES/0002-Fix-default-key-size-for-non-XTS-ciphers.patch

a9076e
From 5d29bc014a33d9bdc1c5fb4b8add2f38850f46a8 Mon Sep 17 00:00:00 2001
a9076e
From: Vojtech Trefny <vtrefny@redhat.com>
a9076e
Date: Wed, 24 Feb 2021 14:44:03 +0100
a9076e
Subject: [PATCH] crypto: Fix default key size for non XTS ciphers
a9076e
a9076e
512 bits should be default only for AES-XTS which needs two keys,
a9076e
default for other modes must be 256 bits.
a9076e
a9076e
resolves: rhbz#1931847
a9076e
---
a9076e
 src/plugins/crypto.c | 11 +++++++++--
a9076e
 src/plugins/crypto.h |  2 +-
a9076e
 tests/crypto_test.py | 36 ++++++++++++++++++++++++++++++++++++
a9076e
 3 files changed, 46 insertions(+), 3 deletions(-)
a9076e
a9076e
diff --git a/src/plugins/crypto.c b/src/plugins/crypto.c
a9076e
index f4a2e8f0..1e7043fa 100644
a9076e
--- a/src/plugins/crypto.c
a9076e
+++ b/src/plugins/crypto.c
a9076e
@@ -774,8 +774,15 @@ static gboolean luks_format (const gchar *device, const gchar *cipher, guint64 k
a9076e
         return FALSE;
a9076e
     }
a9076e
 
a9076e
-    /* resolve requested/default key_size (should be in bytes) */
a9076e
-    key_size = (key_size != 0) ? (key_size / 8) : (DEFAULT_LUKS_KEYSIZE_BITS / 8);
a9076e
+    if (key_size == 0) {
a9076e
+        if (g_str_has_prefix (cipher_specs[1], "xts-"))
a9076e
+            key_size = DEFAULT_LUKS_KEYSIZE_BITS * 2;
a9076e
+        else
a9076e
+            key_size = DEFAULT_LUKS_KEYSIZE_BITS;
a9076e
+    }
a9076e
+
a9076e
+    /* key_size should be in bytes */
a9076e
+    key_size = key_size / 8;
a9076e
 
a9076e
     /* wait for enough random data entropy (if requested) */
a9076e
     if (min_entropy > 0) {
a9076e
diff --git a/src/plugins/crypto.h b/src/plugins/crypto.h
a9076e
index 71a1438d..a38724d9 100644
a9076e
--- a/src/plugins/crypto.h
a9076e
+++ b/src/plugins/crypto.h
a9076e
@@ -36,7 +36,7 @@ typedef enum {
a9076e
 /* 20 chars * 6 bits per char (64-item charset) = 120 "bits of security" */
a9076e
 #define BD_CRYPTO_BACKUP_PASSPHRASE_LENGTH 20
a9076e
 
a9076e
-#define DEFAULT_LUKS_KEYSIZE_BITS 512
a9076e
+#define DEFAULT_LUKS_KEYSIZE_BITS 256
a9076e
 #define DEFAULT_LUKS_CIPHER "aes-xts-plain64"
a9076e
 #define DEFAULT_LUKS2_SECTOR_SIZE 512
a9076e
 
a9076e
diff --git a/tests/crypto_test.py b/tests/crypto_test.py
a9076e
index 0609a070..0aecc032 100644
a9076e
--- a/tests/crypto_test.py
a9076e
+++ b/tests/crypto_test.py
a9076e
@@ -236,6 +236,42 @@ def test_luks2_format(self):
a9076e
             self.fail("Failed to get pbkdf information from:\n%s %s" % (out, err))
a9076e
         self.assertEqual(int(m.group(1)), 5)
a9076e
 
a9076e
+    def _get_luks1_key_size(self, device):
a9076e
+        _ret, out, err = run_command("cryptsetup luksDump %s" % device)
a9076e
+        m = re.search(r"MK bits:\s*(\S+)\s*", out)
a9076e
+        if not m or len(m.groups()) != 1:
a9076e
+            self.fail("Failed to get key size information from:\n%s %s" % (out, err))
a9076e
+        key_size = m.group(1)
a9076e
+        if not key_size.isnumeric():
a9076e
+            self.fail("Failed to get key size information from: %s" % key_size)
a9076e
+        return int(key_size)
a9076e
+
a9076e
+    @tag_test(TestTags.SLOW, TestTags.CORE)
a9076e
+    def test_luks_format_key_size(self):
a9076e
+        """Verify that formating device as LUKS works"""
a9076e
+
a9076e
+        # aes-xts: key size should default to 512
a9076e
+        succ = BlockDev.crypto_luks_format(self.loop_dev, "aes-xts-plain64", 0, PASSWD, None, 0)
a9076e
+        self.assertTrue(succ)
a9076e
+
a9076e
+        key_size = self._get_luks1_key_size(self.loop_dev)
a9076e
+        self.assertEqual(key_size, 512)
a9076e
+
a9076e
+        # aes-cbc: key size should default to 256
a9076e
+        succ = BlockDev.crypto_luks_format(self.loop_dev, "aes-cbc-essiv:sha256", 0, PASSWD, None, 0)
a9076e
+        self.assertTrue(succ)
a9076e
+
a9076e
+        key_size = self._get_luks1_key_size(self.loop_dev)
a9076e
+        self.assertEqual(key_size, 256)
a9076e
+
a9076e
+        # try specifying key size for aes-xts
a9076e
+        succ = BlockDev.crypto_luks_format(self.loop_dev, "aes-xts-plain64", 256, PASSWD, None, 0)
a9076e
+        self.assertTrue(succ)
a9076e
+
a9076e
+        key_size = self._get_luks1_key_size(self.loop_dev)
a9076e
+        self.assertEqual(key_size, 256)
a9076e
+
a9076e
+
a9076e
 class CryptoTestResize(CryptoTestCase):
a9076e
 
a9076e
     def _get_key_location(self, device):