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 .key_size = vk->keylength,
230 .offset = crypt_get_data_offset(cd),
231 .iv_offset = iv_offset,
236 r = device_check_and_adjust(cd, dmd.device,
237 (dmd.flags & CRYPT_ACTIVATE_SHARED) ? DEV_SHARED : DEV_EXCL,
238 &dmd.size, &dmd.offset, &flags);
242 if (crypt_get_cipher_mode(cd))
243 r = asprintf(&dm_cipher, "%s-%s", crypt_get_cipher(cd), crypt_get_cipher_mode(cd));
245 r = asprintf(&dm_cipher, "%s", crypt_get_cipher(cd));
249 dmd.cipher = dm_cipher;
250 log_dbg("Trying to activate PLAIN device %s using cipher %s.", name, dmd.cipher);
252 r = dm_create_device(name, CRYPT_PLAIN, &dmd, 0);
255 if (!cd->plain_uuid && dm_query_device(name, DM_ACTIVE_UUID, &dmd) >= 0)
256 cd->plain_uuid = (char*)dmd.uuid;
262 int crypt_confirm(struct crypt_device *cd, const char *msg)
264 if (!cd || !cd->confirm)
267 return cd->confirm(msg, cd->confirm_usrptr);
270 static int key_from_terminal(struct crypt_device *cd, char *msg, char **key,
271 size_t *key_len, int force_verify)
277 if(!msg && asprintf(&prompt, _("Enter passphrase for %s: "),
278 cd->backing_file ?: cd->device) < 0)
285 *key = crypt_safe_alloc(DEFAULT_PASSPHRASE_SIZE_MAX);
290 r = cd->password(msg, *key, DEFAULT_PASSPHRASE_SIZE_MAX,
291 cd->password_usrptr);
293 crypt_safe_free(*key);
298 r = crypt_get_key(msg, key, key_len, 0, NULL, cd->timeout,
299 (force_verify || cd->password_verify), cd);
302 return (r < 0) ? r: 0;
305 static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslot,
306 struct volume_key **vk)
308 char *passphrase_read = NULL;
309 size_t passphrase_size_read;
310 int r = -EINVAL, eperm = 0, tries = cd->tries;
314 crypt_free_volume_key(*vk);
317 r = key_from_terminal(cd, NULL, &passphrase_read,
318 &passphrase_size_read, 0);
322 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
323 passphrase_size_read, &cd->hdr, vk, cd);
326 crypt_safe_free(passphrase_read);
327 passphrase_read = NULL;
328 } while (r == -EPERM && (--tries > 0));
331 crypt_free_volume_key(*vk);
334 /* Report wrong passphrase if at least one try failed */
335 if (eperm && r == -EPIPE)
339 crypt_safe_free(passphrase_read);
343 static int key_from_file(struct crypt_device *cd, char *msg,
344 char **key, size_t *key_len,
345 const char *key_file, size_t key_size)
347 return crypt_get_key(msg, key, key_len, key_size, key_file,
351 void crypt_set_log_callback(struct crypt_device *cd,
352 void (*log)(int level, const char *msg, void *usrptr),
359 cd->log_usrptr = usrptr;
363 void crypt_set_confirm_callback(struct crypt_device *cd,
364 int (*confirm)(const char *msg, void *usrptr),
367 cd->confirm = confirm;
368 cd->confirm_usrptr = usrptr;
371 void crypt_set_password_callback(struct crypt_device *cd,
372 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
375 cd->password = password;
376 cd->password_usrptr = usrptr;
379 void crypt_get_error(char *buf, size_t size)
381 const char *error = get_error();
383 if (!buf || size < 1)
386 strncpy(buf, error, size - 1);
387 buf[size - 1] = '\0';
393 const char *crypt_get_dir(void)
398 int crypt_init(struct crypt_device **cd, const char *device)
400 struct crypt_device *h = NULL;
406 log_dbg("Allocating crypt device %s context.", device);
408 if (!(h = malloc(sizeof(struct crypt_device))))
411 memset(h, 0, sizeof(*h));
415 r = device_ready(NULL, device, O_RDONLY);
417 h->device = crypt_loop_get_device();
418 log_dbg("Not a block device, %s%s.",
419 h->device ? "using free loop device " :
420 "no free loop device found",
423 log_err(NULL, _("Cannot find a free loopback device.\n"));
428 /* Keep the loop open, dettached on last close. */
429 h->loop_fd = crypt_loop_attach(h->device, device, 0, 1, &readonly);
430 if (h->loop_fd == -1) {
431 log_err(NULL, _("Attaching loopback device failed "
432 "(loop device with autoclear flag is required).\n"));
437 h->backing_file = crypt_loop_backing_file(h->device);
438 r = device_ready(NULL, h->device, O_RDONLY);
446 if (!h->device && device && !(h->device = strdup(device))) {
451 if (dm_init(h, 1) < 0) {
456 h->iteration_time = 1000;
457 h->password_verify = 0;
459 h->rng_type = crypt_random_default_key_rng();
465 if (h->loop_fd != -1)
468 free(h->backing_file);
474 int crypt_init_by_name(struct crypt_device **cd, const char *name)
476 crypt_status_info ci;
477 struct crypt_dm_active_device dmd;
478 char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
482 log_dbg("Allocating crypt device context by device %s.", name);
484 ci = crypt_status(NULL, name);
485 if (ci == CRYPT_INVALID)
488 if (ci < CRYPT_ACTIVE) {
489 log_err(NULL, _("Device %s is not active.\n"), name);
493 r = dm_query_device(name, DM_ACTIVE_ALL, &dmd);
498 r = crypt_init(cd, dmd.device);
500 /* Underlying device disappeared but mapping still active */
501 if (!dmd.device || r == -ENOTBLK)
502 log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
505 /* Underlying device is not readable but crypt mapping exists */
507 free((char*)dmd.device);
509 r = crypt_init(cd, NULL);
515 /* Try to initialise basic parameters from active device */
517 if (!(*cd)->backing_file && dmd.device && crypt_loop_device(dmd.device) &&
518 !((*cd)->backing_file = crypt_loop_backing_file(dmd.device))) {
524 if (!strncmp(CRYPT_PLAIN, dmd.uuid, sizeof(CRYPT_PLAIN)-1)) {
525 (*cd)->type = strdup(CRYPT_PLAIN);
526 (*cd)->plain_uuid = strdup(dmd.uuid);
527 (*cd)->plain_hdr.hash = NULL; /* no way to get this */
528 (*cd)->plain_hdr.offset = dmd.offset;
529 (*cd)->plain_hdr.skip = dmd.iv_offset;
530 (*cd)->volume_key = crypt_alloc_volume_key(dmd.key_size, dmd.key);
531 if (!(*cd)->volume_key) {
536 r = crypt_parse_name_and_mode(dmd.cipher, cipher, NULL, cipher_mode);
538 (*cd)->plain_cipher = strdup(cipher);
539 (*cd)->plain_cipher_mode = strdup(cipher_mode);
541 } else if (!strncmp(CRYPT_LOOPAES, dmd.uuid, sizeof(CRYPT_LOOPAES)-1)) {
542 (*cd)->type = strdup(CRYPT_LOOPAES);
543 (*cd)->loopaes_uuid = strdup(dmd.uuid);
544 (*cd)->loopaes_hdr.offset = dmd.offset;
546 r = crypt_parse_name_and_mode(dmd.cipher, cipher,
547 &key_nums, cipher_mode);
549 (*cd)->loopaes_cipher = strdup(cipher);
550 (*cd)->loopaes_cipher_mode = strdup(cipher_mode);
551 /* version 3 uses last key for IV */
552 if (dmd.key_size % key_nums)
554 (*cd)->loopaes_key_size = dmd.key_size / key_nums;
556 } else if (!strncmp(CRYPT_LUKS1, dmd.uuid, sizeof(CRYPT_LUKS1)-1)) {
558 if (crypt_load(*cd, CRYPT_LUKS1, NULL) < 0 ||
559 crypt_volume_key_verify(*cd, dmd.key, dmd.key_size) < 0) {
560 log_dbg("LUKS device header does not match active device.");
564 (*cd)->volume_key = crypt_alloc_volume_key(dmd.key_size, dmd.key);
565 if (!(*cd)->volume_key) {
572 log_dbg("Active device has no UUID set, some parameters are not set.");
579 crypt_safe_free(dmd.key);
580 free((char*)dmd.device);
581 free((char*)dmd.cipher);
582 free((char*)dmd.uuid);
586 static int _crypt_format_plain(struct crypt_device *cd,
588 const char *cipher_mode,
590 size_t volume_key_size,
591 struct crypt_params_plain *params)
593 if (!cipher || !cipher_mode) {
594 log_err(cd, _("Invalid plain crypt parameters.\n"));
598 if (volume_key_size > 1024) {
599 log_err(cd, _("Invalid key size.\n"));
603 cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
607 cd->plain_cipher = strdup(cipher);
608 cd->plain_cipher_mode = strdup(cipher_mode);
611 cd->plain_uuid = strdup(uuid);
613 if (params && params->hash)
614 cd->plain_hdr.hash = strdup(params->hash);
616 cd->plain_hdr.offset = params ? params->offset : 0;
617 cd->plain_hdr.skip = params ? params->skip : 0;
618 cd->plain_hdr.size = params ? params->size : 0;
620 if (!cd->plain_cipher || !cd->plain_cipher_mode)
626 static int _crypt_format_luks1(struct crypt_device *cd,
628 const char *cipher_mode,
630 const char *volume_key,
631 size_t volume_key_size,
632 struct crypt_params_luks1 *params)
635 unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
636 unsigned long alignment_offset = 0;
639 log_err(cd, _("Can't format LUKS without device.\n"));
644 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
647 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
652 if (params && params->data_alignment)
653 required_alignment = params->data_alignment * SECTOR_SIZE;
655 get_topology_alignment(cd->device, &required_alignment,
656 &alignment_offset, DEFAULT_DISK_ALIGNMENT);
658 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
659 (params && params->hash) ? params->hash : "sha1",
661 required_alignment / SECTOR_SIZE,
662 alignment_offset / SECTOR_SIZE,
663 cd->iteration_time, &cd->PBKDF2_per_sec, cd);
667 /* Wipe first 8 sectors - fs magic numbers etc. */
668 r = wipe_device_header(cd->device, 8);
671 log_err(cd, _("Cannot format device %s which is still in use.\n"),
674 log_err(cd, _("Cannot wipe header on device %s.\n"),
680 r = LUKS_write_phdr(cd->device, &cd->hdr, cd);
685 static int _crypt_format_loopaes(struct crypt_device *cd,
688 size_t volume_key_size,
689 struct crypt_params_loopaes *params)
692 log_err(cd, _("Can't format LOOPAES without device.\n"));
696 if (volume_key_size > 1024) {
697 log_err(cd, _("Invalid key size.\n"));
701 cd->loopaes_key_size = volume_key_size;
703 cd->loopaes_cipher = strdup(cipher ?: DEFAULT_LOOPAES_CIPHER);
706 cd->loopaes_uuid = strdup(uuid);
708 if (params && params->hash)
709 cd->loopaes_hdr.hash = strdup(params->hash);
711 cd->loopaes_hdr.offset = params ? params->offset : 0;
712 cd->loopaes_hdr.skip = params ? params->skip : 0;
717 int crypt_format(struct crypt_device *cd,
720 const char *cipher_mode,
722 const char *volume_key,
723 size_t volume_key_size,
731 log_dbg("Formatting device %s as type %s.", cd->device ?: "(none)", type);
738 r = _crypt_format_plain(cd, cipher, cipher_mode,
739 uuid, volume_key_size, params);
740 else if (isLUKS(type))
741 r = _crypt_format_luks1(cd, cipher, cipher_mode,
742 uuid, volume_key, volume_key_size, params);
743 else if (isLOOPAES(type))
744 r = _crypt_format_loopaes(cd, cipher, uuid, volume_key_size, params);
746 /* FIXME: allow plugins here? */
747 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
751 if (!r && !(cd->type = strdup(type)))
755 crypt_free_volume_key(cd->volume_key);
756 cd->volume_key = NULL;
762 int crypt_load(struct crypt_device *cd,
763 const char *requested_type,
764 void *params __attribute__((unused)))
766 struct luks_phdr hdr;
769 log_dbg("Trying to load %s crypt type from device %s.",
770 requested_type ?: "any", cd->device ?: "(none)");
775 if (requested_type && !isLUKS(requested_type))
782 r = LUKS_read_phdr(cd->device, &hdr, 1, cd);
785 memcpy(&cd->hdr, &hdr, sizeof(hdr));
786 cd->type = strdup(CRYPT_LUKS1);
794 int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
796 struct crypt_dm_active_device dmd;
799 /* Device context type must be initialised */
800 if (!cd->type || !crypt_get_uuid(cd))
803 log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
805 r = dm_query_device(name, DM_ACTIVE_ALL, &dmd);
807 log_err(NULL, _("Device %s is not active.\n"), name);
816 r = device_check_and_adjust(cd, dmd.device, DEV_OK, &new_size, &dmd.offset, &dmd.flags);
820 if (new_size == dmd.size) {
821 log_dbg("Device has already requested size %" PRIu64
822 " sectors.", dmd.size);
826 r = dm_create_device(name, cd->type, &dmd, 1);
829 crypt_safe_free(dmd.key);
830 free((char*)dmd.cipher);
831 free((char*)dmd.device);
832 free((char*)dmd.uuid);
837 int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
839 if (!isLUKS(cd->type)) {
840 log_err(cd, _("This operation is not supported for this device type.\n"));
844 if (uuid && !strncmp(uuid, cd->hdr.uuid, sizeof(cd->hdr.uuid))) {
845 log_dbg("UUID is the same as requested (%s) for device %s.",
851 log_dbg("Requested new UUID change to %s for %s.", uuid, cd->device);
853 log_dbg("Requested new UUID refresh for %s.", cd->device);
855 if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
858 return LUKS_hdr_uuid_set(cd->device, &cd->hdr, uuid, cd);
861 int crypt_header_backup(struct crypt_device *cd,
862 const char *requested_type,
863 const char *backup_file)
867 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
874 log_dbg("Requested header backup of device %s (%s) to "
875 "file %s.", cd->device, requested_type, backup_file);
877 return LUKS_hdr_backup(backup_file, cd->device, &cd->hdr, cd);
880 int crypt_header_restore(struct crypt_device *cd,
881 const char *requested_type,
882 const char *backup_file)
886 if (requested_type && !isLUKS(requested_type))
889 /* Some hash functions need initialized gcrypt library */
894 log_dbg("Requested header restore to device %s (%s) from "
895 "file %s.", cd->device, requested_type, backup_file);
897 return LUKS_hdr_restore(backup_file, cd->device, &cd->hdr, cd);
900 void crypt_free(struct crypt_device *cd)
903 log_dbg("Releasing crypt device %s context.", cd->device);
905 if (cd->loop_fd != -1)
909 crypt_free_volume_key(cd->volume_key);
912 free(cd->backing_file);
915 /* used in plain device only */
916 free((char*)cd->plain_hdr.hash);
917 free(cd->plain_cipher);
918 free(cd->plain_cipher_mode);
919 free(cd->plain_uuid);
921 /* used in loop-AES device only */
922 free((char*)cd->loopaes_hdr.hash);
923 free(cd->loopaes_cipher);
924 free(cd->loopaes_uuid);
930 int crypt_suspend(struct crypt_device *cd,
933 crypt_status_info ci;
936 log_dbg("Suspending volume %s.", name);
938 ci = crypt_status(NULL, name);
939 if (ci < CRYPT_ACTIVE) {
940 log_err(cd, _("Volume %s is not active.\n"), name);
944 if (!cd && dm_init(NULL, 1) < 0)
947 r = dm_status_suspended(name);
952 log_err(cd, _("Volume %s is already suspended.\n"), name);
957 r = dm_suspend_and_wipe_key(name);
959 log_err(cd, "Suspend is not supported for device %s.\n", name);
961 log_err(cd, "Error during suspending device %s.\n", name);
968 int crypt_resume_by_passphrase(struct crypt_device *cd,
971 const char *passphrase,
972 size_t passphrase_size)
974 struct volume_key *vk = NULL;
977 log_dbg("Resuming volume %s.", name);
979 if (!isLUKS(cd->type)) {
980 log_err(cd, _("This operation is supported only for LUKS device.\n"));
985 r = dm_status_suspended(name);
990 log_err(cd, _("Volume %s is not suspended.\n"), name);
995 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
996 passphrase_size, &cd->hdr, &vk, cd);
998 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1002 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1004 log_err(cd, "Resume is not supported for device %s.\n", name);
1006 log_err(cd, "Error during resuming device %s.\n", name);
1010 crypt_free_volume_key(vk);
1011 return r < 0 ? r : keyslot;
1014 int crypt_resume_by_keyfile(struct crypt_device *cd,
1017 const char *keyfile,
1018 size_t keyfile_size)
1020 struct volume_key *vk = NULL;
1021 char *passphrase_read = NULL;
1022 size_t passphrase_size_read;
1025 log_dbg("Resuming volume %s.", name);
1027 if (!isLUKS(cd->type)) {
1028 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1033 r = dm_status_suspended(name);
1038 log_err(cd, _("Volume %s is not suspended.\n"), name);
1045 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1046 &passphrase_size_read, keyfile, keyfile_size);
1050 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1051 passphrase_size_read, &cd->hdr, &vk, cd);
1056 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1058 log_err(cd, "Error during resuming device %s.\n", name);
1060 crypt_safe_free(passphrase_read);
1061 crypt_free_volume_key(vk);
1062 return r < 0 ? r : keyslot;
1065 // slot manipulation
1066 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1067 int keyslot, // -1 any
1068 const char *passphrase, // NULL -> terminal
1069 size_t passphrase_size,
1070 const char *new_passphrase, // NULL -> terminal
1071 size_t new_passphrase_size)
1073 struct volume_key *vk = NULL;
1074 char *password = NULL, *new_password = NULL;
1075 size_t passwordLen, new_passwordLen;
1078 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1079 "new passphrase %sprovided.",
1080 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1082 if (!isLUKS(cd->type)) {
1083 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1087 r = keyslot_verify_or_find_empty(cd, &keyslot);
1091 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1092 /* No slots used, try to use pre-generated key in header */
1093 if (cd->volume_key) {
1094 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1095 r = vk ? 0 : -ENOMEM;
1097 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1100 } else if (passphrase) {
1101 /* Passphrase provided, use it to unlock existing keyslot */
1102 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, passphrase,
1103 passphrase_size, &cd->hdr, &vk, cd);
1105 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1106 r = key_from_terminal(cd, _("Enter any passphrase: "),
1107 &password, &passwordLen, 0);
1111 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
1112 passwordLen, &cd->hdr, &vk, cd);
1113 crypt_safe_free(password);
1119 if (new_passphrase) {
1120 new_password = (char *)new_passphrase;
1121 new_passwordLen = new_passphrase_size;
1123 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1124 &new_password, &new_passwordLen, 1);
1129 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1130 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1135 if (!new_passphrase)
1136 crypt_safe_free(new_password);
1137 crypt_free_volume_key(vk);
1138 return r ?: keyslot;
1141 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1143 const char *keyfile,
1144 size_t keyfile_size,
1145 const char *new_keyfile,
1146 size_t new_keyfile_size)
1148 struct volume_key *vk = NULL;
1149 char *password = NULL; size_t passwordLen;
1150 char *new_password = NULL; size_t new_passwordLen;
1153 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1154 keyfile ?: "[none]", new_keyfile ?: "[none]");
1156 if (!isLUKS(cd->type)) {
1157 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1161 r = keyslot_verify_or_find_empty(cd, &keyslot);
1165 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1166 /* No slots used, try to use pre-generated key in header */
1167 if (cd->volume_key) {
1168 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1169 r = vk ? 0 : -ENOMEM;
1171 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1175 /* Read password from file of (if NULL) from terminal */
1177 r = key_from_file(cd, _("Enter any passphrase: "),
1178 &password, &passwordLen,
1179 keyfile, keyfile_size);
1181 r = key_from_terminal(cd, _("Enter any passphrase: "),
1182 &password, &passwordLen, 0);
1186 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password, passwordLen,
1194 r = key_from_file(cd, _("Enter new passphrase for key slot: "),
1195 &new_password, &new_passwordLen, new_keyfile,
1198 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1199 &new_password, &new_passwordLen, 1);
1203 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1204 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1206 crypt_safe_free(password);
1207 crypt_safe_free(new_password);
1208 crypt_free_volume_key(vk);
1209 return r < 0 ? r : keyslot;
1212 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1214 const char *volume_key,
1215 size_t volume_key_size,
1216 const char *passphrase,
1217 size_t passphrase_size)
1219 struct volume_key *vk = NULL;
1221 char *new_password = NULL; size_t new_passwordLen;
1223 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1225 if (!isLUKS(cd->type)) {
1226 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1231 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1232 else if (cd->volume_key)
1233 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1238 r = LUKS_verify_volume_key(&cd->hdr, vk);
1240 log_err(cd, _("Volume key does not match the volume.\n"));
1244 r = keyslot_verify_or_find_empty(cd, &keyslot);
1249 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1250 &new_password, &new_passwordLen, 1);
1253 passphrase = new_password;
1254 passphrase_size = new_passwordLen;
1257 r = LUKS_set_key(cd->device, keyslot, passphrase, passphrase_size,
1258 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1260 crypt_safe_free(new_password);
1261 crypt_free_volume_key(vk);
1262 return (r < 0) ? r : keyslot;
1265 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1267 crypt_keyslot_info ki;
1269 log_dbg("Destroying keyslot %d.", keyslot);
1271 if (!isLUKS(cd->type)) {
1272 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1276 ki = crypt_keyslot_status(cd, keyslot);
1277 if (ki == CRYPT_SLOT_INVALID) {
1278 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1282 if (ki == CRYPT_SLOT_INACTIVE) {
1283 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1287 return LUKS_del_key(cd->device, keyslot, &cd->hdr, cd);
1290 // activation/deactivation of device mapping
1291 int crypt_activate_by_passphrase(struct crypt_device *cd,
1294 const char *passphrase,
1295 size_t passphrase_size,
1298 crypt_status_info ci;
1299 struct volume_key *vk = NULL;
1300 char *read_passphrase = NULL;
1301 size_t passphraseLen = 0;
1304 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1305 name ? "Activating" : "Checking", name ?: "",
1306 keyslot, passphrase ? "" : "[none] ");
1309 ci = crypt_status(NULL, name);
1310 if (ci == CRYPT_INVALID)
1312 else if (ci >= CRYPT_ACTIVE) {
1313 log_err(cd, _("Device %s already exists.\n"), name);
1318 /* plain, use hashed passphrase */
1319 if (isPLAIN(cd->type)) {
1324 r = key_from_terminal(cd, NULL, &read_passphrase,
1328 passphrase = read_passphrase;
1329 passphrase_size = passphraseLen;
1332 r = process_key(cd, cd->plain_hdr.hash,
1333 cd->volume_key->keylength,
1334 passphrase, passphrase_size, &vk);
1338 r = PLAIN_activate(cd, name, vk,
1340 cd->plain_hdr.skip, flags);
1342 } else if (isLUKS(cd->type)) {
1343 /* provided passphrase, do not retry */
1345 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1346 passphrase_size, &cd->hdr, &vk, cd);
1348 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1353 r = LUKS1_activate(cd, name, vk, flags);
1358 crypt_safe_free(read_passphrase);
1359 crypt_free_volume_key(vk);
1361 return r < 0 ? r : keyslot;
1364 int crypt_activate_by_keyfile(struct crypt_device *cd,
1367 const char *keyfile,
1368 size_t keyfile_size,
1371 crypt_status_info ci;
1372 struct volume_key *vk = NULL;
1373 char *passphrase_read = NULL;
1374 size_t passphrase_size_read;
1375 unsigned int key_count = 0;
1378 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1379 name ?: "", keyslot, keyfile ?: "[none]");
1382 ci = crypt_status(NULL, name);
1383 if (ci == CRYPT_INVALID)
1385 else if (ci >= CRYPT_ACTIVE) {
1386 log_err(cd, _("Device %s already exists.\n"), name);
1394 if (isPLAIN(cd->type)) {
1398 r = key_from_file(cd, _("Enter passphrase: "),
1399 &passphrase_read, &passphrase_size_read,
1400 keyfile, keyfile_size);
1404 r = process_key(cd, cd->plain_hdr.hash,
1405 cd->volume_key->keylength,
1406 passphrase_read, passphrase_size_read, &vk);
1410 r = PLAIN_activate(cd, name, vk,
1412 cd->plain_hdr.skip, flags);
1413 } else if (isLUKS(cd->type)) {
1414 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1415 &passphrase_size_read, keyfile, keyfile_size);
1418 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1419 passphrase_size_read, &cd->hdr, &vk, cd);
1425 r = LUKS1_activate(cd, name, vk, flags);
1430 } else if (isLOOPAES(cd->type)) {
1431 r = key_from_file(cd, NULL, &passphrase_read, &passphrase_size_read,
1432 keyfile, keyfile_size);
1435 r = LOOPAES_parse_keyfile(cd, &vk, cd->loopaes_hdr.hash, &key_count,
1436 passphrase_read, passphrase_size_read);
1440 r = LOOPAES_activate(cd, name, cd->loopaes_cipher,
1442 cd->loopaes_hdr.skip,
1448 crypt_safe_free(passphrase_read);
1449 crypt_free_volume_key(vk);
1454 int crypt_activate_by_volume_key(struct crypt_device *cd,
1456 const char *volume_key,
1457 size_t volume_key_size,
1460 crypt_status_info ci;
1461 struct volume_key *vk = NULL;
1464 log_dbg("Activating volume %s by volume key.", name);
1467 ci = crypt_status(NULL, name);
1468 if (ci == CRYPT_INVALID)
1470 else if (ci >= CRYPT_ACTIVE) {
1471 log_err(cd, _("Device %s already exists.\n"), name);
1476 /* use key directly, no hash */
1477 if (isPLAIN(cd->type)) {
1481 if (!volume_key || !volume_key_size || !cd->volume_key ||
1482 volume_key_size != cd->volume_key->keylength) {
1483 log_err(cd, _("Incorrect volume key specified for plain device.\n"));
1487 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1491 r = PLAIN_activate(cd, name, vk,
1493 cd->plain_hdr.skip, flags);
1494 } else if (isLUKS(cd->type)) {
1495 /* If key is not provided, try to use internal key */
1497 if (!cd->volume_key) {
1498 log_err(cd, _("Volume key does not match the volume.\n"));
1501 volume_key_size = cd->volume_key->keylength;
1502 volume_key = cd->volume_key->key;
1505 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1508 r = LUKS_verify_volume_key(&cd->hdr, vk);
1511 log_err(cd, _("Volume key does not match the volume.\n"));
1514 r = LUKS1_activate(cd, name, vk, flags);
1516 log_err(cd, _("Device type is not properly initialised.\n"));
1518 crypt_free_volume_key(vk);
1523 int crypt_deactivate(struct crypt_device *cd, const char *name)
1530 log_dbg("Deactivating volume %s.", name);
1532 if (!cd && dm_init(NULL, 1) < 0)
1535 switch (crypt_status(cd, name)) {
1537 r = dm_remove_device(name, 0, 0);
1540 log_err(cd, _("Device %s is busy.\n"), name);
1543 case CRYPT_INACTIVE:
1544 log_err(cd, _("Device %s is not active.\n"), name);
1548 log_err(cd, _("Invalid device %s.\n"), name);
1558 int crypt_volume_key_get(struct crypt_device *cd,
1561 size_t *volume_key_size,
1562 const char *passphrase,
1563 size_t passphrase_size)
1565 struct volume_key *vk = NULL;
1569 key_len = crypt_get_volume_key_size(cd);
1570 if (key_len > *volume_key_size) {
1571 log_err(cd, _("Volume key buffer too small.\n"));
1575 if (isPLAIN(cd->type) && cd->plain_hdr.hash) {
1576 r = process_key(cd, cd->plain_hdr.hash, key_len,
1577 passphrase, passphrase_size, &vk);
1579 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
1580 } else if (isLUKS(cd->type)) {
1581 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1582 passphrase_size, &cd->hdr, &vk, cd);
1585 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
1588 memcpy(volume_key, vk->key, vk->keylength);
1589 *volume_key_size = vk->keylength;
1592 crypt_free_volume_key(vk);
1596 int crypt_volume_key_verify(struct crypt_device *cd,
1597 const char *volume_key,
1598 size_t volume_key_size)
1600 struct volume_key *vk;
1603 if (!isLUKS(cd->type)) {
1604 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1608 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1612 r = LUKS_verify_volume_key(&cd->hdr, vk);
1615 log_err(cd, _("Volume key does not match the volume.\n"));
1617 crypt_free_volume_key(vk);
1622 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
1624 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
1625 cd->timeout = timeout_sec;
1628 void crypt_set_password_retry(struct crypt_device *cd, int tries)
1630 log_dbg("Password retry count set to %d.", tries);
1634 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
1636 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
1637 cd->iteration_time = iteration_time_ms;
1640 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
1642 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
1643 cd->password_verify = password_verify ? 1 : 0;
1646 void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
1649 case CRYPT_RNG_URANDOM:
1650 case CRYPT_RNG_RANDOM:
1651 log_dbg("RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
1652 cd->rng_type = rng_type;
1656 int crypt_get_rng_type(struct crypt_device *cd)
1661 return cd->rng_type;
1664 int crypt_memory_lock(struct crypt_device *cd, int lock)
1666 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
1670 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
1674 if (!cd && dm_init(NULL, 1) < 0)
1675 return CRYPT_INVALID;
1677 r = dm_status_device(name);
1682 if (r < 0 && r != -ENODEV)
1683 return CRYPT_INVALID;
1686 return CRYPT_ACTIVE;
1691 return CRYPT_INACTIVE;
1694 static void hexprintICB(struct crypt_device *cd, char *d, int n)
1697 for(i = 0; i < n; i++)
1698 log_std(cd, "%02hhx ", (char)d[i]);
1701 int crypt_dump(struct crypt_device *cd)
1704 if (!isLUKS(cd->type)) { //FIXME
1705 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1709 log_std(cd, "LUKS header information for %s\n\n", cd->device);
1710 log_std(cd, "Version: \t%d\n", cd->hdr.version);
1711 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
1712 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
1713 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec);
1714 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
1715 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
1716 log_std(cd, "MK digest: \t");
1717 hexprintICB(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE);
1719 log_std(cd, "MK salt: \t");
1720 hexprintICB(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2);
1721 log_std(cd, "\n \t");
1722 hexprintICB(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
1724 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
1725 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
1726 for(i = 0; i < LUKS_NUMKEYS; i++) {
1727 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
1728 log_std(cd, "Key Slot %d: ENABLED\n",i);
1729 log_std(cd, "\tIterations: \t%d\n",
1730 cd->hdr.keyblock[i].passwordIterations);
1731 log_std(cd, "\tSalt: \t");
1732 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt,
1734 log_std(cd, "\n\t \t");
1735 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt +
1736 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
1739 log_std(cd, "\tKey material offset:\t%d\n",
1740 cd->hdr.keyblock[i].keyMaterialOffset);
1741 log_std(cd, "\tAF stripes: \t%d\n",
1742 cd->hdr.keyblock[i].stripes);
1745 log_std(cd, "Key Slot %d: DISABLED\n", i);
1751 const char *crypt_get_cipher(struct crypt_device *cd)
1753 if (isPLAIN(cd->type))
1754 return cd->plain_cipher;
1756 if (isLUKS(cd->type))
1757 return cd->hdr.cipherName;
1759 if (isLOOPAES(cd->type))
1760 return cd->loopaes_cipher;
1765 const char *crypt_get_cipher_mode(struct crypt_device *cd)
1767 if (isPLAIN(cd->type))
1768 return cd->plain_cipher_mode;
1770 if (isLUKS(cd->type))
1771 return cd->hdr.cipherMode;
1773 if (isLOOPAES(cd->type))
1774 return cd->loopaes_cipher_mode;
1779 const char *crypt_get_uuid(struct crypt_device *cd)
1781 if (isLUKS(cd->type))
1782 return cd->hdr.uuid;
1784 if (isPLAIN(cd->type))
1785 return cd->plain_uuid;
1787 if (isLOOPAES(cd->type))
1788 return cd->loopaes_uuid;
1793 const char *crypt_get_device_name(struct crypt_device *cd)
1798 int crypt_get_volume_key_size(struct crypt_device *cd)
1800 if (isPLAIN(cd->type) && cd->volume_key)
1801 return cd->volume_key->keylength;
1803 if (isLUKS(cd->type))
1804 return cd->hdr.keyBytes;
1806 if (isLOOPAES(cd->type))
1807 return cd->loopaes_key_size;
1812 uint64_t crypt_get_data_offset(struct crypt_device *cd)
1814 if (isPLAIN(cd->type))
1815 return cd->plain_hdr.offset;
1817 if (isLUKS(cd->type))
1818 return cd->hdr.payloadOffset;
1820 if (isLOOPAES(cd->type))
1821 return cd->loopaes_hdr.offset;
1826 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
1828 if (!isLUKS(cd->type)) {
1829 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1830 return CRYPT_SLOT_INVALID;
1833 return LUKS_keyslot_info(&cd->hdr, keyslot);
1836 int crypt_keyslot_max(const char *type)
1838 if (type && isLUKS(type))
1839 return LUKS_NUMKEYS;
1844 const char *crypt_get_type(struct crypt_device *cd)
1849 int crypt_get_active_device(struct crypt_device *cd __attribute__((unused)),
1851 struct crypt_active_device *cad)
1853 struct crypt_dm_active_device dmd;
1856 r = dm_query_device(name, 0, &dmd);
1860 cad->offset = dmd.offset;
1861 cad->iv_offset = dmd.iv_offset;
1862 cad->size = dmd.size;
1863 cad->flags = dmd.flags;