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;
64 unsigned int key_size;
66 struct { /* used in CRYPT_LOOPAES */
67 struct crypt_params_loopaes hdr;
71 unsigned int key_size;
73 struct { /* used in CRYPT_VERITY */
74 struct crypt_params_verity hdr;
76 unsigned int root_hash_size;
79 struct { /* used in CRYPT_TCRYPT */
80 struct crypt_params_tcrypt params;
81 struct tcrypt_phdr hdr;
85 /* callbacks definitions */
86 void (*log)(int level, const char *msg, void *usrptr);
88 int (*confirm)(const char *msg, void *usrptr);
90 int (*password)(const char *msg, char *buf, size_t length, void *usrptr);
91 void *password_usrptr;
93 /* last error message */
94 char error[MAX_ERROR_LENGTH];
98 /* FIXME: not thread safe, remove this later */
99 static char global_error[MAX_ERROR_LENGTH] = {0};
102 static void (*_default_log)(int level, const char *msg, void *usrptr) = NULL;
103 static int _debug_level = 0;
105 void crypt_set_debug_level(int level)
107 _debug_level = level;
110 int crypt_get_debug_level(void)
115 static void crypt_set_error(struct crypt_device *cd, const char *error)
117 size_t size = strlen(error);
119 /* Set global error, ugly hack... */
120 strncpy(global_error, error, MAX_ERROR_LENGTH - 2);
121 if (size < MAX_ERROR_LENGTH && global_error[size - 1] == '\n')
122 global_error[size - 1] = '\0';
124 /* Set error string per context */
126 strncpy(cd->error, error, MAX_ERROR_LENGTH - 2);
127 if (size < MAX_ERROR_LENGTH && cd->error[size - 1] == '\n')
128 cd->error[size - 1] = '\0';
132 void crypt_log(struct crypt_device *cd, int level, const char *msg)
135 cd->log(level, msg, cd->log_usrptr);
136 else if (_default_log)
137 _default_log(level, msg, NULL);
139 if (level == CRYPT_LOG_ERROR)
140 crypt_set_error(cd, msg);
143 __attribute__((format(printf, 5, 6)))
144 void logger(struct crypt_device *cd, int level, const char *file,
145 int line, const char *format, ...)
150 va_start(argp, format);
152 if (vasprintf(&target, format, argp) > 0 ) {
154 crypt_log(cd, level, target);
156 } else if (_debug_level)
157 printf("# %s:%d %s\n", file ?: "?", line, target);
159 } else if (_debug_level)
160 printf("# %s\n", target);
168 static const char *mdata_device_path(struct crypt_device *cd)
170 return device_path(cd->metadata_device ?: cd->device);
174 struct device *crypt_metadata_device(struct crypt_device *cd)
176 return cd->metadata_device ?: cd->device;
179 struct device *crypt_data_device(struct crypt_device *cd)
184 int init_crypto(struct crypt_device *ctx)
188 crypt_fips_libcryptsetup_check(ctx);
190 r = crypt_random_init(ctx);
192 log_err(ctx, _("Cannot initialize crypto RNG backend.\n"));
196 r = crypt_backend_init(ctx);
198 log_err(ctx, _("Cannot initialize crypto backend.\n"));
200 log_dbg("Crypto backend (%s) initialized.", crypt_backend_version());
204 static int process_key(struct crypt_device *cd, const char *hash_name,
205 size_t key_size, const char *pass, size_t passLen,
206 struct volume_key **vk)
213 *vk = crypt_alloc_volume_key(key_size, NULL);
218 r = crypt_plain_hash(cd, hash_name, (*vk)->key, key_size, pass, passLen);
221 log_err(cd, _("Hash algorithm %s not supported.\n"),
224 log_err(cd, _("Key processing error (using hash %s).\n"),
226 crypt_free_volume_key(*vk);
230 } else if (passLen > key_size) {
231 memcpy((*vk)->key, pass, key_size);
233 memcpy((*vk)->key, pass, passLen);
239 static int isPLAIN(const char *type)
241 return (type && !strcmp(CRYPT_PLAIN, type));
244 static int isLUKS(const char *type)
246 return (type && !strcmp(CRYPT_LUKS1, type));
249 static int isLOOPAES(const char *type)
251 return (type && !strcmp(CRYPT_LOOPAES, type));
254 static int isVERITY(const char *type)
256 return (type && !strcmp(CRYPT_VERITY, type));
259 static int isTCRYPT(const char *type)
261 return (type && !strcmp(CRYPT_TCRYPT, type));
264 /* keyslot helpers */
265 static int keyslot_verify_or_find_empty(struct crypt_device *cd, int *keyslot)
267 if (*keyslot == CRYPT_ANY_SLOT) {
268 *keyslot = LUKS_keyslot_find_empty(&cd->u.luks1.hdr);
270 log_err(cd, _("All key slots full.\n"));
275 switch (LUKS_keyslot_info(&cd->u.luks1.hdr, *keyslot)) {
276 case CRYPT_SLOT_INVALID:
277 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
278 *keyslot, LUKS_NUMKEYS - 1);
280 case CRYPT_SLOT_INACTIVE:
283 log_err(cd, _("Key slot %d is full, please select another one.\n"),
292 * compares UUIDs returned by device-mapper (striped by cryptsetup) and uuid in header
294 static int crypt_uuid_cmp(const char *dm_uuid, const char *hdr_uuid)
299 if (!dm_uuid || !hdr_uuid)
302 str = strchr(dm_uuid, '-');
306 for (i = 0, j = 1; hdr_uuid[i]; i++) {
307 if (hdr_uuid[i] == '-')
310 if (!str[j] || str[j] == '-')
313 if (str[j] != hdr_uuid[i])
321 int PLAIN_activate(struct crypt_device *cd,
323 struct volume_key *vk,
328 char *dm_cipher = NULL;
329 enum devcheck device_check;
330 struct crypt_dm_active_device dmd = {
332 .uuid = crypt_get_uuid(cd),
335 .data_device = crypt_data_device(cd),
339 .offset = crypt_get_data_offset(cd),
340 .iv_offset = crypt_get_iv_offset(cd),
344 if (dmd.flags & CRYPT_ACTIVATE_SHARED)
345 device_check = DEV_SHARED;
347 device_check = DEV_EXCL;
349 r = device_block_adjust(cd, dmd.data_device, device_check,
350 dmd.u.crypt.offset, &dmd.size, &dmd.flags);
354 if (crypt_get_cipher_mode(cd))
355 r = asprintf(&dm_cipher, "%s-%s", crypt_get_cipher(cd), crypt_get_cipher_mode(cd));
357 r = asprintf(&dm_cipher, "%s", crypt_get_cipher(cd));
361 dmd.u.crypt.cipher = dm_cipher;
362 log_dbg("Trying to activate PLAIN device %s using cipher %s.",
363 name, dmd.u.crypt.cipher);
365 r = dm_create_device(cd, name, CRYPT_PLAIN, &dmd, 0);
368 if (!cd->u.plain.uuid && dm_query_device(cd, name, DM_ACTIVE_UUID, &dmd) >= 0)
369 cd->u.plain.uuid = CONST_CAST(char*)dmd.uuid;
375 int crypt_confirm(struct crypt_device *cd, const char *msg)
377 if (!cd || !cd->confirm)
380 return cd->confirm(msg, cd->confirm_usrptr);
383 static int key_from_terminal(struct crypt_device *cd, char *msg, char **key,
384 size_t *key_len, int force_verify)
386 char *prompt = NULL, *device_name;
391 if (crypt_loop_device(crypt_get_device_name(cd)))
392 device_name = crypt_loop_backing_file(crypt_get_device_name(cd));
394 device_name = strdup(crypt_get_device_name(cd));
397 r = asprintf(&prompt, _("Enter passphrase for %s: "), device_name);
405 *key = crypt_safe_alloc(DEFAULT_PASSPHRASE_SIZE_MAX);
410 r = cd->password(msg, *key, DEFAULT_PASSPHRASE_SIZE_MAX,
411 cd->password_usrptr);
413 crypt_safe_free(*key);
418 r = crypt_get_key(msg, key, key_len, 0, 0, NULL, cd->timeout,
419 (force_verify || cd->password_verify), cd);
422 return (r < 0) ? r: 0;
425 static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslot,
426 struct volume_key **vk)
428 char *passphrase_read = NULL;
429 size_t passphrase_size_read;
430 int r = -EINVAL, eperm = 0, tries = cd->tries;
434 crypt_free_volume_key(*vk);
437 r = key_from_terminal(cd, NULL, &passphrase_read,
438 &passphrase_size_read, 0);
439 /* Continue if it is just passphrase verify mismatch */
445 r = LUKS_open_key_with_hdr(keyslot, passphrase_read,
446 passphrase_size_read, &cd->u.luks1.hdr, vk, cd);
449 crypt_safe_free(passphrase_read);
450 passphrase_read = NULL;
451 } while (r == -EPERM && (--tries > 0));
454 crypt_free_volume_key(*vk);
457 /* Report wrong passphrase if at least one try failed */
458 if (eperm && r == -EPIPE)
462 crypt_safe_free(passphrase_read);
466 static int key_from_file(struct crypt_device *cd, char *msg,
467 char **key, size_t *key_len,
468 const char *key_file, size_t key_offset,
471 return crypt_get_key(msg, key, key_len, key_offset, key_size, key_file,
475 void crypt_set_log_callback(struct crypt_device *cd,
476 void (*log)(int level, const char *msg, void *usrptr),
483 cd->log_usrptr = usrptr;
487 void crypt_set_confirm_callback(struct crypt_device *cd,
488 int (*confirm)(const char *msg, void *usrptr),
491 cd->confirm = confirm;
492 cd->confirm_usrptr = usrptr;
495 void crypt_set_password_callback(struct crypt_device *cd,
496 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
499 cd->password = password;
500 cd->password_usrptr = usrptr;
503 static void _get_error(char *error, char *buf, size_t size)
505 if (!buf || size < 1)
508 strncpy(buf, error, size - 1);
509 buf[size - 1] = '\0';
515 void crypt_last_error(struct crypt_device *cd, char *buf, size_t size)
518 return _get_error(cd->error, buf, size);
521 /* Deprecated global error interface */
522 void crypt_get_error(char *buf, size_t size)
524 return _get_error(global_error, buf, size);
527 const char *crypt_get_dir(void)
532 int crypt_init(struct crypt_device **cd, const char *device)
534 struct crypt_device *h = NULL;
540 log_dbg("Allocating crypt device %s context.", device);
542 if (!(h = malloc(sizeof(struct crypt_device))))
545 memset(h, 0, sizeof(*h));
547 r = device_alloc(&h->device, device);
553 h->iteration_time = 1000;
554 h->password_verify = 0;
556 h->rng_type = crypt_random_default_key_rng();
560 device_free(h->device);
565 static int crypt_check_data_device_size(struct crypt_device *cd)
568 uint64_t size, size_min;
570 /* Check data device size, require at least one sector */
571 size_min = crypt_get_data_offset(cd) << SECTOR_SHIFT ?: SECTOR_SIZE;
573 r = device_size(cd->device, &size);
577 if (size < size_min) {
578 log_err(cd, _("Header detected but device %s is too small.\n"),
579 device_path(cd->device));
586 int crypt_set_data_device(struct crypt_device *cd, const char *device)
588 struct device *dev = NULL;
591 log_dbg("Setting ciphertext data device to %s.", device ?: "(none)");
593 if (!isLUKS(cd->type) && !isVERITY(cd->type)) {
594 log_err(cd, _("This operation is not supported for this device type.\n"));
598 /* metadata device must be set */
599 if (!cd->device || !device)
602 r = device_alloc(&dev, device);
606 if (!cd->metadata_device) {
607 cd->metadata_device = cd->device;
609 device_free(cd->device);
613 return crypt_check_data_device_size(cd);
616 static int _crypt_load_luks1(struct crypt_device *cd, int require_header, int repair)
618 struct luks_phdr hdr;
625 r = LUKS_read_phdr(&hdr, require_header, repair, cd);
629 if (!cd->type && !(cd->type = strdup(CRYPT_LUKS1)))
632 memcpy(&cd->u.luks1.hdr, &hdr, sizeof(hdr));
637 static int _crypt_load_tcrypt(struct crypt_device *cd, struct crypt_params_tcrypt *params)
648 memcpy(&cd->u.tcrypt.params, params, sizeof(*params));
650 r = TCRYPT_read_phdr(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
652 cd->u.tcrypt.params.passphrase = NULL;
653 cd->u.tcrypt.params.passphrase_size = 0;
654 cd->u.tcrypt.params.keyfiles = NULL;
655 cd->u.tcrypt.params.keyfiles_count = 0;
660 if (!cd->type && !(cd->type = strdup(CRYPT_TCRYPT)))
666 static int _crypt_load_verity(struct crypt_device *cd, struct crypt_params_verity *params)
669 size_t sb_offset = 0;
675 if (params && params->flags & CRYPT_VERITY_NO_HEADER)
679 sb_offset = params->hash_area_offset;
681 r = VERITY_read_sb(cd, sb_offset, &cd->u.verity.uuid, &cd->u.verity.hdr);
686 cd->u.verity.hdr.flags = params->flags;
688 /* Hash availability checked in sb load */
689 cd->u.verity.root_hash_size = crypt_hash_size(cd->u.verity.hdr.hash_name);
690 if (cd->u.verity.root_hash_size > 4096)
693 if (!cd->type && !(cd->type = strdup(CRYPT_VERITY)))
696 if (params && params->data_device &&
697 (r = crypt_set_data_device(cd, params->data_device)) < 0)
703 static int _init_by_name_crypt(struct crypt_device *cd, const char *name)
705 struct crypt_dm_active_device dmd = {};
706 char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
709 r = dm_query_device(cd, name,
712 DM_ACTIVE_CRYPT_CIPHER |
713 DM_ACTIVE_CRYPT_KEYSIZE, &dmd);
717 if (isPLAIN(cd->type)) {
718 cd->u.plain.uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
719 cd->u.plain.hdr.hash = NULL; /* no way to get this */
720 cd->u.plain.hdr.offset = dmd.u.crypt.offset;
721 cd->u.plain.hdr.skip = dmd.u.crypt.iv_offset;
722 cd->u.plain.key_size = dmd.u.crypt.vk->keylength;
724 r = crypt_parse_name_and_mode(dmd.u.crypt.cipher, cipher, NULL, cipher_mode);
726 cd->u.plain.cipher = strdup(cipher);
727 cd->u.plain.cipher_mode = strdup(cipher_mode);
729 } else if (isLOOPAES(cd->type)) {
730 cd->u.loopaes.uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
731 cd->u.loopaes.hdr.offset = dmd.u.crypt.offset;
733 r = crypt_parse_name_and_mode(dmd.u.crypt.cipher, cipher,
734 &key_nums, cipher_mode);
736 cd->u.loopaes.cipher = strdup(cipher);
737 cd->u.loopaes.cipher_mode = strdup(cipher_mode);
738 /* version 3 uses last key for IV */
739 if (dmd.u.crypt.vk->keylength % key_nums)
741 cd->u.loopaes.key_size = dmd.u.crypt.vk->keylength / key_nums;
743 } else if (isLUKS(cd->type)) {
744 if (crypt_metadata_device(cd)) {
745 r = _crypt_load_luks1(cd, 0, 0);
747 log_dbg("LUKS device header does not match active device.");
753 /* check whether UUIDs match each other */
754 r = crypt_uuid_cmp(dmd.uuid, cd->u.luks1.hdr.uuid);
756 log_dbg("LUKS device header uuid: %s mismatches DM returned uuid %s",
757 cd->u.luks1.hdr.uuid, dmd.uuid);
764 } else if (isTCRYPT(cd->type)) {
765 r = TCRYPT_init_by_name(cd, name, &dmd, &cd->device,
766 &cd->u.tcrypt.params, &cd->u.tcrypt.hdr);
769 crypt_free_volume_key(dmd.u.crypt.vk);
770 device_free(dmd.data_device);
771 free(CONST_CAST(void*)dmd.u.crypt.cipher);
772 free(CONST_CAST(void*)dmd.uuid);
776 static int _init_by_name_verity(struct crypt_device *cd, const char *name)
778 struct crypt_params_verity params = {};
779 struct crypt_dm_active_device dmd = {
781 .u.verity.vp = ¶ms,
785 r = dm_query_device(cd, name,
788 DM_ACTIVE_VERITY_HASH_DEVICE |
789 DM_ACTIVE_VERITY_PARAMS, &dmd);
793 if (isVERITY(cd->type)) {
794 cd->u.verity.uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
795 cd->u.verity.hdr.flags = CRYPT_VERITY_NO_HEADER; //FIXME
796 cd->u.verity.hdr.data_size = params.data_size;
797 cd->u.verity.root_hash_size = dmd.u.verity.root_hash_size;
798 cd->u.verity.root_hash = NULL;
799 cd->u.verity.hdr.hash_name = params.hash_name;
800 cd->u.verity.hdr.data_device = NULL;
801 cd->u.verity.hdr.hash_device = NULL;
802 cd->u.verity.hdr.data_block_size = params.data_block_size;
803 cd->u.verity.hdr.hash_block_size = params.hash_block_size;
804 cd->u.verity.hdr.hash_area_offset = dmd.u.verity.hash_offset;
805 cd->u.verity.hdr.hash_type = params.hash_type;
806 cd->u.verity.hdr.flags = params.flags;
807 cd->u.verity.hdr.salt_size = params.salt_size;
808 cd->u.verity.hdr.salt = params.salt;
809 cd->metadata_device = dmd.u.verity.hash_device;
812 device_free(dmd.data_device);
813 free(CONST_CAST(void*)dmd.uuid);
817 int crypt_init_by_name_and_header(struct crypt_device **cd,
819 const char *header_device)
821 crypt_status_info ci;
822 struct crypt_dm_active_device dmd;
825 log_dbg("Allocating crypt device context by device %s.", name);
827 ci = crypt_status(NULL, name);
828 if (ci == CRYPT_INVALID)
831 if (ci < CRYPT_ACTIVE) {
832 log_err(NULL, _("Device %s is not active.\n"), name);
836 r = dm_query_device(NULL, name, DM_ACTIVE_DEVICE | DM_ACTIVE_UUID, &dmd);
843 r = crypt_init(cd, header_device);
845 r = crypt_init(cd, device_path(dmd.data_device));
847 /* Underlying device disappeared but mapping still active */
848 if (!dmd.data_device || r == -ENOTBLK)
849 log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
852 /* Underlying device is not readable but crypt mapping exists */
854 device_free(dmd.data_device);
855 dmd.data_device = NULL;
856 r = crypt_init(cd, NULL);
864 if (!strncmp(CRYPT_PLAIN, dmd.uuid, sizeof(CRYPT_PLAIN)-1))
865 (*cd)->type = strdup(CRYPT_PLAIN);
866 else if (!strncmp(CRYPT_LOOPAES, dmd.uuid, sizeof(CRYPT_LOOPAES)-1))
867 (*cd)->type = strdup(CRYPT_LOOPAES);
868 else if (!strncmp(CRYPT_LUKS1, dmd.uuid, sizeof(CRYPT_LUKS1)-1))
869 (*cd)->type = strdup(CRYPT_LUKS1);
870 else if (!strncmp(CRYPT_VERITY, dmd.uuid, sizeof(CRYPT_VERITY)-1))
871 (*cd)->type = strdup(CRYPT_VERITY);
872 else if (!strncmp(CRYPT_TCRYPT, dmd.uuid, sizeof(CRYPT_TCRYPT)-1))
873 (*cd)->type = strdup(CRYPT_TCRYPT);
875 log_dbg("Unknown UUID set, some parameters are not set.");
877 log_dbg("Active device has no UUID set, some parameters are not set.");
880 r = crypt_set_data_device(*cd, device_path(dmd.data_device));
885 /* Try to initialise basic parameters from active device */
887 if (dmd.target == DM_CRYPT)
888 r = _init_by_name_crypt(*cd, name);
889 else if (dmd.target == DM_VERITY)
890 r = _init_by_name_verity(*cd, name);
896 device_free(dmd.data_device);
897 free(CONST_CAST(void*)dmd.uuid);
901 int crypt_init_by_name(struct crypt_device **cd, const char *name)
903 return crypt_init_by_name_and_header(cd, name, NULL);
906 static int _crypt_format_plain(struct crypt_device *cd,
908 const char *cipher_mode,
910 size_t volume_key_size,
911 struct crypt_params_plain *params)
913 if (!cipher || !cipher_mode) {
914 log_err(cd, _("Invalid plain crypt parameters.\n"));
918 if (volume_key_size > 1024) {
919 log_err(cd, _("Invalid key size.\n"));
923 if (!(cd->type = strdup(CRYPT_PLAIN)))
926 cd->u.plain.key_size = volume_key_size;
927 cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
931 cd->u.plain.cipher = strdup(cipher);
932 cd->u.plain.cipher_mode = strdup(cipher_mode);
935 cd->u.plain.uuid = strdup(uuid);
937 if (params && params->hash)
938 cd->u.plain.hdr.hash = strdup(params->hash);
940 cd->u.plain.hdr.offset = params ? params->offset : 0;
941 cd->u.plain.hdr.skip = params ? params->skip : 0;
942 cd->u.plain.hdr.size = params ? params->size : 0;
944 if (!cd->u.plain.cipher || !cd->u.plain.cipher_mode)
950 static int _crypt_format_luks1(struct crypt_device *cd,
952 const char *cipher_mode,
954 const char *volume_key,
955 size_t volume_key_size,
956 struct crypt_params_luks1 *params)
959 unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
960 unsigned long alignment_offset = 0;
962 if (!crypt_metadata_device(cd)) {
963 log_err(cd, _("Can't format LUKS without device.\n"));
967 if (!(cd->type = strdup(CRYPT_LUKS1)))
971 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
974 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
979 if (params && params->data_device) {
980 cd->metadata_device = cd->device;
982 if (device_alloc(&cd->device, params->data_device) < 0)
984 required_alignment = params->data_alignment * SECTOR_SIZE;
985 } else if (params && params->data_alignment) {
986 required_alignment = params->data_alignment * SECTOR_SIZE;
988 device_topology_alignment(cd->device,
990 &alignment_offset, DEFAULT_DISK_ALIGNMENT);
992 /* Check early if we cannot allocate block device for key slot access */
993 r = device_block_adjust(cd, cd->device, DEV_OK, 0, NULL, NULL);
997 r = LUKS_generate_phdr(&cd->u.luks1.hdr, cd->volume_key, cipher, cipher_mode,
998 (params && params->hash) ? params->hash : "sha1",
1000 required_alignment / SECTOR_SIZE,
1001 alignment_offset / SECTOR_SIZE,
1002 cd->iteration_time, &cd->u.luks1.PBKDF2_per_sec,
1003 cd->metadata_device ? 1 : 0, cd);
1007 /* Wipe first 8 sectors - fs magic numbers etc. */
1008 r = crypt_wipe(crypt_metadata_device(cd), 0, 8 * SECTOR_SIZE, CRYPT_WIPE_ZERO, 1);
1011 log_err(cd, _("Cannot format device %s which is still in use.\n"),
1012 mdata_device_path(cd));
1013 else if (r == -EACCES) {
1014 log_err(cd, _("Cannot format device %s, permission denied.\n"),
1015 mdata_device_path(cd));
1018 log_err(cd, _("Cannot wipe header on device %s.\n"),
1019 mdata_device_path(cd));
1024 r = LUKS_write_phdr(&cd->u.luks1.hdr, cd);
1029 static int _crypt_format_loopaes(struct crypt_device *cd,
1032 size_t volume_key_size,
1033 struct crypt_params_loopaes *params)
1035 if (!crypt_metadata_device(cd)) {
1036 log_err(cd, _("Can't format LOOPAES without device.\n"));
1040 if (volume_key_size > 1024) {
1041 log_err(cd, _("Invalid key size.\n"));
1045 if (!(cd->type = strdup(CRYPT_LOOPAES)))
1048 cd->u.loopaes.key_size = volume_key_size;
1050 cd->u.loopaes.cipher = strdup(cipher ?: DEFAULT_LOOPAES_CIPHER);
1053 cd->u.loopaes.uuid = strdup(uuid);
1055 if (params && params->hash)
1056 cd->u.loopaes.hdr.hash = strdup(params->hash);
1058 cd->u.loopaes.hdr.offset = params ? params->offset : 0;
1059 cd->u.loopaes.hdr.skip = params ? params->skip : 0;
1064 static int _crypt_format_verity(struct crypt_device *cd,
1066 struct crypt_params_verity *params)
1068 int r = 0, hash_size;
1069 uint64_t data_device_size;
1071 if (!crypt_metadata_device(cd)) {
1072 log_err(cd, _("Can't format VERITY without device.\n"));
1076 if (!params || !params->data_device)
1079 if (params->hash_type > VERITY_MAX_HASH_TYPE) {
1080 log_err(cd, _("Unsupported VERITY hash type %d.\n"), params->hash_type);
1084 if (VERITY_BLOCK_SIZE_OK(params->data_block_size) ||
1085 VERITY_BLOCK_SIZE_OK(params->hash_block_size)) {
1086 log_err(cd, _("Unsupported VERITY block size.\n"));
1090 if (params->hash_area_offset % 512) {
1091 log_err(cd, _("Unsupported VERITY hash offset.\n"));
1095 if (!(cd->type = strdup(CRYPT_VERITY)))
1098 r = crypt_set_data_device(cd, params->data_device);
1101 if (!params->data_size) {
1102 r = device_size(cd->device, &data_device_size);
1106 cd->u.verity.hdr.data_size = data_device_size / params->data_block_size;
1108 cd->u.verity.hdr.data_size = params->data_size;
1110 hash_size = crypt_hash_size(params->hash_name);
1111 if (hash_size <= 0) {
1112 log_err(cd, _("Hash algorithm %s not supported.\n"),
1116 cd->u.verity.root_hash_size = hash_size;
1118 cd->u.verity.root_hash = malloc(cd->u.verity.root_hash_size);
1119 if (!cd->u.verity.root_hash)
1122 cd->u.verity.hdr.flags = params->flags;
1123 if (!(cd->u.verity.hdr.hash_name = strdup(params->hash_name)))
1125 cd->u.verity.hdr.data_device = NULL;
1126 cd->u.verity.hdr.data_block_size = params->data_block_size;
1127 cd->u.verity.hdr.hash_block_size = params->hash_block_size;
1128 cd->u.verity.hdr.hash_area_offset = params->hash_area_offset;
1129 cd->u.verity.hdr.hash_type = params->hash_type;
1130 cd->u.verity.hdr.flags = params->flags;
1131 cd->u.verity.hdr.salt_size = params->salt_size;
1132 if (!(cd->u.verity.hdr.salt = malloc(params->salt_size)))
1136 memcpy(CONST_CAST(char*)cd->u.verity.hdr.salt, params->salt,
1139 r = crypt_random_get(cd, CONST_CAST(char*)cd->u.verity.hdr.salt,
1140 params->salt_size, CRYPT_RND_SALT);
1144 if (params->flags & CRYPT_VERITY_CREATE_HASH) {
1145 r = VERITY_create(cd, &cd->u.verity.hdr,
1146 cd->u.verity.root_hash, cd->u.verity.root_hash_size);
1151 if (!(params->flags & CRYPT_VERITY_NO_HEADER)) {
1153 cd->u.verity.uuid = strdup(uuid);
1155 r = VERITY_UUID_generate(cd, &cd->u.verity.uuid);
1160 r = VERITY_write_sb(cd, cd->u.verity.hdr.hash_area_offset,
1167 int crypt_format(struct crypt_device *cd,
1170 const char *cipher_mode,
1172 const char *volume_key,
1173 size_t volume_key_size,
1182 log_dbg("Context already formatted as %s.", cd->type);
1186 log_dbg("Formatting device %s as type %s.", mdata_device_path(cd) ?: "(none)", type);
1188 r = init_crypto(cd);
1193 r = _crypt_format_plain(cd, cipher, cipher_mode,
1194 uuid, volume_key_size, params);
1195 else if (isLUKS(type))
1196 r = _crypt_format_luks1(cd, cipher, cipher_mode,
1197 uuid, volume_key, volume_key_size, params);
1198 else if (isLOOPAES(type))
1199 r = _crypt_format_loopaes(cd, cipher, uuid, volume_key_size, params);
1200 else if (isVERITY(type))
1201 r = _crypt_format_verity(cd, uuid, params);
1203 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
1210 crypt_free_volume_key(cd->volume_key);
1211 cd->volume_key = NULL;
1217 int crypt_load(struct crypt_device *cd,
1218 const char *requested_type,
1223 log_dbg("Trying to load %s crypt type from device %s.",
1224 requested_type ?: "any", mdata_device_path(cd) ?: "(none)");
1226 if (!crypt_metadata_device(cd))
1229 if (!requested_type || isLUKS(requested_type)) {
1230 if (cd->type && !isLUKS(cd->type)) {
1231 log_dbg("Context is already initialised to type %s", cd->type);
1235 r = _crypt_load_luks1(cd, 1, 0);
1236 } else if (isVERITY(requested_type)) {
1237 if (cd->type && !isVERITY(cd->type)) {
1238 log_dbg("Context is already initialised to type %s", cd->type);
1241 r = _crypt_load_verity(cd, params);
1242 } else if (isTCRYPT(requested_type)) {
1243 if (cd->type && !isTCRYPT(cd->type)) {
1244 log_dbg("Context is already initialised to type %s", cd->type);
1247 r = _crypt_load_tcrypt(cd, params);
1254 int crypt_repair(struct crypt_device *cd,
1255 const char *requested_type,
1256 void *params __attribute__((unused)))
1260 log_dbg("Trying to repair %s crypt type from device %s.",
1261 requested_type ?: "any", mdata_device_path(cd) ?: "(none)");
1263 if (!crypt_metadata_device(cd))
1266 if (requested_type && !isLUKS(requested_type))
1270 /* Load with repair */
1271 r = _crypt_load_luks1(cd, 1, 1);
1275 /* cd->type and header must be set in context */
1276 r = crypt_check_data_device_size(cd);
1285 int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
1287 struct crypt_dm_active_device dmd;
1290 /* Device context type must be initialised */
1291 if (!cd->type || !crypt_get_uuid(cd))
1294 log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
1296 r = dm_query_device(cd, name, DM_ACTIVE_DEVICE | DM_ACTIVE_CRYPT_CIPHER |
1297 DM_ACTIVE_UUID | DM_ACTIVE_CRYPT_KEYSIZE |
1298 DM_ACTIVE_CRYPT_KEY, &dmd);
1300 log_err(NULL, _("Device %s is not active.\n"), name);
1304 if (!dmd.uuid || dmd.target != DM_CRYPT) {
1309 r = device_block_adjust(cd, dmd.data_device, DEV_OK,
1310 dmd.u.crypt.offset, &new_size, &dmd.flags);
1314 if (new_size == dmd.size) {
1315 log_dbg("Device has already requested size %" PRIu64
1316 " sectors.", dmd.size);
1319 dmd.size = new_size;
1320 if (isTCRYPT(cd->type))
1323 r = dm_create_device(cd, name, cd->type, &dmd, 1);
1326 if (dmd.target == DM_CRYPT) {
1327 crypt_free_volume_key(dmd.u.crypt.vk);
1328 free(CONST_CAST(void*)dmd.u.crypt.cipher);
1330 free(CONST_CAST(void*)dmd.data_device);
1331 free(CONST_CAST(void*)dmd.uuid);
1336 int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
1338 if (!isLUKS(cd->type)) {
1339 log_err(cd, _("This operation is not supported for this device type.\n"));
1343 if (uuid && !strncmp(uuid, cd->u.luks1.hdr.uuid, sizeof(cd->u.luks1.hdr.uuid))) {
1344 log_dbg("UUID is the same as requested (%s) for device %s.",
1345 uuid, mdata_device_path(cd));
1350 log_dbg("Requested new UUID change to %s for %s.", uuid, mdata_device_path(cd));
1352 log_dbg("Requested new UUID refresh for %s.", mdata_device_path(cd));
1354 if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
1357 return LUKS_hdr_uuid_set(&cd->u.luks1.hdr, uuid, cd);
1360 int crypt_header_backup(struct crypt_device *cd,
1361 const char *requested_type,
1362 const char *backup_file)
1366 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1369 r = init_crypto(cd);
1373 log_dbg("Requested header backup of device %s (%s) to "
1374 "file %s.", mdata_device_path(cd), requested_type, backup_file);
1376 return LUKS_hdr_backup(backup_file, &cd->u.luks1.hdr, cd);
1379 int crypt_header_restore(struct crypt_device *cd,
1380 const char *requested_type,
1381 const char *backup_file)
1385 if (requested_type && !isLUKS(requested_type))
1388 if (cd->type && !isLUKS(cd->type))
1391 r = init_crypto(cd);
1395 log_dbg("Requested header restore to device %s (%s) from "
1396 "file %s.", mdata_device_path(cd), requested_type, backup_file);
1398 return LUKS_hdr_restore(backup_file, &cd->u.luks1.hdr, cd);
1401 void crypt_free(struct crypt_device *cd)
1404 log_dbg("Releasing crypt device %s context.", mdata_device_path(cd));
1407 crypt_free_volume_key(cd->volume_key);
1409 device_free(cd->device);
1410 device_free(cd->metadata_device);
1412 if (isPLAIN(cd->type)) {
1413 free(CONST_CAST(void*)cd->u.plain.hdr.hash);
1414 free(cd->u.plain.cipher);
1415 free(cd->u.plain.cipher_mode);
1416 free(cd->u.plain.uuid);
1417 } else if (isLOOPAES(cd->type)) {
1418 free(CONST_CAST(void*)cd->u.loopaes.hdr.hash);
1419 free(cd->u.loopaes.cipher);
1420 free(cd->u.loopaes.uuid);
1421 } else if (isVERITY(cd->type)) {
1422 free(CONST_CAST(void*)cd->u.verity.hdr.hash_name);
1423 free(CONST_CAST(void*)cd->u.verity.hdr.salt);
1424 free(cd->u.verity.root_hash);
1425 free(cd->u.verity.uuid);
1429 /* Some structures can contain keys (TCRYPT), wipe it */
1430 memset(cd, 0, sizeof(*cd));
1435 int crypt_suspend(struct crypt_device *cd,
1438 crypt_status_info ci;
1441 log_dbg("Suspending volume %s.", name);
1443 if (!cd || !isLUKS(cd->type)) {
1444 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1449 ci = crypt_status(NULL, name);
1450 if (ci < CRYPT_ACTIVE) {
1451 log_err(cd, _("Volume %s is not active.\n"), name);
1457 r = dm_status_suspended(cd, name);
1462 log_err(cd, _("Volume %s is already suspended.\n"), name);
1467 r = dm_suspend_and_wipe_key(cd, name);
1469 log_err(cd, _("Suspend is not supported for device %s.\n"), name);
1471 log_err(cd, _("Error during suspending device %s.\n"), name);
1477 int crypt_resume_by_passphrase(struct crypt_device *cd,
1480 const char *passphrase,
1481 size_t passphrase_size)
1483 struct volume_key *vk = NULL;
1486 log_dbg("Resuming volume %s.", name);
1488 if (!isLUKS(cd->type)) {
1489 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1494 r = dm_status_suspended(cd, name);
1499 log_err(cd, _("Volume %s is not suspended.\n"), name);
1504 r = LUKS_open_key_with_hdr(keyslot, passphrase, passphrase_size,
1505 &cd->u.luks1.hdr, &vk, cd);
1507 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1511 r = dm_resume_and_reinstate_key(cd, name, vk->keylength, vk->key);
1513 log_err(cd, _("Resume is not supported for device %s.\n"), name);
1515 log_err(cd, _("Error during resuming device %s.\n"), name);
1519 crypt_free_volume_key(vk);
1520 return r < 0 ? r : keyslot;
1523 int crypt_resume_by_keyfile_offset(struct crypt_device *cd,
1526 const char *keyfile,
1527 size_t keyfile_size,
1528 size_t keyfile_offset)
1530 struct volume_key *vk = NULL;
1531 char *passphrase_read = NULL;
1532 size_t passphrase_size_read;
1535 log_dbg("Resuming volume %s.", name);
1537 if (!isLUKS(cd->type)) {
1538 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1543 r = dm_status_suspended(cd, name);
1548 log_err(cd, _("Volume %s is not suspended.\n"), name);
1555 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1556 &passphrase_size_read, keyfile, keyfile_offset,
1561 r = LUKS_open_key_with_hdr(keyslot, passphrase_read,
1562 passphrase_size_read, &cd->u.luks1.hdr, &vk, cd);
1567 r = dm_resume_and_reinstate_key(cd, name, vk->keylength, vk->key);
1569 log_err(cd, _("Error during resuming device %s.\n"), name);
1571 crypt_safe_free(passphrase_read);
1572 crypt_free_volume_key(vk);
1573 return r < 0 ? r : keyslot;
1576 int crypt_resume_by_keyfile(struct crypt_device *cd,
1579 const char *keyfile,
1580 size_t keyfile_size)
1582 return crypt_resume_by_keyfile_offset(cd, name, keyslot,
1583 keyfile, keyfile_size, 0);
1586 // slot manipulation
1587 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1588 int keyslot, // -1 any
1589 const char *passphrase, // NULL -> terminal
1590 size_t passphrase_size,
1591 const char *new_passphrase, // NULL -> terminal
1592 size_t new_passphrase_size)
1594 struct volume_key *vk = NULL;
1595 char *password = NULL, *new_password = NULL;
1596 size_t passwordLen, new_passwordLen;
1599 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1600 "new passphrase %sprovided.",
1601 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1603 if (!isLUKS(cd->type)) {
1604 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1608 r = keyslot_verify_or_find_empty(cd, &keyslot);
1612 if (!LUKS_keyslot_active_count(&cd->u.luks1.hdr)) {
1613 /* No slots used, try to use pre-generated key in header */
1614 if (cd->volume_key) {
1615 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1616 r = vk ? 0 : -ENOMEM;
1618 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1621 } else if (passphrase) {
1622 /* Passphrase provided, use it to unlock existing keyslot */
1623 r = LUKS_open_key_with_hdr(CRYPT_ANY_SLOT, passphrase,
1624 passphrase_size, &cd->u.luks1.hdr, &vk, cd);
1626 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1627 r = key_from_terminal(cd, _("Enter any passphrase: "),
1628 &password, &passwordLen, 0);
1632 r = LUKS_open_key_with_hdr(CRYPT_ANY_SLOT, password,
1633 passwordLen, &cd->u.luks1.hdr, &vk, cd);
1634 crypt_safe_free(password);
1640 if (new_passphrase) {
1641 new_password = CONST_CAST(char*)new_passphrase;
1642 new_passwordLen = new_passphrase_size;
1644 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1645 &new_password, &new_passwordLen, 1);
1650 r = LUKS_set_key(keyslot, new_password, new_passwordLen,
1651 &cd->u.luks1.hdr, vk, cd->iteration_time, &cd->u.luks1.PBKDF2_per_sec, cd);
1656 if (!new_passphrase)
1657 crypt_safe_free(new_password);
1658 crypt_free_volume_key(vk);
1659 return r ?: keyslot;
1662 int crypt_keyslot_change_by_passphrase(struct crypt_device *cd,
1665 const char *passphrase,
1666 size_t passphrase_size,
1667 const char *new_passphrase,
1668 size_t new_passphrase_size)
1670 struct volume_key *vk = NULL;
1673 log_dbg("Changing passphrase from old keyslot %d to new %d.",
1674 keyslot_old, keyslot_new);
1676 if (!isLUKS(cd->type)) {
1677 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1681 r = LUKS_open_key_with_hdr(keyslot_old, passphrase, passphrase_size,
1682 &cd->u.luks1.hdr, &vk, cd);
1686 if (keyslot_old != CRYPT_ANY_SLOT && keyslot_old != r) {
1687 log_dbg("Keyslot mismatch.");
1692 if (keyslot_new == CRYPT_ANY_SLOT) {
1693 keyslot_new = LUKS_keyslot_find_empty(&cd->u.luks1.hdr);
1694 if (keyslot_new < 0)
1695 keyslot_new = keyslot_old;
1698 if (keyslot_old == keyslot_new) {
1699 log_dbg("Key slot %d is going to be overwritten.", keyslot_old);
1700 (void)crypt_keyslot_destroy(cd, keyslot_old);
1703 r = LUKS_set_key(keyslot_new, new_passphrase, new_passphrase_size,
1704 &cd->u.luks1.hdr, vk, cd->iteration_time,
1705 &cd->u.luks1.PBKDF2_per_sec, cd);
1707 if (keyslot_old == keyslot_new) {
1709 log_verbose(cd, _("Key slot %d changed.\n"), r);
1712 log_verbose(cd, _("Replaced with key slot %d.\n"), r);
1713 r = crypt_keyslot_destroy(cd, keyslot_old);
1717 log_err(cd, _("Failed to swap new key slot.\n"));
1719 crypt_free_volume_key(vk);
1720 return r ?: keyslot_new;
1723 int crypt_keyslot_add_by_keyfile_offset(struct crypt_device *cd,
1725 const char *keyfile,
1726 size_t keyfile_size,
1727 size_t keyfile_offset,
1728 const char *new_keyfile,
1729 size_t new_keyfile_size,
1730 size_t new_keyfile_offset)
1732 struct volume_key *vk = NULL;
1733 char *password = NULL; size_t passwordLen;
1734 char *new_password = NULL; size_t new_passwordLen;
1737 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1738 keyfile ?: "[none]", new_keyfile ?: "[none]");
1740 if (!isLUKS(cd->type)) {
1741 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1745 r = keyslot_verify_or_find_empty(cd, &keyslot);
1749 if (!LUKS_keyslot_active_count(&cd->u.luks1.hdr)) {
1750 /* No slots used, try to use pre-generated key in header */
1751 if (cd->volume_key) {
1752 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1753 r = vk ? 0 : -ENOMEM;
1755 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1759 /* Read password from file of (if NULL) from terminal */
1761 r = key_from_file(cd, _("Enter any passphrase: "),
1762 &password, &passwordLen,
1763 keyfile, keyfile_offset, keyfile_size);
1765 r = key_from_terminal(cd, _("Enter any passphrase: "),
1766 &password, &passwordLen, 0);
1770 r = LUKS_open_key_with_hdr(CRYPT_ANY_SLOT, password, passwordLen,
1771 &cd->u.luks1.hdr, &vk, cd);
1778 r = key_from_file(cd, _("Enter new passphrase for key slot: "),
1779 &new_password, &new_passwordLen, new_keyfile,
1780 new_keyfile_offset, new_keyfile_size);
1782 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1783 &new_password, &new_passwordLen, 1);
1787 r = LUKS_set_key(keyslot, new_password, new_passwordLen,
1788 &cd->u.luks1.hdr, vk, cd->iteration_time, &cd->u.luks1.PBKDF2_per_sec, cd);
1790 crypt_safe_free(password);
1791 crypt_safe_free(new_password);
1792 crypt_free_volume_key(vk);
1793 return r < 0 ? r : keyslot;
1796 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1798 const char *keyfile,
1799 size_t keyfile_size,
1800 const char *new_keyfile,
1801 size_t new_keyfile_size)
1803 return crypt_keyslot_add_by_keyfile_offset(cd, keyslot,
1804 keyfile, keyfile_size, 0,
1805 new_keyfile, new_keyfile_size, 0);
1808 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1810 const char *volume_key,
1811 size_t volume_key_size,
1812 const char *passphrase,
1813 size_t passphrase_size)
1815 struct volume_key *vk = NULL;
1817 char *new_password = NULL; size_t new_passwordLen;
1819 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1821 if (!isLUKS(cd->type)) {
1822 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1827 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1828 else if (cd->volume_key)
1829 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1834 r = LUKS_verify_volume_key(&cd->u.luks1.hdr, vk);
1836 log_err(cd, _("Volume key does not match the volume.\n"));
1840 r = keyslot_verify_or_find_empty(cd, &keyslot);
1845 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1846 &new_password, &new_passwordLen, 1);
1849 passphrase = new_password;
1850 passphrase_size = new_passwordLen;
1853 r = LUKS_set_key(keyslot, passphrase, passphrase_size,
1854 &cd->u.luks1.hdr, vk, cd->iteration_time, &cd->u.luks1.PBKDF2_per_sec, cd);
1856 crypt_safe_free(new_password);
1857 crypt_free_volume_key(vk);
1858 return (r < 0) ? r : keyslot;
1861 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1863 crypt_keyslot_info ki;
1865 log_dbg("Destroying keyslot %d.", keyslot);
1867 if (!isLUKS(cd->type)) {
1868 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1872 ki = crypt_keyslot_status(cd, keyslot);
1873 if (ki == CRYPT_SLOT_INVALID) {
1874 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1878 if (ki == CRYPT_SLOT_INACTIVE) {
1879 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1883 return LUKS_del_key(keyslot, &cd->u.luks1.hdr, cd);
1886 // activation/deactivation of device mapping
1887 int crypt_activate_by_passphrase(struct crypt_device *cd,
1890 const char *passphrase,
1891 size_t passphrase_size,
1894 crypt_status_info ci;
1895 struct volume_key *vk = NULL;
1896 char *read_passphrase = NULL;
1897 size_t passphraseLen = 0;
1900 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1901 name ? "Activating" : "Checking", name ?: "",
1902 keyslot, passphrase ? "" : "[none] ");
1905 ci = crypt_status(NULL, name);
1906 if (ci == CRYPT_INVALID)
1908 else if (ci >= CRYPT_ACTIVE) {
1909 log_err(cd, _("Device %s already exists.\n"), name);
1914 /* plain, use hashed passphrase */
1915 if (isPLAIN(cd->type)) {
1920 r = key_from_terminal(cd, NULL, &read_passphrase,
1924 passphrase = read_passphrase;
1925 passphrase_size = passphraseLen;
1928 r = process_key(cd, cd->u.plain.hdr.hash,
1929 cd->u.plain.key_size,
1930 passphrase, passphrase_size, &vk);
1934 r = PLAIN_activate(cd, name, vk, cd->u.plain.hdr.size, flags);
1936 } else if (isLUKS(cd->type)) {
1937 /* provided passphrase, do not retry */
1939 r = LUKS_open_key_with_hdr(keyslot, passphrase,
1940 passphrase_size, &cd->u.luks1.hdr, &vk, cd);
1942 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1947 r = LUKS1_activate(cd, name, vk, flags);
1952 crypt_safe_free(read_passphrase);
1953 crypt_free_volume_key(vk);
1955 return r < 0 ? r : keyslot;
1958 int crypt_activate_by_keyfile_offset(struct crypt_device *cd,
1961 const char *keyfile,
1962 size_t keyfile_size,
1963 size_t keyfile_offset,
1966 crypt_status_info ci;
1967 struct volume_key *vk = NULL;
1968 char *passphrase_read = NULL;
1969 size_t passphrase_size_read;
1970 unsigned int key_count = 0;
1973 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1974 name ?: "", keyslot, keyfile ?: "[none]");
1977 ci = crypt_status(NULL, name);
1978 if (ci == CRYPT_INVALID)
1980 else if (ci >= CRYPT_ACTIVE) {
1981 log_err(cd, _("Device %s already exists.\n"), name);
1989 if (isPLAIN(cd->type)) {
1993 r = key_from_file(cd, _("Enter passphrase: "),
1994 &passphrase_read, &passphrase_size_read,
1995 keyfile, keyfile_offset, keyfile_size);
1999 r = process_key(cd, cd->u.plain.hdr.hash,
2000 cd->u.plain.key_size,
2001 passphrase_read, passphrase_size_read, &vk);
2005 r = PLAIN_activate(cd, name, vk, cd->u.plain.hdr.size, flags);
2006 } else if (isLUKS(cd->type)) {
2007 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
2008 &passphrase_size_read, keyfile, keyfile_offset, keyfile_size);
2011 r = LUKS_open_key_with_hdr(keyslot, passphrase_read,
2012 passphrase_size_read, &cd->u.luks1.hdr, &vk, cd);
2018 r = LUKS1_activate(cd, name, vk, flags);
2023 } else if (isLOOPAES(cd->type)) {
2024 r = key_from_file(cd, NULL, &passphrase_read, &passphrase_size_read,
2025 keyfile, keyfile_offset, keyfile_size);
2028 r = LOOPAES_parse_keyfile(cd, &vk, cd->u.loopaes.hdr.hash, &key_count,
2029 passphrase_read, passphrase_size_read);
2033 r = LOOPAES_activate(cd, name, cd->u.loopaes.cipher,
2034 key_count, vk, flags);
2039 crypt_safe_free(passphrase_read);
2040 crypt_free_volume_key(vk);
2045 int crypt_activate_by_keyfile(struct crypt_device *cd,
2048 const char *keyfile,
2049 size_t keyfile_size,
2052 return crypt_activate_by_keyfile_offset(cd, name, keyslot, keyfile,
2053 keyfile_size, 0, flags);
2056 int crypt_activate_by_volume_key(struct crypt_device *cd,
2058 const char *volume_key,
2059 size_t volume_key_size,
2062 crypt_status_info ci;
2063 struct volume_key *vk = NULL;
2066 log_dbg("Activating volume %s by volume key.", name ?: "[none]");
2069 ci = crypt_status(NULL, name);
2070 if (ci == CRYPT_INVALID)
2072 else if (ci >= CRYPT_ACTIVE) {
2073 log_err(cd, _("Device %s already exists.\n"), name);
2078 /* use key directly, no hash */
2079 if (isPLAIN(cd->type)) {
2083 if (!volume_key || !volume_key_size || volume_key_size != cd->u.plain.key_size) {
2084 log_err(cd, _("Incorrect volume key specified for plain device.\n"));
2088 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2092 r = PLAIN_activate(cd, name, vk, cd->u.plain.hdr.size, flags);
2093 } else if (isLUKS(cd->type)) {
2094 /* If key is not provided, try to use internal key */
2096 if (!cd->volume_key) {
2097 log_err(cd, _("Volume key does not match the volume.\n"));
2100 volume_key_size = cd->volume_key->keylength;
2101 volume_key = cd->volume_key->key;
2104 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2107 r = LUKS_verify_volume_key(&cd->u.luks1.hdr, vk);
2110 log_err(cd, _("Volume key does not match the volume.\n"));
2113 r = LUKS1_activate(cd, name, vk, flags);
2114 } else if (isVERITY(cd->type)) {
2115 /* volume_key == root hash */
2116 if (!volume_key || !volume_key_size) {
2117 log_err(cd, _("Incorrect root hash specified for verity device.\n"));
2121 r = VERITY_activate(cd, name, volume_key, volume_key_size,
2122 &cd->u.verity.hdr, CRYPT_ACTIVATE_READONLY);
2125 free(cd->u.verity.root_hash);
2126 cd->u.verity.root_hash = NULL;
2128 cd->u.verity.root_hash_size = volume_key_size;
2129 if (!cd->u.verity.root_hash)
2130 cd->u.verity.root_hash = malloc(volume_key_size);
2131 if (cd->u.verity.root_hash)
2132 memcpy(cd->u.verity.root_hash, volume_key, volume_key_size);
2134 } else if (isTCRYPT(cd->type)) {
2137 r = TCRYPT_activate(cd, name, &cd->u.tcrypt.hdr,
2138 &cd->u.tcrypt.params, flags);
2140 log_err(cd, _("Device type is not properly initialised.\n"));
2142 crypt_free_volume_key(vk);
2147 int crypt_deactivate(struct crypt_device *cd, const char *name)
2154 log_dbg("Deactivating volume %s.", name);
2159 switch (crypt_status(cd, name)) {
2162 if (cd && isTCRYPT(cd->type))
2163 r = TCRYPT_deactivate(cd, name);
2165 r = dm_remove_device(cd, name, 0, 0);
2167 case CRYPT_INACTIVE:
2168 log_err(cd, _("Device %s is not active.\n"), name);
2172 log_err(cd, _("Invalid device %s.\n"), name);
2182 int crypt_volume_key_get(struct crypt_device *cd,
2185 size_t *volume_key_size,
2186 const char *passphrase,
2187 size_t passphrase_size)
2189 struct volume_key *vk = NULL;
2193 if (crypt_fips_mode()) {
2194 log_err(cd, _("Function not available in FIPS mode.\n"));
2198 key_len = crypt_get_volume_key_size(cd);
2199 if (key_len > *volume_key_size) {
2200 log_err(cd, _("Volume key buffer too small.\n"));
2204 if (isPLAIN(cd->type) && cd->u.plain.hdr.hash) {
2205 r = process_key(cd, cd->u.plain.hdr.hash, key_len,
2206 passphrase, passphrase_size, &vk);
2208 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
2209 } else if (isLUKS(cd->type)) {
2210 r = LUKS_open_key_with_hdr(keyslot, passphrase,
2211 passphrase_size, &cd->u.luks1.hdr, &vk, cd);
2212 } else if (isTCRYPT(cd->type)) {
2213 r = TCRYPT_get_volume_key(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params, &vk);
2215 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
2218 memcpy(volume_key, vk->key, vk->keylength);
2219 *volume_key_size = vk->keylength;
2222 crypt_free_volume_key(vk);
2226 int crypt_volume_key_verify(struct crypt_device *cd,
2227 const char *volume_key,
2228 size_t volume_key_size)
2230 struct volume_key *vk;
2233 if (!isLUKS(cd->type)) {
2234 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2238 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2242 r = LUKS_verify_volume_key(&cd->u.luks1.hdr, vk);
2245 log_err(cd, _("Volume key does not match the volume.\n"));
2247 crypt_free_volume_key(vk);
2252 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
2254 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
2255 cd->timeout = timeout_sec;
2258 void crypt_set_password_retry(struct crypt_device *cd, int tries)
2260 log_dbg("Password retry count set to %d.", tries);
2264 void crypt_set_iteration_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2266 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
2267 cd->iteration_time = iteration_time_ms;
2269 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2271 crypt_set_iteration_time(cd, iteration_time_ms);
2274 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
2276 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
2277 cd->password_verify = password_verify ? 1 : 0;
2280 void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
2283 case CRYPT_RNG_URANDOM:
2284 case CRYPT_RNG_RANDOM:
2285 log_dbg("RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
2286 cd->rng_type = rng_type;
2290 int crypt_get_rng_type(struct crypt_device *cd)
2295 return cd->rng_type;
2298 int crypt_memory_lock(struct crypt_device *cd, int lock)
2300 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
2304 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
2311 r = dm_status_device(cd, name);
2316 if (r < 0 && r != -ENODEV)
2317 return CRYPT_INVALID;
2320 return CRYPT_ACTIVE;
2325 return CRYPT_INACTIVE;
2328 static void hexprint(struct crypt_device *cd, const char *d, int n, const char *sep)
2331 for(i = 0; i < n; i++)
2332 log_std(cd, "%02hhx%s", (const char)d[i], sep);
2335 static int _luks_dump(struct crypt_device *cd)
2339 log_std(cd, "LUKS header information for %s\n\n", mdata_device_path(cd));
2340 log_std(cd, "Version: \t%d\n", cd->u.luks1.hdr.version);
2341 log_std(cd, "Cipher name: \t%s\n", cd->u.luks1.hdr.cipherName);
2342 log_std(cd, "Cipher mode: \t%s\n", cd->u.luks1.hdr.cipherMode);
2343 log_std(cd, "Hash spec: \t%s\n", cd->u.luks1.hdr.hashSpec);
2344 log_std(cd, "Payload offset:\t%d\n", cd->u.luks1.hdr.payloadOffset);
2345 log_std(cd, "MK bits: \t%d\n", cd->u.luks1.hdr.keyBytes * 8);
2346 log_std(cd, "MK digest: \t");
2347 hexprint(cd, cd->u.luks1.hdr.mkDigest, LUKS_DIGESTSIZE, " ");
2349 log_std(cd, "MK salt: \t");
2350 hexprint(cd, cd->u.luks1.hdr.mkDigestSalt, LUKS_SALTSIZE/2, " ");
2351 log_std(cd, "\n \t");
2352 hexprint(cd, cd->u.luks1.hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ");
2354 log_std(cd, "MK iterations: \t%d\n", cd->u.luks1.hdr.mkDigestIterations);
2355 log_std(cd, "UUID: \t%s\n\n", cd->u.luks1.hdr.uuid);
2356 for(i = 0; i < LUKS_NUMKEYS; i++) {
2357 if(cd->u.luks1.hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
2358 log_std(cd, "Key Slot %d: ENABLED\n",i);
2359 log_std(cd, "\tIterations: \t%d\n",
2360 cd->u.luks1.hdr.keyblock[i].passwordIterations);
2361 log_std(cd, "\tSalt: \t");
2362 hexprint(cd, cd->u.luks1.hdr.keyblock[i].passwordSalt,
2363 LUKS_SALTSIZE/2, " ");
2364 log_std(cd, "\n\t \t");
2365 hexprint(cd, cd->u.luks1.hdr.keyblock[i].passwordSalt +
2366 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ");
2369 log_std(cd, "\tKey material offset:\t%d\n",
2370 cd->u.luks1.hdr.keyblock[i].keyMaterialOffset);
2371 log_std(cd, "\tAF stripes: \t%d\n",
2372 cd->u.luks1.hdr.keyblock[i].stripes);
2375 log_std(cd, "Key Slot %d: DISABLED\n", i);
2380 static int _verity_dump(struct crypt_device *cd)
2382 log_std(cd, "VERITY header information for %s\n", mdata_device_path(cd));
2383 log_std(cd, "UUID: \t%s\n", cd->u.verity.uuid ?: "");
2384 log_std(cd, "Hash type: \t%u\n", cd->u.verity.hdr.hash_type);
2385 log_std(cd, "Data blocks: \t%" PRIu64 "\n", cd->u.verity.hdr.data_size);
2386 log_std(cd, "Data block size: \t%u\n", cd->u.verity.hdr.data_block_size);
2387 log_std(cd, "Hash block size: \t%u\n", cd->u.verity.hdr.hash_block_size);
2388 log_std(cd, "Hash algorithm: \t%s\n", cd->u.verity.hdr.hash_name);
2389 log_std(cd, "Salt: \t");
2390 if (cd->u.verity.hdr.salt_size)
2391 hexprint(cd, cd->u.verity.hdr.salt, cd->u.verity.hdr.salt_size, "");
2395 if (cd->u.verity.root_hash) {
2396 log_std(cd, "Root hash: \t");
2397 hexprint(cd, cd->u.verity.root_hash, cd->u.verity.root_hash_size, "");
2403 int crypt_dump(struct crypt_device *cd)
2405 if (isLUKS(cd->type))
2406 return _luks_dump(cd);
2407 else if (isVERITY(cd->type))
2408 return _verity_dump(cd);
2409 else if (isTCRYPT(cd->type))
2410 return TCRYPT_dump(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
2412 log_err(cd, _("Dump operation is not supported for this device type.\n"));
2416 const char *crypt_get_cipher(struct crypt_device *cd)
2418 if (isPLAIN(cd->type))
2419 return cd->u.plain.cipher;
2421 if (isLUKS(cd->type))
2422 return cd->u.luks1.hdr.cipherName;
2424 if (isLOOPAES(cd->type))
2425 return cd->u.loopaes.cipher;
2427 if (isTCRYPT(cd->type))
2428 return cd->u.tcrypt.params.cipher;
2433 const char *crypt_get_cipher_mode(struct crypt_device *cd)
2435 if (isPLAIN(cd->type))
2436 return cd->u.plain.cipher_mode;
2438 if (isLUKS(cd->type))
2439 return cd->u.luks1.hdr.cipherMode;
2441 if (isLOOPAES(cd->type))
2442 return cd->u.loopaes.cipher_mode;
2444 if (isTCRYPT(cd->type))
2445 return cd->u.tcrypt.params.mode;
2450 const char *crypt_get_uuid(struct crypt_device *cd)
2452 if (isLUKS(cd->type))
2453 return cd->u.luks1.hdr.uuid;
2455 if (isPLAIN(cd->type))
2456 return cd->u.plain.uuid;
2458 if (isLOOPAES(cd->type))
2459 return cd->u.loopaes.uuid;
2461 if (isVERITY(cd->type))
2462 return cd->u.verity.uuid;
2467 const char *crypt_get_device_name(struct crypt_device *cd)
2469 const char *path = device_block_path(cd->device);
2472 path = device_path(cd->device);
2477 int crypt_get_volume_key_size(struct crypt_device *cd)
2479 if (isPLAIN(cd->type))
2480 return cd->u.plain.key_size;
2482 if (isLUKS(cd->type))
2483 return cd->u.luks1.hdr.keyBytes;
2485 if (isLOOPAES(cd->type))
2486 return cd->u.loopaes.key_size;
2488 if (isVERITY(cd->type))
2489 return cd->u.verity.root_hash_size;
2491 if (isTCRYPT(cd->type))
2492 return cd->u.tcrypt.params.key_size;
2497 uint64_t crypt_get_data_offset(struct crypt_device *cd)
2499 if (isPLAIN(cd->type))
2500 return cd->u.plain.hdr.offset;
2502 if (isLUKS(cd->type))
2503 return cd->u.luks1.hdr.payloadOffset;
2505 if (isLOOPAES(cd->type))
2506 return cd->u.loopaes.hdr.offset;
2508 if (isTCRYPT(cd->type))
2509 return TCRYPT_get_data_offset(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
2514 uint64_t crypt_get_iv_offset(struct crypt_device *cd)
2516 if (isPLAIN(cd->type))
2517 return cd->u.plain.hdr.skip;
2519 if (isLUKS(cd->type))
2522 if (isLOOPAES(cd->type))
2523 return cd->u.loopaes.hdr.skip;
2525 if (isTCRYPT(cd->type))
2526 return TCRYPT_get_iv_offset(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
2531 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
2533 if (!isLUKS(cd->type)) {
2534 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2535 return CRYPT_SLOT_INVALID;
2538 return LUKS_keyslot_info(&cd->u.luks1.hdr, keyslot);
2541 int crypt_keyslot_max(const char *type)
2543 if (type && isLUKS(type))
2544 return LUKS_NUMKEYS;
2549 int crypt_keyslot_area(struct crypt_device *cd,
2554 if (!isLUKS(cd->type))
2557 return LUKS_keyslot_area(&cd->u.luks1.hdr, keyslot, offset, length);
2560 const char *crypt_get_type(struct crypt_device *cd)
2565 int crypt_get_verity_info(struct crypt_device *cd,
2566 struct crypt_params_verity *vp)
2568 if (!isVERITY(cd->type) || !vp)
2571 vp->data_device = device_path(cd->device);
2572 vp->hash_device = mdata_device_path(cd);
2573 vp->hash_name = cd->u.verity.hdr.hash_name;
2574 vp->salt = cd->u.verity.hdr.salt;
2575 vp->salt_size = cd->u.verity.hdr.salt_size;
2576 vp->data_block_size = cd->u.verity.hdr.data_block_size;
2577 vp->hash_block_size = cd->u.verity.hdr.hash_block_size;
2578 vp->data_size = cd->u.verity.hdr.data_size;
2579 vp->hash_area_offset = cd->u.verity.hdr.hash_area_offset;
2580 vp->hash_type = cd->u.verity.hdr.hash_type;
2581 vp->flags = cd->u.verity.hdr.flags & CRYPT_VERITY_NO_HEADER;
2585 int crypt_get_active_device(struct crypt_device *cd, const char *name,
2586 struct crypt_active_device *cad)
2588 struct crypt_dm_active_device dmd;
2591 r = dm_query_device(cd, name, 0, &dmd);
2595 if (dmd.target != DM_CRYPT && dmd.target != DM_VERITY)
2598 if (cd && isTCRYPT(cd->type)) {
2599 cad->offset = TCRYPT_get_data_offset(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
2600 cad->iv_offset = TCRYPT_get_iv_offset(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
2602 cad->offset = dmd.u.crypt.offset;
2603 cad->iv_offset = dmd.u.crypt.iv_offset;
2605 cad->size = dmd.size;
2606 cad->flags = dmd.flags;