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)
103 key = crypt_safe_alloc(key_size);
104 memset(key, 0, key_size);
106 /* key is coming from binary file */
107 if (key_file && strcmp(key_file, "-")) {
108 if(passLen < key_size) {
109 log_err(cd, _("Cannot not read %d bytes from key file %s.\n"),
111 crypt_safe_free(key);
114 memcpy(key, pass, key_size);
118 /* key is coming from tty, fd or binary stdin */
120 if (hash(NULL, hash_name, key, key_size, pass, passLen) < 0) {
121 log_err(cd, _("Key processing error (using hash algorithm %s).\n"),
123 crypt_safe_free(key);
126 } else if (passLen > key_size) {
127 memcpy(key, pass, key_size);
129 memcpy(key, pass, passLen);
135 static int isPLAIN(const char *type)
137 return (type && !strcmp(CRYPT_PLAIN, type));
140 static int isLUKS(const char *type)
142 return (type && !strcmp(CRYPT_LUKS1, type));
145 /* keyslot helpers */
146 static int keyslot_verify_or_find_empty(struct crypt_device *cd, int *keyslot)
148 if (*keyslot == CRYPT_ANY_SLOT) {
149 *keyslot = LUKS_keyslot_find_empty(&cd->hdr);
151 log_err(cd, _("All key slots full.\n"));
156 switch (LUKS_keyslot_info(&cd->hdr, *keyslot)) {
157 case CRYPT_SLOT_INVALID:
158 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
159 *keyslot, LUKS_NUMKEYS - 1);
161 case CRYPT_SLOT_INACTIVE:
164 log_err(cd, _("Key slot %d is full, please select another one.\n"),
172 static int verify_other_keyslot(struct crypt_device *cd,
173 const char *key_file,
176 struct volume_key *vk = NULL;
177 crypt_keyslot_info ki;
179 char *password = NULL;
180 unsigned int passwordLen;
182 r = crypt_get_key(_("Enter any remaining LUKS passphrase: "),
183 &password, &passwordLen, 0, key_file, cd->timeout,
184 cd->password_verify, cd);
188 ki = crypt_keyslot_status(cd, keyIndex);
189 if (ki == CRYPT_SLOT_ACTIVE) /* Not last slot */
190 LUKS_keyslot_set(&cd->hdr, keyIndex, 0);
192 openedIndex = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT,
193 password, passwordLen,
196 if (ki == CRYPT_SLOT_ACTIVE)
197 LUKS_keyslot_set(&cd->hdr, keyIndex, 1);
202 log_verbose(cd, _("Key slot %d verified.\n"), openedIndex);
204 crypt_free_volume_key(vk);
205 crypt_safe_free(password);
209 static int find_keyslot_by_passphrase(struct crypt_device *cd,
210 const char *key_file,
213 struct volume_key *vk = NULL;
214 char *password = NULL;
215 unsigned int passwordLen;
218 r = crypt_get_key(message,&password,&passwordLen, 0, key_file,
219 cd->timeout, cd->password_verify, cd);
223 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
224 passwordLen, &cd->hdr, &vk, cd);
226 crypt_free_volume_key(vk);
227 crypt_safe_free(password);
231 static int device_check_and_adjust(struct crypt_device *cd,
238 int r, real_readonly;
244 r = get_device_infos(device, open_exclusive, &real_readonly, &real_size);
247 log_err(cd, _("Cannot use device %s which is in use "
248 "(already mapped or mounted).\n"),
251 log_err(cd, _("Cannot get info about device %s.\n"),
259 log_err(cd, _("Device %s has zero size.\n"), device);
262 if (*size < *offset) {
263 log_err(cd, _("Device %s is too small.\n"), device);
272 log_dbg("Calculated device size is %" PRIu64 " sectors (%s), offset %" PRIu64 ".",
273 *size, *read_only ? "RO" : "RW", *offset);
277 static int luks_remove_helper(struct crypt_device *cd,
279 const char *other_key_file,
280 const char *key_file,
283 crypt_keyslot_info ki;
286 if (key_slot == CRYPT_ANY_SLOT) {
287 key_slot = find_keyslot_by_passphrase(cd, key_file,
288 _("Enter LUKS passphrase to be deleted: "));
294 log_std(cd, _("Key slot %d selected for deletion.\n"), key_slot);
297 ki = crypt_keyslot_status(cd, key_slot);
298 if (ki == CRYPT_SLOT_INVALID) {
299 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
300 key_slot, LUKS_NUMKEYS - 1);
304 if (ki <= CRYPT_SLOT_INACTIVE) {
305 log_err(cd, _("Key %d not active. Can't wipe.\n"), key_slot);
310 if (ki == CRYPT_SLOT_ACTIVE_LAST && cd->confirm &&
311 !(cd->confirm(_("This is the last keyslot."
312 " Device will become unusable after purging this key."),
313 cd->confirm_usrptr))) {
319 r = verify_other_keyslot(cd, other_key_file, key_slot);
324 r = crypt_keyslot_destroy(cd, key_slot);
326 return (r < 0) ? r : 0;
329 static int create_device_helper(struct crypt_device *cd,
333 const char *cipher_mode,
334 const char *key_file,
346 crypt_status_info ci;
347 char *dm_cipher = NULL;
348 char *processed_key = NULL;
354 ci = crypt_status(cd, name);
355 if (ci == CRYPT_INVALID)
358 if (reload && ci < CRYPT_ACTIVE)
361 if (!reload && ci >= CRYPT_ACTIVE) {
362 log_err(cd, _("Device %s already exists.\n"), name);
366 if (key_size < 0 || key_size > 1024) {
367 log_err(cd, _("Invalid key size %d.\n"), key_size);
371 r = device_check_and_adjust(cd, cd->device, !reload, &size, &offset, &read_only);
375 if (cipher_mode && asprintf(&dm_cipher, "%s-%s", cipher, cipher_mode) < 0)
378 processed_key = process_key(cd, hash, key_file, key_size, key, keyLen);
379 if (!processed_key) {
384 r = dm_create_device(name, cd->device, dm_cipher ?: cipher, cd->type, uuid, size, skip, offset,
385 key_size, processed_key, read_only, reload);
388 crypt_safe_free(processed_key);
392 static int open_from_hdr_and_vk(struct crypt_device *cd,
393 struct volume_key *vk,
397 uint64_t size, offset;
399 int read_only, no_uuid, r;
402 offset = crypt_get_data_offset(cd);
403 read_only = flags & CRYPT_ACTIVATE_READONLY;
404 no_uuid = flags & CRYPT_ACTIVATE_NO_UUID;
406 r = device_check_and_adjust(cd, cd->device, 1, &size, &offset, &read_only);
410 if (asprintf(&cipher, "%s-%s", crypt_get_cipher(cd),
411 crypt_get_cipher_mode(cd)) < 0)
414 r = dm_create_device(name, cd->device, cipher, cd->type,
415 no_uuid ? NULL : crypt_get_uuid(cd),
416 size, 0, offset, vk->keylength, vk->key,
422 static void log_wrapper(int level, const char *msg, void *usrptr)
424 void (*xlog)(int level, char *msg) = usrptr;
425 xlog(level, (char *)msg);
428 static int yesDialog_wrapper(const char *msg, void *usrptr)
430 int (*xyesDialog)(char *msg) = usrptr;
431 return xyesDialog((char*)msg);
434 int crypt_confirm(struct crypt_device *cd, const char *msg)
436 if (!cd || !cd->confirm)
439 return cd->confirm(msg, cd->confirm_usrptr);
442 static int key_from_terminal(struct crypt_device *cd, char *msg, char **key,
443 unsigned int *key_len, int force_verify)
449 if(!msg && asprintf(&prompt, _("Enter passphrase for %s: "),
457 *key = crypt_safe_alloc(MAX_TTY_PASSWORD_LEN);
462 r = cd->password(msg, *key, MAX_TTY_PASSWORD_LEN, cd->password_usrptr);
464 crypt_safe_free(*key);
469 r = crypt_get_key(msg, key, key_len, 0, NULL, cd->timeout,
470 (force_verify || cd->password_verify), cd);
473 return (r < 0) ? r: 0;
476 static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslot,
477 struct volume_key **vk)
479 char *passphrase_read = NULL;
480 unsigned int passphrase_size_read;
481 int r = -EINVAL, tries = cd->tries;
486 crypt_free_volume_key(*vk);
489 r = key_from_terminal(cd, NULL, &passphrase_read,
490 &passphrase_size_read, 0);
494 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
495 passphrase_size_read, &cd->hdr, vk, cd);
496 crypt_safe_free(passphrase_read);
497 passphrase_read = NULL;
498 } while (r == -EPERM && (--tries > 0));
501 crypt_free_volume_key(*vk);
505 crypt_safe_free(passphrase_read);
509 static int key_from_file(struct crypt_device *cd, char *msg,
510 char **key, unsigned int *key_len,
511 const char *key_file, size_t key_size)
513 return crypt_get_key(msg, key, key_len, key_size, key_file,
517 static int _crypt_init(struct crypt_device **cd,
519 struct crypt_options *options,
520 int load, int need_dm)
524 /* if it is plain device and mapping table is being reloaded
525 initialize it by name*/
526 init_by_name = (type && !strcmp(type, CRYPT_PLAIN) && load);
528 /* Some of old API calls do not require DM in kernel,
529 fake initialisation by initialise it with kernel_check disabled */
531 (void)dm_init(NULL, 0);
533 r = crypt_init_by_name(cd, options->name);
535 r = crypt_init(cd, options->device);
542 crypt_set_log_callback(*cd, log_wrapper, options->icb->log);
543 crypt_set_confirm_callback(*cd, yesDialog_wrapper, options->icb->yesDialog);
545 crypt_set_timeout(*cd, options->timeout);
546 crypt_set_password_retry(*cd, options->tries);
547 crypt_set_iterarion_time(*cd, options->iteration_time ?: 1000);
548 crypt_set_password_verify(*cd, options->flags & CRYPT_FLAG_VERIFY);
550 if (load && !init_by_name)
551 r = crypt_load(*cd, type, NULL);
553 if (!r && type && !(*cd)->type) {
554 (*cd)->type = strdup(type);
565 void crypt_set_log_callback(struct crypt_device *cd,
566 void (*log)(int level, const char *msg, void *usrptr),
573 cd->log_usrptr = usrptr;
577 void crypt_set_confirm_callback(struct crypt_device *cd,
578 int (*confirm)(const char *msg, void *usrptr),
581 cd->confirm = confirm;
582 cd->confirm_usrptr = usrptr;
585 void crypt_set_password_callback(struct crypt_device *cd,
586 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
589 cd->password = password;
590 cd->password_usrptr = usrptr;
593 /* OPTIONS: name, cipher, device, hash, key_file, key_size, key_slot,
594 * offset, size, skip, timeout, tries, passphrase_fd (ignored),
596 static int crypt_create_and_update_device(struct crypt_options *options, int update)
598 struct crypt_device *cd = NULL;
603 r = _crypt_init(&cd, CRYPT_PLAIN, options, 0, 1);
607 r = crypt_get_key(_("Enter passphrase: "), &key, &keyLen, options->key_size,
608 options->key_file, cd->timeout, cd->password_verify, cd);
610 r = create_device_helper(cd, options->name, options->hash,
611 options->cipher, NULL, options->key_file, key, keyLen,
612 options->key_size, options->size, options->skip,
613 options->offset, NULL, options->flags & CRYPT_FLAG_READONLY,
614 options->flags, update);
616 crypt_safe_free(key);
621 int crypt_create_device(struct crypt_options *options)
623 return crypt_create_and_update_device(options, 0);
626 int crypt_update_device(struct crypt_options *options)
628 return crypt_create_and_update_device(options, 1);
631 /* OPTIONS: name, size, icb */
632 int crypt_resize_device(struct crypt_options *options)
634 struct crypt_device *cd = NULL;
635 char *device = NULL, *cipher = NULL, *uuid = NULL, *key = NULL;
637 uint64_t size, skip, offset;
638 int key_size, read_only, r;
640 log_dbg("Resizing device %s to %" PRIu64 " sectors.", options->name, options->size);
642 if (dm_init(NULL, 1) < 0)
645 r = dm_query_device(options->name, &device, &size, &skip, &offset,
646 &cipher, &key_size, &key, &read_only, NULL, &uuid);
648 log_err(NULL, _("Device %s is not active.\n"), options->name);
652 /* Try to determine type of device from UUID */
655 if (!strncmp(uuid, CRYPT_PLAIN, strlen(CRYPT_PLAIN))) {
659 } else if (!strncmp(uuid, CRYPT_LUKS1, strlen(CRYPT_LUKS1)))
663 if (!options->device)
664 options->device = device;
666 r = _crypt_init(&cd, type, options, 1, 1);
670 size = options->size;
671 r = device_check_and_adjust(cd, device, 0, &size, &offset, &read_only);
675 r = dm_create_device(options->name, device, cipher, type,
676 crypt_get_uuid(cd), size, skip, offset,
677 key_size, key, read_only, 1);
679 crypt_safe_free(key);
681 if (options->device == device)
682 options->device = NULL;
690 /* OPTIONS: name, icb */
691 int crypt_query_device(struct crypt_options *options)
695 log_dbg("Query device %s.", options->name);
697 if (dm_init(NULL, 1) < 0)
700 r = dm_status_device(options->name);
704 r = dm_query_device(options->name, (char **)&options->device, &options->size,
705 &options->skip, &options->offset, (char **)&options->cipher,
706 &options->key_size, NULL, &read_only, NULL, NULL);
709 options->flags |= CRYPT_FLAG_READONLY;
711 options->flags |= CRYPT_FLAG_FREE_DEVICE;
712 options->flags |= CRYPT_FLAG_FREE_CIPHER;
724 /* OPTIONS: name, icb */
725 int crypt_remove_device(struct crypt_options *options)
727 struct crypt_device *cd = NULL;
730 r = crypt_init_by_name(&cd, options->name);
732 r = crypt_deactivate(cd, options->name);
739 /* OPTIONS: device, cipher, hash, align_payload, key_size (master key), key_slot
740 * new_key_file, iteration_time, timeout, flags, icb */
741 int crypt_luksFormat(struct crypt_options *options)
743 char cipherName[LUKS_CIPHERNAME_L];
744 char cipherMode[LUKS_CIPHERMODE_L];
746 unsigned int passwordLen;
747 struct crypt_device *cd = NULL;
748 struct crypt_params_luks1 cp = {
749 .hash = options->hash,
750 .data_alignment = options->align_payload
754 r = crypt_parse_name_and_mode(options->cipher, cipherName, cipherMode);
756 log_err(cd, _("No known cipher specification pattern detected.\n"));
760 if ((r = _crypt_init(&cd, CRYPT_LUKS1, options, 0, 1)))
763 if (options->key_slot >= LUKS_NUMKEYS && options->key_slot != CRYPT_ANY_SLOT) {
764 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
765 options->key_slot, LUKS_NUMKEYS - 1);
770 r = crypt_get_key(_("Enter LUKS passphrase: "), &password, &passwordLen, 0,
771 options->new_key_file, cd->timeout, cd->password_verify, cd);
776 r = crypt_format(cd, CRYPT_LUKS1, cipherName, cipherMode,
777 NULL, NULL, options->key_size, &cp);
781 /* Add keyslot using internally stored volume key generated during format */
782 r = crypt_keyslot_add_by_volume_key(cd, options->key_slot, NULL, 0,
783 password, passwordLen);
786 crypt_safe_free(password);
787 return (r < 0) ? r : 0;
790 /* OPTIONS: name, device, key_size, key_file, timeout, tries, flags, icb */
791 int crypt_luksOpen(struct crypt_options *options)
793 struct crypt_device *cd = NULL;
800 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
804 if (options->flags & CRYPT_FLAG_READONLY)
805 flags |= CRYPT_ACTIVATE_READONLY;
807 if (options->flags & CRYPT_FLAG_NON_EXCLUSIVE_ACCESS)
808 flags |= CRYPT_ACTIVATE_NO_UUID;
810 if (options->key_file)
811 r = crypt_activate_by_keyfile(cd, options->name,
812 CRYPT_ANY_SLOT, options->key_file, options->key_size,
815 r = crypt_activate_by_passphrase(cd, options->name,
816 CRYPT_ANY_SLOT, options->passphrase,
817 options->passphrase ? strlen(options->passphrase) : 0,
821 return (r < 0) ? r : 0;
824 /* OPTIONS: device, keys_slot, key_file, timeout, flags, icb */
825 int crypt_luksKillSlot(struct crypt_options *options)
827 struct crypt_device *cd = NULL;
830 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
834 r = luks_remove_helper(cd, options->key_slot, options->key_file, NULL,
835 options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
838 return (r < 0) ? r : 0;
841 /* OPTIONS: device, new_key_file, key_file, timeout, flags, icb */
842 int crypt_luksRemoveKey(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, CRYPT_ANY_SLOT, options->key_file, options->new_key_file,
852 options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
855 return (r < 0) ? r : 0;
859 /* OPTIONS: device, new_key_file, key_file, key_slot, flags,
860 iteration_time, timeout, icb */
861 int crypt_luksAddKey(struct crypt_options *options)
863 struct crypt_device *cd = NULL;
866 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
870 if (options->key_file || options->new_key_file)
871 r = crypt_keyslot_add_by_keyfile(cd, options->key_slot,
872 options->key_file, 0,
873 options->new_key_file, 0);
875 r = crypt_keyslot_add_by_passphrase(cd, options->key_slot,
879 return (r < 0) ? r : 0;
882 /* OPTIONS: device, icb */
883 int crypt_luksUUID(struct crypt_options *options)
885 struct crypt_device *cd = NULL;
889 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
893 uuid = (char *)crypt_get_uuid(cd);
894 log_std(cd, uuid ?: "");
900 /* OPTIONS: device, icb */
901 int crypt_isLuks(struct crypt_options *options)
903 struct crypt_device *cd = NULL;
906 log_dbg("Check device %s for LUKS header.", options->device);
912 r = crypt_init(&cd, options->device);
916 /* Do print fail here, no need to crypt_load() */
917 r = LUKS_read_phdr(cd->device, &cd->hdr, 0, cd) ? -EINVAL : 0;
923 /* OPTIONS: device, icb */
924 int crypt_luksDump(struct crypt_options *options)
926 struct crypt_device *cd = NULL;
929 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
939 void crypt_get_error(char *buf, size_t size)
941 const char *error = get_error();
943 if (!buf || size < 1)
946 strncpy(buf, error, size - 1);
947 buf[size - 1] = '\0';
953 void crypt_put_options(struct crypt_options *options)
955 if (options->flags & CRYPT_FLAG_FREE_DEVICE) {
956 free((char *)options->device);
957 options->device = NULL;
958 options->flags &= ~CRYPT_FLAG_FREE_DEVICE;
960 if (options->flags & CRYPT_FLAG_FREE_CIPHER) {
961 free((char *)options->cipher);
962 options->cipher = NULL;
963 options->flags &= ~CRYPT_FLAG_FREE_CIPHER;
967 const char *crypt_get_dir(void)
972 /////////////////////////////////
977 int crypt_init(struct crypt_device **cd, const char *device)
979 struct crypt_device *h = NULL;
984 log_dbg("Allocating crypt device %s context.", device);
986 if (device && !device_ready(NULL, device, O_RDONLY))
989 if (!(h = malloc(sizeof(struct crypt_device))))
992 memset(h, 0, sizeof(*h));
995 h->device = strdup(device);
1003 if (dm_init(h, 1) < 0) {
1008 h->iteration_time = 1000;
1009 h->password_verify = 0;
1011 h->rng_type = crypt_random_default_key_rng();
1016 int crypt_init_by_name(struct crypt_device **cd, const char *name)
1018 crypt_status_info ci;
1019 struct crypt_active_device cad;
1020 char *device = NULL, *cipher_full = NULL, *device_uuid = NULL;
1021 char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
1022 int key_size = 0, r;
1025 log_dbg("Allocating crypt device context by device %s.", name);
1027 ci = crypt_status(NULL, name);
1028 if (ci == CRYPT_INVALID)
1031 if (ci < CRYPT_ACTIVE) {
1032 log_err(NULL, _("Device %s is not active.\n"), name);
1036 r = dm_query_device(name, &device, &cad.size, &cad.iv_offset, &cad.offset,
1037 &cipher_full, &key_size, NULL, NULL, NULL,
1040 /* Underlying device disappeared but mapping still active */
1041 if (r >= 0 && !device)
1042 log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
1046 r = crypt_init(cd, device);
1048 /* Try to initialise basic parameters from active device */
1049 if (!r && *device_uuid) {
1050 if (!strncmp(CRYPT_PLAIN, device_uuid, sizeof(CRYPT_PLAIN)-1)) {
1051 (*cd)->type = strdup(CRYPT_PLAIN);
1052 (*cd)->plain_uuid = strdup(device_uuid);
1053 (*cd)->plain_hdr.hash = NULL; /* no way to get this */
1054 (*cd)->plain_hdr.offset = cad.offset;
1055 (*cd)->plain_hdr.skip = cad.iv_offset;
1056 (*cd)->volume_key = crypt_alloc_volume_key(key_size, NULL);
1057 if (!(*cd)->volume_key)
1060 r = crypt_parse_name_and_mode(cipher_full, cipher, cipher_mode);
1062 (*cd)->plain_cipher = strdup(cipher);
1063 (*cd)->plain_cipher_mode = strdup(cipher_mode);
1065 } else if (!strncmp(CRYPT_LUKS1, device_uuid, sizeof(CRYPT_LUKS1)-1)) {
1067 r = crypt_load(*cd, CRYPT_LUKS1, NULL);
1077 static int _crypt_format_plain(struct crypt_device *cd,
1079 const char *cipher_mode,
1081 size_t volume_key_size,
1082 struct crypt_params_plain *params)
1084 if (!cipher || !cipher_mode) {
1085 log_err(cd, _("Invalid plain crypt parameters.\n"));
1089 if (volume_key_size > 1024) {
1090 log_err(cd, _("Invalid key size.\n"));
1094 cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
1095 if (!cd->volume_key)
1098 cd->plain_cipher = strdup(cipher);
1099 cd->plain_cipher_mode = strdup(cipher_mode);
1102 cd->plain_uuid = strdup(uuid);
1104 if (params && params->hash)
1105 cd->plain_hdr.hash = strdup(params->hash);
1107 cd->plain_hdr.offset = params ? params->offset : 0;
1108 cd->plain_hdr.skip = params ? params->skip : 0;
1110 if (!cd->plain_cipher || !cd->plain_cipher_mode)
1116 static int _crypt_format_luks1(struct crypt_device *cd,
1118 const char *cipher_mode,
1120 const char *volume_key,
1121 size_t volume_key_size,
1122 struct crypt_params_luks1 *params)
1125 unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
1126 unsigned long alignment_offset = 0;
1129 log_err(cd, _("Can't format LUKS without device.\n"));
1134 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
1137 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
1142 if (params && params->data_alignment)
1143 required_alignment = params->data_alignment * SECTOR_SIZE;
1145 get_topology_alignment(cd->device, &required_alignment,
1146 &alignment_offset, DEFAULT_DISK_ALIGNMENT);
1148 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
1149 (params && params->hash) ? params->hash : "sha1",
1151 required_alignment / SECTOR_SIZE,
1152 alignment_offset / SECTOR_SIZE,
1153 cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1157 /* Wipe first 8 sectors - fs magic numbers etc. */
1158 r = wipe_device_header(cd->device, 8);
1161 log_err(cd, _("Cannot format device %s which is still in use.\n"),
1164 log_err(cd, _("Cannot wipe header on device %s.\n"),
1170 r = LUKS_write_phdr(cd->device, &cd->hdr, cd);
1175 int crypt_format(struct crypt_device *cd,
1178 const char *cipher_mode,
1180 const char *volume_key,
1181 size_t volume_key_size,
1186 log_dbg("Formatting device %s as type %s.", cd->device ?: "(none)", cd->type ?: "(none)");
1191 r = init_crypto(cd);
1196 r = _crypt_format_plain(cd, cipher, cipher_mode,
1197 uuid, volume_key_size, params);
1198 else if (isLUKS(type))
1199 r = _crypt_format_luks1(cd, cipher, cipher_mode,
1200 uuid, volume_key, volume_key_size, params);
1202 /* FIXME: allow plugins here? */
1203 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
1207 if (!r && !(cd->type = strdup(type)))
1211 crypt_free_volume_key(cd->volume_key);
1212 cd->volume_key = NULL;
1218 int crypt_load(struct crypt_device *cd,
1219 const char *requested_type,
1222 struct luks_phdr hdr;
1225 log_dbg("Trying to load %s crypt type from device %s.",
1226 requested_type ?: "any", cd->device ?: "(none)");
1231 if (requested_type && !isLUKS(requested_type))
1234 r = init_crypto(cd);
1238 r = LUKS_read_phdr(cd->device, &hdr, 1, cd);
1241 memcpy(&cd->hdr, &hdr, sizeof(hdr));
1242 cd->type = strdup(requested_type);
1250 int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
1252 char *device = NULL, *cipher = NULL, *uuid = NULL, *key = NULL;
1253 uint64_t size, skip, offset;
1254 int key_size, read_only, r;
1256 /* Device context type must be initialised */
1257 if (!cd->type || !crypt_get_uuid(cd))
1260 r = dm_query_device(name, &device, &size, &skip, &offset,
1261 &cipher, &key_size, &key, &read_only, NULL, &uuid);
1263 log_err(NULL, _("Device %s is not active.\n"), name);
1272 r = device_check_and_adjust(cd, device, 0, &new_size, &offset, &read_only);
1276 if (new_size == size) {
1277 log_dbg("Device has already requested size %" PRIu64
1283 log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
1285 r = dm_create_device(name, device, cipher, cd->type,
1286 crypt_get_uuid(cd), new_size, skip, offset,
1287 key_size, key, read_only, 1);
1289 crypt_safe_free(key);
1297 int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
1299 if (!isLUKS(cd->type)) {
1300 log_err(cd, _("This operation is not supported for this device type.\n"));
1304 if (uuid && !strncmp(uuid, cd->hdr.uuid, sizeof(cd->hdr.uuid))) {
1305 log_dbg("UUID is the same as requested (%s) for device %s.",
1311 log_dbg("Requested new UUID change to %s for %s.", uuid, cd->device);
1313 log_dbg("Requested new UUID refresh for %s.", cd->device);
1315 if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
1318 return LUKS_hdr_uuid_set(cd->device, &cd->hdr, uuid, cd);
1321 int crypt_header_backup(struct crypt_device *cd,
1322 const char *requested_type,
1323 const char *backup_file)
1327 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1330 r = init_crypto(cd);
1334 log_dbg("Requested header backup of device %s (%s) to "
1335 "file %s.", cd->device, requested_type, backup_file);
1337 return LUKS_hdr_backup(backup_file, cd->device, &cd->hdr, cd);
1340 int crypt_header_restore(struct crypt_device *cd,
1341 const char *requested_type,
1342 const char *backup_file)
1346 if (requested_type && !isLUKS(requested_type))
1349 /* Some hash functions need initialized gcrypt library */
1350 r = init_crypto(cd);
1354 log_dbg("Requested header restore to device %s (%s) from "
1355 "file %s.", cd->device, requested_type, backup_file);
1357 return LUKS_hdr_restore(backup_file, cd->device, &cd->hdr, cd);
1360 void crypt_free(struct crypt_device *cd)
1363 log_dbg("Releasing crypt device %s context.", cd->device);
1367 crypt_free_volume_key(cd->volume_key);
1372 /* used in plain device only */
1373 free((char*)cd->plain_hdr.hash);
1374 free(cd->plain_cipher);
1375 free(cd->plain_cipher_mode);
1376 free(cd->plain_uuid);
1382 int crypt_suspend(struct crypt_device *cd,
1385 crypt_status_info ci;
1386 int r, suspended = 0;
1388 log_dbg("Suspending volume %s.", name);
1390 ci = crypt_status(NULL, name);
1391 if (ci < CRYPT_ACTIVE) {
1392 log_err(cd, _("Volume %s is not active.\n"), name);
1396 if (!cd && dm_init(NULL, 1) < 0)
1399 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1400 NULL, NULL, NULL, NULL, &suspended, NULL);
1405 log_err(cd, _("Volume %s is already suspended.\n"), name);
1410 r = dm_suspend_and_wipe_key(name);
1412 log_err(cd, "Suspend is not supported for device %s.\n", name);
1414 log_err(cd, "Error during suspending device %s.\n", name);
1421 int crypt_resume_by_passphrase(struct crypt_device *cd,
1424 const char *passphrase,
1425 size_t passphrase_size)
1427 struct volume_key *vk = NULL;
1428 int r, suspended = 0;
1430 log_dbg("Resuming volume %s.", name);
1432 if (!isLUKS(cd->type)) {
1433 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1438 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1439 NULL, NULL, NULL, NULL, &suspended, NULL);
1444 log_err(cd, _("Volume %s is not suspended.\n"), name);
1449 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1450 passphrase_size, &cd->hdr, &vk, cd);
1452 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1456 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1458 log_err(cd, "Resume is not supported for device %s.\n", name);
1460 log_err(cd, "Error during resuming device %s.\n", name);
1464 crypt_free_volume_key(vk);
1465 return r < 0 ? r : keyslot;
1468 int crypt_resume_by_keyfile(struct crypt_device *cd,
1471 const char *keyfile,
1472 size_t keyfile_size)
1474 struct volume_key *vk = NULL;
1475 char *passphrase_read = NULL;
1476 unsigned int passphrase_size_read;
1477 int r, suspended = 0;
1479 log_dbg("Resuming volume %s.", name);
1481 if (!isLUKS(cd->type)) {
1482 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1487 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1488 NULL, NULL, NULL, NULL, &suspended, NULL);
1493 log_err(cd, _("Volume %s is not suspended.\n"), name);
1500 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1501 &passphrase_size_read, keyfile, keyfile_size);
1505 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1506 passphrase_size_read, &cd->hdr, &vk, cd);
1511 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1513 log_err(cd, "Error during resuming device %s.\n", name);
1515 crypt_safe_free(passphrase_read);
1516 crypt_free_volume_key(vk);
1517 return r < 0 ? r : keyslot;
1520 // slot manipulation
1521 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1522 int keyslot, // -1 any
1523 const char *passphrase, // NULL -> terminal
1524 size_t passphrase_size,
1525 const char *new_passphrase, // NULL -> terminal
1526 size_t new_passphrase_size)
1528 struct volume_key *vk = NULL;
1529 char *password = NULL, *new_password = NULL;
1530 unsigned int passwordLen, new_passwordLen;
1533 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1534 "new passphrase %sprovided.",
1535 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1537 if (!isLUKS(cd->type)) {
1538 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1542 r = keyslot_verify_or_find_empty(cd, &keyslot);
1546 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1547 /* No slots used, try to use pre-generated key in header */
1548 if (cd->volume_key) {
1549 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1550 r = vk ? 0 : -ENOMEM;
1552 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1555 } else if (passphrase) {
1556 /* Passphrase provided, use it to unlock existing keyslot */
1557 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, passphrase,
1558 passphrase_size, &cd->hdr, &vk, cd);
1560 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1561 r = key_from_terminal(cd, _("Enter any passphrase: "),
1562 &password, &passwordLen, 0);
1566 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
1567 passwordLen, &cd->hdr, &vk, cd);
1568 crypt_safe_free(password);
1574 if (new_passphrase) {
1575 new_password = (char *)new_passphrase;
1576 new_passwordLen = new_passphrase_size;
1578 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1579 &new_password, &new_passwordLen, 1);
1584 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1585 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1590 if (!new_passphrase)
1591 crypt_safe_free(new_password);
1592 crypt_free_volume_key(vk);
1593 return r ?: keyslot;
1596 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1598 const char *keyfile,
1599 size_t keyfile_size,
1600 const char *new_keyfile,
1601 size_t new_keyfile_size)
1603 struct volume_key *vk = NULL;
1604 char *password = NULL; unsigned int passwordLen;
1605 char *new_password = NULL; unsigned int new_passwordLen;
1608 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1609 keyfile ?: "[none]", new_keyfile ?: "[none]");
1611 if (!isLUKS(cd->type)) {
1612 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1616 r = keyslot_verify_or_find_empty(cd, &keyslot);
1620 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1621 /* No slots used, try to use pre-generated key in header */
1622 if (cd->volume_key) {
1623 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1624 r = vk ? 0 : -ENOMEM;
1626 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1630 /* Read password from file of (if NULL) from terminal */
1632 r = key_from_file(cd, _("Enter any passphrase: "),
1633 &password, &passwordLen,
1634 keyfile, keyfile_size);
1636 r = key_from_terminal(cd, _("Enter any passphrase: "),
1637 &password, &passwordLen, 0);
1641 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password, passwordLen,
1649 r = key_from_file(cd, _("Enter new passphrase for key slot: "),
1650 &new_password, &new_passwordLen, new_keyfile,
1653 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1654 &new_password, &new_passwordLen, 1);
1658 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1659 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1661 crypt_safe_free(password);
1662 crypt_safe_free(new_password);
1663 crypt_free_volume_key(vk);
1664 return r < 0 ? r : keyslot;
1667 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1669 const char *volume_key,
1670 size_t volume_key_size,
1671 const char *passphrase,
1672 size_t passphrase_size)
1674 struct volume_key *vk = NULL;
1676 char *new_password = NULL; unsigned int new_passwordLen;
1678 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1680 if (!isLUKS(cd->type)) {
1681 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1686 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1687 else if (cd->volume_key)
1688 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1693 r = LUKS_verify_volume_key(&cd->hdr, vk);
1695 log_err(cd, _("Volume key does not match the volume.\n"));
1699 r = keyslot_verify_or_find_empty(cd, &keyslot);
1704 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1705 &new_password, &new_passwordLen, 1);
1708 passphrase = new_password;
1709 passphrase_size = new_passwordLen;
1712 r = LUKS_set_key(cd->device, keyslot, passphrase, passphrase_size,
1713 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1715 crypt_safe_free(new_password);
1716 crypt_free_volume_key(vk);
1717 return (r < 0) ? r : keyslot;
1720 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1722 crypt_keyslot_info ki;
1724 log_dbg("Destroying keyslot %d.", keyslot);
1726 if (!isLUKS(cd->type)) {
1727 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1731 ki = crypt_keyslot_status(cd, keyslot);
1732 if (ki == CRYPT_SLOT_INVALID) {
1733 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1737 if (ki == CRYPT_SLOT_INACTIVE) {
1738 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1742 return LUKS_del_key(cd->device, keyslot, &cd->hdr, cd);
1745 // activation/deactivation of device mapping
1746 int crypt_activate_by_passphrase(struct crypt_device *cd,
1749 const char *passphrase,
1750 size_t passphrase_size,
1753 crypt_status_info ci;
1754 struct volume_key *vk = NULL;
1755 char *read_passphrase = NULL;
1758 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1759 name ? "Activating" : "Checking", name ?: "",
1760 keyslot, passphrase ? "" : "[none] ");
1763 ci = crypt_status(NULL, name);
1764 if (ci == CRYPT_INVALID)
1766 else if (ci >= CRYPT_ACTIVE) {
1767 log_err(cd, _("Device %s already exists.\n"), name);
1772 /* plain, use hashed passphrase */
1773 if (isPLAIN(cd->type)) {
1775 r = key_from_terminal(cd, NULL, &read_passphrase,
1776 &passphrase_size, 0);
1779 passphrase = read_passphrase;
1781 r = create_device_helper(cd, name, cd->plain_hdr.hash,
1782 cd->plain_cipher, cd->plain_cipher_mode,
1783 NULL, passphrase, passphrase_size,
1784 cd->volume_key->keylength, 0,
1785 cd->plain_hdr.skip, cd->plain_hdr.offset,
1787 flags & CRYPT_ACTIVATE_READONLY, 0, 0);
1789 } else if (isLUKS(cd->type)) {
1790 /* provided passphrase, do not retry */
1792 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1793 passphrase_size, &cd->hdr, &vk, cd);
1795 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1800 r = open_from_hdr_and_vk(cd, vk, name, flags);
1805 crypt_safe_free(read_passphrase);
1806 crypt_free_volume_key(vk);
1808 return r < 0 ? r : keyslot;
1811 int crypt_activate_by_keyfile(struct crypt_device *cd,
1814 const char *keyfile,
1815 size_t keyfile_size,
1818 crypt_status_info ci;
1819 struct volume_key *vk = NULL;
1820 char *passphrase_read = NULL;
1821 unsigned int passphrase_size_read;
1824 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1825 name ?: "", keyslot, keyfile ?: "[none]");
1827 if (!isLUKS(cd->type)) {
1828 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1833 ci = crypt_status(NULL, name);
1834 if (ci == CRYPT_INVALID)
1836 else if (ci >= CRYPT_ACTIVE) {
1837 log_err(cd, _("Device %s already exists.\n"), name);
1845 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1846 &passphrase_size_read, keyfile, keyfile_size);
1850 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1851 passphrase_size_read, &cd->hdr, &vk, cd);
1857 r = open_from_hdr_and_vk(cd, vk, name, flags);
1859 crypt_safe_free(passphrase_read);
1860 crypt_free_volume_key(vk);
1862 return r < 0 ? r : keyslot;
1865 int crypt_activate_by_volume_key(struct crypt_device *cd,
1867 const char *volume_key,
1868 size_t volume_key_size,
1871 crypt_status_info ci;
1872 struct volume_key *vk;
1875 log_dbg("Activating volume %s by volume key.", name);
1877 if (!volume_key_size)
1880 /* use key directly, no hash */
1881 if (isPLAIN(cd->type))
1882 return create_device_helper(cd, name, NULL,
1883 cd->plain_cipher, cd->plain_cipher_mode, NULL, volume_key, volume_key_size,
1884 cd->volume_key->keylength, 0, cd->plain_hdr.skip,
1885 cd->plain_hdr.offset, cd->plain_uuid, flags & CRYPT_ACTIVATE_READONLY, 0, 0);
1887 if (!isLUKS(cd->type)) {
1888 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1893 ci = crypt_status(NULL, name);
1894 if (ci == CRYPT_INVALID)
1896 else if (ci >= CRYPT_ACTIVE) {
1897 log_err(cd, _("Device %s already exists.\n"), name);
1902 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1905 r = LUKS_verify_volume_key(&cd->hdr, vk);
1908 log_err(cd, _("Volume key does not match the volume.\n"));
1911 r = open_from_hdr_and_vk(cd, vk, name, flags);
1913 crypt_free_volume_key(vk);
1918 int crypt_deactivate(struct crypt_device *cd, const char *name)
1925 log_dbg("Deactivating volume %s.", name);
1927 if (!cd && dm_init(NULL, 1) < 0)
1930 switch (crypt_status(cd, name)) {
1932 r = dm_remove_device(name, 0, 0);
1935 log_err(cd, _("Device %s is busy.\n"), name);
1938 case CRYPT_INACTIVE:
1939 log_err(cd, _("Device %s is not active.\n"), name);
1943 log_err(cd, _("Invalid device %s.\n"), name);
1953 // misc helper functions
1954 int crypt_volume_key_get(struct crypt_device *cd,
1957 size_t *volume_key_size,
1958 const char *passphrase,
1959 size_t passphrase_size)
1961 struct volume_key *vk;
1962 char *processed_key = NULL;
1965 key_len = crypt_get_volume_key_size(cd);
1966 if (key_len > *volume_key_size) {
1967 log_err(cd, _("Volume key buffer too small.\n"));
1971 if (isPLAIN(cd->type) && cd->plain_hdr.hash) {
1972 processed_key = process_key(cd, cd->plain_hdr.hash, NULL, key_len,
1973 passphrase, passphrase_size);
1974 if (!processed_key) {
1975 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
1978 memcpy(volume_key, processed_key, key_len);
1979 *volume_key_size = key_len;
1980 crypt_safe_free(processed_key);
1984 if (isLUKS(cd->type)) {
1985 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1986 passphrase_size, &cd->hdr, &vk, cd);
1989 memcpy(volume_key, vk->key, vk->keylength);
1990 *volume_key_size = vk->keylength;
1993 crypt_free_volume_key(vk);
1997 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
2001 int crypt_volume_key_verify(struct crypt_device *cd,
2002 const char *volume_key,
2003 size_t volume_key_size)
2005 struct volume_key *vk;
2008 if (!isLUKS(cd->type)) {
2009 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2013 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2017 r = LUKS_verify_volume_key(&cd->hdr, vk);
2020 log_err(cd, _("Volume key does not match the volume.\n"));
2022 crypt_free_volume_key(vk);
2027 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
2029 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
2030 cd->timeout = timeout_sec;
2033 void crypt_set_password_retry(struct crypt_device *cd, int tries)
2035 log_dbg("Password retry count set to %d.", tries);
2039 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2041 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
2042 cd->iteration_time = iteration_time_ms;
2045 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
2047 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
2048 cd->password_verify = password_verify ? 1 : 0;
2051 void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
2054 case CRYPT_RNG_URANDOM:
2055 case CRYPT_RNG_RANDOM:
2056 log_dbg("RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
2057 cd->rng_type = rng_type;
2061 int crypt_get_rng_type(struct crypt_device *cd)
2066 return cd->rng_type;
2069 int crypt_memory_lock(struct crypt_device *cd, int lock)
2071 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
2075 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
2079 if (!cd && dm_init(NULL, 1) < 0)
2080 return CRYPT_INVALID;
2082 r = dm_status_device(name);
2087 if (r < 0 && r != -ENODEV)
2088 return CRYPT_INVALID;
2091 return CRYPT_ACTIVE;
2096 return CRYPT_INACTIVE;
2099 static void hexprintICB(struct crypt_device *cd, char *d, int n)
2102 for(i = 0; i < n; i++)
2103 log_std(cd, "%02hhx ", (char)d[i]);
2106 int crypt_dump(struct crypt_device *cd)
2109 if (!isLUKS(cd->type)) { //FIXME
2110 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2114 log_std(cd, "LUKS header information for %s\n\n", cd->device);
2115 log_std(cd, "Version: \t%d\n", cd->hdr.version);
2116 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
2117 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
2118 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec);
2119 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
2120 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
2121 log_std(cd, "MK digest: \t");
2122 hexprintICB(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE);
2124 log_std(cd, "MK salt: \t");
2125 hexprintICB(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2);
2126 log_std(cd, "\n \t");
2127 hexprintICB(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
2129 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
2130 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
2131 for(i = 0; i < LUKS_NUMKEYS; i++) {
2132 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
2133 log_std(cd, "Key Slot %d: ENABLED\n",i);
2134 log_std(cd, "\tIterations: \t%d\n",
2135 cd->hdr.keyblock[i].passwordIterations);
2136 log_std(cd, "\tSalt: \t");
2137 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt,
2139 log_std(cd, "\n\t \t");
2140 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt +
2141 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
2144 log_std(cd, "\tKey material offset:\t%d\n",
2145 cd->hdr.keyblock[i].keyMaterialOffset);
2146 log_std(cd, "\tAF stripes: \t%d\n",
2147 cd->hdr.keyblock[i].stripes);
2150 log_std(cd, "Key Slot %d: DISABLED\n", i);
2156 const char *crypt_get_cipher(struct crypt_device *cd)
2158 if (isPLAIN(cd->type))
2159 return cd->plain_cipher;
2161 if (isLUKS(cd->type))
2162 return cd->hdr.cipherName;
2167 const char *crypt_get_cipher_mode(struct crypt_device *cd)
2169 if (isPLAIN(cd->type))
2170 return cd->plain_cipher_mode;
2172 if (isLUKS(cd->type))
2173 return cd->hdr.cipherMode;
2178 const char *crypt_get_uuid(struct crypt_device *cd)
2180 if (isLUKS(cd->type))
2181 return cd->hdr.uuid;
2183 if (isPLAIN(cd->type))
2184 return cd->plain_uuid;
2189 const char *crypt_get_device_name(struct crypt_device *cd)
2194 int crypt_get_volume_key_size(struct crypt_device *cd)
2196 if (isPLAIN(cd->type) && cd->volume_key)
2197 return cd->volume_key->keylength;
2199 if (isLUKS(cd->type))
2200 return cd->hdr.keyBytes;
2205 uint64_t crypt_get_data_offset(struct crypt_device *cd)
2207 if (isPLAIN(cd->type))
2208 return cd->plain_hdr.offset;
2210 if (isLUKS(cd->type))
2211 return cd->hdr.payloadOffset;
2216 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
2218 if (!isLUKS(cd->type)) {
2219 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2220 return CRYPT_SLOT_INVALID;
2223 return LUKS_keyslot_info(&cd->hdr, keyslot);
2226 int crypt_keyslot_max(const char *type)
2228 if (type && isLUKS(type))
2229 return LUKS_NUMKEYS;
2234 const char *crypt_get_type(struct crypt_device *cd)
2239 int crypt_get_active_device(struct crypt_device *cd,
2241 struct crypt_active_device *cad)
2245 r = dm_query_device(name, NULL, &cad->size, &cad->iv_offset, &cad->offset,
2246 NULL, NULL, NULL, &readonly, NULL, NULL);
2250 cad->flags = readonly ? CRYPT_ACTIVATE_READONLY : 0;