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, _("Cannot not read %d bytes from key file %s.\n"),
163 (int)key_size, key_file);
164 crypt_safe_free(key);
167 memcpy(key, pass, key_size);
171 /* key is coming from tty, fd or binary stdin */
173 r = crypt_plain_hash(cd, hash_name, key, key_size, pass, passLen);
176 log_err(cd, _("Hash algorithm %s not supported.\n"),
179 log_err(cd, _("Key processing error (using hash %s).\n"),
181 crypt_safe_free(key);
184 } else if (passLen > key_size) {
185 memcpy(key, pass, key_size);
187 memcpy(key, pass, passLen);
193 static int isPLAIN(const char *type)
195 return (type && !strcmp(CRYPT_PLAIN, type));
198 static int isLUKS(const char *type)
200 return (type && !strcmp(CRYPT_LUKS1, type));
203 static int isLOOPAES(const char *type)
205 return (type && !strcmp(CRYPT_LOOPAES, type));
208 /* keyslot helpers */
209 static int keyslot_verify_or_find_empty(struct crypt_device *cd, int *keyslot)
211 if (*keyslot == CRYPT_ANY_SLOT) {
212 *keyslot = LUKS_keyslot_find_empty(&cd->hdr);
214 log_err(cd, _("All key slots full.\n"));
219 switch (LUKS_keyslot_info(&cd->hdr, *keyslot)) {
220 case CRYPT_SLOT_INVALID:
221 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
222 *keyslot, LUKS_NUMKEYS - 1);
224 case CRYPT_SLOT_INACTIVE:
227 log_err(cd, _("Key slot %d is full, please select another one.\n"),
235 static int verify_other_keyslot(struct crypt_device *cd,
236 const char *key_file,
239 struct volume_key *vk = NULL;
240 crypt_keyslot_info ki;
242 char *password = NULL;
245 r = crypt_get_key(_("Enter any remaining LUKS passphrase: "),
246 &password, &passwordLen, 0, key_file, cd->timeout,
247 cd->password_verify, cd);
251 ki = crypt_keyslot_status(cd, keyIndex);
252 if (ki == CRYPT_SLOT_ACTIVE) /* Not last slot */
253 LUKS_keyslot_set(&cd->hdr, keyIndex, 0);
255 openedIndex = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT,
256 password, passwordLen,
259 if (ki == CRYPT_SLOT_ACTIVE)
260 LUKS_keyslot_set(&cd->hdr, keyIndex, 1);
265 log_verbose(cd, _("Key slot %d verified.\n"), openedIndex);
267 crypt_free_volume_key(vk);
268 crypt_safe_free(password);
272 static int find_keyslot_by_passphrase(struct crypt_device *cd,
273 const char *key_file,
276 struct volume_key *vk = NULL;
277 char *password = NULL;
281 r = crypt_get_key(message,&password,&passwordLen, 0, key_file,
282 cd->timeout, cd->password_verify, cd);
286 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
287 passwordLen, &cd->hdr, &vk, cd);
289 crypt_free_volume_key(vk);
290 crypt_safe_free(password);
294 static int luks_remove_helper(struct crypt_device *cd,
296 const char *other_key_file,
297 const char *key_file,
300 crypt_keyslot_info ki;
303 if (key_slot == CRYPT_ANY_SLOT) {
304 key_slot = find_keyslot_by_passphrase(cd, key_file,
305 _("Enter LUKS passphrase to be deleted: "));
311 log_std(cd, _("Key slot %d selected for deletion.\n"), key_slot);
314 ki = crypt_keyslot_status(cd, key_slot);
315 if (ki == CRYPT_SLOT_INVALID) {
316 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
317 key_slot, LUKS_NUMKEYS - 1);
321 if (ki <= CRYPT_SLOT_INACTIVE) {
322 log_err(cd, _("Key %d not active. Can't wipe.\n"), key_slot);
327 if (ki == CRYPT_SLOT_ACTIVE_LAST && cd->confirm &&
328 !(cd->confirm(_("This is the last keyslot."
329 " Device will become unusable after purging this key."),
330 cd->confirm_usrptr))) {
336 r = verify_other_keyslot(cd, other_key_file, key_slot);
341 r = crypt_keyslot_destroy(cd, key_slot);
343 return (r < 0) ? r : 0;
346 static int create_device_helper(struct crypt_device *cd,
350 const char *cipher_mode,
351 const char *key_file,
352 const char *passphrase,
353 size_t passphrase_size,
362 crypt_status_info ci;
363 char *dm_cipher = NULL;
364 char *processed_key = NULL;
370 ci = crypt_status(cd, name);
371 if (ci == CRYPT_INVALID)
374 if (reload && ci < CRYPT_ACTIVE)
377 if (!reload && ci >= CRYPT_ACTIVE) {
378 log_err(cd, _("Device %s already exists.\n"), name);
382 if (key_size > 1024) {
383 log_err(cd, _("Invalid key size %d.\n"), (int)key_size);
387 r = device_check_and_adjust(cd, cd->device, !reload, &size, &offset, &read_only);
391 if (cipher_mode && asprintf(&dm_cipher, "%s-%s", cipher, cipher_mode) < 0)
394 processed_key = process_key(cd, hash, key_file, key_size, passphrase, passphrase_size);
395 if (!processed_key) {
400 r = dm_create_device(name, cd->device, dm_cipher ?: cipher, cd->type, uuid, size, skip, offset,
401 key_size, processed_key, read_only, reload);
403 if (isPLAIN(cd->type) && !uuid)
404 (void)dm_query_device(name, NULL, NULL, NULL, NULL, NULL, NULL,
405 NULL, NULL, NULL, &cd->plain_uuid);
408 crypt_safe_free(processed_key);
412 static int open_from_hdr_and_vk(struct crypt_device *cd,
413 struct volume_key *vk,
417 uint64_t size, offset;
419 int read_only, no_uuid, r;
422 offset = crypt_get_data_offset(cd);
423 read_only = flags & CRYPT_ACTIVATE_READONLY;
424 no_uuid = flags & CRYPT_ACTIVATE_NO_UUID;
426 r = device_check_and_adjust(cd, cd->device, 1, &size, &offset, &read_only);
430 if (asprintf(&cipher, "%s-%s", crypt_get_cipher(cd),
431 crypt_get_cipher_mode(cd)) < 0)
434 r = dm_create_device(name, cd->device, cipher, cd->type,
435 no_uuid ? NULL : crypt_get_uuid(cd),
436 size, 0, offset, vk->keylength, vk->key,
442 static void log_wrapper(int level, const char *msg, void *usrptr)
444 void (*xlog)(int level, char *msg) = usrptr;
445 xlog(level, (char *)msg);
448 static int yesDialog_wrapper(const char *msg, void *usrptr)
450 int (*xyesDialog)(char *msg) = usrptr;
451 return xyesDialog((char*)msg);
454 int crypt_confirm(struct crypt_device *cd, const char *msg)
456 if (!cd || !cd->confirm)
459 return cd->confirm(msg, cd->confirm_usrptr);
462 static int key_from_terminal(struct crypt_device *cd, char *msg, char **key,
463 size_t *key_len, int force_verify)
469 if(!msg && asprintf(&prompt, _("Enter passphrase for %s: "),
470 cd->backing_file ?: cd->device) < 0)
477 *key = crypt_safe_alloc(DEFAULT_PASSPHRASE_SIZE_MAX);
482 r = cd->password(msg, *key, DEFAULT_PASSPHRASE_SIZE_MAX,
483 cd->password_usrptr);
485 crypt_safe_free(*key);
490 r = crypt_get_key(msg, key, key_len, 0, NULL, cd->timeout,
491 (force_verify || cd->password_verify), cd);
494 return (r < 0) ? r: 0;
497 static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslot,
498 struct volume_key **vk)
500 char *passphrase_read = NULL;
501 size_t passphrase_size_read;
502 int r = -EINVAL, eperm = 0, tries = cd->tries;
506 crypt_free_volume_key(*vk);
509 r = key_from_terminal(cd, NULL, &passphrase_read,
510 &passphrase_size_read, 0);
514 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
515 passphrase_size_read, &cd->hdr, vk, cd);
518 crypt_safe_free(passphrase_read);
519 passphrase_read = NULL;
520 } while (r == -EPERM && (--tries > 0));
523 crypt_free_volume_key(*vk);
526 /* Report wrong passphrase if at least one try failed */
527 if (eperm && r == -EPIPE)
531 crypt_safe_free(passphrase_read);
535 static int key_from_file(struct crypt_device *cd, char *msg,
536 char **key, size_t *key_len,
537 const char *key_file, size_t key_size)
539 return crypt_get_key(msg, key, key_len, key_size, key_file,
543 static int _crypt_init(struct crypt_device **cd,
545 struct crypt_options *options,
546 int load, int need_dm)
550 /* if it is plain device and mapping table is being reloaded
551 initialize it by name*/
552 init_by_name = (type && !strcmp(type, CRYPT_PLAIN) && load);
554 /* Some of old API calls do not require DM in kernel,
555 fake initialisation by initialise it with kernel_check disabled */
557 (void)dm_init(NULL, 0);
559 r = crypt_init_by_name(cd, options->name);
561 r = crypt_init(cd, options->device);
568 crypt_set_log_callback(*cd, log_wrapper, options->icb->log);
569 crypt_set_confirm_callback(*cd, yesDialog_wrapper, options->icb->yesDialog);
571 crypt_set_timeout(*cd, options->timeout);
572 crypt_set_password_retry(*cd, options->tries);
573 crypt_set_iterarion_time(*cd, options->iteration_time ?: 1000);
574 crypt_set_password_verify(*cd, options->flags & CRYPT_FLAG_VERIFY);
576 if (load && !init_by_name)
577 r = crypt_load(*cd, type, NULL);
579 if (!r && type && !(*cd)->type) {
580 (*cd)->type = strdup(type);
591 void crypt_set_log_callback(struct crypt_device *cd,
592 void (*log)(int level, const char *msg, void *usrptr),
599 cd->log_usrptr = usrptr;
603 void crypt_set_confirm_callback(struct crypt_device *cd,
604 int (*confirm)(const char *msg, void *usrptr),
607 cd->confirm = confirm;
608 cd->confirm_usrptr = usrptr;
611 void crypt_set_password_callback(struct crypt_device *cd,
612 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
615 cd->password = password;
616 cd->password_usrptr = usrptr;
619 /* OPTIONS: name, cipher, device, hash, key_file, key_size, key_slot,
620 * offset, size, skip, timeout, tries, passphrase_fd (ignored),
622 static int crypt_create_and_update_device(struct crypt_options *options, int update)
624 struct crypt_device *cd = NULL;
625 char *passphrase = NULL;
626 size_t passphrase_size = 0;
629 r = _crypt_init(&cd, CRYPT_PLAIN, options, 0, 1);
633 r = crypt_get_key(_("Enter passphrase: "), &passphrase, &passphrase_size,
634 options->key_size, options->key_file,
635 cd->timeout, cd->password_verify, cd);
637 r = create_device_helper(cd, options->name, options->hash,
638 options->cipher, NULL, options->key_file,
639 passphrase, passphrase_size,
640 options->key_size, options->size, options->skip,
641 options->offset, NULL, options->flags & CRYPT_FLAG_READONLY,
644 crypt_safe_free(passphrase);
649 int crypt_create_device(struct crypt_options *options)
651 return crypt_create_and_update_device(options, 0);
654 int crypt_update_device(struct crypt_options *options)
656 return crypt_create_and_update_device(options, 1);
659 /* OPTIONS: name, size, icb */
660 int crypt_resize_device(struct crypt_options *options)
662 struct crypt_device *cd = NULL;
663 char *device = NULL, *cipher = NULL, *uuid = NULL, *key = NULL;
664 const char *type = NULL;
665 uint64_t size, skip, offset;
666 int key_size, read_only, r;
668 log_dbg("Resizing device %s to %" PRIu64 " sectors.", options->name, options->size);
670 if (dm_init(NULL, 1) < 0)
673 r = dm_query_device(options->name, &device, &size, &skip, &offset,
674 &cipher, &key_size, &key, &read_only, NULL, &uuid);
676 log_err(NULL, _("Device %s is not active.\n"), options->name);
680 /* Try to determine type of device from UUID */
683 if (!strncmp(uuid, CRYPT_PLAIN, strlen(CRYPT_PLAIN))) {
687 } else if (!strncmp(uuid, CRYPT_LUKS1, strlen(CRYPT_LUKS1)))
691 if (!options->device)
692 options->device = device;
694 r = _crypt_init(&cd, type, options, 1, 1);
698 size = options->size;
699 r = device_check_and_adjust(cd, device, 0, &size, &offset, &read_only);
703 r = dm_create_device(options->name, device, cipher, type,
704 crypt_get_uuid(cd), size, skip, offset,
705 key_size, key, read_only, 1);
707 crypt_safe_free(key);
709 if (options->device == device)
710 options->device = NULL;
718 /* OPTIONS: name, icb */
719 int crypt_query_device(struct crypt_options *options)
723 log_dbg("Query device %s.", options->name);
725 if (dm_init(NULL, 1) < 0)
728 r = dm_status_device(options->name);
732 r = dm_query_device(options->name, (char **)&options->device, &options->size,
733 &options->skip, &options->offset, (char **)&options->cipher,
734 &options->key_size, NULL, &read_only, NULL, NULL);
737 options->flags |= CRYPT_FLAG_READONLY;
739 options->flags |= CRYPT_FLAG_FREE_DEVICE;
740 options->flags |= CRYPT_FLAG_FREE_CIPHER;
752 /* OPTIONS: name, icb */
753 int crypt_remove_device(struct crypt_options *options)
755 struct crypt_device *cd = NULL;
758 r = crypt_init_by_name(&cd, options->name);
760 r = crypt_deactivate(cd, options->name);
767 /* OPTIONS: device, cipher, hash, align_payload, key_size (master key), key_slot
768 * new_key_file, iteration_time, timeout, flags, icb */
769 int crypt_luksFormat(struct crypt_options *options)
771 char cipherName[LUKS_CIPHERNAME_L];
772 char cipherMode[LUKS_CIPHERMODE_L];
775 struct crypt_device *cd = NULL;
776 struct crypt_params_luks1 cp = {
777 .hash = options->hash,
778 .data_alignment = options->align_payload
782 r = crypt_parse_name_and_mode(options->cipher, cipherName, NULL, cipherMode);
784 log_err(cd, _("No known cipher specification pattern detected.\n"));
788 if ((r = _crypt_init(&cd, CRYPT_LUKS1, options, 0, 1)))
791 if (options->key_slot >= LUKS_NUMKEYS && options->key_slot != CRYPT_ANY_SLOT) {
792 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
793 options->key_slot, LUKS_NUMKEYS - 1);
798 r = crypt_get_key(_("Enter LUKS passphrase: "), &password, &passwordLen, 0,
799 options->new_key_file, cd->timeout, cd->password_verify, cd);
804 r = crypt_format(cd, CRYPT_LUKS1, cipherName, cipherMode,
805 NULL, NULL, options->key_size, &cp);
809 /* Add keyslot using internally stored volume key generated during format */
810 r = crypt_keyslot_add_by_volume_key(cd, options->key_slot, NULL, 0,
811 password, passwordLen);
814 crypt_safe_free(password);
815 return (r < 0) ? r : 0;
818 /* OPTIONS: name, device, key_size, key_file, timeout, tries, flags, icb */
819 int crypt_luksOpen(struct crypt_options *options)
821 struct crypt_device *cd = NULL;
828 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
832 if (options->flags & CRYPT_FLAG_READONLY)
833 flags |= CRYPT_ACTIVATE_READONLY;
835 if (options->flags & CRYPT_FLAG_NON_EXCLUSIVE_ACCESS)
836 flags |= CRYPT_ACTIVATE_NO_UUID;
838 if (options->key_file)
839 r = crypt_activate_by_keyfile(cd, options->name,
840 CRYPT_ANY_SLOT, options->key_file, 0,
843 r = crypt_activate_by_passphrase(cd, options->name,
844 CRYPT_ANY_SLOT, options->passphrase,
845 options->passphrase ? strlen(options->passphrase) : 0,
849 return (r < 0) ? r : 0;
852 /* OPTIONS: device, keys_slot, key_file, timeout, flags, icb */
853 int crypt_luksKillSlot(struct crypt_options *options)
855 struct crypt_device *cd = NULL;
858 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
862 r = luks_remove_helper(cd, options->key_slot, options->key_file, NULL,
863 options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
866 return (r < 0) ? r : 0;
869 /* OPTIONS: device, new_key_file, key_file, timeout, flags, icb */
870 int crypt_luksRemoveKey(struct crypt_options *options)
872 struct crypt_device *cd = NULL;
875 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
879 r = luks_remove_helper(cd, CRYPT_ANY_SLOT, options->key_file, options->new_key_file,
880 options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
883 return (r < 0) ? r : 0;
887 /* OPTIONS: device, new_key_file, key_file, key_slot, flags,
888 iteration_time, timeout, icb */
889 int crypt_luksAddKey(struct crypt_options *options)
891 struct crypt_device *cd = NULL;
894 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
898 if (options->key_file || options->new_key_file)
899 r = crypt_keyslot_add_by_keyfile(cd, options->key_slot,
900 options->key_file, 0,
901 options->new_key_file, 0);
903 r = crypt_keyslot_add_by_passphrase(cd, options->key_slot,
907 return (r < 0) ? r : 0;
910 /* OPTIONS: device, icb */
911 int crypt_luksUUID(struct crypt_options *options)
913 struct crypt_device *cd = NULL;
917 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
921 uuid = crypt_get_uuid(cd);
922 log_std(cd, "%s\n", uuid ?: "");
927 /* OPTIONS: device, icb */
928 int crypt_isLuks(struct crypt_options *options)
930 struct crypt_device *cd = NULL;
933 log_dbg("Check device %s for LUKS header.", options->device);
939 r = crypt_init(&cd, options->device);
943 /* Do print fail here, no need to crypt_load() */
944 r = LUKS_read_phdr(cd->device, &cd->hdr, 0, cd) ? -EINVAL : 0;
950 /* OPTIONS: device, icb */
951 int crypt_luksDump(struct crypt_options *options)
953 struct crypt_device *cd = NULL;
956 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
966 void crypt_get_error(char *buf, size_t size)
968 const char *error = get_error();
970 if (!buf || size < 1)
973 strncpy(buf, error, size - 1);
974 buf[size - 1] = '\0';
980 void crypt_put_options(struct crypt_options *options)
982 if (options->flags & CRYPT_FLAG_FREE_DEVICE) {
983 free((char *)options->device);
984 options->device = NULL;
985 options->flags &= ~CRYPT_FLAG_FREE_DEVICE;
987 if (options->flags & CRYPT_FLAG_FREE_CIPHER) {
988 free((char *)options->cipher);
989 options->cipher = NULL;
990 options->flags &= ~CRYPT_FLAG_FREE_CIPHER;
994 const char *crypt_get_dir(void)
999 /////////////////////////////////
1004 int crypt_init(struct crypt_device **cd, const char *device)
1006 struct crypt_device *h = NULL;
1007 int r, readonly = 0;
1012 log_dbg("Allocating crypt device %s context.", device);
1014 if (!(h = malloc(sizeof(struct crypt_device))))
1017 memset(h, 0, sizeof(*h));
1021 r = device_ready(NULL, device, O_RDONLY);
1022 if (r == -ENOTBLK) {
1023 h->device = crypt_loop_get_device();
1024 log_dbg("Not a block device, %s%s.",
1025 h->device ? "using free loop device " :
1026 "no free loop device found",
1029 log_err(NULL, _("Cannot find a free loopback device.\n"));
1034 /* Keep the loop open, dettached on last close. */
1035 h->loop_fd = crypt_loop_attach(h->device, device, 0, 1, &readonly);
1036 if (h->loop_fd == -1) {
1037 log_err(NULL, _("Attaching loopback device failed "
1038 "(loop device with autoclear flag is required).\n"));
1043 h->backing_file = crypt_loop_backing_file(h->device);
1044 r = device_ready(NULL, h->device, O_RDONLY);
1052 if (!h->device && device && !(h->device = strdup(device))) {
1057 if (dm_init(h, 1) < 0) {
1062 h->iteration_time = 1000;
1063 h->password_verify = 0;
1065 h->rng_type = crypt_random_default_key_rng();
1071 if (h->loop_fd != -1)
1074 free(h->backing_file);
1080 int crypt_init_by_name(struct crypt_device **cd, const char *name)
1082 crypt_status_info ci;
1083 struct crypt_active_device cad;
1084 char *device = NULL, *cipher_full = NULL, *device_uuid = NULL;
1085 char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
1087 int key_size = 0, key_nums, r;
1090 log_dbg("Allocating crypt device context by device %s.", name);
1092 ci = crypt_status(NULL, name);
1093 if (ci == CRYPT_INVALID)
1096 if (ci < CRYPT_ACTIVE) {
1097 log_err(NULL, _("Device %s is not active.\n"), name);
1101 r = dm_query_device(name, &device, &cad.size, &cad.iv_offset, &cad.offset,
1102 &cipher_full, &key_size, &key, NULL, NULL,
1108 r = crypt_init(cd, device);
1110 /* Underlying device disappeared but mapping still active */
1111 if (!device || r == -ENOTBLK)
1112 log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
1115 /* Underlying device is not readable but crypt mapping exists */
1116 if (r == -ENOTBLK) {
1119 r = crypt_init(cd, NULL);
1125 /* Try to initialise basic parameters from active device */
1127 if (!(*cd)->backing_file && device && crypt_loop_device(device) &&
1128 !((*cd)->backing_file = crypt_loop_backing_file(device))) {
1134 if (!strncmp(CRYPT_PLAIN, device_uuid, sizeof(CRYPT_PLAIN)-1)) {
1135 (*cd)->type = strdup(CRYPT_PLAIN);
1136 (*cd)->plain_uuid = strdup(device_uuid);
1137 (*cd)->plain_hdr.hash = NULL; /* no way to get this */
1138 (*cd)->plain_hdr.offset = cad.offset;
1139 (*cd)->plain_hdr.skip = cad.iv_offset;
1140 (*cd)->volume_key = crypt_alloc_volume_key(key_size, key);
1141 if (!(*cd)->volume_key) {
1146 r = crypt_parse_name_and_mode(cipher_full, cipher, NULL, cipher_mode);
1148 (*cd)->plain_cipher = strdup(cipher);
1149 (*cd)->plain_cipher_mode = strdup(cipher_mode);
1151 } else if (!strncmp(CRYPT_LOOPAES, device_uuid, sizeof(CRYPT_LOOPAES)-1)) {
1152 (*cd)->type = strdup(CRYPT_LOOPAES);
1153 (*cd)->loopaes_uuid = strdup(device_uuid);
1154 (*cd)->loopaes_hdr.offset = cad.offset;
1156 r = crypt_parse_name_and_mode(cipher_full, cipher,
1157 &key_nums, cipher_mode);
1159 (*cd)->loopaes_cipher = strdup(cipher);
1160 (*cd)->loopaes_cipher_mode = strdup(cipher_mode);
1161 /* version 3 uses last key for IV */
1162 if (key_size % key_nums)
1164 (*cd)->loopaes_key_size = key_size / key_nums;
1166 } else if (!strncmp(CRYPT_LUKS1, device_uuid, sizeof(CRYPT_LUKS1)-1)) {
1168 if (crypt_load(*cd, CRYPT_LUKS1, NULL) < 0 ||
1169 crypt_volume_key_verify(*cd, key, key_size) < 0) {
1170 log_dbg("LUKS device header does not match active device.");
1174 (*cd)->volume_key = crypt_alloc_volume_key(key_size, key);
1175 if (!(*cd)->volume_key) {
1182 log_dbg("Active device has no UUID set, some parameters are not set.");
1189 crypt_safe_free(key);
1196 static int _crypt_format_plain(struct crypt_device *cd,
1198 const char *cipher_mode,
1200 size_t volume_key_size,
1201 struct crypt_params_plain *params)
1203 if (!cipher || !cipher_mode) {
1204 log_err(cd, _("Invalid plain crypt parameters.\n"));
1208 if (volume_key_size > 1024) {
1209 log_err(cd, _("Invalid key size.\n"));
1213 cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
1214 if (!cd->volume_key)
1217 cd->plain_cipher = strdup(cipher);
1218 cd->plain_cipher_mode = strdup(cipher_mode);
1221 cd->plain_uuid = strdup(uuid);
1223 if (params && params->hash)
1224 cd->plain_hdr.hash = strdup(params->hash);
1226 cd->plain_hdr.offset = params ? params->offset : 0;
1227 cd->plain_hdr.skip = params ? params->skip : 0;
1229 if (!cd->plain_cipher || !cd->plain_cipher_mode)
1235 static int _crypt_format_luks1(struct crypt_device *cd,
1237 const char *cipher_mode,
1239 const char *volume_key,
1240 size_t volume_key_size,
1241 struct crypt_params_luks1 *params)
1244 unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
1245 unsigned long alignment_offset = 0;
1248 log_err(cd, _("Can't format LUKS without device.\n"));
1253 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
1256 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
1261 if (params && params->data_alignment)
1262 required_alignment = params->data_alignment * SECTOR_SIZE;
1264 get_topology_alignment(cd->device, &required_alignment,
1265 &alignment_offset, DEFAULT_DISK_ALIGNMENT);
1267 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
1268 (params && params->hash) ? params->hash : "sha1",
1270 required_alignment / SECTOR_SIZE,
1271 alignment_offset / SECTOR_SIZE,
1272 cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1276 /* Wipe first 8 sectors - fs magic numbers etc. */
1277 r = wipe_device_header(cd->device, 8);
1280 log_err(cd, _("Cannot format device %s which is still in use.\n"),
1283 log_err(cd, _("Cannot wipe header on device %s.\n"),
1289 r = LUKS_write_phdr(cd->device, &cd->hdr, cd);
1294 static int _crypt_format_loopaes(struct crypt_device *cd,
1297 size_t volume_key_size,
1298 struct crypt_params_loopaes *params)
1301 log_err(cd, _("Can't format LOOPAES without device.\n"));
1305 if (volume_key_size > 1024) {
1306 log_err(cd, _("Invalid key size.\n"));
1310 cd->loopaes_key_size = volume_key_size;
1312 cd->loopaes_cipher = strdup(cipher ?: DEFAULT_LOOPAES_CIPHER);
1315 cd->loopaes_uuid = strdup(uuid);
1317 if (params && params->hash)
1318 cd->loopaes_hdr.hash = strdup(params->hash);
1320 cd->loopaes_hdr.offset = params ? params->offset : 0;
1321 cd->loopaes_hdr.skip = params ? params->skip : 0;
1326 int crypt_format(struct crypt_device *cd,
1329 const char *cipher_mode,
1331 const char *volume_key,
1332 size_t volume_key_size,
1340 log_dbg("Formatting device %s as type %s.", cd->device ?: "(none)", type);
1342 r = init_crypto(cd);
1347 r = _crypt_format_plain(cd, cipher, cipher_mode,
1348 uuid, volume_key_size, params);
1349 else if (isLUKS(type))
1350 r = _crypt_format_luks1(cd, cipher, cipher_mode,
1351 uuid, volume_key, volume_key_size, params);
1352 else if (isLOOPAES(type))
1353 r = _crypt_format_loopaes(cd, cipher, uuid, volume_key_size, params);
1355 /* FIXME: allow plugins here? */
1356 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
1360 if (!r && !(cd->type = strdup(type)))
1364 crypt_free_volume_key(cd->volume_key);
1365 cd->volume_key = NULL;
1371 int crypt_load(struct crypt_device *cd,
1372 const char *requested_type,
1373 void *params __attribute__((unused)))
1375 struct luks_phdr hdr;
1378 log_dbg("Trying to load %s crypt type from device %s.",
1379 requested_type ?: "any", cd->device ?: "(none)");
1384 if (requested_type && !isLUKS(requested_type))
1387 r = init_crypto(cd);
1391 r = LUKS_read_phdr(cd->device, &hdr, 1, cd);
1394 memcpy(&cd->hdr, &hdr, sizeof(hdr));
1395 cd->type = strdup(CRYPT_LUKS1);
1403 int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
1405 char *device = NULL, *cipher = NULL, *uuid = NULL, *key = NULL;
1406 uint64_t size, skip, offset;
1407 int key_size, read_only, r;
1409 /* Device context type must be initialised */
1410 if (!cd->type || !crypt_get_uuid(cd))
1413 log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
1415 r = dm_query_device(name, &device, &size, &skip, &offset,
1416 &cipher, &key_size, &key, &read_only, NULL, &uuid);
1418 log_err(NULL, _("Device %s is not active.\n"), name);
1427 r = device_check_and_adjust(cd, device, 0, &new_size, &offset, &read_only);
1431 if (new_size == size) {
1432 log_dbg("Device has already requested size %" PRIu64
1438 r = dm_create_device(name, device, cipher, cd->type,
1439 crypt_get_uuid(cd), new_size, skip, offset,
1440 key_size, key, read_only, 1);
1442 crypt_safe_free(key);
1450 int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
1452 if (!isLUKS(cd->type)) {
1453 log_err(cd, _("This operation is not supported for this device type.\n"));
1457 if (uuid && !strncmp(uuid, cd->hdr.uuid, sizeof(cd->hdr.uuid))) {
1458 log_dbg("UUID is the same as requested (%s) for device %s.",
1464 log_dbg("Requested new UUID change to %s for %s.", uuid, cd->device);
1466 log_dbg("Requested new UUID refresh for %s.", cd->device);
1468 if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
1471 return LUKS_hdr_uuid_set(cd->device, &cd->hdr, uuid, cd);
1474 int crypt_header_backup(struct crypt_device *cd,
1475 const char *requested_type,
1476 const char *backup_file)
1480 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1483 r = init_crypto(cd);
1487 log_dbg("Requested header backup of device %s (%s) to "
1488 "file %s.", cd->device, requested_type, backup_file);
1490 return LUKS_hdr_backup(backup_file, cd->device, &cd->hdr, cd);
1493 int crypt_header_restore(struct crypt_device *cd,
1494 const char *requested_type,
1495 const char *backup_file)
1499 if (requested_type && !isLUKS(requested_type))
1502 /* Some hash functions need initialized gcrypt library */
1503 r = init_crypto(cd);
1507 log_dbg("Requested header restore to device %s (%s) from "
1508 "file %s.", cd->device, requested_type, backup_file);
1510 return LUKS_hdr_restore(backup_file, cd->device, &cd->hdr, cd);
1513 void crypt_free(struct crypt_device *cd)
1516 log_dbg("Releasing crypt device %s context.", cd->device);
1518 if (cd->loop_fd != -1)
1522 crypt_free_volume_key(cd->volume_key);
1525 free(cd->backing_file);
1528 /* used in plain device only */
1529 free((char*)cd->plain_hdr.hash);
1530 free(cd->plain_cipher);
1531 free(cd->plain_cipher_mode);
1532 free(cd->plain_uuid);
1534 /* used in loop-AES device only */
1535 free((char*)cd->loopaes_hdr.hash);
1536 free(cd->loopaes_cipher);
1537 free(cd->loopaes_uuid);
1543 int crypt_suspend(struct crypt_device *cd,
1546 crypt_status_info ci;
1547 int r, suspended = 0;
1549 log_dbg("Suspending volume %s.", name);
1551 ci = crypt_status(NULL, name);
1552 if (ci < CRYPT_ACTIVE) {
1553 log_err(cd, _("Volume %s is not active.\n"), name);
1557 if (!cd && dm_init(NULL, 1) < 0)
1560 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1561 NULL, NULL, NULL, NULL, &suspended, NULL);
1566 log_err(cd, _("Volume %s is already suspended.\n"), name);
1571 r = dm_suspend_and_wipe_key(name);
1573 log_err(cd, "Suspend is not supported for device %s.\n", name);
1575 log_err(cd, "Error during suspending device %s.\n", name);
1582 int crypt_resume_by_passphrase(struct crypt_device *cd,
1585 const char *passphrase,
1586 size_t passphrase_size)
1588 struct volume_key *vk = NULL;
1589 int r, suspended = 0;
1591 log_dbg("Resuming volume %s.", name);
1593 if (!isLUKS(cd->type)) {
1594 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1599 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1600 NULL, NULL, NULL, NULL, &suspended, NULL);
1605 log_err(cd, _("Volume %s is not suspended.\n"), name);
1610 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1611 passphrase_size, &cd->hdr, &vk, cd);
1613 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1617 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1619 log_err(cd, "Resume is not supported for device %s.\n", name);
1621 log_err(cd, "Error during resuming device %s.\n", name);
1625 crypt_free_volume_key(vk);
1626 return r < 0 ? r : keyslot;
1629 int crypt_resume_by_keyfile(struct crypt_device *cd,
1632 const char *keyfile,
1633 size_t keyfile_size)
1635 struct volume_key *vk = NULL;
1636 char *passphrase_read = NULL;
1637 size_t passphrase_size_read;
1638 int r, suspended = 0;
1640 log_dbg("Resuming volume %s.", name);
1642 if (!isLUKS(cd->type)) {
1643 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1648 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1649 NULL, NULL, NULL, NULL, &suspended, NULL);
1654 log_err(cd, _("Volume %s is not suspended.\n"), name);
1661 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1662 &passphrase_size_read, keyfile, keyfile_size);
1666 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1667 passphrase_size_read, &cd->hdr, &vk, cd);
1672 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1674 log_err(cd, "Error during resuming device %s.\n", name);
1676 crypt_safe_free(passphrase_read);
1677 crypt_free_volume_key(vk);
1678 return r < 0 ? r : keyslot;
1681 // slot manipulation
1682 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1683 int keyslot, // -1 any
1684 const char *passphrase, // NULL -> terminal
1685 size_t passphrase_size,
1686 const char *new_passphrase, // NULL -> terminal
1687 size_t new_passphrase_size)
1689 struct volume_key *vk = NULL;
1690 char *password = NULL, *new_password = NULL;
1691 size_t passwordLen, new_passwordLen;
1694 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1695 "new passphrase %sprovided.",
1696 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1698 if (!isLUKS(cd->type)) {
1699 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1703 r = keyslot_verify_or_find_empty(cd, &keyslot);
1707 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1708 /* No slots used, try to use pre-generated key in header */
1709 if (cd->volume_key) {
1710 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1711 r = vk ? 0 : -ENOMEM;
1713 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1716 } else if (passphrase) {
1717 /* Passphrase provided, use it to unlock existing keyslot */
1718 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, passphrase,
1719 passphrase_size, &cd->hdr, &vk, cd);
1721 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1722 r = key_from_terminal(cd, _("Enter any passphrase: "),
1723 &password, &passwordLen, 0);
1727 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
1728 passwordLen, &cd->hdr, &vk, cd);
1729 crypt_safe_free(password);
1735 if (new_passphrase) {
1736 new_password = (char *)new_passphrase;
1737 new_passwordLen = new_passphrase_size;
1739 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1740 &new_password, &new_passwordLen, 1);
1745 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1746 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1751 if (!new_passphrase)
1752 crypt_safe_free(new_password);
1753 crypt_free_volume_key(vk);
1754 return r ?: keyslot;
1757 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1759 const char *keyfile,
1760 size_t keyfile_size,
1761 const char *new_keyfile,
1762 size_t new_keyfile_size)
1764 struct volume_key *vk = NULL;
1765 char *password = NULL; size_t passwordLen;
1766 char *new_password = NULL; size_t new_passwordLen;
1769 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1770 keyfile ?: "[none]", new_keyfile ?: "[none]");
1772 if (!isLUKS(cd->type)) {
1773 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1777 r = keyslot_verify_or_find_empty(cd, &keyslot);
1781 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1782 /* No slots used, try to use pre-generated key in header */
1783 if (cd->volume_key) {
1784 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1785 r = vk ? 0 : -ENOMEM;
1787 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1791 /* Read password from file of (if NULL) from terminal */
1793 r = key_from_file(cd, _("Enter any passphrase: "),
1794 &password, &passwordLen,
1795 keyfile, keyfile_size);
1797 r = key_from_terminal(cd, _("Enter any passphrase: "),
1798 &password, &passwordLen, 0);
1802 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password, passwordLen,
1810 r = key_from_file(cd, _("Enter new passphrase for key slot: "),
1811 &new_password, &new_passwordLen, new_keyfile,
1814 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1815 &new_password, &new_passwordLen, 1);
1819 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1820 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1822 crypt_safe_free(password);
1823 crypt_safe_free(new_password);
1824 crypt_free_volume_key(vk);
1825 return r < 0 ? r : keyslot;
1828 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1830 const char *volume_key,
1831 size_t volume_key_size,
1832 const char *passphrase,
1833 size_t passphrase_size)
1835 struct volume_key *vk = NULL;
1837 char *new_password = NULL; size_t new_passwordLen;
1839 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1841 if (!isLUKS(cd->type)) {
1842 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1847 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1848 else if (cd->volume_key)
1849 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1854 r = LUKS_verify_volume_key(&cd->hdr, vk);
1856 log_err(cd, _("Volume key does not match the volume.\n"));
1860 r = keyslot_verify_or_find_empty(cd, &keyslot);
1865 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1866 &new_password, &new_passwordLen, 1);
1869 passphrase = new_password;
1870 passphrase_size = new_passwordLen;
1873 r = LUKS_set_key(cd->device, keyslot, passphrase, passphrase_size,
1874 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1876 crypt_safe_free(new_password);
1877 crypt_free_volume_key(vk);
1878 return (r < 0) ? r : keyslot;
1881 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1883 crypt_keyslot_info ki;
1885 log_dbg("Destroying keyslot %d.", keyslot);
1887 if (!isLUKS(cd->type)) {
1888 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1892 ki = crypt_keyslot_status(cd, keyslot);
1893 if (ki == CRYPT_SLOT_INVALID) {
1894 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1898 if (ki == CRYPT_SLOT_INACTIVE) {
1899 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1903 return LUKS_del_key(cd->device, keyslot, &cd->hdr, cd);
1906 // activation/deactivation of device mapping
1907 int crypt_activate_by_passphrase(struct crypt_device *cd,
1910 const char *passphrase,
1911 size_t passphrase_size,
1914 crypt_status_info ci;
1915 struct volume_key *vk = NULL;
1916 char *read_passphrase = NULL;
1917 size_t passphraseLen = 0;
1920 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1921 name ? "Activating" : "Checking", name ?: "",
1922 keyslot, passphrase ? "" : "[none] ");
1925 ci = crypt_status(NULL, name);
1926 if (ci == CRYPT_INVALID)
1928 else if (ci >= CRYPT_ACTIVE) {
1929 log_err(cd, _("Device %s already exists.\n"), name);
1934 /* plain, use hashed passphrase */
1935 if (isPLAIN(cd->type)) {
1937 r = key_from_terminal(cd, NULL, &read_passphrase,
1941 passphrase = read_passphrase;
1942 passphrase_size = passphraseLen;
1944 r = create_device_helper(cd, name, cd->plain_hdr.hash,
1945 cd->plain_cipher, cd->plain_cipher_mode,
1946 NULL, passphrase, passphrase_size,
1947 cd->volume_key->keylength, 0,
1948 cd->plain_hdr.skip, cd->plain_hdr.offset,
1950 flags & CRYPT_ACTIVATE_READONLY, 0);
1952 } else if (isLUKS(cd->type)) {
1953 /* provided passphrase, do not retry */
1955 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1956 passphrase_size, &cd->hdr, &vk, cd);
1958 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1963 r = open_from_hdr_and_vk(cd, vk, name, flags);
1968 crypt_safe_free(read_passphrase);
1969 crypt_free_volume_key(vk);
1971 return r < 0 ? r : keyslot;
1974 int crypt_activate_by_keyfile(struct crypt_device *cd,
1977 const char *keyfile,
1978 size_t keyfile_size,
1981 crypt_status_info ci;
1982 struct volume_key *vk = NULL;
1983 char *passphrase_read = NULL;
1984 size_t passphrase_size_read;
1985 unsigned int key_count = 0;
1988 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1989 name ?: "", keyslot, keyfile ?: "[none]");
1992 ci = crypt_status(NULL, name);
1993 if (ci == CRYPT_INVALID)
1995 else if (ci >= CRYPT_ACTIVE) {
1996 log_err(cd, _("Device %s already exists.\n"), name);
2004 if (isPLAIN(cd->type)) {
2005 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
2006 &passphrase_size_read, keyfile, keyfile_size);
2009 r = create_device_helper(cd, name, cd->plain_hdr.hash,
2010 cd->plain_cipher, cd->plain_cipher_mode,
2011 NULL, passphrase_read, passphrase_size_read,
2012 cd->volume_key->keylength, 0,
2013 cd->plain_hdr.skip, cd->plain_hdr.offset,
2015 flags & CRYPT_ACTIVATE_READONLY, 0);
2016 } else if (isLUKS(cd->type)) {
2017 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
2018 &passphrase_size_read, keyfile, keyfile_size);
2021 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
2022 passphrase_size_read, &cd->hdr, &vk, cd);
2028 r = open_from_hdr_and_vk(cd, vk, name, flags);
2033 } else if (isLOOPAES(cd->type)) {
2034 r = key_from_file(cd, NULL, &passphrase_read, &passphrase_size_read,
2035 keyfile, keyfile_size);
2038 r = LOOPAES_parse_keyfile(cd, &vk, cd->loopaes_hdr.hash, &key_count,
2039 passphrase_read, passphrase_size_read);
2043 r = LOOPAES_activate(cd, name, cd->loopaes_cipher,
2045 cd->loopaes_hdr.offset,
2046 cd->loopaes_hdr.skip,
2052 crypt_safe_free(passphrase_read);
2053 crypt_free_volume_key(vk);
2058 int crypt_activate_by_volume_key(struct crypt_device *cd,
2060 const char *volume_key,
2061 size_t volume_key_size,
2064 crypt_status_info ci;
2065 struct volume_key *vk;
2068 log_dbg("Activating volume %s by volume key.", name);
2070 /* use key directly, no hash */
2071 if (isPLAIN(cd->type)) {
2072 if (!volume_key || !volume_key_size || !cd->volume_key ||
2073 volume_key_size != cd->volume_key->keylength) {
2074 log_err(cd, _("Incorrect volume key specified for plain device.\n"));
2078 return create_device_helper(cd, name, NULL,
2079 cd->plain_cipher, cd->plain_cipher_mode, NULL, volume_key, volume_key_size,
2080 cd->volume_key->keylength, 0, cd->plain_hdr.skip,
2081 cd->plain_hdr.offset, cd->plain_uuid, flags & CRYPT_ACTIVATE_READONLY, 0);
2084 if (!isLUKS(cd->type)) {
2085 log_err(cd, _("Device type is not properly initialised.\n"));
2090 ci = crypt_status(NULL, name);
2091 if (ci == CRYPT_INVALID)
2093 else if (ci >= CRYPT_ACTIVE) {
2094 log_err(cd, _("Device %s already exists.\n"), name);
2099 /* If key is not provided, try to use internal key */
2101 if (!cd->volume_key) {
2102 log_err(cd, _("Volume key does not match the volume.\n"));
2105 volume_key_size = cd->volume_key->keylength;
2106 volume_key = cd->volume_key->key;
2109 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2112 r = LUKS_verify_volume_key(&cd->hdr, vk);
2115 log_err(cd, _("Volume key does not match the volume.\n"));
2118 r = open_from_hdr_and_vk(cd, vk, name, flags);
2120 crypt_free_volume_key(vk);
2125 int crypt_deactivate(struct crypt_device *cd, const char *name)
2132 log_dbg("Deactivating volume %s.", name);
2134 if (!cd && dm_init(NULL, 1) < 0)
2137 switch (crypt_status(cd, name)) {
2139 r = dm_remove_device(name, 0, 0);
2142 log_err(cd, _("Device %s is busy.\n"), name);
2145 case CRYPT_INACTIVE:
2146 log_err(cd, _("Device %s is not active.\n"), name);
2150 log_err(cd, _("Invalid device %s.\n"), name);
2160 int crypt_volume_key_get(struct crypt_device *cd,
2163 size_t *volume_key_size,
2164 const char *passphrase,
2165 size_t passphrase_size)
2167 struct volume_key *vk;
2168 char *processed_key = NULL;
2172 key_len = crypt_get_volume_key_size(cd);
2173 if (key_len > *volume_key_size) {
2174 log_err(cd, _("Volume key buffer too small.\n"));
2178 if (isPLAIN(cd->type) && cd->plain_hdr.hash) {
2179 processed_key = process_key(cd, cd->plain_hdr.hash, NULL, key_len,
2180 passphrase, passphrase_size);
2181 if (!processed_key) {
2182 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
2185 memcpy(volume_key, processed_key, key_len);
2186 *volume_key_size = key_len;
2187 crypt_safe_free(processed_key);
2191 if (isLUKS(cd->type)) {
2192 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
2193 passphrase_size, &cd->hdr, &vk, cd);
2196 memcpy(volume_key, vk->key, vk->keylength);
2197 *volume_key_size = vk->keylength;
2200 crypt_free_volume_key(vk);
2204 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
2208 int crypt_volume_key_verify(struct crypt_device *cd,
2209 const char *volume_key,
2210 size_t volume_key_size)
2212 struct volume_key *vk;
2215 if (!isLUKS(cd->type)) {
2216 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2220 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2224 r = LUKS_verify_volume_key(&cd->hdr, vk);
2227 log_err(cd, _("Volume key does not match the volume.\n"));
2229 crypt_free_volume_key(vk);
2234 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
2236 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
2237 cd->timeout = timeout_sec;
2240 void crypt_set_password_retry(struct crypt_device *cd, int tries)
2242 log_dbg("Password retry count set to %d.", tries);
2246 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2248 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
2249 cd->iteration_time = iteration_time_ms;
2252 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
2254 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
2255 cd->password_verify = password_verify ? 1 : 0;
2258 void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
2261 case CRYPT_RNG_URANDOM:
2262 case CRYPT_RNG_RANDOM:
2263 log_dbg("RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
2264 cd->rng_type = rng_type;
2268 int crypt_get_rng_type(struct crypt_device *cd)
2273 return cd->rng_type;
2276 int crypt_memory_lock(struct crypt_device *cd, int lock)
2278 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
2282 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
2286 if (!cd && dm_init(NULL, 1) < 0)
2287 return CRYPT_INVALID;
2289 r = dm_status_device(name);
2294 if (r < 0 && r != -ENODEV)
2295 return CRYPT_INVALID;
2298 return CRYPT_ACTIVE;
2303 return CRYPT_INACTIVE;
2306 static void hexprintICB(struct crypt_device *cd, char *d, int n)
2309 for(i = 0; i < n; i++)
2310 log_std(cd, "%02hhx ", (char)d[i]);
2313 int crypt_dump(struct crypt_device *cd)
2316 if (!isLUKS(cd->type)) { //FIXME
2317 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2321 log_std(cd, "LUKS header information for %s\n\n", cd->device);
2322 log_std(cd, "Version: \t%d\n", cd->hdr.version);
2323 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
2324 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
2325 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec);
2326 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
2327 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
2328 log_std(cd, "MK digest: \t");
2329 hexprintICB(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE);
2331 log_std(cd, "MK salt: \t");
2332 hexprintICB(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2);
2333 log_std(cd, "\n \t");
2334 hexprintICB(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
2336 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
2337 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
2338 for(i = 0; i < LUKS_NUMKEYS; i++) {
2339 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
2340 log_std(cd, "Key Slot %d: ENABLED\n",i);
2341 log_std(cd, "\tIterations: \t%d\n",
2342 cd->hdr.keyblock[i].passwordIterations);
2343 log_std(cd, "\tSalt: \t");
2344 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt,
2346 log_std(cd, "\n\t \t");
2347 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt +
2348 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
2351 log_std(cd, "\tKey material offset:\t%d\n",
2352 cd->hdr.keyblock[i].keyMaterialOffset);
2353 log_std(cd, "\tAF stripes: \t%d\n",
2354 cd->hdr.keyblock[i].stripes);
2357 log_std(cd, "Key Slot %d: DISABLED\n", i);
2363 const char *crypt_get_cipher(struct crypt_device *cd)
2365 if (isPLAIN(cd->type))
2366 return cd->plain_cipher;
2368 if (isLUKS(cd->type))
2369 return cd->hdr.cipherName;
2371 if (isLOOPAES(cd->type))
2372 return cd->loopaes_cipher;
2377 const char *crypt_get_cipher_mode(struct crypt_device *cd)
2379 if (isPLAIN(cd->type))
2380 return cd->plain_cipher_mode;
2382 if (isLUKS(cd->type))
2383 return cd->hdr.cipherMode;
2385 if (isLOOPAES(cd->type))
2386 return cd->loopaes_cipher_mode;
2391 const char *crypt_get_uuid(struct crypt_device *cd)
2393 if (isLUKS(cd->type))
2394 return cd->hdr.uuid;
2396 if (isPLAIN(cd->type))
2397 return cd->plain_uuid;
2399 if (isLOOPAES(cd->type))
2400 return cd->loopaes_uuid;
2405 const char *crypt_get_device_name(struct crypt_device *cd)
2410 int crypt_get_volume_key_size(struct crypt_device *cd)
2412 if (isPLAIN(cd->type) && cd->volume_key)
2413 return cd->volume_key->keylength;
2415 if (isLUKS(cd->type))
2416 return cd->hdr.keyBytes;
2418 if (isLOOPAES(cd->type))
2419 return cd->loopaes_key_size;
2424 uint64_t crypt_get_data_offset(struct crypt_device *cd)
2426 if (isPLAIN(cd->type))
2427 return cd->plain_hdr.offset;
2429 if (isLUKS(cd->type))
2430 return cd->hdr.payloadOffset;
2432 if (isLOOPAES(cd->type))
2433 return cd->loopaes_hdr.offset;
2438 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
2440 if (!isLUKS(cd->type)) {
2441 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2442 return CRYPT_SLOT_INVALID;
2445 return LUKS_keyslot_info(&cd->hdr, keyslot);
2448 int crypt_keyslot_max(const char *type)
2450 if (type && isLUKS(type))
2451 return LUKS_NUMKEYS;
2456 const char *crypt_get_type(struct crypt_device *cd)
2461 int crypt_get_active_device(struct crypt_device *cd __attribute__((unused)),
2463 struct crypt_active_device *cad)
2467 r = dm_query_device(name, NULL, &cad->size, &cad->iv_offset, &cad->offset,
2468 NULL, NULL, NULL, &readonly, NULL, NULL);
2472 cad->flags = readonly ? CRYPT_ACTIVATE_READONLY : 0;