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-2012, Red Hat, Inc. All rights reserved.
7 * Copyright (C) 2009-2012, Milan Broz
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 #include "libcryptsetup.h"
41 struct device *device;
42 struct device *metadata_device;
44 struct volume_key *volume_key;
46 uint64_t iteration_time;
51 // FIXME: private binary headers and access it properly
52 // through sub-library (LUKS1, TCRYPT)
55 struct { /* used in CRYPT_LUKS1 */
57 uint64_t PBKDF2_per_sec;
59 struct { /* used in CRYPT_PLAIN */
60 struct crypt_params_plain hdr;
63 unsigned int key_size;
65 struct { /* used in CRYPT_LOOPAES */
66 struct crypt_params_loopaes hdr;
69 unsigned int key_size;
71 struct { /* used in CRYPT_VERITY */
72 struct crypt_params_verity hdr;
74 unsigned int root_hash_size;
77 struct { /* used in CRYPT_TCRYPT */
78 struct crypt_params_tcrypt params;
79 struct tcrypt_phdr hdr;
83 /* callbacks definitions */
84 void (*log)(int level, const char *msg, void *usrptr);
86 int (*confirm)(const char *msg, void *usrptr);
88 int (*password)(const char *msg, char *buf, size_t length, void *usrptr);
89 void *password_usrptr;
91 /* last error message */
92 char error[MAX_ERROR_LENGTH];
96 /* FIXME: not thread safe, remove this later */
97 static char global_error[MAX_ERROR_LENGTH] = {0};
100 static void (*_default_log)(int level, const char *msg, void *usrptr) = NULL;
101 static int _debug_level = 0;
103 void crypt_set_debug_level(int level)
105 _debug_level = level;
108 int crypt_get_debug_level(void)
113 static void crypt_set_error(struct crypt_device *cd, const char *error)
115 size_t size = strlen(error);
117 /* Set global error, ugly hack... */
118 strncpy(global_error, error, MAX_ERROR_LENGTH - 2);
119 if (size < MAX_ERROR_LENGTH && global_error[size - 1] == '\n')
120 global_error[size - 1] = '\0';
122 /* Set error string per context */
124 strncpy(cd->error, error, MAX_ERROR_LENGTH - 2);
125 if (size < MAX_ERROR_LENGTH && cd->error[size - 1] == '\n')
126 cd->error[size - 1] = '\0';
130 void crypt_log(struct crypt_device *cd, int level, const char *msg)
133 cd->log(level, msg, cd->log_usrptr);
134 else if (_default_log)
135 _default_log(level, msg, NULL);
137 if (level == CRYPT_LOG_ERROR)
138 crypt_set_error(cd, msg);
141 __attribute__((format(printf, 5, 6)))
142 void logger(struct crypt_device *cd, int level, const char *file,
143 int line, const char *format, ...)
148 va_start(argp, format);
150 if (vasprintf(&target, format, argp) > 0 ) {
152 crypt_log(cd, level, target);
154 } else if (_debug_level)
155 printf("# %s:%d %s\n", file ?: "?", line, target);
157 } else if (_debug_level)
158 printf("# %s\n", target);
166 static const char *mdata_device_path(struct crypt_device *cd)
168 return device_path(cd->metadata_device ?: cd->device);
172 struct device *crypt_metadata_device(struct crypt_device *cd)
174 return cd->metadata_device ?: cd->device;
177 struct device *crypt_data_device(struct crypt_device *cd)
182 int init_crypto(struct crypt_device *ctx)
186 crypt_fips_libcryptsetup_check(ctx);
188 r = crypt_random_init(ctx);
190 log_err(ctx, _("Cannot initialize crypto RNG backend.\n"));
194 r = crypt_backend_init(ctx);
196 log_err(ctx, _("Cannot initialize crypto backend.\n"));
198 log_dbg("Crypto backend (%s) initialized.", crypt_backend_version());
202 static int process_key(struct crypt_device *cd, const char *hash_name,
203 size_t key_size, const char *pass, size_t passLen,
204 struct volume_key **vk)
211 *vk = crypt_alloc_volume_key(key_size, NULL);
216 r = crypt_plain_hash(cd, hash_name, (*vk)->key, key_size, pass, passLen);
219 log_err(cd, _("Hash algorithm %s not supported.\n"),
222 log_err(cd, _("Key processing error (using hash %s).\n"),
224 crypt_free_volume_key(*vk);
228 } else if (passLen > key_size) {
229 memcpy((*vk)->key, pass, key_size);
231 memcpy((*vk)->key, pass, passLen);
237 static int isPLAIN(const char *type)
239 return (type && !strcmp(CRYPT_PLAIN, type));
242 static int isLUKS(const char *type)
244 return (type && !strcmp(CRYPT_LUKS1, type));
247 static int isLOOPAES(const char *type)
249 return (type && !strcmp(CRYPT_LOOPAES, type));
252 static int isVERITY(const char *type)
254 return (type && !strcmp(CRYPT_VERITY, type));
257 static int isTCRYPT(const char *type)
259 return (type && !strcmp(CRYPT_TCRYPT, type));
262 /* keyslot helpers */
263 static int keyslot_verify_or_find_empty(struct crypt_device *cd, int *keyslot)
265 if (*keyslot == CRYPT_ANY_SLOT) {
266 *keyslot = LUKS_keyslot_find_empty(&cd->u.luks1.hdr);
268 log_err(cd, _("All key slots full.\n"));
273 switch (LUKS_keyslot_info(&cd->u.luks1.hdr, *keyslot)) {
274 case CRYPT_SLOT_INVALID:
275 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
276 *keyslot, LUKS_NUMKEYS - 1);
278 case CRYPT_SLOT_INACTIVE:
281 log_err(cd, _("Key slot %d is full, please select another one.\n"),
290 * compares UUIDs returned by device-mapper (striped by cryptsetup) and uuid in header
292 static int crypt_uuid_cmp(const char *dm_uuid, const char *hdr_uuid)
297 if (!dm_uuid || !hdr_uuid)
300 str = strchr(dm_uuid, '-');
304 for (i = 0, j = 1; hdr_uuid[i]; i++) {
305 if (hdr_uuid[i] == '-')
308 if (!str[j] || str[j] == '-')
311 if (str[j] != hdr_uuid[i])
319 int PLAIN_activate(struct crypt_device *cd,
321 struct volume_key *vk,
326 char *dm_cipher = NULL;
327 enum devcheck device_check;
328 struct crypt_dm_active_device dmd = {
332 .data_device = crypt_data_device(cd),
336 .offset = crypt_get_data_offset(cd),
337 .iv_offset = crypt_get_iv_offset(cd),
341 if (dmd.flags & CRYPT_ACTIVATE_SHARED)
342 device_check = DEV_SHARED;
344 device_check = DEV_EXCL;
346 r = device_block_adjust(cd, dmd.data_device, device_check,
347 dmd.u.crypt.offset, &dmd.size, &dmd.flags);
351 if (crypt_get_cipher_mode(cd))
352 r = asprintf(&dm_cipher, "%s-%s", crypt_get_cipher(cd), crypt_get_cipher_mode(cd));
354 r = asprintf(&dm_cipher, "%s", crypt_get_cipher(cd));
358 dmd.u.crypt.cipher = dm_cipher;
359 log_dbg("Trying to activate PLAIN device %s using cipher %s.",
360 name, dmd.u.crypt.cipher);
362 r = dm_create_device(cd, name, CRYPT_PLAIN, &dmd, 0);
368 int crypt_confirm(struct crypt_device *cd, const char *msg)
370 if (!cd || !cd->confirm)
373 return cd->confirm(msg, cd->confirm_usrptr);
376 static int key_from_terminal(struct crypt_device *cd, char *msg, char **key,
377 size_t *key_len, int force_verify)
379 char *prompt = NULL, *device_name;
384 if (crypt_loop_device(crypt_get_device_name(cd)))
385 device_name = crypt_loop_backing_file(crypt_get_device_name(cd));
387 device_name = strdup(crypt_get_device_name(cd));
390 r = asprintf(&prompt, _("Enter passphrase for %s: "), device_name);
398 *key = crypt_safe_alloc(DEFAULT_PASSPHRASE_SIZE_MAX);
403 r = cd->password(msg, *key, DEFAULT_PASSPHRASE_SIZE_MAX,
404 cd->password_usrptr);
406 crypt_safe_free(*key);
411 r = crypt_get_key(msg, key, key_len, 0, 0, NULL, cd->timeout,
412 (force_verify || cd->password_verify), cd);
415 return (r < 0) ? r: 0;
418 static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslot,
419 struct volume_key **vk)
421 char *passphrase_read = NULL;
422 size_t passphrase_size_read;
423 int r = -EINVAL, eperm = 0, tries = cd->tries;
427 crypt_free_volume_key(*vk);
430 r = key_from_terminal(cd, NULL, &passphrase_read,
431 &passphrase_size_read, 0);
432 /* Continue if it is just passphrase verify mismatch */
438 r = LUKS_open_key_with_hdr(keyslot, passphrase_read,
439 passphrase_size_read, &cd->u.luks1.hdr, vk, cd);
442 crypt_safe_free(passphrase_read);
443 passphrase_read = NULL;
444 } while (r == -EPERM && (--tries > 0));
447 crypt_free_volume_key(*vk);
450 /* Report wrong passphrase if at least one try failed */
451 if (eperm && r == -EPIPE)
455 crypt_safe_free(passphrase_read);
459 static int key_from_file(struct crypt_device *cd, char *msg,
460 char **key, size_t *key_len,
461 const char *key_file, size_t key_offset,
464 return crypt_get_key(msg, key, key_len, key_offset, key_size, key_file,
468 void crypt_set_log_callback(struct crypt_device *cd,
469 void (*log)(int level, const char *msg, void *usrptr),
476 cd->log_usrptr = usrptr;
480 void crypt_set_confirm_callback(struct crypt_device *cd,
481 int (*confirm)(const char *msg, void *usrptr),
484 cd->confirm = confirm;
485 cd->confirm_usrptr = usrptr;
488 void crypt_set_password_callback(struct crypt_device *cd,
489 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
492 cd->password = password;
493 cd->password_usrptr = usrptr;
496 static void _get_error(char *error, char *buf, size_t size)
498 if (!buf || size < 1)
501 strncpy(buf, error, size - 1);
502 buf[size - 1] = '\0';
508 void crypt_last_error(struct crypt_device *cd, char *buf, size_t size)
511 return _get_error(cd->error, buf, size);
514 /* Deprecated global error interface */
515 void crypt_get_error(char *buf, size_t size)
517 return _get_error(global_error, buf, size);
520 const char *crypt_get_dir(void)
525 int crypt_init(struct crypt_device **cd, const char *device)
527 struct crypt_device *h = NULL;
533 log_dbg("Allocating crypt device %s context.", device);
535 if (!(h = malloc(sizeof(struct crypt_device))))
538 memset(h, 0, sizeof(*h));
540 r = device_alloc(&h->device, device);
546 h->iteration_time = 1000;
547 h->password_verify = 0;
549 h->rng_type = crypt_random_default_key_rng();
553 device_free(h->device);
558 static int crypt_check_data_device_size(struct crypt_device *cd)
561 uint64_t size, size_min;
563 /* Check data device size, require at least one sector */
564 size_min = crypt_get_data_offset(cd) << SECTOR_SHIFT ?: SECTOR_SIZE;
566 r = device_size(cd->device, &size);
570 if (size < size_min) {
571 log_err(cd, _("Header detected but device %s is too small.\n"),
572 device_path(cd->device));
579 int crypt_set_data_device(struct crypt_device *cd, const char *device)
581 struct device *dev = NULL;
584 log_dbg("Setting ciphertext data device to %s.", device ?: "(none)");
586 if (!isLUKS(cd->type) && !isVERITY(cd->type)) {
587 log_err(cd, _("This operation is not supported for this device type.\n"));
591 /* metadata device must be set */
592 if (!cd->device || !device)
595 r = device_alloc(&dev, device);
599 if (!cd->metadata_device) {
600 cd->metadata_device = cd->device;
602 device_free(cd->device);
606 return crypt_check_data_device_size(cd);
609 static int _crypt_load_luks1(struct crypt_device *cd, int require_header, int repair)
611 struct luks_phdr hdr;
618 r = LUKS_read_phdr(&hdr, require_header, repair, cd);
622 if (!cd->type && !(cd->type = strdup(CRYPT_LUKS1)))
625 memcpy(&cd->u.luks1.hdr, &hdr, sizeof(hdr));
630 static int _crypt_load_tcrypt(struct crypt_device *cd, struct crypt_params_tcrypt *params)
641 memcpy(&cd->u.tcrypt.params, params, sizeof(*params));
643 r = TCRYPT_read_phdr(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
645 cd->u.tcrypt.params.passphrase = NULL;
646 cd->u.tcrypt.params.passphrase_size = 0;
647 cd->u.tcrypt.params.keyfiles = NULL;
648 cd->u.tcrypt.params.keyfiles_count = 0;
653 if (!cd->type && !(cd->type = strdup(CRYPT_TCRYPT)))
659 static int _crypt_load_verity(struct crypt_device *cd, struct crypt_params_verity *params)
662 size_t sb_offset = 0;
668 if (params && params->flags & CRYPT_VERITY_NO_HEADER)
672 sb_offset = params->hash_area_offset;
674 r = VERITY_read_sb(cd, sb_offset, &cd->u.verity.uuid, &cd->u.verity.hdr);
679 cd->u.verity.hdr.flags = params->flags;
681 /* Hash availability checked in sb load */
682 cd->u.verity.root_hash_size = crypt_hash_size(cd->u.verity.hdr.hash_name);
683 if (cd->u.verity.root_hash_size > 4096)
686 if (!cd->type && !(cd->type = strdup(CRYPT_VERITY)))
689 if (params && params->data_device &&
690 (r = crypt_set_data_device(cd, params->data_device)) < 0)
696 static int _init_by_name_crypt(struct crypt_device *cd, const char *name)
698 struct crypt_dm_active_device dmd = {};
699 char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
702 r = dm_query_device(cd, name,
705 DM_ACTIVE_CRYPT_CIPHER |
706 DM_ACTIVE_CRYPT_KEYSIZE, &dmd);
710 if (isPLAIN(cd->type)) {
711 cd->u.plain.hdr.hash = NULL; /* no way to get this */
712 cd->u.plain.hdr.offset = dmd.u.crypt.offset;
713 cd->u.plain.hdr.skip = dmd.u.crypt.iv_offset;
714 cd->u.plain.key_size = dmd.u.crypt.vk->keylength;
716 r = crypt_parse_name_and_mode(dmd.u.crypt.cipher, cipher, NULL, cipher_mode);
718 cd->u.plain.cipher = strdup(cipher);
719 cd->u.plain.cipher_mode = strdup(cipher_mode);
721 } else if (isLOOPAES(cd->type)) {
722 cd->u.loopaes.hdr.offset = dmd.u.crypt.offset;
724 r = crypt_parse_name_and_mode(dmd.u.crypt.cipher, cipher,
725 &key_nums, cipher_mode);
727 cd->u.loopaes.cipher = strdup(cipher);
728 cd->u.loopaes.cipher_mode = strdup(cipher_mode);
729 /* version 3 uses last key for IV */
730 if (dmd.u.crypt.vk->keylength % key_nums)
732 cd->u.loopaes.key_size = dmd.u.crypt.vk->keylength / key_nums;
734 } else if (isLUKS(cd->type)) {
735 if (crypt_metadata_device(cd)) {
736 r = _crypt_load_luks1(cd, 0, 0);
738 log_dbg("LUKS device header does not match active device.");
744 /* check whether UUIDs match each other */
745 r = crypt_uuid_cmp(dmd.uuid, cd->u.luks1.hdr.uuid);
747 log_dbg("LUKS device header uuid: %s mismatches DM returned uuid %s",
748 cd->u.luks1.hdr.uuid, dmd.uuid);
755 } else if (isTCRYPT(cd->type)) {
756 r = TCRYPT_init_by_name(cd, name, &dmd, &cd->device,
757 &cd->u.tcrypt.params, &cd->u.tcrypt.hdr);
760 crypt_free_volume_key(dmd.u.crypt.vk);
761 device_free(dmd.data_device);
762 free(CONST_CAST(void*)dmd.u.crypt.cipher);
763 free(CONST_CAST(void*)dmd.uuid);
767 static int _init_by_name_verity(struct crypt_device *cd, const char *name)
769 struct crypt_params_verity params = {};
770 struct crypt_dm_active_device dmd = {
772 .u.verity.vp = ¶ms,
776 r = dm_query_device(cd, name,
778 DM_ACTIVE_VERITY_HASH_DEVICE |
779 DM_ACTIVE_VERITY_PARAMS, &dmd);
783 if (isVERITY(cd->type)) {
784 cd->u.verity.uuid = NULL; // FIXME
785 cd->u.verity.hdr.flags = CRYPT_VERITY_NO_HEADER; //FIXME
786 cd->u.verity.hdr.data_size = params.data_size;
787 cd->u.verity.root_hash_size = dmd.u.verity.root_hash_size;
788 cd->u.verity.root_hash = NULL;
789 cd->u.verity.hdr.hash_name = params.hash_name;
790 cd->u.verity.hdr.data_device = NULL;
791 cd->u.verity.hdr.hash_device = NULL;
792 cd->u.verity.hdr.data_block_size = params.data_block_size;
793 cd->u.verity.hdr.hash_block_size = params.hash_block_size;
794 cd->u.verity.hdr.hash_area_offset = dmd.u.verity.hash_offset;
795 cd->u.verity.hdr.hash_type = params.hash_type;
796 cd->u.verity.hdr.flags = params.flags;
797 cd->u.verity.hdr.salt_size = params.salt_size;
798 cd->u.verity.hdr.salt = params.salt;
799 cd->metadata_device = dmd.u.verity.hash_device;
802 device_free(dmd.data_device);
806 int crypt_init_by_name_and_header(struct crypt_device **cd,
808 const char *header_device)
810 crypt_status_info ci;
811 struct crypt_dm_active_device dmd;
814 log_dbg("Allocating crypt device context by device %s.", name);
816 ci = crypt_status(NULL, name);
817 if (ci == CRYPT_INVALID)
820 if (ci < CRYPT_ACTIVE) {
821 log_err(NULL, _("Device %s is not active.\n"), name);
825 r = dm_query_device(NULL, name, DM_ACTIVE_DEVICE | DM_ACTIVE_UUID, &dmd);
832 r = crypt_init(cd, header_device);
834 r = crypt_init(cd, device_path(dmd.data_device));
836 /* Underlying device disappeared but mapping still active */
837 if (!dmd.data_device || r == -ENOTBLK)
838 log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
841 /* Underlying device is not readable but crypt mapping exists */
843 device_free(dmd.data_device);
844 dmd.data_device = NULL;
845 r = crypt_init(cd, NULL);
853 if (!strncmp(CRYPT_PLAIN, dmd.uuid, sizeof(CRYPT_PLAIN)-1))
854 (*cd)->type = strdup(CRYPT_PLAIN);
855 else if (!strncmp(CRYPT_LOOPAES, dmd.uuid, sizeof(CRYPT_LOOPAES)-1))
856 (*cd)->type = strdup(CRYPT_LOOPAES);
857 else if (!strncmp(CRYPT_LUKS1, dmd.uuid, sizeof(CRYPT_LUKS1)-1))
858 (*cd)->type = strdup(CRYPT_LUKS1);
859 else if (!strncmp(CRYPT_VERITY, dmd.uuid, sizeof(CRYPT_VERITY)-1))
860 (*cd)->type = strdup(CRYPT_VERITY);
861 else if (!strncmp(CRYPT_TCRYPT, dmd.uuid, sizeof(CRYPT_TCRYPT)-1))
862 (*cd)->type = strdup(CRYPT_TCRYPT);
864 log_dbg("Unknown UUID set, some parameters are not set.");
866 log_dbg("Active device has no UUID set, some parameters are not set.");
869 r = crypt_set_data_device(*cd, device_path(dmd.data_device));
874 /* Try to initialise basic parameters from active device */
876 if (dmd.target == DM_CRYPT)
877 r = _init_by_name_crypt(*cd, name);
878 else if (dmd.target == DM_VERITY)
879 r = _init_by_name_verity(*cd, name);
885 device_free(dmd.data_device);
886 free(CONST_CAST(void*)dmd.uuid);
890 int crypt_init_by_name(struct crypt_device **cd, const char *name)
892 return crypt_init_by_name_and_header(cd, name, NULL);
895 static int _crypt_format_plain(struct crypt_device *cd,
897 const char *cipher_mode,
899 size_t volume_key_size,
900 struct crypt_params_plain *params)
902 if (!cipher || !cipher_mode) {
903 log_err(cd, _("Invalid plain crypt parameters.\n"));
907 if (volume_key_size > 1024) {
908 log_err(cd, _("Invalid key size.\n"));
913 log_err(cd, _("UUID is not supported for this crypt type.\n"));
917 if (!(cd->type = strdup(CRYPT_PLAIN)))
920 cd->u.plain.key_size = volume_key_size;
921 cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
925 cd->u.plain.cipher = strdup(cipher);
926 cd->u.plain.cipher_mode = strdup(cipher_mode);
929 if (params && params->hash)
930 cd->u.plain.hdr.hash = strdup(params->hash);
932 cd->u.plain.hdr.offset = params ? params->offset : 0;
933 cd->u.plain.hdr.skip = params ? params->skip : 0;
934 cd->u.plain.hdr.size = params ? params->size : 0;
936 if (!cd->u.plain.cipher || !cd->u.plain.cipher_mode)
942 static int _crypt_format_luks1(struct crypt_device *cd,
944 const char *cipher_mode,
946 const char *volume_key,
947 size_t volume_key_size,
948 struct crypt_params_luks1 *params)
951 unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
952 unsigned long alignment_offset = 0;
954 if (!crypt_metadata_device(cd)) {
955 log_err(cd, _("Can't format LUKS without device.\n"));
959 if (!(cd->type = strdup(CRYPT_LUKS1)))
963 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
966 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
971 if (params && params->data_device) {
972 cd->metadata_device = cd->device;
974 if (device_alloc(&cd->device, params->data_device) < 0)
976 required_alignment = params->data_alignment * SECTOR_SIZE;
977 } else if (params && params->data_alignment) {
978 required_alignment = params->data_alignment * SECTOR_SIZE;
980 device_topology_alignment(cd->device,
982 &alignment_offset, DEFAULT_DISK_ALIGNMENT);
984 /* Check early if we cannot allocate block device for key slot access */
985 r = device_block_adjust(cd, cd->device, DEV_OK, 0, NULL, NULL);
989 r = LUKS_generate_phdr(&cd->u.luks1.hdr, cd->volume_key, cipher, cipher_mode,
990 (params && params->hash) ? params->hash : "sha1",
992 required_alignment / SECTOR_SIZE,
993 alignment_offset / SECTOR_SIZE,
994 cd->iteration_time, &cd->u.luks1.PBKDF2_per_sec,
995 cd->metadata_device ? 1 : 0, cd);
999 /* Wipe first 8 sectors - fs magic numbers etc. */
1000 r = crypt_wipe(crypt_metadata_device(cd), 0, 8 * SECTOR_SIZE, CRYPT_WIPE_ZERO, 1);
1003 log_err(cd, _("Cannot format device %s which is still in use.\n"),
1004 mdata_device_path(cd));
1005 else if (r == -EACCES) {
1006 log_err(cd, _("Cannot format device %s, permission denied.\n"),
1007 mdata_device_path(cd));
1010 log_err(cd, _("Cannot wipe header on device %s.\n"),
1011 mdata_device_path(cd));
1016 r = LUKS_write_phdr(&cd->u.luks1.hdr, cd);
1021 static int _crypt_format_loopaes(struct crypt_device *cd,
1024 size_t volume_key_size,
1025 struct crypt_params_loopaes *params)
1027 if (!crypt_metadata_device(cd)) {
1028 log_err(cd, _("Can't format LOOPAES without device.\n"));
1032 if (volume_key_size > 1024) {
1033 log_err(cd, _("Invalid key size.\n"));
1038 log_err(cd, _("UUID is not supported for this crypt type.\n"));
1042 if (!(cd->type = strdup(CRYPT_LOOPAES)))
1045 cd->u.loopaes.key_size = volume_key_size;
1047 cd->u.loopaes.cipher = strdup(cipher ?: DEFAULT_LOOPAES_CIPHER);
1049 if (params && params->hash)
1050 cd->u.loopaes.hdr.hash = strdup(params->hash);
1052 cd->u.loopaes.hdr.offset = params ? params->offset : 0;
1053 cd->u.loopaes.hdr.skip = params ? params->skip : 0;
1058 static int _crypt_format_verity(struct crypt_device *cd,
1060 struct crypt_params_verity *params)
1062 int r = 0, hash_size;
1063 uint64_t data_device_size;
1065 if (!crypt_metadata_device(cd)) {
1066 log_err(cd, _("Can't format VERITY without device.\n"));
1070 if (!params || !params->data_device)
1073 if (params->hash_type > VERITY_MAX_HASH_TYPE) {
1074 log_err(cd, _("Unsupported VERITY hash type %d.\n"), params->hash_type);
1078 if (VERITY_BLOCK_SIZE_OK(params->data_block_size) ||
1079 VERITY_BLOCK_SIZE_OK(params->hash_block_size)) {
1080 log_err(cd, _("Unsupported VERITY block size.\n"));
1084 if (params->hash_area_offset % 512) {
1085 log_err(cd, _("Unsupported VERITY hash offset.\n"));
1089 if (!(cd->type = strdup(CRYPT_VERITY)))
1092 r = crypt_set_data_device(cd, params->data_device);
1095 if (!params->data_size) {
1096 r = device_size(cd->device, &data_device_size);
1100 cd->u.verity.hdr.data_size = data_device_size / params->data_block_size;
1102 cd->u.verity.hdr.data_size = params->data_size;
1104 hash_size = crypt_hash_size(params->hash_name);
1105 if (hash_size <= 0) {
1106 log_err(cd, _("Hash algorithm %s not supported.\n"),
1110 cd->u.verity.root_hash_size = hash_size;
1112 cd->u.verity.root_hash = malloc(cd->u.verity.root_hash_size);
1113 if (!cd->u.verity.root_hash)
1116 cd->u.verity.hdr.flags = params->flags;
1117 if (!(cd->u.verity.hdr.hash_name = strdup(params->hash_name)))
1119 cd->u.verity.hdr.data_device = NULL;
1120 cd->u.verity.hdr.data_block_size = params->data_block_size;
1121 cd->u.verity.hdr.hash_block_size = params->hash_block_size;
1122 cd->u.verity.hdr.hash_area_offset = params->hash_area_offset;
1123 cd->u.verity.hdr.hash_type = params->hash_type;
1124 cd->u.verity.hdr.flags = params->flags;
1125 cd->u.verity.hdr.salt_size = params->salt_size;
1126 if (!(cd->u.verity.hdr.salt = malloc(params->salt_size)))
1130 memcpy(CONST_CAST(char*)cd->u.verity.hdr.salt, params->salt,
1133 r = crypt_random_get(cd, CONST_CAST(char*)cd->u.verity.hdr.salt,
1134 params->salt_size, CRYPT_RND_SALT);
1138 if (params->flags & CRYPT_VERITY_CREATE_HASH) {
1139 r = VERITY_create(cd, &cd->u.verity.hdr,
1140 cd->u.verity.root_hash, cd->u.verity.root_hash_size);
1145 if (!(params->flags & CRYPT_VERITY_NO_HEADER)) {
1147 cd->u.verity.uuid = strdup(uuid);
1149 r = VERITY_UUID_generate(cd, &cd->u.verity.uuid);
1154 r = VERITY_write_sb(cd, cd->u.verity.hdr.hash_area_offset,
1161 int crypt_format(struct crypt_device *cd,
1164 const char *cipher_mode,
1166 const char *volume_key,
1167 size_t volume_key_size,
1176 log_dbg("Context already formatted as %s.", cd->type);
1180 log_dbg("Formatting device %s as type %s.", mdata_device_path(cd) ?: "(none)", type);
1182 r = init_crypto(cd);
1187 r = _crypt_format_plain(cd, cipher, cipher_mode,
1188 uuid, volume_key_size, params);
1189 else if (isLUKS(type))
1190 r = _crypt_format_luks1(cd, cipher, cipher_mode,
1191 uuid, volume_key, volume_key_size, params);
1192 else if (isLOOPAES(type))
1193 r = _crypt_format_loopaes(cd, cipher, uuid, volume_key_size, params);
1194 else if (isVERITY(type))
1195 r = _crypt_format_verity(cd, uuid, params);
1197 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
1204 crypt_free_volume_key(cd->volume_key);
1205 cd->volume_key = NULL;
1211 int crypt_load(struct crypt_device *cd,
1212 const char *requested_type,
1217 log_dbg("Trying to load %s crypt type from device %s.",
1218 requested_type ?: "any", mdata_device_path(cd) ?: "(none)");
1220 if (!crypt_metadata_device(cd))
1223 if (!requested_type || isLUKS(requested_type)) {
1224 if (cd->type && !isLUKS(cd->type)) {
1225 log_dbg("Context is already initialised to type %s", cd->type);
1229 r = _crypt_load_luks1(cd, 1, 0);
1230 } else if (isVERITY(requested_type)) {
1231 if (cd->type && !isVERITY(cd->type)) {
1232 log_dbg("Context is already initialised to type %s", cd->type);
1235 r = _crypt_load_verity(cd, params);
1236 } else if (isTCRYPT(requested_type)) {
1237 if (cd->type && !isTCRYPT(cd->type)) {
1238 log_dbg("Context is already initialised to type %s", cd->type);
1241 r = _crypt_load_tcrypt(cd, params);
1248 int crypt_repair(struct crypt_device *cd,
1249 const char *requested_type,
1250 void *params __attribute__((unused)))
1254 log_dbg("Trying to repair %s crypt type from device %s.",
1255 requested_type ?: "any", mdata_device_path(cd) ?: "(none)");
1257 if (!crypt_metadata_device(cd))
1260 if (requested_type && !isLUKS(requested_type))
1264 /* Load with repair */
1265 r = _crypt_load_luks1(cd, 1, 1);
1269 /* cd->type and header must be set in context */
1270 r = crypt_check_data_device_size(cd);
1279 int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
1281 struct crypt_dm_active_device dmd;
1284 /* Device context type must be initialised */
1288 log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
1290 r = dm_query_device(cd, name, DM_ACTIVE_DEVICE | DM_ACTIVE_CRYPT_CIPHER |
1291 DM_ACTIVE_UUID | DM_ACTIVE_CRYPT_KEYSIZE |
1292 DM_ACTIVE_CRYPT_KEY, &dmd);
1294 log_err(NULL, _("Device %s is not active.\n"), name);
1298 if (!dmd.uuid || dmd.target != DM_CRYPT) {
1303 r = device_block_adjust(cd, dmd.data_device, DEV_OK,
1304 dmd.u.crypt.offset, &new_size, &dmd.flags);
1308 if (new_size == dmd.size) {
1309 log_dbg("Device has already requested size %" PRIu64
1310 " sectors.", dmd.size);
1313 dmd.size = new_size;
1314 if (isTCRYPT(cd->type))
1317 r = dm_create_device(cd, name, cd->type, &dmd, 1);
1320 if (dmd.target == DM_CRYPT) {
1321 crypt_free_volume_key(dmd.u.crypt.vk);
1322 free(CONST_CAST(void*)dmd.u.crypt.cipher);
1324 free(CONST_CAST(void*)dmd.data_device);
1325 free(CONST_CAST(void*)dmd.uuid);
1330 int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
1332 if (!isLUKS(cd->type)) {
1333 log_err(cd, _("This operation is not supported for this device type.\n"));
1337 if (uuid && !strncmp(uuid, cd->u.luks1.hdr.uuid, sizeof(cd->u.luks1.hdr.uuid))) {
1338 log_dbg("UUID is the same as requested (%s) for device %s.",
1339 uuid, mdata_device_path(cd));
1344 log_dbg("Requested new UUID change to %s for %s.", uuid, mdata_device_path(cd));
1346 log_dbg("Requested new UUID refresh for %s.", mdata_device_path(cd));
1348 if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
1351 return LUKS_hdr_uuid_set(&cd->u.luks1.hdr, uuid, cd);
1354 int crypt_header_backup(struct crypt_device *cd,
1355 const char *requested_type,
1356 const char *backup_file)
1360 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1363 r = init_crypto(cd);
1367 log_dbg("Requested header backup of device %s (%s) to "
1368 "file %s.", mdata_device_path(cd), requested_type, backup_file);
1370 return LUKS_hdr_backup(backup_file, &cd->u.luks1.hdr, cd);
1373 int crypt_header_restore(struct crypt_device *cd,
1374 const char *requested_type,
1375 const char *backup_file)
1379 if (requested_type && !isLUKS(requested_type))
1382 if (cd->type && !isLUKS(cd->type))
1385 r = init_crypto(cd);
1389 log_dbg("Requested header restore to device %s (%s) from "
1390 "file %s.", mdata_device_path(cd), requested_type, backup_file);
1392 return LUKS_hdr_restore(backup_file, &cd->u.luks1.hdr, cd);
1395 void crypt_free(struct crypt_device *cd)
1398 log_dbg("Releasing crypt device %s context.", mdata_device_path(cd));
1401 crypt_free_volume_key(cd->volume_key);
1403 device_free(cd->device);
1404 device_free(cd->metadata_device);
1406 if (isPLAIN(cd->type)) {
1407 free(CONST_CAST(void*)cd->u.plain.hdr.hash);
1408 free(cd->u.plain.cipher);
1409 free(cd->u.plain.cipher_mode);
1410 } else if (isLOOPAES(cd->type)) {
1411 free(CONST_CAST(void*)cd->u.loopaes.hdr.hash);
1412 free(cd->u.loopaes.cipher);
1413 } else if (isVERITY(cd->type)) {
1414 free(CONST_CAST(void*)cd->u.verity.hdr.hash_name);
1415 free(CONST_CAST(void*)cd->u.verity.hdr.salt);
1416 free(cd->u.verity.root_hash);
1417 free(cd->u.verity.uuid);
1421 /* Some structures can contain keys (TCRYPT), wipe it */
1422 memset(cd, 0, sizeof(*cd));
1427 int crypt_suspend(struct crypt_device *cd,
1430 crypt_status_info ci;
1433 log_dbg("Suspending volume %s.", name);
1435 if (!cd || !isLUKS(cd->type)) {
1436 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1441 ci = crypt_status(NULL, name);
1442 if (ci < CRYPT_ACTIVE) {
1443 log_err(cd, _("Volume %s is not active.\n"), name);
1449 r = dm_status_suspended(cd, name);
1454 log_err(cd, _("Volume %s is already suspended.\n"), name);
1459 r = dm_suspend_and_wipe_key(cd, name);
1461 log_err(cd, _("Suspend is not supported for device %s.\n"), name);
1463 log_err(cd, _("Error during suspending device %s.\n"), name);
1469 int crypt_resume_by_passphrase(struct crypt_device *cd,
1472 const char *passphrase,
1473 size_t passphrase_size)
1475 struct volume_key *vk = NULL;
1478 log_dbg("Resuming volume %s.", name);
1480 if (!isLUKS(cd->type)) {
1481 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1486 r = dm_status_suspended(cd, name);
1491 log_err(cd, _("Volume %s is not suspended.\n"), name);
1496 r = LUKS_open_key_with_hdr(keyslot, passphrase, passphrase_size,
1497 &cd->u.luks1.hdr, &vk, cd);
1499 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1503 r = dm_resume_and_reinstate_key(cd, name, vk->keylength, vk->key);
1505 log_err(cd, _("Resume is not supported for device %s.\n"), name);
1507 log_err(cd, _("Error during resuming device %s.\n"), name);
1511 crypt_free_volume_key(vk);
1512 return r < 0 ? r : keyslot;
1515 int crypt_resume_by_keyfile_offset(struct crypt_device *cd,
1518 const char *keyfile,
1519 size_t keyfile_size,
1520 size_t keyfile_offset)
1522 struct volume_key *vk = NULL;
1523 char *passphrase_read = NULL;
1524 size_t passphrase_size_read;
1527 log_dbg("Resuming volume %s.", name);
1529 if (!isLUKS(cd->type)) {
1530 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1535 r = dm_status_suspended(cd, name);
1540 log_err(cd, _("Volume %s is not suspended.\n"), name);
1547 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1548 &passphrase_size_read, keyfile, keyfile_offset,
1553 r = LUKS_open_key_with_hdr(keyslot, passphrase_read,
1554 passphrase_size_read, &cd->u.luks1.hdr, &vk, cd);
1559 r = dm_resume_and_reinstate_key(cd, name, vk->keylength, vk->key);
1561 log_err(cd, _("Error during resuming device %s.\n"), name);
1563 crypt_safe_free(passphrase_read);
1564 crypt_free_volume_key(vk);
1565 return r < 0 ? r : keyslot;
1568 int crypt_resume_by_keyfile(struct crypt_device *cd,
1571 const char *keyfile,
1572 size_t keyfile_size)
1574 return crypt_resume_by_keyfile_offset(cd, name, keyslot,
1575 keyfile, keyfile_size, 0);
1578 // slot manipulation
1579 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1580 int keyslot, // -1 any
1581 const char *passphrase, // NULL -> terminal
1582 size_t passphrase_size,
1583 const char *new_passphrase, // NULL -> terminal
1584 size_t new_passphrase_size)
1586 struct volume_key *vk = NULL;
1587 char *password = NULL, *new_password = NULL;
1588 size_t passwordLen, new_passwordLen;
1591 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1592 "new passphrase %sprovided.",
1593 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1595 if (!isLUKS(cd->type)) {
1596 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1600 r = keyslot_verify_or_find_empty(cd, &keyslot);
1604 if (!LUKS_keyslot_active_count(&cd->u.luks1.hdr)) {
1605 /* No slots used, try to use pre-generated key in header */
1606 if (cd->volume_key) {
1607 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1608 r = vk ? 0 : -ENOMEM;
1610 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1613 } else if (passphrase) {
1614 /* Passphrase provided, use it to unlock existing keyslot */
1615 r = LUKS_open_key_with_hdr(CRYPT_ANY_SLOT, passphrase,
1616 passphrase_size, &cd->u.luks1.hdr, &vk, cd);
1618 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1619 r = key_from_terminal(cd, _("Enter any passphrase: "),
1620 &password, &passwordLen, 0);
1624 r = LUKS_open_key_with_hdr(CRYPT_ANY_SLOT, password,
1625 passwordLen, &cd->u.luks1.hdr, &vk, cd);
1626 crypt_safe_free(password);
1632 if (new_passphrase) {
1633 new_password = CONST_CAST(char*)new_passphrase;
1634 new_passwordLen = new_passphrase_size;
1636 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1637 &new_password, &new_passwordLen, 1);
1642 r = LUKS_set_key(keyslot, new_password, new_passwordLen,
1643 &cd->u.luks1.hdr, vk, cd->iteration_time, &cd->u.luks1.PBKDF2_per_sec, cd);
1648 if (!new_passphrase)
1649 crypt_safe_free(new_password);
1650 crypt_free_volume_key(vk);
1651 return r ?: keyslot;
1654 int crypt_keyslot_change_by_passphrase(struct crypt_device *cd,
1657 const char *passphrase,
1658 size_t passphrase_size,
1659 const char *new_passphrase,
1660 size_t new_passphrase_size)
1662 struct volume_key *vk = NULL;
1665 log_dbg("Changing passphrase from old keyslot %d to new %d.",
1666 keyslot_old, keyslot_new);
1668 if (!isLUKS(cd->type)) {
1669 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1673 r = LUKS_open_key_with_hdr(keyslot_old, passphrase, passphrase_size,
1674 &cd->u.luks1.hdr, &vk, cd);
1678 if (keyslot_old != CRYPT_ANY_SLOT && keyslot_old != r) {
1679 log_dbg("Keyslot mismatch.");
1684 if (keyslot_new == CRYPT_ANY_SLOT) {
1685 keyslot_new = LUKS_keyslot_find_empty(&cd->u.luks1.hdr);
1686 if (keyslot_new < 0)
1687 keyslot_new = keyslot_old;
1690 if (keyslot_old == keyslot_new) {
1691 log_dbg("Key slot %d is going to be overwritten.", keyslot_old);
1692 (void)crypt_keyslot_destroy(cd, keyslot_old);
1695 r = LUKS_set_key(keyslot_new, new_passphrase, new_passphrase_size,
1696 &cd->u.luks1.hdr, vk, cd->iteration_time,
1697 &cd->u.luks1.PBKDF2_per_sec, cd);
1699 if (keyslot_old == keyslot_new) {
1701 log_verbose(cd, _("Key slot %d changed.\n"), r);
1704 log_verbose(cd, _("Replaced with key slot %d.\n"), r);
1705 r = crypt_keyslot_destroy(cd, keyslot_old);
1709 log_err(cd, _("Failed to swap new key slot.\n"));
1711 crypt_free_volume_key(vk);
1712 return r ?: keyslot_new;
1715 int crypt_keyslot_add_by_keyfile_offset(struct crypt_device *cd,
1717 const char *keyfile,
1718 size_t keyfile_size,
1719 size_t keyfile_offset,
1720 const char *new_keyfile,
1721 size_t new_keyfile_size,
1722 size_t new_keyfile_offset)
1724 struct volume_key *vk = NULL;
1725 char *password = NULL; size_t passwordLen;
1726 char *new_password = NULL; size_t new_passwordLen;
1729 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1730 keyfile ?: "[none]", new_keyfile ?: "[none]");
1732 if (!isLUKS(cd->type)) {
1733 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1737 r = keyslot_verify_or_find_empty(cd, &keyslot);
1741 if (!LUKS_keyslot_active_count(&cd->u.luks1.hdr)) {
1742 /* No slots used, try to use pre-generated key in header */
1743 if (cd->volume_key) {
1744 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1745 r = vk ? 0 : -ENOMEM;
1747 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1751 /* Read password from file of (if NULL) from terminal */
1753 r = key_from_file(cd, _("Enter any passphrase: "),
1754 &password, &passwordLen,
1755 keyfile, keyfile_offset, keyfile_size);
1757 r = key_from_terminal(cd, _("Enter any passphrase: "),
1758 &password, &passwordLen, 0);
1762 r = LUKS_open_key_with_hdr(CRYPT_ANY_SLOT, password, passwordLen,
1763 &cd->u.luks1.hdr, &vk, cd);
1770 r = key_from_file(cd, _("Enter new passphrase for key slot: "),
1771 &new_password, &new_passwordLen, new_keyfile,
1772 new_keyfile_offset, new_keyfile_size);
1774 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1775 &new_password, &new_passwordLen, 1);
1779 r = LUKS_set_key(keyslot, new_password, new_passwordLen,
1780 &cd->u.luks1.hdr, vk, cd->iteration_time, &cd->u.luks1.PBKDF2_per_sec, cd);
1782 crypt_safe_free(password);
1783 crypt_safe_free(new_password);
1784 crypt_free_volume_key(vk);
1785 return r < 0 ? r : keyslot;
1788 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1790 const char *keyfile,
1791 size_t keyfile_size,
1792 const char *new_keyfile,
1793 size_t new_keyfile_size)
1795 return crypt_keyslot_add_by_keyfile_offset(cd, keyslot,
1796 keyfile, keyfile_size, 0,
1797 new_keyfile, new_keyfile_size, 0);
1800 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1802 const char *volume_key,
1803 size_t volume_key_size,
1804 const char *passphrase,
1805 size_t passphrase_size)
1807 struct volume_key *vk = NULL;
1809 char *new_password = NULL; size_t new_passwordLen;
1811 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1813 if (!isLUKS(cd->type)) {
1814 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1819 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1820 else if (cd->volume_key)
1821 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1826 r = LUKS_verify_volume_key(&cd->u.luks1.hdr, vk);
1828 log_err(cd, _("Volume key does not match the volume.\n"));
1832 r = keyslot_verify_or_find_empty(cd, &keyslot);
1837 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1838 &new_password, &new_passwordLen, 1);
1841 passphrase = new_password;
1842 passphrase_size = new_passwordLen;
1845 r = LUKS_set_key(keyslot, passphrase, passphrase_size,
1846 &cd->u.luks1.hdr, vk, cd->iteration_time, &cd->u.luks1.PBKDF2_per_sec, cd);
1848 crypt_safe_free(new_password);
1849 crypt_free_volume_key(vk);
1850 return (r < 0) ? r : keyslot;
1853 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1855 crypt_keyslot_info ki;
1857 log_dbg("Destroying keyslot %d.", keyslot);
1859 if (!isLUKS(cd->type)) {
1860 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1864 ki = crypt_keyslot_status(cd, keyslot);
1865 if (ki == CRYPT_SLOT_INVALID) {
1866 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1870 if (ki == CRYPT_SLOT_INACTIVE) {
1871 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1875 return LUKS_del_key(keyslot, &cd->u.luks1.hdr, cd);
1878 // activation/deactivation of device mapping
1879 int crypt_activate_by_passphrase(struct crypt_device *cd,
1882 const char *passphrase,
1883 size_t passphrase_size,
1886 crypt_status_info ci;
1887 struct volume_key *vk = NULL;
1888 char *read_passphrase = NULL;
1889 size_t passphraseLen = 0;
1892 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1893 name ? "Activating" : "Checking", name ?: "",
1894 keyslot, passphrase ? "" : "[none] ");
1897 ci = crypt_status(NULL, name);
1898 if (ci == CRYPT_INVALID)
1900 else if (ci >= CRYPT_ACTIVE) {
1901 log_err(cd, _("Device %s already exists.\n"), name);
1906 /* plain, use hashed passphrase */
1907 if (isPLAIN(cd->type)) {
1912 r = key_from_terminal(cd, NULL, &read_passphrase,
1916 passphrase = read_passphrase;
1917 passphrase_size = passphraseLen;
1920 r = process_key(cd, cd->u.plain.hdr.hash,
1921 cd->u.plain.key_size,
1922 passphrase, passphrase_size, &vk);
1926 r = PLAIN_activate(cd, name, vk, cd->u.plain.hdr.size, flags);
1928 } else if (isLUKS(cd->type)) {
1929 /* provided passphrase, do not retry */
1931 r = LUKS_open_key_with_hdr(keyslot, passphrase,
1932 passphrase_size, &cd->u.luks1.hdr, &vk, cd);
1934 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1939 r = LUKS1_activate(cd, name, vk, flags);
1944 crypt_safe_free(read_passphrase);
1945 crypt_free_volume_key(vk);
1947 return r < 0 ? r : keyslot;
1950 int crypt_activate_by_keyfile_offset(struct crypt_device *cd,
1953 const char *keyfile,
1954 size_t keyfile_size,
1955 size_t keyfile_offset,
1958 crypt_status_info ci;
1959 struct volume_key *vk = NULL;
1960 char *passphrase_read = NULL;
1961 size_t passphrase_size_read;
1962 unsigned int key_count = 0;
1965 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1966 name ?: "", keyslot, keyfile ?: "[none]");
1969 ci = crypt_status(NULL, name);
1970 if (ci == CRYPT_INVALID)
1972 else if (ci >= CRYPT_ACTIVE) {
1973 log_err(cd, _("Device %s already exists.\n"), name);
1981 if (isPLAIN(cd->type)) {
1985 r = key_from_file(cd, _("Enter passphrase: "),
1986 &passphrase_read, &passphrase_size_read,
1987 keyfile, keyfile_offset, keyfile_size);
1991 r = process_key(cd, cd->u.plain.hdr.hash,
1992 cd->u.plain.key_size,
1993 passphrase_read, passphrase_size_read, &vk);
1997 r = PLAIN_activate(cd, name, vk, cd->u.plain.hdr.size, flags);
1998 } else if (isLUKS(cd->type)) {
1999 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
2000 &passphrase_size_read, keyfile, keyfile_offset, keyfile_size);
2003 r = LUKS_open_key_with_hdr(keyslot, passphrase_read,
2004 passphrase_size_read, &cd->u.luks1.hdr, &vk, cd);
2010 r = LUKS1_activate(cd, name, vk, flags);
2015 } else if (isLOOPAES(cd->type)) {
2016 r = key_from_file(cd, NULL, &passphrase_read, &passphrase_size_read,
2017 keyfile, keyfile_offset, keyfile_size);
2020 r = LOOPAES_parse_keyfile(cd, &vk, cd->u.loopaes.hdr.hash, &key_count,
2021 passphrase_read, passphrase_size_read);
2025 r = LOOPAES_activate(cd, name, cd->u.loopaes.cipher,
2026 key_count, vk, flags);
2031 crypt_safe_free(passphrase_read);
2032 crypt_free_volume_key(vk);
2037 int crypt_activate_by_keyfile(struct crypt_device *cd,
2040 const char *keyfile,
2041 size_t keyfile_size,
2044 return crypt_activate_by_keyfile_offset(cd, name, keyslot, keyfile,
2045 keyfile_size, 0, flags);
2048 int crypt_activate_by_volume_key(struct crypt_device *cd,
2050 const char *volume_key,
2051 size_t volume_key_size,
2054 crypt_status_info ci;
2055 struct volume_key *vk = NULL;
2058 log_dbg("Activating volume %s by volume key.", name ?: "[none]");
2061 ci = crypt_status(NULL, name);
2062 if (ci == CRYPT_INVALID)
2064 else if (ci >= CRYPT_ACTIVE) {
2065 log_err(cd, _("Device %s already exists.\n"), name);
2070 /* use key directly, no hash */
2071 if (isPLAIN(cd->type)) {
2075 if (!volume_key || !volume_key_size || volume_key_size != cd->u.plain.key_size) {
2076 log_err(cd, _("Incorrect volume key specified for plain device.\n"));
2080 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2084 r = PLAIN_activate(cd, name, vk, cd->u.plain.hdr.size, flags);
2085 } else if (isLUKS(cd->type)) {
2086 /* If key is not provided, try to use internal key */
2088 if (!cd->volume_key) {
2089 log_err(cd, _("Volume key does not match the volume.\n"));
2092 volume_key_size = cd->volume_key->keylength;
2093 volume_key = cd->volume_key->key;
2096 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2099 r = LUKS_verify_volume_key(&cd->u.luks1.hdr, vk);
2102 log_err(cd, _("Volume key does not match the volume.\n"));
2105 r = LUKS1_activate(cd, name, vk, flags);
2106 } else if (isVERITY(cd->type)) {
2107 /* volume_key == root hash */
2108 if (!volume_key || !volume_key_size) {
2109 log_err(cd, _("Incorrect root hash specified for verity device.\n"));
2113 r = VERITY_activate(cd, name, volume_key, volume_key_size,
2114 &cd->u.verity.hdr, CRYPT_ACTIVATE_READONLY);
2117 free(cd->u.verity.root_hash);
2118 cd->u.verity.root_hash = NULL;
2120 cd->u.verity.root_hash_size = volume_key_size;
2121 if (!cd->u.verity.root_hash)
2122 cd->u.verity.root_hash = malloc(volume_key_size);
2123 if (cd->u.verity.root_hash)
2124 memcpy(cd->u.verity.root_hash, volume_key, volume_key_size);
2126 } else if (isTCRYPT(cd->type)) {
2129 r = TCRYPT_activate(cd, name, &cd->u.tcrypt.hdr,
2130 &cd->u.tcrypt.params, flags);
2132 log_err(cd, _("Device type is not properly initialised.\n"));
2134 crypt_free_volume_key(vk);
2139 int crypt_deactivate(struct crypt_device *cd, const char *name)
2146 log_dbg("Deactivating volume %s.", name);
2151 switch (crypt_status(cd, name)) {
2154 if (cd && isTCRYPT(cd->type))
2155 r = TCRYPT_deactivate(cd, name);
2157 r = dm_remove_device(cd, name, 0, 0);
2159 case CRYPT_INACTIVE:
2160 log_err(cd, _("Device %s is not active.\n"), name);
2164 log_err(cd, _("Invalid device %s.\n"), name);
2174 int crypt_volume_key_get(struct crypt_device *cd,
2177 size_t *volume_key_size,
2178 const char *passphrase,
2179 size_t passphrase_size)
2181 struct volume_key *vk = NULL;
2185 if (crypt_fips_mode()) {
2186 log_err(cd, _("Function not available in FIPS mode.\n"));
2190 key_len = crypt_get_volume_key_size(cd);
2191 if (key_len > *volume_key_size) {
2192 log_err(cd, _("Volume key buffer too small.\n"));
2196 if (isPLAIN(cd->type) && cd->u.plain.hdr.hash) {
2197 r = process_key(cd, cd->u.plain.hdr.hash, key_len,
2198 passphrase, passphrase_size, &vk);
2200 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
2201 } else if (isLUKS(cd->type)) {
2202 r = LUKS_open_key_with_hdr(keyslot, passphrase,
2203 passphrase_size, &cd->u.luks1.hdr, &vk, cd);
2204 } else if (isTCRYPT(cd->type)) {
2205 r = TCRYPT_get_volume_key(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params, &vk);
2207 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
2210 memcpy(volume_key, vk->key, vk->keylength);
2211 *volume_key_size = vk->keylength;
2214 crypt_free_volume_key(vk);
2218 int crypt_volume_key_verify(struct crypt_device *cd,
2219 const char *volume_key,
2220 size_t volume_key_size)
2222 struct volume_key *vk;
2225 if (!isLUKS(cd->type)) {
2226 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2230 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2234 r = LUKS_verify_volume_key(&cd->u.luks1.hdr, vk);
2237 log_err(cd, _("Volume key does not match the volume.\n"));
2239 crypt_free_volume_key(vk);
2244 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
2246 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
2247 cd->timeout = timeout_sec;
2250 void crypt_set_password_retry(struct crypt_device *cd, int tries)
2252 log_dbg("Password retry count set to %d.", tries);
2256 void crypt_set_iteration_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2258 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
2259 cd->iteration_time = iteration_time_ms;
2261 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2263 crypt_set_iteration_time(cd, iteration_time_ms);
2266 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
2268 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
2269 cd->password_verify = password_verify ? 1 : 0;
2272 void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
2275 case CRYPT_RNG_URANDOM:
2276 case CRYPT_RNG_RANDOM:
2277 log_dbg("RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
2278 cd->rng_type = rng_type;
2282 int crypt_get_rng_type(struct crypt_device *cd)
2287 return cd->rng_type;
2290 int crypt_memory_lock(struct crypt_device *cd, int lock)
2292 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
2296 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
2303 r = dm_status_device(cd, name);
2308 if (r < 0 && r != -ENODEV)
2309 return CRYPT_INVALID;
2312 return CRYPT_ACTIVE;
2317 return CRYPT_INACTIVE;
2320 static void hexprint(struct crypt_device *cd, const char *d, int n, const char *sep)
2323 for(i = 0; i < n; i++)
2324 log_std(cd, "%02hhx%s", (const char)d[i], sep);
2327 static int _luks_dump(struct crypt_device *cd)
2331 log_std(cd, "LUKS header information for %s\n\n", mdata_device_path(cd));
2332 log_std(cd, "Version: \t%d\n", cd->u.luks1.hdr.version);
2333 log_std(cd, "Cipher name: \t%s\n", cd->u.luks1.hdr.cipherName);
2334 log_std(cd, "Cipher mode: \t%s\n", cd->u.luks1.hdr.cipherMode);
2335 log_std(cd, "Hash spec: \t%s\n", cd->u.luks1.hdr.hashSpec);
2336 log_std(cd, "Payload offset:\t%d\n", cd->u.luks1.hdr.payloadOffset);
2337 log_std(cd, "MK bits: \t%d\n", cd->u.luks1.hdr.keyBytes * 8);
2338 log_std(cd, "MK digest: \t");
2339 hexprint(cd, cd->u.luks1.hdr.mkDigest, LUKS_DIGESTSIZE, " ");
2341 log_std(cd, "MK salt: \t");
2342 hexprint(cd, cd->u.luks1.hdr.mkDigestSalt, LUKS_SALTSIZE/2, " ");
2343 log_std(cd, "\n \t");
2344 hexprint(cd, cd->u.luks1.hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ");
2346 log_std(cd, "MK iterations: \t%d\n", cd->u.luks1.hdr.mkDigestIterations);
2347 log_std(cd, "UUID: \t%s\n\n", cd->u.luks1.hdr.uuid);
2348 for(i = 0; i < LUKS_NUMKEYS; i++) {
2349 if(cd->u.luks1.hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
2350 log_std(cd, "Key Slot %d: ENABLED\n",i);
2351 log_std(cd, "\tIterations: \t%d\n",
2352 cd->u.luks1.hdr.keyblock[i].passwordIterations);
2353 log_std(cd, "\tSalt: \t");
2354 hexprint(cd, cd->u.luks1.hdr.keyblock[i].passwordSalt,
2355 LUKS_SALTSIZE/2, " ");
2356 log_std(cd, "\n\t \t");
2357 hexprint(cd, cd->u.luks1.hdr.keyblock[i].passwordSalt +
2358 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ");
2361 log_std(cd, "\tKey material offset:\t%d\n",
2362 cd->u.luks1.hdr.keyblock[i].keyMaterialOffset);
2363 log_std(cd, "\tAF stripes: \t%d\n",
2364 cd->u.luks1.hdr.keyblock[i].stripes);
2367 log_std(cd, "Key Slot %d: DISABLED\n", i);
2372 static int _verity_dump(struct crypt_device *cd)
2374 log_std(cd, "VERITY header information for %s\n", mdata_device_path(cd));
2375 log_std(cd, "UUID: \t%s\n", cd->u.verity.uuid ?: "");
2376 log_std(cd, "Hash type: \t%u\n", cd->u.verity.hdr.hash_type);
2377 log_std(cd, "Data blocks: \t%" PRIu64 "\n", cd->u.verity.hdr.data_size);
2378 log_std(cd, "Data block size: \t%u\n", cd->u.verity.hdr.data_block_size);
2379 log_std(cd, "Hash block size: \t%u\n", cd->u.verity.hdr.hash_block_size);
2380 log_std(cd, "Hash algorithm: \t%s\n", cd->u.verity.hdr.hash_name);
2381 log_std(cd, "Salt: \t");
2382 if (cd->u.verity.hdr.salt_size)
2383 hexprint(cd, cd->u.verity.hdr.salt, cd->u.verity.hdr.salt_size, "");
2387 if (cd->u.verity.root_hash) {
2388 log_std(cd, "Root hash: \t");
2389 hexprint(cd, cd->u.verity.root_hash, cd->u.verity.root_hash_size, "");
2395 int crypt_dump(struct crypt_device *cd)
2397 if (isLUKS(cd->type))
2398 return _luks_dump(cd);
2399 else if (isVERITY(cd->type))
2400 return _verity_dump(cd);
2401 else if (isTCRYPT(cd->type))
2402 return TCRYPT_dump(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
2404 log_err(cd, _("Dump operation is not supported for this device type.\n"));
2408 const char *crypt_get_cipher(struct crypt_device *cd)
2410 if (isPLAIN(cd->type))
2411 return cd->u.plain.cipher;
2413 if (isLUKS(cd->type))
2414 return cd->u.luks1.hdr.cipherName;
2416 if (isLOOPAES(cd->type))
2417 return cd->u.loopaes.cipher;
2419 if (isTCRYPT(cd->type))
2420 return cd->u.tcrypt.params.cipher;
2425 const char *crypt_get_cipher_mode(struct crypt_device *cd)
2427 if (isPLAIN(cd->type))
2428 return cd->u.plain.cipher_mode;
2430 if (isLUKS(cd->type))
2431 return cd->u.luks1.hdr.cipherMode;
2433 if (isLOOPAES(cd->type))
2434 return cd->u.loopaes.cipher_mode;
2436 if (isTCRYPT(cd->type))
2437 return cd->u.tcrypt.params.mode;
2442 const char *crypt_get_uuid(struct crypt_device *cd)
2444 if (isLUKS(cd->type))
2445 return cd->u.luks1.hdr.uuid;
2447 if (isVERITY(cd->type))
2448 return cd->u.verity.uuid;
2453 const char *crypt_get_device_name(struct crypt_device *cd)
2455 const char *path = device_block_path(cd->device);
2458 path = device_path(cd->device);
2463 int crypt_get_volume_key_size(struct crypt_device *cd)
2465 if (isPLAIN(cd->type))
2466 return cd->u.plain.key_size;
2468 if (isLUKS(cd->type))
2469 return cd->u.luks1.hdr.keyBytes;
2471 if (isLOOPAES(cd->type))
2472 return cd->u.loopaes.key_size;
2474 if (isVERITY(cd->type))
2475 return cd->u.verity.root_hash_size;
2477 if (isTCRYPT(cd->type))
2478 return cd->u.tcrypt.params.key_size;
2483 uint64_t crypt_get_data_offset(struct crypt_device *cd)
2485 if (isPLAIN(cd->type))
2486 return cd->u.plain.hdr.offset;
2488 if (isLUKS(cd->type))
2489 return cd->u.luks1.hdr.payloadOffset;
2491 if (isLOOPAES(cd->type))
2492 return cd->u.loopaes.hdr.offset;
2494 if (isTCRYPT(cd->type))
2495 return TCRYPT_get_data_offset(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
2500 uint64_t crypt_get_iv_offset(struct crypt_device *cd)
2502 if (isPLAIN(cd->type))
2503 return cd->u.plain.hdr.skip;
2505 if (isLUKS(cd->type))
2508 if (isLOOPAES(cd->type))
2509 return cd->u.loopaes.hdr.skip;
2511 if (isTCRYPT(cd->type))
2512 return TCRYPT_get_iv_offset(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
2517 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
2519 if (!isLUKS(cd->type)) {
2520 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2521 return CRYPT_SLOT_INVALID;
2524 return LUKS_keyslot_info(&cd->u.luks1.hdr, keyslot);
2527 int crypt_keyslot_max(const char *type)
2529 if (type && isLUKS(type))
2530 return LUKS_NUMKEYS;
2535 int crypt_keyslot_area(struct crypt_device *cd,
2540 if (!isLUKS(cd->type))
2543 return LUKS_keyslot_area(&cd->u.luks1.hdr, keyslot, offset, length);
2546 const char *crypt_get_type(struct crypt_device *cd)
2551 int crypt_get_verity_info(struct crypt_device *cd,
2552 struct crypt_params_verity *vp)
2554 if (!isVERITY(cd->type) || !vp)
2557 vp->data_device = device_path(cd->device);
2558 vp->hash_device = mdata_device_path(cd);
2559 vp->hash_name = cd->u.verity.hdr.hash_name;
2560 vp->salt = cd->u.verity.hdr.salt;
2561 vp->salt_size = cd->u.verity.hdr.salt_size;
2562 vp->data_block_size = cd->u.verity.hdr.data_block_size;
2563 vp->hash_block_size = cd->u.verity.hdr.hash_block_size;
2564 vp->data_size = cd->u.verity.hdr.data_size;
2565 vp->hash_area_offset = cd->u.verity.hdr.hash_area_offset;
2566 vp->hash_type = cd->u.verity.hdr.hash_type;
2567 vp->flags = cd->u.verity.hdr.flags & CRYPT_VERITY_NO_HEADER;
2571 int crypt_get_active_device(struct crypt_device *cd, const char *name,
2572 struct crypt_active_device *cad)
2574 struct crypt_dm_active_device dmd;
2577 r = dm_query_device(cd, name, 0, &dmd);
2581 if (dmd.target != DM_CRYPT && dmd.target != DM_VERITY)
2584 if (cd && isTCRYPT(cd->type)) {
2585 cad->offset = TCRYPT_get_data_offset(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
2586 cad->iv_offset = TCRYPT_get_iv_offset(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
2588 cad->offset = dmd.u.crypt.offset;
2589 cad->iv_offset = dmd.u.crypt.iv_offset;
2591 cad->size = dmd.size;
2592 cad->flags = dmd.flags;