2 * libcryptsetup - cryptsetup library
4 * Copyright (C) 2004, Christophe Saout <christophe@saout.de>
5 * Copyright (C) 2004-2007, Clemens Fruhwirth <clemens@endorphin.org>
6 * Copyright (C) 2009-2011, Red Hat, Inc. All rights reserved.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "libcryptsetup.h"
33 #include "crypto_backend.h"
39 char *metadata_device;
43 struct volume_key *volume_key;
45 uint64_t iteration_time;
50 /* used in CRYPT_LUKS1 */
52 uint64_t PBKDF2_per_sec;
54 /* used in CRYPT_PLAIN */
55 struct crypt_params_plain plain_hdr;
57 char *plain_cipher_mode;
60 /* used in CRYPT_LOOPAES */
61 struct crypt_params_loopaes loopaes_hdr;
63 char *loopaes_cipher_mode;
65 unsigned int loopaes_key_size;
67 /* callbacks definitions */
68 void (*log)(int level, const char *msg, void *usrptr);
70 int (*confirm)(const char *msg, void *usrptr);
72 int (*password)(const char *msg, char *buf, size_t length, void *usrptr);
73 void *password_usrptr;
77 /* FIXME: not thread safe, remove this later */
78 static char global_error[512] = {0};
81 static void (*_default_log)(int level, const char *msg, void *usrptr) = NULL;
82 static int _debug_level = 0;
84 void crypt_set_debug_level(int level)
89 int crypt_get_debug_level(void)
95 void crypt_log(struct crypt_device *cd, int level, const char *msg)
98 cd->log(level, msg, cd->log_usrptr);
99 else if (_default_log)
100 _default_log(level, msg, NULL);
103 /* Set global error, ugly hack... */
104 void set_global_error(const char *error)
106 size_t size = strlen(error);
108 strncpy(global_error, error, sizeof(global_error) - 2);
109 if (size < sizeof(global_error) && global_error[size - 1] == '\n')
110 global_error[size - 1] = '\0';
113 __attribute__((format(printf, 5, 6)))
114 void logger(struct crypt_device *cd, int level, const char *file,
115 int line, const char *format, ...)
120 va_start(argp, format);
122 if (vasprintf(&target, format, argp) > 0 ) {
124 crypt_log(cd, level, target);
126 } else if (_debug_level)
127 printf("# %s:%d %s\n", file ?: "?", line, target);
129 } else if (_debug_level)
130 printf("# %s\n", target);
133 if (level == CRYPT_LOG_ERROR)
134 set_global_error(target);
141 static const char *mdata_device(struct crypt_device *cd)
143 return cd->metadata_device ?: cd->device;
146 static int init_crypto(struct crypt_device *ctx)
150 r = crypt_random_init(ctx);
152 log_err(ctx, _("Cannot initialize crypto RNG backend.\n"));
156 r = crypt_backend_init(ctx);
158 log_err(ctx, _("Cannot initialize crypto backend.\n"));
163 static int process_key(struct crypt_device *cd, const char *hash_name,
164 size_t key_size, const char *pass, size_t passLen,
165 struct volume_key **vk)
172 *vk = crypt_alloc_volume_key(key_size, NULL);
177 r = crypt_plain_hash(cd, hash_name, (*vk)->key, key_size, pass, passLen);
180 log_err(cd, _("Hash algorithm %s not supported.\n"),
183 log_err(cd, _("Key processing error (using hash %s).\n"),
185 crypt_free_volume_key(*vk);
189 } else if (passLen > key_size) {
190 memcpy((*vk)->key, pass, key_size);
192 memcpy((*vk)->key, pass, passLen);
198 static int isPLAIN(const char *type)
200 return (type && !strcmp(CRYPT_PLAIN, type));
203 static int isLUKS(const char *type)
205 return (type && !strcmp(CRYPT_LUKS1, type));
208 static int isLOOPAES(const char *type)
210 return (type && !strcmp(CRYPT_LOOPAES, type));
213 /* keyslot helpers */
214 static int keyslot_verify_or_find_empty(struct crypt_device *cd, int *keyslot)
216 if (*keyslot == CRYPT_ANY_SLOT) {
217 *keyslot = LUKS_keyslot_find_empty(&cd->hdr);
219 log_err(cd, _("All key slots full.\n"));
224 switch (LUKS_keyslot_info(&cd->hdr, *keyslot)) {
225 case CRYPT_SLOT_INVALID:
226 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
227 *keyslot, LUKS_NUMKEYS - 1);
229 case CRYPT_SLOT_INACTIVE:
232 log_err(cd, _("Key slot %d is full, please select another one.\n"),
241 * compares UUIDs returned by device-mapper (striped by cryptsetup) and uuid in header
243 static int crypt_uuid_cmp(const char *dm_uuid, const char *hdr_uuid)
248 if (!dm_uuid || !hdr_uuid)
251 str = strchr(dm_uuid, '-');
255 for (i = 0, j = 1; hdr_uuid[i]; i++) {
256 if (hdr_uuid[i] == '-')
259 if (!str[j] || str[j] == '-')
262 if (str[j] != hdr_uuid[i])
270 int PLAIN_activate(struct crypt_device *cd,
272 struct volume_key *vk,
277 char *dm_cipher = NULL;
278 struct crypt_dm_active_device dmd = {
279 .device = crypt_get_device_name(cd),
281 .uuid = crypt_get_uuid(cd),
283 .offset = crypt_get_data_offset(cd),
284 .iv_offset = crypt_get_iv_offset(cd),
289 r = device_check_and_adjust(cd, dmd.device,
290 (dmd.flags & CRYPT_ACTIVATE_SHARED) ? DEV_SHARED : DEV_EXCL,
291 &dmd.size, &dmd.offset, &flags);
295 if (crypt_get_cipher_mode(cd))
296 r = asprintf(&dm_cipher, "%s-%s", crypt_get_cipher(cd), crypt_get_cipher_mode(cd));
298 r = asprintf(&dm_cipher, "%s", crypt_get_cipher(cd));
302 dmd.cipher = dm_cipher;
303 log_dbg("Trying to activate PLAIN device %s using cipher %s.", name, dmd.cipher);
305 r = dm_create_device(name, CRYPT_PLAIN, &dmd, 0);
308 if (!cd->plain_uuid && dm_query_device(name, DM_ACTIVE_UUID, &dmd) >= 0)
309 cd->plain_uuid = CONST_CAST(char*)dmd.uuid;
315 int crypt_confirm(struct crypt_device *cd, const char *msg)
317 if (!cd || !cd->confirm)
320 return cd->confirm(msg, cd->confirm_usrptr);
323 static int key_from_terminal(struct crypt_device *cd, char *msg, char **key,
324 size_t *key_len, int force_verify)
330 if(!msg && asprintf(&prompt, _("Enter passphrase for %s: "),
331 cd->backing_file ?: crypt_get_device_name(cd)) < 0)
338 *key = crypt_safe_alloc(DEFAULT_PASSPHRASE_SIZE_MAX);
343 r = cd->password(msg, *key, DEFAULT_PASSPHRASE_SIZE_MAX,
344 cd->password_usrptr);
346 crypt_safe_free(*key);
351 r = crypt_get_key(msg, key, key_len, 0, NULL, cd->timeout,
352 (force_verify || cd->password_verify), cd);
355 return (r < 0) ? r: 0;
358 static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslot,
359 struct volume_key **vk)
361 char *passphrase_read = NULL;
362 size_t passphrase_size_read;
363 int r = -EINVAL, eperm = 0, tries = cd->tries;
367 crypt_free_volume_key(*vk);
370 r = key_from_terminal(cd, NULL, &passphrase_read,
371 &passphrase_size_read, 0);
375 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase_read,
376 passphrase_size_read, &cd->hdr, vk, cd);
379 crypt_safe_free(passphrase_read);
380 passphrase_read = NULL;
381 } while (r == -EPERM && (--tries > 0));
384 crypt_free_volume_key(*vk);
387 /* Report wrong passphrase if at least one try failed */
388 if (eperm && r == -EPIPE)
392 crypt_safe_free(passphrase_read);
396 static int key_from_file(struct crypt_device *cd, char *msg,
397 char **key, size_t *key_len,
398 const char *key_file, size_t key_size)
400 return crypt_get_key(msg, key, key_len, key_size, key_file,
404 void crypt_set_log_callback(struct crypt_device *cd,
405 void (*log)(int level, const char *msg, void *usrptr),
412 cd->log_usrptr = usrptr;
416 void crypt_set_confirm_callback(struct crypt_device *cd,
417 int (*confirm)(const char *msg, void *usrptr),
420 cd->confirm = confirm;
421 cd->confirm_usrptr = usrptr;
424 void crypt_set_password_callback(struct crypt_device *cd,
425 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
428 cd->password = password;
429 cd->password_usrptr = usrptr;
432 /* Deprecated global error interface */
433 void crypt_get_error(char *buf, size_t size)
435 if (!buf || size < 1)
436 global_error[0] = '\0';
437 else if (*global_error) {
438 strncpy(buf, global_error, size - 1);
439 buf[size - 1] = '\0';
440 global_error[0] = '\0';
445 const char *crypt_get_dir(void)
450 int crypt_init(struct crypt_device **cd, const char *device)
452 struct crypt_device *h = NULL;
458 log_dbg("Allocating crypt device %s context.", device);
460 if (!(h = malloc(sizeof(struct crypt_device))))
463 memset(h, 0, sizeof(*h));
467 r = device_ready(NULL, device, O_RDONLY);
469 h->device = crypt_loop_get_device();
470 log_dbg("Not a block device, %s%s.",
471 h->device ? "using free loop device " :
472 "no free loop device found",
475 log_err(NULL, _("Cannot find a free loopback device.\n"));
480 /* Keep the loop open, dettached on last close. */
481 h->loop_fd = crypt_loop_attach(h->device, device, 0, 1, &readonly);
482 if (h->loop_fd == -1) {
483 log_err(NULL, _("Attaching loopback device failed "
484 "(loop device with autoclear flag is required).\n"));
489 h->backing_file = crypt_loop_backing_file(h->device);
490 r = device_ready(NULL, h->device, O_RDONLY);
498 if (!h->device && device && !(h->device = strdup(device))) {
503 if (dm_init(h, 1) < 0) {
508 h->iteration_time = 1000;
509 h->password_verify = 0;
511 h->rng_type = crypt_random_default_key_rng();
517 if (h->loop_fd != -1)
520 free(h->backing_file);
526 static int crypt_check_data_device_size(struct crypt_device *cd)
529 uint64_t size, size_min;
531 /* Check data device size, require at least one sector */
532 size_min = crypt_get_data_offset(cd) << SECTOR_SHIFT ?: SECTOR_SIZE;
534 r = device_size(crypt_get_device_name(cd), &size);
538 if (size < size_min) {
539 log_err(cd, _("LUKS header detected but device %s is too small.\n"),
540 crypt_get_device_name(cd));
547 int crypt_set_data_device(struct crypt_device *cd, const char *device)
552 log_dbg("Setting ciphertext data device to %s.", device ?: "(none)");
554 if (!isLUKS(cd->type)) {
555 log_err(cd, _("This operation is not supported for this device type.\n"));
559 /* metadata device must be set */
563 r = device_ready(NULL, device, O_RDONLY);
567 if (!(data_device = strdup(device)))
570 if (!cd->metadata_device)
571 cd->metadata_device = cd->device;
575 cd->device = data_device;
577 return crypt_check_data_device_size(cd);
580 int crypt_init_by_name_and_header(struct crypt_device **cd,
582 const char *header_device)
584 crypt_status_info ci;
585 struct crypt_dm_active_device dmd;
586 char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
590 log_dbg("Allocating crypt device context by device %s.", name);
592 ci = crypt_status(NULL, name);
593 if (ci == CRYPT_INVALID)
596 if (ci < CRYPT_ACTIVE) {
597 log_err(NULL, _("Device %s is not active.\n"), name);
601 r = dm_query_device(name, DM_ACTIVE_DEVICE | DM_ACTIVE_CIPHER |
602 DM_ACTIVE_UUID | DM_ACTIVE_KEYSIZE, &dmd);
609 r = crypt_init(cd, header_device);
611 r = crypt_init(cd, dmd.device);
613 /* Underlying device disappeared but mapping still active */
614 if (!dmd.device || r == -ENOTBLK)
615 log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
618 /* Underlying device is not readable but crypt mapping exists */
620 free(CONST_CAST(void*)dmd.device);
622 r = crypt_init(cd, NULL);
630 if (!strncmp(CRYPT_PLAIN, dmd.uuid, sizeof(CRYPT_PLAIN)-1))
631 (*cd)->type = strdup(CRYPT_PLAIN);
632 else if (!strncmp(CRYPT_LOOPAES, dmd.uuid, sizeof(CRYPT_LOOPAES)-1))
633 (*cd)->type = strdup(CRYPT_LOOPAES);
634 else if (!strncmp(CRYPT_LUKS1, dmd.uuid, sizeof(CRYPT_LUKS1)-1))
635 (*cd)->type = strdup(CRYPT_LUKS1);
637 log_dbg("Unknown UUID set, some parameters are not set.");
639 log_dbg("Active device has no UUID set, some parameters are not set.");
642 r = crypt_set_data_device(*cd, dmd.device);
647 /* Try to initialise basic parameters from active device */
649 if (!(*cd)->backing_file && dmd.device && crypt_loop_device(dmd.device) &&
650 !((*cd)->backing_file = crypt_loop_backing_file(dmd.device))) {
655 if (isPLAIN((*cd)->type)) {
656 (*cd)->plain_uuid = strdup(dmd.uuid);
657 (*cd)->plain_hdr.hash = NULL; /* no way to get this */
658 (*cd)->plain_hdr.offset = dmd.offset;
659 (*cd)->plain_hdr.skip = dmd.iv_offset;
661 r = crypt_parse_name_and_mode(dmd.cipher, cipher, NULL, cipher_mode);
663 (*cd)->plain_cipher = strdup(cipher);
664 (*cd)->plain_cipher_mode = strdup(cipher_mode);
666 } else if (isLOOPAES((*cd)->type)) {
667 (*cd)->loopaes_uuid = strdup(dmd.uuid);
668 (*cd)->loopaes_hdr.offset = dmd.offset;
670 r = crypt_parse_name_and_mode(dmd.cipher, cipher,
671 &key_nums, cipher_mode);
673 (*cd)->loopaes_cipher = strdup(cipher);
674 (*cd)->loopaes_cipher_mode = strdup(cipher_mode);
675 /* version 3 uses last key for IV */
676 if (dmd.vk->keylength % key_nums)
678 (*cd)->loopaes_key_size = dmd.vk->keylength / key_nums;
680 } else if (isLUKS((*cd)->type)) {
681 if (mdata_device(*cd)) {
682 r = crypt_load(*cd, CRYPT_LUKS1, NULL);
684 log_dbg("LUKS device header does not match active device.");
690 /* checks whether UUIDs match each other */
691 r = crypt_uuid_cmp(dmd.uuid, (*cd)->hdr.uuid);
693 log_dbg("LUKS device header uuid: %s mismatches DM returned uuid %s",
694 (*cd)->hdr.uuid, dmd.uuid);
708 crypt_free_volume_key(dmd.vk);
709 free(CONST_CAST(void*)dmd.device);
710 free(CONST_CAST(void*)dmd.cipher);
711 free(CONST_CAST(void*)dmd.uuid);
715 int crypt_init_by_name(struct crypt_device **cd, const char *name)
717 return crypt_init_by_name_and_header(cd, name, NULL);
720 static int _crypt_format_plain(struct crypt_device *cd,
722 const char *cipher_mode,
724 size_t volume_key_size,
725 struct crypt_params_plain *params)
727 if (!cipher || !cipher_mode) {
728 log_err(cd, _("Invalid plain crypt parameters.\n"));
732 if (volume_key_size > 1024) {
733 log_err(cd, _("Invalid key size.\n"));
737 cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
741 cd->plain_cipher = strdup(cipher);
742 cd->plain_cipher_mode = strdup(cipher_mode);
745 cd->plain_uuid = strdup(uuid);
747 if (params && params->hash)
748 cd->plain_hdr.hash = strdup(params->hash);
750 cd->plain_hdr.offset = params ? params->offset : 0;
751 cd->plain_hdr.skip = params ? params->skip : 0;
752 cd->plain_hdr.size = params ? params->size : 0;
754 if (!cd->plain_cipher || !cd->plain_cipher_mode)
760 static int _crypt_format_luks1(struct crypt_device *cd,
762 const char *cipher_mode,
764 const char *volume_key,
765 size_t volume_key_size,
766 struct crypt_params_luks1 *params)
769 unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
770 unsigned long alignment_offset = 0;
772 if (!mdata_device(cd)) {
773 log_err(cd, _("Can't format LUKS without device.\n"));
778 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
781 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
786 if (params && params->data_device) {
787 cd->metadata_device = cd->device;
788 if (!(cd->device = strdup(params->data_device)))
790 required_alignment = params->data_alignment * SECTOR_SIZE;
791 } else if (params && params->data_alignment) {
792 required_alignment = params->data_alignment * SECTOR_SIZE;
794 get_topology_alignment(cd->device, &required_alignment,
795 &alignment_offset, DEFAULT_DISK_ALIGNMENT);
797 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
798 (params && params->hash) ? params->hash : "sha1",
800 required_alignment / SECTOR_SIZE,
801 alignment_offset / SECTOR_SIZE,
802 cd->iteration_time, &cd->PBKDF2_per_sec,
803 cd->metadata_device, cd);
807 /* Wipe first 8 sectors - fs magic numbers etc. */
808 r = crypt_wipe(mdata_device(cd), 0, 8 * SECTOR_SIZE, CRYPT_WIPE_ZERO, 1);
811 log_err(cd, _("Cannot format device %s which is still in use.\n"),
814 log_err(cd, _("Cannot wipe header on device %s.\n"),
820 r = LUKS_write_phdr(mdata_device(cd), &cd->hdr, cd);
825 static int _crypt_format_loopaes(struct crypt_device *cd,
828 size_t volume_key_size,
829 struct crypt_params_loopaes *params)
831 if (!mdata_device(cd)) {
832 log_err(cd, _("Can't format LOOPAES without device.\n"));
836 if (volume_key_size > 1024) {
837 log_err(cd, _("Invalid key size.\n"));
841 cd->loopaes_key_size = volume_key_size;
843 cd->loopaes_cipher = strdup(cipher ?: DEFAULT_LOOPAES_CIPHER);
846 cd->loopaes_uuid = strdup(uuid);
848 if (params && params->hash)
849 cd->loopaes_hdr.hash = strdup(params->hash);
851 cd->loopaes_hdr.offset = params ? params->offset : 0;
852 cd->loopaes_hdr.skip = params ? params->skip : 0;
857 int crypt_format(struct crypt_device *cd,
860 const char *cipher_mode,
862 const char *volume_key,
863 size_t volume_key_size,
872 log_dbg("Context already formatted as %s.", cd->type);
876 log_dbg("Formatting device %s as type %s.", mdata_device(cd) ?: "(none)", type);
883 r = _crypt_format_plain(cd, cipher, cipher_mode,
884 uuid, volume_key_size, params);
885 else if (isLUKS(type))
886 r = _crypt_format_luks1(cd, cipher, cipher_mode,
887 uuid, volume_key, volume_key_size, params);
888 else if (isLOOPAES(type))
889 r = _crypt_format_loopaes(cd, cipher, uuid, volume_key_size, params);
891 /* FIXME: allow plugins here? */
892 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
896 if (!r && !(cd->type = strdup(type)))
900 crypt_free_volume_key(cd->volume_key);
901 cd->volume_key = NULL;
907 int crypt_load(struct crypt_device *cd,
908 const char *requested_type,
909 void *params __attribute__((unused)))
911 struct luks_phdr hdr;
914 log_dbg("Trying to load %s crypt type from device %s.",
915 requested_type ?: "any", mdata_device(cd) ?: "(none)");
917 if (!mdata_device(cd))
920 if (requested_type && !isLUKS(requested_type))
923 if (cd->type && !isLUKS(cd->type)) {
924 log_dbg("Context is already initialised to type %s", cd->type);
932 r = LUKS_read_phdr(mdata_device(cd), &hdr, 1, cd);
936 if (!cd->type && !(cd->type = strdup(CRYPT_LUKS1)))
939 memcpy(&cd->hdr, &hdr, sizeof(hdr));
941 /* cd->type and header must be set in context */
942 r = crypt_check_data_device_size(cd);
951 int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
953 struct crypt_dm_active_device dmd;
956 /* Device context type must be initialised */
957 if (!cd->type || !crypt_get_uuid(cd))
960 log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
962 r = dm_query_device(name, DM_ACTIVE_DEVICE | DM_ACTIVE_CIPHER |
963 DM_ACTIVE_UUID | DM_ACTIVE_KEYSIZE |
964 DM_ACTIVE_KEY, &dmd);
966 log_err(NULL, _("Device %s is not active.\n"), name);
975 r = device_check_and_adjust(cd, dmd.device, DEV_OK, &new_size, &dmd.offset, &dmd.flags);
979 if (new_size == dmd.size) {
980 log_dbg("Device has already requested size %" PRIu64
981 " sectors.", dmd.size);
985 r = dm_create_device(name, cd->type, &dmd, 1);
988 crypt_free_volume_key(dmd.vk);
989 free(CONST_CAST(void*)dmd.cipher);
990 free(CONST_CAST(void*)dmd.device);
991 free(CONST_CAST(void*)dmd.uuid);
996 int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
998 if (!isLUKS(cd->type)) {
999 log_err(cd, _("This operation is not supported for this device type.\n"));
1003 if (uuid && !strncmp(uuid, cd->hdr.uuid, sizeof(cd->hdr.uuid))) {
1004 log_dbg("UUID is the same as requested (%s) for device %s.",
1005 uuid, mdata_device(cd));
1010 log_dbg("Requested new UUID change to %s for %s.", uuid, mdata_device(cd));
1012 log_dbg("Requested new UUID refresh for %s.", mdata_device(cd));
1014 if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
1017 return LUKS_hdr_uuid_set(mdata_device(cd), &cd->hdr, uuid, cd);
1020 int crypt_header_backup(struct crypt_device *cd,
1021 const char *requested_type,
1022 const char *backup_file)
1026 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1029 r = init_crypto(cd);
1033 log_dbg("Requested header backup of device %s (%s) to "
1034 "file %s.", mdata_device(cd), requested_type, backup_file);
1036 return LUKS_hdr_backup(backup_file, mdata_device(cd), &cd->hdr, cd);
1039 int crypt_header_restore(struct crypt_device *cd,
1040 const char *requested_type,
1041 const char *backup_file)
1045 if (requested_type && !isLUKS(requested_type))
1048 /* Some hash functions need initialized gcrypt library */
1049 r = init_crypto(cd);
1053 log_dbg("Requested header restore to device %s (%s) from "
1054 "file %s.", mdata_device(cd), requested_type, backup_file);
1056 return LUKS_hdr_restore(backup_file, mdata_device(cd), &cd->hdr, cd);
1059 void crypt_free(struct crypt_device *cd)
1062 log_dbg("Releasing crypt device %s context.", mdata_device(cd));
1064 if (cd->loop_fd != -1)
1068 crypt_free_volume_key(cd->volume_key);
1071 free(cd->metadata_device);
1072 free(cd->backing_file);
1075 /* used in plain device only */
1076 free(CONST_CAST(void*)cd->plain_hdr.hash);
1077 free(cd->plain_cipher);
1078 free(cd->plain_cipher_mode);
1079 free(cd->plain_uuid);
1081 /* used in loop-AES device only */
1082 free(CONST_CAST(void*)cd->loopaes_hdr.hash);
1083 free(cd->loopaes_cipher);
1084 free(cd->loopaes_uuid);
1090 int crypt_suspend(struct crypt_device *cd,
1093 crypt_status_info ci;
1096 log_dbg("Suspending volume %s.", name);
1098 if (!isLUKS(cd->type)) {
1099 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1104 ci = crypt_status(NULL, name);
1105 if (ci < CRYPT_ACTIVE) {
1106 log_err(cd, _("Volume %s is not active.\n"), name);
1110 if (!cd && dm_init(NULL, 1) < 0)
1113 r = dm_status_suspended(name);
1118 log_err(cd, _("Volume %s is already suspended.\n"), name);
1123 r = dm_suspend_and_wipe_key(name);
1125 log_err(cd, "Suspend is not supported for device %s.\n", name);
1127 log_err(cd, "Error during suspending device %s.\n", name);
1134 int crypt_resume_by_passphrase(struct crypt_device *cd,
1137 const char *passphrase,
1138 size_t passphrase_size)
1140 struct volume_key *vk = NULL;
1143 log_dbg("Resuming volume %s.", name);
1145 if (!isLUKS(cd->type)) {
1146 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1151 r = dm_status_suspended(name);
1156 log_err(cd, _("Volume %s is not suspended.\n"), name);
1161 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase,
1162 passphrase_size, &cd->hdr, &vk, cd);
1164 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1168 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1170 log_err(cd, "Resume is not supported for device %s.\n", name);
1172 log_err(cd, "Error during resuming device %s.\n", name);
1176 crypt_free_volume_key(vk);
1177 return r < 0 ? r : keyslot;
1180 int crypt_resume_by_keyfile(struct crypt_device *cd,
1183 const char *keyfile,
1184 size_t keyfile_size)
1186 struct volume_key *vk = NULL;
1187 char *passphrase_read = NULL;
1188 size_t passphrase_size_read;
1191 log_dbg("Resuming volume %s.", name);
1193 if (!isLUKS(cd->type)) {
1194 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1199 r = dm_status_suspended(name);
1204 log_err(cd, _("Volume %s is not suspended.\n"), name);
1211 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1212 &passphrase_size_read, keyfile, keyfile_size);
1216 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase_read,
1217 passphrase_size_read, &cd->hdr, &vk, cd);
1222 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1224 log_err(cd, "Error during resuming device %s.\n", name);
1226 crypt_safe_free(passphrase_read);
1227 crypt_free_volume_key(vk);
1228 return r < 0 ? r : keyslot;
1231 // slot manipulation
1232 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1233 int keyslot, // -1 any
1234 const char *passphrase, // NULL -> terminal
1235 size_t passphrase_size,
1236 const char *new_passphrase, // NULL -> terminal
1237 size_t new_passphrase_size)
1239 struct volume_key *vk = NULL;
1240 char *password = NULL, *new_password = NULL;
1241 size_t passwordLen, new_passwordLen;
1244 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1245 "new passphrase %sprovided.",
1246 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1248 if (!isLUKS(cd->type)) {
1249 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1253 r = keyslot_verify_or_find_empty(cd, &keyslot);
1257 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1258 /* No slots used, try to use pre-generated key in header */
1259 if (cd->volume_key) {
1260 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1261 r = vk ? 0 : -ENOMEM;
1263 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1266 } else if (passphrase) {
1267 /* Passphrase provided, use it to unlock existing keyslot */
1268 r = LUKS_open_key_with_hdr(mdata_device(cd), CRYPT_ANY_SLOT, passphrase,
1269 passphrase_size, &cd->hdr, &vk, cd);
1271 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1272 r = key_from_terminal(cd, _("Enter any passphrase: "),
1273 &password, &passwordLen, 0);
1277 r = LUKS_open_key_with_hdr(mdata_device(cd), CRYPT_ANY_SLOT, password,
1278 passwordLen, &cd->hdr, &vk, cd);
1279 crypt_safe_free(password);
1285 if (new_passphrase) {
1286 new_password = CONST_CAST(char*)new_passphrase;
1287 new_passwordLen = new_passphrase_size;
1289 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1290 &new_password, &new_passwordLen, 1);
1295 r = LUKS_set_key(mdata_device(cd), keyslot, new_password, new_passwordLen,
1296 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1301 if (!new_passphrase)
1302 crypt_safe_free(new_password);
1303 crypt_free_volume_key(vk);
1304 return r ?: keyslot;
1307 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1309 const char *keyfile,
1310 size_t keyfile_size,
1311 const char *new_keyfile,
1312 size_t new_keyfile_size)
1314 struct volume_key *vk = NULL;
1315 char *password = NULL; size_t passwordLen;
1316 char *new_password = NULL; size_t new_passwordLen;
1319 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1320 keyfile ?: "[none]", new_keyfile ?: "[none]");
1322 if (!isLUKS(cd->type)) {
1323 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1327 r = keyslot_verify_or_find_empty(cd, &keyslot);
1331 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1332 /* No slots used, try to use pre-generated key in header */
1333 if (cd->volume_key) {
1334 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1335 r = vk ? 0 : -ENOMEM;
1337 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1341 /* Read password from file of (if NULL) from terminal */
1343 r = key_from_file(cd, _("Enter any passphrase: "),
1344 &password, &passwordLen,
1345 keyfile, keyfile_size);
1347 r = key_from_terminal(cd, _("Enter any passphrase: "),
1348 &password, &passwordLen, 0);
1352 r = LUKS_open_key_with_hdr(mdata_device(cd), CRYPT_ANY_SLOT, password, passwordLen,
1360 r = key_from_file(cd, _("Enter new passphrase for key slot: "),
1361 &new_password, &new_passwordLen, new_keyfile,
1364 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1365 &new_password, &new_passwordLen, 1);
1369 r = LUKS_set_key(mdata_device(cd), keyslot, new_password, new_passwordLen,
1370 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1372 crypt_safe_free(password);
1373 crypt_safe_free(new_password);
1374 crypt_free_volume_key(vk);
1375 return r < 0 ? r : keyslot;
1378 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1380 const char *volume_key,
1381 size_t volume_key_size,
1382 const char *passphrase,
1383 size_t passphrase_size)
1385 struct volume_key *vk = NULL;
1387 char *new_password = NULL; size_t new_passwordLen;
1389 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1391 if (!isLUKS(cd->type)) {
1392 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1397 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1398 else if (cd->volume_key)
1399 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1404 r = LUKS_verify_volume_key(&cd->hdr, vk);
1406 log_err(cd, _("Volume key does not match the volume.\n"));
1410 r = keyslot_verify_or_find_empty(cd, &keyslot);
1415 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1416 &new_password, &new_passwordLen, 1);
1419 passphrase = new_password;
1420 passphrase_size = new_passwordLen;
1423 r = LUKS_set_key(mdata_device(cd), keyslot, passphrase, passphrase_size,
1424 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1426 crypt_safe_free(new_password);
1427 crypt_free_volume_key(vk);
1428 return (r < 0) ? r : keyslot;
1431 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1433 crypt_keyslot_info ki;
1435 log_dbg("Destroying keyslot %d.", keyslot);
1437 if (!isLUKS(cd->type)) {
1438 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1442 ki = crypt_keyslot_status(cd, keyslot);
1443 if (ki == CRYPT_SLOT_INVALID) {
1444 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1448 if (ki == CRYPT_SLOT_INACTIVE) {
1449 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1453 return LUKS_del_key(mdata_device(cd), keyslot, &cd->hdr, cd);
1456 // activation/deactivation of device mapping
1457 int crypt_activate_by_passphrase(struct crypt_device *cd,
1460 const char *passphrase,
1461 size_t passphrase_size,
1464 crypt_status_info ci;
1465 struct volume_key *vk = NULL;
1466 char *read_passphrase = NULL;
1467 size_t passphraseLen = 0;
1470 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1471 name ? "Activating" : "Checking", name ?: "",
1472 keyslot, passphrase ? "" : "[none] ");
1475 ci = crypt_status(NULL, name);
1476 if (ci == CRYPT_INVALID)
1478 else if (ci >= CRYPT_ACTIVE) {
1479 log_err(cd, _("Device %s already exists.\n"), name);
1484 /* plain, use hashed passphrase */
1485 if (isPLAIN(cd->type)) {
1490 r = key_from_terminal(cd, NULL, &read_passphrase,
1494 passphrase = read_passphrase;
1495 passphrase_size = passphraseLen;
1498 r = process_key(cd, cd->plain_hdr.hash,
1499 cd->volume_key->keylength,
1500 passphrase, passphrase_size, &vk);
1504 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1506 } else if (isLUKS(cd->type)) {
1507 /* provided passphrase, do not retry */
1509 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase,
1510 passphrase_size, &cd->hdr, &vk, cd);
1512 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1517 r = LUKS1_activate(cd, name, vk, flags);
1522 crypt_safe_free(read_passphrase);
1523 crypt_free_volume_key(vk);
1525 return r < 0 ? r : keyslot;
1528 int crypt_activate_by_keyfile(struct crypt_device *cd,
1531 const char *keyfile,
1532 size_t keyfile_size,
1535 crypt_status_info ci;
1536 struct volume_key *vk = NULL;
1537 char *passphrase_read = NULL;
1538 size_t passphrase_size_read;
1539 unsigned int key_count = 0;
1542 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1543 name ?: "", keyslot, keyfile ?: "[none]");
1546 ci = crypt_status(NULL, name);
1547 if (ci == CRYPT_INVALID)
1549 else if (ci >= CRYPT_ACTIVE) {
1550 log_err(cd, _("Device %s already exists.\n"), name);
1558 if (isPLAIN(cd->type)) {
1562 r = key_from_file(cd, _("Enter passphrase: "),
1563 &passphrase_read, &passphrase_size_read,
1564 keyfile, keyfile_size);
1568 r = process_key(cd, cd->plain_hdr.hash,
1569 cd->volume_key->keylength,
1570 passphrase_read, passphrase_size_read, &vk);
1574 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1575 } else if (isLUKS(cd->type)) {
1576 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1577 &passphrase_size_read, keyfile, keyfile_size);
1580 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase_read,
1581 passphrase_size_read, &cd->hdr, &vk, cd);
1587 r = LUKS1_activate(cd, name, vk, flags);
1592 } else if (isLOOPAES(cd->type)) {
1593 r = key_from_file(cd, NULL, &passphrase_read, &passphrase_size_read,
1594 keyfile, keyfile_size);
1597 r = LOOPAES_parse_keyfile(cd, &vk, cd->loopaes_hdr.hash, &key_count,
1598 passphrase_read, passphrase_size_read);
1602 r = LOOPAES_activate(cd, name, cd->loopaes_cipher,
1603 key_count, vk, flags);
1608 crypt_safe_free(passphrase_read);
1609 crypt_free_volume_key(vk);
1614 int crypt_activate_by_volume_key(struct crypt_device *cd,
1616 const char *volume_key,
1617 size_t volume_key_size,
1620 crypt_status_info ci;
1621 struct volume_key *vk = NULL;
1624 log_dbg("Activating volume %s by volume key.", name);
1627 ci = crypt_status(NULL, name);
1628 if (ci == CRYPT_INVALID)
1630 else if (ci >= CRYPT_ACTIVE) {
1631 log_err(cd, _("Device %s already exists.\n"), name);
1636 /* use key directly, no hash */
1637 if (isPLAIN(cd->type)) {
1641 if (!volume_key || !volume_key_size || !cd->volume_key ||
1642 volume_key_size != cd->volume_key->keylength) {
1643 log_err(cd, _("Incorrect volume key specified for plain device.\n"));
1647 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1651 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1652 } else if (isLUKS(cd->type)) {
1653 /* If key is not provided, try to use internal key */
1655 if (!cd->volume_key) {
1656 log_err(cd, _("Volume key does not match the volume.\n"));
1659 volume_key_size = cd->volume_key->keylength;
1660 volume_key = cd->volume_key->key;
1663 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1666 r = LUKS_verify_volume_key(&cd->hdr, vk);
1669 log_err(cd, _("Volume key does not match the volume.\n"));
1672 r = LUKS1_activate(cd, name, vk, flags);
1674 log_err(cd, _("Device type is not properly initialised.\n"));
1676 crypt_free_volume_key(vk);
1681 int crypt_deactivate(struct crypt_device *cd, const char *name)
1688 log_dbg("Deactivating volume %s.", name);
1690 if (!cd && dm_init(NULL, 1) < 0)
1693 switch (crypt_status(cd, name)) {
1695 r = dm_remove_device(name, 0, 0);
1698 log_err(cd, _("Device %s is busy.\n"), name);
1701 case CRYPT_INACTIVE:
1702 log_err(cd, _("Device %s is not active.\n"), name);
1706 log_err(cd, _("Invalid device %s.\n"), name);
1716 int crypt_volume_key_get(struct crypt_device *cd,
1719 size_t *volume_key_size,
1720 const char *passphrase,
1721 size_t passphrase_size)
1723 struct volume_key *vk = NULL;
1727 key_len = crypt_get_volume_key_size(cd);
1728 if (key_len > *volume_key_size) {
1729 log_err(cd, _("Volume key buffer too small.\n"));
1733 if (isPLAIN(cd->type) && cd->plain_hdr.hash) {
1734 r = process_key(cd, cd->plain_hdr.hash, key_len,
1735 passphrase, passphrase_size, &vk);
1737 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
1738 } else if (isLUKS(cd->type)) {
1739 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase,
1740 passphrase_size, &cd->hdr, &vk, cd);
1743 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
1746 memcpy(volume_key, vk->key, vk->keylength);
1747 *volume_key_size = vk->keylength;
1750 crypt_free_volume_key(vk);
1754 int crypt_volume_key_verify(struct crypt_device *cd,
1755 const char *volume_key,
1756 size_t volume_key_size)
1758 struct volume_key *vk;
1761 if (!isLUKS(cd->type)) {
1762 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1766 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1770 r = LUKS_verify_volume_key(&cd->hdr, vk);
1773 log_err(cd, _("Volume key does not match the volume.\n"));
1775 crypt_free_volume_key(vk);
1780 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
1782 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
1783 cd->timeout = timeout_sec;
1786 void crypt_set_password_retry(struct crypt_device *cd, int tries)
1788 log_dbg("Password retry count set to %d.", tries);
1792 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
1794 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
1795 cd->iteration_time = iteration_time_ms;
1798 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
1800 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
1801 cd->password_verify = password_verify ? 1 : 0;
1804 void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
1807 case CRYPT_RNG_URANDOM:
1808 case CRYPT_RNG_RANDOM:
1809 log_dbg("RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
1810 cd->rng_type = rng_type;
1814 int crypt_get_rng_type(struct crypt_device *cd)
1819 return cd->rng_type;
1822 int crypt_memory_lock(struct crypt_device *cd, int lock)
1824 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
1828 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
1832 if (!cd && dm_init(NULL, 1) < 0)
1833 return CRYPT_INVALID;
1835 r = dm_status_device(name);
1840 if (r < 0 && r != -ENODEV)
1841 return CRYPT_INVALID;
1844 return CRYPT_ACTIVE;
1849 return CRYPT_INACTIVE;
1852 static void hexprintICB(struct crypt_device *cd, char *d, int n)
1855 for(i = 0; i < n; i++)
1856 log_std(cd, "%02hhx ", (char)d[i]);
1859 int crypt_dump(struct crypt_device *cd)
1862 if (!isLUKS(cd->type)) { //FIXME
1863 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1867 log_std(cd, "LUKS header information for %s\n\n", mdata_device(cd));
1868 log_std(cd, "Version: \t%d\n", cd->hdr.version);
1869 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
1870 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
1871 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec);
1872 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
1873 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
1874 log_std(cd, "MK digest: \t");
1875 hexprintICB(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE);
1877 log_std(cd, "MK salt: \t");
1878 hexprintICB(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2);
1879 log_std(cd, "\n \t");
1880 hexprintICB(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
1882 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
1883 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
1884 for(i = 0; i < LUKS_NUMKEYS; i++) {
1885 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
1886 log_std(cd, "Key Slot %d: ENABLED\n",i);
1887 log_std(cd, "\tIterations: \t%d\n",
1888 cd->hdr.keyblock[i].passwordIterations);
1889 log_std(cd, "\tSalt: \t");
1890 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt,
1892 log_std(cd, "\n\t \t");
1893 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt +
1894 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
1897 log_std(cd, "\tKey material offset:\t%d\n",
1898 cd->hdr.keyblock[i].keyMaterialOffset);
1899 log_std(cd, "\tAF stripes: \t%d\n",
1900 cd->hdr.keyblock[i].stripes);
1903 log_std(cd, "Key Slot %d: DISABLED\n", i);
1909 const char *crypt_get_cipher(struct crypt_device *cd)
1911 if (isPLAIN(cd->type))
1912 return cd->plain_cipher;
1914 if (isLUKS(cd->type))
1915 return cd->hdr.cipherName;
1917 if (isLOOPAES(cd->type))
1918 return cd->loopaes_cipher;
1923 const char *crypt_get_cipher_mode(struct crypt_device *cd)
1925 if (isPLAIN(cd->type))
1926 return cd->plain_cipher_mode;
1928 if (isLUKS(cd->type))
1929 return cd->hdr.cipherMode;
1931 if (isLOOPAES(cd->type))
1932 return cd->loopaes_cipher_mode;
1937 const char *crypt_get_uuid(struct crypt_device *cd)
1939 if (isLUKS(cd->type))
1940 return cd->hdr.uuid;
1942 if (isPLAIN(cd->type))
1943 return cd->plain_uuid;
1945 if (isLOOPAES(cd->type))
1946 return cd->loopaes_uuid;
1951 const char *crypt_get_device_name(struct crypt_device *cd)
1957 int crypt_get_volume_key_size(struct crypt_device *cd)
1959 if (isPLAIN(cd->type) && cd->volume_key)
1960 return cd->volume_key->keylength;
1962 if (isLUKS(cd->type))
1963 return cd->hdr.keyBytes;
1965 if (isLOOPAES(cd->type))
1966 return cd->loopaes_key_size;
1971 uint64_t crypt_get_data_offset(struct crypt_device *cd)
1973 if (isPLAIN(cd->type))
1974 return cd->plain_hdr.offset;
1976 if (isLUKS(cd->type))
1977 return cd->hdr.payloadOffset;
1979 if (isLOOPAES(cd->type))
1980 return cd->loopaes_hdr.offset;
1985 uint64_t crypt_get_iv_offset(struct crypt_device *cd)
1987 if (isPLAIN(cd->type))
1988 return cd->plain_hdr.skip;
1990 if (isLUKS(cd->type))
1993 if (isLOOPAES(cd->type))
1994 return cd->loopaes_hdr.skip;
1999 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
2001 if (!isLUKS(cd->type)) {
2002 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2003 return CRYPT_SLOT_INVALID;
2006 return LUKS_keyslot_info(&cd->hdr, keyslot);
2009 int crypt_keyslot_max(const char *type)
2011 if (type && isLUKS(type))
2012 return LUKS_NUMKEYS;
2017 const char *crypt_get_type(struct crypt_device *cd)
2022 int crypt_get_active_device(struct crypt_device *cd __attribute__((unused)),
2024 struct crypt_active_device *cad)
2026 struct crypt_dm_active_device dmd;
2029 r = dm_query_device(name, 0, &dmd);
2033 cad->offset = dmd.offset;
2034 cad->iv_offset = dmd.iv_offset;
2035 cad->size = dmd.size;
2036 cad->flags = dmd.flags;