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

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