8 #include "libcryptsetup.h"
12 #include "crypto_backend.h"
20 struct volume_key *volume_key;
22 uint64_t iteration_time;
27 /* used in CRYPT_LUKS1 */
29 uint64_t PBKDF2_per_sec;
31 /* used in CRYPT_PLAIN */
32 struct crypt_params_plain plain_hdr;
34 char *plain_cipher_mode;
37 /* used in CRYPT_LOOPAES */
38 struct crypt_params_loopaes loopaes_hdr;
40 char *loopaes_cipher_mode;
42 unsigned int loopaes_key_size;
44 /* callbacks definitions */
45 void (*log)(int level, const char *msg, void *usrptr);
47 int (*confirm)(const char *msg, void *usrptr);
49 int (*password)(const char *msg, char *buf, size_t length, void *usrptr);
50 void *password_usrptr;
54 static void (*_default_log)(int level, const char *msg, void *usrptr) = NULL;
55 static int _debug_level = 0;
57 void crypt_set_debug_level(int level)
62 int crypt_get_debug_level()
67 void crypt_log(struct crypt_device *cd, int level, const char *msg)
70 cd->log(level, msg, cd->log_usrptr);
71 else if (_default_log)
72 _default_log(level, msg, NULL);
75 void logger(struct crypt_device *cd, int level, const char *file,
76 int line, const char *format, ...)
81 va_start(argp, format);
83 if (vasprintf(&target, format, argp) > 0) {
85 crypt_log(cd, level, target);
87 } else if (_debug_level)
88 printf("# %s:%d %s\n", file ?: "?", line, target);
90 } else if (_debug_level)
91 printf("# %s\n", target);
99 static int init_crypto(struct crypt_device *ctx)
103 r = crypt_random_init(ctx);
105 log_err(ctx, _("Cannot initialize crypto RNG backend.\n"));
109 r = crypt_backend_init(ctx);
111 log_err(ctx, _("Cannot initialize crypto backend.\n"));
117 * Password processing behaviour matrix of process_key
119 * from binary file: check if there is sufficently large key material
120 * interactive & from fd: hash if requested, otherwise crop or pad with '0'
122 static char *process_key(struct crypt_device *cd, const char *hash_name,
123 const char *key_file, size_t key_size,
124 const char *pass, size_t passLen)
131 key = crypt_safe_alloc(key_size);
132 memset(key, 0, key_size);
134 /* key is coming from binary file */
135 if (key_file && strcmp(key_file, "-")) {
136 if(passLen < key_size) {
137 log_err(cd, _("Cannot not read %d bytes from key file %s.\n"),
139 crypt_safe_free(key);
142 memcpy(key, pass, key_size);
146 /* key is coming from tty, fd or binary stdin */
148 if (crypt_plain_hash(cd, hash_name, key, key_size, pass, passLen) < 0) {
149 log_err(cd, _("Key processing error (using hash algorithm %s).\n"),
151 crypt_safe_free(key);
154 } else if (passLen > key_size) {
155 memcpy(key, pass, key_size);
157 memcpy(key, pass, passLen);
163 static int isPLAIN(const char *type)
165 return (type && !strcmp(CRYPT_PLAIN, type));
168 static int isLUKS(const char *type)
170 return (type && !strcmp(CRYPT_LUKS1, type));
173 static int isLOOPAES(const char *type)
175 return (type && !strcmp(CRYPT_LOOPAES, type));
178 /* keyslot helpers */
179 static int keyslot_verify_or_find_empty(struct crypt_device *cd, int *keyslot)
181 if (*keyslot == CRYPT_ANY_SLOT) {
182 *keyslot = LUKS_keyslot_find_empty(&cd->hdr);
184 log_err(cd, _("All key slots full.\n"));
189 switch (LUKS_keyslot_info(&cd->hdr, *keyslot)) {
190 case CRYPT_SLOT_INVALID:
191 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
192 *keyslot, LUKS_NUMKEYS - 1);
194 case CRYPT_SLOT_INACTIVE:
197 log_err(cd, _("Key slot %d is full, please select another one.\n"),
205 static int verify_other_keyslot(struct crypt_device *cd,
206 const char *key_file,
209 struct volume_key *vk = NULL;
210 crypt_keyslot_info ki;
212 char *password = NULL;
213 unsigned int passwordLen;
215 r = crypt_get_key(_("Enter any remaining LUKS passphrase: "),
216 &password, &passwordLen, 0, key_file, cd->timeout,
217 cd->password_verify, cd);
221 ki = crypt_keyslot_status(cd, keyIndex);
222 if (ki == CRYPT_SLOT_ACTIVE) /* Not last slot */
223 LUKS_keyslot_set(&cd->hdr, keyIndex, 0);
225 openedIndex = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT,
226 password, passwordLen,
229 if (ki == CRYPT_SLOT_ACTIVE)
230 LUKS_keyslot_set(&cd->hdr, keyIndex, 1);
235 log_verbose(cd, _("Key slot %d verified.\n"), openedIndex);
237 crypt_free_volume_key(vk);
238 crypt_safe_free(password);
242 static int find_keyslot_by_passphrase(struct crypt_device *cd,
243 const char *key_file,
246 struct volume_key *vk = NULL;
247 char *password = NULL;
248 unsigned int passwordLen;
251 r = crypt_get_key(message,&password,&passwordLen, 0, key_file,
252 cd->timeout, cd->password_verify, cd);
256 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
257 passwordLen, &cd->hdr, &vk, cd);
259 crypt_free_volume_key(vk);
260 crypt_safe_free(password);
264 static int luks_remove_helper(struct crypt_device *cd,
266 const char *other_key_file,
267 const char *key_file,
270 crypt_keyslot_info ki;
273 if (key_slot == CRYPT_ANY_SLOT) {
274 key_slot = find_keyslot_by_passphrase(cd, key_file,
275 _("Enter LUKS passphrase to be deleted: "));
281 log_std(cd, _("Key slot %d selected for deletion.\n"), key_slot);
284 ki = crypt_keyslot_status(cd, key_slot);
285 if (ki == CRYPT_SLOT_INVALID) {
286 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
287 key_slot, LUKS_NUMKEYS - 1);
291 if (ki <= CRYPT_SLOT_INACTIVE) {
292 log_err(cd, _("Key %d not active. Can't wipe.\n"), key_slot);
297 if (ki == CRYPT_SLOT_ACTIVE_LAST && cd->confirm &&
298 !(cd->confirm(_("This is the last keyslot."
299 " Device will become unusable after purging this key."),
300 cd->confirm_usrptr))) {
306 r = verify_other_keyslot(cd, other_key_file, key_slot);
311 r = crypt_keyslot_destroy(cd, key_slot);
313 return (r < 0) ? r : 0;
316 static int create_device_helper(struct crypt_device *cd,
320 const char *cipher_mode,
321 const char *key_file,
333 crypt_status_info ci;
334 char *dm_cipher = NULL;
335 char *processed_key = NULL;
341 ci = crypt_status(cd, name);
342 if (ci == CRYPT_INVALID)
345 if (reload && ci < CRYPT_ACTIVE)
348 if (!reload && ci >= CRYPT_ACTIVE) {
349 log_err(cd, _("Device %s already exists.\n"), name);
353 if (key_size < 0 || key_size > 1024) {
354 log_err(cd, _("Invalid key size %d.\n"), key_size);
358 r = device_check_and_adjust(cd, cd->device, !reload, &size, &offset, &read_only);
362 if (cipher_mode && asprintf(&dm_cipher, "%s-%s", cipher, cipher_mode) < 0)
365 processed_key = process_key(cd, hash, key_file, key_size, key, keyLen);
366 if (!processed_key) {
371 r = dm_create_device(name, cd->device, dm_cipher ?: cipher, cd->type, uuid, size, skip, offset,
372 key_size, processed_key, read_only, reload);
375 crypt_safe_free(processed_key);
379 static int open_from_hdr_and_vk(struct crypt_device *cd,
380 struct volume_key *vk,
384 uint64_t size, offset;
386 int read_only, no_uuid, r;
389 offset = crypt_get_data_offset(cd);
390 read_only = flags & CRYPT_ACTIVATE_READONLY;
391 no_uuid = flags & CRYPT_ACTIVATE_NO_UUID;
393 r = device_check_and_adjust(cd, cd->device, 1, &size, &offset, &read_only);
397 if (asprintf(&cipher, "%s-%s", crypt_get_cipher(cd),
398 crypt_get_cipher_mode(cd)) < 0)
401 r = dm_create_device(name, cd->device, cipher, cd->type,
402 no_uuid ? NULL : crypt_get_uuid(cd),
403 size, 0, offset, vk->keylength, vk->key,
409 static void log_wrapper(int level, const char *msg, void *usrptr)
411 void (*xlog)(int level, char *msg) = usrptr;
412 xlog(level, (char *)msg);
415 static int yesDialog_wrapper(const char *msg, void *usrptr)
417 int (*xyesDialog)(char *msg) = usrptr;
418 return xyesDialog((char*)msg);
421 int crypt_confirm(struct crypt_device *cd, const char *msg)
423 if (!cd || !cd->confirm)
426 return cd->confirm(msg, cd->confirm_usrptr);
429 static int key_from_terminal(struct crypt_device *cd, char *msg, char **key,
430 unsigned int *key_len, int force_verify)
436 if(!msg && asprintf(&prompt, _("Enter passphrase for %s: "),
437 cd->backing_file ?: cd->device) < 0)
444 *key = crypt_safe_alloc(MAX_TTY_PASSWORD_LEN);
449 r = cd->password(msg, *key, MAX_TTY_PASSWORD_LEN, cd->password_usrptr);
451 crypt_safe_free(*key);
456 r = crypt_get_key(msg, key, key_len, 0, NULL, cd->timeout,
457 (force_verify || cd->password_verify), cd);
460 return (r < 0) ? r: 0;
463 static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslot,
464 struct volume_key **vk)
466 char *passphrase_read = NULL;
467 unsigned int passphrase_size_read;
468 int r = -EINVAL, tries = cd->tries;
472 crypt_free_volume_key(*vk);
475 r = key_from_terminal(cd, NULL, &passphrase_read,
476 &passphrase_size_read, 0);
480 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
481 passphrase_size_read, &cd->hdr, vk, cd);
482 crypt_safe_free(passphrase_read);
483 passphrase_read = NULL;
484 } while (r == -EPERM && (--tries > 0));
487 crypt_free_volume_key(*vk);
491 crypt_safe_free(passphrase_read);
495 static int key_from_file(struct crypt_device *cd, char *msg,
496 char **key, unsigned int *key_len,
497 const char *key_file, size_t key_size)
499 return crypt_get_key(msg, key, key_len, key_size, key_file,
503 static int _crypt_init(struct crypt_device **cd,
505 struct crypt_options *options,
506 int load, int need_dm)
510 /* if it is plain device and mapping table is being reloaded
511 initialize it by name*/
512 init_by_name = (type && !strcmp(type, CRYPT_PLAIN) && load);
514 /* Some of old API calls do not require DM in kernel,
515 fake initialisation by initialise it with kernel_check disabled */
517 (void)dm_init(NULL, 0);
519 r = crypt_init_by_name(cd, options->name);
521 r = crypt_init(cd, options->device);
528 crypt_set_log_callback(*cd, log_wrapper, options->icb->log);
529 crypt_set_confirm_callback(*cd, yesDialog_wrapper, options->icb->yesDialog);
531 crypt_set_timeout(*cd, options->timeout);
532 crypt_set_password_retry(*cd, options->tries);
533 crypt_set_iterarion_time(*cd, options->iteration_time ?: 1000);
534 crypt_set_password_verify(*cd, options->flags & CRYPT_FLAG_VERIFY);
536 if (load && !init_by_name)
537 r = crypt_load(*cd, type, NULL);
539 if (!r && type && !(*cd)->type) {
540 (*cd)->type = strdup(type);
551 void crypt_set_log_callback(struct crypt_device *cd,
552 void (*log)(int level, const char *msg, void *usrptr),
559 cd->log_usrptr = usrptr;
563 void crypt_set_confirm_callback(struct crypt_device *cd,
564 int (*confirm)(const char *msg, void *usrptr),
567 cd->confirm = confirm;
568 cd->confirm_usrptr = usrptr;
571 void crypt_set_password_callback(struct crypt_device *cd,
572 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
575 cd->password = password;
576 cd->password_usrptr = usrptr;
579 /* OPTIONS: name, cipher, device, hash, key_file, key_size, key_slot,
580 * offset, size, skip, timeout, tries, passphrase_fd (ignored),
582 static int crypt_create_and_update_device(struct crypt_options *options, int update)
584 struct crypt_device *cd = NULL;
589 r = _crypt_init(&cd, CRYPT_PLAIN, options, 0, 1);
593 r = crypt_get_key(_("Enter passphrase: "), &key, &keyLen, options->key_size,
594 options->key_file, cd->timeout, cd->password_verify, cd);
596 r = create_device_helper(cd, options->name, options->hash,
597 options->cipher, NULL, options->key_file, key, keyLen,
598 options->key_size, options->size, options->skip,
599 options->offset, NULL, options->flags & CRYPT_FLAG_READONLY,
600 options->flags, update);
602 crypt_safe_free(key);
607 int crypt_create_device(struct crypt_options *options)
609 return crypt_create_and_update_device(options, 0);
612 int crypt_update_device(struct crypt_options *options)
614 return crypt_create_and_update_device(options, 1);
617 /* OPTIONS: name, size, icb */
618 int crypt_resize_device(struct crypt_options *options)
620 struct crypt_device *cd = NULL;
621 char *device = NULL, *cipher = NULL, *uuid = NULL, *key = NULL;
623 uint64_t size, skip, offset;
624 int key_size, read_only, r;
626 log_dbg("Resizing device %s to %" PRIu64 " sectors.", options->name, options->size);
628 if (dm_init(NULL, 1) < 0)
631 r = dm_query_device(options->name, &device, &size, &skip, &offset,
632 &cipher, &key_size, &key, &read_only, NULL, &uuid);
634 log_err(NULL, _("Device %s is not active.\n"), options->name);
638 /* Try to determine type of device from UUID */
641 if (!strncmp(uuid, CRYPT_PLAIN, strlen(CRYPT_PLAIN))) {
645 } else if (!strncmp(uuid, CRYPT_LUKS1, strlen(CRYPT_LUKS1)))
649 if (!options->device)
650 options->device = device;
652 r = _crypt_init(&cd, type, options, 1, 1);
656 size = options->size;
657 r = device_check_and_adjust(cd, device, 0, &size, &offset, &read_only);
661 r = dm_create_device(options->name, device, cipher, type,
662 crypt_get_uuid(cd), size, skip, offset,
663 key_size, key, read_only, 1);
665 crypt_safe_free(key);
667 if (options->device == device)
668 options->device = NULL;
676 /* OPTIONS: name, icb */
677 int crypt_query_device(struct crypt_options *options)
681 log_dbg("Query device %s.", options->name);
683 if (dm_init(NULL, 1) < 0)
686 r = dm_status_device(options->name);
690 r = dm_query_device(options->name, (char **)&options->device, &options->size,
691 &options->skip, &options->offset, (char **)&options->cipher,
692 &options->key_size, NULL, &read_only, NULL, NULL);
695 options->flags |= CRYPT_FLAG_READONLY;
697 options->flags |= CRYPT_FLAG_FREE_DEVICE;
698 options->flags |= CRYPT_FLAG_FREE_CIPHER;
710 /* OPTIONS: name, icb */
711 int crypt_remove_device(struct crypt_options *options)
713 struct crypt_device *cd = NULL;
716 r = crypt_init_by_name(&cd, options->name);
718 r = crypt_deactivate(cd, options->name);
725 /* OPTIONS: device, cipher, hash, align_payload, key_size (master key), key_slot
726 * new_key_file, iteration_time, timeout, flags, icb */
727 int crypt_luksFormat(struct crypt_options *options)
729 char cipherName[LUKS_CIPHERNAME_L];
730 char cipherMode[LUKS_CIPHERMODE_L];
732 unsigned int passwordLen;
733 struct crypt_device *cd = NULL;
734 struct crypt_params_luks1 cp = {
735 .hash = options->hash,
736 .data_alignment = options->align_payload
740 r = crypt_parse_name_and_mode(options->cipher, cipherName, NULL, cipherMode);
742 log_err(cd, _("No known cipher specification pattern detected.\n"));
746 if ((r = _crypt_init(&cd, CRYPT_LUKS1, options, 0, 1)))
749 if (options->key_slot >= LUKS_NUMKEYS && options->key_slot != CRYPT_ANY_SLOT) {
750 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
751 options->key_slot, LUKS_NUMKEYS - 1);
756 r = crypt_get_key(_("Enter LUKS passphrase: "), &password, &passwordLen, 0,
757 options->new_key_file, cd->timeout, cd->password_verify, cd);
762 r = crypt_format(cd, CRYPT_LUKS1, cipherName, cipherMode,
763 NULL, NULL, options->key_size, &cp);
767 /* Add keyslot using internally stored volume key generated during format */
768 r = crypt_keyslot_add_by_volume_key(cd, options->key_slot, NULL, 0,
769 password, passwordLen);
772 crypt_safe_free(password);
773 return (r < 0) ? r : 0;
776 /* OPTIONS: name, device, key_size, key_file, timeout, tries, flags, icb */
777 int crypt_luksOpen(struct crypt_options *options)
779 struct crypt_device *cd = NULL;
786 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
790 if (options->flags & CRYPT_FLAG_READONLY)
791 flags |= CRYPT_ACTIVATE_READONLY;
793 if (options->flags & CRYPT_FLAG_NON_EXCLUSIVE_ACCESS)
794 flags |= CRYPT_ACTIVATE_NO_UUID;
796 if (options->key_file)
797 r = crypt_activate_by_keyfile(cd, options->name,
798 CRYPT_ANY_SLOT, options->key_file, options->key_size,
801 r = crypt_activate_by_passphrase(cd, options->name,
802 CRYPT_ANY_SLOT, options->passphrase,
803 options->passphrase ? strlen(options->passphrase) : 0,
807 return (r < 0) ? r : 0;
810 /* OPTIONS: device, keys_slot, key_file, timeout, flags, icb */
811 int crypt_luksKillSlot(struct crypt_options *options)
813 struct crypt_device *cd = NULL;
816 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
820 r = luks_remove_helper(cd, options->key_slot, options->key_file, NULL,
821 options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
824 return (r < 0) ? r : 0;
827 /* OPTIONS: device, new_key_file, key_file, timeout, flags, icb */
828 int crypt_luksRemoveKey(struct crypt_options *options)
830 struct crypt_device *cd = NULL;
833 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
837 r = luks_remove_helper(cd, CRYPT_ANY_SLOT, options->key_file, options->new_key_file,
838 options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
841 return (r < 0) ? r : 0;
845 /* OPTIONS: device, new_key_file, key_file, key_slot, flags,
846 iteration_time, timeout, icb */
847 int crypt_luksAddKey(struct crypt_options *options)
849 struct crypt_device *cd = NULL;
852 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
856 if (options->key_file || options->new_key_file)
857 r = crypt_keyslot_add_by_keyfile(cd, options->key_slot,
858 options->key_file, 0,
859 options->new_key_file, 0);
861 r = crypt_keyslot_add_by_passphrase(cd, options->key_slot,
865 return (r < 0) ? r : 0;
868 /* OPTIONS: device, icb */
869 int crypt_luksUUID(struct crypt_options *options)
871 struct crypt_device *cd = NULL;
875 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
879 uuid = (char *)crypt_get_uuid(cd);
880 log_std(cd, uuid ?: "");
886 /* OPTIONS: device, icb */
887 int crypt_isLuks(struct crypt_options *options)
889 struct crypt_device *cd = NULL;
892 log_dbg("Check device %s for LUKS header.", options->device);
898 r = crypt_init(&cd, options->device);
902 /* Do print fail here, no need to crypt_load() */
903 r = LUKS_read_phdr(cd->device, &cd->hdr, 0, cd) ? -EINVAL : 0;
909 /* OPTIONS: device, icb */
910 int crypt_luksDump(struct crypt_options *options)
912 struct crypt_device *cd = NULL;
915 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
925 void crypt_get_error(char *buf, size_t size)
927 const char *error = get_error();
929 if (!buf || size < 1)
932 strncpy(buf, error, size - 1);
933 buf[size - 1] = '\0';
939 void crypt_put_options(struct crypt_options *options)
941 if (options->flags & CRYPT_FLAG_FREE_DEVICE) {
942 free((char *)options->device);
943 options->device = NULL;
944 options->flags &= ~CRYPT_FLAG_FREE_DEVICE;
946 if (options->flags & CRYPT_FLAG_FREE_CIPHER) {
947 free((char *)options->cipher);
948 options->cipher = NULL;
949 options->flags &= ~CRYPT_FLAG_FREE_CIPHER;
953 const char *crypt_get_dir(void)
958 /////////////////////////////////
963 int crypt_init(struct crypt_device **cd, const char *device)
965 struct crypt_device *h = NULL;
971 log_dbg("Allocating crypt device %s context.", device);
973 if (!(h = malloc(sizeof(struct crypt_device))))
976 memset(h, 0, sizeof(*h));
980 r = device_ready(NULL, device, O_RDONLY);
982 h->device = crypt_loop_get_device();
983 log_dbg("Not a block device, %s%s.",
984 h->device ? "using free loop device " :
985 "no free loop device found",
992 /* Keep the loop open, dettached on last close. */
993 h->loop_fd = crypt_loop_attach(h->device, device, 0, &readonly);
994 if (h->loop_fd == -1) {
995 log_dbg("Attaching loop failed.");
1000 h->backing_file = crypt_loop_backing_file(h->device);
1001 r = device_ready(NULL, h->device, O_RDONLY);
1009 if (!h->device && device && !(h->device = strdup(device))) {
1014 if (dm_init(h, 1) < 0) {
1019 h->iteration_time = 1000;
1020 h->password_verify = 0;
1022 h->rng_type = crypt_random_default_key_rng();
1028 if (h->loop_fd != -1)
1031 free(h->backing_file);
1037 int crypt_init_by_name(struct crypt_device **cd, const char *name)
1039 crypt_status_info ci;
1040 struct crypt_active_device cad;
1041 char *device = NULL, *cipher_full = NULL, *device_uuid = NULL;
1042 char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
1044 int key_size = 0, key_nums, r;
1047 log_dbg("Allocating crypt device context by device %s.", name);
1049 ci = crypt_status(NULL, name);
1050 if (ci == CRYPT_INVALID)
1053 if (ci < CRYPT_ACTIVE) {
1054 log_err(NULL, _("Device %s is not active.\n"), name);
1058 r = dm_query_device(name, &device, &cad.size, &cad.iv_offset, &cad.offset,
1059 &cipher_full, &key_size, &key, NULL, NULL,
1065 r = crypt_init(cd, device);
1067 /* Underlying device disappeared but mapping still active */
1068 if (!device || r == -ENOTBLK)
1069 log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
1072 /* Underlying device is not readable but crypt mapping exists */
1073 if (r == -ENOTBLK) {
1076 r = crypt_init(cd, NULL);
1082 /* Try to initialise basic parameters from active device */
1084 if (!(*cd)->backing_file && device && crypt_loop_device(device) &&
1085 !((*cd)->backing_file = crypt_loop_backing_file(device))) {
1091 if (!strncmp(CRYPT_PLAIN, device_uuid, sizeof(CRYPT_PLAIN)-1)) {
1092 (*cd)->type = strdup(CRYPT_PLAIN);
1093 (*cd)->plain_uuid = strdup(device_uuid);
1094 (*cd)->plain_hdr.hash = NULL; /* no way to get this */
1095 (*cd)->plain_hdr.offset = cad.offset;
1096 (*cd)->plain_hdr.skip = cad.iv_offset;
1097 (*cd)->volume_key = crypt_alloc_volume_key(key_size, key);
1098 if (!(*cd)->volume_key) {
1103 r = crypt_parse_name_and_mode(cipher_full, cipher, NULL, cipher_mode);
1105 (*cd)->plain_cipher = strdup(cipher);
1106 (*cd)->plain_cipher_mode = strdup(cipher_mode);
1108 } else if (!strncmp(CRYPT_LOOPAES, device_uuid, sizeof(CRYPT_LOOPAES)-1)) {
1109 (*cd)->type = strdup(CRYPT_LOOPAES);
1110 (*cd)->loopaes_uuid = strdup(device_uuid);
1111 (*cd)->loopaes_hdr.offset = cad.offset;
1113 r = crypt_parse_name_and_mode(cipher_full, cipher,
1114 &key_nums, cipher_mode);
1116 (*cd)->loopaes_cipher = strdup(cipher);
1117 (*cd)->loopaes_cipher_mode = strdup(cipher_mode);
1118 /* version 3 uses last key for IV */
1119 if (key_size % key_nums)
1121 (*cd)->loopaes_key_size = key_size / key_nums;
1123 } else if (!strncmp(CRYPT_LUKS1, device_uuid, sizeof(CRYPT_LUKS1)-1)) {
1125 if (crypt_load(*cd, CRYPT_LUKS1, NULL) < 0 ||
1126 crypt_volume_key_verify(*cd, key, key_size) < 0) {
1127 log_dbg("LUKS device header does not match active device.");
1131 (*cd)->volume_key = crypt_alloc_volume_key(key_size, key);
1132 if (!(*cd)->volume_key) {
1139 log_dbg("Active device has no UUID set, some parameters are not set.");
1146 crypt_safe_free(key);
1153 static int _crypt_format_plain(struct crypt_device *cd,
1155 const char *cipher_mode,
1157 size_t volume_key_size,
1158 struct crypt_params_plain *params)
1160 if (!cipher || !cipher_mode) {
1161 log_err(cd, _("Invalid plain crypt parameters.\n"));
1165 if (volume_key_size > 1024) {
1166 log_err(cd, _("Invalid key size.\n"));
1170 cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
1171 if (!cd->volume_key)
1174 cd->plain_cipher = strdup(cipher);
1175 cd->plain_cipher_mode = strdup(cipher_mode);
1178 cd->plain_uuid = strdup(uuid);
1180 if (params && params->hash)
1181 cd->plain_hdr.hash = strdup(params->hash);
1183 cd->plain_hdr.offset = params ? params->offset : 0;
1184 cd->plain_hdr.skip = params ? params->skip : 0;
1186 if (!cd->plain_cipher || !cd->plain_cipher_mode)
1192 static int _crypt_format_luks1(struct crypt_device *cd,
1194 const char *cipher_mode,
1196 const char *volume_key,
1197 size_t volume_key_size,
1198 struct crypt_params_luks1 *params)
1201 unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
1202 unsigned long alignment_offset = 0;
1205 log_err(cd, _("Can't format LUKS without device.\n"));
1210 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
1213 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
1218 if (params && params->data_alignment)
1219 required_alignment = params->data_alignment * SECTOR_SIZE;
1221 get_topology_alignment(cd->device, &required_alignment,
1222 &alignment_offset, DEFAULT_DISK_ALIGNMENT);
1224 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
1225 (params && params->hash) ? params->hash : "sha1",
1227 required_alignment / SECTOR_SIZE,
1228 alignment_offset / SECTOR_SIZE,
1229 cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1233 /* Wipe first 8 sectors - fs magic numbers etc. */
1234 r = wipe_device_header(cd->device, 8);
1237 log_err(cd, _("Cannot format device %s which is still in use.\n"),
1240 log_err(cd, _("Cannot wipe header on device %s.\n"),
1246 r = LUKS_write_phdr(cd->device, &cd->hdr, cd);
1251 static int _crypt_format_loopaes(struct crypt_device *cd,
1254 size_t volume_key_size,
1255 struct crypt_params_loopaes *params)
1258 log_err(cd, _("Can't format LOOPAES without device.\n"));
1262 if (volume_key_size > 1024) {
1263 log_err(cd, _("Invalid key size.\n"));
1267 cd->loopaes_key_size = volume_key_size;
1269 cd->loopaes_cipher = strdup(cipher ?: DEFAULT_LOOPAES_CIPHER);
1272 cd->loopaes_uuid = strdup(uuid);
1274 if (params && params->hash)
1275 cd->loopaes_hdr.hash = strdup(params->hash);
1277 cd->loopaes_hdr.offset = params ? params->offset : 0;
1282 int crypt_format(struct crypt_device *cd,
1285 const char *cipher_mode,
1287 const char *volume_key,
1288 size_t volume_key_size,
1296 log_dbg("Formatting device %s as type %s.", cd->device ?: "(none)", type);
1298 r = init_crypto(cd);
1303 r = _crypt_format_plain(cd, cipher, cipher_mode,
1304 uuid, volume_key_size, params);
1305 else if (isLUKS(type))
1306 r = _crypt_format_luks1(cd, cipher, cipher_mode,
1307 uuid, volume_key, volume_key_size, params);
1308 else if (isLOOPAES(type))
1309 r = _crypt_format_loopaes(cd, cipher, uuid, volume_key_size, params);
1311 /* FIXME: allow plugins here? */
1312 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
1316 if (!r && !(cd->type = strdup(type)))
1320 crypt_free_volume_key(cd->volume_key);
1321 cd->volume_key = NULL;
1327 int crypt_load(struct crypt_device *cd,
1328 const char *requested_type,
1331 struct luks_phdr hdr;
1334 log_dbg("Trying to load %s crypt type from device %s.",
1335 requested_type ?: "any", cd->device ?: "(none)");
1340 if (requested_type && !isLUKS(requested_type))
1343 r = init_crypto(cd);
1347 r = LUKS_read_phdr(cd->device, &hdr, 1, cd);
1350 memcpy(&cd->hdr, &hdr, sizeof(hdr));
1351 cd->type = strdup(CRYPT_LUKS1);
1359 int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
1361 char *device = NULL, *cipher = NULL, *uuid = NULL, *key = NULL;
1362 uint64_t size, skip, offset;
1363 int key_size, read_only, r;
1365 /* Device context type must be initialised */
1366 if (!cd->type || !crypt_get_uuid(cd))
1369 r = dm_query_device(name, &device, &size, &skip, &offset,
1370 &cipher, &key_size, &key, &read_only, NULL, &uuid);
1372 log_err(NULL, _("Device %s is not active.\n"), name);
1381 r = device_check_and_adjust(cd, device, 0, &new_size, &offset, &read_only);
1385 if (new_size == size) {
1386 log_dbg("Device has already requested size %" PRIu64
1392 log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
1394 r = dm_create_device(name, device, cipher, cd->type,
1395 crypt_get_uuid(cd), new_size, skip, offset,
1396 key_size, key, read_only, 1);
1398 crypt_safe_free(key);
1406 int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
1408 if (!isLUKS(cd->type)) {
1409 log_err(cd, _("This operation is not supported for this device type.\n"));
1413 if (uuid && !strncmp(uuid, cd->hdr.uuid, sizeof(cd->hdr.uuid))) {
1414 log_dbg("UUID is the same as requested (%s) for device %s.",
1420 log_dbg("Requested new UUID change to %s for %s.", uuid, cd->device);
1422 log_dbg("Requested new UUID refresh for %s.", cd->device);
1424 if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
1427 return LUKS_hdr_uuid_set(cd->device, &cd->hdr, uuid, cd);
1430 int crypt_header_backup(struct crypt_device *cd,
1431 const char *requested_type,
1432 const char *backup_file)
1436 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1439 r = init_crypto(cd);
1443 log_dbg("Requested header backup of device %s (%s) to "
1444 "file %s.", cd->device, requested_type, backup_file);
1446 return LUKS_hdr_backup(backup_file, cd->device, &cd->hdr, cd);
1449 int crypt_header_restore(struct crypt_device *cd,
1450 const char *requested_type,
1451 const char *backup_file)
1455 if (requested_type && !isLUKS(requested_type))
1458 /* Some hash functions need initialized gcrypt library */
1459 r = init_crypto(cd);
1463 log_dbg("Requested header restore to device %s (%s) from "
1464 "file %s.", cd->device, requested_type, backup_file);
1466 return LUKS_hdr_restore(backup_file, cd->device, &cd->hdr, cd);
1469 void crypt_free(struct crypt_device *cd)
1472 log_dbg("Releasing crypt device %s context.", cd->device);
1474 if (cd->loop_fd != -1)
1478 crypt_free_volume_key(cd->volume_key);
1481 free(cd->backing_file);
1484 /* used in plain device only */
1485 free((char*)cd->plain_hdr.hash);
1486 free(cd->plain_cipher);
1487 free(cd->plain_cipher_mode);
1488 free(cd->plain_uuid);
1494 int crypt_suspend(struct crypt_device *cd,
1497 crypt_status_info ci;
1498 int r, suspended = 0;
1500 log_dbg("Suspending volume %s.", name);
1502 ci = crypt_status(NULL, name);
1503 if (ci < CRYPT_ACTIVE) {
1504 log_err(cd, _("Volume %s is not active.\n"), name);
1508 if (!cd && dm_init(NULL, 1) < 0)
1511 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1512 NULL, NULL, NULL, NULL, &suspended, NULL);
1517 log_err(cd, _("Volume %s is already suspended.\n"), name);
1522 r = dm_suspend_and_wipe_key(name);
1524 log_err(cd, "Suspend is not supported for device %s.\n", name);
1526 log_err(cd, "Error during suspending device %s.\n", name);
1533 int crypt_resume_by_passphrase(struct crypt_device *cd,
1536 const char *passphrase,
1537 size_t passphrase_size)
1539 struct volume_key *vk = NULL;
1540 int r, suspended = 0;
1542 log_dbg("Resuming volume %s.", name);
1544 if (!isLUKS(cd->type)) {
1545 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1550 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1551 NULL, NULL, NULL, NULL, &suspended, NULL);
1556 log_err(cd, _("Volume %s is not suspended.\n"), name);
1561 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1562 passphrase_size, &cd->hdr, &vk, cd);
1564 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1568 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1570 log_err(cd, "Resume is not supported for device %s.\n", name);
1572 log_err(cd, "Error during resuming device %s.\n", name);
1576 crypt_free_volume_key(vk);
1577 return r < 0 ? r : keyslot;
1580 int crypt_resume_by_keyfile(struct crypt_device *cd,
1583 const char *keyfile,
1584 size_t keyfile_size)
1586 struct volume_key *vk = NULL;
1587 char *passphrase_read = NULL;
1588 unsigned int passphrase_size_read;
1589 int r, suspended = 0;
1591 log_dbg("Resuming volume %s.", name);
1593 if (!isLUKS(cd->type)) {
1594 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1599 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1600 NULL, NULL, NULL, NULL, &suspended, NULL);
1605 log_err(cd, _("Volume %s is not suspended.\n"), name);
1612 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1613 &passphrase_size_read, keyfile, keyfile_size);
1617 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1618 passphrase_size_read, &cd->hdr, &vk, cd);
1623 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1625 log_err(cd, "Error during resuming device %s.\n", name);
1627 crypt_safe_free(passphrase_read);
1628 crypt_free_volume_key(vk);
1629 return r < 0 ? r : keyslot;
1632 // slot manipulation
1633 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1634 int keyslot, // -1 any
1635 const char *passphrase, // NULL -> terminal
1636 size_t passphrase_size,
1637 const char *new_passphrase, // NULL -> terminal
1638 size_t new_passphrase_size)
1640 struct volume_key *vk = NULL;
1641 char *password = NULL, *new_password = NULL;
1642 unsigned int passwordLen, new_passwordLen;
1645 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1646 "new passphrase %sprovided.",
1647 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1649 if (!isLUKS(cd->type)) {
1650 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1654 r = keyslot_verify_or_find_empty(cd, &keyslot);
1658 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1659 /* No slots used, try to use pre-generated key in header */
1660 if (cd->volume_key) {
1661 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1662 r = vk ? 0 : -ENOMEM;
1664 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1667 } else if (passphrase) {
1668 /* Passphrase provided, use it to unlock existing keyslot */
1669 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, passphrase,
1670 passphrase_size, &cd->hdr, &vk, cd);
1672 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1673 r = key_from_terminal(cd, _("Enter any passphrase: "),
1674 &password, &passwordLen, 0);
1678 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
1679 passwordLen, &cd->hdr, &vk, cd);
1680 crypt_safe_free(password);
1686 if (new_passphrase) {
1687 new_password = (char *)new_passphrase;
1688 new_passwordLen = new_passphrase_size;
1690 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1691 &new_password, &new_passwordLen, 1);
1696 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1697 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1702 if (!new_passphrase)
1703 crypt_safe_free(new_password);
1704 crypt_free_volume_key(vk);
1705 return r ?: keyslot;
1708 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1710 const char *keyfile,
1711 size_t keyfile_size,
1712 const char *new_keyfile,
1713 size_t new_keyfile_size)
1715 struct volume_key *vk = NULL;
1716 char *password = NULL; unsigned int passwordLen;
1717 char *new_password = NULL; unsigned int new_passwordLen;
1720 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1721 keyfile ?: "[none]", new_keyfile ?: "[none]");
1723 if (!isLUKS(cd->type)) {
1724 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1728 r = keyslot_verify_or_find_empty(cd, &keyslot);
1732 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1733 /* No slots used, try to use pre-generated key in header */
1734 if (cd->volume_key) {
1735 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1736 r = vk ? 0 : -ENOMEM;
1738 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1742 /* Read password from file of (if NULL) from terminal */
1744 r = key_from_file(cd, _("Enter any passphrase: "),
1745 &password, &passwordLen,
1746 keyfile, keyfile_size);
1748 r = key_from_terminal(cd, _("Enter any passphrase: "),
1749 &password, &passwordLen, 0);
1753 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password, passwordLen,
1761 r = key_from_file(cd, _("Enter new passphrase for key slot: "),
1762 &new_password, &new_passwordLen, new_keyfile,
1765 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1766 &new_password, &new_passwordLen, 1);
1770 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1771 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1773 crypt_safe_free(password);
1774 crypt_safe_free(new_password);
1775 crypt_free_volume_key(vk);
1776 return r < 0 ? r : keyslot;
1779 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1781 const char *volume_key,
1782 size_t volume_key_size,
1783 const char *passphrase,
1784 size_t passphrase_size)
1786 struct volume_key *vk = NULL;
1788 char *new_password = NULL; unsigned int new_passwordLen;
1790 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1792 if (!isLUKS(cd->type)) {
1793 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1798 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1799 else if (cd->volume_key)
1800 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1805 r = LUKS_verify_volume_key(&cd->hdr, vk);
1807 log_err(cd, _("Volume key does not match the volume.\n"));
1811 r = keyslot_verify_or_find_empty(cd, &keyslot);
1816 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1817 &new_password, &new_passwordLen, 1);
1820 passphrase = new_password;
1821 passphrase_size = new_passwordLen;
1824 r = LUKS_set_key(cd->device, keyslot, passphrase, passphrase_size,
1825 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1827 crypt_safe_free(new_password);
1828 crypt_free_volume_key(vk);
1829 return (r < 0) ? r : keyslot;
1832 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1834 crypt_keyslot_info ki;
1836 log_dbg("Destroying keyslot %d.", keyslot);
1838 if (!isLUKS(cd->type)) {
1839 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1843 ki = crypt_keyslot_status(cd, keyslot);
1844 if (ki == CRYPT_SLOT_INVALID) {
1845 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1849 if (ki == CRYPT_SLOT_INACTIVE) {
1850 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1854 return LUKS_del_key(cd->device, keyslot, &cd->hdr, cd);
1857 // activation/deactivation of device mapping
1858 int crypt_activate_by_passphrase(struct crypt_device *cd,
1861 const char *passphrase,
1862 size_t passphrase_size,
1865 crypt_status_info ci;
1866 struct volume_key *vk = NULL;
1867 char *read_passphrase = NULL;
1868 unsigned int passphraseLen = 0;
1871 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1872 name ? "Activating" : "Checking", name ?: "",
1873 keyslot, passphrase ? "" : "[none] ");
1876 ci = crypt_status(NULL, name);
1877 if (ci == CRYPT_INVALID)
1879 else if (ci >= CRYPT_ACTIVE) {
1880 log_err(cd, _("Device %s already exists.\n"), name);
1885 /* plain, use hashed passphrase */
1886 if (isPLAIN(cd->type)) {
1888 r = key_from_terminal(cd, NULL, &read_passphrase,
1892 passphrase = read_passphrase;
1893 passphrase_size = passphraseLen;
1895 r = create_device_helper(cd, name, cd->plain_hdr.hash,
1896 cd->plain_cipher, cd->plain_cipher_mode,
1897 NULL, passphrase, passphrase_size,
1898 cd->volume_key->keylength, 0,
1899 cd->plain_hdr.skip, cd->plain_hdr.offset,
1901 flags & CRYPT_ACTIVATE_READONLY, 0, 0);
1903 } else if (isLUKS(cd->type)) {
1904 /* provided passphrase, do not retry */
1906 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1907 passphrase_size, &cd->hdr, &vk, cd);
1909 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1914 r = open_from_hdr_and_vk(cd, vk, name, flags);
1919 crypt_safe_free(read_passphrase);
1920 crypt_free_volume_key(vk);
1922 return r < 0 ? r : keyslot;
1925 int crypt_activate_by_keyfile(struct crypt_device *cd,
1928 const char *keyfile,
1929 size_t keyfile_size,
1932 crypt_status_info ci;
1933 struct volume_key *vk = NULL;
1934 char *passphrase_read = NULL;
1935 unsigned int passphrase_size_read, key_count = 0;
1938 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1939 name ?: "", keyslot, keyfile ?: "[none]");
1942 ci = crypt_status(NULL, name);
1943 if (ci == CRYPT_INVALID)
1945 else if (ci >= CRYPT_ACTIVE) {
1946 log_err(cd, _("Device %s already exists.\n"), name);
1954 if (isPLAIN(cd->type)) {
1955 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1956 &passphrase_size_read, keyfile, keyfile_size);
1959 r = create_device_helper(cd, name, cd->plain_hdr.hash,
1960 cd->plain_cipher, cd->plain_cipher_mode,
1961 NULL, passphrase_read, passphrase_size_read,
1962 cd->volume_key->keylength, 0,
1963 cd->plain_hdr.skip, cd->plain_hdr.offset,
1965 flags & CRYPT_ACTIVATE_READONLY, 0, 0);
1966 } else if (isLUKS(cd->type)) {
1967 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1968 &passphrase_size_read, keyfile, keyfile_size);
1971 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1972 passphrase_size_read, &cd->hdr, &vk, cd);
1978 r = open_from_hdr_and_vk(cd, vk, name, flags);
1983 } else if (isLOOPAES(cd->type)) {
1984 r = key_from_file(cd, NULL, &passphrase_read, &passphrase_size_read,
1985 keyfile, LOOPAES_KEYFILE_MAXSIZE);
1988 r = LOOPAES_parse_keyfile(cd, &vk, &key_count,
1989 passphrase_read, passphrase_size_read);
1993 r = LOOPAES_activate(cd, name, cd->loopaes_cipher,
1994 key_count, vk, flags);
1999 crypt_safe_free(passphrase_read);
2000 crypt_free_volume_key(vk);
2005 int crypt_activate_by_volume_key(struct crypt_device *cd,
2007 const char *volume_key,
2008 size_t volume_key_size,
2011 crypt_status_info ci;
2012 struct volume_key *vk;
2015 log_dbg("Activating volume %s by volume key.", name);
2017 /* use key directly, no hash */
2018 if (isPLAIN(cd->type)) {
2019 if (!volume_key || !volume_key_size || !cd->volume_key ||
2020 volume_key_size != cd->volume_key->keylength) {
2021 log_err(cd, _("Incorrect volume key specified for plain device.\n"));
2025 return create_device_helper(cd, name, NULL,
2026 cd->plain_cipher, cd->plain_cipher_mode, NULL, volume_key, volume_key_size,
2027 cd->volume_key->keylength, 0, cd->plain_hdr.skip,
2028 cd->plain_hdr.offset, cd->plain_uuid, flags & CRYPT_ACTIVATE_READONLY, 0, 0);
2031 if (!isLUKS(cd->type)) {
2032 log_err(cd, _("Device type is not properly initialised.\n"));
2037 ci = crypt_status(NULL, name);
2038 if (ci == CRYPT_INVALID)
2040 else if (ci >= CRYPT_ACTIVE) {
2041 log_err(cd, _("Device %s already exists.\n"), name);
2046 /* If key is not provided, try to use internal key */
2048 if (!cd->volume_key) {
2049 log_err(cd, _("Volume key does not match the volume.\n"));
2052 volume_key_size = cd->volume_key->keylength;
2053 volume_key = cd->volume_key->key;
2056 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2059 r = LUKS_verify_volume_key(&cd->hdr, vk);
2062 log_err(cd, _("Volume key does not match the volume.\n"));
2065 r = open_from_hdr_and_vk(cd, vk, name, flags);
2067 crypt_free_volume_key(vk);
2072 int crypt_deactivate(struct crypt_device *cd, const char *name)
2079 log_dbg("Deactivating volume %s.", name);
2081 if (!cd && dm_init(NULL, 1) < 0)
2084 switch (crypt_status(cd, name)) {
2086 r = dm_remove_device(name, 0, 0);
2089 log_err(cd, _("Device %s is busy.\n"), name);
2092 case CRYPT_INACTIVE:
2093 log_err(cd, _("Device %s is not active.\n"), name);
2097 log_err(cd, _("Invalid device %s.\n"), name);
2107 int crypt_volume_key_get(struct crypt_device *cd,
2110 size_t *volume_key_size,
2111 const char *passphrase,
2112 size_t passphrase_size)
2114 struct volume_key *vk;
2115 char *processed_key = NULL;
2118 key_len = crypt_get_volume_key_size(cd);
2119 if (key_len > *volume_key_size) {
2120 log_err(cd, _("Volume key buffer too small.\n"));
2124 if (isPLAIN(cd->type) && cd->plain_hdr.hash) {
2125 processed_key = process_key(cd, cd->plain_hdr.hash, NULL, key_len,
2126 passphrase, passphrase_size);
2127 if (!processed_key) {
2128 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
2131 memcpy(volume_key, processed_key, key_len);
2132 *volume_key_size = key_len;
2133 crypt_safe_free(processed_key);
2137 if (isLUKS(cd->type)) {
2138 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
2139 passphrase_size, &cd->hdr, &vk, cd);
2142 memcpy(volume_key, vk->key, vk->keylength);
2143 *volume_key_size = vk->keylength;
2146 crypt_free_volume_key(vk);
2150 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
2154 int crypt_volume_key_verify(struct crypt_device *cd,
2155 const char *volume_key,
2156 size_t volume_key_size)
2158 struct volume_key *vk;
2161 if (!isLUKS(cd->type)) {
2162 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2166 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2170 r = LUKS_verify_volume_key(&cd->hdr, vk);
2173 log_err(cd, _("Volume key does not match the volume.\n"));
2175 crypt_free_volume_key(vk);
2180 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
2182 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
2183 cd->timeout = timeout_sec;
2186 void crypt_set_password_retry(struct crypt_device *cd, int tries)
2188 log_dbg("Password retry count set to %d.", tries);
2192 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2194 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
2195 cd->iteration_time = iteration_time_ms;
2198 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
2200 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
2201 cd->password_verify = password_verify ? 1 : 0;
2204 void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
2207 case CRYPT_RNG_URANDOM:
2208 case CRYPT_RNG_RANDOM:
2209 log_dbg("RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
2210 cd->rng_type = rng_type;
2214 int crypt_get_rng_type(struct crypt_device *cd)
2219 return cd->rng_type;
2222 int crypt_memory_lock(struct crypt_device *cd, int lock)
2224 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
2228 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
2232 if (!cd && dm_init(NULL, 1) < 0)
2233 return CRYPT_INVALID;
2235 r = dm_status_device(name);
2240 if (r < 0 && r != -ENODEV)
2241 return CRYPT_INVALID;
2244 return CRYPT_ACTIVE;
2249 return CRYPT_INACTIVE;
2252 static void hexprintICB(struct crypt_device *cd, char *d, int n)
2255 for(i = 0; i < n; i++)
2256 log_std(cd, "%02hhx ", (char)d[i]);
2259 int crypt_dump(struct crypt_device *cd)
2262 if (!isLUKS(cd->type)) { //FIXME
2263 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2267 log_std(cd, "LUKS header information for %s\n\n", cd->device);
2268 log_std(cd, "Version: \t%d\n", cd->hdr.version);
2269 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
2270 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
2271 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec);
2272 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
2273 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
2274 log_std(cd, "MK digest: \t");
2275 hexprintICB(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE);
2277 log_std(cd, "MK salt: \t");
2278 hexprintICB(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2);
2279 log_std(cd, "\n \t");
2280 hexprintICB(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
2282 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
2283 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
2284 for(i = 0; i < LUKS_NUMKEYS; i++) {
2285 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
2286 log_std(cd, "Key Slot %d: ENABLED\n",i);
2287 log_std(cd, "\tIterations: \t%d\n",
2288 cd->hdr.keyblock[i].passwordIterations);
2289 log_std(cd, "\tSalt: \t");
2290 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt,
2292 log_std(cd, "\n\t \t");
2293 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt +
2294 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
2297 log_std(cd, "\tKey material offset:\t%d\n",
2298 cd->hdr.keyblock[i].keyMaterialOffset);
2299 log_std(cd, "\tAF stripes: \t%d\n",
2300 cd->hdr.keyblock[i].stripes);
2303 log_std(cd, "Key Slot %d: DISABLED\n", i);
2309 const char *crypt_get_cipher(struct crypt_device *cd)
2311 if (isPLAIN(cd->type))
2312 return cd->plain_cipher;
2314 if (isLUKS(cd->type))
2315 return cd->hdr.cipherName;
2317 if (isLOOPAES(cd->type))
2318 return cd->loopaes_cipher;
2323 const char *crypt_get_cipher_mode(struct crypt_device *cd)
2325 if (isPLAIN(cd->type))
2326 return cd->plain_cipher_mode;
2328 if (isLUKS(cd->type))
2329 return cd->hdr.cipherMode;
2331 if (isLOOPAES(cd->type))
2332 return cd->loopaes_cipher_mode;
2337 const char *crypt_get_uuid(struct crypt_device *cd)
2339 if (isLUKS(cd->type))
2340 return cd->hdr.uuid;
2342 if (isPLAIN(cd->type))
2343 return cd->plain_uuid;
2345 if (isLOOPAES(cd->type))
2346 return cd->loopaes_uuid;
2351 const char *crypt_get_device_name(struct crypt_device *cd)
2356 int crypt_get_volume_key_size(struct crypt_device *cd)
2358 if (isPLAIN(cd->type) && cd->volume_key)
2359 return cd->volume_key->keylength;
2361 if (isLUKS(cd->type))
2362 return cd->hdr.keyBytes;
2364 if (isLOOPAES(cd->type))
2365 return cd->loopaes_key_size;
2370 uint64_t crypt_get_data_offset(struct crypt_device *cd)
2372 if (isPLAIN(cd->type))
2373 return cd->plain_hdr.offset;
2375 if (isLUKS(cd->type))
2376 return cd->hdr.payloadOffset;
2378 if (isLOOPAES(cd->type))
2379 return cd->loopaes_hdr.offset;
2384 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
2386 if (!isLUKS(cd->type)) {
2387 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2388 return CRYPT_SLOT_INVALID;
2391 return LUKS_keyslot_info(&cd->hdr, keyslot);
2394 int crypt_keyslot_max(const char *type)
2396 if (type && isLUKS(type))
2397 return LUKS_NUMKEYS;
2402 const char *crypt_get_type(struct crypt_device *cd)
2407 int crypt_get_active_device(struct crypt_device *cd,
2409 struct crypt_active_device *cad)
2413 r = dm_query_device(name, NULL, &cad->size, &cad->iv_offset, &cad->offset,
2414 NULL, NULL, NULL, &readonly, NULL, NULL);
2418 cad->flags = readonly ? CRYPT_ACTIVATE_READONLY : 0;