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"
39 char *metadata_device;
43 struct volume_key *volume_key;
45 uint64_t iteration_time;
50 /* used in CRYPT_LUKS1 */
52 uint64_t PBKDF2_per_sec;
54 /* used in CRYPT_PLAIN */
55 struct crypt_params_plain plain_hdr;
57 char *plain_cipher_mode;
60 /* used in CRYPT_LOOPAES */
61 struct crypt_params_loopaes loopaes_hdr;
63 char *loopaes_cipher_mode;
65 unsigned int loopaes_key_size;
67 /* callbacks definitions */
68 void (*log)(int level, const char *msg, void *usrptr);
70 int (*confirm)(const char *msg, void *usrptr);
72 int (*password)(const char *msg, char *buf, size_t length, void *usrptr);
73 void *password_usrptr;
77 static void (*_default_log)(int level, const char *msg, void *usrptr) = NULL;
78 static int _debug_level = 0;
80 void crypt_set_debug_level(int level)
85 int crypt_get_debug_level(void)
90 void crypt_log(struct crypt_device *cd, int level, const char *msg)
93 cd->log(level, msg, cd->log_usrptr);
94 else if (_default_log)
95 _default_log(level, msg, NULL);
98 __attribute__((format(printf, 5, 6)))
99 void logger(struct crypt_device *cd, int level, const char *file,
100 int line, const char *format, ...)
105 va_start(argp, format);
107 if (vasprintf(&target, format, argp) > 0) {
109 crypt_log(cd, level, target);
111 } else if (_debug_level)
112 printf("# %s:%d %s\n", file ?: "?", line, target);
114 } else if (_debug_level)
115 printf("# %s\n", target);
123 static const char *mdata_device(struct crypt_device *cd)
125 return cd->metadata_device ?: cd->device;
128 static int init_crypto(struct crypt_device *ctx)
132 r = crypt_random_init(ctx);
134 log_err(ctx, _("Cannot initialize crypto RNG backend.\n"));
138 r = crypt_backend_init(ctx);
140 log_err(ctx, _("Cannot initialize crypto backend.\n"));
145 static int process_key(struct crypt_device *cd, const char *hash_name,
146 size_t key_size, const char *pass, size_t passLen,
147 struct volume_key **vk)
154 *vk = crypt_alloc_volume_key(key_size, NULL);
159 r = crypt_plain_hash(cd, hash_name, (*vk)->key, key_size, pass, passLen);
162 log_err(cd, _("Hash algorithm %s not supported.\n"),
165 log_err(cd, _("Key processing error (using hash %s).\n"),
167 crypt_free_volume_key(*vk);
171 } else if (passLen > key_size) {
172 memcpy((*vk)->key, pass, key_size);
174 memcpy((*vk)->key, pass, passLen);
180 static int isPLAIN(const char *type)
182 return (type && !strcmp(CRYPT_PLAIN, type));
185 static int isLUKS(const char *type)
187 return (type && !strcmp(CRYPT_LUKS1, type));
190 static int isLOOPAES(const char *type)
192 return (type && !strcmp(CRYPT_LOOPAES, type));
195 /* keyslot helpers */
196 static int keyslot_verify_or_find_empty(struct crypt_device *cd, int *keyslot)
198 if (*keyslot == CRYPT_ANY_SLOT) {
199 *keyslot = LUKS_keyslot_find_empty(&cd->hdr);
201 log_err(cd, _("All key slots full.\n"));
206 switch (LUKS_keyslot_info(&cd->hdr, *keyslot)) {
207 case CRYPT_SLOT_INVALID:
208 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
209 *keyslot, LUKS_NUMKEYS - 1);
211 case CRYPT_SLOT_INACTIVE:
214 log_err(cd, _("Key slot %d is full, please select another one.\n"),
222 int PLAIN_activate(struct crypt_device *cd,
224 struct volume_key *vk,
229 char *dm_cipher = NULL;
230 struct crypt_dm_active_device dmd = {
231 .device = crypt_get_device_name(cd),
233 .uuid = crypt_get_uuid(cd),
235 .offset = crypt_get_data_offset(cd),
236 .iv_offset = crypt_get_iv_offset(cd),
241 r = device_check_and_adjust(cd, dmd.device,
242 (dmd.flags & CRYPT_ACTIVATE_SHARED) ? DEV_SHARED : DEV_EXCL,
243 &dmd.size, &dmd.offset, &flags);
247 if (crypt_get_cipher_mode(cd))
248 r = asprintf(&dm_cipher, "%s-%s", crypt_get_cipher(cd), crypt_get_cipher_mode(cd));
250 r = asprintf(&dm_cipher, "%s", crypt_get_cipher(cd));
254 dmd.cipher = dm_cipher;
255 log_dbg("Trying to activate PLAIN device %s using cipher %s.", name, dmd.cipher);
257 r = dm_create_device(name, CRYPT_PLAIN, &dmd, 0);
260 if (!cd->plain_uuid && dm_query_device(name, DM_ACTIVE_UUID, &dmd) >= 0)
261 cd->plain_uuid = (char*)dmd.uuid;
267 int crypt_confirm(struct crypt_device *cd, const char *msg)
269 if (!cd || !cd->confirm)
272 return cd->confirm(msg, cd->confirm_usrptr);
275 static int key_from_terminal(struct crypt_device *cd, char *msg, char **key,
276 size_t *key_len, int force_verify)
282 if(!msg && asprintf(&prompt, _("Enter passphrase for %s: "),
283 cd->backing_file ?: crypt_get_device_name(cd)) < 0)
290 *key = crypt_safe_alloc(DEFAULT_PASSPHRASE_SIZE_MAX);
295 r = cd->password(msg, *key, DEFAULT_PASSPHRASE_SIZE_MAX,
296 cd->password_usrptr);
298 crypt_safe_free(*key);
303 r = crypt_get_key(msg, key, key_len, 0, NULL, cd->timeout,
304 (force_verify || cd->password_verify), cd);
307 return (r < 0) ? r: 0;
310 static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslot,
311 struct volume_key **vk)
313 char *passphrase_read = NULL;
314 size_t passphrase_size_read;
315 int r = -EINVAL, eperm = 0, tries = cd->tries;
319 crypt_free_volume_key(*vk);
322 r = key_from_terminal(cd, NULL, &passphrase_read,
323 &passphrase_size_read, 0);
327 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase_read,
328 passphrase_size_read, &cd->hdr, vk, cd);
331 crypt_safe_free(passphrase_read);
332 passphrase_read = NULL;
333 } while (r == -EPERM && (--tries > 0));
336 crypt_free_volume_key(*vk);
339 /* Report wrong passphrase if at least one try failed */
340 if (eperm && r == -EPIPE)
344 crypt_safe_free(passphrase_read);
348 static int key_from_file(struct crypt_device *cd, char *msg,
349 char **key, size_t *key_len,
350 const char *key_file, size_t key_size)
352 return crypt_get_key(msg, key, key_len, key_size, key_file,
356 void crypt_set_log_callback(struct crypt_device *cd,
357 void (*log)(int level, const char *msg, void *usrptr),
364 cd->log_usrptr = usrptr;
368 void crypt_set_confirm_callback(struct crypt_device *cd,
369 int (*confirm)(const char *msg, void *usrptr),
372 cd->confirm = confirm;
373 cd->confirm_usrptr = usrptr;
376 void crypt_set_password_callback(struct crypt_device *cd,
377 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
380 cd->password = password;
381 cd->password_usrptr = usrptr;
384 void crypt_get_error(char *buf, size_t size)
386 const char *error = get_error();
388 if (!buf || size < 1)
391 strncpy(buf, error, size - 1);
392 buf[size - 1] = '\0';
398 const char *crypt_get_dir(void)
403 int crypt_init(struct crypt_device **cd, const char *device)
405 struct crypt_device *h = NULL;
411 log_dbg("Allocating crypt device %s context.", device);
413 if (!(h = malloc(sizeof(struct crypt_device))))
416 memset(h, 0, sizeof(*h));
420 r = device_ready(NULL, device, O_RDONLY);
422 h->device = crypt_loop_get_device();
423 log_dbg("Not a block device, %s%s.",
424 h->device ? "using free loop device " :
425 "no free loop device found",
428 log_err(NULL, _("Cannot find a free loopback device.\n"));
433 /* Keep the loop open, dettached on last close. */
434 h->loop_fd = crypt_loop_attach(h->device, device, 0, 1, &readonly);
435 if (h->loop_fd == -1) {
436 log_err(NULL, _("Attaching loopback device failed "
437 "(loop device with autoclear flag is required).\n"));
442 h->backing_file = crypt_loop_backing_file(h->device);
443 r = device_ready(NULL, h->device, O_RDONLY);
451 if (!h->device && device && !(h->device = strdup(device))) {
456 if (dm_init(h, 1) < 0) {
461 h->iteration_time = 1000;
462 h->password_verify = 0;
464 h->rng_type = crypt_random_default_key_rng();
470 if (h->loop_fd != -1)
473 free(h->backing_file);
479 static int crypt_check_data_device_size(struct crypt_device *cd)
482 uint64_t size, size_min;
484 /* Check data device size, require at least one sector */
485 size_min = crypt_get_data_offset(cd) ?: SECTOR_SIZE;
487 r = device_size(crypt_get_device_name(cd), &size);
491 if (size < size_min) {
492 log_err(cd, _("LUKS header detected but device %s is too small.\n"),
493 crypt_get_device_name(cd));
500 int crypt_set_data_device(struct crypt_device *cd, const char *device)
505 log_dbg("Setting ciphertext data device to %s.", device ?: "(none)");
507 if (!isLUKS(cd->type)) {
508 log_err(cd, _("This operation is not supported for this device type.\n"));
512 /* metadata device must be set */
516 r = device_ready(NULL, device, O_RDONLY);
520 if (!(data_device = strdup(device)))
523 if (!cd->metadata_device)
524 cd->metadata_device = cd->device;
528 cd->device = data_device;
530 return crypt_check_data_device_size(cd);
533 int crypt_init_by_name_and_header(struct crypt_device **cd,
535 const char *header_device)
537 crypt_status_info ci;
538 struct crypt_dm_active_device dmd;
539 char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
543 log_dbg("Allocating crypt device context by device %s.", name);
545 ci = crypt_status(NULL, name);
546 if (ci == CRYPT_INVALID)
549 if (ci < CRYPT_ACTIVE) {
550 log_err(NULL, _("Device %s is not active.\n"), name);
554 r = dm_query_device(name, DM_ACTIVE_DEVICE | DM_ACTIVE_CIPHER |
555 DM_ACTIVE_UUID | DM_ACTIVE_KEYSIZE, &dmd);
562 r = crypt_init(cd, header_device);
564 r = crypt_init(cd, dmd.device);
566 /* Underlying device disappeared but mapping still active */
567 if (!dmd.device || r == -ENOTBLK)
568 log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
571 /* Underlying device is not readable but crypt mapping exists */
573 free((char*)dmd.device);
575 r = crypt_init(cd, NULL);
583 if (!strncmp(CRYPT_PLAIN, dmd.uuid, sizeof(CRYPT_PLAIN)-1))
584 (*cd)->type = strdup(CRYPT_PLAIN);
585 else if (!strncmp(CRYPT_LOOPAES, dmd.uuid, sizeof(CRYPT_LOOPAES)-1))
586 (*cd)->type = strdup(CRYPT_LOOPAES);
587 else if (!strncmp(CRYPT_LUKS1, dmd.uuid, sizeof(CRYPT_LUKS1)-1))
588 (*cd)->type = strdup(CRYPT_LUKS1);
590 log_dbg("Unknown UUID set, some parameters are not set.");
592 log_dbg("Active device has no UUID set, some parameters are not set.");
595 r = crypt_set_data_device(*cd, dmd.device);
600 /* Try to initialise basic parameters from active device */
602 if (!(*cd)->backing_file && dmd.device && crypt_loop_device(dmd.device) &&
603 !((*cd)->backing_file = crypt_loop_backing_file(dmd.device))) {
608 if (isPLAIN((*cd)->type)) {
609 (*cd)->type = strdup(CRYPT_PLAIN);
610 (*cd)->plain_uuid = strdup(dmd.uuid);
611 (*cd)->plain_hdr.hash = NULL; /* no way to get this */
612 (*cd)->plain_hdr.offset = dmd.offset;
613 (*cd)->plain_hdr.skip = dmd.iv_offset;
615 r = crypt_parse_name_and_mode(dmd.cipher, cipher, NULL, cipher_mode);
617 (*cd)->plain_cipher = strdup(cipher);
618 (*cd)->plain_cipher_mode = strdup(cipher_mode);
620 } else if (isLOOPAES((*cd)->type)) {
621 (*cd)->type = strdup(CRYPT_LOOPAES);
622 (*cd)->loopaes_uuid = strdup(dmd.uuid);
623 (*cd)->loopaes_hdr.offset = dmd.offset;
625 r = crypt_parse_name_and_mode(dmd.cipher, cipher,
626 &key_nums, cipher_mode);
628 (*cd)->loopaes_cipher = strdup(cipher);
629 (*cd)->loopaes_cipher_mode = strdup(cipher_mode);
630 /* version 3 uses last key for IV */
631 if (dmd.vk->keylength % key_nums)
633 (*cd)->loopaes_key_size = dmd.vk->keylength / key_nums;
635 } else if (isLUKS((*cd)->type)) {
636 if (mdata_device(*cd)) {
637 r = crypt_load(*cd, CRYPT_LUKS1, NULL);
639 log_dbg("LUKS device header does not match active device.");
653 crypt_free_volume_key(dmd.vk);
654 free((char*)dmd.device);
655 free((char*)dmd.cipher);
656 free((char*)dmd.uuid);
660 int crypt_init_by_name(struct crypt_device **cd, const char *name)
662 return crypt_init_by_name_and_header(cd, name, NULL);
665 static int _crypt_format_plain(struct crypt_device *cd,
667 const char *cipher_mode,
669 size_t volume_key_size,
670 struct crypt_params_plain *params)
672 if (!cipher || !cipher_mode) {
673 log_err(cd, _("Invalid plain crypt parameters.\n"));
677 if (volume_key_size > 1024) {
678 log_err(cd, _("Invalid key size.\n"));
682 cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
686 cd->plain_cipher = strdup(cipher);
687 cd->plain_cipher_mode = strdup(cipher_mode);
690 cd->plain_uuid = strdup(uuid);
692 if (params && params->hash)
693 cd->plain_hdr.hash = strdup(params->hash);
695 cd->plain_hdr.offset = params ? params->offset : 0;
696 cd->plain_hdr.skip = params ? params->skip : 0;
697 cd->plain_hdr.size = params ? params->size : 0;
699 if (!cd->plain_cipher || !cd->plain_cipher_mode)
705 static int _crypt_format_luks1(struct crypt_device *cd,
707 const char *cipher_mode,
709 const char *volume_key,
710 size_t volume_key_size,
711 struct crypt_params_luks1 *params)
714 unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
715 unsigned long alignment_offset = 0;
717 if (!mdata_device(cd)) {
718 log_err(cd, _("Can't format LUKS without device.\n"));
723 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
726 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
731 if (params && params->data_device) {
732 cd->metadata_device = cd->device;
733 if (!(cd->device = strdup(params->data_device)))
735 required_alignment = params->data_alignment * SECTOR_SIZE;
736 } else if (params && params->data_alignment) {
737 required_alignment = params->data_alignment * SECTOR_SIZE;
739 get_topology_alignment(cd->device, &required_alignment,
740 &alignment_offset, DEFAULT_DISK_ALIGNMENT);
742 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
743 (params && params->hash) ? params->hash : "sha1",
745 required_alignment / SECTOR_SIZE,
746 alignment_offset / SECTOR_SIZE,
747 cd->iteration_time, &cd->PBKDF2_per_sec,
748 cd->metadata_device, cd);
752 /* Wipe first 8 sectors - fs magic numbers etc. */
753 r = wipe_device_header(mdata_device(cd), 8);
756 log_err(cd, _("Cannot format device %s which is still in use.\n"),
759 log_err(cd, _("Cannot wipe header on device %s.\n"),
765 r = LUKS_write_phdr(mdata_device(cd), &cd->hdr, cd);
770 static int _crypt_format_loopaes(struct crypt_device *cd,
773 size_t volume_key_size,
774 struct crypt_params_loopaes *params)
776 if (!mdata_device(cd)) {
777 log_err(cd, _("Can't format LOOPAES without device.\n"));
781 if (volume_key_size > 1024) {
782 log_err(cd, _("Invalid key size.\n"));
786 cd->loopaes_key_size = volume_key_size;
788 cd->loopaes_cipher = strdup(cipher ?: DEFAULT_LOOPAES_CIPHER);
791 cd->loopaes_uuid = strdup(uuid);
793 if (params && params->hash)
794 cd->loopaes_hdr.hash = strdup(params->hash);
796 cd->loopaes_hdr.offset = params ? params->offset : 0;
797 cd->loopaes_hdr.skip = params ? params->skip : 0;
802 int crypt_format(struct crypt_device *cd,
805 const char *cipher_mode,
807 const char *volume_key,
808 size_t volume_key_size,
816 log_dbg("Formatting device %s as type %s.", mdata_device(cd) ?: "(none)", type);
823 r = _crypt_format_plain(cd, cipher, cipher_mode,
824 uuid, volume_key_size, params);
825 else if (isLUKS(type))
826 r = _crypt_format_luks1(cd, cipher, cipher_mode,
827 uuid, volume_key, volume_key_size, params);
828 else if (isLOOPAES(type))
829 r = _crypt_format_loopaes(cd, cipher, uuid, volume_key_size, params);
831 /* FIXME: allow plugins here? */
832 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
836 if (!r && !(cd->type = strdup(type)))
840 crypt_free_volume_key(cd->volume_key);
841 cd->volume_key = NULL;
847 int crypt_load(struct crypt_device *cd,
848 const char *requested_type,
849 void *params __attribute__((unused)))
851 struct luks_phdr hdr;
854 log_dbg("Trying to load %s crypt type from device %s.",
855 requested_type ?: "any", mdata_device(cd) ?: "(none)");
857 if (!mdata_device(cd))
860 if (requested_type && !isLUKS(requested_type))
867 r = LUKS_read_phdr(mdata_device(cd), &hdr, 1, cd);
871 r = crypt_check_data_device_size(cd);
875 memcpy(&cd->hdr, &hdr, sizeof(hdr));
877 cd->type = strdup(CRYPT_LUKS1);
884 int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
886 struct crypt_dm_active_device dmd;
889 /* Device context type must be initialised */
890 if (!cd->type || !crypt_get_uuid(cd))
893 log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
895 r = dm_query_device(name, DM_ACTIVE_DEVICE | DM_ACTIVE_CIPHER |
896 DM_ACTIVE_UUID | DM_ACTIVE_KEYSIZE |
897 DM_ACTIVE_KEY, &dmd);
899 log_err(NULL, _("Device %s is not active.\n"), name);
908 r = device_check_and_adjust(cd, dmd.device, DEV_OK, &new_size, &dmd.offset, &dmd.flags);
912 if (new_size == dmd.size) {
913 log_dbg("Device has already requested size %" PRIu64
914 " sectors.", dmd.size);
918 r = dm_create_device(name, cd->type, &dmd, 1);
921 crypt_free_volume_key(dmd.vk);
922 free((char*)dmd.cipher);
923 free((char*)dmd.device);
924 free((char*)dmd.uuid);
929 int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
931 if (!isLUKS(cd->type)) {
932 log_err(cd, _("This operation is not supported for this device type.\n"));
936 if (uuid && !strncmp(uuid, cd->hdr.uuid, sizeof(cd->hdr.uuid))) {
937 log_dbg("UUID is the same as requested (%s) for device %s.",
938 uuid, mdata_device(cd));
943 log_dbg("Requested new UUID change to %s for %s.", uuid, mdata_device(cd));
945 log_dbg("Requested new UUID refresh for %s.", mdata_device(cd));
947 if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
950 return LUKS_hdr_uuid_set(mdata_device(cd), &cd->hdr, uuid, cd);
953 int crypt_header_backup(struct crypt_device *cd,
954 const char *requested_type,
955 const char *backup_file)
959 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
966 log_dbg("Requested header backup of device %s (%s) to "
967 "file %s.", mdata_device(cd), requested_type, backup_file);
969 return LUKS_hdr_backup(backup_file, mdata_device(cd), &cd->hdr, cd);
972 int crypt_header_restore(struct crypt_device *cd,
973 const char *requested_type,
974 const char *backup_file)
978 if (requested_type && !isLUKS(requested_type))
981 /* Some hash functions need initialized gcrypt library */
986 log_dbg("Requested header restore to device %s (%s) from "
987 "file %s.", mdata_device(cd), requested_type, backup_file);
989 return LUKS_hdr_restore(backup_file, mdata_device(cd), &cd->hdr, cd);
992 void crypt_free(struct crypt_device *cd)
995 log_dbg("Releasing crypt device %s context.", mdata_device(cd));
997 if (cd->loop_fd != -1)
1001 crypt_free_volume_key(cd->volume_key);
1004 free(cd->metadata_device);
1005 free(cd->backing_file);
1008 /* used in plain device only */
1009 free((char*)cd->plain_hdr.hash);
1010 free(cd->plain_cipher);
1011 free(cd->plain_cipher_mode);
1012 free(cd->plain_uuid);
1014 /* used in loop-AES device only */
1015 free((char*)cd->loopaes_hdr.hash);
1016 free(cd->loopaes_cipher);
1017 free(cd->loopaes_uuid);
1023 int crypt_suspend(struct crypt_device *cd,
1026 crypt_status_info ci;
1029 log_dbg("Suspending volume %s.", name);
1031 if (!isLUKS(cd->type)) {
1032 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1037 ci = crypt_status(NULL, name);
1038 if (ci < CRYPT_ACTIVE) {
1039 log_err(cd, _("Volume %s is not active.\n"), name);
1043 if (!cd && dm_init(NULL, 1) < 0)
1046 r = dm_status_suspended(name);
1051 log_err(cd, _("Volume %s is already suspended.\n"), name);
1056 r = dm_suspend_and_wipe_key(name);
1058 log_err(cd, "Suspend is not supported for device %s.\n", name);
1060 log_err(cd, "Error during suspending device %s.\n", name);
1067 int crypt_resume_by_passphrase(struct crypt_device *cd,
1070 const char *passphrase,
1071 size_t passphrase_size)
1073 struct volume_key *vk = NULL;
1076 log_dbg("Resuming volume %s.", name);
1078 if (!isLUKS(cd->type)) {
1079 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1084 r = dm_status_suspended(name);
1089 log_err(cd, _("Volume %s is not suspended.\n"), name);
1094 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase,
1095 passphrase_size, &cd->hdr, &vk, cd);
1097 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1101 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1103 log_err(cd, "Resume is not supported for device %s.\n", name);
1105 log_err(cd, "Error during resuming device %s.\n", name);
1109 crypt_free_volume_key(vk);
1110 return r < 0 ? r : keyslot;
1113 int crypt_resume_by_keyfile(struct crypt_device *cd,
1116 const char *keyfile,
1117 size_t keyfile_size)
1119 struct volume_key *vk = NULL;
1120 char *passphrase_read = NULL;
1121 size_t passphrase_size_read;
1124 log_dbg("Resuming volume %s.", name);
1126 if (!isLUKS(cd->type)) {
1127 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1132 r = dm_status_suspended(name);
1137 log_err(cd, _("Volume %s is not suspended.\n"), name);
1144 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1145 &passphrase_size_read, keyfile, keyfile_size);
1149 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase_read,
1150 passphrase_size_read, &cd->hdr, &vk, cd);
1155 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1157 log_err(cd, "Error during resuming device %s.\n", name);
1159 crypt_safe_free(passphrase_read);
1160 crypt_free_volume_key(vk);
1161 return r < 0 ? r : keyslot;
1164 // slot manipulation
1165 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1166 int keyslot, // -1 any
1167 const char *passphrase, // NULL -> terminal
1168 size_t passphrase_size,
1169 const char *new_passphrase, // NULL -> terminal
1170 size_t new_passphrase_size)
1172 struct volume_key *vk = NULL;
1173 char *password = NULL, *new_password = NULL;
1174 size_t passwordLen, new_passwordLen;
1177 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1178 "new passphrase %sprovided.",
1179 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1181 if (!isLUKS(cd->type)) {
1182 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1186 r = keyslot_verify_or_find_empty(cd, &keyslot);
1190 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1191 /* No slots used, try to use pre-generated key in header */
1192 if (cd->volume_key) {
1193 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1194 r = vk ? 0 : -ENOMEM;
1196 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1199 } else if (passphrase) {
1200 /* Passphrase provided, use it to unlock existing keyslot */
1201 r = LUKS_open_key_with_hdr(mdata_device(cd), CRYPT_ANY_SLOT, passphrase,
1202 passphrase_size, &cd->hdr, &vk, cd);
1204 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1205 r = key_from_terminal(cd, _("Enter any passphrase: "),
1206 &password, &passwordLen, 0);
1210 r = LUKS_open_key_with_hdr(mdata_device(cd), CRYPT_ANY_SLOT, password,
1211 passwordLen, &cd->hdr, &vk, cd);
1212 crypt_safe_free(password);
1218 if (new_passphrase) {
1219 new_password = (char *)new_passphrase;
1220 new_passwordLen = new_passphrase_size;
1222 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1223 &new_password, &new_passwordLen, 1);
1228 r = LUKS_set_key(mdata_device(cd), keyslot, new_password, new_passwordLen,
1229 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1234 if (!new_passphrase)
1235 crypt_safe_free(new_password);
1236 crypt_free_volume_key(vk);
1237 return r ?: keyslot;
1240 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1242 const char *keyfile,
1243 size_t keyfile_size,
1244 const char *new_keyfile,
1245 size_t new_keyfile_size)
1247 struct volume_key *vk = NULL;
1248 char *password = NULL; size_t passwordLen;
1249 char *new_password = NULL; size_t new_passwordLen;
1252 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1253 keyfile ?: "[none]", new_keyfile ?: "[none]");
1255 if (!isLUKS(cd->type)) {
1256 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1260 r = keyslot_verify_or_find_empty(cd, &keyslot);
1264 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1265 /* No slots used, try to use pre-generated key in header */
1266 if (cd->volume_key) {
1267 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1268 r = vk ? 0 : -ENOMEM;
1270 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1274 /* Read password from file of (if NULL) from terminal */
1276 r = key_from_file(cd, _("Enter any passphrase: "),
1277 &password, &passwordLen,
1278 keyfile, keyfile_size);
1280 r = key_from_terminal(cd, _("Enter any passphrase: "),
1281 &password, &passwordLen, 0);
1285 r = LUKS_open_key_with_hdr(mdata_device(cd), CRYPT_ANY_SLOT, password, passwordLen,
1293 r = key_from_file(cd, _("Enter new passphrase for key slot: "),
1294 &new_password, &new_passwordLen, new_keyfile,
1297 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1298 &new_password, &new_passwordLen, 1);
1302 r = LUKS_set_key(mdata_device(cd), keyslot, new_password, new_passwordLen,
1303 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1305 crypt_safe_free(password);
1306 crypt_safe_free(new_password);
1307 crypt_free_volume_key(vk);
1308 return r < 0 ? r : keyslot;
1311 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1313 const char *volume_key,
1314 size_t volume_key_size,
1315 const char *passphrase,
1316 size_t passphrase_size)
1318 struct volume_key *vk = NULL;
1320 char *new_password = NULL; size_t new_passwordLen;
1322 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1324 if (!isLUKS(cd->type)) {
1325 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1330 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1331 else if (cd->volume_key)
1332 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1337 r = LUKS_verify_volume_key(&cd->hdr, vk);
1339 log_err(cd, _("Volume key does not match the volume.\n"));
1343 r = keyslot_verify_or_find_empty(cd, &keyslot);
1348 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1349 &new_password, &new_passwordLen, 1);
1352 passphrase = new_password;
1353 passphrase_size = new_passwordLen;
1356 r = LUKS_set_key(mdata_device(cd), keyslot, passphrase, passphrase_size,
1357 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1359 crypt_safe_free(new_password);
1360 crypt_free_volume_key(vk);
1361 return (r < 0) ? r : keyslot;
1364 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1366 crypt_keyslot_info ki;
1368 log_dbg("Destroying keyslot %d.", keyslot);
1370 if (!isLUKS(cd->type)) {
1371 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1375 ki = crypt_keyslot_status(cd, keyslot);
1376 if (ki == CRYPT_SLOT_INVALID) {
1377 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1381 if (ki == CRYPT_SLOT_INACTIVE) {
1382 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1386 return LUKS_del_key(mdata_device(cd), keyslot, &cd->hdr, cd);
1389 // activation/deactivation of device mapping
1390 int crypt_activate_by_passphrase(struct crypt_device *cd,
1393 const char *passphrase,
1394 size_t passphrase_size,
1397 crypt_status_info ci;
1398 struct volume_key *vk = NULL;
1399 char *read_passphrase = NULL;
1400 size_t passphraseLen = 0;
1403 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1404 name ? "Activating" : "Checking", name ?: "",
1405 keyslot, passphrase ? "" : "[none] ");
1408 ci = crypt_status(NULL, name);
1409 if (ci == CRYPT_INVALID)
1411 else if (ci >= CRYPT_ACTIVE) {
1412 log_err(cd, _("Device %s already exists.\n"), name);
1417 /* plain, use hashed passphrase */
1418 if (isPLAIN(cd->type)) {
1423 r = key_from_terminal(cd, NULL, &read_passphrase,
1427 passphrase = read_passphrase;
1428 passphrase_size = passphraseLen;
1431 r = process_key(cd, cd->plain_hdr.hash,
1432 cd->volume_key->keylength,
1433 passphrase, passphrase_size, &vk);
1437 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1439 } else if (isLUKS(cd->type)) {
1440 /* provided passphrase, do not retry */
1442 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase,
1443 passphrase_size, &cd->hdr, &vk, cd);
1445 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1450 r = LUKS1_activate(cd, name, vk, flags);
1455 crypt_safe_free(read_passphrase);
1456 crypt_free_volume_key(vk);
1458 return r < 0 ? r : keyslot;
1461 int crypt_activate_by_keyfile(struct crypt_device *cd,
1464 const char *keyfile,
1465 size_t keyfile_size,
1468 crypt_status_info ci;
1469 struct volume_key *vk = NULL;
1470 char *passphrase_read = NULL;
1471 size_t passphrase_size_read;
1472 unsigned int key_count = 0;
1475 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1476 name ?: "", keyslot, keyfile ?: "[none]");
1479 ci = crypt_status(NULL, name);
1480 if (ci == CRYPT_INVALID)
1482 else if (ci >= CRYPT_ACTIVE) {
1483 log_err(cd, _("Device %s already exists.\n"), name);
1491 if (isPLAIN(cd->type)) {
1495 r = key_from_file(cd, _("Enter passphrase: "),
1496 &passphrase_read, &passphrase_size_read,
1497 keyfile, keyfile_size);
1501 r = process_key(cd, cd->plain_hdr.hash,
1502 cd->volume_key->keylength,
1503 passphrase_read, passphrase_size_read, &vk);
1507 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1508 } else if (isLUKS(cd->type)) {
1509 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1510 &passphrase_size_read, keyfile, keyfile_size);
1513 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase_read,
1514 passphrase_size_read, &cd->hdr, &vk, cd);
1520 r = LUKS1_activate(cd, name, vk, flags);
1525 } else if (isLOOPAES(cd->type)) {
1526 r = key_from_file(cd, NULL, &passphrase_read, &passphrase_size_read,
1527 keyfile, keyfile_size);
1530 r = LOOPAES_parse_keyfile(cd, &vk, cd->loopaes_hdr.hash, &key_count,
1531 passphrase_read, passphrase_size_read);
1535 r = LOOPAES_activate(cd, name, cd->loopaes_cipher,
1536 key_count, vk, flags);
1541 crypt_safe_free(passphrase_read);
1542 crypt_free_volume_key(vk);
1547 int crypt_activate_by_volume_key(struct crypt_device *cd,
1549 const char *volume_key,
1550 size_t volume_key_size,
1553 crypt_status_info ci;
1554 struct volume_key *vk = NULL;
1557 log_dbg("Activating volume %s by volume key.", name);
1560 ci = crypt_status(NULL, name);
1561 if (ci == CRYPT_INVALID)
1563 else if (ci >= CRYPT_ACTIVE) {
1564 log_err(cd, _("Device %s already exists.\n"), name);
1569 /* use key directly, no hash */
1570 if (isPLAIN(cd->type)) {
1574 if (!volume_key || !volume_key_size || !cd->volume_key ||
1575 volume_key_size != cd->volume_key->keylength) {
1576 log_err(cd, _("Incorrect volume key specified for plain device.\n"));
1580 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1584 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1585 } else if (isLUKS(cd->type)) {
1586 /* If key is not provided, try to use internal key */
1588 if (!cd->volume_key) {
1589 log_err(cd, _("Volume key does not match the volume.\n"));
1592 volume_key_size = cd->volume_key->keylength;
1593 volume_key = cd->volume_key->key;
1596 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1599 r = LUKS_verify_volume_key(&cd->hdr, vk);
1602 log_err(cd, _("Volume key does not match the volume.\n"));
1605 r = LUKS1_activate(cd, name, vk, flags);
1607 log_err(cd, _("Device type is not properly initialised.\n"));
1609 crypt_free_volume_key(vk);
1614 int crypt_deactivate(struct crypt_device *cd, const char *name)
1621 log_dbg("Deactivating volume %s.", name);
1623 if (!cd && dm_init(NULL, 1) < 0)
1626 switch (crypt_status(cd, name)) {
1628 r = dm_remove_device(name, 0, 0);
1631 log_err(cd, _("Device %s is busy.\n"), name);
1634 case CRYPT_INACTIVE:
1635 log_err(cd, _("Device %s is not active.\n"), name);
1639 log_err(cd, _("Invalid device %s.\n"), name);
1649 int crypt_volume_key_get(struct crypt_device *cd,
1652 size_t *volume_key_size,
1653 const char *passphrase,
1654 size_t passphrase_size)
1656 struct volume_key *vk = NULL;
1660 key_len = crypt_get_volume_key_size(cd);
1661 if (key_len > *volume_key_size) {
1662 log_err(cd, _("Volume key buffer too small.\n"));
1666 if (isPLAIN(cd->type) && cd->plain_hdr.hash) {
1667 r = process_key(cd, cd->plain_hdr.hash, key_len,
1668 passphrase, passphrase_size, &vk);
1670 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
1671 } else if (isLUKS(cd->type)) {
1672 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase,
1673 passphrase_size, &cd->hdr, &vk, cd);
1676 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
1679 memcpy(volume_key, vk->key, vk->keylength);
1680 *volume_key_size = vk->keylength;
1683 crypt_free_volume_key(vk);
1687 int crypt_volume_key_verify(struct crypt_device *cd,
1688 const char *volume_key,
1689 size_t volume_key_size)
1691 struct volume_key *vk;
1694 if (!isLUKS(cd->type)) {
1695 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1699 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1703 r = LUKS_verify_volume_key(&cd->hdr, vk);
1706 log_err(cd, _("Volume key does not match the volume.\n"));
1708 crypt_free_volume_key(vk);
1713 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
1715 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
1716 cd->timeout = timeout_sec;
1719 void crypt_set_password_retry(struct crypt_device *cd, int tries)
1721 log_dbg("Password retry count set to %d.", tries);
1725 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
1727 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
1728 cd->iteration_time = iteration_time_ms;
1731 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
1733 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
1734 cd->password_verify = password_verify ? 1 : 0;
1737 void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
1740 case CRYPT_RNG_URANDOM:
1741 case CRYPT_RNG_RANDOM:
1742 log_dbg("RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
1743 cd->rng_type = rng_type;
1747 int crypt_get_rng_type(struct crypt_device *cd)
1752 return cd->rng_type;
1755 int crypt_memory_lock(struct crypt_device *cd, int lock)
1757 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
1761 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
1765 if (!cd && dm_init(NULL, 1) < 0)
1766 return CRYPT_INVALID;
1768 r = dm_status_device(name);
1773 if (r < 0 && r != -ENODEV)
1774 return CRYPT_INVALID;
1777 return CRYPT_ACTIVE;
1782 return CRYPT_INACTIVE;
1785 static void hexprintICB(struct crypt_device *cd, char *d, int n)
1788 for(i = 0; i < n; i++)
1789 log_std(cd, "%02hhx ", (char)d[i]);
1792 int crypt_dump(struct crypt_device *cd)
1795 if (!isLUKS(cd->type)) { //FIXME
1796 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1800 log_std(cd, "LUKS header information for %s\n\n", mdata_device(cd));
1801 log_std(cd, "Version: \t%d\n", cd->hdr.version);
1802 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
1803 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
1804 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec);
1805 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
1806 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
1807 log_std(cd, "MK digest: \t");
1808 hexprintICB(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE);
1810 log_std(cd, "MK salt: \t");
1811 hexprintICB(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2);
1812 log_std(cd, "\n \t");
1813 hexprintICB(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
1815 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
1816 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
1817 for(i = 0; i < LUKS_NUMKEYS; i++) {
1818 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
1819 log_std(cd, "Key Slot %d: ENABLED\n",i);
1820 log_std(cd, "\tIterations: \t%d\n",
1821 cd->hdr.keyblock[i].passwordIterations);
1822 log_std(cd, "\tSalt: \t");
1823 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt,
1825 log_std(cd, "\n\t \t");
1826 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt +
1827 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
1830 log_std(cd, "\tKey material offset:\t%d\n",
1831 cd->hdr.keyblock[i].keyMaterialOffset);
1832 log_std(cd, "\tAF stripes: \t%d\n",
1833 cd->hdr.keyblock[i].stripes);
1836 log_std(cd, "Key Slot %d: DISABLED\n", i);
1842 const char *crypt_get_cipher(struct crypt_device *cd)
1844 if (isPLAIN(cd->type))
1845 return cd->plain_cipher;
1847 if (isLUKS(cd->type))
1848 return cd->hdr.cipherName;
1850 if (isLOOPAES(cd->type))
1851 return cd->loopaes_cipher;
1856 const char *crypt_get_cipher_mode(struct crypt_device *cd)
1858 if (isPLAIN(cd->type))
1859 return cd->plain_cipher_mode;
1861 if (isLUKS(cd->type))
1862 return cd->hdr.cipherMode;
1864 if (isLOOPAES(cd->type))
1865 return cd->loopaes_cipher_mode;
1870 const char *crypt_get_uuid(struct crypt_device *cd)
1872 if (isLUKS(cd->type))
1873 return cd->hdr.uuid;
1875 if (isPLAIN(cd->type))
1876 return cd->plain_uuid;
1878 if (isLOOPAES(cd->type))
1879 return cd->loopaes_uuid;
1884 const char *crypt_get_device_name(struct crypt_device *cd)
1890 int crypt_get_volume_key_size(struct crypt_device *cd)
1892 if (isPLAIN(cd->type) && cd->volume_key)
1893 return cd->volume_key->keylength;
1895 if (isLUKS(cd->type))
1896 return cd->hdr.keyBytes;
1898 if (isLOOPAES(cd->type))
1899 return cd->loopaes_key_size;
1904 uint64_t crypt_get_data_offset(struct crypt_device *cd)
1906 if (isPLAIN(cd->type))
1907 return cd->plain_hdr.offset;
1909 if (isLUKS(cd->type))
1910 return cd->hdr.payloadOffset;
1912 if (isLOOPAES(cd->type))
1913 return cd->loopaes_hdr.offset;
1918 uint64_t crypt_get_iv_offset(struct crypt_device *cd)
1920 if (isPLAIN(cd->type))
1921 return cd->plain_hdr.skip;
1923 if (isLUKS(cd->type))
1926 if (isLOOPAES(cd->type))
1927 return cd->loopaes_hdr.skip;
1932 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
1934 if (!isLUKS(cd->type)) {
1935 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1936 return CRYPT_SLOT_INVALID;
1939 return LUKS_keyslot_info(&cd->hdr, keyslot);
1942 int crypt_keyslot_max(const char *type)
1944 if (type && isLUKS(type))
1945 return LUKS_NUMKEYS;
1950 const char *crypt_get_type(struct crypt_device *cd)
1955 int crypt_get_active_device(struct crypt_device *cd __attribute__((unused)),
1957 struct crypt_active_device *cad)
1959 struct crypt_dm_active_device dmd;
1962 r = dm_query_device(name, 0, &dmd);
1966 cad->offset = dmd.offset;
1967 cad->iv_offset = dmd.iv_offset;
1968 cad->size = dmd.size;
1969 cad->flags = dmd.flags;