Add API tests for plain device.
[platform/upstream/cryptsetup.git] / tests / api-test.c
1 /*
2  * cryptsetup library API check functions
3  *
4  * Copyright (C) 2009-2010 Red Hat, Inc. All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * version 2 as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <linux/fs.h>
26 #include <errno.h>
27 #include <assert.h>
28 #include <sys/stat.h>
29 #include <sys/ioctl.h>
30 #include <libdevmapper.h>
31
32 #include "libcryptsetup.h"
33 #include "utils_loop.h"
34
35 #define DMDIR "/dev/mapper/"
36
37 #define DEVICE_1_UUID "28632274-8c8a-493f-835b-da802e1c576b"
38 #define DEVICE_EMPTY_name "crypt_zero"
39 #define DEVICE_EMPTY DMDIR DEVICE_EMPTY_name
40 #define DEVICE_ERROR_name "crypt_error"
41 #define DEVICE_ERROR DMDIR DEVICE_ERROR_name
42
43 #define CDEVICE_1 "ctest1"
44 #define CDEVICE_2 "ctest2"
45 #define CDEVICE_WRONG "O_o"
46
47 #define IMAGE1 "compatimage.img"
48 #define IMAGE_EMPTY "empty.img"
49
50 #define KEYFILE1 "key1.file"
51 #define KEY1 "compatkey"
52
53 #define KEYFILE2 "key2.file"
54 #define KEY2 "0123456789abcdef"
55
56 #define PASSPHRASE "blabla"
57
58 #define DEVICE_TEST_UUID "12345678-1234-1234-1234-123456789abc"
59
60 #define DEVICE_WRONG "/dev/Ooo_"
61 #define DEVICE_CHAR "/dev/zero"
62 #define TMP_FILE_TEMPLATE "cryptsetuptst.XXXXXX"
63
64 #define SECTOR_SHIFT 9L
65
66 static int _debug   = 0;
67 static int _verbose = 1;
68
69 static char global_log[4096];
70 static int global_lines = 0;
71
72 static char *DEVICE_1 = NULL;
73 static char *DEVICE_2 = NULL;
74 static char *H_DEVICE = NULL;
75
76 static char *tmp_file_1 = NULL;
77 static char *tmp_file_2 = NULL;
78
79 // Helpers
80
81 static int device_size(const char *device, uint64_t *size)
82 {
83         int devfd, r = 0;
84
85         devfd = open(device, O_RDONLY);
86         if(devfd == -1)
87                 return -EINVAL;
88
89         if (ioctl(devfd, BLKGETSIZE64, size) < 0)
90                 r = -EINVAL;
91         close(devfd);
92         return r;
93 }
94
95 // Get key from kernel dm mapping table using dm-ioctl
96 static int _get_key_dm(const char *name, char *buffer, unsigned int buffer_size)
97 {
98         struct dm_task *dmt;
99         struct dm_info dmi;
100         uint64_t start, length;
101         char *target_type, *rcipher, *key, *params;
102         void *next = NULL;
103         int r = -EINVAL;
104
105         if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
106                 goto out;
107         if (!dm_task_set_name(dmt, name))
108                 goto out;
109         if (!dm_task_run(dmt))
110                 goto out;
111         if (!dm_task_get_info(dmt, &dmi))
112                 goto out;
113         if (!dmi.exists)
114                 goto out;
115
116         next = dm_get_next_target(dmt, next, &start, &length, &target_type, &params);
117         if (!target_type || strcmp(target_type, "crypt") != 0)
118                 goto out;
119
120         rcipher = strsep(&params, " ");
121         key = strsep(&params, " ");
122
123         if (buffer_size <= strlen(key))
124                 goto out;
125
126         strncpy(buffer, key, buffer_size);
127         r = 0;
128 out:
129         if (dmt)
130                 dm_task_destroy(dmt);
131
132         return r;
133 }
134
135 static int _prepare_keyfile(const char *name, const char *passphrase, int size)
136 {
137         int fd, r;
138
139         fd = open(name, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR);
140         if (fd != -1) {
141                 r = write(fd, passphrase, size);
142                 close(fd);
143         } else
144                 r = 0;
145
146         return r == size ? 0 : 1;
147 }
148
149 static void _remove_keyfiles(void)
150 {
151         remove(KEYFILE1);
152         remove(KEYFILE2);
153 }
154
155 // Decode key from its hex representation
156 static int crypt_decode_key(char *key, char *hex, unsigned int size)
157 {
158         char buffer[3];
159         char *endp;
160         unsigned int i;
161
162         buffer[2] = '\0';
163
164         for (i = 0; i < size; i++) {
165                 buffer[0] = *hex++;
166                 buffer[1] = *hex++;
167
168                 key[i] = (unsigned char)strtoul(buffer, &endp, 16);
169
170                 if (endp != &buffer[2])
171                         return -1;
172         }
173
174         if (*hex != '\0')
175                 return -1;
176
177         return 0;
178 }
179
180 static void cmdLineLog(int level, char *msg)
181 {
182         strncat(global_log, msg, sizeof(global_log) - strlen(global_log));
183         global_lines++;
184 }
185
186 static void new_log(int level, const char *msg, void *usrptr)
187 {
188         cmdLineLog(level, (char*)msg);
189 }
190
191 static void reset_log()
192 {
193         memset(global_log, 0, sizeof(global_log));
194         global_lines = 0;
195 }
196
197 static void _system(const char *command, int warn)
198 {
199         if (system(command) < 0 && warn)
200                 printf("System command failed: %s", command);
201 }
202
203 static void _cleanup(void)
204 {
205         char *h_str;
206         struct stat st;
207
208         //_system("udevadm settle", 0);
209
210         if (!stat(DMDIR CDEVICE_1, &st))
211                 _system("dmsetup remove " CDEVICE_1, 0);
212
213         if (!stat(DMDIR CDEVICE_2, &st))
214                 _system("dmsetup remove " CDEVICE_2, 0);
215
216         if (!stat(DEVICE_EMPTY, &st))
217                 _system("dmsetup remove " DEVICE_EMPTY_name, 0);
218
219         if (!stat(DEVICE_ERROR, &st))
220                 _system("dmsetup remove " DEVICE_ERROR_name, 0);
221
222         if (crypt_loop_device(DEVICE_1))
223                 crypt_loop_detach(DEVICE_1);
224
225         if (crypt_loop_device(DEVICE_2))
226                 crypt_loop_detach(DEVICE_2);
227
228         _system("rm -f " IMAGE_EMPTY, 0);
229         _system("rm -f " IMAGE1, 0);
230
231         if (tmp_file_1)
232                 remove(tmp_file_1);
233         if (tmp_file_2)
234                 remove(tmp_file_2);
235
236         if (H_DEVICE && crypt_loop_device(H_DEVICE)) {
237                 h_str = crypt_loop_backing_file(H_DEVICE);
238                 if(!crypt_loop_detach(H_DEVICE) && h_str)
239                         remove(h_str);
240         }
241
242         _remove_keyfiles();
243 }
244
245 static int _setup(void)
246 {
247         int fd, ro = 0;
248         char *h_str, cmd[128];
249
250         tmp_file_1 = strdup(TMP_FILE_TEMPLATE);
251         if ((fd=mkstemp(tmp_file_1))==-1) {
252                 printf("cannot create temporary file with template %s\n",tmp_file_1);
253                 return 1;
254         }
255         close(fd);
256         snprintf(cmd,sizeof(cmd),"dd if=/dev/zero of=%s bs=512 count=100", tmp_file_1);
257         _system(cmd,1);
258
259         if (!H_DEVICE)
260                 H_DEVICE = crypt_loop_get_device();
261         if (!H_DEVICE) {
262                 printf("Cannot find free loop device.\n");
263                 return 1;
264         }
265         h_str = strdup(TMP_FILE_TEMPLATE);
266         if ((fd=mkstemp(h_str))==-1) {
267                 printf("cannot create temporary file with template %s\n",tmp_file_1);
268                 return 1;
269         }
270         close(fd);
271         snprintf(cmd,sizeof(cmd),"dd if=/dev/zero of=%s bs=512 count=1", h_str);
272         _system(cmd,1);
273         if (crypt_loop_device(H_DEVICE)) {
274                 fd = crypt_loop_attach(H_DEVICE,h_str,0,0,&ro);
275                 close(fd);
276         }
277         free(h_str);
278
279         tmp_file_2 = strdup(TMP_FILE_TEMPLATE);
280         if ((fd=mkstemp(tmp_file_2))==-1) {
281                 printf("cannot create temporary file with template %s\n",tmp_file_2);
282                 return 1;
283         }
284         close(fd);
285
286         _system("dmsetup create " DEVICE_EMPTY_name " --table \"0 10000 zero\"", 1);
287         _system("dmsetup create " DEVICE_ERROR_name " --table \"0 10000 error\"", 1);
288         if (!DEVICE_1)
289                 DEVICE_1 = crypt_loop_get_device();
290         if (!DEVICE_1) {
291                 printf("Cannot find free loop device.\n");
292                 return 1;
293         }
294         if (crypt_loop_device(DEVICE_1)) {
295                 _system(" [ ! -e " IMAGE1 " ] && bzip2 -dk " IMAGE1 ".bz2", 1);
296                 fd = crypt_loop_attach(DEVICE_1, IMAGE1, 0, 0, &ro);
297                 close(fd);
298         }
299         if (!DEVICE_2)
300                 DEVICE_2 = crypt_loop_get_device();
301         if (!DEVICE_2) {
302                 printf("Cannot find free loop device.\n");
303                 return 1;
304         }
305         if (crypt_loop_device(DEVICE_2)) {
306                 _system("dd if=/dev/zero of=" IMAGE_EMPTY " bs=1M count=4", 1);
307                 fd = crypt_loop_attach(DEVICE_2, IMAGE_EMPTY, 0, 0, &ro);
308                 close(fd);
309         }
310         return 0;
311 }
312
313 void check_ok(int status, int line, const char *func)
314 {
315         char buf[256];
316
317         if (status) {
318                 crypt_get_error(buf, sizeof(buf));
319                 printf("FAIL line %d [%s]: code %d, %s\n", line, func, status, buf);
320                 _cleanup();
321                 exit(-1);
322         }
323 }
324
325 void check_ko(int status, int line, const char *func)
326 {
327         char buf[256];
328
329         memset(buf, 0, sizeof(buf));
330         crypt_get_error(buf, sizeof(buf));
331         if (status >= 0) {
332                 printf("FAIL line %d [%s]: code %d, %s\n", line, func, status, buf);
333                 _cleanup();
334                 exit(-1);
335         } else if (_verbose)
336                 printf("   => errno %d, errmsg: %s\n", status, buf);
337 }
338
339 void check_equal(int line, const char *func)
340 {
341         printf("FAIL line %d [%s]: expected equal values differs.\n", line, func);
342         _cleanup();
343         exit(-1);
344 }
345
346 void xlog(const char *msg, const char *tst, const char *func, int line, const char *txt)
347 {
348         if (_verbose) {
349                 if (txt)
350                         printf(" [%s,%s:%d] %s [%s]\n", msg, func, line, tst, txt);
351                 else
352                         printf(" [%s,%s:%d] %s\n", msg, func, line, tst);
353         }
354 }
355 #define OK_(x)          do { xlog("(success)", #x, __FUNCTION__, __LINE__, NULL); \
356                              check_ok((x), __LINE__, __FUNCTION__); \
357                         } while(0)
358 #define FAIL_(x, y)     do { xlog("(fail)   ", #x, __FUNCTION__, __LINE__, y); \
359                              check_ko((x), __LINE__, __FUNCTION__); \
360                         } while(0)
361 #define EQ_(x, y)       do { xlog("(equal)  ", #x " == " #y, __FUNCTION__, __LINE__, NULL); \
362                              if ((x) != (y)) check_equal(__LINE__, __FUNCTION__); \
363                         } while(0)
364
365 #define RUN_(x, y)              do { printf("%s: %s\n", #x, (y)); x(); } while (0)
366
367 #if 0
368 static int yesDialog(char *msg)
369 {
370         return 1;
371 }
372
373 static struct interface_callbacks cmd_icb = {
374         .yesDialog = yesDialog,
375         .log = cmdLineLog,
376 };
377
378 // OLD API TESTS
379 static void LuksUUID(void)
380 {
381         struct crypt_options co = { .icb = &cmd_icb };
382
383         co.device = DEVICE_EMPTY;
384         EQ_(crypt_luksUUID(&co), -EINVAL);
385
386         co.device = DEVICE_ERROR;
387         EQ_(crypt_luksUUID(&co), -EINVAL);
388
389         reset_log();
390         co.device = DEVICE_1;
391         OK_(crypt_luksUUID(&co));
392         EQ_(strlen(global_log), 37); /* UUID + "\n" */
393         EQ_(strncmp(global_log, DEVICE_1_UUID, strlen(DEVICE_1_UUID)), 0);
394
395 }
396
397 static void IsLuks(void)
398 {
399         struct crypt_options co = {  .icb = &cmd_icb };
400
401         co.device = DEVICE_EMPTY;
402         EQ_(crypt_isLuks(&co), -EINVAL);
403
404         co.device = DEVICE_ERROR;
405         EQ_(crypt_isLuks(&co), -EINVAL);
406
407         co.device = DEVICE_1;
408         OK_(crypt_isLuks(&co));
409 }
410
411 static void LuksOpen(void)
412 {
413         struct crypt_options co = {
414                 .name = CDEVICE_1,
415                 //.passphrase = "blabla",
416                 .icb = &cmd_icb,
417         };
418
419         OK_(_prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
420         co.key_file = KEYFILE1;
421
422         co.device = DEVICE_EMPTY;
423         EQ_(crypt_luksOpen(&co), -EINVAL);
424
425         co.device = DEVICE_ERROR;
426         EQ_(crypt_luksOpen(&co), -EINVAL);
427
428         co.device = DEVICE_1;
429         OK_(crypt_luksOpen(&co));
430         FAIL_(crypt_luksOpen(&co), "already open");
431
432         _remove_keyfiles();
433 }
434
435 static void query_device(void)
436 {
437         struct crypt_options co = {.icb = &cmd_icb };
438
439         co.name = CDEVICE_WRONG;
440         EQ_(crypt_query_device(&co), 0);
441
442         co.name = CDEVICE_1;
443         EQ_(crypt_query_device(&co), 1);
444
445         OK_(strncmp(crypt_get_dir(), DMDIR, 11));
446         OK_(strcmp(co.cipher, "aes-cbc-essiv:sha256"));
447         EQ_(co.key_size, 16);
448         EQ_(co.offset, 1032);
449         EQ_(co.flags & CRYPT_FLAG_READONLY, 0);
450         EQ_(co.skip, 0);
451         crypt_put_options(&co);
452 }
453
454 static void remove_device(void)
455 {
456         int fd;
457         struct crypt_options co = {.icb = &cmd_icb };
458
459         co.name = CDEVICE_WRONG;
460         EQ_(crypt_remove_device(&co), -ENODEV);
461
462         fd = open(DMDIR CDEVICE_1, O_RDONLY);
463         co.name = CDEVICE_1;
464         FAIL_(crypt_remove_device(&co), "device busy");
465         close(fd);
466
467         OK_(crypt_remove_device(&co));
468 }
469
470 static void LuksFormat(void)
471 {
472         struct crypt_options co = {
473                 .device = DEVICE_2,
474                 .key_size = 256 / 8,
475                 .key_slot = -1,
476                 .cipher = "aes-cbc-essiv:sha256",
477                 .hash = "sha1",
478                 .flags = 0,
479                 .iteration_time = 10,
480                 .align_payload = 0,
481                 .icb = &cmd_icb,
482         };
483
484         OK_(_prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
485
486         co.new_key_file = KEYFILE1;
487         co.device = DEVICE_ERROR;
488         FAIL_(crypt_luksFormat(&co), "error device");
489
490         co.device = DEVICE_2;
491         OK_(crypt_luksFormat(&co));
492
493         co.new_key_file = NULL;
494         co.key_file = KEYFILE1;
495         co.name = CDEVICE_2;
496         OK_(crypt_luksOpen(&co));
497         OK_(crypt_remove_device(&co));
498         _remove_keyfiles();
499 }
500
501 static void LuksKeyGame(void)
502 {
503         int i;
504         struct crypt_options co = {
505                 .device = DEVICE_2,
506                 .key_size = 256 / 8,
507                 .key_slot = -1,
508                 .cipher = "aes-cbc-essiv:sha256",
509                 .hash = "sha1",
510                 .flags = 0,
511                 .iteration_time = 10,
512                 .align_payload = 0,
513                 .icb = &cmd_icb,
514         };
515
516         OK_(_prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
517         OK_(_prepare_keyfile(KEYFILE2, KEY2, strlen(KEY2)));
518
519         co.new_key_file = KEYFILE1;
520         co.device = DEVICE_2;
521         co.key_slot = 8;
522         FAIL_(crypt_luksFormat(&co), "wrong slot #");
523
524         co.key_slot = 7; // last slot
525         OK_(crypt_luksFormat(&co));
526
527         co.new_key_file = KEYFILE1;
528         co.key_file = KEYFILE1;
529         co.key_slot = 8;
530         FAIL_(crypt_luksAddKey(&co), "wrong slot #");
531         co.key_slot = 7;
532         FAIL_(crypt_luksAddKey(&co), "slot already used");
533
534         co.key_slot = 6;
535         OK_(crypt_luksAddKey(&co));
536
537         co.key_file = KEYFILE2 "blah";
538         co.key_slot = 5;
539         FAIL_(crypt_luksAddKey(&co), "keyfile not found");
540
541         co.new_key_file = KEYFILE2; // key to add
542         co.key_file = KEYFILE1;
543         co.key_slot = -1;
544         for (i = 0; i < 6; i++)
545                 OK_(crypt_luksAddKey(&co)); //FIXME: EQ_(i)?
546
547         FAIL_(crypt_luksAddKey(&co), "all slots full");
548
549         // REMOVE KEY
550         co.new_key_file = KEYFILE1; // key to remove
551         co.key_file = NULL;
552         co.key_slot = 8; // should be ignored
553          // only 2 slots should use KEYFILE1
554         OK_(crypt_luksRemoveKey(&co));
555         OK_(crypt_luksRemoveKey(&co));
556         FAIL_(crypt_luksRemoveKey(&co), "no slot with this passphrase");
557
558         co.new_key_file = KEYFILE2 "blah";
559         co.key_file = NULL;
560         FAIL_(crypt_luksRemoveKey(&co), "keyfile not found");
561
562         // KILL SLOT
563         co.new_key_file = NULL;
564         co.key_file = NULL;
565         co.key_slot = 8;
566         FAIL_(crypt_luksKillSlot(&co), "wrong slot #");
567         co.key_slot = 7;
568         FAIL_(crypt_luksKillSlot(&co), "slot already wiped");
569
570         co.key_slot = 5;
571         OK_(crypt_luksKillSlot(&co));
572
573         _remove_keyfiles();
574 }
575
576 size_t _get_device_size(const char *device)
577 {
578         unsigned long size = 0;
579         int fd;
580
581         fd = open(device, O_RDONLY);
582         if (fd == -1)
583                 return 0;
584         (void)ioctl(fd, BLKGETSIZE, &size);
585         close(fd);
586
587         return size;
588 }
589
590 void DeviceResizeGame(void)
591 {
592         size_t orig_size;
593         struct crypt_options co = {
594                 .name = CDEVICE_2,
595                 .device = DEVICE_2,
596                 .key_size = 128 / 8,
597                 .cipher = "aes-cbc-plain",
598                 .hash = "sha1",
599                 .offset = 333,
600                 .skip = 0,
601                 .icb = &cmd_icb,
602         };
603
604         orig_size = _get_device_size(DEVICE_2);
605
606         OK_(_prepare_keyfile(KEYFILE2, KEY2, strlen(KEY2)));
607
608         co.key_file = KEYFILE2;
609         co.size = 1000;
610         OK_(crypt_create_device(&co));
611         EQ_(_get_device_size(DMDIR CDEVICE_2), 1000);
612
613         co.size = 2000;
614         OK_(crypt_resize_device(&co));
615         EQ_(_get_device_size(DMDIR CDEVICE_2), 2000);
616
617         co.size = 0;
618         OK_(crypt_resize_device(&co));
619         EQ_(_get_device_size(DMDIR CDEVICE_2), (orig_size - 333));
620         co.size = 0;
621         co.offset = 444;
622         co.skip = 555;
623         co.cipher = "aes-cbc-essiv:sha256";
624         OK_(crypt_update_device(&co));
625         EQ_(_get_device_size(DMDIR CDEVICE_2), (orig_size - 444));
626
627         memset(&co, 0, sizeof(co));
628         co.icb = &cmd_icb,
629         co.name = CDEVICE_2;
630         EQ_(crypt_query_device(&co), 1);
631         EQ_(strcmp(co.cipher, "aes-cbc-essiv:sha256"), 0);
632         EQ_(co.key_size, 128 / 8);
633         EQ_(co.offset, 444);
634         EQ_(co.skip, 555);
635         crypt_put_options(&co);
636
637         // dangerous switch device still works
638         memset(&co, 0, sizeof(co));
639         co.name = CDEVICE_2,
640         co.device = DEVICE_1;
641         co.key_file = KEYFILE2;
642         co.key_size = 128 / 8;
643         co.cipher = "aes-cbc-plain";
644         co.hash = "sha1";
645         co.icb = &cmd_icb;
646         OK_(crypt_update_device(&co));
647
648         memset(&co, 0, sizeof(co));
649         co.icb = &cmd_icb,
650         co.name = CDEVICE_2;
651         EQ_(crypt_query_device(&co), 1);
652         EQ_(strcmp(co.cipher, "aes-cbc-plain"), 0);
653         EQ_(co.key_size, 128 / 8);
654         EQ_(co.offset, 0);
655         EQ_(co.skip, 0);
656         // This expect lookup returns prefered /dev/loopX
657         EQ_(strcmp(co.device, DEVICE_1), 0);
658         crypt_put_options(&co);
659
660         memset(&co, 0, sizeof(co));
661         co.icb = &cmd_icb,
662         co.name = CDEVICE_2;
663         OK_(crypt_remove_device(&co));
664
665         _remove_keyfiles();
666 }
667 #endif
668
669 // NEW API tests
670
671 static void AddDevicePlain(void)
672 {
673         struct crypt_device *cd, *cd2;
674         struct crypt_params_plain params = {
675                 .hash = "sha1",
676                 .skip = 0,
677                 .offset = 0,
678                 .size = 0
679         };
680         int fd;
681         char key[128], key2[128], path[128];
682
683         char *passphrase = PASSPHRASE;
684         // hashed hex version of PASSPHRASE
685         char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
686         size_t key_size = strlen(mk_hex) / 2;
687         char *cipher = "aes";
688         char *cipher_mode = "cbc-essiv:sha256";
689
690         uint64_t size, r_size;
691
692         crypt_decode_key(key, mk_hex, key_size);
693         FAIL_(crypt_init(&cd, ""), "empty device string");
694         FAIL_(crypt_init(&cd, DEVICE_WRONG), "nonexistent device name ");
695         FAIL_(crypt_init(&cd, DEVICE_CHAR), "character device as backing device");
696         OK_(crypt_init(&cd, tmp_file_1));
697         crypt_free(cd);
698
699         // test crypt_format, crypt_get_cipher, crypt_get_cipher_mode, crypt_get_volume_key_size
700         OK_(crypt_init(&cd,DEVICE_1));
701         params.skip = 3;
702         params.offset = 42;
703         FAIL_(crypt_format(cd,CRYPT_PLAIN,NULL,cipher_mode,NULL,NULL,key_size,&params),"cipher param is null");
704         FAIL_(crypt_format(cd,CRYPT_PLAIN,cipher,NULL,NULL,NULL,key_size,&params),"cipher_mode param is null");
705         OK_(crypt_format(cd,CRYPT_PLAIN,cipher,cipher_mode,NULL,NULL,key_size,&params));
706         OK_(strcmp(cipher_mode,crypt_get_cipher_mode(cd)));
707         OK_(strcmp(cipher,crypt_get_cipher(cd)));
708         EQ_(key_size, crypt_get_volume_key_size(cd));
709         EQ_(params.skip, crypt_get_iv_offset(cd));
710         EQ_(params.offset, crypt_get_data_offset(cd));
711         params.skip = 0;
712         params.offset = 0;
713
714         // crypt_set_uuid()
715         FAIL_(crypt_set_uuid(cd,DEVICE_1_UUID),"can't set uuid to plain device");
716
717         // crypt_load() should fail for PLAIN
718         FAIL_(crypt_load(cd,CRYPT_PLAIN,NULL),"can't load header from plain device");
719
720         crypt_free(cd);
721
722         // default is "plain" hash - no password hash
723         OK_(crypt_init(&cd, DEVICE_1));
724         OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, NULL));
725         FAIL_(crypt_activate_by_volume_key(cd, NULL, key, key_size, 0), "cannot verify key with plain");
726         OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
727         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
728         OK_(crypt_deactivate(cd, CDEVICE_1));
729         crypt_free(cd);
730
731         // test boundaries in offset parameter
732         device_size(DEVICE_1,&size);
733         params.hash = NULL;
734         // zero sectors length
735         params.offset = size >> SECTOR_SHIFT;
736         OK_(crypt_init(&cd, DEVICE_1));
737         OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, &params));
738         EQ_(crypt_get_data_offset(cd),params.offset);
739         // device size is 0 sectors
740         FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0), "invalid device size (0 blocks)");
741         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
742         // data part of crypt device is of 1 sector size
743         params.offset = (size >> SECTOR_SHIFT) - 1;
744         OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, &params));
745         OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
746         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
747         snprintf(path, sizeof(path), "%s/%s", crypt_get_dir(), CDEVICE_1);
748         if (device_size(path, &r_size) >= 0)
749                 EQ_(r_size>>SECTOR_SHIFT, 1);
750         OK_(crypt_deactivate(cd, CDEVICE_1));
751
752         // size > device_size
753         params.offset = 0;
754         params.size = (size >> SECTOR_SHIFT) + 1;
755         crypt_init(&cd, DEVICE_1);
756         OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, &params));
757         FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0),"Device too small");
758         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
759
760         // offset == device_size (autodetect size)
761         params.offset = (size >> SECTOR_SHIFT);
762         params.size = 0;
763         OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, &params));
764         FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0),"Device too small");
765         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
766
767         // offset == device_size (user defined size)
768         params.offset = (size >> SECTOR_SHIFT);
769         params.size = 123;
770         OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, &params));
771         FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0),"Device too small");
772         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
773
774         // offset+size > device_size
775         params.offset = 42;
776         params.size = (size >> SECTOR_SHIFT) - params.offset + 1;
777         OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, &params));
778         FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0),"Offset and size are beyond device real size");
779         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
780
781         // offset+size == device_size
782         params.offset = 42;
783         params.size = (size >> SECTOR_SHIFT) - params.offset;
784         OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, &params));
785         OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
786         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
787         if (!device_size(path, &r_size))
788                 EQ_((r_size >> SECTOR_SHIFT),params.size);
789         OK_(crypt_deactivate(cd,CDEVICE_1));
790
791         crypt_free(cd);
792         params.hash = "sha1";
793         params.offset = 0;
794         params.size = 0;
795         params.skip = 0;
796
797         // Now use hashed password
798         OK_(crypt_init(&cd, DEVICE_1));
799         OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, &params));
800         FAIL_(crypt_activate_by_passphrase(cd, NULL, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0),
801               "cannot verify passphrase with plain" );
802         OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
803
804         // device status check
805         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
806         snprintf(path, sizeof(path), "%s/%s", crypt_get_dir(), CDEVICE_1);
807         fd = open(path, O_RDONLY);
808         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_BUSY);
809         FAIL_(crypt_deactivate(cd, CDEVICE_1), "Device is busy");
810         close(fd);
811         OK_(crypt_deactivate(cd, CDEVICE_1));
812         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
813
814         // crypt_init_by_name_and_header
815         OK_(crypt_init(&cd,DEVICE_1));
816         OK_(crypt_format(cd, CRYPT_PLAIN,cipher,cipher_mode,NULL,NULL,key_size,&params));
817         OK_(crypt_activate_by_volume_key(cd,CDEVICE_1,key,key_size,0));
818         FAIL_(crypt_init_by_name_and_header(&cd2,CDEVICE_1,H_DEVICE),"can't init plain device by header device");
819         OK_(crypt_init_by_name(&cd2,CDEVICE_1));
820         OK_(crypt_deactivate(cd,CDEVICE_1));
821         crypt_free(cd);
822         crypt_free(cd2);
823
824         OK_(crypt_init(&cd,DEVICE_1));
825         OK_(crypt_format(cd,CRYPT_PLAIN,cipher,cipher_mode,NULL,NULL,key_size,&params));
826         params.size = 0;
827         params.offset = 0;
828
829         // crypt_set_data_device
830         FAIL_(crypt_set_data_device(cd,H_DEVICE),"can't set data device for plain device");
831
832         // crypt_get_type
833         OK_(strcmp(crypt_get_type(cd),CRYPT_PLAIN));
834
835         OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
836         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
837
838         // crypt_resize()
839         OK_(crypt_resize(cd,CDEVICE_1,size>>SECTOR_SHIFT)); // same size
840         if (!device_size(path,&r_size))
841                 EQ_(r_size, size);
842
843         // size overlaps
844         FAIL_(crypt_resize(cd, CDEVICE_1, ULLONG_MAX),"Backing device is too small");
845         FAIL_(crypt_resize(cd, CDEVICE_1, (size>>SECTOR_SHIFT)+1),"crypt device overlaps backing device");
846
847         // resize ok
848         OK_(crypt_resize(cd,CDEVICE_1, 123));
849         if (!device_size(path,&r_size))
850                 EQ_(r_size>>SECTOR_SHIFT, 123);
851         OK_(crypt_resize(cd,CDEVICE_1,0)); // full size (autodetect)
852         if (!device_size(path,&r_size))
853                 EQ_(r_size, size);
854         OK_(crypt_deactivate(cd,CDEVICE_1));
855         EQ_(crypt_status(cd,CDEVICE_1),CRYPT_INACTIVE);
856
857         // offset tests
858         OK_(crypt_init(&cd,DEVICE_1));
859         params.offset = 42;
860         params.size = (size>>SECTOR_SHIFT) - params.offset - 10;
861         OK_(crypt_format(cd,CRYPT_PLAIN,cipher,cipher_mode,NULL,NULL,key_size,&params));
862         OK_(crypt_activate_by_volume_key(cd,CDEVICE_1,key,key_size,0));
863         if (!device_size(path,&r_size))
864                 EQ_(r_size>>SECTOR_SHIFT, params.size);
865         // resize to fill remaining capacity
866         OK_(crypt_resize(cd,CDEVICE_1,params.size + 10));
867         if (!device_size(path,&r_size))
868                 EQ_(r_size>>SECTOR_SHIFT, params.size + 10);
869
870         // 1 sector beyond real size
871         FAIL_(crypt_resize(cd,CDEVICE_1,params.size + 11), "new device size overlaps backing device"); // with respect to offset
872         if (!device_size(path,&r_size))
873                 EQ_(r_size>>SECTOR_SHIFT, params.size + 10);
874         EQ_(crypt_status(cd,CDEVICE_1),CRYPT_ACTIVE);
875         fd = open(path, O_RDONLY);
876         close(fd);
877         OK_(fd < 0);
878
879         // resize to minimal size
880         OK_(crypt_resize(cd,CDEVICE_1, 1)); // minimal device size
881         if (!device_size(path,&r_size))
882                 EQ_(r_size>>SECTOR_SHIFT, 1);
883         // use size of backing device (autodetect with respect to offset)
884         OK_(crypt_resize(cd,CDEVICE_1,0));
885         if (!device_size(path,&r_size))
886                 EQ_(r_size>>SECTOR_SHIFT, (size >> SECTOR_SHIFT)- 42);
887         OK_(crypt_deactivate(cd,CDEVICE_1));
888
889         params.size = 0;
890         params.offset = 0;
891         OK_(crypt_format(cd,CRYPT_PLAIN,cipher,cipher_mode,NULL,NULL,key_size,&params));
892         OK_(crypt_activate_by_volume_key(cd,CDEVICE_1,key,key_size,0));
893
894         // suspend/resume tests
895         FAIL_(crypt_suspend(cd,CDEVICE_1),"cannot suspend plain device");
896         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
897         FAIL_(crypt_resume_by_passphrase(cd,CDEVICE_1,CRYPT_ANY_SLOT,passphrase, strlen(passphrase)),"cannot resume plain device");
898         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
899
900         // retrieve volume key check
901         memset(key2, 0, key_size);
902         key_size--;
903         // small buffer
904         FAIL_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)), "small buffer");
905         key_size++;
906         OK_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)));
907
908         OK_(memcmp(key, key2, key_size));
909         OK_(strcmp(cipher, crypt_get_cipher(cd)));
910         OK_(strcmp(cipher_mode, crypt_get_cipher_mode(cd)));
911         EQ_(key_size, crypt_get_volume_key_size(cd));
912         EQ_(0, crypt_get_data_offset(cd));
913         OK_(crypt_deactivate(cd, CDEVICE_1));
914
915         // now with keyfile
916         OK_(_prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
917         OK_(_prepare_keyfile(KEYFILE2, KEY2, strlen(KEY2)));
918         FAIL_(crypt_activate_by_keyfile(cd, NULL, CRYPT_ANY_SLOT, KEYFILE1, 0, 0), "cannot verify key with plain");
919         EQ_(0, crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
920         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
921         OK_(crypt_deactivate(cd, CDEVICE_1));
922         _remove_keyfiles();
923
924         OK_(crypt_init(&cd,DEVICE_1));
925         OK_(crypt_format(cd,CRYPT_PLAIN,cipher,cipher_mode,NULL,NULL,key_size,&params));
926
927         // crypt_keyslot_*()
928         FAIL_(crypt_keyslot_add_by_passphrase(cd,CRYPT_ANY_SLOT,passphrase,strlen(passphrase),passphrase,strlen(passphrase)), "can't add keyslot to plain device");
929         FAIL_(crypt_keyslot_add_by_volume_key(cd,CRYPT_ANY_SLOT ,key,key_size,passphrase,strlen(passphrase)),"can't add keyslot to plain device");
930         FAIL_(crypt_keyslot_add_by_keyfile(cd,CRYPT_ANY_SLOT,KEYFILE1,strlen(KEY1),KEYFILE2,strlen(KEY2)),"can't add keyslot to plain device");
931         FAIL_(crypt_keyslot_destroy(cd,1),"can't manipulate keyslots on plain device");
932         EQ_(crypt_keyslot_status(cd, 0), CRYPT_SLOT_INVALID);
933         _remove_keyfiles();
934
935         crypt_free(cd);
936 }
937
938 #define CALLBACK_ERROR "calback_error xyz"
939 static int pass_callback_err(const char *msg, char *buf, size_t length, void *usrptr)
940 {
941         struct crypt_device *cd = usrptr;
942
943         assert(cd);
944         assert(length);
945         assert(msg);
946
947         crypt_log(cd, CRYPT_LOG_ERROR, CALLBACK_ERROR);
948         return -EINVAL;
949 }
950
951 static int pass_callback_ok(const char *msg, char *buf, size_t length, void *usrptr)
952 {
953         assert(length);
954         assert(msg);
955         strcpy(buf, PASSPHRASE);
956         return strlen(buf);
957 }
958
959 static void CallbacksTest(void)
960 {
961         struct crypt_device *cd;
962         struct crypt_params_plain params = {
963                 .hash = "sha1",
964                 .skip = 0,
965                 .offset = 0,
966         };
967
968         size_t key_size = 256 / 8;
969         char *cipher = "aes";
970         char *cipher_mode = "cbc-essiv:sha256";
971         char *passphrase = PASSPHRASE;
972
973         OK_(crypt_init(&cd, DEVICE_1));
974         crypt_set_log_callback(cd, &new_log, NULL);
975         //crypt_set_log_callback(cd, NULL, NULL);
976
977         OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, &params));
978
979         OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
980         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
981         OK_(crypt_deactivate(cd, CDEVICE_1));
982
983         reset_log();
984         crypt_set_password_callback(cd, pass_callback_err, cd);
985         FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, NULL, 0, 0), "callback fails");
986         EQ_(strncmp(global_log, CALLBACK_ERROR, strlen(CALLBACK_ERROR)), 0);
987
988         crypt_set_password_callback(cd, pass_callback_ok, NULL);
989         OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, NULL, 0, 0));
990         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
991         OK_(crypt_deactivate(cd, CDEVICE_1));
992
993         crypt_free(cd);
994 }
995
996 static void UseLuksDevice(void)
997 {
998         struct crypt_device *cd;
999         char key[128];
1000         size_t key_size;
1001
1002         OK_(crypt_init(&cd, DEVICE_1));
1003         OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
1004         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
1005         OK_(crypt_activate_by_passphrase(cd, NULL, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0));
1006         OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0));
1007         FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0), "already open");
1008         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1009         OK_(crypt_deactivate(cd, CDEVICE_1));
1010         FAIL_(crypt_deactivate(cd, CDEVICE_1), "no such device");
1011
1012         key_size = 16;
1013         OK_(strcmp("aes", crypt_get_cipher(cd)));
1014         OK_(strcmp("cbc-essiv:sha256", crypt_get_cipher_mode(cd)));
1015         OK_(strcmp(DEVICE_1_UUID, crypt_get_uuid(cd)));
1016         EQ_(key_size, crypt_get_volume_key_size(cd));
1017         EQ_(1032, crypt_get_data_offset(cd));
1018
1019         EQ_(0, crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key, &key_size, KEY1, strlen(KEY1)));
1020         OK_(crypt_volume_key_verify(cd, key, key_size));
1021         OK_(crypt_activate_by_volume_key(cd, NULL, key, key_size, 0));
1022         OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
1023         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
1024         OK_(crypt_deactivate(cd, CDEVICE_1));
1025
1026         key[1] = ~key[1];
1027         FAIL_(crypt_volume_key_verify(cd, key, key_size), "key mismatch");
1028         FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0), "key mismatch");
1029         crypt_free(cd);
1030 }
1031
1032 static void SuspendDevice(void)
1033 {
1034         int suspend_status;
1035         struct crypt_device *cd;
1036
1037         OK_(crypt_init(&cd, DEVICE_1));
1038         OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
1039         OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0));
1040
1041         suspend_status = crypt_suspend(cd, CDEVICE_1);
1042         if (suspend_status == -ENOTSUP) {
1043                 printf("WARNING: Suspend/Resume not supported, skipping test.\n");
1044                 goto out;
1045         }
1046         OK_(suspend_status);
1047         FAIL_(crypt_suspend(cd, CDEVICE_1), "already suspended");
1048
1049         FAIL_(crypt_resume_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1)-1), "wrong key");
1050         OK_(crypt_resume_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1)));
1051         FAIL_(crypt_resume_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1)), "not suspended");
1052
1053         OK_(_prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
1054         OK_(crypt_suspend(cd, CDEVICE_1));
1055         FAIL_(crypt_resume_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1 "blah", 0), "wrong keyfile");
1056         OK_(crypt_resume_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0));
1057         FAIL_(crypt_resume_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0), "not suspended");
1058         _remove_keyfiles();
1059 out:
1060         OK_(crypt_deactivate(cd, CDEVICE_1));
1061         crypt_free(cd);
1062 }
1063
1064 static void AddDeviceLuks(void)
1065 {
1066         struct crypt_device *cd;
1067         struct crypt_params_luks1 params = {
1068                 .hash = "sha512",
1069                 .data_alignment = 2048, // 4M, data offset will be 4096
1070         };
1071         char key[128], key2[128];
1072
1073         char *passphrase = "blabla";
1074         char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
1075         size_t key_size = strlen(mk_hex) / 2;
1076         char *cipher = "aes";
1077         char *cipher_mode = "cbc-essiv:sha256";
1078
1079         crypt_decode_key(key, mk_hex, key_size);
1080
1081         OK_(crypt_init(&cd, DEVICE_2));
1082         OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params));
1083
1084         // even with no keyslots defined it can be activated by volume key
1085         OK_(crypt_volume_key_verify(cd, key, key_size));
1086         OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, key, key_size, 0));
1087         EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
1088         OK_(crypt_deactivate(cd, CDEVICE_2));
1089
1090         // now with keyslot
1091         EQ_(7, crypt_keyslot_add_by_volume_key(cd, 7, key, key_size, passphrase, strlen(passphrase)));
1092         EQ_(CRYPT_SLOT_ACTIVE_LAST, crypt_keyslot_status(cd, 7));
1093         EQ_(7, crypt_activate_by_passphrase(cd, CDEVICE_2, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
1094         EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
1095         OK_(crypt_deactivate(cd, CDEVICE_2));
1096
1097         EQ_(1, crypt_keyslot_add_by_volume_key(cd, 1, key, key_size, KEY1, strlen(KEY1)));
1098         OK_(_prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
1099         OK_(_prepare_keyfile(KEYFILE2, KEY2, strlen(KEY2)));
1100         EQ_(2, crypt_keyslot_add_by_keyfile(cd, 2, KEYFILE1, 0, KEYFILE2, 0));
1101         FAIL_(crypt_activate_by_keyfile(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, strlen(KEY2)-1, 0), "key mismatch");
1102         EQ_(2, crypt_activate_by_keyfile(cd, NULL, CRYPT_ANY_SLOT, KEYFILE2, 0, 0));
1103         EQ_(2, crypt_activate_by_keyfile(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, 0, 0));
1104         OK_(crypt_keyslot_destroy(cd, 1));
1105         OK_(crypt_keyslot_destroy(cd, 2));
1106         OK_(crypt_deactivate(cd, CDEVICE_2));
1107         _remove_keyfiles();
1108
1109         FAIL_(crypt_keyslot_add_by_volume_key(cd, 7, key, key_size, passphrase, strlen(passphrase)), "slot used");
1110         key[1] = ~key[1];
1111         FAIL_(crypt_keyslot_add_by_volume_key(cd, 6, key, key_size, passphrase, strlen(passphrase)), "key mismatch");
1112         key[1] = ~key[1];
1113         EQ_(6, crypt_keyslot_add_by_volume_key(cd, 6, key, key_size, passphrase, strlen(passphrase)));
1114         EQ_(CRYPT_SLOT_ACTIVE, crypt_keyslot_status(cd, 6));
1115
1116         FAIL_(crypt_keyslot_destroy(cd, 8), "invalid keyslot");
1117         FAIL_(crypt_keyslot_destroy(cd, CRYPT_ANY_SLOT), "invalid keyslot");
1118         FAIL_(crypt_keyslot_destroy(cd, 0), "keyslot not used");
1119         OK_(crypt_keyslot_destroy(cd, 7));
1120         EQ_(CRYPT_SLOT_INACTIVE, crypt_keyslot_status(cd, 7));
1121         EQ_(CRYPT_SLOT_ACTIVE_LAST, crypt_keyslot_status(cd, 6));
1122
1123         EQ_(6, crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)));
1124         OK_(crypt_volume_key_verify(cd, key2, key_size));
1125
1126         OK_(memcmp(key, key2, key_size));
1127         OK_(strcmp(cipher, crypt_get_cipher(cd)));
1128         OK_(strcmp(cipher_mode, crypt_get_cipher_mode(cd)));
1129         EQ_(key_size, crypt_get_volume_key_size(cd));
1130         EQ_(4096, crypt_get_data_offset(cd));
1131         OK_(strcmp(DEVICE_2, crypt_get_device_name(cd)));
1132
1133         reset_log();
1134         crypt_set_log_callback(cd, &new_log, NULL);
1135         OK_(crypt_dump(cd));
1136         OK_(!(global_lines != 0));
1137         crypt_set_log_callback(cd, NULL, NULL);
1138         reset_log();
1139
1140         FAIL_(crypt_set_uuid(cd, "blah"), "wrong UUID format");
1141         OK_(crypt_set_uuid(cd, DEVICE_TEST_UUID));
1142         OK_(strcmp(DEVICE_TEST_UUID, crypt_get_uuid(cd)));
1143
1144         FAIL_(crypt_deactivate(cd, CDEVICE_2), "not active");
1145         crypt_free(cd);
1146 }
1147
1148 static void UseTempVolumes(void)
1149 {
1150         struct crypt_device *cd;
1151         char tmp[256];
1152
1153         // Tepmporary device without keyslot but with on-disk LUKS header
1154         OK_(crypt_init(&cd, DEVICE_2));
1155         FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "not yet formatted");
1156         OK_(crypt_format(cd, CRYPT_LUKS1, "aes", "cbc-essiv:sha256", NULL, NULL, 16, NULL));
1157         OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0));
1158         EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
1159         crypt_free(cd);
1160
1161         OK_(crypt_init_by_name(&cd, CDEVICE_2));
1162         OK_(crypt_deactivate(cd, CDEVICE_2));
1163         crypt_free(cd);
1164
1165         // Dirty checks: device without UUID
1166         // we should be able to remove it but not manuipulate with it
1167         snprintf(tmp, sizeof(tmp), "dmsetup create %s --table \""
1168                 "0 100 crypt aes-cbc-essiv:sha256 deadbabedeadbabedeadbabedeadbabe 0 "
1169                 "%s 2048\"", CDEVICE_2, DEVICE_2);
1170         _system(tmp, 1);
1171         OK_(crypt_init_by_name(&cd, CDEVICE_2));
1172         OK_(crypt_deactivate(cd, CDEVICE_2));
1173         FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "No known device type");
1174         crypt_free(cd);
1175
1176         // Dirty checks: device with UUID but LUKS header key fingerprint must fail)
1177         snprintf(tmp, sizeof(tmp), "dmsetup create %s --table \""
1178                 "0 100 crypt aes-cbc-essiv:sha256 deadbabedeadbabedeadbabedeadbabe 0 "
1179                 "%s 2048\" -u CRYPT-LUKS1-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-ctest1",
1180                  CDEVICE_2, DEVICE_2);
1181         _system(tmp, 1);
1182         OK_(crypt_init_by_name(&cd, CDEVICE_2));
1183         OK_(crypt_deactivate(cd, CDEVICE_2));
1184         FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "wrong volume key");
1185         crypt_free(cd);
1186
1187         // No slots
1188         OK_(crypt_init(&cd, DEVICE_2));
1189         OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
1190         FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "volume key is lost");
1191         crypt_free(cd);
1192
1193         // Plain device
1194         OK_(crypt_init(&cd, DEVICE_2));
1195         OK_(crypt_format(cd, CRYPT_PLAIN, "aes", "cbc-essiv:sha256", NULL, NULL, 16, NULL));
1196         FAIL_(crypt_activate_by_volume_key(cd, NULL, "xxx", 3, 0), "cannot verify key with plain");
1197         FAIL_(crypt_volume_key_verify(cd, "xxx", 3), "cannot verify key with plain");
1198         FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, "xxx", 3, 0), "wrong key lenght");
1199         OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, "volumekeyvolumek", 16, 0));
1200         EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
1201         OK_(crypt_deactivate(cd, CDEVICE_2));
1202         crypt_free(cd);
1203 }
1204
1205 static void HashDevicePlain(void)
1206 {
1207         struct crypt_device *cd;
1208         struct crypt_params_plain params = {
1209                 .hash = NULL,
1210                 .skip = 0,
1211                 .offset = 0,
1212         };
1213
1214         size_t key_size;
1215         char *mk_hex, *keystr, key[256];
1216
1217         OK_(crypt_init(&cd, DEVICE_1));
1218         OK_(crypt_format(cd, CRYPT_PLAIN, "aes", "cbc-essiv:sha256", NULL, NULL, 16, &params));
1219
1220         // hash PLAIN, short key
1221         OK_(_prepare_keyfile(KEYFILE1, "tooshort", 8));
1222         FAIL_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 16, 0), "not enough data in keyfile");
1223         _remove_keyfiles();
1224
1225         // hash PLAIN, exact key
1226         //         0 1 2 3 4 5 6 7 8 9 a b c d e f
1227         mk_hex = "caffeecaffeecaffeecaffeecaffee88";
1228         key_size = 16;
1229         crypt_decode_key(key, mk_hex, key_size);
1230         OK_(_prepare_keyfile(KEYFILE1, key, key_size));
1231         OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size, 0));
1232         OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1233         OK_(strcmp(key, mk_hex));
1234         OK_(crypt_deactivate(cd, CDEVICE_1));
1235
1236         // Limit plain key
1237         mk_hex = "caffeecaffeecaffeecaffeeca000000";
1238         OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size - 3, 0));
1239         OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1240         OK_(strcmp(key, mk_hex));
1241         OK_(crypt_deactivate(cd, CDEVICE_1));
1242
1243         _remove_keyfiles();
1244
1245         // hash PLAIN, long key
1246         //         0 1 2 3 4 5 6 7 8 9 a b c d e f
1247         mk_hex = "caffeecaffeecaffeecaffeecaffee88babebabe";
1248         key_size = 16;
1249         crypt_decode_key(key, mk_hex, key_size);
1250         OK_(_prepare_keyfile(KEYFILE1, key, strlen(mk_hex) / 2));
1251         OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size, 0));
1252         OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1253         FAIL_(strcmp(key, mk_hex), "only key length used");
1254         OK_(strncmp(key, mk_hex, key_size));
1255         OK_(crypt_deactivate(cd, CDEVICE_1));
1256
1257         // Now without explicit limit
1258         OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
1259         OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1260         FAIL_(strcmp(key, mk_hex), "only key length used");
1261         OK_(strncmp(key, mk_hex, key_size));
1262         OK_(crypt_deactivate(cd, CDEVICE_1));
1263
1264         _remove_keyfiles();
1265
1266         // hash sha256
1267         params.hash = "sha256";
1268         OK_(crypt_format(cd, CRYPT_PLAIN, "aes", "cbc-essiv:sha256", NULL, NULL, 16, &params));
1269
1270         //         0 1 2 3 4 5 6 7 8 9 a b c d e f
1271         mk_hex = "c62e4615bd39e222572f3a1bf7c2132e";
1272         keystr = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
1273         key_size = strlen(keystr); // 32
1274         OK_(_prepare_keyfile(KEYFILE1, keystr, strlen(keystr)));
1275         OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size, 0));
1276         OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1277         OK_(strcmp(key, mk_hex));
1278         OK_(crypt_deactivate(cd, CDEVICE_1));
1279
1280         // Read full keyfile
1281         OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
1282         OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1283         OK_(strcmp(key, mk_hex));
1284         OK_(crypt_deactivate(cd, CDEVICE_1));
1285
1286         _remove_keyfiles();
1287
1288         // Limit keyfile read
1289         keystr = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxAAAAAAAA";
1290         OK_(_prepare_keyfile(KEYFILE1, keystr, strlen(keystr)));
1291         OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size, 0));
1292         OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1293         OK_(strcmp(key, mk_hex));
1294         OK_(crypt_deactivate(cd, CDEVICE_1));
1295
1296         // Full keyfile
1297         OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
1298         OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
1299         OK_(strcmp(key, "0e49cb34a1dee1df33f6505e4de44a66"));
1300         OK_(crypt_deactivate(cd, CDEVICE_1));
1301
1302         _remove_keyfiles();
1303
1304         // FIXME: add keyfile="-" tests somehow
1305
1306         crypt_free(cd);
1307 }
1308
1309 // Check that gcrypt is properly initialised in format
1310 static void NonFIPSAlg(void)
1311 {
1312         struct crypt_device *cd;
1313         struct crypt_params_luks1 params = {0};
1314         char key[128] = "";
1315         size_t key_size = 128;
1316         char *cipher = "aes";
1317         char *cipher_mode = "cbc-essiv:sha256";
1318         int ret;
1319
1320         OK_(crypt_init(&cd, DEVICE_2));
1321         params.hash = "sha256";
1322         OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params));
1323
1324         params.hash = "whirlpool";
1325         ret = crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params);
1326         if (ret < 0) {
1327                 printf("WARNING: whirlpool not supported, skipping test.\n");
1328                 crypt_free(cd);
1329                 return;
1330         }
1331
1332         params.hash = "md5";
1333         FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params),
1334               "MD5 unsupported, too short");
1335         crypt_free(cd);
1336 }
1337
1338 int main (int argc, char *argv[])
1339 {
1340         int i;
1341
1342         if (getuid() != 0) {
1343                 printf("You must be root to run this test.\n");
1344                 exit(0);
1345         }
1346
1347         for (i = 1; i < argc; i++) {
1348                 if (!strcmp("-v", argv[i]) || !strcmp("--verbose", argv[i]))
1349                         _verbose = 1;
1350                 else if (!strcmp("--debug", argv[i]))
1351                         _debug = _verbose = 1;
1352         }
1353
1354         _cleanup();
1355         if (_setup())
1356                 goto out;
1357
1358         crypt_set_debug_level(_debug ? CRYPT_DEBUG_ALL : CRYPT_DEBUG_NONE);
1359
1360         RUN_(NonFIPSAlg, "Crypto is properly initialised in format"); //must be the first!
1361 #if 0
1362         RUN_(LuksUUID, "luksUUID API call");
1363         RUN_(IsLuks, "isLuks API call");
1364         RUN_(LuksOpen, "luksOpen API call");
1365         RUN_(query_device, "crypt_query_device API call");
1366         RUN_(remove_device, "crypt_remove_device API call");
1367         RUN_(LuksFormat, "luksFormat API call");
1368         RUN_(LuksKeyGame, "luksAddKey, RemoveKey, KillSlot API calls");
1369         RUN_(DeviceResizeGame, "regular crypto, resize calls");
1370 #endif
1371         RUN_(AddDevicePlain, "plain device API creation exercise");
1372         RUN_(HashDevicePlain, "plain device API hash test");
1373         RUN_(AddDeviceLuks, "Format and use LUKS device");
1374         RUN_(UseLuksDevice, "Use pre-formated LUKS device");
1375         RUN_(SuspendDevice, "Suspend/Resume test");
1376         RUN_(UseTempVolumes, "Format and use temporary encrypted device");
1377
1378         RUN_(CallbacksTest, "API callbacks test");
1379 out:
1380         _cleanup();
1381         return 0;
1382 }