8 #include "libcryptsetup.h"
16 struct luks_masterkey *volume_key;
18 uint64_t iteration_time;
22 /* used in CRYPT_LUKS1 */
24 uint64_t PBKDF2_per_sec;
26 /* used in CRYPT_PLAIN */
27 struct crypt_params_plain plain_hdr;
29 char *plain_cipher_mode;
32 /* callbacks definitions */
33 void (*log)(int class, const char *msg, void *usrptr);
35 int (*confirm)(const char *msg, void *usrptr);
37 int (*password)(const char *msg, char *buf, size_t length, void *usrptr);
38 void *password_usrptr;
42 static void (*_default_log)(int class, char *msg) = NULL;
43 static int _debug_level = 0;
45 void crypt_set_debug_level(int level)
50 void set_default_log(void (*log)(int class, char *msg))
55 void logger(struct crypt_device *cd, int class, const char *file,
56 int line, const char *format, ...)
61 va_start(argp, format);
63 if (vasprintf(&target, format, argp) > 0) {
66 cd->log(class, target, cd->log_usrptr);
67 else if (_default_log)
68 _default_log(class, target);
70 } else if (_debug_level)
71 printf("# %s:%d %s\n", file ?: "?", line, target);
73 } else if (_debug_level)
74 printf("# %s\n", target);
83 * Password processing behaviour matrix of process_key
85 * from binary file: check if there is sufficently large key material
86 * interactive & from fd: hash if requested, otherwise crop or pad with '0'
88 static char *process_key(struct crypt_device *cd, const char *hash_name,
89 const char *key_file, size_t key_size,
90 const char *pass, size_t passLen)
92 char *key = safe_alloc(key_size);
93 memset(key, 0, key_size);
95 /* key is coming from binary file */
96 if (key_file && strcmp(key_file, "-")) {
97 if(passLen < key_size) {
98 log_err(cd, _("Cannot not read %d bytes from key file %s.\n"),
103 memcpy(key, pass, key_size);
107 /* key is coming from tty, fd or binary stdin */
109 if (hash(NULL, hash_name, key, key_size, pass, passLen) < 0) {
110 log_err(cd, _("Key processing error.\n"));
114 } else if (passLen > key_size) {
115 memcpy(key, pass, key_size);
117 memcpy(key, pass, passLen);
123 int parse_into_name_and_mode(const char *nameAndMode, char *name, char *mode)
125 /* Token content stringification, see info cpp/stringification */
127 #define xstr(s) str(s)
128 #define scanpattern1 "%" xstr(LUKS_CIPHERNAME_L) "[^-]-%" xstr(LUKS_CIPHERMODE_L) "s"
129 #define scanpattern2 "%" xstr(LUKS_CIPHERNAME_L) "[^-]"
133 if(sscanf(nameAndMode,scanpattern1, name, mode) != 2) {
134 if((r = sscanf(nameAndMode,scanpattern2,name)) == 1)
135 strncpy(mode,"cbc-plain",10);
148 static int isPLAIN(const char *type)
150 return (type && !strcmp(CRYPT_PLAIN, type));
153 static int isLUKS(const char *type)
155 return (type && !strcmp(CRYPT_LUKS1, type));
158 /* keyslot helpers */
159 static int keyslot_verify_or_find_empty(struct crypt_device *cd, int *keyslot)
161 if (*keyslot == CRYPT_ANY_SLOT) {
162 *keyslot = LUKS_keyslot_find_empty(&cd->hdr);
164 log_err(cd, _("All key slots full.\n"));
169 switch (LUKS_keyslot_info(&cd->hdr, *keyslot)) {
171 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
172 *keyslot, LUKS_NUMKEYS - 1);
177 log_err(cd, _("Key slot %d is full, please select another one.\n"),
185 static int verify_other_keyslot(struct crypt_device *cd,
186 const char *key_file,
190 struct luks_masterkey *mk;
191 crypt_keyslot_info ki;
193 char *password = NULL;
194 unsigned int passwordLen;
196 get_key(_("Enter any remaining LUKS passphrase: "), &password,
197 &passwordLen, 0, key_file, cd->timeout, flags, cd);
201 if (ki == SLOT_ACTIVE_LAST)
202 LUKS_keyslot_set(&cd->hdr, keyIndex, 0);
204 openedIndex = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT,
205 password, passwordLen,
207 if (openedIndex < 0) {
208 LUKS_keyslot_set(&cd->hdr, keyIndex, 1);
212 LUKS_dealloc_masterkey(mk);
215 log_std(cd, _("Key slot %d verified.\n"), openedIndex);
219 static int find_keyslot_by_passphrase(struct crypt_device *cd,
220 const char *key_file,
224 struct luks_masterkey *mk;
225 char *password = NULL;
226 unsigned int passwordLen;
229 get_key(message,&password,&passwordLen, 0, key_file,
230 cd->timeout, flags, cd);
234 keyIndex = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
235 passwordLen, &cd->hdr, &mk, cd);
236 LUKS_dealloc_masterkey(mk);
242 static int device_check_and_adjust(struct crypt_device *cd,
244 uint64_t *size, uint64_t *offset,
247 struct device_infos infos;
249 if (get_device_infos(device, &infos, cd) < 0) {
250 log_err(cd, _("Cannot get info about device %s.\n"), device);
257 log_err(cd, _("Device %s has zero size.\n"), device);
260 if (*size <= *offset) {
261 log_err(cd, _("Device %s is too small.\n"), device);
273 static int luks_remove_helper(struct crypt_device *cd,
275 const char *other_key_file,
276 const char *key_file,
279 crypt_keyslot_info ki;
282 if (key_slot == CRYPT_ANY_SLOT) {
283 key_slot = find_keyslot_by_passphrase(cd, key_file, 0,
284 _("Enter LUKS passphrase to be deleted: "));
290 log_std(cd, _("key slot %d selected for deletion.\n"), key_slot);
293 ki = crypt_keyslot_status(cd, key_slot);
294 if (ki == SLOT_INVALID) {
295 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
296 key_slot, LUKS_NUMKEYS - 1);
300 if (ki <= SLOT_INACTIVE) {
301 log_err(cd, _("Key %d not active. Can't wipe.\n"), key_slot);
306 if (ki == SLOT_ACTIVE_LAST && cd->confirm &&
307 !(cd->confirm(_("This is the last keyslot."
308 " Device will become unusable after purging this key."),
309 cd->confirm_usrptr))) {
315 r = verify_other_keyslot(cd, other_key_file, 0, key_slot);
320 r = crypt_keyslot_destroy(cd, key_slot);
322 return (r < 0) ? r : 0;
325 static int create_device_helper(struct crypt_device *cd,
329 const char *cipher_mode,
330 const char *key_file,
342 crypt_status_info ci;
343 char *dm_cipher = NULL;
344 char *processed_key = NULL;
347 ci = crypt_status(cd, name);
351 if (reload && ci < ACTIVE)
354 if (!reload && ci >= 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, &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, uuid, size, skip, offset,
376 key_size, processed_key, read_only, reload);
379 safe_free(processed_key);
383 static int open_from_hdr_and_mk(struct crypt_device *cd,
384 struct luks_masterkey *mk,
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, &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, no_uuid ? NULL : crypt_get_uuid(cd),
406 size, 0, offset, mk->keyLength, mk->key,
412 static void log_wrapper(int class, const char *msg, void *usrptr)
414 void (*xlog)(int class, char *msg) = usrptr;
415 xlog(class, (char *)msg);
418 static int yesDialog_wrapper(const char *msg, void *usrptr)
420 int (*xyesDialog)(char *msg) = usrptr;
421 return xyesDialog((char*)msg);
424 static void key_from_terminal(struct crypt_device *cd, char *msg, char **key,
425 unsigned int *key_len, int force_verify)
430 *key = safe_alloc(MAX_TTY_PASSWORD_LEN);
433 r = cd->password(msg, *key, (size_t)key_len, cd->password_usrptr);
440 if (force_verify || cd->password_verify)
441 flags |= CRYPT_FLAG_VERIFY_IF_POSSIBLE;
442 get_key(msg, key, key_len, 0, NULL, cd->timeout, flags, cd);
446 static void key_from_file(struct crypt_device *cd, char *msg,
447 char **key, unsigned int *key_len,
448 const char *key_file, size_t key_size)
450 get_key(msg, key, key_len, key_size, key_file, cd->timeout, 0, cd);
453 static int _crypt_init(struct crypt_device **cd,
454 struct crypt_options *options,
455 int load, int need_dm)
459 /* Some of old API calls do not require DM in kernel,
460 fake initialisation by initialise it with kernel_check disabled */
462 (void)dm_init(NULL, 0);
463 r = crypt_init(cd, options->device);
470 crypt_set_log_callback(*cd, log_wrapper, options->icb->log);
471 crypt_set_confirm_callback(*cd, yesDialog_wrapper, options->icb->yesDialog);
473 crypt_set_timeout(*cd, options->timeout);
474 crypt_set_password_retry(*cd, options->tries);
475 crypt_set_iterarion_time(*cd, options->iteration_time);
476 crypt_set_password_verify(*cd, options->flags & CRYPT_FLAG_VERIFY);
479 r = crypt_load(*cd, CRYPT_LUKS1, NULL);
484 void crypt_set_log_callback(struct crypt_device *cd,
485 void (*log)(int class, const char *msg, void *usrptr),
489 cd->log_usrptr = usrptr;
492 void crypt_set_confirm_callback(struct crypt_device *cd,
493 int (*confirm)(const char *msg, void *usrptr),
496 cd->confirm = confirm;
497 cd->confirm_usrptr = usrptr;
500 void crypt_set_password_callback(struct crypt_device *cd,
501 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
504 cd->password = password;
505 cd->password_usrptr = usrptr;
508 /* OPTIONS: name, cipher, device, hash, key_file, key_size, key_slot,
509 * offset, size, skip, timeout, tries, passphrase_fd (ignored),
511 int crypt_create_device(struct crypt_options *options)
513 struct crypt_device *cd = NULL;
518 r = _crypt_init(&cd, options, 0, 1);
522 get_key(_("Enter passphrase: "), &key, &keyLen, options->key_size,
523 options->key_file, cd->timeout, options->flags, cd);
527 r = create_device_helper(cd, options->name, options->hash,
528 options->cipher, NULL, options->key_file, key, keyLen,
529 options->key_size, options->size, options->skip,
530 options->offset, NULL, options->flags & CRYPT_FLAG_READONLY,
538 /* OPTIONS: same as create above */
539 int crypt_update_device(struct crypt_options *options)
541 struct crypt_device *cd = NULL;
546 r = _crypt_init(&cd, options, 0, 1);
550 get_key(_("Enter passphrase: "), &key, &keyLen, options->key_size,
551 options->key_file, cd->timeout, options->flags, cd);
555 r = create_device_helper(cd, options->name, options->hash,
556 options->cipher, NULL, options->key_file, key, keyLen,
557 options->key_size, options->size, options->skip,
558 options->offset, NULL, options->flags & CRYPT_FLAG_READONLY,
566 /* OPTIONS: name, size, icb */
567 int crypt_resize_device(struct crypt_options *options)
569 struct crypt_device *cd = NULL;
570 char *device, *cipher, *key = NULL;
571 uint64_t size, skip, offset;
572 int key_size, read_only, r;
574 r = dm_query_device(options->name, &device, &size, &skip, &offset,
575 &cipher, &key_size, &key, &read_only);
579 r = _crypt_init(&cd, options, 0, 1);
583 size = options->size;
584 r = device_check_and_adjust(cd, device, &size, &offset, &read_only);
588 r = dm_create_device(options->name, device, cipher, NULL, size, skip, offset,
589 key_size, key, read_only, 1);
598 /* OPTIONS: name, icb */
599 int crypt_query_device(struct crypt_options *options)
603 r = dm_status_device(options->name);
607 r = dm_query_device(options->name, (char **)&options->device, &options->size,
608 &options->skip, &options->offset, (char **)&options->cipher,
609 &options->key_size, NULL, &read_only);
615 options->flags |= CRYPT_FLAG_READONLY;
617 options->flags |= CRYPT_FLAG_FREE_DEVICE;
618 options->flags |= CRYPT_FLAG_FREE_CIPHER;
623 /* OPTIONS: name, icb */
624 int crypt_remove_device(struct crypt_options *options)
626 return crypt_deactivate(NULL, options->name);
629 /* OPTIONS: device, cipher, hash, align_payload, key_size (master key), key_slot
630 * new_key_file, iteration_time, timeout, flags, icb */
631 int crypt_luksFormat(struct crypt_options *options)
633 char cipherName[LUKS_CIPHERNAME_L];
634 char cipherMode[LUKS_CIPHERMODE_L];
636 unsigned int passwordLen;
637 struct crypt_device *cd;
638 struct crypt_params_luks1 cp = {
639 .hash = options->hash,
640 .data_alignment = options->align_payload
644 r = parse_into_name_and_mode(options->cipher, cipherName, cipherMode);
646 log_err(cd, _("No known cipher specification pattern detected.\n"));
650 if ((r = _crypt_init(&cd, options, 0, 1)))
653 if (options->key_slot >= LUKS_NUMKEYS && options->key_slot != CRYPT_ANY_SLOT) {
654 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
655 options->key_slot, LUKS_NUMKEYS - 1);
660 get_key(_("Enter LUKS passphrase: "), &password, &passwordLen, 0,
661 options->new_key_file, options->timeout, options->flags, cd);
668 r = crypt_format(cd, CRYPT_LUKS1, cipherName, cipherMode,
669 NULL, NULL, options->key_size, &cp);
673 /* Add keyslot using internally stored volume key generated during format */
674 r = crypt_keyslot_add_by_volume_key(cd, options->key_slot, NULL, 0,
675 password, passwordLen);
679 return (r < 0) ? r : 0;
682 /* OPTIONS: name, device, key_size, key_file, timeout, tries, flags, icb */
683 int crypt_luksOpen(struct crypt_options *options)
685 struct crypt_device *cd = NULL;
692 r = _crypt_init(&cd, options, 1, 1);
696 if (options->flags & CRYPT_FLAG_READONLY)
697 flags |= CRYPT_ACTIVATE_READONLY;
699 if (options->flags & CRYPT_FLAG_NON_EXCLUSIVE_ACCESS)
700 flags |= CRYPT_ACTIVATE_NO_UUID;
702 if (options->key_file)
703 r = crypt_activate_by_keyfile(cd, options->name,
704 CRYPT_ANY_SLOT, options->key_file, options->key_size,
707 r = crypt_activate_by_passphrase(cd, options->name,
708 CRYPT_ANY_SLOT, options->passphrase,
709 options->passphrase ? strlen(options->passphrase) : 0,
713 return (r < 0) ? r : 0;
716 /* OPTIONS: device, keys_slot, key_file, timeout, flags, icb */
717 int crypt_luksKillSlot(struct crypt_options *options)
719 struct crypt_device *cd = NULL;
722 r = _crypt_init(&cd, options, 1, 1);
726 r = luks_remove_helper(cd, options->key_slot, options->key_file, NULL,
727 options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
730 return (r < 0) ? r : 0;
733 /* OPTIONS: device, new_key_file, key_file, timeout, flags, icb */
734 int crypt_luksRemoveKey(struct crypt_options *options)
736 struct crypt_device *cd = NULL;
739 r = _crypt_init(&cd, options, 1, 1);
743 r = luks_remove_helper(cd, CRYPT_ANY_SLOT, options->key_file, options->new_key_file,
744 options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
747 return (r < 0) ? r : 0;
751 /* OPTIONS: device, new_key_file, key_file, key_slot, flags,
752 iteration_time, timeout, icb */
753 int crypt_luksAddKey(struct crypt_options *options)
755 struct crypt_device *cd = NULL;
758 r = _crypt_init(&cd, options, 1, 1);
762 if (options->key_file || options->new_key_file)
763 r = crypt_keyslot_add_by_keyfile(cd, options->key_slot,
764 options->key_file, 0,
765 options->new_key_file, 0);
767 r = crypt_keyslot_add_by_passphrase(cd, options->key_slot,
771 return (r < 0) ? r : 0;
774 /* OPTIONS: device, icb */
775 int crypt_luksUUID(struct crypt_options *options)
777 struct crypt_device *cd = NULL;
781 r = _crypt_init(&cd, options, 1, 0);
785 uuid = (char *)crypt_get_uuid(cd);
786 log_std(cd, uuid ?: "");
792 /* OPTIONS: device, icb */
793 int crypt_isLuks(struct crypt_options *options)
795 struct crypt_device *cd = NULL;
798 r = _crypt_init(&cd, options, 1, 0);
803 /* OPTIONS: device, icb */
804 int crypt_luksDump(struct crypt_options *options)
806 struct crypt_device *cd = NULL;
809 r = _crypt_init(&cd, options, 1, 0);
819 void crypt_get_error(char *buf, size_t size)
821 const char *error = get_error();
823 if (!buf || size < 1)
826 strncpy(buf, error, size - 1);
827 buf[size - 1] = '\0';
833 void crypt_put_options(struct crypt_options *options)
835 if (options->flags & CRYPT_FLAG_FREE_DEVICE) {
836 free((char *)options->device);
837 options->device = NULL;
838 options->flags &= ~CRYPT_FLAG_FREE_DEVICE;
840 if (options->flags & CRYPT_FLAG_FREE_CIPHER) {
841 free((char *)options->cipher);
842 options->cipher = NULL;
843 options->flags &= ~CRYPT_FLAG_FREE_CIPHER;
847 const char *crypt_get_dir(void)
852 /////////////////////////////////
857 int crypt_init(struct crypt_device **cd, const char *device)
859 struct crypt_device *h = NULL;
864 log_dbg("Allocating crypt device %s context.", device);
866 if (!device_ready(NULL, device, 0))
869 if (!(h = malloc(sizeof(struct crypt_device))))
872 memset(h, 0, sizeof(*h));
874 h->device = strdup(device);
880 if (!dm_init(h, 1) < 0) {
885 h->iteration_time = 1000;
886 h->password_verify = 0;
892 static int _crypt_format_plain(struct crypt_device *cd,
894 const char *cipher_mode,
896 struct crypt_params_plain *params)
898 if (!cipher || !cipher_mode || !params || !params->hash) {
899 log_err(cd, _("Invalid plain crypt parameters.\n"));
903 if (cd->volume_key->keyLength > 1024) {
904 log_err(cd, _("Invalid key size.\n"));
908 cd->plain_cipher = strdup(cipher);
909 cd->plain_cipher_mode = strdup(cipher_mode);
912 cd->plain_uuid = strdup(uuid);
915 cd->plain_hdr.hash = strdup(params->hash);
917 cd->plain_hdr.offset = params->offset;
918 cd->plain_hdr.skip = params->skip;
920 if ((params->hash && !cd->plain_hdr.hash) ||
921 !cd->plain_cipher || !cd->plain_cipher_mode)
927 static int _crypt_format_luks1(struct crypt_device *cd,
929 const char *cipher_mode,
931 struct crypt_params_luks1 *params)
935 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
936 (params && params->hash) ? params->hash : "sha1",
938 params ? params->data_alignment: DEFAULT_ALIGNMENT, cd);
942 /* Wipe first 8 sectors - fs magic numbers etc. */
943 r = wipe_device_header(cd->device, 8);
945 log_err(cd, _("Can't wipe header on device %s.\n"), cd->device);
949 r = LUKS_write_phdr(cd->device, &cd->hdr, cd);
954 int crypt_format(struct crypt_device *cd,
957 const char *cipher_mode,
959 const char *volume_key,
960 size_t volume_key_size,
965 log_dbg("Formatting device %s as type %s.", cd->device, cd->type ?: "(none)");
971 cd->volume_key = LUKS_alloc_masterkey(volume_key_size,
974 cd->volume_key = LUKS_generate_masterkey(volume_key_size);
980 r = _crypt_format_plain(cd, cipher, cipher_mode,
982 else if (isLUKS(type))
983 r = _crypt_format_luks1(cd, cipher, cipher_mode,
986 /* FIXME: allow plugins here? */
987 log_err(cd, _("Unkown crypt device type %s requesed.\n"), type);
991 if (!r && !(cd->type = strdup(type)))
995 LUKS_dealloc_masterkey(cd->volume_key);
996 cd->volume_key = NULL;
1002 int crypt_load(struct crypt_device *cd,
1003 const char *requested_type,
1006 struct luks_phdr hdr;
1009 log_dbg("Trying to load %s crypt type from device %s.",
1010 requested_type ?: "any", cd->device);
1012 if (requested_type && !isPLAIN(requested_type) && !isLUKS(requested_type))
1015 r = LUKS_read_phdr(cd->device, &hdr, 0, cd);
1018 memcpy(&cd->hdr, &hdr, sizeof(hdr));
1019 cd->type = strdup(requested_type);
1027 void crypt_free(struct crypt_device *cd)
1030 log_dbg("Releasing crypt device %s context.", cd->device);
1034 LUKS_dealloc_masterkey(cd->volume_key);
1039 /* used in plain device only */
1040 free((char*)cd->plain_hdr.hash);
1041 free(cd->plain_cipher);
1042 free(cd->plain_cipher_mode);
1043 free(cd->plain_uuid);
1049 // slot manipulation
1050 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1051 int keyslot, // -1 any
1052 const char *passphrase, // NULL -> terminal
1053 size_t passphrase_size,
1054 const char *new_passphrase, // NULL -> terminal
1055 size_t new_passphrase_size)
1057 struct luks_masterkey *mk = NULL;
1058 char *password = NULL, *new_password = NULL;
1059 unsigned int passwordLen, new_passwordLen;
1062 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1063 "new passphrase %sprovided.",
1064 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1066 if (!isLUKS(cd->type)) {
1067 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1071 r = keyslot_verify_or_find_empty(cd, &keyslot);
1075 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1076 /* No slots used, try to use pre-generated key in header */
1077 if (cd->volume_key) {
1078 mk = LUKS_alloc_masterkey(cd->volume_key->keyLength, cd->volume_key->key);
1079 r = mk ? 0 : -ENOMEM;
1081 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1084 } else if (passphrase) {
1085 /* Passphrase provided, use it to unlock existing keyslot */
1086 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, passphrase,
1087 passphrase_size, &cd->hdr, &mk, cd);
1089 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1090 key_from_terminal(cd, _("Enter any passphrase: "),
1091 &password, &passwordLen, 0);
1097 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
1098 passwordLen, &cd->hdr, &mk, cd);
1099 safe_free(password);
1105 if (new_passphrase) {
1106 new_password = (char *)new_passphrase;
1107 new_passwordLen = new_passphrase_size;
1109 key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1110 &new_password, &new_passwordLen, 1);
1117 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1118 &cd->hdr, mk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1123 if (!new_passphrase)
1124 safe_free(new_password);
1125 LUKS_dealloc_masterkey(mk);
1126 return r ?: keyslot;
1129 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1131 const char *keyfile,
1132 size_t keyfile_size,
1133 const char *new_keyfile,
1134 size_t new_keyfile_size)
1136 struct luks_masterkey *mk=NULL;
1137 char *password=NULL; unsigned int passwordLen;
1138 char *new_password = NULL; unsigned int new_passwordLen;
1141 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1142 keyfile ?: "[none]", new_keyfile ?: "[none]");
1144 if (!isLUKS(cd->type)) {
1145 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1149 r = keyslot_verify_or_find_empty(cd, &keyslot);
1153 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1154 /* No slots used, try to use pre-generated key in header */
1155 if (cd->volume_key) {
1156 mk = LUKS_alloc_masterkey(cd->volume_key->keyLength, cd->volume_key->key);
1157 r = mk ? 0 : -ENOMEM;
1159 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1163 /* Read password from file of (if NULL) from terminal */
1165 key_from_file(cd, _("Enter any passphrase: "), &password, &passwordLen,
1166 keyfile, keyfile_size);
1168 key_from_terminal(cd, _("Enter any passphrase: "),
1169 &password, &passwordLen, 1);
1174 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password, passwordLen,
1176 safe_free(password);
1183 key_from_file(cd, _("Enter new passphrase for key slot: "),
1184 &new_password, &new_passwordLen, new_keyfile,
1187 key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1188 &new_password, &new_passwordLen, 1);
1195 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1196 &cd->hdr, mk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1198 safe_free(new_password);
1199 LUKS_dealloc_masterkey(mk);
1200 return r < 0 ? r : keyslot;
1203 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1205 const char *volume_key,
1206 size_t volume_key_size,
1207 const char *passphrase,
1208 size_t passphrase_size
1211 struct luks_masterkey *mk;
1213 char *new_password = NULL; unsigned int new_passwordLen;
1215 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1217 if (!isLUKS(cd->type)) {
1218 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1223 mk = LUKS_alloc_masterkey(volume_key_size, volume_key);
1224 else if (cd->volume_key)
1225 mk = LUKS_alloc_masterkey(cd->volume_key->keyLength, cd->volume_key->key);
1230 r = LUKS_verify_master_key(&cd->hdr, mk);
1232 log_err(cd, _("Volume key does not match the volume.\n"));
1236 r = keyslot_verify_or_find_empty(cd, &keyslot);
1241 key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1242 &new_password, &new_passwordLen, 1);
1243 passphrase = new_password;
1244 passphrase_size = new_passwordLen;
1247 r = LUKS_set_key(cd->device, keyslot, passphrase, passphrase_size,
1248 &cd->hdr, mk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1251 safe_free(new_password);
1252 LUKS_dealloc_masterkey(mk);
1253 return r ?: keyslot;
1256 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1258 crypt_keyslot_info ki;
1260 log_dbg("Destroying keyslot %d.", keyslot);
1262 if (!isLUKS(cd->type)) {
1263 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1267 ki = crypt_keyslot_status(cd, keyslot);
1268 if (ki == SLOT_INVALID) {
1269 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1273 if (ki == SLOT_INACTIVE) {
1274 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1278 return LUKS_del_key(cd->device, keyslot, &cd->hdr, cd);
1281 // activation/deactivation of device mapping
1282 int crypt_activate_by_passphrase(struct crypt_device *cd,
1285 const char *passphrase,
1286 size_t passphrase_size,
1289 crypt_status_info ci;
1290 struct luks_masterkey *mk = NULL;
1291 char *prompt = NULL, *passphrase_read = NULL;
1292 unsigned int passphrase_size_read;
1293 int r, tries = cd->tries;
1295 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1296 name ? "Activating" : "Checking", name ?: "",
1297 keyslot, passphrase ? "" : "[none] ");
1302 /* plain, use hashed passphrase */
1303 if (isPLAIN(cd->type))
1304 return create_device_helper(cd, name, cd->plain_hdr.hash,
1305 cd->plain_cipher, cd->plain_cipher_mode, NULL, passphrase, passphrase_size,
1306 cd->volume_key->keyLength, 0, cd->plain_hdr.skip,
1307 cd->plain_hdr.offset, cd->plain_uuid, flags & CRYPT_ACTIVATE_READONLY, 0, 0);
1310 ci = crypt_status(NULL, name);
1313 else if (ci >= ACTIVE) {
1314 log_err(cd, _("Device %s already exists.\n"), name);
1319 if(asprintf(&prompt, _("Enter passphrase for %s: "), cd->device) < 0)
1322 /* provided passphrase, do not retry */
1324 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1325 passphrase_size, &cd->hdr, &mk, cd);
1328 LUKS_dealloc_masterkey(mk);
1331 key_from_terminal(cd, prompt, &passphrase_read,
1332 &passphrase_size_read, 0);
1333 if(!passphrase_read) {
1338 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1339 passphrase_size_read, &cd->hdr, &mk, cd);
1340 safe_free(passphrase_read);
1341 passphrase_read = NULL;
1342 } while (r == -EPERM && (--tries > 0));
1347 r = open_from_hdr_and_mk(cd, mk, name, flags);
1350 LUKS_dealloc_masterkey(mk);
1353 return r < 0 ? r : keyslot;
1356 int crypt_activate_by_keyfile(struct crypt_device *cd,
1359 const char *keyfile,
1360 size_t keyfile_size,
1363 crypt_status_info ci;
1364 struct luks_masterkey *mk = NULL;
1365 char *passphrase_read = NULL;
1366 unsigned int passphrase_size_read;
1369 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1370 name, keyslot, keyfile ?: "[none]");
1372 if (!isLUKS(cd->type)) {
1373 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1378 ci = crypt_status(NULL, name);
1381 else if (ci >= ACTIVE) {
1382 log_err(cd, _("Device %s already exists.\n"), name);
1390 key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1391 &passphrase_size_read, keyfile, keyfile_size);
1392 if(!passphrase_read)
1395 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1396 passphrase_size_read, &cd->hdr, &mk, cd);
1397 safe_free(passphrase_read);
1402 r = open_from_hdr_and_mk(cd, mk, name, flags);
1405 LUKS_dealloc_masterkey(mk);
1407 return r < 0 ? r : keyslot;
1410 int crypt_activate_by_volume_key(struct crypt_device *cd,
1412 const char *volume_key,
1413 size_t volume_key_size,
1416 crypt_status_info ci;
1417 struct luks_masterkey *mk;
1420 log_dbg("Activating volume %s by volume key.", name);
1422 /* use key directly, no hash */
1423 if (isPLAIN(cd->type))
1424 return create_device_helper(cd, name, NULL,
1425 cd->plain_cipher, cd->plain_cipher_mode, NULL, volume_key, volume_key_size,
1426 cd->volume_key->keyLength, 0, cd->plain_hdr.skip,
1427 cd->plain_hdr.offset, cd->plain_uuid, flags & CRYPT_ACTIVATE_READONLY, 0, 0);
1429 if (!isLUKS(cd->type)) {
1430 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1435 ci = crypt_status(NULL, name);
1438 else if (ci >= ACTIVE) {
1439 log_err(cd, _("Device %s already exists.\n"), name);
1444 mk = LUKS_alloc_masterkey(volume_key_size, volume_key);
1447 r = LUKS_verify_master_key(&cd->hdr, mk);
1450 log_err(cd, _("Volume key does not match the volume.\n"));
1453 r = open_from_hdr_and_mk(cd, mk, name, flags);
1455 LUKS_dealloc_masterkey(mk);
1460 int crypt_deactivate(struct crypt_device *cd, const char *name)
1465 log_dbg("Deactivating volume %s.", name);
1467 switch (crypt_status(cd, name)) {
1468 case ACTIVE: return dm_remove_device(name, 0, 0);
1469 case BUSY: log_err(cd, _("Device %s is busy."), name);
1471 case INACTIVE: log_err(cd, _("Device %s is not active."), name);
1473 default: log_err(cd, _("Invalid device %s."), name);
1478 // misc helper functions
1479 int crypt_volume_key_get(struct crypt_device *cd,
1482 size_t *volume_key_size,
1483 const char *passphrase,
1484 size_t passphrase_size)
1486 struct luks_masterkey *mk;
1487 char *processed_key = NULL;
1490 key_len = crypt_get_volume_key_size(cd);
1491 if (key_len > *volume_key_size) {
1492 log_err(cd, _("Volume key buffer too small.\n"));
1496 if (isPLAIN(cd->type)) {
1497 processed_key = process_key(cd, cd->plain_hdr.hash, NULL, key_len,
1498 passphrase, passphrase_size);
1499 if (!processed_key) {
1500 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
1503 memcpy(volume_key, processed_key, key_len);
1504 *volume_key_size = key_len;
1505 safe_free(processed_key);
1509 if (isLUKS(cd->type)) {
1510 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1511 passphrase_size, &cd->hdr, &mk, cd);
1514 memcpy(volume_key, mk->key, mk->keyLength);
1515 *volume_key_size = mk->keyLength;
1518 LUKS_dealloc_masterkey(mk);
1522 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
1526 int crypt_volume_key_verify(struct crypt_device *cd,
1527 const char *volume_key,
1528 size_t volume_key_size)
1530 struct luks_masterkey *mk;
1533 if (!isLUKS(cd->type)) {
1534 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1538 mk = LUKS_alloc_masterkey(volume_key_size, volume_key);
1542 r = LUKS_verify_master_key(&cd->hdr, mk);
1545 log_err(cd, _("Volume key does not match the volume.\n"));
1547 LUKS_dealloc_masterkey(mk);
1552 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
1554 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
1555 cd->timeout = timeout_sec;
1558 void crypt_set_password_retry(struct crypt_device *cd, int tries)
1560 log_dbg("Password retry count set to %d.", tries);
1564 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
1566 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
1567 cd->iteration_time = iteration_time_ms;
1570 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
1572 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
1573 cd->password_verify = password_verify ? 1 : 0;
1576 int crypt_memory_lock(struct crypt_device *cd, int lock)
1578 return lock ? memlock_inc(cd) : memlock_dec(cd);
1582 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
1586 r = dm_status_device(name);
1588 if (r < 0 && r != -ENODEV)
1600 static void hexprintICB(struct crypt_device *cd, char *d, int n)
1603 for(i = 0; i < n; i++)
1604 log_std(cd, "%02hhx ", (char)d[i]);
1607 int crypt_dump(struct crypt_device *cd)
1610 if (!isLUKS(cd->type)) { //FIXME
1611 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1615 log_std(cd, "LUKS header information for %s\n\n", cd->device);
1616 log_std(cd, "Version: \t%d\n", cd->hdr.version);
1617 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
1618 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
1619 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec);
1620 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
1621 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
1622 log_std(cd, "MK digest: \t");
1623 hexprintICB(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE);
1625 log_std(cd, "MK salt: \t");
1626 hexprintICB(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2);
1627 log_std(cd, "\n \t");
1628 hexprintICB(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
1630 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
1631 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
1632 for(i = 0; i < LUKS_NUMKEYS; i++) {
1633 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
1634 log_std(cd, "Key Slot %d: ENABLED\n",i);
1635 log_std(cd, "\tIterations: \t%d\n",
1636 cd->hdr.keyblock[i].passwordIterations);
1637 log_std(cd, "\tSalt: \t");
1638 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt,
1640 log_std(cd, "\n\t \t");
1641 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt +
1642 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
1645 log_std(cd, "\tKey material offset:\t%d\n",
1646 cd->hdr.keyblock[i].keyMaterialOffset);
1647 log_std(cd, "\tAF stripes: \t%d\n",
1648 cd->hdr.keyblock[i].stripes);
1651 log_std(cd, "Key Slot %d: DISABLED\n", i);
1656 const char *crypt_get_cipher(struct crypt_device *cd)
1658 if (isPLAIN(cd->type))
1659 return cd->plain_cipher;
1661 if (isLUKS(cd->type))
1662 return cd->hdr.cipherName;
1667 const char *crypt_get_cipher_mode(struct crypt_device *cd)
1669 if (isPLAIN(cd->type))
1670 return cd->plain_cipher_mode;
1672 if (isLUKS(cd->type))
1673 return cd->hdr.cipherMode;
1678 const char *crypt_get_uuid(struct crypt_device *cd)
1680 if (isLUKS(cd->type))
1681 return cd->hdr.uuid;
1686 int crypt_get_volume_key_size(struct crypt_device *cd)
1688 if (isPLAIN(cd->type))
1689 return cd->volume_key->keyLength;
1691 if (isLUKS(cd->type))
1692 return cd->hdr.keyBytes;
1697 uint64_t crypt_get_data_offset(struct crypt_device *cd)
1699 if (isPLAIN(cd->type))
1700 return cd->plain_hdr.offset;
1702 if (isLUKS(cd->type))
1703 return cd->hdr.payloadOffset;
1708 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
1710 if (!isLUKS(cd->type)) {
1711 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1712 return SLOT_INVALID;
1715 return LUKS_keyslot_info(&cd->hdr, keyslot);