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 struct device *device;
40 struct device *metadata_device;
42 struct volume_key *volume_key;
44 uint64_t iteration_time;
49 /* used in CRYPT_LUKS1 */
51 uint64_t PBKDF2_per_sec;
53 /* used in CRYPT_PLAIN */
54 struct crypt_params_plain plain_hdr;
56 char *plain_cipher_mode;
58 unsigned int plain_key_size;
60 /* used in CRYPT_LOOPAES */
61 struct crypt_params_loopaes loopaes_hdr;
63 char *loopaes_cipher_mode;
65 unsigned int loopaes_key_size;
67 /* used in CRYPT_VERITY */
68 struct crypt_params_verity verity_hdr;
69 char *verity_root_hash;
70 unsigned int verity_root_hash_size;
73 /* used in CRYPT_TCRYPT */
74 struct crypt_params_tcrypt tcrypt_params;
75 struct tcrypt_phdr tcrypt_hdr;
77 /* callbacks definitions */
78 void (*log)(int level, const char *msg, void *usrptr);
80 int (*confirm)(const char *msg, void *usrptr);
82 int (*password)(const char *msg, char *buf, size_t length, void *usrptr);
83 void *password_usrptr;
85 /* last error message */
86 char error[MAX_ERROR_LENGTH];
90 /* FIXME: not thread safe, remove this later */
91 static char global_error[MAX_ERROR_LENGTH] = {0};
94 static void (*_default_log)(int level, const char *msg, void *usrptr) = NULL;
95 static int _debug_level = 0;
97 void crypt_set_debug_level(int level)
102 int crypt_get_debug_level(void)
107 static void crypt_set_error(struct crypt_device *cd, const char *error)
109 size_t size = strlen(error);
111 /* Set global error, ugly hack... */
112 strncpy(global_error, error, MAX_ERROR_LENGTH - 2);
113 if (size < MAX_ERROR_LENGTH && global_error[size - 1] == '\n')
114 global_error[size - 1] = '\0';
116 /* Set error string per context */
118 strncpy(cd->error, error, MAX_ERROR_LENGTH - 2);
119 if (size < MAX_ERROR_LENGTH && cd->error[size - 1] == '\n')
120 cd->error[size - 1] = '\0';
124 void crypt_log(struct crypt_device *cd, int level, const char *msg)
127 cd->log(level, msg, cd->log_usrptr);
128 else if (_default_log)
129 _default_log(level, msg, NULL);
131 if (level == CRYPT_LOG_ERROR)
132 crypt_set_error(cd, msg);
135 __attribute__((format(printf, 5, 6)))
136 void logger(struct crypt_device *cd, int level, const char *file,
137 int line, const char *format, ...)
142 va_start(argp, format);
144 if (vasprintf(&target, format, argp) > 0 ) {
146 crypt_log(cd, level, target);
148 } else if (_debug_level)
149 printf("# %s:%d %s\n", file ?: "?", line, target);
151 } else if (_debug_level)
152 printf("# %s\n", target);
160 static const char *mdata_device_path(struct crypt_device *cd)
162 return device_path(cd->metadata_device ?: cd->device);
166 struct device *crypt_metadata_device(struct crypt_device *cd)
168 return cd->metadata_device ?: cd->device;
171 struct device *crypt_data_device(struct crypt_device *cd)
176 int init_crypto(struct crypt_device *ctx)
180 crypt_fips_libcryptsetup_check(ctx);
182 r = crypt_random_init(ctx);
184 log_err(ctx, _("Cannot initialize crypto RNG backend.\n"));
188 r = crypt_backend_init(ctx);
190 log_err(ctx, _("Cannot initialize crypto backend.\n"));
192 log_dbg("Crypto backend (%s) initialized.", crypt_backend_version());
196 static int process_key(struct crypt_device *cd, const char *hash_name,
197 size_t key_size, const char *pass, size_t passLen,
198 struct volume_key **vk)
205 *vk = crypt_alloc_volume_key(key_size, NULL);
210 r = crypt_plain_hash(cd, hash_name, (*vk)->key, key_size, pass, passLen);
213 log_err(cd, _("Hash algorithm %s not supported.\n"),
216 log_err(cd, _("Key processing error (using hash %s).\n"),
218 crypt_free_volume_key(*vk);
222 } else if (passLen > key_size) {
223 memcpy((*vk)->key, pass, key_size);
225 memcpy((*vk)->key, pass, passLen);
231 static int isPLAIN(const char *type)
233 return (type && !strcmp(CRYPT_PLAIN, type));
236 static int isLUKS(const char *type)
238 return (type && !strcmp(CRYPT_LUKS1, type));
241 static int isLOOPAES(const char *type)
243 return (type && !strcmp(CRYPT_LOOPAES, type));
246 static int isVERITY(const char *type)
248 return (type && !strcmp(CRYPT_VERITY, type));
251 static int isTCRYPT(const char *type)
253 return (type && !strcmp(CRYPT_TCRYPT, type));
256 /* keyslot helpers */
257 static int keyslot_verify_or_find_empty(struct crypt_device *cd, int *keyslot)
259 if (*keyslot == CRYPT_ANY_SLOT) {
260 *keyslot = LUKS_keyslot_find_empty(&cd->hdr);
262 log_err(cd, _("All key slots full.\n"));
267 switch (LUKS_keyslot_info(&cd->hdr, *keyslot)) {
268 case CRYPT_SLOT_INVALID:
269 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
270 *keyslot, LUKS_NUMKEYS - 1);
272 case CRYPT_SLOT_INACTIVE:
275 log_err(cd, _("Key slot %d is full, please select another one.\n"),
284 * compares UUIDs returned by device-mapper (striped by cryptsetup) and uuid in header
286 static int crypt_uuid_cmp(const char *dm_uuid, const char *hdr_uuid)
291 if (!dm_uuid || !hdr_uuid)
294 str = strchr(dm_uuid, '-');
298 for (i = 0, j = 1; hdr_uuid[i]; i++) {
299 if (hdr_uuid[i] == '-')
302 if (!str[j] || str[j] == '-')
305 if (str[j] != hdr_uuid[i])
313 int PLAIN_activate(struct crypt_device *cd,
315 struct volume_key *vk,
320 char *dm_cipher = NULL;
321 enum devcheck device_check;
322 struct crypt_dm_active_device dmd = {
324 .uuid = crypt_get_uuid(cd),
327 .data_device = crypt_data_device(cd),
331 .offset = crypt_get_data_offset(cd),
332 .iv_offset = crypt_get_iv_offset(cd),
336 if (dmd.flags & CRYPT_ACTIVATE_SHARED)
337 device_check = DEV_SHARED;
339 device_check = DEV_EXCL;
341 r = device_block_adjust(cd, dmd.data_device, device_check,
342 dmd.u.crypt.offset, &dmd.size, &dmd.flags);
346 if (crypt_get_cipher_mode(cd))
347 r = asprintf(&dm_cipher, "%s-%s", crypt_get_cipher(cd), crypt_get_cipher_mode(cd));
349 r = asprintf(&dm_cipher, "%s", crypt_get_cipher(cd));
353 dmd.u.crypt.cipher = dm_cipher;
354 log_dbg("Trying to activate PLAIN device %s using cipher %s.",
355 name, dmd.u.crypt.cipher);
357 r = dm_create_device(cd, name, CRYPT_PLAIN, &dmd, 0);
360 if (!cd->plain_uuid && dm_query_device(cd, name, DM_ACTIVE_UUID, &dmd) >= 0)
361 cd->plain_uuid = CONST_CAST(char*)dmd.uuid;
367 int crypt_confirm(struct crypt_device *cd, const char *msg)
369 if (!cd || !cd->confirm)
372 return cd->confirm(msg, cd->confirm_usrptr);
375 static int key_from_terminal(struct crypt_device *cd, char *msg, char **key,
376 size_t *key_len, int force_verify)
378 char *prompt = NULL, *device_name;
383 if (crypt_loop_device(crypt_get_device_name(cd)))
384 device_name = crypt_loop_backing_file(crypt_get_device_name(cd));
386 device_name = strdup(crypt_get_device_name(cd));
389 r = asprintf(&prompt, _("Enter passphrase for %s: "), device_name);
397 *key = crypt_safe_alloc(DEFAULT_PASSPHRASE_SIZE_MAX);
402 r = cd->password(msg, *key, DEFAULT_PASSPHRASE_SIZE_MAX,
403 cd->password_usrptr);
405 crypt_safe_free(*key);
410 r = crypt_get_key(msg, key, key_len, 0, 0, NULL, cd->timeout,
411 (force_verify || cd->password_verify), cd);
414 return (r < 0) ? r: 0;
417 static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslot,
418 struct volume_key **vk)
420 char *passphrase_read = NULL;
421 size_t passphrase_size_read;
422 int r = -EINVAL, eperm = 0, tries = cd->tries;
426 crypt_free_volume_key(*vk);
429 r = key_from_terminal(cd, NULL, &passphrase_read,
430 &passphrase_size_read, 0);
431 /* Continue if it is just passphrase verify mismatch */
437 r = LUKS_open_key_with_hdr(keyslot, passphrase_read,
438 passphrase_size_read, &cd->hdr, vk, cd);
441 crypt_safe_free(passphrase_read);
442 passphrase_read = NULL;
443 } while (r == -EPERM && (--tries > 0));
446 crypt_free_volume_key(*vk);
449 /* Report wrong passphrase if at least one try failed */
450 if (eperm && r == -EPIPE)
454 crypt_safe_free(passphrase_read);
458 static int key_from_file(struct crypt_device *cd, char *msg,
459 char **key, size_t *key_len,
460 const char *key_file, size_t key_offset,
463 return crypt_get_key(msg, key, key_len, key_offset, key_size, key_file,
467 void crypt_set_log_callback(struct crypt_device *cd,
468 void (*log)(int level, const char *msg, void *usrptr),
475 cd->log_usrptr = usrptr;
479 void crypt_set_confirm_callback(struct crypt_device *cd,
480 int (*confirm)(const char *msg, void *usrptr),
483 cd->confirm = confirm;
484 cd->confirm_usrptr = usrptr;
487 void crypt_set_password_callback(struct crypt_device *cd,
488 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
491 cd->password = password;
492 cd->password_usrptr = usrptr;
495 static void _get_error(char *error, char *buf, size_t size)
497 if (!buf || size < 1)
500 strncpy(buf, error, size - 1);
501 buf[size - 1] = '\0';
507 void crypt_last_error(struct crypt_device *cd, char *buf, size_t size)
510 return _get_error(cd->error, buf, size);
513 /* Deprecated global error interface */
514 void crypt_get_error(char *buf, size_t size)
516 return _get_error(global_error, buf, size);
519 const char *crypt_get_dir(void)
524 int crypt_init(struct crypt_device **cd, const char *device)
526 struct crypt_device *h = NULL;
532 log_dbg("Allocating crypt device %s context.", device);
534 if (!(h = malloc(sizeof(struct crypt_device))))
537 memset(h, 0, sizeof(*h));
539 r = device_alloc(&h->device, device);
545 h->iteration_time = 1000;
546 h->password_verify = 0;
548 h->rng_type = crypt_random_default_key_rng();
552 device_free(h->device);
557 static int crypt_check_data_device_size(struct crypt_device *cd)
560 uint64_t size, size_min;
562 /* Check data device size, require at least one sector */
563 size_min = crypt_get_data_offset(cd) << SECTOR_SHIFT ?: SECTOR_SIZE;
565 r = device_size(cd->device, &size);
569 if (size < size_min) {
570 log_err(cd, _("Header detected but device %s is too small.\n"),
571 device_path(cd->device));
578 int crypt_set_data_device(struct crypt_device *cd, const char *device)
580 struct device *dev = NULL;
583 log_dbg("Setting ciphertext data device to %s.", device ?: "(none)");
585 if (!isLUKS(cd->type) && !isVERITY(cd->type)) {
586 log_err(cd, _("This operation is not supported for this device type.\n"));
590 /* metadata device must be set */
591 if (!cd->device || !device)
594 r = device_alloc(&dev, device);
598 if (!cd->metadata_device) {
599 cd->metadata_device = cd->device;
601 device_free(cd->device);
605 return crypt_check_data_device_size(cd);
608 static int _crypt_load_luks1(struct crypt_device *cd, int require_header, int repair)
610 struct luks_phdr hdr;
617 r = LUKS_read_phdr(&hdr, require_header, repair, cd);
621 if (!cd->type && !(cd->type = strdup(CRYPT_LUKS1)))
624 memcpy(&cd->hdr, &hdr, sizeof(hdr));
629 static int _crypt_load_tcrypt(struct crypt_device *cd, struct crypt_params_tcrypt *params)
637 r = TCRYPT_read_phdr(cd, &cd->tcrypt_hdr, &cd->tcrypt_params,
638 params->passphrase, params->passphrase_size,
643 if (!cd->type && !(cd->type = strdup(CRYPT_TCRYPT)))
649 static int _crypt_load_verity(struct crypt_device *cd, struct crypt_params_verity *params)
652 size_t sb_offset = 0;
658 if (params && params->flags & CRYPT_VERITY_NO_HEADER)
662 sb_offset = params->hash_area_offset;
664 r = VERITY_read_sb(cd, sb_offset, &cd->verity_uuid, &cd->verity_hdr);
669 cd->verity_hdr.flags = params->flags;
671 /* Hash availability checked in sb load */
672 cd->verity_root_hash_size = crypt_hash_size(cd->verity_hdr.hash_name);
673 if (cd->verity_root_hash_size > 4096)
676 if (!cd->type && !(cd->type = strdup(CRYPT_VERITY)))
679 if (params && params->data_device &&
680 (r = crypt_set_data_device(cd, params->data_device)) < 0)
686 static int _init_by_name_crypt(struct crypt_device *cd, const char *name)
688 struct crypt_dm_active_device dmd = {};
689 char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
692 r = dm_query_device(cd, name,
695 DM_ACTIVE_CRYPT_CIPHER |
696 DM_ACTIVE_CRYPT_KEYSIZE, &dmd);
700 if (isPLAIN(cd->type)) {
701 cd->plain_uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
702 cd->plain_hdr.hash = NULL; /* no way to get this */
703 cd->plain_hdr.offset = dmd.u.crypt.offset;
704 cd->plain_hdr.skip = dmd.u.crypt.iv_offset;
705 cd->plain_key_size = dmd.u.crypt.vk->keylength;
707 r = crypt_parse_name_and_mode(dmd.u.crypt.cipher, cipher, NULL, cipher_mode);
709 cd->plain_cipher = strdup(cipher);
710 cd->plain_cipher_mode = strdup(cipher_mode);
712 } else if (isLOOPAES(cd->type)) {
713 cd->loopaes_uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
714 cd->loopaes_hdr.offset = dmd.u.crypt.offset;
716 r = crypt_parse_name_and_mode(dmd.u.crypt.cipher, cipher,
717 &key_nums, cipher_mode);
719 cd->loopaes_cipher = strdup(cipher);
720 cd->loopaes_cipher_mode = strdup(cipher_mode);
721 /* version 3 uses last key for IV */
722 if (dmd.u.crypt.vk->keylength % key_nums)
724 cd->loopaes_key_size = dmd.u.crypt.vk->keylength / key_nums;
726 } else if (isLUKS(cd->type)) {
727 if (crypt_metadata_device(cd)) {
728 r = _crypt_load_luks1(cd, 0, 0);
730 log_dbg("LUKS device header does not match active device.");
736 /* check whether UUIDs match each other */
737 r = crypt_uuid_cmp(dmd.uuid, cd->hdr.uuid);
739 log_dbg("LUKS device header uuid: %s mismatches DM returned uuid %s",
740 cd->hdr.uuid, dmd.uuid);
749 crypt_free_volume_key(dmd.u.crypt.vk);
750 device_free(dmd.data_device);
751 free(CONST_CAST(void*)dmd.u.crypt.cipher);
752 free(CONST_CAST(void*)dmd.uuid);
756 static int _init_by_name_verity(struct crypt_device *cd, const char *name)
758 struct crypt_params_verity params = {};
759 struct crypt_dm_active_device dmd = {
761 .u.verity.vp = ¶ms,
765 r = dm_query_device(cd, name,
768 DM_ACTIVE_VERITY_HASH_DEVICE |
769 DM_ACTIVE_VERITY_PARAMS, &dmd);
773 if (isVERITY(cd->type)) {
774 cd->verity_uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
775 cd->verity_hdr.flags = CRYPT_VERITY_NO_HEADER; //FIXME
776 cd->verity_hdr.data_size = params.data_size;
777 cd->verity_root_hash_size = dmd.u.verity.root_hash_size;
778 cd->verity_root_hash = NULL;
779 cd->verity_hdr.hash_name = params.hash_name;
780 cd->verity_hdr.data_device = NULL;
781 cd->verity_hdr.hash_device = NULL;
782 cd->verity_hdr.data_block_size = params.data_block_size;
783 cd->verity_hdr.hash_block_size = params.hash_block_size;
784 cd->verity_hdr.hash_area_offset = dmd.u.verity.hash_offset;
785 cd->verity_hdr.hash_type = params.hash_type;
786 cd->verity_hdr.flags = params.flags;
787 cd->verity_hdr.salt_size = params.salt_size;
788 cd->verity_hdr.salt = params.salt;
789 cd->metadata_device = dmd.u.verity.hash_device;
792 device_free(dmd.data_device);
793 free(CONST_CAST(void*)dmd.uuid);
797 int crypt_init_by_name_and_header(struct crypt_device **cd,
799 const char *header_device)
801 crypt_status_info ci;
802 struct crypt_dm_active_device dmd;
805 log_dbg("Allocating crypt device context by device %s.", name);
807 ci = crypt_status(NULL, name);
808 if (ci == CRYPT_INVALID)
811 if (ci < CRYPT_ACTIVE) {
812 log_err(NULL, _("Device %s is not active.\n"), name);
816 r = dm_query_device(NULL, name, DM_ACTIVE_DEVICE | DM_ACTIVE_UUID, &dmd);
823 r = crypt_init(cd, header_device);
825 r = crypt_init(cd, device_path(dmd.data_device));
827 /* Underlying device disappeared but mapping still active */
828 if (!dmd.data_device || r == -ENOTBLK)
829 log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
832 /* Underlying device is not readable but crypt mapping exists */
834 device_free(dmd.data_device);
835 dmd.data_device = NULL;
836 r = crypt_init(cd, NULL);
844 if (!strncmp(CRYPT_PLAIN, dmd.uuid, sizeof(CRYPT_PLAIN)-1))
845 (*cd)->type = strdup(CRYPT_PLAIN);
846 else if (!strncmp(CRYPT_LOOPAES, dmd.uuid, sizeof(CRYPT_LOOPAES)-1))
847 (*cd)->type = strdup(CRYPT_LOOPAES);
848 else if (!strncmp(CRYPT_LUKS1, dmd.uuid, sizeof(CRYPT_LUKS1)-1))
849 (*cd)->type = strdup(CRYPT_LUKS1);
850 else if (!strncmp(CRYPT_VERITY, dmd.uuid, sizeof(CRYPT_VERITY)-1))
851 (*cd)->type = strdup(CRYPT_VERITY);
853 log_dbg("Unknown UUID set, some parameters are not set.");
855 log_dbg("Active device has no UUID set, some parameters are not set.");
858 r = crypt_set_data_device(*cd, device_path(dmd.data_device));
863 /* Try to initialise basic parameters from active device */
865 if (dmd.target == DM_CRYPT)
866 r = _init_by_name_crypt(*cd, name);
867 else if (dmd.target == DM_VERITY)
868 r = _init_by_name_verity(*cd, name);
874 device_free(dmd.data_device);
875 free(CONST_CAST(void*)dmd.uuid);
879 int crypt_init_by_name(struct crypt_device **cd, const char *name)
881 return crypt_init_by_name_and_header(cd, name, NULL);
884 static int _crypt_format_plain(struct crypt_device *cd,
886 const char *cipher_mode,
888 size_t volume_key_size,
889 struct crypt_params_plain *params)
891 if (!cipher || !cipher_mode) {
892 log_err(cd, _("Invalid plain crypt parameters.\n"));
896 if (volume_key_size > 1024) {
897 log_err(cd, _("Invalid key size.\n"));
901 if (!(cd->type = strdup(CRYPT_PLAIN)))
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 (!crypt_metadata_device(cd)) {
941 log_err(cd, _("Can't format LUKS without device.\n"));
945 if (!(cd->type = strdup(CRYPT_LUKS1)))
949 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
952 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
957 if (params && params->data_device) {
958 cd->metadata_device = cd->device;
960 if (device_alloc(&cd->device, params->data_device) < 0)
962 required_alignment = params->data_alignment * SECTOR_SIZE;
963 } else if (params && params->data_alignment) {
964 required_alignment = params->data_alignment * SECTOR_SIZE;
966 device_topology_alignment(cd->device,
968 &alignment_offset, DEFAULT_DISK_ALIGNMENT);
970 /* Check early if we cannot allocate block device for key slot access */
971 r = device_block_adjust(cd, cd->device, DEV_OK, 0, NULL, NULL);
975 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
976 (params && params->hash) ? params->hash : "sha1",
978 required_alignment / SECTOR_SIZE,
979 alignment_offset / SECTOR_SIZE,
980 cd->iteration_time, &cd->PBKDF2_per_sec,
981 cd->metadata_device ? 1 : 0, cd);
985 /* Wipe first 8 sectors - fs magic numbers etc. */
986 r = crypt_wipe(crypt_metadata_device(cd), 0, 8 * SECTOR_SIZE, CRYPT_WIPE_ZERO, 1);
989 log_err(cd, _("Cannot format device %s which is still in use.\n"),
990 mdata_device_path(cd));
991 else if (r == -EACCES) {
992 log_err(cd, _("Cannot format device %s, permission denied.\n"),
993 mdata_device_path(cd));
996 log_err(cd, _("Cannot wipe header on device %s.\n"),
997 mdata_device_path(cd));
1002 r = LUKS_write_phdr(&cd->hdr, cd);
1007 static int _crypt_format_loopaes(struct crypt_device *cd,
1010 size_t volume_key_size,
1011 struct crypt_params_loopaes *params)
1013 if (!crypt_metadata_device(cd)) {
1014 log_err(cd, _("Can't format LOOPAES without device.\n"));
1018 if (volume_key_size > 1024) {
1019 log_err(cd, _("Invalid key size.\n"));
1023 if (!(cd->type = strdup(CRYPT_LOOPAES)))
1026 cd->loopaes_key_size = volume_key_size;
1028 cd->loopaes_cipher = strdup(cipher ?: DEFAULT_LOOPAES_CIPHER);
1031 cd->loopaes_uuid = strdup(uuid);
1033 if (params && params->hash)
1034 cd->loopaes_hdr.hash = strdup(params->hash);
1036 cd->loopaes_hdr.offset = params ? params->offset : 0;
1037 cd->loopaes_hdr.skip = params ? params->skip : 0;
1042 static int _crypt_format_verity(struct crypt_device *cd,
1044 struct crypt_params_verity *params)
1046 int r = 0, hash_size;
1047 uint64_t data_device_size;
1049 if (!crypt_metadata_device(cd)) {
1050 log_err(cd, _("Can't format VERITY without device.\n"));
1054 if (!params || !params->data_device)
1057 if (params->hash_type > VERITY_MAX_HASH_TYPE) {
1058 log_err(cd, _("Unsupported VERITY hash type %d.\n"), params->hash_type);
1062 if (VERITY_BLOCK_SIZE_OK(params->data_block_size) ||
1063 VERITY_BLOCK_SIZE_OK(params->hash_block_size)) {
1064 log_err(cd, _("Unsupported VERITY block size.\n"));
1068 if (params->hash_area_offset % 512) {
1069 log_err(cd, _("Unsupported VERITY hash offset.\n"));
1073 if (!(cd->type = strdup(CRYPT_VERITY)))
1076 r = crypt_set_data_device(cd, params->data_device);
1079 if (!params->data_size) {
1080 r = device_size(cd->device, &data_device_size);
1084 cd->verity_hdr.data_size = data_device_size / params->data_block_size;
1086 cd->verity_hdr.data_size = params->data_size;
1088 hash_size = crypt_hash_size(params->hash_name);
1089 if (hash_size <= 0) {
1090 log_err(cd, _("Hash algorithm %s not supported.\n"),
1094 cd->verity_root_hash_size = hash_size;
1096 cd->verity_root_hash = malloc(cd->verity_root_hash_size);
1097 if (!cd->verity_root_hash)
1100 cd->verity_hdr.flags = params->flags;
1101 if (!(cd->verity_hdr.hash_name = strdup(params->hash_name)))
1103 cd->verity_hdr.data_device = NULL;
1104 cd->verity_hdr.data_block_size = params->data_block_size;
1105 cd->verity_hdr.hash_block_size = params->hash_block_size;
1106 cd->verity_hdr.hash_area_offset = params->hash_area_offset;
1107 cd->verity_hdr.hash_type = params->hash_type;
1108 cd->verity_hdr.flags = params->flags;
1109 cd->verity_hdr.salt_size = params->salt_size;
1110 if (!(cd->verity_hdr.salt = malloc(params->salt_size)))
1114 memcpy(CONST_CAST(char*)cd->verity_hdr.salt, params->salt,
1117 r = crypt_random_get(cd, CONST_CAST(char*)cd->verity_hdr.salt,
1118 params->salt_size, CRYPT_RND_SALT);
1122 if (params->flags & CRYPT_VERITY_CREATE_HASH) {
1123 r = VERITY_create(cd, &cd->verity_hdr,
1124 cd->verity_root_hash, cd->verity_root_hash_size);
1129 if (!(params->flags & CRYPT_VERITY_NO_HEADER)) {
1131 cd->verity_uuid = strdup(uuid);
1133 r = VERITY_UUID_generate(cd, &cd->verity_uuid);
1138 r = VERITY_write_sb(cd, cd->verity_hdr.hash_area_offset,
1145 int crypt_format(struct crypt_device *cd,
1148 const char *cipher_mode,
1150 const char *volume_key,
1151 size_t volume_key_size,
1160 log_dbg("Context already formatted as %s.", cd->type);
1164 log_dbg("Formatting device %s as type %s.", mdata_device_path(cd) ?: "(none)", type);
1166 r = init_crypto(cd);
1171 r = _crypt_format_plain(cd, cipher, cipher_mode,
1172 uuid, volume_key_size, params);
1173 else if (isLUKS(type))
1174 r = _crypt_format_luks1(cd, cipher, cipher_mode,
1175 uuid, volume_key, volume_key_size, params);
1176 else if (isLOOPAES(type))
1177 r = _crypt_format_loopaes(cd, cipher, uuid, volume_key_size, params);
1178 else if (isVERITY(type))
1179 r = _crypt_format_verity(cd, uuid, params);
1181 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
1188 crypt_free_volume_key(cd->volume_key);
1189 cd->volume_key = NULL;
1195 int crypt_load(struct crypt_device *cd,
1196 const char *requested_type,
1201 log_dbg("Trying to load %s crypt type from device %s.",
1202 requested_type ?: "any", mdata_device_path(cd) ?: "(none)");
1204 if (!crypt_metadata_device(cd))
1207 if (!requested_type || isLUKS(requested_type)) {
1208 if (cd->type && !isLUKS(cd->type)) {
1209 log_dbg("Context is already initialised to type %s", cd->type);
1213 r = _crypt_load_luks1(cd, 1, 0);
1214 } else if (isVERITY(requested_type)) {
1215 if (cd->type && !isVERITY(cd->type)) {
1216 log_dbg("Context is already initialised to type %s", cd->type);
1219 r = _crypt_load_verity(cd, params);
1220 } else if (isTCRYPT(requested_type)) {
1221 if (cd->type && !isTCRYPT(cd->type)) {
1222 log_dbg("Context is already initialised to type %s", cd->type);
1225 r = _crypt_load_tcrypt(cd, params);
1232 int crypt_repair(struct crypt_device *cd,
1233 const char *requested_type,
1234 void *params __attribute__((unused)))
1238 log_dbg("Trying to repair %s crypt type from device %s.",
1239 requested_type ?: "any", mdata_device_path(cd) ?: "(none)");
1241 if (!crypt_metadata_device(cd))
1244 if (requested_type && !isLUKS(requested_type))
1248 /* Load with repair */
1249 r = _crypt_load_luks1(cd, 1, 1);
1253 /* cd->type and header must be set in context */
1254 r = crypt_check_data_device_size(cd);
1263 int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
1265 struct crypt_dm_active_device dmd;
1268 /* Device context type must be initialised */
1269 if (!cd->type || !crypt_get_uuid(cd))
1272 log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
1274 r = dm_query_device(cd, name, DM_ACTIVE_DEVICE | DM_ACTIVE_CRYPT_CIPHER |
1275 DM_ACTIVE_UUID | DM_ACTIVE_CRYPT_KEYSIZE |
1276 DM_ACTIVE_CRYPT_KEY, &dmd);
1278 log_err(NULL, _("Device %s is not active.\n"), name);
1282 if (!dmd.uuid || dmd.target != DM_CRYPT) {
1287 r = device_block_adjust(cd, dmd.data_device, DEV_OK,
1288 dmd.u.crypt.offset, &new_size, &dmd.flags);
1292 if (new_size == dmd.size) {
1293 log_dbg("Device has already requested size %" PRIu64
1294 " sectors.", dmd.size);
1297 dmd.size = new_size;
1298 r = dm_create_device(cd, name, cd->type, &dmd, 1);
1301 if (dmd.target == DM_CRYPT) {
1302 crypt_free_volume_key(dmd.u.crypt.vk);
1303 free(CONST_CAST(void*)dmd.u.crypt.cipher);
1305 free(CONST_CAST(void*)dmd.data_device);
1306 free(CONST_CAST(void*)dmd.uuid);
1311 int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
1313 if (!isLUKS(cd->type)) {
1314 log_err(cd, _("This operation is not supported for this device type.\n"));
1318 if (uuid && !strncmp(uuid, cd->hdr.uuid, sizeof(cd->hdr.uuid))) {
1319 log_dbg("UUID is the same as requested (%s) for device %s.",
1320 uuid, mdata_device_path(cd));
1325 log_dbg("Requested new UUID change to %s for %s.", uuid, mdata_device_path(cd));
1327 log_dbg("Requested new UUID refresh for %s.", mdata_device_path(cd));
1329 if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
1332 return LUKS_hdr_uuid_set(&cd->hdr, uuid, cd);
1335 int crypt_header_backup(struct crypt_device *cd,
1336 const char *requested_type,
1337 const char *backup_file)
1341 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1344 r = init_crypto(cd);
1348 log_dbg("Requested header backup of device %s (%s) to "
1349 "file %s.", mdata_device_path(cd), requested_type, backup_file);
1351 return LUKS_hdr_backup(backup_file, &cd->hdr, cd);
1354 int crypt_header_restore(struct crypt_device *cd,
1355 const char *requested_type,
1356 const char *backup_file)
1360 if (requested_type && !isLUKS(requested_type))
1363 r = init_crypto(cd);
1367 log_dbg("Requested header restore to device %s (%s) from "
1368 "file %s.", mdata_device_path(cd), requested_type, backup_file);
1370 return LUKS_hdr_restore(backup_file, &cd->hdr, cd);
1373 void crypt_free(struct crypt_device *cd)
1376 log_dbg("Releasing crypt device %s context.", mdata_device_path(cd));
1379 crypt_free_volume_key(cd->volume_key);
1381 device_free(cd->device);
1382 device_free(cd->metadata_device);
1385 /* used in plain device only */
1386 free(CONST_CAST(void*)cd->plain_hdr.hash);
1387 free(cd->plain_cipher);
1388 free(cd->plain_cipher_mode);
1389 free(cd->plain_uuid);
1391 /* used in loop-AES device only */
1392 free(CONST_CAST(void*)cd->loopaes_hdr.hash);
1393 free(cd->loopaes_cipher);
1394 free(cd->loopaes_uuid);
1396 /* used in verity device only */
1397 free(CONST_CAST(void*)cd->verity_hdr.hash_name);
1398 free(CONST_CAST(void*)cd->verity_hdr.salt);
1399 free(cd->verity_root_hash);
1400 free(cd->verity_uuid);
1406 int crypt_suspend(struct crypt_device *cd,
1409 crypt_status_info ci;
1412 log_dbg("Suspending volume %s.", name);
1414 if (!cd || !isLUKS(cd->type)) {
1415 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1420 ci = crypt_status(NULL, name);
1421 if (ci < CRYPT_ACTIVE) {
1422 log_err(cd, _("Volume %s is not active.\n"), name);
1428 r = dm_status_suspended(cd, name);
1433 log_err(cd, _("Volume %s is already suspended.\n"), name);
1438 r = dm_suspend_and_wipe_key(cd, name);
1440 log_err(cd, "Suspend is not supported for device %s.\n", name);
1442 log_err(cd, "Error during suspending device %s.\n", name);
1448 int crypt_resume_by_passphrase(struct crypt_device *cd,
1451 const char *passphrase,
1452 size_t passphrase_size)
1454 struct volume_key *vk = NULL;
1457 log_dbg("Resuming volume %s.", name);
1459 if (!isLUKS(cd->type)) {
1460 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1465 r = dm_status_suspended(cd, name);
1470 log_err(cd, _("Volume %s is not suspended.\n"), name);
1475 r = LUKS_open_key_with_hdr(keyslot, passphrase, passphrase_size,
1478 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1482 r = dm_resume_and_reinstate_key(cd, name, vk->keylength, vk->key);
1484 log_err(cd, "Resume is not supported for device %s.\n", name);
1486 log_err(cd, "Error during resuming device %s.\n", name);
1490 crypt_free_volume_key(vk);
1491 return r < 0 ? r : keyslot;
1494 int crypt_resume_by_keyfile_offset(struct crypt_device *cd,
1497 const char *keyfile,
1498 size_t keyfile_size,
1499 size_t keyfile_offset)
1501 struct volume_key *vk = NULL;
1502 char *passphrase_read = NULL;
1503 size_t passphrase_size_read;
1506 log_dbg("Resuming volume %s.", name);
1508 if (!isLUKS(cd->type)) {
1509 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1514 r = dm_status_suspended(cd, name);
1519 log_err(cd, _("Volume %s is not suspended.\n"), name);
1526 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1527 &passphrase_size_read, keyfile, keyfile_offset,
1532 r = LUKS_open_key_with_hdr(keyslot, passphrase_read,
1533 passphrase_size_read, &cd->hdr, &vk, cd);
1538 r = dm_resume_and_reinstate_key(cd, name, vk->keylength, vk->key);
1540 log_err(cd, "Error during resuming device %s.\n", name);
1542 crypt_safe_free(passphrase_read);
1543 crypt_free_volume_key(vk);
1544 return r < 0 ? r : keyslot;
1547 int crypt_resume_by_keyfile(struct crypt_device *cd,
1550 const char *keyfile,
1551 size_t keyfile_size)
1553 return crypt_resume_by_keyfile_offset(cd, name, keyslot,
1554 keyfile, keyfile_size, 0);
1557 // slot manipulation
1558 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1559 int keyslot, // -1 any
1560 const char *passphrase, // NULL -> terminal
1561 size_t passphrase_size,
1562 const char *new_passphrase, // NULL -> terminal
1563 size_t new_passphrase_size)
1565 struct volume_key *vk = NULL;
1566 char *password = NULL, *new_password = NULL;
1567 size_t passwordLen, new_passwordLen;
1570 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1571 "new passphrase %sprovided.",
1572 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1574 if (!isLUKS(cd->type)) {
1575 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1579 r = keyslot_verify_or_find_empty(cd, &keyslot);
1583 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1584 /* No slots used, try to use pre-generated key in header */
1585 if (cd->volume_key) {
1586 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1587 r = vk ? 0 : -ENOMEM;
1589 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1592 } else if (passphrase) {
1593 /* Passphrase provided, use it to unlock existing keyslot */
1594 r = LUKS_open_key_with_hdr(CRYPT_ANY_SLOT, passphrase,
1595 passphrase_size, &cd->hdr, &vk, cd);
1597 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1598 r = key_from_terminal(cd, _("Enter any passphrase: "),
1599 &password, &passwordLen, 0);
1603 r = LUKS_open_key_with_hdr(CRYPT_ANY_SLOT, password,
1604 passwordLen, &cd->hdr, &vk, cd);
1605 crypt_safe_free(password);
1611 if (new_passphrase) {
1612 new_password = CONST_CAST(char*)new_passphrase;
1613 new_passwordLen = new_passphrase_size;
1615 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1616 &new_password, &new_passwordLen, 1);
1621 r = LUKS_set_key(keyslot, new_password, new_passwordLen,
1622 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1627 if (!new_passphrase)
1628 crypt_safe_free(new_password);
1629 crypt_free_volume_key(vk);
1630 return r ?: keyslot;
1633 int crypt_keyslot_add_by_keyfile_offset(struct crypt_device *cd,
1635 const char *keyfile,
1636 size_t keyfile_size,
1637 size_t keyfile_offset,
1638 const char *new_keyfile,
1639 size_t new_keyfile_size,
1640 size_t new_keyfile_offset)
1642 struct volume_key *vk = NULL;
1643 char *password = NULL; size_t passwordLen;
1644 char *new_password = NULL; size_t new_passwordLen;
1647 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1648 keyfile ?: "[none]", new_keyfile ?: "[none]");
1650 if (!isLUKS(cd->type)) {
1651 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1655 r = keyslot_verify_or_find_empty(cd, &keyslot);
1659 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1660 /* No slots used, try to use pre-generated key in header */
1661 if (cd->volume_key) {
1662 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1663 r = vk ? 0 : -ENOMEM;
1665 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1669 /* Read password from file of (if NULL) from terminal */
1671 r = key_from_file(cd, _("Enter any passphrase: "),
1672 &password, &passwordLen,
1673 keyfile, keyfile_offset, keyfile_size);
1675 r = key_from_terminal(cd, _("Enter any passphrase: "),
1676 &password, &passwordLen, 0);
1680 r = LUKS_open_key_with_hdr(CRYPT_ANY_SLOT, password, passwordLen,
1688 r = key_from_file(cd, _("Enter new passphrase for key slot: "),
1689 &new_password, &new_passwordLen, new_keyfile,
1690 new_keyfile_offset, new_keyfile_size);
1692 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1693 &new_password, &new_passwordLen, 1);
1697 r = LUKS_set_key(keyslot, new_password, new_passwordLen,
1698 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1700 crypt_safe_free(password);
1701 crypt_safe_free(new_password);
1702 crypt_free_volume_key(vk);
1703 return r < 0 ? r : keyslot;
1706 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1708 const char *keyfile,
1709 size_t keyfile_size,
1710 const char *new_keyfile,
1711 size_t new_keyfile_size)
1713 return crypt_keyslot_add_by_keyfile_offset(cd, keyslot,
1714 keyfile, keyfile_size, 0,
1715 new_keyfile, new_keyfile_size, 0);
1718 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1720 const char *volume_key,
1721 size_t volume_key_size,
1722 const char *passphrase,
1723 size_t passphrase_size)
1725 struct volume_key *vk = NULL;
1727 char *new_password = NULL; size_t new_passwordLen;
1729 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1731 if (!isLUKS(cd->type)) {
1732 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1737 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1738 else if (cd->volume_key)
1739 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1744 r = LUKS_verify_volume_key(&cd->hdr, vk);
1746 log_err(cd, _("Volume key does not match the volume.\n"));
1750 r = keyslot_verify_or_find_empty(cd, &keyslot);
1755 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1756 &new_password, &new_passwordLen, 1);
1759 passphrase = new_password;
1760 passphrase_size = new_passwordLen;
1763 r = LUKS_set_key(keyslot, passphrase, passphrase_size,
1764 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1766 crypt_safe_free(new_password);
1767 crypt_free_volume_key(vk);
1768 return (r < 0) ? r : keyslot;
1771 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1773 crypt_keyslot_info ki;
1775 log_dbg("Destroying keyslot %d.", keyslot);
1777 if (!isLUKS(cd->type)) {
1778 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1782 ki = crypt_keyslot_status(cd, keyslot);
1783 if (ki == CRYPT_SLOT_INVALID) {
1784 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1788 if (ki == CRYPT_SLOT_INACTIVE) {
1789 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1793 return LUKS_del_key(keyslot, &cd->hdr, cd);
1796 // activation/deactivation of device mapping
1797 int crypt_activate_by_passphrase(struct crypt_device *cd,
1800 const char *passphrase,
1801 size_t passphrase_size,
1804 crypt_status_info ci;
1805 struct volume_key *vk = NULL;
1806 char *read_passphrase = NULL;
1807 size_t passphraseLen = 0;
1810 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1811 name ? "Activating" : "Checking", name ?: "",
1812 keyslot, passphrase ? "" : "[none] ");
1815 ci = crypt_status(NULL, name);
1816 if (ci == CRYPT_INVALID)
1818 else if (ci >= CRYPT_ACTIVE) {
1819 log_err(cd, _("Device %s already exists.\n"), name);
1824 /* plain, use hashed passphrase */
1825 if (isPLAIN(cd->type)) {
1830 r = key_from_terminal(cd, NULL, &read_passphrase,
1834 passphrase = read_passphrase;
1835 passphrase_size = passphraseLen;
1838 r = process_key(cd, cd->plain_hdr.hash,
1840 passphrase, passphrase_size, &vk);
1844 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1846 } else if (isLUKS(cd->type)) {
1847 /* provided passphrase, do not retry */
1849 r = LUKS_open_key_with_hdr(keyslot, passphrase,
1850 passphrase_size, &cd->hdr, &vk, cd);
1852 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1857 r = LUKS1_activate(cd, name, vk, flags);
1862 crypt_safe_free(read_passphrase);
1863 crypt_free_volume_key(vk);
1865 return r < 0 ? r : keyslot;
1868 int crypt_activate_by_keyfile_offset(struct crypt_device *cd,
1871 const char *keyfile,
1872 size_t keyfile_size,
1873 size_t keyfile_offset,
1876 crypt_status_info ci;
1877 struct volume_key *vk = NULL;
1878 char *passphrase_read = NULL;
1879 size_t passphrase_size_read;
1880 unsigned int key_count = 0;
1883 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1884 name ?: "", keyslot, keyfile ?: "[none]");
1887 ci = crypt_status(NULL, name);
1888 if (ci == CRYPT_INVALID)
1890 else if (ci >= CRYPT_ACTIVE) {
1891 log_err(cd, _("Device %s already exists.\n"), name);
1899 if (isPLAIN(cd->type)) {
1903 r = key_from_file(cd, _("Enter passphrase: "),
1904 &passphrase_read, &passphrase_size_read,
1905 keyfile, keyfile_offset, keyfile_size);
1909 r = process_key(cd, cd->plain_hdr.hash,
1911 passphrase_read, passphrase_size_read, &vk);
1915 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1916 } else if (isLUKS(cd->type)) {
1917 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1918 &passphrase_size_read, keyfile, keyfile_offset, keyfile_size);
1921 r = LUKS_open_key_with_hdr(keyslot, passphrase_read,
1922 passphrase_size_read, &cd->hdr, &vk, cd);
1928 r = LUKS1_activate(cd, name, vk, flags);
1933 } else if (isLOOPAES(cd->type)) {
1934 r = key_from_file(cd, NULL, &passphrase_read, &passphrase_size_read,
1935 keyfile, keyfile_offset, keyfile_size);
1938 r = LOOPAES_parse_keyfile(cd, &vk, cd->loopaes_hdr.hash, &key_count,
1939 passphrase_read, passphrase_size_read);
1943 r = LOOPAES_activate(cd, name, cd->loopaes_cipher,
1944 key_count, vk, flags);
1949 crypt_safe_free(passphrase_read);
1950 crypt_free_volume_key(vk);
1955 int crypt_activate_by_keyfile(struct crypt_device *cd,
1958 const char *keyfile,
1959 size_t keyfile_size,
1962 return crypt_activate_by_keyfile_offset(cd, name, keyslot, keyfile,
1963 keyfile_size, 0, flags);
1966 int crypt_activate_by_volume_key(struct crypt_device *cd,
1968 const char *volume_key,
1969 size_t volume_key_size,
1972 crypt_status_info ci;
1973 struct volume_key *vk = NULL;
1976 log_dbg("Activating volume %s by volume key.", name ?: "[none]");
1979 ci = crypt_status(NULL, name);
1980 if (ci == CRYPT_INVALID)
1982 else if (ci >= CRYPT_ACTIVE) {
1983 log_err(cd, _("Device %s already exists.\n"), name);
1988 /* use key directly, no hash */
1989 if (isPLAIN(cd->type)) {
1993 if (!volume_key || !volume_key_size || volume_key_size != cd->plain_key_size) {
1994 log_err(cd, _("Incorrect volume key specified for plain device.\n"));
1998 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2002 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
2003 } else if (isLUKS(cd->type)) {
2004 /* If key is not provided, try to use internal key */
2006 if (!cd->volume_key) {
2007 log_err(cd, _("Volume key does not match the volume.\n"));
2010 volume_key_size = cd->volume_key->keylength;
2011 volume_key = cd->volume_key->key;
2014 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2017 r = LUKS_verify_volume_key(&cd->hdr, vk);
2020 log_err(cd, _("Volume key does not match the volume.\n"));
2023 r = LUKS1_activate(cd, name, vk, flags);
2024 } else if (isVERITY(cd->type)) {
2025 /* volume_key == root hash */
2026 if (!volume_key || !volume_key_size) {
2027 log_err(cd, _("Incorrect root hash specified for verity device.\n"));
2031 r = VERITY_activate(cd, name, volume_key, volume_key_size,
2032 &cd->verity_hdr, CRYPT_ACTIVATE_READONLY);
2035 free(cd->verity_root_hash);
2036 cd->verity_root_hash = NULL;
2038 cd->verity_root_hash_size = volume_key_size;
2039 if (!cd->verity_root_hash)
2040 cd->verity_root_hash = malloc(volume_key_size);
2041 if (cd->verity_root_hash)
2042 memcpy(cd->verity_root_hash, volume_key, volume_key_size);
2044 } else if (isTCRYPT(cd->type)) {
2047 r = TCRYPT_activate(cd, name, &cd->tcrypt_hdr,
2048 &cd->tcrypt_params, flags);
2050 log_err(cd, _("Device type is not properly initialised.\n"));
2052 crypt_free_volume_key(vk);
2057 int crypt_deactivate(struct crypt_device *cd, const char *name)
2064 log_dbg("Deactivating volume %s.", name);
2069 switch (crypt_status(cd, name)) {
2072 r = dm_remove_device(cd, name, 0, 0);
2074 case CRYPT_INACTIVE:
2075 log_err(cd, _("Device %s is not active.\n"), name);
2079 log_err(cd, _("Invalid device %s.\n"), name);
2089 int crypt_volume_key_get(struct crypt_device *cd,
2092 size_t *volume_key_size,
2093 const char *passphrase,
2094 size_t passphrase_size)
2096 struct volume_key *vk = NULL;
2100 if (crypt_fips_mode()) {
2101 log_err(cd, "Function not available in FIPS mode.\n");
2105 key_len = crypt_get_volume_key_size(cd);
2106 if (key_len > *volume_key_size) {
2107 log_err(cd, _("Volume key buffer too small.\n"));
2111 if (isPLAIN(cd->type) && cd->plain_hdr.hash) {
2112 r = process_key(cd, cd->plain_hdr.hash, key_len,
2113 passphrase, passphrase_size, &vk);
2115 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
2116 } else if (isLUKS(cd->type)) {
2117 r = LUKS_open_key_with_hdr(keyslot, passphrase,
2118 passphrase_size, &cd->hdr, &vk, cd);
2121 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
2124 memcpy(volume_key, vk->key, vk->keylength);
2125 *volume_key_size = vk->keylength;
2128 crypt_free_volume_key(vk);
2132 int crypt_volume_key_verify(struct crypt_device *cd,
2133 const char *volume_key,
2134 size_t volume_key_size)
2136 struct volume_key *vk;
2139 if (!isLUKS(cd->type)) {
2140 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2144 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2148 r = LUKS_verify_volume_key(&cd->hdr, vk);
2151 log_err(cd, _("Volume key does not match the volume.\n"));
2153 crypt_free_volume_key(vk);
2158 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
2160 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
2161 cd->timeout = timeout_sec;
2164 void crypt_set_password_retry(struct crypt_device *cd, int tries)
2166 log_dbg("Password retry count set to %d.", tries);
2170 void crypt_set_iteration_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2172 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
2173 cd->iteration_time = iteration_time_ms;
2175 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2177 crypt_set_iteration_time(cd, iteration_time_ms);
2180 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
2182 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
2183 cd->password_verify = password_verify ? 1 : 0;
2186 void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
2189 case CRYPT_RNG_URANDOM:
2190 case CRYPT_RNG_RANDOM:
2191 log_dbg("RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
2192 cd->rng_type = rng_type;
2196 int crypt_get_rng_type(struct crypt_device *cd)
2201 return cd->rng_type;
2204 int crypt_memory_lock(struct crypt_device *cd, int lock)
2206 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
2210 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
2217 r = dm_status_device(cd, name);
2222 if (r < 0 && r != -ENODEV)
2223 return CRYPT_INVALID;
2226 return CRYPT_ACTIVE;
2231 return CRYPT_INACTIVE;
2234 static void hexprint(struct crypt_device *cd, const char *d, int n, const char *sep)
2237 for(i = 0; i < n; i++)
2238 log_std(cd, "%02hhx%s", (const char)d[i], sep);
2241 static int _luks_dump(struct crypt_device *cd)
2245 log_std(cd, "LUKS header information for %s\n\n", mdata_device_path(cd));
2246 log_std(cd, "Version: \t%d\n", cd->hdr.version);
2247 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
2248 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
2249 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec);
2250 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
2251 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
2252 log_std(cd, "MK digest: \t");
2253 hexprint(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE, " ");
2255 log_std(cd, "MK salt: \t");
2256 hexprint(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2, " ");
2257 log_std(cd, "\n \t");
2258 hexprint(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ");
2260 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
2261 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
2262 for(i = 0; i < LUKS_NUMKEYS; i++) {
2263 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
2264 log_std(cd, "Key Slot %d: ENABLED\n",i);
2265 log_std(cd, "\tIterations: \t%d\n",
2266 cd->hdr.keyblock[i].passwordIterations);
2267 log_std(cd, "\tSalt: \t");
2268 hexprint(cd, cd->hdr.keyblock[i].passwordSalt,
2269 LUKS_SALTSIZE/2, " ");
2270 log_std(cd, "\n\t \t");
2271 hexprint(cd, cd->hdr.keyblock[i].passwordSalt +
2272 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ");
2275 log_std(cd, "\tKey material offset:\t%d\n",
2276 cd->hdr.keyblock[i].keyMaterialOffset);
2277 log_std(cd, "\tAF stripes: \t%d\n",
2278 cd->hdr.keyblock[i].stripes);
2281 log_std(cd, "Key Slot %d: DISABLED\n", i);
2286 static int _verity_dump(struct crypt_device *cd)
2288 log_std(cd, "VERITY header information for %s\n", mdata_device_path(cd));
2289 log_std(cd, "UUID: \t%s\n", cd->verity_uuid ?: "");
2290 log_std(cd, "Hash type: \t%u\n", cd->verity_hdr.hash_type);
2291 log_std(cd, "Data blocks: \t%" PRIu64 "\n", cd->verity_hdr.data_size);
2292 log_std(cd, "Data block size: \t%u\n", cd->verity_hdr.data_block_size);
2293 log_std(cd, "Hash block size: \t%u\n", cd->verity_hdr.hash_block_size);
2294 log_std(cd, "Hash algorithm: \t%s\n", cd->verity_hdr.hash_name);
2295 log_std(cd, "Salt: \t");
2296 if (cd->verity_hdr.salt_size)
2297 hexprint(cd, cd->verity_hdr.salt, cd->verity_hdr.salt_size, "");
2301 if (cd->verity_root_hash) {
2302 log_std(cd, "Root hash: \t");
2303 hexprint(cd, cd->verity_root_hash, cd->verity_root_hash_size, "");
2309 int crypt_dump(struct crypt_device *cd)
2311 if (isLUKS(cd->type))
2312 return _luks_dump(cd);
2313 else if (isVERITY(cd->type))
2314 return _verity_dump(cd);
2316 log_err(cd, _("Dump operation is not supported for this device type.\n"));
2320 const char *crypt_get_cipher(struct crypt_device *cd)
2322 if (isPLAIN(cd->type))
2323 return cd->plain_cipher;
2325 if (isLUKS(cd->type))
2326 return cd->hdr.cipherName;
2328 if (isLOOPAES(cd->type))
2329 return cd->loopaes_cipher;
2334 const char *crypt_get_cipher_mode(struct crypt_device *cd)
2336 if (isPLAIN(cd->type))
2337 return cd->plain_cipher_mode;
2339 if (isLUKS(cd->type))
2340 return cd->hdr.cipherMode;
2342 if (isLOOPAES(cd->type))
2343 return cd->loopaes_cipher_mode;
2348 const char *crypt_get_uuid(struct crypt_device *cd)
2350 if (isLUKS(cd->type))
2351 return cd->hdr.uuid;
2353 if (isPLAIN(cd->type))
2354 return cd->plain_uuid;
2356 if (isLOOPAES(cd->type))
2357 return cd->loopaes_uuid;
2359 if (isVERITY(cd->type))
2360 return cd->verity_uuid;
2365 const char *crypt_get_device_name(struct crypt_device *cd)
2367 const char *path = device_block_path(cd->device);
2370 path = device_path(cd->device);
2375 int crypt_get_volume_key_size(struct crypt_device *cd)
2377 if (isPLAIN(cd->type))
2378 return cd->plain_key_size;
2380 if (isLUKS(cd->type))
2381 return cd->hdr.keyBytes;
2383 if (isLOOPAES(cd->type))
2384 return cd->loopaes_key_size;
2386 if (isVERITY(cd->type))
2387 return cd->verity_root_hash_size;
2392 uint64_t crypt_get_data_offset(struct crypt_device *cd)
2394 if (isPLAIN(cd->type))
2395 return cd->plain_hdr.offset;
2397 if (isLUKS(cd->type))
2398 return cd->hdr.payloadOffset;
2400 if (isLOOPAES(cd->type))
2401 return cd->loopaes_hdr.offset;
2403 if (isTCRYPT(cd->type)) { // FIXME: system vol.
2404 if (!cd->tcrypt_hdr.d.mk_offset)
2406 return (cd->tcrypt_hdr.d.mk_offset / cd->tcrypt_hdr.d.sector_size);
2412 uint64_t crypt_get_iv_offset(struct crypt_device *cd)
2414 if (isPLAIN(cd->type))
2415 return cd->plain_hdr.skip;
2417 if (isLUKS(cd->type))
2420 if (isLOOPAES(cd->type))
2421 return cd->loopaes_hdr.skip;
2423 if (isTCRYPT(cd->type)) {
2424 if (!cd->tcrypt_hdr.d.mk_offset)
2426 return (cd->tcrypt_hdr.d.mk_offset / cd->tcrypt_hdr.d.sector_size);
2432 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
2434 if (!isLUKS(cd->type)) {
2435 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2436 return CRYPT_SLOT_INVALID;
2439 return LUKS_keyslot_info(&cd->hdr, keyslot);
2442 int crypt_keyslot_max(const char *type)
2444 if (type && isLUKS(type))
2445 return LUKS_NUMKEYS;
2450 int crypt_keyslot_area(struct crypt_device *cd,
2455 if (!isLUKS(cd->type))
2458 return LUKS_keyslot_area(&cd->hdr, keyslot, offset, length);
2461 const char *crypt_get_type(struct crypt_device *cd)
2466 int crypt_get_verity_info(struct crypt_device *cd,
2467 struct crypt_params_verity *vp)
2469 if (!isVERITY(cd->type) || !vp)
2472 vp->data_device = device_path(cd->device);
2473 vp->hash_device = mdata_device_path(cd);
2474 vp->hash_name = cd->verity_hdr.hash_name;
2475 vp->salt = cd->verity_hdr.salt;
2476 vp->salt_size = cd->verity_hdr.salt_size;
2477 vp->data_block_size = cd->verity_hdr.data_block_size;
2478 vp->hash_block_size = cd->verity_hdr.hash_block_size;
2479 vp->data_size = cd->verity_hdr.data_size;
2480 vp->hash_area_offset = cd->verity_hdr.hash_area_offset;
2481 vp->hash_type = cd->verity_hdr.hash_type;
2482 vp->flags = cd->verity_hdr.flags & CRYPT_VERITY_NO_HEADER;
2486 int crypt_get_active_device(struct crypt_device *cd __attribute__((unused)),
2488 struct crypt_active_device *cad)
2490 struct crypt_dm_active_device dmd;
2493 r = dm_query_device(cd, name, 0, &dmd);
2497 if (dmd.target != DM_CRYPT && dmd.target != DM_VERITY)
2500 cad->offset = dmd.u.crypt.offset;
2501 cad->iv_offset = dmd.u.crypt.iv_offset;
2502 cad->size = dmd.size;
2503 cad->flags = dmd.flags;