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)) {
754 r = TCRYPT_init_by_name(cd, name, &dmd, &cd->tcrypt_params,
758 crypt_free_volume_key(dmd.u.crypt.vk);
759 device_free(dmd.data_device);
760 free(CONST_CAST(void*)dmd.u.crypt.cipher);
761 free(CONST_CAST(void*)dmd.uuid);
765 static int _init_by_name_verity(struct crypt_device *cd, const char *name)
767 struct crypt_params_verity params = {};
768 struct crypt_dm_active_device dmd = {
770 .u.verity.vp = ¶ms,
774 r = dm_query_device(cd, name,
777 DM_ACTIVE_VERITY_HASH_DEVICE |
778 DM_ACTIVE_VERITY_PARAMS, &dmd);
782 if (isVERITY(cd->type)) {
783 cd->verity_uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
784 cd->verity_hdr.flags = CRYPT_VERITY_NO_HEADER; //FIXME
785 cd->verity_hdr.data_size = params.data_size;
786 cd->verity_root_hash_size = dmd.u.verity.root_hash_size;
787 cd->verity_root_hash = NULL;
788 cd->verity_hdr.hash_name = params.hash_name;
789 cd->verity_hdr.data_device = NULL;
790 cd->verity_hdr.hash_device = NULL;
791 cd->verity_hdr.data_block_size = params.data_block_size;
792 cd->verity_hdr.hash_block_size = params.hash_block_size;
793 cd->verity_hdr.hash_area_offset = dmd.u.verity.hash_offset;
794 cd->verity_hdr.hash_type = params.hash_type;
795 cd->verity_hdr.flags = params.flags;
796 cd->verity_hdr.salt_size = params.salt_size;
797 cd->verity_hdr.salt = params.salt;
798 cd->metadata_device = dmd.u.verity.hash_device;
801 device_free(dmd.data_device);
802 free(CONST_CAST(void*)dmd.uuid);
806 int crypt_init_by_name_and_header(struct crypt_device **cd,
808 const char *header_device)
810 crypt_status_info ci;
811 struct crypt_dm_active_device dmd;
814 log_dbg("Allocating crypt device context by device %s.", name);
816 ci = crypt_status(NULL, name);
817 if (ci == CRYPT_INVALID)
820 if (ci < CRYPT_ACTIVE) {
821 log_err(NULL, _("Device %s is not active.\n"), name);
825 r = dm_query_device(NULL, name, DM_ACTIVE_DEVICE | DM_ACTIVE_UUID, &dmd);
832 r = crypt_init(cd, header_device);
834 r = crypt_init(cd, device_path(dmd.data_device));
836 /* Underlying device disappeared but mapping still active */
837 if (!dmd.data_device || r == -ENOTBLK)
838 log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
841 /* Underlying device is not readable but crypt mapping exists */
843 device_free(dmd.data_device);
844 dmd.data_device = NULL;
845 r = crypt_init(cd, NULL);
853 if (!strncmp(CRYPT_PLAIN, dmd.uuid, sizeof(CRYPT_PLAIN)-1))
854 (*cd)->type = strdup(CRYPT_PLAIN);
855 else if (!strncmp(CRYPT_LOOPAES, dmd.uuid, sizeof(CRYPT_LOOPAES)-1))
856 (*cd)->type = strdup(CRYPT_LOOPAES);
857 else if (!strncmp(CRYPT_LUKS1, dmd.uuid, sizeof(CRYPT_LUKS1)-1))
858 (*cd)->type = strdup(CRYPT_LUKS1);
859 else if (!strncmp(CRYPT_VERITY, dmd.uuid, sizeof(CRYPT_VERITY)-1))
860 (*cd)->type = strdup(CRYPT_VERITY);
861 else if (!strncmp(CRYPT_TCRYPT, dmd.uuid, sizeof(CRYPT_TCRYPT)-1))
862 (*cd)->type = strdup(CRYPT_TCRYPT);
864 log_dbg("Unknown UUID set, some parameters are not set.");
866 log_dbg("Active device has no UUID set, some parameters are not set.");
869 r = crypt_set_data_device(*cd, device_path(dmd.data_device));
874 /* Try to initialise basic parameters from active device */
876 if (dmd.target == DM_CRYPT)
877 r = _init_by_name_crypt(*cd, name);
878 else if (dmd.target == DM_VERITY)
879 r = _init_by_name_verity(*cd, name);
885 device_free(dmd.data_device);
886 free(CONST_CAST(void*)dmd.uuid);
890 int crypt_init_by_name(struct crypt_device **cd, const char *name)
892 return crypt_init_by_name_and_header(cd, name, NULL);
895 static int _crypt_format_plain(struct crypt_device *cd,
897 const char *cipher_mode,
899 size_t volume_key_size,
900 struct crypt_params_plain *params)
902 if (!cipher || !cipher_mode) {
903 log_err(cd, _("Invalid plain crypt parameters.\n"));
907 if (volume_key_size > 1024) {
908 log_err(cd, _("Invalid key size.\n"));
912 if (!(cd->type = strdup(CRYPT_PLAIN)))
915 cd->plain_key_size = volume_key_size;
916 cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
920 cd->plain_cipher = strdup(cipher);
921 cd->plain_cipher_mode = strdup(cipher_mode);
924 cd->plain_uuid = strdup(uuid);
926 if (params && params->hash)
927 cd->plain_hdr.hash = strdup(params->hash);
929 cd->plain_hdr.offset = params ? params->offset : 0;
930 cd->plain_hdr.skip = params ? params->skip : 0;
931 cd->plain_hdr.size = params ? params->size : 0;
933 if (!cd->plain_cipher || !cd->plain_cipher_mode)
939 static int _crypt_format_luks1(struct crypt_device *cd,
941 const char *cipher_mode,
943 const char *volume_key,
944 size_t volume_key_size,
945 struct crypt_params_luks1 *params)
948 unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
949 unsigned long alignment_offset = 0;
951 if (!crypt_metadata_device(cd)) {
952 log_err(cd, _("Can't format LUKS without device.\n"));
956 if (!(cd->type = strdup(CRYPT_LUKS1)))
960 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
963 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
968 if (params && params->data_device) {
969 cd->metadata_device = cd->device;
971 if (device_alloc(&cd->device, params->data_device) < 0)
973 required_alignment = params->data_alignment * SECTOR_SIZE;
974 } else if (params && params->data_alignment) {
975 required_alignment = params->data_alignment * SECTOR_SIZE;
977 device_topology_alignment(cd->device,
979 &alignment_offset, DEFAULT_DISK_ALIGNMENT);
981 /* Check early if we cannot allocate block device for key slot access */
982 r = device_block_adjust(cd, cd->device, DEV_OK, 0, NULL, NULL);
986 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
987 (params && params->hash) ? params->hash : "sha1",
989 required_alignment / SECTOR_SIZE,
990 alignment_offset / SECTOR_SIZE,
991 cd->iteration_time, &cd->PBKDF2_per_sec,
992 cd->metadata_device ? 1 : 0, cd);
996 /* Wipe first 8 sectors - fs magic numbers etc. */
997 r = crypt_wipe(crypt_metadata_device(cd), 0, 8 * SECTOR_SIZE, CRYPT_WIPE_ZERO, 1);
1000 log_err(cd, _("Cannot format device %s which is still in use.\n"),
1001 mdata_device_path(cd));
1002 else if (r == -EACCES) {
1003 log_err(cd, _("Cannot format device %s, permission denied.\n"),
1004 mdata_device_path(cd));
1007 log_err(cd, _("Cannot wipe header on device %s.\n"),
1008 mdata_device_path(cd));
1013 r = LUKS_write_phdr(&cd->hdr, cd);
1018 static int _crypt_format_loopaes(struct crypt_device *cd,
1021 size_t volume_key_size,
1022 struct crypt_params_loopaes *params)
1024 if (!crypt_metadata_device(cd)) {
1025 log_err(cd, _("Can't format LOOPAES without device.\n"));
1029 if (volume_key_size > 1024) {
1030 log_err(cd, _("Invalid key size.\n"));
1034 if (!(cd->type = strdup(CRYPT_LOOPAES)))
1037 cd->loopaes_key_size = volume_key_size;
1039 cd->loopaes_cipher = strdup(cipher ?: DEFAULT_LOOPAES_CIPHER);
1042 cd->loopaes_uuid = strdup(uuid);
1044 if (params && params->hash)
1045 cd->loopaes_hdr.hash = strdup(params->hash);
1047 cd->loopaes_hdr.offset = params ? params->offset : 0;
1048 cd->loopaes_hdr.skip = params ? params->skip : 0;
1053 static int _crypt_format_verity(struct crypt_device *cd,
1055 struct crypt_params_verity *params)
1057 int r = 0, hash_size;
1058 uint64_t data_device_size;
1060 if (!crypt_metadata_device(cd)) {
1061 log_err(cd, _("Can't format VERITY without device.\n"));
1065 if (!params || !params->data_device)
1068 if (params->hash_type > VERITY_MAX_HASH_TYPE) {
1069 log_err(cd, _("Unsupported VERITY hash type %d.\n"), params->hash_type);
1073 if (VERITY_BLOCK_SIZE_OK(params->data_block_size) ||
1074 VERITY_BLOCK_SIZE_OK(params->hash_block_size)) {
1075 log_err(cd, _("Unsupported VERITY block size.\n"));
1079 if (params->hash_area_offset % 512) {
1080 log_err(cd, _("Unsupported VERITY hash offset.\n"));
1084 if (!(cd->type = strdup(CRYPT_VERITY)))
1087 r = crypt_set_data_device(cd, params->data_device);
1090 if (!params->data_size) {
1091 r = device_size(cd->device, &data_device_size);
1095 cd->verity_hdr.data_size = data_device_size / params->data_block_size;
1097 cd->verity_hdr.data_size = params->data_size;
1099 hash_size = crypt_hash_size(params->hash_name);
1100 if (hash_size <= 0) {
1101 log_err(cd, _("Hash algorithm %s not supported.\n"),
1105 cd->verity_root_hash_size = hash_size;
1107 cd->verity_root_hash = malloc(cd->verity_root_hash_size);
1108 if (!cd->verity_root_hash)
1111 cd->verity_hdr.flags = params->flags;
1112 if (!(cd->verity_hdr.hash_name = strdup(params->hash_name)))
1114 cd->verity_hdr.data_device = NULL;
1115 cd->verity_hdr.data_block_size = params->data_block_size;
1116 cd->verity_hdr.hash_block_size = params->hash_block_size;
1117 cd->verity_hdr.hash_area_offset = params->hash_area_offset;
1118 cd->verity_hdr.hash_type = params->hash_type;
1119 cd->verity_hdr.flags = params->flags;
1120 cd->verity_hdr.salt_size = params->salt_size;
1121 if (!(cd->verity_hdr.salt = malloc(params->salt_size)))
1125 memcpy(CONST_CAST(char*)cd->verity_hdr.salt, params->salt,
1128 r = crypt_random_get(cd, CONST_CAST(char*)cd->verity_hdr.salt,
1129 params->salt_size, CRYPT_RND_SALT);
1133 if (params->flags & CRYPT_VERITY_CREATE_HASH) {
1134 r = VERITY_create(cd, &cd->verity_hdr,
1135 cd->verity_root_hash, cd->verity_root_hash_size);
1140 if (!(params->flags & CRYPT_VERITY_NO_HEADER)) {
1142 cd->verity_uuid = strdup(uuid);
1144 r = VERITY_UUID_generate(cd, &cd->verity_uuid);
1149 r = VERITY_write_sb(cd, cd->verity_hdr.hash_area_offset,
1156 int crypt_format(struct crypt_device *cd,
1159 const char *cipher_mode,
1161 const char *volume_key,
1162 size_t volume_key_size,
1171 log_dbg("Context already formatted as %s.", cd->type);
1175 log_dbg("Formatting device %s as type %s.", mdata_device_path(cd) ?: "(none)", type);
1177 r = init_crypto(cd);
1182 r = _crypt_format_plain(cd, cipher, cipher_mode,
1183 uuid, volume_key_size, params);
1184 else if (isLUKS(type))
1185 r = _crypt_format_luks1(cd, cipher, cipher_mode,
1186 uuid, volume_key, volume_key_size, params);
1187 else if (isLOOPAES(type))
1188 r = _crypt_format_loopaes(cd, cipher, uuid, volume_key_size, params);
1189 else if (isVERITY(type))
1190 r = _crypt_format_verity(cd, uuid, params);
1192 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
1199 crypt_free_volume_key(cd->volume_key);
1200 cd->volume_key = NULL;
1206 int crypt_load(struct crypt_device *cd,
1207 const char *requested_type,
1212 log_dbg("Trying to load %s crypt type from device %s.",
1213 requested_type ?: "any", mdata_device_path(cd) ?: "(none)");
1215 if (!crypt_metadata_device(cd))
1218 if (!requested_type || isLUKS(requested_type)) {
1219 if (cd->type && !isLUKS(cd->type)) {
1220 log_dbg("Context is already initialised to type %s", cd->type);
1224 r = _crypt_load_luks1(cd, 1, 0);
1225 } else if (isVERITY(requested_type)) {
1226 if (cd->type && !isVERITY(cd->type)) {
1227 log_dbg("Context is already initialised to type %s", cd->type);
1230 r = _crypt_load_verity(cd, params);
1231 } else if (isTCRYPT(requested_type)) {
1232 if (cd->type && !isTCRYPT(cd->type)) {
1233 log_dbg("Context is already initialised to type %s", cd->type);
1236 r = _crypt_load_tcrypt(cd, params);
1243 int crypt_repair(struct crypt_device *cd,
1244 const char *requested_type,
1245 void *params __attribute__((unused)))
1249 log_dbg("Trying to repair %s crypt type from device %s.",
1250 requested_type ?: "any", mdata_device_path(cd) ?: "(none)");
1252 if (!crypt_metadata_device(cd))
1255 if (requested_type && !isLUKS(requested_type))
1259 /* Load with repair */
1260 r = _crypt_load_luks1(cd, 1, 1);
1264 /* cd->type and header must be set in context */
1265 r = crypt_check_data_device_size(cd);
1274 int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
1276 struct crypt_dm_active_device dmd;
1279 /* Device context type must be initialised */
1280 if (!cd->type || !crypt_get_uuid(cd))
1283 log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
1285 r = dm_query_device(cd, name, DM_ACTIVE_DEVICE | DM_ACTIVE_CRYPT_CIPHER |
1286 DM_ACTIVE_UUID | DM_ACTIVE_CRYPT_KEYSIZE |
1287 DM_ACTIVE_CRYPT_KEY, &dmd);
1289 log_err(NULL, _("Device %s is not active.\n"), name);
1293 if (!dmd.uuid || dmd.target != DM_CRYPT) {
1298 r = device_block_adjust(cd, dmd.data_device, DEV_OK,
1299 dmd.u.crypt.offset, &new_size, &dmd.flags);
1303 if (new_size == dmd.size) {
1304 log_dbg("Device has already requested size %" PRIu64
1305 " sectors.", dmd.size);
1308 dmd.size = new_size;
1309 if (isTCRYPT(cd->type))
1312 r = dm_create_device(cd, name, cd->type, &dmd, 1);
1315 if (dmd.target == DM_CRYPT) {
1316 crypt_free_volume_key(dmd.u.crypt.vk);
1317 free(CONST_CAST(void*)dmd.u.crypt.cipher);
1319 free(CONST_CAST(void*)dmd.data_device);
1320 free(CONST_CAST(void*)dmd.uuid);
1325 int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
1327 if (!isLUKS(cd->type)) {
1328 log_err(cd, _("This operation is not supported for this device type.\n"));
1332 if (uuid && !strncmp(uuid, cd->hdr.uuid, sizeof(cd->hdr.uuid))) {
1333 log_dbg("UUID is the same as requested (%s) for device %s.",
1334 uuid, mdata_device_path(cd));
1339 log_dbg("Requested new UUID change to %s for %s.", uuid, mdata_device_path(cd));
1341 log_dbg("Requested new UUID refresh for %s.", mdata_device_path(cd));
1343 if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
1346 return LUKS_hdr_uuid_set(&cd->hdr, uuid, cd);
1349 int crypt_header_backup(struct crypt_device *cd,
1350 const char *requested_type,
1351 const char *backup_file)
1355 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1358 r = init_crypto(cd);
1362 log_dbg("Requested header backup of device %s (%s) to "
1363 "file %s.", mdata_device_path(cd), requested_type, backup_file);
1365 return LUKS_hdr_backup(backup_file, &cd->hdr, cd);
1368 int crypt_header_restore(struct crypt_device *cd,
1369 const char *requested_type,
1370 const char *backup_file)
1374 if (requested_type && !isLUKS(requested_type))
1377 r = init_crypto(cd);
1381 log_dbg("Requested header restore to device %s (%s) from "
1382 "file %s.", mdata_device_path(cd), requested_type, backup_file);
1384 return LUKS_hdr_restore(backup_file, &cd->hdr, cd);
1387 void crypt_free(struct crypt_device *cd)
1390 log_dbg("Releasing crypt device %s context.", mdata_device_path(cd));
1393 crypt_free_volume_key(cd->volume_key);
1395 device_free(cd->device);
1396 device_free(cd->metadata_device);
1399 /* used in plain device only */
1400 free(CONST_CAST(void*)cd->plain_hdr.hash);
1401 free(cd->plain_cipher);
1402 free(cd->plain_cipher_mode);
1403 free(cd->plain_uuid);
1405 /* used in loop-AES device only */
1406 free(CONST_CAST(void*)cd->loopaes_hdr.hash);
1407 free(cd->loopaes_cipher);
1408 free(cd->loopaes_uuid);
1410 /* used in verity device only */
1411 free(CONST_CAST(void*)cd->verity_hdr.hash_name);
1412 free(CONST_CAST(void*)cd->verity_hdr.salt);
1413 free(cd->verity_root_hash);
1414 free(cd->verity_uuid);
1420 int crypt_suspend(struct crypt_device *cd,
1423 crypt_status_info ci;
1426 log_dbg("Suspending volume %s.", name);
1428 if (!cd || !isLUKS(cd->type)) {
1429 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1434 ci = crypt_status(NULL, name);
1435 if (ci < CRYPT_ACTIVE) {
1436 log_err(cd, _("Volume %s is not active.\n"), name);
1442 r = dm_status_suspended(cd, name);
1447 log_err(cd, _("Volume %s is already suspended.\n"), name);
1452 r = dm_suspend_and_wipe_key(cd, name);
1454 log_err(cd, "Suspend is not supported for device %s.\n", name);
1456 log_err(cd, "Error during suspending device %s.\n", name);
1462 int crypt_resume_by_passphrase(struct crypt_device *cd,
1465 const char *passphrase,
1466 size_t passphrase_size)
1468 struct volume_key *vk = NULL;
1471 log_dbg("Resuming volume %s.", name);
1473 if (!isLUKS(cd->type)) {
1474 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1479 r = dm_status_suspended(cd, name);
1484 log_err(cd, _("Volume %s is not suspended.\n"), name);
1489 r = LUKS_open_key_with_hdr(keyslot, passphrase, passphrase_size,
1492 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1496 r = dm_resume_and_reinstate_key(cd, name, vk->keylength, vk->key);
1498 log_err(cd, "Resume is not supported for device %s.\n", name);
1500 log_err(cd, "Error during resuming device %s.\n", name);
1504 crypt_free_volume_key(vk);
1505 return r < 0 ? r : keyslot;
1508 int crypt_resume_by_keyfile_offset(struct crypt_device *cd,
1511 const char *keyfile,
1512 size_t keyfile_size,
1513 size_t keyfile_offset)
1515 struct volume_key *vk = NULL;
1516 char *passphrase_read = NULL;
1517 size_t passphrase_size_read;
1520 log_dbg("Resuming volume %s.", name);
1522 if (!isLUKS(cd->type)) {
1523 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1528 r = dm_status_suspended(cd, name);
1533 log_err(cd, _("Volume %s is not suspended.\n"), name);
1540 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1541 &passphrase_size_read, keyfile, keyfile_offset,
1546 r = LUKS_open_key_with_hdr(keyslot, passphrase_read,
1547 passphrase_size_read, &cd->hdr, &vk, cd);
1552 r = dm_resume_and_reinstate_key(cd, name, vk->keylength, vk->key);
1554 log_err(cd, "Error during resuming device %s.\n", name);
1556 crypt_safe_free(passphrase_read);
1557 crypt_free_volume_key(vk);
1558 return r < 0 ? r : keyslot;
1561 int crypt_resume_by_keyfile(struct crypt_device *cd,
1564 const char *keyfile,
1565 size_t keyfile_size)
1567 return crypt_resume_by_keyfile_offset(cd, name, keyslot,
1568 keyfile, keyfile_size, 0);
1571 // slot manipulation
1572 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1573 int keyslot, // -1 any
1574 const char *passphrase, // NULL -> terminal
1575 size_t passphrase_size,
1576 const char *new_passphrase, // NULL -> terminal
1577 size_t new_passphrase_size)
1579 struct volume_key *vk = NULL;
1580 char *password = NULL, *new_password = NULL;
1581 size_t passwordLen, new_passwordLen;
1584 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1585 "new passphrase %sprovided.",
1586 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1588 if (!isLUKS(cd->type)) {
1589 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1593 r = keyslot_verify_or_find_empty(cd, &keyslot);
1597 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1598 /* No slots used, try to use pre-generated key in header */
1599 if (cd->volume_key) {
1600 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1601 r = vk ? 0 : -ENOMEM;
1603 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1606 } else if (passphrase) {
1607 /* Passphrase provided, use it to unlock existing keyslot */
1608 r = LUKS_open_key_with_hdr(CRYPT_ANY_SLOT, passphrase,
1609 passphrase_size, &cd->hdr, &vk, cd);
1611 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1612 r = key_from_terminal(cd, _("Enter any passphrase: "),
1613 &password, &passwordLen, 0);
1617 r = LUKS_open_key_with_hdr(CRYPT_ANY_SLOT, password,
1618 passwordLen, &cd->hdr, &vk, cd);
1619 crypt_safe_free(password);
1625 if (new_passphrase) {
1626 new_password = CONST_CAST(char*)new_passphrase;
1627 new_passwordLen = new_passphrase_size;
1629 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1630 &new_password, &new_passwordLen, 1);
1635 r = LUKS_set_key(keyslot, new_password, new_passwordLen,
1636 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1641 if (!new_passphrase)
1642 crypt_safe_free(new_password);
1643 crypt_free_volume_key(vk);
1644 return r ?: keyslot;
1647 int crypt_keyslot_add_by_keyfile_offset(struct crypt_device *cd,
1649 const char *keyfile,
1650 size_t keyfile_size,
1651 size_t keyfile_offset,
1652 const char *new_keyfile,
1653 size_t new_keyfile_size,
1654 size_t new_keyfile_offset)
1656 struct volume_key *vk = NULL;
1657 char *password = NULL; size_t passwordLen;
1658 char *new_password = NULL; size_t new_passwordLen;
1661 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1662 keyfile ?: "[none]", new_keyfile ?: "[none]");
1664 if (!isLUKS(cd->type)) {
1665 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1669 r = keyslot_verify_or_find_empty(cd, &keyslot);
1673 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1674 /* No slots used, try to use pre-generated key in header */
1675 if (cd->volume_key) {
1676 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1677 r = vk ? 0 : -ENOMEM;
1679 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1683 /* Read password from file of (if NULL) from terminal */
1685 r = key_from_file(cd, _("Enter any passphrase: "),
1686 &password, &passwordLen,
1687 keyfile, keyfile_offset, keyfile_size);
1689 r = key_from_terminal(cd, _("Enter any passphrase: "),
1690 &password, &passwordLen, 0);
1694 r = LUKS_open_key_with_hdr(CRYPT_ANY_SLOT, password, passwordLen,
1702 r = key_from_file(cd, _("Enter new passphrase for key slot: "),
1703 &new_password, &new_passwordLen, new_keyfile,
1704 new_keyfile_offset, new_keyfile_size);
1706 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1707 &new_password, &new_passwordLen, 1);
1711 r = LUKS_set_key(keyslot, new_password, new_passwordLen,
1712 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1714 crypt_safe_free(password);
1715 crypt_safe_free(new_password);
1716 crypt_free_volume_key(vk);
1717 return r < 0 ? r : keyslot;
1720 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1722 const char *keyfile,
1723 size_t keyfile_size,
1724 const char *new_keyfile,
1725 size_t new_keyfile_size)
1727 return crypt_keyslot_add_by_keyfile_offset(cd, keyslot,
1728 keyfile, keyfile_size, 0,
1729 new_keyfile, new_keyfile_size, 0);
1732 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1734 const char *volume_key,
1735 size_t volume_key_size,
1736 const char *passphrase,
1737 size_t passphrase_size)
1739 struct volume_key *vk = NULL;
1741 char *new_password = NULL; size_t new_passwordLen;
1743 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1745 if (!isLUKS(cd->type)) {
1746 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1751 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1752 else if (cd->volume_key)
1753 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1758 r = LUKS_verify_volume_key(&cd->hdr, vk);
1760 log_err(cd, _("Volume key does not match the volume.\n"));
1764 r = keyslot_verify_or_find_empty(cd, &keyslot);
1769 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1770 &new_password, &new_passwordLen, 1);
1773 passphrase = new_password;
1774 passphrase_size = new_passwordLen;
1777 r = LUKS_set_key(keyslot, passphrase, passphrase_size,
1778 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1780 crypt_safe_free(new_password);
1781 crypt_free_volume_key(vk);
1782 return (r < 0) ? r : keyslot;
1785 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1787 crypt_keyslot_info ki;
1789 log_dbg("Destroying keyslot %d.", keyslot);
1791 if (!isLUKS(cd->type)) {
1792 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1796 ki = crypt_keyslot_status(cd, keyslot);
1797 if (ki == CRYPT_SLOT_INVALID) {
1798 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1802 if (ki == CRYPT_SLOT_INACTIVE) {
1803 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1807 return LUKS_del_key(keyslot, &cd->hdr, cd);
1810 // activation/deactivation of device mapping
1811 int crypt_activate_by_passphrase(struct crypt_device *cd,
1814 const char *passphrase,
1815 size_t passphrase_size,
1818 crypt_status_info ci;
1819 struct volume_key *vk = NULL;
1820 char *read_passphrase = NULL;
1821 size_t passphraseLen = 0;
1824 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1825 name ? "Activating" : "Checking", name ?: "",
1826 keyslot, passphrase ? "" : "[none] ");
1829 ci = crypt_status(NULL, name);
1830 if (ci == CRYPT_INVALID)
1832 else if (ci >= CRYPT_ACTIVE) {
1833 log_err(cd, _("Device %s already exists.\n"), name);
1838 /* plain, use hashed passphrase */
1839 if (isPLAIN(cd->type)) {
1844 r = key_from_terminal(cd, NULL, &read_passphrase,
1848 passphrase = read_passphrase;
1849 passphrase_size = passphraseLen;
1852 r = process_key(cd, cd->plain_hdr.hash,
1854 passphrase, passphrase_size, &vk);
1858 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1860 } else if (isLUKS(cd->type)) {
1861 /* provided passphrase, do not retry */
1863 r = LUKS_open_key_with_hdr(keyslot, passphrase,
1864 passphrase_size, &cd->hdr, &vk, cd);
1866 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1871 r = LUKS1_activate(cd, name, vk, flags);
1876 crypt_safe_free(read_passphrase);
1877 crypt_free_volume_key(vk);
1879 return r < 0 ? r : keyslot;
1882 int crypt_activate_by_keyfile_offset(struct crypt_device *cd,
1885 const char *keyfile,
1886 size_t keyfile_size,
1887 size_t keyfile_offset,
1890 crypt_status_info ci;
1891 struct volume_key *vk = NULL;
1892 char *passphrase_read = NULL;
1893 size_t passphrase_size_read;
1894 unsigned int key_count = 0;
1897 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1898 name ?: "", keyslot, keyfile ?: "[none]");
1901 ci = crypt_status(NULL, name);
1902 if (ci == CRYPT_INVALID)
1904 else if (ci >= CRYPT_ACTIVE) {
1905 log_err(cd, _("Device %s already exists.\n"), name);
1913 if (isPLAIN(cd->type)) {
1917 r = key_from_file(cd, _("Enter passphrase: "),
1918 &passphrase_read, &passphrase_size_read,
1919 keyfile, keyfile_offset, keyfile_size);
1923 r = process_key(cd, cd->plain_hdr.hash,
1925 passphrase_read, passphrase_size_read, &vk);
1929 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1930 } else if (isLUKS(cd->type)) {
1931 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1932 &passphrase_size_read, keyfile, keyfile_offset, keyfile_size);
1935 r = LUKS_open_key_with_hdr(keyslot, passphrase_read,
1936 passphrase_size_read, &cd->hdr, &vk, cd);
1942 r = LUKS1_activate(cd, name, vk, flags);
1947 } else if (isLOOPAES(cd->type)) {
1948 r = key_from_file(cd, NULL, &passphrase_read, &passphrase_size_read,
1949 keyfile, keyfile_offset, keyfile_size);
1952 r = LOOPAES_parse_keyfile(cd, &vk, cd->loopaes_hdr.hash, &key_count,
1953 passphrase_read, passphrase_size_read);
1957 r = LOOPAES_activate(cd, name, cd->loopaes_cipher,
1958 key_count, vk, flags);
1963 crypt_safe_free(passphrase_read);
1964 crypt_free_volume_key(vk);
1969 int crypt_activate_by_keyfile(struct crypt_device *cd,
1972 const char *keyfile,
1973 size_t keyfile_size,
1976 return crypt_activate_by_keyfile_offset(cd, name, keyslot, keyfile,
1977 keyfile_size, 0, flags);
1980 int crypt_activate_by_volume_key(struct crypt_device *cd,
1982 const char *volume_key,
1983 size_t volume_key_size,
1986 crypt_status_info ci;
1987 struct volume_key *vk = NULL;
1990 log_dbg("Activating volume %s by volume key.", name ?: "[none]");
1993 ci = crypt_status(NULL, name);
1994 if (ci == CRYPT_INVALID)
1996 else if (ci >= CRYPT_ACTIVE) {
1997 log_err(cd, _("Device %s already exists.\n"), name);
2002 /* use key directly, no hash */
2003 if (isPLAIN(cd->type)) {
2007 if (!volume_key || !volume_key_size || volume_key_size != cd->plain_key_size) {
2008 log_err(cd, _("Incorrect volume key specified for plain device.\n"));
2012 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2016 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
2017 } else if (isLUKS(cd->type)) {
2018 /* If key is not provided, try to use internal key */
2020 if (!cd->volume_key) {
2021 log_err(cd, _("Volume key does not match the volume.\n"));
2024 volume_key_size = cd->volume_key->keylength;
2025 volume_key = cd->volume_key->key;
2028 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2031 r = LUKS_verify_volume_key(&cd->hdr, vk);
2034 log_err(cd, _("Volume key does not match the volume.\n"));
2037 r = LUKS1_activate(cd, name, vk, flags);
2038 } else if (isVERITY(cd->type)) {
2039 /* volume_key == root hash */
2040 if (!volume_key || !volume_key_size) {
2041 log_err(cd, _("Incorrect root hash specified for verity device.\n"));
2045 r = VERITY_activate(cd, name, volume_key, volume_key_size,
2046 &cd->verity_hdr, CRYPT_ACTIVATE_READONLY);
2049 free(cd->verity_root_hash);
2050 cd->verity_root_hash = NULL;
2052 cd->verity_root_hash_size = volume_key_size;
2053 if (!cd->verity_root_hash)
2054 cd->verity_root_hash = malloc(volume_key_size);
2055 if (cd->verity_root_hash)
2056 memcpy(cd->verity_root_hash, volume_key, volume_key_size);
2058 } else if (isTCRYPT(cd->type)) {
2061 r = TCRYPT_activate(cd, name, &cd->tcrypt_hdr,
2062 &cd->tcrypt_params, flags);
2064 log_err(cd, _("Device type is not properly initialised.\n"));
2066 crypt_free_volume_key(vk);
2071 int crypt_deactivate(struct crypt_device *cd, const char *name)
2078 log_dbg("Deactivating volume %s.", name);
2083 switch (crypt_status(cd, name)) {
2086 if (isTCRYPT(cd->type))
2087 r = TCRYPT_deactivate(cd, name);
2089 r = dm_remove_device(cd, name, 0, 0);
2091 case CRYPT_INACTIVE:
2092 log_err(cd, _("Device %s is not active.\n"), name);
2096 log_err(cd, _("Invalid device %s.\n"), name);
2106 int crypt_volume_key_get(struct crypt_device *cd,
2109 size_t *volume_key_size,
2110 const char *passphrase,
2111 size_t passphrase_size)
2113 struct volume_key *vk = NULL;
2117 if (crypt_fips_mode()) {
2118 log_err(cd, "Function not available in FIPS mode.\n");
2122 key_len = crypt_get_volume_key_size(cd);
2123 if (key_len > *volume_key_size) {
2124 log_err(cd, _("Volume key buffer too small.\n"));
2128 if (isPLAIN(cd->type) && cd->plain_hdr.hash) {
2129 r = process_key(cd, cd->plain_hdr.hash, key_len,
2130 passphrase, passphrase_size, &vk);
2132 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
2133 } else if (isLUKS(cd->type)) {
2134 r = LUKS_open_key_with_hdr(keyslot, passphrase,
2135 passphrase_size, &cd->hdr, &vk, cd);
2138 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
2141 memcpy(volume_key, vk->key, vk->keylength);
2142 *volume_key_size = vk->keylength;
2145 crypt_free_volume_key(vk);
2149 int crypt_volume_key_verify(struct crypt_device *cd,
2150 const char *volume_key,
2151 size_t volume_key_size)
2153 struct volume_key *vk;
2156 if (!isLUKS(cd->type)) {
2157 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2161 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2165 r = LUKS_verify_volume_key(&cd->hdr, vk);
2168 log_err(cd, _("Volume key does not match the volume.\n"));
2170 crypt_free_volume_key(vk);
2175 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
2177 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
2178 cd->timeout = timeout_sec;
2181 void crypt_set_password_retry(struct crypt_device *cd, int tries)
2183 log_dbg("Password retry count set to %d.", tries);
2187 void crypt_set_iteration_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2189 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
2190 cd->iteration_time = iteration_time_ms;
2192 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2194 crypt_set_iteration_time(cd, iteration_time_ms);
2197 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
2199 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
2200 cd->password_verify = password_verify ? 1 : 0;
2203 void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
2206 case CRYPT_RNG_URANDOM:
2207 case CRYPT_RNG_RANDOM:
2208 log_dbg("RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
2209 cd->rng_type = rng_type;
2213 int crypt_get_rng_type(struct crypt_device *cd)
2218 return cd->rng_type;
2221 int crypt_memory_lock(struct crypt_device *cd, int lock)
2223 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
2227 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
2234 r = dm_status_device(cd, name);
2239 if (r < 0 && r != -ENODEV)
2240 return CRYPT_INVALID;
2243 return CRYPT_ACTIVE;
2248 return CRYPT_INACTIVE;
2251 static void hexprint(struct crypt_device *cd, const char *d, int n, const char *sep)
2254 for(i = 0; i < n; i++)
2255 log_std(cd, "%02hhx%s", (const char)d[i], sep);
2258 static int _luks_dump(struct crypt_device *cd)
2262 log_std(cd, "LUKS header information for %s\n\n", mdata_device_path(cd));
2263 log_std(cd, "Version: \t%d\n", cd->hdr.version);
2264 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
2265 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
2266 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec);
2267 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
2268 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
2269 log_std(cd, "MK digest: \t");
2270 hexprint(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE, " ");
2272 log_std(cd, "MK salt: \t");
2273 hexprint(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2, " ");
2274 log_std(cd, "\n \t");
2275 hexprint(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ");
2277 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
2278 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
2279 for(i = 0; i < LUKS_NUMKEYS; i++) {
2280 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
2281 log_std(cd, "Key Slot %d: ENABLED\n",i);
2282 log_std(cd, "\tIterations: \t%d\n",
2283 cd->hdr.keyblock[i].passwordIterations);
2284 log_std(cd, "\tSalt: \t");
2285 hexprint(cd, cd->hdr.keyblock[i].passwordSalt,
2286 LUKS_SALTSIZE/2, " ");
2287 log_std(cd, "\n\t \t");
2288 hexprint(cd, cd->hdr.keyblock[i].passwordSalt +
2289 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ");
2292 log_std(cd, "\tKey material offset:\t%d\n",
2293 cd->hdr.keyblock[i].keyMaterialOffset);
2294 log_std(cd, "\tAF stripes: \t%d\n",
2295 cd->hdr.keyblock[i].stripes);
2298 log_std(cd, "Key Slot %d: DISABLED\n", i);
2303 static int _verity_dump(struct crypt_device *cd)
2305 log_std(cd, "VERITY header information for %s\n", mdata_device_path(cd));
2306 log_std(cd, "UUID: \t%s\n", cd->verity_uuid ?: "");
2307 log_std(cd, "Hash type: \t%u\n", cd->verity_hdr.hash_type);
2308 log_std(cd, "Data blocks: \t%" PRIu64 "\n", cd->verity_hdr.data_size);
2309 log_std(cd, "Data block size: \t%u\n", cd->verity_hdr.data_block_size);
2310 log_std(cd, "Hash block size: \t%u\n", cd->verity_hdr.hash_block_size);
2311 log_std(cd, "Hash algorithm: \t%s\n", cd->verity_hdr.hash_name);
2312 log_std(cd, "Salt: \t");
2313 if (cd->verity_hdr.salt_size)
2314 hexprint(cd, cd->verity_hdr.salt, cd->verity_hdr.salt_size, "");
2318 if (cd->verity_root_hash) {
2319 log_std(cd, "Root hash: \t");
2320 hexprint(cd, cd->verity_root_hash, cd->verity_root_hash_size, "");
2326 int crypt_dump(struct crypt_device *cd)
2328 if (isLUKS(cd->type))
2329 return _luks_dump(cd);
2330 else if (isVERITY(cd->type))
2331 return _verity_dump(cd);
2333 log_err(cd, _("Dump operation is not supported for this device type.\n"));
2337 const char *crypt_get_cipher(struct crypt_device *cd)
2339 if (isPLAIN(cd->type))
2340 return cd->plain_cipher;
2342 if (isLUKS(cd->type))
2343 return cd->hdr.cipherName;
2345 if (isLOOPAES(cd->type))
2346 return cd->loopaes_cipher;
2348 if (isTCRYPT(cd->type))
2349 return cd->tcrypt_params.cipher;
2354 const char *crypt_get_cipher_mode(struct crypt_device *cd)
2356 if (isPLAIN(cd->type))
2357 return cd->plain_cipher_mode;
2359 if (isLUKS(cd->type))
2360 return cd->hdr.cipherMode;
2362 if (isLOOPAES(cd->type))
2363 return cd->loopaes_cipher_mode;
2365 if (isTCRYPT(cd->type))
2366 return cd->tcrypt_params.mode;
2371 const char *crypt_get_uuid(struct crypt_device *cd)
2373 if (isLUKS(cd->type))
2374 return cd->hdr.uuid;
2376 if (isPLAIN(cd->type))
2377 return cd->plain_uuid;
2379 if (isLOOPAES(cd->type))
2380 return cd->loopaes_uuid;
2382 if (isVERITY(cd->type))
2383 return cd->verity_uuid;
2388 const char *crypt_get_device_name(struct crypt_device *cd)
2390 const char *path = device_block_path(cd->device);
2393 path = device_path(cd->device);
2398 int crypt_get_volume_key_size(struct crypt_device *cd)
2400 if (isPLAIN(cd->type))
2401 return cd->plain_key_size;
2403 if (isLUKS(cd->type))
2404 return cd->hdr.keyBytes;
2406 if (isLOOPAES(cd->type))
2407 return cd->loopaes_key_size;
2409 if (isVERITY(cd->type))
2410 return cd->verity_root_hash_size;
2412 if (isTCRYPT(cd->type))
2413 return cd->tcrypt_params.key_size;
2418 uint64_t crypt_get_data_offset(struct crypt_device *cd)
2420 if (isPLAIN(cd->type))
2421 return cd->plain_hdr.offset;
2423 if (isLUKS(cd->type))
2424 return cd->hdr.payloadOffset;
2426 if (isLOOPAES(cd->type))
2427 return cd->loopaes_hdr.offset;
2429 if (isTCRYPT(cd->type)) { // FIXME: system vol.
2430 if (!cd->tcrypt_hdr.d.mk_offset)
2432 return (cd->tcrypt_hdr.d.mk_offset / cd->tcrypt_hdr.d.sector_size);
2438 uint64_t crypt_get_iv_offset(struct crypt_device *cd)
2440 if (isPLAIN(cd->type))
2441 return cd->plain_hdr.skip;
2443 if (isLUKS(cd->type))
2446 if (isLOOPAES(cd->type))
2447 return cd->loopaes_hdr.skip;
2449 if (isTCRYPT(cd->type)) {
2450 if (!cd->tcrypt_hdr.d.mk_offset)
2452 return (cd->tcrypt_hdr.d.mk_offset / cd->tcrypt_hdr.d.sector_size);
2458 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
2460 if (!isLUKS(cd->type)) {
2461 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2462 return CRYPT_SLOT_INVALID;
2465 return LUKS_keyslot_info(&cd->hdr, keyslot);
2468 int crypt_keyslot_max(const char *type)
2470 if (type && isLUKS(type))
2471 return LUKS_NUMKEYS;
2476 int crypt_keyslot_area(struct crypt_device *cd,
2481 if (!isLUKS(cd->type))
2484 return LUKS_keyslot_area(&cd->hdr, keyslot, offset, length);
2487 const char *crypt_get_type(struct crypt_device *cd)
2492 int crypt_get_verity_info(struct crypt_device *cd,
2493 struct crypt_params_verity *vp)
2495 if (!isVERITY(cd->type) || !vp)
2498 vp->data_device = device_path(cd->device);
2499 vp->hash_device = mdata_device_path(cd);
2500 vp->hash_name = cd->verity_hdr.hash_name;
2501 vp->salt = cd->verity_hdr.salt;
2502 vp->salt_size = cd->verity_hdr.salt_size;
2503 vp->data_block_size = cd->verity_hdr.data_block_size;
2504 vp->hash_block_size = cd->verity_hdr.hash_block_size;
2505 vp->data_size = cd->verity_hdr.data_size;
2506 vp->hash_area_offset = cd->verity_hdr.hash_area_offset;
2507 vp->hash_type = cd->verity_hdr.hash_type;
2508 vp->flags = cd->verity_hdr.flags & CRYPT_VERITY_NO_HEADER;
2512 int crypt_get_active_device(struct crypt_device *cd __attribute__((unused)),
2514 struct crypt_active_device *cad)
2516 struct crypt_dm_active_device dmd;
2519 r = dm_query_device(cd, name, 0, &dmd);
2523 if (dmd.target != DM_CRYPT && dmd.target != DM_VERITY)
2526 cad->offset = dmd.u.crypt.offset;
2527 cad->iv_offset = dmd.u.crypt.iv_offset;
2528 cad->size = dmd.size;
2529 cad->flags = dmd.flags;