2 * libcryptsetup - cryptsetup library
4 * Copyright (C) 2004, Jana Saout <jana@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-2014, 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.
28 #include <sys/utsname.h>
32 #include "libcryptsetup.h"
42 struct device *device;
43 struct device *metadata_device;
45 struct volume_key *volume_key;
47 uint64_t iteration_time;
52 // FIXME: private binary headers and access it properly
53 // through sub-library (LUKS1, TCRYPT)
56 struct { /* used in CRYPT_LUKS1 */
58 uint64_t PBKDF2_per_sec;
60 struct { /* used in CRYPT_PLAIN */
61 struct crypt_params_plain hdr;
64 unsigned int key_size;
66 struct { /* used in CRYPT_LOOPAES */
67 struct crypt_params_loopaes hdr;
70 unsigned int key_size;
72 struct { /* used in CRYPT_VERITY */
73 struct crypt_params_verity hdr;
75 unsigned int root_hash_size;
78 struct { /* used in CRYPT_TCRYPT */
79 struct crypt_params_tcrypt params;
80 struct tcrypt_phdr hdr;
82 struct { /* used if initialized without header by name */
84 /* buffers, must refresh from kernel on every query */
85 char cipher[MAX_CIPHER_LEN];
86 char cipher_mode[MAX_CIPHER_LEN];
87 unsigned int key_size;
91 /* callbacks definitions */
92 void (*log)(int level, const char *msg, void *usrptr);
94 int (*confirm)(const char *msg, void *usrptr);
96 int (*password)(const char *msg, char *buf, size_t length, void *usrptr);
97 void *password_usrptr;
99 /* last error message */
100 char error[MAX_ERROR_LENGTH];
103 /* Just to suppress redundant messages about crypto backend */
104 static int _crypto_logged = 0;
107 /* FIXME: not thread safe, remove this later */
108 static char global_error[MAX_ERROR_LENGTH] = {0};
111 static void (*_default_log)(int level, const char *msg, void *usrptr) = NULL;
112 static int _debug_level = 0;
114 void crypt_set_debug_level(int level)
116 _debug_level = level;
119 int crypt_get_debug_level(void)
124 static void crypt_set_error(struct crypt_device *cd, const char *error)
126 size_t size = strlen(error);
128 /* Set global error, ugly hack... */
129 strncpy(global_error, error, MAX_ERROR_LENGTH - 2);
130 if (size < MAX_ERROR_LENGTH && global_error[size - 1] == '\n')
131 global_error[size - 1] = '\0';
133 /* Set error string per context */
135 strncpy(cd->error, error, MAX_ERROR_LENGTH - 2);
136 if (size < MAX_ERROR_LENGTH && cd->error[size - 1] == '\n')
137 cd->error[size - 1] = '\0';
141 void crypt_log(struct crypt_device *cd, int level, const char *msg)
144 cd->log(level, msg, cd->log_usrptr);
145 else if (_default_log)
146 _default_log(level, msg, NULL);
148 if (level == CRYPT_LOG_ERROR)
149 crypt_set_error(cd, msg);
152 __attribute__((format(printf, 5, 6)))
153 void logger(struct crypt_device *cd, int level, const char *file,
154 int line, const char *format, ...)
159 va_start(argp, format);
161 if (vasprintf(&target, format, argp) > 0 ) {
163 crypt_log(cd, level, target);
165 } else if (_debug_level)
166 printf("# %s:%d %s\n", file ?: "?", line, target);
168 } else if (_debug_level)
169 printf("# %s\n", target);
177 static const char *mdata_device_path(struct crypt_device *cd)
179 return device_path(cd->metadata_device ?: cd->device);
183 struct device *crypt_metadata_device(struct crypt_device *cd)
185 return cd->metadata_device ?: cd->device;
188 struct device *crypt_data_device(struct crypt_device *cd)
193 int init_crypto(struct crypt_device *ctx)
198 r = crypt_random_init(ctx);
200 log_err(ctx, _("Cannot initialize crypto RNG backend.\n"));
204 r = crypt_backend_init(ctx);
206 log_err(ctx, _("Cannot initialize crypto backend.\n"));
208 if (!r && !_crypto_logged) {
209 log_dbg("Crypto backend (%s) initialized.", crypt_backend_version());
211 log_dbg("Detected kernel %s %s %s.",
212 uts.sysname, uts.release, uts.machine);
219 static int process_key(struct crypt_device *cd, const char *hash_name,
220 size_t key_size, const char *pass, size_t passLen,
221 struct volume_key **vk)
228 *vk = crypt_alloc_volume_key(key_size, NULL);
233 r = crypt_plain_hash(cd, hash_name, (*vk)->key, key_size, pass, passLen);
236 log_err(cd, _("Hash algorithm %s not supported.\n"),
239 log_err(cd, _("Key processing error (using hash %s).\n"),
241 crypt_free_volume_key(*vk);
245 } else if (passLen > key_size) {
246 memcpy((*vk)->key, pass, key_size);
248 memcpy((*vk)->key, pass, passLen);
254 static int isPLAIN(const char *type)
256 return (type && !strcmp(CRYPT_PLAIN, type));
259 static int isLUKS(const char *type)
261 return (type && !strcmp(CRYPT_LUKS1, type));
264 static int isLOOPAES(const char *type)
266 return (type && !strcmp(CRYPT_LOOPAES, type));
269 static int isVERITY(const char *type)
271 return (type && !strcmp(CRYPT_VERITY, type));
274 static int isTCRYPT(const char *type)
276 return (type && !strcmp(CRYPT_TCRYPT, type));
279 static int onlyLUKS(struct crypt_device *cd)
283 if (cd && !cd->type) {
284 log_err(cd, _("Cannot determine device type. Incompatible activation of device?\n"));
287 if (!cd || !isLUKS(cd->type)) {
288 log_err(cd, _("This operation is supported only for LUKS device.\n"));
295 static void crypt_set_null_type(struct crypt_device *cd)
302 cd->u.none.active_name = NULL;
305 static void crypt_reset_null_type(struct crypt_device *cd)
310 free(cd->u.none.active_name);
311 cd->u.none.active_name = NULL;
314 /* keyslot helpers */
315 static int keyslot_verify_or_find_empty(struct crypt_device *cd, int *keyslot)
317 if (*keyslot == CRYPT_ANY_SLOT) {
318 *keyslot = LUKS_keyslot_find_empty(&cd->u.luks1.hdr);
320 log_err(cd, _("All key slots full.\n"));
325 switch (LUKS_keyslot_info(&cd->u.luks1.hdr, *keyslot)) {
326 case CRYPT_SLOT_INVALID:
327 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
328 *keyslot, LUKS_NUMKEYS - 1);
330 case CRYPT_SLOT_INACTIVE:
333 log_err(cd, _("Key slot %d is full, please select another one.\n"),
342 * compares UUIDs returned by device-mapper (striped by cryptsetup) and uuid in header
344 static int crypt_uuid_cmp(const char *dm_uuid, const char *hdr_uuid)
349 if (!dm_uuid || !hdr_uuid)
352 str = strchr(dm_uuid, '-');
356 for (i = 0, j = 1; hdr_uuid[i]; i++) {
357 if (hdr_uuid[i] == '-')
360 if (!str[j] || str[j] == '-')
363 if (str[j] != hdr_uuid[i])
372 * compares type of active device to provided string (only if there is no explicit type)
374 static int crypt_uuid_type_cmp(struct crypt_device *cd, const char *type)
376 struct crypt_dm_active_device dmd = {};
380 /* Must user header-on-disk if we know type here */
381 if (cd->type || !cd->u.none.active_name)
384 log_dbg("Checking if active device %s without header has UUID type %s.",
385 cd->u.none.active_name, type);
387 r = dm_query_device(cd, cd->u.none.active_name, DM_ACTIVE_UUID, &dmd);
393 if (dmd.uuid && strlen(dmd.uuid) > len &&
394 !strncmp(dmd.uuid, type, len) && dmd.uuid[len] == '-')
397 free(CONST_CAST(void*)dmd.uuid);
401 int PLAIN_activate(struct crypt_device *cd,
403 struct volume_key *vk,
408 char *dm_cipher = NULL;
409 enum devcheck device_check;
410 struct crypt_dm_active_device dmd = {
414 .data_device = crypt_data_device(cd),
418 .offset = crypt_get_data_offset(cd),
419 .iv_offset = crypt_get_iv_offset(cd),
423 if (dmd.flags & CRYPT_ACTIVATE_SHARED)
424 device_check = DEV_SHARED;
426 device_check = DEV_EXCL;
428 r = device_block_adjust(cd, dmd.data_device, device_check,
429 dmd.u.crypt.offset, &dmd.size, &dmd.flags);
433 if (crypt_get_cipher_mode(cd))
434 r = asprintf(&dm_cipher, "%s-%s", crypt_get_cipher(cd), crypt_get_cipher_mode(cd));
436 r = asprintf(&dm_cipher, "%s", crypt_get_cipher(cd));
440 dmd.u.crypt.cipher = dm_cipher;
441 log_dbg("Trying to activate PLAIN device %s using cipher %s.",
442 name, dmd.u.crypt.cipher);
444 r = dm_create_device(cd, name, CRYPT_PLAIN, &dmd, 0);
450 int crypt_confirm(struct crypt_device *cd, const char *msg)
452 if (!cd || !cd->confirm)
455 return cd->confirm(msg, cd->confirm_usrptr);
458 static int key_from_terminal(struct crypt_device *cd, char *msg, char **key,
459 size_t *key_len, int force_verify)
461 char *prompt = NULL, *device_name;
466 if (crypt_loop_device(crypt_get_device_name(cd)))
467 device_name = crypt_loop_backing_file(crypt_get_device_name(cd));
469 device_name = strdup(crypt_get_device_name(cd));
472 r = asprintf(&prompt, _("Enter passphrase for %s: "), device_name);
480 *key = crypt_safe_alloc(DEFAULT_PASSPHRASE_SIZE_MAX);
485 r = cd->password(msg, *key, DEFAULT_PASSPHRASE_SIZE_MAX,
486 cd->password_usrptr);
488 crypt_safe_free(*key);
493 r = crypt_get_key(msg, key, key_len, 0, 0, NULL, cd->timeout,
494 (force_verify || cd->password_verify), cd);
497 return (r < 0) ? r: 0;
500 static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslot,
501 struct volume_key **vk)
503 char *passphrase_read = NULL;
504 size_t passphrase_size_read;
505 int r = -EINVAL, eperm = 0, tries = cd->tries;
509 crypt_free_volume_key(*vk);
512 r = key_from_terminal(cd, NULL, &passphrase_read,
513 &passphrase_size_read, 0);
514 /* Continue if it is just passphrase verify mismatch */
520 r = LUKS_open_key_with_hdr(keyslot, passphrase_read,
521 passphrase_size_read, &cd->u.luks1.hdr, vk, cd);
524 crypt_safe_free(passphrase_read);
525 passphrase_read = NULL;
526 } while (r == -EPERM && (--tries > 0));
529 crypt_free_volume_key(*vk);
532 /* Report wrong passphrase if at least one try failed */
533 if (eperm && r == -EPIPE)
537 crypt_safe_free(passphrase_read);
541 static int key_from_file(struct crypt_device *cd, char *msg,
542 char **key, size_t *key_len,
543 const char *key_file, size_t key_offset,
546 return crypt_get_key(msg, key, key_len, key_offset, key_size, key_file,
550 void crypt_set_log_callback(struct crypt_device *cd,
551 void (*log)(int level, const char *msg, void *usrptr),
558 cd->log_usrptr = usrptr;
562 void crypt_set_confirm_callback(struct crypt_device *cd,
563 int (*confirm)(const char *msg, void *usrptr),
566 cd->confirm = confirm;
567 cd->confirm_usrptr = usrptr;
570 void crypt_set_password_callback(struct crypt_device *cd,
571 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
574 cd->password = password;
575 cd->password_usrptr = usrptr;
578 static void _get_error(char *error, char *buf, size_t size)
580 if (!buf || size < 1)
583 strncpy(buf, error, size - 1);
584 buf[size - 1] = '\0';
590 void crypt_last_error(struct crypt_device *cd, char *buf, size_t size)
593 return _get_error(cd->error, buf, size);
596 /* Deprecated global error interface */
597 void crypt_get_error(char *buf, size_t size)
599 return _get_error(global_error, buf, size);
602 const char *crypt_get_dir(void)
607 int crypt_init(struct crypt_device **cd, const char *device)
609 struct crypt_device *h = NULL;
615 log_dbg("Allocating crypt device %s context.", device);
617 if (!(h = malloc(sizeof(struct crypt_device))))
620 memset(h, 0, sizeof(*h));
622 r = device_alloc(&h->device, device);
628 h->iteration_time = 1000;
629 h->password_verify = 0;
631 h->rng_type = crypt_random_default_key_rng();
635 device_free(h->device);
640 static int crypt_check_data_device_size(struct crypt_device *cd)
643 uint64_t size, size_min;
645 /* Check data device size, require at least one sector */
646 size_min = crypt_get_data_offset(cd) << SECTOR_SHIFT ?: SECTOR_SIZE;
648 r = device_size(cd->device, &size);
652 if (size < size_min) {
653 log_err(cd, _("Header detected but device %s is too small.\n"),
654 device_path(cd->device));
661 int crypt_set_data_device(struct crypt_device *cd, const char *device)
663 struct device *dev = NULL;
666 log_dbg("Setting ciphertext data device to %s.", device ?: "(none)");
668 if (!isLUKS(cd->type) && !isVERITY(cd->type)) {
669 log_err(cd, _("This operation is not supported for this device type.\n"));
673 /* metadata device must be set */
674 if (!cd->device || !device)
677 r = device_alloc(&dev, device);
681 if (!cd->metadata_device) {
682 cd->metadata_device = cd->device;
684 device_free(cd->device);
688 return crypt_check_data_device_size(cd);
691 static int _crypt_load_luks1(struct crypt_device *cd, int require_header, int repair)
693 struct luks_phdr hdr;
700 r = LUKS_read_phdr(&hdr, require_header, repair, cd);
704 if (!cd->type && !(cd->type = strdup(CRYPT_LUKS1)))
707 memcpy(&cd->u.luks1.hdr, &hdr, sizeof(hdr));
712 static int _crypt_load_tcrypt(struct crypt_device *cd, struct crypt_params_tcrypt *params)
723 memcpy(&cd->u.tcrypt.params, params, sizeof(*params));
725 r = TCRYPT_read_phdr(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
727 cd->u.tcrypt.params.passphrase = NULL;
728 cd->u.tcrypt.params.passphrase_size = 0;
729 cd->u.tcrypt.params.keyfiles = NULL;
730 cd->u.tcrypt.params.keyfiles_count = 0;
735 if (!cd->type && !(cd->type = strdup(CRYPT_TCRYPT)))
741 static int _crypt_load_verity(struct crypt_device *cd, struct crypt_params_verity *params)
744 size_t sb_offset = 0;
750 if (params && params->flags & CRYPT_VERITY_NO_HEADER)
754 sb_offset = params->hash_area_offset;
756 r = VERITY_read_sb(cd, sb_offset, &cd->u.verity.uuid, &cd->u.verity.hdr);
761 cd->u.verity.hdr.flags = params->flags;
763 /* Hash availability checked in sb load */
764 cd->u.verity.root_hash_size = crypt_hash_size(cd->u.verity.hdr.hash_name);
765 if (cd->u.verity.root_hash_size > 4096)
768 if (!cd->type && !(cd->type = strdup(CRYPT_VERITY)))
771 if (params && params->data_device &&
772 (r = crypt_set_data_device(cd, params->data_device)) < 0)
778 static int _init_by_name_crypt(struct crypt_device *cd, const char *name)
780 struct crypt_dm_active_device dmd = {};
781 char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
784 r = dm_query_device(cd, name,
787 DM_ACTIVE_CRYPT_CIPHER |
788 DM_ACTIVE_CRYPT_KEYSIZE, &dmd);
794 if (isPLAIN(cd->type)) {
795 cd->u.plain.hdr.hash = NULL; /* no way to get this */
796 cd->u.plain.hdr.offset = dmd.u.crypt.offset;
797 cd->u.plain.hdr.skip = dmd.u.crypt.iv_offset;
798 cd->u.plain.key_size = dmd.u.crypt.vk->keylength;
800 r = crypt_parse_name_and_mode(dmd.u.crypt.cipher, cipher, NULL, cipher_mode);
802 cd->u.plain.cipher = strdup(cipher);
803 cd->u.plain.cipher_mode = strdup(cipher_mode);
805 } else if (isLOOPAES(cd->type)) {
806 cd->u.loopaes.hdr.offset = dmd.u.crypt.offset;
808 r = crypt_parse_name_and_mode(dmd.u.crypt.cipher, cipher,
809 &key_nums, cipher_mode);
811 cd->u.loopaes.cipher = strdup(cipher);
812 cd->u.loopaes.cipher_mode = strdup(cipher_mode);
813 /* version 3 uses last key for IV */
814 if (dmd.u.crypt.vk->keylength % key_nums)
816 cd->u.loopaes.key_size = dmd.u.crypt.vk->keylength / key_nums;
818 } else if (isLUKS(cd->type)) {
819 if (crypt_metadata_device(cd)) {
820 r = _crypt_load_luks1(cd, 0, 0);
822 log_dbg("LUKS device header does not match active device.");
823 crypt_set_null_type(cd);
827 /* check whether UUIDs match each other */
828 r = crypt_uuid_cmp(dmd.uuid, cd->u.luks1.hdr.uuid);
830 log_dbg("LUKS device header uuid: %s mismatches DM returned uuid %s",
831 cd->u.luks1.hdr.uuid, dmd.uuid);
832 crypt_set_null_type(cd);
836 log_dbg("LUKS device header not available.");
837 crypt_set_null_type(cd);
840 } else if (isTCRYPT(cd->type)) {
841 r = TCRYPT_init_by_name(cd, name, &dmd, &cd->device,
842 &cd->u.tcrypt.params, &cd->u.tcrypt.hdr);
845 crypt_free_volume_key(dmd.u.crypt.vk);
846 device_free(dmd.data_device);
847 free(CONST_CAST(void*)dmd.u.crypt.cipher);
848 free(CONST_CAST(void*)dmd.uuid);
852 static int _init_by_name_verity(struct crypt_device *cd, const char *name)
854 struct crypt_params_verity params = {};
855 struct crypt_dm_active_device dmd = {
857 .u.verity.vp = ¶ms,
861 r = dm_query_device(cd, name,
863 DM_ACTIVE_VERITY_HASH_DEVICE |
864 DM_ACTIVE_VERITY_PARAMS, &dmd);
870 if (isVERITY(cd->type)) {
871 cd->u.verity.uuid = NULL; // FIXME
872 cd->u.verity.hdr.flags = CRYPT_VERITY_NO_HEADER; //FIXME
873 cd->u.verity.hdr.data_size = params.data_size;
874 cd->u.verity.root_hash_size = dmd.u.verity.root_hash_size;
875 cd->u.verity.root_hash = NULL;
876 cd->u.verity.hdr.hash_name = params.hash_name;
877 cd->u.verity.hdr.data_device = NULL;
878 cd->u.verity.hdr.hash_device = NULL;
879 cd->u.verity.hdr.data_block_size = params.data_block_size;
880 cd->u.verity.hdr.hash_block_size = params.hash_block_size;
881 cd->u.verity.hdr.hash_area_offset = dmd.u.verity.hash_offset;
882 cd->u.verity.hdr.hash_type = params.hash_type;
883 cd->u.verity.hdr.flags = params.flags;
884 cd->u.verity.hdr.salt_size = params.salt_size;
885 cd->u.verity.hdr.salt = params.salt;
886 cd->metadata_device = dmd.u.verity.hash_device;
889 device_free(dmd.data_device);
893 int crypt_init_by_name_and_header(struct crypt_device **cd,
895 const char *header_device)
897 crypt_status_info ci;
898 struct crypt_dm_active_device dmd;
901 log_dbg("Allocating crypt device context by device %s.", name);
903 ci = crypt_status(NULL, name);
904 if (ci == CRYPT_INVALID)
907 if (ci < CRYPT_ACTIVE) {
908 log_err(NULL, _("Device %s is not active.\n"), name);
912 r = dm_query_device(NULL, name, DM_ACTIVE_DEVICE | DM_ACTIVE_UUID, &dmd);
919 r = crypt_init(cd, header_device);
921 r = crypt_init(cd, device_path(dmd.data_device));
923 /* Underlying device disappeared but mapping still active */
924 if (!dmd.data_device || r == -ENOTBLK)
925 log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
928 /* Underlying device is not readable but crypt mapping exists */
930 device_free(dmd.data_device);
931 dmd.data_device = NULL;
932 r = crypt_init(cd, NULL);
940 if (!strncmp(CRYPT_PLAIN, dmd.uuid, sizeof(CRYPT_PLAIN)-1))
941 (*cd)->type = strdup(CRYPT_PLAIN);
942 else if (!strncmp(CRYPT_LOOPAES, dmd.uuid, sizeof(CRYPT_LOOPAES)-1))
943 (*cd)->type = strdup(CRYPT_LOOPAES);
944 else if (!strncmp(CRYPT_LUKS1, dmd.uuid, sizeof(CRYPT_LUKS1)-1))
945 (*cd)->type = strdup(CRYPT_LUKS1);
946 else if (!strncmp(CRYPT_VERITY, dmd.uuid, sizeof(CRYPT_VERITY)-1))
947 (*cd)->type = strdup(CRYPT_VERITY);
948 else if (!strncmp(CRYPT_TCRYPT, dmd.uuid, sizeof(CRYPT_TCRYPT)-1))
949 (*cd)->type = strdup(CRYPT_TCRYPT);
951 log_dbg("Unknown UUID set, some parameters are not set.");
953 log_dbg("Active device has no UUID set, some parameters are not set.");
956 r = crypt_set_data_device(*cd, device_path(dmd.data_device));
961 /* Try to initialise basic parameters from active device */
963 if (dmd.target == DM_CRYPT)
964 r = _init_by_name_crypt(*cd, name);
965 else if (dmd.target == DM_VERITY)
966 r = _init_by_name_verity(*cd, name);
971 } else if (!(*cd)->type && name) {
972 /* For anonymous device (no header found) remember initialized name */
973 (*cd)->u.none.active_name = strdup(name);
976 device_free(dmd.data_device);
977 free(CONST_CAST(void*)dmd.uuid);
981 int crypt_init_by_name(struct crypt_device **cd, const char *name)
983 return crypt_init_by_name_and_header(cd, name, NULL);
986 static int _crypt_format_plain(struct crypt_device *cd,
988 const char *cipher_mode,
990 size_t volume_key_size,
991 struct crypt_params_plain *params)
993 if (!cipher || !cipher_mode) {
994 log_err(cd, _("Invalid plain crypt parameters.\n"));
998 if (volume_key_size > 1024) {
999 log_err(cd, _("Invalid key size.\n"));
1004 log_err(cd, _("UUID is not supported for this crypt type.\n"));
1008 if (!(cd->type = strdup(CRYPT_PLAIN)))
1011 cd->u.plain.key_size = volume_key_size;
1012 cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
1013 if (!cd->volume_key)
1016 cd->u.plain.cipher = strdup(cipher);
1017 cd->u.plain.cipher_mode = strdup(cipher_mode);
1020 if (params && params->hash)
1021 cd->u.plain.hdr.hash = strdup(params->hash);
1023 cd->u.plain.hdr.offset = params ? params->offset : 0;
1024 cd->u.plain.hdr.skip = params ? params->skip : 0;
1025 cd->u.plain.hdr.size = params ? params->size : 0;
1027 if (!cd->u.plain.cipher || !cd->u.plain.cipher_mode)
1033 static int _crypt_format_luks1(struct crypt_device *cd,
1035 const char *cipher_mode,
1037 const char *volume_key,
1038 size_t volume_key_size,
1039 struct crypt_params_luks1 *params)
1042 unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
1043 unsigned long alignment_offset = 0;
1045 if (!crypt_metadata_device(cd)) {
1046 log_err(cd, _("Can't format LUKS without device.\n"));
1050 if (!(cd->type = strdup(CRYPT_LUKS1)))
1054 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
1057 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
1062 if (params && params->data_device) {
1063 cd->metadata_device = cd->device;
1065 if (device_alloc(&cd->device, params->data_device) < 0)
1067 required_alignment = params->data_alignment * SECTOR_SIZE;
1068 } else if (params && params->data_alignment) {
1069 required_alignment = params->data_alignment * SECTOR_SIZE;
1071 device_topology_alignment(cd->device,
1072 &required_alignment,
1073 &alignment_offset, DEFAULT_DISK_ALIGNMENT);
1075 r = LUKS_generate_phdr(&cd->u.luks1.hdr, cd->volume_key, cipher, cipher_mode,
1076 (params && params->hash) ? params->hash : "sha1",
1078 required_alignment / SECTOR_SIZE,
1079 alignment_offset / SECTOR_SIZE,
1080 cd->iteration_time, &cd->u.luks1.PBKDF2_per_sec,
1081 cd->metadata_device ? 1 : 0, cd);
1085 /* Wipe first 8 sectors - fs magic numbers etc. */
1086 r = crypt_wipe(crypt_metadata_device(cd), 0, 8 * SECTOR_SIZE, CRYPT_WIPE_ZERO, 1);
1089 log_err(cd, _("Cannot format device %s which is still in use.\n"),
1090 mdata_device_path(cd));
1091 else if (r == -EACCES) {
1092 log_err(cd, _("Cannot format device %s, permission denied.\n"),
1093 mdata_device_path(cd));
1096 log_err(cd, _("Cannot wipe header on device %s.\n"),
1097 mdata_device_path(cd));
1102 r = LUKS_write_phdr(&cd->u.luks1.hdr, cd);
1107 static int _crypt_format_loopaes(struct crypt_device *cd,
1110 size_t volume_key_size,
1111 struct crypt_params_loopaes *params)
1113 if (!crypt_metadata_device(cd)) {
1114 log_err(cd, _("Can't format LOOPAES without device.\n"));
1118 if (volume_key_size > 1024) {
1119 log_err(cd, _("Invalid key size.\n"));
1124 log_err(cd, _("UUID is not supported for this crypt type.\n"));
1128 if (!(cd->type = strdup(CRYPT_LOOPAES)))
1131 cd->u.loopaes.key_size = volume_key_size;
1133 cd->u.loopaes.cipher = strdup(cipher ?: DEFAULT_LOOPAES_CIPHER);
1135 if (params && params->hash)
1136 cd->u.loopaes.hdr.hash = strdup(params->hash);
1138 cd->u.loopaes.hdr.offset = params ? params->offset : 0;
1139 cd->u.loopaes.hdr.skip = params ? params->skip : 0;
1144 static int _crypt_format_verity(struct crypt_device *cd,
1146 struct crypt_params_verity *params)
1148 int r = 0, hash_size;
1149 uint64_t data_device_size;
1151 if (!crypt_metadata_device(cd)) {
1152 log_err(cd, _("Can't format VERITY without device.\n"));
1156 if (!params || !params->data_device)
1159 if (params->hash_type > VERITY_MAX_HASH_TYPE) {
1160 log_err(cd, _("Unsupported VERITY hash type %d.\n"), params->hash_type);
1164 if (VERITY_BLOCK_SIZE_OK(params->data_block_size) ||
1165 VERITY_BLOCK_SIZE_OK(params->hash_block_size)) {
1166 log_err(cd, _("Unsupported VERITY block size.\n"));
1170 if (params->hash_area_offset % 512) {
1171 log_err(cd, _("Unsupported VERITY hash offset.\n"));
1175 if (!(cd->type = strdup(CRYPT_VERITY)))
1178 r = crypt_set_data_device(cd, params->data_device);
1181 if (!params->data_size) {
1182 r = device_size(cd->device, &data_device_size);
1186 cd->u.verity.hdr.data_size = data_device_size / params->data_block_size;
1188 cd->u.verity.hdr.data_size = params->data_size;
1190 hash_size = crypt_hash_size(params->hash_name);
1191 if (hash_size <= 0) {
1192 log_err(cd, _("Hash algorithm %s not supported.\n"),
1196 cd->u.verity.root_hash_size = hash_size;
1198 cd->u.verity.root_hash = malloc(cd->u.verity.root_hash_size);
1199 if (!cd->u.verity.root_hash)
1202 cd->u.verity.hdr.flags = params->flags;
1203 if (!(cd->u.verity.hdr.hash_name = strdup(params->hash_name)))
1205 cd->u.verity.hdr.data_device = NULL;
1206 cd->u.verity.hdr.data_block_size = params->data_block_size;
1207 cd->u.verity.hdr.hash_block_size = params->hash_block_size;
1208 cd->u.verity.hdr.hash_area_offset = params->hash_area_offset;
1209 cd->u.verity.hdr.hash_type = params->hash_type;
1210 cd->u.verity.hdr.flags = params->flags;
1211 cd->u.verity.hdr.salt_size = params->salt_size;
1212 if (!(cd->u.verity.hdr.salt = malloc(params->salt_size)))
1216 memcpy(CONST_CAST(char*)cd->u.verity.hdr.salt, params->salt,
1219 r = crypt_random_get(cd, CONST_CAST(char*)cd->u.verity.hdr.salt,
1220 params->salt_size, CRYPT_RND_SALT);
1224 if (params->flags & CRYPT_VERITY_CREATE_HASH) {
1225 r = VERITY_create(cd, &cd->u.verity.hdr,
1226 cd->u.verity.root_hash, cd->u.verity.root_hash_size);
1231 if (!(params->flags & CRYPT_VERITY_NO_HEADER)) {
1233 cd->u.verity.uuid = strdup(uuid);
1235 r = VERITY_UUID_generate(cd, &cd->u.verity.uuid);
1240 r = VERITY_write_sb(cd, cd->u.verity.hdr.hash_area_offset,
1247 int crypt_format(struct crypt_device *cd,
1250 const char *cipher_mode,
1252 const char *volume_key,
1253 size_t volume_key_size,
1262 log_dbg("Context already formatted as %s.", cd->type);
1266 log_dbg("Formatting device %s as type %s.", mdata_device_path(cd) ?: "(none)", type);
1268 crypt_reset_null_type(cd);
1270 r = init_crypto(cd);
1275 r = _crypt_format_plain(cd, cipher, cipher_mode,
1276 uuid, volume_key_size, params);
1277 else if (isLUKS(type))
1278 r = _crypt_format_luks1(cd, cipher, cipher_mode,
1279 uuid, volume_key, volume_key_size, params);
1280 else if (isLOOPAES(type))
1281 r = _crypt_format_loopaes(cd, cipher, uuid, volume_key_size, params);
1282 else if (isVERITY(type))
1283 r = _crypt_format_verity(cd, uuid, params);
1285 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
1290 crypt_set_null_type(cd);
1291 crypt_free_volume_key(cd->volume_key);
1292 cd->volume_key = NULL;
1298 int crypt_load(struct crypt_device *cd,
1299 const char *requested_type,
1304 log_dbg("Trying to load %s crypt type from device %s.",
1305 requested_type ?: "any", mdata_device_path(cd) ?: "(none)");
1307 if (!crypt_metadata_device(cd))
1310 crypt_reset_null_type(cd);
1312 if (!requested_type || isLUKS(requested_type)) {
1313 if (cd->type && !isLUKS(cd->type)) {
1314 log_dbg("Context is already initialised to type %s", cd->type);
1318 r = _crypt_load_luks1(cd, 1, 0);
1319 } else if (isVERITY(requested_type)) {
1320 if (cd->type && !isVERITY(cd->type)) {
1321 log_dbg("Context is already initialised to type %s", cd->type);
1324 r = _crypt_load_verity(cd, params);
1325 } else if (isTCRYPT(requested_type)) {
1326 if (cd->type && !isTCRYPT(cd->type)) {
1327 log_dbg("Context is already initialised to type %s", cd->type);
1330 r = _crypt_load_tcrypt(cd, params);
1337 int crypt_repair(struct crypt_device *cd,
1338 const char *requested_type,
1339 void *params __attribute__((unused)))
1343 log_dbg("Trying to repair %s crypt type from device %s.",
1344 requested_type ?: "any", mdata_device_path(cd) ?: "(none)");
1346 if (!crypt_metadata_device(cd))
1349 if (requested_type && !isLUKS(requested_type))
1353 /* Load with repair */
1354 r = _crypt_load_luks1(cd, 1, 1);
1358 /* cd->type and header must be set in context */
1359 r = crypt_check_data_device_size(cd);
1361 crypt_set_null_type(cd);
1366 int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
1368 struct crypt_dm_active_device dmd;
1371 /* Device context type must be initialised */
1375 log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
1377 r = dm_query_device(cd, name, DM_ACTIVE_DEVICE | DM_ACTIVE_CRYPT_CIPHER |
1378 DM_ACTIVE_UUID | DM_ACTIVE_CRYPT_KEYSIZE |
1379 DM_ACTIVE_CRYPT_KEY, &dmd);
1381 log_err(NULL, _("Device %s is not active.\n"), name);
1385 if (!dmd.uuid || dmd.target != DM_CRYPT) {
1390 r = device_block_adjust(cd, dmd.data_device, DEV_OK,
1391 dmd.u.crypt.offset, &new_size, &dmd.flags);
1395 if (new_size == dmd.size) {
1396 log_dbg("Device has already requested size %" PRIu64
1397 " sectors.", dmd.size);
1400 dmd.size = new_size;
1401 if (isTCRYPT(cd->type))
1404 r = dm_create_device(cd, name, cd->type, &dmd, 1);
1407 if (dmd.target == DM_CRYPT) {
1408 crypt_free_volume_key(dmd.u.crypt.vk);
1409 free(CONST_CAST(void*)dmd.u.crypt.cipher);
1411 free(CONST_CAST(void*)dmd.data_device);
1412 free(CONST_CAST(void*)dmd.uuid);
1417 int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
1419 if (!isLUKS(cd->type)) {
1420 log_err(cd, _("This operation is not supported for this device type.\n"));
1424 if (uuid && !strncmp(uuid, cd->u.luks1.hdr.uuid, sizeof(cd->u.luks1.hdr.uuid))) {
1425 log_dbg("UUID is the same as requested (%s) for device %s.",
1426 uuid, mdata_device_path(cd));
1431 log_dbg("Requested new UUID change to %s for %s.", uuid, mdata_device_path(cd));
1433 log_dbg("Requested new UUID refresh for %s.", mdata_device_path(cd));
1435 if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
1438 return LUKS_hdr_uuid_set(&cd->u.luks1.hdr, uuid, cd);
1441 int crypt_header_backup(struct crypt_device *cd,
1442 const char *requested_type,
1443 const char *backup_file)
1447 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1450 if (cd->type && !isLUKS(cd->type))
1453 r = init_crypto(cd);
1457 log_dbg("Requested header backup of device %s (%s) to "
1458 "file %s.", mdata_device_path(cd), requested_type, backup_file);
1460 r = LUKS_hdr_backup(backup_file, cd);
1464 int crypt_header_restore(struct crypt_device *cd,
1465 const char *requested_type,
1466 const char *backup_file)
1468 struct luks_phdr hdr;
1471 if (requested_type && !isLUKS(requested_type))
1474 if (cd->type && !isLUKS(cd->type))
1477 r = init_crypto(cd);
1481 log_dbg("Requested header restore to device %s (%s) from "
1482 "file %s.", mdata_device_path(cd), requested_type, backup_file);
1484 r = LUKS_hdr_restore(backup_file, isLUKS(cd->type) ? &cd->u.luks1.hdr : &hdr, cd);
1486 crypt_memzero(&hdr, sizeof(hdr));
1490 void crypt_free(struct crypt_device *cd)
1493 log_dbg("Releasing crypt device %s context.", mdata_device_path(cd));
1496 crypt_free_volume_key(cd->volume_key);
1498 device_free(cd->device);
1499 device_free(cd->metadata_device);
1501 if (isPLAIN(cd->type)) {
1502 free(CONST_CAST(void*)cd->u.plain.hdr.hash);
1503 free(cd->u.plain.cipher);
1504 free(cd->u.plain.cipher_mode);
1505 } else if (isLOOPAES(cd->type)) {
1506 free(CONST_CAST(void*)cd->u.loopaes.hdr.hash);
1507 free(cd->u.loopaes.cipher);
1508 } else if (isVERITY(cd->type)) {
1509 free(CONST_CAST(void*)cd->u.verity.hdr.hash_name);
1510 free(CONST_CAST(void*)cd->u.verity.hdr.salt);
1511 free(cd->u.verity.root_hash);
1512 free(cd->u.verity.uuid);
1513 } else if (!cd->type) {
1514 free(cd->u.none.active_name);
1518 /* Some structures can contain keys (TCRYPT), wipe it */
1519 crypt_memzero(cd, sizeof(*cd));
1524 int crypt_suspend(struct crypt_device *cd,
1527 crypt_status_info ci;
1530 log_dbg("Suspending volume %s.", name);
1535 r = crypt_uuid_type_cmp(cd, CRYPT_LUKS1);
1537 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1543 ci = crypt_status(NULL, name);
1544 if (ci < CRYPT_ACTIVE) {
1545 log_err(cd, _("Volume %s is not active.\n"), name);
1551 r = dm_status_suspended(cd, name);
1556 log_err(cd, _("Volume %s is already suspended.\n"), name);
1561 r = dm_suspend_and_wipe_key(cd, name);
1563 log_err(cd, _("Suspend is not supported for device %s.\n"), name);
1565 log_err(cd, _("Error during suspending device %s.\n"), name);
1571 int crypt_resume_by_passphrase(struct crypt_device *cd,
1574 const char *passphrase,
1575 size_t passphrase_size)
1577 struct volume_key *vk = NULL;
1580 log_dbg("Resuming volume %s.", name);
1586 r = dm_status_suspended(cd, name);
1591 log_err(cd, _("Volume %s is not suspended.\n"), name);
1596 r = LUKS_open_key_with_hdr(keyslot, passphrase, passphrase_size,
1597 &cd->u.luks1.hdr, &vk, cd);
1599 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1603 r = dm_resume_and_reinstate_key(cd, name, vk->keylength, vk->key);
1605 log_err(cd, _("Resume is not supported for device %s.\n"), name);
1607 log_err(cd, _("Error during resuming device %s.\n"), name);
1611 crypt_free_volume_key(vk);
1612 return r < 0 ? r : keyslot;
1615 int crypt_resume_by_keyfile_offset(struct crypt_device *cd,
1618 const char *keyfile,
1619 size_t keyfile_size,
1620 size_t keyfile_offset)
1622 struct volume_key *vk = NULL;
1623 char *passphrase_read = NULL;
1624 size_t passphrase_size_read;
1627 log_dbg("Resuming volume %s.", name);
1633 r = dm_status_suspended(cd, name);
1638 log_err(cd, _("Volume %s is not suspended.\n"), name);
1645 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1646 &passphrase_size_read, keyfile, keyfile_offset,
1651 r = LUKS_open_key_with_hdr(keyslot, passphrase_read,
1652 passphrase_size_read, &cd->u.luks1.hdr, &vk, cd);
1657 r = dm_resume_and_reinstate_key(cd, name, vk->keylength, vk->key);
1659 log_err(cd, _("Error during resuming device %s.\n"), name);
1661 crypt_safe_free(passphrase_read);
1662 crypt_free_volume_key(vk);
1663 return r < 0 ? r : keyslot;
1666 int crypt_resume_by_keyfile(struct crypt_device *cd,
1669 const char *keyfile,
1670 size_t keyfile_size)
1672 return crypt_resume_by_keyfile_offset(cd, name, keyslot,
1673 keyfile, keyfile_size, 0);
1676 // slot manipulation
1677 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1678 int keyslot, // -1 any
1679 const char *passphrase, // NULL -> terminal
1680 size_t passphrase_size,
1681 const char *new_passphrase, // NULL -> terminal
1682 size_t new_passphrase_size)
1684 struct volume_key *vk = NULL;
1685 char *password = NULL, *new_password = NULL;
1686 size_t passwordLen, new_passwordLen;
1689 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1690 "new passphrase %sprovided.",
1691 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1697 r = keyslot_verify_or_find_empty(cd, &keyslot);
1701 if (!LUKS_keyslot_active_count(&cd->u.luks1.hdr)) {
1702 /* No slots used, try to use pre-generated key in header */
1703 if (cd->volume_key) {
1704 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1705 r = vk ? 0 : -ENOMEM;
1707 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1710 } else if (passphrase) {
1711 /* Passphrase provided, use it to unlock existing keyslot */
1712 r = LUKS_open_key_with_hdr(CRYPT_ANY_SLOT, passphrase,
1713 passphrase_size, &cd->u.luks1.hdr, &vk, cd);
1715 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1716 r = key_from_terminal(cd, _("Enter any passphrase: "),
1717 &password, &passwordLen, 0);
1721 r = LUKS_open_key_with_hdr(CRYPT_ANY_SLOT, password,
1722 passwordLen, &cd->u.luks1.hdr, &vk, cd);
1723 crypt_safe_free(password);
1729 if (new_passphrase) {
1730 new_password = CONST_CAST(char*)new_passphrase;
1731 new_passwordLen = new_passphrase_size;
1733 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1734 &new_password, &new_passwordLen, 1);
1739 r = LUKS_set_key(keyslot, new_password, new_passwordLen,
1740 &cd->u.luks1.hdr, vk, cd->iteration_time, &cd->u.luks1.PBKDF2_per_sec, cd);
1746 if (!new_passphrase)
1747 crypt_safe_free(new_password);
1748 crypt_free_volume_key(vk);
1749 return r < 0 ? r : keyslot;
1752 int crypt_keyslot_change_by_passphrase(struct crypt_device *cd,
1755 const char *passphrase,
1756 size_t passphrase_size,
1757 const char *new_passphrase,
1758 size_t new_passphrase_size)
1760 struct volume_key *vk = NULL;
1763 log_dbg("Changing passphrase from old keyslot %d to new %d.",
1764 keyslot_old, keyslot_new);
1770 r = LUKS_open_key_with_hdr(keyslot_old, passphrase, passphrase_size,
1771 &cd->u.luks1.hdr, &vk, cd);
1775 if (keyslot_old != CRYPT_ANY_SLOT && keyslot_old != r) {
1776 log_dbg("Keyslot mismatch.");
1781 if (keyslot_new == CRYPT_ANY_SLOT) {
1782 keyslot_new = LUKS_keyslot_find_empty(&cd->u.luks1.hdr);
1783 if (keyslot_new < 0)
1784 keyslot_new = keyslot_old;
1787 if (keyslot_old == keyslot_new) {
1788 log_dbg("Key slot %d is going to be overwritten.", keyslot_old);
1789 (void)crypt_keyslot_destroy(cd, keyslot_old);
1792 r = LUKS_set_key(keyslot_new, new_passphrase, new_passphrase_size,
1793 &cd->u.luks1.hdr, vk, cd->iteration_time,
1794 &cd->u.luks1.PBKDF2_per_sec, cd);
1796 if (keyslot_old == keyslot_new) {
1798 log_verbose(cd, _("Key slot %d changed.\n"), keyslot_new);
1801 log_verbose(cd, _("Replaced with key slot %d.\n"), keyslot_new);
1802 r = crypt_keyslot_destroy(cd, keyslot_old);
1806 log_err(cd, _("Failed to swap new key slot.\n"));
1808 crypt_free_volume_key(vk);
1809 return r < 0 ? r : keyslot_new;
1812 int crypt_keyslot_add_by_keyfile_offset(struct crypt_device *cd,
1814 const char *keyfile,
1815 size_t keyfile_size,
1816 size_t keyfile_offset,
1817 const char *new_keyfile,
1818 size_t new_keyfile_size,
1819 size_t new_keyfile_offset)
1821 struct volume_key *vk = NULL;
1822 char *password = NULL; size_t passwordLen;
1823 char *new_password = NULL; size_t new_passwordLen;
1826 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1827 keyfile ?: "[none]", new_keyfile ?: "[none]");
1833 r = keyslot_verify_or_find_empty(cd, &keyslot);
1837 if (!LUKS_keyslot_active_count(&cd->u.luks1.hdr)) {
1838 /* No slots used, try to use pre-generated key in header */
1839 if (cd->volume_key) {
1840 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1841 r = vk ? 0 : -ENOMEM;
1843 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1847 /* Read password from file of (if NULL) from terminal */
1849 r = key_from_file(cd, _("Enter any passphrase: "),
1850 &password, &passwordLen,
1851 keyfile, keyfile_offset, keyfile_size);
1853 r = key_from_terminal(cd, _("Enter any passphrase: "),
1854 &password, &passwordLen, 0);
1858 r = LUKS_open_key_with_hdr(CRYPT_ANY_SLOT, password, passwordLen,
1859 &cd->u.luks1.hdr, &vk, cd);
1866 r = key_from_file(cd, _("Enter new passphrase for key slot: "),
1867 &new_password, &new_passwordLen, new_keyfile,
1868 new_keyfile_offset, new_keyfile_size);
1870 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1871 &new_password, &new_passwordLen, 1);
1875 r = LUKS_set_key(keyslot, new_password, new_passwordLen,
1876 &cd->u.luks1.hdr, vk, cd->iteration_time, &cd->u.luks1.PBKDF2_per_sec, cd);
1878 crypt_safe_free(password);
1879 crypt_safe_free(new_password);
1880 crypt_free_volume_key(vk);
1881 return r < 0 ? r : keyslot;
1884 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1886 const char *keyfile,
1887 size_t keyfile_size,
1888 const char *new_keyfile,
1889 size_t new_keyfile_size)
1891 return crypt_keyslot_add_by_keyfile_offset(cd, keyslot,
1892 keyfile, keyfile_size, 0,
1893 new_keyfile, new_keyfile_size, 0);
1896 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1898 const char *volume_key,
1899 size_t volume_key_size,
1900 const char *passphrase,
1901 size_t passphrase_size)
1903 struct volume_key *vk = NULL;
1905 char *new_password = NULL; size_t new_passwordLen;
1907 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1914 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1915 else if (cd->volume_key)
1916 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1921 r = LUKS_verify_volume_key(&cd->u.luks1.hdr, vk);
1923 log_err(cd, _("Volume key does not match the volume.\n"));
1927 r = keyslot_verify_or_find_empty(cd, &keyslot);
1932 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1933 &new_password, &new_passwordLen, 1);
1936 passphrase = new_password;
1937 passphrase_size = new_passwordLen;
1940 r = LUKS_set_key(keyslot, passphrase, passphrase_size,
1941 &cd->u.luks1.hdr, vk, cd->iteration_time, &cd->u.luks1.PBKDF2_per_sec, cd);
1943 crypt_safe_free(new_password);
1944 crypt_free_volume_key(vk);
1945 return (r < 0) ? r : keyslot;
1948 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1950 crypt_keyslot_info ki;
1953 log_dbg("Destroying keyslot %d.", keyslot);
1959 ki = crypt_keyslot_status(cd, keyslot);
1960 if (ki == CRYPT_SLOT_INVALID) {
1961 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1965 if (ki == CRYPT_SLOT_INACTIVE) {
1966 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1970 return LUKS_del_key(keyslot, &cd->u.luks1.hdr, cd);
1973 // activation/deactivation of device mapping
1974 int crypt_activate_by_passphrase(struct crypt_device *cd,
1977 const char *passphrase,
1978 size_t passphrase_size,
1981 crypt_status_info ci;
1982 struct volume_key *vk = NULL;
1983 char *read_passphrase = NULL;
1984 size_t passphraseLen = 0;
1987 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1988 name ? "Activating" : "Checking", name ?: "",
1989 keyslot, passphrase ? "" : "[none] ");
1992 ci = crypt_status(NULL, name);
1993 if (ci == CRYPT_INVALID)
1995 else if (ci >= CRYPT_ACTIVE) {
1996 log_err(cd, _("Device %s already exists.\n"), name);
2001 /* plain, use hashed passphrase */
2002 if (isPLAIN(cd->type)) {
2007 r = key_from_terminal(cd, NULL, &read_passphrase,
2011 passphrase = read_passphrase;
2012 passphrase_size = passphraseLen;
2015 r = process_key(cd, cd->u.plain.hdr.hash,
2016 cd->u.plain.key_size,
2017 passphrase, passphrase_size, &vk);
2021 r = PLAIN_activate(cd, name, vk, cd->u.plain.hdr.size, flags);
2023 } else if (isLUKS(cd->type)) {
2024 /* provided passphrase, do not retry */
2026 r = LUKS_open_key_with_hdr(keyslot, passphrase,
2027 passphrase_size, &cd->u.luks1.hdr, &vk, cd);
2029 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
2034 r = LUKS1_activate(cd, name, vk, flags);
2039 crypt_safe_free(read_passphrase);
2040 crypt_free_volume_key(vk);
2042 return r < 0 ? r : keyslot;
2045 int crypt_activate_by_keyfile_offset(struct crypt_device *cd,
2048 const char *keyfile,
2049 size_t keyfile_size,
2050 size_t keyfile_offset,
2053 crypt_status_info ci;
2054 struct volume_key *vk = NULL;
2055 char *passphrase_read = NULL;
2056 size_t passphrase_size_read;
2057 unsigned int key_count = 0;
2060 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
2061 name ?: "", keyslot, keyfile ?: "[none]");
2064 ci = crypt_status(NULL, name);
2065 if (ci == CRYPT_INVALID)
2067 else if (ci >= CRYPT_ACTIVE) {
2068 log_err(cd, _("Device %s already exists.\n"), name);
2076 if (isPLAIN(cd->type)) {
2080 r = key_from_file(cd, _("Enter passphrase: "),
2081 &passphrase_read, &passphrase_size_read,
2082 keyfile, keyfile_offset, keyfile_size);
2086 r = process_key(cd, cd->u.plain.hdr.hash,
2087 cd->u.plain.key_size,
2088 passphrase_read, passphrase_size_read, &vk);
2092 r = PLAIN_activate(cd, name, vk, cd->u.plain.hdr.size, flags);
2093 } else if (isLUKS(cd->type)) {
2094 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
2095 &passphrase_size_read, keyfile, keyfile_offset, keyfile_size);
2098 r = LUKS_open_key_with_hdr(keyslot, passphrase_read,
2099 passphrase_size_read, &cd->u.luks1.hdr, &vk, cd);
2105 r = LUKS1_activate(cd, name, vk, flags);
2110 } else if (isLOOPAES(cd->type)) {
2111 r = key_from_file(cd, NULL, &passphrase_read, &passphrase_size_read,
2112 keyfile, keyfile_offset, keyfile_size);
2115 r = LOOPAES_parse_keyfile(cd, &vk, cd->u.loopaes.hdr.hash, &key_count,
2116 passphrase_read, passphrase_size_read);
2120 r = LOOPAES_activate(cd, name, cd->u.loopaes.cipher,
2121 key_count, vk, flags);
2126 crypt_safe_free(passphrase_read);
2127 crypt_free_volume_key(vk);
2132 int crypt_activate_by_keyfile(struct crypt_device *cd,
2135 const char *keyfile,
2136 size_t keyfile_size,
2139 return crypt_activate_by_keyfile_offset(cd, name, keyslot, keyfile,
2140 keyfile_size, 0, flags);
2143 int crypt_activate_by_volume_key(struct crypt_device *cd,
2145 const char *volume_key,
2146 size_t volume_key_size,
2149 crypt_status_info ci;
2150 struct volume_key *vk = NULL;
2153 log_dbg("Activating volume %s by volume key.", name ?: "[none]");
2156 ci = crypt_status(NULL, name);
2157 if (ci == CRYPT_INVALID)
2159 else if (ci >= CRYPT_ACTIVE) {
2160 log_err(cd, _("Device %s already exists.\n"), name);
2165 /* use key directly, no hash */
2166 if (isPLAIN(cd->type)) {
2170 if (!volume_key || !volume_key_size || volume_key_size != cd->u.plain.key_size) {
2171 log_err(cd, _("Incorrect volume key specified for plain device.\n"));
2175 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2179 r = PLAIN_activate(cd, name, vk, cd->u.plain.hdr.size, flags);
2180 } else if (isLUKS(cd->type)) {
2181 /* If key is not provided, try to use internal key */
2183 if (!cd->volume_key) {
2184 log_err(cd, _("Volume key does not match the volume.\n"));
2187 volume_key_size = cd->volume_key->keylength;
2188 volume_key = cd->volume_key->key;
2191 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2194 r = LUKS_verify_volume_key(&cd->u.luks1.hdr, vk);
2197 log_err(cd, _("Volume key does not match the volume.\n"));
2200 r = LUKS1_activate(cd, name, vk, flags);
2201 } else if (isVERITY(cd->type)) {
2202 /* volume_key == root hash */
2203 if (!volume_key || !volume_key_size) {
2204 log_err(cd, _("Incorrect root hash specified for verity device.\n"));
2208 r = VERITY_activate(cd, name, volume_key, volume_key_size,
2209 &cd->u.verity.hdr, CRYPT_ACTIVATE_READONLY);
2212 free(cd->u.verity.root_hash);
2213 cd->u.verity.root_hash = NULL;
2215 cd->u.verity.root_hash_size = volume_key_size;
2216 if (!cd->u.verity.root_hash)
2217 cd->u.verity.root_hash = malloc(volume_key_size);
2218 if (cd->u.verity.root_hash)
2219 memcpy(cd->u.verity.root_hash, volume_key, volume_key_size);
2221 } else if (isTCRYPT(cd->type)) {
2224 r = TCRYPT_activate(cd, name, &cd->u.tcrypt.hdr,
2225 &cd->u.tcrypt.params, flags);
2227 log_err(cd, _("Device type is not properly initialised.\n"));
2229 crypt_free_volume_key(vk);
2234 int crypt_deactivate(struct crypt_device *cd, const char *name)
2236 struct crypt_device *fake_cd = NULL;
2242 log_dbg("Deactivating volume %s.", name);
2245 r = crypt_init_by_name(&fake_cd, name);
2251 switch (crypt_status(cd, name)) {
2254 if (isTCRYPT(cd->type))
2255 r = TCRYPT_deactivate(cd, name);
2257 r = dm_remove_device(cd, name, 0, 0);
2258 if (r < 0 && crypt_status(cd, name) == CRYPT_BUSY) {
2259 log_err(cd, _("Device %s is still in use.\n"), name);
2263 case CRYPT_INACTIVE:
2264 log_err(cd, _("Device %s is not active.\n"), name);
2268 log_err(cd, _("Invalid device %s.\n"), name);
2272 crypt_free(fake_cd);
2277 int crypt_volume_key_get(struct crypt_device *cd,
2280 size_t *volume_key_size,
2281 const char *passphrase,
2282 size_t passphrase_size)
2284 struct volume_key *vk = NULL;
2288 if (crypt_fips_mode()) {
2289 log_err(cd, _("Function not available in FIPS mode.\n"));
2293 key_len = crypt_get_volume_key_size(cd);
2294 if (key_len > *volume_key_size) {
2295 log_err(cd, _("Volume key buffer too small.\n"));
2299 if (isPLAIN(cd->type) && cd->u.plain.hdr.hash) {
2300 r = process_key(cd, cd->u.plain.hdr.hash, key_len,
2301 passphrase, passphrase_size, &vk);
2303 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
2304 } else if (isLUKS(cd->type)) {
2305 r = LUKS_open_key_with_hdr(keyslot, passphrase,
2306 passphrase_size, &cd->u.luks1.hdr, &vk, cd);
2307 } else if (isTCRYPT(cd->type)) {
2308 r = TCRYPT_get_volume_key(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params, &vk);
2310 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
2313 memcpy(volume_key, vk->key, vk->keylength);
2314 *volume_key_size = vk->keylength;
2317 crypt_free_volume_key(vk);
2321 int crypt_volume_key_verify(struct crypt_device *cd,
2322 const char *volume_key,
2323 size_t volume_key_size)
2325 struct volume_key *vk;
2332 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2336 r = LUKS_verify_volume_key(&cd->u.luks1.hdr, vk);
2339 log_err(cd, _("Volume key does not match the volume.\n"));
2341 crypt_free_volume_key(vk);
2346 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
2348 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
2349 cd->timeout = timeout_sec;
2352 void crypt_set_password_retry(struct crypt_device *cd, int tries)
2354 log_dbg("Password retry count set to %d.", tries);
2358 void crypt_set_iteration_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2360 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
2361 cd->iteration_time = iteration_time_ms;
2363 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2365 crypt_set_iteration_time(cd, iteration_time_ms);
2368 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
2370 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
2371 cd->password_verify = password_verify ? 1 : 0;
2374 void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
2377 case CRYPT_RNG_URANDOM:
2378 case CRYPT_RNG_RANDOM:
2379 log_dbg("RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
2380 cd->rng_type = rng_type;
2384 int crypt_get_rng_type(struct crypt_device *cd)
2389 return cd->rng_type;
2392 int crypt_memory_lock(struct crypt_device *cd, int lock)
2394 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
2398 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
2405 r = dm_status_device(cd, name);
2410 if (r < 0 && r != -ENODEV)
2411 return CRYPT_INVALID;
2414 return CRYPT_ACTIVE;
2419 return CRYPT_INACTIVE;
2422 static void hexprint(struct crypt_device *cd, const char *d, int n, const char *sep)
2425 for(i = 0; i < n; i++)
2426 log_std(cd, "%02hhx%s", (const char)d[i], sep);
2429 static int _luks_dump(struct crypt_device *cd)
2433 log_std(cd, "LUKS header information for %s\n\n", mdata_device_path(cd));
2434 log_std(cd, "Version: \t%" PRIu16 "\n", cd->u.luks1.hdr.version);
2435 log_std(cd, "Cipher name: \t%s\n", cd->u.luks1.hdr.cipherName);
2436 log_std(cd, "Cipher mode: \t%s\n", cd->u.luks1.hdr.cipherMode);
2437 log_std(cd, "Hash spec: \t%s\n", cd->u.luks1.hdr.hashSpec);
2438 log_std(cd, "Payload offset:\t%" PRIu32 "\n", cd->u.luks1.hdr.payloadOffset);
2439 log_std(cd, "MK bits: \t%" PRIu32 "\n", cd->u.luks1.hdr.keyBytes * 8);
2440 log_std(cd, "MK digest: \t");
2441 hexprint(cd, cd->u.luks1.hdr.mkDigest, LUKS_DIGESTSIZE, " ");
2443 log_std(cd, "MK salt: \t");
2444 hexprint(cd, cd->u.luks1.hdr.mkDigestSalt, LUKS_SALTSIZE/2, " ");
2445 log_std(cd, "\n \t");
2446 hexprint(cd, cd->u.luks1.hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ");
2448 log_std(cd, "MK iterations: \t%" PRIu32 "\n", cd->u.luks1.hdr.mkDigestIterations);
2449 log_std(cd, "UUID: \t%s\n\n", cd->u.luks1.hdr.uuid);
2450 for(i = 0; i < LUKS_NUMKEYS; i++) {
2451 if(cd->u.luks1.hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
2452 log_std(cd, "Key Slot %d: ENABLED\n",i);
2453 log_std(cd, "\tIterations: \t%" PRIu32 "\n",
2454 cd->u.luks1.hdr.keyblock[i].passwordIterations);
2455 log_std(cd, "\tSalt: \t");
2456 hexprint(cd, cd->u.luks1.hdr.keyblock[i].passwordSalt,
2457 LUKS_SALTSIZE/2, " ");
2458 log_std(cd, "\n\t \t");
2459 hexprint(cd, cd->u.luks1.hdr.keyblock[i].passwordSalt +
2460 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ");
2463 log_std(cd, "\tKey material offset:\t%" PRIu32 "\n",
2464 cd->u.luks1.hdr.keyblock[i].keyMaterialOffset);
2465 log_std(cd, "\tAF stripes: \t%" PRIu32 "\n",
2466 cd->u.luks1.hdr.keyblock[i].stripes);
2469 log_std(cd, "Key Slot %d: DISABLED\n", i);
2474 static int _verity_dump(struct crypt_device *cd)
2476 log_std(cd, "VERITY header information for %s\n", mdata_device_path(cd));
2477 log_std(cd, "UUID: \t%s\n", cd->u.verity.uuid ?: "");
2478 log_std(cd, "Hash type: \t%u\n", cd->u.verity.hdr.hash_type);
2479 log_std(cd, "Data blocks: \t%" PRIu64 "\n", cd->u.verity.hdr.data_size);
2480 log_std(cd, "Data block size: \t%u\n", cd->u.verity.hdr.data_block_size);
2481 log_std(cd, "Hash block size: \t%u\n", cd->u.verity.hdr.hash_block_size);
2482 log_std(cd, "Hash algorithm: \t%s\n", cd->u.verity.hdr.hash_name);
2483 log_std(cd, "Salt: \t");
2484 if (cd->u.verity.hdr.salt_size)
2485 hexprint(cd, cd->u.verity.hdr.salt, cd->u.verity.hdr.salt_size, "");
2489 if (cd->u.verity.root_hash) {
2490 log_std(cd, "Root hash: \t");
2491 hexprint(cd, cd->u.verity.root_hash, cd->u.verity.root_hash_size, "");
2497 int crypt_dump(struct crypt_device *cd)
2499 if (isLUKS(cd->type))
2500 return _luks_dump(cd);
2501 else if (isVERITY(cd->type))
2502 return _verity_dump(cd);
2503 else if (isTCRYPT(cd->type))
2504 return TCRYPT_dump(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
2506 log_err(cd, _("Dump operation is not supported for this device type.\n"));
2511 static int _init_by_name_crypt_none(struct crypt_device *cd)
2513 struct crypt_dm_active_device dmd = {};
2516 if (cd->type || !cd->u.none.active_name)
2519 r = dm_query_device(cd, cd->u.none.active_name,
2520 DM_ACTIVE_CRYPT_CIPHER |
2521 DM_ACTIVE_CRYPT_KEYSIZE, &dmd);
2523 r = crypt_parse_name_and_mode(dmd.u.crypt.cipher,
2524 cd->u.none.cipher, NULL,
2525 cd->u.none.cipher_mode);
2528 cd->u.none.key_size = dmd.u.crypt.vk->keylength;
2530 crypt_free_volume_key(dmd.u.crypt.vk);
2531 free(CONST_CAST(void*)dmd.u.crypt.cipher);
2535 const char *crypt_get_cipher(struct crypt_device *cd)
2537 if (isPLAIN(cd->type))
2538 return cd->u.plain.cipher;
2540 if (isLUKS(cd->type))
2541 return cd->u.luks1.hdr.cipherName;
2543 if (isLOOPAES(cd->type))
2544 return cd->u.loopaes.cipher;
2546 if (isTCRYPT(cd->type))
2547 return cd->u.tcrypt.params.cipher;
2549 if (!cd->type && !_init_by_name_crypt_none(cd))
2550 return cd->u.none.cipher;
2555 const char *crypt_get_cipher_mode(struct crypt_device *cd)
2557 if (isPLAIN(cd->type))
2558 return cd->u.plain.cipher_mode;
2560 if (isLUKS(cd->type))
2561 return cd->u.luks1.hdr.cipherMode;
2563 if (isLOOPAES(cd->type))
2564 return cd->u.loopaes.cipher_mode;
2566 if (isTCRYPT(cd->type))
2567 return cd->u.tcrypt.params.mode;
2569 if (!cd->type && !_init_by_name_crypt_none(cd))
2570 return cd->u.none.cipher_mode;
2575 const char *crypt_get_uuid(struct crypt_device *cd)
2577 if (isLUKS(cd->type))
2578 return cd->u.luks1.hdr.uuid;
2580 if (isVERITY(cd->type))
2581 return cd->u.verity.uuid;
2586 const char *crypt_get_device_name(struct crypt_device *cd)
2588 const char *path = device_block_path(cd->device);
2591 path = device_path(cd->device);
2596 int crypt_get_volume_key_size(struct crypt_device *cd)
2598 if (isPLAIN(cd->type))
2599 return cd->u.plain.key_size;
2601 if (isLUKS(cd->type))
2602 return cd->u.luks1.hdr.keyBytes;
2604 if (isLOOPAES(cd->type))
2605 return cd->u.loopaes.key_size;
2607 if (isVERITY(cd->type))
2608 return cd->u.verity.root_hash_size;
2610 if (isTCRYPT(cd->type))
2611 return cd->u.tcrypt.params.key_size;
2613 if (!cd->type && !_init_by_name_crypt_none(cd))
2614 return cd->u.none.key_size;
2619 uint64_t crypt_get_data_offset(struct crypt_device *cd)
2621 if (isPLAIN(cd->type))
2622 return cd->u.plain.hdr.offset;
2624 if (isLUKS(cd->type))
2625 return cd->u.luks1.hdr.payloadOffset;
2627 if (isLOOPAES(cd->type))
2628 return cd->u.loopaes.hdr.offset;
2630 if (isTCRYPT(cd->type))
2631 return TCRYPT_get_data_offset(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
2636 uint64_t crypt_get_iv_offset(struct crypt_device *cd)
2638 if (isPLAIN(cd->type))
2639 return cd->u.plain.hdr.skip;
2641 if (isLUKS(cd->type))
2644 if (isLOOPAES(cd->type))
2645 return cd->u.loopaes.hdr.skip;
2647 if (isTCRYPT(cd->type))
2648 return TCRYPT_get_iv_offset(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
2653 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
2655 if (onlyLUKS(cd) < 0)
2656 return CRYPT_SLOT_INVALID;
2658 return LUKS_keyslot_info(&cd->u.luks1.hdr, keyslot);
2661 int crypt_keyslot_max(const char *type)
2663 if (type && isLUKS(type))
2664 return LUKS_NUMKEYS;
2669 int crypt_keyslot_area(struct crypt_device *cd,
2674 if (!isLUKS(cd->type))
2677 return LUKS_keyslot_area(&cd->u.luks1.hdr, keyslot, offset, length);
2680 const char *crypt_get_type(struct crypt_device *cd)
2685 int crypt_get_verity_info(struct crypt_device *cd,
2686 struct crypt_params_verity *vp)
2688 if (!isVERITY(cd->type) || !vp)
2691 vp->data_device = device_path(cd->device);
2692 vp->hash_device = mdata_device_path(cd);
2693 vp->hash_name = cd->u.verity.hdr.hash_name;
2694 vp->salt = cd->u.verity.hdr.salt;
2695 vp->salt_size = cd->u.verity.hdr.salt_size;
2696 vp->data_block_size = cd->u.verity.hdr.data_block_size;
2697 vp->hash_block_size = cd->u.verity.hdr.hash_block_size;
2698 vp->data_size = cd->u.verity.hdr.data_size;
2699 vp->hash_area_offset = cd->u.verity.hdr.hash_area_offset;
2700 vp->hash_type = cd->u.verity.hdr.hash_type;
2701 vp->flags = cd->u.verity.hdr.flags & CRYPT_VERITY_NO_HEADER;
2705 int crypt_get_active_device(struct crypt_device *cd, const char *name,
2706 struct crypt_active_device *cad)
2708 struct crypt_dm_active_device dmd;
2711 r = dm_query_device(cd, name, 0, &dmd);
2715 if (dmd.target != DM_CRYPT && dmd.target != DM_VERITY)
2718 if (cd && isTCRYPT(cd->type)) {
2719 cad->offset = TCRYPT_get_data_offset(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
2720 cad->iv_offset = TCRYPT_get_iv_offset(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
2722 cad->offset = dmd.u.crypt.offset;
2723 cad->iv_offset = dmd.u.crypt.iv_offset;
2725 cad->size = dmd.size;
2726 cad->flags = dmd.flags;