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(void)
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 __attribute__((format(printf, 5, 6)))
97 void logger(struct crypt_device *cd, int level, const char *file,
98 int line, const char *format, ...)
103 va_start(argp, format);
105 if (vasprintf(&target, format, argp) > 0) {
107 crypt_log(cd, level, target);
109 } else if (_debug_level)
110 printf("# %s:%d %s\n", file ?: "?", line, target);
112 } else if (_debug_level)
113 printf("# %s\n", target);
121 static int init_crypto(struct crypt_device *ctx)
125 r = crypt_random_init(ctx);
127 log_err(ctx, _("Cannot initialize crypto RNG backend.\n"));
131 r = crypt_backend_init(ctx);
133 log_err(ctx, _("Cannot initialize crypto backend.\n"));
139 * Password processing behaviour matrix of process_key
141 * from binary file: check if there is sufficently large key material
142 * interactive & from fd: hash if requested, otherwise crop or pad with '0'
144 static char *process_key(struct crypt_device *cd, const char *hash_name,
145 const char *key_file, size_t key_size,
146 const char *pass, size_t passLen)
154 key = crypt_safe_alloc(key_size);
157 memset(key, 0, key_size);
159 /* key is coming from binary file */
160 if (key_file && strcmp(key_file, "-")) {
161 if(passLen < key_size) {
162 log_err(cd, ngettext("Cannot read %d byte from key file %s.\n",
163 "Cannot read %d bytes from key file %s.\n", key_size),
164 (int)key_size, key_file);
165 crypt_safe_free(key);
168 memcpy(key, pass, key_size);
172 /* key is coming from tty, fd or binary stdin */
174 r = crypt_plain_hash(cd, hash_name, key, key_size, pass, passLen);
177 log_err(cd, _("Hash algorithm %s not supported.\n"),
180 log_err(cd, _("Key processing error (using hash %s).\n"),
182 crypt_safe_free(key);
185 } else if (passLen > key_size) {
186 memcpy(key, pass, key_size);
188 memcpy(key, pass, passLen);
194 static int isPLAIN(const char *type)
196 return (type && !strcmp(CRYPT_PLAIN, type));
199 static int isLUKS(const char *type)
201 return (type && !strcmp(CRYPT_LUKS1, type));
204 static int isLOOPAES(const char *type)
206 return (type && !strcmp(CRYPT_LOOPAES, type));
209 /* keyslot helpers */
210 static int keyslot_verify_or_find_empty(struct crypt_device *cd, int *keyslot)
212 if (*keyslot == CRYPT_ANY_SLOT) {
213 *keyslot = LUKS_keyslot_find_empty(&cd->hdr);
215 log_err(cd, _("All key slots full.\n"));
220 switch (LUKS_keyslot_info(&cd->hdr, *keyslot)) {
221 case CRYPT_SLOT_INVALID:
222 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
223 *keyslot, LUKS_NUMKEYS - 1);
225 case CRYPT_SLOT_INACTIVE:
228 log_err(cd, _("Key slot %d is full, please select another one.\n"),
236 static int verify_other_keyslot(struct crypt_device *cd,
237 const char *key_file,
240 struct volume_key *vk = NULL;
241 crypt_keyslot_info ki;
243 char *password = NULL;
246 r = crypt_get_key(_("Enter any remaining LUKS passphrase: "),
247 &password, &passwordLen, 0, key_file, cd->timeout,
248 cd->password_verify, cd);
252 ki = crypt_keyslot_status(cd, keyIndex);
253 if (ki == CRYPT_SLOT_ACTIVE) /* Not last slot */
254 LUKS_keyslot_set(&cd->hdr, keyIndex, 0);
256 openedIndex = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT,
257 password, passwordLen,
260 if (ki == CRYPT_SLOT_ACTIVE)
261 LUKS_keyslot_set(&cd->hdr, keyIndex, 1);
266 log_verbose(cd, _("Key slot %d verified.\n"), openedIndex);
268 crypt_free_volume_key(vk);
269 crypt_safe_free(password);
273 static int find_keyslot_by_passphrase(struct crypt_device *cd,
274 const char *key_file,
277 struct volume_key *vk = NULL;
278 char *password = NULL;
282 r = crypt_get_key(message,&password,&passwordLen, 0, key_file,
283 cd->timeout, cd->password_verify, cd);
287 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
288 passwordLen, &cd->hdr, &vk, cd);
290 crypt_free_volume_key(vk);
291 crypt_safe_free(password);
295 static int luks_remove_helper(struct crypt_device *cd,
297 const char *other_key_file,
298 const char *key_file,
301 crypt_keyslot_info ki;
304 if (key_slot == CRYPT_ANY_SLOT) {
305 key_slot = find_keyslot_by_passphrase(cd, key_file,
306 _("Enter LUKS passphrase to be deleted: "));
312 log_std(cd, _("Key slot %d selected for deletion.\n"), key_slot);
315 ki = crypt_keyslot_status(cd, key_slot);
316 if (ki == CRYPT_SLOT_INVALID) {
317 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
318 key_slot, LUKS_NUMKEYS - 1);
322 if (ki <= CRYPT_SLOT_INACTIVE) {
323 log_err(cd, _("Key %d not active. Can't wipe.\n"), key_slot);
328 if (ki == CRYPT_SLOT_ACTIVE_LAST && cd->confirm &&
329 !(cd->confirm(_("This is the last keyslot."
330 " Device will become unusable after purging this key."),
331 cd->confirm_usrptr))) {
337 r = verify_other_keyslot(cd, other_key_file, key_slot);
342 r = crypt_keyslot_destroy(cd, key_slot);
344 return (r < 0) ? r : 0;
347 static int create_device_helper(struct crypt_device *cd,
351 const char *cipher_mode,
352 const char *key_file,
353 const char *passphrase,
354 size_t passphrase_size,
360 uint32_t activation_flags,
363 crypt_status_info ci;
364 char *dm_cipher = NULL;
365 char *processed_key = NULL;
366 enum devcheck device_check;
372 read_only = activation_flags & CRYPT_ACTIVATE_READONLY ? 1 : 0;
374 device_check = DEV_OK;
375 else if (activation_flags & CRYPT_ACTIVATE_SHARED)
376 device_check = DEV_SHARED;
378 device_check = DEV_EXCL;
380 ci = crypt_status(cd, name);
381 if (ci == CRYPT_INVALID)
384 if (reload && ci < CRYPT_ACTIVE)
387 if (!reload && ci >= CRYPT_ACTIVE) {
388 log_err(cd, _("Device %s already exists.\n"), name);
392 if (key_size > 1024) {
393 log_err(cd, _("Invalid key size %d.\n"), (int)key_size);
397 r = device_check_and_adjust(cd, cd->device, device_check, &size, &offset, &read_only);
401 if (cipher_mode && asprintf(&dm_cipher, "%s-%s", cipher, cipher_mode) < 0)
404 processed_key = process_key(cd, hash, key_file, key_size, passphrase, passphrase_size);
405 if (!processed_key) {
410 r = dm_create_device(name, cd->device, dm_cipher ?: cipher, cd->type, uuid, size, skip, offset,
411 key_size, processed_key, read_only, reload);
413 if (isPLAIN(cd->type) && !uuid)
414 (void)dm_query_device(name, NULL, NULL, NULL, NULL, NULL, NULL,
415 NULL, NULL, NULL, &cd->plain_uuid);
418 crypt_safe_free(processed_key);
422 static int open_from_hdr_and_vk(struct crypt_device *cd,
423 struct volume_key *vk,
427 uint64_t size, offset;
429 int read_only, no_uuid, r;
432 offset = crypt_get_data_offset(cd);
433 read_only = flags & CRYPT_ACTIVATE_READONLY;
434 no_uuid = flags & CRYPT_ACTIVATE_NO_UUID;
436 r = device_check_and_adjust(cd, cd->device, DEV_EXCL, &size, &offset, &read_only);
440 if (asprintf(&cipher, "%s-%s", crypt_get_cipher(cd),
441 crypt_get_cipher_mode(cd)) < 0)
444 r = dm_create_device(name, cd->device, cipher, cd->type,
445 no_uuid ? NULL : crypt_get_uuid(cd),
446 size, 0, offset, vk->keylength, vk->key,
452 static void log_wrapper(int level, const char *msg, void *usrptr)
454 void (*xlog)(int level, char *msg) = usrptr;
455 xlog(level, (char *)msg);
458 static int yesDialog_wrapper(const char *msg, void *usrptr)
460 int (*xyesDialog)(char *msg) = usrptr;
461 return xyesDialog((char*)msg);
464 int crypt_confirm(struct crypt_device *cd, const char *msg)
466 if (!cd || !cd->confirm)
469 return cd->confirm(msg, cd->confirm_usrptr);
472 static int key_from_terminal(struct crypt_device *cd, char *msg, char **key,
473 size_t *key_len, int force_verify)
479 if(!msg && asprintf(&prompt, _("Enter passphrase for %s: "),
480 cd->backing_file ?: cd->device) < 0)
487 *key = crypt_safe_alloc(DEFAULT_PASSPHRASE_SIZE_MAX);
492 r = cd->password(msg, *key, DEFAULT_PASSPHRASE_SIZE_MAX,
493 cd->password_usrptr);
495 crypt_safe_free(*key);
500 r = crypt_get_key(msg, key, key_len, 0, NULL, cd->timeout,
501 (force_verify || cd->password_verify), cd);
504 return (r < 0) ? r: 0;
507 static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslot,
508 struct volume_key **vk)
510 char *passphrase_read = NULL;
511 size_t passphrase_size_read;
512 int r = -EINVAL, eperm = 0, tries = cd->tries;
516 crypt_free_volume_key(*vk);
519 r = key_from_terminal(cd, NULL, &passphrase_read,
520 &passphrase_size_read, 0);
524 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
525 passphrase_size_read, &cd->hdr, vk, cd);
528 crypt_safe_free(passphrase_read);
529 passphrase_read = NULL;
530 } while (r == -EPERM && (--tries > 0));
533 crypt_free_volume_key(*vk);
536 /* Report wrong passphrase if at least one try failed */
537 if (eperm && r == -EPIPE)
541 crypt_safe_free(passphrase_read);
545 static int key_from_file(struct crypt_device *cd, char *msg,
546 char **key, size_t *key_len,
547 const char *key_file, size_t key_size)
549 return crypt_get_key(msg, key, key_len, key_size, key_file,
553 static int _crypt_init(struct crypt_device **cd,
555 struct crypt_options *options,
556 int load, int need_dm)
560 /* if it is plain device and mapping table is being reloaded
561 initialize it by name*/
562 init_by_name = (type && !strcmp(type, CRYPT_PLAIN) && load);
564 /* Some of old API calls do not require DM in kernel,
565 fake initialisation by initialise it with kernel_check disabled */
567 (void)dm_init(NULL, 0);
569 r = crypt_init_by_name(cd, options->name);
571 r = crypt_init(cd, options->device);
578 crypt_set_log_callback(*cd, log_wrapper, options->icb->log);
579 crypt_set_confirm_callback(*cd, yesDialog_wrapper, options->icb->yesDialog);
581 crypt_set_timeout(*cd, options->timeout);
582 crypt_set_password_retry(*cd, options->tries);
583 crypt_set_iterarion_time(*cd, options->iteration_time ?: 1000);
584 crypt_set_password_verify(*cd, options->flags & CRYPT_FLAG_VERIFY);
586 if (load && !init_by_name)
587 r = crypt_load(*cd, type, NULL);
589 if (!r && type && !(*cd)->type) {
590 (*cd)->type = strdup(type);
601 void crypt_set_log_callback(struct crypt_device *cd,
602 void (*log)(int level, const char *msg, void *usrptr),
609 cd->log_usrptr = usrptr;
613 void crypt_set_confirm_callback(struct crypt_device *cd,
614 int (*confirm)(const char *msg, void *usrptr),
617 cd->confirm = confirm;
618 cd->confirm_usrptr = usrptr;
621 void crypt_set_password_callback(struct crypt_device *cd,
622 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
625 cd->password = password;
626 cd->password_usrptr = usrptr;
629 /* OPTIONS: name, cipher, device, hash, key_file, key_size, key_slot,
630 * offset, size, skip, timeout, tries, passphrase_fd (ignored),
632 static int crypt_create_and_update_device(struct crypt_options *options, int update)
634 struct crypt_device *cd = NULL;
635 char *passphrase = NULL;
636 size_t passphrase_size = 0;
637 uint32_t activation_flags;
640 activation_flags = options->flags & CRYPT_FLAG_READONLY ?
641 CRYPT_ACTIVATE_READONLY : 0;
643 r = _crypt_init(&cd, CRYPT_PLAIN, options, 0, 1);
647 r = crypt_get_key(_("Enter passphrase: "), &passphrase, &passphrase_size,
648 options->key_size, options->key_file,
649 cd->timeout, cd->password_verify, cd);
651 r = create_device_helper(cd, options->name, options->hash,
652 options->cipher, NULL, options->key_file,
653 passphrase, passphrase_size,
654 options->key_size, options->size, options->skip,
655 options->offset, NULL, activation_flags, update);
657 crypt_safe_free(passphrase);
662 int crypt_create_device(struct crypt_options *options)
664 return crypt_create_and_update_device(options, 0);
667 int crypt_update_device(struct crypt_options *options)
669 return crypt_create_and_update_device(options, 1);
672 /* OPTIONS: name, size, icb */
673 int crypt_resize_device(struct crypt_options *options)
675 struct crypt_device *cd = NULL;
676 char *device = NULL, *cipher = NULL, *uuid = NULL, *key = NULL;
677 const char *type = NULL;
678 uint64_t size, skip, offset;
679 int key_size, read_only, r;
681 log_dbg("Resizing device %s to %" PRIu64 " sectors.", options->name, options->size);
683 if (dm_init(NULL, 1) < 0)
686 r = dm_query_device(options->name, &device, &size, &skip, &offset,
687 &cipher, &key_size, &key, &read_only, NULL, &uuid);
689 log_err(NULL, _("Device %s is not active.\n"), options->name);
693 /* Try to determine type of device from UUID */
696 if (!strncmp(uuid, CRYPT_PLAIN, strlen(CRYPT_PLAIN))) {
700 } else if (!strncmp(uuid, CRYPT_LUKS1, strlen(CRYPT_LUKS1)))
704 if (!options->device)
705 options->device = device;
707 r = _crypt_init(&cd, type, options, 1, 1);
711 size = options->size;
712 r = device_check_and_adjust(cd, device, DEV_OK, &size, &offset, &read_only);
716 r = dm_create_device(options->name, device, cipher, type,
717 crypt_get_uuid(cd), size, skip, offset,
718 key_size, key, read_only, 1);
720 crypt_safe_free(key);
722 if (options->device == device)
723 options->device = NULL;
731 /* OPTIONS: name, icb */
732 int crypt_query_device(struct crypt_options *options)
736 log_dbg("Query device %s.", options->name);
738 if (dm_init(NULL, 1) < 0)
741 r = dm_status_device(options->name);
745 r = dm_query_device(options->name, (char **)&options->device, &options->size,
746 &options->skip, &options->offset, (char **)&options->cipher,
747 &options->key_size, NULL, &read_only, NULL, NULL);
750 options->flags |= CRYPT_FLAG_READONLY;
752 options->flags |= CRYPT_FLAG_FREE_DEVICE;
753 options->flags |= CRYPT_FLAG_FREE_CIPHER;
765 /* OPTIONS: name, icb */
766 int crypt_remove_device(struct crypt_options *options)
768 struct crypt_device *cd = NULL;
771 r = crypt_init_by_name(&cd, options->name);
773 r = crypt_deactivate(cd, options->name);
780 /* OPTIONS: device, cipher, hash, align_payload, key_size (master key), key_slot
781 * new_key_file, iteration_time, timeout, flags, icb */
782 int crypt_luksFormat(struct crypt_options *options)
784 char cipherName[LUKS_CIPHERNAME_L];
785 char cipherMode[LUKS_CIPHERMODE_L];
788 struct crypt_device *cd = NULL;
789 struct crypt_params_luks1 cp = {
790 .hash = options->hash,
791 .data_alignment = options->align_payload
795 r = crypt_parse_name_and_mode(options->cipher, cipherName, NULL, cipherMode);
797 log_err(cd, _("No known cipher specification pattern detected.\n"));
801 if ((r = _crypt_init(&cd, CRYPT_LUKS1, options, 0, 1)))
804 if (options->key_slot >= LUKS_NUMKEYS && options->key_slot != CRYPT_ANY_SLOT) {
805 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
806 options->key_slot, LUKS_NUMKEYS - 1);
811 r = crypt_get_key(_("Enter LUKS passphrase: "), &password, &passwordLen, 0,
812 options->new_key_file, cd->timeout, cd->password_verify, cd);
817 r = crypt_format(cd, CRYPT_LUKS1, cipherName, cipherMode,
818 NULL, NULL, options->key_size, &cp);
822 /* Add keyslot using internally stored volume key generated during format */
823 r = crypt_keyslot_add_by_volume_key(cd, options->key_slot, NULL, 0,
824 password, passwordLen);
827 crypt_safe_free(password);
828 return (r < 0) ? r : 0;
831 /* OPTIONS: name, device, key_size, key_file, timeout, tries, flags, icb */
832 int crypt_luksOpen(struct crypt_options *options)
834 struct crypt_device *cd = NULL;
841 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
845 if (options->flags & CRYPT_FLAG_READONLY)
846 flags |= CRYPT_ACTIVATE_READONLY;
848 if (options->flags & CRYPT_FLAG_NON_EXCLUSIVE_ACCESS)
849 flags |= CRYPT_ACTIVATE_NO_UUID;
851 if (options->key_file)
852 r = crypt_activate_by_keyfile(cd, options->name,
853 CRYPT_ANY_SLOT, options->key_file, 0,
856 r = crypt_activate_by_passphrase(cd, options->name,
857 CRYPT_ANY_SLOT, options->passphrase,
858 options->passphrase ? strlen(options->passphrase) : 0,
862 return (r < 0) ? r : 0;
865 /* OPTIONS: device, keys_slot, key_file, timeout, flags, icb */
866 int crypt_luksKillSlot(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, options->key_slot, options->key_file, NULL,
876 options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
879 return (r < 0) ? r : 0;
882 /* OPTIONS: device, new_key_file, key_file, timeout, flags, icb */
883 int crypt_luksRemoveKey(struct crypt_options *options)
885 struct crypt_device *cd = NULL;
888 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
892 r = luks_remove_helper(cd, CRYPT_ANY_SLOT, options->key_file, options->new_key_file,
893 options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
896 return (r < 0) ? r : 0;
900 /* OPTIONS: device, new_key_file, key_file, key_slot, flags,
901 iteration_time, timeout, icb */
902 int crypt_luksAddKey(struct crypt_options *options)
904 struct crypt_device *cd = NULL;
907 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
911 if (options->key_file || options->new_key_file)
912 r = crypt_keyslot_add_by_keyfile(cd, options->key_slot,
913 options->key_file, 0,
914 options->new_key_file, 0);
916 r = crypt_keyslot_add_by_passphrase(cd, options->key_slot,
920 return (r < 0) ? r : 0;
923 /* OPTIONS: device, icb */
924 int crypt_luksUUID(struct crypt_options *options)
926 struct crypt_device *cd = NULL;
930 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
934 uuid = crypt_get_uuid(cd);
935 log_std(cd, "%s\n", uuid ?: "");
940 /* OPTIONS: device, icb */
941 int crypt_isLuks(struct crypt_options *options)
943 struct crypt_device *cd = NULL;
946 log_dbg("Check device %s for LUKS header.", options->device);
952 r = crypt_init(&cd, options->device);
956 /* Do print fail here, no need to crypt_load() */
957 r = LUKS_read_phdr(cd->device, &cd->hdr, 0, cd) ? -EINVAL : 0;
963 /* OPTIONS: device, icb */
964 int crypt_luksDump(struct crypt_options *options)
966 struct crypt_device *cd = NULL;
969 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
979 void crypt_get_error(char *buf, size_t size)
981 const char *error = get_error();
983 if (!buf || size < 1)
986 strncpy(buf, error, size - 1);
987 buf[size - 1] = '\0';
993 void crypt_put_options(struct crypt_options *options)
995 if (options->flags & CRYPT_FLAG_FREE_DEVICE) {
996 free((char *)options->device);
997 options->device = NULL;
998 options->flags &= ~CRYPT_FLAG_FREE_DEVICE;
1000 if (options->flags & CRYPT_FLAG_FREE_CIPHER) {
1001 free((char *)options->cipher);
1002 options->cipher = NULL;
1003 options->flags &= ~CRYPT_FLAG_FREE_CIPHER;
1007 const char *crypt_get_dir(void)
1009 return dm_get_dir();
1012 /////////////////////////////////
1017 int crypt_init(struct crypt_device **cd, const char *device)
1019 struct crypt_device *h = NULL;
1020 int r, readonly = 0;
1025 log_dbg("Allocating crypt device %s context.", device);
1027 if (!(h = malloc(sizeof(struct crypt_device))))
1030 memset(h, 0, sizeof(*h));
1034 r = device_ready(NULL, device, O_RDONLY);
1035 if (r == -ENOTBLK) {
1036 h->device = crypt_loop_get_device();
1037 log_dbg("Not a block device, %s%s.",
1038 h->device ? "using free loop device " :
1039 "no free loop device found",
1042 log_err(NULL, _("Cannot find a free loopback device.\n"));
1047 /* Keep the loop open, dettached on last close. */
1048 h->loop_fd = crypt_loop_attach(h->device, device, 0, 1, &readonly);
1049 if (h->loop_fd == -1) {
1050 log_err(NULL, _("Attaching loopback device failed "
1051 "(loop device with autoclear flag is required).\n"));
1056 h->backing_file = crypt_loop_backing_file(h->device);
1057 r = device_ready(NULL, h->device, O_RDONLY);
1065 if (!h->device && device && !(h->device = strdup(device))) {
1070 if (dm_init(h, 1) < 0) {
1075 h->iteration_time = 1000;
1076 h->password_verify = 0;
1078 h->rng_type = crypt_random_default_key_rng();
1084 if (h->loop_fd != -1)
1087 free(h->backing_file);
1093 int crypt_init_by_name(struct crypt_device **cd, const char *name)
1095 crypt_status_info ci;
1096 struct crypt_active_device cad;
1097 char *device = NULL, *cipher_full = NULL, *device_uuid = NULL;
1098 char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
1100 int key_size = 0, key_nums, r;
1103 log_dbg("Allocating crypt device context by device %s.", name);
1105 ci = crypt_status(NULL, name);
1106 if (ci == CRYPT_INVALID)
1109 if (ci < CRYPT_ACTIVE) {
1110 log_err(NULL, _("Device %s is not active.\n"), name);
1114 r = dm_query_device(name, &device, &cad.size, &cad.iv_offset, &cad.offset,
1115 &cipher_full, &key_size, &key, NULL, NULL,
1121 r = crypt_init(cd, device);
1123 /* Underlying device disappeared but mapping still active */
1124 if (!device || r == -ENOTBLK)
1125 log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
1128 /* Underlying device is not readable but crypt mapping exists */
1129 if (r == -ENOTBLK) {
1132 r = crypt_init(cd, NULL);
1138 /* Try to initialise basic parameters from active device */
1140 if (!(*cd)->backing_file && device && crypt_loop_device(device) &&
1141 !((*cd)->backing_file = crypt_loop_backing_file(device))) {
1147 if (!strncmp(CRYPT_PLAIN, device_uuid, sizeof(CRYPT_PLAIN)-1)) {
1148 (*cd)->type = strdup(CRYPT_PLAIN);
1149 (*cd)->plain_uuid = strdup(device_uuid);
1150 (*cd)->plain_hdr.hash = NULL; /* no way to get this */
1151 (*cd)->plain_hdr.offset = cad.offset;
1152 (*cd)->plain_hdr.skip = cad.iv_offset;
1153 (*cd)->volume_key = crypt_alloc_volume_key(key_size, key);
1154 if (!(*cd)->volume_key) {
1159 r = crypt_parse_name_and_mode(cipher_full, cipher, NULL, cipher_mode);
1161 (*cd)->plain_cipher = strdup(cipher);
1162 (*cd)->plain_cipher_mode = strdup(cipher_mode);
1164 } else if (!strncmp(CRYPT_LOOPAES, device_uuid, sizeof(CRYPT_LOOPAES)-1)) {
1165 (*cd)->type = strdup(CRYPT_LOOPAES);
1166 (*cd)->loopaes_uuid = strdup(device_uuid);
1167 (*cd)->loopaes_hdr.offset = cad.offset;
1169 r = crypt_parse_name_and_mode(cipher_full, cipher,
1170 &key_nums, cipher_mode);
1172 (*cd)->loopaes_cipher = strdup(cipher);
1173 (*cd)->loopaes_cipher_mode = strdup(cipher_mode);
1174 /* version 3 uses last key for IV */
1175 if (key_size % key_nums)
1177 (*cd)->loopaes_key_size = key_size / key_nums;
1179 } else if (!strncmp(CRYPT_LUKS1, device_uuid, sizeof(CRYPT_LUKS1)-1)) {
1181 if (crypt_load(*cd, CRYPT_LUKS1, NULL) < 0 ||
1182 crypt_volume_key_verify(*cd, key, key_size) < 0) {
1183 log_dbg("LUKS device header does not match active device.");
1187 (*cd)->volume_key = crypt_alloc_volume_key(key_size, key);
1188 if (!(*cd)->volume_key) {
1195 log_dbg("Active device has no UUID set, some parameters are not set.");
1202 crypt_safe_free(key);
1209 static int _crypt_format_plain(struct crypt_device *cd,
1211 const char *cipher_mode,
1213 size_t volume_key_size,
1214 struct crypt_params_plain *params)
1216 if (!cipher || !cipher_mode) {
1217 log_err(cd, _("Invalid plain crypt parameters.\n"));
1221 if (volume_key_size > 1024) {
1222 log_err(cd, _("Invalid key size.\n"));
1226 cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
1227 if (!cd->volume_key)
1230 cd->plain_cipher = strdup(cipher);
1231 cd->plain_cipher_mode = strdup(cipher_mode);
1234 cd->plain_uuid = strdup(uuid);
1236 if (params && params->hash)
1237 cd->plain_hdr.hash = strdup(params->hash);
1239 cd->plain_hdr.offset = params ? params->offset : 0;
1240 cd->plain_hdr.skip = params ? params->skip : 0;
1241 cd->plain_hdr.size = params ? params->size : 0;
1243 if (!cd->plain_cipher || !cd->plain_cipher_mode)
1249 static int _crypt_format_luks1(struct crypt_device *cd,
1251 const char *cipher_mode,
1253 const char *volume_key,
1254 size_t volume_key_size,
1255 struct crypt_params_luks1 *params)
1258 unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
1259 unsigned long alignment_offset = 0;
1262 log_err(cd, _("Can't format LUKS without device.\n"));
1267 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
1270 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
1275 if (params && params->data_alignment)
1276 required_alignment = params->data_alignment * SECTOR_SIZE;
1278 get_topology_alignment(cd->device, &required_alignment,
1279 &alignment_offset, DEFAULT_DISK_ALIGNMENT);
1281 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
1282 (params && params->hash) ? params->hash : "sha1",
1284 required_alignment / SECTOR_SIZE,
1285 alignment_offset / SECTOR_SIZE,
1286 cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1290 /* Wipe first 8 sectors - fs magic numbers etc. */
1291 r = wipe_device_header(cd->device, 8);
1294 log_err(cd, _("Cannot format device %s which is still in use.\n"),
1297 log_err(cd, _("Cannot wipe header on device %s.\n"),
1303 r = LUKS_write_phdr(cd->device, &cd->hdr, cd);
1308 static int _crypt_format_loopaes(struct crypt_device *cd,
1311 size_t volume_key_size,
1312 struct crypt_params_loopaes *params)
1315 log_err(cd, _("Can't format LOOPAES without device.\n"));
1319 if (volume_key_size > 1024) {
1320 log_err(cd, _("Invalid key size.\n"));
1324 cd->loopaes_key_size = volume_key_size;
1326 cd->loopaes_cipher = strdup(cipher ?: DEFAULT_LOOPAES_CIPHER);
1329 cd->loopaes_uuid = strdup(uuid);
1331 if (params && params->hash)
1332 cd->loopaes_hdr.hash = strdup(params->hash);
1334 cd->loopaes_hdr.offset = params ? params->offset : 0;
1335 cd->loopaes_hdr.skip = params ? params->skip : 0;
1340 int crypt_format(struct crypt_device *cd,
1343 const char *cipher_mode,
1345 const char *volume_key,
1346 size_t volume_key_size,
1354 log_dbg("Formatting device %s as type %s.", cd->device ?: "(none)", type);
1356 r = init_crypto(cd);
1361 r = _crypt_format_plain(cd, cipher, cipher_mode,
1362 uuid, volume_key_size, params);
1363 else if (isLUKS(type))
1364 r = _crypt_format_luks1(cd, cipher, cipher_mode,
1365 uuid, volume_key, volume_key_size, params);
1366 else if (isLOOPAES(type))
1367 r = _crypt_format_loopaes(cd, cipher, uuid, volume_key_size, params);
1369 /* FIXME: allow plugins here? */
1370 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
1374 if (!r && !(cd->type = strdup(type)))
1378 crypt_free_volume_key(cd->volume_key);
1379 cd->volume_key = NULL;
1385 int crypt_load(struct crypt_device *cd,
1386 const char *requested_type,
1387 void *params __attribute__((unused)))
1389 struct luks_phdr hdr;
1392 log_dbg("Trying to load %s crypt type from device %s.",
1393 requested_type ?: "any", cd->device ?: "(none)");
1398 if (requested_type && !isLUKS(requested_type))
1401 r = init_crypto(cd);
1405 r = LUKS_read_phdr(cd->device, &hdr, 1, cd);
1408 memcpy(&cd->hdr, &hdr, sizeof(hdr));
1409 cd->type = strdup(CRYPT_LUKS1);
1417 int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
1419 char *device = NULL, *cipher = NULL, *uuid = NULL, *key = NULL;
1420 uint64_t size, skip, offset;
1421 int key_size, read_only, r;
1423 /* Device context type must be initialised */
1424 if (!cd->type || !crypt_get_uuid(cd))
1427 log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
1429 r = dm_query_device(name, &device, &size, &skip, &offset,
1430 &cipher, &key_size, &key, &read_only, NULL, &uuid);
1432 log_err(NULL, _("Device %s is not active.\n"), name);
1441 r = device_check_and_adjust(cd, device, DEV_OK, &new_size, &offset, &read_only);
1445 if (new_size == size) {
1446 log_dbg("Device has already requested size %" PRIu64
1452 r = dm_create_device(name, device, cipher, cd->type,
1453 crypt_get_uuid(cd), new_size, skip, offset,
1454 key_size, key, read_only, 1);
1456 crypt_safe_free(key);
1464 int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
1466 if (!isLUKS(cd->type)) {
1467 log_err(cd, _("This operation is not supported for this device type.\n"));
1471 if (uuid && !strncmp(uuid, cd->hdr.uuid, sizeof(cd->hdr.uuid))) {
1472 log_dbg("UUID is the same as requested (%s) for device %s.",
1478 log_dbg("Requested new UUID change to %s for %s.", uuid, cd->device);
1480 log_dbg("Requested new UUID refresh for %s.", cd->device);
1482 if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
1485 return LUKS_hdr_uuid_set(cd->device, &cd->hdr, uuid, cd);
1488 int crypt_header_backup(struct crypt_device *cd,
1489 const char *requested_type,
1490 const char *backup_file)
1494 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1497 r = init_crypto(cd);
1501 log_dbg("Requested header backup of device %s (%s) to "
1502 "file %s.", cd->device, requested_type, backup_file);
1504 return LUKS_hdr_backup(backup_file, cd->device, &cd->hdr, cd);
1507 int crypt_header_restore(struct crypt_device *cd,
1508 const char *requested_type,
1509 const char *backup_file)
1513 if (requested_type && !isLUKS(requested_type))
1516 /* Some hash functions need initialized gcrypt library */
1517 r = init_crypto(cd);
1521 log_dbg("Requested header restore to device %s (%s) from "
1522 "file %s.", cd->device, requested_type, backup_file);
1524 return LUKS_hdr_restore(backup_file, cd->device, &cd->hdr, cd);
1527 void crypt_free(struct crypt_device *cd)
1530 log_dbg("Releasing crypt device %s context.", cd->device);
1532 if (cd->loop_fd != -1)
1536 crypt_free_volume_key(cd->volume_key);
1539 free(cd->backing_file);
1542 /* used in plain device only */
1543 free((char*)cd->plain_hdr.hash);
1544 free(cd->plain_cipher);
1545 free(cd->plain_cipher_mode);
1546 free(cd->plain_uuid);
1548 /* used in loop-AES device only */
1549 free((char*)cd->loopaes_hdr.hash);
1550 free(cd->loopaes_cipher);
1551 free(cd->loopaes_uuid);
1557 int crypt_suspend(struct crypt_device *cd,
1560 crypt_status_info ci;
1561 int r, suspended = 0;
1563 log_dbg("Suspending volume %s.", name);
1565 ci = crypt_status(NULL, name);
1566 if (ci < CRYPT_ACTIVE) {
1567 log_err(cd, _("Volume %s is not active.\n"), name);
1571 if (!cd && dm_init(NULL, 1) < 0)
1574 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1575 NULL, NULL, NULL, NULL, &suspended, NULL);
1580 log_err(cd, _("Volume %s is already suspended.\n"), name);
1585 r = dm_suspend_and_wipe_key(name);
1587 log_err(cd, "Suspend is not supported for device %s.\n", name);
1589 log_err(cd, "Error during suspending device %s.\n", name);
1596 int crypt_resume_by_passphrase(struct crypt_device *cd,
1599 const char *passphrase,
1600 size_t passphrase_size)
1602 struct volume_key *vk = NULL;
1603 int r, suspended = 0;
1605 log_dbg("Resuming volume %s.", name);
1607 if (!isLUKS(cd->type)) {
1608 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1613 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1614 NULL, NULL, NULL, NULL, &suspended, NULL);
1619 log_err(cd, _("Volume %s is not suspended.\n"), name);
1624 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1625 passphrase_size, &cd->hdr, &vk, cd);
1627 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1631 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1633 log_err(cd, "Resume is not supported for device %s.\n", name);
1635 log_err(cd, "Error during resuming device %s.\n", name);
1639 crypt_free_volume_key(vk);
1640 return r < 0 ? r : keyslot;
1643 int crypt_resume_by_keyfile(struct crypt_device *cd,
1646 const char *keyfile,
1647 size_t keyfile_size)
1649 struct volume_key *vk = NULL;
1650 char *passphrase_read = NULL;
1651 size_t passphrase_size_read;
1652 int r, suspended = 0;
1654 log_dbg("Resuming volume %s.", name);
1656 if (!isLUKS(cd->type)) {
1657 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1662 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1663 NULL, NULL, NULL, NULL, &suspended, NULL);
1668 log_err(cd, _("Volume %s is not suspended.\n"), name);
1675 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1676 &passphrase_size_read, keyfile, keyfile_size);
1680 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1681 passphrase_size_read, &cd->hdr, &vk, cd);
1686 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1688 log_err(cd, "Error during resuming device %s.\n", name);
1690 crypt_safe_free(passphrase_read);
1691 crypt_free_volume_key(vk);
1692 return r < 0 ? r : keyslot;
1695 // slot manipulation
1696 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1697 int keyslot, // -1 any
1698 const char *passphrase, // NULL -> terminal
1699 size_t passphrase_size,
1700 const char *new_passphrase, // NULL -> terminal
1701 size_t new_passphrase_size)
1703 struct volume_key *vk = NULL;
1704 char *password = NULL, *new_password = NULL;
1705 size_t passwordLen, new_passwordLen;
1708 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1709 "new passphrase %sprovided.",
1710 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1712 if (!isLUKS(cd->type)) {
1713 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1717 r = keyslot_verify_or_find_empty(cd, &keyslot);
1721 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1722 /* No slots used, try to use pre-generated key in header */
1723 if (cd->volume_key) {
1724 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1725 r = vk ? 0 : -ENOMEM;
1727 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1730 } else if (passphrase) {
1731 /* Passphrase provided, use it to unlock existing keyslot */
1732 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, passphrase,
1733 passphrase_size, &cd->hdr, &vk, cd);
1735 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1736 r = key_from_terminal(cd, _("Enter any passphrase: "),
1737 &password, &passwordLen, 0);
1741 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
1742 passwordLen, &cd->hdr, &vk, cd);
1743 crypt_safe_free(password);
1749 if (new_passphrase) {
1750 new_password = (char *)new_passphrase;
1751 new_passwordLen = new_passphrase_size;
1753 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1754 &new_password, &new_passwordLen, 1);
1759 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1760 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1765 if (!new_passphrase)
1766 crypt_safe_free(new_password);
1767 crypt_free_volume_key(vk);
1768 return r ?: keyslot;
1771 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1773 const char *keyfile,
1774 size_t keyfile_size,
1775 const char *new_keyfile,
1776 size_t new_keyfile_size)
1778 struct volume_key *vk = NULL;
1779 char *password = NULL; size_t passwordLen;
1780 char *new_password = NULL; size_t new_passwordLen;
1783 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1784 keyfile ?: "[none]", new_keyfile ?: "[none]");
1786 if (!isLUKS(cd->type)) {
1787 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1791 r = keyslot_verify_or_find_empty(cd, &keyslot);
1795 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1796 /* No slots used, try to use pre-generated key in header */
1797 if (cd->volume_key) {
1798 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1799 r = vk ? 0 : -ENOMEM;
1801 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1805 /* Read password from file of (if NULL) from terminal */
1807 r = key_from_file(cd, _("Enter any passphrase: "),
1808 &password, &passwordLen,
1809 keyfile, keyfile_size);
1811 r = key_from_terminal(cd, _("Enter any passphrase: "),
1812 &password, &passwordLen, 0);
1816 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password, passwordLen,
1824 r = key_from_file(cd, _("Enter new passphrase for key slot: "),
1825 &new_password, &new_passwordLen, new_keyfile,
1828 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1829 &new_password, &new_passwordLen, 1);
1833 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1834 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1836 crypt_safe_free(password);
1837 crypt_safe_free(new_password);
1838 crypt_free_volume_key(vk);
1839 return r < 0 ? r : keyslot;
1842 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1844 const char *volume_key,
1845 size_t volume_key_size,
1846 const char *passphrase,
1847 size_t passphrase_size)
1849 struct volume_key *vk = NULL;
1851 char *new_password = NULL; size_t new_passwordLen;
1853 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1855 if (!isLUKS(cd->type)) {
1856 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1861 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1862 else if (cd->volume_key)
1863 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1868 r = LUKS_verify_volume_key(&cd->hdr, vk);
1870 log_err(cd, _("Volume key does not match the volume.\n"));
1874 r = keyslot_verify_or_find_empty(cd, &keyslot);
1879 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1880 &new_password, &new_passwordLen, 1);
1883 passphrase = new_password;
1884 passphrase_size = new_passwordLen;
1887 r = LUKS_set_key(cd->device, keyslot, passphrase, passphrase_size,
1888 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1890 crypt_safe_free(new_password);
1891 crypt_free_volume_key(vk);
1892 return (r < 0) ? r : keyslot;
1895 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1897 crypt_keyslot_info ki;
1899 log_dbg("Destroying keyslot %d.", keyslot);
1901 if (!isLUKS(cd->type)) {
1902 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1906 ki = crypt_keyslot_status(cd, keyslot);
1907 if (ki == CRYPT_SLOT_INVALID) {
1908 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1912 if (ki == CRYPT_SLOT_INACTIVE) {
1913 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1917 return LUKS_del_key(cd->device, keyslot, &cd->hdr, cd);
1920 // activation/deactivation of device mapping
1921 int crypt_activate_by_passphrase(struct crypt_device *cd,
1924 const char *passphrase,
1925 size_t passphrase_size,
1928 crypt_status_info ci;
1929 struct volume_key *vk = NULL;
1930 char *read_passphrase = NULL;
1931 size_t passphraseLen = 0;
1934 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1935 name ? "Activating" : "Checking", name ?: "",
1936 keyslot, passphrase ? "" : "[none] ");
1939 ci = crypt_status(NULL, name);
1940 if (ci == CRYPT_INVALID)
1942 else if (ci >= CRYPT_ACTIVE) {
1943 log_err(cd, _("Device %s already exists.\n"), name);
1948 /* plain, use hashed passphrase */
1949 if (isPLAIN(cd->type)) {
1951 r = key_from_terminal(cd, NULL, &read_passphrase,
1955 passphrase = read_passphrase;
1956 passphrase_size = passphraseLen;
1958 r = create_device_helper(cd, name, cd->plain_hdr.hash,
1959 cd->plain_cipher, cd->plain_cipher_mode,
1960 NULL, passphrase, passphrase_size,
1961 cd->volume_key->keylength, cd->plain_hdr.size,
1962 cd->plain_hdr.skip, cd->plain_hdr.offset,
1963 cd->plain_uuid, flags, 0);
1965 } else if (isLUKS(cd->type)) {
1966 /* provided passphrase, do not retry */
1968 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1969 passphrase_size, &cd->hdr, &vk, cd);
1971 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1976 r = open_from_hdr_and_vk(cd, vk, name, flags);
1981 crypt_safe_free(read_passphrase);
1982 crypt_free_volume_key(vk);
1984 return r < 0 ? r : keyslot;
1987 int crypt_activate_by_keyfile(struct crypt_device *cd,
1990 const char *keyfile,
1991 size_t keyfile_size,
1994 crypt_status_info ci;
1995 struct volume_key *vk = NULL;
1996 char *passphrase_read = NULL;
1997 size_t passphrase_size_read;
1998 unsigned int key_count = 0;
2001 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
2002 name ?: "", keyslot, keyfile ?: "[none]");
2005 ci = crypt_status(NULL, name);
2006 if (ci == CRYPT_INVALID)
2008 else if (ci >= CRYPT_ACTIVE) {
2009 log_err(cd, _("Device %s already exists.\n"), name);
2017 if (isPLAIN(cd->type)) {
2018 r = key_from_file(cd, _("Enter passphrase: "),
2019 &passphrase_read, &passphrase_size_read,
2020 keyfile, keyfile_size);
2023 r = create_device_helper(cd, name, cd->plain_hdr.hash,
2024 cd->plain_cipher, cd->plain_cipher_mode,
2025 NULL, passphrase_read, passphrase_size_read,
2026 cd->volume_key->keylength, cd->plain_hdr.size,
2027 cd->plain_hdr.skip, cd->plain_hdr.offset,
2028 cd->plain_uuid, flags, 0);
2029 } else if (isLUKS(cd->type)) {
2030 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
2031 &passphrase_size_read, keyfile, keyfile_size);
2034 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
2035 passphrase_size_read, &cd->hdr, &vk, cd);
2041 r = open_from_hdr_and_vk(cd, vk, name, flags);
2046 } else if (isLOOPAES(cd->type)) {
2047 r = key_from_file(cd, NULL, &passphrase_read, &passphrase_size_read,
2048 keyfile, keyfile_size);
2051 r = LOOPAES_parse_keyfile(cd, &vk, cd->loopaes_hdr.hash, &key_count,
2052 passphrase_read, passphrase_size_read);
2056 r = LOOPAES_activate(cd, name, cd->loopaes_cipher,
2058 cd->loopaes_hdr.offset,
2059 cd->loopaes_hdr.skip,
2065 crypt_safe_free(passphrase_read);
2066 crypt_free_volume_key(vk);
2071 int crypt_activate_by_volume_key(struct crypt_device *cd,
2073 const char *volume_key,
2074 size_t volume_key_size,
2077 crypt_status_info ci;
2078 struct volume_key *vk;
2081 log_dbg("Activating volume %s by volume key.", name);
2083 /* use key directly, no hash */
2084 if (isPLAIN(cd->type)) {
2085 if (!volume_key || !volume_key_size || !cd->volume_key ||
2086 volume_key_size != cd->volume_key->keylength) {
2087 log_err(cd, _("Incorrect volume key specified for plain device.\n"));
2091 return create_device_helper(cd, name, NULL,
2092 cd->plain_cipher, cd->plain_cipher_mode, NULL, volume_key, volume_key_size,
2093 cd->volume_key->keylength, cd->plain_hdr.size, cd->plain_hdr.skip,
2094 cd->plain_hdr.offset, cd->plain_uuid, flags, 0);
2097 if (!isLUKS(cd->type)) {
2098 log_err(cd, _("Device type is not properly initialised.\n"));
2103 ci = crypt_status(NULL, name);
2104 if (ci == CRYPT_INVALID)
2106 else if (ci >= CRYPT_ACTIVE) {
2107 log_err(cd, _("Device %s already exists.\n"), name);
2112 /* If key is not provided, try to use internal key */
2114 if (!cd->volume_key) {
2115 log_err(cd, _("Volume key does not match the volume.\n"));
2118 volume_key_size = cd->volume_key->keylength;
2119 volume_key = cd->volume_key->key;
2122 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2125 r = LUKS_verify_volume_key(&cd->hdr, vk);
2128 log_err(cd, _("Volume key does not match the volume.\n"));
2131 r = open_from_hdr_and_vk(cd, vk, name, flags);
2133 crypt_free_volume_key(vk);
2138 int crypt_deactivate(struct crypt_device *cd, const char *name)
2145 log_dbg("Deactivating volume %s.", name);
2147 if (!cd && dm_init(NULL, 1) < 0)
2150 switch (crypt_status(cd, name)) {
2152 r = dm_remove_device(name, 0, 0);
2155 log_err(cd, _("Device %s is busy.\n"), name);
2158 case CRYPT_INACTIVE:
2159 log_err(cd, _("Device %s is not active.\n"), name);
2163 log_err(cd, _("Invalid device %s.\n"), name);
2173 int crypt_volume_key_get(struct crypt_device *cd,
2176 size_t *volume_key_size,
2177 const char *passphrase,
2178 size_t passphrase_size)
2180 struct volume_key *vk;
2181 char *processed_key = NULL;
2185 key_len = crypt_get_volume_key_size(cd);
2186 if (key_len > *volume_key_size) {
2187 log_err(cd, _("Volume key buffer too small.\n"));
2191 if (isPLAIN(cd->type) && cd->plain_hdr.hash) {
2192 processed_key = process_key(cd, cd->plain_hdr.hash, NULL, key_len,
2193 passphrase, passphrase_size);
2194 if (!processed_key) {
2195 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
2198 memcpy(volume_key, processed_key, key_len);
2199 *volume_key_size = key_len;
2200 crypt_safe_free(processed_key);
2204 if (isLUKS(cd->type)) {
2205 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
2206 passphrase_size, &cd->hdr, &vk, cd);
2209 memcpy(volume_key, vk->key, vk->keylength);
2210 *volume_key_size = vk->keylength;
2213 crypt_free_volume_key(vk);
2217 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
2221 int crypt_volume_key_verify(struct crypt_device *cd,
2222 const char *volume_key,
2223 size_t volume_key_size)
2225 struct volume_key *vk;
2228 if (!isLUKS(cd->type)) {
2229 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2233 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2237 r = LUKS_verify_volume_key(&cd->hdr, vk);
2240 log_err(cd, _("Volume key does not match the volume.\n"));
2242 crypt_free_volume_key(vk);
2247 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
2249 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
2250 cd->timeout = timeout_sec;
2253 void crypt_set_password_retry(struct crypt_device *cd, int tries)
2255 log_dbg("Password retry count set to %d.", tries);
2259 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2261 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
2262 cd->iteration_time = iteration_time_ms;
2265 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
2267 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
2268 cd->password_verify = password_verify ? 1 : 0;
2271 void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
2274 case CRYPT_RNG_URANDOM:
2275 case CRYPT_RNG_RANDOM:
2276 log_dbg("RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
2277 cd->rng_type = rng_type;
2281 int crypt_get_rng_type(struct crypt_device *cd)
2286 return cd->rng_type;
2289 int crypt_memory_lock(struct crypt_device *cd, int lock)
2291 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
2295 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
2299 if (!cd && dm_init(NULL, 1) < 0)
2300 return CRYPT_INVALID;
2302 r = dm_status_device(name);
2307 if (r < 0 && r != -ENODEV)
2308 return CRYPT_INVALID;
2311 return CRYPT_ACTIVE;
2316 return CRYPT_INACTIVE;
2319 static void hexprintICB(struct crypt_device *cd, char *d, int n)
2322 for(i = 0; i < n; i++)
2323 log_std(cd, "%02hhx ", (char)d[i]);
2326 int crypt_dump(struct crypt_device *cd)
2329 if (!isLUKS(cd->type)) { //FIXME
2330 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2334 log_std(cd, "LUKS header information for %s\n\n", cd->device);
2335 log_std(cd, "Version: \t%d\n", cd->hdr.version);
2336 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
2337 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
2338 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec);
2339 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
2340 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
2341 log_std(cd, "MK digest: \t");
2342 hexprintICB(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE);
2344 log_std(cd, "MK salt: \t");
2345 hexprintICB(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2);
2346 log_std(cd, "\n \t");
2347 hexprintICB(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
2349 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
2350 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
2351 for(i = 0; i < LUKS_NUMKEYS; i++) {
2352 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
2353 log_std(cd, "Key Slot %d: ENABLED\n",i);
2354 log_std(cd, "\tIterations: \t%d\n",
2355 cd->hdr.keyblock[i].passwordIterations);
2356 log_std(cd, "\tSalt: \t");
2357 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt,
2359 log_std(cd, "\n\t \t");
2360 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt +
2361 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
2364 log_std(cd, "\tKey material offset:\t%d\n",
2365 cd->hdr.keyblock[i].keyMaterialOffset);
2366 log_std(cd, "\tAF stripes: \t%d\n",
2367 cd->hdr.keyblock[i].stripes);
2370 log_std(cd, "Key Slot %d: DISABLED\n", i);
2376 const char *crypt_get_cipher(struct crypt_device *cd)
2378 if (isPLAIN(cd->type))
2379 return cd->plain_cipher;
2381 if (isLUKS(cd->type))
2382 return cd->hdr.cipherName;
2384 if (isLOOPAES(cd->type))
2385 return cd->loopaes_cipher;
2390 const char *crypt_get_cipher_mode(struct crypt_device *cd)
2392 if (isPLAIN(cd->type))
2393 return cd->plain_cipher_mode;
2395 if (isLUKS(cd->type))
2396 return cd->hdr.cipherMode;
2398 if (isLOOPAES(cd->type))
2399 return cd->loopaes_cipher_mode;
2404 const char *crypt_get_uuid(struct crypt_device *cd)
2406 if (isLUKS(cd->type))
2407 return cd->hdr.uuid;
2409 if (isPLAIN(cd->type))
2410 return cd->plain_uuid;
2412 if (isLOOPAES(cd->type))
2413 return cd->loopaes_uuid;
2418 const char *crypt_get_device_name(struct crypt_device *cd)
2423 int crypt_get_volume_key_size(struct crypt_device *cd)
2425 if (isPLAIN(cd->type) && cd->volume_key)
2426 return cd->volume_key->keylength;
2428 if (isLUKS(cd->type))
2429 return cd->hdr.keyBytes;
2431 if (isLOOPAES(cd->type))
2432 return cd->loopaes_key_size;
2437 uint64_t crypt_get_data_offset(struct crypt_device *cd)
2439 if (isPLAIN(cd->type))
2440 return cd->plain_hdr.offset;
2442 if (isLUKS(cd->type))
2443 return cd->hdr.payloadOffset;
2445 if (isLOOPAES(cd->type))
2446 return cd->loopaes_hdr.offset;
2451 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
2453 if (!isLUKS(cd->type)) {
2454 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2455 return CRYPT_SLOT_INVALID;
2458 return LUKS_keyslot_info(&cd->hdr, keyslot);
2461 int crypt_keyslot_max(const char *type)
2463 if (type && isLUKS(type))
2464 return LUKS_NUMKEYS;
2469 const char *crypt_get_type(struct crypt_device *cd)
2474 int crypt_get_active_device(struct crypt_device *cd __attribute__((unused)),
2476 struct crypt_active_device *cad)
2480 r = dm_query_device(name, NULL, &cad->size, &cad->iv_offset, &cad->offset,
2481 NULL, NULL, NULL, &readonly, NULL, NULL);
2485 cad->flags = readonly ? CRYPT_ACTIVATE_READONLY : 0;