8 #include "libcryptsetup.h"
16 struct volume_key *volume_key;
18 uint64_t iteration_time;
23 /* used in CRYPT_LUKS1 */
25 uint64_t PBKDF2_per_sec;
27 /* used in CRYPT_PLAIN */
28 struct crypt_params_plain plain_hdr;
30 char *plain_cipher_mode;
33 /* callbacks definitions */
34 void (*log)(int level, const char *msg, void *usrptr);
36 int (*confirm)(const char *msg, void *usrptr);
38 int (*password)(const char *msg, char *buf, size_t length, void *usrptr);
39 void *password_usrptr;
43 static void (*_default_log)(int level, const char *msg, void *usrptr) = NULL;
44 static int _debug_level = 0;
46 void crypt_set_debug_level(int level)
51 int crypt_get_debug_level()
56 void crypt_log(struct crypt_device *cd, int level, const char *msg)
59 cd->log(level, msg, cd->log_usrptr);
60 else if (_default_log)
61 _default_log(level, msg, NULL);
64 void logger(struct crypt_device *cd, int level, const char *file,
65 int line, const char *format, ...)
70 va_start(argp, format);
72 if (vasprintf(&target, format, argp) > 0) {
74 crypt_log(cd, level, target);
76 } else if (_debug_level)
77 printf("# %s:%d %s\n", file ?: "?", line, target);
79 } else if (_debug_level)
80 printf("# %s\n", target);
89 * Password processing behaviour matrix of process_key
91 * from binary file: check if there is sufficently large key material
92 * interactive & from fd: hash if requested, otherwise crop or pad with '0'
94 static char *process_key(struct crypt_device *cd, const char *hash_name,
95 const char *key_file, size_t key_size,
96 const char *pass, size_t passLen)
98 char *key = crypt_safe_alloc(key_size);
99 memset(key, 0, key_size);
101 /* key is coming from binary file */
102 if (key_file && strcmp(key_file, "-")) {
103 if(passLen < key_size) {
104 log_err(cd, _("Cannot not read %d bytes from key file %s.\n"),
106 crypt_safe_free(key);
109 memcpy(key, pass, key_size);
113 /* key is coming from tty, fd or binary stdin */
115 if (hash(NULL, hash_name, key, key_size, pass, passLen) < 0) {
116 log_err(cd, _("Key processing error (using hash algorithm %s).\n"),
118 crypt_safe_free(key);
121 } else if (passLen > key_size) {
122 memcpy(key, pass, key_size);
124 memcpy(key, pass, passLen);
130 static int isPLAIN(const char *type)
132 return (type && !strcmp(CRYPT_PLAIN, type));
135 static int isLUKS(const char *type)
137 return (type && !strcmp(CRYPT_LUKS1, type));
140 /* keyslot helpers */
141 static int keyslot_verify_or_find_empty(struct crypt_device *cd, int *keyslot)
143 if (*keyslot == CRYPT_ANY_SLOT) {
144 *keyslot = LUKS_keyslot_find_empty(&cd->hdr);
146 log_err(cd, _("All key slots full.\n"));
151 switch (LUKS_keyslot_info(&cd->hdr, *keyslot)) {
152 case CRYPT_SLOT_INVALID:
153 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
154 *keyslot, LUKS_NUMKEYS - 1);
156 case CRYPT_SLOT_INACTIVE:
159 log_err(cd, _("Key slot %d is full, please select another one.\n"),
167 static int verify_other_keyslot(struct crypt_device *cd,
168 const char *key_file,
171 struct volume_key *vk;
172 crypt_keyslot_info ki;
174 char *password = NULL;
175 unsigned int passwordLen;
177 crypt_get_key(_("Enter any remaining LUKS passphrase: "), &password,
178 &passwordLen, 0, key_file, cd->timeout, cd->password_verify, cd);
182 ki = crypt_keyslot_status(cd, keyIndex);
183 if (ki == CRYPT_SLOT_ACTIVE) /* Not last slot */
184 LUKS_keyslot_set(&cd->hdr, keyIndex, 0);
186 openedIndex = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT,
187 password, passwordLen,
190 if (ki == CRYPT_SLOT_ACTIVE)
191 LUKS_keyslot_set(&cd->hdr, keyIndex, 1);
192 crypt_free_volume_key(vk);
193 crypt_safe_free(password);
198 log_verbose(cd, _("Key slot %d verified.\n"), openedIndex);
202 static int find_keyslot_by_passphrase(struct crypt_device *cd,
203 const char *key_file,
206 struct volume_key *vk;
207 char *password = NULL;
208 unsigned int passwordLen;
211 crypt_get_key(message,&password,&passwordLen, 0, key_file,
212 cd->timeout, cd->password_verify, cd);
216 keyIndex = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
217 passwordLen, &cd->hdr, &vk, cd);
218 crypt_free_volume_key(vk);
219 crypt_safe_free(password);
224 static int device_check_and_adjust(struct crypt_device *cd,
231 int r, real_readonly;
237 r = get_device_infos(device, open_exclusive, &real_readonly, &real_size);
240 log_err(cd, _("Cannot use device %s which is in use "
241 "(already mapped or mounted).\n"),
244 log_err(cd, _("Cannot get info about device %s.\n"),
252 log_err(cd, _("Device %s has zero size.\n"), device);
255 if (*size < *offset) {
256 log_err(cd, _("Device %s is too small.\n"), device);
265 log_dbg("Calculated device size is %" PRIu64 " sectors (%s), offset %" PRIu64 ".",
266 *size, *read_only ? "RO" : "RW", *offset);
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);
375 r = dm_create_device(name, cd->device, dm_cipher ?: cipher, cd->type, uuid, size, skip, offset,
376 key_size, processed_key, read_only, reload);
379 crypt_safe_free(processed_key);
383 static int open_from_hdr_and_vk(struct crypt_device *cd,
384 struct volume_key *vk,
388 uint64_t size, offset;
390 int read_only, no_uuid, r;
393 offset = crypt_get_data_offset(cd);
394 read_only = flags & CRYPT_ACTIVATE_READONLY;
395 no_uuid = flags & CRYPT_ACTIVATE_NO_UUID;
397 r = device_check_and_adjust(cd, cd->device, 1, &size, &offset, &read_only);
401 if (asprintf(&cipher, "%s-%s", crypt_get_cipher(cd),
402 crypt_get_cipher_mode(cd)) < 0)
405 r = dm_create_device(name, cd->device, cipher, cd->type,
406 no_uuid ? NULL : crypt_get_uuid(cd),
407 size, 0, offset, vk->keylength, vk->key,
413 static void log_wrapper(int level, const char *msg, void *usrptr)
415 void (*xlog)(int level, char *msg) = usrptr;
416 xlog(level, (char *)msg);
419 static int yesDialog_wrapper(const char *msg, void *usrptr)
421 int (*xyesDialog)(char *msg) = usrptr;
422 return xyesDialog((char*)msg);
425 int crypt_confirm(struct crypt_device *cd, const char *msg)
427 if (!cd || !cd->confirm)
430 return cd->confirm(msg, cd->confirm_usrptr);
433 static void key_from_terminal(struct crypt_device *cd, char *msg, char **key,
434 unsigned int *key_len, int force_verify)
439 *key = crypt_safe_alloc(MAX_TTY_PASSWORD_LEN);
442 r = cd->password(msg, *key, (size_t)key_len, cd->password_usrptr);
444 crypt_safe_free(*key);
449 crypt_get_key(msg, key, key_len, 0, NULL, cd->timeout,
450 (force_verify || cd->password_verify), cd);
453 static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslot,
454 struct volume_key **vk)
456 char *prompt = NULL, *passphrase_read = NULL;
457 unsigned int passphrase_size_read;
458 int r = -EINVAL, tries = cd->tries;
460 if(asprintf(&prompt, _("Enter passphrase for %s: "), cd->device) < 0)
466 crypt_free_volume_key(*vk);
469 key_from_terminal(cd, prompt, &passphrase_read,
470 &passphrase_size_read, 0);
471 if(!passphrase_read) {
476 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
477 passphrase_size_read, &cd->hdr, vk, cd);
478 crypt_safe_free(passphrase_read);
479 passphrase_read = NULL;
480 } while (r == -EPERM && (--tries > 0));
483 crypt_free_volume_key(*vk);
492 static void key_from_file(struct crypt_device *cd, char *msg,
493 char **key, unsigned int *key_len,
494 const char *key_file, size_t key_size)
496 crypt_get_key(msg, key, key_len, key_size, key_file, cd->timeout, 0, cd);
499 static int _crypt_init(struct crypt_device **cd,
501 struct crypt_options *options,
502 int load, int need_dm)
506 /* if it is plain device and mapping table is being reloaded
507 initialize it by name*/
508 init_by_name = (type && !strcmp(type, CRYPT_PLAIN) && load);
510 /* Some of old API calls do not require DM in kernel,
511 fake initialisation by initialise it with kernel_check disabled */
513 (void)dm_init(NULL, 0);
515 r = crypt_init_by_name(cd, options->name);
517 r = crypt_init(cd, options->device);
524 crypt_set_log_callback(*cd, log_wrapper, options->icb->log);
525 crypt_set_confirm_callback(*cd, yesDialog_wrapper, options->icb->yesDialog);
527 crypt_set_timeout(*cd, options->timeout);
528 crypt_set_password_retry(*cd, options->tries);
529 crypt_set_iterarion_time(*cd, options->iteration_time ?: 1000);
530 crypt_set_password_verify(*cd, options->flags & CRYPT_FLAG_VERIFY);
532 if (load && !init_by_name)
533 r = crypt_load(*cd, type, NULL);
535 if (!r && type && !(*cd)->type) {
536 (*cd)->type = strdup(type);
547 void crypt_set_log_callback(struct crypt_device *cd,
548 void (*log)(int level, const char *msg, void *usrptr),
555 cd->log_usrptr = usrptr;
559 void crypt_set_confirm_callback(struct crypt_device *cd,
560 int (*confirm)(const char *msg, void *usrptr),
563 cd->confirm = confirm;
564 cd->confirm_usrptr = usrptr;
567 void crypt_set_password_callback(struct crypt_device *cd,
568 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
571 cd->password = password;
572 cd->password_usrptr = usrptr;
575 /* OPTIONS: name, cipher, device, hash, key_file, key_size, key_slot,
576 * offset, size, skip, timeout, tries, passphrase_fd (ignored),
578 static int crypt_create_and_update_device(struct crypt_options *options, int update)
580 struct crypt_device *cd = NULL;
585 r = _crypt_init(&cd, CRYPT_PLAIN, options, 0, 1);
589 crypt_get_key(_("Enter passphrase: "), &key, &keyLen, options->key_size,
590 options->key_file, cd->timeout, cd->password_verify, cd);
594 r = create_device_helper(cd, options->name, options->hash,
595 options->cipher, NULL, options->key_file, key, keyLen,
596 options->key_size, options->size, options->skip,
597 options->offset, NULL, options->flags & CRYPT_FLAG_READONLY,
598 options->flags, update);
600 crypt_safe_free(key);
605 int crypt_create_device(struct crypt_options *options)
607 return crypt_create_and_update_device(options, 0);
610 int crypt_update_device(struct crypt_options *options)
612 return crypt_create_and_update_device(options, 1);
615 /* OPTIONS: name, size, icb */
616 int crypt_resize_device(struct crypt_options *options)
618 struct crypt_device *cd = NULL;
619 char *device = NULL, *cipher = NULL, *uuid = NULL, *key = NULL;
621 uint64_t size, skip, offset;
622 int key_size, read_only, r;
624 log_dbg("Resizing device %s to %" PRIu64 " sectors.", options->name, options->size);
626 if (dm_init(NULL, 1) < 0)
629 r = dm_query_device(options->name, &device, &size, &skip, &offset,
630 &cipher, &key_size, &key, &read_only, NULL, &uuid);
632 log_err(NULL, _("Device %s is not active.\n"), options->name);
636 /* Try to determine type of device from UUID */
639 if (!strncmp(uuid, CRYPT_PLAIN, strlen(CRYPT_PLAIN))) {
643 } else if (!strncmp(uuid, CRYPT_LUKS1, strlen(CRYPT_LUKS1)))
647 if (!options->device)
648 options->device = device;
650 r = _crypt_init(&cd, type, options, 1, 1);
654 size = options->size;
655 r = device_check_and_adjust(cd, device, 0, &size, &offset, &read_only);
659 r = dm_create_device(options->name, device, cipher, type,
660 crypt_get_uuid(cd), size, skip, offset,
661 key_size, key, read_only, 1);
663 crypt_safe_free(key);
665 if (options->device == device)
666 options->device = NULL;
674 /* OPTIONS: name, icb */
675 int crypt_query_device(struct crypt_options *options)
679 log_dbg("Query device %s.", options->name);
681 if (dm_init(NULL, 1) < 0)
684 r = dm_status_device(options->name);
688 r = dm_query_device(options->name, (char **)&options->device, &options->size,
689 &options->skip, &options->offset, (char **)&options->cipher,
690 &options->key_size, NULL, &read_only, NULL, NULL);
693 options->flags |= CRYPT_FLAG_READONLY;
695 options->flags |= CRYPT_FLAG_FREE_DEVICE;
696 options->flags |= CRYPT_FLAG_FREE_CIPHER;
708 /* OPTIONS: name, icb */
709 int crypt_remove_device(struct crypt_options *options)
711 struct crypt_device *cd = NULL;
714 r = crypt_init_by_name(&cd, options->name);
716 r = crypt_deactivate(cd, options->name);
723 /* OPTIONS: device, cipher, hash, align_payload, key_size (master key), key_slot
724 * new_key_file, iteration_time, timeout, flags, icb */
725 int crypt_luksFormat(struct crypt_options *options)
727 char cipherName[LUKS_CIPHERNAME_L];
728 char cipherMode[LUKS_CIPHERMODE_L];
730 unsigned int passwordLen;
731 struct crypt_device *cd = NULL;
732 struct crypt_params_luks1 cp = {
733 .hash = options->hash,
734 .data_alignment = options->align_payload
738 r = crypt_parse_name_and_mode(options->cipher, cipherName, cipherMode);
740 log_err(cd, _("No known cipher specification pattern detected.\n"));
744 if ((r = _crypt_init(&cd, CRYPT_LUKS1, options, 0, 1)))
747 if (options->key_slot >= LUKS_NUMKEYS && options->key_slot != CRYPT_ANY_SLOT) {
748 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
749 options->key_slot, LUKS_NUMKEYS - 1);
754 crypt_get_key(_("Enter LUKS passphrase: "), &password, &passwordLen, 0,
755 options->new_key_file, cd->timeout, cd->password_verify, cd);
762 r = crypt_format(cd, CRYPT_LUKS1, cipherName, cipherMode,
763 NULL, NULL, options->key_size, &cp);
767 /* Add keyslot using internally stored volume key generated during format */
768 r = crypt_keyslot_add_by_volume_key(cd, options->key_slot, NULL, 0,
769 password, passwordLen);
772 crypt_safe_free(password);
773 return (r < 0) ? r : 0;
776 /* OPTIONS: name, device, key_size, key_file, timeout, tries, flags, icb */
777 int crypt_luksOpen(struct crypt_options *options)
779 struct crypt_device *cd = NULL;
786 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
790 if (options->flags & CRYPT_FLAG_READONLY)
791 flags |= CRYPT_ACTIVATE_READONLY;
793 if (options->flags & CRYPT_FLAG_NON_EXCLUSIVE_ACCESS)
794 flags |= CRYPT_ACTIVATE_NO_UUID;
796 if (options->key_file)
797 r = crypt_activate_by_keyfile(cd, options->name,
798 CRYPT_ANY_SLOT, options->key_file, options->key_size,
801 r = crypt_activate_by_passphrase(cd, options->name,
802 CRYPT_ANY_SLOT, options->passphrase,
803 options->passphrase ? strlen(options->passphrase) : 0,
807 return (r < 0) ? r : 0;
810 /* OPTIONS: device, keys_slot, key_file, timeout, flags, icb */
811 int crypt_luksKillSlot(struct crypt_options *options)
813 struct crypt_device *cd = NULL;
816 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
820 r = luks_remove_helper(cd, options->key_slot, options->key_file, NULL,
821 options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
824 return (r < 0) ? r : 0;
827 /* OPTIONS: device, new_key_file, key_file, timeout, flags, icb */
828 int crypt_luksRemoveKey(struct crypt_options *options)
830 struct crypt_device *cd = NULL;
833 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
837 r = luks_remove_helper(cd, CRYPT_ANY_SLOT, options->key_file, options->new_key_file,
838 options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
841 return (r < 0) ? r : 0;
845 /* OPTIONS: device, new_key_file, key_file, key_slot, flags,
846 iteration_time, timeout, icb */
847 int crypt_luksAddKey(struct crypt_options *options)
849 struct crypt_device *cd = NULL;
852 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
856 if (options->key_file || options->new_key_file)
857 r = crypt_keyslot_add_by_keyfile(cd, options->key_slot,
858 options->key_file, 0,
859 options->new_key_file, 0);
861 r = crypt_keyslot_add_by_passphrase(cd, options->key_slot,
865 return (r < 0) ? r : 0;
868 /* OPTIONS: device, icb */
869 int crypt_luksUUID(struct crypt_options *options)
871 struct crypt_device *cd = NULL;
875 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
879 uuid = (char *)crypt_get_uuid(cd);
880 log_std(cd, uuid ?: "");
886 /* OPTIONS: device, icb */
887 int crypt_isLuks(struct crypt_options *options)
889 struct crypt_device *cd = NULL;
892 log_dbg("Check device %s for LUKS header.", options->device);
898 r = crypt_init(&cd, options->device);
902 /* Do print fail here, no need to crypt_load() */
903 r = LUKS_read_phdr(cd->device, &cd->hdr, 0, cd) ? -EINVAL : 0;
909 /* OPTIONS: device, icb */
910 int crypt_luksDump(struct crypt_options *options)
912 struct crypt_device *cd = NULL;
915 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
925 void crypt_get_error(char *buf, size_t size)
927 const char *error = get_error();
929 if (!buf || size < 1)
932 strncpy(buf, error, size - 1);
933 buf[size - 1] = '\0';
939 void crypt_put_options(struct crypt_options *options)
941 if (options->flags & CRYPT_FLAG_FREE_DEVICE) {
942 free((char *)options->device);
943 options->device = NULL;
944 options->flags &= ~CRYPT_FLAG_FREE_DEVICE;
946 if (options->flags & CRYPT_FLAG_FREE_CIPHER) {
947 free((char *)options->cipher);
948 options->cipher = NULL;
949 options->flags &= ~CRYPT_FLAG_FREE_CIPHER;
953 const char *crypt_get_dir(void)
958 /////////////////////////////////
963 int crypt_init(struct crypt_device **cd, const char *device)
965 struct crypt_device *h = NULL;
970 log_dbg("Allocating crypt device %s context.", device);
972 if (device && !device_ready(NULL, device, O_RDONLY))
975 if (!(h = malloc(sizeof(struct crypt_device))))
978 memset(h, 0, sizeof(*h));
981 h->device = strdup(device);
989 if (dm_init(h, 1) < 0) {
994 h->iteration_time = 1000;
995 h->password_verify = 0;
997 h->rng_type = crypt_random_default_key_rng();
1002 int crypt_init_by_name(struct crypt_device **cd, const char *name)
1004 crypt_status_info ci;
1005 struct crypt_active_device cad;
1006 char *device = NULL, *cipher_full = NULL, *device_uuid = NULL;
1007 char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
1008 int key_size = 0, r;
1011 log_dbg("Allocating crypt device context by device %s.", name);
1013 ci = crypt_status(NULL, name);
1014 if (ci == CRYPT_INVALID)
1017 if (ci < CRYPT_ACTIVE) {
1018 log_err(NULL, _("Device %s is not active.\n"), name);
1022 r = dm_query_device(name, &device, &cad.size, &cad.iv_offset, &cad.offset,
1023 &cipher_full, &key_size, NULL, NULL, NULL,
1026 /* Underlying device disappeared but mapping still active */
1027 if (r >= 0 && !device)
1028 log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
1032 r = crypt_init(cd, device);
1034 /* Try to initialise basic parameters from active device */
1035 if (!r && *device_uuid) {
1036 if (!strncmp(CRYPT_PLAIN, device_uuid, sizeof(CRYPT_PLAIN)-1)) {
1037 (*cd)->type = strdup(CRYPT_PLAIN);
1038 (*cd)->plain_uuid = strdup(device_uuid);
1039 (*cd)->plain_hdr.hash = NULL; /* no way to get this */
1040 (*cd)->plain_hdr.offset = cad.offset;
1041 (*cd)->plain_hdr.skip = cad.iv_offset;
1042 (*cd)->volume_key = crypt_alloc_volume_key(key_size, NULL);
1043 if (!(*cd)->volume_key)
1046 r = crypt_parse_name_and_mode(cipher_full, cipher, cipher_mode);
1048 (*cd)->plain_cipher = strdup(cipher);
1049 (*cd)->plain_cipher_mode = strdup(cipher_mode);
1051 } else if (!strncmp(CRYPT_LUKS1, device_uuid, sizeof(CRYPT_LUKS1)-1)) {
1053 r = crypt_load(*cd, CRYPT_LUKS1, NULL);
1063 static int _crypt_format_plain(struct crypt_device *cd,
1065 const char *cipher_mode,
1067 size_t volume_key_size,
1068 struct crypt_params_plain *params)
1070 if (!cipher || !cipher_mode) {
1071 log_err(cd, _("Invalid plain crypt parameters.\n"));
1075 if (volume_key_size > 1024) {
1076 log_err(cd, _("Invalid key size.\n"));
1080 cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
1081 if (!cd->volume_key)
1084 cd->plain_cipher = strdup(cipher);
1085 cd->plain_cipher_mode = strdup(cipher_mode);
1088 cd->plain_uuid = strdup(uuid);
1090 if (params && params->hash)
1091 cd->plain_hdr.hash = strdup(params->hash);
1093 cd->plain_hdr.offset = params ? params->offset : 0;
1094 cd->plain_hdr.skip = params ? params->skip : 0;
1096 if (!cd->plain_cipher || !cd->plain_cipher_mode)
1102 static int _crypt_format_luks1(struct crypt_device *cd,
1104 const char *cipher_mode,
1106 const char *volume_key,
1107 size_t volume_key_size,
1108 struct crypt_params_luks1 *params)
1111 unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
1112 unsigned long alignment_offset = 0;
1115 log_err(cd, _("Can't format LUKS without device.\n"));
1120 cd->volume_key = crypt_alloc_volume_key(volume_key_size,
1123 cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
1128 if (params && params->data_alignment)
1129 required_alignment = params->data_alignment * SECTOR_SIZE;
1131 get_topology_alignment(cd->device, &required_alignment,
1132 &alignment_offset, DEFAULT_DISK_ALIGNMENT);
1134 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
1135 (params && params->hash) ? params->hash : "sha1",
1137 required_alignment / SECTOR_SIZE,
1138 alignment_offset / SECTOR_SIZE,
1139 cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1143 /* Wipe first 8 sectors - fs magic numbers etc. */
1144 r = wipe_device_header(cd->device, 8);
1147 log_err(cd, _("Cannot format device %s which is still in use.\n"),
1150 log_err(cd, _("Cannot wipe header on device %s.\n"),
1156 r = LUKS_write_phdr(cd->device, &cd->hdr, cd);
1161 int crypt_format(struct crypt_device *cd,
1164 const char *cipher_mode,
1166 const char *volume_key,
1167 size_t volume_key_size,
1172 log_dbg("Formatting device %s as type %s.", cd->device ?: "(none)", cd->type ?: "(none)");
1177 r = init_crypto(cd);
1182 r = _crypt_format_plain(cd, cipher, cipher_mode,
1183 uuid, volume_key_size, params);
1184 else if (isLUKS(type))
1185 r = _crypt_format_luks1(cd, cipher, cipher_mode,
1186 uuid, volume_key, volume_key_size, params);
1188 /* FIXME: allow plugins here? */
1189 log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
1193 if (!r && !(cd->type = strdup(type)))
1197 crypt_free_volume_key(cd->volume_key);
1198 cd->volume_key = NULL;
1204 int crypt_load(struct crypt_device *cd,
1205 const char *requested_type,
1208 struct luks_phdr hdr;
1211 log_dbg("Trying to load %s crypt type from device %s.",
1212 requested_type ?: "any", cd->device ?: "(none)");
1217 if (requested_type && !isLUKS(requested_type))
1220 r = init_crypto(cd);
1224 r = LUKS_read_phdr(cd->device, &hdr, 1, cd);
1227 memcpy(&cd->hdr, &hdr, sizeof(hdr));
1228 cd->type = strdup(requested_type);
1236 int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
1238 char *device = NULL, *cipher = NULL, *uuid = NULL, *key = NULL;
1239 uint64_t size, skip, offset;
1240 int key_size, read_only, r;
1242 /* Device context type must be initialised */
1243 if (!cd->type || !crypt_get_uuid(cd))
1246 r = dm_query_device(name, &device, &size, &skip, &offset,
1247 &cipher, &key_size, &key, &read_only, NULL, &uuid);
1249 log_err(NULL, _("Device %s is not active.\n"), name);
1258 r = device_check_and_adjust(cd, device, 0, &new_size, &offset, &read_only);
1262 if (new_size == size) {
1263 log_dbg("Device has already requested size %" PRIu64
1269 log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
1271 r = dm_create_device(name, device, cipher, cd->type,
1272 crypt_get_uuid(cd), new_size, skip, offset,
1273 key_size, key, read_only, 1);
1275 crypt_safe_free(key);
1283 int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
1285 if (!isLUKS(cd->type)) {
1286 log_err(cd, _("This operation is not supported for this device type.\n"));
1290 if (uuid && !strncmp(uuid, cd->hdr.uuid, sizeof(cd->hdr.uuid))) {
1291 log_dbg("UUID is the same as requested (%s) for device %s.",
1297 log_dbg("Requested new UUID change to %s for %s.", uuid, cd->device);
1299 log_dbg("Requested new UUID refresh for %s.", cd->device);
1301 if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
1304 return LUKS_hdr_uuid_set(cd->device, &cd->hdr, uuid, cd);
1307 int crypt_header_backup(struct crypt_device *cd,
1308 const char *requested_type,
1309 const char *backup_file)
1313 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1316 r = init_crypto(cd);
1320 log_dbg("Requested header backup of device %s (%s) to "
1321 "file %s.", cd->device, requested_type, backup_file);
1323 return LUKS_hdr_backup(backup_file, cd->device, &cd->hdr, cd);
1326 int crypt_header_restore(struct crypt_device *cd,
1327 const char *requested_type,
1328 const char *backup_file)
1332 if (requested_type && !isLUKS(requested_type))
1335 /* Some hash functions need initialized gcrypt library */
1336 r = init_crypto(cd);
1340 log_dbg("Requested header restore to device %s (%s) from "
1341 "file %s.", cd->device, requested_type, backup_file);
1343 return LUKS_hdr_restore(backup_file, cd->device, &cd->hdr, cd);
1346 void crypt_free(struct crypt_device *cd)
1349 log_dbg("Releasing crypt device %s context.", cd->device);
1353 crypt_free_volume_key(cd->volume_key);
1358 /* used in plain device only */
1359 free((char*)cd->plain_hdr.hash);
1360 free(cd->plain_cipher);
1361 free(cd->plain_cipher_mode);
1362 free(cd->plain_uuid);
1368 int crypt_suspend(struct crypt_device *cd,
1371 crypt_status_info ci;
1372 int r, suspended = 0;
1374 log_dbg("Suspending volume %s.", name);
1376 ci = crypt_status(NULL, name);
1377 if (ci < CRYPT_ACTIVE) {
1378 log_err(cd, _("Volume %s is not active.\n"), name);
1382 if (!cd && dm_init(NULL, 1) < 0)
1385 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1386 NULL, NULL, NULL, NULL, &suspended, NULL);
1391 log_err(cd, _("Volume %s is already suspended.\n"), name);
1396 r = dm_suspend_and_wipe_key(name);
1398 log_err(cd, "Suspend is not supported for device %s.\n", name);
1400 log_err(cd, "Error during suspending device %s.\n", name);
1407 int crypt_resume_by_passphrase(struct crypt_device *cd,
1410 const char *passphrase,
1411 size_t passphrase_size)
1413 struct volume_key *vk = NULL;
1414 int r, suspended = 0;
1416 log_dbg("Resuming volume %s.", name);
1418 if (!isLUKS(cd->type)) {
1419 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1424 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1425 NULL, NULL, NULL, NULL, &suspended, NULL);
1430 log_err(cd, _("Volume %s is not suspended.\n"), name);
1435 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1436 passphrase_size, &cd->hdr, &vk, cd);
1438 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1442 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1444 log_err(cd, "Resume is not supported for device %s.\n", name);
1446 log_err(cd, "Error during resuming device %s.\n", name);
1450 crypt_free_volume_key(vk);
1451 return r < 0 ? r : keyslot;
1454 int crypt_resume_by_keyfile(struct crypt_device *cd,
1457 const char *keyfile,
1458 size_t keyfile_size)
1460 struct volume_key *vk = NULL;
1461 char *passphrase_read = NULL;
1462 unsigned int passphrase_size_read;
1463 int r, suspended = 0;
1465 log_dbg("Resuming volume %s.", name);
1467 if (!isLUKS(cd->type)) {
1468 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1473 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1474 NULL, NULL, NULL, NULL, &suspended, NULL);
1479 log_err(cd, _("Volume %s is not suspended.\n"), name);
1486 key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1487 &passphrase_size_read, keyfile, keyfile_size);
1489 if(!passphrase_read)
1492 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1493 passphrase_size_read, &cd->hdr, &vk, cd);
1494 crypt_safe_free(passphrase_read);
1499 r = dm_resume_and_reinstate_key(name, vk->keylength, vk->key);
1501 log_err(cd, "Error during resuming device %s.\n", name);
1505 crypt_free_volume_key(vk);
1506 return r < 0 ? r : keyslot;
1509 // slot manipulation
1510 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1511 int keyslot, // -1 any
1512 const char *passphrase, // NULL -> terminal
1513 size_t passphrase_size,
1514 const char *new_passphrase, // NULL -> terminal
1515 size_t new_passphrase_size)
1517 struct volume_key *vk = NULL;
1518 char *password = NULL, *new_password = NULL;
1519 unsigned int passwordLen, new_passwordLen;
1522 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1523 "new passphrase %sprovided.",
1524 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1526 if (!isLUKS(cd->type)) {
1527 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1531 r = keyslot_verify_or_find_empty(cd, &keyslot);
1535 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1536 /* No slots used, try to use pre-generated key in header */
1537 if (cd->volume_key) {
1538 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1539 r = vk ? 0 : -ENOMEM;
1541 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1544 } else if (passphrase) {
1545 /* Passphrase provided, use it to unlock existing keyslot */
1546 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, passphrase,
1547 passphrase_size, &cd->hdr, &vk, cd);
1549 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1550 key_from_terminal(cd, _("Enter any passphrase: "),
1551 &password, &passwordLen, 0);
1557 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
1558 passwordLen, &cd->hdr, &vk, cd);
1559 crypt_safe_free(password);
1565 if (new_passphrase) {
1566 new_password = (char *)new_passphrase;
1567 new_passwordLen = new_passphrase_size;
1569 key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1570 &new_password, &new_passwordLen, 1);
1577 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1578 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1583 if (!new_passphrase)
1584 crypt_safe_free(new_password);
1585 crypt_free_volume_key(vk);
1586 return r ?: keyslot;
1589 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1591 const char *keyfile,
1592 size_t keyfile_size,
1593 const char *new_keyfile,
1594 size_t new_keyfile_size)
1596 struct volume_key *vk=NULL;
1597 char *password=NULL; unsigned int passwordLen;
1598 char *new_password = NULL; unsigned int new_passwordLen;
1601 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1602 keyfile ?: "[none]", new_keyfile ?: "[none]");
1604 if (!isLUKS(cd->type)) {
1605 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1609 r = keyslot_verify_or_find_empty(cd, &keyslot);
1613 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1614 /* No slots used, try to use pre-generated key in header */
1615 if (cd->volume_key) {
1616 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1617 r = vk ? 0 : -ENOMEM;
1619 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1623 /* Read password from file of (if NULL) from terminal */
1625 key_from_file(cd, _("Enter any passphrase: "), &password, &passwordLen,
1626 keyfile, keyfile_size);
1628 key_from_terminal(cd, _("Enter any passphrase: "),
1629 &password, &passwordLen, 0);
1634 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password, passwordLen,
1636 crypt_safe_free(password);
1643 key_from_file(cd, _("Enter new passphrase for key slot: "),
1644 &new_password, &new_passwordLen, new_keyfile,
1647 key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1648 &new_password, &new_passwordLen, 1);
1655 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1656 &cd->hdr, vk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1658 crypt_safe_free(new_password);
1659 crypt_free_volume_key(vk);
1660 return r < 0 ? r : keyslot;
1663 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1665 const char *volume_key,
1666 size_t volume_key_size,
1667 const char *passphrase,
1668 size_t passphrase_size)
1670 struct volume_key *vk = NULL;
1672 char *new_password = NULL; unsigned int new_passwordLen;
1674 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1676 if (!isLUKS(cd->type)) {
1677 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1682 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1683 else if (cd->volume_key)
1684 vk = crypt_alloc_volume_key(cd->volume_key->keylength, cd->volume_key->key);
1689 r = LUKS_verify_volume_key(&cd->hdr, vk);
1691 log_err(cd, _("Volume key does not match the volume.\n"));
1695 r = keyslot_verify_or_find_empty(cd, &keyslot);
1700 key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1701 &new_password, &new_passwordLen, 1);
1702 passphrase = new_password;
1703 passphrase_size = new_passwordLen;
1706 r = LUKS_set_key(cd->device, keyslot, passphrase, passphrase_size,
1707 &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 ?: 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 *prompt = NULL;
1753 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1754 name ? "Activating" : "Checking", name ?: "",
1755 keyslot, passphrase ? "" : "[none] ");
1757 /* plain, use hashed passphrase */
1758 if (isPLAIN(cd->type))
1759 return create_device_helper(cd, name, cd->plain_hdr.hash,
1760 cd->plain_cipher, cd->plain_cipher_mode, NULL, passphrase, passphrase_size,
1761 cd->volume_key->keylength, 0, cd->plain_hdr.skip,
1762 cd->plain_hdr.offset, cd->plain_uuid, flags & CRYPT_ACTIVATE_READONLY, 0, 0);
1765 ci = crypt_status(NULL, name);
1766 if (ci == CRYPT_INVALID)
1768 else if (ci >= CRYPT_ACTIVE) {
1769 log_err(cd, _("Device %s already exists.\n"), name);
1774 if(asprintf(&prompt, _("Enter passphrase for %s: "), cd->device) < 0)
1777 /* provided passphrase, do not retry */
1779 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1780 passphrase_size, &cd->hdr, &vk, cd);
1782 r = volume_key_by_terminal_passphrase(cd, keyslot, &vk);
1787 r = open_from_hdr_and_vk(cd, vk, name, flags);
1790 crypt_free_volume_key(vk);
1793 return r < 0 ? r : keyslot;
1796 int crypt_activate_by_keyfile(struct crypt_device *cd,
1799 const char *keyfile,
1800 size_t keyfile_size,
1803 crypt_status_info ci;
1804 struct volume_key *vk = NULL;
1805 char *passphrase_read = NULL;
1806 unsigned int passphrase_size_read;
1809 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1810 name ?: "", keyslot, keyfile ?: "[none]");
1812 if (!isLUKS(cd->type)) {
1813 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1818 ci = crypt_status(NULL, name);
1819 if (ci == CRYPT_INVALID)
1821 else if (ci >= CRYPT_ACTIVE) {
1822 log_err(cd, _("Device %s already exists.\n"), name);
1830 key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1831 &passphrase_size_read, keyfile, keyfile_size);
1832 if(!passphrase_read)
1835 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1836 passphrase_size_read, &cd->hdr, &vk, cd);
1837 crypt_safe_free(passphrase_read);
1843 r = open_from_hdr_and_vk(cd, vk, name, flags);
1846 crypt_free_volume_key(vk);
1848 return r < 0 ? r : keyslot;
1851 int crypt_activate_by_volume_key(struct crypt_device *cd,
1853 const char *volume_key,
1854 size_t volume_key_size,
1857 crypt_status_info ci;
1858 struct volume_key *vk;
1861 log_dbg("Activating volume %s by volume key.", name);
1863 /* use key directly, no hash */
1864 if (isPLAIN(cd->type))
1865 return create_device_helper(cd, name, NULL,
1866 cd->plain_cipher, cd->plain_cipher_mode, NULL, volume_key, volume_key_size,
1867 cd->volume_key->keylength, 0, cd->plain_hdr.skip,
1868 cd->plain_hdr.offset, cd->plain_uuid, flags & CRYPT_ACTIVATE_READONLY, 0, 0);
1870 if (!isLUKS(cd->type)) {
1871 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1876 ci = crypt_status(NULL, name);
1877 if (ci == CRYPT_INVALID)
1879 else if (ci >= CRYPT_ACTIVE) {
1880 log_err(cd, _("Device %s already exists.\n"), name);
1885 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
1888 r = LUKS_verify_volume_key(&cd->hdr, vk);
1891 log_err(cd, _("Volume key does not match the volume.\n"));
1894 r = open_from_hdr_and_vk(cd, vk, name, flags);
1896 crypt_free_volume_key(vk);
1901 int crypt_deactivate(struct crypt_device *cd, const char *name)
1908 log_dbg("Deactivating volume %s.", name);
1910 if (!cd && dm_init(NULL, 1) < 0)
1913 switch (crypt_status(cd, name)) {
1915 r = dm_remove_device(name, 0, 0);
1918 log_err(cd, _("Device %s is busy.\n"), name);
1921 case CRYPT_INACTIVE:
1922 log_err(cd, _("Device %s is not active.\n"), name);
1926 log_err(cd, _("Invalid device %s.\n"), name);
1936 // misc helper functions
1937 int crypt_volume_key_get(struct crypt_device *cd,
1940 size_t *volume_key_size,
1941 const char *passphrase,
1942 size_t passphrase_size)
1944 struct volume_key *vk;
1945 char *processed_key = NULL;
1948 key_len = crypt_get_volume_key_size(cd);
1949 if (key_len > *volume_key_size) {
1950 log_err(cd, _("Volume key buffer too small.\n"));
1954 if (isPLAIN(cd->type) && cd->plain_hdr.hash) {
1955 processed_key = process_key(cd, cd->plain_hdr.hash, NULL, key_len,
1956 passphrase, passphrase_size);
1957 if (!processed_key) {
1958 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
1961 memcpy(volume_key, processed_key, key_len);
1962 *volume_key_size = key_len;
1963 crypt_safe_free(processed_key);
1967 if (isLUKS(cd->type)) {
1968 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1969 passphrase_size, &cd->hdr, &vk, cd);
1972 memcpy(volume_key, vk->key, vk->keylength);
1973 *volume_key_size = vk->keylength;
1976 crypt_free_volume_key(vk);
1980 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
1984 int crypt_volume_key_verify(struct crypt_device *cd,
1985 const char *volume_key,
1986 size_t volume_key_size)
1988 struct volume_key *vk;
1991 if (!isLUKS(cd->type)) {
1992 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1996 vk = crypt_alloc_volume_key(volume_key_size, volume_key);
2000 r = LUKS_verify_volume_key(&cd->hdr, vk);
2003 log_err(cd, _("Volume key does not match the volume.\n"));
2005 crypt_free_volume_key(vk);
2010 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
2012 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
2013 cd->timeout = timeout_sec;
2016 void crypt_set_password_retry(struct crypt_device *cd, int tries)
2018 log_dbg("Password retry count set to %d.", tries);
2022 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
2024 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
2025 cd->iteration_time = iteration_time_ms;
2028 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
2030 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
2031 cd->password_verify = password_verify ? 1 : 0;
2034 void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
2037 case CRYPT_RNG_URANDOM:
2038 case CRYPT_RNG_RANDOM:
2039 log_dbg("RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
2040 cd->rng_type = rng_type;
2044 int crypt_get_rng_type(struct crypt_device *cd)
2049 return cd->rng_type;
2052 int crypt_memory_lock(struct crypt_device *cd, int lock)
2054 return lock ? crypt_memlock_inc(cd) : crypt_memlock_dec(cd);
2058 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
2062 if (!cd && dm_init(NULL, 1) < 0)
2063 return CRYPT_INVALID;
2065 r = dm_status_device(name);
2070 if (r < 0 && r != -ENODEV)
2071 return CRYPT_INVALID;
2074 return CRYPT_ACTIVE;
2079 return CRYPT_INACTIVE;
2082 static void hexprintICB(struct crypt_device *cd, char *d, int n)
2085 for(i = 0; i < n; i++)
2086 log_std(cd, "%02hhx ", (char)d[i]);
2089 int crypt_dump(struct crypt_device *cd)
2092 if (!isLUKS(cd->type)) { //FIXME
2093 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2097 log_std(cd, "LUKS header information for %s\n\n", cd->device);
2098 log_std(cd, "Version: \t%d\n", cd->hdr.version);
2099 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
2100 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
2101 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec);
2102 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
2103 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
2104 log_std(cd, "MK digest: \t");
2105 hexprintICB(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE);
2107 log_std(cd, "MK salt: \t");
2108 hexprintICB(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2);
2109 log_std(cd, "\n \t");
2110 hexprintICB(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
2112 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
2113 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
2114 for(i = 0; i < LUKS_NUMKEYS; i++) {
2115 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
2116 log_std(cd, "Key Slot %d: ENABLED\n",i);
2117 log_std(cd, "\tIterations: \t%d\n",
2118 cd->hdr.keyblock[i].passwordIterations);
2119 log_std(cd, "\tSalt: \t");
2120 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt,
2122 log_std(cd, "\n\t \t");
2123 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt +
2124 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
2127 log_std(cd, "\tKey material offset:\t%d\n",
2128 cd->hdr.keyblock[i].keyMaterialOffset);
2129 log_std(cd, "\tAF stripes: \t%d\n",
2130 cd->hdr.keyblock[i].stripes);
2133 log_std(cd, "Key Slot %d: DISABLED\n", i);
2139 const char *crypt_get_cipher(struct crypt_device *cd)
2141 if (isPLAIN(cd->type))
2142 return cd->plain_cipher;
2144 if (isLUKS(cd->type))
2145 return cd->hdr.cipherName;
2150 const char *crypt_get_cipher_mode(struct crypt_device *cd)
2152 if (isPLAIN(cd->type))
2153 return cd->plain_cipher_mode;
2155 if (isLUKS(cd->type))
2156 return cd->hdr.cipherMode;
2161 const char *crypt_get_uuid(struct crypt_device *cd)
2163 if (isLUKS(cd->type))
2164 return cd->hdr.uuid;
2166 if (isPLAIN(cd->type))
2167 return cd->plain_uuid;
2172 const char *crypt_get_device_name(struct crypt_device *cd)
2177 int crypt_get_volume_key_size(struct crypt_device *cd)
2179 if (isPLAIN(cd->type) && cd->volume_key)
2180 return cd->volume_key->keylength;
2182 if (isLUKS(cd->type))
2183 return cd->hdr.keyBytes;
2188 uint64_t crypt_get_data_offset(struct crypt_device *cd)
2190 if (isPLAIN(cd->type))
2191 return cd->plain_hdr.offset;
2193 if (isLUKS(cd->type))
2194 return cd->hdr.payloadOffset;
2199 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
2201 if (!isLUKS(cd->type)) {
2202 log_err(cd, _("This operation is supported only for LUKS device.\n"));
2203 return CRYPT_SLOT_INVALID;
2206 return LUKS_keyslot_info(&cd->hdr, keyslot);
2209 int crypt_keyslot_max(const char *type)
2211 if (type && isLUKS(type))
2212 return LUKS_NUMKEYS;
2217 const char *crypt_get_type(struct crypt_device *cd)
2222 int crypt_get_active_device(struct crypt_device *cd,
2224 struct crypt_active_device *cad)
2228 r = dm_query_device(name, NULL, &cad->size, &cad->iv_offset, &cad->offset,
2229 NULL, NULL, NULL, &readonly, NULL, NULL);
2233 cad->flags = readonly ? CRYPT_ACTIVATE_READONLY : 0;