Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1jE76B-0008CT-H9 for mharc-grub-devel@gnu.org; Tue, 17 Mar 2020 03:57:15 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:48933) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jE767-000887-F9 for grub-devel@gnu.org; Tue, 17 Mar 2020 03:57:12 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jE766-000096-Bt for grub-devel@gnu.org; Tue, 17 Mar 2020 03:57:11 -0400 Received: from cyberdimension.org ([2001:910:1314:ffff::1]:53678 helo=gnutoo.cyberdimension.org) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jE766-0008P7-1r for grub-devel@gnu.org; Tue, 17 Mar 2020 03:57:10 -0400 Received: from gnutoo.cyberdimension.org (localhost [127.0.0.1]) by cyberdimension.org (OpenSMTPD) with ESMTP id 276ebc67; Tue, 17 Mar 2020 07:55:01 +0000 (UTC) Received: from primarylaptop.localdomain (localhost.localdomain [::1]) by gnutoo.cyberdimension.org (OpenSMTPD) with ESMTP id d18406c1; Tue, 17 Mar 2020 07:55:00 +0000 (UTC) From: Denis 'GNUtoo' Carikli To: Patrick Steinhardt , Daniel Kiper Cc: The development of GNU GRUB , Denis 'GNUtoo' Carikli , John Lane Subject: [PATCHv2][ 6/6] cryptodisk: Add support for LUKS1 key files Date: Tue, 17 Mar 2020 08:57:40 +0100 Message-Id: <20200317075740.5430-6-GNUtoo@cyberdimension.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200317075740.5430-1-GNUtoo@cyberdimension.org> References: <20200317075740.5430-1-GNUtoo@cyberdimension.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2001:910:1314:ffff::1 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: Tue, 17 Mar 2020 07:57:12 -0000 cryptsetup supports key files thourh the --key-file --header command line argument for both LUKS1 and LUKS2. This adds support for LUKS1 key files. Signed-off-by: John Lane GNUtoo@cyberdimension.org: rebase, fixes, commit message Signed-off-by: Denis 'GNUtoo' Carikli --- grub-core/disk/luks.c | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/grub-core/disk/luks.c b/grub-core/disk/luks.c index d0f65700d..376895259 100644 --- a/grub-core/disk/luks.c +++ b/grub-core/disk/luks.c @@ -162,13 +162,14 @@ luks_scan (grub_disk_t disk, const char *check_uuid, int check_boot, static grub_err_t luks_recover_key (grub_disk_t source, grub_cryptodisk_t dev, grub_file_t hdr, - grub_uint8_t *keyfile_bytes __attribute__ ((unused)), - grub_size_t keyfile_bytes_size __attribute__ ((unused))) + grub_uint8_t *keyfile_bytes, grub_size_t keyfile_bytes_size) { struct grub_luks_phdr header; grub_size_t keysize; grub_uint8_t *split_key = NULL; - char passphrase[MAX_PASSPHRASE] = ""; + char interactive_passphrase[MAX_PASSPHRASE] = ""; + grub_uint8_t *passphrase; + grub_size_t passphrase_length; grub_uint8_t candidate_digest[sizeof (header.mkDigest)]; unsigned i; grub_size_t length; @@ -205,18 +206,29 @@ luks_recover_key (grub_disk_t source, grub_cryptodisk_t dev, grub_file_t hdr, if (!split_key) return grub_errno; - /* Get the passphrase from the user. */ - tmp = NULL; - if (source->partition) - tmp = grub_partition_get_name (source->partition); - grub_printf_ (N_("Enter passphrase for %s%s%s (%s): "), source->name, - source->partition ? "," : "", tmp ? : "", - dev->uuid); - grub_free (tmp); - if (!grub_password_get (passphrase, MAX_PASSPHRASE)) + if (keyfile_bytes) { - grub_free (split_key); - return grub_error (GRUB_ERR_BAD_ARGUMENT, "Passphrase not supplied"); + /* Use bytestring from key file as passphrase */ + passphrase = keyfile_bytes; + passphrase_length = keyfile_bytes_size; + } + else + { + /* Get the passphrase from the user. */ + tmp = NULL; + if (source->partition) + tmp = grub_partition_get_name (source->partition); + grub_printf_ (N_("Enter passphrase for %s%s%s (%s): "), source->name, + source->partition ? "," : "", tmp ? : "", dev->uuid); + grub_free (tmp); + if (!grub_password_get (interactive_passphrase, MAX_PASSPHRASE)) + { + grub_free (split_key); + return grub_error (GRUB_ERR_BAD_ARGUMENT, "Passphrase not supplied"); + } + + passphrase = (grub_uint8_t *)interactive_passphrase; + passphrase_length = grub_strlen (interactive_passphrase); } /* Try to recover master key from each active keyslot. */ @@ -234,7 +246,7 @@ luks_recover_key (grub_disk_t source, grub_cryptodisk_t dev, grub_file_t hdr, /* Calculate the PBKDF2 of the user supplied passphrase. */ gcry_err = grub_crypto_pbkdf2 (dev->hash, (grub_uint8_t *) passphrase, - grub_strlen (passphrase), + passphrase_length, header.keyblock[i].passwordSalt, sizeof (header.keyblock[i].passwordSalt), grub_be_to_cpu32 (header.keyblock[i]. -- 2.25.1