8 #include "libcryptsetup.h"
16 struct volume_key *volume_key;
18 uint64_t iteration_time;
23 /* used in CRYPT_LUKS1 */
25 uint64_t PBKDF2_per_sec;
27 /* used in CRYPT_PLAIN */
28 struct crypt_params_plain plain_hdr;
30 char *plain_cipher_mode;
33 /* callbacks definitions */
34 void (*log)(int level, const char *msg, void *usrptr);
36 int (*confirm)(const char *msg, void *usrptr);
38 int (*password)(const char *msg, char *buf, size_t length, void *usrptr);
39 void *password_usrptr;
43 static void (*_default_log)(int level, const char *msg, void *usrptr) = NULL;
44 static int _debug_level = 0;
46 void crypt_set_debug_level(int level)
51 int crypt_get_debug_level()
56 void crypt_log(struct crypt_device *cd, int level, const char *msg)
59 cd->log(level, msg, cd->log_usrptr);
60 else if (_default_log)
61 _default_log(level, msg, NULL);
64 void logger(struct crypt_device *cd, int level, const char *file,
65 int line, const char *format, ...)
70 va_start(argp, format);
72 if (vasprintf(&target, format, argp) > 0) {
74 crypt_log(cd, level, target);
76 } else if (_debug_level)
77 printf("# %s:%d %s\n", file ?: "?", line, target);
79 } else if (_debug_level)
80 printf("# %s\n", target);
89 * Password processing behaviour matrix of process_key
91 * from binary file: check if there is sufficently large key material
92 * interactive & from fd: hash if requested, otherwise crop or pad with '0'
94 static char *process_key(struct crypt_device *cd, const char *hash_name,
95 const char *key_file, size_t key_size,
96 const char *pass, size_t passLen)
98 char *key = crypt_safe_alloc(key_size);
99 memset(key, 0, key_size);
101 /* key is coming from binary file */
102 if (key_file && strcmp(key_file, "-")) {
103 if(passLen < key_size) {
104 log_err(cd, _("Cannot not read %d bytes from key file %s.\n"),
106 crypt_safe_free(key);
109 memcpy(key, pass, key_size);
113 /* key is coming from tty, fd or binary stdin */
115 if (hash(NULL, hash_name, key, key_size, pass, passLen) < 0) {
116 log_err(cd, _("Key processing error (using hash algorithm %s).\n"),
118 crypt_safe_free(key);
121 } else if (passLen > key_size) {
122 memcpy(key, pass, key_size);
124 memcpy(key, pass, passLen);
130 static int isPLAIN(const char *type)
132 return (type && !strcmp(CRYPT_PLAIN, type));
135 static int isLUKS(const char *type)
137 return (type && !strcmp(CRYPT_LUKS1, type));
140 /* keyslot helpers */
141 static int keyslot_verify_or_find_empty(struct crypt_device *cd, int *keyslot)
143 if (*keyslot == CRYPT_ANY_SLOT) {
144 *keyslot = LUKS_keyslot_find_empty(&cd->hdr);
146 log_err(cd, _("All key slots full.\n"));
151 switch (LUKS_keyslot_info(&cd->hdr, *keyslot)) {
152 case CRYPT_SLOT_INVALID:
153 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
154 *keyslot, LUKS_NUMKEYS - 1);
156 case CRYPT_SLOT_INACTIVE:
159 log_err(cd, _("Key slot %d is full, please select another one.\n"),
167 static int verify_other_keyslot(struct crypt_device *cd,
168 const char *key_file,
171 struct volume_key *vk;
172 crypt_keyslot_info ki;
174 char *password = NULL;
175 unsigned int passwordLen;
177 crypt_get_key(_("Enter any remaining LUKS passphrase: "), &password,
178 &passwordLen, 0, key_file, cd->timeout, cd->password_verify, cd);
182 ki = crypt_keyslot_status(cd, keyIndex);
183 if (ki == CRYPT_SLOT_ACTIVE) /* Not last slot */
184 LUKS_keyslot_set(&cd->hdr, keyIndex, 0);
186 openedIndex = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT,
187 password, passwordLen,
190 if (ki == CRYPT_SLOT_ACTIVE)
191 LUKS_keyslot_set(&cd->hdr, keyIndex, 1);
192 crypt_free_volume_key(vk);
193 crypt_safe_free(password);
198 log_verbose(cd, _("Key slot %d verified.\n"), openedIndex);
202 static int find_keyslot_by_passphrase(struct crypt_device *cd,
203 const char *key_file,
206 struct volume_key *vk;
207 char *password = NULL;
208 unsigned int passwordLen;
211 crypt_get_key(message,&password,&passwordLen, 0, key_file,
212 cd->timeout, cd->password_verify, cd);
216 keyIndex = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
217 passwordLen, &cd->hdr, &vk, cd);
218 crypt_free_volume_key(vk);
219 crypt_safe_free(password);
224 static int device_check_and_adjust(struct crypt_device *cd,
226 uint64_t *size, uint64_t *offset,
229 struct device_infos infos;
231 if (!device || get_device_infos(device, &infos, cd) < 0) {
232 log_err(cd, _("Cannot get info about device %s.\n"),
240 log_err(cd, _("Device %s has zero size.\n"), device);
243 if (*size < *offset) {
244 log_err(cd, _("Device %s is too small.\n"), device);
253 log_dbg("Calculated device size is %" PRIu64 " sectors (%s), offset %" PRIu64 ".",
254 *size, *read_only ? "RO" : "RW", *offset);
258 static int luks_remove_helper(struct crypt_device *cd,
260 const char *other_key_file,
261 const char *key_file,
264 crypt_keyslot_info ki;
267 if (key_slot == CRYPT_ANY_SLOT) {
268 key_slot = find_keyslot_by_passphrase(cd, key_file,
269 _("Enter LUKS passphrase to be deleted: "));
275 log_std(cd, _("Key slot %d selected for deletion.\n"), key_slot);
278 ki = crypt_keyslot_status(cd, key_slot);
279 if (ki == CRYPT_SLOT_INVALID) {
280 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
281 key_slot, LUKS_NUMKEYS - 1);
285 if (ki <= CRYPT_SLOT_INACTIVE) {
286 log_err(cd, _("Key %d not active. Can't wipe.\n"), key_slot);
291 if (ki == CRYPT_SLOT_ACTIVE_LAST && cd->confirm &&
292 !(cd->confirm(_("This is the last keyslot."
293 " Device will become unusable after purging this key."),
294 cd->confirm_usrptr))) {
300 r = verify_other_keyslot(cd, other_key_file, key_slot);
305 r = crypt_keyslot_destroy(cd, key_slot);
307 return (r < 0) ? r : 0;
310 static int create_device_helper(struct crypt_device *cd,
314 const char *cipher_mode,
315 const char *key_file,
327 crypt_status_info ci;
328 char *dm_cipher = NULL;
329 char *processed_key = NULL;
335 ci = crypt_status(cd, name);
336 if (ci == CRYPT_INVALID)
339 if (reload && ci < CRYPT_ACTIVE)
342 if (!reload && ci >= CRYPT_ACTIVE) {
343 log_err(cd, _("Device %s already exists.\n"), name);
347 if (key_size < 0 || key_size > 1024) {
348 log_err(cd, _("Invalid key size %d.\n"), key_size);
352 r = device_check_and_adjust(cd, cd->device, &size, &offset, &read_only);
356 if (cipher_mode && asprintf(&dm_cipher, "%s-%s", cipher, cipher_mode) < 0)
359 processed_key = process_key(cd, hash, key_file, key_size, key, keyLen);
363 r = dm_create_device(name, cd->device, dm_cipher ?: cipher, cd->type, uuid, size, skip, offset,
364 key_size, processed_key, read_only, reload);
367 crypt_safe_free(processed_key);
371 static int open_from_hdr_and_vk(struct crypt_device *cd,
372 struct volume_key *vk,
376 uint64_t size, offset;
378 int read_only, no_uuid, r;
381 offset = crypt_get_data_offset(cd);
382 read_only = flags & CRYPT_ACTIVATE_READONLY;
383 no_uuid = flags & CRYPT_ACTIVATE_NO_UUID;
385 r = device_check_and_adjust(cd, cd->device, &size, &offset, &read_only);
389 if (asprintf(&cipher, "%s-%s", crypt_get_cipher(cd),
390 crypt_get_cipher_mode(cd)) < 0)
393 r = dm_create_device(name, cd->device, cipher, cd->type,
394 no_uuid ? NULL : crypt_get_uuid(cd),
395 size, 0, offset, vk->keylength, vk->key,
401 static void log_wrapper(int level, const char *msg, void *usrptr)
403 void (*xlog)(int level, char *msg) = usrptr;
404 xlog(level, (char *)msg);
407 static int yesDialog_wrapper(const char *msg, void *usrptr)
409 int (*xyesDialog)(char *msg) = usrptr;
410 return xyesDialog((char*)msg);
413 int crypt_confirm(struct crypt_device *cd, const char *msg)
415 if (!cd || !cd->confirm)
418 return cd->confirm(msg, cd->confirm_usrptr);
421 static void key_from_terminal(struct crypt_device *cd, char *msg, char **key,
422 unsigned int *key_len, int force_verify)
427 *key = crypt_safe_alloc(MAX_TTY_PASSWORD_LEN);
430 r = cd->password(msg, *key, (size_t)key_len, cd->password_usrptr);
432 crypt_safe_free(*key);
437 crypt_get_key(msg, key, key_len, 0, NULL, cd->timeout,
438 (force_verify || cd->password_verify), cd);
441 static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslot,
442 struct volume_key **vk)
444 char *prompt = NULL, *passphrase_read = NULL;
445 unsigned int passphrase_size_read;
446 int r = -EINVAL, tries = cd->tries;
448 if(asprintf(&prompt, _("Enter passphrase for %s: "), cd->device) < 0)
454 crypt_free_volume_key(*vk);
457 key_from_terminal(cd, prompt, &passphrase_read,
458 &passphrase_size_read, 0);
459 if(!passphrase_read) {
464 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
465 passphrase_size_read, &cd->hdr, vk, cd);
466 crypt_safe_free(passphrase_read);
467 passphrase_read = NULL;
468 } while (r == -EPERM && (--tries > 0));
471 crypt_free_volume_key(*vk);
480 static void key_from_file(struct crypt_device *cd, char *msg,
481 char **key, unsigned int *key_len,
482 const char *key_file, size_t key_size)
484 crypt_get_key(msg, key, key_len, key_size, key_file, cd->timeout, 0, cd);
487 static int _crypt_init(struct crypt_device **cd,
489 struct crypt_options *options,
490 int load, int need_dm)
494 /* if it is plain device and mapping table is being reloaded
495 initialize it by name*/
496 init_by_name = (type && !strcmp(type, CRYPT_PLAIN) && load);
498 /* Some of old API calls do not require DM in kernel,
499 fake initialisation by initialise it with kernel_check disabled */
501 (void)dm_init(NULL, 0);
503 r = crypt_init_by_name(cd, options->name);
505 r = crypt_init(cd, options->device);
512 crypt_set_log_callback(*cd, log_wrapper, options->icb->log);
513 crypt_set_confirm_callback(*cd, yesDialog_wrapper, options->icb->yesDialog);
515 crypt_set_timeout(*cd, options->timeout);
516 crypt_set_password_retry(*cd, options->tries);
517 crypt_set_iterarion_time(*cd, options->iteration_time ?: 1000);
518 crypt_set_password_verify(*cd, options->flags & CRYPT_FLAG_VERIFY);
520 if (load && !init_by_name)
521 r = crypt_load(*cd, type, NULL);
523 if (!r && type && !(*cd)->type) {
524 (*cd)->type = strdup(type);
535 void crypt_set_log_callback(struct crypt_device *cd,
536 void (*log)(int level, const char *msg, void *usrptr),
543 cd->log_usrptr = usrptr;
547 void crypt_set_confirm_callback(struct crypt_device *cd,
548 int (*confirm)(const char *msg, void *usrptr),
551 cd->confirm = confirm;
552 cd->confirm_usrptr = usrptr;
555 void crypt_set_password_callback(struct crypt_device *cd,
556 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
559 cd->password = password;
560 cd->password_usrptr = usrptr;
563 /* OPTIONS: name, cipher, device, hash, key_file, key_size, key_slot,
564 * offset, size, skip, timeout, tries, passphrase_fd (ignored),
566 static int crypt_create_and_update_device(struct crypt_options *options, int update)
568 struct crypt_device *cd = NULL;
573 r = _crypt_init(&cd, CRYPT_PLAIN, options, 0, 1);
577 crypt_get_key(_("Enter passphrase: "), &key, &keyLen, options->key_size,
578 options->key_file, cd->timeout, cd->password_verify, cd);
582 r = create_device_helper(cd, options->name, options->hash,
583 options->cipher, NULL, options->key_file, key, keyLen,
584 options->key_size, options->size, options->skip,
585 options->offset, NULL, options->flags & CRYPT_FLAG_READONLY,
586 options->flags, update);
588 crypt_safe_free(key);
593 int crypt_create_device(struct crypt_options *options)
595 return crypt_create_and_update_device(options, 0);
598 int crypt_update_device(struct crypt_options *options)
600 return crypt_create_and_update_device(options, 1);
603 /* OPTIONS: name, size, icb */
604 int crypt_resize_device(struct crypt_options *options)
606 struct crypt_device *cd = NULL;
607 char *device = NULL, *cipher = NULL, *uuid = NULL, *key = NULL;
609 uint64_t size, skip, offset;
610 int key_size, read_only, r;
612 log_dbg("Resizing device %s to %" PRIu64 " sectors.", options->name, options->size);
614 if (dm_init(NULL, 1) < 0)
617 r = dm_query_device(options->name, &device, &size, &skip, &offset,
618 &cipher, &key_size, &key, &read_only, NULL, &uuid);
620 log_err(NULL, _("Device %s is not active.\n"), options->name);
624 /* Try to determine type of device from UUID */
627 if (!strncmp(uuid, CRYPT_PLAIN, strlen(CRYPT_PLAIN))) {
631 } else if (!strncmp(uuid, CRYPT_LUKS1, strlen(CRYPT_LUKS1)))
635 if (!options->device)
636 options->device = device;
638 r = _crypt_init(&cd, type, options, 1, 1);
642 size = options->size;
643 r = device_check_and_adjust(cd, device, &size, &offset, &read_only);
647 r = dm_create_device(options->name, device, cipher, type,
648 crypt_get_uuid(cd), size, skip, offset,
649 key_size, key, read_only, 1);
651 crypt_safe_free(key);
653 if (options->device == device)
654 options->device = NULL;
662 /* OPTIONS: name, icb */
663 int crypt_query_device(struct crypt_options *options)
667 log_dbg("Query device %s.", options->name);
669 if (dm_init(NULL, 1) < 0)
672 r = dm_status_device(options->name);
676 r = dm_query_device(options->name, (char **)&options->device, &options->size,
677 &options->skip, &options->offset, (char **)&options->cipher,
678 &options->key_size, NULL, &read_only, NULL, NULL);
681 options->flags |= CRYPT_FLAG_READONLY;
683 options->flags |= CRYPT_FLAG_FREE_DEVICE;
684 options->flags |= CRYPT_FLAG_FREE_CIPHER;
696 /* OPTIONS: name, icb */
697 int crypt_remove_device(struct crypt_options *options)
699 struct crypt_device *cd = NULL;
702 r = crypt_init_by_name(&cd, options->name);
704 r = crypt_deactivate(cd, options->name);
711 /* OPTIONS: device, cipher, hash, align_payload, key_size (master key), key_slot
712 * new_key_file, iteration_time, timeout, flags, icb */
713 int crypt_luksFormat(struct crypt_options *options)
715 char cipherName[LUKS_CIPHERNAME_L];
716 char cipherMode[LUKS_CIPHERMODE_L];
718 unsigned int passwordLen;
719 struct crypt_device *cd = NULL;
720 struct crypt_params_luks1 cp = {
721 .hash = options->hash,
722 .data_alignment = options->align_payload
726 r = crypt_parse_name_and_mode(options->cipher, cipherName, cipherMode);
728 log_err(cd, _("No known cipher specification pattern detected.\n"));
732 if ((r = _crypt_init(&cd, CRYPT_LUKS1, options, 0, 1)))
735 if (options->key_slot >= LUKS_NUMKEYS && options->key_slot != CRYPT_ANY_SLOT) {
736 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
737 options->key_slot, LUKS_NUMKEYS - 1);
742 crypt_get_key(_("Enter LUKS passphrase: "), &password, &passwordLen, 0,
743 options->new_key_file, cd->timeout, cd->password_verify, cd);
750 r = crypt_format(cd, CRYPT_LUKS1, cipherName, cipherMode,
751 NULL, NULL, options->key_size, &cp);
755 /* Add keyslot using internally stored volume key generated during format */
756 r = crypt_keyslot_add_by_volume_key(cd, options->key_slot, NULL, 0,
757 password, passwordLen);
760 crypt_safe_free(password);
761 return (r < 0) ? r : 0;
764 /* OPTIONS: name, device, key_size, key_file, timeout, tries, flags, icb */
765 int crypt_luksOpen(struct crypt_options *options)
767 struct crypt_device *cd = NULL;
774 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
778 if (options->flags & CRYPT_FLAG_READONLY)
779 flags |= CRYPT_ACTIVATE_READONLY;
781 if (options->flags & CRYPT_FLAG_NON_EXCLUSIVE_ACCESS)
782 flags |= CRYPT_ACTIVATE_NO_UUID;
784 if (options->key_file)
785 r = crypt_activate_by_keyfile(cd, options->name,
786 CRYPT_ANY_SLOT, options->key_file, options->key_size,
789 r = crypt_activate_by_passphrase(cd, options->name,
790 CRYPT_ANY_SLOT, options->passphrase,
791 options->passphrase ? strlen(options->passphrase) : 0,
795 return (r < 0) ? r : 0;
798 /* OPTIONS: device, keys_slot, key_file, timeout, flags, icb */
799 int crypt_luksKillSlot(struct crypt_options *options)
801 struct crypt_device *cd = NULL;
804 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
808 r = luks_remove_helper(cd, options->key_slot, options->key_file, NULL,
809 options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
812 return (r < 0) ? r : 0;
815 /* OPTIONS: device, new_key_file, key_file, timeout, flags, icb */
816 int crypt_luksRemoveKey(struct crypt_options *options)
818 struct crypt_device *cd = NULL;
821 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
825 r = luks_remove_helper(cd, CRYPT_ANY_SLOT, options->key_file, options->new_key_file,
826 options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
829 return (r < 0) ? r : 0;
833 /* OPTIONS: device, new_key_file, key_file, key_slot, flags,
834 iteration_time, timeout, icb */
835 int crypt_luksAddKey(struct crypt_options *options)
837 struct crypt_device *cd = NULL;
840 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
844 if (options->key_file || options->new_key_file)
845 r = crypt_keyslot_add_by_keyfile(cd, options->key_slot,
846 options->key_file, 0,
847 options->new_key_file, 0);
849 r = crypt_keyslot_add_by_passphrase(cd, options->key_slot,
853 return (r < 0) ? r : 0;
856 /* OPTIONS: device, icb */
857 int crypt_luksUUID(struct crypt_options *options)
859 struct crypt_device *cd = NULL;
863 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
867 uuid = (char *)crypt_get_uuid(cd);
868 log_std(cd, uuid ?: "");
874 /* OPTIONS: device, icb */
875 int crypt_isLuks(struct crypt_options *options)
877 struct crypt_device *cd = NULL;
880 log_dbg("Check device %s for LUKS header.", options->device);
886 r = crypt_init(&cd, options->device);
890 /* Do print fail here, no need to crypt_load() */
891 r = LUKS_read_phdr(cd->device, &cd->hdr, 0, cd) ? -EINVAL : 0;
897 /* OPTIONS: device, icb */
898 int crypt_luksDump(struct crypt_options *options)
900 struct crypt_device *cd = NULL;
903 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
913 void crypt_get_error(char *buf, size_t size)
915 const char *error = get_error();
917 if (!buf || size < 1)
920 strncpy(buf, error, size - 1);
921 buf[size - 1] = '\0';
927 void crypt_put_options(struct crypt_options *options)
929 if (options->flags & CRYPT_FLAG_FREE_DEVICE) {
930 free((char *)options->device);
931 options->device = NULL;
932 options->flags &= ~CRYPT_FLAG_FREE_DEVICE;
934 if (options->flags & CRYPT_FLAG_FREE_CIPHER) {
935 free((char *)options->cipher);
936 options->cipher = NULL;
937 options->flags &= ~CRYPT_FLAG_FREE_CIPHER;
941 const char *crypt_get_dir(void)
946 /////////////////////////////////
951 int crypt_init(struct crypt_device **cd, const char *device)
953 struct crypt_device *h = NULL;
958 log_dbg("Allocating crypt device %s context.", device);
960 if (device && !device_ready(NULL, device, O_RDONLY))
963 if (!(h = malloc(sizeof(struct crypt_device))))
966 memset(h, 0, sizeof(*h));
969 h->device = strdup(device);
977 if (dm_init(h, 1) < 0) {
982 h->iteration_time = 1000;
983 h->password_verify = 0;
985 h->rng_type = crypt_random_default_key_rng();
990 int crypt_init_by_name(struct crypt_device **cd, const char *name)
992 crypt_status_info ci;
996 log_dbg("Allocating crypt device context by device %s.", name);
998 ci = crypt_status(NULL, name);
999 if (ci == CRYPT_INVALID)
1002 if (ci < CRYPT_ACTIVE) {
1003 log_err(NULL, _("Device %s is not active.\n"), name);
1007 r = dm_query_device(name, &device, NULL, NULL, NULL,
1008 NULL, NULL, NULL, NULL, NULL, NULL);
1010 /* Underlying device disappeared but mapping still active */
1011 if (r >= 0 && !device)
1012 log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
1016 r = crypt_init(cd, device);
1022 static int _crypt_format_plain(struct crypt_device *cd,
1024 const char *cipher_mode,
1026 size_t volume_key_size,
1027 struct crypt_params_plain *params)
1029 if (!cipher || !cipher_mode) {
1030 log_err(cd, _("Invalid plain crypt parameters.\n"));
1034 if (volume_key_size > 1024) {
1035 log_err(cd, _("Invalid key size.\n"));
1039 cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
1040 if (!cd->volume_key)
1043 cd->plain_cipher = strdup(cipher);
1044 cd->plain_cipher_mode = strdup(cipher_mode);
1047 cd->plain_uuid = strdup(uuid);
1049 if (params && params->hash)
1050 cd->plain_hdr.hash = strdup(params->hash);
1052 cd->plain_hdr.offset = params ? params->offset : 0;
1053 cd->plain_hdr.skip = params ? params->skip : 0;
1055 if (!cd->plain_cipher || !cd->plain_cipher_mode)
1061 static int _crypt_format_luks1(struct crypt_device *cd,
1063 const char *cipher_mode,
1065 const char *volume_key,
1066 size_t volume_key_size,
1067 struct crypt_params_luks1 *params)
1070 unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
1071 unsigned long alignment_offset = 0;
1074 log_err(cd, _("Can't format LUKS without device.\n"));
1079 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
1082 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
1087 if (params && params->data_alignment)
1088 required_alignment = params->data_alignment * SECTOR_SIZE;
1090 get_topology_alignment(cd->device, &required_alignment,
1091 &alignment_offset, DEFAULT_DISK_ALIGNMENT);
1093 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
1094 (params && params->hash) ? params->hash : "sha1",
1096 required_alignment / SECTOR_SIZE,
1097 alignment_offset / SECTOR_SIZE,
1098 cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1102 /* Wipe first 8 sectors - fs magic numbers etc. */
1103 r = wipe_device_header(cd->device, 8);
1105 log_err(cd, _("Can't wipe header on device %s.\n"), cd->device);
1109 r = LUKS_write_phdr(cd->device, &cd->hdr, cd);
1114 int crypt_format(struct crypt_device *cd,
1117 const char *cipher_mode,
1119 const char *volume_key,
1120 size_t volume_key_size,
1125 log_dbg("Formatting device %s as type %s.", cd->device ?: "(none)", cd->type ?: "(none)");
1130 r = init_crypto(cd);
1135 r = _crypt_format_plain(cd, cipher, cipher_mode,
1136 uuid, volume_key_size, params);
1137 else if (isLUKS(type))
1138 r = _crypt_format_luks1(cd, cipher, cipher_mode,
1139 uuid, volume_key, volume_key_size, params);
1141 /* FIXME: allow plugins here? */
1142 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
1146 if (!r && !(cd->type = strdup(type)))
1150 crypt_free_volume_key(cd->volume_key);
1151 cd->volume_key = NULL;
1157 int crypt_load(struct crypt_device *cd,
1158 const char *requested_type,
1161 struct luks_phdr hdr;
1164 log_dbg("Trying to load %s crypt type from device %s.",
1165 requested_type ?: "any", cd->device ?: "(none)");
1170 if (requested_type && !isLUKS(requested_type))
1173 r = init_crypto(cd);
1177 r = LUKS_read_phdr(cd->device, &hdr, 1, cd);
1180 memcpy(&cd->hdr, &hdr, sizeof(hdr));
1181 cd->type = strdup(requested_type);
1189 int crypt_header_backup(struct crypt_device *cd,
1190 const char *requested_type,
1191 const char *backup_file)
1195 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1198 r = init_crypto(cd);
1202 log_dbg("Requested header backup of device %s (%s) to "
1203 "file %s.", cd->device, requested_type, backup_file);
1205 return LUKS_hdr_backup(backup_file, cd->device, &cd->hdr, cd);
1208 int crypt_header_restore(struct crypt_device *cd,
1209 const char *requested_type,
1210 const char *backup_file)
1214 if (requested_type && !isLUKS(requested_type))
1217 /* Some hash functions need initialized gcrypt library */
1218 r = init_crypto(cd);
1222 log_dbg("Requested header restore to device %s (%s) from "
1223 "file %s.", cd->device, requested_type, backup_file);
1225 return LUKS_hdr_restore(backup_file, cd->device, &cd->hdr, cd);
1228 void crypt_free(struct crypt_device *cd)
1231 log_dbg("Releasing crypt device %s context.", cd->device);
1235 crypt_free_volume_key(cd->volume_key);
1240 /* used in plain device only */
1241 free((char*)cd->plain_hdr.hash);
1242 free(cd->plain_cipher);
1243 free(cd->plain_cipher_mode);
1244 free(cd->plain_uuid);
1250 int crypt_suspend(struct crypt_device *cd,
1253 crypt_status_info ci;
1254 int r, suspended = 0;
1256 log_dbg("Suspending volume %s.", name);
1258 ci = crypt_status(NULL, name);
1259 if (ci < CRYPT_ACTIVE) {
1260 log_err(cd, _("Volume %s is not active.\n"), name);
1264 if (!cd && dm_init(NULL, 1) < 0)
1267 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1268 NULL, NULL, NULL, NULL, &suspended, NULL);
1273 log_err(cd, _("Volume %s is already suspended.\n"), name);
1278 r = dm_suspend_and_wipe_key(name);
1280 log_err(cd, "Suspend is not supported for device %s.\n", name);
1282 log_err(cd, "Error during suspending device %s.\n", name);
1289 int crypt_resume_by_passphrase(struct crypt_device *cd,
1292 const char *passphrase,
1293 size_t passphrase_size)
1295 struct volume_key *vk = NULL;
1296 int r, suspended = 0;
1298 log_dbg("Resuming volume %s.", name);
1300 if (!isLUKS(cd->type)) {
1301 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1306 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1307 NULL, NULL, NULL, NULL, &suspended, NULL);
1312 log_err(cd, _("Volume %s is not suspended.\n"), name);
1317 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1318 passphrase_size, &cd->hdr, &vk, cd);
1320 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1324 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1326 log_err(cd, "Resume is not supported for device %s.\n", name);
1328 log_err(cd, "Error during resuming device %s.\n", name);
1332 crypt_free_volume_key(vk);
1333 return r < 0 ? r : keyslot;
1336 int crypt_resume_by_keyfile(struct crypt_device *cd,
1339 const char *keyfile,
1340 size_t keyfile_size)
1342 struct volume_key *vk = NULL;
1343 char *passphrase_read = NULL;
1344 unsigned int passphrase_size_read;
1345 int r, suspended = 0;
1347 log_dbg("Resuming volume %s.", name);
1349 if (!isLUKS(cd->type)) {
1350 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1355 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1356 NULL, NULL, NULL, NULL, &suspended, NULL);
1361 log_err(cd, _("Volume %s is not suspended.\n"), name);
1368 key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1369 &passphrase_size_read, keyfile, keyfile_size);
1371 if(!passphrase_read)
1374 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1375 passphrase_size_read, &cd->hdr, &vk, cd);
1376 crypt_safe_free(passphrase_read);
1381 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1383 log_err(cd, "Error during resuming device %s.\n", name);
1387 crypt_free_volume_key(vk);
1388 return r < 0 ? r : keyslot;
1391 // slot manipulation
1392 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1393 int keyslot, // -1 any
1394 const char *passphrase, // NULL -> terminal
1395 size_t passphrase_size,
1396 const char *new_passphrase, // NULL -> terminal
1397 size_t new_passphrase_size)
1399 struct volume_key *vk = NULL;
1400 char *password = NULL, *new_password = NULL;
1401 unsigned int passwordLen, new_passwordLen;
1404 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1405 "new passphrase %sprovided.",
1406 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1408 if (!isLUKS(cd->type)) {
1409 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1413 r = keyslot_verify_or_find_empty(cd, &keyslot);
1417 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1418 /* No slots used, try to use pre-generated key in header */
1419 if (cd->volume_key) {
1420 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1421 r = vk ? 0 : -ENOMEM;
1423 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1426 } else if (passphrase) {
1427 /* Passphrase provided, use it to unlock existing keyslot */
1428 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, passphrase,
1429 passphrase_size, &cd->hdr, &vk, cd);
1431 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1432 key_from_terminal(cd, _("Enter any passphrase: "),
1433 &password, &passwordLen, 0);
1439 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
1440 passwordLen, &cd->hdr, &vk, cd);
1441 crypt_safe_free(password);
1447 if (new_passphrase) {
1448 new_password = (char *)new_passphrase;
1449 new_passwordLen = new_passphrase_size;
1451 key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1452 &new_password, &new_passwordLen, 1);
1459 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1460 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1465 if (!new_passphrase)
1466 crypt_safe_free(new_password);
1467 crypt_free_volume_key(vk);
1468 return r ?: keyslot;
1471 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1473 const char *keyfile,
1474 size_t keyfile_size,
1475 const char *new_keyfile,
1476 size_t new_keyfile_size)
1478 struct volume_key *vk=NULL;
1479 char *password=NULL; unsigned int passwordLen;
1480 char *new_password = NULL; unsigned int new_passwordLen;
1483 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1484 keyfile ?: "[none]", new_keyfile ?: "[none]");
1486 if (!isLUKS(cd->type)) {
1487 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1491 r = keyslot_verify_or_find_empty(cd, &keyslot);
1495 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1496 /* No slots used, try to use pre-generated key in header */
1497 if (cd->volume_key) {
1498 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1499 r = vk ? 0 : -ENOMEM;
1501 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1505 /* Read password from file of (if NULL) from terminal */
1507 key_from_file(cd, _("Enter any passphrase: "), &password, &passwordLen,
1508 keyfile, keyfile_size);
1510 key_from_terminal(cd, _("Enter any passphrase: "),
1511 &password, &passwordLen, 0);
1516 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password, passwordLen,
1518 crypt_safe_free(password);
1525 key_from_file(cd, _("Enter new passphrase for key slot: "),
1526 &new_password, &new_passwordLen, new_keyfile,
1529 key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1530 &new_password, &new_passwordLen, 1);
1537 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1538 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1540 crypt_safe_free(new_password);
1541 crypt_free_volume_key(vk);
1542 return r < 0 ? r : keyslot;
1545 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1547 const char *volume_key,
1548 size_t volume_key_size,
1549 const char *passphrase,
1550 size_t passphrase_size)
1552 struct volume_key *vk = NULL;
1554 char *new_password = NULL; unsigned int new_passwordLen;
1556 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1558 if (!isLUKS(cd->type)) {
1559 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1564 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1565 else if (cd->volume_key)
1566 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1571 r = LUKS_verify_volume_key(&cd->hdr, vk);
1573 log_err(cd, _("Volume key does not match the volume.\n"));
1577 r = keyslot_verify_or_find_empty(cd, &keyslot);
1582 key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1583 &new_password, &new_passwordLen, 1);
1584 passphrase = new_password;
1585 passphrase_size = new_passwordLen;
1588 r = LUKS_set_key(cd->device, keyslot, passphrase, passphrase_size,
1589 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1592 crypt_safe_free(new_password);
1593 crypt_free_volume_key(vk);
1594 return r ?: keyslot;
1597 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1599 crypt_keyslot_info ki;
1601 log_dbg("Destroying keyslot %d.", keyslot);
1603 if (!isLUKS(cd->type)) {
1604 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1608 ki = crypt_keyslot_status(cd, keyslot);
1609 if (ki == CRYPT_SLOT_INVALID) {
1610 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1614 if (ki == CRYPT_SLOT_INACTIVE) {
1615 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1619 return LUKS_del_key(cd->device, keyslot, &cd->hdr, cd);
1622 // activation/deactivation of device mapping
1623 int crypt_activate_by_passphrase(struct crypt_device *cd,
1626 const char *passphrase,
1627 size_t passphrase_size,
1630 crypt_status_info ci;
1631 struct volume_key *vk = NULL;
1632 char *prompt = NULL;
1635 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1636 name ? "Activating" : "Checking", name ?: "",
1637 keyslot, passphrase ? "" : "[none] ");
1639 /* plain, use hashed passphrase */
1640 if (isPLAIN(cd->type))
1641 return create_device_helper(cd, name, cd->plain_hdr.hash,
1642 cd->plain_cipher, cd->plain_cipher_mode, NULL, passphrase, passphrase_size,
1643 cd->volume_key->keylength, 0, cd->plain_hdr.skip,
1644 cd->plain_hdr.offset, cd->plain_uuid, flags & CRYPT_ACTIVATE_READONLY, 0, 0);
1647 ci = crypt_status(NULL, name);
1648 if (ci == CRYPT_INVALID)
1650 else if (ci >= CRYPT_ACTIVE) {
1651 log_err(cd, _("Device %s already exists.\n"), name);
1656 if(asprintf(&prompt, _("Enter passphrase for %s: "), cd->device) < 0)
1659 /* provided passphrase, do not retry */
1661 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1662 passphrase_size, &cd->hdr, &vk, cd);
1664 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1669 r = open_from_hdr_and_vk(cd, vk, name, flags);
1672 crypt_free_volume_key(vk);
1675 return r < 0 ? r : keyslot;
1678 int crypt_activate_by_keyfile(struct crypt_device *cd,
1681 const char *keyfile,
1682 size_t keyfile_size,
1685 crypt_status_info ci;
1686 struct volume_key *vk = NULL;
1687 char *passphrase_read = NULL;
1688 unsigned int passphrase_size_read;
1691 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1692 name ?: "", keyslot, keyfile ?: "[none]");
1694 if (!isLUKS(cd->type)) {
1695 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1700 ci = crypt_status(NULL, name);
1701 if (ci == CRYPT_INVALID)
1703 else if (ci >= CRYPT_ACTIVE) {
1704 log_err(cd, _("Device %s already exists.\n"), name);
1712 key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1713 &passphrase_size_read, keyfile, keyfile_size);
1714 if(!passphrase_read)
1717 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1718 passphrase_size_read, &cd->hdr, &vk, cd);
1719 crypt_safe_free(passphrase_read);
1725 r = open_from_hdr_and_vk(cd, vk, name, flags);
1728 crypt_free_volume_key(vk);
1730 return r < 0 ? r : keyslot;
1733 int crypt_activate_by_volume_key(struct crypt_device *cd,
1735 const char *volume_key,
1736 size_t volume_key_size,
1739 crypt_status_info ci;
1740 struct volume_key *vk;
1743 log_dbg("Activating volume %s by volume key.", name);
1745 /* use key directly, no hash */
1746 if (isPLAIN(cd->type))
1747 return create_device_helper(cd, name, NULL,
1748 cd->plain_cipher, cd->plain_cipher_mode, NULL, volume_key, volume_key_size,
1749 cd->volume_key->keylength, 0, cd->plain_hdr.skip,
1750 cd->plain_hdr.offset, cd->plain_uuid, flags & CRYPT_ACTIVATE_READONLY, 0, 0);
1752 if (!isLUKS(cd->type)) {
1753 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1758 ci = crypt_status(NULL, name);
1759 if (ci == CRYPT_INVALID)
1761 else if (ci >= CRYPT_ACTIVE) {
1762 log_err(cd, _("Device %s already exists.\n"), name);
1767 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1770 r = LUKS_verify_volume_key(&cd->hdr, vk);
1773 log_err(cd, _("Volume key does not match the volume.\n"));
1776 r = open_from_hdr_and_vk(cd, vk, name, flags);
1778 crypt_free_volume_key(vk);
1783 int crypt_deactivate(struct crypt_device *cd, const char *name)
1790 log_dbg("Deactivating volume %s.", name);
1792 if (!cd && dm_init(NULL, 1) < 0)
1795 switch (crypt_status(cd, name)) {
1797 r = dm_remove_device(name, 0, 0);
1800 log_err(cd, _("Device %s is busy.\n"), name);
1803 case CRYPT_INACTIVE:
1804 log_err(cd, _("Device %s is not active.\n"), name);
1808 log_err(cd, _("Invalid device %s.\n"), name);
1818 // misc helper functions
1819 int crypt_volume_key_get(struct crypt_device *cd,
1822 size_t *volume_key_size,
1823 const char *passphrase,
1824 size_t passphrase_size)
1826 struct volume_key *vk;
1827 char *processed_key = NULL;
1830 key_len = crypt_get_volume_key_size(cd);
1831 if (key_len > *volume_key_size) {
1832 log_err(cd, _("Volume key buffer too small.\n"));
1836 if (isPLAIN(cd->type) && cd->plain_hdr.hash) {
1837 processed_key = process_key(cd, cd->plain_hdr.hash, NULL, key_len,
1838 passphrase, passphrase_size);
1839 if (!processed_key) {
1840 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
1843 memcpy(volume_key, processed_key, key_len);
1844 *volume_key_size = key_len;
1845 crypt_safe_free(processed_key);
1849 if (isLUKS(cd->type)) {
1850 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1851 passphrase_size, &cd->hdr, &vk, cd);
1854 memcpy(volume_key, vk->key, vk->keylength);
1855 *volume_key_size = vk->keylength;
1858 crypt_free_volume_key(vk);
1862 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
1866 int crypt_volume_key_verify(struct crypt_device *cd,
1867 const char *volume_key,
1868 size_t volume_key_size)
1870 struct volume_key *vk;
1873 if (!isLUKS(cd->type)) {
1874 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1878 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1882 r = LUKS_verify_volume_key(&cd->hdr, vk);
1885 log_err(cd, _("Volume key does not match the volume.\n"));
1887 crypt_free_volume_key(vk);
1892 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
1894 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
1895 cd->timeout = timeout_sec;
1898 void crypt_set_password_retry(struct crypt_device *cd, int tries)
1900 log_dbg("Password retry count set to %d.", tries);
1904 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
1906 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
1907 cd->iteration_time = iteration_time_ms;
1910 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
1912 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
1913 cd->password_verify = password_verify ? 1 : 0;
1916 void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
1919 case CRYPT_RNG_URANDOM:
1920 case CRYPT_RNG_RANDOM:
1921 log_dbg("RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
1922 cd->rng_type = rng_type;
1926 int crypt_get_rng_type(struct crypt_device *cd)
1931 return cd->rng_type;
1934 int crypt_memory_lock(struct crypt_device *cd, int lock)
1936 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
1940 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
1944 if (!cd && dm_init(NULL, 1) < 0)
1945 return CRYPT_INVALID;
1947 r = dm_status_device(name);
1952 if (r < 0 && r != -ENODEV)
1953 return CRYPT_INVALID;
1956 return CRYPT_ACTIVE;
1961 return CRYPT_INACTIVE;
1964 static void hexprintICB(struct crypt_device *cd, char *d, int n)
1967 for(i = 0; i < n; i++)
1968 log_std(cd, "%02hhx ", (char)d[i]);
1971 int crypt_dump(struct crypt_device *cd)
1974 if (!isLUKS(cd->type)) { //FIXME
1975 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1979 log_std(cd, "LUKS header information for %s\n\n", cd->device);
1980 log_std(cd, "Version: \t%d\n", cd->hdr.version);
1981 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
1982 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
1983 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec);
1984 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
1985 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
1986 log_std(cd, "MK digest: \t");
1987 hexprintICB(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE);
1989 log_std(cd, "MK salt: \t");
1990 hexprintICB(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2);
1991 log_std(cd, "\n \t");
1992 hexprintICB(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
1994 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
1995 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
1996 for(i = 0; i < LUKS_NUMKEYS; i++) {
1997 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
1998 log_std(cd, "Key Slot %d: ENABLED\n",i);
1999 log_std(cd, "\tIterations: \t%d\n",
2000 cd->hdr.keyblock[i].passwordIterations);
2001 log_std(cd, "\tSalt: \t");
2002 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt,
2004 log_std(cd, "\n\t \t");
2005 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt +
2006 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
2009 log_std(cd, "\tKey material offset:\t%d\n",
2010 cd->hdr.keyblock[i].keyMaterialOffset);
2011 log_std(cd, "\tAF stripes: \t%d\n",
2012 cd->hdr.keyblock[i].stripes);
2015 log_std(cd, "Key Slot %d: DISABLED\n", i);
2018 log_std(cd, "DNAME: %s\n", crypt_get_device_name(cd) ?: "");
2023 const char *crypt_get_cipher(struct crypt_device *cd)
2025 if (isPLAIN(cd->type))
2026 return cd->plain_cipher;
2028 if (isLUKS(cd->type))
2029 return cd->hdr.cipherName;
2034 const char *crypt_get_cipher_mode(struct crypt_device *cd)
2036 if (isPLAIN(cd->type))
2037 return cd->plain_cipher_mode;
2039 if (isLUKS(cd->type))
2040 return cd->hdr.cipherMode;
2045 const char *crypt_get_uuid(struct crypt_device *cd)
2047 if (isLUKS(cd->type))
2048 return cd->hdr.uuid;
2053 const char *crypt_get_device_name(struct crypt_device *cd)
2058 int crypt_get_volume_key_size(struct crypt_device *cd)
2060 if (isPLAIN(cd->type))
2061 return cd->volume_key->keylength;
2063 if (isLUKS(cd->type))
2064 return cd->hdr.keyBytes;
2069 uint64_t crypt_get_data_offset(struct crypt_device *cd)
2071 if (isPLAIN(cd->type))
2072 return cd->plain_hdr.offset;
2074 if (isLUKS(cd->type))
2075 return cd->hdr.payloadOffset;
2080 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
2082 if (!isLUKS(cd->type)) {
2083 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2084 return CRYPT_SLOT_INVALID;
2087 return LUKS_keyslot_info(&cd->hdr, keyslot);