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., 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 struct crypt_dm_active_device dmd = {
289 .device = crypt_get_device_name(cd),
291 .uuid = crypt_get_uuid(cd),
293 .offset = crypt_get_data_offset(cd),
294 .iv_offset = crypt_get_iv_offset(cd),
299 r = device_check_and_adjust(cd, dmd.device,
300 (dmd.flags & CRYPT_ACTIVATE_SHARED) ? DEV_SHARED : DEV_EXCL,
301 &dmd.size, &dmd.offset, &flags);
305 if (crypt_get_cipher_mode(cd))
306 r = asprintf(&dm_cipher, "%s-%s", crypt_get_cipher(cd), crypt_get_cipher_mode(cd));
308 r = asprintf(&dm_cipher, "%s", crypt_get_cipher(cd));
312 dmd.cipher = dm_cipher;
313 log_dbg("Trying to activate PLAIN device %s using cipher %s.", name, dmd.cipher);
315 r = dm_create_device(name, CRYPT_PLAIN, &dmd, 0);
318 if (!cd->plain_uuid && dm_query_device(name, DM_ACTIVE_UUID, &dmd) >= 0)
319 cd->plain_uuid = CONST_CAST(char*)dmd.uuid;
325 int crypt_confirm(struct crypt_device *cd, const char *msg)
327 if (!cd || !cd->confirm)
330 return cd->confirm(msg, cd->confirm_usrptr);
333 static int key_from_terminal(struct crypt_device *cd, char *msg, char **key,
334 size_t *key_len, int force_verify)
340 if(!msg && asprintf(&prompt, _("Enter passphrase for %s: "),
341 cd->backing_file ?: crypt_get_device_name(cd)) < 0)
348 *key = crypt_safe_alloc(DEFAULT_PASSPHRASE_SIZE_MAX);
353 r = cd->password(msg, *key, DEFAULT_PASSPHRASE_SIZE_MAX,
354 cd->password_usrptr);
356 crypt_safe_free(*key);
361 r = crypt_get_key(msg, key, key_len, 0, 0, NULL, cd->timeout,
362 (force_verify || cd->password_verify), cd);
365 return (r < 0) ? r: 0;
368 static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslot,
369 struct volume_key **vk)
371 char *passphrase_read = NULL;
372 size_t passphrase_size_read;
373 int r = -EINVAL, eperm = 0, tries = cd->tries;
377 crypt_free_volume_key(*vk);
380 r = key_from_terminal(cd, NULL, &passphrase_read,
381 &passphrase_size_read, 0);
382 /* Continue if it is just passphrase verify mismatch */
388 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase_read,
389 passphrase_size_read, &cd->hdr, vk, cd);
392 crypt_safe_free(passphrase_read);
393 passphrase_read = NULL;
394 } while (r == -EPERM && (--tries > 0));
397 crypt_free_volume_key(*vk);
400 /* Report wrong passphrase if at least one try failed */
401 if (eperm && r == -EPIPE)
405 crypt_safe_free(passphrase_read);
409 static int key_from_file(struct crypt_device *cd, char *msg,
410 char **key, size_t *key_len,
411 const char *key_file, size_t key_offset,
414 return crypt_get_key(msg, key, key_len, key_offset, key_size, key_file,
418 void crypt_set_log_callback(struct crypt_device *cd,
419 void (*log)(int level, const char *msg, void *usrptr),
426 cd->log_usrptr = usrptr;
430 void crypt_set_confirm_callback(struct crypt_device *cd,
431 int (*confirm)(const char *msg, void *usrptr),
434 cd->confirm = confirm;
435 cd->confirm_usrptr = usrptr;
438 void crypt_set_password_callback(struct crypt_device *cd,
439 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
442 cd->password = password;
443 cd->password_usrptr = usrptr;
446 static void _get_error(char *error, char *buf, size_t size)
448 if (!buf || size < 1)
451 strncpy(buf, error, size - 1);
452 buf[size - 1] = '\0';
458 void crypt_last_error(struct crypt_device *cd, char *buf, size_t size)
461 return _get_error(cd->error, buf, size);
464 /* Deprecated global error interface */
465 void crypt_get_error(char *buf, size_t size)
467 return _get_error(global_error, buf, size);
470 const char *crypt_get_dir(void)
475 int crypt_init(struct crypt_device **cd, const char *device)
477 struct crypt_device *h = NULL;
483 log_dbg("Allocating crypt device %s context.", device);
485 if (!(h = malloc(sizeof(struct crypt_device))))
488 memset(h, 0, sizeof(*h));
492 r = device_ready(NULL, device, O_RDONLY);
494 h->device = crypt_loop_get_device();
495 log_dbg("Not a block device, %s%s.",
496 h->device ? "using free loop device " :
497 "no free loop device found",
500 log_err(NULL, _("Cannot find a free loopback device.\n"));
505 /* Keep the loop open, dettached on last close. */
506 h->loop_fd = crypt_loop_attach(h->device, device, 0, 1, &readonly);
507 if (h->loop_fd == -1) {
508 log_err(NULL, _("Attaching loopback device failed "
509 "(loop device with autoclear flag is required).\n"));
514 h->backing_file = crypt_loop_backing_file(h->device);
515 r = device_ready(NULL, h->device, O_RDONLY);
523 if (!h->device && device && !(h->device = strdup(device))) {
528 if (dm_init(h, 1) < 0) {
533 h->iteration_time = 1000;
534 h->password_verify = 0;
536 h->rng_type = crypt_random_default_key_rng();
542 if (h->loop_fd != -1)
545 free(h->backing_file);
551 static int crypt_check_data_device_size(struct crypt_device *cd)
554 uint64_t size, size_min;
556 /* Check data device size, require at least one sector */
557 size_min = crypt_get_data_offset(cd) << SECTOR_SHIFT ?: SECTOR_SIZE;
559 r = device_size(crypt_get_device_name(cd), &size);
563 if (size < size_min) {
564 log_err(cd, _("LUKS header detected but device %s is too small.\n"),
565 crypt_get_device_name(cd));
572 int crypt_set_data_device(struct crypt_device *cd, const char *device)
577 log_dbg("Setting ciphertext data device to %s.", device ?: "(none)");
579 if (!isLUKS(cd->type)) {
580 log_err(cd, _("This operation is not supported for this device type.\n"));
584 /* metadata device must be set */
588 r = device_ready(NULL, device, O_RDONLY);
592 if (!(data_device = strdup(device)))
595 if (!cd->metadata_device)
596 cd->metadata_device = cd->device;
600 cd->device = data_device;
602 return crypt_check_data_device_size(cd);
605 static int _crypt_load_luks1(struct crypt_device *cd, int require_header)
607 struct luks_phdr hdr;
614 r = LUKS_read_phdr(mdata_device(cd), &hdr, require_header, cd);
618 if (!cd->type && !(cd->type = strdup(CRYPT_LUKS1)))
621 memcpy(&cd->hdr, &hdr, sizeof(hdr));
626 int crypt_init_by_name_and_header(struct crypt_device **cd,
628 const char *header_device)
630 crypt_status_info ci;
631 struct crypt_dm_active_device dmd;
632 char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
636 log_dbg("Allocating crypt device context by device %s.", name);
638 ci = crypt_status(NULL, name);
639 if (ci == CRYPT_INVALID)
642 if (ci < CRYPT_ACTIVE) {
643 log_err(NULL, _("Device %s is not active.\n"), name);
647 r = dm_query_device(name, DM_ACTIVE_DEVICE | DM_ACTIVE_CIPHER |
648 DM_ACTIVE_UUID | DM_ACTIVE_KEYSIZE, &dmd);
655 r = crypt_init(cd, header_device);
657 r = crypt_init(cd, dmd.device);
659 /* Underlying device disappeared but mapping still active */
660 if (!dmd.device || r == -ENOTBLK)
661 log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
664 /* Underlying device is not readable but crypt mapping exists */
666 free(CONST_CAST(void*)dmd.device);
668 r = crypt_init(cd, NULL);
676 if (!strncmp(CRYPT_PLAIN, dmd.uuid, sizeof(CRYPT_PLAIN)-1))
677 (*cd)->type = strdup(CRYPT_PLAIN);
678 else if (!strncmp(CRYPT_LOOPAES, dmd.uuid, sizeof(CRYPT_LOOPAES)-1))
679 (*cd)->type = strdup(CRYPT_LOOPAES);
680 else if (!strncmp(CRYPT_LUKS1, dmd.uuid, sizeof(CRYPT_LUKS1)-1))
681 (*cd)->type = strdup(CRYPT_LUKS1);
683 log_dbg("Unknown UUID set, some parameters are not set.");
685 log_dbg("Active device has no UUID set, some parameters are not set.");
688 r = crypt_set_data_device(*cd, dmd.device);
693 /* Try to initialise basic parameters from active device */
695 if (!(*cd)->backing_file && dmd.device && crypt_loop_device(dmd.device) &&
696 !((*cd)->backing_file = crypt_loop_backing_file(dmd.device))) {
701 if (isPLAIN((*cd)->type)) {
702 (*cd)->plain_uuid = strdup(dmd.uuid);
703 (*cd)->plain_hdr.hash = NULL; /* no way to get this */
704 (*cd)->plain_hdr.offset = dmd.offset;
705 (*cd)->plain_hdr.skip = dmd.iv_offset;
706 (*cd)->plain_key_size = dmd.vk->keylength;
708 r = crypt_parse_name_and_mode(dmd.cipher, cipher, NULL, cipher_mode);
710 (*cd)->plain_cipher = strdup(cipher);
711 (*cd)->plain_cipher_mode = strdup(cipher_mode);
713 } else if (isLOOPAES((*cd)->type)) {
714 (*cd)->loopaes_uuid = strdup(dmd.uuid);
715 (*cd)->loopaes_hdr.offset = dmd.offset;
717 r = crypt_parse_name_and_mode(dmd.cipher, cipher,
718 &key_nums, cipher_mode);
720 (*cd)->loopaes_cipher = strdup(cipher);
721 (*cd)->loopaes_cipher_mode = strdup(cipher_mode);
722 /* version 3 uses last key for IV */
723 if (dmd.vk->keylength % key_nums)
725 (*cd)->loopaes_key_size = dmd.vk->keylength / key_nums;
727 } else if (isLUKS((*cd)->type)) {
728 if (mdata_device(*cd)) {
729 r = _crypt_load_luks1(*cd, 0);
731 log_dbg("LUKS device header does not match active device.");
737 /* checks whether UUIDs match each other */
738 r = crypt_uuid_cmp(dmd.uuid, (*cd)->hdr.uuid);
740 log_dbg("LUKS device header uuid: %s mismatches DM returned uuid %s",
741 (*cd)->hdr.uuid, dmd.uuid);
755 crypt_free_volume_key(dmd.vk);
756 free(CONST_CAST(void*)dmd.device);
757 free(CONST_CAST(void*)dmd.cipher);
758 free(CONST_CAST(void*)dmd.uuid);
762 int crypt_init_by_name(struct crypt_device **cd, const char *name)
764 return crypt_init_by_name_and_header(cd, name, NULL);
767 static int _crypt_format_plain(struct crypt_device *cd,
769 const char *cipher_mode,
771 size_t volume_key_size,
772 struct crypt_params_plain *params)
774 if (!cipher || !cipher_mode) {
775 log_err(cd, _("Invalid plain crypt parameters.\n"));
779 if (volume_key_size > 1024) {
780 log_err(cd, _("Invalid key size.\n"));
784 cd->plain_key_size = volume_key_size;
785 cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
789 cd->plain_cipher = strdup(cipher);
790 cd->plain_cipher_mode = strdup(cipher_mode);
793 cd->plain_uuid = strdup(uuid);
795 if (params && params->hash)
796 cd->plain_hdr.hash = strdup(params->hash);
798 cd->plain_hdr.offset = params ? params->offset : 0;
799 cd->plain_hdr.skip = params ? params->skip : 0;
800 cd->plain_hdr.size = params ? params->size : 0;
802 if (!cd->plain_cipher || !cd->plain_cipher_mode)
808 static int _crypt_format_luks1(struct crypt_device *cd,
810 const char *cipher_mode,
812 const char *volume_key,
813 size_t volume_key_size,
814 struct crypt_params_luks1 *params)
817 unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
818 unsigned long alignment_offset = 0;
820 if (!mdata_device(cd)) {
821 log_err(cd, _("Can't format LUKS without device.\n"));
826 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
829 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
834 if (params && params->data_device) {
835 cd->metadata_device = cd->device;
836 if (!(cd->device = strdup(params->data_device)))
838 required_alignment = params->data_alignment * SECTOR_SIZE;
839 } else if (params && params->data_alignment) {
840 required_alignment = params->data_alignment * SECTOR_SIZE;
842 get_topology_alignment(cd->device, &required_alignment,
843 &alignment_offset, DEFAULT_DISK_ALIGNMENT);
845 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
846 (params && params->hash) ? params->hash : "sha1",
848 required_alignment / SECTOR_SIZE,
849 alignment_offset / SECTOR_SIZE,
850 cd->iteration_time, &cd->PBKDF2_per_sec,
851 cd->metadata_device, cd);
855 /* Wipe first 8 sectors - fs magic numbers etc. */
856 r = crypt_wipe(mdata_device(cd), 0, 8 * SECTOR_SIZE, CRYPT_WIPE_ZERO, 1);
859 log_err(cd, _("Cannot format device %s which is still in use.\n"),
862 log_err(cd, _("Cannot wipe header on device %s.\n"),
868 r = LUKS_write_phdr(mdata_device(cd), &cd->hdr, cd);
873 static int _crypt_format_loopaes(struct crypt_device *cd,
876 size_t volume_key_size,
877 struct crypt_params_loopaes *params)
879 if (!mdata_device(cd)) {
880 log_err(cd, _("Can't format LOOPAES without device.\n"));
884 if (volume_key_size > 1024) {
885 log_err(cd, _("Invalid key size.\n"));
889 cd->loopaes_key_size = volume_key_size;
891 cd->loopaes_cipher = strdup(cipher ?: DEFAULT_LOOPAES_CIPHER);
894 cd->loopaes_uuid = strdup(uuid);
896 if (params && params->hash)
897 cd->loopaes_hdr.hash = strdup(params->hash);
899 cd->loopaes_hdr.offset = params ? params->offset : 0;
900 cd->loopaes_hdr.skip = params ? params->skip : 0;
905 int crypt_format(struct crypt_device *cd,
908 const char *cipher_mode,
910 const char *volume_key,
911 size_t volume_key_size,
920 log_dbg("Context already formatted as %s.", cd->type);
924 log_dbg("Formatting device %s as type %s.", mdata_device(cd) ?: "(none)", type);
931 r = _crypt_format_plain(cd, cipher, cipher_mode,
932 uuid, volume_key_size, params);
933 else if (isLUKS(type))
934 r = _crypt_format_luks1(cd, cipher, cipher_mode,
935 uuid, volume_key, volume_key_size, params);
936 else if (isLOOPAES(type))
937 r = _crypt_format_loopaes(cd, cipher, uuid, volume_key_size, params);
939 /* FIXME: allow plugins here? */
940 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
944 if (!r && !(cd->type = strdup(type)))
948 crypt_free_volume_key(cd->volume_key);
949 cd->volume_key = NULL;
955 int crypt_load(struct crypt_device *cd,
956 const char *requested_type,
957 void *params __attribute__((unused)))
961 log_dbg("Trying to load %s crypt type from device %s.",
962 requested_type ?: "any", mdata_device(cd) ?: "(none)");
964 if (!mdata_device(cd))
967 if (requested_type && !isLUKS(requested_type))
970 if (cd->type && !isLUKS(cd->type)) {
971 log_dbg("Context is already initialised to type %s", cd->type);
975 r = _crypt_load_luks1(cd, 1);
979 /* cd->type and header must be set in context */
980 r = crypt_check_data_device_size(cd);
989 int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
991 struct crypt_dm_active_device dmd;
994 /* Device context type must be initialised */
995 if (!cd->type || !crypt_get_uuid(cd))
998 log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
1000 r = dm_query_device(name, DM_ACTIVE_DEVICE | DM_ACTIVE_CIPHER |
1001 DM_ACTIVE_UUID | DM_ACTIVE_KEYSIZE |
1002 DM_ACTIVE_KEY, &dmd);
1004 log_err(NULL, _("Device %s is not active.\n"), name);
1013 r = device_check_and_adjust(cd, dmd.device, DEV_OK, &new_size, &dmd.offset, &dmd.flags);
1017 if (new_size == dmd.size) {
1018 log_dbg("Device has already requested size %" PRIu64
1019 " sectors.", dmd.size);
1022 dmd.size = new_size;
1023 r = dm_create_device(name, cd->type, &dmd, 1);
1026 crypt_free_volume_key(dmd.vk);
1027 free(CONST_CAST(void*)dmd.cipher);
1028 free(CONST_CAST(void*)dmd.device);
1029 free(CONST_CAST(void*)dmd.uuid);
1034 int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
1036 if (!isLUKS(cd->type)) {
1037 log_err(cd, _("This operation is not supported for this device type.\n"));
1041 if (uuid && !strncmp(uuid, cd->hdr.uuid, sizeof(cd->hdr.uuid))) {
1042 log_dbg("UUID is the same as requested (%s) for device %s.",
1043 uuid, mdata_device(cd));
1048 log_dbg("Requested new UUID change to %s for %s.", uuid, mdata_device(cd));
1050 log_dbg("Requested new UUID refresh for %s.", mdata_device(cd));
1052 if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
1055 return LUKS_hdr_uuid_set(mdata_device(cd), &cd->hdr, uuid, cd);
1058 int crypt_header_backup(struct crypt_device *cd,
1059 const char *requested_type,
1060 const char *backup_file)
1064 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1067 r = init_crypto(cd);
1071 log_dbg("Requested header backup of device %s (%s) to "
1072 "file %s.", mdata_device(cd), requested_type, backup_file);
1074 return LUKS_hdr_backup(backup_file, mdata_device(cd), &cd->hdr, cd);
1077 int crypt_header_restore(struct crypt_device *cd,
1078 const char *requested_type,
1079 const char *backup_file)
1083 if (requested_type && !isLUKS(requested_type))
1086 /* Some hash functions need initialized gcrypt library */
1087 r = init_crypto(cd);
1091 log_dbg("Requested header restore to device %s (%s) from "
1092 "file %s.", mdata_device(cd), requested_type, backup_file);
1094 return LUKS_hdr_restore(backup_file, mdata_device(cd), &cd->hdr, cd);
1097 void crypt_free(struct crypt_device *cd)
1100 log_dbg("Releasing crypt device %s context.", mdata_device(cd));
1102 if (cd->loop_fd != -1)
1106 crypt_free_volume_key(cd->volume_key);
1109 free(cd->metadata_device);
1110 free(cd->backing_file);
1113 /* used in plain device only */
1114 free(CONST_CAST(void*)cd->plain_hdr.hash);
1115 free(cd->plain_cipher);
1116 free(cd->plain_cipher_mode);
1117 free(cd->plain_uuid);
1119 /* used in loop-AES device only */
1120 free(CONST_CAST(void*)cd->loopaes_hdr.hash);
1121 free(cd->loopaes_cipher);
1122 free(cd->loopaes_uuid);
1128 int crypt_suspend(struct crypt_device *cd,
1131 crypt_status_info ci;
1134 log_dbg("Suspending volume %s.", name);
1136 if (!isLUKS(cd->type)) {
1137 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1142 ci = crypt_status(NULL, name);
1143 if (ci < CRYPT_ACTIVE) {
1144 log_err(cd, _("Volume %s is not active.\n"), name);
1148 if (!cd && dm_init(NULL, 1) < 0)
1151 r = dm_status_suspended(name);
1156 log_err(cd, _("Volume %s is already suspended.\n"), name);
1161 r = dm_suspend_and_wipe_key(name);
1163 log_err(cd, "Suspend is not supported for device %s.\n", name);
1165 log_err(cd, "Error during suspending device %s.\n", name);
1172 int crypt_resume_by_passphrase(struct crypt_device *cd,
1175 const char *passphrase,
1176 size_t passphrase_size)
1178 struct volume_key *vk = NULL;
1181 log_dbg("Resuming volume %s.", name);
1183 if (!isLUKS(cd->type)) {
1184 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1189 r = dm_status_suspended(name);
1194 log_err(cd, _("Volume %s is not suspended.\n"), name);
1199 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase,
1200 passphrase_size, &cd->hdr, &vk, cd);
1202 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1206 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1208 log_err(cd, "Resume is not supported for device %s.\n", name);
1210 log_err(cd, "Error during resuming device %s.\n", name);
1214 crypt_free_volume_key(vk);
1215 return r < 0 ? r : keyslot;
1218 int crypt_resume_by_keyfile_offset(struct crypt_device *cd,
1221 const char *keyfile,
1222 size_t keyfile_size,
1223 size_t keyfile_offset)
1225 struct volume_key *vk = NULL;
1226 char *passphrase_read = NULL;
1227 size_t passphrase_size_read;
1230 log_dbg("Resuming volume %s.", name);
1232 if (!isLUKS(cd->type)) {
1233 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1238 r = dm_status_suspended(name);
1243 log_err(cd, _("Volume %s is not suspended.\n"), name);
1250 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1251 &passphrase_size_read, keyfile, keyfile_offset,
1256 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase_read,
1257 passphrase_size_read, &cd->hdr, &vk, cd);
1262 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1264 log_err(cd, "Error during resuming device %s.\n", name);
1266 crypt_safe_free(passphrase_read);
1267 crypt_free_volume_key(vk);
1268 return r < 0 ? r : keyslot;
1271 int crypt_resume_by_keyfile(struct crypt_device *cd,
1274 const char *keyfile,
1275 size_t keyfile_size)
1277 return crypt_resume_by_keyfile_offset(cd, name, keyslot,
1278 keyfile, keyfile_size, 0);
1281 // slot manipulation
1282 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1283 int keyslot, // -1 any
1284 const char *passphrase, // NULL -> terminal
1285 size_t passphrase_size,
1286 const char *new_passphrase, // NULL -> terminal
1287 size_t new_passphrase_size)
1289 struct volume_key *vk = NULL;
1290 char *password = NULL, *new_password = NULL;
1291 size_t passwordLen, new_passwordLen;
1294 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1295 "new passphrase %sprovided.",
1296 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1298 if (!isLUKS(cd->type)) {
1299 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1303 r = keyslot_verify_or_find_empty(cd, &keyslot);
1307 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1308 /* No slots used, try to use pre-generated key in header */
1309 if (cd->volume_key) {
1310 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1311 r = vk ? 0 : -ENOMEM;
1313 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1316 } else if (passphrase) {
1317 /* Passphrase provided, use it to unlock existing keyslot */
1318 r = LUKS_open_key_with_hdr(mdata_device(cd), CRYPT_ANY_SLOT, passphrase,
1319 passphrase_size, &cd->hdr, &vk, cd);
1321 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1322 r = key_from_terminal(cd, _("Enter any passphrase: "),
1323 &password, &passwordLen, 0);
1327 r = LUKS_open_key_with_hdr(mdata_device(cd), CRYPT_ANY_SLOT, password,
1328 passwordLen, &cd->hdr, &vk, cd);
1329 crypt_safe_free(password);
1335 if (new_passphrase) {
1336 new_password = CONST_CAST(char*)new_passphrase;
1337 new_passwordLen = new_passphrase_size;
1339 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1340 &new_password, &new_passwordLen, 1);
1345 r = LUKS_set_key(mdata_device(cd), keyslot, new_password, new_passwordLen,
1346 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1351 if (!new_passphrase)
1352 crypt_safe_free(new_password);
1353 crypt_free_volume_key(vk);
1354 return r ?: keyslot;
1357 int crypt_keyslot_add_by_keyfile_offset(struct crypt_device *cd,
1359 const char *keyfile,
1360 size_t keyfile_size,
1361 size_t keyfile_offset,
1362 const char *new_keyfile,
1363 size_t new_keyfile_size,
1364 size_t new_keyfile_offset)
1366 struct volume_key *vk = NULL;
1367 char *password = NULL; size_t passwordLen;
1368 char *new_password = NULL; size_t new_passwordLen;
1371 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1372 keyfile ?: "[none]", new_keyfile ?: "[none]");
1374 if (!isLUKS(cd->type)) {
1375 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1379 r = keyslot_verify_or_find_empty(cd, &keyslot);
1383 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1384 /* No slots used, try to use pre-generated key in header */
1385 if (cd->volume_key) {
1386 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1387 r = vk ? 0 : -ENOMEM;
1389 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1393 /* Read password from file of (if NULL) from terminal */
1395 r = key_from_file(cd, _("Enter any passphrase: "),
1396 &password, &passwordLen,
1397 keyfile, keyfile_offset, keyfile_size);
1399 r = key_from_terminal(cd, _("Enter any passphrase: "),
1400 &password, &passwordLen, 0);
1404 r = LUKS_open_key_with_hdr(mdata_device(cd), CRYPT_ANY_SLOT, password, passwordLen,
1412 r = key_from_file(cd, _("Enter new passphrase for key slot: "),
1413 &new_password, &new_passwordLen, new_keyfile,
1414 new_keyfile_offset, new_keyfile_size);
1416 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1417 &new_password, &new_passwordLen, 1);
1421 r = LUKS_set_key(mdata_device(cd), keyslot, new_password, new_passwordLen,
1422 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1424 crypt_safe_free(password);
1425 crypt_safe_free(new_password);
1426 crypt_free_volume_key(vk);
1427 return r < 0 ? r : keyslot;
1430 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1432 const char *keyfile,
1433 size_t keyfile_size,
1434 const char *new_keyfile,
1435 size_t new_keyfile_size)
1437 return crypt_keyslot_add_by_keyfile_offset(cd, keyslot,
1438 keyfile, keyfile_size, 0,
1439 new_keyfile, new_keyfile_size, 0);
1442 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1444 const char *volume_key,
1445 size_t volume_key_size,
1446 const char *passphrase,
1447 size_t passphrase_size)
1449 struct volume_key *vk = NULL;
1451 char *new_password = NULL; size_t new_passwordLen;
1453 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1455 if (!isLUKS(cd->type)) {
1456 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1461 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1462 else if (cd->volume_key)
1463 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1468 r = LUKS_verify_volume_key(&cd->hdr, vk);
1470 log_err(cd, _("Volume key does not match the volume.\n"));
1474 r = keyslot_verify_or_find_empty(cd, &keyslot);
1479 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1480 &new_password, &new_passwordLen, 1);
1483 passphrase = new_password;
1484 passphrase_size = new_passwordLen;
1487 r = LUKS_set_key(mdata_device(cd), keyslot, passphrase, passphrase_size,
1488 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1490 crypt_safe_free(new_password);
1491 crypt_free_volume_key(vk);
1492 return (r < 0) ? r : keyslot;
1495 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1497 crypt_keyslot_info ki;
1499 log_dbg("Destroying keyslot %d.", keyslot);
1501 if (!isLUKS(cd->type)) {
1502 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1506 ki = crypt_keyslot_status(cd, keyslot);
1507 if (ki == CRYPT_SLOT_INVALID) {
1508 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1512 if (ki == CRYPT_SLOT_INACTIVE) {
1513 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1517 return LUKS_del_key(mdata_device(cd), keyslot, &cd->hdr, cd);
1520 // activation/deactivation of device mapping
1521 int crypt_activate_by_passphrase(struct crypt_device *cd,
1524 const char *passphrase,
1525 size_t passphrase_size,
1528 crypt_status_info ci;
1529 struct volume_key *vk = NULL;
1530 char *read_passphrase = NULL;
1531 size_t passphraseLen = 0;
1534 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1535 name ? "Activating" : "Checking", name ?: "",
1536 keyslot, passphrase ? "" : "[none] ");
1539 ci = crypt_status(NULL, name);
1540 if (ci == CRYPT_INVALID)
1542 else if (ci >= CRYPT_ACTIVE) {
1543 log_err(cd, _("Device %s already exists.\n"), name);
1548 /* plain, use hashed passphrase */
1549 if (isPLAIN(cd->type)) {
1554 r = key_from_terminal(cd, NULL, &read_passphrase,
1558 passphrase = read_passphrase;
1559 passphrase_size = passphraseLen;
1562 r = process_key(cd, cd->plain_hdr.hash,
1564 passphrase, passphrase_size, &vk);
1568 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1570 } else if (isLUKS(cd->type)) {
1571 /* provided passphrase, do not retry */
1573 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase,
1574 passphrase_size, &cd->hdr, &vk, cd);
1576 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1581 r = LUKS1_activate(cd, name, vk, flags);
1586 crypt_safe_free(read_passphrase);
1587 crypt_free_volume_key(vk);
1589 return r < 0 ? r : keyslot;
1592 int crypt_activate_by_keyfile_offset(struct crypt_device *cd,
1595 const char *keyfile,
1596 size_t keyfile_size,
1597 size_t keyfile_offset,
1600 crypt_status_info ci;
1601 struct volume_key *vk = NULL;
1602 char *passphrase_read = NULL;
1603 size_t passphrase_size_read;
1604 unsigned int key_count = 0;
1607 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1608 name ?: "", keyslot, keyfile ?: "[none]");
1611 ci = crypt_status(NULL, name);
1612 if (ci == CRYPT_INVALID)
1614 else if (ci >= CRYPT_ACTIVE) {
1615 log_err(cd, _("Device %s already exists.\n"), name);
1623 if (isPLAIN(cd->type)) {
1627 r = key_from_file(cd, _("Enter passphrase: "),
1628 &passphrase_read, &passphrase_size_read,
1629 keyfile, keyfile_offset, keyfile_size);
1633 r = process_key(cd, cd->plain_hdr.hash,
1635 passphrase_read, passphrase_size_read, &vk);
1639 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1640 } else if (isLUKS(cd->type)) {
1641 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1642 &passphrase_size_read, keyfile, keyfile_offset, keyfile_size);
1645 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase_read,
1646 passphrase_size_read, &cd->hdr, &vk, cd);
1652 r = LUKS1_activate(cd, name, vk, flags);
1657 } else if (isLOOPAES(cd->type)) {
1658 r = key_from_file(cd, NULL, &passphrase_read, &passphrase_size_read,
1659 keyfile, keyfile_offset, keyfile_size);
1662 r = LOOPAES_parse_keyfile(cd, &vk, cd->loopaes_hdr.hash, &key_count,
1663 passphrase_read, passphrase_size_read);
1667 r = LOOPAES_activate(cd, name, cd->loopaes_cipher,
1668 key_count, vk, flags);
1673 crypt_safe_free(passphrase_read);
1674 crypt_free_volume_key(vk);
1679 int crypt_activate_by_keyfile(struct crypt_device *cd,
1682 const char *keyfile,
1683 size_t keyfile_size,
1686 return crypt_activate_by_keyfile_offset(cd, name, keyslot, keyfile,
1687 keyfile_size, 0, flags);
1690 int crypt_activate_by_volume_key(struct crypt_device *cd,
1692 const char *volume_key,
1693 size_t volume_key_size,
1696 crypt_status_info ci;
1697 struct volume_key *vk = NULL;
1700 log_dbg("Activating volume %s by volume key.", name);
1703 ci = crypt_status(NULL, name);
1704 if (ci == CRYPT_INVALID)
1706 else if (ci >= CRYPT_ACTIVE) {
1707 log_err(cd, _("Device %s already exists.\n"), name);
1712 /* use key directly, no hash */
1713 if (isPLAIN(cd->type)) {
1717 if (!volume_key || !volume_key_size || volume_key_size != cd->plain_key_size) {
1718 log_err(cd, _("Incorrect volume key specified for plain device.\n"));
1722 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1726 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1727 } else if (isLUKS(cd->type)) {
1728 /* If key is not provided, try to use internal key */
1730 if (!cd->volume_key) {
1731 log_err(cd, _("Volume key does not match the volume.\n"));
1734 volume_key_size = cd->volume_key->keylength;
1735 volume_key = cd->volume_key->key;
1738 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1741 r = LUKS_verify_volume_key(&cd->hdr, vk);
1744 log_err(cd, _("Volume key does not match the volume.\n"));
1747 r = LUKS1_activate(cd, name, vk, flags);
1749 log_err(cd, _("Device type is not properly initialised.\n"));
1751 crypt_free_volume_key(vk);
1756 int crypt_deactivate(struct crypt_device *cd, const char *name)
1763 log_dbg("Deactivating volume %s.", name);
1765 if (!cd && dm_init(NULL, 1) < 0)
1768 switch (crypt_status(cd, name)) {
1770 r = dm_remove_device(name, 0, 0);
1773 log_err(cd, _("Device %s is busy.\n"), name);
1776 case CRYPT_INACTIVE:
1777 log_err(cd, _("Device %s is not active.\n"), name);
1781 log_err(cd, _("Invalid device %s.\n"), name);
1791 int crypt_volume_key_get(struct crypt_device *cd,
1794 size_t *volume_key_size,
1795 const char *passphrase,
1796 size_t passphrase_size)
1798 struct volume_key *vk = NULL;
1802 key_len = crypt_get_volume_key_size(cd);
1803 if (key_len > *volume_key_size) {
1804 log_err(cd, _("Volume key buffer too small.\n"));
1808 if (isPLAIN(cd->type) && cd->plain_hdr.hash) {
1809 r = process_key(cd, cd->plain_hdr.hash, key_len,
1810 passphrase, passphrase_size, &vk);
1812 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
1813 } else if (isLUKS(cd->type)) {
1814 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase,
1815 passphrase_size, &cd->hdr, &vk, cd);
1818 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
1821 memcpy(volume_key, vk->key, vk->keylength);
1822 *volume_key_size = vk->keylength;
1825 crypt_free_volume_key(vk);
1829 int crypt_volume_key_verify(struct crypt_device *cd,
1830 const char *volume_key,
1831 size_t volume_key_size)
1833 struct volume_key *vk;
1836 if (!isLUKS(cd->type)) {
1837 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1841 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1845 r = LUKS_verify_volume_key(&cd->hdr, vk);
1848 log_err(cd, _("Volume key does not match the volume.\n"));
1850 crypt_free_volume_key(vk);
1855 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
1857 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
1858 cd->timeout = timeout_sec;
1861 void crypt_set_password_retry(struct crypt_device *cd, int tries)
1863 log_dbg("Password retry count set to %d.", tries);
1867 void crypt_set_iteration_time(struct crypt_device *cd, uint64_t iteration_time_ms)
1869 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
1870 cd->iteration_time = iteration_time_ms;
1872 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
1874 crypt_set_iteration_time(cd, iteration_time_ms);
1877 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
1879 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
1880 cd->password_verify = password_verify ? 1 : 0;
1883 void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
1886 case CRYPT_RNG_URANDOM:
1887 case CRYPT_RNG_RANDOM:
1888 log_dbg("RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
1889 cd->rng_type = rng_type;
1893 int crypt_get_rng_type(struct crypt_device *cd)
1898 return cd->rng_type;
1901 int crypt_memory_lock(struct crypt_device *cd, int lock)
1903 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
1907 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
1911 if (!cd && dm_init(NULL, 1) < 0)
1912 return CRYPT_INVALID;
1914 r = dm_status_device(name);
1919 if (r < 0 && r != -ENODEV)
1920 return CRYPT_INVALID;
1923 return CRYPT_ACTIVE;
1928 return CRYPT_INACTIVE;
1931 static void hexprintICB(struct crypt_device *cd, char *d, int n)
1934 for(i = 0; i < n; i++)
1935 log_std(cd, "%02hhx ", (char)d[i]);
1938 int crypt_dump(struct crypt_device *cd)
1941 if (!isLUKS(cd->type)) { //FIXME
1942 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1946 log_std(cd, "LUKS header information for %s\n\n", mdata_device(cd));
1947 log_std(cd, "Version: \t%d\n", cd->hdr.version);
1948 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
1949 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
1950 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec);
1951 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
1952 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
1953 log_std(cd, "MK digest: \t");
1954 hexprintICB(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE);
1956 log_std(cd, "MK salt: \t");
1957 hexprintICB(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2);
1958 log_std(cd, "\n \t");
1959 hexprintICB(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
1961 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
1962 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
1963 for(i = 0; i < LUKS_NUMKEYS; i++) {
1964 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
1965 log_std(cd, "Key Slot %d: ENABLED\n",i);
1966 log_std(cd, "\tIterations: \t%d\n",
1967 cd->hdr.keyblock[i].passwordIterations);
1968 log_std(cd, "\tSalt: \t");
1969 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt,
1971 log_std(cd, "\n\t \t");
1972 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt +
1973 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
1976 log_std(cd, "\tKey material offset:\t%d\n",
1977 cd->hdr.keyblock[i].keyMaterialOffset);
1978 log_std(cd, "\tAF stripes: \t%d\n",
1979 cd->hdr.keyblock[i].stripes);
1982 log_std(cd, "Key Slot %d: DISABLED\n", i);
1988 const char *crypt_get_cipher(struct crypt_device *cd)
1990 if (isPLAIN(cd->type))
1991 return cd->plain_cipher;
1993 if (isLUKS(cd->type))
1994 return cd->hdr.cipherName;
1996 if (isLOOPAES(cd->type))
1997 return cd->loopaes_cipher;
2002 const char *crypt_get_cipher_mode(struct crypt_device *cd)
2004 if (isPLAIN(cd->type))
2005 return cd->plain_cipher_mode;
2007 if (isLUKS(cd->type))
2008 return cd->hdr.cipherMode;
2010 if (isLOOPAES(cd->type))
2011 return cd->loopaes_cipher_mode;
2016 const char *crypt_get_uuid(struct crypt_device *cd)
2018 if (isLUKS(cd->type))
2019 return cd->hdr.uuid;
2021 if (isPLAIN(cd->type))
2022 return cd->plain_uuid;
2024 if (isLOOPAES(cd->type))
2025 return cd->loopaes_uuid;
2030 const char *crypt_get_device_name(struct crypt_device *cd)
2036 int crypt_get_volume_key_size(struct crypt_device *cd)
2038 if (isPLAIN(cd->type))
2039 return cd->plain_key_size;
2041 if (isLUKS(cd->type))
2042 return cd->hdr.keyBytes;
2044 if (isLOOPAES(cd->type))
2045 return cd->loopaes_key_size;
2050 uint64_t crypt_get_data_offset(struct crypt_device *cd)
2052 if (isPLAIN(cd->type))
2053 return cd->plain_hdr.offset;
2055 if (isLUKS(cd->type))
2056 return cd->hdr.payloadOffset;
2058 if (isLOOPAES(cd->type))
2059 return cd->loopaes_hdr.offset;
2064 uint64_t crypt_get_iv_offset(struct crypt_device *cd)
2066 if (isPLAIN(cd->type))
2067 return cd->plain_hdr.skip;
2069 if (isLUKS(cd->type))
2072 if (isLOOPAES(cd->type))
2073 return cd->loopaes_hdr.skip;
2078 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
2080 if (!isLUKS(cd->type)) {
2081 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2082 return CRYPT_SLOT_INVALID;
2085 return LUKS_keyslot_info(&cd->hdr, keyslot);
2088 int crypt_keyslot_max(const char *type)
2090 if (type && isLUKS(type))
2091 return LUKS_NUMKEYS;
2096 const char *crypt_get_type(struct crypt_device *cd)
2101 int crypt_get_active_device(struct crypt_device *cd __attribute__((unused)),
2103 struct crypt_active_device *cad)
2105 struct crypt_dm_active_device dmd;
2108 r = dm_query_device(name, 0, &dmd);
2112 cad->offset = dmd.offset;
2113 cad->iv_offset = dmd.iv_offset;
2114 cad->size = dmd.size;
2115 cad->flags = dmd.flags;