2 * cryptsetup library API check functions
4 * Copyright (C) 2009-2010 Red Hat, Inc. All rights reserved.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include <sys/ioctl.h>
30 #include <libdevmapper.h>
33 #include "libcryptsetup.h"
34 #include "utils_loop.h"
36 #define DMDIR "/dev/mapper/"
38 #define DEVICE_1_UUID "28632274-8c8a-493f-835b-da802e1c576b"
39 #define DEVICE_EMPTY_name "crypt_zero"
40 #define DEVICE_EMPTY DMDIR DEVICE_EMPTY_name
41 #define DEVICE_ERROR_name "crypt_error"
42 #define DEVICE_ERROR DMDIR DEVICE_ERROR_name
44 #define CDEVICE_1 "ctest1"
45 #define CDEVICE_2 "ctest2"
46 #define CDEVICE_WRONG "O_o"
47 #define H_DEVICE "head_ok"
48 #define H_DEVICE_WRONG "head_wr"
49 #define L_DEVICE_1S "luks_onesec"
50 #define L_DEVICE_0S "luks_zerosec"
51 #define L_DEVICE_WRONG "luks_wr"
52 #define L_DEVICE_OK "luks_ok"
53 #define EVL_HEADER_1 "evil_hdr-luks_hdr_damage"
54 #define EVL_HEADER_2 "evil_hdr-payload_overwrite"
55 #define EVL_HEADER_3 "evil_hdr-stripes_payload_dmg"
56 #define EVL_HEADER_4 "evil_hdr-small_luks_device"
57 #define VALID_HEADER "valid_header_file"
58 #define BACKUP_FILE "csetup_backup_file"
59 #define IMAGE1 "compatimage.img"
60 #define IMAGE_EMPTY "empty.img"
62 #define KEYFILE1 "key1.file"
63 #define KEY1 "compatkey"
65 #define KEYFILE2 "key2.file"
66 #define KEY2 "0123456789abcdef"
68 #define PASSPHRASE "blabla"
70 #define DEVICE_TEST_UUID "12345678-1234-1234-1234-123456789abc"
72 #define DEVICE_WRONG "/dev/Ooo_"
73 #define DEVICE_CHAR "/dev/zero"
74 #define THE_LFILE_TEMPLATE "cryptsetup-tstlp.XXXXXX"
76 #define SECTOR_SHIFT 9L
77 #define SECTOR_SIZE 512
78 #define TST_LOOP_FILE_SIZE (((1<<20)*50)>>SECTOR_SHIFT)
79 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
80 #define DIV_ROUND_UP_MODULO(n,d) (DIV_ROUND_UP(n,d)*(d))
81 #define LUKS_PHDR_SIZE_B 1024
83 static int _debug = 0;
84 static int _verbose = 1;
86 static char global_log[4096];
87 static int global_lines = 0;
89 static char *DEVICE_1 = NULL;
90 static char *DEVICE_2 = NULL;
91 static char *THE_LOOP_DEV = NULL;
93 static char *tmp_file_1 = NULL;
94 static char *test_loop_file = NULL;
95 static uint64_t t_dev_offset = 0;
97 static int _system(const char*, int);
101 static int device_size(const char *device, uint64_t *size)
105 devfd = open(device, O_RDONLY);
109 if (ioctl(devfd, BLKGETSIZE64, size) < 0)
115 static int get_luks_offsets(int metadata_device,
117 unsigned int alignpayload_sec,
118 unsigned int alignoffset_sec,
119 uint64_t *r_header_size,
120 uint64_t *r_payload_offset)
123 uint64_t current_sector;
124 uint32_t sectors_per_stripes_set;
129 sectors_per_stripes_set = DIV_ROUND_UP(keylength*LUKS_STRIPES, SECTOR_SIZE);
130 printf("sectors_per_stripes %" PRIu32 "\n", sectors_per_stripes_set);
131 current_sector = DIV_ROUND_UP_MODULO(DIV_ROUND_UP(LUKS_PHDR_SIZE_B, SECTOR_SIZE),
132 LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE);
133 for(i=0;i < (LUKS_NUMKEYS - 1);i++)
134 current_sector = DIV_ROUND_UP_MODULO(current_sector + sectors_per_stripes_set,
135 LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE);
137 *r_header_size = current_sector + sectors_per_stripes_set;
139 current_sector = DIV_ROUND_UP_MODULO(current_sector + sectors_per_stripes_set,
140 LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE);
142 if (r_payload_offset) {
144 *r_payload_offset = alignpayload_sec;
146 *r_payload_offset = DIV_ROUND_UP_MODULO(current_sector, alignpayload_sec)
154 * Creates dm-linear target over the test loop device. Offset is held in
155 * global variables so that size can be tested whether it fits into remaining
156 * size of the loop device or not
158 static int create_dmdevice_over_loop(const char *dm_name, const uint64_t size)
164 if(device_size(THE_LOOP_DEV, &r_size) < 0 || r_size <= t_dev_offset || !size)
166 if ((r_size - t_dev_offset) < size) {
167 printf("No enough space on backing loop device\n.");
170 snprintf(cmd, sizeof(cmd),
171 "dmsetup create %s --table \"0 %" PRIu64 " linear %s %" PRIu64 "\"",
172 dm_name, size, THE_LOOP_DEV, t_dev_offset);
173 if (!(r = _system(cmd, 1))) {
174 t_dev_offset += size;
179 // TODO some utility to remove dmdevice over the loop file
181 // Get key from kernel dm mapping table using dm-ioctl
182 static int _get_key_dm(const char *name, char *buffer, unsigned int buffer_size)
186 uint64_t start, length;
187 char *target_type, *key, *params;
191 if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
193 if (!dm_task_set_name(dmt, name))
195 if (!dm_task_run(dmt))
197 if (!dm_task_get_info(dmt, &dmi))
202 next = dm_get_next_target(dmt, next, &start, &length, &target_type, ¶ms);
203 if (!target_type || strcmp(target_type, "crypt") != 0)
206 (void)strsep(¶ms, " "); /* rcipher */
207 key = strsep(¶ms, " ");
209 if (buffer_size <= strlen(key))
212 strncpy(buffer, key, buffer_size);
216 dm_task_destroy(dmt);
221 static int _prepare_keyfile(const char *name, const char *passphrase, int size)
225 fd = open(name, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR);
227 r = write(fd, passphrase, size);
232 return r == size ? 0 : 1;
235 static void _remove_keyfiles(void)
241 // Decode key from its hex representation
242 static int crypt_decode_key(char *key, const char *hex, unsigned int size)
250 for (i = 0; i < size; i++) {
254 key[i] = (unsigned char)strtoul(buffer, &endp, 16);
256 if (endp != &buffer[2])
266 static void cmdLineLog(int level, const char *msg)
268 strncat(global_log, msg, sizeof(global_log) - strlen(global_log));
272 static void new_log(int level, const char *msg, void *usrptr)
274 cmdLineLog(level, msg);
277 static void reset_log(void)
279 memset(global_log, 0, sizeof(global_log));
283 static int _system(const char *command, int warn)
286 if ((r=system(command)) < 0 && warn)
287 printf("System command failed: %s", command);
291 static void _cleanup_dmdevices(void)
295 if (!stat(DMDIR H_DEVICE, &st)) {
296 _system("dmsetup remove " H_DEVICE, 0);
298 if (!stat(DMDIR H_DEVICE_WRONG, &st)) {
299 _system("dmsetup remove " H_DEVICE_WRONG, 0);
301 if (!stat(DMDIR L_DEVICE_0S, &st)) {
302 _system("dmsetup remove " L_DEVICE_0S, 0);
304 if (!stat(DMDIR L_DEVICE_1S, &st)) {
305 _system("dmsetup remove " L_DEVICE_1S, 0);
307 if (!stat(DMDIR L_DEVICE_WRONG, &st)) {
308 _system("dmsetup remove " L_DEVICE_WRONG, 0);
310 if (!stat(DMDIR L_DEVICE_OK, &st)) {
311 _system("dmsetup remove " L_DEVICE_OK, 0);
317 static void _cleanup(void)
321 //_system("udevadm settle", 0);
323 if (!stat(DMDIR CDEVICE_1, &st))
324 _system("dmsetup remove " CDEVICE_1, 0);
326 if (!stat(DMDIR CDEVICE_2, &st))
327 _system("dmsetup remove " CDEVICE_2, 0);
329 if (!stat(DEVICE_EMPTY, &st))
330 _system("dmsetup remove " DEVICE_EMPTY_name, 0);
332 if (!stat(DEVICE_ERROR, &st))
333 _system("dmsetup remove " DEVICE_ERROR_name, 0);
335 _cleanup_dmdevices();
337 if (crypt_loop_device(THE_LOOP_DEV))
338 crypt_loop_detach(THE_LOOP_DEV);
340 if (crypt_loop_device(DEVICE_1))
341 crypt_loop_detach(DEVICE_1);
343 if (crypt_loop_device(DEVICE_2))
344 crypt_loop_detach(DEVICE_2);
346 _system("rm -f " IMAGE_EMPTY, 0);
347 _system("rm -f " IMAGE1, 0);
349 remove(test_loop_file);
352 remove(EVL_HEADER_1);
353 remove(EVL_HEADER_2);
354 remove(EVL_HEADER_3);
355 remove(EVL_HEADER_4);
356 remove(VALID_HEADER);
362 free(test_loop_file);
368 static int _setup(void)
373 test_loop_file = strdup(THE_LFILE_TEMPLATE);
374 if ((fd=mkstemp(test_loop_file)) == -1) {
375 printf("cannot create temporary file with template %s\n", test_loop_file);
379 snprintf(cmd, sizeof(cmd), "dd if=/dev/zero of=%s bs=%d count=%d",
380 test_loop_file, SECTOR_SIZE, TST_LOOP_FILE_SIZE);
385 THE_LOOP_DEV = crypt_loop_get_device();
387 printf("Cannot find free loop device.\n");
390 if (crypt_loop_device(THE_LOOP_DEV)) {
391 fd = crypt_loop_attach(THE_LOOP_DEV, test_loop_file, 0, 0, &ro);
395 tmp_file_1 = strdup(THE_LFILE_TEMPLATE);
396 if ((fd=mkstemp(tmp_file_1)) == -1) {
397 printf("cannot create temporary file with template %s\n", tmp_file_1);
401 snprintf(cmd, sizeof(cmd), "dd if=/dev/zero of=%s bs=%d count=%d",
402 tmp_file_1, SECTOR_SIZE, 10);
406 _system("dmsetup create " DEVICE_EMPTY_name " --table \"0 10000 zero\"", 1);
407 _system("dmsetup create " DEVICE_ERROR_name " --table \"0 10000 error\"", 1);
409 DEVICE_1 = crypt_loop_get_device();
411 printf("Cannot find free loop device.\n");
414 if (crypt_loop_device(DEVICE_1)) {
415 _system(" [ ! -e " IMAGE1 " ] && bzip2 -dk " IMAGE1 ".bz2", 1);
416 fd = crypt_loop_attach(DEVICE_1, IMAGE1, 0, 0, &ro);
420 DEVICE_2 = crypt_loop_get_device();
422 printf("Cannot find free loop device.\n");
425 if (crypt_loop_device(DEVICE_2)) {
426 _system("dd if=/dev/zero of=" IMAGE_EMPTY " bs=1M count=4", 1);
427 fd = crypt_loop_attach(DEVICE_2, IMAGE_EMPTY, 0, 0, &ro);
430 /* Keymaterial offset is less than 8 sectors */
431 _system(" [ ! -e " EVL_HEADER_1 " ] && bzip2 -dk " EVL_HEADER_1 ".bz2", 1);
432 /* keymaterial offset aims into payload area */
433 _system(" [ ! -e " EVL_HEADER_2 " ] && bzip2 -dk " EVL_HEADER_2 ".bz2", 1);
434 /* keymaterial offset is valid, number of stripes causes payload area to be overwriten */
435 _system(" [ ! -e " EVL_HEADER_3 " ] && bzip2 -dk " EVL_HEADER_3 ".bz2", 1);
436 /* luks device header for data and header on same device. payloadOffset is greater than
437 * device size (crypt_load() test) */
438 _system(" [ ! -e " EVL_HEADER_4 " ] && bzip2 -dk " EVL_HEADER_4 ".bz2", 1);
439 /* valid header: payloadOffset=4096, key_size=32,
440 * volume_key = bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a */
441 _system(" [ ! -e " VALID_HEADER " ] && bzip2 -dk " VALID_HEADER ".bz2", 1);
446 static void check_ok(int status, int line, const char *func)
451 crypt_get_error(buf, sizeof(buf));
452 printf("FAIL line %d [%s]: code %d, %s\n", line, func, status, buf);
458 static void check_ko(int status, int line, const char *func)
462 memset(buf, 0, sizeof(buf));
463 crypt_get_error(buf, sizeof(buf));
465 printf("FAIL line %d [%s]: code %d, %s\n", line, func, status, buf);
469 printf(" => errno %d, errmsg: %s\n", status, buf);
472 static void check_equal(int line, const char *func, int64_t x, int64_t y)
474 printf("FAIL line %d [%s]: expected equal values differs: %"
475 PRIi64 " != %" PRIi64 "\n", line, func, x, y);
480 static void xlog(const char *msg, const char *tst, const char *func, int line, const char *txt)
484 printf(" [%s,%s:%d] %s [%s]\n", msg, func, line, tst, txt);
486 printf(" [%s,%s:%d] %s\n", msg, func, line, tst);
490 /* crypt_device context must be "cd" to parse error properly here */
491 #define OK_(x) do { xlog("(success)", #x, __FUNCTION__, __LINE__, NULL); \
492 check_ok((x), __LINE__, __FUNCTION__); \
494 #define FAIL_(x, y) do { xlog("(fail) ", #x, __FUNCTION__, __LINE__, y); \
495 check_ko((x), __LINE__, __FUNCTION__); \
497 #define EQ_(x, y) do { xlog("(equal) ", #x " == " #y, __FUNCTION__, __LINE__, NULL); \
498 int64_t _x = (x), _y = (y); \
499 if (_x != _y) check_equal(__LINE__, __FUNCTION__, _x, _y); \
501 #define RUN_(x, y) do { printf("%s: %s\n", #x, (y)); x(); } while (0)
503 static void AddDevicePlain(void)
505 struct crypt_device *cd;
506 struct crypt_params_plain params = {
513 char key[128], key2[128], path[128];
515 const char *passphrase = PASSPHRASE;
516 // hashed hex version of PASSPHRASE
517 const char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
518 size_t key_size = strlen(mk_hex) / 2;
519 const char *cipher = "aes";
520 const char *cipher_mode = "cbc-essiv:sha256";
522 uint64_t size, r_size;
524 crypt_decode_key(key, mk_hex, key_size);
525 FAIL_(crypt_init(&cd, ""), "empty device string");
526 FAIL_(crypt_init(&cd, DEVICE_WRONG), "nonexistent device name ");
527 FAIL_(crypt_init(&cd, DEVICE_CHAR), "character device as backing device");
528 OK_(crypt_init(&cd, tmp_file_1));
531 // test crypt_format, crypt_get_cipher, crypt_get_cipher_mode, crypt_get_volume_key_size
532 OK_(crypt_init(&cd,DEVICE_1));
535 FAIL_(crypt_format(cd,CRYPT_PLAIN,NULL,cipher_mode,NULL,NULL,key_size,¶ms),"cipher param is null");
536 FAIL_(crypt_format(cd,CRYPT_PLAIN,cipher,NULL,NULL,NULL,key_size,¶ms),"cipher_mode param is null");
537 OK_(crypt_format(cd,CRYPT_PLAIN,cipher,cipher_mode,NULL,NULL,key_size,¶ms));
538 OK_(strcmp(cipher_mode,crypt_get_cipher_mode(cd)));
539 OK_(strcmp(cipher,crypt_get_cipher(cd)));
540 EQ_((int)key_size, crypt_get_volume_key_size(cd));
541 EQ_(params.skip, crypt_get_iv_offset(cd));
542 EQ_(params.offset, crypt_get_data_offset(cd));
547 FAIL_(crypt_set_uuid(cd,DEVICE_1_UUID),"can't set uuid to plain device");
551 // default is "plain" hash - no password hash
552 OK_(crypt_init(&cd, DEVICE_1));
553 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, NULL));
554 FAIL_(crypt_activate_by_volume_key(cd, NULL, key, key_size, 0), "cannot verify key with plain");
555 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
556 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
557 OK_(crypt_deactivate(cd, CDEVICE_1));
560 // test boundaries in offset parameter
561 device_size(DEVICE_1,&size);
563 // zero sectors length
564 params.offset = size >> SECTOR_SHIFT;
565 OK_(crypt_init(&cd, DEVICE_1));
566 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
567 EQ_(crypt_get_data_offset(cd),params.offset);
568 // device size is 0 sectors
569 FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0), "invalid device size (0 blocks)");
570 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
571 // data part of crypt device is of 1 sector size
572 params.offset = (size >> SECTOR_SHIFT) - 1;
575 OK_(crypt_init(&cd, DEVICE_1));
576 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
577 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
578 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
579 snprintf(path, sizeof(path), "%s/%s", crypt_get_dir(), CDEVICE_1);
580 if (device_size(path, &r_size) >= 0)
581 EQ_(r_size>>SECTOR_SHIFT, 1);
582 OK_(crypt_deactivate(cd, CDEVICE_1));
585 // size > device_size
587 params.size = (size >> SECTOR_SHIFT) + 1;
588 crypt_init(&cd, DEVICE_1);
589 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
590 FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0),"Device too small");
591 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
594 // offset == device_size (autodetect size)
595 params.offset = (size >> SECTOR_SHIFT);
597 crypt_init(&cd, DEVICE_1);
598 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
599 FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0),"Device too small");
600 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
603 // offset == device_size (user defined size)
604 params.offset = (size >> SECTOR_SHIFT);
606 crypt_init(&cd, DEVICE_1);
607 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
608 FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0),"Device too small");
609 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
612 // offset+size > device_size
614 params.size = (size >> SECTOR_SHIFT) - params.offset + 1;
615 crypt_init(&cd, DEVICE_1);
616 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
617 FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0),"Offset and size are beyond device real size");
618 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
621 // offset+size == device_size
623 params.size = (size >> SECTOR_SHIFT) - params.offset;
624 crypt_init(&cd, DEVICE_1);
625 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
626 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
627 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
628 if (!device_size(path, &r_size))
629 EQ_((r_size >> SECTOR_SHIFT),params.size);
630 OK_(crypt_deactivate(cd,CDEVICE_1));
633 params.hash = "sha1";
638 // Now use hashed password
639 OK_(crypt_init(&cd, DEVICE_1));
640 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
641 FAIL_(crypt_activate_by_passphrase(cd, NULL, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0),
642 "cannot verify passphrase with plain" );
643 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
645 // device status check
646 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
647 snprintf(path, sizeof(path), "%s/%s", crypt_get_dir(), CDEVICE_1);
648 fd = open(path, O_RDONLY);
649 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_BUSY);
650 FAIL_(crypt_deactivate(cd, CDEVICE_1), "Device is busy");
652 OK_(crypt_deactivate(cd, CDEVICE_1));
653 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
656 // crypt_init_by_name_and_header
657 OK_(crypt_init(&cd,DEVICE_1));
658 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
659 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
662 FAIL_(crypt_init_by_name_and_header(&cd, CDEVICE_1, H_DEVICE),"can't init plain device by header device");
663 OK_(crypt_init_by_name(&cd, CDEVICE_1));
664 OK_(strcmp(cipher_mode,crypt_get_cipher_mode(cd)));
665 OK_(strcmp(cipher,crypt_get_cipher(cd)));
666 EQ_((int)key_size, crypt_get_volume_key_size(cd));
667 EQ_(params.skip, crypt_get_iv_offset(cd));
668 EQ_(params.offset, crypt_get_data_offset(cd));
669 OK_(crypt_deactivate(cd, CDEVICE_1));
672 OK_(crypt_init(&cd,DEVICE_1));
673 OK_(crypt_format(cd,CRYPT_PLAIN,cipher,cipher_mode,NULL,NULL,key_size,¶ms));
677 // crypt_set_data_device
678 FAIL_(crypt_set_data_device(cd,H_DEVICE),"can't set data device for plain device");
681 OK_(strcmp(crypt_get_type(cd),CRYPT_PLAIN));
683 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
684 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
687 OK_(crypt_resize(cd,CDEVICE_1,size>>SECTOR_SHIFT)); // same size
688 if (!device_size(path,&r_size))
692 FAIL_(crypt_resize(cd, CDEVICE_1, ULLONG_MAX),"Backing device is too small");
693 FAIL_(crypt_resize(cd, CDEVICE_1, (size>>SECTOR_SHIFT)+1),"crypt device overlaps backing device");
696 OK_(crypt_resize(cd,CDEVICE_1, 123));
697 if (!device_size(path,&r_size))
698 EQ_(r_size>>SECTOR_SHIFT, 123);
699 OK_(crypt_resize(cd,CDEVICE_1,0)); // full size (autodetect)
700 if (!device_size(path,&r_size))
702 OK_(crypt_deactivate(cd,CDEVICE_1));
703 EQ_(crypt_status(cd,CDEVICE_1),CRYPT_INACTIVE);
707 OK_(crypt_init(&cd,DEVICE_1));
709 params.size = (size>>SECTOR_SHIFT) - params.offset - 10;
710 OK_(crypt_format(cd,CRYPT_PLAIN,cipher,cipher_mode,NULL,NULL,key_size,¶ms));
711 OK_(crypt_activate_by_volume_key(cd,CDEVICE_1,key,key_size,0));
712 if (!device_size(path,&r_size))
713 EQ_(r_size>>SECTOR_SHIFT, params.size);
714 // resize to fill remaining capacity
715 OK_(crypt_resize(cd,CDEVICE_1,params.size + 10));
716 if (!device_size(path,&r_size))
717 EQ_(r_size>>SECTOR_SHIFT, params.size + 10);
719 // 1 sector beyond real size
720 FAIL_(crypt_resize(cd,CDEVICE_1,params.size + 11), "new device size overlaps backing device"); // with respect to offset
721 if (!device_size(path,&r_size))
722 EQ_(r_size>>SECTOR_SHIFT, params.size + 10);
723 EQ_(crypt_status(cd,CDEVICE_1),CRYPT_ACTIVE);
724 fd = open(path, O_RDONLY);
728 // resize to minimal size
729 OK_(crypt_resize(cd,CDEVICE_1, 1)); // minimal device size
730 if (!device_size(path,&r_size))
731 EQ_(r_size>>SECTOR_SHIFT, 1);
732 // use size of backing device (autodetect with respect to offset)
733 OK_(crypt_resize(cd,CDEVICE_1,0));
734 if (!device_size(path,&r_size))
735 EQ_(r_size>>SECTOR_SHIFT, (size >> SECTOR_SHIFT)- 42);
736 OK_(crypt_deactivate(cd,CDEVICE_1));
741 OK_(crypt_init(&cd,DEVICE_1));
742 OK_(crypt_format(cd,CRYPT_PLAIN,cipher,cipher_mode,NULL,NULL,key_size,¶ms));
743 OK_(crypt_activate_by_volume_key(cd,CDEVICE_1,key,key_size,0));
745 // suspend/resume tests
746 FAIL_(crypt_suspend(cd,CDEVICE_1),"cannot suspend plain device");
747 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
748 FAIL_(crypt_resume_by_passphrase(cd,CDEVICE_1,CRYPT_ANY_SLOT,passphrase, strlen(passphrase)),"cannot resume plain device");
749 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
751 // retrieve volume key check
752 memset(key2, 0, key_size);
755 FAIL_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)), "small buffer");
757 OK_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)));
759 OK_(memcmp(key, key2, key_size));
760 OK_(strcmp(cipher, crypt_get_cipher(cd)));
761 OK_(strcmp(cipher_mode, crypt_get_cipher_mode(cd)));
762 EQ_((int)key_size, crypt_get_volume_key_size(cd));
763 EQ_(0, crypt_get_data_offset(cd));
764 OK_(crypt_deactivate(cd, CDEVICE_1));
767 OK_(_prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
768 OK_(_prepare_keyfile(KEYFILE2, KEY2, strlen(KEY2)));
769 FAIL_(crypt_activate_by_keyfile(cd, NULL, CRYPT_ANY_SLOT, KEYFILE1, 0, 0), "cannot verify key with plain");
770 EQ_(0, crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
771 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
772 OK_(crypt_deactivate(cd, CDEVICE_1));
773 FAIL_(crypt_activate_by_keyfile_offset(cd, NULL, CRYPT_ANY_SLOT, KEYFILE1, 0, strlen(KEY1) + 1, 0), "cannot seek");
774 EQ_(0, crypt_activate_by_keyfile_offset(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0, 0));
775 OK_(crypt_deactivate(cd, CDEVICE_1));
779 OK_(crypt_init(&cd,DEVICE_1));
780 OK_(crypt_format(cd,CRYPT_PLAIN,cipher,cipher_mode,NULL,NULL,key_size,¶ms));
783 FAIL_(crypt_keyslot_add_by_passphrase(cd,CRYPT_ANY_SLOT,passphrase,strlen(passphrase),passphrase,strlen(passphrase)), "can't add keyslot to plain device");
784 FAIL_(crypt_keyslot_add_by_volume_key(cd,CRYPT_ANY_SLOT ,key,key_size,passphrase,strlen(passphrase)),"can't add keyslot to plain device");
785 FAIL_(crypt_keyslot_add_by_keyfile(cd,CRYPT_ANY_SLOT,KEYFILE1,strlen(KEY1),KEYFILE2,strlen(KEY2)),"can't add keyslot to plain device");
786 FAIL_(crypt_keyslot_destroy(cd,1),"can't manipulate keyslots on plain device");
787 EQ_(crypt_keyslot_status(cd, 0), CRYPT_SLOT_INVALID);
793 #define CALLBACK_ERROR "calback_error xyz"
794 static int pass_callback_err(const char *msg, char *buf, size_t length, void *usrptr)
796 struct crypt_device *cd = usrptr;
802 crypt_log(cd, CRYPT_LOG_ERROR, CALLBACK_ERROR);
806 static int pass_callback_ok(const char *msg, char *buf, size_t length, void *usrptr)
810 strcpy(buf, PASSPHRASE);
814 static void CallbacksTest(void)
816 struct crypt_device *cd;
817 struct crypt_params_plain params = {
823 size_t key_size = 256 / 8;
824 const char *cipher = "aes";
825 const char *cipher_mode = "cbc-essiv:sha256";
826 const char *passphrase = PASSPHRASE;
827 char buf1[256] = {0}, buf2[256] = {0};
829 OK_(crypt_init(&cd, DEVICE_1));
830 crypt_set_log_callback(cd, &new_log, NULL);
831 //crypt_set_log_callback(cd, NULL, NULL);
833 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, ¶ms));
835 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
836 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
837 OK_(crypt_deactivate(cd, CDEVICE_1));
840 crypt_set_password_callback(cd, pass_callback_err, cd);
841 FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, NULL, 0, 0), "callback fails");
842 EQ_(strncmp(global_log, CALLBACK_ERROR, strlen(CALLBACK_ERROR)), 0);
844 crypt_set_password_callback(cd, pass_callback_ok, NULL);
845 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, NULL, 0, 0));
846 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
847 OK_(crypt_deactivate(cd, CDEVICE_1));
849 // Check error reporting.
850 // This must fail and create error message
851 crypt_deactivate(cd, CDEVICE_1);
853 // Here context must be the same
854 crypt_get_error(buf1, sizeof(buf1));
855 crypt_last_error(cd, buf2, sizeof(buf2));
858 OK_(strcmp(buf1, buf2));
860 crypt_get_error(buf1, sizeof(buf1));
861 crypt_last_error(cd, buf2, sizeof(buf2));
868 static void UseLuksDevice(void)
870 struct crypt_device *cd;
874 OK_(crypt_init(&cd, DEVICE_1));
875 OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
876 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
877 OK_(crypt_activate_by_passphrase(cd, NULL, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0));
878 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0));
879 FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0), "already open");
880 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
881 OK_(crypt_deactivate(cd, CDEVICE_1));
882 FAIL_(crypt_deactivate(cd, CDEVICE_1), "no such device");
885 OK_(strcmp("aes", crypt_get_cipher(cd)));
886 OK_(strcmp("cbc-essiv:sha256", crypt_get_cipher_mode(cd)));
887 OK_(strcmp(DEVICE_1_UUID, crypt_get_uuid(cd)));
888 EQ_((int)key_size, crypt_get_volume_key_size(cd));
889 EQ_(1032, crypt_get_data_offset(cd));
891 EQ_(0, crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key, &key_size, KEY1, strlen(KEY1)));
892 OK_(crypt_volume_key_verify(cd, key, key_size));
893 OK_(crypt_activate_by_volume_key(cd, NULL, key, key_size, 0));
894 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
895 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
896 OK_(crypt_deactivate(cd, CDEVICE_1));
899 FAIL_(crypt_volume_key_verify(cd, key, key_size), "key mismatch");
900 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0), "key mismatch");
904 static void SuspendDevice(void)
907 struct crypt_device *cd;
909 OK_(crypt_init(&cd, DEVICE_1));
910 OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
911 OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0));
913 suspend_status = crypt_suspend(cd, CDEVICE_1);
914 if (suspend_status == -ENOTSUP) {
915 printf("WARNING: Suspend/Resume not supported, skipping test.\n");
919 FAIL_(crypt_suspend(cd, CDEVICE_1), "already suspended");
921 FAIL_(crypt_resume_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1)-1), "wrong key");
922 OK_(crypt_resume_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1)));
923 FAIL_(crypt_resume_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1)), "not suspended");
925 OK_(_prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
926 OK_(crypt_suspend(cd, CDEVICE_1));
927 FAIL_(crypt_resume_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1 "blah", 0), "wrong keyfile");
928 FAIL_(crypt_resume_by_keyfile_offset(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 1, 0), "wrong key");
929 OK_(crypt_resume_by_keyfile_offset(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
930 FAIL_(crypt_resume_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0), "not suspended");
933 OK_(crypt_deactivate(cd, CDEVICE_1));
937 static void AddDeviceLuks(void)
939 struct crypt_device *cd;
940 struct crypt_params_luks1 params = {
942 .data_alignment = 2048, // 4M, data offset will be 4096
943 .data_device = DEVICE_2
945 char key[128], key2[128];
947 const char *passphrase = "blabla";
948 const char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
949 size_t key_size = strlen(mk_hex) / 2;
950 const char *cipher = "aes";
951 const char *cipher_mode = "cbc-essiv:sha256";
952 uint64_t r_payload_offset, r_header_size, r_size_1;
954 crypt_decode_key(key, mk_hex, key_size);
957 OK_(get_luks_offsets(1, key_size, 0, 0, &r_header_size, &r_payload_offset));
958 OK_(create_dmdevice_over_loop(H_DEVICE, r_header_size));
959 OK_(create_dmdevice_over_loop(H_DEVICE_WRONG, r_header_size - 1));
962 OK_(crypt_init(&cd, DMDIR H_DEVICE_WRONG));
963 params.data_alignment = 0;
964 FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms), "Not enough space for keyslots material");
967 // test payload_offset = 0 for encrypted device with external header device
968 OK_(crypt_init(&cd, DMDIR H_DEVICE));
969 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
970 EQ_(crypt_get_data_offset(cd), 0);
973 params.data_alignment = 0;
974 params.data_device = NULL;
976 // test payload_offset = 0. format() should look up alignment offset from device topology
977 OK_(crypt_init(&cd, DEVICE_2));
978 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
979 OK_(!(crypt_get_data_offset(cd) > 0));
983 * test limit values for backing device size
985 params.data_alignment = 4096;
986 OK_(get_luks_offsets(0, key_size, params.data_alignment, 0, NULL, &r_payload_offset));
987 OK_(create_dmdevice_over_loop(L_DEVICE_0S, r_payload_offset));
988 OK_(create_dmdevice_over_loop(L_DEVICE_1S, r_payload_offset + 1));
989 OK_(create_dmdevice_over_loop(L_DEVICE_WRONG, r_payload_offset - 1));
991 // 1 sector less than required
992 OK_(crypt_init(&cd, DMDIR L_DEVICE_WRONG));
993 FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms), "Device too small");
996 // 0 sectors for encrypted area
997 OK_(crypt_init(&cd, DMDIR L_DEVICE_0S));
998 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
999 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0), "Encrypted area too small");
1002 // 1 sector for encrypted area
1003 OK_(crypt_init(&cd, DMDIR L_DEVICE_1S));
1004 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1005 EQ_(crypt_get_data_offset(cd), params.data_alignment);
1006 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1007 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1008 OK_(device_size(DMDIR CDEVICE_1, &r_size_1));
1009 EQ_(r_size_1, SECTOR_SIZE);
1010 OK_(crypt_deactivate(cd, CDEVICE_1));
1011 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
1012 // restrict format only to empty context
1013 FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms), "Context is already formated");
1014 FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, NULL), "Context is already formated");
1015 // change data device to wrong one
1016 OK_(crypt_set_data_device(cd, DMDIR L_DEVICE_0S));
1017 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0), "Device too small");
1018 OK_(crypt_set_data_device(cd, DMDIR L_DEVICE_1S));
1019 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1020 OK_(crypt_deactivate(cd, CDEVICE_1));
1023 params.data_alignment = 0;
1024 params.data_device = DEVICE_2;
1026 // generate keyslot material at the end of luks header
1027 OK_(crypt_init(&cd, DMDIR H_DEVICE));
1028 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1029 EQ_(crypt_keyslot_add_by_volume_key(cd, 7, key, key_size, passphrase, strlen(passphrase)), 7);
1030 EQ_(crypt_activate_by_passphrase(cd, CDEVICE_1, 7, passphrase, strlen(passphrase) ,0), 7);
1032 OK_(crypt_init_by_name_and_header(&cd, CDEVICE_1, DMDIR H_DEVICE));
1033 FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms), "Context is already formated");
1034 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1035 OK_(crypt_deactivate(cd, CDEVICE_1));
1038 params.data_alignment = 2048;
1039 params.data_device = NULL;
1041 // test uuid mismatch and _init_by_name_and_header
1042 OK_(crypt_init(&cd, DMDIR L_DEVICE_1S));
1043 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1044 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1046 params.data_alignment = 0;
1047 params.data_device = DEVICE_2;
1048 OK_(crypt_init(&cd, DMDIR H_DEVICE));
1049 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1051 // there we've got uuid mismatch
1052 OK_(crypt_init_by_name_and_header(&cd, CDEVICE_1, DMDIR H_DEVICE));
1053 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1054 OK_((int)crypt_get_type(cd));
1055 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0), "Device is active");
1056 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, key, key_size, 0), "Device is active");
1057 EQ_(crypt_status(cd, CDEVICE_2), CRYPT_INACTIVE);
1058 OK_(crypt_deactivate(cd, CDEVICE_1));
1061 params.data_device = NULL;
1063 OK_(crypt_init(&cd, DEVICE_2));
1064 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1066 // even with no keyslots defined it can be activated by volume key
1067 OK_(crypt_volume_key_verify(cd, key, key_size));
1068 OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, key, key_size, 0));
1069 EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
1070 OK_(crypt_deactivate(cd, CDEVICE_2));
1073 EQ_(7, crypt_keyslot_add_by_volume_key(cd, 7, key, key_size, passphrase, strlen(passphrase)));
1074 EQ_(CRYPT_SLOT_ACTIVE_LAST, crypt_keyslot_status(cd, 7));
1075 EQ_(7, crypt_activate_by_passphrase(cd, CDEVICE_2, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
1076 EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
1077 OK_(crypt_deactivate(cd, CDEVICE_2));
1079 crypt_set_iteration_time(cd, 1);
1080 EQ_(1, crypt_keyslot_add_by_volume_key(cd, 1, key, key_size, KEY1, strlen(KEY1)));
1081 OK_(_prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
1082 OK_(_prepare_keyfile(KEYFILE2, KEY2, strlen(KEY2)));
1083 EQ_(2, crypt_keyslot_add_by_keyfile(cd, 2, KEYFILE1, 0, KEYFILE2, 0));
1084 FAIL_(crypt_keyslot_add_by_keyfile_offset(cd, 3, KEYFILE1, 0, 1, KEYFILE2, 0, 1), "wrong key");
1085 EQ_(3, crypt_keyslot_add_by_keyfile_offset(cd, 3, KEYFILE1, 0, 0, KEYFILE2, 0, 1));
1086 EQ_(4, crypt_keyslot_add_by_keyfile_offset(cd, 4, KEYFILE2, 0, 1, KEYFILE1, 0, 1));
1087 FAIL_(crypt_activate_by_keyfile(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, strlen(KEY2)-1, 0), "key mismatch");
1088 EQ_(2, crypt_activate_by_keyfile(cd, NULL, CRYPT_ANY_SLOT, KEYFILE2, 0, 0));
1089 EQ_(3, crypt_activate_by_keyfile_offset(cd, NULL, CRYPT_ANY_SLOT, KEYFILE2, 0, 1, 0));
1090 EQ_(4, crypt_activate_by_keyfile_offset(cd, NULL, CRYPT_ANY_SLOT, KEYFILE1, 0, 1, 0));
1091 FAIL_(crypt_activate_by_keyfile_offset(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, strlen(KEY2), 2, 0), "not enough data");
1092 FAIL_(crypt_activate_by_keyfile_offset(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, 0, strlen(KEY2) + 1, 0), "cannot seek");
1093 FAIL_(crypt_activate_by_keyfile_offset(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, 0, 2, 0), "wrong key");
1094 EQ_(2, crypt_activate_by_keyfile(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, 0, 0));
1095 OK_(crypt_keyslot_destroy(cd, 1));
1096 OK_(crypt_keyslot_destroy(cd, 2));
1097 OK_(crypt_keyslot_destroy(cd, 3));
1098 OK_(crypt_keyslot_destroy(cd, 4));
1099 OK_(crypt_deactivate(cd, CDEVICE_2));
1102 FAIL_(crypt_keyslot_add_by_volume_key(cd, 7, key, key_size, passphrase, strlen(passphrase)), "slot used");
1104 FAIL_(crypt_keyslot_add_by_volume_key(cd, 6, key, key_size, passphrase, strlen(passphrase)), "key mismatch");
1106 EQ_(6, crypt_keyslot_add_by_volume_key(cd, 6, key, key_size, passphrase, strlen(passphrase)));
1107 EQ_(CRYPT_SLOT_ACTIVE, crypt_keyslot_status(cd, 6));
1109 FAIL_(crypt_keyslot_destroy(cd, 8), "invalid keyslot");
1110 FAIL_(crypt_keyslot_destroy(cd, CRYPT_ANY_SLOT), "invalid keyslot");
1111 FAIL_(crypt_keyslot_destroy(cd, 0), "keyslot not used");
1112 OK_(crypt_keyslot_destroy(cd, 7));
1113 EQ_(CRYPT_SLOT_INACTIVE, crypt_keyslot_status(cd, 7));
1114 EQ_(CRYPT_SLOT_ACTIVE_LAST, crypt_keyslot_status(cd, 6));
1116 EQ_(6, crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)));
1117 OK_(crypt_volume_key_verify(cd, key2, key_size));
1119 OK_(memcmp(key, key2, key_size));
1120 OK_(strcmp(cipher, crypt_get_cipher(cd)));
1121 OK_(strcmp(cipher_mode, crypt_get_cipher_mode(cd)));
1122 EQ_((int)key_size, crypt_get_volume_key_size(cd));
1123 EQ_(4096, crypt_get_data_offset(cd));
1124 OK_(strcmp(DEVICE_2, crypt_get_device_name(cd)));
1127 crypt_set_log_callback(cd, &new_log, NULL);
1128 OK_(crypt_dump(cd));
1129 OK_(!(global_lines != 0));
1130 crypt_set_log_callback(cd, NULL, NULL);
1133 FAIL_(crypt_set_uuid(cd, "blah"), "wrong UUID format");
1134 OK_(crypt_set_uuid(cd, DEVICE_TEST_UUID));
1135 OK_(strcmp(DEVICE_TEST_UUID, crypt_get_uuid(cd)));
1137 FAIL_(crypt_deactivate(cd, CDEVICE_2), "not active");
1139 _cleanup_dmdevices();
1142 static void UseTempVolumes(void)
1144 struct crypt_device *cd;
1147 // Tepmporary device without keyslot but with on-disk LUKS header
1148 OK_(crypt_init(&cd, DEVICE_2));
1149 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "not yet formatted");
1150 OK_(crypt_format(cd, CRYPT_LUKS1, "aes", "cbc-essiv:sha256", NULL, NULL, 16, NULL));
1151 OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0));
1152 EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
1155 OK_(crypt_init_by_name(&cd, CDEVICE_2));
1156 OK_(crypt_deactivate(cd, CDEVICE_2));
1159 // Dirty checks: device without UUID
1160 // we should be able to remove it but not manuipulate with it
1161 snprintf(tmp, sizeof(tmp), "dmsetup create %s --table \""
1162 "0 100 crypt aes-cbc-essiv:sha256 deadbabedeadbabedeadbabedeadbabe 0 "
1163 "%s 2048\"", CDEVICE_2, DEVICE_2);
1165 OK_(crypt_init_by_name(&cd, CDEVICE_2));
1166 OK_(crypt_deactivate(cd, CDEVICE_2));
1167 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "No known device type");
1170 // Dirty checks: device with UUID but LUKS header key fingerprint must fail)
1171 snprintf(tmp, sizeof(tmp), "dmsetup create %s --table \""
1172 "0 100 crypt aes-cbc-essiv:sha256 deadbabedeadbabedeadbabedeadbabe 0 "
1173 "%s 2048\" -u CRYPT-LUKS1-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-ctest1",
1174 CDEVICE_2, DEVICE_2);
1176 OK_(crypt_init_by_name(&cd, CDEVICE_2));
1177 OK_(crypt_deactivate(cd, CDEVICE_2));
1178 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "wrong volume key");
1182 OK_(crypt_init(&cd, DEVICE_2));
1183 OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
1184 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "volume key is lost");
1188 OK_(crypt_init(&cd, DEVICE_2));
1189 OK_(crypt_format(cd, CRYPT_PLAIN, "aes", "cbc-essiv:sha256", NULL, NULL, 16, NULL));
1190 FAIL_(crypt_activate_by_volume_key(cd, NULL, "xxx", 3, 0), "cannot verify key with plain");
1191 FAIL_(crypt_volume_key_verify(cd, "xxx", 3), "cannot verify key with plain");
1192 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, "xxx", 3, 0), "wrong key lenght");
1193 OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, "volumekeyvolumek", 16, 0));
1194 EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
1195 OK_(crypt_deactivate(cd, CDEVICE_2));
1199 static void LuksHeaderRestore(void)
1201 struct crypt_device *cd;
1202 struct crypt_params_luks1 params = {
1204 .data_alignment = 2048, // 4M, data offset will be 4096
1206 struct crypt_params_plain pl_params = {
1212 char key[128], key2[128], cmd[256];
1214 const char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
1215 size_t key_size = strlen(mk_hex) / 2;
1216 const char *cipher = "aes";
1217 const char *cipher_mode = "cbc-essiv:sha256";
1218 uint64_t r_payload_offset;
1220 crypt_decode_key(key, mk_hex, key_size);
1222 OK_(get_luks_offsets(0, key_size, params.data_alignment, 0, NULL, &r_payload_offset));
1223 OK_(create_dmdevice_over_loop(L_DEVICE_OK, r_payload_offset + 5000));
1225 // do not restore header over plain device
1226 OK_(crypt_init(&cd, DMDIR L_DEVICE_OK));
1227 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, &pl_params));
1228 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1229 FAIL_(crypt_header_restore(cd, CRYPT_PLAIN, VALID_HEADER), "Cannot restore header to PLAIN type device");
1230 FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, VALID_HEADER), "Cannot restore header over PLAIN type device");
1231 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1232 OK_(crypt_deactivate(cd, CDEVICE_1));
1236 OK_(crypt_init(&cd, DMDIR L_DEVICE_OK));
1237 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1238 FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, EVL_HEADER_1), "Header corrupted");
1239 FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, EVL_HEADER_2), "Header corrupted");
1240 FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, EVL_HEADER_3), "Header corrupted");
1241 FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, EVL_HEADER_4), "Header too small");
1242 OK_(crypt_header_restore(cd, CRYPT_LUKS1, VALID_HEADER));
1243 // wipe valid luks header
1244 snprintf(cmd, sizeof(cmd), "dd if=/dev/zero of=" DMDIR L_DEVICE_OK " bs=512 count=%" PRIu64, r_payload_offset);
1245 OK_(_system(cmd, 1));
1246 FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, EVL_HEADER_1), "Header corrupted");
1247 FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, EVL_HEADER_2), "Header corrupted");
1248 FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, EVL_HEADER_3), "Header corrupted");
1249 FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, EVL_HEADER_4), "Header too small");
1250 OK_(crypt_header_restore(cd, CRYPT_LUKS1, VALID_HEADER));
1251 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1252 OK_(crypt_deactivate(cd, CDEVICE_1));
1255 // volume key_size mismatch
1256 OK_(crypt_init(&cd, DMDIR L_DEVICE_OK));
1257 memcpy(key2, key, key_size - 1);
1258 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key2, key_size - 1, ¶ms));
1259 FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, VALID_HEADER), "Volume keysize mismatch");
1262 // payload offset mismatch
1263 params.data_alignment = 8192;
1264 OK_(crypt_init(&cd, DMDIR L_DEVICE_OK));
1265 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1266 FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, VALID_HEADER), "Payload offset mismatch");
1267 //_system("dmsetup table;sleep 1",1);
1270 _cleanup_dmdevices();
1273 static void LuksHeaderLoad(void)
1275 struct crypt_device *cd;
1276 struct crypt_params_luks1 params = {
1278 .data_alignment = 2048,
1280 struct crypt_params_plain pl_params = {
1286 char key[128], cmd[256];
1288 const char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
1289 size_t key_size = strlen(mk_hex) / 2;
1290 const char *cipher = "aes";
1291 const char *cipher_mode = "cbc-essiv:sha256";
1292 uint64_t r_payload_offset, r_header_size;
1294 crypt_decode_key(key, mk_hex, key_size);
1297 OK_(get_luks_offsets(0, key_size, params.data_alignment, 0, &r_header_size, &r_payload_offset));
1298 // external header device
1299 OK_(create_dmdevice_over_loop(H_DEVICE, r_header_size));
1300 // prepared header on a device too small to contain header and payload
1301 OK_(create_dmdevice_over_loop(H_DEVICE_WRONG, r_payload_offset - 1));
1302 snprintf(cmd, sizeof(cmd), "dd if=" EVL_HEADER_4 " of=" DMDIR H_DEVICE_WRONG " bs=512 count=%" PRIu64, r_payload_offset - 1);
1303 OK_(_system(cmd, 1));
1305 OK_(create_dmdevice_over_loop(L_DEVICE_OK, r_payload_offset + 1000));
1307 OK_(create_dmdevice_over_loop(L_DEVICE_1S, r_payload_offset + 1));
1308 // 0 sectors device for payload
1309 OK_(create_dmdevice_over_loop(L_DEVICE_0S, r_payload_offset));
1311 // valid metadata and device size
1312 params.data_alignment = 0;
1313 params.data_device = DMDIR L_DEVICE_OK;
1314 OK_(crypt_init(&cd, DMDIR H_DEVICE));
1315 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1317 OK_(crypt_init(&cd, DMDIR H_DEVICE));
1318 OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
1319 OK_(crypt_set_data_device(cd, DMDIR L_DEVICE_OK));
1320 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1321 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1322 OK_(crypt_deactivate(cd, CDEVICE_1));
1325 // bad header: device too small (payloadOffset > device_size)
1326 OK_(crypt_init(&cd, DMDIR H_DEVICE_WRONG));
1327 FAIL_(crypt_load(cd, CRYPT_LUKS1, NULL), "Device too small");
1328 OK_((int)crypt_get_type(cd));
1331 // 0 secs for encrypted data area
1332 params.data_alignment = 2048;
1333 params.data_device = NULL;
1334 OK_(crypt_init(&cd, DMDIR L_DEVICE_0S));
1335 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1337 // load should be ok
1338 OK_(crypt_init(&cd, DMDIR L_DEVICE_0S));
1339 OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
1340 FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0), "Device too small");
1341 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
1345 OK_(_system("dd if=/dev/zero of=" DMDIR L_DEVICE_0S "bs=512 count=8", 1));
1346 OK_(crypt_init(&cd, DMDIR L_DEVICE_OK));
1347 FAIL_(crypt_load(cd, CRYPT_LUKS1, NULL), "Header not found");
1351 OK_(crypt_init(&cd, DMDIR H_DEVICE));
1352 FAIL_(crypt_load(cd, CRYPT_PLAIN, NULL), "Can't load nonLUKS device type");
1354 OK_(crypt_init(&cd, DMDIR H_DEVICE));
1355 OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, key, key_size, &pl_params));
1356 FAIL_(crypt_load(cd, CRYPT_LUKS1, NULL), "Can't load over nonLUKS device type");
1359 _cleanup_dmdevices();
1362 static void LuksHeaderBackup(void)
1364 struct crypt_device *cd;
1365 struct crypt_params_luks1 params = {
1367 .data_alignment = 2048,
1371 const char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
1372 size_t key_size = strlen(mk_hex) / 2;
1373 const char *cipher = "aes";
1374 const char *cipher_mode = "cbc-essiv:sha256";
1375 uint64_t r_payload_offset;
1377 crypt_decode_key(key, mk_hex, key_size);
1379 OK_(get_luks_offsets(0, key_size, params.data_alignment, 0, NULL, &r_payload_offset));
1380 OK_(create_dmdevice_over_loop(L_DEVICE_OK, r_payload_offset + 1));
1382 // create LUKS device and backup the header
1383 OK_(crypt_init(&cd, DMDIR L_DEVICE_OK));
1384 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1385 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1386 OK_(crypt_header_backup(cd, CRYPT_LUKS1, BACKUP_FILE));
1387 OK_(crypt_deactivate(cd, CDEVICE_1));
1390 // restore header from backup
1391 OK_(crypt_init(&cd, DMDIR L_DEVICE_OK));
1392 OK_(crypt_header_restore(cd, CRYPT_LUKS1, BACKUP_FILE));
1393 OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
1394 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1395 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1396 OK_(crypt_deactivate(cd, CDEVICE_1));
1399 _cleanup_dmdevices();
1402 static void ResizeDeviceLuks(void)
1404 struct crypt_device *cd;
1405 struct crypt_params_luks1 params = {
1407 .data_alignment = 2048,
1411 const char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
1412 size_t key_size = strlen(mk_hex) / 2;
1413 const char *cipher = "aes";
1414 const char *cipher_mode = "cbc-essiv:sha256";
1415 uint64_t r_payload_offset, r_header_size, r_size;
1417 crypt_decode_key(key, mk_hex, key_size);
1420 OK_(get_luks_offsets(0, key_size, params.data_alignment, 0, NULL, &r_payload_offset));
1421 OK_(get_luks_offsets(1, key_size, 0, 0, &r_header_size, NULL));
1422 OK_(create_dmdevice_over_loop(H_DEVICE, r_header_size));
1423 OK_(create_dmdevice_over_loop(L_DEVICE_OK, r_payload_offset + 1000));
1424 OK_(create_dmdevice_over_loop(L_DEVICE_0S, 1000));
1426 // test header and encrypted payload all in one device
1427 OK_(crypt_init(&cd, DMDIR L_DEVICE_OK));
1428 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1429 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1430 OK_(crypt_resize(cd, CDEVICE_1, 42));
1431 if (!device_size(DMDIR CDEVICE_1, &r_size))
1432 EQ_(42, r_size >> SECTOR_SHIFT);
1433 // autodetect encrypted device area size
1434 OK_(crypt_resize(cd, CDEVICE_1, 0));
1435 if (!device_size(DMDIR CDEVICE_1, &r_size))
1436 EQ_(1000, r_size >> SECTOR_SHIFT);
1437 FAIL_(crypt_resize(cd, CDEVICE_1, 1001), "Device too small");
1438 if (!device_size(DMDIR CDEVICE_1, &r_size))
1439 EQ_(1000, r_size >> SECTOR_SHIFT);
1440 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1441 OK_(crypt_deactivate(cd, CDEVICE_1));
1444 params.data_alignment = 0;
1445 params.data_device = DMDIR L_DEVICE_0S;
1446 // test case for external header
1447 OK_(crypt_init(&cd, DMDIR H_DEVICE));
1448 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1449 OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1450 OK_(crypt_resize(cd, CDEVICE_1, 666));
1451 if (!device_size(DMDIR CDEVICE_1, &r_size))
1452 EQ_(666, r_size >> SECTOR_SHIFT);
1453 // autodetect encrypted device size
1454 OK_(crypt_resize(cd, CDEVICE_1, 0));
1455 if (!device_size(DMDIR CDEVICE_1, &r_size))
1456 EQ_(1000, r_size >> SECTOR_SHIFT);
1457 FAIL_(crypt_resize(cd, CDEVICE_1, 1001), "Device too small");
1458 if (!device_size(DMDIR CDEVICE_1, &r_size))
1459 EQ_(1000, r_size >> SECTOR_SHIFT);
1460 EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1461 OK_(crypt_deactivate(cd, CDEVICE_1));
1464 _cleanup_dmdevices();
1467 static void HashDevicePlain(void)
1469 struct crypt_device *cd;
1470 struct crypt_params_plain params = {
1477 const char *mk_hex, *keystr;
1480 OK_(crypt_init(&cd, DEVICE_1));
1481 OK_(crypt_format(cd, CRYPT_PLAIN, "aes", "cbc-essiv:sha256", NULL, NULL, 16, ¶ms));
1483 // hash PLAIN, short key
1484 OK_(_prepare_keyfile(KEYFILE1, "tooshort", 8));
1485 FAIL_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 16, 0), "not enough data in keyfile");
1488 // hash PLAIN, exact key
1489 // 0 1 2 3 4 5 6 7 8 9 a b c d e f
1490 mk_hex = "caffeecaffeecaffeecaffeecaffee88";
1492 crypt_decode_key(key, mk_hex, key_size);
1493 OK_(_prepare_keyfile(KEYFILE1, key, key_size));
1494 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size, 0));
1495 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1496 OK_(strcmp(key, mk_hex));
1497 OK_(crypt_deactivate(cd, CDEVICE_1));
1500 mk_hex = "caffeecaffeecaffeecaffeeca000000";
1501 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size - 3, 0));
1502 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1503 OK_(strcmp(key, mk_hex));
1504 OK_(crypt_deactivate(cd, CDEVICE_1));
1508 // hash PLAIN, long key
1509 // 0 1 2 3 4 5 6 7 8 9 a b c d e f
1510 mk_hex = "caffeecaffeecaffeecaffeecaffee88babebabe";
1512 crypt_decode_key(key, mk_hex, key_size);
1513 OK_(_prepare_keyfile(KEYFILE1, key, strlen(mk_hex) / 2));
1514 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size, 0));
1515 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1516 FAIL_(strcmp(key, mk_hex), "only key length used");
1517 OK_(strncmp(key, mk_hex, key_size));
1518 OK_(crypt_deactivate(cd, CDEVICE_1));
1521 // Now without explicit limit
1522 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
1523 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1524 FAIL_(strcmp(key, mk_hex), "only key length used");
1525 OK_(strncmp(key, mk_hex, key_size));
1526 OK_(crypt_deactivate(cd, CDEVICE_1));
1532 params.hash = "sha256";
1533 OK_(crypt_init(&cd, DEVICE_1));
1534 OK_(crypt_format(cd, CRYPT_PLAIN, "aes", "cbc-essiv:sha256", NULL, NULL, 16, ¶ms));
1536 // 0 1 2 3 4 5 6 7 8 9 a b c d e f
1537 mk_hex = "c62e4615bd39e222572f3a1bf7c2132e";
1538 keystr = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
1539 key_size = strlen(keystr); // 32
1540 OK_(_prepare_keyfile(KEYFILE1, keystr, strlen(keystr)));
1541 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size, 0));
1542 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1543 OK_(strcmp(key, mk_hex));
1544 OK_(crypt_deactivate(cd, CDEVICE_1));
1546 // Read full keyfile
1547 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
1548 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1549 OK_(strcmp(key, mk_hex));
1550 OK_(crypt_deactivate(cd, CDEVICE_1));
1554 // Limit keyfile read
1555 keystr = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxAAAAAAAA";
1556 OK_(_prepare_keyfile(KEYFILE1, keystr, strlen(keystr)));
1557 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size, 0));
1558 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1559 OK_(strcmp(key, mk_hex));
1560 OK_(crypt_deactivate(cd, CDEVICE_1));
1563 OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
1564 OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1565 OK_(strcmp(key, "0e49cb34a1dee1df33f6505e4de44a66"));
1566 OK_(crypt_deactivate(cd, CDEVICE_1));
1570 // FIXME: add keyfile="-" tests somehow
1575 // Check that gcrypt is properly initialised in format
1576 static void NonFIPSAlg(void)
1578 struct crypt_device *cd;
1579 struct crypt_params_luks1 params = {0};
1581 size_t key_size = 128;
1582 const char *cipher = "aes";
1583 const char *cipher_mode = "cbc-essiv:sha256";
1586 OK_(crypt_init(&cd, DEVICE_2));
1587 params.hash = "sha256";
1588 OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms));
1590 params.hash = "whirlpool";
1591 ret = crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms);
1593 printf("WARNING: whirlpool not supported, skipping test.\n");
1598 params.hash = "md5";
1599 FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, ¶ms),
1600 "MD5 unsupported, too short");
1604 int main (int argc, char *argv[])
1608 if (getuid() != 0) {
1609 printf("You must be root to run this test.\n");
1613 for (i = 1; i < argc; i++) {
1614 if (!strcmp("-v", argv[i]) || !strcmp("--verbose", argv[i]))
1616 else if (!strcmp("--debug", argv[i]))
1617 _debug = _verbose = 1;
1624 crypt_set_debug_level(_debug ? CRYPT_DEBUG_ALL : CRYPT_DEBUG_NONE);
1626 RUN_(NonFIPSAlg, "Crypto is properly initialised in format"); //must be the first!
1627 RUN_(AddDevicePlain, "plain device API creation exercise");
1628 RUN_(HashDevicePlain, "plain device API hash test");
1629 RUN_(AddDeviceLuks, "Format and use LUKS device");
1630 RUN_(LuksHeaderLoad, "test header load");
1631 RUN_(LuksHeaderRestore, "test LUKS header restore");
1632 RUN_(LuksHeaderBackup, "test LUKS header backup");
1633 RUN_(ResizeDeviceLuks, "Luks device resize tests");
1634 RUN_(UseLuksDevice, "Use pre-formated LUKS device");
1635 RUN_(SuspendDevice, "Suspend/Resume test");
1636 RUN_(UseTempVolumes, "Format and use temporary encrypted device");
1638 RUN_(CallbacksTest, "API callbacks test");