2016-03-16 23:12:19 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2015 Allan Jude <allanjude@FreeBSD.org>
|
|
|
|
* Copyright (c) 2005-2011 Pawel Jakub Dawidek <pawel@dawidek.net>
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
2018-07-13 17:50:25 +00:00
|
|
|
#include <stand.h>
|
|
|
|
#include <stdarg.h>
|
2016-03-16 23:12:19 +00:00
|
|
|
#include "geliboot.h"
|
2018-07-13 17:50:25 +00:00
|
|
|
#include "geliboot_internal.h"
|
2016-03-16 23:12:19 +00:00
|
|
|
|
2018-07-13 17:50:25 +00:00
|
|
|
struct known_dev {
|
|
|
|
char name[GELIDEV_NAMELEN];
|
|
|
|
struct geli_dev *gdev;
|
|
|
|
SLIST_ENTRY(known_dev) entries;
|
|
|
|
};
|
2016-03-16 23:12:19 +00:00
|
|
|
|
2018-07-13 17:50:25 +00:00
|
|
|
SLIST_HEAD(known_dev_list, known_dev) known_devs_head =
|
|
|
|
SLIST_HEAD_INITIALIZER(known_devs_head);
|
Implement boot-time encryption key passing (keybuf)
This patch adds a general mechanism for providing encryption keys to the
kernel from the boot loader. This is intended to enable GELI support at
boot time, providing a better mechanism for passing keys to the kernel
than environment variables. It is designed to be extensible to other
applications, and can easily handle multiple encrypted volumes with
different keys.
This mechanism is currently used by the pending GELI EFI work.
Additionally, this mechanism can potentially be used to interface with
GRUB, opening up options for coreboot+GRUB configurations with completely
encrypted disks.
Another benefit over the existing system is that it does not require
re-deriving the user key from the password at each boot stage.
Most of this patch was written by Eric McCorkle. It was extended by
Allan Jude with a number of minor enhancements and extending the keybuf
feature into boot2.
GELI user keys are now derived once, in boot2, then passed to the loader,
which reuses the key, then passes it to the kernel, where the GELI module
destroys the keybuf after decrypting the volumes.
Submitted by: Eric McCorkle <eric@metricspace.net> (Original Version)
Reviewed by: oshogbo (earlier version), cem (earlier version)
MFC after: 3 weeks
Relnotes: yes
Sponsored by: ScaleEngine Inc.
Differential Revision: https://reviews.freebsd.org/D9575
2017-04-01 05:05:22 +00:00
|
|
|
|
|
|
|
static geli_ukey saved_keys[GELI_MAX_KEYS];
|
|
|
|
static unsigned int nsaved_keys = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy keys from local storage to the keybuf struct.
|
|
|
|
* Destroy the local storage when finished.
|
|
|
|
*/
|
|
|
|
void
|
2018-07-13 17:50:25 +00:00
|
|
|
geli_export_key_buffer(struct keybuf *fkeybuf)
|
Implement boot-time encryption key passing (keybuf)
This patch adds a general mechanism for providing encryption keys to the
kernel from the boot loader. This is intended to enable GELI support at
boot time, providing a better mechanism for passing keys to the kernel
than environment variables. It is designed to be extensible to other
applications, and can easily handle multiple encrypted volumes with
different keys.
This mechanism is currently used by the pending GELI EFI work.
Additionally, this mechanism can potentially be used to interface with
GRUB, opening up options for coreboot+GRUB configurations with completely
encrypted disks.
Another benefit over the existing system is that it does not require
re-deriving the user key from the password at each boot stage.
Most of this patch was written by Eric McCorkle. It was extended by
Allan Jude with a number of minor enhancements and extending the keybuf
feature into boot2.
GELI user keys are now derived once, in boot2, then passed to the loader,
which reuses the key, then passes it to the kernel, where the GELI module
destroys the keybuf after decrypting the volumes.
Submitted by: Eric McCorkle <eric@metricspace.net> (Original Version)
Reviewed by: oshogbo (earlier version), cem (earlier version)
MFC after: 3 weeks
Relnotes: yes
Sponsored by: ScaleEngine Inc.
Differential Revision: https://reviews.freebsd.org/D9575
2017-04-01 05:05:22 +00:00
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < nsaved_keys; i++) {
|
|
|
|
fkeybuf->kb_ents[i].ke_type = KEYBUF_TYPE_GELI;
|
|
|
|
memcpy(fkeybuf->kb_ents[i].ke_data, saved_keys[i],
|
|
|
|
G_ELI_USERKEYLEN);
|
|
|
|
}
|
|
|
|
fkeybuf->kb_nents = nsaved_keys;
|
|
|
|
explicit_bzero(saved_keys, sizeof(saved_keys));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy keys from a keybuf struct into local storage.
|
|
|
|
* Zero out the keybuf.
|
|
|
|
*/
|
|
|
|
void
|
2018-07-13 17:50:25 +00:00
|
|
|
geli_import_key_buffer(struct keybuf *skeybuf)
|
Implement boot-time encryption key passing (keybuf)
This patch adds a general mechanism for providing encryption keys to the
kernel from the boot loader. This is intended to enable GELI support at
boot time, providing a better mechanism for passing keys to the kernel
than environment variables. It is designed to be extensible to other
applications, and can easily handle multiple encrypted volumes with
different keys.
This mechanism is currently used by the pending GELI EFI work.
Additionally, this mechanism can potentially be used to interface with
GRUB, opening up options for coreboot+GRUB configurations with completely
encrypted disks.
Another benefit over the existing system is that it does not require
re-deriving the user key from the password at each boot stage.
Most of this patch was written by Eric McCorkle. It was extended by
Allan Jude with a number of minor enhancements and extending the keybuf
feature into boot2.
GELI user keys are now derived once, in boot2, then passed to the loader,
which reuses the key, then passes it to the kernel, where the GELI module
destroys the keybuf after decrypting the volumes.
Submitted by: Eric McCorkle <eric@metricspace.net> (Original Version)
Reviewed by: oshogbo (earlier version), cem (earlier version)
MFC after: 3 weeks
Relnotes: yes
Sponsored by: ScaleEngine Inc.
Differential Revision: https://reviews.freebsd.org/D9575
2017-04-01 05:05:22 +00:00
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < skeybuf->kb_nents && i < GELI_MAX_KEYS; i++) {
|
|
|
|
memcpy(saved_keys[i], skeybuf->kb_ents[i].ke_data,
|
|
|
|
G_ELI_USERKEYLEN);
|
|
|
|
explicit_bzero(skeybuf->kb_ents[i].ke_data,
|
|
|
|
G_ELI_USERKEYLEN);
|
|
|
|
skeybuf->kb_ents[i].ke_type = KEYBUF_TYPE_NONE;
|
|
|
|
}
|
|
|
|
nsaved_keys = skeybuf->kb_nents;
|
|
|
|
skeybuf->kb_nents = 0;
|
|
|
|
}
|
|
|
|
|
2018-07-13 17:50:25 +00:00
|
|
|
void
|
|
|
|
geli_add_key(geli_ukey key)
|
Implement boot-time encryption key passing (keybuf)
This patch adds a general mechanism for providing encryption keys to the
kernel from the boot loader. This is intended to enable GELI support at
boot time, providing a better mechanism for passing keys to the kernel
than environment variables. It is designed to be extensible to other
applications, and can easily handle multiple encrypted volumes with
different keys.
This mechanism is currently used by the pending GELI EFI work.
Additionally, this mechanism can potentially be used to interface with
GRUB, opening up options for coreboot+GRUB configurations with completely
encrypted disks.
Another benefit over the existing system is that it does not require
re-deriving the user key from the password at each boot stage.
Most of this patch was written by Eric McCorkle. It was extended by
Allan Jude with a number of minor enhancements and extending the keybuf
feature into boot2.
GELI user keys are now derived once, in boot2, then passed to the loader,
which reuses the key, then passes it to the kernel, where the GELI module
destroys the keybuf after decrypting the volumes.
Submitted by: Eric McCorkle <eric@metricspace.net> (Original Version)
Reviewed by: oshogbo (earlier version), cem (earlier version)
MFC after: 3 weeks
Relnotes: yes
Sponsored by: ScaleEngine Inc.
Differential Revision: https://reviews.freebsd.org/D9575
2017-04-01 05:05:22 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we run out of key space, the worst that will happen is
|
|
|
|
* it will ask the user for the password again.
|
|
|
|
*/
|
|
|
|
if (nsaved_keys < GELI_MAX_KEYS) {
|
|
|
|
memcpy(saved_keys[nsaved_keys], key, G_ELI_USERKEYLEN);
|
|
|
|
nsaved_keys++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-16 23:12:19 +00:00
|
|
|
static int
|
2018-07-13 17:50:25 +00:00
|
|
|
geli_findkey(struct geli_dev *gdev, u_char *mkey)
|
Implement boot-time encryption key passing (keybuf)
This patch adds a general mechanism for providing encryption keys to the
kernel from the boot loader. This is intended to enable GELI support at
boot time, providing a better mechanism for passing keys to the kernel
than environment variables. It is designed to be extensible to other
applications, and can easily handle multiple encrypted volumes with
different keys.
This mechanism is currently used by the pending GELI EFI work.
Additionally, this mechanism can potentially be used to interface with
GRUB, opening up options for coreboot+GRUB configurations with completely
encrypted disks.
Another benefit over the existing system is that it does not require
re-deriving the user key from the password at each boot stage.
Most of this patch was written by Eric McCorkle. It was extended by
Allan Jude with a number of minor enhancements and extending the keybuf
feature into boot2.
GELI user keys are now derived once, in boot2, then passed to the loader,
which reuses the key, then passes it to the kernel, where the GELI module
destroys the keybuf after decrypting the volumes.
Submitted by: Eric McCorkle <eric@metricspace.net> (Original Version)
Reviewed by: oshogbo (earlier version), cem (earlier version)
MFC after: 3 weeks
Relnotes: yes
Sponsored by: ScaleEngine Inc.
Differential Revision: https://reviews.freebsd.org/D9575
2017-04-01 05:05:22 +00:00
|
|
|
{
|
|
|
|
u_int keynum;
|
|
|
|
int i;
|
|
|
|
|
2018-07-13 17:50:25 +00:00
|
|
|
if (gdev->keybuf_slot >= 0) {
|
|
|
|
if (g_eli_mkey_decrypt_any(&gdev->md, saved_keys[gdev->keybuf_slot],
|
Implement boot-time encryption key passing (keybuf)
This patch adds a general mechanism for providing encryption keys to the
kernel from the boot loader. This is intended to enable GELI support at
boot time, providing a better mechanism for passing keys to the kernel
than environment variables. It is designed to be extensible to other
applications, and can easily handle multiple encrypted volumes with
different keys.
This mechanism is currently used by the pending GELI EFI work.
Additionally, this mechanism can potentially be used to interface with
GRUB, opening up options for coreboot+GRUB configurations with completely
encrypted disks.
Another benefit over the existing system is that it does not require
re-deriving the user key from the password at each boot stage.
Most of this patch was written by Eric McCorkle. It was extended by
Allan Jude with a number of minor enhancements and extending the keybuf
feature into boot2.
GELI user keys are now derived once, in boot2, then passed to the loader,
which reuses the key, then passes it to the kernel, where the GELI module
destroys the keybuf after decrypting the volumes.
Submitted by: Eric McCorkle <eric@metricspace.net> (Original Version)
Reviewed by: oshogbo (earlier version), cem (earlier version)
MFC after: 3 weeks
Relnotes: yes
Sponsored by: ScaleEngine Inc.
Differential Revision: https://reviews.freebsd.org/D9575
2017-04-01 05:05:22 +00:00
|
|
|
mkey, &keynum) == 0) {
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < nsaved_keys; i++) {
|
2018-07-13 17:50:25 +00:00
|
|
|
if (g_eli_mkey_decrypt_any(&gdev->md, saved_keys[i], mkey,
|
Implement boot-time encryption key passing (keybuf)
This patch adds a general mechanism for providing encryption keys to the
kernel from the boot loader. This is intended to enable GELI support at
boot time, providing a better mechanism for passing keys to the kernel
than environment variables. It is designed to be extensible to other
applications, and can easily handle multiple encrypted volumes with
different keys.
This mechanism is currently used by the pending GELI EFI work.
Additionally, this mechanism can potentially be used to interface with
GRUB, opening up options for coreboot+GRUB configurations with completely
encrypted disks.
Another benefit over the existing system is that it does not require
re-deriving the user key from the password at each boot stage.
Most of this patch was written by Eric McCorkle. It was extended by
Allan Jude with a number of minor enhancements and extending the keybuf
feature into boot2.
GELI user keys are now derived once, in boot2, then passed to the loader,
which reuses the key, then passes it to the kernel, where the GELI module
destroys the keybuf after decrypting the volumes.
Submitted by: Eric McCorkle <eric@metricspace.net> (Original Version)
Reviewed by: oshogbo (earlier version), cem (earlier version)
MFC after: 3 weeks
Relnotes: yes
Sponsored by: ScaleEngine Inc.
Differential Revision: https://reviews.freebsd.org/D9575
2017-04-01 05:05:22 +00:00
|
|
|
&keynum) == 0) {
|
2018-07-13 17:50:25 +00:00
|
|
|
gdev->keybuf_slot = i;
|
Implement boot-time encryption key passing (keybuf)
This patch adds a general mechanism for providing encryption keys to the
kernel from the boot loader. This is intended to enable GELI support at
boot time, providing a better mechanism for passing keys to the kernel
than environment variables. It is designed to be extensible to other
applications, and can easily handle multiple encrypted volumes with
different keys.
This mechanism is currently used by the pending GELI EFI work.
Additionally, this mechanism can potentially be used to interface with
GRUB, opening up options for coreboot+GRUB configurations with completely
encrypted disks.
Another benefit over the existing system is that it does not require
re-deriving the user key from the password at each boot stage.
Most of this patch was written by Eric McCorkle. It was extended by
Allan Jude with a number of minor enhancements and extending the keybuf
feature into boot2.
GELI user keys are now derived once, in boot2, then passed to the loader,
which reuses the key, then passes it to the kernel, where the GELI module
destroys the keybuf after decrypting the volumes.
Submitted by: Eric McCorkle <eric@metricspace.net> (Original Version)
Reviewed by: oshogbo (earlier version), cem (earlier version)
MFC after: 3 weeks
Relnotes: yes
Sponsored by: ScaleEngine Inc.
Differential Revision: https://reviews.freebsd.org/D9575
2017-04-01 05:05:22 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
2016-03-16 23:12:19 +00:00
|
|
|
/*
|
2018-07-13 17:50:25 +00:00
|
|
|
* Read the last sector of a drive or partition and see if it is GELI encrypted.
|
2016-03-16 23:12:19 +00:00
|
|
|
*/
|
2018-07-13 17:50:25 +00:00
|
|
|
struct geli_dev *
|
|
|
|
geli_taste(geli_readfunc readfunc, void *readpriv, daddr_t lastsector,
|
|
|
|
const char *namefmt, ...)
|
2016-03-16 23:12:19 +00:00
|
|
|
{
|
2018-07-13 17:50:25 +00:00
|
|
|
va_list args;
|
2016-03-16 23:12:19 +00:00
|
|
|
struct g_eli_metadata md;
|
2018-07-13 17:50:25 +00:00
|
|
|
struct known_dev *kdev;
|
|
|
|
struct geli_dev *gdev;
|
|
|
|
u_char *buf;
|
|
|
|
char devname[GELIDEV_NAMELEN];
|
2016-03-16 23:12:19 +00:00
|
|
|
int error;
|
2016-04-06 23:21:44 +00:00
|
|
|
off_t alignsector;
|
2016-03-16 23:12:19 +00:00
|
|
|
|
2018-07-13 17:50:25 +00:00
|
|
|
/*
|
|
|
|
* Format the name into a temp buffer and use that to search for an
|
|
|
|
* existing known_dev instance. If not found, this has the side effect
|
|
|
|
* of initializing kdev to NULL.
|
|
|
|
*/
|
|
|
|
va_start(args, namefmt);
|
|
|
|
vsnprintf(devname, sizeof(devname), namefmt, args);
|
|
|
|
va_end(args);
|
|
|
|
SLIST_FOREACH(kdev, &known_devs_head, entries) {
|
|
|
|
if (strcmp(kdev->name, devname) == 0)
|
|
|
|
return (kdev->gdev);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Determine whether the new device is geli-encrypted... */
|
|
|
|
if ((buf = malloc(DEV_GELIBOOT_BSIZE)) == NULL)
|
|
|
|
goto out;
|
2016-10-04 16:33:03 +00:00
|
|
|
alignsector = rounddown2(lastsector * DEV_BSIZE, DEV_GELIBOOT_BSIZE);
|
|
|
|
if (alignsector + DEV_GELIBOOT_BSIZE > ((lastsector + 1) * DEV_BSIZE)) {
|
|
|
|
/* Don't read past the end of the disk */
|
2018-07-13 17:50:25 +00:00
|
|
|
alignsector = (lastsector * DEV_BSIZE) + DEV_BSIZE -
|
|
|
|
DEV_GELIBOOT_BSIZE;
|
2016-10-04 16:33:03 +00:00
|
|
|
}
|
2018-07-13 17:50:25 +00:00
|
|
|
error = readfunc(NULL, readpriv, alignsector, buf, DEV_GELIBOOT_BSIZE);
|
2016-03-16 23:12:19 +00:00
|
|
|
if (error != 0) {
|
2018-07-13 17:50:25 +00:00
|
|
|
goto out;
|
2016-03-16 23:12:19 +00:00
|
|
|
}
|
2018-07-13 17:50:25 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We have a new known_device. Whether it's geli-encrypted or not,
|
|
|
|
* record its existance so we can avoid doing IO to probe it next time.
|
|
|
|
*/
|
|
|
|
if ((kdev = malloc(sizeof(*kdev))) == NULL)
|
|
|
|
goto out;
|
|
|
|
strlcpy(kdev->name, devname, sizeof(kdev->name));
|
|
|
|
kdev->gdev = NULL;
|
|
|
|
SLIST_INSERT_HEAD(&known_devs_head, kdev, entries);
|
|
|
|
|
2016-10-04 16:33:03 +00:00
|
|
|
/* Extract the last 4k sector of the disk. */
|
|
|
|
error = eli_metadata_decode(buf, &md);
|
2016-03-16 23:12:19 +00:00
|
|
|
if (error != 0) {
|
2016-10-04 16:33:03 +00:00
|
|
|
/* Try the last 512 byte sector instead. */
|
|
|
|
error = eli_metadata_decode(buf +
|
|
|
|
(DEV_GELIBOOT_BSIZE - DEV_BSIZE), &md);
|
|
|
|
if (error != 0) {
|
2018-07-13 17:50:25 +00:00
|
|
|
goto out;
|
2016-10-04 16:33:03 +00:00
|
|
|
}
|
2016-03-16 23:12:19 +00:00
|
|
|
}
|
|
|
|
|
2016-04-08 01:27:40 +00:00
|
|
|
if (!(md.md_flags & G_ELI_FLAG_GELIBOOT)) {
|
|
|
|
/* The GELIBOOT feature is not activated */
|
2018-07-13 17:50:25 +00:00
|
|
|
goto out;
|
2016-03-16 23:12:19 +00:00
|
|
|
}
|
2016-04-08 01:27:40 +00:00
|
|
|
if ((md.md_flags & G_ELI_FLAG_ONETIME)) {
|
|
|
|
/* Swap device, skip it. */
|
2018-07-13 17:50:25 +00:00
|
|
|
goto out;
|
2016-03-16 23:12:19 +00:00
|
|
|
}
|
|
|
|
|
2018-07-13 17:50:25 +00:00
|
|
|
/*
|
|
|
|
* It's geli-encrypted, create a geli_dev for it and link it into the
|
|
|
|
* known_dev instance.
|
|
|
|
*/
|
|
|
|
gdev = malloc(sizeof(struct geli_dev));
|
|
|
|
if (gdev == NULL)
|
|
|
|
goto out;
|
|
|
|
gdev->part_end = lastsector;
|
|
|
|
gdev->keybuf_slot = -1;
|
|
|
|
gdev->md = md;
|
|
|
|
gdev->name = kdev->name;
|
|
|
|
eli_metadata_softc(&gdev->sc, &md, DEV_BSIZE,
|
2016-03-16 23:12:19 +00:00
|
|
|
(lastsector + DEV_BSIZE) * DEV_BSIZE);
|
2018-07-13 17:50:25 +00:00
|
|
|
kdev->gdev = gdev;
|
|
|
|
out:
|
|
|
|
free(buf);
|
|
|
|
if (kdev == NULL)
|
|
|
|
return (NULL);
|
|
|
|
return (kdev->gdev);
|
2016-03-16 23:12:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2018-07-13 17:50:25 +00:00
|
|
|
* Attempt to decrypt the device. This will try existing keys first, then will
|
|
|
|
* prompt for a passphrase if there are no existing keys that work.
|
2016-03-16 23:12:19 +00:00
|
|
|
*/
|
2017-08-26 14:07:24 +00:00
|
|
|
static int
|
2018-07-13 17:50:25 +00:00
|
|
|
geli_probe(struct geli_dev *gdev, const char *passphrase, u_char *mkeyp)
|
2016-03-16 23:12:19 +00:00
|
|
|
{
|
|
|
|
u_char key[G_ELI_USERKEYLEN], mkey[G_ELI_DATAIVKEYLEN], *mkp;
|
|
|
|
u_int keynum;
|
|
|
|
struct hmac_ctx ctx;
|
|
|
|
int error;
|
|
|
|
|
Implement boot-time encryption key passing (keybuf)
This patch adds a general mechanism for providing encryption keys to the
kernel from the boot loader. This is intended to enable GELI support at
boot time, providing a better mechanism for passing keys to the kernel
than environment variables. It is designed to be extensible to other
applications, and can easily handle multiple encrypted volumes with
different keys.
This mechanism is currently used by the pending GELI EFI work.
Additionally, this mechanism can potentially be used to interface with
GRUB, opening up options for coreboot+GRUB configurations with completely
encrypted disks.
Another benefit over the existing system is that it does not require
re-deriving the user key from the password at each boot stage.
Most of this patch was written by Eric McCorkle. It was extended by
Allan Jude with a number of minor enhancements and extending the keybuf
feature into boot2.
GELI user keys are now derived once, in boot2, then passed to the loader,
which reuses the key, then passes it to the kernel, where the GELI module
destroys the keybuf after decrypting the volumes.
Submitted by: Eric McCorkle <eric@metricspace.net> (Original Version)
Reviewed by: oshogbo (earlier version), cem (earlier version)
MFC after: 3 weeks
Relnotes: yes
Sponsored by: ScaleEngine Inc.
Differential Revision: https://reviews.freebsd.org/D9575
2017-04-01 05:05:22 +00:00
|
|
|
if (mkeyp != NULL) {
|
|
|
|
memcpy(&mkey, mkeyp, G_ELI_DATAIVKEYLEN);
|
|
|
|
explicit_bzero(mkeyp, G_ELI_DATAIVKEYLEN);
|
2018-07-13 17:50:25 +00:00
|
|
|
goto found_key;
|
Implement boot-time encryption key passing (keybuf)
This patch adds a general mechanism for providing encryption keys to the
kernel from the boot loader. This is intended to enable GELI support at
boot time, providing a better mechanism for passing keys to the kernel
than environment variables. It is designed to be extensible to other
applications, and can easily handle multiple encrypted volumes with
different keys.
This mechanism is currently used by the pending GELI EFI work.
Additionally, this mechanism can potentially be used to interface with
GRUB, opening up options for coreboot+GRUB configurations with completely
encrypted disks.
Another benefit over the existing system is that it does not require
re-deriving the user key from the password at each boot stage.
Most of this patch was written by Eric McCorkle. It was extended by
Allan Jude with a number of minor enhancements and extending the keybuf
feature into boot2.
GELI user keys are now derived once, in boot2, then passed to the loader,
which reuses the key, then passes it to the kernel, where the GELI module
destroys the keybuf after decrypting the volumes.
Submitted by: Eric McCorkle <eric@metricspace.net> (Original Version)
Reviewed by: oshogbo (earlier version), cem (earlier version)
MFC after: 3 weeks
Relnotes: yes
Sponsored by: ScaleEngine Inc.
Differential Revision: https://reviews.freebsd.org/D9575
2017-04-01 05:05:22 +00:00
|
|
|
}
|
|
|
|
|
2018-07-13 17:50:25 +00:00
|
|
|
if (geli_findkey(gdev, mkey) == 0) {
|
2017-08-26 14:07:24 +00:00
|
|
|
goto found_key;
|
|
|
|
}
|
2016-03-16 23:12:19 +00:00
|
|
|
|
2017-08-26 14:07:24 +00:00
|
|
|
g_eli_crypto_hmac_init(&ctx, NULL, 0);
|
|
|
|
/*
|
|
|
|
* Prepare Derived-Key from the user passphrase.
|
|
|
|
*/
|
2018-07-13 17:50:25 +00:00
|
|
|
if (gdev->md.md_iterations < 0) {
|
2017-08-26 14:07:24 +00:00
|
|
|
/* XXX TODO: Support loading key files. */
|
|
|
|
return (1);
|
2018-07-13 17:50:25 +00:00
|
|
|
} else if (gdev->md.md_iterations == 0) {
|
|
|
|
g_eli_crypto_hmac_update(&ctx, gdev->md.md_salt,
|
|
|
|
sizeof(gdev->md.md_salt));
|
2017-12-02 00:07:37 +00:00
|
|
|
g_eli_crypto_hmac_update(&ctx, (const uint8_t *)passphrase,
|
2017-08-26 14:07:24 +00:00
|
|
|
strlen(passphrase));
|
2018-07-13 17:50:25 +00:00
|
|
|
} else if (gdev->md.md_iterations > 0) {
|
|
|
|
printf("Calculating GELI Decryption Key for %s %d"
|
|
|
|
" iterations...\n", gdev->name, gdev->md.md_iterations);
|
2017-08-26 14:07:24 +00:00
|
|
|
u_char dkey[G_ELI_USERKEYLEN];
|
|
|
|
|
2018-07-13 17:50:25 +00:00
|
|
|
pkcs5v2_genkey(dkey, sizeof(dkey), gdev->md.md_salt,
|
|
|
|
sizeof(gdev->md.md_salt), passphrase,
|
|
|
|
gdev->md.md_iterations);
|
2017-08-26 14:07:24 +00:00
|
|
|
g_eli_crypto_hmac_update(&ctx, dkey, sizeof(dkey));
|
|
|
|
explicit_bzero(dkey, sizeof(dkey));
|
|
|
|
}
|
2016-03-16 23:12:19 +00:00
|
|
|
|
2017-08-26 14:07:24 +00:00
|
|
|
g_eli_crypto_hmac_final(&ctx, key, 0);
|
2016-03-16 23:12:19 +00:00
|
|
|
|
2018-07-13 17:50:25 +00:00
|
|
|
error = g_eli_mkey_decrypt_any(&gdev->md, key, mkey, &keynum);
|
2017-08-26 14:07:24 +00:00
|
|
|
if (error == -1) {
|
|
|
|
explicit_bzero(mkey, sizeof(mkey));
|
|
|
|
explicit_bzero(key, sizeof(key));
|
|
|
|
printf("Bad GELI key: bad password?\n");
|
|
|
|
return (error);
|
|
|
|
} else if (error != 0) {
|
2017-03-31 00:04:32 +00:00
|
|
|
explicit_bzero(mkey, sizeof(mkey));
|
2017-08-26 14:07:24 +00:00
|
|
|
explicit_bzero(key, sizeof(key));
|
|
|
|
printf("Failed to decrypt GELI master key: %d\n", error);
|
|
|
|
return (error);
|
|
|
|
} else {
|
|
|
|
/* Add key to keychain */
|
2018-07-13 17:50:25 +00:00
|
|
|
geli_add_key(key);
|
2017-08-26 14:07:24 +00:00
|
|
|
explicit_bzero(&key, sizeof(key));
|
|
|
|
}
|
2016-03-16 23:12:19 +00:00
|
|
|
|
2017-08-26 14:07:24 +00:00
|
|
|
found_key:
|
|
|
|
/* Store the keys */
|
2018-07-13 17:50:25 +00:00
|
|
|
bcopy(mkey, gdev->sc.sc_mkey, sizeof(gdev->sc.sc_mkey));
|
|
|
|
bcopy(mkey, gdev->sc.sc_ivkey, sizeof(gdev->sc.sc_ivkey));
|
|
|
|
mkp = mkey + sizeof(gdev->sc.sc_ivkey);
|
|
|
|
if ((gdev->sc.sc_flags & G_ELI_FLAG_AUTH) == 0) {
|
|
|
|
bcopy(mkp, gdev->sc.sc_ekey, G_ELI_DATAKEYLEN);
|
2017-08-26 14:07:24 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* The encryption key is: ekey = HMAC_SHA512(Data-Key, 0x10)
|
|
|
|
*/
|
2017-12-02 00:07:37 +00:00
|
|
|
g_eli_crypto_hmac(mkp, G_ELI_MAXKEYLEN, (const uint8_t *)"\x10", 1,
|
2018-07-13 17:50:25 +00:00
|
|
|
gdev->sc.sc_ekey, 0);
|
2017-08-26 14:07:24 +00:00
|
|
|
}
|
|
|
|
explicit_bzero(mkey, sizeof(mkey));
|
2016-03-16 23:12:19 +00:00
|
|
|
|
2017-08-26 14:07:24 +00:00
|
|
|
/* Initialize the per-sector IV. */
|
2018-07-13 17:50:25 +00:00
|
|
|
switch (gdev->sc.sc_ealgo) {
|
2017-08-26 14:07:24 +00:00
|
|
|
case CRYPTO_AES_XTS:
|
|
|
|
break;
|
|
|
|
default:
|
2018-07-13 17:50:25 +00:00
|
|
|
SHA256_Init(&gdev->sc.sc_ivctx);
|
|
|
|
SHA256_Update(&gdev->sc.sc_ivctx, gdev->sc.sc_ivkey,
|
|
|
|
sizeof(gdev->sc.sc_ivkey));
|
2017-08-26 14:07:24 +00:00
|
|
|
break;
|
2016-03-16 23:12:19 +00:00
|
|
|
}
|
|
|
|
|
2017-08-26 14:07:24 +00:00
|
|
|
return (0);
|
2016-03-16 23:12:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2020-07-11 06:51:42 +00:00
|
|
|
geli_io(struct geli_dev *gdev, geli_op_t enc, off_t offset, u_char *buf,
|
|
|
|
size_t bytes)
|
2016-03-16 23:12:19 +00:00
|
|
|
{
|
|
|
|
u_char iv[G_ELI_IVKEYLEN];
|
|
|
|
u_char *pbuf;
|
|
|
|
int error;
|
2016-04-06 23:21:44 +00:00
|
|
|
off_t dstoff;
|
2016-03-16 23:12:19 +00:00
|
|
|
uint64_t keyno;
|
2016-04-06 23:21:44 +00:00
|
|
|
size_t n, nsec, secsize;
|
2016-03-16 23:12:19 +00:00
|
|
|
struct g_eli_key gkey;
|
|
|
|
|
2016-04-06 23:21:44 +00:00
|
|
|
pbuf = buf;
|
2016-03-16 23:12:19 +00:00
|
|
|
|
2018-07-13 17:50:25 +00:00
|
|
|
secsize = gdev->sc.sc_sectorsize;
|
|
|
|
nsec = bytes / secsize;
|
|
|
|
if (nsec == 0) {
|
|
|
|
/*
|
|
|
|
* A read of less than the GELI sector size has been
|
|
|
|
* requested. The caller provided destination buffer may
|
|
|
|
* not be big enough to boost the read to a full sector,
|
|
|
|
* so just attempt to decrypt the truncated sector.
|
|
|
|
*/
|
|
|
|
secsize = bytes;
|
|
|
|
nsec = 1;
|
|
|
|
}
|
2016-04-06 23:21:44 +00:00
|
|
|
|
2018-07-13 17:50:25 +00:00
|
|
|
for (n = 0, dstoff = offset; n < nsec; n++, dstoff += secsize) {
|
2016-03-16 23:12:19 +00:00
|
|
|
|
2018-07-13 17:50:25 +00:00
|
|
|
g_eli_crypto_ivgen(&gdev->sc, dstoff, iv, G_ELI_IVKEYLEN);
|
2016-03-16 23:12:19 +00:00
|
|
|
|
2018-07-13 17:50:25 +00:00
|
|
|
/* Get the key that corresponds to this offset. */
|
|
|
|
keyno = (dstoff >> G_ELI_KEY_SHIFT) / secsize;
|
|
|
|
g_eli_key_fill(&gdev->sc, &gkey, keyno);
|
2016-03-16 23:12:19 +00:00
|
|
|
|
2020-07-11 06:51:42 +00:00
|
|
|
error = geliboot_crypt(gdev->sc.sc_ealgo, enc, pbuf, secsize,
|
2018-07-13 17:50:25 +00:00
|
|
|
gkey.gek_key, gdev->sc.sc_ekeylen, iv);
|
2016-03-16 23:12:19 +00:00
|
|
|
|
2018-07-13 17:50:25 +00:00
|
|
|
if (error != 0) {
|
|
|
|
explicit_bzero(&gkey, sizeof(gkey));
|
2020-07-11 06:51:42 +00:00
|
|
|
printf("%s: Failed to %s!", __func__,
|
|
|
|
enc ? "encrypt" : "decrypt");
|
2018-07-13 17:50:25 +00:00
|
|
|
return (error);
|
2016-03-16 23:12:19 +00:00
|
|
|
}
|
2018-07-13 17:50:25 +00:00
|
|
|
pbuf += secsize;
|
2016-03-16 23:12:19 +00:00
|
|
|
}
|
2018-07-13 17:50:25 +00:00
|
|
|
explicit_bzero(&gkey, sizeof(gkey));
|
|
|
|
return (0);
|
2016-03-16 23:12:19 +00:00
|
|
|
}
|
|
|
|
|
Implement boot-time encryption key passing (keybuf)
This patch adds a general mechanism for providing encryption keys to the
kernel from the boot loader. This is intended to enable GELI support at
boot time, providing a better mechanism for passing keys to the kernel
than environment variables. It is designed to be extensible to other
applications, and can easily handle multiple encrypted volumes with
different keys.
This mechanism is currently used by the pending GELI EFI work.
Additionally, this mechanism can potentially be used to interface with
GRUB, opening up options for coreboot+GRUB configurations with completely
encrypted disks.
Another benefit over the existing system is that it does not require
re-deriving the user key from the password at each boot stage.
Most of this patch was written by Eric McCorkle. It was extended by
Allan Jude with a number of minor enhancements and extending the keybuf
feature into boot2.
GELI user keys are now derived once, in boot2, then passed to the loader,
which reuses the key, then passes it to the kernel, where the GELI module
destroys the keybuf after decrypting the volumes.
Submitted by: Eric McCorkle <eric@metricspace.net> (Original Version)
Reviewed by: oshogbo (earlier version), cem (earlier version)
MFC after: 3 weeks
Relnotes: yes
Sponsored by: ScaleEngine Inc.
Differential Revision: https://reviews.freebsd.org/D9575
2017-04-01 05:05:22 +00:00
|
|
|
int
|
2018-07-13 17:50:25 +00:00
|
|
|
geli_havekey(struct geli_dev *gdev)
|
Implement boot-time encryption key passing (keybuf)
This patch adds a general mechanism for providing encryption keys to the
kernel from the boot loader. This is intended to enable GELI support at
boot time, providing a better mechanism for passing keys to the kernel
than environment variables. It is designed to be extensible to other
applications, and can easily handle multiple encrypted volumes with
different keys.
This mechanism is currently used by the pending GELI EFI work.
Additionally, this mechanism can potentially be used to interface with
GRUB, opening up options for coreboot+GRUB configurations with completely
encrypted disks.
Another benefit over the existing system is that it does not require
re-deriving the user key from the password at each boot stage.
Most of this patch was written by Eric McCorkle. It was extended by
Allan Jude with a number of minor enhancements and extending the keybuf
feature into boot2.
GELI user keys are now derived once, in boot2, then passed to the loader,
which reuses the key, then passes it to the kernel, where the GELI module
destroys the keybuf after decrypting the volumes.
Submitted by: Eric McCorkle <eric@metricspace.net> (Original Version)
Reviewed by: oshogbo (earlier version), cem (earlier version)
MFC after: 3 weeks
Relnotes: yes
Sponsored by: ScaleEngine Inc.
Differential Revision: https://reviews.freebsd.org/D9575
2017-04-01 05:05:22 +00:00
|
|
|
{
|
|
|
|
u_char mkey[G_ELI_DATAIVKEYLEN];
|
2018-07-13 17:50:25 +00:00
|
|
|
int err;
|
Implement boot-time encryption key passing (keybuf)
This patch adds a general mechanism for providing encryption keys to the
kernel from the boot loader. This is intended to enable GELI support at
boot time, providing a better mechanism for passing keys to the kernel
than environment variables. It is designed to be extensible to other
applications, and can easily handle multiple encrypted volumes with
different keys.
This mechanism is currently used by the pending GELI EFI work.
Additionally, this mechanism can potentially be used to interface with
GRUB, opening up options for coreboot+GRUB configurations with completely
encrypted disks.
Another benefit over the existing system is that it does not require
re-deriving the user key from the password at each boot stage.
Most of this patch was written by Eric McCorkle. It was extended by
Allan Jude with a number of minor enhancements and extending the keybuf
feature into boot2.
GELI user keys are now derived once, in boot2, then passed to the loader,
which reuses the key, then passes it to the kernel, where the GELI module
destroys the keybuf after decrypting the volumes.
Submitted by: Eric McCorkle <eric@metricspace.net> (Original Version)
Reviewed by: oshogbo (earlier version), cem (earlier version)
MFC after: 3 weeks
Relnotes: yes
Sponsored by: ScaleEngine Inc.
Differential Revision: https://reviews.freebsd.org/D9575
2017-04-01 05:05:22 +00:00
|
|
|
|
2018-07-13 17:50:25 +00:00
|
|
|
err = ENOENT;
|
|
|
|
if (geli_findkey(gdev, mkey) == 0) {
|
|
|
|
if (geli_probe(gdev, NULL, mkey) == 0)
|
|
|
|
err = 0;
|
|
|
|
explicit_bzero(mkey, sizeof(mkey));
|
Implement boot-time encryption key passing (keybuf)
This patch adds a general mechanism for providing encryption keys to the
kernel from the boot loader. This is intended to enable GELI support at
boot time, providing a better mechanism for passing keys to the kernel
than environment variables. It is designed to be extensible to other
applications, and can easily handle multiple encrypted volumes with
different keys.
This mechanism is currently used by the pending GELI EFI work.
Additionally, this mechanism can potentially be used to interface with
GRUB, opening up options for coreboot+GRUB configurations with completely
encrypted disks.
Another benefit over the existing system is that it does not require
re-deriving the user key from the password at each boot stage.
Most of this patch was written by Eric McCorkle. It was extended by
Allan Jude with a number of minor enhancements and extending the keybuf
feature into boot2.
GELI user keys are now derived once, in boot2, then passed to the loader,
which reuses the key, then passes it to the kernel, where the GELI module
destroys the keybuf after decrypting the volumes.
Submitted by: Eric McCorkle <eric@metricspace.net> (Original Version)
Reviewed by: oshogbo (earlier version), cem (earlier version)
MFC after: 3 weeks
Relnotes: yes
Sponsored by: ScaleEngine Inc.
Differential Revision: https://reviews.freebsd.org/D9575
2017-04-01 05:05:22 +00:00
|
|
|
}
|
2018-07-13 17:50:25 +00:00
|
|
|
return (err);
|
Implement boot-time encryption key passing (keybuf)
This patch adds a general mechanism for providing encryption keys to the
kernel from the boot loader. This is intended to enable GELI support at
boot time, providing a better mechanism for passing keys to the kernel
than environment variables. It is designed to be extensible to other
applications, and can easily handle multiple encrypted volumes with
different keys.
This mechanism is currently used by the pending GELI EFI work.
Additionally, this mechanism can potentially be used to interface with
GRUB, opening up options for coreboot+GRUB configurations with completely
encrypted disks.
Another benefit over the existing system is that it does not require
re-deriving the user key from the password at each boot stage.
Most of this patch was written by Eric McCorkle. It was extended by
Allan Jude with a number of minor enhancements and extending the keybuf
feature into boot2.
GELI user keys are now derived once, in boot2, then passed to the loader,
which reuses the key, then passes it to the kernel, where the GELI module
destroys the keybuf after decrypting the volumes.
Submitted by: Eric McCorkle <eric@metricspace.net> (Original Version)
Reviewed by: oshogbo (earlier version), cem (earlier version)
MFC after: 3 weeks
Relnotes: yes
Sponsored by: ScaleEngine Inc.
Differential Revision: https://reviews.freebsd.org/D9575
2017-04-01 05:05:22 +00:00
|
|
|
}
|
|
|
|
|
2016-03-16 23:12:19 +00:00
|
|
|
int
|
2018-07-13 17:50:25 +00:00
|
|
|
geli_passphrase(struct geli_dev *gdev, char *pw)
|
2016-03-16 23:12:19 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2018-07-13 17:50:25 +00:00
|
|
|
/* TODO: Implement GELI keyfile(s) support */
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
/* Try cached passphrase */
|
|
|
|
if (i == 0 && pw[0] != '\0') {
|
|
|
|
if (geli_probe(gdev, pw, NULL) == 0) {
|
2016-03-16 23:12:19 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
}
|
2018-07-13 17:50:25 +00:00
|
|
|
printf("GELI Passphrase for %s ", gdev->name);
|
|
|
|
pwgets(pw, GELI_PW_MAXLEN,
|
|
|
|
(gdev->md.md_flags & G_ELI_FLAG_GELIDISPLAYPASS) == 0);
|
|
|
|
printf("\n");
|
|
|
|
if (geli_probe(gdev, pw, NULL) == 0) {
|
|
|
|
return (0);
|
|
|
|
}
|
2016-03-16 23:12:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (1);
|
|
|
|
}
|