8 #include "libcryptsetup.h"
12 #include "crypto_backend.h"
18 struct volume_key *volume_key;
20 uint64_t iteration_time;
25 /* used in CRYPT_LUKS1 */
27 uint64_t PBKDF2_per_sec;
29 /* used in CRYPT_PLAIN */
30 struct crypt_params_plain plain_hdr;
32 char *plain_cipher_mode;
35 /* used in CRYPT_LOOPAES */
36 struct crypt_params_loopaes loopaes_hdr;
38 char *loopaes_cipher_mode;
40 unsigned int loopaes_key_size;
42 /* callbacks definitions */
43 void (*log)(int level, const char *msg, void *usrptr);
45 int (*confirm)(const char *msg, void *usrptr);
47 int (*password)(const char *msg, char *buf, size_t length, void *usrptr);
48 void *password_usrptr;
52 static void (*_default_log)(int level, const char *msg, void *usrptr) = NULL;
53 static int _debug_level = 0;
55 void crypt_set_debug_level(int level)
60 int crypt_get_debug_level()
65 void crypt_log(struct crypt_device *cd, int level, const char *msg)
68 cd->log(level, msg, cd->log_usrptr);
69 else if (_default_log)
70 _default_log(level, msg, NULL);
73 void logger(struct crypt_device *cd, int level, const char *file,
74 int line, const char *format, ...)
79 va_start(argp, format);
81 if (vasprintf(&target, format, argp) > 0) {
83 crypt_log(cd, level, target);
85 } else if (_debug_level)
86 printf("# %s:%d %s\n", file ?: "?", line, target);
88 } else if (_debug_level)
89 printf("# %s\n", target);
97 static int init_crypto(struct crypt_device *ctx)
101 r = crypt_random_init(ctx);
103 log_err(ctx, _("Cannot initialize crypto RNG backend.\n"));
107 r = crypt_backend_init(ctx);
109 log_err(ctx, _("Cannot initialize crypto backend.\n"));
115 * Password processing behaviour matrix of process_key
117 * from binary file: check if there is sufficently large key material
118 * interactive & from fd: hash if requested, otherwise crop or pad with '0'
120 static char *process_key(struct crypt_device *cd, const char *hash_name,
121 const char *key_file, size_t key_size,
122 const char *pass, size_t passLen)
129 key = crypt_safe_alloc(key_size);
130 memset(key, 0, key_size);
132 /* key is coming from binary file */
133 if (key_file && strcmp(key_file, "-")) {
134 if(passLen < key_size) {
135 log_err(cd, _("Cannot not read %d bytes from key file %s.\n"),
137 crypt_safe_free(key);
140 memcpy(key, pass, key_size);
144 /* key is coming from tty, fd or binary stdin */
146 if (crypt_plain_hash(cd, hash_name, key, key_size, pass, passLen) < 0) {
147 log_err(cd, _("Key processing error (using hash algorithm %s).\n"),
149 crypt_safe_free(key);
152 } else if (passLen > key_size) {
153 memcpy(key, pass, key_size);
155 memcpy(key, pass, passLen);
161 static int isPLAIN(const char *type)
163 return (type && !strcmp(CRYPT_PLAIN, type));
166 static int isLUKS(const char *type)
168 return (type && !strcmp(CRYPT_LUKS1, type));
171 static int isLOOPAES(const char *type)
173 return (type && !strcmp(CRYPT_LOOPAES, type));
176 /* keyslot helpers */
177 static int keyslot_verify_or_find_empty(struct crypt_device *cd, int *keyslot)
179 if (*keyslot == CRYPT_ANY_SLOT) {
180 *keyslot = LUKS_keyslot_find_empty(&cd->hdr);
182 log_err(cd, _("All key slots full.\n"));
187 switch (LUKS_keyslot_info(&cd->hdr, *keyslot)) {
188 case CRYPT_SLOT_INVALID:
189 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
190 *keyslot, LUKS_NUMKEYS - 1);
192 case CRYPT_SLOT_INACTIVE:
195 log_err(cd, _("Key slot %d is full, please select another one.\n"),
203 static int verify_other_keyslot(struct crypt_device *cd,
204 const char *key_file,
207 struct volume_key *vk = NULL;
208 crypt_keyslot_info ki;
210 char *password = NULL;
211 unsigned int passwordLen;
213 r = crypt_get_key(_("Enter any remaining LUKS passphrase: "),
214 &password, &passwordLen, 0, key_file, cd->timeout,
215 cd->password_verify, cd);
219 ki = crypt_keyslot_status(cd, keyIndex);
220 if (ki == CRYPT_SLOT_ACTIVE) /* Not last slot */
221 LUKS_keyslot_set(&cd->hdr, keyIndex, 0);
223 openedIndex = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT,
224 password, passwordLen,
227 if (ki == CRYPT_SLOT_ACTIVE)
228 LUKS_keyslot_set(&cd->hdr, keyIndex, 1);
233 log_verbose(cd, _("Key slot %d verified.\n"), openedIndex);
235 crypt_free_volume_key(vk);
236 crypt_safe_free(password);
240 static int find_keyslot_by_passphrase(struct crypt_device *cd,
241 const char *key_file,
244 struct volume_key *vk = NULL;
245 char *password = NULL;
246 unsigned int passwordLen;
249 r = crypt_get_key(message,&password,&passwordLen, 0, key_file,
250 cd->timeout, cd->password_verify, cd);
254 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
255 passwordLen, &cd->hdr, &vk, cd);
257 crypt_free_volume_key(vk);
258 crypt_safe_free(password);
262 static int luks_remove_helper(struct crypt_device *cd,
264 const char *other_key_file,
265 const char *key_file,
268 crypt_keyslot_info ki;
271 if (key_slot == CRYPT_ANY_SLOT) {
272 key_slot = find_keyslot_by_passphrase(cd, key_file,
273 _("Enter LUKS passphrase to be deleted: "));
279 log_std(cd, _("Key slot %d selected for deletion.\n"), key_slot);
282 ki = crypt_keyslot_status(cd, key_slot);
283 if (ki == CRYPT_SLOT_INVALID) {
284 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
285 key_slot, LUKS_NUMKEYS - 1);
289 if (ki <= CRYPT_SLOT_INACTIVE) {
290 log_err(cd, _("Key %d not active. Can't wipe.\n"), key_slot);
295 if (ki == CRYPT_SLOT_ACTIVE_LAST && cd->confirm &&
296 !(cd->confirm(_("This is the last keyslot."
297 " Device will become unusable after purging this key."),
298 cd->confirm_usrptr))) {
304 r = verify_other_keyslot(cd, other_key_file, key_slot);
309 r = crypt_keyslot_destroy(cd, key_slot);
311 return (r < 0) ? r : 0;
314 static int create_device_helper(struct crypt_device *cd,
318 const char *cipher_mode,
319 const char *key_file,
331 crypt_status_info ci;
332 char *dm_cipher = NULL;
333 char *processed_key = NULL;
339 ci = crypt_status(cd, name);
340 if (ci == CRYPT_INVALID)
343 if (reload && ci < CRYPT_ACTIVE)
346 if (!reload && ci >= CRYPT_ACTIVE) {
347 log_err(cd, _("Device %s already exists.\n"), name);
351 if (key_size < 0 || key_size > 1024) {
352 log_err(cd, _("Invalid key size %d.\n"), key_size);
356 r = device_check_and_adjust(cd, cd->device, !reload, &size, &offset, &read_only);
360 if (cipher_mode && asprintf(&dm_cipher, "%s-%s", cipher, cipher_mode) < 0)
363 processed_key = process_key(cd, hash, key_file, key_size, key, keyLen);
364 if (!processed_key) {
369 r = dm_create_device(name, cd->device, dm_cipher ?: cipher, cd->type, uuid, size, skip, offset,
370 key_size, processed_key, read_only, reload);
373 crypt_safe_free(processed_key);
377 static int open_from_hdr_and_vk(struct crypt_device *cd,
378 struct volume_key *vk,
382 uint64_t size, offset;
384 int read_only, no_uuid, r;
387 offset = crypt_get_data_offset(cd);
388 read_only = flags & CRYPT_ACTIVATE_READONLY;
389 no_uuid = flags & CRYPT_ACTIVATE_NO_UUID;
391 r = device_check_and_adjust(cd, cd->device, 1, &size, &offset, &read_only);
395 if (asprintf(&cipher, "%s-%s", crypt_get_cipher(cd),
396 crypt_get_cipher_mode(cd)) < 0)
399 r = dm_create_device(name, cd->device, cipher, cd->type,
400 no_uuid ? NULL : crypt_get_uuid(cd),
401 size, 0, offset, vk->keylength, vk->key,
407 static void log_wrapper(int level, const char *msg, void *usrptr)
409 void (*xlog)(int level, char *msg) = usrptr;
410 xlog(level, (char *)msg);
413 static int yesDialog_wrapper(const char *msg, void *usrptr)
415 int (*xyesDialog)(char *msg) = usrptr;
416 return xyesDialog((char*)msg);
419 int crypt_confirm(struct crypt_device *cd, const char *msg)
421 if (!cd || !cd->confirm)
424 return cd->confirm(msg, cd->confirm_usrptr);
427 static int key_from_terminal(struct crypt_device *cd, char *msg, char **key,
428 unsigned int *key_len, int force_verify)
434 if(!msg && asprintf(&prompt, _("Enter passphrase for %s: "),
442 *key = crypt_safe_alloc(MAX_TTY_PASSWORD_LEN);
447 r = cd->password(msg, *key, MAX_TTY_PASSWORD_LEN, cd->password_usrptr);
449 crypt_safe_free(*key);
454 r = crypt_get_key(msg, key, key_len, 0, NULL, cd->timeout,
455 (force_verify || cd->password_verify), cd);
458 return (r < 0) ? r: 0;
461 static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslot,
462 struct volume_key **vk)
464 char *passphrase_read = NULL;
465 unsigned int passphrase_size_read;
466 int r = -EINVAL, tries = cd->tries;
470 crypt_free_volume_key(*vk);
473 r = key_from_terminal(cd, NULL, &passphrase_read,
474 &passphrase_size_read, 0);
478 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
479 passphrase_size_read, &cd->hdr, vk, cd);
480 crypt_safe_free(passphrase_read);
481 passphrase_read = NULL;
482 } while (r == -EPERM && (--tries > 0));
485 crypt_free_volume_key(*vk);
489 crypt_safe_free(passphrase_read);
493 static int key_from_file(struct crypt_device *cd, char *msg,
494 char **key, unsigned int *key_len,
495 const char *key_file, size_t key_size)
497 return crypt_get_key(msg, key, key_len, key_size, key_file,
501 static int _crypt_init(struct crypt_device **cd,
503 struct crypt_options *options,
504 int load, int need_dm)
508 /* if it is plain device and mapping table is being reloaded
509 initialize it by name*/
510 init_by_name = (type && !strcmp(type, CRYPT_PLAIN) && load);
512 /* Some of old API calls do not require DM in kernel,
513 fake initialisation by initialise it with kernel_check disabled */
515 (void)dm_init(NULL, 0);
517 r = crypt_init_by_name(cd, options->name);
519 r = crypt_init(cd, options->device);
526 crypt_set_log_callback(*cd, log_wrapper, options->icb->log);
527 crypt_set_confirm_callback(*cd, yesDialog_wrapper, options->icb->yesDialog);
529 crypt_set_timeout(*cd, options->timeout);
530 crypt_set_password_retry(*cd, options->tries);
531 crypt_set_iterarion_time(*cd, options->iteration_time ?: 1000);
532 crypt_set_password_verify(*cd, options->flags & CRYPT_FLAG_VERIFY);
534 if (load && !init_by_name)
535 r = crypt_load(*cd, type, NULL);
537 if (!r && type && !(*cd)->type) {
538 (*cd)->type = strdup(type);
549 void crypt_set_log_callback(struct crypt_device *cd,
550 void (*log)(int level, const char *msg, void *usrptr),
557 cd->log_usrptr = usrptr;
561 void crypt_set_confirm_callback(struct crypt_device *cd,
562 int (*confirm)(const char *msg, void *usrptr),
565 cd->confirm = confirm;
566 cd->confirm_usrptr = usrptr;
569 void crypt_set_password_callback(struct crypt_device *cd,
570 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
573 cd->password = password;
574 cd->password_usrptr = usrptr;
577 /* OPTIONS: name, cipher, device, hash, key_file, key_size, key_slot,
578 * offset, size, skip, timeout, tries, passphrase_fd (ignored),
580 static int crypt_create_and_update_device(struct crypt_options *options, int update)
582 struct crypt_device *cd = NULL;
587 r = _crypt_init(&cd, CRYPT_PLAIN, options, 0, 1);
591 r = crypt_get_key(_("Enter passphrase: "), &key, &keyLen, options->key_size,
592 options->key_file, cd->timeout, cd->password_verify, cd);
594 r = create_device_helper(cd, options->name, options->hash,
595 options->cipher, NULL, options->key_file, key, keyLen,
596 options->key_size, options->size, options->skip,
597 options->offset, NULL, options->flags & CRYPT_FLAG_READONLY,
598 options->flags, update);
600 crypt_safe_free(key);
605 int crypt_create_device(struct crypt_options *options)
607 return crypt_create_and_update_device(options, 0);
610 int crypt_update_device(struct crypt_options *options)
612 return crypt_create_and_update_device(options, 1);
615 /* OPTIONS: name, size, icb */
616 int crypt_resize_device(struct crypt_options *options)
618 struct crypt_device *cd = NULL;
619 char *device = NULL, *cipher = NULL, *uuid = NULL, *key = NULL;
621 uint64_t size, skip, offset;
622 int key_size, read_only, r;
624 log_dbg("Resizing device %s to %" PRIu64 " sectors.", options->name, options->size);
626 if (dm_init(NULL, 1) < 0)
629 r = dm_query_device(options->name, &device, &size, &skip, &offset,
630 &cipher, &key_size, &key, &read_only, NULL, &uuid);
632 log_err(NULL, _("Device %s is not active.\n"), options->name);
636 /* Try to determine type of device from UUID */
639 if (!strncmp(uuid, CRYPT_PLAIN, strlen(CRYPT_PLAIN))) {
643 } else if (!strncmp(uuid, CRYPT_LUKS1, strlen(CRYPT_LUKS1)))
647 if (!options->device)
648 options->device = device;
650 r = _crypt_init(&cd, type, options, 1, 1);
654 size = options->size;
655 r = device_check_and_adjust(cd, device, 0, &size, &offset, &read_only);
659 r = dm_create_device(options->name, device, cipher, type,
660 crypt_get_uuid(cd), size, skip, offset,
661 key_size, key, read_only, 1);
663 crypt_safe_free(key);
665 if (options->device == device)
666 options->device = NULL;
674 /* OPTIONS: name, icb */
675 int crypt_query_device(struct crypt_options *options)
679 log_dbg("Query device %s.", options->name);
681 if (dm_init(NULL, 1) < 0)
684 r = dm_status_device(options->name);
688 r = dm_query_device(options->name, (char **)&options->device, &options->size,
689 &options->skip, &options->offset, (char **)&options->cipher,
690 &options->key_size, NULL, &read_only, NULL, NULL);
693 options->flags |= CRYPT_FLAG_READONLY;
695 options->flags |= CRYPT_FLAG_FREE_DEVICE;
696 options->flags |= CRYPT_FLAG_FREE_CIPHER;
708 /* OPTIONS: name, icb */
709 int crypt_remove_device(struct crypt_options *options)
711 struct crypt_device *cd = NULL;
714 r = crypt_init_by_name(&cd, options->name);
716 r = crypt_deactivate(cd, options->name);
723 /* OPTIONS: device, cipher, hash, align_payload, key_size (master key), key_slot
724 * new_key_file, iteration_time, timeout, flags, icb */
725 int crypt_luksFormat(struct crypt_options *options)
727 char cipherName[LUKS_CIPHERNAME_L];
728 char cipherMode[LUKS_CIPHERMODE_L];
730 unsigned int passwordLen;
731 struct crypt_device *cd = NULL;
732 struct crypt_params_luks1 cp = {
733 .hash = options->hash,
734 .data_alignment = options->align_payload
738 r = crypt_parse_name_and_mode(options->cipher, cipherName, NULL, cipherMode);
740 log_err(cd, _("No known cipher specification pattern detected.\n"));
744 if ((r = _crypt_init(&cd, CRYPT_LUKS1, options, 0, 1)))
747 if (options->key_slot >= LUKS_NUMKEYS && options->key_slot != CRYPT_ANY_SLOT) {
748 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
749 options->key_slot, LUKS_NUMKEYS - 1);
754 r = crypt_get_key(_("Enter LUKS passphrase: "), &password, &passwordLen, 0,
755 options->new_key_file, cd->timeout, cd->password_verify, cd);
760 r = crypt_format(cd, CRYPT_LUKS1, cipherName, cipherMode,
761 NULL, NULL, options->key_size, &cp);
765 /* Add keyslot using internally stored volume key generated during format */
766 r = crypt_keyslot_add_by_volume_key(cd, options->key_slot, NULL, 0,
767 password, passwordLen);
770 crypt_safe_free(password);
771 return (r < 0) ? r : 0;
774 /* OPTIONS: name, device, key_size, key_file, timeout, tries, flags, icb */
775 int crypt_luksOpen(struct crypt_options *options)
777 struct crypt_device *cd = NULL;
784 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
788 if (options->flags & CRYPT_FLAG_READONLY)
789 flags |= CRYPT_ACTIVATE_READONLY;
791 if (options->flags & CRYPT_FLAG_NON_EXCLUSIVE_ACCESS)
792 flags |= CRYPT_ACTIVATE_NO_UUID;
794 if (options->key_file)
795 r = crypt_activate_by_keyfile(cd, options->name,
796 CRYPT_ANY_SLOT, options->key_file, options->key_size,
799 r = crypt_activate_by_passphrase(cd, options->name,
800 CRYPT_ANY_SLOT, options->passphrase,
801 options->passphrase ? strlen(options->passphrase) : 0,
805 return (r < 0) ? r : 0;
808 /* OPTIONS: device, keys_slot, key_file, timeout, flags, icb */
809 int crypt_luksKillSlot(struct crypt_options *options)
811 struct crypt_device *cd = NULL;
814 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
818 r = luks_remove_helper(cd, options->key_slot, options->key_file, NULL,
819 options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
822 return (r < 0) ? r : 0;
825 /* OPTIONS: device, new_key_file, key_file, timeout, flags, icb */
826 int crypt_luksRemoveKey(struct crypt_options *options)
828 struct crypt_device *cd = NULL;
831 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
835 r = luks_remove_helper(cd, CRYPT_ANY_SLOT, options->key_file, options->new_key_file,
836 options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
839 return (r < 0) ? r : 0;
843 /* OPTIONS: device, new_key_file, key_file, key_slot, flags,
844 iteration_time, timeout, icb */
845 int crypt_luksAddKey(struct crypt_options *options)
847 struct crypt_device *cd = NULL;
850 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
854 if (options->key_file || options->new_key_file)
855 r = crypt_keyslot_add_by_keyfile(cd, options->key_slot,
856 options->key_file, 0,
857 options->new_key_file, 0);
859 r = crypt_keyslot_add_by_passphrase(cd, options->key_slot,
863 return (r < 0) ? r : 0;
866 /* OPTIONS: device, icb */
867 int crypt_luksUUID(struct crypt_options *options)
869 struct crypt_device *cd = NULL;
873 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
877 uuid = (char *)crypt_get_uuid(cd);
878 log_std(cd, uuid ?: "");
884 /* OPTIONS: device, icb */
885 int crypt_isLuks(struct crypt_options *options)
887 struct crypt_device *cd = NULL;
890 log_dbg("Check device %s for LUKS header.", options->device);
896 r = crypt_init(&cd, options->device);
900 /* Do print fail here, no need to crypt_load() */
901 r = LUKS_read_phdr(cd->device, &cd->hdr, 0, cd) ? -EINVAL : 0;
907 /* OPTIONS: device, icb */
908 int crypt_luksDump(struct crypt_options *options)
910 struct crypt_device *cd = NULL;
913 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
923 void crypt_get_error(char *buf, size_t size)
925 const char *error = get_error();
927 if (!buf || size < 1)
930 strncpy(buf, error, size - 1);
931 buf[size - 1] = '\0';
937 void crypt_put_options(struct crypt_options *options)
939 if (options->flags & CRYPT_FLAG_FREE_DEVICE) {
940 free((char *)options->device);
941 options->device = NULL;
942 options->flags &= ~CRYPT_FLAG_FREE_DEVICE;
944 if (options->flags & CRYPT_FLAG_FREE_CIPHER) {
945 free((char *)options->cipher);
946 options->cipher = NULL;
947 options->flags &= ~CRYPT_FLAG_FREE_CIPHER;
951 const char *crypt_get_dir(void)
956 /////////////////////////////////
961 int crypt_init(struct crypt_device **cd, const char *device)
963 struct crypt_device *h = NULL;
968 log_dbg("Allocating crypt device %s context.", device);
970 if (device && !device_ready(NULL, device, O_RDONLY))
973 if (!(h = malloc(sizeof(struct crypt_device))))
976 memset(h, 0, sizeof(*h));
979 h->device = strdup(device);
987 if (dm_init(h, 1) < 0) {
992 h->iteration_time = 1000;
993 h->password_verify = 0;
995 h->rng_type = crypt_random_default_key_rng();
1000 int crypt_init_by_name(struct crypt_device **cd, const char *name)
1002 crypt_status_info ci;
1003 struct crypt_active_device cad;
1004 char *device = NULL, *cipher_full = NULL, *device_uuid = NULL;
1005 char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
1007 int key_size = 0, key_nums, r;
1010 log_dbg("Allocating crypt device context by device %s.", name);
1012 ci = crypt_status(NULL, name);
1013 if (ci == CRYPT_INVALID)
1016 if (ci < CRYPT_ACTIVE) {
1017 log_err(NULL, _("Device %s is not active.\n"), name);
1021 r = dm_query_device(name, &device, &cad.size, &cad.iv_offset, &cad.offset,
1022 &cipher_full, &key_size, &key, NULL, NULL,
1028 r = crypt_init(cd, device);
1030 /* Underlying device disappeared but mapping still active */
1031 if (!device || r == -ENOTBLK)
1032 log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
1035 /* Underlying device is not readable but crypt mapping exists */
1036 if (r == -ENOTBLK) {
1039 r = crypt_init(cd, NULL);
1045 /* Try to initialise basic parameters from active device */
1047 if (!strncmp(CRYPT_PLAIN, device_uuid, sizeof(CRYPT_PLAIN)-1)) {
1048 (*cd)->type = strdup(CRYPT_PLAIN);
1049 (*cd)->plain_uuid = strdup(device_uuid);
1050 (*cd)->plain_hdr.hash = NULL; /* no way to get this */
1051 (*cd)->plain_hdr.offset = cad.offset;
1052 (*cd)->plain_hdr.skip = cad.iv_offset;
1053 (*cd)->volume_key = crypt_alloc_volume_key(key_size, key);
1054 if (!(*cd)->volume_key) {
1059 r = crypt_parse_name_and_mode(cipher_full, cipher, NULL, cipher_mode);
1061 (*cd)->plain_cipher = strdup(cipher);
1062 (*cd)->plain_cipher_mode = strdup(cipher_mode);
1064 } else if (!strncmp(CRYPT_LOOPAES, device_uuid, sizeof(CRYPT_LOOPAES)-1)) {
1065 (*cd)->type = strdup(CRYPT_LOOPAES);
1066 (*cd)->loopaes_uuid = strdup(device_uuid);
1067 (*cd)->loopaes_hdr.offset = cad.offset;
1069 r = crypt_parse_name_and_mode(cipher_full, cipher,
1070 &key_nums, cipher_mode);
1072 (*cd)->loopaes_cipher = strdup(cipher);
1073 (*cd)->loopaes_cipher_mode = strdup(cipher_mode);
1074 /* version 3 uses last key for IV */
1075 if (key_size % key_nums)
1077 (*cd)->loopaes_key_size = key_size / key_nums;
1079 } else if (!strncmp(CRYPT_LUKS1, device_uuid, sizeof(CRYPT_LUKS1)-1)) {
1081 if (crypt_load(*cd, CRYPT_LUKS1, NULL) < 0 ||
1082 crypt_volume_key_verify(*cd, key, key_size) < 0) {
1083 log_dbg("LUKS device header does not match active device.");
1087 (*cd)->volume_key = crypt_alloc_volume_key(key_size, key);
1088 if (!(*cd)->volume_key) {
1095 log_dbg("Active device has no UUID set, some parameters are not set.");
1102 crypt_safe_free(key);
1109 static int _crypt_format_plain(struct crypt_device *cd,
1111 const char *cipher_mode,
1113 size_t volume_key_size,
1114 struct crypt_params_plain *params)
1116 if (!cipher || !cipher_mode) {
1117 log_err(cd, _("Invalid plain crypt parameters.\n"));
1121 if (volume_key_size > 1024) {
1122 log_err(cd, _("Invalid key size.\n"));
1126 cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
1127 if (!cd->volume_key)
1130 cd->plain_cipher = strdup(cipher);
1131 cd->plain_cipher_mode = strdup(cipher_mode);
1134 cd->plain_uuid = strdup(uuid);
1136 if (params && params->hash)
1137 cd->plain_hdr.hash = strdup(params->hash);
1139 cd->plain_hdr.offset = params ? params->offset : 0;
1140 cd->plain_hdr.skip = params ? params->skip : 0;
1142 if (!cd->plain_cipher || !cd->plain_cipher_mode)
1148 static int _crypt_format_luks1(struct crypt_device *cd,
1150 const char *cipher_mode,
1152 const char *volume_key,
1153 size_t volume_key_size,
1154 struct crypt_params_luks1 *params)
1157 unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
1158 unsigned long alignment_offset = 0;
1161 log_err(cd, _("Can't format LUKS without device.\n"));
1166 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
1169 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
1174 if (params && params->data_alignment)
1175 required_alignment = params->data_alignment * SECTOR_SIZE;
1177 get_topology_alignment(cd->device, &required_alignment,
1178 &alignment_offset, DEFAULT_DISK_ALIGNMENT);
1180 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
1181 (params && params->hash) ? params->hash : "sha1",
1183 required_alignment / SECTOR_SIZE,
1184 alignment_offset / SECTOR_SIZE,
1185 cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1189 /* Wipe first 8 sectors - fs magic numbers etc. */
1190 r = wipe_device_header(cd->device, 8);
1193 log_err(cd, _("Cannot format device %s which is still in use.\n"),
1196 log_err(cd, _("Cannot wipe header on device %s.\n"),
1202 r = LUKS_write_phdr(cd->device, &cd->hdr, cd);
1207 static int _crypt_format_loopaes(struct crypt_device *cd,
1210 size_t volume_key_size,
1211 struct crypt_params_loopaes *params)
1214 log_err(cd, _("Can't format LOOPAES without device.\n"));
1218 if (volume_key_size > 1024) {
1219 log_err(cd, _("Invalid key size.\n"));
1223 cd->loopaes_key_size = volume_key_size;
1225 cd->loopaes_cipher = strdup(cipher ?: DEFAULT_LOOPAES_CIPHER);
1228 cd->loopaes_uuid = strdup(uuid);
1230 if (params && params->hash)
1231 cd->loopaes_hdr.hash = strdup(params->hash);
1233 cd->loopaes_hdr.offset = params ? params->offset : 0;
1238 int crypt_format(struct crypt_device *cd,
1241 const char *cipher_mode,
1243 const char *volume_key,
1244 size_t volume_key_size,
1252 log_dbg("Formatting device %s as type %s.", cd->device ?: "(none)", type);
1254 r = init_crypto(cd);
1259 r = _crypt_format_plain(cd, cipher, cipher_mode,
1260 uuid, volume_key_size, params);
1261 else if (isLUKS(type))
1262 r = _crypt_format_luks1(cd, cipher, cipher_mode,
1263 uuid, volume_key, volume_key_size, params);
1264 else if (isLOOPAES(type))
1265 r = _crypt_format_loopaes(cd, cipher, uuid, volume_key_size, params);
1267 /* FIXME: allow plugins here? */
1268 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
1272 if (!r && !(cd->type = strdup(type)))
1276 crypt_free_volume_key(cd->volume_key);
1277 cd->volume_key = NULL;
1283 int crypt_load(struct crypt_device *cd,
1284 const char *requested_type,
1287 struct luks_phdr hdr;
1290 log_dbg("Trying to load %s crypt type from device %s.",
1291 requested_type ?: "any", cd->device ?: "(none)");
1296 if (requested_type && !isLUKS(requested_type))
1299 r = init_crypto(cd);
1303 r = LUKS_read_phdr(cd->device, &hdr, 1, cd);
1306 memcpy(&cd->hdr, &hdr, sizeof(hdr));
1307 cd->type = strdup(CRYPT_LUKS1);
1315 int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
1317 char *device = NULL, *cipher = NULL, *uuid = NULL, *key = NULL;
1318 uint64_t size, skip, offset;
1319 int key_size, read_only, r;
1321 /* Device context type must be initialised */
1322 if (!cd->type || !crypt_get_uuid(cd))
1325 r = dm_query_device(name, &device, &size, &skip, &offset,
1326 &cipher, &key_size, &key, &read_only, NULL, &uuid);
1328 log_err(NULL, _("Device %s is not active.\n"), name);
1337 r = device_check_and_adjust(cd, device, 0, &new_size, &offset, &read_only);
1341 if (new_size == size) {
1342 log_dbg("Device has already requested size %" PRIu64
1348 log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
1350 r = dm_create_device(name, device, cipher, cd->type,
1351 crypt_get_uuid(cd), new_size, skip, offset,
1352 key_size, key, read_only, 1);
1354 crypt_safe_free(key);
1362 int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
1364 if (!isLUKS(cd->type)) {
1365 log_err(cd, _("This operation is not supported for this device type.\n"));
1369 if (uuid && !strncmp(uuid, cd->hdr.uuid, sizeof(cd->hdr.uuid))) {
1370 log_dbg("UUID is the same as requested (%s) for device %s.",
1376 log_dbg("Requested new UUID change to %s for %s.", uuid, cd->device);
1378 log_dbg("Requested new UUID refresh for %s.", cd->device);
1380 if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
1383 return LUKS_hdr_uuid_set(cd->device, &cd->hdr, uuid, cd);
1386 int crypt_header_backup(struct crypt_device *cd,
1387 const char *requested_type,
1388 const char *backup_file)
1392 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1395 r = init_crypto(cd);
1399 log_dbg("Requested header backup of device %s (%s) to "
1400 "file %s.", cd->device, requested_type, backup_file);
1402 return LUKS_hdr_backup(backup_file, cd->device, &cd->hdr, cd);
1405 int crypt_header_restore(struct crypt_device *cd,
1406 const char *requested_type,
1407 const char *backup_file)
1411 if (requested_type && !isLUKS(requested_type))
1414 /* Some hash functions need initialized gcrypt library */
1415 r = init_crypto(cd);
1419 log_dbg("Requested header restore to device %s (%s) from "
1420 "file %s.", cd->device, requested_type, backup_file);
1422 return LUKS_hdr_restore(backup_file, cd->device, &cd->hdr, cd);
1425 void crypt_free(struct crypt_device *cd)
1428 log_dbg("Releasing crypt device %s context.", cd->device);
1431 crypt_free_volume_key(cd->volume_key);
1436 /* used in plain device only */
1437 free((char*)cd->plain_hdr.hash);
1438 free(cd->plain_cipher);
1439 free(cd->plain_cipher_mode);
1440 free(cd->plain_uuid);
1446 int crypt_suspend(struct crypt_device *cd,
1449 crypt_status_info ci;
1450 int r, suspended = 0;
1452 log_dbg("Suspending volume %s.", name);
1454 ci = crypt_status(NULL, name);
1455 if (ci < CRYPT_ACTIVE) {
1456 log_err(cd, _("Volume %s is not active.\n"), name);
1460 if (!cd && dm_init(NULL, 1) < 0)
1463 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1464 NULL, NULL, NULL, NULL, &suspended, NULL);
1469 log_err(cd, _("Volume %s is already suspended.\n"), name);
1474 r = dm_suspend_and_wipe_key(name);
1476 log_err(cd, "Suspend is not supported for device %s.\n", name);
1478 log_err(cd, "Error during suspending device %s.\n", name);
1485 int crypt_resume_by_passphrase(struct crypt_device *cd,
1488 const char *passphrase,
1489 size_t passphrase_size)
1491 struct volume_key *vk = NULL;
1492 int r, suspended = 0;
1494 log_dbg("Resuming volume %s.", name);
1496 if (!isLUKS(cd->type)) {
1497 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1502 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1503 NULL, NULL, NULL, NULL, &suspended, NULL);
1508 log_err(cd, _("Volume %s is not suspended.\n"), name);
1513 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1514 passphrase_size, &cd->hdr, &vk, cd);
1516 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1520 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1522 log_err(cd, "Resume is not supported for device %s.\n", name);
1524 log_err(cd, "Error during resuming device %s.\n", name);
1528 crypt_free_volume_key(vk);
1529 return r < 0 ? r : keyslot;
1532 int crypt_resume_by_keyfile(struct crypt_device *cd,
1535 const char *keyfile,
1536 size_t keyfile_size)
1538 struct volume_key *vk = NULL;
1539 char *passphrase_read = NULL;
1540 unsigned int passphrase_size_read;
1541 int r, suspended = 0;
1543 log_dbg("Resuming volume %s.", name);
1545 if (!isLUKS(cd->type)) {
1546 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1551 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1552 NULL, NULL, NULL, NULL, &suspended, NULL);
1557 log_err(cd, _("Volume %s is not suspended.\n"), name);
1564 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1565 &passphrase_size_read, keyfile, keyfile_size);
1569 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1570 passphrase_size_read, &cd->hdr, &vk, cd);
1575 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1577 log_err(cd, "Error during resuming device %s.\n", name);
1579 crypt_safe_free(passphrase_read);
1580 crypt_free_volume_key(vk);
1581 return r < 0 ? r : keyslot;
1584 // slot manipulation
1585 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1586 int keyslot, // -1 any
1587 const char *passphrase, // NULL -> terminal
1588 size_t passphrase_size,
1589 const char *new_passphrase, // NULL -> terminal
1590 size_t new_passphrase_size)
1592 struct volume_key *vk = NULL;
1593 char *password = NULL, *new_password = NULL;
1594 unsigned int passwordLen, new_passwordLen;
1597 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1598 "new passphrase %sprovided.",
1599 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1601 if (!isLUKS(cd->type)) {
1602 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1606 r = keyslot_verify_or_find_empty(cd, &keyslot);
1610 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1611 /* No slots used, try to use pre-generated key in header */
1612 if (cd->volume_key) {
1613 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1614 r = vk ? 0 : -ENOMEM;
1616 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1619 } else if (passphrase) {
1620 /* Passphrase provided, use it to unlock existing keyslot */
1621 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, passphrase,
1622 passphrase_size, &cd->hdr, &vk, cd);
1624 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1625 r = key_from_terminal(cd, _("Enter any passphrase: "),
1626 &password, &passwordLen, 0);
1630 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
1631 passwordLen, &cd->hdr, &vk, cd);
1632 crypt_safe_free(password);
1638 if (new_passphrase) {
1639 new_password = (char *)new_passphrase;
1640 new_passwordLen = new_passphrase_size;
1642 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1643 &new_password, &new_passwordLen, 1);
1648 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1649 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1654 if (!new_passphrase)
1655 crypt_safe_free(new_password);
1656 crypt_free_volume_key(vk);
1657 return r ?: keyslot;
1660 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1662 const char *keyfile,
1663 size_t keyfile_size,
1664 const char *new_keyfile,
1665 size_t new_keyfile_size)
1667 struct volume_key *vk = NULL;
1668 char *password = NULL; unsigned int passwordLen;
1669 char *new_password = NULL; unsigned int new_passwordLen;
1672 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1673 keyfile ?: "[none]", new_keyfile ?: "[none]");
1675 if (!isLUKS(cd->type)) {
1676 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1680 r = keyslot_verify_or_find_empty(cd, &keyslot);
1684 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1685 /* No slots used, try to use pre-generated key in header */
1686 if (cd->volume_key) {
1687 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1688 r = vk ? 0 : -ENOMEM;
1690 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1694 /* Read password from file of (if NULL) from terminal */
1696 r = key_from_file(cd, _("Enter any passphrase: "),
1697 &password, &passwordLen,
1698 keyfile, keyfile_size);
1700 r = key_from_terminal(cd, _("Enter any passphrase: "),
1701 &password, &passwordLen, 0);
1705 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password, passwordLen,
1713 r = key_from_file(cd, _("Enter new passphrase for key slot: "),
1714 &new_password, &new_passwordLen, new_keyfile,
1717 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1718 &new_password, &new_passwordLen, 1);
1722 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1723 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1725 crypt_safe_free(password);
1726 crypt_safe_free(new_password);
1727 crypt_free_volume_key(vk);
1728 return r < 0 ? r : keyslot;
1731 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1733 const char *volume_key,
1734 size_t volume_key_size,
1735 const char *passphrase,
1736 size_t passphrase_size)
1738 struct volume_key *vk = NULL;
1740 char *new_password = NULL; unsigned int new_passwordLen;
1742 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1744 if (!isLUKS(cd->type)) {
1745 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1750 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1751 else if (cd->volume_key)
1752 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1757 r = LUKS_verify_volume_key(&cd->hdr, vk);
1759 log_err(cd, _("Volume key does not match the volume.\n"));
1763 r = keyslot_verify_or_find_empty(cd, &keyslot);
1768 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1769 &new_password, &new_passwordLen, 1);
1772 passphrase = new_password;
1773 passphrase_size = new_passwordLen;
1776 r = LUKS_set_key(cd->device, keyslot, passphrase, passphrase_size,
1777 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1779 crypt_safe_free(new_password);
1780 crypt_free_volume_key(vk);
1781 return (r < 0) ? r : keyslot;
1784 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1786 crypt_keyslot_info ki;
1788 log_dbg("Destroying keyslot %d.", keyslot);
1790 if (!isLUKS(cd->type)) {
1791 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1795 ki = crypt_keyslot_status(cd, keyslot);
1796 if (ki == CRYPT_SLOT_INVALID) {
1797 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1801 if (ki == CRYPT_SLOT_INACTIVE) {
1802 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1806 return LUKS_del_key(cd->device, keyslot, &cd->hdr, cd);
1809 // activation/deactivation of device mapping
1810 int crypt_activate_by_passphrase(struct crypt_device *cd,
1813 const char *passphrase,
1814 size_t passphrase_size,
1817 crypt_status_info ci;
1818 struct volume_key *vk = NULL;
1819 char *read_passphrase = NULL;
1820 unsigned int passphraseLen = 0;
1823 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1824 name ? "Activating" : "Checking", name ?: "",
1825 keyslot, passphrase ? "" : "[none] ");
1828 ci = crypt_status(NULL, name);
1829 if (ci == CRYPT_INVALID)
1831 else if (ci >= CRYPT_ACTIVE) {
1832 log_err(cd, _("Device %s already exists.\n"), name);
1837 /* plain, use hashed passphrase */
1838 if (isPLAIN(cd->type)) {
1840 r = key_from_terminal(cd, NULL, &read_passphrase,
1844 passphrase = read_passphrase;
1845 passphrase_size = passphraseLen;
1847 r = create_device_helper(cd, name, cd->plain_hdr.hash,
1848 cd->plain_cipher, cd->plain_cipher_mode,
1849 NULL, passphrase, passphrase_size,
1850 cd->volume_key->keylength, 0,
1851 cd->plain_hdr.skip, cd->plain_hdr.offset,
1853 flags & CRYPT_ACTIVATE_READONLY, 0, 0);
1855 } else if (isLUKS(cd->type)) {
1856 /* provided passphrase, do not retry */
1858 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1859 passphrase_size, &cd->hdr, &vk, cd);
1861 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1866 r = open_from_hdr_and_vk(cd, vk, name, flags);
1871 crypt_safe_free(read_passphrase);
1872 crypt_free_volume_key(vk);
1874 return r < 0 ? r : keyslot;
1877 int crypt_activate_by_keyfile(struct crypt_device *cd,
1880 const char *keyfile,
1881 size_t keyfile_size,
1884 crypt_status_info ci;
1885 struct volume_key *vk = NULL;
1886 char *passphrase_read = NULL;
1887 unsigned int passphrase_size_read, key_count = 0;
1890 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1891 name ?: "", keyslot, keyfile ?: "[none]");
1894 ci = crypt_status(NULL, name);
1895 if (ci == CRYPT_INVALID)
1897 else if (ci >= CRYPT_ACTIVE) {
1898 log_err(cd, _("Device %s already exists.\n"), name);
1906 if (isPLAIN(cd->type)) {
1907 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1908 &passphrase_size_read, keyfile, keyfile_size);
1911 r = create_device_helper(cd, name, cd->plain_hdr.hash,
1912 cd->plain_cipher, cd->plain_cipher_mode,
1913 NULL, passphrase_read, passphrase_size_read,
1914 cd->volume_key->keylength, 0,
1915 cd->plain_hdr.skip, cd->plain_hdr.offset,
1917 flags & CRYPT_ACTIVATE_READONLY, 0, 0);
1918 } else if (isLUKS(cd->type)) {
1919 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1920 &passphrase_size_read, keyfile, keyfile_size);
1923 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1924 passphrase_size_read, &cd->hdr, &vk, cd);
1930 r = open_from_hdr_and_vk(cd, vk, name, flags);
1935 } else if (isLOOPAES(cd->type)) {
1936 r = key_from_file(cd, NULL, &passphrase_read, &passphrase_size_read,
1937 keyfile, LOOPAES_KEYFILE_MAXSIZE);
1940 r = LOOPAES_parse_keyfile(cd, &vk, &key_count,
1941 passphrase_read, passphrase_size_read);
1945 r = LOOPAES_activate(cd, name, cd->loopaes_cipher,
1946 key_count, vk, flags);
1951 crypt_safe_free(passphrase_read);
1952 crypt_free_volume_key(vk);
1957 int crypt_activate_by_volume_key(struct crypt_device *cd,
1959 const char *volume_key,
1960 size_t volume_key_size,
1963 crypt_status_info ci;
1964 struct volume_key *vk;
1967 log_dbg("Activating volume %s by volume key.", name);
1969 /* use key directly, no hash */
1970 if (isPLAIN(cd->type)) {
1971 if (!volume_key || !volume_key_size || !cd->volume_key ||
1972 volume_key_size != cd->volume_key->keylength) {
1973 log_err(cd, _("Incorrect volume key specified for plain device.\n"));
1977 return create_device_helper(cd, name, NULL,
1978 cd->plain_cipher, cd->plain_cipher_mode, NULL, volume_key, volume_key_size,
1979 cd->volume_key->keylength, 0, cd->plain_hdr.skip,
1980 cd->plain_hdr.offset, cd->plain_uuid, flags & CRYPT_ACTIVATE_READONLY, 0, 0);
1983 if (!isLUKS(cd->type)) {
1984 log_err(cd, _("Device type is not properly initialised.\n"));
1989 ci = crypt_status(NULL, name);
1990 if (ci == CRYPT_INVALID)
1992 else if (ci >= CRYPT_ACTIVE) {
1993 log_err(cd, _("Device %s already exists.\n"), name);
1998 /* If key is not provided, try to use internal key */
2000 if (!cd->volume_key) {
2001 log_err(cd, _("Volume key does not match the volume.\n"));
2004 volume_key_size = cd->volume_key->keylength;
2005 volume_key = cd->volume_key->key;
2008 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2011 r = LUKS_verify_volume_key(&cd->hdr, vk);
2014 log_err(cd, _("Volume key does not match the volume.\n"));
2017 r = open_from_hdr_and_vk(cd, vk, name, flags);
2019 crypt_free_volume_key(vk);
2024 int crypt_deactivate(struct crypt_device *cd, const char *name)
2031 log_dbg("Deactivating volume %s.", name);
2033 if (!cd && dm_init(NULL, 1) < 0)
2036 switch (crypt_status(cd, name)) {
2038 r = dm_remove_device(name, 0, 0);
2041 log_err(cd, _("Device %s is busy.\n"), name);
2044 case CRYPT_INACTIVE:
2045 log_err(cd, _("Device %s is not active.\n"), name);
2049 log_err(cd, _("Invalid device %s.\n"), name);
2059 int crypt_volume_key_get(struct crypt_device *cd,
2062 size_t *volume_key_size,
2063 const char *passphrase,
2064 size_t passphrase_size)
2066 struct volume_key *vk;
2067 char *processed_key = NULL;
2070 key_len = crypt_get_volume_key_size(cd);
2071 if (key_len > *volume_key_size) {
2072 log_err(cd, _("Volume key buffer too small.\n"));
2076 if (isPLAIN(cd->type) && cd->plain_hdr.hash) {
2077 processed_key = process_key(cd, cd->plain_hdr.hash, NULL, key_len,
2078 passphrase, passphrase_size);
2079 if (!processed_key) {
2080 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
2083 memcpy(volume_key, processed_key, key_len);
2084 *volume_key_size = key_len;
2085 crypt_safe_free(processed_key);
2089 if (isLUKS(cd->type)) {
2090 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
2091 passphrase_size, &cd->hdr, &vk, cd);
2094 memcpy(volume_key, vk->key, vk->keylength);
2095 *volume_key_size = vk->keylength;
2098 crypt_free_volume_key(vk);
2102 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
2106 int crypt_volume_key_verify(struct crypt_device *cd,
2107 const char *volume_key,
2108 size_t volume_key_size)
2110 struct volume_key *vk;
2113 if (!isLUKS(cd->type)) {
2114 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2118 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2122 r = LUKS_verify_volume_key(&cd->hdr, vk);
2125 log_err(cd, _("Volume key does not match the volume.\n"));
2127 crypt_free_volume_key(vk);
2132 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
2134 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
2135 cd->timeout = timeout_sec;
2138 void crypt_set_password_retry(struct crypt_device *cd, int tries)
2140 log_dbg("Password retry count set to %d.", tries);
2144 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2146 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
2147 cd->iteration_time = iteration_time_ms;
2150 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
2152 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
2153 cd->password_verify = password_verify ? 1 : 0;
2156 void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
2159 case CRYPT_RNG_URANDOM:
2160 case CRYPT_RNG_RANDOM:
2161 log_dbg("RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
2162 cd->rng_type = rng_type;
2166 int crypt_get_rng_type(struct crypt_device *cd)
2171 return cd->rng_type;
2174 int crypt_memory_lock(struct crypt_device *cd, int lock)
2176 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
2180 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
2184 if (!cd && dm_init(NULL, 1) < 0)
2185 return CRYPT_INVALID;
2187 r = dm_status_device(name);
2192 if (r < 0 && r != -ENODEV)
2193 return CRYPT_INVALID;
2196 return CRYPT_ACTIVE;
2201 return CRYPT_INACTIVE;
2204 static void hexprintICB(struct crypt_device *cd, char *d, int n)
2207 for(i = 0; i < n; i++)
2208 log_std(cd, "%02hhx ", (char)d[i]);
2211 int crypt_dump(struct crypt_device *cd)
2214 if (!isLUKS(cd->type)) { //FIXME
2215 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2219 log_std(cd, "LUKS header information for %s\n\n", cd->device);
2220 log_std(cd, "Version: \t%d\n", cd->hdr.version);
2221 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
2222 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
2223 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec);
2224 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
2225 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
2226 log_std(cd, "MK digest: \t");
2227 hexprintICB(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE);
2229 log_std(cd, "MK salt: \t");
2230 hexprintICB(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2);
2231 log_std(cd, "\n \t");
2232 hexprintICB(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
2234 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
2235 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
2236 for(i = 0; i < LUKS_NUMKEYS; i++) {
2237 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
2238 log_std(cd, "Key Slot %d: ENABLED\n",i);
2239 log_std(cd, "\tIterations: \t%d\n",
2240 cd->hdr.keyblock[i].passwordIterations);
2241 log_std(cd, "\tSalt: \t");
2242 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt,
2244 log_std(cd, "\n\t \t");
2245 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt +
2246 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
2249 log_std(cd, "\tKey material offset:\t%d\n",
2250 cd->hdr.keyblock[i].keyMaterialOffset);
2251 log_std(cd, "\tAF stripes: \t%d\n",
2252 cd->hdr.keyblock[i].stripes);
2255 log_std(cd, "Key Slot %d: DISABLED\n", i);
2261 const char *crypt_get_cipher(struct crypt_device *cd)
2263 if (isPLAIN(cd->type))
2264 return cd->plain_cipher;
2266 if (isLUKS(cd->type))
2267 return cd->hdr.cipherName;
2269 if (isLOOPAES(cd->type))
2270 return cd->loopaes_cipher;
2275 const char *crypt_get_cipher_mode(struct crypt_device *cd)
2277 if (isPLAIN(cd->type))
2278 return cd->plain_cipher_mode;
2280 if (isLUKS(cd->type))
2281 return cd->hdr.cipherMode;
2283 if (isLOOPAES(cd->type))
2284 return cd->loopaes_cipher_mode;
2289 const char *crypt_get_uuid(struct crypt_device *cd)
2291 if (isLUKS(cd->type))
2292 return cd->hdr.uuid;
2294 if (isPLAIN(cd->type))
2295 return cd->plain_uuid;
2297 if (isLOOPAES(cd->type))
2298 return cd->loopaes_uuid;
2303 const char *crypt_get_device_name(struct crypt_device *cd)
2308 int crypt_get_volume_key_size(struct crypt_device *cd)
2310 if (isPLAIN(cd->type) && cd->volume_key)
2311 return cd->volume_key->keylength;
2313 if (isLUKS(cd->type))
2314 return cd->hdr.keyBytes;
2316 if (isLOOPAES(cd->type))
2317 return cd->loopaes_key_size;
2322 uint64_t crypt_get_data_offset(struct crypt_device *cd)
2324 if (isPLAIN(cd->type))
2325 return cd->plain_hdr.offset;
2327 if (isLUKS(cd->type))
2328 return cd->hdr.payloadOffset;
2330 if (isLOOPAES(cd->type))
2331 return cd->loopaes_hdr.offset;
2336 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
2338 if (!isLUKS(cd->type)) {
2339 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2340 return CRYPT_SLOT_INVALID;
2343 return LUKS_keyslot_info(&cd->hdr, keyslot);
2346 int crypt_keyslot_max(const char *type)
2348 if (type && isLUKS(type))
2349 return LUKS_NUMKEYS;
2354 const char *crypt_get_type(struct crypt_device *cd)
2359 int crypt_get_active_device(struct crypt_device *cd,
2361 struct crypt_active_device *cad)
2365 r = dm_query_device(name, NULL, &cad->size, &cad->iv_offset, &cad->offset,
2366 NULL, NULL, NULL, &readonly, NULL, NULL);
2370 cad->flags = readonly ? CRYPT_ACTIVATE_READONLY : 0;