2011-04-21 09:14:29 +00:00
|
|
|
/*
|
|
|
|
* GRUB -- GRand Unified Bootloader
|
2019-12-27 15:18:38 +00:00
|
|
|
* Copyright (C) 2003,2007,2010,2011,2019 Free Software Foundation, Inc.
|
2011-04-21 09:14:29 +00:00
|
|
|
*
|
|
|
|
* GRUB is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* GRUB is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2011-04-23 16:00:42 +00:00
|
|
|
#include <grub/cryptodisk.h>
|
2011-04-21 09:14:29 +00:00
|
|
|
#include <grub/types.h>
|
|
|
|
#include <grub/misc.h>
|
|
|
|
#include <grub/mm.h>
|
|
|
|
#include <grub/dl.h>
|
|
|
|
#include <grub/err.h>
|
|
|
|
#include <grub/disk.h>
|
|
|
|
#include <grub/crypto.h>
|
2011-04-25 12:52:07 +00:00
|
|
|
#include <grub/partition.h>
|
2011-04-21 09:14:29 +00:00
|
|
|
#include <grub/i18n.h>
|
|
|
|
|
|
|
|
GRUB_MOD_LICENSE ("GPLv3+");
|
|
|
|
|
|
|
|
#define MAX_PASSPHRASE 256
|
|
|
|
|
|
|
|
#define LUKS_KEY_ENABLED 0x00AC71F3
|
|
|
|
|
|
|
|
/* On disk LUKS header */
|
|
|
|
struct grub_luks_phdr
|
|
|
|
{
|
|
|
|
grub_uint8_t magic[6];
|
|
|
|
#define LUKS_MAGIC "LUKS\xBA\xBE"
|
|
|
|
grub_uint16_t version;
|
|
|
|
char cipherName[32];
|
|
|
|
char cipherMode[32];
|
|
|
|
char hashSpec[32];
|
|
|
|
grub_uint32_t payloadOffset;
|
|
|
|
grub_uint32_t keyBytes;
|
|
|
|
grub_uint8_t mkDigest[20];
|
|
|
|
grub_uint8_t mkDigestSalt[32];
|
|
|
|
grub_uint32_t mkDigestIterations;
|
2011-04-21 09:38:51 +00:00
|
|
|
char uuid[40];
|
2011-04-21 09:14:29 +00:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
grub_uint32_t active;
|
|
|
|
grub_uint32_t passwordIterations;
|
|
|
|
grub_uint8_t passwordSalt[32];
|
|
|
|
grub_uint32_t keyMaterialOffset;
|
|
|
|
grub_uint32_t stripes;
|
|
|
|
} keyblock[8];
|
2013-12-15 13:14:30 +00:00
|
|
|
} GRUB_PACKED;
|
2011-04-21 09:14:29 +00:00
|
|
|
|
|
|
|
typedef struct grub_luks_phdr *grub_luks_phdr_t;
|
|
|
|
|
|
|
|
gcry_err_code_t AF_merge (const gcry_md_spec_t * hash, grub_uint8_t * src,
|
|
|
|
grub_uint8_t * dst, grub_size_t blocksize,
|
|
|
|
grub_size_t blocknumbers);
|
|
|
|
|
2011-04-23 16:00:42 +00:00
|
|
|
static grub_cryptodisk_t
|
2011-04-25 12:52:07 +00:00
|
|
|
configure_ciphers (grub_disk_t disk, const char *check_uuid,
|
|
|
|
int check_boot)
|
2011-04-21 09:14:29 +00:00
|
|
|
{
|
2011-04-23 16:00:42 +00:00
|
|
|
grub_cryptodisk_t newdev;
|
2011-04-22 14:32:27 +00:00
|
|
|
const char *iptr;
|
2011-04-25 12:52:07 +00:00
|
|
|
struct grub_luks_phdr header;
|
2011-04-22 14:32:27 +00:00
|
|
|
char *optr;
|
2011-04-25 12:52:07 +00:00
|
|
|
char uuid[sizeof (header.uuid) + 1];
|
|
|
|
char ciphername[sizeof (header.cipherName) + 1];
|
|
|
|
char ciphermode[sizeof (header.cipherMode) + 1];
|
|
|
|
char hashspec[sizeof (header.hashSpec) + 1];
|
|
|
|
grub_err_t err;
|
|
|
|
|
|
|
|
if (check_boot)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Read the LUKS header. */
|
|
|
|
err = grub_disk_read (disk, 0, 0, sizeof (header), &header);
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
if (err == GRUB_ERR_OUT_OF_RANGE)
|
|
|
|
grub_errno = GRUB_ERR_NONE;
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-04-23 01:18:07 +00:00
|
|
|
|
2011-04-21 09:14:29 +00:00
|
|
|
/* Look for LUKS magic sequence. */
|
2011-04-25 12:52:07 +00:00
|
|
|
if (grub_memcmp (header.magic, LUKS_MAGIC, sizeof (header.magic))
|
|
|
|
|| grub_be_to_cpu16 (header.version) != 1)
|
2011-04-22 14:32:27 +00:00
|
|
|
return NULL;
|
2011-04-21 09:38:51 +00:00
|
|
|
|
2020-09-07 15:27:36 +00:00
|
|
|
grub_memset (uuid, 0, sizeof (uuid));
|
2011-04-21 10:39:14 +00:00
|
|
|
optr = uuid;
|
2011-04-25 12:52:07 +00:00
|
|
|
for (iptr = header.uuid; iptr < &header.uuid[ARRAY_SIZE (header.uuid)];
|
2011-04-21 10:39:14 +00:00
|
|
|
iptr++)
|
|
|
|
{
|
|
|
|
if (*iptr != '-')
|
|
|
|
*optr++ = *iptr;
|
|
|
|
}
|
|
|
|
*optr = 0;
|
|
|
|
|
2011-04-25 12:52:07 +00:00
|
|
|
if (check_uuid && grub_strcasecmp (check_uuid, uuid) != 0)
|
2011-04-21 10:39:14 +00:00
|
|
|
{
|
2011-04-25 12:52:07 +00:00
|
|
|
grub_dprintf ("luks", "%s != %s\n", uuid, check_uuid);
|
2011-04-22 14:32:27 +00:00
|
|
|
return NULL;
|
2011-04-21 10:39:14 +00:00
|
|
|
}
|
2011-04-21 09:14:29 +00:00
|
|
|
|
2011-04-22 14:32:27 +00:00
|
|
|
/* Make sure that strings are null terminated. */
|
2011-04-25 12:52:07 +00:00
|
|
|
grub_memcpy (ciphername, header.cipherName, sizeof (header.cipherName));
|
|
|
|
ciphername[sizeof (header.cipherName)] = 0;
|
|
|
|
grub_memcpy (ciphermode, header.cipherMode, sizeof (header.cipherMode));
|
|
|
|
ciphermode[sizeof (header.cipherMode)] = 0;
|
|
|
|
grub_memcpy (hashspec, header.hashSpec, sizeof (header.hashSpec));
|
|
|
|
hashspec[sizeof (header.hashSpec)] = 0;
|
2011-04-22 14:32:27 +00:00
|
|
|
|
2019-12-27 15:18:38 +00:00
|
|
|
newdev = grub_zalloc (sizeof (struct grub_cryptodisk));
|
|
|
|
if (!newdev)
|
2011-04-23 00:04:40 +00:00
|
|
|
return NULL;
|
2019-12-27 15:18:38 +00:00
|
|
|
newdev->offset = grub_be_to_cpu32 (header.payloadOffset);
|
|
|
|
newdev->source_disk = NULL;
|
|
|
|
newdev->log_sector_size = 9;
|
|
|
|
newdev->total_length = grub_disk_get_size (disk) - newdev->offset;
|
2020-09-07 15:27:36 +00:00
|
|
|
grub_memcpy (newdev->uuid, uuid, sizeof (uuid));
|
2019-12-27 15:18:38 +00:00
|
|
|
newdev->modname = "luks";
|
2011-04-23 00:04:40 +00:00
|
|
|
|
2011-04-21 09:14:29 +00:00
|
|
|
/* Configure the hash used for the AF splitter and HMAC. */
|
2019-12-27 15:18:38 +00:00
|
|
|
newdev->hash = grub_crypto_lookup_md_by_name (hashspec);
|
|
|
|
if (!newdev->hash)
|
2011-04-21 09:14:29 +00:00
|
|
|
{
|
2019-12-27 15:18:38 +00:00
|
|
|
grub_free (newdev);
|
2011-04-22 14:32:27 +00:00
|
|
|
grub_error (GRUB_ERR_FILE_NOT_FOUND, "Couldn't load %s hash",
|
|
|
|
hashspec);
|
|
|
|
return NULL;
|
2011-04-21 09:14:29 +00:00
|
|
|
}
|
|
|
|
|
2019-12-27 15:18:38 +00:00
|
|
|
err = grub_cryptodisk_setcipher (newdev, ciphername, ciphermode);
|
|
|
|
if (err)
|
2014-11-28 18:12:00 +00:00
|
|
|
{
|
2019-12-27 15:18:38 +00:00
|
|
|
grub_free (newdev);
|
2014-11-28 18:12:00 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2019-12-27 15:18:38 +00:00
|
|
|
|
2011-04-24 19:11:14 +00:00
|
|
|
COMPILE_TIME_ASSERT (sizeof (newdev->uuid) >= sizeof (uuid));
|
2011-04-22 14:32:27 +00:00
|
|
|
return newdev;
|
|
|
|
}
|
2011-04-21 09:14:29 +00:00
|
|
|
|
2011-04-22 14:32:27 +00:00
|
|
|
static grub_err_t
|
2011-04-25 12:52:07 +00:00
|
|
|
luks_recover_key (grub_disk_t source,
|
|
|
|
grub_cryptodisk_t dev)
|
2011-04-22 14:32:27 +00:00
|
|
|
{
|
2011-04-25 12:52:07 +00:00
|
|
|
struct grub_luks_phdr header;
|
|
|
|
grub_size_t keysize;
|
2011-04-22 14:32:27 +00:00
|
|
|
grub_uint8_t *split_key = NULL;
|
|
|
|
char passphrase[MAX_PASSPHRASE] = "";
|
2011-04-25 12:52:07 +00:00
|
|
|
grub_uint8_t candidate_digest[sizeof (header.mkDigest)];
|
2011-04-22 14:32:27 +00:00
|
|
|
unsigned i;
|
|
|
|
grub_size_t length;
|
|
|
|
grub_err_t err;
|
2011-04-24 15:50:22 +00:00
|
|
|
grub_size_t max_stripes = 1;
|
2011-04-25 12:52:07 +00:00
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
err = grub_disk_read (source, 0, 0, sizeof (header), &header);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2011-04-22 14:32:27 +00:00
|
|
|
|
2011-11-11 20:44:56 +00:00
|
|
|
grub_puts_ (N_("Attempting to decrypt master key..."));
|
2011-04-25 12:52:07 +00:00
|
|
|
keysize = grub_be_to_cpu32 (header.keyBytes);
|
2013-11-12 01:48:02 +00:00
|
|
|
if (keysize > GRUB_CRYPTODISK_MAX_KEYLEN)
|
|
|
|
return grub_error (GRUB_ERR_BAD_FS, "key is too long");
|
2011-04-21 09:14:29 +00:00
|
|
|
|
2011-04-25 12:52:07 +00:00
|
|
|
for (i = 0; i < ARRAY_SIZE (header.keyblock); i++)
|
|
|
|
if (grub_be_to_cpu32 (header.keyblock[i].active) == LUKS_KEY_ENABLED
|
|
|
|
&& grub_be_to_cpu32 (header.keyblock[i].stripes) > max_stripes)
|
|
|
|
max_stripes = grub_be_to_cpu32 (header.keyblock[i].stripes);
|
2011-04-24 15:50:22 +00:00
|
|
|
|
2020-06-15 16:26:01 +00:00
|
|
|
split_key = grub_calloc (keysize, max_stripes);
|
2011-04-21 09:14:29 +00:00
|
|
|
if (!split_key)
|
2011-04-23 10:40:43 +00:00
|
|
|
return grub_errno;
|
2011-04-21 09:14:29 +00:00
|
|
|
|
|
|
|
/* Get the passphrase from the user. */
|
2011-04-25 12:52:07 +00:00
|
|
|
tmp = NULL;
|
|
|
|
if (source->partition)
|
|
|
|
tmp = grub_partition_get_name (source->partition);
|
2011-11-11 20:44:56 +00:00
|
|
|
grub_printf_ (N_("Enter passphrase for %s%s%s (%s): "), source->name,
|
2011-04-25 12:52:07 +00:00
|
|
|
source->partition ? "," : "", tmp ? : "",
|
|
|
|
dev->uuid);
|
|
|
|
grub_free (tmp);
|
2011-04-21 09:14:29 +00:00
|
|
|
if (!grub_password_get (passphrase, MAX_PASSPHRASE))
|
|
|
|
{
|
|
|
|
grub_free (split_key);
|
|
|
|
return grub_error (GRUB_ERR_BAD_ARGUMENT, "Passphrase not supplied");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try to recover master key from each active keyslot. */
|
2011-04-25 12:52:07 +00:00
|
|
|
for (i = 0; i < ARRAY_SIZE (header.keyblock); i++)
|
2011-04-21 09:14:29 +00:00
|
|
|
{
|
|
|
|
gcry_err_code_t gcry_err;
|
2013-11-12 01:48:02 +00:00
|
|
|
grub_uint8_t candidate_key[GRUB_CRYPTODISK_MAX_KEYLEN];
|
|
|
|
grub_uint8_t digest[GRUB_CRYPTODISK_MAX_KEYLEN];
|
2011-04-21 09:14:29 +00:00
|
|
|
|
|
|
|
/* Check if keyslot is enabled. */
|
2011-04-25 12:52:07 +00:00
|
|
|
if (grub_be_to_cpu32 (header.keyblock[i].active) != LUKS_KEY_ENABLED)
|
2011-04-21 09:14:29 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
grub_dprintf ("luks", "Trying keyslot %d\n", i);
|
|
|
|
|
|
|
|
/* Calculate the PBKDF2 of the user supplied passphrase. */
|
2011-04-22 14:32:27 +00:00
|
|
|
gcry_err = grub_crypto_pbkdf2 (dev->hash, (grub_uint8_t *) passphrase,
|
2011-04-21 09:14:29 +00:00
|
|
|
grub_strlen (passphrase),
|
2011-04-25 12:52:07 +00:00
|
|
|
header.keyblock[i].passwordSalt,
|
|
|
|
sizeof (header.keyblock[i].passwordSalt),
|
|
|
|
grub_be_to_cpu32 (header.keyblock[i].
|
2011-04-21 09:14:29 +00:00
|
|
|
passwordIterations),
|
|
|
|
digest, keysize);
|
|
|
|
|
|
|
|
if (gcry_err)
|
|
|
|
{
|
|
|
|
grub_free (split_key);
|
|
|
|
return grub_crypto_gcry_error (gcry_err);
|
|
|
|
}
|
|
|
|
|
|
|
|
grub_dprintf ("luks", "PBKDF2 done\n");
|
|
|
|
|
2011-04-23 16:00:42 +00:00
|
|
|
gcry_err = grub_cryptodisk_setkey (dev, digest, keysize);
|
2011-04-21 09:14:29 +00:00
|
|
|
if (gcry_err)
|
|
|
|
{
|
|
|
|
grub_free (split_key);
|
|
|
|
return grub_crypto_gcry_error (gcry_err);
|
|
|
|
}
|
|
|
|
|
2011-04-25 12:52:07 +00:00
|
|
|
length = (keysize * grub_be_to_cpu32 (header.keyblock[i].stripes));
|
2011-04-21 09:14:29 +00:00
|
|
|
|
|
|
|
/* Read and decrypt the key material from the disk. */
|
|
|
|
err = grub_disk_read (source,
|
2011-04-25 12:52:07 +00:00
|
|
|
grub_be_to_cpu32 (header.keyblock
|
2011-04-21 09:14:29 +00:00
|
|
|
[i].keyMaterialOffset), 0,
|
|
|
|
length, split_key);
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
grub_free (split_key);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2011-04-23 16:00:42 +00:00
|
|
|
gcry_err = grub_cryptodisk_decrypt (dev, split_key, length, 0);
|
2011-04-21 09:14:29 +00:00
|
|
|
if (gcry_err)
|
|
|
|
{
|
|
|
|
grub_free (split_key);
|
|
|
|
return grub_crypto_gcry_error (gcry_err);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Merge the decrypted key material to get the candidate master key. */
|
2011-04-22 14:32:27 +00:00
|
|
|
gcry_err = AF_merge (dev->hash, split_key, candidate_key, keysize,
|
2011-04-25 12:52:07 +00:00
|
|
|
grub_be_to_cpu32 (header.keyblock[i].stripes));
|
2011-04-21 09:14:29 +00:00
|
|
|
if (gcry_err)
|
|
|
|
{
|
|
|
|
grub_free (split_key);
|
|
|
|
return grub_crypto_gcry_error (gcry_err);
|
|
|
|
}
|
|
|
|
|
|
|
|
grub_dprintf ("luks", "candidate key recovered\n");
|
|
|
|
|
|
|
|
/* Calculate the PBKDF2 of the candidate master key. */
|
2011-04-22 14:32:27 +00:00
|
|
|
gcry_err = grub_crypto_pbkdf2 (dev->hash, candidate_key,
|
2011-04-25 12:52:07 +00:00
|
|
|
grub_be_to_cpu32 (header.keyBytes),
|
|
|
|
header.mkDigestSalt,
|
|
|
|
sizeof (header.mkDigestSalt),
|
2011-04-21 09:14:29 +00:00
|
|
|
grub_be_to_cpu32
|
2011-04-25 12:52:07 +00:00
|
|
|
(header.mkDigestIterations),
|
2011-04-21 09:14:29 +00:00
|
|
|
candidate_digest,
|
|
|
|
sizeof (candidate_digest));
|
|
|
|
if (gcry_err)
|
|
|
|
{
|
|
|
|
grub_free (split_key);
|
|
|
|
return grub_crypto_gcry_error (gcry_err);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compare the calculated PBKDF2 to the digest stored
|
|
|
|
in the header to see if it's correct. */
|
2011-04-25 12:52:07 +00:00
|
|
|
if (grub_memcmp (candidate_digest, header.mkDigest,
|
|
|
|
sizeof (header.mkDigest)) != 0)
|
2011-04-22 19:48:32 +00:00
|
|
|
{
|
|
|
|
grub_dprintf ("luks", "bad digest\n");
|
|
|
|
continue;
|
|
|
|
}
|
2011-04-21 09:14:29 +00:00
|
|
|
|
2012-03-03 11:59:28 +00:00
|
|
|
/* TRANSLATORS: It's a cryptographic key slot: one element of an array
|
|
|
|
where each element is either empty or holds a key. */
|
2011-11-11 20:44:56 +00:00
|
|
|
grub_printf_ (N_("Slot %d opened\n"), i);
|
2011-04-21 09:14:29 +00:00
|
|
|
|
|
|
|
/* Set the master key. */
|
2011-04-23 16:00:42 +00:00
|
|
|
gcry_err = grub_cryptodisk_setkey (dev, candidate_key, keysize);
|
2011-04-21 09:14:29 +00:00
|
|
|
if (gcry_err)
|
|
|
|
{
|
|
|
|
grub_free (split_key);
|
|
|
|
return grub_crypto_gcry_error (gcry_err);
|
|
|
|
}
|
|
|
|
|
|
|
|
grub_free (split_key);
|
|
|
|
|
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|
|
|
|
|
2014-11-28 18:12:00 +00:00
|
|
|
grub_free (split_key);
|
2011-04-21 09:14:29 +00:00
|
|
|
return GRUB_ACCESS_DENIED;
|
|
|
|
}
|
|
|
|
|
2011-04-25 12:52:07 +00:00
|
|
|
struct grub_cryptodisk_dev luks_crypto = {
|
|
|
|
.scan = configure_ciphers,
|
|
|
|
.recover_key = luks_recover_key
|
|
|
|
};
|
2011-04-21 09:14:29 +00:00
|
|
|
|
|
|
|
GRUB_MOD_INIT (luks)
|
|
|
|
{
|
2011-04-23 16:00:42 +00:00
|
|
|
COMPILE_TIME_ASSERT (sizeof (((struct grub_luks_phdr *) 0)->uuid)
|
|
|
|
< GRUB_CRYPTODISK_MAX_UUID_LENGTH);
|
2011-04-25 12:52:07 +00:00
|
|
|
grub_cryptodisk_dev_register (&luks_crypto);
|
2011-04-21 09:14:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GRUB_MOD_FINI (luks)
|
|
|
|
{
|
2011-04-25 12:52:07 +00:00
|
|
|
grub_cryptodisk_dev_unregister (&luks_crypto);
|
2011-04-21 09:14:29 +00:00
|
|
|
}
|