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