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"
41 struct volume_key *volume_key;
43 uint64_t iteration_time;
48 /* used in CRYPT_LUKS1 */
50 uint64_t PBKDF2_per_sec;
52 /* used in CRYPT_PLAIN */
53 struct crypt_params_plain plain_hdr;
55 char *plain_cipher_mode;
58 /* used in CRYPT_LOOPAES */
59 struct crypt_params_loopaes loopaes_hdr;
61 char *loopaes_cipher_mode;
63 unsigned int loopaes_key_size;
65 /* callbacks definitions */
66 void (*log)(int level, const char *msg, void *usrptr);
68 int (*confirm)(const char *msg, void *usrptr);
70 int (*password)(const char *msg, char *buf, size_t length, void *usrptr);
71 void *password_usrptr;
75 static void (*_default_log)(int level, const char *msg, void *usrptr) = NULL;
76 static int _debug_level = 0;
78 void crypt_set_debug_level(int level)
83 int crypt_get_debug_level(void)
88 void crypt_log(struct crypt_device *cd, int level, const char *msg)
91 cd->log(level, msg, cd->log_usrptr);
92 else if (_default_log)
93 _default_log(level, msg, NULL);
96 __attribute__((format(printf, 5, 6)))
97 void logger(struct crypt_device *cd, int level, const char *file,
98 int line, const char *format, ...)
103 va_start(argp, format);
105 if (vasprintf(&target, format, argp) > 0) {
107 crypt_log(cd, level, target);
109 } else if (_debug_level)
110 printf("# %s:%d %s\n", file ?: "?", line, target);
112 } else if (_debug_level)
113 printf("# %s\n", target);
121 static int init_crypto(struct crypt_device *ctx)
125 r = crypt_random_init(ctx);
127 log_err(ctx, _("Cannot initialize crypto RNG backend.\n"));
131 r = crypt_backend_init(ctx);
133 log_err(ctx, _("Cannot initialize crypto backend.\n"));
138 static int process_key(struct crypt_device *cd, const char *hash_name,
139 size_t key_size, const char *pass, size_t passLen,
140 struct volume_key **vk)
147 *vk = crypt_alloc_volume_key(key_size, NULL);
152 r = crypt_plain_hash(cd, hash_name, (*vk)->key, key_size, pass, passLen);
155 log_err(cd, _("Hash algorithm %s not supported.\n"),
158 log_err(cd, _("Key processing error (using hash %s).\n"),
160 crypt_free_volume_key(*vk);
164 } else if (passLen > key_size) {
165 memcpy((*vk)->key, pass, key_size);
167 memcpy((*vk)->key, pass, passLen);
173 static int isPLAIN(const char *type)
175 return (type && !strcmp(CRYPT_PLAIN, type));
178 static int isLUKS(const char *type)
180 return (type && !strcmp(CRYPT_LUKS1, type));
183 static int isLOOPAES(const char *type)
185 return (type && !strcmp(CRYPT_LOOPAES, type));
188 /* keyslot helpers */
189 static int keyslot_verify_or_find_empty(struct crypt_device *cd, int *keyslot)
191 if (*keyslot == CRYPT_ANY_SLOT) {
192 *keyslot = LUKS_keyslot_find_empty(&cd->hdr);
194 log_err(cd, _("All key slots full.\n"));
199 switch (LUKS_keyslot_info(&cd->hdr, *keyslot)) {
200 case CRYPT_SLOT_INVALID:
201 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
202 *keyslot, LUKS_NUMKEYS - 1);
204 case CRYPT_SLOT_INACTIVE:
207 log_err(cd, _("Key slot %d is full, please select another one.\n"),
215 int PLAIN_activate(struct crypt_device *cd,
217 struct volume_key *vk,
222 char *dm_cipher = NULL;
223 struct crypt_dm_active_device dmd = {
224 .device = crypt_get_device_name(cd),
226 .uuid = crypt_get_uuid(cd),
228 .offset = crypt_get_data_offset(cd),
229 .iv_offset = crypt_get_iv_offset(cd),
234 r = device_check_and_adjust(cd, dmd.device,
235 (dmd.flags & CRYPT_ACTIVATE_SHARED) ? DEV_SHARED : DEV_EXCL,
236 &dmd.size, &dmd.offset, &flags);
240 if (crypt_get_cipher_mode(cd))
241 r = asprintf(&dm_cipher, "%s-%s", crypt_get_cipher(cd), crypt_get_cipher_mode(cd));
243 r = asprintf(&dm_cipher, "%s", crypt_get_cipher(cd));
247 dmd.cipher = dm_cipher;
248 log_dbg("Trying to activate PLAIN device %s using cipher %s.", name, dmd.cipher);
250 r = dm_create_device(name, CRYPT_PLAIN, &dmd, 0);
253 if (!cd->plain_uuid && dm_query_device(name, DM_ACTIVE_UUID, &dmd) >= 0)
254 cd->plain_uuid = (char*)dmd.uuid;
260 int crypt_confirm(struct crypt_device *cd, const char *msg)
262 if (!cd || !cd->confirm)
265 return cd->confirm(msg, cd->confirm_usrptr);
268 static int key_from_terminal(struct crypt_device *cd, char *msg, char **key,
269 size_t *key_len, int force_verify)
275 if(!msg && asprintf(&prompt, _("Enter passphrase for %s: "),
276 cd->backing_file ?: cd->device) < 0)
283 *key = crypt_safe_alloc(DEFAULT_PASSPHRASE_SIZE_MAX);
288 r = cd->password(msg, *key, DEFAULT_PASSPHRASE_SIZE_MAX,
289 cd->password_usrptr);
291 crypt_safe_free(*key);
296 r = crypt_get_key(msg, key, key_len, 0, NULL, cd->timeout,
297 (force_verify || cd->password_verify), cd);
300 return (r < 0) ? r: 0;
303 static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslot,
304 struct volume_key **vk)
306 char *passphrase_read = NULL;
307 size_t passphrase_size_read;
308 int r = -EINVAL, eperm = 0, tries = cd->tries;
312 crypt_free_volume_key(*vk);
315 r = key_from_terminal(cd, NULL, &passphrase_read,
316 &passphrase_size_read, 0);
320 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
321 passphrase_size_read, &cd->hdr, vk, cd);
324 crypt_safe_free(passphrase_read);
325 passphrase_read = NULL;
326 } while (r == -EPERM && (--tries > 0));
329 crypt_free_volume_key(*vk);
332 /* Report wrong passphrase if at least one try failed */
333 if (eperm && r == -EPIPE)
337 crypt_safe_free(passphrase_read);
341 static int key_from_file(struct crypt_device *cd, char *msg,
342 char **key, size_t *key_len,
343 const char *key_file, size_t key_size)
345 return crypt_get_key(msg, key, key_len, key_size, key_file,
349 void crypt_set_log_callback(struct crypt_device *cd,
350 void (*log)(int level, const char *msg, void *usrptr),
357 cd->log_usrptr = usrptr;
361 void crypt_set_confirm_callback(struct crypt_device *cd,
362 int (*confirm)(const char *msg, void *usrptr),
365 cd->confirm = confirm;
366 cd->confirm_usrptr = usrptr;
369 void crypt_set_password_callback(struct crypt_device *cd,
370 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
373 cd->password = password;
374 cd->password_usrptr = usrptr;
377 void crypt_get_error(char *buf, size_t size)
379 const char *error = get_error();
381 if (!buf || size < 1)
384 strncpy(buf, error, size - 1);
385 buf[size - 1] = '\0';
391 const char *crypt_get_dir(void)
396 int crypt_init(struct crypt_device **cd, const char *device)
398 struct crypt_device *h = NULL;
404 log_dbg("Allocating crypt device %s context.", device);
406 if (!(h = malloc(sizeof(struct crypt_device))))
409 memset(h, 0, sizeof(*h));
413 r = device_ready(NULL, device, O_RDONLY);
415 h->device = crypt_loop_get_device();
416 log_dbg("Not a block device, %s%s.",
417 h->device ? "using free loop device " :
418 "no free loop device found",
421 log_err(NULL, _("Cannot find a free loopback device.\n"));
426 /* Keep the loop open, dettached on last close. */
427 h->loop_fd = crypt_loop_attach(h->device, device, 0, 1, &readonly);
428 if (h->loop_fd == -1) {
429 log_err(NULL, _("Attaching loopback device failed "
430 "(loop device with autoclear flag is required).\n"));
435 h->backing_file = crypt_loop_backing_file(h->device);
436 r = device_ready(NULL, h->device, O_RDONLY);
444 if (!h->device && device && !(h->device = strdup(device))) {
449 if (dm_init(h, 1) < 0) {
454 h->iteration_time = 1000;
455 h->password_verify = 0;
457 h->rng_type = crypt_random_default_key_rng();
463 if (h->loop_fd != -1)
466 free(h->backing_file);
472 int crypt_init_by_name(struct crypt_device **cd, const char *name)
474 crypt_status_info ci;
475 struct crypt_dm_active_device dmd;
476 char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
480 log_dbg("Allocating crypt device context by device %s.", name);
482 ci = crypt_status(NULL, name);
483 if (ci == CRYPT_INVALID)
486 if (ci < CRYPT_ACTIVE) {
487 log_err(NULL, _("Device %s is not active.\n"), name);
491 r = dm_query_device(name, DM_ACTIVE_ALL, &dmd);
496 r = crypt_init(cd, dmd.device);
498 /* Underlying device disappeared but mapping still active */
499 if (!dmd.device || r == -ENOTBLK)
500 log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
503 /* Underlying device is not readable but crypt mapping exists */
505 free((char*)dmd.device);
507 r = crypt_init(cd, NULL);
513 /* Try to initialise basic parameters from active device */
515 if (!(*cd)->backing_file && dmd.device && crypt_loop_device(dmd.device) &&
516 !((*cd)->backing_file = crypt_loop_backing_file(dmd.device))) {
522 if (!strncmp(CRYPT_PLAIN, dmd.uuid, sizeof(CRYPT_PLAIN)-1)) {
523 (*cd)->type = strdup(CRYPT_PLAIN);
524 (*cd)->plain_uuid = strdup(dmd.uuid);
525 (*cd)->plain_hdr.hash = NULL; /* no way to get this */
526 (*cd)->plain_hdr.offset = dmd.offset;
527 (*cd)->plain_hdr.skip = dmd.iv_offset;
528 (*cd)->volume_key = dmd.vk;
531 r = crypt_parse_name_and_mode(dmd.cipher, cipher, NULL, cipher_mode);
533 (*cd)->plain_cipher = strdup(cipher);
534 (*cd)->plain_cipher_mode = strdup(cipher_mode);
536 } else if (!strncmp(CRYPT_LOOPAES, dmd.uuid, sizeof(CRYPT_LOOPAES)-1)) {
537 (*cd)->type = strdup(CRYPT_LOOPAES);
538 (*cd)->loopaes_uuid = strdup(dmd.uuid);
539 (*cd)->loopaes_hdr.offset = dmd.offset;
541 r = crypt_parse_name_and_mode(dmd.cipher, cipher,
542 &key_nums, cipher_mode);
544 (*cd)->loopaes_cipher = strdup(cipher);
545 (*cd)->loopaes_cipher_mode = strdup(cipher_mode);
546 /* version 3 uses last key for IV */
547 if (dmd.vk->keylength % key_nums)
549 (*cd)->loopaes_key_size = dmd.vk->keylength / key_nums;
551 } else if (!strncmp(CRYPT_LUKS1, dmd.uuid, sizeof(CRYPT_LUKS1)-1)) {
553 if (crypt_load(*cd, CRYPT_LUKS1, NULL) < 0 ||
554 crypt_volume_key_verify(*cd, dmd.vk->key, dmd.vk->keylength) < 0) {
555 log_dbg("LUKS device header does not match active device.");
559 (*cd)->volume_key = dmd.vk;
564 log_dbg("Active device has no UUID set, some parameters are not set.");
571 crypt_free_volume_key(dmd.vk);
572 free((char*)dmd.device);
573 free((char*)dmd.cipher);
574 free((char*)dmd.uuid);
578 static int _crypt_format_plain(struct crypt_device *cd,
580 const char *cipher_mode,
582 size_t volume_key_size,
583 struct crypt_params_plain *params)
585 if (!cipher || !cipher_mode) {
586 log_err(cd, _("Invalid plain crypt parameters.\n"));
590 if (volume_key_size > 1024) {
591 log_err(cd, _("Invalid key size.\n"));
595 cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
599 cd->plain_cipher = strdup(cipher);
600 cd->plain_cipher_mode = strdup(cipher_mode);
603 cd->plain_uuid = strdup(uuid);
605 if (params && params->hash)
606 cd->plain_hdr.hash = strdup(params->hash);
608 cd->plain_hdr.offset = params ? params->offset : 0;
609 cd->plain_hdr.skip = params ? params->skip : 0;
610 cd->plain_hdr.size = params ? params->size : 0;
612 if (!cd->plain_cipher || !cd->plain_cipher_mode)
618 static int _crypt_format_luks1(struct crypt_device *cd,
620 const char *cipher_mode,
622 const char *volume_key,
623 size_t volume_key_size,
624 struct crypt_params_luks1 *params)
627 unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
628 unsigned long alignment_offset = 0;
631 log_err(cd, _("Can't format LUKS without device.\n"));
636 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
639 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
644 if (params && params->data_alignment)
645 required_alignment = params->data_alignment * SECTOR_SIZE;
647 get_topology_alignment(cd->device, &required_alignment,
648 &alignment_offset, DEFAULT_DISK_ALIGNMENT);
650 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
651 (params && params->hash) ? params->hash : "sha1",
653 required_alignment / SECTOR_SIZE,
654 alignment_offset / SECTOR_SIZE,
655 cd->iteration_time, &cd->PBKDF2_per_sec, cd);
659 /* Wipe first 8 sectors - fs magic numbers etc. */
660 r = wipe_device_header(cd->device, 8);
663 log_err(cd, _("Cannot format device %s which is still in use.\n"),
666 log_err(cd, _("Cannot wipe header on device %s.\n"),
672 r = LUKS_write_phdr(cd->device, &cd->hdr, cd);
677 static int _crypt_format_loopaes(struct crypt_device *cd,
680 size_t volume_key_size,
681 struct crypt_params_loopaes *params)
684 log_err(cd, _("Can't format LOOPAES without device.\n"));
688 if (volume_key_size > 1024) {
689 log_err(cd, _("Invalid key size.\n"));
693 cd->loopaes_key_size = volume_key_size;
695 cd->loopaes_cipher = strdup(cipher ?: DEFAULT_LOOPAES_CIPHER);
698 cd->loopaes_uuid = strdup(uuid);
700 if (params && params->hash)
701 cd->loopaes_hdr.hash = strdup(params->hash);
703 cd->loopaes_hdr.offset = params ? params->offset : 0;
704 cd->loopaes_hdr.skip = params ? params->skip : 0;
709 int crypt_format(struct crypt_device *cd,
712 const char *cipher_mode,
714 const char *volume_key,
715 size_t volume_key_size,
723 log_dbg("Formatting device %s as type %s.", cd->device ?: "(none)", type);
730 r = _crypt_format_plain(cd, cipher, cipher_mode,
731 uuid, volume_key_size, params);
732 else if (isLUKS(type))
733 r = _crypt_format_luks1(cd, cipher, cipher_mode,
734 uuid, volume_key, volume_key_size, params);
735 else if (isLOOPAES(type))
736 r = _crypt_format_loopaes(cd, cipher, uuid, volume_key_size, params);
738 /* FIXME: allow plugins here? */
739 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
743 if (!r && !(cd->type = strdup(type)))
747 crypt_free_volume_key(cd->volume_key);
748 cd->volume_key = NULL;
754 int crypt_load(struct crypt_device *cd,
755 const char *requested_type,
756 void *params __attribute__((unused)))
758 struct luks_phdr hdr;
761 log_dbg("Trying to load %s crypt type from device %s.",
762 requested_type ?: "any", cd->device ?: "(none)");
767 if (requested_type && !isLUKS(requested_type))
774 r = LUKS_read_phdr(cd->device, &hdr, 1, cd);
777 memcpy(&cd->hdr, &hdr, sizeof(hdr));
778 cd->type = strdup(CRYPT_LUKS1);
786 int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
788 struct crypt_dm_active_device dmd;
791 /* Device context type must be initialised */
792 if (!cd->type || !crypt_get_uuid(cd))
795 log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
797 r = dm_query_device(name, DM_ACTIVE_ALL, &dmd);
799 log_err(NULL, _("Device %s is not active.\n"), name);
808 r = device_check_and_adjust(cd, dmd.device, DEV_OK, &new_size, &dmd.offset, &dmd.flags);
812 if (new_size == dmd.size) {
813 log_dbg("Device has already requested size %" PRIu64
814 " sectors.", dmd.size);
818 r = dm_create_device(name, cd->type, &dmd, 1);
821 crypt_free_volume_key(dmd.vk);
822 free((char*)dmd.cipher);
823 free((char*)dmd.device);
824 free((char*)dmd.uuid);
829 int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
831 if (!isLUKS(cd->type)) {
832 log_err(cd, _("This operation is not supported for this device type.\n"));
836 if (uuid && !strncmp(uuid, cd->hdr.uuid, sizeof(cd->hdr.uuid))) {
837 log_dbg("UUID is the same as requested (%s) for device %s.",
843 log_dbg("Requested new UUID change to %s for %s.", uuid, cd->device);
845 log_dbg("Requested new UUID refresh for %s.", cd->device);
847 if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
850 return LUKS_hdr_uuid_set(cd->device, &cd->hdr, uuid, cd);
853 int crypt_header_backup(struct crypt_device *cd,
854 const char *requested_type,
855 const char *backup_file)
859 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
866 log_dbg("Requested header backup of device %s (%s) to "
867 "file %s.", cd->device, requested_type, backup_file);
869 return LUKS_hdr_backup(backup_file, cd->device, &cd->hdr, cd);
872 int crypt_header_restore(struct crypt_device *cd,
873 const char *requested_type,
874 const char *backup_file)
878 if (requested_type && !isLUKS(requested_type))
881 /* Some hash functions need initialized gcrypt library */
886 log_dbg("Requested header restore to device %s (%s) from "
887 "file %s.", cd->device, requested_type, backup_file);
889 return LUKS_hdr_restore(backup_file, cd->device, &cd->hdr, cd);
892 void crypt_free(struct crypt_device *cd)
895 log_dbg("Releasing crypt device %s context.", cd->device);
897 if (cd->loop_fd != -1)
901 crypt_free_volume_key(cd->volume_key);
904 free(cd->backing_file);
907 /* used in plain device only */
908 free((char*)cd->plain_hdr.hash);
909 free(cd->plain_cipher);
910 free(cd->plain_cipher_mode);
911 free(cd->plain_uuid);
913 /* used in loop-AES device only */
914 free((char*)cd->loopaes_hdr.hash);
915 free(cd->loopaes_cipher);
916 free(cd->loopaes_uuid);
922 int crypt_suspend(struct crypt_device *cd,
925 crypt_status_info ci;
928 log_dbg("Suspending volume %s.", name);
930 ci = crypt_status(NULL, name);
931 if (ci < CRYPT_ACTIVE) {
932 log_err(cd, _("Volume %s is not active.\n"), name);
936 if (!cd && dm_init(NULL, 1) < 0)
939 r = dm_status_suspended(name);
944 log_err(cd, _("Volume %s is already suspended.\n"), name);
949 r = dm_suspend_and_wipe_key(name);
951 log_err(cd, "Suspend is not supported for device %s.\n", name);
953 log_err(cd, "Error during suspending device %s.\n", name);
960 int crypt_resume_by_passphrase(struct crypt_device *cd,
963 const char *passphrase,
964 size_t passphrase_size)
966 struct volume_key *vk = NULL;
969 log_dbg("Resuming volume %s.", name);
971 if (!isLUKS(cd->type)) {
972 log_err(cd, _("This operation is supported only for LUKS device.\n"));
977 r = dm_status_suspended(name);
982 log_err(cd, _("Volume %s is not suspended.\n"), name);
987 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
988 passphrase_size, &cd->hdr, &vk, cd);
990 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
994 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
996 log_err(cd, "Resume is not supported for device %s.\n", name);
998 log_err(cd, "Error during resuming device %s.\n", name);
1002 crypt_free_volume_key(vk);
1003 return r < 0 ? r : keyslot;
1006 int crypt_resume_by_keyfile(struct crypt_device *cd,
1009 const char *keyfile,
1010 size_t keyfile_size)
1012 struct volume_key *vk = NULL;
1013 char *passphrase_read = NULL;
1014 size_t passphrase_size_read;
1017 log_dbg("Resuming volume %s.", name);
1019 if (!isLUKS(cd->type)) {
1020 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1025 r = dm_status_suspended(name);
1030 log_err(cd, _("Volume %s is not suspended.\n"), name);
1037 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1038 &passphrase_size_read, keyfile, keyfile_size);
1042 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1043 passphrase_size_read, &cd->hdr, &vk, cd);
1048 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1050 log_err(cd, "Error during resuming device %s.\n", name);
1052 crypt_safe_free(passphrase_read);
1053 crypt_free_volume_key(vk);
1054 return r < 0 ? r : keyslot;
1057 // slot manipulation
1058 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1059 int keyslot, // -1 any
1060 const char *passphrase, // NULL -> terminal
1061 size_t passphrase_size,
1062 const char *new_passphrase, // NULL -> terminal
1063 size_t new_passphrase_size)
1065 struct volume_key *vk = NULL;
1066 char *password = NULL, *new_password = NULL;
1067 size_t passwordLen, new_passwordLen;
1070 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1071 "new passphrase %sprovided.",
1072 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1074 if (!isLUKS(cd->type)) {
1075 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1079 r = keyslot_verify_or_find_empty(cd, &keyslot);
1083 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1084 /* No slots used, try to use pre-generated key in header */
1085 if (cd->volume_key) {
1086 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1087 r = vk ? 0 : -ENOMEM;
1089 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1092 } else if (passphrase) {
1093 /* Passphrase provided, use it to unlock existing keyslot */
1094 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, passphrase,
1095 passphrase_size, &cd->hdr, &vk, cd);
1097 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1098 r = key_from_terminal(cd, _("Enter any passphrase: "),
1099 &password, &passwordLen, 0);
1103 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
1104 passwordLen, &cd->hdr, &vk, cd);
1105 crypt_safe_free(password);
1111 if (new_passphrase) {
1112 new_password = (char *)new_passphrase;
1113 new_passwordLen = new_passphrase_size;
1115 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1116 &new_password, &new_passwordLen, 1);
1121 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1122 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1127 if (!new_passphrase)
1128 crypt_safe_free(new_password);
1129 crypt_free_volume_key(vk);
1130 return r ?: keyslot;
1133 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1135 const char *keyfile,
1136 size_t keyfile_size,
1137 const char *new_keyfile,
1138 size_t new_keyfile_size)
1140 struct volume_key *vk = NULL;
1141 char *password = NULL; size_t passwordLen;
1142 char *new_password = NULL; size_t new_passwordLen;
1145 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1146 keyfile ?: "[none]", new_keyfile ?: "[none]");
1148 if (!isLUKS(cd->type)) {
1149 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1153 r = keyslot_verify_or_find_empty(cd, &keyslot);
1157 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1158 /* No slots used, try to use pre-generated key in header */
1159 if (cd->volume_key) {
1160 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1161 r = vk ? 0 : -ENOMEM;
1163 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1167 /* Read password from file of (if NULL) from terminal */
1169 r = key_from_file(cd, _("Enter any passphrase: "),
1170 &password, &passwordLen,
1171 keyfile, keyfile_size);
1173 r = key_from_terminal(cd, _("Enter any passphrase: "),
1174 &password, &passwordLen, 0);
1178 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password, passwordLen,
1186 r = key_from_file(cd, _("Enter new passphrase for key slot: "),
1187 &new_password, &new_passwordLen, new_keyfile,
1190 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1191 &new_password, &new_passwordLen, 1);
1195 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1196 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1198 crypt_safe_free(password);
1199 crypt_safe_free(new_password);
1200 crypt_free_volume_key(vk);
1201 return r < 0 ? r : keyslot;
1204 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1206 const char *volume_key,
1207 size_t volume_key_size,
1208 const char *passphrase,
1209 size_t passphrase_size)
1211 struct volume_key *vk = NULL;
1213 char *new_password = NULL; size_t new_passwordLen;
1215 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1217 if (!isLUKS(cd->type)) {
1218 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1223 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1224 else if (cd->volume_key)
1225 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1230 r = LUKS_verify_volume_key(&cd->hdr, vk);
1232 log_err(cd, _("Volume key does not match the volume.\n"));
1236 r = keyslot_verify_or_find_empty(cd, &keyslot);
1241 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1242 &new_password, &new_passwordLen, 1);
1245 passphrase = new_password;
1246 passphrase_size = new_passwordLen;
1249 r = LUKS_set_key(cd->device, keyslot, passphrase, passphrase_size,
1250 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1252 crypt_safe_free(new_password);
1253 crypt_free_volume_key(vk);
1254 return (r < 0) ? r : keyslot;
1257 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1259 crypt_keyslot_info ki;
1261 log_dbg("Destroying keyslot %d.", keyslot);
1263 if (!isLUKS(cd->type)) {
1264 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1268 ki = crypt_keyslot_status(cd, keyslot);
1269 if (ki == CRYPT_SLOT_INVALID) {
1270 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1274 if (ki == CRYPT_SLOT_INACTIVE) {
1275 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1279 return LUKS_del_key(cd->device, keyslot, &cd->hdr, cd);
1282 // activation/deactivation of device mapping
1283 int crypt_activate_by_passphrase(struct crypt_device *cd,
1286 const char *passphrase,
1287 size_t passphrase_size,
1290 crypt_status_info ci;
1291 struct volume_key *vk = NULL;
1292 char *read_passphrase = NULL;
1293 size_t passphraseLen = 0;
1296 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1297 name ? "Activating" : "Checking", name ?: "",
1298 keyslot, passphrase ? "" : "[none] ");
1301 ci = crypt_status(NULL, name);
1302 if (ci == CRYPT_INVALID)
1304 else if (ci >= CRYPT_ACTIVE) {
1305 log_err(cd, _("Device %s already exists.\n"), name);
1310 /* plain, use hashed passphrase */
1311 if (isPLAIN(cd->type)) {
1316 r = key_from_terminal(cd, NULL, &read_passphrase,
1320 passphrase = read_passphrase;
1321 passphrase_size = passphraseLen;
1324 r = process_key(cd, cd->plain_hdr.hash,
1325 cd->volume_key->keylength,
1326 passphrase, passphrase_size, &vk);
1330 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1332 } else if (isLUKS(cd->type)) {
1333 /* provided passphrase, do not retry */
1335 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1336 passphrase_size, &cd->hdr, &vk, cd);
1338 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1343 r = LUKS1_activate(cd, name, vk, flags);
1348 crypt_safe_free(read_passphrase);
1349 crypt_free_volume_key(vk);
1351 return r < 0 ? r : keyslot;
1354 int crypt_activate_by_keyfile(struct crypt_device *cd,
1357 const char *keyfile,
1358 size_t keyfile_size,
1361 crypt_status_info ci;
1362 struct volume_key *vk = NULL;
1363 char *passphrase_read = NULL;
1364 size_t passphrase_size_read;
1365 unsigned int key_count = 0;
1368 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1369 name ?: "", keyslot, keyfile ?: "[none]");
1372 ci = crypt_status(NULL, name);
1373 if (ci == CRYPT_INVALID)
1375 else if (ci >= CRYPT_ACTIVE) {
1376 log_err(cd, _("Device %s already exists.\n"), name);
1384 if (isPLAIN(cd->type)) {
1388 r = key_from_file(cd, _("Enter passphrase: "),
1389 &passphrase_read, &passphrase_size_read,
1390 keyfile, keyfile_size);
1394 r = process_key(cd, cd->plain_hdr.hash,
1395 cd->volume_key->keylength,
1396 passphrase_read, passphrase_size_read, &vk);
1400 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1401 } else if (isLUKS(cd->type)) {
1402 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1403 &passphrase_size_read, keyfile, keyfile_size);
1406 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1407 passphrase_size_read, &cd->hdr, &vk, cd);
1413 r = LUKS1_activate(cd, name, vk, flags);
1418 } else if (isLOOPAES(cd->type)) {
1419 r = key_from_file(cd, NULL, &passphrase_read, &passphrase_size_read,
1420 keyfile, keyfile_size);
1423 r = LOOPAES_parse_keyfile(cd, &vk, cd->loopaes_hdr.hash, &key_count,
1424 passphrase_read, passphrase_size_read);
1428 r = LOOPAES_activate(cd, name, cd->loopaes_cipher,
1429 key_count, vk, flags);
1434 crypt_safe_free(passphrase_read);
1435 crypt_free_volume_key(vk);
1440 int crypt_activate_by_volume_key(struct crypt_device *cd,
1442 const char *volume_key,
1443 size_t volume_key_size,
1446 crypt_status_info ci;
1447 struct volume_key *vk = NULL;
1450 log_dbg("Activating volume %s by volume key.", name);
1453 ci = crypt_status(NULL, name);
1454 if (ci == CRYPT_INVALID)
1456 else if (ci >= CRYPT_ACTIVE) {
1457 log_err(cd, _("Device %s already exists.\n"), name);
1462 /* use key directly, no hash */
1463 if (isPLAIN(cd->type)) {
1467 if (!volume_key || !volume_key_size || !cd->volume_key ||
1468 volume_key_size != cd->volume_key->keylength) {
1469 log_err(cd, _("Incorrect volume key specified for plain device.\n"));
1473 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1477 r = PLAIN_activate(cd, name, vk, cd->plain_hdr.size, flags);
1478 } else if (isLUKS(cd->type)) {
1479 /* If key is not provided, try to use internal key */
1481 if (!cd->volume_key) {
1482 log_err(cd, _("Volume key does not match the volume.\n"));
1485 volume_key_size = cd->volume_key->keylength;
1486 volume_key = cd->volume_key->key;
1489 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1492 r = LUKS_verify_volume_key(&cd->hdr, vk);
1495 log_err(cd, _("Volume key does not match the volume.\n"));
1498 r = LUKS1_activate(cd, name, vk, flags);
1500 log_err(cd, _("Device type is not properly initialised.\n"));
1502 crypt_free_volume_key(vk);
1507 int crypt_deactivate(struct crypt_device *cd, const char *name)
1514 log_dbg("Deactivating volume %s.", name);
1516 if (!cd && dm_init(NULL, 1) < 0)
1519 switch (crypt_status(cd, name)) {
1521 r = dm_remove_device(name, 0, 0);
1524 log_err(cd, _("Device %s is busy.\n"), name);
1527 case CRYPT_INACTIVE:
1528 log_err(cd, _("Device %s is not active.\n"), name);
1532 log_err(cd, _("Invalid device %s.\n"), name);
1542 int crypt_volume_key_get(struct crypt_device *cd,
1545 size_t *volume_key_size,
1546 const char *passphrase,
1547 size_t passphrase_size)
1549 struct volume_key *vk = NULL;
1553 key_len = crypt_get_volume_key_size(cd);
1554 if (key_len > *volume_key_size) {
1555 log_err(cd, _("Volume key buffer too small.\n"));
1559 if (isPLAIN(cd->type) && cd->plain_hdr.hash) {
1560 r = process_key(cd, cd->plain_hdr.hash, key_len,
1561 passphrase, passphrase_size, &vk);
1563 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
1564 } else if (isLUKS(cd->type)) {
1565 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1566 passphrase_size, &cd->hdr, &vk, cd);
1569 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
1572 memcpy(volume_key, vk->key, vk->keylength);
1573 *volume_key_size = vk->keylength;
1576 crypt_free_volume_key(vk);
1580 int crypt_volume_key_verify(struct crypt_device *cd,
1581 const char *volume_key,
1582 size_t volume_key_size)
1584 struct volume_key *vk;
1587 if (!isLUKS(cd->type)) {
1588 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1592 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1596 r = LUKS_verify_volume_key(&cd->hdr, vk);
1599 log_err(cd, _("Volume key does not match the volume.\n"));
1601 crypt_free_volume_key(vk);
1606 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
1608 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
1609 cd->timeout = timeout_sec;
1612 void crypt_set_password_retry(struct crypt_device *cd, int tries)
1614 log_dbg("Password retry count set to %d.", tries);
1618 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
1620 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
1621 cd->iteration_time = iteration_time_ms;
1624 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
1626 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
1627 cd->password_verify = password_verify ? 1 : 0;
1630 void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
1633 case CRYPT_RNG_URANDOM:
1634 case CRYPT_RNG_RANDOM:
1635 log_dbg("RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
1636 cd->rng_type = rng_type;
1640 int crypt_get_rng_type(struct crypt_device *cd)
1645 return cd->rng_type;
1648 int crypt_memory_lock(struct crypt_device *cd, int lock)
1650 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
1654 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
1658 if (!cd && dm_init(NULL, 1) < 0)
1659 return CRYPT_INVALID;
1661 r = dm_status_device(name);
1666 if (r < 0 && r != -ENODEV)
1667 return CRYPT_INVALID;
1670 return CRYPT_ACTIVE;
1675 return CRYPT_INACTIVE;
1678 static void hexprintICB(struct crypt_device *cd, char *d, int n)
1681 for(i = 0; i < n; i++)
1682 log_std(cd, "%02hhx ", (char)d[i]);
1685 int crypt_dump(struct crypt_device *cd)
1688 if (!isLUKS(cd->type)) { //FIXME
1689 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1693 log_std(cd, "LUKS header information for %s\n\n", cd->device);
1694 log_std(cd, "Version: \t%d\n", cd->hdr.version);
1695 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
1696 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
1697 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec);
1698 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
1699 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
1700 log_std(cd, "MK digest: \t");
1701 hexprintICB(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE);
1703 log_std(cd, "MK salt: \t");
1704 hexprintICB(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2);
1705 log_std(cd, "\n \t");
1706 hexprintICB(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
1708 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
1709 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
1710 for(i = 0; i < LUKS_NUMKEYS; i++) {
1711 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
1712 log_std(cd, "Key Slot %d: ENABLED\n",i);
1713 log_std(cd, "\tIterations: \t%d\n",
1714 cd->hdr.keyblock[i].passwordIterations);
1715 log_std(cd, "\tSalt: \t");
1716 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt,
1718 log_std(cd, "\n\t \t");
1719 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt +
1720 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
1723 log_std(cd, "\tKey material offset:\t%d\n",
1724 cd->hdr.keyblock[i].keyMaterialOffset);
1725 log_std(cd, "\tAF stripes: \t%d\n",
1726 cd->hdr.keyblock[i].stripes);
1729 log_std(cd, "Key Slot %d: DISABLED\n", i);
1735 const char *crypt_get_cipher(struct crypt_device *cd)
1737 if (isPLAIN(cd->type))
1738 return cd->plain_cipher;
1740 if (isLUKS(cd->type))
1741 return cd->hdr.cipherName;
1743 if (isLOOPAES(cd->type))
1744 return cd->loopaes_cipher;
1749 const char *crypt_get_cipher_mode(struct crypt_device *cd)
1751 if (isPLAIN(cd->type))
1752 return cd->plain_cipher_mode;
1754 if (isLUKS(cd->type))
1755 return cd->hdr.cipherMode;
1757 if (isLOOPAES(cd->type))
1758 return cd->loopaes_cipher_mode;
1763 const char *crypt_get_uuid(struct crypt_device *cd)
1765 if (isLUKS(cd->type))
1766 return cd->hdr.uuid;
1768 if (isPLAIN(cd->type))
1769 return cd->plain_uuid;
1771 if (isLOOPAES(cd->type))
1772 return cd->loopaes_uuid;
1777 const char *crypt_get_device_name(struct crypt_device *cd)
1782 int crypt_get_volume_key_size(struct crypt_device *cd)
1784 if (isPLAIN(cd->type) && cd->volume_key)
1785 return cd->volume_key->keylength;
1787 if (isLUKS(cd->type))
1788 return cd->hdr.keyBytes;
1790 if (isLOOPAES(cd->type))
1791 return cd->loopaes_key_size;
1796 uint64_t crypt_get_data_offset(struct crypt_device *cd)
1798 if (isPLAIN(cd->type))
1799 return cd->plain_hdr.offset;
1801 if (isLUKS(cd->type))
1802 return cd->hdr.payloadOffset;
1804 if (isLOOPAES(cd->type))
1805 return cd->loopaes_hdr.offset;
1810 uint64_t crypt_get_iv_offset(struct crypt_device *cd)
1812 if (isPLAIN(cd->type))
1813 return cd->plain_hdr.skip;
1815 if (isLUKS(cd->type))
1818 if (isLOOPAES(cd->type))
1819 return cd->loopaes_hdr.skip;
1824 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
1826 if (!isLUKS(cd->type)) {
1827 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1828 return CRYPT_SLOT_INVALID;
1831 return LUKS_keyslot_info(&cd->hdr, keyslot);
1834 int crypt_keyslot_max(const char *type)
1836 if (type && isLUKS(type))
1837 return LUKS_NUMKEYS;
1842 const char *crypt_get_type(struct crypt_device *cd)
1847 int crypt_get_active_device(struct crypt_device *cd __attribute__((unused)),
1849 struct crypt_active_device *cad)
1851 struct crypt_dm_active_device dmd;
1854 r = dm_query_device(name, 0, &dmd);
1858 cad->offset = dmd.offset;
1859 cad->iv_offset = dmd.iv_offset;
1860 cad->size = dmd.size;
1861 cad->flags = dmd.flags;