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"
38 struct device *device;
39 struct device *metadata_device;
41 struct volume_key *volume_key;
43 uint64_t iteration_time;
48 /* used in CRYPT_LUKS1 */
50 uint64_t PBKDF2_per_sec;
52 /* used in CRYPT_PLAIN */
53 struct crypt_params_plain plain_hdr;
55 char *plain_cipher_mode;
57 unsigned int plain_key_size;
59 /* used in CRYPT_LOOPAES */
60 struct crypt_params_loopaes loopaes_hdr;
62 char *loopaes_cipher_mode;
64 unsigned int loopaes_key_size;
66 /* used in CRYPT_VERITY */
67 struct crypt_params_verity verity_hdr;
68 char *verity_root_hash;
69 unsigned int verity_root_hash_size;
72 /* callbacks definitions */
73 void (*log)(int level, const char *msg, void *usrptr);
75 int (*confirm)(const char *msg, void *usrptr);
77 int (*password)(const char *msg, char *buf, size_t length, void *usrptr);
78 void *password_usrptr;
80 /* last error message */
81 char error[MAX_ERROR_LENGTH];
85 /* FIXME: not thread safe, remove this later */
86 static char global_error[MAX_ERROR_LENGTH] = {0};
89 static void (*_default_log)(int level, const char *msg, void *usrptr) = NULL;
90 static int _debug_level = 0;
92 void crypt_set_debug_level(int level)
97 int crypt_get_debug_level(void)
102 static void crypt_set_error(struct crypt_device *cd, const char *error)
104 size_t size = strlen(error);
106 /* Set global error, ugly hack... */
107 strncpy(global_error, error, MAX_ERROR_LENGTH - 2);
108 if (size < MAX_ERROR_LENGTH && global_error[size - 1] == '\n')
109 global_error[size - 1] = '\0';
111 /* Set error string per context */
113 strncpy(cd->error, error, MAX_ERROR_LENGTH - 2);
114 if (size < MAX_ERROR_LENGTH && cd->error[size - 1] == '\n')
115 cd->error[size - 1] = '\0';
119 void crypt_log(struct crypt_device *cd, int level, const char *msg)
122 cd->log(level, msg, cd->log_usrptr);
123 else if (_default_log)
124 _default_log(level, msg, NULL);
126 if (level == CRYPT_LOG_ERROR)
127 crypt_set_error(cd, msg);
130 __attribute__((format(printf, 5, 6)))
131 void logger(struct crypt_device *cd, int level, const char *file,
132 int line, const char *format, ...)
137 va_start(argp, format);
139 if (vasprintf(&target, format, argp) > 0 ) {
141 crypt_log(cd, level, target);
143 } else if (_debug_level)
144 printf("# %s:%d %s\n", file ?: "?", line, target);
146 } else if (_debug_level)
147 printf("# %s\n", target);
155 static const char *mdata_device_path(struct crypt_device *cd)
157 return device_path(cd->metadata_device ?: cd->device);
161 struct device *crypt_metadata_device(struct crypt_device *cd)
163 return cd->metadata_device ?: cd->device;
166 struct device *crypt_data_device(struct crypt_device *cd)
171 static int init_crypto(struct crypt_device *ctx)
175 crypt_fips_libcryptsetup_check(ctx);
177 r = crypt_random_init(ctx);
179 log_err(ctx, _("Cannot initialize crypto RNG backend.\n"));
183 r = crypt_backend_init(ctx);
185 log_err(ctx, _("Cannot initialize crypto backend.\n"));
187 log_dbg("Crypto backend (%s) initialized.", crypt_backend_version());
191 static int process_key(struct crypt_device *cd, const char *hash_name,
192 size_t key_size, const char *pass, size_t passLen,
193 struct volume_key **vk)
200 *vk = crypt_alloc_volume_key(key_size, NULL);
205 r = crypt_plain_hash(cd, hash_name, (*vk)->key, key_size, pass, passLen);
208 log_err(cd, _("Hash algorithm %s not supported.\n"),
211 log_err(cd, _("Key processing error (using hash %s).\n"),
213 crypt_free_volume_key(*vk);
217 } else if (passLen > key_size) {
218 memcpy((*vk)->key, pass, key_size);
220 memcpy((*vk)->key, pass, passLen);
226 static int isPLAIN(const char *type)
228 return (type && !strcmp(CRYPT_PLAIN, type));
231 static int isLUKS(const char *type)
233 return (type && !strcmp(CRYPT_LUKS1, type));
236 static int isLOOPAES(const char *type)
238 return (type && !strcmp(CRYPT_LOOPAES, type));
241 static int isVERITY(const char *type)
243 return (type && !strcmp(CRYPT_VERITY, type));
246 /* keyslot helpers */
247 static int keyslot_verify_or_find_empty(struct crypt_device *cd, int *keyslot)
249 if (*keyslot == CRYPT_ANY_SLOT) {
250 *keyslot = LUKS_keyslot_find_empty(&cd->hdr);
252 log_err(cd, _("All key slots full.\n"));
257 switch (LUKS_keyslot_info(&cd->hdr, *keyslot)) {
258 case CRYPT_SLOT_INVALID:
259 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
260 *keyslot, LUKS_NUMKEYS - 1);
262 case CRYPT_SLOT_INACTIVE:
265 log_err(cd, _("Key slot %d is full, please select another one.\n"),
274 * compares UUIDs returned by device-mapper (striped by cryptsetup) and uuid in header
276 static int crypt_uuid_cmp(const char *dm_uuid, const char *hdr_uuid)
281 if (!dm_uuid || !hdr_uuid)
284 str = strchr(dm_uuid, '-');
288 for (i = 0, j = 1; hdr_uuid[i]; i++) {
289 if (hdr_uuid[i] == '-')
292 if (!str[j] || str[j] == '-')
295 if (str[j] != hdr_uuid[i])
303 int PLAIN_activate(struct crypt_device *cd,
305 struct volume_key *vk,
310 char *dm_cipher = NULL;
311 enum devcheck device_check;
312 struct crypt_dm_active_device dmd = {
314 .uuid = crypt_get_uuid(cd),
317 .data_device = crypt_data_device(cd),
321 .offset = crypt_get_data_offset(cd),
322 .iv_offset = crypt_get_iv_offset(cd),
326 if (dmd.flags & CRYPT_ACTIVATE_SHARED)
327 device_check = DEV_SHARED;
329 device_check = DEV_EXCL;
331 r = device_block_adjust(cd, dmd.data_device, device_check,
332 dmd.u.crypt.offset, &dmd.size, &dmd.flags);
336 if (crypt_get_cipher_mode(cd))
337 r = asprintf(&dm_cipher, "%s-%s", crypt_get_cipher(cd), crypt_get_cipher_mode(cd));
339 r = asprintf(&dm_cipher, "%s", crypt_get_cipher(cd));
343 dmd.u.crypt.cipher = dm_cipher;
344 log_dbg("Trying to activate PLAIN device %s using cipher %s.",
345 name, dmd.u.crypt.cipher);
347 r = dm_create_device(cd, name, CRYPT_PLAIN, &dmd, 0);
350 if (!cd->plain_uuid && dm_query_device(cd, name, DM_ACTIVE_UUID, &dmd) >= 0)
351 cd->plain_uuid = CONST_CAST(char*)dmd.uuid;
357 int crypt_confirm(struct crypt_device *cd, const char *msg)
359 if (!cd || !cd->confirm)
362 return cd->confirm(msg, cd->confirm_usrptr);
365 static int key_from_terminal(struct crypt_device *cd, char *msg, char **key,
366 size_t *key_len, int force_verify)
368 char *prompt = NULL, *device_name;
373 if (crypt_loop_device(crypt_get_device_name(cd)))
374 device_name = crypt_loop_backing_file(crypt_get_device_name(cd));
376 device_name = strdup(crypt_get_device_name(cd));
379 r = asprintf(&prompt, _("Enter passphrase for %s: "), device_name);
387 *key = crypt_safe_alloc(DEFAULT_PASSPHRASE_SIZE_MAX);
392 r = cd->password(msg, *key, DEFAULT_PASSPHRASE_SIZE_MAX,
393 cd->password_usrptr);
395 crypt_safe_free(*key);
400 r = crypt_get_key(msg, key, key_len, 0, 0, NULL, cd->timeout,
401 (force_verify || cd->password_verify), cd);
404 return (r < 0) ? r: 0;
407 static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslot,
408 struct volume_key **vk)
410 char *passphrase_read = NULL;
411 size_t passphrase_size_read;
412 int r = -EINVAL, eperm = 0, tries = cd->tries;
416 crypt_free_volume_key(*vk);
419 r = key_from_terminal(cd, NULL, &passphrase_read,
420 &passphrase_size_read, 0);
421 /* Continue if it is just passphrase verify mismatch */
427 r = LUKS_open_key_with_hdr(keyslot, passphrase_read,
428 passphrase_size_read, &cd->hdr, vk, cd);
431 crypt_safe_free(passphrase_read);
432 passphrase_read = NULL;
433 } while (r == -EPERM && (--tries > 0));
436 crypt_free_volume_key(*vk);
439 /* Report wrong passphrase if at least one try failed */
440 if (eperm && r == -EPIPE)
444 crypt_safe_free(passphrase_read);
448 static int key_from_file(struct crypt_device *cd, char *msg,
449 char **key, size_t *key_len,
450 const char *key_file, size_t key_offset,
453 return crypt_get_key(msg, key, key_len, key_offset, key_size, key_file,
457 void crypt_set_log_callback(struct crypt_device *cd,
458 void (*log)(int level, const char *msg, void *usrptr),
465 cd->log_usrptr = usrptr;
469 void crypt_set_confirm_callback(struct crypt_device *cd,
470 int (*confirm)(const char *msg, void *usrptr),
473 cd->confirm = confirm;
474 cd->confirm_usrptr = usrptr;
477 void crypt_set_password_callback(struct crypt_device *cd,
478 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
481 cd->password = password;
482 cd->password_usrptr = usrptr;
485 static void _get_error(char *error, char *buf, size_t size)
487 if (!buf || size < 1)
490 strncpy(buf, error, size - 1);
491 buf[size - 1] = '\0';
497 void crypt_last_error(struct crypt_device *cd, char *buf, size_t size)
500 return _get_error(cd->error, buf, size);
503 /* Deprecated global error interface */
504 void crypt_get_error(char *buf, size_t size)
506 return _get_error(global_error, buf, size);
509 const char *crypt_get_dir(void)
514 int crypt_init(struct crypt_device **cd, const char *device)
516 struct crypt_device *h = NULL;
522 log_dbg("Allocating crypt device %s context.", device);
524 if (!(h = malloc(sizeof(struct crypt_device))))
527 memset(h, 0, sizeof(*h));
529 r = device_alloc(&h->device, device);
535 h->iteration_time = 1000;
536 h->password_verify = 0;
538 h->rng_type = crypt_random_default_key_rng();
542 device_free(h->device);
547 static int crypt_check_data_device_size(struct crypt_device *cd)
550 uint64_t size, size_min;
552 /* Check data device size, require at least one sector */
553 size_min = crypt_get_data_offset(cd) << SECTOR_SHIFT ?: SECTOR_SIZE;
555 r = device_size(cd->device, &size);
559 if (size < size_min) {
560 log_err(cd, _("Header detected but device %s is too small.\n"),
561 device_path(cd->device));
568 int crypt_set_data_device(struct crypt_device *cd, const char *device)
570 struct device *dev = NULL;
573 log_dbg("Setting ciphertext data device to %s.", device ?: "(none)");
575 if (!isLUKS(cd->type) && !isVERITY(cd->type)) {
576 log_err(cd, _("This operation is not supported for this device type.\n"));
580 /* metadata device must be set */
581 if (!cd->device || !device)
584 r = device_alloc(&dev, device);
588 if (!cd->metadata_device) {
589 cd->metadata_device = cd->device;
591 device_free(cd->device);
595 return crypt_check_data_device_size(cd);
598 static int _crypt_load_luks1(struct crypt_device *cd, int require_header, int repair)
600 struct luks_phdr hdr;
607 r = LUKS_read_phdr(&hdr, require_header, repair, cd);
611 if (!cd->type && !(cd->type = strdup(CRYPT_LUKS1)))
614 memcpy(&cd->hdr, &hdr, sizeof(hdr));
619 static int _crypt_load_verity(struct crypt_device *cd, struct crypt_params_verity *params)
622 size_t sb_offset = 0;
628 if (params->flags & CRYPT_VERITY_NO_HEADER)
632 sb_offset = params->hash_area_offset;
634 r = VERITY_read_sb(cd, sb_offset, &cd->verity_uuid, &cd->verity_hdr);
639 cd->verity_hdr.flags = params->flags;
641 /* Hash availability checked in sb load */
642 cd->verity_root_hash_size = crypt_hash_size(cd->verity_hdr.hash_name);
643 if (cd->verity_root_hash_size > 4096)
646 if (!cd->type && !(cd->type = strdup(CRYPT_VERITY)))
649 if (params && params->data_device &&
650 (r = crypt_set_data_device(cd, params->data_device)) < 0)
656 static int _init_by_name_crypt(struct crypt_device *cd, const char *name)
658 struct crypt_dm_active_device dmd = {};
659 char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
662 r = dm_query_device(cd, name,
665 DM_ACTIVE_CRYPT_CIPHER |
666 DM_ACTIVE_CRYPT_KEYSIZE, &dmd);
670 if (isPLAIN(cd->type)) {
671 cd->plain_uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
672 cd->plain_hdr.hash = NULL; /* no way to get this */
673 cd->plain_hdr.offset = dmd.u.crypt.offset;
674 cd->plain_hdr.skip = dmd.u.crypt.iv_offset;
675 cd->plain_key_size = dmd.u.crypt.vk->keylength;
677 r = crypt_parse_name_and_mode(dmd.u.crypt.cipher, cipher, NULL, cipher_mode);
679 cd->plain_cipher = strdup(cipher);
680 cd->plain_cipher_mode = strdup(cipher_mode);
682 } else if (isLOOPAES(cd->type)) {
683 cd->loopaes_uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
684 cd->loopaes_hdr.offset = dmd.u.crypt.offset;
686 r = crypt_parse_name_and_mode(dmd.u.crypt.cipher, cipher,
687 &key_nums, cipher_mode);
689 cd->loopaes_cipher = strdup(cipher);
690 cd->loopaes_cipher_mode = strdup(cipher_mode);
691 /* version 3 uses last key for IV */
692 if (dmd.u.crypt.vk->keylength % key_nums)
694 cd->loopaes_key_size = dmd.u.crypt.vk->keylength / key_nums;
696 } else if (isLUKS(cd->type)) {
697 if (crypt_metadata_device(cd)) {
698 r = _crypt_load_luks1(cd, 0, 0);
700 log_dbg("LUKS device header does not match active device.");
706 /* check whether UUIDs match each other */
707 r = crypt_uuid_cmp(dmd.uuid, cd->hdr.uuid);
709 log_dbg("LUKS device header uuid: %s mismatches DM returned uuid %s",
710 cd->hdr.uuid, dmd.uuid);
719 crypt_free_volume_key(dmd.u.crypt.vk);
720 device_free(dmd.data_device);
721 free(CONST_CAST(void*)dmd.u.crypt.cipher);
722 free(CONST_CAST(void*)dmd.uuid);
726 static int _init_by_name_verity(struct crypt_device *cd, const char *name)
728 struct crypt_params_verity params = {};
729 struct crypt_dm_active_device dmd = {
731 .u.verity.vp = ¶ms,
735 r = dm_query_device(cd, name,
738 DM_ACTIVE_VERITY_HASH_DEVICE |
739 DM_ACTIVE_VERITY_PARAMS, &dmd);
743 if (isVERITY(cd->type)) {
744 cd->verity_uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
745 cd->verity_hdr.flags = CRYPT_VERITY_NO_HEADER; //FIXME
746 cd->verity_hdr.data_size = params.data_size;
747 cd->verity_root_hash_size = dmd.u.verity.root_hash_size;
748 cd->verity_root_hash = NULL;
749 cd->verity_hdr.hash_name = params.hash_name;
750 cd->verity_hdr.data_device = NULL;
751 cd->verity_hdr.hash_device = NULL;
752 cd->verity_hdr.data_block_size = params.data_block_size;
753 cd->verity_hdr.hash_block_size = params.hash_block_size;
754 cd->verity_hdr.hash_area_offset = dmd.u.verity.hash_offset;
755 cd->verity_hdr.hash_type = params.hash_type;
756 cd->verity_hdr.flags = params.flags;
757 cd->verity_hdr.salt_size = params.salt_size;
758 cd->verity_hdr.salt = params.salt;
759 cd->metadata_device = dmd.u.verity.hash_device;
762 device_free(dmd.data_device);
763 free(CONST_CAST(void*)dmd.uuid);
767 int crypt_init_by_name_and_header(struct crypt_device **cd,
769 const char *header_device)
771 crypt_status_info ci;
772 struct crypt_dm_active_device dmd;
775 log_dbg("Allocating crypt device context by device %s.", name);
777 ci = crypt_status(NULL, name);
778 if (ci == CRYPT_INVALID)
781 if (ci < CRYPT_ACTIVE) {
782 log_err(NULL, _("Device %s is not active.\n"), name);
786 r = dm_query_device(NULL, name, DM_ACTIVE_DEVICE | DM_ACTIVE_UUID, &dmd);
793 r = crypt_init(cd, header_device);
795 r = crypt_init(cd, device_path(dmd.data_device));
797 /* Underlying device disappeared but mapping still active */
798 if (!dmd.data_device || r == -ENOTBLK)
799 log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
802 /* Underlying device is not readable but crypt mapping exists */
804 device_free(dmd.data_device);
805 dmd.data_device = NULL;
806 r = crypt_init(cd, NULL);
814 if (!strncmp(CRYPT_PLAIN, dmd.uuid, sizeof(CRYPT_PLAIN)-1))
815 (*cd)->type = strdup(CRYPT_PLAIN);
816 else if (!strncmp(CRYPT_LOOPAES, dmd.uuid, sizeof(CRYPT_LOOPAES)-1))
817 (*cd)->type = strdup(CRYPT_LOOPAES);
818 else if (!strncmp(CRYPT_LUKS1, dmd.uuid, sizeof(CRYPT_LUKS1)-1))
819 (*cd)->type = strdup(CRYPT_LUKS1);
820 else if (!strncmp(CRYPT_VERITY, dmd.uuid, sizeof(CRYPT_VERITY)-1))
821 (*cd)->type = strdup(CRYPT_VERITY);
823 log_dbg("Unknown UUID set, some parameters are not set.");
825 log_dbg("Active device has no UUID set, some parameters are not set.");
828 r = crypt_set_data_device(*cd, device_path(dmd.data_device));
833 /* Try to initialise basic parameters from active device */
835 if (dmd.target == DM_CRYPT)
836 r = _init_by_name_crypt(*cd, name);
837 else if (dmd.target == DM_VERITY)
838 r = _init_by_name_verity(*cd, name);
844 device_free(dmd.data_device);
845 free(CONST_CAST(void*)dmd.uuid);
849 int crypt_init_by_name(struct crypt_device **cd, const char *name)
851 return crypt_init_by_name_and_header(cd, name, NULL);
854 static int _crypt_format_plain(struct crypt_device *cd,
856 const char *cipher_mode,
858 size_t volume_key_size,
859 struct crypt_params_plain *params)
861 if (!cipher || !cipher_mode) {
862 log_err(cd, _("Invalid plain crypt parameters.\n"));
866 if (volume_key_size > 1024) {
867 log_err(cd, _("Invalid key size.\n"));
871 if (!(cd->type = strdup(CRYPT_PLAIN)))
874 cd->plain_key_size = volume_key_size;
875 cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
879 cd->plain_cipher = strdup(cipher);
880 cd->plain_cipher_mode = strdup(cipher_mode);
883 cd->plain_uuid = strdup(uuid);
885 if (params && params->hash)
886 cd->plain_hdr.hash = strdup(params->hash);
888 cd->plain_hdr.offset = params ? params->offset : 0;
889 cd->plain_hdr.skip = params ? params->skip : 0;
890 cd->plain_hdr.size = params ? params->size : 0;
892 if (!cd->plain_cipher || !cd->plain_cipher_mode)
898 static int _crypt_format_luks1(struct crypt_device *cd,
900 const char *cipher_mode,
902 const char *volume_key,
903 size_t volume_key_size,
904 struct crypt_params_luks1 *params)
907 unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
908 unsigned long alignment_offset = 0;
910 if (!crypt_metadata_device(cd)) {
911 log_err(cd, _("Can't format LUKS without device.\n"));
915 if (!(cd->type = strdup(CRYPT_LUKS1)))
919 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
922 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
927 if (params && params->data_device) {
928 cd->metadata_device = cd->device;
930 if (device_alloc(&cd->device, params->data_device) < 0)
932 required_alignment = params->data_alignment * SECTOR_SIZE;
933 } else if (params && params->data_alignment) {
934 required_alignment = params->data_alignment * SECTOR_SIZE;
936 device_topology_alignment(cd->device,
938 &alignment_offset, DEFAULT_DISK_ALIGNMENT);
940 /* Check early if we cannot allocate block device for key slot access */
941 r = device_block_adjust(cd, cd->device, DEV_OK, 0, NULL, NULL);
945 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
946 (params && params->hash) ? params->hash : "sha1",
948 required_alignment / SECTOR_SIZE,
949 alignment_offset / SECTOR_SIZE,
950 cd->iteration_time, &cd->PBKDF2_per_sec,
951 cd->metadata_device ? 1 : 0, cd);
955 /* Wipe first 8 sectors - fs magic numbers etc. */
956 r = crypt_wipe(crypt_metadata_device(cd), 0, 8 * SECTOR_SIZE, CRYPT_WIPE_ZERO, 1);
959 log_err(cd, _("Cannot format device %s which is still in use.\n"),
960 mdata_device_path(cd));
961 else if (r == -EACCES) {
962 log_err(cd, _("Cannot format device %s, permission denied.\n"),
963 mdata_device_path(cd));
966 log_err(cd, _("Cannot wipe header on device %s.\n"),
967 mdata_device_path(cd));
972 r = LUKS_write_phdr(&cd->hdr, cd);
977 static int _crypt_format_loopaes(struct crypt_device *cd,
980 size_t volume_key_size,
981 struct crypt_params_loopaes *params)
983 if (!crypt_metadata_device(cd)) {
984 log_err(cd, _("Can't format LOOPAES without device.\n"));
988 if (volume_key_size > 1024) {
989 log_err(cd, _("Invalid key size.\n"));
993 if (!(cd->type = strdup(CRYPT_LOOPAES)))
996 cd->loopaes_key_size = volume_key_size;
998 cd->loopaes_cipher = strdup(cipher ?: DEFAULT_LOOPAES_CIPHER);
1001 cd->loopaes_uuid = strdup(uuid);
1003 if (params && params->hash)
1004 cd->loopaes_hdr.hash = strdup(params->hash);
1006 cd->loopaes_hdr.offset = params ? params->offset : 0;
1007 cd->loopaes_hdr.skip = params ? params->skip : 0;
1012 static int _crypt_format_verity(struct crypt_device *cd,
1014 struct crypt_params_verity *params)
1016 int r = 0, hash_size;
1017 uint64_t data_device_size;
1019 if (!crypt_metadata_device(cd)) {
1020 log_err(cd, _("Can't format VERITY without device.\n"));
1024 if (!params || !params->data_device)
1027 if (params->hash_type > VERITY_MAX_HASH_TYPE) {
1028 log_err(cd, _("Unsupported VERITY hash type %d.\n"), params->hash_type);
1032 if (VERITY_BLOCK_SIZE_OK(params->data_block_size) ||
1033 VERITY_BLOCK_SIZE_OK(params->hash_block_size)) {
1034 log_err(cd, _("Unsupported VERITY block size.\n"));
1038 if (params->hash_area_offset % 512) {
1039 log_err(cd, _("Unsupported VERITY hash offset.\n"));
1043 if (!(cd->type = strdup(CRYPT_VERITY)))
1046 r = crypt_set_data_device(cd, params->data_device);
1049 if (!params->data_size) {
1050 r = device_size(cd->device, &data_device_size);
1054 cd->verity_hdr.data_size = data_device_size / params->data_block_size;
1056 cd->verity_hdr.data_size = params->data_size;
1058 hash_size = crypt_hash_size(params->hash_name);
1059 if (hash_size <= 0) {
1060 log_err(cd, _("Hash algorithm %s not supported.\n"),
1064 cd->verity_root_hash_size = hash_size;
1066 cd->verity_root_hash = malloc(cd->verity_root_hash_size);
1067 if (!cd->verity_root_hash)
1070 cd->verity_hdr.flags = params->flags;
1071 cd->verity_hdr.hash_name = strdup(params->hash_name);
1072 cd->verity_hdr.data_device = NULL;
1073 cd->verity_hdr.data_block_size = params->data_block_size;
1074 cd->verity_hdr.hash_block_size = params->hash_block_size;
1075 cd->verity_hdr.hash_area_offset = params->hash_area_offset;
1076 cd->verity_hdr.hash_type = params->hash_type;
1077 cd->verity_hdr.flags = params->flags;
1078 cd->verity_hdr.salt_size = params->salt_size;
1079 cd->verity_hdr.salt = malloc(params->salt_size);
1081 memcpy(CONST_CAST(char*)cd->verity_hdr.salt, params->salt,
1084 r = crypt_random_get(cd, CONST_CAST(char*)cd->verity_hdr.salt,
1085 params->salt_size, CRYPT_RND_SALT);
1089 if (params->flags & CRYPT_VERITY_CREATE_HASH) {
1090 r = VERITY_create(cd, &cd->verity_hdr,
1091 cd->verity_root_hash, cd->verity_root_hash_size);
1096 if (!(params->flags & CRYPT_VERITY_NO_HEADER)) {
1098 cd->verity_uuid = strdup(uuid);
1100 r = VERITY_UUID_generate(cd, &cd->verity_uuid);
1105 r = VERITY_write_sb(cd, cd->verity_hdr.hash_area_offset,
1112 int crypt_format(struct crypt_device *cd,
1115 const char *cipher_mode,
1117 const char *volume_key,
1118 size_t volume_key_size,
1127 log_dbg("Context already formatted as %s.", cd->type);
1131 log_dbg("Formatting device %s as type %s.", mdata_device_path(cd) ?: "(none)", type);
1133 r = init_crypto(cd);
1138 r = _crypt_format_plain(cd, cipher, cipher_mode,
1139 uuid, volume_key_size, params);
1140 else if (isLUKS(type))
1141 r = _crypt_format_luks1(cd, cipher, cipher_mode,
1142 uuid, volume_key, volume_key_size, params);
1143 else if (isLOOPAES(type))
1144 r = _crypt_format_loopaes(cd, cipher, uuid, volume_key_size, params);
1145 else if (isVERITY(type))
1146 r = _crypt_format_verity(cd, uuid, params);
1148 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
1155 crypt_free_volume_key(cd->volume_key);
1156 cd->volume_key = NULL;
1162 int crypt_load(struct crypt_device *cd,
1163 const char *requested_type,
1168 log_dbg("Trying to load %s crypt type from device %s.",
1169 requested_type ?: "any", mdata_device_path(cd) ?: "(none)");
1171 if (!crypt_metadata_device(cd))
1174 if (!requested_type || isLUKS(requested_type)) {
1175 if (cd->type && !isLUKS(cd->type)) {
1176 log_dbg("Context is already initialised to type %s", cd->type);
1180 r = _crypt_load_luks1(cd, 1, 0);
1181 } else if (isVERITY(requested_type)) {
1182 if (cd->type && !isVERITY(cd->type)) {
1183 log_dbg("Context is already initialised to type %s", cd->type);
1186 r = _crypt_load_verity(cd, params);
1193 int crypt_repair(struct crypt_device *cd,
1194 const char *requested_type,
1195 void *params __attribute__((unused)))
1199 log_dbg("Trying to repair %s crypt type from device %s.",
1200 requested_type ?: "any", mdata_device_path(cd) ?: "(none)");
1202 if (!crypt_metadata_device(cd))
1205 if (requested_type && !isLUKS(requested_type))
1209 /* Load with repair */
1210 r = _crypt_load_luks1(cd, 1, 1);
1214 /* cd->type and header must be set in context */
1215 r = crypt_check_data_device_size(cd);
1224 int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
1226 struct crypt_dm_active_device dmd;
1229 /* Device context type must be initialised */
1230 if (!cd->type || !crypt_get_uuid(cd))
1233 log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
1235 r = dm_query_device(cd, name, DM_ACTIVE_DEVICE | DM_ACTIVE_CRYPT_CIPHER |
1236 DM_ACTIVE_UUID | DM_ACTIVE_CRYPT_KEYSIZE |
1237 DM_ACTIVE_CRYPT_KEY, &dmd);
1239 log_err(NULL, _("Device %s is not active.\n"), name);
1243 if (!dmd.uuid || dmd.target != DM_CRYPT) {
1248 r = device_block_adjust(cd, dmd.data_device, DEV_OK,
1249 dmd.u.crypt.offset, &new_size, &dmd.flags);
1253 if (new_size == dmd.size) {
1254 log_dbg("Device has already requested size %" PRIu64
1255 " sectors.", dmd.size);
1258 dmd.size = new_size;
1259 r = dm_create_device(cd, name, cd->type, &dmd, 1);
1262 if (dmd.target == DM_CRYPT) {
1263 crypt_free_volume_key(dmd.u.crypt.vk);
1264 free(CONST_CAST(void*)dmd.u.crypt.cipher);
1266 free(CONST_CAST(void*)dmd.data_device);
1267 free(CONST_CAST(void*)dmd.uuid);
1272 int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
1274 if (!isLUKS(cd->type)) {
1275 log_err(cd, _("This operation is not supported for this device type.\n"));
1279 if (uuid && !strncmp(uuid, cd->hdr.uuid, sizeof(cd->hdr.uuid))) {
1280 log_dbg("UUID is the same as requested (%s) for device %s.",
1281 uuid, mdata_device_path(cd));
1286 log_dbg("Requested new UUID change to %s for %s.", uuid, mdata_device_path(cd));
1288 log_dbg("Requested new UUID refresh for %s.", mdata_device_path(cd));
1290 if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
1293 return LUKS_hdr_uuid_set(&cd->hdr, uuid, cd);
1296 int crypt_header_backup(struct crypt_device *cd,
1297 const char *requested_type,
1298 const char *backup_file)
1302 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1305 r = init_crypto(cd);
1309 log_dbg("Requested header backup of device %s (%s) to "
1310 "file %s.", mdata_device_path(cd), requested_type, backup_file);
1312 return LUKS_hdr_backup(backup_file, &cd->hdr, cd);
1315 int crypt_header_restore(struct crypt_device *cd,
1316 const char *requested_type,
1317 const char *backup_file)
1321 if (requested_type && !isLUKS(requested_type))
1324 r = init_crypto(cd);
1328 log_dbg("Requested header restore to device %s (%s) from "
1329 "file %s.", mdata_device_path(cd), requested_type, backup_file);
1331 return LUKS_hdr_restore(backup_file, &cd->hdr, cd);
1334 void crypt_free(struct crypt_device *cd)
1337 log_dbg("Releasing crypt device %s context.", mdata_device_path(cd));
1340 crypt_free_volume_key(cd->volume_key);
1342 device_free(cd->device);
1343 device_free(cd->metadata_device);
1346 /* used in plain device only */
1347 free(CONST_CAST(void*)cd->plain_hdr.hash);
1348 free(cd->plain_cipher);
1349 free(cd->plain_cipher_mode);
1350 free(cd->plain_uuid);
1352 /* used in loop-AES device only */
1353 free(CONST_CAST(void*)cd->loopaes_hdr.hash);
1354 free(cd->loopaes_cipher);
1355 free(cd->loopaes_uuid);
1357 /* used in verity device only */
1358 free(CONST_CAST(void*)cd->verity_hdr.hash_name);
1359 free(CONST_CAST(void*)cd->verity_hdr.salt);
1360 free(cd->verity_root_hash);
1361 free(cd->verity_uuid);
1367 int crypt_suspend(struct crypt_device *cd,
1370 crypt_status_info ci;
1373 log_dbg("Suspending volume %s.", name);
1375 if (!isLUKS(cd->type)) {
1376 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1381 ci = crypt_status(NULL, name);
1382 if (ci < CRYPT_ACTIVE) {
1383 log_err(cd, _("Volume %s is not active.\n"), name);
1390 r = dm_status_suspended(cd, name);
1395 log_err(cd, _("Volume %s is already suspended.\n"), name);
1400 r = dm_suspend_and_wipe_key(cd, name);
1402 log_err(cd, "Suspend is not supported for device %s.\n", name);
1404 log_err(cd, "Error during suspending device %s.\n", name);
1411 int crypt_resume_by_passphrase(struct crypt_device *cd,
1414 const char *passphrase,
1415 size_t passphrase_size)
1417 struct volume_key *vk = NULL;
1420 log_dbg("Resuming volume %s.", name);
1422 if (!isLUKS(cd->type)) {
1423 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1428 r = dm_status_suspended(cd, name);
1433 log_err(cd, _("Volume %s is not suspended.\n"), name);
1438 r = LUKS_open_key_with_hdr(keyslot, passphrase, passphrase_size,
1441 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1445 r = dm_resume_and_reinstate_key(cd, name, vk->keylength, vk->key);
1447 log_err(cd, "Resume is not supported for device %s.\n", name);
1449 log_err(cd, "Error during resuming device %s.\n", name);
1453 crypt_free_volume_key(vk);
1454 return r < 0 ? r : keyslot;
1457 int crypt_resume_by_keyfile_offset(struct crypt_device *cd,
1460 const char *keyfile,
1461 size_t keyfile_size,
1462 size_t keyfile_offset)
1464 struct volume_key *vk = NULL;
1465 char *passphrase_read = NULL;
1466 size_t passphrase_size_read;
1469 log_dbg("Resuming volume %s.", name);
1471 if (!isLUKS(cd->type)) {
1472 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1477 r = dm_status_suspended(cd, name);
1482 log_err(cd, _("Volume %s is not suspended.\n"), name);
1489 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1490 &passphrase_size_read, keyfile, keyfile_offset,
1495 r = LUKS_open_key_with_hdr(keyslot, passphrase_read,
1496 passphrase_size_read, &cd->hdr, &vk, cd);
1501 r = dm_resume_and_reinstate_key(cd, name, vk->keylength, vk->key);
1503 log_err(cd, "Error during resuming device %s.\n", name);
1505 crypt_safe_free(passphrase_read);
1506 crypt_free_volume_key(vk);
1507 return r < 0 ? r : keyslot;
1510 int crypt_resume_by_keyfile(struct crypt_device *cd,
1513 const char *keyfile,
1514 size_t keyfile_size)
1516 return crypt_resume_by_keyfile_offset(cd, name, keyslot,
1517 keyfile, keyfile_size, 0);
1520 // slot manipulation
1521 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1522 int keyslot, // -1 any
1523 const char *passphrase, // NULL -> terminal
1524 size_t passphrase_size,
1525 const char *new_passphrase, // NULL -> terminal
1526 size_t new_passphrase_size)
1528 struct volume_key *vk = NULL;
1529 char *password = NULL, *new_password = NULL;
1530 size_t passwordLen, new_passwordLen;
1533 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1534 "new passphrase %sprovided.",
1535 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1537 if (!isLUKS(cd->type)) {
1538 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1542 r = keyslot_verify_or_find_empty(cd, &keyslot);
1546 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1547 /* No slots used, try to use pre-generated key in header */
1548 if (cd->volume_key) {
1549 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1550 r = vk ? 0 : -ENOMEM;
1552 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1555 } else if (passphrase) {
1556 /* Passphrase provided, use it to unlock existing keyslot */
1557 r = LUKS_open_key_with_hdr(CRYPT_ANY_SLOT, passphrase,
1558 passphrase_size, &cd->hdr, &vk, cd);
1560 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1561 r = key_from_terminal(cd, _("Enter any passphrase: "),
1562 &password, &passwordLen, 0);
1566 r = LUKS_open_key_with_hdr(CRYPT_ANY_SLOT, password,
1567 passwordLen, &cd->hdr, &vk, cd);
1568 crypt_safe_free(password);
1574 if (new_passphrase) {
1575 new_password = CONST_CAST(char*)new_passphrase;
1576 new_passwordLen = new_passphrase_size;
1578 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1579 &new_password, &new_passwordLen, 1);
1584 r = LUKS_set_key(keyslot, new_password, new_passwordLen,
1585 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1590 if (!new_passphrase)
1591 crypt_safe_free(new_password);
1592 crypt_free_volume_key(vk);
1593 return r ?: keyslot;
1596 int crypt_keyslot_add_by_keyfile_offset(struct crypt_device *cd,
1598 const char *keyfile,
1599 size_t keyfile_size,
1600 size_t keyfile_offset,
1601 const char *new_keyfile,
1602 size_t new_keyfile_size,
1603 size_t new_keyfile_offset)
1605 struct volume_key *vk = NULL;
1606 char *password = NULL; size_t passwordLen;
1607 char *new_password = NULL; size_t new_passwordLen;
1610 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1611 keyfile ?: "[none]", new_keyfile ?: "[none]");
1613 if (!isLUKS(cd->type)) {
1614 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1618 r = keyslot_verify_or_find_empty(cd, &keyslot);
1622 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1623 /* No slots used, try to use pre-generated key in header */
1624 if (cd->volume_key) {
1625 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1626 r = vk ? 0 : -ENOMEM;
1628 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1632 /* Read password from file of (if NULL) from terminal */
1634 r = key_from_file(cd, _("Enter any passphrase: "),
1635 &password, &passwordLen,
1636 keyfile, keyfile_offset, keyfile_size);
1638 r = key_from_terminal(cd, _("Enter any passphrase: "),
1639 &password, &passwordLen, 0);
1643 r = LUKS_open_key_with_hdr(CRYPT_ANY_SLOT, password, passwordLen,
1651 r = key_from_file(cd, _("Enter new passphrase for key slot: "),
1652 &new_password, &new_passwordLen, new_keyfile,
1653 new_keyfile_offset, new_keyfile_size);
1655 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1656 &new_password, &new_passwordLen, 1);
1660 r = LUKS_set_key(keyslot, new_password, new_passwordLen,
1661 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1663 crypt_safe_free(password);
1664 crypt_safe_free(new_password);
1665 crypt_free_volume_key(vk);
1666 return r < 0 ? r : keyslot;
1669 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1671 const char *keyfile,
1672 size_t keyfile_size,
1673 const char *new_keyfile,
1674 size_t new_keyfile_size)
1676 return crypt_keyslot_add_by_keyfile_offset(cd, keyslot,
1677 keyfile, keyfile_size, 0,
1678 new_keyfile, new_keyfile_size, 0);
1681 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1683 const char *volume_key,
1684 size_t volume_key_size,
1685 const char *passphrase,
1686 size_t passphrase_size)
1688 struct volume_key *vk = NULL;
1690 char *new_password = NULL; size_t new_passwordLen;
1692 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1694 if (!isLUKS(cd->type)) {
1695 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1700 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1701 else if (cd->volume_key)
1702 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1707 r = LUKS_verify_volume_key(&cd->hdr, vk);
1709 log_err(cd, _("Volume key does not match the volume.\n"));
1713 r = keyslot_verify_or_find_empty(cd, &keyslot);
1718 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1719 &new_password, &new_passwordLen, 1);
1722 passphrase = new_password;
1723 passphrase_size = new_passwordLen;
1726 r = LUKS_set_key(keyslot, passphrase, passphrase_size,
1727 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1729 crypt_safe_free(new_password);
1730 crypt_free_volume_key(vk);
1731 return (r < 0) ? r : keyslot;
1734 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1736 crypt_keyslot_info ki;
1738 log_dbg("Destroying keyslot %d.", keyslot);
1740 if (!isLUKS(cd->type)) {
1741 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1745 ki = crypt_keyslot_status(cd, keyslot);
1746 if (ki == CRYPT_SLOT_INVALID) {
1747 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1751 if (ki == CRYPT_SLOT_INACTIVE) {
1752 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1756 return LUKS_del_key(keyslot, &cd->hdr, cd);
1759 // activation/deactivation of device mapping
1760 int crypt_activate_by_passphrase(struct crypt_device *cd,
1763 const char *passphrase,
1764 size_t passphrase_size,
1767 crypt_status_info ci;
1768 struct volume_key *vk = NULL;
1769 char *read_passphrase = NULL;
1770 size_t passphraseLen = 0;
1773 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1774 name ? "Activating" : "Checking", name ?: "",
1775 keyslot, passphrase ? "" : "[none] ");
1778 ci = crypt_status(NULL, name);
1779 if (ci == CRYPT_INVALID)
1781 else if (ci >= CRYPT_ACTIVE) {
1782 log_err(cd, _("Device %s already exists.\n"), name);
1787 /* plain, use hashed passphrase */
1788 if (isPLAIN(cd->type)) {
1793 r = key_from_terminal(cd, NULL, &read_passphrase,
1797 passphrase = read_passphrase;
1798 passphrase_size = passphraseLen;
1801 r = process_key(cd, cd->plain_hdr.hash,
1803 passphrase, passphrase_size, &vk);
1807 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1809 } else if (isLUKS(cd->type)) {
1810 /* provided passphrase, do not retry */
1812 r = LUKS_open_key_with_hdr(keyslot, passphrase,
1813 passphrase_size, &cd->hdr, &vk, cd);
1815 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1820 r = LUKS1_activate(cd, name, vk, flags);
1825 crypt_safe_free(read_passphrase);
1826 crypt_free_volume_key(vk);
1828 return r < 0 ? r : keyslot;
1831 int crypt_activate_by_keyfile_offset(struct crypt_device *cd,
1834 const char *keyfile,
1835 size_t keyfile_size,
1836 size_t keyfile_offset,
1839 crypt_status_info ci;
1840 struct volume_key *vk = NULL;
1841 char *passphrase_read = NULL;
1842 size_t passphrase_size_read;
1843 unsigned int key_count = 0;
1846 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1847 name ?: "", keyslot, keyfile ?: "[none]");
1850 ci = crypt_status(NULL, name);
1851 if (ci == CRYPT_INVALID)
1853 else if (ci >= CRYPT_ACTIVE) {
1854 log_err(cd, _("Device %s already exists.\n"), name);
1862 if (isPLAIN(cd->type)) {
1866 r = key_from_file(cd, _("Enter passphrase: "),
1867 &passphrase_read, &passphrase_size_read,
1868 keyfile, keyfile_offset, keyfile_size);
1872 r = process_key(cd, cd->plain_hdr.hash,
1874 passphrase_read, passphrase_size_read, &vk);
1878 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1879 } else if (isLUKS(cd->type)) {
1880 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1881 &passphrase_size_read, keyfile, keyfile_offset, keyfile_size);
1884 r = LUKS_open_key_with_hdr(keyslot, passphrase_read,
1885 passphrase_size_read, &cd->hdr, &vk, cd);
1891 r = LUKS1_activate(cd, name, vk, flags);
1896 } else if (isLOOPAES(cd->type)) {
1897 r = key_from_file(cd, NULL, &passphrase_read, &passphrase_size_read,
1898 keyfile, keyfile_offset, keyfile_size);
1901 r = LOOPAES_parse_keyfile(cd, &vk, cd->loopaes_hdr.hash, &key_count,
1902 passphrase_read, passphrase_size_read);
1906 r = LOOPAES_activate(cd, name, cd->loopaes_cipher,
1907 key_count, vk, flags);
1912 crypt_safe_free(passphrase_read);
1913 crypt_free_volume_key(vk);
1918 int crypt_activate_by_keyfile(struct crypt_device *cd,
1921 const char *keyfile,
1922 size_t keyfile_size,
1925 return crypt_activate_by_keyfile_offset(cd, name, keyslot, keyfile,
1926 keyfile_size, 0, flags);
1929 int crypt_activate_by_volume_key(struct crypt_device *cd,
1931 const char *volume_key,
1932 size_t volume_key_size,
1935 crypt_status_info ci;
1936 struct volume_key *vk = NULL;
1939 log_dbg("Activating volume %s by volume key.", name ?: "[none]");
1942 ci = crypt_status(NULL, name);
1943 if (ci == CRYPT_INVALID)
1945 else if (ci >= CRYPT_ACTIVE) {
1946 log_err(cd, _("Device %s already exists.\n"), name);
1951 /* use key directly, no hash */
1952 if (isPLAIN(cd->type)) {
1956 if (!volume_key || !volume_key_size || volume_key_size != cd->plain_key_size) {
1957 log_err(cd, _("Incorrect volume key specified for plain device.\n"));
1961 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1965 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1966 } else if (isLUKS(cd->type)) {
1967 /* If key is not provided, try to use internal key */
1969 if (!cd->volume_key) {
1970 log_err(cd, _("Volume key does not match the volume.\n"));
1973 volume_key_size = cd->volume_key->keylength;
1974 volume_key = cd->volume_key->key;
1977 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1980 r = LUKS_verify_volume_key(&cd->hdr, vk);
1983 log_err(cd, _("Volume key does not match the volume.\n"));
1986 r = LUKS1_activate(cd, name, vk, flags);
1987 } else if (isVERITY(cd->type)) {
1988 /* volume_key == root hash */
1989 if (!volume_key || !volume_key_size) {
1990 log_err(cd, _("Incorrect root hash specified for verity device.\n"));
1994 r = VERITY_activate(cd, name, volume_key, volume_key_size,
1995 &cd->verity_hdr, CRYPT_ACTIVATE_READONLY);
1998 free(cd->verity_root_hash);
1999 cd->verity_root_hash = NULL;
2001 cd->verity_root_hash_size = volume_key_size;
2002 if (!cd->verity_root_hash)
2003 cd->verity_root_hash = malloc(volume_key_size);
2004 if (cd->verity_root_hash)
2005 memcpy(cd->verity_root_hash, volume_key, volume_key_size);
2008 log_err(cd, _("Device type is not properly initialised.\n"));
2010 crypt_free_volume_key(vk);
2015 int crypt_deactivate(struct crypt_device *cd, const char *name)
2022 log_dbg("Deactivating volume %s.", name);
2027 switch (crypt_status(cd, name)) {
2030 r = dm_remove_device(cd, name, 0, 0);
2032 case CRYPT_INACTIVE:
2033 log_err(cd, _("Device %s is not active.\n"), name);
2037 log_err(cd, _("Invalid device %s.\n"), name);
2047 int crypt_volume_key_get(struct crypt_device *cd,
2050 size_t *volume_key_size,
2051 const char *passphrase,
2052 size_t passphrase_size)
2054 struct volume_key *vk = NULL;
2058 if (crypt_fips_mode()) {
2059 log_err(cd, "Function not available in FIPS mode.\n");
2063 key_len = crypt_get_volume_key_size(cd);
2064 if (key_len > *volume_key_size) {
2065 log_err(cd, _("Volume key buffer too small.\n"));
2069 if (isPLAIN(cd->type) && cd->plain_hdr.hash) {
2070 r = process_key(cd, cd->plain_hdr.hash, key_len,
2071 passphrase, passphrase_size, &vk);
2073 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
2074 } else if (isLUKS(cd->type)) {
2075 r = LUKS_open_key_with_hdr(keyslot, passphrase,
2076 passphrase_size, &cd->hdr, &vk, cd);
2079 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
2082 memcpy(volume_key, vk->key, vk->keylength);
2083 *volume_key_size = vk->keylength;
2086 crypt_free_volume_key(vk);
2090 int crypt_volume_key_verify(struct crypt_device *cd,
2091 const char *volume_key,
2092 size_t volume_key_size)
2094 struct volume_key *vk;
2097 if (!isLUKS(cd->type)) {
2098 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2102 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2106 r = LUKS_verify_volume_key(&cd->hdr, vk);
2109 log_err(cd, _("Volume key does not match the volume.\n"));
2111 crypt_free_volume_key(vk);
2116 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
2118 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
2119 cd->timeout = timeout_sec;
2122 void crypt_set_password_retry(struct crypt_device *cd, int tries)
2124 log_dbg("Password retry count set to %d.", tries);
2128 void crypt_set_iteration_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2130 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
2131 cd->iteration_time = iteration_time_ms;
2133 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2135 crypt_set_iteration_time(cd, iteration_time_ms);
2138 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
2140 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
2141 cd->password_verify = password_verify ? 1 : 0;
2144 void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
2147 case CRYPT_RNG_URANDOM:
2148 case CRYPT_RNG_RANDOM:
2149 log_dbg("RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
2150 cd->rng_type = rng_type;
2154 int crypt_get_rng_type(struct crypt_device *cd)
2159 return cd->rng_type;
2162 int crypt_memory_lock(struct crypt_device *cd, int lock)
2164 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
2168 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
2175 r = dm_status_device(cd, name);
2180 if (r < 0 && r != -ENODEV)
2181 return CRYPT_INVALID;
2184 return CRYPT_ACTIVE;
2189 return CRYPT_INACTIVE;
2192 static void hexprint(struct crypt_device *cd, const char *d, int n, const char *sep)
2195 for(i = 0; i < n; i++)
2196 log_std(cd, "%02hhx%s", (const char)d[i], sep);
2199 static int _luks_dump(struct crypt_device *cd)
2203 log_std(cd, "LUKS header information for %s\n\n", mdata_device_path(cd));
2204 log_std(cd, "Version: \t%d\n", cd->hdr.version);
2205 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
2206 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
2207 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec);
2208 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
2209 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
2210 log_std(cd, "MK digest: \t");
2211 hexprint(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE, " ");
2213 log_std(cd, "MK salt: \t");
2214 hexprint(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2, " ");
2215 log_std(cd, "\n \t");
2216 hexprint(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ");
2218 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
2219 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
2220 for(i = 0; i < LUKS_NUMKEYS; i++) {
2221 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
2222 log_std(cd, "Key Slot %d: ENABLED\n",i);
2223 log_std(cd, "\tIterations: \t%d\n",
2224 cd->hdr.keyblock[i].passwordIterations);
2225 log_std(cd, "\tSalt: \t");
2226 hexprint(cd, cd->hdr.keyblock[i].passwordSalt,
2227 LUKS_SALTSIZE/2, " ");
2228 log_std(cd, "\n\t \t");
2229 hexprint(cd, cd->hdr.keyblock[i].passwordSalt +
2230 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ");
2233 log_std(cd, "\tKey material offset:\t%d\n",
2234 cd->hdr.keyblock[i].keyMaterialOffset);
2235 log_std(cd, "\tAF stripes: \t%d\n",
2236 cd->hdr.keyblock[i].stripes);
2239 log_std(cd, "Key Slot %d: DISABLED\n", i);
2244 static int _verity_dump(struct crypt_device *cd)
2246 log_std(cd, "VERITY header information for %s\n", mdata_device_path(cd));
2247 log_std(cd, "UUID: \t%s\n", cd->verity_uuid ?: "");
2248 log_std(cd, "Hash type: \t%u\n", cd->verity_hdr.hash_type);
2249 log_std(cd, "Data blocks: \t%" PRIu64 "\n", cd->verity_hdr.data_size);
2250 log_std(cd, "Data block size: \t%u\n", cd->verity_hdr.data_block_size);
2251 log_std(cd, "Hash block size: \t%u\n", cd->verity_hdr.hash_block_size);
2252 log_std(cd, "Hash algorithm: \t%s\n", cd->verity_hdr.hash_name);
2253 log_std(cd, "Salt: \t");
2254 if (cd->verity_hdr.salt_size)
2255 hexprint(cd, cd->verity_hdr.salt, cd->verity_hdr.salt_size, "");
2259 if (cd->verity_root_hash) {
2260 log_std(cd, "Root hash: \t");
2261 hexprint(cd, cd->verity_root_hash, cd->verity_root_hash_size, "");
2267 int crypt_dump(struct crypt_device *cd)
2269 if (isLUKS(cd->type))
2270 return _luks_dump(cd);
2271 else if (isVERITY(cd->type))
2272 return _verity_dump(cd);
2274 log_err(cd, _("Dump operation is not supported for this device type.\n"));
2278 const char *crypt_get_cipher(struct crypt_device *cd)
2280 if (isPLAIN(cd->type))
2281 return cd->plain_cipher;
2283 if (isLUKS(cd->type))
2284 return cd->hdr.cipherName;
2286 if (isLOOPAES(cd->type))
2287 return cd->loopaes_cipher;
2292 const char *crypt_get_cipher_mode(struct crypt_device *cd)
2294 if (isPLAIN(cd->type))
2295 return cd->plain_cipher_mode;
2297 if (isLUKS(cd->type))
2298 return cd->hdr.cipherMode;
2300 if (isLOOPAES(cd->type))
2301 return cd->loopaes_cipher_mode;
2306 const char *crypt_get_uuid(struct crypt_device *cd)
2308 if (isLUKS(cd->type))
2309 return cd->hdr.uuid;
2311 if (isPLAIN(cd->type))
2312 return cd->plain_uuid;
2314 if (isLOOPAES(cd->type))
2315 return cd->loopaes_uuid;
2317 if (isVERITY(cd->type))
2318 return cd->verity_uuid;
2323 const char *crypt_get_device_name(struct crypt_device *cd)
2325 const char *path = device_block_path(cd->device);
2328 path = device_path(cd->device);
2333 int crypt_get_volume_key_size(struct crypt_device *cd)
2335 if (isPLAIN(cd->type))
2336 return cd->plain_key_size;
2338 if (isLUKS(cd->type))
2339 return cd->hdr.keyBytes;
2341 if (isLOOPAES(cd->type))
2342 return cd->loopaes_key_size;
2344 if (isVERITY(cd->type))
2345 return cd->verity_root_hash_size;
2350 uint64_t crypt_get_data_offset(struct crypt_device *cd)
2352 if (isPLAIN(cd->type))
2353 return cd->plain_hdr.offset;
2355 if (isLUKS(cd->type))
2356 return cd->hdr.payloadOffset;
2358 if (isLOOPAES(cd->type))
2359 return cd->loopaes_hdr.offset;
2364 uint64_t crypt_get_iv_offset(struct crypt_device *cd)
2366 if (isPLAIN(cd->type))
2367 return cd->plain_hdr.skip;
2369 if (isLUKS(cd->type))
2372 if (isLOOPAES(cd->type))
2373 return cd->loopaes_hdr.skip;
2378 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
2380 if (!isLUKS(cd->type)) {
2381 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2382 return CRYPT_SLOT_INVALID;
2385 return LUKS_keyslot_info(&cd->hdr, keyslot);
2388 int crypt_keyslot_max(const char *type)
2390 if (type && isLUKS(type))
2391 return LUKS_NUMKEYS;
2396 int crypt_keyslot_area(struct crypt_device *cd,
2401 if (!isLUKS(cd->type))
2404 return LUKS_keyslot_area(&cd->hdr, keyslot, offset, length);
2407 const char *crypt_get_type(struct crypt_device *cd)
2412 int crypt_get_verity_info(struct crypt_device *cd,
2413 struct crypt_params_verity *vp)
2415 if (!isVERITY(cd->type) || !vp)
2418 vp->data_device = device_path(cd->device);
2419 vp->hash_device = mdata_device_path(cd);
2420 vp->hash_name = cd->verity_hdr.hash_name;
2421 vp->salt = cd->verity_hdr.salt;
2422 vp->salt_size = cd->verity_hdr.salt_size;
2423 vp->data_block_size = cd->verity_hdr.data_block_size;
2424 vp->hash_block_size = cd->verity_hdr.hash_block_size;
2425 vp->data_size = cd->verity_hdr.data_size;
2426 vp->hash_area_offset = cd->verity_hdr.hash_area_offset;
2427 vp->hash_type = cd->verity_hdr.hash_type;
2428 vp->flags = cd->verity_hdr.flags & CRYPT_VERITY_NO_HEADER;
2432 int crypt_get_active_device(struct crypt_device *cd __attribute__((unused)),
2434 struct crypt_active_device *cad)
2436 struct crypt_dm_active_device dmd;
2439 r = dm_query_device(cd, name, 0, &dmd);
2443 if (dmd.target != DM_CRYPT && dmd.target != DM_VERITY)
2446 cad->offset = dmd.u.crypt.offset;
2447 cad->iv_offset = dmd.u.crypt.iv_offset;
2448 cad->size = dmd.size;
2449 cad->flags = dmd.flags;