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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include "libcryptsetup.h"
39 struct device *device;
40 struct device *metadata_device;
42 struct volume_key *volume_key;
44 uint64_t iteration_time;
49 // FIXME: private binary headers and access it properly
50 // through sub-library (LUKS1, TCRYPT)
53 struct { /* used in CRYPT_LUKS1 */
55 uint64_t PBKDF2_per_sec;
57 struct { /* used in CRYPT_PLAIN */
58 struct crypt_params_plain hdr;
62 unsigned int key_size;
64 struct { /* used in CRYPT_LOOPAES */
65 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 = {
330 .uuid = crypt_get_uuid(cd),
333 .data_device = crypt_data_device(cd),
337 .offset = crypt_get_data_offset(cd),
338 .iv_offset = crypt_get_iv_offset(cd),
342 if (dmd.flags & CRYPT_ACTIVATE_SHARED)
343 device_check = DEV_SHARED;
345 device_check = DEV_EXCL;
347 r = device_block_adjust(cd, dmd.data_device, device_check,
348 dmd.u.crypt.offset, &dmd.size, &dmd.flags);
352 if (crypt_get_cipher_mode(cd))
353 r = asprintf(&dm_cipher, "%s-%s", crypt_get_cipher(cd), crypt_get_cipher_mode(cd));
355 r = asprintf(&dm_cipher, "%s", crypt_get_cipher(cd));
359 dmd.u.crypt.cipher = dm_cipher;
360 log_dbg("Trying to activate PLAIN device %s using cipher %s.",
361 name, dmd.u.crypt.cipher);
363 r = dm_create_device(cd, name, CRYPT_PLAIN, &dmd, 0);
366 if (!cd->u.plain.uuid && dm_query_device(cd, name, DM_ACTIVE_UUID, &dmd) >= 0)
367 cd->u.plain.uuid = CONST_CAST(char*)dmd.uuid;
373 int crypt_confirm(struct crypt_device *cd, const char *msg)
375 if (!cd || !cd->confirm)
378 return cd->confirm(msg, cd->confirm_usrptr);
381 static int key_from_terminal(struct crypt_device *cd, char *msg, char **key,
382 size_t *key_len, int force_verify)
384 char *prompt = NULL, *device_name;
389 if (crypt_loop_device(crypt_get_device_name(cd)))
390 device_name = crypt_loop_backing_file(crypt_get_device_name(cd));
392 device_name = strdup(crypt_get_device_name(cd));
395 r = asprintf(&prompt, _("Enter passphrase for %s: "), device_name);
403 *key = crypt_safe_alloc(DEFAULT_PASSPHRASE_SIZE_MAX);
408 r = cd->password(msg, *key, DEFAULT_PASSPHRASE_SIZE_MAX,
409 cd->password_usrptr);
411 crypt_safe_free(*key);
416 r = crypt_get_key(msg, key, key_len, 0, 0, NULL, cd->timeout,
417 (force_verify || cd->password_verify), cd);
420 return (r < 0) ? r: 0;
423 static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslot,
424 struct volume_key **vk)
426 char *passphrase_read = NULL;
427 size_t passphrase_size_read;
428 int r = -EINVAL, eperm = 0, tries = cd->tries;
432 crypt_free_volume_key(*vk);
435 r = key_from_terminal(cd, NULL, &passphrase_read,
436 &passphrase_size_read, 0);
437 /* Continue if it is just passphrase verify mismatch */
443 r = LUKS_open_key_with_hdr(keyslot, passphrase_read,
444 passphrase_size_read, &cd->u.luks1.hdr, vk, cd);
447 crypt_safe_free(passphrase_read);
448 passphrase_read = NULL;
449 } while (r == -EPERM && (--tries > 0));
452 crypt_free_volume_key(*vk);
455 /* Report wrong passphrase if at least one try failed */
456 if (eperm && r == -EPIPE)
460 crypt_safe_free(passphrase_read);
464 static int key_from_file(struct crypt_device *cd, char *msg,
465 char **key, size_t *key_len,
466 const char *key_file, size_t key_offset,
469 return crypt_get_key(msg, key, key_len, key_offset, key_size, key_file,
473 void crypt_set_log_callback(struct crypt_device *cd,
474 void (*log)(int level, const char *msg, void *usrptr),
481 cd->log_usrptr = usrptr;
485 void crypt_set_confirm_callback(struct crypt_device *cd,
486 int (*confirm)(const char *msg, void *usrptr),
489 cd->confirm = confirm;
490 cd->confirm_usrptr = usrptr;
493 void crypt_set_password_callback(struct crypt_device *cd,
494 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
497 cd->password = password;
498 cd->password_usrptr = usrptr;
501 static void _get_error(char *error, char *buf, size_t size)
503 if (!buf || size < 1)
506 strncpy(buf, error, size - 1);
507 buf[size - 1] = '\0';
513 void crypt_last_error(struct crypt_device *cd, char *buf, size_t size)
516 return _get_error(cd->error, buf, size);
519 /* Deprecated global error interface */
520 void crypt_get_error(char *buf, size_t size)
522 return _get_error(global_error, buf, size);
525 const char *crypt_get_dir(void)
530 int crypt_init(struct crypt_device **cd, const char *device)
532 struct crypt_device *h = NULL;
538 log_dbg("Allocating crypt device %s context.", device);
540 if (!(h = malloc(sizeof(struct crypt_device))))
543 memset(h, 0, sizeof(*h));
545 r = device_alloc(&h->device, device);
551 h->iteration_time = 1000;
552 h->password_verify = 0;
554 h->rng_type = crypt_random_default_key_rng();
558 device_free(h->device);
563 static int crypt_check_data_device_size(struct crypt_device *cd)
566 uint64_t size, size_min;
568 /* Check data device size, require at least one sector */
569 size_min = crypt_get_data_offset(cd) << SECTOR_SHIFT ?: SECTOR_SIZE;
571 r = device_size(cd->device, &size);
575 if (size < size_min) {
576 log_err(cd, _("Header detected but device %s is too small.\n"),
577 device_path(cd->device));
584 int crypt_set_data_device(struct crypt_device *cd, const char *device)
586 struct device *dev = NULL;
589 log_dbg("Setting ciphertext data device to %s.", device ?: "(none)");
591 if (!isLUKS(cd->type) && !isVERITY(cd->type)) {
592 log_err(cd, _("This operation is not supported for this device type.\n"));
596 /* metadata device must be set */
597 if (!cd->device || !device)
600 r = device_alloc(&dev, device);
604 if (!cd->metadata_device) {
605 cd->metadata_device = cd->device;
607 device_free(cd->device);
611 return crypt_check_data_device_size(cd);
614 static int _crypt_load_luks1(struct crypt_device *cd, int require_header, int repair)
616 struct luks_phdr hdr;
623 r = LUKS_read_phdr(&hdr, require_header, repair, cd);
627 if (!cd->type && !(cd->type = strdup(CRYPT_LUKS1)))
630 memcpy(&cd->u.luks1.hdr, &hdr, sizeof(hdr));
635 static int _crypt_load_tcrypt(struct crypt_device *cd, struct crypt_params_tcrypt *params)
643 memcpy(&cd->u.tcrypt.params, params, sizeof(*params));
645 r = TCRYPT_read_phdr(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
647 cd->u.tcrypt.params.passphrase = NULL;
648 cd->u.tcrypt.params.passphrase_size = 0;
649 cd->u.tcrypt.params.keyfiles = NULL;
650 cd->u.tcrypt.params.keyfiles_count = 0;
655 if (!cd->type && !(cd->type = strdup(CRYPT_TCRYPT)))
661 static int _crypt_load_verity(struct crypt_device *cd, struct crypt_params_verity *params)
664 size_t sb_offset = 0;
670 if (params && params->flags & CRYPT_VERITY_NO_HEADER)
674 sb_offset = params->hash_area_offset;
676 r = VERITY_read_sb(cd, sb_offset, &cd->u.verity.uuid, &cd->u.verity.hdr);
681 cd->u.verity.hdr.flags = params->flags;
683 /* Hash availability checked in sb load */
684 cd->u.verity.root_hash_size = crypt_hash_size(cd->u.verity.hdr.hash_name);
685 if (cd->u.verity.root_hash_size > 4096)
688 if (!cd->type && !(cd->type = strdup(CRYPT_VERITY)))
691 if (params && params->data_device &&
692 (r = crypt_set_data_device(cd, params->data_device)) < 0)
698 static int _init_by_name_crypt(struct crypt_device *cd, const char *name)
700 struct crypt_dm_active_device dmd = {};
701 char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
704 r = dm_query_device(cd, name,
707 DM_ACTIVE_CRYPT_CIPHER |
708 DM_ACTIVE_CRYPT_KEYSIZE, &dmd);
712 if (isPLAIN(cd->type)) {
713 cd->u.plain.uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
714 cd->u.plain.hdr.hash = NULL; /* no way to get this */
715 cd->u.plain.hdr.offset = dmd.u.crypt.offset;
716 cd->u.plain.hdr.skip = dmd.u.crypt.iv_offset;
717 cd->u.plain.key_size = dmd.u.crypt.vk->keylength;
719 r = crypt_parse_name_and_mode(dmd.u.crypt.cipher, cipher, NULL, cipher_mode);
721 cd->u.plain.cipher = strdup(cipher);
722 cd->u.plain.cipher_mode = strdup(cipher_mode);
724 } else if (isLOOPAES(cd->type)) {
725 cd->u.loopaes.uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
726 cd->u.loopaes.hdr.offset = dmd.u.crypt.offset;
728 r = crypt_parse_name_and_mode(dmd.u.crypt.cipher, cipher,
729 &key_nums, cipher_mode);
731 cd->u.loopaes.cipher = strdup(cipher);
732 cd->u.loopaes.cipher_mode = strdup(cipher_mode);
733 /* version 3 uses last key for IV */
734 if (dmd.u.crypt.vk->keylength % key_nums)
736 cd->u.loopaes.key_size = dmd.u.crypt.vk->keylength / key_nums;
738 } else if (isLUKS(cd->type)) {
739 if (crypt_metadata_device(cd)) {
740 r = _crypt_load_luks1(cd, 0, 0);
742 log_dbg("LUKS device header does not match active device.");
748 /* check whether UUIDs match each other */
749 r = crypt_uuid_cmp(dmd.uuid, cd->u.luks1.hdr.uuid);
751 log_dbg("LUKS device header uuid: %s mismatches DM returned uuid %s",
752 cd->u.luks1.hdr.uuid, dmd.uuid);
759 } else if (isTCRYPT(cd->type)) {
760 r = TCRYPT_init_by_name(cd, name, &dmd, &cd->device,
761 &cd->u.tcrypt.params, &cd->u.tcrypt.hdr);
764 crypt_free_volume_key(dmd.u.crypt.vk);
765 device_free(dmd.data_device);
766 free(CONST_CAST(void*)dmd.u.crypt.cipher);
767 free(CONST_CAST(void*)dmd.uuid);
771 static int _init_by_name_verity(struct crypt_device *cd, const char *name)
773 struct crypt_params_verity params = {};
774 struct crypt_dm_active_device dmd = {
776 .u.verity.vp = ¶ms,
780 r = dm_query_device(cd, name,
783 DM_ACTIVE_VERITY_HASH_DEVICE |
784 DM_ACTIVE_VERITY_PARAMS, &dmd);
788 if (isVERITY(cd->type)) {
789 cd->u.verity.uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
790 cd->u.verity.hdr.flags = CRYPT_VERITY_NO_HEADER; //FIXME
791 cd->u.verity.hdr.data_size = params.data_size;
792 cd->u.verity.root_hash_size = dmd.u.verity.root_hash_size;
793 cd->u.verity.root_hash = NULL;
794 cd->u.verity.hdr.hash_name = params.hash_name;
795 cd->u.verity.hdr.data_device = NULL;
796 cd->u.verity.hdr.hash_device = NULL;
797 cd->u.verity.hdr.data_block_size = params.data_block_size;
798 cd->u.verity.hdr.hash_block_size = params.hash_block_size;
799 cd->u.verity.hdr.hash_area_offset = dmd.u.verity.hash_offset;
800 cd->u.verity.hdr.hash_type = params.hash_type;
801 cd->u.verity.hdr.flags = params.flags;
802 cd->u.verity.hdr.salt_size = params.salt_size;
803 cd->u.verity.hdr.salt = params.salt;
804 cd->metadata_device = dmd.u.verity.hash_device;
807 device_free(dmd.data_device);
808 free(CONST_CAST(void*)dmd.uuid);
812 int crypt_init_by_name_and_header(struct crypt_device **cd,
814 const char *header_device)
816 crypt_status_info ci;
817 struct crypt_dm_active_device dmd;
820 log_dbg("Allocating crypt device context by device %s.", name);
822 ci = crypt_status(NULL, name);
823 if (ci == CRYPT_INVALID)
826 if (ci < CRYPT_ACTIVE) {
827 log_err(NULL, _("Device %s is not active.\n"), name);
831 r = dm_query_device(NULL, name, DM_ACTIVE_DEVICE | DM_ACTIVE_UUID, &dmd);
838 r = crypt_init(cd, header_device);
840 r = crypt_init(cd, device_path(dmd.data_device));
842 /* Underlying device disappeared but mapping still active */
843 if (!dmd.data_device || r == -ENOTBLK)
844 log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
847 /* Underlying device is not readable but crypt mapping exists */
849 device_free(dmd.data_device);
850 dmd.data_device = NULL;
851 r = crypt_init(cd, NULL);
859 if (!strncmp(CRYPT_PLAIN, dmd.uuid, sizeof(CRYPT_PLAIN)-1))
860 (*cd)->type = strdup(CRYPT_PLAIN);
861 else if (!strncmp(CRYPT_LOOPAES, dmd.uuid, sizeof(CRYPT_LOOPAES)-1))
862 (*cd)->type = strdup(CRYPT_LOOPAES);
863 else if (!strncmp(CRYPT_LUKS1, dmd.uuid, sizeof(CRYPT_LUKS1)-1))
864 (*cd)->type = strdup(CRYPT_LUKS1);
865 else if (!strncmp(CRYPT_VERITY, dmd.uuid, sizeof(CRYPT_VERITY)-1))
866 (*cd)->type = strdup(CRYPT_VERITY);
867 else if (!strncmp(CRYPT_TCRYPT, dmd.uuid, sizeof(CRYPT_TCRYPT)-1))
868 (*cd)->type = strdup(CRYPT_TCRYPT);
870 log_dbg("Unknown UUID set, some parameters are not set.");
872 log_dbg("Active device has no UUID set, some parameters are not set.");
875 r = crypt_set_data_device(*cd, device_path(dmd.data_device));
880 /* Try to initialise basic parameters from active device */
882 if (dmd.target == DM_CRYPT)
883 r = _init_by_name_crypt(*cd, name);
884 else if (dmd.target == DM_VERITY)
885 r = _init_by_name_verity(*cd, name);
891 device_free(dmd.data_device);
892 free(CONST_CAST(void*)dmd.uuid);
896 int crypt_init_by_name(struct crypt_device **cd, const char *name)
898 return crypt_init_by_name_and_header(cd, name, NULL);
901 static int _crypt_format_plain(struct crypt_device *cd,
903 const char *cipher_mode,
905 size_t volume_key_size,
906 struct crypt_params_plain *params)
908 if (!cipher || !cipher_mode) {
909 log_err(cd, _("Invalid plain crypt parameters.\n"));
913 if (volume_key_size > 1024) {
914 log_err(cd, _("Invalid key size.\n"));
918 if (!(cd->type = strdup(CRYPT_PLAIN)))
921 cd->u.plain.key_size = volume_key_size;
922 cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
926 cd->u.plain.cipher = strdup(cipher);
927 cd->u.plain.cipher_mode = strdup(cipher_mode);
930 cd->u.plain.uuid = strdup(uuid);
932 if (params && params->hash)
933 cd->u.plain.hdr.hash = strdup(params->hash);
935 cd->u.plain.hdr.offset = params ? params->offset : 0;
936 cd->u.plain.hdr.skip = params ? params->skip : 0;
937 cd->u.plain.hdr.size = params ? params->size : 0;
939 if (!cd->u.plain.cipher || !cd->u.plain.cipher_mode)
945 static int _crypt_format_luks1(struct crypt_device *cd,
947 const char *cipher_mode,
949 const char *volume_key,
950 size_t volume_key_size,
951 struct crypt_params_luks1 *params)
954 unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
955 unsigned long alignment_offset = 0;
957 if (!crypt_metadata_device(cd)) {
958 log_err(cd, _("Can't format LUKS without device.\n"));
962 if (!(cd->type = strdup(CRYPT_LUKS1)))
966 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
969 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
974 if (params && params->data_device) {
975 cd->metadata_device = cd->device;
977 if (device_alloc(&cd->device, params->data_device) < 0)
979 required_alignment = params->data_alignment * SECTOR_SIZE;
980 } else if (params && params->data_alignment) {
981 required_alignment = params->data_alignment * SECTOR_SIZE;
983 device_topology_alignment(cd->device,
985 &alignment_offset, DEFAULT_DISK_ALIGNMENT);
987 /* Check early if we cannot allocate block device for key slot access */
988 r = device_block_adjust(cd, cd->device, DEV_OK, 0, NULL, NULL);
992 r = LUKS_generate_phdr(&cd->u.luks1.hdr, cd->volume_key, cipher, cipher_mode,
993 (params && params->hash) ? params->hash : "sha1",
995 required_alignment / SECTOR_SIZE,
996 alignment_offset / SECTOR_SIZE,
997 cd->iteration_time, &cd->u.luks1.PBKDF2_per_sec,
998 cd->metadata_device ? 1 : 0, cd);
1002 /* Wipe first 8 sectors - fs magic numbers etc. */
1003 r = crypt_wipe(crypt_metadata_device(cd), 0, 8 * SECTOR_SIZE, CRYPT_WIPE_ZERO, 1);
1006 log_err(cd, _("Cannot format device %s which is still in use.\n"),
1007 mdata_device_path(cd));
1008 else if (r == -EACCES) {
1009 log_err(cd, _("Cannot format device %s, permission denied.\n"),
1010 mdata_device_path(cd));
1013 log_err(cd, _("Cannot wipe header on device %s.\n"),
1014 mdata_device_path(cd));
1019 r = LUKS_write_phdr(&cd->u.luks1.hdr, cd);
1024 static int _crypt_format_loopaes(struct crypt_device *cd,
1027 size_t volume_key_size,
1028 struct crypt_params_loopaes *params)
1030 if (!crypt_metadata_device(cd)) {
1031 log_err(cd, _("Can't format LOOPAES without device.\n"));
1035 if (volume_key_size > 1024) {
1036 log_err(cd, _("Invalid key size.\n"));
1040 if (!(cd->type = strdup(CRYPT_LOOPAES)))
1043 cd->u.loopaes.key_size = volume_key_size;
1045 cd->u.loopaes.cipher = strdup(cipher ?: DEFAULT_LOOPAES_CIPHER);
1048 cd->u.loopaes.uuid = strdup(uuid);
1050 if (params && params->hash)
1051 cd->u.loopaes.hdr.hash = strdup(params->hash);
1053 cd->u.loopaes.hdr.offset = params ? params->offset : 0;
1054 cd->u.loopaes.hdr.skip = params ? params->skip : 0;
1059 static int _crypt_format_verity(struct crypt_device *cd,
1061 struct crypt_params_verity *params)
1063 int r = 0, hash_size;
1064 uint64_t data_device_size;
1066 if (!crypt_metadata_device(cd)) {
1067 log_err(cd, _("Can't format VERITY without device.\n"));
1071 if (!params || !params->data_device)
1074 if (params->hash_type > VERITY_MAX_HASH_TYPE) {
1075 log_err(cd, _("Unsupported VERITY hash type %d.\n"), params->hash_type);
1079 if (VERITY_BLOCK_SIZE_OK(params->data_block_size) ||
1080 VERITY_BLOCK_SIZE_OK(params->hash_block_size)) {
1081 log_err(cd, _("Unsupported VERITY block size.\n"));
1085 if (params->hash_area_offset % 512) {
1086 log_err(cd, _("Unsupported VERITY hash offset.\n"));
1090 if (!(cd->type = strdup(CRYPT_VERITY)))
1093 r = crypt_set_data_device(cd, params->data_device);
1096 if (!params->data_size) {
1097 r = device_size(cd->device, &data_device_size);
1101 cd->u.verity.hdr.data_size = data_device_size / params->data_block_size;
1103 cd->u.verity.hdr.data_size = params->data_size;
1105 hash_size = crypt_hash_size(params->hash_name);
1106 if (hash_size <= 0) {
1107 log_err(cd, _("Hash algorithm %s not supported.\n"),
1111 cd->u.verity.root_hash_size = hash_size;
1113 cd->u.verity.root_hash = malloc(cd->u.verity.root_hash_size);
1114 if (!cd->u.verity.root_hash)
1117 cd->u.verity.hdr.flags = params->flags;
1118 if (!(cd->u.verity.hdr.hash_name = strdup(params->hash_name)))
1120 cd->u.verity.hdr.data_device = NULL;
1121 cd->u.verity.hdr.data_block_size = params->data_block_size;
1122 cd->u.verity.hdr.hash_block_size = params->hash_block_size;
1123 cd->u.verity.hdr.hash_area_offset = params->hash_area_offset;
1124 cd->u.verity.hdr.hash_type = params->hash_type;
1125 cd->u.verity.hdr.flags = params->flags;
1126 cd->u.verity.hdr.salt_size = params->salt_size;
1127 if (!(cd->u.verity.hdr.salt = malloc(params->salt_size)))
1131 memcpy(CONST_CAST(char*)cd->u.verity.hdr.salt, params->salt,
1134 r = crypt_random_get(cd, CONST_CAST(char*)cd->u.verity.hdr.salt,
1135 params->salt_size, CRYPT_RND_SALT);
1139 if (params->flags & CRYPT_VERITY_CREATE_HASH) {
1140 r = VERITY_create(cd, &cd->u.verity.hdr,
1141 cd->u.verity.root_hash, cd->u.verity.root_hash_size);
1146 if (!(params->flags & CRYPT_VERITY_NO_HEADER)) {
1148 cd->u.verity.uuid = strdup(uuid);
1150 r = VERITY_UUID_generate(cd, &cd->u.verity.uuid);
1155 r = VERITY_write_sb(cd, cd->u.verity.hdr.hash_area_offset,
1162 int crypt_format(struct crypt_device *cd,
1165 const char *cipher_mode,
1167 const char *volume_key,
1168 size_t volume_key_size,
1177 log_dbg("Context already formatted as %s.", cd->type);
1181 log_dbg("Formatting device %s as type %s.", mdata_device_path(cd) ?: "(none)", type);
1183 r = init_crypto(cd);
1188 r = _crypt_format_plain(cd, cipher, cipher_mode,
1189 uuid, volume_key_size, params);
1190 else if (isLUKS(type))
1191 r = _crypt_format_luks1(cd, cipher, cipher_mode,
1192 uuid, volume_key, volume_key_size, params);
1193 else if (isLOOPAES(type))
1194 r = _crypt_format_loopaes(cd, cipher, uuid, volume_key_size, params);
1195 else if (isVERITY(type))
1196 r = _crypt_format_verity(cd, uuid, params);
1198 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
1205 crypt_free_volume_key(cd->volume_key);
1206 cd->volume_key = NULL;
1212 int crypt_load(struct crypt_device *cd,
1213 const char *requested_type,
1218 log_dbg("Trying to load %s crypt type from device %s.",
1219 requested_type ?: "any", mdata_device_path(cd) ?: "(none)");
1221 if (!crypt_metadata_device(cd))
1224 if (!requested_type || isLUKS(requested_type)) {
1225 if (cd->type && !isLUKS(cd->type)) {
1226 log_dbg("Context is already initialised to type %s", cd->type);
1230 r = _crypt_load_luks1(cd, 1, 0);
1231 } else if (isVERITY(requested_type)) {
1232 if (cd->type && !isVERITY(cd->type)) {
1233 log_dbg("Context is already initialised to type %s", cd->type);
1236 r = _crypt_load_verity(cd, params);
1237 } else if (isTCRYPT(requested_type)) {
1238 if (cd->type && !isTCRYPT(cd->type)) {
1239 log_dbg("Context is already initialised to type %s", cd->type);
1242 r = _crypt_load_tcrypt(cd, params);
1249 int crypt_repair(struct crypt_device *cd,
1250 const char *requested_type,
1251 void *params __attribute__((unused)))
1255 log_dbg("Trying to repair %s crypt type from device %s.",
1256 requested_type ?: "any", mdata_device_path(cd) ?: "(none)");
1258 if (!crypt_metadata_device(cd))
1261 if (requested_type && !isLUKS(requested_type))
1265 /* Load with repair */
1266 r = _crypt_load_luks1(cd, 1, 1);
1270 /* cd->type and header must be set in context */
1271 r = crypt_check_data_device_size(cd);
1280 int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
1282 struct crypt_dm_active_device dmd;
1285 /* Device context type must be initialised */
1286 if (!cd->type || !crypt_get_uuid(cd))
1289 log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
1291 r = dm_query_device(cd, name, DM_ACTIVE_DEVICE | DM_ACTIVE_CRYPT_CIPHER |
1292 DM_ACTIVE_UUID | DM_ACTIVE_CRYPT_KEYSIZE |
1293 DM_ACTIVE_CRYPT_KEY, &dmd);
1295 log_err(NULL, _("Device %s is not active.\n"), name);
1299 if (!dmd.uuid || dmd.target != DM_CRYPT) {
1304 r = device_block_adjust(cd, dmd.data_device, DEV_OK,
1305 dmd.u.crypt.offset, &new_size, &dmd.flags);
1309 if (new_size == dmd.size) {
1310 log_dbg("Device has already requested size %" PRIu64
1311 " sectors.", dmd.size);
1314 dmd.size = new_size;
1315 if (isTCRYPT(cd->type))
1318 r = dm_create_device(cd, name, cd->type, &dmd, 1);
1321 if (dmd.target == DM_CRYPT) {
1322 crypt_free_volume_key(dmd.u.crypt.vk);
1323 free(CONST_CAST(void*)dmd.u.crypt.cipher);
1325 free(CONST_CAST(void*)dmd.data_device);
1326 free(CONST_CAST(void*)dmd.uuid);
1331 int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
1333 if (!isLUKS(cd->type)) {
1334 log_err(cd, _("This operation is not supported for this device type.\n"));
1338 if (uuid && !strncmp(uuid, cd->u.luks1.hdr.uuid, sizeof(cd->u.luks1.hdr.uuid))) {
1339 log_dbg("UUID is the same as requested (%s) for device %s.",
1340 uuid, mdata_device_path(cd));
1345 log_dbg("Requested new UUID change to %s for %s.", uuid, mdata_device_path(cd));
1347 log_dbg("Requested new UUID refresh for %s.", mdata_device_path(cd));
1349 if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
1352 return LUKS_hdr_uuid_set(&cd->u.luks1.hdr, uuid, cd);
1355 int crypt_header_backup(struct crypt_device *cd,
1356 const char *requested_type,
1357 const char *backup_file)
1361 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1364 r = init_crypto(cd);
1368 log_dbg("Requested header backup of device %s (%s) to "
1369 "file %s.", mdata_device_path(cd), requested_type, backup_file);
1371 return LUKS_hdr_backup(backup_file, &cd->u.luks1.hdr, cd);
1374 int crypt_header_restore(struct crypt_device *cd,
1375 const char *requested_type,
1376 const char *backup_file)
1380 if (requested_type && !isLUKS(requested_type))
1383 if (cd->type && !isLUKS(cd->type))
1386 r = init_crypto(cd);
1390 log_dbg("Requested header restore to device %s (%s) from "
1391 "file %s.", mdata_device_path(cd), requested_type, backup_file);
1393 return LUKS_hdr_restore(backup_file, &cd->u.luks1.hdr, cd);
1396 void crypt_free(struct crypt_device *cd)
1399 log_dbg("Releasing crypt device %s context.", mdata_device_path(cd));
1402 crypt_free_volume_key(cd->volume_key);
1404 device_free(cd->device);
1405 device_free(cd->metadata_device);
1407 if (isPLAIN(cd->type)) {
1408 free(CONST_CAST(void*)cd->u.plain.hdr.hash);
1409 free(cd->u.plain.cipher);
1410 free(cd->u.plain.cipher_mode);
1411 free(cd->u.plain.uuid);
1412 } else if (isLOOPAES(cd->type)) {
1413 free(CONST_CAST(void*)cd->u.loopaes.hdr.hash);
1414 free(cd->u.loopaes.cipher);
1415 free(cd->u.loopaes.uuid);
1416 } else if (isVERITY(cd->type)) {
1417 free(CONST_CAST(void*)cd->u.verity.hdr.hash_name);
1418 free(CONST_CAST(void*)cd->u.verity.hdr.salt);
1419 free(cd->u.verity.root_hash);
1420 free(cd->u.verity.uuid);
1428 int crypt_suspend(struct crypt_device *cd,
1431 crypt_status_info ci;
1434 log_dbg("Suspending volume %s.", name);
1436 if (!cd || !isLUKS(cd->type)) {
1437 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1442 ci = crypt_status(NULL, name);
1443 if (ci < CRYPT_ACTIVE) {
1444 log_err(cd, _("Volume %s is not active.\n"), name);
1450 r = dm_status_suspended(cd, name);
1455 log_err(cd, _("Volume %s is already suspended.\n"), name);
1460 r = dm_suspend_and_wipe_key(cd, name);
1462 log_err(cd, "Suspend is not supported for device %s.\n", name);
1464 log_err(cd, "Error during suspending device %s.\n", name);
1470 int crypt_resume_by_passphrase(struct crypt_device *cd,
1473 const char *passphrase,
1474 size_t passphrase_size)
1476 struct volume_key *vk = NULL;
1479 log_dbg("Resuming volume %s.", name);
1481 if (!isLUKS(cd->type)) {
1482 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1487 r = dm_status_suspended(cd, name);
1492 log_err(cd, _("Volume %s is not suspended.\n"), name);
1497 r = LUKS_open_key_with_hdr(keyslot, passphrase, passphrase_size,
1498 &cd->u.luks1.hdr, &vk, cd);
1500 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1504 r = dm_resume_and_reinstate_key(cd, name, vk->keylength, vk->key);
1506 log_err(cd, "Resume is not supported for device %s.\n", name);
1508 log_err(cd, "Error during resuming device %s.\n", name);
1512 crypt_free_volume_key(vk);
1513 return r < 0 ? r : keyslot;
1516 int crypt_resume_by_keyfile_offset(struct crypt_device *cd,
1519 const char *keyfile,
1520 size_t keyfile_size,
1521 size_t keyfile_offset)
1523 struct volume_key *vk = NULL;
1524 char *passphrase_read = NULL;
1525 size_t passphrase_size_read;
1528 log_dbg("Resuming volume %s.", name);
1530 if (!isLUKS(cd->type)) {
1531 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1536 r = dm_status_suspended(cd, name);
1541 log_err(cd, _("Volume %s is not suspended.\n"), name);
1548 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1549 &passphrase_size_read, keyfile, keyfile_offset,
1554 r = LUKS_open_key_with_hdr(keyslot, passphrase_read,
1555 passphrase_size_read, &cd->u.luks1.hdr, &vk, cd);
1560 r = dm_resume_and_reinstate_key(cd, name, vk->keylength, vk->key);
1562 log_err(cd, "Error during resuming device %s.\n", name);
1564 crypt_safe_free(passphrase_read);
1565 crypt_free_volume_key(vk);
1566 return r < 0 ? r : keyslot;
1569 int crypt_resume_by_keyfile(struct crypt_device *cd,
1572 const char *keyfile,
1573 size_t keyfile_size)
1575 return crypt_resume_by_keyfile_offset(cd, name, keyslot,
1576 keyfile, keyfile_size, 0);
1579 // slot manipulation
1580 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1581 int keyslot, // -1 any
1582 const char *passphrase, // NULL -> terminal
1583 size_t passphrase_size,
1584 const char *new_passphrase, // NULL -> terminal
1585 size_t new_passphrase_size)
1587 struct volume_key *vk = NULL;
1588 char *password = NULL, *new_password = NULL;
1589 size_t passwordLen, new_passwordLen;
1592 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1593 "new passphrase %sprovided.",
1594 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1596 if (!isLUKS(cd->type)) {
1597 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1601 r = keyslot_verify_or_find_empty(cd, &keyslot);
1605 if (!LUKS_keyslot_active_count(&cd->u.luks1.hdr)) {
1606 /* No slots used, try to use pre-generated key in header */
1607 if (cd->volume_key) {
1608 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1609 r = vk ? 0 : -ENOMEM;
1611 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1614 } else if (passphrase) {
1615 /* Passphrase provided, use it to unlock existing keyslot */
1616 r = LUKS_open_key_with_hdr(CRYPT_ANY_SLOT, passphrase,
1617 passphrase_size, &cd->u.luks1.hdr, &vk, cd);
1619 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1620 r = key_from_terminal(cd, _("Enter any passphrase: "),
1621 &password, &passwordLen, 0);
1625 r = LUKS_open_key_with_hdr(CRYPT_ANY_SLOT, password,
1626 passwordLen, &cd->u.luks1.hdr, &vk, cd);
1627 crypt_safe_free(password);
1633 if (new_passphrase) {
1634 new_password = CONST_CAST(char*)new_passphrase;
1635 new_passwordLen = new_passphrase_size;
1637 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1638 &new_password, &new_passwordLen, 1);
1643 r = LUKS_set_key(keyslot, new_password, new_passwordLen,
1644 &cd->u.luks1.hdr, vk, cd->iteration_time, &cd->u.luks1.PBKDF2_per_sec, cd);
1649 if (!new_passphrase)
1650 crypt_safe_free(new_password);
1651 crypt_free_volume_key(vk);
1652 return r ?: keyslot;
1655 int crypt_keyslot_change_by_passphrase(struct crypt_device *cd,
1658 const char *passphrase,
1659 size_t passphrase_size,
1660 const char *new_passphrase,
1661 size_t new_passphrase_size)
1663 struct volume_key *vk = NULL;
1666 log_dbg("Changing passphrase from old keyslot %d to new %d.",
1667 keyslot_old, keyslot_new);
1669 if (!isLUKS(cd->type)) {
1670 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1674 r = LUKS_open_key_with_hdr(keyslot_old, passphrase, passphrase_size,
1675 &cd->u.luks1.hdr, &vk, cd);
1679 if (keyslot_old != CRYPT_ANY_SLOT && keyslot_old != r) {
1680 log_dbg("Keyslot mismatch.");
1685 if (keyslot_new == CRYPT_ANY_SLOT) {
1686 keyslot_new = LUKS_keyslot_find_empty(&cd->u.luks1.hdr);
1687 if (keyslot_new < 0)
1688 keyslot_new = keyslot_old;
1691 if (keyslot_old == keyslot_new) {
1692 log_dbg("Key slot %d is going to be overwritten.", keyslot_old);
1693 (void)crypt_keyslot_destroy(cd, keyslot_old);
1696 r = LUKS_set_key(keyslot_new, new_passphrase, new_passphrase_size,
1697 &cd->u.luks1.hdr, vk, cd->iteration_time,
1698 &cd->u.luks1.PBKDF2_per_sec, cd);
1700 if (keyslot_old == keyslot_new) {
1702 log_verbose(cd, _("Key slot %d changed.\n"), r);
1705 log_verbose(cd, _("Replaced with key slot %d.\n"), r);
1706 r = crypt_keyslot_destroy(cd, keyslot_old);
1710 log_err(cd, _("Failed to swap new key slot.\n"));
1712 crypt_free_volume_key(vk);
1713 return r ?: keyslot_new;
1716 int crypt_keyslot_add_by_keyfile_offset(struct crypt_device *cd,
1718 const char *keyfile,
1719 size_t keyfile_size,
1720 size_t keyfile_offset,
1721 const char *new_keyfile,
1722 size_t new_keyfile_size,
1723 size_t new_keyfile_offset)
1725 struct volume_key *vk = NULL;
1726 char *password = NULL; size_t passwordLen;
1727 char *new_password = NULL; size_t new_passwordLen;
1730 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1731 keyfile ?: "[none]", new_keyfile ?: "[none]");
1733 if (!isLUKS(cd->type)) {
1734 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1738 r = keyslot_verify_or_find_empty(cd, &keyslot);
1742 if (!LUKS_keyslot_active_count(&cd->u.luks1.hdr)) {
1743 /* No slots used, try to use pre-generated key in header */
1744 if (cd->volume_key) {
1745 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1746 r = vk ? 0 : -ENOMEM;
1748 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1752 /* Read password from file of (if NULL) from terminal */
1754 r = key_from_file(cd, _("Enter any passphrase: "),
1755 &password, &passwordLen,
1756 keyfile, keyfile_offset, keyfile_size);
1758 r = key_from_terminal(cd, _("Enter any passphrase: "),
1759 &password, &passwordLen, 0);
1763 r = LUKS_open_key_with_hdr(CRYPT_ANY_SLOT, password, passwordLen,
1764 &cd->u.luks1.hdr, &vk, cd);
1771 r = key_from_file(cd, _("Enter new passphrase for key slot: "),
1772 &new_password, &new_passwordLen, new_keyfile,
1773 new_keyfile_offset, new_keyfile_size);
1775 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1776 &new_password, &new_passwordLen, 1);
1780 r = LUKS_set_key(keyslot, new_password, new_passwordLen,
1781 &cd->u.luks1.hdr, vk, cd->iteration_time, &cd->u.luks1.PBKDF2_per_sec, cd);
1783 crypt_safe_free(password);
1784 crypt_safe_free(new_password);
1785 crypt_free_volume_key(vk);
1786 return r < 0 ? r : keyslot;
1789 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1791 const char *keyfile,
1792 size_t keyfile_size,
1793 const char *new_keyfile,
1794 size_t new_keyfile_size)
1796 return crypt_keyslot_add_by_keyfile_offset(cd, keyslot,
1797 keyfile, keyfile_size, 0,
1798 new_keyfile, new_keyfile_size, 0);
1801 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1803 const char *volume_key,
1804 size_t volume_key_size,
1805 const char *passphrase,
1806 size_t passphrase_size)
1808 struct volume_key *vk = NULL;
1810 char *new_password = NULL; size_t new_passwordLen;
1812 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1814 if (!isLUKS(cd->type)) {
1815 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1820 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1821 else if (cd->volume_key)
1822 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1827 r = LUKS_verify_volume_key(&cd->u.luks1.hdr, vk);
1829 log_err(cd, _("Volume key does not match the volume.\n"));
1833 r = keyslot_verify_or_find_empty(cd, &keyslot);
1838 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1839 &new_password, &new_passwordLen, 1);
1842 passphrase = new_password;
1843 passphrase_size = new_passwordLen;
1846 r = LUKS_set_key(keyslot, passphrase, passphrase_size,
1847 &cd->u.luks1.hdr, vk, cd->iteration_time, &cd->u.luks1.PBKDF2_per_sec, cd);
1849 crypt_safe_free(new_password);
1850 crypt_free_volume_key(vk);
1851 return (r < 0) ? r : keyslot;
1854 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1856 crypt_keyslot_info ki;
1858 log_dbg("Destroying keyslot %d.", keyslot);
1860 if (!isLUKS(cd->type)) {
1861 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1865 ki = crypt_keyslot_status(cd, keyslot);
1866 if (ki == CRYPT_SLOT_INVALID) {
1867 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1871 if (ki == CRYPT_SLOT_INACTIVE) {
1872 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1876 return LUKS_del_key(keyslot, &cd->u.luks1.hdr, cd);
1879 // activation/deactivation of device mapping
1880 int crypt_activate_by_passphrase(struct crypt_device *cd,
1883 const char *passphrase,
1884 size_t passphrase_size,
1887 crypt_status_info ci;
1888 struct volume_key *vk = NULL;
1889 char *read_passphrase = NULL;
1890 size_t passphraseLen = 0;
1893 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1894 name ? "Activating" : "Checking", name ?: "",
1895 keyslot, passphrase ? "" : "[none] ");
1898 ci = crypt_status(NULL, name);
1899 if (ci == CRYPT_INVALID)
1901 else if (ci >= CRYPT_ACTIVE) {
1902 log_err(cd, _("Device %s already exists.\n"), name);
1907 /* plain, use hashed passphrase */
1908 if (isPLAIN(cd->type)) {
1913 r = key_from_terminal(cd, NULL, &read_passphrase,
1917 passphrase = read_passphrase;
1918 passphrase_size = passphraseLen;
1921 r = process_key(cd, cd->u.plain.hdr.hash,
1922 cd->u.plain.key_size,
1923 passphrase, passphrase_size, &vk);
1927 r = PLAIN_activate(cd, name, vk, cd->u.plain.hdr.size, flags);
1929 } else if (isLUKS(cd->type)) {
1930 /* provided passphrase, do not retry */
1932 r = LUKS_open_key_with_hdr(keyslot, passphrase,
1933 passphrase_size, &cd->u.luks1.hdr, &vk, cd);
1935 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1940 r = LUKS1_activate(cd, name, vk, flags);
1945 crypt_safe_free(read_passphrase);
1946 crypt_free_volume_key(vk);
1948 return r < 0 ? r : keyslot;
1951 int crypt_activate_by_keyfile_offset(struct crypt_device *cd,
1954 const char *keyfile,
1955 size_t keyfile_size,
1956 size_t keyfile_offset,
1959 crypt_status_info ci;
1960 struct volume_key *vk = NULL;
1961 char *passphrase_read = NULL;
1962 size_t passphrase_size_read;
1963 unsigned int key_count = 0;
1966 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1967 name ?: "", keyslot, keyfile ?: "[none]");
1970 ci = crypt_status(NULL, name);
1971 if (ci == CRYPT_INVALID)
1973 else if (ci >= CRYPT_ACTIVE) {
1974 log_err(cd, _("Device %s already exists.\n"), name);
1982 if (isPLAIN(cd->type)) {
1986 r = key_from_file(cd, _("Enter passphrase: "),
1987 &passphrase_read, &passphrase_size_read,
1988 keyfile, keyfile_offset, keyfile_size);
1992 r = process_key(cd, cd->u.plain.hdr.hash,
1993 cd->u.plain.key_size,
1994 passphrase_read, passphrase_size_read, &vk);
1998 r = PLAIN_activate(cd, name, vk, cd->u.plain.hdr.size, flags);
1999 } else if (isLUKS(cd->type)) {
2000 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
2001 &passphrase_size_read, keyfile, keyfile_offset, keyfile_size);
2004 r = LUKS_open_key_with_hdr(keyslot, passphrase_read,
2005 passphrase_size_read, &cd->u.luks1.hdr, &vk, cd);
2011 r = LUKS1_activate(cd, name, vk, flags);
2016 } else if (isLOOPAES(cd->type)) {
2017 r = key_from_file(cd, NULL, &passphrase_read, &passphrase_size_read,
2018 keyfile, keyfile_offset, keyfile_size);
2021 r = LOOPAES_parse_keyfile(cd, &vk, cd->u.loopaes.hdr.hash, &key_count,
2022 passphrase_read, passphrase_size_read);
2026 r = LOOPAES_activate(cd, name, cd->u.loopaes.cipher,
2027 key_count, vk, flags);
2032 crypt_safe_free(passphrase_read);
2033 crypt_free_volume_key(vk);
2038 int crypt_activate_by_keyfile(struct crypt_device *cd,
2041 const char *keyfile,
2042 size_t keyfile_size,
2045 return crypt_activate_by_keyfile_offset(cd, name, keyslot, keyfile,
2046 keyfile_size, 0, flags);
2049 int crypt_activate_by_volume_key(struct crypt_device *cd,
2051 const char *volume_key,
2052 size_t volume_key_size,
2055 crypt_status_info ci;
2056 struct volume_key *vk = NULL;
2059 log_dbg("Activating volume %s by volume key.", name ?: "[none]");
2062 ci = crypt_status(NULL, name);
2063 if (ci == CRYPT_INVALID)
2065 else if (ci >= CRYPT_ACTIVE) {
2066 log_err(cd, _("Device %s already exists.\n"), name);
2071 /* use key directly, no hash */
2072 if (isPLAIN(cd->type)) {
2076 if (!volume_key || !volume_key_size || volume_key_size != cd->u.plain.key_size) {
2077 log_err(cd, _("Incorrect volume key specified for plain device.\n"));
2081 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2085 r = PLAIN_activate(cd, name, vk, cd->u.plain.hdr.size, flags);
2086 } else if (isLUKS(cd->type)) {
2087 /* If key is not provided, try to use internal key */
2089 if (!cd->volume_key) {
2090 log_err(cd, _("Volume key does not match the volume.\n"));
2093 volume_key_size = cd->volume_key->keylength;
2094 volume_key = cd->volume_key->key;
2097 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2100 r = LUKS_verify_volume_key(&cd->u.luks1.hdr, vk);
2103 log_err(cd, _("Volume key does not match the volume.\n"));
2106 r = LUKS1_activate(cd, name, vk, flags);
2107 } else if (isVERITY(cd->type)) {
2108 /* volume_key == root hash */
2109 if (!volume_key || !volume_key_size) {
2110 log_err(cd, _("Incorrect root hash specified for verity device.\n"));
2114 r = VERITY_activate(cd, name, volume_key, volume_key_size,
2115 &cd->u.verity.hdr, CRYPT_ACTIVATE_READONLY);
2118 free(cd->u.verity.root_hash);
2119 cd->u.verity.root_hash = NULL;
2121 cd->u.verity.root_hash_size = volume_key_size;
2122 if (!cd->u.verity.root_hash)
2123 cd->u.verity.root_hash = malloc(volume_key_size);
2124 if (cd->u.verity.root_hash)
2125 memcpy(cd->u.verity.root_hash, volume_key, volume_key_size);
2127 } else if (isTCRYPT(cd->type)) {
2130 r = TCRYPT_activate(cd, name, &cd->u.tcrypt.hdr,
2131 &cd->u.tcrypt.params, flags);
2133 log_err(cd, _("Device type is not properly initialised.\n"));
2135 crypt_free_volume_key(vk);
2140 int crypt_deactivate(struct crypt_device *cd, const char *name)
2147 log_dbg("Deactivating volume %s.", name);
2152 switch (crypt_status(cd, name)) {
2155 if (cd && isTCRYPT(cd->type))
2156 r = TCRYPT_deactivate(cd, name);
2158 r = dm_remove_device(cd, name, 0, 0);
2160 case CRYPT_INACTIVE:
2161 log_err(cd, _("Device %s is not active.\n"), name);
2165 log_err(cd, _("Invalid device %s.\n"), name);
2175 int crypt_volume_key_get(struct crypt_device *cd,
2178 size_t *volume_key_size,
2179 const char *passphrase,
2180 size_t passphrase_size)
2182 struct volume_key *vk = NULL;
2186 if (crypt_fips_mode()) {
2187 log_err(cd, "Function not available in FIPS mode.\n");
2191 key_len = crypt_get_volume_key_size(cd);
2192 if (key_len > *volume_key_size) {
2193 log_err(cd, _("Volume key buffer too small.\n"));
2197 if (isPLAIN(cd->type) && cd->u.plain.hdr.hash) {
2198 r = process_key(cd, cd->u.plain.hdr.hash, key_len,
2199 passphrase, passphrase_size, &vk);
2201 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
2202 } else if (isLUKS(cd->type)) {
2203 r = LUKS_open_key_with_hdr(keyslot, passphrase,
2204 passphrase_size, &cd->u.luks1.hdr, &vk, cd);
2205 } else if (isTCRYPT(cd->type)) {
2206 r = TCRYPT_get_volume_key(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params, &vk);
2208 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
2211 memcpy(volume_key, vk->key, vk->keylength);
2212 *volume_key_size = vk->keylength;
2215 crypt_free_volume_key(vk);
2219 int crypt_volume_key_verify(struct crypt_device *cd,
2220 const char *volume_key,
2221 size_t volume_key_size)
2223 struct volume_key *vk;
2226 if (!isLUKS(cd->type)) {
2227 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2231 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2235 r = LUKS_verify_volume_key(&cd->u.luks1.hdr, vk);
2238 log_err(cd, _("Volume key does not match the volume.\n"));
2240 crypt_free_volume_key(vk);
2245 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
2247 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
2248 cd->timeout = timeout_sec;
2251 void crypt_set_password_retry(struct crypt_device *cd, int tries)
2253 log_dbg("Password retry count set to %d.", tries);
2257 void crypt_set_iteration_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2259 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
2260 cd->iteration_time = iteration_time_ms;
2262 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2264 crypt_set_iteration_time(cd, iteration_time_ms);
2267 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
2269 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
2270 cd->password_verify = password_verify ? 1 : 0;
2273 void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
2276 case CRYPT_RNG_URANDOM:
2277 case CRYPT_RNG_RANDOM:
2278 log_dbg("RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
2279 cd->rng_type = rng_type;
2283 int crypt_get_rng_type(struct crypt_device *cd)
2288 return cd->rng_type;
2291 int crypt_memory_lock(struct crypt_device *cd, int lock)
2293 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
2297 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
2304 r = dm_status_device(cd, name);
2309 if (r < 0 && r != -ENODEV)
2310 return CRYPT_INVALID;
2313 return CRYPT_ACTIVE;
2318 return CRYPT_INACTIVE;
2321 static void hexprint(struct crypt_device *cd, const char *d, int n, const char *sep)
2324 for(i = 0; i < n; i++)
2325 log_std(cd, "%02hhx%s", (const char)d[i], sep);
2328 static int _luks_dump(struct crypt_device *cd)
2332 log_std(cd, "LUKS header information for %s\n\n", mdata_device_path(cd));
2333 log_std(cd, "Version: \t%d\n", cd->u.luks1.hdr.version);
2334 log_std(cd, "Cipher name: \t%s\n", cd->u.luks1.hdr.cipherName);
2335 log_std(cd, "Cipher mode: \t%s\n", cd->u.luks1.hdr.cipherMode);
2336 log_std(cd, "Hash spec: \t%s\n", cd->u.luks1.hdr.hashSpec);
2337 log_std(cd, "Payload offset:\t%d\n", cd->u.luks1.hdr.payloadOffset);
2338 log_std(cd, "MK bits: \t%d\n", cd->u.luks1.hdr.keyBytes * 8);
2339 log_std(cd, "MK digest: \t");
2340 hexprint(cd, cd->u.luks1.hdr.mkDigest, LUKS_DIGESTSIZE, " ");
2342 log_std(cd, "MK salt: \t");
2343 hexprint(cd, cd->u.luks1.hdr.mkDigestSalt, LUKS_SALTSIZE/2, " ");
2344 log_std(cd, "\n \t");
2345 hexprint(cd, cd->u.luks1.hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ");
2347 log_std(cd, "MK iterations: \t%d\n", cd->u.luks1.hdr.mkDigestIterations);
2348 log_std(cd, "UUID: \t%s\n\n", cd->u.luks1.hdr.uuid);
2349 for(i = 0; i < LUKS_NUMKEYS; i++) {
2350 if(cd->u.luks1.hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
2351 log_std(cd, "Key Slot %d: ENABLED\n",i);
2352 log_std(cd, "\tIterations: \t%d\n",
2353 cd->u.luks1.hdr.keyblock[i].passwordIterations);
2354 log_std(cd, "\tSalt: \t");
2355 hexprint(cd, cd->u.luks1.hdr.keyblock[i].passwordSalt,
2356 LUKS_SALTSIZE/2, " ");
2357 log_std(cd, "\n\t \t");
2358 hexprint(cd, cd->u.luks1.hdr.keyblock[i].passwordSalt +
2359 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ");
2362 log_std(cd, "\tKey material offset:\t%d\n",
2363 cd->u.luks1.hdr.keyblock[i].keyMaterialOffset);
2364 log_std(cd, "\tAF stripes: \t%d\n",
2365 cd->u.luks1.hdr.keyblock[i].stripes);
2368 log_std(cd, "Key Slot %d: DISABLED\n", i);
2373 static int _verity_dump(struct crypt_device *cd)
2375 log_std(cd, "VERITY header information for %s\n", mdata_device_path(cd));
2376 log_std(cd, "UUID: \t%s\n", cd->u.verity.uuid ?: "");
2377 log_std(cd, "Hash type: \t%u\n", cd->u.verity.hdr.hash_type);
2378 log_std(cd, "Data blocks: \t%" PRIu64 "\n", cd->u.verity.hdr.data_size);
2379 log_std(cd, "Data block size: \t%u\n", cd->u.verity.hdr.data_block_size);
2380 log_std(cd, "Hash block size: \t%u\n", cd->u.verity.hdr.hash_block_size);
2381 log_std(cd, "Hash algorithm: \t%s\n", cd->u.verity.hdr.hash_name);
2382 log_std(cd, "Salt: \t");
2383 if (cd->u.verity.hdr.salt_size)
2384 hexprint(cd, cd->u.verity.hdr.salt, cd->u.verity.hdr.salt_size, "");
2388 if (cd->u.verity.root_hash) {
2389 log_std(cd, "Root hash: \t");
2390 hexprint(cd, cd->u.verity.root_hash, cd->u.verity.root_hash_size, "");
2396 int crypt_dump(struct crypt_device *cd)
2398 if (isLUKS(cd->type))
2399 return _luks_dump(cd);
2400 else if (isVERITY(cd->type))
2401 return _verity_dump(cd);
2402 else if (isTCRYPT(cd->type))
2403 return TCRYPT_dump(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
2405 log_err(cd, _("Dump operation is not supported for this device type.\n"));
2409 const char *crypt_get_cipher(struct crypt_device *cd)
2411 if (isPLAIN(cd->type))
2412 return cd->u.plain.cipher;
2414 if (isLUKS(cd->type))
2415 return cd->u.luks1.hdr.cipherName;
2417 if (isLOOPAES(cd->type))
2418 return cd->u.loopaes.cipher;
2420 if (isTCRYPT(cd->type))
2421 return cd->u.tcrypt.params.cipher;
2426 const char *crypt_get_cipher_mode(struct crypt_device *cd)
2428 if (isPLAIN(cd->type))
2429 return cd->u.plain.cipher_mode;
2431 if (isLUKS(cd->type))
2432 return cd->u.luks1.hdr.cipherMode;
2434 if (isLOOPAES(cd->type))
2435 return cd->u.loopaes.cipher_mode;
2437 if (isTCRYPT(cd->type))
2438 return cd->u.tcrypt.params.mode;
2443 const char *crypt_get_uuid(struct crypt_device *cd)
2445 if (isLUKS(cd->type))
2446 return cd->u.luks1.hdr.uuid;
2448 if (isPLAIN(cd->type))
2449 return cd->u.plain.uuid;
2451 if (isLOOPAES(cd->type))
2452 return cd->u.loopaes.uuid;
2454 if (isVERITY(cd->type))
2455 return cd->u.verity.uuid;
2460 const char *crypt_get_device_name(struct crypt_device *cd)
2462 const char *path = device_block_path(cd->device);
2465 path = device_path(cd->device);
2470 int crypt_get_volume_key_size(struct crypt_device *cd)
2472 if (isPLAIN(cd->type))
2473 return cd->u.plain.key_size;
2475 if (isLUKS(cd->type))
2476 return cd->u.luks1.hdr.keyBytes;
2478 if (isLOOPAES(cd->type))
2479 return cd->u.loopaes.key_size;
2481 if (isVERITY(cd->type))
2482 return cd->u.verity.root_hash_size;
2484 if (isTCRYPT(cd->type))
2485 return cd->u.tcrypt.params.key_size;
2490 uint64_t crypt_get_data_offset(struct crypt_device *cd)
2492 if (isPLAIN(cd->type))
2493 return cd->u.plain.hdr.offset;
2495 if (isLUKS(cd->type))
2496 return cd->u.luks1.hdr.payloadOffset;
2498 if (isLOOPAES(cd->type))
2499 return cd->u.loopaes.hdr.offset;
2501 if (isTCRYPT(cd->type))
2502 return TCRYPT_get_data_offset(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
2507 uint64_t crypt_get_iv_offset(struct crypt_device *cd)
2509 if (isPLAIN(cd->type))
2510 return cd->u.plain.hdr.skip;
2512 if (isLUKS(cd->type))
2515 if (isLOOPAES(cd->type))
2516 return cd->u.loopaes.hdr.skip;
2518 if (isTCRYPT(cd->type))
2519 return TCRYPT_get_iv_offset(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
2524 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
2526 if (!isLUKS(cd->type)) {
2527 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2528 return CRYPT_SLOT_INVALID;
2531 return LUKS_keyslot_info(&cd->u.luks1.hdr, keyslot);
2534 int crypt_keyslot_max(const char *type)
2536 if (type && isLUKS(type))
2537 return LUKS_NUMKEYS;
2542 int crypt_keyslot_area(struct crypt_device *cd,
2547 if (!isLUKS(cd->type))
2550 return LUKS_keyslot_area(&cd->u.luks1.hdr, keyslot, offset, length);
2553 const char *crypt_get_type(struct crypt_device *cd)
2558 int crypt_get_verity_info(struct crypt_device *cd,
2559 struct crypt_params_verity *vp)
2561 if (!isVERITY(cd->type) || !vp)
2564 vp->data_device = device_path(cd->device);
2565 vp->hash_device = mdata_device_path(cd);
2566 vp->hash_name = cd->u.verity.hdr.hash_name;
2567 vp->salt = cd->u.verity.hdr.salt;
2568 vp->salt_size = cd->u.verity.hdr.salt_size;
2569 vp->data_block_size = cd->u.verity.hdr.data_block_size;
2570 vp->hash_block_size = cd->u.verity.hdr.hash_block_size;
2571 vp->data_size = cd->u.verity.hdr.data_size;
2572 vp->hash_area_offset = cd->u.verity.hdr.hash_area_offset;
2573 vp->hash_type = cd->u.verity.hdr.hash_type;
2574 vp->flags = cd->u.verity.hdr.flags & CRYPT_VERITY_NO_HEADER;
2578 int crypt_get_active_device(struct crypt_device *cd, const char *name,
2579 struct crypt_active_device *cad)
2581 struct crypt_dm_active_device dmd;
2584 r = dm_query_device(cd, name, 0, &dmd);
2588 if (dmd.target != DM_CRYPT && dmd.target != DM_VERITY)
2591 if (cd && isTCRYPT(cd->type)) {
2592 cad->offset = TCRYPT_get_data_offset(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
2593 cad->iv_offset = TCRYPT_get_iv_offset(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
2595 cad->offset = dmd.u.crypt.offset;
2596 cad->iv_offset = dmd.u.crypt.iv_offset;
2598 cad->size = dmd.size;
2599 cad->flags = dmd.flags;