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 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 #include <sys/ioctl.h>
32 #include <libdevmapper.h>
35 #include "libcryptsetup.h"
36 #include "utils_loop.h"
38 #define DMDIR "/dev/mapper/"
40 #define DEVICE_1_UUID "28632274-8c8a-493f-835b-da802e1c576b"
41 #define DEVICE_EMPTY_name "crypt_zero"
42 #define DEVICE_EMPTY DMDIR DEVICE_EMPTY_name
43 #define DEVICE_ERROR_name "crypt_error"
44 #define DEVICE_ERROR DMDIR DEVICE_ERROR_name
46 #define CDEVICE_1 "ctest1"
47 #define CDEVICE_2 "ctest2"
48 #define CDEVICE_WRONG "O_o"
49 #define H_DEVICE "head_ok"
50 #define H_DEVICE_WRONG "head_wr"
51 #define L_DEVICE_1S "luks_onesec"
52 #define L_DEVICE_0S "luks_zerosec"
53 #define L_DEVICE_WRONG "luks_wr"
54 #define L_DEVICE_OK "luks_ok"
55 #define EVL_HEADER_1 "evil_hdr-luks_hdr_damage"
56 #define EVL_HEADER_2 "evil_hdr-payload_overwrite"
57 #define EVL_HEADER_3 "evil_hdr-stripes_payload_dmg"
58 #define EVL_HEADER_4 "evil_hdr-small_luks_device"
59 #define VALID_HEADER "valid_header_file"
60 #define BACKUP_FILE "csetup_backup_file"
61 #define IMAGE1 "compatimage.img"
62 #define IMAGE_EMPTY "empty.img"
64 #define KEYFILE1 "key1.file"
65 #define KEY1 "compatkey"
67 #define KEYFILE2 "key2.file"
68 #define KEY2 "0123456789abcdef"
70 #define PASSPHRASE "blabla"
72 #define DEVICE_TEST_UUID "12345678-1234-1234-1234-123456789abc"
74 #define DEVICE_WRONG "/dev/Ooo_"
75 #define DEVICE_CHAR "/dev/zero"
76 #define THE_LFILE_TEMPLATE "cryptsetup-tstlp.XXXXXX"
78 #define SECTOR_SHIFT 9L
79 #define SECTOR_SIZE 512
80 #define TST_LOOP_FILE_SIZE (((1<<20)*50)>>SECTOR_SHIFT)
81 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
82 #define DIV_ROUND_UP_MODULO(n,d) (DIV_ROUND_UP(n,d)*(d))
83 #define LUKS_PHDR_SIZE_B 1024
85 static int _debug = 0;
86 static int _verbose = 1;
87 static int _fips_mode = 0;
89 static char global_log[4096];
90 static int global_lines = 0;
92 static char *DEVICE_1 = NULL;
93 static char *DEVICE_2 = NULL;
94 static char *THE_LOOP_DEV = NULL;
96 static char *tmp_file_1 = NULL;
97 static char *test_loop_file = NULL;
98 static uint64_t t_dev_offset = 0;
100 static int _system(const char*, int);
104 static int device_size(const char *device, uint64_t *size)
108 devfd = open(device, O_RDONLY);
112 if (ioctl(devfd, BLKGETSIZE64, size) < 0)
118 static int fips_mode(void)
123 fd = open("/proc/sys/crypto/fips_enabled", O_RDONLY);
128 if (read(fd, &buf, 1) != 1)
136 static int get_luks_offsets(int metadata_device,
138 unsigned int alignpayload_sec,
139 unsigned int alignoffset_sec,
140 uint64_t *r_header_size,
141 uint64_t *r_payload_offset)
144 uint64_t current_sector;
145 uint32_t sectors_per_stripes_set;
150 sectors_per_stripes_set = DIV_ROUND_UP(keylength*LUKS_STRIPES, SECTOR_SIZE);
151 printf("sectors_per_stripes %" PRIu32 "\n", sectors_per_stripes_set);
152 current_sector = DIV_ROUND_UP_MODULO(DIV_ROUND_UP(LUKS_PHDR_SIZE_B, SECTOR_SIZE),
153 LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE);
154 for(i=0;i < (LUKS_NUMKEYS - 1);i++)
155 current_sector = DIV_ROUND_UP_MODULO(current_sector + sectors_per_stripes_set,
156 LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE);
158 *r_header_size = current_sector + sectors_per_stripes_set;
160 current_sector = DIV_ROUND_UP_MODULO(current_sector + sectors_per_stripes_set,
161 LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE);
163 if (r_payload_offset) {
165 *r_payload_offset = alignpayload_sec;
167 *r_payload_offset = DIV_ROUND_UP_MODULO(current_sector, alignpayload_sec)
175 * Creates dm-linear target over the test loop device. Offset is held in
176 * global variables so that size can be tested whether it fits into remaining
177 * size of the loop device or not
179 static int create_dmdevice_over_loop(const char *dm_name, const uint64_t size)
185 if(device_size(THE_LOOP_DEV, &r_size) < 0 || r_size <= t_dev_offset || !size)
187 if ((r_size - t_dev_offset) < size) {
188 printf("No enough space on backing loop device\n.");
191 snprintf(cmd, sizeof(cmd),
192 "dmsetup create %s --table \"0 %" PRIu64 " linear %s %" PRIu64 "\"",
193 dm_name, size, THE_LOOP_DEV, t_dev_offset);
194 if (!(r = _system(cmd, 1))) {
195 t_dev_offset += size;
200 // TODO some utility to remove dmdevice over the loop file
202 // Get key from kernel dm mapping table using dm-ioctl
203 static int _get_key_dm(const char *name, char *buffer, unsigned int buffer_size)
207 uint64_t start, length;
208 char *target_type, *key, *params;
212 if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
214 if (!dm_task_set_name(dmt, name))
216 if (!dm_task_run(dmt))
218 if (!dm_task_get_info(dmt, &dmi))
223 next = dm_get_next_target(dmt, next, &start, &length, &target_type, ¶ms);
224 if (!target_type || strcmp(target_type, "crypt") != 0)
227 (void)strsep(¶ms, " "); /* rcipher */
228 key = strsep(¶ms, " ");
230 if (buffer_size <= strlen(key))
233 strncpy(buffer, key, buffer_size);
237 dm_task_destroy(dmt);
242 static int _prepare_keyfile(const char *name, const char *passphrase, int size)
246 fd = open(name, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR);
248 r = write(fd, passphrase, size);
253 return r == size ? 0 : 1;
256 static void _remove_keyfiles(void)
262 // Decode key from its hex representation
263 static int crypt_decode_key(char *key, const char *hex, unsigned int size)
271 for (i = 0; i < size; i++) {
275 key[i] = (unsigned char)strtoul(buffer, &endp, 16);
277 if (endp != &buffer[2])
287 static void cmdLineLog(int level, const char *msg)
289 strncat(global_log, msg, sizeof(global_log) - strlen(global_log));
293 static void new_log(int level, const char *msg, void *usrptr)
296 printf("LOG: %s", msg);
297 cmdLineLog(level, msg);
300 static void reset_log(void)
302 memset(global_log, 0, sizeof(global_log));
306 static int _system(const char *command, int warn)
310 printf("Running system: %s\n", command);
311 if ((r=system(command)) < 0 && warn)
312 printf("System command failed: %s", command);
316 static void _cleanup_dmdevices(void)
320 if (!stat(DMDIR H_DEVICE, &st)) {
321 _system("dmsetup remove " H_DEVICE, 0);
323 if (!stat(DMDIR H_DEVICE_WRONG, &st)) {
324 _system("dmsetup remove " H_DEVICE_WRONG, 0);
326 if (!stat(DMDIR L_DEVICE_0S, &st)) {
327 _system("dmsetup remove " L_DEVICE_0S, 0);
329 if (!stat(DMDIR L_DEVICE_1S, &st)) {
330 _system("dmsetup remove " L_DEVICE_1S, 0);
332 if (!stat(DMDIR L_DEVICE_WRONG, &st)) {
333 _system("dmsetup remove " L_DEVICE_WRONG, 0);
335 if (!stat(DMDIR L_DEVICE_OK, &st)) {
336 _system("dmsetup remove " L_DEVICE_OK, 0);
342 static void _cleanup(void)
346 //_system("udevadm settle", 0);
348 if (!stat(DMDIR CDEVICE_1, &st))
349 _system("dmsetup remove " CDEVICE_1, 0);
351 if (!stat(DMDIR CDEVICE_2, &st))
352 _system("dmsetup remove " CDEVICE_2, 0);
354 if (!stat(DEVICE_EMPTY, &st))
355 _system("dmsetup remove " DEVICE_EMPTY_name, 0);
357 if (!stat(DEVICE_ERROR, &st))
358 _system("dmsetup remove " DEVICE_ERROR_name, 0);
360 _cleanup_dmdevices();
362 if (crypt_loop_device(THE_LOOP_DEV))
363 crypt_loop_detach(THE_LOOP_DEV);
365 if (crypt_loop_device(DEVICE_1))
366 crypt_loop_detach(DEVICE_1);
368 if (crypt_loop_device(DEVICE_2))
369 crypt_loop_detach(DEVICE_2);
371 _system("rm -f " IMAGE_EMPTY, 0);
372 _system("rm -f " IMAGE1, 0);
374 remove(test_loop_file);
377 remove(EVL_HEADER_1);
378 remove(EVL_HEADER_2);
379 remove(EVL_HEADER_3);
380 remove(EVL_HEADER_4);
381 remove(VALID_HEADER);
387 free(test_loop_file);
393 static int _setup(void)
398 test_loop_file = strdup(THE_LFILE_TEMPLATE);
399 if ((fd=mkstemp(test_loop_file)) == -1) {
400 printf("cannot create temporary file with template %s\n", test_loop_file);
404 snprintf(cmd, sizeof(cmd), "dd if=/dev/zero of=%s bs=%d count=%d 2>/dev/null",
405 test_loop_file, SECTOR_SIZE, TST_LOOP_FILE_SIZE);
410 THE_LOOP_DEV = crypt_loop_get_device();
412 printf("Cannot find free loop device.\n");
415 if (crypt_loop_device(THE_LOOP_DEV)) {
416 fd = crypt_loop_attach(THE_LOOP_DEV, test_loop_file, 0, 0, &ro);
420 tmp_file_1 = strdup(THE_LFILE_TEMPLATE);
421 if ((fd=mkstemp(tmp_file_1)) == -1) {
422 printf("cannot create temporary file with template %s\n", tmp_file_1);
426 snprintf(cmd, sizeof(cmd), "dd if=/dev/zero of=%s bs=%d count=%d 2>/dev/null",
427 tmp_file_1, SECTOR_SIZE, 10);
431 _system("dmsetup create " DEVICE_EMPTY_name " --table \"0 10000 zero\"", 1);
432 _system("dmsetup create " DEVICE_ERROR_name " --table \"0 10000 error\"", 1);
434 DEVICE_1 = crypt_loop_get_device();
436 printf("Cannot find free loop device.\n");
439 if (crypt_loop_device(DEVICE_1)) {
440 _system(" [ ! -e " IMAGE1 " ] && bzip2 -dk " IMAGE1 ".bz2", 1);
441 fd = crypt_loop_attach(DEVICE_1, IMAGE1, 0, 0, &ro);
445 DEVICE_2 = crypt_loop_get_device();
447 printf("Cannot find free loop device.\n");
450 if (crypt_loop_device(DEVICE_2)) {
451 _system("dd if=/dev/zero of=" IMAGE_EMPTY " bs=1M count=4 2>/dev/null", 1);
452 fd = crypt_loop_attach(DEVICE_2, IMAGE_EMPTY, 0, 0, &ro);
455 /* Keymaterial offset is less than 8 sectors */
456 _system(" [ ! -e " EVL_HEADER_1 " ] && bzip2 -dk " EVL_HEADER_1 ".bz2", 1);
457 /* keymaterial offset aims into payload area */
458 _system(" [ ! -e " EVL_HEADER_2 " ] && bzip2 -dk " EVL_HEADER_2 ".bz2", 1);
459 /* keymaterial offset is valid, number of stripes causes payload area to be overwriten */
460 _system(" [ ! -e " EVL_HEADER_3 " ] && bzip2 -dk " EVL_HEADER_3 ".bz2", 1);
461 /* luks device header for data and header on same device. payloadOffset is greater than
462 * device size (crypt_load() test) */
463 _system(" [ ! -e " EVL_HEADER_4 " ] && bzip2 -dk " EVL_HEADER_4 ".bz2", 1);
464 /* valid header: payloadOffset=4096, key_size=32,
465 * volume_key = bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a */
466 _system(" [ ! -e " VALID_HEADER " ] && bzip2 -dk " VALID_HEADER ".bz2", 1);
468 /* Prepare tcrypt images */
469 _system(" [ ! -d tcrypt-images ] && tar xjf tcrypt-images.tar.bz2 2>/dev/null", 1);
471 _system("modprobe dm-crypt", 0);
472 _system("modprobe dm-verity", 0);
474 _fips_mode = fips_mode();
476 printf("FIPS MODE: %d\n", _fips_mode);
481 static void check_ok(int status, int line, const char *func)
486 crypt_get_error(buf, sizeof(buf));
487 printf("FAIL line %d [%s]: code %d, %s\n", line, func, status, buf);
493 static void check_ko(int status, int line, const char *func)
497 memset(buf, 0, sizeof(buf));
498 crypt_get_error(buf, sizeof(buf));
500 printf("FAIL line %d [%s]: code %d, %s\n", line, func, status, buf);
504 printf(" => errno %d, errmsg: %s\n", status, buf);
507 static void check_equal(int line, const char *func, int64_t x, int64_t y)
509 printf("FAIL line %d [%s]: expected equal values differs: %"
510 PRIi64 " != %" PRIi64 "\n", line, func, x, y);
515 static void xlog(const char *msg, const char *tst, const char *func, int line, const char *txt)
519 printf(" [%s,%s:%d] %s [%s]\n", msg, func, line, tst, txt);
521 printf(" [%s,%s:%d] %s\n", msg, func, line, tst);
525 /* crypt_device context must be "cd" to parse error properly here */
526 #define OK_(x) do { xlog("(success)", #x, __FUNCTION__, __LINE__, NULL); \
527 check_ok((x), __LINE__, __FUNCTION__); \
529 #define FAIL_(x, y) do { xlog("(fail) ", #x, __FUNCTION__, __LINE__, y); \
530 check_ko((x), __LINE__, __FUNCTION__); \
532 #define EQ_(x, y) do { xlog("(equal) ", #x " == " #y, __FUNCTION__, __LINE__, NULL); \
533 int64_t _x = (x), _y = (y); \
534 if (_x != _y) check_equal(__LINE__, __FUNCTION__, _x, _y); \
536 #define RUN_(x, y) do { printf("%s: %s\n", #x, (y)); x(); } while (0)
538 static void AddDevicePlain(void)
540 struct crypt_device *cd;
541 struct crypt_params_plain params = {
548 char key[128], key2[128], path[128];
550 const char *passphrase = PASSPHRASE;
551 // hashed hex version of PASSPHRASE
552 const char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
553 size_t key_size = strlen(mk_hex) / 2;
554 const char *cipher = "aes";
555 const char *cipher_mode = "cbc-essiv:sha256";
557 uint64_t size, r_size;
559 crypt_decode_key(key, mk_hex, key_size);
560 FAIL_(crypt_init(&cd, ""), "empty device string");
561 FAIL_(crypt_init(&cd, DEVICE_WRONG), "nonexistent device name ");
562 FAIL_(crypt_init(&cd, DEVICE_CHAR), "character device as backing device");
563 OK_(crypt_init(&cd, tmp_file_1));
566 // test crypt_format, crypt_get_cipher, crypt_get_cipher_mode, crypt_get_volume_key_size
567 OK_(crypt_init(&cd,DEVICE_1));
570 FAIL_(crypt_format(cd,CRYPT_PLAIN,NULL,cipher_mode,NULL,NULL,key_size,¶ms),"cipher param is null");
571 FAIL_(crypt_format(cd,CRYPT_PLAIN,cipher,NULL,NULL,NULL,key_size,¶ms),"cipher_mode param is null");
572 OK_(crypt_format(cd,CRYPT_PLAIN,cipher,cipher_mode,NULL,NULL,key_size,¶ms));
573 OK_(strcmp(cipher_mode,crypt_get_cipher_mode(cd)));
574 OK_(strcmp(cipher,crypt_get_cipher(cd)));
575 EQ_((int)key_size, crypt_get_volume_key_size(cd));
576 EQ_(params.skip, crypt_get_iv_offset(cd));
577 EQ_(params.offset, crypt_get_data_offset(cd));
582 FAIL_(crypt_set_uuid(cd,DEVICE_1_UUID),"can't set uuid to plain device");
586 // default is "plain" hash - no password hash
587 OK_(crypt_init(&cd, DEVICE_1));
588 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, NULL));
589 FAIL_(crypt_activate_by_volume_key(cd, NULL, key, key_size, 0), "cannot verify key with plain");
590 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
591 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
592 OK_(crypt_deactivate(cd, CDEVICE_1));
595 // test boundaries in offset parameter
596 device_size(DEVICE_1,&size);
598 // zero sectors length
599 params.offset = size >> SECTOR_SHIFT;
600 OK_(crypt_init(&cd, DEVICE_1));
601 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
602 EQ_(crypt_get_data_offset(cd),params.offset);
603 // device size is 0 sectors
604 FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0), "invalid device size (0 blocks)");
605 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
606 // data part of crypt device is of 1 sector size
607 params.offset = (size >> SECTOR_SHIFT) - 1;
610 OK_(crypt_init(&cd, DEVICE_1));
611 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
612 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
613 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
614 snprintf(path, sizeof(path), "%s/%s", crypt_get_dir(), CDEVICE_1);
615 if (device_size(path, &r_size) >= 0)
616 EQ_(r_size>>SECTOR_SHIFT, 1);
617 OK_(crypt_deactivate(cd, CDEVICE_1));
620 // size > device_size
622 params.size = (size >> SECTOR_SHIFT) + 1;
623 crypt_init(&cd, DEVICE_1);
624 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
625 FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0),"Device too small");
626 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
629 // offset == device_size (autodetect size)
630 params.offset = (size >> SECTOR_SHIFT);
632 crypt_init(&cd, DEVICE_1);
633 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
634 FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0),"Device too small");
635 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
638 // offset == device_size (user defined size)
639 params.offset = (size >> SECTOR_SHIFT);
641 crypt_init(&cd, DEVICE_1);
642 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
643 FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0),"Device too small");
644 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
647 // offset+size > device_size
649 params.size = (size >> SECTOR_SHIFT) - params.offset + 1;
650 crypt_init(&cd, DEVICE_1);
651 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
652 FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0),"Offset and size are beyond device real size");
653 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
656 // offset+size == device_size
658 params.size = (size >> SECTOR_SHIFT) - params.offset;
659 crypt_init(&cd, DEVICE_1);
660 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
661 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
662 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
663 if (!device_size(path, &r_size))
664 EQ_((r_size >> SECTOR_SHIFT),params.size);
665 OK_(crypt_deactivate(cd,CDEVICE_1));
668 params.hash = "sha1";
673 // Now use hashed password
674 OK_(crypt_init(&cd, DEVICE_1));
675 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
676 FAIL_(crypt_activate_by_passphrase(cd, NULL, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0),
677 "cannot verify passphrase with plain" );
678 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
680 // device status check
681 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
682 snprintf(path, sizeof(path), "%s/%s", crypt_get_dir(), CDEVICE_1);
683 fd = open(path, O_RDONLY);
684 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_BUSY);
685 FAIL_(crypt_deactivate(cd, CDEVICE_1), "Device is busy");
687 OK_(crypt_deactivate(cd, CDEVICE_1));
688 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
691 // crypt_init_by_name_and_header
692 OK_(crypt_init(&cd,DEVICE_1));
693 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
694 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
697 FAIL_(crypt_init_by_name_and_header(&cd, CDEVICE_1, H_DEVICE),"can't init plain device by header device");
698 OK_(crypt_init_by_name(&cd, CDEVICE_1));
699 OK_(strcmp(cipher_mode,crypt_get_cipher_mode(cd)));
700 OK_(strcmp(cipher,crypt_get_cipher(cd)));
701 EQ_((int)key_size, crypt_get_volume_key_size(cd));
702 EQ_(params.skip, crypt_get_iv_offset(cd));
703 EQ_(params.offset, crypt_get_data_offset(cd));
704 OK_(crypt_deactivate(cd, CDEVICE_1));
707 OK_(crypt_init(&cd,DEVICE_1));
708 OK_(crypt_format(cd,CRYPT_PLAIN,cipher,cipher_mode,NULL,NULL,key_size,¶ms));
712 // crypt_set_data_device
713 FAIL_(crypt_set_data_device(cd,H_DEVICE),"can't set data device for plain device");
716 OK_(strcmp(crypt_get_type(cd),CRYPT_PLAIN));
718 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
719 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
722 OK_(crypt_resize(cd,CDEVICE_1,size>>SECTOR_SHIFT)); // same size
723 if (!device_size(path,&r_size))
727 FAIL_(crypt_resize(cd, CDEVICE_1, (uint64_t)-1),"Backing device is too small");
728 FAIL_(crypt_resize(cd, CDEVICE_1, (size>>SECTOR_SHIFT)+1),"crypt device overlaps backing device");
731 OK_(crypt_resize(cd,CDEVICE_1, 123));
732 if (!device_size(path,&r_size))
733 EQ_(r_size>>SECTOR_SHIFT, 123);
734 OK_(crypt_resize(cd,CDEVICE_1,0)); // full size (autodetect)
735 if (!device_size(path,&r_size))
737 OK_(crypt_deactivate(cd,CDEVICE_1));
738 EQ_(crypt_status(cd,CDEVICE_1),CRYPT_INACTIVE);
742 OK_(crypt_init(&cd,DEVICE_1));
744 params.size = (size>>SECTOR_SHIFT) - params.offset - 10;
745 OK_(crypt_format(cd,CRYPT_PLAIN,cipher,cipher_mode,NULL,NULL,key_size,¶ms));
746 OK_(crypt_activate_by_volume_key(cd,CDEVICE_1,key,key_size,0));
747 if (!device_size(path,&r_size))
748 EQ_(r_size>>SECTOR_SHIFT, params.size);
749 // resize to fill remaining capacity
750 OK_(crypt_resize(cd,CDEVICE_1,params.size + 10));
751 if (!device_size(path,&r_size))
752 EQ_(r_size>>SECTOR_SHIFT, params.size + 10);
754 // 1 sector beyond real size
755 FAIL_(crypt_resize(cd,CDEVICE_1,params.size + 11), "new device size overlaps backing device"); // with respect to offset
756 if (!device_size(path,&r_size))
757 EQ_(r_size>>SECTOR_SHIFT, params.size + 10);
758 EQ_(crypt_status(cd,CDEVICE_1),CRYPT_ACTIVE);
759 fd = open(path, O_RDONLY);
763 // resize to minimal size
764 OK_(crypt_resize(cd,CDEVICE_1, 1)); // minimal device size
765 if (!device_size(path,&r_size))
766 EQ_(r_size>>SECTOR_SHIFT, 1);
767 // use size of backing device (autodetect with respect to offset)
768 OK_(crypt_resize(cd,CDEVICE_1,0));
769 if (!device_size(path,&r_size))
770 EQ_(r_size>>SECTOR_SHIFT, (size >> SECTOR_SHIFT)- 42);
771 OK_(crypt_deactivate(cd,CDEVICE_1));
776 OK_(crypt_init(&cd,DEVICE_1));
777 OK_(crypt_format(cd,CRYPT_PLAIN,cipher,cipher_mode,NULL,NULL,key_size,¶ms));
778 OK_(crypt_activate_by_volume_key(cd,CDEVICE_1,key,key_size,0));
780 // suspend/resume tests
781 FAIL_(crypt_suspend(cd,CDEVICE_1),"cannot suspend plain device");
782 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
783 FAIL_(crypt_resume_by_passphrase(cd,CDEVICE_1,CRYPT_ANY_SLOT,passphrase, strlen(passphrase)),"cannot resume plain device");
784 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
786 // retrieve volume key check
788 memset(key2, 0, key_size);
791 FAIL_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)), "small buffer");
793 OK_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)));
795 OK_(memcmp(key, key2, key_size));
797 OK_(strcmp(cipher, crypt_get_cipher(cd)));
798 OK_(strcmp(cipher_mode, crypt_get_cipher_mode(cd)));
799 EQ_((int)key_size, crypt_get_volume_key_size(cd));
800 EQ_(0, crypt_get_data_offset(cd));
801 OK_(crypt_deactivate(cd, CDEVICE_1));
804 OK_(_prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
805 OK_(_prepare_keyfile(KEYFILE2, KEY2, strlen(KEY2)));
806 FAIL_(crypt_activate_by_keyfile(cd, NULL, CRYPT_ANY_SLOT, KEYFILE1, 0, 0), "cannot verify key with plain");
807 EQ_(0, crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
808 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
809 OK_(crypt_deactivate(cd, CDEVICE_1));
810 FAIL_(crypt_activate_by_keyfile_offset(cd, NULL, CRYPT_ANY_SLOT, KEYFILE1, 0, strlen(KEY1) + 1, 0), "cannot seek");
811 EQ_(0, crypt_activate_by_keyfile_offset(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0, 0));
812 OK_(crypt_deactivate(cd, CDEVICE_1));
816 OK_(crypt_init(&cd,DEVICE_1));
817 OK_(crypt_format(cd,CRYPT_PLAIN,cipher,cipher_mode,NULL,NULL,key_size,¶ms));
820 FAIL_(crypt_keyslot_add_by_passphrase(cd,CRYPT_ANY_SLOT,passphrase,strlen(passphrase),passphrase,strlen(passphrase)), "can't add keyslot to plain device");
821 FAIL_(crypt_keyslot_add_by_volume_key(cd,CRYPT_ANY_SLOT ,key,key_size,passphrase,strlen(passphrase)),"can't add keyslot to plain device");
822 FAIL_(crypt_keyslot_add_by_keyfile(cd,CRYPT_ANY_SLOT,KEYFILE1,strlen(KEY1),KEYFILE2,strlen(KEY2)),"can't add keyslot to plain device");
823 FAIL_(crypt_keyslot_destroy(cd,1),"can't manipulate keyslots on plain device");
824 EQ_(crypt_keyslot_status(cd, 0), CRYPT_SLOT_INVALID);
830 #define CALLBACK_ERROR "calback_error xyz"
831 static int pass_callback_err(const char *msg, char *buf, size_t length, void *usrptr)
833 struct crypt_device *cd = usrptr;
839 crypt_log(cd, CRYPT_LOG_ERROR, CALLBACK_ERROR);
843 static int pass_callback_ok(const char *msg, char *buf, size_t length, void *usrptr)
847 strcpy(buf, PASSPHRASE);
851 static void CallbacksTest(void)
853 struct crypt_device *cd;
854 struct crypt_params_plain params = {
860 size_t key_size = 256 / 8;
861 const char *cipher = "aes";
862 const char *cipher_mode = "cbc-essiv:sha256";
863 const char *passphrase = PASSPHRASE;
864 char buf1[256] = {0}, buf2[256] = {0};
866 OK_(crypt_init(&cd, DEVICE_1));
867 crypt_set_log_callback(cd, &new_log, NULL);
868 //crypt_set_log_callback(cd, NULL, NULL);
870 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
872 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
873 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
874 OK_(crypt_deactivate(cd, CDEVICE_1));
877 crypt_set_password_callback(cd, pass_callback_err, cd);
878 FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, NULL, 0, 0), "callback fails");
879 EQ_(strncmp(global_log, CALLBACK_ERROR, strlen(CALLBACK_ERROR)), 0);
881 crypt_set_password_callback(cd, pass_callback_ok, NULL);
882 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, NULL, 0, 0));
883 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
884 OK_(crypt_deactivate(cd, CDEVICE_1));
886 // Check error reporting.
887 // This must fail and create error message
888 crypt_deactivate(cd, CDEVICE_1);
890 // Here context must be the same
891 crypt_get_error(buf1, sizeof(buf1));
892 crypt_last_error(cd, buf2, sizeof(buf2));
895 OK_(strcmp(buf1, buf2));
897 crypt_get_error(buf1, sizeof(buf1));
898 crypt_last_error(cd, buf2, sizeof(buf2));
905 static void UseLuksDevice(void)
907 struct crypt_device *cd;
911 OK_(crypt_init(&cd, DEVICE_1));
912 OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
913 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
914 OK_(crypt_activate_by_passphrase(cd, NULL, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0));
915 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0));
916 FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0), "already open");
917 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
918 OK_(crypt_deactivate(cd, CDEVICE_1));
919 FAIL_(crypt_deactivate(cd, CDEVICE_1), "no such device");
922 OK_(strcmp("aes", crypt_get_cipher(cd)));
923 OK_(strcmp("cbc-essiv:sha256", crypt_get_cipher_mode(cd)));
924 OK_(strcmp(DEVICE_1_UUID, crypt_get_uuid(cd)));
925 EQ_((int)key_size, crypt_get_volume_key_size(cd));
926 EQ_(1032, crypt_get_data_offset(cd));
929 EQ_(0, crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key, &key_size, KEY1, strlen(KEY1)));
930 OK_(crypt_volume_key_verify(cd, key, key_size));
931 OK_(crypt_activate_by_volume_key(cd, NULL, key, key_size, 0));
932 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
933 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
934 OK_(crypt_deactivate(cd, CDEVICE_1));
937 FAIL_(crypt_volume_key_verify(cd, key, key_size), "key mismatch");
938 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0), "key mismatch");
943 static void SuspendDevice(void)
946 struct crypt_device *cd;
948 OK_(crypt_init(&cd, DEVICE_1));
949 OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
950 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0));
952 suspend_status = crypt_suspend(cd, CDEVICE_1);
953 if (suspend_status == -ENOTSUP) {
954 printf("WARNING: Suspend/Resume not supported, skipping test.\n");
958 FAIL_(crypt_suspend(cd, CDEVICE_1), "already suspended");
960 FAIL_(crypt_resume_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1)-1), "wrong key");
961 OK_(crypt_resume_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1)));
962 FAIL_(crypt_resume_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1)), "not suspended");
964 OK_(_prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
965 OK_(crypt_suspend(cd, CDEVICE_1));
966 FAIL_(crypt_resume_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1 "blah", 0), "wrong keyfile");
967 FAIL_(crypt_resume_by_keyfile_offset(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 1, 0), "wrong key");
968 OK_(crypt_resume_by_keyfile_offset(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
969 FAIL_(crypt_resume_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0), "not suspended");
972 OK_(crypt_deactivate(cd, CDEVICE_1));
976 static void AddDeviceLuks(void)
978 struct crypt_device *cd;
979 struct crypt_params_luks1 params = {
981 .data_alignment = 2048, // 4M, data offset will be 4096
982 .data_device = DEVICE_2
984 char key[128], key2[128];
986 const char *passphrase = "blabla", *passphrase2 = "nsdkFI&Y#.sd";
987 const char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
988 size_t key_size = strlen(mk_hex) / 2;
989 const char *cipher = "aes";
990 const char *cipher_mode = "cbc-essiv:sha256";
991 uint64_t r_payload_offset, r_header_size, r_size_1;
993 crypt_decode_key(key, mk_hex, key_size);
996 OK_(get_luks_offsets(1, key_size, 0, 0, &r_header_size, &r_payload_offset));
997 OK_(create_dmdevice_over_loop(H_DEVICE, r_header_size));
998 OK_(create_dmdevice_over_loop(H_DEVICE_WRONG, r_header_size - 1));
1001 OK_(crypt_init(&cd, DMDIR H_DEVICE_WRONG));
1002 params.data_alignment = 0;
1003 FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms), "Not enough space for keyslots material");
1006 // test payload_offset = 0 for encrypted device with external header device
1007 OK_(crypt_init(&cd, DMDIR H_DEVICE));
1008 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1009 EQ_(crypt_get_data_offset(cd), 0);
1012 params.data_alignment = 0;
1013 params.data_device = NULL;
1015 // test payload_offset = 0. format() should look up alignment offset from device topology
1016 OK_(crypt_init(&cd, DEVICE_2));
1017 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1018 OK_(!(crypt_get_data_offset(cd) > 0));
1022 * test limit values for backing device size
1024 params.data_alignment = 4096;
1025 OK_(get_luks_offsets(0, key_size, params.data_alignment, 0, NULL, &r_payload_offset));
1026 OK_(create_dmdevice_over_loop(L_DEVICE_0S, r_payload_offset));
1027 OK_(create_dmdevice_over_loop(L_DEVICE_1S, r_payload_offset + 1));
1028 //OK_(create_dmdevice_over_loop(L_DEVICE_WRONG, r_payload_offset - 1));
1029 OK_(create_dmdevice_over_loop(L_DEVICE_WRONG, 2050 - 1)); //FIXME last keyslot - 1 sector
1031 // 1 sector less than required
1032 OK_(crypt_init(&cd, DMDIR L_DEVICE_WRONG));
1033 FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms), "Device too small");
1036 // 0 sectors for encrypted area
1037 OK_(crypt_init(&cd, DMDIR L_DEVICE_0S));
1038 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1039 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0), "Encrypted area too small");
1042 // 1 sector for encrypted area
1043 OK_(crypt_init(&cd, DMDIR L_DEVICE_1S));
1044 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1045 EQ_(crypt_get_data_offset(cd), params.data_alignment);
1046 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1047 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1048 OK_(device_size(DMDIR CDEVICE_1, &r_size_1));
1049 EQ_(r_size_1, SECTOR_SIZE);
1050 OK_(crypt_deactivate(cd, CDEVICE_1));
1051 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
1052 // restrict format only to empty context
1053 FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms), "Context is already formated");
1054 FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, NULL), "Context is already formated");
1055 // change data device to wrong one
1056 OK_(crypt_set_data_device(cd, DMDIR L_DEVICE_0S));
1057 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0), "Device too small");
1058 OK_(crypt_set_data_device(cd, DMDIR L_DEVICE_1S));
1059 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1060 OK_(crypt_deactivate(cd, CDEVICE_1));
1063 params.data_alignment = 0;
1064 params.data_device = DEVICE_2;
1066 // generate keyslot material at the end of luks header
1067 OK_(crypt_init(&cd, DMDIR H_DEVICE));
1068 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1069 EQ_(crypt_keyslot_add_by_volume_key(cd, 7, key, key_size, passphrase, strlen(passphrase)), 7);
1070 EQ_(crypt_activate_by_passphrase(cd, CDEVICE_1, 7, passphrase, strlen(passphrase) ,0), 7);
1072 OK_(crypt_init_by_name_and_header(&cd, CDEVICE_1, DMDIR H_DEVICE));
1073 FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms), "Context is already formated");
1074 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1075 OK_(crypt_deactivate(cd, CDEVICE_1));
1078 params.data_alignment = 2048;
1079 params.data_device = NULL;
1081 // test uuid mismatch and _init_by_name_and_header
1082 OK_(crypt_init(&cd, DMDIR L_DEVICE_1S));
1083 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1084 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1086 params.data_alignment = 0;
1087 params.data_device = DEVICE_2;
1088 OK_(crypt_init(&cd, DMDIR H_DEVICE));
1089 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1091 // there we've got uuid mismatch
1092 OK_(crypt_init_by_name_and_header(&cd, CDEVICE_1, DMDIR H_DEVICE));
1093 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1094 OK_(!!crypt_get_type(cd));
1095 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0), "Device is active");
1096 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, key, key_size, 0), "Device is active");
1097 EQ_(crypt_status(cd, CDEVICE_2), CRYPT_INACTIVE);
1098 OK_(crypt_deactivate(cd, CDEVICE_1));
1101 params.data_device = NULL;
1103 OK_(crypt_init(&cd, DEVICE_2));
1104 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1106 // even with no keyslots defined it can be activated by volume key
1107 OK_(crypt_volume_key_verify(cd, key, key_size));
1108 OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, key, key_size, 0));
1109 EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
1110 OK_(crypt_deactivate(cd, CDEVICE_2));
1113 EQ_(7, crypt_keyslot_add_by_volume_key(cd, 7, key, key_size, passphrase, strlen(passphrase)));
1114 EQ_(CRYPT_SLOT_ACTIVE_LAST, crypt_keyslot_status(cd, 7));
1115 EQ_(7, crypt_activate_by_passphrase(cd, CDEVICE_2, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
1116 EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
1117 OK_(crypt_deactivate(cd, CDEVICE_2));
1119 crypt_set_iteration_time(cd, 1);
1120 EQ_(1, crypt_keyslot_add_by_volume_key(cd, 1, key, key_size, KEY1, strlen(KEY1)));
1121 OK_(_prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
1122 OK_(_prepare_keyfile(KEYFILE2, KEY2, strlen(KEY2)));
1123 EQ_(2, crypt_keyslot_add_by_keyfile(cd, 2, KEYFILE1, 0, KEYFILE2, 0));
1124 FAIL_(crypt_keyslot_add_by_keyfile_offset(cd, 3, KEYFILE1, 0, 1, KEYFILE2, 0, 1), "wrong key");
1125 EQ_(3, crypt_keyslot_add_by_keyfile_offset(cd, 3, KEYFILE1, 0, 0, KEYFILE2, 0, 1));
1126 EQ_(4, crypt_keyslot_add_by_keyfile_offset(cd, 4, KEYFILE2, 0, 1, KEYFILE1, 0, 1));
1127 FAIL_(crypt_activate_by_keyfile(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, strlen(KEY2)-1, 0), "key mismatch");
1128 EQ_(2, crypt_activate_by_keyfile(cd, NULL, CRYPT_ANY_SLOT, KEYFILE2, 0, 0));
1129 EQ_(3, crypt_activate_by_keyfile_offset(cd, NULL, CRYPT_ANY_SLOT, KEYFILE2, 0, 1, 0));
1130 EQ_(4, crypt_activate_by_keyfile_offset(cd, NULL, CRYPT_ANY_SLOT, KEYFILE1, 0, 1, 0));
1131 FAIL_(crypt_activate_by_keyfile_offset(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, strlen(KEY2), 2, 0), "not enough data");
1132 FAIL_(crypt_activate_by_keyfile_offset(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, 0, strlen(KEY2) + 1, 0), "cannot seek");
1133 FAIL_(crypt_activate_by_keyfile_offset(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, 0, 2, 0), "wrong key");
1134 EQ_(2, crypt_activate_by_keyfile(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, 0, 0));
1135 OK_(crypt_keyslot_destroy(cd, 1));
1136 OK_(crypt_keyslot_destroy(cd, 2));
1137 OK_(crypt_keyslot_destroy(cd, 3));
1138 OK_(crypt_keyslot_destroy(cd, 4));
1139 OK_(crypt_deactivate(cd, CDEVICE_2));
1142 FAIL_(crypt_keyslot_add_by_volume_key(cd, 7, key, key_size, passphrase, strlen(passphrase)), "slot used");
1144 FAIL_(crypt_keyslot_add_by_volume_key(cd, 6, key, key_size, passphrase, strlen(passphrase)), "key mismatch");
1146 EQ_(6, crypt_keyslot_add_by_volume_key(cd, 6, key, key_size, passphrase, strlen(passphrase)));
1147 EQ_(CRYPT_SLOT_ACTIVE, crypt_keyslot_status(cd, 6));
1149 FAIL_(crypt_keyslot_destroy(cd, 8), "invalid keyslot");
1150 FAIL_(crypt_keyslot_destroy(cd, CRYPT_ANY_SLOT), "invalid keyslot");
1151 FAIL_(crypt_keyslot_destroy(cd, 0), "keyslot not used");
1152 OK_(crypt_keyslot_destroy(cd, 7));
1153 EQ_(CRYPT_SLOT_INACTIVE, crypt_keyslot_status(cd, 7));
1154 EQ_(CRYPT_SLOT_ACTIVE_LAST, crypt_keyslot_status(cd, 6));
1156 EQ_(7, crypt_keyslot_change_by_passphrase(cd, 6, 7, passphrase, strlen(passphrase), passphrase2, strlen(passphrase2)));
1157 EQ_(CRYPT_SLOT_ACTIVE_LAST, crypt_keyslot_status(cd, 7));
1158 EQ_(7, crypt_activate_by_passphrase(cd, NULL, 7, passphrase2, strlen(passphrase2), 0));
1159 EQ_(6, crypt_keyslot_change_by_passphrase(cd, CRYPT_ANY_SLOT, 6, passphrase2, strlen(passphrase2), passphrase, strlen(passphrase)));
1162 EQ_(6, crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)));
1163 OK_(crypt_volume_key_verify(cd, key2, key_size));
1165 OK_(memcmp(key, key2, key_size));
1167 OK_(strcmp(cipher, crypt_get_cipher(cd)));
1168 OK_(strcmp(cipher_mode, crypt_get_cipher_mode(cd)));
1169 EQ_((int)key_size, crypt_get_volume_key_size(cd));
1170 EQ_(4096, crypt_get_data_offset(cd));
1171 OK_(strcmp(DEVICE_2, crypt_get_device_name(cd)));
1174 crypt_set_log_callback(cd, &new_log, NULL);
1175 OK_(crypt_dump(cd));
1176 OK_(!(global_lines != 0));
1177 crypt_set_log_callback(cd, NULL, NULL);
1180 FAIL_(crypt_set_uuid(cd, "blah"), "wrong UUID format");
1181 OK_(crypt_set_uuid(cd, DEVICE_TEST_UUID));
1182 OK_(strcmp(DEVICE_TEST_UUID, crypt_get_uuid(cd)));
1184 FAIL_(crypt_deactivate(cd, CDEVICE_2), "not active");
1186 _cleanup_dmdevices();
1189 static void UseTempVolumes(void)
1191 struct crypt_device *cd;
1194 // Tepmporary device without keyslot but with on-disk LUKS header
1195 OK_(crypt_init(&cd, DEVICE_2));
1196 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "not yet formatted");
1197 OK_(crypt_format(cd, CRYPT_LUKS1, "aes", "cbc-essiv:sha256", NULL, NULL, 16, NULL));
1198 OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0));
1199 EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
1202 OK_(crypt_init_by_name(&cd, CDEVICE_2));
1203 OK_(crypt_deactivate(cd, CDEVICE_2));
1206 // Dirty checks: device without UUID
1207 // we should be able to remove it but not manuipulate with it
1208 snprintf(tmp, sizeof(tmp), "dmsetup create %s --table \""
1209 "0 100 crypt aes-cbc-essiv:sha256 deadbabedeadbabedeadbabedeadbabe 0 "
1210 "%s 2048\"", CDEVICE_2, DEVICE_2);
1212 OK_(crypt_init_by_name(&cd, CDEVICE_2));
1213 OK_(crypt_deactivate(cd, CDEVICE_2));
1214 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "No known device type");
1217 // Dirty checks: device with UUID but LUKS header key fingerprint must fail)
1218 snprintf(tmp, sizeof(tmp), "dmsetup create %s --table \""
1219 "0 100 crypt aes-cbc-essiv:sha256 deadbabedeadbabedeadbabedeadbabe 0 "
1220 "%s 2048\" -u CRYPT-LUKS1-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-ctest1",
1221 CDEVICE_2, DEVICE_2);
1223 OK_(crypt_init_by_name(&cd, CDEVICE_2));
1224 OK_(crypt_deactivate(cd, CDEVICE_2));
1225 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "wrong volume key");
1229 OK_(crypt_init(&cd, DEVICE_2));
1230 OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
1231 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "volume key is lost");
1235 OK_(crypt_init(&cd, DEVICE_2));
1236 OK_(crypt_format(cd, CRYPT_PLAIN, "aes", "cbc-essiv:sha256", NULL, NULL, 16, NULL));
1237 FAIL_(crypt_activate_by_volume_key(cd, NULL, "xxx", 3, 0), "cannot verify key with plain");
1238 FAIL_(crypt_volume_key_verify(cd, "xxx", 3), "cannot verify key with plain");
1239 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, "xxx", 3, 0), "wrong key lenght");
1240 OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, "volumekeyvolumek", 16, 0));
1241 EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
1242 OK_(crypt_deactivate(cd, CDEVICE_2));
1246 static void LuksHeaderRestore(void)
1248 struct crypt_device *cd;
1249 struct crypt_params_luks1 params = {
1251 .data_alignment = 2048, // 4M, data offset will be 4096
1253 struct crypt_params_plain pl_params = {
1259 char key[128], key2[128], cmd[256];
1261 const char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
1262 size_t key_size = strlen(mk_hex) / 2;
1263 const char *cipher = "aes";
1264 const char *cipher_mode = "cbc-essiv:sha256";
1265 uint64_t r_payload_offset;
1267 crypt_decode_key(key, mk_hex, key_size);
1269 OK_(get_luks_offsets(0, key_size, params.data_alignment, 0, NULL, &r_payload_offset));
1270 OK_(create_dmdevice_over_loop(L_DEVICE_OK, r_payload_offset + 5000));
1272 // do not restore header over plain device
1273 OK_(crypt_init(&cd, DMDIR L_DEVICE_OK));
1274 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, &pl_params));
1275 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1276 FAIL_(crypt_header_restore(cd, CRYPT_PLAIN, VALID_HEADER), "Cannot restore header to PLAIN type device");
1277 FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, VALID_HEADER), "Cannot restore header over PLAIN type device");
1278 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1279 OK_(crypt_deactivate(cd, CDEVICE_1));
1283 OK_(crypt_init(&cd, DMDIR L_DEVICE_OK));
1284 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1285 FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, EVL_HEADER_1), "Header corrupted");
1286 FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, EVL_HEADER_2), "Header corrupted");
1287 FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, EVL_HEADER_3), "Header corrupted");
1288 FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, EVL_HEADER_4), "Header too small");
1289 OK_(crypt_header_restore(cd, CRYPT_LUKS1, VALID_HEADER));
1290 // wipe valid luks header
1291 snprintf(cmd, sizeof(cmd), "dd if=/dev/zero of=" DMDIR L_DEVICE_OK " bs=512 count=%" PRIu64 " 2>/dev/null", r_payload_offset);
1292 OK_(_system(cmd, 1));
1293 FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, EVL_HEADER_1), "Header corrupted");
1294 FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, EVL_HEADER_2), "Header corrupted");
1295 FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, EVL_HEADER_3), "Header corrupted");
1296 FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, EVL_HEADER_4), "Header too small");
1297 OK_(crypt_header_restore(cd, CRYPT_LUKS1, VALID_HEADER));
1298 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1299 OK_(crypt_deactivate(cd, CDEVICE_1));
1302 // volume key_size mismatch
1303 OK_(crypt_init(&cd, DMDIR L_DEVICE_OK));
1304 memcpy(key2, key, key_size - 1);
1305 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key2, key_size - 1, ¶ms));
1306 FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, VALID_HEADER), "Volume keysize mismatch");
1309 // payload offset mismatch
1310 params.data_alignment = 8192;
1311 OK_(crypt_init(&cd, DMDIR L_DEVICE_OK));
1312 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1313 FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, VALID_HEADER), "Payload offset mismatch");
1314 //_system("dmsetup table;sleep 1",1);
1317 _cleanup_dmdevices();
1320 static void LuksHeaderLoad(void)
1322 struct crypt_device *cd;
1323 struct crypt_params_luks1 params = {
1325 .data_alignment = 2048,
1327 struct crypt_params_plain pl_params = {
1333 char key[128], cmd[256];
1335 const char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
1336 size_t key_size = strlen(mk_hex) / 2;
1337 const char *cipher = "aes";
1338 const char *cipher_mode = "cbc-essiv:sha256";
1339 uint64_t r_payload_offset, r_header_size;
1341 crypt_decode_key(key, mk_hex, key_size);
1344 OK_(get_luks_offsets(0, key_size, params.data_alignment, 0, &r_header_size, &r_payload_offset));
1345 // external header device
1346 OK_(create_dmdevice_over_loop(H_DEVICE, r_header_size));
1347 // prepared header on a device too small to contain header and payload
1348 //OK_(create_dmdevice_over_loop(H_DEVICE_WRONG, r_payload_offset - 1));
1349 OK_(create_dmdevice_over_loop(H_DEVICE_WRONG, 2050 - 1)); //FIXME
1350 //snprintf(cmd, sizeof(cmd), "dd if=" EVL_HEADER_4 " of=" DMDIR H_DEVICE_WRONG " bs=512 count=%" PRIu64, r_payload_offset - 1);
1351 snprintf(cmd, sizeof(cmd), "dd if=" EVL_HEADER_4 " of=" DMDIR H_DEVICE_WRONG " bs=512 count=%d 2>/dev/null", 2050 - 1);
1352 OK_(_system(cmd, 1));
1354 OK_(create_dmdevice_over_loop(L_DEVICE_OK, r_payload_offset + 1000));
1356 OK_(create_dmdevice_over_loop(L_DEVICE_1S, r_payload_offset + 1));
1357 // 0 sectors device for payload
1358 OK_(create_dmdevice_over_loop(L_DEVICE_0S, r_payload_offset));
1360 // valid metadata and device size
1361 params.data_alignment = 0;
1362 params.data_device = DMDIR L_DEVICE_OK;
1363 OK_(crypt_init(&cd, DMDIR H_DEVICE));
1364 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1366 OK_(crypt_init(&cd, DMDIR H_DEVICE));
1367 OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
1368 OK_(crypt_set_data_device(cd, DMDIR L_DEVICE_OK));
1369 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1370 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1371 OK_(crypt_deactivate(cd, CDEVICE_1));
1374 // bad header: device too small (payloadOffset > device_size)
1375 OK_(crypt_init(&cd, DMDIR H_DEVICE_WRONG));
1376 FAIL_(crypt_load(cd, CRYPT_LUKS1, NULL), "Device too small");
1377 OK_(!!crypt_get_type(cd));
1380 // 0 secs for encrypted data area
1381 params.data_alignment = 2048;
1382 params.data_device = NULL;
1383 OK_(crypt_init(&cd, DMDIR L_DEVICE_0S));
1384 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1386 // load should be ok
1387 OK_(crypt_init(&cd, DMDIR L_DEVICE_0S));
1388 OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
1389 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0), "Device too small");
1390 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
1394 OK_(_system("dd if=/dev/zero of=" DMDIR L_DEVICE_OK " bs=512 count=8 2>/dev/null", 1));
1395 OK_(crypt_init(&cd, DMDIR L_DEVICE_OK));
1396 FAIL_(crypt_load(cd, CRYPT_LUKS1, NULL), "Header not found");
1400 OK_(crypt_init(&cd, DMDIR H_DEVICE));
1401 FAIL_(crypt_load(cd, CRYPT_PLAIN, NULL), "Can't load nonLUKS device type");
1403 OK_(crypt_init(&cd, DMDIR H_DEVICE));
1404 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, key, key_size, &pl_params));
1405 FAIL_(crypt_load(cd, CRYPT_LUKS1, NULL), "Can't load over nonLUKS device type");
1408 _cleanup_dmdevices();
1411 static void LuksHeaderBackup(void)
1413 struct crypt_device *cd;
1414 struct crypt_params_luks1 params = {
1416 .data_alignment = 2048,
1420 const char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
1421 size_t key_size = strlen(mk_hex) / 2;
1422 const char *cipher = "aes";
1423 const char *cipher_mode = "cbc-essiv:sha256";
1424 uint64_t r_payload_offset;
1426 crypt_decode_key(key, mk_hex, key_size);
1428 OK_(get_luks_offsets(0, key_size, params.data_alignment, 0, NULL, &r_payload_offset));
1429 OK_(create_dmdevice_over_loop(L_DEVICE_OK, r_payload_offset + 1));
1431 // create LUKS device and backup the header
1432 OK_(crypt_init(&cd, DMDIR L_DEVICE_OK));
1433 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1434 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1435 OK_(crypt_header_backup(cd, CRYPT_LUKS1, BACKUP_FILE));
1436 OK_(crypt_deactivate(cd, CDEVICE_1));
1439 // restore header from backup
1440 OK_(crypt_init(&cd, DMDIR L_DEVICE_OK));
1441 OK_(crypt_header_restore(cd, CRYPT_LUKS1, BACKUP_FILE));
1442 OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
1443 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1444 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1445 OK_(crypt_deactivate(cd, CDEVICE_1));
1448 _cleanup_dmdevices();
1451 static void ResizeDeviceLuks(void)
1453 struct crypt_device *cd;
1454 struct crypt_params_luks1 params = {
1456 .data_alignment = 2048,
1460 const char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
1461 size_t key_size = strlen(mk_hex) / 2;
1462 const char *cipher = "aes";
1463 const char *cipher_mode = "cbc-essiv:sha256";
1464 uint64_t r_payload_offset, r_header_size, r_size;
1466 crypt_decode_key(key, mk_hex, key_size);
1469 OK_(get_luks_offsets(0, key_size, params.data_alignment, 0, NULL, &r_payload_offset));
1470 OK_(get_luks_offsets(1, key_size, 0, 0, &r_header_size, NULL));
1471 OK_(create_dmdevice_over_loop(H_DEVICE, r_header_size));
1472 OK_(create_dmdevice_over_loop(L_DEVICE_OK, r_payload_offset + 1000));
1473 OK_(create_dmdevice_over_loop(L_DEVICE_0S, 1000));
1475 // test header and encrypted payload all in one device
1476 OK_(crypt_init(&cd, DMDIR L_DEVICE_OK));
1477 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1478 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1479 OK_(crypt_resize(cd, CDEVICE_1, 42));
1480 if (!device_size(DMDIR CDEVICE_1, &r_size))
1481 EQ_(42, r_size >> SECTOR_SHIFT);
1482 // autodetect encrypted device area size
1483 OK_(crypt_resize(cd, CDEVICE_1, 0));
1484 if (!device_size(DMDIR CDEVICE_1, &r_size))
1485 EQ_(1000, r_size >> SECTOR_SHIFT);
1486 FAIL_(crypt_resize(cd, CDEVICE_1, 1001), "Device too small");
1487 if (!device_size(DMDIR CDEVICE_1, &r_size))
1488 EQ_(1000, r_size >> SECTOR_SHIFT);
1489 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1490 OK_(crypt_deactivate(cd, CDEVICE_1));
1493 params.data_alignment = 0;
1494 params.data_device = DMDIR L_DEVICE_0S;
1495 // test case for external header
1496 OK_(crypt_init(&cd, DMDIR H_DEVICE));
1497 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1498 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1499 OK_(crypt_resize(cd, CDEVICE_1, 666));
1500 if (!device_size(DMDIR CDEVICE_1, &r_size))
1501 EQ_(666, r_size >> SECTOR_SHIFT);
1502 // autodetect encrypted device size
1503 OK_(crypt_resize(cd, CDEVICE_1, 0));
1504 if (!device_size(DMDIR CDEVICE_1, &r_size))
1505 EQ_(1000, r_size >> SECTOR_SHIFT);
1506 FAIL_(crypt_resize(cd, CDEVICE_1, 1001), "Device too small");
1507 if (!device_size(DMDIR CDEVICE_1, &r_size))
1508 EQ_(1000, r_size >> SECTOR_SHIFT);
1509 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1510 OK_(crypt_deactivate(cd, CDEVICE_1));
1513 _cleanup_dmdevices();
1516 static void HashDevicePlain(void)
1518 struct crypt_device *cd;
1519 struct crypt_params_plain params = {
1526 const char *mk_hex, *keystr;
1529 OK_(crypt_init(&cd, DEVICE_1));
1530 OK_(crypt_format(cd, CRYPT_PLAIN, "aes", "cbc-essiv:sha256", NULL, NULL, 16, ¶ms));
1532 // hash PLAIN, short key
1533 OK_(_prepare_keyfile(KEYFILE1, "tooshort", 8));
1534 FAIL_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 16, 0), "not enough data in keyfile");
1537 // hash PLAIN, exact key
1538 // 0 1 2 3 4 5 6 7 8 9 a b c d e f
1539 mk_hex = "caffeecaffeecaffeecaffeecaffee88";
1541 crypt_decode_key(key, mk_hex, key_size);
1542 OK_(_prepare_keyfile(KEYFILE1, key, key_size));
1543 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size, 0));
1544 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1545 OK_(strcmp(key, mk_hex));
1546 OK_(crypt_deactivate(cd, CDEVICE_1));
1549 mk_hex = "caffeecaffeecaffeecaffeeca000000";
1550 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size - 3, 0));
1551 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1552 OK_(strcmp(key, mk_hex));
1553 OK_(crypt_deactivate(cd, CDEVICE_1));
1557 // hash PLAIN, long key
1558 // 0 1 2 3 4 5 6 7 8 9 a b c d e f
1559 mk_hex = "caffeecaffeecaffeecaffeecaffee88babebabe";
1561 crypt_decode_key(key, mk_hex, key_size);
1562 OK_(_prepare_keyfile(KEYFILE1, key, strlen(mk_hex) / 2));
1563 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size, 0));
1564 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1565 FAIL_(strcmp(key, mk_hex), "only key length used");
1566 OK_(strncmp(key, mk_hex, key_size));
1567 OK_(crypt_deactivate(cd, CDEVICE_1));
1570 // Now without explicit limit
1571 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
1572 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1573 FAIL_(strcmp(key, mk_hex), "only key length used");
1574 OK_(strncmp(key, mk_hex, key_size));
1575 OK_(crypt_deactivate(cd, CDEVICE_1));
1581 params.hash = "sha256";
1582 OK_(crypt_init(&cd, DEVICE_1));
1583 OK_(crypt_format(cd, CRYPT_PLAIN, "aes", "cbc-essiv:sha256", NULL, NULL, 16, ¶ms));
1585 // 0 1 2 3 4 5 6 7 8 9 a b c d e f
1586 mk_hex = "c62e4615bd39e222572f3a1bf7c2132e";
1587 keystr = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
1588 key_size = strlen(keystr); // 32
1589 OK_(_prepare_keyfile(KEYFILE1, keystr, strlen(keystr)));
1590 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size, 0));
1591 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1592 OK_(strcmp(key, mk_hex));
1593 OK_(crypt_deactivate(cd, CDEVICE_1));
1595 // Read full keyfile
1596 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
1597 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1598 OK_(strcmp(key, mk_hex));
1599 OK_(crypt_deactivate(cd, CDEVICE_1));
1603 // Limit keyfile read
1604 keystr = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxAAAAAAAA";
1605 OK_(_prepare_keyfile(KEYFILE1, keystr, strlen(keystr)));
1606 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size, 0));
1607 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1608 OK_(strcmp(key, mk_hex));
1609 OK_(crypt_deactivate(cd, CDEVICE_1));
1612 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
1613 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1614 OK_(strcmp(key, "0e49cb34a1dee1df33f6505e4de44a66"));
1615 OK_(crypt_deactivate(cd, CDEVICE_1));
1619 // FIXME: add keyfile="-" tests somehow
1624 static void VerityTest(void)
1626 struct crypt_device *cd;
1627 const char *salt_hex = "20c28ffc129c12360ba6ceea2b6cf04e89c2b41cfe6b8439eb53c1897f50df7b";
1628 const char *root_hex = "ab018b003a967fc782effb293b6dccb60b4f40c06bf80d16391acf686d28b5d6";
1629 char salt[256], root_hash[256];
1630 struct crypt_active_device cad;
1631 struct crypt_params_verity params = {
1632 .data_device = DEVICE_EMPTY,
1634 .data_size = 0, /* whole device */
1635 .hash_area_offset = 0,
1636 .flags = CRYPT_VERITY_CREATE_HASH,
1639 crypt_decode_key(salt, salt_hex, strlen(salt_hex) / 2);
1640 crypt_decode_key(root_hash, root_hex, strlen(root_hex) / 2);
1643 OK_(crypt_init(&cd, DEVICE_2));
1646 params.data_block_size = 333;
1647 FAIL_(crypt_format(cd, CRYPT_VERITY, NULL, NULL, NULL, NULL, 0, ¶ms),
1648 "Unsupppored block size.");
1649 params.data_block_size = 4096;
1650 params.hash_block_size = 333;
1651 FAIL_(crypt_format(cd, CRYPT_VERITY, NULL, NULL, NULL, NULL, 0, ¶ms),
1652 "Unsupppored block size.");
1653 params.hash_block_size = 4096;
1656 params.salt_size = 257;
1657 FAIL_(crypt_format(cd, CRYPT_VERITY, NULL, NULL, NULL, NULL, 0, ¶ms),
1659 params.salt_size = 32;
1662 params.hash_type = 3;
1663 FAIL_(crypt_format(cd, CRYPT_VERITY, NULL, NULL, NULL, NULL, 0, ¶ms),
1664 "Unsupported hash type.");
1665 params.hash_type = 1;
1666 params.hash_name = "blah";
1667 FAIL_(crypt_format(cd, CRYPT_VERITY, NULL, NULL, NULL, NULL, 0, ¶ms),
1668 "Unsupported hash name.");
1669 params.hash_name = "sha256";
1671 OK_(crypt_format(cd, CRYPT_VERITY, NULL, NULL, NULL, NULL, 0, ¶ms));
1675 OK_(crypt_init(&cd, DEVICE_2));
1676 memset(¶ms, 0, sizeof(params));
1677 params.data_device = DEVICE_EMPTY;
1678 params.flags = CRYPT_VERITY_CHECK_HASH;
1679 OK_(crypt_load(cd, CRYPT_VERITY, ¶ms));
1681 /* check verity params */
1682 EQ_(crypt_get_volume_key_size(cd), 32);
1683 OK_(strcmp(CRYPT_VERITY, crypt_get_type(cd)));
1684 memset(¶ms, 0, sizeof(params));
1685 OK_(crypt_get_verity_info(cd, ¶ms));
1686 OK_(strcmp("sha256", params.hash_name));
1687 EQ_(strlen(salt_hex) / 2, params.salt_size);
1688 OK_(memcmp(salt, params.salt, params.salt_size));
1689 EQ_(4096, params.data_block_size);
1690 EQ_(4096, params.hash_block_size);
1691 EQ_(1, params.hash_type);
1692 EQ_(crypt_get_volume_key_size(cd), 32);
1694 OK_(crypt_activate_by_volume_key(cd, NULL, root_hash, 32, 0));
1695 OK_(crypt_set_data_device(cd, DEVICE_1));
1696 FAIL_(crypt_activate_by_volume_key(cd, NULL, root_hash, 32, 0), "Data corrupted");;
1698 OK_(crypt_set_data_device(cd, DEVICE_EMPTY));
1699 if (crypt_activate_by_volume_key(cd, CDEVICE_1, root_hash, 32,
1700 CRYPT_ACTIVATE_READONLY) == -ENOTSUP) {
1701 printf("WARNING: kernel dm-verity not supported, skipping test.\n");
1705 OK_(crypt_get_active_device(cd, CDEVICE_1, &cad));
1706 EQ_(CRYPT_ACTIVATE_READONLY, cad.flags);
1709 OK_(crypt_init_by_name(&cd, CDEVICE_1));
1710 OK_(crypt_deactivate(cd, CDEVICE_1));
1713 root_hash[1] = ~root_hash[1];
1714 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, root_hash, 32, CRYPT_ACTIVATE_READONLY));
1715 OK_(crypt_get_active_device(cd, CDEVICE_1, &cad));
1716 EQ_(CRYPT_ACTIVATE_READONLY|CRYPT_ACTIVATE_CORRUPTED, cad.flags);
1717 OK_(crypt_deactivate(cd, CDEVICE_1));
1718 root_hash[1] = ~root_hash[1];
1721 OK_(crypt_set_data_device(cd, DEVICE_1));
1722 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, root_hash, 32, CRYPT_ACTIVATE_READONLY));
1723 OK_(crypt_get_active_device(cd, CDEVICE_1, &cad));
1724 EQ_(CRYPT_ACTIVATE_READONLY|CRYPT_ACTIVATE_CORRUPTED, cad.flags);
1725 OK_(crypt_deactivate(cd, CDEVICE_1));
1730 static void TcryptTest(void)
1732 struct crypt_device *cd = NULL;
1733 struct crypt_active_device cad;
1734 const char *passphrase = "aaaaaaaaaaaa";
1735 const char *kf1 = "tcrypt-images/keyfile1";
1736 const char *kf2 = "tcrypt-images/keyfile2";
1737 const char *keyfiles[] = { kf1, kf2 };
1738 struct crypt_params_tcrypt params = {
1739 .passphrase = passphrase,
1740 .passphrase_size = strlen(passphrase),
1741 .keyfiles = keyfiles,
1742 .keyfiles_count = 2,
1744 double enc_mbr = 0, dec_mbr = 0;
1745 const char *tcrypt_dev = "tcrypt-images/tck_5-sha512-xts-aes";
1746 size_t key_size = 64;
1747 char key[key_size], key_def[key_size];
1748 const char *key_hex =
1749 "98dee64abe44bbf41d171c1f7b3e8eacda6d6b01f459097459a167f8c2872a96"
1750 "3979531d1cdc18af62757cf22286f16f8583d848524f128d7594ac2082668c73";
1753 crypt_decode_key(key_def, key_hex, strlen(key_hex) / 2);
1755 // First ensure we can use af_alg skcipher interface
1756 r = crypt_benchmark(NULL, "aes", "xts", 512, 16, 1024, &enc_mbr, &dec_mbr);
1757 if (r == -ENOTSUP || r == -ENOENT) {
1758 printf("WARNING: algif_skcipher interface not present, skipping test.\n");
1762 OK_(crypt_init(&cd, tcrypt_dev));
1763 params.passphrase_size--;
1764 FAIL_(crypt_load(cd, CRYPT_TCRYPT, ¶ms), "Wrong passphrase");
1765 params.passphrase_size++;
1766 OK_(crypt_load(cd, CRYPT_TCRYPT, ¶ms));
1768 // check params after load
1769 OK_(strcmp("xts-plain64", crypt_get_cipher_mode(cd)));
1770 OK_(strcmp("aes", crypt_get_cipher(cd)));
1771 EQ_(key_size, crypt_get_volume_key_size(cd));
1772 EQ_(256, crypt_get_iv_offset(cd));
1773 EQ_(256, crypt_get_data_offset(cd));
1775 memset(key, 0, key_size);
1779 FAIL_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key, &key_size, NULL, 0), "small buffer");
1781 OK_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key, &key_size, NULL, 0));
1782 OK_(memcmp(key, key_def, key_size));
1786 crypt_set_log_callback(cd, &new_log, NULL);
1787 OK_(crypt_dump(cd));
1788 OK_(!(global_lines != 0));
1789 crypt_set_log_callback(cd, NULL, NULL);
1792 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, NULL, 0, CRYPT_ACTIVATE_READONLY));
1795 OK_(crypt_init_by_name_and_header(&cd, CDEVICE_1, NULL));
1796 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1798 FAIL_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key, &key_size, NULL, 0), "Need crypt_load");
1800 // check params after init_by_name
1801 OK_(strcmp("xts-plain64", crypt_get_cipher_mode(cd)));
1802 OK_(strcmp("aes", crypt_get_cipher(cd)));
1803 EQ_(key_size, crypt_get_volume_key_size(cd));
1804 EQ_(256, crypt_get_iv_offset(cd));
1805 EQ_(256, crypt_get_data_offset(cd));
1807 OK_(crypt_get_active_device(cd, CDEVICE_1, &cad));
1808 EQ_(CRYPT_ACTIVATE_READONLY, cad.flags);
1809 EQ_(256, cad.offset);
1810 EQ_(256, cad.iv_offset);
1813 OK_(crypt_deactivate(cd, CDEVICE_1));
1817 // Check that gcrypt is properly initialised in format
1818 static void NonFIPSAlg(void)
1820 struct crypt_device *cd;
1821 struct crypt_params_luks1 params = {0};
1823 size_t key_size = 128;
1824 const char *cipher = "aes";
1825 const char *cipher_mode = "cbc-essiv:sha256";
1828 OK_(crypt_init(&cd, DEVICE_2));
1829 params.hash = "sha256";
1830 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1831 FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms),
1832 "Already formatted.");
1835 params.hash = "whirlpool";
1836 OK_(crypt_init(&cd, DEVICE_2));
1837 ret = crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms);
1839 printf("WARNING: whirlpool not supported, skipping test.\n");
1845 params.hash = "md5";
1846 OK_(crypt_init(&cd, DEVICE_2));
1847 FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms),
1848 "MD5 unsupported, too short");
1852 int main(int argc, char *argv[])
1856 if (getuid() != 0) {
1857 printf("You must be root to run this test.\n");
1861 for (i = 1; i < argc; i++) {
1862 if (!strcmp("-v", argv[i]) || !strcmp("--verbose", argv[i]))
1864 else if (!strcmp("--debug", argv[i]))
1865 _debug = _verbose = 1;
1872 crypt_set_debug_level(_debug ? CRYPT_DEBUG_ALL : CRYPT_DEBUG_NONE);
1874 RUN_(NonFIPSAlg, "Crypto is properly initialised in format"); //must be the first!
1875 RUN_(AddDevicePlain, "plain device API creation exercise");
1876 RUN_(HashDevicePlain, "plain device API hash test");
1877 RUN_(AddDeviceLuks, "Format and use LUKS device");
1878 RUN_(LuksHeaderLoad, "test header load");
1879 RUN_(LuksHeaderRestore, "test LUKS header restore");
1880 RUN_(LuksHeaderBackup, "test LUKS header backup");
1881 RUN_(ResizeDeviceLuks, "Luks device resize tests");
1882 RUN_(UseLuksDevice, "Use pre-formated LUKS device");
1883 RUN_(SuspendDevice, "Suspend/Resume test");
1884 RUN_(UseTempVolumes, "Format and use temporary encrypted device");
1885 RUN_(CallbacksTest, "API callbacks test");
1886 RUN_(VerityTest, "DM verity test");
1887 RUN_(TcryptTest, "Tcrypt API test");