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 = {
304 .device = crypt_get_device_name(cd),
306 .uuid = crypt_get_uuid(cd),
308 .offset = crypt_get_data_offset(cd),
309 .iv_offset = crypt_get_iv_offset(cd),
314 if (dmd.flags & CRYPT_ACTIVATE_SHARED)
315 device_check = DEV_SHARED;
317 device_check = DEV_EXCL;
319 r = device_check_and_adjust(cd, dmd.device, device_check,
320 &dmd.size, &dmd.offset, &flags);
324 if (crypt_get_cipher_mode(cd))
325 r = asprintf(&dm_cipher, "%s-%s", crypt_get_cipher(cd), crypt_get_cipher_mode(cd));
327 r = asprintf(&dm_cipher, "%s", crypt_get_cipher(cd));
331 dmd.cipher = dm_cipher;
332 log_dbg("Trying to activate PLAIN device %s using cipher %s.", name, dmd.cipher);
334 r = dm_create_device(name, CRYPT_PLAIN, &dmd, 0);
337 if (!cd->plain_uuid && dm_query_device(name, DM_ACTIVE_UUID, &dmd) >= 0)
338 cd->plain_uuid = CONST_CAST(char*)dmd.uuid;
344 int crypt_confirm(struct crypt_device *cd, const char *msg)
346 if (!cd || !cd->confirm)
349 return cd->confirm(msg, cd->confirm_usrptr);
352 static int key_from_terminal(struct crypt_device *cd, char *msg, char **key,
353 size_t *key_len, int force_verify)
359 if(!msg && asprintf(&prompt, _("Enter passphrase for %s: "),
360 cd->backing_file ?: crypt_get_device_name(cd)) < 0)
367 *key = crypt_safe_alloc(DEFAULT_PASSPHRASE_SIZE_MAX);
372 r = cd->password(msg, *key, DEFAULT_PASSPHRASE_SIZE_MAX,
373 cd->password_usrptr);
375 crypt_safe_free(*key);
380 r = crypt_get_key(msg, key, key_len, 0, 0, NULL, cd->timeout,
381 (force_verify || cd->password_verify), cd);
384 return (r < 0) ? r: 0;
387 static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslot,
388 struct volume_key **vk)
390 char *passphrase_read = NULL;
391 size_t passphrase_size_read;
392 int r = -EINVAL, eperm = 0, tries = cd->tries;
396 crypt_free_volume_key(*vk);
399 r = key_from_terminal(cd, NULL, &passphrase_read,
400 &passphrase_size_read, 0);
401 /* Continue if it is just passphrase verify mismatch */
407 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase_read,
408 passphrase_size_read, &cd->hdr, vk, cd);
411 crypt_safe_free(passphrase_read);
412 passphrase_read = NULL;
413 } while (r == -EPERM && (--tries > 0));
416 crypt_free_volume_key(*vk);
419 /* Report wrong passphrase if at least one try failed */
420 if (eperm && r == -EPIPE)
424 crypt_safe_free(passphrase_read);
428 static int key_from_file(struct crypt_device *cd, char *msg,
429 char **key, size_t *key_len,
430 const char *key_file, size_t key_offset,
433 return crypt_get_key(msg, key, key_len, key_offset, key_size, key_file,
437 void crypt_set_log_callback(struct crypt_device *cd,
438 void (*log)(int level, const char *msg, void *usrptr),
445 cd->log_usrptr = usrptr;
449 void crypt_set_confirm_callback(struct crypt_device *cd,
450 int (*confirm)(const char *msg, void *usrptr),
453 cd->confirm = confirm;
454 cd->confirm_usrptr = usrptr;
457 void crypt_set_password_callback(struct crypt_device *cd,
458 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
461 cd->password = password;
462 cd->password_usrptr = usrptr;
465 static void _get_error(char *error, char *buf, size_t size)
467 if (!buf || size < 1)
470 strncpy(buf, error, size - 1);
471 buf[size - 1] = '\0';
477 void crypt_last_error(struct crypt_device *cd, char *buf, size_t size)
480 return _get_error(cd->error, buf, size);
483 /* Deprecated global error interface */
484 void crypt_get_error(char *buf, size_t size)
486 return _get_error(global_error, buf, size);
489 const char *crypt_get_dir(void)
494 int crypt_init(struct crypt_device **cd, const char *device)
496 struct crypt_device *h = NULL;
502 log_dbg("Allocating crypt device %s context.", device);
504 if (!(h = malloc(sizeof(struct crypt_device))))
507 memset(h, 0, sizeof(*h));
511 r = device_ready(NULL, device, O_RDONLY);
513 h->device = crypt_loop_get_device();
514 log_dbg("Not a block device, %s%s.",
515 h->device ? "using free loop device " :
516 "no free loop device found",
519 log_err(NULL, _("Cannot find a free loopback device.\n"));
524 /* Keep the loop open, dettached on last close. */
525 h->loop_fd = crypt_loop_attach(h->device, device, 0, 1, &readonly);
526 if (h->loop_fd == -1) {
527 log_err(NULL, _("Attaching loopback device failed "
528 "(loop device with autoclear flag is required).\n"));
533 h->backing_file = crypt_loop_backing_file(h->device);
534 r = device_ready(NULL, h->device, O_RDONLY);
542 if (!h->device && device && !(h->device = strdup(device))) {
547 if (dm_init(h, 1) < 0) {
552 h->iteration_time = 1000;
553 h->password_verify = 0;
555 h->rng_type = crypt_random_default_key_rng();
561 if (h->loop_fd != -1)
564 free(h->backing_file);
570 static int crypt_check_data_device_size(struct crypt_device *cd)
573 uint64_t size, size_min;
575 /* Check data device size, require at least one sector */
576 size_min = crypt_get_data_offset(cd) << SECTOR_SHIFT ?: SECTOR_SIZE;
578 r = device_size(crypt_get_device_name(cd), &size);
582 if (size < size_min) {
583 log_err(cd, _("LUKS header detected but device %s is too small.\n"),
584 crypt_get_device_name(cd));
591 int crypt_set_data_device(struct crypt_device *cd, const char *device)
596 log_dbg("Setting ciphertext data device to %s.", device ?: "(none)");
598 if (!isLUKS(cd->type) && !isVERITY(cd->type)) {
599 log_err(cd, _("This operation is not supported for this device type.\n"));
603 /* metadata device must be set */
604 if (!cd->device || !device)
607 r = device_ready(NULL, device, O_RDONLY);
611 if (!(data_device = strdup(device)))
614 if (!cd->metadata_device)
615 cd->metadata_device = cd->device;
619 cd->device = data_device;
621 return crypt_check_data_device_size(cd);
624 static int _crypt_load_luks1(struct crypt_device *cd, int require_header, int repair)
626 struct luks_phdr hdr;
633 r = LUKS_read_phdr(mdata_device(cd), &hdr, require_header, repair, cd);
637 if (!cd->type && !(cd->type = strdup(CRYPT_LUKS1)))
640 memcpy(&cd->hdr, &hdr, sizeof(hdr));
645 static int _crypt_load_verity(struct crypt_device *cd, struct crypt_params_verity *params)
648 size_t sb_offset = 0;
655 sb_offset = params->hash_area_offset;
657 r = VERITY_read_sb(cd, mdata_device(cd), sb_offset, &cd->verity_hdr);
662 cd->verity_flags = params->flags;
664 if (params && params->data_device &&
665 (r = crypt_set_data_device(cd, params->data_device)) < 0)
668 cd->verity_root_hash_size = crypt_hash_size(cd->verity_hdr.hash_name);
670 if (!cd->type && !(cd->type = strdup(CRYPT_VERITY)))
676 int crypt_init_by_name_and_header(struct crypt_device **cd,
678 const char *header_device)
680 crypt_status_info ci;
681 struct crypt_dm_active_device dmd;
682 char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
686 log_dbg("Allocating crypt device context by device %s.", name);
688 ci = crypt_status(NULL, name);
689 if (ci == CRYPT_INVALID)
692 if (ci < CRYPT_ACTIVE) {
693 log_err(NULL, _("Device %s is not active.\n"), name);
697 r = dm_query_device(name, DM_ACTIVE_DEVICE | DM_ACTIVE_CIPHER |
698 DM_ACTIVE_UUID | DM_ACTIVE_KEYSIZE, &dmd);
705 r = crypt_init(cd, header_device);
707 r = crypt_init(cd, dmd.device);
709 /* Underlying device disappeared but mapping still active */
710 if (!dmd.device || r == -ENOTBLK)
711 log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
714 /* Underlying device is not readable but crypt mapping exists */
716 free(CONST_CAST(void*)dmd.device);
718 r = crypt_init(cd, NULL);
726 if (!strncmp(CRYPT_PLAIN, dmd.uuid, sizeof(CRYPT_PLAIN)-1))
727 (*cd)->type = strdup(CRYPT_PLAIN);
728 else if (!strncmp(CRYPT_LOOPAES, dmd.uuid, sizeof(CRYPT_LOOPAES)-1))
729 (*cd)->type = strdup(CRYPT_LOOPAES);
730 else if (!strncmp(CRYPT_LUKS1, dmd.uuid, sizeof(CRYPT_LUKS1)-1))
731 (*cd)->type = strdup(CRYPT_LUKS1);
733 log_dbg("Unknown UUID set, some parameters are not set.");
735 log_dbg("Active device has no UUID set, some parameters are not set.");
738 r = crypt_set_data_device(*cd, dmd.device);
743 /* Try to initialise basic parameters from active device */
745 if (!(*cd)->backing_file && dmd.device && crypt_loop_device(dmd.device) &&
746 !((*cd)->backing_file = crypt_loop_backing_file(dmd.device))) {
751 if (isPLAIN((*cd)->type)) {
752 (*cd)->plain_uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
753 (*cd)->plain_hdr.hash = NULL; /* no way to get this */
754 (*cd)->plain_hdr.offset = dmd.offset;
755 (*cd)->plain_hdr.skip = dmd.iv_offset;
756 (*cd)->plain_key_size = dmd.vk->keylength;
758 r = crypt_parse_name_and_mode(dmd.cipher, cipher, NULL, cipher_mode);
760 (*cd)->plain_cipher = strdup(cipher);
761 (*cd)->plain_cipher_mode = strdup(cipher_mode);
763 } else if (isLOOPAES((*cd)->type)) {
764 (*cd)->loopaes_uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
765 (*cd)->loopaes_hdr.offset = dmd.offset;
767 r = crypt_parse_name_and_mode(dmd.cipher, cipher,
768 &key_nums, cipher_mode);
770 (*cd)->loopaes_cipher = strdup(cipher);
771 (*cd)->loopaes_cipher_mode = strdup(cipher_mode);
772 /* version 3 uses last key for IV */
773 if (dmd.vk->keylength % key_nums)
775 (*cd)->loopaes_key_size = dmd.vk->keylength / key_nums;
777 } else if (isLUKS((*cd)->type)) {
778 if (mdata_device(*cd)) {
779 r = _crypt_load_luks1(*cd, 0, 0);
781 log_dbg("LUKS device header does not match active device.");
787 /* checks whether UUIDs match each other */
788 r = crypt_uuid_cmp(dmd.uuid, (*cd)->hdr.uuid);
790 log_dbg("LUKS device header uuid: %s mismatches DM returned uuid %s",
791 (*cd)->hdr.uuid, dmd.uuid);
805 crypt_free_volume_key(dmd.vk);
806 free(CONST_CAST(void*)dmd.device);
807 free(CONST_CAST(void*)dmd.cipher);
808 free(CONST_CAST(void*)dmd.uuid);
812 int crypt_init_by_name(struct crypt_device **cd, const char *name)
814 return crypt_init_by_name_and_header(cd, name, NULL);
817 static int _crypt_format_plain(struct crypt_device *cd,
819 const char *cipher_mode,
821 size_t volume_key_size,
822 struct crypt_params_plain *params)
824 if (!cipher || !cipher_mode) {
825 log_err(cd, _("Invalid plain crypt parameters.\n"));
829 if (volume_key_size > 1024) {
830 log_err(cd, _("Invalid key size.\n"));
834 cd->plain_key_size = volume_key_size;
835 cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
839 cd->plain_cipher = strdup(cipher);
840 cd->plain_cipher_mode = strdup(cipher_mode);
843 cd->plain_uuid = strdup(uuid);
845 if (params && params->hash)
846 cd->plain_hdr.hash = strdup(params->hash);
848 cd->plain_hdr.offset = params ? params->offset : 0;
849 cd->plain_hdr.skip = params ? params->skip : 0;
850 cd->plain_hdr.size = params ? params->size : 0;
852 if (!cd->plain_cipher || !cd->plain_cipher_mode)
858 static int _crypt_format_luks1(struct crypt_device *cd,
860 const char *cipher_mode,
862 const char *volume_key,
863 size_t volume_key_size,
864 struct crypt_params_luks1 *params)
867 unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
868 unsigned long alignment_offset = 0;
870 if (!mdata_device(cd)) {
871 log_err(cd, _("Can't format LUKS without device.\n"));
876 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
879 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
884 if (params && params->data_device) {
885 cd->metadata_device = cd->device;
886 if (!(cd->device = strdup(params->data_device)))
888 required_alignment = params->data_alignment * SECTOR_SIZE;
889 } else if (params && params->data_alignment) {
890 required_alignment = params->data_alignment * SECTOR_SIZE;
892 get_topology_alignment(cd->device, &required_alignment,
893 &alignment_offset, DEFAULT_DISK_ALIGNMENT);
895 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
896 (params && params->hash) ? params->hash : "sha1",
898 required_alignment / SECTOR_SIZE,
899 alignment_offset / SECTOR_SIZE,
900 cd->iteration_time, &cd->PBKDF2_per_sec,
901 cd->metadata_device, cd);
905 /* Wipe first 8 sectors - fs magic numbers etc. */
906 r = crypt_wipe(mdata_device(cd), 0, 8 * SECTOR_SIZE, CRYPT_WIPE_ZERO, 1);
909 log_err(cd, _("Cannot format device %s which is still in use.\n"),
912 log_err(cd, _("Cannot wipe header on device %s.\n"),
918 r = LUKS_write_phdr(mdata_device(cd), &cd->hdr, cd);
923 static int _crypt_format_loopaes(struct crypt_device *cd,
926 size_t volume_key_size,
927 struct crypt_params_loopaes *params)
929 if (!mdata_device(cd)) {
930 log_err(cd, _("Can't format LOOPAES without device.\n"));
934 if (volume_key_size > 1024) {
935 log_err(cd, _("Invalid key size.\n"));
939 cd->loopaes_key_size = volume_key_size;
941 cd->loopaes_cipher = strdup(cipher ?: DEFAULT_LOOPAES_CIPHER);
944 cd->loopaes_uuid = strdup(uuid);
946 if (params && params->hash)
947 cd->loopaes_hdr.hash = strdup(params->hash);
949 cd->loopaes_hdr.offset = params ? params->offset : 0;
950 cd->loopaes_hdr.skip = params ? params->skip : 0;
955 static int _crypt_format_verity(struct crypt_device *cd,
956 struct crypt_params_verity *params)
959 uint64_t data_device_size;
961 if (!mdata_device(cd)) {
962 log_err(cd, _("Can't format VERITY without device.\n"));
966 if (!params || !params->data_device)
969 if (params->version > 1)
973 cd->type = CRYPT_VERITY;
974 r = crypt_set_data_device(cd, params->data_device);
978 if (!params->data_size) {
979 r = device_size(params->data_device, &data_device_size);
983 cd->verity_hdr.data_size = data_device_size / params->data_block_size;
985 cd->verity_hdr.data_size = params->data_size;
988 cd->verity_root_hash_size = crypt_hash_size(params->hash_name);
989 if (!cd->verity_root_hash_size)
992 cd->verity_flags = params->flags;
993 cd->verity_root_hash = malloc(cd->verity_root_hash_size);
994 if (!cd->verity_root_hash)
997 cd->verity_hdr.hash_name = strdup(params->hash_name);
998 cd->verity_hdr.data_device = NULL;
999 cd->verity_hdr.data_block_size = params->data_block_size;
1000 cd->verity_hdr.hash_block_size = params->hash_block_size;
1001 cd->verity_hdr.hash_area_offset = params->hash_area_offset;
1002 cd->verity_hdr.version = params->version;
1003 cd->verity_hdr.flags = params->flags;
1004 cd->verity_hdr.salt_size = params->salt_size;
1005 cd->verity_hdr.salt = malloc(params->salt_size);
1007 memcpy(CONST_CAST(char*)cd->verity_hdr.salt, params->salt,
1010 r = crypt_random_get(cd, CONST_CAST(char*)cd->verity_hdr.salt,
1011 params->salt_size, CRYPT_RND_SALT);
1015 log_dbg("Creating verity hash on device %s.", mdata_device(cd));
1016 r = VERITY_create(cd, &cd->verity_hdr, cd->device, mdata_device(cd),
1017 cd->verity_root_hash, cd->verity_root_hash_size);
1021 r = VERITY_write_sb(cd, mdata_device(cd),
1022 cd->verity_hdr.hash_area_offset,
1026 free(cd->verity_root_hash);
1027 free(CONST_CAST(char*)cd->verity_hdr.hash_name);
1028 free(CONST_CAST(char*)cd->verity_hdr.salt);
1034 int crypt_format(struct crypt_device *cd,
1037 const char *cipher_mode,
1039 const char *volume_key,
1040 size_t volume_key_size,
1049 log_dbg("Context already formatted as %s.", cd->type);
1053 log_dbg("Formatting device %s as type %s.", mdata_device(cd) ?: "(none)", type);
1055 r = init_crypto(cd);
1060 r = _crypt_format_plain(cd, cipher, cipher_mode,
1061 uuid, volume_key_size, params);
1062 else if (isLUKS(type))
1063 r = _crypt_format_luks1(cd, cipher, cipher_mode,
1064 uuid, volume_key, volume_key_size, params);
1065 else if (isLOOPAES(type))
1066 r = _crypt_format_loopaes(cd, cipher, uuid, volume_key_size, params);
1067 else if (isVERITY(type))
1068 r = _crypt_format_verity(cd, params);
1070 /* FIXME: allow plugins here? */
1071 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
1075 if (!r && !(cd->type = strdup(type)))
1079 crypt_free_volume_key(cd->volume_key);
1080 cd->volume_key = NULL;
1086 int crypt_load(struct crypt_device *cd,
1087 const char *requested_type,
1092 log_dbg("Trying to load %s crypt type from device %s.",
1093 requested_type ?: "any", mdata_device(cd) ?: "(none)");
1095 if (!mdata_device(cd))
1098 if (!requested_type || isLUKS(requested_type)) {
1099 if (cd->type && !isLUKS(cd->type)) {
1100 log_dbg("Context is already initialised to type %s", cd->type);
1104 r = _crypt_load_luks1(cd, 1, 0);
1105 } else if (isVERITY(requested_type)) {
1106 if (cd->type && !isVERITY(cd->type)) {
1107 log_dbg("Context is already initialised to type %s", cd->type);
1110 r = _crypt_load_verity(cd, params);
1117 /* cd->type and header must be set in context */
1118 r = crypt_check_data_device_size(cd);
1127 int crypt_repair(struct crypt_device *cd,
1128 const char *requested_type,
1129 void *params __attribute__((unused)))
1133 log_dbg("Trying to repair %s crypt type from device %s.",
1134 requested_type ?: "any", mdata_device(cd) ?: "(none)");
1136 if (!mdata_device(cd))
1139 if (requested_type && !isLUKS(requested_type))
1143 /* Load with repair */
1144 r = _crypt_load_luks1(cd, 1, 1);
1148 /* cd->type and header must be set in context */
1149 r = crypt_check_data_device_size(cd);
1158 int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
1160 struct crypt_dm_active_device dmd;
1163 /* Device context type must be initialised */
1164 if (!cd->type || !crypt_get_uuid(cd))
1167 log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
1169 r = dm_query_device(name, DM_ACTIVE_DEVICE | DM_ACTIVE_CIPHER |
1170 DM_ACTIVE_UUID | DM_ACTIVE_KEYSIZE |
1171 DM_ACTIVE_KEY, &dmd);
1173 log_err(NULL, _("Device %s is not active.\n"), name);
1182 r = device_check_and_adjust(cd, dmd.device, DEV_OK, &new_size, &dmd.offset, &dmd.flags);
1186 if (new_size == dmd.size) {
1187 log_dbg("Device has already requested size %" PRIu64
1188 " sectors.", dmd.size);
1191 dmd.size = new_size;
1192 r = dm_create_device(name, cd->type, &dmd, 1);
1195 crypt_free_volume_key(dmd.vk);
1196 free(CONST_CAST(void*)dmd.cipher);
1197 free(CONST_CAST(void*)dmd.device);
1198 free(CONST_CAST(void*)dmd.uuid);
1203 int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
1205 if (!isLUKS(cd->type)) {
1206 log_err(cd, _("This operation is not supported for this device type.\n"));
1210 if (uuid && !strncmp(uuid, cd->hdr.uuid, sizeof(cd->hdr.uuid))) {
1211 log_dbg("UUID is the same as requested (%s) for device %s.",
1212 uuid, mdata_device(cd));
1217 log_dbg("Requested new UUID change to %s for %s.", uuid, mdata_device(cd));
1219 log_dbg("Requested new UUID refresh for %s.", mdata_device(cd));
1221 if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
1224 return LUKS_hdr_uuid_set(mdata_device(cd), &cd->hdr, uuid, cd);
1227 int crypt_header_backup(struct crypt_device *cd,
1228 const char *requested_type,
1229 const char *backup_file)
1233 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1236 r = init_crypto(cd);
1240 log_dbg("Requested header backup of device %s (%s) to "
1241 "file %s.", mdata_device(cd), requested_type, backup_file);
1243 return LUKS_hdr_backup(backup_file, mdata_device(cd), &cd->hdr, cd);
1246 int crypt_header_restore(struct crypt_device *cd,
1247 const char *requested_type,
1248 const char *backup_file)
1252 if (requested_type && !isLUKS(requested_type))
1255 /* Some hash functions need initialized gcrypt library */
1256 r = init_crypto(cd);
1260 log_dbg("Requested header restore to device %s (%s) from "
1261 "file %s.", mdata_device(cd), requested_type, backup_file);
1263 return LUKS_hdr_restore(backup_file, mdata_device(cd), &cd->hdr, cd);
1266 void crypt_free(struct crypt_device *cd)
1269 log_dbg("Releasing crypt device %s context.", mdata_device(cd));
1271 if (cd->loop_fd != -1)
1275 crypt_free_volume_key(cd->volume_key);
1278 free(cd->metadata_device);
1279 free(cd->backing_file);
1282 /* used in plain device only */
1283 free(CONST_CAST(void*)cd->plain_hdr.hash);
1284 free(cd->plain_cipher);
1285 free(cd->plain_cipher_mode);
1286 free(cd->plain_uuid);
1288 /* used in loop-AES device only */
1289 free(CONST_CAST(void*)cd->loopaes_hdr.hash);
1290 free(cd->loopaes_cipher);
1291 free(cd->loopaes_uuid);
1293 /* used in verity device only */
1294 free(CONST_CAST(void*)cd->verity_hdr.hash_name);
1295 free(CONST_CAST(void*)cd->verity_hdr.salt);
1296 free(cd->verity_root_hash);
1302 int crypt_suspend(struct crypt_device *cd,
1305 crypt_status_info ci;
1308 log_dbg("Suspending volume %s.", name);
1310 if (!isLUKS(cd->type)) {
1311 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1316 ci = crypt_status(NULL, name);
1317 if (ci < CRYPT_ACTIVE) {
1318 log_err(cd, _("Volume %s is not active.\n"), name);
1322 if (!cd && dm_init(NULL, 1) < 0)
1325 r = dm_status_suspended(name);
1330 log_err(cd, _("Volume %s is already suspended.\n"), name);
1335 r = dm_suspend_and_wipe_key(name);
1337 log_err(cd, "Suspend is not supported for device %s.\n", name);
1339 log_err(cd, "Error during suspending device %s.\n", name);
1346 int crypt_resume_by_passphrase(struct crypt_device *cd,
1349 const char *passphrase,
1350 size_t passphrase_size)
1352 struct volume_key *vk = NULL;
1355 log_dbg("Resuming volume %s.", name);
1357 if (!isLUKS(cd->type)) {
1358 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1363 r = dm_status_suspended(name);
1368 log_err(cd, _("Volume %s is not suspended.\n"), name);
1373 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase,
1374 passphrase_size, &cd->hdr, &vk, cd);
1376 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1380 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1382 log_err(cd, "Resume is not supported for device %s.\n", name);
1384 log_err(cd, "Error during resuming device %s.\n", name);
1388 crypt_free_volume_key(vk);
1389 return r < 0 ? r : keyslot;
1392 int crypt_resume_by_keyfile_offset(struct crypt_device *cd,
1395 const char *keyfile,
1396 size_t keyfile_size,
1397 size_t keyfile_offset)
1399 struct volume_key *vk = NULL;
1400 char *passphrase_read = NULL;
1401 size_t passphrase_size_read;
1404 log_dbg("Resuming volume %s.", name);
1406 if (!isLUKS(cd->type)) {
1407 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1412 r = dm_status_suspended(name);
1417 log_err(cd, _("Volume %s is not suspended.\n"), name);
1424 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1425 &passphrase_size_read, keyfile, keyfile_offset,
1430 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase_read,
1431 passphrase_size_read, &cd->hdr, &vk, cd);
1436 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1438 log_err(cd, "Error during resuming device %s.\n", name);
1440 crypt_safe_free(passphrase_read);
1441 crypt_free_volume_key(vk);
1442 return r < 0 ? r : keyslot;
1445 int crypt_resume_by_keyfile(struct crypt_device *cd,
1448 const char *keyfile,
1449 size_t keyfile_size)
1451 return crypt_resume_by_keyfile_offset(cd, name, keyslot,
1452 keyfile, keyfile_size, 0);
1455 // slot manipulation
1456 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1457 int keyslot, // -1 any
1458 const char *passphrase, // NULL -> terminal
1459 size_t passphrase_size,
1460 const char *new_passphrase, // NULL -> terminal
1461 size_t new_passphrase_size)
1463 struct volume_key *vk = NULL;
1464 char *password = NULL, *new_password = NULL;
1465 size_t passwordLen, new_passwordLen;
1468 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1469 "new passphrase %sprovided.",
1470 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1472 if (!isLUKS(cd->type)) {
1473 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1477 r = keyslot_verify_or_find_empty(cd, &keyslot);
1481 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1482 /* No slots used, try to use pre-generated key in header */
1483 if (cd->volume_key) {
1484 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1485 r = vk ? 0 : -ENOMEM;
1487 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1490 } else if (passphrase) {
1491 /* Passphrase provided, use it to unlock existing keyslot */
1492 r = LUKS_open_key_with_hdr(mdata_device(cd), CRYPT_ANY_SLOT, passphrase,
1493 passphrase_size, &cd->hdr, &vk, cd);
1495 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1496 r = key_from_terminal(cd, _("Enter any passphrase: "),
1497 &password, &passwordLen, 0);
1501 r = LUKS_open_key_with_hdr(mdata_device(cd), CRYPT_ANY_SLOT, password,
1502 passwordLen, &cd->hdr, &vk, cd);
1503 crypt_safe_free(password);
1509 if (new_passphrase) {
1510 new_password = CONST_CAST(char*)new_passphrase;
1511 new_passwordLen = new_passphrase_size;
1513 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1514 &new_password, &new_passwordLen, 1);
1519 r = LUKS_set_key(mdata_device(cd), keyslot, new_password, new_passwordLen,
1520 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1525 if (!new_passphrase)
1526 crypt_safe_free(new_password);
1527 crypt_free_volume_key(vk);
1528 return r ?: keyslot;
1531 int crypt_keyslot_add_by_keyfile_offset(struct crypt_device *cd,
1533 const char *keyfile,
1534 size_t keyfile_size,
1535 size_t keyfile_offset,
1536 const char *new_keyfile,
1537 size_t new_keyfile_size,
1538 size_t new_keyfile_offset)
1540 struct volume_key *vk = NULL;
1541 char *password = NULL; size_t passwordLen;
1542 char *new_password = NULL; size_t new_passwordLen;
1545 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1546 keyfile ?: "[none]", new_keyfile ?: "[none]");
1548 if (!isLUKS(cd->type)) {
1549 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1553 r = keyslot_verify_or_find_empty(cd, &keyslot);
1557 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1558 /* No slots used, try to use pre-generated key in header */
1559 if (cd->volume_key) {
1560 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1561 r = vk ? 0 : -ENOMEM;
1563 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1567 /* Read password from file of (if NULL) from terminal */
1569 r = key_from_file(cd, _("Enter any passphrase: "),
1570 &password, &passwordLen,
1571 keyfile, keyfile_offset, keyfile_size);
1573 r = key_from_terminal(cd, _("Enter any passphrase: "),
1574 &password, &passwordLen, 0);
1578 r = LUKS_open_key_with_hdr(mdata_device(cd), CRYPT_ANY_SLOT, password, passwordLen,
1586 r = key_from_file(cd, _("Enter new passphrase for key slot: "),
1587 &new_password, &new_passwordLen, new_keyfile,
1588 new_keyfile_offset, new_keyfile_size);
1590 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1591 &new_password, &new_passwordLen, 1);
1595 r = LUKS_set_key(mdata_device(cd), keyslot, new_password, new_passwordLen,
1596 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1598 crypt_safe_free(password);
1599 crypt_safe_free(new_password);
1600 crypt_free_volume_key(vk);
1601 return r < 0 ? r : keyslot;
1604 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1606 const char *keyfile,
1607 size_t keyfile_size,
1608 const char *new_keyfile,
1609 size_t new_keyfile_size)
1611 return crypt_keyslot_add_by_keyfile_offset(cd, keyslot,
1612 keyfile, keyfile_size, 0,
1613 new_keyfile, new_keyfile_size, 0);
1616 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1618 const char *volume_key,
1619 size_t volume_key_size,
1620 const char *passphrase,
1621 size_t passphrase_size)
1623 struct volume_key *vk = NULL;
1625 char *new_password = NULL; size_t new_passwordLen;
1627 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1629 if (!isLUKS(cd->type)) {
1630 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1635 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1636 else if (cd->volume_key)
1637 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1642 r = LUKS_verify_volume_key(&cd->hdr, vk);
1644 log_err(cd, _("Volume key does not match the volume.\n"));
1648 r = keyslot_verify_or_find_empty(cd, &keyslot);
1653 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1654 &new_password, &new_passwordLen, 1);
1657 passphrase = new_password;
1658 passphrase_size = new_passwordLen;
1661 r = LUKS_set_key(mdata_device(cd), keyslot, passphrase, passphrase_size,
1662 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1664 crypt_safe_free(new_password);
1665 crypt_free_volume_key(vk);
1666 return (r < 0) ? r : keyslot;
1669 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1671 crypt_keyslot_info ki;
1673 log_dbg("Destroying keyslot %d.", keyslot);
1675 if (!isLUKS(cd->type)) {
1676 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1680 ki = crypt_keyslot_status(cd, keyslot);
1681 if (ki == CRYPT_SLOT_INVALID) {
1682 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1686 if (ki == CRYPT_SLOT_INACTIVE) {
1687 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1691 return LUKS_del_key(mdata_device(cd), keyslot, &cd->hdr, cd);
1694 // activation/deactivation of device mapping
1695 int crypt_activate_by_passphrase(struct crypt_device *cd,
1698 const char *passphrase,
1699 size_t passphrase_size,
1702 crypt_status_info ci;
1703 struct volume_key *vk = NULL;
1704 char *read_passphrase = NULL;
1705 size_t passphraseLen = 0;
1708 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1709 name ? "Activating" : "Checking", name ?: "",
1710 keyslot, passphrase ? "" : "[none] ");
1713 ci = crypt_status(NULL, name);
1714 if (ci == CRYPT_INVALID)
1716 else if (ci >= CRYPT_ACTIVE) {
1717 log_err(cd, _("Device %s already exists.\n"), name);
1722 /* plain, use hashed passphrase */
1723 if (isPLAIN(cd->type)) {
1728 r = key_from_terminal(cd, NULL, &read_passphrase,
1732 passphrase = read_passphrase;
1733 passphrase_size = passphraseLen;
1736 r = process_key(cd, cd->plain_hdr.hash,
1738 passphrase, passphrase_size, &vk);
1742 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1744 } else if (isLUKS(cd->type)) {
1745 /* provided passphrase, do not retry */
1747 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase,
1748 passphrase_size, &cd->hdr, &vk, cd);
1750 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1755 r = LUKS1_activate(cd, name, vk, flags);
1760 crypt_safe_free(read_passphrase);
1761 crypt_free_volume_key(vk);
1763 return r < 0 ? r : keyslot;
1766 int crypt_activate_by_keyfile_offset(struct crypt_device *cd,
1769 const char *keyfile,
1770 size_t keyfile_size,
1771 size_t keyfile_offset,
1774 crypt_status_info ci;
1775 struct volume_key *vk = NULL;
1776 char *passphrase_read = NULL;
1777 size_t passphrase_size_read;
1778 unsigned int key_count = 0;
1781 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1782 name ?: "", keyslot, keyfile ?: "[none]");
1785 ci = crypt_status(NULL, name);
1786 if (ci == CRYPT_INVALID)
1788 else if (ci >= CRYPT_ACTIVE) {
1789 log_err(cd, _("Device %s already exists.\n"), name);
1797 if (isPLAIN(cd->type)) {
1801 r = key_from_file(cd, _("Enter passphrase: "),
1802 &passphrase_read, &passphrase_size_read,
1803 keyfile, keyfile_offset, keyfile_size);
1807 r = process_key(cd, cd->plain_hdr.hash,
1809 passphrase_read, passphrase_size_read, &vk);
1813 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1814 } else if (isLUKS(cd->type)) {
1815 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1816 &passphrase_size_read, keyfile, keyfile_offset, keyfile_size);
1819 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase_read,
1820 passphrase_size_read, &cd->hdr, &vk, cd);
1826 r = LUKS1_activate(cd, name, vk, flags);
1831 } else if (isLOOPAES(cd->type)) {
1832 r = key_from_file(cd, NULL, &passphrase_read, &passphrase_size_read,
1833 keyfile, keyfile_offset, keyfile_size);
1836 r = LOOPAES_parse_keyfile(cd, &vk, cd->loopaes_hdr.hash, &key_count,
1837 passphrase_read, passphrase_size_read);
1841 r = LOOPAES_activate(cd, name, cd->loopaes_cipher,
1842 key_count, vk, flags);
1847 crypt_safe_free(passphrase_read);
1848 crypt_free_volume_key(vk);
1853 int crypt_activate_by_keyfile(struct crypt_device *cd,
1856 const char *keyfile,
1857 size_t keyfile_size,
1860 return crypt_activate_by_keyfile_offset(cd, name, keyslot, keyfile,
1861 keyfile_size, 0, flags);
1864 int crypt_activate_by_volume_key(struct crypt_device *cd,
1866 const char *volume_key,
1867 size_t volume_key_size,
1870 crypt_status_info ci;
1871 struct volume_key *vk = NULL;
1874 log_dbg("Activating volume %s by volume key.", name);
1877 ci = crypt_status(NULL, name);
1878 if (ci == CRYPT_INVALID)
1880 else if (ci >= CRYPT_ACTIVE) {
1881 log_err(cd, _("Device %s already exists.\n"), name);
1886 /* use key directly, no hash */
1887 if (isPLAIN(cd->type)) {
1891 if (!volume_key || !volume_key_size || volume_key_size != cd->plain_key_size) {
1892 log_err(cd, _("Incorrect volume key specified for plain device.\n"));
1896 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1900 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1901 } else if (isLUKS(cd->type)) {
1902 /* If key is not provided, try to use internal key */
1904 if (!cd->volume_key) {
1905 log_err(cd, _("Volume key does not match the volume.\n"));
1908 volume_key_size = cd->volume_key->keylength;
1909 volume_key = cd->volume_key->key;
1912 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1915 r = LUKS_verify_volume_key(&cd->hdr, vk);
1918 log_err(cd, _("Volume key does not match the volume.\n"));
1921 r = LUKS1_activate(cd, name, vk, flags);
1922 } else if (isVERITY(cd->type)) {
1923 /* volume_key == root hash */
1924 if (!volume_key || !volume_key_size) {
1925 log_err(cd, _("Incorrect root hash specified for verity device.\n"));
1929 r = VERITY_activate(cd, name, mdata_device(cd),
1930 volume_key, volume_key_size,
1931 &cd->verity_hdr, cd->verity_flags);
1934 free(cd->verity_root_hash);
1935 cd->verity_root_hash = NULL;
1937 cd->verity_root_hash_size = volume_key_size;
1938 if (!cd->verity_root_hash)
1939 cd->verity_root_hash = malloc(volume_key_size);
1940 if (cd->verity_root_hash)
1941 memcpy(cd->verity_root_hash, volume_key, volume_key_size);
1944 log_err(cd, _("Device type is not properly initialised.\n"));
1946 crypt_free_volume_key(vk);
1951 int crypt_deactivate(struct crypt_device *cd, const char *name)
1958 log_dbg("Deactivating volume %s.", name);
1960 if (!cd && dm_init(NULL, 1) < 0)
1963 switch (crypt_status(cd, name)) {
1966 r = dm_remove_device(name, 0, 0);
1968 case CRYPT_INACTIVE:
1969 log_err(cd, _("Device %s is not active.\n"), name);
1973 log_err(cd, _("Invalid device %s.\n"), name);
1983 int crypt_volume_key_get(struct crypt_device *cd,
1986 size_t *volume_key_size,
1987 const char *passphrase,
1988 size_t passphrase_size)
1990 struct volume_key *vk = NULL;
1994 if (crypt_fips_mode()) {
1995 log_err(cd, "Function not available in FIPS mode.\n");
1999 key_len = crypt_get_volume_key_size(cd);
2000 if (key_len > *volume_key_size) {
2001 log_err(cd, _("Volume key buffer too small.\n"));
2005 if (isPLAIN(cd->type) && cd->plain_hdr.hash) {
2006 r = process_key(cd, cd->plain_hdr.hash, key_len,
2007 passphrase, passphrase_size, &vk);
2009 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
2010 } else if (isLUKS(cd->type)) {
2011 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase,
2012 passphrase_size, &cd->hdr, &vk, cd);
2015 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
2018 memcpy(volume_key, vk->key, vk->keylength);
2019 *volume_key_size = vk->keylength;
2022 crypt_free_volume_key(vk);
2026 int crypt_volume_key_verify(struct crypt_device *cd,
2027 const char *volume_key,
2028 size_t volume_key_size)
2030 struct volume_key *vk;
2033 if (!isLUKS(cd->type)) {
2034 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2038 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2042 r = LUKS_verify_volume_key(&cd->hdr, vk);
2045 log_err(cd, _("Volume key does not match the volume.\n"));
2047 crypt_free_volume_key(vk);
2052 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
2054 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
2055 cd->timeout = timeout_sec;
2058 void crypt_set_password_retry(struct crypt_device *cd, int tries)
2060 log_dbg("Password retry count set to %d.", tries);
2064 void crypt_set_iteration_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2066 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
2067 cd->iteration_time = iteration_time_ms;
2069 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2071 crypt_set_iteration_time(cd, iteration_time_ms);
2074 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
2076 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
2077 cd->password_verify = password_verify ? 1 : 0;
2080 void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
2083 case CRYPT_RNG_URANDOM:
2084 case CRYPT_RNG_RANDOM:
2085 log_dbg("RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
2086 cd->rng_type = rng_type;
2090 int crypt_get_rng_type(struct crypt_device *cd)
2095 return cd->rng_type;
2098 int crypt_memory_lock(struct crypt_device *cd, int lock)
2100 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
2104 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
2108 if (!cd && dm_init(NULL, 1) < 0)
2109 return CRYPT_INVALID;
2111 r = dm_status_device(name);
2116 if (r < 0 && r != -ENODEV)
2117 return CRYPT_INVALID;
2120 return CRYPT_ACTIVE;
2125 return CRYPT_INACTIVE;
2128 static void hexprint(struct crypt_device *cd, const char *d, int n, const char *sep)
2131 for(i = 0; i < n; i++)
2132 log_std(cd, "%02hhx%s", (const char)d[i], sep);
2135 static int _luks_dump(struct crypt_device *cd)
2139 log_std(cd, "LUKS header information for %s\n\n", mdata_device(cd));
2140 log_std(cd, "Version: \t%d\n", cd->hdr.version);
2141 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
2142 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
2143 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec);
2144 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
2145 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
2146 log_std(cd, "MK digest: \t");
2147 hexprint(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE, " ");
2149 log_std(cd, "MK salt: \t");
2150 hexprint(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2, " ");
2151 log_std(cd, "\n \t");
2152 hexprint(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ");
2154 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
2155 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
2156 for(i = 0; i < LUKS_NUMKEYS; i++) {
2157 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
2158 log_std(cd, "Key Slot %d: ENABLED\n",i);
2159 log_std(cd, "\tIterations: \t%d\n",
2160 cd->hdr.keyblock[i].passwordIterations);
2161 log_std(cd, "\tSalt: \t");
2162 hexprint(cd, cd->hdr.keyblock[i].passwordSalt,
2163 LUKS_SALTSIZE/2, " ");
2164 log_std(cd, "\n\t \t");
2165 hexprint(cd, cd->hdr.keyblock[i].passwordSalt +
2166 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ");
2169 log_std(cd, "\tKey material offset:\t%d\n",
2170 cd->hdr.keyblock[i].keyMaterialOffset);
2171 log_std(cd, "\tAF stripes: \t%d\n",
2172 cd->hdr.keyblock[i].stripes);
2175 log_std(cd, "Key Slot %d: DISABLED\n", i);
2180 static int _verity_dump(struct crypt_device *cd)
2182 log_std(cd, "VERITY header information for %s\n", mdata_device(cd));
2183 log_std(cd, "Version: \t%u\n", cd->verity_hdr.version);
2184 log_std(cd, "Data blocks: \t%" PRIu64 "\n", cd->verity_hdr.data_size);
2185 log_std(cd, "Data block size: \t%u\n", cd->verity_hdr.data_block_size);
2186 log_std(cd, "Hash block size: \t%u\n", cd->verity_hdr.hash_block_size);
2187 log_std(cd, "Hash algorithm: \t%s\n", cd->verity_hdr.hash_name);
2188 log_std(cd, "Salt: \t");
2189 if (cd->verity_hdr.salt_size)
2190 hexprint(cd, cd->verity_hdr.salt, cd->verity_hdr.salt_size, "");
2194 if (cd->verity_root_hash) {
2195 log_std(cd, "Root hash: \t");
2196 hexprint(cd, cd->verity_root_hash, cd->verity_root_hash_size, "");
2202 int crypt_dump(struct crypt_device *cd)
2204 if (isLUKS(cd->type))
2205 return _luks_dump(cd);
2206 else if (isVERITY(cd->type))
2207 return _verity_dump(cd);
2209 log_err(cd, _("Dump operation is not supported for this device type.\n"));
2213 const char *crypt_get_cipher(struct crypt_device *cd)
2215 if (isPLAIN(cd->type))
2216 return cd->plain_cipher;
2218 if (isLUKS(cd->type))
2219 return cd->hdr.cipherName;
2221 if (isLOOPAES(cd->type))
2222 return cd->loopaes_cipher;
2227 const char *crypt_get_cipher_mode(struct crypt_device *cd)
2229 if (isPLAIN(cd->type))
2230 return cd->plain_cipher_mode;
2232 if (isLUKS(cd->type))
2233 return cd->hdr.cipherMode;
2235 if (isLOOPAES(cd->type))
2236 return cd->loopaes_cipher_mode;
2241 const char *crypt_get_uuid(struct crypt_device *cd)
2243 if (isLUKS(cd->type))
2244 return cd->hdr.uuid;
2246 if (isPLAIN(cd->type))
2247 return cd->plain_uuid;
2249 if (isLOOPAES(cd->type))
2250 return cd->loopaes_uuid;
2255 const char *crypt_get_device_name(struct crypt_device *cd)
2260 int crypt_get_volume_key_size(struct crypt_device *cd)
2262 if (isPLAIN(cd->type))
2263 return cd->plain_key_size;
2265 if (isLUKS(cd->type))
2266 return cd->hdr.keyBytes;
2268 if (isLOOPAES(cd->type))
2269 return cd->loopaes_key_size;
2271 if (isVERITY(cd->type))
2272 return cd->verity_root_hash_size;
2277 uint64_t crypt_get_data_offset(struct crypt_device *cd)
2279 if (isPLAIN(cd->type))
2280 return cd->plain_hdr.offset;
2282 if (isLUKS(cd->type))
2283 return cd->hdr.payloadOffset;
2285 if (isLOOPAES(cd->type))
2286 return cd->loopaes_hdr.offset;
2291 uint64_t crypt_get_iv_offset(struct crypt_device *cd)
2293 if (isPLAIN(cd->type))
2294 return cd->plain_hdr.skip;
2296 if (isLUKS(cd->type))
2299 if (isLOOPAES(cd->type))
2300 return cd->loopaes_hdr.skip;
2305 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
2307 if (!isLUKS(cd->type)) {
2308 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2309 return CRYPT_SLOT_INVALID;
2312 return LUKS_keyslot_info(&cd->hdr, keyslot);
2315 int crypt_keyslot_max(const char *type)
2317 if (type && isLUKS(type))
2318 return LUKS_NUMKEYS;
2323 const char *crypt_get_type(struct crypt_device *cd)
2328 int crypt_get_active_device(struct crypt_device *cd __attribute__((unused)),
2330 struct crypt_active_device *cad)
2332 struct crypt_dm_active_device dmd;
2335 r = dm_query_device(name, 0, &dmd);
2339 cad->offset = dmd.offset;
2340 cad->iv_offset = dmd.iv_offset;
2341 cad->size = dmd.size;
2342 cad->flags = dmd.flags;