2011-04-23 22:00:29 +00:00
|
|
|
/*
|
|
|
|
* GRUB -- GRand Unified Bootloader
|
|
|
|
* Copyright (C) 2003,2007,2010,2011 Free Software Foundation, Inc.
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* This file is loosely based on FreeBSD geli implementation
|
|
|
|
(but no code was directly copied). FreeBSD geli is distributed under
|
|
|
|
following terms: */
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2005-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <grub/cryptodisk.h>
|
|
|
|
#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-23 22:00:29 +00:00
|
|
|
#include <grub/i18n.h>
|
|
|
|
|
|
|
|
GRUB_MOD_LICENSE ("GPLv3+");
|
|
|
|
|
2011-11-08 18:34:19 +00:00
|
|
|
/* Dirty trick to solve circular dependency. */
|
|
|
|
#ifdef GRUB_UTIL
|
|
|
|
|
|
|
|
#include <grub/util/misc.h>
|
|
|
|
|
|
|
|
#undef GRUB_MD_SHA256
|
|
|
|
#undef GRUB_MD_SHA512
|
|
|
|
|
|
|
|
static const gcry_md_spec_t *
|
|
|
|
grub_md_sha256_real (void)
|
|
|
|
{
|
|
|
|
const gcry_md_spec_t *ret;
|
|
|
|
ret = grub_crypto_lookup_md_by_name ("sha256");
|
|
|
|
if (!ret)
|
2012-03-03 12:09:14 +00:00
|
|
|
grub_util_error ("%s", _("Couldn't load sha256"));
|
2011-11-08 18:34:19 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const gcry_md_spec_t *
|
|
|
|
grub_md_sha512_real (void)
|
|
|
|
{
|
|
|
|
const gcry_md_spec_t *ret;
|
|
|
|
ret = grub_crypto_lookup_md_by_name ("sha512");
|
|
|
|
if (!ret)
|
2012-03-03 12:09:14 +00:00
|
|
|
grub_util_error ("%s", _("Couldn't load sha512"));
|
2011-11-08 18:34:19 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define GRUB_MD_SHA256 grub_md_sha256_real()
|
|
|
|
#define GRUB_MD_SHA512 grub_md_sha512_real()
|
|
|
|
#endif
|
|
|
|
|
2011-04-23 22:00:29 +00:00
|
|
|
struct grub_geli_key
|
|
|
|
{
|
|
|
|
grub_uint8_t iv_key[64];
|
|
|
|
grub_uint8_t cipher_key[64];
|
|
|
|
grub_uint8_t hmac[64];
|
2013-12-15 13:14:30 +00:00
|
|
|
} GRUB_PACKED;
|
2011-04-23 22:00:29 +00:00
|
|
|
|
|
|
|
struct grub_geli_phdr
|
|
|
|
{
|
|
|
|
grub_uint8_t magic[16];
|
|
|
|
#define GELI_MAGIC "GEOM::ELI"
|
|
|
|
grub_uint32_t version;
|
2011-04-24 19:40:13 +00:00
|
|
|
grub_uint32_t flags;
|
2011-04-23 22:00:29 +00:00
|
|
|
grub_uint16_t alg;
|
|
|
|
grub_uint16_t keylen;
|
2011-04-24 12:59:38 +00:00
|
|
|
grub_uint16_t unused3[5];
|
|
|
|
grub_uint32_t sector_size;
|
2011-04-23 22:00:29 +00:00
|
|
|
grub_uint8_t keys_used;
|
|
|
|
grub_uint32_t niter;
|
|
|
|
grub_uint8_t salt[64];
|
|
|
|
struct grub_geli_key keys[2];
|
2013-12-15 13:14:30 +00:00
|
|
|
} GRUB_PACKED;
|
2011-04-23 22:00:29 +00:00
|
|
|
|
2011-04-24 19:40:13 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
GRUB_GELI_FLAGS_ONETIME = 1,
|
|
|
|
GRUB_GELI_FLAGS_BOOT = 2,
|
|
|
|
};
|
|
|
|
|
2011-04-24 19:41:49 +00:00
|
|
|
/* FIXME: support version 0. */
|
2011-04-24 15:18:18 +00:00
|
|
|
/* FIXME: support big-endian pre-version-4 volumes. */
|
|
|
|
/* FIXME: support for keyfiles. */
|
|
|
|
/* FIXME: support for HMAC. */
|
2011-04-23 22:00:29 +00:00
|
|
|
const char *algorithms[] = {
|
2011-04-24 00:37:23 +00:00
|
|
|
[0x01] = "des",
|
|
|
|
[0x02] = "3des",
|
|
|
|
[0x03] = "blowfish",
|
|
|
|
[0x04] = "cast5",
|
2011-04-24 15:18:18 +00:00
|
|
|
/* FIXME: 0x05 is skipjack, but we don't have it. */
|
2011-04-23 22:00:29 +00:00
|
|
|
[0x0b] = "aes",
|
2011-04-24 15:18:18 +00:00
|
|
|
/* FIXME: 0x10 is null. */
|
2011-04-24 00:37:23 +00:00
|
|
|
[0x15] = "camellia128",
|
2011-04-24 15:41:50 +00:00
|
|
|
[0x16] = "aes"
|
2011-04-23 22:00:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define MAX_PASSPHRASE 256
|
|
|
|
|
2011-04-24 15:15:55 +00:00
|
|
|
static gcry_err_code_t
|
|
|
|
geli_rekey (struct grub_cryptodisk *dev, grub_uint64_t zoneno)
|
|
|
|
{
|
|
|
|
gcry_err_code_t gcry_err;
|
|
|
|
const struct {
|
|
|
|
char magic[4];
|
|
|
|
grub_uint64_t zone;
|
2013-12-15 13:14:30 +00:00
|
|
|
} GRUB_PACKED tohash
|
2011-04-24 15:15:55 +00:00
|
|
|
= { {'e', 'k', 'e', 'y'}, grub_cpu_to_le64 (zoneno) };
|
2013-11-12 01:48:02 +00:00
|
|
|
GRUB_PROPERLY_ALIGNED_ARRAY (key, GRUB_CRYPTO_MAX_MDLEN);
|
|
|
|
|
|
|
|
if (dev->hash->mdlen > GRUB_CRYPTO_MAX_MDLEN)
|
|
|
|
return GPG_ERR_INV_ARG;
|
2011-04-24 15:15:55 +00:00
|
|
|
|
|
|
|
grub_dprintf ("geli", "rekeying %" PRIuGRUB_UINT64_T " keysize=%d\n",
|
|
|
|
zoneno, dev->rekey_derived_size);
|
|
|
|
gcry_err = grub_crypto_hmac_buffer (dev->hash, dev->rekey_key, 64,
|
|
|
|
&tohash, sizeof (tohash), key);
|
|
|
|
if (gcry_err)
|
2013-11-06 23:57:21 +00:00
|
|
|
return gcry_err;
|
2011-04-24 15:15:55 +00:00
|
|
|
|
|
|
|
return grub_cryptodisk_setkey (dev, (grub_uint8_t *) key,
|
|
|
|
dev->rekey_derived_size);
|
|
|
|
}
|
|
|
|
|
2011-04-25 12:52:07 +00:00
|
|
|
static inline gcry_err_code_t
|
|
|
|
make_uuid (const struct grub_geli_phdr *header,
|
|
|
|
char *uuid)
|
2011-04-23 22:00:29 +00:00
|
|
|
{
|
2013-11-12 01:48:02 +00:00
|
|
|
grub_uint8_t uuidbin[GRUB_CRYPTODISK_MAX_UUID_LENGTH];
|
2011-04-25 12:52:07 +00:00
|
|
|
gcry_err_code_t err;
|
2011-04-24 19:11:14 +00:00
|
|
|
grub_uint8_t *iptr;
|
|
|
|
char *optr;
|
2011-04-25 12:52:07 +00:00
|
|
|
|
2013-11-12 01:48:02 +00:00
|
|
|
if (2 * GRUB_MD_SHA256->mdlen + 1 > GRUB_CRYPTODISK_MAX_UUID_LENGTH)
|
|
|
|
return GPG_ERR_TOO_LARGE;
|
2011-04-25 12:52:07 +00:00
|
|
|
err = grub_crypto_hmac_buffer (GRUB_MD_SHA256,
|
|
|
|
header->salt, sizeof (header->salt),
|
|
|
|
"uuid", sizeof ("uuid") - 1, uuidbin);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
optr = uuid;
|
2013-11-12 01:48:02 +00:00
|
|
|
for (iptr = uuidbin; iptr < &uuidbin[GRUB_MD_SHA256->mdlen]; iptr++)
|
2011-04-25 12:52:07 +00:00
|
|
|
{
|
|
|
|
grub_snprintf (optr, 3, "%02x", *iptr);
|
|
|
|
optr += 2;
|
|
|
|
}
|
|
|
|
*optr = 0;
|
|
|
|
return GPG_ERR_NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef GRUB_UTIL
|
|
|
|
|
|
|
|
#include <grub/emu/hostdisk.h>
|
|
|
|
#include <grub/emu/misc.h>
|
|
|
|
|
|
|
|
char *
|
|
|
|
grub_util_get_geli_uuid (const char *dev)
|
|
|
|
{
|
2013-10-15 15:55:20 +00:00
|
|
|
grub_util_fd_t fd;
|
2011-04-25 12:52:07 +00:00
|
|
|
grub_uint64_t s;
|
|
|
|
unsigned log_secsize;
|
|
|
|
grub_uint8_t hdr[512];
|
|
|
|
struct grub_geli_phdr *header;
|
|
|
|
char *uuid;
|
|
|
|
gcry_err_code_t err;
|
|
|
|
|
2013-10-15 15:55:20 +00:00
|
|
|
fd = grub_util_fd_open (dev, GRUB_UTIL_FD_O_RDONLY);
|
|
|
|
|
2013-09-23 09:21:09 +00:00
|
|
|
if (!GRUB_UTIL_FD_IS_VALID (fd))
|
2011-04-25 12:52:07 +00:00
|
|
|
return NULL;
|
|
|
|
|
2012-02-29 14:23:31 +00:00
|
|
|
s = grub_util_get_fd_size (fd, dev, &log_secsize);
|
|
|
|
s >>= log_secsize;
|
2013-10-14 10:47:09 +00:00
|
|
|
grub_util_fd_seek (fd, (s << log_secsize) - 512);
|
2011-04-25 12:52:07 +00:00
|
|
|
|
|
|
|
uuid = xmalloc (GRUB_MD_SHA256->mdlen * 2 + 1);
|
|
|
|
if (grub_util_fd_read (fd, (void *) &hdr, 512) < 0)
|
2012-02-10 13:01:53 +00:00
|
|
|
grub_util_error ("%s", _("couldn't read ELI metadata"));
|
2013-10-15 15:55:20 +00:00
|
|
|
|
|
|
|
grub_util_fd_close (fd);
|
2011-04-25 12:52:07 +00:00
|
|
|
|
|
|
|
COMPILE_TIME_ASSERT (sizeof (header) <= 512);
|
|
|
|
header = (void *) &hdr;
|
2011-04-23 22:00:29 +00:00
|
|
|
|
|
|
|
/* Look for GELI magic sequence. */
|
|
|
|
if (grub_memcmp (header->magic, GELI_MAGIC, sizeof (GELI_MAGIC))
|
2011-04-24 15:15:55 +00:00
|
|
|
|| grub_le_to_cpu32 (header->version) > 5
|
|
|
|
|| grub_le_to_cpu32 (header->version) < 1)
|
2012-02-10 13:01:53 +00:00
|
|
|
grub_util_error ("%s", _("wrong ELI magic or version"));
|
2011-04-25 12:52:07 +00:00
|
|
|
|
|
|
|
err = make_uuid ((void *) &hdr, uuid);
|
|
|
|
if (err)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return uuid;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static grub_cryptodisk_t
|
|
|
|
configure_ciphers (grub_disk_t disk, const char *check_uuid,
|
|
|
|
int boot_only)
|
|
|
|
{
|
|
|
|
grub_cryptodisk_t newdev;
|
|
|
|
struct grub_geli_phdr header;
|
|
|
|
grub_crypto_cipher_handle_t cipher = NULL, secondary_cipher = NULL;
|
|
|
|
const struct gcry_cipher_spec *ciph;
|
|
|
|
const char *ciphername = NULL;
|
|
|
|
gcry_err_code_t gcry_err;
|
2013-11-12 01:48:02 +00:00
|
|
|
char uuid[GRUB_CRYPTODISK_MAX_UUID_LENGTH];
|
2011-04-25 12:52:07 +00:00
|
|
|
grub_disk_addr_t sector;
|
|
|
|
grub_err_t err;
|
|
|
|
|
2013-11-12 01:48:02 +00:00
|
|
|
if (2 * GRUB_MD_SHA256->mdlen + 1 > GRUB_CRYPTODISK_MAX_UUID_LENGTH)
|
|
|
|
return NULL;
|
|
|
|
|
2011-04-25 12:52:07 +00:00
|
|
|
sector = grub_disk_get_size (disk);
|
|
|
|
if (sector == GRUB_DISK_SIZE_UNKNOWN || sector == 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Read the GELI header. */
|
|
|
|
err = grub_disk_read (disk, sector - 1, 0, sizeof (header), &header);
|
|
|
|
if (err)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Look for GELI magic sequence. */
|
|
|
|
if (grub_memcmp (header.magic, GELI_MAGIC, sizeof (GELI_MAGIC))
|
|
|
|
|| grub_le_to_cpu32 (header.version) > 5
|
|
|
|
|| grub_le_to_cpu32 (header.version) < 1)
|
2011-04-23 22:00:29 +00:00
|
|
|
{
|
2011-04-25 12:52:07 +00:00
|
|
|
grub_dprintf ("geli", "wrong magic %02x\n", header.magic[0]);
|
2011-04-23 22:00:29 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2011-04-25 12:52:07 +00:00
|
|
|
|
|
|
|
if ((grub_le_to_cpu32 (header.sector_size)
|
|
|
|
& (grub_le_to_cpu32 (header.sector_size) - 1))
|
|
|
|
|| grub_le_to_cpu32 (header.sector_size) == 0)
|
2011-04-24 12:59:38 +00:00
|
|
|
{
|
|
|
|
grub_dprintf ("geli", "incorrect sector size %d\n",
|
2011-04-25 12:52:07 +00:00
|
|
|
grub_le_to_cpu32 (header.sector_size));
|
2011-04-24 12:59:38 +00:00
|
|
|
return NULL;
|
2011-04-24 19:40:13 +00:00
|
|
|
}
|
|
|
|
|
2011-04-25 12:52:07 +00:00
|
|
|
if (grub_le_to_cpu32 (header.flags) & GRUB_GELI_FLAGS_ONETIME)
|
2011-04-24 19:40:13 +00:00
|
|
|
{
|
|
|
|
grub_dprintf ("geli", "skipping one-time volume\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-04-25 12:52:07 +00:00
|
|
|
if (boot_only && !(grub_le_to_cpu32 (header.flags) & GRUB_GELI_FLAGS_BOOT))
|
2011-04-24 19:40:13 +00:00
|
|
|
{
|
|
|
|
grub_dprintf ("geli", "not a boot volume\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-04-23 22:00:29 +00:00
|
|
|
|
2011-04-25 12:52:07 +00:00
|
|
|
gcry_err = make_uuid (&header, uuid);
|
2011-04-24 19:11:14 +00:00
|
|
|
if (gcry_err)
|
|
|
|
{
|
|
|
|
grub_crypto_gcry_error (gcry_err);
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-04-23 22:00:29 +00:00
|
|
|
|
2011-04-25 12:52:07 +00:00
|
|
|
if (check_uuid && grub_strcasecmp (check_uuid, uuid) != 0)
|
2011-04-23 22:00:29 +00:00
|
|
|
{
|
2011-04-25 12:52:07 +00:00
|
|
|
grub_dprintf ("geli", "%s != %s\n", uuid, check_uuid);
|
2011-04-23 22:00:29 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-04-25 12:52:07 +00:00
|
|
|
if (grub_le_to_cpu16 (header.alg) >= ARRAY_SIZE (algorithms)
|
|
|
|
|| algorithms[grub_le_to_cpu16 (header.alg)] == NULL)
|
2011-04-23 22:00:29 +00:00
|
|
|
{
|
|
|
|
grub_error (GRUB_ERR_FILE_NOT_FOUND, "Cipher 0x%x unknown",
|
2011-04-25 12:52:07 +00:00
|
|
|
grub_le_to_cpu16 (header.alg));
|
2011-04-23 22:00:29 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-04-25 12:52:07 +00:00
|
|
|
ciphername = algorithms[grub_le_to_cpu16 (header.alg)];
|
2011-04-23 22:00:29 +00:00
|
|
|
ciph = grub_crypto_lookup_cipher_by_name (ciphername);
|
|
|
|
if (!ciph)
|
|
|
|
{
|
|
|
|
grub_error (GRUB_ERR_FILE_NOT_FOUND, "Cipher %s isn't available",
|
|
|
|
ciphername);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Configure the cipher used for the bulk data. */
|
|
|
|
cipher = grub_crypto_cipher_open (ciph);
|
|
|
|
if (!cipher)
|
|
|
|
return NULL;
|
|
|
|
|
2011-04-25 12:52:07 +00:00
|
|
|
if (grub_le_to_cpu16 (header.alg) == 0x16)
|
2011-04-24 15:41:50 +00:00
|
|
|
{
|
|
|
|
secondary_cipher = grub_crypto_cipher_open (ciph);
|
|
|
|
if (!secondary_cipher)
|
2014-11-28 19:23:30 +00:00
|
|
|
{
|
|
|
|
grub_crypto_cipher_close (cipher);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-04-24 15:41:50 +00:00
|
|
|
}
|
|
|
|
|
2011-04-25 12:52:07 +00:00
|
|
|
if (grub_le_to_cpu16 (header.keylen) > 1024)
|
2011-04-23 22:00:29 +00:00
|
|
|
{
|
|
|
|
grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid keysize %d",
|
2011-04-25 12:52:07 +00:00
|
|
|
grub_le_to_cpu16 (header.keylen));
|
2014-11-28 19:23:30 +00:00
|
|
|
grub_crypto_cipher_close (cipher);
|
|
|
|
grub_crypto_cipher_close (secondary_cipher);
|
2011-04-23 22:00:29 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
newdev = grub_zalloc (sizeof (struct grub_cryptodisk));
|
|
|
|
if (!newdev)
|
2014-11-28 19:23:30 +00:00
|
|
|
{
|
|
|
|
grub_crypto_cipher_close (cipher);
|
|
|
|
grub_crypto_cipher_close (secondary_cipher);
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-04-23 22:00:29 +00:00
|
|
|
newdev->cipher = cipher;
|
2011-04-24 15:41:50 +00:00
|
|
|
newdev->secondary_cipher = secondary_cipher;
|
2011-04-23 22:00:29 +00:00
|
|
|
newdev->offset = 0;
|
|
|
|
newdev->source_disk = NULL;
|
|
|
|
newdev->benbi_log = 0;
|
2011-04-25 12:52:07 +00:00
|
|
|
if (grub_le_to_cpu16 (header.alg) == 0x16)
|
2011-04-24 15:41:50 +00:00
|
|
|
{
|
|
|
|
newdev->mode = GRUB_CRYPTODISK_MODE_XTS;
|
|
|
|
newdev->mode_iv = GRUB_CRYPTODISK_MODE_IV_BYTECOUNT64;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newdev->mode = GRUB_CRYPTODISK_MODE_CBC;
|
|
|
|
newdev->mode_iv = GRUB_CRYPTODISK_MODE_IV_BYTECOUNT64_HASH;
|
|
|
|
}
|
2011-04-23 22:00:29 +00:00
|
|
|
newdev->essiv_cipher = NULL;
|
|
|
|
newdev->essiv_hash = NULL;
|
2011-04-24 18:37:56 +00:00
|
|
|
newdev->hash = GRUB_MD_SHA512;
|
|
|
|
newdev->iv_hash = GRUB_MD_SHA256;
|
2011-04-24 12:59:38 +00:00
|
|
|
|
|
|
|
for (newdev->log_sector_size = 0;
|
2011-04-25 12:52:07 +00:00
|
|
|
(1U << newdev->log_sector_size) < grub_le_to_cpu32 (header.sector_size);
|
2011-04-24 12:59:38 +00:00
|
|
|
newdev->log_sector_size++);
|
2011-04-24 15:15:55 +00:00
|
|
|
|
2011-04-25 12:52:07 +00:00
|
|
|
if (grub_le_to_cpu32 (header.version) >= 5)
|
2011-04-24 15:15:55 +00:00
|
|
|
{
|
|
|
|
newdev->rekey = geli_rekey;
|
|
|
|
newdev->rekey_shift = 20;
|
|
|
|
}
|
2011-04-24 19:11:14 +00:00
|
|
|
|
2011-04-25 12:52:07 +00:00
|
|
|
newdev->modname = "geli";
|
|
|
|
|
|
|
|
newdev->total_length = grub_disk_get_size (disk) - 1;
|
2011-04-23 22:00:29 +00:00
|
|
|
grub_memcpy (newdev->uuid, uuid, sizeof (newdev->uuid));
|
2011-04-24 19:11:14 +00:00
|
|
|
COMPILE_TIME_ASSERT (sizeof (newdev->uuid) >= 32 * 2 + 1);
|
2011-04-23 22:00:29 +00:00
|
|
|
return newdev;
|
|
|
|
}
|
|
|
|
|
|
|
|
static grub_err_t
|
2011-04-25 12:52:07 +00:00
|
|
|
recover_key (grub_disk_t source, grub_cryptodisk_t dev)
|
2011-04-23 22:00:29 +00:00
|
|
|
{
|
2011-04-25 12:52:07 +00:00
|
|
|
grub_size_t keysize;
|
2013-11-12 01:48:02 +00:00
|
|
|
grub_uint8_t digest[GRUB_CRYPTO_MAX_MDLEN];
|
|
|
|
grub_uint8_t geomkey[GRUB_CRYPTO_MAX_MDLEN];
|
|
|
|
grub_uint8_t verify_key[GRUB_CRYPTO_MAX_MDLEN];
|
|
|
|
grub_uint8_t zero[GRUB_CRYPTO_MAX_CIPHER_BLOCKSIZE];
|
2011-04-23 22:00:29 +00:00
|
|
|
char passphrase[MAX_PASSPHRASE] = "";
|
|
|
|
unsigned i;
|
|
|
|
gcry_err_code_t gcry_err;
|
2011-04-25 12:52:07 +00:00
|
|
|
struct grub_geli_phdr header;
|
|
|
|
char *tmp;
|
|
|
|
grub_disk_addr_t sector;
|
|
|
|
grub_err_t err;
|
|
|
|
|
2013-11-12 01:48:02 +00:00
|
|
|
if (dev->cipher->cipher->blocksize > GRUB_CRYPTO_MAX_CIPHER_BLOCKSIZE)
|
|
|
|
return grub_error (GRUB_ERR_BUG, "cipher block is too long");
|
|
|
|
|
|
|
|
if (dev->hash->mdlen > GRUB_CRYPTO_MAX_MDLEN)
|
|
|
|
return grub_error (GRUB_ERR_BUG, "mdlen is too long");
|
|
|
|
|
2011-04-25 12:52:07 +00:00
|
|
|
sector = grub_disk_get_size (source);
|
|
|
|
if (sector == GRUB_DISK_SIZE_UNKNOWN || sector == 0)
|
2012-02-08 18:26:01 +00:00
|
|
|
return grub_error (GRUB_ERR_BUG, "not a geli");
|
2011-04-23 22:00:29 +00:00
|
|
|
|
2011-04-25 12:52:07 +00:00
|
|
|
/* Read the GELI header. */
|
|
|
|
err = grub_disk_read (source, sector - 1, 0, sizeof (header), &header);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2013-04-05 08:59:26 +00:00
|
|
|
keysize = grub_le_to_cpu16 (header.keylen) / GRUB_CHAR_BIT;
|
2011-04-23 22:00:29 +00:00
|
|
|
grub_memset (zero, 0, sizeof (zero));
|
|
|
|
|
2011-11-11 20:44:56 +00:00
|
|
|
grub_puts_ (N_("Attempting to decrypt master key..."));
|
2011-04-23 22:00: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,
|
|
|
|
source->partition ? "," : "", tmp ? : "",
|
|
|
|
dev->uuid);
|
2011-04-25 12:52:07 +00:00
|
|
|
grub_free (tmp);
|
2011-04-23 22:00:29 +00:00
|
|
|
if (!grub_password_get (passphrase, MAX_PASSPHRASE))
|
|
|
|
return grub_error (GRUB_ERR_BAD_ARGUMENT, "Passphrase not supplied");
|
|
|
|
|
2011-04-24 00:36:04 +00:00
|
|
|
/* Calculate the PBKDF2 of the user supplied passphrase. */
|
2011-04-25 12:52:07 +00:00
|
|
|
if (grub_le_to_cpu32 (header.niter) != 0)
|
2011-04-24 00:36:04 +00:00
|
|
|
{
|
|
|
|
grub_uint8_t pbkdf_key[64];
|
|
|
|
gcry_err = grub_crypto_pbkdf2 (dev->hash, (grub_uint8_t *) passphrase,
|
|
|
|
grub_strlen (passphrase),
|
2011-04-25 12:52:07 +00:00
|
|
|
header.salt,
|
|
|
|
sizeof (header.salt),
|
|
|
|
grub_le_to_cpu32 (header.niter),
|
2011-04-24 00:36:04 +00:00
|
|
|
pbkdf_key, sizeof (pbkdf_key));
|
2011-04-23 22:00:29 +00:00
|
|
|
|
2011-04-24 00:36:04 +00:00
|
|
|
if (gcry_err)
|
|
|
|
return grub_crypto_gcry_error (gcry_err);
|
2011-04-23 22:00:29 +00:00
|
|
|
|
2011-04-24 00:36:04 +00:00
|
|
|
gcry_err = grub_crypto_hmac_buffer (dev->hash, NULL, 0, pbkdf_key,
|
|
|
|
sizeof (pbkdf_key), geomkey);
|
|
|
|
if (gcry_err)
|
|
|
|
return grub_crypto_gcry_error (gcry_err);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct grub_crypto_hmac_handle *hnd;
|
|
|
|
|
|
|
|
hnd = grub_crypto_hmac_init (dev->hash, NULL, 0);
|
|
|
|
if (!hnd)
|
|
|
|
return grub_crypto_gcry_error (GPG_ERR_OUT_OF_MEMORY);
|
|
|
|
|
2011-04-25 12:52:07 +00:00
|
|
|
grub_crypto_hmac_write (hnd, header.salt, sizeof (header.salt));
|
2011-04-24 00:36:04 +00:00
|
|
|
grub_crypto_hmac_write (hnd, passphrase, grub_strlen (passphrase));
|
|
|
|
|
|
|
|
gcry_err = grub_crypto_hmac_fini (hnd, geomkey);
|
|
|
|
if (gcry_err)
|
|
|
|
return grub_crypto_gcry_error (gcry_err);
|
|
|
|
}
|
2011-04-23 22:00:29 +00:00
|
|
|
|
|
|
|
gcry_err = grub_crypto_hmac_buffer (dev->hash, geomkey,
|
2013-11-12 01:48:02 +00:00
|
|
|
dev->hash->mdlen, "\1", 1, digest);
|
2011-04-23 22:00:29 +00:00
|
|
|
if (gcry_err)
|
|
|
|
return grub_crypto_gcry_error (gcry_err);
|
|
|
|
|
|
|
|
gcry_err = grub_crypto_hmac_buffer (dev->hash, geomkey,
|
2013-11-12 01:48:02 +00:00
|
|
|
dev->hash->mdlen, "\0", 1, verify_key);
|
2011-04-23 22:00:29 +00:00
|
|
|
if (gcry_err)
|
|
|
|
return grub_crypto_gcry_error (gcry_err);
|
|
|
|
|
|
|
|
grub_dprintf ("geli", "keylen = %" PRIuGRUB_SIZE "\n", keysize);
|
|
|
|
|
|
|
|
/* Try to recover master key from each active keyslot. */
|
2011-04-25 12:52:07 +00:00
|
|
|
for (i = 0; i < ARRAY_SIZE (header.keys); i++)
|
2011-04-23 22:00:29 +00:00
|
|
|
{
|
|
|
|
struct grub_geli_key candidate_key;
|
2013-11-12 01:48:02 +00:00
|
|
|
grub_uint8_t key_hmac[GRUB_CRYPTO_MAX_MDLEN];
|
2011-04-23 22:00:29 +00:00
|
|
|
|
|
|
|
/* Check if keyslot is enabled. */
|
2011-04-25 12:52:07 +00:00
|
|
|
if (! (header.keys_used & (1 << i)))
|
2011-04-23 22:00:29 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
grub_dprintf ("geli", "Trying keyslot %d\n", i);
|
|
|
|
|
|
|
|
gcry_err = grub_crypto_cipher_set_key (dev->cipher,
|
|
|
|
digest, keysize);
|
|
|
|
if (gcry_err)
|
|
|
|
return grub_crypto_gcry_error (gcry_err);
|
|
|
|
|
|
|
|
gcry_err = grub_crypto_cbc_decrypt (dev->cipher, &candidate_key,
|
2011-04-25 12:52:07 +00:00
|
|
|
&header.keys[i],
|
2011-04-23 22:00:29 +00:00
|
|
|
sizeof (candidate_key),
|
|
|
|
zero);
|
|
|
|
if (gcry_err)
|
|
|
|
return grub_crypto_gcry_error (gcry_err);
|
|
|
|
|
|
|
|
gcry_err = grub_crypto_hmac_buffer (dev->hash, verify_key,
|
2013-11-12 01:48:02 +00:00
|
|
|
dev->hash->mdlen,
|
2011-04-23 22:00:29 +00:00
|
|
|
&candidate_key,
|
|
|
|
(sizeof (candidate_key)
|
|
|
|
- sizeof (candidate_key.hmac)),
|
|
|
|
key_hmac);
|
|
|
|
if (gcry_err)
|
|
|
|
return grub_crypto_gcry_error (gcry_err);
|
|
|
|
|
|
|
|
if (grub_memcmp (candidate_key.hmac, key_hmac, dev->hash->mdlen) != 0)
|
|
|
|
continue;
|
2011-11-11 20:44:56 +00:00
|
|
|
grub_printf_ (N_("Slot %d opened\n"), i);
|
2011-04-23 22:00:29 +00:00
|
|
|
|
|
|
|
/* Set the master key. */
|
2011-04-24 15:15:55 +00:00
|
|
|
if (!dev->rekey)
|
|
|
|
{
|
2011-04-24 15:41:50 +00:00
|
|
|
grub_size_t real_keysize = keysize;
|
2011-04-25 12:52:07 +00:00
|
|
|
if (grub_le_to_cpu16 (header.alg) == 0x16)
|
2011-04-24 15:41:50 +00:00
|
|
|
real_keysize *= 2;
|
2011-04-24 15:15:55 +00:00
|
|
|
gcry_err = grub_cryptodisk_setkey (dev, candidate_key.cipher_key,
|
2011-04-24 15:41:50 +00:00
|
|
|
real_keysize);
|
2011-04-24 15:15:55 +00:00
|
|
|
if (gcry_err)
|
|
|
|
return grub_crypto_gcry_error (gcry_err);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-04-24 15:41:50 +00:00
|
|
|
grub_size_t real_keysize = keysize;
|
2011-04-25 12:52:07 +00:00
|
|
|
if (grub_le_to_cpu16 (header.alg) == 0x16)
|
2011-04-24 15:41:50 +00:00
|
|
|
real_keysize *= 2;
|
2011-04-24 15:15:55 +00:00
|
|
|
/* For a reason I don't know, the IV key is used in rekeying. */
|
|
|
|
grub_memcpy (dev->rekey_key, candidate_key.iv_key,
|
|
|
|
sizeof (candidate_key.iv_key));
|
2011-04-24 15:41:50 +00:00
|
|
|
dev->rekey_derived_size = real_keysize;
|
2011-04-24 15:15:55 +00:00
|
|
|
dev->last_rekey = -1;
|
|
|
|
COMPILE_TIME_ASSERT (sizeof (dev->rekey_key)
|
|
|
|
>= sizeof (candidate_key.iv_key));
|
|
|
|
}
|
2011-04-23 22:00:29 +00:00
|
|
|
|
|
|
|
dev->iv_prefix_len = sizeof (candidate_key.iv_key);
|
|
|
|
grub_memcpy (dev->iv_prefix, candidate_key.iv_key,
|
|
|
|
sizeof (candidate_key.iv_key));
|
|
|
|
|
|
|
|
COMPILE_TIME_ASSERT (sizeof (dev->iv_prefix) >= sizeof (candidate_key.iv_key));
|
|
|
|
|
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GRUB_ACCESS_DENIED;
|
|
|
|
}
|
|
|
|
|
2011-04-25 12:52:07 +00:00
|
|
|
struct grub_cryptodisk_dev geli_crypto = {
|
|
|
|
.scan = configure_ciphers,
|
|
|
|
.recover_key = recover_key
|
|
|
|
};
|
2011-04-23 22:00:29 +00:00
|
|
|
|
|
|
|
GRUB_MOD_INIT (geli)
|
|
|
|
{
|
2011-04-25 12:52:07 +00:00
|
|
|
grub_cryptodisk_dev_register (&geli_crypto);
|
2011-04-23 22:00:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GRUB_MOD_FINI (geli)
|
|
|
|
{
|
2011-04-25 12:52:07 +00:00
|
|
|
grub_cryptodisk_dev_unregister (&geli_crypto);
|
2011-04-23 22:00:29 +00:00
|
|
|
}
|