2 * cryptsetup library API check functions
4 * Copyright (C) 2009-2010 Red Hat, Inc. All rights reserved.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include <sys/ioctl.h>
30 #include <libdevmapper.h>
32 #include "libcryptsetup.h"
33 #include "utils_loop.h"
35 #define DMDIR "/dev/mapper/"
37 #define DEVICE_1_UUID "28632274-8c8a-493f-835b-da802e1c576b"
38 #define DEVICE_EMPTY_name "crypt_zero"
39 #define DEVICE_EMPTY DMDIR DEVICE_EMPTY_name
40 #define DEVICE_ERROR_name "crypt_error"
41 #define DEVICE_ERROR DMDIR DEVICE_ERROR_name
43 #define CDEVICE_1 "ctest1"
44 #define CDEVICE_2 "ctest2"
45 #define CDEVICE_WRONG "O_o"
47 #define IMAGE1 "compatimage.img"
48 #define IMAGE_EMPTY "empty.img"
50 #define KEYFILE1 "key1.file"
51 #define KEY1 "compatkey"
53 #define KEYFILE2 "key2.file"
54 #define KEY2 "0123456789abcdef"
56 #define PASSPHRASE "blabla"
58 #define DEVICE_TEST_UUID "12345678-1234-1234-1234-123456789abc"
60 #define DEVICE_WRONG "/dev/Ooo_"
61 #define DEVICE_CHAR "/dev/zero"
62 #define TMP_FILE_TEMPLATE "cryptsetuptst.XXXXXX"
64 #define SECTOR_SHIFT 9L
66 static int _debug = 0;
67 static int _verbose = 1;
69 static char global_log[4096];
70 static int global_lines = 0;
72 static char *DEVICE_1 = NULL;
73 static char *DEVICE_2 = NULL;
74 static char *H_DEVICE = NULL;
76 static char *tmp_file_1 = NULL;
77 static char *tmp_file_2 = NULL;
81 static int device_size(const char *device, uint64_t *size)
85 devfd = open(device, O_RDONLY);
89 if (ioctl(devfd, BLKGETSIZE64, size) < 0)
95 // Get key from kernel dm mapping table using dm-ioctl
96 static int _get_key_dm(const char *name, char *buffer, unsigned int buffer_size)
100 uint64_t start, length;
101 char *target_type, *rcipher, *key, *params;
105 if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
107 if (!dm_task_set_name(dmt, name))
109 if (!dm_task_run(dmt))
111 if (!dm_task_get_info(dmt, &dmi))
116 next = dm_get_next_target(dmt, next, &start, &length, &target_type, ¶ms);
117 if (!target_type || strcmp(target_type, "crypt") != 0)
120 rcipher = strsep(¶ms, " ");
121 key = strsep(¶ms, " ");
123 if (buffer_size <= strlen(key))
126 strncpy(buffer, key, buffer_size);
130 dm_task_destroy(dmt);
135 static int _prepare_keyfile(const char *name, const char *passphrase, int size)
139 fd = open(name, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR);
141 r = write(fd, passphrase, size);
146 return r == size ? 0 : 1;
149 static void _remove_keyfiles(void)
155 // Decode key from its hex representation
156 static int crypt_decode_key(char *key, char *hex, unsigned int size)
164 for (i = 0; i < size; i++) {
168 key[i] = (unsigned char)strtoul(buffer, &endp, 16);
170 if (endp != &buffer[2])
180 static void cmdLineLog(int level, char *msg)
182 strncat(global_log, msg, sizeof(global_log) - strlen(global_log));
186 static void new_log(int level, const char *msg, void *usrptr)
188 cmdLineLog(level, (char*)msg);
191 static void reset_log()
193 memset(global_log, 0, sizeof(global_log));
197 static void _system(const char *command, int warn)
199 if (system(command) < 0 && warn)
200 printf("System command failed: %s", command);
203 static void _cleanup(void)
208 //_system("udevadm settle", 0);
210 if (!stat(DMDIR CDEVICE_1, &st))
211 _system("dmsetup remove " CDEVICE_1, 0);
213 if (!stat(DMDIR CDEVICE_2, &st))
214 _system("dmsetup remove " CDEVICE_2, 0);
216 if (!stat(DEVICE_EMPTY, &st))
217 _system("dmsetup remove " DEVICE_EMPTY_name, 0);
219 if (!stat(DEVICE_ERROR, &st))
220 _system("dmsetup remove " DEVICE_ERROR_name, 0);
222 if (crypt_loop_device(DEVICE_1))
223 crypt_loop_detach(DEVICE_1);
225 if (crypt_loop_device(DEVICE_2))
226 crypt_loop_detach(DEVICE_2);
228 _system("rm -f " IMAGE_EMPTY, 0);
229 _system("rm -f " IMAGE1, 0);
236 if (H_DEVICE && crypt_loop_device(H_DEVICE)) {
237 h_str = crypt_loop_backing_file(H_DEVICE);
238 if(!crypt_loop_detach(H_DEVICE) && h_str)
245 static int _setup(void)
248 char *h_str, cmd[128];
250 tmp_file_1 = strdup(TMP_FILE_TEMPLATE);
251 if ((fd=mkstemp(tmp_file_1))==-1) {
252 printf("cannot create temporary file with template %s\n",tmp_file_1);
256 snprintf(cmd,sizeof(cmd),"dd if=/dev/zero of=%s bs=512 count=100", tmp_file_1);
260 H_DEVICE = crypt_loop_get_device();
262 printf("Cannot find free loop device.\n");
265 h_str = strdup(TMP_FILE_TEMPLATE);
266 if ((fd=mkstemp(h_str))==-1) {
267 printf("cannot create temporary file with template %s\n",tmp_file_1);
271 snprintf(cmd,sizeof(cmd),"dd if=/dev/zero of=%s bs=512 count=1", h_str);
273 if (crypt_loop_device(H_DEVICE)) {
274 fd = crypt_loop_attach(H_DEVICE,h_str,0,0,&ro);
279 tmp_file_2 = strdup(TMP_FILE_TEMPLATE);
280 if ((fd=mkstemp(tmp_file_2))==-1) {
281 printf("cannot create temporary file with template %s\n",tmp_file_2);
286 _system("dmsetup create " DEVICE_EMPTY_name " --table \"0 10000 zero\"", 1);
287 _system("dmsetup create " DEVICE_ERROR_name " --table \"0 10000 error\"", 1);
289 DEVICE_1 = crypt_loop_get_device();
291 printf("Cannot find free loop device.\n");
294 if (crypt_loop_device(DEVICE_1)) {
295 _system(" [ ! -e " IMAGE1 " ] && bzip2 -dk " IMAGE1 ".bz2", 1);
296 fd = crypt_loop_attach(DEVICE_1, IMAGE1, 0, 0, &ro);
300 DEVICE_2 = crypt_loop_get_device();
302 printf("Cannot find free loop device.\n");
305 if (crypt_loop_device(DEVICE_2)) {
306 _system("dd if=/dev/zero of=" IMAGE_EMPTY " bs=1M count=4", 1);
307 fd = crypt_loop_attach(DEVICE_2, IMAGE_EMPTY, 0, 0, &ro);
313 void check_ok(int status, int line, const char *func)
318 crypt_get_error(buf, sizeof(buf));
319 printf("FAIL line %d [%s]: code %d, %s\n", line, func, status, buf);
325 void check_ko(int status, int line, const char *func)
329 memset(buf, 0, sizeof(buf));
330 crypt_get_error(buf, sizeof(buf));
332 printf("FAIL line %d [%s]: code %d, %s\n", line, func, status, buf);
336 printf(" => errno %d, errmsg: %s\n", status, buf);
339 void check_equal(int line, const char *func)
341 printf("FAIL line %d [%s]: expected equal values differs.\n", line, func);
346 void xlog(const char *msg, const char *tst, const char *func, int line, const char *txt)
350 printf(" [%s,%s:%d] %s [%s]\n", msg, func, line, tst, txt);
352 printf(" [%s,%s:%d] %s\n", msg, func, line, tst);
355 #define OK_(x) do { xlog("(success)", #x, __FUNCTION__, __LINE__, NULL); \
356 check_ok((x), __LINE__, __FUNCTION__); \
358 #define FAIL_(x, y) do { xlog("(fail) ", #x, __FUNCTION__, __LINE__, y); \
359 check_ko((x), __LINE__, __FUNCTION__); \
361 #define EQ_(x, y) do { xlog("(equal) ", #x " == " #y, __FUNCTION__, __LINE__, NULL); \
362 if ((x) != (y)) check_equal(__LINE__, __FUNCTION__); \
365 #define RUN_(x, y) do { printf("%s: %s\n", #x, (y)); x(); } while (0)
368 static int yesDialog(char *msg)
373 static struct interface_callbacks cmd_icb = {
374 .yesDialog = yesDialog,
379 static void LuksUUID(void)
381 struct crypt_options co = { .icb = &cmd_icb };
383 co.device = DEVICE_EMPTY;
384 EQ_(crypt_luksUUID(&co), -EINVAL);
386 co.device = DEVICE_ERROR;
387 EQ_(crypt_luksUUID(&co), -EINVAL);
390 co.device = DEVICE_1;
391 OK_(crypt_luksUUID(&co));
392 EQ_(strlen(global_log), 37); /* UUID + "\n" */
393 EQ_(strncmp(global_log, DEVICE_1_UUID, strlen(DEVICE_1_UUID)), 0);
397 static void IsLuks(void)
399 struct crypt_options co = { .icb = &cmd_icb };
401 co.device = DEVICE_EMPTY;
402 EQ_(crypt_isLuks(&co), -EINVAL);
404 co.device = DEVICE_ERROR;
405 EQ_(crypt_isLuks(&co), -EINVAL);
407 co.device = DEVICE_1;
408 OK_(crypt_isLuks(&co));
411 static void LuksOpen(void)
413 struct crypt_options co = {
415 //.passphrase = "blabla",
419 OK_(_prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
420 co.key_file = KEYFILE1;
422 co.device = DEVICE_EMPTY;
423 EQ_(crypt_luksOpen(&co), -EINVAL);
425 co.device = DEVICE_ERROR;
426 EQ_(crypt_luksOpen(&co), -EINVAL);
428 co.device = DEVICE_1;
429 OK_(crypt_luksOpen(&co));
430 FAIL_(crypt_luksOpen(&co), "already open");
435 static void query_device(void)
437 struct crypt_options co = {.icb = &cmd_icb };
439 co.name = CDEVICE_WRONG;
440 EQ_(crypt_query_device(&co), 0);
443 EQ_(crypt_query_device(&co), 1);
445 OK_(strncmp(crypt_get_dir(), DMDIR, 11));
446 OK_(strcmp(co.cipher, "aes-cbc-essiv:sha256"));
447 EQ_(co.key_size, 16);
448 EQ_(co.offset, 1032);
449 EQ_(co.flags & CRYPT_FLAG_READONLY, 0);
451 crypt_put_options(&co);
454 static void remove_device(void)
457 struct crypt_options co = {.icb = &cmd_icb };
459 co.name = CDEVICE_WRONG;
460 EQ_(crypt_remove_device(&co), -ENODEV);
462 fd = open(DMDIR CDEVICE_1, O_RDONLY);
464 FAIL_(crypt_remove_device(&co), "device busy");
467 OK_(crypt_remove_device(&co));
470 static void LuksFormat(void)
472 struct crypt_options co = {
476 .cipher = "aes-cbc-essiv:sha256",
479 .iteration_time = 10,
484 OK_(_prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
486 co.new_key_file = KEYFILE1;
487 co.device = DEVICE_ERROR;
488 FAIL_(crypt_luksFormat(&co), "error device");
490 co.device = DEVICE_2;
491 OK_(crypt_luksFormat(&co));
493 co.new_key_file = NULL;
494 co.key_file = KEYFILE1;
496 OK_(crypt_luksOpen(&co));
497 OK_(crypt_remove_device(&co));
501 static void LuksKeyGame(void)
504 struct crypt_options co = {
508 .cipher = "aes-cbc-essiv:sha256",
511 .iteration_time = 10,
516 OK_(_prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
517 OK_(_prepare_keyfile(KEYFILE2, KEY2, strlen(KEY2)));
519 co.new_key_file = KEYFILE1;
520 co.device = DEVICE_2;
522 FAIL_(crypt_luksFormat(&co), "wrong slot #");
524 co.key_slot = 7; // last slot
525 OK_(crypt_luksFormat(&co));
527 co.new_key_file = KEYFILE1;
528 co.key_file = KEYFILE1;
530 FAIL_(crypt_luksAddKey(&co), "wrong slot #");
532 FAIL_(crypt_luksAddKey(&co), "slot already used");
535 OK_(crypt_luksAddKey(&co));
537 co.key_file = KEYFILE2 "blah";
539 FAIL_(crypt_luksAddKey(&co), "keyfile not found");
541 co.new_key_file = KEYFILE2; // key to add
542 co.key_file = KEYFILE1;
544 for (i = 0; i < 6; i++)
545 OK_(crypt_luksAddKey(&co)); //FIXME: EQ_(i)?
547 FAIL_(crypt_luksAddKey(&co), "all slots full");
550 co.new_key_file = KEYFILE1; // key to remove
552 co.key_slot = 8; // should be ignored
553 // only 2 slots should use KEYFILE1
554 OK_(crypt_luksRemoveKey(&co));
555 OK_(crypt_luksRemoveKey(&co));
556 FAIL_(crypt_luksRemoveKey(&co), "no slot with this passphrase");
558 co.new_key_file = KEYFILE2 "blah";
560 FAIL_(crypt_luksRemoveKey(&co), "keyfile not found");
563 co.new_key_file = NULL;
566 FAIL_(crypt_luksKillSlot(&co), "wrong slot #");
568 FAIL_(crypt_luksKillSlot(&co), "slot already wiped");
571 OK_(crypt_luksKillSlot(&co));
576 size_t _get_device_size(const char *device)
578 unsigned long size = 0;
581 fd = open(device, O_RDONLY);
584 (void)ioctl(fd, BLKGETSIZE, &size);
590 void DeviceResizeGame(void)
593 struct crypt_options co = {
597 .cipher = "aes-cbc-plain",
604 orig_size = _get_device_size(DEVICE_2);
606 OK_(_prepare_keyfile(KEYFILE2, KEY2, strlen(KEY2)));
608 co.key_file = KEYFILE2;
610 OK_(crypt_create_device(&co));
611 EQ_(_get_device_size(DMDIR CDEVICE_2), 1000);
614 OK_(crypt_resize_device(&co));
615 EQ_(_get_device_size(DMDIR CDEVICE_2), 2000);
618 OK_(crypt_resize_device(&co));
619 EQ_(_get_device_size(DMDIR CDEVICE_2), (orig_size - 333));
623 co.cipher = "aes-cbc-essiv:sha256";
624 OK_(crypt_update_device(&co));
625 EQ_(_get_device_size(DMDIR CDEVICE_2), (orig_size - 444));
627 memset(&co, 0, sizeof(co));
630 EQ_(crypt_query_device(&co), 1);
631 EQ_(strcmp(co.cipher, "aes-cbc-essiv:sha256"), 0);
632 EQ_(co.key_size, 128 / 8);
635 crypt_put_options(&co);
637 // dangerous switch device still works
638 memset(&co, 0, sizeof(co));
640 co.device = DEVICE_1;
641 co.key_file = KEYFILE2;
642 co.key_size = 128 / 8;
643 co.cipher = "aes-cbc-plain";
646 OK_(crypt_update_device(&co));
648 memset(&co, 0, sizeof(co));
651 EQ_(crypt_query_device(&co), 1);
652 EQ_(strcmp(co.cipher, "aes-cbc-plain"), 0);
653 EQ_(co.key_size, 128 / 8);
656 // This expect lookup returns prefered /dev/loopX
657 EQ_(strcmp(co.device, DEVICE_1), 0);
658 crypt_put_options(&co);
660 memset(&co, 0, sizeof(co));
663 OK_(crypt_remove_device(&co));
671 static void AddDevicePlain(void)
673 struct crypt_device *cd, *cd2;
674 struct crypt_params_plain params = {
681 char key[128], key2[128], path[128];
683 char *passphrase = PASSPHRASE;
684 // hashed hex version of PASSPHRASE
685 char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
686 size_t key_size = strlen(mk_hex) / 2;
687 char *cipher = "aes";
688 char *cipher_mode = "cbc-essiv:sha256";
690 uint64_t size, r_size;
692 crypt_decode_key(key, mk_hex, key_size);
693 FAIL_(crypt_init(&cd, ""), "empty device string");
694 FAIL_(crypt_init(&cd, DEVICE_WRONG), "nonexistent device name ");
695 FAIL_(crypt_init(&cd, DEVICE_CHAR), "character device as backing device");
696 OK_(crypt_init(&cd, tmp_file_1));
699 // test crypt_format, crypt_get_cipher, crypt_get_cipher_mode, crypt_get_volume_key_size
700 OK_(crypt_init(&cd,DEVICE_1));
703 FAIL_(crypt_format(cd,CRYPT_PLAIN,NULL,cipher_mode,NULL,NULL,key_size,¶ms),"cipher param is null");
704 FAIL_(crypt_format(cd,CRYPT_PLAIN,cipher,NULL,NULL,NULL,key_size,¶ms),"cipher_mode param is null");
705 OK_(crypt_format(cd,CRYPT_PLAIN,cipher,cipher_mode,NULL,NULL,key_size,¶ms));
706 OK_(strcmp(cipher_mode,crypt_get_cipher_mode(cd)));
707 OK_(strcmp(cipher,crypt_get_cipher(cd)));
708 EQ_(key_size, crypt_get_volume_key_size(cd));
709 EQ_(params.skip, crypt_get_iv_offset(cd));
710 EQ_(params.offset, crypt_get_data_offset(cd));
715 FAIL_(crypt_set_uuid(cd,DEVICE_1_UUID),"can't set uuid to plain device");
717 // crypt_load() should fail for PLAIN
718 FAIL_(crypt_load(cd,CRYPT_PLAIN,NULL),"can't load header from plain device");
722 // default is "plain" hash - no password hash
723 OK_(crypt_init(&cd, DEVICE_1));
724 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, NULL));
725 FAIL_(crypt_activate_by_volume_key(cd, NULL, key, key_size, 0), "cannot verify key with plain");
726 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
727 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
728 OK_(crypt_deactivate(cd, CDEVICE_1));
731 // test boundaries in offset parameter
732 device_size(DEVICE_1,&size);
734 // zero sectors length
735 params.offset = size >> SECTOR_SHIFT;
736 OK_(crypt_init(&cd, DEVICE_1));
737 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
738 EQ_(crypt_get_data_offset(cd),params.offset);
739 // device size is 0 sectors
740 FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0), "invalid device size (0 blocks)");
741 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
742 // data part of crypt device is of 1 sector size
743 params.offset = (size >> SECTOR_SHIFT) - 1;
744 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
745 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
746 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
747 snprintf(path, sizeof(path), "%s/%s", crypt_get_dir(), CDEVICE_1);
748 if (device_size(path, &r_size) >= 0)
749 EQ_(r_size>>SECTOR_SHIFT, 1);
750 OK_(crypt_deactivate(cd, CDEVICE_1));
752 // size > device_size
754 params.size = (size >> SECTOR_SHIFT) + 1;
755 crypt_init(&cd, DEVICE_1);
756 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
757 FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0),"Device too small");
758 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
760 // offset == device_size (autodetect size)
761 params.offset = (size >> SECTOR_SHIFT);
763 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
764 FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0),"Device too small");
765 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
767 // offset == device_size (user defined size)
768 params.offset = (size >> SECTOR_SHIFT);
770 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
771 FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0),"Device too small");
772 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
774 // offset+size > device_size
776 params.size = (size >> SECTOR_SHIFT) - params.offset + 1;
777 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
778 FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0),"Offset and size are beyond device real size");
779 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
781 // offset+size == device_size
783 params.size = (size >> SECTOR_SHIFT) - params.offset;
784 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
785 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
786 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
787 if (!device_size(path, &r_size))
788 EQ_((r_size >> SECTOR_SHIFT),params.size);
789 OK_(crypt_deactivate(cd,CDEVICE_1));
792 params.hash = "sha1";
797 // Now use hashed password
798 OK_(crypt_init(&cd, DEVICE_1));
799 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
800 FAIL_(crypt_activate_by_passphrase(cd, NULL, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0),
801 "cannot verify passphrase with plain" );
802 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
804 // device status check
805 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
806 snprintf(path, sizeof(path), "%s/%s", crypt_get_dir(), CDEVICE_1);
807 fd = open(path, O_RDONLY);
808 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_BUSY);
809 FAIL_(crypt_deactivate(cd, CDEVICE_1), "Device is busy");
811 OK_(crypt_deactivate(cd, CDEVICE_1));
812 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
814 // crypt_init_by_name_and_header
815 OK_(crypt_init(&cd,DEVICE_1));
816 OK_(crypt_format(cd, CRYPT_PLAIN,cipher,cipher_mode,NULL,NULL,key_size,¶ms));
817 OK_(crypt_activate_by_volume_key(cd,CDEVICE_1,key,key_size,0));
818 FAIL_(crypt_init_by_name_and_header(&cd2,CDEVICE_1,H_DEVICE),"can't init plain device by header device");
819 OK_(crypt_init_by_name(&cd2,CDEVICE_1));
820 OK_(crypt_deactivate(cd,CDEVICE_1));
824 OK_(crypt_init(&cd,DEVICE_1));
825 OK_(crypt_format(cd,CRYPT_PLAIN,cipher,cipher_mode,NULL,NULL,key_size,¶ms));
829 // crypt_set_data_device
830 FAIL_(crypt_set_data_device(cd,H_DEVICE),"can't set data device for plain device");
833 OK_(strcmp(crypt_get_type(cd),CRYPT_PLAIN));
835 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
836 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
839 OK_(crypt_resize(cd,CDEVICE_1,size>>SECTOR_SHIFT)); // same size
840 if (!device_size(path,&r_size))
844 FAIL_(crypt_resize(cd, CDEVICE_1, ULLONG_MAX),"Backing device is too small");
845 FAIL_(crypt_resize(cd, CDEVICE_1, (size>>SECTOR_SHIFT)+1),"crypt device overlaps backing device");
848 OK_(crypt_resize(cd,CDEVICE_1, 123));
849 if (!device_size(path,&r_size))
850 EQ_(r_size>>SECTOR_SHIFT, 123);
851 OK_(crypt_resize(cd,CDEVICE_1,0)); // full size (autodetect)
852 if (!device_size(path,&r_size))
854 OK_(crypt_deactivate(cd,CDEVICE_1));
855 EQ_(crypt_status(cd,CDEVICE_1),CRYPT_INACTIVE);
858 OK_(crypt_init(&cd,DEVICE_1));
860 params.size = (size>>SECTOR_SHIFT) - params.offset - 10;
861 OK_(crypt_format(cd,CRYPT_PLAIN,cipher,cipher_mode,NULL,NULL,key_size,¶ms));
862 OK_(crypt_activate_by_volume_key(cd,CDEVICE_1,key,key_size,0));
863 if (!device_size(path,&r_size))
864 EQ_(r_size>>SECTOR_SHIFT, params.size);
865 // resize to fill remaining capacity
866 OK_(crypt_resize(cd,CDEVICE_1,params.size + 10));
867 if (!device_size(path,&r_size))
868 EQ_(r_size>>SECTOR_SHIFT, params.size + 10);
870 // 1 sector beyond real size
871 FAIL_(crypt_resize(cd,CDEVICE_1,params.size + 11), "new device size overlaps backing device"); // with respect to offset
872 if (!device_size(path,&r_size))
873 EQ_(r_size>>SECTOR_SHIFT, params.size + 10);
874 EQ_(crypt_status(cd,CDEVICE_1),CRYPT_ACTIVE);
875 fd = open(path, O_RDONLY);
879 // resize to minimal size
880 OK_(crypt_resize(cd,CDEVICE_1, 1)); // minimal device size
881 if (!device_size(path,&r_size))
882 EQ_(r_size>>SECTOR_SHIFT, 1);
883 // use size of backing device (autodetect with respect to offset)
884 OK_(crypt_resize(cd,CDEVICE_1,0));
885 if (!device_size(path,&r_size))
886 EQ_(r_size>>SECTOR_SHIFT, (size >> SECTOR_SHIFT)- 42);
887 OK_(crypt_deactivate(cd,CDEVICE_1));
891 OK_(crypt_format(cd,CRYPT_PLAIN,cipher,cipher_mode,NULL,NULL,key_size,¶ms));
892 OK_(crypt_activate_by_volume_key(cd,CDEVICE_1,key,key_size,0));
894 // suspend/resume tests
895 FAIL_(crypt_suspend(cd,CDEVICE_1),"cannot suspend plain device");
896 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
897 FAIL_(crypt_resume_by_passphrase(cd,CDEVICE_1,CRYPT_ANY_SLOT,passphrase, strlen(passphrase)),"cannot resume plain device");
898 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
900 // retrieve volume key check
901 memset(key2, 0, key_size);
904 FAIL_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)), "small buffer");
906 OK_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)));
908 OK_(memcmp(key, key2, key_size));
909 OK_(strcmp(cipher, crypt_get_cipher(cd)));
910 OK_(strcmp(cipher_mode, crypt_get_cipher_mode(cd)));
911 EQ_(key_size, crypt_get_volume_key_size(cd));
912 EQ_(0, crypt_get_data_offset(cd));
913 OK_(crypt_deactivate(cd, CDEVICE_1));
916 OK_(_prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
917 OK_(_prepare_keyfile(KEYFILE2, KEY2, strlen(KEY2)));
918 FAIL_(crypt_activate_by_keyfile(cd, NULL, CRYPT_ANY_SLOT, KEYFILE1, 0, 0), "cannot verify key with plain");
919 EQ_(0, crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
920 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
921 OK_(crypt_deactivate(cd, CDEVICE_1));
924 OK_(crypt_init(&cd,DEVICE_1));
925 OK_(crypt_format(cd,CRYPT_PLAIN,cipher,cipher_mode,NULL,NULL,key_size,¶ms));
928 FAIL_(crypt_keyslot_add_by_passphrase(cd,CRYPT_ANY_SLOT,passphrase,strlen(passphrase),passphrase,strlen(passphrase)), "can't add keyslot to plain device");
929 FAIL_(crypt_keyslot_add_by_volume_key(cd,CRYPT_ANY_SLOT ,key,key_size,passphrase,strlen(passphrase)),"can't add keyslot to plain device");
930 FAIL_(crypt_keyslot_add_by_keyfile(cd,CRYPT_ANY_SLOT,KEYFILE1,strlen(KEY1),KEYFILE2,strlen(KEY2)),"can't add keyslot to plain device");
931 FAIL_(crypt_keyslot_destroy(cd,1),"can't manipulate keyslots on plain device");
932 EQ_(crypt_keyslot_status(cd, 0), CRYPT_SLOT_INVALID);
938 #define CALLBACK_ERROR "calback_error xyz"
939 static int pass_callback_err(const char *msg, char *buf, size_t length, void *usrptr)
941 struct crypt_device *cd = usrptr;
947 crypt_log(cd, CRYPT_LOG_ERROR, CALLBACK_ERROR);
951 static int pass_callback_ok(const char *msg, char *buf, size_t length, void *usrptr)
955 strcpy(buf, PASSPHRASE);
959 static void CallbacksTest(void)
961 struct crypt_device *cd;
962 struct crypt_params_plain params = {
968 size_t key_size = 256 / 8;
969 char *cipher = "aes";
970 char *cipher_mode = "cbc-essiv:sha256";
971 char *passphrase = PASSPHRASE;
973 OK_(crypt_init(&cd, DEVICE_1));
974 crypt_set_log_callback(cd, &new_log, NULL);
975 //crypt_set_log_callback(cd, NULL, NULL);
977 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
979 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
980 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
981 OK_(crypt_deactivate(cd, CDEVICE_1));
984 crypt_set_password_callback(cd, pass_callback_err, cd);
985 FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, NULL, 0, 0), "callback fails");
986 EQ_(strncmp(global_log, CALLBACK_ERROR, strlen(CALLBACK_ERROR)), 0);
988 crypt_set_password_callback(cd, pass_callback_ok, NULL);
989 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, NULL, 0, 0));
990 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
991 OK_(crypt_deactivate(cd, CDEVICE_1));
996 static void UseLuksDevice(void)
998 struct crypt_device *cd;
1002 OK_(crypt_init(&cd, DEVICE_1));
1003 OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
1004 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
1005 OK_(crypt_activate_by_passphrase(cd, NULL, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0));
1006 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0));
1007 FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0), "already open");
1008 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1009 OK_(crypt_deactivate(cd, CDEVICE_1));
1010 FAIL_(crypt_deactivate(cd, CDEVICE_1), "no such device");
1013 OK_(strcmp("aes", crypt_get_cipher(cd)));
1014 OK_(strcmp("cbc-essiv:sha256", crypt_get_cipher_mode(cd)));
1015 OK_(strcmp(DEVICE_1_UUID, crypt_get_uuid(cd)));
1016 EQ_(key_size, crypt_get_volume_key_size(cd));
1017 EQ_(1032, crypt_get_data_offset(cd));
1019 EQ_(0, crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key, &key_size, KEY1, strlen(KEY1)));
1020 OK_(crypt_volume_key_verify(cd, key, key_size));
1021 OK_(crypt_activate_by_volume_key(cd, NULL, key, key_size, 0));
1022 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1023 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1024 OK_(crypt_deactivate(cd, CDEVICE_1));
1027 FAIL_(crypt_volume_key_verify(cd, key, key_size), "key mismatch");
1028 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0), "key mismatch");
1032 static void SuspendDevice(void)
1035 struct crypt_device *cd;
1037 OK_(crypt_init(&cd, DEVICE_1));
1038 OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
1039 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0));
1041 suspend_status = crypt_suspend(cd, CDEVICE_1);
1042 if (suspend_status == -ENOTSUP) {
1043 printf("WARNING: Suspend/Resume not supported, skipping test.\n");
1046 OK_(suspend_status);
1047 FAIL_(crypt_suspend(cd, CDEVICE_1), "already suspended");
1049 FAIL_(crypt_resume_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1)-1), "wrong key");
1050 OK_(crypt_resume_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1)));
1051 FAIL_(crypt_resume_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1)), "not suspended");
1053 OK_(_prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
1054 OK_(crypt_suspend(cd, CDEVICE_1));
1055 FAIL_(crypt_resume_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1 "blah", 0), "wrong keyfile");
1056 OK_(crypt_resume_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0));
1057 FAIL_(crypt_resume_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0), "not suspended");
1060 OK_(crypt_deactivate(cd, CDEVICE_1));
1064 static void AddDeviceLuks(void)
1066 struct crypt_device *cd;
1067 struct crypt_params_luks1 params = {
1069 .data_alignment = 2048, // 4M, data offset will be 4096
1071 char key[128], key2[128];
1073 char *passphrase = "blabla";
1074 char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
1075 size_t key_size = strlen(mk_hex) / 2;
1076 char *cipher = "aes";
1077 char *cipher_mode = "cbc-essiv:sha256";
1079 crypt_decode_key(key, mk_hex, key_size);
1081 OK_(crypt_init(&cd, DEVICE_2));
1082 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1084 // even with no keyslots defined it can be activated by volume key
1085 OK_(crypt_volume_key_verify(cd, key, key_size));
1086 OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, key, key_size, 0));
1087 EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
1088 OK_(crypt_deactivate(cd, CDEVICE_2));
1091 EQ_(7, crypt_keyslot_add_by_volume_key(cd, 7, key, key_size, passphrase, strlen(passphrase)));
1092 EQ_(CRYPT_SLOT_ACTIVE_LAST, crypt_keyslot_status(cd, 7));
1093 EQ_(7, crypt_activate_by_passphrase(cd, CDEVICE_2, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
1094 EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
1095 OK_(crypt_deactivate(cd, CDEVICE_2));
1097 EQ_(1, crypt_keyslot_add_by_volume_key(cd, 1, key, key_size, KEY1, strlen(KEY1)));
1098 OK_(_prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
1099 OK_(_prepare_keyfile(KEYFILE2, KEY2, strlen(KEY2)));
1100 EQ_(2, crypt_keyslot_add_by_keyfile(cd, 2, KEYFILE1, 0, KEYFILE2, 0));
1101 FAIL_(crypt_activate_by_keyfile(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, strlen(KEY2)-1, 0), "key mismatch");
1102 EQ_(2, crypt_activate_by_keyfile(cd, NULL, CRYPT_ANY_SLOT, KEYFILE2, 0, 0));
1103 EQ_(2, crypt_activate_by_keyfile(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, 0, 0));
1104 OK_(crypt_keyslot_destroy(cd, 1));
1105 OK_(crypt_keyslot_destroy(cd, 2));
1106 OK_(crypt_deactivate(cd, CDEVICE_2));
1109 FAIL_(crypt_keyslot_add_by_volume_key(cd, 7, key, key_size, passphrase, strlen(passphrase)), "slot used");
1111 FAIL_(crypt_keyslot_add_by_volume_key(cd, 6, key, key_size, passphrase, strlen(passphrase)), "key mismatch");
1113 EQ_(6, crypt_keyslot_add_by_volume_key(cd, 6, key, key_size, passphrase, strlen(passphrase)));
1114 EQ_(CRYPT_SLOT_ACTIVE, crypt_keyslot_status(cd, 6));
1116 FAIL_(crypt_keyslot_destroy(cd, 8), "invalid keyslot");
1117 FAIL_(crypt_keyslot_destroy(cd, CRYPT_ANY_SLOT), "invalid keyslot");
1118 FAIL_(crypt_keyslot_destroy(cd, 0), "keyslot not used");
1119 OK_(crypt_keyslot_destroy(cd, 7));
1120 EQ_(CRYPT_SLOT_INACTIVE, crypt_keyslot_status(cd, 7));
1121 EQ_(CRYPT_SLOT_ACTIVE_LAST, crypt_keyslot_status(cd, 6));
1123 EQ_(6, crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)));
1124 OK_(crypt_volume_key_verify(cd, key2, key_size));
1126 OK_(memcmp(key, key2, key_size));
1127 OK_(strcmp(cipher, crypt_get_cipher(cd)));
1128 OK_(strcmp(cipher_mode, crypt_get_cipher_mode(cd)));
1129 EQ_(key_size, crypt_get_volume_key_size(cd));
1130 EQ_(4096, crypt_get_data_offset(cd));
1131 OK_(strcmp(DEVICE_2, crypt_get_device_name(cd)));
1134 crypt_set_log_callback(cd, &new_log, NULL);
1135 OK_(crypt_dump(cd));
1136 OK_(!(global_lines != 0));
1137 crypt_set_log_callback(cd, NULL, NULL);
1140 FAIL_(crypt_set_uuid(cd, "blah"), "wrong UUID format");
1141 OK_(crypt_set_uuid(cd, DEVICE_TEST_UUID));
1142 OK_(strcmp(DEVICE_TEST_UUID, crypt_get_uuid(cd)));
1144 FAIL_(crypt_deactivate(cd, CDEVICE_2), "not active");
1148 static void UseTempVolumes(void)
1150 struct crypt_device *cd;
1153 // Tepmporary device without keyslot but with on-disk LUKS header
1154 OK_(crypt_init(&cd, DEVICE_2));
1155 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "not yet formatted");
1156 OK_(crypt_format(cd, CRYPT_LUKS1, "aes", "cbc-essiv:sha256", NULL, NULL, 16, NULL));
1157 OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0));
1158 EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
1161 OK_(crypt_init_by_name(&cd, CDEVICE_2));
1162 OK_(crypt_deactivate(cd, CDEVICE_2));
1165 // Dirty checks: device without UUID
1166 // we should be able to remove it but not manuipulate with it
1167 snprintf(tmp, sizeof(tmp), "dmsetup create %s --table \""
1168 "0 100 crypt aes-cbc-essiv:sha256 deadbabedeadbabedeadbabedeadbabe 0 "
1169 "%s 2048\"", CDEVICE_2, DEVICE_2);
1171 OK_(crypt_init_by_name(&cd, CDEVICE_2));
1172 OK_(crypt_deactivate(cd, CDEVICE_2));
1173 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "No known device type");
1176 // Dirty checks: device with UUID but LUKS header key fingerprint must fail)
1177 snprintf(tmp, sizeof(tmp), "dmsetup create %s --table \""
1178 "0 100 crypt aes-cbc-essiv:sha256 deadbabedeadbabedeadbabedeadbabe 0 "
1179 "%s 2048\" -u CRYPT-LUKS1-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-ctest1",
1180 CDEVICE_2, DEVICE_2);
1182 OK_(crypt_init_by_name(&cd, CDEVICE_2));
1183 OK_(crypt_deactivate(cd, CDEVICE_2));
1184 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "wrong volume key");
1188 OK_(crypt_init(&cd, DEVICE_2));
1189 OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
1190 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "volume key is lost");
1194 OK_(crypt_init(&cd, DEVICE_2));
1195 OK_(crypt_format(cd, CRYPT_PLAIN, "aes", "cbc-essiv:sha256", NULL, NULL, 16, NULL));
1196 FAIL_(crypt_activate_by_volume_key(cd, NULL, "xxx", 3, 0), "cannot verify key with plain");
1197 FAIL_(crypt_volume_key_verify(cd, "xxx", 3), "cannot verify key with plain");
1198 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, "xxx", 3, 0), "wrong key lenght");
1199 OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, "volumekeyvolumek", 16, 0));
1200 EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
1201 OK_(crypt_deactivate(cd, CDEVICE_2));
1205 static void HashDevicePlain(void)
1207 struct crypt_device *cd;
1208 struct crypt_params_plain params = {
1215 char *mk_hex, *keystr, key[256];
1217 OK_(crypt_init(&cd, DEVICE_1));
1218 OK_(crypt_format(cd, CRYPT_PLAIN, "aes", "cbc-essiv:sha256", NULL, NULL, 16, ¶ms));
1220 // hash PLAIN, short key
1221 OK_(_prepare_keyfile(KEYFILE1, "tooshort", 8));
1222 FAIL_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 16, 0), "not enough data in keyfile");
1225 // hash PLAIN, exact key
1226 // 0 1 2 3 4 5 6 7 8 9 a b c d e f
1227 mk_hex = "caffeecaffeecaffeecaffeecaffee88";
1229 crypt_decode_key(key, mk_hex, key_size);
1230 OK_(_prepare_keyfile(KEYFILE1, key, key_size));
1231 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size, 0));
1232 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1233 OK_(strcmp(key, mk_hex));
1234 OK_(crypt_deactivate(cd, CDEVICE_1));
1237 mk_hex = "caffeecaffeecaffeecaffeeca000000";
1238 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size - 3, 0));
1239 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1240 OK_(strcmp(key, mk_hex));
1241 OK_(crypt_deactivate(cd, CDEVICE_1));
1245 // hash PLAIN, long key
1246 // 0 1 2 3 4 5 6 7 8 9 a b c d e f
1247 mk_hex = "caffeecaffeecaffeecaffeecaffee88babebabe";
1249 crypt_decode_key(key, mk_hex, key_size);
1250 OK_(_prepare_keyfile(KEYFILE1, key, strlen(mk_hex) / 2));
1251 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size, 0));
1252 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1253 FAIL_(strcmp(key, mk_hex), "only key length used");
1254 OK_(strncmp(key, mk_hex, key_size));
1255 OK_(crypt_deactivate(cd, CDEVICE_1));
1257 // Now without explicit limit
1258 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
1259 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1260 FAIL_(strcmp(key, mk_hex), "only key length used");
1261 OK_(strncmp(key, mk_hex, key_size));
1262 OK_(crypt_deactivate(cd, CDEVICE_1));
1267 params.hash = "sha256";
1268 OK_(crypt_format(cd, CRYPT_PLAIN, "aes", "cbc-essiv:sha256", NULL, NULL, 16, ¶ms));
1270 // 0 1 2 3 4 5 6 7 8 9 a b c d e f
1271 mk_hex = "c62e4615bd39e222572f3a1bf7c2132e";
1272 keystr = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
1273 key_size = strlen(keystr); // 32
1274 OK_(_prepare_keyfile(KEYFILE1, keystr, strlen(keystr)));
1275 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size, 0));
1276 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1277 OK_(strcmp(key, mk_hex));
1278 OK_(crypt_deactivate(cd, CDEVICE_1));
1280 // Read full keyfile
1281 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
1282 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1283 OK_(strcmp(key, mk_hex));
1284 OK_(crypt_deactivate(cd, CDEVICE_1));
1288 // Limit keyfile read
1289 keystr = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxAAAAAAAA";
1290 OK_(_prepare_keyfile(KEYFILE1, keystr, strlen(keystr)));
1291 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size, 0));
1292 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1293 OK_(strcmp(key, mk_hex));
1294 OK_(crypt_deactivate(cd, CDEVICE_1));
1297 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
1298 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1299 OK_(strcmp(key, "0e49cb34a1dee1df33f6505e4de44a66"));
1300 OK_(crypt_deactivate(cd, CDEVICE_1));
1304 // FIXME: add keyfile="-" tests somehow
1309 // Check that gcrypt is properly initialised in format
1310 static void NonFIPSAlg(void)
1312 struct crypt_device *cd;
1313 struct crypt_params_luks1 params = {0};
1315 size_t key_size = 128;
1316 char *cipher = "aes";
1317 char *cipher_mode = "cbc-essiv:sha256";
1320 OK_(crypt_init(&cd, DEVICE_2));
1321 params.hash = "sha256";
1322 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1324 params.hash = "whirlpool";
1325 ret = crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms);
1327 printf("WARNING: whirlpool not supported, skipping test.\n");
1332 params.hash = "md5";
1333 FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms),
1334 "MD5 unsupported, too short");
1338 int main (int argc, char *argv[])
1342 if (getuid() != 0) {
1343 printf("You must be root to run this test.\n");
1347 for (i = 1; i < argc; i++) {
1348 if (!strcmp("-v", argv[i]) || !strcmp("--verbose", argv[i]))
1350 else if (!strcmp("--debug", argv[i]))
1351 _debug = _verbose = 1;
1358 crypt_set_debug_level(_debug ? CRYPT_DEBUG_ALL : CRYPT_DEBUG_NONE);
1360 RUN_(NonFIPSAlg, "Crypto is properly initialised in format"); //must be the first!
1362 RUN_(LuksUUID, "luksUUID API call");
1363 RUN_(IsLuks, "isLuks API call");
1364 RUN_(LuksOpen, "luksOpen API call");
1365 RUN_(query_device, "crypt_query_device API call");
1366 RUN_(remove_device, "crypt_remove_device API call");
1367 RUN_(LuksFormat, "luksFormat API call");
1368 RUN_(LuksKeyGame, "luksAddKey, RemoveKey, KillSlot API calls");
1369 RUN_(DeviceResizeGame, "regular crypto, resize calls");
1371 RUN_(AddDevicePlain, "plain device API creation exercise");
1372 RUN_(HashDevicePlain, "plain device API hash test");
1373 RUN_(AddDeviceLuks, "Format and use LUKS device");
1374 RUN_(UseLuksDevice, "Use pre-formated LUKS device");
1375 RUN_(SuspendDevice, "Suspend/Resume test");
1376 RUN_(UseTempVolumes, "Format and use temporary encrypted device");
1378 RUN_(CallbacksTest, "API callbacks test");