Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1jggpb-0007K5-FL for mharc-grub-devel@gnu.org; Wed, 03 Jun 2020 23:46:15 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51642) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jggpZ-0007Fl-K0 for grub-devel@gnu.org; Wed, 03 Jun 2020 23:46:13 -0400 Received: from cyberdimension.org ([2001:910:1314:ffff::1]:51798 helo=gnutoo.cyberdimension.org) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_CHACHA20_POLY1305:256) (Exim 4.90_1) (envelope-from ) id 1jggpX-0006Mp-KM for grub-devel@gnu.org; Wed, 03 Jun 2020 23:46:13 -0400 Received: from gnutoo.cyberdimension.org (localhost [127.0.0.1]) by cyberdimension.org (OpenSMTPD) with ESMTP id 55af26ca; Thu, 4 Jun 2020 03:42:38 +0000 (UTC) Received: from primarylaptop.localdomain (localhost.localdomain [::1]) by gnutoo.cyberdimension.org (OpenSMTPD) with ESMTP id 3b94fabe; Thu, 4 Jun 2020 03:42:38 +0000 (UTC) From: Denis 'GNUtoo' Carikli To: Patrick Steinhardt , Daniel Kiper Cc: The development of GNU GRUB , John Lane , Denis 'GNUtoo' Carikli Subject: [PATCH v4][ 4/6] cryptodisk: add support for LUKS1 detached headers Date: Thu, 4 Jun 2020 05:43:22 +0200 Message-Id: <20200604034324.4565-5-GNUtoo@cyberdimension.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200604034324.4565-1-GNUtoo@cyberdimension.org> References: <20200604034324.4565-1-GNUtoo@cyberdimension.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2001:910:1314:ffff::1; envelope-from=GNUtoo@cyberdimension.org; helo=gnutoo.cyberdimension.org X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 04 Jun 2020 03:46:13 -0000 From: John Lane cryptsetup supports having a detached header through the --header command line argument for both LUKS1 and LUKS2. This adds support for LUKS1 detached headers. Signed-off-by: John Lane GNUtoo@cyberdimension.org: rebase, small fixes, commit message Signed-off-by: Denis 'GNUtoo' Carikli --- grub-core/disk/luks.c | 48 ++++++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/grub-core/disk/luks.c b/grub-core/disk/luks.c index ffeb679d1..0b20908ac 100644 --- a/grub-core/disk/luks.c +++ b/grub-core/disk/luks.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -76,17 +77,23 @@ luks_scan (grub_disk_t disk, const char *check_uuid, int check_boot, char ciphername[sizeof (header.cipherName) + 1]; char ciphermode[sizeof (header.cipherMode) + 1]; char hashspec[sizeof (header.hashSpec) + 1]; - grub_err_t err; - - /* Detached headers are not implemented yet */ - if (hdr) - return NULL; + grub_err_t err = GRUB_ERR_NONE; if (check_boot) return NULL; /* Read the LUKS header. */ - err = grub_disk_read (disk, 0, 0, sizeof (header), &header); + if (hdr) + { + if (grub_file_seek (hdr, 0) == (grub_off_t) -1) + return NULL; + + if (grub_file_read (hdr, &header, sizeof (header)) != sizeof (header)) + return NULL; + } + else + err = grub_disk_read (disk, 0, 0, sizeof (header), &header); + if (err) { if (err == GRUB_ERR_OUT_OF_RANGE) @@ -163,15 +170,22 @@ luks_recover_key (grub_disk_t source, grub_cryptodisk_t dev, grub_file_t hdr) grub_uint8_t candidate_digest[sizeof (header.mkDigest)]; unsigned i; grub_size_t length; - grub_err_t err; + grub_err_t err = GRUB_ERR_NONE; grub_size_t max_stripes = 1; char *tmp; + grub_uint32_t sector; - /* Detached headers are not implemented yet */ if (hdr) - return GRUB_ERR_NOT_IMPLEMENTED_YET; + { + if (grub_file_seek (hdr, 0) == (grub_off_t) -1) + return grub_errno; + + if (grub_file_read (hdr, &header, sizeof (header)) != sizeof (header)) + return grub_errno; + } + else + err = grub_disk_read (source, 0, 0, sizeof (header), &header); - err = grub_disk_read (source, 0, 0, sizeof (header), &header); if (err) return err; @@ -240,13 +254,19 @@ luks_recover_key (grub_disk_t source, grub_cryptodisk_t dev, grub_file_t hdr) return grub_crypto_gcry_error (gcry_err); } + sector = grub_be_to_cpu32 (header.keyblock[i].keyMaterialOffset); length = (keysize * grub_be_to_cpu32 (header.keyblock[i].stripes)); /* Read and decrypt the key material from the disk. */ - err = grub_disk_read (source, - grub_be_to_cpu32 (header.keyblock - [i].keyMaterialOffset), 0, - length, split_key); + if (hdr) + { + if (grub_file_seek (hdr, sector * 512)) + return grub_errno; + if (grub_file_read (hdr, split_key, length) != (grub_ssize_t)length) + return grub_errno; + } + else + err = grub_disk_read (source, sector, 0, length, split_key); if (err) { grub_free (split_key); -- 2.26.2