2 * libcryptsetup - cryptsetup library
4 * Copyright (C) 2004, Christophe Saout <christophe@saout.de>
5 * Copyright (C) 2004-2007, Clemens Fruhwirth <clemens@endorphin.org>
6 * Copyright (C) 2009-2011, Red Hat, Inc. All rights reserved.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "libcryptsetup.h"
33 #include "crypto_backend.h"
41 struct volume_key *volume_key;
43 uint64_t iteration_time;
48 /* used in CRYPT_LUKS1 */
50 uint64_t PBKDF2_per_sec;
52 /* used in CRYPT_PLAIN */
53 struct crypt_params_plain plain_hdr;
55 char *plain_cipher_mode;
58 /* used in CRYPT_LOOPAES */
59 struct crypt_params_loopaes loopaes_hdr;
61 char *loopaes_cipher_mode;
63 unsigned int loopaes_key_size;
65 /* callbacks definitions */
66 void (*log)(int level, const char *msg, void *usrptr);
68 int (*confirm)(const char *msg, void *usrptr);
70 int (*password)(const char *msg, char *buf, size_t length, void *usrptr);
71 void *password_usrptr;
75 static void (*_default_log)(int level, const char *msg, void *usrptr) = NULL;
76 static int _debug_level = 0;
78 void crypt_set_debug_level(int level)
83 int crypt_get_debug_level()
88 void crypt_log(struct crypt_device *cd, int level, const char *msg)
91 cd->log(level, msg, cd->log_usrptr);
92 else if (_default_log)
93 _default_log(level, msg, NULL);
96 void logger(struct crypt_device *cd, int level, const char *file,
97 int line, const char *format, ...)
102 va_start(argp, format);
104 if (vasprintf(&target, format, argp) > 0) {
106 crypt_log(cd, level, target);
108 } else if (_debug_level)
109 printf("# %s:%d %s\n", file ?: "?", line, target);
111 } else if (_debug_level)
112 printf("# %s\n", target);
120 static int init_crypto(struct crypt_device *ctx)
124 r = crypt_random_init(ctx);
126 log_err(ctx, _("Cannot initialize crypto RNG backend.\n"));
130 r = crypt_backend_init(ctx);
132 log_err(ctx, _("Cannot initialize crypto backend.\n"));
138 * Password processing behaviour matrix of process_key
140 * from binary file: check if there is sufficently large key material
141 * interactive & from fd: hash if requested, otherwise crop or pad with '0'
143 static char *process_key(struct crypt_device *cd, const char *hash_name,
144 const char *key_file, size_t key_size,
145 const char *pass, size_t passLen)
153 key = crypt_safe_alloc(key_size);
156 memset(key, 0, key_size);
158 /* key is coming from binary file */
159 if (key_file && strcmp(key_file, "-")) {
160 if(passLen < key_size) {
161 log_err(cd, _("Cannot not read %d bytes from key file %s.\n"),
163 crypt_safe_free(key);
166 memcpy(key, pass, key_size);
170 /* key is coming from tty, fd or binary stdin */
172 r = crypt_plain_hash(cd, hash_name, key, key_size, pass, passLen);
175 log_err(cd, _("Hash algorithm %s not supported.\n"),
178 log_err(cd, _("Key processing error (using hash %s).\n"),
180 crypt_safe_free(key);
183 } else if (passLen > key_size) {
184 memcpy(key, pass, key_size);
186 memcpy(key, pass, passLen);
192 static int isPLAIN(const char *type)
194 return (type && !strcmp(CRYPT_PLAIN, type));
197 static int isLUKS(const char *type)
199 return (type && !strcmp(CRYPT_LUKS1, type));
202 static int isLOOPAES(const char *type)
204 return (type && !strcmp(CRYPT_LOOPAES, type));
207 /* keyslot helpers */
208 static int keyslot_verify_or_find_empty(struct crypt_device *cd, int *keyslot)
210 if (*keyslot == CRYPT_ANY_SLOT) {
211 *keyslot = LUKS_keyslot_find_empty(&cd->hdr);
213 log_err(cd, _("All key slots full.\n"));
218 switch (LUKS_keyslot_info(&cd->hdr, *keyslot)) {
219 case CRYPT_SLOT_INVALID:
220 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
221 *keyslot, LUKS_NUMKEYS - 1);
223 case CRYPT_SLOT_INACTIVE:
226 log_err(cd, _("Key slot %d is full, please select another one.\n"),
234 static int verify_other_keyslot(struct crypt_device *cd,
235 const char *key_file,
238 struct volume_key *vk = NULL;
239 crypt_keyslot_info ki;
241 char *password = NULL;
244 r = crypt_get_key(_("Enter any remaining LUKS passphrase: "),
245 &password, &passwordLen, 0, key_file, cd->timeout,
246 cd->password_verify, cd);
250 ki = crypt_keyslot_status(cd, keyIndex);
251 if (ki == CRYPT_SLOT_ACTIVE) /* Not last slot */
252 LUKS_keyslot_set(&cd->hdr, keyIndex, 0);
254 openedIndex = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT,
255 password, passwordLen,
258 if (ki == CRYPT_SLOT_ACTIVE)
259 LUKS_keyslot_set(&cd->hdr, keyIndex, 1);
264 log_verbose(cd, _("Key slot %d verified.\n"), openedIndex);
266 crypt_free_volume_key(vk);
267 crypt_safe_free(password);
271 static int find_keyslot_by_passphrase(struct crypt_device *cd,
272 const char *key_file,
275 struct volume_key *vk = NULL;
276 char *password = NULL;
280 r = crypt_get_key(message,&password,&passwordLen, 0, key_file,
281 cd->timeout, cd->password_verify, cd);
285 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
286 passwordLen, &cd->hdr, &vk, cd);
288 crypt_free_volume_key(vk);
289 crypt_safe_free(password);
293 static int luks_remove_helper(struct crypt_device *cd,
295 const char *other_key_file,
296 const char *key_file,
299 crypt_keyslot_info ki;
302 if (key_slot == CRYPT_ANY_SLOT) {
303 key_slot = find_keyslot_by_passphrase(cd, key_file,
304 _("Enter LUKS passphrase to be deleted: "));
310 log_std(cd, _("Key slot %d selected for deletion.\n"), key_slot);
313 ki = crypt_keyslot_status(cd, key_slot);
314 if (ki == CRYPT_SLOT_INVALID) {
315 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
316 key_slot, LUKS_NUMKEYS - 1);
320 if (ki <= CRYPT_SLOT_INACTIVE) {
321 log_err(cd, _("Key %d not active. Can't wipe.\n"), key_slot);
326 if (ki == CRYPT_SLOT_ACTIVE_LAST && cd->confirm &&
327 !(cd->confirm(_("This is the last keyslot."
328 " Device will become unusable after purging this key."),
329 cd->confirm_usrptr))) {
335 r = verify_other_keyslot(cd, other_key_file, key_slot);
340 r = crypt_keyslot_destroy(cd, key_slot);
342 return (r < 0) ? r : 0;
345 static int create_device_helper(struct crypt_device *cd,
349 const char *cipher_mode,
350 const char *key_file,
351 const char *passphrase,
352 size_t passphrase_size,
362 crypt_status_info ci;
363 char *dm_cipher = NULL;
364 char *processed_key = NULL;
370 ci = crypt_status(cd, name);
371 if (ci == CRYPT_INVALID)
374 if (reload && ci < CRYPT_ACTIVE)
377 if (!reload && ci >= CRYPT_ACTIVE) {
378 log_err(cd, _("Device %s already exists.\n"), name);
382 if (key_size < 0 || key_size > 1024) {
383 log_err(cd, _("Invalid key size %d.\n"), key_size);
387 r = device_check_and_adjust(cd, cd->device, !reload, &size, &offset, &read_only);
391 if (cipher_mode && asprintf(&dm_cipher, "%s-%s", cipher, cipher_mode) < 0)
394 processed_key = process_key(cd, hash, key_file, key_size, passphrase, passphrase_size);
395 if (!processed_key) {
400 r = dm_create_device(name, cd->device, dm_cipher ?: cipher, cd->type, uuid, size, skip, offset,
401 key_size, processed_key, read_only, reload);
404 crypt_safe_free(processed_key);
408 static int open_from_hdr_and_vk(struct crypt_device *cd,
409 struct volume_key *vk,
413 uint64_t size, offset;
415 int read_only, no_uuid, r;
418 offset = crypt_get_data_offset(cd);
419 read_only = flags & CRYPT_ACTIVATE_READONLY;
420 no_uuid = flags & CRYPT_ACTIVATE_NO_UUID;
422 r = device_check_and_adjust(cd, cd->device, 1, &size, &offset, &read_only);
426 if (asprintf(&cipher, "%s-%s", crypt_get_cipher(cd),
427 crypt_get_cipher_mode(cd)) < 0)
430 r = dm_create_device(name, cd->device, cipher, cd->type,
431 no_uuid ? NULL : crypt_get_uuid(cd),
432 size, 0, offset, vk->keylength, vk->key,
438 static void log_wrapper(int level, const char *msg, void *usrptr)
440 void (*xlog)(int level, char *msg) = usrptr;
441 xlog(level, (char *)msg);
444 static int yesDialog_wrapper(const char *msg, void *usrptr)
446 int (*xyesDialog)(char *msg) = usrptr;
447 return xyesDialog((char*)msg);
450 int crypt_confirm(struct crypt_device *cd, const char *msg)
452 if (!cd || !cd->confirm)
455 return cd->confirm(msg, cd->confirm_usrptr);
458 static int key_from_terminal(struct crypt_device *cd, char *msg, char **key,
459 size_t *key_len, int force_verify)
465 if(!msg && asprintf(&prompt, _("Enter passphrase for %s: "),
466 cd->backing_file ?: cd->device) < 0)
473 *key = crypt_safe_alloc(DEFAULT_PASSPHRASE_SIZE_MAX);
478 r = cd->password(msg, *key, DEFAULT_PASSPHRASE_SIZE_MAX,
479 cd->password_usrptr);
481 crypt_safe_free(*key);
486 r = crypt_get_key(msg, key, key_len, 0, NULL, cd->timeout,
487 (force_verify || cd->password_verify), cd);
490 return (r < 0) ? r: 0;
493 static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslot,
494 struct volume_key **vk)
496 char *passphrase_read = NULL;
497 size_t passphrase_size_read;
498 int r = -EINVAL, eperm = 0, tries = cd->tries;
502 crypt_free_volume_key(*vk);
505 r = key_from_terminal(cd, NULL, &passphrase_read,
506 &passphrase_size_read, 0);
510 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
511 passphrase_size_read, &cd->hdr, vk, cd);
514 crypt_safe_free(passphrase_read);
515 passphrase_read = NULL;
516 } while (r == -EPERM && (--tries > 0));
519 crypt_free_volume_key(*vk);
522 /* Report wrong passphrase if at least one try failed */
523 if (eperm && r == -EPIPE)
527 crypt_safe_free(passphrase_read);
531 static int key_from_file(struct crypt_device *cd, char *msg,
532 char **key, size_t *key_len,
533 const char *key_file, size_t key_size)
535 return crypt_get_key(msg, key, key_len, key_size, key_file,
539 static int _crypt_init(struct crypt_device **cd,
541 struct crypt_options *options,
542 int load, int need_dm)
546 /* if it is plain device and mapping table is being reloaded
547 initialize it by name*/
548 init_by_name = (type && !strcmp(type, CRYPT_PLAIN) && load);
550 /* Some of old API calls do not require DM in kernel,
551 fake initialisation by initialise it with kernel_check disabled */
553 (void)dm_init(NULL, 0);
555 r = crypt_init_by_name(cd, options->name);
557 r = crypt_init(cd, options->device);
564 crypt_set_log_callback(*cd, log_wrapper, options->icb->log);
565 crypt_set_confirm_callback(*cd, yesDialog_wrapper, options->icb->yesDialog);
567 crypt_set_timeout(*cd, options->timeout);
568 crypt_set_password_retry(*cd, options->tries);
569 crypt_set_iterarion_time(*cd, options->iteration_time ?: 1000);
570 crypt_set_password_verify(*cd, options->flags & CRYPT_FLAG_VERIFY);
572 if (load && !init_by_name)
573 r = crypt_load(*cd, type, NULL);
575 if (!r && type && !(*cd)->type) {
576 (*cd)->type = strdup(type);
587 void crypt_set_log_callback(struct crypt_device *cd,
588 void (*log)(int level, const char *msg, void *usrptr),
595 cd->log_usrptr = usrptr;
599 void crypt_set_confirm_callback(struct crypt_device *cd,
600 int (*confirm)(const char *msg, void *usrptr),
603 cd->confirm = confirm;
604 cd->confirm_usrptr = usrptr;
607 void crypt_set_password_callback(struct crypt_device *cd,
608 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
611 cd->password = password;
612 cd->password_usrptr = usrptr;
615 /* OPTIONS: name, cipher, device, hash, key_file, key_size, key_slot,
616 * offset, size, skip, timeout, tries, passphrase_fd (ignored),
618 static int crypt_create_and_update_device(struct crypt_options *options, int update)
620 struct crypt_device *cd = NULL;
621 char *passphrase = NULL;
622 size_t passphrase_size = 0;
625 r = _crypt_init(&cd, CRYPT_PLAIN, options, 0, 1);
629 r = crypt_get_key(_("Enter passphrase: "), &passphrase, &passphrase_size,
630 options->key_size, options->key_file,
631 cd->timeout, cd->password_verify, cd);
633 r = create_device_helper(cd, options->name, options->hash,
634 options->cipher, NULL, options->key_file,
635 passphrase, passphrase_size,
636 options->key_size, options->size, options->skip,
637 options->offset, NULL, options->flags & CRYPT_FLAG_READONLY,
638 options->flags, update);
640 crypt_safe_free(passphrase);
645 int crypt_create_device(struct crypt_options *options)
647 return crypt_create_and_update_device(options, 0);
650 int crypt_update_device(struct crypt_options *options)
652 return crypt_create_and_update_device(options, 1);
655 /* OPTIONS: name, size, icb */
656 int crypt_resize_device(struct crypt_options *options)
658 struct crypt_device *cd = NULL;
659 char *device = NULL, *cipher = NULL, *uuid = NULL, *key = NULL;
661 uint64_t size, skip, offset;
662 int key_size, read_only, r;
664 log_dbg("Resizing device %s to %" PRIu64 " sectors.", options->name, options->size);
666 if (dm_init(NULL, 1) < 0)
669 r = dm_query_device(options->name, &device, &size, &skip, &offset,
670 &cipher, &key_size, &key, &read_only, NULL, &uuid);
672 log_err(NULL, _("Device %s is not active.\n"), options->name);
676 /* Try to determine type of device from UUID */
679 if (!strncmp(uuid, CRYPT_PLAIN, strlen(CRYPT_PLAIN))) {
683 } else if (!strncmp(uuid, CRYPT_LUKS1, strlen(CRYPT_LUKS1)))
687 if (!options->device)
688 options->device = device;
690 r = _crypt_init(&cd, type, options, 1, 1);
694 size = options->size;
695 r = device_check_and_adjust(cd, device, 0, &size, &offset, &read_only);
699 r = dm_create_device(options->name, device, cipher, type,
700 crypt_get_uuid(cd), size, skip, offset,
701 key_size, key, read_only, 1);
703 crypt_safe_free(key);
705 if (options->device == device)
706 options->device = NULL;
714 /* OPTIONS: name, icb */
715 int crypt_query_device(struct crypt_options *options)
719 log_dbg("Query device %s.", options->name);
721 if (dm_init(NULL, 1) < 0)
724 r = dm_status_device(options->name);
728 r = dm_query_device(options->name, (char **)&options->device, &options->size,
729 &options->skip, &options->offset, (char **)&options->cipher,
730 &options->key_size, NULL, &read_only, NULL, NULL);
733 options->flags |= CRYPT_FLAG_READONLY;
735 options->flags |= CRYPT_FLAG_FREE_DEVICE;
736 options->flags |= CRYPT_FLAG_FREE_CIPHER;
748 /* OPTIONS: name, icb */
749 int crypt_remove_device(struct crypt_options *options)
751 struct crypt_device *cd = NULL;
754 r = crypt_init_by_name(&cd, options->name);
756 r = crypt_deactivate(cd, options->name);
763 /* OPTIONS: device, cipher, hash, align_payload, key_size (master key), key_slot
764 * new_key_file, iteration_time, timeout, flags, icb */
765 int crypt_luksFormat(struct crypt_options *options)
767 char cipherName[LUKS_CIPHERNAME_L];
768 char cipherMode[LUKS_CIPHERMODE_L];
771 struct crypt_device *cd = NULL;
772 struct crypt_params_luks1 cp = {
773 .hash = options->hash,
774 .data_alignment = options->align_payload
778 r = crypt_parse_name_and_mode(options->cipher, cipherName, NULL, cipherMode);
780 log_err(cd, _("No known cipher specification pattern detected.\n"));
784 if ((r = _crypt_init(&cd, CRYPT_LUKS1, options, 0, 1)))
787 if (options->key_slot >= LUKS_NUMKEYS && options->key_slot != CRYPT_ANY_SLOT) {
788 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
789 options->key_slot, LUKS_NUMKEYS - 1);
794 r = crypt_get_key(_("Enter LUKS passphrase: "), &password, &passwordLen, 0,
795 options->new_key_file, cd->timeout, cd->password_verify, cd);
800 r = crypt_format(cd, CRYPT_LUKS1, cipherName, cipherMode,
801 NULL, NULL, options->key_size, &cp);
805 /* Add keyslot using internally stored volume key generated during format */
806 r = crypt_keyslot_add_by_volume_key(cd, options->key_slot, NULL, 0,
807 password, passwordLen);
810 crypt_safe_free(password);
811 return (r < 0) ? r : 0;
814 /* OPTIONS: name, device, key_size, key_file, timeout, tries, flags, icb */
815 int crypt_luksOpen(struct crypt_options *options)
817 struct crypt_device *cd = NULL;
824 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
828 if (options->flags & CRYPT_FLAG_READONLY)
829 flags |= CRYPT_ACTIVATE_READONLY;
831 if (options->flags & CRYPT_FLAG_NON_EXCLUSIVE_ACCESS)
832 flags |= CRYPT_ACTIVATE_NO_UUID;
834 if (options->key_file)
835 r = crypt_activate_by_keyfile(cd, options->name,
836 CRYPT_ANY_SLOT, options->key_file, 0,
839 r = crypt_activate_by_passphrase(cd, options->name,
840 CRYPT_ANY_SLOT, options->passphrase,
841 options->passphrase ? strlen(options->passphrase) : 0,
845 return (r < 0) ? r : 0;
848 /* OPTIONS: device, keys_slot, key_file, timeout, flags, icb */
849 int crypt_luksKillSlot(struct crypt_options *options)
851 struct crypt_device *cd = NULL;
854 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
858 r = luks_remove_helper(cd, options->key_slot, options->key_file, NULL,
859 options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
862 return (r < 0) ? r : 0;
865 /* OPTIONS: device, new_key_file, key_file, timeout, flags, icb */
866 int crypt_luksRemoveKey(struct crypt_options *options)
868 struct crypt_device *cd = NULL;
871 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
875 r = luks_remove_helper(cd, CRYPT_ANY_SLOT, options->key_file, options->new_key_file,
876 options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
879 return (r < 0) ? r : 0;
883 /* OPTIONS: device, new_key_file, key_file, key_slot, flags,
884 iteration_time, timeout, icb */
885 int crypt_luksAddKey(struct crypt_options *options)
887 struct crypt_device *cd = NULL;
890 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
894 if (options->key_file || options->new_key_file)
895 r = crypt_keyslot_add_by_keyfile(cd, options->key_slot,
896 options->key_file, 0,
897 options->new_key_file, 0);
899 r = crypt_keyslot_add_by_passphrase(cd, options->key_slot,
903 return (r < 0) ? r : 0;
906 /* OPTIONS: device, icb */
907 int crypt_luksUUID(struct crypt_options *options)
909 struct crypt_device *cd = NULL;
913 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
917 uuid = (char *)crypt_get_uuid(cd);
918 log_std(cd, uuid ?: "");
924 /* OPTIONS: device, icb */
925 int crypt_isLuks(struct crypt_options *options)
927 struct crypt_device *cd = NULL;
930 log_dbg("Check device %s for LUKS header.", options->device);
936 r = crypt_init(&cd, options->device);
940 /* Do print fail here, no need to crypt_load() */
941 r = LUKS_read_phdr(cd->device, &cd->hdr, 0, cd) ? -EINVAL : 0;
947 /* OPTIONS: device, icb */
948 int crypt_luksDump(struct crypt_options *options)
950 struct crypt_device *cd = NULL;
953 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
963 void crypt_get_error(char *buf, size_t size)
965 const char *error = get_error();
967 if (!buf || size < 1)
970 strncpy(buf, error, size - 1);
971 buf[size - 1] = '\0';
977 void crypt_put_options(struct crypt_options *options)
979 if (options->flags & CRYPT_FLAG_FREE_DEVICE) {
980 free((char *)options->device);
981 options->device = NULL;
982 options->flags &= ~CRYPT_FLAG_FREE_DEVICE;
984 if (options->flags & CRYPT_FLAG_FREE_CIPHER) {
985 free((char *)options->cipher);
986 options->cipher = NULL;
987 options->flags &= ~CRYPT_FLAG_FREE_CIPHER;
991 const char *crypt_get_dir(void)
996 /////////////////////////////////
1001 int crypt_init(struct crypt_device **cd, const char *device)
1003 struct crypt_device *h = NULL;
1004 int r, readonly = 0;
1009 log_dbg("Allocating crypt device %s context.", device);
1011 if (!(h = malloc(sizeof(struct crypt_device))))
1014 memset(h, 0, sizeof(*h));
1018 r = device_ready(NULL, device, O_RDONLY);
1019 if (r == -ENOTBLK) {
1020 h->device = crypt_loop_get_device();
1021 log_dbg("Not a block device, %s%s.",
1022 h->device ? "using free loop device " :
1023 "no free loop device found",
1026 log_err(NULL, _("Cannot find a free loopback device.\n"));
1031 /* Keep the loop open, dettached on last close. */
1032 h->loop_fd = crypt_loop_attach(h->device, device, 0, 1, &readonly);
1033 if (h->loop_fd == -1) {
1034 log_err(NULL, _("Attaching loopback device failed "
1035 "(loop device with autoclear flag is required).\n"));
1040 h->backing_file = crypt_loop_backing_file(h->device);
1041 r = device_ready(NULL, h->device, O_RDONLY);
1049 if (!h->device && device && !(h->device = strdup(device))) {
1054 if (dm_init(h, 1) < 0) {
1059 h->iteration_time = 1000;
1060 h->password_verify = 0;
1062 h->rng_type = crypt_random_default_key_rng();
1068 if (h->loop_fd != -1)
1071 free(h->backing_file);
1077 int crypt_init_by_name(struct crypt_device **cd, const char *name)
1079 crypt_status_info ci;
1080 struct crypt_active_device cad;
1081 char *device = NULL, *cipher_full = NULL, *device_uuid = NULL;
1082 char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
1084 int key_size = 0, key_nums, r;
1087 log_dbg("Allocating crypt device context by device %s.", name);
1089 ci = crypt_status(NULL, name);
1090 if (ci == CRYPT_INVALID)
1093 if (ci < CRYPT_ACTIVE) {
1094 log_err(NULL, _("Device %s is not active.\n"), name);
1098 r = dm_query_device(name, &device, &cad.size, &cad.iv_offset, &cad.offset,
1099 &cipher_full, &key_size, &key, NULL, NULL,
1105 r = crypt_init(cd, device);
1107 /* Underlying device disappeared but mapping still active */
1108 if (!device || r == -ENOTBLK)
1109 log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
1112 /* Underlying device is not readable but crypt mapping exists */
1113 if (r == -ENOTBLK) {
1116 r = crypt_init(cd, NULL);
1122 /* Try to initialise basic parameters from active device */
1124 if (!(*cd)->backing_file && device && crypt_loop_device(device) &&
1125 !((*cd)->backing_file = crypt_loop_backing_file(device))) {
1131 if (!strncmp(CRYPT_PLAIN, device_uuid, sizeof(CRYPT_PLAIN)-1)) {
1132 (*cd)->type = strdup(CRYPT_PLAIN);
1133 (*cd)->plain_uuid = strdup(device_uuid);
1134 (*cd)->plain_hdr.hash = NULL; /* no way to get this */
1135 (*cd)->plain_hdr.offset = cad.offset;
1136 (*cd)->plain_hdr.skip = cad.iv_offset;
1137 (*cd)->volume_key = crypt_alloc_volume_key(key_size, key);
1138 if (!(*cd)->volume_key) {
1143 r = crypt_parse_name_and_mode(cipher_full, cipher, NULL, cipher_mode);
1145 (*cd)->plain_cipher = strdup(cipher);
1146 (*cd)->plain_cipher_mode = strdup(cipher_mode);
1148 } else if (!strncmp(CRYPT_LOOPAES, device_uuid, sizeof(CRYPT_LOOPAES)-1)) {
1149 (*cd)->type = strdup(CRYPT_LOOPAES);
1150 (*cd)->loopaes_uuid = strdup(device_uuid);
1151 (*cd)->loopaes_hdr.offset = cad.offset;
1153 r = crypt_parse_name_and_mode(cipher_full, cipher,
1154 &key_nums, cipher_mode);
1156 (*cd)->loopaes_cipher = strdup(cipher);
1157 (*cd)->loopaes_cipher_mode = strdup(cipher_mode);
1158 /* version 3 uses last key for IV */
1159 if (key_size % key_nums)
1161 (*cd)->loopaes_key_size = key_size / key_nums;
1163 } else if (!strncmp(CRYPT_LUKS1, device_uuid, sizeof(CRYPT_LUKS1)-1)) {
1165 if (crypt_load(*cd, CRYPT_LUKS1, NULL) < 0 ||
1166 crypt_volume_key_verify(*cd, key, key_size) < 0) {
1167 log_dbg("LUKS device header does not match active device.");
1171 (*cd)->volume_key = crypt_alloc_volume_key(key_size, key);
1172 if (!(*cd)->volume_key) {
1179 log_dbg("Active device has no UUID set, some parameters are not set.");
1186 crypt_safe_free(key);
1193 static int _crypt_format_plain(struct crypt_device *cd,
1195 const char *cipher_mode,
1197 size_t volume_key_size,
1198 struct crypt_params_plain *params)
1200 if (!cipher || !cipher_mode) {
1201 log_err(cd, _("Invalid plain crypt parameters.\n"));
1205 if (volume_key_size > 1024) {
1206 log_err(cd, _("Invalid key size.\n"));
1210 cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
1211 if (!cd->volume_key)
1214 cd->plain_cipher = strdup(cipher);
1215 cd->plain_cipher_mode = strdup(cipher_mode);
1218 cd->plain_uuid = strdup(uuid);
1220 if (params && params->hash)
1221 cd->plain_hdr.hash = strdup(params->hash);
1223 cd->plain_hdr.offset = params ? params->offset : 0;
1224 cd->plain_hdr.skip = params ? params->skip : 0;
1226 if (!cd->plain_cipher || !cd->plain_cipher_mode)
1232 static int _crypt_format_luks1(struct crypt_device *cd,
1234 const char *cipher_mode,
1236 const char *volume_key,
1237 size_t volume_key_size,
1238 struct crypt_params_luks1 *params)
1241 unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
1242 unsigned long alignment_offset = 0;
1245 log_err(cd, _("Can't format LUKS without device.\n"));
1250 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
1253 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
1258 if (params && params->data_alignment)
1259 required_alignment = params->data_alignment * SECTOR_SIZE;
1261 get_topology_alignment(cd->device, &required_alignment,
1262 &alignment_offset, DEFAULT_DISK_ALIGNMENT);
1264 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
1265 (params && params->hash) ? params->hash : "sha1",
1267 required_alignment / SECTOR_SIZE,
1268 alignment_offset / SECTOR_SIZE,
1269 cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1273 /* Wipe first 8 sectors - fs magic numbers etc. */
1274 r = wipe_device_header(cd->device, 8);
1277 log_err(cd, _("Cannot format device %s which is still in use.\n"),
1280 log_err(cd, _("Cannot wipe header on device %s.\n"),
1286 r = LUKS_write_phdr(cd->device, &cd->hdr, cd);
1291 static int _crypt_format_loopaes(struct crypt_device *cd,
1294 size_t volume_key_size,
1295 struct crypt_params_loopaes *params)
1298 log_err(cd, _("Can't format LOOPAES without device.\n"));
1302 if (volume_key_size > 1024) {
1303 log_err(cd, _("Invalid key size.\n"));
1307 cd->loopaes_key_size = volume_key_size;
1309 cd->loopaes_cipher = strdup(cipher ?: DEFAULT_LOOPAES_CIPHER);
1312 cd->loopaes_uuid = strdup(uuid);
1314 if (params && params->hash)
1315 cd->loopaes_hdr.hash = strdup(params->hash);
1317 cd->loopaes_hdr.offset = params ? params->offset : 0;
1318 cd->loopaes_hdr.skip = params ? params->skip : 0;
1323 int crypt_format(struct crypt_device *cd,
1326 const char *cipher_mode,
1328 const char *volume_key,
1329 size_t volume_key_size,
1337 log_dbg("Formatting device %s as type %s.", cd->device ?: "(none)", type);
1339 r = init_crypto(cd);
1344 r = _crypt_format_plain(cd, cipher, cipher_mode,
1345 uuid, volume_key_size, params);
1346 else if (isLUKS(type))
1347 r = _crypt_format_luks1(cd, cipher, cipher_mode,
1348 uuid, volume_key, volume_key_size, params);
1349 else if (isLOOPAES(type))
1350 r = _crypt_format_loopaes(cd, cipher, uuid, volume_key_size, params);
1352 /* FIXME: allow plugins here? */
1353 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
1357 if (!r && !(cd->type = strdup(type)))
1361 crypt_free_volume_key(cd->volume_key);
1362 cd->volume_key = NULL;
1368 int crypt_load(struct crypt_device *cd,
1369 const char *requested_type,
1372 struct luks_phdr hdr;
1375 log_dbg("Trying to load %s crypt type from device %s.",
1376 requested_type ?: "any", cd->device ?: "(none)");
1381 if (requested_type && !isLUKS(requested_type))
1384 r = init_crypto(cd);
1388 r = LUKS_read_phdr(cd->device, &hdr, 1, cd);
1391 memcpy(&cd->hdr, &hdr, sizeof(hdr));
1392 cd->type = strdup(CRYPT_LUKS1);
1400 int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
1402 char *device = NULL, *cipher = NULL, *uuid = NULL, *key = NULL;
1403 uint64_t size, skip, offset;
1404 int key_size, read_only, r;
1406 /* Device context type must be initialised */
1407 if (!cd->type || !crypt_get_uuid(cd))
1410 r = dm_query_device(name, &device, &size, &skip, &offset,
1411 &cipher, &key_size, &key, &read_only, NULL, &uuid);
1413 log_err(NULL, _("Device %s is not active.\n"), name);
1422 r = device_check_and_adjust(cd, device, 0, &new_size, &offset, &read_only);
1426 if (new_size == size) {
1427 log_dbg("Device has already requested size %" PRIu64
1433 log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
1435 r = dm_create_device(name, device, cipher, cd->type,
1436 crypt_get_uuid(cd), new_size, skip, offset,
1437 key_size, key, read_only, 1);
1439 crypt_safe_free(key);
1447 int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
1449 if (!isLUKS(cd->type)) {
1450 log_err(cd, _("This operation is not supported for this device type.\n"));
1454 if (uuid && !strncmp(uuid, cd->hdr.uuid, sizeof(cd->hdr.uuid))) {
1455 log_dbg("UUID is the same as requested (%s) for device %s.",
1461 log_dbg("Requested new UUID change to %s for %s.", uuid, cd->device);
1463 log_dbg("Requested new UUID refresh for %s.", cd->device);
1465 if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
1468 return LUKS_hdr_uuid_set(cd->device, &cd->hdr, uuid, cd);
1471 int crypt_header_backup(struct crypt_device *cd,
1472 const char *requested_type,
1473 const char *backup_file)
1477 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1480 r = init_crypto(cd);
1484 log_dbg("Requested header backup of device %s (%s) to "
1485 "file %s.", cd->device, requested_type, backup_file);
1487 return LUKS_hdr_backup(backup_file, cd->device, &cd->hdr, cd);
1490 int crypt_header_restore(struct crypt_device *cd,
1491 const char *requested_type,
1492 const char *backup_file)
1496 if (requested_type && !isLUKS(requested_type))
1499 /* Some hash functions need initialized gcrypt library */
1500 r = init_crypto(cd);
1504 log_dbg("Requested header restore to device %s (%s) from "
1505 "file %s.", cd->device, requested_type, backup_file);
1507 return LUKS_hdr_restore(backup_file, cd->device, &cd->hdr, cd);
1510 void crypt_free(struct crypt_device *cd)
1513 log_dbg("Releasing crypt device %s context.", cd->device);
1515 if (cd->loop_fd != -1)
1519 crypt_free_volume_key(cd->volume_key);
1522 free(cd->backing_file);
1525 /* used in plain device only */
1526 free((char*)cd->plain_hdr.hash);
1527 free(cd->plain_cipher);
1528 free(cd->plain_cipher_mode);
1529 free(cd->plain_uuid);
1531 /* used in loop-AES device only */
1532 free((char*)cd->loopaes_hdr.hash);
1533 free(cd->loopaes_cipher);
1534 free(cd->loopaes_uuid);
1540 int crypt_suspend(struct crypt_device *cd,
1543 crypt_status_info ci;
1544 int r, suspended = 0;
1546 log_dbg("Suspending volume %s.", name);
1548 ci = crypt_status(NULL, name);
1549 if (ci < CRYPT_ACTIVE) {
1550 log_err(cd, _("Volume %s is not active.\n"), name);
1554 if (!cd && dm_init(NULL, 1) < 0)
1557 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1558 NULL, NULL, NULL, NULL, &suspended, NULL);
1563 log_err(cd, _("Volume %s is already suspended.\n"), name);
1568 r = dm_suspend_and_wipe_key(name);
1570 log_err(cd, "Suspend is not supported for device %s.\n", name);
1572 log_err(cd, "Error during suspending device %s.\n", name);
1579 int crypt_resume_by_passphrase(struct crypt_device *cd,
1582 const char *passphrase,
1583 size_t passphrase_size)
1585 struct volume_key *vk = NULL;
1586 int r, suspended = 0;
1588 log_dbg("Resuming volume %s.", name);
1590 if (!isLUKS(cd->type)) {
1591 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1596 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1597 NULL, NULL, NULL, NULL, &suspended, NULL);
1602 log_err(cd, _("Volume %s is not suspended.\n"), name);
1607 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1608 passphrase_size, &cd->hdr, &vk, cd);
1610 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1614 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1616 log_err(cd, "Resume is not supported for device %s.\n", name);
1618 log_err(cd, "Error during resuming device %s.\n", name);
1622 crypt_free_volume_key(vk);
1623 return r < 0 ? r : keyslot;
1626 int crypt_resume_by_keyfile(struct crypt_device *cd,
1629 const char *keyfile,
1630 size_t keyfile_size)
1632 struct volume_key *vk = NULL;
1633 char *passphrase_read = NULL;
1634 size_t passphrase_size_read;
1635 int r, suspended = 0;
1637 log_dbg("Resuming volume %s.", name);
1639 if (!isLUKS(cd->type)) {
1640 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1645 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1646 NULL, NULL, NULL, NULL, &suspended, NULL);
1651 log_err(cd, _("Volume %s is not suspended.\n"), name);
1658 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1659 &passphrase_size_read, keyfile, keyfile_size);
1663 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1664 passphrase_size_read, &cd->hdr, &vk, cd);
1669 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1671 log_err(cd, "Error during resuming device %s.\n", name);
1673 crypt_safe_free(passphrase_read);
1674 crypt_free_volume_key(vk);
1675 return r < 0 ? r : keyslot;
1678 // slot manipulation
1679 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1680 int keyslot, // -1 any
1681 const char *passphrase, // NULL -> terminal
1682 size_t passphrase_size,
1683 const char *new_passphrase, // NULL -> terminal
1684 size_t new_passphrase_size)
1686 struct volume_key *vk = NULL;
1687 char *password = NULL, *new_password = NULL;
1688 size_t passwordLen, new_passwordLen;
1691 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1692 "new passphrase %sprovided.",
1693 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1695 if (!isLUKS(cd->type)) {
1696 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1700 r = keyslot_verify_or_find_empty(cd, &keyslot);
1704 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1705 /* No slots used, try to use pre-generated key in header */
1706 if (cd->volume_key) {
1707 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1708 r = vk ? 0 : -ENOMEM;
1710 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1713 } else if (passphrase) {
1714 /* Passphrase provided, use it to unlock existing keyslot */
1715 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, passphrase,
1716 passphrase_size, &cd->hdr, &vk, cd);
1718 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1719 r = key_from_terminal(cd, _("Enter any passphrase: "),
1720 &password, &passwordLen, 0);
1724 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
1725 passwordLen, &cd->hdr, &vk, cd);
1726 crypt_safe_free(password);
1732 if (new_passphrase) {
1733 new_password = (char *)new_passphrase;
1734 new_passwordLen = new_passphrase_size;
1736 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1737 &new_password, &new_passwordLen, 1);
1742 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1743 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1748 if (!new_passphrase)
1749 crypt_safe_free(new_password);
1750 crypt_free_volume_key(vk);
1751 return r ?: keyslot;
1754 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1756 const char *keyfile,
1757 size_t keyfile_size,
1758 const char *new_keyfile,
1759 size_t new_keyfile_size)
1761 struct volume_key *vk = NULL;
1762 char *password = NULL; size_t passwordLen;
1763 char *new_password = NULL; size_t new_passwordLen;
1766 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1767 keyfile ?: "[none]", new_keyfile ?: "[none]");
1769 if (!isLUKS(cd->type)) {
1770 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1774 r = keyslot_verify_or_find_empty(cd, &keyslot);
1778 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1779 /* No slots used, try to use pre-generated key in header */
1780 if (cd->volume_key) {
1781 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1782 r = vk ? 0 : -ENOMEM;
1784 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1788 /* Read password from file of (if NULL) from terminal */
1790 r = key_from_file(cd, _("Enter any passphrase: "),
1791 &password, &passwordLen,
1792 keyfile, keyfile_size);
1794 r = key_from_terminal(cd, _("Enter any passphrase: "),
1795 &password, &passwordLen, 0);
1799 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password, passwordLen,
1807 r = key_from_file(cd, _("Enter new passphrase for key slot: "),
1808 &new_password, &new_passwordLen, new_keyfile,
1811 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1812 &new_password, &new_passwordLen, 1);
1816 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1817 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1819 crypt_safe_free(password);
1820 crypt_safe_free(new_password);
1821 crypt_free_volume_key(vk);
1822 return r < 0 ? r : keyslot;
1825 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1827 const char *volume_key,
1828 size_t volume_key_size,
1829 const char *passphrase,
1830 size_t passphrase_size)
1832 struct volume_key *vk = NULL;
1834 char *new_password = NULL; size_t new_passwordLen;
1836 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1838 if (!isLUKS(cd->type)) {
1839 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1844 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1845 else if (cd->volume_key)
1846 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1851 r = LUKS_verify_volume_key(&cd->hdr, vk);
1853 log_err(cd, _("Volume key does not match the volume.\n"));
1857 r = keyslot_verify_or_find_empty(cd, &keyslot);
1862 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1863 &new_password, &new_passwordLen, 1);
1866 passphrase = new_password;
1867 passphrase_size = new_passwordLen;
1870 r = LUKS_set_key(cd->device, keyslot, passphrase, passphrase_size,
1871 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1873 crypt_safe_free(new_password);
1874 crypt_free_volume_key(vk);
1875 return (r < 0) ? r : keyslot;
1878 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1880 crypt_keyslot_info ki;
1882 log_dbg("Destroying keyslot %d.", keyslot);
1884 if (!isLUKS(cd->type)) {
1885 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1889 ki = crypt_keyslot_status(cd, keyslot);
1890 if (ki == CRYPT_SLOT_INVALID) {
1891 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1895 if (ki == CRYPT_SLOT_INACTIVE) {
1896 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1900 return LUKS_del_key(cd->device, keyslot, &cd->hdr, cd);
1903 // activation/deactivation of device mapping
1904 int crypt_activate_by_passphrase(struct crypt_device *cd,
1907 const char *passphrase,
1908 size_t passphrase_size,
1911 crypt_status_info ci;
1912 struct volume_key *vk = NULL;
1913 char *read_passphrase = NULL;
1914 size_t passphraseLen = 0;
1917 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1918 name ? "Activating" : "Checking", name ?: "",
1919 keyslot, passphrase ? "" : "[none] ");
1922 ci = crypt_status(NULL, name);
1923 if (ci == CRYPT_INVALID)
1925 else if (ci >= CRYPT_ACTIVE) {
1926 log_err(cd, _("Device %s already exists.\n"), name);
1931 /* plain, use hashed passphrase */
1932 if (isPLAIN(cd->type)) {
1934 r = key_from_terminal(cd, NULL, &read_passphrase,
1938 passphrase = read_passphrase;
1939 passphrase_size = passphraseLen;
1941 r = create_device_helper(cd, name, cd->plain_hdr.hash,
1942 cd->plain_cipher, cd->plain_cipher_mode,
1943 NULL, passphrase, passphrase_size,
1944 cd->volume_key->keylength, 0,
1945 cd->plain_hdr.skip, cd->plain_hdr.offset,
1947 flags & CRYPT_ACTIVATE_READONLY, 0, 0);
1949 } else if (isLUKS(cd->type)) {
1950 /* provided passphrase, do not retry */
1952 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1953 passphrase_size, &cd->hdr, &vk, cd);
1955 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1960 r = open_from_hdr_and_vk(cd, vk, name, flags);
1965 crypt_safe_free(read_passphrase);
1966 crypt_free_volume_key(vk);
1968 return r < 0 ? r : keyslot;
1971 int crypt_activate_by_keyfile(struct crypt_device *cd,
1974 const char *keyfile,
1975 size_t keyfile_size,
1978 crypt_status_info ci;
1979 struct volume_key *vk = NULL;
1980 char *passphrase_read = NULL;
1981 size_t passphrase_size_read;
1982 unsigned int key_count = 0;
1985 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1986 name ?: "", keyslot, keyfile ?: "[none]");
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);
2001 if (isPLAIN(cd->type)) {
2002 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
2003 &passphrase_size_read, keyfile, keyfile_size);
2006 r = create_device_helper(cd, name, cd->plain_hdr.hash,
2007 cd->plain_cipher, cd->plain_cipher_mode,
2008 NULL, passphrase_read, passphrase_size_read,
2009 cd->volume_key->keylength, 0,
2010 cd->plain_hdr.skip, cd->plain_hdr.offset,
2012 flags & CRYPT_ACTIVATE_READONLY, 0, 0);
2013 } else if (isLUKS(cd->type)) {
2014 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
2015 &passphrase_size_read, keyfile, keyfile_size);
2018 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
2019 passphrase_size_read, &cd->hdr, &vk, cd);
2025 r = open_from_hdr_and_vk(cd, vk, name, flags);
2030 } else if (isLOOPAES(cd->type)) {
2031 r = key_from_file(cd, NULL, &passphrase_read, &passphrase_size_read,
2032 keyfile, keyfile_size);
2035 r = LOOPAES_parse_keyfile(cd, &vk, cd->loopaes_hdr.hash, &key_count,
2036 passphrase_read, passphrase_size_read);
2040 r = LOOPAES_activate(cd, name, cd->loopaes_cipher,
2042 cd->loopaes_hdr.offset,
2043 cd->loopaes_hdr.skip,
2049 crypt_safe_free(passphrase_read);
2050 crypt_free_volume_key(vk);
2055 int crypt_activate_by_volume_key(struct crypt_device *cd,
2057 const char *volume_key,
2058 size_t volume_key_size,
2061 crypt_status_info ci;
2062 struct volume_key *vk;
2065 log_dbg("Activating volume %s by volume key.", name);
2067 /* use key directly, no hash */
2068 if (isPLAIN(cd->type)) {
2069 if (!volume_key || !volume_key_size || !cd->volume_key ||
2070 volume_key_size != cd->volume_key->keylength) {
2071 log_err(cd, _("Incorrect volume key specified for plain device.\n"));
2075 return create_device_helper(cd, name, NULL,
2076 cd->plain_cipher, cd->plain_cipher_mode, NULL, volume_key, volume_key_size,
2077 cd->volume_key->keylength, 0, cd->plain_hdr.skip,
2078 cd->plain_hdr.offset, cd->plain_uuid, flags & CRYPT_ACTIVATE_READONLY, 0, 0);
2081 if (!isLUKS(cd->type)) {
2082 log_err(cd, _("Device type is not properly initialised.\n"));
2087 ci = crypt_status(NULL, name);
2088 if (ci == CRYPT_INVALID)
2090 else if (ci >= CRYPT_ACTIVE) {
2091 log_err(cd, _("Device %s already exists.\n"), name);
2096 /* If key is not provided, try to use internal key */
2098 if (!cd->volume_key) {
2099 log_err(cd, _("Volume key does not match the volume.\n"));
2102 volume_key_size = cd->volume_key->keylength;
2103 volume_key = cd->volume_key->key;
2106 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2109 r = LUKS_verify_volume_key(&cd->hdr, vk);
2112 log_err(cd, _("Volume key does not match the volume.\n"));
2115 r = open_from_hdr_and_vk(cd, vk, name, flags);
2117 crypt_free_volume_key(vk);
2122 int crypt_deactivate(struct crypt_device *cd, const char *name)
2129 log_dbg("Deactivating volume %s.", name);
2131 if (!cd && dm_init(NULL, 1) < 0)
2134 switch (crypt_status(cd, name)) {
2136 r = dm_remove_device(name, 0, 0);
2139 log_err(cd, _("Device %s is busy.\n"), name);
2142 case CRYPT_INACTIVE:
2143 log_err(cd, _("Device %s is not active.\n"), name);
2147 log_err(cd, _("Invalid device %s.\n"), name);
2157 int crypt_volume_key_get(struct crypt_device *cd,
2160 size_t *volume_key_size,
2161 const char *passphrase,
2162 size_t passphrase_size)
2164 struct volume_key *vk;
2165 char *processed_key = NULL;
2168 key_len = crypt_get_volume_key_size(cd);
2169 if (key_len > *volume_key_size) {
2170 log_err(cd, _("Volume key buffer too small.\n"));
2174 if (isPLAIN(cd->type) && cd->plain_hdr.hash) {
2175 processed_key = process_key(cd, cd->plain_hdr.hash, NULL, key_len,
2176 passphrase, passphrase_size);
2177 if (!processed_key) {
2178 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
2181 memcpy(volume_key, processed_key, key_len);
2182 *volume_key_size = key_len;
2183 crypt_safe_free(processed_key);
2187 if (isLUKS(cd->type)) {
2188 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
2189 passphrase_size, &cd->hdr, &vk, cd);
2192 memcpy(volume_key, vk->key, vk->keylength);
2193 *volume_key_size = vk->keylength;
2196 crypt_free_volume_key(vk);
2200 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
2204 int crypt_volume_key_verify(struct crypt_device *cd,
2205 const char *volume_key,
2206 size_t volume_key_size)
2208 struct volume_key *vk;
2211 if (!isLUKS(cd->type)) {
2212 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2216 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2220 r = LUKS_verify_volume_key(&cd->hdr, vk);
2223 log_err(cd, _("Volume key does not match the volume.\n"));
2225 crypt_free_volume_key(vk);
2230 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
2232 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
2233 cd->timeout = timeout_sec;
2236 void crypt_set_password_retry(struct crypt_device *cd, int tries)
2238 log_dbg("Password retry count set to %d.", tries);
2242 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2244 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
2245 cd->iteration_time = iteration_time_ms;
2248 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
2250 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
2251 cd->password_verify = password_verify ? 1 : 0;
2254 void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
2257 case CRYPT_RNG_URANDOM:
2258 case CRYPT_RNG_RANDOM:
2259 log_dbg("RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
2260 cd->rng_type = rng_type;
2264 int crypt_get_rng_type(struct crypt_device *cd)
2269 return cd->rng_type;
2272 int crypt_memory_lock(struct crypt_device *cd, int lock)
2274 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
2278 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
2282 if (!cd && dm_init(NULL, 1) < 0)
2283 return CRYPT_INVALID;
2285 r = dm_status_device(name);
2290 if (r < 0 && r != -ENODEV)
2291 return CRYPT_INVALID;
2294 return CRYPT_ACTIVE;
2299 return CRYPT_INACTIVE;
2302 static void hexprintICB(struct crypt_device *cd, char *d, int n)
2305 for(i = 0; i < n; i++)
2306 log_std(cd, "%02hhx ", (char)d[i]);
2309 int crypt_dump(struct crypt_device *cd)
2312 if (!isLUKS(cd->type)) { //FIXME
2313 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2317 log_std(cd, "LUKS header information for %s\n\n", cd->device);
2318 log_std(cd, "Version: \t%d\n", cd->hdr.version);
2319 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
2320 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
2321 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec);
2322 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
2323 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
2324 log_std(cd, "MK digest: \t");
2325 hexprintICB(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE);
2327 log_std(cd, "MK salt: \t");
2328 hexprintICB(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2);
2329 log_std(cd, "\n \t");
2330 hexprintICB(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
2332 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
2333 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
2334 for(i = 0; i < LUKS_NUMKEYS; i++) {
2335 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
2336 log_std(cd, "Key Slot %d: ENABLED\n",i);
2337 log_std(cd, "\tIterations: \t%d\n",
2338 cd->hdr.keyblock[i].passwordIterations);
2339 log_std(cd, "\tSalt: \t");
2340 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt,
2342 log_std(cd, "\n\t \t");
2343 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt +
2344 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
2347 log_std(cd, "\tKey material offset:\t%d\n",
2348 cd->hdr.keyblock[i].keyMaterialOffset);
2349 log_std(cd, "\tAF stripes: \t%d\n",
2350 cd->hdr.keyblock[i].stripes);
2353 log_std(cd, "Key Slot %d: DISABLED\n", i);
2359 const char *crypt_get_cipher(struct crypt_device *cd)
2361 if (isPLAIN(cd->type))
2362 return cd->plain_cipher;
2364 if (isLUKS(cd->type))
2365 return cd->hdr.cipherName;
2367 if (isLOOPAES(cd->type))
2368 return cd->loopaes_cipher;
2373 const char *crypt_get_cipher_mode(struct crypt_device *cd)
2375 if (isPLAIN(cd->type))
2376 return cd->plain_cipher_mode;
2378 if (isLUKS(cd->type))
2379 return cd->hdr.cipherMode;
2381 if (isLOOPAES(cd->type))
2382 return cd->loopaes_cipher_mode;
2387 const char *crypt_get_uuid(struct crypt_device *cd)
2389 if (isLUKS(cd->type))
2390 return cd->hdr.uuid;
2392 if (isPLAIN(cd->type))
2393 return cd->plain_uuid;
2395 if (isLOOPAES(cd->type))
2396 return cd->loopaes_uuid;
2401 const char *crypt_get_device_name(struct crypt_device *cd)
2406 int crypt_get_volume_key_size(struct crypt_device *cd)
2408 if (isPLAIN(cd->type) && cd->volume_key)
2409 return cd->volume_key->keylength;
2411 if (isLUKS(cd->type))
2412 return cd->hdr.keyBytes;
2414 if (isLOOPAES(cd->type))
2415 return cd->loopaes_key_size;
2420 uint64_t crypt_get_data_offset(struct crypt_device *cd)
2422 if (isPLAIN(cd->type))
2423 return cd->plain_hdr.offset;
2425 if (isLUKS(cd->type))
2426 return cd->hdr.payloadOffset;
2428 if (isLOOPAES(cd->type))
2429 return cd->loopaes_hdr.offset;
2434 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
2436 if (!isLUKS(cd->type)) {
2437 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2438 return CRYPT_SLOT_INVALID;
2441 return LUKS_keyslot_info(&cd->hdr, keyslot);
2444 int crypt_keyslot_max(const char *type)
2446 if (type && isLUKS(type))
2447 return LUKS_NUMKEYS;
2452 const char *crypt_get_type(struct crypt_device *cd)
2457 int crypt_get_active_device(struct crypt_device *cd,
2459 struct crypt_active_device *cad)
2463 r = dm_query_device(name, NULL, &cad->size, &cad->iv_offset, &cad->offset,
2464 NULL, NULL, NULL, &readonly, NULL, NULL);
2468 cad->flags = readonly ? CRYPT_ACTIVATE_READONLY : 0;