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