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, 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);
512 crypt_safe_free(passphrase_read);
513 passphrase_read = NULL;
514 } while (r == -EPERM && (--tries > 0));
517 crypt_free_volume_key(*vk);
521 crypt_safe_free(passphrase_read);
525 static int key_from_file(struct crypt_device *cd, char *msg,
526 char **key, size_t *key_len,
527 const char *key_file, size_t key_size)
529 return crypt_get_key(msg, key, key_len, key_size, key_file,
533 static int _crypt_init(struct crypt_device **cd,
535 struct crypt_options *options,
536 int load, int need_dm)
540 /* if it is plain device and mapping table is being reloaded
541 initialize it by name*/
542 init_by_name = (type && !strcmp(type, CRYPT_PLAIN) && load);
544 /* Some of old API calls do not require DM in kernel,
545 fake initialisation by initialise it with kernel_check disabled */
547 (void)dm_init(NULL, 0);
549 r = crypt_init_by_name(cd, options->name);
551 r = crypt_init(cd, options->device);
558 crypt_set_log_callback(*cd, log_wrapper, options->icb->log);
559 crypt_set_confirm_callback(*cd, yesDialog_wrapper, options->icb->yesDialog);
561 crypt_set_timeout(*cd, options->timeout);
562 crypt_set_password_retry(*cd, options->tries);
563 crypt_set_iterarion_time(*cd, options->iteration_time ?: 1000);
564 crypt_set_password_verify(*cd, options->flags & CRYPT_FLAG_VERIFY);
566 if (load && !init_by_name)
567 r = crypt_load(*cd, type, NULL);
569 if (!r && type && !(*cd)->type) {
570 (*cd)->type = strdup(type);
581 void crypt_set_log_callback(struct crypt_device *cd,
582 void (*log)(int level, const char *msg, void *usrptr),
589 cd->log_usrptr = usrptr;
593 void crypt_set_confirm_callback(struct crypt_device *cd,
594 int (*confirm)(const char *msg, void *usrptr),
597 cd->confirm = confirm;
598 cd->confirm_usrptr = usrptr;
601 void crypt_set_password_callback(struct crypt_device *cd,
602 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
605 cd->password = password;
606 cd->password_usrptr = usrptr;
609 /* OPTIONS: name, cipher, device, hash, key_file, key_size, key_slot,
610 * offset, size, skip, timeout, tries, passphrase_fd (ignored),
612 static int crypt_create_and_update_device(struct crypt_options *options, int update)
614 struct crypt_device *cd = NULL;
615 char *passphrase = NULL;
616 size_t passphrase_size = 0;
619 r = _crypt_init(&cd, CRYPT_PLAIN, options, 0, 1);
623 r = crypt_get_key(_("Enter passphrase: "), &passphrase, &passphrase_size,
624 options->key_size, options->key_file,
625 cd->timeout, cd->password_verify, cd);
627 r = create_device_helper(cd, options->name, options->hash,
628 options->cipher, NULL, options->key_file,
629 passphrase, passphrase_size,
630 options->key_size, options->size, options->skip,
631 options->offset, NULL, options->flags & CRYPT_FLAG_READONLY,
632 options->flags, update);
634 crypt_safe_free(passphrase);
639 int crypt_create_device(struct crypt_options *options)
641 return crypt_create_and_update_device(options, 0);
644 int crypt_update_device(struct crypt_options *options)
646 return crypt_create_and_update_device(options, 1);
649 /* OPTIONS: name, size, icb */
650 int crypt_resize_device(struct crypt_options *options)
652 struct crypt_device *cd = NULL;
653 char *device = NULL, *cipher = NULL, *uuid = NULL, *key = NULL;
655 uint64_t size, skip, offset;
656 int key_size, read_only, r;
658 log_dbg("Resizing device %s to %" PRIu64 " sectors.", options->name, options->size);
660 if (dm_init(NULL, 1) < 0)
663 r = dm_query_device(options->name, &device, &size, &skip, &offset,
664 &cipher, &key_size, &key, &read_only, NULL, &uuid);
666 log_err(NULL, _("Device %s is not active.\n"), options->name);
670 /* Try to determine type of device from UUID */
673 if (!strncmp(uuid, CRYPT_PLAIN, strlen(CRYPT_PLAIN))) {
677 } else if (!strncmp(uuid, CRYPT_LUKS1, strlen(CRYPT_LUKS1)))
681 if (!options->device)
682 options->device = device;
684 r = _crypt_init(&cd, type, options, 1, 1);
688 size = options->size;
689 r = device_check_and_adjust(cd, device, 0, &size, &offset, &read_only);
693 r = dm_create_device(options->name, device, cipher, type,
694 crypt_get_uuid(cd), size, skip, offset,
695 key_size, key, read_only, 1);
697 crypt_safe_free(key);
699 if (options->device == device)
700 options->device = NULL;
708 /* OPTIONS: name, icb */
709 int crypt_query_device(struct crypt_options *options)
713 log_dbg("Query device %s.", options->name);
715 if (dm_init(NULL, 1) < 0)
718 r = dm_status_device(options->name);
722 r = dm_query_device(options->name, (char **)&options->device, &options->size,
723 &options->skip, &options->offset, (char **)&options->cipher,
724 &options->key_size, NULL, &read_only, NULL, NULL);
727 options->flags |= CRYPT_FLAG_READONLY;
729 options->flags |= CRYPT_FLAG_FREE_DEVICE;
730 options->flags |= CRYPT_FLAG_FREE_CIPHER;
742 /* OPTIONS: name, icb */
743 int crypt_remove_device(struct crypt_options *options)
745 struct crypt_device *cd = NULL;
748 r = crypt_init_by_name(&cd, options->name);
750 r = crypt_deactivate(cd, options->name);
757 /* OPTIONS: device, cipher, hash, align_payload, key_size (master key), key_slot
758 * new_key_file, iteration_time, timeout, flags, icb */
759 int crypt_luksFormat(struct crypt_options *options)
761 char cipherName[LUKS_CIPHERNAME_L];
762 char cipherMode[LUKS_CIPHERMODE_L];
765 struct crypt_device *cd = NULL;
766 struct crypt_params_luks1 cp = {
767 .hash = options->hash,
768 .data_alignment = options->align_payload
772 r = crypt_parse_name_and_mode(options->cipher, cipherName, NULL, cipherMode);
774 log_err(cd, _("No known cipher specification pattern detected.\n"));
778 if ((r = _crypt_init(&cd, CRYPT_LUKS1, options, 0, 1)))
781 if (options->key_slot >= LUKS_NUMKEYS && options->key_slot != CRYPT_ANY_SLOT) {
782 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
783 options->key_slot, LUKS_NUMKEYS - 1);
788 r = crypt_get_key(_("Enter LUKS passphrase: "), &password, &passwordLen, 0,
789 options->new_key_file, cd->timeout, cd->password_verify, cd);
794 r = crypt_format(cd, CRYPT_LUKS1, cipherName, cipherMode,
795 NULL, NULL, options->key_size, &cp);
799 /* Add keyslot using internally stored volume key generated during format */
800 r = crypt_keyslot_add_by_volume_key(cd, options->key_slot, NULL, 0,
801 password, passwordLen);
804 crypt_safe_free(password);
805 return (r < 0) ? r : 0;
808 /* OPTIONS: name, device, key_size, key_file, timeout, tries, flags, icb */
809 int crypt_luksOpen(struct crypt_options *options)
811 struct crypt_device *cd = NULL;
818 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
822 if (options->flags & CRYPT_FLAG_READONLY)
823 flags |= CRYPT_ACTIVATE_READONLY;
825 if (options->flags & CRYPT_FLAG_NON_EXCLUSIVE_ACCESS)
826 flags |= CRYPT_ACTIVATE_NO_UUID;
828 if (options->key_file)
829 r = crypt_activate_by_keyfile(cd, options->name,
830 CRYPT_ANY_SLOT, options->key_file, 0,
833 r = crypt_activate_by_passphrase(cd, options->name,
834 CRYPT_ANY_SLOT, options->passphrase,
835 options->passphrase ? strlen(options->passphrase) : 0,
839 return (r < 0) ? r : 0;
842 /* OPTIONS: device, keys_slot, key_file, timeout, flags, icb */
843 int crypt_luksKillSlot(struct crypt_options *options)
845 struct crypt_device *cd = NULL;
848 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
852 r = luks_remove_helper(cd, options->key_slot, options->key_file, NULL,
853 options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
856 return (r < 0) ? r : 0;
859 /* OPTIONS: device, new_key_file, key_file, timeout, flags, icb */
860 int crypt_luksRemoveKey(struct crypt_options *options)
862 struct crypt_device *cd = NULL;
865 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
869 r = luks_remove_helper(cd, CRYPT_ANY_SLOT, options->key_file, options->new_key_file,
870 options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
873 return (r < 0) ? r : 0;
877 /* OPTIONS: device, new_key_file, key_file, key_slot, flags,
878 iteration_time, timeout, icb */
879 int crypt_luksAddKey(struct crypt_options *options)
881 struct crypt_device *cd = NULL;
884 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
888 if (options->key_file || options->new_key_file)
889 r = crypt_keyslot_add_by_keyfile(cd, options->key_slot,
890 options->key_file, 0,
891 options->new_key_file, 0);
893 r = crypt_keyslot_add_by_passphrase(cd, options->key_slot,
897 return (r < 0) ? r : 0;
900 /* OPTIONS: device, icb */
901 int crypt_luksUUID(struct crypt_options *options)
903 struct crypt_device *cd = NULL;
907 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
911 uuid = (char *)crypt_get_uuid(cd);
912 log_std(cd, uuid ?: "");
918 /* OPTIONS: device, icb */
919 int crypt_isLuks(struct crypt_options *options)
921 struct crypt_device *cd = NULL;
924 log_dbg("Check device %s for LUKS header.", options->device);
930 r = crypt_init(&cd, options->device);
934 /* Do print fail here, no need to crypt_load() */
935 r = LUKS_read_phdr(cd->device, &cd->hdr, 0, cd) ? -EINVAL : 0;
941 /* OPTIONS: device, icb */
942 int crypt_luksDump(struct crypt_options *options)
944 struct crypt_device *cd = NULL;
947 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
957 void crypt_get_error(char *buf, size_t size)
959 const char *error = get_error();
961 if (!buf || size < 1)
964 strncpy(buf, error, size - 1);
965 buf[size - 1] = '\0';
971 void crypt_put_options(struct crypt_options *options)
973 if (options->flags & CRYPT_FLAG_FREE_DEVICE) {
974 free((char *)options->device);
975 options->device = NULL;
976 options->flags &= ~CRYPT_FLAG_FREE_DEVICE;
978 if (options->flags & CRYPT_FLAG_FREE_CIPHER) {
979 free((char *)options->cipher);
980 options->cipher = NULL;
981 options->flags &= ~CRYPT_FLAG_FREE_CIPHER;
985 const char *crypt_get_dir(void)
990 /////////////////////////////////
995 int crypt_init(struct crypt_device **cd, const char *device)
997 struct crypt_device *h = NULL;
1003 log_dbg("Allocating crypt device %s context.", device);
1005 if (!(h = malloc(sizeof(struct crypt_device))))
1008 memset(h, 0, sizeof(*h));
1012 r = device_ready(NULL, device, O_RDONLY);
1013 if (r == -ENOTBLK) {
1014 h->device = crypt_loop_get_device();
1015 log_dbg("Not a block device, %s%s.",
1016 h->device ? "using free loop device " :
1017 "no free loop device found",
1020 log_err(NULL, _("Cannot find a free loopback device.\n"));
1025 /* Keep the loop open, dettached on last close. */
1026 h->loop_fd = crypt_loop_attach(h->device, device, 0, 1, &readonly);
1027 if (h->loop_fd == -1) {
1028 log_err(NULL, _("Attaching loopback device failed "
1029 "(loop device with autoclear flag is required).\n"));
1034 h->backing_file = crypt_loop_backing_file(h->device);
1035 r = device_ready(NULL, h->device, O_RDONLY);
1043 if (!h->device && device && !(h->device = strdup(device))) {
1048 if (dm_init(h, 1) < 0) {
1053 h->iteration_time = 1000;
1054 h->password_verify = 0;
1056 h->rng_type = crypt_random_default_key_rng();
1062 if (h->loop_fd != -1)
1065 free(h->backing_file);
1071 int crypt_init_by_name(struct crypt_device **cd, const char *name)
1073 crypt_status_info ci;
1074 struct crypt_active_device cad;
1075 char *device = NULL, *cipher_full = NULL, *device_uuid = NULL;
1076 char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
1078 int key_size = 0, key_nums, r;
1081 log_dbg("Allocating crypt device context by device %s.", name);
1083 ci = crypt_status(NULL, name);
1084 if (ci == CRYPT_INVALID)
1087 if (ci < CRYPT_ACTIVE) {
1088 log_err(NULL, _("Device %s is not active.\n"), name);
1092 r = dm_query_device(name, &device, &cad.size, &cad.iv_offset, &cad.offset,
1093 &cipher_full, &key_size, &key, NULL, NULL,
1099 r = crypt_init(cd, device);
1101 /* Underlying device disappeared but mapping still active */
1102 if (!device || r == -ENOTBLK)
1103 log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
1106 /* Underlying device is not readable but crypt mapping exists */
1107 if (r == -ENOTBLK) {
1110 r = crypt_init(cd, NULL);
1116 /* Try to initialise basic parameters from active device */
1118 if (!(*cd)->backing_file && device && crypt_loop_device(device) &&
1119 !((*cd)->backing_file = crypt_loop_backing_file(device))) {
1125 if (!strncmp(CRYPT_PLAIN, device_uuid, sizeof(CRYPT_PLAIN)-1)) {
1126 (*cd)->type = strdup(CRYPT_PLAIN);
1127 (*cd)->plain_uuid = strdup(device_uuid);
1128 (*cd)->plain_hdr.hash = NULL; /* no way to get this */
1129 (*cd)->plain_hdr.offset = cad.offset;
1130 (*cd)->plain_hdr.skip = cad.iv_offset;
1131 (*cd)->volume_key = crypt_alloc_volume_key(key_size, key);
1132 if (!(*cd)->volume_key) {
1137 r = crypt_parse_name_and_mode(cipher_full, cipher, NULL, cipher_mode);
1139 (*cd)->plain_cipher = strdup(cipher);
1140 (*cd)->plain_cipher_mode = strdup(cipher_mode);
1142 } else if (!strncmp(CRYPT_LOOPAES, device_uuid, sizeof(CRYPT_LOOPAES)-1)) {
1143 (*cd)->type = strdup(CRYPT_LOOPAES);
1144 (*cd)->loopaes_uuid = strdup(device_uuid);
1145 (*cd)->loopaes_hdr.offset = cad.offset;
1147 r = crypt_parse_name_and_mode(cipher_full, cipher,
1148 &key_nums, cipher_mode);
1150 (*cd)->loopaes_cipher = strdup(cipher);
1151 (*cd)->loopaes_cipher_mode = strdup(cipher_mode);
1152 /* version 3 uses last key for IV */
1153 if (key_size % key_nums)
1155 (*cd)->loopaes_key_size = key_size / key_nums;
1157 } else if (!strncmp(CRYPT_LUKS1, device_uuid, sizeof(CRYPT_LUKS1)-1)) {
1159 if (crypt_load(*cd, CRYPT_LUKS1, NULL) < 0 ||
1160 crypt_volume_key_verify(*cd, key, key_size) < 0) {
1161 log_dbg("LUKS device header does not match active device.");
1165 (*cd)->volume_key = crypt_alloc_volume_key(key_size, key);
1166 if (!(*cd)->volume_key) {
1173 log_dbg("Active device has no UUID set, some parameters are not set.");
1180 crypt_safe_free(key);
1187 static int _crypt_format_plain(struct crypt_device *cd,
1189 const char *cipher_mode,
1191 size_t volume_key_size,
1192 struct crypt_params_plain *params)
1194 if (!cipher || !cipher_mode) {
1195 log_err(cd, _("Invalid plain crypt parameters.\n"));
1199 if (volume_key_size > 1024) {
1200 log_err(cd, _("Invalid key size.\n"));
1204 cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
1205 if (!cd->volume_key)
1208 cd->plain_cipher = strdup(cipher);
1209 cd->plain_cipher_mode = strdup(cipher_mode);
1212 cd->plain_uuid = strdup(uuid);
1214 if (params && params->hash)
1215 cd->plain_hdr.hash = strdup(params->hash);
1217 cd->plain_hdr.offset = params ? params->offset : 0;
1218 cd->plain_hdr.skip = params ? params->skip : 0;
1220 if (!cd->plain_cipher || !cd->plain_cipher_mode)
1226 static int _crypt_format_luks1(struct crypt_device *cd,
1228 const char *cipher_mode,
1230 const char *volume_key,
1231 size_t volume_key_size,
1232 struct crypt_params_luks1 *params)
1235 unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
1236 unsigned long alignment_offset = 0;
1239 log_err(cd, _("Can't format LUKS without device.\n"));
1244 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
1247 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
1252 if (params && params->data_alignment)
1253 required_alignment = params->data_alignment * SECTOR_SIZE;
1255 get_topology_alignment(cd->device, &required_alignment,
1256 &alignment_offset, DEFAULT_DISK_ALIGNMENT);
1258 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
1259 (params && params->hash) ? params->hash : "sha1",
1261 required_alignment / SECTOR_SIZE,
1262 alignment_offset / SECTOR_SIZE,
1263 cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1267 /* Wipe first 8 sectors - fs magic numbers etc. */
1268 r = wipe_device_header(cd->device, 8);
1271 log_err(cd, _("Cannot format device %s which is still in use.\n"),
1274 log_err(cd, _("Cannot wipe header on device %s.\n"),
1280 r = LUKS_write_phdr(cd->device, &cd->hdr, cd);
1285 static int _crypt_format_loopaes(struct crypt_device *cd,
1288 size_t volume_key_size,
1289 struct crypt_params_loopaes *params)
1292 log_err(cd, _("Can't format LOOPAES without device.\n"));
1296 if (volume_key_size > 1024) {
1297 log_err(cd, _("Invalid key size.\n"));
1301 cd->loopaes_key_size = volume_key_size;
1303 cd->loopaes_cipher = strdup(cipher ?: DEFAULT_LOOPAES_CIPHER);
1306 cd->loopaes_uuid = strdup(uuid);
1308 if (params && params->hash)
1309 cd->loopaes_hdr.hash = strdup(params->hash);
1311 cd->loopaes_hdr.offset = params ? params->offset : 0;
1316 int crypt_format(struct crypt_device *cd,
1319 const char *cipher_mode,
1321 const char *volume_key,
1322 size_t volume_key_size,
1330 log_dbg("Formatting device %s as type %s.", cd->device ?: "(none)", type);
1332 r = init_crypto(cd);
1337 r = _crypt_format_plain(cd, cipher, cipher_mode,
1338 uuid, volume_key_size, params);
1339 else if (isLUKS(type))
1340 r = _crypt_format_luks1(cd, cipher, cipher_mode,
1341 uuid, volume_key, volume_key_size, params);
1342 else if (isLOOPAES(type))
1343 r = _crypt_format_loopaes(cd, cipher, uuid, volume_key_size, params);
1345 /* FIXME: allow plugins here? */
1346 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
1350 if (!r && !(cd->type = strdup(type)))
1354 crypt_free_volume_key(cd->volume_key);
1355 cd->volume_key = NULL;
1361 int crypt_load(struct crypt_device *cd,
1362 const char *requested_type,
1365 struct luks_phdr hdr;
1368 log_dbg("Trying to load %s crypt type from device %s.",
1369 requested_type ?: "any", cd->device ?: "(none)");
1374 if (requested_type && !isLUKS(requested_type))
1377 r = init_crypto(cd);
1381 r = LUKS_read_phdr(cd->device, &hdr, 1, cd);
1384 memcpy(&cd->hdr, &hdr, sizeof(hdr));
1385 cd->type = strdup(CRYPT_LUKS1);
1393 int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
1395 char *device = NULL, *cipher = NULL, *uuid = NULL, *key = NULL;
1396 uint64_t size, skip, offset;
1397 int key_size, read_only, r;
1399 /* Device context type must be initialised */
1400 if (!cd->type || !crypt_get_uuid(cd))
1403 r = dm_query_device(name, &device, &size, &skip, &offset,
1404 &cipher, &key_size, &key, &read_only, NULL, &uuid);
1406 log_err(NULL, _("Device %s is not active.\n"), name);
1415 r = device_check_and_adjust(cd, device, 0, &new_size, &offset, &read_only);
1419 if (new_size == size) {
1420 log_dbg("Device has already requested size %" PRIu64
1426 log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
1428 r = dm_create_device(name, device, cipher, cd->type,
1429 crypt_get_uuid(cd), new_size, skip, offset,
1430 key_size, key, read_only, 1);
1432 crypt_safe_free(key);
1440 int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
1442 if (!isLUKS(cd->type)) {
1443 log_err(cd, _("This operation is not supported for this device type.\n"));
1447 if (uuid && !strncmp(uuid, cd->hdr.uuid, sizeof(cd->hdr.uuid))) {
1448 log_dbg("UUID is the same as requested (%s) for device %s.",
1454 log_dbg("Requested new UUID change to %s for %s.", uuid, cd->device);
1456 log_dbg("Requested new UUID refresh for %s.", cd->device);
1458 if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
1461 return LUKS_hdr_uuid_set(cd->device, &cd->hdr, uuid, cd);
1464 int crypt_header_backup(struct crypt_device *cd,
1465 const char *requested_type,
1466 const char *backup_file)
1470 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1473 r = init_crypto(cd);
1477 log_dbg("Requested header backup of device %s (%s) to "
1478 "file %s.", cd->device, requested_type, backup_file);
1480 return LUKS_hdr_backup(backup_file, cd->device, &cd->hdr, cd);
1483 int crypt_header_restore(struct crypt_device *cd,
1484 const char *requested_type,
1485 const char *backup_file)
1489 if (requested_type && !isLUKS(requested_type))
1492 /* Some hash functions need initialized gcrypt library */
1493 r = init_crypto(cd);
1497 log_dbg("Requested header restore to device %s (%s) from "
1498 "file %s.", cd->device, requested_type, backup_file);
1500 return LUKS_hdr_restore(backup_file, cd->device, &cd->hdr, cd);
1503 void crypt_free(struct crypt_device *cd)
1506 log_dbg("Releasing crypt device %s context.", cd->device);
1508 if (cd->loop_fd != -1)
1512 crypt_free_volume_key(cd->volume_key);
1515 free(cd->backing_file);
1518 /* used in plain device only */
1519 free((char*)cd->plain_hdr.hash);
1520 free(cd->plain_cipher);
1521 free(cd->plain_cipher_mode);
1522 free(cd->plain_uuid);
1528 int crypt_suspend(struct crypt_device *cd,
1531 crypt_status_info ci;
1532 int r, suspended = 0;
1534 log_dbg("Suspending volume %s.", name);
1536 ci = crypt_status(NULL, name);
1537 if (ci < CRYPT_ACTIVE) {
1538 log_err(cd, _("Volume %s is not active.\n"), name);
1542 if (!cd && dm_init(NULL, 1) < 0)
1545 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1546 NULL, NULL, NULL, NULL, &suspended, NULL);
1551 log_err(cd, _("Volume %s is already suspended.\n"), name);
1556 r = dm_suspend_and_wipe_key(name);
1558 log_err(cd, "Suspend is not supported for device %s.\n", name);
1560 log_err(cd, "Error during suspending device %s.\n", name);
1567 int crypt_resume_by_passphrase(struct crypt_device *cd,
1570 const char *passphrase,
1571 size_t passphrase_size)
1573 struct volume_key *vk = NULL;
1574 int r, suspended = 0;
1576 log_dbg("Resuming volume %s.", name);
1578 if (!isLUKS(cd->type)) {
1579 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1584 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1585 NULL, NULL, NULL, NULL, &suspended, NULL);
1590 log_err(cd, _("Volume %s is not suspended.\n"), name);
1595 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1596 passphrase_size, &cd->hdr, &vk, cd);
1598 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1602 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1604 log_err(cd, "Resume is not supported for device %s.\n", name);
1606 log_err(cd, "Error during resuming device %s.\n", name);
1610 crypt_free_volume_key(vk);
1611 return r < 0 ? r : keyslot;
1614 int crypt_resume_by_keyfile(struct crypt_device *cd,
1617 const char *keyfile,
1618 size_t keyfile_size)
1620 struct volume_key *vk = NULL;
1621 char *passphrase_read = NULL;
1622 size_t passphrase_size_read;
1623 int r, suspended = 0;
1625 log_dbg("Resuming volume %s.", name);
1627 if (!isLUKS(cd->type)) {
1628 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1633 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1634 NULL, NULL, NULL, NULL, &suspended, NULL);
1639 log_err(cd, _("Volume %s is not suspended.\n"), name);
1646 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1647 &passphrase_size_read, keyfile, keyfile_size);
1651 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1652 passphrase_size_read, &cd->hdr, &vk, cd);
1657 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1659 log_err(cd, "Error during resuming device %s.\n", name);
1661 crypt_safe_free(passphrase_read);
1662 crypt_free_volume_key(vk);
1663 return r < 0 ? r : keyslot;
1666 // slot manipulation
1667 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1668 int keyslot, // -1 any
1669 const char *passphrase, // NULL -> terminal
1670 size_t passphrase_size,
1671 const char *new_passphrase, // NULL -> terminal
1672 size_t new_passphrase_size)
1674 struct volume_key *vk = NULL;
1675 char *password = NULL, *new_password = NULL;
1676 size_t passwordLen, new_passwordLen;
1679 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1680 "new passphrase %sprovided.",
1681 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1683 if (!isLUKS(cd->type)) {
1684 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1688 r = keyslot_verify_or_find_empty(cd, &keyslot);
1692 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1693 /* No slots used, try to use pre-generated key in header */
1694 if (cd->volume_key) {
1695 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1696 r = vk ? 0 : -ENOMEM;
1698 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1701 } else if (passphrase) {
1702 /* Passphrase provided, use it to unlock existing keyslot */
1703 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, passphrase,
1704 passphrase_size, &cd->hdr, &vk, cd);
1706 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1707 r = key_from_terminal(cd, _("Enter any passphrase: "),
1708 &password, &passwordLen, 0);
1712 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
1713 passwordLen, &cd->hdr, &vk, cd);
1714 crypt_safe_free(password);
1720 if (new_passphrase) {
1721 new_password = (char *)new_passphrase;
1722 new_passwordLen = new_passphrase_size;
1724 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1725 &new_password, &new_passwordLen, 1);
1730 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1731 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1736 if (!new_passphrase)
1737 crypt_safe_free(new_password);
1738 crypt_free_volume_key(vk);
1739 return r ?: keyslot;
1742 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1744 const char *keyfile,
1745 size_t keyfile_size,
1746 const char *new_keyfile,
1747 size_t new_keyfile_size)
1749 struct volume_key *vk = NULL;
1750 char *password = NULL; size_t passwordLen;
1751 char *new_password = NULL; size_t new_passwordLen;
1754 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1755 keyfile ?: "[none]", new_keyfile ?: "[none]");
1757 if (!isLUKS(cd->type)) {
1758 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1762 r = keyslot_verify_or_find_empty(cd, &keyslot);
1766 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1767 /* No slots used, try to use pre-generated key in header */
1768 if (cd->volume_key) {
1769 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1770 r = vk ? 0 : -ENOMEM;
1772 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1776 /* Read password from file of (if NULL) from terminal */
1778 r = key_from_file(cd, _("Enter any passphrase: "),
1779 &password, &passwordLen,
1780 keyfile, keyfile_size);
1782 r = key_from_terminal(cd, _("Enter any passphrase: "),
1783 &password, &passwordLen, 0);
1787 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password, passwordLen,
1795 r = key_from_file(cd, _("Enter new passphrase for key slot: "),
1796 &new_password, &new_passwordLen, new_keyfile,
1799 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1800 &new_password, &new_passwordLen, 1);
1804 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1805 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1807 crypt_safe_free(password);
1808 crypt_safe_free(new_password);
1809 crypt_free_volume_key(vk);
1810 return r < 0 ? r : keyslot;
1813 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1815 const char *volume_key,
1816 size_t volume_key_size,
1817 const char *passphrase,
1818 size_t passphrase_size)
1820 struct volume_key *vk = NULL;
1822 char *new_password = NULL; size_t new_passwordLen;
1824 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1826 if (!isLUKS(cd->type)) {
1827 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1832 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1833 else if (cd->volume_key)
1834 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1839 r = LUKS_verify_volume_key(&cd->hdr, vk);
1841 log_err(cd, _("Volume key does not match the volume.\n"));
1845 r = keyslot_verify_or_find_empty(cd, &keyslot);
1850 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1851 &new_password, &new_passwordLen, 1);
1854 passphrase = new_password;
1855 passphrase_size = new_passwordLen;
1858 r = LUKS_set_key(cd->device, keyslot, passphrase, passphrase_size,
1859 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1861 crypt_safe_free(new_password);
1862 crypt_free_volume_key(vk);
1863 return (r < 0) ? r : keyslot;
1866 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1868 crypt_keyslot_info ki;
1870 log_dbg("Destroying keyslot %d.", keyslot);
1872 if (!isLUKS(cd->type)) {
1873 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1877 ki = crypt_keyslot_status(cd, keyslot);
1878 if (ki == CRYPT_SLOT_INVALID) {
1879 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1883 if (ki == CRYPT_SLOT_INACTIVE) {
1884 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1888 return LUKS_del_key(cd->device, keyslot, &cd->hdr, cd);
1891 // activation/deactivation of device mapping
1892 int crypt_activate_by_passphrase(struct crypt_device *cd,
1895 const char *passphrase,
1896 size_t passphrase_size,
1899 crypt_status_info ci;
1900 struct volume_key *vk = NULL;
1901 char *read_passphrase = NULL;
1902 size_t passphraseLen = 0;
1905 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1906 name ? "Activating" : "Checking", name ?: "",
1907 keyslot, passphrase ? "" : "[none] ");
1910 ci = crypt_status(NULL, name);
1911 if (ci == CRYPT_INVALID)
1913 else if (ci >= CRYPT_ACTIVE) {
1914 log_err(cd, _("Device %s already exists.\n"), name);
1919 /* plain, use hashed passphrase */
1920 if (isPLAIN(cd->type)) {
1922 r = key_from_terminal(cd, NULL, &read_passphrase,
1926 passphrase = read_passphrase;
1927 passphrase_size = passphraseLen;
1929 r = create_device_helper(cd, name, cd->plain_hdr.hash,
1930 cd->plain_cipher, cd->plain_cipher_mode,
1931 NULL, passphrase, passphrase_size,
1932 cd->volume_key->keylength, 0,
1933 cd->plain_hdr.skip, cd->plain_hdr.offset,
1935 flags & CRYPT_ACTIVATE_READONLY, 0, 0);
1937 } else if (isLUKS(cd->type)) {
1938 /* provided passphrase, do not retry */
1940 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1941 passphrase_size, &cd->hdr, &vk, cd);
1943 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1948 r = open_from_hdr_and_vk(cd, vk, name, flags);
1953 crypt_safe_free(read_passphrase);
1954 crypt_free_volume_key(vk);
1956 return r < 0 ? r : keyslot;
1959 int crypt_activate_by_keyfile(struct crypt_device *cd,
1962 const char *keyfile,
1963 size_t keyfile_size,
1966 crypt_status_info ci;
1967 struct volume_key *vk = NULL;
1968 char *passphrase_read = NULL;
1969 size_t passphrase_size_read;
1970 unsigned int key_count = 0;
1973 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1974 name ?: "", keyslot, keyfile ?: "[none]");
1977 ci = crypt_status(NULL, name);
1978 if (ci == CRYPT_INVALID)
1980 else if (ci >= CRYPT_ACTIVE) {
1981 log_err(cd, _("Device %s already exists.\n"), name);
1989 if (isPLAIN(cd->type)) {
1990 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1991 &passphrase_size_read, keyfile, keyfile_size);
1994 r = create_device_helper(cd, name, cd->plain_hdr.hash,
1995 cd->plain_cipher, cd->plain_cipher_mode,
1996 NULL, passphrase_read, passphrase_size_read,
1997 cd->volume_key->keylength, 0,
1998 cd->plain_hdr.skip, cd->plain_hdr.offset,
2000 flags & CRYPT_ACTIVATE_READONLY, 0, 0);
2001 } else if (isLUKS(cd->type)) {
2002 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
2003 &passphrase_size_read, keyfile, keyfile_size);
2006 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
2007 passphrase_size_read, &cd->hdr, &vk, cd);
2013 r = open_from_hdr_and_vk(cd, vk, name, flags);
2018 } else if (isLOOPAES(cd->type)) {
2019 r = key_from_file(cd, NULL, &passphrase_read, &passphrase_size_read,
2020 keyfile, keyfile_size);
2023 r = LOOPAES_parse_keyfile(cd, &vk, &key_count,
2024 passphrase_read, passphrase_size_read);
2028 r = LOOPAES_activate(cd, name, cd->loopaes_cipher,
2029 key_count, vk, flags);
2034 crypt_safe_free(passphrase_read);
2035 crypt_free_volume_key(vk);
2040 int crypt_activate_by_volume_key(struct crypt_device *cd,
2042 const char *volume_key,
2043 size_t volume_key_size,
2046 crypt_status_info ci;
2047 struct volume_key *vk;
2050 log_dbg("Activating volume %s by volume key.", name);
2052 /* use key directly, no hash */
2053 if (isPLAIN(cd->type)) {
2054 if (!volume_key || !volume_key_size || !cd->volume_key ||
2055 volume_key_size != cd->volume_key->keylength) {
2056 log_err(cd, _("Incorrect volume key specified for plain device.\n"));
2060 return create_device_helper(cd, name, NULL,
2061 cd->plain_cipher, cd->plain_cipher_mode, NULL, volume_key, volume_key_size,
2062 cd->volume_key->keylength, 0, cd->plain_hdr.skip,
2063 cd->plain_hdr.offset, cd->plain_uuid, flags & CRYPT_ACTIVATE_READONLY, 0, 0);
2066 if (!isLUKS(cd->type)) {
2067 log_err(cd, _("Device type is not properly initialised.\n"));
2072 ci = crypt_status(NULL, name);
2073 if (ci == CRYPT_INVALID)
2075 else if (ci >= CRYPT_ACTIVE) {
2076 log_err(cd, _("Device %s already exists.\n"), name);
2081 /* If key is not provided, try to use internal key */
2083 if (!cd->volume_key) {
2084 log_err(cd, _("Volume key does not match the volume.\n"));
2087 volume_key_size = cd->volume_key->keylength;
2088 volume_key = cd->volume_key->key;
2091 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2094 r = LUKS_verify_volume_key(&cd->hdr, vk);
2097 log_err(cd, _("Volume key does not match the volume.\n"));
2100 r = open_from_hdr_and_vk(cd, vk, name, flags);
2102 crypt_free_volume_key(vk);
2107 int crypt_deactivate(struct crypt_device *cd, const char *name)
2114 log_dbg("Deactivating volume %s.", name);
2116 if (!cd && dm_init(NULL, 1) < 0)
2119 switch (crypt_status(cd, name)) {
2121 r = dm_remove_device(name, 0, 0);
2124 log_err(cd, _("Device %s is busy.\n"), name);
2127 case CRYPT_INACTIVE:
2128 log_err(cd, _("Device %s is not active.\n"), name);
2132 log_err(cd, _("Invalid device %s.\n"), name);
2142 int crypt_volume_key_get(struct crypt_device *cd,
2145 size_t *volume_key_size,
2146 const char *passphrase,
2147 size_t passphrase_size)
2149 struct volume_key *vk;
2150 char *processed_key = NULL;
2153 key_len = crypt_get_volume_key_size(cd);
2154 if (key_len > *volume_key_size) {
2155 log_err(cd, _("Volume key buffer too small.\n"));
2159 if (isPLAIN(cd->type) && cd->plain_hdr.hash) {
2160 processed_key = process_key(cd, cd->plain_hdr.hash, NULL, key_len,
2161 passphrase, passphrase_size);
2162 if (!processed_key) {
2163 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
2166 memcpy(volume_key, processed_key, key_len);
2167 *volume_key_size = key_len;
2168 crypt_safe_free(processed_key);
2172 if (isLUKS(cd->type)) {
2173 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
2174 passphrase_size, &cd->hdr, &vk, cd);
2177 memcpy(volume_key, vk->key, vk->keylength);
2178 *volume_key_size = vk->keylength;
2181 crypt_free_volume_key(vk);
2185 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
2189 int crypt_volume_key_verify(struct crypt_device *cd,
2190 const char *volume_key,
2191 size_t volume_key_size)
2193 struct volume_key *vk;
2196 if (!isLUKS(cd->type)) {
2197 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2201 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2205 r = LUKS_verify_volume_key(&cd->hdr, vk);
2208 log_err(cd, _("Volume key does not match the volume.\n"));
2210 crypt_free_volume_key(vk);
2215 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
2217 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
2218 cd->timeout = timeout_sec;
2221 void crypt_set_password_retry(struct crypt_device *cd, int tries)
2223 log_dbg("Password retry count set to %d.", tries);
2227 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2229 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
2230 cd->iteration_time = iteration_time_ms;
2233 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
2235 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
2236 cd->password_verify = password_verify ? 1 : 0;
2239 void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
2242 case CRYPT_RNG_URANDOM:
2243 case CRYPT_RNG_RANDOM:
2244 log_dbg("RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
2245 cd->rng_type = rng_type;
2249 int crypt_get_rng_type(struct crypt_device *cd)
2254 return cd->rng_type;
2257 int crypt_memory_lock(struct crypt_device *cd, int lock)
2259 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
2263 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
2267 if (!cd && dm_init(NULL, 1) < 0)
2268 return CRYPT_INVALID;
2270 r = dm_status_device(name);
2275 if (r < 0 && r != -ENODEV)
2276 return CRYPT_INVALID;
2279 return CRYPT_ACTIVE;
2284 return CRYPT_INACTIVE;
2287 static void hexprintICB(struct crypt_device *cd, char *d, int n)
2290 for(i = 0; i < n; i++)
2291 log_std(cd, "%02hhx ", (char)d[i]);
2294 int crypt_dump(struct crypt_device *cd)
2297 if (!isLUKS(cd->type)) { //FIXME
2298 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2302 log_std(cd, "LUKS header information for %s\n\n", cd->device);
2303 log_std(cd, "Version: \t%d\n", cd->hdr.version);
2304 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
2305 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
2306 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec);
2307 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
2308 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
2309 log_std(cd, "MK digest: \t");
2310 hexprintICB(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE);
2312 log_std(cd, "MK salt: \t");
2313 hexprintICB(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2);
2314 log_std(cd, "\n \t");
2315 hexprintICB(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
2317 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
2318 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
2319 for(i = 0; i < LUKS_NUMKEYS; i++) {
2320 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
2321 log_std(cd, "Key Slot %d: ENABLED\n",i);
2322 log_std(cd, "\tIterations: \t%d\n",
2323 cd->hdr.keyblock[i].passwordIterations);
2324 log_std(cd, "\tSalt: \t");
2325 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt,
2327 log_std(cd, "\n\t \t");
2328 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt +
2329 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
2332 log_std(cd, "\tKey material offset:\t%d\n",
2333 cd->hdr.keyblock[i].keyMaterialOffset);
2334 log_std(cd, "\tAF stripes: \t%d\n",
2335 cd->hdr.keyblock[i].stripes);
2338 log_std(cd, "Key Slot %d: DISABLED\n", i);
2344 const char *crypt_get_cipher(struct crypt_device *cd)
2346 if (isPLAIN(cd->type))
2347 return cd->plain_cipher;
2349 if (isLUKS(cd->type))
2350 return cd->hdr.cipherName;
2352 if (isLOOPAES(cd->type))
2353 return cd->loopaes_cipher;
2358 const char *crypt_get_cipher_mode(struct crypt_device *cd)
2360 if (isPLAIN(cd->type))
2361 return cd->plain_cipher_mode;
2363 if (isLUKS(cd->type))
2364 return cd->hdr.cipherMode;
2366 if (isLOOPAES(cd->type))
2367 return cd->loopaes_cipher_mode;
2372 const char *crypt_get_uuid(struct crypt_device *cd)
2374 if (isLUKS(cd->type))
2375 return cd->hdr.uuid;
2377 if (isPLAIN(cd->type))
2378 return cd->plain_uuid;
2380 if (isLOOPAES(cd->type))
2381 return cd->loopaes_uuid;
2386 const char *crypt_get_device_name(struct crypt_device *cd)
2391 int crypt_get_volume_key_size(struct crypt_device *cd)
2393 if (isPLAIN(cd->type) && cd->volume_key)
2394 return cd->volume_key->keylength;
2396 if (isLUKS(cd->type))
2397 return cd->hdr.keyBytes;
2399 if (isLOOPAES(cd->type))
2400 return cd->loopaes_key_size;
2405 uint64_t crypt_get_data_offset(struct crypt_device *cd)
2407 if (isPLAIN(cd->type))
2408 return cd->plain_hdr.offset;
2410 if (isLUKS(cd->type))
2411 return cd->hdr.payloadOffset;
2413 if (isLOOPAES(cd->type))
2414 return cd->loopaes_hdr.offset;
2419 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
2421 if (!isLUKS(cd->type)) {
2422 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2423 return CRYPT_SLOT_INVALID;
2426 return LUKS_keyslot_info(&cd->hdr, keyslot);
2429 int crypt_keyslot_max(const char *type)
2431 if (type && isLUKS(type))
2432 return LUKS_NUMKEYS;
2437 const char *crypt_get_type(struct crypt_device *cd)
2442 int crypt_get_active_device(struct crypt_device *cd,
2444 struct crypt_active_device *cad)
2448 r = dm_query_device(name, NULL, &cad->size, &cad->iv_offset, &cad->offset,
2449 NULL, NULL, NULL, &readonly, NULL, NULL);
2453 cad->flags = readonly ? CRYPT_ACTIVATE_READONLY : 0;