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 char *metadata_device;
41 /* loopback automatic detach helpers */
43 int loop_metadata_device_fd;
45 struct volume_key *volume_key;
47 uint64_t iteration_time;
52 /* used in CRYPT_LUKS1 */
54 uint64_t PBKDF2_per_sec;
56 /* used in CRYPT_PLAIN */
57 struct crypt_params_plain plain_hdr;
59 char *plain_cipher_mode;
61 unsigned int plain_key_size;
63 /* used in CRYPT_LOOPAES */
64 struct crypt_params_loopaes loopaes_hdr;
66 char *loopaes_cipher_mode;
68 unsigned int loopaes_key_size;
70 /* used in CRYPT_VERITY */
71 struct crypt_params_verity verity_hdr;
72 char *verity_root_hash;
73 unsigned int verity_root_hash_size;
76 /* callbacks definitions */
77 void (*log)(int level, const char *msg, void *usrptr);
79 int (*confirm)(const char *msg, void *usrptr);
81 int (*password)(const char *msg, char *buf, size_t length, void *usrptr);
82 void *password_usrptr;
84 /* last error message */
85 char error[MAX_ERROR_LENGTH];
89 /* FIXME: not thread safe, remove this later */
90 static char global_error[MAX_ERROR_LENGTH] = {0};
93 static void (*_default_log)(int level, const char *msg, void *usrptr) = NULL;
94 static int _debug_level = 0;
96 void crypt_set_debug_level(int level)
101 int crypt_get_debug_level(void)
106 static void crypt_set_error(struct crypt_device *cd, const char *error)
108 size_t size = strlen(error);
110 /* Set global error, ugly hack... */
111 strncpy(global_error, error, MAX_ERROR_LENGTH - 2);
112 if (size < MAX_ERROR_LENGTH && global_error[size - 1] == '\n')
113 global_error[size - 1] = '\0';
115 /* Set error string per context */
117 strncpy(cd->error, error, MAX_ERROR_LENGTH - 2);
118 if (size < MAX_ERROR_LENGTH && cd->error[size - 1] == '\n')
119 cd->error[size - 1] = '\0';
123 void crypt_log(struct crypt_device *cd, int level, const char *msg)
126 cd->log(level, msg, cd->log_usrptr);
127 else if (_default_log)
128 _default_log(level, msg, NULL);
130 if (level == CRYPT_LOG_ERROR)
131 crypt_set_error(cd, msg);
134 __attribute__((format(printf, 5, 6)))
135 void logger(struct crypt_device *cd, int level, const char *file,
136 int line, const char *format, ...)
141 va_start(argp, format);
143 if (vasprintf(&target, format, argp) > 0 ) {
145 crypt_log(cd, level, target);
147 } else if (_debug_level)
148 printf("# %s:%d %s\n", file ?: "?", line, target);
150 } else if (_debug_level)
151 printf("# %s\n", target);
159 static const char *mdata_device(struct crypt_device *cd)
161 return cd->metadata_device ?: cd->device;
164 static int init_crypto(struct crypt_device *ctx)
168 crypt_fips_libcryptsetup_check(ctx);
170 r = crypt_random_init(ctx);
172 log_err(ctx, _("Cannot initialize crypto RNG backend.\n"));
176 r = crypt_backend_init(ctx);
178 log_err(ctx, _("Cannot initialize crypto backend.\n"));
180 log_dbg("Crypto backend (%s) initialized.", crypt_backend_version());
184 static int process_key(struct crypt_device *cd, const char *hash_name,
185 size_t key_size, const char *pass, size_t passLen,
186 struct volume_key **vk)
193 *vk = crypt_alloc_volume_key(key_size, NULL);
198 r = crypt_plain_hash(cd, hash_name, (*vk)->key, key_size, pass, passLen);
201 log_err(cd, _("Hash algorithm %s not supported.\n"),
204 log_err(cd, _("Key processing error (using hash %s).\n"),
206 crypt_free_volume_key(*vk);
210 } else if (passLen > key_size) {
211 memcpy((*vk)->key, pass, key_size);
213 memcpy((*vk)->key, pass, passLen);
219 static int isPLAIN(const char *type)
221 return (type && !strcmp(CRYPT_PLAIN, type));
224 static int isLUKS(const char *type)
226 return (type && !strcmp(CRYPT_LUKS1, type));
229 static int isLOOPAES(const char *type)
231 return (type && !strcmp(CRYPT_LOOPAES, type));
234 static int isVERITY(const char *type)
236 return (type && !strcmp(CRYPT_VERITY, type));
239 /* keyslot helpers */
240 static int keyslot_verify_or_find_empty(struct crypt_device *cd, int *keyslot)
242 if (*keyslot == CRYPT_ANY_SLOT) {
243 *keyslot = LUKS_keyslot_find_empty(&cd->hdr);
245 log_err(cd, _("All key slots full.\n"));
250 switch (LUKS_keyslot_info(&cd->hdr, *keyslot)) {
251 case CRYPT_SLOT_INVALID:
252 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
253 *keyslot, LUKS_NUMKEYS - 1);
255 case CRYPT_SLOT_INACTIVE:
258 log_err(cd, _("Key slot %d is full, please select another one.\n"),
267 * compares UUIDs returned by device-mapper (striped by cryptsetup) and uuid in header
269 static int crypt_uuid_cmp(const char *dm_uuid, const char *hdr_uuid)
274 if (!dm_uuid || !hdr_uuid)
277 str = strchr(dm_uuid, '-');
281 for (i = 0, j = 1; hdr_uuid[i]; i++) {
282 if (hdr_uuid[i] == '-')
285 if (!str[j] || str[j] == '-')
288 if (str[j] != hdr_uuid[i])
296 int PLAIN_activate(struct crypt_device *cd,
298 struct volume_key *vk,
303 char *dm_cipher = NULL;
304 enum devcheck device_check;
305 struct crypt_dm_active_device dmd = {
307 .uuid = crypt_get_uuid(cd),
310 .data_device = crypt_get_device_name(cd),
314 .offset = crypt_get_data_offset(cd),
315 .iv_offset = crypt_get_iv_offset(cd),
319 if (dmd.flags & CRYPT_ACTIVATE_SHARED)
320 device_check = DEV_SHARED;
322 device_check = DEV_EXCL;
324 r = device_check_and_adjust(cd, dmd.data_device, device_check,
325 &dmd.size, &dmd.u.crypt.offset, &dmd.flags);
329 if (crypt_get_cipher_mode(cd))
330 r = asprintf(&dm_cipher, "%s-%s", crypt_get_cipher(cd), crypt_get_cipher_mode(cd));
332 r = asprintf(&dm_cipher, "%s", crypt_get_cipher(cd));
336 dmd.u.crypt.cipher = dm_cipher;
337 log_dbg("Trying to activate PLAIN device %s using cipher %s.",
338 name, dmd.u.crypt.cipher);
340 r = dm_create_device(name, CRYPT_PLAIN, &dmd, 0);
343 if (!cd->plain_uuid && dm_query_device(name, DM_ACTIVE_UUID, &dmd) >= 0)
344 cd->plain_uuid = CONST_CAST(char*)dmd.uuid;
350 int crypt_confirm(struct crypt_device *cd, const char *msg)
352 if (!cd || !cd->confirm)
355 return cd->confirm(msg, cd->confirm_usrptr);
358 static int key_from_terminal(struct crypt_device *cd, char *msg, char **key,
359 size_t *key_len, int force_verify)
361 char *prompt = NULL, *device_name;
366 if (crypt_loop_device(crypt_get_device_name(cd)))
367 device_name = crypt_loop_backing_file(crypt_get_device_name(cd));
369 device_name = strdup(crypt_get_device_name(cd));
372 r = asprintf(&prompt, _("Enter passphrase for %s: "), device_name);
380 *key = crypt_safe_alloc(DEFAULT_PASSPHRASE_SIZE_MAX);
385 r = cd->password(msg, *key, DEFAULT_PASSPHRASE_SIZE_MAX,
386 cd->password_usrptr);
388 crypt_safe_free(*key);
393 r = crypt_get_key(msg, key, key_len, 0, 0, NULL, cd->timeout,
394 (force_verify || cd->password_verify), cd);
397 return (r < 0) ? r: 0;
400 static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslot,
401 struct volume_key **vk)
403 char *passphrase_read = NULL;
404 size_t passphrase_size_read;
405 int r = -EINVAL, eperm = 0, tries = cd->tries;
409 crypt_free_volume_key(*vk);
412 r = key_from_terminal(cd, NULL, &passphrase_read,
413 &passphrase_size_read, 0);
414 /* Continue if it is just passphrase verify mismatch */
420 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase_read,
421 passphrase_size_read, &cd->hdr, vk, cd);
424 crypt_safe_free(passphrase_read);
425 passphrase_read = NULL;
426 } while (r == -EPERM && (--tries > 0));
429 crypt_free_volume_key(*vk);
432 /* Report wrong passphrase if at least one try failed */
433 if (eperm && r == -EPIPE)
437 crypt_safe_free(passphrase_read);
441 static int key_from_file(struct crypt_device *cd, char *msg,
442 char **key, size_t *key_len,
443 const char *key_file, size_t key_offset,
446 return crypt_get_key(msg, key, key_len, key_offset, key_size, key_file,
450 void crypt_set_log_callback(struct crypt_device *cd,
451 void (*log)(int level, const char *msg, void *usrptr),
458 cd->log_usrptr = usrptr;
462 void crypt_set_confirm_callback(struct crypt_device *cd,
463 int (*confirm)(const char *msg, void *usrptr),
466 cd->confirm = confirm;
467 cd->confirm_usrptr = usrptr;
470 void crypt_set_password_callback(struct crypt_device *cd,
471 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
474 cd->password = password;
475 cd->password_usrptr = usrptr;
478 static void _get_error(char *error, char *buf, size_t size)
480 if (!buf || size < 1)
483 strncpy(buf, error, size - 1);
484 buf[size - 1] = '\0';
490 void crypt_last_error(struct crypt_device *cd, char *buf, size_t size)
493 return _get_error(cd->error, buf, size);
496 /* Deprecated global error interface */
497 void crypt_get_error(char *buf, size_t size)
499 return _get_error(global_error, buf, size);
502 const char *crypt_get_dir(void)
507 static int set_device_or_loop(const char *device_org, char **device, int *loop_fd)
514 r = device_ready(NULL, device_org, O_RDONLY);
516 *device = crypt_loop_get_device();
517 log_dbg("Not a block device, %s%s.", *device ?
518 "using free loop device " : "no free loop device found",
521 log_err(NULL, _("Cannot find a free loopback device.\n"));
525 /* Keep the loop open, dettached on last close. */
526 *loop_fd = crypt_loop_attach(*device, device_org, 0, 1, &readonly);
527 if (*loop_fd == -1) {
528 log_err(NULL, _("Attaching loopback device failed "
529 "(loop device with autoclear flag is required).\n"));
533 r = device_ready(NULL, *device, O_RDONLY);
539 if (!*device && device_org && !(*device = strdup(device_org)))
545 int crypt_init(struct crypt_device **cd, const char *device)
547 struct crypt_device *h = NULL;
553 log_dbg("Allocating crypt device %s context.", device);
555 if (!(h = malloc(sizeof(struct crypt_device))))
558 memset(h, 0, sizeof(*h));
559 h->loop_device_fd = -1;
560 h->loop_metadata_device_fd = -1;
562 r = set_device_or_loop(device, &h->device, &h->loop_device_fd);
566 if (dm_init(h, 1) < 0) {
571 h->iteration_time = 1000;
572 h->password_verify = 0;
574 h->rng_type = crypt_random_default_key_rng();
580 if (h->loop_device_fd != -1)
581 close(h->loop_device_fd);
588 static int crypt_check_data_device_size(struct crypt_device *cd)
591 uint64_t size, size_min;
593 /* Check data device size, require at least one sector */
594 size_min = crypt_get_data_offset(cd) << SECTOR_SHIFT ?: SECTOR_SIZE;
596 r = device_size(crypt_get_device_name(cd), &size);
600 if (size < size_min) {
601 log_err(cd, _("Header detected but device %s is too small.\n"),
602 crypt_get_device_name(cd));
609 int crypt_set_data_device(struct crypt_device *cd, const char *device)
611 char *data_device = NULL;
614 log_dbg("Setting ciphertext data device to %s.", device ?: "(none)");
616 if (!isLUKS(cd->type) && !isVERITY(cd->type)) {
617 log_err(cd, _("This operation is not supported for this device type.\n"));
621 /* metadata device must be set */
622 if (!cd->device || !device)
625 r = set_device_or_loop(device, &data_device, &loop_fd);
629 if (!cd->metadata_device) {
630 cd->metadata_device = cd->device;
631 cd->loop_metadata_device_fd = cd->loop_device_fd;
634 if (cd->loop_device_fd != -1)
635 close(cd->loop_device_fd);
638 cd->device = data_device;
639 cd->loop_device_fd = loop_fd;
641 return crypt_check_data_device_size(cd);
644 static int _crypt_load_luks1(struct crypt_device *cd, int require_header, int repair)
646 struct luks_phdr hdr;
653 r = LUKS_read_phdr(mdata_device(cd), &hdr, require_header, repair, cd);
657 if (!cd->type && !(cd->type = strdup(CRYPT_LUKS1)))
660 memcpy(&cd->hdr, &hdr, sizeof(hdr));
665 static int _crypt_load_verity(struct crypt_device *cd, struct crypt_params_verity *params)
668 size_t sb_offset = 0;
674 if (params->flags & CRYPT_VERITY_NO_HEADER)
678 sb_offset = params->hash_area_offset;
680 r = VERITY_read_sb(cd, mdata_device(cd), sb_offset,
681 &cd->verity_uuid, &cd->verity_hdr);
686 cd->verity_hdr.flags = params->flags;
688 if (params && params->data_device &&
689 (r = crypt_set_data_device(cd, params->data_device)) < 0)
692 /* Hash availability checked in sb load */
693 cd->verity_root_hash_size = crypt_hash_size(cd->verity_hdr.hash_name);
694 if (cd->verity_root_hash_size > 4096)
697 if (!cd->type && !(cd->type = strdup(CRYPT_VERITY)))
703 static int _init_by_name_crypt(struct crypt_device *cd, const char *name)
705 struct crypt_dm_active_device dmd = {};
706 char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
709 r = dm_query_device(name, DM_ACTIVE_DEVICE |
711 DM_ACTIVE_CRYPT_CIPHER |
712 DM_ACTIVE_CRYPT_KEYSIZE, &dmd);
716 if (isPLAIN(cd->type)) {
717 cd->plain_uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
718 cd->plain_hdr.hash = NULL; /* no way to get this */
719 cd->plain_hdr.offset = dmd.u.crypt.offset;
720 cd->plain_hdr.skip = dmd.u.crypt.iv_offset;
721 cd->plain_key_size = dmd.u.crypt.vk->keylength;
723 r = crypt_parse_name_and_mode(dmd.u.crypt.cipher, cipher, NULL, cipher_mode);
725 cd->plain_cipher = strdup(cipher);
726 cd->plain_cipher_mode = strdup(cipher_mode);
728 } else if (isLOOPAES(cd->type)) {
729 cd->loopaes_uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
730 cd->loopaes_hdr.offset = dmd.u.crypt.offset;
732 r = crypt_parse_name_and_mode(dmd.u.crypt.cipher, cipher,
733 &key_nums, cipher_mode);
735 cd->loopaes_cipher = strdup(cipher);
736 cd->loopaes_cipher_mode = strdup(cipher_mode);
737 /* version 3 uses last key for IV */
738 if (dmd.u.crypt.vk->keylength % key_nums)
740 cd->loopaes_key_size = dmd.u.crypt.vk->keylength / key_nums;
742 } else if (isLUKS(cd->type)) {
743 if (mdata_device(cd)) {
744 r = _crypt_load_luks1(cd, 0, 0);
746 log_dbg("LUKS device header does not match active device.");
752 /* check whether UUIDs match each other */
753 r = crypt_uuid_cmp(dmd.uuid, cd->hdr.uuid);
755 log_dbg("LUKS device header uuid: %s mismatches DM returned uuid %s",
756 cd->hdr.uuid, dmd.uuid);
765 crypt_free_volume_key(dmd.u.crypt.vk);
766 free(CONST_CAST(void*)dmd.u.crypt.cipher);
767 free(CONST_CAST(void*)dmd.data_device);
768 free(CONST_CAST(void*)dmd.uuid);
772 static int _init_by_name_verity(struct crypt_device *cd, const char *name)
774 struct crypt_params_verity params = {};
775 struct crypt_dm_active_device dmd = {
777 .u.verity.vp = ¶ms,
781 r = dm_query_device(name, DM_ACTIVE_DEVICE |
783 DM_ACTIVE_VERITY_HASH_DEVICE |
784 DM_ACTIVE_VERITY_PARAMS, &dmd);
788 if (isVERITY(cd->type)) {
789 cd->verity_uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
790 cd->verity_hdr.flags = CRYPT_VERITY_NO_HEADER; //FIXME
791 cd->verity_hdr.data_size = params.data_size;
792 cd->verity_root_hash_size = dmd.u.verity.root_hash_size;
793 cd->verity_root_hash = NULL;
794 cd->verity_hdr.hash_name = params.hash_name;
795 cd->verity_hdr.data_device = NULL;
796 cd->verity_hdr.hash_device = NULL;
797 cd->verity_hdr.data_block_size = params.data_block_size;
798 cd->verity_hdr.hash_block_size = params.hash_block_size;
799 cd->verity_hdr.hash_area_offset = dmd.u.verity.hash_offset;
800 cd->verity_hdr.hash_type = params.hash_type;
801 cd->verity_hdr.flags = params.flags;
802 cd->verity_hdr.salt_size = params.salt_size;
803 cd->verity_hdr.salt = params.salt;
804 if (!(cd->metadata_device = strdup(dmd.u.verity.hash_device)))
808 free(CONST_CAST(void*)dmd.u.verity.hash_device);
809 free(CONST_CAST(void*)dmd.data_device);
810 free(CONST_CAST(void*)dmd.uuid);
814 int crypt_init_by_name_and_header(struct crypt_device **cd,
816 const char *header_device)
818 crypt_status_info ci;
819 struct crypt_dm_active_device dmd;
822 log_dbg("Allocating crypt device context by device %s.", name);
824 ci = crypt_status(NULL, name);
825 if (ci == CRYPT_INVALID)
828 if (ci < CRYPT_ACTIVE) {
829 log_err(NULL, _("Device %s is not active.\n"), name);
833 r = dm_query_device(name, DM_ACTIVE_DEVICE | DM_ACTIVE_UUID, &dmd);
840 r = crypt_init(cd, header_device);
842 r = crypt_init(cd, dmd.data_device);
844 /* Underlying device disappeared but mapping still active */
845 if (!dmd.data_device || r == -ENOTBLK)
846 log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
849 /* Underlying device is not readable but crypt mapping exists */
851 free(CONST_CAST(void*)dmd.data_device);
852 dmd.data_device = NULL;
853 r = crypt_init(cd, NULL);
861 if (!strncmp(CRYPT_PLAIN, dmd.uuid, sizeof(CRYPT_PLAIN)-1))
862 (*cd)->type = strdup(CRYPT_PLAIN);
863 else if (!strncmp(CRYPT_LOOPAES, dmd.uuid, sizeof(CRYPT_LOOPAES)-1))
864 (*cd)->type = strdup(CRYPT_LOOPAES);
865 else if (!strncmp(CRYPT_LUKS1, dmd.uuid, sizeof(CRYPT_LUKS1)-1))
866 (*cd)->type = strdup(CRYPT_LUKS1);
867 else if (!strncmp(CRYPT_VERITY, dmd.uuid, sizeof(CRYPT_VERITY)-1))
868 (*cd)->type = strdup(CRYPT_VERITY);
870 log_dbg("Unknown UUID set, some parameters are not set.");
872 log_dbg("Active device has no UUID set, some parameters are not set.");
875 r = crypt_set_data_device(*cd, dmd.data_device);
880 /* Try to initialise basic parameters from active device */
882 if (dmd.target == DM_CRYPT)
883 r = _init_by_name_crypt(*cd, name);
884 else if (dmd.target == DM_VERITY)
885 r = _init_by_name_verity(*cd, name);
891 free(CONST_CAST(void*)dmd.data_device);
892 free(CONST_CAST(void*)dmd.uuid);
896 int crypt_init_by_name(struct crypt_device **cd, const char *name)
898 return crypt_init_by_name_and_header(cd, name, NULL);
901 static int _crypt_format_plain(struct crypt_device *cd,
903 const char *cipher_mode,
905 size_t volume_key_size,
906 struct crypt_params_plain *params)
908 if (!cipher || !cipher_mode) {
909 log_err(cd, _("Invalid plain crypt parameters.\n"));
913 if (volume_key_size > 1024) {
914 log_err(cd, _("Invalid key size.\n"));
918 if (!(cd->type = strdup(CRYPT_PLAIN)))
921 cd->plain_key_size = volume_key_size;
922 cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
926 cd->plain_cipher = strdup(cipher);
927 cd->plain_cipher_mode = strdup(cipher_mode);
930 cd->plain_uuid = strdup(uuid);
932 if (params && params->hash)
933 cd->plain_hdr.hash = strdup(params->hash);
935 cd->plain_hdr.offset = params ? params->offset : 0;
936 cd->plain_hdr.skip = params ? params->skip : 0;
937 cd->plain_hdr.size = params ? params->size : 0;
939 if (!cd->plain_cipher || !cd->plain_cipher_mode)
945 static int _crypt_format_luks1(struct crypt_device *cd,
947 const char *cipher_mode,
949 const char *volume_key,
950 size_t volume_key_size,
951 struct crypt_params_luks1 *params)
954 unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
955 unsigned long alignment_offset = 0;
957 if (!mdata_device(cd)) {
958 log_err(cd, _("Can't format LUKS without device.\n"));
962 if (!(cd->type = strdup(CRYPT_LUKS1)))
966 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
969 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
974 if (params && params->data_device) {
975 cd->metadata_device = cd->device;
976 if (!(cd->device = strdup(params->data_device)))
978 required_alignment = params->data_alignment * SECTOR_SIZE;
979 } else if (params && params->data_alignment) {
980 required_alignment = params->data_alignment * SECTOR_SIZE;
982 get_topology_alignment(cd->device, &required_alignment,
983 &alignment_offset, DEFAULT_DISK_ALIGNMENT);
985 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
986 (params && params->hash) ? params->hash : "sha1",
988 required_alignment / SECTOR_SIZE,
989 alignment_offset / SECTOR_SIZE,
990 cd->iteration_time, &cd->PBKDF2_per_sec,
991 cd->metadata_device, cd);
995 /* Wipe first 8 sectors - fs magic numbers etc. */
996 r = crypt_wipe(mdata_device(cd), 0, 8 * SECTOR_SIZE, CRYPT_WIPE_ZERO, 1);
999 log_err(cd, _("Cannot format device %s which is still in use.\n"),
1002 log_err(cd, _("Cannot wipe header on device %s.\n"),
1008 r = LUKS_write_phdr(mdata_device(cd), &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 (!mdata_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 (!mdata_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 cd->verity_hdr.hash_name = strdup(params->hash_name);
1108 cd->verity_hdr.data_device = NULL;
1109 cd->verity_hdr.data_block_size = params->data_block_size;
1110 cd->verity_hdr.hash_block_size = params->hash_block_size;
1111 cd->verity_hdr.hash_area_offset = params->hash_area_offset;
1112 cd->verity_hdr.hash_type = params->hash_type;
1113 cd->verity_hdr.flags = params->flags;
1114 cd->verity_hdr.salt_size = params->salt_size;
1115 cd->verity_hdr.salt = malloc(params->salt_size);
1117 memcpy(CONST_CAST(char*)cd->verity_hdr.salt, params->salt,
1120 r = crypt_random_get(cd, CONST_CAST(char*)cd->verity_hdr.salt,
1121 params->salt_size, CRYPT_RND_SALT);
1125 if (params->flags & CRYPT_VERITY_CREATE_HASH) {
1126 r = VERITY_create(cd, &cd->verity_hdr, cd->device, mdata_device(cd),
1127 cd->verity_root_hash, cd->verity_root_hash_size);
1132 if (!(params->flags & CRYPT_VERITY_NO_HEADER)) {
1134 cd->verity_uuid = strdup(uuid);
1136 r = VERITY_UUID_generate(cd, &cd->verity_uuid);
1141 r = VERITY_write_sb(cd, mdata_device(cd),
1142 cd->verity_hdr.hash_area_offset,
1149 int crypt_format(struct crypt_device *cd,
1152 const char *cipher_mode,
1154 const char *volume_key,
1155 size_t volume_key_size,
1164 log_dbg("Context already formatted as %s.", cd->type);
1168 log_dbg("Formatting device %s as type %s.", mdata_device(cd) ?: "(none)", type);
1170 r = init_crypto(cd);
1175 r = _crypt_format_plain(cd, cipher, cipher_mode,
1176 uuid, volume_key_size, params);
1177 else if (isLUKS(type))
1178 r = _crypt_format_luks1(cd, cipher, cipher_mode,
1179 uuid, volume_key, volume_key_size, params);
1180 else if (isLOOPAES(type))
1181 r = _crypt_format_loopaes(cd, cipher, uuid, volume_key_size, params);
1182 else if (isVERITY(type))
1183 r = _crypt_format_verity(cd, uuid, params);
1185 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
1192 crypt_free_volume_key(cd->volume_key);
1193 cd->volume_key = NULL;
1199 int crypt_load(struct crypt_device *cd,
1200 const char *requested_type,
1205 log_dbg("Trying to load %s crypt type from device %s.",
1206 requested_type ?: "any", mdata_device(cd) ?: "(none)");
1208 if (!mdata_device(cd))
1211 if (!requested_type || isLUKS(requested_type)) {
1212 if (cd->type && !isLUKS(cd->type)) {
1213 log_dbg("Context is already initialised to type %s", cd->type);
1217 r = _crypt_load_luks1(cd, 1, 0);
1218 } else if (isVERITY(requested_type)) {
1219 if (cd->type && !isVERITY(cd->type)) {
1220 log_dbg("Context is already initialised to type %s", cd->type);
1223 r = _crypt_load_verity(cd, params);
1230 int crypt_repair(struct crypt_device *cd,
1231 const char *requested_type,
1232 void *params __attribute__((unused)))
1236 log_dbg("Trying to repair %s crypt type from device %s.",
1237 requested_type ?: "any", mdata_device(cd) ?: "(none)");
1239 if (!mdata_device(cd))
1242 if (requested_type && !isLUKS(requested_type))
1246 /* Load with repair */
1247 r = _crypt_load_luks1(cd, 1, 1);
1251 /* cd->type and header must be set in context */
1252 r = crypt_check_data_device_size(cd);
1261 int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
1263 struct crypt_dm_active_device dmd;
1266 /* Device context type must be initialised */
1267 if (!cd->type || !crypt_get_uuid(cd))
1270 log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
1272 r = dm_query_device(name, DM_ACTIVE_DEVICE | DM_ACTIVE_CRYPT_CIPHER |
1273 DM_ACTIVE_UUID | DM_ACTIVE_CRYPT_KEYSIZE |
1274 DM_ACTIVE_CRYPT_KEY, &dmd);
1276 log_err(NULL, _("Device %s is not active.\n"), name);
1280 if (!dmd.uuid || dmd.target != DM_CRYPT) {
1285 r = device_check_and_adjust(cd, dmd.data_device, DEV_OK, &new_size,
1286 &dmd.u.crypt.offset, &dmd.flags);
1290 if (new_size == dmd.size) {
1291 log_dbg("Device has already requested size %" PRIu64
1292 " sectors.", dmd.size);
1295 dmd.size = new_size;
1296 r = dm_create_device(name, cd->type, &dmd, 1);
1299 if (dmd.target == DM_CRYPT) {
1300 crypt_free_volume_key(dmd.u.crypt.vk);
1301 free(CONST_CAST(void*)dmd.u.crypt.cipher);
1303 free(CONST_CAST(void*)dmd.data_device);
1304 free(CONST_CAST(void*)dmd.uuid);
1309 int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
1311 if (!isLUKS(cd->type)) {
1312 log_err(cd, _("This operation is not supported for this device type.\n"));
1316 if (uuid && !strncmp(uuid, cd->hdr.uuid, sizeof(cd->hdr.uuid))) {
1317 log_dbg("UUID is the same as requested (%s) for device %s.",
1318 uuid, mdata_device(cd));
1323 log_dbg("Requested new UUID change to %s for %s.", uuid, mdata_device(cd));
1325 log_dbg("Requested new UUID refresh for %s.", mdata_device(cd));
1327 if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
1330 return LUKS_hdr_uuid_set(mdata_device(cd), &cd->hdr, uuid, cd);
1333 int crypt_header_backup(struct crypt_device *cd,
1334 const char *requested_type,
1335 const char *backup_file)
1339 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1342 r = init_crypto(cd);
1346 log_dbg("Requested header backup of device %s (%s) to "
1347 "file %s.", mdata_device(cd), requested_type, backup_file);
1349 return LUKS_hdr_backup(backup_file, mdata_device(cd), &cd->hdr, cd);
1352 int crypt_header_restore(struct crypt_device *cd,
1353 const char *requested_type,
1354 const char *backup_file)
1358 if (requested_type && !isLUKS(requested_type))
1361 r = init_crypto(cd);
1365 log_dbg("Requested header restore to device %s (%s) from "
1366 "file %s.", mdata_device(cd), requested_type, backup_file);
1368 return LUKS_hdr_restore(backup_file, mdata_device(cd), &cd->hdr, cd);
1371 void crypt_free(struct crypt_device *cd)
1374 log_dbg("Releasing crypt device %s context.", mdata_device(cd));
1376 if (cd->loop_device_fd != -1)
1377 close(cd->loop_device_fd);
1378 if (cd->loop_metadata_device_fd != -1)
1379 close(cd->loop_metadata_device_fd);
1382 crypt_free_volume_key(cd->volume_key);
1385 free(cd->metadata_device);
1388 /* used in plain device only */
1389 free(CONST_CAST(void*)cd->plain_hdr.hash);
1390 free(cd->plain_cipher);
1391 free(cd->plain_cipher_mode);
1392 free(cd->plain_uuid);
1394 /* used in loop-AES device only */
1395 free(CONST_CAST(void*)cd->loopaes_hdr.hash);
1396 free(cd->loopaes_cipher);
1397 free(cd->loopaes_uuid);
1399 /* used in verity device only */
1400 free(CONST_CAST(void*)cd->verity_hdr.hash_name);
1401 free(CONST_CAST(void*)cd->verity_hdr.salt);
1402 free(cd->verity_root_hash);
1403 free(cd->verity_uuid);
1409 int crypt_suspend(struct crypt_device *cd,
1412 crypt_status_info ci;
1415 log_dbg("Suspending volume %s.", name);
1417 if (!isLUKS(cd->type)) {
1418 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1423 ci = crypt_status(NULL, name);
1424 if (ci < CRYPT_ACTIVE) {
1425 log_err(cd, _("Volume %s is not active.\n"), name);
1429 if (!cd && dm_init(NULL, 1) < 0)
1432 r = dm_status_suspended(name);
1437 log_err(cd, _("Volume %s is already suspended.\n"), name);
1442 r = dm_suspend_and_wipe_key(name);
1444 log_err(cd, "Suspend is not supported for device %s.\n", name);
1446 log_err(cd, "Error during suspending device %s.\n", name);
1453 int crypt_resume_by_passphrase(struct crypt_device *cd,
1456 const char *passphrase,
1457 size_t passphrase_size)
1459 struct volume_key *vk = NULL;
1462 log_dbg("Resuming volume %s.", name);
1464 if (!isLUKS(cd->type)) {
1465 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1470 r = dm_status_suspended(name);
1475 log_err(cd, _("Volume %s is not suspended.\n"), name);
1480 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase,
1481 passphrase_size, &cd->hdr, &vk, cd);
1483 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1487 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1489 log_err(cd, "Resume is not supported for device %s.\n", name);
1491 log_err(cd, "Error during resuming device %s.\n", name);
1495 crypt_free_volume_key(vk);
1496 return r < 0 ? r : keyslot;
1499 int crypt_resume_by_keyfile_offset(struct crypt_device *cd,
1502 const char *keyfile,
1503 size_t keyfile_size,
1504 size_t keyfile_offset)
1506 struct volume_key *vk = NULL;
1507 char *passphrase_read = NULL;
1508 size_t passphrase_size_read;
1511 log_dbg("Resuming volume %s.", name);
1513 if (!isLUKS(cd->type)) {
1514 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1519 r = dm_status_suspended(name);
1524 log_err(cd, _("Volume %s is not suspended.\n"), name);
1531 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1532 &passphrase_size_read, keyfile, keyfile_offset,
1537 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase_read,
1538 passphrase_size_read, &cd->hdr, &vk, cd);
1543 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1545 log_err(cd, "Error during resuming device %s.\n", name);
1547 crypt_safe_free(passphrase_read);
1548 crypt_free_volume_key(vk);
1549 return r < 0 ? r : keyslot;
1552 int crypt_resume_by_keyfile(struct crypt_device *cd,
1555 const char *keyfile,
1556 size_t keyfile_size)
1558 return crypt_resume_by_keyfile_offset(cd, name, keyslot,
1559 keyfile, keyfile_size, 0);
1562 // slot manipulation
1563 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1564 int keyslot, // -1 any
1565 const char *passphrase, // NULL -> terminal
1566 size_t passphrase_size,
1567 const char *new_passphrase, // NULL -> terminal
1568 size_t new_passphrase_size)
1570 struct volume_key *vk = NULL;
1571 char *password = NULL, *new_password = NULL;
1572 size_t passwordLen, new_passwordLen;
1575 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1576 "new passphrase %sprovided.",
1577 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1579 if (!isLUKS(cd->type)) {
1580 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1584 r = keyslot_verify_or_find_empty(cd, &keyslot);
1588 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1589 /* No slots used, try to use pre-generated key in header */
1590 if (cd->volume_key) {
1591 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1592 r = vk ? 0 : -ENOMEM;
1594 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1597 } else if (passphrase) {
1598 /* Passphrase provided, use it to unlock existing keyslot */
1599 r = LUKS_open_key_with_hdr(mdata_device(cd), CRYPT_ANY_SLOT, passphrase,
1600 passphrase_size, &cd->hdr, &vk, cd);
1602 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1603 r = key_from_terminal(cd, _("Enter any passphrase: "),
1604 &password, &passwordLen, 0);
1608 r = LUKS_open_key_with_hdr(mdata_device(cd), CRYPT_ANY_SLOT, password,
1609 passwordLen, &cd->hdr, &vk, cd);
1610 crypt_safe_free(password);
1616 if (new_passphrase) {
1617 new_password = CONST_CAST(char*)new_passphrase;
1618 new_passwordLen = new_passphrase_size;
1620 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1621 &new_password, &new_passwordLen, 1);
1626 r = LUKS_set_key(mdata_device(cd), keyslot, new_password, new_passwordLen,
1627 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1632 if (!new_passphrase)
1633 crypt_safe_free(new_password);
1634 crypt_free_volume_key(vk);
1635 return r ?: keyslot;
1638 int crypt_keyslot_add_by_keyfile_offset(struct crypt_device *cd,
1640 const char *keyfile,
1641 size_t keyfile_size,
1642 size_t keyfile_offset,
1643 const char *new_keyfile,
1644 size_t new_keyfile_size,
1645 size_t new_keyfile_offset)
1647 struct volume_key *vk = NULL;
1648 char *password = NULL; size_t passwordLen;
1649 char *new_password = NULL; size_t new_passwordLen;
1652 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1653 keyfile ?: "[none]", new_keyfile ?: "[none]");
1655 if (!isLUKS(cd->type)) {
1656 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1660 r = keyslot_verify_or_find_empty(cd, &keyslot);
1664 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1665 /* No slots used, try to use pre-generated key in header */
1666 if (cd->volume_key) {
1667 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1668 r = vk ? 0 : -ENOMEM;
1670 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1674 /* Read password from file of (if NULL) from terminal */
1676 r = key_from_file(cd, _("Enter any passphrase: "),
1677 &password, &passwordLen,
1678 keyfile, keyfile_offset, keyfile_size);
1680 r = key_from_terminal(cd, _("Enter any passphrase: "),
1681 &password, &passwordLen, 0);
1685 r = LUKS_open_key_with_hdr(mdata_device(cd), CRYPT_ANY_SLOT, password, passwordLen,
1693 r = key_from_file(cd, _("Enter new passphrase for key slot: "),
1694 &new_password, &new_passwordLen, new_keyfile,
1695 new_keyfile_offset, new_keyfile_size);
1697 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1698 &new_password, &new_passwordLen, 1);
1702 r = LUKS_set_key(mdata_device(cd), keyslot, new_password, new_passwordLen,
1703 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1705 crypt_safe_free(password);
1706 crypt_safe_free(new_password);
1707 crypt_free_volume_key(vk);
1708 return r < 0 ? r : keyslot;
1711 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1713 const char *keyfile,
1714 size_t keyfile_size,
1715 const char *new_keyfile,
1716 size_t new_keyfile_size)
1718 return crypt_keyslot_add_by_keyfile_offset(cd, keyslot,
1719 keyfile, keyfile_size, 0,
1720 new_keyfile, new_keyfile_size, 0);
1723 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1725 const char *volume_key,
1726 size_t volume_key_size,
1727 const char *passphrase,
1728 size_t passphrase_size)
1730 struct volume_key *vk = NULL;
1732 char *new_password = NULL; size_t new_passwordLen;
1734 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1736 if (!isLUKS(cd->type)) {
1737 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1742 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1743 else if (cd->volume_key)
1744 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1749 r = LUKS_verify_volume_key(&cd->hdr, vk);
1751 log_err(cd, _("Volume key does not match the volume.\n"));
1755 r = keyslot_verify_or_find_empty(cd, &keyslot);
1760 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1761 &new_password, &new_passwordLen, 1);
1764 passphrase = new_password;
1765 passphrase_size = new_passwordLen;
1768 r = LUKS_set_key(mdata_device(cd), keyslot, passphrase, passphrase_size,
1769 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1771 crypt_safe_free(new_password);
1772 crypt_free_volume_key(vk);
1773 return (r < 0) ? r : keyslot;
1776 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1778 crypt_keyslot_info ki;
1780 log_dbg("Destroying keyslot %d.", keyslot);
1782 if (!isLUKS(cd->type)) {
1783 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1787 ki = crypt_keyslot_status(cd, keyslot);
1788 if (ki == CRYPT_SLOT_INVALID) {
1789 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1793 if (ki == CRYPT_SLOT_INACTIVE) {
1794 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1798 return LUKS_del_key(mdata_device(cd), keyslot, &cd->hdr, cd);
1801 // activation/deactivation of device mapping
1802 int crypt_activate_by_passphrase(struct crypt_device *cd,
1805 const char *passphrase,
1806 size_t passphrase_size,
1809 crypt_status_info ci;
1810 struct volume_key *vk = NULL;
1811 char *read_passphrase = NULL;
1812 size_t passphraseLen = 0;
1815 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1816 name ? "Activating" : "Checking", name ?: "",
1817 keyslot, passphrase ? "" : "[none] ");
1820 ci = crypt_status(NULL, name);
1821 if (ci == CRYPT_INVALID)
1823 else if (ci >= CRYPT_ACTIVE) {
1824 log_err(cd, _("Device %s already exists.\n"), name);
1829 /* plain, use hashed passphrase */
1830 if (isPLAIN(cd->type)) {
1835 r = key_from_terminal(cd, NULL, &read_passphrase,
1839 passphrase = read_passphrase;
1840 passphrase_size = passphraseLen;
1843 r = process_key(cd, cd->plain_hdr.hash,
1845 passphrase, passphrase_size, &vk);
1849 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1851 } else if (isLUKS(cd->type)) {
1852 /* provided passphrase, do not retry */
1854 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase,
1855 passphrase_size, &cd->hdr, &vk, cd);
1857 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1862 r = LUKS1_activate(cd, name, vk, flags);
1867 crypt_safe_free(read_passphrase);
1868 crypt_free_volume_key(vk);
1870 return r < 0 ? r : keyslot;
1873 int crypt_activate_by_keyfile_offset(struct crypt_device *cd,
1876 const char *keyfile,
1877 size_t keyfile_size,
1878 size_t keyfile_offset,
1881 crypt_status_info ci;
1882 struct volume_key *vk = NULL;
1883 char *passphrase_read = NULL;
1884 size_t passphrase_size_read;
1885 unsigned int key_count = 0;
1888 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1889 name ?: "", keyslot, keyfile ?: "[none]");
1892 ci = crypt_status(NULL, name);
1893 if (ci == CRYPT_INVALID)
1895 else if (ci >= CRYPT_ACTIVE) {
1896 log_err(cd, _("Device %s already exists.\n"), name);
1904 if (isPLAIN(cd->type)) {
1908 r = key_from_file(cd, _("Enter passphrase: "),
1909 &passphrase_read, &passphrase_size_read,
1910 keyfile, keyfile_offset, keyfile_size);
1914 r = process_key(cd, cd->plain_hdr.hash,
1916 passphrase_read, passphrase_size_read, &vk);
1920 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1921 } else if (isLUKS(cd->type)) {
1922 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1923 &passphrase_size_read, keyfile, keyfile_offset, keyfile_size);
1926 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase_read,
1927 passphrase_size_read, &cd->hdr, &vk, cd);
1933 r = LUKS1_activate(cd, name, vk, flags);
1938 } else if (isLOOPAES(cd->type)) {
1939 r = key_from_file(cd, NULL, &passphrase_read, &passphrase_size_read,
1940 keyfile, keyfile_offset, keyfile_size);
1943 r = LOOPAES_parse_keyfile(cd, &vk, cd->loopaes_hdr.hash, &key_count,
1944 passphrase_read, passphrase_size_read);
1948 r = LOOPAES_activate(cd, name, cd->loopaes_cipher,
1949 key_count, vk, flags);
1954 crypt_safe_free(passphrase_read);
1955 crypt_free_volume_key(vk);
1960 int crypt_activate_by_keyfile(struct crypt_device *cd,
1963 const char *keyfile,
1964 size_t keyfile_size,
1967 return crypt_activate_by_keyfile_offset(cd, name, keyslot, keyfile,
1968 keyfile_size, 0, flags);
1971 int crypt_activate_by_volume_key(struct crypt_device *cd,
1973 const char *volume_key,
1974 size_t volume_key_size,
1977 crypt_status_info ci;
1978 struct volume_key *vk = NULL;
1981 log_dbg("Activating volume %s by volume key.", name ?: "[none]");
1984 ci = crypt_status(NULL, name);
1985 if (ci == CRYPT_INVALID)
1987 else if (ci >= CRYPT_ACTIVE) {
1988 log_err(cd, _("Device %s already exists.\n"), name);
1993 /* use key directly, no hash */
1994 if (isPLAIN(cd->type)) {
1998 if (!volume_key || !volume_key_size || volume_key_size != cd->plain_key_size) {
1999 log_err(cd, _("Incorrect volume key specified for plain device.\n"));
2003 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2007 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
2008 } else if (isLUKS(cd->type)) {
2009 /* If key is not provided, try to use internal key */
2011 if (!cd->volume_key) {
2012 log_err(cd, _("Volume key does not match the volume.\n"));
2015 volume_key_size = cd->volume_key->keylength;
2016 volume_key = cd->volume_key->key;
2019 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2022 r = LUKS_verify_volume_key(&cd->hdr, vk);
2025 log_err(cd, _("Volume key does not match the volume.\n"));
2028 r = LUKS1_activate(cd, name, vk, flags);
2029 } else if (isVERITY(cd->type)) {
2030 /* volume_key == root hash */
2031 if (!volume_key || !volume_key_size) {
2032 log_err(cd, _("Incorrect root hash specified for verity device.\n"));
2036 r = VERITY_activate(cd, name, mdata_device(cd),
2037 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);
2051 log_err(cd, _("Device type is not properly initialised.\n"));
2053 crypt_free_volume_key(vk);
2058 int crypt_deactivate(struct crypt_device *cd, const char *name)
2065 log_dbg("Deactivating volume %s.", name);
2067 if (!cd && dm_init(NULL, 1) < 0)
2070 switch (crypt_status(cd, name)) {
2073 r = dm_remove_device(name, 0, 0);
2075 case CRYPT_INACTIVE:
2076 log_err(cd, _("Device %s is not active.\n"), name);
2080 log_err(cd, _("Invalid device %s.\n"), name);
2090 int crypt_volume_key_get(struct crypt_device *cd,
2093 size_t *volume_key_size,
2094 const char *passphrase,
2095 size_t passphrase_size)
2097 struct volume_key *vk = NULL;
2101 if (crypt_fips_mode()) {
2102 log_err(cd, "Function not available in FIPS mode.\n");
2106 key_len = crypt_get_volume_key_size(cd);
2107 if (key_len > *volume_key_size) {
2108 log_err(cd, _("Volume key buffer too small.\n"));
2112 if (isPLAIN(cd->type) && cd->plain_hdr.hash) {
2113 r = process_key(cd, cd->plain_hdr.hash, key_len,
2114 passphrase, passphrase_size, &vk);
2116 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
2117 } else if (isLUKS(cd->type)) {
2118 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase,
2119 passphrase_size, &cd->hdr, &vk, cd);
2122 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
2125 memcpy(volume_key, vk->key, vk->keylength);
2126 *volume_key_size = vk->keylength;
2129 crypt_free_volume_key(vk);
2133 int crypt_volume_key_verify(struct crypt_device *cd,
2134 const char *volume_key,
2135 size_t volume_key_size)
2137 struct volume_key *vk;
2140 if (!isLUKS(cd->type)) {
2141 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2145 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2149 r = LUKS_verify_volume_key(&cd->hdr, vk);
2152 log_err(cd, _("Volume key does not match the volume.\n"));
2154 crypt_free_volume_key(vk);
2159 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
2161 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
2162 cd->timeout = timeout_sec;
2165 void crypt_set_password_retry(struct crypt_device *cd, int tries)
2167 log_dbg("Password retry count set to %d.", tries);
2171 void crypt_set_iteration_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2173 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
2174 cd->iteration_time = iteration_time_ms;
2176 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2178 crypt_set_iteration_time(cd, iteration_time_ms);
2181 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
2183 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
2184 cd->password_verify = password_verify ? 1 : 0;
2187 void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
2190 case CRYPT_RNG_URANDOM:
2191 case CRYPT_RNG_RANDOM:
2192 log_dbg("RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
2193 cd->rng_type = rng_type;
2197 int crypt_get_rng_type(struct crypt_device *cd)
2202 return cd->rng_type;
2205 int crypt_memory_lock(struct crypt_device *cd, int lock)
2207 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
2211 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
2215 if (!cd && dm_init(NULL, 1) < 0)
2216 return CRYPT_INVALID;
2218 r = dm_status_device(name);
2223 if (r < 0 && r != -ENODEV)
2224 return CRYPT_INVALID;
2227 return CRYPT_ACTIVE;
2232 return CRYPT_INACTIVE;
2235 static void hexprint(struct crypt_device *cd, const char *d, int n, const char *sep)
2238 for(i = 0; i < n; i++)
2239 log_std(cd, "%02hhx%s", (const char)d[i], sep);
2242 static int _luks_dump(struct crypt_device *cd)
2246 log_std(cd, "LUKS header information for %s\n\n", mdata_device(cd));
2247 log_std(cd, "Version: \t%d\n", cd->hdr.version);
2248 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
2249 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
2250 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec);
2251 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
2252 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
2253 log_std(cd, "MK digest: \t");
2254 hexprint(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE, " ");
2256 log_std(cd, "MK salt: \t");
2257 hexprint(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2, " ");
2258 log_std(cd, "\n \t");
2259 hexprint(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ");
2261 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
2262 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
2263 for(i = 0; i < LUKS_NUMKEYS; i++) {
2264 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
2265 log_std(cd, "Key Slot %d: ENABLED\n",i);
2266 log_std(cd, "\tIterations: \t%d\n",
2267 cd->hdr.keyblock[i].passwordIterations);
2268 log_std(cd, "\tSalt: \t");
2269 hexprint(cd, cd->hdr.keyblock[i].passwordSalt,
2270 LUKS_SALTSIZE/2, " ");
2271 log_std(cd, "\n\t \t");
2272 hexprint(cd, cd->hdr.keyblock[i].passwordSalt +
2273 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ");
2276 log_std(cd, "\tKey material offset:\t%d\n",
2277 cd->hdr.keyblock[i].keyMaterialOffset);
2278 log_std(cd, "\tAF stripes: \t%d\n",
2279 cd->hdr.keyblock[i].stripes);
2282 log_std(cd, "Key Slot %d: DISABLED\n", i);
2287 static int _verity_dump(struct crypt_device *cd)
2289 log_std(cd, "VERITY header information for %s\n", mdata_device(cd));
2290 log_std(cd, "UUID: \t%s\n", cd->verity_uuid ?: "");
2291 log_std(cd, "Hash type: \t%u\n", cd->verity_hdr.hash_type);
2292 log_std(cd, "Data blocks: \t%" PRIu64 "\n", cd->verity_hdr.data_size);
2293 log_std(cd, "Data block size: \t%u\n", cd->verity_hdr.data_block_size);
2294 log_std(cd, "Hash block size: \t%u\n", cd->verity_hdr.hash_block_size);
2295 log_std(cd, "Hash algorithm: \t%s\n", cd->verity_hdr.hash_name);
2296 log_std(cd, "Salt: \t");
2297 if (cd->verity_hdr.salt_size)
2298 hexprint(cd, cd->verity_hdr.salt, cd->verity_hdr.salt_size, "");
2302 if (cd->verity_root_hash) {
2303 log_std(cd, "Root hash: \t");
2304 hexprint(cd, cd->verity_root_hash, cd->verity_root_hash_size, "");
2310 int crypt_dump(struct crypt_device *cd)
2312 if (isLUKS(cd->type))
2313 return _luks_dump(cd);
2314 else if (isVERITY(cd->type))
2315 return _verity_dump(cd);
2317 log_err(cd, _("Dump operation is not supported for this device type.\n"));
2321 const char *crypt_get_cipher(struct crypt_device *cd)
2323 if (isPLAIN(cd->type))
2324 return cd->plain_cipher;
2326 if (isLUKS(cd->type))
2327 return cd->hdr.cipherName;
2329 if (isLOOPAES(cd->type))
2330 return cd->loopaes_cipher;
2335 const char *crypt_get_cipher_mode(struct crypt_device *cd)
2337 if (isPLAIN(cd->type))
2338 return cd->plain_cipher_mode;
2340 if (isLUKS(cd->type))
2341 return cd->hdr.cipherMode;
2343 if (isLOOPAES(cd->type))
2344 return cd->loopaes_cipher_mode;
2349 const char *crypt_get_uuid(struct crypt_device *cd)
2351 if (isLUKS(cd->type))
2352 return cd->hdr.uuid;
2354 if (isPLAIN(cd->type))
2355 return cd->plain_uuid;
2357 if (isLOOPAES(cd->type))
2358 return cd->loopaes_uuid;
2360 if (isVERITY(cd->type))
2361 return cd->verity_uuid;
2366 const char *crypt_get_device_name(struct crypt_device *cd)
2371 int crypt_get_volume_key_size(struct crypt_device *cd)
2373 if (isPLAIN(cd->type))
2374 return cd->plain_key_size;
2376 if (isLUKS(cd->type))
2377 return cd->hdr.keyBytes;
2379 if (isLOOPAES(cd->type))
2380 return cd->loopaes_key_size;
2382 if (isVERITY(cd->type))
2383 return cd->verity_root_hash_size;
2388 uint64_t crypt_get_data_offset(struct crypt_device *cd)
2390 if (isPLAIN(cd->type))
2391 return cd->plain_hdr.offset;
2393 if (isLUKS(cd->type))
2394 return cd->hdr.payloadOffset;
2396 if (isLOOPAES(cd->type))
2397 return cd->loopaes_hdr.offset;
2402 uint64_t crypt_get_iv_offset(struct crypt_device *cd)
2404 if (isPLAIN(cd->type))
2405 return cd->plain_hdr.skip;
2407 if (isLUKS(cd->type))
2410 if (isLOOPAES(cd->type))
2411 return cd->loopaes_hdr.skip;
2416 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
2418 if (!isLUKS(cd->type)) {
2419 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2420 return CRYPT_SLOT_INVALID;
2423 return LUKS_keyslot_info(&cd->hdr, keyslot);
2426 int crypt_keyslot_max(const char *type)
2428 if (type && isLUKS(type))
2429 return LUKS_NUMKEYS;
2434 const char *crypt_get_type(struct crypt_device *cd)
2439 int crypt_get_verity_info(struct crypt_device *cd,
2440 struct crypt_params_verity *vp)
2442 if (!isVERITY(cd->type) || !vp)
2445 vp->data_device = cd->device;
2446 vp->hash_device = mdata_device(cd);
2447 vp->hash_name = cd->verity_hdr.hash_name;
2448 vp->salt = cd->verity_hdr.salt;
2449 vp->salt_size = cd->verity_hdr.salt_size;
2450 vp->data_block_size = cd->verity_hdr.data_block_size;
2451 vp->hash_block_size = cd->verity_hdr.hash_block_size;
2452 vp->data_size = cd->verity_hdr.data_size;
2453 vp->hash_area_offset = cd->verity_hdr.hash_area_offset;
2454 vp->hash_type = cd->verity_hdr.hash_type;
2455 vp->flags = cd->verity_hdr.flags & CRYPT_VERITY_NO_HEADER;
2459 int crypt_get_active_device(struct crypt_device *cd __attribute__((unused)),
2461 struct crypt_active_device *cad)
2463 struct crypt_dm_active_device dmd;
2466 r = dm_query_device(name, 0, &dmd);
2470 if (dmd.target != DM_CRYPT && dmd.target != DM_VERITY)
2473 cad->offset = dmd.u.crypt.offset;
2474 cad->iv_offset = dmd.u.crypt.iv_offset;
2475 cad->size = dmd.size;
2476 cad->flags = dmd.flags;