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,
223 char *dm_cipher = NULL;
224 struct crypt_dm_active_device dmd = {
225 .device = crypt_get_device_name(cd),
227 .uuid = crypt_get_uuid(cd),
229 .offset = crypt_get_data_offset(cd),
230 .iv_offset = iv_offset,
235 r = device_check_and_adjust(cd, dmd.device,
236 (dmd.flags & CRYPT_ACTIVATE_SHARED) ? DEV_SHARED : DEV_EXCL,
237 &dmd.size, &dmd.offset, &flags);
241 if (crypt_get_cipher_mode(cd))
242 r = asprintf(&dm_cipher, "%s-%s", crypt_get_cipher(cd), crypt_get_cipher_mode(cd));
244 r = asprintf(&dm_cipher, "%s", crypt_get_cipher(cd));
248 dmd.cipher = dm_cipher;
249 log_dbg("Trying to activate PLAIN device %s using cipher %s.", name, dmd.cipher);
251 r = dm_create_device(name, CRYPT_PLAIN, &dmd, 0);
254 if (!cd->plain_uuid && dm_query_device(name, DM_ACTIVE_UUID, &dmd) >= 0)
255 cd->plain_uuid = (char*)dmd.uuid;
261 int crypt_confirm(struct crypt_device *cd, const char *msg)
263 if (!cd || !cd->confirm)
266 return cd->confirm(msg, cd->confirm_usrptr);
269 static int key_from_terminal(struct crypt_device *cd, char *msg, char **key,
270 size_t *key_len, int force_verify)
276 if(!msg && asprintf(&prompt, _("Enter passphrase for %s: "),
277 cd->backing_file ?: cd->device) < 0)
284 *key = crypt_safe_alloc(DEFAULT_PASSPHRASE_SIZE_MAX);
289 r = cd->password(msg, *key, DEFAULT_PASSPHRASE_SIZE_MAX,
290 cd->password_usrptr);
292 crypt_safe_free(*key);
297 r = crypt_get_key(msg, key, key_len, 0, NULL, cd->timeout,
298 (force_verify || cd->password_verify), cd);
301 return (r < 0) ? r: 0;
304 static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslot,
305 struct volume_key **vk)
307 char *passphrase_read = NULL;
308 size_t passphrase_size_read;
309 int r = -EINVAL, eperm = 0, tries = cd->tries;
313 crypt_free_volume_key(*vk);
316 r = key_from_terminal(cd, NULL, &passphrase_read,
317 &passphrase_size_read, 0);
321 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
322 passphrase_size_read, &cd->hdr, vk, cd);
325 crypt_safe_free(passphrase_read);
326 passphrase_read = NULL;
327 } while (r == -EPERM && (--tries > 0));
330 crypt_free_volume_key(*vk);
333 /* Report wrong passphrase if at least one try failed */
334 if (eperm && r == -EPIPE)
338 crypt_safe_free(passphrase_read);
342 static int key_from_file(struct crypt_device *cd, char *msg,
343 char **key, size_t *key_len,
344 const char *key_file, size_t key_size)
346 return crypt_get_key(msg, key, key_len, key_size, key_file,
350 void crypt_set_log_callback(struct crypt_device *cd,
351 void (*log)(int level, const char *msg, void *usrptr),
358 cd->log_usrptr = usrptr;
362 void crypt_set_confirm_callback(struct crypt_device *cd,
363 int (*confirm)(const char *msg, void *usrptr),
366 cd->confirm = confirm;
367 cd->confirm_usrptr = usrptr;
370 void crypt_set_password_callback(struct crypt_device *cd,
371 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
374 cd->password = password;
375 cd->password_usrptr = usrptr;
378 void crypt_get_error(char *buf, size_t size)
380 const char *error = get_error();
382 if (!buf || size < 1)
385 strncpy(buf, error, size - 1);
386 buf[size - 1] = '\0';
392 const char *crypt_get_dir(void)
397 int crypt_init(struct crypt_device **cd, const char *device)
399 struct crypt_device *h = NULL;
405 log_dbg("Allocating crypt device %s context.", device);
407 if (!(h = malloc(sizeof(struct crypt_device))))
410 memset(h, 0, sizeof(*h));
414 r = device_ready(NULL, device, O_RDONLY);
416 h->device = crypt_loop_get_device();
417 log_dbg("Not a block device, %s%s.",
418 h->device ? "using free loop device " :
419 "no free loop device found",
422 log_err(NULL, _("Cannot find a free loopback device.\n"));
427 /* Keep the loop open, dettached on last close. */
428 h->loop_fd = crypt_loop_attach(h->device, device, 0, 1, &readonly);
429 if (h->loop_fd == -1) {
430 log_err(NULL, _("Attaching loopback device failed "
431 "(loop device with autoclear flag is required).\n"));
436 h->backing_file = crypt_loop_backing_file(h->device);
437 r = device_ready(NULL, h->device, O_RDONLY);
445 if (!h->device && device && !(h->device = strdup(device))) {
450 if (dm_init(h, 1) < 0) {
455 h->iteration_time = 1000;
456 h->password_verify = 0;
458 h->rng_type = crypt_random_default_key_rng();
464 if (h->loop_fd != -1)
467 free(h->backing_file);
473 int crypt_init_by_name(struct crypt_device **cd, const char *name)
475 crypt_status_info ci;
476 struct crypt_dm_active_device dmd;
477 char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
481 log_dbg("Allocating crypt device context by device %s.", name);
483 ci = crypt_status(NULL, name);
484 if (ci == CRYPT_INVALID)
487 if (ci < CRYPT_ACTIVE) {
488 log_err(NULL, _("Device %s is not active.\n"), name);
492 r = dm_query_device(name, DM_ACTIVE_ALL, &dmd);
497 r = crypt_init(cd, dmd.device);
499 /* Underlying device disappeared but mapping still active */
500 if (!dmd.device || r == -ENOTBLK)
501 log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
504 /* Underlying device is not readable but crypt mapping exists */
506 free((char*)dmd.device);
508 r = crypt_init(cd, NULL);
514 /* Try to initialise basic parameters from active device */
516 if (!(*cd)->backing_file && dmd.device && crypt_loop_device(dmd.device) &&
517 !((*cd)->backing_file = crypt_loop_backing_file(dmd.device))) {
523 if (!strncmp(CRYPT_PLAIN, dmd.uuid, sizeof(CRYPT_PLAIN)-1)) {
524 (*cd)->type = strdup(CRYPT_PLAIN);
525 (*cd)->plain_uuid = strdup(dmd.uuid);
526 (*cd)->plain_hdr.hash = NULL; /* no way to get this */
527 (*cd)->plain_hdr.offset = dmd.offset;
528 (*cd)->plain_hdr.skip = dmd.iv_offset;
529 (*cd)->volume_key = dmd.vk;
532 r = crypt_parse_name_and_mode(dmd.cipher, cipher, NULL, cipher_mode);
534 (*cd)->plain_cipher = strdup(cipher);
535 (*cd)->plain_cipher_mode = strdup(cipher_mode);
537 } else if (!strncmp(CRYPT_LOOPAES, dmd.uuid, sizeof(CRYPT_LOOPAES)-1)) {
538 (*cd)->type = strdup(CRYPT_LOOPAES);
539 (*cd)->loopaes_uuid = strdup(dmd.uuid);
540 (*cd)->loopaes_hdr.offset = dmd.offset;
542 r = crypt_parse_name_and_mode(dmd.cipher, cipher,
543 &key_nums, cipher_mode);
545 (*cd)->loopaes_cipher = strdup(cipher);
546 (*cd)->loopaes_cipher_mode = strdup(cipher_mode);
547 /* version 3 uses last key for IV */
548 if (dmd.vk->keylength % key_nums)
550 (*cd)->loopaes_key_size = dmd.vk->keylength / key_nums;
552 } else if (!strncmp(CRYPT_LUKS1, dmd.uuid, sizeof(CRYPT_LUKS1)-1)) {
554 if (crypt_load(*cd, CRYPT_LUKS1, NULL) < 0 ||
555 crypt_volume_key_verify(*cd, dmd.vk->key, dmd.vk->keylength) < 0) {
556 log_dbg("LUKS device header does not match active device.");
560 (*cd)->volume_key = dmd.vk;
565 log_dbg("Active device has no UUID set, some parameters are not set.");
572 crypt_free_volume_key(dmd.vk);
573 free((char*)dmd.device);
574 free((char*)dmd.cipher);
575 free((char*)dmd.uuid);
579 static int _crypt_format_plain(struct crypt_device *cd,
581 const char *cipher_mode,
583 size_t volume_key_size,
584 struct crypt_params_plain *params)
586 if (!cipher || !cipher_mode) {
587 log_err(cd, _("Invalid plain crypt parameters.\n"));
591 if (volume_key_size > 1024) {
592 log_err(cd, _("Invalid key size.\n"));
596 cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
600 cd->plain_cipher = strdup(cipher);
601 cd->plain_cipher_mode = strdup(cipher_mode);
604 cd->plain_uuid = strdup(uuid);
606 if (params && params->hash)
607 cd->plain_hdr.hash = strdup(params->hash);
609 cd->plain_hdr.offset = params ? params->offset : 0;
610 cd->plain_hdr.skip = params ? params->skip : 0;
611 cd->plain_hdr.size = params ? params->size : 0;
613 if (!cd->plain_cipher || !cd->plain_cipher_mode)
619 static int _crypt_format_luks1(struct crypt_device *cd,
621 const char *cipher_mode,
623 const char *volume_key,
624 size_t volume_key_size,
625 struct crypt_params_luks1 *params)
628 unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
629 unsigned long alignment_offset = 0;
632 log_err(cd, _("Can't format LUKS without device.\n"));
637 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
640 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
645 if (params && params->data_alignment)
646 required_alignment = params->data_alignment * SECTOR_SIZE;
648 get_topology_alignment(cd->device, &required_alignment,
649 &alignment_offset, DEFAULT_DISK_ALIGNMENT);
651 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
652 (params && params->hash) ? params->hash : "sha1",
654 required_alignment / SECTOR_SIZE,
655 alignment_offset / SECTOR_SIZE,
656 cd->iteration_time, &cd->PBKDF2_per_sec, cd);
660 /* Wipe first 8 sectors - fs magic numbers etc. */
661 r = wipe_device_header(cd->device, 8);
664 log_err(cd, _("Cannot format device %s which is still in use.\n"),
667 log_err(cd, _("Cannot wipe header on device %s.\n"),
673 r = LUKS_write_phdr(cd->device, &cd->hdr, cd);
678 static int _crypt_format_loopaes(struct crypt_device *cd,
681 size_t volume_key_size,
682 struct crypt_params_loopaes *params)
685 log_err(cd, _("Can't format LOOPAES without device.\n"));
689 if (volume_key_size > 1024) {
690 log_err(cd, _("Invalid key size.\n"));
694 cd->loopaes_key_size = volume_key_size;
696 cd->loopaes_cipher = strdup(cipher ?: DEFAULT_LOOPAES_CIPHER);
699 cd->loopaes_uuid = strdup(uuid);
701 if (params && params->hash)
702 cd->loopaes_hdr.hash = strdup(params->hash);
704 cd->loopaes_hdr.offset = params ? params->offset : 0;
705 cd->loopaes_hdr.skip = params ? params->skip : 0;
710 int crypt_format(struct crypt_device *cd,
713 const char *cipher_mode,
715 const char *volume_key,
716 size_t volume_key_size,
724 log_dbg("Formatting device %s as type %s.", cd->device ?: "(none)", type);
731 r = _crypt_format_plain(cd, cipher, cipher_mode,
732 uuid, volume_key_size, params);
733 else if (isLUKS(type))
734 r = _crypt_format_luks1(cd, cipher, cipher_mode,
735 uuid, volume_key, volume_key_size, params);
736 else if (isLOOPAES(type))
737 r = _crypt_format_loopaes(cd, cipher, uuid, volume_key_size, params);
739 /* FIXME: allow plugins here? */
740 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
744 if (!r && !(cd->type = strdup(type)))
748 crypt_free_volume_key(cd->volume_key);
749 cd->volume_key = NULL;
755 int crypt_load(struct crypt_device *cd,
756 const char *requested_type,
757 void *params __attribute__((unused)))
759 struct luks_phdr hdr;
762 log_dbg("Trying to load %s crypt type from device %s.",
763 requested_type ?: "any", cd->device ?: "(none)");
768 if (requested_type && !isLUKS(requested_type))
775 r = LUKS_read_phdr(cd->device, &hdr, 1, cd);
778 memcpy(&cd->hdr, &hdr, sizeof(hdr));
779 cd->type = strdup(CRYPT_LUKS1);
787 int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
789 struct crypt_dm_active_device dmd;
792 /* Device context type must be initialised */
793 if (!cd->type || !crypt_get_uuid(cd))
796 log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
798 r = dm_query_device(name, DM_ACTIVE_ALL, &dmd);
800 log_err(NULL, _("Device %s is not active.\n"), name);
809 r = device_check_and_adjust(cd, dmd.device, DEV_OK, &new_size, &dmd.offset, &dmd.flags);
813 if (new_size == dmd.size) {
814 log_dbg("Device has already requested size %" PRIu64
815 " sectors.", dmd.size);
819 r = dm_create_device(name, cd->type, &dmd, 1);
822 crypt_free_volume_key(dmd.vk);
823 free((char*)dmd.cipher);
824 free((char*)dmd.device);
825 free((char*)dmd.uuid);
830 int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
832 if (!isLUKS(cd->type)) {
833 log_err(cd, _("This operation is not supported for this device type.\n"));
837 if (uuid && !strncmp(uuid, cd->hdr.uuid, sizeof(cd->hdr.uuid))) {
838 log_dbg("UUID is the same as requested (%s) for device %s.",
844 log_dbg("Requested new UUID change to %s for %s.", uuid, cd->device);
846 log_dbg("Requested new UUID refresh for %s.", cd->device);
848 if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
851 return LUKS_hdr_uuid_set(cd->device, &cd->hdr, uuid, cd);
854 int crypt_header_backup(struct crypt_device *cd,
855 const char *requested_type,
856 const char *backup_file)
860 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
867 log_dbg("Requested header backup of device %s (%s) to "
868 "file %s.", cd->device, requested_type, backup_file);
870 return LUKS_hdr_backup(backup_file, cd->device, &cd->hdr, cd);
873 int crypt_header_restore(struct crypt_device *cd,
874 const char *requested_type,
875 const char *backup_file)
879 if (requested_type && !isLUKS(requested_type))
882 /* Some hash functions need initialized gcrypt library */
887 log_dbg("Requested header restore to device %s (%s) from "
888 "file %s.", cd->device, requested_type, backup_file);
890 return LUKS_hdr_restore(backup_file, cd->device, &cd->hdr, cd);
893 void crypt_free(struct crypt_device *cd)
896 log_dbg("Releasing crypt device %s context.", cd->device);
898 if (cd->loop_fd != -1)
902 crypt_free_volume_key(cd->volume_key);
905 free(cd->backing_file);
908 /* used in plain device only */
909 free((char*)cd->plain_hdr.hash);
910 free(cd->plain_cipher);
911 free(cd->plain_cipher_mode);
912 free(cd->plain_uuid);
914 /* used in loop-AES device only */
915 free((char*)cd->loopaes_hdr.hash);
916 free(cd->loopaes_cipher);
917 free(cd->loopaes_uuid);
923 int crypt_suspend(struct crypt_device *cd,
926 crypt_status_info ci;
929 log_dbg("Suspending volume %s.", name);
931 ci = crypt_status(NULL, name);
932 if (ci < CRYPT_ACTIVE) {
933 log_err(cd, _("Volume %s is not active.\n"), name);
937 if (!cd && dm_init(NULL, 1) < 0)
940 r = dm_status_suspended(name);
945 log_err(cd, _("Volume %s is already suspended.\n"), name);
950 r = dm_suspend_and_wipe_key(name);
952 log_err(cd, "Suspend is not supported for device %s.\n", name);
954 log_err(cd, "Error during suspending device %s.\n", name);
961 int crypt_resume_by_passphrase(struct crypt_device *cd,
964 const char *passphrase,
965 size_t passphrase_size)
967 struct volume_key *vk = NULL;
970 log_dbg("Resuming volume %s.", name);
972 if (!isLUKS(cd->type)) {
973 log_err(cd, _("This operation is supported only for LUKS device.\n"));
978 r = dm_status_suspended(name);
983 log_err(cd, _("Volume %s is not suspended.\n"), name);
988 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
989 passphrase_size, &cd->hdr, &vk, cd);
991 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
995 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
997 log_err(cd, "Resume is not supported for device %s.\n", name);
999 log_err(cd, "Error during resuming device %s.\n", name);
1003 crypt_free_volume_key(vk);
1004 return r < 0 ? r : keyslot;
1007 int crypt_resume_by_keyfile(struct crypt_device *cd,
1010 const char *keyfile,
1011 size_t keyfile_size)
1013 struct volume_key *vk = NULL;
1014 char *passphrase_read = NULL;
1015 size_t passphrase_size_read;
1018 log_dbg("Resuming volume %s.", name);
1020 if (!isLUKS(cd->type)) {
1021 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1026 r = dm_status_suspended(name);
1031 log_err(cd, _("Volume %s is not suspended.\n"), name);
1038 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1039 &passphrase_size_read, keyfile, keyfile_size);
1043 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1044 passphrase_size_read, &cd->hdr, &vk, cd);
1049 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1051 log_err(cd, "Error during resuming device %s.\n", name);
1053 crypt_safe_free(passphrase_read);
1054 crypt_free_volume_key(vk);
1055 return r < 0 ? r : keyslot;
1058 // slot manipulation
1059 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1060 int keyslot, // -1 any
1061 const char *passphrase, // NULL -> terminal
1062 size_t passphrase_size,
1063 const char *new_passphrase, // NULL -> terminal
1064 size_t new_passphrase_size)
1066 struct volume_key *vk = NULL;
1067 char *password = NULL, *new_password = NULL;
1068 size_t passwordLen, new_passwordLen;
1071 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1072 "new passphrase %sprovided.",
1073 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1075 if (!isLUKS(cd->type)) {
1076 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1080 r = keyslot_verify_or_find_empty(cd, &keyslot);
1084 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1085 /* No slots used, try to use pre-generated key in header */
1086 if (cd->volume_key) {
1087 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1088 r = vk ? 0 : -ENOMEM;
1090 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1093 } else if (passphrase) {
1094 /* Passphrase provided, use it to unlock existing keyslot */
1095 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, passphrase,
1096 passphrase_size, &cd->hdr, &vk, cd);
1098 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1099 r = key_from_terminal(cd, _("Enter any passphrase: "),
1100 &password, &passwordLen, 0);
1104 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
1105 passwordLen, &cd->hdr, &vk, cd);
1106 crypt_safe_free(password);
1112 if (new_passphrase) {
1113 new_password = (char *)new_passphrase;
1114 new_passwordLen = new_passphrase_size;
1116 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1117 &new_password, &new_passwordLen, 1);
1122 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1123 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1128 if (!new_passphrase)
1129 crypt_safe_free(new_password);
1130 crypt_free_volume_key(vk);
1131 return r ?: keyslot;
1134 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1136 const char *keyfile,
1137 size_t keyfile_size,
1138 const char *new_keyfile,
1139 size_t new_keyfile_size)
1141 struct volume_key *vk = NULL;
1142 char *password = NULL; size_t passwordLen;
1143 char *new_password = NULL; size_t new_passwordLen;
1146 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1147 keyfile ?: "[none]", new_keyfile ?: "[none]");
1149 if (!isLUKS(cd->type)) {
1150 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1154 r = keyslot_verify_or_find_empty(cd, &keyslot);
1158 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1159 /* No slots used, try to use pre-generated key in header */
1160 if (cd->volume_key) {
1161 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1162 r = vk ? 0 : -ENOMEM;
1164 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1168 /* Read password from file of (if NULL) from terminal */
1170 r = key_from_file(cd, _("Enter any passphrase: "),
1171 &password, &passwordLen,
1172 keyfile, keyfile_size);
1174 r = key_from_terminal(cd, _("Enter any passphrase: "),
1175 &password, &passwordLen, 0);
1179 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password, passwordLen,
1187 r = key_from_file(cd, _("Enter new passphrase for key slot: "),
1188 &new_password, &new_passwordLen, new_keyfile,
1191 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1192 &new_password, &new_passwordLen, 1);
1196 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1197 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1199 crypt_safe_free(password);
1200 crypt_safe_free(new_password);
1201 crypt_free_volume_key(vk);
1202 return r < 0 ? r : keyslot;
1205 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1207 const char *volume_key,
1208 size_t volume_key_size,
1209 const char *passphrase,
1210 size_t passphrase_size)
1212 struct volume_key *vk = NULL;
1214 char *new_password = NULL; size_t new_passwordLen;
1216 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1218 if (!isLUKS(cd->type)) {
1219 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1224 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1225 else if (cd->volume_key)
1226 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1231 r = LUKS_verify_volume_key(&cd->hdr, vk);
1233 log_err(cd, _("Volume key does not match the volume.\n"));
1237 r = keyslot_verify_or_find_empty(cd, &keyslot);
1242 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1243 &new_password, &new_passwordLen, 1);
1246 passphrase = new_password;
1247 passphrase_size = new_passwordLen;
1250 r = LUKS_set_key(cd->device, keyslot, passphrase, passphrase_size,
1251 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1253 crypt_safe_free(new_password);
1254 crypt_free_volume_key(vk);
1255 return (r < 0) ? r : keyslot;
1258 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1260 crypt_keyslot_info ki;
1262 log_dbg("Destroying keyslot %d.", keyslot);
1264 if (!isLUKS(cd->type)) {
1265 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1269 ki = crypt_keyslot_status(cd, keyslot);
1270 if (ki == CRYPT_SLOT_INVALID) {
1271 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1275 if (ki == CRYPT_SLOT_INACTIVE) {
1276 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1280 return LUKS_del_key(cd->device, keyslot, &cd->hdr, cd);
1283 // activation/deactivation of device mapping
1284 int crypt_activate_by_passphrase(struct crypt_device *cd,
1287 const char *passphrase,
1288 size_t passphrase_size,
1291 crypt_status_info ci;
1292 struct volume_key *vk = NULL;
1293 char *read_passphrase = NULL;
1294 size_t passphraseLen = 0;
1297 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1298 name ? "Activating" : "Checking", name ?: "",
1299 keyslot, passphrase ? "" : "[none] ");
1302 ci = crypt_status(NULL, name);
1303 if (ci == CRYPT_INVALID)
1305 else if (ci >= CRYPT_ACTIVE) {
1306 log_err(cd, _("Device %s already exists.\n"), name);
1311 /* plain, use hashed passphrase */
1312 if (isPLAIN(cd->type)) {
1317 r = key_from_terminal(cd, NULL, &read_passphrase,
1321 passphrase = read_passphrase;
1322 passphrase_size = passphraseLen;
1325 r = process_key(cd, cd->plain_hdr.hash,
1326 cd->volume_key->keylength,
1327 passphrase, passphrase_size, &vk);
1331 r = PLAIN_activate(cd, name, vk,
1333 cd->plain_hdr.skip, flags);
1335 } else if (isLUKS(cd->type)) {
1336 /* provided passphrase, do not retry */
1338 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1339 passphrase_size, &cd->hdr, &vk, cd);
1341 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1346 r = LUKS1_activate(cd, name, vk, flags);
1351 crypt_safe_free(read_passphrase);
1352 crypt_free_volume_key(vk);
1354 return r < 0 ? r : keyslot;
1357 int crypt_activate_by_keyfile(struct crypt_device *cd,
1360 const char *keyfile,
1361 size_t keyfile_size,
1364 crypt_status_info ci;
1365 struct volume_key *vk = NULL;
1366 char *passphrase_read = NULL;
1367 size_t passphrase_size_read;
1368 unsigned int key_count = 0;
1371 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1372 name ?: "", keyslot, keyfile ?: "[none]");
1375 ci = crypt_status(NULL, name);
1376 if (ci == CRYPT_INVALID)
1378 else if (ci >= CRYPT_ACTIVE) {
1379 log_err(cd, _("Device %s already exists.\n"), name);
1387 if (isPLAIN(cd->type)) {
1391 r = key_from_file(cd, _("Enter passphrase: "),
1392 &passphrase_read, &passphrase_size_read,
1393 keyfile, keyfile_size);
1397 r = process_key(cd, cd->plain_hdr.hash,
1398 cd->volume_key->keylength,
1399 passphrase_read, passphrase_size_read, &vk);
1403 r = PLAIN_activate(cd, name, vk,
1405 cd->plain_hdr.skip, flags);
1406 } else if (isLUKS(cd->type)) {
1407 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1408 &passphrase_size_read, keyfile, keyfile_size);
1411 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1412 passphrase_size_read, &cd->hdr, &vk, cd);
1418 r = LUKS1_activate(cd, name, vk, flags);
1423 } else if (isLOOPAES(cd->type)) {
1424 r = key_from_file(cd, NULL, &passphrase_read, &passphrase_size_read,
1425 keyfile, keyfile_size);
1428 r = LOOPAES_parse_keyfile(cd, &vk, cd->loopaes_hdr.hash, &key_count,
1429 passphrase_read, passphrase_size_read);
1433 r = LOOPAES_activate(cd, name, cd->loopaes_cipher,
1435 cd->loopaes_hdr.skip,
1441 crypt_safe_free(passphrase_read);
1442 crypt_free_volume_key(vk);
1447 int crypt_activate_by_volume_key(struct crypt_device *cd,
1449 const char *volume_key,
1450 size_t volume_key_size,
1453 crypt_status_info ci;
1454 struct volume_key *vk = NULL;
1457 log_dbg("Activating volume %s by volume key.", name);
1460 ci = crypt_status(NULL, name);
1461 if (ci == CRYPT_INVALID)
1463 else if (ci >= CRYPT_ACTIVE) {
1464 log_err(cd, _("Device %s already exists.\n"), name);
1469 /* use key directly, no hash */
1470 if (isPLAIN(cd->type)) {
1474 if (!volume_key || !volume_key_size || !cd->volume_key ||
1475 volume_key_size != cd->volume_key->keylength) {
1476 log_err(cd, _("Incorrect volume key specified for plain device.\n"));
1480 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1484 r = PLAIN_activate(cd, name, vk,
1486 cd->plain_hdr.skip, flags);
1487 } else if (isLUKS(cd->type)) {
1488 /* If key is not provided, try to use internal key */
1490 if (!cd->volume_key) {
1491 log_err(cd, _("Volume key does not match the volume.\n"));
1494 volume_key_size = cd->volume_key->keylength;
1495 volume_key = cd->volume_key->key;
1498 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1501 r = LUKS_verify_volume_key(&cd->hdr, vk);
1504 log_err(cd, _("Volume key does not match the volume.\n"));
1507 r = LUKS1_activate(cd, name, vk, flags);
1509 log_err(cd, _("Device type is not properly initialised.\n"));
1511 crypt_free_volume_key(vk);
1516 int crypt_deactivate(struct crypt_device *cd, const char *name)
1523 log_dbg("Deactivating volume %s.", name);
1525 if (!cd && dm_init(NULL, 1) < 0)
1528 switch (crypt_status(cd, name)) {
1530 r = dm_remove_device(name, 0, 0);
1533 log_err(cd, _("Device %s is busy.\n"), name);
1536 case CRYPT_INACTIVE:
1537 log_err(cd, _("Device %s is not active.\n"), name);
1541 log_err(cd, _("Invalid device %s.\n"), name);
1551 int crypt_volume_key_get(struct crypt_device *cd,
1554 size_t *volume_key_size,
1555 const char *passphrase,
1556 size_t passphrase_size)
1558 struct volume_key *vk = NULL;
1562 key_len = crypt_get_volume_key_size(cd);
1563 if (key_len > *volume_key_size) {
1564 log_err(cd, _("Volume key buffer too small.\n"));
1568 if (isPLAIN(cd->type) && cd->plain_hdr.hash) {
1569 r = process_key(cd, cd->plain_hdr.hash, key_len,
1570 passphrase, passphrase_size, &vk);
1572 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
1573 } else if (isLUKS(cd->type)) {
1574 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1575 passphrase_size, &cd->hdr, &vk, cd);
1578 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
1581 memcpy(volume_key, vk->key, vk->keylength);
1582 *volume_key_size = vk->keylength;
1585 crypt_free_volume_key(vk);
1589 int crypt_volume_key_verify(struct crypt_device *cd,
1590 const char *volume_key,
1591 size_t volume_key_size)
1593 struct volume_key *vk;
1596 if (!isLUKS(cd->type)) {
1597 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1601 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1605 r = LUKS_verify_volume_key(&cd->hdr, vk);
1608 log_err(cd, _("Volume key does not match the volume.\n"));
1610 crypt_free_volume_key(vk);
1615 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
1617 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
1618 cd->timeout = timeout_sec;
1621 void crypt_set_password_retry(struct crypt_device *cd, int tries)
1623 log_dbg("Password retry count set to %d.", tries);
1627 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
1629 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
1630 cd->iteration_time = iteration_time_ms;
1633 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
1635 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
1636 cd->password_verify = password_verify ? 1 : 0;
1639 void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
1642 case CRYPT_RNG_URANDOM:
1643 case CRYPT_RNG_RANDOM:
1644 log_dbg("RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
1645 cd->rng_type = rng_type;
1649 int crypt_get_rng_type(struct crypt_device *cd)
1654 return cd->rng_type;
1657 int crypt_memory_lock(struct crypt_device *cd, int lock)
1659 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
1663 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
1667 if (!cd && dm_init(NULL, 1) < 0)
1668 return CRYPT_INVALID;
1670 r = dm_status_device(name);
1675 if (r < 0 && r != -ENODEV)
1676 return CRYPT_INVALID;
1679 return CRYPT_ACTIVE;
1684 return CRYPT_INACTIVE;
1687 static void hexprintICB(struct crypt_device *cd, char *d, int n)
1690 for(i = 0; i < n; i++)
1691 log_std(cd, "%02hhx ", (char)d[i]);
1694 int crypt_dump(struct crypt_device *cd)
1697 if (!isLUKS(cd->type)) { //FIXME
1698 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1702 log_std(cd, "LUKS header information for %s\n\n", cd->device);
1703 log_std(cd, "Version: \t%d\n", cd->hdr.version);
1704 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
1705 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
1706 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec);
1707 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
1708 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
1709 log_std(cd, "MK digest: \t");
1710 hexprintICB(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE);
1712 log_std(cd, "MK salt: \t");
1713 hexprintICB(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2);
1714 log_std(cd, "\n \t");
1715 hexprintICB(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
1717 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
1718 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
1719 for(i = 0; i < LUKS_NUMKEYS; i++) {
1720 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
1721 log_std(cd, "Key Slot %d: ENABLED\n",i);
1722 log_std(cd, "\tIterations: \t%d\n",
1723 cd->hdr.keyblock[i].passwordIterations);
1724 log_std(cd, "\tSalt: \t");
1725 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt,
1727 log_std(cd, "\n\t \t");
1728 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt +
1729 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
1732 log_std(cd, "\tKey material offset:\t%d\n",
1733 cd->hdr.keyblock[i].keyMaterialOffset);
1734 log_std(cd, "\tAF stripes: \t%d\n",
1735 cd->hdr.keyblock[i].stripes);
1738 log_std(cd, "Key Slot %d: DISABLED\n", i);
1744 const char *crypt_get_cipher(struct crypt_device *cd)
1746 if (isPLAIN(cd->type))
1747 return cd->plain_cipher;
1749 if (isLUKS(cd->type))
1750 return cd->hdr.cipherName;
1752 if (isLOOPAES(cd->type))
1753 return cd->loopaes_cipher;
1758 const char *crypt_get_cipher_mode(struct crypt_device *cd)
1760 if (isPLAIN(cd->type))
1761 return cd->plain_cipher_mode;
1763 if (isLUKS(cd->type))
1764 return cd->hdr.cipherMode;
1766 if (isLOOPAES(cd->type))
1767 return cd->loopaes_cipher_mode;
1772 const char *crypt_get_uuid(struct crypt_device *cd)
1774 if (isLUKS(cd->type))
1775 return cd->hdr.uuid;
1777 if (isPLAIN(cd->type))
1778 return cd->plain_uuid;
1780 if (isLOOPAES(cd->type))
1781 return cd->loopaes_uuid;
1786 const char *crypt_get_device_name(struct crypt_device *cd)
1791 int crypt_get_volume_key_size(struct crypt_device *cd)
1793 if (isPLAIN(cd->type) && cd->volume_key)
1794 return cd->volume_key->keylength;
1796 if (isLUKS(cd->type))
1797 return cd->hdr.keyBytes;
1799 if (isLOOPAES(cd->type))
1800 return cd->loopaes_key_size;
1805 uint64_t crypt_get_data_offset(struct crypt_device *cd)
1807 if (isPLAIN(cd->type))
1808 return cd->plain_hdr.offset;
1810 if (isLUKS(cd->type))
1811 return cd->hdr.payloadOffset;
1813 if (isLOOPAES(cd->type))
1814 return cd->loopaes_hdr.offset;
1819 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
1821 if (!isLUKS(cd->type)) {
1822 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1823 return CRYPT_SLOT_INVALID;
1826 return LUKS_keyslot_info(&cd->hdr, keyslot);
1829 int crypt_keyslot_max(const char *type)
1831 if (type && isLUKS(type))
1832 return LUKS_NUMKEYS;
1837 const char *crypt_get_type(struct crypt_device *cd)
1842 int crypt_get_active_device(struct crypt_device *cd __attribute__((unused)),
1844 struct crypt_active_device *cad)
1846 struct crypt_dm_active_device dmd;
1849 r = dm_query_device(name, 0, &dmd);
1853 cad->offset = dmd.offset;
1854 cad->iv_offset = dmd.iv_offset;
1855 cad->size = dmd.size;
1856 cad->flags = dmd.flags;