14 #include "libcryptsetup.h"
24 static int memory_unsafe = 0;
25 static char *default_backend = NULL;
27 #define at_least_one(a) ({ __typeof__(a) __at_least_one=(a); (__at_least_one)?__at_least_one:1; })
29 static void logger(struct crypt_options *options, int class, char *format, ...) {
33 va_start(argp, format);
34 vasprintf(&target, format, argp);
35 options->icb->log(class, target);
41 static void hexprintICB(struct crypt_options *options, int class, char *d, int n)
44 for(i = 0; i < n; i++)
45 logger(options, class, "%02hhx ", (char)d[i]);
48 static int setup_enter(struct setup_backend *backend, void (*log)(int, char *))
53 * from here we could have sensible data in memory
54 * so protect it from being swapped out
56 r = mlockall(MCL_CURRENT | MCL_FUTURE);
58 perror("mlockall failed");
59 log(CRYPT_LOG_ERROR, "WARNING!!! Possibly insecure memory. Are you root?\n");
76 static int setup_leave(struct setup_backend *backend)
81 /* dangerous, we can't wipe all the memory */
89 * Password processing behaviour matrix of process_key
91 * from binary file: check if there is sufficently large key material
92 * interactive & from fd: hash if requested, otherwise crop or pad with '0'
95 static char *process_key(struct crypt_options *options, char *pass, int passLen) {
96 char *key = safe_alloc(options->key_size);
97 memset(key, 0, options->key_size);
99 /* key is coming from binary file */
100 if (options->key_file && strcmp(options->key_file, "-")) {
101 if(passLen < options->key_size) {
102 set_error("Could not read %d bytes from key file",
107 memcpy(key,pass,options->key_size);
111 /* key is coming from tty, fd or binary stdin */
113 if (hash(NULL, options->hash,
114 key, options->key_size,
120 } else if (passLen > options->key_size) {
121 memcpy(key, pass, options->key_size);
123 memcpy(key, pass, passLen);
129 static int get_device_infos(const char *device, struct device_infos *infos)
133 unsigned long size_small;
138 /* Try to open read-write to check whether it is a read-only device */
139 fd = open(device, O_RDWR);
141 if (errno == EROFS) {
143 fd = open(device, O_RDONLY);
147 fd = open(device, O_RDONLY);
150 set_error("Error opening device: %s",
151 strerror_r(errno, buf, 128));
156 /* If the device can be opened read-write, i.e. readonly is still 0, then
157 * check whether BKROGET says that it is read-only. E.g. read-only loop
158 * devices may be openend read-write but are read-only according to BLKROGET
161 if (ioctl(fd, BLKROGET, &readonly) < 0) {
162 set_error("BLKROGET failed on device: %s",
163 strerror_r(errno, buf, 128));
168 #error BLKROGET not available
172 if (ioctl(fd, BLKGETSIZE64, &size) >= 0) {
173 size >>= SECTOR_SHIFT;
180 if (ioctl(fd, BLKGETSIZE, &size_small) >= 0) {
181 size = (uint64_t)size_small;
186 # error Need at least the BLKGETSIZE ioctl!
189 set_error("BLKGETSIZE ioctl failed on device: %s",
190 strerror_r(errno, buf, 128));
195 infos->readonly = readonly;
201 static int parse_into_name_and_mode(const char *nameAndMode, char *name,
204 /* Token content stringification, see info cpp/stringification */
206 #define xstr(s) str(s)
207 #define scanpattern1 "%" xstr(LUKS_CIPHERNAME_L) "[^-]-%" xstr(LUKS_CIPHERMODE_L) "s"
208 #define scanpattern2 "%" xstr(LUKS_CIPHERNAME_L) "[^-]"
212 if(sscanf(nameAndMode,scanpattern1, name, mode) != 2) {
213 if((r = sscanf(nameAndMode,scanpattern2,name)) == 1) {
214 strncpy(mode,"cbc-plain",10);
217 set_error("no known cipher-spec pattern detected");
230 /* Select free keyslot or verifies that the one specified is empty */
231 static int keyslot_from_option(int keySlotOption, struct luks_phdr *hdr) {
232 if(keySlotOption != -1) {
233 if(keySlotOption >= LUKS_NUMKEYS) {
234 set_error("slot %d too high, please pick between 0 and %d", keySlotOption, LUKS_NUMKEYS);
236 } else if(hdr->keyblock[keySlotOption].active != LUKS_KEY_DISABLED) {
237 set_error("slot %d full, please pick another one", keySlotOption);
240 return keySlotOption;
244 /* Find empty key slot */
245 for(i=0; i<LUKS_NUMKEYS; i++) {
246 if(hdr->keyblock[i].active == LUKS_KEY_DISABLED) break;
248 if(i==LUKS_NUMKEYS) {
249 set_error("All slots full");
256 static int __crypt_create_device(int reload, struct setup_backend *backend,
257 struct crypt_options *options)
259 struct crypt_options tmp = {
260 .name = options->name,
262 struct device_infos infos;
265 char *processed_key = NULL;
268 r = backend->status(0, &tmp, NULL);
274 set_error("Device already exists");
281 if (options->key_size < 0 || options->key_size > 1024) {
282 set_error("Invalid key size");
286 if (get_device_infos(options->device, &infos) < 0)
289 if (!options->size) {
290 options->size = infos.size;
291 if (!options->size) {
292 set_error("Not a block device");
295 if (options->size <= options->offset) {
296 set_error("Invalid offset");
299 options->size -= options->offset;
303 options->flags |= CRYPT_FLAG_READONLY;
305 get_key("Enter passphrase: ", &key, &keyLen, options->key_size, options->key_file, options->passphrase_fd, options->timeout, options->flags);
307 set_error("Key reading error");
311 processed_key = process_key(options,key,keyLen);
314 if (!processed_key) {
315 const char *error=get_error();
317 char *c_error_handling_sucks;
318 asprintf(&c_error_handling_sucks,"Key processing error: %s",error);
319 set_error(c_error_handling_sucks);
320 free(c_error_handling_sucks);
322 set_error("Key processing error");
326 r = backend->create(reload, options, processed_key);
328 safe_free(processed_key);
333 static int __crypt_query_device(int details, struct setup_backend *backend,
334 struct crypt_options *options)
336 int r = backend->status(details, options, NULL);
345 static int __crypt_resize_device(int details, struct setup_backend *backend,
346 struct crypt_options *options)
348 struct crypt_options tmp = {
349 .name = options->name,
351 struct device_infos infos;
355 r = backend->status(1, &tmp, &key);
359 if (get_device_infos(tmp.device, &infos) < 0)
362 if (!options->size) {
363 options->size = infos.size;
364 if (!options->size) {
365 set_error("Not a block device");
368 if (options->size <= tmp.offset) {
369 set_error("Invalid offset");
372 options->size -= tmp.offset;
374 tmp.size = options->size;
377 options->flags |= CRYPT_FLAG_READONLY;
379 r = backend->create(1, &tmp, key);
386 static int __crypt_remove_device(int arg, struct setup_backend *backend,
387 struct crypt_options *options)
391 r = backend->status(0, options, NULL);
395 set_error("Device busy");
399 return backend->remove(0, options);
402 static int __crypt_luks_format(int arg, struct setup_backend *backend, struct crypt_options *options)
406 struct luks_phdr header;
407 struct luks_masterkey *mk=NULL;
409 char cipherName[LUKS_CIPHERNAME_L];
410 char cipherMode[LUKS_CIPHERMODE_L];
411 unsigned int passwordLen;
415 if (!LUKS_device_ready(options->device, O_RDWR | O_EXCL)) {
416 set_error("Can not access device");
417 r = -ENOTBLK; goto out;
420 mk = LUKS_generate_masterkey(options->key_size);
421 if(NULL == mk) return -ENOMEM; // FIXME This may be misleading, since we don't know what went wrong
424 #define printoffset(entry) logger(options, CRYPT_LOG_ERROR, ("offset of " #entry " = %d\n", (char *)(&header.entry)-(char *)(&header))
426 logger(options, CRYPT_LOG_ERROR, "sizeof phdr %d, key slot %d\n",sizeof(struct luks_phdr),sizeof(header.keyblock[0]));
429 printoffset(version);
430 printoffset(cipherName);
431 printoffset(cipherMode);
432 printoffset(hashSpec);
433 printoffset(payloadOffset);
434 printoffset(keyBytes);
435 printoffset(mkDigest);
436 printoffset(mkDigestSalt);
437 printoffset(mkDigestIterations);
441 r = parse_into_name_and_mode(options->cipher, cipherName, cipherMode);
444 r = LUKS_generate_phdr(&header,mk,cipherName, cipherMode,LUKS_STRIPES, options->align_payload);
446 set_error("Can't generate phdr");
450 keyIndex = keyslot_from_option(options->key_slot, &header);
452 PBKDF2perSecond = LUKS_benchmarkt_iterations();
453 header.keyblock[keyIndex].passwordIterations = at_least_one(PBKDF2perSecond * ((float)options->iteration_time / 1000.0));
455 logger(options->icb->log,CRYPT_LOG_ERROR, "pitr %d\n", header.keyblock[0].passwordIterations);
457 get_key("Enter LUKS passphrase: ",&password,&passwordLen, 0, options->new_key_file, options->passphrase_fd, options->timeout, options->flags);
459 r = -EINVAL; goto out;
462 /* Set key, also writes phdr */
463 r = LUKS_set_key(options->device, keyIndex, password, passwordLen, &header, mk, backend);
468 LUKS_dealloc_masterkey(mk);
473 static int __crypt_luks_open(int arg, struct setup_backend *backend, struct crypt_options *options)
475 struct luks_masterkey *mk=NULL;
476 struct luks_phdr hdr;
478 unsigned int passwordLen;
479 struct device_infos infos;
480 struct crypt_options tmp = {
481 .name = options->name,
484 int r, tries = options->tries;
485 int excl = (options->flags & CRYPT_FLAG_NON_EXCLUSIVE_ACCESS) ? 0 : O_EXCL ;
487 r = backend->status(0, &tmp, NULL);
489 set_error("Device already exists");
493 if (!LUKS_device_ready(options->device, O_RDONLY | excl)) {
494 set_error("Can not access device");
498 if (get_device_infos(options->device, &infos) < 0) {
499 set_error("Can't get device information.\n");
504 options->flags |= CRYPT_FLAG_READONLY;
509 if(get_key("Enter LUKS passphrase: ",&password,&passwordLen, 0, options->key_file, options->passphrase_fd, options->timeout, options->flags))
515 r = -EINVAL; goto out;
518 r = LUKS_open_any_key(options->device, password, passwordLen, &hdr, &mk, backend);
520 set_error("No key available with this passphrase.\n");
523 logger(options, CRYPT_LOG_NORMAL,"key slot %d unlocked.\n", r);
526 options->offset = hdr.payloadOffset;
527 asprintf(&dmCipherSpec, "%s-%s", hdr.cipherName, hdr.cipherMode);
532 options->cipher = dmCipherSpec;
533 options->key_size = mk->keyLength;
536 options->size = infos.size;
537 if (!options->size) {
538 set_error("Not a block device.\n");
539 r = -ENOTBLK; goto out2;
541 if (options->size <= options->offset) {
542 set_error("Invalid offset");
543 r = -EINVAL; goto out2;
545 options->size -= options->offset;
546 r = backend->create(0, options, mk->key);
553 LUKS_dealloc_masterkey(mk);
554 if (r == -EPERM && tries > 0)
560 static int __crypt_luks_add_key(int arg, struct setup_backend *backend, struct crypt_options *options)
562 struct luks_masterkey *mk=NULL;
563 struct luks_phdr hdr;
564 char *password=NULL; unsigned int passwordLen;
565 unsigned int i; unsigned int keyIndex;
566 const char *device = options->device;
568 int key_slot = options->key_slot;
570 if (!LUKS_device_ready(options->device, O_RDWR)) {
571 set_error("Can not access device");
572 r = -ENOTBLK; goto out;
575 r = LUKS_read_phdr(device, &hdr);
579 keyIndex = keyslot_from_option(options->key_slot, &hdr);
581 get_key("Enter any LUKS passphrase: ",
586 options->passphrase_fd,
588 options->flags & ~(CRYPT_FLAG_VERIFY | CRYPT_FLAG_VERIFY_IF_POSSIBLE));
591 r = -EINVAL; goto out;
593 r = LUKS_open_any_key(device, password, passwordLen, &hdr, &mk, backend);
595 options->icb->log(CRYPT_LOG_ERROR,"No key available with this passphrase.\n");
596 r = -EPERM; goto out;
598 logger(options, CRYPT_LOG_NORMAL,"key slot %d unlocked.\n", r);
602 get_key("Enter new passphrase for key slot: ",
606 options->new_key_file,
607 options->passphrase_fd,
611 r = -EINVAL; goto out;
614 hdr.keyblock[keyIndex].passwordIterations = at_least_one(LUKS_benchmarkt_iterations() * ((float)options->iteration_time / 1000));
616 r = LUKS_set_key(device, keyIndex, password, passwordLen, &hdr, mk, backend);
622 LUKS_dealloc_masterkey(mk);
626 static int luks_remove_helper(int arg, struct setup_backend *backend, struct crypt_options *options, int supply_it)
628 struct luks_masterkey *mk;
629 struct luks_phdr hdr;
631 unsigned int passwordLen;
632 const char *device = options->device;
636 if (!LUKS_device_ready(options->device, O_RDWR)) {
637 set_error("Can not access device");
638 r = -ENOTBLK; goto out;
642 get_key("Enter LUKS passphrase to be deleted: ",&password,&passwordLen, 0, options->new_key_file, options->passphrase_fd, options->timeout, options->flags);
644 r = -EINVAL; goto out;
646 keyIndex = LUKS_open_any_key(device, password, passwordLen, &hdr, &mk, backend);
648 options->icb->log(CRYPT_LOG_ERROR,"No remaining key available with this passphrase.\n");
649 r = -EPERM; goto out;
651 logger(options, CRYPT_LOG_NORMAL,"key slot %d selected for deletion.\n", keyIndex);
654 keyIndex = options->key_slot;
657 if(LUKS_is_last_keyslot(options->device, keyIndex) &&
658 !(options->icb->yesDialog(_("This is the last keyslot. Device will become unusable after purging this key.")))) {
663 if(options->flags & CRYPT_FLAG_VERIFY_ON_DELKEY) {
664 options->flags &= ~CRYPT_FLAG_VERIFY_ON_DELKEY;
665 get_key("Enter any remaining LUKS passphrase: ",&password,&passwordLen, 0, options->key_file, options->passphrase_fd, options->timeout, options->flags);
667 r = -EINVAL; goto out;
669 openedIndex = LUKS_open_any_key(device, password, passwordLen, &hdr, &mk, backend);
671 if (openedIndex >= 0) {
672 LUKS_dealloc_masterkey(mk);
675 if(openedIndex < 0 || keyIndex == openedIndex) {
676 options->icb->log(CRYPT_LOG_ERROR,"No remaining key available with this passphrase.\n");
677 r = -EPERM; goto out;
679 logger(options, CRYPT_LOG_NORMAL,"key slot %d verified.\n", keyIndex);
681 r = LUKS_del_key(device, keyIndex);
690 static int __crypt_luks_kill_slot(int arg, struct setup_backend *backend, struct crypt_options *options) {
691 return luks_remove_helper(arg, backend, options, 0);
694 static int __crypt_luks_remove_key(int arg, struct setup_backend *backend, struct crypt_options *options) {
695 return luks_remove_helper(arg, backend, options, 1);
699 static int crypt_job(int (*job)(int arg, struct setup_backend *backend,
700 struct crypt_options *options),
701 int arg, struct crypt_options *options)
703 struct setup_backend *backend;
706 backend = get_setup_backend(default_backend);
708 if (setup_enter(backend,options->icb->log) < 0) {
714 set_error("No setup backend available");
719 r = job(arg, backend, options);
721 setup_leave(backend);
723 put_setup_backend(backend);
731 int crypt_create_device(struct crypt_options *options)
733 return crypt_job(__crypt_create_device, 0, options);
736 int crypt_update_device(struct crypt_options *options)
738 return crypt_job(__crypt_create_device, 1, options);
741 int crypt_resize_device(struct crypt_options *options)
743 return crypt_job(__crypt_resize_device, 0, options);
746 int crypt_query_device(struct crypt_options *options)
748 return crypt_job(__crypt_query_device, 1, options);
751 int crypt_remove_device(struct crypt_options *options)
753 return crypt_job(__crypt_remove_device, 0, options);
757 int crypt_luksFormat(struct crypt_options *options)
759 return crypt_job(__crypt_luks_format, 0, options);
762 int crypt_luksOpen(struct crypt_options *options)
764 return crypt_job(__crypt_luks_open, 0, options);
767 int crypt_luksKillSlot(struct crypt_options *options)
769 return crypt_job(__crypt_luks_kill_slot, 0, options);
772 int crypt_luksRemoveKey(struct crypt_options *options)
774 return crypt_job(__crypt_luks_remove_key, 0, options);
777 int crypt_luksAddKey(struct crypt_options *options)
779 return crypt_job(__crypt_luks_add_key, 0, options);
782 int crypt_luksUUID(struct crypt_options *options)
784 struct luks_phdr hdr;
787 r = LUKS_read_phdr(options->device,&hdr);
790 options->icb->log(CRYPT_LOG_NORMAL,hdr.uuid);
791 options->icb->log(CRYPT_LOG_NORMAL,"\n");
795 int crypt_isLuks(struct crypt_options *options)
797 struct luks_phdr hdr;
798 return LUKS_read_phdr(options->device,&hdr);
801 int crypt_luksDump(struct crypt_options *options)
803 struct luks_phdr hdr;
806 r = LUKS_read_phdr(options->device,&hdr);
809 logger(options, CRYPT_LOG_NORMAL, "LUKS header information for %s\n\n",options->device);
810 logger(options, CRYPT_LOG_NORMAL, "Version: \t%d\n",hdr.version);
811 logger(options, CRYPT_LOG_NORMAL, "Cipher name: \t%s\n",hdr.cipherName);
812 logger(options, CRYPT_LOG_NORMAL, "Cipher mode: \t%s\n",hdr.cipherMode);
813 logger(options, CRYPT_LOG_NORMAL, "Hash spec: \t%s\n",hdr.hashSpec);
814 logger(options, CRYPT_LOG_NORMAL, "Payload offset:\t%d\n",hdr.payloadOffset);
815 logger(options, CRYPT_LOG_NORMAL, "MK bits: \t%d\n",hdr.keyBytes*8);
816 logger(options, CRYPT_LOG_NORMAL, "MK digest: \t");
817 hexprintICB(options, CRYPT_LOG_NORMAL, hdr.mkDigest,LUKS_DIGESTSIZE);
818 logger(options, CRYPT_LOG_NORMAL, "\n");
819 logger(options, CRYPT_LOG_NORMAL, "MK salt: \t");
820 hexprintICB(options, CRYPT_LOG_NORMAL, hdr.mkDigestSalt,LUKS_SALTSIZE/2);
821 logger(options, CRYPT_LOG_NORMAL, "\n \t");
822 hexprintICB(options, CRYPT_LOG_NORMAL, hdr.mkDigestSalt+LUKS_SALTSIZE/2,LUKS_SALTSIZE/2);
823 logger(options, CRYPT_LOG_NORMAL, "\n");
824 logger(options, CRYPT_LOG_NORMAL, "MK iterations: \t%d\n",hdr.mkDigestIterations);
825 logger(options, CRYPT_LOG_NORMAL, "UUID: \t%s\n\n",hdr.uuid);
826 for(i=0;i<LUKS_NUMKEYS;i++) {
827 if(hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
828 logger(options, CRYPT_LOG_NORMAL, "Key Slot %d: ENABLED\n",i);
829 logger(options, CRYPT_LOG_NORMAL, "\tIterations: \t%d\n",hdr.keyblock[i].passwordIterations);
830 logger(options, CRYPT_LOG_NORMAL, "\tSalt: \t");
831 hexprintICB(options, CRYPT_LOG_NORMAL, hdr.keyblock[i].passwordSalt,LUKS_SALTSIZE/2);
832 logger(options, CRYPT_LOG_NORMAL, "\n\t \t");
833 hexprintICB(options, CRYPT_LOG_NORMAL, hdr.keyblock[i].passwordSalt+LUKS_SALTSIZE/2,LUKS_SALTSIZE/2);
834 logger(options, CRYPT_LOG_NORMAL, "\n");
836 logger(options, CRYPT_LOG_NORMAL, "\tKey material offset:\t%d\n",hdr.keyblock[i].keyMaterialOffset);
837 logger(options, CRYPT_LOG_NORMAL, "\tAF stripes: \t%d\n",hdr.keyblock[i].stripes);
840 logger(options, CRYPT_LOG_NORMAL, "Key Slot %d: DISABLED\n",i);
846 void crypt_get_error(char *buf, size_t size)
848 const char *error = get_error();
850 if (!buf || size < 1)
853 strncpy(buf, error, size - 1);
854 buf[size - 1] = '\0';
860 void crypt_put_options(struct crypt_options *options)
862 if (options->flags & CRYPT_FLAG_FREE_DEVICE) {
863 free((char *)options->device);
864 options->device = NULL;
865 options->flags &= ~CRYPT_FLAG_FREE_DEVICE;
867 if (options->flags & CRYPT_FLAG_FREE_CIPHER) {
868 free((char *)options->cipher);
869 options->cipher = NULL;
870 options->flags &= ~CRYPT_FLAG_FREE_CIPHER;
874 void crypt_set_default_backend(const char *backend)
877 free(default_backend);
879 default_backend = strdup(backend);
881 default_backend = NULL;
884 const char *crypt_get_dir(void)
886 struct setup_backend *backend;
889 backend = get_setup_backend(default_backend);
893 dir = backend->dir();
895 put_setup_backend(backend);
902 // indent-tabs-mode: nil