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-2011, 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "libcryptsetup.h"
33 #include "crypto_backend.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;
60 /* used in CRYPT_LOOPAES */
61 struct crypt_params_loopaes loopaes_hdr;
63 char *loopaes_cipher_mode;
65 unsigned int loopaes_key_size;
67 /* callbacks definitions */
68 void (*log)(int level, const char *msg, void *usrptr);
70 int (*confirm)(const char *msg, void *usrptr);
72 int (*password)(const char *msg, char *buf, size_t length, void *usrptr);
73 void *password_usrptr;
75 /* last error message */
76 char error[MAX_ERROR_LENGTH];
80 /* FIXME: not thread safe, remove this later */
81 static char global_error[MAX_ERROR_LENGTH] = {0};
84 static void (*_default_log)(int level, const char *msg, void *usrptr) = NULL;
85 static int _debug_level = 0;
87 void crypt_set_debug_level(int level)
92 int crypt_get_debug_level(void)
97 static void crypt_set_error(struct crypt_device *cd, const char *error)
99 size_t size = strlen(error);
101 /* Set global error, ugly hack... */
102 strncpy(global_error, error, MAX_ERROR_LENGTH - 2);
103 if (size < MAX_ERROR_LENGTH && global_error[size - 1] == '\n')
104 global_error[size - 1] = '\0';
106 /* Set error string per context */
108 strncpy(cd->error, error, MAX_ERROR_LENGTH - 2);
109 if (size < MAX_ERROR_LENGTH && cd->error[size - 1] == '\n')
110 cd->error[size - 1] = '\0';
114 void crypt_log(struct crypt_device *cd, int level, const char *msg)
117 cd->log(level, msg, cd->log_usrptr);
118 else if (_default_log)
119 _default_log(level, msg, NULL);
121 if (level == CRYPT_LOG_ERROR)
122 crypt_set_error(cd, msg);
125 __attribute__((format(printf, 5, 6)))
126 void logger(struct crypt_device *cd, int level, const char *file,
127 int line, const char *format, ...)
132 va_start(argp, format);
134 if (vasprintf(&target, format, argp) > 0 ) {
136 crypt_log(cd, level, target);
138 } else if (_debug_level)
139 printf("# %s:%d %s\n", file ?: "?", line, target);
141 } else if (_debug_level)
142 printf("# %s\n", target);
150 static const char *mdata_device(struct crypt_device *cd)
152 return cd->metadata_device ?: cd->device;
155 static int init_crypto(struct crypt_device *ctx)
159 r = crypt_random_init(ctx);
161 log_err(ctx, _("Cannot initialize crypto RNG backend.\n"));
165 r = crypt_backend_init(ctx);
167 log_err(ctx, _("Cannot initialize crypto backend.\n"));
172 static int process_key(struct crypt_device *cd, const char *hash_name,
173 size_t key_size, const char *pass, size_t passLen,
174 struct volume_key **vk)
181 *vk = crypt_alloc_volume_key(key_size, NULL);
186 r = crypt_plain_hash(cd, hash_name, (*vk)->key, key_size, pass, passLen);
189 log_err(cd, _("Hash algorithm %s not supported.\n"),
192 log_err(cd, _("Key processing error (using hash %s).\n"),
194 crypt_free_volume_key(*vk);
198 } else if (passLen > key_size) {
199 memcpy((*vk)->key, pass, key_size);
201 memcpy((*vk)->key, pass, passLen);
207 static int isPLAIN(const char *type)
209 return (type && !strcmp(CRYPT_PLAIN, type));
212 static int isLUKS(const char *type)
214 return (type && !strcmp(CRYPT_LUKS1, type));
217 static int isLOOPAES(const char *type)
219 return (type && !strcmp(CRYPT_LOOPAES, type));
222 /* keyslot helpers */
223 static int keyslot_verify_or_find_empty(struct crypt_device *cd, int *keyslot)
225 if (*keyslot == CRYPT_ANY_SLOT) {
226 *keyslot = LUKS_keyslot_find_empty(&cd->hdr);
228 log_err(cd, _("All key slots full.\n"));
233 switch (LUKS_keyslot_info(&cd->hdr, *keyslot)) {
234 case CRYPT_SLOT_INVALID:
235 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
236 *keyslot, LUKS_NUMKEYS - 1);
238 case CRYPT_SLOT_INACTIVE:
241 log_err(cd, _("Key slot %d is full, please select another one.\n"),
250 * compares UUIDs returned by device-mapper (striped by cryptsetup) and uuid in header
252 static int crypt_uuid_cmp(const char *dm_uuid, const char *hdr_uuid)
257 if (!dm_uuid || !hdr_uuid)
260 str = strchr(dm_uuid, '-');
264 for (i = 0, j = 1; hdr_uuid[i]; i++) {
265 if (hdr_uuid[i] == '-')
268 if (!str[j] || str[j] == '-')
271 if (str[j] != hdr_uuid[i])
279 int PLAIN_activate(struct crypt_device *cd,
281 struct volume_key *vk,
286 char *dm_cipher = NULL;
287 struct crypt_dm_active_device dmd = {
288 .device = crypt_get_device_name(cd),
290 .uuid = crypt_get_uuid(cd),
292 .offset = crypt_get_data_offset(cd),
293 .iv_offset = crypt_get_iv_offset(cd),
298 r = device_check_and_adjust(cd, dmd.device,
299 (dmd.flags & CRYPT_ACTIVATE_SHARED) ? DEV_SHARED : DEV_EXCL,
300 &dmd.size, &dmd.offset, &flags);
304 if (crypt_get_cipher_mode(cd))
305 r = asprintf(&dm_cipher, "%s-%s", crypt_get_cipher(cd), crypt_get_cipher_mode(cd));
307 r = asprintf(&dm_cipher, "%s", crypt_get_cipher(cd));
311 dmd.cipher = dm_cipher;
312 log_dbg("Trying to activate PLAIN device %s using cipher %s.", name, dmd.cipher);
314 r = dm_create_device(name, CRYPT_PLAIN, &dmd, 0);
317 if (!cd->plain_uuid && dm_query_device(name, DM_ACTIVE_UUID, &dmd) >= 0)
318 cd->plain_uuid = CONST_CAST(char*)dmd.uuid;
324 int crypt_confirm(struct crypt_device *cd, const char *msg)
326 if (!cd || !cd->confirm)
329 return cd->confirm(msg, cd->confirm_usrptr);
332 static int key_from_terminal(struct crypt_device *cd, char *msg, char **key,
333 size_t *key_len, int force_verify)
339 if(!msg && asprintf(&prompt, _("Enter passphrase for %s: "),
340 cd->backing_file ?: crypt_get_device_name(cd)) < 0)
347 *key = crypt_safe_alloc(DEFAULT_PASSPHRASE_SIZE_MAX);
352 r = cd->password(msg, *key, DEFAULT_PASSPHRASE_SIZE_MAX,
353 cd->password_usrptr);
355 crypt_safe_free(*key);
360 r = crypt_get_key(msg, key, key_len, 0, NULL, cd->timeout,
361 (force_verify || cd->password_verify), cd);
364 return (r < 0) ? r: 0;
367 static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslot,
368 struct volume_key **vk)
370 char *passphrase_read = NULL;
371 size_t passphrase_size_read;
372 int r = -EINVAL, eperm = 0, tries = cd->tries;
376 crypt_free_volume_key(*vk);
379 r = key_from_terminal(cd, NULL, &passphrase_read,
380 &passphrase_size_read, 0);
384 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase_read,
385 passphrase_size_read, &cd->hdr, vk, cd);
388 crypt_safe_free(passphrase_read);
389 passphrase_read = NULL;
390 } while (r == -EPERM && (--tries > 0));
393 crypt_free_volume_key(*vk);
396 /* Report wrong passphrase if at least one try failed */
397 if (eperm && r == -EPIPE)
401 crypt_safe_free(passphrase_read);
405 static int key_from_file(struct crypt_device *cd, char *msg,
406 char **key, size_t *key_len,
407 const char *key_file, size_t key_size)
409 return crypt_get_key(msg, key, key_len, key_size, key_file,
413 void crypt_set_log_callback(struct crypt_device *cd,
414 void (*log)(int level, const char *msg, void *usrptr),
421 cd->log_usrptr = usrptr;
425 void crypt_set_confirm_callback(struct crypt_device *cd,
426 int (*confirm)(const char *msg, void *usrptr),
429 cd->confirm = confirm;
430 cd->confirm_usrptr = usrptr;
433 void crypt_set_password_callback(struct crypt_device *cd,
434 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
437 cd->password = password;
438 cd->password_usrptr = usrptr;
441 static void _get_error(char *error, char *buf, size_t size)
443 if (!buf || size < 1)
446 strncpy(buf, error, size - 1);
447 buf[size - 1] = '\0';
453 void crypt_last_error(struct crypt_device *cd, char *buf, size_t size)
456 return _get_error(cd->error, buf, size);
459 /* Deprecated global error interface */
460 void crypt_get_error(char *buf, size_t size)
462 return _get_error(global_error, buf, size);
465 const char *crypt_get_dir(void)
470 int crypt_init(struct crypt_device **cd, const char *device)
472 struct crypt_device *h = NULL;
478 log_dbg("Allocating crypt device %s context.", device);
480 if (!(h = malloc(sizeof(struct crypt_device))))
483 memset(h, 0, sizeof(*h));
487 r = device_ready(NULL, device, O_RDONLY);
489 h->device = crypt_loop_get_device();
490 log_dbg("Not a block device, %s%s.",
491 h->device ? "using free loop device " :
492 "no free loop device found",
495 log_err(NULL, _("Cannot find a free loopback device.\n"));
500 /* Keep the loop open, dettached on last close. */
501 h->loop_fd = crypt_loop_attach(h->device, device, 0, 1, &readonly);
502 if (h->loop_fd == -1) {
503 log_err(NULL, _("Attaching loopback device failed "
504 "(loop device with autoclear flag is required).\n"));
509 h->backing_file = crypt_loop_backing_file(h->device);
510 r = device_ready(NULL, h->device, O_RDONLY);
518 if (!h->device && device && !(h->device = strdup(device))) {
523 if (dm_init(h, 1) < 0) {
528 h->iteration_time = 1000;
529 h->password_verify = 0;
531 h->rng_type = crypt_random_default_key_rng();
537 if (h->loop_fd != -1)
540 free(h->backing_file);
546 static int crypt_check_data_device_size(struct crypt_device *cd)
549 uint64_t size, size_min;
551 /* Check data device size, require at least one sector */
552 size_min = crypt_get_data_offset(cd) << SECTOR_SHIFT ?: SECTOR_SIZE;
554 r = device_size(crypt_get_device_name(cd), &size);
558 if (size < size_min) {
559 log_err(cd, _("LUKS header detected but device %s is too small.\n"),
560 crypt_get_device_name(cd));
567 int crypt_set_data_device(struct crypt_device *cd, const char *device)
572 log_dbg("Setting ciphertext data device to %s.", device ?: "(none)");
574 if (!isLUKS(cd->type)) {
575 log_err(cd, _("This operation is not supported for this device type.\n"));
579 /* metadata device must be set */
583 r = device_ready(NULL, device, O_RDONLY);
587 if (!(data_device = strdup(device)))
590 if (!cd->metadata_device)
591 cd->metadata_device = cd->device;
595 cd->device = data_device;
597 return crypt_check_data_device_size(cd);
600 int crypt_init_by_name_and_header(struct crypt_device **cd,
602 const char *header_device)
604 crypt_status_info ci;
605 struct crypt_dm_active_device dmd;
606 char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
610 log_dbg("Allocating crypt device context by device %s.", name);
612 ci = crypt_status(NULL, name);
613 if (ci == CRYPT_INVALID)
616 if (ci < CRYPT_ACTIVE) {
617 log_err(NULL, _("Device %s is not active.\n"), name);
621 r = dm_query_device(name, DM_ACTIVE_DEVICE | DM_ACTIVE_CIPHER |
622 DM_ACTIVE_UUID | DM_ACTIVE_KEYSIZE, &dmd);
629 r = crypt_init(cd, header_device);
631 r = crypt_init(cd, dmd.device);
633 /* Underlying device disappeared but mapping still active */
634 if (!dmd.device || r == -ENOTBLK)
635 log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
638 /* Underlying device is not readable but crypt mapping exists */
640 free(CONST_CAST(void*)dmd.device);
642 r = crypt_init(cd, NULL);
650 if (!strncmp(CRYPT_PLAIN, dmd.uuid, sizeof(CRYPT_PLAIN)-1))
651 (*cd)->type = strdup(CRYPT_PLAIN);
652 else if (!strncmp(CRYPT_LOOPAES, dmd.uuid, sizeof(CRYPT_LOOPAES)-1))
653 (*cd)->type = strdup(CRYPT_LOOPAES);
654 else if (!strncmp(CRYPT_LUKS1, dmd.uuid, sizeof(CRYPT_LUKS1)-1))
655 (*cd)->type = strdup(CRYPT_LUKS1);
657 log_dbg("Unknown UUID set, some parameters are not set.");
659 log_dbg("Active device has no UUID set, some parameters are not set.");
662 r = crypt_set_data_device(*cd, dmd.device);
667 /* Try to initialise basic parameters from active device */
669 if (!(*cd)->backing_file && dmd.device && crypt_loop_device(dmd.device) &&
670 !((*cd)->backing_file = crypt_loop_backing_file(dmd.device))) {
675 if (isPLAIN((*cd)->type)) {
676 (*cd)->plain_uuid = strdup(dmd.uuid);
677 (*cd)->plain_hdr.hash = NULL; /* no way to get this */
678 (*cd)->plain_hdr.offset = dmd.offset;
679 (*cd)->plain_hdr.skip = dmd.iv_offset;
681 r = crypt_parse_name_and_mode(dmd.cipher, cipher, NULL, cipher_mode);
683 (*cd)->plain_cipher = strdup(cipher);
684 (*cd)->plain_cipher_mode = strdup(cipher_mode);
686 } else if (isLOOPAES((*cd)->type)) {
687 (*cd)->loopaes_uuid = strdup(dmd.uuid);
688 (*cd)->loopaes_hdr.offset = dmd.offset;
690 r = crypt_parse_name_and_mode(dmd.cipher, cipher,
691 &key_nums, cipher_mode);
693 (*cd)->loopaes_cipher = strdup(cipher);
694 (*cd)->loopaes_cipher_mode = strdup(cipher_mode);
695 /* version 3 uses last key for IV */
696 if (dmd.vk->keylength % key_nums)
698 (*cd)->loopaes_key_size = dmd.vk->keylength / key_nums;
700 } else if (isLUKS((*cd)->type)) {
701 if (mdata_device(*cd)) {
702 r = crypt_load(*cd, CRYPT_LUKS1, NULL);
704 log_dbg("LUKS device header does not match active device.");
710 /* checks whether UUIDs match each other */
711 r = crypt_uuid_cmp(dmd.uuid, (*cd)->hdr.uuid);
713 log_dbg("LUKS device header uuid: %s mismatches DM returned uuid %s",
714 (*cd)->hdr.uuid, dmd.uuid);
728 crypt_free_volume_key(dmd.vk);
729 free(CONST_CAST(void*)dmd.device);
730 free(CONST_CAST(void*)dmd.cipher);
731 free(CONST_CAST(void*)dmd.uuid);
735 int crypt_init_by_name(struct crypt_device **cd, const char *name)
737 return crypt_init_by_name_and_header(cd, name, NULL);
740 static int _crypt_format_plain(struct crypt_device *cd,
742 const char *cipher_mode,
744 size_t volume_key_size,
745 struct crypt_params_plain *params)
747 if (!cipher || !cipher_mode) {
748 log_err(cd, _("Invalid plain crypt parameters.\n"));
752 if (volume_key_size > 1024) {
753 log_err(cd, _("Invalid key size.\n"));
757 cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
761 cd->plain_cipher = strdup(cipher);
762 cd->plain_cipher_mode = strdup(cipher_mode);
765 cd->plain_uuid = strdup(uuid);
767 if (params && params->hash)
768 cd->plain_hdr.hash = strdup(params->hash);
770 cd->plain_hdr.offset = params ? params->offset : 0;
771 cd->plain_hdr.skip = params ? params->skip : 0;
772 cd->plain_hdr.size = params ? params->size : 0;
774 if (!cd->plain_cipher || !cd->plain_cipher_mode)
780 static int _crypt_format_luks1(struct crypt_device *cd,
782 const char *cipher_mode,
784 const char *volume_key,
785 size_t volume_key_size,
786 struct crypt_params_luks1 *params)
789 unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
790 unsigned long alignment_offset = 0;
792 if (!mdata_device(cd)) {
793 log_err(cd, _("Can't format LUKS without device.\n"));
798 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
801 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
806 if (params && params->data_device) {
807 cd->metadata_device = cd->device;
808 if (!(cd->device = strdup(params->data_device)))
810 required_alignment = params->data_alignment * SECTOR_SIZE;
811 } else if (params && params->data_alignment) {
812 required_alignment = params->data_alignment * SECTOR_SIZE;
814 get_topology_alignment(cd->device, &required_alignment,
815 &alignment_offset, DEFAULT_DISK_ALIGNMENT);
817 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
818 (params && params->hash) ? params->hash : "sha1",
820 required_alignment / SECTOR_SIZE,
821 alignment_offset / SECTOR_SIZE,
822 cd->iteration_time, &cd->PBKDF2_per_sec,
823 cd->metadata_device, cd);
827 /* Wipe first 8 sectors - fs magic numbers etc. */
828 r = crypt_wipe(mdata_device(cd), 0, 8 * SECTOR_SIZE, CRYPT_WIPE_ZERO, 1);
831 log_err(cd, _("Cannot format device %s which is still in use.\n"),
834 log_err(cd, _("Cannot wipe header on device %s.\n"),
840 r = LUKS_write_phdr(mdata_device(cd), &cd->hdr, cd);
845 static int _crypt_format_loopaes(struct crypt_device *cd,
848 size_t volume_key_size,
849 struct crypt_params_loopaes *params)
851 if (!mdata_device(cd)) {
852 log_err(cd, _("Can't format LOOPAES without device.\n"));
856 if (volume_key_size > 1024) {
857 log_err(cd, _("Invalid key size.\n"));
861 cd->loopaes_key_size = volume_key_size;
863 cd->loopaes_cipher = strdup(cipher ?: DEFAULT_LOOPAES_CIPHER);
866 cd->loopaes_uuid = strdup(uuid);
868 if (params && params->hash)
869 cd->loopaes_hdr.hash = strdup(params->hash);
871 cd->loopaes_hdr.offset = params ? params->offset : 0;
872 cd->loopaes_hdr.skip = params ? params->skip : 0;
877 int crypt_format(struct crypt_device *cd,
880 const char *cipher_mode,
882 const char *volume_key,
883 size_t volume_key_size,
892 log_dbg("Context already formatted as %s.", cd->type);
896 log_dbg("Formatting device %s as type %s.", mdata_device(cd) ?: "(none)", type);
903 r = _crypt_format_plain(cd, cipher, cipher_mode,
904 uuid, volume_key_size, params);
905 else if (isLUKS(type))
906 r = _crypt_format_luks1(cd, cipher, cipher_mode,
907 uuid, volume_key, volume_key_size, params);
908 else if (isLOOPAES(type))
909 r = _crypt_format_loopaes(cd, cipher, uuid, volume_key_size, params);
911 /* FIXME: allow plugins here? */
912 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
916 if (!r && !(cd->type = strdup(type)))
920 crypt_free_volume_key(cd->volume_key);
921 cd->volume_key = NULL;
927 int crypt_load(struct crypt_device *cd,
928 const char *requested_type,
929 void *params __attribute__((unused)))
931 struct luks_phdr hdr;
934 log_dbg("Trying to load %s crypt type from device %s.",
935 requested_type ?: "any", mdata_device(cd) ?: "(none)");
937 if (!mdata_device(cd))
940 if (requested_type && !isLUKS(requested_type))
943 if (cd->type && !isLUKS(cd->type)) {
944 log_dbg("Context is already initialised to type %s", cd->type);
952 r = LUKS_read_phdr(mdata_device(cd), &hdr, 1, cd);
956 if (!cd->type && !(cd->type = strdup(CRYPT_LUKS1)))
959 memcpy(&cd->hdr, &hdr, sizeof(hdr));
961 /* cd->type and header must be set in context */
962 r = crypt_check_data_device_size(cd);
971 int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
973 struct crypt_dm_active_device dmd;
976 /* Device context type must be initialised */
977 if (!cd->type || !crypt_get_uuid(cd))
980 log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
982 r = dm_query_device(name, DM_ACTIVE_DEVICE | DM_ACTIVE_CIPHER |
983 DM_ACTIVE_UUID | DM_ACTIVE_KEYSIZE |
984 DM_ACTIVE_KEY, &dmd);
986 log_err(NULL, _("Device %s is not active.\n"), name);
995 r = device_check_and_adjust(cd, dmd.device, DEV_OK, &new_size, &dmd.offset, &dmd.flags);
999 if (new_size == dmd.size) {
1000 log_dbg("Device has already requested size %" PRIu64
1001 " sectors.", dmd.size);
1004 dmd.size = new_size;
1005 r = dm_create_device(name, cd->type, &dmd, 1);
1008 crypt_free_volume_key(dmd.vk);
1009 free(CONST_CAST(void*)dmd.cipher);
1010 free(CONST_CAST(void*)dmd.device);
1011 free(CONST_CAST(void*)dmd.uuid);
1016 int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
1018 if (!isLUKS(cd->type)) {
1019 log_err(cd, _("This operation is not supported for this device type.\n"));
1023 if (uuid && !strncmp(uuid, cd->hdr.uuid, sizeof(cd->hdr.uuid))) {
1024 log_dbg("UUID is the same as requested (%s) for device %s.",
1025 uuid, mdata_device(cd));
1030 log_dbg("Requested new UUID change to %s for %s.", uuid, mdata_device(cd));
1032 log_dbg("Requested new UUID refresh for %s.", mdata_device(cd));
1034 if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
1037 return LUKS_hdr_uuid_set(mdata_device(cd), &cd->hdr, uuid, cd);
1040 int crypt_header_backup(struct crypt_device *cd,
1041 const char *requested_type,
1042 const char *backup_file)
1046 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1049 r = init_crypto(cd);
1053 log_dbg("Requested header backup of device %s (%s) to "
1054 "file %s.", mdata_device(cd), requested_type, backup_file);
1056 return LUKS_hdr_backup(backup_file, mdata_device(cd), &cd->hdr, cd);
1059 int crypt_header_restore(struct crypt_device *cd,
1060 const char *requested_type,
1061 const char *backup_file)
1065 if (requested_type && !isLUKS(requested_type))
1068 /* Some hash functions need initialized gcrypt library */
1069 r = init_crypto(cd);
1073 log_dbg("Requested header restore to device %s (%s) from "
1074 "file %s.", mdata_device(cd), requested_type, backup_file);
1076 return LUKS_hdr_restore(backup_file, mdata_device(cd), &cd->hdr, cd);
1079 void crypt_free(struct crypt_device *cd)
1082 log_dbg("Releasing crypt device %s context.", mdata_device(cd));
1084 if (cd->loop_fd != -1)
1088 crypt_free_volume_key(cd->volume_key);
1091 free(cd->metadata_device);
1092 free(cd->backing_file);
1095 /* used in plain device only */
1096 free(CONST_CAST(void*)cd->plain_hdr.hash);
1097 free(cd->plain_cipher);
1098 free(cd->plain_cipher_mode);
1099 free(cd->plain_uuid);
1101 /* used in loop-AES device only */
1102 free(CONST_CAST(void*)cd->loopaes_hdr.hash);
1103 free(cd->loopaes_cipher);
1104 free(cd->loopaes_uuid);
1110 int crypt_suspend(struct crypt_device *cd,
1113 crypt_status_info ci;
1116 log_dbg("Suspending volume %s.", name);
1118 if (!isLUKS(cd->type)) {
1119 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1124 ci = crypt_status(NULL, name);
1125 if (ci < CRYPT_ACTIVE) {
1126 log_err(cd, _("Volume %s is not active.\n"), name);
1130 if (!cd && dm_init(NULL, 1) < 0)
1133 r = dm_status_suspended(name);
1138 log_err(cd, _("Volume %s is already suspended.\n"), name);
1143 r = dm_suspend_and_wipe_key(name);
1145 log_err(cd, "Suspend is not supported for device %s.\n", name);
1147 log_err(cd, "Error during suspending device %s.\n", name);
1154 int crypt_resume_by_passphrase(struct crypt_device *cd,
1157 const char *passphrase,
1158 size_t passphrase_size)
1160 struct volume_key *vk = NULL;
1163 log_dbg("Resuming volume %s.", name);
1165 if (!isLUKS(cd->type)) {
1166 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1171 r = dm_status_suspended(name);
1176 log_err(cd, _("Volume %s is not suspended.\n"), name);
1181 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase,
1182 passphrase_size, &cd->hdr, &vk, cd);
1184 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1188 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1190 log_err(cd, "Resume is not supported for device %s.\n", name);
1192 log_err(cd, "Error during resuming device %s.\n", name);
1196 crypt_free_volume_key(vk);
1197 return r < 0 ? r : keyslot;
1200 int crypt_resume_by_keyfile(struct crypt_device *cd,
1203 const char *keyfile,
1204 size_t keyfile_size)
1206 struct volume_key *vk = NULL;
1207 char *passphrase_read = NULL;
1208 size_t passphrase_size_read;
1211 log_dbg("Resuming volume %s.", name);
1213 if (!isLUKS(cd->type)) {
1214 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1219 r = dm_status_suspended(name);
1224 log_err(cd, _("Volume %s is not suspended.\n"), name);
1231 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1232 &passphrase_size_read, keyfile, keyfile_size);
1236 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase_read,
1237 passphrase_size_read, &cd->hdr, &vk, cd);
1242 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1244 log_err(cd, "Error during resuming device %s.\n", name);
1246 crypt_safe_free(passphrase_read);
1247 crypt_free_volume_key(vk);
1248 return r < 0 ? r : keyslot;
1251 // slot manipulation
1252 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1253 int keyslot, // -1 any
1254 const char *passphrase, // NULL -> terminal
1255 size_t passphrase_size,
1256 const char *new_passphrase, // NULL -> terminal
1257 size_t new_passphrase_size)
1259 struct volume_key *vk = NULL;
1260 char *password = NULL, *new_password = NULL;
1261 size_t passwordLen, new_passwordLen;
1264 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1265 "new passphrase %sprovided.",
1266 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1268 if (!isLUKS(cd->type)) {
1269 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1273 r = keyslot_verify_or_find_empty(cd, &keyslot);
1277 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1278 /* No slots used, try to use pre-generated key in header */
1279 if (cd->volume_key) {
1280 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1281 r = vk ? 0 : -ENOMEM;
1283 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1286 } else if (passphrase) {
1287 /* Passphrase provided, use it to unlock existing keyslot */
1288 r = LUKS_open_key_with_hdr(mdata_device(cd), CRYPT_ANY_SLOT, passphrase,
1289 passphrase_size, &cd->hdr, &vk, cd);
1291 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1292 r = key_from_terminal(cd, _("Enter any passphrase: "),
1293 &password, &passwordLen, 0);
1297 r = LUKS_open_key_with_hdr(mdata_device(cd), CRYPT_ANY_SLOT, password,
1298 passwordLen, &cd->hdr, &vk, cd);
1299 crypt_safe_free(password);
1305 if (new_passphrase) {
1306 new_password = CONST_CAST(char*)new_passphrase;
1307 new_passwordLen = new_passphrase_size;
1309 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1310 &new_password, &new_passwordLen, 1);
1315 r = LUKS_set_key(mdata_device(cd), keyslot, new_password, new_passwordLen,
1316 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1321 if (!new_passphrase)
1322 crypt_safe_free(new_password);
1323 crypt_free_volume_key(vk);
1324 return r ?: keyslot;
1327 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1329 const char *keyfile,
1330 size_t keyfile_size,
1331 const char *new_keyfile,
1332 size_t new_keyfile_size)
1334 struct volume_key *vk = NULL;
1335 char *password = NULL; size_t passwordLen;
1336 char *new_password = NULL; size_t new_passwordLen;
1339 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1340 keyfile ?: "[none]", new_keyfile ?: "[none]");
1342 if (!isLUKS(cd->type)) {
1343 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1347 r = keyslot_verify_or_find_empty(cd, &keyslot);
1351 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1352 /* No slots used, try to use pre-generated key in header */
1353 if (cd->volume_key) {
1354 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1355 r = vk ? 0 : -ENOMEM;
1357 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1361 /* Read password from file of (if NULL) from terminal */
1363 r = key_from_file(cd, _("Enter any passphrase: "),
1364 &password, &passwordLen,
1365 keyfile, keyfile_size);
1367 r = key_from_terminal(cd, _("Enter any passphrase: "),
1368 &password, &passwordLen, 0);
1372 r = LUKS_open_key_with_hdr(mdata_device(cd), CRYPT_ANY_SLOT, password, passwordLen,
1380 r = key_from_file(cd, _("Enter new passphrase for key slot: "),
1381 &new_password, &new_passwordLen, new_keyfile,
1384 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1385 &new_password, &new_passwordLen, 1);
1389 r = LUKS_set_key(mdata_device(cd), keyslot, new_password, new_passwordLen,
1390 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1392 crypt_safe_free(password);
1393 crypt_safe_free(new_password);
1394 crypt_free_volume_key(vk);
1395 return r < 0 ? r : keyslot;
1398 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1400 const char *volume_key,
1401 size_t volume_key_size,
1402 const char *passphrase,
1403 size_t passphrase_size)
1405 struct volume_key *vk = NULL;
1407 char *new_password = NULL; size_t new_passwordLen;
1409 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1411 if (!isLUKS(cd->type)) {
1412 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1417 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1418 else if (cd->volume_key)
1419 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1424 r = LUKS_verify_volume_key(&cd->hdr, vk);
1426 log_err(cd, _("Volume key does not match the volume.\n"));
1430 r = keyslot_verify_or_find_empty(cd, &keyslot);
1435 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1436 &new_password, &new_passwordLen, 1);
1439 passphrase = new_password;
1440 passphrase_size = new_passwordLen;
1443 r = LUKS_set_key(mdata_device(cd), keyslot, passphrase, passphrase_size,
1444 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1446 crypt_safe_free(new_password);
1447 crypt_free_volume_key(vk);
1448 return (r < 0) ? r : keyslot;
1451 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1453 crypt_keyslot_info ki;
1455 log_dbg("Destroying keyslot %d.", keyslot);
1457 if (!isLUKS(cd->type)) {
1458 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1462 ki = crypt_keyslot_status(cd, keyslot);
1463 if (ki == CRYPT_SLOT_INVALID) {
1464 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1468 if (ki == CRYPT_SLOT_INACTIVE) {
1469 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1473 return LUKS_del_key(mdata_device(cd), keyslot, &cd->hdr, cd);
1476 // activation/deactivation of device mapping
1477 int crypt_activate_by_passphrase(struct crypt_device *cd,
1480 const char *passphrase,
1481 size_t passphrase_size,
1484 crypt_status_info ci;
1485 struct volume_key *vk = NULL;
1486 char *read_passphrase = NULL;
1487 size_t passphraseLen = 0;
1490 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1491 name ? "Activating" : "Checking", name ?: "",
1492 keyslot, passphrase ? "" : "[none] ");
1495 ci = crypt_status(NULL, name);
1496 if (ci == CRYPT_INVALID)
1498 else if (ci >= CRYPT_ACTIVE) {
1499 log_err(cd, _("Device %s already exists.\n"), name);
1504 /* plain, use hashed passphrase */
1505 if (isPLAIN(cd->type)) {
1510 r = key_from_terminal(cd, NULL, &read_passphrase,
1514 passphrase = read_passphrase;
1515 passphrase_size = passphraseLen;
1518 r = process_key(cd, cd->plain_hdr.hash,
1519 cd->volume_key->keylength,
1520 passphrase, passphrase_size, &vk);
1524 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1526 } else if (isLUKS(cd->type)) {
1527 /* provided passphrase, do not retry */
1529 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase,
1530 passphrase_size, &cd->hdr, &vk, cd);
1532 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1537 r = LUKS1_activate(cd, name, vk, flags);
1542 crypt_safe_free(read_passphrase);
1543 crypt_free_volume_key(vk);
1545 return r < 0 ? r : keyslot;
1548 int crypt_activate_by_keyfile(struct crypt_device *cd,
1551 const char *keyfile,
1552 size_t keyfile_size,
1555 crypt_status_info ci;
1556 struct volume_key *vk = NULL;
1557 char *passphrase_read = NULL;
1558 size_t passphrase_size_read;
1559 unsigned int key_count = 0;
1562 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1563 name ?: "", keyslot, keyfile ?: "[none]");
1566 ci = crypt_status(NULL, name);
1567 if (ci == CRYPT_INVALID)
1569 else if (ci >= CRYPT_ACTIVE) {
1570 log_err(cd, _("Device %s already exists.\n"), name);
1578 if (isPLAIN(cd->type)) {
1582 r = key_from_file(cd, _("Enter passphrase: "),
1583 &passphrase_read, &passphrase_size_read,
1584 keyfile, keyfile_size);
1588 r = process_key(cd, cd->plain_hdr.hash,
1589 cd->volume_key->keylength,
1590 passphrase_read, passphrase_size_read, &vk);
1594 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1595 } else if (isLUKS(cd->type)) {
1596 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1597 &passphrase_size_read, keyfile, keyfile_size);
1600 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase_read,
1601 passphrase_size_read, &cd->hdr, &vk, cd);
1607 r = LUKS1_activate(cd, name, vk, flags);
1612 } else if (isLOOPAES(cd->type)) {
1613 r = key_from_file(cd, NULL, &passphrase_read, &passphrase_size_read,
1614 keyfile, keyfile_size);
1617 r = LOOPAES_parse_keyfile(cd, &vk, cd->loopaes_hdr.hash, &key_count,
1618 passphrase_read, passphrase_size_read);
1622 r = LOOPAES_activate(cd, name, cd->loopaes_cipher,
1623 key_count, vk, flags);
1628 crypt_safe_free(passphrase_read);
1629 crypt_free_volume_key(vk);
1634 int crypt_activate_by_volume_key(struct crypt_device *cd,
1636 const char *volume_key,
1637 size_t volume_key_size,
1640 crypt_status_info ci;
1641 struct volume_key *vk = NULL;
1644 log_dbg("Activating volume %s by volume key.", name);
1647 ci = crypt_status(NULL, name);
1648 if (ci == CRYPT_INVALID)
1650 else if (ci >= CRYPT_ACTIVE) {
1651 log_err(cd, _("Device %s already exists.\n"), name);
1656 /* use key directly, no hash */
1657 if (isPLAIN(cd->type)) {
1661 if (!volume_key || !volume_key_size || !cd->volume_key ||
1662 volume_key_size != cd->volume_key->keylength) {
1663 log_err(cd, _("Incorrect volume key specified for plain device.\n"));
1667 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1671 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1672 } else if (isLUKS(cd->type)) {
1673 /* If key is not provided, try to use internal key */
1675 if (!cd->volume_key) {
1676 log_err(cd, _("Volume key does not match the volume.\n"));
1679 volume_key_size = cd->volume_key->keylength;
1680 volume_key = cd->volume_key->key;
1683 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1686 r = LUKS_verify_volume_key(&cd->hdr, vk);
1689 log_err(cd, _("Volume key does not match the volume.\n"));
1692 r = LUKS1_activate(cd, name, vk, flags);
1694 log_err(cd, _("Device type is not properly initialised.\n"));
1696 crypt_free_volume_key(vk);
1701 int crypt_deactivate(struct crypt_device *cd, const char *name)
1708 log_dbg("Deactivating volume %s.", name);
1710 if (!cd && dm_init(NULL, 1) < 0)
1713 switch (crypt_status(cd, name)) {
1715 r = dm_remove_device(name, 0, 0);
1718 log_err(cd, _("Device %s is busy.\n"), name);
1721 case CRYPT_INACTIVE:
1722 log_err(cd, _("Device %s is not active.\n"), name);
1726 log_err(cd, _("Invalid device %s.\n"), name);
1736 int crypt_volume_key_get(struct crypt_device *cd,
1739 size_t *volume_key_size,
1740 const char *passphrase,
1741 size_t passphrase_size)
1743 struct volume_key *vk = NULL;
1747 key_len = crypt_get_volume_key_size(cd);
1748 if (key_len > *volume_key_size) {
1749 log_err(cd, _("Volume key buffer too small.\n"));
1753 if (isPLAIN(cd->type) && cd->plain_hdr.hash) {
1754 r = process_key(cd, cd->plain_hdr.hash, key_len,
1755 passphrase, passphrase_size, &vk);
1757 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
1758 } else if (isLUKS(cd->type)) {
1759 r = LUKS_open_key_with_hdr(mdata_device(cd), keyslot, passphrase,
1760 passphrase_size, &cd->hdr, &vk, cd);
1763 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
1766 memcpy(volume_key, vk->key, vk->keylength);
1767 *volume_key_size = vk->keylength;
1770 crypt_free_volume_key(vk);
1774 int crypt_volume_key_verify(struct crypt_device *cd,
1775 const char *volume_key,
1776 size_t volume_key_size)
1778 struct volume_key *vk;
1781 if (!isLUKS(cd->type)) {
1782 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1786 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1790 r = LUKS_verify_volume_key(&cd->hdr, vk);
1793 log_err(cd, _("Volume key does not match the volume.\n"));
1795 crypt_free_volume_key(vk);
1800 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
1802 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
1803 cd->timeout = timeout_sec;
1806 void crypt_set_password_retry(struct crypt_device *cd, int tries)
1808 log_dbg("Password retry count set to %d.", tries);
1812 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
1814 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
1815 cd->iteration_time = iteration_time_ms;
1818 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
1820 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
1821 cd->password_verify = password_verify ? 1 : 0;
1824 void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
1827 case CRYPT_RNG_URANDOM:
1828 case CRYPT_RNG_RANDOM:
1829 log_dbg("RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
1830 cd->rng_type = rng_type;
1834 int crypt_get_rng_type(struct crypt_device *cd)
1839 return cd->rng_type;
1842 int crypt_memory_lock(struct crypt_device *cd, int lock)
1844 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
1848 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
1852 if (!cd && dm_init(NULL, 1) < 0)
1853 return CRYPT_INVALID;
1855 r = dm_status_device(name);
1860 if (r < 0 && r != -ENODEV)
1861 return CRYPT_INVALID;
1864 return CRYPT_ACTIVE;
1869 return CRYPT_INACTIVE;
1872 static void hexprintICB(struct crypt_device *cd, char *d, int n)
1875 for(i = 0; i < n; i++)
1876 log_std(cd, "%02hhx ", (char)d[i]);
1879 int crypt_dump(struct crypt_device *cd)
1882 if (!isLUKS(cd->type)) { //FIXME
1883 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1887 log_std(cd, "LUKS header information for %s\n\n", mdata_device(cd));
1888 log_std(cd, "Version: \t%d\n", cd->hdr.version);
1889 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
1890 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
1891 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec);
1892 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
1893 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
1894 log_std(cd, "MK digest: \t");
1895 hexprintICB(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE);
1897 log_std(cd, "MK salt: \t");
1898 hexprintICB(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2);
1899 log_std(cd, "\n \t");
1900 hexprintICB(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
1902 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
1903 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
1904 for(i = 0; i < LUKS_NUMKEYS; i++) {
1905 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
1906 log_std(cd, "Key Slot %d: ENABLED\n",i);
1907 log_std(cd, "\tIterations: \t%d\n",
1908 cd->hdr.keyblock[i].passwordIterations);
1909 log_std(cd, "\tSalt: \t");
1910 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt,
1912 log_std(cd, "\n\t \t");
1913 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt +
1914 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
1917 log_std(cd, "\tKey material offset:\t%d\n",
1918 cd->hdr.keyblock[i].keyMaterialOffset);
1919 log_std(cd, "\tAF stripes: \t%d\n",
1920 cd->hdr.keyblock[i].stripes);
1923 log_std(cd, "Key Slot %d: DISABLED\n", i);
1929 const char *crypt_get_cipher(struct crypt_device *cd)
1931 if (isPLAIN(cd->type))
1932 return cd->plain_cipher;
1934 if (isLUKS(cd->type))
1935 return cd->hdr.cipherName;
1937 if (isLOOPAES(cd->type))
1938 return cd->loopaes_cipher;
1943 const char *crypt_get_cipher_mode(struct crypt_device *cd)
1945 if (isPLAIN(cd->type))
1946 return cd->plain_cipher_mode;
1948 if (isLUKS(cd->type))
1949 return cd->hdr.cipherMode;
1951 if (isLOOPAES(cd->type))
1952 return cd->loopaes_cipher_mode;
1957 const char *crypt_get_uuid(struct crypt_device *cd)
1959 if (isLUKS(cd->type))
1960 return cd->hdr.uuid;
1962 if (isPLAIN(cd->type))
1963 return cd->plain_uuid;
1965 if (isLOOPAES(cd->type))
1966 return cd->loopaes_uuid;
1971 const char *crypt_get_device_name(struct crypt_device *cd)
1977 int crypt_get_volume_key_size(struct crypt_device *cd)
1979 if (isPLAIN(cd->type) && cd->volume_key)
1980 return cd->volume_key->keylength;
1982 if (isLUKS(cd->type))
1983 return cd->hdr.keyBytes;
1985 if (isLOOPAES(cd->type))
1986 return cd->loopaes_key_size;
1991 uint64_t crypt_get_data_offset(struct crypt_device *cd)
1993 if (isPLAIN(cd->type))
1994 return cd->plain_hdr.offset;
1996 if (isLUKS(cd->type))
1997 return cd->hdr.payloadOffset;
1999 if (isLOOPAES(cd->type))
2000 return cd->loopaes_hdr.offset;
2005 uint64_t crypt_get_iv_offset(struct crypt_device *cd)
2007 if (isPLAIN(cd->type))
2008 return cd->plain_hdr.skip;
2010 if (isLUKS(cd->type))
2013 if (isLOOPAES(cd->type))
2014 return cd->loopaes_hdr.skip;
2019 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
2021 if (!isLUKS(cd->type)) {
2022 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2023 return CRYPT_SLOT_INVALID;
2026 return LUKS_keyslot_info(&cd->hdr, keyslot);
2029 int crypt_keyslot_max(const char *type)
2031 if (type && isLUKS(type))
2032 return LUKS_NUMKEYS;
2037 const char *crypt_get_type(struct crypt_device *cd)
2042 int crypt_get_active_device(struct crypt_device *cd __attribute__((unused)),
2044 struct crypt_active_device *cad)
2046 struct crypt_dm_active_device dmd;
2049 r = dm_query_device(name, 0, &dmd);
2053 cad->offset = dmd.offset;
2054 cad->iv_offset = dmd.iv_offset;
2055 cad->size = dmd.size;
2056 cad->flags = dmd.flags;