8 #include "libcryptsetup.h"
11 #include "crypto_backend.h"
17 struct volume_key *volume_key;
19 uint64_t iteration_time;
24 /* used in CRYPT_LUKS1 */
26 uint64_t PBKDF2_per_sec;
28 /* used in CRYPT_PLAIN */
29 struct crypt_params_plain plain_hdr;
31 char *plain_cipher_mode;
34 /* callbacks definitions */
35 void (*log)(int level, const char *msg, void *usrptr);
37 int (*confirm)(const char *msg, void *usrptr);
39 int (*password)(const char *msg, char *buf, size_t length, void *usrptr);
40 void *password_usrptr;
44 static void (*_default_log)(int level, const char *msg, void *usrptr) = NULL;
45 static int _debug_level = 0;
47 void crypt_set_debug_level(int level)
52 int crypt_get_debug_level()
57 void crypt_log(struct crypt_device *cd, int level, const char *msg)
60 cd->log(level, msg, cd->log_usrptr);
61 else if (_default_log)
62 _default_log(level, msg, NULL);
65 void logger(struct crypt_device *cd, int level, const char *file,
66 int line, const char *format, ...)
71 va_start(argp, format);
73 if (vasprintf(&target, format, argp) > 0) {
75 crypt_log(cd, level, target);
77 } else if (_debug_level)
78 printf("# %s:%d %s\n", file ?: "?", line, target);
80 } else if (_debug_level)
81 printf("# %s\n", target);
89 static int init_crypto(struct crypt_device *ctx)
93 r = crypt_random_init(ctx);
95 log_err(ctx, _("Cannot initialize crypto RNG backend.\n"));
99 r = crypt_backend_init(ctx);
101 log_err(ctx, _("Cannot initialize crypto backend.\n"));
107 * Password processing behaviour matrix of process_key
109 * from binary file: check if there is sufficently large key material
110 * interactive & from fd: hash if requested, otherwise crop or pad with '0'
112 static char *process_key(struct crypt_device *cd, const char *hash_name,
113 const char *key_file, size_t key_size,
114 const char *pass, size_t passLen)
121 key = crypt_safe_alloc(key_size);
122 memset(key, 0, key_size);
124 /* key is coming from binary file */
125 if (key_file && strcmp(key_file, "-")) {
126 if(passLen < key_size) {
127 log_err(cd, _("Cannot not read %d bytes from key file %s.\n"),
129 crypt_safe_free(key);
132 memcpy(key, pass, key_size);
136 /* key is coming from tty, fd or binary stdin */
138 if (crypt_plain_hash(cd, hash_name, key, key_size, pass, passLen) < 0) {
139 log_err(cd, _("Key processing error (using hash algorithm %s).\n"),
141 crypt_safe_free(key);
144 } else if (passLen > key_size) {
145 memcpy(key, pass, key_size);
147 memcpy(key, pass, passLen);
153 static int isPLAIN(const char *type)
155 return (type && !strcmp(CRYPT_PLAIN, type));
158 static int isLUKS(const char *type)
160 return (type && !strcmp(CRYPT_LUKS1, type));
163 /* keyslot helpers */
164 static int keyslot_verify_or_find_empty(struct crypt_device *cd, int *keyslot)
166 if (*keyslot == CRYPT_ANY_SLOT) {
167 *keyslot = LUKS_keyslot_find_empty(&cd->hdr);
169 log_err(cd, _("All key slots full.\n"));
174 switch (LUKS_keyslot_info(&cd->hdr, *keyslot)) {
175 case CRYPT_SLOT_INVALID:
176 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
177 *keyslot, LUKS_NUMKEYS - 1);
179 case CRYPT_SLOT_INACTIVE:
182 log_err(cd, _("Key slot %d is full, please select another one.\n"),
190 static int verify_other_keyslot(struct crypt_device *cd,
191 const char *key_file,
194 struct volume_key *vk = NULL;
195 crypt_keyslot_info ki;
197 char *password = NULL;
198 unsigned int passwordLen;
200 r = crypt_get_key(_("Enter any remaining LUKS passphrase: "),
201 &password, &passwordLen, 0, key_file, cd->timeout,
202 cd->password_verify, cd);
206 ki = crypt_keyslot_status(cd, keyIndex);
207 if (ki == CRYPT_SLOT_ACTIVE) /* Not last slot */
208 LUKS_keyslot_set(&cd->hdr, keyIndex, 0);
210 openedIndex = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT,
211 password, passwordLen,
214 if (ki == CRYPT_SLOT_ACTIVE)
215 LUKS_keyslot_set(&cd->hdr, keyIndex, 1);
220 log_verbose(cd, _("Key slot %d verified.\n"), openedIndex);
222 crypt_free_volume_key(vk);
223 crypt_safe_free(password);
227 static int find_keyslot_by_passphrase(struct crypt_device *cd,
228 const char *key_file,
231 struct volume_key *vk = NULL;
232 char *password = NULL;
233 unsigned int passwordLen;
236 r = crypt_get_key(message,&password,&passwordLen, 0, key_file,
237 cd->timeout, cd->password_verify, cd);
241 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
242 passwordLen, &cd->hdr, &vk, cd);
244 crypt_free_volume_key(vk);
245 crypt_safe_free(password);
249 static int luks_remove_helper(struct crypt_device *cd,
251 const char *other_key_file,
252 const char *key_file,
255 crypt_keyslot_info ki;
258 if (key_slot == CRYPT_ANY_SLOT) {
259 key_slot = find_keyslot_by_passphrase(cd, key_file,
260 _("Enter LUKS passphrase to be deleted: "));
266 log_std(cd, _("Key slot %d selected for deletion.\n"), key_slot);
269 ki = crypt_keyslot_status(cd, key_slot);
270 if (ki == CRYPT_SLOT_INVALID) {
271 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
272 key_slot, LUKS_NUMKEYS - 1);
276 if (ki <= CRYPT_SLOT_INACTIVE) {
277 log_err(cd, _("Key %d not active. Can't wipe.\n"), key_slot);
282 if (ki == CRYPT_SLOT_ACTIVE_LAST && cd->confirm &&
283 !(cd->confirm(_("This is the last keyslot."
284 " Device will become unusable after purging this key."),
285 cd->confirm_usrptr))) {
291 r = verify_other_keyslot(cd, other_key_file, key_slot);
296 r = crypt_keyslot_destroy(cd, key_slot);
298 return (r < 0) ? r : 0;
301 static int create_device_helper(struct crypt_device *cd,
305 const char *cipher_mode,
306 const char *key_file,
318 crypt_status_info ci;
319 char *dm_cipher = NULL;
320 char *processed_key = NULL;
326 ci = crypt_status(cd, name);
327 if (ci == CRYPT_INVALID)
330 if (reload && ci < CRYPT_ACTIVE)
333 if (!reload && ci >= CRYPT_ACTIVE) {
334 log_err(cd, _("Device %s already exists.\n"), name);
338 if (key_size < 0 || key_size > 1024) {
339 log_err(cd, _("Invalid key size %d.\n"), key_size);
343 r = device_check_and_adjust(cd, cd->device, !reload, &size, &offset, &read_only);
347 if (cipher_mode && asprintf(&dm_cipher, "%s-%s", cipher, cipher_mode) < 0)
350 processed_key = process_key(cd, hash, key_file, key_size, key, keyLen);
351 if (!processed_key) {
356 r = dm_create_device(name, cd->device, dm_cipher ?: cipher, cd->type, uuid, size, skip, offset,
357 key_size, processed_key, read_only, reload);
360 crypt_safe_free(processed_key);
364 static int open_from_hdr_and_vk(struct crypt_device *cd,
365 struct volume_key *vk,
369 uint64_t size, offset;
371 int read_only, no_uuid, r;
374 offset = crypt_get_data_offset(cd);
375 read_only = flags & CRYPT_ACTIVATE_READONLY;
376 no_uuid = flags & CRYPT_ACTIVATE_NO_UUID;
378 r = device_check_and_adjust(cd, cd->device, 1, &size, &offset, &read_only);
382 if (asprintf(&cipher, "%s-%s", crypt_get_cipher(cd),
383 crypt_get_cipher_mode(cd)) < 0)
386 r = dm_create_device(name, cd->device, cipher, cd->type,
387 no_uuid ? NULL : crypt_get_uuid(cd),
388 size, 0, offset, vk->keylength, vk->key,
394 static void log_wrapper(int level, const char *msg, void *usrptr)
396 void (*xlog)(int level, char *msg) = usrptr;
397 xlog(level, (char *)msg);
400 static int yesDialog_wrapper(const char *msg, void *usrptr)
402 int (*xyesDialog)(char *msg) = usrptr;
403 return xyesDialog((char*)msg);
406 int crypt_confirm(struct crypt_device *cd, const char *msg)
408 if (!cd || !cd->confirm)
411 return cd->confirm(msg, cd->confirm_usrptr);
414 static int key_from_terminal(struct crypt_device *cd, char *msg, char **key,
415 unsigned int *key_len, int force_verify)
421 if(!msg && asprintf(&prompt, _("Enter passphrase for %s: "),
429 *key = crypt_safe_alloc(MAX_TTY_PASSWORD_LEN);
434 r = cd->password(msg, *key, MAX_TTY_PASSWORD_LEN, cd->password_usrptr);
436 crypt_safe_free(*key);
441 r = crypt_get_key(msg, key, key_len, 0, NULL, cd->timeout,
442 (force_verify || cd->password_verify), cd);
445 return (r < 0) ? r: 0;
448 static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslot,
449 struct volume_key **vk)
451 char *passphrase_read = NULL;
452 unsigned int passphrase_size_read;
453 int r = -EINVAL, tries = cd->tries;
457 crypt_free_volume_key(*vk);
460 r = key_from_terminal(cd, NULL, &passphrase_read,
461 &passphrase_size_read, 0);
465 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
466 passphrase_size_read, &cd->hdr, vk, cd);
467 crypt_safe_free(passphrase_read);
468 passphrase_read = NULL;
469 } while (r == -EPERM && (--tries > 0));
472 crypt_free_volume_key(*vk);
476 crypt_safe_free(passphrase_read);
480 static int key_from_file(struct crypt_device *cd, char *msg,
481 char **key, unsigned int *key_len,
482 const char *key_file, size_t key_size)
484 return crypt_get_key(msg, key, key_len, key_size, key_file,
488 static int _crypt_init(struct crypt_device **cd,
490 struct crypt_options *options,
491 int load, int need_dm)
495 /* if it is plain device and mapping table is being reloaded
496 initialize it by name*/
497 init_by_name = (type && !strcmp(type, CRYPT_PLAIN) && load);
499 /* Some of old API calls do not require DM in kernel,
500 fake initialisation by initialise it with kernel_check disabled */
502 (void)dm_init(NULL, 0);
504 r = crypt_init_by_name(cd, options->name);
506 r = crypt_init(cd, options->device);
513 crypt_set_log_callback(*cd, log_wrapper, options->icb->log);
514 crypt_set_confirm_callback(*cd, yesDialog_wrapper, options->icb->yesDialog);
516 crypt_set_timeout(*cd, options->timeout);
517 crypt_set_password_retry(*cd, options->tries);
518 crypt_set_iterarion_time(*cd, options->iteration_time ?: 1000);
519 crypt_set_password_verify(*cd, options->flags & CRYPT_FLAG_VERIFY);
521 if (load && !init_by_name)
522 r = crypt_load(*cd, type, NULL);
524 if (!r && type && !(*cd)->type) {
525 (*cd)->type = strdup(type);
536 void crypt_set_log_callback(struct crypt_device *cd,
537 void (*log)(int level, const char *msg, void *usrptr),
544 cd->log_usrptr = usrptr;
548 void crypt_set_confirm_callback(struct crypt_device *cd,
549 int (*confirm)(const char *msg, void *usrptr),
552 cd->confirm = confirm;
553 cd->confirm_usrptr = usrptr;
556 void crypt_set_password_callback(struct crypt_device *cd,
557 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
560 cd->password = password;
561 cd->password_usrptr = usrptr;
564 /* OPTIONS: name, cipher, device, hash, key_file, key_size, key_slot,
565 * offset, size, skip, timeout, tries, passphrase_fd (ignored),
567 static int crypt_create_and_update_device(struct crypt_options *options, int update)
569 struct crypt_device *cd = NULL;
574 r = _crypt_init(&cd, CRYPT_PLAIN, options, 0, 1);
578 r = crypt_get_key(_("Enter passphrase: "), &key, &keyLen, options->key_size,
579 options->key_file, cd->timeout, cd->password_verify, cd);
581 r = create_device_helper(cd, options->name, options->hash,
582 options->cipher, NULL, options->key_file, key, keyLen,
583 options->key_size, options->size, options->skip,
584 options->offset, NULL, options->flags & CRYPT_FLAG_READONLY,
585 options->flags, update);
587 crypt_safe_free(key);
592 int crypt_create_device(struct crypt_options *options)
594 return crypt_create_and_update_device(options, 0);
597 int crypt_update_device(struct crypt_options *options)
599 return crypt_create_and_update_device(options, 1);
602 /* OPTIONS: name, size, icb */
603 int crypt_resize_device(struct crypt_options *options)
605 struct crypt_device *cd = NULL;
606 char *device = NULL, *cipher = NULL, *uuid = NULL, *key = NULL;
608 uint64_t size, skip, offset;
609 int key_size, read_only, r;
611 log_dbg("Resizing device %s to %" PRIu64 " sectors.", options->name, options->size);
613 if (dm_init(NULL, 1) < 0)
616 r = dm_query_device(options->name, &device, &size, &skip, &offset,
617 &cipher, &key_size, &key, &read_only, NULL, &uuid);
619 log_err(NULL, _("Device %s is not active.\n"), options->name);
623 /* Try to determine type of device from UUID */
626 if (!strncmp(uuid, CRYPT_PLAIN, strlen(CRYPT_PLAIN))) {
630 } else if (!strncmp(uuid, CRYPT_LUKS1, strlen(CRYPT_LUKS1)))
634 if (!options->device)
635 options->device = device;
637 r = _crypt_init(&cd, type, options, 1, 1);
641 size = options->size;
642 r = device_check_and_adjust(cd, device, 0, &size, &offset, &read_only);
646 r = dm_create_device(options->name, device, cipher, type,
647 crypt_get_uuid(cd), size, skip, offset,
648 key_size, key, read_only, 1);
650 crypt_safe_free(key);
652 if (options->device == device)
653 options->device = NULL;
661 /* OPTIONS: name, icb */
662 int crypt_query_device(struct crypt_options *options)
666 log_dbg("Query device %s.", options->name);
668 if (dm_init(NULL, 1) < 0)
671 r = dm_status_device(options->name);
675 r = dm_query_device(options->name, (char **)&options->device, &options->size,
676 &options->skip, &options->offset, (char **)&options->cipher,
677 &options->key_size, NULL, &read_only, NULL, NULL);
680 options->flags |= CRYPT_FLAG_READONLY;
682 options->flags |= CRYPT_FLAG_FREE_DEVICE;
683 options->flags |= CRYPT_FLAG_FREE_CIPHER;
695 /* OPTIONS: name, icb */
696 int crypt_remove_device(struct crypt_options *options)
698 struct crypt_device *cd = NULL;
701 r = crypt_init_by_name(&cd, options->name);
703 r = crypt_deactivate(cd, options->name);
710 /* OPTIONS: device, cipher, hash, align_payload, key_size (master key), key_slot
711 * new_key_file, iteration_time, timeout, flags, icb */
712 int crypt_luksFormat(struct crypt_options *options)
714 char cipherName[LUKS_CIPHERNAME_L];
715 char cipherMode[LUKS_CIPHERMODE_L];
717 unsigned int passwordLen;
718 struct crypt_device *cd = NULL;
719 struct crypt_params_luks1 cp = {
720 .hash = options->hash,
721 .data_alignment = options->align_payload
725 r = crypt_parse_name_and_mode(options->cipher, cipherName, NULL, cipherMode);
727 log_err(cd, _("No known cipher specification pattern detected.\n"));
731 if ((r = _crypt_init(&cd, CRYPT_LUKS1, options, 0, 1)))
734 if (options->key_slot >= LUKS_NUMKEYS && options->key_slot != CRYPT_ANY_SLOT) {
735 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
736 options->key_slot, LUKS_NUMKEYS - 1);
741 r = crypt_get_key(_("Enter LUKS passphrase: "), &password, &passwordLen, 0,
742 options->new_key_file, cd->timeout, cd->password_verify, cd);
747 r = crypt_format(cd, CRYPT_LUKS1, cipherName, cipherMode,
748 NULL, NULL, options->key_size, &cp);
752 /* Add keyslot using internally stored volume key generated during format */
753 r = crypt_keyslot_add_by_volume_key(cd, options->key_slot, NULL, 0,
754 password, passwordLen);
757 crypt_safe_free(password);
758 return (r < 0) ? r : 0;
761 /* OPTIONS: name, device, key_size, key_file, timeout, tries, flags, icb */
762 int crypt_luksOpen(struct crypt_options *options)
764 struct crypt_device *cd = NULL;
771 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
775 if (options->flags & CRYPT_FLAG_READONLY)
776 flags |= CRYPT_ACTIVATE_READONLY;
778 if (options->flags & CRYPT_FLAG_NON_EXCLUSIVE_ACCESS)
779 flags |= CRYPT_ACTIVATE_NO_UUID;
781 if (options->key_file)
782 r = crypt_activate_by_keyfile(cd, options->name,
783 CRYPT_ANY_SLOT, options->key_file, options->key_size,
786 r = crypt_activate_by_passphrase(cd, options->name,
787 CRYPT_ANY_SLOT, options->passphrase,
788 options->passphrase ? strlen(options->passphrase) : 0,
792 return (r < 0) ? r : 0;
795 /* OPTIONS: device, keys_slot, key_file, timeout, flags, icb */
796 int crypt_luksKillSlot(struct crypt_options *options)
798 struct crypt_device *cd = NULL;
801 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
805 r = luks_remove_helper(cd, options->key_slot, options->key_file, NULL,
806 options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
809 return (r < 0) ? r : 0;
812 /* OPTIONS: device, new_key_file, key_file, timeout, flags, icb */
813 int crypt_luksRemoveKey(struct crypt_options *options)
815 struct crypt_device *cd = NULL;
818 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
822 r = luks_remove_helper(cd, CRYPT_ANY_SLOT, options->key_file, options->new_key_file,
823 options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
826 return (r < 0) ? r : 0;
830 /* OPTIONS: device, new_key_file, key_file, key_slot, flags,
831 iteration_time, timeout, icb */
832 int crypt_luksAddKey(struct crypt_options *options)
834 struct crypt_device *cd = NULL;
837 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
841 if (options->key_file || options->new_key_file)
842 r = crypt_keyslot_add_by_keyfile(cd, options->key_slot,
843 options->key_file, 0,
844 options->new_key_file, 0);
846 r = crypt_keyslot_add_by_passphrase(cd, options->key_slot,
850 return (r < 0) ? r : 0;
853 /* OPTIONS: device, icb */
854 int crypt_luksUUID(struct crypt_options *options)
856 struct crypt_device *cd = NULL;
860 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
864 uuid = (char *)crypt_get_uuid(cd);
865 log_std(cd, uuid ?: "");
871 /* OPTIONS: device, icb */
872 int crypt_isLuks(struct crypt_options *options)
874 struct crypt_device *cd = NULL;
877 log_dbg("Check device %s for LUKS header.", options->device);
883 r = crypt_init(&cd, options->device);
887 /* Do print fail here, no need to crypt_load() */
888 r = LUKS_read_phdr(cd->device, &cd->hdr, 0, cd) ? -EINVAL : 0;
894 /* OPTIONS: device, icb */
895 int crypt_luksDump(struct crypt_options *options)
897 struct crypt_device *cd = NULL;
900 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
910 void crypt_get_error(char *buf, size_t size)
912 const char *error = get_error();
914 if (!buf || size < 1)
917 strncpy(buf, error, size - 1);
918 buf[size - 1] = '\0';
924 void crypt_put_options(struct crypt_options *options)
926 if (options->flags & CRYPT_FLAG_FREE_DEVICE) {
927 free((char *)options->device);
928 options->device = NULL;
929 options->flags &= ~CRYPT_FLAG_FREE_DEVICE;
931 if (options->flags & CRYPT_FLAG_FREE_CIPHER) {
932 free((char *)options->cipher);
933 options->cipher = NULL;
934 options->flags &= ~CRYPT_FLAG_FREE_CIPHER;
938 const char *crypt_get_dir(void)
943 /////////////////////////////////
948 int crypt_init(struct crypt_device **cd, const char *device)
950 struct crypt_device *h = NULL;
955 log_dbg("Allocating crypt device %s context.", device);
957 if (device && !device_ready(NULL, device, O_RDONLY))
960 if (!(h = malloc(sizeof(struct crypt_device))))
963 memset(h, 0, sizeof(*h));
966 h->device = strdup(device);
974 if (dm_init(h, 1) < 0) {
979 h->iteration_time = 1000;
980 h->password_verify = 0;
982 h->rng_type = crypt_random_default_key_rng();
987 int crypt_init_by_name(struct crypt_device **cd, const char *name)
989 crypt_status_info ci;
990 struct crypt_active_device cad;
991 char *device = NULL, *cipher_full = NULL, *device_uuid = NULL;
992 char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
997 log_dbg("Allocating crypt device context by device %s.", name);
999 ci = crypt_status(NULL, name);
1000 if (ci == CRYPT_INVALID)
1003 if (ci < CRYPT_ACTIVE) {
1004 log_err(NULL, _("Device %s is not active.\n"), name);
1008 r = dm_query_device(name, &device, &cad.size, &cad.iv_offset, &cad.offset,
1009 &cipher_full, &key_size, &key, NULL, NULL,
1014 /* Underlying device disappeared but mapping still active */
1016 log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
1020 r = crypt_init(cd, device);
1024 /* Try to initialise basic parameters from active device */
1026 if (!strncmp(CRYPT_PLAIN, device_uuid, sizeof(CRYPT_PLAIN)-1)) {
1027 (*cd)->type = strdup(CRYPT_PLAIN);
1028 (*cd)->plain_uuid = strdup(device_uuid);
1029 (*cd)->plain_hdr.hash = NULL; /* no way to get this */
1030 (*cd)->plain_hdr.offset = cad.offset;
1031 (*cd)->plain_hdr.skip = cad.iv_offset;
1032 (*cd)->volume_key = crypt_alloc_volume_key(key_size, key);
1033 if (!(*cd)->volume_key) {
1038 r = crypt_parse_name_and_mode(cipher_full, cipher, NULL, cipher_mode);
1040 (*cd)->plain_cipher = strdup(cipher);
1041 (*cd)->plain_cipher_mode = strdup(cipher_mode);
1043 } else if (!strncmp(CRYPT_LUKS1, device_uuid, sizeof(CRYPT_LUKS1)-1)) {
1045 if (crypt_load(*cd, CRYPT_LUKS1, NULL) < 0 ||
1046 crypt_volume_key_verify(*cd, key, key_size) < 0) {
1047 log_dbg("LUKS device header does not match active device.");
1051 (*cd)->volume_key = crypt_alloc_volume_key(key_size, key);
1052 if (!(*cd)->volume_key) {
1059 log_dbg("Active device has no UUID set, some parameters are not set.");
1066 crypt_safe_free(key);
1073 static int _crypt_format_plain(struct crypt_device *cd,
1075 const char *cipher_mode,
1077 size_t volume_key_size,
1078 struct crypt_params_plain *params)
1080 if (!cipher || !cipher_mode) {
1081 log_err(cd, _("Invalid plain crypt parameters.\n"));
1085 if (volume_key_size > 1024) {
1086 log_err(cd, _("Invalid key size.\n"));
1090 cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
1091 if (!cd->volume_key)
1094 cd->plain_cipher = strdup(cipher);
1095 cd->plain_cipher_mode = strdup(cipher_mode);
1098 cd->plain_uuid = strdup(uuid);
1100 if (params && params->hash)
1101 cd->plain_hdr.hash = strdup(params->hash);
1103 cd->plain_hdr.offset = params ? params->offset : 0;
1104 cd->plain_hdr.skip = params ? params->skip : 0;
1106 if (!cd->plain_cipher || !cd->plain_cipher_mode)
1112 static int _crypt_format_luks1(struct crypt_device *cd,
1114 const char *cipher_mode,
1116 const char *volume_key,
1117 size_t volume_key_size,
1118 struct crypt_params_luks1 *params)
1121 unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
1122 unsigned long alignment_offset = 0;
1125 log_err(cd, _("Can't format LUKS without device.\n"));
1130 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
1133 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
1138 if (params && params->data_alignment)
1139 required_alignment = params->data_alignment * SECTOR_SIZE;
1141 get_topology_alignment(cd->device, &required_alignment,
1142 &alignment_offset, DEFAULT_DISK_ALIGNMENT);
1144 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
1145 (params && params->hash) ? params->hash : "sha1",
1147 required_alignment / SECTOR_SIZE,
1148 alignment_offset / SECTOR_SIZE,
1149 cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1153 /* Wipe first 8 sectors - fs magic numbers etc. */
1154 r = wipe_device_header(cd->device, 8);
1157 log_err(cd, _("Cannot format device %s which is still in use.\n"),
1160 log_err(cd, _("Cannot wipe header on device %s.\n"),
1166 r = LUKS_write_phdr(cd->device, &cd->hdr, cd);
1171 int crypt_format(struct crypt_device *cd,
1174 const char *cipher_mode,
1176 const char *volume_key,
1177 size_t volume_key_size,
1185 log_dbg("Formatting device %s as type %s.", cd->device ?: "(none)", type);
1187 r = init_crypto(cd);
1192 r = _crypt_format_plain(cd, cipher, cipher_mode,
1193 uuid, volume_key_size, params);
1194 else if (isLUKS(type))
1195 r = _crypt_format_luks1(cd, cipher, cipher_mode,
1196 uuid, volume_key, volume_key_size, params);
1198 /* FIXME: allow plugins here? */
1199 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
1203 if (!r && !(cd->type = strdup(type)))
1207 crypt_free_volume_key(cd->volume_key);
1208 cd->volume_key = NULL;
1214 int crypt_load(struct crypt_device *cd,
1215 const char *requested_type,
1218 struct luks_phdr hdr;
1221 log_dbg("Trying to load %s crypt type from device %s.",
1222 requested_type ?: "any", cd->device ?: "(none)");
1227 if (requested_type && !isLUKS(requested_type))
1230 r = init_crypto(cd);
1234 r = LUKS_read_phdr(cd->device, &hdr, 1, cd);
1237 memcpy(&cd->hdr, &hdr, sizeof(hdr));
1238 cd->type = strdup(CRYPT_LUKS1);
1246 int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
1248 char *device = NULL, *cipher = NULL, *uuid = NULL, *key = NULL;
1249 uint64_t size, skip, offset;
1250 int key_size, read_only, r;
1252 /* Device context type must be initialised */
1253 if (!cd->type || !crypt_get_uuid(cd))
1256 r = dm_query_device(name, &device, &size, &skip, &offset,
1257 &cipher, &key_size, &key, &read_only, NULL, &uuid);
1259 log_err(NULL, _("Device %s is not active.\n"), name);
1268 r = device_check_and_adjust(cd, device, 0, &new_size, &offset, &read_only);
1272 if (new_size == size) {
1273 log_dbg("Device has already requested size %" PRIu64
1279 log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
1281 r = dm_create_device(name, device, cipher, cd->type,
1282 crypt_get_uuid(cd), new_size, skip, offset,
1283 key_size, key, read_only, 1);
1285 crypt_safe_free(key);
1293 int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
1295 if (!isLUKS(cd->type)) {
1296 log_err(cd, _("This operation is not supported for this device type.\n"));
1300 if (uuid && !strncmp(uuid, cd->hdr.uuid, sizeof(cd->hdr.uuid))) {
1301 log_dbg("UUID is the same as requested (%s) for device %s.",
1307 log_dbg("Requested new UUID change to %s for %s.", uuid, cd->device);
1309 log_dbg("Requested new UUID refresh for %s.", cd->device);
1311 if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
1314 return LUKS_hdr_uuid_set(cd->device, &cd->hdr, uuid, cd);
1317 int crypt_header_backup(struct crypt_device *cd,
1318 const char *requested_type,
1319 const char *backup_file)
1323 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1326 r = init_crypto(cd);
1330 log_dbg("Requested header backup of device %s (%s) to "
1331 "file %s.", cd->device, requested_type, backup_file);
1333 return LUKS_hdr_backup(backup_file, cd->device, &cd->hdr, cd);
1336 int crypt_header_restore(struct crypt_device *cd,
1337 const char *requested_type,
1338 const char *backup_file)
1342 if (requested_type && !isLUKS(requested_type))
1345 /* Some hash functions need initialized gcrypt library */
1346 r = init_crypto(cd);
1350 log_dbg("Requested header restore to device %s (%s) from "
1351 "file %s.", cd->device, requested_type, backup_file);
1353 return LUKS_hdr_restore(backup_file, cd->device, &cd->hdr, cd);
1356 void crypt_free(struct crypt_device *cd)
1359 log_dbg("Releasing crypt device %s context.", cd->device);
1362 crypt_free_volume_key(cd->volume_key);
1367 /* used in plain device only */
1368 free((char*)cd->plain_hdr.hash);
1369 free(cd->plain_cipher);
1370 free(cd->plain_cipher_mode);
1371 free(cd->plain_uuid);
1377 int crypt_suspend(struct crypt_device *cd,
1380 crypt_status_info ci;
1381 int r, suspended = 0;
1383 log_dbg("Suspending volume %s.", name);
1385 ci = crypt_status(NULL, name);
1386 if (ci < CRYPT_ACTIVE) {
1387 log_err(cd, _("Volume %s is not active.\n"), name);
1391 if (!cd && dm_init(NULL, 1) < 0)
1394 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1395 NULL, NULL, NULL, NULL, &suspended, NULL);
1400 log_err(cd, _("Volume %s is already suspended.\n"), name);
1405 r = dm_suspend_and_wipe_key(name);
1407 log_err(cd, "Suspend is not supported for device %s.\n", name);
1409 log_err(cd, "Error during suspending device %s.\n", name);
1416 int crypt_resume_by_passphrase(struct crypt_device *cd,
1419 const char *passphrase,
1420 size_t passphrase_size)
1422 struct volume_key *vk = NULL;
1423 int r, suspended = 0;
1425 log_dbg("Resuming volume %s.", name);
1427 if (!isLUKS(cd->type)) {
1428 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1433 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1434 NULL, NULL, NULL, NULL, &suspended, NULL);
1439 log_err(cd, _("Volume %s is not suspended.\n"), name);
1444 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1445 passphrase_size, &cd->hdr, &vk, cd);
1447 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1451 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1453 log_err(cd, "Resume is not supported for device %s.\n", name);
1455 log_err(cd, "Error during resuming device %s.\n", name);
1459 crypt_free_volume_key(vk);
1460 return r < 0 ? r : keyslot;
1463 int crypt_resume_by_keyfile(struct crypt_device *cd,
1466 const char *keyfile,
1467 size_t keyfile_size)
1469 struct volume_key *vk = NULL;
1470 char *passphrase_read = NULL;
1471 unsigned int passphrase_size_read;
1472 int r, suspended = 0;
1474 log_dbg("Resuming volume %s.", name);
1476 if (!isLUKS(cd->type)) {
1477 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1482 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1483 NULL, NULL, NULL, NULL, &suspended, NULL);
1488 log_err(cd, _("Volume %s is not suspended.\n"), name);
1495 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1496 &passphrase_size_read, keyfile, keyfile_size);
1500 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1501 passphrase_size_read, &cd->hdr, &vk, cd);
1506 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1508 log_err(cd, "Error during resuming device %s.\n", name);
1510 crypt_safe_free(passphrase_read);
1511 crypt_free_volume_key(vk);
1512 return r < 0 ? r : keyslot;
1515 // slot manipulation
1516 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1517 int keyslot, // -1 any
1518 const char *passphrase, // NULL -> terminal
1519 size_t passphrase_size,
1520 const char *new_passphrase, // NULL -> terminal
1521 size_t new_passphrase_size)
1523 struct volume_key *vk = NULL;
1524 char *password = NULL, *new_password = NULL;
1525 unsigned int passwordLen, new_passwordLen;
1528 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1529 "new passphrase %sprovided.",
1530 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1532 if (!isLUKS(cd->type)) {
1533 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1537 r = keyslot_verify_or_find_empty(cd, &keyslot);
1541 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1542 /* No slots used, try to use pre-generated key in header */
1543 if (cd->volume_key) {
1544 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1545 r = vk ? 0 : -ENOMEM;
1547 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1550 } else if (passphrase) {
1551 /* Passphrase provided, use it to unlock existing keyslot */
1552 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, passphrase,
1553 passphrase_size, &cd->hdr, &vk, cd);
1555 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1556 r = key_from_terminal(cd, _("Enter any passphrase: "),
1557 &password, &passwordLen, 0);
1561 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
1562 passwordLen, &cd->hdr, &vk, cd);
1563 crypt_safe_free(password);
1569 if (new_passphrase) {
1570 new_password = (char *)new_passphrase;
1571 new_passwordLen = new_passphrase_size;
1573 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1574 &new_password, &new_passwordLen, 1);
1579 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1580 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1585 if (!new_passphrase)
1586 crypt_safe_free(new_password);
1587 crypt_free_volume_key(vk);
1588 return r ?: keyslot;
1591 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1593 const char *keyfile,
1594 size_t keyfile_size,
1595 const char *new_keyfile,
1596 size_t new_keyfile_size)
1598 struct volume_key *vk = NULL;
1599 char *password = NULL; unsigned int passwordLen;
1600 char *new_password = NULL; unsigned int new_passwordLen;
1603 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1604 keyfile ?: "[none]", new_keyfile ?: "[none]");
1606 if (!isLUKS(cd->type)) {
1607 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1611 r = keyslot_verify_or_find_empty(cd, &keyslot);
1615 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1616 /* No slots used, try to use pre-generated key in header */
1617 if (cd->volume_key) {
1618 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1619 r = vk ? 0 : -ENOMEM;
1621 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1625 /* Read password from file of (if NULL) from terminal */
1627 r = key_from_file(cd, _("Enter any passphrase: "),
1628 &password, &passwordLen,
1629 keyfile, keyfile_size);
1631 r = key_from_terminal(cd, _("Enter any passphrase: "),
1632 &password, &passwordLen, 0);
1636 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password, passwordLen,
1644 r = key_from_file(cd, _("Enter new passphrase for key slot: "),
1645 &new_password, &new_passwordLen, new_keyfile,
1648 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1649 &new_password, &new_passwordLen, 1);
1653 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1654 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1656 crypt_safe_free(password);
1657 crypt_safe_free(new_password);
1658 crypt_free_volume_key(vk);
1659 return r < 0 ? r : keyslot;
1662 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1664 const char *volume_key,
1665 size_t volume_key_size,
1666 const char *passphrase,
1667 size_t passphrase_size)
1669 struct volume_key *vk = NULL;
1671 char *new_password = NULL; unsigned int new_passwordLen;
1673 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1675 if (!isLUKS(cd->type)) {
1676 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1681 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1682 else if (cd->volume_key)
1683 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1688 r = LUKS_verify_volume_key(&cd->hdr, vk);
1690 log_err(cd, _("Volume key does not match the volume.\n"));
1694 r = keyslot_verify_or_find_empty(cd, &keyslot);
1699 r = key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1700 &new_password, &new_passwordLen, 1);
1703 passphrase = new_password;
1704 passphrase_size = new_passwordLen;
1707 r = LUKS_set_key(cd->device, keyslot, passphrase, passphrase_size,
1708 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1710 crypt_safe_free(new_password);
1711 crypt_free_volume_key(vk);
1712 return (r < 0) ? r : keyslot;
1715 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1717 crypt_keyslot_info ki;
1719 log_dbg("Destroying keyslot %d.", keyslot);
1721 if (!isLUKS(cd->type)) {
1722 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1726 ki = crypt_keyslot_status(cd, keyslot);
1727 if (ki == CRYPT_SLOT_INVALID) {
1728 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1732 if (ki == CRYPT_SLOT_INACTIVE) {
1733 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1737 return LUKS_del_key(cd->device, keyslot, &cd->hdr, cd);
1740 // activation/deactivation of device mapping
1741 int crypt_activate_by_passphrase(struct crypt_device *cd,
1744 const char *passphrase,
1745 size_t passphrase_size,
1748 crypt_status_info ci;
1749 struct volume_key *vk = NULL;
1750 char *read_passphrase = NULL;
1751 unsigned int passphraseLen = 0;
1754 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1755 name ? "Activating" : "Checking", name ?: "",
1756 keyslot, passphrase ? "" : "[none] ");
1759 ci = crypt_status(NULL, name);
1760 if (ci == CRYPT_INVALID)
1762 else if (ci >= CRYPT_ACTIVE) {
1763 log_err(cd, _("Device %s already exists.\n"), name);
1768 /* plain, use hashed passphrase */
1769 if (isPLAIN(cd->type)) {
1771 r = key_from_terminal(cd, NULL, &read_passphrase,
1775 passphrase = read_passphrase;
1776 passphrase_size = passphraseLen;
1778 r = create_device_helper(cd, name, cd->plain_hdr.hash,
1779 cd->plain_cipher, cd->plain_cipher_mode,
1780 NULL, passphrase, passphrase_size,
1781 cd->volume_key->keylength, 0,
1782 cd->plain_hdr.skip, cd->plain_hdr.offset,
1784 flags & CRYPT_ACTIVATE_READONLY, 0, 0);
1786 } else if (isLUKS(cd->type)) {
1787 /* provided passphrase, do not retry */
1789 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1790 passphrase_size, &cd->hdr, &vk, cd);
1792 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1797 r = open_from_hdr_and_vk(cd, vk, name, flags);
1802 crypt_safe_free(read_passphrase);
1803 crypt_free_volume_key(vk);
1805 return r < 0 ? r : keyslot;
1808 int crypt_activate_by_keyfile(struct crypt_device *cd,
1811 const char *keyfile,
1812 size_t keyfile_size,
1815 crypt_status_info ci;
1816 struct volume_key *vk = NULL;
1817 char *passphrase_read = NULL;
1818 unsigned int passphrase_size_read;
1821 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1822 name ?: "", keyslot, keyfile ?: "[none]");
1825 ci = crypt_status(NULL, name);
1826 if (ci == CRYPT_INVALID)
1828 else if (ci >= CRYPT_ACTIVE) {
1829 log_err(cd, _("Device %s already exists.\n"), name);
1837 if (isPLAIN(cd->type)) {
1838 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1839 &passphrase_size_read, keyfile, keyfile_size);
1842 r = create_device_helper(cd, name, cd->plain_hdr.hash,
1843 cd->plain_cipher, cd->plain_cipher_mode,
1844 NULL, passphrase_read, passphrase_size_read,
1845 cd->volume_key->keylength, 0,
1846 cd->plain_hdr.skip, cd->plain_hdr.offset,
1848 flags & CRYPT_ACTIVATE_READONLY, 0, 0);
1849 } else if (isLUKS(cd->type)) {
1850 r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1851 &passphrase_size_read, keyfile, keyfile_size);
1854 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1855 passphrase_size_read, &cd->hdr, &vk, cd);
1861 r = open_from_hdr_and_vk(cd, vk, name, flags);
1870 crypt_safe_free(passphrase_read);
1871 crypt_free_volume_key(vk);
1876 int crypt_activate_by_volume_key(struct crypt_device *cd,
1878 const char *volume_key,
1879 size_t volume_key_size,
1882 crypt_status_info ci;
1883 struct volume_key *vk;
1886 log_dbg("Activating volume %s by volume key.", name);
1888 /* use key directly, no hash */
1889 if (isPLAIN(cd->type)) {
1890 if (!volume_key || !volume_key_size || !cd->volume_key ||
1891 volume_key_size != cd->volume_key->keylength) {
1892 log_err(cd, _("Incorrect volume key specified for plain device.\n"));
1896 return create_device_helper(cd, name, NULL,
1897 cd->plain_cipher, cd->plain_cipher_mode, NULL, volume_key, volume_key_size,
1898 cd->volume_key->keylength, 0, cd->plain_hdr.skip,
1899 cd->plain_hdr.offset, cd->plain_uuid, flags & CRYPT_ACTIVATE_READONLY, 0, 0);
1902 if (!isLUKS(cd->type)) {
1903 log_err(cd, _("Device type is not properly initialised.\n"));
1908 ci = crypt_status(NULL, name);
1909 if (ci == CRYPT_INVALID)
1911 else if (ci >= CRYPT_ACTIVE) {
1912 log_err(cd, _("Device %s already exists.\n"), name);
1917 /* If key is not provided, try to use internal key */
1919 if (!cd->volume_key) {
1920 log_err(cd, _("Volume key does not match the volume.\n"));
1923 volume_key_size = cd->volume_key->keylength;
1924 volume_key = cd->volume_key->key;
1927 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1930 r = LUKS_verify_volume_key(&cd->hdr, vk);
1933 log_err(cd, _("Volume key does not match the volume.\n"));
1936 r = open_from_hdr_and_vk(cd, vk, name, flags);
1938 crypt_free_volume_key(vk);
1943 int crypt_deactivate(struct crypt_device *cd, const char *name)
1950 log_dbg("Deactivating volume %s.", name);
1952 if (!cd && dm_init(NULL, 1) < 0)
1955 switch (crypt_status(cd, name)) {
1957 r = dm_remove_device(name, 0, 0);
1960 log_err(cd, _("Device %s is busy.\n"), name);
1963 case CRYPT_INACTIVE:
1964 log_err(cd, _("Device %s is not active.\n"), name);
1968 log_err(cd, _("Invalid device %s.\n"), name);
1978 int crypt_volume_key_get(struct crypt_device *cd,
1981 size_t *volume_key_size,
1982 const char *passphrase,
1983 size_t passphrase_size)
1985 struct volume_key *vk;
1986 char *processed_key = NULL;
1989 key_len = crypt_get_volume_key_size(cd);
1990 if (key_len > *volume_key_size) {
1991 log_err(cd, _("Volume key buffer too small.\n"));
1995 if (isPLAIN(cd->type) && cd->plain_hdr.hash) {
1996 processed_key = process_key(cd, cd->plain_hdr.hash, NULL, key_len,
1997 passphrase, passphrase_size);
1998 if (!processed_key) {
1999 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
2002 memcpy(volume_key, processed_key, key_len);
2003 *volume_key_size = key_len;
2004 crypt_safe_free(processed_key);
2008 if (isLUKS(cd->type)) {
2009 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
2010 passphrase_size, &cd->hdr, &vk, cd);
2013 memcpy(volume_key, vk->key, vk->keylength);
2014 *volume_key_size = vk->keylength;
2017 crypt_free_volume_key(vk);
2021 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
2025 int crypt_volume_key_verify(struct crypt_device *cd,
2026 const char *volume_key,
2027 size_t volume_key_size)
2029 struct volume_key *vk;
2032 if (!isLUKS(cd->type)) {
2033 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2037 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2041 r = LUKS_verify_volume_key(&cd->hdr, vk);
2044 log_err(cd, _("Volume key does not match the volume.\n"));
2046 crypt_free_volume_key(vk);
2051 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
2053 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
2054 cd->timeout = timeout_sec;
2057 void crypt_set_password_retry(struct crypt_device *cd, int tries)
2059 log_dbg("Password retry count set to %d.", tries);
2063 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2065 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
2066 cd->iteration_time = iteration_time_ms;
2069 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
2071 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
2072 cd->password_verify = password_verify ? 1 : 0;
2075 void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
2078 case CRYPT_RNG_URANDOM:
2079 case CRYPT_RNG_RANDOM:
2080 log_dbg("RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
2081 cd->rng_type = rng_type;
2085 int crypt_get_rng_type(struct crypt_device *cd)
2090 return cd->rng_type;
2093 int crypt_memory_lock(struct crypt_device *cd, int lock)
2095 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
2099 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
2103 if (!cd && dm_init(NULL, 1) < 0)
2104 return CRYPT_INVALID;
2106 r = dm_status_device(name);
2111 if (r < 0 && r != -ENODEV)
2112 return CRYPT_INVALID;
2115 return CRYPT_ACTIVE;
2120 return CRYPT_INACTIVE;
2123 static void hexprintICB(struct crypt_device *cd, char *d, int n)
2126 for(i = 0; i < n; i++)
2127 log_std(cd, "%02hhx ", (char)d[i]);
2130 int crypt_dump(struct crypt_device *cd)
2133 if (!isLUKS(cd->type)) { //FIXME
2134 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2138 log_std(cd, "LUKS header information for %s\n\n", cd->device);
2139 log_std(cd, "Version: \t%d\n", cd->hdr.version);
2140 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
2141 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
2142 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec);
2143 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
2144 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
2145 log_std(cd, "MK digest: \t");
2146 hexprintICB(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE);
2148 log_std(cd, "MK salt: \t");
2149 hexprintICB(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2);
2150 log_std(cd, "\n \t");
2151 hexprintICB(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
2153 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
2154 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
2155 for(i = 0; i < LUKS_NUMKEYS; i++) {
2156 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
2157 log_std(cd, "Key Slot %d: ENABLED\n",i);
2158 log_std(cd, "\tIterations: \t%d\n",
2159 cd->hdr.keyblock[i].passwordIterations);
2160 log_std(cd, "\tSalt: \t");
2161 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt,
2163 log_std(cd, "\n\t \t");
2164 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt +
2165 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
2168 log_std(cd, "\tKey material offset:\t%d\n",
2169 cd->hdr.keyblock[i].keyMaterialOffset);
2170 log_std(cd, "\tAF stripes: \t%d\n",
2171 cd->hdr.keyblock[i].stripes);
2174 log_std(cd, "Key Slot %d: DISABLED\n", i);
2180 const char *crypt_get_cipher(struct crypt_device *cd)
2182 if (isPLAIN(cd->type))
2183 return cd->plain_cipher;
2185 if (isLUKS(cd->type))
2186 return cd->hdr.cipherName;
2191 const char *crypt_get_cipher_mode(struct crypt_device *cd)
2193 if (isPLAIN(cd->type))
2194 return cd->plain_cipher_mode;
2196 if (isLUKS(cd->type))
2197 return cd->hdr.cipherMode;
2202 const char *crypt_get_uuid(struct crypt_device *cd)
2204 if (isLUKS(cd->type))
2205 return cd->hdr.uuid;
2207 if (isPLAIN(cd->type))
2208 return cd->plain_uuid;
2213 const char *crypt_get_device_name(struct crypt_device *cd)
2218 int crypt_get_volume_key_size(struct crypt_device *cd)
2220 if (isPLAIN(cd->type) && cd->volume_key)
2221 return cd->volume_key->keylength;
2223 if (isLUKS(cd->type))
2224 return cd->hdr.keyBytes;
2229 uint64_t crypt_get_data_offset(struct crypt_device *cd)
2231 if (isPLAIN(cd->type))
2232 return cd->plain_hdr.offset;
2234 if (isLUKS(cd->type))
2235 return cd->hdr.payloadOffset;
2240 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
2242 if (!isLUKS(cd->type)) {
2243 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2244 return CRYPT_SLOT_INVALID;
2247 return LUKS_keyslot_info(&cd->hdr, keyslot);
2250 int crypt_keyslot_max(const char *type)
2252 if (type && isLUKS(type))
2253 return LUKS_NUMKEYS;
2258 const char *crypt_get_type(struct crypt_device *cd)
2263 int crypt_get_active_device(struct crypt_device *cd,
2265 struct crypt_active_device *cad)
2269 r = dm_query_device(name, NULL, &cad->size, &cad->iv_offset, &cad->offset,
2270 NULL, NULL, NULL, &readonly, NULL, NULL);
2274 cad->flags = readonly ? CRYPT_ACTIVATE_READONLY : 0;