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);
755 crypt_free_volume_key(dmd.u.crypt.vk);
756 device_free(dmd.data_device);
757 free(CONST_CAST(void*)dmd.u.crypt.cipher);
758 free(CONST_CAST(void*)dmd.uuid);
762 static int _init_by_name_verity(struct crypt_device *cd, const char *name)
764 struct crypt_params_verity params = {};
765 struct crypt_dm_active_device dmd = {
767 .u.verity.vp = ¶ms,
771 r = dm_query_device(cd, name,
774 DM_ACTIVE_VERITY_HASH_DEVICE |
775 DM_ACTIVE_VERITY_PARAMS, &dmd);
779 if (isVERITY(cd->type)) {
780 cd->verity_uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
781 cd->verity_hdr.flags = CRYPT_VERITY_NO_HEADER; //FIXME
782 cd->verity_hdr.data_size = params.data_size;
783 cd->verity_root_hash_size = dmd.u.verity.root_hash_size;
784 cd->verity_root_hash = NULL;
785 cd->verity_hdr.hash_name = params.hash_name;
786 cd->verity_hdr.data_device = NULL;
787 cd->verity_hdr.hash_device = NULL;
788 cd->verity_hdr.data_block_size = params.data_block_size;
789 cd->verity_hdr.hash_block_size = params.hash_block_size;
790 cd->verity_hdr.hash_area_offset = dmd.u.verity.hash_offset;
791 cd->verity_hdr.hash_type = params.hash_type;
792 cd->verity_hdr.flags = params.flags;
793 cd->verity_hdr.salt_size = params.salt_size;
794 cd->verity_hdr.salt = params.salt;
795 cd->metadata_device = dmd.u.verity.hash_device;
798 device_free(dmd.data_device);
799 free(CONST_CAST(void*)dmd.uuid);
803 int crypt_init_by_name_and_header(struct crypt_device **cd,
805 const char *header_device)
807 crypt_status_info ci;
808 struct crypt_dm_active_device dmd;
811 log_dbg("Allocating crypt device context by device %s.", name);
813 ci = crypt_status(NULL, name);
814 if (ci == CRYPT_INVALID)
817 if (ci < CRYPT_ACTIVE) {
818 log_err(NULL, _("Device %s is not active.\n"), name);
822 r = dm_query_device(NULL, name, DM_ACTIVE_DEVICE | DM_ACTIVE_UUID, &dmd);
829 r = crypt_init(cd, header_device);
831 r = crypt_init(cd, device_path(dmd.data_device));
833 /* Underlying device disappeared but mapping still active */
834 if (!dmd.data_device || r == -ENOTBLK)
835 log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
838 /* Underlying device is not readable but crypt mapping exists */
840 device_free(dmd.data_device);
841 dmd.data_device = NULL;
842 r = crypt_init(cd, NULL);
850 if (!strncmp(CRYPT_PLAIN, dmd.uuid, sizeof(CRYPT_PLAIN)-1))
851 (*cd)->type = strdup(CRYPT_PLAIN);
852 else if (!strncmp(CRYPT_LOOPAES, dmd.uuid, sizeof(CRYPT_LOOPAES)-1))
853 (*cd)->type = strdup(CRYPT_LOOPAES);
854 else if (!strncmp(CRYPT_LUKS1, dmd.uuid, sizeof(CRYPT_LUKS1)-1))
855 (*cd)->type = strdup(CRYPT_LUKS1);
856 else if (!strncmp(CRYPT_VERITY, dmd.uuid, sizeof(CRYPT_VERITY)-1))
857 (*cd)->type = strdup(CRYPT_VERITY);
859 log_dbg("Unknown UUID set, some parameters are not set.");
861 log_dbg("Active device has no UUID set, some parameters are not set.");
864 r = crypt_set_data_device(*cd, device_path(dmd.data_device));
869 /* Try to initialise basic parameters from active device */
871 if (dmd.target == DM_CRYPT)
872 r = _init_by_name_crypt(*cd, name);
873 else if (dmd.target == DM_VERITY)
874 r = _init_by_name_verity(*cd, name);
880 device_free(dmd.data_device);
881 free(CONST_CAST(void*)dmd.uuid);
885 int crypt_init_by_name(struct crypt_device **cd, const char *name)
887 return crypt_init_by_name_and_header(cd, name, NULL);
890 static int _crypt_format_plain(struct crypt_device *cd,
892 const char *cipher_mode,
894 size_t volume_key_size,
895 struct crypt_params_plain *params)
897 if (!cipher || !cipher_mode) {
898 log_err(cd, _("Invalid plain crypt parameters.\n"));
902 if (volume_key_size > 1024) {
903 log_err(cd, _("Invalid key size.\n"));
907 if (!(cd->type = strdup(CRYPT_PLAIN)))
910 cd->plain_key_size = volume_key_size;
911 cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
915 cd->plain_cipher = strdup(cipher);
916 cd->plain_cipher_mode = strdup(cipher_mode);
919 cd->plain_uuid = strdup(uuid);
921 if (params && params->hash)
922 cd->plain_hdr.hash = strdup(params->hash);
924 cd->plain_hdr.offset = params ? params->offset : 0;
925 cd->plain_hdr.skip = params ? params->skip : 0;
926 cd->plain_hdr.size = params ? params->size : 0;
928 if (!cd->plain_cipher || !cd->plain_cipher_mode)
934 static int _crypt_format_luks1(struct crypt_device *cd,
936 const char *cipher_mode,
938 const char *volume_key,
939 size_t volume_key_size,
940 struct crypt_params_luks1 *params)
943 unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
944 unsigned long alignment_offset = 0;
946 if (!crypt_metadata_device(cd)) {
947 log_err(cd, _("Can't format LUKS without device.\n"));
951 if (!(cd->type = strdup(CRYPT_LUKS1)))
955 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
958 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
963 if (params && params->data_device) {
964 cd->metadata_device = cd->device;
966 if (device_alloc(&cd->device, params->data_device) < 0)
968 required_alignment = params->data_alignment * SECTOR_SIZE;
969 } else if (params && params->data_alignment) {
970 required_alignment = params->data_alignment * SECTOR_SIZE;
972 device_topology_alignment(cd->device,
974 &alignment_offset, DEFAULT_DISK_ALIGNMENT);
976 /* Check early if we cannot allocate block device for key slot access */
977 r = device_block_adjust(cd, cd->device, DEV_OK, 0, NULL, NULL);
981 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
982 (params && params->hash) ? params->hash : "sha1",
984 required_alignment / SECTOR_SIZE,
985 alignment_offset / SECTOR_SIZE,
986 cd->iteration_time, &cd->PBKDF2_per_sec,
987 cd->metadata_device ? 1 : 0, cd);
991 /* Wipe first 8 sectors - fs magic numbers etc. */
992 r = crypt_wipe(crypt_metadata_device(cd), 0, 8 * SECTOR_SIZE, CRYPT_WIPE_ZERO, 1);
995 log_err(cd, _("Cannot format device %s which is still in use.\n"),
996 mdata_device_path(cd));
997 else if (r == -EACCES) {
998 log_err(cd, _("Cannot format device %s, permission denied.\n"),
999 mdata_device_path(cd));
1002 log_err(cd, _("Cannot wipe header on device %s.\n"),
1003 mdata_device_path(cd));
1008 r = LUKS_write_phdr(&cd->hdr, cd);
1013 static int _crypt_format_loopaes(struct crypt_device *cd,
1016 size_t volume_key_size,
1017 struct crypt_params_loopaes *params)
1019 if (!crypt_metadata_device(cd)) {
1020 log_err(cd, _("Can't format LOOPAES without device.\n"));
1024 if (volume_key_size > 1024) {
1025 log_err(cd, _("Invalid key size.\n"));
1029 if (!(cd->type = strdup(CRYPT_LOOPAES)))
1032 cd->loopaes_key_size = volume_key_size;
1034 cd->loopaes_cipher = strdup(cipher ?: DEFAULT_LOOPAES_CIPHER);
1037 cd->loopaes_uuid = strdup(uuid);
1039 if (params && params->hash)
1040 cd->loopaes_hdr.hash = strdup(params->hash);
1042 cd->loopaes_hdr.offset = params ? params->offset : 0;
1043 cd->loopaes_hdr.skip = params ? params->skip : 0;
1048 static int _crypt_format_verity(struct crypt_device *cd,
1050 struct crypt_params_verity *params)
1052 int r = 0, hash_size;
1053 uint64_t data_device_size;
1055 if (!crypt_metadata_device(cd)) {
1056 log_err(cd, _("Can't format VERITY without device.\n"));
1060 if (!params || !params->data_device)
1063 if (params->hash_type > VERITY_MAX_HASH_TYPE) {
1064 log_err(cd, _("Unsupported VERITY hash type %d.\n"), params->hash_type);
1068 if (VERITY_BLOCK_SIZE_OK(params->data_block_size) ||
1069 VERITY_BLOCK_SIZE_OK(params->hash_block_size)) {
1070 log_err(cd, _("Unsupported VERITY block size.\n"));
1074 if (params->hash_area_offset % 512) {
1075 log_err(cd, _("Unsupported VERITY hash offset.\n"));
1079 if (!(cd->type = strdup(CRYPT_VERITY)))
1082 r = crypt_set_data_device(cd, params->data_device);
1085 if (!params->data_size) {
1086 r = device_size(cd->device, &data_device_size);
1090 cd->verity_hdr.data_size = data_device_size / params->data_block_size;
1092 cd->verity_hdr.data_size = params->data_size;
1094 hash_size = crypt_hash_size(params->hash_name);
1095 if (hash_size <= 0) {
1096 log_err(cd, _("Hash algorithm %s not supported.\n"),
1100 cd->verity_root_hash_size = hash_size;
1102 cd->verity_root_hash = malloc(cd->verity_root_hash_size);
1103 if (!cd->verity_root_hash)
1106 cd->verity_hdr.flags = params->flags;
1107 if (!(cd->verity_hdr.hash_name = strdup(params->hash_name)))
1109 cd->verity_hdr.data_device = NULL;
1110 cd->verity_hdr.data_block_size = params->data_block_size;
1111 cd->verity_hdr.hash_block_size = params->hash_block_size;
1112 cd->verity_hdr.hash_area_offset = params->hash_area_offset;
1113 cd->verity_hdr.hash_type = params->hash_type;
1114 cd->verity_hdr.flags = params->flags;
1115 cd->verity_hdr.salt_size = params->salt_size;
1116 if (!(cd->verity_hdr.salt = malloc(params->salt_size)))
1120 memcpy(CONST_CAST(char*)cd->verity_hdr.salt, params->salt,
1123 r = crypt_random_get(cd, CONST_CAST(char*)cd->verity_hdr.salt,
1124 params->salt_size, CRYPT_RND_SALT);
1128 if (params->flags & CRYPT_VERITY_CREATE_HASH) {
1129 r = VERITY_create(cd, &cd->verity_hdr,
1130 cd->verity_root_hash, cd->verity_root_hash_size);
1135 if (!(params->flags & CRYPT_VERITY_NO_HEADER)) {
1137 cd->verity_uuid = strdup(uuid);
1139 r = VERITY_UUID_generate(cd, &cd->verity_uuid);
1144 r = VERITY_write_sb(cd, cd->verity_hdr.hash_area_offset,
1151 int crypt_format(struct crypt_device *cd,
1154 const char *cipher_mode,
1156 const char *volume_key,
1157 size_t volume_key_size,
1166 log_dbg("Context already formatted as %s.", cd->type);
1170 log_dbg("Formatting device %s as type %s.", mdata_device_path(cd) ?: "(none)", type);
1172 r = init_crypto(cd);
1177 r = _crypt_format_plain(cd, cipher, cipher_mode,
1178 uuid, volume_key_size, params);
1179 else if (isLUKS(type))
1180 r = _crypt_format_luks1(cd, cipher, cipher_mode,
1181 uuid, volume_key, volume_key_size, params);
1182 else if (isLOOPAES(type))
1183 r = _crypt_format_loopaes(cd, cipher, uuid, volume_key_size, params);
1184 else if (isVERITY(type))
1185 r = _crypt_format_verity(cd, uuid, params);
1187 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
1194 crypt_free_volume_key(cd->volume_key);
1195 cd->volume_key = NULL;
1201 int crypt_load(struct crypt_device *cd,
1202 const char *requested_type,
1207 log_dbg("Trying to load %s crypt type from device %s.",
1208 requested_type ?: "any", mdata_device_path(cd) ?: "(none)");
1210 if (!crypt_metadata_device(cd))
1213 if (!requested_type || isLUKS(requested_type)) {
1214 if (cd->type && !isLUKS(cd->type)) {
1215 log_dbg("Context is already initialised to type %s", cd->type);
1219 r = _crypt_load_luks1(cd, 1, 0);
1220 } else if (isVERITY(requested_type)) {
1221 if (cd->type && !isVERITY(cd->type)) {
1222 log_dbg("Context is already initialised to type %s", cd->type);
1225 r = _crypt_load_verity(cd, params);
1226 } else if (isTCRYPT(requested_type)) {
1227 if (cd->type && !isTCRYPT(cd->type)) {
1228 log_dbg("Context is already initialised to type %s", cd->type);
1231 r = _crypt_load_tcrypt(cd, params);
1238 int crypt_repair(struct crypt_device *cd,
1239 const char *requested_type,
1240 void *params __attribute__((unused)))
1244 log_dbg("Trying to repair %s crypt type from device %s.",
1245 requested_type ?: "any", mdata_device_path(cd) ?: "(none)");
1247 if (!crypt_metadata_device(cd))
1250 if (requested_type && !isLUKS(requested_type))
1254 /* Load with repair */
1255 r = _crypt_load_luks1(cd, 1, 1);
1259 /* cd->type and header must be set in context */
1260 r = crypt_check_data_device_size(cd);
1269 int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
1271 struct crypt_dm_active_device dmd;
1274 /* Device context type must be initialised */
1275 if (!cd->type || !crypt_get_uuid(cd))
1278 log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
1280 r = dm_query_device(cd, name, DM_ACTIVE_DEVICE | DM_ACTIVE_CRYPT_CIPHER |
1281 DM_ACTIVE_UUID | DM_ACTIVE_CRYPT_KEYSIZE |
1282 DM_ACTIVE_CRYPT_KEY, &dmd);
1284 log_err(NULL, _("Device %s is not active.\n"), name);
1288 if (!dmd.uuid || dmd.target != DM_CRYPT) {
1293 r = device_block_adjust(cd, dmd.data_device, DEV_OK,
1294 dmd.u.crypt.offset, &new_size, &dmd.flags);
1298 if (new_size == dmd.size) {
1299 log_dbg("Device has already requested size %" PRIu64
1300 " sectors.", dmd.size);
1303 dmd.size = new_size;
1304 r = dm_create_device(cd, name, cd->type, &dmd, 1);
1307 if (dmd.target == DM_CRYPT) {
1308 crypt_free_volume_key(dmd.u.crypt.vk);
1309 free(CONST_CAST(void*)dmd.u.crypt.cipher);
1311 free(CONST_CAST(void*)dmd.data_device);
1312 free(CONST_CAST(void*)dmd.uuid);
1317 int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
1319 if (!isLUKS(cd->type)) {
1320 log_err(cd, _("This operation is not supported for this device type.\n"));
1324 if (uuid && !strncmp(uuid, cd->hdr.uuid, sizeof(cd->hdr.uuid))) {
1325 log_dbg("UUID is the same as requested (%s) for device %s.",
1326 uuid, mdata_device_path(cd));
1331 log_dbg("Requested new UUID change to %s for %s.", uuid, mdata_device_path(cd));
1333 log_dbg("Requested new UUID refresh for %s.", mdata_device_path(cd));
1335 if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
1338 return LUKS_hdr_uuid_set(&cd->hdr, uuid, cd);
1341 int crypt_header_backup(struct crypt_device *cd,
1342 const char *requested_type,
1343 const char *backup_file)
1347 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1350 r = init_crypto(cd);
1354 log_dbg("Requested header backup of device %s (%s) to "
1355 "file %s.", mdata_device_path(cd), requested_type, backup_file);
1357 return LUKS_hdr_backup(backup_file, &cd->hdr, cd);
1360 int crypt_header_restore(struct crypt_device *cd,
1361 const char *requested_type,
1362 const char *backup_file)
1366 if (requested_type && !isLUKS(requested_type))
1369 r = init_crypto(cd);
1373 log_dbg("Requested header restore to device %s (%s) from "
1374 "file %s.", mdata_device_path(cd), requested_type, backup_file);
1376 return LUKS_hdr_restore(backup_file, &cd->hdr, cd);
1379 void crypt_free(struct crypt_device *cd)
1382 log_dbg("Releasing crypt device %s context.", mdata_device_path(cd));
1385 crypt_free_volume_key(cd->volume_key);
1387 device_free(cd->device);
1388 device_free(cd->metadata_device);
1391 /* used in plain device only */
1392 free(CONST_CAST(void*)cd->plain_hdr.hash);
1393 free(cd->plain_cipher);
1394 free(cd->plain_cipher_mode);
1395 free(cd->plain_uuid);
1397 /* used in loop-AES device only */
1398 free(CONST_CAST(void*)cd->loopaes_hdr.hash);
1399 free(cd->loopaes_cipher);
1400 free(cd->loopaes_uuid);
1402 /* used in verity device only */
1403 free(CONST_CAST(void*)cd->verity_hdr.hash_name);
1404 free(CONST_CAST(void*)cd->verity_hdr.salt);
1405 free(cd->verity_root_hash);
1406 free(cd->verity_uuid);
1412 int crypt_suspend(struct crypt_device *cd,
1415 crypt_status_info ci;
1418 log_dbg("Suspending volume %s.", name);
1420 if (!cd || !isLUKS(cd->type)) {
1421 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1426 ci = crypt_status(NULL, name);
1427 if (ci < CRYPT_ACTIVE) {
1428 log_err(cd, _("Volume %s is not active.\n"), name);
1434 r = dm_status_suspended(cd, name);
1439 log_err(cd, _("Volume %s is already suspended.\n"), name);
1444 r = dm_suspend_and_wipe_key(cd, name);
1446 log_err(cd, "Suspend is not supported for device %s.\n", name);
1448 log_err(cd, "Error during suspending device %s.\n", name);
1454 int crypt_resume_by_passphrase(struct crypt_device *cd,
1457 const char *passphrase,
1458 size_t passphrase_size)
1460 struct volume_key *vk = NULL;
1463 log_dbg("Resuming volume %s.", name);
1465 if (!isLUKS(cd->type)) {
1466 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1471 r = dm_status_suspended(cd, name);
1476 log_err(cd, _("Volume %s is not suspended.\n"), name);
1481 r = LUKS_open_key_with_hdr(keyslot, passphrase, passphrase_size,
1484 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1488 r = dm_resume_and_reinstate_key(cd, name, vk->keylength, vk->key);
1490 log_err(cd, "Resume is not supported for device %s.\n", name);
1492 log_err(cd, "Error during resuming device %s.\n", name);
1496 crypt_free_volume_key(vk);
1497 return r < 0 ? r : keyslot;
1500 int crypt_resume_by_keyfile_offset(struct crypt_device *cd,
1503 const char *keyfile,
1504 size_t keyfile_size,
1505 size_t keyfile_offset)
1507 struct volume_key *vk = NULL;
1508 char *passphrase_read = NULL;
1509 size_t passphrase_size_read;
1512 log_dbg("Resuming volume %s.", name);
1514 if (!isLUKS(cd->type)) {
1515 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1520 r = dm_status_suspended(cd, name);
1525 log_err(cd, _("Volume %s is not suspended.\n"), name);
1532 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1533 &passphrase_size_read, keyfile, keyfile_offset,
1538 r = LUKS_open_key_with_hdr(keyslot, passphrase_read,
1539 passphrase_size_read, &cd->hdr, &vk, cd);
1544 r = dm_resume_and_reinstate_key(cd, name, vk->keylength, vk->key);
1546 log_err(cd, "Error during resuming device %s.\n", name);
1548 crypt_safe_free(passphrase_read);
1549 crypt_free_volume_key(vk);
1550 return r < 0 ? r : keyslot;
1553 int crypt_resume_by_keyfile(struct crypt_device *cd,
1556 const char *keyfile,
1557 size_t keyfile_size)
1559 return crypt_resume_by_keyfile_offset(cd, name, keyslot,
1560 keyfile, keyfile_size, 0);
1563 // slot manipulation
1564 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1565 int keyslot, // -1 any
1566 const char *passphrase, // NULL -> terminal
1567 size_t passphrase_size,
1568 const char *new_passphrase, // NULL -> terminal
1569 size_t new_passphrase_size)
1571 struct volume_key *vk = NULL;
1572 char *password = NULL, *new_password = NULL;
1573 size_t passwordLen, new_passwordLen;
1576 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1577 "new passphrase %sprovided.",
1578 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1580 if (!isLUKS(cd->type)) {
1581 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1585 r = keyslot_verify_or_find_empty(cd, &keyslot);
1589 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1590 /* No slots used, try to use pre-generated key in header */
1591 if (cd->volume_key) {
1592 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1593 r = vk ? 0 : -ENOMEM;
1595 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1598 } else if (passphrase) {
1599 /* Passphrase provided, use it to unlock existing keyslot */
1600 r = LUKS_open_key_with_hdr(CRYPT_ANY_SLOT, passphrase,
1601 passphrase_size, &cd->hdr, &vk, cd);
1603 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1604 r = key_from_terminal(cd, _("Enter any passphrase: "),
1605 &password, &passwordLen, 0);
1609 r = LUKS_open_key_with_hdr(CRYPT_ANY_SLOT, password,
1610 passwordLen, &cd->hdr, &vk, cd);
1611 crypt_safe_free(password);
1617 if (new_passphrase) {
1618 new_password = CONST_CAST(char*)new_passphrase;
1619 new_passwordLen = new_passphrase_size;
1621 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1622 &new_password, &new_passwordLen, 1);
1627 r = LUKS_set_key(keyslot, new_password, new_passwordLen,
1628 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1633 if (!new_passphrase)
1634 crypt_safe_free(new_password);
1635 crypt_free_volume_key(vk);
1636 return r ?: keyslot;
1639 int crypt_keyslot_add_by_keyfile_offset(struct crypt_device *cd,
1641 const char *keyfile,
1642 size_t keyfile_size,
1643 size_t keyfile_offset,
1644 const char *new_keyfile,
1645 size_t new_keyfile_size,
1646 size_t new_keyfile_offset)
1648 struct volume_key *vk = NULL;
1649 char *password = NULL; size_t passwordLen;
1650 char *new_password = NULL; size_t new_passwordLen;
1653 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1654 keyfile ?: "[none]", new_keyfile ?: "[none]");
1656 if (!isLUKS(cd->type)) {
1657 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1661 r = keyslot_verify_or_find_empty(cd, &keyslot);
1665 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1666 /* No slots used, try to use pre-generated key in header */
1667 if (cd->volume_key) {
1668 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1669 r = vk ? 0 : -ENOMEM;
1671 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1675 /* Read password from file of (if NULL) from terminal */
1677 r = key_from_file(cd, _("Enter any passphrase: "),
1678 &password, &passwordLen,
1679 keyfile, keyfile_offset, keyfile_size);
1681 r = key_from_terminal(cd, _("Enter any passphrase: "),
1682 &password, &passwordLen, 0);
1686 r = LUKS_open_key_with_hdr(CRYPT_ANY_SLOT, password, passwordLen,
1694 r = key_from_file(cd, _("Enter new passphrase for key slot: "),
1695 &new_password, &new_passwordLen, new_keyfile,
1696 new_keyfile_offset, new_keyfile_size);
1698 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1699 &new_password, &new_passwordLen, 1);
1703 r = LUKS_set_key(keyslot, new_password, new_passwordLen,
1704 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1706 crypt_safe_free(password);
1707 crypt_safe_free(new_password);
1708 crypt_free_volume_key(vk);
1709 return r < 0 ? r : keyslot;
1712 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1714 const char *keyfile,
1715 size_t keyfile_size,
1716 const char *new_keyfile,
1717 size_t new_keyfile_size)
1719 return crypt_keyslot_add_by_keyfile_offset(cd, keyslot,
1720 keyfile, keyfile_size, 0,
1721 new_keyfile, new_keyfile_size, 0);
1724 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1726 const char *volume_key,
1727 size_t volume_key_size,
1728 const char *passphrase,
1729 size_t passphrase_size)
1731 struct volume_key *vk = NULL;
1733 char *new_password = NULL; size_t new_passwordLen;
1735 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1737 if (!isLUKS(cd->type)) {
1738 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1743 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1744 else if (cd->volume_key)
1745 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1750 r = LUKS_verify_volume_key(&cd->hdr, vk);
1752 log_err(cd, _("Volume key does not match the volume.\n"));
1756 r = keyslot_verify_or_find_empty(cd, &keyslot);
1761 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1762 &new_password, &new_passwordLen, 1);
1765 passphrase = new_password;
1766 passphrase_size = new_passwordLen;
1769 r = LUKS_set_key(keyslot, passphrase, passphrase_size,
1770 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1772 crypt_safe_free(new_password);
1773 crypt_free_volume_key(vk);
1774 return (r < 0) ? r : keyslot;
1777 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1779 crypt_keyslot_info ki;
1781 log_dbg("Destroying keyslot %d.", keyslot);
1783 if (!isLUKS(cd->type)) {
1784 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1788 ki = crypt_keyslot_status(cd, keyslot);
1789 if (ki == CRYPT_SLOT_INVALID) {
1790 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1794 if (ki == CRYPT_SLOT_INACTIVE) {
1795 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1799 return LUKS_del_key(keyslot, &cd->hdr, cd);
1802 // activation/deactivation of device mapping
1803 int crypt_activate_by_passphrase(struct crypt_device *cd,
1806 const char *passphrase,
1807 size_t passphrase_size,
1810 crypt_status_info ci;
1811 struct volume_key *vk = NULL;
1812 char *read_passphrase = NULL;
1813 size_t passphraseLen = 0;
1816 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1817 name ? "Activating" : "Checking", name ?: "",
1818 keyslot, passphrase ? "" : "[none] ");
1821 ci = crypt_status(NULL, name);
1822 if (ci == CRYPT_INVALID)
1824 else if (ci >= CRYPT_ACTIVE) {
1825 log_err(cd, _("Device %s already exists.\n"), name);
1830 /* plain, use hashed passphrase */
1831 if (isPLAIN(cd->type)) {
1836 r = key_from_terminal(cd, NULL, &read_passphrase,
1840 passphrase = read_passphrase;
1841 passphrase_size = passphraseLen;
1844 r = process_key(cd, cd->plain_hdr.hash,
1846 passphrase, passphrase_size, &vk);
1850 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1852 } else if (isLUKS(cd->type)) {
1853 /* provided passphrase, do not retry */
1855 r = LUKS_open_key_with_hdr(keyslot, passphrase,
1856 passphrase_size, &cd->hdr, &vk, cd);
1858 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1863 r = LUKS1_activate(cd, name, vk, flags);
1868 crypt_safe_free(read_passphrase);
1869 crypt_free_volume_key(vk);
1871 return r < 0 ? r : keyslot;
1874 int crypt_activate_by_keyfile_offset(struct crypt_device *cd,
1877 const char *keyfile,
1878 size_t keyfile_size,
1879 size_t keyfile_offset,
1882 crypt_status_info ci;
1883 struct volume_key *vk = NULL;
1884 char *passphrase_read = NULL;
1885 size_t passphrase_size_read;
1886 unsigned int key_count = 0;
1889 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1890 name ?: "", keyslot, keyfile ?: "[none]");
1893 ci = crypt_status(NULL, name);
1894 if (ci == CRYPT_INVALID)
1896 else if (ci >= CRYPT_ACTIVE) {
1897 log_err(cd, _("Device %s already exists.\n"), name);
1905 if (isPLAIN(cd->type)) {
1909 r = key_from_file(cd, _("Enter passphrase: "),
1910 &passphrase_read, &passphrase_size_read,
1911 keyfile, keyfile_offset, keyfile_size);
1915 r = process_key(cd, cd->plain_hdr.hash,
1917 passphrase_read, passphrase_size_read, &vk);
1921 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1922 } else if (isLUKS(cd->type)) {
1923 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1924 &passphrase_size_read, keyfile, keyfile_offset, keyfile_size);
1927 r = LUKS_open_key_with_hdr(keyslot, passphrase_read,
1928 passphrase_size_read, &cd->hdr, &vk, cd);
1934 r = LUKS1_activate(cd, name, vk, flags);
1939 } else if (isLOOPAES(cd->type)) {
1940 r = key_from_file(cd, NULL, &passphrase_read, &passphrase_size_read,
1941 keyfile, keyfile_offset, keyfile_size);
1944 r = LOOPAES_parse_keyfile(cd, &vk, cd->loopaes_hdr.hash, &key_count,
1945 passphrase_read, passphrase_size_read);
1949 r = LOOPAES_activate(cd, name, cd->loopaes_cipher,
1950 key_count, vk, flags);
1955 crypt_safe_free(passphrase_read);
1956 crypt_free_volume_key(vk);
1961 int crypt_activate_by_keyfile(struct crypt_device *cd,
1964 const char *keyfile,
1965 size_t keyfile_size,
1968 return crypt_activate_by_keyfile_offset(cd, name, keyslot, keyfile,
1969 keyfile_size, 0, flags);
1972 int crypt_activate_by_volume_key(struct crypt_device *cd,
1974 const char *volume_key,
1975 size_t volume_key_size,
1978 crypt_status_info ci;
1979 struct volume_key *vk = NULL;
1982 log_dbg("Activating volume %s by volume key.", name ?: "[none]");
1985 ci = crypt_status(NULL, name);
1986 if (ci == CRYPT_INVALID)
1988 else if (ci >= CRYPT_ACTIVE) {
1989 log_err(cd, _("Device %s already exists.\n"), name);
1994 /* use key directly, no hash */
1995 if (isPLAIN(cd->type)) {
1999 if (!volume_key || !volume_key_size || volume_key_size != cd->plain_key_size) {
2000 log_err(cd, _("Incorrect volume key specified for plain device.\n"));
2004 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2008 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
2009 } else if (isLUKS(cd->type)) {
2010 /* If key is not provided, try to use internal key */
2012 if (!cd->volume_key) {
2013 log_err(cd, _("Volume key does not match the volume.\n"));
2016 volume_key_size = cd->volume_key->keylength;
2017 volume_key = cd->volume_key->key;
2020 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2023 r = LUKS_verify_volume_key(&cd->hdr, vk);
2026 log_err(cd, _("Volume key does not match the volume.\n"));
2029 r = LUKS1_activate(cd, name, vk, flags);
2030 } else if (isVERITY(cd->type)) {
2031 /* volume_key == root hash */
2032 if (!volume_key || !volume_key_size) {
2033 log_err(cd, _("Incorrect root hash specified for verity device.\n"));
2037 r = VERITY_activate(cd, name, volume_key, volume_key_size,
2038 &cd->verity_hdr, CRYPT_ACTIVATE_READONLY);
2041 free(cd->verity_root_hash);
2042 cd->verity_root_hash = NULL;
2044 cd->verity_root_hash_size = volume_key_size;
2045 if (!cd->verity_root_hash)
2046 cd->verity_root_hash = malloc(volume_key_size);
2047 if (cd->verity_root_hash)
2048 memcpy(cd->verity_root_hash, volume_key, volume_key_size);
2050 } else if (isTCRYPT(cd->type)) {
2053 r = TCRYPT_activate(cd, name, &cd->tcrypt_hdr,
2054 &cd->tcrypt_params, flags);
2056 log_err(cd, _("Device type is not properly initialised.\n"));
2058 crypt_free_volume_key(vk);
2063 int crypt_deactivate(struct crypt_device *cd, const char *name)
2070 log_dbg("Deactivating volume %s.", name);
2075 switch (crypt_status(cd, name)) {
2078 r = dm_remove_device(cd, name, 0, 0);
2080 case CRYPT_INACTIVE:
2081 log_err(cd, _("Device %s is not active.\n"), name);
2085 log_err(cd, _("Invalid device %s.\n"), name);
2095 int crypt_volume_key_get(struct crypt_device *cd,
2098 size_t *volume_key_size,
2099 const char *passphrase,
2100 size_t passphrase_size)
2102 struct volume_key *vk = NULL;
2106 if (crypt_fips_mode()) {
2107 log_err(cd, "Function not available in FIPS mode.\n");
2111 key_len = crypt_get_volume_key_size(cd);
2112 if (key_len > *volume_key_size) {
2113 log_err(cd, _("Volume key buffer too small.\n"));
2117 if (isPLAIN(cd->type) && cd->plain_hdr.hash) {
2118 r = process_key(cd, cd->plain_hdr.hash, key_len,
2119 passphrase, passphrase_size, &vk);
2121 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
2122 } else if (isLUKS(cd->type)) {
2123 r = LUKS_open_key_with_hdr(keyslot, passphrase,
2124 passphrase_size, &cd->hdr, &vk, cd);
2127 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
2130 memcpy(volume_key, vk->key, vk->keylength);
2131 *volume_key_size = vk->keylength;
2134 crypt_free_volume_key(vk);
2138 int crypt_volume_key_verify(struct crypt_device *cd,
2139 const char *volume_key,
2140 size_t volume_key_size)
2142 struct volume_key *vk;
2145 if (!isLUKS(cd->type)) {
2146 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2150 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2154 r = LUKS_verify_volume_key(&cd->hdr, vk);
2157 log_err(cd, _("Volume key does not match the volume.\n"));
2159 crypt_free_volume_key(vk);
2164 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
2166 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
2167 cd->timeout = timeout_sec;
2170 void crypt_set_password_retry(struct crypt_device *cd, int tries)
2172 log_dbg("Password retry count set to %d.", tries);
2176 void crypt_set_iteration_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2178 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
2179 cd->iteration_time = iteration_time_ms;
2181 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2183 crypt_set_iteration_time(cd, iteration_time_ms);
2186 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
2188 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
2189 cd->password_verify = password_verify ? 1 : 0;
2192 void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
2195 case CRYPT_RNG_URANDOM:
2196 case CRYPT_RNG_RANDOM:
2197 log_dbg("RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
2198 cd->rng_type = rng_type;
2202 int crypt_get_rng_type(struct crypt_device *cd)
2207 return cd->rng_type;
2210 int crypt_memory_lock(struct crypt_device *cd, int lock)
2212 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
2216 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
2223 r = dm_status_device(cd, name);
2228 if (r < 0 && r != -ENODEV)
2229 return CRYPT_INVALID;
2232 return CRYPT_ACTIVE;
2237 return CRYPT_INACTIVE;
2240 static void hexprint(struct crypt_device *cd, const char *d, int n, const char *sep)
2243 for(i = 0; i < n; i++)
2244 log_std(cd, "%02hhx%s", (const char)d[i], sep);
2247 static int _luks_dump(struct crypt_device *cd)
2251 log_std(cd, "LUKS header information for %s\n\n", mdata_device_path(cd));
2252 log_std(cd, "Version: \t%d\n", cd->hdr.version);
2253 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
2254 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
2255 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec);
2256 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
2257 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
2258 log_std(cd, "MK digest: \t");
2259 hexprint(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE, " ");
2261 log_std(cd, "MK salt: \t");
2262 hexprint(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2, " ");
2263 log_std(cd, "\n \t");
2264 hexprint(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ");
2266 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
2267 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
2268 for(i = 0; i < LUKS_NUMKEYS; i++) {
2269 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
2270 log_std(cd, "Key Slot %d: ENABLED\n",i);
2271 log_std(cd, "\tIterations: \t%d\n",
2272 cd->hdr.keyblock[i].passwordIterations);
2273 log_std(cd, "\tSalt: \t");
2274 hexprint(cd, cd->hdr.keyblock[i].passwordSalt,
2275 LUKS_SALTSIZE/2, " ");
2276 log_std(cd, "\n\t \t");
2277 hexprint(cd, cd->hdr.keyblock[i].passwordSalt +
2278 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ");
2281 log_std(cd, "\tKey material offset:\t%d\n",
2282 cd->hdr.keyblock[i].keyMaterialOffset);
2283 log_std(cd, "\tAF stripes: \t%d\n",
2284 cd->hdr.keyblock[i].stripes);
2287 log_std(cd, "Key Slot %d: DISABLED\n", i);
2292 static int _verity_dump(struct crypt_device *cd)
2294 log_std(cd, "VERITY header information for %s\n", mdata_device_path(cd));
2295 log_std(cd, "UUID: \t%s\n", cd->verity_uuid ?: "");
2296 log_std(cd, "Hash type: \t%u\n", cd->verity_hdr.hash_type);
2297 log_std(cd, "Data blocks: \t%" PRIu64 "\n", cd->verity_hdr.data_size);
2298 log_std(cd, "Data block size: \t%u\n", cd->verity_hdr.data_block_size);
2299 log_std(cd, "Hash block size: \t%u\n", cd->verity_hdr.hash_block_size);
2300 log_std(cd, "Hash algorithm: \t%s\n", cd->verity_hdr.hash_name);
2301 log_std(cd, "Salt: \t");
2302 if (cd->verity_hdr.salt_size)
2303 hexprint(cd, cd->verity_hdr.salt, cd->verity_hdr.salt_size, "");
2307 if (cd->verity_root_hash) {
2308 log_std(cd, "Root hash: \t");
2309 hexprint(cd, cd->verity_root_hash, cd->verity_root_hash_size, "");
2315 int crypt_dump(struct crypt_device *cd)
2317 if (isLUKS(cd->type))
2318 return _luks_dump(cd);
2319 else if (isVERITY(cd->type))
2320 return _verity_dump(cd);
2322 log_err(cd, _("Dump operation is not supported for this device type.\n"));
2326 const char *crypt_get_cipher(struct crypt_device *cd)
2328 if (isPLAIN(cd->type))
2329 return cd->plain_cipher;
2331 if (isLUKS(cd->type))
2332 return cd->hdr.cipherName;
2334 if (isLOOPAES(cd->type))
2335 return cd->loopaes_cipher;
2340 const char *crypt_get_cipher_mode(struct crypt_device *cd)
2342 if (isPLAIN(cd->type))
2343 return cd->plain_cipher_mode;
2345 if (isLUKS(cd->type))
2346 return cd->hdr.cipherMode;
2348 if (isLOOPAES(cd->type))
2349 return cd->loopaes_cipher_mode;
2354 const char *crypt_get_uuid(struct crypt_device *cd)
2356 if (isLUKS(cd->type))
2357 return cd->hdr.uuid;
2359 if (isPLAIN(cd->type))
2360 return cd->plain_uuid;
2362 if (isLOOPAES(cd->type))
2363 return cd->loopaes_uuid;
2365 if (isVERITY(cd->type))
2366 return cd->verity_uuid;
2371 const char *crypt_get_device_name(struct crypt_device *cd)
2373 const char *path = device_block_path(cd->device);
2376 path = device_path(cd->device);
2381 int crypt_get_volume_key_size(struct crypt_device *cd)
2383 if (isPLAIN(cd->type))
2384 return cd->plain_key_size;
2386 if (isLUKS(cd->type))
2387 return cd->hdr.keyBytes;
2389 if (isLOOPAES(cd->type))
2390 return cd->loopaes_key_size;
2392 if (isVERITY(cd->type))
2393 return cd->verity_root_hash_size;
2398 uint64_t crypt_get_data_offset(struct crypt_device *cd)
2400 if (isPLAIN(cd->type))
2401 return cd->plain_hdr.offset;
2403 if (isLUKS(cd->type))
2404 return cd->hdr.payloadOffset;
2406 if (isLOOPAES(cd->type))
2407 return cd->loopaes_hdr.offset;
2409 if (isTCRYPT(cd->type)) { // FIXME: system vol.
2410 if (!cd->tcrypt_hdr.d.mk_offset)
2412 return (cd->tcrypt_hdr.d.mk_offset / cd->tcrypt_hdr.d.sector_size);
2418 uint64_t crypt_get_iv_offset(struct crypt_device *cd)
2420 if (isPLAIN(cd->type))
2421 return cd->plain_hdr.skip;
2423 if (isLUKS(cd->type))
2426 if (isLOOPAES(cd->type))
2427 return cd->loopaes_hdr.skip;
2429 if (isTCRYPT(cd->type)) {
2430 if (!cd->tcrypt_hdr.d.mk_offset)
2432 return (cd->tcrypt_hdr.d.mk_offset / cd->tcrypt_hdr.d.sector_size);
2438 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
2440 if (!isLUKS(cd->type)) {
2441 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2442 return CRYPT_SLOT_INVALID;
2445 return LUKS_keyslot_info(&cd->hdr, keyslot);
2448 int crypt_keyslot_max(const char *type)
2450 if (type && isLUKS(type))
2451 return LUKS_NUMKEYS;
2456 int crypt_keyslot_area(struct crypt_device *cd,
2461 if (!isLUKS(cd->type))
2464 return LUKS_keyslot_area(&cd->hdr, keyslot, offset, length);
2467 const char *crypt_get_type(struct crypt_device *cd)
2472 int crypt_get_verity_info(struct crypt_device *cd,
2473 struct crypt_params_verity *vp)
2475 if (!isVERITY(cd->type) || !vp)
2478 vp->data_device = device_path(cd->device);
2479 vp->hash_device = mdata_device_path(cd);
2480 vp->hash_name = cd->verity_hdr.hash_name;
2481 vp->salt = cd->verity_hdr.salt;
2482 vp->salt_size = cd->verity_hdr.salt_size;
2483 vp->data_block_size = cd->verity_hdr.data_block_size;
2484 vp->hash_block_size = cd->verity_hdr.hash_block_size;
2485 vp->data_size = cd->verity_hdr.data_size;
2486 vp->hash_area_offset = cd->verity_hdr.hash_area_offset;
2487 vp->hash_type = cd->verity_hdr.hash_type;
2488 vp->flags = cd->verity_hdr.flags & CRYPT_VERITY_NO_HEADER;
2492 int crypt_get_active_device(struct crypt_device *cd __attribute__((unused)),
2494 struct crypt_active_device *cad)
2496 struct crypt_dm_active_device dmd;
2499 r = dm_query_device(cd, name, 0, &dmd);
2503 if (dmd.target != DM_CRYPT && dmd.target != DM_VERITY)
2506 cad->offset = dmd.u.crypt.offset;
2507 cad->iv_offset = dmd.u.crypt.iv_offset;
2508 cad->size = dmd.size;
2509 cad->flags = dmd.flags;