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 keyslot_is_valid(struct crypt_device *cd, int keySlotIndex)
187 if(keySlotIndex >= LUKS_NUMKEYS || keySlotIndex < 0) {
188 log_err(cd, _("Key slot %d is invalid, please select between 0 and %d.\n"),
189 keySlotIndex, LUKS_NUMKEYS - 1);
196 /* Select free keyslot or verifies that the one specified is empty */
197 static int keyslot_from_option(struct crypt_device *cd, int keySlotOption, struct luks_phdr *hdr) {
198 if(keySlotOption >= 0) {
199 if(!keyslot_is_valid(cd, keySlotOption))
201 else if(hdr->keyblock[keySlotOption].active != LUKS_KEY_DISABLED) {
202 log_err(cd, _("Key slot %d is full, please select another one.\n"),
206 return keySlotOption;
210 /* Find empty key slot */
211 for(i=0; i<LUKS_NUMKEYS; i++) {
212 if(hdr->keyblock[i].active == LUKS_KEY_DISABLED) break;
214 if(i==LUKS_NUMKEYS) {
215 log_err(cd, _("All key slots full.\n"));
222 static int device_check_and_adjust(struct crypt_device *cd,
224 uint64_t *size, uint64_t *offset,
227 struct device_infos infos;
229 if (get_device_infos(device, &infos, cd) < 0) {
230 log_err(cd, _("Cannot get info about device %s.\n"), device);
237 log_err(cd, _("Device %s has zero size.\n"), device);
240 if (*size <= *offset) {
241 log_err(cd, _("Device %s is too small.\n"), device);
253 static int create_device_helper_old(int reload, struct crypt_options *options)
255 struct crypt_device *cd = NULL;
258 char *processed_key = NULL;
262 r = dm_status_device(options->name);
268 log_err(cd, _("Device %s already exists.\n"), options->name);
275 if (options->key_size < 0 || options->key_size > 1024) {
276 log_err(cd, _("Invalid key size %d.\n"), options->key_size);
280 read_only = (options->flags & CRYPT_FLAG_READONLY);
281 r = device_check_and_adjust(cd, options->device, &options->size, &options->offset, &read_only);
285 get_key("Enter passphrase: ", &key, &keyLen, options->key_size,
286 options->key_file, options->timeout, options->flags, NULL);
288 log_err(cd, "Key reading error");
292 processed_key = process_key(cd, options->hash, options->key_file, options->key_size, key, keyLen);
298 r = dm_create_device(options->name, options->device, options->cipher,
299 NULL, options->size, options->skip, options->offset,
300 options->key_size, processed_key,
303 safe_free(processed_key);
308 static int luks_remove_helper_old(struct crypt_device *cd,
309 struct crypt_options *options, int supply_it)
311 struct luks_masterkey *mk;
312 struct luks_phdr hdr;
314 unsigned int passwordLen;
315 const char *device = options->device;
320 r = LUKS_read_phdr(options->device, &hdr, 1, cd);
325 get_key("Enter LUKS passphrase to be deleted: ",&password,&passwordLen, 0, options->new_key_file,
326 options->timeout, options->flags, cd);
328 r = -EINVAL; goto out;
331 keyIndex = LUKS_open_key_with_hdr(device, CRYPT_ANY_SLOT, password, passwordLen, &hdr, &mk, cd);
333 log_err(cd, "No remaining key available with this passphrase.\n");
334 r = -EPERM; goto out;
336 log_std(cd ,"key slot %d selected for deletion.\n", keyIndex);
341 keyIndex = options->key_slot;
342 if (!keyslot_is_valid(cd, keyIndex)) {
343 r = -EINVAL; goto out;
347 if (LUKS_keyslot_info(&hdr, keyIndex) == SLOT_INACTIVE) {
348 log_err(cd, _("Key %d not active. Can't wipe.\n"), keyIndex);
353 last_slot = (LUKS_keyslot_info(&hdr, keyIndex) == SLOT_ACTIVE_LAST);
354 if(last_slot && !(options->icb->yesDialog(_("This is the last keyslot. Device will become unusable after purging this key.")))) {
355 r = -EINVAL; goto out;
358 if(options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY) {
359 options->flags &= ~CRYPT_FLAG_VERIFY_ON_DELKEY;
360 get_key("Enter any remaining LUKS passphrase: ",&password,&passwordLen, 0, options->key_file,
361 options->timeout, options->flags, cd);
363 r = -EINVAL; goto out;
366 r = LUKS_read_phdr(device, &hdr, 1, cd);
368 options->icb->log(CRYPT_LOG_ERROR,"Failed to access device.\n");
373 hdr.keyblock[keyIndex].active = LUKS_KEY_DISABLED;
375 openedIndex = LUKS_open_key_with_hdr(device, CRYPT_ANY_SLOT, password, passwordLen, &hdr, &mk, cd);
377 if (openedIndex >= 0) {
378 LUKS_dealloc_masterkey(mk);
381 if(openedIndex < 0) {
382 log_err(cd, "No remaining key available with this passphrase.\n");
383 r = -EPERM; goto out;
385 log_std(cd, "key slot %d verified.\n", openedIndex);
387 r = LUKS_del_key(device, keyIndex, cd);
396 static int create_device_helper(struct crypt_device *cd,
400 const char *cipher_mode,
401 const char *key_file,
413 crypt_status_info ci;
414 char *dm_cipher = NULL;
415 char *processed_key = NULL;
418 ci = crypt_status(cd, name);
422 if (reload && ci < ACTIVE)
425 if (!reload && ci >= ACTIVE) {
426 log_err(cd, _("Device %s already exists.\n"), name);
430 if (key_size < 0 || key_size > 1024) {
431 log_err(cd, _("Invalid key size %d.\n"), key_size);
435 r = device_check_and_adjust(cd, cd->device, &size, &offset, &read_only);
439 if (cipher_mode && asprintf(&dm_cipher, "%s-%s", cipher, cipher_mode) < 0)
442 processed_key = process_key(cd, hash, key_file, key_size, key, keyLen);
446 r = dm_create_device(name, cd->device, dm_cipher ?: cipher, uuid, size, skip, offset,
447 key_size, processed_key, read_only, reload);
450 safe_free(processed_key);
454 static int open_from_hdr_and_mk(struct crypt_device *cd,
455 struct luks_masterkey *mk,
459 uint64_t size, offset;
461 int read_only, no_uuid, r;
464 offset = crypt_get_data_offset(cd);
465 read_only = flags & CRYPT_ACTIVATE_READONLY;
466 no_uuid = flags & CRYPT_ACTIVATE_NO_UUID;
468 r = device_check_and_adjust(cd, cd->device, &size, &offset, &read_only);
472 if (asprintf(&cipher, "%s-%s", crypt_get_cipher(cd),
473 crypt_get_cipher_mode(cd)) < 0)
476 r = dm_create_device(name, cd->device, cipher, no_uuid ? NULL : crypt_get_uuid(cd),
477 size, 0, offset, mk->keyLength, mk->key,
483 static void key_from_terminal(struct crypt_device *cd, char *msg, char **key,
484 unsigned int *key_len, int force_verify)
489 *key = safe_alloc(MAX_TTY_PASSWORD_LEN);
492 r = cd->password(msg, *key, (size_t)key_len, cd->password_usrptr);
499 if (force_verify || cd->password_verify)
500 flags |= CRYPT_FLAG_VERIFY_IF_POSSIBLE;
501 get_key(msg, key, key_len, 0, NULL, cd->timeout, flags, cd);
505 static void key_from_file(struct crypt_device *cd, char *msg,
506 char **key, unsigned int *key_len,
507 const char *key_file, size_t key_size)
509 get_key(msg, key, key_len, key_size, key_file, cd->timeout, 0, cd);
512 void crypt_set_log_callback(struct crypt_device *cd,
513 void (*log)(int class, const char *msg, void *usrptr),
517 cd->log_usrptr = usrptr;
520 void crypt_set_confirm_callback(struct crypt_device *cd,
521 int (*confirm)(const char *msg, void *usrptr),
524 cd->confirm = confirm;
525 cd->confirm_usrptr = usrptr;
528 void crypt_set_password_callback(struct crypt_device *cd,
529 int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
532 cd->password = password;
533 cd->password_usrptr = usrptr;
536 /* OPTIONS: name, cipher, device, hash, key_file, key_size, key_slot,
537 * offset, size, skip, timeout, tries, passphrase_fd (ignored),
539 int crypt_create_device(struct crypt_options *options)
541 return create_device_helper_old(0, options);
544 /* OPTIONS: same as create above */
545 int crypt_update_device(struct crypt_options *options)
547 return create_device_helper_old(1, options);
550 /* OPTIONS: name, size, icb */
551 int crypt_resize_device(struct crypt_options *options)
553 struct crypt_device *cd = NULL;
554 char *device, *cipher, *key = NULL;
555 uint64_t size, skip, offset;
556 int key_size, read_only, r;
558 r = dm_query_device(options->name, &device, &size, &skip, &offset,
559 &cipher, &key_size, &key, &read_only);
563 size = options->size;
564 r = device_check_and_adjust(cd, device, &size, &offset, &read_only);
568 r = dm_create_device(options->name, device, cipher, NULL, size, skip, offset,
569 key_size, key, read_only, 1);
578 /* OPTIONS: name, icb */
579 int crypt_query_device(struct crypt_options *options)
583 r = dm_status_device(options->name);
587 r = dm_query_device(options->name, (char **)&options->device, &options->size,
588 &options->skip, &options->offset, (char **)&options->cipher,
589 &options->key_size, NULL, &read_only);
595 options->flags |= CRYPT_FLAG_READONLY;
597 options->flags |= CRYPT_FLAG_FREE_DEVICE;
598 options->flags |= CRYPT_FLAG_FREE_CIPHER;
603 /* OPTIONS: name, icb */
604 int crypt_remove_device(struct crypt_options *options)
608 r = dm_status_device(options->name);
612 log_err(NULL, "Device busy");
616 return dm_remove_device(options->name, 0, 0);
619 /* OPTIONS: device, cipher, hash, align_payload, key_size (master key), key_slot
620 * new_key_file, iteration_time, timeout, flags, icb */
621 int crypt_luksFormat(struct crypt_options *options)
623 struct crypt_device *cd = NULL;
624 struct luks_phdr header;
625 struct luks_masterkey *mk=NULL;
627 char cipherName[LUKS_CIPHERNAME_L];
628 char cipherMode[LUKS_CIPHERMODE_L];
629 unsigned int passwordLen;
630 uint64_t PBKDF2perSecond = 0;
633 if (!device_ready(cd, options->device, O_RDWR | O_EXCL))
636 mk = LUKS_generate_masterkey(options->key_size);
637 if(NULL == mk) return -ENOMEM; // FIXME This may be misleading, since we don't know what went wrong
640 #define printoffset(entry) \
641 logger(options, CRYPT_LOG_ERROR, \
642 "offset of " #entry " = %d\n", (char *)(&header.entry)-(char *)(&header))
644 log_err("sizeof phdr %d, sizeof key slot %d\n",
645 sizeof(struct luks_phdr),
646 sizeof(header.keyblock[0]));
649 printoffset(version);
650 printoffset(cipherName);
651 printoffset(cipherMode);
652 printoffset(hashSpec);
653 printoffset(payloadOffset);
654 printoffset(keyBytes);
655 printoffset(mkDigest);
656 printoffset(mkDigestSalt);
657 printoffset(mkDigestIterations);
661 r = parse_into_name_and_mode(options->cipher, cipherName, cipherMode);
663 log_err(cd, _("No known cipher specification pattern detected.\n"));
667 r = LUKS_generate_phdr(&header, mk, cipherName, cipherMode, options->hash, NULL, LUKS_STRIPES, options->align_payload, NULL);
670 keyIndex = keyslot_from_option(NULL, options->key_slot, &header);
671 if(keyIndex == -EINVAL) {
672 r = -EINVAL; goto out;
675 get_key("Enter LUKS passphrase: ",&password,&passwordLen, 0, options->new_key_file,
676 options->timeout, options->flags, NULL);
678 r = -EINVAL; goto out;
681 /* Wipe first 8 sectors - fs magic numbers etc. */
682 r = wipe_device_header(options->device, 8);
685 /* Set key, also writes phdr */
686 r = LUKS_set_key(options->device, keyIndex, password, passwordLen, &header, mk,
687 options->iteration_time, &PBKDF2perSecond, NULL);
692 LUKS_dealloc_masterkey(mk);
697 /* OPTIONS: name, device, key_size, key_file, timeout, tries, flags, icb */
698 int crypt_luksOpen(struct crypt_options *options)
700 struct crypt_device *cd = NULL;
701 struct luks_masterkey *mk=NULL;
702 struct luks_phdr hdr;
705 unsigned int passwordLen;
706 char *dmCipherSpec = NULL;
708 int r, tries = options->tries;
709 int excl = (options->flags & CRYPT_FLAG_NON_EXCLUSIVE_ACCESS) ? 0 : O_EXCL ;
711 r = dm_status_device(options->name);
713 log_err(cd, "Device %s already exists.", options->name);
717 if (!device_ready(cd, options->device, O_RDONLY | excl))
720 if(asprintf(&prompt, "Enter LUKS passphrase for %s: ", options->device) < 0)
726 if(options->passphrase) {
727 passwordLen = strlen(options->passphrase);
728 password = safe_alloc(passwordLen + 1);
729 strncpy(password, options->passphrase, passwordLen + 1);
732 get_key(prompt, &password, &passwordLen, options->key_size, options->key_file,
733 options->timeout, options->flags, cd);
741 r = -EINVAL; goto out;
744 r = LUKS_read_phdr(options->device, &hdr, 1, cd);
748 r = LUKS_open_key_with_hdr(options->device, CRYPT_ANY_SLOT, password, passwordLen, &hdr, &mk, cd);
750 log_err(cd, "No key available with this passphrase.\n");
754 log_err(NULL, "key slot %d unlocked.\n", r);
757 options->offset = hdr.payloadOffset;
758 if (asprintf(&dmCipherSpec, "%s-%s", hdr.cipherName, hdr.cipherMode) < 0) {
762 options->cipher = dmCipherSpec;
763 options->key_size = mk->keyLength;
767 read_only = (options->flags & CRYPT_FLAG_READONLY);
768 r = device_check_and_adjust(cd, options->device, &options->size, &options->offset, &read_only);
772 /* FIXME: code allows multiple crypt mapping, cannot use uuid then.
773 * anyway, it is dangerous and can corrupt data. Remove it in next version! */
774 r = dm_create_device(options->name, options->device, options->cipher,
775 excl ? hdr.uuid : NULL, options->size,
776 0, options->offset, mk->keyLength, mk->key,
777 options->flags & CRYPT_FLAG_READONLY, 0);
785 LUKS_dealloc_masterkey(mk);
786 if (r == -EPERM && tries > 0)
794 /* OPTIONS: device, keys_slot, key_file, timeout, flags, icb */
795 int crypt_luksKillSlot(struct crypt_options *options)
797 return luks_remove_helper_old(NULL, options, 0);
800 /* OPTIONS: device, new_key_file, key_file, timeout, flags, icb */
801 int crypt_luksRemoveKey(struct crypt_options *options)
803 return luks_remove_helper_old(NULL, options, 1);
806 /* OPTIONS: device, new_key_file, key_file, key_slot, flags,
807 iteration_time, timeout, icb */
808 int crypt_luksAddKey(struct crypt_options *options)
810 struct crypt_device *cd = NULL;
811 struct luks_masterkey *mk=NULL;
812 struct luks_phdr hdr;
813 char *password=NULL; unsigned int passwordLen;
814 unsigned int keyIndex;
815 uint64_t PBKDF2perSecond = 0;
816 const char *device = options->device;
819 if (!device_ready(cd, options->device, O_RDWR))
822 r = LUKS_read_phdr(device, &hdr, 1, cd);
826 keyIndex = keyslot_from_option(cd, options->key_slot, &hdr);
827 if(keyIndex == -EINVAL) {
828 r = -EINVAL; goto out;
831 get_key("Enter any LUKS passphrase: ",
837 options->flags & ~(CRYPT_FLAG_VERIFY | CRYPT_FLAG_VERIFY_IF_POSSIBLE), cd);
840 r = -EINVAL; goto out;
842 r = LUKS_open_key_with_hdr(device, CRYPT_ANY_SLOT, password, passwordLen, &hdr, &mk, cd);
844 options->icb->log(CRYPT_LOG_ERROR,"No key available with this passphrase.\n");
845 r = -EPERM; goto out;
849 get_key("Enter new passphrase for key slot: ",
853 options->new_key_file,
857 r = -EINVAL; goto out;
860 r = LUKS_set_key(device, keyIndex, password, passwordLen, &hdr, mk, options->iteration_time, &PBKDF2perSecond, cd);
866 LUKS_dealloc_masterkey(mk);
870 /* OPTIONS: device, icb */
871 int crypt_luksUUID(struct crypt_options *options)
873 struct crypt_device *cd = NULL;
874 struct luks_phdr hdr;
877 r = LUKS_read_phdr(options->device, &hdr, 1, cd);
880 options->icb->log(CRYPT_LOG_NORMAL,hdr.uuid);
881 options->icb->log(CRYPT_LOG_NORMAL,"\n");
885 /* OPTIONS: device, icb */
886 int crypt_isLuks(struct crypt_options *options)
888 struct crypt_device *cd = NULL;
889 struct luks_phdr hdr;
890 return LUKS_read_phdr(options->device, &hdr, 0, cd);
893 /* OPTIONS: device, icb */
894 static void hexprintICB(struct crypt_device *cd, char *d, int n);
895 int crypt_luksDump(struct crypt_options *options)
897 struct crypt_device *cd = NULL;
898 struct luks_phdr hdr;
901 r = LUKS_read_phdr(options->device, &hdr, 1, cd);
904 log_std(cd, "LUKS header information for %s\n\n", options->device);
905 log_std(cd, "Version: \t%d\n", hdr.version);
906 log_std(cd, "Cipher name: \t%s\n", hdr.cipherName);
907 log_std(cd, "Cipher mode: \t%s\n", hdr.cipherMode);
908 log_std(cd, "Hash spec: \t%s\n", hdr.hashSpec);
909 log_std(cd, "Payload offset:\t%d\n", hdr.payloadOffset);
910 log_std(cd, "MK bits: \t%d\n", hdr.keyBytes * 8);
911 log_std(cd, "MK digest: \t");
912 hexprintICB(cd, hdr.mkDigest, LUKS_DIGESTSIZE);
914 log_std(cd, "MK salt: \t");
915 hexprintICB(cd, hdr.mkDigestSalt, LUKS_SALTSIZE/2);
916 log_std(cd, "\n \t");
917 hexprintICB(cd, hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
919 log_std(cd, "MK iterations: \t%d\n", hdr.mkDigestIterations);
920 log_std(cd, "UUID: \t%s\n\n", hdr.uuid);
921 for(i = 0; i < LUKS_NUMKEYS; i++) {
922 if(hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
923 log_std(cd, "Key Slot %d: ENABLED\n",i);
924 log_std(cd, "\tIterations: \t%d\n",
925 hdr.keyblock[i].passwordIterations);
926 log_std(cd, "\tSalt: \t");
927 hexprintICB(cd, hdr.keyblock[i].passwordSalt,
929 log_std(cd, "\n\t \t");
930 hexprintICB(cd, hdr.keyblock[i].passwordSalt +
931 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
934 log_std(cd, "\tKey material offset:\t%d\n",
935 hdr.keyblock[i].keyMaterialOffset);
936 log_std(cd, "\tAF stripes: \t%d\n",
937 hdr.keyblock[i].stripes);
940 log_std(cd, "Key Slot %d: DISABLED\n", i);
945 void crypt_get_error(char *buf, size_t size)
947 const char *error = get_error();
949 if (!buf || size < 1)
952 strncpy(buf, error, size - 1);
953 buf[size - 1] = '\0';
959 void crypt_put_options(struct crypt_options *options)
961 if (options->flags & CRYPT_FLAG_FREE_DEVICE) {
962 free((char *)options->device);
963 options->device = NULL;
964 options->flags &= ~CRYPT_FLAG_FREE_DEVICE;
966 if (options->flags & CRYPT_FLAG_FREE_CIPHER) {
967 free((char *)options->cipher);
968 options->cipher = NULL;
969 options->flags &= ~CRYPT_FLAG_FREE_CIPHER;
973 const char *crypt_get_dir(void)
978 /////////////////////////////////
983 int crypt_init(struct crypt_device **cd, const char *device)
985 struct crypt_device *h = NULL;
990 log_dbg("Allocating crypt device %s context.", device);
992 if (!device_ready(NULL, device, 0))
995 if (!(h = malloc(sizeof(struct crypt_device))))
998 memset(h, 0, sizeof(*h));
1000 h->device = strdup(device);
1006 if (!dm_init(h, 1) < 0) {
1011 h->iteration_time = 1000;
1012 h->password_verify = 0;
1018 static int _crypt_format_plain(struct crypt_device *cd,
1020 const char *cipher_mode,
1022 struct crypt_params_plain *params)
1024 if (!cipher || !cipher_mode || !params || !params->hash) {
1025 log_err(cd, _("Invalid plain crypt parameters.\n"));
1029 if (cd->volume_key->keyLength > 1024) {
1030 log_err(cd, _("Invalid key size.\n"));
1034 cd->plain_cipher = strdup(cipher);
1035 cd->plain_cipher_mode = strdup(cipher_mode);
1038 cd->plain_uuid = strdup(uuid);
1041 cd->plain_hdr.hash = strdup(params->hash);
1043 cd->plain_hdr.offset = params->offset;
1044 cd->plain_hdr.skip = params->skip;
1046 if ((params->hash && !cd->plain_hdr.hash) ||
1047 !cd->plain_cipher || !cd->plain_cipher_mode)
1053 static int _crypt_format_luks1(struct crypt_device *cd,
1055 const char *cipher_mode,
1057 struct crypt_params_luks1 *params)
1061 r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
1062 (params && params->hash) ? params->hash : "sha1",
1064 params ? params->data_alignment: DEFAULT_ALIGNMENT, cd);
1068 /* Wipe first 8 sectors - fs magic numbers etc. */
1069 r = wipe_device_header(cd->device, 8);
1071 log_err(cd, _("Can't wipe header on device %s.\n"), cd->device);
1075 r = LUKS_write_phdr(cd->device, &cd->hdr, cd);
1080 int crypt_format(struct crypt_device *cd,
1083 const char *cipher_mode,
1085 const char *volume_key,
1086 size_t volume_key_size,
1091 log_dbg("Formatting device %s as type %s.", cd->device, cd->type ?: "(none)");
1097 cd->volume_key = LUKS_alloc_masterkey(volume_key_size,
1100 cd->volume_key = LUKS_generate_masterkey(volume_key_size);
1106 r = _crypt_format_plain(cd, cipher, cipher_mode,
1108 else if (isLUKS(type))
1109 r = _crypt_format_luks1(cd, cipher, cipher_mode,
1112 /* FIXME: allow plugins here? */
1113 log_err(cd, _("Unkown crypt device type %s requesed.\n"), type);
1117 if (!r && !(cd->type = strdup(type)))
1121 LUKS_dealloc_masterkey(cd->volume_key);
1122 cd->volume_key = NULL;
1128 int crypt_load(struct crypt_device *cd,
1129 const char *requested_type,
1132 struct luks_phdr hdr;
1135 log_dbg("Trying to load %s crypt type from device %s.",
1136 requested_type ?: "any", cd->device);
1138 if (requested_type && !isPLAIN(requested_type) && !isLUKS(requested_type))
1141 r = LUKS_read_phdr(cd->device, &hdr, 0, cd);
1144 memcpy(&cd->hdr, &hdr, sizeof(hdr));
1145 cd->type = strdup(requested_type);
1153 void crypt_free(struct crypt_device *cd)
1156 log_dbg("Releasing crypt device %s context.", cd->device);
1160 LUKS_dealloc_masterkey(cd->volume_key);
1165 /* used in plain device only */
1166 free((char*)cd->plain_hdr.hash);
1167 free(cd->plain_cipher);
1168 free(cd->plain_cipher_mode);
1169 free(cd->plain_uuid);
1175 // slot manipulation
1176 int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
1177 int keyslot, // -1 any
1178 const char *passphrase, // NULL -> terminal
1179 size_t passphrase_size,
1180 const char *new_passphrase, // NULL -> terminal
1181 size_t new_passphrase_size)
1183 struct luks_masterkey *mk = NULL;
1184 char *password = NULL, *new_password = NULL;
1185 unsigned int passwordLen, new_passwordLen;
1188 log_dbg("Adding new keyslot, existing passphrase %sprovided,"
1189 "new passphrase %sprovided.",
1190 passphrase ? "" : "not ", new_passphrase ? "" : "not ");
1192 if (!isLUKS(cd->type)) {
1193 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1197 r = keyslot_verify_or_find_empty(cd, &keyslot);
1201 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1202 /* No slots used, try to use pre-generated key in header */
1203 if (cd->volume_key) {
1204 mk = LUKS_alloc_masterkey(cd->volume_key->keyLength, cd->volume_key->key);
1205 r = mk ? 0 : -ENOMEM;
1207 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1210 } else if (passphrase) {
1211 /* Passphrase provided, use it to unlock existing keyslot */
1212 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, passphrase,
1213 passphrase_size, &cd->hdr, &mk, cd);
1215 /* Passphrase not provided, ask first and use it to unlock existing keyslot */
1216 key_from_terminal(cd, _("Enter any passphrase: "),
1217 &password, &passwordLen, 0);
1223 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password,
1224 passwordLen, &cd->hdr, &mk, cd);
1225 safe_free(password);
1231 if (new_passphrase) {
1232 new_password = (char *)new_passphrase;
1233 new_passwordLen = new_passphrase_size;
1235 key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1236 &new_password, &new_passwordLen, 1);
1243 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1244 &cd->hdr, mk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1249 if (!new_passphrase)
1250 safe_free(new_password);
1251 LUKS_dealloc_masterkey(mk);
1252 return r ?: keyslot;
1255 int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
1257 const char *keyfile,
1258 size_t keyfile_size,
1259 const char *new_keyfile,
1260 size_t new_keyfile_size)
1262 struct luks_masterkey *mk=NULL;
1263 char *password=NULL; unsigned int passwordLen;
1264 char *new_password = NULL; unsigned int new_passwordLen;
1267 log_dbg("Adding new keyslot, existing keyfile %s, new keyfile %s.",
1268 keyfile ?: "[none]", new_keyfile ?: "[none]");
1270 if (!isLUKS(cd->type)) {
1271 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1275 r = keyslot_verify_or_find_empty(cd, &keyslot);
1279 if (!LUKS_keyslot_active_count(&cd->hdr)) {
1280 /* No slots used, try to use pre-generated key in header */
1281 if (cd->volume_key) {
1282 mk = LUKS_alloc_masterkey(cd->volume_key->keyLength, cd->volume_key->key);
1283 r = mk ? 0 : -ENOMEM;
1285 log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.\n"));
1289 /* Read password from file of (if NULL) from terminal */
1291 key_from_file(cd, _("Enter any passphrase: "), &password, &passwordLen,
1292 keyfile, keyfile_size);
1294 key_from_terminal(cd, _("Enter any passphrase: "),
1295 &password, &passwordLen, 1);
1300 r = LUKS_open_key_with_hdr(cd->device, CRYPT_ANY_SLOT, password, passwordLen,
1302 safe_free(password);
1309 key_from_file(cd, _("Enter new passphrase for key slot: "),
1310 &new_password, &new_passwordLen, new_keyfile,
1313 key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1314 &new_password, &new_passwordLen, 1);
1321 r = LUKS_set_key(cd->device, keyslot, new_password, new_passwordLen,
1322 &cd->hdr, mk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1324 safe_free(new_password);
1325 LUKS_dealloc_masterkey(mk);
1326 return r < 0 ? r : keyslot;
1329 int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
1331 const char *volume_key,
1332 size_t volume_key_size,
1333 const char *passphrase,
1334 size_t passphrase_size
1337 struct luks_masterkey *mk;
1339 char *new_password = NULL; unsigned int new_passwordLen;
1341 log_dbg("Adding new keyslot %d using volume key.", keyslot);
1343 if (!isLUKS(cd->type)) {
1344 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1349 mk = LUKS_alloc_masterkey(volume_key_size, volume_key);
1350 else if (cd->volume_key)
1351 mk = LUKS_alloc_masterkey(cd->volume_key->keyLength, cd->volume_key->key);
1356 r = LUKS_verify_master_key(&cd->hdr, mk);
1358 log_err(cd, _("Volume key does not match the volume.\n"));
1362 r = keyslot_verify_or_find_empty(cd, &keyslot);
1367 key_from_terminal(cd, _("Enter new passphrase for key slot: "),
1368 &new_password, &new_passwordLen, 1);
1369 passphrase = new_password;
1370 passphrase_size = new_passwordLen;
1373 r = LUKS_set_key(cd->device, keyslot, passphrase, passphrase_size,
1374 &cd->hdr, mk, cd->iteration_time, &cd->PBKDF2_per_sec, cd);
1377 safe_free(new_password);
1378 LUKS_dealloc_masterkey(mk);
1379 return r ?: keyslot;
1382 int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
1384 crypt_keyslot_info ki;
1386 log_dbg("Destroying keyslot %d.", keyslot);
1388 if (!isLUKS(cd->type)) {
1389 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1393 ki = crypt_keyslot_status(cd, keyslot);
1394 if (ki == SLOT_INVALID)
1397 if (ki == SLOT_INACTIVE)
1400 return LUKS_del_key(cd->device, keyslot, cd);
1403 // activation/deactivation of device mapping
1404 int crypt_activate_by_passphrase(struct crypt_device *cd,
1407 const char *passphrase,
1408 size_t passphrase_size,
1411 crypt_status_info ci;
1412 struct luks_masterkey *mk = NULL;
1413 char *prompt = NULL, *passphrase_read = NULL;
1414 unsigned int passphrase_size_read;
1415 int r, tries = cd->tries;
1417 log_dbg("%s volume %s [keyslot %d] using %spassphrase.",
1418 name ? "Activating" : "Checking", name ?: "",
1419 keyslot, passphrase ? "" : "[none] ");
1424 /* plain, use hashed passphrase */
1425 if (isPLAIN(cd->type))
1426 return create_device_helper(cd, name, cd->plain_hdr.hash,
1427 cd->plain_cipher, cd->plain_cipher_mode, NULL, passphrase, passphrase_size,
1428 cd->volume_key->keyLength, 0, cd->plain_hdr.skip,
1429 cd->plain_hdr.offset, cd->plain_uuid, flags & CRYPT_ACTIVATE_READONLY, 0, 0);
1432 ci = crypt_status(NULL, name);
1435 else if (ci >= ACTIVE) {
1436 log_err(cd, _("Device %s already exists.\n"), name);
1441 if(asprintf(&prompt, _("Enter passphrase for %s: "), cd->device) < 0)
1444 /* provided passphrase, do not retry */
1446 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1447 passphrase_size, &cd->hdr, &mk, cd);
1450 LUKS_dealloc_masterkey(mk);
1453 key_from_terminal(cd, prompt, &passphrase_read,
1454 &passphrase_size_read, 0);
1455 if(!passphrase_read) {
1460 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1461 passphrase_size_read, &cd->hdr, &mk, cd);
1462 safe_free(passphrase_read);
1463 passphrase_read = NULL;
1464 } while (r == -EPERM && (--tries > 0));
1469 r = open_from_hdr_and_mk(cd, mk, name, flags);
1472 LUKS_dealloc_masterkey(mk);
1475 return r < 0 ? r : keyslot;
1478 int crypt_activate_by_keyfile(struct crypt_device *cd,
1481 const char *keyfile,
1482 size_t keyfile_size,
1485 crypt_status_info ci;
1486 struct luks_masterkey *mk = NULL;
1487 char *passphrase_read = NULL;
1488 unsigned int passphrase_size_read;
1491 log_dbg("Activating volume %s [keyslot %d] using keyfile %s.",
1492 name, keyslot, keyfile ?: "[none]");
1494 if (!isLUKS(cd->type)) {
1495 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1500 ci = crypt_status(NULL, name);
1503 else if (ci >= ACTIVE) {
1504 log_err(cd, _("Device %s already exists.\n"), name);
1512 key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
1513 &passphrase_size_read, keyfile, keyfile_size);
1514 if(!passphrase_read)
1517 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read,
1518 passphrase_size_read, &cd->hdr, &mk, cd);
1519 safe_free(passphrase_read);
1524 r = open_from_hdr_and_mk(cd, mk, name, flags);
1527 LUKS_dealloc_masterkey(mk);
1529 return r < 0 ? r : keyslot;
1532 int crypt_activate_by_volume_key(struct crypt_device *cd,
1534 const char *volume_key,
1535 size_t volume_key_size,
1538 crypt_status_info ci;
1539 struct luks_masterkey *mk;
1542 log_dbg("Activating volume %s by volume key.", name);
1544 /* use key directly, no hash */
1545 if (isPLAIN(cd->type))
1546 return create_device_helper(cd, name, NULL,
1547 cd->plain_cipher, cd->plain_cipher_mode, NULL, volume_key, volume_key_size,
1548 cd->volume_key->keyLength, 0, cd->plain_hdr.skip,
1549 cd->plain_hdr.offset, cd->plain_uuid, flags & CRYPT_ACTIVATE_READONLY, 0, 0);
1551 if (!isLUKS(cd->type)) {
1552 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1557 ci = crypt_status(NULL, name);
1560 else if (ci >= ACTIVE) {
1561 log_err(cd, _("Device %s already exists.\n"), name);
1566 mk = LUKS_alloc_masterkey(volume_key_size, volume_key);
1569 r = LUKS_verify_master_key(&cd->hdr, mk);
1572 log_err(cd, _("Volume key does not match the volume.\n"));
1575 r = open_from_hdr_and_mk(cd, mk, name, flags);
1577 LUKS_dealloc_masterkey(mk);
1582 int crypt_deactivate(struct crypt_device *cd, const char *name)
1587 log_dbg("Deactivating volume %s.", name);
1589 switch (crypt_status(cd, name)) {
1590 case ACTIVE: return dm_remove_device(name, 0, 0);
1591 case BUSY: log_err(cd, _("Device %s is busy."), name);
1593 case INACTIVE: return -ENODEV;
1594 default: log_err(cd, _("Invalid device %s."), name);
1599 // misc helper functions
1600 int crypt_volume_key_get(struct crypt_device *cd,
1603 size_t *volume_key_size,
1604 const char *passphrase,
1605 size_t passphrase_size)
1607 struct luks_masterkey *mk;
1608 char *processed_key = NULL;
1611 key_len = crypt_get_volume_key_size(cd);
1612 if (key_len > *volume_key_size) {
1613 log_err(cd, _("Volume key buffer too small.\n"));
1617 if (isPLAIN(cd->type)) {
1618 processed_key = process_key(cd, cd->plain_hdr.hash, NULL, key_len,
1619 passphrase, passphrase_size);
1620 if (!processed_key) {
1621 log_err(cd, _("Cannot retrieve volume key for plain device.\n"));
1624 memcpy(volume_key, processed_key, key_len);
1625 *volume_key_size = key_len;
1626 safe_free(processed_key);
1630 if (isLUKS(cd->type)) {
1631 r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase,
1632 passphrase_size, &cd->hdr, &mk, cd);
1635 memcpy(volume_key, mk->key, mk->keyLength);
1636 *volume_key_size = mk->keyLength;
1639 LUKS_dealloc_masterkey(mk);
1643 log_err(cd, _("This operation is not supported for %s crypt device.\n"), cd->type ?: "(none)");
1647 int crypt_volume_key_verify(struct crypt_device *cd,
1648 const char *volume_key,
1649 size_t volume_key_size)
1651 struct luks_masterkey *mk;
1654 if (!isLUKS(cd->type)) {
1655 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1659 mk = LUKS_alloc_masterkey(volume_key_size, volume_key);
1663 r = LUKS_verify_master_key(&cd->hdr, mk);
1666 log_err(cd, _("Volume key does not match the volume.\n"));
1668 LUKS_dealloc_masterkey(mk);
1673 void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec)
1675 log_dbg("Timeout set to %" PRIu64 " miliseconds.", timeout_sec);
1676 cd->timeout = timeout_sec;
1679 void crypt_set_password_retry(struct crypt_device *cd, int tries)
1681 log_dbg("Password retry count set to %d.", tries);
1685 void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms)
1687 log_dbg("Iteration time set to %" PRIu64 " miliseconds.", iteration_time_ms);
1688 cd->iteration_time = iteration_time_ms;
1691 void crypt_set_password_verify(struct crypt_device *cd, int password_verify)
1693 log_dbg("Password verification %s.", password_verify ? "enabled" : "disabled");
1694 cd->password_verify = password_verify ? 1 : 0;
1697 int crypt_memory_lock(struct crypt_device *cd, int lock)
1699 return lock ? memlock_inc(cd) : memlock_dec(cd);
1703 crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
1707 r = dm_status_device(name);
1709 if (r < 0 && r != -ENODEV)
1721 static void hexprintICB(struct crypt_device *cd, char *d, int n)
1724 for(i = 0; i < n; i++)
1725 log_std(cd, "%02hhx ", (char)d[i]);
1728 int crypt_dump(struct crypt_device *cd)
1731 if (!isLUKS(cd->type)) { //FIXME
1732 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1736 log_std(cd, "LUKS header information for %s\n\n", cd->device);
1737 log_std(cd, "Version: \t%d\n", cd->hdr.version);
1738 log_std(cd, "Cipher name: \t%s\n", cd->hdr.cipherName);
1739 log_std(cd, "Cipher mode: \t%s\n", cd->hdr.cipherMode);
1740 log_std(cd, "Hash spec: \t%s\n", cd->hdr.hashSpec);
1741 log_std(cd, "Payload offset:\t%d\n", cd->hdr.payloadOffset);
1742 log_std(cd, "MK bits: \t%d\n", cd->hdr.keyBytes * 8);
1743 log_std(cd, "MK digest: \t");
1744 hexprintICB(cd, cd->hdr.mkDigest, LUKS_DIGESTSIZE);
1746 log_std(cd, "MK salt: \t");
1747 hexprintICB(cd, cd->hdr.mkDigestSalt, LUKS_SALTSIZE/2);
1748 log_std(cd, "\n \t");
1749 hexprintICB(cd, cd->hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
1751 log_std(cd, "MK iterations: \t%d\n", cd->hdr.mkDigestIterations);
1752 log_std(cd, "UUID: \t%s\n\n", cd->hdr.uuid);
1753 for(i = 0; i < LUKS_NUMKEYS; i++) {
1754 if(cd->hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
1755 log_std(cd, "Key Slot %d: ENABLED\n",i);
1756 log_std(cd, "\tIterations: \t%d\n",
1757 cd->hdr.keyblock[i].passwordIterations);
1758 log_std(cd, "\tSalt: \t");
1759 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt,
1761 log_std(cd, "\n\t \t");
1762 hexprintICB(cd, cd->hdr.keyblock[i].passwordSalt +
1763 LUKS_SALTSIZE/2, LUKS_SALTSIZE/2);
1766 log_std(cd, "\tKey material offset:\t%d\n",
1767 cd->hdr.keyblock[i].keyMaterialOffset);
1768 log_std(cd, "\tAF stripes: \t%d\n",
1769 cd->hdr.keyblock[i].stripes);
1772 log_std(cd, "Key Slot %d: DISABLED\n", i);
1777 const char *crypt_get_cipher(struct crypt_device *cd)
1779 if (isPLAIN(cd->type))
1780 return cd->plain_cipher;
1782 if (isLUKS(cd->type))
1783 return cd->hdr.cipherName;
1788 const char *crypt_get_cipher_mode(struct crypt_device *cd)
1790 if (isPLAIN(cd->type))
1791 return cd->plain_cipher_mode;
1793 if (isLUKS(cd->type))
1794 return cd->hdr.cipherMode;
1799 const char *crypt_get_uuid(struct crypt_device *cd)
1801 if (isLUKS(cd->type))
1802 return cd->hdr.uuid;
1807 int crypt_get_volume_key_size(struct crypt_device *cd)
1809 if (isPLAIN(cd->type))
1810 return cd->volume_key->keyLength;
1812 if (isLUKS(cd->type))
1813 return cd->hdr.keyBytes;
1818 uint64_t crypt_get_data_offset(struct crypt_device *cd)
1820 if (isPLAIN(cd->type))
1821 return cd->plain_hdr.offset;
1823 if (isLUKS(cd->type))
1824 return cd->hdr.payloadOffset;
1829 crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
1831 if (!isLUKS(cd->type)) {
1832 log_err(cd, _("This operation is supported only for LUKS device.\n"));
1833 return SLOT_INVALID;
1836 return LUKS_keyslot_info(&cd->hdr, keyslot);