2 * libcryptsetup - cryptsetup library
4 * Copyright (C) 2004, Christophe Saout <christophe@saout.de>
5 * Copyright (C) 2004-2007, Clemens Fruhwirth <clemens@endorphin.org>
6 * Copyright (C) 2009-2012, Red Hat, Inc. All rights reserved.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include "libcryptsetup.h"
39 struct device *device;
40 struct device *metadata_device;
42 struct volume_key *volume_key;
44 uint64_t iteration_time;
49 // FIXME: private binary headers and access it properly
50 // through sub-library (LUKS1, TCRYPT)
53 struct { /* used in CRYPT_LUKS1 */
55 uint64_t PBKDF2_per_sec;
57 struct { /* used in CRYPT_PLAIN */
58 struct crypt_params_plain hdr;
62 unsigned int key_size;
64 struct { /* used in CRYPT_LOOPAES */
65 struct crypt_params_loopaes hdr;
69 unsigned int key_size;
71 struct { /* used in CRYPT_VERITY */
72 struct crypt_params_verity hdr;
74 unsigned int root_hash_size;
77 struct { /* used in CRYPT_TCRYPT */
78 struct crypt_params_tcrypt params;
79 struct tcrypt_phdr hdr;
83 /* callbacks definitions */
84 void (*log)(int level, const char *msg, void *usrptr);
86 int (*confirm)(const char *msg, void *usrptr);
88 int (*password)(const char *msg, char *buf, size_t length, void *usrptr);
89 void *password_usrptr;
91 /* last error message */
92 char error[MAX_ERROR_LENGTH];
96 /* FIXME: not thread safe, remove this later */
97 static char global_error[MAX_ERROR_LENGTH] = {0};
100 static void (*_default_log)(int level, const char *msg, void *usrptr) = NULL;
101 static int _debug_level = 0;
103 void crypt_set_debug_level(int level)
105 _debug_level = level;
108 int crypt_get_debug_level(void)
113 static void crypt_set_error(struct crypt_device *cd, const char *error)
115 size_t size = strlen(error);
117 /* Set global error, ugly hack... */
118 strncpy(global_error, error, MAX_ERROR_LENGTH - 2);
119 if (size < MAX_ERROR_LENGTH && global_error[size - 1] == '\n')
120 global_error[size - 1] = '\0';
122 /* Set error string per context */
124 strncpy(cd->error, error, MAX_ERROR_LENGTH - 2);
125 if (size < MAX_ERROR_LENGTH && cd->error[size - 1] == '\n')
126 cd->error[size - 1] = '\0';
130 void crypt_log(struct crypt_device *cd, int level, const char *msg)
133 cd->log(level, msg, cd->log_usrptr);
134 else if (_default_log)
135 _default_log(level, msg, NULL);
137 if (level == CRYPT_LOG_ERROR)
138 crypt_set_error(cd, msg);
141 __attribute__((format(printf, 5, 6)))
142 void logger(struct crypt_device *cd, int level, const char *file,
143 int line, const char *format, ...)
148 va_start(argp, format);
150 if (vasprintf(&target, format, argp) > 0 ) {
152 crypt_log(cd, level, target);
154 } else if (_debug_level)
155 printf("# %s:%d %s\n", file ?: "?", line, target);
157 } else if (_debug_level)
158 printf("# %s\n", target);
166 static const char *mdata_device_path(struct crypt_device *cd)
168 return device_path(cd->metadata_device ?: cd->device);
172 struct device *crypt_metadata_device(struct crypt_device *cd)
174 return cd->metadata_device ?: cd->device;
177 struct device *crypt_data_device(struct crypt_device *cd)
182 int init_crypto(struct crypt_device *ctx)
186 crypt_fips_libcryptsetup_check(ctx);
188 r = crypt_random_init(ctx);
190 log_err(ctx, _("Cannot initialize crypto RNG backend.\n"));
194 r = crypt_backend_init(ctx);
196 log_err(ctx, _("Cannot initialize crypto backend.\n"));
198 log_dbg("Crypto backend (%s) initialized.", crypt_backend_version());
202 static int process_key(struct crypt_device *cd, const char *hash_name,
203 size_t key_size, const char *pass, size_t passLen,
204 struct volume_key **vk)
211 *vk = crypt_alloc_volume_key(key_size, NULL);
216 r = crypt_plain_hash(cd, hash_name, (*vk)->key, key_size, pass, passLen);
219 log_err(cd, _("Hash algorithm %s not supported.\n"),
222 log_err(cd, _("Key processing error (using hash %s).\n"),
224 crypt_free_volume_key(*vk);
228 } else if (passLen > key_size) {
229 memcpy((*vk)->key, pass, key_size);
231 memcpy((*vk)->key, pass, passLen);
237 static int isPLAIN(const char *type)
239 return (type && !strcmp(CRYPT_PLAIN, type));
242 static int isLUKS(const char *type)
244 return (type && !strcmp(CRYPT_LUKS1, type));
247 static int isLOOPAES(const char *type)
249 return (type && !strcmp(CRYPT_LOOPAES, type));
252 static int isVERITY(const char *type)
254 return (type && !strcmp(CRYPT_VERITY, type));
257 static int isTCRYPT(const char *type)
259 return (type && !strcmp(CRYPT_TCRYPT, type));
262 /* keyslot helpers */
263 static int keyslot_verify_or_find_empty(struct crypt_device *cd, int *keyslot)
265 if (*keyslot == CRYPT_ANY_SLOT) {
266 *keyslot = LUKS_keyslot_find_empty(&cd->u.luks1.hdr);
268 log_err(cd, _("All key slots full.\n"));
273 switch (LUKS_keyslot_info(&cd->u.luks1.hdr, *keyslot)) {
274 case CRYPT_SLOT_INVALID:
275 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
276 *keyslot, LUKS_NUMKEYS - 1);
278 case CRYPT_SLOT_INACTIVE:
281 log_err(cd, _("Key slot %d is full, please select another one.\n"),
290 * compares UUIDs returned by device-mapper (striped by cryptsetup) and uuid in header
292 static int crypt_uuid_cmp(const char *dm_uuid, const char *hdr_uuid)
297 if (!dm_uuid || !hdr_uuid)
300 str = strchr(dm_uuid, '-');
304 for (i = 0, j = 1; hdr_uuid[i]; i++) {
305 if (hdr_uuid[i] == '-')
308 if (!str[j] || str[j] == '-')
311 if (str[j] != hdr_uuid[i])
319 int PLAIN_activate(struct crypt_device *cd,
321 struct volume_key *vk,
326 char *dm_cipher = NULL;
327 enum devcheck device_check;
328 struct crypt_dm_active_device dmd = {
330 .uuid = crypt_get_uuid(cd),
333 .data_device = crypt_data_device(cd),
337 .offset = crypt_get_data_offset(cd),
338 .iv_offset = crypt_get_iv_offset(cd),
342 if (dmd.flags & CRYPT_ACTIVATE_SHARED)
343 device_check = DEV_SHARED;
345 device_check = DEV_EXCL;
347 r = device_block_adjust(cd, dmd.data_device, device_check,
348 dmd.u.crypt.offset, &dmd.size, &dmd.flags);
352 if (crypt_get_cipher_mode(cd))
353 r = asprintf(&dm_cipher, "%s-%s", crypt_get_cipher(cd), crypt_get_cipher_mode(cd));
355 r = asprintf(&dm_cipher, "%s", crypt_get_cipher(cd));
359 dmd.u.crypt.cipher = dm_cipher;
360 log_dbg("Trying to activate PLAIN device %s using cipher %s.",
361 name, dmd.u.crypt.cipher);
363 r = dm_create_device(cd, name, CRYPT_PLAIN, &dmd, 0);
366 if (!cd->u.plain.uuid && dm_query_device(cd, name, DM_ACTIVE_UUID, &dmd) >= 0)
367 cd->u.plain.uuid = CONST_CAST(char*)dmd.uuid;
373 int crypt_confirm(struct crypt_device *cd, const char *msg)
375 if (!cd || !cd->confirm)
378 return cd->confirm(msg, cd->confirm_usrptr);
381 static int key_from_terminal(struct crypt_device *cd, char *msg, char **key,
382 size_t *key_len, int force_verify)
384 char *prompt = NULL, *device_name;
389 if (crypt_loop_device(crypt_get_device_name(cd)))
390 device_name = crypt_loop_backing_file(crypt_get_device_name(cd));
392 device_name = strdup(crypt_get_device_name(cd));
395 r = asprintf(&prompt, _("Enter passphrase for %s: "), device_name);
403 *key = crypt_safe_alloc(DEFAULT_PASSPHRASE_SIZE_MAX);
408 r = cd->password(msg, *key, DEFAULT_PASSPHRASE_SIZE_MAX,
409 cd->password_usrptr);
411 crypt_safe_free(*key);
416 r = crypt_get_key(msg, key, key_len, 0, 0, NULL, cd->timeout,
417 (force_verify || cd->password_verify), cd);
420 return (r < 0) ? r: 0;
423 static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslot,
424 struct volume_key **vk)
426 char *passphrase_read = NULL;
427 size_t passphrase_size_read;
428 int r = -EINVAL, eperm = 0, tries = cd->tries;
432 crypt_free_volume_key(*vk);
435 r = key_from_terminal(cd, NULL, &passphrase_read,
436 &passphrase_size_read, 0);
437 /* Continue if it is just passphrase verify mismatch */
443 r = LUKS_open_key_with_hdr(keyslot, passphrase_read,
444 passphrase_size_read, &cd->u.luks1.hdr, vk, cd);
447 crypt_safe_free(passphrase_read);
448 passphrase_read = NULL;
449 } while (r == -EPERM && (--tries > 0));
452 crypt_free_volume_key(*vk);
455 /* Report wrong passphrase if at least one try failed */
456 if (eperm && r == -EPIPE)
460 crypt_safe_free(passphrase_read);
464 static int key_from_file(struct crypt_device *cd, char *msg,
465 char **key, size_t *key_len,
466 const char *key_file, size_t key_offset,
469 return crypt_get_key(msg, key, key_len, key_offset, key_size, key_file,
473 void crypt_set_log_callback(struct crypt_device *cd,
474 void (*log)(int level, const char *msg, void *usrptr),
481 cd->log_usrptr = usrptr;
485 void crypt_set_confirm_callback(struct crypt_device *cd,
486 int (*confirm)(const char *msg, void *usrptr),
489 cd->confirm = confirm;
490 cd->confirm_usrptr = usrptr;
493 void crypt_set_password_callback(struct crypt_device *cd,
494 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
497 cd->password = password;
498 cd->password_usrptr = usrptr;
501 static void _get_error(char *error, char *buf, size_t size)
503 if (!buf || size < 1)
506 strncpy(buf, error, size - 1);
507 buf[size - 1] = '\0';
513 void crypt_last_error(struct crypt_device *cd, char *buf, size_t size)
516 return _get_error(cd->error, buf, size);
519 /* Deprecated global error interface */
520 void crypt_get_error(char *buf, size_t size)
522 return _get_error(global_error, buf, size);
525 const char *crypt_get_dir(void)
530 int crypt_init(struct crypt_device **cd, const char *device)
532 struct crypt_device *h = NULL;
538 log_dbg("Allocating crypt device %s context.", device);
540 if (!(h = malloc(sizeof(struct crypt_device))))
543 memset(h, 0, sizeof(*h));
545 r = device_alloc(&h->device, device);
551 h->iteration_time = 1000;
552 h->password_verify = 0;
554 h->rng_type = crypt_random_default_key_rng();
558 device_free(h->device);
563 static int crypt_check_data_device_size(struct crypt_device *cd)
566 uint64_t size, size_min;
568 /* Check data device size, require at least one sector */
569 size_min = crypt_get_data_offset(cd) << SECTOR_SHIFT ?: SECTOR_SIZE;
571 r = device_size(cd->device, &size);
575 if (size < size_min) {
576 log_err(cd, _("Header detected but device %s is too small.\n"),
577 device_path(cd->device));
584 int crypt_set_data_device(struct crypt_device *cd, const char *device)
586 struct device *dev = NULL;
589 log_dbg("Setting ciphertext data device to %s.", device ?: "(none)");
591 if (!isLUKS(cd->type) && !isVERITY(cd->type)) {
592 log_err(cd, _("This operation is not supported for this device type.\n"));
596 /* metadata device must be set */
597 if (!cd->device || !device)
600 r = device_alloc(&dev, device);
604 if (!cd->metadata_device) {
605 cd->metadata_device = cd->device;
607 device_free(cd->device);
611 return crypt_check_data_device_size(cd);
614 static int _crypt_load_luks1(struct crypt_device *cd, int require_header, int repair)
616 struct luks_phdr hdr;
623 r = LUKS_read_phdr(&hdr, require_header, repair, cd);
627 if (!cd->type && !(cd->type = strdup(CRYPT_LUKS1)))
630 memcpy(&cd->u.luks1.hdr, &hdr, sizeof(hdr));
635 static int _crypt_load_tcrypt(struct crypt_device *cd, struct crypt_params_tcrypt *params)
646 memcpy(&cd->u.tcrypt.params, params, sizeof(*params));
648 r = TCRYPT_read_phdr(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
650 cd->u.tcrypt.params.passphrase = NULL;
651 cd->u.tcrypt.params.passphrase_size = 0;
652 cd->u.tcrypt.params.keyfiles = NULL;
653 cd->u.tcrypt.params.keyfiles_count = 0;
658 if (!cd->type && !(cd->type = strdup(CRYPT_TCRYPT)))
664 static int _crypt_load_verity(struct crypt_device *cd, struct crypt_params_verity *params)
667 size_t sb_offset = 0;
673 if (params && params->flags & CRYPT_VERITY_NO_HEADER)
677 sb_offset = params->hash_area_offset;
679 r = VERITY_read_sb(cd, sb_offset, &cd->u.verity.uuid, &cd->u.verity.hdr);
684 cd->u.verity.hdr.flags = params->flags;
686 /* Hash availability checked in sb load */
687 cd->u.verity.root_hash_size = crypt_hash_size(cd->u.verity.hdr.hash_name);
688 if (cd->u.verity.root_hash_size > 4096)
691 if (!cd->type && !(cd->type = strdup(CRYPT_VERITY)))
694 if (params && params->data_device &&
695 (r = crypt_set_data_device(cd, params->data_device)) < 0)
701 static int _init_by_name_crypt(struct crypt_device *cd, const char *name)
703 struct crypt_dm_active_device dmd = {};
704 char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
707 r = dm_query_device(cd, name,
710 DM_ACTIVE_CRYPT_CIPHER |
711 DM_ACTIVE_CRYPT_KEYSIZE, &dmd);
715 if (isPLAIN(cd->type)) {
716 cd->u.plain.uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
717 cd->u.plain.hdr.hash = NULL; /* no way to get this */
718 cd->u.plain.hdr.offset = dmd.u.crypt.offset;
719 cd->u.plain.hdr.skip = dmd.u.crypt.iv_offset;
720 cd->u.plain.key_size = dmd.u.crypt.vk->keylength;
722 r = crypt_parse_name_and_mode(dmd.u.crypt.cipher, cipher, NULL, cipher_mode);
724 cd->u.plain.cipher = strdup(cipher);
725 cd->u.plain.cipher_mode = strdup(cipher_mode);
727 } else if (isLOOPAES(cd->type)) {
728 cd->u.loopaes.uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
729 cd->u.loopaes.hdr.offset = dmd.u.crypt.offset;
731 r = crypt_parse_name_and_mode(dmd.u.crypt.cipher, cipher,
732 &key_nums, cipher_mode);
734 cd->u.loopaes.cipher = strdup(cipher);
735 cd->u.loopaes.cipher_mode = strdup(cipher_mode);
736 /* version 3 uses last key for IV */
737 if (dmd.u.crypt.vk->keylength % key_nums)
739 cd->u.loopaes.key_size = dmd.u.crypt.vk->keylength / key_nums;
741 } else if (isLUKS(cd->type)) {
742 if (crypt_metadata_device(cd)) {
743 r = _crypt_load_luks1(cd, 0, 0);
745 log_dbg("LUKS device header does not match active device.");
751 /* check whether UUIDs match each other */
752 r = crypt_uuid_cmp(dmd.uuid, cd->u.luks1.hdr.uuid);
754 log_dbg("LUKS device header uuid: %s mismatches DM returned uuid %s",
755 cd->u.luks1.hdr.uuid, dmd.uuid);
762 } else if (isTCRYPT(cd->type)) {
763 r = TCRYPT_init_by_name(cd, name, &dmd, &cd->device,
764 &cd->u.tcrypt.params, &cd->u.tcrypt.hdr);
767 crypt_free_volume_key(dmd.u.crypt.vk);
768 device_free(dmd.data_device);
769 free(CONST_CAST(void*)dmd.u.crypt.cipher);
770 free(CONST_CAST(void*)dmd.uuid);
774 static int _init_by_name_verity(struct crypt_device *cd, const char *name)
776 struct crypt_params_verity params = {};
777 struct crypt_dm_active_device dmd = {
779 .u.verity.vp = ¶ms,
783 r = dm_query_device(cd, name,
786 DM_ACTIVE_VERITY_HASH_DEVICE |
787 DM_ACTIVE_VERITY_PARAMS, &dmd);
791 if (isVERITY(cd->type)) {
792 cd->u.verity.uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
793 cd->u.verity.hdr.flags = CRYPT_VERITY_NO_HEADER; //FIXME
794 cd->u.verity.hdr.data_size = params.data_size;
795 cd->u.verity.root_hash_size = dmd.u.verity.root_hash_size;
796 cd->u.verity.root_hash = NULL;
797 cd->u.verity.hdr.hash_name = params.hash_name;
798 cd->u.verity.hdr.data_device = NULL;
799 cd->u.verity.hdr.hash_device = NULL;
800 cd->u.verity.hdr.data_block_size = params.data_block_size;
801 cd->u.verity.hdr.hash_block_size = params.hash_block_size;
802 cd->u.verity.hdr.hash_area_offset = dmd.u.verity.hash_offset;
803 cd->u.verity.hdr.hash_type = params.hash_type;
804 cd->u.verity.hdr.flags = params.flags;
805 cd->u.verity.hdr.salt_size = params.salt_size;
806 cd->u.verity.hdr.salt = params.salt;
807 cd->metadata_device = dmd.u.verity.hash_device;
810 device_free(dmd.data_device);
811 free(CONST_CAST(void*)dmd.uuid);
815 int crypt_init_by_name_and_header(struct crypt_device **cd,
817 const char *header_device)
819 crypt_status_info ci;
820 struct crypt_dm_active_device dmd;
823 log_dbg("Allocating crypt device context by device %s.", name);
825 ci = crypt_status(NULL, name);
826 if (ci == CRYPT_INVALID)
829 if (ci < CRYPT_ACTIVE) {
830 log_err(NULL, _("Device %s is not active.\n"), name);
834 r = dm_query_device(NULL, name, DM_ACTIVE_DEVICE | DM_ACTIVE_UUID, &dmd);
841 r = crypt_init(cd, header_device);
843 r = crypt_init(cd, device_path(dmd.data_device));
845 /* Underlying device disappeared but mapping still active */
846 if (!dmd.data_device || r == -ENOTBLK)
847 log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
850 /* Underlying device is not readable but crypt mapping exists */
852 device_free(dmd.data_device);
853 dmd.data_device = NULL;
854 r = crypt_init(cd, NULL);
862 if (!strncmp(CRYPT_PLAIN, dmd.uuid, sizeof(CRYPT_PLAIN)-1))
863 (*cd)->type = strdup(CRYPT_PLAIN);
864 else if (!strncmp(CRYPT_LOOPAES, dmd.uuid, sizeof(CRYPT_LOOPAES)-1))
865 (*cd)->type = strdup(CRYPT_LOOPAES);
866 else if (!strncmp(CRYPT_LUKS1, dmd.uuid, sizeof(CRYPT_LUKS1)-1))
867 (*cd)->type = strdup(CRYPT_LUKS1);
868 else if (!strncmp(CRYPT_VERITY, dmd.uuid, sizeof(CRYPT_VERITY)-1))
869 (*cd)->type = strdup(CRYPT_VERITY);
870 else if (!strncmp(CRYPT_TCRYPT, dmd.uuid, sizeof(CRYPT_TCRYPT)-1))
871 (*cd)->type = strdup(CRYPT_TCRYPT);
873 log_dbg("Unknown UUID set, some parameters are not set.");
875 log_dbg("Active device has no UUID set, some parameters are not set.");
878 r = crypt_set_data_device(*cd, device_path(dmd.data_device));
883 /* Try to initialise basic parameters from active device */
885 if (dmd.target == DM_CRYPT)
886 r = _init_by_name_crypt(*cd, name);
887 else if (dmd.target == DM_VERITY)
888 r = _init_by_name_verity(*cd, name);
894 device_free(dmd.data_device);
895 free(CONST_CAST(void*)dmd.uuid);
899 int crypt_init_by_name(struct crypt_device **cd, const char *name)
901 return crypt_init_by_name_and_header(cd, name, NULL);
904 static int _crypt_format_plain(struct crypt_device *cd,
906 const char *cipher_mode,
908 size_t volume_key_size,
909 struct crypt_params_plain *params)
911 if (!cipher || !cipher_mode) {
912 log_err(cd, _("Invalid plain crypt parameters.\n"));
916 if (volume_key_size > 1024) {
917 log_err(cd, _("Invalid key size.\n"));
921 if (!(cd->type = strdup(CRYPT_PLAIN)))
924 cd->u.plain.key_size = volume_key_size;
925 cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
929 cd->u.plain.cipher = strdup(cipher);
930 cd->u.plain.cipher_mode = strdup(cipher_mode);
933 cd->u.plain.uuid = strdup(uuid);
935 if (params && params->hash)
936 cd->u.plain.hdr.hash = strdup(params->hash);
938 cd->u.plain.hdr.offset = params ? params->offset : 0;
939 cd->u.plain.hdr.skip = params ? params->skip : 0;
940 cd->u.plain.hdr.size = params ? params->size : 0;
942 if (!cd->u.plain.cipher || !cd->u.plain.cipher_mode)
948 static int _crypt_format_luks1(struct crypt_device *cd,
950 const char *cipher_mode,
952 const char *volume_key,
953 size_t volume_key_size,
954 struct crypt_params_luks1 *params)
957 unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
958 unsigned long alignment_offset = 0;
960 if (!crypt_metadata_device(cd)) {
961 log_err(cd, _("Can't format LUKS without device.\n"));
965 if (!(cd->type = strdup(CRYPT_LUKS1)))
969 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
972 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
977 if (params && params->data_device) {
978 cd->metadata_device = cd->device;
980 if (device_alloc(&cd->device, params->data_device) < 0)
982 required_alignment = params->data_alignment * SECTOR_SIZE;
983 } else if (params && params->data_alignment) {
984 required_alignment = params->data_alignment * SECTOR_SIZE;
986 device_topology_alignment(cd->device,
988 &alignment_offset, DEFAULT_DISK_ALIGNMENT);
990 /* Check early if we cannot allocate block device for key slot access */
991 r = device_block_adjust(cd, cd->device, DEV_OK, 0, NULL, NULL);
995 r = LUKS_generate_phdr(&cd->u.luks1.hdr, cd->volume_key, cipher, cipher_mode,
996 (params && params->hash) ? params->hash : "sha1",
998 required_alignment / SECTOR_SIZE,
999 alignment_offset / SECTOR_SIZE,
1000 cd->iteration_time, &cd->u.luks1.PBKDF2_per_sec,
1001 cd->metadata_device ? 1 : 0, cd);
1005 /* Wipe first 8 sectors - fs magic numbers etc. */
1006 r = crypt_wipe(crypt_metadata_device(cd), 0, 8 * SECTOR_SIZE, CRYPT_WIPE_ZERO, 1);
1009 log_err(cd, _("Cannot format device %s which is still in use.\n"),
1010 mdata_device_path(cd));
1011 else if (r == -EACCES) {
1012 log_err(cd, _("Cannot format device %s, permission denied.\n"),
1013 mdata_device_path(cd));
1016 log_err(cd, _("Cannot wipe header on device %s.\n"),
1017 mdata_device_path(cd));
1022 r = LUKS_write_phdr(&cd->u.luks1.hdr, cd);
1027 static int _crypt_format_loopaes(struct crypt_device *cd,
1030 size_t volume_key_size,
1031 struct crypt_params_loopaes *params)
1033 if (!crypt_metadata_device(cd)) {
1034 log_err(cd, _("Can't format LOOPAES without device.\n"));
1038 if (volume_key_size > 1024) {
1039 log_err(cd, _("Invalid key size.\n"));
1043 if (!(cd->type = strdup(CRYPT_LOOPAES)))
1046 cd->u.loopaes.key_size = volume_key_size;
1048 cd->u.loopaes.cipher = strdup(cipher ?: DEFAULT_LOOPAES_CIPHER);
1051 cd->u.loopaes.uuid = strdup(uuid);
1053 if (params && params->hash)
1054 cd->u.loopaes.hdr.hash = strdup(params->hash);
1056 cd->u.loopaes.hdr.offset = params ? params->offset : 0;
1057 cd->u.loopaes.hdr.skip = params ? params->skip : 0;
1062 static int _crypt_format_verity(struct crypt_device *cd,
1064 struct crypt_params_verity *params)
1066 int r = 0, hash_size;
1067 uint64_t data_device_size;
1069 if (!crypt_metadata_device(cd)) {
1070 log_err(cd, _("Can't format VERITY without device.\n"));
1074 if (!params || !params->data_device)
1077 if (params->hash_type > VERITY_MAX_HASH_TYPE) {
1078 log_err(cd, _("Unsupported VERITY hash type %d.\n"), params->hash_type);
1082 if (VERITY_BLOCK_SIZE_OK(params->data_block_size) ||
1083 VERITY_BLOCK_SIZE_OK(params->hash_block_size)) {
1084 log_err(cd, _("Unsupported VERITY block size.\n"));
1088 if (params->hash_area_offset % 512) {
1089 log_err(cd, _("Unsupported VERITY hash offset.\n"));
1093 if (!(cd->type = strdup(CRYPT_VERITY)))
1096 r = crypt_set_data_device(cd, params->data_device);
1099 if (!params->data_size) {
1100 r = device_size(cd->device, &data_device_size);
1104 cd->u.verity.hdr.data_size = data_device_size / params->data_block_size;
1106 cd->u.verity.hdr.data_size = params->data_size;
1108 hash_size = crypt_hash_size(params->hash_name);
1109 if (hash_size <= 0) {
1110 log_err(cd, _("Hash algorithm %s not supported.\n"),
1114 cd->u.verity.root_hash_size = hash_size;
1116 cd->u.verity.root_hash = malloc(cd->u.verity.root_hash_size);
1117 if (!cd->u.verity.root_hash)
1120 cd->u.verity.hdr.flags = params->flags;
1121 if (!(cd->u.verity.hdr.hash_name = strdup(params->hash_name)))
1123 cd->u.verity.hdr.data_device = NULL;
1124 cd->u.verity.hdr.data_block_size = params->data_block_size;
1125 cd->u.verity.hdr.hash_block_size = params->hash_block_size;
1126 cd->u.verity.hdr.hash_area_offset = params->hash_area_offset;
1127 cd->u.verity.hdr.hash_type = params->hash_type;
1128 cd->u.verity.hdr.flags = params->flags;
1129 cd->u.verity.hdr.salt_size = params->salt_size;
1130 if (!(cd->u.verity.hdr.salt = malloc(params->salt_size)))
1134 memcpy(CONST_CAST(char*)cd->u.verity.hdr.salt, params->salt,
1137 r = crypt_random_get(cd, CONST_CAST(char*)cd->u.verity.hdr.salt,
1138 params->salt_size, CRYPT_RND_SALT);
1142 if (params->flags & CRYPT_VERITY_CREATE_HASH) {
1143 r = VERITY_create(cd, &cd->u.verity.hdr,
1144 cd->u.verity.root_hash, cd->u.verity.root_hash_size);
1149 if (!(params->flags & CRYPT_VERITY_NO_HEADER)) {
1151 cd->u.verity.uuid = strdup(uuid);
1153 r = VERITY_UUID_generate(cd, &cd->u.verity.uuid);
1158 r = VERITY_write_sb(cd, cd->u.verity.hdr.hash_area_offset,
1165 int crypt_format(struct crypt_device *cd,
1168 const char *cipher_mode,
1170 const char *volume_key,
1171 size_t volume_key_size,
1180 log_dbg("Context already formatted as %s.", cd->type);
1184 log_dbg("Formatting device %s as type %s.", mdata_device_path(cd) ?: "(none)", type);
1186 r = init_crypto(cd);
1191 r = _crypt_format_plain(cd, cipher, cipher_mode,
1192 uuid, volume_key_size, params);
1193 else if (isLUKS(type))
1194 r = _crypt_format_luks1(cd, cipher, cipher_mode,
1195 uuid, volume_key, volume_key_size, params);
1196 else if (isLOOPAES(type))
1197 r = _crypt_format_loopaes(cd, cipher, uuid, volume_key_size, params);
1198 else if (isVERITY(type))
1199 r = _crypt_format_verity(cd, uuid, params);
1201 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
1208 crypt_free_volume_key(cd->volume_key);
1209 cd->volume_key = NULL;
1215 int crypt_load(struct crypt_device *cd,
1216 const char *requested_type,
1221 log_dbg("Trying to load %s crypt type from device %s.",
1222 requested_type ?: "any", mdata_device_path(cd) ?: "(none)");
1224 if (!crypt_metadata_device(cd))
1227 if (!requested_type || isLUKS(requested_type)) {
1228 if (cd->type && !isLUKS(cd->type)) {
1229 log_dbg("Context is already initialised to type %s", cd->type);
1233 r = _crypt_load_luks1(cd, 1, 0);
1234 } else if (isVERITY(requested_type)) {
1235 if (cd->type && !isVERITY(cd->type)) {
1236 log_dbg("Context is already initialised to type %s", cd->type);
1239 r = _crypt_load_verity(cd, params);
1240 } else if (isTCRYPT(requested_type)) {
1241 if (cd->type && !isTCRYPT(cd->type)) {
1242 log_dbg("Context is already initialised to type %s", cd->type);
1245 r = _crypt_load_tcrypt(cd, params);
1252 int crypt_repair(struct crypt_device *cd,
1253 const char *requested_type,
1254 void *params __attribute__((unused)))
1258 log_dbg("Trying to repair %s crypt type from device %s.",
1259 requested_type ?: "any", mdata_device_path(cd) ?: "(none)");
1261 if (!crypt_metadata_device(cd))
1264 if (requested_type && !isLUKS(requested_type))
1268 /* Load with repair */
1269 r = _crypt_load_luks1(cd, 1, 1);
1273 /* cd->type and header must be set in context */
1274 r = crypt_check_data_device_size(cd);
1283 int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
1285 struct crypt_dm_active_device dmd;
1288 /* Device context type must be initialised */
1289 if (!cd->type || !crypt_get_uuid(cd))
1292 log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
1294 r = dm_query_device(cd, name, DM_ACTIVE_DEVICE | DM_ACTIVE_CRYPT_CIPHER |
1295 DM_ACTIVE_UUID | DM_ACTIVE_CRYPT_KEYSIZE |
1296 DM_ACTIVE_CRYPT_KEY, &dmd);
1298 log_err(NULL, _("Device %s is not active.\n"), name);
1302 if (!dmd.uuid || dmd.target != DM_CRYPT) {
1307 r = device_block_adjust(cd, dmd.data_device, DEV_OK,
1308 dmd.u.crypt.offset, &new_size, &dmd.flags);
1312 if (new_size == dmd.size) {
1313 log_dbg("Device has already requested size %" PRIu64
1314 " sectors.", dmd.size);
1317 dmd.size = new_size;
1318 if (isTCRYPT(cd->type))
1321 r = dm_create_device(cd, name, cd->type, &dmd, 1);
1324 if (dmd.target == DM_CRYPT) {
1325 crypt_free_volume_key(dmd.u.crypt.vk);
1326 free(CONST_CAST(void*)dmd.u.crypt.cipher);
1328 free(CONST_CAST(void*)dmd.data_device);
1329 free(CONST_CAST(void*)dmd.uuid);
1334 int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
1336 if (!isLUKS(cd->type)) {
1337 log_err(cd, _("This operation is not supported for this device type.\n"));
1341 if (uuid && !strncmp(uuid, cd->u.luks1.hdr.uuid, sizeof(cd->u.luks1.hdr.uuid))) {
1342 log_dbg("UUID is the same as requested (%s) for device %s.",
1343 uuid, mdata_device_path(cd));
1348 log_dbg("Requested new UUID change to %s for %s.", uuid, mdata_device_path(cd));
1350 log_dbg("Requested new UUID refresh for %s.", mdata_device_path(cd));
1352 if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
1355 return LUKS_hdr_uuid_set(&cd->u.luks1.hdr, uuid, cd);
1358 int crypt_header_backup(struct crypt_device *cd,
1359 const char *requested_type,
1360 const char *backup_file)
1364 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1367 r = init_crypto(cd);
1371 log_dbg("Requested header backup of device %s (%s) to "
1372 "file %s.", mdata_device_path(cd), requested_type, backup_file);
1374 return LUKS_hdr_backup(backup_file, &cd->u.luks1.hdr, cd);
1377 int crypt_header_restore(struct crypt_device *cd,
1378 const char *requested_type,
1379 const char *backup_file)
1383 if (requested_type && !isLUKS(requested_type))
1386 if (cd->type && !isLUKS(cd->type))
1389 r = init_crypto(cd);
1393 log_dbg("Requested header restore to device %s (%s) from "
1394 "file %s.", mdata_device_path(cd), requested_type, backup_file);
1396 return LUKS_hdr_restore(backup_file, &cd->u.luks1.hdr, cd);
1399 void crypt_free(struct crypt_device *cd)
1402 log_dbg("Releasing crypt device %s context.", mdata_device_path(cd));
1405 crypt_free_volume_key(cd->volume_key);
1407 device_free(cd->device);
1408 device_free(cd->metadata_device);
1410 if (isPLAIN(cd->type)) {
1411 free(CONST_CAST(void*)cd->u.plain.hdr.hash);
1412 free(cd->u.plain.cipher);
1413 free(cd->u.plain.cipher_mode);
1414 free(cd->u.plain.uuid);
1415 } else if (isLOOPAES(cd->type)) {
1416 free(CONST_CAST(void*)cd->u.loopaes.hdr.hash);
1417 free(cd->u.loopaes.cipher);
1418 free(cd->u.loopaes.uuid);
1419 } else if (isVERITY(cd->type)) {
1420 free(CONST_CAST(void*)cd->u.verity.hdr.hash_name);
1421 free(CONST_CAST(void*)cd->u.verity.hdr.salt);
1422 free(cd->u.verity.root_hash);
1423 free(cd->u.verity.uuid);
1427 /* Some structures can contain keys (TCRYPT), wipe it */
1428 memset(cd, 0, sizeof(*cd));
1433 int crypt_suspend(struct crypt_device *cd,
1436 crypt_status_info ci;
1439 log_dbg("Suspending volume %s.", name);
1441 if (!cd || !isLUKS(cd->type)) {
1442 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1447 ci = crypt_status(NULL, name);
1448 if (ci < CRYPT_ACTIVE) {
1449 log_err(cd, _("Volume %s is not active.\n"), name);
1455 r = dm_status_suspended(cd, name);
1460 log_err(cd, _("Volume %s is already suspended.\n"), name);
1465 r = dm_suspend_and_wipe_key(cd, name);
1467 log_err(cd, "Suspend is not supported for device %s.\n", name);
1469 log_err(cd, "Error during suspending device %s.\n", name);
1475 int crypt_resume_by_passphrase(struct crypt_device *cd,
1478 const char *passphrase,
1479 size_t passphrase_size)
1481 struct volume_key *vk = NULL;
1484 log_dbg("Resuming volume %s.", name);
1486 if (!isLUKS(cd->type)) {
1487 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1492 r = dm_status_suspended(cd, name);
1497 log_err(cd, _("Volume %s is not suspended.\n"), name);
1502 r = LUKS_open_key_with_hdr(keyslot, passphrase, passphrase_size,
1503 &cd->u.luks1.hdr, &vk, cd);
1505 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1509 r = dm_resume_and_reinstate_key(cd, name, vk->keylength, vk->key);
1511 log_err(cd, "Resume is not supported for device %s.\n", name);
1513 log_err(cd, "Error during resuming device %s.\n", name);
1517 crypt_free_volume_key(vk);
1518 return r < 0 ? r : keyslot;
1521 int crypt_resume_by_keyfile_offset(struct crypt_device *cd,
1524 const char *keyfile,
1525 size_t keyfile_size,
1526 size_t keyfile_offset)
1528 struct volume_key *vk = NULL;
1529 char *passphrase_read = NULL;
1530 size_t passphrase_size_read;
1533 log_dbg("Resuming volume %s.", name);
1535 if (!isLUKS(cd->type)) {
1536 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1541 r = dm_status_suspended(cd, name);
1546 log_err(cd, _("Volume %s is not suspended.\n"), name);
1553 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1554 &passphrase_size_read, keyfile, keyfile_offset,
1559 r = LUKS_open_key_with_hdr(keyslot, passphrase_read,
1560 passphrase_size_read, &cd->u.luks1.hdr, &vk, cd);
1565 r = dm_resume_and_reinstate_key(cd, name, vk->keylength, vk->key);
1567 log_err(cd, "Error during resuming device %s.\n", name);
1569 crypt_safe_free(passphrase_read);
1570 crypt_free_volume_key(vk);
1571 return r < 0 ? r : keyslot;
1574 int crypt_resume_by_keyfile(struct crypt_device *cd,
1577 const char *keyfile,
1578 size_t keyfile_size)
1580 return crypt_resume_by_keyfile_offset(cd, name, keyslot,
1581 keyfile, keyfile_size, 0);
1584 // slot manipulation
1585 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1586 int keyslot, // -1 any
1587 const char *passphrase, // NULL -> terminal
1588 size_t passphrase_size,
1589 const char *new_passphrase, // NULL -> terminal
1590 size_t new_passphrase_size)
1592 struct volume_key *vk = NULL;
1593 char *password = NULL, *new_password = NULL;
1594 size_t passwordLen, new_passwordLen;
1597 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1598 "new passphrase %sprovided.",
1599 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1601 if (!isLUKS(cd->type)) {
1602 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1606 r = keyslot_verify_or_find_empty(cd, &keyslot);
1610 if (!LUKS_keyslot_active_count(&cd->u.luks1.hdr)) {
1611 /* No slots used, try to use pre-generated key in header */
1612 if (cd->volume_key) {
1613 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1614 r = vk ? 0 : -ENOMEM;
1616 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1619 } else if (passphrase) {
1620 /* Passphrase provided, use it to unlock existing keyslot */
1621 r = LUKS_open_key_with_hdr(CRYPT_ANY_SLOT, passphrase,
1622 passphrase_size, &cd->u.luks1.hdr, &vk, cd);
1624 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1625 r = key_from_terminal(cd, _("Enter any passphrase: "),
1626 &password, &passwordLen, 0);
1630 r = LUKS_open_key_with_hdr(CRYPT_ANY_SLOT, password,
1631 passwordLen, &cd->u.luks1.hdr, &vk, cd);
1632 crypt_safe_free(password);
1638 if (new_passphrase) {
1639 new_password = CONST_CAST(char*)new_passphrase;
1640 new_passwordLen = new_passphrase_size;
1642 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1643 &new_password, &new_passwordLen, 1);
1648 r = LUKS_set_key(keyslot, new_password, new_passwordLen,
1649 &cd->u.luks1.hdr, vk, cd->iteration_time, &cd->u.luks1.PBKDF2_per_sec, cd);
1654 if (!new_passphrase)
1655 crypt_safe_free(new_password);
1656 crypt_free_volume_key(vk);
1657 return r ?: keyslot;
1660 int crypt_keyslot_change_by_passphrase(struct crypt_device *cd,
1663 const char *passphrase,
1664 size_t passphrase_size,
1665 const char *new_passphrase,
1666 size_t new_passphrase_size)
1668 struct volume_key *vk = NULL;
1671 log_dbg("Changing passphrase from old keyslot %d to new %d.",
1672 keyslot_old, keyslot_new);
1674 if (!isLUKS(cd->type)) {
1675 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1679 r = LUKS_open_key_with_hdr(keyslot_old, passphrase, passphrase_size,
1680 &cd->u.luks1.hdr, &vk, cd);
1684 if (keyslot_old != CRYPT_ANY_SLOT && keyslot_old != r) {
1685 log_dbg("Keyslot mismatch.");
1690 if (keyslot_new == CRYPT_ANY_SLOT) {
1691 keyslot_new = LUKS_keyslot_find_empty(&cd->u.luks1.hdr);
1692 if (keyslot_new < 0)
1693 keyslot_new = keyslot_old;
1696 if (keyslot_old == keyslot_new) {
1697 log_dbg("Key slot %d is going to be overwritten.", keyslot_old);
1698 (void)crypt_keyslot_destroy(cd, keyslot_old);
1701 r = LUKS_set_key(keyslot_new, new_passphrase, new_passphrase_size,
1702 &cd->u.luks1.hdr, vk, cd->iteration_time,
1703 &cd->u.luks1.PBKDF2_per_sec, cd);
1705 if (keyslot_old == keyslot_new) {
1707 log_verbose(cd, _("Key slot %d changed.\n"), r);
1710 log_verbose(cd, _("Replaced with key slot %d.\n"), r);
1711 r = crypt_keyslot_destroy(cd, keyslot_old);
1715 log_err(cd, _("Failed to swap new key slot.\n"));
1717 crypt_free_volume_key(vk);
1718 return r ?: keyslot_new;
1721 int crypt_keyslot_add_by_keyfile_offset(struct crypt_device *cd,
1723 const char *keyfile,
1724 size_t keyfile_size,
1725 size_t keyfile_offset,
1726 const char *new_keyfile,
1727 size_t new_keyfile_size,
1728 size_t new_keyfile_offset)
1730 struct volume_key *vk = NULL;
1731 char *password = NULL; size_t passwordLen;
1732 char *new_password = NULL; size_t new_passwordLen;
1735 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1736 keyfile ?: "[none]", new_keyfile ?: "[none]");
1738 if (!isLUKS(cd->type)) {
1739 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1743 r = keyslot_verify_or_find_empty(cd, &keyslot);
1747 if (!LUKS_keyslot_active_count(&cd->u.luks1.hdr)) {
1748 /* No slots used, try to use pre-generated key in header */
1749 if (cd->volume_key) {
1750 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1751 r = vk ? 0 : -ENOMEM;
1753 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1757 /* Read password from file of (if NULL) from terminal */
1759 r = key_from_file(cd, _("Enter any passphrase: "),
1760 &password, &passwordLen,
1761 keyfile, keyfile_offset, keyfile_size);
1763 r = key_from_terminal(cd, _("Enter any passphrase: "),
1764 &password, &passwordLen, 0);
1768 r = LUKS_open_key_with_hdr(CRYPT_ANY_SLOT, password, passwordLen,
1769 &cd->u.luks1.hdr, &vk, cd);
1776 r = key_from_file(cd, _("Enter new passphrase for key slot: "),
1777 &new_password, &new_passwordLen, new_keyfile,
1778 new_keyfile_offset, new_keyfile_size);
1780 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1781 &new_password, &new_passwordLen, 1);
1785 r = LUKS_set_key(keyslot, new_password, new_passwordLen,
1786 &cd->u.luks1.hdr, vk, cd->iteration_time, &cd->u.luks1.PBKDF2_per_sec, cd);
1788 crypt_safe_free(password);
1789 crypt_safe_free(new_password);
1790 crypt_free_volume_key(vk);
1791 return r < 0 ? r : keyslot;
1794 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1796 const char *keyfile,
1797 size_t keyfile_size,
1798 const char *new_keyfile,
1799 size_t new_keyfile_size)
1801 return crypt_keyslot_add_by_keyfile_offset(cd, keyslot,
1802 keyfile, keyfile_size, 0,
1803 new_keyfile, new_keyfile_size, 0);
1806 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1808 const char *volume_key,
1809 size_t volume_key_size,
1810 const char *passphrase,
1811 size_t passphrase_size)
1813 struct volume_key *vk = NULL;
1815 char *new_password = NULL; size_t new_passwordLen;
1817 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1819 if (!isLUKS(cd->type)) {
1820 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1825 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1826 else if (cd->volume_key)
1827 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1832 r = LUKS_verify_volume_key(&cd->u.luks1.hdr, vk);
1834 log_err(cd, _("Volume key does not match the volume.\n"));
1838 r = keyslot_verify_or_find_empty(cd, &keyslot);
1843 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1844 &new_password, &new_passwordLen, 1);
1847 passphrase = new_password;
1848 passphrase_size = new_passwordLen;
1851 r = LUKS_set_key(keyslot, passphrase, passphrase_size,
1852 &cd->u.luks1.hdr, vk, cd->iteration_time, &cd->u.luks1.PBKDF2_per_sec, cd);
1854 crypt_safe_free(new_password);
1855 crypt_free_volume_key(vk);
1856 return (r < 0) ? r : keyslot;
1859 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1861 crypt_keyslot_info ki;
1863 log_dbg("Destroying keyslot %d.", keyslot);
1865 if (!isLUKS(cd->type)) {
1866 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1870 ki = crypt_keyslot_status(cd, keyslot);
1871 if (ki == CRYPT_SLOT_INVALID) {
1872 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1876 if (ki == CRYPT_SLOT_INACTIVE) {
1877 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1881 return LUKS_del_key(keyslot, &cd->u.luks1.hdr, cd);
1884 // activation/deactivation of device mapping
1885 int crypt_activate_by_passphrase(struct crypt_device *cd,
1888 const char *passphrase,
1889 size_t passphrase_size,
1892 crypt_status_info ci;
1893 struct volume_key *vk = NULL;
1894 char *read_passphrase = NULL;
1895 size_t passphraseLen = 0;
1898 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1899 name ? "Activating" : "Checking", name ?: "",
1900 keyslot, passphrase ? "" : "[none] ");
1903 ci = crypt_status(NULL, name);
1904 if (ci == CRYPT_INVALID)
1906 else if (ci >= CRYPT_ACTIVE) {
1907 log_err(cd, _("Device %s already exists.\n"), name);
1912 /* plain, use hashed passphrase */
1913 if (isPLAIN(cd->type)) {
1918 r = key_from_terminal(cd, NULL, &read_passphrase,
1922 passphrase = read_passphrase;
1923 passphrase_size = passphraseLen;
1926 r = process_key(cd, cd->u.plain.hdr.hash,
1927 cd->u.plain.key_size,
1928 passphrase, passphrase_size, &vk);
1932 r = PLAIN_activate(cd, name, vk, cd->u.plain.hdr.size, flags);
1934 } else if (isLUKS(cd->type)) {
1935 /* provided passphrase, do not retry */
1937 r = LUKS_open_key_with_hdr(keyslot, passphrase,
1938 passphrase_size, &cd->u.luks1.hdr, &vk, cd);
1940 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1945 r = LUKS1_activate(cd, name, vk, flags);
1950 crypt_safe_free(read_passphrase);
1951 crypt_free_volume_key(vk);
1953 return r < 0 ? r : keyslot;
1956 int crypt_activate_by_keyfile_offset(struct crypt_device *cd,
1959 const char *keyfile,
1960 size_t keyfile_size,
1961 size_t keyfile_offset,
1964 crypt_status_info ci;
1965 struct volume_key *vk = NULL;
1966 char *passphrase_read = NULL;
1967 size_t passphrase_size_read;
1968 unsigned int key_count = 0;
1971 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1972 name ?: "", keyslot, keyfile ?: "[none]");
1975 ci = crypt_status(NULL, name);
1976 if (ci == CRYPT_INVALID)
1978 else if (ci >= CRYPT_ACTIVE) {
1979 log_err(cd, _("Device %s already exists.\n"), name);
1987 if (isPLAIN(cd->type)) {
1991 r = key_from_file(cd, _("Enter passphrase: "),
1992 &passphrase_read, &passphrase_size_read,
1993 keyfile, keyfile_offset, keyfile_size);
1997 r = process_key(cd, cd->u.plain.hdr.hash,
1998 cd->u.plain.key_size,
1999 passphrase_read, passphrase_size_read, &vk);
2003 r = PLAIN_activate(cd, name, vk, cd->u.plain.hdr.size, flags);
2004 } else if (isLUKS(cd->type)) {
2005 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
2006 &passphrase_size_read, keyfile, keyfile_offset, keyfile_size);
2009 r = LUKS_open_key_with_hdr(keyslot, passphrase_read,
2010 passphrase_size_read, &cd->u.luks1.hdr, &vk, cd);
2016 r = LUKS1_activate(cd, name, vk, flags);
2021 } else if (isLOOPAES(cd->type)) {
2022 r = key_from_file(cd, NULL, &passphrase_read, &passphrase_size_read,
2023 keyfile, keyfile_offset, keyfile_size);
2026 r = LOOPAES_parse_keyfile(cd, &vk, cd->u.loopaes.hdr.hash, &key_count,
2027 passphrase_read, passphrase_size_read);
2031 r = LOOPAES_activate(cd, name, cd->u.loopaes.cipher,
2032 key_count, vk, flags);
2037 crypt_safe_free(passphrase_read);
2038 crypt_free_volume_key(vk);
2043 int crypt_activate_by_keyfile(struct crypt_device *cd,
2046 const char *keyfile,
2047 size_t keyfile_size,
2050 return crypt_activate_by_keyfile_offset(cd, name, keyslot, keyfile,
2051 keyfile_size, 0, flags);
2054 int crypt_activate_by_volume_key(struct crypt_device *cd,
2056 const char *volume_key,
2057 size_t volume_key_size,
2060 crypt_status_info ci;
2061 struct volume_key *vk = NULL;
2064 log_dbg("Activating volume %s by volume key.", name ?: "[none]");
2067 ci = crypt_status(NULL, name);
2068 if (ci == CRYPT_INVALID)
2070 else if (ci >= CRYPT_ACTIVE) {
2071 log_err(cd, _("Device %s already exists.\n"), name);
2076 /* use key directly, no hash */
2077 if (isPLAIN(cd->type)) {
2081 if (!volume_key || !volume_key_size || volume_key_size != cd->u.plain.key_size) {
2082 log_err(cd, _("Incorrect volume key specified for plain device.\n"));
2086 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2090 r = PLAIN_activate(cd, name, vk, cd->u.plain.hdr.size, flags);
2091 } else if (isLUKS(cd->type)) {
2092 /* If key is not provided, try to use internal key */
2094 if (!cd->volume_key) {
2095 log_err(cd, _("Volume key does not match the volume.\n"));
2098 volume_key_size = cd->volume_key->keylength;
2099 volume_key = cd->volume_key->key;
2102 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2105 r = LUKS_verify_volume_key(&cd->u.luks1.hdr, vk);
2108 log_err(cd, _("Volume key does not match the volume.\n"));
2111 r = LUKS1_activate(cd, name, vk, flags);
2112 } else if (isVERITY(cd->type)) {
2113 /* volume_key == root hash */
2114 if (!volume_key || !volume_key_size) {
2115 log_err(cd, _("Incorrect root hash specified for verity device.\n"));
2119 r = VERITY_activate(cd, name, volume_key, volume_key_size,
2120 &cd->u.verity.hdr, CRYPT_ACTIVATE_READONLY);
2123 free(cd->u.verity.root_hash);
2124 cd->u.verity.root_hash = NULL;
2126 cd->u.verity.root_hash_size = volume_key_size;
2127 if (!cd->u.verity.root_hash)
2128 cd->u.verity.root_hash = malloc(volume_key_size);
2129 if (cd->u.verity.root_hash)
2130 memcpy(cd->u.verity.root_hash, volume_key, volume_key_size);
2132 } else if (isTCRYPT(cd->type)) {
2135 r = TCRYPT_activate(cd, name, &cd->u.tcrypt.hdr,
2136 &cd->u.tcrypt.params, flags);
2138 log_err(cd, _("Device type is not properly initialised.\n"));
2140 crypt_free_volume_key(vk);
2145 int crypt_deactivate(struct crypt_device *cd, const char *name)
2152 log_dbg("Deactivating volume %s.", name);
2157 switch (crypt_status(cd, name)) {
2160 if (cd && isTCRYPT(cd->type))
2161 r = TCRYPT_deactivate(cd, name);
2163 r = dm_remove_device(cd, name, 0, 0);
2165 case CRYPT_INACTIVE:
2166 log_err(cd, _("Device %s is not active.\n"), name);
2170 log_err(cd, _("Invalid device %s.\n"), name);
2180 int crypt_volume_key_get(struct crypt_device *cd,
2183 size_t *volume_key_size,
2184 const char *passphrase,
2185 size_t passphrase_size)
2187 struct volume_key *vk = NULL;
2191 if (crypt_fips_mode()) {
2192 log_err(cd, "Function not available in FIPS mode.\n");
2196 key_len = crypt_get_volume_key_size(cd);
2197 if (key_len > *volume_key_size) {
2198 log_err(cd, _("Volume key buffer too small.\n"));
2202 if (isPLAIN(cd->type) && cd->u.plain.hdr.hash) {
2203 r = process_key(cd, cd->u.plain.hdr.hash, key_len,
2204 passphrase, passphrase_size, &vk);
2206 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
2207 } else if (isLUKS(cd->type)) {
2208 r = LUKS_open_key_with_hdr(keyslot, passphrase,
2209 passphrase_size, &cd->u.luks1.hdr, &vk, cd);
2210 } else if (isTCRYPT(cd->type)) {
2211 r = TCRYPT_get_volume_key(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params, &vk);
2213 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
2216 memcpy(volume_key, vk->key, vk->keylength);
2217 *volume_key_size = vk->keylength;
2220 crypt_free_volume_key(vk);
2224 int crypt_volume_key_verify(struct crypt_device *cd,
2225 const char *volume_key,
2226 size_t volume_key_size)
2228 struct volume_key *vk;
2231 if (!isLUKS(cd->type)) {
2232 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2236 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2240 r = LUKS_verify_volume_key(&cd->u.luks1.hdr, vk);
2243 log_err(cd, _("Volume key does not match the volume.\n"));
2245 crypt_free_volume_key(vk);
2250 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
2252 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
2253 cd->timeout = timeout_sec;
2256 void crypt_set_password_retry(struct crypt_device *cd, int tries)
2258 log_dbg("Password retry count set to %d.", tries);
2262 void crypt_set_iteration_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2264 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
2265 cd->iteration_time = iteration_time_ms;
2267 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2269 crypt_set_iteration_time(cd, iteration_time_ms);
2272 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
2274 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
2275 cd->password_verify = password_verify ? 1 : 0;
2278 void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
2281 case CRYPT_RNG_URANDOM:
2282 case CRYPT_RNG_RANDOM:
2283 log_dbg("RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
2284 cd->rng_type = rng_type;
2288 int crypt_get_rng_type(struct crypt_device *cd)
2293 return cd->rng_type;
2296 int crypt_memory_lock(struct crypt_device *cd, int lock)
2298 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
2302 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
2309 r = dm_status_device(cd, name);
2314 if (r < 0 && r != -ENODEV)
2315 return CRYPT_INVALID;
2318 return CRYPT_ACTIVE;
2323 return CRYPT_INACTIVE;
2326 static void hexprint(struct crypt_device *cd, const char *d, int n, const char *sep)
2329 for(i = 0; i < n; i++)
2330 log_std(cd, "%02hhx%s", (const char)d[i], sep);
2333 static int _luks_dump(struct crypt_device *cd)
2337 log_std(cd, "LUKS header information for %s\n\n", mdata_device_path(cd));
2338 log_std(cd, "Version: \t%d\n", cd->u.luks1.hdr.version);
2339 log_std(cd, "Cipher name: \t%s\n", cd->u.luks1.hdr.cipherName);
2340 log_std(cd, "Cipher mode: \t%s\n", cd->u.luks1.hdr.cipherMode);
2341 log_std(cd, "Hash spec: \t%s\n", cd->u.luks1.hdr.hashSpec);
2342 log_std(cd, "Payload offset:\t%d\n", cd->u.luks1.hdr.payloadOffset);
2343 log_std(cd, "MK bits: \t%d\n", cd->u.luks1.hdr.keyBytes * 8);
2344 log_std(cd, "MK digest: \t");
2345 hexprint(cd, cd->u.luks1.hdr.mkDigest, LUKS_DIGESTSIZE, " ");
2347 log_std(cd, "MK salt: \t");
2348 hexprint(cd, cd->u.luks1.hdr.mkDigestSalt, LUKS_SALTSIZE/2, " ");
2349 log_std(cd, "\n \t");
2350 hexprint(cd, cd->u.luks1.hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ");
2352 log_std(cd, "MK iterations: \t%d\n", cd->u.luks1.hdr.mkDigestIterations);
2353 log_std(cd, "UUID: \t%s\n\n", cd->u.luks1.hdr.uuid);
2354 for(i = 0; i < LUKS_NUMKEYS; i++) {
2355 if(cd->u.luks1.hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
2356 log_std(cd, "Key Slot %d: ENABLED\n",i);
2357 log_std(cd, "\tIterations: \t%d\n",
2358 cd->u.luks1.hdr.keyblock[i].passwordIterations);
2359 log_std(cd, "\tSalt: \t");
2360 hexprint(cd, cd->u.luks1.hdr.keyblock[i].passwordSalt,
2361 LUKS_SALTSIZE/2, " ");
2362 log_std(cd, "\n\t \t");
2363 hexprint(cd, cd->u.luks1.hdr.keyblock[i].passwordSalt +
2364 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ");
2367 log_std(cd, "\tKey material offset:\t%d\n",
2368 cd->u.luks1.hdr.keyblock[i].keyMaterialOffset);
2369 log_std(cd, "\tAF stripes: \t%d\n",
2370 cd->u.luks1.hdr.keyblock[i].stripes);
2373 log_std(cd, "Key Slot %d: DISABLED\n", i);
2378 static int _verity_dump(struct crypt_device *cd)
2380 log_std(cd, "VERITY header information for %s\n", mdata_device_path(cd));
2381 log_std(cd, "UUID: \t%s\n", cd->u.verity.uuid ?: "");
2382 log_std(cd, "Hash type: \t%u\n", cd->u.verity.hdr.hash_type);
2383 log_std(cd, "Data blocks: \t%" PRIu64 "\n", cd->u.verity.hdr.data_size);
2384 log_std(cd, "Data block size: \t%u\n", cd->u.verity.hdr.data_block_size);
2385 log_std(cd, "Hash block size: \t%u\n", cd->u.verity.hdr.hash_block_size);
2386 log_std(cd, "Hash algorithm: \t%s\n", cd->u.verity.hdr.hash_name);
2387 log_std(cd, "Salt: \t");
2388 if (cd->u.verity.hdr.salt_size)
2389 hexprint(cd, cd->u.verity.hdr.salt, cd->u.verity.hdr.salt_size, "");
2393 if (cd->u.verity.root_hash) {
2394 log_std(cd, "Root hash: \t");
2395 hexprint(cd, cd->u.verity.root_hash, cd->u.verity.root_hash_size, "");
2401 int crypt_dump(struct crypt_device *cd)
2403 if (isLUKS(cd->type))
2404 return _luks_dump(cd);
2405 else if (isVERITY(cd->type))
2406 return _verity_dump(cd);
2407 else if (isTCRYPT(cd->type))
2408 return TCRYPT_dump(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
2410 log_err(cd, _("Dump operation is not supported for this device type.\n"));
2414 const char *crypt_get_cipher(struct crypt_device *cd)
2416 if (isPLAIN(cd->type))
2417 return cd->u.plain.cipher;
2419 if (isLUKS(cd->type))
2420 return cd->u.luks1.hdr.cipherName;
2422 if (isLOOPAES(cd->type))
2423 return cd->u.loopaes.cipher;
2425 if (isTCRYPT(cd->type))
2426 return cd->u.tcrypt.params.cipher;
2431 const char *crypt_get_cipher_mode(struct crypt_device *cd)
2433 if (isPLAIN(cd->type))
2434 return cd->u.plain.cipher_mode;
2436 if (isLUKS(cd->type))
2437 return cd->u.luks1.hdr.cipherMode;
2439 if (isLOOPAES(cd->type))
2440 return cd->u.loopaes.cipher_mode;
2442 if (isTCRYPT(cd->type))
2443 return cd->u.tcrypt.params.mode;
2448 const char *crypt_get_uuid(struct crypt_device *cd)
2450 if (isLUKS(cd->type))
2451 return cd->u.luks1.hdr.uuid;
2453 if (isPLAIN(cd->type))
2454 return cd->u.plain.uuid;
2456 if (isLOOPAES(cd->type))
2457 return cd->u.loopaes.uuid;
2459 if (isVERITY(cd->type))
2460 return cd->u.verity.uuid;
2465 const char *crypt_get_device_name(struct crypt_device *cd)
2467 const char *path = device_block_path(cd->device);
2470 path = device_path(cd->device);
2475 int crypt_get_volume_key_size(struct crypt_device *cd)
2477 if (isPLAIN(cd->type))
2478 return cd->u.plain.key_size;
2480 if (isLUKS(cd->type))
2481 return cd->u.luks1.hdr.keyBytes;
2483 if (isLOOPAES(cd->type))
2484 return cd->u.loopaes.key_size;
2486 if (isVERITY(cd->type))
2487 return cd->u.verity.root_hash_size;
2489 if (isTCRYPT(cd->type))
2490 return cd->u.tcrypt.params.key_size;
2495 uint64_t crypt_get_data_offset(struct crypt_device *cd)
2497 if (isPLAIN(cd->type))
2498 return cd->u.plain.hdr.offset;
2500 if (isLUKS(cd->type))
2501 return cd->u.luks1.hdr.payloadOffset;
2503 if (isLOOPAES(cd->type))
2504 return cd->u.loopaes.hdr.offset;
2506 if (isTCRYPT(cd->type))
2507 return TCRYPT_get_data_offset(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
2512 uint64_t crypt_get_iv_offset(struct crypt_device *cd)
2514 if (isPLAIN(cd->type))
2515 return cd->u.plain.hdr.skip;
2517 if (isLUKS(cd->type))
2520 if (isLOOPAES(cd->type))
2521 return cd->u.loopaes.hdr.skip;
2523 if (isTCRYPT(cd->type))
2524 return TCRYPT_get_iv_offset(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
2529 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
2531 if (!isLUKS(cd->type)) {
2532 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2533 return CRYPT_SLOT_INVALID;
2536 return LUKS_keyslot_info(&cd->u.luks1.hdr, keyslot);
2539 int crypt_keyslot_max(const char *type)
2541 if (type && isLUKS(type))
2542 return LUKS_NUMKEYS;
2547 int crypt_keyslot_area(struct crypt_device *cd,
2552 if (!isLUKS(cd->type))
2555 return LUKS_keyslot_area(&cd->u.luks1.hdr, keyslot, offset, length);
2558 const char *crypt_get_type(struct crypt_device *cd)
2563 int crypt_get_verity_info(struct crypt_device *cd,
2564 struct crypt_params_verity *vp)
2566 if (!isVERITY(cd->type) || !vp)
2569 vp->data_device = device_path(cd->device);
2570 vp->hash_device = mdata_device_path(cd);
2571 vp->hash_name = cd->u.verity.hdr.hash_name;
2572 vp->salt = cd->u.verity.hdr.salt;
2573 vp->salt_size = cd->u.verity.hdr.salt_size;
2574 vp->data_block_size = cd->u.verity.hdr.data_block_size;
2575 vp->hash_block_size = cd->u.verity.hdr.hash_block_size;
2576 vp->data_size = cd->u.verity.hdr.data_size;
2577 vp->hash_area_offset = cd->u.verity.hdr.hash_area_offset;
2578 vp->hash_type = cd->u.verity.hdr.hash_type;
2579 vp->flags = cd->u.verity.hdr.flags & CRYPT_VERITY_NO_HEADER;
2583 int crypt_get_active_device(struct crypt_device *cd, const char *name,
2584 struct crypt_active_device *cad)
2586 struct crypt_dm_active_device dmd;
2589 r = dm_query_device(cd, name, 0, &dmd);
2593 if (dmd.target != DM_CRYPT && dmd.target != DM_VERITY)
2596 if (cd && isTCRYPT(cd->type)) {
2597 cad->offset = TCRYPT_get_data_offset(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
2598 cad->iv_offset = TCRYPT_get_iv_offset(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
2600 cad->offset = dmd.u.crypt.offset;
2601 cad->iv_offset = dmd.u.crypt.iv_offset;
2603 cad->size = dmd.size;
2604 cad->flags = dmd.flags;