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