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 void _system(const char *command, int warn)
139 if (system(command) < 0 && warn)
140 printf("System command failed: %s", command);
143 static struct interface_callbacks cmd_icb = {
144 .yesDialog = yesDialog,
148 static void _cleanup(void)
152 //_system("udevadm settle", 0);
154 if (!stat(DMDIR CDEVICE_1, &st))
155 _system("dmsetup remove " CDEVICE_1, 0);
157 if (!stat(DMDIR CDEVICE_2, &st))
158 _system("dmsetup remove " CDEVICE_2, 0);
160 if (!stat(DEVICE_EMPTY, &st))
161 _system("dmsetup remove " DEVICE_EMPTY_name, 0);
163 if (!stat(DEVICE_ERROR, &st))
164 _system("dmsetup remove " DEVICE_ERROR_name, 0);
166 if (!strncmp("/dev/loop", DEVICE_1, 9))
167 _system("losetup -d " DEVICE_1, 0);
169 if (!strncmp("/dev/loop", DEVICE_2, 9))
170 _system("losetup -d " DEVICE_2, 0);
172 _system("rm -f " IMAGE_EMPTY, 0);
176 static void _setup(void)
178 _system("dmsetup create " DEVICE_EMPTY_name " --table \"0 10000 zero\"", 1);
179 _system("dmsetup create " DEVICE_ERROR_name " --table \"0 10000 error\"", 1);
180 if (!strncmp("/dev/loop", DEVICE_1, 9)) {
181 _system(" [ ! -e " IMAGE1 " ] && bzip2 -dk " IMAGE1 ".bz2", 1);
182 _system("losetup " DEVICE_1 " " IMAGE1, 1);
184 if (!strncmp("/dev/loop", DEVICE_2, 9)) {
185 _system("dd if=/dev/zero of=" IMAGE_EMPTY " bs=1M count=4", 1);
186 _system("losetup " DEVICE_2 " " IMAGE_EMPTY, 1);
191 void check_ok(int status, int line, const char *func)
196 crypt_get_error(buf, sizeof(buf));
197 printf("FAIL line %d [%s]: code %d, %s\n", line, func, status, buf);
203 void check_ko(int status, int line, const char *func)
207 memset(buf, 0, sizeof(buf));
208 crypt_get_error(buf, sizeof(buf));
210 printf("FAIL line %d [%s]: code %d, %s\n", line, func, status, buf);
214 printf(" => errno %d, errmsg: %s\n", status, buf);
217 void check_equal(int line, const char *func)
219 printf("FAIL line %d [%s]: expected equal values differs.\n", line, func);
224 void xlog(const char *msg, const char *tst, const char *func, int line, const char *txt)
228 printf(" [%s,%s:%d] %s [%s]\n", msg, func, line, tst, txt);
230 printf(" [%s,%s:%d] %s\n", msg, func, line, tst);
233 #define OK_(x) do { xlog("(success)", #x, __FUNCTION__, __LINE__, NULL); \
234 check_ok((x), __LINE__, __FUNCTION__); \
236 #define FAIL_(x, y) do { xlog("(fail) ", #x, __FUNCTION__, __LINE__, y); \
237 check_ko((x), __LINE__, __FUNCTION__); \
239 #define EQ_(x, y) do { xlog("(equal) ", #x " == " #y, __FUNCTION__, __LINE__, NULL); \
240 if ((x) != (y)) check_equal(__LINE__, __FUNCTION__); \
243 #define RUN_(x, y) do { printf("%s: %s\n", #x, (y)); x(); } while (0)
246 static void LuksUUID(void)
248 struct crypt_options co = { .icb = &cmd_icb };
250 co.device = DEVICE_EMPTY;
251 EQ_(crypt_luksUUID(&co), -EINVAL);
253 co.device = DEVICE_ERROR;
254 EQ_(crypt_luksUUID(&co), -EINVAL);
257 co.device = DEVICE_1;
258 OK_(crypt_luksUUID(&co));
259 EQ_(strlen(global_log), 37); /* UUID + "\n" */
260 EQ_(strncmp(global_log, DEVICE_1_UUID, strlen(DEVICE_1_UUID)), 0);
264 static void IsLuks(void)
266 struct crypt_options co = { .icb = &cmd_icb };
268 co.device = DEVICE_EMPTY;
269 EQ_(crypt_isLuks(&co), -EINVAL);
271 co.device = DEVICE_ERROR;
272 EQ_(crypt_isLuks(&co), -EINVAL);
274 co.device = DEVICE_1;
275 OK_(crypt_isLuks(&co));
278 static void LuksOpen(void)
280 struct crypt_options co = {
282 //.passphrase = "blabla",
286 OK_(_prepare_keyfile(KEYFILE1, KEY1));
287 co.key_file = KEYFILE1;
289 co.device = DEVICE_EMPTY;
290 EQ_(crypt_luksOpen(&co), -EINVAL);
292 co.device = DEVICE_ERROR;
293 EQ_(crypt_luksOpen(&co), -EINVAL);
295 co.device = DEVICE_1;
296 OK_(crypt_luksOpen(&co));
297 FAIL_(crypt_luksOpen(&co), "already open");
302 static void query_device(void)
304 struct crypt_options co = {.icb = &cmd_icb };
306 co.name = CDEVICE_WRONG;
307 EQ_(crypt_query_device(&co), 0);
310 EQ_(crypt_query_device(&co), 1);
312 OK_(strncmp(crypt_get_dir(), DMDIR, 11));
313 OK_(strcmp(co.cipher, "aes-cbc-essiv:sha256"));
314 EQ_(co.key_size, 16);
315 EQ_(co.offset, 1032);
316 EQ_(co.flags & CRYPT_FLAG_READONLY, 0);
318 crypt_put_options(&co);
321 static void remove_device(void)
324 struct crypt_options co = {.icb = &cmd_icb };
326 co.name = CDEVICE_WRONG;
327 EQ_(crypt_remove_device(&co), -ENODEV);
329 fd = open(DMDIR CDEVICE_1, O_RDONLY);
331 FAIL_(crypt_remove_device(&co), "device busy");
334 OK_(crypt_remove_device(&co));
337 static void LuksFormat(void)
339 struct crypt_options co = {
343 .cipher = "aes-cbc-essiv:sha256",
346 .iteration_time = 10,
351 OK_(_prepare_keyfile(KEYFILE1, KEY1));
353 co.new_key_file = KEYFILE1;
354 co.device = DEVICE_ERROR;
355 FAIL_(crypt_luksFormat(&co), "error device");
357 co.device = DEVICE_2;
358 OK_(crypt_luksFormat(&co));
360 co.new_key_file = NULL;
361 co.key_file = KEYFILE1;
363 OK_(crypt_luksOpen(&co));
364 OK_(crypt_remove_device(&co));
368 static void LuksKeyGame(void)
371 struct crypt_options co = {
375 .cipher = "aes-cbc-essiv:sha256",
378 .iteration_time = 10,
383 OK_(_prepare_keyfile(KEYFILE1, KEY1));
384 OK_(_prepare_keyfile(KEYFILE2, KEY2));
386 co.new_key_file = KEYFILE1;
387 co.device = DEVICE_2;
389 FAIL_(crypt_luksFormat(&co), "wrong slot #");
391 co.key_slot = 7; // last slot
392 OK_(crypt_luksFormat(&co));
394 co.new_key_file = KEYFILE1;
395 co.key_file = KEYFILE1;
397 FAIL_(crypt_luksAddKey(&co), "wrong slot #");
399 FAIL_(crypt_luksAddKey(&co), "slot already used");
402 OK_(crypt_luksAddKey(&co));
404 co.key_file = KEYFILE2 "blah";
406 FAIL_(crypt_luksAddKey(&co), "keyfile not found");
408 co.new_key_file = KEYFILE2; // key to add
409 co.key_file = KEYFILE1;
411 for (i = 0; i < 6; i++)
412 OK_(crypt_luksAddKey(&co)); //FIXME: EQ_(i)?
414 FAIL_(crypt_luksAddKey(&co), "all slots full");
417 co.new_key_file = KEYFILE1; // key to remove
419 co.key_slot = 8; // should be ignored
420 // only 2 slots should use KEYFILE1
421 OK_(crypt_luksRemoveKey(&co));
422 OK_(crypt_luksRemoveKey(&co));
423 FAIL_(crypt_luksRemoveKey(&co), "no slot with this passphrase");
425 co.new_key_file = KEYFILE2 "blah";
427 FAIL_(crypt_luksRemoveKey(&co), "keyfile not found");
430 co.new_key_file = NULL;
433 FAIL_(crypt_luksKillSlot(&co), "wrong slot #");
435 FAIL_(crypt_luksKillSlot(&co), "slot already wiped");
438 OK_(crypt_luksKillSlot(&co));
443 size_t _get_device_size(const char *device)
445 unsigned long size = 0;
448 fd = open(device, O_RDONLY);
451 (void)ioctl(fd, BLKGETSIZE, &size);
457 void DeviceResizeGame(void)
460 struct crypt_options co = {
464 .cipher = "aes-cbc-plain",
471 orig_size = _get_device_size(DEVICE_2);
473 OK_(_prepare_keyfile(KEYFILE2, KEY2));
475 co.key_file = KEYFILE2;
477 OK_(crypt_create_device(&co));
478 EQ_(_get_device_size(DMDIR CDEVICE_2), 1000);
481 OK_(crypt_resize_device(&co));
482 EQ_(_get_device_size(DMDIR CDEVICE_2), 2000);
485 OK_(crypt_resize_device(&co));
486 EQ_(_get_device_size(DMDIR CDEVICE_2), (orig_size - 333));
490 co.cipher = "aes-cbc-essiv:sha256";
491 OK_(crypt_update_device(&co));
492 EQ_(_get_device_size(DMDIR CDEVICE_2), (orig_size - 444));
494 memset(&co, 0, sizeof(co));
497 EQ_(crypt_query_device(&co), 1);
498 EQ_(strcmp(co.cipher, "aes-cbc-essiv:sha256"), 0);
499 EQ_(co.key_size, 128 / 8);
502 crypt_put_options(&co);
504 // dangerous switch device still works
505 memset(&co, 0, sizeof(co));
507 co.device = DEVICE_1;
508 co.key_file = KEYFILE2;
509 co.key_size = 128 / 8;
510 co.cipher = "aes-cbc-plain";
513 OK_(crypt_update_device(&co));
515 memset(&co, 0, sizeof(co));
518 EQ_(crypt_query_device(&co), 1);
519 EQ_(strcmp(co.cipher, "aes-cbc-plain"), 0);
520 EQ_(co.key_size, 128 / 8);
523 // This expect lookup returns prefered /dev/loopX
524 EQ_(strcmp(co.device, DEVICE_1), 0);
525 crypt_put_options(&co);
527 memset(&co, 0, sizeof(co));
530 OK_(crypt_remove_device(&co));
537 static void AddDevicePlain(void)
539 struct crypt_device *cd;
540 struct crypt_params_plain params = {
546 char key[128], key2[128], path[128];
548 char *passphrase = PASSPHRASE;
549 char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
550 size_t key_size = strlen(mk_hex) / 2;
551 char *cipher = "aes";
552 char *cipher_mode = "cbc-essiv:sha256";
554 crypt_decode_key(key, mk_hex, key_size);
556 FAIL_(crypt_init(&cd, ""), "empty device string");
558 // default is "plain" hash - no password hash
559 OK_(crypt_init(&cd, DEVICE_1));
560 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, NULL));
561 FAIL_(crypt_activate_by_volume_key(cd, NULL, key, key_size, 0), "cannot verify key with plain");
562 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
563 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
564 // FIXME: this should get key from active device?
565 //OK_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)));
566 //OK_(memcmp(key, key2, key_size));
567 OK_(crypt_deactivate(cd, CDEVICE_1));
570 // Now use hashed password
571 OK_(crypt_init(&cd, DEVICE_1));
572 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
573 FAIL_(crypt_activate_by_passphrase(cd, NULL, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0),
574 "cannot verify passphrase with plain" );
575 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
577 // device status check
578 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
579 snprintf(path, sizeof(path), "%s/%s", crypt_get_dir(), CDEVICE_1);
580 fd = open(path, O_RDONLY);
581 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_BUSY);
582 FAIL_(crypt_deactivate(cd, CDEVICE_1), "Device is busy");
584 OK_(crypt_deactivate(cd, CDEVICE_1));
585 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
587 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
588 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
590 // retrieve volume key check
591 memset(key2, 0, key_size);
594 FAIL_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)), "small buffer");
596 OK_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)));
598 OK_(memcmp(key, key2, key_size));
599 OK_(strcmp(cipher, crypt_get_cipher(cd)));
600 OK_(strcmp(cipher_mode, crypt_get_cipher_mode(cd)));
601 EQ_(key_size, crypt_get_volume_key_size(cd));
602 EQ_(0, crypt_get_data_offset(cd));
603 OK_(crypt_deactivate(cd, CDEVICE_1));
607 #define CALLBACK_ERROR "calback_error xyz"
608 static int pass_callback_err(const char *msg, char *buf, size_t length, void *usrptr)
610 struct crypt_device *cd = usrptr;
616 crypt_log(cd, CRYPT_LOG_ERROR, CALLBACK_ERROR);
620 static int pass_callback_ok(const char *msg, char *buf, size_t length, void *usrptr)
624 strcpy(buf, PASSPHRASE);
628 static void CallbacksTest(void)
630 struct crypt_device *cd;
631 struct crypt_params_plain params = {
637 size_t key_size = 256 / 8;
638 char *cipher = "aes";
639 char *cipher_mode = "cbc-essiv:sha256";
640 char *passphrase = PASSPHRASE;
642 OK_(crypt_init(&cd, DEVICE_1));
643 crypt_set_log_callback(cd, &new_log, NULL);
644 //crypt_set_log_callback(cd, NULL, NULL);
646 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
648 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
649 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
650 OK_(crypt_deactivate(cd, CDEVICE_1));
653 crypt_set_password_callback(cd, pass_callback_err, cd);
654 FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, NULL, 0, 0), "callback fails");
655 EQ_(strncmp(global_log, CALLBACK_ERROR, strlen(CALLBACK_ERROR)), 0);
657 crypt_set_password_callback(cd, pass_callback_ok, NULL);
658 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, NULL, 0, 0));
659 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
660 OK_(crypt_deactivate(cd, CDEVICE_1));
665 static void UseLuksDevice(void)
667 struct crypt_device *cd;
671 OK_(crypt_init(&cd, DEVICE_1));
672 OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
673 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
674 OK_(crypt_activate_by_passphrase(cd, NULL, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0));
675 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0));
676 FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0), "already open");
677 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
678 OK_(crypt_deactivate(cd, CDEVICE_1));
679 FAIL_(crypt_deactivate(cd, CDEVICE_1), "no such device");
682 OK_(strcmp("aes", crypt_get_cipher(cd)));
683 OK_(strcmp("cbc-essiv:sha256", crypt_get_cipher_mode(cd)));
684 OK_(strcmp(DEVICE_1_UUID, crypt_get_uuid(cd)));
685 EQ_(key_size, crypt_get_volume_key_size(cd));
686 EQ_(1032, crypt_get_data_offset(cd));
688 EQ_(0, crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key, &key_size, KEY1, strlen(KEY1)));
689 OK_(crypt_volume_key_verify(cd, key, key_size));
690 OK_(crypt_activate_by_volume_key(cd, NULL, key, key_size, 0));
691 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
692 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
693 OK_(crypt_deactivate(cd, CDEVICE_1));
696 FAIL_(crypt_volume_key_verify(cd, key, key_size), "key mismatch");
697 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0), "key mismatch");
701 static void SuspendDevice(void)
704 struct crypt_device *cd;
706 OK_(crypt_init(&cd, DEVICE_1));
707 OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
708 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0));
710 suspend_status = crypt_suspend(cd, CDEVICE_1);
711 if (suspend_status == -ENOTSUP) {
712 printf("WARNING: Suspend/Resume not supported, skipping test.\n");
716 FAIL_(crypt_suspend(cd, CDEVICE_1), "already suspended");
718 FAIL_(crypt_resume_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1)-1), "wrong key");
719 OK_(crypt_resume_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1)));
720 FAIL_(crypt_resume_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1)), "not suspended");
722 OK_(_prepare_keyfile(KEYFILE1, KEY1));
723 OK_(crypt_suspend(cd, CDEVICE_1));
724 FAIL_(crypt_resume_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1 "blah", 0), "wrong keyfile");
725 OK_(crypt_resume_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0));
726 FAIL_(crypt_resume_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0), "not suspended");
729 OK_(crypt_deactivate(cd, CDEVICE_1));
733 static void AddDeviceLuks(void)
735 struct crypt_device *cd;
736 struct crypt_params_luks1 params = {
738 .data_alignment = 2048, // 4M, data offset will be 4096
740 char key[128], key2[128];
742 char *passphrase = "blabla";
743 char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
744 size_t key_size = strlen(mk_hex) / 2;
745 char *cipher = "aes";
746 char *cipher_mode = "cbc-essiv:sha256";
748 crypt_decode_key(key, mk_hex, key_size);
750 OK_(crypt_init(&cd, DEVICE_2));
751 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
753 // even with no keyslots defined it can be activated by volume key
754 OK_(crypt_volume_key_verify(cd, key, key_size));
755 OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, key, key_size, 0));
756 EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
757 OK_(crypt_deactivate(cd, CDEVICE_2));
760 EQ_(7, crypt_keyslot_add_by_volume_key(cd, 7, key, key_size, passphrase, strlen(passphrase)));
761 EQ_(CRYPT_SLOT_ACTIVE_LAST, crypt_keyslot_status(cd, 7));
762 EQ_(7, crypt_activate_by_passphrase(cd, CDEVICE_2, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
763 EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
764 OK_(crypt_deactivate(cd, CDEVICE_2));
766 EQ_(1, crypt_keyslot_add_by_volume_key(cd, 1, key, key_size, KEY1, strlen(KEY1)));
767 OK_(_prepare_keyfile(KEYFILE1, KEY1));
768 OK_(_prepare_keyfile(KEYFILE2, KEY2));
769 EQ_(2, crypt_keyslot_add_by_keyfile(cd, 2, KEYFILE1, 0, KEYFILE2, 0));
770 FAIL_(crypt_activate_by_keyfile(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, strlen(KEY2)-1, 0), "key mismatch");
771 EQ_(2, crypt_activate_by_keyfile(cd, NULL, CRYPT_ANY_SLOT, KEYFILE2, 0, 0));
772 EQ_(2, crypt_activate_by_keyfile(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, 0, 0));
773 OK_(crypt_keyslot_destroy(cd, 1));
774 OK_(crypt_keyslot_destroy(cd, 2));
775 OK_(crypt_deactivate(cd, CDEVICE_2));
778 FAIL_(crypt_keyslot_add_by_volume_key(cd, 7, key, key_size, passphrase, strlen(passphrase)), "slot used");
780 FAIL_(crypt_keyslot_add_by_volume_key(cd, 6, key, key_size, passphrase, strlen(passphrase)), "key mismatch");
782 EQ_(6, crypt_keyslot_add_by_volume_key(cd, 6, key, key_size, passphrase, strlen(passphrase)));
783 EQ_(CRYPT_SLOT_ACTIVE, crypt_keyslot_status(cd, 6));
785 FAIL_(crypt_keyslot_destroy(cd, 8), "invalid keyslot");
786 FAIL_(crypt_keyslot_destroy(cd, CRYPT_ANY_SLOT), "invalid keyslot");
787 FAIL_(crypt_keyslot_destroy(cd, 0), "keyslot not used");
788 OK_(crypt_keyslot_destroy(cd, 7));
789 EQ_(CRYPT_SLOT_INACTIVE, crypt_keyslot_status(cd, 7));
790 EQ_(CRYPT_SLOT_ACTIVE_LAST, crypt_keyslot_status(cd, 6));
792 EQ_(6, crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)));
793 OK_(crypt_volume_key_verify(cd, key2, key_size));
795 OK_(memcmp(key, key2, key_size));
796 OK_(strcmp(cipher, crypt_get_cipher(cd)));
797 OK_(strcmp(cipher_mode, crypt_get_cipher_mode(cd)));
798 EQ_(key_size, crypt_get_volume_key_size(cd));
799 EQ_(4096, crypt_get_data_offset(cd));
800 OK_(strcmp(DEVICE_2, crypt_get_device_name(cd)));
803 crypt_set_log_callback(cd, &new_log, NULL);
805 OK_(!(global_lines != 0));
806 crypt_set_log_callback(cd, NULL, NULL);
809 FAIL_(crypt_set_uuid(cd, "blah"), "wrong UUID format");
810 OK_(crypt_set_uuid(cd, DEVICE_TEST_UUID));
811 OK_(strcmp(DEVICE_TEST_UUID, crypt_get_uuid(cd)));
813 FAIL_(crypt_deactivate(cd, CDEVICE_2), "not active");
817 static void UseTempVolumes(void)
819 struct crypt_device *cd;
821 // Tepmporary device without keyslot but with on-disk LUKS header
822 OK_(crypt_init(&cd, DEVICE_2));
823 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "not yet formatted");
824 OK_(crypt_format(cd, CRYPT_LUKS1, "aes", "cbc-essiv:sha256", NULL, NULL, 16, NULL));
825 OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0));
826 EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
829 // Volume key is properly initialised from active device
830 OK_(crypt_init_by_name(&cd, CDEVICE_2));
831 OK_(crypt_deactivate(cd, CDEVICE_2));
832 OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0));
833 OK_(crypt_deactivate(cd, CDEVICE_2));
836 // Dirty checks: device without UUID
837 // we should be able to remove it but not manuipulate with it
838 _system("dmsetup create " CDEVICE_2 " --table \""
839 "0 100 crypt aes-cbc-essiv:sha256 deadbabedeadbabedeadbabedeadbabe 0 "
840 DEVICE_2 " 2048\"", 1);
841 OK_(crypt_init_by_name(&cd, CDEVICE_2));
842 OK_(crypt_deactivate(cd, CDEVICE_2));
843 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "No known device type");
846 // Dirty checks: device with UUID but LUKS header key fingerprint must fail)
847 _system("dmsetup create " CDEVICE_2 " --table \""
848 "0 100 crypt aes-cbc-essiv:sha256 deadbabedeadbabedeadbabedeadbabe 0 "
850 "-u CRYPT-LUKS1-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-ctest1", 1);
851 OK_(crypt_init_by_name(&cd, CDEVICE_2));
852 OK_(crypt_deactivate(cd, CDEVICE_2));
853 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "wrong volume key");
857 OK_(crypt_init(&cd, DEVICE_2));
858 OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
859 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "volume key is lost");
863 OK_(crypt_init(&cd, DEVICE_2));
864 OK_(crypt_format(cd, CRYPT_PLAIN, "aes", "cbc-essiv:sha256", NULL, NULL, 16, NULL));
865 FAIL_(crypt_activate_by_volume_key(cd, NULL, "xxx", 3, 0), "cannot verify key with plain");
866 FAIL_(crypt_volume_key_verify(cd, "xxx", 3), "cannot verify key with plain");
867 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, "xxx", 3, 0), "wrong key lenght");
868 OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, "volumekeyvolumek", 16, 0));
869 EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
870 OK_(crypt_deactivate(cd, CDEVICE_2));
874 // Check that gcrypt is properly initialised in format
875 static void NonFIPSAlg(void)
877 struct crypt_device *cd;
878 struct crypt_params_luks1 params = {
882 size_t key_size = 128;
883 char *cipher = "aes";
884 char *cipher_mode = "cbc-essiv:sha256";
886 if (!gcrypt_compatible) {
887 printf("WARNING: old libgcrypt, skipping test.\n");
890 OK_(crypt_init(&cd, DEVICE_2));
891 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
894 FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms),
895 "MD5 unsupported, too short");
900 static void _gcrypt_compatible()
905 if (!(f = popen("libgcrypt-config --version", "r")))
908 if (fscanf(f, "%d.%d.%d", &maj, &min, &patch) == 3 &&
909 maj >= 1 && min >= 4)
910 gcrypt_compatible = 1;
912 printf("libgcrypt version %d.%d.%d detected.\n", maj, min, patch);
918 int main (int argc, char *argv[])
923 printf("You must be root to run this test.\n");
927 for (i = 1; i < argc; i++) {
928 if (!strcmp("-v", argv[i]) || !strcmp("--verbose", argv[i]))
930 else if (!strcmp("--debug", argv[i]))
931 _debug = _verbose = 1;
936 _gcrypt_compatible();
938 crypt_set_debug_level(_debug ? CRYPT_DEBUG_ALL : CRYPT_DEBUG_NONE);
940 RUN_(NonFIPSAlg, "Crypto is properly initialised in format"); //must be the first!
942 RUN_(LuksUUID, "luksUUID API call");
943 RUN_(IsLuks, "isLuks API call");
944 RUN_(LuksOpen, "luksOpen API call");
945 RUN_(query_device, "crypt_query_device API call");
946 RUN_(remove_device, "crypt_remove_device API call");
947 RUN_(LuksFormat, "luksFormat API call");
948 RUN_(LuksKeyGame, "luksAddKey, RemoveKey, KillSlot API calls");
949 RUN_(DeviceResizeGame, "regular crypto, resize calls");
951 RUN_(AddDevicePlain, "plain device API creation exercise");
952 RUN_(AddDeviceLuks, "Format and use LUKS device");
953 RUN_(UseLuksDevice, "Use pre-formated LUKS device");
954 RUN_(SuspendDevice, "Suspend/Resume test");
955 RUN_(UseTempVolumes, "Format and use temporary encrypted device");
957 RUN_(CallbacksTest, "API callbacks test");