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