Remove not needed header.
[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
31 #include "libcryptsetup.h"
32 #include "utils_loop.h"
33
34 #define DMDIR "/dev/mapper/"
35
36 #define DEVICE_1_UUID "28632274-8c8a-493f-835b-da802e1c576b"
37 #define DEVICE_EMPTY_name "crypt_zero"
38 #define DEVICE_EMPTY DMDIR DEVICE_EMPTY_name
39 #define DEVICE_ERROR_name "crypt_error"
40 #define DEVICE_ERROR DMDIR DEVICE_ERROR_name
41
42 #define CDEVICE_1 "ctest1"
43 #define CDEVICE_2 "ctest2"
44 #define CDEVICE_WRONG "O_o"
45
46 #define IMAGE1 "compatimage.img"
47 #define IMAGE_EMPTY "empty.img"
48
49 #define KEYFILE1 "key1.file"
50 #define KEY1 "compatkey"
51
52 #define KEYFILE2 "key2.file"
53 #define KEY2 "0123456789abcdef"
54
55 #define PASSPHRASE "blabla"
56
57 #define DEVICE_TEST_UUID "12345678-1234-1234-1234-123456789abc"
58
59 static int _debug   = 0;
60 static int _verbose = 1;
61
62 static char global_log[4096];
63 static int global_lines = 0;
64
65 static char *DEVICE_1 = NULL;
66 static char *DEVICE_2 = NULL;
67
68 // Helpers
69 static int _prepare_keyfile(const char *name, const char *passphrase)
70 {
71         int fd, r;
72
73         fd = open(name, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR);
74         if (fd != -1) {
75                 r = write(fd, passphrase, strlen(passphrase));
76                 close(fd);
77         } else
78                 r = 0;
79
80         return r == strlen(passphrase) ? 0 : 1;
81 }
82
83 static void _remove_keyfiles(void)
84 {
85         remove(KEYFILE1);
86         remove(KEYFILE2);
87 }
88
89 // Decode key from its hex representation
90 static int crypt_decode_key(char *key, char *hex, unsigned int size)
91 {
92         char buffer[3];
93         char *endp;
94         unsigned int i;
95
96         buffer[2] = '\0';
97
98         for (i = 0; i < size; i++) {
99                 buffer[0] = *hex++;
100                 buffer[1] = *hex++;
101
102                 key[i] = (unsigned char)strtoul(buffer, &endp, 16);
103
104                 if (endp != &buffer[2])
105                         return -1;
106         }
107
108         if (*hex != '\0')
109                 return -1;
110
111         return 0;
112 }
113
114 static int yesDialog(char *msg)
115 {
116         return 1;
117 }
118
119 static void cmdLineLog(int level, char *msg)
120 {
121         strncat(global_log, msg, sizeof(global_log) - strlen(global_log));
122         global_lines++;
123 }
124
125 static void new_log(int level, const char *msg, void *usrptr)
126 {
127         cmdLineLog(level, (char*)msg);
128 }
129
130
131 static void reset_log()
132 {
133         memset(global_log, 0, sizeof(global_log));
134         global_lines = 0;
135 }
136
137 static void _system(const char *command, int warn)
138 {
139         if (system(command) < 0 && warn)
140                 printf("System command failed: %s", command);
141 }
142
143 static struct interface_callbacks cmd_icb = {
144         .yesDialog = yesDialog,
145         .log = cmdLineLog,
146 };
147
148 static void _cleanup(void)
149 {
150         struct stat st;
151
152         //_system("udevadm settle", 0);
153
154         if (!stat(DMDIR CDEVICE_1, &st))
155                 _system("dmsetup remove " CDEVICE_1, 0);
156
157         if (!stat(DMDIR CDEVICE_2, &st))
158                 _system("dmsetup remove " CDEVICE_2, 0);
159
160         if (!stat(DEVICE_EMPTY, &st))
161                 _system("dmsetup remove " DEVICE_EMPTY_name, 0);
162
163         if (!stat(DEVICE_ERROR, &st))
164                 _system("dmsetup remove " DEVICE_ERROR_name, 0);
165
166         if (crypt_loop_device(DEVICE_1))
167                 crypt_loop_detach(DEVICE_1);
168
169         if (crypt_loop_device(DEVICE_2))
170                 crypt_loop_detach(DEVICE_2);
171
172         _system("rm -f " IMAGE_EMPTY, 0);
173         _remove_keyfiles();
174 }
175
176 static int _setup(void)
177 {
178         int fd, ro = 0;
179
180         _system("dmsetup create " DEVICE_EMPTY_name " --table \"0 10000 zero\"", 1);
181         _system("dmsetup create " DEVICE_ERROR_name " --table \"0 10000 error\"", 1);
182         if (!DEVICE_1)
183                 DEVICE_1 = crypt_loop_get_device();
184         if (!DEVICE_1) {
185                 printf("Cannot find free loop device.\n");
186                 return 1;
187         }
188         if (crypt_loop_device(DEVICE_1)) {
189                 _system(" [ ! -e " IMAGE1 " ] && bzip2 -dk " IMAGE1 ".bz2", 1);
190                 fd = crypt_loop_attach(DEVICE_1, IMAGE1, 0, 0, &ro);
191                 close(fd);
192         }
193         if (!DEVICE_2)
194                 DEVICE_2 = crypt_loop_get_device();
195         if (!DEVICE_2) {
196                 printf("Cannot find free loop device.\n");
197                 return 1;
198         }
199         if (crypt_loop_device(DEVICE_2)) {
200                 _system("dd if=/dev/zero of=" IMAGE_EMPTY " bs=1M count=4", 1);
201                 fd = crypt_loop_attach(DEVICE_2, IMAGE_EMPTY, 0, 0, &ro);
202                 close(fd);
203         }
204         return 0;
205 }
206
207 void check_ok(int status, int line, const char *func)
208 {
209         char buf[256];
210
211         if (status) {
212                 crypt_get_error(buf, sizeof(buf));
213                 printf("FAIL line %d [%s]: code %d, %s\n", line, func, status, buf);
214                 _cleanup();
215                 exit(-1);
216         }
217 }
218
219 void check_ko(int status, int line, const char *func)
220 {
221         char buf[256];
222
223         memset(buf, 0, sizeof(buf));
224         crypt_get_error(buf, sizeof(buf));
225         if (status >= 0) {
226                 printf("FAIL line %d [%s]: code %d, %s\n", line, func, status, buf);
227                 _cleanup();
228                 exit(-1);
229         } else if (_verbose)
230                 printf("   => errno %d, errmsg: %s\n", status, buf);
231 }
232
233 void check_equal(int line, const char *func)
234 {
235         printf("FAIL line %d [%s]: expected equal values differs.\n", line, func);
236         _cleanup();
237         exit(-1);
238 }
239
240 void xlog(const char *msg, const char *tst, const char *func, int line, const char *txt)
241 {
242         if (_verbose) {
243                 if (txt)
244                         printf(" [%s,%s:%d] %s [%s]\n", msg, func, line, tst, txt);
245                 else
246                         printf(" [%s,%s:%d] %s\n", msg, func, line, tst);
247         }
248 }
249 #define OK_(x)          do { xlog("(success)", #x, __FUNCTION__, __LINE__, NULL); \
250                              check_ok((x), __LINE__, __FUNCTION__); \
251                         } while(0)
252 #define FAIL_(x, y)     do { xlog("(fail)   ", #x, __FUNCTION__, __LINE__, y); \
253                              check_ko((x), __LINE__, __FUNCTION__); \
254                         } while(0)
255 #define EQ_(x, y)       do { xlog("(equal)  ", #x " == " #y, __FUNCTION__, __LINE__, NULL); \
256                              if ((x) != (y)) check_equal(__LINE__, __FUNCTION__); \
257                         } while(0)
258
259 #define RUN_(x, y)              do { printf("%s: %s\n", #x, (y)); x(); } while (0)
260
261 // OLD API TESTS
262 static void LuksUUID(void)
263 {
264         struct crypt_options co = { .icb = &cmd_icb };
265
266         co.device = DEVICE_EMPTY;
267         EQ_(crypt_luksUUID(&co), -EINVAL);
268
269         co.device = DEVICE_ERROR;
270         EQ_(crypt_luksUUID(&co), -EINVAL);
271
272         reset_log();
273         co.device = DEVICE_1;
274         OK_(crypt_luksUUID(&co));
275         EQ_(strlen(global_log), 37); /* UUID + "\n" */
276         EQ_(strncmp(global_log, DEVICE_1_UUID, strlen(DEVICE_1_UUID)), 0);
277
278 }
279
280 static void IsLuks(void)
281 {
282         struct crypt_options co = {  .icb = &cmd_icb };
283
284         co.device = DEVICE_EMPTY;
285         EQ_(crypt_isLuks(&co), -EINVAL);
286
287         co.device = DEVICE_ERROR;
288         EQ_(crypt_isLuks(&co), -EINVAL);
289
290         co.device = DEVICE_1;
291         OK_(crypt_isLuks(&co));
292 }
293
294 static void LuksOpen(void)
295 {
296         struct crypt_options co = {
297                 .name = CDEVICE_1,
298                 //.passphrase = "blabla",
299                 .icb = &cmd_icb,
300         };
301
302         OK_(_prepare_keyfile(KEYFILE1, KEY1));
303         co.key_file = KEYFILE1;
304
305         co.device = DEVICE_EMPTY;
306         EQ_(crypt_luksOpen(&co), -EINVAL);
307
308         co.device = DEVICE_ERROR;
309         EQ_(crypt_luksOpen(&co), -EINVAL);
310
311         co.device = DEVICE_1;
312         OK_(crypt_luksOpen(&co));
313         FAIL_(crypt_luksOpen(&co), "already open");
314
315         _remove_keyfiles();
316 }
317
318 static void query_device(void)
319 {
320         struct crypt_options co = {.icb = &cmd_icb };
321
322         co.name = CDEVICE_WRONG;
323         EQ_(crypt_query_device(&co), 0);
324
325         co.name = CDEVICE_1;
326         EQ_(crypt_query_device(&co), 1);
327
328         OK_(strncmp(crypt_get_dir(), DMDIR, 11));
329         OK_(strcmp(co.cipher, "aes-cbc-essiv:sha256"));
330         EQ_(co.key_size, 16);
331         EQ_(co.offset, 1032);
332         EQ_(co.flags & CRYPT_FLAG_READONLY, 0);
333         EQ_(co.skip, 0);
334         crypt_put_options(&co);
335 }
336
337 static void remove_device(void)
338 {
339         int fd;
340         struct crypt_options co = {.icb = &cmd_icb };
341
342         co.name = CDEVICE_WRONG;
343         EQ_(crypt_remove_device(&co), -ENODEV);
344
345         fd = open(DMDIR CDEVICE_1, O_RDONLY);
346         co.name = CDEVICE_1;
347         FAIL_(crypt_remove_device(&co), "device busy");
348         close(fd);
349
350         OK_(crypt_remove_device(&co));
351 }
352
353 static void LuksFormat(void)
354 {
355         struct crypt_options co = {
356                 .device = DEVICE_2,
357                 .key_size = 256 / 8,
358                 .key_slot = -1,
359                 .cipher = "aes-cbc-essiv:sha256",
360                 .hash = "sha1",
361                 .flags = 0,
362                 .iteration_time = 10,
363                 .align_payload = 0,
364                 .icb = &cmd_icb,
365         };
366
367         OK_(_prepare_keyfile(KEYFILE1, KEY1));
368
369         co.new_key_file = KEYFILE1;
370         co.device = DEVICE_ERROR;
371         FAIL_(crypt_luksFormat(&co), "error device");
372
373         co.device = DEVICE_2;
374         OK_(crypt_luksFormat(&co));
375
376         co.new_key_file = NULL;
377         co.key_file = KEYFILE1;
378         co.name = CDEVICE_2;
379         OK_(crypt_luksOpen(&co));
380         OK_(crypt_remove_device(&co));
381         _remove_keyfiles();
382 }
383
384 static void LuksKeyGame(void)
385 {
386         int i;
387         struct crypt_options co = {
388                 .device = DEVICE_2,
389                 .key_size = 256 / 8,
390                 .key_slot = -1,
391                 .cipher = "aes-cbc-essiv:sha256",
392                 .hash = "sha1",
393                 .flags = 0,
394                 .iteration_time = 10,
395                 .align_payload = 0,
396                 .icb = &cmd_icb,
397         };
398
399         OK_(_prepare_keyfile(KEYFILE1, KEY1));
400         OK_(_prepare_keyfile(KEYFILE2, KEY2));
401
402         co.new_key_file = KEYFILE1;
403         co.device = DEVICE_2;
404         co.key_slot = 8;
405         FAIL_(crypt_luksFormat(&co), "wrong slot #");
406
407         co.key_slot = 7; // last slot
408         OK_(crypt_luksFormat(&co));
409
410         co.new_key_file = KEYFILE1;
411         co.key_file = KEYFILE1;
412         co.key_slot = 8;
413         FAIL_(crypt_luksAddKey(&co), "wrong slot #");
414         co.key_slot = 7;
415         FAIL_(crypt_luksAddKey(&co), "slot already used");
416
417         co.key_slot = 6;
418         OK_(crypt_luksAddKey(&co));
419
420         co.key_file = KEYFILE2 "blah";
421         co.key_slot = 5;
422         FAIL_(crypt_luksAddKey(&co), "keyfile not found");
423
424         co.new_key_file = KEYFILE2; // key to add
425         co.key_file = KEYFILE1;
426         co.key_slot = -1;
427         for (i = 0; i < 6; i++)
428                 OK_(crypt_luksAddKey(&co)); //FIXME: EQ_(i)?
429
430         FAIL_(crypt_luksAddKey(&co), "all slots full");
431
432         // REMOVE KEY
433         co.new_key_file = KEYFILE1; // key to remove
434         co.key_file = NULL;
435         co.key_slot = 8; // should be ignored
436          // only 2 slots should use KEYFILE1
437         OK_(crypt_luksRemoveKey(&co));
438         OK_(crypt_luksRemoveKey(&co));
439         FAIL_(crypt_luksRemoveKey(&co), "no slot with this passphrase");
440
441         co.new_key_file = KEYFILE2 "blah";
442         co.key_file = NULL;
443         FAIL_(crypt_luksRemoveKey(&co), "keyfile not found");
444
445         // KILL SLOT
446         co.new_key_file = NULL;
447         co.key_file = NULL;
448         co.key_slot = 8;
449         FAIL_(crypt_luksKillSlot(&co), "wrong slot #");
450         co.key_slot = 7;
451         FAIL_(crypt_luksKillSlot(&co), "slot already wiped");
452
453         co.key_slot = 5;
454         OK_(crypt_luksKillSlot(&co));
455
456         _remove_keyfiles();
457 }
458
459 size_t _get_device_size(const char *device)
460 {
461         unsigned long size = 0;
462         int fd;
463
464         fd = open(device, O_RDONLY);
465         if (fd == -1)
466                 return 0;
467         (void)ioctl(fd, BLKGETSIZE, &size);
468         close(fd);
469
470         return size;
471 }
472
473 void DeviceResizeGame(void)
474 {
475         size_t orig_size;
476         struct crypt_options co = {
477                 .name = CDEVICE_2,
478                 .device = DEVICE_2,
479                 .key_size = 128 / 8,
480                 .cipher = "aes-cbc-plain",
481                 .hash = "sha1",
482                 .offset = 333,
483                 .skip = 0,
484                 .icb = &cmd_icb,
485         };
486
487         orig_size = _get_device_size(DEVICE_2);
488
489         OK_(_prepare_keyfile(KEYFILE2, KEY2));
490
491         co.key_file = KEYFILE2;
492         co.size = 1000;
493         OK_(crypt_create_device(&co));
494         EQ_(_get_device_size(DMDIR CDEVICE_2), 1000);
495
496         co.size = 2000;
497         OK_(crypt_resize_device(&co));
498         EQ_(_get_device_size(DMDIR CDEVICE_2), 2000);
499
500         co.size = 0;
501         OK_(crypt_resize_device(&co));
502         EQ_(_get_device_size(DMDIR CDEVICE_2), (orig_size - 333));
503         co.size = 0;
504         co.offset = 444;
505         co.skip = 555;
506         co.cipher = "aes-cbc-essiv:sha256";
507         OK_(crypt_update_device(&co));
508         EQ_(_get_device_size(DMDIR CDEVICE_2), (orig_size - 444));
509
510         memset(&co, 0, sizeof(co));
511         co.icb = &cmd_icb,
512         co.name = CDEVICE_2;
513         EQ_(crypt_query_device(&co), 1);
514         EQ_(strcmp(co.cipher, "aes-cbc-essiv:sha256"), 0);
515         EQ_(co.key_size, 128 / 8);
516         EQ_(co.offset, 444);
517         EQ_(co.skip, 555);
518         crypt_put_options(&co);
519
520         // dangerous switch device still works
521         memset(&co, 0, sizeof(co));
522         co.name = CDEVICE_2,
523         co.device = DEVICE_1;
524         co.key_file = KEYFILE2;
525         co.key_size = 128 / 8;
526         co.cipher = "aes-cbc-plain";
527         co.hash = "sha1";
528         co.icb = &cmd_icb;
529         OK_(crypt_update_device(&co));
530
531         memset(&co, 0, sizeof(co));
532         co.icb = &cmd_icb,
533         co.name = CDEVICE_2;
534         EQ_(crypt_query_device(&co), 1);
535         EQ_(strcmp(co.cipher, "aes-cbc-plain"), 0);
536         EQ_(co.key_size, 128 / 8);
537         EQ_(co.offset, 0);
538         EQ_(co.skip, 0);
539         // This expect lookup returns prefered /dev/loopX
540         EQ_(strcmp(co.device, DEVICE_1), 0);
541         crypt_put_options(&co);
542
543         memset(&co, 0, sizeof(co));
544         co.icb = &cmd_icb,
545         co.name = CDEVICE_2;
546         OK_(crypt_remove_device(&co));
547
548         _remove_keyfiles();
549 }
550
551 // NEW API tests
552
553 static void AddDevicePlain(void)
554 {
555         struct crypt_device *cd;
556         struct crypt_params_plain params = {
557                 .hash = "sha1",
558                 .skip = 0,
559                 .offset = 0,
560         };
561         int fd;
562         char key[128], key2[128], path[128];
563
564         char *passphrase = PASSPHRASE;
565         char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
566         size_t key_size = strlen(mk_hex) / 2;
567         char *cipher = "aes";
568         char *cipher_mode = "cbc-essiv:sha256";
569
570         crypt_decode_key(key, mk_hex, key_size);
571
572         FAIL_(crypt_init(&cd, ""), "empty device string");
573
574         // default is "plain" hash - no password hash
575         OK_(crypt_init(&cd, DEVICE_1));
576         OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, NULL));
577         FAIL_(crypt_activate_by_volume_key(cd, NULL, key, key_size, 0), "cannot verify key with plain");
578         OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
579         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
580         // FIXME: this should get key from active device?
581         //OK_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)));
582         //OK_(memcmp(key, key2, key_size));
583         OK_(crypt_deactivate(cd, CDEVICE_1));
584         crypt_free(cd);
585
586         // Now use hashed password
587         OK_(crypt_init(&cd, DEVICE_1));
588         OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, &params));
589         FAIL_(crypt_activate_by_passphrase(cd, NULL, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0),
590               "cannot verify passphrase with plain" );
591         OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
592
593         // device status check
594         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
595         snprintf(path, sizeof(path), "%s/%s", crypt_get_dir(), CDEVICE_1);
596         fd = open(path, O_RDONLY);
597         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_BUSY);
598         FAIL_(crypt_deactivate(cd, CDEVICE_1), "Device is busy");
599         close(fd);
600         OK_(crypt_deactivate(cd, CDEVICE_1));
601         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
602
603         OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
604         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
605
606         // retrieve volume key check
607         memset(key2, 0, key_size);
608         key_size--;
609         // small buffer
610         FAIL_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)), "small buffer");
611         key_size++;
612         OK_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)));
613
614         OK_(memcmp(key, key2, key_size));
615         OK_(strcmp(cipher, crypt_get_cipher(cd)));
616         OK_(strcmp(cipher_mode, crypt_get_cipher_mode(cd)));
617         EQ_(key_size, crypt_get_volume_key_size(cd));
618         EQ_(0, crypt_get_data_offset(cd));
619         OK_(crypt_deactivate(cd, CDEVICE_1));
620
621         // now with keyfile
622         OK_(_prepare_keyfile(KEYFILE1, KEY1));
623         FAIL_(crypt_activate_by_keyfile(cd, NULL, CRYPT_ANY_SLOT, KEYFILE1, 0, 0), "cannot verify key with plain");
624         EQ_(0, crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
625         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
626         OK_(crypt_deactivate(cd, CDEVICE_1));
627         _remove_keyfiles();
628
629         crypt_free(cd);
630 }
631
632 #define CALLBACK_ERROR "calback_error xyz"
633 static int pass_callback_err(const char *msg, char *buf, size_t length, void *usrptr)
634 {
635         struct crypt_device *cd = usrptr;
636
637         assert(cd);
638         assert(length);
639         assert(msg);
640
641         crypt_log(cd, CRYPT_LOG_ERROR, CALLBACK_ERROR);
642         return -EINVAL;
643 }
644
645 static int pass_callback_ok(const char *msg, char *buf, size_t length, void *usrptr)
646 {
647         assert(length);
648         assert(msg);
649         strcpy(buf, PASSPHRASE);
650         return strlen(buf);
651 }
652
653 static void CallbacksTest(void)
654 {
655         struct crypt_device *cd;
656         struct crypt_params_plain params = {
657                 .hash = "sha1",
658                 .skip = 0,
659                 .offset = 0,
660         };
661
662         size_t key_size = 256 / 8;
663         char *cipher = "aes";
664         char *cipher_mode = "cbc-essiv:sha256";
665         char *passphrase = PASSPHRASE;
666
667         OK_(crypt_init(&cd, DEVICE_1));
668         crypt_set_log_callback(cd, &new_log, NULL);
669         //crypt_set_log_callback(cd, NULL, NULL);
670
671         OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, &params));
672
673         OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
674         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
675         OK_(crypt_deactivate(cd, CDEVICE_1));
676
677         reset_log();
678         crypt_set_password_callback(cd, pass_callback_err, cd);
679         FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, NULL, 0, 0), "callback fails");
680         EQ_(strncmp(global_log, CALLBACK_ERROR, strlen(CALLBACK_ERROR)), 0);
681
682         crypt_set_password_callback(cd, pass_callback_ok, NULL);
683         OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, NULL, 0, 0));
684         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
685         OK_(crypt_deactivate(cd, CDEVICE_1));
686
687         crypt_free(cd);
688 }
689
690 static void UseLuksDevice(void)
691 {
692         struct crypt_device *cd;
693         char key[128];
694         size_t key_size;
695
696         OK_(crypt_init(&cd, DEVICE_1));
697         OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
698         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
699         OK_(crypt_activate_by_passphrase(cd, NULL, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0));
700         OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0));
701         FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0), "already open");
702         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
703         OK_(crypt_deactivate(cd, CDEVICE_1));
704         FAIL_(crypt_deactivate(cd, CDEVICE_1), "no such device");
705
706         key_size = 16;
707         OK_(strcmp("aes", crypt_get_cipher(cd)));
708         OK_(strcmp("cbc-essiv:sha256", crypt_get_cipher_mode(cd)));
709         OK_(strcmp(DEVICE_1_UUID, crypt_get_uuid(cd)));
710         EQ_(key_size, crypt_get_volume_key_size(cd));
711         EQ_(1032, crypt_get_data_offset(cd));
712
713         EQ_(0, crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key, &key_size, KEY1, strlen(KEY1)));
714         OK_(crypt_volume_key_verify(cd, key, key_size));
715         OK_(crypt_activate_by_volume_key(cd, NULL, key, key_size, 0));
716         OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
717         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
718         OK_(crypt_deactivate(cd, CDEVICE_1));
719
720         key[1] = ~key[1];
721         FAIL_(crypt_volume_key_verify(cd, key, key_size), "key mismatch");
722         FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0), "key mismatch");
723         crypt_free(cd);
724 }
725
726 static void SuspendDevice(void)
727 {
728         int suspend_status;
729         struct crypt_device *cd;
730
731         OK_(crypt_init(&cd, DEVICE_1));
732         OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
733         OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0));
734
735         suspend_status = crypt_suspend(cd, CDEVICE_1);
736         if (suspend_status == -ENOTSUP) {
737                 printf("WARNING: Suspend/Resume not supported, skipping test.\n");
738                 goto out;
739         }
740         OK_(suspend_status);
741         FAIL_(crypt_suspend(cd, CDEVICE_1), "already suspended");
742
743         FAIL_(crypt_resume_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1)-1), "wrong key");
744         OK_(crypt_resume_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1)));
745         FAIL_(crypt_resume_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1)), "not suspended");
746
747         OK_(_prepare_keyfile(KEYFILE1, KEY1));
748         OK_(crypt_suspend(cd, CDEVICE_1));
749         FAIL_(crypt_resume_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1 "blah", 0), "wrong keyfile");
750         OK_(crypt_resume_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0));
751         FAIL_(crypt_resume_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0), "not suspended");
752         _remove_keyfiles();
753 out:
754         OK_(crypt_deactivate(cd, CDEVICE_1));
755         crypt_free(cd);
756 }
757
758 static void AddDeviceLuks(void)
759 {
760         struct crypt_device *cd;
761         struct crypt_params_luks1 params = {
762                 .hash = "sha512",
763                 .data_alignment = 2048, // 4M, data offset will be 4096
764         };
765         char key[128], key2[128];
766
767         char *passphrase = "blabla";
768         char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
769         size_t key_size = strlen(mk_hex) / 2;
770         char *cipher = "aes";
771         char *cipher_mode = "cbc-essiv:sha256";
772
773         crypt_decode_key(key, mk_hex, key_size);
774
775         OK_(crypt_init(&cd, DEVICE_2));
776         OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params));
777
778         // even with no keyslots defined it can be activated by volume key
779         OK_(crypt_volume_key_verify(cd, key, key_size));
780         OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, key, key_size, 0));
781         EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
782         OK_(crypt_deactivate(cd, CDEVICE_2));
783
784         // now with keyslot
785         EQ_(7, crypt_keyslot_add_by_volume_key(cd, 7, key, key_size, passphrase, strlen(passphrase)));
786         EQ_(CRYPT_SLOT_ACTIVE_LAST, crypt_keyslot_status(cd, 7));
787         EQ_(7, crypt_activate_by_passphrase(cd, CDEVICE_2, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
788         EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
789         OK_(crypt_deactivate(cd, CDEVICE_2));
790
791         EQ_(1, crypt_keyslot_add_by_volume_key(cd, 1, key, key_size, KEY1, strlen(KEY1)));
792         OK_(_prepare_keyfile(KEYFILE1, KEY1));
793         OK_(_prepare_keyfile(KEYFILE2, KEY2));
794         EQ_(2, crypt_keyslot_add_by_keyfile(cd, 2, KEYFILE1, 0, KEYFILE2, 0));
795         FAIL_(crypt_activate_by_keyfile(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, strlen(KEY2)-1, 0), "key mismatch");
796         EQ_(2, crypt_activate_by_keyfile(cd, NULL, CRYPT_ANY_SLOT, KEYFILE2, 0, 0));
797         EQ_(2, crypt_activate_by_keyfile(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, 0, 0));
798         OK_(crypt_keyslot_destroy(cd, 1));
799         OK_(crypt_keyslot_destroy(cd, 2));
800         OK_(crypt_deactivate(cd, CDEVICE_2));
801         _remove_keyfiles();
802
803         FAIL_(crypt_keyslot_add_by_volume_key(cd, 7, key, key_size, passphrase, strlen(passphrase)), "slot used");
804         key[1] = ~key[1];
805         FAIL_(crypt_keyslot_add_by_volume_key(cd, 6, key, key_size, passphrase, strlen(passphrase)), "key mismatch");
806         key[1] = ~key[1];
807         EQ_(6, crypt_keyslot_add_by_volume_key(cd, 6, key, key_size, passphrase, strlen(passphrase)));
808         EQ_(CRYPT_SLOT_ACTIVE, crypt_keyslot_status(cd, 6));
809
810         FAIL_(crypt_keyslot_destroy(cd, 8), "invalid keyslot");
811         FAIL_(crypt_keyslot_destroy(cd, CRYPT_ANY_SLOT), "invalid keyslot");
812         FAIL_(crypt_keyslot_destroy(cd, 0), "keyslot not used");
813         OK_(crypt_keyslot_destroy(cd, 7));
814         EQ_(CRYPT_SLOT_INACTIVE, crypt_keyslot_status(cd, 7));
815         EQ_(CRYPT_SLOT_ACTIVE_LAST, crypt_keyslot_status(cd, 6));
816
817         EQ_(6, crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)));
818         OK_(crypt_volume_key_verify(cd, key2, key_size));
819
820         OK_(memcmp(key, key2, key_size));
821         OK_(strcmp(cipher, crypt_get_cipher(cd)));
822         OK_(strcmp(cipher_mode, crypt_get_cipher_mode(cd)));
823         EQ_(key_size, crypt_get_volume_key_size(cd));
824         EQ_(4096, crypt_get_data_offset(cd));
825         OK_(strcmp(DEVICE_2, crypt_get_device_name(cd)));
826
827         reset_log();
828         crypt_set_log_callback(cd, &new_log, NULL);
829         OK_(crypt_dump(cd));
830         OK_(!(global_lines != 0));
831         crypt_set_log_callback(cd, NULL, NULL);
832         reset_log();
833
834         FAIL_(crypt_set_uuid(cd, "blah"), "wrong UUID format");
835         OK_(crypt_set_uuid(cd, DEVICE_TEST_UUID));
836         OK_(strcmp(DEVICE_TEST_UUID, crypt_get_uuid(cd)));
837
838         FAIL_(crypt_deactivate(cd, CDEVICE_2), "not active");
839         crypt_free(cd);
840 }
841
842 static void UseTempVolumes(void)
843 {
844         struct crypt_device *cd;
845         char tmp[256];
846
847         // Tepmporary device without keyslot but with on-disk LUKS header
848         OK_(crypt_init(&cd, DEVICE_2));
849         FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "not yet formatted");
850         OK_(crypt_format(cd, CRYPT_LUKS1, "aes", "cbc-essiv:sha256", NULL, NULL, 16, NULL));
851         OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0));
852         EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
853         crypt_free(cd);
854
855         // Volume key is properly initialised from active device
856         OK_(crypt_init_by_name(&cd, CDEVICE_2));
857         OK_(crypt_deactivate(cd, CDEVICE_2));
858         OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0));
859         OK_(crypt_deactivate(cd, CDEVICE_2));
860         crypt_free(cd);
861
862         // Dirty checks: device without UUID
863         // we should be able to remove it but not manuipulate with it
864         snprintf(tmp, sizeof(tmp), "dmsetup create %s --table \""
865                 "0 100 crypt aes-cbc-essiv:sha256 deadbabedeadbabedeadbabedeadbabe 0 "
866                 "%s 2048\"", CDEVICE_2, DEVICE_2);
867         _system(tmp, 1);
868         OK_(crypt_init_by_name(&cd, CDEVICE_2));
869         OK_(crypt_deactivate(cd, CDEVICE_2));
870         FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "No known device type");
871         crypt_free(cd);
872
873         // Dirty checks: device with UUID but LUKS header key fingerprint must fail)
874         snprintf(tmp, sizeof(tmp), "dmsetup create %s --table \""
875                 "0 100 crypt aes-cbc-essiv:sha256 deadbabedeadbabedeadbabedeadbabe 0 "
876                 "%s 2048\" -u CRYPT-LUKS1-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-ctest1",
877                  CDEVICE_2, DEVICE_2);
878         _system(tmp, 1);
879         OK_(crypt_init_by_name(&cd, CDEVICE_2));
880         OK_(crypt_deactivate(cd, CDEVICE_2));
881         FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "wrong volume key");
882         crypt_free(cd);
883
884         // No slots
885         OK_(crypt_init(&cd, DEVICE_2));
886         OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
887         FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "volume key is lost");
888         crypt_free(cd);
889
890         // Plain device
891         OK_(crypt_init(&cd, DEVICE_2));
892         OK_(crypt_format(cd, CRYPT_PLAIN, "aes", "cbc-essiv:sha256", NULL, NULL, 16, NULL));
893         FAIL_(crypt_activate_by_volume_key(cd, NULL, "xxx", 3, 0), "cannot verify key with plain");
894         FAIL_(crypt_volume_key_verify(cd, "xxx", 3), "cannot verify key with plain");
895         FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, "xxx", 3, 0), "wrong key lenght");
896         OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, "volumekeyvolumek", 16, 0));
897         EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
898         OK_(crypt_deactivate(cd, CDEVICE_2));
899         crypt_free(cd);
900 }
901
902 // Check that gcrypt is properly initialised in format
903 static void NonFIPSAlg(void)
904 {
905         struct crypt_device *cd;
906         struct crypt_params_luks1 params = {0};
907         char key[128] = "";
908         size_t key_size = 128;
909         char *cipher = "aes";
910         char *cipher_mode = "cbc-essiv:sha256";
911         int ret;
912
913         OK_(crypt_init(&cd, DEVICE_2));
914         params.hash = "sha256";
915         OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params));
916
917         params.hash = "whirlpool";
918         ret = crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params);
919         if (ret < 0) {
920                 printf("WARNING: whirlpool not supported, skipping test.\n");
921                 crypt_free(cd);
922                 return;
923         }
924
925         params.hash = "md5";
926         FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params),
927               "MD5 unsupported, too short");
928         crypt_free(cd);
929 }
930
931 int main (int argc, char *argv[])
932 {
933         int i;
934
935         if (getuid() != 0) {
936                 printf("You must be root to run this test.\n");
937                 exit(0);
938         }
939
940         for (i = 1; i < argc; i++) {
941                 if (!strcmp("-v", argv[i]) || !strcmp("--verbose", argv[i]))
942                         _verbose = 1;
943                 else if (!strcmp("--debug", argv[i]))
944                         _debug = _verbose = 1;
945         }
946
947         _cleanup();
948         if (_setup())
949                 goto out;
950
951         crypt_set_debug_level(_debug ? CRYPT_DEBUG_ALL : CRYPT_DEBUG_NONE);
952
953         RUN_(NonFIPSAlg, "Crypto is properly initialised in format"); //must be the first!
954
955         RUN_(LuksUUID, "luksUUID API call");
956         RUN_(IsLuks, "isLuks API call");
957         RUN_(LuksOpen, "luksOpen API call");
958         RUN_(query_device, "crypt_query_device API call");
959         RUN_(remove_device, "crypt_remove_device API call");
960         RUN_(LuksFormat, "luksFormat API call");
961         RUN_(LuksKeyGame, "luksAddKey, RemoveKey, KillSlot API calls");
962         RUN_(DeviceResizeGame, "regular crypto, resize calls");
963
964         RUN_(AddDevicePlain, "plain device API creation exercise");
965         RUN_(AddDeviceLuks, "Format and use LUKS device");
966         RUN_(UseLuksDevice, "Use pre-formated LUKS device");
967         RUN_(SuspendDevice, "Suspend/Resume test");
968         RUN_(UseTempVolumes, "Format and use temporary encrypted device");
969
970         RUN_(CallbacksTest, "API callbacks test");
971 out:
972         _cleanup();
973         return 0;
974 }