8 #include "libcryptsetup.h"
11 #include "crypto_backend.h"
17 struct volume_key *volume_key;
19 uint64_t iteration_time;
24 /* used in CRYPT_LUKS1 */
26 uint64_t PBKDF2_per_sec;
28 /* used in CRYPT_PLAIN */
29 struct crypt_params_plain plain_hdr;
31 char *plain_cipher_mode;
34 /* callbacks definitions */
35 void (*log)(int level, const char *msg, void *usrptr);
37 int (*confirm)(const char *msg, void *usrptr);
39 int (*password)(const char *msg, char *buf, size_t length, void *usrptr);
40 void *password_usrptr;
44 static void (*_default_log)(int level, const char *msg, void *usrptr) = NULL;
45 static int _debug_level = 0;
47 void crypt_set_debug_level(int level)
52 int crypt_get_debug_level()
57 void crypt_log(struct crypt_device *cd, int level, const char *msg)
60 cd->log(level, msg, cd->log_usrptr);
61 else if (_default_log)
62 _default_log(level, msg, NULL);
65 void logger(struct crypt_device *cd, int level, const char *file,
66 int line, const char *format, ...)
71 va_start(argp, format);
73 if (vasprintf(&target, format, argp) > 0) {
75 crypt_log(cd, level, target);
77 } else if (_debug_level)
78 printf("# %s:%d %s\n", file ?: "?", line, target);
80 } else if (_debug_level)
81 printf("# %s\n", target);
89 static int init_crypto(struct crypt_device *ctx)
93 r = crypt_random_init(ctx);
95 log_err(ctx, _("Cannot initialize crypto RNG backend.\n"));
99 r = crypt_backend_init();
101 log_err(ctx, _("Cannot initialize crypto backend.\n"));
107 * Password processing behaviour matrix of process_key
109 * from binary file: check if there is sufficently large key material
110 * interactive & from fd: hash if requested, otherwise crop or pad with '0'
112 static char *process_key(struct crypt_device *cd, const char *hash_name,
113 const char *key_file, size_t key_size,
114 const char *pass, size_t passLen)
121 key = crypt_safe_alloc(key_size);
122 memset(key, 0, key_size);
124 /* key is coming from binary file */
125 if (key_file && strcmp(key_file, "-")) {
126 if(passLen < key_size) {
127 log_err(cd, _("Cannot not read %d bytes from key file %s.\n"),
129 crypt_safe_free(key);
132 memcpy(key, pass, key_size);
136 /* key is coming from tty, fd or binary stdin */
138 if (crypt_plain_hash(cd, hash_name, key, key_size, pass, passLen) < 0) {
139 log_err(cd, _("Key processing error (using hash algorithm %s).\n"),
141 crypt_safe_free(key);
144 } else if (passLen > key_size) {
145 memcpy(key, pass, key_size);
147 memcpy(key, pass, passLen);
153 static int isPLAIN(const char *type)
155 return (type && !strcmp(CRYPT_PLAIN, type));
158 static int isLUKS(const char *type)
160 return (type && !strcmp(CRYPT_LUKS1, type));
163 /* keyslot helpers */
164 static int keyslot_verify_or_find_empty(struct crypt_device *cd, int *keyslot)
166 if (*keyslot == CRYPT_ANY_SLOT) {
167 *keyslot = LUKS_keyslot_find_empty(&cd->hdr);
169 log_err(cd, _("All key slots full.\n"));
174 switch (LUKS_keyslot_info(&cd->hdr, *keyslot)) {
175 case CRYPT_SLOT_INVALID:
176 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
177 *keyslot, LUKS_NUMKEYS - 1);
179 case CRYPT_SLOT_INACTIVE:
182 log_err(cd, _("Key slot %d is full, please select another one.\n"),
190 static int verify_other_keyslot(struct crypt_device *cd,
191 const char *key_file,
194 struct volume_key *vk = NULL;
195 crypt_keyslot_info ki;
197 char *password = NULL;
198 unsigned int passwordLen;
200 r = crypt_get_key(_("Enter any remaining LUKS passphrase: "),
201 &password, &passwordLen, 0, key_file, cd->timeout,
202 cd->password_verify, cd);
206 ki = crypt_keyslot_status(cd, keyIndex);
207 if (ki == CRYPT_SLOT_ACTIVE) /* Not last slot */
208 LUKS_keyslot_set(&cd->hdr, keyIndex, 0);
210 openedIndex = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT,
211 password, passwordLen,
214 if (ki == CRYPT_SLOT_ACTIVE)
215 LUKS_keyslot_set(&cd->hdr, keyIndex, 1);
220 log_verbose(cd, _("Key slot %d verified.\n"), openedIndex);
222 crypt_free_volume_key(vk);
223 crypt_safe_free(password);
227 static int find_keyslot_by_passphrase(struct crypt_device *cd,
228 const char *key_file,
231 struct volume_key *vk = NULL;
232 char *password = NULL;
233 unsigned int passwordLen;
236 r = crypt_get_key(message,&password,&passwordLen, 0, key_file,
237 cd->timeout, cd->password_verify, cd);
241 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
242 passwordLen, &cd->hdr, &vk, cd);
244 crypt_free_volume_key(vk);
245 crypt_safe_free(password);
249 static int device_check_and_adjust(struct crypt_device *cd,
256 int r, real_readonly;
262 r = get_device_infos(device, open_exclusive, &real_readonly, &real_size);
265 log_err(cd, _("Cannot use device %s which is in use "
266 "(already mapped or mounted).\n"),
269 log_err(cd, _("Cannot get info about device %s.\n"),
277 log_err(cd, _("Device %s has zero size.\n"), device);
280 if (*size < *offset) {
281 log_err(cd, _("Device %s is too small.\n"), device);
290 log_dbg("Calculated device size is %" PRIu64 " sectors (%s), offset %" PRIu64 ".",
291 *size, *read_only ? "RO" : "RW", *offset);
295 static int luks_remove_helper(struct crypt_device *cd,
297 const char *other_key_file,
298 const char *key_file,
301 crypt_keyslot_info ki;
304 if (key_slot == CRYPT_ANY_SLOT) {
305 key_slot = find_keyslot_by_passphrase(cd, key_file,
306 _("Enter LUKS passphrase to be deleted: "));
312 log_std(cd, _("Key slot %d selected for deletion.\n"), key_slot);
315 ki = crypt_keyslot_status(cd, key_slot);
316 if (ki == CRYPT_SLOT_INVALID) {
317 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
318 key_slot, LUKS_NUMKEYS - 1);
322 if (ki <= CRYPT_SLOT_INACTIVE) {
323 log_err(cd, _("Key %d not active. Can't wipe.\n"), key_slot);
328 if (ki == CRYPT_SLOT_ACTIVE_LAST && cd->confirm &&
329 !(cd->confirm(_("This is the last keyslot."
330 " Device will become unusable after purging this key."),
331 cd->confirm_usrptr))) {
337 r = verify_other_keyslot(cd, other_key_file, key_slot);
342 r = crypt_keyslot_destroy(cd, key_slot);
344 return (r < 0) ? r : 0;
347 static int create_device_helper(struct crypt_device *cd,
351 const char *cipher_mode,
352 const char *key_file,
364 crypt_status_info ci;
365 char *dm_cipher = NULL;
366 char *processed_key = NULL;
372 ci = crypt_status(cd, name);
373 if (ci == CRYPT_INVALID)
376 if (reload && ci < CRYPT_ACTIVE)
379 if (!reload && ci >= CRYPT_ACTIVE) {
380 log_err(cd, _("Device %s already exists.\n"), name);
384 if (key_size < 0 || key_size > 1024) {
385 log_err(cd, _("Invalid key size %d.\n"), key_size);
389 r = device_check_and_adjust(cd, cd->device, !reload, &size, &offset, &read_only);
393 if (cipher_mode && asprintf(&dm_cipher, "%s-%s", cipher, cipher_mode) < 0)
396 processed_key = process_key(cd, hash, key_file, key_size, key, keyLen);
397 if (!processed_key) {
402 r = dm_create_device(name, cd->device, dm_cipher ?: cipher, cd->type, uuid, size, skip, offset,
403 key_size, processed_key, read_only, reload);
406 crypt_safe_free(processed_key);
410 static int open_from_hdr_and_vk(struct crypt_device *cd,
411 struct volume_key *vk,
415 uint64_t size, offset;
417 int read_only, no_uuid, r;
420 offset = crypt_get_data_offset(cd);
421 read_only = flags & CRYPT_ACTIVATE_READONLY;
422 no_uuid = flags & CRYPT_ACTIVATE_NO_UUID;
424 r = device_check_and_adjust(cd, cd->device, 1, &size, &offset, &read_only);
428 if (asprintf(&cipher, "%s-%s", crypt_get_cipher(cd),
429 crypt_get_cipher_mode(cd)) < 0)
432 r = dm_create_device(name, cd->device, cipher, cd->type,
433 no_uuid ? NULL : crypt_get_uuid(cd),
434 size, 0, offset, vk->keylength, vk->key,
440 static void log_wrapper(int level, const char *msg, void *usrptr)
442 void (*xlog)(int level, char *msg) = usrptr;
443 xlog(level, (char *)msg);
446 static int yesDialog_wrapper(const char *msg, void *usrptr)
448 int (*xyesDialog)(char *msg) = usrptr;
449 return xyesDialog((char*)msg);
452 int crypt_confirm(struct crypt_device *cd, const char *msg)
454 if (!cd || !cd->confirm)
457 return cd->confirm(msg, cd->confirm_usrptr);
460 static int key_from_terminal(struct crypt_device *cd, char *msg, char **key,
461 unsigned int *key_len, int force_verify)
467 if(!msg && asprintf(&prompt, _("Enter passphrase for %s: "),
475 *key = crypt_safe_alloc(MAX_TTY_PASSWORD_LEN);
480 r = cd->password(msg, *key, MAX_TTY_PASSWORD_LEN, cd->password_usrptr);
482 crypt_safe_free(*key);
487 r = crypt_get_key(msg, key, key_len, 0, NULL, cd->timeout,
488 (force_verify || cd->password_verify), cd);
491 return (r < 0) ? r: 0;
494 static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslot,
495 struct volume_key **vk)
497 char *passphrase_read = NULL;
498 unsigned int passphrase_size_read;
499 int r = -EINVAL, tries = cd->tries;
503 crypt_free_volume_key(*vk);
506 r = key_from_terminal(cd, NULL, &passphrase_read,
507 &passphrase_size_read, 0);
511 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
512 passphrase_size_read, &cd->hdr, vk, cd);
513 crypt_safe_free(passphrase_read);
514 passphrase_read = NULL;
515 } while (r == -EPERM && (--tries > 0));
518 crypt_free_volume_key(*vk);
522 crypt_safe_free(passphrase_read);
526 static int key_from_file(struct crypt_device *cd, char *msg,
527 char **key, unsigned int *key_len,
528 const char *key_file, size_t key_size)
530 return crypt_get_key(msg, key, key_len, key_size, key_file,
534 static int _crypt_init(struct crypt_device **cd,
536 struct crypt_options *options,
537 int load, int need_dm)
541 /* if it is plain device and mapping table is being reloaded
542 initialize it by name*/
543 init_by_name = (type && !strcmp(type, CRYPT_PLAIN) && load);
545 /* Some of old API calls do not require DM in kernel,
546 fake initialisation by initialise it with kernel_check disabled */
548 (void)dm_init(NULL, 0);
550 r = crypt_init_by_name(cd, options->name);
552 r = crypt_init(cd, options->device);
559 crypt_set_log_callback(*cd, log_wrapper, options->icb->log);
560 crypt_set_confirm_callback(*cd, yesDialog_wrapper, options->icb->yesDialog);
562 crypt_set_timeout(*cd, options->timeout);
563 crypt_set_password_retry(*cd, options->tries);
564 crypt_set_iterarion_time(*cd, options->iteration_time ?: 1000);
565 crypt_set_password_verify(*cd, options->flags & CRYPT_FLAG_VERIFY);
567 if (load && !init_by_name)
568 r = crypt_load(*cd, type, NULL);
570 if (!r && type && !(*cd)->type) {
571 (*cd)->type = strdup(type);
582 void crypt_set_log_callback(struct crypt_device *cd,
583 void (*log)(int level, const char *msg, void *usrptr),
590 cd->log_usrptr = usrptr;
594 void crypt_set_confirm_callback(struct crypt_device *cd,
595 int (*confirm)(const char *msg, void *usrptr),
598 cd->confirm = confirm;
599 cd->confirm_usrptr = usrptr;
602 void crypt_set_password_callback(struct crypt_device *cd,
603 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
606 cd->password = password;
607 cd->password_usrptr = usrptr;
610 /* OPTIONS: name, cipher, device, hash, key_file, key_size, key_slot,
611 * offset, size, skip, timeout, tries, passphrase_fd (ignored),
613 static int crypt_create_and_update_device(struct crypt_options *options, int update)
615 struct crypt_device *cd = NULL;
620 r = _crypt_init(&cd, CRYPT_PLAIN, options, 0, 1);
624 r = crypt_get_key(_("Enter passphrase: "), &key, &keyLen, options->key_size,
625 options->key_file, cd->timeout, cd->password_verify, cd);
627 r = create_device_helper(cd, options->name, options->hash,
628 options->cipher, NULL, options->key_file, key, keyLen,
629 options->key_size, options->size, options->skip,
630 options->offset, NULL, options->flags & CRYPT_FLAG_READONLY,
631 options->flags, update);
633 crypt_safe_free(key);
638 int crypt_create_device(struct crypt_options *options)
640 return crypt_create_and_update_device(options, 0);
643 int crypt_update_device(struct crypt_options *options)
645 return crypt_create_and_update_device(options, 1);
648 /* OPTIONS: name, size, icb */
649 int crypt_resize_device(struct crypt_options *options)
651 struct crypt_device *cd = NULL;
652 char *device = NULL, *cipher = NULL, *uuid = NULL, *key = NULL;
654 uint64_t size, skip, offset;
655 int key_size, read_only, r;
657 log_dbg("Resizing device %s to %" PRIu64 " sectors.", options->name, options->size);
659 if (dm_init(NULL, 1) < 0)
662 r = dm_query_device(options->name, &device, &size, &skip, &offset,
663 &cipher, &key_size, &key, &read_only, NULL, &uuid);
665 log_err(NULL, _("Device %s is not active.\n"), options->name);
669 /* Try to determine type of device from UUID */
672 if (!strncmp(uuid, CRYPT_PLAIN, strlen(CRYPT_PLAIN))) {
676 } else if (!strncmp(uuid, CRYPT_LUKS1, strlen(CRYPT_LUKS1)))
680 if (!options->device)
681 options->device = device;
683 r = _crypt_init(&cd, type, options, 1, 1);
687 size = options->size;
688 r = device_check_and_adjust(cd, device, 0, &size, &offset, &read_only);
692 r = dm_create_device(options->name, device, cipher, type,
693 crypt_get_uuid(cd), size, skip, offset,
694 key_size, key, read_only, 1);
696 crypt_safe_free(key);
698 if (options->device == device)
699 options->device = NULL;
707 /* OPTIONS: name, icb */
708 int crypt_query_device(struct crypt_options *options)
712 log_dbg("Query device %s.", options->name);
714 if (dm_init(NULL, 1) < 0)
717 r = dm_status_device(options->name);
721 r = dm_query_device(options->name, (char **)&options->device, &options->size,
722 &options->skip, &options->offset, (char **)&options->cipher,
723 &options->key_size, NULL, &read_only, NULL, NULL);
726 options->flags |= CRYPT_FLAG_READONLY;
728 options->flags |= CRYPT_FLAG_FREE_DEVICE;
729 options->flags |= CRYPT_FLAG_FREE_CIPHER;
741 /* OPTIONS: name, icb */
742 int crypt_remove_device(struct crypt_options *options)
744 struct crypt_device *cd = NULL;
747 r = crypt_init_by_name(&cd, options->name);
749 r = crypt_deactivate(cd, options->name);
756 /* OPTIONS: device, cipher, hash, align_payload, key_size (master key), key_slot
757 * new_key_file, iteration_time, timeout, flags, icb */
758 int crypt_luksFormat(struct crypt_options *options)
760 char cipherName[LUKS_CIPHERNAME_L];
761 char cipherMode[LUKS_CIPHERMODE_L];
763 unsigned int passwordLen;
764 struct crypt_device *cd = NULL;
765 struct crypt_params_luks1 cp = {
766 .hash = options->hash,
767 .data_alignment = options->align_payload
771 r = crypt_parse_name_and_mode(options->cipher, cipherName, cipherMode);
773 log_err(cd, _("No known cipher specification pattern detected.\n"));
777 if ((r = _crypt_init(&cd, CRYPT_LUKS1, options, 0, 1)))
780 if (options->key_slot >= LUKS_NUMKEYS && options->key_slot != CRYPT_ANY_SLOT) {
781 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
782 options->key_slot, LUKS_NUMKEYS - 1);
787 r = crypt_get_key(_("Enter LUKS passphrase: "), &password, &passwordLen, 0,
788 options->new_key_file, cd->timeout, cd->password_verify, cd);
793 r = crypt_format(cd, CRYPT_LUKS1, cipherName, cipherMode,
794 NULL, NULL, options->key_size, &cp);
798 /* Add keyslot using internally stored volume key generated during format */
799 r = crypt_keyslot_add_by_volume_key(cd, options->key_slot, NULL, 0,
800 password, passwordLen);
803 crypt_safe_free(password);
804 return (r < 0) ? r : 0;
807 /* OPTIONS: name, device, key_size, key_file, timeout, tries, flags, icb */
808 int crypt_luksOpen(struct crypt_options *options)
810 struct crypt_device *cd = NULL;
817 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
821 if (options->flags & CRYPT_FLAG_READONLY)
822 flags |= CRYPT_ACTIVATE_READONLY;
824 if (options->flags & CRYPT_FLAG_NON_EXCLUSIVE_ACCESS)
825 flags |= CRYPT_ACTIVATE_NO_UUID;
827 if (options->key_file)
828 r = crypt_activate_by_keyfile(cd, options->name,
829 CRYPT_ANY_SLOT, options->key_file, options->key_size,
832 r = crypt_activate_by_passphrase(cd, options->name,
833 CRYPT_ANY_SLOT, options->passphrase,
834 options->passphrase ? strlen(options->passphrase) : 0,
838 return (r < 0) ? r : 0;
841 /* OPTIONS: device, keys_slot, key_file, timeout, flags, icb */
842 int crypt_luksKillSlot(struct crypt_options *options)
844 struct crypt_device *cd = NULL;
847 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
851 r = luks_remove_helper(cd, options->key_slot, options->key_file, NULL,
852 options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
855 return (r < 0) ? r : 0;
858 /* OPTIONS: device, new_key_file, key_file, timeout, flags, icb */
859 int crypt_luksRemoveKey(struct crypt_options *options)
861 struct crypt_device *cd = NULL;
864 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
868 r = luks_remove_helper(cd, CRYPT_ANY_SLOT, options->key_file, options->new_key_file,
869 options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
872 return (r < 0) ? r : 0;
876 /* OPTIONS: device, new_key_file, key_file, key_slot, flags,
877 iteration_time, timeout, icb */
878 int crypt_luksAddKey(struct crypt_options *options)
880 struct crypt_device *cd = NULL;
883 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
887 if (options->key_file || options->new_key_file)
888 r = crypt_keyslot_add_by_keyfile(cd, options->key_slot,
889 options->key_file, 0,
890 options->new_key_file, 0);
892 r = crypt_keyslot_add_by_passphrase(cd, options->key_slot,
896 return (r < 0) ? r : 0;
899 /* OPTIONS: device, icb */
900 int crypt_luksUUID(struct crypt_options *options)
902 struct crypt_device *cd = NULL;
906 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
910 uuid = (char *)crypt_get_uuid(cd);
911 log_std(cd, uuid ?: "");
917 /* OPTIONS: device, icb */
918 int crypt_isLuks(struct crypt_options *options)
920 struct crypt_device *cd = NULL;
923 log_dbg("Check device %s for LUKS header.", options->device);
929 r = crypt_init(&cd, options->device);
933 /* Do print fail here, no need to crypt_load() */
934 r = LUKS_read_phdr(cd->device, &cd->hdr, 0, cd) ? -EINVAL : 0;
940 /* OPTIONS: device, icb */
941 int crypt_luksDump(struct crypt_options *options)
943 struct crypt_device *cd = NULL;
946 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
956 void crypt_get_error(char *buf, size_t size)
958 const char *error = get_error();
960 if (!buf || size < 1)
963 strncpy(buf, error, size - 1);
964 buf[size - 1] = '\0';
970 void crypt_put_options(struct crypt_options *options)
972 if (options->flags & CRYPT_FLAG_FREE_DEVICE) {
973 free((char *)options->device);
974 options->device = NULL;
975 options->flags &= ~CRYPT_FLAG_FREE_DEVICE;
977 if (options->flags & CRYPT_FLAG_FREE_CIPHER) {
978 free((char *)options->cipher);
979 options->cipher = NULL;
980 options->flags &= ~CRYPT_FLAG_FREE_CIPHER;
984 const char *crypt_get_dir(void)
989 /////////////////////////////////
994 int crypt_init(struct crypt_device **cd, const char *device)
996 struct crypt_device *h = NULL;
1001 log_dbg("Allocating crypt device %s context.", device);
1003 if (device && !device_ready(NULL, device, O_RDONLY))
1006 if (!(h = malloc(sizeof(struct crypt_device))))
1009 memset(h, 0, sizeof(*h));
1012 h->device = strdup(device);
1020 if (dm_init(h, 1) < 0) {
1025 h->iteration_time = 1000;
1026 h->password_verify = 0;
1028 h->rng_type = crypt_random_default_key_rng();
1033 int crypt_init_by_name(struct crypt_device **cd, const char *name)
1035 crypt_status_info ci;
1036 struct crypt_active_device cad;
1037 char *device = NULL, *cipher_full = NULL, *device_uuid = NULL;
1038 char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
1040 int key_size = 0, r;
1043 log_dbg("Allocating crypt device context by device %s.", name);
1045 ci = crypt_status(NULL, name);
1046 if (ci == CRYPT_INVALID)
1049 if (ci < CRYPT_ACTIVE) {
1050 log_err(NULL, _("Device %s is not active.\n"), name);
1054 r = dm_query_device(name, &device, &cad.size, &cad.iv_offset, &cad.offset,
1055 &cipher_full, &key_size, &key, NULL, NULL,
1060 /* Underlying device disappeared but mapping still active */
1062 log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
1066 r = crypt_init(cd, device);
1070 /* Try to initialise basic parameters from active device */
1072 if (!strncmp(CRYPT_PLAIN, device_uuid, sizeof(CRYPT_PLAIN)-1)) {
1073 (*cd)->type = strdup(CRYPT_PLAIN);
1074 (*cd)->plain_uuid = strdup(device_uuid);
1075 (*cd)->plain_hdr.hash = NULL; /* no way to get this */
1076 (*cd)->plain_hdr.offset = cad.offset;
1077 (*cd)->plain_hdr.skip = cad.iv_offset;
1078 (*cd)->volume_key = crypt_alloc_volume_key(key_size, key);
1079 if (!(*cd)->volume_key) {
1084 r = crypt_parse_name_and_mode(cipher_full, cipher, cipher_mode);
1086 (*cd)->plain_cipher = strdup(cipher);
1087 (*cd)->plain_cipher_mode = strdup(cipher_mode);
1089 } else if (!strncmp(CRYPT_LUKS1, device_uuid, sizeof(CRYPT_LUKS1)-1)) {
1091 if (crypt_load(*cd, CRYPT_LUKS1, NULL) < 0 ||
1092 crypt_volume_key_verify(*cd, key, key_size) < 0) {
1093 log_dbg("LUKS device header does not match active device.");
1097 (*cd)->volume_key = crypt_alloc_volume_key(key_size, key);
1098 if (!(*cd)->volume_key) {
1105 log_dbg("Active device has no UUID set, some parameters are not set.");
1112 crypt_safe_free(key);
1119 static int _crypt_format_plain(struct crypt_device *cd,
1121 const char *cipher_mode,
1123 size_t volume_key_size,
1124 struct crypt_params_plain *params)
1126 if (!cipher || !cipher_mode) {
1127 log_err(cd, _("Invalid plain crypt parameters.\n"));
1131 if (volume_key_size > 1024) {
1132 log_err(cd, _("Invalid key size.\n"));
1136 cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
1137 if (!cd->volume_key)
1140 cd->plain_cipher = strdup(cipher);
1141 cd->plain_cipher_mode = strdup(cipher_mode);
1144 cd->plain_uuid = strdup(uuid);
1146 if (params && params->hash)
1147 cd->plain_hdr.hash = strdup(params->hash);
1149 cd->plain_hdr.offset = params ? params->offset : 0;
1150 cd->plain_hdr.skip = params ? params->skip : 0;
1152 if (!cd->plain_cipher || !cd->plain_cipher_mode)
1158 static int _crypt_format_luks1(struct crypt_device *cd,
1160 const char *cipher_mode,
1162 const char *volume_key,
1163 size_t volume_key_size,
1164 struct crypt_params_luks1 *params)
1167 unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
1168 unsigned long alignment_offset = 0;
1171 log_err(cd, _("Can't format LUKS without device.\n"));
1176 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
1179 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
1184 if (params && params->data_alignment)
1185 required_alignment = params->data_alignment * SECTOR_SIZE;
1187 get_topology_alignment(cd->device, &required_alignment,
1188 &alignment_offset, DEFAULT_DISK_ALIGNMENT);
1190 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
1191 (params && params->hash) ? params->hash : "sha1",
1193 required_alignment / SECTOR_SIZE,
1194 alignment_offset / SECTOR_SIZE,
1195 cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1199 /* Wipe first 8 sectors - fs magic numbers etc. */
1200 r = wipe_device_header(cd->device, 8);
1203 log_err(cd, _("Cannot format device %s which is still in use.\n"),
1206 log_err(cd, _("Cannot wipe header on device %s.\n"),
1212 r = LUKS_write_phdr(cd->device, &cd->hdr, cd);
1217 int crypt_format(struct crypt_device *cd,
1220 const char *cipher_mode,
1222 const char *volume_key,
1223 size_t volume_key_size,
1228 log_dbg("Formatting device %s as type %s.", cd->device ?: "(none)", cd->type ?: "(none)");
1233 r = init_crypto(cd);
1238 r = _crypt_format_plain(cd, cipher, cipher_mode,
1239 uuid, volume_key_size, params);
1240 else if (isLUKS(type))
1241 r = _crypt_format_luks1(cd, cipher, cipher_mode,
1242 uuid, volume_key, volume_key_size, params);
1244 /* FIXME: allow plugins here? */
1245 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
1249 if (!r && !(cd->type = strdup(type)))
1253 crypt_free_volume_key(cd->volume_key);
1254 cd->volume_key = NULL;
1260 int crypt_load(struct crypt_device *cd,
1261 const char *requested_type,
1264 struct luks_phdr hdr;
1267 log_dbg("Trying to load %s crypt type from device %s.",
1268 requested_type ?: "any", cd->device ?: "(none)");
1273 if (requested_type && !isLUKS(requested_type))
1276 r = init_crypto(cd);
1280 r = LUKS_read_phdr(cd->device, &hdr, 1, cd);
1283 memcpy(&cd->hdr, &hdr, sizeof(hdr));
1284 cd->type = strdup(CRYPT_LUKS1);
1292 int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
1294 char *device = NULL, *cipher = NULL, *uuid = NULL, *key = NULL;
1295 uint64_t size, skip, offset;
1296 int key_size, read_only, r;
1298 /* Device context type must be initialised */
1299 if (!cd->type || !crypt_get_uuid(cd))
1302 r = dm_query_device(name, &device, &size, &skip, &offset,
1303 &cipher, &key_size, &key, &read_only, NULL, &uuid);
1305 log_err(NULL, _("Device %s is not active.\n"), name);
1314 r = device_check_and_adjust(cd, device, 0, &new_size, &offset, &read_only);
1318 if (new_size == size) {
1319 log_dbg("Device has already requested size %" PRIu64
1325 log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
1327 r = dm_create_device(name, device, cipher, cd->type,
1328 crypt_get_uuid(cd), new_size, skip, offset,
1329 key_size, key, read_only, 1);
1331 crypt_safe_free(key);
1339 int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
1341 if (!isLUKS(cd->type)) {
1342 log_err(cd, _("This operation is not supported for this device type.\n"));
1346 if (uuid && !strncmp(uuid, cd->hdr.uuid, sizeof(cd->hdr.uuid))) {
1347 log_dbg("UUID is the same as requested (%s) for device %s.",
1353 log_dbg("Requested new UUID change to %s for %s.", uuid, cd->device);
1355 log_dbg("Requested new UUID refresh for %s.", cd->device);
1357 if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
1360 return LUKS_hdr_uuid_set(cd->device, &cd->hdr, uuid, cd);
1363 int crypt_header_backup(struct crypt_device *cd,
1364 const char *requested_type,
1365 const char *backup_file)
1369 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1372 r = init_crypto(cd);
1376 log_dbg("Requested header backup of device %s (%s) to "
1377 "file %s.", cd->device, requested_type, backup_file);
1379 return LUKS_hdr_backup(backup_file, cd->device, &cd->hdr, cd);
1382 int crypt_header_restore(struct crypt_device *cd,
1383 const char *requested_type,
1384 const char *backup_file)
1388 if (requested_type && !isLUKS(requested_type))
1391 /* Some hash functions need initialized gcrypt library */
1392 r = init_crypto(cd);
1396 log_dbg("Requested header restore to device %s (%s) from "
1397 "file %s.", cd->device, requested_type, backup_file);
1399 return LUKS_hdr_restore(backup_file, cd->device, &cd->hdr, cd);
1402 void crypt_free(struct crypt_device *cd)
1405 log_dbg("Releasing crypt device %s context.", cd->device);
1408 crypt_free_volume_key(cd->volume_key);
1413 /* used in plain device only */
1414 free((char*)cd->plain_hdr.hash);
1415 free(cd->plain_cipher);
1416 free(cd->plain_cipher_mode);
1417 free(cd->plain_uuid);
1423 int crypt_suspend(struct crypt_device *cd,
1426 crypt_status_info ci;
1427 int r, suspended = 0;
1429 log_dbg("Suspending volume %s.", name);
1431 ci = crypt_status(NULL, name);
1432 if (ci < CRYPT_ACTIVE) {
1433 log_err(cd, _("Volume %s is not active.\n"), name);
1437 if (!cd && dm_init(NULL, 1) < 0)
1440 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1441 NULL, NULL, NULL, NULL, &suspended, NULL);
1446 log_err(cd, _("Volume %s is already suspended.\n"), name);
1451 r = dm_suspend_and_wipe_key(name);
1453 log_err(cd, "Suspend is not supported for device %s.\n", name);
1455 log_err(cd, "Error during suspending device %s.\n", name);
1462 int crypt_resume_by_passphrase(struct crypt_device *cd,
1465 const char *passphrase,
1466 size_t passphrase_size)
1468 struct volume_key *vk = NULL;
1469 int r, suspended = 0;
1471 log_dbg("Resuming volume %s.", name);
1473 if (!isLUKS(cd->type)) {
1474 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1479 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1480 NULL, NULL, NULL, NULL, &suspended, NULL);
1485 log_err(cd, _("Volume %s is not suspended.\n"), name);
1490 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1491 passphrase_size, &cd->hdr, &vk, cd);
1493 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1497 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1499 log_err(cd, "Resume is not supported for device %s.\n", name);
1501 log_err(cd, "Error during resuming device %s.\n", name);
1505 crypt_free_volume_key(vk);
1506 return r < 0 ? r : keyslot;
1509 int crypt_resume_by_keyfile(struct crypt_device *cd,
1512 const char *keyfile,
1513 size_t keyfile_size)
1515 struct volume_key *vk = NULL;
1516 char *passphrase_read = NULL;
1517 unsigned int passphrase_size_read;
1518 int r, suspended = 0;
1520 log_dbg("Resuming volume %s.", name);
1522 if (!isLUKS(cd->type)) {
1523 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1528 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1529 NULL, NULL, NULL, NULL, &suspended, NULL);
1534 log_err(cd, _("Volume %s is not suspended.\n"), name);
1541 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1542 &passphrase_size_read, keyfile, keyfile_size);
1546 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1547 passphrase_size_read, &cd->hdr, &vk, cd);
1552 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1554 log_err(cd, "Error during resuming device %s.\n", name);
1556 crypt_safe_free(passphrase_read);
1557 crypt_free_volume_key(vk);
1558 return r < 0 ? r : keyslot;
1561 // slot manipulation
1562 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1563 int keyslot, // -1 any
1564 const char *passphrase, // NULL -> terminal
1565 size_t passphrase_size,
1566 const char *new_passphrase, // NULL -> terminal
1567 size_t new_passphrase_size)
1569 struct volume_key *vk = NULL;
1570 char *password = NULL, *new_password = NULL;
1571 unsigned int passwordLen, new_passwordLen;
1574 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1575 "new passphrase %sprovided.",
1576 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1578 if (!isLUKS(cd->type)) {
1579 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1583 r = keyslot_verify_or_find_empty(cd, &keyslot);
1587 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1588 /* No slots used, try to use pre-generated key in header */
1589 if (cd->volume_key) {
1590 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1591 r = vk ? 0 : -ENOMEM;
1593 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1596 } else if (passphrase) {
1597 /* Passphrase provided, use it to unlock existing keyslot */
1598 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, passphrase,
1599 passphrase_size, &cd->hdr, &vk, cd);
1601 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1602 r = key_from_terminal(cd, _("Enter any passphrase: "),
1603 &password, &passwordLen, 0);
1607 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
1608 passwordLen, &cd->hdr, &vk, cd);
1609 crypt_safe_free(password);
1615 if (new_passphrase) {
1616 new_password = (char *)new_passphrase;
1617 new_passwordLen = new_passphrase_size;
1619 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1620 &new_password, &new_passwordLen, 1);
1625 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1626 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1631 if (!new_passphrase)
1632 crypt_safe_free(new_password);
1633 crypt_free_volume_key(vk);
1634 return r ?: keyslot;
1637 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1639 const char *keyfile,
1640 size_t keyfile_size,
1641 const char *new_keyfile,
1642 size_t new_keyfile_size)
1644 struct volume_key *vk = NULL;
1645 char *password = NULL; unsigned int passwordLen;
1646 char *new_password = NULL; unsigned int new_passwordLen;
1649 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1650 keyfile ?: "[none]", new_keyfile ?: "[none]");
1652 if (!isLUKS(cd->type)) {
1653 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1657 r = keyslot_verify_or_find_empty(cd, &keyslot);
1661 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1662 /* No slots used, try to use pre-generated key in header */
1663 if (cd->volume_key) {
1664 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1665 r = vk ? 0 : -ENOMEM;
1667 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1671 /* Read password from file of (if NULL) from terminal */
1673 r = key_from_file(cd, _("Enter any passphrase: "),
1674 &password, &passwordLen,
1675 keyfile, keyfile_size);
1677 r = key_from_terminal(cd, _("Enter any passphrase: "),
1678 &password, &passwordLen, 0);
1682 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password, passwordLen,
1690 r = key_from_file(cd, _("Enter new passphrase for key slot: "),
1691 &new_password, &new_passwordLen, new_keyfile,
1694 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1695 &new_password, &new_passwordLen, 1);
1699 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1700 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1702 crypt_safe_free(password);
1703 crypt_safe_free(new_password);
1704 crypt_free_volume_key(vk);
1705 return r < 0 ? r : keyslot;
1708 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1710 const char *volume_key,
1711 size_t volume_key_size,
1712 const char *passphrase,
1713 size_t passphrase_size)
1715 struct volume_key *vk = NULL;
1717 char *new_password = NULL; unsigned int new_passwordLen;
1719 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1721 if (!isLUKS(cd->type)) {
1722 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1727 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1728 else if (cd->volume_key)
1729 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1734 r = LUKS_verify_volume_key(&cd->hdr, vk);
1736 log_err(cd, _("Volume key does not match the volume.\n"));
1740 r = keyslot_verify_or_find_empty(cd, &keyslot);
1745 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1746 &new_password, &new_passwordLen, 1);
1749 passphrase = new_password;
1750 passphrase_size = new_passwordLen;
1753 r = LUKS_set_key(cd->device, keyslot, passphrase, passphrase_size,
1754 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1756 crypt_safe_free(new_password);
1757 crypt_free_volume_key(vk);
1758 return (r < 0) ? r : keyslot;
1761 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1763 crypt_keyslot_info ki;
1765 log_dbg("Destroying keyslot %d.", keyslot);
1767 if (!isLUKS(cd->type)) {
1768 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1772 ki = crypt_keyslot_status(cd, keyslot);
1773 if (ki == CRYPT_SLOT_INVALID) {
1774 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1778 if (ki == CRYPT_SLOT_INACTIVE) {
1779 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1783 return LUKS_del_key(cd->device, keyslot, &cd->hdr, cd);
1786 // activation/deactivation of device mapping
1787 int crypt_activate_by_passphrase(struct crypt_device *cd,
1790 const char *passphrase,
1791 size_t passphrase_size,
1794 crypt_status_info ci;
1795 struct volume_key *vk = NULL;
1796 char *read_passphrase = NULL;
1797 unsigned int passphraseLen = 0;
1800 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1801 name ? "Activating" : "Checking", name ?: "",
1802 keyslot, passphrase ? "" : "[none] ");
1805 ci = crypt_status(NULL, name);
1806 if (ci == CRYPT_INVALID)
1808 else if (ci >= CRYPT_ACTIVE) {
1809 log_err(cd, _("Device %s already exists.\n"), name);
1814 /* plain, use hashed passphrase */
1815 if (isPLAIN(cd->type)) {
1817 r = key_from_terminal(cd, NULL, &read_passphrase,
1821 passphrase = read_passphrase;
1822 passphrase_size = passphraseLen;
1824 r = create_device_helper(cd, name, cd->plain_hdr.hash,
1825 cd->plain_cipher, cd->plain_cipher_mode,
1826 NULL, passphrase, passphrase_size,
1827 cd->volume_key->keylength, 0,
1828 cd->plain_hdr.skip, cd->plain_hdr.offset,
1830 flags & CRYPT_ACTIVATE_READONLY, 0, 0);
1832 } else if (isLUKS(cd->type)) {
1833 /* provided passphrase, do not retry */
1835 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1836 passphrase_size, &cd->hdr, &vk, cd);
1838 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1843 r = open_from_hdr_and_vk(cd, vk, name, flags);
1848 crypt_safe_free(read_passphrase);
1849 crypt_free_volume_key(vk);
1851 return r < 0 ? r : keyslot;
1854 int crypt_activate_by_keyfile(struct crypt_device *cd,
1857 const char *keyfile,
1858 size_t keyfile_size,
1861 crypt_status_info ci;
1862 struct volume_key *vk = NULL;
1863 char *passphrase_read = NULL;
1864 unsigned int passphrase_size_read;
1867 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1868 name ?: "", keyslot, keyfile ?: "[none]");
1871 ci = crypt_status(NULL, name);
1872 if (ci == CRYPT_INVALID)
1874 else if (ci >= CRYPT_ACTIVE) {
1875 log_err(cd, _("Device %s already exists.\n"), name);
1883 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1884 &passphrase_size_read, keyfile, keyfile_size);
1888 if (isPLAIN(cd->type)) {
1889 r = create_device_helper(cd, name, cd->plain_hdr.hash,
1890 cd->plain_cipher, cd->plain_cipher_mode,
1891 NULL, passphrase_read, passphrase_size_read,
1892 cd->volume_key->keylength, 0,
1893 cd->plain_hdr.skip, cd->plain_hdr.offset,
1895 flags & CRYPT_ACTIVATE_READONLY, 0, 0);
1897 } else if (isLUKS(cd->type)) {
1898 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1899 passphrase_size_read, &cd->hdr, &vk, cd);
1906 r = open_from_hdr_and_vk(cd, vk, name, flags);
1911 crypt_safe_free(passphrase_read);
1912 crypt_free_volume_key(vk);
1914 return r < 0 ? r : keyslot;
1917 int crypt_activate_by_volume_key(struct crypt_device *cd,
1919 const char *volume_key,
1920 size_t volume_key_size,
1923 crypt_status_info ci;
1924 struct volume_key *vk;
1927 log_dbg("Activating volume %s by volume key.", name);
1929 /* use key directly, no hash */
1930 if (isPLAIN(cd->type)) {
1931 if (!volume_key || !volume_key_size || !cd->volume_key ||
1932 volume_key_size != cd->volume_key->keylength) {
1933 log_err(cd, _("Incorrect volume key specified for plain device.\n"));
1937 return create_device_helper(cd, name, NULL,
1938 cd->plain_cipher, cd->plain_cipher_mode, NULL, volume_key, volume_key_size,
1939 cd->volume_key->keylength, 0, cd->plain_hdr.skip,
1940 cd->plain_hdr.offset, cd->plain_uuid, flags & CRYPT_ACTIVATE_READONLY, 0, 0);
1943 if (!isLUKS(cd->type)) {
1944 log_err(cd, _("Device type is not properly initialised.\n"));
1949 ci = crypt_status(NULL, name);
1950 if (ci == CRYPT_INVALID)
1952 else if (ci >= CRYPT_ACTIVE) {
1953 log_err(cd, _("Device %s already exists.\n"), name);
1958 /* If key is not provided, try to use internal key */
1960 if (!cd->volume_key) {
1961 log_err(cd, _("Volume key does not match the volume.\n"));
1964 volume_key_size = cd->volume_key->keylength;
1965 volume_key = cd->volume_key->key;
1968 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1971 r = LUKS_verify_volume_key(&cd->hdr, vk);
1974 log_err(cd, _("Volume key does not match the volume.\n"));
1977 r = open_from_hdr_and_vk(cd, vk, name, flags);
1979 crypt_free_volume_key(vk);
1984 int crypt_deactivate(struct crypt_device *cd, const char *name)
1991 log_dbg("Deactivating volume %s.", name);
1993 if (!cd && dm_init(NULL, 1) < 0)
1996 switch (crypt_status(cd, name)) {
1998 r = dm_remove_device(name, 0, 0);
2001 log_err(cd, _("Device %s is busy.\n"), name);
2004 case CRYPT_INACTIVE:
2005 log_err(cd, _("Device %s is not active.\n"), name);
2009 log_err(cd, _("Invalid device %s.\n"), name);
2019 int crypt_volume_key_get(struct crypt_device *cd,
2022 size_t *volume_key_size,
2023 const char *passphrase,
2024 size_t passphrase_size)
2026 struct volume_key *vk;
2027 char *processed_key = NULL;
2030 key_len = crypt_get_volume_key_size(cd);
2031 if (key_len > *volume_key_size) {
2032 log_err(cd, _("Volume key buffer too small.\n"));
2036 if (isPLAIN(cd->type) && cd->plain_hdr.hash) {
2037 processed_key = process_key(cd, cd->plain_hdr.hash, NULL, key_len,
2038 passphrase, passphrase_size);
2039 if (!processed_key) {
2040 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
2043 memcpy(volume_key, processed_key, key_len);
2044 *volume_key_size = key_len;
2045 crypt_safe_free(processed_key);
2049 if (isLUKS(cd->type)) {
2050 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
2051 passphrase_size, &cd->hdr, &vk, cd);
2054 memcpy(volume_key, vk->key, vk->keylength);
2055 *volume_key_size = vk->keylength;
2058 crypt_free_volume_key(vk);
2062 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
2066 int crypt_volume_key_verify(struct crypt_device *cd,
2067 const char *volume_key,
2068 size_t volume_key_size)
2070 struct volume_key *vk;
2073 if (!isLUKS(cd->type)) {
2074 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2078 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2082 r = LUKS_verify_volume_key(&cd->hdr, vk);
2085 log_err(cd, _("Volume key does not match the volume.\n"));
2087 crypt_free_volume_key(vk);
2092 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
2094 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
2095 cd->timeout = timeout_sec;
2098 void crypt_set_password_retry(struct crypt_device *cd, int tries)
2100 log_dbg("Password retry count set to %d.", tries);
2104 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2106 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
2107 cd->iteration_time = iteration_time_ms;
2110 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
2112 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
2113 cd->password_verify = password_verify ? 1 : 0;
2116 void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
2119 case CRYPT_RNG_URANDOM:
2120 case CRYPT_RNG_RANDOM:
2121 log_dbg("RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
2122 cd->rng_type = rng_type;
2126 int crypt_get_rng_type(struct crypt_device *cd)
2131 return cd->rng_type;
2134 int crypt_memory_lock(struct crypt_device *cd, int lock)
2136 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
2140 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
2144 if (!cd && dm_init(NULL, 1) < 0)
2145 return CRYPT_INVALID;
2147 r = dm_status_device(name);
2152 if (r < 0 && r != -ENODEV)
2153 return CRYPT_INVALID;
2156 return CRYPT_ACTIVE;
2161 return CRYPT_INACTIVE;
2164 static void hexprintICB(struct crypt_device *cd, char *d, int n)
2167 for(i = 0; i < n; i++)
2168 log_std(cd, "%02hhx ", (char)d[i]);
2171 int crypt_dump(struct crypt_device *cd)
2174 if (!isLUKS(cd->type)) { //FIXME
2175 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2179 log_std(cd, "LUKS header information for %s\n\n", cd->device);
2180 log_std(cd, "Version: \t%d\n", cd->hdr.version);
2181 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
2182 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
2183 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec);
2184 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
2185 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
2186 log_std(cd, "MK digest: \t");
2187 hexprintICB(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE);
2189 log_std(cd, "MK salt: \t");
2190 hexprintICB(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2);
2191 log_std(cd, "\n \t");
2192 hexprintICB(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
2194 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
2195 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
2196 for(i = 0; i < LUKS_NUMKEYS; i++) {
2197 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
2198 log_std(cd, "Key Slot %d: ENABLED\n",i);
2199 log_std(cd, "\tIterations: \t%d\n",
2200 cd->hdr.keyblock[i].passwordIterations);
2201 log_std(cd, "\tSalt: \t");
2202 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt,
2204 log_std(cd, "\n\t \t");
2205 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt +
2206 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
2209 log_std(cd, "\tKey material offset:\t%d\n",
2210 cd->hdr.keyblock[i].keyMaterialOffset);
2211 log_std(cd, "\tAF stripes: \t%d\n",
2212 cd->hdr.keyblock[i].stripes);
2215 log_std(cd, "Key Slot %d: DISABLED\n", i);
2221 const char *crypt_get_cipher(struct crypt_device *cd)
2223 if (isPLAIN(cd->type))
2224 return cd->plain_cipher;
2226 if (isLUKS(cd->type))
2227 return cd->hdr.cipherName;
2232 const char *crypt_get_cipher_mode(struct crypt_device *cd)
2234 if (isPLAIN(cd->type))
2235 return cd->plain_cipher_mode;
2237 if (isLUKS(cd->type))
2238 return cd->hdr.cipherMode;
2243 const char *crypt_get_uuid(struct crypt_device *cd)
2245 if (isLUKS(cd->type))
2246 return cd->hdr.uuid;
2248 if (isPLAIN(cd->type))
2249 return cd->plain_uuid;
2254 const char *crypt_get_device_name(struct crypt_device *cd)
2259 int crypt_get_volume_key_size(struct crypt_device *cd)
2261 if (isPLAIN(cd->type) && cd->volume_key)
2262 return cd->volume_key->keylength;
2264 if (isLUKS(cd->type))
2265 return cd->hdr.keyBytes;
2270 uint64_t crypt_get_data_offset(struct crypt_device *cd)
2272 if (isPLAIN(cd->type))
2273 return cd->plain_hdr.offset;
2275 if (isLUKS(cd->type))
2276 return cd->hdr.payloadOffset;
2281 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
2283 if (!isLUKS(cd->type)) {
2284 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2285 return CRYPT_SLOT_INVALID;
2288 return LUKS_keyslot_info(&cd->hdr, keyslot);
2291 int crypt_keyslot_max(const char *type)
2293 if (type && isLUKS(type))
2294 return LUKS_NUMKEYS;
2299 const char *crypt_get_type(struct crypt_device *cd)
2304 int crypt_get_active_device(struct crypt_device *cd,
2306 struct crypt_active_device *cad)
2310 r = dm_query_device(name, NULL, &cad->size, &cad->iv_offset, &cad->offset,
2311 NULL, NULL, NULL, &readonly, NULL, NULL);
2315 cad->flags = readonly ? CRYPT_ACTIVATE_READONLY : 0;