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 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 luks_masterkey *mk;
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 LUKS_dealloc_masterkey(mk);
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 luks_masterkey *mk;
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, &mk, cd);
244 LUKS_dealloc_masterkey(mk);
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;
358 ci = crypt_status(cd, name);
359 if (ci == CRYPT_INVALID)
362 if (reload && ci < CRYPT_ACTIVE)
365 if (!reload && ci >= CRYPT_ACTIVE) {
366 log_err(cd, _("Device %s already exists.\n"), name);
370 if (key_size < 0 || key_size > 1024) {
371 log_err(cd, _("Invalid key size %d.\n"), key_size);
375 r = device_check_and_adjust(cd, cd->device, &size, &offset, &read_only);
379 if (cipher_mode && asprintf(&dm_cipher, "%s-%s", cipher, cipher_mode) < 0)
382 processed_key = process_key(cd, hash, key_file, key_size, key, keyLen);
386 r = dm_create_device(name, cd->device, dm_cipher ?: cipher, cd->type, uuid, size, skip, offset,
387 key_size, processed_key, read_only, reload);
390 safe_free(processed_key);
394 static int open_from_hdr_and_mk(struct crypt_device *cd,
395 struct luks_masterkey *mk,
399 uint64_t size, offset;
401 int read_only, no_uuid, r;
404 offset = crypt_get_data_offset(cd);
405 read_only = flags & CRYPT_ACTIVATE_READONLY;
406 no_uuid = flags & CRYPT_ACTIVATE_NO_UUID;
408 r = device_check_and_adjust(cd, cd->device, &size, &offset, &read_only);
412 if (asprintf(&cipher, "%s-%s", crypt_get_cipher(cd),
413 crypt_get_cipher_mode(cd)) < 0)
416 r = dm_create_device(name, cd->device, cipher, cd->type,
417 no_uuid ? NULL : crypt_get_uuid(cd),
418 size, 0, offset, mk->keyLength, mk->key,
424 static void log_wrapper(int level, const char *msg, void *usrptr)
426 void (*xlog)(int level, char *msg) = usrptr;
427 xlog(level, (char *)msg);
430 static int yesDialog_wrapper(const char *msg, void *usrptr)
432 int (*xyesDialog)(char *msg) = usrptr;
433 return xyesDialog((char*)msg);
436 int crypt_confirm(struct crypt_device *cd, const char *msg)
438 if (!cd || !cd->confirm)
441 return cd->confirm(msg, cd->confirm_usrptr);
444 static void key_from_terminal(struct crypt_device *cd, char *msg, char **key,
445 unsigned int *key_len, int force_verify)
450 *key = safe_alloc(MAX_TTY_PASSWORD_LEN);
453 r = cd->password(msg, *key, (size_t)key_len, cd->password_usrptr);
460 if (force_verify || cd->password_verify)
461 flags |= CRYPT_FLAG_VERIFY_IF_POSSIBLE;
462 get_key(msg, key, key_len, 0, NULL, cd->timeout, flags, cd);
466 static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslot,
467 struct luks_masterkey **mk)
469 char *prompt = NULL, *passphrase_read = NULL;
470 unsigned int passphrase_size_read;
471 int r = -EINVAL, tries = cd->tries;
473 if(asprintf(&prompt, _("Enter passphrase for %s: "), cd->device) < 0)
479 LUKS_dealloc_masterkey(*mk);
482 key_from_terminal(cd, prompt, &passphrase_read,
483 &passphrase_size_read, 0);
484 if(!passphrase_read) {
489 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
490 passphrase_size_read, &cd->hdr, mk, cd);
491 safe_free(passphrase_read);
492 passphrase_read = NULL;
493 } while (r == -EPERM && (--tries > 0));
496 LUKS_dealloc_masterkey(*mk);
505 static void key_from_file(struct crypt_device *cd, char *msg,
506 char **key, unsigned int *key_len,
507 const char *key_file, size_t key_size)
509 get_key(msg, key, key_len, key_size, key_file, cd->timeout, 0, cd);
512 static int _crypt_init(struct crypt_device **cd,
514 struct crypt_options *options,
515 int load, int need_dm)
519 /* if it is plain device and mapping table is being reloaded
520 initialize it by name*/
521 init_by_name = (type && !strcmp(type, CRYPT_PLAIN) && load);
523 /* Some of old API calls do not require DM in kernel,
524 fake initialisation by initialise it with kernel_check disabled */
526 (void)dm_init(NULL, 0);
528 r = crypt_init_by_name(cd, options->name);
530 r = crypt_init(cd, options->device);
537 crypt_set_log_callback(*cd, log_wrapper, options->icb->log);
538 crypt_set_confirm_callback(*cd, yesDialog_wrapper, options->icb->yesDialog);
540 crypt_set_timeout(*cd, options->timeout);
541 crypt_set_password_retry(*cd, options->tries);
542 crypt_set_iterarion_time(*cd, options->iteration_time ?: 1000);
543 crypt_set_password_verify(*cd, options->flags & CRYPT_FLAG_VERIFY);
545 if (load && !init_by_name)
546 r = crypt_load(*cd, type, NULL);
548 if (!r && type && !(*cd)->type) {
549 (*cd)->type = strdup(type);
560 void crypt_set_log_callback(struct crypt_device *cd,
561 void (*log)(int level, const char *msg, void *usrptr),
568 cd->log_usrptr = usrptr;
572 void crypt_set_confirm_callback(struct crypt_device *cd,
573 int (*confirm)(const char *msg, void *usrptr),
576 cd->confirm = confirm;
577 cd->confirm_usrptr = usrptr;
580 void crypt_set_password_callback(struct crypt_device *cd,
581 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
584 cd->password = password;
585 cd->password_usrptr = usrptr;
588 /* OPTIONS: name, cipher, device, hash, key_file, key_size, key_slot,
589 * offset, size, skip, timeout, tries, passphrase_fd (ignored),
591 static int crypt_create_and_update_device(struct crypt_options *options, int update)
593 struct crypt_device *cd = NULL;
598 r = _crypt_init(&cd, CRYPT_PLAIN, options, 0, 1);
602 get_key(_("Enter passphrase: "), &key, &keyLen, options->key_size,
603 options->key_file, cd->timeout, options->flags, cd);
607 r = create_device_helper(cd, options->name, options->hash,
608 options->cipher, NULL, options->key_file, key, keyLen,
609 options->key_size, options->size, options->skip,
610 options->offset, NULL, options->flags & CRYPT_FLAG_READONLY,
611 options->flags, update);
618 int crypt_create_device(struct crypt_options *options)
620 return crypt_create_and_update_device(options, 0);
623 int crypt_update_device(struct crypt_options *options)
625 return crypt_create_and_update_device(options, 1);
628 /* OPTIONS: name, size, icb */
629 int crypt_resize_device(struct crypt_options *options)
631 struct crypt_device *cd = NULL;
632 char *device = NULL, *cipher = NULL, *uuid = NULL, *key = NULL;
634 uint64_t size, skip, offset;
635 int key_size, read_only, r;
637 log_dbg("Resizing device %s to %" PRIu64 " sectors.", options->name, options->size);
639 if (dm_init(NULL, 1) < 0)
642 r = dm_query_device(options->name, &device, &size, &skip, &offset,
643 &cipher, &key_size, &key, &read_only, NULL, &uuid);
645 log_err(NULL, _("Device %s is not active.\n"), options->name);
649 /* Try to determine type of device from UUID */
652 if (!strncmp(uuid, CRYPT_PLAIN, strlen(CRYPT_PLAIN))) {
656 } else if (!strncmp(uuid, CRYPT_LUKS1, strlen(CRYPT_LUKS1)))
660 if (!options->device)
661 options->device = device;
663 r = _crypt_init(&cd, type, options, 1, 1);
667 size = options->size;
668 r = device_check_and_adjust(cd, device, &size, &offset, &read_only);
672 r = dm_create_device(options->name, device, cipher, type,
673 crypt_get_uuid(cd), size, skip, offset,
674 key_size, key, read_only, 1);
678 if (options->device == device)
679 options->device = NULL;
687 /* OPTIONS: name, icb */
688 int crypt_query_device(struct crypt_options *options)
692 log_dbg("Query device %s.", options->name);
694 if (dm_init(NULL, 1) < 0)
697 r = dm_status_device(options->name);
703 r = dm_query_device(options->name, (char **)&options->device, &options->size,
704 &options->skip, &options->offset, (char **)&options->cipher,
705 &options->key_size, NULL, &read_only, NULL, NULL);
712 options->flags |= CRYPT_FLAG_READONLY;
714 options->flags |= CRYPT_FLAG_FREE_DEVICE;
715 options->flags |= CRYPT_FLAG_FREE_CIPHER;
720 /* OPTIONS: name, icb */
721 int crypt_remove_device(struct crypt_options *options)
723 struct crypt_device *cd = NULL;
726 r = crypt_init_by_name(&cd, options->name);
728 r = crypt_deactivate(cd, options->name);
735 /* OPTIONS: device, cipher, hash, align_payload, key_size (master key), key_slot
736 * new_key_file, iteration_time, timeout, flags, icb */
737 int crypt_luksFormat(struct crypt_options *options)
739 char cipherName[LUKS_CIPHERNAME_L];
740 char cipherMode[LUKS_CIPHERMODE_L];
742 unsigned int passwordLen;
743 struct crypt_device *cd = NULL;
744 struct crypt_params_luks1 cp = {
745 .hash = options->hash,
746 .data_alignment = options->align_payload
750 r = parse_into_name_and_mode(options->cipher, cipherName, cipherMode);
752 log_err(cd, _("No known cipher specification pattern detected.\n"));
756 if ((r = _crypt_init(&cd, CRYPT_LUKS1, options, 0, 1)))
759 if (options->key_slot >= LUKS_NUMKEYS && options->key_slot != CRYPT_ANY_SLOT) {
760 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
761 options->key_slot, LUKS_NUMKEYS - 1);
766 get_key(_("Enter LUKS passphrase: "), &password, &passwordLen, 0,
767 options->new_key_file, options->timeout, options->flags, cd);
774 r = crypt_format(cd, CRYPT_LUKS1, cipherName, cipherMode,
775 NULL, NULL, options->key_size, &cp);
779 /* Add keyslot using internally stored volume key generated during format */
780 r = crypt_keyslot_add_by_volume_key(cd, options->key_slot, NULL, 0,
781 password, passwordLen);
785 return (r < 0) ? r : 0;
788 /* OPTIONS: name, device, key_size, key_file, timeout, tries, flags, icb */
789 int crypt_luksOpen(struct crypt_options *options)
791 struct crypt_device *cd = NULL;
798 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
802 if (options->flags & CRYPT_FLAG_READONLY)
803 flags |= CRYPT_ACTIVATE_READONLY;
805 if (options->flags & CRYPT_FLAG_NON_EXCLUSIVE_ACCESS)
806 flags |= CRYPT_ACTIVATE_NO_UUID;
808 if (options->key_file)
809 r = crypt_activate_by_keyfile(cd, options->name,
810 CRYPT_ANY_SLOT, options->key_file, options->key_size,
813 r = crypt_activate_by_passphrase(cd, options->name,
814 CRYPT_ANY_SLOT, options->passphrase,
815 options->passphrase ? strlen(options->passphrase) : 0,
819 return (r < 0) ? r : 0;
822 /* OPTIONS: device, keys_slot, key_file, timeout, flags, icb */
823 int crypt_luksKillSlot(struct crypt_options *options)
825 struct crypt_device *cd = NULL;
828 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
832 r = luks_remove_helper(cd, options->key_slot, options->key_file, NULL,
833 options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
836 return (r < 0) ? r : 0;
839 /* OPTIONS: device, new_key_file, key_file, timeout, flags, icb */
840 int crypt_luksRemoveKey(struct crypt_options *options)
842 struct crypt_device *cd = NULL;
845 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
849 r = luks_remove_helper(cd, CRYPT_ANY_SLOT, options->key_file, options->new_key_file,
850 options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
853 return (r < 0) ? r : 0;
857 /* OPTIONS: device, new_key_file, key_file, key_slot, flags,
858 iteration_time, timeout, icb */
859 int crypt_luksAddKey(struct crypt_options *options)
861 struct crypt_device *cd = NULL;
864 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
868 if (options->key_file || options->new_key_file)
869 r = crypt_keyslot_add_by_keyfile(cd, options->key_slot,
870 options->key_file, 0,
871 options->new_key_file, 0);
873 r = crypt_keyslot_add_by_passphrase(cd, options->key_slot,
877 return (r < 0) ? r : 0;
880 /* OPTIONS: device, icb */
881 int crypt_luksUUID(struct crypt_options *options)
883 struct crypt_device *cd = NULL;
887 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
891 uuid = (char *)crypt_get_uuid(cd);
892 log_std(cd, uuid ?: "");
898 /* OPTIONS: device, icb */
899 int crypt_isLuks(struct crypt_options *options)
901 struct crypt_device *cd = NULL;
904 log_dbg("Check device %s for LUKS header.", options->device);
907 log_err(cd, _("Cannot initialize crypto backend.\n"));
911 r = crypt_init(&cd, options->device);
915 /* Do print fail here, no need to crypt_load() */
916 r = LUKS_read_phdr(cd->device, &cd->hdr, 0, cd) ? -EINVAL : 0;
922 /* OPTIONS: device, icb */
923 int crypt_luksDump(struct crypt_options *options)
925 struct crypt_device *cd = NULL;
928 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
938 void crypt_get_error(char *buf, size_t size)
940 const char *error = get_error();
942 if (!buf || size < 1)
945 strncpy(buf, error, size - 1);
946 buf[size - 1] = '\0';
952 void crypt_put_options(struct crypt_options *options)
954 if (options->flags & CRYPT_FLAG_FREE_DEVICE) {
955 free((char *)options->device);
956 options->device = NULL;
957 options->flags &= ~CRYPT_FLAG_FREE_DEVICE;
959 if (options->flags & CRYPT_FLAG_FREE_CIPHER) {
960 free((char *)options->cipher);
961 options->cipher = NULL;
962 options->flags &= ~CRYPT_FLAG_FREE_CIPHER;
966 const char *crypt_get_dir(void)
971 /////////////////////////////////
976 int crypt_init(struct crypt_device **cd, const char *device)
978 struct crypt_device *h = NULL;
983 log_dbg("Allocating crypt device %s context.", device);
985 if (device && !device_ready(NULL, device, O_RDONLY))
988 if (!(h = malloc(sizeof(struct crypt_device))))
991 memset(h, 0, sizeof(*h));
994 h->device = strdup(device);
1002 if (dm_init(h, 1) < 0) {
1007 h->iteration_time = 1000;
1008 h->password_verify = 0;
1014 int crypt_init_by_name(struct crypt_device **cd, const char *name)
1016 crypt_status_info ci;
1017 char *device = NULL;
1020 log_dbg("Allocating crypt device context by device %s.", name);
1022 ci = crypt_status(NULL, name);
1023 if (ci == CRYPT_INVALID)
1026 if (ci < CRYPT_ACTIVE) {
1027 log_err(NULL, _("Device %s is not active.\n"), name);
1031 r = dm_query_device(name, &device, NULL, NULL, NULL,
1032 NULL, NULL, NULL, NULL, NULL, NULL);
1034 /* Underlying device disappeared but mapping still active */
1035 if (r >= 0 && !device)
1036 log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
1040 r = crypt_init(cd, device);
1046 static int _crypt_format_plain(struct crypt_device *cd,
1048 const char *cipher_mode,
1050 struct crypt_params_plain *params)
1052 if (!cipher || !cipher_mode) {
1053 log_err(cd, _("Invalid plain crypt parameters.\n"));
1057 if (cd->volume_key->keyLength > 1024) {
1058 log_err(cd, _("Invalid key size.\n"));
1062 cd->plain_cipher = strdup(cipher);
1063 cd->plain_cipher_mode = strdup(cipher_mode);
1066 cd->plain_uuid = strdup(uuid);
1068 if (params && params->hash)
1069 cd->plain_hdr.hash = strdup(params->hash);
1071 cd->plain_hdr.offset = params ? params->offset : 0;
1072 cd->plain_hdr.skip = params ? params->skip : 0;
1074 if (!cd->plain_cipher || !cd->plain_cipher_mode)
1080 static int _crypt_format_luks1(struct crypt_device *cd,
1082 const char *cipher_mode,
1084 struct crypt_params_luks1 *params)
1087 unsigned long required_alignment = DEFAULT_ALIGNMENT;
1088 unsigned long alignment_offset = 0;
1091 log_err(cd, _("Can't format LUKS without device.\n"));
1095 if (params && params->data_alignment)
1096 required_alignment = params->data_alignment * SECTOR_SIZE;
1098 get_topology_alignment(cd->device, &required_alignment,
1099 &alignment_offset, DEFAULT_ALIGNMENT);
1101 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
1102 (params && params->hash) ? params->hash : "sha1",
1104 required_alignment / SECTOR_SIZE,
1105 alignment_offset / SECTOR_SIZE,
1106 cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1110 /* Wipe first 8 sectors - fs magic numbers etc. */
1111 r = wipe_device_header(cd->device, 8);
1113 log_err(cd, _("Can't wipe header on device %s.\n"), cd->device);
1117 r = LUKS_write_phdr(cd->device, &cd->hdr, cd);
1122 int crypt_format(struct crypt_device *cd,
1125 const char *cipher_mode,
1127 const char *volume_key,
1128 size_t volume_key_size,
1133 log_dbg("Formatting device %s as type %s.", cd->device ?: "(none)", cd->type ?: "(none)");
1138 /* Some hash functions need initialized gcrypt library */
1139 if (init_crypto()) {
1140 log_err(cd, _("Cannot initialize crypto backend.\n"));
1145 cd->volume_key = LUKS_alloc_masterkey(volume_key_size,
1148 cd->volume_key = LUKS_generate_masterkey(volume_key_size);
1154 r = _crypt_format_plain(cd, cipher, cipher_mode,
1156 else if (isLUKS(type))
1157 r = _crypt_format_luks1(cd, cipher, cipher_mode,
1160 /* FIXME: allow plugins here? */
1161 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
1165 if (!r && !(cd->type = strdup(type)))
1169 LUKS_dealloc_masterkey(cd->volume_key);
1170 cd->volume_key = NULL;
1176 int crypt_load(struct crypt_device *cd,
1177 const char *requested_type,
1180 struct luks_phdr hdr;
1183 log_dbg("Trying to load %s crypt type from device %s.",
1184 requested_type ?: "any", cd->device ?: "(none)");
1189 if (requested_type && !isLUKS(requested_type))
1192 /* Some hash functions need initialized gcrypt library */
1193 if (init_crypto()) {
1194 log_err(cd, _("Cannot initialize crypto backend.\n"));
1198 r = LUKS_read_phdr(cd->device, &hdr, 1, cd);
1201 memcpy(&cd->hdr, &hdr, sizeof(hdr));
1202 cd->type = strdup(requested_type);
1210 int crypt_header_backup(struct crypt_device *cd,
1211 const char *requested_type,
1212 const char *backup_file)
1214 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1217 /* Some hash functions need initialized gcrypt library */
1218 if (init_crypto()) {
1219 log_err(cd, _("Cannot initialize crypto backend.\n"));
1223 log_dbg("Requested header backup of device %s (%s) to "
1224 "file %s.", cd->device, requested_type, backup_file);
1226 return LUKS_hdr_backup(backup_file, cd->device, &cd->hdr, cd);
1229 int crypt_header_restore(struct crypt_device *cd,
1230 const char *requested_type,
1231 const char *backup_file)
1233 if (requested_type && !isLUKS(requested_type))
1236 /* Some hash functions need initialized gcrypt library */
1237 if (init_crypto()) {
1238 log_err(cd, _("Cannot initialize crypto backend.\n"));
1242 log_dbg("Requested header restore to device %s (%s) from "
1243 "file %s.", cd->device, requested_type, backup_file);
1245 return LUKS_hdr_restore(backup_file, cd->device, &cd->hdr, cd);
1248 void crypt_free(struct crypt_device *cd)
1251 log_dbg("Releasing crypt device %s context.", cd->device);
1255 LUKS_dealloc_masterkey(cd->volume_key);
1260 /* used in plain device only */
1261 free((char*)cd->plain_hdr.hash);
1262 free(cd->plain_cipher);
1263 free(cd->plain_cipher_mode);
1264 free(cd->plain_uuid);
1270 int crypt_suspend(struct crypt_device *cd,
1273 crypt_status_info ci;
1274 int r, suspended = 0;
1276 log_dbg("Suspending volume %s.", name);
1278 ci = crypt_status(NULL, name);
1279 if (ci < CRYPT_ACTIVE) {
1280 log_err(cd, _("Volume %s is not active.\n"), name);
1284 if (!cd && dm_init(NULL, 1) < 0)
1287 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1288 NULL, NULL, NULL, NULL, &suspended, NULL);
1293 log_err(cd, _("Volume %s is already suspended.\n"), name);
1298 r = dm_suspend_and_wipe_key(name);
1300 log_err(cd, "Suspend is not supported for device %s.\n", name);
1302 log_err(cd, "Error during suspending device %s.\n", name);
1309 int crypt_resume_by_passphrase(struct crypt_device *cd,
1312 const char *passphrase,
1313 size_t passphrase_size)
1315 struct luks_masterkey *mk = NULL;
1316 int r, suspended = 0;
1318 log_dbg("Resuming volume %s.", name);
1320 if (!isLUKS(cd->type)) {
1321 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1326 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1327 NULL, NULL, NULL, NULL, &suspended, NULL);
1332 log_err(cd, _("Volume %s is not suspended.\n"), name);
1337 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1338 passphrase_size, &cd->hdr, &mk, cd);
1340 r = volume_key_by_terminal_passphrase(cd, keyslot, &mk);
1344 r = dm_resume_and_reinstate_key(name, mk->keyLength, mk->key);
1346 log_err(cd, "Resume is not supported for device %s.\n", name);
1348 log_err(cd, "Error during resuming device %s.\n", name);
1352 LUKS_dealloc_masterkey(mk);
1353 return r < 0 ? r : keyslot;
1356 int crypt_resume_by_keyfile(struct crypt_device *cd,
1359 const char *keyfile,
1360 size_t keyfile_size)
1362 struct luks_masterkey *mk = NULL;
1363 char *passphrase_read = NULL;
1364 unsigned int passphrase_size_read;
1365 int r, suspended = 0;
1367 log_dbg("Resuming volume %s.", name);
1369 if (!isLUKS(cd->type)) {
1370 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1375 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1376 NULL, NULL, NULL, NULL, &suspended, NULL);
1381 log_err(cd, _("Volume %s is not suspended.\n"), name);
1388 key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1389 &passphrase_size_read, keyfile, keyfile_size);
1391 if(!passphrase_read)
1394 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1395 passphrase_size_read, &cd->hdr, &mk, cd);
1396 safe_free(passphrase_read);
1401 r = dm_resume_and_reinstate_key(name, mk->keyLength, mk->key);
1403 log_err(cd, "Error during resuming device %s.\n", name);
1407 LUKS_dealloc_masterkey(mk);
1408 return r < 0 ? r : keyslot;
1411 // slot manipulation
1412 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1413 int keyslot, // -1 any
1414 const char *passphrase, // NULL -> terminal
1415 size_t passphrase_size,
1416 const char *new_passphrase, // NULL -> terminal
1417 size_t new_passphrase_size)
1419 struct luks_masterkey *mk = NULL;
1420 char *password = NULL, *new_password = NULL;
1421 unsigned int passwordLen, new_passwordLen;
1424 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1425 "new passphrase %sprovided.",
1426 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1428 if (!isLUKS(cd->type)) {
1429 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1433 r = keyslot_verify_or_find_empty(cd, &keyslot);
1437 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1438 /* No slots used, try to use pre-generated key in header */
1439 if (cd->volume_key) {
1440 mk = LUKS_alloc_masterkey(cd->volume_key->keyLength, cd->volume_key->key);
1441 r = mk ? 0 : -ENOMEM;
1443 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1446 } else if (passphrase) {
1447 /* Passphrase provided, use it to unlock existing keyslot */
1448 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, passphrase,
1449 passphrase_size, &cd->hdr, &mk, cd);
1451 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1452 key_from_terminal(cd, _("Enter any passphrase: "),
1453 &password, &passwordLen, 0);
1459 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
1460 passwordLen, &cd->hdr, &mk, cd);
1461 safe_free(password);
1467 if (new_passphrase) {
1468 new_password = (char *)new_passphrase;
1469 new_passwordLen = new_passphrase_size;
1471 key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1472 &new_password, &new_passwordLen, 1);
1479 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1480 &cd->hdr, mk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1485 if (!new_passphrase)
1486 safe_free(new_password);
1487 LUKS_dealloc_masterkey(mk);
1488 return r ?: keyslot;
1491 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1493 const char *keyfile,
1494 size_t keyfile_size,
1495 const char *new_keyfile,
1496 size_t new_keyfile_size)
1498 struct luks_masterkey *mk=NULL;
1499 char *password=NULL; unsigned int passwordLen;
1500 char *new_password = NULL; unsigned int new_passwordLen;
1503 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1504 keyfile ?: "[none]", new_keyfile ?: "[none]");
1506 if (!isLUKS(cd->type)) {
1507 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1511 r = keyslot_verify_or_find_empty(cd, &keyslot);
1515 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1516 /* No slots used, try to use pre-generated key in header */
1517 if (cd->volume_key) {
1518 mk = LUKS_alloc_masterkey(cd->volume_key->keyLength, cd->volume_key->key);
1519 r = mk ? 0 : -ENOMEM;
1521 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1525 /* Read password from file of (if NULL) from terminal */
1527 key_from_file(cd, _("Enter any passphrase: "), &password, &passwordLen,
1528 keyfile, keyfile_size);
1530 key_from_terminal(cd, _("Enter any passphrase: "),
1531 &password, &passwordLen, 0);
1536 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password, passwordLen,
1538 safe_free(password);
1545 key_from_file(cd, _("Enter new passphrase for key slot: "),
1546 &new_password, &new_passwordLen, new_keyfile,
1549 key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1550 &new_password, &new_passwordLen, 1);
1557 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1558 &cd->hdr, mk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1560 safe_free(new_password);
1561 LUKS_dealloc_masterkey(mk);
1562 return r < 0 ? r : keyslot;
1565 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1567 const char *volume_key,
1568 size_t volume_key_size,
1569 const char *passphrase,
1570 size_t passphrase_size)
1572 struct luks_masterkey *mk = NULL;
1574 char *new_password = NULL; unsigned int new_passwordLen;
1576 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1578 if (!isLUKS(cd->type)) {
1579 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1584 mk = LUKS_alloc_masterkey(volume_key_size, volume_key);
1585 else if (cd->volume_key)
1586 mk = LUKS_alloc_masterkey(cd->volume_key->keyLength, cd->volume_key->key);
1591 r = LUKS_verify_master_key(&cd->hdr, mk);
1593 log_err(cd, _("Volume key does not match the volume.\n"));
1597 r = keyslot_verify_or_find_empty(cd, &keyslot);
1602 key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1603 &new_password, &new_passwordLen, 1);
1604 passphrase = new_password;
1605 passphrase_size = new_passwordLen;
1608 r = LUKS_set_key(cd->device, keyslot, passphrase, passphrase_size,
1609 &cd->hdr, mk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1612 safe_free(new_password);
1613 LUKS_dealloc_masterkey(mk);
1614 return r ?: keyslot;
1617 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1619 crypt_keyslot_info ki;
1621 log_dbg("Destroying keyslot %d.", keyslot);
1623 if (!isLUKS(cd->type)) {
1624 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1628 ki = crypt_keyslot_status(cd, keyslot);
1629 if (ki == CRYPT_SLOT_INVALID) {
1630 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1634 if (ki == CRYPT_SLOT_INACTIVE) {
1635 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1639 return LUKS_del_key(cd->device, keyslot, &cd->hdr, cd);
1642 // activation/deactivation of device mapping
1643 int crypt_activate_by_passphrase(struct crypt_device *cd,
1646 const char *passphrase,
1647 size_t passphrase_size,
1650 crypt_status_info ci;
1651 struct luks_masterkey *mk = NULL;
1652 char *prompt = NULL;
1655 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1656 name ? "Activating" : "Checking", name ?: "",
1657 keyslot, passphrase ? "" : "[none] ");
1662 /* plain, use hashed passphrase */
1663 if (isPLAIN(cd->type))
1664 return create_device_helper(cd, name, cd->plain_hdr.hash,
1665 cd->plain_cipher, cd->plain_cipher_mode, NULL, passphrase, passphrase_size,
1666 cd->volume_key->keyLength, 0, cd->plain_hdr.skip,
1667 cd->plain_hdr.offset, cd->plain_uuid, flags & CRYPT_ACTIVATE_READONLY, 0, 0);
1670 ci = crypt_status(NULL, name);
1671 if (ci == CRYPT_INVALID)
1673 else if (ci >= CRYPT_ACTIVE) {
1674 log_err(cd, _("Device %s already exists.\n"), name);
1679 if(asprintf(&prompt, _("Enter passphrase for %s: "), cd->device) < 0)
1682 /* provided passphrase, do not retry */
1684 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1685 passphrase_size, &cd->hdr, &mk, cd);
1687 r = volume_key_by_terminal_passphrase(cd, keyslot, &mk);
1692 r = open_from_hdr_and_mk(cd, mk, name, flags);
1695 LUKS_dealloc_masterkey(mk);
1698 return r < 0 ? r : keyslot;
1701 int crypt_activate_by_keyfile(struct crypt_device *cd,
1704 const char *keyfile,
1705 size_t keyfile_size,
1708 crypt_status_info ci;
1709 struct luks_masterkey *mk = NULL;
1710 char *passphrase_read = NULL;
1711 unsigned int passphrase_size_read;
1714 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1715 name, keyslot, keyfile ?: "[none]");
1717 if (!isLUKS(cd->type)) {
1718 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1723 ci = crypt_status(NULL, name);
1724 if (ci == CRYPT_INVALID)
1726 else if (ci >= CRYPT_ACTIVE) {
1727 log_err(cd, _("Device %s already exists.\n"), name);
1735 key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1736 &passphrase_size_read, keyfile, keyfile_size);
1737 if(!passphrase_read)
1740 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1741 passphrase_size_read, &cd->hdr, &mk, cd);
1742 safe_free(passphrase_read);
1747 r = open_from_hdr_and_mk(cd, mk, name, flags);
1750 LUKS_dealloc_masterkey(mk);
1752 return r < 0 ? r : keyslot;
1755 int crypt_activate_by_volume_key(struct crypt_device *cd,
1757 const char *volume_key,
1758 size_t volume_key_size,
1761 crypt_status_info ci;
1762 struct luks_masterkey *mk;
1765 log_dbg("Activating volume %s by volume key.", name);
1767 /* use key directly, no hash */
1768 if (isPLAIN(cd->type))
1769 return create_device_helper(cd, name, NULL,
1770 cd->plain_cipher, cd->plain_cipher_mode, NULL, volume_key, volume_key_size,
1771 cd->volume_key->keyLength, 0, cd->plain_hdr.skip,
1772 cd->plain_hdr.offset, cd->plain_uuid, flags & CRYPT_ACTIVATE_READONLY, 0, 0);
1774 if (!isLUKS(cd->type)) {
1775 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1780 ci = crypt_status(NULL, name);
1781 if (ci == CRYPT_INVALID)
1783 else if (ci >= CRYPT_ACTIVE) {
1784 log_err(cd, _("Device %s already exists.\n"), name);
1789 mk = LUKS_alloc_masterkey(volume_key_size, volume_key);
1792 r = LUKS_verify_master_key(&cd->hdr, mk);
1795 log_err(cd, _("Volume key does not match the volume.\n"));
1798 r = open_from_hdr_and_mk(cd, mk, name, flags);
1800 LUKS_dealloc_masterkey(mk);
1805 int crypt_deactivate(struct crypt_device *cd, const char *name)
1812 log_dbg("Deactivating volume %s.", name);
1814 if (!cd && dm_init(NULL, 1) < 0)
1817 switch (crypt_status(cd, name)) {
1819 r = dm_remove_device(name, 0, 0);
1822 log_err(cd, _("Device %s is busy.\n"), name);
1825 case CRYPT_INACTIVE:
1826 log_err(cd, _("Device %s is not active.\n"), name);
1830 log_err(cd, _("Invalid device %s.\n"), name);
1840 // misc helper functions
1841 int crypt_volume_key_get(struct crypt_device *cd,
1844 size_t *volume_key_size,
1845 const char *passphrase,
1846 size_t passphrase_size)
1848 struct luks_masterkey *mk;
1849 char *processed_key = NULL;
1852 key_len = crypt_get_volume_key_size(cd);
1853 if (key_len > *volume_key_size) {
1854 log_err(cd, _("Volume key buffer too small.\n"));
1858 if (isPLAIN(cd->type) && cd->plain_hdr.hash) {
1859 processed_key = process_key(cd, cd->plain_hdr.hash, NULL, key_len,
1860 passphrase, passphrase_size);
1861 if (!processed_key) {
1862 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
1865 memcpy(volume_key, processed_key, key_len);
1866 *volume_key_size = key_len;
1867 safe_free(processed_key);
1871 if (isLUKS(cd->type)) {
1872 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1873 passphrase_size, &cd->hdr, &mk, cd);
1876 memcpy(volume_key, mk->key, mk->keyLength);
1877 *volume_key_size = mk->keyLength;
1880 LUKS_dealloc_masterkey(mk);
1884 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
1888 int crypt_volume_key_verify(struct crypt_device *cd,
1889 const char *volume_key,
1890 size_t volume_key_size)
1892 struct luks_masterkey *mk;
1895 if (!isLUKS(cd->type)) {
1896 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1900 mk = LUKS_alloc_masterkey(volume_key_size, volume_key);
1904 r = LUKS_verify_master_key(&cd->hdr, mk);
1907 log_err(cd, _("Volume key does not match the volume.\n"));
1909 LUKS_dealloc_masterkey(mk);
1914 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
1916 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
1917 cd->timeout = timeout_sec;
1920 void crypt_set_password_retry(struct crypt_device *cd, int tries)
1922 log_dbg("Password retry count set to %d.", tries);
1926 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
1928 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
1929 cd->iteration_time = iteration_time_ms;
1932 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
1934 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
1935 cd->password_verify = password_verify ? 1 : 0;
1938 int crypt_memory_lock(struct crypt_device *cd, int lock)
1940 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
1944 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
1948 if (!cd && dm_init(NULL, 1) < 0)
1949 return CRYPT_INVALID;
1951 r = dm_status_device(name);
1956 if (r < 0 && r != -ENODEV)
1957 return CRYPT_INVALID;
1960 return CRYPT_ACTIVE;
1965 return CRYPT_INACTIVE;
1968 static void hexprintICB(struct crypt_device *cd, char *d, int n)
1971 for(i = 0; i < n; i++)
1972 log_std(cd, "%02hhx ", (char)d[i]);
1975 int crypt_dump(struct crypt_device *cd)
1978 if (!isLUKS(cd->type)) { //FIXME
1979 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1983 log_std(cd, "LUKS header information for %s\n\n", cd->device);
1984 log_std(cd, "Version: \t%d\n", cd->hdr.version);
1985 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
1986 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
1987 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec);
1988 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
1989 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
1990 log_std(cd, "MK digest: \t");
1991 hexprintICB(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE);
1993 log_std(cd, "MK salt: \t");
1994 hexprintICB(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2);
1995 log_std(cd, "\n \t");
1996 hexprintICB(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
1998 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
1999 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
2000 for(i = 0; i < LUKS_NUMKEYS; i++) {
2001 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
2002 log_std(cd, "Key Slot %d: ENABLED\n",i);
2003 log_std(cd, "\tIterations: \t%d\n",
2004 cd->hdr.keyblock[i].passwordIterations);
2005 log_std(cd, "\tSalt: \t");
2006 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt,
2008 log_std(cd, "\n\t \t");
2009 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt +
2010 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
2013 log_std(cd, "\tKey material offset:\t%d\n",
2014 cd->hdr.keyblock[i].keyMaterialOffset);
2015 log_std(cd, "\tAF stripes: \t%d\n",
2016 cd->hdr.keyblock[i].stripes);
2019 log_std(cd, "Key Slot %d: DISABLED\n", i);
2024 const char *crypt_get_cipher(struct crypt_device *cd)
2026 if (isPLAIN(cd->type))
2027 return cd->plain_cipher;
2029 if (isLUKS(cd->type))
2030 return cd->hdr.cipherName;
2035 const char *crypt_get_cipher_mode(struct crypt_device *cd)
2037 if (isPLAIN(cd->type))
2038 return cd->plain_cipher_mode;
2040 if (isLUKS(cd->type))
2041 return cd->hdr.cipherMode;
2046 const char *crypt_get_uuid(struct crypt_device *cd)
2048 if (isLUKS(cd->type))
2049 return cd->hdr.uuid;
2054 int crypt_get_volume_key_size(struct crypt_device *cd)
2056 if (isPLAIN(cd->type))
2057 return cd->volume_key->keyLength;
2059 if (isLUKS(cd->type))
2060 return cd->hdr.keyBytes;
2065 uint64_t crypt_get_data_offset(struct crypt_device *cd)
2067 if (isPLAIN(cd->type))
2068 return cd->plain_hdr.offset;
2070 if (isLUKS(cd->type))
2071 return cd->hdr.payloadOffset;
2076 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
2078 if (!isLUKS(cd->type)) {
2079 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2080 return CRYPT_SLOT_INVALID;
2083 return LUKS_keyslot_info(&cd->hdr, keyslot);