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",
988 log_err(NULL, _("Cannot find a free loopback device.\n"));
993 /* Keep the loop open, dettached on last close. */
994 h->loop_fd = crypt_loop_attach(h->device, device, 0, &readonly);
995 if (h->loop_fd == -1) {
996 log_err(NULL, _("Attaching loopback device failed "
997 "(loop device with autoclear flag is required).\n"));
1002 h->backing_file = crypt_loop_backing_file(h->device);
1003 r = device_ready(NULL, h->device, O_RDONLY);
1011 if (!h->device && device && !(h->device = strdup(device))) {
1016 if (dm_init(h, 1) < 0) {
1021 h->iteration_time = 1000;
1022 h->password_verify = 0;
1024 h->rng_type = crypt_random_default_key_rng();
1030 if (h->loop_fd != -1)
1033 free(h->backing_file);
1039 int crypt_init_by_name(struct crypt_device **cd, const char *name)
1041 crypt_status_info ci;
1042 struct crypt_active_device cad;
1043 char *device = NULL, *cipher_full = NULL, *device_uuid = NULL;
1044 char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
1046 int key_size = 0, key_nums, r;
1049 log_dbg("Allocating crypt device context by device %s.", name);
1051 ci = crypt_status(NULL, name);
1052 if (ci == CRYPT_INVALID)
1055 if (ci < CRYPT_ACTIVE) {
1056 log_err(NULL, _("Device %s is not active.\n"), name);
1060 r = dm_query_device(name, &device, &cad.size, &cad.iv_offset, &cad.offset,
1061 &cipher_full, &key_size, &key, NULL, NULL,
1067 r = crypt_init(cd, device);
1069 /* Underlying device disappeared but mapping still active */
1070 if (!device || r == -ENOTBLK)
1071 log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
1074 /* Underlying device is not readable but crypt mapping exists */
1075 if (r == -ENOTBLK) {
1078 r = crypt_init(cd, NULL);
1084 /* Try to initialise basic parameters from active device */
1086 if (!(*cd)->backing_file && device && crypt_loop_device(device) &&
1087 !((*cd)->backing_file = crypt_loop_backing_file(device))) {
1093 if (!strncmp(CRYPT_PLAIN, device_uuid, sizeof(CRYPT_PLAIN)-1)) {
1094 (*cd)->type = strdup(CRYPT_PLAIN);
1095 (*cd)->plain_uuid = strdup(device_uuid);
1096 (*cd)->plain_hdr.hash = NULL; /* no way to get this */
1097 (*cd)->plain_hdr.offset = cad.offset;
1098 (*cd)->plain_hdr.skip = cad.iv_offset;
1099 (*cd)->volume_key = crypt_alloc_volume_key(key_size, key);
1100 if (!(*cd)->volume_key) {
1105 r = crypt_parse_name_and_mode(cipher_full, cipher, NULL, cipher_mode);
1107 (*cd)->plain_cipher = strdup(cipher);
1108 (*cd)->plain_cipher_mode = strdup(cipher_mode);
1110 } else if (!strncmp(CRYPT_LOOPAES, device_uuid, sizeof(CRYPT_LOOPAES)-1)) {
1111 (*cd)->type = strdup(CRYPT_LOOPAES);
1112 (*cd)->loopaes_uuid = strdup(device_uuid);
1113 (*cd)->loopaes_hdr.offset = cad.offset;
1115 r = crypt_parse_name_and_mode(cipher_full, cipher,
1116 &key_nums, cipher_mode);
1118 (*cd)->loopaes_cipher = strdup(cipher);
1119 (*cd)->loopaes_cipher_mode = strdup(cipher_mode);
1120 /* version 3 uses last key for IV */
1121 if (key_size % key_nums)
1123 (*cd)->loopaes_key_size = key_size / key_nums;
1125 } else if (!strncmp(CRYPT_LUKS1, device_uuid, sizeof(CRYPT_LUKS1)-1)) {
1127 if (crypt_load(*cd, CRYPT_LUKS1, NULL) < 0 ||
1128 crypt_volume_key_verify(*cd, key, key_size) < 0) {
1129 log_dbg("LUKS device header does not match active device.");
1133 (*cd)->volume_key = crypt_alloc_volume_key(key_size, key);
1134 if (!(*cd)->volume_key) {
1141 log_dbg("Active device has no UUID set, some parameters are not set.");
1148 crypt_safe_free(key);
1155 static int _crypt_format_plain(struct crypt_device *cd,
1157 const char *cipher_mode,
1159 size_t volume_key_size,
1160 struct crypt_params_plain *params)
1162 if (!cipher || !cipher_mode) {
1163 log_err(cd, _("Invalid plain crypt parameters.\n"));
1167 if (volume_key_size > 1024) {
1168 log_err(cd, _("Invalid key size.\n"));
1172 cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
1173 if (!cd->volume_key)
1176 cd->plain_cipher = strdup(cipher);
1177 cd->plain_cipher_mode = strdup(cipher_mode);
1180 cd->plain_uuid = strdup(uuid);
1182 if (params && params->hash)
1183 cd->plain_hdr.hash = strdup(params->hash);
1185 cd->plain_hdr.offset = params ? params->offset : 0;
1186 cd->plain_hdr.skip = params ? params->skip : 0;
1188 if (!cd->plain_cipher || !cd->plain_cipher_mode)
1194 static int _crypt_format_luks1(struct crypt_device *cd,
1196 const char *cipher_mode,
1198 const char *volume_key,
1199 size_t volume_key_size,
1200 struct crypt_params_luks1 *params)
1203 unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
1204 unsigned long alignment_offset = 0;
1207 log_err(cd, _("Can't format LUKS without device.\n"));
1212 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
1215 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
1220 if (params && params->data_alignment)
1221 required_alignment = params->data_alignment * SECTOR_SIZE;
1223 get_topology_alignment(cd->device, &required_alignment,
1224 &alignment_offset, DEFAULT_DISK_ALIGNMENT);
1226 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
1227 (params && params->hash) ? params->hash : "sha1",
1229 required_alignment / SECTOR_SIZE,
1230 alignment_offset / SECTOR_SIZE,
1231 cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1235 /* Wipe first 8 sectors - fs magic numbers etc. */
1236 r = wipe_device_header(cd->device, 8);
1239 log_err(cd, _("Cannot format device %s which is still in use.\n"),
1242 log_err(cd, _("Cannot wipe header on device %s.\n"),
1248 r = LUKS_write_phdr(cd->device, &cd->hdr, cd);
1253 static int _crypt_format_loopaes(struct crypt_device *cd,
1256 size_t volume_key_size,
1257 struct crypt_params_loopaes *params)
1260 log_err(cd, _("Can't format LOOPAES without device.\n"));
1264 if (volume_key_size > 1024) {
1265 log_err(cd, _("Invalid key size.\n"));
1269 cd->loopaes_key_size = volume_key_size;
1271 cd->loopaes_cipher = strdup(cipher ?: DEFAULT_LOOPAES_CIPHER);
1274 cd->loopaes_uuid = strdup(uuid);
1276 if (params && params->hash)
1277 cd->loopaes_hdr.hash = strdup(params->hash);
1279 cd->loopaes_hdr.offset = params ? params->offset : 0;
1284 int crypt_format(struct crypt_device *cd,
1287 const char *cipher_mode,
1289 const char *volume_key,
1290 size_t volume_key_size,
1298 log_dbg("Formatting device %s as type %s.", cd->device ?: "(none)", type);
1300 r = init_crypto(cd);
1305 r = _crypt_format_plain(cd, cipher, cipher_mode,
1306 uuid, volume_key_size, params);
1307 else if (isLUKS(type))
1308 r = _crypt_format_luks1(cd, cipher, cipher_mode,
1309 uuid, volume_key, volume_key_size, params);
1310 else if (isLOOPAES(type))
1311 r = _crypt_format_loopaes(cd, cipher, uuid, volume_key_size, params);
1313 /* FIXME: allow plugins here? */
1314 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
1318 if (!r && !(cd->type = strdup(type)))
1322 crypt_free_volume_key(cd->volume_key);
1323 cd->volume_key = NULL;
1329 int crypt_load(struct crypt_device *cd,
1330 const char *requested_type,
1333 struct luks_phdr hdr;
1336 log_dbg("Trying to load %s crypt type from device %s.",
1337 requested_type ?: "any", cd->device ?: "(none)");
1342 if (requested_type && !isLUKS(requested_type))
1345 r = init_crypto(cd);
1349 r = LUKS_read_phdr(cd->device, &hdr, 1, cd);
1352 memcpy(&cd->hdr, &hdr, sizeof(hdr));
1353 cd->type = strdup(CRYPT_LUKS1);
1361 int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
1363 char *device = NULL, *cipher = NULL, *uuid = NULL, *key = NULL;
1364 uint64_t size, skip, offset;
1365 int key_size, read_only, r;
1367 /* Device context type must be initialised */
1368 if (!cd->type || !crypt_get_uuid(cd))
1371 r = dm_query_device(name, &device, &size, &skip, &offset,
1372 &cipher, &key_size, &key, &read_only, NULL, &uuid);
1374 log_err(NULL, _("Device %s is not active.\n"), name);
1383 r = device_check_and_adjust(cd, device, 0, &new_size, &offset, &read_only);
1387 if (new_size == size) {
1388 log_dbg("Device has already requested size %" PRIu64
1394 log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
1396 r = dm_create_device(name, device, cipher, cd->type,
1397 crypt_get_uuid(cd), new_size, skip, offset,
1398 key_size, key, read_only, 1);
1400 crypt_safe_free(key);
1408 int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
1410 if (!isLUKS(cd->type)) {
1411 log_err(cd, _("This operation is not supported for this device type.\n"));
1415 if (uuid && !strncmp(uuid, cd->hdr.uuid, sizeof(cd->hdr.uuid))) {
1416 log_dbg("UUID is the same as requested (%s) for device %s.",
1422 log_dbg("Requested new UUID change to %s for %s.", uuid, cd->device);
1424 log_dbg("Requested new UUID refresh for %s.", cd->device);
1426 if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
1429 return LUKS_hdr_uuid_set(cd->device, &cd->hdr, uuid, cd);
1432 int crypt_header_backup(struct crypt_device *cd,
1433 const char *requested_type,
1434 const char *backup_file)
1438 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1441 r = init_crypto(cd);
1445 log_dbg("Requested header backup of device %s (%s) to "
1446 "file %s.", cd->device, requested_type, backup_file);
1448 return LUKS_hdr_backup(backup_file, cd->device, &cd->hdr, cd);
1451 int crypt_header_restore(struct crypt_device *cd,
1452 const char *requested_type,
1453 const char *backup_file)
1457 if (requested_type && !isLUKS(requested_type))
1460 /* Some hash functions need initialized gcrypt library */
1461 r = init_crypto(cd);
1465 log_dbg("Requested header restore to device %s (%s) from "
1466 "file %s.", cd->device, requested_type, backup_file);
1468 return LUKS_hdr_restore(backup_file, cd->device, &cd->hdr, cd);
1471 void crypt_free(struct crypt_device *cd)
1474 log_dbg("Releasing crypt device %s context.", cd->device);
1476 if (cd->loop_fd != -1)
1480 crypt_free_volume_key(cd->volume_key);
1483 free(cd->backing_file);
1486 /* used in plain device only */
1487 free((char*)cd->plain_hdr.hash);
1488 free(cd->plain_cipher);
1489 free(cd->plain_cipher_mode);
1490 free(cd->plain_uuid);
1496 int crypt_suspend(struct crypt_device *cd,
1499 crypt_status_info ci;
1500 int r, suspended = 0;
1502 log_dbg("Suspending volume %s.", name);
1504 ci = crypt_status(NULL, name);
1505 if (ci < CRYPT_ACTIVE) {
1506 log_err(cd, _("Volume %s is not active.\n"), name);
1510 if (!cd && dm_init(NULL, 1) < 0)
1513 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1514 NULL, NULL, NULL, NULL, &suspended, NULL);
1519 log_err(cd, _("Volume %s is already suspended.\n"), name);
1524 r = dm_suspend_and_wipe_key(name);
1526 log_err(cd, "Suspend is not supported for device %s.\n", name);
1528 log_err(cd, "Error during suspending device %s.\n", name);
1535 int crypt_resume_by_passphrase(struct crypt_device *cd,
1538 const char *passphrase,
1539 size_t passphrase_size)
1541 struct volume_key *vk = NULL;
1542 int r, suspended = 0;
1544 log_dbg("Resuming volume %s.", name);
1546 if (!isLUKS(cd->type)) {
1547 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1552 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1553 NULL, NULL, NULL, NULL, &suspended, NULL);
1558 log_err(cd, _("Volume %s is not suspended.\n"), name);
1563 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1564 passphrase_size, &cd->hdr, &vk, cd);
1566 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1570 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1572 log_err(cd, "Resume is not supported for device %s.\n", name);
1574 log_err(cd, "Error during resuming device %s.\n", name);
1578 crypt_free_volume_key(vk);
1579 return r < 0 ? r : keyslot;
1582 int crypt_resume_by_keyfile(struct crypt_device *cd,
1585 const char *keyfile,
1586 size_t keyfile_size)
1588 struct volume_key *vk = NULL;
1589 char *passphrase_read = NULL;
1590 unsigned int passphrase_size_read;
1591 int r, suspended = 0;
1593 log_dbg("Resuming volume %s.", name);
1595 if (!isLUKS(cd->type)) {
1596 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1601 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1602 NULL, NULL, NULL, NULL, &suspended, NULL);
1607 log_err(cd, _("Volume %s is not suspended.\n"), name);
1614 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1615 &passphrase_size_read, keyfile, keyfile_size);
1619 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1620 passphrase_size_read, &cd->hdr, &vk, cd);
1625 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1627 log_err(cd, "Error during resuming device %s.\n", name);
1629 crypt_safe_free(passphrase_read);
1630 crypt_free_volume_key(vk);
1631 return r < 0 ? r : keyslot;
1634 // slot manipulation
1635 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1636 int keyslot, // -1 any
1637 const char *passphrase, // NULL -> terminal
1638 size_t passphrase_size,
1639 const char *new_passphrase, // NULL -> terminal
1640 size_t new_passphrase_size)
1642 struct volume_key *vk = NULL;
1643 char *password = NULL, *new_password = NULL;
1644 unsigned int passwordLen, new_passwordLen;
1647 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1648 "new passphrase %sprovided.",
1649 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1651 if (!isLUKS(cd->type)) {
1652 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1656 r = keyslot_verify_or_find_empty(cd, &keyslot);
1660 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1661 /* No slots used, try to use pre-generated key in header */
1662 if (cd->volume_key) {
1663 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1664 r = vk ? 0 : -ENOMEM;
1666 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1669 } else if (passphrase) {
1670 /* Passphrase provided, use it to unlock existing keyslot */
1671 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, passphrase,
1672 passphrase_size, &cd->hdr, &vk, cd);
1674 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1675 r = key_from_terminal(cd, _("Enter any passphrase: "),
1676 &password, &passwordLen, 0);
1680 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
1681 passwordLen, &cd->hdr, &vk, cd);
1682 crypt_safe_free(password);
1688 if (new_passphrase) {
1689 new_password = (char *)new_passphrase;
1690 new_passwordLen = new_passphrase_size;
1692 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1693 &new_password, &new_passwordLen, 1);
1698 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1699 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1704 if (!new_passphrase)
1705 crypt_safe_free(new_password);
1706 crypt_free_volume_key(vk);
1707 return r ?: keyslot;
1710 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1712 const char *keyfile,
1713 size_t keyfile_size,
1714 const char *new_keyfile,
1715 size_t new_keyfile_size)
1717 struct volume_key *vk = NULL;
1718 char *password = NULL; unsigned int passwordLen;
1719 char *new_password = NULL; unsigned int new_passwordLen;
1722 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1723 keyfile ?: "[none]", new_keyfile ?: "[none]");
1725 if (!isLUKS(cd->type)) {
1726 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1730 r = keyslot_verify_or_find_empty(cd, &keyslot);
1734 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1735 /* No slots used, try to use pre-generated key in header */
1736 if (cd->volume_key) {
1737 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1738 r = vk ? 0 : -ENOMEM;
1740 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1744 /* Read password from file of (if NULL) from terminal */
1746 r = key_from_file(cd, _("Enter any passphrase: "),
1747 &password, &passwordLen,
1748 keyfile, keyfile_size);
1750 r = key_from_terminal(cd, _("Enter any passphrase: "),
1751 &password, &passwordLen, 0);
1755 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password, passwordLen,
1763 r = key_from_file(cd, _("Enter new passphrase for key slot: "),
1764 &new_password, &new_passwordLen, new_keyfile,
1767 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1768 &new_password, &new_passwordLen, 1);
1772 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1773 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1775 crypt_safe_free(password);
1776 crypt_safe_free(new_password);
1777 crypt_free_volume_key(vk);
1778 return r < 0 ? r : keyslot;
1781 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1783 const char *volume_key,
1784 size_t volume_key_size,
1785 const char *passphrase,
1786 size_t passphrase_size)
1788 struct volume_key *vk = NULL;
1790 char *new_password = NULL; unsigned int new_passwordLen;
1792 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1794 if (!isLUKS(cd->type)) {
1795 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1800 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1801 else if (cd->volume_key)
1802 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1807 r = LUKS_verify_volume_key(&cd->hdr, vk);
1809 log_err(cd, _("Volume key does not match the volume.\n"));
1813 r = keyslot_verify_or_find_empty(cd, &keyslot);
1818 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1819 &new_password, &new_passwordLen, 1);
1822 passphrase = new_password;
1823 passphrase_size = new_passwordLen;
1826 r = LUKS_set_key(cd->device, keyslot, passphrase, passphrase_size,
1827 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1829 crypt_safe_free(new_password);
1830 crypt_free_volume_key(vk);
1831 return (r < 0) ? r : keyslot;
1834 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1836 crypt_keyslot_info ki;
1838 log_dbg("Destroying keyslot %d.", keyslot);
1840 if (!isLUKS(cd->type)) {
1841 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1845 ki = crypt_keyslot_status(cd, keyslot);
1846 if (ki == CRYPT_SLOT_INVALID) {
1847 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1851 if (ki == CRYPT_SLOT_INACTIVE) {
1852 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1856 return LUKS_del_key(cd->device, keyslot, &cd->hdr, cd);
1859 // activation/deactivation of device mapping
1860 int crypt_activate_by_passphrase(struct crypt_device *cd,
1863 const char *passphrase,
1864 size_t passphrase_size,
1867 crypt_status_info ci;
1868 struct volume_key *vk = NULL;
1869 char *read_passphrase = NULL;
1870 unsigned int passphraseLen = 0;
1873 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1874 name ? "Activating" : "Checking", name ?: "",
1875 keyslot, passphrase ? "" : "[none] ");
1878 ci = crypt_status(NULL, name);
1879 if (ci == CRYPT_INVALID)
1881 else if (ci >= CRYPT_ACTIVE) {
1882 log_err(cd, _("Device %s already exists.\n"), name);
1887 /* plain, use hashed passphrase */
1888 if (isPLAIN(cd->type)) {
1890 r = key_from_terminal(cd, NULL, &read_passphrase,
1894 passphrase = read_passphrase;
1895 passphrase_size = passphraseLen;
1897 r = create_device_helper(cd, name, cd->plain_hdr.hash,
1898 cd->plain_cipher, cd->plain_cipher_mode,
1899 NULL, passphrase, passphrase_size,
1900 cd->volume_key->keylength, 0,
1901 cd->plain_hdr.skip, cd->plain_hdr.offset,
1903 flags & CRYPT_ACTIVATE_READONLY, 0, 0);
1905 } else if (isLUKS(cd->type)) {
1906 /* provided passphrase, do not retry */
1908 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1909 passphrase_size, &cd->hdr, &vk, cd);
1911 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1916 r = open_from_hdr_and_vk(cd, vk, name, flags);
1921 crypt_safe_free(read_passphrase);
1922 crypt_free_volume_key(vk);
1924 return r < 0 ? r : keyslot;
1927 int crypt_activate_by_keyfile(struct crypt_device *cd,
1930 const char *keyfile,
1931 size_t keyfile_size,
1934 crypt_status_info ci;
1935 struct volume_key *vk = NULL;
1936 char *passphrase_read = NULL;
1937 unsigned int passphrase_size_read, key_count = 0;
1940 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1941 name ?: "", keyslot, keyfile ?: "[none]");
1944 ci = crypt_status(NULL, name);
1945 if (ci == CRYPT_INVALID)
1947 else if (ci >= CRYPT_ACTIVE) {
1948 log_err(cd, _("Device %s already exists.\n"), name);
1956 if (isPLAIN(cd->type)) {
1957 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1958 &passphrase_size_read, keyfile, keyfile_size);
1961 r = create_device_helper(cd, name, cd->plain_hdr.hash,
1962 cd->plain_cipher, cd->plain_cipher_mode,
1963 NULL, passphrase_read, passphrase_size_read,
1964 cd->volume_key->keylength, 0,
1965 cd->plain_hdr.skip, cd->plain_hdr.offset,
1967 flags & CRYPT_ACTIVATE_READONLY, 0, 0);
1968 } else if (isLUKS(cd->type)) {
1969 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1970 &passphrase_size_read, keyfile, keyfile_size);
1973 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1974 passphrase_size_read, &cd->hdr, &vk, cd);
1980 r = open_from_hdr_and_vk(cd, vk, name, flags);
1985 } else if (isLOOPAES(cd->type)) {
1986 r = key_from_file(cd, NULL, &passphrase_read, &passphrase_size_read,
1987 keyfile, LOOPAES_KEYFILE_MAXSIZE);
1990 r = LOOPAES_parse_keyfile(cd, &vk, &key_count,
1991 passphrase_read, passphrase_size_read);
1995 r = LOOPAES_activate(cd, name, cd->loopaes_cipher,
1996 key_count, vk, flags);
2001 crypt_safe_free(passphrase_read);
2002 crypt_free_volume_key(vk);
2007 int crypt_activate_by_volume_key(struct crypt_device *cd,
2009 const char *volume_key,
2010 size_t volume_key_size,
2013 crypt_status_info ci;
2014 struct volume_key *vk;
2017 log_dbg("Activating volume %s by volume key.", name);
2019 /* use key directly, no hash */
2020 if (isPLAIN(cd->type)) {
2021 if (!volume_key || !volume_key_size || !cd->volume_key ||
2022 volume_key_size != cd->volume_key->keylength) {
2023 log_err(cd, _("Incorrect volume key specified for plain device.\n"));
2027 return create_device_helper(cd, name, NULL,
2028 cd->plain_cipher, cd->plain_cipher_mode, NULL, volume_key, volume_key_size,
2029 cd->volume_key->keylength, 0, cd->plain_hdr.skip,
2030 cd->plain_hdr.offset, cd->plain_uuid, flags & CRYPT_ACTIVATE_READONLY, 0, 0);
2033 if (!isLUKS(cd->type)) {
2034 log_err(cd, _("Device type is not properly initialised.\n"));
2039 ci = crypt_status(NULL, name);
2040 if (ci == CRYPT_INVALID)
2042 else if (ci >= CRYPT_ACTIVE) {
2043 log_err(cd, _("Device %s already exists.\n"), name);
2048 /* If key is not provided, try to use internal key */
2050 if (!cd->volume_key) {
2051 log_err(cd, _("Volume key does not match the volume.\n"));
2054 volume_key_size = cd->volume_key->keylength;
2055 volume_key = cd->volume_key->key;
2058 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2061 r = LUKS_verify_volume_key(&cd->hdr, vk);
2064 log_err(cd, _("Volume key does not match the volume.\n"));
2067 r = open_from_hdr_and_vk(cd, vk, name, flags);
2069 crypt_free_volume_key(vk);
2074 int crypt_deactivate(struct crypt_device *cd, const char *name)
2081 log_dbg("Deactivating volume %s.", name);
2083 if (!cd && dm_init(NULL, 1) < 0)
2086 switch (crypt_status(cd, name)) {
2088 r = dm_remove_device(name, 0, 0);
2091 log_err(cd, _("Device %s is busy.\n"), name);
2094 case CRYPT_INACTIVE:
2095 log_err(cd, _("Device %s is not active.\n"), name);
2099 log_err(cd, _("Invalid device %s.\n"), name);
2109 int crypt_volume_key_get(struct crypt_device *cd,
2112 size_t *volume_key_size,
2113 const char *passphrase,
2114 size_t passphrase_size)
2116 struct volume_key *vk;
2117 char *processed_key = NULL;
2120 key_len = crypt_get_volume_key_size(cd);
2121 if (key_len > *volume_key_size) {
2122 log_err(cd, _("Volume key buffer too small.\n"));
2126 if (isPLAIN(cd->type) && cd->plain_hdr.hash) {
2127 processed_key = process_key(cd, cd->plain_hdr.hash, NULL, key_len,
2128 passphrase, passphrase_size);
2129 if (!processed_key) {
2130 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
2133 memcpy(volume_key, processed_key, key_len);
2134 *volume_key_size = key_len;
2135 crypt_safe_free(processed_key);
2139 if (isLUKS(cd->type)) {
2140 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
2141 passphrase_size, &cd->hdr, &vk, cd);
2144 memcpy(volume_key, vk->key, vk->keylength);
2145 *volume_key_size = vk->keylength;
2148 crypt_free_volume_key(vk);
2152 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
2156 int crypt_volume_key_verify(struct crypt_device *cd,
2157 const char *volume_key,
2158 size_t volume_key_size)
2160 struct volume_key *vk;
2163 if (!isLUKS(cd->type)) {
2164 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2168 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2172 r = LUKS_verify_volume_key(&cd->hdr, vk);
2175 log_err(cd, _("Volume key does not match the volume.\n"));
2177 crypt_free_volume_key(vk);
2182 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
2184 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
2185 cd->timeout = timeout_sec;
2188 void crypt_set_password_retry(struct crypt_device *cd, int tries)
2190 log_dbg("Password retry count set to %d.", tries);
2194 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2196 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
2197 cd->iteration_time = iteration_time_ms;
2200 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
2202 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
2203 cd->password_verify = password_verify ? 1 : 0;
2206 void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
2209 case CRYPT_RNG_URANDOM:
2210 case CRYPT_RNG_RANDOM:
2211 log_dbg("RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
2212 cd->rng_type = rng_type;
2216 int crypt_get_rng_type(struct crypt_device *cd)
2221 return cd->rng_type;
2224 int crypt_memory_lock(struct crypt_device *cd, int lock)
2226 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
2230 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
2234 if (!cd && dm_init(NULL, 1) < 0)
2235 return CRYPT_INVALID;
2237 r = dm_status_device(name);
2242 if (r < 0 && r != -ENODEV)
2243 return CRYPT_INVALID;
2246 return CRYPT_ACTIVE;
2251 return CRYPT_INACTIVE;
2254 static void hexprintICB(struct crypt_device *cd, char *d, int n)
2257 for(i = 0; i < n; i++)
2258 log_std(cd, "%02hhx ", (char)d[i]);
2261 int crypt_dump(struct crypt_device *cd)
2264 if (!isLUKS(cd->type)) { //FIXME
2265 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2269 log_std(cd, "LUKS header information for %s\n\n", cd->device);
2270 log_std(cd, "Version: \t%d\n", cd->hdr.version);
2271 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
2272 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
2273 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec);
2274 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
2275 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
2276 log_std(cd, "MK digest: \t");
2277 hexprintICB(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE);
2279 log_std(cd, "MK salt: \t");
2280 hexprintICB(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2);
2281 log_std(cd, "\n \t");
2282 hexprintICB(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
2284 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
2285 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
2286 for(i = 0; i < LUKS_NUMKEYS; i++) {
2287 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
2288 log_std(cd, "Key Slot %d: ENABLED\n",i);
2289 log_std(cd, "\tIterations: \t%d\n",
2290 cd->hdr.keyblock[i].passwordIterations);
2291 log_std(cd, "\tSalt: \t");
2292 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt,
2294 log_std(cd, "\n\t \t");
2295 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt +
2296 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
2299 log_std(cd, "\tKey material offset:\t%d\n",
2300 cd->hdr.keyblock[i].keyMaterialOffset);
2301 log_std(cd, "\tAF stripes: \t%d\n",
2302 cd->hdr.keyblock[i].stripes);
2305 log_std(cd, "Key Slot %d: DISABLED\n", i);
2311 const char *crypt_get_cipher(struct crypt_device *cd)
2313 if (isPLAIN(cd->type))
2314 return cd->plain_cipher;
2316 if (isLUKS(cd->type))
2317 return cd->hdr.cipherName;
2319 if (isLOOPAES(cd->type))
2320 return cd->loopaes_cipher;
2325 const char *crypt_get_cipher_mode(struct crypt_device *cd)
2327 if (isPLAIN(cd->type))
2328 return cd->plain_cipher_mode;
2330 if (isLUKS(cd->type))
2331 return cd->hdr.cipherMode;
2333 if (isLOOPAES(cd->type))
2334 return cd->loopaes_cipher_mode;
2339 const char *crypt_get_uuid(struct crypt_device *cd)
2341 if (isLUKS(cd->type))
2342 return cd->hdr.uuid;
2344 if (isPLAIN(cd->type))
2345 return cd->plain_uuid;
2347 if (isLOOPAES(cd->type))
2348 return cd->loopaes_uuid;
2353 const char *crypt_get_device_name(struct crypt_device *cd)
2358 int crypt_get_volume_key_size(struct crypt_device *cd)
2360 if (isPLAIN(cd->type) && cd->volume_key)
2361 return cd->volume_key->keylength;
2363 if (isLUKS(cd->type))
2364 return cd->hdr.keyBytes;
2366 if (isLOOPAES(cd->type))
2367 return cd->loopaes_key_size;
2372 uint64_t crypt_get_data_offset(struct crypt_device *cd)
2374 if (isPLAIN(cd->type))
2375 return cd->plain_hdr.offset;
2377 if (isLUKS(cd->type))
2378 return cd->hdr.payloadOffset;
2380 if (isLOOPAES(cd->type))
2381 return cd->loopaes_hdr.offset;
2386 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
2388 if (!isLUKS(cd->type)) {
2389 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2390 return CRYPT_SLOT_INVALID;
2393 return LUKS_keyslot_info(&cd->hdr, keyslot);
2396 int crypt_keyslot_max(const char *type)
2398 if (type && isLUKS(type))
2399 return LUKS_NUMKEYS;
2404 const char *crypt_get_type(struct crypt_device *cd)
2409 int crypt_get_active_device(struct crypt_device *cd,
2411 struct crypt_active_device *cad)
2415 r = dm_query_device(name, NULL, &cad->size, &cad->iv_offset, &cad->offset,
2416 NULL, NULL, NULL, &readonly, NULL, NULL);
2420 cad->flags = readonly ? CRYPT_ACTIVATE_READONLY : 0;