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,
363 crypt_status_info ci;
364 char *dm_cipher = NULL;
365 char *processed_key = NULL;
371 ci = crypt_status(cd, name);
372 if (ci == CRYPT_INVALID)
375 if (reload && ci < CRYPT_ACTIVE)
378 if (!reload && ci >= CRYPT_ACTIVE) {
379 log_err(cd, _("Device %s already exists.\n"), name);
383 if (key_size > 1024) {
384 log_err(cd, _("Invalid key size %d.\n"), (int)key_size);
388 r = device_check_and_adjust(cd, cd->device, !reload, &size, &offset, &read_only);
392 if (cipher_mode && asprintf(&dm_cipher, "%s-%s", cipher, cipher_mode) < 0)
395 processed_key = process_key(cd, hash, key_file, key_size, passphrase, passphrase_size);
396 if (!processed_key) {
401 r = dm_create_device(name, cd->device, dm_cipher ?: cipher, cd->type, uuid, size, skip, offset,
402 key_size, processed_key, read_only, reload);
404 if (isPLAIN(cd->type) && !uuid)
405 (void)dm_query_device(name, NULL, NULL, NULL, NULL, NULL, NULL,
406 NULL, NULL, NULL, &cd->plain_uuid);
409 crypt_safe_free(processed_key);
413 static int open_from_hdr_and_vk(struct crypt_device *cd,
414 struct volume_key *vk,
418 uint64_t size, offset;
420 int read_only, no_uuid, r;
423 offset = crypt_get_data_offset(cd);
424 read_only = flags & CRYPT_ACTIVATE_READONLY;
425 no_uuid = flags & CRYPT_ACTIVATE_NO_UUID;
427 r = device_check_and_adjust(cd, cd->device, 1, &size, &offset, &read_only);
431 if (asprintf(&cipher, "%s-%s", crypt_get_cipher(cd),
432 crypt_get_cipher_mode(cd)) < 0)
435 r = dm_create_device(name, cd->device, cipher, cd->type,
436 no_uuid ? NULL : crypt_get_uuid(cd),
437 size, 0, offset, vk->keylength, vk->key,
443 static void log_wrapper(int level, const char *msg, void *usrptr)
445 void (*xlog)(int level, char *msg) = usrptr;
446 xlog(level, (char *)msg);
449 static int yesDialog_wrapper(const char *msg, void *usrptr)
451 int (*xyesDialog)(char *msg) = usrptr;
452 return xyesDialog((char*)msg);
455 int crypt_confirm(struct crypt_device *cd, const char *msg)
457 if (!cd || !cd->confirm)
460 return cd->confirm(msg, cd->confirm_usrptr);
463 static int key_from_terminal(struct crypt_device *cd, char *msg, char **key,
464 size_t *key_len, int force_verify)
470 if(!msg && asprintf(&prompt, _("Enter passphrase for %s: "),
471 cd->backing_file ?: cd->device) < 0)
478 *key = crypt_safe_alloc(DEFAULT_PASSPHRASE_SIZE_MAX);
483 r = cd->password(msg, *key, DEFAULT_PASSPHRASE_SIZE_MAX,
484 cd->password_usrptr);
486 crypt_safe_free(*key);
491 r = crypt_get_key(msg, key, key_len, 0, NULL, cd->timeout,
492 (force_verify || cd->password_verify), cd);
495 return (r < 0) ? r: 0;
498 static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslot,
499 struct volume_key **vk)
501 char *passphrase_read = NULL;
502 size_t passphrase_size_read;
503 int r = -EINVAL, eperm = 0, tries = cd->tries;
507 crypt_free_volume_key(*vk);
510 r = key_from_terminal(cd, NULL, &passphrase_read,
511 &passphrase_size_read, 0);
515 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
516 passphrase_size_read, &cd->hdr, vk, cd);
519 crypt_safe_free(passphrase_read);
520 passphrase_read = NULL;
521 } while (r == -EPERM && (--tries > 0));
524 crypt_free_volume_key(*vk);
527 /* Report wrong passphrase if at least one try failed */
528 if (eperm && r == -EPIPE)
532 crypt_safe_free(passphrase_read);
536 static int key_from_file(struct crypt_device *cd, char *msg,
537 char **key, size_t *key_len,
538 const char *key_file, size_t key_size)
540 return crypt_get_key(msg, key, key_len, key_size, key_file,
544 static int _crypt_init(struct crypt_device **cd,
546 struct crypt_options *options,
547 int load, int need_dm)
551 /* if it is plain device and mapping table is being reloaded
552 initialize it by name*/
553 init_by_name = (type && !strcmp(type, CRYPT_PLAIN) && load);
555 /* Some of old API calls do not require DM in kernel,
556 fake initialisation by initialise it with kernel_check disabled */
558 (void)dm_init(NULL, 0);
560 r = crypt_init_by_name(cd, options->name);
562 r = crypt_init(cd, options->device);
569 crypt_set_log_callback(*cd, log_wrapper, options->icb->log);
570 crypt_set_confirm_callback(*cd, yesDialog_wrapper, options->icb->yesDialog);
572 crypt_set_timeout(*cd, options->timeout);
573 crypt_set_password_retry(*cd, options->tries);
574 crypt_set_iterarion_time(*cd, options->iteration_time ?: 1000);
575 crypt_set_password_verify(*cd, options->flags & CRYPT_FLAG_VERIFY);
577 if (load && !init_by_name)
578 r = crypt_load(*cd, type, NULL);
580 if (!r && type && !(*cd)->type) {
581 (*cd)->type = strdup(type);
592 void crypt_set_log_callback(struct crypt_device *cd,
593 void (*log)(int level, const char *msg, void *usrptr),
600 cd->log_usrptr = usrptr;
604 void crypt_set_confirm_callback(struct crypt_device *cd,
605 int (*confirm)(const char *msg, void *usrptr),
608 cd->confirm = confirm;
609 cd->confirm_usrptr = usrptr;
612 void crypt_set_password_callback(struct crypt_device *cd,
613 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
616 cd->password = password;
617 cd->password_usrptr = usrptr;
620 /* OPTIONS: name, cipher, device, hash, key_file, key_size, key_slot,
621 * offset, size, skip, timeout, tries, passphrase_fd (ignored),
623 static int crypt_create_and_update_device(struct crypt_options *options, int update)
625 struct crypt_device *cd = NULL;
626 char *passphrase = NULL;
627 size_t passphrase_size = 0;
630 r = _crypt_init(&cd, CRYPT_PLAIN, options, 0, 1);
634 r = crypt_get_key(_("Enter passphrase: "), &passphrase, &passphrase_size,
635 options->key_size, options->key_file,
636 cd->timeout, cd->password_verify, cd);
638 r = create_device_helper(cd, options->name, options->hash,
639 options->cipher, NULL, options->key_file,
640 passphrase, passphrase_size,
641 options->key_size, options->size, options->skip,
642 options->offset, NULL, options->flags & CRYPT_FLAG_READONLY,
645 crypt_safe_free(passphrase);
650 int crypt_create_device(struct crypt_options *options)
652 return crypt_create_and_update_device(options, 0);
655 int crypt_update_device(struct crypt_options *options)
657 return crypt_create_and_update_device(options, 1);
660 /* OPTIONS: name, size, icb */
661 int crypt_resize_device(struct crypt_options *options)
663 struct crypt_device *cd = NULL;
664 char *device = NULL, *cipher = NULL, *uuid = NULL, *key = NULL;
665 const char *type = NULL;
666 uint64_t size, skip, offset;
667 int key_size, read_only, r;
669 log_dbg("Resizing device %s to %" PRIu64 " sectors.", options->name, options->size);
671 if (dm_init(NULL, 1) < 0)
674 r = dm_query_device(options->name, &device, &size, &skip, &offset,
675 &cipher, &key_size, &key, &read_only, NULL, &uuid);
677 log_err(NULL, _("Device %s is not active.\n"), options->name);
681 /* Try to determine type of device from UUID */
684 if (!strncmp(uuid, CRYPT_PLAIN, strlen(CRYPT_PLAIN))) {
688 } else if (!strncmp(uuid, CRYPT_LUKS1, strlen(CRYPT_LUKS1)))
692 if (!options->device)
693 options->device = device;
695 r = _crypt_init(&cd, type, options, 1, 1);
699 size = options->size;
700 r = device_check_and_adjust(cd, device, 0, &size, &offset, &read_only);
704 r = dm_create_device(options->name, device, cipher, type,
705 crypt_get_uuid(cd), size, skip, offset,
706 key_size, key, read_only, 1);
708 crypt_safe_free(key);
710 if (options->device == device)
711 options->device = NULL;
719 /* OPTIONS: name, icb */
720 int crypt_query_device(struct crypt_options *options)
724 log_dbg("Query device %s.", options->name);
726 if (dm_init(NULL, 1) < 0)
729 r = dm_status_device(options->name);
733 r = dm_query_device(options->name, (char **)&options->device, &options->size,
734 &options->skip, &options->offset, (char **)&options->cipher,
735 &options->key_size, NULL, &read_only, NULL, NULL);
738 options->flags |= CRYPT_FLAG_READONLY;
740 options->flags |= CRYPT_FLAG_FREE_DEVICE;
741 options->flags |= CRYPT_FLAG_FREE_CIPHER;
753 /* OPTIONS: name, icb */
754 int crypt_remove_device(struct crypt_options *options)
756 struct crypt_device *cd = NULL;
759 r = crypt_init_by_name(&cd, options->name);
761 r = crypt_deactivate(cd, options->name);
768 /* OPTIONS: device, cipher, hash, align_payload, key_size (master key), key_slot
769 * new_key_file, iteration_time, timeout, flags, icb */
770 int crypt_luksFormat(struct crypt_options *options)
772 char cipherName[LUKS_CIPHERNAME_L];
773 char cipherMode[LUKS_CIPHERMODE_L];
776 struct crypt_device *cd = NULL;
777 struct crypt_params_luks1 cp = {
778 .hash = options->hash,
779 .data_alignment = options->align_payload
783 r = crypt_parse_name_and_mode(options->cipher, cipherName, NULL, cipherMode);
785 log_err(cd, _("No known cipher specification pattern detected.\n"));
789 if ((r = _crypt_init(&cd, CRYPT_LUKS1, options, 0, 1)))
792 if (options->key_slot >= LUKS_NUMKEYS && options->key_slot != CRYPT_ANY_SLOT) {
793 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
794 options->key_slot, LUKS_NUMKEYS - 1);
799 r = crypt_get_key(_("Enter LUKS passphrase: "), &password, &passwordLen, 0,
800 options->new_key_file, cd->timeout, cd->password_verify, cd);
805 r = crypt_format(cd, CRYPT_LUKS1, cipherName, cipherMode,
806 NULL, NULL, options->key_size, &cp);
810 /* Add keyslot using internally stored volume key generated during format */
811 r = crypt_keyslot_add_by_volume_key(cd, options->key_slot, NULL, 0,
812 password, passwordLen);
815 crypt_safe_free(password);
816 return (r < 0) ? r : 0;
819 /* OPTIONS: name, device, key_size, key_file, timeout, tries, flags, icb */
820 int crypt_luksOpen(struct crypt_options *options)
822 struct crypt_device *cd = NULL;
829 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
833 if (options->flags & CRYPT_FLAG_READONLY)
834 flags |= CRYPT_ACTIVATE_READONLY;
836 if (options->flags & CRYPT_FLAG_NON_EXCLUSIVE_ACCESS)
837 flags |= CRYPT_ACTIVATE_NO_UUID;
839 if (options->key_file)
840 r = crypt_activate_by_keyfile(cd, options->name,
841 CRYPT_ANY_SLOT, options->key_file, 0,
844 r = crypt_activate_by_passphrase(cd, options->name,
845 CRYPT_ANY_SLOT, options->passphrase,
846 options->passphrase ? strlen(options->passphrase) : 0,
850 return (r < 0) ? r : 0;
853 /* OPTIONS: device, keys_slot, key_file, timeout, flags, icb */
854 int crypt_luksKillSlot(struct crypt_options *options)
856 struct crypt_device *cd = NULL;
859 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
863 r = luks_remove_helper(cd, options->key_slot, options->key_file, NULL,
864 options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
867 return (r < 0) ? r : 0;
870 /* OPTIONS: device, new_key_file, key_file, timeout, flags, icb */
871 int crypt_luksRemoveKey(struct crypt_options *options)
873 struct crypt_device *cd = NULL;
876 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
880 r = luks_remove_helper(cd, CRYPT_ANY_SLOT, options->key_file, options->new_key_file,
881 options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
884 return (r < 0) ? r : 0;
888 /* OPTIONS: device, new_key_file, key_file, key_slot, flags,
889 iteration_time, timeout, icb */
890 int crypt_luksAddKey(struct crypt_options *options)
892 struct crypt_device *cd = NULL;
895 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
899 if (options->key_file || options->new_key_file)
900 r = crypt_keyslot_add_by_keyfile(cd, options->key_slot,
901 options->key_file, 0,
902 options->new_key_file, 0);
904 r = crypt_keyslot_add_by_passphrase(cd, options->key_slot,
908 return (r < 0) ? r : 0;
911 /* OPTIONS: device, icb */
912 int crypt_luksUUID(struct crypt_options *options)
914 struct crypt_device *cd = NULL;
918 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
922 uuid = crypt_get_uuid(cd);
923 log_std(cd, "%s\n", uuid ?: "");
928 /* OPTIONS: device, icb */
929 int crypt_isLuks(struct crypt_options *options)
931 struct crypt_device *cd = NULL;
934 log_dbg("Check device %s for LUKS header.", options->device);
940 r = crypt_init(&cd, options->device);
944 /* Do print fail here, no need to crypt_load() */
945 r = LUKS_read_phdr(cd->device, &cd->hdr, 0, cd) ? -EINVAL : 0;
951 /* OPTIONS: device, icb */
952 int crypt_luksDump(struct crypt_options *options)
954 struct crypt_device *cd = NULL;
957 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
967 void crypt_get_error(char *buf, size_t size)
969 const char *error = get_error();
971 if (!buf || size < 1)
974 strncpy(buf, error, size - 1);
975 buf[size - 1] = '\0';
981 void crypt_put_options(struct crypt_options *options)
983 if (options->flags & CRYPT_FLAG_FREE_DEVICE) {
984 free((char *)options->device);
985 options->device = NULL;
986 options->flags &= ~CRYPT_FLAG_FREE_DEVICE;
988 if (options->flags & CRYPT_FLAG_FREE_CIPHER) {
989 free((char *)options->cipher);
990 options->cipher = NULL;
991 options->flags &= ~CRYPT_FLAG_FREE_CIPHER;
995 const char *crypt_get_dir(void)
1000 /////////////////////////////////
1005 int crypt_init(struct crypt_device **cd, const char *device)
1007 struct crypt_device *h = NULL;
1008 int r, readonly = 0;
1013 log_dbg("Allocating crypt device %s context.", device);
1015 if (!(h = malloc(sizeof(struct crypt_device))))
1018 memset(h, 0, sizeof(*h));
1022 r = device_ready(NULL, device, O_RDONLY);
1023 if (r == -ENOTBLK) {
1024 h->device = crypt_loop_get_device();
1025 log_dbg("Not a block device, %s%s.",
1026 h->device ? "using free loop device " :
1027 "no free loop device found",
1030 log_err(NULL, _("Cannot find a free loopback device.\n"));
1035 /* Keep the loop open, dettached on last close. */
1036 h->loop_fd = crypt_loop_attach(h->device, device, 0, 1, &readonly);
1037 if (h->loop_fd == -1) {
1038 log_err(NULL, _("Attaching loopback device failed "
1039 "(loop device with autoclear flag is required).\n"));
1044 h->backing_file = crypt_loop_backing_file(h->device);
1045 r = device_ready(NULL, h->device, O_RDONLY);
1053 if (!h->device && device && !(h->device = strdup(device))) {
1058 if (dm_init(h, 1) < 0) {
1063 h->iteration_time = 1000;
1064 h->password_verify = 0;
1066 h->rng_type = crypt_random_default_key_rng();
1072 if (h->loop_fd != -1)
1075 free(h->backing_file);
1081 int crypt_init_by_name(struct crypt_device **cd, const char *name)
1083 crypt_status_info ci;
1084 struct crypt_active_device cad;
1085 char *device = NULL, *cipher_full = NULL, *device_uuid = NULL;
1086 char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
1088 int key_size = 0, key_nums, r;
1091 log_dbg("Allocating crypt device context by device %s.", name);
1093 ci = crypt_status(NULL, name);
1094 if (ci == CRYPT_INVALID)
1097 if (ci < CRYPT_ACTIVE) {
1098 log_err(NULL, _("Device %s is not active.\n"), name);
1102 r = dm_query_device(name, &device, &cad.size, &cad.iv_offset, &cad.offset,
1103 &cipher_full, &key_size, &key, NULL, NULL,
1109 r = crypt_init(cd, device);
1111 /* Underlying device disappeared but mapping still active */
1112 if (!device || r == -ENOTBLK)
1113 log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
1116 /* Underlying device is not readable but crypt mapping exists */
1117 if (r == -ENOTBLK) {
1120 r = crypt_init(cd, NULL);
1126 /* Try to initialise basic parameters from active device */
1128 if (!(*cd)->backing_file && device && crypt_loop_device(device) &&
1129 !((*cd)->backing_file = crypt_loop_backing_file(device))) {
1135 if (!strncmp(CRYPT_PLAIN, device_uuid, sizeof(CRYPT_PLAIN)-1)) {
1136 (*cd)->type = strdup(CRYPT_PLAIN);
1137 (*cd)->plain_uuid = strdup(device_uuid);
1138 (*cd)->plain_hdr.hash = NULL; /* no way to get this */
1139 (*cd)->plain_hdr.offset = cad.offset;
1140 (*cd)->plain_hdr.skip = cad.iv_offset;
1141 (*cd)->volume_key = crypt_alloc_volume_key(key_size, key);
1142 if (!(*cd)->volume_key) {
1147 r = crypt_parse_name_and_mode(cipher_full, cipher, NULL, cipher_mode);
1149 (*cd)->plain_cipher = strdup(cipher);
1150 (*cd)->plain_cipher_mode = strdup(cipher_mode);
1152 } else if (!strncmp(CRYPT_LOOPAES, device_uuid, sizeof(CRYPT_LOOPAES)-1)) {
1153 (*cd)->type = strdup(CRYPT_LOOPAES);
1154 (*cd)->loopaes_uuid = strdup(device_uuid);
1155 (*cd)->loopaes_hdr.offset = cad.offset;
1157 r = crypt_parse_name_and_mode(cipher_full, cipher,
1158 &key_nums, cipher_mode);
1160 (*cd)->loopaes_cipher = strdup(cipher);
1161 (*cd)->loopaes_cipher_mode = strdup(cipher_mode);
1162 /* version 3 uses last key for IV */
1163 if (key_size % key_nums)
1165 (*cd)->loopaes_key_size = key_size / key_nums;
1167 } else if (!strncmp(CRYPT_LUKS1, device_uuid, sizeof(CRYPT_LUKS1)-1)) {
1169 if (crypt_load(*cd, CRYPT_LUKS1, NULL) < 0 ||
1170 crypt_volume_key_verify(*cd, key, key_size) < 0) {
1171 log_dbg("LUKS device header does not match active device.");
1175 (*cd)->volume_key = crypt_alloc_volume_key(key_size, key);
1176 if (!(*cd)->volume_key) {
1183 log_dbg("Active device has no UUID set, some parameters are not set.");
1190 crypt_safe_free(key);
1197 static int _crypt_format_plain(struct crypt_device *cd,
1199 const char *cipher_mode,
1201 size_t volume_key_size,
1202 struct crypt_params_plain *params)
1204 if (!cipher || !cipher_mode) {
1205 log_err(cd, _("Invalid plain crypt parameters.\n"));
1209 if (volume_key_size > 1024) {
1210 log_err(cd, _("Invalid key size.\n"));
1214 cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
1215 if (!cd->volume_key)
1218 cd->plain_cipher = strdup(cipher);
1219 cd->plain_cipher_mode = strdup(cipher_mode);
1222 cd->plain_uuid = strdup(uuid);
1224 if (params && params->hash)
1225 cd->plain_hdr.hash = strdup(params->hash);
1227 cd->plain_hdr.offset = params ? params->offset : 0;
1228 cd->plain_hdr.skip = params ? params->skip : 0;
1230 if (!cd->plain_cipher || !cd->plain_cipher_mode)
1236 static int _crypt_format_luks1(struct crypt_device *cd,
1238 const char *cipher_mode,
1240 const char *volume_key,
1241 size_t volume_key_size,
1242 struct crypt_params_luks1 *params)
1245 unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
1246 unsigned long alignment_offset = 0;
1249 log_err(cd, _("Can't format LUKS without device.\n"));
1254 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
1257 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
1262 if (params && params->data_alignment)
1263 required_alignment = params->data_alignment * SECTOR_SIZE;
1265 get_topology_alignment(cd->device, &required_alignment,
1266 &alignment_offset, DEFAULT_DISK_ALIGNMENT);
1268 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
1269 (params && params->hash) ? params->hash : "sha1",
1271 required_alignment / SECTOR_SIZE,
1272 alignment_offset / SECTOR_SIZE,
1273 cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1277 /* Wipe first 8 sectors - fs magic numbers etc. */
1278 r = wipe_device_header(cd->device, 8);
1281 log_err(cd, _("Cannot format device %s which is still in use.\n"),
1284 log_err(cd, _("Cannot wipe header on device %s.\n"),
1290 r = LUKS_write_phdr(cd->device, &cd->hdr, cd);
1295 static int _crypt_format_loopaes(struct crypt_device *cd,
1298 size_t volume_key_size,
1299 struct crypt_params_loopaes *params)
1302 log_err(cd, _("Can't format LOOPAES without device.\n"));
1306 if (volume_key_size > 1024) {
1307 log_err(cd, _("Invalid key size.\n"));
1311 cd->loopaes_key_size = volume_key_size;
1313 cd->loopaes_cipher = strdup(cipher ?: DEFAULT_LOOPAES_CIPHER);
1316 cd->loopaes_uuid = strdup(uuid);
1318 if (params && params->hash)
1319 cd->loopaes_hdr.hash = strdup(params->hash);
1321 cd->loopaes_hdr.offset = params ? params->offset : 0;
1322 cd->loopaes_hdr.skip = params ? params->skip : 0;
1327 int crypt_format(struct crypt_device *cd,
1330 const char *cipher_mode,
1332 const char *volume_key,
1333 size_t volume_key_size,
1341 log_dbg("Formatting device %s as type %s.", cd->device ?: "(none)", type);
1343 r = init_crypto(cd);
1348 r = _crypt_format_plain(cd, cipher, cipher_mode,
1349 uuid, volume_key_size, params);
1350 else if (isLUKS(type))
1351 r = _crypt_format_luks1(cd, cipher, cipher_mode,
1352 uuid, volume_key, volume_key_size, params);
1353 else if (isLOOPAES(type))
1354 r = _crypt_format_loopaes(cd, cipher, uuid, volume_key_size, params);
1356 /* FIXME: allow plugins here? */
1357 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
1361 if (!r && !(cd->type = strdup(type)))
1365 crypt_free_volume_key(cd->volume_key);
1366 cd->volume_key = NULL;
1372 int crypt_load(struct crypt_device *cd,
1373 const char *requested_type,
1374 void *params __attribute__((unused)))
1376 struct luks_phdr hdr;
1379 log_dbg("Trying to load %s crypt type from device %s.",
1380 requested_type ?: "any", cd->device ?: "(none)");
1385 if (requested_type && !isLUKS(requested_type))
1388 r = init_crypto(cd);
1392 r = LUKS_read_phdr(cd->device, &hdr, 1, cd);
1395 memcpy(&cd->hdr, &hdr, sizeof(hdr));
1396 cd->type = strdup(CRYPT_LUKS1);
1404 int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
1406 char *device = NULL, *cipher = NULL, *uuid = NULL, *key = NULL;
1407 uint64_t size, skip, offset;
1408 int key_size, read_only, r;
1410 /* Device context type must be initialised */
1411 if (!cd->type || !crypt_get_uuid(cd))
1414 log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
1416 r = dm_query_device(name, &device, &size, &skip, &offset,
1417 &cipher, &key_size, &key, &read_only, NULL, &uuid);
1419 log_err(NULL, _("Device %s is not active.\n"), name);
1428 r = device_check_and_adjust(cd, device, 0, &new_size, &offset, &read_only);
1432 if (new_size == size) {
1433 log_dbg("Device has already requested size %" PRIu64
1439 r = dm_create_device(name, device, cipher, cd->type,
1440 crypt_get_uuid(cd), new_size, skip, offset,
1441 key_size, key, read_only, 1);
1443 crypt_safe_free(key);
1451 int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
1453 if (!isLUKS(cd->type)) {
1454 log_err(cd, _("This operation is not supported for this device type.\n"));
1458 if (uuid && !strncmp(uuid, cd->hdr.uuid, sizeof(cd->hdr.uuid))) {
1459 log_dbg("UUID is the same as requested (%s) for device %s.",
1465 log_dbg("Requested new UUID change to %s for %s.", uuid, cd->device);
1467 log_dbg("Requested new UUID refresh for %s.", cd->device);
1469 if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
1472 return LUKS_hdr_uuid_set(cd->device, &cd->hdr, uuid, cd);
1475 int crypt_header_backup(struct crypt_device *cd,
1476 const char *requested_type,
1477 const char *backup_file)
1481 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1484 r = init_crypto(cd);
1488 log_dbg("Requested header backup of device %s (%s) to "
1489 "file %s.", cd->device, requested_type, backup_file);
1491 return LUKS_hdr_backup(backup_file, cd->device, &cd->hdr, cd);
1494 int crypt_header_restore(struct crypt_device *cd,
1495 const char *requested_type,
1496 const char *backup_file)
1500 if (requested_type && !isLUKS(requested_type))
1503 /* Some hash functions need initialized gcrypt library */
1504 r = init_crypto(cd);
1508 log_dbg("Requested header restore to device %s (%s) from "
1509 "file %s.", cd->device, requested_type, backup_file);
1511 return LUKS_hdr_restore(backup_file, cd->device, &cd->hdr, cd);
1514 void crypt_free(struct crypt_device *cd)
1517 log_dbg("Releasing crypt device %s context.", cd->device);
1519 if (cd->loop_fd != -1)
1523 crypt_free_volume_key(cd->volume_key);
1526 free(cd->backing_file);
1529 /* used in plain device only */
1530 free((char*)cd->plain_hdr.hash);
1531 free(cd->plain_cipher);
1532 free(cd->plain_cipher_mode);
1533 free(cd->plain_uuid);
1535 /* used in loop-AES device only */
1536 free((char*)cd->loopaes_hdr.hash);
1537 free(cd->loopaes_cipher);
1538 free(cd->loopaes_uuid);
1544 int crypt_suspend(struct crypt_device *cd,
1547 crypt_status_info ci;
1548 int r, suspended = 0;
1550 log_dbg("Suspending volume %s.", name);
1552 ci = crypt_status(NULL, name);
1553 if (ci < CRYPT_ACTIVE) {
1554 log_err(cd, _("Volume %s is not active.\n"), name);
1558 if (!cd && dm_init(NULL, 1) < 0)
1561 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1562 NULL, NULL, NULL, NULL, &suspended, NULL);
1567 log_err(cd, _("Volume %s is already suspended.\n"), name);
1572 r = dm_suspend_and_wipe_key(name);
1574 log_err(cd, "Suspend is not supported for device %s.\n", name);
1576 log_err(cd, "Error during suspending device %s.\n", name);
1583 int crypt_resume_by_passphrase(struct crypt_device *cd,
1586 const char *passphrase,
1587 size_t passphrase_size)
1589 struct volume_key *vk = NULL;
1590 int r, suspended = 0;
1592 log_dbg("Resuming volume %s.", name);
1594 if (!isLUKS(cd->type)) {
1595 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1600 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1601 NULL, NULL, NULL, NULL, &suspended, NULL);
1606 log_err(cd, _("Volume %s is not suspended.\n"), name);
1611 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1612 passphrase_size, &cd->hdr, &vk, cd);
1614 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1618 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1620 log_err(cd, "Resume is not supported for device %s.\n", name);
1622 log_err(cd, "Error during resuming device %s.\n", name);
1626 crypt_free_volume_key(vk);
1627 return r < 0 ? r : keyslot;
1630 int crypt_resume_by_keyfile(struct crypt_device *cd,
1633 const char *keyfile,
1634 size_t keyfile_size)
1636 struct volume_key *vk = NULL;
1637 char *passphrase_read = NULL;
1638 size_t passphrase_size_read;
1639 int r, suspended = 0;
1641 log_dbg("Resuming volume %s.", name);
1643 if (!isLUKS(cd->type)) {
1644 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1649 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1650 NULL, NULL, NULL, NULL, &suspended, NULL);
1655 log_err(cd, _("Volume %s is not suspended.\n"), name);
1662 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1663 &passphrase_size_read, keyfile, keyfile_size);
1667 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1668 passphrase_size_read, &cd->hdr, &vk, cd);
1673 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1675 log_err(cd, "Error during resuming device %s.\n", name);
1677 crypt_safe_free(passphrase_read);
1678 crypt_free_volume_key(vk);
1679 return r < 0 ? r : keyslot;
1682 // slot manipulation
1683 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1684 int keyslot, // -1 any
1685 const char *passphrase, // NULL -> terminal
1686 size_t passphrase_size,
1687 const char *new_passphrase, // NULL -> terminal
1688 size_t new_passphrase_size)
1690 struct volume_key *vk = NULL;
1691 char *password = NULL, *new_password = NULL;
1692 size_t passwordLen, new_passwordLen;
1695 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1696 "new passphrase %sprovided.",
1697 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1699 if (!isLUKS(cd->type)) {
1700 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1704 r = keyslot_verify_or_find_empty(cd, &keyslot);
1708 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1709 /* No slots used, try to use pre-generated key in header */
1710 if (cd->volume_key) {
1711 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1712 r = vk ? 0 : -ENOMEM;
1714 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1717 } else if (passphrase) {
1718 /* Passphrase provided, use it to unlock existing keyslot */
1719 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, passphrase,
1720 passphrase_size, &cd->hdr, &vk, cd);
1722 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1723 r = key_from_terminal(cd, _("Enter any passphrase: "),
1724 &password, &passwordLen, 0);
1728 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
1729 passwordLen, &cd->hdr, &vk, cd);
1730 crypt_safe_free(password);
1736 if (new_passphrase) {
1737 new_password = (char *)new_passphrase;
1738 new_passwordLen = new_passphrase_size;
1740 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1741 &new_password, &new_passwordLen, 1);
1746 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1747 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1752 if (!new_passphrase)
1753 crypt_safe_free(new_password);
1754 crypt_free_volume_key(vk);
1755 return r ?: keyslot;
1758 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1760 const char *keyfile,
1761 size_t keyfile_size,
1762 const char *new_keyfile,
1763 size_t new_keyfile_size)
1765 struct volume_key *vk = NULL;
1766 char *password = NULL; size_t passwordLen;
1767 char *new_password = NULL; size_t new_passwordLen;
1770 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1771 keyfile ?: "[none]", new_keyfile ?: "[none]");
1773 if (!isLUKS(cd->type)) {
1774 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1778 r = keyslot_verify_or_find_empty(cd, &keyslot);
1782 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1783 /* No slots used, try to use pre-generated key in header */
1784 if (cd->volume_key) {
1785 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1786 r = vk ? 0 : -ENOMEM;
1788 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1792 /* Read password from file of (if NULL) from terminal */
1794 r = key_from_file(cd, _("Enter any passphrase: "),
1795 &password, &passwordLen,
1796 keyfile, keyfile_size);
1798 r = key_from_terminal(cd, _("Enter any passphrase: "),
1799 &password, &passwordLen, 0);
1803 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password, passwordLen,
1811 r = key_from_file(cd, _("Enter new passphrase for key slot: "),
1812 &new_password, &new_passwordLen, new_keyfile,
1815 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1816 &new_password, &new_passwordLen, 1);
1820 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1821 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1823 crypt_safe_free(password);
1824 crypt_safe_free(new_password);
1825 crypt_free_volume_key(vk);
1826 return r < 0 ? r : keyslot;
1829 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1831 const char *volume_key,
1832 size_t volume_key_size,
1833 const char *passphrase,
1834 size_t passphrase_size)
1836 struct volume_key *vk = NULL;
1838 char *new_password = NULL; size_t new_passwordLen;
1840 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1842 if (!isLUKS(cd->type)) {
1843 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1848 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1849 else if (cd->volume_key)
1850 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1855 r = LUKS_verify_volume_key(&cd->hdr, vk);
1857 log_err(cd, _("Volume key does not match the volume.\n"));
1861 r = keyslot_verify_or_find_empty(cd, &keyslot);
1866 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1867 &new_password, &new_passwordLen, 1);
1870 passphrase = new_password;
1871 passphrase_size = new_passwordLen;
1874 r = LUKS_set_key(cd->device, keyslot, passphrase, passphrase_size,
1875 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1877 crypt_safe_free(new_password);
1878 crypt_free_volume_key(vk);
1879 return (r < 0) ? r : keyslot;
1882 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1884 crypt_keyslot_info ki;
1886 log_dbg("Destroying keyslot %d.", keyslot);
1888 if (!isLUKS(cd->type)) {
1889 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1893 ki = crypt_keyslot_status(cd, keyslot);
1894 if (ki == CRYPT_SLOT_INVALID) {
1895 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1899 if (ki == CRYPT_SLOT_INACTIVE) {
1900 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1904 return LUKS_del_key(cd->device, keyslot, &cd->hdr, cd);
1907 // activation/deactivation of device mapping
1908 int crypt_activate_by_passphrase(struct crypt_device *cd,
1911 const char *passphrase,
1912 size_t passphrase_size,
1915 crypt_status_info ci;
1916 struct volume_key *vk = NULL;
1917 char *read_passphrase = NULL;
1918 size_t passphraseLen = 0;
1921 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1922 name ? "Activating" : "Checking", name ?: "",
1923 keyslot, passphrase ? "" : "[none] ");
1926 ci = crypt_status(NULL, name);
1927 if (ci == CRYPT_INVALID)
1929 else if (ci >= CRYPT_ACTIVE) {
1930 log_err(cd, _("Device %s already exists.\n"), name);
1935 /* plain, use hashed passphrase */
1936 if (isPLAIN(cd->type)) {
1938 r = key_from_terminal(cd, NULL, &read_passphrase,
1942 passphrase = read_passphrase;
1943 passphrase_size = passphraseLen;
1945 r = create_device_helper(cd, name, cd->plain_hdr.hash,
1946 cd->plain_cipher, cd->plain_cipher_mode,
1947 NULL, passphrase, passphrase_size,
1948 cd->volume_key->keylength, 0,
1949 cd->plain_hdr.skip, cd->plain_hdr.offset,
1951 flags & CRYPT_ACTIVATE_READONLY, 0);
1953 } else if (isLUKS(cd->type)) {
1954 /* provided passphrase, do not retry */
1956 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1957 passphrase_size, &cd->hdr, &vk, cd);
1959 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1964 r = open_from_hdr_and_vk(cd, vk, name, flags);
1969 crypt_safe_free(read_passphrase);
1970 crypt_free_volume_key(vk);
1972 return r < 0 ? r : keyslot;
1975 int crypt_activate_by_keyfile(struct crypt_device *cd,
1978 const char *keyfile,
1979 size_t keyfile_size,
1982 crypt_status_info ci;
1983 struct volume_key *vk = NULL;
1984 char *passphrase_read = NULL;
1985 size_t passphrase_size_read;
1986 unsigned int key_count = 0;
1989 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1990 name ?: "", keyslot, keyfile ?: "[none]");
1993 ci = crypt_status(NULL, name);
1994 if (ci == CRYPT_INVALID)
1996 else if (ci >= CRYPT_ACTIVE) {
1997 log_err(cd, _("Device %s already exists.\n"), name);
2005 if (isPLAIN(cd->type)) {
2006 if (cd->plain_hdr.hash)
2007 r = crypt_get_key(_("Enter passphrase: "),
2008 &passphrase_read, &passphrase_size_read,
2009 0, keyfile, cd->timeout, 0, cd);
2011 r = key_from_file(cd, _("Enter passphrase: "),
2012 &passphrase_read, &passphrase_size_read,
2013 keyfile, keyfile_size);
2016 r = create_device_helper(cd, name, cd->plain_hdr.hash,
2017 cd->plain_cipher, cd->plain_cipher_mode,
2018 NULL, passphrase_read, passphrase_size_read,
2019 cd->volume_key->keylength, 0,
2020 cd->plain_hdr.skip, cd->plain_hdr.offset,
2022 flags & CRYPT_ACTIVATE_READONLY, 0);
2023 } else if (isLUKS(cd->type)) {
2024 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
2025 &passphrase_size_read, keyfile, keyfile_size);
2028 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
2029 passphrase_size_read, &cd->hdr, &vk, cd);
2035 r = open_from_hdr_and_vk(cd, vk, name, flags);
2040 } else if (isLOOPAES(cd->type)) {
2041 r = key_from_file(cd, NULL, &passphrase_read, &passphrase_size_read,
2042 keyfile, keyfile_size);
2045 r = LOOPAES_parse_keyfile(cd, &vk, cd->loopaes_hdr.hash, &key_count,
2046 passphrase_read, passphrase_size_read);
2050 r = LOOPAES_activate(cd, name, cd->loopaes_cipher,
2052 cd->loopaes_hdr.offset,
2053 cd->loopaes_hdr.skip,
2059 crypt_safe_free(passphrase_read);
2060 crypt_free_volume_key(vk);
2065 int crypt_activate_by_volume_key(struct crypt_device *cd,
2067 const char *volume_key,
2068 size_t volume_key_size,
2071 crypt_status_info ci;
2072 struct volume_key *vk;
2075 log_dbg("Activating volume %s by volume key.", name);
2077 /* use key directly, no hash */
2078 if (isPLAIN(cd->type)) {
2079 if (!volume_key || !volume_key_size || !cd->volume_key ||
2080 volume_key_size != cd->volume_key->keylength) {
2081 log_err(cd, _("Incorrect volume key specified for plain device.\n"));
2085 return create_device_helper(cd, name, NULL,
2086 cd->plain_cipher, cd->plain_cipher_mode, NULL, volume_key, volume_key_size,
2087 cd->volume_key->keylength, 0, cd->plain_hdr.skip,
2088 cd->plain_hdr.offset, cd->plain_uuid, flags & CRYPT_ACTIVATE_READONLY, 0);
2091 if (!isLUKS(cd->type)) {
2092 log_err(cd, _("Device type is not properly initialised.\n"));
2097 ci = crypt_status(NULL, name);
2098 if (ci == CRYPT_INVALID)
2100 else if (ci >= CRYPT_ACTIVE) {
2101 log_err(cd, _("Device %s already exists.\n"), name);
2106 /* If key is not provided, try to use internal key */
2108 if (!cd->volume_key) {
2109 log_err(cd, _("Volume key does not match the volume.\n"));
2112 volume_key_size = cd->volume_key->keylength;
2113 volume_key = cd->volume_key->key;
2116 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2119 r = LUKS_verify_volume_key(&cd->hdr, vk);
2122 log_err(cd, _("Volume key does not match the volume.\n"));
2125 r = open_from_hdr_and_vk(cd, vk, name, flags);
2127 crypt_free_volume_key(vk);
2132 int crypt_deactivate(struct crypt_device *cd, const char *name)
2139 log_dbg("Deactivating volume %s.", name);
2141 if (!cd && dm_init(NULL, 1) < 0)
2144 switch (crypt_status(cd, name)) {
2146 r = dm_remove_device(name, 0, 0);
2149 log_err(cd, _("Device %s is busy.\n"), name);
2152 case CRYPT_INACTIVE:
2153 log_err(cd, _("Device %s is not active.\n"), name);
2157 log_err(cd, _("Invalid device %s.\n"), name);
2167 int crypt_volume_key_get(struct crypt_device *cd,
2170 size_t *volume_key_size,
2171 const char *passphrase,
2172 size_t passphrase_size)
2174 struct volume_key *vk;
2175 char *processed_key = NULL;
2179 key_len = crypt_get_volume_key_size(cd);
2180 if (key_len > *volume_key_size) {
2181 log_err(cd, _("Volume key buffer too small.\n"));
2185 if (isPLAIN(cd->type) && cd->plain_hdr.hash) {
2186 processed_key = process_key(cd, cd->plain_hdr.hash, NULL, key_len,
2187 passphrase, passphrase_size);
2188 if (!processed_key) {
2189 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
2192 memcpy(volume_key, processed_key, key_len);
2193 *volume_key_size = key_len;
2194 crypt_safe_free(processed_key);
2198 if (isLUKS(cd->type)) {
2199 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
2200 passphrase_size, &cd->hdr, &vk, cd);
2203 memcpy(volume_key, vk->key, vk->keylength);
2204 *volume_key_size = vk->keylength;
2207 crypt_free_volume_key(vk);
2211 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
2215 int crypt_volume_key_verify(struct crypt_device *cd,
2216 const char *volume_key,
2217 size_t volume_key_size)
2219 struct volume_key *vk;
2222 if (!isLUKS(cd->type)) {
2223 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2227 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2231 r = LUKS_verify_volume_key(&cd->hdr, vk);
2234 log_err(cd, _("Volume key does not match the volume.\n"));
2236 crypt_free_volume_key(vk);
2241 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
2243 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
2244 cd->timeout = timeout_sec;
2247 void crypt_set_password_retry(struct crypt_device *cd, int tries)
2249 log_dbg("Password retry count set to %d.", tries);
2253 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2255 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
2256 cd->iteration_time = iteration_time_ms;
2259 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
2261 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
2262 cd->password_verify = password_verify ? 1 : 0;
2265 void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
2268 case CRYPT_RNG_URANDOM:
2269 case CRYPT_RNG_RANDOM:
2270 log_dbg("RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
2271 cd->rng_type = rng_type;
2275 int crypt_get_rng_type(struct crypt_device *cd)
2280 return cd->rng_type;
2283 int crypt_memory_lock(struct crypt_device *cd, int lock)
2285 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
2289 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
2293 if (!cd && dm_init(NULL, 1) < 0)
2294 return CRYPT_INVALID;
2296 r = dm_status_device(name);
2301 if (r < 0 && r != -ENODEV)
2302 return CRYPT_INVALID;
2305 return CRYPT_ACTIVE;
2310 return CRYPT_INACTIVE;
2313 static void hexprintICB(struct crypt_device *cd, char *d, int n)
2316 for(i = 0; i < n; i++)
2317 log_std(cd, "%02hhx ", (char)d[i]);
2320 int crypt_dump(struct crypt_device *cd)
2323 if (!isLUKS(cd->type)) { //FIXME
2324 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2328 log_std(cd, "LUKS header information for %s\n\n", cd->device);
2329 log_std(cd, "Version: \t%d\n", cd->hdr.version);
2330 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
2331 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
2332 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec);
2333 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
2334 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
2335 log_std(cd, "MK digest: \t");
2336 hexprintICB(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE);
2338 log_std(cd, "MK salt: \t");
2339 hexprintICB(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2);
2340 log_std(cd, "\n \t");
2341 hexprintICB(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
2343 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
2344 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
2345 for(i = 0; i < LUKS_NUMKEYS; i++) {
2346 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
2347 log_std(cd, "Key Slot %d: ENABLED\n",i);
2348 log_std(cd, "\tIterations: \t%d\n",
2349 cd->hdr.keyblock[i].passwordIterations);
2350 log_std(cd, "\tSalt: \t");
2351 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt,
2353 log_std(cd, "\n\t \t");
2354 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt +
2355 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
2358 log_std(cd, "\tKey material offset:\t%d\n",
2359 cd->hdr.keyblock[i].keyMaterialOffset);
2360 log_std(cd, "\tAF stripes: \t%d\n",
2361 cd->hdr.keyblock[i].stripes);
2364 log_std(cd, "Key Slot %d: DISABLED\n", i);
2370 const char *crypt_get_cipher(struct crypt_device *cd)
2372 if (isPLAIN(cd->type))
2373 return cd->plain_cipher;
2375 if (isLUKS(cd->type))
2376 return cd->hdr.cipherName;
2378 if (isLOOPAES(cd->type))
2379 return cd->loopaes_cipher;
2384 const char *crypt_get_cipher_mode(struct crypt_device *cd)
2386 if (isPLAIN(cd->type))
2387 return cd->plain_cipher_mode;
2389 if (isLUKS(cd->type))
2390 return cd->hdr.cipherMode;
2392 if (isLOOPAES(cd->type))
2393 return cd->loopaes_cipher_mode;
2398 const char *crypt_get_uuid(struct crypt_device *cd)
2400 if (isLUKS(cd->type))
2401 return cd->hdr.uuid;
2403 if (isPLAIN(cd->type))
2404 return cd->plain_uuid;
2406 if (isLOOPAES(cd->type))
2407 return cd->loopaes_uuid;
2412 const char *crypt_get_device_name(struct crypt_device *cd)
2417 int crypt_get_volume_key_size(struct crypt_device *cd)
2419 if (isPLAIN(cd->type) && cd->volume_key)
2420 return cd->volume_key->keylength;
2422 if (isLUKS(cd->type))
2423 return cd->hdr.keyBytes;
2425 if (isLOOPAES(cd->type))
2426 return cd->loopaes_key_size;
2431 uint64_t crypt_get_data_offset(struct crypt_device *cd)
2433 if (isPLAIN(cd->type))
2434 return cd->plain_hdr.offset;
2436 if (isLUKS(cd->type))
2437 return cd->hdr.payloadOffset;
2439 if (isLOOPAES(cd->type))
2440 return cd->loopaes_hdr.offset;
2445 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
2447 if (!isLUKS(cd->type)) {
2448 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2449 return CRYPT_SLOT_INVALID;
2452 return LUKS_keyslot_info(&cd->hdr, keyslot);
2455 int crypt_keyslot_max(const char *type)
2457 if (type && isLUKS(type))
2458 return LUKS_NUMKEYS;
2463 const char *crypt_get_type(struct crypt_device *cd)
2468 int crypt_get_active_device(struct crypt_device *cd __attribute__((unused)),
2470 struct crypt_active_device *cad)
2474 r = dm_query_device(name, NULL, &cad->size, &cad->iv_offset, &cad->offset,
2475 NULL, NULL, NULL, &readonly, NULL, NULL);
2479 cad->flags = readonly ? CRYPT_ACTIVATE_READONLY : 0;