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>
31 #include "libcryptsetup.h"
33 #define DMDIR "/dev/mapper/"
35 #define DEVICE_1 "/dev/loop5"
36 #define DEVICE_1_UUID "28632274-8c8a-493f-835b-da802e1c576b"
37 #define DEVICE_2 "/dev/loop6"
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 int gcrypt_compatible = 0;
69 static int _prepare_keyfile(const char *name, const char *passphrase)
73 fd = open(name, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR);
75 r = write(fd, passphrase, strlen(passphrase));
80 return r == strlen(passphrase) ? 0 : 1;
83 static void _remove_keyfiles(void)
89 // Decode key from its hex representation
90 static int crypt_decode_key(char *key, char *hex, unsigned int size)
98 for (i = 0; i < size; i++) {
102 key[i] = (unsigned char)strtoul(buffer, &endp, 16);
104 if (endp != &buffer[2])
114 static int yesDialog(char *msg)
119 static void cmdLineLog(int level, char *msg)
121 strncat(global_log, msg, sizeof(global_log) - strlen(global_log));
125 static void new_log(int level, const char *msg, void *usrptr)
127 cmdLineLog(level, (char*)msg);
131 static void reset_log()
133 memset(global_log, 0, sizeof(global_log));
137 static struct interface_callbacks cmd_icb = {
138 .yesDialog = yesDialog,
142 static void _cleanup(void)
147 //r = system("udevadm settle");
149 if (!stat(DMDIR CDEVICE_1, &st))
150 r = system("dmsetup remove " CDEVICE_1);
152 if (!stat(DMDIR CDEVICE_2, &st))
153 r = system("dmsetup remove " CDEVICE_2);
155 if (!stat(DEVICE_EMPTY, &st))
156 r = system("dmsetup remove " DEVICE_EMPTY_name);
158 if (!stat(DEVICE_ERROR, &st))
159 r = system("dmsetup remove " DEVICE_ERROR_name);
161 if (!strncmp("/dev/loop", DEVICE_1, 9))
162 r = system("losetup -d " DEVICE_1);
164 if (!strncmp("/dev/loop", DEVICE_2, 9))
165 r = system("losetup -d " DEVICE_2);
167 r = system("rm -f " IMAGE_EMPTY);
171 static void _setup(void)
175 r = system("dmsetup create " DEVICE_EMPTY_name " --table \"0 10000 zero\"");
176 r = system("dmsetup create " DEVICE_ERROR_name " --table \"0 10000 error\"");
177 if (!strncmp("/dev/loop", DEVICE_1, 9)) {
178 r = system(" [ ! -e " IMAGE1 " ] && bzip2 -dk " IMAGE1 ".bz2");
179 r = system("losetup " DEVICE_1 " " IMAGE1);
181 if (!strncmp("/dev/loop", DEVICE_2, 9)) {
182 r = system("dd if=/dev/zero of=" IMAGE_EMPTY " bs=1M count=4");
183 r = system("losetup " DEVICE_2 " " IMAGE_EMPTY);
188 void check_ok(int status, int line, const char *func)
193 crypt_get_error(buf, sizeof(buf));
194 printf("FAIL line %d [%s]: code %d, %s\n", line, func, status, buf);
200 void check_ko(int status, int line, const char *func)
204 memset(buf, 0, sizeof(buf));
205 crypt_get_error(buf, sizeof(buf));
207 printf("FAIL line %d [%s]: code %d, %s\n", line, func, status, buf);
211 printf(" => errno %d, errmsg: %s\n", status, buf);
214 void check_equal(int line, const char *func)
216 printf("FAIL line %d [%s]: expected equal values differs.\n", line, func);
221 void xlog(const char *msg, const char *tst, const char *func, int line, const char *txt)
225 printf(" [%s,%s:%d] %s [%s]\n", msg, func, line, tst, txt);
227 printf(" [%s,%s:%d] %s\n", msg, func, line, tst);
230 #define OK_(x) do { xlog("(success)", #x, __FUNCTION__, __LINE__, NULL); \
231 check_ok((x), __LINE__, __FUNCTION__); \
233 #define FAIL_(x, y) do { xlog("(fail) ", #x, __FUNCTION__, __LINE__, y); \
234 check_ko((x), __LINE__, __FUNCTION__); \
236 #define EQ_(x, y) do { xlog("(equal) ", #x " == " #y, __FUNCTION__, __LINE__, NULL); \
237 if ((x) != (y)) check_equal(__LINE__, __FUNCTION__); \
240 #define RUN_(x, y) do { printf("%s: %s\n", #x, (y)); x(); } while (0)
243 static void LuksUUID(void)
245 struct crypt_options co = { .icb = &cmd_icb };
247 co.device = DEVICE_EMPTY;
248 EQ_(crypt_luksUUID(&co), -EINVAL);
250 co.device = DEVICE_ERROR;
251 EQ_(crypt_luksUUID(&co), -EINVAL);
254 co.device = DEVICE_1;
255 OK_(crypt_luksUUID(&co));
256 EQ_(strlen(global_log), 37); /* UUID + "\n" */
257 EQ_(strncmp(global_log, DEVICE_1_UUID, strlen(DEVICE_1_UUID)), 0);
261 static void IsLuks(void)
263 struct crypt_options co = { .icb = &cmd_icb };
265 co.device = DEVICE_EMPTY;
266 EQ_(crypt_isLuks(&co), -EINVAL);
268 co.device = DEVICE_ERROR;
269 EQ_(crypt_isLuks(&co), -EINVAL);
271 co.device = DEVICE_1;
272 OK_(crypt_isLuks(&co));
275 static void LuksOpen(void)
277 struct crypt_options co = {
279 //.passphrase = "blabla",
283 OK_(_prepare_keyfile(KEYFILE1, KEY1));
284 co.key_file = KEYFILE1;
286 co.device = DEVICE_EMPTY;
287 EQ_(crypt_luksOpen(&co), -EINVAL);
289 co.device = DEVICE_ERROR;
290 EQ_(crypt_luksOpen(&co), -EINVAL);
292 co.device = DEVICE_1;
293 OK_(crypt_luksOpen(&co));
294 FAIL_(crypt_luksOpen(&co), "already open");
299 static void query_device(void)
301 struct crypt_options co = {.icb = &cmd_icb };
303 co.name = CDEVICE_WRONG;
304 EQ_(crypt_query_device(&co), 0);
307 EQ_(crypt_query_device(&co), 1);
309 OK_(strncmp(crypt_get_dir(), DMDIR, 11));
310 OK_(strcmp(co.cipher, "aes-cbc-essiv:sha256"));
311 EQ_(co.key_size, 16);
312 EQ_(co.offset, 1032);
313 EQ_(co.flags & CRYPT_FLAG_READONLY, 0);
315 crypt_put_options(&co);
318 static void remove_device(void)
321 struct crypt_options co = {.icb = &cmd_icb };
323 co.name = CDEVICE_WRONG;
324 EQ_(crypt_remove_device(&co), -ENODEV);
326 fd = open(DMDIR CDEVICE_1, O_RDONLY);
328 FAIL_(crypt_remove_device(&co), "device busy");
331 OK_(crypt_remove_device(&co));
334 static void LuksFormat(void)
336 struct crypt_options co = {
340 .cipher = "aes-cbc-essiv:sha256",
343 .iteration_time = 10,
348 OK_(_prepare_keyfile(KEYFILE1, KEY1));
350 co.new_key_file = KEYFILE1;
351 co.device = DEVICE_ERROR;
352 FAIL_(crypt_luksFormat(&co), "error device");
354 co.device = DEVICE_2;
355 OK_(crypt_luksFormat(&co));
357 co.new_key_file = NULL;
358 co.key_file = KEYFILE1;
360 OK_(crypt_luksOpen(&co));
361 OK_(crypt_remove_device(&co));
365 static void LuksKeyGame(void)
368 struct crypt_options co = {
372 .cipher = "aes-cbc-essiv:sha256",
375 .iteration_time = 10,
380 OK_(_prepare_keyfile(KEYFILE1, KEY1));
381 OK_(_prepare_keyfile(KEYFILE2, KEY2));
383 co.new_key_file = KEYFILE1;
384 co.device = DEVICE_2;
386 FAIL_(crypt_luksFormat(&co), "wrong slot #");
388 co.key_slot = 7; // last slot
389 OK_(crypt_luksFormat(&co));
391 co.new_key_file = KEYFILE1;
392 co.key_file = KEYFILE1;
394 FAIL_(crypt_luksAddKey(&co), "wrong slot #");
396 FAIL_(crypt_luksAddKey(&co), "slot already used");
399 OK_(crypt_luksAddKey(&co));
401 co.key_file = KEYFILE2 "blah";
403 FAIL_(crypt_luksAddKey(&co), "keyfile not found");
405 co.new_key_file = KEYFILE2; // key to add
406 co.key_file = KEYFILE1;
408 for (i = 0; i < 6; i++)
409 OK_(crypt_luksAddKey(&co)); //FIXME: EQ_(i)?
411 FAIL_(crypt_luksAddKey(&co), "all slots full");
414 co.new_key_file = KEYFILE1; // key to remove
416 co.key_slot = 8; // should be ignored
417 // only 2 slots should use KEYFILE1
418 OK_(crypt_luksRemoveKey(&co));
419 OK_(crypt_luksRemoveKey(&co));
420 FAIL_(crypt_luksRemoveKey(&co), "no slot with this passphrase");
422 co.new_key_file = KEYFILE2 "blah";
424 FAIL_(crypt_luksRemoveKey(&co), "keyfile not found");
427 co.new_key_file = NULL;
430 FAIL_(crypt_luksKillSlot(&co), "wrong slot #");
432 FAIL_(crypt_luksKillSlot(&co), "slot already wiped");
435 OK_(crypt_luksKillSlot(&co));
440 size_t _get_device_size(const char *device)
442 unsigned long size = 0;
445 fd = open(device, O_RDONLY);
448 (void)ioctl(fd, BLKGETSIZE, &size);
454 void DeviceResizeGame(void)
457 struct crypt_options co = {
461 .cipher = "aes-cbc-plain",
468 orig_size = _get_device_size(DEVICE_2);
470 OK_(_prepare_keyfile(KEYFILE2, KEY2));
472 co.key_file = KEYFILE2;
474 OK_(crypt_create_device(&co));
475 EQ_(_get_device_size(DMDIR CDEVICE_2), 1000);
478 OK_(crypt_resize_device(&co));
479 EQ_(_get_device_size(DMDIR CDEVICE_2), 2000);
482 OK_(crypt_resize_device(&co));
483 EQ_(_get_device_size(DMDIR CDEVICE_2), (orig_size - 333));
487 co.cipher = "aes-cbc-essiv:sha256";
488 OK_(crypt_update_device(&co));
489 EQ_(_get_device_size(DMDIR CDEVICE_2), (orig_size - 444));
491 memset(&co, 0, sizeof(co));
494 EQ_(crypt_query_device(&co), 1);
495 EQ_(strcmp(co.cipher, "aes-cbc-essiv:sha256"), 0);
496 EQ_(co.key_size, 128 / 8);
499 crypt_put_options(&co);
501 // dangerous switch device still works
502 memset(&co, 0, sizeof(co));
504 co.device = DEVICE_1;
505 co.key_file = KEYFILE2;
506 co.key_size = 128 / 8;
507 co.cipher = "aes-cbc-plain";
510 OK_(crypt_update_device(&co));
512 memset(&co, 0, sizeof(co));
515 EQ_(crypt_query_device(&co), 1);
516 EQ_(strcmp(co.cipher, "aes-cbc-plain"), 0);
517 EQ_(co.key_size, 128 / 8);
520 // This expect lookup returns prefered /dev/loopX
521 EQ_(strcmp(co.device, DEVICE_1), 0);
522 crypt_put_options(&co);
524 memset(&co, 0, sizeof(co));
527 OK_(crypt_remove_device(&co));
534 static void AddDevicePlain(void)
536 struct crypt_device *cd;
537 struct crypt_params_plain params = {
543 char key[128], key2[128], path[128];
545 char *passphrase = PASSPHRASE;
546 char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
547 size_t key_size = strlen(mk_hex) / 2;
548 char *cipher = "aes";
549 char *cipher_mode = "cbc-essiv:sha256";
551 crypt_decode_key(key, mk_hex, key_size);
553 FAIL_(crypt_init(&cd, ""), "empty device string");
555 // default is "plain" hash - no password hash
556 OK_(crypt_init(&cd, DEVICE_1));
557 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, NULL));
558 FAIL_(crypt_activate_by_volume_key(cd, NULL, key, key_size, 0), "cannot verify key with plain");
559 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
560 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
561 // FIXME: this should get key from active device?
562 //OK_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)));
563 //OK_(memcmp(key, key2, key_size));
564 OK_(crypt_deactivate(cd, CDEVICE_1));
567 // Now use hashed password
568 OK_(crypt_init(&cd, DEVICE_1));
569 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
570 FAIL_(crypt_activate_by_passphrase(cd, NULL, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0),
571 "cannot verify passphrase with plain" );
572 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
574 // device status check
575 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
576 snprintf(path, sizeof(path), "%s/%s", crypt_get_dir(), CDEVICE_1);
577 fd = open(path, O_RDONLY);
578 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_BUSY);
579 FAIL_(crypt_deactivate(cd, CDEVICE_1), "Device is busy");
581 OK_(crypt_deactivate(cd, CDEVICE_1));
582 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
584 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
585 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
587 // retrieve volume key check
588 memset(key2, 0, key_size);
591 FAIL_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)), "small buffer");
593 OK_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)));
595 OK_(memcmp(key, key2, key_size));
596 OK_(strcmp(cipher, crypt_get_cipher(cd)));
597 OK_(strcmp(cipher_mode, crypt_get_cipher_mode(cd)));
598 EQ_(key_size, crypt_get_volume_key_size(cd));
599 EQ_(0, crypt_get_data_offset(cd));
600 OK_(crypt_deactivate(cd, CDEVICE_1));
604 #define CALLBACK_ERROR "calback_error xyz"
605 static int pass_callback_err(const char *msg, char *buf, size_t length, void *usrptr)
607 struct crypt_device *cd = usrptr;
613 crypt_log(cd, CRYPT_LOG_ERROR, CALLBACK_ERROR);
617 static int pass_callback_ok(const char *msg, char *buf, size_t length, void *usrptr)
621 strcpy(buf, PASSPHRASE);
625 static void CallbacksTest(void)
627 struct crypt_device *cd;
628 struct crypt_params_plain params = {
634 size_t key_size = 256 / 8;
635 char *cipher = "aes";
636 char *cipher_mode = "cbc-essiv:sha256";
637 char *passphrase = PASSPHRASE;
639 OK_(crypt_init(&cd, DEVICE_1));
640 crypt_set_log_callback(cd, &new_log, NULL);
641 //crypt_set_log_callback(cd, NULL, NULL);
643 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
645 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
646 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
647 OK_(crypt_deactivate(cd, CDEVICE_1));
650 crypt_set_password_callback(cd, pass_callback_err, cd);
651 FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, NULL, 0, 0), "callback fails");
652 EQ_(strncmp(global_log, CALLBACK_ERROR, strlen(CALLBACK_ERROR)), 0);
654 crypt_set_password_callback(cd, pass_callback_ok, NULL);
655 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, NULL, 0, 0));
656 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
657 OK_(crypt_deactivate(cd, CDEVICE_1));
662 static void UseLuksDevice(void)
664 struct crypt_device *cd;
668 OK_(crypt_init(&cd, DEVICE_1));
669 OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
670 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
671 OK_(crypt_activate_by_passphrase(cd, NULL, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0));
672 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0));
673 FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0), "already open");
674 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
675 OK_(crypt_deactivate(cd, CDEVICE_1));
676 FAIL_(crypt_deactivate(cd, CDEVICE_1), "no such device");
679 OK_(strcmp("aes", crypt_get_cipher(cd)));
680 OK_(strcmp("cbc-essiv:sha256", crypt_get_cipher_mode(cd)));
681 OK_(strcmp(DEVICE_1_UUID, crypt_get_uuid(cd)));
682 EQ_(key_size, crypt_get_volume_key_size(cd));
683 EQ_(1032, crypt_get_data_offset(cd));
685 EQ_(0, crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key, &key_size, KEY1, strlen(KEY1)));
686 OK_(crypt_volume_key_verify(cd, key, key_size));
687 OK_(crypt_activate_by_volume_key(cd, NULL, key, key_size, 0));
688 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
689 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
690 OK_(crypt_deactivate(cd, CDEVICE_1));
693 FAIL_(crypt_volume_key_verify(cd, key, key_size), "key mismatch");
694 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0), "key mismatch");
698 static void SuspendDevice(void)
701 struct crypt_device *cd;
703 OK_(crypt_init(&cd, DEVICE_1));
704 OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
705 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0));
707 suspend_status = crypt_suspend(cd, CDEVICE_1);
708 if (suspend_status == -ENOTSUP) {
709 printf("WARNING: Suspend/Resume not supported, skipping test.\n");
713 FAIL_(crypt_suspend(cd, CDEVICE_1), "already suspended");
715 FAIL_(crypt_resume_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1)-1), "wrong key");
716 OK_(crypt_resume_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1)));
717 FAIL_(crypt_resume_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1)), "not suspended");
719 OK_(_prepare_keyfile(KEYFILE1, KEY1));
720 OK_(crypt_suspend(cd, CDEVICE_1));
721 FAIL_(crypt_resume_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1 "blah", 0), "wrong keyfile");
722 OK_(crypt_resume_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0));
723 FAIL_(crypt_resume_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0), "not suspended");
726 OK_(crypt_deactivate(cd, CDEVICE_1));
730 static void AddDeviceLuks(void)
732 struct crypt_device *cd;
733 struct crypt_params_luks1 params = {
735 .data_alignment = 2048, // 4M, data offset will be 4096
737 char key[128], key2[128];
739 char *passphrase = "blabla";
740 char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
741 size_t key_size = strlen(mk_hex) / 2;
742 char *cipher = "aes";
743 char *cipher_mode = "cbc-essiv:sha256";
745 crypt_decode_key(key, mk_hex, key_size);
747 OK_(crypt_init(&cd, DEVICE_2));
748 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
750 // even with no keyslots defined it can be activated by volume key
751 OK_(crypt_volume_key_verify(cd, key, key_size));
752 OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, key, key_size, 0));
753 EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
754 OK_(crypt_deactivate(cd, CDEVICE_2));
757 EQ_(7, crypt_keyslot_add_by_volume_key(cd, 7, key, key_size, passphrase, strlen(passphrase)));
758 EQ_(CRYPT_SLOT_ACTIVE_LAST, crypt_keyslot_status(cd, 7));
759 EQ_(7, crypt_activate_by_passphrase(cd, CDEVICE_2, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
760 EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
761 OK_(crypt_deactivate(cd, CDEVICE_2));
763 EQ_(1, crypt_keyslot_add_by_volume_key(cd, 1, key, key_size, KEY1, strlen(KEY1)));
764 OK_(_prepare_keyfile(KEYFILE1, KEY1));
765 OK_(_prepare_keyfile(KEYFILE2, KEY2));
766 EQ_(2, crypt_keyslot_add_by_keyfile(cd, 2, KEYFILE1, 0, KEYFILE2, 0));
767 FAIL_(crypt_activate_by_keyfile(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, strlen(KEY2)-1, 0), "key mismatch");
768 EQ_(2, crypt_activate_by_keyfile(cd, NULL, CRYPT_ANY_SLOT, KEYFILE2, 0, 0));
769 EQ_(2, crypt_activate_by_keyfile(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, 0, 0));
770 OK_(crypt_keyslot_destroy(cd, 1));
771 OK_(crypt_keyslot_destroy(cd, 2));
772 OK_(crypt_deactivate(cd, CDEVICE_2));
775 FAIL_(crypt_keyslot_add_by_volume_key(cd, 7, key, key_size, passphrase, strlen(passphrase)), "slot used");
777 FAIL_(crypt_keyslot_add_by_volume_key(cd, 6, key, key_size, passphrase, strlen(passphrase)), "key mismatch");
779 EQ_(6, crypt_keyslot_add_by_volume_key(cd, 6, key, key_size, passphrase, strlen(passphrase)));
780 EQ_(CRYPT_SLOT_ACTIVE, crypt_keyslot_status(cd, 6));
782 FAIL_(crypt_keyslot_destroy(cd, 8), "invalid keyslot");
783 FAIL_(crypt_keyslot_destroy(cd, CRYPT_ANY_SLOT), "invalid keyslot");
784 FAIL_(crypt_keyslot_destroy(cd, 0), "keyslot not used");
785 OK_(crypt_keyslot_destroy(cd, 7));
786 EQ_(CRYPT_SLOT_INACTIVE, crypt_keyslot_status(cd, 7));
787 EQ_(CRYPT_SLOT_ACTIVE_LAST, crypt_keyslot_status(cd, 6));
789 EQ_(6, crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)));
790 OK_(crypt_volume_key_verify(cd, key2, key_size));
792 OK_(memcmp(key, key2, key_size));
793 OK_(strcmp(cipher, crypt_get_cipher(cd)));
794 OK_(strcmp(cipher_mode, crypt_get_cipher_mode(cd)));
795 EQ_(key_size, crypt_get_volume_key_size(cd));
796 EQ_(4096, crypt_get_data_offset(cd));
797 OK_(strcmp(DEVICE_2, crypt_get_device_name(cd)));
800 crypt_set_log_callback(cd, &new_log, NULL);
802 OK_(!(global_lines != 0));
803 crypt_set_log_callback(cd, NULL, NULL);
806 FAIL_(crypt_set_uuid(cd, "blah"), "wrong UUID format");
807 OK_(crypt_set_uuid(cd, DEVICE_TEST_UUID));
808 OK_(strcmp(DEVICE_TEST_UUID, crypt_get_uuid(cd)));
810 FAIL_(crypt_deactivate(cd, CDEVICE_2), "not active");
814 static void UseTempVolumes(void)
816 struct crypt_device *cd;
817 struct crypt_params_plain params = {
823 // Tepmporary device without keyslot but with on-disk LUKS header
824 OK_(crypt_init(&cd, DEVICE_2));
825 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "not yet formatted");
826 OK_(crypt_format(cd, CRYPT_LUKS1, "aes", "cbc-essiv:sha256", NULL, NULL, 16, NULL));
827 OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0));
828 EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
831 // Volume key is properly initialised from active device
832 OK_(crypt_init_by_name(&cd, CDEVICE_2));
833 OK_(crypt_deactivate(cd, CDEVICE_2));
834 OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0));
835 OK_(crypt_deactivate(cd, CDEVICE_2));
838 // Dirty checks: device without UUID
839 // we should be able to remove it but not manuipulate with it
840 system("dmsetup create " CDEVICE_2 " --table \""
841 "0 100 crypt aes-cbc-essiv:sha256 deadbabedeadbabedeadbabedeadbabe 0 "
843 OK_(crypt_init_by_name(&cd, CDEVICE_2));
844 OK_(crypt_deactivate(cd, CDEVICE_2));
845 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "No known device type");
848 // Dirty checks: device with UUID but LUKS header key fingerprint must fail)
849 system("dmsetup create " CDEVICE_2 " --table \""
850 "0 100 crypt aes-cbc-essiv:sha256 deadbabedeadbabedeadbabedeadbabe 0 "
852 "-u CRYPT-LUKS1-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-ctest1");
853 OK_(crypt_init_by_name(&cd, CDEVICE_2));
854 OK_(crypt_deactivate(cd, CDEVICE_2));
855 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "wrong volume key");
859 OK_(crypt_init(&cd, DEVICE_2));
860 OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
861 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "volume key is lost");
865 OK_(crypt_init(&cd, DEVICE_2));
866 OK_(crypt_format(cd, CRYPT_PLAIN, "aes", "cbc-essiv:sha256", NULL, NULL, 16, NULL));
867 FAIL_(crypt_activate_by_volume_key(cd, NULL, "xxx", 3, 0), "cannot verify key with plain");
868 FAIL_(crypt_volume_key_verify(cd, "xxx", 3), "cannot verify key with plain");
869 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, "xxx", 3, 0), "wrong key lenght");
870 OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, "volumekeyvolumek", 16, 0));
871 EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
872 OK_(crypt_deactivate(cd, CDEVICE_2));
876 // Check that gcrypt is properly initialised in format
877 static void NonFIPSAlg(void)
879 struct crypt_device *cd;
880 struct crypt_params_luks1 params = {
884 size_t key_size = 128;
885 char *cipher = "aes";
886 char *cipher_mode = "cbc-essiv:sha256";
888 if (!gcrypt_compatible) {
889 printf("WARNING: old libgcrypt, skipping test.\n");
892 OK_(crypt_init(&cd, DEVICE_2));
893 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
896 FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms),
897 "MD5 unsupported, too short");
902 static void _gcrypt_compatible()
907 if (!(f = popen("libgcrypt-config --version", "r")))
910 if (fscanf(f, "%d.%d.%d", &maj, &min, &patch) == 3 &&
911 maj >= 1 && min >= 4)
912 gcrypt_compatible = 1;
914 printf("libgcrypt version %d.%d.%d detected.\n", maj, min, patch);
920 int main (int argc, char *argv[])
925 printf("You must be root to run this test.\n");
929 for (i = 1; i < argc; i++) {
930 if (!strcmp("-v", argv[i]) || !strcmp("--verbose", argv[i]))
932 else if (!strcmp("--debug", argv[i]))
933 _debug = _verbose = 1;
938 _gcrypt_compatible();
940 crypt_set_debug_level(_debug ? CRYPT_DEBUG_ALL : CRYPT_DEBUG_NONE);
942 RUN_(NonFIPSAlg, "Crypto is properly initialised in format"); //must be the first!
944 RUN_(LuksUUID, "luksUUID API call");
945 RUN_(IsLuks, "isLuks API call");
946 RUN_(LuksOpen, "luksOpen API call");
947 RUN_(query_device, "crypt_query_device API call");
948 RUN_(remove_device, "crypt_remove_device API call");
949 RUN_(LuksFormat, "luksFormat API call");
950 RUN_(LuksKeyGame, "luksAddKey, RemoveKey, KillSlot API calls");
951 RUN_(DeviceResizeGame, "regular crypto, resize calls");
953 RUN_(AddDevicePlain, "plain device API creation exercise");
954 RUN_(AddDeviceLuks, "Format and use LUKS device");
955 RUN_(UseLuksDevice, "Use pre-formated LUKS device");
956 RUN_(SuspendDevice, "Suspend/Resume test");
957 RUN_(UseTempVolumes, "Format and use temporary encrypted device");
959 RUN_(CallbacksTest, "API callbacks test");