*
* Copyright (C) 2004, Christophe Saout <christophe@saout.de>
* Copyright (C) 2004-2007, Clemens Fruhwirth <clemens@endorphin.org>
- * Copyright (C) 2009-2011, Red Hat, Inc. All rights reserved.
+ * Copyright (C) 2009-2012, Red Hat, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <string.h>
#include "libcryptsetup.h"
#include "luks.h"
#include "loopaes.h"
+#include "verity.h"
#include "internal.h"
-#include "crypto_backend.h"
struct crypt_device {
char *type;
char *plain_cipher;
char *plain_cipher_mode;
char *plain_uuid;
+ unsigned int plain_key_size;
/* used in CRYPT_LOOPAES */
struct crypt_params_loopaes loopaes_hdr;
char *loopaes_uuid;
unsigned int loopaes_key_size;
+ /* used in CRYPT_VERITY */
+ struct crypt_params_verity verity_hdr;
+ char *verity_root_hash;
+ uint64_t verity_root_hash_size;
+ char *verity_uuid;
+
/* callbacks definitions */
void (*log)(int level, const char *msg, void *usrptr);
void *log_usrptr;
void *confirm_usrptr;
int (*password)(const char *msg, char *buf, size_t length, void *usrptr);
void *password_usrptr;
+
+ /* last error message */
+ char error[MAX_ERROR_LENGTH];
};
+/* Global error */
+/* FIXME: not thread safe, remove this later */
+static char global_error[MAX_ERROR_LENGTH] = {0};
+
/* Log helper */
static void (*_default_log)(int level, const char *msg, void *usrptr) = NULL;
static int _debug_level = 0;
return _debug_level;
}
+static void crypt_set_error(struct crypt_device *cd, const char *error)
+{
+ size_t size = strlen(error);
+
+ /* Set global error, ugly hack... */
+ strncpy(global_error, error, MAX_ERROR_LENGTH - 2);
+ if (size < MAX_ERROR_LENGTH && global_error[size - 1] == '\n')
+ global_error[size - 1] = '\0';
+
+ /* Set error string per context */
+ if (cd) {
+ strncpy(cd->error, error, MAX_ERROR_LENGTH - 2);
+ if (size < MAX_ERROR_LENGTH && cd->error[size - 1] == '\n')
+ cd->error[size - 1] = '\0';
+ }
+}
+
void crypt_log(struct crypt_device *cd, int level, const char *msg)
{
if (cd && cd->log)
cd->log(level, msg, cd->log_usrptr);
else if (_default_log)
_default_log(level, msg, NULL);
+
+ if (level == CRYPT_LOG_ERROR)
+ crypt_set_error(cd, msg);
}
__attribute__((format(printf, 5, 6)))
va_start(argp, format);
- if (vasprintf(&target, format, argp) > 0) {
+ if (vasprintf(&target, format, argp) > 0 ) {
if (level >= 0) {
crypt_log(cd, level, target);
#ifdef CRYPT_DEBUG
{
int r;
+ crypt_fips_libcryptsetup_check(ctx);
+
r = crypt_random_init(ctx);
if (r < 0) {
log_err(ctx, _("Cannot initialize crypto RNG backend.\n"));
if (r < 0)
log_err(ctx, _("Cannot initialize crypto backend.\n"));
+ log_dbg("Crypto backend (%s) initialized.", crypt_backend_version());
return r;
}
return (type && !strcmp(CRYPT_LOOPAES, type));
}
+static int isVERITY(const char *type)
+{
+ return (type && !strcmp(CRYPT_VERITY, type));
+}
+
/* keyslot helpers */
static int keyslot_verify_or_find_empty(struct crypt_device *cd, int *keyslot)
{
{
int r;
char *dm_cipher = NULL;
+ enum devcheck device_check;
struct crypt_dm_active_device dmd = {
- .device = crypt_get_device_name(cd),
- .cipher = NULL,
+ .target = DM_CRYPT,
.uuid = crypt_get_uuid(cd),
- .vk = vk,
- .offset = crypt_get_data_offset(cd),
- .iv_offset = crypt_get_iv_offset(cd),
.size = size,
- .flags = flags
+ .flags = flags,
+ .data_device = crypt_get_device_name(cd),
+ .u.crypt = {
+ .cipher = NULL,
+ .vk = vk,
+ .offset = crypt_get_data_offset(cd),
+ .iv_offset = crypt_get_iv_offset(cd),
+ }
};
- r = device_check_and_adjust(cd, dmd.device,
- (dmd.flags & CRYPT_ACTIVATE_SHARED) ? DEV_SHARED : DEV_EXCL,
- &dmd.size, &dmd.offset, &flags);
+ if (dmd.flags & CRYPT_ACTIVATE_SHARED)
+ device_check = DEV_SHARED;
+ else
+ device_check = DEV_EXCL;
+
+ r = device_check_and_adjust(cd, dmd.data_device, device_check,
+ &dmd.size, &dmd.u.crypt.offset, &dmd.flags);
if (r)
return r;
if (r < 0)
return -ENOMEM;
- dmd.cipher = dm_cipher;
- log_dbg("Trying to activate PLAIN device %s using cipher %s.", name, dmd.cipher);
+ dmd.u.crypt.cipher = dm_cipher;
+ log_dbg("Trying to activate PLAIN device %s using cipher %s.",
+ name, dmd.u.crypt.cipher);
r = dm_create_device(name, CRYPT_PLAIN, &dmd, 0);
} else
*key_len = r;
} else
- r = crypt_get_key(msg, key, key_len, 0, NULL, cd->timeout,
+ r = crypt_get_key(msg, key, key_len, 0, 0, NULL, cd->timeout,
(force_verify || cd->password_verify), cd);
out:
free(prompt);
r = key_from_terminal(cd, NULL, &passphrase_read,
&passphrase_size_read, 0);
+ /* Continue if it is just passphrase verify mismatch */
+ if (r == -EPERM)
+ continue;
if(r < 0)
goto out;
static int key_from_file(struct crypt_device *cd, char *msg,
char **key, size_t *key_len,
- const char *key_file, size_t key_size)
+ const char *key_file, size_t key_offset,
+ size_t key_size)
{
- return crypt_get_key(msg, key, key_len, key_size, key_file,
+ return crypt_get_key(msg, key, key_len, key_offset, key_size, key_file,
cd->timeout, 0, cd);
}
cd->password_usrptr = usrptr;
}
-void crypt_get_error(char *buf, size_t size)
+static void _get_error(char *error, char *buf, size_t size)
{
- const char *error = get_error();
-
if (!buf || size < 1)
- set_error(NULL);
- else if (error) {
+ error[0] = '\0';
+ else if (*error) {
strncpy(buf, error, size - 1);
buf[size - 1] = '\0';
- set_error(NULL);
+ error[0] = '\0';
} else
buf[0] = '\0';
}
+void crypt_last_error(struct crypt_device *cd, char *buf, size_t size)
+{
+ if (cd)
+ return _get_error(cd->error, buf, size);
+}
+
+/* Deprecated global error interface */
+void crypt_get_error(char *buf, size_t size)
+{
+ return _get_error(global_error, buf, size);
+}
+
const char *crypt_get_dir(void)
{
return dm_get_dir();
uint64_t size, size_min;
/* Check data device size, require at least one sector */
- size_min = crypt_get_data_offset(cd) ?: SECTOR_SIZE;
+ size_min = crypt_get_data_offset(cd) << SECTOR_SHIFT ?: SECTOR_SIZE;
r = device_size(crypt_get_device_name(cd), &size);
if (r < 0)
log_dbg("Setting ciphertext data device to %s.", device ?: "(none)");
- if (!isLUKS(cd->type)) {
+ if (!isLUKS(cd->type) && !isVERITY(cd->type)) {
log_err(cd, _("This operation is not supported for this device type.\n"));
return -EINVAL;
}
/* metadata device must be set */
- if (!cd->device)
+ if (!cd->device || !device)
return -EINVAL;
r = device_ready(NULL, device, O_RDONLY);
return crypt_check_data_device_size(cd);
}
+static int _crypt_load_luks1(struct crypt_device *cd, int require_header, int repair)
+{
+ struct luks_phdr hdr;
+ int r;
+
+ r = init_crypto(cd);
+ if (r < 0)
+ return r;
+
+ r = LUKS_read_phdr(mdata_device(cd), &hdr, require_header, repair, cd);
+ if (r < 0)
+ return r;
+
+ if (!cd->type && !(cd->type = strdup(CRYPT_LUKS1)))
+ return -ENOMEM;
+
+ memcpy(&cd->hdr, &hdr, sizeof(hdr));
+
+ return r;
+}
+
+static int _crypt_load_verity(struct crypt_device *cd, struct crypt_params_verity *params)
+{
+ int r;
+ size_t sb_offset = 0;
+
+ r = init_crypto(cd);
+ if (r < 0)
+ return r;
+
+ if (params->flags & CRYPT_VERITY_NO_HEADER)
+ return -EINVAL;
+
+ if (params)
+ sb_offset = params->hash_area_offset;
+
+ r = VERITY_read_sb(cd, mdata_device(cd), sb_offset,
+ &cd->verity_uuid, &cd->verity_hdr);
+ if (r < 0)
+ return r;
+
+ if (params)
+ cd->verity_hdr.flags = params->flags;
+
+ if (params && params->data_device &&
+ (r = crypt_set_data_device(cd, params->data_device)) < 0)
+ return r;
+
+ cd->verity_root_hash_size = crypt_hash_size(cd->verity_hdr.hash_name);
+
+ if (!cd->type && !(cd->type = strdup(CRYPT_VERITY)))
+ return -ENOMEM;
+
+ return r;
+}
+
+static int _init_by_name_crypt(struct crypt_device *cd, const char *name)
+{
+ struct crypt_dm_active_device dmd = {};
+ char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
+ int key_nums, r;
+
+ r = dm_query_device(name, DM_ACTIVE_DEVICE |
+ DM_ACTIVE_UUID |
+ DM_ACTIVE_CRYPT_CIPHER |
+ DM_ACTIVE_CRYPT_KEYSIZE, &dmd);
+ if (r < 0)
+ goto out;
+
+ if (isPLAIN(cd->type)) {
+ cd->plain_uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
+ cd->plain_hdr.hash = NULL; /* no way to get this */
+ cd->plain_hdr.offset = dmd.u.crypt.offset;
+ cd->plain_hdr.skip = dmd.u.crypt.iv_offset;
+ cd->plain_key_size = dmd.u.crypt.vk->keylength;
+
+ r = crypt_parse_name_and_mode(dmd.u.crypt.cipher, cipher, NULL, cipher_mode);
+ if (!r) {
+ cd->plain_cipher = strdup(cipher);
+ cd->plain_cipher_mode = strdup(cipher_mode);
+ }
+ } else if (isLOOPAES(cd->type)) {
+ cd->loopaes_uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
+ cd->loopaes_hdr.offset = dmd.u.crypt.offset;
+
+ r = crypt_parse_name_and_mode(dmd.u.crypt.cipher, cipher,
+ &key_nums, cipher_mode);
+ if (!r) {
+ cd->loopaes_cipher = strdup(cipher);
+ cd->loopaes_cipher_mode = strdup(cipher_mode);
+ /* version 3 uses last key for IV */
+ if (dmd.u.crypt.vk->keylength % key_nums)
+ key_nums++;
+ cd->loopaes_key_size = dmd.u.crypt.vk->keylength / key_nums;
+ }
+ } else if (isLUKS(cd->type)) {
+ if (mdata_device(cd)) {
+ r = _crypt_load_luks1(cd, 0, 0);
+ if (r < 0) {
+ log_dbg("LUKS device header does not match active device.");
+ free(cd->type);
+ cd->type = NULL;
+ r = 0;
+ goto out;
+ }
+ /* check whether UUIDs match each other */
+ r = crypt_uuid_cmp(dmd.uuid, cd->hdr.uuid);
+ if (r < 0) {
+ log_dbg("LUKS device header uuid: %s mismatches DM returned uuid %s",
+ cd->hdr.uuid, dmd.uuid);
+ free(cd->type);
+ cd->type = NULL;
+ r = 0;
+ goto out;
+ }
+ }
+ }
+out:
+ crypt_free_volume_key(dmd.u.crypt.vk);
+ free(CONST_CAST(void*)dmd.u.crypt.cipher);
+ free(CONST_CAST(void*)dmd.data_device);
+ free(CONST_CAST(void*)dmd.uuid);
+ return r;
+}
+
+static int _init_by_name_verity(struct crypt_device *cd, const char *name)
+{
+ struct crypt_params_verity params = {};
+ struct crypt_dm_active_device dmd = {
+ .target = DM_VERITY,
+ .u.verity.vp = ¶ms,
+ };
+ int r;
+
+ r = dm_query_device(name, DM_ACTIVE_DEVICE |
+ DM_ACTIVE_UUID |
+ DM_ACTIVE_VERITY_HASH_DEVICE |
+ DM_ACTIVE_VERITY_PARAMS, &dmd);
+ if (r < 0)
+ goto out;
+
+ if (isVERITY(cd->type)) {
+ cd->verity_uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
+ cd->verity_hdr.flags = CRYPT_VERITY_NO_HEADER; //FIXME
+ cd->verity_hdr.data_size = params.data_size;
+ cd->verity_root_hash_size = dmd.u.verity.root_hash_size;
+ cd->verity_root_hash = NULL;
+ cd->verity_hdr.hash_name = params.hash_name;
+ cd->verity_hdr.data_device = NULL;
+ cd->verity_hdr.hash_device = NULL;
+ cd->verity_hdr.data_block_size = params.data_block_size;
+ cd->verity_hdr.hash_block_size = params.hash_block_size;
+ cd->verity_hdr.hash_area_offset = dmd.u.verity.hash_offset;
+ cd->verity_hdr.hash_type = params.hash_type;
+ cd->verity_hdr.flags = params.flags;
+ cd->verity_hdr.salt_size = params.salt_size;
+ cd->verity_hdr.salt = params.salt;
+ if (!(cd->metadata_device = strdup(dmd.u.verity.hash_device)))
+ r = -ENOMEM;
+ }
+out:
+ free(CONST_CAST(void*)dmd.u.verity.hash_device);
+ free(CONST_CAST(void*)dmd.data_device);
+ free(CONST_CAST(void*)dmd.uuid);
+ return r;
+}
+
int crypt_init_by_name_and_header(struct crypt_device **cd,
const char *name,
const char *header_device)
{
crypt_status_info ci;
struct crypt_dm_active_device dmd;
- char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
- int key_nums, r;
-
+ int r;
log_dbg("Allocating crypt device context by device %s.", name);
return -ENODEV;
}
- r = dm_query_device(name, DM_ACTIVE_DEVICE | DM_ACTIVE_CIPHER |
- DM_ACTIVE_UUID | DM_ACTIVE_KEYSIZE, &dmd);
+ r = dm_query_device(name, DM_ACTIVE_DEVICE | DM_ACTIVE_UUID, &dmd);
if (r < 0)
goto out;
if (header_device) {
r = crypt_init(cd, header_device);
} else {
- r = crypt_init(cd, dmd.device);
+ r = crypt_init(cd, dmd.data_device);
/* Underlying device disappeared but mapping still active */
- if (!dmd.device || r == -ENOTBLK)
+ if (!dmd.data_device || r == -ENOTBLK)
log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
name);
/* Underlying device is not readable but crypt mapping exists */
if (r == -ENOTBLK) {
- free(CONST_CAST(void*)dmd.device);
- dmd.device = NULL;
+ free(CONST_CAST(void*)dmd.data_device);
+ dmd.data_device = NULL;
r = crypt_init(cd, NULL);
}
}
(*cd)->type = strdup(CRYPT_LOOPAES);
else if (!strncmp(CRYPT_LUKS1, dmd.uuid, sizeof(CRYPT_LUKS1)-1))
(*cd)->type = strdup(CRYPT_LUKS1);
+ else if (!strncmp(CRYPT_VERITY, dmd.uuid, sizeof(CRYPT_VERITY)-1))
+ (*cd)->type = strdup(CRYPT_VERITY);
else
log_dbg("Unknown UUID set, some parameters are not set.");
} else
log_dbg("Active device has no UUID set, some parameters are not set.");
if (header_device) {
- r = crypt_set_data_device(*cd, dmd.device);
+ r = crypt_set_data_device(*cd, dmd.data_device);
if (r < 0)
goto out;
}
/* Try to initialise basic parameters from active device */
- if (!(*cd)->backing_file && dmd.device && crypt_loop_device(dmd.device) &&
- !((*cd)->backing_file = crypt_loop_backing_file(dmd.device))) {
+ if (!(*cd)->backing_file && dmd.data_device &&
+ crypt_loop_device(dmd.data_device) &&
+ !((*cd)->backing_file = crypt_loop_backing_file(dmd.data_device))) {
r = -ENOMEM;
goto out;
}
- if (isPLAIN((*cd)->type)) {
- (*cd)->plain_uuid = strdup(dmd.uuid);
- (*cd)->plain_hdr.hash = NULL; /* no way to get this */
- (*cd)->plain_hdr.offset = dmd.offset;
- (*cd)->plain_hdr.skip = dmd.iv_offset;
-
- r = crypt_parse_name_and_mode(dmd.cipher, cipher, NULL, cipher_mode);
- if (!r) {
- (*cd)->plain_cipher = strdup(cipher);
- (*cd)->plain_cipher_mode = strdup(cipher_mode);
- }
- } else if (isLOOPAES((*cd)->type)) {
- (*cd)->loopaes_uuid = strdup(dmd.uuid);
- (*cd)->loopaes_hdr.offset = dmd.offset;
-
- r = crypt_parse_name_and_mode(dmd.cipher, cipher,
- &key_nums, cipher_mode);
- if (!r) {
- (*cd)->loopaes_cipher = strdup(cipher);
- (*cd)->loopaes_cipher_mode = strdup(cipher_mode);
- /* version 3 uses last key for IV */
- if (dmd.vk->keylength % key_nums)
- key_nums++;
- (*cd)->loopaes_key_size = dmd.vk->keylength / key_nums;
- }
- } else if (isLUKS((*cd)->type)) {
- if (mdata_device(*cd)) {
- r = crypt_load(*cd, CRYPT_LUKS1, NULL);
- if (r < 0) {
- log_dbg("LUKS device header does not match active device.");
- free((*cd)->type);
- (*cd)->type = NULL;
- r = 0;
- goto out;
- }
- /* checks whether UUIDs match each other */
- r = crypt_uuid_cmp(dmd.uuid, (*cd)->hdr.uuid);
- if (r < 0) {
- log_dbg("LUKS device header uuid: %s mismatches DM returned uuid %s",
- (*cd)->hdr.uuid, dmd.uuid);
- free((*cd)->type);
- (*cd)->type = NULL;
- r = 0;
- goto out;
- }
- }
- }
-
+ if (dmd.target == DM_CRYPT)
+ r = _init_by_name_crypt(*cd, name);
+ else if (dmd.target == DM_VERITY)
+ r = _init_by_name_verity(*cd, name);
out:
if (r < 0) {
crypt_free(*cd);
*cd = NULL;
}
- crypt_free_volume_key(dmd.vk);
- free(CONST_CAST(void*)dmd.device);
- free(CONST_CAST(void*)dmd.cipher);
+ free(CONST_CAST(void*)dmd.data_device);
free(CONST_CAST(void*)dmd.uuid);
return r;
}
return -EINVAL;
}
+ cd->plain_key_size = volume_key_size;
cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
if (!cd->volume_key)
return -ENOMEM;
return r;
/* Wipe first 8 sectors - fs magic numbers etc. */
- r = wipe_device_header(mdata_device(cd), 8);
+ r = crypt_wipe(mdata_device(cd), 0, 8 * SECTOR_SIZE, CRYPT_WIPE_ZERO, 1);
if(r < 0) {
if (r == -EBUSY)
log_err(cd, _("Cannot format device %s which is still in use.\n"),
return 0;
}
+static int _crypt_format_verity(struct crypt_device *cd,
+ const char *uuid,
+ struct crypt_params_verity *params)
+{
+ int r = 0;
+ uint64_t data_device_size;
+
+ if (!mdata_device(cd)) {
+ log_err(cd, _("Can't format VERITY without device.\n"));
+ return -EINVAL;
+ }
+
+ if (!params || !params->data_device)
+ return -EINVAL;
+
+ /* set data device */
+ cd->type = CRYPT_VERITY;
+ r = crypt_set_data_device(cd, params->data_device);
+ cd->type = NULL;
+ if (r)
+ return r;
+ if (!params->data_size) {
+ r = device_size(params->data_device, &data_device_size);
+ if (r < 0)
+ return r;
+
+ cd->verity_hdr.data_size = data_device_size / params->data_block_size;
+ } else
+ cd->verity_hdr.data_size = params->data_size;
+
+ cd->verity_root_hash_size = crypt_hash_size(params->hash_name);
+ if (!cd->verity_root_hash_size)
+ return -EINVAL;
+
+ cd->verity_root_hash = malloc(cd->verity_root_hash_size);
+ if (!cd->verity_root_hash)
+ return -ENOMEM;
+
+ cd->verity_hdr.flags = params->flags;
+ cd->verity_hdr.hash_name = strdup(params->hash_name);
+ cd->verity_hdr.data_device = NULL;
+ cd->verity_hdr.data_block_size = params->data_block_size;
+ cd->verity_hdr.hash_block_size = params->hash_block_size;
+ cd->verity_hdr.hash_area_offset = params->hash_area_offset;
+ cd->verity_hdr.hash_type = params->hash_type;
+ cd->verity_hdr.flags = params->flags;
+ cd->verity_hdr.salt_size = params->salt_size;
+ cd->verity_hdr.salt = malloc(params->salt_size);
+ if (params->salt)
+ memcpy(CONST_CAST(char*)cd->verity_hdr.salt, params->salt,
+ params->salt_size);
+ else
+ r = crypt_random_get(cd, CONST_CAST(char*)cd->verity_hdr.salt,
+ params->salt_size, CRYPT_RND_SALT);
+ if (r)
+ return r;
+
+ if (params->flags & CRYPT_VERITY_CREATE_HASH) {
+ r = VERITY_create(cd, &cd->verity_hdr, cd->device, mdata_device(cd),
+ cd->verity_root_hash, cd->verity_root_hash_size);
+ if (r)
+ return r;
+ }
+
+ if (!(params->flags & CRYPT_VERITY_NO_HEADER)) {
+ if (uuid)
+ cd->verity_uuid = strdup(uuid);
+ else {
+ r = VERITY_UUID_generate(cd, &cd->verity_uuid);
+ if (r)
+ return r;
+ }
+
+ r = VERITY_write_sb(cd, mdata_device(cd),
+ cd->verity_hdr.hash_area_offset,
+ cd->verity_uuid,
+ &cd->verity_hdr);
+ }
+ return r;
+}
+
int crypt_format(struct crypt_device *cd,
const char *type,
const char *cipher,
if (!type)
return -EINVAL;
+ if (cd->type) {
+ log_dbg("Context already formatted as %s.", cd->type);
+ return -EINVAL;
+ }
+
log_dbg("Formatting device %s as type %s.", mdata_device(cd) ?: "(none)", type);
r = init_crypto(cd);
uuid, volume_key, volume_key_size, params);
else if (isLOOPAES(type))
r = _crypt_format_loopaes(cd, cipher, uuid, volume_key_size, params);
+ else if (isVERITY(type))
+ r = _crypt_format_verity(cd, uuid, params);
else {
/* FIXME: allow plugins here? */
log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
int crypt_load(struct crypt_device *cd,
const char *requested_type,
- void *params __attribute__((unused)))
+ void *params)
{
- struct luks_phdr hdr;
int r;
log_dbg("Trying to load %s crypt type from device %s.",
if (!mdata_device(cd))
return -EINVAL;
- if (requested_type && !isLUKS(requested_type))
- return -EINVAL;
+ if (!requested_type || isLUKS(requested_type)) {
+ if (cd->type && !isLUKS(cd->type)) {
+ log_dbg("Context is already initialised to type %s", cd->type);
+ return -EINVAL;
+ }
- if (cd->type && !isLUKS(cd->type)) {
- log_dbg("Context is already initialised to type %s", cd->type);
+ r = _crypt_load_luks1(cd, 1, 0);
+ } else if (isVERITY(requested_type)) {
+ if (cd->type && !isVERITY(cd->type)) {
+ log_dbg("Context is already initialised to type %s", cd->type);
+ return -EINVAL;
+ }
+ r = _crypt_load_verity(cd, params);
+ } else
return -EINVAL;
- }
- r = init_crypto(cd);
if (r < 0)
return r;
- r = LUKS_read_phdr(mdata_device(cd), &hdr, 1, cd);
- if (r < 0)
- return r;
+ /* cd->type and header must be set in context */
+ r = crypt_check_data_device_size(cd);
+ if (r < 0) {
+ free(cd->type);
+ cd->type = NULL;
+ }
- if (!cd->type && !(cd->type = strdup(CRYPT_LUKS1)))
- return -ENOMEM;
+ return r;
+}
- memcpy(&cd->hdr, &hdr, sizeof(hdr));
+int crypt_repair(struct crypt_device *cd,
+ const char *requested_type,
+ void *params __attribute__((unused)))
+{
+ int r;
+
+ log_dbg("Trying to repair %s crypt type from device %s.",
+ requested_type ?: "any", mdata_device(cd) ?: "(none)");
+
+ if (!mdata_device(cd))
+ return -EINVAL;
+
+ if (requested_type && !isLUKS(requested_type))
+ return -EINVAL;
+
+
+ /* Load with repair */
+ r = _crypt_load_luks1(cd, 1, 1);
+ if (r < 0)
+ return r;
/* cd->type and header must be set in context */
r = crypt_check_data_device_size(cd);
log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
- r = dm_query_device(name, DM_ACTIVE_DEVICE | DM_ACTIVE_CIPHER |
- DM_ACTIVE_UUID | DM_ACTIVE_KEYSIZE |
- DM_ACTIVE_KEY, &dmd);
+ r = dm_query_device(name, DM_ACTIVE_DEVICE | DM_ACTIVE_CRYPT_CIPHER |
+ DM_ACTIVE_UUID | DM_ACTIVE_CRYPT_KEYSIZE |
+ DM_ACTIVE_CRYPT_KEY, &dmd);
if (r < 0) {
log_err(NULL, _("Device %s is not active.\n"), name);
- goto out;
+ return -EINVAL;
}
- if (!dmd.uuid) {
+ if (!dmd.uuid || dmd.target != DM_CRYPT) {
r = -EINVAL;
goto out;
}
- r = device_check_and_adjust(cd, dmd.device, DEV_OK, &new_size, &dmd.offset, &dmd.flags);
+ r = device_check_and_adjust(cd, dmd.data_device, DEV_OK, &new_size,
+ &dmd.u.crypt.offset, &dmd.flags);
if (r)
goto out;
r = dm_create_device(name, cd->type, &dmd, 1);
}
out:
- crypt_free_volume_key(dmd.vk);
- free(CONST_CAST(void*)dmd.cipher);
- free(CONST_CAST(void*)dmd.device);
+ if (dmd.target == DM_CRYPT) {
+ crypt_free_volume_key(dmd.u.crypt.vk);
+ free(CONST_CAST(void*)dmd.u.crypt.cipher);
+ }
+ free(CONST_CAST(void*)dmd.data_device);
free(CONST_CAST(void*)dmd.uuid);
return r;
if (requested_type && !isLUKS(requested_type))
return -EINVAL;
- /* Some hash functions need initialized gcrypt library */
r = init_crypto(cd);
if (r < 0)
return r;
free(cd->loopaes_cipher);
free(cd->loopaes_uuid);
+ /* used in verity device only */
+ free(CONST_CAST(void*)cd->verity_hdr.hash_name);
+ free(CONST_CAST(void*)cd->verity_hdr.salt);
+ free(cd->verity_root_hash);
+ free(cd->verity_uuid);
+
free(cd);
}
}
return r < 0 ? r : keyslot;
}
-int crypt_resume_by_keyfile(struct crypt_device *cd,
- const char *name,
- int keyslot,
- const char *keyfile,
- size_t keyfile_size)
+int crypt_resume_by_keyfile_offset(struct crypt_device *cd,
+ const char *name,
+ int keyslot,
+ const char *keyfile,
+ size_t keyfile_size,
+ size_t keyfile_offset)
{
struct volume_key *vk = NULL;
char *passphrase_read = NULL;
return -EINVAL;
r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
- &passphrase_size_read, keyfile, keyfile_size);
+ &passphrase_size_read, keyfile, keyfile_offset,
+ keyfile_size);
if (r < 0)
goto out;
return r < 0 ? r : keyslot;
}
+int crypt_resume_by_keyfile(struct crypt_device *cd,
+ const char *name,
+ int keyslot,
+ const char *keyfile,
+ size_t keyfile_size)
+{
+ return crypt_resume_by_keyfile_offset(cd, name, keyslot,
+ keyfile, keyfile_size, 0);
+}
+
// slot manipulation
int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
int keyslot, // -1 any
return r ?: keyslot;
}
-int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
+int crypt_keyslot_add_by_keyfile_offset(struct crypt_device *cd,
int keyslot,
const char *keyfile,
size_t keyfile_size,
+ size_t keyfile_offset,
const char *new_keyfile,
- size_t new_keyfile_size)
+ size_t new_keyfile_size,
+ size_t new_keyfile_offset)
{
struct volume_key *vk = NULL;
char *password = NULL; size_t passwordLen;
if (keyfile)
r = key_from_file(cd, _("Enter any passphrase: "),
&password, &passwordLen,
- keyfile, keyfile_size);
+ keyfile, keyfile_offset, keyfile_size);
else
r = key_from_terminal(cd, _("Enter any passphrase: "),
&password, &passwordLen, 0);
if (new_keyfile)
r = key_from_file(cd, _("Enter new passphrase for key slot: "),
&new_password, &new_passwordLen, new_keyfile,
- new_keyfile_size);
+ new_keyfile_offset, new_keyfile_size);
else
r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
&new_password, &new_passwordLen, 1);
return r < 0 ? r : keyslot;
}
+int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
+ int keyslot,
+ const char *keyfile,
+ size_t keyfile_size,
+ const char *new_keyfile,
+ size_t new_keyfile_size)
+{
+ return crypt_keyslot_add_by_keyfile_offset(cd, keyslot,
+ keyfile, keyfile_size, 0,
+ new_keyfile, new_keyfile_size, 0);
+}
+
int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
int keyslot,
const char *volume_key,
}
r = process_key(cd, cd->plain_hdr.hash,
- cd->volume_key->keylength,
+ cd->plain_key_size,
passphrase, passphrase_size, &vk);
if (r < 0)
goto out;
return r < 0 ? r : keyslot;
}
-int crypt_activate_by_keyfile(struct crypt_device *cd,
+int crypt_activate_by_keyfile_offset(struct crypt_device *cd,
const char *name,
int keyslot,
const char *keyfile,
size_t keyfile_size,
+ size_t keyfile_offset,
uint32_t flags)
{
crypt_status_info ci;
r = key_from_file(cd, _("Enter passphrase: "),
&passphrase_read, &passphrase_size_read,
- keyfile, keyfile_size);
+ keyfile, keyfile_offset, keyfile_size);
if (r < 0)
goto out;
r = process_key(cd, cd->plain_hdr.hash,
- cd->volume_key->keylength,
+ cd->plain_key_size,
passphrase_read, passphrase_size_read, &vk);
if (r < 0)
goto out;
r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
} else if (isLUKS(cd->type)) {
r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
- &passphrase_size_read, keyfile, keyfile_size);
+ &passphrase_size_read, keyfile, keyfile_offset, keyfile_size);
if (r < 0)
goto out;
r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase_read,
r = keyslot;
} else if (isLOOPAES(cd->type)) {
r = key_from_file(cd, NULL, &passphrase_read, &passphrase_size_read,
- keyfile, keyfile_size);
+ keyfile, keyfile_offset, keyfile_size);
if (r < 0)
goto out;
r = LOOPAES_parse_keyfile(cd, &vk, cd->loopaes_hdr.hash, &key_count,
return r;
}
+int crypt_activate_by_keyfile(struct crypt_device *cd,
+ const char *name,
+ int keyslot,
+ const char *keyfile,
+ size_t keyfile_size,
+ uint32_t flags)
+{
+ return crypt_activate_by_keyfile_offset(cd, name, keyslot, keyfile,
+ keyfile_size, 0, flags);
+}
+
int crypt_activate_by_volume_key(struct crypt_device *cd,
const char *name,
const char *volume_key,
struct volume_key *vk = NULL;
int r = -EINVAL;
- log_dbg("Activating volume %s by volume key.", name);
+ log_dbg("Activating volume %s by volume key.", name ?: "[none]");
if (name) {
ci = crypt_status(NULL, name);
if (!name)
return -EINVAL;
- if (!volume_key || !volume_key_size || !cd->volume_key ||
- volume_key_size != cd->volume_key->keylength) {
+ if (!volume_key || !volume_key_size || volume_key_size != cd->plain_key_size) {
log_err(cd, _("Incorrect volume key specified for plain device.\n"));
return -EINVAL;
}
if (!r && name)
r = LUKS1_activate(cd, name, vk, flags);
+ } else if (isVERITY(cd->type)) {
+ /* volume_key == root hash */
+ if (!volume_key || !volume_key_size) {
+ log_err(cd, _("Incorrect root hash specified for verity device.\n"));
+ return -EINVAL;
+ }
+
+ r = VERITY_activate(cd, name, mdata_device(cd),
+ volume_key, volume_key_size,
+ &cd->verity_hdr, CRYPT_ACTIVATE_READONLY);
+
+ if (r == -EPERM) {
+ free(cd->verity_root_hash);
+ cd->verity_root_hash = NULL;
+ } if (!r) {
+ cd->verity_root_hash_size = volume_key_size;
+ if (!cd->verity_root_hash)
+ cd->verity_root_hash = malloc(volume_key_size);
+ if (cd->verity_root_hash)
+ memcpy(cd->verity_root_hash, volume_key, volume_key_size);
+ }
} else
log_err(cd, _("Device type is not properly initialised.\n"));
switch (crypt_status(cd, name)) {
case CRYPT_ACTIVE:
- r = dm_remove_device(name, 0, 0);
- break;
case CRYPT_BUSY:
- log_err(cd, _("Device %s is busy.\n"), name);
- r = -EBUSY;
+ r = dm_remove_device(name, 0, 0);
break;
case CRYPT_INACTIVE:
log_err(cd, _("Device %s is not active.\n"), name);
unsigned key_len;
int r = -EINVAL;
+ if (crypt_fips_mode()) {
+ log_err(cd, "Function not available in FIPS mode.\n");
+ return -EACCES;
+ }
+
key_len = crypt_get_volume_key_size(cd);
if (key_len > *volume_key_size) {
log_err(cd, _("Volume key buffer too small.\n"));
cd->tries = tries;
}
-void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
+void crypt_set_iteration_time(struct crypt_device *cd, uint64_t iteration_time_ms)
{
log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
cd->iteration_time = iteration_time_ms;
}
+void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
+{
+ crypt_set_iteration_time(cd, iteration_time_ms);
+}
void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
{
return CRYPT_INACTIVE;
}
-static void hexprintICB(struct crypt_device *cd, char *d, int n)
+static void hexprint(struct crypt_device *cd, const char *d, int n, const char *sep)
{
int i;
for(i = 0; i < n; i++)
- log_std(cd, "%02hhx ", (char)d[i]);
+ log_std(cd, "%02hhx%s", (const char)d[i], sep);
}
-int crypt_dump(struct crypt_device *cd)
+static int _luks_dump(struct crypt_device *cd)
{
int i;
- if (!isLUKS(cd->type)) { //FIXME
- log_err(cd, _("This operation is supported only for LUKS device.\n"));
- return -EINVAL;
- }
log_std(cd, "LUKS header information for %s\n\n", mdata_device(cd));
log_std(cd, "Version: \t%d\n", cd->hdr.version);
log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
log_std(cd, "MK digest: \t");
- hexprintICB(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE);
+ hexprint(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE, " ");
log_std(cd, "\n");
log_std(cd, "MK salt: \t");
- hexprintICB(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2);
+ hexprint(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2, " ");
log_std(cd, "\n \t");
- hexprintICB(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
+ hexprint(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ");
log_std(cd, "\n");
log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
log_std(cd, "\tIterations: \t%d\n",
cd->hdr.keyblock[i].passwordIterations);
log_std(cd, "\tSalt: \t");
- hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt,
- LUKS_SALTSIZE/2);
+ hexprint(cd, cd->hdr.keyblock[i].passwordSalt,
+ LUKS_SALTSIZE/2, " ");
log_std(cd, "\n\t \t");
- hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt +
- LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
+ hexprint(cd, cd->hdr.keyblock[i].passwordSalt +
+ LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ");
log_std(cd, "\n");
log_std(cd, "\tKey material offset:\t%d\n",
else
log_std(cd, "Key Slot %d: DISABLED\n", i);
}
+ return 0;
+}
+static int _verity_dump(struct crypt_device *cd)
+{
+ log_std(cd, "VERITY header information for %s\n", mdata_device(cd));
+ log_std(cd, "UUID: \t%s\n", cd->verity_uuid ?: "");
+ log_std(cd, "Hash type: \t%u\n", cd->verity_hdr.hash_type);
+ log_std(cd, "Data blocks: \t%" PRIu64 "\n", cd->verity_hdr.data_size);
+ log_std(cd, "Data block size: \t%u\n", cd->verity_hdr.data_block_size);
+ log_std(cd, "Hash block size: \t%u\n", cd->verity_hdr.hash_block_size);
+ log_std(cd, "Hash algorithm: \t%s\n", cd->verity_hdr.hash_name);
+ log_std(cd, "Salt: \t");
+ if (cd->verity_hdr.salt_size)
+ hexprint(cd, cd->verity_hdr.salt, cd->verity_hdr.salt_size, "");
+ else
+ log_std(cd, "-");
+ log_std(cd, "\n");
+ if (cd->verity_root_hash) {
+ log_std(cd, "Root hash: \t");
+ hexprint(cd, cd->verity_root_hash, cd->verity_root_hash_size, "");
+ log_std(cd, "\n");
+ }
return 0;
}
+int crypt_dump(struct crypt_device *cd)
+{
+ if (isLUKS(cd->type))
+ return _luks_dump(cd);
+ else if (isVERITY(cd->type))
+ return _verity_dump(cd);
+
+ log_err(cd, _("Dump operation is not supported for this device type.\n"));
+ return -EINVAL;
+}
+
const char *crypt_get_cipher(struct crypt_device *cd)
{
if (isPLAIN(cd->type))
if (isLOOPAES(cd->type))
return cd->loopaes_uuid;
+ if (isVERITY(cd->type))
+ return cd->verity_uuid;
+
return NULL;
}
return cd->device;
}
-
int crypt_get_volume_key_size(struct crypt_device *cd)
{
- if (isPLAIN(cd->type) && cd->volume_key)
- return cd->volume_key->keylength;
+ if (isPLAIN(cd->type))
+ return cd->plain_key_size;
if (isLUKS(cd->type))
return cd->hdr.keyBytes;
if (isLOOPAES(cd->type))
return cd->loopaes_key_size;
+ if (isVERITY(cd->type))
+ return cd->verity_root_hash_size;
+
return 0;
}
return cd->type;
}
+int crypt_get_verity_info(struct crypt_device *cd,
+ struct crypt_params_verity *vp)
+{
+ if (!isVERITY(cd->type) || !vp)
+ return -EINVAL;
+
+ vp->data_device = cd->device;
+ vp->hash_device = mdata_device(cd);
+ vp->hash_name = cd->verity_hdr.hash_name;
+ vp->salt = cd->verity_hdr.salt;
+ vp->salt_size = cd->verity_hdr.salt_size;
+ vp->data_block_size = cd->verity_hdr.data_block_size;
+ vp->hash_block_size = cd->verity_hdr.hash_block_size;
+ vp->data_size = cd->verity_hdr.data_size;
+ vp->hash_area_offset = cd->verity_hdr.hash_area_offset;
+ vp->hash_type = cd->verity_hdr.hash_type;
+ vp->flags = cd->verity_hdr.flags & CRYPT_VERITY_NO_HEADER;
+ return 0;
+}
+
int crypt_get_active_device(struct crypt_device *cd __attribute__((unused)),
const char *name,
struct crypt_active_device *cad)
if (r < 0)
return r;
- cad->offset = dmd.offset;
- cad->iv_offset = dmd.iv_offset;
+ if (dmd.target != DM_CRYPT && dmd.target != DM_VERITY)
+ return -ENOTSUP;
+
+ cad->offset = dmd.u.crypt.offset;
+ cad->iv_offset = dmd.u.crypt.iv_offset;
cad->size = dmd.size;
cad->flags = dmd.flags;