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 /* used in CRYPT_LUKS1 */
51 uint64_t PBKDF2_per_sec;
53 /* used in CRYPT_PLAIN */
54 struct crypt_params_plain plain_hdr;
56 char *plain_cipher_mode;
58 unsigned int plain_key_size;
60 /* used in CRYPT_LOOPAES */
61 struct crypt_params_loopaes loopaes_hdr;
63 char *loopaes_cipher_mode;
65 unsigned int loopaes_key_size;
67 /* used in CRYPT_VERITY */
68 struct crypt_params_verity verity_hdr;
69 char *verity_root_hash;
70 unsigned int verity_root_hash_size;
73 /* used in CRYPT_TCRYPT */
74 struct crypt_params_tcrypt tcrypt_params;
75 struct tcrypt_phdr tcrypt_hdr;
77 /* callbacks definitions */
78 void (*log)(int level, const char *msg, void *usrptr);
80 int (*confirm)(const char *msg, void *usrptr);
82 int (*password)(const char *msg, char *buf, size_t length, void *usrptr);
83 void *password_usrptr;
85 /* last error message */
86 char error[MAX_ERROR_LENGTH];
90 /* FIXME: not thread safe, remove this later */
91 static char global_error[MAX_ERROR_LENGTH] = {0};
94 static void (*_default_log)(int level, const char *msg, void *usrptr) = NULL;
95 static int _debug_level = 0;
97 void crypt_set_debug_level(int level)
102 int crypt_get_debug_level(void)
107 static void crypt_set_error(struct crypt_device *cd, const char *error)
109 size_t size = strlen(error);
111 /* Set global error, ugly hack... */
112 strncpy(global_error, error, MAX_ERROR_LENGTH - 2);
113 if (size < MAX_ERROR_LENGTH && global_error[size - 1] == '\n')
114 global_error[size - 1] = '\0';
116 /* Set error string per context */
118 strncpy(cd->error, error, MAX_ERROR_LENGTH - 2);
119 if (size < MAX_ERROR_LENGTH && cd->error[size - 1] == '\n')
120 cd->error[size - 1] = '\0';
124 void crypt_log(struct crypt_device *cd, int level, const char *msg)
127 cd->log(level, msg, cd->log_usrptr);
128 else if (_default_log)
129 _default_log(level, msg, NULL);
131 if (level == CRYPT_LOG_ERROR)
132 crypt_set_error(cd, msg);
135 __attribute__((format(printf, 5, 6)))
136 void logger(struct crypt_device *cd, int level, const char *file,
137 int line, const char *format, ...)
142 va_start(argp, format);
144 if (vasprintf(&target, format, argp) > 0 ) {
146 crypt_log(cd, level, target);
148 } else if (_debug_level)
149 printf("# %s:%d %s\n", file ?: "?", line, target);
151 } else if (_debug_level)
152 printf("# %s\n", target);
160 static const char *mdata_device_path(struct crypt_device *cd)
162 return device_path(cd->metadata_device ?: cd->device);
166 struct device *crypt_metadata_device(struct crypt_device *cd)
168 return cd->metadata_device ?: cd->device;
171 struct device *crypt_data_device(struct crypt_device *cd)
176 int init_crypto(struct crypt_device *ctx)
180 crypt_fips_libcryptsetup_check(ctx);
182 r = crypt_random_init(ctx);
184 log_err(ctx, _("Cannot initialize crypto RNG backend.\n"));
188 r = crypt_backend_init(ctx);
190 log_err(ctx, _("Cannot initialize crypto backend.\n"));
192 log_dbg("Crypto backend (%s) initialized.", crypt_backend_version());
196 static int process_key(struct crypt_device *cd, const char *hash_name,
197 size_t key_size, const char *pass, size_t passLen,
198 struct volume_key **vk)
205 *vk = crypt_alloc_volume_key(key_size, NULL);
210 r = crypt_plain_hash(cd, hash_name, (*vk)->key, key_size, pass, passLen);
213 log_err(cd, _("Hash algorithm %s not supported.\n"),
216 log_err(cd, _("Key processing error (using hash %s).\n"),
218 crypt_free_volume_key(*vk);
222 } else if (passLen > key_size) {
223 memcpy((*vk)->key, pass, key_size);
225 memcpy((*vk)->key, pass, passLen);
231 static int isPLAIN(const char *type)
233 return (type && !strcmp(CRYPT_PLAIN, type));
236 static int isLUKS(const char *type)
238 return (type && !strcmp(CRYPT_LUKS1, type));
241 static int isLOOPAES(const char *type)
243 return (type && !strcmp(CRYPT_LOOPAES, type));
246 static int isVERITY(const char *type)
248 return (type && !strcmp(CRYPT_VERITY, type));
251 static int isTCRYPT(const char *type)
253 return (type && !strcmp(CRYPT_TCRYPT, type));
256 /* keyslot helpers */
257 static int keyslot_verify_or_find_empty(struct crypt_device *cd, int *keyslot)
259 if (*keyslot == CRYPT_ANY_SLOT) {
260 *keyslot = LUKS_keyslot_find_empty(&cd->hdr);
262 log_err(cd, _("All key slots full.\n"));
267 switch (LUKS_keyslot_info(&cd->hdr, *keyslot)) {
268 case CRYPT_SLOT_INVALID:
269 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
270 *keyslot, LUKS_NUMKEYS - 1);
272 case CRYPT_SLOT_INACTIVE:
275 log_err(cd, _("Key slot %d is full, please select another one.\n"),
284 * compares UUIDs returned by device-mapper (striped by cryptsetup) and uuid in header
286 static int crypt_uuid_cmp(const char *dm_uuid, const char *hdr_uuid)
291 if (!dm_uuid || !hdr_uuid)
294 str = strchr(dm_uuid, '-');
298 for (i = 0, j = 1; hdr_uuid[i]; i++) {
299 if (hdr_uuid[i] == '-')
302 if (!str[j] || str[j] == '-')
305 if (str[j] != hdr_uuid[i])
313 int PLAIN_activate(struct crypt_device *cd,
315 struct volume_key *vk,
320 char *dm_cipher = NULL;
321 enum devcheck device_check;
322 struct crypt_dm_active_device dmd = {
324 .uuid = crypt_get_uuid(cd),
327 .data_device = crypt_data_device(cd),
331 .offset = crypt_get_data_offset(cd),
332 .iv_offset = crypt_get_iv_offset(cd),
336 if (dmd.flags & CRYPT_ACTIVATE_SHARED)
337 device_check = DEV_SHARED;
339 device_check = DEV_EXCL;
341 r = device_block_adjust(cd, dmd.data_device, device_check,
342 dmd.u.crypt.offset, &dmd.size, &dmd.flags);
346 if (crypt_get_cipher_mode(cd))
347 r = asprintf(&dm_cipher, "%s-%s", crypt_get_cipher(cd), crypt_get_cipher_mode(cd));
349 r = asprintf(&dm_cipher, "%s", crypt_get_cipher(cd));
353 dmd.u.crypt.cipher = dm_cipher;
354 log_dbg("Trying to activate PLAIN device %s using cipher %s.",
355 name, dmd.u.crypt.cipher);
357 r = dm_create_device(cd, name, CRYPT_PLAIN, &dmd, 0);
360 if (!cd->plain_uuid && dm_query_device(cd, name, DM_ACTIVE_UUID, &dmd) >= 0)
361 cd->plain_uuid = CONST_CAST(char*)dmd.uuid;
367 int crypt_confirm(struct crypt_device *cd, const char *msg)
369 if (!cd || !cd->confirm)
372 return cd->confirm(msg, cd->confirm_usrptr);
375 static int key_from_terminal(struct crypt_device *cd, char *msg, char **key,
376 size_t *key_len, int force_verify)
378 char *prompt = NULL, *device_name;
383 if (crypt_loop_device(crypt_get_device_name(cd)))
384 device_name = crypt_loop_backing_file(crypt_get_device_name(cd));
386 device_name = strdup(crypt_get_device_name(cd));
389 r = asprintf(&prompt, _("Enter passphrase for %s: "), device_name);
397 *key = crypt_safe_alloc(DEFAULT_PASSPHRASE_SIZE_MAX);
402 r = cd->password(msg, *key, DEFAULT_PASSPHRASE_SIZE_MAX,
403 cd->password_usrptr);
405 crypt_safe_free(*key);
410 r = crypt_get_key(msg, key, key_len, 0, 0, NULL, cd->timeout,
411 (force_verify || cd->password_verify), cd);
414 return (r < 0) ? r: 0;
417 static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslot,
418 struct volume_key **vk)
420 char *passphrase_read = NULL;
421 size_t passphrase_size_read;
422 int r = -EINVAL, eperm = 0, tries = cd->tries;
426 crypt_free_volume_key(*vk);
429 r = key_from_terminal(cd, NULL, &passphrase_read,
430 &passphrase_size_read, 0);
431 /* Continue if it is just passphrase verify mismatch */
437 r = LUKS_open_key_with_hdr(keyslot, passphrase_read,
438 passphrase_size_read, &cd->hdr, vk, cd);
441 crypt_safe_free(passphrase_read);
442 passphrase_read = NULL;
443 } while (r == -EPERM && (--tries > 0));
446 crypt_free_volume_key(*vk);
449 /* Report wrong passphrase if at least one try failed */
450 if (eperm && r == -EPIPE)
454 crypt_safe_free(passphrase_read);
458 static int key_from_file(struct crypt_device *cd, char *msg,
459 char **key, size_t *key_len,
460 const char *key_file, size_t key_offset,
463 return crypt_get_key(msg, key, key_len, key_offset, key_size, key_file,
467 void crypt_set_log_callback(struct crypt_device *cd,
468 void (*log)(int level, const char *msg, void *usrptr),
475 cd->log_usrptr = usrptr;
479 void crypt_set_confirm_callback(struct crypt_device *cd,
480 int (*confirm)(const char *msg, void *usrptr),
483 cd->confirm = confirm;
484 cd->confirm_usrptr = usrptr;
487 void crypt_set_password_callback(struct crypt_device *cd,
488 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
491 cd->password = password;
492 cd->password_usrptr = usrptr;
495 static void _get_error(char *error, char *buf, size_t size)
497 if (!buf || size < 1)
500 strncpy(buf, error, size - 1);
501 buf[size - 1] = '\0';
507 void crypt_last_error(struct crypt_device *cd, char *buf, size_t size)
510 return _get_error(cd->error, buf, size);
513 /* Deprecated global error interface */
514 void crypt_get_error(char *buf, size_t size)
516 return _get_error(global_error, buf, size);
519 const char *crypt_get_dir(void)
524 int crypt_init(struct crypt_device **cd, const char *device)
526 struct crypt_device *h = NULL;
532 log_dbg("Allocating crypt device %s context.", device);
534 if (!(h = malloc(sizeof(struct crypt_device))))
537 memset(h, 0, sizeof(*h));
539 r = device_alloc(&h->device, device);
545 h->iteration_time = 1000;
546 h->password_verify = 0;
548 h->rng_type = crypt_random_default_key_rng();
552 device_free(h->device);
557 static int crypt_check_data_device_size(struct crypt_device *cd)
560 uint64_t size, size_min;
562 /* Check data device size, require at least one sector */
563 size_min = crypt_get_data_offset(cd) << SECTOR_SHIFT ?: SECTOR_SIZE;
565 r = device_size(cd->device, &size);
569 if (size < size_min) {
570 log_err(cd, _("Header detected but device %s is too small.\n"),
571 device_path(cd->device));
578 int crypt_set_data_device(struct crypt_device *cd, const char *device)
580 struct device *dev = NULL;
583 log_dbg("Setting ciphertext data device to %s.", device ?: "(none)");
585 if (!isLUKS(cd->type) && !isVERITY(cd->type)) {
586 log_err(cd, _("This operation is not supported for this device type.\n"));
590 /* metadata device must be set */
591 if (!cd->device || !device)
594 r = device_alloc(&dev, device);
598 if (!cd->metadata_device) {
599 cd->metadata_device = cd->device;
601 device_free(cd->device);
605 return crypt_check_data_device_size(cd);
608 static int _crypt_load_luks1(struct crypt_device *cd, int require_header, int repair)
610 struct luks_phdr hdr;
617 r = LUKS_read_phdr(&hdr, require_header, repair, cd);
621 if (!cd->type && !(cd->type = strdup(CRYPT_LUKS1)))
624 memcpy(&cd->hdr, &hdr, sizeof(hdr));
629 static int _crypt_load_tcrypt(struct crypt_device *cd, struct crypt_params_tcrypt *params)
637 memcpy(&cd->tcrypt_params, params, sizeof(*params));
639 r = TCRYPT_read_phdr(cd, &cd->tcrypt_hdr, &cd->tcrypt_params);
641 cd->tcrypt_params.passphrase = NULL;
642 cd->tcrypt_params.passphrase_size = 0;
643 cd->tcrypt_params.keyfiles = NULL;
644 cd->tcrypt_params.keyfiles_count = 0;
649 if (!cd->type && !(cd->type = strdup(CRYPT_TCRYPT)))
655 static int _crypt_load_verity(struct crypt_device *cd, struct crypt_params_verity *params)
658 size_t sb_offset = 0;
664 if (params && params->flags & CRYPT_VERITY_NO_HEADER)
668 sb_offset = params->hash_area_offset;
670 r = VERITY_read_sb(cd, sb_offset, &cd->verity_uuid, &cd->verity_hdr);
675 cd->verity_hdr.flags = params->flags;
677 /* Hash availability checked in sb load */
678 cd->verity_root_hash_size = crypt_hash_size(cd->verity_hdr.hash_name);
679 if (cd->verity_root_hash_size > 4096)
682 if (!cd->type && !(cd->type = strdup(CRYPT_VERITY)))
685 if (params && params->data_device &&
686 (r = crypt_set_data_device(cd, params->data_device)) < 0)
692 static int _init_by_name_crypt(struct crypt_device *cd, const char *name)
694 struct crypt_dm_active_device dmd = {};
695 char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
698 r = dm_query_device(cd, name,
701 DM_ACTIVE_CRYPT_CIPHER |
702 DM_ACTIVE_CRYPT_KEYSIZE, &dmd);
706 if (isPLAIN(cd->type)) {
707 cd->plain_uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
708 cd->plain_hdr.hash = NULL; /* no way to get this */
709 cd->plain_hdr.offset = dmd.u.crypt.offset;
710 cd->plain_hdr.skip = dmd.u.crypt.iv_offset;
711 cd->plain_key_size = dmd.u.crypt.vk->keylength;
713 r = crypt_parse_name_and_mode(dmd.u.crypt.cipher, cipher, NULL, cipher_mode);
715 cd->plain_cipher = strdup(cipher);
716 cd->plain_cipher_mode = strdup(cipher_mode);
718 } else if (isLOOPAES(cd->type)) {
719 cd->loopaes_uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
720 cd->loopaes_hdr.offset = dmd.u.crypt.offset;
722 r = crypt_parse_name_and_mode(dmd.u.crypt.cipher, cipher,
723 &key_nums, cipher_mode);
725 cd->loopaes_cipher = strdup(cipher);
726 cd->loopaes_cipher_mode = strdup(cipher_mode);
727 /* version 3 uses last key for IV */
728 if (dmd.u.crypt.vk->keylength % key_nums)
730 cd->loopaes_key_size = dmd.u.crypt.vk->keylength / key_nums;
732 } else if (isLUKS(cd->type)) {
733 if (crypt_metadata_device(cd)) {
734 r = _crypt_load_luks1(cd, 0, 0);
736 log_dbg("LUKS device header does not match active device.");
742 /* check whether UUIDs match each other */
743 r = crypt_uuid_cmp(dmd.uuid, cd->hdr.uuid);
745 log_dbg("LUKS device header uuid: %s mismatches DM returned uuid %s",
746 cd->hdr.uuid, dmd.uuid);
753 } else if (isTCRYPT(cd->type)) {
757 crypt_free_volume_key(dmd.u.crypt.vk);
758 device_free(dmd.data_device);
759 free(CONST_CAST(void*)dmd.u.crypt.cipher);
760 free(CONST_CAST(void*)dmd.uuid);
764 static int _init_by_name_verity(struct crypt_device *cd, const char *name)
766 struct crypt_params_verity params = {};
767 struct crypt_dm_active_device dmd = {
769 .u.verity.vp = ¶ms,
773 r = dm_query_device(cd, name,
776 DM_ACTIVE_VERITY_HASH_DEVICE |
777 DM_ACTIVE_VERITY_PARAMS, &dmd);
781 if (isVERITY(cd->type)) {
782 cd->verity_uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
783 cd->verity_hdr.flags = CRYPT_VERITY_NO_HEADER; //FIXME
784 cd->verity_hdr.data_size = params.data_size;
785 cd->verity_root_hash_size = dmd.u.verity.root_hash_size;
786 cd->verity_root_hash = NULL;
787 cd->verity_hdr.hash_name = params.hash_name;
788 cd->verity_hdr.data_device = NULL;
789 cd->verity_hdr.hash_device = NULL;
790 cd->verity_hdr.data_block_size = params.data_block_size;
791 cd->verity_hdr.hash_block_size = params.hash_block_size;
792 cd->verity_hdr.hash_area_offset = dmd.u.verity.hash_offset;
793 cd->verity_hdr.hash_type = params.hash_type;
794 cd->verity_hdr.flags = params.flags;
795 cd->verity_hdr.salt_size = params.salt_size;
796 cd->verity_hdr.salt = params.salt;
797 cd->metadata_device = dmd.u.verity.hash_device;
800 device_free(dmd.data_device);
801 free(CONST_CAST(void*)dmd.uuid);
805 int crypt_init_by_name_and_header(struct crypt_device **cd,
807 const char *header_device)
809 crypt_status_info ci;
810 struct crypt_dm_active_device dmd;
813 log_dbg("Allocating crypt device context by device %s.", name);
815 ci = crypt_status(NULL, name);
816 if (ci == CRYPT_INVALID)
819 if (ci < CRYPT_ACTIVE) {
820 log_err(NULL, _("Device %s is not active.\n"), name);
824 r = dm_query_device(NULL, name, DM_ACTIVE_DEVICE | DM_ACTIVE_UUID, &dmd);
831 r = crypt_init(cd, header_device);
833 r = crypt_init(cd, device_path(dmd.data_device));
835 /* Underlying device disappeared but mapping still active */
836 if (!dmd.data_device || r == -ENOTBLK)
837 log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
840 /* Underlying device is not readable but crypt mapping exists */
842 device_free(dmd.data_device);
843 dmd.data_device = NULL;
844 r = crypt_init(cd, NULL);
852 if (!strncmp(CRYPT_PLAIN, dmd.uuid, sizeof(CRYPT_PLAIN)-1))
853 (*cd)->type = strdup(CRYPT_PLAIN);
854 else if (!strncmp(CRYPT_LOOPAES, dmd.uuid, sizeof(CRYPT_LOOPAES)-1))
855 (*cd)->type = strdup(CRYPT_LOOPAES);
856 else if (!strncmp(CRYPT_LUKS1, dmd.uuid, sizeof(CRYPT_LUKS1)-1))
857 (*cd)->type = strdup(CRYPT_LUKS1);
858 else if (!strncmp(CRYPT_VERITY, dmd.uuid, sizeof(CRYPT_VERITY)-1))
859 (*cd)->type = strdup(CRYPT_VERITY);
860 else if (!strncmp(CRYPT_TCRYPT, dmd.uuid, sizeof(CRYPT_TCRYPT)-1))
861 (*cd)->type = strdup(CRYPT_TCRYPT);
863 log_dbg("Unknown UUID set, some parameters are not set.");
865 log_dbg("Active device has no UUID set, some parameters are not set.");
868 r = crypt_set_data_device(*cd, device_path(dmd.data_device));
873 /* Try to initialise basic parameters from active device */
875 if (dmd.target == DM_CRYPT)
876 r = _init_by_name_crypt(*cd, name);
877 else if (dmd.target == DM_VERITY)
878 r = _init_by_name_verity(*cd, name);
884 device_free(dmd.data_device);
885 free(CONST_CAST(void*)dmd.uuid);
889 int crypt_init_by_name(struct crypt_device **cd, const char *name)
891 return crypt_init_by_name_and_header(cd, name, NULL);
894 static int _crypt_format_plain(struct crypt_device *cd,
896 const char *cipher_mode,
898 size_t volume_key_size,
899 struct crypt_params_plain *params)
901 if (!cipher || !cipher_mode) {
902 log_err(cd, _("Invalid plain crypt parameters.\n"));
906 if (volume_key_size > 1024) {
907 log_err(cd, _("Invalid key size.\n"));
911 if (!(cd->type = strdup(CRYPT_PLAIN)))
914 cd->plain_key_size = volume_key_size;
915 cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
919 cd->plain_cipher = strdup(cipher);
920 cd->plain_cipher_mode = strdup(cipher_mode);
923 cd->plain_uuid = strdup(uuid);
925 if (params && params->hash)
926 cd->plain_hdr.hash = strdup(params->hash);
928 cd->plain_hdr.offset = params ? params->offset : 0;
929 cd->plain_hdr.skip = params ? params->skip : 0;
930 cd->plain_hdr.size = params ? params->size : 0;
932 if (!cd->plain_cipher || !cd->plain_cipher_mode)
938 static int _crypt_format_luks1(struct crypt_device *cd,
940 const char *cipher_mode,
942 const char *volume_key,
943 size_t volume_key_size,
944 struct crypt_params_luks1 *params)
947 unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
948 unsigned long alignment_offset = 0;
950 if (!crypt_metadata_device(cd)) {
951 log_err(cd, _("Can't format LUKS without device.\n"));
955 if (!(cd->type = strdup(CRYPT_LUKS1)))
959 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
962 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
967 if (params && params->data_device) {
968 cd->metadata_device = cd->device;
970 if (device_alloc(&cd->device, params->data_device) < 0)
972 required_alignment = params->data_alignment * SECTOR_SIZE;
973 } else if (params && params->data_alignment) {
974 required_alignment = params->data_alignment * SECTOR_SIZE;
976 device_topology_alignment(cd->device,
978 &alignment_offset, DEFAULT_DISK_ALIGNMENT);
980 /* Check early if we cannot allocate block device for key slot access */
981 r = device_block_adjust(cd, cd->device, DEV_OK, 0, NULL, NULL);
985 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
986 (params && params->hash) ? params->hash : "sha1",
988 required_alignment / SECTOR_SIZE,
989 alignment_offset / SECTOR_SIZE,
990 cd->iteration_time, &cd->PBKDF2_per_sec,
991 cd->metadata_device ? 1 : 0, cd);
995 /* Wipe first 8 sectors - fs magic numbers etc. */
996 r = crypt_wipe(crypt_metadata_device(cd), 0, 8 * SECTOR_SIZE, CRYPT_WIPE_ZERO, 1);
999 log_err(cd, _("Cannot format device %s which is still in use.\n"),
1000 mdata_device_path(cd));
1001 else if (r == -EACCES) {
1002 log_err(cd, _("Cannot format device %s, permission denied.\n"),
1003 mdata_device_path(cd));
1006 log_err(cd, _("Cannot wipe header on device %s.\n"),
1007 mdata_device_path(cd));
1012 r = LUKS_write_phdr(&cd->hdr, cd);
1017 static int _crypt_format_loopaes(struct crypt_device *cd,
1020 size_t volume_key_size,
1021 struct crypt_params_loopaes *params)
1023 if (!crypt_metadata_device(cd)) {
1024 log_err(cd, _("Can't format LOOPAES without device.\n"));
1028 if (volume_key_size > 1024) {
1029 log_err(cd, _("Invalid key size.\n"));
1033 if (!(cd->type = strdup(CRYPT_LOOPAES)))
1036 cd->loopaes_key_size = volume_key_size;
1038 cd->loopaes_cipher = strdup(cipher ?: DEFAULT_LOOPAES_CIPHER);
1041 cd->loopaes_uuid = strdup(uuid);
1043 if (params && params->hash)
1044 cd->loopaes_hdr.hash = strdup(params->hash);
1046 cd->loopaes_hdr.offset = params ? params->offset : 0;
1047 cd->loopaes_hdr.skip = params ? params->skip : 0;
1052 static int _crypt_format_verity(struct crypt_device *cd,
1054 struct crypt_params_verity *params)
1056 int r = 0, hash_size;
1057 uint64_t data_device_size;
1059 if (!crypt_metadata_device(cd)) {
1060 log_err(cd, _("Can't format VERITY without device.\n"));
1064 if (!params || !params->data_device)
1067 if (params->hash_type > VERITY_MAX_HASH_TYPE) {
1068 log_err(cd, _("Unsupported VERITY hash type %d.\n"), params->hash_type);
1072 if (VERITY_BLOCK_SIZE_OK(params->data_block_size) ||
1073 VERITY_BLOCK_SIZE_OK(params->hash_block_size)) {
1074 log_err(cd, _("Unsupported VERITY block size.\n"));
1078 if (params->hash_area_offset % 512) {
1079 log_err(cd, _("Unsupported VERITY hash offset.\n"));
1083 if (!(cd->type = strdup(CRYPT_VERITY)))
1086 r = crypt_set_data_device(cd, params->data_device);
1089 if (!params->data_size) {
1090 r = device_size(cd->device, &data_device_size);
1094 cd->verity_hdr.data_size = data_device_size / params->data_block_size;
1096 cd->verity_hdr.data_size = params->data_size;
1098 hash_size = crypt_hash_size(params->hash_name);
1099 if (hash_size <= 0) {
1100 log_err(cd, _("Hash algorithm %s not supported.\n"),
1104 cd->verity_root_hash_size = hash_size;
1106 cd->verity_root_hash = malloc(cd->verity_root_hash_size);
1107 if (!cd->verity_root_hash)
1110 cd->verity_hdr.flags = params->flags;
1111 if (!(cd->verity_hdr.hash_name = strdup(params->hash_name)))
1113 cd->verity_hdr.data_device = NULL;
1114 cd->verity_hdr.data_block_size = params->data_block_size;
1115 cd->verity_hdr.hash_block_size = params->hash_block_size;
1116 cd->verity_hdr.hash_area_offset = params->hash_area_offset;
1117 cd->verity_hdr.hash_type = params->hash_type;
1118 cd->verity_hdr.flags = params->flags;
1119 cd->verity_hdr.salt_size = params->salt_size;
1120 if (!(cd->verity_hdr.salt = malloc(params->salt_size)))
1124 memcpy(CONST_CAST(char*)cd->verity_hdr.salt, params->salt,
1127 r = crypt_random_get(cd, CONST_CAST(char*)cd->verity_hdr.salt,
1128 params->salt_size, CRYPT_RND_SALT);
1132 if (params->flags & CRYPT_VERITY_CREATE_HASH) {
1133 r = VERITY_create(cd, &cd->verity_hdr,
1134 cd->verity_root_hash, cd->verity_root_hash_size);
1139 if (!(params->flags & CRYPT_VERITY_NO_HEADER)) {
1141 cd->verity_uuid = strdup(uuid);
1143 r = VERITY_UUID_generate(cd, &cd->verity_uuid);
1148 r = VERITY_write_sb(cd, cd->verity_hdr.hash_area_offset,
1155 int crypt_format(struct crypt_device *cd,
1158 const char *cipher_mode,
1160 const char *volume_key,
1161 size_t volume_key_size,
1170 log_dbg("Context already formatted as %s.", cd->type);
1174 log_dbg("Formatting device %s as type %s.", mdata_device_path(cd) ?: "(none)", type);
1176 r = init_crypto(cd);
1181 r = _crypt_format_plain(cd, cipher, cipher_mode,
1182 uuid, volume_key_size, params);
1183 else if (isLUKS(type))
1184 r = _crypt_format_luks1(cd, cipher, cipher_mode,
1185 uuid, volume_key, volume_key_size, params);
1186 else if (isLOOPAES(type))
1187 r = _crypt_format_loopaes(cd, cipher, uuid, volume_key_size, params);
1188 else if (isVERITY(type))
1189 r = _crypt_format_verity(cd, uuid, params);
1191 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
1198 crypt_free_volume_key(cd->volume_key);
1199 cd->volume_key = NULL;
1205 int crypt_load(struct crypt_device *cd,
1206 const char *requested_type,
1211 log_dbg("Trying to load %s crypt type from device %s.",
1212 requested_type ?: "any", mdata_device_path(cd) ?: "(none)");
1214 if (!crypt_metadata_device(cd))
1217 if (!requested_type || isLUKS(requested_type)) {
1218 if (cd->type && !isLUKS(cd->type)) {
1219 log_dbg("Context is already initialised to type %s", cd->type);
1223 r = _crypt_load_luks1(cd, 1, 0);
1224 } else if (isVERITY(requested_type)) {
1225 if (cd->type && !isVERITY(cd->type)) {
1226 log_dbg("Context is already initialised to type %s", cd->type);
1229 r = _crypt_load_verity(cd, params);
1230 } else if (isTCRYPT(requested_type)) {
1231 if (cd->type && !isTCRYPT(cd->type)) {
1232 log_dbg("Context is already initialised to type %s", cd->type);
1235 r = _crypt_load_tcrypt(cd, params);
1242 int crypt_repair(struct crypt_device *cd,
1243 const char *requested_type,
1244 void *params __attribute__((unused)))
1248 log_dbg("Trying to repair %s crypt type from device %s.",
1249 requested_type ?: "any", mdata_device_path(cd) ?: "(none)");
1251 if (!crypt_metadata_device(cd))
1254 if (requested_type && !isLUKS(requested_type))
1258 /* Load with repair */
1259 r = _crypt_load_luks1(cd, 1, 1);
1263 /* cd->type and header must be set in context */
1264 r = crypt_check_data_device_size(cd);
1273 int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
1275 struct crypt_dm_active_device dmd;
1278 /* Device context type must be initialised */
1279 if (!cd->type || !crypt_get_uuid(cd))
1282 log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
1284 r = dm_query_device(cd, name, DM_ACTIVE_DEVICE | DM_ACTIVE_CRYPT_CIPHER |
1285 DM_ACTIVE_UUID | DM_ACTIVE_CRYPT_KEYSIZE |
1286 DM_ACTIVE_CRYPT_KEY, &dmd);
1288 log_err(NULL, _("Device %s is not active.\n"), name);
1292 if (!dmd.uuid || dmd.target != DM_CRYPT) {
1297 r = device_block_adjust(cd, dmd.data_device, DEV_OK,
1298 dmd.u.crypt.offset, &new_size, &dmd.flags);
1302 if (new_size == dmd.size) {
1303 log_dbg("Device has already requested size %" PRIu64
1304 " sectors.", dmd.size);
1307 dmd.size = new_size;
1308 if (isTCRYPT(cd->type))
1311 r = dm_create_device(cd, name, cd->type, &dmd, 1);
1314 if (dmd.target == DM_CRYPT) {
1315 crypt_free_volume_key(dmd.u.crypt.vk);
1316 free(CONST_CAST(void*)dmd.u.crypt.cipher);
1318 free(CONST_CAST(void*)dmd.data_device);
1319 free(CONST_CAST(void*)dmd.uuid);
1324 int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
1326 if (!isLUKS(cd->type)) {
1327 log_err(cd, _("This operation is not supported for this device type.\n"));
1331 if (uuid && !strncmp(uuid, cd->hdr.uuid, sizeof(cd->hdr.uuid))) {
1332 log_dbg("UUID is the same as requested (%s) for device %s.",
1333 uuid, mdata_device_path(cd));
1338 log_dbg("Requested new UUID change to %s for %s.", uuid, mdata_device_path(cd));
1340 log_dbg("Requested new UUID refresh for %s.", mdata_device_path(cd));
1342 if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
1345 return LUKS_hdr_uuid_set(&cd->hdr, uuid, cd);
1348 int crypt_header_backup(struct crypt_device *cd,
1349 const char *requested_type,
1350 const char *backup_file)
1354 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1357 r = init_crypto(cd);
1361 log_dbg("Requested header backup of device %s (%s) to "
1362 "file %s.", mdata_device_path(cd), requested_type, backup_file);
1364 return LUKS_hdr_backup(backup_file, &cd->hdr, cd);
1367 int crypt_header_restore(struct crypt_device *cd,
1368 const char *requested_type,
1369 const char *backup_file)
1373 if (requested_type && !isLUKS(requested_type))
1376 r = init_crypto(cd);
1380 log_dbg("Requested header restore to device %s (%s) from "
1381 "file %s.", mdata_device_path(cd), requested_type, backup_file);
1383 return LUKS_hdr_restore(backup_file, &cd->hdr, cd);
1386 void crypt_free(struct crypt_device *cd)
1389 log_dbg("Releasing crypt device %s context.", mdata_device_path(cd));
1392 crypt_free_volume_key(cd->volume_key);
1394 device_free(cd->device);
1395 device_free(cd->metadata_device);
1398 /* used in plain device only */
1399 free(CONST_CAST(void*)cd->plain_hdr.hash);
1400 free(cd->plain_cipher);
1401 free(cd->plain_cipher_mode);
1402 free(cd->plain_uuid);
1404 /* used in loop-AES device only */
1405 free(CONST_CAST(void*)cd->loopaes_hdr.hash);
1406 free(cd->loopaes_cipher);
1407 free(cd->loopaes_uuid);
1409 /* used in verity device only */
1410 free(CONST_CAST(void*)cd->verity_hdr.hash_name);
1411 free(CONST_CAST(void*)cd->verity_hdr.salt);
1412 free(cd->verity_root_hash);
1413 free(cd->verity_uuid);
1419 int crypt_suspend(struct crypt_device *cd,
1422 crypt_status_info ci;
1425 log_dbg("Suspending volume %s.", name);
1427 if (!cd || !isLUKS(cd->type)) {
1428 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1433 ci = crypt_status(NULL, name);
1434 if (ci < CRYPT_ACTIVE) {
1435 log_err(cd, _("Volume %s is not active.\n"), name);
1441 r = dm_status_suspended(cd, name);
1446 log_err(cd, _("Volume %s is already suspended.\n"), name);
1451 r = dm_suspend_and_wipe_key(cd, name);
1453 log_err(cd, "Suspend is not supported for device %s.\n", name);
1455 log_err(cd, "Error during suspending device %s.\n", name);
1461 int crypt_resume_by_passphrase(struct crypt_device *cd,
1464 const char *passphrase,
1465 size_t passphrase_size)
1467 struct volume_key *vk = NULL;
1470 log_dbg("Resuming volume %s.", name);
1472 if (!isLUKS(cd->type)) {
1473 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1478 r = dm_status_suspended(cd, name);
1483 log_err(cd, _("Volume %s is not suspended.\n"), name);
1488 r = LUKS_open_key_with_hdr(keyslot, passphrase, passphrase_size,
1491 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1495 r = dm_resume_and_reinstate_key(cd, name, vk->keylength, vk->key);
1497 log_err(cd, "Resume is not supported for device %s.\n", name);
1499 log_err(cd, "Error during resuming device %s.\n", name);
1503 crypt_free_volume_key(vk);
1504 return r < 0 ? r : keyslot;
1507 int crypt_resume_by_keyfile_offset(struct crypt_device *cd,
1510 const char *keyfile,
1511 size_t keyfile_size,
1512 size_t keyfile_offset)
1514 struct volume_key *vk = NULL;
1515 char *passphrase_read = NULL;
1516 size_t passphrase_size_read;
1519 log_dbg("Resuming volume %s.", name);
1521 if (!isLUKS(cd->type)) {
1522 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1527 r = dm_status_suspended(cd, name);
1532 log_err(cd, _("Volume %s is not suspended.\n"), name);
1539 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1540 &passphrase_size_read, keyfile, keyfile_offset,
1545 r = LUKS_open_key_with_hdr(keyslot, passphrase_read,
1546 passphrase_size_read, &cd->hdr, &vk, cd);
1551 r = dm_resume_and_reinstate_key(cd, name, vk->keylength, vk->key);
1553 log_err(cd, "Error during resuming device %s.\n", name);
1555 crypt_safe_free(passphrase_read);
1556 crypt_free_volume_key(vk);
1557 return r < 0 ? r : keyslot;
1560 int crypt_resume_by_keyfile(struct crypt_device *cd,
1563 const char *keyfile,
1564 size_t keyfile_size)
1566 return crypt_resume_by_keyfile_offset(cd, name, keyslot,
1567 keyfile, keyfile_size, 0);
1570 // slot manipulation
1571 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1572 int keyslot, // -1 any
1573 const char *passphrase, // NULL -> terminal
1574 size_t passphrase_size,
1575 const char *new_passphrase, // NULL -> terminal
1576 size_t new_passphrase_size)
1578 struct volume_key *vk = NULL;
1579 char *password = NULL, *new_password = NULL;
1580 size_t passwordLen, new_passwordLen;
1583 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1584 "new passphrase %sprovided.",
1585 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1587 if (!isLUKS(cd->type)) {
1588 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1592 r = keyslot_verify_or_find_empty(cd, &keyslot);
1596 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1597 /* No slots used, try to use pre-generated key in header */
1598 if (cd->volume_key) {
1599 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1600 r = vk ? 0 : -ENOMEM;
1602 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1605 } else if (passphrase) {
1606 /* Passphrase provided, use it to unlock existing keyslot */
1607 r = LUKS_open_key_with_hdr(CRYPT_ANY_SLOT, passphrase,
1608 passphrase_size, &cd->hdr, &vk, cd);
1610 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1611 r = key_from_terminal(cd, _("Enter any passphrase: "),
1612 &password, &passwordLen, 0);
1616 r = LUKS_open_key_with_hdr(CRYPT_ANY_SLOT, password,
1617 passwordLen, &cd->hdr, &vk, cd);
1618 crypt_safe_free(password);
1624 if (new_passphrase) {
1625 new_password = CONST_CAST(char*)new_passphrase;
1626 new_passwordLen = new_passphrase_size;
1628 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1629 &new_password, &new_passwordLen, 1);
1634 r = LUKS_set_key(keyslot, new_password, new_passwordLen,
1635 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1640 if (!new_passphrase)
1641 crypt_safe_free(new_password);
1642 crypt_free_volume_key(vk);
1643 return r ?: keyslot;
1646 int crypt_keyslot_add_by_keyfile_offset(struct crypt_device *cd,
1648 const char *keyfile,
1649 size_t keyfile_size,
1650 size_t keyfile_offset,
1651 const char *new_keyfile,
1652 size_t new_keyfile_size,
1653 size_t new_keyfile_offset)
1655 struct volume_key *vk = NULL;
1656 char *password = NULL; size_t passwordLen;
1657 char *new_password = NULL; size_t new_passwordLen;
1660 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1661 keyfile ?: "[none]", new_keyfile ?: "[none]");
1663 if (!isLUKS(cd->type)) {
1664 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1668 r = keyslot_verify_or_find_empty(cd, &keyslot);
1672 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1673 /* No slots used, try to use pre-generated key in header */
1674 if (cd->volume_key) {
1675 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1676 r = vk ? 0 : -ENOMEM;
1678 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1682 /* Read password from file of (if NULL) from terminal */
1684 r = key_from_file(cd, _("Enter any passphrase: "),
1685 &password, &passwordLen,
1686 keyfile, keyfile_offset, keyfile_size);
1688 r = key_from_terminal(cd, _("Enter any passphrase: "),
1689 &password, &passwordLen, 0);
1693 r = LUKS_open_key_with_hdr(CRYPT_ANY_SLOT, password, passwordLen,
1701 r = key_from_file(cd, _("Enter new passphrase for key slot: "),
1702 &new_password, &new_passwordLen, new_keyfile,
1703 new_keyfile_offset, new_keyfile_size);
1705 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1706 &new_password, &new_passwordLen, 1);
1710 r = LUKS_set_key(keyslot, new_password, new_passwordLen,
1711 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1713 crypt_safe_free(password);
1714 crypt_safe_free(new_password);
1715 crypt_free_volume_key(vk);
1716 return r < 0 ? r : keyslot;
1719 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1721 const char *keyfile,
1722 size_t keyfile_size,
1723 const char *new_keyfile,
1724 size_t new_keyfile_size)
1726 return crypt_keyslot_add_by_keyfile_offset(cd, keyslot,
1727 keyfile, keyfile_size, 0,
1728 new_keyfile, new_keyfile_size, 0);
1731 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1733 const char *volume_key,
1734 size_t volume_key_size,
1735 const char *passphrase,
1736 size_t passphrase_size)
1738 struct volume_key *vk = NULL;
1740 char *new_password = NULL; size_t new_passwordLen;
1742 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1744 if (!isLUKS(cd->type)) {
1745 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1750 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1751 else if (cd->volume_key)
1752 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1757 r = LUKS_verify_volume_key(&cd->hdr, vk);
1759 log_err(cd, _("Volume key does not match the volume.\n"));
1763 r = keyslot_verify_or_find_empty(cd, &keyslot);
1768 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1769 &new_password, &new_passwordLen, 1);
1772 passphrase = new_password;
1773 passphrase_size = new_passwordLen;
1776 r = LUKS_set_key(keyslot, passphrase, passphrase_size,
1777 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1779 crypt_safe_free(new_password);
1780 crypt_free_volume_key(vk);
1781 return (r < 0) ? r : keyslot;
1784 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1786 crypt_keyslot_info ki;
1788 log_dbg("Destroying keyslot %d.", keyslot);
1790 if (!isLUKS(cd->type)) {
1791 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1795 ki = crypt_keyslot_status(cd, keyslot);
1796 if (ki == CRYPT_SLOT_INVALID) {
1797 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1801 if (ki == CRYPT_SLOT_INACTIVE) {
1802 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1806 return LUKS_del_key(keyslot, &cd->hdr, cd);
1809 // activation/deactivation of device mapping
1810 int crypt_activate_by_passphrase(struct crypt_device *cd,
1813 const char *passphrase,
1814 size_t passphrase_size,
1817 crypt_status_info ci;
1818 struct volume_key *vk = NULL;
1819 char *read_passphrase = NULL;
1820 size_t passphraseLen = 0;
1823 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1824 name ? "Activating" : "Checking", name ?: "",
1825 keyslot, passphrase ? "" : "[none] ");
1828 ci = crypt_status(NULL, name);
1829 if (ci == CRYPT_INVALID)
1831 else if (ci >= CRYPT_ACTIVE) {
1832 log_err(cd, _("Device %s already exists.\n"), name);
1837 /* plain, use hashed passphrase */
1838 if (isPLAIN(cd->type)) {
1843 r = key_from_terminal(cd, NULL, &read_passphrase,
1847 passphrase = read_passphrase;
1848 passphrase_size = passphraseLen;
1851 r = process_key(cd, cd->plain_hdr.hash,
1853 passphrase, passphrase_size, &vk);
1857 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1859 } else if (isLUKS(cd->type)) {
1860 /* provided passphrase, do not retry */
1862 r = LUKS_open_key_with_hdr(keyslot, passphrase,
1863 passphrase_size, &cd->hdr, &vk, cd);
1865 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1870 r = LUKS1_activate(cd, name, vk, flags);
1875 crypt_safe_free(read_passphrase);
1876 crypt_free_volume_key(vk);
1878 return r < 0 ? r : keyslot;
1881 int crypt_activate_by_keyfile_offset(struct crypt_device *cd,
1884 const char *keyfile,
1885 size_t keyfile_size,
1886 size_t keyfile_offset,
1889 crypt_status_info ci;
1890 struct volume_key *vk = NULL;
1891 char *passphrase_read = NULL;
1892 size_t passphrase_size_read;
1893 unsigned int key_count = 0;
1896 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1897 name ?: "", keyslot, keyfile ?: "[none]");
1900 ci = crypt_status(NULL, name);
1901 if (ci == CRYPT_INVALID)
1903 else if (ci >= CRYPT_ACTIVE) {
1904 log_err(cd, _("Device %s already exists.\n"), name);
1912 if (isPLAIN(cd->type)) {
1916 r = key_from_file(cd, _("Enter passphrase: "),
1917 &passphrase_read, &passphrase_size_read,
1918 keyfile, keyfile_offset, keyfile_size);
1922 r = process_key(cd, cd->plain_hdr.hash,
1924 passphrase_read, passphrase_size_read, &vk);
1928 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1929 } else if (isLUKS(cd->type)) {
1930 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1931 &passphrase_size_read, keyfile, keyfile_offset, keyfile_size);
1934 r = LUKS_open_key_with_hdr(keyslot, passphrase_read,
1935 passphrase_size_read, &cd->hdr, &vk, cd);
1941 r = LUKS1_activate(cd, name, vk, flags);
1946 } else if (isLOOPAES(cd->type)) {
1947 r = key_from_file(cd, NULL, &passphrase_read, &passphrase_size_read,
1948 keyfile, keyfile_offset, keyfile_size);
1951 r = LOOPAES_parse_keyfile(cd, &vk, cd->loopaes_hdr.hash, &key_count,
1952 passphrase_read, passphrase_size_read);
1956 r = LOOPAES_activate(cd, name, cd->loopaes_cipher,
1957 key_count, vk, flags);
1962 crypt_safe_free(passphrase_read);
1963 crypt_free_volume_key(vk);
1968 int crypt_activate_by_keyfile(struct crypt_device *cd,
1971 const char *keyfile,
1972 size_t keyfile_size,
1975 return crypt_activate_by_keyfile_offset(cd, name, keyslot, keyfile,
1976 keyfile_size, 0, flags);
1979 int crypt_activate_by_volume_key(struct crypt_device *cd,
1981 const char *volume_key,
1982 size_t volume_key_size,
1985 crypt_status_info ci;
1986 struct volume_key *vk = NULL;
1989 log_dbg("Activating volume %s by volume key.", name ?: "[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 /* use key directly, no hash */
2002 if (isPLAIN(cd->type)) {
2006 if (!volume_key || !volume_key_size || volume_key_size != cd->plain_key_size) {
2007 log_err(cd, _("Incorrect volume key specified for plain device.\n"));
2011 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2015 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
2016 } else if (isLUKS(cd->type)) {
2017 /* If key is not provided, try to use internal key */
2019 if (!cd->volume_key) {
2020 log_err(cd, _("Volume key does not match the volume.\n"));
2023 volume_key_size = cd->volume_key->keylength;
2024 volume_key = cd->volume_key->key;
2027 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2030 r = LUKS_verify_volume_key(&cd->hdr, vk);
2033 log_err(cd, _("Volume key does not match the volume.\n"));
2036 r = LUKS1_activate(cd, name, vk, flags);
2037 } else if (isVERITY(cd->type)) {
2038 /* volume_key == root hash */
2039 if (!volume_key || !volume_key_size) {
2040 log_err(cd, _("Incorrect root hash specified for verity device.\n"));
2044 r = VERITY_activate(cd, name, volume_key, volume_key_size,
2045 &cd->verity_hdr, CRYPT_ACTIVATE_READONLY);
2048 free(cd->verity_root_hash);
2049 cd->verity_root_hash = NULL;
2051 cd->verity_root_hash_size = volume_key_size;
2052 if (!cd->verity_root_hash)
2053 cd->verity_root_hash = malloc(volume_key_size);
2054 if (cd->verity_root_hash)
2055 memcpy(cd->verity_root_hash, volume_key, volume_key_size);
2057 } else if (isTCRYPT(cd->type)) {
2060 r = TCRYPT_activate(cd, name, &cd->tcrypt_hdr,
2061 &cd->tcrypt_params, flags);
2063 log_err(cd, _("Device type is not properly initialised.\n"));
2065 crypt_free_volume_key(vk);
2070 int crypt_deactivate(struct crypt_device *cd, const char *name)
2077 log_dbg("Deactivating volume %s.", name);
2082 switch (crypt_status(cd, name)) {
2085 if (isTCRYPT(cd->type))
2086 r = TCRYPT_deactivate(cd, name);
2088 r = dm_remove_device(cd, name, 0, 0);
2090 case CRYPT_INACTIVE:
2091 log_err(cd, _("Device %s is not active.\n"), name);
2095 log_err(cd, _("Invalid device %s.\n"), name);
2105 int crypt_volume_key_get(struct crypt_device *cd,
2108 size_t *volume_key_size,
2109 const char *passphrase,
2110 size_t passphrase_size)
2112 struct volume_key *vk = NULL;
2116 if (crypt_fips_mode()) {
2117 log_err(cd, "Function not available in FIPS mode.\n");
2121 key_len = crypt_get_volume_key_size(cd);
2122 if (key_len > *volume_key_size) {
2123 log_err(cd, _("Volume key buffer too small.\n"));
2127 if (isPLAIN(cd->type) && cd->plain_hdr.hash) {
2128 r = process_key(cd, cd->plain_hdr.hash, key_len,
2129 passphrase, passphrase_size, &vk);
2131 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
2132 } else if (isLUKS(cd->type)) {
2133 r = LUKS_open_key_with_hdr(keyslot, passphrase,
2134 passphrase_size, &cd->hdr, &vk, cd);
2137 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
2140 memcpy(volume_key, vk->key, vk->keylength);
2141 *volume_key_size = vk->keylength;
2144 crypt_free_volume_key(vk);
2148 int crypt_volume_key_verify(struct crypt_device *cd,
2149 const char *volume_key,
2150 size_t volume_key_size)
2152 struct volume_key *vk;
2155 if (!isLUKS(cd->type)) {
2156 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2160 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2164 r = LUKS_verify_volume_key(&cd->hdr, vk);
2167 log_err(cd, _("Volume key does not match the volume.\n"));
2169 crypt_free_volume_key(vk);
2174 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
2176 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
2177 cd->timeout = timeout_sec;
2180 void crypt_set_password_retry(struct crypt_device *cd, int tries)
2182 log_dbg("Password retry count set to %d.", tries);
2186 void crypt_set_iteration_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2188 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
2189 cd->iteration_time = iteration_time_ms;
2191 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2193 crypt_set_iteration_time(cd, iteration_time_ms);
2196 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
2198 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
2199 cd->password_verify = password_verify ? 1 : 0;
2202 void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
2205 case CRYPT_RNG_URANDOM:
2206 case CRYPT_RNG_RANDOM:
2207 log_dbg("RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
2208 cd->rng_type = rng_type;
2212 int crypt_get_rng_type(struct crypt_device *cd)
2217 return cd->rng_type;
2220 int crypt_memory_lock(struct crypt_device *cd, int lock)
2222 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
2226 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
2233 r = dm_status_device(cd, name);
2238 if (r < 0 && r != -ENODEV)
2239 return CRYPT_INVALID;
2242 return CRYPT_ACTIVE;
2247 return CRYPT_INACTIVE;
2250 static void hexprint(struct crypt_device *cd, const char *d, int n, const char *sep)
2253 for(i = 0; i < n; i++)
2254 log_std(cd, "%02hhx%s", (const char)d[i], sep);
2257 static int _luks_dump(struct crypt_device *cd)
2261 log_std(cd, "LUKS header information for %s\n\n", mdata_device_path(cd));
2262 log_std(cd, "Version: \t%d\n", cd->hdr.version);
2263 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
2264 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
2265 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec);
2266 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
2267 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
2268 log_std(cd, "MK digest: \t");
2269 hexprint(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE, " ");
2271 log_std(cd, "MK salt: \t");
2272 hexprint(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2, " ");
2273 log_std(cd, "\n \t");
2274 hexprint(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ");
2276 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
2277 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
2278 for(i = 0; i < LUKS_NUMKEYS; i++) {
2279 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
2280 log_std(cd, "Key Slot %d: ENABLED\n",i);
2281 log_std(cd, "\tIterations: \t%d\n",
2282 cd->hdr.keyblock[i].passwordIterations);
2283 log_std(cd, "\tSalt: \t");
2284 hexprint(cd, cd->hdr.keyblock[i].passwordSalt,
2285 LUKS_SALTSIZE/2, " ");
2286 log_std(cd, "\n\t \t");
2287 hexprint(cd, cd->hdr.keyblock[i].passwordSalt +
2288 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ");
2291 log_std(cd, "\tKey material offset:\t%d\n",
2292 cd->hdr.keyblock[i].keyMaterialOffset);
2293 log_std(cd, "\tAF stripes: \t%d\n",
2294 cd->hdr.keyblock[i].stripes);
2297 log_std(cd, "Key Slot %d: DISABLED\n", i);
2302 static int _verity_dump(struct crypt_device *cd)
2304 log_std(cd, "VERITY header information for %s\n", mdata_device_path(cd));
2305 log_std(cd, "UUID: \t%s\n", cd->verity_uuid ?: "");
2306 log_std(cd, "Hash type: \t%u\n", cd->verity_hdr.hash_type);
2307 log_std(cd, "Data blocks: \t%" PRIu64 "\n", cd->verity_hdr.data_size);
2308 log_std(cd, "Data block size: \t%u\n", cd->verity_hdr.data_block_size);
2309 log_std(cd, "Hash block size: \t%u\n", cd->verity_hdr.hash_block_size);
2310 log_std(cd, "Hash algorithm: \t%s\n", cd->verity_hdr.hash_name);
2311 log_std(cd, "Salt: \t");
2312 if (cd->verity_hdr.salt_size)
2313 hexprint(cd, cd->verity_hdr.salt, cd->verity_hdr.salt_size, "");
2317 if (cd->verity_root_hash) {
2318 log_std(cd, "Root hash: \t");
2319 hexprint(cd, cd->verity_root_hash, cd->verity_root_hash_size, "");
2325 int crypt_dump(struct crypt_device *cd)
2327 if (isLUKS(cd->type))
2328 return _luks_dump(cd);
2329 else if (isVERITY(cd->type))
2330 return _verity_dump(cd);
2332 log_err(cd, _("Dump operation is not supported for this device type.\n"));
2336 const char *crypt_get_cipher(struct crypt_device *cd)
2338 if (isPLAIN(cd->type))
2339 return cd->plain_cipher;
2341 if (isLUKS(cd->type))
2342 return cd->hdr.cipherName;
2344 if (isLOOPAES(cd->type))
2345 return cd->loopaes_cipher;
2350 const char *crypt_get_cipher_mode(struct crypt_device *cd)
2352 if (isPLAIN(cd->type))
2353 return cd->plain_cipher_mode;
2355 if (isLUKS(cd->type))
2356 return cd->hdr.cipherMode;
2358 if (isLOOPAES(cd->type))
2359 return cd->loopaes_cipher_mode;
2364 const char *crypt_get_uuid(struct crypt_device *cd)
2366 if (isLUKS(cd->type))
2367 return cd->hdr.uuid;
2369 if (isPLAIN(cd->type))
2370 return cd->plain_uuid;
2372 if (isLOOPAES(cd->type))
2373 return cd->loopaes_uuid;
2375 if (isVERITY(cd->type))
2376 return cd->verity_uuid;
2381 const char *crypt_get_device_name(struct crypt_device *cd)
2383 const char *path = device_block_path(cd->device);
2386 path = device_path(cd->device);
2391 int crypt_get_volume_key_size(struct crypt_device *cd)
2393 if (isPLAIN(cd->type))
2394 return cd->plain_key_size;
2396 if (isLUKS(cd->type))
2397 return cd->hdr.keyBytes;
2399 if (isLOOPAES(cd->type))
2400 return cd->loopaes_key_size;
2402 if (isVERITY(cd->type))
2403 return cd->verity_root_hash_size;
2408 uint64_t crypt_get_data_offset(struct crypt_device *cd)
2410 if (isPLAIN(cd->type))
2411 return cd->plain_hdr.offset;
2413 if (isLUKS(cd->type))
2414 return cd->hdr.payloadOffset;
2416 if (isLOOPAES(cd->type))
2417 return cd->loopaes_hdr.offset;
2419 if (isTCRYPT(cd->type)) { // FIXME: system vol.
2420 if (!cd->tcrypt_hdr.d.mk_offset)
2422 return (cd->tcrypt_hdr.d.mk_offset / cd->tcrypt_hdr.d.sector_size);
2428 uint64_t crypt_get_iv_offset(struct crypt_device *cd)
2430 if (isPLAIN(cd->type))
2431 return cd->plain_hdr.skip;
2433 if (isLUKS(cd->type))
2436 if (isLOOPAES(cd->type))
2437 return cd->loopaes_hdr.skip;
2439 if (isTCRYPT(cd->type)) {
2440 if (!cd->tcrypt_hdr.d.mk_offset)
2442 return (cd->tcrypt_hdr.d.mk_offset / cd->tcrypt_hdr.d.sector_size);
2448 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
2450 if (!isLUKS(cd->type)) {
2451 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2452 return CRYPT_SLOT_INVALID;
2455 return LUKS_keyslot_info(&cd->hdr, keyslot);
2458 int crypt_keyslot_max(const char *type)
2460 if (type && isLUKS(type))
2461 return LUKS_NUMKEYS;
2466 int crypt_keyslot_area(struct crypt_device *cd,
2471 if (!isLUKS(cd->type))
2474 return LUKS_keyslot_area(&cd->hdr, keyslot, offset, length);
2477 const char *crypt_get_type(struct crypt_device *cd)
2482 int crypt_get_verity_info(struct crypt_device *cd,
2483 struct crypt_params_verity *vp)
2485 if (!isVERITY(cd->type) || !vp)
2488 vp->data_device = device_path(cd->device);
2489 vp->hash_device = mdata_device_path(cd);
2490 vp->hash_name = cd->verity_hdr.hash_name;
2491 vp->salt = cd->verity_hdr.salt;
2492 vp->salt_size = cd->verity_hdr.salt_size;
2493 vp->data_block_size = cd->verity_hdr.data_block_size;
2494 vp->hash_block_size = cd->verity_hdr.hash_block_size;
2495 vp->data_size = cd->verity_hdr.data_size;
2496 vp->hash_area_offset = cd->verity_hdr.hash_area_offset;
2497 vp->hash_type = cd->verity_hdr.hash_type;
2498 vp->flags = cd->verity_hdr.flags & CRYPT_VERITY_NO_HEADER;
2502 int crypt_get_active_device(struct crypt_device *cd __attribute__((unused)),
2504 struct crypt_active_device *cad)
2506 struct crypt_dm_active_device dmd;
2509 r = dm_query_device(cd, name, 0, &dmd);
2513 if (dmd.target != DM_CRYPT && dmd.target != DM_VERITY)
2516 cad->offset = dmd.u.crypt.offset;
2517 cad->iv_offset = dmd.u.crypt.iv_offset;
2518 cad->size = dmd.size;
2519 cad->flags = dmd.flags;