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;
73 /* callbacks definitions */
74 void (*log)(int level, const char *msg, void *usrptr);
76 int (*confirm)(const char *msg, void *usrptr);
78 int (*password)(const char *msg, char *buf, size_t length, void *usrptr);
79 void *password_usrptr;
81 /* last error message */
82 char error[MAX_ERROR_LENGTH];
86 /* FIXME: not thread safe, remove this later */
87 static char global_error[MAX_ERROR_LENGTH] = {0};
90 static void (*_default_log)(int level, const char *msg, void *usrptr) = NULL;
91 static int _debug_level = 0;
93 void crypt_set_debug_level(int level)
98 int crypt_get_debug_level(void)
103 static void crypt_set_error(struct crypt_device *cd, const char *error)
105 size_t size = strlen(error);
107 /* Set global error, ugly hack... */
108 strncpy(global_error, error, MAX_ERROR_LENGTH - 2);
109 if (size < MAX_ERROR_LENGTH && global_error[size - 1] == '\n')
110 global_error[size - 1] = '\0';
112 /* Set error string per context */
114 strncpy(cd->error, error, MAX_ERROR_LENGTH - 2);
115 if (size < MAX_ERROR_LENGTH && cd->error[size - 1] == '\n')
116 cd->error[size - 1] = '\0';
120 void crypt_log(struct crypt_device *cd, int level, const char *msg)
123 cd->log(level, msg, cd->log_usrptr);
124 else if (_default_log)
125 _default_log(level, msg, NULL);
127 if (level == CRYPT_LOG_ERROR)
128 crypt_set_error(cd, msg);
131 __attribute__((format(printf, 5, 6)))
132 void logger(struct crypt_device *cd, int level, const char *file,
133 int line, const char *format, ...)
138 va_start(argp, format);
140 if (vasprintf(&target, format, argp) > 0 ) {
142 crypt_log(cd, level, target);
144 } else if (_debug_level)
145 printf("# %s:%d %s\n", file ?: "?", line, target);
147 } else if (_debug_level)
148 printf("# %s\n", target);
156 static const char *mdata_device(struct crypt_device *cd)
158 return cd->metadata_device ?: cd->device;
161 static int init_crypto(struct crypt_device *ctx)
165 crypt_fips_libcryptsetup_check(ctx);
167 r = crypt_random_init(ctx);
169 log_err(ctx, _("Cannot initialize crypto RNG backend.\n"));
173 r = crypt_backend_init(ctx);
175 log_err(ctx, _("Cannot initialize crypto backend.\n"));
177 log_dbg("Crypto backend (%s) initialized.", crypt_backend_version());
181 static int process_key(struct crypt_device *cd, const char *hash_name,
182 size_t key_size, const char *pass, size_t passLen,
183 struct volume_key **vk)
190 *vk = crypt_alloc_volume_key(key_size, NULL);
195 r = crypt_plain_hash(cd, hash_name, (*vk)->key, key_size, pass, passLen);
198 log_err(cd, _("Hash algorithm %s not supported.\n"),
201 log_err(cd, _("Key processing error (using hash %s).\n"),
203 crypt_free_volume_key(*vk);
207 } else if (passLen > key_size) {
208 memcpy((*vk)->key, pass, key_size);
210 memcpy((*vk)->key, pass, passLen);
216 static int isPLAIN(const char *type)
218 return (type && !strcmp(CRYPT_PLAIN, type));
221 static int isLUKS(const char *type)
223 return (type && !strcmp(CRYPT_LUKS1, type));
226 static int isLOOPAES(const char *type)
228 return (type && !strcmp(CRYPT_LOOPAES, type));
231 static int isVERITY(const char *type)
233 return (type && !strcmp(CRYPT_VERITY, type));
236 /* keyslot helpers */
237 static int keyslot_verify_or_find_empty(struct crypt_device *cd, int *keyslot)
239 if (*keyslot == CRYPT_ANY_SLOT) {
240 *keyslot = LUKS_keyslot_find_empty(&cd->hdr);
242 log_err(cd, _("All key slots full.\n"));
247 switch (LUKS_keyslot_info(&cd->hdr, *keyslot)) {
248 case CRYPT_SLOT_INVALID:
249 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
250 *keyslot, LUKS_NUMKEYS - 1);
252 case CRYPT_SLOT_INACTIVE:
255 log_err(cd, _("Key slot %d is full, please select another one.\n"),
264 * compares UUIDs returned by device-mapper (striped by cryptsetup) and uuid in header
266 static int crypt_uuid_cmp(const char *dm_uuid, const char *hdr_uuid)
271 if (!dm_uuid || !hdr_uuid)
274 str = strchr(dm_uuid, '-');
278 for (i = 0, j = 1; hdr_uuid[i]; i++) {
279 if (hdr_uuid[i] == '-')
282 if (!str[j] || str[j] == '-')
285 if (str[j] != hdr_uuid[i])
293 int PLAIN_activate(struct crypt_device *cd,
295 struct volume_key *vk,
300 char *dm_cipher = NULL;
301 enum devcheck device_check;
302 struct crypt_dm_active_device dmd = {
304 .uuid = crypt_get_uuid(cd),
307 .data_device = crypt_get_device_name(cd),
311 .offset = crypt_get_data_offset(cd),
312 .iv_offset = crypt_get_iv_offset(cd),
316 if (dmd.flags & CRYPT_ACTIVATE_SHARED)
317 device_check = DEV_SHARED;
319 device_check = DEV_EXCL;
321 r = device_check_and_adjust(cd, dmd.data_device, device_check,
322 &dmd.size, &dmd.u.crypt.offset, &dmd.flags);
326 if (crypt_get_cipher_mode(cd))
327 r = asprintf(&dm_cipher, "%s-%s", crypt_get_cipher(cd), crypt_get_cipher_mode(cd));
329 r = asprintf(&dm_cipher, "%s", crypt_get_cipher(cd));
333 dmd.u.crypt.cipher = dm_cipher;
334 log_dbg("Trying to activate PLAIN device %s using cipher %s.",
335 name, dmd.u.crypt.cipher);
337 r = dm_create_device(name, CRYPT_PLAIN, &dmd, 0);
340 if (!cd->plain_uuid && dm_query_device(name, DM_ACTIVE_UUID, &dmd) >= 0)
341 cd->plain_uuid = CONST_CAST(char*)dmd.uuid;
347 int crypt_confirm(struct crypt_device *cd, const char *msg)
349 if (!cd || !cd->confirm)
352 return cd->confirm(msg, cd->confirm_usrptr);
355 static int key_from_terminal(struct crypt_device *cd, char *msg, char **key,
356 size_t *key_len, int force_verify)
362 if(!msg && asprintf(&prompt, _("Enter passphrase for %s: "),
363 cd->backing_file ?: crypt_get_device_name(cd)) < 0)
370 *key = crypt_safe_alloc(DEFAULT_PASSPHRASE_SIZE_MAX);
375 r = cd->password(msg, *key, DEFAULT_PASSPHRASE_SIZE_MAX,
376 cd->password_usrptr);
378 crypt_safe_free(*key);
383 r = crypt_get_key(msg, key, key_len, 0, 0, NULL, cd->timeout,
384 (force_verify || cd->password_verify), cd);
387 return (r < 0) ? r: 0;
390 static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslot,
391 struct volume_key **vk)
393 char *passphrase_read = NULL;
394 size_t passphrase_size_read;
395 int r = -EINVAL, eperm = 0, tries = cd->tries;
399 crypt_free_volume_key(*vk);
402 r = key_from_terminal(cd, NULL, &passphrase_read,
403 &passphrase_size_read, 0);
404 /* Continue if it is just passphrase verify mismatch */
410 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase_read,
411 passphrase_size_read, &cd->hdr, vk, cd);
414 crypt_safe_free(passphrase_read);
415 passphrase_read = NULL;
416 } while (r == -EPERM && (--tries > 0));
419 crypt_free_volume_key(*vk);
422 /* Report wrong passphrase if at least one try failed */
423 if (eperm && r == -EPIPE)
427 crypt_safe_free(passphrase_read);
431 static int key_from_file(struct crypt_device *cd, char *msg,
432 char **key, size_t *key_len,
433 const char *key_file, size_t key_offset,
436 return crypt_get_key(msg, key, key_len, key_offset, key_size, key_file,
440 void crypt_set_log_callback(struct crypt_device *cd,
441 void (*log)(int level, const char *msg, void *usrptr),
448 cd->log_usrptr = usrptr;
452 void crypt_set_confirm_callback(struct crypt_device *cd,
453 int (*confirm)(const char *msg, void *usrptr),
456 cd->confirm = confirm;
457 cd->confirm_usrptr = usrptr;
460 void crypt_set_password_callback(struct crypt_device *cd,
461 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
464 cd->password = password;
465 cd->password_usrptr = usrptr;
468 static void _get_error(char *error, char *buf, size_t size)
470 if (!buf || size < 1)
473 strncpy(buf, error, size - 1);
474 buf[size - 1] = '\0';
480 void crypt_last_error(struct crypt_device *cd, char *buf, size_t size)
483 return _get_error(cd->error, buf, size);
486 /* Deprecated global error interface */
487 void crypt_get_error(char *buf, size_t size)
489 return _get_error(global_error, buf, size);
492 const char *crypt_get_dir(void)
497 int crypt_init(struct crypt_device **cd, const char *device)
499 struct crypt_device *h = NULL;
505 log_dbg("Allocating crypt device %s context.", device);
507 if (!(h = malloc(sizeof(struct crypt_device))))
510 memset(h, 0, sizeof(*h));
514 r = device_ready(NULL, device, O_RDONLY);
516 h->device = crypt_loop_get_device();
517 log_dbg("Not a block device, %s%s.",
518 h->device ? "using free loop device " :
519 "no free loop device found",
522 log_err(NULL, _("Cannot find a free loopback device.\n"));
527 /* Keep the loop open, dettached on last close. */
528 h->loop_fd = crypt_loop_attach(h->device, device, 0, 1, &readonly);
529 if (h->loop_fd == -1) {
530 log_err(NULL, _("Attaching loopback device failed "
531 "(loop device with autoclear flag is required).\n"));
536 h->backing_file = crypt_loop_backing_file(h->device);
537 r = device_ready(NULL, h->device, O_RDONLY);
545 if (!h->device && device && !(h->device = strdup(device))) {
550 if (dm_init(h, 1) < 0) {
555 h->iteration_time = 1000;
556 h->password_verify = 0;
558 h->rng_type = crypt_random_default_key_rng();
564 if (h->loop_fd != -1)
567 free(h->backing_file);
573 static int crypt_check_data_device_size(struct crypt_device *cd)
576 uint64_t size, size_min;
578 /* Check data device size, require at least one sector */
579 size_min = crypt_get_data_offset(cd) << SECTOR_SHIFT ?: SECTOR_SIZE;
581 r = device_size(crypt_get_device_name(cd), &size);
585 if (size < size_min) {
586 log_err(cd, _("LUKS header detected but device %s is too small.\n"),
587 crypt_get_device_name(cd));
594 int crypt_set_data_device(struct crypt_device *cd, const char *device)
599 log_dbg("Setting ciphertext data device to %s.", device ?: "(none)");
601 if (!isLUKS(cd->type) && !isVERITY(cd->type)) {
602 log_err(cd, _("This operation is not supported for this device type.\n"));
606 /* metadata device must be set */
607 if (!cd->device || !device)
610 r = device_ready(NULL, device, O_RDONLY);
614 if (!(data_device = strdup(device)))
617 if (!cd->metadata_device)
618 cd->metadata_device = cd->device;
622 cd->device = data_device;
624 return crypt_check_data_device_size(cd);
627 static int _crypt_load_luks1(struct crypt_device *cd, int require_header, int repair)
629 struct luks_phdr hdr;
636 r = LUKS_read_phdr(mdata_device(cd), &hdr, require_header, repair, cd);
640 if (!cd->type && !(cd->type = strdup(CRYPT_LUKS1)))
643 memcpy(&cd->hdr, &hdr, sizeof(hdr));
648 static int _crypt_load_verity(struct crypt_device *cd, struct crypt_params_verity *params)
651 size_t sb_offset = 0;
657 if (params->flags & CRYPT_VERITY_NO_HEADER)
661 sb_offset = params->hash_area_offset;
663 r = VERITY_read_sb(cd, mdata_device(cd), sb_offset, &cd->verity_hdr);
668 cd->verity_hdr.flags = params->flags;
670 if (params && params->data_device &&
671 (r = crypt_set_data_device(cd, params->data_device)) < 0)
674 cd->verity_root_hash_size = crypt_hash_size(cd->verity_hdr.hash_name);
676 if (!cd->type && !(cd->type = strdup(CRYPT_VERITY)))
682 static int _init_by_name_crypt(struct crypt_device *cd, const char *name)
684 struct crypt_dm_active_device dmd = {};
685 char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
688 r = dm_query_device(name, DM_ACTIVE_DEVICE |
690 DM_ACTIVE_CRYPT_CIPHER |
691 DM_ACTIVE_CRYPT_KEYSIZE, &dmd);
695 if (isPLAIN(cd->type)) {
696 cd->plain_uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
697 cd->plain_hdr.hash = NULL; /* no way to get this */
698 cd->plain_hdr.offset = dmd.u.crypt.offset;
699 cd->plain_hdr.skip = dmd.u.crypt.iv_offset;
700 cd->plain_key_size = dmd.u.crypt.vk->keylength;
702 r = crypt_parse_name_and_mode(dmd.u.crypt.cipher, cipher, NULL, cipher_mode);
704 cd->plain_cipher = strdup(cipher);
705 cd->plain_cipher_mode = strdup(cipher_mode);
707 } else if (isLOOPAES(cd->type)) {
708 cd->loopaes_uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
709 cd->loopaes_hdr.offset = dmd.u.crypt.offset;
711 r = crypt_parse_name_and_mode(dmd.u.crypt.cipher, cipher,
712 &key_nums, cipher_mode);
714 cd->loopaes_cipher = strdup(cipher);
715 cd->loopaes_cipher_mode = strdup(cipher_mode);
716 /* version 3 uses last key for IV */
717 if (dmd.u.crypt.vk->keylength % key_nums)
719 cd->loopaes_key_size = dmd.u.crypt.vk->keylength / key_nums;
721 } else if (isLUKS(cd->type)) {
722 if (mdata_device(cd)) {
723 r = _crypt_load_luks1(cd, 0, 0);
725 log_dbg("LUKS device header does not match active device.");
731 /* check whether UUIDs match each other */
732 r = crypt_uuid_cmp(dmd.uuid, cd->hdr.uuid);
734 log_dbg("LUKS device header uuid: %s mismatches DM returned uuid %s",
735 cd->hdr.uuid, dmd.uuid);
744 crypt_free_volume_key(dmd.u.crypt.vk);
745 free(CONST_CAST(void*)dmd.u.crypt.cipher);
746 free(CONST_CAST(void*)dmd.data_device);
747 free(CONST_CAST(void*)dmd.uuid);
751 static int _init_by_name_verity(struct crypt_device *cd, const char *name)
753 struct crypt_params_verity params = {};
754 struct crypt_dm_active_device dmd = {
756 .u.verity.vp = ¶ms,
760 r = dm_query_device(name, DM_ACTIVE_DEVICE |
762 DM_ACTIVE_VERITY_HASH_DEVICE |
763 DM_ACTIVE_VERITY_PARAMS, &dmd);
767 if (isVERITY(cd->type)) {
768 cd->verity_hdr.flags = CRYPT_VERITY_NO_HEADER; //FIXME
769 //cd->verity_uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
770 cd->verity_hdr.data_size = params.data_size;
771 cd->verity_root_hash_size = dmd.u.verity.root_hash_size;
772 cd->verity_root_hash = NULL;
773 cd->verity_hdr.hash_name = params.hash_name;
774 cd->verity_hdr.data_device = NULL;
775 cd->verity_hdr.hash_device = NULL;
776 cd->verity_hdr.data_block_size = params.data_block_size;
777 cd->verity_hdr.hash_block_size = params.hash_block_size;
778 cd->verity_hdr.hash_area_offset = dmd.u.verity.hash_offset;
779 cd->verity_hdr.version = params.version;
780 cd->verity_hdr.flags = params.flags;
781 cd->verity_hdr.salt_size = params.salt_size;
782 cd->verity_hdr.salt = params.salt;
783 if (!(cd->metadata_device = strdup(dmd.u.verity.hash_device)))
787 free(CONST_CAST(void*)dmd.u.verity.hash_device);
788 free(CONST_CAST(void*)dmd.data_device);
789 free(CONST_CAST(void*)dmd.uuid);
793 int crypt_init_by_name_and_header(struct crypt_device **cd,
795 const char *header_device)
797 crypt_status_info ci;
798 struct crypt_dm_active_device dmd;
801 log_dbg("Allocating crypt device context by device %s.", name);
803 ci = crypt_status(NULL, name);
804 if (ci == CRYPT_INVALID)
807 if (ci < CRYPT_ACTIVE) {
808 log_err(NULL, _("Device %s is not active.\n"), name);
812 r = dm_query_device(name, DM_ACTIVE_DEVICE | DM_ACTIVE_UUID, &dmd);
819 r = crypt_init(cd, header_device);
821 r = crypt_init(cd, dmd.data_device);
823 /* Underlying device disappeared but mapping still active */
824 if (!dmd.data_device || r == -ENOTBLK)
825 log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
828 /* Underlying device is not readable but crypt mapping exists */
830 free(CONST_CAST(void*)dmd.data_device);
831 dmd.data_device = NULL;
832 r = crypt_init(cd, NULL);
840 if (!strncmp(CRYPT_PLAIN, dmd.uuid, sizeof(CRYPT_PLAIN)-1))
841 (*cd)->type = strdup(CRYPT_PLAIN);
842 else if (!strncmp(CRYPT_LOOPAES, dmd.uuid, sizeof(CRYPT_LOOPAES)-1))
843 (*cd)->type = strdup(CRYPT_LOOPAES);
844 else if (!strncmp(CRYPT_LUKS1, dmd.uuid, sizeof(CRYPT_LUKS1)-1))
845 (*cd)->type = strdup(CRYPT_LUKS1);
846 else if (!strncmp(CRYPT_VERITY, dmd.uuid, sizeof(CRYPT_VERITY)-1))
847 (*cd)->type = strdup(CRYPT_VERITY);
849 log_dbg("Unknown UUID set, some parameters are not set.");
851 log_dbg("Active device has no UUID set, some parameters are not set.");
854 r = crypt_set_data_device(*cd, dmd.data_device);
859 /* Try to initialise basic parameters from active device */
861 if (!(*cd)->backing_file && dmd.data_device &&
862 crypt_loop_device(dmd.data_device) &&
863 !((*cd)->backing_file = crypt_loop_backing_file(dmd.data_device))) {
868 if (dmd.target == DM_CRYPT)
869 r = _init_by_name_crypt(*cd, name);
870 else if (dmd.target == DM_VERITY)
871 r = _init_by_name_verity(*cd, name);
877 free(CONST_CAST(void*)dmd.data_device);
878 free(CONST_CAST(void*)dmd.uuid);
882 int crypt_init_by_name(struct crypt_device **cd, const char *name)
884 return crypt_init_by_name_and_header(cd, name, NULL);
887 static int _crypt_format_plain(struct crypt_device *cd,
889 const char *cipher_mode,
891 size_t volume_key_size,
892 struct crypt_params_plain *params)
894 if (!cipher || !cipher_mode) {
895 log_err(cd, _("Invalid plain crypt parameters.\n"));
899 if (volume_key_size > 1024) {
900 log_err(cd, _("Invalid key size.\n"));
904 cd->plain_key_size = volume_key_size;
905 cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
909 cd->plain_cipher = strdup(cipher);
910 cd->plain_cipher_mode = strdup(cipher_mode);
913 cd->plain_uuid = strdup(uuid);
915 if (params && params->hash)
916 cd->plain_hdr.hash = strdup(params->hash);
918 cd->plain_hdr.offset = params ? params->offset : 0;
919 cd->plain_hdr.skip = params ? params->skip : 0;
920 cd->plain_hdr.size = params ? params->size : 0;
922 if (!cd->plain_cipher || !cd->plain_cipher_mode)
928 static int _crypt_format_luks1(struct crypt_device *cd,
930 const char *cipher_mode,
932 const char *volume_key,
933 size_t volume_key_size,
934 struct crypt_params_luks1 *params)
937 unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
938 unsigned long alignment_offset = 0;
940 if (!mdata_device(cd)) {
941 log_err(cd, _("Can't format LUKS without device.\n"));
946 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
949 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
954 if (params && params->data_device) {
955 cd->metadata_device = cd->device;
956 if (!(cd->device = strdup(params->data_device)))
958 required_alignment = params->data_alignment * SECTOR_SIZE;
959 } else if (params && params->data_alignment) {
960 required_alignment = params->data_alignment * SECTOR_SIZE;
962 get_topology_alignment(cd->device, &required_alignment,
963 &alignment_offset, DEFAULT_DISK_ALIGNMENT);
965 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
966 (params && params->hash) ? params->hash : "sha1",
968 required_alignment / SECTOR_SIZE,
969 alignment_offset / SECTOR_SIZE,
970 cd->iteration_time, &cd->PBKDF2_per_sec,
971 cd->metadata_device, cd);
975 /* Wipe first 8 sectors - fs magic numbers etc. */
976 r = crypt_wipe(mdata_device(cd), 0, 8 * SECTOR_SIZE, CRYPT_WIPE_ZERO, 1);
979 log_err(cd, _("Cannot format device %s which is still in use.\n"),
982 log_err(cd, _("Cannot wipe header on device %s.\n"),
988 r = LUKS_write_phdr(mdata_device(cd), &cd->hdr, cd);
993 static int _crypt_format_loopaes(struct crypt_device *cd,
996 size_t volume_key_size,
997 struct crypt_params_loopaes *params)
999 if (!mdata_device(cd)) {
1000 log_err(cd, _("Can't format LOOPAES without device.\n"));
1004 if (volume_key_size > 1024) {
1005 log_err(cd, _("Invalid key size.\n"));
1009 cd->loopaes_key_size = volume_key_size;
1011 cd->loopaes_cipher = strdup(cipher ?: DEFAULT_LOOPAES_CIPHER);
1014 cd->loopaes_uuid = strdup(uuid);
1016 if (params && params->hash)
1017 cd->loopaes_hdr.hash = strdup(params->hash);
1019 cd->loopaes_hdr.offset = params ? params->offset : 0;
1020 cd->loopaes_hdr.skip = params ? params->skip : 0;
1025 static int _crypt_format_verity(struct crypt_device *cd,
1026 struct crypt_params_verity *params)
1029 uint64_t data_device_size;
1031 if (!mdata_device(cd)) {
1032 log_err(cd, _("Can't format VERITY without device.\n"));
1036 if (!params || !params->data_device)
1039 if (params->version > 1)
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;
1058 cd->verity_root_hash_size = crypt_hash_size(params->hash_name);
1059 if (!cd->verity_root_hash_size)
1062 cd->verity_root_hash = malloc(cd->verity_root_hash_size);
1063 if (!cd->verity_root_hash)
1066 cd->verity_hdr.flags = params->flags;
1067 cd->verity_hdr.hash_name = strdup(params->hash_name);
1068 cd->verity_hdr.data_device = NULL;
1069 cd->verity_hdr.data_block_size = params->data_block_size;
1070 cd->verity_hdr.hash_block_size = params->hash_block_size;
1071 cd->verity_hdr.hash_area_offset = params->hash_area_offset;
1072 cd->verity_hdr.version = params->version;
1073 cd->verity_hdr.flags = params->flags;
1074 cd->verity_hdr.salt_size = params->salt_size;
1075 cd->verity_hdr.salt = malloc(params->salt_size);
1077 memcpy(CONST_CAST(char*)cd->verity_hdr.salt, params->salt,
1080 r = crypt_random_get(cd, CONST_CAST(char*)cd->verity_hdr.salt,
1081 params->salt_size, CRYPT_RND_SALT);
1085 if (params->flags & CRYPT_VERITY_CREATE_HASH) {
1086 r = VERITY_create(cd, &cd->verity_hdr, cd->device, mdata_device(cd),
1087 cd->verity_root_hash, cd->verity_root_hash_size);
1092 if (!(params->flags & CRYPT_VERITY_NO_HEADER))
1093 r = VERITY_write_sb(cd, mdata_device(cd),
1094 cd->verity_hdr.hash_area_offset,
1098 free(cd->verity_root_hash);
1099 free(CONST_CAST(char*)cd->verity_hdr.hash_name);
1100 free(CONST_CAST(char*)cd->verity_hdr.salt);
1106 int crypt_format(struct crypt_device *cd,
1109 const char *cipher_mode,
1111 const char *volume_key,
1112 size_t volume_key_size,
1121 log_dbg("Context already formatted as %s.", cd->type);
1125 log_dbg("Formatting device %s as type %s.", mdata_device(cd) ?: "(none)", type);
1127 r = init_crypto(cd);
1132 r = _crypt_format_plain(cd, cipher, cipher_mode,
1133 uuid, volume_key_size, params);
1134 else if (isLUKS(type))
1135 r = _crypt_format_luks1(cd, cipher, cipher_mode,
1136 uuid, volume_key, volume_key_size, params);
1137 else if (isLOOPAES(type))
1138 r = _crypt_format_loopaes(cd, cipher, uuid, volume_key_size, params);
1139 else if (isVERITY(type))
1140 r = _crypt_format_verity(cd, params);
1142 /* FIXME: allow plugins here? */
1143 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
1147 if (!r && !(cd->type = strdup(type)))
1151 crypt_free_volume_key(cd->volume_key);
1152 cd->volume_key = NULL;
1158 int crypt_load(struct crypt_device *cd,
1159 const char *requested_type,
1164 log_dbg("Trying to load %s crypt type from device %s.",
1165 requested_type ?: "any", mdata_device(cd) ?: "(none)");
1167 if (!mdata_device(cd))
1170 if (!requested_type || isLUKS(requested_type)) {
1171 if (cd->type && !isLUKS(cd->type)) {
1172 log_dbg("Context is already initialised to type %s", cd->type);
1176 r = _crypt_load_luks1(cd, 1, 0);
1177 } else if (isVERITY(requested_type)) {
1178 if (cd->type && !isVERITY(cd->type)) {
1179 log_dbg("Context is already initialised to type %s", cd->type);
1182 r = _crypt_load_verity(cd, params);
1189 /* cd->type and header must be set in context */
1190 r = crypt_check_data_device_size(cd);
1199 int crypt_repair(struct crypt_device *cd,
1200 const char *requested_type,
1201 void *params __attribute__((unused)))
1205 log_dbg("Trying to repair %s crypt type from device %s.",
1206 requested_type ?: "any", mdata_device(cd) ?: "(none)");
1208 if (!mdata_device(cd))
1211 if (requested_type && !isLUKS(requested_type))
1215 /* Load with repair */
1216 r = _crypt_load_luks1(cd, 1, 1);
1220 /* cd->type and header must be set in context */
1221 r = crypt_check_data_device_size(cd);
1230 int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
1232 struct crypt_dm_active_device dmd;
1235 /* Device context type must be initialised */
1236 if (!cd->type || !crypt_get_uuid(cd))
1239 log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
1241 r = dm_query_device(name, DM_ACTIVE_DEVICE | DM_ACTIVE_CRYPT_CIPHER |
1242 DM_ACTIVE_UUID | DM_ACTIVE_CRYPT_KEYSIZE |
1243 DM_ACTIVE_CRYPT_KEY, &dmd);
1245 log_err(NULL, _("Device %s is not active.\n"), name);
1249 if (!dmd.uuid || dmd.target != DM_CRYPT) {
1254 r = device_check_and_adjust(cd, dmd.data_device, DEV_OK, &new_size,
1255 &dmd.u.crypt.offset, &dmd.flags);
1259 if (new_size == dmd.size) {
1260 log_dbg("Device has already requested size %" PRIu64
1261 " sectors.", dmd.size);
1264 dmd.size = new_size;
1265 r = dm_create_device(name, cd->type, &dmd, 1);
1268 if (dmd.target == DM_CRYPT) {
1269 crypt_free_volume_key(dmd.u.crypt.vk);
1270 free(CONST_CAST(void*)dmd.u.crypt.cipher);
1272 free(CONST_CAST(void*)dmd.data_device);
1273 free(CONST_CAST(void*)dmd.uuid);
1278 int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
1280 if (!isLUKS(cd->type)) {
1281 log_err(cd, _("This operation is not supported for this device type.\n"));
1285 if (uuid && !strncmp(uuid, cd->hdr.uuid, sizeof(cd->hdr.uuid))) {
1286 log_dbg("UUID is the same as requested (%s) for device %s.",
1287 uuid, mdata_device(cd));
1292 log_dbg("Requested new UUID change to %s for %s.", uuid, mdata_device(cd));
1294 log_dbg("Requested new UUID refresh for %s.", mdata_device(cd));
1296 if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
1299 return LUKS_hdr_uuid_set(mdata_device(cd), &cd->hdr, uuid, cd);
1302 int crypt_header_backup(struct crypt_device *cd,
1303 const char *requested_type,
1304 const char *backup_file)
1308 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1311 r = init_crypto(cd);
1315 log_dbg("Requested header backup of device %s (%s) to "
1316 "file %s.", mdata_device(cd), requested_type, backup_file);
1318 return LUKS_hdr_backup(backup_file, mdata_device(cd), &cd->hdr, cd);
1321 int crypt_header_restore(struct crypt_device *cd,
1322 const char *requested_type,
1323 const char *backup_file)
1327 if (requested_type && !isLUKS(requested_type))
1330 /* Some hash functions need initialized gcrypt library */
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);
1377 int crypt_suspend(struct crypt_device *cd,
1380 crypt_status_info ci;
1383 log_dbg("Suspending volume %s.", name);
1385 if (!isLUKS(cd->type)) {
1386 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1391 ci = crypt_status(NULL, name);
1392 if (ci < CRYPT_ACTIVE) {
1393 log_err(cd, _("Volume %s is not active.\n"), name);
1397 if (!cd && dm_init(NULL, 1) < 0)
1400 r = dm_status_suspended(name);
1405 log_err(cd, _("Volume %s is already suspended.\n"), name);
1410 r = dm_suspend_and_wipe_key(name);
1412 log_err(cd, "Suspend is not supported for device %s.\n", name);
1414 log_err(cd, "Error during suspending device %s.\n", name);
1421 int crypt_resume_by_passphrase(struct crypt_device *cd,
1424 const char *passphrase,
1425 size_t passphrase_size)
1427 struct volume_key *vk = NULL;
1430 log_dbg("Resuming volume %s.", name);
1432 if (!isLUKS(cd->type)) {
1433 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1438 r = dm_status_suspended(name);
1443 log_err(cd, _("Volume %s is not suspended.\n"), name);
1448 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase,
1449 passphrase_size, &cd->hdr, &vk, cd);
1451 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1455 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1457 log_err(cd, "Resume is not supported for device %s.\n", name);
1459 log_err(cd, "Error during resuming device %s.\n", name);
1463 crypt_free_volume_key(vk);
1464 return r < 0 ? r : keyslot;
1467 int crypt_resume_by_keyfile_offset(struct crypt_device *cd,
1470 const char *keyfile,
1471 size_t keyfile_size,
1472 size_t keyfile_offset)
1474 struct volume_key *vk = NULL;
1475 char *passphrase_read = NULL;
1476 size_t passphrase_size_read;
1479 log_dbg("Resuming volume %s.", name);
1481 if (!isLUKS(cd->type)) {
1482 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1487 r = dm_status_suspended(name);
1492 log_err(cd, _("Volume %s is not suspended.\n"), name);
1499 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1500 &passphrase_size_read, keyfile, keyfile_offset,
1505 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase_read,
1506 passphrase_size_read, &cd->hdr, &vk, cd);
1511 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1513 log_err(cd, "Error during resuming device %s.\n", name);
1515 crypt_safe_free(passphrase_read);
1516 crypt_free_volume_key(vk);
1517 return r < 0 ? r : keyslot;
1520 int crypt_resume_by_keyfile(struct crypt_device *cd,
1523 const char *keyfile,
1524 size_t keyfile_size)
1526 return crypt_resume_by_keyfile_offset(cd, name, keyslot,
1527 keyfile, keyfile_size, 0);
1530 // slot manipulation
1531 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1532 int keyslot, // -1 any
1533 const char *passphrase, // NULL -> terminal
1534 size_t passphrase_size,
1535 const char *new_passphrase, // NULL -> terminal
1536 size_t new_passphrase_size)
1538 struct volume_key *vk = NULL;
1539 char *password = NULL, *new_password = NULL;
1540 size_t passwordLen, new_passwordLen;
1543 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1544 "new passphrase %sprovided.",
1545 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1547 if (!isLUKS(cd->type)) {
1548 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1552 r = keyslot_verify_or_find_empty(cd, &keyslot);
1556 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1557 /* No slots used, try to use pre-generated key in header */
1558 if (cd->volume_key) {
1559 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1560 r = vk ? 0 : -ENOMEM;
1562 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1565 } else if (passphrase) {
1566 /* Passphrase provided, use it to unlock existing keyslot */
1567 r = LUKS_open_key_with_hdr(mdata_device(cd), CRYPT_ANY_SLOT, passphrase,
1568 passphrase_size, &cd->hdr, &vk, cd);
1570 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1571 r = key_from_terminal(cd, _("Enter any passphrase: "),
1572 &password, &passwordLen, 0);
1576 r = LUKS_open_key_with_hdr(mdata_device(cd), CRYPT_ANY_SLOT, password,
1577 passwordLen, &cd->hdr, &vk, cd);
1578 crypt_safe_free(password);
1584 if (new_passphrase) {
1585 new_password = CONST_CAST(char*)new_passphrase;
1586 new_passwordLen = new_passphrase_size;
1588 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1589 &new_password, &new_passwordLen, 1);
1594 r = LUKS_set_key(mdata_device(cd), keyslot, new_password, new_passwordLen,
1595 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1600 if (!new_passphrase)
1601 crypt_safe_free(new_password);
1602 crypt_free_volume_key(vk);
1603 return r ?: keyslot;
1606 int crypt_keyslot_add_by_keyfile_offset(struct crypt_device *cd,
1608 const char *keyfile,
1609 size_t keyfile_size,
1610 size_t keyfile_offset,
1611 const char *new_keyfile,
1612 size_t new_keyfile_size,
1613 size_t new_keyfile_offset)
1615 struct volume_key *vk = NULL;
1616 char *password = NULL; size_t passwordLen;
1617 char *new_password = NULL; size_t new_passwordLen;
1620 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1621 keyfile ?: "[none]", new_keyfile ?: "[none]");
1623 if (!isLUKS(cd->type)) {
1624 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1628 r = keyslot_verify_or_find_empty(cd, &keyslot);
1632 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1633 /* No slots used, try to use pre-generated key in header */
1634 if (cd->volume_key) {
1635 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1636 r = vk ? 0 : -ENOMEM;
1638 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1642 /* Read password from file of (if NULL) from terminal */
1644 r = key_from_file(cd, _("Enter any passphrase: "),
1645 &password, &passwordLen,
1646 keyfile, keyfile_offset, keyfile_size);
1648 r = key_from_terminal(cd, _("Enter any passphrase: "),
1649 &password, &passwordLen, 0);
1653 r = LUKS_open_key_with_hdr(mdata_device(cd), CRYPT_ANY_SLOT, password, passwordLen,
1661 r = key_from_file(cd, _("Enter new passphrase for key slot: "),
1662 &new_password, &new_passwordLen, new_keyfile,
1663 new_keyfile_offset, new_keyfile_size);
1665 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1666 &new_password, &new_passwordLen, 1);
1670 r = LUKS_set_key(mdata_device(cd), keyslot, new_password, new_passwordLen,
1671 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1673 crypt_safe_free(password);
1674 crypt_safe_free(new_password);
1675 crypt_free_volume_key(vk);
1676 return r < 0 ? r : keyslot;
1679 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1681 const char *keyfile,
1682 size_t keyfile_size,
1683 const char *new_keyfile,
1684 size_t new_keyfile_size)
1686 return crypt_keyslot_add_by_keyfile_offset(cd, keyslot,
1687 keyfile, keyfile_size, 0,
1688 new_keyfile, new_keyfile_size, 0);
1691 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1693 const char *volume_key,
1694 size_t volume_key_size,
1695 const char *passphrase,
1696 size_t passphrase_size)
1698 struct volume_key *vk = NULL;
1700 char *new_password = NULL; size_t new_passwordLen;
1702 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1704 if (!isLUKS(cd->type)) {
1705 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1710 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1711 else if (cd->volume_key)
1712 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1717 r = LUKS_verify_volume_key(&cd->hdr, vk);
1719 log_err(cd, _("Volume key does not match the volume.\n"));
1723 r = keyslot_verify_or_find_empty(cd, &keyslot);
1728 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1729 &new_password, &new_passwordLen, 1);
1732 passphrase = new_password;
1733 passphrase_size = new_passwordLen;
1736 r = LUKS_set_key(mdata_device(cd), keyslot, passphrase, passphrase_size,
1737 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1739 crypt_safe_free(new_password);
1740 crypt_free_volume_key(vk);
1741 return (r < 0) ? r : keyslot;
1744 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1746 crypt_keyslot_info ki;
1748 log_dbg("Destroying keyslot %d.", keyslot);
1750 if (!isLUKS(cd->type)) {
1751 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1755 ki = crypt_keyslot_status(cd, keyslot);
1756 if (ki == CRYPT_SLOT_INVALID) {
1757 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1761 if (ki == CRYPT_SLOT_INACTIVE) {
1762 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1766 return LUKS_del_key(mdata_device(cd), keyslot, &cd->hdr, cd);
1769 // activation/deactivation of device mapping
1770 int crypt_activate_by_passphrase(struct crypt_device *cd,
1773 const char *passphrase,
1774 size_t passphrase_size,
1777 crypt_status_info ci;
1778 struct volume_key *vk = NULL;
1779 char *read_passphrase = NULL;
1780 size_t passphraseLen = 0;
1783 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1784 name ? "Activating" : "Checking", name ?: "",
1785 keyslot, passphrase ? "" : "[none] ");
1788 ci = crypt_status(NULL, name);
1789 if (ci == CRYPT_INVALID)
1791 else if (ci >= CRYPT_ACTIVE) {
1792 log_err(cd, _("Device %s already exists.\n"), name);
1797 /* plain, use hashed passphrase */
1798 if (isPLAIN(cd->type)) {
1803 r = key_from_terminal(cd, NULL, &read_passphrase,
1807 passphrase = read_passphrase;
1808 passphrase_size = passphraseLen;
1811 r = process_key(cd, cd->plain_hdr.hash,
1813 passphrase, passphrase_size, &vk);
1817 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1819 } else if (isLUKS(cd->type)) {
1820 /* provided passphrase, do not retry */
1822 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase,
1823 passphrase_size, &cd->hdr, &vk, cd);
1825 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1830 r = LUKS1_activate(cd, name, vk, flags);
1835 crypt_safe_free(read_passphrase);
1836 crypt_free_volume_key(vk);
1838 return r < 0 ? r : keyslot;
1841 int crypt_activate_by_keyfile_offset(struct crypt_device *cd,
1844 const char *keyfile,
1845 size_t keyfile_size,
1846 size_t keyfile_offset,
1849 crypt_status_info ci;
1850 struct volume_key *vk = NULL;
1851 char *passphrase_read = NULL;
1852 size_t passphrase_size_read;
1853 unsigned int key_count = 0;
1856 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1857 name ?: "", keyslot, keyfile ?: "[none]");
1860 ci = crypt_status(NULL, name);
1861 if (ci == CRYPT_INVALID)
1863 else if (ci >= CRYPT_ACTIVE) {
1864 log_err(cd, _("Device %s already exists.\n"), name);
1872 if (isPLAIN(cd->type)) {
1876 r = key_from_file(cd, _("Enter passphrase: "),
1877 &passphrase_read, &passphrase_size_read,
1878 keyfile, keyfile_offset, keyfile_size);
1882 r = process_key(cd, cd->plain_hdr.hash,
1884 passphrase_read, passphrase_size_read, &vk);
1888 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1889 } else if (isLUKS(cd->type)) {
1890 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1891 &passphrase_size_read, keyfile, keyfile_offset, keyfile_size);
1894 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase_read,
1895 passphrase_size_read, &cd->hdr, &vk, cd);
1901 r = LUKS1_activate(cd, name, vk, flags);
1906 } else if (isLOOPAES(cd->type)) {
1907 r = key_from_file(cd, NULL, &passphrase_read, &passphrase_size_read,
1908 keyfile, keyfile_offset, keyfile_size);
1911 r = LOOPAES_parse_keyfile(cd, &vk, cd->loopaes_hdr.hash, &key_count,
1912 passphrase_read, passphrase_size_read);
1916 r = LOOPAES_activate(cd, name, cd->loopaes_cipher,
1917 key_count, vk, flags);
1922 crypt_safe_free(passphrase_read);
1923 crypt_free_volume_key(vk);
1928 int crypt_activate_by_keyfile(struct crypt_device *cd,
1931 const char *keyfile,
1932 size_t keyfile_size,
1935 return crypt_activate_by_keyfile_offset(cd, name, keyslot, keyfile,
1936 keyfile_size, 0, flags);
1939 int crypt_activate_by_volume_key(struct crypt_device *cd,
1941 const char *volume_key,
1942 size_t volume_key_size,
1945 crypt_status_info ci;
1946 struct volume_key *vk = NULL;
1949 log_dbg("Activating volume %s by volume key.", name);
1952 ci = crypt_status(NULL, name);
1953 if (ci == CRYPT_INVALID)
1955 else if (ci >= CRYPT_ACTIVE) {
1956 log_err(cd, _("Device %s already exists.\n"), name);
1961 /* use key directly, no hash */
1962 if (isPLAIN(cd->type)) {
1966 if (!volume_key || !volume_key_size || volume_key_size != cd->plain_key_size) {
1967 log_err(cd, _("Incorrect volume key specified for plain device.\n"));
1971 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1975 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1976 } else if (isLUKS(cd->type)) {
1977 /* If key is not provided, try to use internal key */
1979 if (!cd->volume_key) {
1980 log_err(cd, _("Volume key does not match the volume.\n"));
1983 volume_key_size = cd->volume_key->keylength;
1984 volume_key = cd->volume_key->key;
1987 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1990 r = LUKS_verify_volume_key(&cd->hdr, vk);
1993 log_err(cd, _("Volume key does not match the volume.\n"));
1996 r = LUKS1_activate(cd, name, vk, flags);
1997 } else if (isVERITY(cd->type)) {
1998 /* volume_key == root hash */
1999 if (!volume_key || !volume_key_size) {
2000 log_err(cd, _("Incorrect root hash specified for verity device.\n"));
2004 r = VERITY_activate(cd, name, mdata_device(cd),
2005 volume_key, volume_key_size,
2006 &cd->verity_hdr, CRYPT_ACTIVATE_READONLY);
2009 free(cd->verity_root_hash);
2010 cd->verity_root_hash = NULL;
2012 cd->verity_root_hash_size = volume_key_size;
2013 if (!cd->verity_root_hash)
2014 cd->verity_root_hash = malloc(volume_key_size);
2015 if (cd->verity_root_hash)
2016 memcpy(cd->verity_root_hash, volume_key, volume_key_size);
2019 log_err(cd, _("Device type is not properly initialised.\n"));
2021 crypt_free_volume_key(vk);
2026 int crypt_deactivate(struct crypt_device *cd, const char *name)
2033 log_dbg("Deactivating volume %s.", name);
2035 if (!cd && dm_init(NULL, 1) < 0)
2038 switch (crypt_status(cd, name)) {
2041 r = dm_remove_device(name, 0, 0);
2043 case CRYPT_INACTIVE:
2044 log_err(cd, _("Device %s is not active.\n"), name);
2048 log_err(cd, _("Invalid device %s.\n"), name);
2058 int crypt_volume_key_get(struct crypt_device *cd,
2061 size_t *volume_key_size,
2062 const char *passphrase,
2063 size_t passphrase_size)
2065 struct volume_key *vk = NULL;
2069 if (crypt_fips_mode()) {
2070 log_err(cd, "Function not available in FIPS mode.\n");
2074 key_len = crypt_get_volume_key_size(cd);
2075 if (key_len > *volume_key_size) {
2076 log_err(cd, _("Volume key buffer too small.\n"));
2080 if (isPLAIN(cd->type) && cd->plain_hdr.hash) {
2081 r = process_key(cd, cd->plain_hdr.hash, key_len,
2082 passphrase, passphrase_size, &vk);
2084 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
2085 } else if (isLUKS(cd->type)) {
2086 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase,
2087 passphrase_size, &cd->hdr, &vk, cd);
2090 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
2093 memcpy(volume_key, vk->key, vk->keylength);
2094 *volume_key_size = vk->keylength;
2097 crypt_free_volume_key(vk);
2101 int crypt_volume_key_verify(struct crypt_device *cd,
2102 const char *volume_key,
2103 size_t volume_key_size)
2105 struct volume_key *vk;
2108 if (!isLUKS(cd->type)) {
2109 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2113 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2117 r = LUKS_verify_volume_key(&cd->hdr, vk);
2120 log_err(cd, _("Volume key does not match the volume.\n"));
2122 crypt_free_volume_key(vk);
2127 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
2129 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
2130 cd->timeout = timeout_sec;
2133 void crypt_set_password_retry(struct crypt_device *cd, int tries)
2135 log_dbg("Password retry count set to %d.", tries);
2139 void crypt_set_iteration_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2141 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
2142 cd->iteration_time = iteration_time_ms;
2144 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2146 crypt_set_iteration_time(cd, iteration_time_ms);
2149 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
2151 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
2152 cd->password_verify = password_verify ? 1 : 0;
2155 void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
2158 case CRYPT_RNG_URANDOM:
2159 case CRYPT_RNG_RANDOM:
2160 log_dbg("RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
2161 cd->rng_type = rng_type;
2165 int crypt_get_rng_type(struct crypt_device *cd)
2170 return cd->rng_type;
2173 int crypt_memory_lock(struct crypt_device *cd, int lock)
2175 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
2179 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
2183 if (!cd && dm_init(NULL, 1) < 0)
2184 return CRYPT_INVALID;
2186 r = dm_status_device(name);
2191 if (r < 0 && r != -ENODEV)
2192 return CRYPT_INVALID;
2195 return CRYPT_ACTIVE;
2200 return CRYPT_INACTIVE;
2203 static void hexprint(struct crypt_device *cd, const char *d, int n, const char *sep)
2206 for(i = 0; i < n; i++)
2207 log_std(cd, "%02hhx%s", (const char)d[i], sep);
2210 static int _luks_dump(struct crypt_device *cd)
2214 log_std(cd, "LUKS header information for %s\n\n", mdata_device(cd));
2215 log_std(cd, "Version: \t%d\n", cd->hdr.version);
2216 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
2217 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
2218 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec);
2219 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
2220 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
2221 log_std(cd, "MK digest: \t");
2222 hexprint(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE, " ");
2224 log_std(cd, "MK salt: \t");
2225 hexprint(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2, " ");
2226 log_std(cd, "\n \t");
2227 hexprint(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ");
2229 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
2230 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
2231 for(i = 0; i < LUKS_NUMKEYS; i++) {
2232 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
2233 log_std(cd, "Key Slot %d: ENABLED\n",i);
2234 log_std(cd, "\tIterations: \t%d\n",
2235 cd->hdr.keyblock[i].passwordIterations);
2236 log_std(cd, "\tSalt: \t");
2237 hexprint(cd, cd->hdr.keyblock[i].passwordSalt,
2238 LUKS_SALTSIZE/2, " ");
2239 log_std(cd, "\n\t \t");
2240 hexprint(cd, cd->hdr.keyblock[i].passwordSalt +
2241 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ");
2244 log_std(cd, "\tKey material offset:\t%d\n",
2245 cd->hdr.keyblock[i].keyMaterialOffset);
2246 log_std(cd, "\tAF stripes: \t%d\n",
2247 cd->hdr.keyblock[i].stripes);
2250 log_std(cd, "Key Slot %d: DISABLED\n", i);
2255 static int _verity_dump(struct crypt_device *cd)
2257 log_std(cd, "VERITY header information for %s\n", mdata_device(cd));
2258 log_std(cd, "Version: \t%u\n", cd->verity_hdr.version);
2259 log_std(cd, "Data blocks: \t%" PRIu64 "\n", cd->verity_hdr.data_size);
2260 log_std(cd, "Data block size: \t%u\n", cd->verity_hdr.data_block_size);
2261 log_std(cd, "Hash block size: \t%u\n", cd->verity_hdr.hash_block_size);
2262 log_std(cd, "Hash algorithm: \t%s\n", cd->verity_hdr.hash_name);
2263 log_std(cd, "Salt: \t");
2264 if (cd->verity_hdr.salt_size)
2265 hexprint(cd, cd->verity_hdr.salt, cd->verity_hdr.salt_size, "");
2269 if (cd->verity_root_hash) {
2270 log_std(cd, "Root hash: \t");
2271 hexprint(cd, cd->verity_root_hash, cd->verity_root_hash_size, "");
2277 int crypt_dump(struct crypt_device *cd)
2279 if (isLUKS(cd->type))
2280 return _luks_dump(cd);
2281 else if (isVERITY(cd->type))
2282 return _verity_dump(cd);
2284 log_err(cd, _("Dump operation is not supported for this device type.\n"));
2288 const char *crypt_get_cipher(struct crypt_device *cd)
2290 if (isPLAIN(cd->type))
2291 return cd->plain_cipher;
2293 if (isLUKS(cd->type))
2294 return cd->hdr.cipherName;
2296 if (isLOOPAES(cd->type))
2297 return cd->loopaes_cipher;
2302 const char *crypt_get_cipher_mode(struct crypt_device *cd)
2304 if (isPLAIN(cd->type))
2305 return cd->plain_cipher_mode;
2307 if (isLUKS(cd->type))
2308 return cd->hdr.cipherMode;
2310 if (isLOOPAES(cd->type))
2311 return cd->loopaes_cipher_mode;
2316 const char *crypt_get_uuid(struct crypt_device *cd)
2318 if (isLUKS(cd->type))
2319 return cd->hdr.uuid;
2321 if (isPLAIN(cd->type))
2322 return cd->plain_uuid;
2324 if (isLOOPAES(cd->type))
2325 return cd->loopaes_uuid;
2330 const char *crypt_get_device_name(struct crypt_device *cd)
2335 int crypt_get_volume_key_size(struct crypt_device *cd)
2337 if (isPLAIN(cd->type))
2338 return cd->plain_key_size;
2340 if (isLUKS(cd->type))
2341 return cd->hdr.keyBytes;
2343 if (isLOOPAES(cd->type))
2344 return cd->loopaes_key_size;
2346 if (isVERITY(cd->type))
2347 return cd->verity_root_hash_size;
2352 uint64_t crypt_get_data_offset(struct crypt_device *cd)
2354 if (isPLAIN(cd->type))
2355 return cd->plain_hdr.offset;
2357 if (isLUKS(cd->type))
2358 return cd->hdr.payloadOffset;
2360 if (isLOOPAES(cd->type))
2361 return cd->loopaes_hdr.offset;
2366 uint64_t crypt_get_iv_offset(struct crypt_device *cd)
2368 if (isPLAIN(cd->type))
2369 return cd->plain_hdr.skip;
2371 if (isLUKS(cd->type))
2374 if (isLOOPAES(cd->type))
2375 return cd->loopaes_hdr.skip;
2380 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
2382 if (!isLUKS(cd->type)) {
2383 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2384 return CRYPT_SLOT_INVALID;
2387 return LUKS_keyslot_info(&cd->hdr, keyslot);
2390 int crypt_keyslot_max(const char *type)
2392 if (type && isLUKS(type))
2393 return LUKS_NUMKEYS;
2398 const char *crypt_get_type(struct crypt_device *cd)
2403 int crypt_get_verity_info(struct crypt_device *cd,
2404 struct crypt_params_verity *vp)
2406 if (!isVERITY(cd->type) || !vp)
2409 vp->data_device = cd->device;
2410 vp->hash_device = mdata_device(cd);
2411 vp->hash_name = cd->verity_hdr.hash_name;
2412 vp->salt = cd->verity_hdr.salt;
2413 vp->salt_size = cd->verity_hdr.salt_size;
2414 vp->data_block_size = cd->verity_hdr.data_block_size;
2415 vp->hash_block_size = cd->verity_hdr.hash_block_size;
2416 vp->data_size = cd->verity_hdr.data_size;
2417 vp->hash_area_offset = cd->verity_hdr.hash_area_offset;
2418 vp->version = cd->verity_hdr.version;
2419 vp->flags = cd->verity_hdr.flags & CRYPT_VERITY_NO_HEADER;
2423 int crypt_get_active_device(struct crypt_device *cd __attribute__((unused)),
2425 struct crypt_active_device *cad)
2427 struct crypt_dm_active_device dmd;
2430 r = dm_query_device(name, 0, &dmd);
2434 if (dmd.target != DM_CRYPT && dmd.target != DM_VERITY)
2437 cad->offset = dmd.u.crypt.offset;
2438 cad->iv_offset = dmd.u.crypt.iv_offset;
2439 cad->size = dmd.size;
2440 cad->flags = dmd.flags;