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_std(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 (get_device_infos(device, &infos, cd) < 0) {
258 log_err(cd, _("Cannot get info about device %s.\n"), device);
265 log_err(cd, _("Device %s has zero size.\n"), device);
268 if (*size < *offset) {
269 log_err(cd, _("Device %s is too small.\n"), device);
278 log_dbg("Calculated device size is %" PRIu64 " sectors (%s), offset %" PRIu64 ".",
279 *size, *read_only ? "RO" : "RW", *offset);
283 static int luks_remove_helper(struct crypt_device *cd,
285 const char *other_key_file,
286 const char *key_file,
289 crypt_keyslot_info ki;
292 if (key_slot == CRYPT_ANY_SLOT) {
293 key_slot = find_keyslot_by_passphrase(cd, key_file, 0,
294 _("Enter LUKS passphrase to be deleted: "));
300 log_std(cd, _("key slot %d selected for deletion.\n"), key_slot);
303 ki = crypt_keyslot_status(cd, key_slot);
304 if (ki == CRYPT_SLOT_INVALID) {
305 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
306 key_slot, LUKS_NUMKEYS - 1);
310 if (ki <= CRYPT_SLOT_INACTIVE) {
311 log_err(cd, _("Key %d not active. Can't wipe.\n"), key_slot);
316 if (ki == CRYPT_SLOT_ACTIVE_LAST && cd->confirm &&
317 !(cd->confirm(_("This is the last keyslot."
318 " Device will become unusable after purging this key."),
319 cd->confirm_usrptr))) {
325 r = verify_other_keyslot(cd, other_key_file, 0, key_slot);
330 r = crypt_keyslot_destroy(cd, key_slot);
332 return (r < 0) ? r : 0;
335 static int create_device_helper(struct crypt_device *cd,
339 const char *cipher_mode,
340 const char *key_file,
352 crypt_status_info ci;
353 char *dm_cipher = NULL;
354 char *processed_key = NULL;
357 ci = crypt_status(cd, name);
358 if (ci == CRYPT_INVALID)
361 if (reload && ci < CRYPT_ACTIVE)
364 if (!reload && ci >= CRYPT_ACTIVE) {
365 log_err(cd, _("Device %s already exists.\n"), name);
369 if (key_size < 0 || key_size > 1024) {
370 log_err(cd, _("Invalid key size %d.\n"), key_size);
374 r = device_check_and_adjust(cd, cd->device, &size, &offset, &read_only);
378 if (cipher_mode && asprintf(&dm_cipher, "%s-%s", cipher, cipher_mode) < 0)
381 processed_key = process_key(cd, hash, key_file, key_size, key, keyLen);
385 r = dm_create_device(name, cd->device, dm_cipher ?: cipher, cd->type, uuid, size, skip, offset,
386 key_size, processed_key, read_only, reload);
389 safe_free(processed_key);
393 static int open_from_hdr_and_mk(struct crypt_device *cd,
394 struct luks_masterkey *mk,
398 uint64_t size, offset;
400 int read_only, no_uuid, r;
403 offset = crypt_get_data_offset(cd);
404 read_only = flags & CRYPT_ACTIVATE_READONLY;
405 no_uuid = flags & CRYPT_ACTIVATE_NO_UUID;
407 r = device_check_and_adjust(cd, cd->device, &size, &offset, &read_only);
411 if (asprintf(&cipher, "%s-%s", crypt_get_cipher(cd),
412 crypt_get_cipher_mode(cd)) < 0)
415 r = dm_create_device(name, cd->device, cipher, cd->type,
416 no_uuid ? NULL : crypt_get_uuid(cd),
417 size, 0, offset, mk->keyLength, mk->key,
423 static void log_wrapper(int level, const char *msg, void *usrptr)
425 void (*xlog)(int level, char *msg) = usrptr;
426 xlog(level, (char *)msg);
429 static int yesDialog_wrapper(const char *msg, void *usrptr)
431 int (*xyesDialog)(char *msg) = usrptr;
432 return xyesDialog((char*)msg);
435 int crypt_confirm(struct crypt_device *cd, const char *msg)
437 if (!cd || !cd->confirm)
440 return cd->confirm(msg, cd->confirm_usrptr);
443 static void key_from_terminal(struct crypt_device *cd, char *msg, char **key,
444 unsigned int *key_len, int force_verify)
449 *key = safe_alloc(MAX_TTY_PASSWORD_LEN);
452 r = cd->password(msg, *key, (size_t)key_len, cd->password_usrptr);
459 if (force_verify || cd->password_verify)
460 flags |= CRYPT_FLAG_VERIFY_IF_POSSIBLE;
461 get_key(msg, key, key_len, 0, NULL, cd->timeout, flags, cd);
465 static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslot,
466 struct luks_masterkey **mk)
468 char *prompt = NULL, *passphrase_read = NULL;
469 unsigned int passphrase_size_read;
470 int r = -EINVAL, tries = cd->tries;
472 if(asprintf(&prompt, _("Enter passphrase for %s: "), cd->device) < 0)
478 LUKS_dealloc_masterkey(*mk);
481 key_from_terminal(cd, prompt, &passphrase_read,
482 &passphrase_size_read, 0);
483 if(!passphrase_read) {
488 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
489 passphrase_size_read, &cd->hdr, mk, cd);
490 safe_free(passphrase_read);
491 passphrase_read = NULL;
492 } while (r == -EPERM && (--tries > 0));
495 LUKS_dealloc_masterkey(*mk);
504 static void key_from_file(struct crypt_device *cd, char *msg,
505 char **key, unsigned int *key_len,
506 const char *key_file, size_t key_size)
508 get_key(msg, key, key_len, key_size, key_file, cd->timeout, 0, cd);
511 static int _crypt_init(struct crypt_device **cd,
513 struct crypt_options *options,
514 int load, int need_dm)
518 /* if it is plain device and mapping table is being reloaded
519 initialize it by name*/
520 init_by_name = (type && !strcmp(type, CRYPT_PLAIN) && load);
522 /* Some of old API calls do not require DM in kernel,
523 fake initialisation by initialise it with kernel_check disabled */
525 (void)dm_init(NULL, 0);
527 r = crypt_init_by_name(cd, options->name);
529 r = crypt_init(cd, options->device);
536 crypt_set_log_callback(*cd, log_wrapper, options->icb->log);
537 crypt_set_confirm_callback(*cd, yesDialog_wrapper, options->icb->yesDialog);
539 crypt_set_timeout(*cd, options->timeout);
540 crypt_set_password_retry(*cd, options->tries);
541 crypt_set_iterarion_time(*cd, options->iteration_time ?: 1000);
542 crypt_set_password_verify(*cd, options->flags & CRYPT_FLAG_VERIFY);
544 if (load && !init_by_name)
545 r = crypt_load(*cd, type, NULL);
547 if (!r && type && !(*cd)->type) {
548 (*cd)->type = strdup(type);
559 void crypt_set_log_callback(struct crypt_device *cd,
560 void (*log)(int level, const char *msg, void *usrptr),
567 cd->log_usrptr = usrptr;
571 void crypt_set_confirm_callback(struct crypt_device *cd,
572 int (*confirm)(const char *msg, void *usrptr),
575 cd->confirm = confirm;
576 cd->confirm_usrptr = usrptr;
579 void crypt_set_password_callback(struct crypt_device *cd,
580 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
583 cd->password = password;
584 cd->password_usrptr = usrptr;
587 /* OPTIONS: name, cipher, device, hash, key_file, key_size, key_slot,
588 * offset, size, skip, timeout, tries, passphrase_fd (ignored),
590 int crypt_create_device(struct crypt_options *options)
592 struct crypt_device *cd = NULL;
597 r = _crypt_init(&cd, CRYPT_PLAIN, options, 0, 1);
601 get_key(_("Enter passphrase: "), &key, &keyLen, options->key_size,
602 options->key_file, cd->timeout, options->flags, cd);
606 r = create_device_helper(cd, options->name, options->hash,
607 options->cipher, NULL, options->key_file, key, keyLen,
608 options->key_size, options->size, options->skip,
609 options->offset, NULL, options->flags & CRYPT_FLAG_READONLY,
617 /* OPTIONS: same as create above */
618 int crypt_update_device(struct crypt_options *options)
620 struct crypt_device *cd = NULL;
625 r = _crypt_init(&cd, CRYPT_PLAIN, options, 1, 1);
629 get_key(_("Enter passphrase: "), &key, &keyLen, options->key_size,
630 options->key_file, cd->timeout, options->flags, cd);
634 r = create_device_helper(cd, options->name, options->hash,
635 options->cipher, NULL, options->key_file, key, keyLen,
636 options->key_size, options->size, options->skip,
637 options->offset, NULL, options->flags & CRYPT_FLAG_READONLY,
645 /* OPTIONS: name, size, icb */
646 int crypt_resize_device(struct crypt_options *options)
648 struct crypt_device *cd = NULL;
649 char *device = NULL, *cipher = NULL, *uuid = NULL, *key = NULL;
651 uint64_t size, skip, offset;
652 int key_size, read_only, r;
654 log_dbg("Resizing device %s to %" PRIu64 " sectors.", options->name, options->size);
656 if (dm_init(NULL, 1) < 0)
659 r = dm_query_device(options->name, &device, &size, &skip, &offset,
660 &cipher, &key_size, &key, &read_only, NULL, &uuid);
662 log_err(NULL, _("Device %s is not active.\n"), options->name);
666 /* Try to determine type of device from UUID */
669 if (!strncmp(uuid, CRYPT_PLAIN, strlen(CRYPT_PLAIN))) {
673 } else if (!strncmp(uuid, CRYPT_LUKS1, strlen(CRYPT_LUKS1)))
677 if (!options->device)
678 options->device = device;
680 r = _crypt_init(&cd, type, options, 1, 1);
684 size = options->size;
685 r = device_check_and_adjust(cd, device, &size, &offset, &read_only);
689 r = dm_create_device(options->name, device, cipher, type,
690 crypt_get_uuid(cd), size, skip, offset,
691 key_size, key, read_only, 1);
695 if (options->device == device)
696 options->device = NULL;
704 /* OPTIONS: name, icb */
705 int crypt_query_device(struct crypt_options *options)
709 log_dbg("Query device %s.", options->name);
711 if (dm_init(NULL, 1) < 0)
714 r = dm_status_device(options->name);
720 r = dm_query_device(options->name, (char **)&options->device, &options->size,
721 &options->skip, &options->offset, (char **)&options->cipher,
722 &options->key_size, NULL, &read_only, NULL, NULL);
729 options->flags |= CRYPT_FLAG_READONLY;
731 options->flags |= CRYPT_FLAG_FREE_DEVICE;
732 options->flags |= CRYPT_FLAG_FREE_CIPHER;
737 /* OPTIONS: name, icb */
738 int crypt_remove_device(struct crypt_options *options)
740 struct crypt_device *cd = NULL;
743 r = crypt_init_by_name(&cd, options->name);
747 r = crypt_deactivate(cd, options->name);
754 /* OPTIONS: device, cipher, hash, align_payload, key_size (master key), key_slot
755 * new_key_file, iteration_time, timeout, flags, icb */
756 int crypt_luksFormat(struct crypt_options *options)
758 char cipherName[LUKS_CIPHERNAME_L];
759 char cipherMode[LUKS_CIPHERMODE_L];
761 unsigned int passwordLen;
762 struct crypt_device *cd;
763 struct crypt_params_luks1 cp = {
764 .hash = options->hash,
765 .data_alignment = options->align_payload
769 r = parse_into_name_and_mode(options->cipher, cipherName, cipherMode);
771 log_err(cd, _("No known cipher specification pattern detected.\n"));
775 if ((r = _crypt_init(&cd, CRYPT_LUKS1, options, 0, 1)))
778 if (options->key_slot >= LUKS_NUMKEYS && options->key_slot != CRYPT_ANY_SLOT) {
779 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
780 options->key_slot, LUKS_NUMKEYS - 1);
785 get_key(_("Enter LUKS passphrase: "), &password, &passwordLen, 0,
786 options->new_key_file, options->timeout, options->flags, cd);
793 r = crypt_format(cd, CRYPT_LUKS1, cipherName, cipherMode,
794 NULL, NULL, options->key_size, &cp);
798 /* Add keyslot using internally stored volume key generated during format */
799 r = crypt_keyslot_add_by_volume_key(cd, options->key_slot, NULL, 0,
800 password, passwordLen);
804 return (r < 0) ? r : 0;
807 /* OPTIONS: name, device, key_size, key_file, timeout, tries, flags, icb */
808 int crypt_luksOpen(struct crypt_options *options)
810 struct crypt_device *cd = NULL;
817 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
821 if (options->flags & CRYPT_FLAG_READONLY)
822 flags |= CRYPT_ACTIVATE_READONLY;
824 if (options->flags & CRYPT_FLAG_NON_EXCLUSIVE_ACCESS)
825 flags |= CRYPT_ACTIVATE_NO_UUID;
827 if (options->key_file && strcmp(options->key_file, "-"))
828 r = crypt_activate_by_keyfile(cd, options->name,
829 CRYPT_ANY_SLOT, options->key_file, options->key_size,
832 r = crypt_activate_by_passphrase(cd, options->name,
833 CRYPT_ANY_SLOT, options->passphrase,
834 options->passphrase ? strlen(options->passphrase) : 0,
838 return (r < 0) ? r : 0;
841 /* OPTIONS: device, keys_slot, key_file, timeout, flags, icb */
842 int crypt_luksKillSlot(struct crypt_options *options)
844 struct crypt_device *cd = NULL;
847 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
851 r = luks_remove_helper(cd, options->key_slot, options->key_file, NULL,
852 options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
855 return (r < 0) ? r : 0;
858 /* OPTIONS: device, new_key_file, key_file, timeout, flags, icb */
859 int crypt_luksRemoveKey(struct crypt_options *options)
861 struct crypt_device *cd = NULL;
864 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
868 r = luks_remove_helper(cd, CRYPT_ANY_SLOT, options->key_file, options->new_key_file,
869 options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
872 return (r < 0) ? r : 0;
876 /* OPTIONS: device, new_key_file, key_file, key_slot, flags,
877 iteration_time, timeout, icb */
878 int crypt_luksAddKey(struct crypt_options *options)
880 struct crypt_device *cd = NULL;
883 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
887 if (options->key_file || options->new_key_file)
888 r = crypt_keyslot_add_by_keyfile(cd, options->key_slot,
889 options->key_file, 0,
890 options->new_key_file, 0);
892 r = crypt_keyslot_add_by_passphrase(cd, options->key_slot,
896 return (r < 0) ? r : 0;
899 /* OPTIONS: device, icb */
900 int crypt_luksUUID(struct crypt_options *options)
902 struct crypt_device *cd = NULL;
906 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
910 uuid = (char *)crypt_get_uuid(cd);
911 log_std(cd, uuid ?: "");
917 /* OPTIONS: device, icb */
918 int crypt_isLuks(struct crypt_options *options)
920 struct crypt_device *cd = NULL;
923 log_dbg("Check device %s for LUKS header.", options->device);
925 r = crypt_init(&cd, options->device);
929 /* Do print fail here, no need to crypt_load() */
930 r = LUKS_read_phdr(cd->device, &cd->hdr, 0, cd) ? -EINVAL : 0;
936 /* OPTIONS: device, icb */
937 int crypt_luksDump(struct crypt_options *options)
939 struct crypt_device *cd = NULL;
942 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
952 void crypt_get_error(char *buf, size_t size)
954 const char *error = get_error();
956 if (!buf || size < 1)
959 strncpy(buf, error, size - 1);
960 buf[size - 1] = '\0';
966 void crypt_put_options(struct crypt_options *options)
968 if (options->flags & CRYPT_FLAG_FREE_DEVICE) {
969 free((char *)options->device);
970 options->device = NULL;
971 options->flags &= ~CRYPT_FLAG_FREE_DEVICE;
973 if (options->flags & CRYPT_FLAG_FREE_CIPHER) {
974 free((char *)options->cipher);
975 options->cipher = NULL;
976 options->flags &= ~CRYPT_FLAG_FREE_CIPHER;
980 const char *crypt_get_dir(void)
985 /////////////////////////////////
990 int crypt_init(struct crypt_device **cd, const char *device)
992 struct crypt_device *h = NULL;
997 log_dbg("Allocating crypt device %s context.", device);
999 if (device && !device_ready(NULL, device, O_RDONLY))
1002 if (!(h = malloc(sizeof(struct crypt_device))))
1005 memset(h, 0, sizeof(*h));
1008 h->device = strdup(device);
1016 if (dm_init(h, 1) < 0) {
1021 h->iteration_time = 1000;
1022 h->password_verify = 0;
1028 int crypt_init_by_name(struct crypt_device **cd, const char *name)
1030 crypt_status_info ci;
1031 char *device = NULL;
1034 log_dbg("Allocating crypt device context by device %s.", name);
1036 ci = crypt_status(NULL, name);
1037 if (ci == CRYPT_INVALID)
1040 if (ci < CRYPT_ACTIVE) {
1041 log_err(NULL, _("Device %s is not active.\n"), name);
1045 r = dm_query_device(name, &device, NULL, NULL, NULL,
1046 NULL, NULL, NULL, NULL, NULL, NULL);
1048 r = crypt_init(cd, device);
1054 static int _crypt_format_plain(struct crypt_device *cd,
1056 const char *cipher_mode,
1058 struct crypt_params_plain *params)
1060 if (!cipher || !cipher_mode) {
1061 log_err(cd, _("Invalid plain crypt parameters.\n"));
1065 if (cd->volume_key->keyLength > 1024) {
1066 log_err(cd, _("Invalid key size.\n"));
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 struct crypt_params_luks1 *params)
1095 unsigned long required_alignment = DEFAULT_ALIGNMENT;
1096 unsigned long alignment_offset = 0;
1099 log_err(cd, _("Can't format LUKS without device.\n"));
1103 if (params && params->data_alignment)
1104 required_alignment = params->data_alignment * SECTOR_SIZE;
1106 get_topology_alignment(cd->device, &required_alignment,
1107 &alignment_offset, DEFAULT_ALIGNMENT);
1109 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
1110 (params && params->hash) ? params->hash : "sha1",
1112 required_alignment / SECTOR_SIZE,
1113 alignment_offset / SECTOR_SIZE,
1114 cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1118 /* Wipe first 8 sectors - fs magic numbers etc. */
1119 r = wipe_device_header(cd->device, 8);
1121 log_err(cd, _("Can't wipe header on device %s.\n"), cd->device);
1125 r = LUKS_write_phdr(cd->device, &cd->hdr, cd);
1130 int crypt_format(struct crypt_device *cd,
1133 const char *cipher_mode,
1135 const char *volume_key,
1136 size_t volume_key_size,
1141 log_dbg("Formatting device %s as type %s.", cd->device ?: "(none)", cd->type ?: "(none)");
1146 /* Some hash functions need initialized gcrypt library */
1147 if (init_crypto()) {
1148 log_err(cd, _("Cannot initialize crypto backend.\n"));
1153 cd->volume_key = LUKS_alloc_masterkey(volume_key_size,
1156 cd->volume_key = LUKS_generate_masterkey(volume_key_size);
1162 r = _crypt_format_plain(cd, cipher, cipher_mode,
1164 else if (isLUKS(type))
1165 r = _crypt_format_luks1(cd, cipher, cipher_mode,
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 LUKS_dealloc_masterkey(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 /* Some hash functions need initialized gcrypt library */
1201 if (init_crypto()) {
1202 log_err(cd, _("Cannot initialize crypto backend.\n"));
1206 r = LUKS_read_phdr(cd->device, &hdr, 1, cd);
1209 memcpy(&cd->hdr, &hdr, sizeof(hdr));
1210 cd->type = strdup(requested_type);
1218 int crypt_header_backup(struct crypt_device *cd,
1219 const char *requested_type,
1220 const char *backup_file)
1222 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1225 /* Some hash functions need initialized gcrypt library */
1226 if (init_crypto()) {
1227 log_err(cd, _("Cannot initialize crypto backend.\n"));
1231 log_dbg("Requested header backup of device %s (%s) to "
1232 "file %s.", cd->device, requested_type, backup_file);
1234 return LUKS_hdr_backup(backup_file, cd->device, &cd->hdr, cd);
1237 int crypt_header_restore(struct crypt_device *cd,
1238 const char *requested_type,
1239 const char *backup_file)
1241 if (requested_type && !isLUKS(requested_type))
1244 /* Some hash functions need initialized gcrypt library */
1245 if (init_crypto()) {
1246 log_err(cd, _("Cannot initialize crypto backend.\n"));
1250 log_dbg("Requested header restore to device %s (%s) from "
1251 "file %s.", cd->device, requested_type, backup_file);
1253 return LUKS_hdr_restore(backup_file, cd->device, &cd->hdr, cd);
1256 void crypt_free(struct crypt_device *cd)
1259 log_dbg("Releasing crypt device %s context.", cd->device);
1263 LUKS_dealloc_masterkey(cd->volume_key);
1268 /* used in plain device only */
1269 free((char*)cd->plain_hdr.hash);
1270 free(cd->plain_cipher);
1271 free(cd->plain_cipher_mode);
1272 free(cd->plain_uuid);
1278 int crypt_suspend(struct crypt_device *cd,
1281 crypt_status_info ci;
1282 int r, suspended = 0;
1284 log_dbg("Suspending volume %s.", name);
1286 ci = crypt_status(NULL, name);
1287 if (ci < CRYPT_ACTIVE) {
1288 log_err(cd, _("Volume %s is not active.\n"), name);
1292 if (!cd && dm_init(NULL, 1) < 0)
1295 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1296 NULL, NULL, NULL, NULL, &suspended, NULL);
1301 log_err(cd, _("Volume %s is already suspended.\n"), name);
1306 r = dm_suspend_and_wipe_key(name);
1308 log_err(cd, "Suspend is not supported for device %s.\n", name);
1310 log_err(cd, "Error during suspending device %s.\n", name);
1317 int crypt_resume_by_passphrase(struct crypt_device *cd,
1320 const char *passphrase,
1321 size_t passphrase_size)
1323 struct luks_masterkey *mk = NULL;
1324 int r, suspended = 0;
1326 log_dbg("Resuming volume %s.", name);
1328 if (!isLUKS(cd->type)) {
1329 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1334 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1335 NULL, NULL, NULL, NULL, &suspended, NULL);
1340 log_err(cd, _("Volume %s is not suspended.\n"), name);
1345 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1346 passphrase_size, &cd->hdr, &mk, cd);
1348 r = volume_key_by_terminal_passphrase(cd, keyslot, &mk);
1352 r = dm_resume_and_reinstate_key(name, mk->keyLength, mk->key);
1354 log_err(cd, "Resume is not supported for device %s.\n", name);
1356 log_err(cd, "Error during resuming device %s.\n", name);
1360 LUKS_dealloc_masterkey(mk);
1361 return r < 0 ? r : keyslot;
1364 int crypt_resume_by_keyfile(struct crypt_device *cd,
1367 const char *keyfile,
1368 size_t keyfile_size)
1370 struct luks_masterkey *mk = NULL;
1371 char *passphrase_read = NULL;
1372 unsigned int passphrase_size_read;
1373 int r, suspended = 0;
1375 log_dbg("Resuming volume %s.", name);
1377 if (!isLUKS(cd->type)) {
1378 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1383 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1384 NULL, NULL, NULL, NULL, &suspended, NULL);
1389 log_err(cd, _("Volume %s is not suspended.\n"), name);
1396 key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1397 &passphrase_size_read, keyfile, keyfile_size);
1399 if(!passphrase_read)
1402 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1403 passphrase_size_read, &cd->hdr, &mk, cd);
1404 safe_free(passphrase_read);
1409 r = dm_resume_and_reinstate_key(name, mk->keyLength, mk->key);
1411 log_err(cd, "Error during resuming device %s.\n", name);
1415 LUKS_dealloc_masterkey(mk);
1416 return r < 0 ? r : keyslot;
1419 // slot manipulation
1420 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1421 int keyslot, // -1 any
1422 const char *passphrase, // NULL -> terminal
1423 size_t passphrase_size,
1424 const char *new_passphrase, // NULL -> terminal
1425 size_t new_passphrase_size)
1427 struct luks_masterkey *mk = NULL;
1428 char *password = NULL, *new_password = NULL;
1429 unsigned int passwordLen, new_passwordLen;
1432 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1433 "new passphrase %sprovided.",
1434 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1436 if (!isLUKS(cd->type)) {
1437 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1441 r = keyslot_verify_or_find_empty(cd, &keyslot);
1445 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1446 /* No slots used, try to use pre-generated key in header */
1447 if (cd->volume_key) {
1448 mk = LUKS_alloc_masterkey(cd->volume_key->keyLength, cd->volume_key->key);
1449 r = mk ? 0 : -ENOMEM;
1451 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1454 } else if (passphrase) {
1455 /* Passphrase provided, use it to unlock existing keyslot */
1456 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, passphrase,
1457 passphrase_size, &cd->hdr, &mk, cd);
1459 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1460 key_from_terminal(cd, _("Enter any passphrase: "),
1461 &password, &passwordLen, 0);
1467 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
1468 passwordLen, &cd->hdr, &mk, cd);
1469 safe_free(password);
1475 if (new_passphrase) {
1476 new_password = (char *)new_passphrase;
1477 new_passwordLen = new_passphrase_size;
1479 key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1480 &new_password, &new_passwordLen, 1);
1487 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1488 &cd->hdr, mk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1493 if (!new_passphrase)
1494 safe_free(new_password);
1495 LUKS_dealloc_masterkey(mk);
1496 return r ?: keyslot;
1499 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1501 const char *keyfile,
1502 size_t keyfile_size,
1503 const char *new_keyfile,
1504 size_t new_keyfile_size)
1506 struct luks_masterkey *mk=NULL;
1507 char *password=NULL; unsigned int passwordLen;
1508 char *new_password = NULL; unsigned int new_passwordLen;
1511 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1512 keyfile ?: "[none]", new_keyfile ?: "[none]");
1514 if (!isLUKS(cd->type)) {
1515 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1519 r = keyslot_verify_or_find_empty(cd, &keyslot);
1523 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1524 /* No slots used, try to use pre-generated key in header */
1525 if (cd->volume_key) {
1526 mk = LUKS_alloc_masterkey(cd->volume_key->keyLength, cd->volume_key->key);
1527 r = mk ? 0 : -ENOMEM;
1529 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1533 /* Read password from file of (if NULL) from terminal */
1535 key_from_file(cd, _("Enter any passphrase: "), &password, &passwordLen,
1536 keyfile, keyfile_size);
1538 key_from_terminal(cd, _("Enter any passphrase: "),
1539 &password, &passwordLen, 0);
1544 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password, passwordLen,
1546 safe_free(password);
1553 key_from_file(cd, _("Enter new passphrase for key slot: "),
1554 &new_password, &new_passwordLen, new_keyfile,
1557 key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1558 &new_password, &new_passwordLen, 1);
1565 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1566 &cd->hdr, mk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1568 safe_free(new_password);
1569 LUKS_dealloc_masterkey(mk);
1570 return r < 0 ? r : keyslot;
1573 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1575 const char *volume_key,
1576 size_t volume_key_size,
1577 const char *passphrase,
1578 size_t passphrase_size)
1580 struct luks_masterkey *mk = NULL;
1582 char *new_password = NULL; unsigned int new_passwordLen;
1584 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1586 if (!isLUKS(cd->type)) {
1587 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1592 mk = LUKS_alloc_masterkey(volume_key_size, volume_key);
1593 else if (cd->volume_key)
1594 mk = LUKS_alloc_masterkey(cd->volume_key->keyLength, cd->volume_key->key);
1599 r = LUKS_verify_master_key(&cd->hdr, mk);
1601 log_err(cd, _("Volume key does not match the volume.\n"));
1605 r = keyslot_verify_or_find_empty(cd, &keyslot);
1610 key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1611 &new_password, &new_passwordLen, 1);
1612 passphrase = new_password;
1613 passphrase_size = new_passwordLen;
1616 r = LUKS_set_key(cd->device, keyslot, passphrase, passphrase_size,
1617 &cd->hdr, mk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1620 safe_free(new_password);
1621 LUKS_dealloc_masterkey(mk);
1622 return r ?: keyslot;
1625 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1627 crypt_keyslot_info ki;
1629 log_dbg("Destroying keyslot %d.", keyslot);
1631 if (!isLUKS(cd->type)) {
1632 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1636 ki = crypt_keyslot_status(cd, keyslot);
1637 if (ki == CRYPT_SLOT_INVALID) {
1638 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1642 if (ki == CRYPT_SLOT_INACTIVE) {
1643 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1647 return LUKS_del_key(cd->device, keyslot, &cd->hdr, cd);
1650 // activation/deactivation of device mapping
1651 int crypt_activate_by_passphrase(struct crypt_device *cd,
1654 const char *passphrase,
1655 size_t passphrase_size,
1658 crypt_status_info ci;
1659 struct luks_masterkey *mk = NULL;
1660 char *prompt = NULL;
1663 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1664 name ? "Activating" : "Checking", name ?: "",
1665 keyslot, passphrase ? "" : "[none] ");
1670 /* plain, use hashed passphrase */
1671 if (isPLAIN(cd->type))
1672 return create_device_helper(cd, name, cd->plain_hdr.hash,
1673 cd->plain_cipher, cd->plain_cipher_mode, NULL, passphrase, passphrase_size,
1674 cd->volume_key->keyLength, 0, cd->plain_hdr.skip,
1675 cd->plain_hdr.offset, cd->plain_uuid, flags & CRYPT_ACTIVATE_READONLY, 0, 0);
1678 ci = crypt_status(NULL, name);
1679 if (ci == CRYPT_INVALID)
1681 else if (ci >= CRYPT_ACTIVE) {
1682 log_err(cd, _("Device %s already exists.\n"), name);
1687 if(asprintf(&prompt, _("Enter passphrase for %s: "), cd->device) < 0)
1690 /* provided passphrase, do not retry */
1692 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1693 passphrase_size, &cd->hdr, &mk, cd);
1695 r = volume_key_by_terminal_passphrase(cd, keyslot, &mk);
1700 r = open_from_hdr_and_mk(cd, mk, name, flags);
1703 LUKS_dealloc_masterkey(mk);
1706 return r < 0 ? r : keyslot;
1709 int crypt_activate_by_keyfile(struct crypt_device *cd,
1712 const char *keyfile,
1713 size_t keyfile_size,
1716 crypt_status_info ci;
1717 struct luks_masterkey *mk = NULL;
1718 char *passphrase_read = NULL;
1719 unsigned int passphrase_size_read;
1722 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1723 name, keyslot, keyfile ?: "[none]");
1725 if (!isLUKS(cd->type)) {
1726 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1731 ci = crypt_status(NULL, name);
1732 if (ci == CRYPT_INVALID)
1734 else if (ci >= CRYPT_ACTIVE) {
1735 log_err(cd, _("Device %s already exists.\n"), name);
1743 key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1744 &passphrase_size_read, keyfile, keyfile_size);
1745 if(!passphrase_read)
1748 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1749 passphrase_size_read, &cd->hdr, &mk, cd);
1750 safe_free(passphrase_read);
1755 r = open_from_hdr_and_mk(cd, mk, name, flags);
1758 LUKS_dealloc_masterkey(mk);
1760 return r < 0 ? r : keyslot;
1763 int crypt_activate_by_volume_key(struct crypt_device *cd,
1765 const char *volume_key,
1766 size_t volume_key_size,
1769 crypt_status_info ci;
1770 struct luks_masterkey *mk;
1773 log_dbg("Activating volume %s by volume key.", name);
1775 /* use key directly, no hash */
1776 if (isPLAIN(cd->type))
1777 return create_device_helper(cd, name, NULL,
1778 cd->plain_cipher, cd->plain_cipher_mode, NULL, volume_key, volume_key_size,
1779 cd->volume_key->keyLength, 0, cd->plain_hdr.skip,
1780 cd->plain_hdr.offset, cd->plain_uuid, flags & CRYPT_ACTIVATE_READONLY, 0, 0);
1782 if (!isLUKS(cd->type)) {
1783 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1788 ci = crypt_status(NULL, name);
1789 if (ci == CRYPT_INVALID)
1791 else if (ci >= CRYPT_ACTIVE) {
1792 log_err(cd, _("Device %s already exists.\n"), name);
1797 mk = LUKS_alloc_masterkey(volume_key_size, volume_key);
1800 r = LUKS_verify_master_key(&cd->hdr, mk);
1803 log_err(cd, _("Volume key does not match the volume.\n"));
1806 r = open_from_hdr_and_mk(cd, mk, name, flags);
1808 LUKS_dealloc_masterkey(mk);
1813 int crypt_deactivate(struct crypt_device *cd, const char *name)
1820 log_dbg("Deactivating volume %s.", name);
1822 if (!cd && dm_init(NULL, 1) < 0)
1825 switch (crypt_status(cd, name)) {
1827 r = dm_remove_device(name, 0, 0);
1830 log_err(cd, _("Device %s is busy.\n"), name);
1833 case CRYPT_INACTIVE:
1834 log_err(cd, _("Device %s is not active.\n"), name);
1838 log_err(cd, _("Invalid device %s.\n"), name);
1848 // misc helper functions
1849 int crypt_volume_key_get(struct crypt_device *cd,
1852 size_t *volume_key_size,
1853 const char *passphrase,
1854 size_t passphrase_size)
1856 struct luks_masterkey *mk;
1857 char *processed_key = NULL;
1860 key_len = crypt_get_volume_key_size(cd);
1861 if (key_len > *volume_key_size) {
1862 log_err(cd, _("Volume key buffer too small.\n"));
1866 if (isPLAIN(cd->type) && cd->plain_hdr.hash) {
1867 processed_key = process_key(cd, cd->plain_hdr.hash, NULL, key_len,
1868 passphrase, passphrase_size);
1869 if (!processed_key) {
1870 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
1873 memcpy(volume_key, processed_key, key_len);
1874 *volume_key_size = key_len;
1875 safe_free(processed_key);
1879 if (isLUKS(cd->type)) {
1880 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1881 passphrase_size, &cd->hdr, &mk, cd);
1884 memcpy(volume_key, mk->key, mk->keyLength);
1885 *volume_key_size = mk->keyLength;
1888 LUKS_dealloc_masterkey(mk);
1892 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
1896 int crypt_volume_key_verify(struct crypt_device *cd,
1897 const char *volume_key,
1898 size_t volume_key_size)
1900 struct luks_masterkey *mk;
1903 if (!isLUKS(cd->type)) {
1904 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1908 mk = LUKS_alloc_masterkey(volume_key_size, volume_key);
1912 r = LUKS_verify_master_key(&cd->hdr, mk);
1915 log_err(cd, _("Volume key does not match the volume.\n"));
1917 LUKS_dealloc_masterkey(mk);
1922 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
1924 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
1925 cd->timeout = timeout_sec;
1928 void crypt_set_password_retry(struct crypt_device *cd, int tries)
1930 log_dbg("Password retry count set to %d.", tries);
1934 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
1936 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
1937 cd->iteration_time = iteration_time_ms;
1940 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
1942 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
1943 cd->password_verify = password_verify ? 1 : 0;
1946 int crypt_memory_lock(struct crypt_device *cd, int lock)
1948 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
1952 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
1956 if (!cd && dm_init(NULL, 1) < 0)
1957 return CRYPT_INVALID;
1959 r = dm_status_device(name);
1964 if (r < 0 && r != -ENODEV)
1965 return CRYPT_INVALID;
1968 return CRYPT_ACTIVE;
1973 return CRYPT_INACTIVE;
1976 static void hexprintICB(struct crypt_device *cd, char *d, int n)
1979 for(i = 0; i < n; i++)
1980 log_std(cd, "%02hhx ", (char)d[i]);
1983 int crypt_dump(struct crypt_device *cd)
1986 if (!isLUKS(cd->type)) { //FIXME
1987 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1991 log_std(cd, "LUKS header information for %s\n\n", cd->device);
1992 log_std(cd, "Version: \t%d\n", cd->hdr.version);
1993 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
1994 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
1995 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec);
1996 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
1997 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
1998 log_std(cd, "MK digest: \t");
1999 hexprintICB(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE);
2001 log_std(cd, "MK salt: \t");
2002 hexprintICB(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2);
2003 log_std(cd, "\n \t");
2004 hexprintICB(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
2006 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
2007 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
2008 for(i = 0; i < LUKS_NUMKEYS; i++) {
2009 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
2010 log_std(cd, "Key Slot %d: ENABLED\n",i);
2011 log_std(cd, "\tIterations: \t%d\n",
2012 cd->hdr.keyblock[i].passwordIterations);
2013 log_std(cd, "\tSalt: \t");
2014 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt,
2016 log_std(cd, "\n\t \t");
2017 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt +
2018 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
2021 log_std(cd, "\tKey material offset:\t%d\n",
2022 cd->hdr.keyblock[i].keyMaterialOffset);
2023 log_std(cd, "\tAF stripes: \t%d\n",
2024 cd->hdr.keyblock[i].stripes);
2027 log_std(cd, "Key Slot %d: DISABLED\n", i);
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 int crypt_get_volume_key_size(struct crypt_device *cd)
2064 if (isPLAIN(cd->type))
2065 return cd->volume_key->keyLength;
2067 if (isLUKS(cd->type))
2068 return cd->hdr.keyBytes;
2073 uint64_t crypt_get_data_offset(struct crypt_device *cd)
2075 if (isPLAIN(cd->type))
2076 return cd->plain_hdr.offset;
2078 if (isLUKS(cd->type))
2079 return cd->hdr.payloadOffset;
2084 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
2086 if (!isLUKS(cd->type)) {
2087 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2088 return CRYPT_SLOT_INVALID;
2091 return LUKS_keyslot_info(&cd->hdr, keyslot);