8 #include "libcryptsetup.h"
16 struct luks_masterkey *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 class, 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 class, 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 class, const char *msg)
58 cd->log(class, msg, cd->log_usrptr);
59 else if (_default_log)
60 _default_log(class, msg, NULL);
63 void logger(struct crypt_device *cd, int class, 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, class, 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.\n"));
119 } else if (passLen > key_size) {
120 memcpy(key, pass, key_size);
122 memcpy(key, pass, passLen);
128 int parse_into_name_and_mode(const char *nameAndMode, char *name, char *mode)
130 /* Token content stringification, see info cpp/stringification */
132 #define xstr(s) str(s)
133 #define scanpattern1 "%" xstr(LUKS_CIPHERNAME_L) "[^-]-%" xstr(LUKS_CIPHERMODE_L) "s"
134 #define scanpattern2 "%" xstr(LUKS_CIPHERNAME_L) "[^-]"
138 if(sscanf(nameAndMode,scanpattern1, name, mode) != 2) {
139 if((r = sscanf(nameAndMode,scanpattern2,name)) == 1)
140 strncpy(mode,"cbc-plain",10);
153 static int isPLAIN(const char *type)
155 return (type && !strcmp(CRYPT_PLAIN, type));
158 static int isLUKS(const char *type)
160 return (type && !strcmp(CRYPT_LUKS1, type));
163 /* keyslot helpers */
164 static int keyslot_verify_or_find_empty(struct crypt_device *cd, int *keyslot)
166 if (*keyslot == CRYPT_ANY_SLOT) {
167 *keyslot = LUKS_keyslot_find_empty(&cd->hdr);
169 log_err(cd, _("All key slots full.\n"));
174 switch (LUKS_keyslot_info(&cd->hdr, *keyslot)) {
175 case CRYPT_SLOT_INVALID:
176 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
177 *keyslot, LUKS_NUMKEYS - 1);
179 case CRYPT_SLOT_INACTIVE:
182 log_err(cd, _("Key slot %d is full, please select another one.\n"),
190 static int verify_other_keyslot(struct crypt_device *cd,
191 const char *key_file,
195 struct luks_masterkey *mk;
196 crypt_keyslot_info ki;
198 char *password = NULL;
199 unsigned int passwordLen;
201 get_key(_("Enter any remaining LUKS passphrase: "), &password,
202 &passwordLen, 0, key_file, cd->timeout, flags, cd);
206 ki = crypt_keyslot_status(cd, keyIndex);
207 if (ki == CRYPT_SLOT_ACTIVE) /* Not last slot */
208 LUKS_keyslot_set(&cd->hdr, keyIndex, 0);
210 openedIndex = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT,
211 password, passwordLen,
214 if (ki == CRYPT_SLOT_ACTIVE)
215 LUKS_keyslot_set(&cd->hdr, keyIndex, 1);
216 LUKS_dealloc_masterkey(mk);
222 log_std(cd, _("Key slot %d verified.\n"), openedIndex);
226 static int find_keyslot_by_passphrase(struct crypt_device *cd,
227 const char *key_file,
231 struct luks_masterkey *mk;
232 char *password = NULL;
233 unsigned int passwordLen;
236 get_key(message,&password,&passwordLen, 0, key_file,
237 cd->timeout, flags, cd);
241 keyIndex = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
242 passwordLen, &cd->hdr, &mk, cd);
243 LUKS_dealloc_masterkey(mk);
249 static int device_check_and_adjust(struct crypt_device *cd,
251 uint64_t *size, uint64_t *offset,
254 struct device_infos infos;
256 if (get_device_infos(device, &infos, cd) < 0) {
257 log_err(cd, _("Cannot get info about device %s.\n"), device);
264 log_err(cd, _("Device %s has zero size.\n"), device);
267 if (*size < *offset) {
268 log_err(cd, _("Device %s is too small.\n"), device);
277 log_dbg("Calculated device size is %" PRIu64 " sectors (%s), offset %" PRIu64 ".",
278 *size, *read_only ? "RO" : "RW", *offset);
282 static int luks_remove_helper(struct crypt_device *cd,
284 const char *other_key_file,
285 const char *key_file,
288 crypt_keyslot_info ki;
291 if (key_slot == CRYPT_ANY_SLOT) {
292 key_slot = find_keyslot_by_passphrase(cd, key_file, 0,
293 _("Enter LUKS passphrase to be deleted: "));
299 log_std(cd, _("key slot %d selected for deletion.\n"), key_slot);
302 ki = crypt_keyslot_status(cd, key_slot);
303 if (ki == CRYPT_SLOT_INVALID) {
304 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
305 key_slot, LUKS_NUMKEYS - 1);
309 if (ki <= CRYPT_SLOT_INACTIVE) {
310 log_err(cd, _("Key %d not active. Can't wipe.\n"), key_slot);
315 if (ki == CRYPT_SLOT_ACTIVE_LAST && cd->confirm &&
316 !(cd->confirm(_("This is the last keyslot."
317 " Device will become unusable after purging this key."),
318 cd->confirm_usrptr))) {
324 r = verify_other_keyslot(cd, other_key_file, 0, key_slot);
329 r = crypt_keyslot_destroy(cd, key_slot);
331 return (r < 0) ? r : 0;
334 static int create_device_helper(struct crypt_device *cd,
338 const char *cipher_mode,
339 const char *key_file,
351 crypt_status_info ci;
352 char *dm_cipher = NULL;
353 char *processed_key = NULL;
356 ci = crypt_status(cd, name);
357 if (ci == CRYPT_INVALID)
360 if (reload && ci < CRYPT_ACTIVE)
363 if (!reload && ci >= CRYPT_ACTIVE) {
364 log_err(cd, _("Device %s already exists.\n"), name);
368 if (key_size < 0 || key_size > 1024) {
369 log_err(cd, _("Invalid key size %d.\n"), key_size);
373 r = device_check_and_adjust(cd, cd->device, &size, &offset, &read_only);
377 if (cipher_mode && asprintf(&dm_cipher, "%s-%s", cipher, cipher_mode) < 0)
380 processed_key = process_key(cd, hash, key_file, key_size, key, keyLen);
384 r = dm_create_device(name, cd->device, dm_cipher ?: cipher, cd->type, uuid, size, skip, offset,
385 key_size, processed_key, read_only, reload);
388 safe_free(processed_key);
392 static int open_from_hdr_and_mk(struct crypt_device *cd,
393 struct luks_masterkey *mk,
397 uint64_t size, offset;
399 int read_only, no_uuid, r;
402 offset = crypt_get_data_offset(cd);
403 read_only = flags & CRYPT_ACTIVATE_READONLY;
404 no_uuid = flags & CRYPT_ACTIVATE_NO_UUID;
406 r = device_check_and_adjust(cd, cd->device, &size, &offset, &read_only);
410 if (asprintf(&cipher, "%s-%s", crypt_get_cipher(cd),
411 crypt_get_cipher_mode(cd)) < 0)
414 r = dm_create_device(name, cd->device, cipher, cd->type,
415 no_uuid ? NULL : crypt_get_uuid(cd),
416 size, 0, offset, mk->keyLength, mk->key,
422 static void log_wrapper(int class, const char *msg, void *usrptr)
424 void (*xlog)(int class, char *msg) = usrptr;
425 xlog(class, (char *)msg);
428 static int yesDialog_wrapper(const char *msg, void *usrptr)
430 int (*xyesDialog)(char *msg) = usrptr;
431 return xyesDialog((char*)msg);
434 int crypt_confirm(struct crypt_device *cd, const char *msg)
436 if (!cd || !cd->confirm)
439 return cd->confirm(msg, cd->confirm_usrptr);
442 static void key_from_terminal(struct crypt_device *cd, char *msg, char **key,
443 unsigned int *key_len, int force_verify)
448 *key = safe_alloc(MAX_TTY_PASSWORD_LEN);
451 r = cd->password(msg, *key, (size_t)key_len, cd->password_usrptr);
458 if (force_verify || cd->password_verify)
459 flags |= CRYPT_FLAG_VERIFY_IF_POSSIBLE;
460 get_key(msg, key, key_len, 0, NULL, cd->timeout, flags, cd);
464 static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslot,
465 struct luks_masterkey **mk)
467 char *prompt = NULL, *passphrase_read = NULL;
468 unsigned int passphrase_size_read;
469 int r = -EINVAL, tries = cd->tries;
471 if(asprintf(&prompt, _("Enter passphrase for %s: "), cd->device) < 0)
477 LUKS_dealloc_masterkey(*mk);
480 key_from_terminal(cd, prompt, &passphrase_read,
481 &passphrase_size_read, 0);
482 if(!passphrase_read) {
487 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
488 passphrase_size_read, &cd->hdr, mk, cd);
489 safe_free(passphrase_read);
490 passphrase_read = NULL;
491 } while (r == -EPERM && (--tries > 0));
494 LUKS_dealloc_masterkey(*mk);
503 static void key_from_file(struct crypt_device *cd, char *msg,
504 char **key, unsigned int *key_len,
505 const char *key_file, size_t key_size)
507 get_key(msg, key, key_len, key_size, key_file, cd->timeout, 0, cd);
510 static int _crypt_init(struct crypt_device **cd,
512 struct crypt_options *options,
513 int load, int need_dm)
517 /* if it is plain device and mapping table is being reloaded
518 initialize it by name*/
519 init_by_name = (type && !strcmp(type, CRYPT_PLAIN) && load);
521 /* Some of old API calls do not require DM in kernel,
522 fake initialisation by initialise it with kernel_check disabled */
524 (void)dm_init(NULL, 0);
526 r = crypt_init_by_name(cd, options->name);
528 r = crypt_init(cd, options->device);
535 crypt_set_log_callback(*cd, log_wrapper, options->icb->log);
536 crypt_set_confirm_callback(*cd, yesDialog_wrapper, options->icb->yesDialog);
538 crypt_set_timeout(*cd, options->timeout);
539 crypt_set_password_retry(*cd, options->tries);
540 crypt_set_iterarion_time(*cd, options->iteration_time ?: 1000);
541 crypt_set_password_verify(*cd, options->flags & CRYPT_FLAG_VERIFY);
543 if (load && !init_by_name)
544 r = crypt_load(*cd, type, NULL);
546 if (!r && type && !(*cd)->type) {
547 (*cd)->type = strdup(type);
558 void crypt_set_log_callback(struct crypt_device *cd,
559 void (*log)(int class, const char *msg, void *usrptr),
566 cd->log_usrptr = usrptr;
570 void crypt_set_confirm_callback(struct crypt_device *cd,
571 int (*confirm)(const char *msg, void *usrptr),
574 cd->confirm = confirm;
575 cd->confirm_usrptr = usrptr;
578 void crypt_set_password_callback(struct crypt_device *cd,
579 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
582 cd->password = password;
583 cd->password_usrptr = usrptr;
586 /* OPTIONS: name, cipher, device, hash, key_file, key_size, key_slot,
587 * offset, size, skip, timeout, tries, passphrase_fd (ignored),
589 int crypt_create_device(struct crypt_options *options)
591 struct crypt_device *cd = NULL;
596 r = _crypt_init(&cd, CRYPT_PLAIN, options, 0, 1);
600 get_key(_("Enter passphrase: "), &key, &keyLen, options->key_size,
601 options->key_file, cd->timeout, options->flags, cd);
605 r = create_device_helper(cd, options->name, options->hash,
606 options->cipher, NULL, options->key_file, key, keyLen,
607 options->key_size, options->size, options->skip,
608 options->offset, NULL, options->flags & CRYPT_FLAG_READONLY,
616 /* OPTIONS: same as create above */
617 int crypt_update_device(struct crypt_options *options)
619 struct crypt_device *cd = NULL;
624 r = _crypt_init(&cd, CRYPT_PLAIN, options, 1, 1);
628 get_key(_("Enter passphrase: "), &key, &keyLen, options->key_size,
629 options->key_file, cd->timeout, options->flags, cd);
633 r = create_device_helper(cd, options->name, options->hash,
634 options->cipher, NULL, options->key_file, key, keyLen,
635 options->key_size, options->size, options->skip,
636 options->offset, NULL, options->flags & CRYPT_FLAG_READONLY,
644 /* OPTIONS: name, size, icb */
645 int crypt_resize_device(struct crypt_options *options)
647 struct crypt_device *cd = NULL;
648 char *device = NULL, *cipher = NULL, *uuid = NULL, *key = NULL;
650 uint64_t size, skip, offset;
651 int key_size, read_only, r;
653 log_dbg("Resizing device %s to %" PRIu64 " sectors.", options->name, options->size);
655 if (dm_init(NULL, 1) < 0)
658 r = dm_query_device(options->name, &device, &size, &skip, &offset,
659 &cipher, &key_size, &key, &read_only, NULL, &uuid);
661 log_err(NULL, _("Device %s is not active.\n"), options->name);
665 /* Try to determine type of device from UUID */
668 if (!strncmp(uuid, CRYPT_PLAIN, strlen(CRYPT_PLAIN))) {
672 } else if (!strncmp(uuid, CRYPT_LUKS1, strlen(CRYPT_LUKS1)))
676 if (!options->device)
677 options->device = device;
679 r = _crypt_init(&cd, type, options, 1, 1);
683 size = options->size;
684 r = device_check_and_adjust(cd, device, &size, &offset, &read_only);
688 r = dm_create_device(options->name, device, cipher, type,
689 crypt_get_uuid(cd), size, skip, offset,
690 key_size, key, read_only, 1);
694 if (options->device == device)
695 options->device = NULL;
703 /* OPTIONS: name, icb */
704 int crypt_query_device(struct crypt_options *options)
708 log_dbg("Query device %s.", options->name);
710 if (dm_init(NULL, 1) < 0)
713 r = dm_status_device(options->name);
719 r = dm_query_device(options->name, (char **)&options->device, &options->size,
720 &options->skip, &options->offset, (char **)&options->cipher,
721 &options->key_size, NULL, &read_only, NULL, NULL);
728 options->flags |= CRYPT_FLAG_READONLY;
730 options->flags |= CRYPT_FLAG_FREE_DEVICE;
731 options->flags |= CRYPT_FLAG_FREE_CIPHER;
736 /* OPTIONS: name, icb */
737 int crypt_remove_device(struct crypt_options *options)
739 struct crypt_device *cd = NULL;
742 r = crypt_init_by_name(&cd, options->name);
746 r = crypt_deactivate(cd, options->name);
753 /* OPTIONS: device, cipher, hash, align_payload, key_size (master key), key_slot
754 * new_key_file, iteration_time, timeout, flags, icb */
755 int crypt_luksFormat(struct crypt_options *options)
757 char cipherName[LUKS_CIPHERNAME_L];
758 char cipherMode[LUKS_CIPHERMODE_L];
760 unsigned int passwordLen;
761 struct crypt_device *cd;
762 struct crypt_params_luks1 cp = {
763 .hash = options->hash,
764 .data_alignment = options->align_payload
768 r = parse_into_name_and_mode(options->cipher, cipherName, cipherMode);
770 log_err(cd, _("No known cipher specification pattern detected.\n"));
774 if ((r = _crypt_init(&cd, CRYPT_LUKS1, options, 0, 1)))
777 if (options->key_slot >= LUKS_NUMKEYS && options->key_slot != CRYPT_ANY_SLOT) {
778 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
779 options->key_slot, LUKS_NUMKEYS - 1);
784 get_key(_("Enter LUKS passphrase: "), &password, &passwordLen, 0,
785 options->new_key_file, options->timeout, options->flags, cd);
792 r = crypt_format(cd, CRYPT_LUKS1, cipherName, cipherMode,
793 NULL, NULL, options->key_size, &cp);
797 /* Add keyslot using internally stored volume key generated during format */
798 r = crypt_keyslot_add_by_volume_key(cd, options->key_slot, NULL, 0,
799 password, passwordLen);
803 return (r < 0) ? r : 0;
806 /* OPTIONS: name, device, key_size, key_file, timeout, tries, flags, icb */
807 int crypt_luksOpen(struct crypt_options *options)
809 struct crypt_device *cd = NULL;
816 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
820 if (options->flags & CRYPT_FLAG_READONLY)
821 flags |= CRYPT_ACTIVATE_READONLY;
823 if (options->flags & CRYPT_FLAG_NON_EXCLUSIVE_ACCESS)
824 flags |= CRYPT_ACTIVATE_NO_UUID;
826 if (options->key_file)
827 r = crypt_activate_by_keyfile(cd, options->name,
828 CRYPT_ANY_SLOT, options->key_file, options->key_size,
831 r = crypt_activate_by_passphrase(cd, options->name,
832 CRYPT_ANY_SLOT, options->passphrase,
833 options->passphrase ? strlen(options->passphrase) : 0,
837 return (r < 0) ? r : 0;
840 /* OPTIONS: device, keys_slot, key_file, timeout, flags, icb */
841 int crypt_luksKillSlot(struct crypt_options *options)
843 struct crypt_device *cd = NULL;
846 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
850 r = luks_remove_helper(cd, options->key_slot, options->key_file, NULL,
851 options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
854 return (r < 0) ? r : 0;
857 /* OPTIONS: device, new_key_file, key_file, timeout, flags, icb */
858 int crypt_luksRemoveKey(struct crypt_options *options)
860 struct crypt_device *cd = NULL;
863 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
867 r = luks_remove_helper(cd, CRYPT_ANY_SLOT, options->key_file, options->new_key_file,
868 options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
871 return (r < 0) ? r : 0;
875 /* OPTIONS: device, new_key_file, key_file, key_slot, flags,
876 iteration_time, timeout, icb */
877 int crypt_luksAddKey(struct crypt_options *options)
879 struct crypt_device *cd = NULL;
882 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
886 if (options->key_file || options->new_key_file)
887 r = crypt_keyslot_add_by_keyfile(cd, options->key_slot,
888 options->key_file, 0,
889 options->new_key_file, 0);
891 r = crypt_keyslot_add_by_passphrase(cd, options->key_slot,
895 return (r < 0) ? r : 0;
898 /* OPTIONS: device, icb */
899 int crypt_luksUUID(struct crypt_options *options)
901 struct crypt_device *cd = NULL;
905 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
909 uuid = (char *)crypt_get_uuid(cd);
910 log_std(cd, uuid ?: "");
916 /* OPTIONS: device, icb */
917 int crypt_isLuks(struct crypt_options *options)
919 struct crypt_device *cd = NULL;
922 log_dbg("Check device %s for LUKS header.", options->device);
924 r = crypt_init(&cd, options->device);
928 /* Do print fail here, no need to crypt_load() */
929 r = LUKS_read_phdr(cd->device, &cd->hdr, 0, cd) ? -EINVAL : 0;
935 /* OPTIONS: device, icb */
936 int crypt_luksDump(struct crypt_options *options)
938 struct crypt_device *cd = NULL;
941 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
951 void crypt_get_error(char *buf, size_t size)
953 const char *error = get_error();
955 if (!buf || size < 1)
958 strncpy(buf, error, size - 1);
959 buf[size - 1] = '\0';
965 void crypt_put_options(struct crypt_options *options)
967 if (options->flags & CRYPT_FLAG_FREE_DEVICE) {
968 free((char *)options->device);
969 options->device = NULL;
970 options->flags &= ~CRYPT_FLAG_FREE_DEVICE;
972 if (options->flags & CRYPT_FLAG_FREE_CIPHER) {
973 free((char *)options->cipher);
974 options->cipher = NULL;
975 options->flags &= ~CRYPT_FLAG_FREE_CIPHER;
979 const char *crypt_get_dir(void)
984 /////////////////////////////////
989 int crypt_init(struct crypt_device **cd, const char *device)
991 struct crypt_device *h = NULL;
996 log_dbg("Allocating crypt device %s context.", device);
998 if (device && !device_ready(NULL, device, O_RDONLY))
1001 if (!(h = malloc(sizeof(struct crypt_device))))
1004 memset(h, 0, sizeof(*h));
1007 h->device = strdup(device);
1015 if (dm_init(h, 1) < 0) {
1020 h->iteration_time = 1000;
1021 h->password_verify = 0;
1027 int crypt_init_by_name(struct crypt_device **cd, const char *name)
1029 crypt_status_info ci;
1030 char *device = NULL;
1033 log_dbg("Allocating crypt device context by device %s.", name);
1035 ci = crypt_status(NULL, name);
1036 if (ci == CRYPT_INVALID)
1039 if (ci < CRYPT_ACTIVE) {
1040 log_err(NULL, _("Device %s is not active.\n"), name);
1044 r = dm_query_device(name, &device, NULL, NULL, NULL,
1045 NULL, NULL, NULL, NULL, NULL, NULL);
1047 r = crypt_init(cd, device);
1053 static int _crypt_format_plain(struct crypt_device *cd,
1055 const char *cipher_mode,
1057 struct crypt_params_plain *params)
1059 if (!cipher || !cipher_mode || !params || !params->hash) {
1060 log_err(cd, _("Invalid plain crypt parameters.\n"));
1064 if (cd->volume_key->keyLength > 1024) {
1065 log_err(cd, _("Invalid key size.\n"));
1069 cd->plain_cipher = strdup(cipher);
1070 cd->plain_cipher_mode = strdup(cipher_mode);
1073 cd->plain_uuid = strdup(uuid);
1076 cd->plain_hdr.hash = strdup(params->hash);
1078 cd->plain_hdr.offset = params->offset;
1079 cd->plain_hdr.skip = params->skip;
1081 if ((params->hash && !cd->plain_hdr.hash) ||
1082 !cd->plain_cipher || !cd->plain_cipher_mode)
1088 static int _crypt_format_luks1(struct crypt_device *cd,
1090 const char *cipher_mode,
1092 struct crypt_params_luks1 *params)
1097 log_err(cd, _("Can't format LUKS without device.\n"));
1101 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
1102 (params && params->hash) ? params->hash : "sha1",
1104 params ? params->data_alignment: DEFAULT_ALIGNMENT,
1105 cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1109 /* Wipe first 8 sectors - fs magic numbers etc. */
1110 r = wipe_device_header(cd->device, 8);
1112 log_err(cd, _("Can't wipe header on device %s.\n"), cd->device);
1116 r = LUKS_write_phdr(cd->device, &cd->hdr, cd);
1121 int crypt_format(struct crypt_device *cd,
1124 const char *cipher_mode,
1126 const char *volume_key,
1127 size_t volume_key_size,
1132 log_dbg("Formatting device %s as type %s.", cd->device ?: "(none)", cd->type ?: "(none)");
1137 /* Some hash functions need initialized gcrypt library */
1138 if (init_crypto()) {
1139 log_err(cd, _("Cannot initialize crypto backend.\n"));
1144 cd->volume_key = LUKS_alloc_masterkey(volume_key_size,
1147 cd->volume_key = LUKS_generate_masterkey(volume_key_size);
1153 r = _crypt_format_plain(cd, cipher, cipher_mode,
1155 else if (isLUKS(type))
1156 r = _crypt_format_luks1(cd, cipher, cipher_mode,
1159 /* FIXME: allow plugins here? */
1160 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
1164 if (!r && !(cd->type = strdup(type)))
1168 LUKS_dealloc_masterkey(cd->volume_key);
1169 cd->volume_key = NULL;
1175 int crypt_load(struct crypt_device *cd,
1176 const char *requested_type,
1179 struct luks_phdr hdr;
1182 log_dbg("Trying to load %s crypt type from device %s.",
1183 requested_type ?: "any", cd->device ?: "(none)");
1188 if (requested_type && !isLUKS(requested_type))
1191 /* Some hash functions need initialized gcrypt library */
1192 if (init_crypto()) {
1193 log_err(cd, _("Cannot initialize crypto backend.\n"));
1197 r = LUKS_read_phdr(cd->device, &hdr, 1, cd);
1200 memcpy(&cd->hdr, &hdr, sizeof(hdr));
1201 cd->type = strdup(requested_type);
1209 int crypt_header_backup(struct crypt_device *cd,
1210 const char *requested_type,
1211 const char *backup_file)
1213 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1216 /* Some hash functions need initialized gcrypt library */
1217 if (init_crypto()) {
1218 log_err(cd, _("Cannot initialize crypto backend.\n"));
1222 log_dbg("Requested header backup of device %s (%s) to "
1223 "file %s.", cd->device, requested_type, backup_file);
1225 return LUKS_hdr_backup(backup_file, cd->device, &cd->hdr, cd);
1228 int crypt_header_restore(struct crypt_device *cd,
1229 const char *requested_type,
1230 const char *backup_file)
1232 if (requested_type && !isLUKS(requested_type))
1235 /* Some hash functions need initialized gcrypt library */
1236 if (init_crypto()) {
1237 log_err(cd, _("Cannot initialize crypto backend.\n"));
1241 log_dbg("Requested header restore to device %s (%s) from "
1242 "file %s.", cd->device, requested_type, backup_file);
1244 return LUKS_hdr_restore(backup_file, cd->device, &cd->hdr, cd);
1247 void crypt_free(struct crypt_device *cd)
1250 log_dbg("Releasing crypt device %s context.", cd->device);
1254 LUKS_dealloc_masterkey(cd->volume_key);
1259 /* used in plain device only */
1260 free((char*)cd->plain_hdr.hash);
1261 free(cd->plain_cipher);
1262 free(cd->plain_cipher_mode);
1263 free(cd->plain_uuid);
1269 int crypt_suspend(struct crypt_device *cd,
1272 crypt_status_info ci;
1273 int r, suspended = 0;
1275 log_dbg("Suspending volume %s.", name);
1277 ci = crypt_status(NULL, name);
1278 if (ci < CRYPT_ACTIVE) {
1279 log_err(cd, _("Volume %s is not active.\n"), name);
1283 if (!cd && dm_init(NULL, 1) < 0)
1286 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1287 NULL, NULL, NULL, NULL, &suspended, NULL);
1292 log_err(cd, _("Volume %s is already suspended.\n"), name);
1297 r = dm_suspend_and_wipe_key(name);
1299 log_err(cd, "Error during suspending device %s.\n", name);
1306 int crypt_resume_by_passphrase(struct crypt_device *cd,
1309 const char *passphrase,
1310 size_t passphrase_size)
1312 struct luks_masterkey *mk = NULL;
1313 int r, suspended = 0;
1315 log_dbg("Resuming volume %s.", name);
1317 if (!isLUKS(cd->type)) {
1318 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1323 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1324 NULL, NULL, NULL, NULL, &suspended, NULL);
1329 log_err(cd, _("Volume %s is not suspended.\n"), name);
1334 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1335 passphrase_size, &cd->hdr, &mk, cd);
1337 r = volume_key_by_terminal_passphrase(cd, keyslot, &mk);
1341 r = dm_resume_and_reinstate_key(name, mk->keyLength, mk->key);
1343 log_err(cd, "Error during resuming device %s.\n", name);
1347 LUKS_dealloc_masterkey(mk);
1348 return r < 0 ? r : keyslot;
1351 int crypt_resume_by_keyfile(struct crypt_device *cd,
1354 const char *keyfile,
1355 size_t keyfile_size)
1357 struct luks_masterkey *mk = NULL;
1358 char *passphrase_read = NULL;
1359 unsigned int passphrase_size_read;
1360 int r, suspended = 0;
1362 log_dbg("Resuming volume %s.", name);
1364 if (!isLUKS(cd->type)) {
1365 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1370 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1371 NULL, NULL, NULL, NULL, &suspended, NULL);
1376 log_err(cd, _("Volume %s is not suspended.\n"), name);
1383 key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1384 &passphrase_size_read, keyfile, keyfile_size);
1386 if(!passphrase_read)
1389 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1390 passphrase_size_read, &cd->hdr, &mk, cd);
1391 safe_free(passphrase_read);
1396 r = dm_resume_and_reinstate_key(name, mk->keyLength, mk->key);
1398 log_err(cd, "Error during resuming device %s.\n", name);
1402 LUKS_dealloc_masterkey(mk);
1403 return r < 0 ? r : keyslot;
1406 // slot manipulation
1407 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1408 int keyslot, // -1 any
1409 const char *passphrase, // NULL -> terminal
1410 size_t passphrase_size,
1411 const char *new_passphrase, // NULL -> terminal
1412 size_t new_passphrase_size)
1414 struct luks_masterkey *mk = NULL;
1415 char *password = NULL, *new_password = NULL;
1416 unsigned int passwordLen, new_passwordLen;
1419 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1420 "new passphrase %sprovided.",
1421 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1423 if (!isLUKS(cd->type)) {
1424 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1428 r = keyslot_verify_or_find_empty(cd, &keyslot);
1432 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1433 /* No slots used, try to use pre-generated key in header */
1434 if (cd->volume_key) {
1435 mk = LUKS_alloc_masterkey(cd->volume_key->keyLength, cd->volume_key->key);
1436 r = mk ? 0 : -ENOMEM;
1438 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1441 } else if (passphrase) {
1442 /* Passphrase provided, use it to unlock existing keyslot */
1443 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, passphrase,
1444 passphrase_size, &cd->hdr, &mk, cd);
1446 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1447 key_from_terminal(cd, _("Enter any passphrase: "),
1448 &password, &passwordLen, 0);
1454 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
1455 passwordLen, &cd->hdr, &mk, cd);
1456 safe_free(password);
1462 if (new_passphrase) {
1463 new_password = (char *)new_passphrase;
1464 new_passwordLen = new_passphrase_size;
1466 key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1467 &new_password, &new_passwordLen, 1);
1474 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1475 &cd->hdr, mk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1480 if (!new_passphrase)
1481 safe_free(new_password);
1482 LUKS_dealloc_masterkey(mk);
1483 return r ?: keyslot;
1486 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1488 const char *keyfile,
1489 size_t keyfile_size,
1490 const char *new_keyfile,
1491 size_t new_keyfile_size)
1493 struct luks_masterkey *mk=NULL;
1494 char *password=NULL; unsigned int passwordLen;
1495 char *new_password = NULL; unsigned int new_passwordLen;
1498 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1499 keyfile ?: "[none]", new_keyfile ?: "[none]");
1501 if (!isLUKS(cd->type)) {
1502 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1506 r = keyslot_verify_or_find_empty(cd, &keyslot);
1510 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1511 /* No slots used, try to use pre-generated key in header */
1512 if (cd->volume_key) {
1513 mk = LUKS_alloc_masterkey(cd->volume_key->keyLength, cd->volume_key->key);
1514 r = mk ? 0 : -ENOMEM;
1516 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1520 /* Read password from file of (if NULL) from terminal */
1522 key_from_file(cd, _("Enter any passphrase: "), &password, &passwordLen,
1523 keyfile, keyfile_size);
1525 key_from_terminal(cd, _("Enter any passphrase: "),
1526 &password, &passwordLen, 0);
1531 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password, passwordLen,
1533 safe_free(password);
1540 key_from_file(cd, _("Enter new passphrase for key slot: "),
1541 &new_password, &new_passwordLen, new_keyfile,
1544 key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1545 &new_password, &new_passwordLen, 1);
1552 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1553 &cd->hdr, mk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1555 safe_free(new_password);
1556 LUKS_dealloc_masterkey(mk);
1557 return r < 0 ? r : keyslot;
1560 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1562 const char *volume_key,
1563 size_t volume_key_size,
1564 const char *passphrase,
1565 size_t passphrase_size)
1567 struct luks_masterkey *mk = NULL;
1569 char *new_password = NULL; unsigned int new_passwordLen;
1571 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1573 if (!isLUKS(cd->type)) {
1574 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1579 mk = LUKS_alloc_masterkey(volume_key_size, volume_key);
1580 else if (cd->volume_key)
1581 mk = LUKS_alloc_masterkey(cd->volume_key->keyLength, cd->volume_key->key);
1586 r = LUKS_verify_master_key(&cd->hdr, mk);
1588 log_err(cd, _("Volume key does not match the volume.\n"));
1592 r = keyslot_verify_or_find_empty(cd, &keyslot);
1597 key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1598 &new_password, &new_passwordLen, 1);
1599 passphrase = new_password;
1600 passphrase_size = new_passwordLen;
1603 r = LUKS_set_key(cd->device, keyslot, passphrase, passphrase_size,
1604 &cd->hdr, mk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1607 safe_free(new_password);
1608 LUKS_dealloc_masterkey(mk);
1609 return r ?: keyslot;
1612 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1614 crypt_keyslot_info ki;
1616 log_dbg("Destroying keyslot %d.", keyslot);
1618 if (!isLUKS(cd->type)) {
1619 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1623 ki = crypt_keyslot_status(cd, keyslot);
1624 if (ki == CRYPT_SLOT_INVALID) {
1625 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1629 if (ki == CRYPT_SLOT_INACTIVE) {
1630 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1634 return LUKS_del_key(cd->device, keyslot, &cd->hdr, cd);
1637 // activation/deactivation of device mapping
1638 int crypt_activate_by_passphrase(struct crypt_device *cd,
1641 const char *passphrase,
1642 size_t passphrase_size,
1645 crypt_status_info ci;
1646 struct luks_masterkey *mk = NULL;
1647 char *prompt = NULL;
1650 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1651 name ? "Activating" : "Checking", name ?: "",
1652 keyslot, passphrase ? "" : "[none] ");
1657 /* plain, use hashed passphrase */
1658 if (isPLAIN(cd->type))
1659 return create_device_helper(cd, name, cd->plain_hdr.hash,
1660 cd->plain_cipher, cd->plain_cipher_mode, NULL, passphrase, passphrase_size,
1661 cd->volume_key->keyLength, 0, cd->plain_hdr.skip,
1662 cd->plain_hdr.offset, cd->plain_uuid, flags & CRYPT_ACTIVATE_READONLY, 0, 0);
1665 ci = crypt_status(NULL, name);
1666 if (ci == CRYPT_INVALID)
1668 else if (ci >= CRYPT_ACTIVE) {
1669 log_err(cd, _("Device %s already exists.\n"), name);
1674 if(asprintf(&prompt, _("Enter passphrase for %s: "), cd->device) < 0)
1677 /* provided passphrase, do not retry */
1679 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1680 passphrase_size, &cd->hdr, &mk, cd);
1682 r = volume_key_by_terminal_passphrase(cd, keyslot, &mk);
1687 r = open_from_hdr_and_mk(cd, mk, name, flags);
1690 LUKS_dealloc_masterkey(mk);
1693 return r < 0 ? r : keyslot;
1696 int crypt_activate_by_keyfile(struct crypt_device *cd,
1699 const char *keyfile,
1700 size_t keyfile_size,
1703 crypt_status_info ci;
1704 struct luks_masterkey *mk = NULL;
1705 char *passphrase_read = NULL;
1706 unsigned int passphrase_size_read;
1709 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1710 name, keyslot, keyfile ?: "[none]");
1712 if (!isLUKS(cd->type)) {
1713 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1718 ci = crypt_status(NULL, name);
1719 if (ci == CRYPT_INVALID)
1721 else if (ci >= CRYPT_ACTIVE) {
1722 log_err(cd, _("Device %s already exists.\n"), name);
1730 key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1731 &passphrase_size_read, keyfile, keyfile_size);
1732 if(!passphrase_read)
1735 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1736 passphrase_size_read, &cd->hdr, &mk, cd);
1737 safe_free(passphrase_read);
1742 r = open_from_hdr_and_mk(cd, mk, name, flags);
1745 LUKS_dealloc_masterkey(mk);
1747 return r < 0 ? r : keyslot;
1750 int crypt_activate_by_volume_key(struct crypt_device *cd,
1752 const char *volume_key,
1753 size_t volume_key_size,
1756 crypt_status_info ci;
1757 struct luks_masterkey *mk;
1760 log_dbg("Activating volume %s by volume key.", name);
1762 /* use key directly, no hash */
1763 if (isPLAIN(cd->type))
1764 return create_device_helper(cd, name, NULL,
1765 cd->plain_cipher, cd->plain_cipher_mode, NULL, volume_key, volume_key_size,
1766 cd->volume_key->keyLength, 0, cd->plain_hdr.skip,
1767 cd->plain_hdr.offset, cd->plain_uuid, flags & CRYPT_ACTIVATE_READONLY, 0, 0);
1769 if (!isLUKS(cd->type)) {
1770 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1775 ci = crypt_status(NULL, name);
1776 if (ci == CRYPT_INVALID)
1778 else if (ci >= CRYPT_ACTIVE) {
1779 log_err(cd, _("Device %s already exists.\n"), name);
1784 mk = LUKS_alloc_masterkey(volume_key_size, volume_key);
1787 r = LUKS_verify_master_key(&cd->hdr, mk);
1790 log_err(cd, _("Volume key does not match the volume.\n"));
1793 r = open_from_hdr_and_mk(cd, mk, name, flags);
1795 LUKS_dealloc_masterkey(mk);
1800 int crypt_deactivate(struct crypt_device *cd, const char *name)
1807 log_dbg("Deactivating volume %s.", name);
1809 if (!cd && dm_init(NULL, 1) < 0)
1812 switch (crypt_status(cd, name)) {
1814 r = dm_remove_device(name, 0, 0);
1817 log_err(cd, _("Device %s is busy.\n"), name);
1820 case CRYPT_INACTIVE:
1821 log_err(cd, _("Device %s is not active.\n"), name);
1825 log_err(cd, _("Invalid device %s.\n"), name);
1835 // misc helper functions
1836 int crypt_volume_key_get(struct crypt_device *cd,
1839 size_t *volume_key_size,
1840 const char *passphrase,
1841 size_t passphrase_size)
1843 struct luks_masterkey *mk;
1844 char *processed_key = NULL;
1847 key_len = crypt_get_volume_key_size(cd);
1848 if (key_len > *volume_key_size) {
1849 log_err(cd, _("Volume key buffer too small.\n"));
1853 if (isPLAIN(cd->type)) {
1854 processed_key = process_key(cd, cd->plain_hdr.hash, NULL, key_len,
1855 passphrase, passphrase_size);
1856 if (!processed_key) {
1857 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
1860 memcpy(volume_key, processed_key, key_len);
1861 *volume_key_size = key_len;
1862 safe_free(processed_key);
1866 if (isLUKS(cd->type)) {
1867 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1868 passphrase_size, &cd->hdr, &mk, cd);
1871 memcpy(volume_key, mk->key, mk->keyLength);
1872 *volume_key_size = mk->keyLength;
1875 LUKS_dealloc_masterkey(mk);
1879 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
1883 int crypt_volume_key_verify(struct crypt_device *cd,
1884 const char *volume_key,
1885 size_t volume_key_size)
1887 struct luks_masterkey *mk;
1890 if (!isLUKS(cd->type)) {
1891 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1895 mk = LUKS_alloc_masterkey(volume_key_size, volume_key);
1899 r = LUKS_verify_master_key(&cd->hdr, mk);
1902 log_err(cd, _("Volume key does not match the volume.\n"));
1904 LUKS_dealloc_masterkey(mk);
1909 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
1911 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
1912 cd->timeout = timeout_sec;
1915 void crypt_set_password_retry(struct crypt_device *cd, int tries)
1917 log_dbg("Password retry count set to %d.", tries);
1921 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
1923 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
1924 cd->iteration_time = iteration_time_ms;
1927 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
1929 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
1930 cd->password_verify = password_verify ? 1 : 0;
1933 int crypt_memory_lock(struct crypt_device *cd, int lock)
1935 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
1939 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
1943 if (!cd && dm_init(NULL, 1) < 0)
1944 return CRYPT_INVALID;
1946 r = dm_status_device(name);
1951 if (r < 0 && r != -ENODEV)
1952 return CRYPT_INVALID;
1955 return CRYPT_ACTIVE;
1960 return CRYPT_INACTIVE;
1963 static void hexprintICB(struct crypt_device *cd, char *d, int n)
1966 for(i = 0; i < n; i++)
1967 log_std(cd, "%02hhx ", (char)d[i]);
1970 int crypt_dump(struct crypt_device *cd)
1973 if (!isLUKS(cd->type)) { //FIXME
1974 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1978 log_std(cd, "LUKS header information for %s\n\n", cd->device);
1979 log_std(cd, "Version: \t%d\n", cd->hdr.version);
1980 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
1981 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
1982 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec);
1983 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
1984 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
1985 log_std(cd, "MK digest: \t");
1986 hexprintICB(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE);
1988 log_std(cd, "MK salt: \t");
1989 hexprintICB(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2);
1990 log_std(cd, "\n \t");
1991 hexprintICB(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
1993 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
1994 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
1995 for(i = 0; i < LUKS_NUMKEYS; i++) {
1996 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
1997 log_std(cd, "Key Slot %d: ENABLED\n",i);
1998 log_std(cd, "\tIterations: \t%d\n",
1999 cd->hdr.keyblock[i].passwordIterations);
2000 log_std(cd, "\tSalt: \t");
2001 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt,
2003 log_std(cd, "\n\t \t");
2004 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt +
2005 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
2008 log_std(cd, "\tKey material offset:\t%d\n",
2009 cd->hdr.keyblock[i].keyMaterialOffset);
2010 log_std(cd, "\tAF stripes: \t%d\n",
2011 cd->hdr.keyblock[i].stripes);
2014 log_std(cd, "Key Slot %d: DISABLED\n", i);
2019 const char *crypt_get_cipher(struct crypt_device *cd)
2021 if (isPLAIN(cd->type))
2022 return cd->plain_cipher;
2024 if (isLUKS(cd->type))
2025 return cd->hdr.cipherName;
2030 const char *crypt_get_cipher_mode(struct crypt_device *cd)
2032 if (isPLAIN(cd->type))
2033 return cd->plain_cipher_mode;
2035 if (isLUKS(cd->type))
2036 return cd->hdr.cipherMode;
2041 const char *crypt_get_uuid(struct crypt_device *cd)
2043 if (isLUKS(cd->type))
2044 return cd->hdr.uuid;
2049 int crypt_get_volume_key_size(struct crypt_device *cd)
2051 if (isPLAIN(cd->type))
2052 return cd->volume_key->keyLength;
2054 if (isLUKS(cd->type))
2055 return cd->hdr.keyBytes;
2060 uint64_t crypt_get_data_offset(struct crypt_device *cd)
2062 if (isPLAIN(cd->type))
2063 return cd->plain_hdr.offset;
2065 if (isLUKS(cd->type))
2066 return cd->hdr.payloadOffset;
2071 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
2073 if (!isLUKS(cd->type)) {
2074 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2075 return CRYPT_SLOT_INVALID;
2078 return LUKS_keyslot_info(&cd->hdr, keyslot);