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;
88 static char global_log[4096];
89 static int global_lines = 0;
91 static char *DEVICE_1 = NULL;
92 static char *DEVICE_2 = NULL;
93 static char *THE_LOOP_DEV = NULL;
95 static char *tmp_file_1 = NULL;
96 static char *test_loop_file = NULL;
97 static uint64_t t_dev_offset = 0;
99 static int _system(const char*, int);
103 static int device_size(const char *device, uint64_t *size)
107 devfd = open(device, O_RDONLY);
111 if (ioctl(devfd, BLKGETSIZE64, size) < 0)
117 static int get_luks_offsets(int metadata_device,
119 unsigned int alignpayload_sec,
120 unsigned int alignoffset_sec,
121 uint64_t *r_header_size,
122 uint64_t *r_payload_offset)
125 uint64_t current_sector;
126 uint32_t sectors_per_stripes_set;
131 sectors_per_stripes_set = DIV_ROUND_UP(keylength*LUKS_STRIPES, SECTOR_SIZE);
132 printf("sectors_per_stripes %" PRIu32 "\n", sectors_per_stripes_set);
133 current_sector = DIV_ROUND_UP_MODULO(DIV_ROUND_UP(LUKS_PHDR_SIZE_B, SECTOR_SIZE),
134 LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE);
135 for(i=0;i < (LUKS_NUMKEYS - 1);i++)
136 current_sector = DIV_ROUND_UP_MODULO(current_sector + sectors_per_stripes_set,
137 LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE);
139 *r_header_size = current_sector + sectors_per_stripes_set;
141 current_sector = DIV_ROUND_UP_MODULO(current_sector + sectors_per_stripes_set,
142 LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE);
144 if (r_payload_offset) {
146 *r_payload_offset = alignpayload_sec;
148 *r_payload_offset = DIV_ROUND_UP_MODULO(current_sector, alignpayload_sec)
156 * Creates dm-linear target over the test loop device. Offset is held in
157 * global variables so that size can be tested whether it fits into remaining
158 * size of the loop device or not
160 static int create_dmdevice_over_loop(const char *dm_name, const uint64_t size)
166 if(device_size(THE_LOOP_DEV, &r_size) < 0 || r_size <= t_dev_offset || !size)
168 if ((r_size - t_dev_offset) < size) {
169 printf("No enough space on backing loop device\n.");
172 snprintf(cmd, sizeof(cmd),
173 "dmsetup create %s --table \"0 %" PRIu64 " linear %s %" PRIu64 "\"",
174 dm_name, size, THE_LOOP_DEV, t_dev_offset);
175 if (!(r = _system(cmd, 1))) {
176 t_dev_offset += size;
181 // TODO some utility to remove dmdevice over the loop file
183 // Get key from kernel dm mapping table using dm-ioctl
184 static int _get_key_dm(const char *name, char *buffer, unsigned int buffer_size)
188 uint64_t start, length;
189 char *target_type, *key, *params;
193 if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
195 if (!dm_task_set_name(dmt, name))
197 if (!dm_task_run(dmt))
199 if (!dm_task_get_info(dmt, &dmi))
204 next = dm_get_next_target(dmt, next, &start, &length, &target_type, ¶ms);
205 if (!target_type || strcmp(target_type, "crypt") != 0)
208 (void)strsep(¶ms, " "); /* rcipher */
209 key = strsep(¶ms, " ");
211 if (buffer_size <= strlen(key))
214 strncpy(buffer, key, buffer_size);
218 dm_task_destroy(dmt);
223 static int _prepare_keyfile(const char *name, const char *passphrase, int size)
227 fd = open(name, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR);
229 r = write(fd, passphrase, size);
234 return r == size ? 0 : 1;
237 static void _remove_keyfiles(void)
243 // Decode key from its hex representation
244 static int crypt_decode_key(char *key, const char *hex, unsigned int size)
252 for (i = 0; i < size; i++) {
256 key[i] = (unsigned char)strtoul(buffer, &endp, 16);
258 if (endp != &buffer[2])
268 static void cmdLineLog(int level, const char *msg)
270 strncat(global_log, msg, sizeof(global_log) - strlen(global_log));
274 static void new_log(int level, const char *msg, void *usrptr)
277 printf("LOG: %s", msg);
278 cmdLineLog(level, msg);
281 static void reset_log(void)
283 memset(global_log, 0, sizeof(global_log));
287 static int _system(const char *command, int warn)
291 printf("Running system: %s\n", command);
292 if ((r=system(command)) < 0 && warn)
293 printf("System command failed: %s", command);
297 static void _cleanup_dmdevices(void)
301 if (!stat(DMDIR H_DEVICE, &st)) {
302 _system("dmsetup remove " H_DEVICE, 0);
304 if (!stat(DMDIR H_DEVICE_WRONG, &st)) {
305 _system("dmsetup remove " H_DEVICE_WRONG, 0);
307 if (!stat(DMDIR L_DEVICE_0S, &st)) {
308 _system("dmsetup remove " L_DEVICE_0S, 0);
310 if (!stat(DMDIR L_DEVICE_1S, &st)) {
311 _system("dmsetup remove " L_DEVICE_1S, 0);
313 if (!stat(DMDIR L_DEVICE_WRONG, &st)) {
314 _system("dmsetup remove " L_DEVICE_WRONG, 0);
316 if (!stat(DMDIR L_DEVICE_OK, &st)) {
317 _system("dmsetup remove " L_DEVICE_OK, 0);
323 static void _cleanup(void)
327 //_system("udevadm settle", 0);
329 if (!stat(DMDIR CDEVICE_1, &st))
330 _system("dmsetup remove " CDEVICE_1, 0);
332 if (!stat(DMDIR CDEVICE_2, &st))
333 _system("dmsetup remove " CDEVICE_2, 0);
335 if (!stat(DEVICE_EMPTY, &st))
336 _system("dmsetup remove " DEVICE_EMPTY_name, 0);
338 if (!stat(DEVICE_ERROR, &st))
339 _system("dmsetup remove " DEVICE_ERROR_name, 0);
341 _cleanup_dmdevices();
343 if (crypt_loop_device(THE_LOOP_DEV))
344 crypt_loop_detach(THE_LOOP_DEV);
346 if (crypt_loop_device(DEVICE_1))
347 crypt_loop_detach(DEVICE_1);
349 if (crypt_loop_device(DEVICE_2))
350 crypt_loop_detach(DEVICE_2);
352 _system("rm -f " IMAGE_EMPTY, 0);
353 _system("rm -f " IMAGE1, 0);
355 remove(test_loop_file);
358 remove(EVL_HEADER_1);
359 remove(EVL_HEADER_2);
360 remove(EVL_HEADER_3);
361 remove(EVL_HEADER_4);
362 remove(VALID_HEADER);
368 free(test_loop_file);
374 static int _setup(void)
379 test_loop_file = strdup(THE_LFILE_TEMPLATE);
380 if ((fd=mkstemp(test_loop_file)) == -1) {
381 printf("cannot create temporary file with template %s\n", test_loop_file);
385 snprintf(cmd, sizeof(cmd), "dd if=/dev/zero of=%s bs=%d count=%d 2>/dev/null",
386 test_loop_file, SECTOR_SIZE, TST_LOOP_FILE_SIZE);
391 THE_LOOP_DEV = crypt_loop_get_device();
393 printf("Cannot find free loop device.\n");
396 if (crypt_loop_device(THE_LOOP_DEV)) {
397 fd = crypt_loop_attach(THE_LOOP_DEV, test_loop_file, 0, 0, &ro);
401 tmp_file_1 = strdup(THE_LFILE_TEMPLATE);
402 if ((fd=mkstemp(tmp_file_1)) == -1) {
403 printf("cannot create temporary file with template %s\n", tmp_file_1);
407 snprintf(cmd, sizeof(cmd), "dd if=/dev/zero of=%s bs=%d count=%d 2>/dev/null",
408 tmp_file_1, SECTOR_SIZE, 10);
412 _system("dmsetup create " DEVICE_EMPTY_name " --table \"0 10000 zero\"", 1);
413 _system("dmsetup create " DEVICE_ERROR_name " --table \"0 10000 error\"", 1);
415 DEVICE_1 = crypt_loop_get_device();
417 printf("Cannot find free loop device.\n");
420 if (crypt_loop_device(DEVICE_1)) {
421 _system(" [ ! -e " IMAGE1 " ] && bzip2 -dk " IMAGE1 ".bz2", 1);
422 fd = crypt_loop_attach(DEVICE_1, IMAGE1, 0, 0, &ro);
426 DEVICE_2 = crypt_loop_get_device();
428 printf("Cannot find free loop device.\n");
431 if (crypt_loop_device(DEVICE_2)) {
432 _system("dd if=/dev/zero of=" IMAGE_EMPTY " bs=1M count=4 2>/dev/null", 1);
433 fd = crypt_loop_attach(DEVICE_2, IMAGE_EMPTY, 0, 0, &ro);
436 /* Keymaterial offset is less than 8 sectors */
437 _system(" [ ! -e " EVL_HEADER_1 " ] && bzip2 -dk " EVL_HEADER_1 ".bz2", 1);
438 /* keymaterial offset aims into payload area */
439 _system(" [ ! -e " EVL_HEADER_2 " ] && bzip2 -dk " EVL_HEADER_2 ".bz2", 1);
440 /* keymaterial offset is valid, number of stripes causes payload area to be overwriten */
441 _system(" [ ! -e " EVL_HEADER_3 " ] && bzip2 -dk " EVL_HEADER_3 ".bz2", 1);
442 /* luks device header for data and header on same device. payloadOffset is greater than
443 * device size (crypt_load() test) */
444 _system(" [ ! -e " EVL_HEADER_4 " ] && bzip2 -dk " EVL_HEADER_4 ".bz2", 1);
445 /* valid header: payloadOffset=4096, key_size=32,
446 * volume_key = bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a */
447 _system(" [ ! -e " VALID_HEADER " ] && bzip2 -dk " VALID_HEADER ".bz2", 1);
449 /* Prepare tcrypt images */
450 _system(" [ ! -d tcrypt-images ] && tar xjf tcrypt-images.tar.bz2 2>/dev/null", 1);
452 _system("modprobe dm-crypt", 0);
453 _system("modprobe dm-verity", 0);
457 static void check_ok(int status, int line, const char *func)
462 crypt_get_error(buf, sizeof(buf));
463 printf("FAIL line %d [%s]: code %d, %s\n", line, func, status, buf);
469 static void check_ko(int status, int line, const char *func)
473 memset(buf, 0, sizeof(buf));
474 crypt_get_error(buf, sizeof(buf));
476 printf("FAIL line %d [%s]: code %d, %s\n", line, func, status, buf);
480 printf(" => errno %d, errmsg: %s\n", status, buf);
483 static void check_equal(int line, const char *func, int64_t x, int64_t y)
485 printf("FAIL line %d [%s]: expected equal values differs: %"
486 PRIi64 " != %" PRIi64 "\n", line, func, x, y);
491 static void xlog(const char *msg, const char *tst, const char *func, int line, const char *txt)
495 printf(" [%s,%s:%d] %s [%s]\n", msg, func, line, tst, txt);
497 printf(" [%s,%s:%d] %s\n", msg, func, line, tst);
501 /* crypt_device context must be "cd" to parse error properly here */
502 #define OK_(x) do { xlog("(success)", #x, __FUNCTION__, __LINE__, NULL); \
503 check_ok((x), __LINE__, __FUNCTION__); \
505 #define FAIL_(x, y) do { xlog("(fail) ", #x, __FUNCTION__, __LINE__, y); \
506 check_ko((x), __LINE__, __FUNCTION__); \
508 #define EQ_(x, y) do { xlog("(equal) ", #x " == " #y, __FUNCTION__, __LINE__, NULL); \
509 int64_t _x = (x), _y = (y); \
510 if (_x != _y) check_equal(__LINE__, __FUNCTION__, _x, _y); \
512 #define RUN_(x, y) do { printf("%s: %s\n", #x, (y)); x(); } while (0)
514 static void AddDevicePlain(void)
516 struct crypt_device *cd;
517 struct crypt_params_plain params = {
524 char key[128], key2[128], path[128];
526 const char *passphrase = PASSPHRASE;
527 // hashed hex version of PASSPHRASE
528 const char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
529 size_t key_size = strlen(mk_hex) / 2;
530 const char *cipher = "aes";
531 const char *cipher_mode = "cbc-essiv:sha256";
533 uint64_t size, r_size;
535 crypt_decode_key(key, mk_hex, key_size);
536 FAIL_(crypt_init(&cd, ""), "empty device string");
537 FAIL_(crypt_init(&cd, DEVICE_WRONG), "nonexistent device name ");
538 FAIL_(crypt_init(&cd, DEVICE_CHAR), "character device as backing device");
539 OK_(crypt_init(&cd, tmp_file_1));
542 // test crypt_format, crypt_get_cipher, crypt_get_cipher_mode, crypt_get_volume_key_size
543 OK_(crypt_init(&cd,DEVICE_1));
546 FAIL_(crypt_format(cd,CRYPT_PLAIN,NULL,cipher_mode,NULL,NULL,key_size,¶ms),"cipher param is null");
547 FAIL_(crypt_format(cd,CRYPT_PLAIN,cipher,NULL,NULL,NULL,key_size,¶ms),"cipher_mode param is null");
548 OK_(crypt_format(cd,CRYPT_PLAIN,cipher,cipher_mode,NULL,NULL,key_size,¶ms));
549 OK_(strcmp(cipher_mode,crypt_get_cipher_mode(cd)));
550 OK_(strcmp(cipher,crypt_get_cipher(cd)));
551 EQ_((int)key_size, crypt_get_volume_key_size(cd));
552 EQ_(params.skip, crypt_get_iv_offset(cd));
553 EQ_(params.offset, crypt_get_data_offset(cd));
558 FAIL_(crypt_set_uuid(cd,DEVICE_1_UUID),"can't set uuid to plain device");
562 // default is "plain" hash - no password hash
563 OK_(crypt_init(&cd, DEVICE_1));
564 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, NULL));
565 FAIL_(crypt_activate_by_volume_key(cd, NULL, key, key_size, 0), "cannot verify key with plain");
566 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
567 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
568 OK_(crypt_deactivate(cd, CDEVICE_1));
571 // test boundaries in offset parameter
572 device_size(DEVICE_1,&size);
574 // zero sectors length
575 params.offset = size >> SECTOR_SHIFT;
576 OK_(crypt_init(&cd, DEVICE_1));
577 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
578 EQ_(crypt_get_data_offset(cd),params.offset);
579 // device size is 0 sectors
580 FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0), "invalid device size (0 blocks)");
581 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
582 // data part of crypt device is of 1 sector size
583 params.offset = (size >> SECTOR_SHIFT) - 1;
586 OK_(crypt_init(&cd, DEVICE_1));
587 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
588 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
589 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
590 snprintf(path, sizeof(path), "%s/%s", crypt_get_dir(), CDEVICE_1);
591 if (device_size(path, &r_size) >= 0)
592 EQ_(r_size>>SECTOR_SHIFT, 1);
593 OK_(crypt_deactivate(cd, CDEVICE_1));
596 // size > device_size
598 params.size = (size >> SECTOR_SHIFT) + 1;
599 crypt_init(&cd, DEVICE_1);
600 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
601 FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0),"Device too small");
602 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
605 // offset == device_size (autodetect size)
606 params.offset = (size >> SECTOR_SHIFT);
608 crypt_init(&cd, DEVICE_1);
609 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
610 FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0),"Device too small");
611 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
614 // offset == device_size (user defined size)
615 params.offset = (size >> SECTOR_SHIFT);
617 crypt_init(&cd, DEVICE_1);
618 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
619 FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0),"Device too small");
620 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
623 // offset+size > device_size
625 params.size = (size >> SECTOR_SHIFT) - params.offset + 1;
626 crypt_init(&cd, DEVICE_1);
627 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
628 FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0),"Offset and size are beyond device real size");
629 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
632 // offset+size == device_size
634 params.size = (size >> SECTOR_SHIFT) - params.offset;
635 crypt_init(&cd, DEVICE_1);
636 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
637 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
638 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
639 if (!device_size(path, &r_size))
640 EQ_((r_size >> SECTOR_SHIFT),params.size);
641 OK_(crypt_deactivate(cd,CDEVICE_1));
644 params.hash = "sha1";
649 // Now use hashed password
650 OK_(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, NULL, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0),
653 "cannot verify passphrase with plain" );
654 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
656 // device status check
657 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
658 snprintf(path, sizeof(path), "%s/%s", crypt_get_dir(), CDEVICE_1);
659 fd = open(path, O_RDONLY);
660 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_BUSY);
661 FAIL_(crypt_deactivate(cd, CDEVICE_1), "Device is busy");
663 OK_(crypt_deactivate(cd, CDEVICE_1));
664 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
667 // crypt_init_by_name_and_header
668 OK_(crypt_init(&cd,DEVICE_1));
669 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
670 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
673 FAIL_(crypt_init_by_name_and_header(&cd, CDEVICE_1, H_DEVICE),"can't init plain device by header device");
674 OK_(crypt_init_by_name(&cd, CDEVICE_1));
675 OK_(strcmp(cipher_mode,crypt_get_cipher_mode(cd)));
676 OK_(strcmp(cipher,crypt_get_cipher(cd)));
677 EQ_((int)key_size, crypt_get_volume_key_size(cd));
678 EQ_(params.skip, crypt_get_iv_offset(cd));
679 EQ_(params.offset, crypt_get_data_offset(cd));
680 OK_(crypt_deactivate(cd, CDEVICE_1));
683 OK_(crypt_init(&cd,DEVICE_1));
684 OK_(crypt_format(cd,CRYPT_PLAIN,cipher,cipher_mode,NULL,NULL,key_size,¶ms));
688 // crypt_set_data_device
689 FAIL_(crypt_set_data_device(cd,H_DEVICE),"can't set data device for plain device");
692 OK_(strcmp(crypt_get_type(cd),CRYPT_PLAIN));
694 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
695 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
698 OK_(crypt_resize(cd,CDEVICE_1,size>>SECTOR_SHIFT)); // same size
699 if (!device_size(path,&r_size))
703 FAIL_(crypt_resize(cd, CDEVICE_1, (uint64_t)-1),"Backing device is too small");
704 FAIL_(crypt_resize(cd, CDEVICE_1, (size>>SECTOR_SHIFT)+1),"crypt device overlaps backing device");
707 OK_(crypt_resize(cd,CDEVICE_1, 123));
708 if (!device_size(path,&r_size))
709 EQ_(r_size>>SECTOR_SHIFT, 123);
710 OK_(crypt_resize(cd,CDEVICE_1,0)); // full size (autodetect)
711 if (!device_size(path,&r_size))
713 OK_(crypt_deactivate(cd,CDEVICE_1));
714 EQ_(crypt_status(cd,CDEVICE_1),CRYPT_INACTIVE);
718 OK_(crypt_init(&cd,DEVICE_1));
720 params.size = (size>>SECTOR_SHIFT) - params.offset - 10;
721 OK_(crypt_format(cd,CRYPT_PLAIN,cipher,cipher_mode,NULL,NULL,key_size,¶ms));
722 OK_(crypt_activate_by_volume_key(cd,CDEVICE_1,key,key_size,0));
723 if (!device_size(path,&r_size))
724 EQ_(r_size>>SECTOR_SHIFT, params.size);
725 // resize to fill remaining capacity
726 OK_(crypt_resize(cd,CDEVICE_1,params.size + 10));
727 if (!device_size(path,&r_size))
728 EQ_(r_size>>SECTOR_SHIFT, params.size + 10);
730 // 1 sector beyond real size
731 FAIL_(crypt_resize(cd,CDEVICE_1,params.size + 11), "new device size overlaps backing device"); // with respect to offset
732 if (!device_size(path,&r_size))
733 EQ_(r_size>>SECTOR_SHIFT, params.size + 10);
734 EQ_(crypt_status(cd,CDEVICE_1),CRYPT_ACTIVE);
735 fd = open(path, O_RDONLY);
739 // resize to minimal size
740 OK_(crypt_resize(cd,CDEVICE_1, 1)); // minimal device size
741 if (!device_size(path,&r_size))
742 EQ_(r_size>>SECTOR_SHIFT, 1);
743 // use size of backing device (autodetect with respect to offset)
744 OK_(crypt_resize(cd,CDEVICE_1,0));
745 if (!device_size(path,&r_size))
746 EQ_(r_size>>SECTOR_SHIFT, (size >> SECTOR_SHIFT)- 42);
747 OK_(crypt_deactivate(cd,CDEVICE_1));
752 OK_(crypt_init(&cd,DEVICE_1));
753 OK_(crypt_format(cd,CRYPT_PLAIN,cipher,cipher_mode,NULL,NULL,key_size,¶ms));
754 OK_(crypt_activate_by_volume_key(cd,CDEVICE_1,key,key_size,0));
756 // suspend/resume tests
757 FAIL_(crypt_suspend(cd,CDEVICE_1),"cannot suspend plain device");
758 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
759 FAIL_(crypt_resume_by_passphrase(cd,CDEVICE_1,CRYPT_ANY_SLOT,passphrase, strlen(passphrase)),"cannot resume plain device");
760 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
762 // retrieve volume key check
763 memset(key2, 0, key_size);
766 FAIL_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)), "small buffer");
768 OK_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)));
770 OK_(memcmp(key, key2, key_size));
771 OK_(strcmp(cipher, crypt_get_cipher(cd)));
772 OK_(strcmp(cipher_mode, crypt_get_cipher_mode(cd)));
773 EQ_((int)key_size, crypt_get_volume_key_size(cd));
774 EQ_(0, crypt_get_data_offset(cd));
775 OK_(crypt_deactivate(cd, CDEVICE_1));
778 OK_(_prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
779 OK_(_prepare_keyfile(KEYFILE2, KEY2, strlen(KEY2)));
780 FAIL_(crypt_activate_by_keyfile(cd, NULL, CRYPT_ANY_SLOT, KEYFILE1, 0, 0), "cannot verify key with plain");
781 EQ_(0, crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
782 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
783 OK_(crypt_deactivate(cd, CDEVICE_1));
784 FAIL_(crypt_activate_by_keyfile_offset(cd, NULL, CRYPT_ANY_SLOT, KEYFILE1, 0, strlen(KEY1) + 1, 0), "cannot seek");
785 EQ_(0, crypt_activate_by_keyfile_offset(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0, 0));
786 OK_(crypt_deactivate(cd, CDEVICE_1));
790 OK_(crypt_init(&cd,DEVICE_1));
791 OK_(crypt_format(cd,CRYPT_PLAIN,cipher,cipher_mode,NULL,NULL,key_size,¶ms));
794 FAIL_(crypt_keyslot_add_by_passphrase(cd,CRYPT_ANY_SLOT,passphrase,strlen(passphrase),passphrase,strlen(passphrase)), "can't add keyslot to plain device");
795 FAIL_(crypt_keyslot_add_by_volume_key(cd,CRYPT_ANY_SLOT ,key,key_size,passphrase,strlen(passphrase)),"can't add keyslot to plain device");
796 FAIL_(crypt_keyslot_add_by_keyfile(cd,CRYPT_ANY_SLOT,KEYFILE1,strlen(KEY1),KEYFILE2,strlen(KEY2)),"can't add keyslot to plain device");
797 FAIL_(crypt_keyslot_destroy(cd,1),"can't manipulate keyslots on plain device");
798 EQ_(crypt_keyslot_status(cd, 0), CRYPT_SLOT_INVALID);
804 #define CALLBACK_ERROR "calback_error xyz"
805 static int pass_callback_err(const char *msg, char *buf, size_t length, void *usrptr)
807 struct crypt_device *cd = usrptr;
813 crypt_log(cd, CRYPT_LOG_ERROR, CALLBACK_ERROR);
817 static int pass_callback_ok(const char *msg, char *buf, size_t length, void *usrptr)
821 strcpy(buf, PASSPHRASE);
825 static void CallbacksTest(void)
827 struct crypt_device *cd;
828 struct crypt_params_plain params = {
834 size_t key_size = 256 / 8;
835 const char *cipher = "aes";
836 const char *cipher_mode = "cbc-essiv:sha256";
837 const char *passphrase = PASSPHRASE;
838 char buf1[256] = {0}, buf2[256] = {0};
840 OK_(crypt_init(&cd, DEVICE_1));
841 crypt_set_log_callback(cd, &new_log, NULL);
842 //crypt_set_log_callback(cd, NULL, NULL);
844 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
846 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
847 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
848 OK_(crypt_deactivate(cd, CDEVICE_1));
851 crypt_set_password_callback(cd, pass_callback_err, cd);
852 FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, NULL, 0, 0), "callback fails");
853 EQ_(strncmp(global_log, CALLBACK_ERROR, strlen(CALLBACK_ERROR)), 0);
855 crypt_set_password_callback(cd, pass_callback_ok, NULL);
856 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, NULL, 0, 0));
857 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
858 OK_(crypt_deactivate(cd, CDEVICE_1));
860 // Check error reporting.
861 // This must fail and create error message
862 crypt_deactivate(cd, CDEVICE_1);
864 // Here context must be the same
865 crypt_get_error(buf1, sizeof(buf1));
866 crypt_last_error(cd, buf2, sizeof(buf2));
869 OK_(strcmp(buf1, buf2));
871 crypt_get_error(buf1, sizeof(buf1));
872 crypt_last_error(cd, buf2, sizeof(buf2));
879 static void UseLuksDevice(void)
881 struct crypt_device *cd;
885 OK_(crypt_init(&cd, DEVICE_1));
886 OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
887 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
888 OK_(crypt_activate_by_passphrase(cd, NULL, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0));
889 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0));
890 FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0), "already open");
891 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
892 OK_(crypt_deactivate(cd, CDEVICE_1));
893 FAIL_(crypt_deactivate(cd, CDEVICE_1), "no such device");
896 OK_(strcmp("aes", crypt_get_cipher(cd)));
897 OK_(strcmp("cbc-essiv:sha256", crypt_get_cipher_mode(cd)));
898 OK_(strcmp(DEVICE_1_UUID, crypt_get_uuid(cd)));
899 EQ_((int)key_size, crypt_get_volume_key_size(cd));
900 EQ_(1032, crypt_get_data_offset(cd));
902 EQ_(0, crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key, &key_size, KEY1, strlen(KEY1)));
903 OK_(crypt_volume_key_verify(cd, key, key_size));
904 OK_(crypt_activate_by_volume_key(cd, NULL, key, key_size, 0));
905 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
906 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
907 OK_(crypt_deactivate(cd, CDEVICE_1));
910 FAIL_(crypt_volume_key_verify(cd, key, key_size), "key mismatch");
911 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0), "key mismatch");
915 static void SuspendDevice(void)
918 struct crypt_device *cd;
920 OK_(crypt_init(&cd, DEVICE_1));
921 OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
922 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0));
924 suspend_status = crypt_suspend(cd, CDEVICE_1);
925 if (suspend_status == -ENOTSUP) {
926 printf("WARNING: Suspend/Resume not supported, skipping test.\n");
930 FAIL_(crypt_suspend(cd, CDEVICE_1), "already suspended");
932 FAIL_(crypt_resume_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1)-1), "wrong key");
933 OK_(crypt_resume_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1)));
934 FAIL_(crypt_resume_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1)), "not suspended");
936 OK_(_prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
937 OK_(crypt_suspend(cd, CDEVICE_1));
938 FAIL_(crypt_resume_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1 "blah", 0), "wrong keyfile");
939 FAIL_(crypt_resume_by_keyfile_offset(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 1, 0), "wrong key");
940 OK_(crypt_resume_by_keyfile_offset(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
941 FAIL_(crypt_resume_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0), "not suspended");
944 OK_(crypt_deactivate(cd, CDEVICE_1));
948 static void AddDeviceLuks(void)
950 struct crypt_device *cd;
951 struct crypt_params_luks1 params = {
953 .data_alignment = 2048, // 4M, data offset will be 4096
954 .data_device = DEVICE_2
956 char key[128], key2[128];
958 const char *passphrase = "blabla", *passphrase2 = "nsdkFI&Y#.sd";
959 const char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
960 size_t key_size = strlen(mk_hex) / 2;
961 const char *cipher = "aes";
962 const char *cipher_mode = "cbc-essiv:sha256";
963 uint64_t r_payload_offset, r_header_size, r_size_1;
965 crypt_decode_key(key, mk_hex, key_size);
968 OK_(get_luks_offsets(1, key_size, 0, 0, &r_header_size, &r_payload_offset));
969 OK_(create_dmdevice_over_loop(H_DEVICE, r_header_size));
970 OK_(create_dmdevice_over_loop(H_DEVICE_WRONG, r_header_size - 1));
973 OK_(crypt_init(&cd, DMDIR H_DEVICE_WRONG));
974 params.data_alignment = 0;
975 FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms), "Not enough space for keyslots material");
978 // test payload_offset = 0 for encrypted device with external header device
979 OK_(crypt_init(&cd, DMDIR H_DEVICE));
980 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
981 EQ_(crypt_get_data_offset(cd), 0);
984 params.data_alignment = 0;
985 params.data_device = NULL;
987 // test payload_offset = 0. format() should look up alignment offset from device topology
988 OK_(crypt_init(&cd, DEVICE_2));
989 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
990 OK_(!(crypt_get_data_offset(cd) > 0));
994 * test limit values for backing device size
996 params.data_alignment = 4096;
997 OK_(get_luks_offsets(0, key_size, params.data_alignment, 0, NULL, &r_payload_offset));
998 OK_(create_dmdevice_over_loop(L_DEVICE_0S, r_payload_offset));
999 OK_(create_dmdevice_over_loop(L_DEVICE_1S, r_payload_offset + 1));
1000 //OK_(create_dmdevice_over_loop(L_DEVICE_WRONG, r_payload_offset - 1));
1001 OK_(create_dmdevice_over_loop(L_DEVICE_WRONG, 2050 - 1)); //FIXME last keyslot - 1 sector
1003 // 1 sector less than required
1004 OK_(crypt_init(&cd, DMDIR L_DEVICE_WRONG));
1005 FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms), "Device too small");
1008 // 0 sectors for encrypted area
1009 OK_(crypt_init(&cd, DMDIR L_DEVICE_0S));
1010 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1011 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0), "Encrypted area too small");
1014 // 1 sector for encrypted area
1015 OK_(crypt_init(&cd, DMDIR L_DEVICE_1S));
1016 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1017 EQ_(crypt_get_data_offset(cd), params.data_alignment);
1018 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1019 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1020 OK_(device_size(DMDIR CDEVICE_1, &r_size_1));
1021 EQ_(r_size_1, SECTOR_SIZE);
1022 OK_(crypt_deactivate(cd, CDEVICE_1));
1023 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
1024 // restrict format only to empty context
1025 FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms), "Context is already formated");
1026 FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, NULL), "Context is already formated");
1027 // change data device to wrong one
1028 OK_(crypt_set_data_device(cd, DMDIR L_DEVICE_0S));
1029 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0), "Device too small");
1030 OK_(crypt_set_data_device(cd, DMDIR L_DEVICE_1S));
1031 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1032 OK_(crypt_deactivate(cd, CDEVICE_1));
1035 params.data_alignment = 0;
1036 params.data_device = DEVICE_2;
1038 // generate keyslot material at the end of luks header
1039 OK_(crypt_init(&cd, DMDIR H_DEVICE));
1040 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1041 EQ_(crypt_keyslot_add_by_volume_key(cd, 7, key, key_size, passphrase, strlen(passphrase)), 7);
1042 EQ_(crypt_activate_by_passphrase(cd, CDEVICE_1, 7, passphrase, strlen(passphrase) ,0), 7);
1044 OK_(crypt_init_by_name_and_header(&cd, CDEVICE_1, DMDIR H_DEVICE));
1045 FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms), "Context is already formated");
1046 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1047 OK_(crypt_deactivate(cd, CDEVICE_1));
1050 params.data_alignment = 2048;
1051 params.data_device = NULL;
1053 // test uuid mismatch and _init_by_name_and_header
1054 OK_(crypt_init(&cd, DMDIR L_DEVICE_1S));
1055 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1056 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1058 params.data_alignment = 0;
1059 params.data_device = DEVICE_2;
1060 OK_(crypt_init(&cd, DMDIR H_DEVICE));
1061 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1063 // there we've got uuid mismatch
1064 OK_(crypt_init_by_name_and_header(&cd, CDEVICE_1, DMDIR H_DEVICE));
1065 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1066 OK_(!!crypt_get_type(cd));
1067 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0), "Device is active");
1068 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, key, key_size, 0), "Device is active");
1069 EQ_(crypt_status(cd, CDEVICE_2), CRYPT_INACTIVE);
1070 OK_(crypt_deactivate(cd, CDEVICE_1));
1073 params.data_device = NULL;
1075 OK_(crypt_init(&cd, DEVICE_2));
1076 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1078 // even with no keyslots defined it can be activated by volume key
1079 OK_(crypt_volume_key_verify(cd, key, key_size));
1080 OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, key, key_size, 0));
1081 EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
1082 OK_(crypt_deactivate(cd, CDEVICE_2));
1085 EQ_(7, crypt_keyslot_add_by_volume_key(cd, 7, key, key_size, passphrase, strlen(passphrase)));
1086 EQ_(CRYPT_SLOT_ACTIVE_LAST, crypt_keyslot_status(cd, 7));
1087 EQ_(7, crypt_activate_by_passphrase(cd, CDEVICE_2, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
1088 EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
1089 OK_(crypt_deactivate(cd, CDEVICE_2));
1091 crypt_set_iteration_time(cd, 1);
1092 EQ_(1, crypt_keyslot_add_by_volume_key(cd, 1, key, key_size, KEY1, strlen(KEY1)));
1093 OK_(_prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
1094 OK_(_prepare_keyfile(KEYFILE2, KEY2, strlen(KEY2)));
1095 EQ_(2, crypt_keyslot_add_by_keyfile(cd, 2, KEYFILE1, 0, KEYFILE2, 0));
1096 FAIL_(crypt_keyslot_add_by_keyfile_offset(cd, 3, KEYFILE1, 0, 1, KEYFILE2, 0, 1), "wrong key");
1097 EQ_(3, crypt_keyslot_add_by_keyfile_offset(cd, 3, KEYFILE1, 0, 0, KEYFILE2, 0, 1));
1098 EQ_(4, crypt_keyslot_add_by_keyfile_offset(cd, 4, KEYFILE2, 0, 1, KEYFILE1, 0, 1));
1099 FAIL_(crypt_activate_by_keyfile(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, strlen(KEY2)-1, 0), "key mismatch");
1100 EQ_(2, crypt_activate_by_keyfile(cd, NULL, CRYPT_ANY_SLOT, KEYFILE2, 0, 0));
1101 EQ_(3, crypt_activate_by_keyfile_offset(cd, NULL, CRYPT_ANY_SLOT, KEYFILE2, 0, 1, 0));
1102 EQ_(4, crypt_activate_by_keyfile_offset(cd, NULL, CRYPT_ANY_SLOT, KEYFILE1, 0, 1, 0));
1103 FAIL_(crypt_activate_by_keyfile_offset(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, strlen(KEY2), 2, 0), "not enough data");
1104 FAIL_(crypt_activate_by_keyfile_offset(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, 0, strlen(KEY2) + 1, 0), "cannot seek");
1105 FAIL_(crypt_activate_by_keyfile_offset(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, 0, 2, 0), "wrong key");
1106 EQ_(2, crypt_activate_by_keyfile(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, 0, 0));
1107 OK_(crypt_keyslot_destroy(cd, 1));
1108 OK_(crypt_keyslot_destroy(cd, 2));
1109 OK_(crypt_keyslot_destroy(cd, 3));
1110 OK_(crypt_keyslot_destroy(cd, 4));
1111 OK_(crypt_deactivate(cd, CDEVICE_2));
1114 FAIL_(crypt_keyslot_add_by_volume_key(cd, 7, key, key_size, passphrase, strlen(passphrase)), "slot used");
1116 FAIL_(crypt_keyslot_add_by_volume_key(cd, 6, key, key_size, passphrase, strlen(passphrase)), "key mismatch");
1118 EQ_(6, crypt_keyslot_add_by_volume_key(cd, 6, key, key_size, passphrase, strlen(passphrase)));
1119 EQ_(CRYPT_SLOT_ACTIVE, crypt_keyslot_status(cd, 6));
1121 FAIL_(crypt_keyslot_destroy(cd, 8), "invalid keyslot");
1122 FAIL_(crypt_keyslot_destroy(cd, CRYPT_ANY_SLOT), "invalid keyslot");
1123 FAIL_(crypt_keyslot_destroy(cd, 0), "keyslot not used");
1124 OK_(crypt_keyslot_destroy(cd, 7));
1125 EQ_(CRYPT_SLOT_INACTIVE, crypt_keyslot_status(cd, 7));
1126 EQ_(CRYPT_SLOT_ACTIVE_LAST, crypt_keyslot_status(cd, 6));
1128 EQ_(7, crypt_keyslot_change_by_passphrase(cd, 6, 7, passphrase, strlen(passphrase), passphrase2, strlen(passphrase2)));
1129 EQ_(CRYPT_SLOT_ACTIVE_LAST, crypt_keyslot_status(cd, 7));
1130 EQ_(7, crypt_activate_by_passphrase(cd, NULL, 7, passphrase2, strlen(passphrase2), 0));
1131 EQ_(6, crypt_keyslot_change_by_passphrase(cd, CRYPT_ANY_SLOT, 6, passphrase2, strlen(passphrase2), passphrase, strlen(passphrase)));
1133 EQ_(6, crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)));
1134 OK_(crypt_volume_key_verify(cd, key2, key_size));
1136 OK_(memcmp(key, key2, key_size));
1137 OK_(strcmp(cipher, crypt_get_cipher(cd)));
1138 OK_(strcmp(cipher_mode, crypt_get_cipher_mode(cd)));
1139 EQ_((int)key_size, crypt_get_volume_key_size(cd));
1140 EQ_(4096, crypt_get_data_offset(cd));
1141 OK_(strcmp(DEVICE_2, crypt_get_device_name(cd)));
1144 crypt_set_log_callback(cd, &new_log, NULL);
1145 OK_(crypt_dump(cd));
1146 OK_(!(global_lines != 0));
1147 crypt_set_log_callback(cd, NULL, NULL);
1150 FAIL_(crypt_set_uuid(cd, "blah"), "wrong UUID format");
1151 OK_(crypt_set_uuid(cd, DEVICE_TEST_UUID));
1152 OK_(strcmp(DEVICE_TEST_UUID, crypt_get_uuid(cd)));
1154 FAIL_(crypt_deactivate(cd, CDEVICE_2), "not active");
1156 _cleanup_dmdevices();
1159 static void UseTempVolumes(void)
1161 struct crypt_device *cd;
1164 // Tepmporary device without keyslot but with on-disk LUKS header
1165 OK_(crypt_init(&cd, DEVICE_2));
1166 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "not yet formatted");
1167 OK_(crypt_format(cd, CRYPT_LUKS1, "aes", "cbc-essiv:sha256", NULL, NULL, 16, NULL));
1168 OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0));
1169 EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
1172 OK_(crypt_init_by_name(&cd, CDEVICE_2));
1173 OK_(crypt_deactivate(cd, CDEVICE_2));
1176 // Dirty checks: device without UUID
1177 // we should be able to remove it but not manuipulate with it
1178 snprintf(tmp, sizeof(tmp), "dmsetup create %s --table \""
1179 "0 100 crypt aes-cbc-essiv:sha256 deadbabedeadbabedeadbabedeadbabe 0 "
1180 "%s 2048\"", CDEVICE_2, DEVICE_2);
1182 OK_(crypt_init_by_name(&cd, CDEVICE_2));
1183 OK_(crypt_deactivate(cd, CDEVICE_2));
1184 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "No known device type");
1187 // Dirty checks: device with UUID but LUKS header key fingerprint must fail)
1188 snprintf(tmp, sizeof(tmp), "dmsetup create %s --table \""
1189 "0 100 crypt aes-cbc-essiv:sha256 deadbabedeadbabedeadbabedeadbabe 0 "
1190 "%s 2048\" -u CRYPT-LUKS1-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-ctest1",
1191 CDEVICE_2, DEVICE_2);
1193 OK_(crypt_init_by_name(&cd, CDEVICE_2));
1194 OK_(crypt_deactivate(cd, CDEVICE_2));
1195 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "wrong volume key");
1199 OK_(crypt_init(&cd, DEVICE_2));
1200 OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
1201 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "volume key is lost");
1205 OK_(crypt_init(&cd, DEVICE_2));
1206 OK_(crypt_format(cd, CRYPT_PLAIN, "aes", "cbc-essiv:sha256", NULL, NULL, 16, NULL));
1207 FAIL_(crypt_activate_by_volume_key(cd, NULL, "xxx", 3, 0), "cannot verify key with plain");
1208 FAIL_(crypt_volume_key_verify(cd, "xxx", 3), "cannot verify key with plain");
1209 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, "xxx", 3, 0), "wrong key lenght");
1210 OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, "volumekeyvolumek", 16, 0));
1211 EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
1212 OK_(crypt_deactivate(cd, CDEVICE_2));
1216 static void LuksHeaderRestore(void)
1218 struct crypt_device *cd;
1219 struct crypt_params_luks1 params = {
1221 .data_alignment = 2048, // 4M, data offset will be 4096
1223 struct crypt_params_plain pl_params = {
1229 char key[128], key2[128], cmd[256];
1231 const char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
1232 size_t key_size = strlen(mk_hex) / 2;
1233 const char *cipher = "aes";
1234 const char *cipher_mode = "cbc-essiv:sha256";
1235 uint64_t r_payload_offset;
1237 crypt_decode_key(key, mk_hex, key_size);
1239 OK_(get_luks_offsets(0, key_size, params.data_alignment, 0, NULL, &r_payload_offset));
1240 OK_(create_dmdevice_over_loop(L_DEVICE_OK, r_payload_offset + 5000));
1242 // do not restore header over plain device
1243 OK_(crypt_init(&cd, DMDIR L_DEVICE_OK));
1244 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, &pl_params));
1245 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1246 FAIL_(crypt_header_restore(cd, CRYPT_PLAIN, VALID_HEADER), "Cannot restore header to PLAIN type device");
1247 FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, VALID_HEADER), "Cannot restore header over PLAIN type device");
1248 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1249 OK_(crypt_deactivate(cd, CDEVICE_1));
1253 OK_(crypt_init(&cd, DMDIR L_DEVICE_OK));
1254 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1255 FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, EVL_HEADER_1), "Header corrupted");
1256 FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, EVL_HEADER_2), "Header corrupted");
1257 FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, EVL_HEADER_3), "Header corrupted");
1258 FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, EVL_HEADER_4), "Header too small");
1259 OK_(crypt_header_restore(cd, CRYPT_LUKS1, VALID_HEADER));
1260 // wipe valid luks header
1261 snprintf(cmd, sizeof(cmd), "dd if=/dev/zero of=" DMDIR L_DEVICE_OK " bs=512 count=%" PRIu64 " 2>/dev/null", r_payload_offset);
1262 OK_(_system(cmd, 1));
1263 FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, EVL_HEADER_1), "Header corrupted");
1264 FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, EVL_HEADER_2), "Header corrupted");
1265 FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, EVL_HEADER_3), "Header corrupted");
1266 FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, EVL_HEADER_4), "Header too small");
1267 OK_(crypt_header_restore(cd, CRYPT_LUKS1, VALID_HEADER));
1268 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1269 OK_(crypt_deactivate(cd, CDEVICE_1));
1272 // volume key_size mismatch
1273 OK_(crypt_init(&cd, DMDIR L_DEVICE_OK));
1274 memcpy(key2, key, key_size - 1);
1275 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key2, key_size - 1, ¶ms));
1276 FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, VALID_HEADER), "Volume keysize mismatch");
1279 // payload offset mismatch
1280 params.data_alignment = 8192;
1281 OK_(crypt_init(&cd, DMDIR L_DEVICE_OK));
1282 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1283 FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, VALID_HEADER), "Payload offset mismatch");
1284 //_system("dmsetup table;sleep 1",1);
1287 _cleanup_dmdevices();
1290 static void LuksHeaderLoad(void)
1292 struct crypt_device *cd;
1293 struct crypt_params_luks1 params = {
1295 .data_alignment = 2048,
1297 struct crypt_params_plain pl_params = {
1303 char key[128], cmd[256];
1305 const char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
1306 size_t key_size = strlen(mk_hex) / 2;
1307 const char *cipher = "aes";
1308 const char *cipher_mode = "cbc-essiv:sha256";
1309 uint64_t r_payload_offset, r_header_size;
1311 crypt_decode_key(key, mk_hex, key_size);
1314 OK_(get_luks_offsets(0, key_size, params.data_alignment, 0, &r_header_size, &r_payload_offset));
1315 // external header device
1316 OK_(create_dmdevice_over_loop(H_DEVICE, r_header_size));
1317 // prepared header on a device too small to contain header and payload
1318 //OK_(create_dmdevice_over_loop(H_DEVICE_WRONG, r_payload_offset - 1));
1319 OK_(create_dmdevice_over_loop(H_DEVICE_WRONG, 2050 - 1)); //FIXME
1320 //snprintf(cmd, sizeof(cmd), "dd if=" EVL_HEADER_4 " of=" DMDIR H_DEVICE_WRONG " bs=512 count=%" PRIu64, r_payload_offset - 1);
1321 snprintf(cmd, sizeof(cmd), "dd if=" EVL_HEADER_4 " of=" DMDIR H_DEVICE_WRONG " bs=512 count=%d 2>/dev/null", 2050 - 1);
1322 OK_(_system(cmd, 1));
1324 OK_(create_dmdevice_over_loop(L_DEVICE_OK, r_payload_offset + 1000));
1326 OK_(create_dmdevice_over_loop(L_DEVICE_1S, r_payload_offset + 1));
1327 // 0 sectors device for payload
1328 OK_(create_dmdevice_over_loop(L_DEVICE_0S, r_payload_offset));
1330 // valid metadata and device size
1331 params.data_alignment = 0;
1332 params.data_device = DMDIR L_DEVICE_OK;
1333 OK_(crypt_init(&cd, DMDIR H_DEVICE));
1334 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1336 OK_(crypt_init(&cd, DMDIR H_DEVICE));
1337 OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
1338 OK_(crypt_set_data_device(cd, DMDIR L_DEVICE_OK));
1339 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1340 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1341 OK_(crypt_deactivate(cd, CDEVICE_1));
1344 // bad header: device too small (payloadOffset > device_size)
1345 OK_(crypt_init(&cd, DMDIR H_DEVICE_WRONG));
1346 FAIL_(crypt_load(cd, CRYPT_LUKS1, NULL), "Device too small");
1347 OK_(!!crypt_get_type(cd));
1350 // 0 secs for encrypted data area
1351 params.data_alignment = 2048;
1352 params.data_device = NULL;
1353 OK_(crypt_init(&cd, DMDIR L_DEVICE_0S));
1354 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1356 // load should be ok
1357 OK_(crypt_init(&cd, DMDIR L_DEVICE_0S));
1358 OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
1359 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0), "Device too small");
1360 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
1364 OK_(_system("dd if=/dev/zero of=" DMDIR L_DEVICE_OK " bs=512 count=8 2>/dev/null", 1));
1365 OK_(crypt_init(&cd, DMDIR L_DEVICE_OK));
1366 FAIL_(crypt_load(cd, CRYPT_LUKS1, NULL), "Header not found");
1370 OK_(crypt_init(&cd, DMDIR H_DEVICE));
1371 FAIL_(crypt_load(cd, CRYPT_PLAIN, NULL), "Can't load nonLUKS device type");
1373 OK_(crypt_init(&cd, DMDIR H_DEVICE));
1374 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, key, key_size, &pl_params));
1375 FAIL_(crypt_load(cd, CRYPT_LUKS1, NULL), "Can't load over nonLUKS device type");
1378 _cleanup_dmdevices();
1381 static void LuksHeaderBackup(void)
1383 struct crypt_device *cd;
1384 struct crypt_params_luks1 params = {
1386 .data_alignment = 2048,
1390 const char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
1391 size_t key_size = strlen(mk_hex) / 2;
1392 const char *cipher = "aes";
1393 const char *cipher_mode = "cbc-essiv:sha256";
1394 uint64_t r_payload_offset;
1396 crypt_decode_key(key, mk_hex, key_size);
1398 OK_(get_luks_offsets(0, key_size, params.data_alignment, 0, NULL, &r_payload_offset));
1399 OK_(create_dmdevice_over_loop(L_DEVICE_OK, r_payload_offset + 1));
1401 // create LUKS device and backup the header
1402 OK_(crypt_init(&cd, DMDIR L_DEVICE_OK));
1403 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1404 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1405 OK_(crypt_header_backup(cd, CRYPT_LUKS1, BACKUP_FILE));
1406 OK_(crypt_deactivate(cd, CDEVICE_1));
1409 // restore header from backup
1410 OK_(crypt_init(&cd, DMDIR L_DEVICE_OK));
1411 OK_(crypt_header_restore(cd, CRYPT_LUKS1, BACKUP_FILE));
1412 OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
1413 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1414 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1415 OK_(crypt_deactivate(cd, CDEVICE_1));
1418 _cleanup_dmdevices();
1421 static void ResizeDeviceLuks(void)
1423 struct crypt_device *cd;
1424 struct crypt_params_luks1 params = {
1426 .data_alignment = 2048,
1430 const char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
1431 size_t key_size = strlen(mk_hex) / 2;
1432 const char *cipher = "aes";
1433 const char *cipher_mode = "cbc-essiv:sha256";
1434 uint64_t r_payload_offset, r_header_size, r_size;
1436 crypt_decode_key(key, mk_hex, key_size);
1439 OK_(get_luks_offsets(0, key_size, params.data_alignment, 0, NULL, &r_payload_offset));
1440 OK_(get_luks_offsets(1, key_size, 0, 0, &r_header_size, NULL));
1441 OK_(create_dmdevice_over_loop(H_DEVICE, r_header_size));
1442 OK_(create_dmdevice_over_loop(L_DEVICE_OK, r_payload_offset + 1000));
1443 OK_(create_dmdevice_over_loop(L_DEVICE_0S, 1000));
1445 // test header and encrypted payload all in one device
1446 OK_(crypt_init(&cd, DMDIR L_DEVICE_OK));
1447 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1448 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1449 OK_(crypt_resize(cd, CDEVICE_1, 42));
1450 if (!device_size(DMDIR CDEVICE_1, &r_size))
1451 EQ_(42, r_size >> SECTOR_SHIFT);
1452 // autodetect encrypted device area size
1453 OK_(crypt_resize(cd, CDEVICE_1, 0));
1454 if (!device_size(DMDIR CDEVICE_1, &r_size))
1455 EQ_(1000, r_size >> SECTOR_SHIFT);
1456 FAIL_(crypt_resize(cd, CDEVICE_1, 1001), "Device too small");
1457 if (!device_size(DMDIR CDEVICE_1, &r_size))
1458 EQ_(1000, r_size >> SECTOR_SHIFT);
1459 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1460 OK_(crypt_deactivate(cd, CDEVICE_1));
1463 params.data_alignment = 0;
1464 params.data_device = DMDIR L_DEVICE_0S;
1465 // test case for external header
1466 OK_(crypt_init(&cd, DMDIR H_DEVICE));
1467 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1468 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1469 OK_(crypt_resize(cd, CDEVICE_1, 666));
1470 if (!device_size(DMDIR CDEVICE_1, &r_size))
1471 EQ_(666, r_size >> SECTOR_SHIFT);
1472 // autodetect encrypted device size
1473 OK_(crypt_resize(cd, CDEVICE_1, 0));
1474 if (!device_size(DMDIR CDEVICE_1, &r_size))
1475 EQ_(1000, r_size >> SECTOR_SHIFT);
1476 FAIL_(crypt_resize(cd, CDEVICE_1, 1001), "Device too small");
1477 if (!device_size(DMDIR CDEVICE_1, &r_size))
1478 EQ_(1000, r_size >> SECTOR_SHIFT);
1479 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1480 OK_(crypt_deactivate(cd, CDEVICE_1));
1483 _cleanup_dmdevices();
1486 static void HashDevicePlain(void)
1488 struct crypt_device *cd;
1489 struct crypt_params_plain params = {
1496 const char *mk_hex, *keystr;
1499 OK_(crypt_init(&cd, DEVICE_1));
1500 OK_(crypt_format(cd, CRYPT_PLAIN, "aes", "cbc-essiv:sha256", NULL, NULL, 16, ¶ms));
1502 // hash PLAIN, short key
1503 OK_(_prepare_keyfile(KEYFILE1, "tooshort", 8));
1504 FAIL_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 16, 0), "not enough data in keyfile");
1507 // hash PLAIN, exact key
1508 // 0 1 2 3 4 5 6 7 8 9 a b c d e f
1509 mk_hex = "caffeecaffeecaffeecaffeecaffee88";
1511 crypt_decode_key(key, mk_hex, key_size);
1512 OK_(_prepare_keyfile(KEYFILE1, key, key_size));
1513 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size, 0));
1514 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1515 OK_(strcmp(key, mk_hex));
1516 OK_(crypt_deactivate(cd, CDEVICE_1));
1519 mk_hex = "caffeecaffeecaffeecaffeeca000000";
1520 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size - 3, 0));
1521 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1522 OK_(strcmp(key, mk_hex));
1523 OK_(crypt_deactivate(cd, CDEVICE_1));
1527 // hash PLAIN, long key
1528 // 0 1 2 3 4 5 6 7 8 9 a b c d e f
1529 mk_hex = "caffeecaffeecaffeecaffeecaffee88babebabe";
1531 crypt_decode_key(key, mk_hex, key_size);
1532 OK_(_prepare_keyfile(KEYFILE1, key, strlen(mk_hex) / 2));
1533 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size, 0));
1534 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1535 FAIL_(strcmp(key, mk_hex), "only key length used");
1536 OK_(strncmp(key, mk_hex, key_size));
1537 OK_(crypt_deactivate(cd, CDEVICE_1));
1540 // Now without explicit limit
1541 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
1542 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1543 FAIL_(strcmp(key, mk_hex), "only key length used");
1544 OK_(strncmp(key, mk_hex, key_size));
1545 OK_(crypt_deactivate(cd, CDEVICE_1));
1551 params.hash = "sha256";
1552 OK_(crypt_init(&cd, DEVICE_1));
1553 OK_(crypt_format(cd, CRYPT_PLAIN, "aes", "cbc-essiv:sha256", NULL, NULL, 16, ¶ms));
1555 // 0 1 2 3 4 5 6 7 8 9 a b c d e f
1556 mk_hex = "c62e4615bd39e222572f3a1bf7c2132e";
1557 keystr = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
1558 key_size = strlen(keystr); // 32
1559 OK_(_prepare_keyfile(KEYFILE1, keystr, strlen(keystr)));
1560 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size, 0));
1561 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1562 OK_(strcmp(key, mk_hex));
1563 OK_(crypt_deactivate(cd, CDEVICE_1));
1565 // Read full keyfile
1566 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
1567 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1568 OK_(strcmp(key, mk_hex));
1569 OK_(crypt_deactivate(cd, CDEVICE_1));
1573 // Limit keyfile read
1574 keystr = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxAAAAAAAA";
1575 OK_(_prepare_keyfile(KEYFILE1, keystr, strlen(keystr)));
1576 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size, 0));
1577 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1578 OK_(strcmp(key, mk_hex));
1579 OK_(crypt_deactivate(cd, CDEVICE_1));
1582 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
1583 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1584 OK_(strcmp(key, "0e49cb34a1dee1df33f6505e4de44a66"));
1585 OK_(crypt_deactivate(cd, CDEVICE_1));
1589 // FIXME: add keyfile="-" tests somehow
1594 static void VerityTest(void)
1596 struct crypt_device *cd;
1597 const char *salt_hex = "20c28ffc129c12360ba6ceea2b6cf04e89c2b41cfe6b8439eb53c1897f50df7b";
1598 const char *root_hex = "ab018b003a967fc782effb293b6dccb60b4f40c06bf80d16391acf686d28b5d6";
1599 char salt[256], root_hash[256];
1600 struct crypt_active_device cad;
1601 struct crypt_params_verity params = {
1602 .data_device = DEVICE_EMPTY,
1604 .data_size = 0, /* whole device */
1605 .hash_area_offset = 0,
1606 .flags = CRYPT_VERITY_CREATE_HASH,
1609 crypt_decode_key(salt, salt_hex, strlen(salt_hex) / 2);
1610 crypt_decode_key(root_hash, root_hex, strlen(root_hex) / 2);
1613 OK_(crypt_init(&cd, DEVICE_2));
1616 params.data_block_size = 333;
1617 FAIL_(crypt_format(cd, CRYPT_VERITY, NULL, NULL, NULL, NULL, 0, ¶ms),
1618 "Unsupppored block size.");
1619 params.data_block_size = 4096;
1620 params.hash_block_size = 333;
1621 FAIL_(crypt_format(cd, CRYPT_VERITY, NULL, NULL, NULL, NULL, 0, ¶ms),
1622 "Unsupppored block size.");
1623 params.hash_block_size = 4096;
1626 params.salt_size = 257;
1627 FAIL_(crypt_format(cd, CRYPT_VERITY, NULL, NULL, NULL, NULL, 0, ¶ms),
1629 params.salt_size = 32;
1632 params.hash_type = 3;
1633 FAIL_(crypt_format(cd, CRYPT_VERITY, NULL, NULL, NULL, NULL, 0, ¶ms),
1634 "Unsupported hash type.");
1635 params.hash_type = 1;
1636 params.hash_name = "blah";
1637 FAIL_(crypt_format(cd, CRYPT_VERITY, NULL, NULL, NULL, NULL, 0, ¶ms),
1638 "Unsupported hash name.");
1639 params.hash_name = "sha256";
1641 OK_(crypt_format(cd, CRYPT_VERITY, NULL, NULL, NULL, NULL, 0, ¶ms));
1645 OK_(crypt_init(&cd, DEVICE_2));
1646 memset(¶ms, 0, sizeof(params));
1647 params.data_device = DEVICE_EMPTY;
1648 params.flags = CRYPT_VERITY_CHECK_HASH;
1649 OK_(crypt_load(cd, CRYPT_VERITY, ¶ms));
1651 /* check verity params */
1652 EQ_(crypt_get_volume_key_size(cd), 32);
1653 OK_(strcmp(CRYPT_VERITY, crypt_get_type(cd)));
1654 memset(¶ms, 0, sizeof(params));
1655 OK_(crypt_get_verity_info(cd, ¶ms));
1656 OK_(strcmp("sha256", params.hash_name));
1657 EQ_(strlen(salt_hex) / 2, params.salt_size);
1658 OK_(memcmp(salt, params.salt, params.salt_size));
1659 EQ_(4096, params.data_block_size);
1660 EQ_(4096, params.hash_block_size);
1661 EQ_(1, params.hash_type);
1662 EQ_(crypt_get_volume_key_size(cd), 32);
1664 OK_(crypt_activate_by_volume_key(cd, NULL, root_hash, 32, 0));
1665 OK_(crypt_set_data_device(cd, DEVICE_1));
1666 FAIL_(crypt_activate_by_volume_key(cd, NULL, root_hash, 32, 0), "Data corrupted");;
1668 OK_(crypt_set_data_device(cd, DEVICE_EMPTY));
1669 if (crypt_activate_by_volume_key(cd, CDEVICE_1, root_hash, 32,
1670 CRYPT_ACTIVATE_READONLY) == -ENOTSUP) {
1671 printf("WARNING: kernel dm-verity not supported, skipping test.\n");
1675 OK_(crypt_get_active_device(cd, CDEVICE_1, &cad));
1676 EQ_(CRYPT_ACTIVATE_READONLY, cad.flags);
1679 OK_(crypt_init_by_name(&cd, CDEVICE_1));
1680 OK_(crypt_deactivate(cd, CDEVICE_1));
1683 root_hash[1] = ~root_hash[1];
1684 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, root_hash, 32, CRYPT_ACTIVATE_READONLY));
1685 OK_(crypt_get_active_device(cd, CDEVICE_1, &cad));
1686 EQ_(CRYPT_ACTIVATE_READONLY|CRYPT_ACTIVATE_CORRUPTED, cad.flags);
1687 OK_(crypt_deactivate(cd, CDEVICE_1));
1688 root_hash[1] = ~root_hash[1];
1691 OK_(crypt_set_data_device(cd, DEVICE_1));
1692 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, root_hash, 32, CRYPT_ACTIVATE_READONLY));
1693 OK_(crypt_get_active_device(cd, CDEVICE_1, &cad));
1694 EQ_(CRYPT_ACTIVATE_READONLY|CRYPT_ACTIVATE_CORRUPTED, cad.flags);
1695 OK_(crypt_deactivate(cd, CDEVICE_1));
1700 static void TcryptTest(void)
1702 struct crypt_device *cd = NULL;
1703 struct crypt_active_device cad;
1704 const char *passphrase = "aaaaaaaaaaaa";
1705 const char *kf1 = "tcrypt-images/keyfile1";
1706 const char *kf2 = "tcrypt-images/keyfile2";
1707 const char *keyfiles[] = { kf1, kf2 };
1708 struct crypt_params_tcrypt params = {
1709 .passphrase = passphrase,
1710 .passphrase_size = strlen(passphrase),
1711 .keyfiles = keyfiles,
1712 .keyfiles_count = 2,
1714 double enc_mbr = 0, dec_mbr = 0;
1715 const char *tcrypt_dev = "tcrypt-images/tck_5-sha512-xts-aes";
1716 size_t key_size = 64;
1717 char key[key_size], key_def[key_size];
1718 const char *key_hex =
1719 "98dee64abe44bbf41d171c1f7b3e8eacda6d6b01f459097459a167f8c2872a96"
1720 "3979531d1cdc18af62757cf22286f16f8583d848524f128d7594ac2082668c73";
1723 crypt_decode_key(key_def, key_hex, strlen(key_hex) / 2);
1725 // First ensure we can use af_alg skcipher interface
1726 r = crypt_benchmark(NULL, "aes", "xts", 512, 16, 1024, &enc_mbr, &dec_mbr);
1727 if (r == -ENOTSUP || r == -ENOENT) {
1728 printf("WARNING: algif_skcipher interface not present, skipping test.\n");
1732 OK_(crypt_init(&cd, tcrypt_dev));
1733 params.passphrase_size--;
1734 FAIL_(crypt_load(cd, CRYPT_TCRYPT, ¶ms), "Wrong passphrase");
1735 params.passphrase_size++;
1736 OK_(crypt_load(cd, CRYPT_TCRYPT, ¶ms));
1738 // check params after load
1739 OK_(strcmp("xts-plain64", crypt_get_cipher_mode(cd)));
1740 OK_(strcmp("aes", crypt_get_cipher(cd)));
1741 EQ_(key_size, crypt_get_volume_key_size(cd));
1742 EQ_(256, crypt_get_iv_offset(cd));
1743 EQ_(256, crypt_get_data_offset(cd));
1745 memset(key, 0, key_size);
1748 FAIL_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key, &key_size, NULL, 0), "small buffer");
1750 OK_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key, &key_size, NULL, 0));
1751 OK_(memcmp(key, key_def, key_size));
1754 crypt_set_log_callback(cd, &new_log, NULL);
1755 OK_(crypt_dump(cd));
1756 OK_(!(global_lines != 0));
1757 crypt_set_log_callback(cd, NULL, NULL);
1760 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, NULL, 0, CRYPT_ACTIVATE_READONLY));
1763 OK_(crypt_init_by_name_and_header(&cd, CDEVICE_1, NULL));
1764 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1766 FAIL_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key, &key_size, NULL, 0), "Need crypt_load");
1768 // check params after init_by_name
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 OK_(crypt_get_active_device(cd, CDEVICE_1, &cad));
1776 EQ_(CRYPT_ACTIVATE_READONLY, cad.flags);
1777 EQ_(256, cad.offset);
1778 EQ_(256, cad.iv_offset);
1781 OK_(crypt_deactivate(cd, CDEVICE_1));
1785 // Check that gcrypt is properly initialised in format
1786 static void NonFIPSAlg(void)
1788 struct crypt_device *cd;
1789 struct crypt_params_luks1 params = {0};
1791 size_t key_size = 128;
1792 const char *cipher = "aes";
1793 const char *cipher_mode = "cbc-essiv:sha256";
1796 OK_(crypt_init(&cd, DEVICE_2));
1797 params.hash = "sha256";
1798 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1799 FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms),
1800 "Already formatted.");
1803 params.hash = "whirlpool";
1804 OK_(crypt_init(&cd, DEVICE_2));
1805 ret = crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms);
1807 printf("WARNING: whirlpool not supported, skipping test.\n");
1813 params.hash = "md5";
1814 OK_(crypt_init(&cd, DEVICE_2));
1815 FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms),
1816 "MD5 unsupported, too short");
1820 int main(int argc, char *argv[])
1824 if (getuid() != 0) {
1825 printf("You must be root to run this test.\n");
1829 for (i = 1; i < argc; i++) {
1830 if (!strcmp("-v", argv[i]) || !strcmp("--verbose", argv[i]))
1832 else if (!strcmp("--debug", argv[i]))
1833 _debug = _verbose = 1;
1840 crypt_set_debug_level(_debug ? CRYPT_DEBUG_ALL : CRYPT_DEBUG_NONE);
1842 RUN_(NonFIPSAlg, "Crypto is properly initialised in format"); //must be the first!
1843 RUN_(AddDevicePlain, "plain device API creation exercise");
1844 RUN_(HashDevicePlain, "plain device API hash test");
1845 RUN_(AddDeviceLuks, "Format and use LUKS device");
1846 RUN_(LuksHeaderLoad, "test header load");
1847 RUN_(LuksHeaderRestore, "test LUKS header restore");
1848 RUN_(LuksHeaderBackup, "test LUKS header backup");
1849 RUN_(ResizeDeviceLuks, "Luks device resize tests");
1850 RUN_(UseLuksDevice, "Use pre-formated LUKS device");
1851 RUN_(SuspendDevice, "Suspend/Resume test");
1852 RUN_(UseTempVolumes, "Format and use temporary encrypted device");
1853 RUN_(CallbacksTest, "API callbacks test");
1854 RUN_(VerityTest, "DM verity test");
1855 RUN_(TcryptTest, "Tcrypt API test");