#include "libcryptsetup.h"
#include "luks.h"
+#include "loopaes.h"
#include "internal.h"
+#include "crypto_backend.h"
struct crypt_device {
char *type;
char *device;
- struct luks_masterkey *volume_key;
+ 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_mode;
char *plain_uuid;
+ /* 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;
+
/* 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;
};
/* Log helper */
-static void (*_default_log)(int class, char *msg) = 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;
}
-void set_default_log(void (*log)(int class, char *msg))
+int crypt_get_debug_level()
+{
+ return _debug_level;
+}
+
+void crypt_log(struct crypt_device *cd, int level, const char *msg)
{
- _default_log = log;
+ if (cd && cd->log)
+ cd->log(level, msg, cd->log_usrptr);
+ else if (_default_log)
+ _default_log(level, msg, NULL);
}
-void logger(struct crypt_device *cd, int class, const char *file,
+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) {
- if (cd && cd->log)
- cd->log(class, target, cd->log_usrptr);
- else if (_default_log)
- _default_log(class, target);
+ 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);
}
+static int init_crypto(struct crypt_device *ctx)
+{
+ int r;
+
+ 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"));
+
+ return r;
+}
+
/*
* Password processing behaviour matrix of process_key
*
const char *key_file, size_t key_size,
const char *pass, size_t passLen)
{
- char *key = safe_alloc(key_size);
+ char *key;
+
+ if (!key_size)
+ return NULL;
+
+ key = crypt_safe_alloc(key_size);
memset(key, 0, key_size);
/* key is coming from binary 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);
+ crypt_safe_free(key);
return NULL;
}
memcpy(key, pass, key_size);
/* 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);
+ if (crypt_plain_hash(cd, hash_name, key, key_size, pass, passLen) < 0) {
+ log_err(cd, _("Key processing error (using hash algorithm %s).\n"),
+ hash_name);
+ crypt_safe_free(key);
return NULL;
}
} else if (passLen > key_size) {
return key;
}
-int parse_into_name_and_mode(const char *nameAndMode, char *name, char *mode)
-{
-/* 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;
-
- if(sscanf(nameAndMode,scanpattern1, name, mode) != 2) {
- if((r = sscanf(nameAndMode,scanpattern2,name)) == 1)
- strncpy(mode,"cbc-plain",10);
- else
- return -EINVAL;
- }
-
- return 0;
-
-#undef scanpattern1
-#undef scanpattern2
-#undef str
-#undef xstr
-}
-
static int isPLAIN(const char *type)
{
return (type && !strcmp(CRYPT_PLAIN, type));
return (type && !strcmp(CRYPT_LUKS1, type));
}
+static int isLOOPAES(const char *type)
+{
+ return (type && !strcmp(CRYPT_LOOPAES, type));
+}
+
/* keyslot helpers */
static int keyslot_verify_or_find_empty(struct crypt_device *cd, int *keyslot)
{
}
switch (LUKS_keyslot_info(&cd->hdr, *keyslot)) {
- case SLOT_INVALID:
+ case CRYPT_SLOT_INVALID:
log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
*keyslot, LUKS_NUMKEYS - 1);
return -EINVAL;
- case SLOT_INACTIVE:
+ case CRYPT_SLOT_INACTIVE:
break;
default:
log_err(cd, _("Key slot %d is full, please select another one.\n"),
static int verify_other_keyslot(struct crypt_device *cd,
const char *key_file,
- unsigned int flags,
int keyIndex)
{
- struct luks_masterkey *mk;
+ struct volume_key *vk = NULL;
crypt_keyslot_info ki;
- int openedIndex;
+ int openedIndex, r;
char *password = NULL;
unsigned int passwordLen;
- get_key(_("Enter any remaining LUKS passphrase: "), &password,
- &passwordLen, 0, key_file, cd->timeout, flags, cd);
- if(!password)
- return -EINVAL;
+ r = crypt_get_key(_("Enter any remaining LUKS passphrase: "),
+ &password, &passwordLen, 0, key_file, cd->timeout,
+ cd->password_verify, cd);
+ if(r < 0)
+ goto out;
- if (ki == SLOT_ACTIVE_LAST)
+ 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 (openedIndex < 0) {
- LUKS_keyslot_set(&cd->hdr, keyIndex, 1);
- return -EPERM;
- }
+ &cd->hdr, &vk, cd);
- LUKS_dealloc_masterkey(mk);
- safe_free(password);
+ if (ki == CRYPT_SLOT_ACTIVE)
+ LUKS_keyslot_set(&cd->hdr, keyIndex, 1);
- log_std(cd, _("Key slot %d verified.\n"), openedIndex);
- return 0;
+ if (openedIndex < 0)
+ r = -EPERM;
+ else
+ log_verbose(cd, _("Key slot %d verified.\n"), openedIndex);
+out:
+ crypt_free_volume_key(vk);
+ crypt_safe_free(password);
+ return r;
}
static int find_keyslot_by_passphrase(struct crypt_device *cd,
const char *key_file,
- unsigned int flags,
char *message)
{
- struct luks_masterkey *mk;
+ struct volume_key *vk = NULL;
char *password = NULL;
unsigned int passwordLen;
- int keyIndex;
-
- get_key(message,&password,&passwordLen, 0, key_file,
- cd->timeout, flags, cd);
- if(!password)
- 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;
-
- if (get_device_infos(device, &infos, cd) < 0) {
- log_err(cd, _("Cannot get info about device %s.\n"), device);
- return -ENOTBLK;
- }
-
- 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);
- return -EINVAL;
- }
- *size -= *offset;
- }
+ int r;
- if (infos.readonly)
- *read_only = 1;
+ r = crypt_get_key(message,&password,&passwordLen, 0, key_file,
+ cd->timeout, cd->password_verify, cd);
+ if (r < 0)
+ goto out;
- return 0;
+ r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
+ passwordLen, &cd->hdr, &vk, cd);
+out:
+ crypt_free_volume_key(vk);
+ crypt_safe_free(password);
+ return r;
}
static int luks_remove_helper(struct crypt_device *cd,
int r = -EINVAL;
if (key_slot == CRYPT_ANY_SLOT) {
- key_slot = find_keyslot_by_passphrase(cd, key_file, 0,
+ key_slot = find_keyslot_by_passphrase(cd, key_file,
_("Enter LUKS passphrase to be deleted: "));
if(key_slot < 0) {
r = -EPERM;
goto out;
}
- log_std(cd, _("key slot %d selected for deletion.\n"), key_slot);
+ log_std(cd, _("Key slot %d selected for deletion.\n"), key_slot);
}
ki = crypt_keyslot_status(cd, key_slot);
- if (ki == SLOT_INVALID) {
+ 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 <= SLOT_INACTIVE) {
+ if (ki <= CRYPT_SLOT_INACTIVE) {
log_err(cd, _("Key %d not active. Can't wipe.\n"), key_slot);
r = -EINVAL;
goto out;
}
- if (ki == SLOT_ACTIVE_LAST && cd->confirm &&
+ 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))) {
}
if(verify)
- r = verify_other_keyslot(cd, other_key_file, 0, key_slot);
+ r = verify_other_keyslot(cd, other_key_file, key_slot);
else
r = 0;
char *processed_key = NULL;
int r;
+ if (!name)
+ return -EINVAL;
+
ci = crypt_status(cd, name);
- if (ci == INVALID)
+ if (ci == CRYPT_INVALID)
return -EINVAL;
- if (reload && ci < ACTIVE)
+ if (reload && ci < CRYPT_ACTIVE)
return -EINVAL;
- if (!reload && ci >= ACTIVE) {
+ if (!reload && ci >= CRYPT_ACTIVE) {
log_err(cd, _("Device %s already exists.\n"), name);
return -EEXIST;
}
return -EINVAL;
}
- r = device_check_and_adjust(cd, cd->device, &size, &offset, &read_only);
+ r = device_check_and_adjust(cd, cd->device, !reload, &size, &offset, &read_only);
if (r)
return r;
return -ENOMEM;
processed_key = process_key(cd, hash, key_file, key_size, key, keyLen);
- if (!processed_key)
- return -ENOENT;
+ if (!processed_key) {
+ r = -ENOENT;
+ goto out;
+ }
- r = dm_create_device(name, cd->device, dm_cipher ?: cipher, uuid, size, skip, offset,
+ r = dm_create_device(name, cd->device, dm_cipher ?: cipher, cd->type, uuid, size, skip, offset,
key_size, processed_key, read_only, reload);
-
+out:
free(dm_cipher);
- safe_free(processed_key);
+ crypt_safe_free(processed_key);
return r;
}
-static int open_from_hdr_and_mk(struct crypt_device *cd,
- struct luks_masterkey *mk,
+static int open_from_hdr_and_vk(struct crypt_device *cd,
+ struct volume_key *vk,
const char *name,
uint32_t flags)
{
read_only = flags & CRYPT_ACTIVATE_READONLY;
no_uuid = flags & CRYPT_ACTIVATE_NO_UUID;
- r = device_check_and_adjust(cd, cd->device, &size, &offset, &read_only);
+ r = device_check_and_adjust(cd, cd->device, 1, &size, &offset, &read_only);
if (r)
return r;
crypt_get_cipher_mode(cd)) < 0)
r = -ENOMEM;
else
- r = dm_create_device(name, cd->device, cipher, no_uuid ? NULL : crypt_get_uuid(cd),
- size, 0, offset, mk->keyLength, mk->key,
- read_only, 0);
+ r = dm_create_device(name, cd->device, cipher, cd->type,
+ no_uuid ? NULL : crypt_get_uuid(cd),
+ size, 0, offset, vk->keylength, vk->key,
+ read_only, 0);
free(cipher);
return r;
}
-static void log_wrapper(int class, const char *msg, void *usrptr)
+static void log_wrapper(int level, const char *msg, void *usrptr)
{
- void (*xlog)(int class, char *msg) = usrptr;
- xlog(class, (char *)msg);
+ void (*xlog)(int level, char *msg) = usrptr;
+ xlog(level, (char *)msg);
}
static int yesDialog_wrapper(const char *msg, void *usrptr)
return xyesDialog((char*)msg);
}
-static void key_from_terminal(struct crypt_device *cd, char *msg, char **key,
+int crypt_confirm(struct crypt_device *cd, const char *msg)
+{
+ if (!cd || !cd->confirm)
+ return 1;
+ else
+ return cd->confirm(msg, cd->confirm_usrptr);
+}
+
+static int key_from_terminal(struct crypt_device *cd, char *msg, char **key,
unsigned int *key_len, int force_verify)
{
- int r, flags = 0;
+ char *prompt = NULL;
+ int r;
+
+ *key = NULL;
+ if(!msg && asprintf(&prompt, _("Enter passphrase for %s: "),
+ cd->device) < 0)
+ return -ENOMEM;
+
+ if (!msg)
+ 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(MAX_TTY_PASSWORD_LEN);
+ if (!*key) {
+ r = -ENOMEM;
+ goto out;
+ }
+ r = cd->password(msg, *key, MAX_TTY_PASSWORD_LEN, 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, 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 volume_key **vk)
+{
+ char *passphrase_read = NULL;
+ unsigned int passphrase_size_read;
+ int r = -EINVAL, tries = cd->tries;
+
+ *vk = NULL;
+ do {
+ crypt_free_volume_key(*vk);
+ *vk = NULL;
+
+ r = key_from_terminal(cd, NULL, &passphrase_read,
+ &passphrase_size_read, 0);
+ if(r < 0)
+ goto out;
+
+ r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
+ passphrase_size_read, &cd->hdr, vk, cd);
+ crypt_safe_free(passphrase_read);
+ passphrase_read = NULL;
+ } while (r == -EPERM && (--tries > 0));
+out:
+ if (r < 0) {
+ crypt_free_volume_key(*vk);
+ *vk = NULL;
}
+
+ crypt_safe_free(passphrase_read);
+ return r;
}
-static void key_from_file(struct crypt_device *cd, char *msg,
+static int 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);
+ return crypt_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)
{
- int r;
+ 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);
- r = crypt_init(cd, options->device);
+ 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 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);
+ crypt_set_iterarion_time(*cd, options->iteration_time ?: 1000);
crypt_set_password_verify(*cd, options->flags & CRYPT_FLAG_VERIFY);
- if (load)
- r = crypt_load(*cd, CRYPT_LUKS1, NULL);
+ 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;
}
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)
{
- cd->log = log;
- cd->log_usrptr = usrptr;
+ if (!cd)
+ _default_log = log;
+ else {
+ cd->log = log;
+ cd->log_usrptr = usrptr;
+ }
}
void crypt_set_confirm_callback(struct crypt_device *cd,
/* 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 int crypt_create_and_update_device(struct crypt_options *options, int update)
{
struct crypt_device *cd = NULL;
char *key = NULL;
unsigned int keyLen;
int r;
- r = _crypt_init(&cd, options, 0, 1);
+ 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 = crypt_get_key(_("Enter passphrase: "), &key, &keyLen, options->key_size,
+ options->key_file, cd->timeout, cd->password_verify, cd);
+ if (!r)
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);
+ options->flags, update);
- safe_free(key);
+ crypt_safe_free(key);
crypt_free(cd);
return r;
}
-/* OPTIONS: same as create above */
-int crypt_update_device(struct crypt_options *options)
+int crypt_create_device(struct crypt_options *options)
{
- struct crypt_device *cd = NULL;
- char *key = NULL;
- unsigned int keyLen;
- int r;
-
- r = _crypt_init(&cd, 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, 1);
+ return crypt_create_and_update_device(options, 0);
+}
- safe_free(key);
- crypt_free(cd);
- return r;
+int crypt_update_device(struct crypt_options *options)
+{
+ return crypt_create_and_update_device(options, 1);
}
/* OPTIONS: name, size, icb */
int crypt_resize_device(struct crypt_options *options)
{
struct crypt_device *cd = NULL;
- char *device, *cipher, *key = NULL;
+ char *device = NULL, *cipher = NULL, *uuid = NULL, *key = NULL;
+ char *type = NULL;
uint64_t size, skip, offset;
int key_size, read_only, r;
+ log_dbg("Resizing device %s to %" PRIu64 " sectors.", options->name, options->size);
+
+ if (dm_init(NULL, 1) < 0)
+ return -ENOSYS;
+
r = dm_query_device(options->name, &device, &size, &skip, &offset,
- &cipher, &key_size, &key, &read_only);
- if (r < 0)
- return r;
+ &cipher, &key_size, &key, &read_only, NULL, &uuid);
+ if (r < 0) {
+ log_err(NULL, _("Device %s is not active.\n"), options->name);
+ goto out;
+ }
+
+ /* 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 (!options->device)
+ options->device = device;
- r = _crypt_init(&cd, options, 0, 1);
+ r = _crypt_init(&cd, type, options, 1, 1);
if (r)
- return r;
+ goto out;
size = options->size;
- r = device_check_and_adjust(cd, device, &size, &offset, &read_only);
+ r = device_check_and_adjust(cd, device, 0, &size, &offset, &read_only);
if (r)
- return r;
+ goto out;
- r = dm_create_device(options->name, device, cipher, NULL, size, skip, offset,
+ r = dm_create_device(options->name, device, cipher, type,
+ crypt_get_uuid(cd), size, skip, offset,
key_size, key, read_only, 1);
-
- safe_free(key);
+out:
+ crypt_safe_free(key);
free(cipher);
+ if (options->device == device)
+ options->device = NULL;
free(device);
+ free(uuid);
crypt_free(cd);
+ dm_exit();
return r;
}
{
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)
- return 0;
+ if (r < 0)
+ goto out;
r = dm_query_device(options->name, (char **)&options->device, &options->size,
&options->skip, &options->offset, (char **)&options->cipher,
- &options->key_size, NULL, &read_only);
-
- if (r < 0)
- return r;
+ &options->key_size, NULL, &read_only, NULL, NULL);
+ if (r >= 0) {
+ if (read_only)
+ options->flags |= CRYPT_FLAG_READONLY;
- if (read_only)
- options->flags |= CRYPT_FLAG_READONLY;
+ options->flags |= CRYPT_FLAG_FREE_DEVICE;
+ options->flags |= CRYPT_FLAG_FREE_CIPHER;
- options->flags |= CRYPT_FLAG_FREE_DEVICE;
- options->flags |= CRYPT_FLAG_FREE_CIPHER;
+ r = 1;
+ }
+out:
+ if (r == -ENODEV)
+ r = 0;
- return 1;
+ dm_exit();
+ return r;
}
/* OPTIONS: name, icb */
int crypt_remove_device(struct crypt_options *options)
{
- return crypt_deactivate(NULL, options->name);
+ struct crypt_device *cd = NULL;
+ int r;
+
+ r = crypt_init_by_name(&cd, options->name);
+ if (r == 0)
+ r = crypt_deactivate(cd, options->name);
+
+ crypt_free(cd);
+ return r;
+
}
/* OPTIONS: device, cipher, hash, align_payload, key_size (master key), key_slot
char cipherMode[LUKS_CIPHERMODE_L];
char *password=NULL;
unsigned int passwordLen;
- struct crypt_device *cd;
+ struct crypt_device *cd = NULL;
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);
+ r = crypt_parse_name_and_mode(options->cipher, cipherName, NULL, cipherMode);
if(r < 0) {
log_err(cd, _("No known cipher specification pattern detected.\n"));
return r;
}
- if ((r = _crypt_init(&cd, options, 0, 1)))
+ if ((r = _crypt_init(&cd, CRYPT_LUKS1, options, 0, 1)))
return r;
if (options->key_slot >= LUKS_NUMKEYS && options->key_slot != CRYPT_ANY_SLOT) {
goto out;
}
- get_key(_("Enter LUKS passphrase: "), &password, &passwordLen, 0,
- options->new_key_file, options->timeout, options->flags, cd);
+ r = crypt_get_key(_("Enter LUKS passphrase: "), &password, &passwordLen, 0,
+ options->new_key_file, cd->timeout, cd->password_verify, cd);
- if(!password) {
- r = -EINVAL;
+ if(r < 0)
goto out;
- }
r = crypt_format(cd, CRYPT_LUKS1, cipherName, cipherMode,
NULL, NULL, options->key_size, &cp);
password, passwordLen);
out:
crypt_free(cd);
- safe_free(password);
+ crypt_safe_free(password);
return (r < 0) ? r : 0;
}
if (!options->name)
return -EINVAL;
- r = _crypt_init(&cd, options, 1, 1);
+ r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
if (r)
return r;
struct crypt_device *cd = NULL;
int r;
- r = _crypt_init(&cd, options, 1, 1);
+ r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
if (r)
return r;
struct crypt_device *cd = NULL;
int r;
- r = _crypt_init(&cd, options, 1, 1);
+ r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
if (r)
return r;
struct crypt_device *cd = NULL;
int r = -EINVAL;
- r = _crypt_init(&cd, options, 1, 1);
+ r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
if (r)
return r;
char *uuid;
int r;
- r = _crypt_init(&cd, options, 1, 0);
+ r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
if (r)
return r;
struct crypt_device *cd = NULL;
int r;
- r = _crypt_init(&cd, options, 1, 0);
+ log_dbg("Check device %s for LUKS header.", options->device);
+
+ r = init_crypto(cd);
+ if (r < 0)
+ return r;
+
+ r = crypt_init(&cd, options->device);
+ if (r < 0)
+ return -EINVAL;
+
+ /* Do print fail here, no need to crypt_load() */
+ r = LUKS_read_phdr(cd->device, &cd->hdr, 0, cd) ? -EINVAL : 0;
+
crypt_free(cd);
return r;
}
struct crypt_device *cd = NULL;
int r;
- r = _crypt_init(&cd, options, 1, 0);
+ r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
if(r < 0)
return r;
r = crypt_dump(cd);
crypt_free(cd);
- return 0;
+ return r;
}
void crypt_get_error(char *buf, size_t size)
log_dbg("Allocating crypt device %s context.", device);
- if (!device_ready(NULL, device, 0))
+ if (device && !device_ready(NULL, device, O_RDONLY))
return -ENOTBLK;
if (!(h = malloc(sizeof(struct crypt_device))))
memset(h, 0, sizeof(*h));
- h->device = strdup(device);
- if (!h->device) {
- free(h);
- return -ENOMEM;
- }
+ if (device) {
+ h->device = strdup(device);
+ if (!h->device) {
+ free(h);
+ return -ENOMEM;
+ }
+ } else
+ h->device = NULL;
- if (!dm_init(h, 1) < 0) {
+ if (dm_init(h, 1) < 0) {
free(h);
return -ENOSYS;
}
h->iteration_time = 1000;
h->password_verify = 0;
h->tries = 3;
+ h->rng_type = crypt_random_default_key_rng();
*cd = h;
return 0;
}
+int crypt_init_by_name(struct crypt_device **cd, const char *name)
+{
+ crypt_status_info ci;
+ struct crypt_active_device cad;
+ char *device = NULL, *cipher_full = NULL, *device_uuid = NULL;
+ char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
+ char *key = NULL;
+ int key_size = 0, key_nums, r;
+
+
+ log_dbg("Allocating crypt device context by device %s.", name);
+
+ ci = crypt_status(NULL, name);
+ if (ci == CRYPT_INVALID)
+ return -ENODEV;
+
+ if (ci < CRYPT_ACTIVE) {
+ log_err(NULL, _("Device %s is not active.\n"), name);
+ return -ENODEV;
+ }
+
+ r = dm_query_device(name, &device, &cad.size, &cad.iv_offset, &cad.offset,
+ &cipher_full, &key_size, &key, NULL, NULL,
+ &device_uuid);
+ if (r < 0)
+ goto out;
+
+ *cd = NULL;
+ r = crypt_init(cd, device);
+
+ /* Underlying device disappeared but mapping still active */
+ if (!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(device);
+ device = NULL;
+ r = crypt_init(cd, NULL);
+ }
+
+ if (r < 0)
+ goto out;
+
+ /* Try to initialise basic parameters from active device */
+ if (device_uuid) {
+ if (!strncmp(CRYPT_PLAIN, device_uuid, sizeof(CRYPT_PLAIN)-1)) {
+ (*cd)->type = strdup(CRYPT_PLAIN);
+ (*cd)->plain_uuid = strdup(device_uuid);
+ (*cd)->plain_hdr.hash = NULL; /* no way to get this */
+ (*cd)->plain_hdr.offset = cad.offset;
+ (*cd)->plain_hdr.skip = cad.iv_offset;
+ (*cd)->volume_key = crypt_alloc_volume_key(key_size, key);
+ if (!(*cd)->volume_key) {
+ r = -ENOMEM;
+ goto out;
+ }
+
+ r = crypt_parse_name_and_mode(cipher_full, cipher, NULL, cipher_mode);
+ if (!r) {
+ (*cd)->plain_cipher = strdup(cipher);
+ (*cd)->plain_cipher_mode = strdup(cipher_mode);
+ }
+ } else if (!strncmp(CRYPT_LOOPAES, device_uuid, sizeof(CRYPT_LOOPAES)-1)) {
+ (*cd)->type = strdup(CRYPT_LOOPAES);
+ (*cd)->loopaes_uuid = strdup(device_uuid);
+ (*cd)->loopaes_hdr.offset = cad.offset;
+
+ r = crypt_parse_name_and_mode(cipher_full, 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 (key_size % key_nums)
+ key_nums++;
+ (*cd)->loopaes_key_size = key_size / key_nums;
+ }
+ } else if (!strncmp(CRYPT_LUKS1, device_uuid, sizeof(CRYPT_LUKS1)-1)) {
+ if (device) {
+ if (crypt_load(*cd, CRYPT_LUKS1, NULL) < 0 ||
+ crypt_volume_key_verify(*cd, key, key_size) < 0) {
+ log_dbg("LUKS device header does not match active device.");
+ goto out;
+ }
+
+ (*cd)->volume_key = crypt_alloc_volume_key(key_size, key);
+ if (!(*cd)->volume_key) {
+ r = -ENOMEM;
+ goto out;
+ }
+ }
+ }
+ } else
+ log_dbg("Active device has no UUID set, some parameters are not set.");
+
+out:
+ if (r < 0) {
+ crypt_free(*cd);
+ *cd = NULL;
+ }
+ crypt_safe_free(key);
+ free(device);
+ free(cipher_full);
+ free(device_uuid);
+ return r;
+}
+
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;
}
+ 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;
- 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) {
+ log_err(cd, _("Can't format LUKS without device.\n"));
+ return -EINVAL;
+ }
+
+ 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_alignment)
+ required_alignment = params->data_alignment * SECTOR_SIZE;
+ else
+ get_topology_alignment(cd->device, &required_alignment,
+ &alignment_offset, DEFAULT_DISK_ALIGNMENT);
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);
+ required_alignment / SECTOR_SIZE,
+ alignment_offset / SECTOR_SIZE,
+ cd->iteration_time, &cd->PBKDF2_per_sec, cd);
if(r < 0)
return r;
/* Wipe first 8 sectors - fs magic numbers etc. */
r = wipe_device_header(cd->device, 8);
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"),
+ cd->device);
+ else
+ log_err(cd, _("Cannot wipe header on device %s.\n"),
+ cd->device);
+
return r;
}
return r;
}
-int crypt_format(struct crypt_device *cd,
- const char *type,
- const char *cipher,
- const char *cipher_mode,
- const char *uuid,
- const char *volume_key,
- size_t volume_key_size,
+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 (!cd->device) {
+ 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;
+ }
+
+ 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;
+
+ return 0;
+}
+
+int crypt_format(struct crypt_device *cd,
+ const char *type,
+ const char *cipher,
+ const char *cipher_mode,
+ const char *uuid,
+ const char *volume_key,
+ size_t volume_key_size,
void *params)
{
int r;
- log_dbg("Formatting device %s as type %s.", cd->device, cd->type ?: "(none)");
-
if (!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.", cd->device ?: "(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 {
/* FIXME: allow plugins here? */
- log_err(cd, _("Unkown crypt device type %s requesed.\n"), type);
+ log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
r = -EINVAL;
}
r = -ENOMEM;
if (r < 0) {
- LUKS_dealloc_masterkey(cd->volume_key);
+ crypt_free_volume_key(cd->volume_key);
cd->volume_key = NULL;
}
int r;
log_dbg("Trying to load %s crypt type from device %s.",
- requested_type ?: "any", cd->device);
+ requested_type ?: "any", cd->device ?: "(none)");
+
+ if (!cd->device)
+ return -EINVAL;
- if (requested_type && !isPLAIN(requested_type) && !isLUKS(requested_type))
+ if (requested_type && !isLUKS(requested_type))
return -EINVAL;
- r = LUKS_read_phdr(cd->device, &hdr, 0, cd);
+ r = init_crypto(cd);
+ if (r < 0)
+ return r;
+
+ r = LUKS_read_phdr(cd->device, &hdr, 1, cd);
if (!r) {
memcpy(&cd->hdr, &hdr, sizeof(hdr));
- cd->type = strdup(requested_type);
+ cd->type = strdup(CRYPT_LUKS1);
if (!cd->type)
r = -ENOMEM;
}
return r;
}
+int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
+{
+ char *device = NULL, *cipher = NULL, *uuid = NULL, *key = NULL;
+ uint64_t size, skip, offset;
+ int key_size, read_only, r;
+
+ /* Device context type must be initialised */
+ if (!cd->type || !crypt_get_uuid(cd))
+ return -EINVAL;
+
+ r = dm_query_device(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"), name);
+ goto out;
+ }
+
+ if (!uuid) {
+ r = -EINVAL;
+ goto out;
+ }
+
+ r = device_check_and_adjust(cd, device, 0, &new_size, &offset, &read_only);
+ if (r)
+ goto out;
+
+ if (new_size == size) {
+ log_dbg("Device has already requested size %" PRIu64
+ " sectors.", size);
+ r = 0;
+ goto out;
+ }
+
+ log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
+
+ r = dm_create_device(name, device, cipher, cd->type,
+ crypt_get_uuid(cd), new_size, skip, offset,
+ key_size, key, read_only, 1);
+out:
+ crypt_safe_free(key);
+ free(cipher);
+ free(device);
+ free(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, cd->device);
+ return 0;
+ }
+
+ if (uuid)
+ log_dbg("Requested new UUID change to %s for %s.", uuid, cd->device);
+ else
+ log_dbg("Requested new UUID refresh for %s.", cd->device);
+
+ if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
+ return -EPERM;
+
+ return LUKS_hdr_uuid_set(cd->device, &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);
+
+ return LUKS_hdr_backup(backup_file, cd->device, &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;
+
+ /* Some hash functions need initialized gcrypt library */
+ 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);
+
+ return LUKS_hdr_restore(backup_file, cd->device, &cd->hdr, cd);
+}
+
void crypt_free(struct crypt_device *cd)
{
if (cd) {
log_dbg("Releasing crypt device %s context.", cd->device);
dm_exit();
- if (cd->volume_key)
- LUKS_dealloc_masterkey(cd->volume_key);
+ crypt_free_volume_key(cd->volume_key);
free(cd->device);
free(cd->type);
}
}
+int crypt_suspend(struct crypt_device *cd,
+ const char *name)
+{
+ crypt_status_info ci;
+ int r, suspended = 0;
+
+ log_dbg("Suspending volume %s.", name);
+
+ 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;
+
+ r = dm_query_device(name, NULL, NULL, NULL, NULL,
+ NULL, NULL, NULL, NULL, &suspended, NULL);
+ if (r < 0)
+ goto out;
+
+ if (suspended) {
+ log_err(cd, _("Volume %s is already suspended.\n"), name);
+ r = -EINVAL;
+ goto out;
+ }
+
+ r = dm_suspend_and_wipe_key(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();
+ return r;
+}
+
+int crypt_resume_by_passphrase(struct crypt_device *cd,
+ const char *name,
+ int keyslot,
+ const char *passphrase,
+ size_t passphrase_size)
+{
+ struct volume_key *vk = NULL;
+ int r, suspended = 0;
+
+ log_dbg("Resuming volume %s.", name);
+
+ if (!isLUKS(cd->type)) {
+ log_err(cd, _("This operation is supported only for LUKS device.\n"));
+ r = -EINVAL;
+ goto out;
+ }
+
+ r = dm_query_device(name, NULL, NULL, NULL, NULL,
+ NULL, NULL, NULL, NULL, &suspended, NULL);
+ if (r < 0)
+ return r;
+
+ if (!suspended) {
+ 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, &vk, cd);
+ } else
+ r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
+
+ if (r >= 0) {
+ keyslot = r;
+ r = dm_resume_and_reinstate_key(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:
+ 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)
+{
+ struct volume_key *vk = NULL;
+ char *passphrase_read = NULL;
+ unsigned int passphrase_size_read;
+ int r, suspended = 0;
+
+ log_dbg("Resuming volume %s.", name);
+
+ if (!isLUKS(cd->type)) {
+ log_err(cd, _("This operation is supported only for LUKS device.\n"));
+ r = -EINVAL;
+ goto out;
+ }
+
+ r = dm_query_device(name, NULL, NULL, NULL, NULL,
+ NULL, NULL, NULL, NULL, &suspended, NULL);
+ if (r < 0)
+ return r;
+
+ if (!suspended) {
+ log_err(cd, _("Volume %s is not suspended.\n"), name);
+ return -EINVAL;
+ }
+
+ if (!keyfile)
+ return -EINVAL;
+
+ r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
+ &passphrase_size_read, keyfile, keyfile_size);
+ if (r < 0)
+ goto out;
+
+ r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
+ passphrase_size_read, &cd->hdr, &vk, cd);
+ if (r < 0)
+ goto out;
+
+ keyslot = r;
+ r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
+ if (r)
+ log_err(cd, "Error during resuming device %s.\n", name);
+out:
+ crypt_safe_free(passphrase_read);
+ crypt_free_volume_key(vk);
+ return r < 0 ? r : keyslot;
+}
+
// 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;
int r;
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);
+ 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);
+ passwordLen, &cd->hdr, &vk, cd);
+ crypt_safe_free(password);
}
if(r < 0)
new_password = (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);
+ &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;
}
const char *new_keyfile,
size_t new_keyfile_size)
{
- struct luks_masterkey *mk=NULL;
- char *password=NULL; unsigned int passwordLen;
+ struct volume_key *vk = NULL;
+ char *password = NULL; unsigned int passwordLen;
char *new_password = NULL; unsigned int new_passwordLen;
int r;
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_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);
+ &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_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);
+ &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;
}
const char *volume_key,
size_t volume_key_size,
const char *passphrase,
- size_t passphrase_size
-)
+ size_t passphrase_size)
{
- struct luks_masterkey *mk;
+ struct volume_key *vk = NULL;
int r = -EINVAL;
char *new_password = NULL; unsigned int new_passwordLen;
}
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);
+ &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)
}
ki = crypt_keyslot_status(cd, keyslot);
- if (ki == SLOT_INVALID) {
+ if (ki == CRYPT_SLOT_INVALID) {
log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
return -EINVAL;
}
- if (ki == SLOT_INACTIVE) {
+ if (ki == CRYPT_SLOT_INACTIVE) {
log_err(cd, _("Key slot %d is not used.\n"), keyslot);
return -EINVAL;
}
uint32_t flags)
{
crypt_status_info ci;
- struct luks_masterkey *mk = NULL;
- char *prompt = NULL, *passphrase_read = NULL;
- unsigned int passphrase_size_read;
- int r, tries = cd->tries;
+ struct volume_key *vk = NULL;
+ char *read_passphrase = NULL;
+ unsigned int 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 == INVALID)
+ if (ci == CRYPT_INVALID)
return -EINVAL;
- else if (ci >= ACTIVE) {
+ else if (ci >= CRYPT_ACTIVE) {
log_err(cd, _("Device %s already exists.\n"), name);
return -EEXIST;
}
}
- if(asprintf(&prompt, _("Enter passphrase for %s: "), cd->device) < 0)
- return -ENOMEM;
-
- /* provided passphrase, do not retry */
- if (passphrase) {
- r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
- passphrase_size, &cd->hdr, &mk, cd);
- } else 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;
+ /* plain, use hashed passphrase */
+ if (isPLAIN(cd->type)) {
+ if (!passphrase) {
+ r = key_from_terminal(cd, NULL, &read_passphrase,
+ &passphraseLen, 0);
+ if (r < 0)
+ goto out;
+ passphrase = read_passphrase;
+ passphrase_size = passphraseLen;
}
+ r = 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);
+ keyslot = 0;
+ } else if (isLUKS(cd->type)) {
+ /* provided passphrase, do not retry */
+ if (passphrase) {
+ r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
+ passphrase_size, &cd->hdr, &vk, cd);
+ } else
+ r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
- r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
- passphrase_size_read, &cd->hdr, &mk, cd);
- safe_free(passphrase_read);
- passphrase_read = NULL;
- } while (r == -EPERM && (--tries > 0));
-
- if (r >= 0) {
- keyslot = r;
- if (name)
- r = open_from_hdr_and_mk(cd, mk, name, flags);
- }
-
- LUKS_dealloc_masterkey(mk);
- free(prompt);
+ if (r >= 0) {
+ keyslot = r;
+ if (name)
+ r = open_from_hdr_and_vk(cd, vk, name, flags);
+ }
+ } else
+ r = -EINVAL;
+out:
+ crypt_safe_free(read_passphrase);
+ crypt_free_volume_key(vk);
return r < 0 ? r : keyslot;
}
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;
+ unsigned int passphrase_size_read, 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 (ci == INVALID)
+ if (ci == CRYPT_INVALID)
return -EINVAL;
- else if (ci >= ACTIVE) {
+ else if (ci >= CRYPT_ACTIVE) {
log_err(cd, _("Device %s already exists.\n"), name);
return -EEXIST;
}
if (!keyfile)
return -EINVAL;
- key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
- &passphrase_size_read, keyfile, keyfile_size);
- if(!passphrase_read)
- r = -EINVAL;
- else {
+ if (isPLAIN(cd->type)) {
+ r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
+ &passphrase_size_read, keyfile, keyfile_size);
+ if (r < 0)
+ goto out;
+ r = create_device_helper(cd, name, cd->plain_hdr.hash,
+ cd->plain_cipher, cd->plain_cipher_mode,
+ NULL, passphrase_read, passphrase_size_read,
+ cd->volume_key->keylength, 0,
+ cd->plain_hdr.skip, cd->plain_hdr.offset,
+ cd->plain_uuid,
+ flags & CRYPT_ACTIVATE_READONLY, 0, 0);
+ } else if (isLUKS(cd->type)) {
+ r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
+ &passphrase_size_read, keyfile, keyfile_size);
+ 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);
- }
-
- if (r >= 0) {
+ 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 = open_from_hdr_and_vk(cd, vk, name, 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, LOOPAES_KEYFILE_MAXSIZE);
+ if (r < 0)
+ goto out;
+ r = LOOPAES_parse_keyfile(cd, &vk, &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;
- return r < 0 ? r : keyslot;
+out:
+ crypt_safe_free(passphrase_read);
+ crypt_free_volume_key(vk);
+
+ return r;
}
int crypt_activate_by_volume_key(struct crypt_device *cd,
uint32_t flags)
{
crypt_status_info ci;
- struct luks_masterkey *mk;
+ struct volume_key *vk;
int r;
log_dbg("Activating volume %s by volume key.", name);
/* use key directly, no hash */
- if (isPLAIN(cd->type))
+ if (isPLAIN(cd->type)) {
+ if (!volume_key || !volume_key_size || !cd->volume_key ||
+ volume_key_size != cd->volume_key->keylength) {
+ log_err(cd, _("Incorrect volume key specified for plain device.\n"));
+ return -EINVAL;
+ }
+
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->volume_key->keylength, 0, cd->plain_hdr.skip,
cd->plain_hdr.offset, cd->plain_uuid, flags & CRYPT_ACTIVATE_READONLY, 0, 0);
+ }
if (!isLUKS(cd->type)) {
- log_err(cd, _("This operation is supported only for LUKS device.\n"));
+ log_err(cd, _("Device type is not properly initialised.\n"));
return -EINVAL;
}
if (name) {
ci = crypt_status(NULL, name);
- if (ci == INVALID)
+ if (ci == CRYPT_INVALID)
return -EINVAL;
- else if (ci >= ACTIVE) {
+ else if (ci >= CRYPT_ACTIVE) {
log_err(cd, _("Device %s already exists.\n"), name);
return -EEXIST;
}
}
- mk = LUKS_alloc_masterkey(volume_key_size, volume_key);
- if (!mk)
+ /* 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_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"));
if (!r && name)
- r = open_from_hdr_and_mk(cd, mk, name, flags);
+ r = open_from_hdr_and_vk(cd, vk, name, flags);
- LUKS_dealloc_masterkey(mk);
+ crypt_free_volume_key(vk);
return r;
}
int crypt_deactivate(struct crypt_device *cd, const char *name)
{
+ int r;
+
if (!name)
return -EINVAL;
log_dbg("Deactivating volume %s.", name);
+ if (!cd && dm_init(NULL, 1) < 0)
+ return -ENOSYS;
+
switch (crypt_status(cd, name)) {
- case ACTIVE: return dm_remove_device(name, 0, 0);
- case BUSY: log_err(cd, _("Device %s is busy."), name);
- return -EBUSY;
- case INACTIVE: log_err(cd, _("Device %s is not active."), name);
- return -ENODEV;
- default: log_err(cd, _("Invalid device %s."), name);
- return -EINVAL;
+ 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;
+ break;
+ case CRYPT_INACTIVE:
+ log_err(cd, _("Device %s is not active.\n"), name);
+ r = -ENODEV;
+ break;
+ default:
+ log_err(cd, _("Invalid device %s.\n"), name);
+ r = -EINVAL;
}
+
+ if (!cd)
+ dm_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;
+ struct volume_key *vk;
char *processed_key = NULL;
int r, key_len;
return -ENOMEM;
}
- if (isPLAIN(cd->type)) {
+ if (isPLAIN(cd->type) && cd->plain_hdr.hash) {
processed_key = process_key(cd, cd->plain_hdr.hash, NULL, key_len,
passphrase, passphrase_size);
if (!processed_key) {
}
memcpy(volume_key, processed_key, key_len);
*volume_key_size = key_len;
- safe_free(processed_key);
+ crypt_safe_free(processed_key);
return 0;
}
if (isLUKS(cd->type)) {
r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
- passphrase_size, &cd->hdr, &mk, cd);
+ passphrase_size, &cd->hdr, &vk, cd);
if (r >= 0) {
- memcpy(volume_key, mk->key, mk->keyLength);
- *volume_key_size = mk->keyLength;
+ memcpy(volume_key, vk->key, vk->keylength);
+ *volume_key_size = vk->keylength;
}
- LUKS_dealloc_masterkey(mk);
+ crypt_free_volume_key(vk);
return r;
}
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->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 ? memlock_inc(cd) : memlock_dec(cd);
+ return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
}
// reporting
{
int r;
+ if (!cd && dm_init(NULL, 1) < 0)
+ return CRYPT_INVALID;
+
r = dm_status_device(name);
+ if (!cd)
+ dm_exit();
+
if (r < 0 && r != -ENODEV)
- return INVALID;
+ return CRYPT_INVALID;
if (r == 0)
- return ACTIVE;
+ return CRYPT_ACTIVE;
if (r > 0)
- return BUSY;
+ return CRYPT_BUSY;
- return INACTIVE;
+ return CRYPT_INACTIVE;
}
static void hexprintICB(struct crypt_device *cd, char *d, int n)
else
log_std(cd, "Key Slot %d: DISABLED\n", i);
}
+
return 0;
}
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;
+
return NULL;
}
+const char *crypt_get_device_name(struct crypt_device *cd)
+{
+ return cd->device;
+}
+
int crypt_get_volume_key_size(struct crypt_device *cd)
{
- if (isPLAIN(cd->type))
- return cd->volume_key->keyLength;
+ if (isPLAIN(cd->type) && cd->volume_key)
+ return cd->volume_key->keylength;
if (isLUKS(cd->type))
return cd->hdr.keyBytes;
+ if (isLOOPAES(cd->type))
+ return cd->loopaes_key_size;
+
return 0;
}
if (isLUKS(cd->type))
return cd->hdr.payloadOffset;
+ if (isLOOPAES(cd->type))
+ return cd->loopaes_hdr.offset;
+
return 0;
}
{
if (!isLUKS(cd->type)) {
log_err(cd, _("This operation is supported only for LUKS device.\n"));
- return SLOT_INVALID;
+ return CRYPT_SLOT_INVALID;
}
return LUKS_keyslot_info(&cd->hdr, keyslot);
}
+
+int crypt_keyslot_max(const char *type)
+{
+ if (type && isLUKS(type))
+ return LUKS_NUMKEYS;
+
+ return -EINVAL;
+}
+
+const char *crypt_get_type(struct crypt_device *cd)
+{
+ return cd->type;
+}
+
+int crypt_get_active_device(struct crypt_device *cd,
+ const char *name,
+ struct crypt_active_device *cad)
+{
+ int r, readonly;
+
+ r = dm_query_device(name, NULL, &cad->size, &cad->iv_offset, &cad->offset,
+ NULL, NULL, NULL, &readonly, NULL, NULL);
+ if (r < 0)
+ return r;
+
+ cad->flags = readonly ? CRYPT_ACTIVATE_READONLY : 0;
+
+ return 0;
+}