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;
485 crypt_free_volume_key(*vk);
488 r = key_from_terminal(cd, NULL, &passphrase_read,
489 &passphrase_size_read, 0);
493 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
494 passphrase_size_read, &cd->hdr, vk, cd);
495 crypt_safe_free(passphrase_read);
496 passphrase_read = NULL;
497 } while (r == -EPERM && (--tries > 0));
500 crypt_free_volume_key(*vk);
504 crypt_safe_free(passphrase_read);
508 static int key_from_file(struct crypt_device *cd, char *msg,
509 char **key, unsigned int *key_len,
510 const char *key_file, size_t key_size)
512 return crypt_get_key(msg, key, key_len, key_size, key_file,
516 static int _crypt_init(struct crypt_device **cd,
518 struct crypt_options *options,
519 int load, int need_dm)
523 /* if it is plain device and mapping table is being reloaded
524 initialize it by name*/
525 init_by_name = (type && !strcmp(type, CRYPT_PLAIN) && load);
527 /* Some of old API calls do not require DM in kernel,
528 fake initialisation by initialise it with kernel_check disabled */
530 (void)dm_init(NULL, 0);
532 r = crypt_init_by_name(cd, options->name);
534 r = crypt_init(cd, options->device);
541 crypt_set_log_callback(*cd, log_wrapper, options->icb->log);
542 crypt_set_confirm_callback(*cd, yesDialog_wrapper, options->icb->yesDialog);
544 crypt_set_timeout(*cd, options->timeout);
545 crypt_set_password_retry(*cd, options->tries);
546 crypt_set_iterarion_time(*cd, options->iteration_time ?: 1000);
547 crypt_set_password_verify(*cd, options->flags & CRYPT_FLAG_VERIFY);
549 if (load && !init_by_name)
550 r = crypt_load(*cd, type, NULL);
552 if (!r && type && !(*cd)->type) {
553 (*cd)->type = strdup(type);
564 void crypt_set_log_callback(struct crypt_device *cd,
565 void (*log)(int level, const char *msg, void *usrptr),
572 cd->log_usrptr = usrptr;
576 void crypt_set_confirm_callback(struct crypt_device *cd,
577 int (*confirm)(const char *msg, void *usrptr),
580 cd->confirm = confirm;
581 cd->confirm_usrptr = usrptr;
584 void crypt_set_password_callback(struct crypt_device *cd,
585 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
588 cd->password = password;
589 cd->password_usrptr = usrptr;
592 /* OPTIONS: name, cipher, device, hash, key_file, key_size, key_slot,
593 * offset, size, skip, timeout, tries, passphrase_fd (ignored),
595 static int crypt_create_and_update_device(struct crypt_options *options, int update)
597 struct crypt_device *cd = NULL;
602 r = _crypt_init(&cd, CRYPT_PLAIN, options, 0, 1);
606 r = crypt_get_key(_("Enter passphrase: "), &key, &keyLen, options->key_size,
607 options->key_file, cd->timeout, cd->password_verify, cd);
609 r = create_device_helper(cd, options->name, options->hash,
610 options->cipher, NULL, options->key_file, key, keyLen,
611 options->key_size, options->size, options->skip,
612 options->offset, NULL, options->flags & CRYPT_FLAG_READONLY,
613 options->flags, update);
615 crypt_safe_free(key);
620 int crypt_create_device(struct crypt_options *options)
622 return crypt_create_and_update_device(options, 0);
625 int crypt_update_device(struct crypt_options *options)
627 return crypt_create_and_update_device(options, 1);
630 /* OPTIONS: name, size, icb */
631 int crypt_resize_device(struct crypt_options *options)
633 struct crypt_device *cd = NULL;
634 char *device = NULL, *cipher = NULL, *uuid = NULL, *key = NULL;
636 uint64_t size, skip, offset;
637 int key_size, read_only, r;
639 log_dbg("Resizing device %s to %" PRIu64 " sectors.", options->name, options->size);
641 if (dm_init(NULL, 1) < 0)
644 r = dm_query_device(options->name, &device, &size, &skip, &offset,
645 &cipher, &key_size, &key, &read_only, NULL, &uuid);
647 log_err(NULL, _("Device %s is not active.\n"), options->name);
651 /* Try to determine type of device from UUID */
654 if (!strncmp(uuid, CRYPT_PLAIN, strlen(CRYPT_PLAIN))) {
658 } else if (!strncmp(uuid, CRYPT_LUKS1, strlen(CRYPT_LUKS1)))
662 if (!options->device)
663 options->device = device;
665 r = _crypt_init(&cd, type, options, 1, 1);
669 size = options->size;
670 r = device_check_and_adjust(cd, device, 0, &size, &offset, &read_only);
674 r = dm_create_device(options->name, device, cipher, type,
675 crypt_get_uuid(cd), size, skip, offset,
676 key_size, key, read_only, 1);
678 crypt_safe_free(key);
680 if (options->device == device)
681 options->device = NULL;
689 /* OPTIONS: name, icb */
690 int crypt_query_device(struct crypt_options *options)
694 log_dbg("Query device %s.", options->name);
696 if (dm_init(NULL, 1) < 0)
699 r = dm_status_device(options->name);
703 r = dm_query_device(options->name, (char **)&options->device, &options->size,
704 &options->skip, &options->offset, (char **)&options->cipher,
705 &options->key_size, NULL, &read_only, NULL, NULL);
708 options->flags |= CRYPT_FLAG_READONLY;
710 options->flags |= CRYPT_FLAG_FREE_DEVICE;
711 options->flags |= CRYPT_FLAG_FREE_CIPHER;
723 /* OPTIONS: name, icb */
724 int crypt_remove_device(struct crypt_options *options)
726 struct crypt_device *cd = NULL;
729 r = crypt_init_by_name(&cd, options->name);
731 r = crypt_deactivate(cd, options->name);
738 /* OPTIONS: device, cipher, hash, align_payload, key_size (master key), key_slot
739 * new_key_file, iteration_time, timeout, flags, icb */
740 int crypt_luksFormat(struct crypt_options *options)
742 char cipherName[LUKS_CIPHERNAME_L];
743 char cipherMode[LUKS_CIPHERMODE_L];
745 unsigned int passwordLen;
746 struct crypt_device *cd = NULL;
747 struct crypt_params_luks1 cp = {
748 .hash = options->hash,
749 .data_alignment = options->align_payload
753 r = crypt_parse_name_and_mode(options->cipher, cipherName, cipherMode);
755 log_err(cd, _("No known cipher specification pattern detected.\n"));
759 if ((r = _crypt_init(&cd, CRYPT_LUKS1, options, 0, 1)))
762 if (options->key_slot >= LUKS_NUMKEYS && options->key_slot != CRYPT_ANY_SLOT) {
763 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
764 options->key_slot, LUKS_NUMKEYS - 1);
769 r = crypt_get_key(_("Enter LUKS passphrase: "), &password, &passwordLen, 0,
770 options->new_key_file, cd->timeout, cd->password_verify, cd);
775 r = crypt_format(cd, CRYPT_LUKS1, cipherName, cipherMode,
776 NULL, NULL, options->key_size, &cp);
780 /* Add keyslot using internally stored volume key generated during format */
781 r = crypt_keyslot_add_by_volume_key(cd, options->key_slot, NULL, 0,
782 password, passwordLen);
785 crypt_safe_free(password);
786 return (r < 0) ? r : 0;
789 /* OPTIONS: name, device, key_size, key_file, timeout, tries, flags, icb */
790 int crypt_luksOpen(struct crypt_options *options)
792 struct crypt_device *cd = NULL;
799 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
803 if (options->flags & CRYPT_FLAG_READONLY)
804 flags |= CRYPT_ACTIVATE_READONLY;
806 if (options->flags & CRYPT_FLAG_NON_EXCLUSIVE_ACCESS)
807 flags |= CRYPT_ACTIVATE_NO_UUID;
809 if (options->key_file)
810 r = crypt_activate_by_keyfile(cd, options->name,
811 CRYPT_ANY_SLOT, options->key_file, options->key_size,
814 r = crypt_activate_by_passphrase(cd, options->name,
815 CRYPT_ANY_SLOT, options->passphrase,
816 options->passphrase ? strlen(options->passphrase) : 0,
820 return (r < 0) ? r : 0;
823 /* OPTIONS: device, keys_slot, key_file, timeout, flags, icb */
824 int crypt_luksKillSlot(struct crypt_options *options)
826 struct crypt_device *cd = NULL;
829 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
833 r = luks_remove_helper(cd, options->key_slot, options->key_file, NULL,
834 options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
837 return (r < 0) ? r : 0;
840 /* OPTIONS: device, new_key_file, key_file, timeout, flags, icb */
841 int crypt_luksRemoveKey(struct crypt_options *options)
843 struct crypt_device *cd = NULL;
846 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
850 r = luks_remove_helper(cd, CRYPT_ANY_SLOT, options->key_file, options->new_key_file,
851 options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
854 return (r < 0) ? r : 0;
858 /* OPTIONS: device, new_key_file, key_file, key_slot, flags,
859 iteration_time, timeout, icb */
860 int crypt_luksAddKey(struct crypt_options *options)
862 struct crypt_device *cd = NULL;
865 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
869 if (options->key_file || options->new_key_file)
870 r = crypt_keyslot_add_by_keyfile(cd, options->key_slot,
871 options->key_file, 0,
872 options->new_key_file, 0);
874 r = crypt_keyslot_add_by_passphrase(cd, options->key_slot,
878 return (r < 0) ? r : 0;
881 /* OPTIONS: device, icb */
882 int crypt_luksUUID(struct crypt_options *options)
884 struct crypt_device *cd = NULL;
888 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
892 uuid = (char *)crypt_get_uuid(cd);
893 log_std(cd, uuid ?: "");
899 /* OPTIONS: device, icb */
900 int crypt_isLuks(struct crypt_options *options)
902 struct crypt_device *cd = NULL;
905 log_dbg("Check device %s for LUKS header.", options->device);
911 r = crypt_init(&cd, options->device);
915 /* Do print fail here, no need to crypt_load() */
916 r = LUKS_read_phdr(cd->device, &cd->hdr, 0, cd) ? -EINVAL : 0;
922 /* OPTIONS: device, icb */
923 int crypt_luksDump(struct crypt_options *options)
925 struct crypt_device *cd = NULL;
928 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
938 void crypt_get_error(char *buf, size_t size)
940 const char *error = get_error();
942 if (!buf || size < 1)
945 strncpy(buf, error, size - 1);
946 buf[size - 1] = '\0';
952 void crypt_put_options(struct crypt_options *options)
954 if (options->flags & CRYPT_FLAG_FREE_DEVICE) {
955 free((char *)options->device);
956 options->device = NULL;
957 options->flags &= ~CRYPT_FLAG_FREE_DEVICE;
959 if (options->flags & CRYPT_FLAG_FREE_CIPHER) {
960 free((char *)options->cipher);
961 options->cipher = NULL;
962 options->flags &= ~CRYPT_FLAG_FREE_CIPHER;
966 const char *crypt_get_dir(void)
971 /////////////////////////////////
976 int crypt_init(struct crypt_device **cd, const char *device)
978 struct crypt_device *h = NULL;
983 log_dbg("Allocating crypt device %s context.", device);
985 if (device && !device_ready(NULL, device, O_RDONLY))
988 if (!(h = malloc(sizeof(struct crypt_device))))
991 memset(h, 0, sizeof(*h));
994 h->device = strdup(device);
1002 if (dm_init(h, 1) < 0) {
1007 h->iteration_time = 1000;
1008 h->password_verify = 0;
1010 h->rng_type = crypt_random_default_key_rng();
1015 int crypt_init_by_name(struct crypt_device **cd, const char *name)
1017 crypt_status_info ci;
1018 struct crypt_active_device cad;
1019 char *device = NULL, *cipher_full = NULL, *device_uuid = NULL;
1020 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, &key, NULL, NULL,
1042 /* Underlying device disappeared but mapping still active */
1044 log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
1048 r = crypt_init(cd, device);
1052 /* Try to initialise basic parameters from active device */
1054 if (!strncmp(CRYPT_PLAIN, device_uuid, sizeof(CRYPT_PLAIN)-1)) {
1055 (*cd)->type = strdup(CRYPT_PLAIN);
1056 (*cd)->plain_uuid = strdup(device_uuid);
1057 (*cd)->plain_hdr.hash = NULL; /* no way to get this */
1058 (*cd)->plain_hdr.offset = cad.offset;
1059 (*cd)->plain_hdr.skip = cad.iv_offset;
1060 (*cd)->volume_key = crypt_alloc_volume_key(key_size, key);
1061 if (!(*cd)->volume_key) {
1066 r = crypt_parse_name_and_mode(cipher_full, cipher, cipher_mode);
1068 (*cd)->plain_cipher = strdup(cipher);
1069 (*cd)->plain_cipher_mode = strdup(cipher_mode);
1071 } else if (!strncmp(CRYPT_LUKS1, device_uuid, sizeof(CRYPT_LUKS1)-1)) {
1073 if (crypt_load(*cd, CRYPT_LUKS1, NULL) < 0 ||
1074 crypt_volume_key_verify(*cd, key, key_size) < 0) {
1075 log_dbg("LUKS device header does not match active device.");
1079 (*cd)->volume_key = crypt_alloc_volume_key(key_size, key);
1080 if (!(*cd)->volume_key) {
1087 log_dbg("Active device has no UUID set, some parameters are not set.");
1094 crypt_safe_free(key);
1101 static int _crypt_format_plain(struct crypt_device *cd,
1103 const char *cipher_mode,
1105 size_t volume_key_size,
1106 struct crypt_params_plain *params)
1108 if (!cipher || !cipher_mode) {
1109 log_err(cd, _("Invalid plain crypt parameters.\n"));
1113 if (volume_key_size > 1024) {
1114 log_err(cd, _("Invalid key size.\n"));
1118 cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
1119 if (!cd->volume_key)
1122 cd->plain_cipher = strdup(cipher);
1123 cd->plain_cipher_mode = strdup(cipher_mode);
1126 cd->plain_uuid = strdup(uuid);
1128 if (params && params->hash)
1129 cd->plain_hdr.hash = strdup(params->hash);
1131 cd->plain_hdr.offset = params ? params->offset : 0;
1132 cd->plain_hdr.skip = params ? params->skip : 0;
1134 if (!cd->plain_cipher || !cd->plain_cipher_mode)
1140 static int _crypt_format_luks1(struct crypt_device *cd,
1142 const char *cipher_mode,
1144 const char *volume_key,
1145 size_t volume_key_size,
1146 struct crypt_params_luks1 *params)
1149 unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
1150 unsigned long alignment_offset = 0;
1153 log_err(cd, _("Can't format LUKS without device.\n"));
1158 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
1161 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
1166 if (params && params->data_alignment)
1167 required_alignment = params->data_alignment * SECTOR_SIZE;
1169 get_topology_alignment(cd->device, &required_alignment,
1170 &alignment_offset, DEFAULT_DISK_ALIGNMENT);
1172 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
1173 (params && params->hash) ? params->hash : "sha1",
1175 required_alignment / SECTOR_SIZE,
1176 alignment_offset / SECTOR_SIZE,
1177 cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1181 /* Wipe first 8 sectors - fs magic numbers etc. */
1182 r = wipe_device_header(cd->device, 8);
1185 log_err(cd, _("Cannot format device %s which is still in use.\n"),
1188 log_err(cd, _("Cannot wipe header on device %s.\n"),
1194 r = LUKS_write_phdr(cd->device, &cd->hdr, cd);
1199 int crypt_format(struct crypt_device *cd,
1202 const char *cipher_mode,
1204 const char *volume_key,
1205 size_t volume_key_size,
1210 log_dbg("Formatting device %s as type %s.", cd->device ?: "(none)", cd->type ?: "(none)");
1215 r = init_crypto(cd);
1220 r = _crypt_format_plain(cd, cipher, cipher_mode,
1221 uuid, volume_key_size, params);
1222 else if (isLUKS(type))
1223 r = _crypt_format_luks1(cd, cipher, cipher_mode,
1224 uuid, volume_key, volume_key_size, params);
1226 /* FIXME: allow plugins here? */
1227 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
1231 if (!r && !(cd->type = strdup(type)))
1235 crypt_free_volume_key(cd->volume_key);
1236 cd->volume_key = NULL;
1242 int crypt_load(struct crypt_device *cd,
1243 const char *requested_type,
1246 struct luks_phdr hdr;
1249 log_dbg("Trying to load %s crypt type from device %s.",
1250 requested_type ?: "any", cd->device ?: "(none)");
1255 if (requested_type && !isLUKS(requested_type))
1258 r = init_crypto(cd);
1262 r = LUKS_read_phdr(cd->device, &hdr, 1, cd);
1265 memcpy(&cd->hdr, &hdr, sizeof(hdr));
1266 cd->type = strdup(requested_type);
1274 int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
1276 char *device = NULL, *cipher = NULL, *uuid = NULL, *key = NULL;
1277 uint64_t size, skip, offset;
1278 int key_size, read_only, r;
1280 /* Device context type must be initialised */
1281 if (!cd->type || !crypt_get_uuid(cd))
1284 r = dm_query_device(name, &device, &size, &skip, &offset,
1285 &cipher, &key_size, &key, &read_only, NULL, &uuid);
1287 log_err(NULL, _("Device %s is not active.\n"), name);
1296 r = device_check_and_adjust(cd, device, 0, &new_size, &offset, &read_only);
1300 if (new_size == size) {
1301 log_dbg("Device has already requested size %" PRIu64
1307 log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
1309 r = dm_create_device(name, device, cipher, cd->type,
1310 crypt_get_uuid(cd), new_size, skip, offset,
1311 key_size, key, read_only, 1);
1313 crypt_safe_free(key);
1321 int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
1323 if (!isLUKS(cd->type)) {
1324 log_err(cd, _("This operation is not supported for this device type.\n"));
1328 if (uuid && !strncmp(uuid, cd->hdr.uuid, sizeof(cd->hdr.uuid))) {
1329 log_dbg("UUID is the same as requested (%s) for device %s.",
1335 log_dbg("Requested new UUID change to %s for %s.", uuid, cd->device);
1337 log_dbg("Requested new UUID refresh for %s.", cd->device);
1339 if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
1342 return LUKS_hdr_uuid_set(cd->device, &cd->hdr, uuid, cd);
1345 int crypt_header_backup(struct crypt_device *cd,
1346 const char *requested_type,
1347 const char *backup_file)
1351 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1354 r = init_crypto(cd);
1358 log_dbg("Requested header backup of device %s (%s) to "
1359 "file %s.", cd->device, requested_type, backup_file);
1361 return LUKS_hdr_backup(backup_file, cd->device, &cd->hdr, cd);
1364 int crypt_header_restore(struct crypt_device *cd,
1365 const char *requested_type,
1366 const char *backup_file)
1370 if (requested_type && !isLUKS(requested_type))
1373 /* Some hash functions need initialized gcrypt library */
1374 r = init_crypto(cd);
1378 log_dbg("Requested header restore to device %s (%s) from "
1379 "file %s.", cd->device, requested_type, backup_file);
1381 return LUKS_hdr_restore(backup_file, cd->device, &cd->hdr, cd);
1384 void crypt_free(struct crypt_device *cd)
1387 log_dbg("Releasing crypt device %s context.", cd->device);
1390 crypt_free_volume_key(cd->volume_key);
1395 /* used in plain device only */
1396 free((char*)cd->plain_hdr.hash);
1397 free(cd->plain_cipher);
1398 free(cd->plain_cipher_mode);
1399 free(cd->plain_uuid);
1405 int crypt_suspend(struct crypt_device *cd,
1408 crypt_status_info ci;
1409 int r, suspended = 0;
1411 log_dbg("Suspending volume %s.", name);
1413 ci = crypt_status(NULL, name);
1414 if (ci < CRYPT_ACTIVE) {
1415 log_err(cd, _("Volume %s is not active.\n"), name);
1419 if (!cd && dm_init(NULL, 1) < 0)
1422 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1423 NULL, NULL, NULL, NULL, &suspended, NULL);
1428 log_err(cd, _("Volume %s is already suspended.\n"), name);
1433 r = dm_suspend_and_wipe_key(name);
1435 log_err(cd, "Suspend is not supported for device %s.\n", name);
1437 log_err(cd, "Error during suspending device %s.\n", name);
1444 int crypt_resume_by_passphrase(struct crypt_device *cd,
1447 const char *passphrase,
1448 size_t passphrase_size)
1450 struct volume_key *vk = NULL;
1451 int r, suspended = 0;
1453 log_dbg("Resuming volume %s.", name);
1455 if (!isLUKS(cd->type)) {
1456 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1461 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1462 NULL, NULL, NULL, NULL, &suspended, NULL);
1467 log_err(cd, _("Volume %s is not suspended.\n"), name);
1472 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1473 passphrase_size, &cd->hdr, &vk, cd);
1475 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1479 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1481 log_err(cd, "Resume is not supported for device %s.\n", name);
1483 log_err(cd, "Error during resuming device %s.\n", name);
1487 crypt_free_volume_key(vk);
1488 return r < 0 ? r : keyslot;
1491 int crypt_resume_by_keyfile(struct crypt_device *cd,
1494 const char *keyfile,
1495 size_t keyfile_size)
1497 struct volume_key *vk = NULL;
1498 char *passphrase_read = NULL;
1499 unsigned int passphrase_size_read;
1500 int r, suspended = 0;
1502 log_dbg("Resuming volume %s.", name);
1504 if (!isLUKS(cd->type)) {
1505 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1510 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1511 NULL, NULL, NULL, NULL, &suspended, NULL);
1516 log_err(cd, _("Volume %s is not suspended.\n"), name);
1523 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1524 &passphrase_size_read, keyfile, keyfile_size);
1528 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1529 passphrase_size_read, &cd->hdr, &vk, cd);
1534 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1536 log_err(cd, "Error during resuming device %s.\n", name);
1538 crypt_safe_free(passphrase_read);
1539 crypt_free_volume_key(vk);
1540 return r < 0 ? r : keyslot;
1543 // slot manipulation
1544 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1545 int keyslot, // -1 any
1546 const char *passphrase, // NULL -> terminal
1547 size_t passphrase_size,
1548 const char *new_passphrase, // NULL -> terminal
1549 size_t new_passphrase_size)
1551 struct volume_key *vk = NULL;
1552 char *password = NULL, *new_password = NULL;
1553 unsigned int passwordLen, new_passwordLen;
1556 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1557 "new passphrase %sprovided.",
1558 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1560 if (!isLUKS(cd->type)) {
1561 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1565 r = keyslot_verify_or_find_empty(cd, &keyslot);
1569 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1570 /* No slots used, try to use pre-generated key in header */
1571 if (cd->volume_key) {
1572 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1573 r = vk ? 0 : -ENOMEM;
1575 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1578 } else if (passphrase) {
1579 /* Passphrase provided, use it to unlock existing keyslot */
1580 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, passphrase,
1581 passphrase_size, &cd->hdr, &vk, cd);
1583 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1584 r = key_from_terminal(cd, _("Enter any passphrase: "),
1585 &password, &passwordLen, 0);
1589 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
1590 passwordLen, &cd->hdr, &vk, cd);
1591 crypt_safe_free(password);
1597 if (new_passphrase) {
1598 new_password = (char *)new_passphrase;
1599 new_passwordLen = new_passphrase_size;
1601 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1602 &new_password, &new_passwordLen, 1);
1607 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1608 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1613 if (!new_passphrase)
1614 crypt_safe_free(new_password);
1615 crypt_free_volume_key(vk);
1616 return r ?: keyslot;
1619 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1621 const char *keyfile,
1622 size_t keyfile_size,
1623 const char *new_keyfile,
1624 size_t new_keyfile_size)
1626 struct volume_key *vk = NULL;
1627 char *password = NULL; unsigned int passwordLen;
1628 char *new_password = NULL; unsigned int new_passwordLen;
1631 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1632 keyfile ?: "[none]", new_keyfile ?: "[none]");
1634 if (!isLUKS(cd->type)) {
1635 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1639 r = keyslot_verify_or_find_empty(cd, &keyslot);
1643 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1644 /* No slots used, try to use pre-generated key in header */
1645 if (cd->volume_key) {
1646 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1647 r = vk ? 0 : -ENOMEM;
1649 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1653 /* Read password from file of (if NULL) from terminal */
1655 r = key_from_file(cd, _("Enter any passphrase: "),
1656 &password, &passwordLen,
1657 keyfile, keyfile_size);
1659 r = key_from_terminal(cd, _("Enter any passphrase: "),
1660 &password, &passwordLen, 0);
1664 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password, passwordLen,
1672 r = key_from_file(cd, _("Enter new passphrase for key slot: "),
1673 &new_password, &new_passwordLen, new_keyfile,
1676 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1677 &new_password, &new_passwordLen, 1);
1681 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1682 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1684 crypt_safe_free(password);
1685 crypt_safe_free(new_password);
1686 crypt_free_volume_key(vk);
1687 return r < 0 ? r : keyslot;
1690 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1692 const char *volume_key,
1693 size_t volume_key_size,
1694 const char *passphrase,
1695 size_t passphrase_size)
1697 struct volume_key *vk = NULL;
1699 char *new_password = NULL; unsigned int new_passwordLen;
1701 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1703 if (!isLUKS(cd->type)) {
1704 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1709 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1710 else if (cd->volume_key)
1711 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1716 r = LUKS_verify_volume_key(&cd->hdr, vk);
1718 log_err(cd, _("Volume key does not match the volume.\n"));
1722 r = keyslot_verify_or_find_empty(cd, &keyslot);
1727 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1728 &new_password, &new_passwordLen, 1);
1731 passphrase = new_password;
1732 passphrase_size = new_passwordLen;
1735 r = LUKS_set_key(cd->device, keyslot, passphrase, passphrase_size,
1736 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1738 crypt_safe_free(new_password);
1739 crypt_free_volume_key(vk);
1740 return (r < 0) ? r : keyslot;
1743 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1745 crypt_keyslot_info ki;
1747 log_dbg("Destroying keyslot %d.", keyslot);
1749 if (!isLUKS(cd->type)) {
1750 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1754 ki = crypt_keyslot_status(cd, keyslot);
1755 if (ki == CRYPT_SLOT_INVALID) {
1756 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1760 if (ki == CRYPT_SLOT_INACTIVE) {
1761 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1765 return LUKS_del_key(cd->device, keyslot, &cd->hdr, cd);
1768 // activation/deactivation of device mapping
1769 int crypt_activate_by_passphrase(struct crypt_device *cd,
1772 const char *passphrase,
1773 size_t passphrase_size,
1776 crypt_status_info ci;
1777 struct volume_key *vk = NULL;
1778 char *read_passphrase = NULL;
1781 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1782 name ? "Activating" : "Checking", name ?: "",
1783 keyslot, passphrase ? "" : "[none] ");
1786 ci = crypt_status(NULL, name);
1787 if (ci == CRYPT_INVALID)
1789 else if (ci >= CRYPT_ACTIVE) {
1790 log_err(cd, _("Device %s already exists.\n"), name);
1795 /* plain, use hashed passphrase */
1796 if (isPLAIN(cd->type)) {
1798 r = key_from_terminal(cd, NULL, &read_passphrase,
1799 &passphrase_size, 0);
1802 passphrase = read_passphrase;
1804 r = create_device_helper(cd, name, cd->plain_hdr.hash,
1805 cd->plain_cipher, cd->plain_cipher_mode,
1806 NULL, passphrase, passphrase_size,
1807 cd->volume_key->keylength, 0,
1808 cd->plain_hdr.skip, cd->plain_hdr.offset,
1810 flags & CRYPT_ACTIVATE_READONLY, 0, 0);
1812 } else if (isLUKS(cd->type)) {
1813 /* provided passphrase, do not retry */
1815 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1816 passphrase_size, &cd->hdr, &vk, cd);
1818 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1823 r = open_from_hdr_and_vk(cd, vk, name, flags);
1828 crypt_safe_free(read_passphrase);
1829 crypt_free_volume_key(vk);
1831 return r < 0 ? r : keyslot;
1834 int crypt_activate_by_keyfile(struct crypt_device *cd,
1837 const char *keyfile,
1838 size_t keyfile_size,
1841 crypt_status_info ci;
1842 struct volume_key *vk = NULL;
1843 char *passphrase_read = NULL;
1844 unsigned int passphrase_size_read;
1847 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1848 name ?: "", keyslot, keyfile ?: "[none]");
1850 if (!isLUKS(cd->type)) {
1851 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1856 ci = crypt_status(NULL, name);
1857 if (ci == CRYPT_INVALID)
1859 else if (ci >= CRYPT_ACTIVE) {
1860 log_err(cd, _("Device %s already exists.\n"), name);
1868 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1869 &passphrase_size_read, keyfile, keyfile_size);
1873 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1874 passphrase_size_read, &cd->hdr, &vk, cd);
1880 r = open_from_hdr_and_vk(cd, vk, name, flags);
1882 crypt_safe_free(passphrase_read);
1883 crypt_free_volume_key(vk);
1885 return r < 0 ? r : keyslot;
1888 int crypt_activate_by_volume_key(struct crypt_device *cd,
1890 const char *volume_key,
1891 size_t volume_key_size,
1894 crypt_status_info ci;
1895 struct volume_key *vk;
1898 log_dbg("Activating volume %s by volume key.", name);
1900 /* use key directly, no hash */
1901 if (isPLAIN(cd->type)) {
1902 if (!volume_key || !volume_key_size || !cd->volume_key ||
1903 volume_key_size != cd->volume_key->keylength) {
1904 log_err(cd, _("Incorrect volume key specified for plain device.\n"));
1908 return create_device_helper(cd, name, NULL,
1909 cd->plain_cipher, cd->plain_cipher_mode, NULL, volume_key, volume_key_size,
1910 cd->volume_key->keylength, 0, cd->plain_hdr.skip,
1911 cd->plain_hdr.offset, cd->plain_uuid, flags & CRYPT_ACTIVATE_READONLY, 0, 0);
1914 if (!isLUKS(cd->type)) {
1915 log_err(cd, _("Device type is not properly initialised.\n"));
1920 ci = crypt_status(NULL, name);
1921 if (ci == CRYPT_INVALID)
1923 else if (ci >= CRYPT_ACTIVE) {
1924 log_err(cd, _("Device %s already exists.\n"), name);
1929 /* If key is not provided, try to use internal key */
1931 if (!cd->volume_key) {
1932 log_err(cd, _("Volume key does not match the volume.\n"));
1935 volume_key_size = cd->volume_key->keylength;
1936 volume_key = cd->volume_key->key;
1939 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1942 r = LUKS_verify_volume_key(&cd->hdr, vk);
1945 log_err(cd, _("Volume key does not match the volume.\n"));
1948 r = open_from_hdr_and_vk(cd, vk, name, flags);
1950 crypt_free_volume_key(vk);
1955 int crypt_deactivate(struct crypt_device *cd, const char *name)
1962 log_dbg("Deactivating volume %s.", name);
1964 if (!cd && dm_init(NULL, 1) < 0)
1967 switch (crypt_status(cd, name)) {
1969 r = dm_remove_device(name, 0, 0);
1972 log_err(cd, _("Device %s is busy.\n"), name);
1975 case CRYPT_INACTIVE:
1976 log_err(cd, _("Device %s is not active.\n"), name);
1980 log_err(cd, _("Invalid device %s.\n"), name);
1990 int crypt_volume_key_get(struct crypt_device *cd,
1993 size_t *volume_key_size,
1994 const char *passphrase,
1995 size_t passphrase_size)
1997 struct volume_key *vk;
1998 char *processed_key = NULL;
2001 key_len = crypt_get_volume_key_size(cd);
2002 if (key_len > *volume_key_size) {
2003 log_err(cd, _("Volume key buffer too small.\n"));
2007 if (isPLAIN(cd->type) && cd->plain_hdr.hash) {
2008 processed_key = process_key(cd, cd->plain_hdr.hash, NULL, key_len,
2009 passphrase, passphrase_size);
2010 if (!processed_key) {
2011 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
2014 memcpy(volume_key, processed_key, key_len);
2015 *volume_key_size = key_len;
2016 crypt_safe_free(processed_key);
2020 if (isLUKS(cd->type)) {
2021 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
2022 passphrase_size, &cd->hdr, &vk, cd);
2025 memcpy(volume_key, vk->key, vk->keylength);
2026 *volume_key_size = vk->keylength;
2029 crypt_free_volume_key(vk);
2033 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
2037 int crypt_volume_key_verify(struct crypt_device *cd,
2038 const char *volume_key,
2039 size_t volume_key_size)
2041 struct volume_key *vk;
2044 if (!isLUKS(cd->type)) {
2045 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2049 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2053 r = LUKS_verify_volume_key(&cd->hdr, vk);
2056 log_err(cd, _("Volume key does not match the volume.\n"));
2058 crypt_free_volume_key(vk);
2063 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
2065 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
2066 cd->timeout = timeout_sec;
2069 void crypt_set_password_retry(struct crypt_device *cd, int tries)
2071 log_dbg("Password retry count set to %d.", tries);
2075 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2077 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
2078 cd->iteration_time = iteration_time_ms;
2081 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
2083 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
2084 cd->password_verify = password_verify ? 1 : 0;
2087 void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
2090 case CRYPT_RNG_URANDOM:
2091 case CRYPT_RNG_RANDOM:
2092 log_dbg("RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
2093 cd->rng_type = rng_type;
2097 int crypt_get_rng_type(struct crypt_device *cd)
2102 return cd->rng_type;
2105 int crypt_memory_lock(struct crypt_device *cd, int lock)
2107 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
2111 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
2115 if (!cd && dm_init(NULL, 1) < 0)
2116 return CRYPT_INVALID;
2118 r = dm_status_device(name);
2123 if (r < 0 && r != -ENODEV)
2124 return CRYPT_INVALID;
2127 return CRYPT_ACTIVE;
2132 return CRYPT_INACTIVE;
2135 static void hexprintICB(struct crypt_device *cd, char *d, int n)
2138 for(i = 0; i < n; i++)
2139 log_std(cd, "%02hhx ", (char)d[i]);
2142 int crypt_dump(struct crypt_device *cd)
2145 if (!isLUKS(cd->type)) { //FIXME
2146 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2150 log_std(cd, "LUKS header information for %s\n\n", cd->device);
2151 log_std(cd, "Version: \t%d\n", cd->hdr.version);
2152 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
2153 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
2154 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec);
2155 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
2156 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
2157 log_std(cd, "MK digest: \t");
2158 hexprintICB(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE);
2160 log_std(cd, "MK salt: \t");
2161 hexprintICB(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2);
2162 log_std(cd, "\n \t");
2163 hexprintICB(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
2165 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
2166 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
2167 for(i = 0; i < LUKS_NUMKEYS; i++) {
2168 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
2169 log_std(cd, "Key Slot %d: ENABLED\n",i);
2170 log_std(cd, "\tIterations: \t%d\n",
2171 cd->hdr.keyblock[i].passwordIterations);
2172 log_std(cd, "\tSalt: \t");
2173 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt,
2175 log_std(cd, "\n\t \t");
2176 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt +
2177 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
2180 log_std(cd, "\tKey material offset:\t%d\n",
2181 cd->hdr.keyblock[i].keyMaterialOffset);
2182 log_std(cd, "\tAF stripes: \t%d\n",
2183 cd->hdr.keyblock[i].stripes);
2186 log_std(cd, "Key Slot %d: DISABLED\n", i);
2192 const char *crypt_get_cipher(struct crypt_device *cd)
2194 if (isPLAIN(cd->type))
2195 return cd->plain_cipher;
2197 if (isLUKS(cd->type))
2198 return cd->hdr.cipherName;
2203 const char *crypt_get_cipher_mode(struct crypt_device *cd)
2205 if (isPLAIN(cd->type))
2206 return cd->plain_cipher_mode;
2208 if (isLUKS(cd->type))
2209 return cd->hdr.cipherMode;
2214 const char *crypt_get_uuid(struct crypt_device *cd)
2216 if (isLUKS(cd->type))
2217 return cd->hdr.uuid;
2219 if (isPLAIN(cd->type))
2220 return cd->plain_uuid;
2225 const char *crypt_get_device_name(struct crypt_device *cd)
2230 int crypt_get_volume_key_size(struct crypt_device *cd)
2232 if (isPLAIN(cd->type) && cd->volume_key)
2233 return cd->volume_key->keylength;
2235 if (isLUKS(cd->type))
2236 return cd->hdr.keyBytes;
2241 uint64_t crypt_get_data_offset(struct crypt_device *cd)
2243 if (isPLAIN(cd->type))
2244 return cd->plain_hdr.offset;
2246 if (isLUKS(cd->type))
2247 return cd->hdr.payloadOffset;
2252 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
2254 if (!isLUKS(cd->type)) {
2255 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2256 return CRYPT_SLOT_INVALID;
2259 return LUKS_keyslot_info(&cd->hdr, keyslot);
2262 int crypt_keyslot_max(const char *type)
2264 if (type && isLUKS(type))
2265 return LUKS_NUMKEYS;
2270 const char *crypt_get_type(struct crypt_device *cd)
2275 int crypt_get_active_device(struct crypt_device *cd,
2277 struct crypt_active_device *cad)
2281 r = dm_query_device(name, NULL, &cad->size, &cad->iv_offset, &cad->offset,
2282 NULL, NULL, NULL, &readonly, NULL, NULL);
2286 cad->flags = readonly ? CRYPT_ACTIVATE_READONLY : 0;