Fix crypt_activate_by_keyfile() to work with PLAIN devices.
[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
605         // now with keyfile
606         OK_(_prepare_keyfile(KEYFILE1, KEY1));
607         FAIL_(crypt_activate_by_keyfile(cd, NULL, CRYPT_ANY_SLOT, KEYFILE1, 0, 0), "cannot verify key with plain");
608         EQ_(0, crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
609         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
610         OK_(crypt_deactivate(cd, CDEVICE_1));
611         _remove_keyfiles();
612
613         crypt_free(cd);
614 }
615
616 #define CALLBACK_ERROR "calback_error xyz"
617 static int pass_callback_err(const char *msg, char *buf, size_t length, void *usrptr)
618 {
619         struct crypt_device *cd = usrptr;
620
621         assert(cd);
622         assert(length);
623         assert(msg);
624
625         crypt_log(cd, CRYPT_LOG_ERROR, CALLBACK_ERROR);
626         return -EINVAL;
627 }
628
629 static int pass_callback_ok(const char *msg, char *buf, size_t length, void *usrptr)
630 {
631         assert(length);
632         assert(msg);
633         strcpy(buf, PASSPHRASE);
634         return strlen(buf);
635 }
636
637 static void CallbacksTest(void)
638 {
639         struct crypt_device *cd;
640         struct crypt_params_plain params = {
641                 .hash = "sha1",
642                 .skip = 0,
643                 .offset = 0,
644         };
645
646         size_t key_size = 256 / 8;
647         char *cipher = "aes";
648         char *cipher_mode = "cbc-essiv:sha256";
649         char *passphrase = PASSPHRASE;
650
651         OK_(crypt_init(&cd, DEVICE_1));
652         crypt_set_log_callback(cd, &new_log, NULL);
653         //crypt_set_log_callback(cd, NULL, NULL);
654
655         OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, &params));
656
657         OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
658         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
659         OK_(crypt_deactivate(cd, CDEVICE_1));
660
661         reset_log();
662         crypt_set_password_callback(cd, pass_callback_err, cd);
663         FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, NULL, 0, 0), "callback fails");
664         EQ_(strncmp(global_log, CALLBACK_ERROR, strlen(CALLBACK_ERROR)), 0);
665
666         crypt_set_password_callback(cd, pass_callback_ok, NULL);
667         OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, NULL, 0, 0));
668         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
669         OK_(crypt_deactivate(cd, CDEVICE_1));
670
671         crypt_free(cd);
672 }
673
674 static void UseLuksDevice(void)
675 {
676         struct crypt_device *cd;
677         char key[128];
678         size_t key_size;
679
680         OK_(crypt_init(&cd, DEVICE_1));
681         OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
682         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
683         OK_(crypt_activate_by_passphrase(cd, NULL, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0));
684         OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0));
685         FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0), "already open");
686         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
687         OK_(crypt_deactivate(cd, CDEVICE_1));
688         FAIL_(crypt_deactivate(cd, CDEVICE_1), "no such device");
689
690         key_size = 16;
691         OK_(strcmp("aes", crypt_get_cipher(cd)));
692         OK_(strcmp("cbc-essiv:sha256", crypt_get_cipher_mode(cd)));
693         OK_(strcmp(DEVICE_1_UUID, crypt_get_uuid(cd)));
694         EQ_(key_size, crypt_get_volume_key_size(cd));
695         EQ_(1032, crypt_get_data_offset(cd));
696
697         EQ_(0, crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key, &key_size, KEY1, strlen(KEY1)));
698         OK_(crypt_volume_key_verify(cd, key, key_size));
699         OK_(crypt_activate_by_volume_key(cd, NULL, key, key_size, 0));
700         OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
701         EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
702         OK_(crypt_deactivate(cd, CDEVICE_1));
703
704         key[1] = ~key[1];
705         FAIL_(crypt_volume_key_verify(cd, key, key_size), "key mismatch");
706         FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0), "key mismatch");
707         crypt_free(cd);
708 }
709
710 static void SuspendDevice(void)
711 {
712         int suspend_status;
713         struct crypt_device *cd;
714
715         OK_(crypt_init(&cd, DEVICE_1));
716         OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
717         OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1), 0));
718
719         suspend_status = crypt_suspend(cd, CDEVICE_1);
720         if (suspend_status == -ENOTSUP) {
721                 printf("WARNING: Suspend/Resume not supported, skipping test.\n");
722                 goto out;
723         }
724         OK_(suspend_status);
725         FAIL_(crypt_suspend(cd, CDEVICE_1), "already suspended");
726
727         FAIL_(crypt_resume_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1)-1), "wrong key");
728         OK_(crypt_resume_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1)));
729         FAIL_(crypt_resume_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1)), "not suspended");
730
731         OK_(_prepare_keyfile(KEYFILE1, KEY1));
732         OK_(crypt_suspend(cd, CDEVICE_1));
733         FAIL_(crypt_resume_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1 "blah", 0), "wrong keyfile");
734         OK_(crypt_resume_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0));
735         FAIL_(crypt_resume_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0), "not suspended");
736         _remove_keyfiles();
737 out:
738         OK_(crypt_deactivate(cd, CDEVICE_1));
739         crypt_free(cd);
740 }
741
742 static void AddDeviceLuks(void)
743 {
744         struct crypt_device *cd;
745         struct crypt_params_luks1 params = {
746                 .hash = "sha512",
747                 .data_alignment = 2048, // 4M, data offset will be 4096
748         };
749         char key[128], key2[128];
750
751         char *passphrase = "blabla";
752         char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
753         size_t key_size = strlen(mk_hex) / 2;
754         char *cipher = "aes";
755         char *cipher_mode = "cbc-essiv:sha256";
756
757         crypt_decode_key(key, mk_hex, key_size);
758
759         OK_(crypt_init(&cd, DEVICE_2));
760         OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params));
761
762         // even with no keyslots defined it can be activated by volume key
763         OK_(crypt_volume_key_verify(cd, key, key_size));
764         OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, key, key_size, 0));
765         EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
766         OK_(crypt_deactivate(cd, CDEVICE_2));
767
768         // now with keyslot
769         EQ_(7, crypt_keyslot_add_by_volume_key(cd, 7, key, key_size, passphrase, strlen(passphrase)));
770         EQ_(CRYPT_SLOT_ACTIVE_LAST, crypt_keyslot_status(cd, 7));
771         EQ_(7, crypt_activate_by_passphrase(cd, CDEVICE_2, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
772         EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
773         OK_(crypt_deactivate(cd, CDEVICE_2));
774
775         EQ_(1, crypt_keyslot_add_by_volume_key(cd, 1, key, key_size, KEY1, strlen(KEY1)));
776         OK_(_prepare_keyfile(KEYFILE1, KEY1));
777         OK_(_prepare_keyfile(KEYFILE2, KEY2));
778         EQ_(2, crypt_keyslot_add_by_keyfile(cd, 2, KEYFILE1, 0, KEYFILE2, 0));
779         FAIL_(crypt_activate_by_keyfile(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, strlen(KEY2)-1, 0), "key mismatch");
780         EQ_(2, crypt_activate_by_keyfile(cd, NULL, CRYPT_ANY_SLOT, KEYFILE2, 0, 0));
781         EQ_(2, crypt_activate_by_keyfile(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, 0, 0));
782         OK_(crypt_keyslot_destroy(cd, 1));
783         OK_(crypt_keyslot_destroy(cd, 2));
784         OK_(crypt_deactivate(cd, CDEVICE_2));
785         _remove_keyfiles();
786
787         FAIL_(crypt_keyslot_add_by_volume_key(cd, 7, key, key_size, passphrase, strlen(passphrase)), "slot used");
788         key[1] = ~key[1];
789         FAIL_(crypt_keyslot_add_by_volume_key(cd, 6, key, key_size, passphrase, strlen(passphrase)), "key mismatch");
790         key[1] = ~key[1];
791         EQ_(6, crypt_keyslot_add_by_volume_key(cd, 6, key, key_size, passphrase, strlen(passphrase)));
792         EQ_(CRYPT_SLOT_ACTIVE, crypt_keyslot_status(cd, 6));
793
794         FAIL_(crypt_keyslot_destroy(cd, 8), "invalid keyslot");
795         FAIL_(crypt_keyslot_destroy(cd, CRYPT_ANY_SLOT), "invalid keyslot");
796         FAIL_(crypt_keyslot_destroy(cd, 0), "keyslot not used");
797         OK_(crypt_keyslot_destroy(cd, 7));
798         EQ_(CRYPT_SLOT_INACTIVE, crypt_keyslot_status(cd, 7));
799         EQ_(CRYPT_SLOT_ACTIVE_LAST, crypt_keyslot_status(cd, 6));
800
801         EQ_(6, crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)));
802         OK_(crypt_volume_key_verify(cd, key2, key_size));
803
804         OK_(memcmp(key, key2, key_size));
805         OK_(strcmp(cipher, crypt_get_cipher(cd)));
806         OK_(strcmp(cipher_mode, crypt_get_cipher_mode(cd)));
807         EQ_(key_size, crypt_get_volume_key_size(cd));
808         EQ_(4096, crypt_get_data_offset(cd));
809         OK_(strcmp(DEVICE_2, crypt_get_device_name(cd)));
810
811         reset_log();
812         crypt_set_log_callback(cd, &new_log, NULL);
813         OK_(crypt_dump(cd));
814         OK_(!(global_lines != 0));
815         crypt_set_log_callback(cd, NULL, NULL);
816         reset_log();
817
818         FAIL_(crypt_set_uuid(cd, "blah"), "wrong UUID format");
819         OK_(crypt_set_uuid(cd, DEVICE_TEST_UUID));
820         OK_(strcmp(DEVICE_TEST_UUID, crypt_get_uuid(cd)));
821
822         FAIL_(crypt_deactivate(cd, CDEVICE_2), "not active");
823         crypt_free(cd);
824 }
825
826 static void UseTempVolumes(void)
827 {
828         struct crypt_device *cd;
829
830         // Tepmporary device without keyslot but with on-disk LUKS header
831         OK_(crypt_init(&cd, DEVICE_2));
832         FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "not yet formatted");
833         OK_(crypt_format(cd, CRYPT_LUKS1, "aes", "cbc-essiv:sha256", NULL, NULL, 16, NULL));
834         OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0));
835         EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
836         crypt_free(cd);
837
838         // Volume key is properly initialised from active device
839         OK_(crypt_init_by_name(&cd, CDEVICE_2));
840         OK_(crypt_deactivate(cd, CDEVICE_2));
841         OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0));
842         OK_(crypt_deactivate(cd, CDEVICE_2));
843         crypt_free(cd);
844
845         // Dirty checks: device without UUID
846         // we should be able to remove it but not manuipulate with it
847         _system("dmsetup create " CDEVICE_2 " --table \""
848                 "0 100 crypt aes-cbc-essiv:sha256 deadbabedeadbabedeadbabedeadbabe 0 "
849                 DEVICE_2 " 2048\"", 1);
850         OK_(crypt_init_by_name(&cd, CDEVICE_2));
851         OK_(crypt_deactivate(cd, CDEVICE_2));
852         FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "No known device type");
853         crypt_free(cd);
854
855         // Dirty checks: device with UUID but LUKS header key fingerprint must fail)
856         _system("dmsetup create " CDEVICE_2 " --table \""
857                 "0 100 crypt aes-cbc-essiv:sha256 deadbabedeadbabedeadbabedeadbabe 0 "
858                 DEVICE_2 " 2048\" "
859                 "-u CRYPT-LUKS1-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-ctest1", 1);
860         OK_(crypt_init_by_name(&cd, CDEVICE_2));
861         OK_(crypt_deactivate(cd, CDEVICE_2));
862         FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "wrong volume key");
863         crypt_free(cd);
864
865         // No slots
866         OK_(crypt_init(&cd, DEVICE_2));
867         OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
868         FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, NULL, 0, 0), "volume key is lost");
869         crypt_free(cd);
870
871         // Plain device
872         OK_(crypt_init(&cd, DEVICE_2));
873         OK_(crypt_format(cd, CRYPT_PLAIN, "aes", "cbc-essiv:sha256", NULL, NULL, 16, NULL));
874         FAIL_(crypt_activate_by_volume_key(cd, NULL, "xxx", 3, 0), "cannot verify key with plain");
875         FAIL_(crypt_volume_key_verify(cd, "xxx", 3), "cannot verify key with plain");
876         FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, "xxx", 3, 0), "wrong key lenght");
877         OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, "volumekeyvolumek", 16, 0));
878         EQ_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
879         OK_(crypt_deactivate(cd, CDEVICE_2));
880         crypt_free(cd);
881 }
882
883 // Check that gcrypt is properly initialised in format
884 static void NonFIPSAlg(void)
885 {
886         struct crypt_device *cd;
887         struct crypt_params_luks1 params = {
888                 .hash = "whirlpool",
889         };
890         char key[128] = "";
891         size_t key_size = 128;
892         char *cipher = "aes";
893         char *cipher_mode = "cbc-essiv:sha256";
894
895         if (!gcrypt_compatible) {
896                 printf("WARNING: old libgcrypt, skipping test.\n");
897                 return;
898         }
899         OK_(crypt_init(&cd, DEVICE_2));
900         OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params));
901
902         params.hash = "md5";
903         FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params),
904               "MD5 unsupported, too short");
905         crypt_free(cd);
906 }
907
908
909 static void _gcrypt_compatible()
910 {
911         int maj, min, patch;
912         FILE *f;
913
914         if (!(f = popen("libgcrypt-config --version", "r")))
915                 return;
916
917         if (fscanf(f, "%d.%d.%d", &maj, &min, &patch) == 3 &&
918             maj >= 1 && min >= 4)
919                 gcrypt_compatible = 1;
920         if (_debug)
921                 printf("libgcrypt version %d.%d.%d detected.\n", maj, min, patch);
922
923         (void)fclose(f);
924         return;
925 }
926
927 int main (int argc, char *argv[])
928 {
929         int i;
930
931         if (getuid() != 0) {
932                 printf("You must be root to run this test.\n");
933                 exit(0);
934         }
935
936         for (i = 1; i < argc; i++) {
937                 if (!strcmp("-v", argv[i]) || !strcmp("--verbose", argv[i]))
938                         _verbose = 1;
939                 else if (!strcmp("--debug", argv[i]))
940                         _debug = _verbose = 1;
941         }
942
943         _cleanup();
944         _setup();
945         _gcrypt_compatible();
946
947         crypt_set_debug_level(_debug ? CRYPT_DEBUG_ALL : CRYPT_DEBUG_NONE);
948
949         RUN_(NonFIPSAlg, "Crypto is properly initialised in format"); //must be the first!
950
951         RUN_(LuksUUID, "luksUUID API call");
952         RUN_(IsLuks, "isLuks API call");
953         RUN_(LuksOpen, "luksOpen API call");
954         RUN_(query_device, "crypt_query_device API call");
955         RUN_(remove_device, "crypt_remove_device API call");
956         RUN_(LuksFormat, "luksFormat API call");
957         RUN_(LuksKeyGame, "luksAddKey, RemoveKey, KillSlot API calls");
958         RUN_(DeviceResizeGame, "regular crypto, resize calls");
959
960         RUN_(AddDevicePlain, "plain device API creation exercise");
961         RUN_(AddDeviceLuks, "Format and use LUKS device");
962         RUN_(UseLuksDevice, "Use pre-formated LUKS device");
963         RUN_(SuspendDevice, "Suspend/Resume test");
964         RUN_(UseTempVolumes, "Format and use temporary encrypted device");
965
966         RUN_(CallbacksTest, "API callbacks test");
967
968         _cleanup();
969         return 0;
970 }