2 * cryptsetup library API check functions
4 * Copyright (C) 2009-2012 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2009-2012, Milan Broz
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2 as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 #include <sys/ioctl.h>
31 #include <libdevmapper.h>
34 #include "libcryptsetup.h"
35 #include "utils_loop.h"
37 #define DMDIR "/dev/mapper/"
39 #define DEVICE_1_UUID "28632274-8c8a-493f-835b-da802e1c576b"
40 #define DEVICE_EMPTY_name "crypt_zero"
41 #define DEVICE_EMPTY DMDIR DEVICE_EMPTY_name
42 #define DEVICE_ERROR_name "crypt_error"
43 #define DEVICE_ERROR DMDIR DEVICE_ERROR_name
45 #define CDEVICE_1 "ctest1"
46 #define CDEVICE_2 "ctest2"
47 #define CDEVICE_WRONG "O_o"
48 #define H_DEVICE "head_ok"
49 #define H_DEVICE_WRONG "head_wr"
50 #define L_DEVICE_1S "luks_onesec"
51 #define L_DEVICE_0S "luks_zerosec"
52 #define L_DEVICE_WRONG "luks_wr"
53 #define L_DEVICE_OK "luks_ok"
54 #define EVL_HEADER_1 "evil_hdr-luks_hdr_damage"
55 #define EVL_HEADER_2 "evil_hdr-payload_overwrite"
56 #define EVL_HEADER_3 "evil_hdr-stripes_payload_dmg"
57 #define EVL_HEADER_4 "evil_hdr-small_luks_device"
58 #define VALID_HEADER "valid_header_file"
59 #define BACKUP_FILE "csetup_backup_file"
60 #define IMAGE1 "compatimage.img"
61 #define IMAGE_EMPTY "empty.img"
63 #define KEYFILE1 "key1.file"
64 #define KEY1 "compatkey"
66 #define KEYFILE2 "key2.file"
67 #define KEY2 "0123456789abcdef"
69 #define PASSPHRASE "blabla"
71 #define DEVICE_TEST_UUID "12345678-1234-1234-1234-123456789abc"
73 #define DEVICE_WRONG "/dev/Ooo_"
74 #define DEVICE_CHAR "/dev/zero"
75 #define THE_LFILE_TEMPLATE "cryptsetup-tstlp.XXXXXX"
77 #define SECTOR_SHIFT 9L
78 #define SECTOR_SIZE 512
79 #define TST_LOOP_FILE_SIZE (((1<<20)*50)>>SECTOR_SHIFT)
80 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
81 #define DIV_ROUND_UP_MODULO(n,d) (DIV_ROUND_UP(n,d)*(d))
82 #define LUKS_PHDR_SIZE_B 1024
84 static int _debug = 0;
85 static int _verbose = 1;
87 static char global_log[4096];
88 static int global_lines = 0;
90 static char *DEVICE_1 = NULL;
91 static char *DEVICE_2 = NULL;
92 static char *THE_LOOP_DEV = NULL;
94 static char *tmp_file_1 = NULL;
95 static char *test_loop_file = NULL;
96 static uint64_t t_dev_offset = 0;
98 static int _system(const char*, int);
102 static int device_size(const char *device, uint64_t *size)
106 devfd = open(device, O_RDONLY);
110 if (ioctl(devfd, BLKGETSIZE64, size) < 0)
116 static int get_luks_offsets(int metadata_device,
118 unsigned int alignpayload_sec,
119 unsigned int alignoffset_sec,
120 uint64_t *r_header_size,
121 uint64_t *r_payload_offset)
124 uint64_t current_sector;
125 uint32_t sectors_per_stripes_set;
130 sectors_per_stripes_set = DIV_ROUND_UP(keylength*LUKS_STRIPES, SECTOR_SIZE);
131 printf("sectors_per_stripes %" PRIu32 "\n", sectors_per_stripes_set);
132 current_sector = DIV_ROUND_UP_MODULO(DIV_ROUND_UP(LUKS_PHDR_SIZE_B, SECTOR_SIZE),
133 LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE);
134 for(i=0;i < (LUKS_NUMKEYS - 1);i++)
135 current_sector = DIV_ROUND_UP_MODULO(current_sector + sectors_per_stripes_set,
136 LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE);
138 *r_header_size = current_sector + sectors_per_stripes_set;
140 current_sector = DIV_ROUND_UP_MODULO(current_sector + sectors_per_stripes_set,
141 LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE);
143 if (r_payload_offset) {
145 *r_payload_offset = alignpayload_sec;
147 *r_payload_offset = DIV_ROUND_UP_MODULO(current_sector, alignpayload_sec)
155 * Creates dm-linear target over the test loop device. Offset is held in
156 * global variables so that size can be tested whether it fits into remaining
157 * size of the loop device or not
159 static int create_dmdevice_over_loop(const char *dm_name, const uint64_t size)
165 if(device_size(THE_LOOP_DEV, &r_size) < 0 || r_size <= t_dev_offset || !size)
167 if ((r_size - t_dev_offset) < size) {
168 printf("No enough space on backing loop device\n.");
171 snprintf(cmd, sizeof(cmd),
172 "dmsetup create %s --table \"0 %" PRIu64 " linear %s %" PRIu64 "\"",
173 dm_name, size, THE_LOOP_DEV, t_dev_offset);
174 if (!(r = _system(cmd, 1))) {
175 t_dev_offset += size;
180 // TODO some utility to remove dmdevice over the loop file
182 // Get key from kernel dm mapping table using dm-ioctl
183 static int _get_key_dm(const char *name, char *buffer, unsigned int buffer_size)
187 uint64_t start, length;
188 char *target_type, *key, *params;
192 if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
194 if (!dm_task_set_name(dmt, name))
196 if (!dm_task_run(dmt))
198 if (!dm_task_get_info(dmt, &dmi))
203 next = dm_get_next_target(dmt, next, &start, &length, &target_type, ¶ms);
204 if (!target_type || strcmp(target_type, "crypt") != 0)
207 (void)strsep(¶ms, " "); /* rcipher */
208 key = strsep(¶ms, " ");
210 if (buffer_size <= strlen(key))
213 strncpy(buffer, key, buffer_size);
217 dm_task_destroy(dmt);
222 static int _prepare_keyfile(const char *name, const char *passphrase, int size)
226 fd = open(name, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR);
228 r = write(fd, passphrase, size);
233 return r == size ? 0 : 1;
236 static void _remove_keyfiles(void)
242 // Decode key from its hex representation
243 static int crypt_decode_key(char *key, const char *hex, unsigned int size)
251 for (i = 0; i < size; i++) {
255 key[i] = (unsigned char)strtoul(buffer, &endp, 16);
257 if (endp != &buffer[2])
267 static void cmdLineLog(int level, const char *msg)
269 strncat(global_log, msg, sizeof(global_log) - strlen(global_log));
273 static void new_log(int level, const char *msg, void *usrptr)
276 printf("LOG: %s", msg);
277 cmdLineLog(level, msg);
280 static void reset_log(void)
282 memset(global_log, 0, sizeof(global_log));
286 static int _system(const char *command, int warn)
290 printf("Running system: %s\n", command);
291 if ((r=system(command)) < 0 && warn)
292 printf("System command failed: %s", command);
296 static void _cleanup_dmdevices(void)
300 if (!stat(DMDIR H_DEVICE, &st)) {
301 _system("dmsetup remove " H_DEVICE, 0);
303 if (!stat(DMDIR H_DEVICE_WRONG, &st)) {
304 _system("dmsetup remove " H_DEVICE_WRONG, 0);
306 if (!stat(DMDIR L_DEVICE_0S, &st)) {
307 _system("dmsetup remove " L_DEVICE_0S, 0);
309 if (!stat(DMDIR L_DEVICE_1S, &st)) {
310 _system("dmsetup remove " L_DEVICE_1S, 0);
312 if (!stat(DMDIR L_DEVICE_WRONG, &st)) {
313 _system("dmsetup remove " L_DEVICE_WRONG, 0);
315 if (!stat(DMDIR L_DEVICE_OK, &st)) {
316 _system("dmsetup remove " L_DEVICE_OK, 0);
322 static void _cleanup(void)
326 //_system("udevadm settle", 0);
328 if (!stat(DMDIR CDEVICE_1, &st))
329 _system("dmsetup remove " CDEVICE_1, 0);
331 if (!stat(DMDIR CDEVICE_2, &st))
332 _system("dmsetup remove " CDEVICE_2, 0);
334 if (!stat(DEVICE_EMPTY, &st))
335 _system("dmsetup remove " DEVICE_EMPTY_name, 0);
337 if (!stat(DEVICE_ERROR, &st))
338 _system("dmsetup remove " DEVICE_ERROR_name, 0);
340 _cleanup_dmdevices();
342 if (crypt_loop_device(THE_LOOP_DEV))
343 crypt_loop_detach(THE_LOOP_DEV);
345 if (crypt_loop_device(DEVICE_1))
346 crypt_loop_detach(DEVICE_1);
348 if (crypt_loop_device(DEVICE_2))
349 crypt_loop_detach(DEVICE_2);
351 _system("rm -f " IMAGE_EMPTY, 0);
352 _system("rm -f " IMAGE1, 0);
354 remove(test_loop_file);
357 remove(EVL_HEADER_1);
358 remove(EVL_HEADER_2);
359 remove(EVL_HEADER_3);
360 remove(EVL_HEADER_4);
361 remove(VALID_HEADER);
367 free(test_loop_file);
373 static int _setup(void)
378 test_loop_file = strdup(THE_LFILE_TEMPLATE);
379 if ((fd=mkstemp(test_loop_file)) == -1) {
380 printf("cannot create temporary file with template %s\n", test_loop_file);
384 snprintf(cmd, sizeof(cmd), "dd if=/dev/zero of=%s bs=%d count=%d 2>/dev/null",
385 test_loop_file, SECTOR_SIZE, TST_LOOP_FILE_SIZE);
390 THE_LOOP_DEV = crypt_loop_get_device();
392 printf("Cannot find free loop device.\n");
395 if (crypt_loop_device(THE_LOOP_DEV)) {
396 fd = crypt_loop_attach(THE_LOOP_DEV, test_loop_file, 0, 0, &ro);
400 tmp_file_1 = strdup(THE_LFILE_TEMPLATE);
401 if ((fd=mkstemp(tmp_file_1)) == -1) {
402 printf("cannot create temporary file with template %s\n", tmp_file_1);
406 snprintf(cmd, sizeof(cmd), "dd if=/dev/zero of=%s bs=%d count=%d 2>/dev/null",
407 tmp_file_1, SECTOR_SIZE, 10);
411 _system("dmsetup create " DEVICE_EMPTY_name " --table \"0 10000 zero\"", 1);
412 _system("dmsetup create " DEVICE_ERROR_name " --table \"0 10000 error\"", 1);
414 DEVICE_1 = crypt_loop_get_device();
416 printf("Cannot find free loop device.\n");
419 if (crypt_loop_device(DEVICE_1)) {
420 _system(" [ ! -e " IMAGE1 " ] && bzip2 -dk " IMAGE1 ".bz2", 1);
421 fd = crypt_loop_attach(DEVICE_1, IMAGE1, 0, 0, &ro);
425 DEVICE_2 = crypt_loop_get_device();
427 printf("Cannot find free loop device.\n");
430 if (crypt_loop_device(DEVICE_2)) {
431 _system("dd if=/dev/zero of=" IMAGE_EMPTY " bs=1M count=4 2>/dev/null", 1);
432 fd = crypt_loop_attach(DEVICE_2, IMAGE_EMPTY, 0, 0, &ro);
435 /* Keymaterial offset is less than 8 sectors */
436 _system(" [ ! -e " EVL_HEADER_1 " ] && bzip2 -dk " EVL_HEADER_1 ".bz2", 1);
437 /* keymaterial offset aims into payload area */
438 _system(" [ ! -e " EVL_HEADER_2 " ] && bzip2 -dk " EVL_HEADER_2 ".bz2", 1);
439 /* keymaterial offset is valid, number of stripes causes payload area to be overwriten */
440 _system(" [ ! -e " EVL_HEADER_3 " ] && bzip2 -dk " EVL_HEADER_3 ".bz2", 1);
441 /* luks device header for data and header on same device. payloadOffset is greater than
442 * device size (crypt_load() test) */
443 _system(" [ ! -e " EVL_HEADER_4 " ] && bzip2 -dk " EVL_HEADER_4 ".bz2", 1);
444 /* valid header: payloadOffset=4096, key_size=32,
445 * volume_key = bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a */
446 _system(" [ ! -e " VALID_HEADER " ] && bzip2 -dk " VALID_HEADER ".bz2", 1);
448 /* Prepare tcrypt images */
449 _system(" [ ! -d tcrypt-images ] && tar xjf tcrypt-images.tar.bz2 2>/dev/null", 1);
451 _system("modprobe dm-crypt", 0);
452 _system("modprobe dm-verity", 0);
456 static void check_ok(int status, int line, const char *func)
461 crypt_get_error(buf, sizeof(buf));
462 printf("FAIL line %d [%s]: code %d, %s\n", line, func, status, buf);
468 static void check_ko(int status, int line, const char *func)
472 memset(buf, 0, sizeof(buf));
473 crypt_get_error(buf, sizeof(buf));
475 printf("FAIL line %d [%s]: code %d, %s\n", line, func, status, buf);
479 printf(" => errno %d, errmsg: %s\n", status, buf);
482 static void check_equal(int line, const char *func, int64_t x, int64_t y)
484 printf("FAIL line %d [%s]: expected equal values differs: %"
485 PRIi64 " != %" PRIi64 "\n", line, func, x, y);
490 static void xlog(const char *msg, const char *tst, const char *func, int line, const char *txt)
494 printf(" [%s,%s:%d] %s [%s]\n", msg, func, line, tst, txt);
496 printf(" [%s,%s:%d] %s\n", msg, func, line, tst);
500 /* crypt_device context must be "cd" to parse error properly here */
501 #define OK_(x) do { xlog("(success)", #x, __FUNCTION__, __LINE__, NULL); \
502 check_ok((x), __LINE__, __FUNCTION__); \
504 #define FAIL_(x, y) do { xlog("(fail) ", #x, __FUNCTION__, __LINE__, y); \
505 check_ko((x), __LINE__, __FUNCTION__); \
507 #define EQ_(x, y) do { xlog("(equal) ", #x " == " #y, __FUNCTION__, __LINE__, NULL); \
508 int64_t _x = (x), _y = (y); \
509 if (_x != _y) check_equal(__LINE__, __FUNCTION__, _x, _y); \
511 #define RUN_(x, y) do { printf("%s: %s\n", #x, (y)); x(); } while (0)
513 static void AddDevicePlain(void)
515 struct crypt_device *cd;
516 struct crypt_params_plain params = {
523 char key[128], key2[128], path[128];
525 const char *passphrase = PASSPHRASE;
526 // hashed hex version of PASSPHRASE
527 const char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
528 size_t key_size = strlen(mk_hex) / 2;
529 const char *cipher = "aes";
530 const char *cipher_mode = "cbc-essiv:sha256";
532 uint64_t size, r_size;
534 crypt_decode_key(key, mk_hex, key_size);
535 FAIL_(crypt_init(&cd, ""), "empty device string");
536 FAIL_(crypt_init(&cd, DEVICE_WRONG), "nonexistent device name ");
537 FAIL_(crypt_init(&cd, DEVICE_CHAR), "character device as backing device");
538 OK_(crypt_init(&cd, tmp_file_1));
541 // test crypt_format, crypt_get_cipher, crypt_get_cipher_mode, crypt_get_volume_key_size
542 OK_(crypt_init(&cd,DEVICE_1));
545 FAIL_(crypt_format(cd,CRYPT_PLAIN,NULL,cipher_mode,NULL,NULL,key_size,¶ms),"cipher param is null");
546 FAIL_(crypt_format(cd,CRYPT_PLAIN,cipher,NULL,NULL,NULL,key_size,¶ms),"cipher_mode param is null");
547 OK_(crypt_format(cd,CRYPT_PLAIN,cipher,cipher_mode,NULL,NULL,key_size,¶ms));
548 OK_(strcmp(cipher_mode,crypt_get_cipher_mode(cd)));
549 OK_(strcmp(cipher,crypt_get_cipher(cd)));
550 EQ_((int)key_size, crypt_get_volume_key_size(cd));
551 EQ_(params.skip, crypt_get_iv_offset(cd));
552 EQ_(params.offset, crypt_get_data_offset(cd));
557 FAIL_(crypt_set_uuid(cd,DEVICE_1_UUID),"can't set uuid to plain device");
561 // default is "plain" hash - no password hash
562 OK_(crypt_init(&cd, DEVICE_1));
563 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, NULL));
564 FAIL_(crypt_activate_by_volume_key(cd, NULL, key, key_size, 0), "cannot verify key with plain");
565 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
566 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
567 OK_(crypt_deactivate(cd, CDEVICE_1));
570 // test boundaries in offset parameter
571 device_size(DEVICE_1,&size);
573 // zero sectors length
574 params.offset = size >> SECTOR_SHIFT;
575 OK_(crypt_init(&cd, DEVICE_1));
576 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
577 EQ_(crypt_get_data_offset(cd),params.offset);
578 // device size is 0 sectors
579 FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0), "invalid device size (0 blocks)");
580 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
581 // data part of crypt device is of 1 sector size
582 params.offset = (size >> SECTOR_SHIFT) - 1;
585 OK_(crypt_init(&cd, DEVICE_1));
586 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
587 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
588 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
589 snprintf(path, sizeof(path), "%s/%s", crypt_get_dir(), CDEVICE_1);
590 if (device_size(path, &r_size) >= 0)
591 EQ_(r_size>>SECTOR_SHIFT, 1);
592 OK_(crypt_deactivate(cd, CDEVICE_1));
595 // size > device_size
597 params.size = (size >> SECTOR_SHIFT) + 1;
598 crypt_init(&cd, DEVICE_1);
599 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
600 FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0),"Device too small");
601 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
604 // offset == device_size (autodetect size)
605 params.offset = (size >> SECTOR_SHIFT);
607 crypt_init(&cd, DEVICE_1);
608 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
609 FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0),"Device too small");
610 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
613 // offset == device_size (user defined size)
614 params.offset = (size >> SECTOR_SHIFT);
616 crypt_init(&cd, DEVICE_1);
617 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
618 FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0),"Device too small");
619 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
622 // offset+size > device_size
624 params.size = (size >> SECTOR_SHIFT) - params.offset + 1;
625 crypt_init(&cd, DEVICE_1);
626 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
627 FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0),"Offset and size are beyond device real size");
628 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
631 // offset+size == device_size
633 params.size = (size >> SECTOR_SHIFT) - params.offset;
634 crypt_init(&cd, DEVICE_1);
635 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
636 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
637 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
638 if (!device_size(path, &r_size))
639 EQ_((r_size >> SECTOR_SHIFT),params.size);
640 OK_(crypt_deactivate(cd,CDEVICE_1));
643 params.hash = "sha1";
648 // Now use hashed password
649 OK_(crypt_init(&cd, DEVICE_1));
650 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
651 FAIL_(crypt_activate_by_passphrase(cd, NULL, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0),
652 "cannot verify passphrase with plain" );
653 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
655 // device status check
656 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
657 snprintf(path, sizeof(path), "%s/%s", crypt_get_dir(), CDEVICE_1);
658 fd = open(path, O_RDONLY);
659 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_BUSY);
660 FAIL_(crypt_deactivate(cd, CDEVICE_1), "Device is busy");
662 OK_(crypt_deactivate(cd, CDEVICE_1));
663 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
666 // crypt_init_by_name_and_header
667 OK_(crypt_init(&cd,DEVICE_1));
668 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
669 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
672 FAIL_(crypt_init_by_name_and_header(&cd, CDEVICE_1, H_DEVICE),"can't init plain device by header device");
673 OK_(crypt_init_by_name(&cd, CDEVICE_1));
674 OK_(strcmp(cipher_mode,crypt_get_cipher_mode(cd)));
675 OK_(strcmp(cipher,crypt_get_cipher(cd)));
676 EQ_((int)key_size, crypt_get_volume_key_size(cd));
677 EQ_(params.skip, crypt_get_iv_offset(cd));
678 EQ_(params.offset, crypt_get_data_offset(cd));
679 OK_(crypt_deactivate(cd, CDEVICE_1));
682 OK_(crypt_init(&cd,DEVICE_1));
683 OK_(crypt_format(cd,CRYPT_PLAIN,cipher,cipher_mode,NULL,NULL,key_size,¶ms));
687 // crypt_set_data_device
688 FAIL_(crypt_set_data_device(cd,H_DEVICE),"can't set data device for plain device");
691 OK_(strcmp(crypt_get_type(cd),CRYPT_PLAIN));
693 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
694 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
697 OK_(crypt_resize(cd,CDEVICE_1,size>>SECTOR_SHIFT)); // same size
698 if (!device_size(path,&r_size))
702 FAIL_(crypt_resize(cd, CDEVICE_1, ULLONG_MAX),"Backing device is too small");
703 FAIL_(crypt_resize(cd, CDEVICE_1, (size>>SECTOR_SHIFT)+1),"crypt device overlaps backing device");
706 OK_(crypt_resize(cd,CDEVICE_1, 123));
707 if (!device_size(path,&r_size))
708 EQ_(r_size>>SECTOR_SHIFT, 123);
709 OK_(crypt_resize(cd,CDEVICE_1,0)); // full size (autodetect)
710 if (!device_size(path,&r_size))
712 OK_(crypt_deactivate(cd,CDEVICE_1));
713 EQ_(crypt_status(cd,CDEVICE_1),CRYPT_INACTIVE);
717 OK_(crypt_init(&cd,DEVICE_1));
719 params.size = (size>>SECTOR_SHIFT) - params.offset - 10;
720 OK_(crypt_format(cd,CRYPT_PLAIN,cipher,cipher_mode,NULL,NULL,key_size,¶ms));
721 OK_(crypt_activate_by_volume_key(cd,CDEVICE_1,key,key_size,0));
722 if (!device_size(path,&r_size))
723 EQ_(r_size>>SECTOR_SHIFT, params.size);
724 // resize to fill remaining capacity
725 OK_(crypt_resize(cd,CDEVICE_1,params.size + 10));
726 if (!device_size(path,&r_size))
727 EQ_(r_size>>SECTOR_SHIFT, params.size + 10);
729 // 1 sector beyond real size
730 FAIL_(crypt_resize(cd,CDEVICE_1,params.size + 11), "new device size overlaps backing device"); // with respect to offset
731 if (!device_size(path,&r_size))
732 EQ_(r_size>>SECTOR_SHIFT, params.size + 10);
733 EQ_(crypt_status(cd,CDEVICE_1),CRYPT_ACTIVE);
734 fd = open(path, O_RDONLY);
738 // resize to minimal size
739 OK_(crypt_resize(cd,CDEVICE_1, 1)); // minimal device size
740 if (!device_size(path,&r_size))
741 EQ_(r_size>>SECTOR_SHIFT, 1);
742 // use size of backing device (autodetect with respect to offset)
743 OK_(crypt_resize(cd,CDEVICE_1,0));
744 if (!device_size(path,&r_size))
745 EQ_(r_size>>SECTOR_SHIFT, (size >> SECTOR_SHIFT)- 42);
746 OK_(crypt_deactivate(cd,CDEVICE_1));
751 OK_(crypt_init(&cd,DEVICE_1));
752 OK_(crypt_format(cd,CRYPT_PLAIN,cipher,cipher_mode,NULL,NULL,key_size,¶ms));
753 OK_(crypt_activate_by_volume_key(cd,CDEVICE_1,key,key_size,0));
755 // suspend/resume tests
756 FAIL_(crypt_suspend(cd,CDEVICE_1),"cannot suspend plain device");
757 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
758 FAIL_(crypt_resume_by_passphrase(cd,CDEVICE_1,CRYPT_ANY_SLOT,passphrase, strlen(passphrase)),"cannot resume plain device");
759 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
761 // retrieve volume key check
762 memset(key2, 0, key_size);
765 FAIL_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)), "small buffer");
767 OK_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)));
769 OK_(memcmp(key, key2, key_size));
770 OK_(strcmp(cipher, crypt_get_cipher(cd)));
771 OK_(strcmp(cipher_mode, crypt_get_cipher_mode(cd)));
772 EQ_((int)key_size, crypt_get_volume_key_size(cd));
773 EQ_(0, crypt_get_data_offset(cd));
774 OK_(crypt_deactivate(cd, CDEVICE_1));
777 OK_(_prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
778 OK_(_prepare_keyfile(KEYFILE2, KEY2, strlen(KEY2)));
779 FAIL_(crypt_activate_by_keyfile(cd, NULL, CRYPT_ANY_SLOT, KEYFILE1, 0, 0), "cannot verify key with plain");
780 EQ_(0, crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
781 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
782 OK_(crypt_deactivate(cd, CDEVICE_1));
783 FAIL_(crypt_activate_by_keyfile_offset(cd, NULL, CRYPT_ANY_SLOT, KEYFILE1, 0, strlen(KEY1) + 1, 0), "cannot seek");
784 EQ_(0, crypt_activate_by_keyfile_offset(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0, 0));
785 OK_(crypt_deactivate(cd, CDEVICE_1));
789 OK_(crypt_init(&cd,DEVICE_1));
790 OK_(crypt_format(cd,CRYPT_PLAIN,cipher,cipher_mode,NULL,NULL,key_size,¶ms));
793 FAIL_(crypt_keyslot_add_by_passphrase(cd,CRYPT_ANY_SLOT,passphrase,strlen(passphrase),passphrase,strlen(passphrase)), "can't add keyslot to plain device");
794 FAIL_(crypt_keyslot_add_by_volume_key(cd,CRYPT_ANY_SLOT ,key,key_size,passphrase,strlen(passphrase)),"can't add keyslot to plain device");
795 FAIL_(crypt_keyslot_add_by_keyfile(cd,CRYPT_ANY_SLOT,KEYFILE1,strlen(KEY1),KEYFILE2,strlen(KEY2)),"can't add keyslot to plain device");
796 FAIL_(crypt_keyslot_destroy(cd,1),"can't manipulate keyslots on plain device");
797 EQ_(crypt_keyslot_status(cd, 0), CRYPT_SLOT_INVALID);
803 #define CALLBACK_ERROR "calback_error xyz"
804 static int pass_callback_err(const char *msg, char *buf, size_t length, void *usrptr)
806 struct crypt_device *cd = usrptr;
812 crypt_log(cd, CRYPT_LOG_ERROR, CALLBACK_ERROR);
816 static int pass_callback_ok(const char *msg, char *buf, size_t length, void *usrptr)
820 strcpy(buf, PASSPHRASE);
824 static void CallbacksTest(void)
826 struct crypt_device *cd;
827 struct crypt_params_plain params = {
833 size_t key_size = 256 / 8;
834 const char *cipher = "aes";
835 const char *cipher_mode = "cbc-essiv:sha256";
836 const char *passphrase = PASSPHRASE;
837 char buf1[256] = {0}, buf2[256] = {0};
839 OK_(crypt_init(&cd, DEVICE_1));
840 crypt_set_log_callback(cd, &new_log, NULL);
841 //crypt_set_log_callback(cd, NULL, NULL);
843 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
845 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
846 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
847 OK_(crypt_deactivate(cd, CDEVICE_1));
850 crypt_set_password_callback(cd, pass_callback_err, cd);
851 FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, NULL, 0, 0), "callback fails");
852 EQ_(strncmp(global_log, CALLBACK_ERROR, strlen(CALLBACK_ERROR)), 0);
854 crypt_set_password_callback(cd, pass_callback_ok, NULL);
855 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, NULL, 0, 0));
856 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
857 OK_(crypt_deactivate(cd, CDEVICE_1));
859 // Check error reporting.
860 // This must fail and create error message
861 crypt_deactivate(cd, CDEVICE_1);
863 // Here context must be the same
864 crypt_get_error(buf1, sizeof(buf1));
865 crypt_last_error(cd, buf2, sizeof(buf2));
868 OK_(strcmp(buf1, buf2));
870 crypt_get_error(buf1, sizeof(buf1));
871 crypt_last_error(cd, buf2, sizeof(buf2));
878 static void UseLuksDevice(void)
880 struct crypt_device *cd;
884 OK_(crypt_init(&cd, DEVICE_1));
885 OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
886 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
887 OK_(crypt_activate_by_passphrase(cd, NULL, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0));
888 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0));
889 FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0), "already open");
890 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
891 OK_(crypt_deactivate(cd, CDEVICE_1));
892 FAIL_(crypt_deactivate(cd, CDEVICE_1), "no such device");
895 OK_(strcmp("aes", crypt_get_cipher(cd)));
896 OK_(strcmp("cbc-essiv:sha256", crypt_get_cipher_mode(cd)));
897 OK_(strcmp(DEVICE_1_UUID, crypt_get_uuid(cd)));
898 EQ_((int)key_size, crypt_get_volume_key_size(cd));
899 EQ_(1032, crypt_get_data_offset(cd));
901 EQ_(0, crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key, &key_size, KEY1, strlen(KEY1)));
902 OK_(crypt_volume_key_verify(cd, key, key_size));
903 OK_(crypt_activate_by_volume_key(cd, NULL, key, key_size, 0));
904 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
905 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
906 OK_(crypt_deactivate(cd, CDEVICE_1));
909 FAIL_(crypt_volume_key_verify(cd, key, key_size), "key mismatch");
910 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0), "key mismatch");
914 static void SuspendDevice(void)
917 struct crypt_device *cd;
919 OK_(crypt_init(&cd, DEVICE_1));
920 OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
921 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0));
923 suspend_status = crypt_suspend(cd, CDEVICE_1);
924 if (suspend_status == -ENOTSUP) {
925 printf("WARNING: Suspend/Resume not supported, skipping test.\n");
929 FAIL_(crypt_suspend(cd, CDEVICE_1), "already suspended");
931 FAIL_(crypt_resume_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1)-1), "wrong key");
932 OK_(crypt_resume_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1)));
933 FAIL_(crypt_resume_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1)), "not suspended");
935 OK_(_prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
936 OK_(crypt_suspend(cd, CDEVICE_1));
937 FAIL_(crypt_resume_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1 "blah", 0), "wrong keyfile");
938 FAIL_(crypt_resume_by_keyfile_offset(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 1, 0), "wrong key");
939 OK_(crypt_resume_by_keyfile_offset(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
940 FAIL_(crypt_resume_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0), "not suspended");
943 OK_(crypt_deactivate(cd, CDEVICE_1));
947 static void AddDeviceLuks(void)
949 struct crypt_device *cd;
950 struct crypt_params_luks1 params = {
952 .data_alignment = 2048, // 4M, data offset will be 4096
953 .data_device = DEVICE_2
955 char key[128], key2[128];
957 const char *passphrase = "blabla", *passphrase2 = "nsdkFI&Y#.sd";
958 const char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
959 size_t key_size = strlen(mk_hex) / 2;
960 const char *cipher = "aes";
961 const char *cipher_mode = "cbc-essiv:sha256";
962 uint64_t r_payload_offset, r_header_size, r_size_1;
964 crypt_decode_key(key, mk_hex, key_size);
967 OK_(get_luks_offsets(1, key_size, 0, 0, &r_header_size, &r_payload_offset));
968 OK_(create_dmdevice_over_loop(H_DEVICE, r_header_size));
969 OK_(create_dmdevice_over_loop(H_DEVICE_WRONG, r_header_size - 1));
972 OK_(crypt_init(&cd, DMDIR H_DEVICE_WRONG));
973 params.data_alignment = 0;
974 FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms), "Not enough space for keyslots material");
977 // test payload_offset = 0 for encrypted device with external header device
978 OK_(crypt_init(&cd, DMDIR H_DEVICE));
979 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
980 EQ_(crypt_get_data_offset(cd), 0);
983 params.data_alignment = 0;
984 params.data_device = NULL;
986 // test payload_offset = 0. format() should look up alignment offset from device topology
987 OK_(crypt_init(&cd, DEVICE_2));
988 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
989 OK_(!(crypt_get_data_offset(cd) > 0));
993 * test limit values for backing device size
995 params.data_alignment = 4096;
996 OK_(get_luks_offsets(0, key_size, params.data_alignment, 0, NULL, &r_payload_offset));
997 OK_(create_dmdevice_over_loop(L_DEVICE_0S, r_payload_offset));
998 OK_(create_dmdevice_over_loop(L_DEVICE_1S, r_payload_offset + 1));
999 //OK_(create_dmdevice_over_loop(L_DEVICE_WRONG, r_payload_offset - 1));
1000 OK_(create_dmdevice_over_loop(L_DEVICE_WRONG, 2050 - 1)); //FIXME last keyslot - 1 sector
1002 // 1 sector less than required
1003 OK_(crypt_init(&cd, DMDIR L_DEVICE_WRONG));
1004 FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms), "Device too small");
1007 // 0 sectors for encrypted area
1008 OK_(crypt_init(&cd, DMDIR L_DEVICE_0S));
1009 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1010 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0), "Encrypted area too small");
1013 // 1 sector for encrypted area
1014 OK_(crypt_init(&cd, DMDIR L_DEVICE_1S));
1015 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1016 EQ_(crypt_get_data_offset(cd), params.data_alignment);
1017 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1018 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1019 OK_(device_size(DMDIR CDEVICE_1, &r_size_1));
1020 EQ_(r_size_1, SECTOR_SIZE);
1021 OK_(crypt_deactivate(cd, CDEVICE_1));
1022 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
1023 // restrict format only to empty context
1024 FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms), "Context is already formated");
1025 FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, NULL), "Context is already formated");
1026 // change data device to wrong one
1027 OK_(crypt_set_data_device(cd, DMDIR L_DEVICE_0S));
1028 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0), "Device too small");
1029 OK_(crypt_set_data_device(cd, DMDIR L_DEVICE_1S));
1030 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1031 OK_(crypt_deactivate(cd, CDEVICE_1));
1034 params.data_alignment = 0;
1035 params.data_device = DEVICE_2;
1037 // generate keyslot material at the end of luks header
1038 OK_(crypt_init(&cd, DMDIR H_DEVICE));
1039 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1040 EQ_(crypt_keyslot_add_by_volume_key(cd, 7, key, key_size, passphrase, strlen(passphrase)), 7);
1041 EQ_(crypt_activate_by_passphrase(cd, CDEVICE_1, 7, passphrase, strlen(passphrase) ,0), 7);
1043 OK_(crypt_init_by_name_and_header(&cd, CDEVICE_1, DMDIR H_DEVICE));
1044 FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms), "Context is already formated");
1045 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1046 OK_(crypt_deactivate(cd, CDEVICE_1));
1049 params.data_alignment = 2048;
1050 params.data_device = NULL;
1052 // test uuid mismatch and _init_by_name_and_header
1053 OK_(crypt_init(&cd, DMDIR L_DEVICE_1S));
1054 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1055 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1057 params.data_alignment = 0;
1058 params.data_device = DEVICE_2;
1059 OK_(crypt_init(&cd, DMDIR H_DEVICE));
1060 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1062 // there we've got uuid mismatch
1063 OK_(crypt_init_by_name_and_header(&cd, CDEVICE_1, DMDIR H_DEVICE));
1064 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1065 OK_(!!crypt_get_type(cd));
1066 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0), "Device is active");
1067 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, key, key_size, 0), "Device is active");
1068 EQ_(crypt_status(cd, CDEVICE_2), CRYPT_INACTIVE);
1069 OK_(crypt_deactivate(cd, CDEVICE_1));
1072 params.data_device = NULL;
1074 OK_(crypt_init(&cd, DEVICE_2));
1075 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1077 // even with no keyslots defined it can be activated by volume key
1078 OK_(crypt_volume_key_verify(cd, key, key_size));
1079 OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, key, key_size, 0));
1080 EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
1081 OK_(crypt_deactivate(cd, CDEVICE_2));
1084 EQ_(7, crypt_keyslot_add_by_volume_key(cd, 7, key, key_size, passphrase, strlen(passphrase)));
1085 EQ_(CRYPT_SLOT_ACTIVE_LAST, crypt_keyslot_status(cd, 7));
1086 EQ_(7, crypt_activate_by_passphrase(cd, CDEVICE_2, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
1087 EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
1088 OK_(crypt_deactivate(cd, CDEVICE_2));
1090 crypt_set_iteration_time(cd, 1);
1091 EQ_(1, crypt_keyslot_add_by_volume_key(cd, 1, key, key_size, KEY1, strlen(KEY1)));
1092 OK_(_prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
1093 OK_(_prepare_keyfile(KEYFILE2, KEY2, strlen(KEY2)));
1094 EQ_(2, crypt_keyslot_add_by_keyfile(cd, 2, KEYFILE1, 0, KEYFILE2, 0));
1095 FAIL_(crypt_keyslot_add_by_keyfile_offset(cd, 3, KEYFILE1, 0, 1, KEYFILE2, 0, 1), "wrong key");
1096 EQ_(3, crypt_keyslot_add_by_keyfile_offset(cd, 3, KEYFILE1, 0, 0, KEYFILE2, 0, 1));
1097 EQ_(4, crypt_keyslot_add_by_keyfile_offset(cd, 4, KEYFILE2, 0, 1, KEYFILE1, 0, 1));
1098 FAIL_(crypt_activate_by_keyfile(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, strlen(KEY2)-1, 0), "key mismatch");
1099 EQ_(2, crypt_activate_by_keyfile(cd, NULL, CRYPT_ANY_SLOT, KEYFILE2, 0, 0));
1100 EQ_(3, crypt_activate_by_keyfile_offset(cd, NULL, CRYPT_ANY_SLOT, KEYFILE2, 0, 1, 0));
1101 EQ_(4, crypt_activate_by_keyfile_offset(cd, NULL, CRYPT_ANY_SLOT, KEYFILE1, 0, 1, 0));
1102 FAIL_(crypt_activate_by_keyfile_offset(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, strlen(KEY2), 2, 0), "not enough data");
1103 FAIL_(crypt_activate_by_keyfile_offset(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, 0, strlen(KEY2) + 1, 0), "cannot seek");
1104 FAIL_(crypt_activate_by_keyfile_offset(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, 0, 2, 0), "wrong key");
1105 EQ_(2, crypt_activate_by_keyfile(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, 0, 0));
1106 OK_(crypt_keyslot_destroy(cd, 1));
1107 OK_(crypt_keyslot_destroy(cd, 2));
1108 OK_(crypt_keyslot_destroy(cd, 3));
1109 OK_(crypt_keyslot_destroy(cd, 4));
1110 OK_(crypt_deactivate(cd, CDEVICE_2));
1113 FAIL_(crypt_keyslot_add_by_volume_key(cd, 7, key, key_size, passphrase, strlen(passphrase)), "slot used");
1115 FAIL_(crypt_keyslot_add_by_volume_key(cd, 6, key, key_size, passphrase, strlen(passphrase)), "key mismatch");
1117 EQ_(6, crypt_keyslot_add_by_volume_key(cd, 6, key, key_size, passphrase, strlen(passphrase)));
1118 EQ_(CRYPT_SLOT_ACTIVE, crypt_keyslot_status(cd, 6));
1120 FAIL_(crypt_keyslot_destroy(cd, 8), "invalid keyslot");
1121 FAIL_(crypt_keyslot_destroy(cd, CRYPT_ANY_SLOT), "invalid keyslot");
1122 FAIL_(crypt_keyslot_destroy(cd, 0), "keyslot not used");
1123 OK_(crypt_keyslot_destroy(cd, 7));
1124 EQ_(CRYPT_SLOT_INACTIVE, crypt_keyslot_status(cd, 7));
1125 EQ_(CRYPT_SLOT_ACTIVE_LAST, crypt_keyslot_status(cd, 6));
1127 EQ_(7, crypt_keyslot_change_by_passphrase(cd, 6, 7, passphrase, strlen(passphrase), passphrase2, strlen(passphrase2)));
1128 EQ_(CRYPT_SLOT_ACTIVE_LAST, crypt_keyslot_status(cd, 7));
1129 EQ_(7, crypt_activate_by_passphrase(cd, NULL, 7, passphrase2, strlen(passphrase2), 0));
1130 EQ_(6, crypt_keyslot_change_by_passphrase(cd, CRYPT_ANY_SLOT, 6, passphrase2, strlen(passphrase2), passphrase, strlen(passphrase)));
1132 EQ_(6, crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)));
1133 OK_(crypt_volume_key_verify(cd, key2, key_size));
1135 OK_(memcmp(key, key2, key_size));
1136 OK_(strcmp(cipher, crypt_get_cipher(cd)));
1137 OK_(strcmp(cipher_mode, crypt_get_cipher_mode(cd)));
1138 EQ_((int)key_size, crypt_get_volume_key_size(cd));
1139 EQ_(4096, crypt_get_data_offset(cd));
1140 OK_(strcmp(DEVICE_2, crypt_get_device_name(cd)));
1143 crypt_set_log_callback(cd, &new_log, NULL);
1144 OK_(crypt_dump(cd));
1145 OK_(!(global_lines != 0));
1146 crypt_set_log_callback(cd, NULL, NULL);
1149 FAIL_(crypt_set_uuid(cd, "blah"), "wrong UUID format");
1150 OK_(crypt_set_uuid(cd, DEVICE_TEST_UUID));
1151 OK_(strcmp(DEVICE_TEST_UUID, crypt_get_uuid(cd)));
1153 FAIL_(crypt_deactivate(cd, CDEVICE_2), "not active");
1155 _cleanup_dmdevices();
1158 static void UseTempVolumes(void)
1160 struct crypt_device *cd;
1163 // Tepmporary device without keyslot but with on-disk LUKS header
1164 OK_(crypt_init(&cd, DEVICE_2));
1165 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "not yet formatted");
1166 OK_(crypt_format(cd, CRYPT_LUKS1, "aes", "cbc-essiv:sha256", NULL, NULL, 16, NULL));
1167 OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0));
1168 EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
1171 OK_(crypt_init_by_name(&cd, CDEVICE_2));
1172 OK_(crypt_deactivate(cd, CDEVICE_2));
1175 // Dirty checks: device without UUID
1176 // we should be able to remove it but not manuipulate with it
1177 snprintf(tmp, sizeof(tmp), "dmsetup create %s --table \""
1178 "0 100 crypt aes-cbc-essiv:sha256 deadbabedeadbabedeadbabedeadbabe 0 "
1179 "%s 2048\"", CDEVICE_2, DEVICE_2);
1181 OK_(crypt_init_by_name(&cd, CDEVICE_2));
1182 OK_(crypt_deactivate(cd, CDEVICE_2));
1183 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "No known device type");
1186 // Dirty checks: device with UUID but LUKS header key fingerprint must fail)
1187 snprintf(tmp, sizeof(tmp), "dmsetup create %s --table \""
1188 "0 100 crypt aes-cbc-essiv:sha256 deadbabedeadbabedeadbabedeadbabe 0 "
1189 "%s 2048\" -u CRYPT-LUKS1-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-ctest1",
1190 CDEVICE_2, DEVICE_2);
1192 OK_(crypt_init_by_name(&cd, CDEVICE_2));
1193 OK_(crypt_deactivate(cd, CDEVICE_2));
1194 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "wrong volume key");
1198 OK_(crypt_init(&cd, DEVICE_2));
1199 OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
1200 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "volume key is lost");
1204 OK_(crypt_init(&cd, DEVICE_2));
1205 OK_(crypt_format(cd, CRYPT_PLAIN, "aes", "cbc-essiv:sha256", NULL, NULL, 16, NULL));
1206 FAIL_(crypt_activate_by_volume_key(cd, NULL, "xxx", 3, 0), "cannot verify key with plain");
1207 FAIL_(crypt_volume_key_verify(cd, "xxx", 3), "cannot verify key with plain");
1208 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, "xxx", 3, 0), "wrong key lenght");
1209 OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, "volumekeyvolumek", 16, 0));
1210 EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
1211 OK_(crypt_deactivate(cd, CDEVICE_2));
1215 static void LuksHeaderRestore(void)
1217 struct crypt_device *cd;
1218 struct crypt_params_luks1 params = {
1220 .data_alignment = 2048, // 4M, data offset will be 4096
1222 struct crypt_params_plain pl_params = {
1228 char key[128], key2[128], cmd[256];
1230 const char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
1231 size_t key_size = strlen(mk_hex) / 2;
1232 const char *cipher = "aes";
1233 const char *cipher_mode = "cbc-essiv:sha256";
1234 uint64_t r_payload_offset;
1236 crypt_decode_key(key, mk_hex, key_size);
1238 OK_(get_luks_offsets(0, key_size, params.data_alignment, 0, NULL, &r_payload_offset));
1239 OK_(create_dmdevice_over_loop(L_DEVICE_OK, r_payload_offset + 5000));
1241 // do not restore header over plain device
1242 OK_(crypt_init(&cd, DMDIR L_DEVICE_OK));
1243 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, &pl_params));
1244 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1245 FAIL_(crypt_header_restore(cd, CRYPT_PLAIN, VALID_HEADER), "Cannot restore header to PLAIN type device");
1246 FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, VALID_HEADER), "Cannot restore header over PLAIN type device");
1247 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1248 OK_(crypt_deactivate(cd, CDEVICE_1));
1252 OK_(crypt_init(&cd, DMDIR L_DEVICE_OK));
1253 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1254 FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, EVL_HEADER_1), "Header corrupted");
1255 FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, EVL_HEADER_2), "Header corrupted");
1256 FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, EVL_HEADER_3), "Header corrupted");
1257 FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, EVL_HEADER_4), "Header too small");
1258 OK_(crypt_header_restore(cd, CRYPT_LUKS1, VALID_HEADER));
1259 // wipe valid luks header
1260 snprintf(cmd, sizeof(cmd), "dd if=/dev/zero of=" DMDIR L_DEVICE_OK " bs=512 count=%" PRIu64 " 2>/dev/null", r_payload_offset);
1261 OK_(_system(cmd, 1));
1262 FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, EVL_HEADER_1), "Header corrupted");
1263 FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, EVL_HEADER_2), "Header corrupted");
1264 FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, EVL_HEADER_3), "Header corrupted");
1265 FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, EVL_HEADER_4), "Header too small");
1266 OK_(crypt_header_restore(cd, CRYPT_LUKS1, VALID_HEADER));
1267 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1268 OK_(crypt_deactivate(cd, CDEVICE_1));
1271 // volume key_size mismatch
1272 OK_(crypt_init(&cd, DMDIR L_DEVICE_OK));
1273 memcpy(key2, key, key_size - 1);
1274 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key2, key_size - 1, ¶ms));
1275 FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, VALID_HEADER), "Volume keysize mismatch");
1278 // payload offset mismatch
1279 params.data_alignment = 8192;
1280 OK_(crypt_init(&cd, DMDIR L_DEVICE_OK));
1281 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1282 FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, VALID_HEADER), "Payload offset mismatch");
1283 //_system("dmsetup table;sleep 1",1);
1286 _cleanup_dmdevices();
1289 static void LuksHeaderLoad(void)
1291 struct crypt_device *cd;
1292 struct crypt_params_luks1 params = {
1294 .data_alignment = 2048,
1296 struct crypt_params_plain pl_params = {
1302 char key[128], cmd[256];
1304 const char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
1305 size_t key_size = strlen(mk_hex) / 2;
1306 const char *cipher = "aes";
1307 const char *cipher_mode = "cbc-essiv:sha256";
1308 uint64_t r_payload_offset, r_header_size;
1310 crypt_decode_key(key, mk_hex, key_size);
1313 OK_(get_luks_offsets(0, key_size, params.data_alignment, 0, &r_header_size, &r_payload_offset));
1314 // external header device
1315 OK_(create_dmdevice_over_loop(H_DEVICE, r_header_size));
1316 // prepared header on a device too small to contain header and payload
1317 //OK_(create_dmdevice_over_loop(H_DEVICE_WRONG, r_payload_offset - 1));
1318 OK_(create_dmdevice_over_loop(H_DEVICE_WRONG, 2050 - 1)); //FIXME
1319 //snprintf(cmd, sizeof(cmd), "dd if=" EVL_HEADER_4 " of=" DMDIR H_DEVICE_WRONG " bs=512 count=%" PRIu64, r_payload_offset - 1);
1320 snprintf(cmd, sizeof(cmd), "dd if=" EVL_HEADER_4 " of=" DMDIR H_DEVICE_WRONG " bs=512 count=%d 2>/dev/null", 2050 - 1);
1321 OK_(_system(cmd, 1));
1323 OK_(create_dmdevice_over_loop(L_DEVICE_OK, r_payload_offset + 1000));
1325 OK_(create_dmdevice_over_loop(L_DEVICE_1S, r_payload_offset + 1));
1326 // 0 sectors device for payload
1327 OK_(create_dmdevice_over_loop(L_DEVICE_0S, r_payload_offset));
1329 // valid metadata and device size
1330 params.data_alignment = 0;
1331 params.data_device = DMDIR L_DEVICE_OK;
1332 OK_(crypt_init(&cd, DMDIR H_DEVICE));
1333 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1335 OK_(crypt_init(&cd, DMDIR H_DEVICE));
1336 OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
1337 OK_(crypt_set_data_device(cd, DMDIR L_DEVICE_OK));
1338 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1339 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1340 OK_(crypt_deactivate(cd, CDEVICE_1));
1343 // bad header: device too small (payloadOffset > device_size)
1344 OK_(crypt_init(&cd, DMDIR H_DEVICE_WRONG));
1345 FAIL_(crypt_load(cd, CRYPT_LUKS1, NULL), "Device too small");
1346 OK_(!!crypt_get_type(cd));
1349 // 0 secs for encrypted data area
1350 params.data_alignment = 2048;
1351 params.data_device = NULL;
1352 OK_(crypt_init(&cd, DMDIR L_DEVICE_0S));
1353 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1355 // load should be ok
1356 OK_(crypt_init(&cd, DMDIR L_DEVICE_0S));
1357 OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
1358 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0), "Device too small");
1359 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
1363 OK_(_system("dd if=/dev/zero of=" DMDIR L_DEVICE_OK " bs=512 count=8 2>/dev/null", 1));
1364 OK_(crypt_init(&cd, DMDIR L_DEVICE_OK));
1365 FAIL_(crypt_load(cd, CRYPT_LUKS1, NULL), "Header not found");
1369 OK_(crypt_init(&cd, DMDIR H_DEVICE));
1370 FAIL_(crypt_load(cd, CRYPT_PLAIN, NULL), "Can't load nonLUKS device type");
1372 OK_(crypt_init(&cd, DMDIR H_DEVICE));
1373 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, key, key_size, &pl_params));
1374 FAIL_(crypt_load(cd, CRYPT_LUKS1, NULL), "Can't load over nonLUKS device type");
1377 _cleanup_dmdevices();
1380 static void LuksHeaderBackup(void)
1382 struct crypt_device *cd;
1383 struct crypt_params_luks1 params = {
1385 .data_alignment = 2048,
1389 const char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
1390 size_t key_size = strlen(mk_hex) / 2;
1391 const char *cipher = "aes";
1392 const char *cipher_mode = "cbc-essiv:sha256";
1393 uint64_t r_payload_offset;
1395 crypt_decode_key(key, mk_hex, key_size);
1397 OK_(get_luks_offsets(0, key_size, params.data_alignment, 0, NULL, &r_payload_offset));
1398 OK_(create_dmdevice_over_loop(L_DEVICE_OK, r_payload_offset + 1));
1400 // create LUKS device and backup the header
1401 OK_(crypt_init(&cd, DMDIR L_DEVICE_OK));
1402 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1403 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1404 OK_(crypt_header_backup(cd, CRYPT_LUKS1, BACKUP_FILE));
1405 OK_(crypt_deactivate(cd, CDEVICE_1));
1408 // restore header from backup
1409 OK_(crypt_init(&cd, DMDIR L_DEVICE_OK));
1410 OK_(crypt_header_restore(cd, CRYPT_LUKS1, BACKUP_FILE));
1411 OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
1412 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1413 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1414 OK_(crypt_deactivate(cd, CDEVICE_1));
1417 _cleanup_dmdevices();
1420 static void ResizeDeviceLuks(void)
1422 struct crypt_device *cd;
1423 struct crypt_params_luks1 params = {
1425 .data_alignment = 2048,
1429 const char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
1430 size_t key_size = strlen(mk_hex) / 2;
1431 const char *cipher = "aes";
1432 const char *cipher_mode = "cbc-essiv:sha256";
1433 uint64_t r_payload_offset, r_header_size, r_size;
1435 crypt_decode_key(key, mk_hex, key_size);
1438 OK_(get_luks_offsets(0, key_size, params.data_alignment, 0, NULL, &r_payload_offset));
1439 OK_(get_luks_offsets(1, key_size, 0, 0, &r_header_size, NULL));
1440 OK_(create_dmdevice_over_loop(H_DEVICE, r_header_size));
1441 OK_(create_dmdevice_over_loop(L_DEVICE_OK, r_payload_offset + 1000));
1442 OK_(create_dmdevice_over_loop(L_DEVICE_0S, 1000));
1444 // test header and encrypted payload all in one device
1445 OK_(crypt_init(&cd, DMDIR L_DEVICE_OK));
1446 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1447 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1448 OK_(crypt_resize(cd, CDEVICE_1, 42));
1449 if (!device_size(DMDIR CDEVICE_1, &r_size))
1450 EQ_(42, r_size >> SECTOR_SHIFT);
1451 // autodetect encrypted device area size
1452 OK_(crypt_resize(cd, CDEVICE_1, 0));
1453 if (!device_size(DMDIR CDEVICE_1, &r_size))
1454 EQ_(1000, r_size >> SECTOR_SHIFT);
1455 FAIL_(crypt_resize(cd, CDEVICE_1, 1001), "Device too small");
1456 if (!device_size(DMDIR CDEVICE_1, &r_size))
1457 EQ_(1000, r_size >> SECTOR_SHIFT);
1458 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1459 OK_(crypt_deactivate(cd, CDEVICE_1));
1462 params.data_alignment = 0;
1463 params.data_device = DMDIR L_DEVICE_0S;
1464 // test case for external header
1465 OK_(crypt_init(&cd, DMDIR H_DEVICE));
1466 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1467 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1468 OK_(crypt_resize(cd, CDEVICE_1, 666));
1469 if (!device_size(DMDIR CDEVICE_1, &r_size))
1470 EQ_(666, r_size >> SECTOR_SHIFT);
1471 // autodetect encrypted device size
1472 OK_(crypt_resize(cd, CDEVICE_1, 0));
1473 if (!device_size(DMDIR CDEVICE_1, &r_size))
1474 EQ_(1000, r_size >> SECTOR_SHIFT);
1475 FAIL_(crypt_resize(cd, CDEVICE_1, 1001), "Device too small");
1476 if (!device_size(DMDIR CDEVICE_1, &r_size))
1477 EQ_(1000, r_size >> SECTOR_SHIFT);
1478 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1479 OK_(crypt_deactivate(cd, CDEVICE_1));
1482 _cleanup_dmdevices();
1485 static void HashDevicePlain(void)
1487 struct crypt_device *cd;
1488 struct crypt_params_plain params = {
1495 const char *mk_hex, *keystr;
1498 OK_(crypt_init(&cd, DEVICE_1));
1499 OK_(crypt_format(cd, CRYPT_PLAIN, "aes", "cbc-essiv:sha256", NULL, NULL, 16, ¶ms));
1501 // hash PLAIN, short key
1502 OK_(_prepare_keyfile(KEYFILE1, "tooshort", 8));
1503 FAIL_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 16, 0), "not enough data in keyfile");
1506 // hash PLAIN, exact key
1507 // 0 1 2 3 4 5 6 7 8 9 a b c d e f
1508 mk_hex = "caffeecaffeecaffeecaffeecaffee88";
1510 crypt_decode_key(key, mk_hex, key_size);
1511 OK_(_prepare_keyfile(KEYFILE1, key, key_size));
1512 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size, 0));
1513 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1514 OK_(strcmp(key, mk_hex));
1515 OK_(crypt_deactivate(cd, CDEVICE_1));
1518 mk_hex = "caffeecaffeecaffeecaffeeca000000";
1519 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size - 3, 0));
1520 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1521 OK_(strcmp(key, mk_hex));
1522 OK_(crypt_deactivate(cd, CDEVICE_1));
1526 // hash PLAIN, long key
1527 // 0 1 2 3 4 5 6 7 8 9 a b c d e f
1528 mk_hex = "caffeecaffeecaffeecaffeecaffee88babebabe";
1530 crypt_decode_key(key, mk_hex, key_size);
1531 OK_(_prepare_keyfile(KEYFILE1, key, strlen(mk_hex) / 2));
1532 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size, 0));
1533 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1534 FAIL_(strcmp(key, mk_hex), "only key length used");
1535 OK_(strncmp(key, mk_hex, key_size));
1536 OK_(crypt_deactivate(cd, CDEVICE_1));
1539 // Now without explicit limit
1540 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
1541 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1542 FAIL_(strcmp(key, mk_hex), "only key length used");
1543 OK_(strncmp(key, mk_hex, key_size));
1544 OK_(crypt_deactivate(cd, CDEVICE_1));
1550 params.hash = "sha256";
1551 OK_(crypt_init(&cd, DEVICE_1));
1552 OK_(crypt_format(cd, CRYPT_PLAIN, "aes", "cbc-essiv:sha256", NULL, NULL, 16, ¶ms));
1554 // 0 1 2 3 4 5 6 7 8 9 a b c d e f
1555 mk_hex = "c62e4615bd39e222572f3a1bf7c2132e";
1556 keystr = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
1557 key_size = strlen(keystr); // 32
1558 OK_(_prepare_keyfile(KEYFILE1, keystr, strlen(keystr)));
1559 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size, 0));
1560 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1561 OK_(strcmp(key, mk_hex));
1562 OK_(crypt_deactivate(cd, CDEVICE_1));
1564 // Read full keyfile
1565 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
1566 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1567 OK_(strcmp(key, mk_hex));
1568 OK_(crypt_deactivate(cd, CDEVICE_1));
1572 // Limit keyfile read
1573 keystr = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxAAAAAAAA";
1574 OK_(_prepare_keyfile(KEYFILE1, keystr, strlen(keystr)));
1575 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size, 0));
1576 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1577 OK_(strcmp(key, mk_hex));
1578 OK_(crypt_deactivate(cd, CDEVICE_1));
1581 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
1582 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1583 OK_(strcmp(key, "0e49cb34a1dee1df33f6505e4de44a66"));
1584 OK_(crypt_deactivate(cd, CDEVICE_1));
1588 // FIXME: add keyfile="-" tests somehow
1593 static void VerityTest(void)
1595 struct crypt_device *cd;
1596 const char *salt_hex = "20c28ffc129c12360ba6ceea2b6cf04e89c2b41cfe6b8439eb53c1897f50df7b";
1597 const char *root_hex = "ab018b003a967fc782effb293b6dccb60b4f40c06bf80d16391acf686d28b5d6";
1598 char salt[256], root_hash[256];
1599 struct crypt_active_device cad;
1600 struct crypt_params_verity params = {
1601 .data_device = DEVICE_EMPTY,
1603 .data_size = 0, /* whole device */
1604 .hash_area_offset = 0,
1605 .flags = CRYPT_VERITY_CREATE_HASH,
1608 crypt_decode_key(salt, salt_hex, strlen(salt_hex) / 2);
1609 crypt_decode_key(root_hash, root_hex, strlen(root_hex) / 2);
1612 OK_(crypt_init(&cd, DEVICE_2));
1615 params.data_block_size = 333;
1616 FAIL_(crypt_format(cd, CRYPT_VERITY, NULL, NULL, NULL, NULL, 0, ¶ms),
1617 "Unsupppored block size.");
1618 params.data_block_size = 4096;
1619 params.hash_block_size = 333;
1620 FAIL_(crypt_format(cd, CRYPT_VERITY, NULL, NULL, NULL, NULL, 0, ¶ms),
1621 "Unsupppored block size.");
1622 params.hash_block_size = 4096;
1625 params.salt_size = 257;
1626 FAIL_(crypt_format(cd, CRYPT_VERITY, NULL, NULL, NULL, NULL, 0, ¶ms),
1628 params.salt_size = 32;
1631 params.hash_type = 3;
1632 FAIL_(crypt_format(cd, CRYPT_VERITY, NULL, NULL, NULL, NULL, 0, ¶ms),
1633 "Unsupported hash type.");
1634 params.hash_type = 1;
1635 params.hash_name = "blah";
1636 FAIL_(crypt_format(cd, CRYPT_VERITY, NULL, NULL, NULL, NULL, 0, ¶ms),
1637 "Unsupported hash name.");
1638 params.hash_name = "sha256";
1640 OK_(crypt_format(cd, CRYPT_VERITY, NULL, NULL, NULL, NULL, 0, ¶ms));
1644 OK_(crypt_init(&cd, DEVICE_2));
1645 memset(¶ms, 0, sizeof(params));
1646 params.data_device = DEVICE_EMPTY;
1647 params.flags = CRYPT_VERITY_CHECK_HASH;
1648 OK_(crypt_load(cd, CRYPT_VERITY, ¶ms));
1650 /* check verity params */
1651 EQ_(crypt_get_volume_key_size(cd), 32);
1652 OK_(strcmp(CRYPT_VERITY, crypt_get_type(cd)));
1653 memset(¶ms, 0, sizeof(params));
1654 OK_(crypt_get_verity_info(cd, ¶ms));
1655 OK_(strcmp("sha256", params.hash_name));
1656 EQ_(strlen(salt_hex) / 2, params.salt_size);
1657 OK_(memcmp(salt, params.salt, params.salt_size));
1658 EQ_(4096, params.data_block_size);
1659 EQ_(4096, params.hash_block_size);
1660 EQ_(1, params.hash_type);
1661 EQ_(crypt_get_volume_key_size(cd), 32);
1663 OK_(crypt_activate_by_volume_key(cd, NULL, root_hash, 32, 0));
1664 OK_(crypt_set_data_device(cd, DEVICE_1));
1665 FAIL_(crypt_activate_by_volume_key(cd, NULL, root_hash, 32, 0), "Data corrupted");;
1667 OK_(crypt_set_data_device(cd, DEVICE_EMPTY));
1668 if (crypt_activate_by_volume_key(cd, CDEVICE_1, root_hash, 32,
1669 CRYPT_ACTIVATE_READONLY) == -ENOTSUP) {
1670 printf("WARNING: kernel dm-verity not supported, skipping test.\n");
1674 OK_(crypt_get_active_device(cd, CDEVICE_1, &cad));
1675 EQ_(CRYPT_ACTIVATE_READONLY, cad.flags);
1678 OK_(crypt_init_by_name(&cd, CDEVICE_1));
1679 OK_(crypt_deactivate(cd, CDEVICE_1));
1682 root_hash[1] = ~root_hash[1];
1683 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, root_hash, 32, CRYPT_ACTIVATE_READONLY));
1684 OK_(crypt_get_active_device(cd, CDEVICE_1, &cad));
1685 EQ_(CRYPT_ACTIVATE_READONLY|CRYPT_ACTIVATE_CORRUPTED, cad.flags);
1686 OK_(crypt_deactivate(cd, CDEVICE_1));
1687 root_hash[1] = ~root_hash[1];
1690 OK_(crypt_set_data_device(cd, DEVICE_1));
1691 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, root_hash, 32, CRYPT_ACTIVATE_READONLY));
1692 OK_(crypt_get_active_device(cd, CDEVICE_1, &cad));
1693 EQ_(CRYPT_ACTIVATE_READONLY|CRYPT_ACTIVATE_CORRUPTED, cad.flags);
1694 OK_(crypt_deactivate(cd, CDEVICE_1));
1699 static void TcryptTest(void)
1701 struct crypt_device *cd = NULL;
1702 struct crypt_active_device cad;
1703 const char *passphrase = "aaaaaaaaaaaa";
1704 const char *kf1 = "tcrypt-images/keyfile1";
1705 const char *kf2 = "tcrypt-images/keyfile2";
1706 const char *keyfiles[] = { kf1, kf2 };
1707 struct crypt_params_tcrypt params = {
1708 .passphrase = passphrase,
1709 .passphrase_size = strlen(passphrase),
1710 .keyfiles = keyfiles,
1711 .keyfiles_count = 2,
1713 double enc_mbr = 0, dec_mbr = 0;
1714 const char *tcrypt_dev = "tcrypt-images/tck_5-sha512-xts-aes";
1715 size_t key_size = 64;
1716 char key[key_size], key_def[key_size];
1717 const char *key_hex =
1718 "98dee64abe44bbf41d171c1f7b3e8eacda6d6b01f459097459a167f8c2872a96"
1719 "3979531d1cdc18af62757cf22286f16f8583d848524f128d7594ac2082668c73";
1722 crypt_decode_key(key_def, key_hex, strlen(key_hex) / 2);
1724 // First ensure we can use af_alg skcipher interface
1725 r = crypt_benchmark(NULL, "aes", "xts", 512, 16, 1024, &enc_mbr, &dec_mbr);
1726 if (r == -ENOTSUP || r == -ENOENT) {
1727 printf("WARNING: algif_skcipher interface not present, skipping test.\n");
1731 OK_(crypt_init(&cd, tcrypt_dev));
1732 params.passphrase_size--;
1733 FAIL_(crypt_load(cd, CRYPT_TCRYPT, ¶ms), "Wrong passphrase");
1734 params.passphrase_size++;
1735 OK_(crypt_load(cd, CRYPT_TCRYPT, ¶ms));
1737 // check params after load
1738 OK_(strcmp("xts-plain64", crypt_get_cipher_mode(cd)));
1739 OK_(strcmp("aes", crypt_get_cipher(cd)));
1740 EQ_(key_size, crypt_get_volume_key_size(cd));
1741 EQ_(256, crypt_get_iv_offset(cd));
1742 EQ_(256, crypt_get_data_offset(cd));
1744 memset(key, 0, key_size);
1747 FAIL_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key, &key_size, NULL, 0), "small buffer");
1749 OK_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key, &key_size, NULL, 0));
1750 OK_(memcmp(key, key_def, key_size));
1753 crypt_set_log_callback(cd, &new_log, NULL);
1754 OK_(crypt_dump(cd));
1755 OK_(!(global_lines != 0));
1756 crypt_set_log_callback(cd, NULL, NULL);
1759 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, NULL, 0, CRYPT_ACTIVATE_READONLY));
1762 OK_(crypt_init_by_name_and_header(&cd, CDEVICE_1, NULL));
1763 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1765 FAIL_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key, &key_size, NULL, 0), "Need crypt_load");
1767 // check params after init_by_name
1768 OK_(strcmp("xts-plain64", crypt_get_cipher_mode(cd)));
1769 OK_(strcmp("aes", crypt_get_cipher(cd)));
1770 EQ_(key_size, crypt_get_volume_key_size(cd));
1771 EQ_(256, crypt_get_iv_offset(cd));
1772 EQ_(256, crypt_get_data_offset(cd));
1774 OK_(crypt_get_active_device(cd, CDEVICE_1, &cad));
1775 EQ_(CRYPT_ACTIVATE_READONLY, cad.flags);
1776 EQ_(256, cad.offset);
1777 EQ_(256, cad.iv_offset);
1780 OK_(crypt_deactivate(cd, CDEVICE_1));
1784 // Check that gcrypt is properly initialised in format
1785 static void NonFIPSAlg(void)
1787 struct crypt_device *cd;
1788 struct crypt_params_luks1 params = {0};
1790 size_t key_size = 128;
1791 const char *cipher = "aes";
1792 const char *cipher_mode = "cbc-essiv:sha256";
1795 OK_(crypt_init(&cd, DEVICE_2));
1796 params.hash = "sha256";
1797 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1798 FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms),
1799 "Already formatted.");
1802 params.hash = "whirlpool";
1803 OK_(crypt_init(&cd, DEVICE_2));
1804 ret = crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms);
1806 printf("WARNING: whirlpool not supported, skipping test.\n");
1812 params.hash = "md5";
1813 OK_(crypt_init(&cd, DEVICE_2));
1814 FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms),
1815 "MD5 unsupported, too short");
1819 int main(int argc, char *argv[])
1823 if (getuid() != 0) {
1824 printf("You must be root to run this test.\n");
1828 for (i = 1; i < argc; i++) {
1829 if (!strcmp("-v", argv[i]) || !strcmp("--verbose", argv[i]))
1831 else if (!strcmp("--debug", argv[i]))
1832 _debug = _verbose = 1;
1839 crypt_set_debug_level(_debug ? CRYPT_DEBUG_ALL : CRYPT_DEBUG_NONE);
1841 RUN_(NonFIPSAlg, "Crypto is properly initialised in format"); //must be the first!
1842 RUN_(AddDevicePlain, "plain device API creation exercise");
1843 RUN_(HashDevicePlain, "plain device API hash test");
1844 RUN_(AddDeviceLuks, "Format and use LUKS device");
1845 RUN_(LuksHeaderLoad, "test header load");
1846 RUN_(LuksHeaderRestore, "test LUKS header restore");
1847 RUN_(LuksHeaderBackup, "test LUKS header backup");
1848 RUN_(ResizeDeviceLuks, "Luks device resize tests");
1849 RUN_(UseLuksDevice, "Use pre-formated LUKS device");
1850 RUN_(SuspendDevice, "Suspend/Resume test");
1851 RUN_(UseTempVolumes, "Format and use temporary encrypted device");
1852 RUN_(CallbacksTest, "API callbacks test");
1853 RUN_(VerityTest, "DM verity test");
1854 RUN_(TcryptTest, "Tcrypt API test");