Add TCRYPT api test, fix some minor problems found.
[platform/upstream/cryptsetup.git] / tests / api-test.c
1 /*
2  * cryptsetup library API check functions
3  *
4  * Copyright (C) 2009-2012 Red Hat, Inc. All rights reserved.
5  *
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.
9  *
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.
14  *
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.
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <linux/fs.h>
26 #include <errno.h>
27 #include <assert.h>
28 #include <sys/stat.h>
29 #include <sys/ioctl.h>
30 #include <libdevmapper.h>
31
32 #include "luks.h"
33 #include "libcryptsetup.h"
34 #include "utils_loop.h"
35
36 #define DMDIR "/dev/mapper/"
37
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
43
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"
61
62 #define KEYFILE1 "key1.file"
63 #define KEY1 "compatkey"
64
65 #define KEYFILE2 "key2.file"
66 #define KEY2 "0123456789abcdef"
67
68 #define PASSPHRASE "blabla"
69
70 #define DEVICE_TEST_UUID "12345678-1234-1234-1234-123456789abc"
71
72 #define DEVICE_WRONG "/dev/Ooo_"
73 #define DEVICE_CHAR "/dev/zero"
74 #define THE_LFILE_TEMPLATE "cryptsetup-tstlp.XXXXXX"
75
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
82
83 static int _debug   = 0;
84 static int _verbose = 1;
85
86 static char global_log[4096];
87 static int global_lines = 0;
88
89 static char *DEVICE_1 = NULL;
90 static char *DEVICE_2 = NULL;
91 static char *THE_LOOP_DEV = NULL;
92
93 static char *tmp_file_1 = NULL;
94 static char *test_loop_file = NULL;
95 static uint64_t t_dev_offset = 0;
96
97 static int _system(const char*, int);
98
99 // Helpers
100
101 static int device_size(const char *device, uint64_t *size)
102 {
103         int devfd, r = 0;
104
105         devfd = open(device, O_RDONLY);
106         if(devfd == -1)
107                 return -EINVAL;
108
109         if (ioctl(devfd, BLKGETSIZE64, size) < 0)
110                 r = -EINVAL;
111         close(devfd);
112         return r;
113 }
114
115 static int get_luks_offsets(int metadata_device,
116                             size_t keylength,
117                             unsigned int alignpayload_sec,
118                             unsigned int alignoffset_sec,
119                             uint64_t *r_header_size,
120                             uint64_t *r_payload_offset)
121 {
122         int i;
123         uint64_t current_sector;
124         uint32_t sectors_per_stripes_set;
125
126         if (!keylength)
127                 return -1;
128
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);
136         if (r_header_size)
137                 *r_header_size = current_sector + sectors_per_stripes_set;
138
139         current_sector = DIV_ROUND_UP_MODULO(current_sector + sectors_per_stripes_set,
140                                 LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE);
141
142         if (r_payload_offset) {
143                 if (metadata_device)
144                         *r_payload_offset = alignpayload_sec;
145                 else
146                         *r_payload_offset = DIV_ROUND_UP_MODULO(current_sector, alignpayload_sec)
147                                 + alignoffset_sec;
148         }
149
150         return 0;
151 }
152
153 /*
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
157  */
158 static int create_dmdevice_over_loop(const char *dm_name, const uint64_t size)
159 {
160         char cmd[128];
161         int r;
162         uint64_t r_size;
163
164         if(device_size(THE_LOOP_DEV, &r_size) < 0 || r_size <= t_dev_offset || !size) 
165                 return -1;
166         if ((r_size - t_dev_offset) < size) {
167                 printf("No enough space on backing loop device\n.");
168                 return -2;
169         }
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;
175         }
176         return r;
177 }
178
179 // TODO some utility to remove dmdevice over the loop file
180
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)
183 {
184         struct dm_task *dmt;
185         struct dm_info dmi;
186         uint64_t start, length;
187         char *target_type, *key, *params;
188         void *next = NULL;
189         int r = -EINVAL;
190
191         if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
192                 goto out;
193         if (!dm_task_set_name(dmt, name))
194                 goto out;
195         if (!dm_task_run(dmt))
196                 goto out;
197         if (!dm_task_get_info(dmt, &dmi))
198                 goto out;
199         if (!dmi.exists)
200                 goto out;
201
202         next = dm_get_next_target(dmt, next, &start, &length, &target_type, &params);
203         if (!target_type || strcmp(target_type, "crypt") != 0)
204                 goto out;
205
206         (void)strsep(&params, " "); /* rcipher */
207         key = strsep(&params, " ");
208
209         if (buffer_size <= strlen(key))
210                 goto out;
211
212         strncpy(buffer, key, buffer_size);
213         r = 0;
214 out:
215         if (dmt)
216                 dm_task_destroy(dmt);
217
218         return r;
219 }
220
221 static int _prepare_keyfile(const char *name, const char *passphrase, int size)
222 {
223         int fd, r;
224
225         fd = open(name, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR);
226         if (fd != -1) {
227                 r = write(fd, passphrase, size);
228                 close(fd);
229         } else
230                 r = 0;
231
232         return r == size ? 0 : 1;
233 }
234
235 static void _remove_keyfiles(void)
236 {
237         remove(KEYFILE1);
238         remove(KEYFILE2);
239 }
240
241 // Decode key from its hex representation
242 static int crypt_decode_key(char *key, const char *hex, unsigned int size)
243 {
244         char buffer[3];
245         char *endp;
246         unsigned int i;
247
248         buffer[2] = '\0';
249
250         for (i = 0; i < size; i++) {
251                 buffer[0] = *hex++;
252                 buffer[1] = *hex++;
253
254                 key[i] = (unsigned char)strtoul(buffer, &endp, 16);
255
256                 if (endp != &buffer[2])
257                         return -1;
258         }
259
260         if (*hex != '\0')
261                 return -1;
262
263         return 0;
264 }
265
266 static void cmdLineLog(int level, const char *msg)
267 {
268         strncat(global_log, msg, sizeof(global_log) - strlen(global_log));
269         global_lines++;
270 }
271
272 static void new_log(int level, const char *msg, void *usrptr)
273 {
274         if (_debug)
275                 printf("LOG: %s", msg);
276         cmdLineLog(level, msg);
277 }
278
279 static void reset_log(void)
280 {
281         memset(global_log, 0, sizeof(global_log));
282         global_lines = 0;
283 }
284
285 static int _system(const char *command, int warn)
286 {
287         int r;
288         if (_debug)
289                 printf("Running system: %s\n", command);
290         if ((r=system(command)) < 0 && warn)
291                 printf("System command failed: %s", command);
292         return r;
293 }
294
295 static void _cleanup_dmdevices(void)
296 {
297         struct stat st;
298
299         if (!stat(DMDIR H_DEVICE, &st)) {
300                 _system("dmsetup remove " H_DEVICE, 0);
301         }
302         if (!stat(DMDIR H_DEVICE_WRONG, &st)) {
303                 _system("dmsetup remove " H_DEVICE_WRONG, 0);
304         }
305         if (!stat(DMDIR L_DEVICE_0S, &st)) {
306                 _system("dmsetup remove " L_DEVICE_0S, 0);
307         }
308         if (!stat(DMDIR L_DEVICE_1S, &st)) {
309                 _system("dmsetup remove " L_DEVICE_1S, 0);
310         }
311         if (!stat(DMDIR L_DEVICE_WRONG, &st)) {
312                 _system("dmsetup remove " L_DEVICE_WRONG, 0);
313         }
314         if (!stat(DMDIR L_DEVICE_OK, &st)) {
315                 _system("dmsetup remove " L_DEVICE_OK, 0);
316         }
317
318         t_dev_offset = 0;
319 }
320
321 static void _cleanup(void)
322 {
323         struct stat st;
324
325         //_system("udevadm settle", 0);
326
327         if (!stat(DMDIR CDEVICE_1, &st))
328                 _system("dmsetup remove " CDEVICE_1, 0);
329
330         if (!stat(DMDIR CDEVICE_2, &st))
331                 _system("dmsetup remove " CDEVICE_2, 0);
332
333         if (!stat(DEVICE_EMPTY, &st))
334                 _system("dmsetup remove " DEVICE_EMPTY_name, 0);
335
336         if (!stat(DEVICE_ERROR, &st))
337                 _system("dmsetup remove " DEVICE_ERROR_name, 0);
338
339         _cleanup_dmdevices();
340
341         if (crypt_loop_device(THE_LOOP_DEV))
342                 crypt_loop_detach(THE_LOOP_DEV);
343
344         if (crypt_loop_device(DEVICE_1))
345                 crypt_loop_detach(DEVICE_1);
346
347         if (crypt_loop_device(DEVICE_2))
348                 crypt_loop_detach(DEVICE_2);
349
350         _system("rm -f " IMAGE_EMPTY, 0);
351         _system("rm -f " IMAGE1, 0);
352
353         remove(test_loop_file);
354         remove(tmp_file_1);
355
356         remove(EVL_HEADER_1);
357         remove(EVL_HEADER_2);
358         remove(EVL_HEADER_3);
359         remove(EVL_HEADER_4);
360         remove(VALID_HEADER);
361         remove(BACKUP_FILE);
362
363         _remove_keyfiles();
364
365         free(tmp_file_1);
366         free(test_loop_file);
367         free(THE_LOOP_DEV);
368         free(DEVICE_1);
369         free(DEVICE_2);
370 }
371
372 static int _setup(void)
373 {
374         int fd, ro = 0;
375         char cmd[128];
376
377         test_loop_file = strdup(THE_LFILE_TEMPLATE);
378         if ((fd=mkstemp(test_loop_file)) == -1) {
379                 printf("cannot create temporary file with template %s\n", test_loop_file);
380                 return 1;
381         }
382         close(fd);
383         snprintf(cmd, sizeof(cmd), "dd if=/dev/zero of=%s bs=%d count=%d 2>/dev/null",
384                  test_loop_file, SECTOR_SIZE, TST_LOOP_FILE_SIZE);
385         if (_system(cmd, 1))
386                 return 1;
387
388         if (!THE_LOOP_DEV)
389                 THE_LOOP_DEV = crypt_loop_get_device();
390         if (!THE_LOOP_DEV) {
391                 printf("Cannot find free loop device.\n");
392                 return 1;
393         }
394         if (crypt_loop_device(THE_LOOP_DEV)) {
395                 fd = crypt_loop_attach(THE_LOOP_DEV, test_loop_file, 0, 0, &ro);
396                 close(fd);
397         }
398
399         tmp_file_1 = strdup(THE_LFILE_TEMPLATE);
400         if ((fd=mkstemp(tmp_file_1)) == -1) {
401                 printf("cannot create temporary file with template %s\n", tmp_file_1);
402                 return 1;
403         }
404         close(fd);
405         snprintf(cmd, sizeof(cmd), "dd if=/dev/zero of=%s bs=%d count=%d 2>/dev/null",
406                  tmp_file_1, SECTOR_SIZE, 10);
407         if (_system(cmd, 1))
408                 return 1;
409
410         _system("dmsetup create " DEVICE_EMPTY_name " --table \"0 10000 zero\"", 1);
411         _system("dmsetup create " DEVICE_ERROR_name " --table \"0 10000 error\"", 1);
412         if (!DEVICE_1)
413                 DEVICE_1 = crypt_loop_get_device();
414         if (!DEVICE_1) {
415                 printf("Cannot find free loop device.\n");
416                 return 1;
417         }
418         if (crypt_loop_device(DEVICE_1)) {
419                 _system(" [ ! -e " IMAGE1 " ] && bzip2 -dk " IMAGE1 ".bz2", 1);
420                 fd = crypt_loop_attach(DEVICE_1, IMAGE1, 0, 0, &ro);
421                 close(fd);
422         }
423         if (!DEVICE_2)
424                 DEVICE_2 = crypt_loop_get_device();
425         if (!DEVICE_2) {
426                 printf("Cannot find free loop device.\n");
427                 return 1;
428         }
429         if (crypt_loop_device(DEVICE_2)) {
430                 _system("dd if=/dev/zero of=" IMAGE_EMPTY " bs=1M count=4 2>/dev/null", 1);
431                 fd = crypt_loop_attach(DEVICE_2, IMAGE_EMPTY, 0, 0, &ro);
432                 close(fd);
433         }
434         /* Keymaterial offset is less than 8 sectors */
435         _system(" [ ! -e " EVL_HEADER_1 " ] && bzip2 -dk " EVL_HEADER_1 ".bz2", 1);
436         /* keymaterial offset aims into payload area */
437         _system(" [ ! -e " EVL_HEADER_2 " ] && bzip2 -dk " EVL_HEADER_2 ".bz2", 1);
438         /* keymaterial offset is valid, number of stripes causes payload area to be overwriten */
439         _system(" [ ! -e " EVL_HEADER_3 " ] && bzip2 -dk " EVL_HEADER_3 ".bz2", 1);
440         /* luks device header for data and header on same device. payloadOffset is greater than
441          * device size (crypt_load() test) */
442         _system(" [ ! -e " EVL_HEADER_4 " ] && bzip2 -dk " EVL_HEADER_4 ".bz2", 1);
443         /* valid header: payloadOffset=4096, key_size=32,
444          * volume_key = bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a */
445         _system(" [ ! -e " VALID_HEADER " ] && bzip2 -dk " VALID_HEADER ".bz2", 1);
446
447         /* Prepare tcrypt images */
448         _system(" [ ! -d tcrypt-images ] && tar xjf tcrypt-images.tar.bz2 2>/dev/null", 1);
449
450         _system("modprobe dm-crypt", 0);
451         _system("modprobe dm-verity", 0);
452         return 0;
453 }
454
455 static void check_ok(int status, int line, const char *func)
456 {
457         char buf[256];
458
459         if (status) {
460                 crypt_get_error(buf, sizeof(buf));
461                 printf("FAIL line %d [%s]: code %d, %s\n", line, func, status, buf);
462                 _cleanup();
463                 exit(-1);
464         }
465 }
466
467 static void check_ko(int status, int line, const char *func)
468 {
469         char buf[256];
470
471         memset(buf, 0, sizeof(buf));
472         crypt_get_error(buf, sizeof(buf));
473         if (status >= 0) {
474                 printf("FAIL line %d [%s]: code %d, %s\n", line, func, status, buf);
475                 _cleanup();
476                 exit(-1);
477         } else if (_verbose)
478                 printf("   => errno %d, errmsg: %s\n", status, buf);
479 }
480
481 static void check_equal(int line, const char *func, int64_t x, int64_t y)
482 {
483         printf("FAIL line %d [%s]: expected equal values differs: %"
484                 PRIi64 " != %" PRIi64 "\n", line, func, x, y);
485         _cleanup();
486         exit(-1);
487 }
488
489 static void xlog(const char *msg, const char *tst, const char *func, int line, const char *txt)
490 {
491         if (_verbose) {
492                 if (txt)
493                         printf(" [%s,%s:%d] %s [%s]\n", msg, func, line, tst, txt);
494                 else
495                         printf(" [%s,%s:%d] %s\n", msg, func, line, tst);
496         }
497 }
498
499 /* crypt_device context must be "cd" to parse error properly here */
500 #define OK_(x)          do { xlog("(success)", #x, __FUNCTION__, __LINE__, NULL); \
501                              check_ok((x), __LINE__, __FUNCTION__); \
502                         } while(0)
503 #define FAIL_(x, y)     do { xlog("(fail)   ", #x, __FUNCTION__, __LINE__, y); \
504                              check_ko((x), __LINE__, __FUNCTION__); \
505                         } while(0)
506 #define EQ_(x, y)       do { xlog("(equal)  ", #x " == " #y, __FUNCTION__, __LINE__, NULL); \
507                              int64_t _x = (x), _y = (y); \
508                              if (_x != _y) check_equal(__LINE__, __FUNCTION__, _x, _y); \
509                         } while(0)
510 #define RUN_(x, y)              do { printf("%s: %s\n", #x, (y)); x(); } while (0)
511
512 static void AddDevicePlain(void)
513 {
514         struct crypt_device *cd;
515         struct crypt_params_plain params = {
516                 .hash = "sha1",
517                 .skip = 0,
518                 .offset = 0,
519                 .size = 0
520         };
521         int fd;
522         char key[128], key2[128], path[128];
523
524         const char *passphrase = PASSPHRASE;
525         // hashed hex version of PASSPHRASE
526         const char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
527         size_t key_size = strlen(mk_hex) / 2;
528         const char *cipher = "aes";
529         const char *cipher_mode = "cbc-essiv:sha256";
530
531         uint64_t size, r_size;
532
533         crypt_decode_key(key, mk_hex, key_size);
534         FAIL_(crypt_init(&cd, ""), "empty device string");
535         FAIL_(crypt_init(&cd, DEVICE_WRONG), "nonexistent device name ");
536         FAIL_(crypt_init(&cd, DEVICE_CHAR), "character device as backing device");
537         OK_(crypt_init(&cd, tmp_file_1));
538         crypt_free(cd);
539
540         // test crypt_format, crypt_get_cipher, crypt_get_cipher_mode, crypt_get_volume_key_size
541         OK_(crypt_init(&cd,DEVICE_1));
542         params.skip = 3;
543         params.offset = 42;
544         FAIL_(crypt_format(cd,CRYPT_PLAIN,NULL,cipher_mode,NULL,NULL,key_size,&params),"cipher param is null");
545         FAIL_(crypt_format(cd,CRYPT_PLAIN,cipher,NULL,NULL,NULL,key_size,&params),"cipher_mode param is null");
546         OK_(crypt_format(cd,CRYPT_PLAIN,cipher,cipher_mode,NULL,NULL,key_size,&params));
547         OK_(strcmp(cipher_mode,crypt_get_cipher_mode(cd)));
548         OK_(strcmp(cipher,crypt_get_cipher(cd)));
549         EQ_((int)key_size, crypt_get_volume_key_size(cd));
550         EQ_(params.skip, crypt_get_iv_offset(cd));
551         EQ_(params.offset, crypt_get_data_offset(cd));
552         params.skip = 0;
553         params.offset = 0;
554
555         // crypt_set_uuid()
556         FAIL_(crypt_set_uuid(cd,DEVICE_1_UUID),"can't set uuid to plain device");
557
558         crypt_free(cd);
559
560         // default is "plain" hash - no password hash
561         OK_(crypt_init(&cd, DEVICE_1));
562         OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, NULL));
563         FAIL_(crypt_activate_by_volume_key(cd, NULL, key, key_size, 0), "cannot verify key with plain");
564         OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
565         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
566         OK_(crypt_deactivate(cd, CDEVICE_1));
567         crypt_free(cd);
568
569         // test boundaries in offset parameter
570         device_size(DEVICE_1,&size);
571         params.hash = NULL;
572         // zero sectors length
573         params.offset = size >> SECTOR_SHIFT;
574         OK_(crypt_init(&cd, DEVICE_1));
575         OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, &params));
576         EQ_(crypt_get_data_offset(cd),params.offset);
577         // device size is 0 sectors
578         FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0), "invalid device size (0 blocks)");
579         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
580         // data part of crypt device is of 1 sector size
581         params.offset = (size >> SECTOR_SHIFT) - 1;
582         crypt_free(cd);
583
584         OK_(crypt_init(&cd, DEVICE_1));
585         OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, &params));
586         OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
587         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
588         snprintf(path, sizeof(path), "%s/%s", crypt_get_dir(), CDEVICE_1);
589         if (device_size(path, &r_size) >= 0)
590                 EQ_(r_size>>SECTOR_SHIFT, 1);
591         OK_(crypt_deactivate(cd, CDEVICE_1));
592         crypt_free(cd);
593
594         // size > device_size
595         params.offset = 0;
596         params.size = (size >> SECTOR_SHIFT) + 1;
597         crypt_init(&cd, DEVICE_1);
598         OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, &params));
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);
601         crypt_free(cd);
602
603         // offset == device_size (autodetect size)
604         params.offset = (size >> SECTOR_SHIFT);
605         params.size = 0;
606         crypt_init(&cd, DEVICE_1);
607         OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, &params));
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);
610         crypt_free(cd);
611
612         // offset == device_size (user defined size)
613         params.offset = (size >> SECTOR_SHIFT);
614         params.size = 123;
615         crypt_init(&cd, DEVICE_1);
616         OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, &params));
617         FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0),"Device too small");
618         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
619         crypt_free(cd);
620
621         // offset+size > device_size
622         params.offset = 42;
623         params.size = (size >> SECTOR_SHIFT) - params.offset + 1;
624         crypt_init(&cd, DEVICE_1);
625         OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, &params));
626         FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0),"Offset and size are beyond device real size");
627         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
628         crypt_free(cd);
629
630         // offset+size == device_size
631         params.offset = 42;
632         params.size = (size >> SECTOR_SHIFT) - params.offset;
633         crypt_init(&cd, DEVICE_1);
634         OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, &params));
635         OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
636         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
637         if (!device_size(path, &r_size))
638                 EQ_((r_size >> SECTOR_SHIFT),params.size);
639         OK_(crypt_deactivate(cd,CDEVICE_1));
640
641         crypt_free(cd);
642         params.hash = "sha1";
643         params.offset = 0;
644         params.size = 0;
645         params.skip = 0;
646
647         // Now use hashed password
648         OK_(crypt_init(&cd, DEVICE_1));
649         OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, &params));
650         FAIL_(crypt_activate_by_passphrase(cd, NULL, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0),
651               "cannot verify passphrase with plain" );
652         OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
653
654         // device status check
655         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
656         snprintf(path, sizeof(path), "%s/%s", crypt_get_dir(), CDEVICE_1);
657         fd = open(path, O_RDONLY);
658         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_BUSY);
659         FAIL_(crypt_deactivate(cd, CDEVICE_1), "Device is busy");
660         close(fd);
661         OK_(crypt_deactivate(cd, CDEVICE_1));
662         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
663         crypt_free(cd);
664
665         // crypt_init_by_name_and_header
666         OK_(crypt_init(&cd,DEVICE_1));
667         OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, &params));
668         OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
669         crypt_free(cd);
670
671         FAIL_(crypt_init_by_name_and_header(&cd, CDEVICE_1, H_DEVICE),"can't init plain device by header device");
672         OK_(crypt_init_by_name(&cd, CDEVICE_1));
673         OK_(strcmp(cipher_mode,crypt_get_cipher_mode(cd)));
674         OK_(strcmp(cipher,crypt_get_cipher(cd)));
675         EQ_((int)key_size, crypt_get_volume_key_size(cd));
676         EQ_(params.skip, crypt_get_iv_offset(cd));
677         EQ_(params.offset, crypt_get_data_offset(cd));
678         OK_(crypt_deactivate(cd, CDEVICE_1));
679         crypt_free(cd);
680
681         OK_(crypt_init(&cd,DEVICE_1));
682         OK_(crypt_format(cd,CRYPT_PLAIN,cipher,cipher_mode,NULL,NULL,key_size,&params));
683         params.size = 0;
684         params.offset = 0;
685
686         // crypt_set_data_device
687         FAIL_(crypt_set_data_device(cd,H_DEVICE),"can't set data device for plain device");
688
689         // crypt_get_type
690         OK_(strcmp(crypt_get_type(cd),CRYPT_PLAIN));
691
692         OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
693         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
694
695         // crypt_resize()
696         OK_(crypt_resize(cd,CDEVICE_1,size>>SECTOR_SHIFT)); // same size
697         if (!device_size(path,&r_size))
698                 EQ_(r_size, size);
699
700         // size overlaps
701         FAIL_(crypt_resize(cd, CDEVICE_1, ULLONG_MAX),"Backing device is too small");
702         FAIL_(crypt_resize(cd, CDEVICE_1, (size>>SECTOR_SHIFT)+1),"crypt device overlaps backing device");
703
704         // resize ok
705         OK_(crypt_resize(cd,CDEVICE_1, 123));
706         if (!device_size(path,&r_size))
707                 EQ_(r_size>>SECTOR_SHIFT, 123);
708         OK_(crypt_resize(cd,CDEVICE_1,0)); // full size (autodetect)
709         if (!device_size(path,&r_size))
710                 EQ_(r_size, size);
711         OK_(crypt_deactivate(cd,CDEVICE_1));
712         EQ_(crypt_status(cd,CDEVICE_1),CRYPT_INACTIVE);
713         crypt_free(cd);
714
715         // offset tests
716         OK_(crypt_init(&cd,DEVICE_1));
717         params.offset = 42;
718         params.size = (size>>SECTOR_SHIFT) - params.offset - 10;
719         OK_(crypt_format(cd,CRYPT_PLAIN,cipher,cipher_mode,NULL,NULL,key_size,&params));
720         OK_(crypt_activate_by_volume_key(cd,CDEVICE_1,key,key_size,0));
721         if (!device_size(path,&r_size))
722                 EQ_(r_size>>SECTOR_SHIFT, params.size);
723         // resize to fill remaining capacity
724         OK_(crypt_resize(cd,CDEVICE_1,params.size + 10));
725         if (!device_size(path,&r_size))
726                 EQ_(r_size>>SECTOR_SHIFT, params.size + 10);
727
728         // 1 sector beyond real size
729         FAIL_(crypt_resize(cd,CDEVICE_1,params.size + 11), "new device size overlaps backing device"); // with respect to offset
730         if (!device_size(path,&r_size))
731                 EQ_(r_size>>SECTOR_SHIFT, params.size + 10);
732         EQ_(crypt_status(cd,CDEVICE_1),CRYPT_ACTIVE);
733         fd = open(path, O_RDONLY);
734         close(fd);
735         OK_(fd < 0);
736
737         // resize to minimal size
738         OK_(crypt_resize(cd,CDEVICE_1, 1)); // minimal device size
739         if (!device_size(path,&r_size))
740                 EQ_(r_size>>SECTOR_SHIFT, 1);
741         // use size of backing device (autodetect with respect to offset)
742         OK_(crypt_resize(cd,CDEVICE_1,0));
743         if (!device_size(path,&r_size))
744                 EQ_(r_size>>SECTOR_SHIFT, (size >> SECTOR_SHIFT)- 42);
745         OK_(crypt_deactivate(cd,CDEVICE_1));
746         crypt_free(cd);
747
748         params.size = 0;
749         params.offset = 0;
750         OK_(crypt_init(&cd,DEVICE_1));
751         OK_(crypt_format(cd,CRYPT_PLAIN,cipher,cipher_mode,NULL,NULL,key_size,&params));
752         OK_(crypt_activate_by_volume_key(cd,CDEVICE_1,key,key_size,0));
753
754         // suspend/resume tests
755         FAIL_(crypt_suspend(cd,CDEVICE_1),"cannot suspend plain device");
756         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
757         FAIL_(crypt_resume_by_passphrase(cd,CDEVICE_1,CRYPT_ANY_SLOT,passphrase, strlen(passphrase)),"cannot resume plain device");
758         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
759
760         // retrieve volume key check
761         memset(key2, 0, key_size);
762         key_size--;
763         // small buffer
764         FAIL_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)), "small buffer");
765         key_size++;
766         OK_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)));
767
768         OK_(memcmp(key, key2, key_size));
769         OK_(strcmp(cipher, crypt_get_cipher(cd)));
770         OK_(strcmp(cipher_mode, crypt_get_cipher_mode(cd)));
771         EQ_((int)key_size, crypt_get_volume_key_size(cd));
772         EQ_(0, crypt_get_data_offset(cd));
773         OK_(crypt_deactivate(cd, CDEVICE_1));
774
775         // now with keyfile
776         OK_(_prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
777         OK_(_prepare_keyfile(KEYFILE2, KEY2, strlen(KEY2)));
778         FAIL_(crypt_activate_by_keyfile(cd, NULL, CRYPT_ANY_SLOT, KEYFILE1, 0, 0), "cannot verify key with plain");
779         EQ_(0, crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
780         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
781         OK_(crypt_deactivate(cd, CDEVICE_1));
782         FAIL_(crypt_activate_by_keyfile_offset(cd, NULL, CRYPT_ANY_SLOT, KEYFILE1, 0, strlen(KEY1) + 1, 0), "cannot seek");
783         EQ_(0, crypt_activate_by_keyfile_offset(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0, 0));
784         OK_(crypt_deactivate(cd, CDEVICE_1));
785         _remove_keyfiles();
786         crypt_free(cd);
787
788         OK_(crypt_init(&cd,DEVICE_1));
789         OK_(crypt_format(cd,CRYPT_PLAIN,cipher,cipher_mode,NULL,NULL,key_size,&params));
790
791         // crypt_keyslot_*()
792         FAIL_(crypt_keyslot_add_by_passphrase(cd,CRYPT_ANY_SLOT,passphrase,strlen(passphrase),passphrase,strlen(passphrase)), "can't add keyslot to plain device");
793         FAIL_(crypt_keyslot_add_by_volume_key(cd,CRYPT_ANY_SLOT ,key,key_size,passphrase,strlen(passphrase)),"can't add keyslot to plain device");
794         FAIL_(crypt_keyslot_add_by_keyfile(cd,CRYPT_ANY_SLOT,KEYFILE1,strlen(KEY1),KEYFILE2,strlen(KEY2)),"can't add keyslot to plain device");
795         FAIL_(crypt_keyslot_destroy(cd,1),"can't manipulate keyslots on plain device");
796         EQ_(crypt_keyslot_status(cd, 0), CRYPT_SLOT_INVALID);
797         _remove_keyfiles();
798
799         crypt_free(cd);
800 }
801
802 #define CALLBACK_ERROR "calback_error xyz"
803 static int pass_callback_err(const char *msg, char *buf, size_t length, void *usrptr)
804 {
805         struct crypt_device *cd = usrptr;
806
807         assert(cd);
808         assert(length);
809         assert(msg);
810
811         crypt_log(cd, CRYPT_LOG_ERROR, CALLBACK_ERROR);
812         return -EINVAL;
813 }
814
815 static int pass_callback_ok(const char *msg, char *buf, size_t length, void *usrptr)
816 {
817         assert(length);
818         assert(msg);
819         strcpy(buf, PASSPHRASE);
820         return strlen(buf);
821 }
822
823 static void CallbacksTest(void)
824 {
825         struct crypt_device *cd;
826         struct crypt_params_plain params = {
827                 .hash = "sha1",
828                 .skip = 0,
829                 .offset = 0,
830         };
831
832         size_t key_size = 256 / 8;
833         const char *cipher = "aes";
834         const char *cipher_mode = "cbc-essiv:sha256";
835         const char *passphrase = PASSPHRASE;
836         char buf1[256] = {0}, buf2[256] = {0};
837
838         OK_(crypt_init(&cd, DEVICE_1));
839         crypt_set_log_callback(cd, &new_log, NULL);
840         //crypt_set_log_callback(cd, NULL, NULL);
841
842         OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, &params));
843
844         OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
845         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
846         OK_(crypt_deactivate(cd, CDEVICE_1));
847
848         reset_log();
849         crypt_set_password_callback(cd, pass_callback_err, cd);
850         FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, NULL, 0, 0), "callback fails");
851         EQ_(strncmp(global_log, CALLBACK_ERROR, strlen(CALLBACK_ERROR)), 0);
852
853         crypt_set_password_callback(cd, pass_callback_ok, NULL);
854         OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, NULL, 0, 0));
855         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
856         OK_(crypt_deactivate(cd, CDEVICE_1));
857
858         // Check error reporting.
859         // This must fail and create error message
860         crypt_deactivate(cd, CDEVICE_1);
861
862         // Here context must be the same
863         crypt_get_error(buf1, sizeof(buf1));
864         crypt_last_error(cd, buf2, sizeof(buf2));
865         OK_(!*buf1);
866         OK_(!*buf2);
867         OK_(strcmp(buf1, buf2));
868
869         crypt_get_error(buf1, sizeof(buf1));
870         crypt_last_error(cd, buf2, sizeof(buf2));
871         OK_(*buf1);
872         OK_(*buf2);
873
874         crypt_free(cd);
875 }
876
877 static void UseLuksDevice(void)
878 {
879         struct crypt_device *cd;
880         char key[128];
881         size_t key_size;
882
883         OK_(crypt_init(&cd, DEVICE_1));
884         OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
885         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
886         OK_(crypt_activate_by_passphrase(cd, NULL, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0));
887         OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0));
888         FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0), "already open");
889         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
890         OK_(crypt_deactivate(cd, CDEVICE_1));
891         FAIL_(crypt_deactivate(cd, CDEVICE_1), "no such device");
892
893         key_size = 16;
894         OK_(strcmp("aes", crypt_get_cipher(cd)));
895         OK_(strcmp("cbc-essiv:sha256", crypt_get_cipher_mode(cd)));
896         OK_(strcmp(DEVICE_1_UUID, crypt_get_uuid(cd)));
897         EQ_((int)key_size, crypt_get_volume_key_size(cd));
898         EQ_(1032, crypt_get_data_offset(cd));
899
900         EQ_(0, crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key, &key_size, KEY1, strlen(KEY1)));
901         OK_(crypt_volume_key_verify(cd, key, key_size));
902         OK_(crypt_activate_by_volume_key(cd, NULL, key, key_size, 0));
903         OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
904         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
905         OK_(crypt_deactivate(cd, CDEVICE_1));
906
907         key[1] = ~key[1];
908         FAIL_(crypt_volume_key_verify(cd, key, key_size), "key mismatch");
909         FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0), "key mismatch");
910         crypt_free(cd);
911 }
912
913 static void SuspendDevice(void)
914 {
915         int suspend_status;
916         struct crypt_device *cd;
917
918         OK_(crypt_init(&cd, DEVICE_1));
919         OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
920         OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0));
921
922         suspend_status = crypt_suspend(cd, CDEVICE_1);
923         if (suspend_status == -ENOTSUP) {
924                 printf("WARNING: Suspend/Resume not supported, skipping test.\n");
925                 goto out;
926         }
927         OK_(suspend_status);
928         FAIL_(crypt_suspend(cd, CDEVICE_1), "already suspended");
929
930         FAIL_(crypt_resume_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1)-1), "wrong key");
931         OK_(crypt_resume_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1)));
932         FAIL_(crypt_resume_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1)), "not suspended");
933
934         OK_(_prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
935         OK_(crypt_suspend(cd, CDEVICE_1));
936         FAIL_(crypt_resume_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1 "blah", 0), "wrong keyfile");
937         FAIL_(crypt_resume_by_keyfile_offset(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 1, 0), "wrong key");
938         OK_(crypt_resume_by_keyfile_offset(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
939         FAIL_(crypt_resume_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0), "not suspended");
940         _remove_keyfiles();
941 out:
942         OK_(crypt_deactivate(cd, CDEVICE_1));
943         crypt_free(cd);
944 }
945
946 static void AddDeviceLuks(void)
947 {
948         struct crypt_device *cd;
949         struct crypt_params_luks1 params = {
950                 .hash = "sha512",
951                 .data_alignment = 2048, // 4M, data offset will be 4096
952                 .data_device = DEVICE_2
953         };
954         char key[128], key2[128];
955
956         const char *passphrase = "blabla";
957         const char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
958         size_t key_size = strlen(mk_hex) / 2;
959         const char *cipher = "aes";
960         const char *cipher_mode = "cbc-essiv:sha256";
961         uint64_t r_payload_offset, r_header_size, r_size_1;
962
963         crypt_decode_key(key, mk_hex, key_size);
964
965         // init test devices
966         OK_(get_luks_offsets(1, key_size, 0, 0, &r_header_size, &r_payload_offset));
967         OK_(create_dmdevice_over_loop(H_DEVICE, r_header_size));
968         OK_(create_dmdevice_over_loop(H_DEVICE_WRONG, r_header_size - 1));
969
970         // format
971         OK_(crypt_init(&cd, DMDIR H_DEVICE_WRONG));
972         params.data_alignment = 0;
973         FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params), "Not enough space for keyslots material");
974         crypt_free(cd);
975
976         // test payload_offset = 0 for encrypted device with external header device
977         OK_(crypt_init(&cd, DMDIR H_DEVICE));
978         OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params));
979         EQ_(crypt_get_data_offset(cd), 0);
980         crypt_free(cd);
981
982         params.data_alignment = 0;
983         params.data_device = NULL;
984
985         // test payload_offset = 0. format() should look up alignment offset from device topology
986         OK_(crypt_init(&cd, DEVICE_2));
987         OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params));
988         OK_(!(crypt_get_data_offset(cd) > 0));
989         crypt_free(cd);
990
991         /*
992          * test limit values for backing device size
993          */
994         params.data_alignment = 4096;
995         OK_(get_luks_offsets(0, key_size, params.data_alignment, 0, NULL, &r_payload_offset));
996         OK_(create_dmdevice_over_loop(L_DEVICE_0S, r_payload_offset));
997         OK_(create_dmdevice_over_loop(L_DEVICE_1S, r_payload_offset + 1));
998         //OK_(create_dmdevice_over_loop(L_DEVICE_WRONG, r_payload_offset - 1));
999         OK_(create_dmdevice_over_loop(L_DEVICE_WRONG, 2050 - 1)); //FIXME last keyslot - 1 sector
1000
1001         // 1 sector less than required
1002         OK_(crypt_init(&cd, DMDIR L_DEVICE_WRONG));
1003         FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params), "Device too small");
1004         crypt_free(cd);
1005
1006         // 0 sectors for encrypted area
1007         OK_(crypt_init(&cd, DMDIR L_DEVICE_0S));
1008         OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params));
1009         FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0), "Encrypted area too small");
1010         crypt_free(cd);
1011
1012         // 1 sector for encrypted area
1013         OK_(crypt_init(&cd, DMDIR L_DEVICE_1S));
1014         OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params));
1015         EQ_(crypt_get_data_offset(cd), params.data_alignment);
1016         OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1017         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1018         OK_(device_size(DMDIR CDEVICE_1, &r_size_1));
1019         EQ_(r_size_1, SECTOR_SIZE);
1020         OK_(crypt_deactivate(cd, CDEVICE_1));
1021         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
1022         // restrict format only to empty context
1023         FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params), "Context is already formated");
1024         FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, NULL), "Context is already formated");
1025         // change data device to wrong one
1026         OK_(crypt_set_data_device(cd, DMDIR L_DEVICE_0S));
1027         FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0), "Device too small");
1028         OK_(crypt_set_data_device(cd, DMDIR L_DEVICE_1S));
1029         OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1030         OK_(crypt_deactivate(cd, CDEVICE_1));
1031         crypt_free(cd);
1032
1033         params.data_alignment = 0;
1034         params.data_device = DEVICE_2;
1035
1036         // generate keyslot material at the end of luks header
1037         OK_(crypt_init(&cd, DMDIR H_DEVICE));
1038         OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params));
1039         EQ_(crypt_keyslot_add_by_volume_key(cd, 7, key, key_size, passphrase, strlen(passphrase)), 7);
1040         EQ_(crypt_activate_by_passphrase(cd, CDEVICE_1, 7, passphrase, strlen(passphrase) ,0), 7);
1041         crypt_free(cd);
1042         OK_(crypt_init_by_name_and_header(&cd, CDEVICE_1, DMDIR H_DEVICE));
1043         FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params), "Context is already formated");
1044         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1045         OK_(crypt_deactivate(cd, CDEVICE_1));
1046         crypt_free(cd);
1047
1048         params.data_alignment = 2048;
1049         params.data_device = NULL;
1050
1051         // test uuid mismatch and _init_by_name_and_header
1052         OK_(crypt_init(&cd, DMDIR L_DEVICE_1S));
1053         OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params));
1054         OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1055         crypt_free(cd);
1056         params.data_alignment = 0;
1057         params.data_device = DEVICE_2;
1058         OK_(crypt_init(&cd, DMDIR H_DEVICE));
1059         OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params));
1060         crypt_free(cd);
1061         // there we've got uuid mismatch
1062         OK_(crypt_init_by_name_and_header(&cd, CDEVICE_1, DMDIR H_DEVICE));
1063         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1064         OK_(!!crypt_get_type(cd));
1065         FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0), "Device is active");
1066         FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, key, key_size, 0), "Device is active");
1067         EQ_(crypt_status(cd, CDEVICE_2), CRYPT_INACTIVE);
1068         OK_(crypt_deactivate(cd, CDEVICE_1));
1069         crypt_free(cd);
1070
1071         params.data_device = NULL;
1072
1073         OK_(crypt_init(&cd, DEVICE_2));
1074         OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params));
1075
1076         // even with no keyslots defined it can be activated by volume key
1077         OK_(crypt_volume_key_verify(cd, key, key_size));
1078         OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, key, key_size, 0));
1079         EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
1080         OK_(crypt_deactivate(cd, CDEVICE_2));
1081
1082         // now with keyslot
1083         EQ_(7, crypt_keyslot_add_by_volume_key(cd, 7, key, key_size, passphrase, strlen(passphrase)));
1084         EQ_(CRYPT_SLOT_ACTIVE_LAST, crypt_keyslot_status(cd, 7));
1085         EQ_(7, crypt_activate_by_passphrase(cd, CDEVICE_2, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
1086         EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
1087         OK_(crypt_deactivate(cd, CDEVICE_2));
1088
1089         crypt_set_iteration_time(cd, 1);
1090         EQ_(1, crypt_keyslot_add_by_volume_key(cd, 1, key, key_size, KEY1, strlen(KEY1)));
1091         OK_(_prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
1092         OK_(_prepare_keyfile(KEYFILE2, KEY2, strlen(KEY2)));
1093         EQ_(2, crypt_keyslot_add_by_keyfile(cd, 2, KEYFILE1, 0, KEYFILE2, 0));
1094         FAIL_(crypt_keyslot_add_by_keyfile_offset(cd, 3, KEYFILE1, 0, 1, KEYFILE2, 0, 1), "wrong key");
1095         EQ_(3, crypt_keyslot_add_by_keyfile_offset(cd, 3, KEYFILE1, 0, 0, KEYFILE2, 0, 1));
1096         EQ_(4, crypt_keyslot_add_by_keyfile_offset(cd, 4, KEYFILE2, 0, 1, KEYFILE1, 0, 1));
1097         FAIL_(crypt_activate_by_keyfile(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, strlen(KEY2)-1, 0), "key mismatch");
1098         EQ_(2, crypt_activate_by_keyfile(cd, NULL, CRYPT_ANY_SLOT, KEYFILE2, 0, 0));
1099         EQ_(3, crypt_activate_by_keyfile_offset(cd, NULL, CRYPT_ANY_SLOT, KEYFILE2, 0, 1, 0));
1100         EQ_(4, crypt_activate_by_keyfile_offset(cd, NULL, CRYPT_ANY_SLOT, KEYFILE1, 0, 1, 0));
1101         FAIL_(crypt_activate_by_keyfile_offset(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, strlen(KEY2), 2, 0), "not enough data");
1102         FAIL_(crypt_activate_by_keyfile_offset(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, 0, strlen(KEY2) + 1, 0), "cannot seek");
1103         FAIL_(crypt_activate_by_keyfile_offset(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, 0, 2, 0), "wrong key");
1104         EQ_(2, crypt_activate_by_keyfile(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, 0, 0));
1105         OK_(crypt_keyslot_destroy(cd, 1));
1106         OK_(crypt_keyslot_destroy(cd, 2));
1107         OK_(crypt_keyslot_destroy(cd, 3));
1108         OK_(crypt_keyslot_destroy(cd, 4));
1109         OK_(crypt_deactivate(cd, CDEVICE_2));
1110         _remove_keyfiles();
1111
1112         FAIL_(crypt_keyslot_add_by_volume_key(cd, 7, key, key_size, passphrase, strlen(passphrase)), "slot used");
1113         key[1] = ~key[1];
1114         FAIL_(crypt_keyslot_add_by_volume_key(cd, 6, key, key_size, passphrase, strlen(passphrase)), "key mismatch");
1115         key[1] = ~key[1];
1116         EQ_(6, crypt_keyslot_add_by_volume_key(cd, 6, key, key_size, passphrase, strlen(passphrase)));
1117         EQ_(CRYPT_SLOT_ACTIVE, crypt_keyslot_status(cd, 6));
1118
1119         FAIL_(crypt_keyslot_destroy(cd, 8), "invalid keyslot");
1120         FAIL_(crypt_keyslot_destroy(cd, CRYPT_ANY_SLOT), "invalid keyslot");
1121         FAIL_(crypt_keyslot_destroy(cd, 0), "keyslot not used");
1122         OK_(crypt_keyslot_destroy(cd, 7));
1123         EQ_(CRYPT_SLOT_INACTIVE, crypt_keyslot_status(cd, 7));
1124         EQ_(CRYPT_SLOT_ACTIVE_LAST, crypt_keyslot_status(cd, 6));
1125
1126         EQ_(6, crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)));
1127         OK_(crypt_volume_key_verify(cd, key2, key_size));
1128
1129         OK_(memcmp(key, key2, key_size));
1130         OK_(strcmp(cipher, crypt_get_cipher(cd)));
1131         OK_(strcmp(cipher_mode, crypt_get_cipher_mode(cd)));
1132         EQ_((int)key_size, crypt_get_volume_key_size(cd));
1133         EQ_(4096, crypt_get_data_offset(cd));
1134         OK_(strcmp(DEVICE_2, crypt_get_device_name(cd)));
1135
1136         reset_log();
1137         crypt_set_log_callback(cd, &new_log, NULL);
1138         OK_(crypt_dump(cd));
1139         OK_(!(global_lines != 0));
1140         crypt_set_log_callback(cd, NULL, NULL);
1141         reset_log();
1142
1143         FAIL_(crypt_set_uuid(cd, "blah"), "wrong UUID format");
1144         OK_(crypt_set_uuid(cd, DEVICE_TEST_UUID));
1145         OK_(strcmp(DEVICE_TEST_UUID, crypt_get_uuid(cd)));
1146
1147         FAIL_(crypt_deactivate(cd, CDEVICE_2), "not active");
1148         crypt_free(cd);
1149         _cleanup_dmdevices();
1150 }
1151
1152 static void UseTempVolumes(void)
1153 {
1154         struct crypt_device *cd;
1155         char tmp[256];
1156
1157         // Tepmporary device without keyslot but with on-disk LUKS header
1158         OK_(crypt_init(&cd, DEVICE_2));
1159         FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "not yet formatted");
1160         OK_(crypt_format(cd, CRYPT_LUKS1, "aes", "cbc-essiv:sha256", NULL, NULL, 16, NULL));
1161         OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0));
1162         EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
1163         crypt_free(cd);
1164
1165         OK_(crypt_init_by_name(&cd, CDEVICE_2));
1166         OK_(crypt_deactivate(cd, CDEVICE_2));
1167         crypt_free(cd);
1168
1169         // Dirty checks: device without UUID
1170         // we should be able to remove it but not manuipulate with it
1171         snprintf(tmp, sizeof(tmp), "dmsetup create %s --table \""
1172                 "0 100 crypt aes-cbc-essiv:sha256 deadbabedeadbabedeadbabedeadbabe 0 "
1173                 "%s 2048\"", CDEVICE_2, DEVICE_2);
1174         _system(tmp, 1);
1175         OK_(crypt_init_by_name(&cd, CDEVICE_2));
1176         OK_(crypt_deactivate(cd, CDEVICE_2));
1177         FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "No known device type");
1178         crypt_free(cd);
1179
1180         // Dirty checks: device with UUID but LUKS header key fingerprint must fail)
1181         snprintf(tmp, sizeof(tmp), "dmsetup create %s --table \""
1182                 "0 100 crypt aes-cbc-essiv:sha256 deadbabedeadbabedeadbabedeadbabe 0 "
1183                 "%s 2048\" -u CRYPT-LUKS1-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-ctest1",
1184                  CDEVICE_2, DEVICE_2);
1185         _system(tmp, 1);
1186         OK_(crypt_init_by_name(&cd, CDEVICE_2));
1187         OK_(crypt_deactivate(cd, CDEVICE_2));
1188         FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "wrong volume key");
1189         crypt_free(cd);
1190
1191         // No slots
1192         OK_(crypt_init(&cd, DEVICE_2));
1193         OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
1194         FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "volume key is lost");
1195         crypt_free(cd);
1196
1197         // Plain device
1198         OK_(crypt_init(&cd, DEVICE_2));
1199         OK_(crypt_format(cd, CRYPT_PLAIN, "aes", "cbc-essiv:sha256", NULL, NULL, 16, NULL));
1200         FAIL_(crypt_activate_by_volume_key(cd, NULL, "xxx", 3, 0), "cannot verify key with plain");
1201         FAIL_(crypt_volume_key_verify(cd, "xxx", 3), "cannot verify key with plain");
1202         FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, "xxx", 3, 0), "wrong key lenght");
1203         OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, "volumekeyvolumek", 16, 0));
1204         EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
1205         OK_(crypt_deactivate(cd, CDEVICE_2));
1206         crypt_free(cd);
1207 }
1208
1209 static void LuksHeaderRestore(void)
1210 {
1211         struct crypt_device *cd;
1212         struct crypt_params_luks1 params = {
1213                 .hash = "sha512",
1214                 .data_alignment = 2048, // 4M, data offset will be 4096
1215         };
1216         struct crypt_params_plain pl_params = {
1217                 .hash = "sha1",
1218                 .skip = 0,
1219                 .offset = 0,
1220                 .size = 0
1221         };
1222         char key[128], key2[128], cmd[256];
1223
1224         const char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
1225         size_t key_size = strlen(mk_hex) / 2;
1226         const char *cipher = "aes";
1227         const char *cipher_mode = "cbc-essiv:sha256";
1228         uint64_t r_payload_offset;
1229
1230         crypt_decode_key(key, mk_hex, key_size);
1231
1232         OK_(get_luks_offsets(0, key_size, params.data_alignment, 0, NULL, &r_payload_offset));
1233         OK_(create_dmdevice_over_loop(L_DEVICE_OK, r_payload_offset + 5000));
1234
1235         // do not restore header over plain device
1236         OK_(crypt_init(&cd, DMDIR L_DEVICE_OK));
1237         OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, &pl_params));
1238         OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1239         FAIL_(crypt_header_restore(cd, CRYPT_PLAIN, VALID_HEADER), "Cannot restore header to PLAIN type device");
1240         FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, VALID_HEADER), "Cannot restore header over PLAIN type device");
1241         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1242         OK_(crypt_deactivate(cd, CDEVICE_1));
1243         crypt_free(cd);
1244
1245         // invalid headers
1246         OK_(crypt_init(&cd, DMDIR L_DEVICE_OK));
1247         OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params));
1248         FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, EVL_HEADER_1), "Header corrupted");
1249         FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, EVL_HEADER_2), "Header corrupted");
1250         FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, EVL_HEADER_3), "Header corrupted");
1251         FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, EVL_HEADER_4), "Header too small");
1252         OK_(crypt_header_restore(cd, CRYPT_LUKS1, VALID_HEADER));
1253         // wipe valid luks header
1254         snprintf(cmd, sizeof(cmd), "dd if=/dev/zero of=" DMDIR L_DEVICE_OK " bs=512 count=%" PRIu64 " 2>/dev/null", r_payload_offset);
1255         OK_(_system(cmd, 1));
1256         FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, EVL_HEADER_1), "Header corrupted");
1257         FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, EVL_HEADER_2), "Header corrupted");
1258         FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, EVL_HEADER_3), "Header corrupted");
1259         FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, EVL_HEADER_4), "Header too small");
1260         OK_(crypt_header_restore(cd, CRYPT_LUKS1, VALID_HEADER));
1261         OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1262         OK_(crypt_deactivate(cd, CDEVICE_1));
1263         crypt_free(cd);
1264
1265         // volume key_size mismatch
1266         OK_(crypt_init(&cd, DMDIR L_DEVICE_OK));
1267         memcpy(key2, key, key_size - 1);
1268         OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key2, key_size - 1, &params));
1269         FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, VALID_HEADER), "Volume keysize mismatch");
1270         crypt_free(cd);
1271
1272         // payload offset mismatch
1273         params.data_alignment = 8192;
1274         OK_(crypt_init(&cd, DMDIR L_DEVICE_OK));
1275         OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params));
1276         FAIL_(crypt_header_restore(cd, CRYPT_LUKS1, VALID_HEADER), "Payload offset mismatch");
1277         //_system("dmsetup table;sleep 1",1);
1278         crypt_free(cd);
1279
1280         _cleanup_dmdevices();
1281 }
1282
1283 static void LuksHeaderLoad(void)
1284 {
1285         struct crypt_device *cd;
1286         struct crypt_params_luks1 params = {
1287                 .hash = "sha512",
1288                 .data_alignment = 2048,
1289         };
1290         struct crypt_params_plain pl_params = {
1291                 .hash = "sha1",
1292                 .skip = 0,
1293                 .offset = 0,
1294                 .size = 0
1295         };
1296         char key[128], cmd[256];
1297
1298         const char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
1299         size_t key_size = strlen(mk_hex) / 2;
1300         const char *cipher = "aes";
1301         const char *cipher_mode = "cbc-essiv:sha256";
1302         uint64_t r_payload_offset, r_header_size;
1303
1304         crypt_decode_key(key, mk_hex, key_size);
1305
1306         // prepare test env
1307         OK_(get_luks_offsets(0, key_size, params.data_alignment, 0, &r_header_size, &r_payload_offset));
1308         // external header device
1309         OK_(create_dmdevice_over_loop(H_DEVICE, r_header_size));
1310         // prepared header on a device too small to contain header and payload
1311         //OK_(create_dmdevice_over_loop(H_DEVICE_WRONG, r_payload_offset - 1));
1312         OK_(create_dmdevice_over_loop(H_DEVICE_WRONG, 2050 - 1)); //FIXME
1313         //snprintf(cmd, sizeof(cmd), "dd if=" EVL_HEADER_4 " of=" DMDIR H_DEVICE_WRONG " bs=512 count=%" PRIu64, r_payload_offset - 1);
1314         snprintf(cmd, sizeof(cmd), "dd if=" EVL_HEADER_4 " of=" DMDIR H_DEVICE_WRONG " bs=512 count=%d 2>/dev/null", 2050 - 1);
1315         OK_(_system(cmd, 1));
1316         // some device
1317         OK_(create_dmdevice_over_loop(L_DEVICE_OK, r_payload_offset + 1000));
1318         // 1 sector device
1319         OK_(create_dmdevice_over_loop(L_DEVICE_1S, r_payload_offset + 1));
1320         // 0 sectors device for payload
1321         OK_(create_dmdevice_over_loop(L_DEVICE_0S, r_payload_offset));
1322
1323         // valid metadata and device size
1324         params.data_alignment = 0;
1325         params.data_device = DMDIR L_DEVICE_OK;
1326         OK_(crypt_init(&cd, DMDIR H_DEVICE));
1327         OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params));
1328         crypt_free(cd);
1329         OK_(crypt_init(&cd, DMDIR H_DEVICE));
1330         OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
1331         OK_(crypt_set_data_device(cd, DMDIR L_DEVICE_OK));
1332         OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1333         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1334         OK_(crypt_deactivate(cd, CDEVICE_1));
1335         crypt_free(cd);
1336
1337         // bad header: device too small (payloadOffset > device_size)
1338         OK_(crypt_init(&cd, DMDIR H_DEVICE_WRONG));
1339         FAIL_(crypt_load(cd, CRYPT_LUKS1, NULL), "Device too small");
1340         OK_(!!crypt_get_type(cd));
1341         crypt_free(cd);
1342
1343         // 0 secs for encrypted data area
1344         params.data_alignment = 2048;
1345         params.data_device = NULL;
1346         OK_(crypt_init(&cd, DMDIR L_DEVICE_0S));
1347         OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params));
1348         crypt_free(cd);
1349         // load should be ok
1350         OK_(crypt_init(&cd, DMDIR L_DEVICE_0S));
1351         OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
1352         FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0), "Device too small");
1353         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
1354         crypt_free(cd);
1355
1356         // damaged header
1357         OK_(_system("dd if=/dev/zero of=" DMDIR L_DEVICE_OK " bs=512 count=8 2>/dev/null", 1));
1358         OK_(crypt_init(&cd, DMDIR L_DEVICE_OK));
1359         FAIL_(crypt_load(cd, CRYPT_LUKS1, NULL), "Header not found");
1360         crypt_free(cd);
1361
1362         // plain device
1363         OK_(crypt_init(&cd, DMDIR H_DEVICE));
1364         FAIL_(crypt_load(cd, CRYPT_PLAIN, NULL), "Can't load nonLUKS device type");
1365         crypt_free(cd);
1366         OK_(crypt_init(&cd, DMDIR H_DEVICE));
1367         OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, key, key_size, &pl_params));
1368         FAIL_(crypt_load(cd, CRYPT_LUKS1, NULL), "Can't load over nonLUKS device type");
1369         crypt_free(cd);
1370
1371         _cleanup_dmdevices();
1372 }
1373
1374 static void LuksHeaderBackup(void)
1375 {
1376         struct crypt_device *cd;
1377         struct crypt_params_luks1 params = {
1378                 .hash = "sha512",
1379                 .data_alignment = 2048,
1380         };
1381         char key[128];
1382
1383         const char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
1384         size_t key_size = strlen(mk_hex) / 2;
1385         const char *cipher = "aes";
1386         const char *cipher_mode = "cbc-essiv:sha256";
1387         uint64_t r_payload_offset;
1388
1389         crypt_decode_key(key, mk_hex, key_size);
1390
1391         OK_(get_luks_offsets(0, key_size, params.data_alignment, 0, NULL, &r_payload_offset));
1392         OK_(create_dmdevice_over_loop(L_DEVICE_OK, r_payload_offset + 1));
1393
1394         // create LUKS device and backup the header
1395         OK_(crypt_init(&cd, DMDIR L_DEVICE_OK));
1396         OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params));
1397         OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1398         OK_(crypt_header_backup(cd, CRYPT_LUKS1, BACKUP_FILE));
1399         OK_(crypt_deactivate(cd, CDEVICE_1));
1400         crypt_free(cd);
1401
1402         // restore header from backup
1403         OK_(crypt_init(&cd, DMDIR L_DEVICE_OK));
1404         OK_(crypt_header_restore(cd, CRYPT_LUKS1, BACKUP_FILE));
1405         OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
1406         OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1407         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1408         OK_(crypt_deactivate(cd, CDEVICE_1));
1409         crypt_free(cd);
1410
1411         _cleanup_dmdevices();
1412 }
1413
1414 static void ResizeDeviceLuks(void)
1415 {
1416         struct crypt_device *cd;
1417         struct crypt_params_luks1 params = {
1418                 .hash = "sha512",
1419                 .data_alignment = 2048,
1420         };
1421         char key[128];
1422
1423         const char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
1424         size_t key_size = strlen(mk_hex) / 2;
1425         const char *cipher = "aes";
1426         const char *cipher_mode = "cbc-essiv:sha256";
1427         uint64_t r_payload_offset, r_header_size, r_size;
1428
1429         crypt_decode_key(key, mk_hex, key_size);
1430
1431         // prepare env
1432         OK_(get_luks_offsets(0, key_size, params.data_alignment, 0, NULL, &r_payload_offset));
1433         OK_(get_luks_offsets(1, key_size, 0, 0, &r_header_size, NULL));
1434         OK_(create_dmdevice_over_loop(H_DEVICE, r_header_size));
1435         OK_(create_dmdevice_over_loop(L_DEVICE_OK, r_payload_offset + 1000));
1436         OK_(create_dmdevice_over_loop(L_DEVICE_0S, 1000));
1437
1438         // test header and encrypted payload all in one device
1439         OK_(crypt_init(&cd, DMDIR L_DEVICE_OK));
1440         OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params));
1441         OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1442         OK_(crypt_resize(cd, CDEVICE_1, 42));
1443         if (!device_size(DMDIR CDEVICE_1, &r_size))
1444                 EQ_(42, r_size >> SECTOR_SHIFT);
1445         // autodetect encrypted device area size
1446         OK_(crypt_resize(cd, CDEVICE_1, 0));
1447         if (!device_size(DMDIR CDEVICE_1, &r_size))
1448                 EQ_(1000, r_size >> SECTOR_SHIFT);
1449         FAIL_(crypt_resize(cd, CDEVICE_1, 1001), "Device too small");
1450         if (!device_size(DMDIR CDEVICE_1, &r_size))
1451                 EQ_(1000, r_size >> SECTOR_SHIFT);
1452         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1453         OK_(crypt_deactivate(cd, CDEVICE_1));
1454         crypt_free(cd);
1455
1456         params.data_alignment = 0;
1457         params.data_device = DMDIR L_DEVICE_0S;
1458         // test case for external header
1459         OK_(crypt_init(&cd, DMDIR H_DEVICE));
1460         OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params));
1461         OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1462         OK_(crypt_resize(cd, CDEVICE_1, 666));
1463         if (!device_size(DMDIR CDEVICE_1, &r_size))
1464                 EQ_(666, r_size >> SECTOR_SHIFT);
1465         // autodetect encrypted device size
1466         OK_(crypt_resize(cd, CDEVICE_1, 0));
1467         if (!device_size(DMDIR CDEVICE_1, &r_size))
1468                 EQ_(1000, r_size >> SECTOR_SHIFT);
1469         FAIL_(crypt_resize(cd, CDEVICE_1, 1001), "Device too small");
1470         if (!device_size(DMDIR CDEVICE_1, &r_size))
1471                 EQ_(1000, r_size >> SECTOR_SHIFT);
1472         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1473         OK_(crypt_deactivate(cd, CDEVICE_1));
1474         crypt_free(cd);
1475
1476         _cleanup_dmdevices();
1477 }
1478
1479 static void HashDevicePlain(void)
1480 {
1481         struct crypt_device *cd;
1482         struct crypt_params_plain params = {
1483                 .hash = NULL,
1484                 .skip = 0,
1485                 .offset = 0,
1486         };
1487
1488         size_t key_size;
1489         const char *mk_hex, *keystr;
1490         char key[256];
1491
1492         OK_(crypt_init(&cd, DEVICE_1));
1493         OK_(crypt_format(cd, CRYPT_PLAIN, "aes", "cbc-essiv:sha256", NULL, NULL, 16, &params));
1494
1495         // hash PLAIN, short key
1496         OK_(_prepare_keyfile(KEYFILE1, "tooshort", 8));
1497         FAIL_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 16, 0), "not enough data in keyfile");
1498         _remove_keyfiles();
1499
1500         // hash PLAIN, exact key
1501         //         0 1 2 3 4 5 6 7 8 9 a b c d e f
1502         mk_hex = "caffeecaffeecaffeecaffeecaffee88";
1503         key_size = 16;
1504         crypt_decode_key(key, mk_hex, key_size);
1505         OK_(_prepare_keyfile(KEYFILE1, key, key_size));
1506         OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size, 0));
1507         OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1508         OK_(strcmp(key, mk_hex));
1509         OK_(crypt_deactivate(cd, CDEVICE_1));
1510
1511         // Limit plain key
1512         mk_hex = "caffeecaffeecaffeecaffeeca000000";
1513         OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size - 3, 0));
1514         OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1515         OK_(strcmp(key, mk_hex));
1516         OK_(crypt_deactivate(cd, CDEVICE_1));
1517
1518         _remove_keyfiles();
1519
1520         // hash PLAIN, long key
1521         //         0 1 2 3 4 5 6 7 8 9 a b c d e f
1522         mk_hex = "caffeecaffeecaffeecaffeecaffee88babebabe";
1523         key_size = 16;
1524         crypt_decode_key(key, mk_hex, key_size);
1525         OK_(_prepare_keyfile(KEYFILE1, key, strlen(mk_hex) / 2));
1526         OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size, 0));
1527         OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1528         FAIL_(strcmp(key, mk_hex), "only key length used");
1529         OK_(strncmp(key, mk_hex, key_size));
1530         OK_(crypt_deactivate(cd, CDEVICE_1));
1531
1532
1533         // Now without explicit limit
1534         OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
1535         OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1536         FAIL_(strcmp(key, mk_hex), "only key length used");
1537         OK_(strncmp(key, mk_hex, key_size));
1538         OK_(crypt_deactivate(cd, CDEVICE_1));
1539         crypt_free(cd);
1540
1541         _remove_keyfiles();
1542
1543         // hash sha256
1544         params.hash = "sha256";
1545         OK_(crypt_init(&cd, DEVICE_1));
1546         OK_(crypt_format(cd, CRYPT_PLAIN, "aes", "cbc-essiv:sha256", NULL, NULL, 16, &params));
1547
1548         //         0 1 2 3 4 5 6 7 8 9 a b c d e f
1549         mk_hex = "c62e4615bd39e222572f3a1bf7c2132e";
1550         keystr = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
1551         key_size = strlen(keystr); // 32
1552         OK_(_prepare_keyfile(KEYFILE1, keystr, strlen(keystr)));
1553         OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size, 0));
1554         OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1555         OK_(strcmp(key, mk_hex));
1556         OK_(crypt_deactivate(cd, CDEVICE_1));
1557
1558         // Read full keyfile
1559         OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
1560         OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1561         OK_(strcmp(key, mk_hex));
1562         OK_(crypt_deactivate(cd, CDEVICE_1));
1563
1564         _remove_keyfiles();
1565
1566         // Limit keyfile read
1567         keystr = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxAAAAAAAA";
1568         OK_(_prepare_keyfile(KEYFILE1, keystr, strlen(keystr)));
1569         OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size, 0));
1570         OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1571         OK_(strcmp(key, mk_hex));
1572         OK_(crypt_deactivate(cd, CDEVICE_1));
1573
1574         // Full keyfile
1575         OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
1576         OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1577         OK_(strcmp(key, "0e49cb34a1dee1df33f6505e4de44a66"));
1578         OK_(crypt_deactivate(cd, CDEVICE_1));
1579
1580         _remove_keyfiles();
1581
1582         // FIXME: add keyfile="-" tests somehow
1583
1584         crypt_free(cd);
1585 }
1586
1587 static void VerityTest(void)
1588 {
1589         struct crypt_device *cd;
1590         const char *salt_hex =  "20c28ffc129c12360ba6ceea2b6cf04e89c2b41cfe6b8439eb53c1897f50df7b";
1591         const char *root_hex =  "ab018b003a967fc782effb293b6dccb60b4f40c06bf80d16391acf686d28b5d6";
1592         char salt[256], root_hash[256];
1593         struct crypt_active_device cad;
1594         struct crypt_params_verity params = {
1595                 .data_device = DEVICE_EMPTY,
1596                 .salt = salt,
1597                 .data_size = 0, /* whole device */
1598                 .hash_area_offset = 0,
1599                 .flags = CRYPT_VERITY_CREATE_HASH,
1600         };
1601
1602         crypt_decode_key(salt, salt_hex, strlen(salt_hex) / 2);
1603         crypt_decode_key(root_hash, root_hex, strlen(root_hex) / 2);
1604
1605         /* Format */
1606         OK_(crypt_init(&cd, DEVICE_2));
1607
1608         /* block size */
1609         params.data_block_size = 333;
1610         FAIL_(crypt_format(cd, CRYPT_VERITY, NULL, NULL, NULL, NULL, 0, &params),
1611                 "Unsupppored block size.");
1612         params.data_block_size = 4096;
1613         params.hash_block_size = 333;
1614         FAIL_(crypt_format(cd, CRYPT_VERITY, NULL, NULL, NULL, NULL, 0, &params),
1615                 "Unsupppored block size.");
1616         params.hash_block_size = 4096;
1617
1618         /* salt size */
1619         params.salt_size = 257;
1620         FAIL_(crypt_format(cd, CRYPT_VERITY, NULL, NULL, NULL, NULL, 0, &params),
1621                 "Too large salt.");
1622         params.salt_size = 32;
1623
1624         /* hash_type */
1625         params.hash_type = 3;
1626         FAIL_(crypt_format(cd, CRYPT_VERITY, NULL, NULL, NULL, NULL, 0, &params),
1627                 "Unsupported hash type.");
1628         params.hash_type = 1;
1629         params.hash_name = "blah";
1630         FAIL_(crypt_format(cd, CRYPT_VERITY, NULL, NULL, NULL, NULL, 0, &params),
1631                 "Unsupported hash name.");
1632         params.hash_name = "sha256";
1633
1634         OK_(crypt_format(cd, CRYPT_VERITY, NULL, NULL, NULL, NULL, 0, &params));
1635         crypt_free(cd);
1636
1637         /* Verify */
1638         OK_(crypt_init(&cd, DEVICE_2));
1639         memset(&params, 0, sizeof(params));
1640         params.data_device = DEVICE_EMPTY;
1641         params.flags = CRYPT_VERITY_CHECK_HASH;
1642         OK_(crypt_load(cd, CRYPT_VERITY, &params));
1643
1644         /* check verity params */
1645         EQ_(crypt_get_volume_key_size(cd), 32);
1646         OK_(strcmp(CRYPT_VERITY, crypt_get_type(cd)));
1647         memset(&params, 0, sizeof(params));
1648         OK_(crypt_get_verity_info(cd, &params));
1649         OK_(strcmp("sha256", params.hash_name));
1650         EQ_(strlen(salt_hex) / 2, params.salt_size);
1651         OK_(memcmp(salt, params.salt, params.salt_size));
1652         EQ_(4096, params.data_block_size);
1653         EQ_(4096, params.hash_block_size);
1654         EQ_(1, params.hash_type);
1655         EQ_(crypt_get_volume_key_size(cd), 32);
1656
1657         OK_(crypt_activate_by_volume_key(cd, NULL, root_hash, 32, 0));
1658         OK_(crypt_set_data_device(cd, DEVICE_1));
1659         FAIL_(crypt_activate_by_volume_key(cd, NULL, root_hash, 32, 0), "Data corrupted");;
1660
1661         OK_(crypt_set_data_device(cd, DEVICE_EMPTY));
1662         if (crypt_activate_by_volume_key(cd, CDEVICE_1, root_hash, 32,
1663             CRYPT_ACTIVATE_READONLY) == -ENOTSUP) {
1664                 printf("WARNING: kernel dm-verity not supported, skipping test.\n");
1665                 crypt_free(cd);
1666                 return;
1667         }
1668         OK_(crypt_get_active_device(cd, CDEVICE_1, &cad));
1669         EQ_(CRYPT_ACTIVATE_READONLY, cad.flags);
1670         crypt_free(cd);
1671
1672         OK_(crypt_init_by_name(&cd, CDEVICE_1));
1673         OK_(crypt_deactivate(cd, CDEVICE_1));
1674
1675         /* hash fail */
1676         root_hash[1] = ~root_hash[1];
1677         OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, root_hash, 32, CRYPT_ACTIVATE_READONLY));
1678         OK_(crypt_get_active_device(cd, CDEVICE_1, &cad));
1679         EQ_(CRYPT_ACTIVATE_READONLY|CRYPT_ACTIVATE_CORRUPTED, cad.flags);
1680         OK_(crypt_deactivate(cd, CDEVICE_1));
1681         root_hash[1] = ~root_hash[1];
1682
1683         /* data fail */
1684         OK_(crypt_set_data_device(cd, DEVICE_1));
1685         OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, root_hash, 32, CRYPT_ACTIVATE_READONLY));
1686         OK_(crypt_get_active_device(cd, CDEVICE_1, &cad));
1687         EQ_(CRYPT_ACTIVATE_READONLY|CRYPT_ACTIVATE_CORRUPTED, cad.flags);
1688         OK_(crypt_deactivate(cd, CDEVICE_1));
1689
1690         crypt_free(cd);
1691 }
1692
1693 static void TcryptTest(void)
1694 {
1695         struct crypt_device *cd = NULL;
1696         struct crypt_active_device cad;
1697         const char *passphrase = "aaaaaaaaaaaa";
1698         struct crypt_params_tcrypt params = {
1699                 .passphrase = passphrase,
1700                 .passphrase_size = strlen(passphrase),
1701         };
1702         const char *tcrypt_dev = "tcrypt-images/tc_5-sha512-xts-aes";
1703         size_t key_size = 64;
1704         char key[key_size], key_def[key_size];
1705         const char *key_hex =
1706                 "e87dd14403a547b440f459aa8284da62db364658a286b94ba2f3c7957c03f290"
1707                 "266d38facd211e12cd0abfc5b41555df6019d73374f85fbcb23fd4efc43b0c64";
1708
1709         crypt_decode_key(key_def, key_hex, strlen(key_hex) / 2);
1710
1711         OK_(crypt_init(&cd, tcrypt_dev));
1712         params.passphrase_size--;
1713         FAIL_(crypt_load(cd, CRYPT_TCRYPT, &params), "Wrong passphrase");
1714         params.passphrase_size++;
1715         OK_(crypt_load(cd, CRYPT_TCRYPT, &params));
1716
1717         // check params after load
1718         OK_(strcmp("xts-plain64", crypt_get_cipher_mode(cd)));
1719         OK_(strcmp("aes", crypt_get_cipher(cd)));
1720         EQ_(key_size, crypt_get_volume_key_size(cd));
1721         EQ_(256, crypt_get_iv_offset(cd));
1722         EQ_(256, crypt_get_data_offset(cd));
1723
1724         memset(key, 0, key_size);
1725         key_size--;
1726         // small buffer
1727         FAIL_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key, &key_size, NULL, 0), "small buffer");
1728         key_size++;
1729         OK_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key, &key_size, NULL, 0));
1730         OK_(memcmp(key, key_def, key_size));
1731
1732         reset_log();
1733         crypt_set_log_callback(cd, &new_log, NULL);
1734         OK_(crypt_dump(cd));
1735         OK_(!(global_lines != 0));
1736         crypt_set_log_callback(cd, NULL, NULL);
1737         reset_log();
1738
1739         OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, NULL, 0, CRYPT_ACTIVATE_READONLY));
1740         crypt_free(cd);
1741
1742         OK_(crypt_init_by_name_and_header(&cd, CDEVICE_1, NULL));
1743         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1744
1745         FAIL_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key, &key_size, NULL, 0), "Need crypt_load");
1746
1747         // check params after init_by_name
1748         OK_(strcmp("xts-plain64", crypt_get_cipher_mode(cd)));
1749         OK_(strcmp("aes", crypt_get_cipher(cd)));
1750         EQ_(key_size, crypt_get_volume_key_size(cd));
1751         EQ_(256, crypt_get_iv_offset(cd));
1752         EQ_(256, crypt_get_data_offset(cd));
1753
1754         OK_(crypt_get_active_device(cd, CDEVICE_1, &cad));
1755         EQ_(CRYPT_ACTIVATE_READONLY, cad.flags);
1756         EQ_(256, cad.offset);
1757         EQ_(256, cad.iv_offset);
1758         EQ_(72, cad.size);
1759
1760         OK_(crypt_deactivate(cd, CDEVICE_1));
1761         crypt_free(cd);
1762 }
1763
1764 // Check that gcrypt is properly initialised in format
1765 static void NonFIPSAlg(void)
1766 {
1767         struct crypt_device *cd;
1768         struct crypt_params_luks1 params = {0};
1769         char key[128] = "";
1770         size_t key_size = 128;
1771         const char *cipher = "aes";
1772         const char *cipher_mode = "cbc-essiv:sha256";
1773         int ret;
1774
1775         OK_(crypt_init(&cd, DEVICE_2));
1776         params.hash = "sha256";
1777         OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params));
1778         FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params),
1779               "Already formatted.");
1780         crypt_free(cd);
1781
1782         params.hash = "whirlpool";
1783         OK_(crypt_init(&cd, DEVICE_2));
1784         ret = crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params);
1785         if (ret < 0) {
1786                 printf("WARNING: whirlpool not supported, skipping test.\n");
1787                 crypt_free(cd);
1788                 return;
1789         }
1790         crypt_free(cd);
1791
1792         params.hash = "md5";
1793         OK_(crypt_init(&cd, DEVICE_2));
1794         FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params),
1795               "MD5 unsupported, too short");
1796         crypt_free(cd);
1797 }
1798
1799 int main(int argc, char *argv[])
1800 {
1801         int i;
1802
1803         if (getuid() != 0) {
1804                 printf("You must be root to run this test.\n");
1805                 exit(0);
1806         }
1807
1808         for (i = 1; i < argc; i++) {
1809                 if (!strcmp("-v", argv[i]) || !strcmp("--verbose", argv[i]))
1810                         _verbose = 1;
1811                 else if (!strcmp("--debug", argv[i]))
1812                         _debug = _verbose = 1;
1813         }
1814
1815         _cleanup();
1816         if (_setup())
1817                 goto out;
1818
1819         crypt_set_debug_level(_debug ? CRYPT_DEBUG_ALL : CRYPT_DEBUG_NONE);
1820
1821         RUN_(NonFIPSAlg, "Crypto is properly initialised in format"); //must be the first!
1822         RUN_(AddDevicePlain, "plain device API creation exercise");
1823         RUN_(HashDevicePlain, "plain device API hash test");
1824         RUN_(AddDeviceLuks, "Format and use LUKS device");
1825         RUN_(LuksHeaderLoad, "test header load");
1826         RUN_(LuksHeaderRestore, "test LUKS header restore");
1827         RUN_(LuksHeaderBackup, "test LUKS header backup");
1828         RUN_(ResizeDeviceLuks, "Luks device resize tests");
1829         RUN_(UseLuksDevice, "Use pre-formated LUKS device");
1830         RUN_(SuspendDevice, "Suspend/Resume test");
1831         RUN_(UseTempVolumes, "Format and use temporary encrypted device");
1832         RUN_(CallbacksTest, "API callbacks test");
1833         RUN_(VerityTest, "DM verity test");
1834         RUN_(TcryptTest, "Tcrypt API test");
1835 out:
1836         _cleanup();
1837         return 0;
1838 }