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 char *verity_root_hash;
71 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;
658 if (params->flags & CRYPT_VERITY_NO_HEADER)
662 sb_offset = params->hash_area_offset;
664 r = VERITY_read_sb(cd, mdata_device(cd), sb_offset,
665 &cd->verity_uuid, &cd->verity_hdr);
670 cd->verity_hdr.flags = params->flags;
672 if (params && params->data_device &&
673 (r = crypt_set_data_device(cd, params->data_device)) < 0)
676 cd->verity_root_hash_size = crypt_hash_size(cd->verity_hdr.hash_name);
678 if (!cd->type && !(cd->type = strdup(CRYPT_VERITY)))
684 static int _init_by_name_crypt(struct crypt_device *cd, const char *name)
686 struct crypt_dm_active_device dmd = {};
687 char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
690 r = dm_query_device(name, DM_ACTIVE_DEVICE |
692 DM_ACTIVE_CRYPT_CIPHER |
693 DM_ACTIVE_CRYPT_KEYSIZE, &dmd);
697 if (isPLAIN(cd->type)) {
698 cd->plain_uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
699 cd->plain_hdr.hash = NULL; /* no way to get this */
700 cd->plain_hdr.offset = dmd.u.crypt.offset;
701 cd->plain_hdr.skip = dmd.u.crypt.iv_offset;
702 cd->plain_key_size = dmd.u.crypt.vk->keylength;
704 r = crypt_parse_name_and_mode(dmd.u.crypt.cipher, cipher, NULL, cipher_mode);
706 cd->plain_cipher = strdup(cipher);
707 cd->plain_cipher_mode = strdup(cipher_mode);
709 } else if (isLOOPAES(cd->type)) {
710 cd->loopaes_uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
711 cd->loopaes_hdr.offset = dmd.u.crypt.offset;
713 r = crypt_parse_name_and_mode(dmd.u.crypt.cipher, cipher,
714 &key_nums, cipher_mode);
716 cd->loopaes_cipher = strdup(cipher);
717 cd->loopaes_cipher_mode = strdup(cipher_mode);
718 /* version 3 uses last key for IV */
719 if (dmd.u.crypt.vk->keylength % key_nums)
721 cd->loopaes_key_size = dmd.u.crypt.vk->keylength / key_nums;
723 } else if (isLUKS(cd->type)) {
724 if (mdata_device(cd)) {
725 r = _crypt_load_luks1(cd, 0, 0);
727 log_dbg("LUKS device header does not match active device.");
733 /* check whether UUIDs match each other */
734 r = crypt_uuid_cmp(dmd.uuid, cd->hdr.uuid);
736 log_dbg("LUKS device header uuid: %s mismatches DM returned uuid %s",
737 cd->hdr.uuid, dmd.uuid);
746 crypt_free_volume_key(dmd.u.crypt.vk);
747 free(CONST_CAST(void*)dmd.u.crypt.cipher);
748 free(CONST_CAST(void*)dmd.data_device);
749 free(CONST_CAST(void*)dmd.uuid);
753 static int _init_by_name_verity(struct crypt_device *cd, const char *name)
755 struct crypt_params_verity params = {};
756 struct crypt_dm_active_device dmd = {
758 .u.verity.vp = ¶ms,
762 r = dm_query_device(name, DM_ACTIVE_DEVICE |
764 DM_ACTIVE_VERITY_HASH_DEVICE |
765 DM_ACTIVE_VERITY_PARAMS, &dmd);
769 if (isVERITY(cd->type)) {
770 cd->verity_uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
771 cd->verity_hdr.flags = CRYPT_VERITY_NO_HEADER; //FIXME
772 cd->verity_hdr.data_size = params.data_size;
773 cd->verity_root_hash_size = dmd.u.verity.root_hash_size;
774 cd->verity_root_hash = NULL;
775 cd->verity_hdr.hash_name = params.hash_name;
776 cd->verity_hdr.data_device = NULL;
777 cd->verity_hdr.hash_device = NULL;
778 cd->verity_hdr.data_block_size = params.data_block_size;
779 cd->verity_hdr.hash_block_size = params.hash_block_size;
780 cd->verity_hdr.hash_area_offset = dmd.u.verity.hash_offset;
781 cd->verity_hdr.hash_type = params.hash_type;
782 cd->verity_hdr.flags = params.flags;
783 cd->verity_hdr.salt_size = params.salt_size;
784 cd->verity_hdr.salt = params.salt;
785 if (!(cd->metadata_device = strdup(dmd.u.verity.hash_device)))
789 free(CONST_CAST(void*)dmd.u.verity.hash_device);
790 free(CONST_CAST(void*)dmd.data_device);
791 free(CONST_CAST(void*)dmd.uuid);
795 int crypt_init_by_name_and_header(struct crypt_device **cd,
797 const char *header_device)
799 crypt_status_info ci;
800 struct crypt_dm_active_device dmd;
803 log_dbg("Allocating crypt device context by device %s.", name);
805 ci = crypt_status(NULL, name);
806 if (ci == CRYPT_INVALID)
809 if (ci < CRYPT_ACTIVE) {
810 log_err(NULL, _("Device %s is not active.\n"), name);
814 r = dm_query_device(name, DM_ACTIVE_DEVICE | DM_ACTIVE_UUID, &dmd);
821 r = crypt_init(cd, header_device);
823 r = crypt_init(cd, dmd.data_device);
825 /* Underlying device disappeared but mapping still active */
826 if (!dmd.data_device || r == -ENOTBLK)
827 log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
830 /* Underlying device is not readable but crypt mapping exists */
832 free(CONST_CAST(void*)dmd.data_device);
833 dmd.data_device = NULL;
834 r = crypt_init(cd, NULL);
842 if (!strncmp(CRYPT_PLAIN, dmd.uuid, sizeof(CRYPT_PLAIN)-1))
843 (*cd)->type = strdup(CRYPT_PLAIN);
844 else if (!strncmp(CRYPT_LOOPAES, dmd.uuid, sizeof(CRYPT_LOOPAES)-1))
845 (*cd)->type = strdup(CRYPT_LOOPAES);
846 else if (!strncmp(CRYPT_LUKS1, dmd.uuid, sizeof(CRYPT_LUKS1)-1))
847 (*cd)->type = strdup(CRYPT_LUKS1);
848 else if (!strncmp(CRYPT_VERITY, dmd.uuid, sizeof(CRYPT_VERITY)-1))
849 (*cd)->type = strdup(CRYPT_VERITY);
851 log_dbg("Unknown UUID set, some parameters are not set.");
853 log_dbg("Active device has no UUID set, some parameters are not set.");
856 r = crypt_set_data_device(*cd, dmd.data_device);
861 /* Try to initialise basic parameters from active device */
863 if (!(*cd)->backing_file && dmd.data_device &&
864 crypt_loop_device(dmd.data_device) &&
865 !((*cd)->backing_file = crypt_loop_backing_file(dmd.data_device))) {
870 if (dmd.target == DM_CRYPT)
871 r = _init_by_name_crypt(*cd, name);
872 else if (dmd.target == DM_VERITY)
873 r = _init_by_name_verity(*cd, name);
879 free(CONST_CAST(void*)dmd.data_device);
880 free(CONST_CAST(void*)dmd.uuid);
884 int crypt_init_by_name(struct crypt_device **cd, const char *name)
886 return crypt_init_by_name_and_header(cd, name, NULL);
889 static int _crypt_format_plain(struct crypt_device *cd,
891 const char *cipher_mode,
893 size_t volume_key_size,
894 struct crypt_params_plain *params)
896 if (!cipher || !cipher_mode) {
897 log_err(cd, _("Invalid plain crypt parameters.\n"));
901 if (volume_key_size > 1024) {
902 log_err(cd, _("Invalid key size.\n"));
906 cd->plain_key_size = volume_key_size;
907 cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
911 cd->plain_cipher = strdup(cipher);
912 cd->plain_cipher_mode = strdup(cipher_mode);
915 cd->plain_uuid = strdup(uuid);
917 if (params && params->hash)
918 cd->plain_hdr.hash = strdup(params->hash);
920 cd->plain_hdr.offset = params ? params->offset : 0;
921 cd->plain_hdr.skip = params ? params->skip : 0;
922 cd->plain_hdr.size = params ? params->size : 0;
924 if (!cd->plain_cipher || !cd->plain_cipher_mode)
930 static int _crypt_format_luks1(struct crypt_device *cd,
932 const char *cipher_mode,
934 const char *volume_key,
935 size_t volume_key_size,
936 struct crypt_params_luks1 *params)
939 unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
940 unsigned long alignment_offset = 0;
942 if (!mdata_device(cd)) {
943 log_err(cd, _("Can't format LUKS without device.\n"));
948 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
951 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
956 if (params && params->data_device) {
957 cd->metadata_device = cd->device;
958 if (!(cd->device = strdup(params->data_device)))
960 required_alignment = params->data_alignment * SECTOR_SIZE;
961 } else if (params && params->data_alignment) {
962 required_alignment = params->data_alignment * SECTOR_SIZE;
964 get_topology_alignment(cd->device, &required_alignment,
965 &alignment_offset, DEFAULT_DISK_ALIGNMENT);
967 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
968 (params && params->hash) ? params->hash : "sha1",
970 required_alignment / SECTOR_SIZE,
971 alignment_offset / SECTOR_SIZE,
972 cd->iteration_time, &cd->PBKDF2_per_sec,
973 cd->metadata_device, cd);
977 /* Wipe first 8 sectors - fs magic numbers etc. */
978 r = crypt_wipe(mdata_device(cd), 0, 8 * SECTOR_SIZE, CRYPT_WIPE_ZERO, 1);
981 log_err(cd, _("Cannot format device %s which is still in use.\n"),
984 log_err(cd, _("Cannot wipe header on device %s.\n"),
990 r = LUKS_write_phdr(mdata_device(cd), &cd->hdr, cd);
995 static int _crypt_format_loopaes(struct crypt_device *cd,
998 size_t volume_key_size,
999 struct crypt_params_loopaes *params)
1001 if (!mdata_device(cd)) {
1002 log_err(cd, _("Can't format LOOPAES without device.\n"));
1006 if (volume_key_size > 1024) {
1007 log_err(cd, _("Invalid key size.\n"));
1011 cd->loopaes_key_size = volume_key_size;
1013 cd->loopaes_cipher = strdup(cipher ?: DEFAULT_LOOPAES_CIPHER);
1016 cd->loopaes_uuid = strdup(uuid);
1018 if (params && params->hash)
1019 cd->loopaes_hdr.hash = strdup(params->hash);
1021 cd->loopaes_hdr.offset = params ? params->offset : 0;
1022 cd->loopaes_hdr.skip = params ? params->skip : 0;
1027 static int _crypt_format_verity(struct crypt_device *cd,
1029 struct crypt_params_verity *params)
1032 uint64_t data_device_size;
1034 if (!mdata_device(cd)) {
1035 log_err(cd, _("Can't format VERITY without device.\n"));
1039 if (!params || !params->data_device)
1042 /* set data device */
1043 cd->type = CRYPT_VERITY;
1044 r = crypt_set_data_device(cd, params->data_device);
1048 if (!params->data_size) {
1049 r = device_size(params->data_device, &data_device_size);
1053 cd->verity_hdr.data_size = data_device_size / params->data_block_size;
1055 cd->verity_hdr.data_size = params->data_size;
1057 cd->verity_root_hash_size = crypt_hash_size(params->hash_name);
1058 if (!cd->verity_root_hash_size)
1061 cd->verity_root_hash = malloc(cd->verity_root_hash_size);
1062 if (!cd->verity_root_hash)
1065 cd->verity_hdr.flags = params->flags;
1066 cd->verity_hdr.hash_name = strdup(params->hash_name);
1067 cd->verity_hdr.data_device = NULL;
1068 cd->verity_hdr.data_block_size = params->data_block_size;
1069 cd->verity_hdr.hash_block_size = params->hash_block_size;
1070 cd->verity_hdr.hash_area_offset = params->hash_area_offset;
1071 cd->verity_hdr.hash_type = params->hash_type;
1072 cd->verity_hdr.flags = params->flags;
1073 cd->verity_hdr.salt_size = params->salt_size;
1074 cd->verity_hdr.salt = malloc(params->salt_size);
1076 memcpy(CONST_CAST(char*)cd->verity_hdr.salt, params->salt,
1079 r = crypt_random_get(cd, CONST_CAST(char*)cd->verity_hdr.salt,
1080 params->salt_size, CRYPT_RND_SALT);
1084 if (params->flags & CRYPT_VERITY_CREATE_HASH) {
1085 r = VERITY_create(cd, &cd->verity_hdr, cd->device, mdata_device(cd),
1086 cd->verity_root_hash, cd->verity_root_hash_size);
1091 if (!(params->flags & CRYPT_VERITY_NO_HEADER)) {
1093 cd->verity_uuid = strdup(uuid);
1095 r = VERITY_UUID_generate(cd, &cd->verity_uuid);
1100 r = VERITY_write_sb(cd, mdata_device(cd),
1101 cd->verity_hdr.hash_area_offset,
1108 int crypt_format(struct crypt_device *cd,
1111 const char *cipher_mode,
1113 const char *volume_key,
1114 size_t volume_key_size,
1123 log_dbg("Context already formatted as %s.", cd->type);
1127 log_dbg("Formatting device %s as type %s.", mdata_device(cd) ?: "(none)", type);
1129 r = init_crypto(cd);
1134 r = _crypt_format_plain(cd, cipher, cipher_mode,
1135 uuid, volume_key_size, params);
1136 else if (isLUKS(type))
1137 r = _crypt_format_luks1(cd, cipher, cipher_mode,
1138 uuid, volume_key, volume_key_size, params);
1139 else if (isLOOPAES(type))
1140 r = _crypt_format_loopaes(cd, cipher, uuid, volume_key_size, params);
1141 else if (isVERITY(type))
1142 r = _crypt_format_verity(cd, uuid, params);
1144 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
1148 if (!r && !(cd->type = strdup(type)))
1152 crypt_free_volume_key(cd->volume_key);
1153 cd->volume_key = NULL;
1159 int crypt_load(struct crypt_device *cd,
1160 const char *requested_type,
1165 log_dbg("Trying to load %s crypt type from device %s.",
1166 requested_type ?: "any", mdata_device(cd) ?: "(none)");
1168 if (!mdata_device(cd))
1171 if (!requested_type || isLUKS(requested_type)) {
1172 if (cd->type && !isLUKS(cd->type)) {
1173 log_dbg("Context is already initialised to type %s", cd->type);
1177 r = _crypt_load_luks1(cd, 1, 0);
1178 } else if (isVERITY(requested_type)) {
1179 if (cd->type && !isVERITY(cd->type)) {
1180 log_dbg("Context is already initialised to type %s", cd->type);
1183 r = _crypt_load_verity(cd, params);
1190 /* cd->type and header must be set in context */
1191 r = crypt_check_data_device_size(cd);
1200 int crypt_repair(struct crypt_device *cd,
1201 const char *requested_type,
1202 void *params __attribute__((unused)))
1206 log_dbg("Trying to repair %s crypt type from device %s.",
1207 requested_type ?: "any", mdata_device(cd) ?: "(none)");
1209 if (!mdata_device(cd))
1212 if (requested_type && !isLUKS(requested_type))
1216 /* Load with repair */
1217 r = _crypt_load_luks1(cd, 1, 1);
1221 /* cd->type and header must be set in context */
1222 r = crypt_check_data_device_size(cd);
1231 int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
1233 struct crypt_dm_active_device dmd;
1236 /* Device context type must be initialised */
1237 if (!cd->type || !crypt_get_uuid(cd))
1240 log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
1242 r = dm_query_device(name, DM_ACTIVE_DEVICE | DM_ACTIVE_CRYPT_CIPHER |
1243 DM_ACTIVE_UUID | DM_ACTIVE_CRYPT_KEYSIZE |
1244 DM_ACTIVE_CRYPT_KEY, &dmd);
1246 log_err(NULL, _("Device %s is not active.\n"), name);
1250 if (!dmd.uuid || dmd.target != DM_CRYPT) {
1255 r = device_check_and_adjust(cd, dmd.data_device, DEV_OK, &new_size,
1256 &dmd.u.crypt.offset, &dmd.flags);
1260 if (new_size == dmd.size) {
1261 log_dbg("Device has already requested size %" PRIu64
1262 " sectors.", dmd.size);
1265 dmd.size = new_size;
1266 r = dm_create_device(name, cd->type, &dmd, 1);
1269 if (dmd.target == DM_CRYPT) {
1270 crypt_free_volume_key(dmd.u.crypt.vk);
1271 free(CONST_CAST(void*)dmd.u.crypt.cipher);
1273 free(CONST_CAST(void*)dmd.data_device);
1274 free(CONST_CAST(void*)dmd.uuid);
1279 int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
1281 if (!isLUKS(cd->type)) {
1282 log_err(cd, _("This operation is not supported for this device type.\n"));
1286 if (uuid && !strncmp(uuid, cd->hdr.uuid, sizeof(cd->hdr.uuid))) {
1287 log_dbg("UUID is the same as requested (%s) for device %s.",
1288 uuid, mdata_device(cd));
1293 log_dbg("Requested new UUID change to %s for %s.", uuid, mdata_device(cd));
1295 log_dbg("Requested new UUID refresh for %s.", mdata_device(cd));
1297 if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
1300 return LUKS_hdr_uuid_set(mdata_device(cd), &cd->hdr, uuid, cd);
1303 int crypt_header_backup(struct crypt_device *cd,
1304 const char *requested_type,
1305 const char *backup_file)
1309 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1312 r = init_crypto(cd);
1316 log_dbg("Requested header backup of device %s (%s) to "
1317 "file %s.", mdata_device(cd), requested_type, backup_file);
1319 return LUKS_hdr_backup(backup_file, mdata_device(cd), &cd->hdr, cd);
1322 int crypt_header_restore(struct crypt_device *cd,
1323 const char *requested_type,
1324 const char *backup_file)
1328 if (requested_type && !isLUKS(requested_type))
1331 r = init_crypto(cd);
1335 log_dbg("Requested header restore to device %s (%s) from "
1336 "file %s.", mdata_device(cd), requested_type, backup_file);
1338 return LUKS_hdr_restore(backup_file, mdata_device(cd), &cd->hdr, cd);
1341 void crypt_free(struct crypt_device *cd)
1344 log_dbg("Releasing crypt device %s context.", mdata_device(cd));
1346 if (cd->loop_fd != -1)
1350 crypt_free_volume_key(cd->volume_key);
1353 free(cd->metadata_device);
1354 free(cd->backing_file);
1357 /* used in plain device only */
1358 free(CONST_CAST(void*)cd->plain_hdr.hash);
1359 free(cd->plain_cipher);
1360 free(cd->plain_cipher_mode);
1361 free(cd->plain_uuid);
1363 /* used in loop-AES device only */
1364 free(CONST_CAST(void*)cd->loopaes_hdr.hash);
1365 free(cd->loopaes_cipher);
1366 free(cd->loopaes_uuid);
1368 /* used in verity device only */
1369 free(CONST_CAST(void*)cd->verity_hdr.hash_name);
1370 free(CONST_CAST(void*)cd->verity_hdr.salt);
1371 free(cd->verity_root_hash);
1372 free(cd->verity_uuid);
1378 int crypt_suspend(struct crypt_device *cd,
1381 crypt_status_info ci;
1384 log_dbg("Suspending volume %s.", name);
1386 if (!isLUKS(cd->type)) {
1387 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1392 ci = crypt_status(NULL, name);
1393 if (ci < CRYPT_ACTIVE) {
1394 log_err(cd, _("Volume %s is not active.\n"), name);
1398 if (!cd && dm_init(NULL, 1) < 0)
1401 r = dm_status_suspended(name);
1406 log_err(cd, _("Volume %s is already suspended.\n"), name);
1411 r = dm_suspend_and_wipe_key(name);
1413 log_err(cd, "Suspend is not supported for device %s.\n", name);
1415 log_err(cd, "Error during suspending device %s.\n", name);
1422 int crypt_resume_by_passphrase(struct crypt_device *cd,
1425 const char *passphrase,
1426 size_t passphrase_size)
1428 struct volume_key *vk = NULL;
1431 log_dbg("Resuming volume %s.", name);
1433 if (!isLUKS(cd->type)) {
1434 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1439 r = dm_status_suspended(name);
1444 log_err(cd, _("Volume %s is not suspended.\n"), name);
1449 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase,
1450 passphrase_size, &cd->hdr, &vk, cd);
1452 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1456 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1458 log_err(cd, "Resume is not supported for device %s.\n", name);
1460 log_err(cd, "Error during resuming device %s.\n", name);
1464 crypt_free_volume_key(vk);
1465 return r < 0 ? r : keyslot;
1468 int crypt_resume_by_keyfile_offset(struct crypt_device *cd,
1471 const char *keyfile,
1472 size_t keyfile_size,
1473 size_t keyfile_offset)
1475 struct volume_key *vk = NULL;
1476 char *passphrase_read = NULL;
1477 size_t passphrase_size_read;
1480 log_dbg("Resuming volume %s.", name);
1482 if (!isLUKS(cd->type)) {
1483 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1488 r = dm_status_suspended(name);
1493 log_err(cd, _("Volume %s is not suspended.\n"), name);
1500 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1501 &passphrase_size_read, keyfile, keyfile_offset,
1506 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase_read,
1507 passphrase_size_read, &cd->hdr, &vk, cd);
1512 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1514 log_err(cd, "Error during resuming device %s.\n", name);
1516 crypt_safe_free(passphrase_read);
1517 crypt_free_volume_key(vk);
1518 return r < 0 ? r : keyslot;
1521 int crypt_resume_by_keyfile(struct crypt_device *cd,
1524 const char *keyfile,
1525 size_t keyfile_size)
1527 return crypt_resume_by_keyfile_offset(cd, name, keyslot,
1528 keyfile, keyfile_size, 0);
1531 // slot manipulation
1532 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1533 int keyslot, // -1 any
1534 const char *passphrase, // NULL -> terminal
1535 size_t passphrase_size,
1536 const char *new_passphrase, // NULL -> terminal
1537 size_t new_passphrase_size)
1539 struct volume_key *vk = NULL;
1540 char *password = NULL, *new_password = NULL;
1541 size_t passwordLen, new_passwordLen;
1544 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1545 "new passphrase %sprovided.",
1546 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
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"));
1566 } else if (passphrase) {
1567 /* Passphrase provided, use it to unlock existing keyslot */
1568 r = LUKS_open_key_with_hdr(mdata_device(cd), CRYPT_ANY_SLOT, passphrase,
1569 passphrase_size, &cd->hdr, &vk, cd);
1571 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1572 r = key_from_terminal(cd, _("Enter any passphrase: "),
1573 &password, &passwordLen, 0);
1577 r = LUKS_open_key_with_hdr(mdata_device(cd), CRYPT_ANY_SLOT, password,
1578 passwordLen, &cd->hdr, &vk, cd);
1579 crypt_safe_free(password);
1585 if (new_passphrase) {
1586 new_password = CONST_CAST(char*)new_passphrase;
1587 new_passwordLen = new_passphrase_size;
1589 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1590 &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);
1601 if (!new_passphrase)
1602 crypt_safe_free(new_password);
1603 crypt_free_volume_key(vk);
1604 return r ?: keyslot;
1607 int crypt_keyslot_add_by_keyfile_offset(struct crypt_device *cd,
1609 const char *keyfile,
1610 size_t keyfile_size,
1611 size_t keyfile_offset,
1612 const char *new_keyfile,
1613 size_t new_keyfile_size,
1614 size_t new_keyfile_offset)
1616 struct volume_key *vk = NULL;
1617 char *password = NULL; size_t passwordLen;
1618 char *new_password = NULL; size_t new_passwordLen;
1621 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1622 keyfile ?: "[none]", new_keyfile ?: "[none]");
1624 if (!isLUKS(cd->type)) {
1625 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1629 r = keyslot_verify_or_find_empty(cd, &keyslot);
1633 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1634 /* No slots used, try to use pre-generated key in header */
1635 if (cd->volume_key) {
1636 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1637 r = vk ? 0 : -ENOMEM;
1639 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1643 /* Read password from file of (if NULL) from terminal */
1645 r = key_from_file(cd, _("Enter any passphrase: "),
1646 &password, &passwordLen,
1647 keyfile, keyfile_offset, keyfile_size);
1649 r = key_from_terminal(cd, _("Enter any passphrase: "),
1650 &password, &passwordLen, 0);
1654 r = LUKS_open_key_with_hdr(mdata_device(cd), CRYPT_ANY_SLOT, password, passwordLen,
1662 r = key_from_file(cd, _("Enter new passphrase for key slot: "),
1663 &new_password, &new_passwordLen, new_keyfile,
1664 new_keyfile_offset, new_keyfile_size);
1666 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1667 &new_password, &new_passwordLen, 1);
1671 r = LUKS_set_key(mdata_device(cd), keyslot, new_password, new_passwordLen,
1672 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1674 crypt_safe_free(password);
1675 crypt_safe_free(new_password);
1676 crypt_free_volume_key(vk);
1677 return r < 0 ? r : keyslot;
1680 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1682 const char *keyfile,
1683 size_t keyfile_size,
1684 const char *new_keyfile,
1685 size_t new_keyfile_size)
1687 return crypt_keyslot_add_by_keyfile_offset(cd, keyslot,
1688 keyfile, keyfile_size, 0,
1689 new_keyfile, new_keyfile_size, 0);
1692 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1694 const char *volume_key,
1695 size_t volume_key_size,
1696 const char *passphrase,
1697 size_t passphrase_size)
1699 struct volume_key *vk = NULL;
1701 char *new_password = NULL; size_t new_passwordLen;
1703 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1705 if (!isLUKS(cd->type)) {
1706 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1711 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1712 else if (cd->volume_key)
1713 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1718 r = LUKS_verify_volume_key(&cd->hdr, vk);
1720 log_err(cd, _("Volume key does not match the volume.\n"));
1724 r = keyslot_verify_or_find_empty(cd, &keyslot);
1729 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1730 &new_password, &new_passwordLen, 1);
1733 passphrase = new_password;
1734 passphrase_size = new_passwordLen;
1737 r = LUKS_set_key(mdata_device(cd), keyslot, passphrase, passphrase_size,
1738 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1740 crypt_safe_free(new_password);
1741 crypt_free_volume_key(vk);
1742 return (r < 0) ? r : keyslot;
1745 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1747 crypt_keyslot_info ki;
1749 log_dbg("Destroying keyslot %d.", keyslot);
1751 if (!isLUKS(cd->type)) {
1752 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1756 ki = crypt_keyslot_status(cd, keyslot);
1757 if (ki == CRYPT_SLOT_INVALID) {
1758 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1762 if (ki == CRYPT_SLOT_INACTIVE) {
1763 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1767 return LUKS_del_key(mdata_device(cd), keyslot, &cd->hdr, cd);
1770 // activation/deactivation of device mapping
1771 int crypt_activate_by_passphrase(struct crypt_device *cd,
1774 const char *passphrase,
1775 size_t passphrase_size,
1778 crypt_status_info ci;
1779 struct volume_key *vk = NULL;
1780 char *read_passphrase = NULL;
1781 size_t passphraseLen = 0;
1784 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1785 name ? "Activating" : "Checking", name ?: "",
1786 keyslot, passphrase ? "" : "[none] ");
1789 ci = crypt_status(NULL, name);
1790 if (ci == CRYPT_INVALID)
1792 else if (ci >= CRYPT_ACTIVE) {
1793 log_err(cd, _("Device %s already exists.\n"), name);
1798 /* plain, use hashed passphrase */
1799 if (isPLAIN(cd->type)) {
1804 r = key_from_terminal(cd, NULL, &read_passphrase,
1808 passphrase = read_passphrase;
1809 passphrase_size = passphraseLen;
1812 r = process_key(cd, cd->plain_hdr.hash,
1814 passphrase, passphrase_size, &vk);
1818 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1820 } else if (isLUKS(cd->type)) {
1821 /* provided passphrase, do not retry */
1823 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase,
1824 passphrase_size, &cd->hdr, &vk, cd);
1826 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1831 r = LUKS1_activate(cd, name, vk, flags);
1836 crypt_safe_free(read_passphrase);
1837 crypt_free_volume_key(vk);
1839 return r < 0 ? r : keyslot;
1842 int crypt_activate_by_keyfile_offset(struct crypt_device *cd,
1845 const char *keyfile,
1846 size_t keyfile_size,
1847 size_t keyfile_offset,
1850 crypt_status_info ci;
1851 struct volume_key *vk = NULL;
1852 char *passphrase_read = NULL;
1853 size_t passphrase_size_read;
1854 unsigned int key_count = 0;
1857 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1858 name ?: "", keyslot, keyfile ?: "[none]");
1861 ci = crypt_status(NULL, name);
1862 if (ci == CRYPT_INVALID)
1864 else if (ci >= CRYPT_ACTIVE) {
1865 log_err(cd, _("Device %s already exists.\n"), name);
1873 if (isPLAIN(cd->type)) {
1877 r = key_from_file(cd, _("Enter passphrase: "),
1878 &passphrase_read, &passphrase_size_read,
1879 keyfile, keyfile_offset, keyfile_size);
1883 r = process_key(cd, cd->plain_hdr.hash,
1885 passphrase_read, passphrase_size_read, &vk);
1889 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1890 } else if (isLUKS(cd->type)) {
1891 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1892 &passphrase_size_read, keyfile, keyfile_offset, keyfile_size);
1895 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase_read,
1896 passphrase_size_read, &cd->hdr, &vk, cd);
1902 r = LUKS1_activate(cd, name, vk, flags);
1907 } else if (isLOOPAES(cd->type)) {
1908 r = key_from_file(cd, NULL, &passphrase_read, &passphrase_size_read,
1909 keyfile, keyfile_offset, keyfile_size);
1912 r = LOOPAES_parse_keyfile(cd, &vk, cd->loopaes_hdr.hash, &key_count,
1913 passphrase_read, passphrase_size_read);
1917 r = LOOPAES_activate(cd, name, cd->loopaes_cipher,
1918 key_count, vk, flags);
1923 crypt_safe_free(passphrase_read);
1924 crypt_free_volume_key(vk);
1929 int crypt_activate_by_keyfile(struct crypt_device *cd,
1932 const char *keyfile,
1933 size_t keyfile_size,
1936 return crypt_activate_by_keyfile_offset(cd, name, keyslot, keyfile,
1937 keyfile_size, 0, flags);
1940 int crypt_activate_by_volume_key(struct crypt_device *cd,
1942 const char *volume_key,
1943 size_t volume_key_size,
1946 crypt_status_info ci;
1947 struct volume_key *vk = NULL;
1950 log_dbg("Activating volume %s by volume key.", name ?: "[none]");
1953 ci = crypt_status(NULL, name);
1954 if (ci == CRYPT_INVALID)
1956 else if (ci >= CRYPT_ACTIVE) {
1957 log_err(cd, _("Device %s already exists.\n"), name);
1962 /* use key directly, no hash */
1963 if (isPLAIN(cd->type)) {
1967 if (!volume_key || !volume_key_size || volume_key_size != cd->plain_key_size) {
1968 log_err(cd, _("Incorrect volume key specified for plain device.\n"));
1972 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1976 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1977 } else if (isLUKS(cd->type)) {
1978 /* If key is not provided, try to use internal key */
1980 if (!cd->volume_key) {
1981 log_err(cd, _("Volume key does not match the volume.\n"));
1984 volume_key_size = cd->volume_key->keylength;
1985 volume_key = cd->volume_key->key;
1988 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1991 r = LUKS_verify_volume_key(&cd->hdr, vk);
1994 log_err(cd, _("Volume key does not match the volume.\n"));
1997 r = LUKS1_activate(cd, name, vk, flags);
1998 } else if (isVERITY(cd->type)) {
1999 /* volume_key == root hash */
2000 if (!volume_key || !volume_key_size) {
2001 log_err(cd, _("Incorrect root hash specified for verity device.\n"));
2005 r = VERITY_activate(cd, name, mdata_device(cd),
2006 volume_key, volume_key_size,
2007 &cd->verity_hdr, CRYPT_ACTIVATE_READONLY);
2010 free(cd->verity_root_hash);
2011 cd->verity_root_hash = NULL;
2013 cd->verity_root_hash_size = volume_key_size;
2014 if (!cd->verity_root_hash)
2015 cd->verity_root_hash = malloc(volume_key_size);
2016 if (cd->verity_root_hash)
2017 memcpy(cd->verity_root_hash, volume_key, volume_key_size);
2020 log_err(cd, _("Device type is not properly initialised.\n"));
2022 crypt_free_volume_key(vk);
2027 int crypt_deactivate(struct crypt_device *cd, const char *name)
2034 log_dbg("Deactivating volume %s.", name);
2036 if (!cd && dm_init(NULL, 1) < 0)
2039 switch (crypt_status(cd, name)) {
2042 r = dm_remove_device(name, 0, 0);
2044 case CRYPT_INACTIVE:
2045 log_err(cd, _("Device %s is not active.\n"), name);
2049 log_err(cd, _("Invalid device %s.\n"), name);
2059 int crypt_volume_key_get(struct crypt_device *cd,
2062 size_t *volume_key_size,
2063 const char *passphrase,
2064 size_t passphrase_size)
2066 struct volume_key *vk = NULL;
2070 if (crypt_fips_mode()) {
2071 log_err(cd, "Function not available in FIPS mode.\n");
2075 key_len = crypt_get_volume_key_size(cd);
2076 if (key_len > *volume_key_size) {
2077 log_err(cd, _("Volume key buffer too small.\n"));
2081 if (isPLAIN(cd->type) && cd->plain_hdr.hash) {
2082 r = process_key(cd, cd->plain_hdr.hash, key_len,
2083 passphrase, passphrase_size, &vk);
2085 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
2086 } else if (isLUKS(cd->type)) {
2087 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase,
2088 passphrase_size, &cd->hdr, &vk, cd);
2091 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
2094 memcpy(volume_key, vk->key, vk->keylength);
2095 *volume_key_size = vk->keylength;
2098 crypt_free_volume_key(vk);
2102 int crypt_volume_key_verify(struct crypt_device *cd,
2103 const char *volume_key,
2104 size_t volume_key_size)
2106 struct volume_key *vk;
2109 if (!isLUKS(cd->type)) {
2110 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2114 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2118 r = LUKS_verify_volume_key(&cd->hdr, vk);
2121 log_err(cd, _("Volume key does not match the volume.\n"));
2123 crypt_free_volume_key(vk);
2128 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
2130 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
2131 cd->timeout = timeout_sec;
2134 void crypt_set_password_retry(struct crypt_device *cd, int tries)
2136 log_dbg("Password retry count set to %d.", tries);
2140 void crypt_set_iteration_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2142 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
2143 cd->iteration_time = iteration_time_ms;
2145 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2147 crypt_set_iteration_time(cd, iteration_time_ms);
2150 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
2152 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
2153 cd->password_verify = password_verify ? 1 : 0;
2156 void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
2159 case CRYPT_RNG_URANDOM:
2160 case CRYPT_RNG_RANDOM:
2161 log_dbg("RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
2162 cd->rng_type = rng_type;
2166 int crypt_get_rng_type(struct crypt_device *cd)
2171 return cd->rng_type;
2174 int crypt_memory_lock(struct crypt_device *cd, int lock)
2176 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
2180 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
2184 if (!cd && dm_init(NULL, 1) < 0)
2185 return CRYPT_INVALID;
2187 r = dm_status_device(name);
2192 if (r < 0 && r != -ENODEV)
2193 return CRYPT_INVALID;
2196 return CRYPT_ACTIVE;
2201 return CRYPT_INACTIVE;
2204 static void hexprint(struct crypt_device *cd, const char *d, int n, const char *sep)
2207 for(i = 0; i < n; i++)
2208 log_std(cd, "%02hhx%s", (const char)d[i], sep);
2211 static int _luks_dump(struct crypt_device *cd)
2215 log_std(cd, "LUKS header information for %s\n\n", mdata_device(cd));
2216 log_std(cd, "Version: \t%d\n", cd->hdr.version);
2217 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
2218 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
2219 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec);
2220 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
2221 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
2222 log_std(cd, "MK digest: \t");
2223 hexprint(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE, " ");
2225 log_std(cd, "MK salt: \t");
2226 hexprint(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2, " ");
2227 log_std(cd, "\n \t");
2228 hexprint(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ");
2230 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
2231 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
2232 for(i = 0; i < LUKS_NUMKEYS; i++) {
2233 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
2234 log_std(cd, "Key Slot %d: ENABLED\n",i);
2235 log_std(cd, "\tIterations: \t%d\n",
2236 cd->hdr.keyblock[i].passwordIterations);
2237 log_std(cd, "\tSalt: \t");
2238 hexprint(cd, cd->hdr.keyblock[i].passwordSalt,
2239 LUKS_SALTSIZE/2, " ");
2240 log_std(cd, "\n\t \t");
2241 hexprint(cd, cd->hdr.keyblock[i].passwordSalt +
2242 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ");
2245 log_std(cd, "\tKey material offset:\t%d\n",
2246 cd->hdr.keyblock[i].keyMaterialOffset);
2247 log_std(cd, "\tAF stripes: \t%d\n",
2248 cd->hdr.keyblock[i].stripes);
2251 log_std(cd, "Key Slot %d: DISABLED\n", i);
2256 static int _verity_dump(struct crypt_device *cd)
2258 log_std(cd, "VERITY header information for %s\n", mdata_device(cd));
2259 log_std(cd, "UUID: \t%s\n", cd->verity_uuid ?: "");
2260 log_std(cd, "Hash type: \t%u\n", cd->verity_hdr.hash_type);
2261 log_std(cd, "Data blocks: \t%" PRIu64 "\n", cd->verity_hdr.data_size);
2262 log_std(cd, "Data block size: \t%u\n", cd->verity_hdr.data_block_size);
2263 log_std(cd, "Hash block size: \t%u\n", cd->verity_hdr.hash_block_size);
2264 log_std(cd, "Hash algorithm: \t%s\n", cd->verity_hdr.hash_name);
2265 log_std(cd, "Salt: \t");
2266 if (cd->verity_hdr.salt_size)
2267 hexprint(cd, cd->verity_hdr.salt, cd->verity_hdr.salt_size, "");
2271 if (cd->verity_root_hash) {
2272 log_std(cd, "Root hash: \t");
2273 hexprint(cd, cd->verity_root_hash, cd->verity_root_hash_size, "");
2279 int crypt_dump(struct crypt_device *cd)
2281 if (isLUKS(cd->type))
2282 return _luks_dump(cd);
2283 else if (isVERITY(cd->type))
2284 return _verity_dump(cd);
2286 log_err(cd, _("Dump operation is not supported for this device type.\n"));
2290 const char *crypt_get_cipher(struct crypt_device *cd)
2292 if (isPLAIN(cd->type))
2293 return cd->plain_cipher;
2295 if (isLUKS(cd->type))
2296 return cd->hdr.cipherName;
2298 if (isLOOPAES(cd->type))
2299 return cd->loopaes_cipher;
2304 const char *crypt_get_cipher_mode(struct crypt_device *cd)
2306 if (isPLAIN(cd->type))
2307 return cd->plain_cipher_mode;
2309 if (isLUKS(cd->type))
2310 return cd->hdr.cipherMode;
2312 if (isLOOPAES(cd->type))
2313 return cd->loopaes_cipher_mode;
2318 const char *crypt_get_uuid(struct crypt_device *cd)
2320 if (isLUKS(cd->type))
2321 return cd->hdr.uuid;
2323 if (isPLAIN(cd->type))
2324 return cd->plain_uuid;
2326 if (isLOOPAES(cd->type))
2327 return cd->loopaes_uuid;
2329 if (isVERITY(cd->type))
2330 return cd->verity_uuid;
2335 const char *crypt_get_device_name(struct crypt_device *cd)
2340 int crypt_get_volume_key_size(struct crypt_device *cd)
2342 if (isPLAIN(cd->type))
2343 return cd->plain_key_size;
2345 if (isLUKS(cd->type))
2346 return cd->hdr.keyBytes;
2348 if (isLOOPAES(cd->type))
2349 return cd->loopaes_key_size;
2351 if (isVERITY(cd->type))
2352 return cd->verity_root_hash_size;
2357 uint64_t crypt_get_data_offset(struct crypt_device *cd)
2359 if (isPLAIN(cd->type))
2360 return cd->plain_hdr.offset;
2362 if (isLUKS(cd->type))
2363 return cd->hdr.payloadOffset;
2365 if (isLOOPAES(cd->type))
2366 return cd->loopaes_hdr.offset;
2371 uint64_t crypt_get_iv_offset(struct crypt_device *cd)
2373 if (isPLAIN(cd->type))
2374 return cd->plain_hdr.skip;
2376 if (isLUKS(cd->type))
2379 if (isLOOPAES(cd->type))
2380 return cd->loopaes_hdr.skip;
2385 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
2387 if (!isLUKS(cd->type)) {
2388 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2389 return CRYPT_SLOT_INVALID;
2392 return LUKS_keyslot_info(&cd->hdr, keyslot);
2395 int crypt_keyslot_max(const char *type)
2397 if (type && isLUKS(type))
2398 return LUKS_NUMKEYS;
2403 const char *crypt_get_type(struct crypt_device *cd)
2408 int crypt_get_verity_info(struct crypt_device *cd,
2409 struct crypt_params_verity *vp)
2411 if (!isVERITY(cd->type) || !vp)
2414 vp->data_device = cd->device;
2415 vp->hash_device = mdata_device(cd);
2416 vp->hash_name = cd->verity_hdr.hash_name;
2417 vp->salt = cd->verity_hdr.salt;
2418 vp->salt_size = cd->verity_hdr.salt_size;
2419 vp->data_block_size = cd->verity_hdr.data_block_size;
2420 vp->hash_block_size = cd->verity_hdr.hash_block_size;
2421 vp->data_size = cd->verity_hdr.data_size;
2422 vp->hash_area_offset = cd->verity_hdr.hash_area_offset;
2423 vp->hash_type = cd->verity_hdr.hash_type;
2424 vp->flags = cd->verity_hdr.flags & CRYPT_VERITY_NO_HEADER;
2428 int crypt_get_active_device(struct crypt_device *cd __attribute__((unused)),
2430 struct crypt_active_device *cad)
2432 struct crypt_dm_active_device dmd;
2435 r = dm_query_device(name, 0, &dmd);
2439 if (dmd.target != DM_CRYPT && dmd.target != DM_VERITY)
2442 cad->offset = dmd.u.crypt.offset;
2443 cad->iv_offset = dmd.u.crypt.iv_offset;
2444 cad->size = dmd.size;
2445 cad->flags = dmd.flags;