2 * libcryptsetup - cryptsetup library
4 * Copyright (C) 2004, Christophe Saout <christophe@saout.de>
5 * Copyright (C) 2004-2007, Clemens Fruhwirth <clemens@endorphin.org>
6 * Copyright (C) 2009-2011, Red Hat, Inc. All rights reserved.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "libcryptsetup.h"
33 #include "crypto_backend.h"
41 struct volume_key *volume_key;
43 uint64_t iteration_time;
48 /* used in CRYPT_LUKS1 */
50 uint64_t PBKDF2_per_sec;
52 /* used in CRYPT_PLAIN */
53 struct crypt_params_plain plain_hdr;
55 char *plain_cipher_mode;
58 /* used in CRYPT_LOOPAES */
59 struct crypt_params_loopaes loopaes_hdr;
61 char *loopaes_cipher_mode;
63 unsigned int loopaes_key_size;
65 /* callbacks definitions */
66 void (*log)(int level, const char *msg, void *usrptr);
68 int (*confirm)(const char *msg, void *usrptr);
70 int (*password)(const char *msg, char *buf, size_t length, void *usrptr);
71 void *password_usrptr;
75 static void (*_default_log)(int level, const char *msg, void *usrptr) = NULL;
76 static int _debug_level = 0;
78 void crypt_set_debug_level(int level)
83 int crypt_get_debug_level()
88 void crypt_log(struct crypt_device *cd, int level, const char *msg)
91 cd->log(level, msg, cd->log_usrptr);
92 else if (_default_log)
93 _default_log(level, msg, NULL);
96 void logger(struct crypt_device *cd, int level, const char *file,
97 int line, const char *format, ...)
102 va_start(argp, format);
104 if (vasprintf(&target, format, argp) > 0) {
106 crypt_log(cd, level, target);
108 } else if (_debug_level)
109 printf("# %s:%d %s\n", file ?: "?", line, target);
111 } else if (_debug_level)
112 printf("# %s\n", target);
120 static int init_crypto(struct crypt_device *ctx)
124 r = crypt_random_init(ctx);
126 log_err(ctx, _("Cannot initialize crypto RNG backend.\n"));
130 r = crypt_backend_init(ctx);
132 log_err(ctx, _("Cannot initialize crypto backend.\n"));
138 * Password processing behaviour matrix of process_key
140 * from binary file: check if there is sufficently large key material
141 * interactive & from fd: hash if requested, otherwise crop or pad with '0'
143 static char *process_key(struct crypt_device *cd, const char *hash_name,
144 const char *key_file, size_t key_size,
145 const char *pass, size_t passLen)
153 key = crypt_safe_alloc(key_size);
154 memset(key, 0, key_size);
156 /* key is coming from binary file */
157 if (key_file && strcmp(key_file, "-")) {
158 if(passLen < key_size) {
159 log_err(cd, _("Cannot not read %d bytes from key file %s.\n"),
161 crypt_safe_free(key);
164 memcpy(key, pass, key_size);
168 /* key is coming from tty, fd or binary stdin */
170 r = crypt_plain_hash(cd, hash_name, key, key_size, pass, passLen);
173 log_err(cd, _("Hash algorithm %s not supported.\n"),
176 log_err(cd, _("Key processing error (using hash %s).\n"),
178 crypt_safe_free(key);
181 } else if (passLen > key_size) {
182 memcpy(key, pass, key_size);
184 memcpy(key, pass, passLen);
190 static int isPLAIN(const char *type)
192 return (type && !strcmp(CRYPT_PLAIN, type));
195 static int isLUKS(const char *type)
197 return (type && !strcmp(CRYPT_LUKS1, type));
200 static int isLOOPAES(const char *type)
202 return (type && !strcmp(CRYPT_LOOPAES, type));
205 /* keyslot helpers */
206 static int keyslot_verify_or_find_empty(struct crypt_device *cd, int *keyslot)
208 if (*keyslot == CRYPT_ANY_SLOT) {
209 *keyslot = LUKS_keyslot_find_empty(&cd->hdr);
211 log_err(cd, _("All key slots full.\n"));
216 switch (LUKS_keyslot_info(&cd->hdr, *keyslot)) {
217 case CRYPT_SLOT_INVALID:
218 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
219 *keyslot, LUKS_NUMKEYS - 1);
221 case CRYPT_SLOT_INACTIVE:
224 log_err(cd, _("Key slot %d is full, please select another one.\n"),
232 static int verify_other_keyslot(struct crypt_device *cd,
233 const char *key_file,
236 struct volume_key *vk = NULL;
237 crypt_keyslot_info ki;
239 char *password = NULL;
240 unsigned int passwordLen;
242 r = crypt_get_key(_("Enter any remaining LUKS passphrase: "),
243 &password, &passwordLen, 0, key_file, cd->timeout,
244 cd->password_verify, cd);
248 ki = crypt_keyslot_status(cd, keyIndex);
249 if (ki == CRYPT_SLOT_ACTIVE) /* Not last slot */
250 LUKS_keyslot_set(&cd->hdr, keyIndex, 0);
252 openedIndex = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT,
253 password, passwordLen,
256 if (ki == CRYPT_SLOT_ACTIVE)
257 LUKS_keyslot_set(&cd->hdr, keyIndex, 1);
262 log_verbose(cd, _("Key slot %d verified.\n"), openedIndex);
264 crypt_free_volume_key(vk);
265 crypt_safe_free(password);
269 static int find_keyslot_by_passphrase(struct crypt_device *cd,
270 const char *key_file,
273 struct volume_key *vk = NULL;
274 char *password = NULL;
275 unsigned int passwordLen;
278 r = crypt_get_key(message,&password,&passwordLen, 0, key_file,
279 cd->timeout, cd->password_verify, cd);
283 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
284 passwordLen, &cd->hdr, &vk, cd);
286 crypt_free_volume_key(vk);
287 crypt_safe_free(password);
291 static int luks_remove_helper(struct crypt_device *cd,
293 const char *other_key_file,
294 const char *key_file,
297 crypt_keyslot_info ki;
300 if (key_slot == CRYPT_ANY_SLOT) {
301 key_slot = find_keyslot_by_passphrase(cd, key_file,
302 _("Enter LUKS passphrase to be deleted: "));
308 log_std(cd, _("Key slot %d selected for deletion.\n"), key_slot);
311 ki = crypt_keyslot_status(cd, key_slot);
312 if (ki == CRYPT_SLOT_INVALID) {
313 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
314 key_slot, LUKS_NUMKEYS - 1);
318 if (ki <= CRYPT_SLOT_INACTIVE) {
319 log_err(cd, _("Key %d not active. Can't wipe.\n"), key_slot);
324 if (ki == CRYPT_SLOT_ACTIVE_LAST && cd->confirm &&
325 !(cd->confirm(_("This is the last keyslot."
326 " Device will become unusable after purging this key."),
327 cd->confirm_usrptr))) {
333 r = verify_other_keyslot(cd, other_key_file, key_slot);
338 r = crypt_keyslot_destroy(cd, key_slot);
340 return (r < 0) ? r : 0;
343 static int create_device_helper(struct crypt_device *cd,
347 const char *cipher_mode,
348 const char *key_file,
360 crypt_status_info ci;
361 char *dm_cipher = NULL;
362 char *processed_key = NULL;
368 ci = crypt_status(cd, name);
369 if (ci == CRYPT_INVALID)
372 if (reload && ci < CRYPT_ACTIVE)
375 if (!reload && ci >= CRYPT_ACTIVE) {
376 log_err(cd, _("Device %s already exists.\n"), name);
380 if (key_size < 0 || key_size > 1024) {
381 log_err(cd, _("Invalid key size %d.\n"), key_size);
385 r = device_check_and_adjust(cd, cd->device, !reload, &size, &offset, &read_only);
389 if (cipher_mode && asprintf(&dm_cipher, "%s-%s", cipher, cipher_mode) < 0)
392 processed_key = process_key(cd, hash, key_file, key_size, key, keyLen);
393 if (!processed_key) {
398 r = dm_create_device(name, cd->device, dm_cipher ?: cipher, cd->type, uuid, size, skip, offset,
399 key_size, processed_key, read_only, reload);
402 crypt_safe_free(processed_key);
406 static int open_from_hdr_and_vk(struct crypt_device *cd,
407 struct volume_key *vk,
411 uint64_t size, offset;
413 int read_only, no_uuid, r;
416 offset = crypt_get_data_offset(cd);
417 read_only = flags & CRYPT_ACTIVATE_READONLY;
418 no_uuid = flags & CRYPT_ACTIVATE_NO_UUID;
420 r = device_check_and_adjust(cd, cd->device, 1, &size, &offset, &read_only);
424 if (asprintf(&cipher, "%s-%s", crypt_get_cipher(cd),
425 crypt_get_cipher_mode(cd)) < 0)
428 r = dm_create_device(name, cd->device, cipher, cd->type,
429 no_uuid ? NULL : crypt_get_uuid(cd),
430 size, 0, offset, vk->keylength, vk->key,
436 static void log_wrapper(int level, const char *msg, void *usrptr)
438 void (*xlog)(int level, char *msg) = usrptr;
439 xlog(level, (char *)msg);
442 static int yesDialog_wrapper(const char *msg, void *usrptr)
444 int (*xyesDialog)(char *msg) = usrptr;
445 return xyesDialog((char*)msg);
448 int crypt_confirm(struct crypt_device *cd, const char *msg)
450 if (!cd || !cd->confirm)
453 return cd->confirm(msg, cd->confirm_usrptr);
456 static int key_from_terminal(struct crypt_device *cd, char *msg, char **key,
457 unsigned int *key_len, int force_verify)
463 if(!msg && asprintf(&prompt, _("Enter passphrase for %s: "),
464 cd->backing_file ?: cd->device) < 0)
471 *key = crypt_safe_alloc(MAX_TTY_PASSWORD_LEN);
476 r = cd->password(msg, *key, MAX_TTY_PASSWORD_LEN, cd->password_usrptr);
478 crypt_safe_free(*key);
483 r = crypt_get_key(msg, key, key_len, 0, NULL, cd->timeout,
484 (force_verify || cd->password_verify), cd);
487 return (r < 0) ? r: 0;
490 static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslot,
491 struct volume_key **vk)
493 char *passphrase_read = NULL;
494 unsigned int passphrase_size_read;
495 int r = -EINVAL, tries = cd->tries;
499 crypt_free_volume_key(*vk);
502 r = key_from_terminal(cd, NULL, &passphrase_read,
503 &passphrase_size_read, 0);
507 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
508 passphrase_size_read, &cd->hdr, vk, cd);
509 crypt_safe_free(passphrase_read);
510 passphrase_read = NULL;
511 } while (r == -EPERM && (--tries > 0));
514 crypt_free_volume_key(*vk);
518 crypt_safe_free(passphrase_read);
522 static int key_from_file(struct crypt_device *cd, char *msg,
523 char **key, unsigned int *key_len,
524 const char *key_file, size_t key_size)
526 return crypt_get_key(msg, key, key_len, key_size, key_file,
530 static int _crypt_init(struct crypt_device **cd,
532 struct crypt_options *options,
533 int load, int need_dm)
537 /* if it is plain device and mapping table is being reloaded
538 initialize it by name*/
539 init_by_name = (type && !strcmp(type, CRYPT_PLAIN) && load);
541 /* Some of old API calls do not require DM in kernel,
542 fake initialisation by initialise it with kernel_check disabled */
544 (void)dm_init(NULL, 0);
546 r = crypt_init_by_name(cd, options->name);
548 r = crypt_init(cd, options->device);
555 crypt_set_log_callback(*cd, log_wrapper, options->icb->log);
556 crypt_set_confirm_callback(*cd, yesDialog_wrapper, options->icb->yesDialog);
558 crypt_set_timeout(*cd, options->timeout);
559 crypt_set_password_retry(*cd, options->tries);
560 crypt_set_iterarion_time(*cd, options->iteration_time ?: 1000);
561 crypt_set_password_verify(*cd, options->flags & CRYPT_FLAG_VERIFY);
563 if (load && !init_by_name)
564 r = crypt_load(*cd, type, NULL);
566 if (!r && type && !(*cd)->type) {
567 (*cd)->type = strdup(type);
578 void crypt_set_log_callback(struct crypt_device *cd,
579 void (*log)(int level, const char *msg, void *usrptr),
586 cd->log_usrptr = usrptr;
590 void crypt_set_confirm_callback(struct crypt_device *cd,
591 int (*confirm)(const char *msg, void *usrptr),
594 cd->confirm = confirm;
595 cd->confirm_usrptr = usrptr;
598 void crypt_set_password_callback(struct crypt_device *cd,
599 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
602 cd->password = password;
603 cd->password_usrptr = usrptr;
606 /* OPTIONS: name, cipher, device, hash, key_file, key_size, key_slot,
607 * offset, size, skip, timeout, tries, passphrase_fd (ignored),
609 static int crypt_create_and_update_device(struct crypt_options *options, int update)
611 struct crypt_device *cd = NULL;
616 r = _crypt_init(&cd, CRYPT_PLAIN, options, 0, 1);
620 r = crypt_get_key(_("Enter passphrase: "), &key, &keyLen, options->key_size,
621 options->key_file, cd->timeout, cd->password_verify, cd);
623 r = create_device_helper(cd, options->name, options->hash,
624 options->cipher, NULL, options->key_file, key, keyLen,
625 options->key_size, options->size, options->skip,
626 options->offset, NULL, options->flags & CRYPT_FLAG_READONLY,
627 options->flags, update);
629 crypt_safe_free(key);
634 int crypt_create_device(struct crypt_options *options)
636 return crypt_create_and_update_device(options, 0);
639 int crypt_update_device(struct crypt_options *options)
641 return crypt_create_and_update_device(options, 1);
644 /* OPTIONS: name, size, icb */
645 int crypt_resize_device(struct crypt_options *options)
647 struct crypt_device *cd = NULL;
648 char *device = NULL, *cipher = NULL, *uuid = NULL, *key = NULL;
650 uint64_t size, skip, offset;
651 int key_size, read_only, r;
653 log_dbg("Resizing device %s to %" PRIu64 " sectors.", options->name, options->size);
655 if (dm_init(NULL, 1) < 0)
658 r = dm_query_device(options->name, &device, &size, &skip, &offset,
659 &cipher, &key_size, &key, &read_only, NULL, &uuid);
661 log_err(NULL, _("Device %s is not active.\n"), options->name);
665 /* Try to determine type of device from UUID */
668 if (!strncmp(uuid, CRYPT_PLAIN, strlen(CRYPT_PLAIN))) {
672 } else if (!strncmp(uuid, CRYPT_LUKS1, strlen(CRYPT_LUKS1)))
676 if (!options->device)
677 options->device = device;
679 r = _crypt_init(&cd, type, options, 1, 1);
683 size = options->size;
684 r = device_check_and_adjust(cd, device, 0, &size, &offset, &read_only);
688 r = dm_create_device(options->name, device, cipher, type,
689 crypt_get_uuid(cd), size, skip, offset,
690 key_size, key, read_only, 1);
692 crypt_safe_free(key);
694 if (options->device == device)
695 options->device = NULL;
703 /* OPTIONS: name, icb */
704 int crypt_query_device(struct crypt_options *options)
708 log_dbg("Query device %s.", options->name);
710 if (dm_init(NULL, 1) < 0)
713 r = dm_status_device(options->name);
717 r = dm_query_device(options->name, (char **)&options->device, &options->size,
718 &options->skip, &options->offset, (char **)&options->cipher,
719 &options->key_size, NULL, &read_only, NULL, NULL);
722 options->flags |= CRYPT_FLAG_READONLY;
724 options->flags |= CRYPT_FLAG_FREE_DEVICE;
725 options->flags |= CRYPT_FLAG_FREE_CIPHER;
737 /* OPTIONS: name, icb */
738 int crypt_remove_device(struct crypt_options *options)
740 struct crypt_device *cd = NULL;
743 r = crypt_init_by_name(&cd, options->name);
745 r = crypt_deactivate(cd, options->name);
752 /* OPTIONS: device, cipher, hash, align_payload, key_size (master key), key_slot
753 * new_key_file, iteration_time, timeout, flags, icb */
754 int crypt_luksFormat(struct crypt_options *options)
756 char cipherName[LUKS_CIPHERNAME_L];
757 char cipherMode[LUKS_CIPHERMODE_L];
759 unsigned int passwordLen;
760 struct crypt_device *cd = NULL;
761 struct crypt_params_luks1 cp = {
762 .hash = options->hash,
763 .data_alignment = options->align_payload
767 r = crypt_parse_name_and_mode(options->cipher, cipherName, NULL, cipherMode);
769 log_err(cd, _("No known cipher specification pattern detected.\n"));
773 if ((r = _crypt_init(&cd, CRYPT_LUKS1, options, 0, 1)))
776 if (options->key_slot >= LUKS_NUMKEYS && options->key_slot != CRYPT_ANY_SLOT) {
777 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
778 options->key_slot, LUKS_NUMKEYS - 1);
783 r = crypt_get_key(_("Enter LUKS passphrase: "), &password, &passwordLen, 0,
784 options->new_key_file, cd->timeout, cd->password_verify, cd);
789 r = crypt_format(cd, CRYPT_LUKS1, cipherName, cipherMode,
790 NULL, NULL, options->key_size, &cp);
794 /* Add keyslot using internally stored volume key generated during format */
795 r = crypt_keyslot_add_by_volume_key(cd, options->key_slot, NULL, 0,
796 password, passwordLen);
799 crypt_safe_free(password);
800 return (r < 0) ? r : 0;
803 /* OPTIONS: name, device, key_size, key_file, timeout, tries, flags, icb */
804 int crypt_luksOpen(struct crypt_options *options)
806 struct crypt_device *cd = NULL;
813 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
817 if (options->flags & CRYPT_FLAG_READONLY)
818 flags |= CRYPT_ACTIVATE_READONLY;
820 if (options->flags & CRYPT_FLAG_NON_EXCLUSIVE_ACCESS)
821 flags |= CRYPT_ACTIVATE_NO_UUID;
823 if (options->key_file)
824 r = crypt_activate_by_keyfile(cd, options->name,
825 CRYPT_ANY_SLOT, options->key_file, options->key_size,
828 r = crypt_activate_by_passphrase(cd, options->name,
829 CRYPT_ANY_SLOT, options->passphrase,
830 options->passphrase ? strlen(options->passphrase) : 0,
834 return (r < 0) ? r : 0;
837 /* OPTIONS: device, keys_slot, key_file, timeout, flags, icb */
838 int crypt_luksKillSlot(struct crypt_options *options)
840 struct crypt_device *cd = NULL;
843 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
847 r = luks_remove_helper(cd, options->key_slot, options->key_file, NULL,
848 options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
851 return (r < 0) ? r : 0;
854 /* OPTIONS: device, new_key_file, key_file, timeout, flags, icb */
855 int crypt_luksRemoveKey(struct crypt_options *options)
857 struct crypt_device *cd = NULL;
860 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
864 r = luks_remove_helper(cd, CRYPT_ANY_SLOT, options->key_file, options->new_key_file,
865 options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
868 return (r < 0) ? r : 0;
872 /* OPTIONS: device, new_key_file, key_file, key_slot, flags,
873 iteration_time, timeout, icb */
874 int crypt_luksAddKey(struct crypt_options *options)
876 struct crypt_device *cd = NULL;
879 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
883 if (options->key_file || options->new_key_file)
884 r = crypt_keyslot_add_by_keyfile(cd, options->key_slot,
885 options->key_file, 0,
886 options->new_key_file, 0);
888 r = crypt_keyslot_add_by_passphrase(cd, options->key_slot,
892 return (r < 0) ? r : 0;
895 /* OPTIONS: device, icb */
896 int crypt_luksUUID(struct crypt_options *options)
898 struct crypt_device *cd = NULL;
902 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
906 uuid = (char *)crypt_get_uuid(cd);
907 log_std(cd, uuid ?: "");
913 /* OPTIONS: device, icb */
914 int crypt_isLuks(struct crypt_options *options)
916 struct crypt_device *cd = NULL;
919 log_dbg("Check device %s for LUKS header.", options->device);
925 r = crypt_init(&cd, options->device);
929 /* Do print fail here, no need to crypt_load() */
930 r = LUKS_read_phdr(cd->device, &cd->hdr, 0, cd) ? -EINVAL : 0;
936 /* OPTIONS: device, icb */
937 int crypt_luksDump(struct crypt_options *options)
939 struct crypt_device *cd = NULL;
942 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
952 void crypt_get_error(char *buf, size_t size)
954 const char *error = get_error();
956 if (!buf || size < 1)
959 strncpy(buf, error, size - 1);
960 buf[size - 1] = '\0';
966 void crypt_put_options(struct crypt_options *options)
968 if (options->flags & CRYPT_FLAG_FREE_DEVICE) {
969 free((char *)options->device);
970 options->device = NULL;
971 options->flags &= ~CRYPT_FLAG_FREE_DEVICE;
973 if (options->flags & CRYPT_FLAG_FREE_CIPHER) {
974 free((char *)options->cipher);
975 options->cipher = NULL;
976 options->flags &= ~CRYPT_FLAG_FREE_CIPHER;
980 const char *crypt_get_dir(void)
985 /////////////////////////////////
990 int crypt_init(struct crypt_device **cd, const char *device)
992 struct crypt_device *h = NULL;
998 log_dbg("Allocating crypt device %s context.", device);
1000 if (!(h = malloc(sizeof(struct crypt_device))))
1003 memset(h, 0, sizeof(*h));
1007 r = device_ready(NULL, device, O_RDONLY);
1008 if (r == -ENOTBLK) {
1009 h->device = crypt_loop_get_device();
1010 log_dbg("Not a block device, %s%s.",
1011 h->device ? "using free loop device " :
1012 "no free loop device found",
1015 log_err(NULL, _("Cannot find a free loopback device.\n"));
1020 /* Keep the loop open, dettached on last close. */
1021 h->loop_fd = crypt_loop_attach(h->device, device, 0, 1, &readonly);
1022 if (h->loop_fd == -1) {
1023 log_err(NULL, _("Attaching loopback device failed "
1024 "(loop device with autoclear flag is required).\n"));
1029 h->backing_file = crypt_loop_backing_file(h->device);
1030 r = device_ready(NULL, h->device, O_RDONLY);
1038 if (!h->device && device && !(h->device = strdup(device))) {
1043 if (dm_init(h, 1) < 0) {
1048 h->iteration_time = 1000;
1049 h->password_verify = 0;
1051 h->rng_type = crypt_random_default_key_rng();
1057 if (h->loop_fd != -1)
1060 free(h->backing_file);
1066 int crypt_init_by_name(struct crypt_device **cd, const char *name)
1068 crypt_status_info ci;
1069 struct crypt_active_device cad;
1070 char *device = NULL, *cipher_full = NULL, *device_uuid = NULL;
1071 char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
1073 int key_size = 0, key_nums, r;
1076 log_dbg("Allocating crypt device context by device %s.", name);
1078 ci = crypt_status(NULL, name);
1079 if (ci == CRYPT_INVALID)
1082 if (ci < CRYPT_ACTIVE) {
1083 log_err(NULL, _("Device %s is not active.\n"), name);
1087 r = dm_query_device(name, &device, &cad.size, &cad.iv_offset, &cad.offset,
1088 &cipher_full, &key_size, &key, NULL, NULL,
1094 r = crypt_init(cd, device);
1096 /* Underlying device disappeared but mapping still active */
1097 if (!device || r == -ENOTBLK)
1098 log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
1101 /* Underlying device is not readable but crypt mapping exists */
1102 if (r == -ENOTBLK) {
1105 r = crypt_init(cd, NULL);
1111 /* Try to initialise basic parameters from active device */
1113 if (!(*cd)->backing_file && device && crypt_loop_device(device) &&
1114 !((*cd)->backing_file = crypt_loop_backing_file(device))) {
1120 if (!strncmp(CRYPT_PLAIN, device_uuid, sizeof(CRYPT_PLAIN)-1)) {
1121 (*cd)->type = strdup(CRYPT_PLAIN);
1122 (*cd)->plain_uuid = strdup(device_uuid);
1123 (*cd)->plain_hdr.hash = NULL; /* no way to get this */
1124 (*cd)->plain_hdr.offset = cad.offset;
1125 (*cd)->plain_hdr.skip = cad.iv_offset;
1126 (*cd)->volume_key = crypt_alloc_volume_key(key_size, key);
1127 if (!(*cd)->volume_key) {
1132 r = crypt_parse_name_and_mode(cipher_full, cipher, NULL, cipher_mode);
1134 (*cd)->plain_cipher = strdup(cipher);
1135 (*cd)->plain_cipher_mode = strdup(cipher_mode);
1137 } else if (!strncmp(CRYPT_LOOPAES, device_uuid, sizeof(CRYPT_LOOPAES)-1)) {
1138 (*cd)->type = strdup(CRYPT_LOOPAES);
1139 (*cd)->loopaes_uuid = strdup(device_uuid);
1140 (*cd)->loopaes_hdr.offset = cad.offset;
1142 r = crypt_parse_name_and_mode(cipher_full, cipher,
1143 &key_nums, cipher_mode);
1145 (*cd)->loopaes_cipher = strdup(cipher);
1146 (*cd)->loopaes_cipher_mode = strdup(cipher_mode);
1147 /* version 3 uses last key for IV */
1148 if (key_size % key_nums)
1150 (*cd)->loopaes_key_size = key_size / key_nums;
1152 } else if (!strncmp(CRYPT_LUKS1, device_uuid, sizeof(CRYPT_LUKS1)-1)) {
1154 if (crypt_load(*cd, CRYPT_LUKS1, NULL) < 0 ||
1155 crypt_volume_key_verify(*cd, key, key_size) < 0) {
1156 log_dbg("LUKS device header does not match active device.");
1160 (*cd)->volume_key = crypt_alloc_volume_key(key_size, key);
1161 if (!(*cd)->volume_key) {
1168 log_dbg("Active device has no UUID set, some parameters are not set.");
1175 crypt_safe_free(key);
1182 static int _crypt_format_plain(struct crypt_device *cd,
1184 const char *cipher_mode,
1186 size_t volume_key_size,
1187 struct crypt_params_plain *params)
1189 if (!cipher || !cipher_mode) {
1190 log_err(cd, _("Invalid plain crypt parameters.\n"));
1194 if (volume_key_size > 1024) {
1195 log_err(cd, _("Invalid key size.\n"));
1199 cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
1200 if (!cd->volume_key)
1203 cd->plain_cipher = strdup(cipher);
1204 cd->plain_cipher_mode = strdup(cipher_mode);
1207 cd->plain_uuid = strdup(uuid);
1209 if (params && params->hash)
1210 cd->plain_hdr.hash = strdup(params->hash);
1212 cd->plain_hdr.offset = params ? params->offset : 0;
1213 cd->plain_hdr.skip = params ? params->skip : 0;
1215 if (!cd->plain_cipher || !cd->plain_cipher_mode)
1221 static int _crypt_format_luks1(struct crypt_device *cd,
1223 const char *cipher_mode,
1225 const char *volume_key,
1226 size_t volume_key_size,
1227 struct crypt_params_luks1 *params)
1230 unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
1231 unsigned long alignment_offset = 0;
1234 log_err(cd, _("Can't format LUKS without device.\n"));
1239 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
1242 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
1247 if (params && params->data_alignment)
1248 required_alignment = params->data_alignment * SECTOR_SIZE;
1250 get_topology_alignment(cd->device, &required_alignment,
1251 &alignment_offset, DEFAULT_DISK_ALIGNMENT);
1253 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
1254 (params && params->hash) ? params->hash : "sha1",
1256 required_alignment / SECTOR_SIZE,
1257 alignment_offset / SECTOR_SIZE,
1258 cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1262 /* Wipe first 8 sectors - fs magic numbers etc. */
1263 r = wipe_device_header(cd->device, 8);
1266 log_err(cd, _("Cannot format device %s which is still in use.\n"),
1269 log_err(cd, _("Cannot wipe header on device %s.\n"),
1275 r = LUKS_write_phdr(cd->device, &cd->hdr, cd);
1280 static int _crypt_format_loopaes(struct crypt_device *cd,
1283 size_t volume_key_size,
1284 struct crypt_params_loopaes *params)
1287 log_err(cd, _("Can't format LOOPAES without device.\n"));
1291 if (volume_key_size > 1024) {
1292 log_err(cd, _("Invalid key size.\n"));
1296 cd->loopaes_key_size = volume_key_size;
1298 cd->loopaes_cipher = strdup(cipher ?: DEFAULT_LOOPAES_CIPHER);
1301 cd->loopaes_uuid = strdup(uuid);
1303 if (params && params->hash)
1304 cd->loopaes_hdr.hash = strdup(params->hash);
1306 cd->loopaes_hdr.offset = params ? params->offset : 0;
1311 int crypt_format(struct crypt_device *cd,
1314 const char *cipher_mode,
1316 const char *volume_key,
1317 size_t volume_key_size,
1325 log_dbg("Formatting device %s as type %s.", cd->device ?: "(none)", type);
1327 r = init_crypto(cd);
1332 r = _crypt_format_plain(cd, cipher, cipher_mode,
1333 uuid, volume_key_size, params);
1334 else if (isLUKS(type))
1335 r = _crypt_format_luks1(cd, cipher, cipher_mode,
1336 uuid, volume_key, volume_key_size, params);
1337 else if (isLOOPAES(type))
1338 r = _crypt_format_loopaes(cd, cipher, uuid, volume_key_size, params);
1340 /* FIXME: allow plugins here? */
1341 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
1345 if (!r && !(cd->type = strdup(type)))
1349 crypt_free_volume_key(cd->volume_key);
1350 cd->volume_key = NULL;
1356 int crypt_load(struct crypt_device *cd,
1357 const char *requested_type,
1360 struct luks_phdr hdr;
1363 log_dbg("Trying to load %s crypt type from device %s.",
1364 requested_type ?: "any", cd->device ?: "(none)");
1369 if (requested_type && !isLUKS(requested_type))
1372 r = init_crypto(cd);
1376 r = LUKS_read_phdr(cd->device, &hdr, 1, cd);
1379 memcpy(&cd->hdr, &hdr, sizeof(hdr));
1380 cd->type = strdup(CRYPT_LUKS1);
1388 int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
1390 char *device = NULL, *cipher = NULL, *uuid = NULL, *key = NULL;
1391 uint64_t size, skip, offset;
1392 int key_size, read_only, r;
1394 /* Device context type must be initialised */
1395 if (!cd->type || !crypt_get_uuid(cd))
1398 r = dm_query_device(name, &device, &size, &skip, &offset,
1399 &cipher, &key_size, &key, &read_only, NULL, &uuid);
1401 log_err(NULL, _("Device %s is not active.\n"), name);
1410 r = device_check_and_adjust(cd, device, 0, &new_size, &offset, &read_only);
1414 if (new_size == size) {
1415 log_dbg("Device has already requested size %" PRIu64
1421 log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
1423 r = dm_create_device(name, device, cipher, cd->type,
1424 crypt_get_uuid(cd), new_size, skip, offset,
1425 key_size, key, read_only, 1);
1427 crypt_safe_free(key);
1435 int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
1437 if (!isLUKS(cd->type)) {
1438 log_err(cd, _("This operation is not supported for this device type.\n"));
1442 if (uuid && !strncmp(uuid, cd->hdr.uuid, sizeof(cd->hdr.uuid))) {
1443 log_dbg("UUID is the same as requested (%s) for device %s.",
1449 log_dbg("Requested new UUID change to %s for %s.", uuid, cd->device);
1451 log_dbg("Requested new UUID refresh for %s.", cd->device);
1453 if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
1456 return LUKS_hdr_uuid_set(cd->device, &cd->hdr, uuid, cd);
1459 int crypt_header_backup(struct crypt_device *cd,
1460 const char *requested_type,
1461 const char *backup_file)
1465 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1468 r = init_crypto(cd);
1472 log_dbg("Requested header backup of device %s (%s) to "
1473 "file %s.", cd->device, requested_type, backup_file);
1475 return LUKS_hdr_backup(backup_file, cd->device, &cd->hdr, cd);
1478 int crypt_header_restore(struct crypt_device *cd,
1479 const char *requested_type,
1480 const char *backup_file)
1484 if (requested_type && !isLUKS(requested_type))
1487 /* Some hash functions need initialized gcrypt library */
1488 r = init_crypto(cd);
1492 log_dbg("Requested header restore to device %s (%s) from "
1493 "file %s.", cd->device, requested_type, backup_file);
1495 return LUKS_hdr_restore(backup_file, cd->device, &cd->hdr, cd);
1498 void crypt_free(struct crypt_device *cd)
1501 log_dbg("Releasing crypt device %s context.", cd->device);
1503 if (cd->loop_fd != -1)
1507 crypt_free_volume_key(cd->volume_key);
1510 free(cd->backing_file);
1513 /* used in plain device only */
1514 free((char*)cd->plain_hdr.hash);
1515 free(cd->plain_cipher);
1516 free(cd->plain_cipher_mode);
1517 free(cd->plain_uuid);
1523 int crypt_suspend(struct crypt_device *cd,
1526 crypt_status_info ci;
1527 int r, suspended = 0;
1529 log_dbg("Suspending volume %s.", name);
1531 ci = crypt_status(NULL, name);
1532 if (ci < CRYPT_ACTIVE) {
1533 log_err(cd, _("Volume %s is not active.\n"), name);
1537 if (!cd && dm_init(NULL, 1) < 0)
1540 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1541 NULL, NULL, NULL, NULL, &suspended, NULL);
1546 log_err(cd, _("Volume %s is already suspended.\n"), name);
1551 r = dm_suspend_and_wipe_key(name);
1553 log_err(cd, "Suspend is not supported for device %s.\n", name);
1555 log_err(cd, "Error during suspending device %s.\n", name);
1562 int crypt_resume_by_passphrase(struct crypt_device *cd,
1565 const char *passphrase,
1566 size_t passphrase_size)
1568 struct volume_key *vk = NULL;
1569 int r, suspended = 0;
1571 log_dbg("Resuming volume %s.", name);
1573 if (!isLUKS(cd->type)) {
1574 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1579 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1580 NULL, NULL, NULL, NULL, &suspended, NULL);
1585 log_err(cd, _("Volume %s is not suspended.\n"), name);
1590 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1591 passphrase_size, &cd->hdr, &vk, cd);
1593 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1597 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1599 log_err(cd, "Resume is not supported for device %s.\n", name);
1601 log_err(cd, "Error during resuming device %s.\n", name);
1605 crypt_free_volume_key(vk);
1606 return r < 0 ? r : keyslot;
1609 int crypt_resume_by_keyfile(struct crypt_device *cd,
1612 const char *keyfile,
1613 size_t keyfile_size)
1615 struct volume_key *vk = NULL;
1616 char *passphrase_read = NULL;
1617 unsigned int passphrase_size_read;
1618 int r, suspended = 0;
1620 log_dbg("Resuming volume %s.", name);
1622 if (!isLUKS(cd->type)) {
1623 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1628 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1629 NULL, NULL, NULL, NULL, &suspended, NULL);
1634 log_err(cd, _("Volume %s is not suspended.\n"), name);
1641 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1642 &passphrase_size_read, keyfile, keyfile_size);
1646 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1647 passphrase_size_read, &cd->hdr, &vk, cd);
1652 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1654 log_err(cd, "Error during resuming device %s.\n", name);
1656 crypt_safe_free(passphrase_read);
1657 crypt_free_volume_key(vk);
1658 return r < 0 ? r : keyslot;
1661 // slot manipulation
1662 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1663 int keyslot, // -1 any
1664 const char *passphrase, // NULL -> terminal
1665 size_t passphrase_size,
1666 const char *new_passphrase, // NULL -> terminal
1667 size_t new_passphrase_size)
1669 struct volume_key *vk = NULL;
1670 char *password = NULL, *new_password = NULL;
1671 unsigned int passwordLen, new_passwordLen;
1674 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1675 "new passphrase %sprovided.",
1676 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1678 if (!isLUKS(cd->type)) {
1679 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1683 r = keyslot_verify_or_find_empty(cd, &keyslot);
1687 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1688 /* No slots used, try to use pre-generated key in header */
1689 if (cd->volume_key) {
1690 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1691 r = vk ? 0 : -ENOMEM;
1693 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1696 } else if (passphrase) {
1697 /* Passphrase provided, use it to unlock existing keyslot */
1698 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, passphrase,
1699 passphrase_size, &cd->hdr, &vk, cd);
1701 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1702 r = key_from_terminal(cd, _("Enter any passphrase: "),
1703 &password, &passwordLen, 0);
1707 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
1708 passwordLen, &cd->hdr, &vk, cd);
1709 crypt_safe_free(password);
1715 if (new_passphrase) {
1716 new_password = (char *)new_passphrase;
1717 new_passwordLen = new_passphrase_size;
1719 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1720 &new_password, &new_passwordLen, 1);
1725 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1726 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1731 if (!new_passphrase)
1732 crypt_safe_free(new_password);
1733 crypt_free_volume_key(vk);
1734 return r ?: keyslot;
1737 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1739 const char *keyfile,
1740 size_t keyfile_size,
1741 const char *new_keyfile,
1742 size_t new_keyfile_size)
1744 struct volume_key *vk = NULL;
1745 char *password = NULL; unsigned int passwordLen;
1746 char *new_password = NULL; unsigned int new_passwordLen;
1749 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1750 keyfile ?: "[none]", new_keyfile ?: "[none]");
1752 if (!isLUKS(cd->type)) {
1753 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1757 r = keyslot_verify_or_find_empty(cd, &keyslot);
1761 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1762 /* No slots used, try to use pre-generated key in header */
1763 if (cd->volume_key) {
1764 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1765 r = vk ? 0 : -ENOMEM;
1767 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1771 /* Read password from file of (if NULL) from terminal */
1773 r = key_from_file(cd, _("Enter any passphrase: "),
1774 &password, &passwordLen,
1775 keyfile, keyfile_size);
1777 r = key_from_terminal(cd, _("Enter any passphrase: "),
1778 &password, &passwordLen, 0);
1782 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password, passwordLen,
1790 r = key_from_file(cd, _("Enter new passphrase for key slot: "),
1791 &new_password, &new_passwordLen, new_keyfile,
1794 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1795 &new_password, &new_passwordLen, 1);
1799 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1800 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1802 crypt_safe_free(password);
1803 crypt_safe_free(new_password);
1804 crypt_free_volume_key(vk);
1805 return r < 0 ? r : keyslot;
1808 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1810 const char *volume_key,
1811 size_t volume_key_size,
1812 const char *passphrase,
1813 size_t passphrase_size)
1815 struct volume_key *vk = NULL;
1817 char *new_password = NULL; unsigned int new_passwordLen;
1819 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1821 if (!isLUKS(cd->type)) {
1822 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1827 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1828 else if (cd->volume_key)
1829 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1834 r = LUKS_verify_volume_key(&cd->hdr, vk);
1836 log_err(cd, _("Volume key does not match the volume.\n"));
1840 r = keyslot_verify_or_find_empty(cd, &keyslot);
1845 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1846 &new_password, &new_passwordLen, 1);
1849 passphrase = new_password;
1850 passphrase_size = new_passwordLen;
1853 r = LUKS_set_key(cd->device, keyslot, passphrase, passphrase_size,
1854 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1856 crypt_safe_free(new_password);
1857 crypt_free_volume_key(vk);
1858 return (r < 0) ? r : keyslot;
1861 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1863 crypt_keyslot_info ki;
1865 log_dbg("Destroying keyslot %d.", keyslot);
1867 if (!isLUKS(cd->type)) {
1868 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1872 ki = crypt_keyslot_status(cd, keyslot);
1873 if (ki == CRYPT_SLOT_INVALID) {
1874 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1878 if (ki == CRYPT_SLOT_INACTIVE) {
1879 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1883 return LUKS_del_key(cd->device, keyslot, &cd->hdr, cd);
1886 // activation/deactivation of device mapping
1887 int crypt_activate_by_passphrase(struct crypt_device *cd,
1890 const char *passphrase,
1891 size_t passphrase_size,
1894 crypt_status_info ci;
1895 struct volume_key *vk = NULL;
1896 char *read_passphrase = NULL;
1897 unsigned int passphraseLen = 0;
1900 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1901 name ? "Activating" : "Checking", name ?: "",
1902 keyslot, passphrase ? "" : "[none] ");
1905 ci = crypt_status(NULL, name);
1906 if (ci == CRYPT_INVALID)
1908 else if (ci >= CRYPT_ACTIVE) {
1909 log_err(cd, _("Device %s already exists.\n"), name);
1914 /* plain, use hashed passphrase */
1915 if (isPLAIN(cd->type)) {
1917 r = key_from_terminal(cd, NULL, &read_passphrase,
1921 passphrase = read_passphrase;
1922 passphrase_size = passphraseLen;
1924 r = create_device_helper(cd, name, cd->plain_hdr.hash,
1925 cd->plain_cipher, cd->plain_cipher_mode,
1926 NULL, passphrase, passphrase_size,
1927 cd->volume_key->keylength, 0,
1928 cd->plain_hdr.skip, cd->plain_hdr.offset,
1930 flags & CRYPT_ACTIVATE_READONLY, 0, 0);
1932 } else if (isLUKS(cd->type)) {
1933 /* provided passphrase, do not retry */
1935 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1936 passphrase_size, &cd->hdr, &vk, cd);
1938 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1943 r = open_from_hdr_and_vk(cd, vk, name, flags);
1948 crypt_safe_free(read_passphrase);
1949 crypt_free_volume_key(vk);
1951 return r < 0 ? r : keyslot;
1954 int crypt_activate_by_keyfile(struct crypt_device *cd,
1957 const char *keyfile,
1958 size_t keyfile_size,
1961 crypt_status_info ci;
1962 struct volume_key *vk = NULL;
1963 char *passphrase_read = NULL;
1964 unsigned int passphrase_size_read, key_count = 0;
1967 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1968 name ?: "", keyslot, keyfile ?: "[none]");
1971 ci = crypt_status(NULL, name);
1972 if (ci == CRYPT_INVALID)
1974 else if (ci >= CRYPT_ACTIVE) {
1975 log_err(cd, _("Device %s already exists.\n"), name);
1983 if (isPLAIN(cd->type)) {
1984 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1985 &passphrase_size_read, keyfile, keyfile_size);
1988 r = create_device_helper(cd, name, cd->plain_hdr.hash,
1989 cd->plain_cipher, cd->plain_cipher_mode,
1990 NULL, passphrase_read, passphrase_size_read,
1991 cd->volume_key->keylength, 0,
1992 cd->plain_hdr.skip, cd->plain_hdr.offset,
1994 flags & CRYPT_ACTIVATE_READONLY, 0, 0);
1995 } else if (isLUKS(cd->type)) {
1996 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1997 &passphrase_size_read, keyfile, keyfile_size);
2000 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
2001 passphrase_size_read, &cd->hdr, &vk, cd);
2007 r = open_from_hdr_and_vk(cd, vk, name, flags);
2012 } else if (isLOOPAES(cd->type)) {
2013 r = key_from_file(cd, NULL, &passphrase_read, &passphrase_size_read,
2014 keyfile, LOOPAES_KEYFILE_MAXSIZE);
2017 r = LOOPAES_parse_keyfile(cd, &vk, &key_count,
2018 passphrase_read, passphrase_size_read);
2022 r = LOOPAES_activate(cd, name, cd->loopaes_cipher,
2023 key_count, vk, flags);
2028 crypt_safe_free(passphrase_read);
2029 crypt_free_volume_key(vk);
2034 int crypt_activate_by_volume_key(struct crypt_device *cd,
2036 const char *volume_key,
2037 size_t volume_key_size,
2040 crypt_status_info ci;
2041 struct volume_key *vk;
2044 log_dbg("Activating volume %s by volume key.", name);
2046 /* use key directly, no hash */
2047 if (isPLAIN(cd->type)) {
2048 if (!volume_key || !volume_key_size || !cd->volume_key ||
2049 volume_key_size != cd->volume_key->keylength) {
2050 log_err(cd, _("Incorrect volume key specified for plain device.\n"));
2054 return create_device_helper(cd, name, NULL,
2055 cd->plain_cipher, cd->plain_cipher_mode, NULL, volume_key, volume_key_size,
2056 cd->volume_key->keylength, 0, cd->plain_hdr.skip,
2057 cd->plain_hdr.offset, cd->plain_uuid, flags & CRYPT_ACTIVATE_READONLY, 0, 0);
2060 if (!isLUKS(cd->type)) {
2061 log_err(cd, _("Device type is not properly initialised.\n"));
2066 ci = crypt_status(NULL, name);
2067 if (ci == CRYPT_INVALID)
2069 else if (ci >= CRYPT_ACTIVE) {
2070 log_err(cd, _("Device %s already exists.\n"), name);
2075 /* If key is not provided, try to use internal key */
2077 if (!cd->volume_key) {
2078 log_err(cd, _("Volume key does not match the volume.\n"));
2081 volume_key_size = cd->volume_key->keylength;
2082 volume_key = cd->volume_key->key;
2085 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2088 r = LUKS_verify_volume_key(&cd->hdr, vk);
2091 log_err(cd, _("Volume key does not match the volume.\n"));
2094 r = open_from_hdr_and_vk(cd, vk, name, flags);
2096 crypt_free_volume_key(vk);
2101 int crypt_deactivate(struct crypt_device *cd, const char *name)
2108 log_dbg("Deactivating volume %s.", name);
2110 if (!cd && dm_init(NULL, 1) < 0)
2113 switch (crypt_status(cd, name)) {
2115 r = dm_remove_device(name, 0, 0);
2118 log_err(cd, _("Device %s is busy.\n"), name);
2121 case CRYPT_INACTIVE:
2122 log_err(cd, _("Device %s is not active.\n"), name);
2126 log_err(cd, _("Invalid device %s.\n"), name);
2136 int crypt_volume_key_get(struct crypt_device *cd,
2139 size_t *volume_key_size,
2140 const char *passphrase,
2141 size_t passphrase_size)
2143 struct volume_key *vk;
2144 char *processed_key = NULL;
2147 key_len = crypt_get_volume_key_size(cd);
2148 if (key_len > *volume_key_size) {
2149 log_err(cd, _("Volume key buffer too small.\n"));
2153 if (isPLAIN(cd->type) && cd->plain_hdr.hash) {
2154 processed_key = process_key(cd, cd->plain_hdr.hash, NULL, key_len,
2155 passphrase, passphrase_size);
2156 if (!processed_key) {
2157 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
2160 memcpy(volume_key, processed_key, key_len);
2161 *volume_key_size = key_len;
2162 crypt_safe_free(processed_key);
2166 if (isLUKS(cd->type)) {
2167 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
2168 passphrase_size, &cd->hdr, &vk, cd);
2171 memcpy(volume_key, vk->key, vk->keylength);
2172 *volume_key_size = vk->keylength;
2175 crypt_free_volume_key(vk);
2179 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
2183 int crypt_volume_key_verify(struct crypt_device *cd,
2184 const char *volume_key,
2185 size_t volume_key_size)
2187 struct volume_key *vk;
2190 if (!isLUKS(cd->type)) {
2191 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2195 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2199 r = LUKS_verify_volume_key(&cd->hdr, vk);
2202 log_err(cd, _("Volume key does not match the volume.\n"));
2204 crypt_free_volume_key(vk);
2209 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
2211 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
2212 cd->timeout = timeout_sec;
2215 void crypt_set_password_retry(struct crypt_device *cd, int tries)
2217 log_dbg("Password retry count set to %d.", tries);
2221 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2223 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
2224 cd->iteration_time = iteration_time_ms;
2227 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
2229 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
2230 cd->password_verify = password_verify ? 1 : 0;
2233 void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
2236 case CRYPT_RNG_URANDOM:
2237 case CRYPT_RNG_RANDOM:
2238 log_dbg("RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
2239 cd->rng_type = rng_type;
2243 int crypt_get_rng_type(struct crypt_device *cd)
2248 return cd->rng_type;
2251 int crypt_memory_lock(struct crypt_device *cd, int lock)
2253 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
2257 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
2261 if (!cd && dm_init(NULL, 1) < 0)
2262 return CRYPT_INVALID;
2264 r = dm_status_device(name);
2269 if (r < 0 && r != -ENODEV)
2270 return CRYPT_INVALID;
2273 return CRYPT_ACTIVE;
2278 return CRYPT_INACTIVE;
2281 static void hexprintICB(struct crypt_device *cd, char *d, int n)
2284 for(i = 0; i < n; i++)
2285 log_std(cd, "%02hhx ", (char)d[i]);
2288 int crypt_dump(struct crypt_device *cd)
2291 if (!isLUKS(cd->type)) { //FIXME
2292 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2296 log_std(cd, "LUKS header information for %s\n\n", cd->device);
2297 log_std(cd, "Version: \t%d\n", cd->hdr.version);
2298 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
2299 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
2300 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec);
2301 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
2302 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
2303 log_std(cd, "MK digest: \t");
2304 hexprintICB(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE);
2306 log_std(cd, "MK salt: \t");
2307 hexprintICB(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2);
2308 log_std(cd, "\n \t");
2309 hexprintICB(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
2311 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
2312 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
2313 for(i = 0; i < LUKS_NUMKEYS; i++) {
2314 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
2315 log_std(cd, "Key Slot %d: ENABLED\n",i);
2316 log_std(cd, "\tIterations: \t%d\n",
2317 cd->hdr.keyblock[i].passwordIterations);
2318 log_std(cd, "\tSalt: \t");
2319 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt,
2321 log_std(cd, "\n\t \t");
2322 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt +
2323 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
2326 log_std(cd, "\tKey material offset:\t%d\n",
2327 cd->hdr.keyblock[i].keyMaterialOffset);
2328 log_std(cd, "\tAF stripes: \t%d\n",
2329 cd->hdr.keyblock[i].stripes);
2332 log_std(cd, "Key Slot %d: DISABLED\n", i);
2338 const char *crypt_get_cipher(struct crypt_device *cd)
2340 if (isPLAIN(cd->type))
2341 return cd->plain_cipher;
2343 if (isLUKS(cd->type))
2344 return cd->hdr.cipherName;
2346 if (isLOOPAES(cd->type))
2347 return cd->loopaes_cipher;
2352 const char *crypt_get_cipher_mode(struct crypt_device *cd)
2354 if (isPLAIN(cd->type))
2355 return cd->plain_cipher_mode;
2357 if (isLUKS(cd->type))
2358 return cd->hdr.cipherMode;
2360 if (isLOOPAES(cd->type))
2361 return cd->loopaes_cipher_mode;
2366 const char *crypt_get_uuid(struct crypt_device *cd)
2368 if (isLUKS(cd->type))
2369 return cd->hdr.uuid;
2371 if (isPLAIN(cd->type))
2372 return cd->plain_uuid;
2374 if (isLOOPAES(cd->type))
2375 return cd->loopaes_uuid;
2380 const char *crypt_get_device_name(struct crypt_device *cd)
2385 int crypt_get_volume_key_size(struct crypt_device *cd)
2387 if (isPLAIN(cd->type) && cd->volume_key)
2388 return cd->volume_key->keylength;
2390 if (isLUKS(cd->type))
2391 return cd->hdr.keyBytes;
2393 if (isLOOPAES(cd->type))
2394 return cd->loopaes_key_size;
2399 uint64_t crypt_get_data_offset(struct crypt_device *cd)
2401 if (isPLAIN(cd->type))
2402 return cd->plain_hdr.offset;
2404 if (isLUKS(cd->type))
2405 return cd->hdr.payloadOffset;
2407 if (isLOOPAES(cd->type))
2408 return cd->loopaes_hdr.offset;
2413 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
2415 if (!isLUKS(cd->type)) {
2416 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2417 return CRYPT_SLOT_INVALID;
2420 return LUKS_keyslot_info(&cd->hdr, keyslot);
2423 int crypt_keyslot_max(const char *type)
2425 if (type && isLUKS(type))
2426 return LUKS_NUMKEYS;
2431 const char *crypt_get_type(struct crypt_device *cd)
2436 int crypt_get_active_device(struct crypt_device *cd,
2438 struct crypt_active_device *cad)
2442 r = dm_query_device(name, NULL, &cad->size, &cad->iv_offset, &cad->offset,
2443 NULL, NULL, NULL, &readonly, NULL, NULL);
2447 cad->flags = readonly ? CRYPT_ACTIVATE_READONLY : 0;