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;
43 struct volume_key *volume_key;
45 uint64_t iteration_time;
50 /* used in CRYPT_LUKS1 */
52 uint64_t PBKDF2_per_sec;
54 /* used in CRYPT_PLAIN */
55 struct crypt_params_plain plain_hdr;
57 char *plain_cipher_mode;
59 unsigned int plain_key_size;
61 /* used in CRYPT_LOOPAES */
62 struct crypt_params_loopaes loopaes_hdr;
64 char *loopaes_cipher_mode;
66 unsigned int loopaes_key_size;
68 /* used in CRYPT_VERITY */
69 struct crypt_params_verity verity_hdr;
70 uint32_t verity_flags;
71 char *verity_root_hash;
72 uint64_t verity_root_hash_size;
74 /* callbacks definitions */
75 void (*log)(int level, const char *msg, void *usrptr);
77 int (*confirm)(const char *msg, void *usrptr);
79 int (*password)(const char *msg, char *buf, size_t length, void *usrptr);
80 void *password_usrptr;
82 /* last error message */
83 char error[MAX_ERROR_LENGTH];
87 /* FIXME: not thread safe, remove this later */
88 static char global_error[MAX_ERROR_LENGTH] = {0};
91 static void (*_default_log)(int level, const char *msg, void *usrptr) = NULL;
92 static int _debug_level = 0;
94 void crypt_set_debug_level(int level)
99 int crypt_get_debug_level(void)
104 static void crypt_set_error(struct crypt_device *cd, const char *error)
106 size_t size = strlen(error);
108 /* Set global error, ugly hack... */
109 strncpy(global_error, error, MAX_ERROR_LENGTH - 2);
110 if (size < MAX_ERROR_LENGTH && global_error[size - 1] == '\n')
111 global_error[size - 1] = '\0';
113 /* Set error string per context */
115 strncpy(cd->error, error, MAX_ERROR_LENGTH - 2);
116 if (size < MAX_ERROR_LENGTH && cd->error[size - 1] == '\n')
117 cd->error[size - 1] = '\0';
121 void crypt_log(struct crypt_device *cd, int level, const char *msg)
124 cd->log(level, msg, cd->log_usrptr);
125 else if (_default_log)
126 _default_log(level, msg, NULL);
128 if (level == CRYPT_LOG_ERROR)
129 crypt_set_error(cd, msg);
132 __attribute__((format(printf, 5, 6)))
133 void logger(struct crypt_device *cd, int level, const char *file,
134 int line, const char *format, ...)
139 va_start(argp, format);
141 if (vasprintf(&target, format, argp) > 0 ) {
143 crypt_log(cd, level, target);
145 } else if (_debug_level)
146 printf("# %s:%d %s\n", file ?: "?", line, target);
148 } else if (_debug_level)
149 printf("# %s\n", target);
157 static const char *mdata_device(struct crypt_device *cd)
159 return cd->metadata_device ?: cd->device;
162 static int init_crypto(struct crypt_device *ctx)
166 crypt_fips_libcryptsetup_check(ctx);
168 r = crypt_random_init(ctx);
170 log_err(ctx, _("Cannot initialize crypto RNG backend.\n"));
174 r = crypt_backend_init(ctx);
176 log_err(ctx, _("Cannot initialize crypto backend.\n"));
178 log_dbg("Crypto backend (%s) initialized.", crypt_backend_version());
182 static int process_key(struct crypt_device *cd, const char *hash_name,
183 size_t key_size, const char *pass, size_t passLen,
184 struct volume_key **vk)
191 *vk = crypt_alloc_volume_key(key_size, NULL);
196 r = crypt_plain_hash(cd, hash_name, (*vk)->key, key_size, pass, passLen);
199 log_err(cd, _("Hash algorithm %s not supported.\n"),
202 log_err(cd, _("Key processing error (using hash %s).\n"),
204 crypt_free_volume_key(*vk);
208 } else if (passLen > key_size) {
209 memcpy((*vk)->key, pass, key_size);
211 memcpy((*vk)->key, pass, passLen);
217 static int isPLAIN(const char *type)
219 return (type && !strcmp(CRYPT_PLAIN, type));
222 static int isLUKS(const char *type)
224 return (type && !strcmp(CRYPT_LUKS1, type));
227 static int isLOOPAES(const char *type)
229 return (type && !strcmp(CRYPT_LOOPAES, type));
232 static int isVERITY(const char *type)
234 return (type && !strcmp(CRYPT_VERITY, type));
237 /* keyslot helpers */
238 static int keyslot_verify_or_find_empty(struct crypt_device *cd, int *keyslot)
240 if (*keyslot == CRYPT_ANY_SLOT) {
241 *keyslot = LUKS_keyslot_find_empty(&cd->hdr);
243 log_err(cd, _("All key slots full.\n"));
248 switch (LUKS_keyslot_info(&cd->hdr, *keyslot)) {
249 case CRYPT_SLOT_INVALID:
250 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
251 *keyslot, LUKS_NUMKEYS - 1);
253 case CRYPT_SLOT_INACTIVE:
256 log_err(cd, _("Key slot %d is full, please select another one.\n"),
265 * compares UUIDs returned by device-mapper (striped by cryptsetup) and uuid in header
267 static int crypt_uuid_cmp(const char *dm_uuid, const char *hdr_uuid)
272 if (!dm_uuid || !hdr_uuid)
275 str = strchr(dm_uuid, '-');
279 for (i = 0, j = 1; hdr_uuid[i]; i++) {
280 if (hdr_uuid[i] == '-')
283 if (!str[j] || str[j] == '-')
286 if (str[j] != hdr_uuid[i])
294 int PLAIN_activate(struct crypt_device *cd,
296 struct volume_key *vk,
301 char *dm_cipher = NULL;
302 enum devcheck device_check;
303 struct crypt_dm_active_device dmd = {
305 .uuid = crypt_get_uuid(cd),
309 .device = crypt_get_device_name(cd),
312 .offset = crypt_get_data_offset(cd),
313 .iv_offset = crypt_get_iv_offset(cd),
317 if (dmd.flags & CRYPT_ACTIVATE_SHARED)
318 device_check = DEV_SHARED;
320 device_check = DEV_EXCL;
322 r = device_check_and_adjust(cd, dmd.u.crypt.device, device_check,
323 &dmd.size, &dmd.u.crypt.offset, &dmd.flags);
327 if (crypt_get_cipher_mode(cd))
328 r = asprintf(&dm_cipher, "%s-%s", crypt_get_cipher(cd), crypt_get_cipher_mode(cd));
330 r = asprintf(&dm_cipher, "%s", crypt_get_cipher(cd));
334 dmd.u.crypt.cipher = dm_cipher;
335 log_dbg("Trying to activate PLAIN device %s using cipher %s.",
336 name, dmd.u.crypt.cipher);
338 r = dm_create_device(name, CRYPT_PLAIN, &dmd, NULL, 0);
341 if (!cd->plain_uuid && dm_query_device(name, DM_ACTIVE_UUID, &dmd) >= 0)
342 cd->plain_uuid = CONST_CAST(char*)dmd.uuid;
348 int crypt_confirm(struct crypt_device *cd, const char *msg)
350 if (!cd || !cd->confirm)
353 return cd->confirm(msg, cd->confirm_usrptr);
356 static int key_from_terminal(struct crypt_device *cd, char *msg, char **key,
357 size_t *key_len, int force_verify)
363 if(!msg && asprintf(&prompt, _("Enter passphrase for %s: "),
364 cd->backing_file ?: crypt_get_device_name(cd)) < 0)
371 *key = crypt_safe_alloc(DEFAULT_PASSPHRASE_SIZE_MAX);
376 r = cd->password(msg, *key, DEFAULT_PASSPHRASE_SIZE_MAX,
377 cd->password_usrptr);
379 crypt_safe_free(*key);
384 r = crypt_get_key(msg, key, key_len, 0, 0, NULL, cd->timeout,
385 (force_verify || cd->password_verify), cd);
388 return (r < 0) ? r: 0;
391 static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslot,
392 struct volume_key **vk)
394 char *passphrase_read = NULL;
395 size_t passphrase_size_read;
396 int r = -EINVAL, eperm = 0, tries = cd->tries;
400 crypt_free_volume_key(*vk);
403 r = key_from_terminal(cd, NULL, &passphrase_read,
404 &passphrase_size_read, 0);
405 /* Continue if it is just passphrase verify mismatch */
411 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase_read,
412 passphrase_size_read, &cd->hdr, vk, cd);
415 crypt_safe_free(passphrase_read);
416 passphrase_read = NULL;
417 } while (r == -EPERM && (--tries > 0));
420 crypt_free_volume_key(*vk);
423 /* Report wrong passphrase if at least one try failed */
424 if (eperm && r == -EPIPE)
428 crypt_safe_free(passphrase_read);
432 static int key_from_file(struct crypt_device *cd, char *msg,
433 char **key, size_t *key_len,
434 const char *key_file, size_t key_offset,
437 return crypt_get_key(msg, key, key_len, key_offset, key_size, key_file,
441 void crypt_set_log_callback(struct crypt_device *cd,
442 void (*log)(int level, const char *msg, void *usrptr),
449 cd->log_usrptr = usrptr;
453 void crypt_set_confirm_callback(struct crypt_device *cd,
454 int (*confirm)(const char *msg, void *usrptr),
457 cd->confirm = confirm;
458 cd->confirm_usrptr = usrptr;
461 void crypt_set_password_callback(struct crypt_device *cd,
462 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
465 cd->password = password;
466 cd->password_usrptr = usrptr;
469 static void _get_error(char *error, char *buf, size_t size)
471 if (!buf || size < 1)
474 strncpy(buf, error, size - 1);
475 buf[size - 1] = '\0';
481 void crypt_last_error(struct crypt_device *cd, char *buf, size_t size)
484 return _get_error(cd->error, buf, size);
487 /* Deprecated global error interface */
488 void crypt_get_error(char *buf, size_t size)
490 return _get_error(global_error, buf, size);
493 const char *crypt_get_dir(void)
498 int crypt_init(struct crypt_device **cd, const char *device)
500 struct crypt_device *h = NULL;
506 log_dbg("Allocating crypt device %s context.", device);
508 if (!(h = malloc(sizeof(struct crypt_device))))
511 memset(h, 0, sizeof(*h));
515 r = device_ready(NULL, device, O_RDONLY);
517 h->device = crypt_loop_get_device();
518 log_dbg("Not a block device, %s%s.",
519 h->device ? "using free loop device " :
520 "no free loop device found",
523 log_err(NULL, _("Cannot find a free loopback device.\n"));
528 /* Keep the loop open, dettached on last close. */
529 h->loop_fd = crypt_loop_attach(h->device, device, 0, 1, &readonly);
530 if (h->loop_fd == -1) {
531 log_err(NULL, _("Attaching loopback device failed "
532 "(loop device with autoclear flag is required).\n"));
537 h->backing_file = crypt_loop_backing_file(h->device);
538 r = device_ready(NULL, h->device, O_RDONLY);
546 if (!h->device && device && !(h->device = strdup(device))) {
551 if (dm_init(h, 1) < 0) {
556 h->iteration_time = 1000;
557 h->password_verify = 0;
559 h->rng_type = crypt_random_default_key_rng();
565 if (h->loop_fd != -1)
568 free(h->backing_file);
574 static int crypt_check_data_device_size(struct crypt_device *cd)
577 uint64_t size, size_min;
579 /* Check data device size, require at least one sector */
580 size_min = crypt_get_data_offset(cd) << SECTOR_SHIFT ?: SECTOR_SIZE;
582 r = device_size(crypt_get_device_name(cd), &size);
586 if (size < size_min) {
587 log_err(cd, _("LUKS header detected but device %s is too small.\n"),
588 crypt_get_device_name(cd));
595 int crypt_set_data_device(struct crypt_device *cd, const char *device)
600 log_dbg("Setting ciphertext data device to %s.", device ?: "(none)");
602 if (!isLUKS(cd->type) && !isVERITY(cd->type)) {
603 log_err(cd, _("This operation is not supported for this device type.\n"));
607 /* metadata device must be set */
608 if (!cd->device || !device)
611 r = device_ready(NULL, device, O_RDONLY);
615 if (!(data_device = strdup(device)))
618 if (!cd->metadata_device)
619 cd->metadata_device = cd->device;
623 cd->device = data_device;
625 return crypt_check_data_device_size(cd);
628 static int _crypt_load_luks1(struct crypt_device *cd, int require_header, int repair)
630 struct luks_phdr hdr;
637 r = LUKS_read_phdr(mdata_device(cd), &hdr, require_header, repair, cd);
641 if (!cd->type && !(cd->type = strdup(CRYPT_LUKS1)))
644 memcpy(&cd->hdr, &hdr, sizeof(hdr));
649 static int _crypt_load_verity(struct crypt_device *cd, struct crypt_params_verity *params)
652 size_t sb_offset = 0;
659 sb_offset = params->hash_area_offset;
661 r = VERITY_read_sb(cd, mdata_device(cd), sb_offset, &cd->verity_hdr);
666 cd->verity_flags = params->flags;
668 if (params && params->data_device &&
669 (r = crypt_set_data_device(cd, params->data_device)) < 0)
672 cd->verity_root_hash_size = crypt_hash_size(cd->verity_hdr.hash_name);
674 if (!cd->type && !(cd->type = strdup(CRYPT_VERITY)))
680 int crypt_init_by_name_and_header(struct crypt_device **cd,
682 const char *header_device)
684 crypt_status_info ci;
685 struct crypt_dm_active_device dmd;
686 char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
690 log_dbg("Allocating crypt device context by device %s.", name);
692 ci = crypt_status(NULL, name);
693 if (ci == CRYPT_INVALID)
696 if (ci < CRYPT_ACTIVE) {
697 log_err(NULL, _("Device %s is not active.\n"), name);
701 r = dm_query_device(name, DM_ACTIVE_DEVICE | DM_ACTIVE_CIPHER |
702 DM_ACTIVE_UUID | DM_ACTIVE_KEYSIZE, &dmd);
709 r = crypt_init(cd, header_device);
711 r = crypt_init(cd, dmd.u.crypt.device);
713 /* Underlying device disappeared but mapping still active */
714 if (!dmd.u.crypt.device || r == -ENOTBLK)
715 log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
718 /* Underlying device is not readable but crypt mapping exists */
720 free(CONST_CAST(void*)dmd.u.crypt.device);
721 dmd.u.crypt.device = NULL;
722 r = crypt_init(cd, NULL);
730 if (!strncmp(CRYPT_PLAIN, dmd.uuid, sizeof(CRYPT_PLAIN)-1))
731 (*cd)->type = strdup(CRYPT_PLAIN);
732 else if (!strncmp(CRYPT_LOOPAES, dmd.uuid, sizeof(CRYPT_LOOPAES)-1))
733 (*cd)->type = strdup(CRYPT_LOOPAES);
734 else if (!strncmp(CRYPT_LUKS1, dmd.uuid, sizeof(CRYPT_LUKS1)-1))
735 (*cd)->type = strdup(CRYPT_LUKS1);
737 log_dbg("Unknown UUID set, some parameters are not set.");
739 log_dbg("Active device has no UUID set, some parameters are not set.");
742 r = crypt_set_data_device(*cd, dmd.u.crypt.device);
747 /* Try to initialise basic parameters from active device */
749 if (!(*cd)->backing_file && dmd.u.crypt.device &&
750 crypt_loop_device(dmd.u.crypt.device) &&
751 !((*cd)->backing_file = crypt_loop_backing_file(dmd.u.crypt.device))) {
756 if (isPLAIN((*cd)->type)) {
757 (*cd)->plain_uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
758 (*cd)->plain_hdr.hash = NULL; /* no way to get this */
759 (*cd)->plain_hdr.offset = dmd.u.crypt.offset;
760 (*cd)->plain_hdr.skip = dmd.u.crypt.iv_offset;
761 (*cd)->plain_key_size = dmd.u.crypt.vk->keylength;
763 r = crypt_parse_name_and_mode(dmd.u.crypt.cipher, cipher, NULL, cipher_mode);
765 (*cd)->plain_cipher = strdup(cipher);
766 (*cd)->plain_cipher_mode = strdup(cipher_mode);
768 } else if (isLOOPAES((*cd)->type)) {
769 (*cd)->loopaes_uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
770 (*cd)->loopaes_hdr.offset = dmd.u.crypt.offset;
772 r = crypt_parse_name_and_mode(dmd.u.crypt.cipher, cipher,
773 &key_nums, cipher_mode);
775 (*cd)->loopaes_cipher = strdup(cipher);
776 (*cd)->loopaes_cipher_mode = strdup(cipher_mode);
777 /* version 3 uses last key for IV */
778 if (dmd.u.crypt.vk->keylength % key_nums)
780 (*cd)->loopaes_key_size = dmd.u.crypt.vk->keylength / key_nums;
782 } else if (isLUKS((*cd)->type)) {
783 if (mdata_device(*cd)) {
784 r = _crypt_load_luks1(*cd, 0, 0);
786 log_dbg("LUKS device header does not match active device.");
792 /* checks whether UUIDs match each other */
793 r = crypt_uuid_cmp(dmd.uuid, (*cd)->hdr.uuid);
795 log_dbg("LUKS device header uuid: %s mismatches DM returned uuid %s",
796 (*cd)->hdr.uuid, dmd.uuid);
810 crypt_free_volume_key(dmd.u.crypt.vk);
811 free(CONST_CAST(void*)dmd.u.crypt.device);
812 free(CONST_CAST(void*)dmd.u.crypt.cipher);
813 free(CONST_CAST(void*)dmd.uuid);
817 int crypt_init_by_name(struct crypt_device **cd, const char *name)
819 return crypt_init_by_name_and_header(cd, name, NULL);
822 static int _crypt_format_plain(struct crypt_device *cd,
824 const char *cipher_mode,
826 size_t volume_key_size,
827 struct crypt_params_plain *params)
829 if (!cipher || !cipher_mode) {
830 log_err(cd, _("Invalid plain crypt parameters.\n"));
834 if (volume_key_size > 1024) {
835 log_err(cd, _("Invalid key size.\n"));
839 cd->plain_key_size = volume_key_size;
840 cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
844 cd->plain_cipher = strdup(cipher);
845 cd->plain_cipher_mode = strdup(cipher_mode);
848 cd->plain_uuid = strdup(uuid);
850 if (params && params->hash)
851 cd->plain_hdr.hash = strdup(params->hash);
853 cd->plain_hdr.offset = params ? params->offset : 0;
854 cd->plain_hdr.skip = params ? params->skip : 0;
855 cd->plain_hdr.size = params ? params->size : 0;
857 if (!cd->plain_cipher || !cd->plain_cipher_mode)
863 static int _crypt_format_luks1(struct crypt_device *cd,
865 const char *cipher_mode,
867 const char *volume_key,
868 size_t volume_key_size,
869 struct crypt_params_luks1 *params)
872 unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
873 unsigned long alignment_offset = 0;
875 if (!mdata_device(cd)) {
876 log_err(cd, _("Can't format LUKS without device.\n"));
881 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
884 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
889 if (params && params->data_device) {
890 cd->metadata_device = cd->device;
891 if (!(cd->device = strdup(params->data_device)))
893 required_alignment = params->data_alignment * SECTOR_SIZE;
894 } else if (params && params->data_alignment) {
895 required_alignment = params->data_alignment * SECTOR_SIZE;
897 get_topology_alignment(cd->device, &required_alignment,
898 &alignment_offset, DEFAULT_DISK_ALIGNMENT);
900 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
901 (params && params->hash) ? params->hash : "sha1",
903 required_alignment / SECTOR_SIZE,
904 alignment_offset / SECTOR_SIZE,
905 cd->iteration_time, &cd->PBKDF2_per_sec,
906 cd->metadata_device, cd);
910 /* Wipe first 8 sectors - fs magic numbers etc. */
911 r = crypt_wipe(mdata_device(cd), 0, 8 * SECTOR_SIZE, CRYPT_WIPE_ZERO, 1);
914 log_err(cd, _("Cannot format device %s which is still in use.\n"),
917 log_err(cd, _("Cannot wipe header on device %s.\n"),
923 r = LUKS_write_phdr(mdata_device(cd), &cd->hdr, cd);
928 static int _crypt_format_loopaes(struct crypt_device *cd,
931 size_t volume_key_size,
932 struct crypt_params_loopaes *params)
934 if (!mdata_device(cd)) {
935 log_err(cd, _("Can't format LOOPAES without device.\n"));
939 if (volume_key_size > 1024) {
940 log_err(cd, _("Invalid key size.\n"));
944 cd->loopaes_key_size = volume_key_size;
946 cd->loopaes_cipher = strdup(cipher ?: DEFAULT_LOOPAES_CIPHER);
949 cd->loopaes_uuid = strdup(uuid);
951 if (params && params->hash)
952 cd->loopaes_hdr.hash = strdup(params->hash);
954 cd->loopaes_hdr.offset = params ? params->offset : 0;
955 cd->loopaes_hdr.skip = params ? params->skip : 0;
960 static int _crypt_format_verity(struct crypt_device *cd,
961 struct crypt_params_verity *params)
964 uint64_t data_device_size;
966 if (!mdata_device(cd)) {
967 log_err(cd, _("Can't format VERITY without device.\n"));
971 if (!params || !params->data_device)
974 if (params->version > 1)
978 cd->type = CRYPT_VERITY;
979 r = crypt_set_data_device(cd, params->data_device);
983 if (!params->data_size) {
984 r = device_size(params->data_device, &data_device_size);
988 cd->verity_hdr.data_size = data_device_size / params->data_block_size;
990 cd->verity_hdr.data_size = params->data_size;
993 cd->verity_root_hash_size = crypt_hash_size(params->hash_name);
994 if (!cd->verity_root_hash_size)
997 cd->verity_flags = params->flags;
998 cd->verity_root_hash = malloc(cd->verity_root_hash_size);
999 if (!cd->verity_root_hash)
1002 cd->verity_hdr.hash_name = strdup(params->hash_name);
1003 cd->verity_hdr.data_device = NULL;
1004 cd->verity_hdr.data_block_size = params->data_block_size;
1005 cd->verity_hdr.hash_block_size = params->hash_block_size;
1006 cd->verity_hdr.hash_area_offset = params->hash_area_offset;
1007 cd->verity_hdr.version = params->version;
1008 cd->verity_hdr.flags = params->flags;
1009 cd->verity_hdr.salt_size = params->salt_size;
1010 cd->verity_hdr.salt = malloc(params->salt_size);
1012 memcpy(CONST_CAST(char*)cd->verity_hdr.salt, params->salt,
1015 r = crypt_random_get(cd, CONST_CAST(char*)cd->verity_hdr.salt,
1016 params->salt_size, CRYPT_RND_SALT);
1020 log_dbg("Creating verity hash on device %s.", mdata_device(cd));
1021 r = VERITY_create(cd, &cd->verity_hdr, cd->device, mdata_device(cd),
1022 cd->verity_root_hash, cd->verity_root_hash_size);
1026 r = VERITY_write_sb(cd, mdata_device(cd),
1027 cd->verity_hdr.hash_area_offset,
1031 free(cd->verity_root_hash);
1032 free(CONST_CAST(char*)cd->verity_hdr.hash_name);
1033 free(CONST_CAST(char*)cd->verity_hdr.salt);
1039 int crypt_format(struct crypt_device *cd,
1042 const char *cipher_mode,
1044 const char *volume_key,
1045 size_t volume_key_size,
1054 log_dbg("Context already formatted as %s.", cd->type);
1058 log_dbg("Formatting device %s as type %s.", mdata_device(cd) ?: "(none)", type);
1060 r = init_crypto(cd);
1065 r = _crypt_format_plain(cd, cipher, cipher_mode,
1066 uuid, volume_key_size, params);
1067 else if (isLUKS(type))
1068 r = _crypt_format_luks1(cd, cipher, cipher_mode,
1069 uuid, volume_key, volume_key_size, params);
1070 else if (isLOOPAES(type))
1071 r = _crypt_format_loopaes(cd, cipher, uuid, volume_key_size, params);
1072 else if (isVERITY(type))
1073 r = _crypt_format_verity(cd, params);
1075 /* FIXME: allow plugins here? */
1076 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
1080 if (!r && !(cd->type = strdup(type)))
1084 crypt_free_volume_key(cd->volume_key);
1085 cd->volume_key = NULL;
1091 int crypt_load(struct crypt_device *cd,
1092 const char *requested_type,
1097 log_dbg("Trying to load %s crypt type from device %s.",
1098 requested_type ?: "any", mdata_device(cd) ?: "(none)");
1100 if (!mdata_device(cd))
1103 if (!requested_type || isLUKS(requested_type)) {
1104 if (cd->type && !isLUKS(cd->type)) {
1105 log_dbg("Context is already initialised to type %s", cd->type);
1109 r = _crypt_load_luks1(cd, 1, 0);
1110 } else if (isVERITY(requested_type)) {
1111 if (cd->type && !isVERITY(cd->type)) {
1112 log_dbg("Context is already initialised to type %s", cd->type);
1115 r = _crypt_load_verity(cd, params);
1122 /* cd->type and header must be set in context */
1123 r = crypt_check_data_device_size(cd);
1132 int crypt_repair(struct crypt_device *cd,
1133 const char *requested_type,
1134 void *params __attribute__((unused)))
1138 log_dbg("Trying to repair %s crypt type from device %s.",
1139 requested_type ?: "any", mdata_device(cd) ?: "(none)");
1141 if (!mdata_device(cd))
1144 if (requested_type && !isLUKS(requested_type))
1148 /* Load with repair */
1149 r = _crypt_load_luks1(cd, 1, 1);
1153 /* cd->type and header must be set in context */
1154 r = crypt_check_data_device_size(cd);
1163 int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
1165 struct crypt_dm_active_device dmd;
1168 /* Device context type must be initialised */
1169 if (!cd->type || !crypt_get_uuid(cd))
1172 log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
1174 r = dm_query_device(name, DM_ACTIVE_DEVICE | DM_ACTIVE_CIPHER |
1175 DM_ACTIVE_UUID | DM_ACTIVE_KEYSIZE |
1176 DM_ACTIVE_KEY, &dmd);
1178 log_err(NULL, _("Device %s is not active.\n"), name);
1187 r = device_check_and_adjust(cd, dmd.u.crypt.device, DEV_OK, &new_size,
1188 &dmd.u.crypt.offset, &dmd.flags);
1192 if (new_size == dmd.size) {
1193 log_dbg("Device has already requested size %" PRIu64
1194 " sectors.", dmd.size);
1197 dmd.size = new_size;
1198 r = dm_create_device(name, cd->type, &dmd, NULL, 1);
1201 crypt_free_volume_key(dmd.u.crypt.vk);
1202 free(CONST_CAST(void*)dmd.u.crypt.cipher);
1203 free(CONST_CAST(void*)dmd.u.crypt.device);
1204 free(CONST_CAST(void*)dmd.uuid);
1209 int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
1211 if (!isLUKS(cd->type)) {
1212 log_err(cd, _("This operation is not supported for this device type.\n"));
1216 if (uuid && !strncmp(uuid, cd->hdr.uuid, sizeof(cd->hdr.uuid))) {
1217 log_dbg("UUID is the same as requested (%s) for device %s.",
1218 uuid, mdata_device(cd));
1223 log_dbg("Requested new UUID change to %s for %s.", uuid, mdata_device(cd));
1225 log_dbg("Requested new UUID refresh for %s.", mdata_device(cd));
1227 if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
1230 return LUKS_hdr_uuid_set(mdata_device(cd), &cd->hdr, uuid, cd);
1233 int crypt_header_backup(struct crypt_device *cd,
1234 const char *requested_type,
1235 const char *backup_file)
1239 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1242 r = init_crypto(cd);
1246 log_dbg("Requested header backup of device %s (%s) to "
1247 "file %s.", mdata_device(cd), requested_type, backup_file);
1249 return LUKS_hdr_backup(backup_file, mdata_device(cd), &cd->hdr, cd);
1252 int crypt_header_restore(struct crypt_device *cd,
1253 const char *requested_type,
1254 const char *backup_file)
1258 if (requested_type && !isLUKS(requested_type))
1261 /* Some hash functions need initialized gcrypt library */
1262 r = init_crypto(cd);
1266 log_dbg("Requested header restore to device %s (%s) from "
1267 "file %s.", mdata_device(cd), requested_type, backup_file);
1269 return LUKS_hdr_restore(backup_file, mdata_device(cd), &cd->hdr, cd);
1272 void crypt_free(struct crypt_device *cd)
1275 log_dbg("Releasing crypt device %s context.", mdata_device(cd));
1277 if (cd->loop_fd != -1)
1281 crypt_free_volume_key(cd->volume_key);
1284 free(cd->metadata_device);
1285 free(cd->backing_file);
1288 /* used in plain device only */
1289 free(CONST_CAST(void*)cd->plain_hdr.hash);
1290 free(cd->plain_cipher);
1291 free(cd->plain_cipher_mode);
1292 free(cd->plain_uuid);
1294 /* used in loop-AES device only */
1295 free(CONST_CAST(void*)cd->loopaes_hdr.hash);
1296 free(cd->loopaes_cipher);
1297 free(cd->loopaes_uuid);
1299 /* used in verity device only */
1300 free(CONST_CAST(void*)cd->verity_hdr.hash_name);
1301 free(CONST_CAST(void*)cd->verity_hdr.salt);
1302 free(cd->verity_root_hash);
1308 int crypt_suspend(struct crypt_device *cd,
1311 crypt_status_info ci;
1314 log_dbg("Suspending volume %s.", name);
1316 if (!isLUKS(cd->type)) {
1317 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1322 ci = crypt_status(NULL, name);
1323 if (ci < CRYPT_ACTIVE) {
1324 log_err(cd, _("Volume %s is not active.\n"), name);
1328 if (!cd && dm_init(NULL, 1) < 0)
1331 r = dm_status_suspended(name);
1336 log_err(cd, _("Volume %s is already suspended.\n"), name);
1341 r = dm_suspend_and_wipe_key(name);
1343 log_err(cd, "Suspend is not supported for device %s.\n", name);
1345 log_err(cd, "Error during suspending device %s.\n", name);
1352 int crypt_resume_by_passphrase(struct crypt_device *cd,
1355 const char *passphrase,
1356 size_t passphrase_size)
1358 struct volume_key *vk = NULL;
1361 log_dbg("Resuming volume %s.", name);
1363 if (!isLUKS(cd->type)) {
1364 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1369 r = dm_status_suspended(name);
1374 log_err(cd, _("Volume %s is not suspended.\n"), name);
1379 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase,
1380 passphrase_size, &cd->hdr, &vk, cd);
1382 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1386 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1388 log_err(cd, "Resume is not supported for device %s.\n", name);
1390 log_err(cd, "Error during resuming device %s.\n", name);
1394 crypt_free_volume_key(vk);
1395 return r < 0 ? r : keyslot;
1398 int crypt_resume_by_keyfile_offset(struct crypt_device *cd,
1401 const char *keyfile,
1402 size_t keyfile_size,
1403 size_t keyfile_offset)
1405 struct volume_key *vk = NULL;
1406 char *passphrase_read = NULL;
1407 size_t passphrase_size_read;
1410 log_dbg("Resuming volume %s.", name);
1412 if (!isLUKS(cd->type)) {
1413 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1418 r = dm_status_suspended(name);
1423 log_err(cd, _("Volume %s is not suspended.\n"), name);
1430 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1431 &passphrase_size_read, keyfile, keyfile_offset,
1436 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase_read,
1437 passphrase_size_read, &cd->hdr, &vk, cd);
1442 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1444 log_err(cd, "Error during resuming device %s.\n", name);
1446 crypt_safe_free(passphrase_read);
1447 crypt_free_volume_key(vk);
1448 return r < 0 ? r : keyslot;
1451 int crypt_resume_by_keyfile(struct crypt_device *cd,
1454 const char *keyfile,
1455 size_t keyfile_size)
1457 return crypt_resume_by_keyfile_offset(cd, name, keyslot,
1458 keyfile, keyfile_size, 0);
1461 // slot manipulation
1462 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1463 int keyslot, // -1 any
1464 const char *passphrase, // NULL -> terminal
1465 size_t passphrase_size,
1466 const char *new_passphrase, // NULL -> terminal
1467 size_t new_passphrase_size)
1469 struct volume_key *vk = NULL;
1470 char *password = NULL, *new_password = NULL;
1471 size_t passwordLen, new_passwordLen;
1474 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1475 "new passphrase %sprovided.",
1476 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1478 if (!isLUKS(cd->type)) {
1479 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1483 r = keyslot_verify_or_find_empty(cd, &keyslot);
1487 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1488 /* No slots used, try to use pre-generated key in header */
1489 if (cd->volume_key) {
1490 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1491 r = vk ? 0 : -ENOMEM;
1493 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1496 } else if (passphrase) {
1497 /* Passphrase provided, use it to unlock existing keyslot */
1498 r = LUKS_open_key_with_hdr(mdata_device(cd), CRYPT_ANY_SLOT, passphrase,
1499 passphrase_size, &cd->hdr, &vk, cd);
1501 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1502 r = key_from_terminal(cd, _("Enter any passphrase: "),
1503 &password, &passwordLen, 0);
1507 r = LUKS_open_key_with_hdr(mdata_device(cd), CRYPT_ANY_SLOT, password,
1508 passwordLen, &cd->hdr, &vk, cd);
1509 crypt_safe_free(password);
1515 if (new_passphrase) {
1516 new_password = CONST_CAST(char*)new_passphrase;
1517 new_passwordLen = new_passphrase_size;
1519 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1520 &new_password, &new_passwordLen, 1);
1525 r = LUKS_set_key(mdata_device(cd), keyslot, new_password, new_passwordLen,
1526 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1531 if (!new_passphrase)
1532 crypt_safe_free(new_password);
1533 crypt_free_volume_key(vk);
1534 return r ?: keyslot;
1537 int crypt_keyslot_add_by_keyfile_offset(struct crypt_device *cd,
1539 const char *keyfile,
1540 size_t keyfile_size,
1541 size_t keyfile_offset,
1542 const char *new_keyfile,
1543 size_t new_keyfile_size,
1544 size_t new_keyfile_offset)
1546 struct volume_key *vk = NULL;
1547 char *password = NULL; size_t passwordLen;
1548 char *new_password = NULL; size_t new_passwordLen;
1551 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1552 keyfile ?: "[none]", new_keyfile ?: "[none]");
1554 if (!isLUKS(cd->type)) {
1555 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1559 r = keyslot_verify_or_find_empty(cd, &keyslot);
1563 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1564 /* No slots used, try to use pre-generated key in header */
1565 if (cd->volume_key) {
1566 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1567 r = vk ? 0 : -ENOMEM;
1569 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1573 /* Read password from file of (if NULL) from terminal */
1575 r = key_from_file(cd, _("Enter any passphrase: "),
1576 &password, &passwordLen,
1577 keyfile, keyfile_offset, keyfile_size);
1579 r = key_from_terminal(cd, _("Enter any passphrase: "),
1580 &password, &passwordLen, 0);
1584 r = LUKS_open_key_with_hdr(mdata_device(cd), CRYPT_ANY_SLOT, password, passwordLen,
1592 r = key_from_file(cd, _("Enter new passphrase for key slot: "),
1593 &new_password, &new_passwordLen, new_keyfile,
1594 new_keyfile_offset, new_keyfile_size);
1596 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1597 &new_password, &new_passwordLen, 1);
1601 r = LUKS_set_key(mdata_device(cd), keyslot, new_password, new_passwordLen,
1602 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1604 crypt_safe_free(password);
1605 crypt_safe_free(new_password);
1606 crypt_free_volume_key(vk);
1607 return r < 0 ? r : keyslot;
1610 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1612 const char *keyfile,
1613 size_t keyfile_size,
1614 const char *new_keyfile,
1615 size_t new_keyfile_size)
1617 return crypt_keyslot_add_by_keyfile_offset(cd, keyslot,
1618 keyfile, keyfile_size, 0,
1619 new_keyfile, new_keyfile_size, 0);
1622 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1624 const char *volume_key,
1625 size_t volume_key_size,
1626 const char *passphrase,
1627 size_t passphrase_size)
1629 struct volume_key *vk = NULL;
1631 char *new_password = NULL; size_t new_passwordLen;
1633 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1635 if (!isLUKS(cd->type)) {
1636 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1641 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1642 else if (cd->volume_key)
1643 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1648 r = LUKS_verify_volume_key(&cd->hdr, vk);
1650 log_err(cd, _("Volume key does not match the volume.\n"));
1654 r = keyslot_verify_or_find_empty(cd, &keyslot);
1659 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1660 &new_password, &new_passwordLen, 1);
1663 passphrase = new_password;
1664 passphrase_size = new_passwordLen;
1667 r = LUKS_set_key(mdata_device(cd), keyslot, passphrase, passphrase_size,
1668 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1670 crypt_safe_free(new_password);
1671 crypt_free_volume_key(vk);
1672 return (r < 0) ? r : keyslot;
1675 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1677 crypt_keyslot_info ki;
1679 log_dbg("Destroying keyslot %d.", keyslot);
1681 if (!isLUKS(cd->type)) {
1682 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1686 ki = crypt_keyslot_status(cd, keyslot);
1687 if (ki == CRYPT_SLOT_INVALID) {
1688 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1692 if (ki == CRYPT_SLOT_INACTIVE) {
1693 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1697 return LUKS_del_key(mdata_device(cd), keyslot, &cd->hdr, cd);
1700 // activation/deactivation of device mapping
1701 int crypt_activate_by_passphrase(struct crypt_device *cd,
1704 const char *passphrase,
1705 size_t passphrase_size,
1708 crypt_status_info ci;
1709 struct volume_key *vk = NULL;
1710 char *read_passphrase = NULL;
1711 size_t passphraseLen = 0;
1714 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1715 name ? "Activating" : "Checking", name ?: "",
1716 keyslot, passphrase ? "" : "[none] ");
1719 ci = crypt_status(NULL, name);
1720 if (ci == CRYPT_INVALID)
1722 else if (ci >= CRYPT_ACTIVE) {
1723 log_err(cd, _("Device %s already exists.\n"), name);
1728 /* plain, use hashed passphrase */
1729 if (isPLAIN(cd->type)) {
1734 r = key_from_terminal(cd, NULL, &read_passphrase,
1738 passphrase = read_passphrase;
1739 passphrase_size = passphraseLen;
1742 r = process_key(cd, cd->plain_hdr.hash,
1744 passphrase, passphrase_size, &vk);
1748 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1750 } else if (isLUKS(cd->type)) {
1751 /* provided passphrase, do not retry */
1753 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase,
1754 passphrase_size, &cd->hdr, &vk, cd);
1756 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1761 r = LUKS1_activate(cd, name, vk, flags);
1766 crypt_safe_free(read_passphrase);
1767 crypt_free_volume_key(vk);
1769 return r < 0 ? r : keyslot;
1772 int crypt_activate_by_keyfile_offset(struct crypt_device *cd,
1775 const char *keyfile,
1776 size_t keyfile_size,
1777 size_t keyfile_offset,
1780 crypt_status_info ci;
1781 struct volume_key *vk = NULL;
1782 char *passphrase_read = NULL;
1783 size_t passphrase_size_read;
1784 unsigned int key_count = 0;
1787 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1788 name ?: "", keyslot, keyfile ?: "[none]");
1791 ci = crypt_status(NULL, name);
1792 if (ci == CRYPT_INVALID)
1794 else if (ci >= CRYPT_ACTIVE) {
1795 log_err(cd, _("Device %s already exists.\n"), name);
1803 if (isPLAIN(cd->type)) {
1807 r = key_from_file(cd, _("Enter passphrase: "),
1808 &passphrase_read, &passphrase_size_read,
1809 keyfile, keyfile_offset, keyfile_size);
1813 r = process_key(cd, cd->plain_hdr.hash,
1815 passphrase_read, passphrase_size_read, &vk);
1819 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1820 } else if (isLUKS(cd->type)) {
1821 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1822 &passphrase_size_read, keyfile, keyfile_offset, keyfile_size);
1825 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase_read,
1826 passphrase_size_read, &cd->hdr, &vk, cd);
1832 r = LUKS1_activate(cd, name, vk, flags);
1837 } else if (isLOOPAES(cd->type)) {
1838 r = key_from_file(cd, NULL, &passphrase_read, &passphrase_size_read,
1839 keyfile, keyfile_offset, keyfile_size);
1842 r = LOOPAES_parse_keyfile(cd, &vk, cd->loopaes_hdr.hash, &key_count,
1843 passphrase_read, passphrase_size_read);
1847 r = LOOPAES_activate(cd, name, cd->loopaes_cipher,
1848 key_count, vk, flags);
1853 crypt_safe_free(passphrase_read);
1854 crypt_free_volume_key(vk);
1859 int crypt_activate_by_keyfile(struct crypt_device *cd,
1862 const char *keyfile,
1863 size_t keyfile_size,
1866 return crypt_activate_by_keyfile_offset(cd, name, keyslot, keyfile,
1867 keyfile_size, 0, flags);
1870 int crypt_activate_by_volume_key(struct crypt_device *cd,
1872 const char *volume_key,
1873 size_t volume_key_size,
1876 crypt_status_info ci;
1877 struct volume_key *vk = NULL;
1880 log_dbg("Activating volume %s by volume key.", name);
1883 ci = crypt_status(NULL, name);
1884 if (ci == CRYPT_INVALID)
1886 else if (ci >= CRYPT_ACTIVE) {
1887 log_err(cd, _("Device %s already exists.\n"), name);
1892 /* use key directly, no hash */
1893 if (isPLAIN(cd->type)) {
1897 if (!volume_key || !volume_key_size || volume_key_size != cd->plain_key_size) {
1898 log_err(cd, _("Incorrect volume key specified for plain device.\n"));
1902 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1906 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1907 } else if (isLUKS(cd->type)) {
1908 /* If key is not provided, try to use internal key */
1910 if (!cd->volume_key) {
1911 log_err(cd, _("Volume key does not match the volume.\n"));
1914 volume_key_size = cd->volume_key->keylength;
1915 volume_key = cd->volume_key->key;
1918 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1921 r = LUKS_verify_volume_key(&cd->hdr, vk);
1924 log_err(cd, _("Volume key does not match the volume.\n"));
1927 r = LUKS1_activate(cd, name, vk, flags);
1928 } else if (isVERITY(cd->type)) {
1929 /* volume_key == root hash */
1930 if (!volume_key || !volume_key_size) {
1931 log_err(cd, _("Incorrect root hash specified for verity device.\n"));
1935 r = VERITY_activate(cd, name, mdata_device(cd),
1936 volume_key, volume_key_size,
1937 &cd->verity_hdr, cd->verity_flags);
1940 free(cd->verity_root_hash);
1941 cd->verity_root_hash = NULL;
1943 cd->verity_root_hash_size = volume_key_size;
1944 if (!cd->verity_root_hash)
1945 cd->verity_root_hash = malloc(volume_key_size);
1946 if (cd->verity_root_hash)
1947 memcpy(cd->verity_root_hash, volume_key, volume_key_size);
1950 log_err(cd, _("Device type is not properly initialised.\n"));
1952 crypt_free_volume_key(vk);
1957 int crypt_deactivate(struct crypt_device *cd, const char *name)
1964 log_dbg("Deactivating volume %s.", name);
1966 if (!cd && dm_init(NULL, 1) < 0)
1969 switch (crypt_status(cd, name)) {
1972 r = dm_remove_device(name, 0, 0);
1974 case CRYPT_INACTIVE:
1975 log_err(cd, _("Device %s is not active.\n"), name);
1979 log_err(cd, _("Invalid device %s.\n"), name);
1989 int crypt_volume_key_get(struct crypt_device *cd,
1992 size_t *volume_key_size,
1993 const char *passphrase,
1994 size_t passphrase_size)
1996 struct volume_key *vk = NULL;
2000 if (crypt_fips_mode()) {
2001 log_err(cd, "Function not available in FIPS mode.\n");
2005 key_len = crypt_get_volume_key_size(cd);
2006 if (key_len > *volume_key_size) {
2007 log_err(cd, _("Volume key buffer too small.\n"));
2011 if (isPLAIN(cd->type) && cd->plain_hdr.hash) {
2012 r = process_key(cd, cd->plain_hdr.hash, key_len,
2013 passphrase, passphrase_size, &vk);
2015 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
2016 } else if (isLUKS(cd->type)) {
2017 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase,
2018 passphrase_size, &cd->hdr, &vk, cd);
2021 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
2024 memcpy(volume_key, vk->key, vk->keylength);
2025 *volume_key_size = vk->keylength;
2028 crypt_free_volume_key(vk);
2032 int crypt_volume_key_verify(struct crypt_device *cd,
2033 const char *volume_key,
2034 size_t volume_key_size)
2036 struct volume_key *vk;
2039 if (!isLUKS(cd->type)) {
2040 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2044 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2048 r = LUKS_verify_volume_key(&cd->hdr, vk);
2051 log_err(cd, _("Volume key does not match the volume.\n"));
2053 crypt_free_volume_key(vk);
2058 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
2060 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
2061 cd->timeout = timeout_sec;
2064 void crypt_set_password_retry(struct crypt_device *cd, int tries)
2066 log_dbg("Password retry count set to %d.", tries);
2070 void crypt_set_iteration_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2072 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
2073 cd->iteration_time = iteration_time_ms;
2075 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2077 crypt_set_iteration_time(cd, iteration_time_ms);
2080 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
2082 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
2083 cd->password_verify = password_verify ? 1 : 0;
2086 void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
2089 case CRYPT_RNG_URANDOM:
2090 case CRYPT_RNG_RANDOM:
2091 log_dbg("RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
2092 cd->rng_type = rng_type;
2096 int crypt_get_rng_type(struct crypt_device *cd)
2101 return cd->rng_type;
2104 int crypt_memory_lock(struct crypt_device *cd, int lock)
2106 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
2110 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
2114 if (!cd && dm_init(NULL, 1) < 0)
2115 return CRYPT_INVALID;
2117 r = dm_status_device(name);
2122 if (r < 0 && r != -ENODEV)
2123 return CRYPT_INVALID;
2126 return CRYPT_ACTIVE;
2131 return CRYPT_INACTIVE;
2134 static void hexprint(struct crypt_device *cd, const char *d, int n, const char *sep)
2137 for(i = 0; i < n; i++)
2138 log_std(cd, "%02hhx%s", (const char)d[i], sep);
2141 static int _luks_dump(struct crypt_device *cd)
2145 log_std(cd, "LUKS header information for %s\n\n", mdata_device(cd));
2146 log_std(cd, "Version: \t%d\n", cd->hdr.version);
2147 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
2148 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
2149 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec);
2150 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
2151 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
2152 log_std(cd, "MK digest: \t");
2153 hexprint(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE, " ");
2155 log_std(cd, "MK salt: \t");
2156 hexprint(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2, " ");
2157 log_std(cd, "\n \t");
2158 hexprint(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ");
2160 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
2161 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
2162 for(i = 0; i < LUKS_NUMKEYS; i++) {
2163 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
2164 log_std(cd, "Key Slot %d: ENABLED\n",i);
2165 log_std(cd, "\tIterations: \t%d\n",
2166 cd->hdr.keyblock[i].passwordIterations);
2167 log_std(cd, "\tSalt: \t");
2168 hexprint(cd, cd->hdr.keyblock[i].passwordSalt,
2169 LUKS_SALTSIZE/2, " ");
2170 log_std(cd, "\n\t \t");
2171 hexprint(cd, cd->hdr.keyblock[i].passwordSalt +
2172 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ");
2175 log_std(cd, "\tKey material offset:\t%d\n",
2176 cd->hdr.keyblock[i].keyMaterialOffset);
2177 log_std(cd, "\tAF stripes: \t%d\n",
2178 cd->hdr.keyblock[i].stripes);
2181 log_std(cd, "Key Slot %d: DISABLED\n", i);
2186 static int _verity_dump(struct crypt_device *cd)
2188 log_std(cd, "VERITY header information for %s\n", mdata_device(cd));
2189 log_std(cd, "Version: \t%u\n", cd->verity_hdr.version);
2190 log_std(cd, "Data blocks: \t%" PRIu64 "\n", cd->verity_hdr.data_size);
2191 log_std(cd, "Data block size: \t%u\n", cd->verity_hdr.data_block_size);
2192 log_std(cd, "Hash block size: \t%u\n", cd->verity_hdr.hash_block_size);
2193 log_std(cd, "Hash algorithm: \t%s\n", cd->verity_hdr.hash_name);
2194 log_std(cd, "Salt: \t");
2195 if (cd->verity_hdr.salt_size)
2196 hexprint(cd, cd->verity_hdr.salt, cd->verity_hdr.salt_size, "");
2200 if (cd->verity_root_hash) {
2201 log_std(cd, "Root hash: \t");
2202 hexprint(cd, cd->verity_root_hash, cd->verity_root_hash_size, "");
2208 int crypt_dump(struct crypt_device *cd)
2210 if (isLUKS(cd->type))
2211 return _luks_dump(cd);
2212 else if (isVERITY(cd->type))
2213 return _verity_dump(cd);
2215 log_err(cd, _("Dump operation is not supported for this device type.\n"));
2219 const char *crypt_get_cipher(struct crypt_device *cd)
2221 if (isPLAIN(cd->type))
2222 return cd->plain_cipher;
2224 if (isLUKS(cd->type))
2225 return cd->hdr.cipherName;
2227 if (isLOOPAES(cd->type))
2228 return cd->loopaes_cipher;
2233 const char *crypt_get_cipher_mode(struct crypt_device *cd)
2235 if (isPLAIN(cd->type))
2236 return cd->plain_cipher_mode;
2238 if (isLUKS(cd->type))
2239 return cd->hdr.cipherMode;
2241 if (isLOOPAES(cd->type))
2242 return cd->loopaes_cipher_mode;
2247 const char *crypt_get_uuid(struct crypt_device *cd)
2249 if (isLUKS(cd->type))
2250 return cd->hdr.uuid;
2252 if (isPLAIN(cd->type))
2253 return cd->plain_uuid;
2255 if (isLOOPAES(cd->type))
2256 return cd->loopaes_uuid;
2261 const char *crypt_get_device_name(struct crypt_device *cd)
2266 int crypt_get_volume_key_size(struct crypt_device *cd)
2268 if (isPLAIN(cd->type))
2269 return cd->plain_key_size;
2271 if (isLUKS(cd->type))
2272 return cd->hdr.keyBytes;
2274 if (isLOOPAES(cd->type))
2275 return cd->loopaes_key_size;
2277 if (isVERITY(cd->type))
2278 return cd->verity_root_hash_size;
2283 uint64_t crypt_get_data_offset(struct crypt_device *cd)
2285 if (isPLAIN(cd->type))
2286 return cd->plain_hdr.offset;
2288 if (isLUKS(cd->type))
2289 return cd->hdr.payloadOffset;
2291 if (isLOOPAES(cd->type))
2292 return cd->loopaes_hdr.offset;
2297 uint64_t crypt_get_iv_offset(struct crypt_device *cd)
2299 if (isPLAIN(cd->type))
2300 return cd->plain_hdr.skip;
2302 if (isLUKS(cd->type))
2305 if (isLOOPAES(cd->type))
2306 return cd->loopaes_hdr.skip;
2311 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
2313 if (!isLUKS(cd->type)) {
2314 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2315 return CRYPT_SLOT_INVALID;
2318 return LUKS_keyslot_info(&cd->hdr, keyslot);
2321 int crypt_keyslot_max(const char *type)
2323 if (type && isLUKS(type))
2324 return LUKS_NUMKEYS;
2329 const char *crypt_get_type(struct crypt_device *cd)
2334 int crypt_get_active_device(struct crypt_device *cd __attribute__((unused)),
2336 struct crypt_active_device *cad)
2338 struct crypt_dm_active_device dmd;
2341 r = dm_query_device(name, 0, &dmd);
2345 cad->offset = dmd.u.crypt.offset;
2346 cad->iv_offset = dmd.u.crypt.iv_offset;
2347 cad->size = dmd.size;
2348 cad->flags = dmd.flags;