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"
38 char *metadata_device;
42 struct volume_key *volume_key;
44 uint64_t iteration_time;
49 /* used in CRYPT_LUKS1 */
51 uint64_t PBKDF2_per_sec;
53 /* used in CRYPT_PLAIN */
54 struct crypt_params_plain plain_hdr;
56 char *plain_cipher_mode;
58 unsigned int plain_key_size;
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;
75 /* last error message */
76 char error[MAX_ERROR_LENGTH];
80 /* FIXME: not thread safe, remove this later */
81 static char global_error[MAX_ERROR_LENGTH] = {0};
84 static void (*_default_log)(int level, const char *msg, void *usrptr) = NULL;
85 static int _debug_level = 0;
87 void crypt_set_debug_level(int level)
92 int crypt_get_debug_level(void)
97 static void crypt_set_error(struct crypt_device *cd, const char *error)
99 size_t size = strlen(error);
101 /* Set global error, ugly hack... */
102 strncpy(global_error, error, MAX_ERROR_LENGTH - 2);
103 if (size < MAX_ERROR_LENGTH && global_error[size - 1] == '\n')
104 global_error[size - 1] = '\0';
106 /* Set error string per context */
108 strncpy(cd->error, error, MAX_ERROR_LENGTH - 2);
109 if (size < MAX_ERROR_LENGTH && cd->error[size - 1] == '\n')
110 cd->error[size - 1] = '\0';
114 void crypt_log(struct crypt_device *cd, int level, const char *msg)
117 cd->log(level, msg, cd->log_usrptr);
118 else if (_default_log)
119 _default_log(level, msg, NULL);
121 if (level == CRYPT_LOG_ERROR)
122 crypt_set_error(cd, msg);
125 __attribute__((format(printf, 5, 6)))
126 void logger(struct crypt_device *cd, int level, const char *file,
127 int line, const char *format, ...)
132 va_start(argp, format);
134 if (vasprintf(&target, format, argp) > 0 ) {
136 crypt_log(cd, level, target);
138 } else if (_debug_level)
139 printf("# %s:%d %s\n", file ?: "?", line, target);
141 } else if (_debug_level)
142 printf("# %s\n", target);
150 static const char *mdata_device(struct crypt_device *cd)
152 return cd->metadata_device ?: cd->device;
155 static int init_crypto(struct crypt_device *ctx)
159 crypt_fips_libcryptsetup_check(ctx);
161 r = crypt_random_init(ctx);
163 log_err(ctx, _("Cannot initialize crypto RNG backend.\n"));
167 r = crypt_backend_init(ctx);
169 log_err(ctx, _("Cannot initialize crypto backend.\n"));
171 log_dbg("Crypto backend (%s) initialized.", crypt_backend_version());
175 static int process_key(struct crypt_device *cd, const char *hash_name,
176 size_t key_size, const char *pass, size_t passLen,
177 struct volume_key **vk)
184 *vk = crypt_alloc_volume_key(key_size, NULL);
189 r = crypt_plain_hash(cd, hash_name, (*vk)->key, key_size, pass, passLen);
192 log_err(cd, _("Hash algorithm %s not supported.\n"),
195 log_err(cd, _("Key processing error (using hash %s).\n"),
197 crypt_free_volume_key(*vk);
201 } else if (passLen > key_size) {
202 memcpy((*vk)->key, pass, key_size);
204 memcpy((*vk)->key, pass, passLen);
210 static int isPLAIN(const char *type)
212 return (type && !strcmp(CRYPT_PLAIN, type));
215 static int isLUKS(const char *type)
217 return (type && !strcmp(CRYPT_LUKS1, type));
220 static int isLOOPAES(const char *type)
222 return (type && !strcmp(CRYPT_LOOPAES, type));
225 /* keyslot helpers */
226 static int keyslot_verify_or_find_empty(struct crypt_device *cd, int *keyslot)
228 if (*keyslot == CRYPT_ANY_SLOT) {
229 *keyslot = LUKS_keyslot_find_empty(&cd->hdr);
231 log_err(cd, _("All key slots full.\n"));
236 switch (LUKS_keyslot_info(&cd->hdr, *keyslot)) {
237 case CRYPT_SLOT_INVALID:
238 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
239 *keyslot, LUKS_NUMKEYS - 1);
241 case CRYPT_SLOT_INACTIVE:
244 log_err(cd, _("Key slot %d is full, please select another one.\n"),
253 * compares UUIDs returned by device-mapper (striped by cryptsetup) and uuid in header
255 static int crypt_uuid_cmp(const char *dm_uuid, const char *hdr_uuid)
260 if (!dm_uuid || !hdr_uuid)
263 str = strchr(dm_uuid, '-');
267 for (i = 0, j = 1; hdr_uuid[i]; i++) {
268 if (hdr_uuid[i] == '-')
271 if (!str[j] || str[j] == '-')
274 if (str[j] != hdr_uuid[i])
282 int PLAIN_activate(struct crypt_device *cd,
284 struct volume_key *vk,
289 char *dm_cipher = NULL;
290 enum devcheck device_check;
291 struct crypt_dm_active_device dmd = {
292 .device = crypt_get_device_name(cd),
294 .uuid = crypt_get_uuid(cd),
296 .offset = crypt_get_data_offset(cd),
297 .iv_offset = crypt_get_iv_offset(cd),
302 if (dmd.flags & CRYPT_ACTIVATE_SHARED)
303 device_check = DEV_SHARED;
305 device_check = DEV_EXCL;
307 r = device_check_and_adjust(cd, dmd.device, device_check,
308 &dmd.size, &dmd.offset, &flags);
312 if (crypt_get_cipher_mode(cd))
313 r = asprintf(&dm_cipher, "%s-%s", crypt_get_cipher(cd), crypt_get_cipher_mode(cd));
315 r = asprintf(&dm_cipher, "%s", crypt_get_cipher(cd));
319 dmd.cipher = dm_cipher;
320 log_dbg("Trying to activate PLAIN device %s using cipher %s.", name, dmd.cipher);
322 r = dm_create_device(name, CRYPT_PLAIN, &dmd, 0);
325 if (!cd->plain_uuid && dm_query_device(name, DM_ACTIVE_UUID, &dmd) >= 0)
326 cd->plain_uuid = CONST_CAST(char*)dmd.uuid;
332 int crypt_confirm(struct crypt_device *cd, const char *msg)
334 if (!cd || !cd->confirm)
337 return cd->confirm(msg, cd->confirm_usrptr);
340 static int key_from_terminal(struct crypt_device *cd, char *msg, char **key,
341 size_t *key_len, int force_verify)
347 if(!msg && asprintf(&prompt, _("Enter passphrase for %s: "),
348 cd->backing_file ?: crypt_get_device_name(cd)) < 0)
355 *key = crypt_safe_alloc(DEFAULT_PASSPHRASE_SIZE_MAX);
360 r = cd->password(msg, *key, DEFAULT_PASSPHRASE_SIZE_MAX,
361 cd->password_usrptr);
363 crypt_safe_free(*key);
368 r = crypt_get_key(msg, key, key_len, 0, 0, NULL, cd->timeout,
369 (force_verify || cd->password_verify), cd);
372 return (r < 0) ? r: 0;
375 static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslot,
376 struct volume_key **vk)
378 char *passphrase_read = NULL;
379 size_t passphrase_size_read;
380 int r = -EINVAL, eperm = 0, tries = cd->tries;
384 crypt_free_volume_key(*vk);
387 r = key_from_terminal(cd, NULL, &passphrase_read,
388 &passphrase_size_read, 0);
389 /* Continue if it is just passphrase verify mismatch */
395 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase_read,
396 passphrase_size_read, &cd->hdr, vk, cd);
399 crypt_safe_free(passphrase_read);
400 passphrase_read = NULL;
401 } while (r == -EPERM && (--tries > 0));
404 crypt_free_volume_key(*vk);
407 /* Report wrong passphrase if at least one try failed */
408 if (eperm && r == -EPIPE)
412 crypt_safe_free(passphrase_read);
416 static int key_from_file(struct crypt_device *cd, char *msg,
417 char **key, size_t *key_len,
418 const char *key_file, size_t key_offset,
421 return crypt_get_key(msg, key, key_len, key_offset, key_size, key_file,
425 void crypt_set_log_callback(struct crypt_device *cd,
426 void (*log)(int level, const char *msg, void *usrptr),
433 cd->log_usrptr = usrptr;
437 void crypt_set_confirm_callback(struct crypt_device *cd,
438 int (*confirm)(const char *msg, void *usrptr),
441 cd->confirm = confirm;
442 cd->confirm_usrptr = usrptr;
445 void crypt_set_password_callback(struct crypt_device *cd,
446 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
449 cd->password = password;
450 cd->password_usrptr = usrptr;
453 static void _get_error(char *error, char *buf, size_t size)
455 if (!buf || size < 1)
458 strncpy(buf, error, size - 1);
459 buf[size - 1] = '\0';
465 void crypt_last_error(struct crypt_device *cd, char *buf, size_t size)
468 return _get_error(cd->error, buf, size);
471 /* Deprecated global error interface */
472 void crypt_get_error(char *buf, size_t size)
474 return _get_error(global_error, buf, size);
477 const char *crypt_get_dir(void)
482 int crypt_init(struct crypt_device **cd, const char *device)
484 struct crypt_device *h = NULL;
490 log_dbg("Allocating crypt device %s context.", device);
492 if (!(h = malloc(sizeof(struct crypt_device))))
495 memset(h, 0, sizeof(*h));
499 r = device_ready(NULL, device, O_RDONLY);
501 h->device = crypt_loop_get_device();
502 log_dbg("Not a block device, %s%s.",
503 h->device ? "using free loop device " :
504 "no free loop device found",
507 log_err(NULL, _("Cannot find a free loopback device.\n"));
512 /* Keep the loop open, dettached on last close. */
513 h->loop_fd = crypt_loop_attach(h->device, device, 0, 1, &readonly);
514 if (h->loop_fd == -1) {
515 log_err(NULL, _("Attaching loopback device failed "
516 "(loop device with autoclear flag is required).\n"));
521 h->backing_file = crypt_loop_backing_file(h->device);
522 r = device_ready(NULL, h->device, O_RDONLY);
530 if (!h->device && device && !(h->device = strdup(device))) {
535 if (dm_init(h, 1) < 0) {
540 h->iteration_time = 1000;
541 h->password_verify = 0;
543 h->rng_type = crypt_random_default_key_rng();
549 if (h->loop_fd != -1)
552 free(h->backing_file);
558 static int crypt_check_data_device_size(struct crypt_device *cd)
561 uint64_t size, size_min;
563 /* Check data device size, require at least one sector */
564 size_min = crypt_get_data_offset(cd) << SECTOR_SHIFT ?: SECTOR_SIZE;
566 r = device_size(crypt_get_device_name(cd), &size);
570 if (size < size_min) {
571 log_err(cd, _("LUKS header detected but device %s is too small.\n"),
572 crypt_get_device_name(cd));
579 int crypt_set_data_device(struct crypt_device *cd, const char *device)
584 log_dbg("Setting ciphertext data device to %s.", device ?: "(none)");
586 if (!isLUKS(cd->type)) {
587 log_err(cd, _("This operation is not supported for this device type.\n"));
591 /* metadata device must be set */
592 if (!cd->device || !device)
595 r = device_ready(NULL, device, O_RDONLY);
599 if (!(data_device = strdup(device)))
602 if (!cd->metadata_device)
603 cd->metadata_device = cd->device;
607 cd->device = data_device;
609 return crypt_check_data_device_size(cd);
612 static int _crypt_load_luks1(struct crypt_device *cd, int require_header, int repair)
614 struct luks_phdr hdr;
621 r = LUKS_read_phdr(mdata_device(cd), &hdr, require_header, repair, cd);
625 if (!cd->type && !(cd->type = strdup(CRYPT_LUKS1)))
628 memcpy(&cd->hdr, &hdr, sizeof(hdr));
633 int crypt_init_by_name_and_header(struct crypt_device **cd,
635 const char *header_device)
637 crypt_status_info ci;
638 struct crypt_dm_active_device dmd;
639 char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
643 log_dbg("Allocating crypt device context by device %s.", name);
645 ci = crypt_status(NULL, name);
646 if (ci == CRYPT_INVALID)
649 if (ci < CRYPT_ACTIVE) {
650 log_err(NULL, _("Device %s is not active.\n"), name);
654 r = dm_query_device(name, DM_ACTIVE_DEVICE | DM_ACTIVE_CIPHER |
655 DM_ACTIVE_UUID | DM_ACTIVE_KEYSIZE, &dmd);
662 r = crypt_init(cd, header_device);
664 r = crypt_init(cd, dmd.device);
666 /* Underlying device disappeared but mapping still active */
667 if (!dmd.device || r == -ENOTBLK)
668 log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
671 /* Underlying device is not readable but crypt mapping exists */
673 free(CONST_CAST(void*)dmd.device);
675 r = crypt_init(cd, NULL);
683 if (!strncmp(CRYPT_PLAIN, dmd.uuid, sizeof(CRYPT_PLAIN)-1))
684 (*cd)->type = strdup(CRYPT_PLAIN);
685 else if (!strncmp(CRYPT_LOOPAES, dmd.uuid, sizeof(CRYPT_LOOPAES)-1))
686 (*cd)->type = strdup(CRYPT_LOOPAES);
687 else if (!strncmp(CRYPT_LUKS1, dmd.uuid, sizeof(CRYPT_LUKS1)-1))
688 (*cd)->type = strdup(CRYPT_LUKS1);
690 log_dbg("Unknown UUID set, some parameters are not set.");
692 log_dbg("Active device has no UUID set, some parameters are not set.");
695 r = crypt_set_data_device(*cd, dmd.device);
700 /* Try to initialise basic parameters from active device */
702 if (!(*cd)->backing_file && dmd.device && crypt_loop_device(dmd.device) &&
703 !((*cd)->backing_file = crypt_loop_backing_file(dmd.device))) {
708 if (isPLAIN((*cd)->type)) {
709 (*cd)->plain_uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
710 (*cd)->plain_hdr.hash = NULL; /* no way to get this */
711 (*cd)->plain_hdr.offset = dmd.offset;
712 (*cd)->plain_hdr.skip = dmd.iv_offset;
713 (*cd)->plain_key_size = dmd.vk->keylength;
715 r = crypt_parse_name_and_mode(dmd.cipher, cipher, NULL, cipher_mode);
717 (*cd)->plain_cipher = strdup(cipher);
718 (*cd)->plain_cipher_mode = strdup(cipher_mode);
720 } else if (isLOOPAES((*cd)->type)) {
721 (*cd)->loopaes_uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
722 (*cd)->loopaes_hdr.offset = dmd.offset;
724 r = crypt_parse_name_and_mode(dmd.cipher, cipher,
725 &key_nums, cipher_mode);
727 (*cd)->loopaes_cipher = strdup(cipher);
728 (*cd)->loopaes_cipher_mode = strdup(cipher_mode);
729 /* version 3 uses last key for IV */
730 if (dmd.vk->keylength % key_nums)
732 (*cd)->loopaes_key_size = dmd.vk->keylength / key_nums;
734 } else if (isLUKS((*cd)->type)) {
735 if (mdata_device(*cd)) {
736 r = _crypt_load_luks1(*cd, 0, 0);
738 log_dbg("LUKS device header does not match active device.");
744 /* checks whether UUIDs match each other */
745 r = crypt_uuid_cmp(dmd.uuid, (*cd)->hdr.uuid);
747 log_dbg("LUKS device header uuid: %s mismatches DM returned uuid %s",
748 (*cd)->hdr.uuid, dmd.uuid);
762 crypt_free_volume_key(dmd.vk);
763 free(CONST_CAST(void*)dmd.device);
764 free(CONST_CAST(void*)dmd.cipher);
765 free(CONST_CAST(void*)dmd.uuid);
769 int crypt_init_by_name(struct crypt_device **cd, const char *name)
771 return crypt_init_by_name_and_header(cd, name, NULL);
774 static int _crypt_format_plain(struct crypt_device *cd,
776 const char *cipher_mode,
778 size_t volume_key_size,
779 struct crypt_params_plain *params)
781 if (!cipher || !cipher_mode) {
782 log_err(cd, _("Invalid plain crypt parameters.\n"));
786 if (volume_key_size > 1024) {
787 log_err(cd, _("Invalid key size.\n"));
791 cd->plain_key_size = volume_key_size;
792 cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
796 cd->plain_cipher = strdup(cipher);
797 cd->plain_cipher_mode = strdup(cipher_mode);
800 cd->plain_uuid = strdup(uuid);
802 if (params && params->hash)
803 cd->plain_hdr.hash = strdup(params->hash);
805 cd->plain_hdr.offset = params ? params->offset : 0;
806 cd->plain_hdr.skip = params ? params->skip : 0;
807 cd->plain_hdr.size = params ? params->size : 0;
809 if (!cd->plain_cipher || !cd->plain_cipher_mode)
815 static int _crypt_format_luks1(struct crypt_device *cd,
817 const char *cipher_mode,
819 const char *volume_key,
820 size_t volume_key_size,
821 struct crypt_params_luks1 *params)
824 unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
825 unsigned long alignment_offset = 0;
827 if (!mdata_device(cd)) {
828 log_err(cd, _("Can't format LUKS without device.\n"));
833 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
836 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
841 if (params && params->data_device) {
842 cd->metadata_device = cd->device;
843 if (!(cd->device = strdup(params->data_device)))
845 required_alignment = params->data_alignment * SECTOR_SIZE;
846 } else if (params && params->data_alignment) {
847 required_alignment = params->data_alignment * SECTOR_SIZE;
849 get_topology_alignment(cd->device, &required_alignment,
850 &alignment_offset, DEFAULT_DISK_ALIGNMENT);
852 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
853 (params && params->hash) ? params->hash : "sha1",
855 required_alignment / SECTOR_SIZE,
856 alignment_offset / SECTOR_SIZE,
857 cd->iteration_time, &cd->PBKDF2_per_sec,
858 cd->metadata_device, cd);
862 /* Wipe first 8 sectors - fs magic numbers etc. */
863 r = crypt_wipe(mdata_device(cd), 0, 8 * SECTOR_SIZE, CRYPT_WIPE_ZERO, 1);
866 log_err(cd, _("Cannot format device %s which is still in use.\n"),
869 log_err(cd, _("Cannot wipe header on device %s.\n"),
875 r = LUKS_write_phdr(mdata_device(cd), &cd->hdr, cd);
880 static int _crypt_format_loopaes(struct crypt_device *cd,
883 size_t volume_key_size,
884 struct crypt_params_loopaes *params)
886 if (!mdata_device(cd)) {
887 log_err(cd, _("Can't format LOOPAES without device.\n"));
891 if (volume_key_size > 1024) {
892 log_err(cd, _("Invalid key size.\n"));
896 cd->loopaes_key_size = volume_key_size;
898 cd->loopaes_cipher = strdup(cipher ?: DEFAULT_LOOPAES_CIPHER);
901 cd->loopaes_uuid = strdup(uuid);
903 if (params && params->hash)
904 cd->loopaes_hdr.hash = strdup(params->hash);
906 cd->loopaes_hdr.offset = params ? params->offset : 0;
907 cd->loopaes_hdr.skip = params ? params->skip : 0;
912 int crypt_format(struct crypt_device *cd,
915 const char *cipher_mode,
917 const char *volume_key,
918 size_t volume_key_size,
927 log_dbg("Context already formatted as %s.", cd->type);
931 log_dbg("Formatting device %s as type %s.", mdata_device(cd) ?: "(none)", type);
938 r = _crypt_format_plain(cd, cipher, cipher_mode,
939 uuid, volume_key_size, params);
940 else if (isLUKS(type))
941 r = _crypt_format_luks1(cd, cipher, cipher_mode,
942 uuid, volume_key, volume_key_size, params);
943 else if (isLOOPAES(type))
944 r = _crypt_format_loopaes(cd, cipher, uuid, volume_key_size, params);
946 /* FIXME: allow plugins here? */
947 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
951 if (!r && !(cd->type = strdup(type)))
955 crypt_free_volume_key(cd->volume_key);
956 cd->volume_key = NULL;
962 int crypt_load(struct crypt_device *cd,
963 const char *requested_type,
964 void *params __attribute__((unused)))
968 log_dbg("Trying to load %s crypt type from device %s.",
969 requested_type ?: "any", mdata_device(cd) ?: "(none)");
971 if (!mdata_device(cd))
974 if (requested_type && !isLUKS(requested_type))
977 if (cd->type && !isLUKS(cd->type)) {
978 log_dbg("Context is already initialised to type %s", cd->type);
982 r = _crypt_load_luks1(cd, 1, 0);
986 /* cd->type and header must be set in context */
987 r = crypt_check_data_device_size(cd);
996 int crypt_repair(struct crypt_device *cd,
997 const char *requested_type,
998 void *params __attribute__((unused)))
1002 log_dbg("Trying to repair %s crypt type from device %s.",
1003 requested_type ?: "any", mdata_device(cd) ?: "(none)");
1005 if (!mdata_device(cd))
1008 if (requested_type && !isLUKS(requested_type))
1012 /* Load with repair */
1013 r = _crypt_load_luks1(cd, 1, 1);
1017 /* cd->type and header must be set in context */
1018 r = crypt_check_data_device_size(cd);
1027 int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
1029 struct crypt_dm_active_device dmd;
1032 /* Device context type must be initialised */
1033 if (!cd->type || !crypt_get_uuid(cd))
1036 log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
1038 r = dm_query_device(name, DM_ACTIVE_DEVICE | DM_ACTIVE_CIPHER |
1039 DM_ACTIVE_UUID | DM_ACTIVE_KEYSIZE |
1040 DM_ACTIVE_KEY, &dmd);
1042 log_err(NULL, _("Device %s is not active.\n"), name);
1051 r = device_check_and_adjust(cd, dmd.device, DEV_OK, &new_size, &dmd.offset, &dmd.flags);
1055 if (new_size == dmd.size) {
1056 log_dbg("Device has already requested size %" PRIu64
1057 " sectors.", dmd.size);
1060 dmd.size = new_size;
1061 r = dm_create_device(name, cd->type, &dmd, 1);
1064 crypt_free_volume_key(dmd.vk);
1065 free(CONST_CAST(void*)dmd.cipher);
1066 free(CONST_CAST(void*)dmd.device);
1067 free(CONST_CAST(void*)dmd.uuid);
1072 int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
1074 if (!isLUKS(cd->type)) {
1075 log_err(cd, _("This operation is not supported for this device type.\n"));
1079 if (uuid && !strncmp(uuid, cd->hdr.uuid, sizeof(cd->hdr.uuid))) {
1080 log_dbg("UUID is the same as requested (%s) for device %s.",
1081 uuid, mdata_device(cd));
1086 log_dbg("Requested new UUID change to %s for %s.", uuid, mdata_device(cd));
1088 log_dbg("Requested new UUID refresh for %s.", mdata_device(cd));
1090 if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
1093 return LUKS_hdr_uuid_set(mdata_device(cd), &cd->hdr, uuid, cd);
1096 int crypt_header_backup(struct crypt_device *cd,
1097 const char *requested_type,
1098 const char *backup_file)
1102 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1105 r = init_crypto(cd);
1109 log_dbg("Requested header backup of device %s (%s) to "
1110 "file %s.", mdata_device(cd), requested_type, backup_file);
1112 return LUKS_hdr_backup(backup_file, mdata_device(cd), &cd->hdr, cd);
1115 int crypt_header_restore(struct crypt_device *cd,
1116 const char *requested_type,
1117 const char *backup_file)
1121 if (requested_type && !isLUKS(requested_type))
1124 /* Some hash functions need initialized gcrypt library */
1125 r = init_crypto(cd);
1129 log_dbg("Requested header restore to device %s (%s) from "
1130 "file %s.", mdata_device(cd), requested_type, backup_file);
1132 return LUKS_hdr_restore(backup_file, mdata_device(cd), &cd->hdr, cd);
1135 void crypt_free(struct crypt_device *cd)
1138 log_dbg("Releasing crypt device %s context.", mdata_device(cd));
1140 if (cd->loop_fd != -1)
1144 crypt_free_volume_key(cd->volume_key);
1147 free(cd->metadata_device);
1148 free(cd->backing_file);
1151 /* used in plain device only */
1152 free(CONST_CAST(void*)cd->plain_hdr.hash);
1153 free(cd->plain_cipher);
1154 free(cd->plain_cipher_mode);
1155 free(cd->plain_uuid);
1157 /* used in loop-AES device only */
1158 free(CONST_CAST(void*)cd->loopaes_hdr.hash);
1159 free(cd->loopaes_cipher);
1160 free(cd->loopaes_uuid);
1166 int crypt_suspend(struct crypt_device *cd,
1169 crypt_status_info ci;
1172 log_dbg("Suspending volume %s.", name);
1174 if (!isLUKS(cd->type)) {
1175 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1180 ci = crypt_status(NULL, name);
1181 if (ci < CRYPT_ACTIVE) {
1182 log_err(cd, _("Volume %s is not active.\n"), name);
1186 if (!cd && dm_init(NULL, 1) < 0)
1189 r = dm_status_suspended(name);
1194 log_err(cd, _("Volume %s is already suspended.\n"), name);
1199 r = dm_suspend_and_wipe_key(name);
1201 log_err(cd, "Suspend is not supported for device %s.\n", name);
1203 log_err(cd, "Error during suspending device %s.\n", name);
1210 int crypt_resume_by_passphrase(struct crypt_device *cd,
1213 const char *passphrase,
1214 size_t passphrase_size)
1216 struct volume_key *vk = NULL;
1219 log_dbg("Resuming volume %s.", name);
1221 if (!isLUKS(cd->type)) {
1222 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1227 r = dm_status_suspended(name);
1232 log_err(cd, _("Volume %s is not suspended.\n"), name);
1237 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase,
1238 passphrase_size, &cd->hdr, &vk, cd);
1240 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1244 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1246 log_err(cd, "Resume is not supported for device %s.\n", name);
1248 log_err(cd, "Error during resuming device %s.\n", name);
1252 crypt_free_volume_key(vk);
1253 return r < 0 ? r : keyslot;
1256 int crypt_resume_by_keyfile_offset(struct crypt_device *cd,
1259 const char *keyfile,
1260 size_t keyfile_size,
1261 size_t keyfile_offset)
1263 struct volume_key *vk = NULL;
1264 char *passphrase_read = NULL;
1265 size_t passphrase_size_read;
1268 log_dbg("Resuming volume %s.", name);
1270 if (!isLUKS(cd->type)) {
1271 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1276 r = dm_status_suspended(name);
1281 log_err(cd, _("Volume %s is not suspended.\n"), name);
1288 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1289 &passphrase_size_read, keyfile, keyfile_offset,
1294 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase_read,
1295 passphrase_size_read, &cd->hdr, &vk, cd);
1300 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1302 log_err(cd, "Error during resuming device %s.\n", name);
1304 crypt_safe_free(passphrase_read);
1305 crypt_free_volume_key(vk);
1306 return r < 0 ? r : keyslot;
1309 int crypt_resume_by_keyfile(struct crypt_device *cd,
1312 const char *keyfile,
1313 size_t keyfile_size)
1315 return crypt_resume_by_keyfile_offset(cd, name, keyslot,
1316 keyfile, keyfile_size, 0);
1319 // slot manipulation
1320 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1321 int keyslot, // -1 any
1322 const char *passphrase, // NULL -> terminal
1323 size_t passphrase_size,
1324 const char *new_passphrase, // NULL -> terminal
1325 size_t new_passphrase_size)
1327 struct volume_key *vk = NULL;
1328 char *password = NULL, *new_password = NULL;
1329 size_t passwordLen, new_passwordLen;
1332 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1333 "new passphrase %sprovided.",
1334 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1336 if (!isLUKS(cd->type)) {
1337 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1341 r = keyslot_verify_or_find_empty(cd, &keyslot);
1345 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1346 /* No slots used, try to use pre-generated key in header */
1347 if (cd->volume_key) {
1348 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1349 r = vk ? 0 : -ENOMEM;
1351 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1354 } else if (passphrase) {
1355 /* Passphrase provided, use it to unlock existing keyslot */
1356 r = LUKS_open_key_with_hdr(mdata_device(cd), CRYPT_ANY_SLOT, passphrase,
1357 passphrase_size, &cd->hdr, &vk, cd);
1359 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1360 r = key_from_terminal(cd, _("Enter any passphrase: "),
1361 &password, &passwordLen, 0);
1365 r = LUKS_open_key_with_hdr(mdata_device(cd), CRYPT_ANY_SLOT, password,
1366 passwordLen, &cd->hdr, &vk, cd);
1367 crypt_safe_free(password);
1373 if (new_passphrase) {
1374 new_password = CONST_CAST(char*)new_passphrase;
1375 new_passwordLen = new_passphrase_size;
1377 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1378 &new_password, &new_passwordLen, 1);
1383 r = LUKS_set_key(mdata_device(cd), keyslot, new_password, new_passwordLen,
1384 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1389 if (!new_passphrase)
1390 crypt_safe_free(new_password);
1391 crypt_free_volume_key(vk);
1392 return r ?: keyslot;
1395 int crypt_keyslot_add_by_keyfile_offset(struct crypt_device *cd,
1397 const char *keyfile,
1398 size_t keyfile_size,
1399 size_t keyfile_offset,
1400 const char *new_keyfile,
1401 size_t new_keyfile_size,
1402 size_t new_keyfile_offset)
1404 struct volume_key *vk = NULL;
1405 char *password = NULL; size_t passwordLen;
1406 char *new_password = NULL; size_t new_passwordLen;
1409 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1410 keyfile ?: "[none]", new_keyfile ?: "[none]");
1412 if (!isLUKS(cd->type)) {
1413 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1417 r = keyslot_verify_or_find_empty(cd, &keyslot);
1421 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1422 /* No slots used, try to use pre-generated key in header */
1423 if (cd->volume_key) {
1424 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1425 r = vk ? 0 : -ENOMEM;
1427 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1431 /* Read password from file of (if NULL) from terminal */
1433 r = key_from_file(cd, _("Enter any passphrase: "),
1434 &password, &passwordLen,
1435 keyfile, keyfile_offset, keyfile_size);
1437 r = key_from_terminal(cd, _("Enter any passphrase: "),
1438 &password, &passwordLen, 0);
1442 r = LUKS_open_key_with_hdr(mdata_device(cd), CRYPT_ANY_SLOT, password, passwordLen,
1450 r = key_from_file(cd, _("Enter new passphrase for key slot: "),
1451 &new_password, &new_passwordLen, new_keyfile,
1452 new_keyfile_offset, new_keyfile_size);
1454 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1455 &new_password, &new_passwordLen, 1);
1459 r = LUKS_set_key(mdata_device(cd), keyslot, new_password, new_passwordLen,
1460 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1462 crypt_safe_free(password);
1463 crypt_safe_free(new_password);
1464 crypt_free_volume_key(vk);
1465 return r < 0 ? r : keyslot;
1468 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1470 const char *keyfile,
1471 size_t keyfile_size,
1472 const char *new_keyfile,
1473 size_t new_keyfile_size)
1475 return crypt_keyslot_add_by_keyfile_offset(cd, keyslot,
1476 keyfile, keyfile_size, 0,
1477 new_keyfile, new_keyfile_size, 0);
1480 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1482 const char *volume_key,
1483 size_t volume_key_size,
1484 const char *passphrase,
1485 size_t passphrase_size)
1487 struct volume_key *vk = NULL;
1489 char *new_password = NULL; size_t new_passwordLen;
1491 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1493 if (!isLUKS(cd->type)) {
1494 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1499 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1500 else if (cd->volume_key)
1501 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1506 r = LUKS_verify_volume_key(&cd->hdr, vk);
1508 log_err(cd, _("Volume key does not match the volume.\n"));
1512 r = keyslot_verify_or_find_empty(cd, &keyslot);
1517 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1518 &new_password, &new_passwordLen, 1);
1521 passphrase = new_password;
1522 passphrase_size = new_passwordLen;
1525 r = LUKS_set_key(mdata_device(cd), keyslot, passphrase, passphrase_size,
1526 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1528 crypt_safe_free(new_password);
1529 crypt_free_volume_key(vk);
1530 return (r < 0) ? r : keyslot;
1533 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1535 crypt_keyslot_info ki;
1537 log_dbg("Destroying keyslot %d.", keyslot);
1539 if (!isLUKS(cd->type)) {
1540 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1544 ki = crypt_keyslot_status(cd, keyslot);
1545 if (ki == CRYPT_SLOT_INVALID) {
1546 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1550 if (ki == CRYPT_SLOT_INACTIVE) {
1551 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1555 return LUKS_del_key(mdata_device(cd), keyslot, &cd->hdr, cd);
1558 // activation/deactivation of device mapping
1559 int crypt_activate_by_passphrase(struct crypt_device *cd,
1562 const char *passphrase,
1563 size_t passphrase_size,
1566 crypt_status_info ci;
1567 struct volume_key *vk = NULL;
1568 char *read_passphrase = NULL;
1569 size_t passphraseLen = 0;
1572 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1573 name ? "Activating" : "Checking", name ?: "",
1574 keyslot, passphrase ? "" : "[none] ");
1577 ci = crypt_status(NULL, name);
1578 if (ci == CRYPT_INVALID)
1580 else if (ci >= CRYPT_ACTIVE) {
1581 log_err(cd, _("Device %s already exists.\n"), name);
1586 /* plain, use hashed passphrase */
1587 if (isPLAIN(cd->type)) {
1592 r = key_from_terminal(cd, NULL, &read_passphrase,
1596 passphrase = read_passphrase;
1597 passphrase_size = passphraseLen;
1600 r = process_key(cd, cd->plain_hdr.hash,
1602 passphrase, passphrase_size, &vk);
1606 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1608 } else if (isLUKS(cd->type)) {
1609 /* provided passphrase, do not retry */
1611 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase,
1612 passphrase_size, &cd->hdr, &vk, cd);
1614 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1619 r = LUKS1_activate(cd, name, vk, flags);
1624 crypt_safe_free(read_passphrase);
1625 crypt_free_volume_key(vk);
1627 return r < 0 ? r : keyslot;
1630 int crypt_activate_by_keyfile_offset(struct crypt_device *cd,
1633 const char *keyfile,
1634 size_t keyfile_size,
1635 size_t keyfile_offset,
1638 crypt_status_info ci;
1639 struct volume_key *vk = NULL;
1640 char *passphrase_read = NULL;
1641 size_t passphrase_size_read;
1642 unsigned int key_count = 0;
1645 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1646 name ?: "", keyslot, keyfile ?: "[none]");
1649 ci = crypt_status(NULL, name);
1650 if (ci == CRYPT_INVALID)
1652 else if (ci >= CRYPT_ACTIVE) {
1653 log_err(cd, _("Device %s already exists.\n"), name);
1661 if (isPLAIN(cd->type)) {
1665 r = key_from_file(cd, _("Enter passphrase: "),
1666 &passphrase_read, &passphrase_size_read,
1667 keyfile, keyfile_offset, keyfile_size);
1671 r = process_key(cd, cd->plain_hdr.hash,
1673 passphrase_read, passphrase_size_read, &vk);
1677 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1678 } else if (isLUKS(cd->type)) {
1679 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1680 &passphrase_size_read, keyfile, keyfile_offset, keyfile_size);
1683 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase_read,
1684 passphrase_size_read, &cd->hdr, &vk, cd);
1690 r = LUKS1_activate(cd, name, vk, flags);
1695 } else if (isLOOPAES(cd->type)) {
1696 r = key_from_file(cd, NULL, &passphrase_read, &passphrase_size_read,
1697 keyfile, keyfile_offset, keyfile_size);
1700 r = LOOPAES_parse_keyfile(cd, &vk, cd->loopaes_hdr.hash, &key_count,
1701 passphrase_read, passphrase_size_read);
1705 r = LOOPAES_activate(cd, name, cd->loopaes_cipher,
1706 key_count, vk, flags);
1711 crypt_safe_free(passphrase_read);
1712 crypt_free_volume_key(vk);
1717 int crypt_activate_by_keyfile(struct crypt_device *cd,
1720 const char *keyfile,
1721 size_t keyfile_size,
1724 return crypt_activate_by_keyfile_offset(cd, name, keyslot, keyfile,
1725 keyfile_size, 0, flags);
1728 int crypt_activate_by_volume_key(struct crypt_device *cd,
1730 const char *volume_key,
1731 size_t volume_key_size,
1734 crypt_status_info ci;
1735 struct volume_key *vk = NULL;
1738 log_dbg("Activating volume %s by volume key.", name);
1741 ci = crypt_status(NULL, name);
1742 if (ci == CRYPT_INVALID)
1744 else if (ci >= CRYPT_ACTIVE) {
1745 log_err(cd, _("Device %s already exists.\n"), name);
1750 /* use key directly, no hash */
1751 if (isPLAIN(cd->type)) {
1755 if (!volume_key || !volume_key_size || volume_key_size != cd->plain_key_size) {
1756 log_err(cd, _("Incorrect volume key specified for plain device.\n"));
1760 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1764 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1765 } else if (isLUKS(cd->type)) {
1766 /* If key is not provided, try to use internal key */
1768 if (!cd->volume_key) {
1769 log_err(cd, _("Volume key does not match the volume.\n"));
1772 volume_key_size = cd->volume_key->keylength;
1773 volume_key = cd->volume_key->key;
1776 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1779 r = LUKS_verify_volume_key(&cd->hdr, vk);
1782 log_err(cd, _("Volume key does not match the volume.\n"));
1785 r = LUKS1_activate(cd, name, vk, flags);
1787 log_err(cd, _("Device type is not properly initialised.\n"));
1789 crypt_free_volume_key(vk);
1794 int crypt_deactivate(struct crypt_device *cd, const char *name)
1801 log_dbg("Deactivating volume %s.", name);
1803 if (!cd && dm_init(NULL, 1) < 0)
1806 switch (crypt_status(cd, name)) {
1809 r = dm_remove_device(name, 0, 0);
1811 case CRYPT_INACTIVE:
1812 log_err(cd, _("Device %s is not active.\n"), name);
1816 log_err(cd, _("Invalid device %s.\n"), name);
1826 int crypt_volume_key_get(struct crypt_device *cd,
1829 size_t *volume_key_size,
1830 const char *passphrase,
1831 size_t passphrase_size)
1833 struct volume_key *vk = NULL;
1837 if (crypt_fips_mode()) {
1838 log_err(cd, "Function not available in FIPS mode.\n");
1842 key_len = crypt_get_volume_key_size(cd);
1843 if (key_len > *volume_key_size) {
1844 log_err(cd, _("Volume key buffer too small.\n"));
1848 if (isPLAIN(cd->type) && cd->plain_hdr.hash) {
1849 r = process_key(cd, cd->plain_hdr.hash, key_len,
1850 passphrase, passphrase_size, &vk);
1852 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
1853 } else if (isLUKS(cd->type)) {
1854 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase,
1855 passphrase_size, &cd->hdr, &vk, cd);
1858 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
1861 memcpy(volume_key, vk->key, vk->keylength);
1862 *volume_key_size = vk->keylength;
1865 crypt_free_volume_key(vk);
1869 int crypt_volume_key_verify(struct crypt_device *cd,
1870 const char *volume_key,
1871 size_t volume_key_size)
1873 struct volume_key *vk;
1876 if (!isLUKS(cd->type)) {
1877 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1881 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1885 r = LUKS_verify_volume_key(&cd->hdr, vk);
1888 log_err(cd, _("Volume key does not match the volume.\n"));
1890 crypt_free_volume_key(vk);
1895 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
1897 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
1898 cd->timeout = timeout_sec;
1901 void crypt_set_password_retry(struct crypt_device *cd, int tries)
1903 log_dbg("Password retry count set to %d.", tries);
1907 void crypt_set_iteration_time(struct crypt_device *cd, uint64_t iteration_time_ms)
1909 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
1910 cd->iteration_time = iteration_time_ms;
1912 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
1914 crypt_set_iteration_time(cd, iteration_time_ms);
1917 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
1919 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
1920 cd->password_verify = password_verify ? 1 : 0;
1923 void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
1926 case CRYPT_RNG_URANDOM:
1927 case CRYPT_RNG_RANDOM:
1928 log_dbg("RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
1929 cd->rng_type = rng_type;
1933 int crypt_get_rng_type(struct crypt_device *cd)
1938 return cd->rng_type;
1941 int crypt_memory_lock(struct crypt_device *cd, int lock)
1943 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
1947 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
1951 if (!cd && dm_init(NULL, 1) < 0)
1952 return CRYPT_INVALID;
1954 r = dm_status_device(name);
1959 if (r < 0 && r != -ENODEV)
1960 return CRYPT_INVALID;
1963 return CRYPT_ACTIVE;
1968 return CRYPT_INACTIVE;
1971 static void hexprintICB(struct crypt_device *cd, char *d, int n)
1974 for(i = 0; i < n; i++)
1975 log_std(cd, "%02hhx ", (char)d[i]);
1978 int crypt_dump(struct crypt_device *cd)
1981 if (!isLUKS(cd->type)) { //FIXME
1982 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1986 log_std(cd, "LUKS header information for %s\n\n", mdata_device(cd));
1987 log_std(cd, "Version: \t%d\n", cd->hdr.version);
1988 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
1989 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
1990 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec);
1991 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
1992 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
1993 log_std(cd, "MK digest: \t");
1994 hexprintICB(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE);
1996 log_std(cd, "MK salt: \t");
1997 hexprintICB(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2);
1998 log_std(cd, "\n \t");
1999 hexprintICB(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
2001 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
2002 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
2003 for(i = 0; i < LUKS_NUMKEYS; i++) {
2004 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
2005 log_std(cd, "Key Slot %d: ENABLED\n",i);
2006 log_std(cd, "\tIterations: \t%d\n",
2007 cd->hdr.keyblock[i].passwordIterations);
2008 log_std(cd, "\tSalt: \t");
2009 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt,
2011 log_std(cd, "\n\t \t");
2012 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt +
2013 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
2016 log_std(cd, "\tKey material offset:\t%d\n",
2017 cd->hdr.keyblock[i].keyMaterialOffset);
2018 log_std(cd, "\tAF stripes: \t%d\n",
2019 cd->hdr.keyblock[i].stripes);
2022 log_std(cd, "Key Slot %d: DISABLED\n", i);
2028 const char *crypt_get_cipher(struct crypt_device *cd)
2030 if (isPLAIN(cd->type))
2031 return cd->plain_cipher;
2033 if (isLUKS(cd->type))
2034 return cd->hdr.cipherName;
2036 if (isLOOPAES(cd->type))
2037 return cd->loopaes_cipher;
2042 const char *crypt_get_cipher_mode(struct crypt_device *cd)
2044 if (isPLAIN(cd->type))
2045 return cd->plain_cipher_mode;
2047 if (isLUKS(cd->type))
2048 return cd->hdr.cipherMode;
2050 if (isLOOPAES(cd->type))
2051 return cd->loopaes_cipher_mode;
2056 const char *crypt_get_uuid(struct crypt_device *cd)
2058 if (isLUKS(cd->type))
2059 return cd->hdr.uuid;
2061 if (isPLAIN(cd->type))
2062 return cd->plain_uuid;
2064 if (isLOOPAES(cd->type))
2065 return cd->loopaes_uuid;
2070 const char *crypt_get_device_name(struct crypt_device *cd)
2076 int crypt_get_volume_key_size(struct crypt_device *cd)
2078 if (isPLAIN(cd->type))
2079 return cd->plain_key_size;
2081 if (isLUKS(cd->type))
2082 return cd->hdr.keyBytes;
2084 if (isLOOPAES(cd->type))
2085 return cd->loopaes_key_size;
2090 uint64_t crypt_get_data_offset(struct crypt_device *cd)
2092 if (isPLAIN(cd->type))
2093 return cd->plain_hdr.offset;
2095 if (isLUKS(cd->type))
2096 return cd->hdr.payloadOffset;
2098 if (isLOOPAES(cd->type))
2099 return cd->loopaes_hdr.offset;
2104 uint64_t crypt_get_iv_offset(struct crypt_device *cd)
2106 if (isPLAIN(cd->type))
2107 return cd->plain_hdr.skip;
2109 if (isLUKS(cd->type))
2112 if (isLOOPAES(cd->type))
2113 return cd->loopaes_hdr.skip;
2118 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
2120 if (!isLUKS(cd->type)) {
2121 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2122 return CRYPT_SLOT_INVALID;
2125 return LUKS_keyslot_info(&cd->hdr, keyslot);
2128 int crypt_keyslot_max(const char *type)
2130 if (type && isLUKS(type))
2131 return LUKS_NUMKEYS;
2136 const char *crypt_get_type(struct crypt_device *cd)
2141 int crypt_get_active_device(struct crypt_device *cd __attribute__((unused)),
2143 struct crypt_active_device *cad)
2145 struct crypt_dm_active_device dmd;
2148 r = dm_query_device(name, 0, &dmd);
2152 cad->offset = dmd.offset;
2153 cad->iv_offset = dmd.iv_offset;
2154 cad->size = dmd.size;
2155 cad->flags = dmd.flags;