8 #include "libcryptsetup.h"
12 #include "crypto_backend.h"
20 struct volume_key *volume_key;
22 uint64_t iteration_time;
27 /* used in CRYPT_LUKS1 */
29 uint64_t PBKDF2_per_sec;
31 /* used in CRYPT_PLAIN */
32 struct crypt_params_plain plain_hdr;
34 char *plain_cipher_mode;
37 /* used in CRYPT_LOOPAES */
38 struct crypt_params_loopaes loopaes_hdr;
40 char *loopaes_cipher_mode;
42 unsigned int loopaes_key_size;
44 /* callbacks definitions */
45 void (*log)(int level, const char *msg, void *usrptr);
47 int (*confirm)(const char *msg, void *usrptr);
49 int (*password)(const char *msg, char *buf, size_t length, void *usrptr);
50 void *password_usrptr;
54 static void (*_default_log)(int level, const char *msg, void *usrptr) = NULL;
55 static int _debug_level = 0;
57 void crypt_set_debug_level(int level)
62 int crypt_get_debug_level()
67 void crypt_log(struct crypt_device *cd, int level, const char *msg)
70 cd->log(level, msg, cd->log_usrptr);
71 else if (_default_log)
72 _default_log(level, msg, NULL);
75 void logger(struct crypt_device *cd, int level, const char *file,
76 int line, const char *format, ...)
81 va_start(argp, format);
83 if (vasprintf(&target, format, argp) > 0) {
85 crypt_log(cd, level, target);
87 } else if (_debug_level)
88 printf("# %s:%d %s\n", file ?: "?", line, target);
90 } else if (_debug_level)
91 printf("# %s\n", target);
99 static int init_crypto(struct crypt_device *ctx)
103 r = crypt_random_init(ctx);
105 log_err(ctx, _("Cannot initialize crypto RNG backend.\n"));
109 r = crypt_backend_init(ctx);
111 log_err(ctx, _("Cannot initialize crypto backend.\n"));
117 * Password processing behaviour matrix of process_key
119 * from binary file: check if there is sufficently large key material
120 * interactive & from fd: hash if requested, otherwise crop or pad with '0'
122 static char *process_key(struct crypt_device *cd, const char *hash_name,
123 const char *key_file, size_t key_size,
124 const char *pass, size_t passLen)
132 key = crypt_safe_alloc(key_size);
133 memset(key, 0, key_size);
135 /* key is coming from binary file */
136 if (key_file && strcmp(key_file, "-")) {
137 if(passLen < key_size) {
138 log_err(cd, _("Cannot not read %d bytes from key file %s.\n"),
140 crypt_safe_free(key);
143 memcpy(key, pass, key_size);
147 /* key is coming from tty, fd or binary stdin */
149 r = crypt_plain_hash(cd, hash_name, key, key_size, pass, passLen);
152 log_err(cd, _("Hash algorithm %s not supported.\n"),
155 log_err(cd, _("Key processing error (using hash %s).\n"),
157 crypt_safe_free(key);
160 } else if (passLen > key_size) {
161 memcpy(key, pass, key_size);
163 memcpy(key, pass, passLen);
169 static int isPLAIN(const char *type)
171 return (type && !strcmp(CRYPT_PLAIN, type));
174 static int isLUKS(const char *type)
176 return (type && !strcmp(CRYPT_LUKS1, type));
179 static int isLOOPAES(const char *type)
181 return (type && !strcmp(CRYPT_LOOPAES, type));
184 /* keyslot helpers */
185 static int keyslot_verify_or_find_empty(struct crypt_device *cd, int *keyslot)
187 if (*keyslot == CRYPT_ANY_SLOT) {
188 *keyslot = LUKS_keyslot_find_empty(&cd->hdr);
190 log_err(cd, _("All key slots full.\n"));
195 switch (LUKS_keyslot_info(&cd->hdr, *keyslot)) {
196 case CRYPT_SLOT_INVALID:
197 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
198 *keyslot, LUKS_NUMKEYS - 1);
200 case CRYPT_SLOT_INACTIVE:
203 log_err(cd, _("Key slot %d is full, please select another one.\n"),
211 static int verify_other_keyslot(struct crypt_device *cd,
212 const char *key_file,
215 struct volume_key *vk = NULL;
216 crypt_keyslot_info ki;
218 char *password = NULL;
219 unsigned int passwordLen;
221 r = crypt_get_key(_("Enter any remaining LUKS passphrase: "),
222 &password, &passwordLen, 0, key_file, cd->timeout,
223 cd->password_verify, cd);
227 ki = crypt_keyslot_status(cd, keyIndex);
228 if (ki == CRYPT_SLOT_ACTIVE) /* Not last slot */
229 LUKS_keyslot_set(&cd->hdr, keyIndex, 0);
231 openedIndex = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT,
232 password, passwordLen,
235 if (ki == CRYPT_SLOT_ACTIVE)
236 LUKS_keyslot_set(&cd->hdr, keyIndex, 1);
241 log_verbose(cd, _("Key slot %d verified.\n"), openedIndex);
243 crypt_free_volume_key(vk);
244 crypt_safe_free(password);
248 static int find_keyslot_by_passphrase(struct crypt_device *cd,
249 const char *key_file,
252 struct volume_key *vk = NULL;
253 char *password = NULL;
254 unsigned int passwordLen;
257 r = crypt_get_key(message,&password,&passwordLen, 0, key_file,
258 cd->timeout, cd->password_verify, cd);
262 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
263 passwordLen, &cd->hdr, &vk, cd);
265 crypt_free_volume_key(vk);
266 crypt_safe_free(password);
270 static int luks_remove_helper(struct crypt_device *cd,
272 const char *other_key_file,
273 const char *key_file,
276 crypt_keyslot_info ki;
279 if (key_slot == CRYPT_ANY_SLOT) {
280 key_slot = find_keyslot_by_passphrase(cd, key_file,
281 _("Enter LUKS passphrase to be deleted: "));
287 log_std(cd, _("Key slot %d selected for deletion.\n"), key_slot);
290 ki = crypt_keyslot_status(cd, key_slot);
291 if (ki == CRYPT_SLOT_INVALID) {
292 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
293 key_slot, LUKS_NUMKEYS - 1);
297 if (ki <= CRYPT_SLOT_INACTIVE) {
298 log_err(cd, _("Key %d not active. Can't wipe.\n"), key_slot);
303 if (ki == CRYPT_SLOT_ACTIVE_LAST && cd->confirm &&
304 !(cd->confirm(_("This is the last keyslot."
305 " Device will become unusable after purging this key."),
306 cd->confirm_usrptr))) {
312 r = verify_other_keyslot(cd, other_key_file, key_slot);
317 r = crypt_keyslot_destroy(cd, key_slot);
319 return (r < 0) ? r : 0;
322 static int create_device_helper(struct crypt_device *cd,
326 const char *cipher_mode,
327 const char *key_file,
339 crypt_status_info ci;
340 char *dm_cipher = NULL;
341 char *processed_key = NULL;
347 ci = crypt_status(cd, name);
348 if (ci == CRYPT_INVALID)
351 if (reload && ci < CRYPT_ACTIVE)
354 if (!reload && ci >= CRYPT_ACTIVE) {
355 log_err(cd, _("Device %s already exists.\n"), name);
359 if (key_size < 0 || key_size > 1024) {
360 log_err(cd, _("Invalid key size %d.\n"), key_size);
364 r = device_check_and_adjust(cd, cd->device, !reload, &size, &offset, &read_only);
368 if (cipher_mode && asprintf(&dm_cipher, "%s-%s", cipher, cipher_mode) < 0)
371 processed_key = process_key(cd, hash, key_file, key_size, key, keyLen);
372 if (!processed_key) {
377 r = dm_create_device(name, cd->device, dm_cipher ?: cipher, cd->type, uuid, size, skip, offset,
378 key_size, processed_key, read_only, reload);
381 crypt_safe_free(processed_key);
385 static int open_from_hdr_and_vk(struct crypt_device *cd,
386 struct volume_key *vk,
390 uint64_t size, offset;
392 int read_only, no_uuid, r;
395 offset = crypt_get_data_offset(cd);
396 read_only = flags & CRYPT_ACTIVATE_READONLY;
397 no_uuid = flags & CRYPT_ACTIVATE_NO_UUID;
399 r = device_check_and_adjust(cd, cd->device, 1, &size, &offset, &read_only);
403 if (asprintf(&cipher, "%s-%s", crypt_get_cipher(cd),
404 crypt_get_cipher_mode(cd)) < 0)
407 r = dm_create_device(name, cd->device, cipher, cd->type,
408 no_uuid ? NULL : crypt_get_uuid(cd),
409 size, 0, offset, vk->keylength, vk->key,
415 static void log_wrapper(int level, const char *msg, void *usrptr)
417 void (*xlog)(int level, char *msg) = usrptr;
418 xlog(level, (char *)msg);
421 static int yesDialog_wrapper(const char *msg, void *usrptr)
423 int (*xyesDialog)(char *msg) = usrptr;
424 return xyesDialog((char*)msg);
427 int crypt_confirm(struct crypt_device *cd, const char *msg)
429 if (!cd || !cd->confirm)
432 return cd->confirm(msg, cd->confirm_usrptr);
435 static int key_from_terminal(struct crypt_device *cd, char *msg, char **key,
436 unsigned int *key_len, int force_verify)
442 if(!msg && asprintf(&prompt, _("Enter passphrase for %s: "),
443 cd->backing_file ?: cd->device) < 0)
450 *key = crypt_safe_alloc(MAX_TTY_PASSWORD_LEN);
455 r = cd->password(msg, *key, MAX_TTY_PASSWORD_LEN, cd->password_usrptr);
457 crypt_safe_free(*key);
462 r = crypt_get_key(msg, key, key_len, 0, NULL, cd->timeout,
463 (force_verify || cd->password_verify), cd);
466 return (r < 0) ? r: 0;
469 static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslot,
470 struct volume_key **vk)
472 char *passphrase_read = NULL;
473 unsigned int passphrase_size_read;
474 int r = -EINVAL, tries = cd->tries;
478 crypt_free_volume_key(*vk);
481 r = key_from_terminal(cd, NULL, &passphrase_read,
482 &passphrase_size_read, 0);
486 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
487 passphrase_size_read, &cd->hdr, vk, cd);
488 crypt_safe_free(passphrase_read);
489 passphrase_read = NULL;
490 } while (r == -EPERM && (--tries > 0));
493 crypt_free_volume_key(*vk);
497 crypt_safe_free(passphrase_read);
501 static int key_from_file(struct crypt_device *cd, char *msg,
502 char **key, unsigned int *key_len,
503 const char *key_file, size_t key_size)
505 return crypt_get_key(msg, key, key_len, key_size, key_file,
509 static int _crypt_init(struct crypt_device **cd,
511 struct crypt_options *options,
512 int load, int need_dm)
516 /* if it is plain device and mapping table is being reloaded
517 initialize it by name*/
518 init_by_name = (type && !strcmp(type, CRYPT_PLAIN) && load);
520 /* Some of old API calls do not require DM in kernel,
521 fake initialisation by initialise it with kernel_check disabled */
523 (void)dm_init(NULL, 0);
525 r = crypt_init_by_name(cd, options->name);
527 r = crypt_init(cd, options->device);
534 crypt_set_log_callback(*cd, log_wrapper, options->icb->log);
535 crypt_set_confirm_callback(*cd, yesDialog_wrapper, options->icb->yesDialog);
537 crypt_set_timeout(*cd, options->timeout);
538 crypt_set_password_retry(*cd, options->tries);
539 crypt_set_iterarion_time(*cd, options->iteration_time ?: 1000);
540 crypt_set_password_verify(*cd, options->flags & CRYPT_FLAG_VERIFY);
542 if (load && !init_by_name)
543 r = crypt_load(*cd, type, NULL);
545 if (!r && type && !(*cd)->type) {
546 (*cd)->type = strdup(type);
557 void crypt_set_log_callback(struct crypt_device *cd,
558 void (*log)(int level, const char *msg, void *usrptr),
565 cd->log_usrptr = usrptr;
569 void crypt_set_confirm_callback(struct crypt_device *cd,
570 int (*confirm)(const char *msg, void *usrptr),
573 cd->confirm = confirm;
574 cd->confirm_usrptr = usrptr;
577 void crypt_set_password_callback(struct crypt_device *cd,
578 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
581 cd->password = password;
582 cd->password_usrptr = usrptr;
585 /* OPTIONS: name, cipher, device, hash, key_file, key_size, key_slot,
586 * offset, size, skip, timeout, tries, passphrase_fd (ignored),
588 static int crypt_create_and_update_device(struct crypt_options *options, int update)
590 struct crypt_device *cd = NULL;
595 r = _crypt_init(&cd, CRYPT_PLAIN, options, 0, 1);
599 r = crypt_get_key(_("Enter passphrase: "), &key, &keyLen, options->key_size,
600 options->key_file, cd->timeout, cd->password_verify, cd);
602 r = create_device_helper(cd, options->name, options->hash,
603 options->cipher, NULL, options->key_file, key, keyLen,
604 options->key_size, options->size, options->skip,
605 options->offset, NULL, options->flags & CRYPT_FLAG_READONLY,
606 options->flags, update);
608 crypt_safe_free(key);
613 int crypt_create_device(struct crypt_options *options)
615 return crypt_create_and_update_device(options, 0);
618 int crypt_update_device(struct crypt_options *options)
620 return crypt_create_and_update_device(options, 1);
623 /* OPTIONS: name, size, icb */
624 int crypt_resize_device(struct crypt_options *options)
626 struct crypt_device *cd = NULL;
627 char *device = NULL, *cipher = NULL, *uuid = NULL, *key = NULL;
629 uint64_t size, skip, offset;
630 int key_size, read_only, r;
632 log_dbg("Resizing device %s to %" PRIu64 " sectors.", options->name, options->size);
634 if (dm_init(NULL, 1) < 0)
637 r = dm_query_device(options->name, &device, &size, &skip, &offset,
638 &cipher, &key_size, &key, &read_only, NULL, &uuid);
640 log_err(NULL, _("Device %s is not active.\n"), options->name);
644 /* Try to determine type of device from UUID */
647 if (!strncmp(uuid, CRYPT_PLAIN, strlen(CRYPT_PLAIN))) {
651 } else if (!strncmp(uuid, CRYPT_LUKS1, strlen(CRYPT_LUKS1)))
655 if (!options->device)
656 options->device = device;
658 r = _crypt_init(&cd, type, options, 1, 1);
662 size = options->size;
663 r = device_check_and_adjust(cd, device, 0, &size, &offset, &read_only);
667 r = dm_create_device(options->name, device, cipher, type,
668 crypt_get_uuid(cd), size, skip, offset,
669 key_size, key, read_only, 1);
671 crypt_safe_free(key);
673 if (options->device == device)
674 options->device = NULL;
682 /* OPTIONS: name, icb */
683 int crypt_query_device(struct crypt_options *options)
687 log_dbg("Query device %s.", options->name);
689 if (dm_init(NULL, 1) < 0)
692 r = dm_status_device(options->name);
696 r = dm_query_device(options->name, (char **)&options->device, &options->size,
697 &options->skip, &options->offset, (char **)&options->cipher,
698 &options->key_size, NULL, &read_only, NULL, NULL);
701 options->flags |= CRYPT_FLAG_READONLY;
703 options->flags |= CRYPT_FLAG_FREE_DEVICE;
704 options->flags |= CRYPT_FLAG_FREE_CIPHER;
716 /* OPTIONS: name, icb */
717 int crypt_remove_device(struct crypt_options *options)
719 struct crypt_device *cd = NULL;
722 r = crypt_init_by_name(&cd, options->name);
724 r = crypt_deactivate(cd, options->name);
731 /* OPTIONS: device, cipher, hash, align_payload, key_size (master key), key_slot
732 * new_key_file, iteration_time, timeout, flags, icb */
733 int crypt_luksFormat(struct crypt_options *options)
735 char cipherName[LUKS_CIPHERNAME_L];
736 char cipherMode[LUKS_CIPHERMODE_L];
738 unsigned int passwordLen;
739 struct crypt_device *cd = NULL;
740 struct crypt_params_luks1 cp = {
741 .hash = options->hash,
742 .data_alignment = options->align_payload
746 r = crypt_parse_name_and_mode(options->cipher, cipherName, NULL, cipherMode);
748 log_err(cd, _("No known cipher specification pattern detected.\n"));
752 if ((r = _crypt_init(&cd, CRYPT_LUKS1, options, 0, 1)))
755 if (options->key_slot >= LUKS_NUMKEYS && options->key_slot != CRYPT_ANY_SLOT) {
756 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
757 options->key_slot, LUKS_NUMKEYS - 1);
762 r = crypt_get_key(_("Enter LUKS passphrase: "), &password, &passwordLen, 0,
763 options->new_key_file, cd->timeout, cd->password_verify, cd);
768 r = crypt_format(cd, CRYPT_LUKS1, cipherName, cipherMode,
769 NULL, NULL, options->key_size, &cp);
773 /* Add keyslot using internally stored volume key generated during format */
774 r = crypt_keyslot_add_by_volume_key(cd, options->key_slot, NULL, 0,
775 password, passwordLen);
778 crypt_safe_free(password);
779 return (r < 0) ? r : 0;
782 /* OPTIONS: name, device, key_size, key_file, timeout, tries, flags, icb */
783 int crypt_luksOpen(struct crypt_options *options)
785 struct crypt_device *cd = NULL;
792 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
796 if (options->flags & CRYPT_FLAG_READONLY)
797 flags |= CRYPT_ACTIVATE_READONLY;
799 if (options->flags & CRYPT_FLAG_NON_EXCLUSIVE_ACCESS)
800 flags |= CRYPT_ACTIVATE_NO_UUID;
802 if (options->key_file)
803 r = crypt_activate_by_keyfile(cd, options->name,
804 CRYPT_ANY_SLOT, options->key_file, options->key_size,
807 r = crypt_activate_by_passphrase(cd, options->name,
808 CRYPT_ANY_SLOT, options->passphrase,
809 options->passphrase ? strlen(options->passphrase) : 0,
813 return (r < 0) ? r : 0;
816 /* OPTIONS: device, keys_slot, key_file, timeout, flags, icb */
817 int crypt_luksKillSlot(struct crypt_options *options)
819 struct crypt_device *cd = NULL;
822 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
826 r = luks_remove_helper(cd, options->key_slot, options->key_file, NULL,
827 options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
830 return (r < 0) ? r : 0;
833 /* OPTIONS: device, new_key_file, key_file, timeout, flags, icb */
834 int crypt_luksRemoveKey(struct crypt_options *options)
836 struct crypt_device *cd = NULL;
839 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
843 r = luks_remove_helper(cd, CRYPT_ANY_SLOT, options->key_file, options->new_key_file,
844 options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
847 return (r < 0) ? r : 0;
851 /* OPTIONS: device, new_key_file, key_file, key_slot, flags,
852 iteration_time, timeout, icb */
853 int crypt_luksAddKey(struct crypt_options *options)
855 struct crypt_device *cd = NULL;
858 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
862 if (options->key_file || options->new_key_file)
863 r = crypt_keyslot_add_by_keyfile(cd, options->key_slot,
864 options->key_file, 0,
865 options->new_key_file, 0);
867 r = crypt_keyslot_add_by_passphrase(cd, options->key_slot,
871 return (r < 0) ? r : 0;
874 /* OPTIONS: device, icb */
875 int crypt_luksUUID(struct crypt_options *options)
877 struct crypt_device *cd = NULL;
881 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
885 uuid = (char *)crypt_get_uuid(cd);
886 log_std(cd, uuid ?: "");
892 /* OPTIONS: device, icb */
893 int crypt_isLuks(struct crypt_options *options)
895 struct crypt_device *cd = NULL;
898 log_dbg("Check device %s for LUKS header.", options->device);
904 r = crypt_init(&cd, options->device);
908 /* Do print fail here, no need to crypt_load() */
909 r = LUKS_read_phdr(cd->device, &cd->hdr, 0, cd) ? -EINVAL : 0;
915 /* OPTIONS: device, icb */
916 int crypt_luksDump(struct crypt_options *options)
918 struct crypt_device *cd = NULL;
921 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
931 void crypt_get_error(char *buf, size_t size)
933 const char *error = get_error();
935 if (!buf || size < 1)
938 strncpy(buf, error, size - 1);
939 buf[size - 1] = '\0';
945 void crypt_put_options(struct crypt_options *options)
947 if (options->flags & CRYPT_FLAG_FREE_DEVICE) {
948 free((char *)options->device);
949 options->device = NULL;
950 options->flags &= ~CRYPT_FLAG_FREE_DEVICE;
952 if (options->flags & CRYPT_FLAG_FREE_CIPHER) {
953 free((char *)options->cipher);
954 options->cipher = NULL;
955 options->flags &= ~CRYPT_FLAG_FREE_CIPHER;
959 const char *crypt_get_dir(void)
964 /////////////////////////////////
969 int crypt_init(struct crypt_device **cd, const char *device)
971 struct crypt_device *h = NULL;
977 log_dbg("Allocating crypt device %s context.", device);
979 if (!(h = malloc(sizeof(struct crypt_device))))
982 memset(h, 0, sizeof(*h));
986 r = device_ready(NULL, device, O_RDONLY);
988 h->device = crypt_loop_get_device();
989 log_dbg("Not a block device, %s%s.",
990 h->device ? "using free loop device " :
991 "no free loop device found",
994 log_err(NULL, _("Cannot find a free loopback device.\n"));
999 /* Keep the loop open, dettached on last close. */
1000 h->loop_fd = crypt_loop_attach(h->device, device, 0, 1, &readonly);
1001 if (h->loop_fd == -1) {
1002 log_err(NULL, _("Attaching loopback device failed "
1003 "(loop device with autoclear flag is required).\n"));
1008 h->backing_file = crypt_loop_backing_file(h->device);
1009 r = device_ready(NULL, h->device, O_RDONLY);
1017 if (!h->device && device && !(h->device = strdup(device))) {
1022 if (dm_init(h, 1) < 0) {
1027 h->iteration_time = 1000;
1028 h->password_verify = 0;
1030 h->rng_type = crypt_random_default_key_rng();
1036 if (h->loop_fd != -1)
1039 free(h->backing_file);
1045 int crypt_init_by_name(struct crypt_device **cd, const char *name)
1047 crypt_status_info ci;
1048 struct crypt_active_device cad;
1049 char *device = NULL, *cipher_full = NULL, *device_uuid = NULL;
1050 char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
1052 int key_size = 0, key_nums, r;
1055 log_dbg("Allocating crypt device context by device %s.", name);
1057 ci = crypt_status(NULL, name);
1058 if (ci == CRYPT_INVALID)
1061 if (ci < CRYPT_ACTIVE) {
1062 log_err(NULL, _("Device %s is not active.\n"), name);
1066 r = dm_query_device(name, &device, &cad.size, &cad.iv_offset, &cad.offset,
1067 &cipher_full, &key_size, &key, NULL, NULL,
1073 r = crypt_init(cd, device);
1075 /* Underlying device disappeared but mapping still active */
1076 if (!device || r == -ENOTBLK)
1077 log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
1080 /* Underlying device is not readable but crypt mapping exists */
1081 if (r == -ENOTBLK) {
1084 r = crypt_init(cd, NULL);
1090 /* Try to initialise basic parameters from active device */
1092 if (!(*cd)->backing_file && device && crypt_loop_device(device) &&
1093 !((*cd)->backing_file = crypt_loop_backing_file(device))) {
1099 if (!strncmp(CRYPT_PLAIN, device_uuid, sizeof(CRYPT_PLAIN)-1)) {
1100 (*cd)->type = strdup(CRYPT_PLAIN);
1101 (*cd)->plain_uuid = strdup(device_uuid);
1102 (*cd)->plain_hdr.hash = NULL; /* no way to get this */
1103 (*cd)->plain_hdr.offset = cad.offset;
1104 (*cd)->plain_hdr.skip = cad.iv_offset;
1105 (*cd)->volume_key = crypt_alloc_volume_key(key_size, key);
1106 if (!(*cd)->volume_key) {
1111 r = crypt_parse_name_and_mode(cipher_full, cipher, NULL, cipher_mode);
1113 (*cd)->plain_cipher = strdup(cipher);
1114 (*cd)->plain_cipher_mode = strdup(cipher_mode);
1116 } else if (!strncmp(CRYPT_LOOPAES, device_uuid, sizeof(CRYPT_LOOPAES)-1)) {
1117 (*cd)->type = strdup(CRYPT_LOOPAES);
1118 (*cd)->loopaes_uuid = strdup(device_uuid);
1119 (*cd)->loopaes_hdr.offset = cad.offset;
1121 r = crypt_parse_name_and_mode(cipher_full, cipher,
1122 &key_nums, cipher_mode);
1124 (*cd)->loopaes_cipher = strdup(cipher);
1125 (*cd)->loopaes_cipher_mode = strdup(cipher_mode);
1126 /* version 3 uses last key for IV */
1127 if (key_size % key_nums)
1129 (*cd)->loopaes_key_size = key_size / key_nums;
1131 } else if (!strncmp(CRYPT_LUKS1, device_uuid, sizeof(CRYPT_LUKS1)-1)) {
1133 if (crypt_load(*cd, CRYPT_LUKS1, NULL) < 0 ||
1134 crypt_volume_key_verify(*cd, key, key_size) < 0) {
1135 log_dbg("LUKS device header does not match active device.");
1139 (*cd)->volume_key = crypt_alloc_volume_key(key_size, key);
1140 if (!(*cd)->volume_key) {
1147 log_dbg("Active device has no UUID set, some parameters are not set.");
1154 crypt_safe_free(key);
1161 static int _crypt_format_plain(struct crypt_device *cd,
1163 const char *cipher_mode,
1165 size_t volume_key_size,
1166 struct crypt_params_plain *params)
1168 if (!cipher || !cipher_mode) {
1169 log_err(cd, _("Invalid plain crypt parameters.\n"));
1173 if (volume_key_size > 1024) {
1174 log_err(cd, _("Invalid key size.\n"));
1178 cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
1179 if (!cd->volume_key)
1182 cd->plain_cipher = strdup(cipher);
1183 cd->plain_cipher_mode = strdup(cipher_mode);
1186 cd->plain_uuid = strdup(uuid);
1188 if (params && params->hash)
1189 cd->plain_hdr.hash = strdup(params->hash);
1191 cd->plain_hdr.offset = params ? params->offset : 0;
1192 cd->plain_hdr.skip = params ? params->skip : 0;
1194 if (!cd->plain_cipher || !cd->plain_cipher_mode)
1200 static int _crypt_format_luks1(struct crypt_device *cd,
1202 const char *cipher_mode,
1204 const char *volume_key,
1205 size_t volume_key_size,
1206 struct crypt_params_luks1 *params)
1209 unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
1210 unsigned long alignment_offset = 0;
1213 log_err(cd, _("Can't format LUKS without device.\n"));
1218 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
1221 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
1226 if (params && params->data_alignment)
1227 required_alignment = params->data_alignment * SECTOR_SIZE;
1229 get_topology_alignment(cd->device, &required_alignment,
1230 &alignment_offset, DEFAULT_DISK_ALIGNMENT);
1232 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
1233 (params && params->hash) ? params->hash : "sha1",
1235 required_alignment / SECTOR_SIZE,
1236 alignment_offset / SECTOR_SIZE,
1237 cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1241 /* Wipe first 8 sectors - fs magic numbers etc. */
1242 r = wipe_device_header(cd->device, 8);
1245 log_err(cd, _("Cannot format device %s which is still in use.\n"),
1248 log_err(cd, _("Cannot wipe header on device %s.\n"),
1254 r = LUKS_write_phdr(cd->device, &cd->hdr, cd);
1259 static int _crypt_format_loopaes(struct crypt_device *cd,
1262 size_t volume_key_size,
1263 struct crypt_params_loopaes *params)
1266 log_err(cd, _("Can't format LOOPAES without device.\n"));
1270 if (volume_key_size > 1024) {
1271 log_err(cd, _("Invalid key size.\n"));
1275 cd->loopaes_key_size = volume_key_size;
1277 cd->loopaes_cipher = strdup(cipher ?: DEFAULT_LOOPAES_CIPHER);
1280 cd->loopaes_uuid = strdup(uuid);
1282 if (params && params->hash)
1283 cd->loopaes_hdr.hash = strdup(params->hash);
1285 cd->loopaes_hdr.offset = params ? params->offset : 0;
1290 int crypt_format(struct crypt_device *cd,
1293 const char *cipher_mode,
1295 const char *volume_key,
1296 size_t volume_key_size,
1304 log_dbg("Formatting device %s as type %s.", cd->device ?: "(none)", type);
1306 r = init_crypto(cd);
1311 r = _crypt_format_plain(cd, cipher, cipher_mode,
1312 uuid, volume_key_size, params);
1313 else if (isLUKS(type))
1314 r = _crypt_format_luks1(cd, cipher, cipher_mode,
1315 uuid, volume_key, volume_key_size, params);
1316 else if (isLOOPAES(type))
1317 r = _crypt_format_loopaes(cd, cipher, uuid, volume_key_size, params);
1319 /* FIXME: allow plugins here? */
1320 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
1324 if (!r && !(cd->type = strdup(type)))
1328 crypt_free_volume_key(cd->volume_key);
1329 cd->volume_key = NULL;
1335 int crypt_load(struct crypt_device *cd,
1336 const char *requested_type,
1339 struct luks_phdr hdr;
1342 log_dbg("Trying to load %s crypt type from device %s.",
1343 requested_type ?: "any", cd->device ?: "(none)");
1348 if (requested_type && !isLUKS(requested_type))
1351 r = init_crypto(cd);
1355 r = LUKS_read_phdr(cd->device, &hdr, 1, cd);
1358 memcpy(&cd->hdr, &hdr, sizeof(hdr));
1359 cd->type = strdup(CRYPT_LUKS1);
1367 int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
1369 char *device = NULL, *cipher = NULL, *uuid = NULL, *key = NULL;
1370 uint64_t size, skip, offset;
1371 int key_size, read_only, r;
1373 /* Device context type must be initialised */
1374 if (!cd->type || !crypt_get_uuid(cd))
1377 r = dm_query_device(name, &device, &size, &skip, &offset,
1378 &cipher, &key_size, &key, &read_only, NULL, &uuid);
1380 log_err(NULL, _("Device %s is not active.\n"), name);
1389 r = device_check_and_adjust(cd, device, 0, &new_size, &offset, &read_only);
1393 if (new_size == size) {
1394 log_dbg("Device has already requested size %" PRIu64
1400 log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
1402 r = dm_create_device(name, device, cipher, cd->type,
1403 crypt_get_uuid(cd), new_size, skip, offset,
1404 key_size, key, read_only, 1);
1406 crypt_safe_free(key);
1414 int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
1416 if (!isLUKS(cd->type)) {
1417 log_err(cd, _("This operation is not supported for this device type.\n"));
1421 if (uuid && !strncmp(uuid, cd->hdr.uuid, sizeof(cd->hdr.uuid))) {
1422 log_dbg("UUID is the same as requested (%s) for device %s.",
1428 log_dbg("Requested new UUID change to %s for %s.", uuid, cd->device);
1430 log_dbg("Requested new UUID refresh for %s.", cd->device);
1432 if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
1435 return LUKS_hdr_uuid_set(cd->device, &cd->hdr, uuid, cd);
1438 int crypt_header_backup(struct crypt_device *cd,
1439 const char *requested_type,
1440 const char *backup_file)
1444 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1447 r = init_crypto(cd);
1451 log_dbg("Requested header backup of device %s (%s) to "
1452 "file %s.", cd->device, requested_type, backup_file);
1454 return LUKS_hdr_backup(backup_file, cd->device, &cd->hdr, cd);
1457 int crypt_header_restore(struct crypt_device *cd,
1458 const char *requested_type,
1459 const char *backup_file)
1463 if (requested_type && !isLUKS(requested_type))
1466 /* Some hash functions need initialized gcrypt library */
1467 r = init_crypto(cd);
1471 log_dbg("Requested header restore to device %s (%s) from "
1472 "file %s.", cd->device, requested_type, backup_file);
1474 return LUKS_hdr_restore(backup_file, cd->device, &cd->hdr, cd);
1477 void crypt_free(struct crypt_device *cd)
1480 log_dbg("Releasing crypt device %s context.", cd->device);
1482 if (cd->loop_fd != -1)
1486 crypt_free_volume_key(cd->volume_key);
1489 free(cd->backing_file);
1492 /* used in plain device only */
1493 free((char*)cd->plain_hdr.hash);
1494 free(cd->plain_cipher);
1495 free(cd->plain_cipher_mode);
1496 free(cd->plain_uuid);
1502 int crypt_suspend(struct crypt_device *cd,
1505 crypt_status_info ci;
1506 int r, suspended = 0;
1508 log_dbg("Suspending volume %s.", name);
1510 ci = crypt_status(NULL, name);
1511 if (ci < CRYPT_ACTIVE) {
1512 log_err(cd, _("Volume %s is not active.\n"), name);
1516 if (!cd && dm_init(NULL, 1) < 0)
1519 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1520 NULL, NULL, NULL, NULL, &suspended, NULL);
1525 log_err(cd, _("Volume %s is already suspended.\n"), name);
1530 r = dm_suspend_and_wipe_key(name);
1532 log_err(cd, "Suspend is not supported for device %s.\n", name);
1534 log_err(cd, "Error during suspending device %s.\n", name);
1541 int crypt_resume_by_passphrase(struct crypt_device *cd,
1544 const char *passphrase,
1545 size_t passphrase_size)
1547 struct volume_key *vk = NULL;
1548 int r, suspended = 0;
1550 log_dbg("Resuming volume %s.", name);
1552 if (!isLUKS(cd->type)) {
1553 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1558 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1559 NULL, NULL, NULL, NULL, &suspended, NULL);
1564 log_err(cd, _("Volume %s is not suspended.\n"), name);
1569 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1570 passphrase_size, &cd->hdr, &vk, cd);
1572 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1576 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1578 log_err(cd, "Resume is not supported for device %s.\n", name);
1580 log_err(cd, "Error during resuming device %s.\n", name);
1584 crypt_free_volume_key(vk);
1585 return r < 0 ? r : keyslot;
1588 int crypt_resume_by_keyfile(struct crypt_device *cd,
1591 const char *keyfile,
1592 size_t keyfile_size)
1594 struct volume_key *vk = NULL;
1595 char *passphrase_read = NULL;
1596 unsigned int passphrase_size_read;
1597 int r, suspended = 0;
1599 log_dbg("Resuming volume %s.", name);
1601 if (!isLUKS(cd->type)) {
1602 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1607 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1608 NULL, NULL, NULL, NULL, &suspended, NULL);
1613 log_err(cd, _("Volume %s is not suspended.\n"), name);
1620 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1621 &passphrase_size_read, keyfile, keyfile_size);
1625 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1626 passphrase_size_read, &cd->hdr, &vk, cd);
1631 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1633 log_err(cd, "Error during resuming device %s.\n", name);
1635 crypt_safe_free(passphrase_read);
1636 crypt_free_volume_key(vk);
1637 return r < 0 ? r : keyslot;
1640 // slot manipulation
1641 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1642 int keyslot, // -1 any
1643 const char *passphrase, // NULL -> terminal
1644 size_t passphrase_size,
1645 const char *new_passphrase, // NULL -> terminal
1646 size_t new_passphrase_size)
1648 struct volume_key *vk = NULL;
1649 char *password = NULL, *new_password = NULL;
1650 unsigned int passwordLen, new_passwordLen;
1653 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1654 "new passphrase %sprovided.",
1655 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1657 if (!isLUKS(cd->type)) {
1658 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1662 r = keyslot_verify_or_find_empty(cd, &keyslot);
1666 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1667 /* No slots used, try to use pre-generated key in header */
1668 if (cd->volume_key) {
1669 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1670 r = vk ? 0 : -ENOMEM;
1672 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1675 } else if (passphrase) {
1676 /* Passphrase provided, use it to unlock existing keyslot */
1677 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, passphrase,
1678 passphrase_size, &cd->hdr, &vk, cd);
1680 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1681 r = key_from_terminal(cd, _("Enter any passphrase: "),
1682 &password, &passwordLen, 0);
1686 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
1687 passwordLen, &cd->hdr, &vk, cd);
1688 crypt_safe_free(password);
1694 if (new_passphrase) {
1695 new_password = (char *)new_passphrase;
1696 new_passwordLen = new_passphrase_size;
1698 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1699 &new_password, &new_passwordLen, 1);
1704 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1705 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1710 if (!new_passphrase)
1711 crypt_safe_free(new_password);
1712 crypt_free_volume_key(vk);
1713 return r ?: keyslot;
1716 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1718 const char *keyfile,
1719 size_t keyfile_size,
1720 const char *new_keyfile,
1721 size_t new_keyfile_size)
1723 struct volume_key *vk = NULL;
1724 char *password = NULL; unsigned int passwordLen;
1725 char *new_password = NULL; unsigned int new_passwordLen;
1728 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1729 keyfile ?: "[none]", new_keyfile ?: "[none]");
1731 if (!isLUKS(cd->type)) {
1732 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1736 r = keyslot_verify_or_find_empty(cd, &keyslot);
1740 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1741 /* No slots used, try to use pre-generated key in header */
1742 if (cd->volume_key) {
1743 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1744 r = vk ? 0 : -ENOMEM;
1746 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1750 /* Read password from file of (if NULL) from terminal */
1752 r = key_from_file(cd, _("Enter any passphrase: "),
1753 &password, &passwordLen,
1754 keyfile, keyfile_size);
1756 r = key_from_terminal(cd, _("Enter any passphrase: "),
1757 &password, &passwordLen, 0);
1761 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password, passwordLen,
1769 r = key_from_file(cd, _("Enter new passphrase for key slot: "),
1770 &new_password, &new_passwordLen, new_keyfile,
1773 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1774 &new_password, &new_passwordLen, 1);
1778 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1779 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1781 crypt_safe_free(password);
1782 crypt_safe_free(new_password);
1783 crypt_free_volume_key(vk);
1784 return r < 0 ? r : keyslot;
1787 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1789 const char *volume_key,
1790 size_t volume_key_size,
1791 const char *passphrase,
1792 size_t passphrase_size)
1794 struct volume_key *vk = NULL;
1796 char *new_password = NULL; unsigned int new_passwordLen;
1798 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1800 if (!isLUKS(cd->type)) {
1801 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1806 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1807 else if (cd->volume_key)
1808 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1813 r = LUKS_verify_volume_key(&cd->hdr, vk);
1815 log_err(cd, _("Volume key does not match the volume.\n"));
1819 r = keyslot_verify_or_find_empty(cd, &keyslot);
1824 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1825 &new_password, &new_passwordLen, 1);
1828 passphrase = new_password;
1829 passphrase_size = new_passwordLen;
1832 r = LUKS_set_key(cd->device, keyslot, passphrase, passphrase_size,
1833 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1835 crypt_safe_free(new_password);
1836 crypt_free_volume_key(vk);
1837 return (r < 0) ? r : keyslot;
1840 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1842 crypt_keyslot_info ki;
1844 log_dbg("Destroying keyslot %d.", keyslot);
1846 if (!isLUKS(cd->type)) {
1847 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1851 ki = crypt_keyslot_status(cd, keyslot);
1852 if (ki == CRYPT_SLOT_INVALID) {
1853 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1857 if (ki == CRYPT_SLOT_INACTIVE) {
1858 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1862 return LUKS_del_key(cd->device, keyslot, &cd->hdr, cd);
1865 // activation/deactivation of device mapping
1866 int crypt_activate_by_passphrase(struct crypt_device *cd,
1869 const char *passphrase,
1870 size_t passphrase_size,
1873 crypt_status_info ci;
1874 struct volume_key *vk = NULL;
1875 char *read_passphrase = NULL;
1876 unsigned int passphraseLen = 0;
1879 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1880 name ? "Activating" : "Checking", name ?: "",
1881 keyslot, passphrase ? "" : "[none] ");
1884 ci = crypt_status(NULL, name);
1885 if (ci == CRYPT_INVALID)
1887 else if (ci >= CRYPT_ACTIVE) {
1888 log_err(cd, _("Device %s already exists.\n"), name);
1893 /* plain, use hashed passphrase */
1894 if (isPLAIN(cd->type)) {
1896 r = key_from_terminal(cd, NULL, &read_passphrase,
1900 passphrase = read_passphrase;
1901 passphrase_size = passphraseLen;
1903 r = create_device_helper(cd, name, cd->plain_hdr.hash,
1904 cd->plain_cipher, cd->plain_cipher_mode,
1905 NULL, passphrase, passphrase_size,
1906 cd->volume_key->keylength, 0,
1907 cd->plain_hdr.skip, cd->plain_hdr.offset,
1909 flags & CRYPT_ACTIVATE_READONLY, 0, 0);
1911 } else if (isLUKS(cd->type)) {
1912 /* provided passphrase, do not retry */
1914 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1915 passphrase_size, &cd->hdr, &vk, cd);
1917 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1922 r = open_from_hdr_and_vk(cd, vk, name, flags);
1927 crypt_safe_free(read_passphrase);
1928 crypt_free_volume_key(vk);
1930 return r < 0 ? r : keyslot;
1933 int crypt_activate_by_keyfile(struct crypt_device *cd,
1936 const char *keyfile,
1937 size_t keyfile_size,
1940 crypt_status_info ci;
1941 struct volume_key *vk = NULL;
1942 char *passphrase_read = NULL;
1943 unsigned int passphrase_size_read, key_count = 0;
1946 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1947 name ?: "", keyslot, keyfile ?: "[none]");
1950 ci = crypt_status(NULL, name);
1951 if (ci == CRYPT_INVALID)
1953 else if (ci >= CRYPT_ACTIVE) {
1954 log_err(cd, _("Device %s already exists.\n"), name);
1962 if (isPLAIN(cd->type)) {
1963 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1964 &passphrase_size_read, keyfile, keyfile_size);
1967 r = create_device_helper(cd, name, cd->plain_hdr.hash,
1968 cd->plain_cipher, cd->plain_cipher_mode,
1969 NULL, passphrase_read, passphrase_size_read,
1970 cd->volume_key->keylength, 0,
1971 cd->plain_hdr.skip, cd->plain_hdr.offset,
1973 flags & CRYPT_ACTIVATE_READONLY, 0, 0);
1974 } else if (isLUKS(cd->type)) {
1975 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1976 &passphrase_size_read, keyfile, keyfile_size);
1979 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1980 passphrase_size_read, &cd->hdr, &vk, cd);
1986 r = open_from_hdr_and_vk(cd, vk, name, flags);
1991 } else if (isLOOPAES(cd->type)) {
1992 r = key_from_file(cd, NULL, &passphrase_read, &passphrase_size_read,
1993 keyfile, LOOPAES_KEYFILE_MAXSIZE);
1996 r = LOOPAES_parse_keyfile(cd, &vk, &key_count,
1997 passphrase_read, passphrase_size_read);
2001 r = LOOPAES_activate(cd, name, cd->loopaes_cipher,
2002 key_count, vk, flags);
2007 crypt_safe_free(passphrase_read);
2008 crypt_free_volume_key(vk);
2013 int crypt_activate_by_volume_key(struct crypt_device *cd,
2015 const char *volume_key,
2016 size_t volume_key_size,
2019 crypt_status_info ci;
2020 struct volume_key *vk;
2023 log_dbg("Activating volume %s by volume key.", name);
2025 /* use key directly, no hash */
2026 if (isPLAIN(cd->type)) {
2027 if (!volume_key || !volume_key_size || !cd->volume_key ||
2028 volume_key_size != cd->volume_key->keylength) {
2029 log_err(cd, _("Incorrect volume key specified for plain device.\n"));
2033 return create_device_helper(cd, name, NULL,
2034 cd->plain_cipher, cd->plain_cipher_mode, NULL, volume_key, volume_key_size,
2035 cd->volume_key->keylength, 0, cd->plain_hdr.skip,
2036 cd->plain_hdr.offset, cd->plain_uuid, flags & CRYPT_ACTIVATE_READONLY, 0, 0);
2039 if (!isLUKS(cd->type)) {
2040 log_err(cd, _("Device type is not properly initialised.\n"));
2045 ci = crypt_status(NULL, name);
2046 if (ci == CRYPT_INVALID)
2048 else if (ci >= CRYPT_ACTIVE) {
2049 log_err(cd, _("Device %s already exists.\n"), name);
2054 /* If key is not provided, try to use internal key */
2056 if (!cd->volume_key) {
2057 log_err(cd, _("Volume key does not match the volume.\n"));
2060 volume_key_size = cd->volume_key->keylength;
2061 volume_key = cd->volume_key->key;
2064 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2067 r = LUKS_verify_volume_key(&cd->hdr, vk);
2070 log_err(cd, _("Volume key does not match the volume.\n"));
2073 r = open_from_hdr_and_vk(cd, vk, name, flags);
2075 crypt_free_volume_key(vk);
2080 int crypt_deactivate(struct crypt_device *cd, const char *name)
2087 log_dbg("Deactivating volume %s.", name);
2089 if (!cd && dm_init(NULL, 1) < 0)
2092 switch (crypt_status(cd, name)) {
2094 r = dm_remove_device(name, 0, 0);
2097 log_err(cd, _("Device %s is busy.\n"), name);
2100 case CRYPT_INACTIVE:
2101 log_err(cd, _("Device %s is not active.\n"), name);
2105 log_err(cd, _("Invalid device %s.\n"), name);
2115 int crypt_volume_key_get(struct crypt_device *cd,
2118 size_t *volume_key_size,
2119 const char *passphrase,
2120 size_t passphrase_size)
2122 struct volume_key *vk;
2123 char *processed_key = NULL;
2126 key_len = crypt_get_volume_key_size(cd);
2127 if (key_len > *volume_key_size) {
2128 log_err(cd, _("Volume key buffer too small.\n"));
2132 if (isPLAIN(cd->type) && cd->plain_hdr.hash) {
2133 processed_key = process_key(cd, cd->plain_hdr.hash, NULL, key_len,
2134 passphrase, passphrase_size);
2135 if (!processed_key) {
2136 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
2139 memcpy(volume_key, processed_key, key_len);
2140 *volume_key_size = key_len;
2141 crypt_safe_free(processed_key);
2145 if (isLUKS(cd->type)) {
2146 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
2147 passphrase_size, &cd->hdr, &vk, cd);
2150 memcpy(volume_key, vk->key, vk->keylength);
2151 *volume_key_size = vk->keylength;
2154 crypt_free_volume_key(vk);
2158 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
2162 int crypt_volume_key_verify(struct crypt_device *cd,
2163 const char *volume_key,
2164 size_t volume_key_size)
2166 struct volume_key *vk;
2169 if (!isLUKS(cd->type)) {
2170 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2174 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2178 r = LUKS_verify_volume_key(&cd->hdr, vk);
2181 log_err(cd, _("Volume key does not match the volume.\n"));
2183 crypt_free_volume_key(vk);
2188 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
2190 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
2191 cd->timeout = timeout_sec;
2194 void crypt_set_password_retry(struct crypt_device *cd, int tries)
2196 log_dbg("Password retry count set to %d.", tries);
2200 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2202 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
2203 cd->iteration_time = iteration_time_ms;
2206 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
2208 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
2209 cd->password_verify = password_verify ? 1 : 0;
2212 void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
2215 case CRYPT_RNG_URANDOM:
2216 case CRYPT_RNG_RANDOM:
2217 log_dbg("RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
2218 cd->rng_type = rng_type;
2222 int crypt_get_rng_type(struct crypt_device *cd)
2227 return cd->rng_type;
2230 int crypt_memory_lock(struct crypt_device *cd, int lock)
2232 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
2236 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
2240 if (!cd && dm_init(NULL, 1) < 0)
2241 return CRYPT_INVALID;
2243 r = dm_status_device(name);
2248 if (r < 0 && r != -ENODEV)
2249 return CRYPT_INVALID;
2252 return CRYPT_ACTIVE;
2257 return CRYPT_INACTIVE;
2260 static void hexprintICB(struct crypt_device *cd, char *d, int n)
2263 for(i = 0; i < n; i++)
2264 log_std(cd, "%02hhx ", (char)d[i]);
2267 int crypt_dump(struct crypt_device *cd)
2270 if (!isLUKS(cd->type)) { //FIXME
2271 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2275 log_std(cd, "LUKS header information for %s\n\n", cd->device);
2276 log_std(cd, "Version: \t%d\n", cd->hdr.version);
2277 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
2278 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
2279 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec);
2280 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
2281 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
2282 log_std(cd, "MK digest: \t");
2283 hexprintICB(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE);
2285 log_std(cd, "MK salt: \t");
2286 hexprintICB(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2);
2287 log_std(cd, "\n \t");
2288 hexprintICB(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
2290 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
2291 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
2292 for(i = 0; i < LUKS_NUMKEYS; i++) {
2293 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
2294 log_std(cd, "Key Slot %d: ENABLED\n",i);
2295 log_std(cd, "\tIterations: \t%d\n",
2296 cd->hdr.keyblock[i].passwordIterations);
2297 log_std(cd, "\tSalt: \t");
2298 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt,
2300 log_std(cd, "\n\t \t");
2301 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt +
2302 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
2305 log_std(cd, "\tKey material offset:\t%d\n",
2306 cd->hdr.keyblock[i].keyMaterialOffset);
2307 log_std(cd, "\tAF stripes: \t%d\n",
2308 cd->hdr.keyblock[i].stripes);
2311 log_std(cd, "Key Slot %d: DISABLED\n", i);
2317 const char *crypt_get_cipher(struct crypt_device *cd)
2319 if (isPLAIN(cd->type))
2320 return cd->plain_cipher;
2322 if (isLUKS(cd->type))
2323 return cd->hdr.cipherName;
2325 if (isLOOPAES(cd->type))
2326 return cd->loopaes_cipher;
2331 const char *crypt_get_cipher_mode(struct crypt_device *cd)
2333 if (isPLAIN(cd->type))
2334 return cd->plain_cipher_mode;
2336 if (isLUKS(cd->type))
2337 return cd->hdr.cipherMode;
2339 if (isLOOPAES(cd->type))
2340 return cd->loopaes_cipher_mode;
2345 const char *crypt_get_uuid(struct crypt_device *cd)
2347 if (isLUKS(cd->type))
2348 return cd->hdr.uuid;
2350 if (isPLAIN(cd->type))
2351 return cd->plain_uuid;
2353 if (isLOOPAES(cd->type))
2354 return cd->loopaes_uuid;
2359 const char *crypt_get_device_name(struct crypt_device *cd)
2364 int crypt_get_volume_key_size(struct crypt_device *cd)
2366 if (isPLAIN(cd->type) && cd->volume_key)
2367 return cd->volume_key->keylength;
2369 if (isLUKS(cd->type))
2370 return cd->hdr.keyBytes;
2372 if (isLOOPAES(cd->type))
2373 return cd->loopaes_key_size;
2378 uint64_t crypt_get_data_offset(struct crypt_device *cd)
2380 if (isPLAIN(cd->type))
2381 return cd->plain_hdr.offset;
2383 if (isLUKS(cd->type))
2384 return cd->hdr.payloadOffset;
2386 if (isLOOPAES(cd->type))
2387 return cd->loopaes_hdr.offset;
2392 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
2394 if (!isLUKS(cd->type)) {
2395 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2396 return CRYPT_SLOT_INVALID;
2399 return LUKS_keyslot_info(&cd->hdr, keyslot);
2402 int crypt_keyslot_max(const char *type)
2404 if (type && isLUKS(type))
2405 return LUKS_NUMKEYS;
2410 const char *crypt_get_type(struct crypt_device *cd)
2415 int crypt_get_active_device(struct crypt_device *cd,
2417 struct crypt_active_device *cad)
2421 r = dm_query_device(name, NULL, &cad->size, &cad->iv_offset, &cad->offset,
2422 NULL, NULL, NULL, &readonly, NULL, NULL);
2426 cad->flags = readonly ? CRYPT_ACTIVATE_READONLY : 0;