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 static int _debug = 0;
61 static int _verbose = 1;
63 static char global_log[4096];
64 static int global_lines = 0;
66 static char *DEVICE_1 = NULL;
67 static char *DEVICE_2 = NULL;
71 // Get key from kernel dm mapping table using dm-ioctl
72 static int _get_key_dm(const char *name, char *buffer, unsigned int buffer_size)
76 uint64_t start, length;
77 char *target_type, *rcipher, *key, *params;
81 if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
83 if (!dm_task_set_name(dmt, name))
85 if (!dm_task_run(dmt))
87 if (!dm_task_get_info(dmt, &dmi))
92 next = dm_get_next_target(dmt, next, &start, &length, &target_type, ¶ms);
93 if (!target_type || strcmp(target_type, "crypt") != 0)
96 rcipher = strsep(¶ms, " ");
97 key = strsep(¶ms, " ");
99 if (buffer_size <= strlen(key))
102 strncpy(buffer, key, buffer_size);
106 dm_task_destroy(dmt);
111 static int _prepare_keyfile(const char *name, const char *passphrase, int size)
115 fd = open(name, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR);
117 r = write(fd, passphrase, size);
122 return r == size ? 0 : 1;
125 static void _remove_keyfiles(void)
131 // Decode key from its hex representation
132 static int crypt_decode_key(char *key, char *hex, unsigned int size)
140 for (i = 0; i < size; i++) {
144 key[i] = (unsigned char)strtoul(buffer, &endp, 16);
146 if (endp != &buffer[2])
156 static int yesDialog(char *msg)
161 static void cmdLineLog(int level, char *msg)
163 strncat(global_log, msg, sizeof(global_log) - strlen(global_log));
167 static void new_log(int level, const char *msg, void *usrptr)
169 cmdLineLog(level, (char*)msg);
173 static void reset_log()
175 memset(global_log, 0, sizeof(global_log));
179 static void _system(const char *command, int warn)
181 if (system(command) < 0 && warn)
182 printf("System command failed: %s", command);
185 static struct interface_callbacks cmd_icb = {
186 .yesDialog = yesDialog,
190 static void _cleanup(void)
194 //_system("udevadm settle", 0);
196 if (!stat(DMDIR CDEVICE_1, &st))
197 _system("dmsetup remove " CDEVICE_1, 0);
199 if (!stat(DMDIR CDEVICE_2, &st))
200 _system("dmsetup remove " CDEVICE_2, 0);
202 if (!stat(DEVICE_EMPTY, &st))
203 _system("dmsetup remove " DEVICE_EMPTY_name, 0);
205 if (!stat(DEVICE_ERROR, &st))
206 _system("dmsetup remove " DEVICE_ERROR_name, 0);
208 if (crypt_loop_device(DEVICE_1))
209 crypt_loop_detach(DEVICE_1);
211 if (crypt_loop_device(DEVICE_2))
212 crypt_loop_detach(DEVICE_2);
214 _system("rm -f " IMAGE_EMPTY, 0);
218 static int _setup(void)
222 _system("dmsetup create " DEVICE_EMPTY_name " --table \"0 10000 zero\"", 1);
223 _system("dmsetup create " DEVICE_ERROR_name " --table \"0 10000 error\"", 1);
225 DEVICE_1 = crypt_loop_get_device();
227 printf("Cannot find free loop device.\n");
230 if (crypt_loop_device(DEVICE_1)) {
231 _system(" [ ! -e " IMAGE1 " ] && bzip2 -dk " IMAGE1 ".bz2", 1);
232 fd = crypt_loop_attach(DEVICE_1, IMAGE1, 0, 0, &ro);
236 DEVICE_2 = crypt_loop_get_device();
238 printf("Cannot find free loop device.\n");
241 if (crypt_loop_device(DEVICE_2)) {
242 _system("dd if=/dev/zero of=" IMAGE_EMPTY " bs=1M count=4", 1);
243 fd = crypt_loop_attach(DEVICE_2, IMAGE_EMPTY, 0, 0, &ro);
249 void check_ok(int status, int line, const char *func)
254 crypt_get_error(buf, sizeof(buf));
255 printf("FAIL line %d [%s]: code %d, %s\n", line, func, status, buf);
261 void check_ko(int status, int line, const char *func)
265 memset(buf, 0, sizeof(buf));
266 crypt_get_error(buf, sizeof(buf));
268 printf("FAIL line %d [%s]: code %d, %s\n", line, func, status, buf);
272 printf(" => errno %d, errmsg: %s\n", status, buf);
275 void check_equal(int line, const char *func)
277 printf("FAIL line %d [%s]: expected equal values differs.\n", line, func);
282 void xlog(const char *msg, const char *tst, const char *func, int line, const char *txt)
286 printf(" [%s,%s:%d] %s [%s]\n", msg, func, line, tst, txt);
288 printf(" [%s,%s:%d] %s\n", msg, func, line, tst);
291 #define OK_(x) do { xlog("(success)", #x, __FUNCTION__, __LINE__, NULL); \
292 check_ok((x), __LINE__, __FUNCTION__); \
294 #define FAIL_(x, y) do { xlog("(fail) ", #x, __FUNCTION__, __LINE__, y); \
295 check_ko((x), __LINE__, __FUNCTION__); \
297 #define EQ_(x, y) do { xlog("(equal) ", #x " == " #y, __FUNCTION__, __LINE__, NULL); \
298 if ((x) != (y)) check_equal(__LINE__, __FUNCTION__); \
301 #define RUN_(x, y) do { printf("%s: %s\n", #x, (y)); x(); } while (0)
304 static void LuksUUID(void)
306 struct crypt_options co = { .icb = &cmd_icb };
308 co.device = DEVICE_EMPTY;
309 EQ_(crypt_luksUUID(&co), -EINVAL);
311 co.device = DEVICE_ERROR;
312 EQ_(crypt_luksUUID(&co), -EINVAL);
315 co.device = DEVICE_1;
316 OK_(crypt_luksUUID(&co));
317 EQ_(strlen(global_log), 37); /* UUID + "\n" */
318 EQ_(strncmp(global_log, DEVICE_1_UUID, strlen(DEVICE_1_UUID)), 0);
322 static void IsLuks(void)
324 struct crypt_options co = { .icb = &cmd_icb };
326 co.device = DEVICE_EMPTY;
327 EQ_(crypt_isLuks(&co), -EINVAL);
329 co.device = DEVICE_ERROR;
330 EQ_(crypt_isLuks(&co), -EINVAL);
332 co.device = DEVICE_1;
333 OK_(crypt_isLuks(&co));
336 static void LuksOpen(void)
338 struct crypt_options co = {
340 //.passphrase = "blabla",
344 OK_(_prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
345 co.key_file = KEYFILE1;
347 co.device = DEVICE_EMPTY;
348 EQ_(crypt_luksOpen(&co), -EINVAL);
350 co.device = DEVICE_ERROR;
351 EQ_(crypt_luksOpen(&co), -EINVAL);
353 co.device = DEVICE_1;
354 OK_(crypt_luksOpen(&co));
355 FAIL_(crypt_luksOpen(&co), "already open");
360 static void query_device(void)
362 struct crypt_options co = {.icb = &cmd_icb };
364 co.name = CDEVICE_WRONG;
365 EQ_(crypt_query_device(&co), 0);
368 EQ_(crypt_query_device(&co), 1);
370 OK_(strncmp(crypt_get_dir(), DMDIR, 11));
371 OK_(strcmp(co.cipher, "aes-cbc-essiv:sha256"));
372 EQ_(co.key_size, 16);
373 EQ_(co.offset, 1032);
374 EQ_(co.flags & CRYPT_FLAG_READONLY, 0);
376 crypt_put_options(&co);
379 static void remove_device(void)
382 struct crypt_options co = {.icb = &cmd_icb };
384 co.name = CDEVICE_WRONG;
385 EQ_(crypt_remove_device(&co), -ENODEV);
387 fd = open(DMDIR CDEVICE_1, O_RDONLY);
389 FAIL_(crypt_remove_device(&co), "device busy");
392 OK_(crypt_remove_device(&co));
395 static void LuksFormat(void)
397 struct crypt_options co = {
401 .cipher = "aes-cbc-essiv:sha256",
404 .iteration_time = 10,
409 OK_(_prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
411 co.new_key_file = KEYFILE1;
412 co.device = DEVICE_ERROR;
413 FAIL_(crypt_luksFormat(&co), "error device");
415 co.device = DEVICE_2;
416 OK_(crypt_luksFormat(&co));
418 co.new_key_file = NULL;
419 co.key_file = KEYFILE1;
421 OK_(crypt_luksOpen(&co));
422 OK_(crypt_remove_device(&co));
426 static void LuksKeyGame(void)
429 struct crypt_options co = {
433 .cipher = "aes-cbc-essiv:sha256",
436 .iteration_time = 10,
441 OK_(_prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
442 OK_(_prepare_keyfile(KEYFILE2, KEY2, strlen(KEY2)));
444 co.new_key_file = KEYFILE1;
445 co.device = DEVICE_2;
447 FAIL_(crypt_luksFormat(&co), "wrong slot #");
449 co.key_slot = 7; // last slot
450 OK_(crypt_luksFormat(&co));
452 co.new_key_file = KEYFILE1;
453 co.key_file = KEYFILE1;
455 FAIL_(crypt_luksAddKey(&co), "wrong slot #");
457 FAIL_(crypt_luksAddKey(&co), "slot already used");
460 OK_(crypt_luksAddKey(&co));
462 co.key_file = KEYFILE2 "blah";
464 FAIL_(crypt_luksAddKey(&co), "keyfile not found");
466 co.new_key_file = KEYFILE2; // key to add
467 co.key_file = KEYFILE1;
469 for (i = 0; i < 6; i++)
470 OK_(crypt_luksAddKey(&co)); //FIXME: EQ_(i)?
472 FAIL_(crypt_luksAddKey(&co), "all slots full");
475 co.new_key_file = KEYFILE1; // key to remove
477 co.key_slot = 8; // should be ignored
478 // only 2 slots should use KEYFILE1
479 OK_(crypt_luksRemoveKey(&co));
480 OK_(crypt_luksRemoveKey(&co));
481 FAIL_(crypt_luksRemoveKey(&co), "no slot with this passphrase");
483 co.new_key_file = KEYFILE2 "blah";
485 FAIL_(crypt_luksRemoveKey(&co), "keyfile not found");
488 co.new_key_file = NULL;
491 FAIL_(crypt_luksKillSlot(&co), "wrong slot #");
493 FAIL_(crypt_luksKillSlot(&co), "slot already wiped");
496 OK_(crypt_luksKillSlot(&co));
501 size_t _get_device_size(const char *device)
503 unsigned long size = 0;
506 fd = open(device, O_RDONLY);
509 (void)ioctl(fd, BLKGETSIZE, &size);
515 void DeviceResizeGame(void)
518 struct crypt_options co = {
522 .cipher = "aes-cbc-plain",
529 orig_size = _get_device_size(DEVICE_2);
531 OK_(_prepare_keyfile(KEYFILE2, KEY2, strlen(KEY2)));
533 co.key_file = KEYFILE2;
535 OK_(crypt_create_device(&co));
536 EQ_(_get_device_size(DMDIR CDEVICE_2), 1000);
539 OK_(crypt_resize_device(&co));
540 EQ_(_get_device_size(DMDIR CDEVICE_2), 2000);
543 OK_(crypt_resize_device(&co));
544 EQ_(_get_device_size(DMDIR CDEVICE_2), (orig_size - 333));
548 co.cipher = "aes-cbc-essiv:sha256";
549 OK_(crypt_update_device(&co));
550 EQ_(_get_device_size(DMDIR CDEVICE_2), (orig_size - 444));
552 memset(&co, 0, sizeof(co));
555 EQ_(crypt_query_device(&co), 1);
556 EQ_(strcmp(co.cipher, "aes-cbc-essiv:sha256"), 0);
557 EQ_(co.key_size, 128 / 8);
560 crypt_put_options(&co);
562 // dangerous switch device still works
563 memset(&co, 0, sizeof(co));
565 co.device = DEVICE_1;
566 co.key_file = KEYFILE2;
567 co.key_size = 128 / 8;
568 co.cipher = "aes-cbc-plain";
571 OK_(crypt_update_device(&co));
573 memset(&co, 0, sizeof(co));
576 EQ_(crypt_query_device(&co), 1);
577 EQ_(strcmp(co.cipher, "aes-cbc-plain"), 0);
578 EQ_(co.key_size, 128 / 8);
581 // This expect lookup returns prefered /dev/loopX
582 EQ_(strcmp(co.device, DEVICE_1), 0);
583 crypt_put_options(&co);
585 memset(&co, 0, sizeof(co));
588 OK_(crypt_remove_device(&co));
595 static void AddDevicePlain(void)
597 struct crypt_device *cd;
598 struct crypt_params_plain params = {
604 char key[128], key2[128], path[128];
606 char *passphrase = PASSPHRASE;
607 char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
608 size_t key_size = strlen(mk_hex) / 2;
609 char *cipher = "aes";
610 char *cipher_mode = "cbc-essiv:sha256";
612 crypt_decode_key(key, mk_hex, key_size);
614 FAIL_(crypt_init(&cd, ""), "empty device string");
616 // default is "plain" hash - no password hash
617 OK_(crypt_init(&cd, DEVICE_1));
618 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, NULL));
619 FAIL_(crypt_activate_by_volume_key(cd, NULL, key, key_size, 0), "cannot verify key with plain");
620 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
621 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
622 // FIXME: this should get key from active device?
623 //OK_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)));
624 //OK_(memcmp(key, key2, key_size));
625 OK_(crypt_deactivate(cd, CDEVICE_1));
628 // Now use hashed password
629 OK_(crypt_init(&cd, DEVICE_1));
630 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
631 FAIL_(crypt_activate_by_passphrase(cd, NULL, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0),
632 "cannot verify passphrase with plain" );
633 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
635 // device status check
636 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
637 snprintf(path, sizeof(path), "%s/%s", crypt_get_dir(), CDEVICE_1);
638 fd = open(path, O_RDONLY);
639 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_BUSY);
640 FAIL_(crypt_deactivate(cd, CDEVICE_1), "Device is busy");
642 OK_(crypt_deactivate(cd, CDEVICE_1));
643 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
645 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
646 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
648 // retrieve volume key check
649 memset(key2, 0, key_size);
652 FAIL_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)), "small buffer");
654 OK_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)));
656 OK_(memcmp(key, key2, key_size));
657 OK_(strcmp(cipher, crypt_get_cipher(cd)));
658 OK_(strcmp(cipher_mode, crypt_get_cipher_mode(cd)));
659 EQ_(key_size, crypt_get_volume_key_size(cd));
660 EQ_(0, crypt_get_data_offset(cd));
661 OK_(crypt_deactivate(cd, CDEVICE_1));
664 OK_(_prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
665 FAIL_(crypt_activate_by_keyfile(cd, NULL, CRYPT_ANY_SLOT, KEYFILE1, 0, 0), "cannot verify key with plain");
666 EQ_(0, crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
667 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
668 OK_(crypt_deactivate(cd, CDEVICE_1));
674 #define CALLBACK_ERROR "calback_error xyz"
675 static int pass_callback_err(const char *msg, char *buf, size_t length, void *usrptr)
677 struct crypt_device *cd = usrptr;
683 crypt_log(cd, CRYPT_LOG_ERROR, CALLBACK_ERROR);
687 static int pass_callback_ok(const char *msg, char *buf, size_t length, void *usrptr)
691 strcpy(buf, PASSPHRASE);
695 static void CallbacksTest(void)
697 struct crypt_device *cd;
698 struct crypt_params_plain params = {
704 size_t key_size = 256 / 8;
705 char *cipher = "aes";
706 char *cipher_mode = "cbc-essiv:sha256";
707 char *passphrase = PASSPHRASE;
709 OK_(crypt_init(&cd, DEVICE_1));
710 crypt_set_log_callback(cd, &new_log, NULL);
711 //crypt_set_log_callback(cd, NULL, NULL);
713 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
715 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
716 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
717 OK_(crypt_deactivate(cd, CDEVICE_1));
720 crypt_set_password_callback(cd, pass_callback_err, cd);
721 FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, NULL, 0, 0), "callback fails");
722 EQ_(strncmp(global_log, CALLBACK_ERROR, strlen(CALLBACK_ERROR)), 0);
724 crypt_set_password_callback(cd, pass_callback_ok, NULL);
725 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, NULL, 0, 0));
726 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
727 OK_(crypt_deactivate(cd, CDEVICE_1));
732 static void UseLuksDevice(void)
734 struct crypt_device *cd;
738 OK_(crypt_init(&cd, DEVICE_1));
739 OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
740 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
741 OK_(crypt_activate_by_passphrase(cd, NULL, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0));
742 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0));
743 FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0), "already open");
744 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
745 OK_(crypt_deactivate(cd, CDEVICE_1));
746 FAIL_(crypt_deactivate(cd, CDEVICE_1), "no such device");
749 OK_(strcmp("aes", crypt_get_cipher(cd)));
750 OK_(strcmp("cbc-essiv:sha256", crypt_get_cipher_mode(cd)));
751 OK_(strcmp(DEVICE_1_UUID, crypt_get_uuid(cd)));
752 EQ_(key_size, crypt_get_volume_key_size(cd));
753 EQ_(1032, crypt_get_data_offset(cd));
755 EQ_(0, crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key, &key_size, KEY1, strlen(KEY1)));
756 OK_(crypt_volume_key_verify(cd, key, key_size));
757 OK_(crypt_activate_by_volume_key(cd, NULL, key, key_size, 0));
758 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
759 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
760 OK_(crypt_deactivate(cd, CDEVICE_1));
763 FAIL_(crypt_volume_key_verify(cd, key, key_size), "key mismatch");
764 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0), "key mismatch");
768 static void SuspendDevice(void)
771 struct crypt_device *cd;
773 OK_(crypt_init(&cd, DEVICE_1));
774 OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
775 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0));
777 suspend_status = crypt_suspend(cd, CDEVICE_1);
778 if (suspend_status == -ENOTSUP) {
779 printf("WARNING: Suspend/Resume not supported, skipping test.\n");
783 FAIL_(crypt_suspend(cd, CDEVICE_1), "already suspended");
785 FAIL_(crypt_resume_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1)-1), "wrong key");
786 OK_(crypt_resume_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1)));
787 FAIL_(crypt_resume_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1)), "not suspended");
789 OK_(_prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
790 OK_(crypt_suspend(cd, CDEVICE_1));
791 FAIL_(crypt_resume_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1 "blah", 0), "wrong keyfile");
792 OK_(crypt_resume_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0));
793 FAIL_(crypt_resume_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0), "not suspended");
796 OK_(crypt_deactivate(cd, CDEVICE_1));
800 static void AddDeviceLuks(void)
802 struct crypt_device *cd;
803 struct crypt_params_luks1 params = {
805 .data_alignment = 2048, // 4M, data offset will be 4096
807 char key[128], key2[128];
809 char *passphrase = "blabla";
810 char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
811 size_t key_size = strlen(mk_hex) / 2;
812 char *cipher = "aes";
813 char *cipher_mode = "cbc-essiv:sha256";
815 crypt_decode_key(key, mk_hex, key_size);
817 OK_(crypt_init(&cd, DEVICE_2));
818 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
820 // even with no keyslots defined it can be activated by volume key
821 OK_(crypt_volume_key_verify(cd, key, key_size));
822 OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, key, key_size, 0));
823 EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
824 OK_(crypt_deactivate(cd, CDEVICE_2));
827 EQ_(7, crypt_keyslot_add_by_volume_key(cd, 7, key, key_size, passphrase, strlen(passphrase)));
828 EQ_(CRYPT_SLOT_ACTIVE_LAST, crypt_keyslot_status(cd, 7));
829 EQ_(7, crypt_activate_by_passphrase(cd, CDEVICE_2, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
830 EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
831 OK_(crypt_deactivate(cd, CDEVICE_2));
833 EQ_(1, crypt_keyslot_add_by_volume_key(cd, 1, key, key_size, KEY1, strlen(KEY1)));
834 OK_(_prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
835 OK_(_prepare_keyfile(KEYFILE2, KEY2, strlen(KEY2)));
836 EQ_(2, crypt_keyslot_add_by_keyfile(cd, 2, KEYFILE1, 0, KEYFILE2, 0));
837 FAIL_(crypt_activate_by_keyfile(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, strlen(KEY2)-1, 0), "key mismatch");
838 EQ_(2, crypt_activate_by_keyfile(cd, NULL, CRYPT_ANY_SLOT, KEYFILE2, 0, 0));
839 EQ_(2, crypt_activate_by_keyfile(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, 0, 0));
840 OK_(crypt_keyslot_destroy(cd, 1));
841 OK_(crypt_keyslot_destroy(cd, 2));
842 OK_(crypt_deactivate(cd, CDEVICE_2));
845 FAIL_(crypt_keyslot_add_by_volume_key(cd, 7, key, key_size, passphrase, strlen(passphrase)), "slot used");
847 FAIL_(crypt_keyslot_add_by_volume_key(cd, 6, key, key_size, passphrase, strlen(passphrase)), "key mismatch");
849 EQ_(6, crypt_keyslot_add_by_volume_key(cd, 6, key, key_size, passphrase, strlen(passphrase)));
850 EQ_(CRYPT_SLOT_ACTIVE, crypt_keyslot_status(cd, 6));
852 FAIL_(crypt_keyslot_destroy(cd, 8), "invalid keyslot");
853 FAIL_(crypt_keyslot_destroy(cd, CRYPT_ANY_SLOT), "invalid keyslot");
854 FAIL_(crypt_keyslot_destroy(cd, 0), "keyslot not used");
855 OK_(crypt_keyslot_destroy(cd, 7));
856 EQ_(CRYPT_SLOT_INACTIVE, crypt_keyslot_status(cd, 7));
857 EQ_(CRYPT_SLOT_ACTIVE_LAST, crypt_keyslot_status(cd, 6));
859 EQ_(6, crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)));
860 OK_(crypt_volume_key_verify(cd, key2, key_size));
862 OK_(memcmp(key, key2, key_size));
863 OK_(strcmp(cipher, crypt_get_cipher(cd)));
864 OK_(strcmp(cipher_mode, crypt_get_cipher_mode(cd)));
865 EQ_(key_size, crypt_get_volume_key_size(cd));
866 EQ_(4096, crypt_get_data_offset(cd));
867 OK_(strcmp(DEVICE_2, crypt_get_device_name(cd)));
870 crypt_set_log_callback(cd, &new_log, NULL);
872 OK_(!(global_lines != 0));
873 crypt_set_log_callback(cd, NULL, NULL);
876 FAIL_(crypt_set_uuid(cd, "blah"), "wrong UUID format");
877 OK_(crypt_set_uuid(cd, DEVICE_TEST_UUID));
878 OK_(strcmp(DEVICE_TEST_UUID, crypt_get_uuid(cd)));
880 FAIL_(crypt_deactivate(cd, CDEVICE_2), "not active");
884 static void UseTempVolumes(void)
886 struct crypt_device *cd;
889 // Tepmporary device without keyslot but with on-disk LUKS header
890 OK_(crypt_init(&cd, DEVICE_2));
891 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "not yet formatted");
892 OK_(crypt_format(cd, CRYPT_LUKS1, "aes", "cbc-essiv:sha256", NULL, NULL, 16, NULL));
893 OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0));
894 EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
897 // Volume key is properly initialised from active device
898 OK_(crypt_init_by_name(&cd, CDEVICE_2));
899 OK_(crypt_deactivate(cd, CDEVICE_2));
900 OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0));
901 OK_(crypt_deactivate(cd, CDEVICE_2));
904 // Dirty checks: device without UUID
905 // we should be able to remove it but not manuipulate with it
906 snprintf(tmp, sizeof(tmp), "dmsetup create %s --table \""
907 "0 100 crypt aes-cbc-essiv:sha256 deadbabedeadbabedeadbabedeadbabe 0 "
908 "%s 2048\"", CDEVICE_2, DEVICE_2);
910 OK_(crypt_init_by_name(&cd, CDEVICE_2));
911 OK_(crypt_deactivate(cd, CDEVICE_2));
912 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "No known device type");
915 // Dirty checks: device with UUID but LUKS header key fingerprint must fail)
916 snprintf(tmp, sizeof(tmp), "dmsetup create %s --table \""
917 "0 100 crypt aes-cbc-essiv:sha256 deadbabedeadbabedeadbabedeadbabe 0 "
918 "%s 2048\" -u CRYPT-LUKS1-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-ctest1",
919 CDEVICE_2, DEVICE_2);
921 OK_(crypt_init_by_name(&cd, CDEVICE_2));
922 OK_(crypt_deactivate(cd, CDEVICE_2));
923 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "wrong volume key");
927 OK_(crypt_init(&cd, DEVICE_2));
928 OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
929 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "volume key is lost");
933 OK_(crypt_init(&cd, DEVICE_2));
934 OK_(crypt_format(cd, CRYPT_PLAIN, "aes", "cbc-essiv:sha256", NULL, NULL, 16, NULL));
935 FAIL_(crypt_activate_by_volume_key(cd, NULL, "xxx", 3, 0), "cannot verify key with plain");
936 FAIL_(crypt_volume_key_verify(cd, "xxx", 3), "cannot verify key with plain");
937 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, "xxx", 3, 0), "wrong key lenght");
938 OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, "volumekeyvolumek", 16, 0));
939 EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
940 OK_(crypt_deactivate(cd, CDEVICE_2));
944 static void HashDevicePlain(void)
946 struct crypt_device *cd;
947 struct crypt_params_plain params = {
954 char *mk_hex, *keystr, key[256];
956 OK_(crypt_init(&cd, DEVICE_1));
957 OK_(crypt_format(cd, CRYPT_PLAIN, "aes", "cbc-essiv:sha256", NULL, NULL, 16, ¶ms));
959 // hash PLAIN, short key
960 OK_(_prepare_keyfile(KEYFILE1, "tooshort", 8));
961 FAIL_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 16, 0), "not enough data in keyfile");
964 // hash PLAIN, exact key
965 // 0 1 2 3 4 5 6 7 8 9 a b c d e f
966 mk_hex = "caffeecaffeecaffeecaffeecaffee88";
968 crypt_decode_key(key, mk_hex, key_size);
969 OK_(_prepare_keyfile(KEYFILE1, key, key_size));
970 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size, 0));
971 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
972 OK_(strcmp(key, mk_hex));
973 OK_(crypt_deactivate(cd, CDEVICE_1));
976 mk_hex = "caffeecaffeecaffeecaffeeca000000";
977 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size - 3, 0));
978 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
979 OK_(strcmp(key, mk_hex));
980 OK_(crypt_deactivate(cd, CDEVICE_1));
984 // hash PLAIN, long key
985 // 0 1 2 3 4 5 6 7 8 9 a b c d e f
986 mk_hex = "caffeecaffeecaffeecaffeecaffee88babebabe";
988 crypt_decode_key(key, mk_hex, key_size);
989 OK_(_prepare_keyfile(KEYFILE1, key, strlen(mk_hex) / 2));
990 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size, 0));
991 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
992 FAIL_(strcmp(key, mk_hex), "only key length used");
993 OK_(strncmp(key, mk_hex, key_size));
994 OK_(crypt_deactivate(cd, CDEVICE_1));
996 // Now without explicit limit
997 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
998 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
999 FAIL_(strcmp(key, mk_hex), "only key length used");
1000 OK_(strncmp(key, mk_hex, key_size));
1001 OK_(crypt_deactivate(cd, CDEVICE_1));
1006 params.hash = "sha256";
1007 OK_(crypt_format(cd, CRYPT_PLAIN, "aes", "cbc-essiv:sha256", NULL, NULL, 16, ¶ms));
1009 // 0 1 2 3 4 5 6 7 8 9 a b c d e f
1010 mk_hex = "c62e4615bd39e222572f3a1bf7c2132e";
1011 keystr = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
1012 key_size = strlen(keystr); // 32
1013 OK_(_prepare_keyfile(KEYFILE1, keystr, strlen(keystr)));
1014 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size, 0));
1015 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1016 OK_(strcmp(key, mk_hex));
1017 OK_(crypt_deactivate(cd, CDEVICE_1));
1019 // Read full keyfile
1020 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
1021 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1022 OK_(strcmp(key, mk_hex));
1023 OK_(crypt_deactivate(cd, CDEVICE_1));
1027 // Limit keyfile read
1028 keystr = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxAAAAAAAA";
1029 OK_(_prepare_keyfile(KEYFILE1, keystr, strlen(keystr)));
1030 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size, 0));
1031 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1032 OK_(strcmp(key, mk_hex));
1033 OK_(crypt_deactivate(cd, CDEVICE_1));
1036 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
1037 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1038 OK_(strcmp(key, "0e49cb34a1dee1df33f6505e4de44a66"));
1039 OK_(crypt_deactivate(cd, CDEVICE_1));
1043 // FIXME: add keyfile="-" tests somehow
1048 // Check that gcrypt is properly initialised in format
1049 static void NonFIPSAlg(void)
1051 struct crypt_device *cd;
1052 struct crypt_params_luks1 params = {0};
1054 size_t key_size = 128;
1055 char *cipher = "aes";
1056 char *cipher_mode = "cbc-essiv:sha256";
1059 OK_(crypt_init(&cd, DEVICE_2));
1060 params.hash = "sha256";
1061 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1063 params.hash = "whirlpool";
1064 ret = crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms);
1066 printf("WARNING: whirlpool not supported, skipping test.\n");
1071 params.hash = "md5";
1072 FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms),
1073 "MD5 unsupported, too short");
1077 int main (int argc, char *argv[])
1081 if (getuid() != 0) {
1082 printf("You must be root to run this test.\n");
1086 for (i = 1; i < argc; i++) {
1087 if (!strcmp("-v", argv[i]) || !strcmp("--verbose", argv[i]))
1089 else if (!strcmp("--debug", argv[i]))
1090 _debug = _verbose = 1;
1097 crypt_set_debug_level(_debug ? CRYPT_DEBUG_ALL : CRYPT_DEBUG_NONE);
1099 RUN_(NonFIPSAlg, "Crypto is properly initialised in format"); //must be the first!
1101 RUN_(LuksUUID, "luksUUID API call");
1102 RUN_(IsLuks, "isLuks API call");
1103 RUN_(LuksOpen, "luksOpen API call");
1104 RUN_(query_device, "crypt_query_device API call");
1105 RUN_(remove_device, "crypt_remove_device API call");
1106 RUN_(LuksFormat, "luksFormat API call");
1107 RUN_(LuksKeyGame, "luksAddKey, RemoveKey, KillSlot API calls");
1108 RUN_(DeviceResizeGame, "regular crypto, resize calls");
1110 RUN_(AddDevicePlain, "plain device API creation exercise");
1111 RUN_(HashDevicePlain, "plain device API hash test");
1112 RUN_(AddDeviceLuks, "Format and use LUKS device");
1113 RUN_(UseLuksDevice, "Use pre-formated LUKS device");
1114 RUN_(SuspendDevice, "Suspend/Resume test");
1115 RUN_(UseTempVolumes, "Format and use temporary encrypted device");
1117 RUN_(CallbacksTest, "API callbacks test");