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
28 #include <sys/ioctl.h>
30 #include "libcryptsetup.h"
32 #define DMDIR "/dev/mapper/"
34 #define DEVICE_1 "/dev/loop5"
35 #define DEVICE_1_UUID "28632274-8c8a-493f-835b-da802e1c576b"
36 #define DEVICE_2 "/dev/loop6"
37 #define DEVICE_EMPTY_name "crypt_zero"
38 #define DEVICE_EMPTY DMDIR DEVICE_EMPTY_name
39 #define DEVICE_ERROR_name "crypt_error"
40 #define DEVICE_ERROR DMDIR DEVICE_ERROR_name
42 #define CDEVICE_1 "ctest1"
43 #define CDEVICE_2 "ctest2"
44 #define CDEVICE_WRONG "O_o"
46 #define IMAGE1 "compatimage.img"
47 #define IMAGE_EMPTY "empty.img"
49 #define KEYFILE1 "key1.file"
50 #define KEY1 "compatkey"
52 #define KEYFILE2 "key2.file"
53 #define KEY2 "0123456789abcdef"
55 #define DEVICE_TEST_UUID "12345678-1234-1234-1234-123456789abc"
57 static int _debug = 0;
58 static int _verbose = 1;
60 static char global_log[4096];
61 static int global_lines = 0;
63 static int gcrypt_compatible = 0;
66 static int _prepare_keyfile(const char *name, const char *passphrase)
70 fd = open(name, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR);
72 r = write(fd, passphrase, strlen(passphrase));
77 return r == strlen(passphrase) ? 0 : 1;
80 static void _remove_keyfiles(void)
86 // Decode key from its hex representation
87 static int crypt_decode_key(char *key, char *hex, unsigned int size)
95 for (i = 0; i < size; i++) {
99 key[i] = (unsigned char)strtoul(buffer, &endp, 16);
101 if (endp != &buffer[2])
111 static int yesDialog(char *msg)
116 static void cmdLineLog(int level, char *msg)
118 strncat(global_log, msg, sizeof(global_log) - strlen(global_log));
122 static void new_log(int level, const char *msg, void *usrptr)
124 cmdLineLog(level, (char*)msg);
128 static void reset_log()
130 memset(global_log, 0, sizeof(global_log));
134 static struct interface_callbacks cmd_icb = {
135 .yesDialog = yesDialog,
139 static void _cleanup(void)
144 //r = system("udevadm settle");
146 if (!stat(DMDIR CDEVICE_1, &st))
147 r = system("dmsetup remove " CDEVICE_1);
149 if (!stat(DMDIR CDEVICE_2, &st))
150 r = system("dmsetup remove " CDEVICE_2);
152 if (!stat(DEVICE_EMPTY, &st))
153 r = system("dmsetup remove " DEVICE_EMPTY_name);
155 if (!stat(DEVICE_ERROR, &st))
156 r = system("dmsetup remove " DEVICE_ERROR_name);
158 if (!strncmp("/dev/loop", DEVICE_1, 9))
159 r = system("losetup -d " DEVICE_1);
161 if (!strncmp("/dev/loop", DEVICE_2, 9))
162 r = system("losetup -d " DEVICE_2);
164 r = system("rm -f " IMAGE_EMPTY);
168 static void _setup(void)
172 r = system("dmsetup create " DEVICE_EMPTY_name " --table \"0 10000 zero\"");
173 r = system("dmsetup create " DEVICE_ERROR_name " --table \"0 10000 error\"");
174 if (!strncmp("/dev/loop", DEVICE_1, 9)) {
175 r = system(" [ ! -e " IMAGE1 " ] && bzip2 -dk " IMAGE1 ".bz2");
176 r = system("losetup " DEVICE_1 " " IMAGE1);
178 if (!strncmp("/dev/loop", DEVICE_2, 9)) {
179 r = system("dd if=/dev/zero of=" IMAGE_EMPTY " bs=1M count=4");
180 r = system("losetup " DEVICE_2 " " IMAGE_EMPTY);
185 void check_ok(int status, int line, const char *func)
190 crypt_get_error(buf, sizeof(buf));
191 printf("FAIL line %d [%s]: code %d, %s\n", line, func, status, buf);
197 void check_ko(int status, int line, const char *func)
201 memset(buf, 0, sizeof(buf));
202 crypt_get_error(buf, sizeof(buf));
204 printf("FAIL line %d [%s]: code %d, %s\n", line, func, status, buf);
208 printf(" => errno %d, errmsg: %s\n", status, buf);
211 void check_equal(int line, const char *func)
213 printf("FAIL line %d [%s]: expected equal values differs.\n", line, func);
218 void xlog(const char *msg, const char *tst, const char *func, int line, const char *txt)
222 printf(" [%s,%s:%d] %s [%s]\n", msg, func, line, tst, txt);
224 printf(" [%s,%s:%d] %s\n", msg, func, line, tst);
227 #define OK_(x) do { xlog("(success)", #x, __FUNCTION__, __LINE__, NULL); \
228 check_ok((x), __LINE__, __FUNCTION__); \
230 #define FAIL_(x, y) do { xlog("(fail) ", #x, __FUNCTION__, __LINE__, y); \
231 check_ko((x), __LINE__, __FUNCTION__); \
233 #define EQ_(x, y) do { xlog("(equal) ", #x " == " #y, __FUNCTION__, __LINE__, NULL); \
234 if ((x) != (y)) check_equal(__LINE__, __FUNCTION__); \
237 #define RUN_(x, y) do { printf("%s: %s\n", #x, (y)); x(); } while (0)
240 static void LuksUUID(void)
242 struct crypt_options co = { .icb = &cmd_icb };
244 co.device = DEVICE_EMPTY;
245 EQ_(crypt_luksUUID(&co), -EINVAL);
247 co.device = DEVICE_ERROR;
248 EQ_(crypt_luksUUID(&co), -EINVAL);
251 co.device = DEVICE_1;
252 OK_(crypt_luksUUID(&co));
253 EQ_(strlen(global_log), 37); /* UUID + "\n" */
254 EQ_(strncmp(global_log, DEVICE_1_UUID, strlen(DEVICE_1_UUID)), 0);
258 static void IsLuks(void)
260 struct crypt_options co = { .icb = &cmd_icb };
262 co.device = DEVICE_EMPTY;
263 EQ_(crypt_isLuks(&co), -EINVAL);
265 co.device = DEVICE_ERROR;
266 EQ_(crypt_isLuks(&co), -EINVAL);
268 co.device = DEVICE_1;
269 OK_(crypt_isLuks(&co));
272 static void LuksOpen(void)
274 struct crypt_options co = {
276 //.passphrase = "blabla",
280 OK_(_prepare_keyfile(KEYFILE1, KEY1));
281 co.key_file = KEYFILE1;
283 co.device = DEVICE_EMPTY;
284 EQ_(crypt_luksOpen(&co), -EINVAL);
286 co.device = DEVICE_ERROR;
287 EQ_(crypt_luksOpen(&co), -EINVAL);
289 co.device = DEVICE_1;
290 OK_(crypt_luksOpen(&co));
291 FAIL_(crypt_luksOpen(&co), "already open");
296 static void query_device(void)
298 struct crypt_options co = {.icb = &cmd_icb };
300 co.name = CDEVICE_WRONG;
301 EQ_(crypt_query_device(&co), 0);
304 EQ_(crypt_query_device(&co), 1);
306 OK_(strncmp(crypt_get_dir(), DMDIR, 11));
307 OK_(strcmp(co.cipher, "aes-cbc-essiv:sha256"));
308 EQ_(co.key_size, 16);
309 EQ_(co.offset, 1032);
310 EQ_(co.flags & CRYPT_FLAG_READONLY, 0);
312 crypt_put_options(&co);
315 static void remove_device(void)
318 struct crypt_options co = {.icb = &cmd_icb };
320 co.name = CDEVICE_WRONG;
321 EQ_(crypt_remove_device(&co), -ENODEV);
323 fd = open(DMDIR CDEVICE_1, O_RDONLY);
325 FAIL_(crypt_remove_device(&co), "device busy");
328 OK_(crypt_remove_device(&co));
331 static void LuksFormat(void)
333 struct crypt_options co = {
337 .cipher = "aes-cbc-essiv:sha256",
340 .iteration_time = 10,
345 OK_(_prepare_keyfile(KEYFILE1, KEY1));
347 co.new_key_file = KEYFILE1;
348 co.device = DEVICE_ERROR;
349 FAIL_(crypt_luksFormat(&co), "error device");
351 co.device = DEVICE_2;
352 OK_(crypt_luksFormat(&co));
354 co.new_key_file = NULL;
355 co.key_file = KEYFILE1;
357 OK_(crypt_luksOpen(&co));
358 OK_(crypt_remove_device(&co));
362 static void LuksKeyGame(void)
365 struct crypt_options co = {
369 .cipher = "aes-cbc-essiv:sha256",
372 .iteration_time = 10,
377 OK_(_prepare_keyfile(KEYFILE1, KEY1));
378 OK_(_prepare_keyfile(KEYFILE2, KEY2));
380 co.new_key_file = KEYFILE1;
381 co.device = DEVICE_2;
383 FAIL_(crypt_luksFormat(&co), "wrong slot #");
385 co.key_slot = 7; // last slot
386 OK_(crypt_luksFormat(&co));
388 co.new_key_file = KEYFILE1;
389 co.key_file = KEYFILE1;
391 FAIL_(crypt_luksAddKey(&co), "wrong slot #");
393 FAIL_(crypt_luksAddKey(&co), "slot already used");
396 OK_(crypt_luksAddKey(&co));
398 co.key_file = KEYFILE2 "blah";
400 FAIL_(crypt_luksAddKey(&co), "keyfile not found");
402 co.new_key_file = KEYFILE2; // key to add
403 co.key_file = KEYFILE1;
405 for (i = 0; i < 6; i++)
406 OK_(crypt_luksAddKey(&co)); //FIXME: EQ_(i)?
408 FAIL_(crypt_luksAddKey(&co), "all slots full");
411 co.new_key_file = KEYFILE1; // key to remove
413 co.key_slot = 8; // should be ignored
414 // only 2 slots should use KEYFILE1
415 OK_(crypt_luksRemoveKey(&co));
416 OK_(crypt_luksRemoveKey(&co));
417 FAIL_(crypt_luksRemoveKey(&co), "no slot with this passphrase");
419 co.new_key_file = KEYFILE2 "blah";
421 FAIL_(crypt_luksRemoveKey(&co), "keyfile not found");
424 co.new_key_file = NULL;
427 FAIL_(crypt_luksKillSlot(&co), "wrong slot #");
429 FAIL_(crypt_luksKillSlot(&co), "slot already wiped");
432 OK_(crypt_luksKillSlot(&co));
437 size_t _get_device_size(const char *device)
439 unsigned long size = 0;
442 fd = open(device, O_RDONLY);
445 (void)ioctl(fd, BLKGETSIZE, &size);
451 void DeviceResizeGame(void)
454 struct crypt_options co = {
458 .cipher = "aes-cbc-plain",
465 orig_size = _get_device_size(DEVICE_2);
467 OK_(_prepare_keyfile(KEYFILE2, KEY2));
469 co.key_file = KEYFILE2;
471 OK_(crypt_create_device(&co));
472 EQ_(_get_device_size(DMDIR CDEVICE_2), 1000);
475 OK_(crypt_resize_device(&co));
476 EQ_(_get_device_size(DMDIR CDEVICE_2), 2000);
479 OK_(crypt_resize_device(&co));
480 EQ_(_get_device_size(DMDIR CDEVICE_2), (orig_size - 333));
484 co.cipher = "aes-cbc-essiv:sha256";
485 OK_(crypt_update_device(&co));
486 EQ_(_get_device_size(DMDIR CDEVICE_2), (orig_size - 444));
488 memset(&co, 0, sizeof(co));
491 EQ_(crypt_query_device(&co), 1);
492 EQ_(strcmp(co.cipher, "aes-cbc-essiv:sha256"), 0);
493 EQ_(co.key_size, 128 / 8);
496 crypt_put_options(&co);
498 // dangerous switch device still works
499 memset(&co, 0, sizeof(co));
501 co.device = DEVICE_1;
502 co.key_file = KEYFILE2;
503 co.key_size = 128 / 8;
504 co.cipher = "aes-cbc-plain";
507 OK_(crypt_update_device(&co));
509 memset(&co, 0, sizeof(co));
512 EQ_(crypt_query_device(&co), 1);
513 EQ_(strcmp(co.cipher, "aes-cbc-plain"), 0);
514 EQ_(co.key_size, 128 / 8);
517 // This expect lookup returns prefered /dev/loopX
518 EQ_(strcmp(co.device, DEVICE_1), 0);
519 crypt_put_options(&co);
521 memset(&co, 0, sizeof(co));
524 OK_(crypt_remove_device(&co));
531 static void AddDevicePlain(void)
533 struct crypt_device *cd;
534 struct crypt_params_plain params = {
540 char key[128], key2[128], path[128];
542 char *passphrase = "blabla";
543 char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
544 size_t key_size = strlen(mk_hex) / 2;
545 char *cipher = "aes";
546 char *cipher_mode = "cbc-essiv:sha256";
548 crypt_decode_key(key, mk_hex, key_size);
550 FAIL_(crypt_init(&cd, ""), "empty device string");
552 // default is "plain" hash - no password hash
553 OK_(crypt_init(&cd, DEVICE_1));
554 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, NULL));
555 FAIL_(crypt_activate_by_volume_key(cd, NULL, key, key_size, 0), "cannot verify key with plain");
556 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
557 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
558 // FIXME: this should get key from active device?
559 //OK_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)));
560 //OK_(memcmp(key, key2, key_size));
561 OK_(crypt_deactivate(cd, CDEVICE_1));
564 // Now use hashed password
565 OK_(crypt_init(&cd, DEVICE_1));
566 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
567 FAIL_(crypt_activate_by_passphrase(cd, NULL, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0),
568 "cannot verify passphrase with plain" );
569 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
571 // device status check
572 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
573 snprintf(path, sizeof(path), "%s/%s", crypt_get_dir(), CDEVICE_1);
574 fd = open(path, O_RDONLY);
575 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_BUSY);
576 FAIL_(crypt_deactivate(cd, CDEVICE_1), "Device is busy");
578 OK_(crypt_deactivate(cd, CDEVICE_1));
579 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
581 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
582 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
584 // retrieve volume key check
585 memset(key2, 0, key_size);
588 FAIL_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)), "small buffer");
590 OK_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)));
592 OK_(memcmp(key, key2, key_size));
593 OK_(strcmp(cipher, crypt_get_cipher(cd)));
594 OK_(strcmp(cipher_mode, crypt_get_cipher_mode(cd)));
595 EQ_(key_size, crypt_get_volume_key_size(cd));
596 EQ_(0, crypt_get_data_offset(cd));
597 OK_(crypt_deactivate(cd, CDEVICE_1));
601 static void UseLuksDevice(void)
603 struct crypt_device *cd;
607 OK_(crypt_init(&cd, DEVICE_1));
608 OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
609 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
610 OK_(crypt_activate_by_passphrase(cd, NULL, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0));
611 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0));
612 FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0), "already open");
613 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
614 OK_(crypt_deactivate(cd, CDEVICE_1));
615 FAIL_(crypt_deactivate(cd, CDEVICE_1), "no such device");
618 OK_(strcmp("aes", crypt_get_cipher(cd)));
619 OK_(strcmp("cbc-essiv:sha256", crypt_get_cipher_mode(cd)));
620 OK_(strcmp(DEVICE_1_UUID, crypt_get_uuid(cd)));
621 EQ_(key_size, crypt_get_volume_key_size(cd));
622 EQ_(1032, crypt_get_data_offset(cd));
624 EQ_(0, crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key, &key_size, KEY1, strlen(KEY1)));
625 OK_(crypt_volume_key_verify(cd, key, key_size));
626 OK_(crypt_activate_by_volume_key(cd, NULL, key, key_size, 0));
627 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
628 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
629 OK_(crypt_deactivate(cd, CDEVICE_1));
632 FAIL_(crypt_volume_key_verify(cd, key, key_size), "key mismatch");
633 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0), "key mismatch");
637 static void SuspendDevice(void)
640 struct crypt_device *cd;
642 OK_(crypt_init(&cd, DEVICE_1));
643 OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
644 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0));
646 suspend_status = crypt_suspend(cd, CDEVICE_1);
647 if (suspend_status == -ENOTSUP) {
648 printf("WARNING: Suspend/Resume not supported, skipping test.\n");
652 FAIL_(crypt_suspend(cd, CDEVICE_1), "already suspended");
654 FAIL_(crypt_resume_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1)-1), "wrong key");
655 OK_(crypt_resume_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1)));
656 FAIL_(crypt_resume_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1)), "not suspended");
658 OK_(_prepare_keyfile(KEYFILE1, KEY1));
659 OK_(crypt_suspend(cd, CDEVICE_1));
660 FAIL_(crypt_resume_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1 "blah", 0), "wrong keyfile");
661 OK_(crypt_resume_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0));
662 FAIL_(crypt_resume_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0), "not suspended");
665 OK_(crypt_deactivate(cd, CDEVICE_1));
669 static void AddDeviceLuks(void)
671 struct crypt_device *cd;
672 struct crypt_params_luks1 params = {
674 .data_alignment = 2048, // 4M, data offset will be 4096
676 char key[128], key2[128];
678 char *passphrase = "blabla";
679 char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
680 size_t key_size = strlen(mk_hex) / 2;
681 char *cipher = "aes";
682 char *cipher_mode = "cbc-essiv:sha256";
684 crypt_decode_key(key, mk_hex, key_size);
686 OK_(crypt_init(&cd, DEVICE_2));
687 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
689 // even with no keyslots defined it can be activated by volume key
690 OK_(crypt_volume_key_verify(cd, key, key_size));
691 OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, key, key_size, 0));
692 EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
693 OK_(crypt_deactivate(cd, CDEVICE_2));
696 EQ_(7, crypt_keyslot_add_by_volume_key(cd, 7, key, key_size, passphrase, strlen(passphrase)));
697 EQ_(CRYPT_SLOT_ACTIVE_LAST, crypt_keyslot_status(cd, 7));
698 EQ_(7, crypt_activate_by_passphrase(cd, CDEVICE_2, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
699 EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
700 OK_(crypt_deactivate(cd, CDEVICE_2));
702 EQ_(1, crypt_keyslot_add_by_volume_key(cd, 1, key, key_size, KEY1, strlen(KEY1)));
703 OK_(_prepare_keyfile(KEYFILE1, KEY1));
704 OK_(_prepare_keyfile(KEYFILE2, KEY2));
705 EQ_(2, crypt_keyslot_add_by_keyfile(cd, 2, KEYFILE1, 0, KEYFILE2, 0));
706 FAIL_(crypt_activate_by_keyfile(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, strlen(KEY2)-1, 0), "key mismatch");
707 EQ_(2, crypt_activate_by_keyfile(cd, NULL, CRYPT_ANY_SLOT, KEYFILE2, 0, 0));
708 EQ_(2, crypt_activate_by_keyfile(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, 0, 0));
709 OK_(crypt_keyslot_destroy(cd, 1));
710 OK_(crypt_keyslot_destroy(cd, 2));
711 OK_(crypt_deactivate(cd, CDEVICE_2));
714 FAIL_(crypt_keyslot_add_by_volume_key(cd, 7, key, key_size, passphrase, strlen(passphrase)), "slot used");
716 FAIL_(crypt_keyslot_add_by_volume_key(cd, 6, key, key_size, passphrase, strlen(passphrase)), "key mismatch");
718 EQ_(6, crypt_keyslot_add_by_volume_key(cd, 6, key, key_size, passphrase, strlen(passphrase)));
719 EQ_(CRYPT_SLOT_ACTIVE, crypt_keyslot_status(cd, 6));
721 FAIL_(crypt_keyslot_destroy(cd, 8), "invalid keyslot");
722 FAIL_(crypt_keyslot_destroy(cd, CRYPT_ANY_SLOT), "invalid keyslot");
723 FAIL_(crypt_keyslot_destroy(cd, 0), "keyslot not used");
724 OK_(crypt_keyslot_destroy(cd, 7));
725 EQ_(CRYPT_SLOT_INACTIVE, crypt_keyslot_status(cd, 7));
726 EQ_(CRYPT_SLOT_ACTIVE_LAST, crypt_keyslot_status(cd, 6));
728 EQ_(6, crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)));
729 OK_(crypt_volume_key_verify(cd, key2, key_size));
731 OK_(memcmp(key, key2, key_size));
732 OK_(strcmp(cipher, crypt_get_cipher(cd)));
733 OK_(strcmp(cipher_mode, crypt_get_cipher_mode(cd)));
734 EQ_(key_size, crypt_get_volume_key_size(cd));
735 EQ_(4096, crypt_get_data_offset(cd));
736 OK_(strcmp(DEVICE_2, crypt_get_device_name(cd)));
739 crypt_set_log_callback(cd, &new_log, NULL);
741 OK_(!(global_lines != 0));
742 crypt_set_log_callback(cd, NULL, NULL);
745 FAIL_(crypt_set_uuid(cd, "blah"), "wrong UUID format");
746 OK_(crypt_set_uuid(cd, DEVICE_TEST_UUID));
747 OK_(strcmp(DEVICE_TEST_UUID, crypt_get_uuid(cd)));
749 FAIL_(crypt_deactivate(cd, CDEVICE_2), "not active");
753 // Check that gcrypt is properly initialised in format
754 static void NonFIPSAlg(void)
756 struct crypt_device *cd;
757 struct crypt_params_luks1 params = {
761 size_t key_size = 128;
762 char *cipher = "aes";
763 char *cipher_mode = "cbc-essiv:sha256";
765 if (!gcrypt_compatible) {
766 printf("WARNING: old libgcrypt, skipping test.\n");
769 OK_(crypt_init(&cd, DEVICE_2));
770 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
773 FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms),
774 "MD5 unsupported, too short");
779 static void _gcrypt_compatible()
784 if (!(f = popen("libgcrypt-config --version", "r")))
787 if (fscanf(f, "%d.%d.%d", &maj, &min, &patch) == 3 &&
788 maj >= 1 && min >= 4)
789 gcrypt_compatible = 1;
791 printf("libgcrypt version %d.%d.%d detected.\n", maj, min, patch);
797 int main (int argc, char *argv[])
802 printf("You must be root to run this test.\n");
806 for (i = 1; i < argc; i++) {
807 if (!strcmp("-v", argv[i]) || !strcmp("--verbose", argv[i]))
809 else if (!strcmp("--debug", argv[i]))
810 _debug = _verbose = 1;
815 _gcrypt_compatible();
817 crypt_set_debug_level(_debug ? CRYPT_DEBUG_ALL : CRYPT_DEBUG_NONE);
819 RUN_(NonFIPSAlg, "Crypto is properly initialised in format"); //must be the first!
820 RUN_(LuksUUID, "luksUUID API call");
821 RUN_(IsLuks, "isLuks API call");
822 RUN_(LuksOpen, "luksOpen API call");
823 RUN_(query_device, "crypt_query_device API call");
824 RUN_(remove_device, "crypt_remove_device API call");
825 RUN_(LuksFormat, "luksFormat API call");
826 RUN_(LuksKeyGame, "luksAddKey, RemoveKey, KillSlot API calls");
827 RUN_(DeviceResizeGame, "regular crypto, resize calls");
829 RUN_(AddDevicePlain, "plain device API creation exercise");
830 RUN_(AddDeviceLuks, "Format and use LUKS device");
831 RUN_(UseLuksDevice, "Use pre-formated LUKS device");
832 RUN_(SuspendDevice, "Suspend/Resume test");