+/*
+ * libcryptsetup - cryptsetup library
+ *
+ * Copyright (C) 2004, Christophe Saout <christophe@saout.de>
+ * Copyright (C) 2004-2007, Clemens Fruhwirth <clemens@endorphin.org>
+ * 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
+ * version 2 as published by the Free Software Foundation.
+ *
+ * This program 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 this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "libcryptsetup.h"
#include "luks.h"
+#include "loopaes.h"
+#include "verity.h"
+#include "tcrypt.h"
#include "internal.h"
struct crypt_device {
char *type;
- char *device;
- struct luks_masterkey *volume_key;
+ struct device *device;
+ struct device *metadata_device;
+
+ struct volume_key *volume_key;
uint64_t timeout;
uint64_t iteration_time;
int tries;
int password_verify;
+ int rng_type;
/* used in CRYPT_LUKS1 */
struct luks_phdr hdr;
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_cipher;
+ char *loopaes_cipher_mode;
+ char *loopaes_uuid;
+ unsigned int loopaes_key_size;
+
+ /* used in CRYPT_VERITY */
+ struct crypt_params_verity verity_hdr;
+ char *verity_root_hash;
+ unsigned int verity_root_hash_size;
+ char *verity_uuid;
+
+ /* used in CRYPT_TCRYPT */
+ struct crypt_params_tcrypt tcrypt_params;
+ struct tcrypt_phdr tcrypt_hdr;
/* callbacks definitions */
- void (*log)(int class, const char *msg, void *usrptr);
+ void (*log)(int level, const char *msg, void *usrptr);
void *log_usrptr;
int (*confirm)(const char *msg, void *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 class, const char *msg, void *usrptr) = NULL;
+static void (*_default_log)(int level, const char *msg, void *usrptr) = NULL;
static int _debug_level = 0;
void crypt_set_debug_level(int level)
_debug_level = level;
}
-int crypt_get_debug_level()
+int crypt_get_debug_level(void)
{
return _debug_level;
}
-void crypt_log(struct crypt_device *cd, int class, const char *msg)
+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(class, msg, cd->log_usrptr);
+ cd->log(level, msg, cd->log_usrptr);
else if (_default_log)
- _default_log(class, msg, NULL);
+ _default_log(level, msg, NULL);
+
+ if (level == CRYPT_LOG_ERROR)
+ crypt_set_error(cd, msg);
}
-void logger(struct crypt_device *cd, int class, const char *file,
+__attribute__((format(printf, 5, 6)))
+void logger(struct crypt_device *cd, int level, const char *file,
int line, const char *format, ...)
{
va_list argp;
va_start(argp, format);
- if (vasprintf(&target, format, argp) > 0) {
- if (class >= 0) {
- crypt_log(cd, class, target);
+ if (vasprintf(&target, format, argp) > 0 ) {
+ if (level >= 0) {
+ crypt_log(cd, level, target);
#ifdef CRYPT_DEBUG
} else if (_debug_level)
printf("# %s:%d %s\n", file ?: "?", line, target);
free(target);
}
-/*
- * Password processing behaviour matrix of process_key
- *
- * from binary file: check if there is sufficently large key material
- * interactive & from fd: hash if requested, otherwise crop or pad with '0'
- */
-static char *process_key(struct crypt_device *cd, const char *hash_name,
- const char *key_file, size_t key_size,
- const char *pass, size_t passLen)
-{
- char *key = safe_alloc(key_size);
- memset(key, 0, key_size);
-
- /* key is coming from binary file */
- if (key_file && strcmp(key_file, "-")) {
- if(passLen < key_size) {
- log_err(cd, _("Cannot not read %d bytes from key file %s.\n"),
- key_size, key_file);
- safe_free(key);
- return NULL;
- }
- memcpy(key, pass, key_size);
- return key;
- }
+static const char *mdata_device_path(struct crypt_device *cd)
+{
+ return device_path(cd->metadata_device ?: cd->device);
+}
- /* key is coming from tty, fd or binary stdin */
- if (hash_name) {
- if (hash(NULL, hash_name, key, key_size, pass, passLen) < 0) {
- log_err(cd, _("Key processing error.\n"));
- safe_free(key);
- return NULL;
- }
- } else if (passLen > key_size) {
- memcpy(key, pass, key_size);
- } else {
- memcpy(key, pass, passLen);
- }
+/* internal only */
+struct device *crypt_metadata_device(struct crypt_device *cd)
+{
+ return cd->metadata_device ?: cd->device;
+}
- return key;
+struct device *crypt_data_device(struct crypt_device *cd)
+{
+ return cd->device;
}
-int parse_into_name_and_mode(const char *nameAndMode, char *name, char *mode)
+int init_crypto(struct crypt_device *ctx)
{
-/* Token content stringification, see info cpp/stringification */
-#define str(s) #s
-#define xstr(s) str(s)
-#define scanpattern1 "%" xstr(LUKS_CIPHERNAME_L) "[^-]-%" xstr(LUKS_CIPHERMODE_L) "s"
-#define scanpattern2 "%" xstr(LUKS_CIPHERNAME_L) "[^-]"
+ int r;
+
+ crypt_fips_libcryptsetup_check(ctx);
+
+ r = crypt_random_init(ctx);
+ if (r < 0) {
+ log_err(ctx, _("Cannot initialize crypto RNG backend.\n"));
+ return r;
+ }
+
+ r = crypt_backend_init(ctx);
+ if (r < 0)
+ log_err(ctx, _("Cannot initialize crypto backend.\n"));
+ log_dbg("Crypto backend (%s) initialized.", crypt_backend_version());
+ return r;
+}
+
+static int process_key(struct crypt_device *cd, const char *hash_name,
+ size_t key_size, const char *pass, size_t passLen,
+ struct volume_key **vk)
+{
int r;
- if(sscanf(nameAndMode,scanpattern1, name, mode) != 2) {
- if((r = sscanf(nameAndMode,scanpattern2,name)) == 1)
- strncpy(mode,"cbc-plain",10);
- else
+ if (!key_size)
+ return -EINVAL;
+
+ *vk = crypt_alloc_volume_key(key_size, NULL);
+ if (!*vk)
+ return -ENOMEM;
+
+ if (hash_name) {
+ r = crypt_plain_hash(cd, hash_name, (*vk)->key, key_size, pass, passLen);
+ if (r < 0) {
+ if (r == -ENOENT)
+ log_err(cd, _("Hash algorithm %s not supported.\n"),
+ hash_name);
+ else
+ log_err(cd, _("Key processing error (using hash %s).\n"),
+ hash_name);
+ crypt_free_volume_key(*vk);
+ *vk = NULL;
return -EINVAL;
+ }
+ } else if (passLen > key_size) {
+ memcpy((*vk)->key, pass, key_size);
+ } else {
+ memcpy((*vk)->key, pass, passLen);
}
return 0;
-
-#undef scanpattern1
-#undef scanpattern2
-#undef str
-#undef xstr
}
static int isPLAIN(const char *type)
return (type && !strcmp(CRYPT_LUKS1, type));
}
+static int isLOOPAES(const char *type)
+{
+ return (type && !strcmp(CRYPT_LOOPAES, type));
+}
+
+static int isVERITY(const char *type)
+{
+ return (type && !strcmp(CRYPT_VERITY, type));
+}
+
+static int isTCRYPT(const char *type)
+{
+ return (type && !strcmp(CRYPT_TCRYPT, type));
+}
+
/* keyslot helpers */
static int keyslot_verify_or_find_empty(struct crypt_device *cd, int *keyslot)
{
return 0;
}
-static int verify_other_keyslot(struct crypt_device *cd,
- const char *key_file,
- unsigned int flags,
- int keyIndex)
+/*
+ * compares UUIDs returned by device-mapper (striped by cryptsetup) and uuid in header
+ */
+static int crypt_uuid_cmp(const char *dm_uuid, const char *hdr_uuid)
{
- struct luks_masterkey *mk;
- crypt_keyslot_info ki;
- int openedIndex;
- char *password = NULL;
- unsigned int passwordLen;
+ int i, j;
+ char *str;
- get_key(_("Enter any remaining LUKS passphrase: "), &password,
- &passwordLen, 0, key_file, cd->timeout, flags, cd);
- if(!password)
+ if (!dm_uuid || !hdr_uuid)
return -EINVAL;
- ki = crypt_keyslot_status(cd, keyIndex);
- if (ki == CRYPT_SLOT_ACTIVE) /* Not last slot */
- LUKS_keyslot_set(&cd->hdr, keyIndex, 0);
-
- openedIndex = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT,
- password, passwordLen,
- &cd->hdr, &mk, cd);
-
- if (ki == CRYPT_SLOT_ACTIVE)
- LUKS_keyslot_set(&cd->hdr, keyIndex, 1);
- LUKS_dealloc_masterkey(mk);
- safe_free(password);
-
- if (openedIndex < 0)
- return -EPERM;
-
- log_std(cd, _("Key slot %d verified.\n"), openedIndex);
- return 0;
-}
-
-static int find_keyslot_by_passphrase(struct crypt_device *cd,
- const char *key_file,
- unsigned int flags,
- char *message)
-{
- struct luks_masterkey *mk;
- char *password = NULL;
- unsigned int passwordLen;
- int keyIndex;
-
- get_key(message,&password,&passwordLen, 0, key_file,
- cd->timeout, flags, cd);
- if(!password)
+ str = strchr(dm_uuid, '-');
+ if (!str)
return -EINVAL;
- keyIndex = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
- passwordLen, &cd->hdr, &mk, cd);
- LUKS_dealloc_masterkey(mk);
- safe_free(password);
-
- return keyIndex;
-}
-
-static int device_check_and_adjust(struct crypt_device *cd,
- const char *device,
- uint64_t *size, uint64_t *offset,
- int *read_only)
-{
- struct device_infos infos;
+ for (i = 0, j = 1; hdr_uuid[i]; i++) {
+ if (hdr_uuid[i] == '-')
+ continue;
- if (get_device_infos(device, &infos, cd) < 0) {
- log_err(cd, _("Cannot get info about device %s.\n"), device);
- return -ENOTBLK;
- }
+ if (!str[j] || str[j] == '-')
+ return -EINVAL;
- if (!*size) {
- *size = infos.size;
- if (!*size) {
- log_err(cd, _("Device %s has zero size.\n"), device);
- return -ENOTBLK;
- }
- if (*size < *offset) {
- log_err(cd, _("Device %s is too small.\n"), device);
+ if (str[j] != hdr_uuid[i])
return -EINVAL;
- }
- *size -= *offset;
+ j++;
}
- if (infos.readonly)
- *read_only = 1;
-
- log_dbg("Calculated device size is %" PRIu64 " sectors (%s), offset %" PRIu64 ".",
- *size, *read_only ? "RO" : "RW", *offset);
return 0;
}
-static int luks_remove_helper(struct crypt_device *cd,
- int key_slot,
- const char *other_key_file,
- const char *key_file,
- int verify)
+int PLAIN_activate(struct crypt_device *cd,
+ const char *name,
+ struct volume_key *vk,
+ uint64_t size,
+ uint32_t flags)
{
- crypt_keyslot_info ki;
- int r = -EINVAL;
-
- if (key_slot == CRYPT_ANY_SLOT) {
- key_slot = find_keyslot_by_passphrase(cd, key_file, 0,
- _("Enter LUKS passphrase to be deleted: "));
- if(key_slot < 0) {
- r = -EPERM;
- goto out;
+ int r;
+ char *dm_cipher = NULL;
+ enum devcheck device_check;
+ struct crypt_dm_active_device dmd = {
+ .target = DM_CRYPT,
+ .uuid = crypt_get_uuid(cd),
+ .size = size,
+ .flags = flags,
+ .data_device = crypt_data_device(cd),
+ .u.crypt = {
+ .cipher = NULL,
+ .vk = vk,
+ .offset = crypt_get_data_offset(cd),
+ .iv_offset = crypt_get_iv_offset(cd),
}
+ };
- log_std(cd, _("key slot %d selected for deletion.\n"), key_slot);
- }
-
- ki = crypt_keyslot_status(cd, key_slot);
- if (ki == CRYPT_SLOT_INVALID) {
- log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
- key_slot, LUKS_NUMKEYS - 1);
- r = -EINVAL;
- goto out;
- }
- if (ki <= CRYPT_SLOT_INACTIVE) {
- log_err(cd, _("Key %d not active. Can't wipe.\n"), key_slot);
- r = -EINVAL;
- goto out;
- }
-
- if (ki == CRYPT_SLOT_ACTIVE_LAST && cd->confirm &&
- !(cd->confirm(_("This is the last keyslot."
- " Device will become unusable after purging this key."),
- cd->confirm_usrptr))) {
- r = -EINVAL;
- goto out;
- }
-
- if(verify)
- r = verify_other_keyslot(cd, other_key_file, 0, key_slot);
+ if (dmd.flags & CRYPT_ACTIVATE_SHARED)
+ device_check = DEV_SHARED;
else
- r = 0;
-
- if (!r)
- r = crypt_keyslot_destroy(cd, key_slot);
-out:
- return (r < 0) ? r : 0;
-}
-
-static int create_device_helper(struct crypt_device *cd,
- const char *name,
- const char *hash,
- const char *cipher,
- const char *cipher_mode,
- const char *key_file,
- const char *key,
- unsigned int keyLen,
- int key_size,
- uint64_t size,
- uint64_t skip,
- uint64_t offset,
- const char *uuid,
- int read_only,
- unsigned int flags,
- int reload)
-{
- crypt_status_info ci;
- char *dm_cipher = NULL;
- char *processed_key = NULL;
- int r;
-
- ci = crypt_status(cd, name);
- if (ci == CRYPT_INVALID)
- return -EINVAL;
-
- if (reload && ci < CRYPT_ACTIVE)
- return -EINVAL;
-
- if (!reload && ci >= CRYPT_ACTIVE) {
- log_err(cd, _("Device %s already exists.\n"), name);
- return -EEXIST;
- }
-
- if (key_size < 0 || key_size > 1024) {
- log_err(cd, _("Invalid key size %d.\n"), key_size);
- return -EINVAL;
- }
+ device_check = DEV_EXCL;
- r = device_check_and_adjust(cd, cd->device, &size, &offset, &read_only);
+ r = device_block_adjust(cd, dmd.data_device, device_check,
+ dmd.u.crypt.offset, &dmd.size, &dmd.flags);
if (r)
return r;
- if (cipher_mode && asprintf(&dm_cipher, "%s-%s", cipher, cipher_mode) < 0)
+ if (crypt_get_cipher_mode(cd))
+ r = asprintf(&dm_cipher, "%s-%s", crypt_get_cipher(cd), crypt_get_cipher_mode(cd));
+ else
+ r = asprintf(&dm_cipher, "%s", crypt_get_cipher(cd));
+ if (r < 0)
return -ENOMEM;
- processed_key = process_key(cd, hash, key_file, key_size, key, keyLen);
- if (!processed_key)
- return -ENOENT;
-
- r = dm_create_device(name, cd->device, dm_cipher ?: cipher, cd->type, uuid, size, skip, offset,
- key_size, processed_key, read_only, reload);
-
- free(dm_cipher);
- safe_free(processed_key);
- return r;
-}
-
-static int open_from_hdr_and_mk(struct crypt_device *cd,
- struct luks_masterkey *mk,
- const char *name,
- uint32_t flags)
-{
- uint64_t size, offset;
- char *cipher;
- int read_only, no_uuid, r;
+ dmd.u.crypt.cipher = dm_cipher;
+ log_dbg("Trying to activate PLAIN device %s using cipher %s.",
+ name, dmd.u.crypt.cipher);
- size = 0;
- offset = crypt_get_data_offset(cd);
- read_only = flags & CRYPT_ACTIVATE_READONLY;
- no_uuid = flags & CRYPT_ACTIVATE_NO_UUID;
+ r = dm_create_device(cd, name, CRYPT_PLAIN, &dmd, 0);
- r = device_check_and_adjust(cd, cd->device, &size, &offset, &read_only);
- if (r)
- return r;
+ // FIXME
+ if (!cd->plain_uuid && dm_query_device(cd, name, DM_ACTIVE_UUID, &dmd) >= 0)
+ cd->plain_uuid = CONST_CAST(char*)dmd.uuid;
- if (asprintf(&cipher, "%s-%s", crypt_get_cipher(cd),
- crypt_get_cipher_mode(cd)) < 0)
- r = -ENOMEM;
- else
- r = dm_create_device(name, cd->device, cipher, cd->type,
- no_uuid ? NULL : crypt_get_uuid(cd),
- size, 0, offset, mk->keyLength, mk->key,
- read_only, 0);
- free(cipher);
+ free(dm_cipher);
return r;
}
-static void log_wrapper(int class, const char *msg, void *usrptr)
-{
- void (*xlog)(int class, char *msg) = usrptr;
- xlog(class, (char *)msg);
-}
-
-static int yesDialog_wrapper(const char *msg, void *usrptr)
-{
- int (*xyesDialog)(char *msg) = usrptr;
- return xyesDialog((char*)msg);
-}
-
int crypt_confirm(struct crypt_device *cd, const char *msg)
{
if (!cd || !cd->confirm)
return cd->confirm(msg, cd->confirm_usrptr);
}
-static void key_from_terminal(struct crypt_device *cd, char *msg, char **key,
- unsigned int *key_len, int force_verify)
+static int key_from_terminal(struct crypt_device *cd, char *msg, char **key,
+ size_t *key_len, int force_verify)
{
- int r, flags = 0;
+ char *prompt = NULL, *device_name;
+ int r;
+
+ *key = NULL;
+ if(!msg) {
+ if (crypt_loop_device(crypt_get_device_name(cd)))
+ device_name = crypt_loop_backing_file(crypt_get_device_name(cd));
+ else
+ device_name = strdup(crypt_get_device_name(cd));
+ if (!device_name)
+ return -ENOMEM;
+ r = asprintf(&prompt, _("Enter passphrase for %s: "), device_name);
+ free(device_name);
+ if (r < 0)
+ return -ENOMEM;
+ msg = prompt;
+ }
if (cd->password) {
- *key = safe_alloc(MAX_TTY_PASSWORD_LEN);
- if (*key)
- return;
- r = cd->password(msg, *key, (size_t)key_len, cd->password_usrptr);
+ *key = crypt_safe_alloc(DEFAULT_PASSPHRASE_SIZE_MAX);
+ if (!*key) {
+ r = -ENOMEM;
+ goto out;
+ }
+ r = cd->password(msg, *key, DEFAULT_PASSPHRASE_SIZE_MAX,
+ cd->password_usrptr);
if (r < 0) {
- safe_free(*key);
+ crypt_safe_free(*key);
*key = NULL;
} else
*key_len = r;
- } else {
- if (force_verify || cd->password_verify)
- flags |= CRYPT_FLAG_VERIFY_IF_POSSIBLE;
- get_key(msg, key, key_len, 0, NULL, cd->timeout, flags, cd);
- }
+ } else
+ r = crypt_get_key(msg, key, key_len, 0, 0, NULL, cd->timeout,
+ (force_verify || cd->password_verify), cd);
+out:
+ free(prompt);
+ return (r < 0) ? r: 0;
}
static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslot,
- struct luks_masterkey **mk)
+ struct volume_key **vk)
{
- char *prompt = NULL, *passphrase_read = NULL;
- unsigned int passphrase_size_read;
- int r = -EINVAL, tries = cd->tries;
-
- if(asprintf(&prompt, _("Enter passphrase for %s: "), cd->device) < 0)
- return -ENOMEM;
+ char *passphrase_read = NULL;
+ size_t passphrase_size_read;
+ int r = -EINVAL, eperm = 0, tries = cd->tries;
- *mk = NULL;
+ *vk = NULL;
do {
- if (*mk)
- LUKS_dealloc_masterkey(*mk);
- *mk = NULL;
-
- key_from_terminal(cd, prompt, &passphrase_read,
- &passphrase_size_read, 0);
- if(!passphrase_read) {
- r = -EINVAL;
- break;
- }
+ crypt_free_volume_key(*vk);
+ *vk = NULL;
+
+ 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;
- r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
- passphrase_size_read, &cd->hdr, mk, cd);
- safe_free(passphrase_read);
+ r = LUKS_open_key_with_hdr(keyslot, passphrase_read,
+ passphrase_size_read, &cd->hdr, vk, cd);
+ if (r == -EPERM)
+ eperm = 1;
+ crypt_safe_free(passphrase_read);
passphrase_read = NULL;
} while (r == -EPERM && (--tries > 0));
+out:
+ if (r < 0) {
+ crypt_free_volume_key(*vk);
+ *vk = NULL;
- if (r < 0 && *mk) {
- LUKS_dealloc_masterkey(*mk);
- *mk = NULL;
+ /* Report wrong passphrase if at least one try failed */
+ if (eperm && r == -EPIPE)
+ r = -EPERM;
}
- free(prompt);
+ crypt_safe_free(passphrase_read);
return r;
-
-}
-
-static void key_from_file(struct crypt_device *cd, char *msg,
- char **key, unsigned int *key_len,
- const char *key_file, size_t key_size)
-{
- get_key(msg, key, key_len, key_size, key_file, cd->timeout, 0, cd);
}
-static int _crypt_init(struct crypt_device **cd,
- const char *type,
- struct crypt_options *options,
- int load, int need_dm)
+static int key_from_file(struct crypt_device *cd, char *msg,
+ char **key, size_t *key_len,
+ const char *key_file, size_t key_offset,
+ size_t key_size)
{
- int init_by_name, r;
-
- /* if it is plain device and mapping table is being reloaded
- initialize it by name*/
- init_by_name = (type && !strcmp(type, CRYPT_PLAIN) && load);
-
- /* Some of old API calls do not require DM in kernel,
- fake initialisation by initialise it with kernel_check disabled */
- if (!need_dm)
- (void)dm_init(NULL, 0);
- if (init_by_name)
- r = crypt_init_by_name(cd, options->name);
- else
- r = crypt_init(cd, options->device);
- if (!need_dm)
- dm_exit();
-
- if (r)
- return -EINVAL;
-
- crypt_set_log_callback(*cd, log_wrapper, options->icb->log);
- crypt_set_confirm_callback(*cd, yesDialog_wrapper, options->icb->yesDialog);
-
- crypt_set_timeout(*cd, options->timeout);
- crypt_set_password_retry(*cd, options->tries);
- crypt_set_iterarion_time(*cd, options->iteration_time ?: 1000);
- crypt_set_password_verify(*cd, options->flags & CRYPT_FLAG_VERIFY);
-
- if (load && !init_by_name)
- r = crypt_load(*cd, type, NULL);
-
- if (!r && type && !(*cd)->type) {
- (*cd)->type = strdup(type);
- if (!(*cd)->type)
- r = -ENOMEM;
- }
-
- if (r)
- crypt_free(*cd);
-
- return r;
+ return crypt_get_key(msg, key, key_len, key_offset, key_size, key_file,
+ cd->timeout, 0, cd);
}
void crypt_set_log_callback(struct crypt_device *cd,
- void (*log)(int class, const char *msg, void *usrptr),
+ void (*log)(int level, const char *msg, void *usrptr),
void *usrptr)
{
if (!cd)
cd->password_usrptr = usrptr;
}
-/* OPTIONS: name, cipher, device, hash, key_file, key_size, key_slot,
- * offset, size, skip, timeout, tries, passphrase_fd (ignored),
- * flags, icb */
-int crypt_create_device(struct crypt_options *options)
+static void _get_error(char *error, char *buf, size_t size)
{
- struct crypt_device *cd = NULL;
- char *key = NULL;
- unsigned int keyLen;
- int r;
-
- r = _crypt_init(&cd, CRYPT_PLAIN, options, 0, 1);
- if (r)
- return r;
-
- get_key(_("Enter passphrase: "), &key, &keyLen, options->key_size,
- options->key_file, cd->timeout, options->flags, cd);
- if (!key)
- r = -ENOENT;
- else
- r = create_device_helper(cd, options->name, options->hash,
- options->cipher, NULL, options->key_file, key, keyLen,
- options->key_size, options->size, options->skip,
- options->offset, NULL, options->flags & CRYPT_FLAG_READONLY,
- options->flags, 0);
-
- safe_free(key);
- crypt_free(cd);
- return r;
+ if (!buf || size < 1)
+ error[0] = '\0';
+ else if (*error) {
+ strncpy(buf, error, size - 1);
+ buf[size - 1] = '\0';
+ error[0] = '\0';
+ } else
+ buf[0] = '\0';
}
-/* OPTIONS: same as create above */
-int crypt_update_device(struct crypt_options *options)
+void crypt_last_error(struct crypt_device *cd, char *buf, size_t size)
{
- struct crypt_device *cd = NULL;
- char *key = NULL;
- unsigned int keyLen;
- int r;
-
- r = _crypt_init(&cd, CRYPT_PLAIN, options, 1, 1);
- if (r)
- return r;
+ if (cd)
+ return _get_error(cd->error, buf, size);
+}
- get_key(_("Enter passphrase: "), &key, &keyLen, options->key_size,
- options->key_file, cd->timeout, options->flags, cd);
- if (!key)
- r = -ENOENT;
- else
- r = create_device_helper(cd, options->name, options->hash,
- options->cipher, NULL, options->key_file, key, keyLen,
- options->key_size, options->size, options->skip,
- options->offset, NULL, options->flags & CRYPT_FLAG_READONLY,
- options->flags, 1);
-
- safe_free(key);
- crypt_free(cd);
- return r;
+/* Deprecated global error interface */
+void crypt_get_error(char *buf, size_t size)
+{
+ return _get_error(global_error, buf, size);
}
-/* OPTIONS: name, size, icb */
-int crypt_resize_device(struct crypt_options *options)
+const char *crypt_get_dir(void)
{
- struct crypt_device *cd = NULL;
- char *device = NULL, *cipher = NULL, *uuid = NULL, *key = NULL;
- char *type = NULL;
- uint64_t size, skip, offset;
- int key_size, read_only, r;
+ return dm_get_dir();
+}
- log_dbg("Resizing device %s to %" PRIu64 " sectors.", options->name, options->size);
+int crypt_init(struct crypt_device **cd, const char *device)
+{
+ struct crypt_device *h = NULL;
+ int r;
- if (dm_init(NULL, 1) < 0)
- return -ENOSYS;
+ if (!cd)
+ return -EINVAL;
- r = dm_query_device(options->name, &device, &size, &skip, &offset,
- &cipher, &key_size, &key, &read_only, NULL, &uuid);
- if (r < 0) {
- log_err(NULL, _("Device %s is not active.\n"), options->name);
- goto out;
- }
+ log_dbg("Allocating crypt device %s context.", device);
- /* Try to determine type of device from UUID */
- type = CRYPT_PLAIN;
- if (uuid) {
- if (!strncmp(uuid, CRYPT_PLAIN, strlen(CRYPT_PLAIN))) {
- type = CRYPT_PLAIN;
- free (uuid);
- uuid = NULL;
- } else if (!strncmp(uuid, CRYPT_LUKS1, strlen(CRYPT_LUKS1)))
- type = CRYPT_LUKS1;
- }
+ if (!(h = malloc(sizeof(struct crypt_device))))
+ return -ENOMEM;
- if (!options->device)
- options->device = device;
+ memset(h, 0, sizeof(*h));
- r = _crypt_init(&cd, type, options, 1, 1);
- if (r)
- goto out;
+ r = device_alloc(&h->device, device);
+ if (r < 0)
+ goto bad;
- size = options->size;
- r = device_check_and_adjust(cd, device, &size, &offset, &read_only);
- if (r)
- goto out;
+ dm_backend_init();
- r = dm_create_device(options->name, device, cipher, type,
- crypt_get_uuid(cd), size, skip, offset,
- key_size, key, read_only, 1);
-out:
- safe_free(key);
- free(cipher);
- if (options->device == device)
- options->device = NULL;
- free(device);
- free(uuid);
- crypt_free(cd);
- dm_exit();
+ h->iteration_time = 1000;
+ h->password_verify = 0;
+ h->tries = 3;
+ h->rng_type = crypt_random_default_key_rng();
+ *cd = h;
+ return 0;
+bad:
+ device_free(h->device);
+ free(h);
return r;
}
-/* OPTIONS: name, icb */
-int crypt_query_device(struct crypt_options *options)
+static int crypt_check_data_device_size(struct crypt_device *cd)
{
- int read_only, r;
-
- log_dbg("Query device %s.", options->name);
-
- if (dm_init(NULL, 1) < 0)
- return -ENOSYS;
-
- r = dm_status_device(options->name);
- if (r == -ENODEV) {
- dm_exit();
- return 0;
- }
+ int r;
+ uint64_t size, size_min;
- r = dm_query_device(options->name, (char **)&options->device, &options->size,
- &options->skip, &options->offset, (char **)&options->cipher,
- &options->key_size, NULL, &read_only, NULL, NULL);
+ /* Check data device size, require at least one sector */
+ size_min = crypt_get_data_offset(cd) << SECTOR_SHIFT ?: SECTOR_SIZE;
- dm_exit();
+ r = device_size(cd->device, &size);
if (r < 0)
return r;
- if (read_only)
- options->flags |= CRYPT_FLAG_READONLY;
-
- options->flags |= CRYPT_FLAG_FREE_DEVICE;
- options->flags |= CRYPT_FLAG_FREE_CIPHER;
+ if (size < size_min) {
+ log_err(cd, _("Header detected but device %s is too small.\n"),
+ device_path(cd->device));
+ return -EINVAL;
+ }
- return 1;
+ return r;
}
-/* OPTIONS: name, icb */
-int crypt_remove_device(struct crypt_options *options)
+int crypt_set_data_device(struct crypt_device *cd, const char *device)
{
- struct crypt_device *cd = NULL;
+ struct device *dev = NULL;
int r;
- r = crypt_init_by_name(&cd, options->name);
- if (r)
- return r;
+ log_dbg("Setting ciphertext data device to %s.", device ?: "(none)");
- r = crypt_deactivate(cd, options->name);
-
- crypt_free(cd);
- return r;
-
-}
-
-/* OPTIONS: device, cipher, hash, align_payload, key_size (master key), key_slot
- * new_key_file, iteration_time, timeout, flags, icb */
-int crypt_luksFormat(struct crypt_options *options)
-{
- char cipherName[LUKS_CIPHERNAME_L];
- char cipherMode[LUKS_CIPHERMODE_L];
- char *password=NULL;
- unsigned int passwordLen;
- struct crypt_device *cd;
- struct crypt_params_luks1 cp = {
- .hash = options->hash,
- .data_alignment = options->align_payload
- };
- int r;
-
- r = parse_into_name_and_mode(options->cipher, cipherName, cipherMode);
- if(r < 0) {
- log_err(cd, _("No known cipher specification pattern detected.\n"));
- return r;
- }
-
- if ((r = _crypt_init(&cd, CRYPT_LUKS1, options, 0, 1)))
- return r;
-
- if (options->key_slot >= LUKS_NUMKEYS && options->key_slot != CRYPT_ANY_SLOT) {
- log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
- options->key_slot, LUKS_NUMKEYS - 1);
- r = -EINVAL;
- goto out;
- }
-
- get_key(_("Enter LUKS passphrase: "), &password, &passwordLen, 0,
- options->new_key_file, options->timeout, options->flags, cd);
-
- if(!password) {
- r = -EINVAL;
- goto out;
+ if (!isLUKS(cd->type) && !isVERITY(cd->type)) {
+ log_err(cd, _("This operation is not supported for this device type.\n"));
+ return -EINVAL;
}
- r = crypt_format(cd, CRYPT_LUKS1, cipherName, cipherMode,
- NULL, NULL, options->key_size, &cp);
- if (r < 0)
- goto out;
-
- /* Add keyslot using internally stored volume key generated during format */
- r = crypt_keyslot_add_by_volume_key(cd, options->key_slot, NULL, 0,
- password, passwordLen);
-out:
- crypt_free(cd);
- safe_free(password);
- return (r < 0) ? r : 0;
-}
-
-/* OPTIONS: name, device, key_size, key_file, timeout, tries, flags, icb */
-int crypt_luksOpen(struct crypt_options *options)
-{
- struct crypt_device *cd = NULL;
- uint32_t flags = 0;
- int r;
-
- if (!options->name)
+ /* metadata device must be set */
+ if (!cd->device || !device)
return -EINVAL;
- r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
- if (r)
+ r = device_alloc(&dev, device);
+ if (r < 0)
return r;
- if (options->flags & CRYPT_FLAG_READONLY)
- flags |= CRYPT_ACTIVATE_READONLY;
-
- if (options->flags & CRYPT_FLAG_NON_EXCLUSIVE_ACCESS)
- flags |= CRYPT_ACTIVATE_NO_UUID;
+ if (!cd->metadata_device) {
+ cd->metadata_device = cd->device;
+ } else
+ device_free(cd->device);
- if (options->key_file)
- r = crypt_activate_by_keyfile(cd, options->name,
- CRYPT_ANY_SLOT, options->key_file, options->key_size,
- flags);
- else
- r = crypt_activate_by_passphrase(cd, options->name,
- CRYPT_ANY_SLOT, options->passphrase,
- options->passphrase ? strlen(options->passphrase) : 0,
- flags);
+ cd->device = dev;
- crypt_free(cd);
- return (r < 0) ? r : 0;
+ return crypt_check_data_device_size(cd);
}
-/* OPTIONS: device, keys_slot, key_file, timeout, flags, icb */
-int crypt_luksKillSlot(struct crypt_options *options)
+static int _crypt_load_luks1(struct crypt_device *cd, int require_header, int repair)
{
- struct crypt_device *cd = NULL;
+ struct luks_phdr hdr;
int r;
- r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
- if (r)
+ r = init_crypto(cd);
+ if (r < 0)
return r;
- r = luks_remove_helper(cd, options->key_slot, options->key_file, NULL,
- options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
+ r = LUKS_read_phdr(&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));
- crypt_free(cd);
- return (r < 0) ? r : 0;
+ return r;
}
-/* OPTIONS: device, new_key_file, key_file, timeout, flags, icb */
-int crypt_luksRemoveKey(struct crypt_options *options)
+static int _crypt_load_tcrypt(struct crypt_device *cd, struct crypt_params_tcrypt *params)
{
- struct crypt_device *cd = NULL;
int r;
- r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
- if (r)
+ r = init_crypto(cd);
+ if (r < 0)
return r;
- r = luks_remove_helper(cd, CRYPT_ANY_SLOT, options->key_file, options->new_key_file,
- options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
+ memcpy(&cd->tcrypt_params, params, sizeof(*params));
- crypt_free(cd);
- return (r < 0) ? r : 0;
-}
+ r = TCRYPT_read_phdr(cd, &cd->tcrypt_hdr, &cd->tcrypt_params);
+ cd->tcrypt_params.passphrase = NULL;
+ cd->tcrypt_params.passphrase_size = 0;
+ cd->tcrypt_params.keyfiles = NULL;
+ cd->tcrypt_params.keyfiles_count = 0;
-/* OPTIONS: device, new_key_file, key_file, key_slot, flags,
- iteration_time, timeout, icb */
-int crypt_luksAddKey(struct crypt_options *options)
-{
- struct crypt_device *cd = NULL;
- int r = -EINVAL;
-
- r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
- if (r)
+ if (r < 0)
return r;
- if (options->key_file || options->new_key_file)
- r = crypt_keyslot_add_by_keyfile(cd, options->key_slot,
- options->key_file, 0,
- options->new_key_file, 0);
- else
- r = crypt_keyslot_add_by_passphrase(cd, options->key_slot,
- NULL, 0, NULL, 0);
+ if (!cd->type && !(cd->type = strdup(CRYPT_TCRYPT)))
+ return -ENOMEM;
- crypt_free(cd);
- return (r < 0) ? r : 0;
+ return r;
}
-/* OPTIONS: device, icb */
-int crypt_luksUUID(struct crypt_options *options)
+static int _crypt_load_verity(struct crypt_device *cd, struct crypt_params_verity *params)
{
- struct crypt_device *cd = NULL;
- char *uuid;
int r;
+ size_t sb_offset = 0;
- r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
- if (r)
+ r = init_crypto(cd);
+ if (r < 0)
return r;
- uuid = (char *)crypt_get_uuid(cd);
- log_std(cd, uuid ?: "");
- log_std(cd, "\n");
- crypt_free(cd);
- return 0;
-}
-
-/* OPTIONS: device, icb */
-int crypt_isLuks(struct crypt_options *options)
-{
- struct crypt_device *cd = NULL;
- int r;
+ if (params && params->flags & CRYPT_VERITY_NO_HEADER)
+ return -EINVAL;
- log_dbg("Check device %s for LUKS header.", options->device);
+ if (params)
+ sb_offset = params->hash_area_offset;
- r = crypt_init(&cd, options->device);
+ r = VERITY_read_sb(cd, sb_offset, &cd->verity_uuid, &cd->verity_hdr);
if (r < 0)
- return -EINVAL;
+ return r;
- /* Do print fail here, no need to crypt_load() */
- r = LUKS_read_phdr(cd->device, &cd->hdr, 0, cd) ? -EINVAL : 0;
+ if (params)
+ cd->verity_hdr.flags = params->flags;
- crypt_free(cd);
- return r;
-}
+ /* Hash availability checked in sb load */
+ cd->verity_root_hash_size = crypt_hash_size(cd->verity_hdr.hash_name);
+ if (cd->verity_root_hash_size > 4096)
+ return -EINVAL;
-/* OPTIONS: device, icb */
-int crypt_luksDump(struct crypt_options *options)
-{
- struct crypt_device *cd = NULL;
- int r;
+ if (!cd->type && !(cd->type = strdup(CRYPT_VERITY)))
+ return -ENOMEM;
- r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
- if(r < 0)
+ if (params && params->data_device &&
+ (r = crypt_set_data_device(cd, params->data_device)) < 0)
return r;
- r = crypt_dump(cd);
-
- crypt_free(cd);
- return 0;
+ return r;
}
-void crypt_get_error(char *buf, size_t size)
+static int _init_by_name_crypt(struct crypt_device *cd, const char *name)
{
- const char *error = get_error();
+ struct crypt_dm_active_device dmd = {};
+ char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
+ int key_nums, r;
- if (!buf || size < 1)
- set_error(NULL);
- else if (error) {
- strncpy(buf, error, size - 1);
- buf[size - 1] = '\0';
- set_error(NULL);
- } else
- buf[0] = '\0';
-}
+ r = dm_query_device(cd, name,
+ DM_ACTIVE_DEVICE |
+ DM_ACTIVE_UUID |
+ DM_ACTIVE_CRYPT_CIPHER |
+ DM_ACTIVE_CRYPT_KEYSIZE, &dmd);
+ if (r < 0)
+ goto out;
-void crypt_put_options(struct crypt_options *options)
-{
- if (options->flags & CRYPT_FLAG_FREE_DEVICE) {
- free((char *)options->device);
- options->device = NULL;
- options->flags &= ~CRYPT_FLAG_FREE_DEVICE;
- }
- if (options->flags & CRYPT_FLAG_FREE_CIPHER) {
- free((char *)options->cipher);
- options->cipher = NULL;
- options->flags &= ~CRYPT_FLAG_FREE_CIPHER;
+ 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 (crypt_metadata_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);
+ device_free(dmd.data_device);
+ free(CONST_CAST(void*)dmd.u.crypt.cipher);
+ free(CONST_CAST(void*)dmd.uuid);
+ return r;
}
-const char *crypt_get_dir(void)
-{
- return dm_get_dir();
-}
-
-/////////////////////////////////
-//
-// New API
-//
-
-int crypt_init(struct crypt_device **cd, const char *device)
+static int _init_by_name_verity(struct crypt_device *cd, const char *name)
{
- struct crypt_device *h = NULL;
-
- if (!cd)
- return -EINVAL;
-
- log_dbg("Allocating crypt device %s context.", device);
-
- if (device && !device_ready(NULL, device, O_RDONLY))
- return -ENOTBLK;
-
- if (!(h = malloc(sizeof(struct crypt_device))))
- return -ENOMEM;
-
- memset(h, 0, sizeof(*h));
+ struct crypt_params_verity params = {};
+ struct crypt_dm_active_device dmd = {
+ .target = DM_VERITY,
+ .u.verity.vp = ¶ms,
+ };
+ int r;
- if (device) {
- h->device = strdup(device);
- if (!h->device) {
- free(h);
- return -ENOMEM;
- }
- } else
- h->device = NULL;
+ r = dm_query_device(cd, name,
+ DM_ACTIVE_DEVICE |
+ DM_ACTIVE_UUID |
+ DM_ACTIVE_VERITY_HASH_DEVICE |
+ DM_ACTIVE_VERITY_PARAMS, &dmd);
+ if (r < 0)
+ goto out;
- if (dm_init(h, 1) < 0) {
- free(h);
- return -ENOSYS;
+ 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;
+ cd->metadata_device = dmd.u.verity.hash_device;
}
-
- h->iteration_time = 1000;
- h->password_verify = 0;
- h->tries = 3;
- *cd = h;
- return 0;
+out:
+ device_free(dmd.data_device);
+ free(CONST_CAST(void*)dmd.uuid);
+ return r;
}
-int crypt_init_by_name(struct crypt_device **cd, const char *name)
+int crypt_init_by_name_and_header(struct crypt_device **cd,
+ const char *name,
+ const char *header_device)
{
crypt_status_info ci;
- char *device = NULL;
+ struct crypt_dm_active_device dmd;
int r;
log_dbg("Allocating crypt device context by device %s.", name);
return -ENODEV;
}
- r = dm_query_device(name, &device, NULL, NULL, NULL,
- NULL, NULL, NULL, NULL, NULL, NULL);
- if (r >= 0)
- r = crypt_init(cd, device);
+ r = dm_query_device(NULL, name, DM_ACTIVE_DEVICE | DM_ACTIVE_UUID, &dmd);
+ if (r < 0)
+ goto out;
+
+ *cd = NULL;
+
+ if (header_device) {
+ r = crypt_init(cd, header_device);
+ } else {
+ r = crypt_init(cd, device_path(dmd.data_device));
+
+ /* Underlying device disappeared but mapping still active */
+ 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) {
+ device_free(dmd.data_device);
+ dmd.data_device = NULL;
+ r = crypt_init(cd, NULL);
+ }
+ }
+
+ if (r < 0)
+ goto out;
+
+ if (dmd.uuid) {
+ if (!strncmp(CRYPT_PLAIN, dmd.uuid, sizeof(CRYPT_PLAIN)-1))
+ (*cd)->type = strdup(CRYPT_PLAIN);
+ else if (!strncmp(CRYPT_LOOPAES, dmd.uuid, sizeof(CRYPT_LOOPAES)-1))
+ (*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, device_path(dmd.data_device));
+ if (r < 0)
+ goto out;
+ }
+
+ /* Try to initialise basic parameters from active device */
- free(device);
+ 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;
+ }
+ device_free(dmd.data_device);
+ free(CONST_CAST(void*)dmd.uuid);
return r;
}
+int crypt_init_by_name(struct crypt_device **cd, const char *name)
+{
+ return crypt_init_by_name_and_header(cd, name, NULL);
+}
+
static int _crypt_format_plain(struct crypt_device *cd,
const char *cipher,
const char *cipher_mode,
const char *uuid,
+ size_t volume_key_size,
struct crypt_params_plain *params)
{
- if (!cipher || !cipher_mode || !params || !params->hash) {
+ if (!cipher || !cipher_mode) {
log_err(cd, _("Invalid plain crypt parameters.\n"));
return -EINVAL;
}
- if (cd->volume_key->keyLength > 1024) {
+ if (volume_key_size > 1024) {
log_err(cd, _("Invalid key size.\n"));
return -EINVAL;
}
+ if (!(cd->type = strdup(CRYPT_PLAIN)))
+ return -ENOMEM;
+
+ cd->plain_key_size = volume_key_size;
+ cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
+ if (!cd->volume_key)
+ return -ENOMEM;
+
cd->plain_cipher = strdup(cipher);
cd->plain_cipher_mode = strdup(cipher_mode);
if (uuid)
cd->plain_uuid = strdup(uuid);
- if (params->hash)
+ if (params && params->hash)
cd->plain_hdr.hash = strdup(params->hash);
- cd->plain_hdr.offset = params->offset;
- cd->plain_hdr.skip = params->skip;
+ cd->plain_hdr.offset = params ? params->offset : 0;
+ cd->plain_hdr.skip = params ? params->skip : 0;
+ cd->plain_hdr.size = params ? params->size : 0;
- if ((params->hash && !cd->plain_hdr.hash) ||
- !cd->plain_cipher || !cd->plain_cipher_mode)
+ if (!cd->plain_cipher || !cd->plain_cipher_mode)
return -ENOMEM;
return 0;
const char *cipher,
const char *cipher_mode,
const char *uuid,
+ const char *volume_key,
+ size_t volume_key_size,
struct crypt_params_luks1 *params)
{
int r;
+ unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
+ unsigned long alignment_offset = 0;
- if (!cd->device) {
+ if (!crypt_metadata_device(cd)) {
log_err(cd, _("Can't format LUKS without device.\n"));
return -EINVAL;
}
+ if (!(cd->type = strdup(CRYPT_LUKS1)))
+ return -ENOMEM;
+
+ if (volume_key)
+ cd->volume_key = crypt_alloc_volume_key(volume_key_size,
+ volume_key);
+ else
+ cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
+
+ if(!cd->volume_key)
+ return -ENOMEM;
+
+ if (params && params->data_device) {
+ cd->metadata_device = cd->device;
+ cd->device = NULL;
+ if (device_alloc(&cd->device, params->data_device) < 0)
+ return -ENOMEM;
+ required_alignment = params->data_alignment * SECTOR_SIZE;
+ } else if (params && params->data_alignment) {
+ required_alignment = params->data_alignment * SECTOR_SIZE;
+ } else
+ device_topology_alignment(cd->device,
+ &required_alignment,
+ &alignment_offset, DEFAULT_DISK_ALIGNMENT);
+
+ /* Check early if we cannot allocate block device for key slot access */
+ r = device_block_adjust(cd, cd->device, DEV_OK, 0, NULL, NULL);
+ if(r < 0)
+ return r;
+
r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
(params && params->hash) ? params->hash : "sha1",
uuid, LUKS_STRIPES,
- params ? params->data_alignment: DEFAULT_ALIGNMENT,
- cd->iteration_time, &cd->PBKDF2_per_sec, cd);
+ required_alignment / SECTOR_SIZE,
+ alignment_offset / SECTOR_SIZE,
+ cd->iteration_time, &cd->PBKDF2_per_sec,
+ cd->metadata_device ? 1 : 0, cd);
if(r < 0)
return r;
/* Wipe first 8 sectors - fs magic numbers etc. */
- r = wipe_device_header(cd->device, 8);
+ r = crypt_wipe(crypt_metadata_device(cd), 0, 8 * SECTOR_SIZE, CRYPT_WIPE_ZERO, 1);
if(r < 0) {
- log_err(cd, _("Can't wipe header on device %s.\n"), cd->device);
+ if (r == -EBUSY)
+ log_err(cd, _("Cannot format device %s which is still in use.\n"),
+ mdata_device_path(cd));
+ else if (r == -EACCES) {
+ log_err(cd, _("Cannot format device %s, permission denied.\n"),
+ mdata_device_path(cd));
+ r = -EINVAL;
+ } else
+ log_err(cd, _("Cannot wipe header on device %s.\n"),
+ mdata_device_path(cd));
+
+ return r;
+ }
+
+ r = LUKS_write_phdr(&cd->hdr, cd);
+
+ return r;
+}
+
+static int _crypt_format_loopaes(struct crypt_device *cd,
+ const char *cipher,
+ const char *uuid,
+ size_t volume_key_size,
+ struct crypt_params_loopaes *params)
+{
+ if (!crypt_metadata_device(cd)) {
+ log_err(cd, _("Can't format LOOPAES without device.\n"));
+ return -EINVAL;
+ }
+
+ if (volume_key_size > 1024) {
+ log_err(cd, _("Invalid key size.\n"));
+ return -EINVAL;
+ }
+
+ if (!(cd->type = strdup(CRYPT_LOOPAES)))
+ return -ENOMEM;
+
+ cd->loopaes_key_size = volume_key_size;
+
+ cd->loopaes_cipher = strdup(cipher ?: DEFAULT_LOOPAES_CIPHER);
+
+ if (uuid)
+ cd->loopaes_uuid = strdup(uuid);
+
+ if (params && params->hash)
+ cd->loopaes_hdr.hash = strdup(params->hash);
+
+ cd->loopaes_hdr.offset = params ? params->offset : 0;
+ cd->loopaes_hdr.skip = params ? params->skip : 0;
+
+ return 0;
+}
+
+static int _crypt_format_verity(struct crypt_device *cd,
+ const char *uuid,
+ struct crypt_params_verity *params)
+{
+ int r = 0, hash_size;
+ uint64_t data_device_size;
+
+ if (!crypt_metadata_device(cd)) {
+ log_err(cd, _("Can't format VERITY without device.\n"));
+ return -EINVAL;
+ }
+
+ if (!params || !params->data_device)
+ return -EINVAL;
+
+ if (params->hash_type > VERITY_MAX_HASH_TYPE) {
+ log_err(cd, _("Unsupported VERITY hash type %d.\n"), params->hash_type);
+ return -EINVAL;
+ }
+
+ if (VERITY_BLOCK_SIZE_OK(params->data_block_size) ||
+ VERITY_BLOCK_SIZE_OK(params->hash_block_size)) {
+ log_err(cd, _("Unsupported VERITY block size.\n"));
+ return -EINVAL;
+ }
+
+ if (params->hash_area_offset % 512) {
+ log_err(cd, _("Unsupported VERITY hash offset.\n"));
+ return -EINVAL;
+ }
+
+ if (!(cd->type = strdup(CRYPT_VERITY)))
+ return -ENOMEM;
+
+ r = crypt_set_data_device(cd, params->data_device);
+ if (r)
return r;
+ if (!params->data_size) {
+ r = device_size(cd->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;
+
+ hash_size = crypt_hash_size(params->hash_name);
+ if (hash_size <= 0) {
+ log_err(cd, _("Hash algorithm %s not supported.\n"),
+ params->hash_name);
+ return -EINVAL;
}
+ cd->verity_root_hash_size = hash_size;
+
+ cd->verity_root_hash = malloc(cd->verity_root_hash_size);
+ if (!cd->verity_root_hash)
+ return -ENOMEM;
- r = LUKS_write_phdr(cd->device, &cd->hdr, cd);
+ cd->verity_hdr.flags = params->flags;
+ if (!(cd->verity_hdr.hash_name = strdup(params->hash_name)))
+ return -ENOMEM;
+ 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;
+ if (!(cd->verity_hdr.salt = malloc(params->salt_size)))
+ return -ENOMEM;
+ 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->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, cd->verity_hdr.hash_area_offset,
+ cd->verity_uuid,
+ &cd->verity_hdr);
+ }
return r;
}
{
int r;
- log_dbg("Formatting device %s as type %s.", cd->device ?: "(none)", cd->type ?: "(none)");
-
if (!type)
return -EINVAL;
- /* Some hash functions need initialized gcrypt library */
- if (init_crypto()) {
- log_err(cd, _("Cannot initialize crypto backend.\n"));
- return -ENOSYS;
+ if (cd->type) {
+ log_dbg("Context already formatted as %s.", cd->type);
+ return -EINVAL;
}
- if (volume_key)
- cd->volume_key = LUKS_alloc_masterkey(volume_key_size,
- volume_key);
- else
- cd->volume_key = LUKS_generate_masterkey(volume_key_size);
+ log_dbg("Formatting device %s as type %s.", mdata_device_path(cd) ?: "(none)", type);
- if(!cd->volume_key)
- return -ENOMEM;
+ r = init_crypto(cd);
+ if (r < 0)
+ return r;
if (isPLAIN(type))
r = _crypt_format_plain(cd, cipher, cipher_mode,
- uuid, params);
+ uuid, volume_key_size, params);
else if (isLUKS(type))
r = _crypt_format_luks1(cd, cipher, cipher_mode,
- uuid, params);
+ 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);
r = -EINVAL;
}
- if (!r && !(cd->type = strdup(type)))
- r = -ENOMEM;
-
if (r < 0) {
- LUKS_dealloc_masterkey(cd->volume_key);
+ free(cd->type);
+ cd->type = NULL;
+ crypt_free_volume_key(cd->volume_key);
cd->volume_key = NULL;
}
const char *requested_type,
void *params)
{
- struct luks_phdr hdr;
int r;
log_dbg("Trying to load %s crypt type from device %s.",
- requested_type ?: "any", cd->device ?: "(none)");
+ requested_type ?: "any", mdata_device_path(cd) ?: "(none)");
+
+ if (!crypt_metadata_device(cd))
+ 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;
+ }
+
+ 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 if (isTCRYPT(requested_type)) {
+ if (cd->type && !isTCRYPT(cd->type)) {
+ log_dbg("Context is already initialised to type %s", cd->type);
+ return -EINVAL;
+ }
+ r = _crypt_load_tcrypt(cd, params);
+ } else
+ return -EINVAL;
+
+ return r;
+}
+
+int crypt_repair(struct crypt_device *cd,
+ const char *requested_type,
+ void *params __attribute__((unused)))
+{
+ int r;
- if (!cd->device)
+ log_dbg("Trying to repair %s crypt type from device %s.",
+ requested_type ?: "any", mdata_device_path(cd) ?: "(none)");
+
+ if (!crypt_metadata_device(cd))
return -EINVAL;
if (requested_type && !isLUKS(requested_type))
return -EINVAL;
- /* Some hash functions need initialized gcrypt library */
- if (init_crypto()) {
- log_err(cd, _("Cannot initialize crypto backend.\n"));
- return -ENOSYS;
+
+ /* 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);
+ if (r < 0) {
+ free(cd->type);
+ cd->type = NULL;
}
- r = LUKS_read_phdr(cd->device, &hdr, 1, cd);
+ return r;
+}
- if (!r) {
- memcpy(&cd->hdr, &hdr, sizeof(hdr));
- cd->type = strdup(requested_type);
- if (!cd->type)
- r = -ENOMEM;
+int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
+{
+ struct crypt_dm_active_device dmd;
+ int r;
+
+ /* Device context type must be initialised */
+ if (!cd->type || !crypt_get_uuid(cd))
+ return -EINVAL;
+
+ log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
+
+ r = dm_query_device(cd, 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);
+ return -EINVAL;
+ }
+
+ if (!dmd.uuid || dmd.target != DM_CRYPT) {
+ r = -EINVAL;
+ goto out;
+ }
+
+ r = device_block_adjust(cd, dmd.data_device, DEV_OK,
+ dmd.u.crypt.offset, &new_size, &dmd.flags);
+ if (r)
+ goto out;
+
+ if (new_size == dmd.size) {
+ log_dbg("Device has already requested size %" PRIu64
+ " sectors.", dmd.size);
+ r = 0;
+ } else {
+ dmd.size = new_size;
+ r = dm_create_device(cd, name, cd->type, &dmd, 1);
+ }
+out:
+ 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;
}
+int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
+{
+ if (!isLUKS(cd->type)) {
+ log_err(cd, _("This operation is not supported for this device type.\n"));
+ return -EINVAL;
+ }
+
+ if (uuid && !strncmp(uuid, cd->hdr.uuid, sizeof(cd->hdr.uuid))) {
+ log_dbg("UUID is the same as requested (%s) for device %s.",
+ uuid, mdata_device_path(cd));
+ return 0;
+ }
+
+ if (uuid)
+ log_dbg("Requested new UUID change to %s for %s.", uuid, mdata_device_path(cd));
+ else
+ log_dbg("Requested new UUID refresh for %s.", mdata_device_path(cd));
+
+ if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
+ return -EPERM;
+
+ return LUKS_hdr_uuid_set(&cd->hdr, uuid, cd);
+}
+
int crypt_header_backup(struct crypt_device *cd,
const char *requested_type,
const char *backup_file)
{
+ int r;
+
if ((requested_type && !isLUKS(requested_type)) || !backup_file)
return -EINVAL;
+ r = init_crypto(cd);
+ if (r < 0)
+ return r;
+
log_dbg("Requested header backup of device %s (%s) to "
- "file %s.", cd->device, requested_type, backup_file);
+ "file %s.", mdata_device_path(cd), requested_type, backup_file);
- return LUKS_hdr_backup(backup_file, cd->device, &cd->hdr, cd);
+ return LUKS_hdr_backup(backup_file, &cd->hdr, cd);
}
int crypt_header_restore(struct crypt_device *cd,
const char *requested_type,
const char *backup_file)
{
+ int r;
+
if (requested_type && !isLUKS(requested_type))
return -EINVAL;
+ r = init_crypto(cd);
+ if (r < 0)
+ return r;
+
log_dbg("Requested header restore to device %s (%s) from "
- "file %s.", cd->device, requested_type, backup_file);
+ "file %s.", mdata_device_path(cd), requested_type, backup_file);
- return LUKS_hdr_restore(backup_file, cd->device, &cd->hdr, cd);
+ return LUKS_hdr_restore(backup_file, &cd->hdr, cd);
}
void crypt_free(struct crypt_device *cd)
{
if (cd) {
- log_dbg("Releasing crypt device %s context.", cd->device);
+ log_dbg("Releasing crypt device %s context.", mdata_device_path(cd));
- dm_exit();
- if (cd->volume_key)
- LUKS_dealloc_masterkey(cd->volume_key);
+ dm_backend_exit();
+ crypt_free_volume_key(cd->volume_key);
- free(cd->device);
+ device_free(cd->device);
+ device_free(cd->metadata_device);
free(cd->type);
/* used in plain device only */
- free((char*)cd->plain_hdr.hash);
+ free(CONST_CAST(void*)cd->plain_hdr.hash);
free(cd->plain_cipher);
free(cd->plain_cipher_mode);
free(cd->plain_uuid);
+ /* used in loop-AES device only */
+ free(CONST_CAST(void*)cd->loopaes_hdr.hash);
+ 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);
}
}
const char *name)
{
crypt_status_info ci;
- int r, suspended = 0;
+ int r;
log_dbg("Suspending volume %s.", name);
+ if (!cd || !isLUKS(cd->type)) {
+ log_err(cd, _("This operation is supported only for LUKS device.\n"));
+ r = -EINVAL;
+ goto out;
+ }
+
ci = crypt_status(NULL, name);
if (ci < CRYPT_ACTIVE) {
log_err(cd, _("Volume %s is not active.\n"), name);
return -EINVAL;
}
- if (!cd && dm_init(NULL, 1) < 0)
- return -ENOSYS;
+ dm_backend_init();
- r = dm_query_device(name, NULL, NULL, NULL, NULL,
- NULL, NULL, NULL, NULL, &suspended, NULL);
+ r = dm_status_suspended(cd, name);
if (r < 0)
goto out;
- if (suspended) {
+ if (r) {
log_err(cd, _("Volume %s is already suspended.\n"), name);
r = -EINVAL;
goto out;
}
- r = dm_suspend_and_wipe_key(name);
- if (r)
+ r = dm_suspend_and_wipe_key(cd, name);
+ if (r == -ENOTSUP)
+ log_err(cd, "Suspend is not supported for device %s.\n", name);
+ else if (r)
log_err(cd, "Error during suspending device %s.\n", name);
out:
- if (!cd)
- dm_exit();
+ dm_backend_exit();
return r;
}
const char *passphrase,
size_t passphrase_size)
{
- struct luks_masterkey *mk = NULL;
- int r, suspended = 0;
+ struct volume_key *vk = NULL;
+ int r;
log_dbg("Resuming volume %s.", name);
goto out;
}
- r = dm_query_device(name, NULL, NULL, NULL, NULL,
- NULL, NULL, NULL, NULL, &suspended, NULL);
+ r = dm_status_suspended(cd, name);
if (r < 0)
return r;
- if (!suspended) {
+ if (!r) {
log_err(cd, _("Volume %s is not suspended.\n"), name);
return -EINVAL;
}
if (passphrase) {
- r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
- passphrase_size, &cd->hdr, &mk, cd);
+ r = LUKS_open_key_with_hdr(keyslot, passphrase, passphrase_size,
+ &cd->hdr, &vk, cd);
} else
- r = volume_key_by_terminal_passphrase(cd, keyslot, &mk);
+ r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
if (r >= 0) {
keyslot = r;
- r = dm_resume_and_reinstate_key(name, mk->keyLength, mk->key);
- if (r)
+ r = dm_resume_and_reinstate_key(cd, name, vk->keylength, vk->key);
+ if (r == -ENOTSUP)
+ log_err(cd, "Resume is not supported for device %s.\n", name);
+ else if (r)
log_err(cd, "Error during resuming device %s.\n", name);
} else
r = keyslot;
out:
- LUKS_dealloc_masterkey(mk);
+ crypt_free_volume_key(vk);
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 luks_masterkey *mk = NULL;
+ struct volume_key *vk = NULL;
char *passphrase_read = NULL;
- unsigned int passphrase_size_read;
- int r, suspended = 0;
+ size_t passphrase_size_read;
+ int r;
log_dbg("Resuming volume %s.", name);
goto out;
}
- r = dm_query_device(name, NULL, NULL, NULL, NULL,
- NULL, NULL, NULL, NULL, &suspended, NULL);
+ r = dm_status_suspended(cd, name);
if (r < 0)
return r;
- if (!suspended) {
+ if (!r) {
log_err(cd, _("Volume %s is not suspended.\n"), name);
return -EINVAL;
}
if (!keyfile)
return -EINVAL;
- key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
- &passphrase_size_read, keyfile, keyfile_size);
+ r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
+ &passphrase_size_read, keyfile, keyfile_offset,
+ keyfile_size);
+ if (r < 0)
+ goto out;
- if(!passphrase_read)
- r = -EINVAL;
- else {
- r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
- passphrase_size_read, &cd->hdr, &mk, cd);
- safe_free(passphrase_read);
- }
+ r = LUKS_open_key_with_hdr(keyslot, passphrase_read,
+ passphrase_size_read, &cd->hdr, &vk, cd);
+ if (r < 0)
+ goto out;
- if (r >= 0) {
- keyslot = r;
- r = dm_resume_and_reinstate_key(name, mk->keyLength, mk->key);
- if (r)
- log_err(cd, "Error during resuming device %s.\n", name);
- } else
- r = keyslot;
+ keyslot = r;
+ r = dm_resume_and_reinstate_key(cd, name, vk->keylength, vk->key);
+ if (r)
+ log_err(cd, "Error during resuming device %s.\n", name);
out:
- LUKS_dealloc_masterkey(mk);
+ crypt_safe_free(passphrase_read);
+ crypt_free_volume_key(vk);
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
const char *new_passphrase, // NULL -> terminal
size_t new_passphrase_size)
{
- struct luks_masterkey *mk = NULL;
+ struct volume_key *vk = NULL;
char *password = NULL, *new_password = NULL;
- unsigned int passwordLen, new_passwordLen;
+ size_t passwordLen, new_passwordLen;
int r;
log_dbg("Adding new keyslot, existing passphrase %sprovided,"
if (!LUKS_keyslot_active_count(&cd->hdr)) {
/* No slots used, try to use pre-generated key in header */
if (cd->volume_key) {
- mk = LUKS_alloc_masterkey(cd->volume_key->keyLength, cd->volume_key->key);
- r = mk ? 0 : -ENOMEM;
+ vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
+ r = vk ? 0 : -ENOMEM;
} else {
log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
return -EINVAL;
}
} else if (passphrase) {
/* Passphrase provided, use it to unlock existing keyslot */
- r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, passphrase,
- passphrase_size, &cd->hdr, &mk, cd);
+ r = LUKS_open_key_with_hdr(CRYPT_ANY_SLOT, passphrase,
+ passphrase_size, &cd->hdr, &vk, cd);
} else {
/* Passphrase not provided, ask first and use it to unlock existing keyslot */
- key_from_terminal(cd, _("Enter any passphrase: "),
- &password, &passwordLen, 0);
- if (!password) {
- r = -EINVAL;
+ r = key_from_terminal(cd, _("Enter any passphrase: "),
+ &password, &passwordLen, 0);
+ if (r < 0)
goto out;
- }
- r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
- passwordLen, &cd->hdr, &mk, cd);
- safe_free(password);
+ r = LUKS_open_key_with_hdr(CRYPT_ANY_SLOT, password,
+ passwordLen, &cd->hdr, &vk, cd);
+ crypt_safe_free(password);
}
if(r < 0)
goto out;
if (new_passphrase) {
- new_password = (char *)new_passphrase;
+ new_password = CONST_CAST(char*)new_passphrase;
new_passwordLen = new_passphrase_size;
} else {
- key_from_terminal(cd, _("Enter new passphrase for key slot: "),
- &new_password, &new_passwordLen, 1);
- if(!new_password) {
- r = -EINVAL;
+ r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
+ &new_password, &new_passwordLen, 1);
+ if(r < 0)
goto out;
- }
}
- r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
- &cd->hdr, mk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
+ r = LUKS_set_key(keyslot, new_password, new_passwordLen,
+ &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
if(r < 0) goto out;
r = 0;
out:
if (!new_passphrase)
- safe_free(new_password);
- LUKS_dealloc_masterkey(mk);
+ crypt_safe_free(new_password);
+ crypt_free_volume_key(vk);
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 luks_masterkey *mk=NULL;
- char *password=NULL; unsigned int passwordLen;
- char *new_password = NULL; unsigned int new_passwordLen;
+ struct volume_key *vk = NULL;
+ char *password = NULL; size_t passwordLen;
+ char *new_password = NULL; size_t new_passwordLen;
int r;
log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
if (!LUKS_keyslot_active_count(&cd->hdr)) {
/* No slots used, try to use pre-generated key in header */
if (cd->volume_key) {
- mk = LUKS_alloc_masterkey(cd->volume_key->keyLength, cd->volume_key->key);
- r = mk ? 0 : -ENOMEM;
+ vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
+ r = vk ? 0 : -ENOMEM;
} else {
log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
return -EINVAL;
} else {
/* Read password from file of (if NULL) from terminal */
if (keyfile)
- key_from_file(cd, _("Enter any passphrase: "), &password, &passwordLen,
- keyfile, keyfile_size);
+ r = key_from_file(cd, _("Enter any passphrase: "),
+ &password, &passwordLen,
+ keyfile, keyfile_offset, keyfile_size);
else
- key_from_terminal(cd, _("Enter any passphrase: "),
- &password, &passwordLen, 1);
-
- if (!password)
- return -EINVAL;
+ r = key_from_terminal(cd, _("Enter any passphrase: "),
+ &password, &passwordLen, 0);
+ if (r < 0)
+ goto out;
- r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password, passwordLen,
- &cd->hdr, &mk, cd);
- safe_free(password);
+ r = LUKS_open_key_with_hdr(CRYPT_ANY_SLOT, password, passwordLen,
+ &cd->hdr, &vk, cd);
}
if(r < 0)
goto out;
if (new_keyfile)
- key_from_file(cd, _("Enter new passphrase for key slot: "),
- &new_password, &new_passwordLen, new_keyfile,
- new_keyfile_size);
+ r = key_from_file(cd, _("Enter new passphrase for key slot: "),
+ &new_password, &new_passwordLen, new_keyfile,
+ new_keyfile_offset, new_keyfile_size);
else
- key_from_terminal(cd, _("Enter new passphrase for key slot: "),
- &new_password, &new_passwordLen, 1);
-
- if(!new_password) {
- r = -EINVAL;
+ r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
+ &new_password, &new_passwordLen, 1);
+ if (r < 0)
goto out;
- }
- r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
- &cd->hdr, mk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
+ r = LUKS_set_key(keyslot, new_password, new_passwordLen,
+ &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
out:
- safe_free(new_password);
- LUKS_dealloc_masterkey(mk);
+ crypt_safe_free(password);
+ crypt_safe_free(new_password);
+ crypt_free_volume_key(vk);
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,
const char *passphrase,
size_t passphrase_size)
{
- struct luks_masterkey *mk = NULL;
+ struct volume_key *vk = NULL;
int r = -EINVAL;
- char *new_password = NULL; unsigned int new_passwordLen;
+ char *new_password = NULL; size_t new_passwordLen;
log_dbg("Adding new keyslot %d using volume key.", keyslot);
}
if (volume_key)
- mk = LUKS_alloc_masterkey(volume_key_size, volume_key);
+ vk = crypt_alloc_volume_key(volume_key_size, volume_key);
else if (cd->volume_key)
- mk = LUKS_alloc_masterkey(cd->volume_key->keyLength, cd->volume_key->key);
+ vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
- if (!mk)
+ if (!vk)
return -ENOMEM;
- r = LUKS_verify_master_key(&cd->hdr, mk);
+ r = LUKS_verify_volume_key(&cd->hdr, vk);
if (r < 0) {
log_err(cd, _("Volume key does not match the volume.\n"));
goto out;
goto out;
if (!passphrase) {
- key_from_terminal(cd, _("Enter new passphrase for key slot: "),
- &new_password, &new_passwordLen, 1);
+ r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
+ &new_password, &new_passwordLen, 1);
+ if (r < 0)
+ goto out;
passphrase = new_password;
passphrase_size = new_passwordLen;
}
- r = LUKS_set_key(cd->device, keyslot, passphrase, passphrase_size,
- &cd->hdr, mk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
+ r = LUKS_set_key(keyslot, passphrase, passphrase_size,
+ &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
out:
- if (new_password)
- safe_free(new_password);
- LUKS_dealloc_masterkey(mk);
- return r ?: keyslot;
+ crypt_safe_free(new_password);
+ crypt_free_volume_key(vk);
+ return (r < 0) ? r : keyslot;
}
int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
return -EINVAL;
}
- return LUKS_del_key(cd->device, keyslot, &cd->hdr, cd);
+ return LUKS_del_key(keyslot, &cd->hdr, cd);
}
// activation/deactivation of device mapping
uint32_t flags)
{
crypt_status_info ci;
- struct luks_masterkey *mk = NULL;
- char *prompt = NULL;
+ struct volume_key *vk = NULL;
+ char *read_passphrase = NULL;
+ size_t passphraseLen = 0;
int r;
log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
name ? "Activating" : "Checking", name ?: "",
keyslot, passphrase ? "" : "[none] ");
- if (!name)
- return -EINVAL;
-
- /* plain, use hashed passphrase */
- if (isPLAIN(cd->type))
- return create_device_helper(cd, name, cd->plain_hdr.hash,
- cd->plain_cipher, cd->plain_cipher_mode, NULL, passphrase, passphrase_size,
- cd->volume_key->keyLength, 0, cd->plain_hdr.skip,
- cd->plain_hdr.offset, cd->plain_uuid, flags & CRYPT_ACTIVATE_READONLY, 0, 0);
-
if (name) {
ci = crypt_status(NULL, name);
if (ci == CRYPT_INVALID)
}
}
- if(asprintf(&prompt, _("Enter passphrase for %s: "), cd->device) < 0)
- return -ENOMEM;
+ /* plain, use hashed passphrase */
+ if (isPLAIN(cd->type)) {
+ if (!name)
+ return -EINVAL;
- /* provided passphrase, do not retry */
- if (passphrase) {
- r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
- passphrase_size, &cd->hdr, &mk, cd);
- } else
- r = volume_key_by_terminal_passphrase(cd, keyslot, &mk);
+ if (!passphrase) {
+ r = key_from_terminal(cd, NULL, &read_passphrase,
+ &passphraseLen, 0);
+ if (r < 0)
+ goto out;
+ passphrase = read_passphrase;
+ passphrase_size = passphraseLen;
+ }
- if (r >= 0) {
- keyslot = r;
- if (name)
- r = open_from_hdr_and_mk(cd, mk, name, flags);
- }
+ r = process_key(cd, cd->plain_hdr.hash,
+ cd->plain_key_size,
+ passphrase, passphrase_size, &vk);
+ if (r < 0)
+ goto out;
- LUKS_dealloc_masterkey(mk);
- free(prompt);
+ r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
+ keyslot = 0;
+ } else if (isLUKS(cd->type)) {
+ /* provided passphrase, do not retry */
+ if (passphrase) {
+ r = LUKS_open_key_with_hdr(keyslot, passphrase,
+ passphrase_size, &cd->hdr, &vk, cd);
+ } else
+ r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
+
+ if (r >= 0) {
+ keyslot = r;
+ if (name)
+ r = LUKS1_activate(cd, name, vk, flags);
+ }
+ } else
+ r = -EINVAL;
+out:
+ crypt_safe_free(read_passphrase);
+ crypt_free_volume_key(vk);
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;
- struct luks_masterkey *mk = NULL;
+ struct volume_key *vk = NULL;
char *passphrase_read = NULL;
- unsigned int passphrase_size_read;
+ size_t passphrase_size_read;
+ unsigned int key_count = 0;
int r;
log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
- name, keyslot, keyfile ?: "[none]");
-
- if (!isLUKS(cd->type)) {
- log_err(cd, _("This operation is supported only for LUKS device.\n"));
- return -EINVAL;
- }
+ name ?: "", keyslot, keyfile ?: "[none]");
if (name) {
ci = crypt_status(NULL, name);
if (!keyfile)
return -EINVAL;
- key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
- &passphrase_size_read, keyfile, keyfile_size);
- if(!passphrase_read)
- r = -EINVAL;
- else {
- r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
- passphrase_size_read, &cd->hdr, &mk, cd);
- safe_free(passphrase_read);
- }
+ if (isPLAIN(cd->type)) {
+ if (!name)
+ return -EINVAL;
- if (r >= 0) {
+ r = key_from_file(cd, _("Enter passphrase: "),
+ &passphrase_read, &passphrase_size_read,
+ keyfile, keyfile_offset, keyfile_size);
+ if (r < 0)
+ goto out;
+
+ r = process_key(cd, cd->plain_hdr.hash,
+ 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_offset, keyfile_size);
+ if (r < 0)
+ goto out;
+ r = LUKS_open_key_with_hdr(keyslot, passphrase_read,
+ passphrase_size_read, &cd->hdr, &vk, cd);
+ if (r < 0)
+ goto out;
keyslot = r;
- r = open_from_hdr_and_mk(cd, mk, name, flags);
- }
- LUKS_dealloc_masterkey(mk);
+ if (name) {
+ r = LUKS1_activate(cd, name, vk, flags);
+ if (r < 0)
+ goto out;
+ }
+ r = keyslot;
+ } else if (isLOOPAES(cd->type)) {
+ r = key_from_file(cd, NULL, &passphrase_read, &passphrase_size_read,
+ keyfile, keyfile_offset, keyfile_size);
+ if (r < 0)
+ goto out;
+ r = LOOPAES_parse_keyfile(cd, &vk, cd->loopaes_hdr.hash, &key_count,
+ passphrase_read, passphrase_size_read);
+ if (r < 0)
+ goto out;
+ if (name)
+ r = LOOPAES_activate(cd, name, cd->loopaes_cipher,
+ key_count, vk, flags);
+ } else
+ r = -EINVAL;
+
+out:
+ crypt_safe_free(passphrase_read);
+ crypt_free_volume_key(vk);
- return r < 0 ? r : keyslot;
+ 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,
uint32_t flags)
{
crypt_status_info ci;
- struct luks_masterkey *mk;
- int r;
-
- log_dbg("Activating volume %s by volume key.", name);
-
- /* use key directly, no hash */
- if (isPLAIN(cd->type))
- return create_device_helper(cd, name, NULL,
- cd->plain_cipher, cd->plain_cipher_mode, NULL, volume_key, volume_key_size,
- cd->volume_key->keyLength, 0, cd->plain_hdr.skip,
- cd->plain_hdr.offset, cd->plain_uuid, flags & CRYPT_ACTIVATE_READONLY, 0, 0);
+ struct volume_key *vk = NULL;
+ int r = -EINVAL;
- if (!isLUKS(cd->type)) {
- log_err(cd, _("This operation is supported only for LUKS device.\n"));
- return -EINVAL;
- }
+ log_dbg("Activating volume %s by volume key.", name ?: "[none]");
if (name) {
ci = crypt_status(NULL, name);
}
}
- mk = LUKS_alloc_masterkey(volume_key_size, volume_key);
- if (!mk)
- return -ENOMEM;
- r = LUKS_verify_master_key(&cd->hdr, mk);
+ /* use key directly, no hash */
+ if (isPLAIN(cd->type)) {
+ if (!name)
+ return -EINVAL;
- if (r == -EPERM)
- log_err(cd, _("Volume key does not match the volume.\n"));
+ 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;
+ }
+
+ vk = crypt_alloc_volume_key(volume_key_size, volume_key);
+ if (!vk)
+ return -ENOMEM;
+
+ r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
+ } else if (isLUKS(cd->type)) {
+ /* If key is not provided, try to use internal key */
+ if (!volume_key) {
+ if (!cd->volume_key) {
+ log_err(cd, _("Volume key does not match the volume.\n"));
+ return -EINVAL;
+ }
+ volume_key_size = cd->volume_key->keylength;
+ volume_key = cd->volume_key->key;
+ }
+
+ vk = crypt_alloc_volume_key(volume_key_size, volume_key);
+ if (!vk)
+ return -ENOMEM;
+ r = LUKS_verify_volume_key(&cd->hdr, vk);
+
+ if (r == -EPERM)
+ log_err(cd, _("Volume key does not match the volume.\n"));
+
+ 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;
+ }
- if (!r && name)
- r = open_from_hdr_and_mk(cd, mk, name, flags);
+ r = VERITY_activate(cd, name, 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 if (isTCRYPT(cd->type)) {
+ if (!name)
+ return -EINVAL;
+ r = TCRYPT_activate(cd, name, &cd->tcrypt_hdr,
+ &cd->tcrypt_params, flags);
+ } else
+ log_err(cd, _("Device type is not properly initialised.\n"));
- LUKS_dealloc_masterkey(mk);
+ crypt_free_volume_key(vk);
return r;
}
log_dbg("Deactivating volume %s.", name);
- if (!cd && dm_init(NULL, 1) < 0)
- return -ENOSYS;
+ if (!cd)
+ dm_backend_init();
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(cd, name, 0, 0);
break;
case CRYPT_INACTIVE:
log_err(cd, _("Device %s is not active.\n"), name);
}
if (!cd)
- dm_exit();
+ dm_backend_exit();
return r;
}
-// misc helper functions
int crypt_volume_key_get(struct crypt_device *cd,
int keyslot,
char *volume_key,
const char *passphrase,
size_t passphrase_size)
{
- struct luks_masterkey *mk;
- char *processed_key = NULL;
- int r, key_len;
+ struct volume_key *vk = NULL;
+ 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) {
return -ENOMEM;
}
- if (isPLAIN(cd->type)) {
- processed_key = process_key(cd, cd->plain_hdr.hash, NULL, key_len,
- passphrase, passphrase_size);
- if (!processed_key) {
+ if (isPLAIN(cd->type) && cd->plain_hdr.hash) {
+ r = process_key(cd, cd->plain_hdr.hash, key_len,
+ passphrase, passphrase_size, &vk);
+ if (r < 0)
log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
- return -EINVAL;
- }
- memcpy(volume_key, processed_key, key_len);
- *volume_key_size = key_len;
- safe_free(processed_key);
- return 0;
- }
+ } else if (isLUKS(cd->type)) {
+ r = LUKS_open_key_with_hdr(keyslot, passphrase,
+ passphrase_size, &cd->hdr, &vk, cd);
- if (isLUKS(cd->type)) {
- r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
- passphrase_size, &cd->hdr, &mk, cd);
-
- if (r >= 0) {
- memcpy(volume_key, mk->key, mk->keyLength);
- *volume_key_size = mk->keyLength;
- }
+ } else
+ log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
- LUKS_dealloc_masterkey(mk);
- return r;
+ if (r >= 0) {
+ memcpy(volume_key, vk->key, vk->keylength);
+ *volume_key_size = vk->keylength;
}
- log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
- return -EINVAL;
+ crypt_free_volume_key(vk);
+ return r;
}
int crypt_volume_key_verify(struct crypt_device *cd,
const char *volume_key,
size_t volume_key_size)
{
- struct luks_masterkey *mk;
+ struct volume_key *vk;
int r;
if (!isLUKS(cd->type)) {
return -EINVAL;
}
- mk = LUKS_alloc_masterkey(volume_key_size, volume_key);
- if (!mk)
+ vk = crypt_alloc_volume_key(volume_key_size, volume_key);
+ if (!vk)
return -ENOMEM;
- r = LUKS_verify_master_key(&cd->hdr, mk);
+ r = LUKS_verify_volume_key(&cd->hdr, vk);
if (r == -EPERM)
log_err(cd, _("Volume key does not match the volume.\n"));
- LUKS_dealloc_masterkey(mk);
+ crypt_free_volume_key(vk);
return r;
}
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)
{
cd->password_verify = password_verify ? 1 : 0;
}
+void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
+{
+ switch (rng_type) {
+ case CRYPT_RNG_URANDOM:
+ case CRYPT_RNG_RANDOM:
+ log_dbg("RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
+ cd->rng_type = rng_type;
+ }
+}
+
+int crypt_get_rng_type(struct crypt_device *cd)
+{
+ if (!cd)
+ return -EINVAL;
+
+ return cd->rng_type;
+}
+
int crypt_memory_lock(struct crypt_device *cd, int lock)
{
return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
{
int r;
- if (!cd && dm_init(NULL, 1) < 0)
- return CRYPT_INVALID;
+ if (!cd)
+ dm_backend_init();
- r = dm_status_device(name);
+ r = dm_status_device(cd, name);
if (!cd)
- dm_exit();
+ dm_backend_exit();
if (r < 0 && r != -ENODEV)
return CRYPT_INVALID;
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", cd->device);
+ log_std(cd, "LUKS header information for %s\n\n", mdata_device_path(cd));
log_std(cd, "Version: \t%d\n", cd->hdr.version);
log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
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",
return 0;
}
+static int _verity_dump(struct crypt_device *cd)
+{
+ log_std(cd, "VERITY header information for %s\n", mdata_device_path(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 (isLUKS(cd->type))
return cd->hdr.cipherName;
+ if (isLOOPAES(cd->type))
+ return cd->loopaes_cipher;
+
return NULL;
}
if (isLUKS(cd->type))
return cd->hdr.cipherMode;
+ if (isLOOPAES(cd->type))
+ return cd->loopaes_cipher_mode;
+
return NULL;
}
if (isLUKS(cd->type))
return cd->hdr.uuid;
+ if (isPLAIN(cd->type))
+ return cd->plain_uuid;
+
+ if (isLOOPAES(cd->type))
+ return cd->loopaes_uuid;
+
+ if (isVERITY(cd->type))
+ return cd->verity_uuid;
+
return NULL;
}
+const char *crypt_get_device_name(struct crypt_device *cd)
+{
+ const char *path = device_block_path(cd->device);
+
+ if (!path)
+ path = device_path(cd->device);
+
+ return path;
+}
+
int crypt_get_volume_key_size(struct crypt_device *cd)
{
if (isPLAIN(cd->type))
- return cd->volume_key->keyLength;
+ 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;
}
if (isLUKS(cd->type))
return cd->hdr.payloadOffset;
+ if (isLOOPAES(cd->type))
+ return cd->loopaes_hdr.offset;
+
+ if (isTCRYPT(cd->type)) { // FIXME: system vol.
+ if (!cd->tcrypt_hdr.d.mk_offset)
+ return 1;
+ return (cd->tcrypt_hdr.d.mk_offset / cd->tcrypt_hdr.d.sector_size);
+ }
+
+ return 0;
+}
+
+uint64_t crypt_get_iv_offset(struct crypt_device *cd)
+{
+ if (isPLAIN(cd->type))
+ return cd->plain_hdr.skip;
+
+ if (isLUKS(cd->type))
+ return 0;
+
+ if (isLOOPAES(cd->type))
+ return cd->loopaes_hdr.skip;
+
+ if (isTCRYPT(cd->type)) {
+ if (!cd->tcrypt_hdr.d.mk_offset)
+ return 0;
+ return (cd->tcrypt_hdr.d.mk_offset / cd->tcrypt_hdr.d.sector_size);
+ }
+
return 0;
}
return LUKS_keyslot_info(&cd->hdr, keyslot);
}
+
+int crypt_keyslot_max(const char *type)
+{
+ if (type && isLUKS(type))
+ return LUKS_NUMKEYS;
+
+ return -EINVAL;
+}
+
+int crypt_keyslot_area(struct crypt_device *cd,
+ int keyslot,
+ uint64_t *offset,
+ uint64_t *length)
+{
+ if (!isLUKS(cd->type))
+ return -EINVAL;
+
+ return LUKS_keyslot_area(&cd->hdr, keyslot, offset, length);
+}
+
+const char *crypt_get_type(struct crypt_device *cd)
+{
+ 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 = device_path(cd->device);
+ vp->hash_device = mdata_device_path(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)
+{
+ struct crypt_dm_active_device dmd;
+ int r;
+
+ r = dm_query_device(cd, name, 0, &dmd);
+ if (r < 0)
+ return r;
+
+ 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;
+
+ return 0;
+}