8 #include "libcryptsetup.h"
16 struct volume_key *volume_key;
18 uint64_t iteration_time;
22 /* used in CRYPT_LUKS1 */
24 uint64_t PBKDF2_per_sec;
26 /* used in CRYPT_PLAIN */
27 struct crypt_params_plain plain_hdr;
29 char *plain_cipher_mode;
32 /* callbacks definitions */
33 void (*log)(int level, const char *msg, void *usrptr);
35 int (*confirm)(const char *msg, void *usrptr);
37 int (*password)(const char *msg, char *buf, size_t length, void *usrptr);
38 void *password_usrptr;
42 static void (*_default_log)(int level, const char *msg, void *usrptr) = NULL;
43 static int _debug_level = 0;
45 void crypt_set_debug_level(int level)
50 int crypt_get_debug_level()
55 void crypt_log(struct crypt_device *cd, int level, const char *msg)
58 cd->log(level, msg, cd->log_usrptr);
59 else if (_default_log)
60 _default_log(level, msg, NULL);
63 void logger(struct crypt_device *cd, int level, const char *file,
64 int line, const char *format, ...)
69 va_start(argp, format);
71 if (vasprintf(&target, format, argp) > 0) {
73 crypt_log(cd, level, target);
75 } else if (_debug_level)
76 printf("# %s:%d %s\n", file ?: "?", line, target);
78 } else if (_debug_level)
79 printf("# %s\n", target);
88 * Password processing behaviour matrix of process_key
90 * from binary file: check if there is sufficently large key material
91 * interactive & from fd: hash if requested, otherwise crop or pad with '0'
93 static char *process_key(struct crypt_device *cd, const char *hash_name,
94 const char *key_file, size_t key_size,
95 const char *pass, size_t passLen)
97 char *key = crypt_safe_alloc(key_size);
98 memset(key, 0, key_size);
100 /* key is coming from binary file */
101 if (key_file && strcmp(key_file, "-")) {
102 if(passLen < key_size) {
103 log_err(cd, _("Cannot not read %d bytes from key file %s.\n"),
105 crypt_safe_free(key);
108 memcpy(key, pass, key_size);
112 /* key is coming from tty, fd or binary stdin */
114 if (hash(NULL, hash_name, key, key_size, pass, passLen) < 0) {
115 log_err(cd, _("Key processing error (using hash algorithm %s).\n"),
117 crypt_safe_free(key);
120 } else if (passLen > key_size) {
121 memcpy(key, pass, key_size);
123 memcpy(key, pass, passLen);
129 static int isPLAIN(const char *type)
131 return (type && !strcmp(CRYPT_PLAIN, type));
134 static int isLUKS(const char *type)
136 return (type && !strcmp(CRYPT_LUKS1, type));
139 /* keyslot helpers */
140 static int keyslot_verify_or_find_empty(struct crypt_device *cd, int *keyslot)
142 if (*keyslot == CRYPT_ANY_SLOT) {
143 *keyslot = LUKS_keyslot_find_empty(&cd->hdr);
145 log_err(cd, _("All key slots full.\n"));
150 switch (LUKS_keyslot_info(&cd->hdr, *keyslot)) {
151 case CRYPT_SLOT_INVALID:
152 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
153 *keyslot, LUKS_NUMKEYS - 1);
155 case CRYPT_SLOT_INACTIVE:
158 log_err(cd, _("Key slot %d is full, please select another one.\n"),
166 static int verify_other_keyslot(struct crypt_device *cd,
167 const char *key_file,
170 struct volume_key *vk;
171 crypt_keyslot_info ki;
173 char *password = NULL;
174 unsigned int passwordLen;
176 crypt_get_key(_("Enter any remaining LUKS passphrase: "), &password,
177 &passwordLen, 0, key_file, cd->timeout, cd->password_verify, cd);
181 ki = crypt_keyslot_status(cd, keyIndex);
182 if (ki == CRYPT_SLOT_ACTIVE) /* Not last slot */
183 LUKS_keyslot_set(&cd->hdr, keyIndex, 0);
185 openedIndex = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT,
186 password, passwordLen,
189 if (ki == CRYPT_SLOT_ACTIVE)
190 LUKS_keyslot_set(&cd->hdr, keyIndex, 1);
191 crypt_free_volume_key(vk);
192 crypt_safe_free(password);
197 log_verbose(cd, _("Key slot %d verified.\n"), openedIndex);
201 static int find_keyslot_by_passphrase(struct crypt_device *cd,
202 const char *key_file,
205 struct volume_key *vk;
206 char *password = NULL;
207 unsigned int passwordLen;
210 crypt_get_key(message,&password,&passwordLen, 0, key_file,
211 cd->timeout, cd->password_verify, cd);
215 keyIndex = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
216 passwordLen, &cd->hdr, &vk, cd);
217 crypt_free_volume_key(vk);
218 crypt_safe_free(password);
223 static int device_check_and_adjust(struct crypt_device *cd,
225 uint64_t *size, uint64_t *offset,
228 struct device_infos infos;
230 if (!device || get_device_infos(device, &infos, cd) < 0) {
231 log_err(cd, _("Cannot get info about device %s.\n"),
239 log_err(cd, _("Device %s has zero size.\n"), device);
242 if (*size < *offset) {
243 log_err(cd, _("Device %s is too small.\n"), device);
252 log_dbg("Calculated device size is %" PRIu64 " sectors (%s), offset %" PRIu64 ".",
253 *size, *read_only ? "RO" : "RW", *offset);
257 static int luks_remove_helper(struct crypt_device *cd,
259 const char *other_key_file,
260 const char *key_file,
263 crypt_keyslot_info ki;
266 if (key_slot == CRYPT_ANY_SLOT) {
267 key_slot = find_keyslot_by_passphrase(cd, key_file,
268 _("Enter LUKS passphrase to be deleted: "));
274 log_std(cd, _("Key slot %d selected for deletion.\n"), key_slot);
277 ki = crypt_keyslot_status(cd, key_slot);
278 if (ki == CRYPT_SLOT_INVALID) {
279 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
280 key_slot, LUKS_NUMKEYS - 1);
284 if (ki <= CRYPT_SLOT_INACTIVE) {
285 log_err(cd, _("Key %d not active. Can't wipe.\n"), key_slot);
290 if (ki == CRYPT_SLOT_ACTIVE_LAST && cd->confirm &&
291 !(cd->confirm(_("This is the last keyslot."
292 " Device will become unusable after purging this key."),
293 cd->confirm_usrptr))) {
299 r = verify_other_keyslot(cd, other_key_file, key_slot);
304 r = crypt_keyslot_destroy(cd, key_slot);
306 return (r < 0) ? r : 0;
309 static int create_device_helper(struct crypt_device *cd,
313 const char *cipher_mode,
314 const char *key_file,
326 crypt_status_info ci;
327 char *dm_cipher = NULL;
328 char *processed_key = NULL;
334 ci = crypt_status(cd, name);
335 if (ci == CRYPT_INVALID)
338 if (reload && ci < CRYPT_ACTIVE)
341 if (!reload && ci >= CRYPT_ACTIVE) {
342 log_err(cd, _("Device %s already exists.\n"), name);
346 if (key_size < 0 || key_size > 1024) {
347 log_err(cd, _("Invalid key size %d.\n"), key_size);
351 r = device_check_and_adjust(cd, cd->device, &size, &offset, &read_only);
355 if (cipher_mode && asprintf(&dm_cipher, "%s-%s", cipher, cipher_mode) < 0)
358 processed_key = process_key(cd, hash, key_file, key_size, key, keyLen);
362 r = dm_create_device(name, cd->device, dm_cipher ?: cipher, cd->type, uuid, size, skip, offset,
363 key_size, processed_key, read_only, reload);
366 crypt_safe_free(processed_key);
370 static int open_from_hdr_and_vk(struct crypt_device *cd,
371 struct volume_key *vk,
375 uint64_t size, offset;
377 int read_only, no_uuid, r;
380 offset = crypt_get_data_offset(cd);
381 read_only = flags & CRYPT_ACTIVATE_READONLY;
382 no_uuid = flags & CRYPT_ACTIVATE_NO_UUID;
384 r = device_check_and_adjust(cd, cd->device, &size, &offset, &read_only);
388 if (asprintf(&cipher, "%s-%s", crypt_get_cipher(cd),
389 crypt_get_cipher_mode(cd)) < 0)
392 r = dm_create_device(name, cd->device, cipher, cd->type,
393 no_uuid ? NULL : crypt_get_uuid(cd),
394 size, 0, offset, vk->keylength, vk->key,
400 static void log_wrapper(int level, const char *msg, void *usrptr)
402 void (*xlog)(int level, char *msg) = usrptr;
403 xlog(level, (char *)msg);
406 static int yesDialog_wrapper(const char *msg, void *usrptr)
408 int (*xyesDialog)(char *msg) = usrptr;
409 return xyesDialog((char*)msg);
412 int crypt_confirm(struct crypt_device *cd, const char *msg)
414 if (!cd || !cd->confirm)
417 return cd->confirm(msg, cd->confirm_usrptr);
420 static void key_from_terminal(struct crypt_device *cd, char *msg, char **key,
421 unsigned int *key_len, int force_verify)
426 *key = crypt_safe_alloc(MAX_TTY_PASSWORD_LEN);
429 r = cd->password(msg, *key, (size_t)key_len, cd->password_usrptr);
431 crypt_safe_free(*key);
436 crypt_get_key(msg, key, key_len, 0, NULL, cd->timeout,
437 (force_verify || cd->password_verify), cd);
440 static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslot,
441 struct volume_key **vk)
443 char *prompt = NULL, *passphrase_read = NULL;
444 unsigned int passphrase_size_read;
445 int r = -EINVAL, tries = cd->tries;
447 if(asprintf(&prompt, _("Enter passphrase for %s: "), cd->device) < 0)
453 crypt_free_volume_key(*vk);
456 key_from_terminal(cd, prompt, &passphrase_read,
457 &passphrase_size_read, 0);
458 if(!passphrase_read) {
463 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
464 passphrase_size_read, &cd->hdr, vk, cd);
465 crypt_safe_free(passphrase_read);
466 passphrase_read = NULL;
467 } while (r == -EPERM && (--tries > 0));
470 crypt_free_volume_key(*vk);
479 static void key_from_file(struct crypt_device *cd, char *msg,
480 char **key, unsigned int *key_len,
481 const char *key_file, size_t key_size)
483 crypt_get_key(msg, key, key_len, key_size, key_file, cd->timeout, 0, cd);
486 static int _crypt_init(struct crypt_device **cd,
488 struct crypt_options *options,
489 int load, int need_dm)
493 /* if it is plain device and mapping table is being reloaded
494 initialize it by name*/
495 init_by_name = (type && !strcmp(type, CRYPT_PLAIN) && load);
497 /* Some of old API calls do not require DM in kernel,
498 fake initialisation by initialise it with kernel_check disabled */
500 (void)dm_init(NULL, 0);
502 r = crypt_init_by_name(cd, options->name);
504 r = crypt_init(cd, options->device);
511 crypt_set_log_callback(*cd, log_wrapper, options->icb->log);
512 crypt_set_confirm_callback(*cd, yesDialog_wrapper, options->icb->yesDialog);
514 crypt_set_timeout(*cd, options->timeout);
515 crypt_set_password_retry(*cd, options->tries);
516 crypt_set_iterarion_time(*cd, options->iteration_time ?: 1000);
517 crypt_set_password_verify(*cd, options->flags & CRYPT_FLAG_VERIFY);
519 if (load && !init_by_name)
520 r = crypt_load(*cd, type, NULL);
522 if (!r && type && !(*cd)->type) {
523 (*cd)->type = strdup(type);
534 void crypt_set_log_callback(struct crypt_device *cd,
535 void (*log)(int level, const char *msg, void *usrptr),
542 cd->log_usrptr = usrptr;
546 void crypt_set_confirm_callback(struct crypt_device *cd,
547 int (*confirm)(const char *msg, void *usrptr),
550 cd->confirm = confirm;
551 cd->confirm_usrptr = usrptr;
554 void crypt_set_password_callback(struct crypt_device *cd,
555 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
558 cd->password = password;
559 cd->password_usrptr = usrptr;
562 /* OPTIONS: name, cipher, device, hash, key_file, key_size, key_slot,
563 * offset, size, skip, timeout, tries, passphrase_fd (ignored),
565 static int crypt_create_and_update_device(struct crypt_options *options, int update)
567 struct crypt_device *cd = NULL;
572 r = _crypt_init(&cd, CRYPT_PLAIN, options, 0, 1);
576 crypt_get_key(_("Enter passphrase: "), &key, &keyLen, options->key_size,
577 options->key_file, cd->timeout, cd->password_verify, cd);
581 r = create_device_helper(cd, options->name, options->hash,
582 options->cipher, NULL, options->key_file, key, keyLen,
583 options->key_size, options->size, options->skip,
584 options->offset, NULL, options->flags & CRYPT_FLAG_READONLY,
585 options->flags, update);
587 crypt_safe_free(key);
592 int crypt_create_device(struct crypt_options *options)
594 return crypt_create_and_update_device(options, 0);
597 int crypt_update_device(struct crypt_options *options)
599 return crypt_create_and_update_device(options, 1);
602 /* OPTIONS: name, size, icb */
603 int crypt_resize_device(struct crypt_options *options)
605 struct crypt_device *cd = NULL;
606 char *device = NULL, *cipher = NULL, *uuid = NULL, *key = NULL;
608 uint64_t size, skip, offset;
609 int key_size, read_only, r;
611 log_dbg("Resizing device %s to %" PRIu64 " sectors.", options->name, options->size);
613 if (dm_init(NULL, 1) < 0)
616 r = dm_query_device(options->name, &device, &size, &skip, &offset,
617 &cipher, &key_size, &key, &read_only, NULL, &uuid);
619 log_err(NULL, _("Device %s is not active.\n"), options->name);
623 /* Try to determine type of device from UUID */
626 if (!strncmp(uuid, CRYPT_PLAIN, strlen(CRYPT_PLAIN))) {
630 } else if (!strncmp(uuid, CRYPT_LUKS1, strlen(CRYPT_LUKS1)))
634 if (!options->device)
635 options->device = device;
637 r = _crypt_init(&cd, type, options, 1, 1);
641 size = options->size;
642 r = device_check_and_adjust(cd, device, &size, &offset, &read_only);
646 r = dm_create_device(options->name, device, cipher, type,
647 crypt_get_uuid(cd), size, skip, offset,
648 key_size, key, read_only, 1);
650 crypt_safe_free(key);
652 if (options->device == device)
653 options->device = NULL;
661 /* OPTIONS: name, icb */
662 int crypt_query_device(struct crypt_options *options)
666 log_dbg("Query device %s.", options->name);
668 if (dm_init(NULL, 1) < 0)
671 r = dm_status_device(options->name);
675 r = dm_query_device(options->name, (char **)&options->device, &options->size,
676 &options->skip, &options->offset, (char **)&options->cipher,
677 &options->key_size, NULL, &read_only, NULL, NULL);
680 options->flags |= CRYPT_FLAG_READONLY;
682 options->flags |= CRYPT_FLAG_FREE_DEVICE;
683 options->flags |= CRYPT_FLAG_FREE_CIPHER;
695 /* OPTIONS: name, icb */
696 int crypt_remove_device(struct crypt_options *options)
698 struct crypt_device *cd = NULL;
701 r = crypt_init_by_name(&cd, options->name);
703 r = crypt_deactivate(cd, options->name);
710 /* OPTIONS: device, cipher, hash, align_payload, key_size (master key), key_slot
711 * new_key_file, iteration_time, timeout, flags, icb */
712 int crypt_luksFormat(struct crypt_options *options)
714 char cipherName[LUKS_CIPHERNAME_L];
715 char cipherMode[LUKS_CIPHERMODE_L];
717 unsigned int passwordLen;
718 struct crypt_device *cd = NULL;
719 struct crypt_params_luks1 cp = {
720 .hash = options->hash,
721 .data_alignment = options->align_payload
725 r = crypt_parse_name_and_mode(options->cipher, cipherName, cipherMode);
727 log_err(cd, _("No known cipher specification pattern detected.\n"));
731 if ((r = _crypt_init(&cd, CRYPT_LUKS1, options, 0, 1)))
734 if (options->key_slot >= LUKS_NUMKEYS && options->key_slot != CRYPT_ANY_SLOT) {
735 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
736 options->key_slot, LUKS_NUMKEYS - 1);
741 crypt_get_key(_("Enter LUKS passphrase: "), &password, &passwordLen, 0,
742 options->new_key_file, cd->timeout, cd->password_verify, cd);
749 r = crypt_format(cd, CRYPT_LUKS1, cipherName, cipherMode,
750 NULL, NULL, options->key_size, &cp);
754 /* Add keyslot using internally stored volume key generated during format */
755 r = crypt_keyslot_add_by_volume_key(cd, options->key_slot, NULL, 0,
756 password, passwordLen);
759 crypt_safe_free(password);
760 return (r < 0) ? r : 0;
763 /* OPTIONS: name, device, key_size, key_file, timeout, tries, flags, icb */
764 int crypt_luksOpen(struct crypt_options *options)
766 struct crypt_device *cd = NULL;
773 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
777 if (options->flags & CRYPT_FLAG_READONLY)
778 flags |= CRYPT_ACTIVATE_READONLY;
780 if (options->flags & CRYPT_FLAG_NON_EXCLUSIVE_ACCESS)
781 flags |= CRYPT_ACTIVATE_NO_UUID;
783 if (options->key_file)
784 r = crypt_activate_by_keyfile(cd, options->name,
785 CRYPT_ANY_SLOT, options->key_file, options->key_size,
788 r = crypt_activate_by_passphrase(cd, options->name,
789 CRYPT_ANY_SLOT, options->passphrase,
790 options->passphrase ? strlen(options->passphrase) : 0,
794 return (r < 0) ? r : 0;
797 /* OPTIONS: device, keys_slot, key_file, timeout, flags, icb */
798 int crypt_luksKillSlot(struct crypt_options *options)
800 struct crypt_device *cd = NULL;
803 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
807 r = luks_remove_helper(cd, options->key_slot, options->key_file, NULL,
808 options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
811 return (r < 0) ? r : 0;
814 /* OPTIONS: device, new_key_file, key_file, timeout, flags, icb */
815 int crypt_luksRemoveKey(struct crypt_options *options)
817 struct crypt_device *cd = NULL;
820 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
824 r = luks_remove_helper(cd, CRYPT_ANY_SLOT, options->key_file, options->new_key_file,
825 options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
828 return (r < 0) ? r : 0;
832 /* OPTIONS: device, new_key_file, key_file, key_slot, flags,
833 iteration_time, timeout, icb */
834 int crypt_luksAddKey(struct crypt_options *options)
836 struct crypt_device *cd = NULL;
839 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
843 if (options->key_file || options->new_key_file)
844 r = crypt_keyslot_add_by_keyfile(cd, options->key_slot,
845 options->key_file, 0,
846 options->new_key_file, 0);
848 r = crypt_keyslot_add_by_passphrase(cd, options->key_slot,
852 return (r < 0) ? r : 0;
855 /* OPTIONS: device, icb */
856 int crypt_luksUUID(struct crypt_options *options)
858 struct crypt_device *cd = NULL;
862 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
866 uuid = (char *)crypt_get_uuid(cd);
867 log_std(cd, uuid ?: "");
873 /* OPTIONS: device, icb */
874 int crypt_isLuks(struct crypt_options *options)
876 struct crypt_device *cd = NULL;
879 log_dbg("Check device %s for LUKS header.", options->device);
885 r = crypt_init(&cd, options->device);
889 /* Do print fail here, no need to crypt_load() */
890 r = LUKS_read_phdr(cd->device, &cd->hdr, 0, cd) ? -EINVAL : 0;
896 /* OPTIONS: device, icb */
897 int crypt_luksDump(struct crypt_options *options)
899 struct crypt_device *cd = NULL;
902 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
912 void crypt_get_error(char *buf, size_t size)
914 const char *error = get_error();
916 if (!buf || size < 1)
919 strncpy(buf, error, size - 1);
920 buf[size - 1] = '\0';
926 void crypt_put_options(struct crypt_options *options)
928 if (options->flags & CRYPT_FLAG_FREE_DEVICE) {
929 free((char *)options->device);
930 options->device = NULL;
931 options->flags &= ~CRYPT_FLAG_FREE_DEVICE;
933 if (options->flags & CRYPT_FLAG_FREE_CIPHER) {
934 free((char *)options->cipher);
935 options->cipher = NULL;
936 options->flags &= ~CRYPT_FLAG_FREE_CIPHER;
940 const char *crypt_get_dir(void)
945 /////////////////////////////////
950 int crypt_init(struct crypt_device **cd, const char *device)
952 struct crypt_device *h = NULL;
957 log_dbg("Allocating crypt device %s context.", device);
959 if (device && !device_ready(NULL, device, O_RDONLY))
962 if (!(h = malloc(sizeof(struct crypt_device))))
965 memset(h, 0, sizeof(*h));
968 h->device = strdup(device);
976 if (dm_init(h, 1) < 0) {
981 h->iteration_time = 1000;
982 h->password_verify = 0;
988 int crypt_init_by_name(struct crypt_device **cd, const char *name)
990 crypt_status_info ci;
994 log_dbg("Allocating crypt device context by device %s.", name);
996 ci = crypt_status(NULL, name);
997 if (ci == CRYPT_INVALID)
1000 if (ci < CRYPT_ACTIVE) {
1001 log_err(NULL, _("Device %s is not active.\n"), name);
1005 r = dm_query_device(name, &device, NULL, NULL, NULL,
1006 NULL, NULL, NULL, NULL, NULL, NULL);
1008 /* Underlying device disappeared but mapping still active */
1009 if (r >= 0 && !device)
1010 log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
1014 r = crypt_init(cd, device);
1020 static int _crypt_format_plain(struct crypt_device *cd,
1022 const char *cipher_mode,
1024 size_t volume_key_size,
1025 struct crypt_params_plain *params)
1027 if (!cipher || !cipher_mode) {
1028 log_err(cd, _("Invalid plain crypt parameters.\n"));
1032 if (volume_key_size > 1024) {
1033 log_err(cd, _("Invalid key size.\n"));
1037 cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
1038 if (!cd->volume_key)
1041 cd->plain_cipher = strdup(cipher);
1042 cd->plain_cipher_mode = strdup(cipher_mode);
1045 cd->plain_uuid = strdup(uuid);
1047 if (params && params->hash)
1048 cd->plain_hdr.hash = strdup(params->hash);
1050 cd->plain_hdr.offset = params ? params->offset : 0;
1051 cd->plain_hdr.skip = params ? params->skip : 0;
1053 if (!cd->plain_cipher || !cd->plain_cipher_mode)
1059 static int _crypt_format_luks1(struct crypt_device *cd,
1061 const char *cipher_mode,
1063 const char *volume_key,
1064 size_t volume_key_size,
1065 struct crypt_params_luks1 *params)
1068 unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
1069 unsigned long alignment_offset = 0;
1072 log_err(cd, _("Can't format LUKS without device.\n"));
1077 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
1080 cd->volume_key = crypt_generate_volume_key(volume_key_size);
1085 if (params && params->data_alignment)
1086 required_alignment = params->data_alignment * SECTOR_SIZE;
1088 get_topology_alignment(cd->device, &required_alignment,
1089 &alignment_offset, DEFAULT_DISK_ALIGNMENT);
1091 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
1092 (params && params->hash) ? params->hash : "sha1",
1094 required_alignment / SECTOR_SIZE,
1095 alignment_offset / SECTOR_SIZE,
1096 cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1100 /* Wipe first 8 sectors - fs magic numbers etc. */
1101 r = wipe_device_header(cd->device, 8);
1103 log_err(cd, _("Can't wipe header on device %s.\n"), cd->device);
1107 r = LUKS_write_phdr(cd->device, &cd->hdr, cd);
1112 int crypt_format(struct crypt_device *cd,
1115 const char *cipher_mode,
1117 const char *volume_key,
1118 size_t volume_key_size,
1123 log_dbg("Formatting device %s as type %s.", cd->device ?: "(none)", cd->type ?: "(none)");
1128 r = init_crypto(cd);
1133 r = _crypt_format_plain(cd, cipher, cipher_mode,
1134 uuid, volume_key_size, params);
1135 else if (isLUKS(type))
1136 r = _crypt_format_luks1(cd, cipher, cipher_mode,
1137 uuid, volume_key, volume_key_size, params);
1139 /* FIXME: allow plugins here? */
1140 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
1144 if (!r && !(cd->type = strdup(type)))
1148 crypt_free_volume_key(cd->volume_key);
1149 cd->volume_key = NULL;
1155 int crypt_load(struct crypt_device *cd,
1156 const char *requested_type,
1159 struct luks_phdr hdr;
1162 log_dbg("Trying to load %s crypt type from device %s.",
1163 requested_type ?: "any", cd->device ?: "(none)");
1168 if (requested_type && !isLUKS(requested_type))
1171 r = init_crypto(cd);
1175 r = LUKS_read_phdr(cd->device, &hdr, 1, cd);
1178 memcpy(&cd->hdr, &hdr, sizeof(hdr));
1179 cd->type = strdup(requested_type);
1187 int crypt_header_backup(struct crypt_device *cd,
1188 const char *requested_type,
1189 const char *backup_file)
1193 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1196 r = init_crypto(cd);
1200 log_dbg("Requested header backup of device %s (%s) to "
1201 "file %s.", cd->device, requested_type, backup_file);
1203 return LUKS_hdr_backup(backup_file, cd->device, &cd->hdr, cd);
1206 int crypt_header_restore(struct crypt_device *cd,
1207 const char *requested_type,
1208 const char *backup_file)
1212 if (requested_type && !isLUKS(requested_type))
1215 /* Some hash functions need initialized gcrypt library */
1216 r = init_crypto(cd);
1220 log_dbg("Requested header restore to device %s (%s) from "
1221 "file %s.", cd->device, requested_type, backup_file);
1223 return LUKS_hdr_restore(backup_file, cd->device, &cd->hdr, cd);
1226 void crypt_free(struct crypt_device *cd)
1229 log_dbg("Releasing crypt device %s context.", cd->device);
1233 crypt_free_volume_key(cd->volume_key);
1238 /* used in plain device only */
1239 free((char*)cd->plain_hdr.hash);
1240 free(cd->plain_cipher);
1241 free(cd->plain_cipher_mode);
1242 free(cd->plain_uuid);
1248 int crypt_suspend(struct crypt_device *cd,
1251 crypt_status_info ci;
1252 int r, suspended = 0;
1254 log_dbg("Suspending volume %s.", name);
1256 ci = crypt_status(NULL, name);
1257 if (ci < CRYPT_ACTIVE) {
1258 log_err(cd, _("Volume %s is not active.\n"), name);
1262 if (!cd && dm_init(NULL, 1) < 0)
1265 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1266 NULL, NULL, NULL, NULL, &suspended, NULL);
1271 log_err(cd, _("Volume %s is already suspended.\n"), name);
1276 r = dm_suspend_and_wipe_key(name);
1278 log_err(cd, "Suspend is not supported for device %s.\n", name);
1280 log_err(cd, "Error during suspending device %s.\n", name);
1287 int crypt_resume_by_passphrase(struct crypt_device *cd,
1290 const char *passphrase,
1291 size_t passphrase_size)
1293 struct volume_key *vk = NULL;
1294 int r, suspended = 0;
1296 log_dbg("Resuming volume %s.", name);
1298 if (!isLUKS(cd->type)) {
1299 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1304 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1305 NULL, NULL, NULL, NULL, &suspended, NULL);
1310 log_err(cd, _("Volume %s is not suspended.\n"), name);
1315 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1316 passphrase_size, &cd->hdr, &vk, cd);
1318 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1322 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1324 log_err(cd, "Resume is not supported for device %s.\n", name);
1326 log_err(cd, "Error during resuming device %s.\n", name);
1330 crypt_free_volume_key(vk);
1331 return r < 0 ? r : keyslot;
1334 int crypt_resume_by_keyfile(struct crypt_device *cd,
1337 const char *keyfile,
1338 size_t keyfile_size)
1340 struct volume_key *vk = NULL;
1341 char *passphrase_read = NULL;
1342 unsigned int passphrase_size_read;
1343 int r, suspended = 0;
1345 log_dbg("Resuming volume %s.", name);
1347 if (!isLUKS(cd->type)) {
1348 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1353 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1354 NULL, NULL, NULL, NULL, &suspended, NULL);
1359 log_err(cd, _("Volume %s is not suspended.\n"), name);
1366 key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1367 &passphrase_size_read, keyfile, keyfile_size);
1369 if(!passphrase_read)
1372 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1373 passphrase_size_read, &cd->hdr, &vk, cd);
1374 crypt_safe_free(passphrase_read);
1379 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1381 log_err(cd, "Error during resuming device %s.\n", name);
1385 crypt_free_volume_key(vk);
1386 return r < 0 ? r : keyslot;
1389 // slot manipulation
1390 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1391 int keyslot, // -1 any
1392 const char *passphrase, // NULL -> terminal
1393 size_t passphrase_size,
1394 const char *new_passphrase, // NULL -> terminal
1395 size_t new_passphrase_size)
1397 struct volume_key *vk = NULL;
1398 char *password = NULL, *new_password = NULL;
1399 unsigned int passwordLen, new_passwordLen;
1402 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1403 "new passphrase %sprovided.",
1404 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1406 if (!isLUKS(cd->type)) {
1407 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1411 r = keyslot_verify_or_find_empty(cd, &keyslot);
1415 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1416 /* No slots used, try to use pre-generated key in header */
1417 if (cd->volume_key) {
1418 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1419 r = vk ? 0 : -ENOMEM;
1421 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1424 } else if (passphrase) {
1425 /* Passphrase provided, use it to unlock existing keyslot */
1426 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, passphrase,
1427 passphrase_size, &cd->hdr, &vk, cd);
1429 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1430 key_from_terminal(cd, _("Enter any passphrase: "),
1431 &password, &passwordLen, 0);
1437 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
1438 passwordLen, &cd->hdr, &vk, cd);
1439 crypt_safe_free(password);
1445 if (new_passphrase) {
1446 new_password = (char *)new_passphrase;
1447 new_passwordLen = new_passphrase_size;
1449 key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1450 &new_password, &new_passwordLen, 1);
1457 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1458 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1463 if (!new_passphrase)
1464 crypt_safe_free(new_password);
1465 crypt_free_volume_key(vk);
1466 return r ?: keyslot;
1469 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1471 const char *keyfile,
1472 size_t keyfile_size,
1473 const char *new_keyfile,
1474 size_t new_keyfile_size)
1476 struct volume_key *vk=NULL;
1477 char *password=NULL; unsigned int passwordLen;
1478 char *new_password = NULL; unsigned int new_passwordLen;
1481 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1482 keyfile ?: "[none]", new_keyfile ?: "[none]");
1484 if (!isLUKS(cd->type)) {
1485 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1489 r = keyslot_verify_or_find_empty(cd, &keyslot);
1493 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1494 /* No slots used, try to use pre-generated key in header */
1495 if (cd->volume_key) {
1496 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1497 r = vk ? 0 : -ENOMEM;
1499 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1503 /* Read password from file of (if NULL) from terminal */
1505 key_from_file(cd, _("Enter any passphrase: "), &password, &passwordLen,
1506 keyfile, keyfile_size);
1508 key_from_terminal(cd, _("Enter any passphrase: "),
1509 &password, &passwordLen, 0);
1514 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password, passwordLen,
1516 crypt_safe_free(password);
1523 key_from_file(cd, _("Enter new passphrase for key slot: "),
1524 &new_password, &new_passwordLen, new_keyfile,
1527 key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1528 &new_password, &new_passwordLen, 1);
1535 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1536 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1538 crypt_safe_free(new_password);
1539 crypt_free_volume_key(vk);
1540 return r < 0 ? r : keyslot;
1543 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1545 const char *volume_key,
1546 size_t volume_key_size,
1547 const char *passphrase,
1548 size_t passphrase_size)
1550 struct volume_key *vk = NULL;
1552 char *new_password = NULL; unsigned int new_passwordLen;
1554 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1556 if (!isLUKS(cd->type)) {
1557 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1562 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1563 else if (cd->volume_key)
1564 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1569 r = LUKS_verify_volume_key(&cd->hdr, vk);
1571 log_err(cd, _("Volume key does not match the volume.\n"));
1575 r = keyslot_verify_or_find_empty(cd, &keyslot);
1580 key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1581 &new_password, &new_passwordLen, 1);
1582 passphrase = new_password;
1583 passphrase_size = new_passwordLen;
1586 r = LUKS_set_key(cd->device, keyslot, passphrase, passphrase_size,
1587 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1590 crypt_safe_free(new_password);
1591 crypt_free_volume_key(vk);
1592 return r ?: keyslot;
1595 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1597 crypt_keyslot_info ki;
1599 log_dbg("Destroying keyslot %d.", keyslot);
1601 if (!isLUKS(cd->type)) {
1602 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1606 ki = crypt_keyslot_status(cd, keyslot);
1607 if (ki == CRYPT_SLOT_INVALID) {
1608 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1612 if (ki == CRYPT_SLOT_INACTIVE) {
1613 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1617 return LUKS_del_key(cd->device, keyslot, &cd->hdr, cd);
1620 // activation/deactivation of device mapping
1621 int crypt_activate_by_passphrase(struct crypt_device *cd,
1624 const char *passphrase,
1625 size_t passphrase_size,
1628 crypt_status_info ci;
1629 struct volume_key *vk = NULL;
1630 char *prompt = NULL;
1633 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1634 name ? "Activating" : "Checking", name ?: "",
1635 keyslot, passphrase ? "" : "[none] ");
1637 /* plain, use hashed passphrase */
1638 if (isPLAIN(cd->type))
1639 return create_device_helper(cd, name, cd->plain_hdr.hash,
1640 cd->plain_cipher, cd->plain_cipher_mode, NULL, passphrase, passphrase_size,
1641 cd->volume_key->keylength, 0, cd->plain_hdr.skip,
1642 cd->plain_hdr.offset, cd->plain_uuid, flags & CRYPT_ACTIVATE_READONLY, 0, 0);
1645 ci = crypt_status(NULL, name);
1646 if (ci == CRYPT_INVALID)
1648 else if (ci >= CRYPT_ACTIVE) {
1649 log_err(cd, _("Device %s already exists.\n"), name);
1654 if(asprintf(&prompt, _("Enter passphrase for %s: "), cd->device) < 0)
1657 /* provided passphrase, do not retry */
1659 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1660 passphrase_size, &cd->hdr, &vk, cd);
1662 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1667 r = open_from_hdr_and_vk(cd, vk, name, flags);
1670 crypt_free_volume_key(vk);
1673 return r < 0 ? r : keyslot;
1676 int crypt_activate_by_keyfile(struct crypt_device *cd,
1679 const char *keyfile,
1680 size_t keyfile_size,
1683 crypt_status_info ci;
1684 struct volume_key *vk = NULL;
1685 char *passphrase_read = NULL;
1686 unsigned int passphrase_size_read;
1689 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1690 name ?: "", keyslot, keyfile ?: "[none]");
1692 if (!isLUKS(cd->type)) {
1693 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1698 ci = crypt_status(NULL, name);
1699 if (ci == CRYPT_INVALID)
1701 else if (ci >= CRYPT_ACTIVE) {
1702 log_err(cd, _("Device %s already exists.\n"), name);
1710 key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1711 &passphrase_size_read, keyfile, keyfile_size);
1712 if(!passphrase_read)
1715 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1716 passphrase_size_read, &cd->hdr, &vk, cd);
1717 crypt_safe_free(passphrase_read);
1723 r = open_from_hdr_and_vk(cd, vk, name, flags);
1726 crypt_free_volume_key(vk);
1728 return r < 0 ? r : keyslot;
1731 int crypt_activate_by_volume_key(struct crypt_device *cd,
1733 const char *volume_key,
1734 size_t volume_key_size,
1737 crypt_status_info ci;
1738 struct volume_key *vk;
1741 log_dbg("Activating volume %s by volume key.", name);
1743 /* use key directly, no hash */
1744 if (isPLAIN(cd->type))
1745 return create_device_helper(cd, name, NULL,
1746 cd->plain_cipher, cd->plain_cipher_mode, NULL, volume_key, volume_key_size,
1747 cd->volume_key->keylength, 0, cd->plain_hdr.skip,
1748 cd->plain_hdr.offset, cd->plain_uuid, flags & CRYPT_ACTIVATE_READONLY, 0, 0);
1750 if (!isLUKS(cd->type)) {
1751 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1756 ci = crypt_status(NULL, name);
1757 if (ci == CRYPT_INVALID)
1759 else if (ci >= CRYPT_ACTIVE) {
1760 log_err(cd, _("Device %s already exists.\n"), name);
1765 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1768 r = LUKS_verify_volume_key(&cd->hdr, vk);
1771 log_err(cd, _("Volume key does not match the volume.\n"));
1774 r = open_from_hdr_and_vk(cd, vk, name, flags);
1776 crypt_free_volume_key(vk);
1781 int crypt_deactivate(struct crypt_device *cd, const char *name)
1788 log_dbg("Deactivating volume %s.", name);
1790 if (!cd && dm_init(NULL, 1) < 0)
1793 switch (crypt_status(cd, name)) {
1795 r = dm_remove_device(name, 0, 0);
1798 log_err(cd, _("Device %s is busy.\n"), name);
1801 case CRYPT_INACTIVE:
1802 log_err(cd, _("Device %s is not active.\n"), name);
1806 log_err(cd, _("Invalid device %s.\n"), name);
1816 // misc helper functions
1817 int crypt_volume_key_get(struct crypt_device *cd,
1820 size_t *volume_key_size,
1821 const char *passphrase,
1822 size_t passphrase_size)
1824 struct volume_key *vk;
1825 char *processed_key = NULL;
1828 key_len = crypt_get_volume_key_size(cd);
1829 if (key_len > *volume_key_size) {
1830 log_err(cd, _("Volume key buffer too small.\n"));
1834 if (isPLAIN(cd->type) && cd->plain_hdr.hash) {
1835 processed_key = process_key(cd, cd->plain_hdr.hash, NULL, key_len,
1836 passphrase, passphrase_size);
1837 if (!processed_key) {
1838 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
1841 memcpy(volume_key, processed_key, key_len);
1842 *volume_key_size = key_len;
1843 crypt_safe_free(processed_key);
1847 if (isLUKS(cd->type)) {
1848 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1849 passphrase_size, &cd->hdr, &vk, cd);
1852 memcpy(volume_key, vk->key, vk->keylength);
1853 *volume_key_size = vk->keylength;
1856 crypt_free_volume_key(vk);
1860 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
1864 int crypt_volume_key_verify(struct crypt_device *cd,
1865 const char *volume_key,
1866 size_t volume_key_size)
1868 struct volume_key *vk;
1871 if (!isLUKS(cd->type)) {
1872 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1876 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1880 r = LUKS_verify_volume_key(&cd->hdr, vk);
1883 log_err(cd, _("Volume key does not match the volume.\n"));
1885 crypt_free_volume_key(vk);
1890 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
1892 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
1893 cd->timeout = timeout_sec;
1896 void crypt_set_password_retry(struct crypt_device *cd, int tries)
1898 log_dbg("Password retry count set to %d.", tries);
1902 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
1904 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
1905 cd->iteration_time = iteration_time_ms;
1908 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
1910 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
1911 cd->password_verify = password_verify ? 1 : 0;
1914 int crypt_memory_lock(struct crypt_device *cd, int lock)
1916 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
1920 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
1924 if (!cd && dm_init(NULL, 1) < 0)
1925 return CRYPT_INVALID;
1927 r = dm_status_device(name);
1932 if (r < 0 && r != -ENODEV)
1933 return CRYPT_INVALID;
1936 return CRYPT_ACTIVE;
1941 return CRYPT_INACTIVE;
1944 static void hexprintICB(struct crypt_device *cd, char *d, int n)
1947 for(i = 0; i < n; i++)
1948 log_std(cd, "%02hhx ", (char)d[i]);
1951 int crypt_dump(struct crypt_device *cd)
1954 if (!isLUKS(cd->type)) { //FIXME
1955 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1959 log_std(cd, "LUKS header information for %s\n\n", cd->device);
1960 log_std(cd, "Version: \t%d\n", cd->hdr.version);
1961 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
1962 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
1963 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec);
1964 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
1965 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
1966 log_std(cd, "MK digest: \t");
1967 hexprintICB(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE);
1969 log_std(cd, "MK salt: \t");
1970 hexprintICB(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2);
1971 log_std(cd, "\n \t");
1972 hexprintICB(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
1974 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
1975 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
1976 for(i = 0; i < LUKS_NUMKEYS; i++) {
1977 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
1978 log_std(cd, "Key Slot %d: ENABLED\n",i);
1979 log_std(cd, "\tIterations: \t%d\n",
1980 cd->hdr.keyblock[i].passwordIterations);
1981 log_std(cd, "\tSalt: \t");
1982 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt,
1984 log_std(cd, "\n\t \t");
1985 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt +
1986 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
1989 log_std(cd, "\tKey material offset:\t%d\n",
1990 cd->hdr.keyblock[i].keyMaterialOffset);
1991 log_std(cd, "\tAF stripes: \t%d\n",
1992 cd->hdr.keyblock[i].stripes);
1995 log_std(cd, "Key Slot %d: DISABLED\n", i);
1998 log_std(cd, "DNAME: %s\n", crypt_get_device_name(cd) ?: "");
2003 const char *crypt_get_cipher(struct crypt_device *cd)
2005 if (isPLAIN(cd->type))
2006 return cd->plain_cipher;
2008 if (isLUKS(cd->type))
2009 return cd->hdr.cipherName;
2014 const char *crypt_get_cipher_mode(struct crypt_device *cd)
2016 if (isPLAIN(cd->type))
2017 return cd->plain_cipher_mode;
2019 if (isLUKS(cd->type))
2020 return cd->hdr.cipherMode;
2025 const char *crypt_get_uuid(struct crypt_device *cd)
2027 if (isLUKS(cd->type))
2028 return cd->hdr.uuid;
2033 const char *crypt_get_device_name(struct crypt_device *cd)
2038 int crypt_get_volume_key_size(struct crypt_device *cd)
2040 if (isPLAIN(cd->type))
2041 return cd->volume_key->keylength;
2043 if (isLUKS(cd->type))
2044 return cd->hdr.keyBytes;
2049 uint64_t crypt_get_data_offset(struct crypt_device *cd)
2051 if (isPLAIN(cd->type))
2052 return cd->plain_hdr.offset;
2054 if (isLUKS(cd->type))
2055 return cd->hdr.payloadOffset;
2060 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
2062 if (!isLUKS(cd->type)) {
2063 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2064 return CRYPT_SLOT_INVALID;
2067 return LUKS_keyslot_info(&cd->hdr, keyslot);