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 = 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"),
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"),
120 } else if (passLen > key_size) {
121 memcpy(key, pass, key_size);
123 memcpy(key, pass, passLen);
129 int parse_into_name_and_mode(const char *nameAndMode, char *name, char *mode)
131 /* Token content stringification, see info cpp/stringification */
133 #define xstr(s) str(s)
134 #define scanpattern1 "%" xstr(LUKS_CIPHERNAME_L) "[^-]-%" xstr(LUKS_CIPHERMODE_L) "s"
135 #define scanpattern2 "%" xstr(LUKS_CIPHERNAME_L) "[^-]"
139 if(sscanf(nameAndMode,scanpattern1, name, mode) != 2) {
140 if((r = sscanf(nameAndMode,scanpattern2,name)) == 1)
141 strncpy(mode,"cbc-plain",10);
154 static int isPLAIN(const char *type)
156 return (type && !strcmp(CRYPT_PLAIN, type));
159 static int isLUKS(const char *type)
161 return (type && !strcmp(CRYPT_LUKS1, type));
164 /* keyslot helpers */
165 static int keyslot_verify_or_find_empty(struct crypt_device *cd, int *keyslot)
167 if (*keyslot == CRYPT_ANY_SLOT) {
168 *keyslot = LUKS_keyslot_find_empty(&cd->hdr);
170 log_err(cd, _("All key slots full.\n"));
175 switch (LUKS_keyslot_info(&cd->hdr, *keyslot)) {
176 case CRYPT_SLOT_INVALID:
177 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
178 *keyslot, LUKS_NUMKEYS - 1);
180 case CRYPT_SLOT_INACTIVE:
183 log_err(cd, _("Key slot %d is full, please select another one.\n"),
191 static int verify_other_keyslot(struct crypt_device *cd,
192 const char *key_file,
196 struct volume_key *vk;
197 crypt_keyslot_info ki;
199 char *password = NULL;
200 unsigned int passwordLen;
202 get_key(_("Enter any remaining LUKS passphrase: "), &password,
203 &passwordLen, 0, key_file, cd->timeout, flags, cd);
207 ki = crypt_keyslot_status(cd, keyIndex);
208 if (ki == CRYPT_SLOT_ACTIVE) /* Not last slot */
209 LUKS_keyslot_set(&cd->hdr, keyIndex, 0);
211 openedIndex = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT,
212 password, passwordLen,
215 if (ki == CRYPT_SLOT_ACTIVE)
216 LUKS_keyslot_set(&cd->hdr, keyIndex, 1);
217 crypt_free_volume_key(vk);
223 log_verbose(cd, _("Key slot %d verified.\n"), openedIndex);
227 static int find_keyslot_by_passphrase(struct crypt_device *cd,
228 const char *key_file,
232 struct volume_key *vk;
233 char *password = NULL;
234 unsigned int passwordLen;
237 get_key(message,&password,&passwordLen, 0, key_file,
238 cd->timeout, flags, cd);
242 keyIndex = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
243 passwordLen, &cd->hdr, &vk, cd);
244 crypt_free_volume_key(vk);
250 static int device_check_and_adjust(struct crypt_device *cd,
252 uint64_t *size, uint64_t *offset,
255 struct device_infos infos;
257 if (!device || get_device_infos(device, &infos, cd) < 0) {
258 log_err(cd, _("Cannot get info about device %s.\n"),
266 log_err(cd, _("Device %s has zero size.\n"), device);
269 if (*size < *offset) {
270 log_err(cd, _("Device %s is too small.\n"), device);
279 log_dbg("Calculated device size is %" PRIu64 " sectors (%s), offset %" PRIu64 ".",
280 *size, *read_only ? "RO" : "RW", *offset);
284 static int luks_remove_helper(struct crypt_device *cd,
286 const char *other_key_file,
287 const char *key_file,
290 crypt_keyslot_info ki;
293 if (key_slot == CRYPT_ANY_SLOT) {
294 key_slot = find_keyslot_by_passphrase(cd, key_file, 0,
295 _("Enter LUKS passphrase to be deleted: "));
301 log_std(cd, _("key slot %d selected for deletion.\n"), key_slot);
304 ki = crypt_keyslot_status(cd, key_slot);
305 if (ki == CRYPT_SLOT_INVALID) {
306 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
307 key_slot, LUKS_NUMKEYS - 1);
311 if (ki <= CRYPT_SLOT_INACTIVE) {
312 log_err(cd, _("Key %d not active. Can't wipe.\n"), key_slot);
317 if (ki == CRYPT_SLOT_ACTIVE_LAST && cd->confirm &&
318 !(cd->confirm(_("This is the last keyslot."
319 " Device will become unusable after purging this key."),
320 cd->confirm_usrptr))) {
326 r = verify_other_keyslot(cd, other_key_file, 0, key_slot);
331 r = crypt_keyslot_destroy(cd, key_slot);
333 return (r < 0) ? r : 0;
336 static int create_device_helper(struct crypt_device *cd,
340 const char *cipher_mode,
341 const char *key_file,
353 crypt_status_info ci;
354 char *dm_cipher = NULL;
355 char *processed_key = NULL;
361 ci = crypt_status(cd, name);
362 if (ci == CRYPT_INVALID)
365 if (reload && ci < CRYPT_ACTIVE)
368 if (!reload && ci >= CRYPT_ACTIVE) {
369 log_err(cd, _("Device %s already exists.\n"), name);
373 if (key_size < 0 || key_size > 1024) {
374 log_err(cd, _("Invalid key size %d.\n"), key_size);
378 r = device_check_and_adjust(cd, cd->device, &size, &offset, &read_only);
382 if (cipher_mode && asprintf(&dm_cipher, "%s-%s", cipher, cipher_mode) < 0)
385 processed_key = process_key(cd, hash, key_file, key_size, key, keyLen);
389 r = dm_create_device(name, cd->device, dm_cipher ?: cipher, cd->type, uuid, size, skip, offset,
390 key_size, processed_key, read_only, reload);
393 safe_free(processed_key);
397 static int open_from_hdr_and_vk(struct crypt_device *cd,
398 struct volume_key *vk,
402 uint64_t size, offset;
404 int read_only, no_uuid, r;
407 offset = crypt_get_data_offset(cd);
408 read_only = flags & CRYPT_ACTIVATE_READONLY;
409 no_uuid = flags & CRYPT_ACTIVATE_NO_UUID;
411 r = device_check_and_adjust(cd, cd->device, &size, &offset, &read_only);
415 if (asprintf(&cipher, "%s-%s", crypt_get_cipher(cd),
416 crypt_get_cipher_mode(cd)) < 0)
419 r = dm_create_device(name, cd->device, cipher, cd->type,
420 no_uuid ? NULL : crypt_get_uuid(cd),
421 size, 0, offset, vk->keylength, vk->key,
427 static void log_wrapper(int level, const char *msg, void *usrptr)
429 void (*xlog)(int level, char *msg) = usrptr;
430 xlog(level, (char *)msg);
433 static int yesDialog_wrapper(const char *msg, void *usrptr)
435 int (*xyesDialog)(char *msg) = usrptr;
436 return xyesDialog((char*)msg);
439 int crypt_confirm(struct crypt_device *cd, const char *msg)
441 if (!cd || !cd->confirm)
444 return cd->confirm(msg, cd->confirm_usrptr);
447 static void key_from_terminal(struct crypt_device *cd, char *msg, char **key,
448 unsigned int *key_len, int force_verify)
453 *key = safe_alloc(MAX_TTY_PASSWORD_LEN);
456 r = cd->password(msg, *key, (size_t)key_len, cd->password_usrptr);
463 if (force_verify || cd->password_verify)
464 flags |= CRYPT_FLAG_VERIFY_IF_POSSIBLE;
465 get_key(msg, key, key_len, 0, NULL, cd->timeout, flags, cd);
469 static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslot,
470 struct volume_key **vk)
472 char *prompt = NULL, *passphrase_read = NULL;
473 unsigned int passphrase_size_read;
474 int r = -EINVAL, tries = cd->tries;
476 if(asprintf(&prompt, _("Enter passphrase for %s: "), cd->device) < 0)
482 crypt_free_volume_key(*vk);
485 key_from_terminal(cd, prompt, &passphrase_read,
486 &passphrase_size_read, 0);
487 if(!passphrase_read) {
492 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
493 passphrase_size_read, &cd->hdr, vk, cd);
494 safe_free(passphrase_read);
495 passphrase_read = NULL;
496 } while (r == -EPERM && (--tries > 0));
499 crypt_free_volume_key(*vk);
508 static void key_from_file(struct crypt_device *cd, char *msg,
509 char **key, unsigned int *key_len,
510 const char *key_file, size_t key_size)
512 get_key(msg, key, key_len, key_size, key_file, cd->timeout, 0, cd);
515 static int _crypt_init(struct crypt_device **cd,
517 struct crypt_options *options,
518 int load, int need_dm)
522 /* if it is plain device and mapping table is being reloaded
523 initialize it by name*/
524 init_by_name = (type && !strcmp(type, CRYPT_PLAIN) && load);
526 /* Some of old API calls do not require DM in kernel,
527 fake initialisation by initialise it with kernel_check disabled */
529 (void)dm_init(NULL, 0);
531 r = crypt_init_by_name(cd, options->name);
533 r = crypt_init(cd, options->device);
540 crypt_set_log_callback(*cd, log_wrapper, options->icb->log);
541 crypt_set_confirm_callback(*cd, yesDialog_wrapper, options->icb->yesDialog);
543 crypt_set_timeout(*cd, options->timeout);
544 crypt_set_password_retry(*cd, options->tries);
545 crypt_set_iterarion_time(*cd, options->iteration_time ?: 1000);
546 crypt_set_password_verify(*cd, options->flags & CRYPT_FLAG_VERIFY);
548 if (load && !init_by_name)
549 r = crypt_load(*cd, type, NULL);
551 if (!r && type && !(*cd)->type) {
552 (*cd)->type = strdup(type);
563 void crypt_set_log_callback(struct crypt_device *cd,
564 void (*log)(int level, const char *msg, void *usrptr),
571 cd->log_usrptr = usrptr;
575 void crypt_set_confirm_callback(struct crypt_device *cd,
576 int (*confirm)(const char *msg, void *usrptr),
579 cd->confirm = confirm;
580 cd->confirm_usrptr = usrptr;
583 void crypt_set_password_callback(struct crypt_device *cd,
584 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
587 cd->password = password;
588 cd->password_usrptr = usrptr;
591 /* OPTIONS: name, cipher, device, hash, key_file, key_size, key_slot,
592 * offset, size, skip, timeout, tries, passphrase_fd (ignored),
594 static int crypt_create_and_update_device(struct crypt_options *options, int update)
596 struct crypt_device *cd = NULL;
601 r = _crypt_init(&cd, CRYPT_PLAIN, options, 0, 1);
605 get_key(_("Enter passphrase: "), &key, &keyLen, options->key_size,
606 options->key_file, cd->timeout, options->flags, cd);
610 r = create_device_helper(cd, options->name, options->hash,
611 options->cipher, NULL, options->key_file, key, keyLen,
612 options->key_size, options->size, options->skip,
613 options->offset, NULL, options->flags & CRYPT_FLAG_READONLY,
614 options->flags, update);
621 int crypt_create_device(struct crypt_options *options)
623 return crypt_create_and_update_device(options, 0);
626 int crypt_update_device(struct crypt_options *options)
628 return crypt_create_and_update_device(options, 1);
631 /* OPTIONS: name, size, icb */
632 int crypt_resize_device(struct crypt_options *options)
634 struct crypt_device *cd = NULL;
635 char *device = NULL, *cipher = NULL, *uuid = NULL, *key = NULL;
637 uint64_t size, skip, offset;
638 int key_size, read_only, r;
640 log_dbg("Resizing device %s to %" PRIu64 " sectors.", options->name, options->size);
642 if (dm_init(NULL, 1) < 0)
645 r = dm_query_device(options->name, &device, &size, &skip, &offset,
646 &cipher, &key_size, &key, &read_only, NULL, &uuid);
648 log_err(NULL, _("Device %s is not active.\n"), options->name);
652 /* Try to determine type of device from UUID */
655 if (!strncmp(uuid, CRYPT_PLAIN, strlen(CRYPT_PLAIN))) {
659 } else if (!strncmp(uuid, CRYPT_LUKS1, strlen(CRYPT_LUKS1)))
663 if (!options->device)
664 options->device = device;
666 r = _crypt_init(&cd, type, options, 1, 1);
670 size = options->size;
671 r = device_check_and_adjust(cd, device, &size, &offset, &read_only);
675 r = dm_create_device(options->name, device, cipher, type,
676 crypt_get_uuid(cd), size, skip, offset,
677 key_size, key, read_only, 1);
681 if (options->device == device)
682 options->device = NULL;
690 /* OPTIONS: name, icb */
691 int crypt_query_device(struct crypt_options *options)
695 log_dbg("Query device %s.", options->name);
697 if (dm_init(NULL, 1) < 0)
700 r = dm_status_device(options->name);
704 r = dm_query_device(options->name, (char **)&options->device, &options->size,
705 &options->skip, &options->offset, (char **)&options->cipher,
706 &options->key_size, NULL, &read_only, NULL, NULL);
709 options->flags |= CRYPT_FLAG_READONLY;
711 options->flags |= CRYPT_FLAG_FREE_DEVICE;
712 options->flags |= CRYPT_FLAG_FREE_CIPHER;
724 /* OPTIONS: name, icb */
725 int crypt_remove_device(struct crypt_options *options)
727 struct crypt_device *cd = NULL;
730 r = crypt_init_by_name(&cd, options->name);
732 r = crypt_deactivate(cd, options->name);
739 /* OPTIONS: device, cipher, hash, align_payload, key_size (master key), key_slot
740 * new_key_file, iteration_time, timeout, flags, icb */
741 int crypt_luksFormat(struct crypt_options *options)
743 char cipherName[LUKS_CIPHERNAME_L];
744 char cipherMode[LUKS_CIPHERMODE_L];
746 unsigned int passwordLen;
747 struct crypt_device *cd = NULL;
748 struct crypt_params_luks1 cp = {
749 .hash = options->hash,
750 .data_alignment = options->align_payload
754 r = parse_into_name_and_mode(options->cipher, cipherName, cipherMode);
756 log_err(cd, _("No known cipher specification pattern detected.\n"));
760 if ((r = _crypt_init(&cd, CRYPT_LUKS1, options, 0, 1)))
763 if (options->key_slot >= LUKS_NUMKEYS && options->key_slot != CRYPT_ANY_SLOT) {
764 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
765 options->key_slot, LUKS_NUMKEYS - 1);
770 get_key(_("Enter LUKS passphrase: "), &password, &passwordLen, 0,
771 options->new_key_file, options->timeout, options->flags, cd);
778 r = crypt_format(cd, CRYPT_LUKS1, cipherName, cipherMode,
779 NULL, NULL, options->key_size, &cp);
783 /* Add keyslot using internally stored volume key generated during format */
784 r = crypt_keyslot_add_by_volume_key(cd, options->key_slot, NULL, 0,
785 password, passwordLen);
789 return (r < 0) ? r : 0;
792 /* OPTIONS: name, device, key_size, key_file, timeout, tries, flags, icb */
793 int crypt_luksOpen(struct crypt_options *options)
795 struct crypt_device *cd = NULL;
802 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
806 if (options->flags & CRYPT_FLAG_READONLY)
807 flags |= CRYPT_ACTIVATE_READONLY;
809 if (options->flags & CRYPT_FLAG_NON_EXCLUSIVE_ACCESS)
810 flags |= CRYPT_ACTIVATE_NO_UUID;
812 if (options->key_file)
813 r = crypt_activate_by_keyfile(cd, options->name,
814 CRYPT_ANY_SLOT, options->key_file, options->key_size,
817 r = crypt_activate_by_passphrase(cd, options->name,
818 CRYPT_ANY_SLOT, options->passphrase,
819 options->passphrase ? strlen(options->passphrase) : 0,
823 return (r < 0) ? r : 0;
826 /* OPTIONS: device, keys_slot, key_file, timeout, flags, icb */
827 int crypt_luksKillSlot(struct crypt_options *options)
829 struct crypt_device *cd = NULL;
832 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
836 r = luks_remove_helper(cd, options->key_slot, options->key_file, NULL,
837 options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
840 return (r < 0) ? r : 0;
843 /* OPTIONS: device, new_key_file, key_file, timeout, flags, icb */
844 int crypt_luksRemoveKey(struct crypt_options *options)
846 struct crypt_device *cd = NULL;
849 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
853 r = luks_remove_helper(cd, CRYPT_ANY_SLOT, options->key_file, options->new_key_file,
854 options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
857 return (r < 0) ? r : 0;
861 /* OPTIONS: device, new_key_file, key_file, key_slot, flags,
862 iteration_time, timeout, icb */
863 int crypt_luksAddKey(struct crypt_options *options)
865 struct crypt_device *cd = NULL;
868 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
872 if (options->key_file || options->new_key_file)
873 r = crypt_keyslot_add_by_keyfile(cd, options->key_slot,
874 options->key_file, 0,
875 options->new_key_file, 0);
877 r = crypt_keyslot_add_by_passphrase(cd, options->key_slot,
881 return (r < 0) ? r : 0;
884 /* OPTIONS: device, icb */
885 int crypt_luksUUID(struct crypt_options *options)
887 struct crypt_device *cd = NULL;
891 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
895 uuid = (char *)crypt_get_uuid(cd);
896 log_std(cd, uuid ?: "");
902 /* OPTIONS: device, icb */
903 int crypt_isLuks(struct crypt_options *options)
905 struct crypt_device *cd = NULL;
908 log_dbg("Check device %s for LUKS header.", options->device);
914 r = crypt_init(&cd, options->device);
918 /* Do print fail here, no need to crypt_load() */
919 r = LUKS_read_phdr(cd->device, &cd->hdr, 0, cd) ? -EINVAL : 0;
925 /* OPTIONS: device, icb */
926 int crypt_luksDump(struct crypt_options *options)
928 struct crypt_device *cd = NULL;
931 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
941 void crypt_get_error(char *buf, size_t size)
943 const char *error = get_error();
945 if (!buf || size < 1)
948 strncpy(buf, error, size - 1);
949 buf[size - 1] = '\0';
955 void crypt_put_options(struct crypt_options *options)
957 if (options->flags & CRYPT_FLAG_FREE_DEVICE) {
958 free((char *)options->device);
959 options->device = NULL;
960 options->flags &= ~CRYPT_FLAG_FREE_DEVICE;
962 if (options->flags & CRYPT_FLAG_FREE_CIPHER) {
963 free((char *)options->cipher);
964 options->cipher = NULL;
965 options->flags &= ~CRYPT_FLAG_FREE_CIPHER;
969 const char *crypt_get_dir(void)
974 /////////////////////////////////
979 int crypt_init(struct crypt_device **cd, const char *device)
981 struct crypt_device *h = NULL;
986 log_dbg("Allocating crypt device %s context.", device);
988 if (device && !device_ready(NULL, device, O_RDONLY))
991 if (!(h = malloc(sizeof(struct crypt_device))))
994 memset(h, 0, sizeof(*h));
997 h->device = strdup(device);
1005 if (dm_init(h, 1) < 0) {
1010 h->iteration_time = 1000;
1011 h->password_verify = 0;
1017 int crypt_init_by_name(struct crypt_device **cd, const char *name)
1019 crypt_status_info ci;
1020 char *device = NULL;
1023 log_dbg("Allocating crypt device context by device %s.", name);
1025 ci = crypt_status(NULL, name);
1026 if (ci == CRYPT_INVALID)
1029 if (ci < CRYPT_ACTIVE) {
1030 log_err(NULL, _("Device %s is not active.\n"), name);
1034 r = dm_query_device(name, &device, NULL, NULL, NULL,
1035 NULL, NULL, NULL, NULL, NULL, NULL);
1037 /* Underlying device disappeared but mapping still active */
1038 if (r >= 0 && !device)
1039 log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
1043 r = crypt_init(cd, device);
1049 static int _crypt_format_plain(struct crypt_device *cd,
1051 const char *cipher_mode,
1053 size_t volume_key_size,
1054 struct crypt_params_plain *params)
1056 if (!cipher || !cipher_mode) {
1057 log_err(cd, _("Invalid plain crypt parameters.\n"));
1061 if (volume_key_size > 1024) {
1062 log_err(cd, _("Invalid key size.\n"));
1066 cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
1067 if (!cd->volume_key)
1070 cd->plain_cipher = strdup(cipher);
1071 cd->plain_cipher_mode = strdup(cipher_mode);
1074 cd->plain_uuid = strdup(uuid);
1076 if (params && params->hash)
1077 cd->plain_hdr.hash = strdup(params->hash);
1079 cd->plain_hdr.offset = params ? params->offset : 0;
1080 cd->plain_hdr.skip = params ? params->skip : 0;
1082 if (!cd->plain_cipher || !cd->plain_cipher_mode)
1088 static int _crypt_format_luks1(struct crypt_device *cd,
1090 const char *cipher_mode,
1092 const char *volume_key,
1093 size_t volume_key_size,
1094 struct crypt_params_luks1 *params)
1097 unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
1098 unsigned long alignment_offset = 0;
1101 log_err(cd, _("Can't format LUKS without device.\n"));
1106 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
1109 cd->volume_key = crypt_generate_volume_key(volume_key_size);
1114 if (params && params->data_alignment)
1115 required_alignment = params->data_alignment * SECTOR_SIZE;
1117 get_topology_alignment(cd->device, &required_alignment,
1118 &alignment_offset, DEFAULT_DISK_ALIGNMENT);
1120 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
1121 (params && params->hash) ? params->hash : "sha1",
1123 required_alignment / SECTOR_SIZE,
1124 alignment_offset / SECTOR_SIZE,
1125 cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1129 /* Wipe first 8 sectors - fs magic numbers etc. */
1130 r = wipe_device_header(cd->device, 8);
1132 log_err(cd, _("Can't wipe header on device %s.\n"), cd->device);
1136 r = LUKS_write_phdr(cd->device, &cd->hdr, cd);
1141 int crypt_format(struct crypt_device *cd,
1144 const char *cipher_mode,
1146 const char *volume_key,
1147 size_t volume_key_size,
1152 log_dbg("Formatting device %s as type %s.", cd->device ?: "(none)", cd->type ?: "(none)");
1157 r = init_crypto(cd);
1162 r = _crypt_format_plain(cd, cipher, cipher_mode,
1163 uuid, volume_key_size, params);
1164 else if (isLUKS(type))
1165 r = _crypt_format_luks1(cd, cipher, cipher_mode,
1166 uuid, volume_key, volume_key_size, params);
1168 /* FIXME: allow plugins here? */
1169 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
1173 if (!r && !(cd->type = strdup(type)))
1177 crypt_free_volume_key(cd->volume_key);
1178 cd->volume_key = NULL;
1184 int crypt_load(struct crypt_device *cd,
1185 const char *requested_type,
1188 struct luks_phdr hdr;
1191 log_dbg("Trying to load %s crypt type from device %s.",
1192 requested_type ?: "any", cd->device ?: "(none)");
1197 if (requested_type && !isLUKS(requested_type))
1200 r = init_crypto(cd);
1204 r = LUKS_read_phdr(cd->device, &hdr, 1, cd);
1207 memcpy(&cd->hdr, &hdr, sizeof(hdr));
1208 cd->type = strdup(requested_type);
1216 int crypt_header_backup(struct crypt_device *cd,
1217 const char *requested_type,
1218 const char *backup_file)
1222 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1225 r = init_crypto(cd);
1229 log_dbg("Requested header backup of device %s (%s) to "
1230 "file %s.", cd->device, requested_type, backup_file);
1232 return LUKS_hdr_backup(backup_file, cd->device, &cd->hdr, cd);
1235 int crypt_header_restore(struct crypt_device *cd,
1236 const char *requested_type,
1237 const char *backup_file)
1241 if (requested_type && !isLUKS(requested_type))
1244 /* Some hash functions need initialized gcrypt library */
1245 r = init_crypto(cd);
1249 log_dbg("Requested header restore to device %s (%s) from "
1250 "file %s.", cd->device, requested_type, backup_file);
1252 return LUKS_hdr_restore(backup_file, cd->device, &cd->hdr, cd);
1255 void crypt_free(struct crypt_device *cd)
1258 log_dbg("Releasing crypt device %s context.", cd->device);
1262 crypt_free_volume_key(cd->volume_key);
1267 /* used in plain device only */
1268 free((char*)cd->plain_hdr.hash);
1269 free(cd->plain_cipher);
1270 free(cd->plain_cipher_mode);
1271 free(cd->plain_uuid);
1277 int crypt_suspend(struct crypt_device *cd,
1280 crypt_status_info ci;
1281 int r, suspended = 0;
1283 log_dbg("Suspending volume %s.", name);
1285 ci = crypt_status(NULL, name);
1286 if (ci < CRYPT_ACTIVE) {
1287 log_err(cd, _("Volume %s is not active.\n"), name);
1291 if (!cd && dm_init(NULL, 1) < 0)
1294 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1295 NULL, NULL, NULL, NULL, &suspended, NULL);
1300 log_err(cd, _("Volume %s is already suspended.\n"), name);
1305 r = dm_suspend_and_wipe_key(name);
1307 log_err(cd, "Suspend is not supported for device %s.\n", name);
1309 log_err(cd, "Error during suspending device %s.\n", name);
1316 int crypt_resume_by_passphrase(struct crypt_device *cd,
1319 const char *passphrase,
1320 size_t passphrase_size)
1322 struct volume_key *vk = NULL;
1323 int r, suspended = 0;
1325 log_dbg("Resuming volume %s.", name);
1327 if (!isLUKS(cd->type)) {
1328 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1333 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1334 NULL, NULL, NULL, NULL, &suspended, NULL);
1339 log_err(cd, _("Volume %s is not suspended.\n"), name);
1344 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1345 passphrase_size, &cd->hdr, &vk, cd);
1347 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1351 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1353 log_err(cd, "Resume is not supported for device %s.\n", name);
1355 log_err(cd, "Error during resuming device %s.\n", name);
1359 crypt_free_volume_key(vk);
1360 return r < 0 ? r : keyslot;
1363 int crypt_resume_by_keyfile(struct crypt_device *cd,
1366 const char *keyfile,
1367 size_t keyfile_size)
1369 struct volume_key *vk = NULL;
1370 char *passphrase_read = NULL;
1371 unsigned int passphrase_size_read;
1372 int r, suspended = 0;
1374 log_dbg("Resuming volume %s.", name);
1376 if (!isLUKS(cd->type)) {
1377 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1382 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1383 NULL, NULL, NULL, NULL, &suspended, NULL);
1388 log_err(cd, _("Volume %s is not suspended.\n"), name);
1395 key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1396 &passphrase_size_read, keyfile, keyfile_size);
1398 if(!passphrase_read)
1401 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1402 passphrase_size_read, &cd->hdr, &vk, cd);
1403 safe_free(passphrase_read);
1408 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1410 log_err(cd, "Error during resuming device %s.\n", name);
1414 crypt_free_volume_key(vk);
1415 return r < 0 ? r : keyslot;
1418 // slot manipulation
1419 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1420 int keyslot, // -1 any
1421 const char *passphrase, // NULL -> terminal
1422 size_t passphrase_size,
1423 const char *new_passphrase, // NULL -> terminal
1424 size_t new_passphrase_size)
1426 struct volume_key *vk = NULL;
1427 char *password = NULL, *new_password = NULL;
1428 unsigned int passwordLen, new_passwordLen;
1431 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1432 "new passphrase %sprovided.",
1433 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1435 if (!isLUKS(cd->type)) {
1436 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1440 r = keyslot_verify_or_find_empty(cd, &keyslot);
1444 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1445 /* No slots used, try to use pre-generated key in header */
1446 if (cd->volume_key) {
1447 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1448 r = vk ? 0 : -ENOMEM;
1450 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1453 } else if (passphrase) {
1454 /* Passphrase provided, use it to unlock existing keyslot */
1455 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, passphrase,
1456 passphrase_size, &cd->hdr, &vk, cd);
1458 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1459 key_from_terminal(cd, _("Enter any passphrase: "),
1460 &password, &passwordLen, 0);
1466 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
1467 passwordLen, &cd->hdr, &vk, cd);
1468 safe_free(password);
1474 if (new_passphrase) {
1475 new_password = (char *)new_passphrase;
1476 new_passwordLen = new_passphrase_size;
1478 key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1479 &new_password, &new_passwordLen, 1);
1486 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1487 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1492 if (!new_passphrase)
1493 safe_free(new_password);
1494 crypt_free_volume_key(vk);
1495 return r ?: keyslot;
1498 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1500 const char *keyfile,
1501 size_t keyfile_size,
1502 const char *new_keyfile,
1503 size_t new_keyfile_size)
1505 struct volume_key *vk=NULL;
1506 char *password=NULL; unsigned int passwordLen;
1507 char *new_password = NULL; unsigned int new_passwordLen;
1510 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1511 keyfile ?: "[none]", new_keyfile ?: "[none]");
1513 if (!isLUKS(cd->type)) {
1514 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1518 r = keyslot_verify_or_find_empty(cd, &keyslot);
1522 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1523 /* No slots used, try to use pre-generated key in header */
1524 if (cd->volume_key) {
1525 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1526 r = vk ? 0 : -ENOMEM;
1528 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1532 /* Read password from file of (if NULL) from terminal */
1534 key_from_file(cd, _("Enter any passphrase: "), &password, &passwordLen,
1535 keyfile, keyfile_size);
1537 key_from_terminal(cd, _("Enter any passphrase: "),
1538 &password, &passwordLen, 0);
1543 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password, passwordLen,
1545 safe_free(password);
1552 key_from_file(cd, _("Enter new passphrase for key slot: "),
1553 &new_password, &new_passwordLen, new_keyfile,
1556 key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1557 &new_password, &new_passwordLen, 1);
1564 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1565 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1567 safe_free(new_password);
1568 crypt_free_volume_key(vk);
1569 return r < 0 ? r : keyslot;
1572 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1574 const char *volume_key,
1575 size_t volume_key_size,
1576 const char *passphrase,
1577 size_t passphrase_size)
1579 struct volume_key *vk = NULL;
1581 char *new_password = NULL; unsigned int new_passwordLen;
1583 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1585 if (!isLUKS(cd->type)) {
1586 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1591 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1592 else if (cd->volume_key)
1593 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1598 r = LUKS_verify_volume_key(&cd->hdr, vk);
1600 log_err(cd, _("Volume key does not match the volume.\n"));
1604 r = keyslot_verify_or_find_empty(cd, &keyslot);
1609 key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1610 &new_password, &new_passwordLen, 1);
1611 passphrase = new_password;
1612 passphrase_size = new_passwordLen;
1615 r = LUKS_set_key(cd->device, keyslot, passphrase, passphrase_size,
1616 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1619 safe_free(new_password);
1620 crypt_free_volume_key(vk);
1621 return r ?: keyslot;
1624 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1626 crypt_keyslot_info ki;
1628 log_dbg("Destroying keyslot %d.", keyslot);
1630 if (!isLUKS(cd->type)) {
1631 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1635 ki = crypt_keyslot_status(cd, keyslot);
1636 if (ki == CRYPT_SLOT_INVALID) {
1637 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1641 if (ki == CRYPT_SLOT_INACTIVE) {
1642 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1646 return LUKS_del_key(cd->device, keyslot, &cd->hdr, cd);
1649 // activation/deactivation of device mapping
1650 int crypt_activate_by_passphrase(struct crypt_device *cd,
1653 const char *passphrase,
1654 size_t passphrase_size,
1657 crypt_status_info ci;
1658 struct volume_key *vk = NULL;
1659 char *prompt = NULL;
1662 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1663 name ? "Activating" : "Checking", name ?: "",
1664 keyslot, passphrase ? "" : "[none] ");
1666 /* plain, use hashed passphrase */
1667 if (isPLAIN(cd->type))
1668 return create_device_helper(cd, name, cd->plain_hdr.hash,
1669 cd->plain_cipher, cd->plain_cipher_mode, NULL, passphrase, passphrase_size,
1670 cd->volume_key->keylength, 0, cd->plain_hdr.skip,
1671 cd->plain_hdr.offset, cd->plain_uuid, flags & CRYPT_ACTIVATE_READONLY, 0, 0);
1674 ci = crypt_status(NULL, name);
1675 if (ci == CRYPT_INVALID)
1677 else if (ci >= CRYPT_ACTIVE) {
1678 log_err(cd, _("Device %s already exists.\n"), name);
1683 if(asprintf(&prompt, _("Enter passphrase for %s: "), cd->device) < 0)
1686 /* provided passphrase, do not retry */
1688 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1689 passphrase_size, &cd->hdr, &vk, cd);
1691 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1696 r = open_from_hdr_and_vk(cd, vk, name, flags);
1699 crypt_free_volume_key(vk);
1702 return r < 0 ? r : keyslot;
1705 int crypt_activate_by_keyfile(struct crypt_device *cd,
1708 const char *keyfile,
1709 size_t keyfile_size,
1712 crypt_status_info ci;
1713 struct volume_key *vk = NULL;
1714 char *passphrase_read = NULL;
1715 unsigned int passphrase_size_read;
1718 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1719 name ?: "", keyslot, keyfile ?: "[none]");
1721 if (!isLUKS(cd->type)) {
1722 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1727 ci = crypt_status(NULL, name);
1728 if (ci == CRYPT_INVALID)
1730 else if (ci >= CRYPT_ACTIVE) {
1731 log_err(cd, _("Device %s already exists.\n"), name);
1739 key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1740 &passphrase_size_read, keyfile, keyfile_size);
1741 if(!passphrase_read)
1744 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1745 passphrase_size_read, &cd->hdr, &vk, cd);
1746 safe_free(passphrase_read);
1752 r = open_from_hdr_and_vk(cd, vk, name, flags);
1755 crypt_free_volume_key(vk);
1757 return r < 0 ? r : keyslot;
1760 int crypt_activate_by_volume_key(struct crypt_device *cd,
1762 const char *volume_key,
1763 size_t volume_key_size,
1766 crypt_status_info ci;
1767 struct volume_key *vk;
1770 log_dbg("Activating volume %s by volume key.", name);
1772 /* use key directly, no hash */
1773 if (isPLAIN(cd->type))
1774 return create_device_helper(cd, name, NULL,
1775 cd->plain_cipher, cd->plain_cipher_mode, NULL, volume_key, volume_key_size,
1776 cd->volume_key->keylength, 0, cd->plain_hdr.skip,
1777 cd->plain_hdr.offset, cd->plain_uuid, flags & CRYPT_ACTIVATE_READONLY, 0, 0);
1779 if (!isLUKS(cd->type)) {
1780 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1785 ci = crypt_status(NULL, name);
1786 if (ci == CRYPT_INVALID)
1788 else if (ci >= CRYPT_ACTIVE) {
1789 log_err(cd, _("Device %s already exists.\n"), name);
1794 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1797 r = LUKS_verify_volume_key(&cd->hdr, vk);
1800 log_err(cd, _("Volume key does not match the volume.\n"));
1803 r = open_from_hdr_and_vk(cd, vk, name, flags);
1805 crypt_free_volume_key(vk);
1810 int crypt_deactivate(struct crypt_device *cd, const char *name)
1817 log_dbg("Deactivating volume %s.", name);
1819 if (!cd && dm_init(NULL, 1) < 0)
1822 switch (crypt_status(cd, name)) {
1824 r = dm_remove_device(name, 0, 0);
1827 log_err(cd, _("Device %s is busy.\n"), name);
1830 case CRYPT_INACTIVE:
1831 log_err(cd, _("Device %s is not active.\n"), name);
1835 log_err(cd, _("Invalid device %s.\n"), name);
1845 // misc helper functions
1846 int crypt_volume_key_get(struct crypt_device *cd,
1849 size_t *volume_key_size,
1850 const char *passphrase,
1851 size_t passphrase_size)
1853 struct volume_key *vk;
1854 char *processed_key = NULL;
1857 key_len = crypt_get_volume_key_size(cd);
1858 if (key_len > *volume_key_size) {
1859 log_err(cd, _("Volume key buffer too small.\n"));
1863 if (isPLAIN(cd->type) && cd->plain_hdr.hash) {
1864 processed_key = process_key(cd, cd->plain_hdr.hash, NULL, key_len,
1865 passphrase, passphrase_size);
1866 if (!processed_key) {
1867 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
1870 memcpy(volume_key, processed_key, key_len);
1871 *volume_key_size = key_len;
1872 safe_free(processed_key);
1876 if (isLUKS(cd->type)) {
1877 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1878 passphrase_size, &cd->hdr, &vk, cd);
1881 memcpy(volume_key, vk->key, vk->keylength);
1882 *volume_key_size = vk->keylength;
1885 crypt_free_volume_key(vk);
1889 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
1893 int crypt_volume_key_verify(struct crypt_device *cd,
1894 const char *volume_key,
1895 size_t volume_key_size)
1897 struct volume_key *vk;
1900 if (!isLUKS(cd->type)) {
1901 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1905 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1909 r = LUKS_verify_volume_key(&cd->hdr, vk);
1912 log_err(cd, _("Volume key does not match the volume.\n"));
1914 crypt_free_volume_key(vk);
1919 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
1921 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
1922 cd->timeout = timeout_sec;
1925 void crypt_set_password_retry(struct crypt_device *cd, int tries)
1927 log_dbg("Password retry count set to %d.", tries);
1931 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
1933 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
1934 cd->iteration_time = iteration_time_ms;
1937 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
1939 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
1940 cd->password_verify = password_verify ? 1 : 0;
1943 int crypt_memory_lock(struct crypt_device *cd, int lock)
1945 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
1949 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
1953 if (!cd && dm_init(NULL, 1) < 0)
1954 return CRYPT_INVALID;
1956 r = dm_status_device(name);
1961 if (r < 0 && r != -ENODEV)
1962 return CRYPT_INVALID;
1965 return CRYPT_ACTIVE;
1970 return CRYPT_INACTIVE;
1973 static void hexprintICB(struct crypt_device *cd, char *d, int n)
1976 for(i = 0; i < n; i++)
1977 log_std(cd, "%02hhx ", (char)d[i]);
1980 int crypt_dump(struct crypt_device *cd)
1983 if (!isLUKS(cd->type)) { //FIXME
1984 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1988 log_std(cd, "LUKS header information for %s\n\n", cd->device);
1989 log_std(cd, "Version: \t%d\n", cd->hdr.version);
1990 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
1991 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
1992 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec);
1993 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
1994 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
1995 log_std(cd, "MK digest: \t");
1996 hexprintICB(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE);
1998 log_std(cd, "MK salt: \t");
1999 hexprintICB(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2);
2000 log_std(cd, "\n \t");
2001 hexprintICB(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
2003 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
2004 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
2005 for(i = 0; i < LUKS_NUMKEYS; i++) {
2006 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
2007 log_std(cd, "Key Slot %d: ENABLED\n",i);
2008 log_std(cd, "\tIterations: \t%d\n",
2009 cd->hdr.keyblock[i].passwordIterations);
2010 log_std(cd, "\tSalt: \t");
2011 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt,
2013 log_std(cd, "\n\t \t");
2014 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt +
2015 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
2018 log_std(cd, "\tKey material offset:\t%d\n",
2019 cd->hdr.keyblock[i].keyMaterialOffset);
2020 log_std(cd, "\tAF stripes: \t%d\n",
2021 cd->hdr.keyblock[i].stripes);
2024 log_std(cd, "Key Slot %d: DISABLED\n", i);
2027 log_std(cd, "DNAME: %s\n", crypt_get_device_name(cd) ?: "");
2032 const char *crypt_get_cipher(struct crypt_device *cd)
2034 if (isPLAIN(cd->type))
2035 return cd->plain_cipher;
2037 if (isLUKS(cd->type))
2038 return cd->hdr.cipherName;
2043 const char *crypt_get_cipher_mode(struct crypt_device *cd)
2045 if (isPLAIN(cd->type))
2046 return cd->plain_cipher_mode;
2048 if (isLUKS(cd->type))
2049 return cd->hdr.cipherMode;
2054 const char *crypt_get_uuid(struct crypt_device *cd)
2056 if (isLUKS(cd->type))
2057 return cd->hdr.uuid;
2062 const char *crypt_get_device_name(struct crypt_device *cd)
2067 int crypt_get_volume_key_size(struct crypt_device *cd)
2069 if (isPLAIN(cd->type))
2070 return cd->volume_key->keylength;
2072 if (isLUKS(cd->type))
2073 return cd->hdr.keyBytes;
2078 uint64_t crypt_get_data_offset(struct crypt_device *cd)
2080 if (isPLAIN(cd->type))
2081 return cd->plain_hdr.offset;
2083 if (isLUKS(cd->type))
2084 return cd->hdr.payloadOffset;
2089 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
2091 if (!isLUKS(cd->type)) {
2092 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2093 return CRYPT_SLOT_INVALID;
2096 return LUKS_keyslot_info(&cd->hdr, keyslot);