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 void cmdLineLog(int level, char *msg)
158 strncat(global_log, msg, sizeof(global_log) - strlen(global_log));
162 static void new_log(int level, const char *msg, void *usrptr)
164 cmdLineLog(level, (char*)msg);
167 static void reset_log()
169 memset(global_log, 0, sizeof(global_log));
173 static void _system(const char *command, int warn)
175 if (system(command) < 0 && warn)
176 printf("System command failed: %s", command);
179 static void _cleanup(void)
183 //_system("udevadm settle", 0);
185 if (!stat(DMDIR CDEVICE_1, &st))
186 _system("dmsetup remove " CDEVICE_1, 0);
188 if (!stat(DMDIR CDEVICE_2, &st))
189 _system("dmsetup remove " CDEVICE_2, 0);
191 if (!stat(DEVICE_EMPTY, &st))
192 _system("dmsetup remove " DEVICE_EMPTY_name, 0);
194 if (!stat(DEVICE_ERROR, &st))
195 _system("dmsetup remove " DEVICE_ERROR_name, 0);
197 if (crypt_loop_device(DEVICE_1))
198 crypt_loop_detach(DEVICE_1);
200 if (crypt_loop_device(DEVICE_2))
201 crypt_loop_detach(DEVICE_2);
203 _system("rm -f " IMAGE_EMPTY, 0);
207 static int _setup(void)
211 _system("dmsetup create " DEVICE_EMPTY_name " --table \"0 10000 zero\"", 1);
212 _system("dmsetup create " DEVICE_ERROR_name " --table \"0 10000 error\"", 1);
214 DEVICE_1 = crypt_loop_get_device();
216 printf("Cannot find free loop device.\n");
219 if (crypt_loop_device(DEVICE_1)) {
220 _system(" [ ! -e " IMAGE1 " ] && bzip2 -dk " IMAGE1 ".bz2", 1);
221 fd = crypt_loop_attach(DEVICE_1, IMAGE1, 0, 0, &ro);
225 DEVICE_2 = crypt_loop_get_device();
227 printf("Cannot find free loop device.\n");
230 if (crypt_loop_device(DEVICE_2)) {
231 _system("dd if=/dev/zero of=" IMAGE_EMPTY " bs=1M count=4", 1);
232 fd = crypt_loop_attach(DEVICE_2, IMAGE_EMPTY, 0, 0, &ro);
238 void check_ok(int status, int line, const char *func)
243 crypt_get_error(buf, sizeof(buf));
244 printf("FAIL line %d [%s]: code %d, %s\n", line, func, status, buf);
250 void check_ko(int status, int line, const char *func)
254 memset(buf, 0, sizeof(buf));
255 crypt_get_error(buf, sizeof(buf));
257 printf("FAIL line %d [%s]: code %d, %s\n", line, func, status, buf);
261 printf(" => errno %d, errmsg: %s\n", status, buf);
264 void check_equal(int line, const char *func)
266 printf("FAIL line %d [%s]: expected equal values differs.\n", line, func);
271 void xlog(const char *msg, const char *tst, const char *func, int line, const char *txt)
275 printf(" [%s,%s:%d] %s [%s]\n", msg, func, line, tst, txt);
277 printf(" [%s,%s:%d] %s\n", msg, func, line, tst);
280 #define OK_(x) do { xlog("(success)", #x, __FUNCTION__, __LINE__, NULL); \
281 check_ok((x), __LINE__, __FUNCTION__); \
283 #define FAIL_(x, y) do { xlog("(fail) ", #x, __FUNCTION__, __LINE__, y); \
284 check_ko((x), __LINE__, __FUNCTION__); \
286 #define EQ_(x, y) do { xlog("(equal) ", #x " == " #y, __FUNCTION__, __LINE__, NULL); \
287 if ((x) != (y)) check_equal(__LINE__, __FUNCTION__); \
290 #define RUN_(x, y) do { printf("%s: %s\n", #x, (y)); x(); } while (0)
293 static int yesDialog(char *msg)
298 static struct interface_callbacks cmd_icb = {
299 .yesDialog = yesDialog,
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));
596 static void AddDevicePlain(void)
598 struct crypt_device *cd;
599 struct crypt_params_plain params = {
605 char key[128], key2[128], path[128];
607 char *passphrase = PASSPHRASE;
608 char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
609 size_t key_size = strlen(mk_hex) / 2;
610 char *cipher = "aes";
611 char *cipher_mode = "cbc-essiv:sha256";
613 crypt_decode_key(key, mk_hex, key_size);
615 FAIL_(crypt_init(&cd, ""), "empty device string");
617 // default is "plain" hash - no password hash
618 OK_(crypt_init(&cd, DEVICE_1));
619 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, NULL));
620 FAIL_(crypt_activate_by_volume_key(cd, NULL, key, key_size, 0), "cannot verify key with plain");
621 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
622 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
623 // FIXME: this should get key from active device?
624 //OK_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)));
625 //OK_(memcmp(key, key2, key_size));
626 OK_(crypt_deactivate(cd, CDEVICE_1));
629 // Now use hashed password
630 OK_(crypt_init(&cd, DEVICE_1));
631 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
632 FAIL_(crypt_activate_by_passphrase(cd, NULL, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0),
633 "cannot verify passphrase with plain" );
634 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
636 // device status check
637 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
638 snprintf(path, sizeof(path), "%s/%s", crypt_get_dir(), CDEVICE_1);
639 fd = open(path, O_RDONLY);
640 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_BUSY);
641 FAIL_(crypt_deactivate(cd, CDEVICE_1), "Device is busy");
643 OK_(crypt_deactivate(cd, CDEVICE_1));
644 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
646 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
647 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
649 // retrieve volume key check
650 memset(key2, 0, key_size);
653 FAIL_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)), "small buffer");
655 OK_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)));
657 OK_(memcmp(key, key2, key_size));
658 OK_(strcmp(cipher, crypt_get_cipher(cd)));
659 OK_(strcmp(cipher_mode, crypt_get_cipher_mode(cd)));
660 EQ_(key_size, crypt_get_volume_key_size(cd));
661 EQ_(0, crypt_get_data_offset(cd));
662 OK_(crypt_deactivate(cd, CDEVICE_1));
665 OK_(_prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
666 FAIL_(crypt_activate_by_keyfile(cd, NULL, CRYPT_ANY_SLOT, KEYFILE1, 0, 0), "cannot verify key with plain");
667 EQ_(0, crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
668 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
669 OK_(crypt_deactivate(cd, CDEVICE_1));
675 #define CALLBACK_ERROR "calback_error xyz"
676 static int pass_callback_err(const char *msg, char *buf, size_t length, void *usrptr)
678 struct crypt_device *cd = usrptr;
684 crypt_log(cd, CRYPT_LOG_ERROR, CALLBACK_ERROR);
688 static int pass_callback_ok(const char *msg, char *buf, size_t length, void *usrptr)
692 strcpy(buf, PASSPHRASE);
696 static void CallbacksTest(void)
698 struct crypt_device *cd;
699 struct crypt_params_plain params = {
705 size_t key_size = 256 / 8;
706 char *cipher = "aes";
707 char *cipher_mode = "cbc-essiv:sha256";
708 char *passphrase = PASSPHRASE;
710 OK_(crypt_init(&cd, DEVICE_1));
711 crypt_set_log_callback(cd, &new_log, NULL);
712 //crypt_set_log_callback(cd, NULL, NULL);
714 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
716 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
717 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
718 OK_(crypt_deactivate(cd, CDEVICE_1));
721 crypt_set_password_callback(cd, pass_callback_err, cd);
722 FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, NULL, 0, 0), "callback fails");
723 EQ_(strncmp(global_log, CALLBACK_ERROR, strlen(CALLBACK_ERROR)), 0);
725 crypt_set_password_callback(cd, pass_callback_ok, NULL);
726 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, NULL, 0, 0));
727 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
728 OK_(crypt_deactivate(cd, CDEVICE_1));
733 static void UseLuksDevice(void)
735 struct crypt_device *cd;
739 OK_(crypt_init(&cd, DEVICE_1));
740 OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
741 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
742 OK_(crypt_activate_by_passphrase(cd, NULL, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0));
743 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0));
744 FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0), "already open");
745 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
746 OK_(crypt_deactivate(cd, CDEVICE_1));
747 FAIL_(crypt_deactivate(cd, CDEVICE_1), "no such device");
750 OK_(strcmp("aes", crypt_get_cipher(cd)));
751 OK_(strcmp("cbc-essiv:sha256", crypt_get_cipher_mode(cd)));
752 OK_(strcmp(DEVICE_1_UUID, crypt_get_uuid(cd)));
753 EQ_(key_size, crypt_get_volume_key_size(cd));
754 EQ_(1032, crypt_get_data_offset(cd));
756 EQ_(0, crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key, &key_size, KEY1, strlen(KEY1)));
757 OK_(crypt_volume_key_verify(cd, key, key_size));
758 OK_(crypt_activate_by_volume_key(cd, NULL, key, key_size, 0));
759 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
760 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
761 OK_(crypt_deactivate(cd, CDEVICE_1));
764 FAIL_(crypt_volume_key_verify(cd, key, key_size), "key mismatch");
765 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0), "key mismatch");
769 static void SuspendDevice(void)
772 struct crypt_device *cd;
774 OK_(crypt_init(&cd, DEVICE_1));
775 OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
776 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0));
778 suspend_status = crypt_suspend(cd, CDEVICE_1);
779 if (suspend_status == -ENOTSUP) {
780 printf("WARNING: Suspend/Resume not supported, skipping test.\n");
784 FAIL_(crypt_suspend(cd, CDEVICE_1), "already suspended");
786 FAIL_(crypt_resume_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1)-1), "wrong key");
787 OK_(crypt_resume_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1)));
788 FAIL_(crypt_resume_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1)), "not suspended");
790 OK_(_prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
791 OK_(crypt_suspend(cd, CDEVICE_1));
792 FAIL_(crypt_resume_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1 "blah", 0), "wrong keyfile");
793 OK_(crypt_resume_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0));
794 FAIL_(crypt_resume_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0), "not suspended");
797 OK_(crypt_deactivate(cd, CDEVICE_1));
801 static void AddDeviceLuks(void)
803 struct crypt_device *cd;
804 struct crypt_params_luks1 params = {
806 .data_alignment = 2048, // 4M, data offset will be 4096
808 char key[128], key2[128];
810 char *passphrase = "blabla";
811 char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
812 size_t key_size = strlen(mk_hex) / 2;
813 char *cipher = "aes";
814 char *cipher_mode = "cbc-essiv:sha256";
816 crypt_decode_key(key, mk_hex, key_size);
818 OK_(crypt_init(&cd, DEVICE_2));
819 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
821 // even with no keyslots defined it can be activated by volume key
822 OK_(crypt_volume_key_verify(cd, key, key_size));
823 OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, key, key_size, 0));
824 EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
825 OK_(crypt_deactivate(cd, CDEVICE_2));
828 EQ_(7, crypt_keyslot_add_by_volume_key(cd, 7, key, key_size, passphrase, strlen(passphrase)));
829 EQ_(CRYPT_SLOT_ACTIVE_LAST, crypt_keyslot_status(cd, 7));
830 EQ_(7, crypt_activate_by_passphrase(cd, CDEVICE_2, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
831 EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
832 OK_(crypt_deactivate(cd, CDEVICE_2));
834 EQ_(1, crypt_keyslot_add_by_volume_key(cd, 1, key, key_size, KEY1, strlen(KEY1)));
835 OK_(_prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
836 OK_(_prepare_keyfile(KEYFILE2, KEY2, strlen(KEY2)));
837 EQ_(2, crypt_keyslot_add_by_keyfile(cd, 2, KEYFILE1, 0, KEYFILE2, 0));
838 FAIL_(crypt_activate_by_keyfile(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, strlen(KEY2)-1, 0), "key mismatch");
839 EQ_(2, crypt_activate_by_keyfile(cd, NULL, CRYPT_ANY_SLOT, KEYFILE2, 0, 0));
840 EQ_(2, crypt_activate_by_keyfile(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, 0, 0));
841 OK_(crypt_keyslot_destroy(cd, 1));
842 OK_(crypt_keyslot_destroy(cd, 2));
843 OK_(crypt_deactivate(cd, CDEVICE_2));
846 FAIL_(crypt_keyslot_add_by_volume_key(cd, 7, key, key_size, passphrase, strlen(passphrase)), "slot used");
848 FAIL_(crypt_keyslot_add_by_volume_key(cd, 6, key, key_size, passphrase, strlen(passphrase)), "key mismatch");
850 EQ_(6, crypt_keyslot_add_by_volume_key(cd, 6, key, key_size, passphrase, strlen(passphrase)));
851 EQ_(CRYPT_SLOT_ACTIVE, crypt_keyslot_status(cd, 6));
853 FAIL_(crypt_keyslot_destroy(cd, 8), "invalid keyslot");
854 FAIL_(crypt_keyslot_destroy(cd, CRYPT_ANY_SLOT), "invalid keyslot");
855 FAIL_(crypt_keyslot_destroy(cd, 0), "keyslot not used");
856 OK_(crypt_keyslot_destroy(cd, 7));
857 EQ_(CRYPT_SLOT_INACTIVE, crypt_keyslot_status(cd, 7));
858 EQ_(CRYPT_SLOT_ACTIVE_LAST, crypt_keyslot_status(cd, 6));
860 EQ_(6, crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)));
861 OK_(crypt_volume_key_verify(cd, key2, key_size));
863 OK_(memcmp(key, key2, key_size));
864 OK_(strcmp(cipher, crypt_get_cipher(cd)));
865 OK_(strcmp(cipher_mode, crypt_get_cipher_mode(cd)));
866 EQ_(key_size, crypt_get_volume_key_size(cd));
867 EQ_(4096, crypt_get_data_offset(cd));
868 OK_(strcmp(DEVICE_2, crypt_get_device_name(cd)));
871 crypt_set_log_callback(cd, &new_log, NULL);
873 OK_(!(global_lines != 0));
874 crypt_set_log_callback(cd, NULL, NULL);
877 FAIL_(crypt_set_uuid(cd, "blah"), "wrong UUID format");
878 OK_(crypt_set_uuid(cd, DEVICE_TEST_UUID));
879 OK_(strcmp(DEVICE_TEST_UUID, crypt_get_uuid(cd)));
881 FAIL_(crypt_deactivate(cd, CDEVICE_2), "not active");
885 static void UseTempVolumes(void)
887 struct crypt_device *cd;
890 // Tepmporary device without keyslot but with on-disk LUKS header
891 OK_(crypt_init(&cd, DEVICE_2));
892 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "not yet formatted");
893 OK_(crypt_format(cd, CRYPT_LUKS1, "aes", "cbc-essiv:sha256", NULL, NULL, 16, NULL));
894 OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0));
895 EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
898 // Volume key is properly initialised from active device
899 OK_(crypt_init_by_name(&cd, CDEVICE_2));
900 OK_(crypt_deactivate(cd, CDEVICE_2));
901 OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0));
902 OK_(crypt_deactivate(cd, CDEVICE_2));
905 // Dirty checks: device without UUID
906 // we should be able to remove it but not manuipulate with it
907 snprintf(tmp, sizeof(tmp), "dmsetup create %s --table \""
908 "0 100 crypt aes-cbc-essiv:sha256 deadbabedeadbabedeadbabedeadbabe 0 "
909 "%s 2048\"", CDEVICE_2, DEVICE_2);
911 OK_(crypt_init_by_name(&cd, CDEVICE_2));
912 OK_(crypt_deactivate(cd, CDEVICE_2));
913 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "No known device type");
916 // Dirty checks: device with UUID but LUKS header key fingerprint must fail)
917 snprintf(tmp, sizeof(tmp), "dmsetup create %s --table \""
918 "0 100 crypt aes-cbc-essiv:sha256 deadbabedeadbabedeadbabedeadbabe 0 "
919 "%s 2048\" -u CRYPT-LUKS1-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-ctest1",
920 CDEVICE_2, DEVICE_2);
922 OK_(crypt_init_by_name(&cd, CDEVICE_2));
923 OK_(crypt_deactivate(cd, CDEVICE_2));
924 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "wrong volume key");
928 OK_(crypt_init(&cd, DEVICE_2));
929 OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
930 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "volume key is lost");
934 OK_(crypt_init(&cd, DEVICE_2));
935 OK_(crypt_format(cd, CRYPT_PLAIN, "aes", "cbc-essiv:sha256", NULL, NULL, 16, NULL));
936 FAIL_(crypt_activate_by_volume_key(cd, NULL, "xxx", 3, 0), "cannot verify key with plain");
937 FAIL_(crypt_volume_key_verify(cd, "xxx", 3), "cannot verify key with plain");
938 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, "xxx", 3, 0), "wrong key lenght");
939 OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, "volumekeyvolumek", 16, 0));
940 EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
941 OK_(crypt_deactivate(cd, CDEVICE_2));
945 static void HashDevicePlain(void)
947 struct crypt_device *cd;
948 struct crypt_params_plain params = {
955 char *mk_hex, *keystr, key[256];
957 OK_(crypt_init(&cd, DEVICE_1));
958 OK_(crypt_format(cd, CRYPT_PLAIN, "aes", "cbc-essiv:sha256", NULL, NULL, 16, ¶ms));
960 // hash PLAIN, short key
961 OK_(_prepare_keyfile(KEYFILE1, "tooshort", 8));
962 FAIL_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 16, 0), "not enough data in keyfile");
965 // hash PLAIN, exact key
966 // 0 1 2 3 4 5 6 7 8 9 a b c d e f
967 mk_hex = "caffeecaffeecaffeecaffeecaffee88";
969 crypt_decode_key(key, mk_hex, key_size);
970 OK_(_prepare_keyfile(KEYFILE1, key, key_size));
971 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size, 0));
972 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
973 OK_(strcmp(key, mk_hex));
974 OK_(crypt_deactivate(cd, CDEVICE_1));
977 mk_hex = "caffeecaffeecaffeecaffeeca000000";
978 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size - 3, 0));
979 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
980 OK_(strcmp(key, mk_hex));
981 OK_(crypt_deactivate(cd, CDEVICE_1));
985 // hash PLAIN, long key
986 // 0 1 2 3 4 5 6 7 8 9 a b c d e f
987 mk_hex = "caffeecaffeecaffeecaffeecaffee88babebabe";
989 crypt_decode_key(key, mk_hex, key_size);
990 OK_(_prepare_keyfile(KEYFILE1, key, strlen(mk_hex) / 2));
991 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size, 0));
992 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
993 FAIL_(strcmp(key, mk_hex), "only key length used");
994 OK_(strncmp(key, mk_hex, key_size));
995 OK_(crypt_deactivate(cd, CDEVICE_1));
997 // Now without explicit limit
998 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
999 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1000 FAIL_(strcmp(key, mk_hex), "only key length used");
1001 OK_(strncmp(key, mk_hex, key_size));
1002 OK_(crypt_deactivate(cd, CDEVICE_1));
1007 params.hash = "sha256";
1008 OK_(crypt_format(cd, CRYPT_PLAIN, "aes", "cbc-essiv:sha256", NULL, NULL, 16, ¶ms));
1010 // 0 1 2 3 4 5 6 7 8 9 a b c d e f
1011 mk_hex = "c62e4615bd39e222572f3a1bf7c2132e";
1012 keystr = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
1013 key_size = strlen(keystr); // 32
1014 OK_(_prepare_keyfile(KEYFILE1, keystr, strlen(keystr)));
1015 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size, 0));
1016 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1017 OK_(strcmp(key, mk_hex));
1018 OK_(crypt_deactivate(cd, CDEVICE_1));
1020 // Read full keyfile
1021 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
1022 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1023 OK_(strcmp(key, mk_hex));
1024 OK_(crypt_deactivate(cd, CDEVICE_1));
1028 // Limit keyfile read
1029 keystr = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxAAAAAAAA";
1030 OK_(_prepare_keyfile(KEYFILE1, keystr, strlen(keystr)));
1031 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size, 0));
1032 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1033 OK_(strcmp(key, mk_hex));
1034 OK_(crypt_deactivate(cd, CDEVICE_1));
1037 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
1038 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1039 OK_(strcmp(key, "0e49cb34a1dee1df33f6505e4de44a66"));
1040 OK_(crypt_deactivate(cd, CDEVICE_1));
1044 // FIXME: add keyfile="-" tests somehow
1049 // Check that gcrypt is properly initialised in format
1050 static void NonFIPSAlg(void)
1052 struct crypt_device *cd;
1053 struct crypt_params_luks1 params = {0};
1055 size_t key_size = 128;
1056 char *cipher = "aes";
1057 char *cipher_mode = "cbc-essiv:sha256";
1060 OK_(crypt_init(&cd, DEVICE_2));
1061 params.hash = "sha256";
1062 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1064 params.hash = "whirlpool";
1065 ret = crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms);
1067 printf("WARNING: whirlpool not supported, skipping test.\n");
1072 params.hash = "md5";
1073 FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms),
1074 "MD5 unsupported, too short");
1078 int main (int argc, char *argv[])
1082 if (getuid() != 0) {
1083 printf("You must be root to run this test.\n");
1087 for (i = 1; i < argc; i++) {
1088 if (!strcmp("-v", argv[i]) || !strcmp("--verbose", argv[i]))
1090 else if (!strcmp("--debug", argv[i]))
1091 _debug = _verbose = 1;
1098 crypt_set_debug_level(_debug ? CRYPT_DEBUG_ALL : CRYPT_DEBUG_NONE);
1100 RUN_(NonFIPSAlg, "Crypto is properly initialised in format"); //must be the first!
1102 RUN_(LuksUUID, "luksUUID API call");
1103 RUN_(IsLuks, "isLuks API call");
1104 RUN_(LuksOpen, "luksOpen API call");
1105 RUN_(query_device, "crypt_query_device API call");
1106 RUN_(remove_device, "crypt_remove_device API call");
1107 RUN_(LuksFormat, "luksFormat API call");
1108 RUN_(LuksKeyGame, "luksAddKey, RemoveKey, KillSlot API calls");
1109 RUN_(DeviceResizeGame, "regular crypto, resize calls");
1111 RUN_(AddDevicePlain, "plain device API creation exercise");
1112 RUN_(HashDevicePlain, "plain device API hash test");
1113 RUN_(AddDeviceLuks, "Format and use LUKS device");
1114 RUN_(UseLuksDevice, "Use pre-formated LUKS device");
1115 RUN_(SuspendDevice, "Suspend/Resume test");
1116 RUN_(UseTempVolumes, "Format and use temporary encrypted device");
1118 RUN_(CallbacksTest, "API callbacks test");