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-2012, 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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;
59 unsigned int plain_key_size;
61 /* used in CRYPT_LOOPAES */
62 struct crypt_params_loopaes loopaes_hdr;
64 char *loopaes_cipher_mode;
66 unsigned int loopaes_key_size;
68 /* callbacks definitions */
69 void (*log)(int level, const char *msg, void *usrptr);
71 int (*confirm)(const char *msg, void *usrptr);
73 int (*password)(const char *msg, char *buf, size_t length, void *usrptr);
74 void *password_usrptr;
76 /* last error message */
77 char error[MAX_ERROR_LENGTH];
81 /* FIXME: not thread safe, remove this later */
82 static char global_error[MAX_ERROR_LENGTH] = {0};
85 static void (*_default_log)(int level, const char *msg, void *usrptr) = NULL;
86 static int _debug_level = 0;
88 void crypt_set_debug_level(int level)
93 int crypt_get_debug_level(void)
98 static void crypt_set_error(struct crypt_device *cd, const char *error)
100 size_t size = strlen(error);
102 /* Set global error, ugly hack... */
103 strncpy(global_error, error, MAX_ERROR_LENGTH - 2);
104 if (size < MAX_ERROR_LENGTH && global_error[size - 1] == '\n')
105 global_error[size - 1] = '\0';
107 /* Set error string per context */
109 strncpy(cd->error, error, MAX_ERROR_LENGTH - 2);
110 if (size < MAX_ERROR_LENGTH && cd->error[size - 1] == '\n')
111 cd->error[size - 1] = '\0';
115 void crypt_log(struct crypt_device *cd, int level, const char *msg)
118 cd->log(level, msg, cd->log_usrptr);
119 else if (_default_log)
120 _default_log(level, msg, NULL);
122 if (level == CRYPT_LOG_ERROR)
123 crypt_set_error(cd, msg);
126 __attribute__((format(printf, 5, 6)))
127 void logger(struct crypt_device *cd, int level, const char *file,
128 int line, const char *format, ...)
133 va_start(argp, format);
135 if (vasprintf(&target, format, argp) > 0 ) {
137 crypt_log(cd, level, target);
139 } else if (_debug_level)
140 printf("# %s:%d %s\n", file ?: "?", line, target);
142 } else if (_debug_level)
143 printf("# %s\n", target);
151 static const char *mdata_device(struct crypt_device *cd)
153 return cd->metadata_device ?: cd->device;
156 static int init_crypto(struct crypt_device *ctx)
160 r = crypt_random_init(ctx);
162 log_err(ctx, _("Cannot initialize crypto RNG backend.\n"));
166 r = crypt_backend_init(ctx);
168 log_err(ctx, _("Cannot initialize crypto backend.\n"));
173 static int process_key(struct crypt_device *cd, const char *hash_name,
174 size_t key_size, const char *pass, size_t passLen,
175 struct volume_key **vk)
182 *vk = crypt_alloc_volume_key(key_size, NULL);
187 r = crypt_plain_hash(cd, hash_name, (*vk)->key, key_size, pass, passLen);
190 log_err(cd, _("Hash algorithm %s not supported.\n"),
193 log_err(cd, _("Key processing error (using hash %s).\n"),
195 crypt_free_volume_key(*vk);
199 } else if (passLen > key_size) {
200 memcpy((*vk)->key, pass, key_size);
202 memcpy((*vk)->key, pass, passLen);
208 static int isPLAIN(const char *type)
210 return (type && !strcmp(CRYPT_PLAIN, type));
213 static int isLUKS(const char *type)
215 return (type && !strcmp(CRYPT_LUKS1, type));
218 static int isLOOPAES(const char *type)
220 return (type && !strcmp(CRYPT_LOOPAES, type));
223 /* keyslot helpers */
224 static int keyslot_verify_or_find_empty(struct crypt_device *cd, int *keyslot)
226 if (*keyslot == CRYPT_ANY_SLOT) {
227 *keyslot = LUKS_keyslot_find_empty(&cd->hdr);
229 log_err(cd, _("All key slots full.\n"));
234 switch (LUKS_keyslot_info(&cd->hdr, *keyslot)) {
235 case CRYPT_SLOT_INVALID:
236 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
237 *keyslot, LUKS_NUMKEYS - 1);
239 case CRYPT_SLOT_INACTIVE:
242 log_err(cd, _("Key slot %d is full, please select another one.\n"),
251 * compares UUIDs returned by device-mapper (striped by cryptsetup) and uuid in header
253 static int crypt_uuid_cmp(const char *dm_uuid, const char *hdr_uuid)
258 if (!dm_uuid || !hdr_uuid)
261 str = strchr(dm_uuid, '-');
265 for (i = 0, j = 1; hdr_uuid[i]; i++) {
266 if (hdr_uuid[i] == '-')
269 if (!str[j] || str[j] == '-')
272 if (str[j] != hdr_uuid[i])
280 int PLAIN_activate(struct crypt_device *cd,
282 struct volume_key *vk,
287 char *dm_cipher = NULL;
288 enum devcheck device_check;
289 struct crypt_dm_active_device dmd = {
290 .device = crypt_get_device_name(cd),
292 .uuid = crypt_get_uuid(cd),
294 .offset = crypt_get_data_offset(cd),
295 .iv_offset = crypt_get_iv_offset(cd),
300 if (dmd.flags & CRYPT_ACTIVATE_SHARED)
301 device_check = DEV_SHARED;
303 device_check = DEV_EXCL;
305 r = device_check_and_adjust(cd, dmd.device, device_check,
306 &dmd.size, &dmd.offset, &flags);
310 if (crypt_get_cipher_mode(cd))
311 r = asprintf(&dm_cipher, "%s-%s", crypt_get_cipher(cd), crypt_get_cipher_mode(cd));
313 r = asprintf(&dm_cipher, "%s", crypt_get_cipher(cd));
317 dmd.cipher = dm_cipher;
318 log_dbg("Trying to activate PLAIN device %s using cipher %s.", name, dmd.cipher);
320 r = dm_create_device(name, CRYPT_PLAIN, &dmd, 0);
323 if (!cd->plain_uuid && dm_query_device(name, DM_ACTIVE_UUID, &dmd) >= 0)
324 cd->plain_uuid = CONST_CAST(char*)dmd.uuid;
330 int crypt_confirm(struct crypt_device *cd, const char *msg)
332 if (!cd || !cd->confirm)
335 return cd->confirm(msg, cd->confirm_usrptr);
338 static int key_from_terminal(struct crypt_device *cd, char *msg, char **key,
339 size_t *key_len, int force_verify)
345 if(!msg && asprintf(&prompt, _("Enter passphrase for %s: "),
346 cd->backing_file ?: crypt_get_device_name(cd)) < 0)
353 *key = crypt_safe_alloc(DEFAULT_PASSPHRASE_SIZE_MAX);
358 r = cd->password(msg, *key, DEFAULT_PASSPHRASE_SIZE_MAX,
359 cd->password_usrptr);
361 crypt_safe_free(*key);
366 r = crypt_get_key(msg, key, key_len, 0, 0, NULL, cd->timeout,
367 (force_verify || cd->password_verify), cd);
370 return (r < 0) ? r: 0;
373 static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslot,
374 struct volume_key **vk)
376 char *passphrase_read = NULL;
377 size_t passphrase_size_read;
378 int r = -EINVAL, eperm = 0, tries = cd->tries;
382 crypt_free_volume_key(*vk);
385 r = key_from_terminal(cd, NULL, &passphrase_read,
386 &passphrase_size_read, 0);
387 /* Continue if it is just passphrase verify mismatch */
393 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase_read,
394 passphrase_size_read, &cd->hdr, vk, cd);
397 crypt_safe_free(passphrase_read);
398 passphrase_read = NULL;
399 } while (r == -EPERM && (--tries > 0));
402 crypt_free_volume_key(*vk);
405 /* Report wrong passphrase if at least one try failed */
406 if (eperm && r == -EPIPE)
410 crypt_safe_free(passphrase_read);
414 static int key_from_file(struct crypt_device *cd, char *msg,
415 char **key, size_t *key_len,
416 const char *key_file, size_t key_offset,
419 return crypt_get_key(msg, key, key_len, key_offset, key_size, key_file,
423 void crypt_set_log_callback(struct crypt_device *cd,
424 void (*log)(int level, const char *msg, void *usrptr),
431 cd->log_usrptr = usrptr;
435 void crypt_set_confirm_callback(struct crypt_device *cd,
436 int (*confirm)(const char *msg, void *usrptr),
439 cd->confirm = confirm;
440 cd->confirm_usrptr = usrptr;
443 void crypt_set_password_callback(struct crypt_device *cd,
444 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
447 cd->password = password;
448 cd->password_usrptr = usrptr;
451 static void _get_error(char *error, char *buf, size_t size)
453 if (!buf || size < 1)
456 strncpy(buf, error, size - 1);
457 buf[size - 1] = '\0';
463 void crypt_last_error(struct crypt_device *cd, char *buf, size_t size)
466 return _get_error(cd->error, buf, size);
469 /* Deprecated global error interface */
470 void crypt_get_error(char *buf, size_t size)
472 return _get_error(global_error, buf, size);
475 const char *crypt_get_dir(void)
480 int crypt_init(struct crypt_device **cd, const char *device)
482 struct crypt_device *h = NULL;
488 log_dbg("Allocating crypt device %s context.", device);
490 if (!(h = malloc(sizeof(struct crypt_device))))
493 memset(h, 0, sizeof(*h));
497 r = device_ready(NULL, device, O_RDONLY);
499 h->device = crypt_loop_get_device();
500 log_dbg("Not a block device, %s%s.",
501 h->device ? "using free loop device " :
502 "no free loop device found",
505 log_err(NULL, _("Cannot find a free loopback device.\n"));
510 /* Keep the loop open, dettached on last close. */
511 h->loop_fd = crypt_loop_attach(h->device, device, 0, 1, &readonly);
512 if (h->loop_fd == -1) {
513 log_err(NULL, _("Attaching loopback device failed "
514 "(loop device with autoclear flag is required).\n"));
519 h->backing_file = crypt_loop_backing_file(h->device);
520 r = device_ready(NULL, h->device, O_RDONLY);
528 if (!h->device && device && !(h->device = strdup(device))) {
533 if (dm_init(h, 1) < 0) {
538 h->iteration_time = 1000;
539 h->password_verify = 0;
541 h->rng_type = crypt_random_default_key_rng();
547 if (h->loop_fd != -1)
550 free(h->backing_file);
556 static int crypt_check_data_device_size(struct crypt_device *cd)
559 uint64_t size, size_min;
561 /* Check data device size, require at least one sector */
562 size_min = crypt_get_data_offset(cd) << SECTOR_SHIFT ?: SECTOR_SIZE;
564 r = device_size(crypt_get_device_name(cd), &size);
568 if (size < size_min) {
569 log_err(cd, _("LUKS header detected but device %s is too small.\n"),
570 crypt_get_device_name(cd));
577 int crypt_set_data_device(struct crypt_device *cd, const char *device)
582 log_dbg("Setting ciphertext data device to %s.", device ?: "(none)");
584 if (!isLUKS(cd->type)) {
585 log_err(cd, _("This operation is not supported for this device type.\n"));
589 /* metadata device must be set */
593 r = device_ready(NULL, device, O_RDONLY);
597 if (!(data_device = strdup(device)))
600 if (!cd->metadata_device)
601 cd->metadata_device = cd->device;
605 cd->device = data_device;
607 return crypt_check_data_device_size(cd);
610 static int _crypt_load_luks1(struct crypt_device *cd, int require_header, int repair)
612 struct luks_phdr hdr;
619 r = LUKS_read_phdr(mdata_device(cd), &hdr, require_header, repair, cd);
623 if (!cd->type && !(cd->type = strdup(CRYPT_LUKS1)))
626 memcpy(&cd->hdr, &hdr, sizeof(hdr));
631 int crypt_init_by_name_and_header(struct crypt_device **cd,
633 const char *header_device)
635 crypt_status_info ci;
636 struct crypt_dm_active_device dmd;
637 char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
641 log_dbg("Allocating crypt device context by device %s.", name);
643 ci = crypt_status(NULL, name);
644 if (ci == CRYPT_INVALID)
647 if (ci < CRYPT_ACTIVE) {
648 log_err(NULL, _("Device %s is not active.\n"), name);
652 r = dm_query_device(name, DM_ACTIVE_DEVICE | DM_ACTIVE_CIPHER |
653 DM_ACTIVE_UUID | DM_ACTIVE_KEYSIZE, &dmd);
660 r = crypt_init(cd, header_device);
662 r = crypt_init(cd, dmd.device);
664 /* Underlying device disappeared but mapping still active */
665 if (!dmd.device || r == -ENOTBLK)
666 log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
669 /* Underlying device is not readable but crypt mapping exists */
671 free(CONST_CAST(void*)dmd.device);
673 r = crypt_init(cd, NULL);
681 if (!strncmp(CRYPT_PLAIN, dmd.uuid, sizeof(CRYPT_PLAIN)-1))
682 (*cd)->type = strdup(CRYPT_PLAIN);
683 else if (!strncmp(CRYPT_LOOPAES, dmd.uuid, sizeof(CRYPT_LOOPAES)-1))
684 (*cd)->type = strdup(CRYPT_LOOPAES);
685 else if (!strncmp(CRYPT_LUKS1, dmd.uuid, sizeof(CRYPT_LUKS1)-1))
686 (*cd)->type = strdup(CRYPT_LUKS1);
688 log_dbg("Unknown UUID set, some parameters are not set.");
690 log_dbg("Active device has no UUID set, some parameters are not set.");
693 r = crypt_set_data_device(*cd, dmd.device);
698 /* Try to initialise basic parameters from active device */
700 if (!(*cd)->backing_file && dmd.device && crypt_loop_device(dmd.device) &&
701 !((*cd)->backing_file = crypt_loop_backing_file(dmd.device))) {
706 if (isPLAIN((*cd)->type)) {
707 (*cd)->plain_uuid = strdup(dmd.uuid);
708 (*cd)->plain_hdr.hash = NULL; /* no way to get this */
709 (*cd)->plain_hdr.offset = dmd.offset;
710 (*cd)->plain_hdr.skip = dmd.iv_offset;
711 (*cd)->plain_key_size = dmd.vk->keylength;
713 r = crypt_parse_name_and_mode(dmd.cipher, cipher, NULL, cipher_mode);
715 (*cd)->plain_cipher = strdup(cipher);
716 (*cd)->plain_cipher_mode = strdup(cipher_mode);
718 } else if (isLOOPAES((*cd)->type)) {
719 (*cd)->loopaes_uuid = strdup(dmd.uuid);
720 (*cd)->loopaes_hdr.offset = dmd.offset;
722 r = crypt_parse_name_and_mode(dmd.cipher, cipher,
723 &key_nums, cipher_mode);
725 (*cd)->loopaes_cipher = strdup(cipher);
726 (*cd)->loopaes_cipher_mode = strdup(cipher_mode);
727 /* version 3 uses last key for IV */
728 if (dmd.vk->keylength % key_nums)
730 (*cd)->loopaes_key_size = dmd.vk->keylength / key_nums;
732 } else if (isLUKS((*cd)->type)) {
733 if (mdata_device(*cd)) {
734 r = _crypt_load_luks1(*cd, 0, 0);
736 log_dbg("LUKS device header does not match active device.");
742 /* checks whether UUIDs match each other */
743 r = crypt_uuid_cmp(dmd.uuid, (*cd)->hdr.uuid);
745 log_dbg("LUKS device header uuid: %s mismatches DM returned uuid %s",
746 (*cd)->hdr.uuid, dmd.uuid);
760 crypt_free_volume_key(dmd.vk);
761 free(CONST_CAST(void*)dmd.device);
762 free(CONST_CAST(void*)dmd.cipher);
763 free(CONST_CAST(void*)dmd.uuid);
767 int crypt_init_by_name(struct crypt_device **cd, const char *name)
769 return crypt_init_by_name_and_header(cd, name, NULL);
772 static int _crypt_format_plain(struct crypt_device *cd,
774 const char *cipher_mode,
776 size_t volume_key_size,
777 struct crypt_params_plain *params)
779 if (!cipher || !cipher_mode) {
780 log_err(cd, _("Invalid plain crypt parameters.\n"));
784 if (volume_key_size > 1024) {
785 log_err(cd, _("Invalid key size.\n"));
789 cd->plain_key_size = volume_key_size;
790 cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
794 cd->plain_cipher = strdup(cipher);
795 cd->plain_cipher_mode = strdup(cipher_mode);
798 cd->plain_uuid = strdup(uuid);
800 if (params && params->hash)
801 cd->plain_hdr.hash = strdup(params->hash);
803 cd->plain_hdr.offset = params ? params->offset : 0;
804 cd->plain_hdr.skip = params ? params->skip : 0;
805 cd->plain_hdr.size = params ? params->size : 0;
807 if (!cd->plain_cipher || !cd->plain_cipher_mode)
813 static int _crypt_format_luks1(struct crypt_device *cd,
815 const char *cipher_mode,
817 const char *volume_key,
818 size_t volume_key_size,
819 struct crypt_params_luks1 *params)
822 unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
823 unsigned long alignment_offset = 0;
825 if (!mdata_device(cd)) {
826 log_err(cd, _("Can't format LUKS without device.\n"));
831 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
834 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
839 if (params && params->data_device) {
840 cd->metadata_device = cd->device;
841 if (!(cd->device = strdup(params->data_device)))
843 required_alignment = params->data_alignment * SECTOR_SIZE;
844 } else if (params && params->data_alignment) {
845 required_alignment = params->data_alignment * SECTOR_SIZE;
847 get_topology_alignment(cd->device, &required_alignment,
848 &alignment_offset, DEFAULT_DISK_ALIGNMENT);
850 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
851 (params && params->hash) ? params->hash : "sha1",
853 required_alignment / SECTOR_SIZE,
854 alignment_offset / SECTOR_SIZE,
855 cd->iteration_time, &cd->PBKDF2_per_sec,
856 cd->metadata_device, cd);
860 /* Wipe first 8 sectors - fs magic numbers etc. */
861 r = crypt_wipe(mdata_device(cd), 0, 8 * SECTOR_SIZE, CRYPT_WIPE_ZERO, 1);
864 log_err(cd, _("Cannot format device %s which is still in use.\n"),
867 log_err(cd, _("Cannot wipe header on device %s.\n"),
873 r = LUKS_write_phdr(mdata_device(cd), &cd->hdr, cd);
878 static int _crypt_format_loopaes(struct crypt_device *cd,
881 size_t volume_key_size,
882 struct crypt_params_loopaes *params)
884 if (!mdata_device(cd)) {
885 log_err(cd, _("Can't format LOOPAES without device.\n"));
889 if (volume_key_size > 1024) {
890 log_err(cd, _("Invalid key size.\n"));
894 cd->loopaes_key_size = volume_key_size;
896 cd->loopaes_cipher = strdup(cipher ?: DEFAULT_LOOPAES_CIPHER);
899 cd->loopaes_uuid = strdup(uuid);
901 if (params && params->hash)
902 cd->loopaes_hdr.hash = strdup(params->hash);
904 cd->loopaes_hdr.offset = params ? params->offset : 0;
905 cd->loopaes_hdr.skip = params ? params->skip : 0;
910 int crypt_format(struct crypt_device *cd,
913 const char *cipher_mode,
915 const char *volume_key,
916 size_t volume_key_size,
925 log_dbg("Context already formatted as %s.", cd->type);
929 log_dbg("Formatting device %s as type %s.", mdata_device(cd) ?: "(none)", type);
936 r = _crypt_format_plain(cd, cipher, cipher_mode,
937 uuid, volume_key_size, params);
938 else if (isLUKS(type))
939 r = _crypt_format_luks1(cd, cipher, cipher_mode,
940 uuid, volume_key, volume_key_size, params);
941 else if (isLOOPAES(type))
942 r = _crypt_format_loopaes(cd, cipher, uuid, volume_key_size, params);
944 /* FIXME: allow plugins here? */
945 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
949 if (!r && !(cd->type = strdup(type)))
953 crypt_free_volume_key(cd->volume_key);
954 cd->volume_key = NULL;
960 int crypt_load(struct crypt_device *cd,
961 const char *requested_type,
962 void *params __attribute__((unused)))
966 log_dbg("Trying to load %s crypt type from device %s.",
967 requested_type ?: "any", mdata_device(cd) ?: "(none)");
969 if (!mdata_device(cd))
972 if (requested_type && !isLUKS(requested_type))
975 if (cd->type && !isLUKS(cd->type)) {
976 log_dbg("Context is already initialised to type %s", cd->type);
980 r = _crypt_load_luks1(cd, 1, 0);
984 /* cd->type and header must be set in context */
985 r = crypt_check_data_device_size(cd);
994 int crypt_repair(struct crypt_device *cd,
995 const char *requested_type,
996 void *params __attribute__((unused)))
1000 log_dbg("Trying to repair %s crypt type from device %s.",
1001 requested_type ?: "any", mdata_device(cd) ?: "(none)");
1003 if (!mdata_device(cd))
1006 if (requested_type && !isLUKS(requested_type))
1010 /* Load with repair */
1011 r = _crypt_load_luks1(cd, 1, 1);
1015 /* cd->type and header must be set in context */
1016 r = crypt_check_data_device_size(cd);
1025 int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
1027 struct crypt_dm_active_device dmd;
1030 /* Device context type must be initialised */
1031 if (!cd->type || !crypt_get_uuid(cd))
1034 log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
1036 r = dm_query_device(name, DM_ACTIVE_DEVICE | DM_ACTIVE_CIPHER |
1037 DM_ACTIVE_UUID | DM_ACTIVE_KEYSIZE |
1038 DM_ACTIVE_KEY, &dmd);
1040 log_err(NULL, _("Device %s is not active.\n"), name);
1049 r = device_check_and_adjust(cd, dmd.device, DEV_OK, &new_size, &dmd.offset, &dmd.flags);
1053 if (new_size == dmd.size) {
1054 log_dbg("Device has already requested size %" PRIu64
1055 " sectors.", dmd.size);
1058 dmd.size = new_size;
1059 r = dm_create_device(name, cd->type, &dmd, 1);
1062 crypt_free_volume_key(dmd.vk);
1063 free(CONST_CAST(void*)dmd.cipher);
1064 free(CONST_CAST(void*)dmd.device);
1065 free(CONST_CAST(void*)dmd.uuid);
1070 int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
1072 if (!isLUKS(cd->type)) {
1073 log_err(cd, _("This operation is not supported for this device type.\n"));
1077 if (uuid && !strncmp(uuid, cd->hdr.uuid, sizeof(cd->hdr.uuid))) {
1078 log_dbg("UUID is the same as requested (%s) for device %s.",
1079 uuid, mdata_device(cd));
1084 log_dbg("Requested new UUID change to %s for %s.", uuid, mdata_device(cd));
1086 log_dbg("Requested new UUID refresh for %s.", mdata_device(cd));
1088 if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
1091 return LUKS_hdr_uuid_set(mdata_device(cd), &cd->hdr, uuid, cd);
1094 int crypt_header_backup(struct crypt_device *cd,
1095 const char *requested_type,
1096 const char *backup_file)
1100 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1103 r = init_crypto(cd);
1107 log_dbg("Requested header backup of device %s (%s) to "
1108 "file %s.", mdata_device(cd), requested_type, backup_file);
1110 return LUKS_hdr_backup(backup_file, mdata_device(cd), &cd->hdr, cd);
1113 int crypt_header_restore(struct crypt_device *cd,
1114 const char *requested_type,
1115 const char *backup_file)
1119 if (requested_type && !isLUKS(requested_type))
1122 /* Some hash functions need initialized gcrypt library */
1123 r = init_crypto(cd);
1127 log_dbg("Requested header restore to device %s (%s) from "
1128 "file %s.", mdata_device(cd), requested_type, backup_file);
1130 return LUKS_hdr_restore(backup_file, mdata_device(cd), &cd->hdr, cd);
1133 void crypt_free(struct crypt_device *cd)
1136 log_dbg("Releasing crypt device %s context.", mdata_device(cd));
1138 if (cd->loop_fd != -1)
1142 crypt_free_volume_key(cd->volume_key);
1145 free(cd->metadata_device);
1146 free(cd->backing_file);
1149 /* used in plain device only */
1150 free(CONST_CAST(void*)cd->plain_hdr.hash);
1151 free(cd->plain_cipher);
1152 free(cd->plain_cipher_mode);
1153 free(cd->plain_uuid);
1155 /* used in loop-AES device only */
1156 free(CONST_CAST(void*)cd->loopaes_hdr.hash);
1157 free(cd->loopaes_cipher);
1158 free(cd->loopaes_uuid);
1164 int crypt_suspend(struct crypt_device *cd,
1167 crypt_status_info ci;
1170 log_dbg("Suspending volume %s.", name);
1172 if (!isLUKS(cd->type)) {
1173 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1178 ci = crypt_status(NULL, name);
1179 if (ci < CRYPT_ACTIVE) {
1180 log_err(cd, _("Volume %s is not active.\n"), name);
1184 if (!cd && dm_init(NULL, 1) < 0)
1187 r = dm_status_suspended(name);
1192 log_err(cd, _("Volume %s is already suspended.\n"), name);
1197 r = dm_suspend_and_wipe_key(name);
1199 log_err(cd, "Suspend is not supported for device %s.\n", name);
1201 log_err(cd, "Error during suspending device %s.\n", name);
1208 int crypt_resume_by_passphrase(struct crypt_device *cd,
1211 const char *passphrase,
1212 size_t passphrase_size)
1214 struct volume_key *vk = NULL;
1217 log_dbg("Resuming volume %s.", name);
1219 if (!isLUKS(cd->type)) {
1220 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1225 r = dm_status_suspended(name);
1230 log_err(cd, _("Volume %s is not suspended.\n"), name);
1235 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase,
1236 passphrase_size, &cd->hdr, &vk, cd);
1238 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1242 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1244 log_err(cd, "Resume is not supported for device %s.\n", name);
1246 log_err(cd, "Error during resuming device %s.\n", name);
1250 crypt_free_volume_key(vk);
1251 return r < 0 ? r : keyslot;
1254 int crypt_resume_by_keyfile_offset(struct crypt_device *cd,
1257 const char *keyfile,
1258 size_t keyfile_size,
1259 size_t keyfile_offset)
1261 struct volume_key *vk = NULL;
1262 char *passphrase_read = NULL;
1263 size_t passphrase_size_read;
1266 log_dbg("Resuming volume %s.", name);
1268 if (!isLUKS(cd->type)) {
1269 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1274 r = dm_status_suspended(name);
1279 log_err(cd, _("Volume %s is not suspended.\n"), name);
1286 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1287 &passphrase_size_read, keyfile, keyfile_offset,
1292 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase_read,
1293 passphrase_size_read, &cd->hdr, &vk, cd);
1298 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1300 log_err(cd, "Error during resuming device %s.\n", name);
1302 crypt_safe_free(passphrase_read);
1303 crypt_free_volume_key(vk);
1304 return r < 0 ? r : keyslot;
1307 int crypt_resume_by_keyfile(struct crypt_device *cd,
1310 const char *keyfile,
1311 size_t keyfile_size)
1313 return crypt_resume_by_keyfile_offset(cd, name, keyslot,
1314 keyfile, keyfile_size, 0);
1317 // slot manipulation
1318 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1319 int keyslot, // -1 any
1320 const char *passphrase, // NULL -> terminal
1321 size_t passphrase_size,
1322 const char *new_passphrase, // NULL -> terminal
1323 size_t new_passphrase_size)
1325 struct volume_key *vk = NULL;
1326 char *password = NULL, *new_password = NULL;
1327 size_t passwordLen, new_passwordLen;
1330 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1331 "new passphrase %sprovided.",
1332 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1334 if (!isLUKS(cd->type)) {
1335 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1339 r = keyslot_verify_or_find_empty(cd, &keyslot);
1343 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1344 /* No slots used, try to use pre-generated key in header */
1345 if (cd->volume_key) {
1346 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1347 r = vk ? 0 : -ENOMEM;
1349 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1352 } else if (passphrase) {
1353 /* Passphrase provided, use it to unlock existing keyslot */
1354 r = LUKS_open_key_with_hdr(mdata_device(cd), CRYPT_ANY_SLOT, passphrase,
1355 passphrase_size, &cd->hdr, &vk, cd);
1357 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1358 r = key_from_terminal(cd, _("Enter any passphrase: "),
1359 &password, &passwordLen, 0);
1363 r = LUKS_open_key_with_hdr(mdata_device(cd), CRYPT_ANY_SLOT, password,
1364 passwordLen, &cd->hdr, &vk, cd);
1365 crypt_safe_free(password);
1371 if (new_passphrase) {
1372 new_password = CONST_CAST(char*)new_passphrase;
1373 new_passwordLen = new_passphrase_size;
1375 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1376 &new_password, &new_passwordLen, 1);
1381 r = LUKS_set_key(mdata_device(cd), keyslot, new_password, new_passwordLen,
1382 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1387 if (!new_passphrase)
1388 crypt_safe_free(new_password);
1389 crypt_free_volume_key(vk);
1390 return r ?: keyslot;
1393 int crypt_keyslot_add_by_keyfile_offset(struct crypt_device *cd,
1395 const char *keyfile,
1396 size_t keyfile_size,
1397 size_t keyfile_offset,
1398 const char *new_keyfile,
1399 size_t new_keyfile_size,
1400 size_t new_keyfile_offset)
1402 struct volume_key *vk = NULL;
1403 char *password = NULL; size_t passwordLen;
1404 char *new_password = NULL; size_t new_passwordLen;
1407 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1408 keyfile ?: "[none]", new_keyfile ?: "[none]");
1410 if (!isLUKS(cd->type)) {
1411 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1415 r = keyslot_verify_or_find_empty(cd, &keyslot);
1419 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1420 /* No slots used, try to use pre-generated key in header */
1421 if (cd->volume_key) {
1422 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1423 r = vk ? 0 : -ENOMEM;
1425 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1429 /* Read password from file of (if NULL) from terminal */
1431 r = key_from_file(cd, _("Enter any passphrase: "),
1432 &password, &passwordLen,
1433 keyfile, keyfile_offset, keyfile_size);
1435 r = key_from_terminal(cd, _("Enter any passphrase: "),
1436 &password, &passwordLen, 0);
1440 r = LUKS_open_key_with_hdr(mdata_device(cd), CRYPT_ANY_SLOT, password, passwordLen,
1448 r = key_from_file(cd, _("Enter new passphrase for key slot: "),
1449 &new_password, &new_passwordLen, new_keyfile,
1450 new_keyfile_offset, new_keyfile_size);
1452 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1453 &new_password, &new_passwordLen, 1);
1457 r = LUKS_set_key(mdata_device(cd), keyslot, new_password, new_passwordLen,
1458 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1460 crypt_safe_free(password);
1461 crypt_safe_free(new_password);
1462 crypt_free_volume_key(vk);
1463 return r < 0 ? r : keyslot;
1466 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1468 const char *keyfile,
1469 size_t keyfile_size,
1470 const char *new_keyfile,
1471 size_t new_keyfile_size)
1473 return crypt_keyslot_add_by_keyfile_offset(cd, keyslot,
1474 keyfile, keyfile_size, 0,
1475 new_keyfile, new_keyfile_size, 0);
1478 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1480 const char *volume_key,
1481 size_t volume_key_size,
1482 const char *passphrase,
1483 size_t passphrase_size)
1485 struct volume_key *vk = NULL;
1487 char *new_password = NULL; size_t new_passwordLen;
1489 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1491 if (!isLUKS(cd->type)) {
1492 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1497 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1498 else if (cd->volume_key)
1499 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1504 r = LUKS_verify_volume_key(&cd->hdr, vk);
1506 log_err(cd, _("Volume key does not match the volume.\n"));
1510 r = keyslot_verify_or_find_empty(cd, &keyslot);
1515 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1516 &new_password, &new_passwordLen, 1);
1519 passphrase = new_password;
1520 passphrase_size = new_passwordLen;
1523 r = LUKS_set_key(mdata_device(cd), keyslot, passphrase, passphrase_size,
1524 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1526 crypt_safe_free(new_password);
1527 crypt_free_volume_key(vk);
1528 return (r < 0) ? r : keyslot;
1531 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1533 crypt_keyslot_info ki;
1535 log_dbg("Destroying keyslot %d.", keyslot);
1537 if (!isLUKS(cd->type)) {
1538 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1542 ki = crypt_keyslot_status(cd, keyslot);
1543 if (ki == CRYPT_SLOT_INVALID) {
1544 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1548 if (ki == CRYPT_SLOT_INACTIVE) {
1549 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1553 return LUKS_del_key(mdata_device(cd), keyslot, &cd->hdr, cd);
1556 // activation/deactivation of device mapping
1557 int crypt_activate_by_passphrase(struct crypt_device *cd,
1560 const char *passphrase,
1561 size_t passphrase_size,
1564 crypt_status_info ci;
1565 struct volume_key *vk = NULL;
1566 char *read_passphrase = NULL;
1567 size_t passphraseLen = 0;
1570 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1571 name ? "Activating" : "Checking", name ?: "",
1572 keyslot, passphrase ? "" : "[none] ");
1575 ci = crypt_status(NULL, name);
1576 if (ci == CRYPT_INVALID)
1578 else if (ci >= CRYPT_ACTIVE) {
1579 log_err(cd, _("Device %s already exists.\n"), name);
1584 /* plain, use hashed passphrase */
1585 if (isPLAIN(cd->type)) {
1590 r = key_from_terminal(cd, NULL, &read_passphrase,
1594 passphrase = read_passphrase;
1595 passphrase_size = passphraseLen;
1598 r = process_key(cd, cd->plain_hdr.hash,
1600 passphrase, passphrase_size, &vk);
1604 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1606 } else if (isLUKS(cd->type)) {
1607 /* provided passphrase, do not retry */
1609 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase,
1610 passphrase_size, &cd->hdr, &vk, cd);
1612 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1617 r = LUKS1_activate(cd, name, vk, flags);
1622 crypt_safe_free(read_passphrase);
1623 crypt_free_volume_key(vk);
1625 return r < 0 ? r : keyslot;
1628 int crypt_activate_by_keyfile_offset(struct crypt_device *cd,
1631 const char *keyfile,
1632 size_t keyfile_size,
1633 size_t keyfile_offset,
1636 crypt_status_info ci;
1637 struct volume_key *vk = NULL;
1638 char *passphrase_read = NULL;
1639 size_t passphrase_size_read;
1640 unsigned int key_count = 0;
1643 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1644 name ?: "", keyslot, keyfile ?: "[none]");
1647 ci = crypt_status(NULL, name);
1648 if (ci == CRYPT_INVALID)
1650 else if (ci >= CRYPT_ACTIVE) {
1651 log_err(cd, _("Device %s already exists.\n"), name);
1659 if (isPLAIN(cd->type)) {
1663 r = key_from_file(cd, _("Enter passphrase: "),
1664 &passphrase_read, &passphrase_size_read,
1665 keyfile, keyfile_offset, keyfile_size);
1669 r = process_key(cd, cd->plain_hdr.hash,
1671 passphrase_read, passphrase_size_read, &vk);
1675 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1676 } else if (isLUKS(cd->type)) {
1677 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1678 &passphrase_size_read, keyfile, keyfile_offset, keyfile_size);
1681 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase_read,
1682 passphrase_size_read, &cd->hdr, &vk, cd);
1688 r = LUKS1_activate(cd, name, vk, flags);
1693 } else if (isLOOPAES(cd->type)) {
1694 r = key_from_file(cd, NULL, &passphrase_read, &passphrase_size_read,
1695 keyfile, keyfile_offset, keyfile_size);
1698 r = LOOPAES_parse_keyfile(cd, &vk, cd->loopaes_hdr.hash, &key_count,
1699 passphrase_read, passphrase_size_read);
1703 r = LOOPAES_activate(cd, name, cd->loopaes_cipher,
1704 key_count, vk, flags);
1709 crypt_safe_free(passphrase_read);
1710 crypt_free_volume_key(vk);
1715 int crypt_activate_by_keyfile(struct crypt_device *cd,
1718 const char *keyfile,
1719 size_t keyfile_size,
1722 return crypt_activate_by_keyfile_offset(cd, name, keyslot, keyfile,
1723 keyfile_size, 0, flags);
1726 int crypt_activate_by_volume_key(struct crypt_device *cd,
1728 const char *volume_key,
1729 size_t volume_key_size,
1732 crypt_status_info ci;
1733 struct volume_key *vk = NULL;
1736 log_dbg("Activating volume %s by volume key.", name);
1739 ci = crypt_status(NULL, name);
1740 if (ci == CRYPT_INVALID)
1742 else if (ci >= CRYPT_ACTIVE) {
1743 log_err(cd, _("Device %s already exists.\n"), name);
1748 /* use key directly, no hash */
1749 if (isPLAIN(cd->type)) {
1753 if (!volume_key || !volume_key_size || volume_key_size != cd->plain_key_size) {
1754 log_err(cd, _("Incorrect volume key specified for plain device.\n"));
1758 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1762 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1763 } else if (isLUKS(cd->type)) {
1764 /* If key is not provided, try to use internal key */
1766 if (!cd->volume_key) {
1767 log_err(cd, _("Volume key does not match the volume.\n"));
1770 volume_key_size = cd->volume_key->keylength;
1771 volume_key = cd->volume_key->key;
1774 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1777 r = LUKS_verify_volume_key(&cd->hdr, vk);
1780 log_err(cd, _("Volume key does not match the volume.\n"));
1783 r = LUKS1_activate(cd, name, vk, flags);
1785 log_err(cd, _("Device type is not properly initialised.\n"));
1787 crypt_free_volume_key(vk);
1792 int crypt_deactivate(struct crypt_device *cd, const char *name)
1799 log_dbg("Deactivating volume %s.", name);
1801 if (!cd && dm_init(NULL, 1) < 0)
1804 switch (crypt_status(cd, name)) {
1807 r = dm_remove_device(name, 0, 0);
1809 case CRYPT_INACTIVE:
1810 log_err(cd, _("Device %s is not active.\n"), name);
1814 log_err(cd, _("Invalid device %s.\n"), name);
1824 int crypt_volume_key_get(struct crypt_device *cd,
1827 size_t *volume_key_size,
1828 const char *passphrase,
1829 size_t passphrase_size)
1831 struct volume_key *vk = NULL;
1835 if (crypt_fips_mode()) {
1836 log_err(cd, "Function not available in FIPS mode.\n");
1840 key_len = crypt_get_volume_key_size(cd);
1841 if (key_len > *volume_key_size) {
1842 log_err(cd, _("Volume key buffer too small.\n"));
1846 if (isPLAIN(cd->type) && cd->plain_hdr.hash) {
1847 r = process_key(cd, cd->plain_hdr.hash, key_len,
1848 passphrase, passphrase_size, &vk);
1850 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
1851 } else if (isLUKS(cd->type)) {
1852 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase,
1853 passphrase_size, &cd->hdr, &vk, cd);
1856 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
1859 memcpy(volume_key, vk->key, vk->keylength);
1860 *volume_key_size = vk->keylength;
1863 crypt_free_volume_key(vk);
1867 int crypt_volume_key_verify(struct crypt_device *cd,
1868 const char *volume_key,
1869 size_t volume_key_size)
1871 struct volume_key *vk;
1874 if (!isLUKS(cd->type)) {
1875 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1879 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1883 r = LUKS_verify_volume_key(&cd->hdr, vk);
1886 log_err(cd, _("Volume key does not match the volume.\n"));
1888 crypt_free_volume_key(vk);
1893 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
1895 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
1896 cd->timeout = timeout_sec;
1899 void crypt_set_password_retry(struct crypt_device *cd, int tries)
1901 log_dbg("Password retry count set to %d.", tries);
1905 void crypt_set_iteration_time(struct crypt_device *cd, uint64_t iteration_time_ms)
1907 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
1908 cd->iteration_time = iteration_time_ms;
1910 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
1912 crypt_set_iteration_time(cd, iteration_time_ms);
1915 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
1917 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
1918 cd->password_verify = password_verify ? 1 : 0;
1921 void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
1924 case CRYPT_RNG_URANDOM:
1925 case CRYPT_RNG_RANDOM:
1926 log_dbg("RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
1927 cd->rng_type = rng_type;
1931 int crypt_get_rng_type(struct crypt_device *cd)
1936 return cd->rng_type;
1939 int crypt_memory_lock(struct crypt_device *cd, int lock)
1941 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
1945 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
1949 if (!cd && dm_init(NULL, 1) < 0)
1950 return CRYPT_INVALID;
1952 r = dm_status_device(name);
1957 if (r < 0 && r != -ENODEV)
1958 return CRYPT_INVALID;
1961 return CRYPT_ACTIVE;
1966 return CRYPT_INACTIVE;
1969 static void hexprintICB(struct crypt_device *cd, char *d, int n)
1972 for(i = 0; i < n; i++)
1973 log_std(cd, "%02hhx ", (char)d[i]);
1976 int crypt_dump(struct crypt_device *cd)
1979 if (!isLUKS(cd->type)) { //FIXME
1980 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1984 log_std(cd, "LUKS header information for %s\n\n", mdata_device(cd));
1985 log_std(cd, "Version: \t%d\n", cd->hdr.version);
1986 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
1987 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
1988 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec);
1989 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
1990 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
1991 log_std(cd, "MK digest: \t");
1992 hexprintICB(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE);
1994 log_std(cd, "MK salt: \t");
1995 hexprintICB(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2);
1996 log_std(cd, "\n \t");
1997 hexprintICB(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
1999 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
2000 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
2001 for(i = 0; i < LUKS_NUMKEYS; i++) {
2002 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
2003 log_std(cd, "Key Slot %d: ENABLED\n",i);
2004 log_std(cd, "\tIterations: \t%d\n",
2005 cd->hdr.keyblock[i].passwordIterations);
2006 log_std(cd, "\tSalt: \t");
2007 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt,
2009 log_std(cd, "\n\t \t");
2010 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt +
2011 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
2014 log_std(cd, "\tKey material offset:\t%d\n",
2015 cd->hdr.keyblock[i].keyMaterialOffset);
2016 log_std(cd, "\tAF stripes: \t%d\n",
2017 cd->hdr.keyblock[i].stripes);
2020 log_std(cd, "Key Slot %d: DISABLED\n", i);
2026 const char *crypt_get_cipher(struct crypt_device *cd)
2028 if (isPLAIN(cd->type))
2029 return cd->plain_cipher;
2031 if (isLUKS(cd->type))
2032 return cd->hdr.cipherName;
2034 if (isLOOPAES(cd->type))
2035 return cd->loopaes_cipher;
2040 const char *crypt_get_cipher_mode(struct crypt_device *cd)
2042 if (isPLAIN(cd->type))
2043 return cd->plain_cipher_mode;
2045 if (isLUKS(cd->type))
2046 return cd->hdr.cipherMode;
2048 if (isLOOPAES(cd->type))
2049 return cd->loopaes_cipher_mode;
2054 const char *crypt_get_uuid(struct crypt_device *cd)
2056 if (isLUKS(cd->type))
2057 return cd->hdr.uuid;
2059 if (isPLAIN(cd->type))
2060 return cd->plain_uuid;
2062 if (isLOOPAES(cd->type))
2063 return cd->loopaes_uuid;
2068 const char *crypt_get_device_name(struct crypt_device *cd)
2074 int crypt_get_volume_key_size(struct crypt_device *cd)
2076 if (isPLAIN(cd->type))
2077 return cd->plain_key_size;
2079 if (isLUKS(cd->type))
2080 return cd->hdr.keyBytes;
2082 if (isLOOPAES(cd->type))
2083 return cd->loopaes_key_size;
2088 uint64_t crypt_get_data_offset(struct crypt_device *cd)
2090 if (isPLAIN(cd->type))
2091 return cd->plain_hdr.offset;
2093 if (isLUKS(cd->type))
2094 return cd->hdr.payloadOffset;
2096 if (isLOOPAES(cd->type))
2097 return cd->loopaes_hdr.offset;
2102 uint64_t crypt_get_iv_offset(struct crypt_device *cd)
2104 if (isPLAIN(cd->type))
2105 return cd->plain_hdr.skip;
2107 if (isLUKS(cd->type))
2110 if (isLOOPAES(cd->type))
2111 return cd->loopaes_hdr.skip;
2116 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
2118 if (!isLUKS(cd->type)) {
2119 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2120 return CRYPT_SLOT_INVALID;
2123 return LUKS_keyslot_info(&cd->hdr, keyslot);
2126 int crypt_keyslot_max(const char *type)
2128 if (type && isLUKS(type))
2129 return LUKS_NUMKEYS;
2134 const char *crypt_get_type(struct crypt_device *cd)
2139 int crypt_get_active_device(struct crypt_device *cd __attribute__((unused)),
2141 struct crypt_active_device *cad)
2143 struct crypt_dm_active_device dmd;
2146 r = dm_query_device(name, 0, &dmd);
2150 cad->offset = dmd.offset;
2151 cad->iv_offset = dmd.iv_offset;
2152 cad->size = dmd.size;
2153 cad->flags = dmd.flags;