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, const char *msg, void *usrptr) = NULL;
43 static int _debug_level = 0;
45 void crypt_set_debug_level(int level)
50 void crypt_log(struct crypt_device *cd, int class, const char *msg)
53 cd->log(class, msg, cd->log_usrptr);
54 else if (_default_log)
55 _default_log(class, msg, NULL);
58 void logger(struct crypt_device *cd, int class, const char *file,
59 int line, const char *format, ...)
64 va_start(argp, format);
66 if (vasprintf(&target, format, argp) > 0) {
68 crypt_log(cd, 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 ki = crypt_keyslot_status(cd, keyIndex);
202 if (ki == SLOT_ACTIVE) /* Not last slot */
203 LUKS_keyslot_set(&cd->hdr, keyIndex, 0);
205 openedIndex = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT,
206 password, passwordLen,
209 if (ki == SLOT_ACTIVE)
210 LUKS_keyslot_set(&cd->hdr, keyIndex, 1);
211 LUKS_dealloc_masterkey(mk);
217 log_std(cd, _("Key slot %d verified.\n"), openedIndex);
221 static int find_keyslot_by_passphrase(struct crypt_device *cd,
222 const char *key_file,
226 struct luks_masterkey *mk;
227 char *password = NULL;
228 unsigned int passwordLen;
231 get_key(message,&password,&passwordLen, 0, key_file,
232 cd->timeout, flags, cd);
236 keyIndex = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
237 passwordLen, &cd->hdr, &mk, cd);
238 LUKS_dealloc_masterkey(mk);
244 static int device_check_and_adjust(struct crypt_device *cd,
246 uint64_t *size, uint64_t *offset,
249 struct device_infos infos;
251 if (get_device_infos(device, &infos, cd) < 0) {
252 log_err(cd, _("Cannot get info about device %s.\n"), device);
259 log_err(cd, _("Device %s has zero size.\n"), device);
262 if (*size <= *offset) {
263 log_err(cd, _("Device %s is too small.\n"), device);
275 static int luks_remove_helper(struct crypt_device *cd,
277 const char *other_key_file,
278 const char *key_file,
281 crypt_keyslot_info ki;
284 if (key_slot == CRYPT_ANY_SLOT) {
285 key_slot = find_keyslot_by_passphrase(cd, key_file, 0,
286 _("Enter LUKS passphrase to be deleted: "));
292 log_std(cd, _("key slot %d selected for deletion.\n"), key_slot);
295 ki = crypt_keyslot_status(cd, key_slot);
296 if (ki == SLOT_INVALID) {
297 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
298 key_slot, LUKS_NUMKEYS - 1);
302 if (ki <= SLOT_INACTIVE) {
303 log_err(cd, _("Key %d not active. Can't wipe.\n"), key_slot);
308 if (ki == SLOT_ACTIVE_LAST && cd->confirm &&
309 !(cd->confirm(_("This is the last keyslot."
310 " Device will become unusable after purging this key."),
311 cd->confirm_usrptr))) {
317 r = verify_other_keyslot(cd, other_key_file, 0, key_slot);
322 r = crypt_keyslot_destroy(cd, key_slot);
324 return (r < 0) ? r : 0;
327 static int create_device_helper(struct crypt_device *cd,
331 const char *cipher_mode,
332 const char *key_file,
344 crypt_status_info ci;
345 char *dm_cipher = NULL;
346 char *processed_key = NULL;
349 ci = crypt_status(cd, name);
353 if (reload && ci < ACTIVE)
356 if (!reload && ci >= ACTIVE) {
357 log_err(cd, _("Device %s already exists.\n"), name);
361 if (key_size < 0 || key_size > 1024) {
362 log_err(cd, _("Invalid key size %d.\n"), key_size);
366 r = device_check_and_adjust(cd, cd->device, &size, &offset, &read_only);
370 if (cipher_mode && asprintf(&dm_cipher, "%s-%s", cipher, cipher_mode) < 0)
373 processed_key = process_key(cd, hash, key_file, key_size, key, keyLen);
377 r = dm_create_device(name, cd->device, dm_cipher ?: cipher, cd->type, uuid, size, skip, offset,
378 key_size, processed_key, read_only, reload);
381 safe_free(processed_key);
385 static int open_from_hdr_and_mk(struct crypt_device *cd,
386 struct luks_masterkey *mk,
390 uint64_t size, offset;
392 int read_only, no_uuid, r;
395 offset = crypt_get_data_offset(cd);
396 read_only = flags & CRYPT_ACTIVATE_READONLY;
397 no_uuid = flags & CRYPT_ACTIVATE_NO_UUID;
399 r = device_check_and_adjust(cd, cd->device, &size, &offset, &read_only);
403 if (asprintf(&cipher, "%s-%s", crypt_get_cipher(cd),
404 crypt_get_cipher_mode(cd)) < 0)
407 r = dm_create_device(name, cd->device, cipher, cd->type,
408 no_uuid ? NULL : crypt_get_uuid(cd),
409 size, 0, offset, mk->keyLength, mk->key,
415 static void log_wrapper(int class, const char *msg, void *usrptr)
417 void (*xlog)(int class, char *msg) = usrptr;
418 xlog(class, (char *)msg);
421 static int yesDialog_wrapper(const char *msg, void *usrptr)
423 int (*xyesDialog)(char *msg) = usrptr;
424 return xyesDialog((char*)msg);
427 int crypt_confirm(struct crypt_device *cd, const char *msg)
429 if (!cd || !cd->confirm)
432 return cd->confirm(msg, cd->confirm_usrptr);
435 static void key_from_terminal(struct crypt_device *cd, char *msg, char **key,
436 unsigned int *key_len, int force_verify)
441 *key = safe_alloc(MAX_TTY_PASSWORD_LEN);
444 r = cd->password(msg, *key, (size_t)key_len, cd->password_usrptr);
451 if (force_verify || cd->password_verify)
452 flags |= CRYPT_FLAG_VERIFY_IF_POSSIBLE;
453 get_key(msg, key, key_len, 0, NULL, cd->timeout, flags, cd);
457 static int volume_key_by_terminal_passphrase(struct crypt_device *cd, int keyslot,
458 struct luks_masterkey **mk)
460 char *prompt = NULL, *passphrase_read = NULL;
461 unsigned int passphrase_size_read;
462 int r = -EINVAL, tries = cd->tries;
464 if(asprintf(&prompt, _("Enter passphrase for %s: "), cd->device) < 0)
470 LUKS_dealloc_masterkey(*mk);
473 key_from_terminal(cd, prompt, &passphrase_read,
474 &passphrase_size_read, 0);
475 if(!passphrase_read) {
480 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
481 passphrase_size_read, &cd->hdr, mk, cd);
482 safe_free(passphrase_read);
483 passphrase_read = NULL;
484 } while (r == -EPERM && (--tries > 0));
487 LUKS_dealloc_masterkey(*mk);
496 static void key_from_file(struct crypt_device *cd, char *msg,
497 char **key, unsigned int *key_len,
498 const char *key_file, size_t key_size)
500 get_key(msg, key, key_len, key_size, key_file, cd->timeout, 0, cd);
503 static int _crypt_init(struct crypt_device **cd,
505 struct crypt_options *options,
506 int load, int need_dm)
510 /* if it is plain device and mapping table is being reloaded
511 initialize it by name*/
512 init_by_name = (type && !strcmp(type, CRYPT_PLAIN) && load);
514 /* Some of old API calls do not require DM in kernel,
515 fake initialisation by initialise it with kernel_check disabled */
517 (void)dm_init(NULL, 0);
519 r = crypt_init_by_name(cd, options->name);
521 r = crypt_init(cd, options->device);
528 crypt_set_log_callback(*cd, log_wrapper, options->icb->log);
529 crypt_set_confirm_callback(*cd, yesDialog_wrapper, options->icb->yesDialog);
531 crypt_set_timeout(*cd, options->timeout);
532 crypt_set_password_retry(*cd, options->tries);
533 crypt_set_iterarion_time(*cd, options->iteration_time ?: 1000);
534 crypt_set_password_verify(*cd, options->flags & CRYPT_FLAG_VERIFY);
536 if (load && !init_by_name)
537 r = crypt_load(*cd, type, NULL);
539 if (!r && type && !(*cd)->type) {
540 (*cd)->type = strdup(type);
551 void crypt_set_log_callback(struct crypt_device *cd,
552 void (*log)(int class, const char *msg, void *usrptr),
559 cd->log_usrptr = usrptr;
563 void crypt_set_confirm_callback(struct crypt_device *cd,
564 int (*confirm)(const char *msg, void *usrptr),
567 cd->confirm = confirm;
568 cd->confirm_usrptr = usrptr;
571 void crypt_set_password_callback(struct crypt_device *cd,
572 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
575 cd->password = password;
576 cd->password_usrptr = usrptr;
579 /* OPTIONS: name, cipher, device, hash, key_file, key_size, key_slot,
580 * offset, size, skip, timeout, tries, passphrase_fd (ignored),
582 int crypt_create_device(struct crypt_options *options)
584 struct crypt_device *cd = NULL;
589 r = _crypt_init(&cd, CRYPT_PLAIN, options, 0, 1);
593 get_key(_("Enter passphrase: "), &key, &keyLen, options->key_size,
594 options->key_file, cd->timeout, options->flags, cd);
598 r = create_device_helper(cd, options->name, options->hash,
599 options->cipher, NULL, options->key_file, key, keyLen,
600 options->key_size, options->size, options->skip,
601 options->offset, NULL, options->flags & CRYPT_FLAG_READONLY,
609 /* OPTIONS: same as create above */
610 int crypt_update_device(struct crypt_options *options)
612 struct crypt_device *cd = NULL;
617 r = _crypt_init(&cd, CRYPT_PLAIN, options, 1, 1);
621 get_key(_("Enter passphrase: "), &key, &keyLen, options->key_size,
622 options->key_file, cd->timeout, options->flags, cd);
626 r = create_device_helper(cd, options->name, options->hash,
627 options->cipher, NULL, options->key_file, key, keyLen,
628 options->key_size, options->size, options->skip,
629 options->offset, NULL, options->flags & CRYPT_FLAG_READONLY,
637 /* OPTIONS: name, size, icb */
638 int crypt_resize_device(struct crypt_options *options)
640 struct crypt_device *cd = NULL;
641 char *device = NULL, *cipher = NULL, *uuid = NULL, *key = NULL;
643 uint64_t size, skip, offset;
644 int key_size, read_only, r;
646 r = dm_query_device(options->name, &device, &size, &skip, &offset,
647 &cipher, &key_size, &key, &read_only, NULL, &uuid);
651 /* Try to determine type of device from UUID */
653 if (!strncmp(uuid, CRYPT_PLAIN, strlen(CRYPT_PLAIN))) {
657 } else if (!strncmp(uuid, CRYPT_LUKS1, strlen(CRYPT_LUKS1)))
661 r = _crypt_init(&cd, type, options, 1, 1);
665 size = options->size;
666 r = device_check_and_adjust(cd, device, &size, &offset, &read_only);
670 r = dm_create_device(options->name, device, cipher, type,
671 crypt_get_uuid(cd), size, skip, offset,
672 key_size, key, read_only, 1);
682 /* OPTIONS: name, icb */
683 int crypt_query_device(struct crypt_options *options)
687 r = dm_status_device(options->name);
691 r = dm_query_device(options->name, (char **)&options->device, &options->size,
692 &options->skip, &options->offset, (char **)&options->cipher,
693 &options->key_size, NULL, &read_only, NULL, NULL);
699 options->flags |= CRYPT_FLAG_READONLY;
701 options->flags |= CRYPT_FLAG_FREE_DEVICE;
702 options->flags |= CRYPT_FLAG_FREE_CIPHER;
707 /* OPTIONS: name, icb */
708 int crypt_remove_device(struct crypt_options *options)
710 struct crypt_device *cd = NULL;
713 r = crypt_init_by_name(&cd, options->name);
717 r = crypt_deactivate(cd, options->name);
724 /* OPTIONS: device, cipher, hash, align_payload, key_size (master key), key_slot
725 * new_key_file, iteration_time, timeout, flags, icb */
726 int crypt_luksFormat(struct crypt_options *options)
728 char cipherName[LUKS_CIPHERNAME_L];
729 char cipherMode[LUKS_CIPHERMODE_L];
731 unsigned int passwordLen;
732 struct crypt_device *cd;
733 struct crypt_params_luks1 cp = {
734 .hash = options->hash,
735 .data_alignment = options->align_payload
739 r = parse_into_name_and_mode(options->cipher, cipherName, cipherMode);
741 log_err(cd, _("No known cipher specification pattern detected.\n"));
745 if ((r = _crypt_init(&cd, CRYPT_LUKS1, options, 0, 1)))
748 if (options->key_slot >= LUKS_NUMKEYS && options->key_slot != CRYPT_ANY_SLOT) {
749 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
750 options->key_slot, LUKS_NUMKEYS - 1);
755 get_key(_("Enter LUKS passphrase: "), &password, &passwordLen, 0,
756 options->new_key_file, options->timeout, options->flags, cd);
763 r = crypt_format(cd, CRYPT_LUKS1, cipherName, cipherMode,
764 NULL, NULL, options->key_size, &cp);
768 /* Add keyslot using internally stored volume key generated during format */
769 r = crypt_keyslot_add_by_volume_key(cd, options->key_slot, NULL, 0,
770 password, passwordLen);
774 return (r < 0) ? r : 0;
777 /* OPTIONS: name, device, key_size, key_file, timeout, tries, flags, icb */
778 int crypt_luksOpen(struct crypt_options *options)
780 struct crypt_device *cd = NULL;
787 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
791 if (options->flags & CRYPT_FLAG_READONLY)
792 flags |= CRYPT_ACTIVATE_READONLY;
794 if (options->flags & CRYPT_FLAG_NON_EXCLUSIVE_ACCESS)
795 flags |= CRYPT_ACTIVATE_NO_UUID;
797 if (options->key_file)
798 r = crypt_activate_by_keyfile(cd, options->name,
799 CRYPT_ANY_SLOT, options->key_file, options->key_size,
802 r = crypt_activate_by_passphrase(cd, options->name,
803 CRYPT_ANY_SLOT, options->passphrase,
804 options->passphrase ? strlen(options->passphrase) : 0,
808 return (r < 0) ? r : 0;
811 /* OPTIONS: device, keys_slot, key_file, timeout, flags, icb */
812 int crypt_luksKillSlot(struct crypt_options *options)
814 struct crypt_device *cd = NULL;
817 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
821 r = luks_remove_helper(cd, options->key_slot, options->key_file, NULL,
822 options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
825 return (r < 0) ? r : 0;
828 /* OPTIONS: device, new_key_file, key_file, timeout, flags, icb */
829 int crypt_luksRemoveKey(struct crypt_options *options)
831 struct crypt_device *cd = NULL;
834 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
838 r = luks_remove_helper(cd, CRYPT_ANY_SLOT, options->key_file, options->new_key_file,
839 options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY);
842 return (r < 0) ? r : 0;
846 /* OPTIONS: device, new_key_file, key_file, key_slot, flags,
847 iteration_time, timeout, icb */
848 int crypt_luksAddKey(struct crypt_options *options)
850 struct crypt_device *cd = NULL;
853 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 1);
857 if (options->key_file || options->new_key_file)
858 r = crypt_keyslot_add_by_keyfile(cd, options->key_slot,
859 options->key_file, 0,
860 options->new_key_file, 0);
862 r = crypt_keyslot_add_by_passphrase(cd, options->key_slot,
866 return (r < 0) ? r : 0;
869 /* OPTIONS: device, icb */
870 int crypt_luksUUID(struct crypt_options *options)
872 struct crypt_device *cd = NULL;
876 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
880 uuid = (char *)crypt_get_uuid(cd);
881 log_std(cd, uuid ?: "");
887 /* OPTIONS: device, icb */
888 int crypt_isLuks(struct crypt_options *options)
890 struct crypt_device *cd = NULL;
893 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
899 /* OPTIONS: device, icb */
900 int crypt_luksDump(struct crypt_options *options)
902 struct crypt_device *cd = NULL;
905 r = _crypt_init(&cd, CRYPT_LUKS1, options, 1, 0);
915 void crypt_get_error(char *buf, size_t size)
917 const char *error = get_error();
919 if (!buf || size < 1)
922 strncpy(buf, error, size - 1);
923 buf[size - 1] = '\0';
929 void crypt_put_options(struct crypt_options *options)
931 if (options->flags & CRYPT_FLAG_FREE_DEVICE) {
932 free((char *)options->device);
933 options->device = NULL;
934 options->flags &= ~CRYPT_FLAG_FREE_DEVICE;
936 if (options->flags & CRYPT_FLAG_FREE_CIPHER) {
937 free((char *)options->cipher);
938 options->cipher = NULL;
939 options->flags &= ~CRYPT_FLAG_FREE_CIPHER;
943 const char *crypt_get_dir(void)
948 /////////////////////////////////
953 int crypt_init(struct crypt_device **cd, const char *device)
955 struct crypt_device *h = NULL;
960 log_dbg("Allocating crypt device %s context.", device);
962 if (!device_ready(NULL, device, O_RDONLY))
965 if (!(h = malloc(sizeof(struct crypt_device))))
968 memset(h, 0, sizeof(*h));
970 h->device = strdup(device);
976 if (!dm_init(h, 1) < 0) {
981 h->iteration_time = 1000;
982 h->password_verify = 0;
988 int crypt_init_by_name(struct crypt_device **cd, const char *name)
990 crypt_status_info ci;
994 log_dbg("Allocating crypt device context by device %s.", name);
996 ci = crypt_status(NULL, name);
998 log_err(NULL, _("Device %s is not active.\n"), name);
1002 r = dm_query_device(name, &device, NULL, NULL, NULL,
1003 NULL, NULL, NULL, NULL, NULL, NULL);
1005 r = crypt_init(cd, device);
1011 static int _crypt_format_plain(struct crypt_device *cd,
1013 const char *cipher_mode,
1015 struct crypt_params_plain *params)
1017 if (!cipher || !cipher_mode || !params || !params->hash) {
1018 log_err(cd, _("Invalid plain crypt parameters.\n"));
1022 if (cd->volume_key->keyLength > 1024) {
1023 log_err(cd, _("Invalid key size.\n"));
1027 cd->plain_cipher = strdup(cipher);
1028 cd->plain_cipher_mode = strdup(cipher_mode);
1031 cd->plain_uuid = strdup(uuid);
1034 cd->plain_hdr.hash = strdup(params->hash);
1036 cd->plain_hdr.offset = params->offset;
1037 cd->plain_hdr.skip = params->skip;
1039 if ((params->hash && !cd->plain_hdr.hash) ||
1040 !cd->plain_cipher || !cd->plain_cipher_mode)
1046 static int _crypt_format_luks1(struct crypt_device *cd,
1048 const char *cipher_mode,
1050 struct crypt_params_luks1 *params)
1054 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
1055 (params && params->hash) ? params->hash : "sha1",
1057 params ? params->data_alignment: DEFAULT_ALIGNMENT, cd);
1061 /* Wipe first 8 sectors - fs magic numbers etc. */
1062 r = wipe_device_header(cd->device, 8);
1064 log_err(cd, _("Can't wipe header on device %s.\n"), cd->device);
1068 r = LUKS_write_phdr(cd->device, &cd->hdr, cd);
1073 int crypt_format(struct crypt_device *cd,
1076 const char *cipher_mode,
1078 const char *volume_key,
1079 size_t volume_key_size,
1084 log_dbg("Formatting device %s as type %s.", cd->device, cd->type ?: "(none)");
1090 cd->volume_key = LUKS_alloc_masterkey(volume_key_size,
1093 cd->volume_key = LUKS_generate_masterkey(volume_key_size);
1099 r = _crypt_format_plain(cd, cipher, cipher_mode,
1101 else if (isLUKS(type))
1102 r = _crypt_format_luks1(cd, cipher, cipher_mode,
1105 /* FIXME: allow plugins here? */
1106 log_err(cd, _("Unkown crypt device type %s requesed.\n"), type);
1110 if (!r && !(cd->type = strdup(type)))
1114 LUKS_dealloc_masterkey(cd->volume_key);
1115 cd->volume_key = NULL;
1121 int crypt_load(struct crypt_device *cd,
1122 const char *requested_type,
1125 struct luks_phdr hdr;
1128 log_dbg("Trying to load %s crypt type from device %s.",
1129 requested_type ?: "any", cd->device);
1131 if (requested_type && !isPLAIN(requested_type) && !isLUKS(requested_type))
1134 /* Some hash functions need initialized gcrypt library */
1135 if (init_crypto()) {
1136 log_err(cd, _("Cannot initialize crypto backend.\n"));
1140 r = LUKS_read_phdr(cd->device, &hdr, 0, cd);
1143 memcpy(&cd->hdr, &hdr, sizeof(hdr));
1144 cd->type = strdup(requested_type);
1152 int crypt_header_backup(struct crypt_device *cd,
1153 const char *requested_type,
1154 const char *backup_file)
1156 if ((requested_type && !isLUKS(requested_type)) || !backup_file)
1159 log_dbg("Requested header backup of device %s (%s) to "
1160 "file %s.", cd->device, requested_type, backup_file);
1162 return LUKS_hdr_backup(backup_file, cd->device, &cd->hdr, cd);
1165 int crypt_header_restore(struct crypt_device *cd,
1166 const char *requested_type,
1167 const char *backup_file)
1169 if (requested_type && !isLUKS(requested_type))
1172 log_dbg("Requested header restore to device %s (%s) from "
1173 "file %s.", cd->device, requested_type, backup_file);
1175 return LUKS_hdr_restore(backup_file, cd->device, &cd->hdr, cd);
1178 void crypt_free(struct crypt_device *cd)
1181 log_dbg("Releasing crypt device %s context.", cd->device);
1185 LUKS_dealloc_masterkey(cd->volume_key);
1190 /* used in plain device only */
1191 free((char*)cd->plain_hdr.hash);
1192 free(cd->plain_cipher);
1193 free(cd->plain_cipher_mode);
1194 free(cd->plain_uuid);
1200 int crypt_suspend(struct crypt_device *cd,
1203 crypt_status_info ci;
1204 int r, suspended = 0;
1206 log_dbg("Suspending volume %s.", name);
1208 ci = crypt_status(NULL, name);
1210 log_err(cd, _("Volume %s is not active.\n"), name);
1214 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1215 NULL, NULL, NULL, NULL, &suspended, NULL);
1220 log_err(cd, _("Volume %s is already suspended.\n"), name);
1224 r = dm_suspend_and_wipe_key(name);
1226 log_err(cd, "Error during suspending device %s.\n", name);
1231 int crypt_resume_by_passphrase(struct crypt_device *cd,
1234 const char *passphrase,
1235 size_t passphrase_size)
1237 struct luks_masterkey *mk = NULL;
1238 int r, suspended = 0;
1240 log_dbg("Resuming volume %s.", name);
1242 if (!isLUKS(cd->type)) {
1243 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1248 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1249 NULL, NULL, NULL, NULL, &suspended, NULL);
1254 log_err(cd, _("Volume %s is not suspended.\n"), name);
1259 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1260 passphrase_size, &cd->hdr, &mk, cd);
1262 r = volume_key_by_terminal_passphrase(cd, keyslot, &mk);
1266 r = dm_resume_and_reinstate_key(name, mk->keyLength, mk->key);
1268 log_err(cd, "Error during resuming device %s.\n", name);
1272 LUKS_dealloc_masterkey(mk);
1273 return r < 0 ? r : keyslot;
1276 int crypt_resume_by_keyfile(struct crypt_device *cd,
1279 const char *keyfile,
1280 size_t keyfile_size)
1282 struct luks_masterkey *mk = NULL;
1283 char *passphrase_read = NULL;
1284 unsigned int passphrase_size_read;
1285 int r, suspended = 0;
1287 log_dbg("Resuming volume %s.", name);
1289 if (!isLUKS(cd->type)) {
1290 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1295 r = dm_query_device(name, NULL, NULL, NULL, NULL,
1296 NULL, NULL, NULL, NULL, &suspended, NULL);
1301 log_err(cd, _("Volume %s is not suspended.\n"), name);
1308 key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1309 &passphrase_size_read, keyfile, keyfile_size);
1311 if(!passphrase_read)
1314 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1315 passphrase_size_read, &cd->hdr, &mk, cd);
1316 safe_free(passphrase_read);
1321 r = dm_resume_and_reinstate_key(name, mk->keyLength, mk->key);
1323 log_err(cd, "Error during resuming device %s.\n", name);
1327 LUKS_dealloc_masterkey(mk);
1328 return r < 0 ? r : keyslot;
1331 // slot manipulation
1332 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1333 int keyslot, // -1 any
1334 const char *passphrase, // NULL -> terminal
1335 size_t passphrase_size,
1336 const char *new_passphrase, // NULL -> terminal
1337 size_t new_passphrase_size)
1339 struct luks_masterkey *mk = NULL;
1340 char *password = NULL, *new_password = NULL;
1341 unsigned int passwordLen, new_passwordLen;
1344 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1345 "new passphrase %sprovided.",
1346 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1348 if (!isLUKS(cd->type)) {
1349 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1353 r = keyslot_verify_or_find_empty(cd, &keyslot);
1357 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1358 /* No slots used, try to use pre-generated key in header */
1359 if (cd->volume_key) {
1360 mk = LUKS_alloc_masterkey(cd->volume_key->keyLength, cd->volume_key->key);
1361 r = mk ? 0 : -ENOMEM;
1363 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1366 } else if (passphrase) {
1367 /* Passphrase provided, use it to unlock existing keyslot */
1368 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, passphrase,
1369 passphrase_size, &cd->hdr, &mk, cd);
1371 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1372 key_from_terminal(cd, _("Enter any passphrase: "),
1373 &password, &passwordLen, 0);
1379 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
1380 passwordLen, &cd->hdr, &mk, cd);
1381 safe_free(password);
1387 if (new_passphrase) {
1388 new_password = (char *)new_passphrase;
1389 new_passwordLen = new_passphrase_size;
1391 key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1392 &new_password, &new_passwordLen, 1);
1399 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1400 &cd->hdr, mk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1405 if (!new_passphrase)
1406 safe_free(new_password);
1407 LUKS_dealloc_masterkey(mk);
1408 return r ?: keyslot;
1411 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1413 const char *keyfile,
1414 size_t keyfile_size,
1415 const char *new_keyfile,
1416 size_t new_keyfile_size)
1418 struct luks_masterkey *mk=NULL;
1419 char *password=NULL; unsigned int passwordLen;
1420 char *new_password = NULL; unsigned int new_passwordLen;
1423 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1424 keyfile ?: "[none]", new_keyfile ?: "[none]");
1426 if (!isLUKS(cd->type)) {
1427 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1431 r = keyslot_verify_or_find_empty(cd, &keyslot);
1435 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1436 /* No slots used, try to use pre-generated key in header */
1437 if (cd->volume_key) {
1438 mk = LUKS_alloc_masterkey(cd->volume_key->keyLength, cd->volume_key->key);
1439 r = mk ? 0 : -ENOMEM;
1441 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1445 /* Read password from file of (if NULL) from terminal */
1447 key_from_file(cd, _("Enter any passphrase: "), &password, &passwordLen,
1448 keyfile, keyfile_size);
1450 key_from_terminal(cd, _("Enter any passphrase: "),
1451 &password, &passwordLen, 1);
1456 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password, passwordLen,
1458 safe_free(password);
1465 key_from_file(cd, _("Enter new passphrase for key slot: "),
1466 &new_password, &new_passwordLen, new_keyfile,
1469 key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1470 &new_password, &new_passwordLen, 1);
1477 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1478 &cd->hdr, mk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1480 safe_free(new_password);
1481 LUKS_dealloc_masterkey(mk);
1482 return r < 0 ? r : keyslot;
1485 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1487 const char *volume_key,
1488 size_t volume_key_size,
1489 const char *passphrase,
1490 size_t passphrase_size)
1492 struct luks_masterkey *mk = NULL;
1494 char *new_password = NULL; unsigned int new_passwordLen;
1496 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1498 if (!isLUKS(cd->type)) {
1499 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1504 mk = LUKS_alloc_masterkey(volume_key_size, volume_key);
1505 else if (cd->volume_key)
1506 mk = LUKS_alloc_masterkey(cd->volume_key->keyLength, cd->volume_key->key);
1511 r = LUKS_verify_master_key(&cd->hdr, mk);
1513 log_err(cd, _("Volume key does not match the volume.\n"));
1517 r = keyslot_verify_or_find_empty(cd, &keyslot);
1522 key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1523 &new_password, &new_passwordLen, 1);
1524 passphrase = new_password;
1525 passphrase_size = new_passwordLen;
1528 r = LUKS_set_key(cd->device, keyslot, passphrase, passphrase_size,
1529 &cd->hdr, mk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1532 safe_free(new_password);
1533 LUKS_dealloc_masterkey(mk);
1534 return r ?: keyslot;
1537 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1539 crypt_keyslot_info ki;
1541 log_dbg("Destroying keyslot %d.", keyslot);
1543 if (!isLUKS(cd->type)) {
1544 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1548 ki = crypt_keyslot_status(cd, keyslot);
1549 if (ki == SLOT_INVALID) {
1550 log_err(cd, _("Key slot %d is invalid.\n"), keyslot);
1554 if (ki == SLOT_INACTIVE) {
1555 log_err(cd, _("Key slot %d is not used.\n"), keyslot);
1559 return LUKS_del_key(cd->device, keyslot, &cd->hdr, cd);
1562 // activation/deactivation of device mapping
1563 int crypt_activate_by_passphrase(struct crypt_device *cd,
1566 const char *passphrase,
1567 size_t passphrase_size,
1570 crypt_status_info ci;
1571 struct luks_masterkey *mk = NULL;
1572 char *prompt = NULL;
1575 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1576 name ? "Activating" : "Checking", name ?: "",
1577 keyslot, passphrase ? "" : "[none] ");
1582 /* plain, use hashed passphrase */
1583 if (isPLAIN(cd->type))
1584 return create_device_helper(cd, name, cd->plain_hdr.hash,
1585 cd->plain_cipher, cd->plain_cipher_mode, NULL, passphrase, passphrase_size,
1586 cd->volume_key->keyLength, 0, cd->plain_hdr.skip,
1587 cd->plain_hdr.offset, cd->plain_uuid, flags & CRYPT_ACTIVATE_READONLY, 0, 0);
1590 ci = crypt_status(NULL, name);
1593 else if (ci >= ACTIVE) {
1594 log_err(cd, _("Device %s already exists.\n"), name);
1599 if(asprintf(&prompt, _("Enter passphrase for %s: "), cd->device) < 0)
1602 /* provided passphrase, do not retry */
1604 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1605 passphrase_size, &cd->hdr, &mk, cd);
1607 r = volume_key_by_terminal_passphrase(cd, keyslot, &mk);
1612 r = open_from_hdr_and_mk(cd, mk, name, flags);
1615 LUKS_dealloc_masterkey(mk);
1618 return r < 0 ? r : keyslot;
1621 int crypt_activate_by_keyfile(struct crypt_device *cd,
1624 const char *keyfile,
1625 size_t keyfile_size,
1628 crypt_status_info ci;
1629 struct luks_masterkey *mk = NULL;
1630 char *passphrase_read = NULL;
1631 unsigned int passphrase_size_read;
1634 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1635 name, keyslot, keyfile ?: "[none]");
1637 if (!isLUKS(cd->type)) {
1638 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1643 ci = crypt_status(NULL, name);
1646 else if (ci >= ACTIVE) {
1647 log_err(cd, _("Device %s already exists.\n"), name);
1655 key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1656 &passphrase_size_read, keyfile, keyfile_size);
1657 if(!passphrase_read)
1660 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1661 passphrase_size_read, &cd->hdr, &mk, cd);
1662 safe_free(passphrase_read);
1667 r = open_from_hdr_and_mk(cd, mk, name, flags);
1670 LUKS_dealloc_masterkey(mk);
1672 return r < 0 ? r : keyslot;
1675 int crypt_activate_by_volume_key(struct crypt_device *cd,
1677 const char *volume_key,
1678 size_t volume_key_size,
1681 crypt_status_info ci;
1682 struct luks_masterkey *mk;
1685 log_dbg("Activating volume %s by volume key.", name);
1687 /* use key directly, no hash */
1688 if (isPLAIN(cd->type))
1689 return create_device_helper(cd, name, NULL,
1690 cd->plain_cipher, cd->plain_cipher_mode, NULL, volume_key, volume_key_size,
1691 cd->volume_key->keyLength, 0, cd->plain_hdr.skip,
1692 cd->plain_hdr.offset, cd->plain_uuid, flags & CRYPT_ACTIVATE_READONLY, 0, 0);
1694 if (!isLUKS(cd->type)) {
1695 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1700 ci = crypt_status(NULL, name);
1703 else if (ci >= ACTIVE) {
1704 log_err(cd, _("Device %s already exists.\n"), name);
1709 mk = LUKS_alloc_masterkey(volume_key_size, volume_key);
1712 r = LUKS_verify_master_key(&cd->hdr, mk);
1715 log_err(cd, _("Volume key does not match the volume.\n"));
1718 r = open_from_hdr_and_mk(cd, mk, name, flags);
1720 LUKS_dealloc_masterkey(mk);
1725 int crypt_deactivate(struct crypt_device *cd, const char *name)
1730 log_dbg("Deactivating volume %s.", name);
1732 switch (crypt_status(cd, name)) {
1733 case ACTIVE: return dm_remove_device(name, 0, 0);
1734 case BUSY: log_err(cd, _("Device %s is busy."), name);
1736 case INACTIVE: log_err(cd, _("Device %s is not active."), name);
1738 default: log_err(cd, _("Invalid device %s."), name);
1743 // misc helper functions
1744 int crypt_volume_key_get(struct crypt_device *cd,
1747 size_t *volume_key_size,
1748 const char *passphrase,
1749 size_t passphrase_size)
1751 struct luks_masterkey *mk;
1752 char *processed_key = NULL;
1755 key_len = crypt_get_volume_key_size(cd);
1756 if (key_len > *volume_key_size) {
1757 log_err(cd, _("Volume key buffer too small.\n"));
1761 if (isPLAIN(cd->type)) {
1762 processed_key = process_key(cd, cd->plain_hdr.hash, NULL, key_len,
1763 passphrase, passphrase_size);
1764 if (!processed_key) {
1765 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
1768 memcpy(volume_key, processed_key, key_len);
1769 *volume_key_size = key_len;
1770 safe_free(processed_key);
1774 if (isLUKS(cd->type)) {
1775 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1776 passphrase_size, &cd->hdr, &mk, cd);
1779 memcpy(volume_key, mk->key, mk->keyLength);
1780 *volume_key_size = mk->keyLength;
1783 LUKS_dealloc_masterkey(mk);
1787 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
1791 int crypt_volume_key_verify(struct crypt_device *cd,
1792 const char *volume_key,
1793 size_t volume_key_size)
1795 struct luks_masterkey *mk;
1798 if (!isLUKS(cd->type)) {
1799 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1803 mk = LUKS_alloc_masterkey(volume_key_size, volume_key);
1807 r = LUKS_verify_master_key(&cd->hdr, mk);
1810 log_err(cd, _("Volume key does not match the volume.\n"));
1812 LUKS_dealloc_masterkey(mk);
1817 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
1819 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
1820 cd->timeout = timeout_sec;
1823 void crypt_set_password_retry(struct crypt_device *cd, int tries)
1825 log_dbg("Password retry count set to %d.", tries);
1829 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
1831 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
1832 cd->iteration_time = iteration_time_ms;
1835 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
1837 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
1838 cd->password_verify = password_verify ? 1 : 0;
1841 int crypt_memory_lock(struct crypt_device *cd, int lock)
1843 return lock ? memlock_inc(cd) : memlock_dec(cd);
1847 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
1851 r = dm_status_device(name);
1853 if (r < 0 && r != -ENODEV)
1865 static void hexprintICB(struct crypt_device *cd, char *d, int n)
1868 for(i = 0; i < n; i++)
1869 log_std(cd, "%02hhx ", (char)d[i]);
1872 int crypt_dump(struct crypt_device *cd)
1875 if (!isLUKS(cd->type)) { //FIXME
1876 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1880 log_std(cd, "LUKS header information for %s\n\n", cd->device);
1881 log_std(cd, "Version: \t%d\n", cd->hdr.version);
1882 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
1883 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
1884 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec);
1885 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
1886 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
1887 log_std(cd, "MK digest: \t");
1888 hexprintICB(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE);
1890 log_std(cd, "MK salt: \t");
1891 hexprintICB(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2);
1892 log_std(cd, "\n \t");
1893 hexprintICB(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
1895 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
1896 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
1897 for(i = 0; i < LUKS_NUMKEYS; i++) {
1898 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
1899 log_std(cd, "Key Slot %d: ENABLED\n",i);
1900 log_std(cd, "\tIterations: \t%d\n",
1901 cd->hdr.keyblock[i].passwordIterations);
1902 log_std(cd, "\tSalt: \t");
1903 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt,
1905 log_std(cd, "\n\t \t");
1906 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt +
1907 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
1910 log_std(cd, "\tKey material offset:\t%d\n",
1911 cd->hdr.keyblock[i].keyMaterialOffset);
1912 log_std(cd, "\tAF stripes: \t%d\n",
1913 cd->hdr.keyblock[i].stripes);
1916 log_std(cd, "Key Slot %d: DISABLED\n", i);
1921 const char *crypt_get_cipher(struct crypt_device *cd)
1923 if (isPLAIN(cd->type))
1924 return cd->plain_cipher;
1926 if (isLUKS(cd->type))
1927 return cd->hdr.cipherName;
1932 const char *crypt_get_cipher_mode(struct crypt_device *cd)
1934 if (isPLAIN(cd->type))
1935 return cd->plain_cipher_mode;
1937 if (isLUKS(cd->type))
1938 return cd->hdr.cipherMode;
1943 const char *crypt_get_uuid(struct crypt_device *cd)
1945 if (isLUKS(cd->type))
1946 return cd->hdr.uuid;
1951 int crypt_get_volume_key_size(struct crypt_device *cd)
1953 if (isPLAIN(cd->type))
1954 return cd->volume_key->keyLength;
1956 if (isLUKS(cd->type))
1957 return cd->hdr.keyBytes;
1962 uint64_t crypt_get_data_offset(struct crypt_device *cd)
1964 if (isPLAIN(cd->type))
1965 return cd->plain_hdr.offset;
1967 if (isLUKS(cd->type))
1968 return cd->hdr.payloadOffset;
1973 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
1975 if (!isLUKS(cd->type)) {
1976 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1977 return SLOT_INVALID;
1980 return LUKS_keyslot_info(&cd->hdr, keyslot);