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),
308 .data_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.data_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, 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 static int _init_by_name_crypt(struct crypt_device *cd, const char *name)
682 struct crypt_dm_active_device dmd = {};
683 char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
686 r = dm_query_device(name, DM_ACTIVE_DEVICE |
688 DM_ACTIVE_CRYPT_CIPHER |
689 DM_ACTIVE_CRYPT_KEYSIZE, &dmd);
693 if (isPLAIN(cd->type)) {
694 cd->plain_uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
695 cd->plain_hdr.hash = NULL; /* no way to get this */
696 cd->plain_hdr.offset = dmd.u.crypt.offset;
697 cd->plain_hdr.skip = dmd.u.crypt.iv_offset;
698 cd->plain_key_size = dmd.u.crypt.vk->keylength;
700 r = crypt_parse_name_and_mode(dmd.u.crypt.cipher, cipher, NULL, cipher_mode);
702 cd->plain_cipher = strdup(cipher);
703 cd->plain_cipher_mode = strdup(cipher_mode);
705 } else if (isLOOPAES(cd->type)) {
706 cd->loopaes_uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
707 cd->loopaes_hdr.offset = dmd.u.crypt.offset;
709 r = crypt_parse_name_and_mode(dmd.u.crypt.cipher, cipher,
710 &key_nums, cipher_mode);
712 cd->loopaes_cipher = strdup(cipher);
713 cd->loopaes_cipher_mode = strdup(cipher_mode);
714 /* version 3 uses last key for IV */
715 if (dmd.u.crypt.vk->keylength % key_nums)
717 cd->loopaes_key_size = dmd.u.crypt.vk->keylength / key_nums;
719 } else if (isLUKS(cd->type)) {
720 if (mdata_device(cd)) {
721 r = _crypt_load_luks1(cd, 0, 0);
723 log_dbg("LUKS device header does not match active device.");
729 /* check whether UUIDs match each other */
730 r = crypt_uuid_cmp(dmd.uuid, cd->hdr.uuid);
732 log_dbg("LUKS device header uuid: %s mismatches DM returned uuid %s",
733 cd->hdr.uuid, dmd.uuid);
742 crypt_free_volume_key(dmd.u.crypt.vk);
743 free(CONST_CAST(void*)dmd.u.crypt.cipher);
744 free(CONST_CAST(void*)dmd.data_device);
745 free(CONST_CAST(void*)dmd.uuid);
749 static int _init_by_name_verity(struct crypt_device *cd, const char *name)
751 struct crypt_params_verity params = {};
752 struct crypt_dm_active_device dmd = {
754 .u.verity.vp = ¶ms,
758 r = dm_query_device(name, DM_ACTIVE_DEVICE |
760 DM_ACTIVE_VERITY_HASH_DEVICE |
761 DM_ACTIVE_VERITY_PARAMS, &dmd);
765 if (isVERITY(cd->type)) {
766 cd->verity_flags = CRYPT_VERITY_NO_HEADER; //FIXME
767 //cd->verity_uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
768 cd->verity_hdr.data_size = params.data_size;
769 cd->verity_root_hash_size = dmd.u.verity.root_hash_size;
770 cd->verity_root_hash = NULL;
771 cd->verity_hdr.hash_name = params.hash_name;
772 cd->verity_hdr.data_device = NULL;
773 cd->verity_hdr.data_block_size = params.data_block_size;
774 cd->verity_hdr.hash_block_size = params.hash_block_size;
775 cd->verity_hdr.hash_area_offset = params.hash_area_offset;
776 cd->verity_hdr.version = params.version;
777 cd->verity_hdr.flags = params.flags;
778 cd->verity_hdr.salt_size = params.salt_size;
779 cd->verity_hdr.salt = params.salt;
782 free(CONST_CAST(void*)dmd.u.verity.hash_device);
783 free(CONST_CAST(void*)dmd.data_device);
784 free(CONST_CAST(void*)dmd.uuid);
788 int crypt_init_by_name_and_header(struct crypt_device **cd,
790 const char *header_device)
792 crypt_status_info ci;
793 struct crypt_dm_active_device dmd;
796 log_dbg("Allocating crypt device context by device %s.", name);
798 ci = crypt_status(NULL, name);
799 if (ci == CRYPT_INVALID)
802 if (ci < CRYPT_ACTIVE) {
803 log_err(NULL, _("Device %s is not active.\n"), name);
807 r = dm_query_device(name, DM_ACTIVE_DEVICE | DM_ACTIVE_UUID, &dmd);
814 r = crypt_init(cd, header_device);
816 r = crypt_init(cd, dmd.data_device);
818 /* Underlying device disappeared but mapping still active */
819 if (!dmd.data_device || r == -ENOTBLK)
820 log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
823 /* Underlying device is not readable but crypt mapping exists */
825 free(CONST_CAST(void*)dmd.data_device);
826 dmd.data_device = NULL;
827 r = crypt_init(cd, NULL);
835 if (!strncmp(CRYPT_PLAIN, dmd.uuid, sizeof(CRYPT_PLAIN)-1))
836 (*cd)->type = strdup(CRYPT_PLAIN);
837 else if (!strncmp(CRYPT_LOOPAES, dmd.uuid, sizeof(CRYPT_LOOPAES)-1))
838 (*cd)->type = strdup(CRYPT_LOOPAES);
839 else if (!strncmp(CRYPT_LUKS1, dmd.uuid, sizeof(CRYPT_LUKS1)-1))
840 (*cd)->type = strdup(CRYPT_LUKS1);
841 else if (!strncmp(CRYPT_VERITY, dmd.uuid, sizeof(CRYPT_VERITY)-1))
842 (*cd)->type = strdup(CRYPT_VERITY);
844 log_dbg("Unknown UUID set, some parameters are not set.");
846 log_dbg("Active device has no UUID set, some parameters are not set.");
849 r = crypt_set_data_device(*cd, dmd.data_device);
854 /* Try to initialise basic parameters from active device */
856 if (!(*cd)->backing_file && dmd.data_device &&
857 crypt_loop_device(dmd.data_device) &&
858 !((*cd)->backing_file = crypt_loop_backing_file(dmd.data_device))) {
863 if (dmd.target == DM_CRYPT)
864 r = _init_by_name_crypt(*cd, name);
865 else if (dmd.target == DM_VERITY)
866 r = _init_by_name_verity(*cd, name);
872 free(CONST_CAST(void*)dmd.data_device);
873 free(CONST_CAST(void*)dmd.uuid);
877 int crypt_init_by_name(struct crypt_device **cd, const char *name)
879 return crypt_init_by_name_and_header(cd, name, NULL);
882 static int _crypt_format_plain(struct crypt_device *cd,
884 const char *cipher_mode,
886 size_t volume_key_size,
887 struct crypt_params_plain *params)
889 if (!cipher || !cipher_mode) {
890 log_err(cd, _("Invalid plain crypt parameters.\n"));
894 if (volume_key_size > 1024) {
895 log_err(cd, _("Invalid key size.\n"));
899 cd->plain_key_size = volume_key_size;
900 cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
904 cd->plain_cipher = strdup(cipher);
905 cd->plain_cipher_mode = strdup(cipher_mode);
908 cd->plain_uuid = strdup(uuid);
910 if (params && params->hash)
911 cd->plain_hdr.hash = strdup(params->hash);
913 cd->plain_hdr.offset = params ? params->offset : 0;
914 cd->plain_hdr.skip = params ? params->skip : 0;
915 cd->plain_hdr.size = params ? params->size : 0;
917 if (!cd->plain_cipher || !cd->plain_cipher_mode)
923 static int _crypt_format_luks1(struct crypt_device *cd,
925 const char *cipher_mode,
927 const char *volume_key,
928 size_t volume_key_size,
929 struct crypt_params_luks1 *params)
932 unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
933 unsigned long alignment_offset = 0;
935 if (!mdata_device(cd)) {
936 log_err(cd, _("Can't format LUKS without device.\n"));
941 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
944 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
949 if (params && params->data_device) {
950 cd->metadata_device = cd->device;
951 if (!(cd->device = strdup(params->data_device)))
953 required_alignment = params->data_alignment * SECTOR_SIZE;
954 } else if (params && params->data_alignment) {
955 required_alignment = params->data_alignment * SECTOR_SIZE;
957 get_topology_alignment(cd->device, &required_alignment,
958 &alignment_offset, DEFAULT_DISK_ALIGNMENT);
960 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
961 (params && params->hash) ? params->hash : "sha1",
963 required_alignment / SECTOR_SIZE,
964 alignment_offset / SECTOR_SIZE,
965 cd->iteration_time, &cd->PBKDF2_per_sec,
966 cd->metadata_device, cd);
970 /* Wipe first 8 sectors - fs magic numbers etc. */
971 r = crypt_wipe(mdata_device(cd), 0, 8 * SECTOR_SIZE, CRYPT_WIPE_ZERO, 1);
974 log_err(cd, _("Cannot format device %s which is still in use.\n"),
977 log_err(cd, _("Cannot wipe header on device %s.\n"),
983 r = LUKS_write_phdr(mdata_device(cd), &cd->hdr, cd);
988 static int _crypt_format_loopaes(struct crypt_device *cd,
991 size_t volume_key_size,
992 struct crypt_params_loopaes *params)
994 if (!mdata_device(cd)) {
995 log_err(cd, _("Can't format LOOPAES without device.\n"));
999 if (volume_key_size > 1024) {
1000 log_err(cd, _("Invalid key size.\n"));
1004 cd->loopaes_key_size = volume_key_size;
1006 cd->loopaes_cipher = strdup(cipher ?: DEFAULT_LOOPAES_CIPHER);
1009 cd->loopaes_uuid = strdup(uuid);
1011 if (params && params->hash)
1012 cd->loopaes_hdr.hash = strdup(params->hash);
1014 cd->loopaes_hdr.offset = params ? params->offset : 0;
1015 cd->loopaes_hdr.skip = params ? params->skip : 0;
1020 static int _crypt_format_verity(struct crypt_device *cd,
1021 struct crypt_params_verity *params)
1024 uint64_t data_device_size;
1026 if (!mdata_device(cd)) {
1027 log_err(cd, _("Can't format VERITY without device.\n"));
1031 if (!params || !params->data_device)
1034 if (params->version > 1)
1037 /* set dat device */
1038 cd->type = CRYPT_VERITY;
1039 r = crypt_set_data_device(cd, params->data_device);
1043 if (!params->data_size) {
1044 r = device_size(params->data_device, &data_device_size);
1048 cd->verity_hdr.data_size = data_device_size / params->data_block_size;
1050 cd->verity_hdr.data_size = params->data_size;
1053 cd->verity_root_hash_size = crypt_hash_size(params->hash_name);
1054 if (!cd->verity_root_hash_size)
1057 cd->verity_flags = params->flags;
1058 cd->verity_root_hash = malloc(cd->verity_root_hash_size);
1059 if (!cd->verity_root_hash)
1062 cd->verity_hdr.hash_name = strdup(params->hash_name);
1063 cd->verity_hdr.data_device = NULL;
1064 cd->verity_hdr.data_block_size = params->data_block_size;
1065 cd->verity_hdr.hash_block_size = params->hash_block_size;
1066 cd->verity_hdr.hash_area_offset = params->hash_area_offset;
1067 cd->verity_hdr.version = params->version;
1068 cd->verity_hdr.flags = params->flags;
1069 cd->verity_hdr.salt_size = params->salt_size;
1070 cd->verity_hdr.salt = malloc(params->salt_size);
1072 memcpy(CONST_CAST(char*)cd->verity_hdr.salt, params->salt,
1075 r = crypt_random_get(cd, CONST_CAST(char*)cd->verity_hdr.salt,
1076 params->salt_size, CRYPT_RND_SALT);
1080 log_dbg("Creating verity hash on device %s.", mdata_device(cd));
1081 r = VERITY_create(cd, &cd->verity_hdr, cd->device, mdata_device(cd),
1082 cd->verity_root_hash, cd->verity_root_hash_size);
1086 r = VERITY_write_sb(cd, mdata_device(cd),
1087 cd->verity_hdr.hash_area_offset,
1091 free(cd->verity_root_hash);
1092 free(CONST_CAST(char*)cd->verity_hdr.hash_name);
1093 free(CONST_CAST(char*)cd->verity_hdr.salt);
1099 int crypt_format(struct crypt_device *cd,
1102 const char *cipher_mode,
1104 const char *volume_key,
1105 size_t volume_key_size,
1114 log_dbg("Context already formatted as %s.", cd->type);
1118 log_dbg("Formatting device %s as type %s.", mdata_device(cd) ?: "(none)", type);
1120 r = init_crypto(cd);
1125 r = _crypt_format_plain(cd, cipher, cipher_mode,
1126 uuid, volume_key_size, params);
1127 else if (isLUKS(type))
1128 r = _crypt_format_luks1(cd, cipher, cipher_mode,
1129 uuid, volume_key, volume_key_size, params);
1130 else if (isLOOPAES(type))
1131 r = _crypt_format_loopaes(cd, cipher, uuid, volume_key_size, params);
1132 else if (isVERITY(type))
1133 r = _crypt_format_verity(cd, params);
1135 /* FIXME: allow plugins here? */
1136 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
1140 if (!r && !(cd->type = strdup(type)))
1144 crypt_free_volume_key(cd->volume_key);
1145 cd->volume_key = NULL;
1151 int crypt_load(struct crypt_device *cd,
1152 const char *requested_type,
1157 log_dbg("Trying to load %s crypt type from device %s.",
1158 requested_type ?: "any", mdata_device(cd) ?: "(none)");
1160 if (!mdata_device(cd))
1163 if (!requested_type || isLUKS(requested_type)) {
1164 if (cd->type && !isLUKS(cd->type)) {
1165 log_dbg("Context is already initialised to type %s", cd->type);
1169 r = _crypt_load_luks1(cd, 1, 0);
1170 } else if (isVERITY(requested_type)) {
1171 if (cd->type && !isVERITY(cd->type)) {
1172 log_dbg("Context is already initialised to type %s", cd->type);
1175 r = _crypt_load_verity(cd, params);
1182 /* cd->type and header must be set in context */
1183 r = crypt_check_data_device_size(cd);
1192 int crypt_repair(struct crypt_device *cd,
1193 const char *requested_type,
1194 void *params __attribute__((unused)))
1198 log_dbg("Trying to repair %s crypt type from device %s.",
1199 requested_type ?: "any", mdata_device(cd) ?: "(none)");
1201 if (!mdata_device(cd))
1204 if (requested_type && !isLUKS(requested_type))
1208 /* Load with repair */
1209 r = _crypt_load_luks1(cd, 1, 1);
1213 /* cd->type and header must be set in context */
1214 r = crypt_check_data_device_size(cd);
1223 int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
1225 struct crypt_dm_active_device dmd;
1228 /* Device context type must be initialised */
1229 if (!cd->type || !crypt_get_uuid(cd))
1232 log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
1234 r = dm_query_device(name, DM_ACTIVE_DEVICE | DM_ACTIVE_CRYPT_CIPHER |
1235 DM_ACTIVE_UUID | DM_ACTIVE_CRYPT_KEYSIZE |
1236 DM_ACTIVE_CRYPT_KEY, &dmd);
1238 log_err(NULL, _("Device %s is not active.\n"), name);
1242 if (!dmd.uuid || dmd.target != DM_CRYPT) {
1247 r = device_check_and_adjust(cd, dmd.data_device, DEV_OK, &new_size,
1248 &dmd.u.crypt.offset, &dmd.flags);
1252 if (new_size == dmd.size) {
1253 log_dbg("Device has already requested size %" PRIu64
1254 " sectors.", dmd.size);
1257 dmd.size = new_size;
1258 r = dm_create_device(name, cd->type, &dmd, 1);
1261 if (dmd.target == DM_CRYPT) {
1262 crypt_free_volume_key(dmd.u.crypt.vk);
1263 free(CONST_CAST(void*)dmd.u.crypt.cipher);
1265 free(CONST_CAST(void*)dmd.data_device);
1266 free(CONST_CAST(void*)dmd.uuid);
1271 int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
1273 if (!isLUKS(cd->type)) {
1274 log_err(cd, _("This operation is not supported for this device type.\n"));
1278 if (uuid && !strncmp(uuid, cd->hdr.uuid, sizeof(cd->hdr.uuid))) {
1279 log_dbg("UUID is the same as requested (%s) for device %s.",
1280 uuid, mdata_device(cd));
1285 log_dbg("Requested new UUID change to %s for %s.", uuid, mdata_device(cd));
1287 log_dbg("Requested new UUID refresh for %s.", mdata_device(cd));
1289 if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
1292 return LUKS_hdr_uuid_set(mdata_device(cd), &cd->hdr, uuid, cd);
1295 int crypt_header_backup(struct crypt_device *cd,
1296 const char *requested_type,
1297 const char *backup_file)
1301 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1304 r = init_crypto(cd);
1308 log_dbg("Requested header backup of device %s (%s) to "
1309 "file %s.", mdata_device(cd), requested_type, backup_file);
1311 return LUKS_hdr_backup(backup_file, mdata_device(cd), &cd->hdr, cd);
1314 int crypt_header_restore(struct crypt_device *cd,
1315 const char *requested_type,
1316 const char *backup_file)
1320 if (requested_type && !isLUKS(requested_type))
1323 /* Some hash functions need initialized gcrypt library */
1324 r = init_crypto(cd);
1328 log_dbg("Requested header restore to device %s (%s) from "
1329 "file %s.", mdata_device(cd), requested_type, backup_file);
1331 return LUKS_hdr_restore(backup_file, mdata_device(cd), &cd->hdr, cd);
1334 void crypt_free(struct crypt_device *cd)
1337 log_dbg("Releasing crypt device %s context.", mdata_device(cd));
1339 if (cd->loop_fd != -1)
1343 crypt_free_volume_key(cd->volume_key);
1346 free(cd->metadata_device);
1347 free(cd->backing_file);
1350 /* used in plain device only */
1351 free(CONST_CAST(void*)cd->plain_hdr.hash);
1352 free(cd->plain_cipher);
1353 free(cd->plain_cipher_mode);
1354 free(cd->plain_uuid);
1356 /* used in loop-AES device only */
1357 free(CONST_CAST(void*)cd->loopaes_hdr.hash);
1358 free(cd->loopaes_cipher);
1359 free(cd->loopaes_uuid);
1361 /* used in verity device only */
1362 free(CONST_CAST(void*)cd->verity_hdr.hash_name);
1363 free(CONST_CAST(void*)cd->verity_hdr.salt);
1364 free(cd->verity_root_hash);
1370 int crypt_suspend(struct crypt_device *cd,
1373 crypt_status_info ci;
1376 log_dbg("Suspending volume %s.", name);
1378 if (!isLUKS(cd->type)) {
1379 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1384 ci = crypt_status(NULL, name);
1385 if (ci < CRYPT_ACTIVE) {
1386 log_err(cd, _("Volume %s is not active.\n"), name);
1390 if (!cd && dm_init(NULL, 1) < 0)
1393 r = dm_status_suspended(name);
1398 log_err(cd, _("Volume %s is already suspended.\n"), name);
1403 r = dm_suspend_and_wipe_key(name);
1405 log_err(cd, "Suspend is not supported for device %s.\n", name);
1407 log_err(cd, "Error during suspending device %s.\n", name);
1414 int crypt_resume_by_passphrase(struct crypt_device *cd,
1417 const char *passphrase,
1418 size_t passphrase_size)
1420 struct volume_key *vk = NULL;
1423 log_dbg("Resuming volume %s.", name);
1425 if (!isLUKS(cd->type)) {
1426 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1431 r = dm_status_suspended(name);
1436 log_err(cd, _("Volume %s is not suspended.\n"), name);
1441 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase,
1442 passphrase_size, &cd->hdr, &vk, cd);
1444 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1448 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1450 log_err(cd, "Resume is not supported for device %s.\n", name);
1452 log_err(cd, "Error during resuming device %s.\n", name);
1456 crypt_free_volume_key(vk);
1457 return r < 0 ? r : keyslot;
1460 int crypt_resume_by_keyfile_offset(struct crypt_device *cd,
1463 const char *keyfile,
1464 size_t keyfile_size,
1465 size_t keyfile_offset)
1467 struct volume_key *vk = NULL;
1468 char *passphrase_read = NULL;
1469 size_t passphrase_size_read;
1472 log_dbg("Resuming volume %s.", name);
1474 if (!isLUKS(cd->type)) {
1475 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1480 r = dm_status_suspended(name);
1485 log_err(cd, _("Volume %s is not suspended.\n"), name);
1492 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1493 &passphrase_size_read, keyfile, keyfile_offset,
1498 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase_read,
1499 passphrase_size_read, &cd->hdr, &vk, cd);
1504 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1506 log_err(cd, "Error during resuming device %s.\n", name);
1508 crypt_safe_free(passphrase_read);
1509 crypt_free_volume_key(vk);
1510 return r < 0 ? r : keyslot;
1513 int crypt_resume_by_keyfile(struct crypt_device *cd,
1516 const char *keyfile,
1517 size_t keyfile_size)
1519 return crypt_resume_by_keyfile_offset(cd, name, keyslot,
1520 keyfile, keyfile_size, 0);
1523 // slot manipulation
1524 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1525 int keyslot, // -1 any
1526 const char *passphrase, // NULL -> terminal
1527 size_t passphrase_size,
1528 const char *new_passphrase, // NULL -> terminal
1529 size_t new_passphrase_size)
1531 struct volume_key *vk = NULL;
1532 char *password = NULL, *new_password = NULL;
1533 size_t passwordLen, new_passwordLen;
1536 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1537 "new passphrase %sprovided.",
1538 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1540 if (!isLUKS(cd->type)) {
1541 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1545 r = keyslot_verify_or_find_empty(cd, &keyslot);
1549 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1550 /* No slots used, try to use pre-generated key in header */
1551 if (cd->volume_key) {
1552 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1553 r = vk ? 0 : -ENOMEM;
1555 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1558 } else if (passphrase) {
1559 /* Passphrase provided, use it to unlock existing keyslot */
1560 r = LUKS_open_key_with_hdr(mdata_device(cd), CRYPT_ANY_SLOT, passphrase,
1561 passphrase_size, &cd->hdr, &vk, cd);
1563 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1564 r = key_from_terminal(cd, _("Enter any passphrase: "),
1565 &password, &passwordLen, 0);
1569 r = LUKS_open_key_with_hdr(mdata_device(cd), CRYPT_ANY_SLOT, password,
1570 passwordLen, &cd->hdr, &vk, cd);
1571 crypt_safe_free(password);
1577 if (new_passphrase) {
1578 new_password = CONST_CAST(char*)new_passphrase;
1579 new_passwordLen = new_passphrase_size;
1581 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1582 &new_password, &new_passwordLen, 1);
1587 r = LUKS_set_key(mdata_device(cd), keyslot, new_password, new_passwordLen,
1588 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1593 if (!new_passphrase)
1594 crypt_safe_free(new_password);
1595 crypt_free_volume_key(vk);
1596 return r ?: keyslot;
1599 int crypt_keyslot_add_by_keyfile_offset(struct crypt_device *cd,
1601 const char *keyfile,
1602 size_t keyfile_size,
1603 size_t keyfile_offset,
1604 const char *new_keyfile,
1605 size_t new_keyfile_size,
1606 size_t new_keyfile_offset)
1608 struct volume_key *vk = NULL;
1609 char *password = NULL; size_t passwordLen;
1610 char *new_password = NULL; size_t new_passwordLen;
1613 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1614 keyfile ?: "[none]", new_keyfile ?: "[none]");
1616 if (!isLUKS(cd->type)) {
1617 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1621 r = keyslot_verify_or_find_empty(cd, &keyslot);
1625 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1626 /* No slots used, try to use pre-generated key in header */
1627 if (cd->volume_key) {
1628 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1629 r = vk ? 0 : -ENOMEM;
1631 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1635 /* Read password from file of (if NULL) from terminal */
1637 r = key_from_file(cd, _("Enter any passphrase: "),
1638 &password, &passwordLen,
1639 keyfile, keyfile_offset, keyfile_size);
1641 r = key_from_terminal(cd, _("Enter any passphrase: "),
1642 &password, &passwordLen, 0);
1646 r = LUKS_open_key_with_hdr(mdata_device(cd), CRYPT_ANY_SLOT, password, passwordLen,
1654 r = key_from_file(cd, _("Enter new passphrase for key slot: "),
1655 &new_password, &new_passwordLen, new_keyfile,
1656 new_keyfile_offset, new_keyfile_size);
1658 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1659 &new_password, &new_passwordLen, 1);
1663 r = LUKS_set_key(mdata_device(cd), keyslot, new_password, new_passwordLen,
1664 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1666 crypt_safe_free(password);
1667 crypt_safe_free(new_password);
1668 crypt_free_volume_key(vk);
1669 return r < 0 ? r : keyslot;
1672 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1674 const char *keyfile,
1675 size_t keyfile_size,
1676 const char *new_keyfile,
1677 size_t new_keyfile_size)
1679 return crypt_keyslot_add_by_keyfile_offset(cd, keyslot,
1680 keyfile, keyfile_size, 0,
1681 new_keyfile, new_keyfile_size, 0);
1684 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1686 const char *volume_key,
1687 size_t volume_key_size,
1688 const char *passphrase,
1689 size_t passphrase_size)
1691 struct volume_key *vk = NULL;
1693 char *new_password = NULL; size_t new_passwordLen;
1695 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1697 if (!isLUKS(cd->type)) {
1698 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1703 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1704 else if (cd->volume_key)
1705 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1710 r = LUKS_verify_volume_key(&cd->hdr, vk);
1712 log_err(cd, _("Volume key does not match the volume.\n"));
1716 r = keyslot_verify_or_find_empty(cd, &keyslot);
1721 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1722 &new_password, &new_passwordLen, 1);
1725 passphrase = new_password;
1726 passphrase_size = new_passwordLen;
1729 r = LUKS_set_key(mdata_device(cd), keyslot, passphrase, passphrase_size,
1730 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1732 crypt_safe_free(new_password);
1733 crypt_free_volume_key(vk);
1734 return (r < 0) ? r : keyslot;
1737 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1739 crypt_keyslot_info ki;
1741 log_dbg("Destroying keyslot %d.", keyslot);
1743 if (!isLUKS(cd->type)) {
1744 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1748 ki = crypt_keyslot_status(cd, keyslot);
1749 if (ki == CRYPT_SLOT_INVALID) {
1750 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1754 if (ki == CRYPT_SLOT_INACTIVE) {
1755 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1759 return LUKS_del_key(mdata_device(cd), keyslot, &cd->hdr, cd);
1762 // activation/deactivation of device mapping
1763 int crypt_activate_by_passphrase(struct crypt_device *cd,
1766 const char *passphrase,
1767 size_t passphrase_size,
1770 crypt_status_info ci;
1771 struct volume_key *vk = NULL;
1772 char *read_passphrase = NULL;
1773 size_t passphraseLen = 0;
1776 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1777 name ? "Activating" : "Checking", name ?: "",
1778 keyslot, passphrase ? "" : "[none] ");
1781 ci = crypt_status(NULL, name);
1782 if (ci == CRYPT_INVALID)
1784 else if (ci >= CRYPT_ACTIVE) {
1785 log_err(cd, _("Device %s already exists.\n"), name);
1790 /* plain, use hashed passphrase */
1791 if (isPLAIN(cd->type)) {
1796 r = key_from_terminal(cd, NULL, &read_passphrase,
1800 passphrase = read_passphrase;
1801 passphrase_size = passphraseLen;
1804 r = process_key(cd, cd->plain_hdr.hash,
1806 passphrase, passphrase_size, &vk);
1810 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1812 } else if (isLUKS(cd->type)) {
1813 /* provided passphrase, do not retry */
1815 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase,
1816 passphrase_size, &cd->hdr, &vk, cd);
1818 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1823 r = LUKS1_activate(cd, name, vk, flags);
1828 crypt_safe_free(read_passphrase);
1829 crypt_free_volume_key(vk);
1831 return r < 0 ? r : keyslot;
1834 int crypt_activate_by_keyfile_offset(struct crypt_device *cd,
1837 const char *keyfile,
1838 size_t keyfile_size,
1839 size_t keyfile_offset,
1842 crypt_status_info ci;
1843 struct volume_key *vk = NULL;
1844 char *passphrase_read = NULL;
1845 size_t passphrase_size_read;
1846 unsigned int key_count = 0;
1849 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1850 name ?: "", keyslot, keyfile ?: "[none]");
1853 ci = crypt_status(NULL, name);
1854 if (ci == CRYPT_INVALID)
1856 else if (ci >= CRYPT_ACTIVE) {
1857 log_err(cd, _("Device %s already exists.\n"), name);
1865 if (isPLAIN(cd->type)) {
1869 r = key_from_file(cd, _("Enter passphrase: "),
1870 &passphrase_read, &passphrase_size_read,
1871 keyfile, keyfile_offset, keyfile_size);
1875 r = process_key(cd, cd->plain_hdr.hash,
1877 passphrase_read, passphrase_size_read, &vk);
1881 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1882 } else if (isLUKS(cd->type)) {
1883 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1884 &passphrase_size_read, keyfile, keyfile_offset, keyfile_size);
1887 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase_read,
1888 passphrase_size_read, &cd->hdr, &vk, cd);
1894 r = LUKS1_activate(cd, name, vk, flags);
1899 } else if (isLOOPAES(cd->type)) {
1900 r = key_from_file(cd, NULL, &passphrase_read, &passphrase_size_read,
1901 keyfile, keyfile_offset, keyfile_size);
1904 r = LOOPAES_parse_keyfile(cd, &vk, cd->loopaes_hdr.hash, &key_count,
1905 passphrase_read, passphrase_size_read);
1909 r = LOOPAES_activate(cd, name, cd->loopaes_cipher,
1910 key_count, vk, flags);
1915 crypt_safe_free(passphrase_read);
1916 crypt_free_volume_key(vk);
1921 int crypt_activate_by_keyfile(struct crypt_device *cd,
1924 const char *keyfile,
1925 size_t keyfile_size,
1928 return crypt_activate_by_keyfile_offset(cd, name, keyslot, keyfile,
1929 keyfile_size, 0, flags);
1932 int crypt_activate_by_volume_key(struct crypt_device *cd,
1934 const char *volume_key,
1935 size_t volume_key_size,
1938 crypt_status_info ci;
1939 struct volume_key *vk = NULL;
1942 log_dbg("Activating volume %s by volume key.", name);
1945 ci = crypt_status(NULL, name);
1946 if (ci == CRYPT_INVALID)
1948 else if (ci >= CRYPT_ACTIVE) {
1949 log_err(cd, _("Device %s already exists.\n"), name);
1954 /* use key directly, no hash */
1955 if (isPLAIN(cd->type)) {
1959 if (!volume_key || !volume_key_size || volume_key_size != cd->plain_key_size) {
1960 log_err(cd, _("Incorrect volume key specified for plain device.\n"));
1964 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1968 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1969 } else if (isLUKS(cd->type)) {
1970 /* If key is not provided, try to use internal key */
1972 if (!cd->volume_key) {
1973 log_err(cd, _("Volume key does not match the volume.\n"));
1976 volume_key_size = cd->volume_key->keylength;
1977 volume_key = cd->volume_key->key;
1980 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1983 r = LUKS_verify_volume_key(&cd->hdr, vk);
1986 log_err(cd, _("Volume key does not match the volume.\n"));
1989 r = LUKS1_activate(cd, name, vk, flags);
1990 } else if (isVERITY(cd->type)) {
1991 /* volume_key == root hash */
1992 if (!volume_key || !volume_key_size) {
1993 log_err(cd, _("Incorrect root hash specified for verity device.\n"));
1997 r = VERITY_activate(cd, name, mdata_device(cd),
1998 volume_key, volume_key_size,
1999 &cd->verity_hdr, cd->verity_flags);
2002 free(cd->verity_root_hash);
2003 cd->verity_root_hash = NULL;
2005 cd->verity_root_hash_size = volume_key_size;
2006 if (!cd->verity_root_hash)
2007 cd->verity_root_hash = malloc(volume_key_size);
2008 if (cd->verity_root_hash)
2009 memcpy(cd->verity_root_hash, volume_key, volume_key_size);
2012 log_err(cd, _("Device type is not properly initialised.\n"));
2014 crypt_free_volume_key(vk);
2019 int crypt_deactivate(struct crypt_device *cd, const char *name)
2026 log_dbg("Deactivating volume %s.", name);
2028 if (!cd && dm_init(NULL, 1) < 0)
2031 switch (crypt_status(cd, name)) {
2034 r = dm_remove_device(name, 0, 0);
2036 case CRYPT_INACTIVE:
2037 log_err(cd, _("Device %s is not active.\n"), name);
2041 log_err(cd, _("Invalid device %s.\n"), name);
2051 int crypt_volume_key_get(struct crypt_device *cd,
2054 size_t *volume_key_size,
2055 const char *passphrase,
2056 size_t passphrase_size)
2058 struct volume_key *vk = NULL;
2062 if (crypt_fips_mode()) {
2063 log_err(cd, "Function not available in FIPS mode.\n");
2067 key_len = crypt_get_volume_key_size(cd);
2068 if (key_len > *volume_key_size) {
2069 log_err(cd, _("Volume key buffer too small.\n"));
2073 if (isPLAIN(cd->type) && cd->plain_hdr.hash) {
2074 r = process_key(cd, cd->plain_hdr.hash, key_len,
2075 passphrase, passphrase_size, &vk);
2077 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
2078 } else if (isLUKS(cd->type)) {
2079 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase,
2080 passphrase_size, &cd->hdr, &vk, cd);
2083 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
2086 memcpy(volume_key, vk->key, vk->keylength);
2087 *volume_key_size = vk->keylength;
2090 crypt_free_volume_key(vk);
2094 int crypt_volume_key_verify(struct crypt_device *cd,
2095 const char *volume_key,
2096 size_t volume_key_size)
2098 struct volume_key *vk;
2101 if (!isLUKS(cd->type)) {
2102 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2106 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2110 r = LUKS_verify_volume_key(&cd->hdr, vk);
2113 log_err(cd, _("Volume key does not match the volume.\n"));
2115 crypt_free_volume_key(vk);
2120 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
2122 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
2123 cd->timeout = timeout_sec;
2126 void crypt_set_password_retry(struct crypt_device *cd, int tries)
2128 log_dbg("Password retry count set to %d.", tries);
2132 void crypt_set_iteration_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2134 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
2135 cd->iteration_time = iteration_time_ms;
2137 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2139 crypt_set_iteration_time(cd, iteration_time_ms);
2142 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
2144 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
2145 cd->password_verify = password_verify ? 1 : 0;
2148 void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
2151 case CRYPT_RNG_URANDOM:
2152 case CRYPT_RNG_RANDOM:
2153 log_dbg("RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
2154 cd->rng_type = rng_type;
2158 int crypt_get_rng_type(struct crypt_device *cd)
2163 return cd->rng_type;
2166 int crypt_memory_lock(struct crypt_device *cd, int lock)
2168 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
2172 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
2176 if (!cd && dm_init(NULL, 1) < 0)
2177 return CRYPT_INVALID;
2179 r = dm_status_device(name);
2184 if (r < 0 && r != -ENODEV)
2185 return CRYPT_INVALID;
2188 return CRYPT_ACTIVE;
2193 return CRYPT_INACTIVE;
2196 static void hexprint(struct crypt_device *cd, const char *d, int n, const char *sep)
2199 for(i = 0; i < n; i++)
2200 log_std(cd, "%02hhx%s", (const char)d[i], sep);
2203 static int _luks_dump(struct crypt_device *cd)
2207 log_std(cd, "LUKS header information for %s\n\n", mdata_device(cd));
2208 log_std(cd, "Version: \t%d\n", cd->hdr.version);
2209 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
2210 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
2211 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec);
2212 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
2213 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
2214 log_std(cd, "MK digest: \t");
2215 hexprint(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE, " ");
2217 log_std(cd, "MK salt: \t");
2218 hexprint(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2, " ");
2219 log_std(cd, "\n \t");
2220 hexprint(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ");
2222 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
2223 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
2224 for(i = 0; i < LUKS_NUMKEYS; i++) {
2225 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
2226 log_std(cd, "Key Slot %d: ENABLED\n",i);
2227 log_std(cd, "\tIterations: \t%d\n",
2228 cd->hdr.keyblock[i].passwordIterations);
2229 log_std(cd, "\tSalt: \t");
2230 hexprint(cd, cd->hdr.keyblock[i].passwordSalt,
2231 LUKS_SALTSIZE/2, " ");
2232 log_std(cd, "\n\t \t");
2233 hexprint(cd, cd->hdr.keyblock[i].passwordSalt +
2234 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ");
2237 log_std(cd, "\tKey material offset:\t%d\n",
2238 cd->hdr.keyblock[i].keyMaterialOffset);
2239 log_std(cd, "\tAF stripes: \t%d\n",
2240 cd->hdr.keyblock[i].stripes);
2243 log_std(cd, "Key Slot %d: DISABLED\n", i);
2248 static int _verity_dump(struct crypt_device *cd)
2250 log_std(cd, "VERITY header information for %s\n", mdata_device(cd));
2251 log_std(cd, "Version: \t%u\n", cd->verity_hdr.version);
2252 log_std(cd, "Data blocks: \t%" PRIu64 "\n", cd->verity_hdr.data_size);
2253 log_std(cd, "Data block size: \t%u\n", cd->verity_hdr.data_block_size);
2254 log_std(cd, "Hash block size: \t%u\n", cd->verity_hdr.hash_block_size);
2255 log_std(cd, "Hash algorithm: \t%s\n", cd->verity_hdr.hash_name);
2256 log_std(cd, "Salt: \t");
2257 if (cd->verity_hdr.salt_size)
2258 hexprint(cd, cd->verity_hdr.salt, cd->verity_hdr.salt_size, "");
2262 if (cd->verity_root_hash) {
2263 log_std(cd, "Root hash: \t");
2264 hexprint(cd, cd->verity_root_hash, cd->verity_root_hash_size, "");
2270 int crypt_dump(struct crypt_device *cd)
2272 if (isLUKS(cd->type))
2273 return _luks_dump(cd);
2274 else if (isVERITY(cd->type))
2275 return _verity_dump(cd);
2277 log_err(cd, _("Dump operation is not supported for this device type.\n"));
2281 const char *crypt_get_cipher(struct crypt_device *cd)
2283 if (isPLAIN(cd->type))
2284 return cd->plain_cipher;
2286 if (isLUKS(cd->type))
2287 return cd->hdr.cipherName;
2289 if (isLOOPAES(cd->type))
2290 return cd->loopaes_cipher;
2295 const char *crypt_get_cipher_mode(struct crypt_device *cd)
2297 if (isPLAIN(cd->type))
2298 return cd->plain_cipher_mode;
2300 if (isLUKS(cd->type))
2301 return cd->hdr.cipherMode;
2303 if (isLOOPAES(cd->type))
2304 return cd->loopaes_cipher_mode;
2309 const char *crypt_get_uuid(struct crypt_device *cd)
2311 if (isLUKS(cd->type))
2312 return cd->hdr.uuid;
2314 if (isPLAIN(cd->type))
2315 return cd->plain_uuid;
2317 if (isLOOPAES(cd->type))
2318 return cd->loopaes_uuid;
2323 const char *crypt_get_device_name(struct crypt_device *cd)
2328 int crypt_get_volume_key_size(struct crypt_device *cd)
2330 if (isPLAIN(cd->type))
2331 return cd->plain_key_size;
2333 if (isLUKS(cd->type))
2334 return cd->hdr.keyBytes;
2336 if (isLOOPAES(cd->type))
2337 return cd->loopaes_key_size;
2339 if (isVERITY(cd->type))
2340 return cd->verity_root_hash_size;
2345 uint64_t crypt_get_data_offset(struct crypt_device *cd)
2347 if (isPLAIN(cd->type))
2348 return cd->plain_hdr.offset;
2350 if (isLUKS(cd->type))
2351 return cd->hdr.payloadOffset;
2353 if (isLOOPAES(cd->type))
2354 return cd->loopaes_hdr.offset;
2359 uint64_t crypt_get_iv_offset(struct crypt_device *cd)
2361 if (isPLAIN(cd->type))
2362 return cd->plain_hdr.skip;
2364 if (isLUKS(cd->type))
2367 if (isLOOPAES(cd->type))
2368 return cd->loopaes_hdr.skip;
2373 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
2375 if (!isLUKS(cd->type)) {
2376 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2377 return CRYPT_SLOT_INVALID;
2380 return LUKS_keyslot_info(&cd->hdr, keyslot);
2383 int crypt_keyslot_max(const char *type)
2385 if (type && isLUKS(type))
2386 return LUKS_NUMKEYS;
2391 const char *crypt_get_type(struct crypt_device *cd)
2396 int crypt_get_active_device(struct crypt_device *cd __attribute__((unused)),
2398 struct crypt_active_device *cad)
2400 struct crypt_dm_active_device dmd;
2403 r = dm_query_device(name, 0, &dmd);
2407 if (dmd.target != DM_CRYPT)
2410 cad->offset = dmd.u.crypt.offset;
2411 cad->iv_offset = dmd.u.crypt.iv_offset;
2412 cad->size = dmd.size;
2413 cad->flags = dmd.flags;