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