2 * cryptsetup library API check functions
4 * Copyright (C) 2009 Red Hat, Inc. All rights reserved.
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.
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.
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
28 #include "libcryptsetup.h"
30 #define DMDIR "/dev/mapper/"
32 #define DEVICE_1 "/dev/loop5"
33 #define DEVICE_1_UUID "28632274-8c8a-493f-835b-da802e1c576b"
34 #define DEVICE_2 "/dev/loop6"
35 #define DEVICE_EMPTY_name "crypt_zero"
36 #define DEVICE_EMPTY DMDIR DEVICE_EMPTY_name
37 #define DEVICE_ERROR_name "crypt_error"
38 #define DEVICE_ERROR DMDIR DEVICE_ERROR_name
40 #define CDEVICE_1 "ctest1"
41 #define CDEVICE_2 "ctest2"
42 #define CDEVICE_WRONG "O_o"
44 #define IMAGE1 "compatimage.img"
45 #define IMAGE_EMPTY "empty.img"
47 #define KEYFILE1 "key1.file"
48 #define KEY1 "compatkey"
50 #define KEYFILE2 "key2.file"
51 #define KEY2 "0123456789abcdef"
53 static int _debug = 0;
54 static int _verbose = 1;
56 static char global_log[4096];
59 static int _prepare_keyfile(const char *name, const char *passphrase)
63 fd = open(name, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR);
65 r = write(fd, passphrase, strlen(passphrase));
70 return r == strlen(passphrase) ? 0 : 1;
73 static void _remove_keyfiles(void)
79 static int yesDialog(char *msg)
84 static void cmdLineLog(int class, char *msg)
86 strncat(global_log, msg, sizeof(global_log));
89 static void reset_log()
91 memset(global_log, 0, sizeof(global_log));
94 static struct interface_callbacks cmd_icb = {
95 .yesDialog = yesDialog,
99 static void _cleanup(void)
103 //system("udevadm settle");
105 if (!stat(DMDIR CDEVICE_1, &st))
106 system("dmsetup remove " CDEVICE_1);
108 if (!stat(DMDIR CDEVICE_2, &st))
109 system("dmsetup remove " CDEVICE_2);
111 if (!stat(DEVICE_EMPTY, &st))
112 system("dmsetup remove " DEVICE_EMPTY_name);
114 if (!stat(DEVICE_ERROR, &st))
115 system("dmsetup remove " DEVICE_ERROR_name);
117 if (!strncmp("/dev/loop", DEVICE_1, 9))
118 system("losetup -d " DEVICE_1);
120 if (!strncmp("/dev/loop", DEVICE_2, 9))
121 system("losetup -d " DEVICE_2);
123 system("rm -f " IMAGE_EMPTY);
127 static void _setup(void)
129 system("dmsetup create " DEVICE_EMPTY_name " --table \"0 10000 zero\"");
130 system("dmsetup create " DEVICE_ERROR_name " --table \"0 10000 error\"");
131 if (!strncmp("/dev/loop", DEVICE_1, 9))
132 system("losetup " DEVICE_1 " " IMAGE1);
133 if (!strncmp("/dev/loop", DEVICE_2, 9)) {
134 system("dd if=/dev/zero of=" IMAGE_EMPTY " bs=1M count=4");
135 system("losetup " DEVICE_2 " " IMAGE_EMPTY);
140 void check_ok(int status, int line, const char *func)
145 crypt_get_error(buf, sizeof(buf));
146 printf("FAIL line %d [%s]: code %d, %s\n", line, func, status, buf);
152 void check_ko(int status, int line, const char *func)
156 memset(buf, 0, sizeof(buf));
157 crypt_get_error(buf, sizeof(buf));
159 printf("FAIL line %d [%s]: code %d, %s\n", line, func, status, buf);
163 printf(" => errno %d, errmsg: %s\n", status, buf);
166 void check_equal(int line, const char *func)
168 printf("FAIL line %d [%s]: expected equal values differs.\n", line, func);
173 void xlog(const char *msg, const char *tst, const char *func, int line, const char *txt)
177 printf(" [%s,%s:%d] %s [%s]\n", msg, func, line, tst, txt);
179 printf(" [%s,%s:%d] %s\n", msg, func, line, tst);
182 #define OK_(x) do { xlog("(success)", #x, __FUNCTION__, __LINE__, NULL); \
183 check_ok((x), __LINE__, __FUNCTION__); \
185 #define FAIL_(x, y) do { xlog("(fail) ", #x, __FUNCTION__, __LINE__, y); \
186 check_ko((x), __LINE__, __FUNCTION__); \
188 #define EQ_(x, y) do { xlog("(equal) ", #x " == " #y, __FUNCTION__, __LINE__, NULL); \
189 if ((x) != (y)) check_equal(__LINE__, __FUNCTION__); \
192 #define RUN_(x, y) do { printf("%s: %s\n", #x, (y)); x(); } while (0)
195 static void LuksUUID(void)
197 struct crypt_options co = { .icb = &cmd_icb };
199 co.device = DEVICE_EMPTY;
200 EQ_(crypt_luksUUID(&co), -EINVAL);
202 co.device = DEVICE_ERROR;
203 EQ_(crypt_luksUUID(&co), -EINVAL);
206 co.device = DEVICE_1;
207 OK_(crypt_luksUUID(&co));
208 EQ_(strlen(global_log), 37); /* UUID + "\n" */
209 EQ_(strncmp(global_log, DEVICE_1_UUID, strlen(DEVICE_1_UUID)), 0);
213 static void IsLuks(void)
215 struct crypt_options co = { .icb = &cmd_icb };
217 co.device = DEVICE_EMPTY;
218 EQ_(crypt_isLuks(&co), -EINVAL);
220 co.device = DEVICE_ERROR;
221 EQ_(crypt_isLuks(&co), -EINVAL);
223 co.device = DEVICE_1;
224 OK_(crypt_isLuks(&co));
227 static void LuksOpen(void)
229 struct crypt_options co = {
231 //.passphrase = "blabla",
235 OK_(_prepare_keyfile(KEYFILE1, KEY1));
236 co.key_file = KEYFILE1;
238 co.device = DEVICE_EMPTY;
239 EQ_(crypt_luksOpen(&co), -EINVAL);
241 co.device = DEVICE_ERROR;
242 EQ_(crypt_luksOpen(&co), -EINVAL);
244 co.device = DEVICE_1;
245 OK_(crypt_luksOpen(&co));
246 FAIL_(crypt_luksOpen(&co), "already open");
251 static void query_device(void)
253 struct crypt_options co = {. icb = &cmd_icb };
255 co.name = CDEVICE_WRONG;
256 EQ_(crypt_query_device(&co), 0);
259 EQ_(crypt_query_device(&co), 1);
261 OK_(strncmp(crypt_get_dir(), DMDIR, 11));
262 OK_(strcmp(co.cipher, "aes-cbc-essiv:sha256"));
263 EQ_(co.key_size, 16);
264 EQ_(co.offset, 1032);
265 EQ_(co.flags & CRYPT_FLAG_READONLY, 0);
267 crypt_put_options(&co);
270 static void remove_device(void)
273 struct crypt_options co = {. icb = &cmd_icb };
275 co.name = CDEVICE_WRONG;
276 EQ_(crypt_remove_device(&co), -ENODEV);
278 fd = open(DMDIR CDEVICE_1, O_RDONLY);
280 FAIL_(crypt_remove_device(&co), "device busy");
283 OK_(crypt_remove_device(&co));
286 static void LuksFormat(void)
288 struct crypt_options co = {
292 .cipher = "aes-cbc-essiv:sha256",
295 .iteration_time = 10,
300 OK_(_prepare_keyfile(KEYFILE1, KEY1));
302 co.new_key_file = KEYFILE1;
303 co.device = DEVICE_ERROR;
304 FAIL_(crypt_luksFormat(&co), "error device");
306 co.device = DEVICE_2;
307 OK_(crypt_luksFormat(&co));
309 co.new_key_file = NULL;
310 co.key_file = KEYFILE1;
312 OK_(crypt_luksOpen(&co));
313 OK_(crypt_remove_device(&co));
317 static void LuksKeyGame(void)
320 struct crypt_options co = {
324 .cipher = "aes-cbc-essiv:sha256",
327 .iteration_time = 10,
332 OK_(_prepare_keyfile(KEYFILE1, KEY1));
333 OK_(_prepare_keyfile(KEYFILE2, KEY2));
335 co.new_key_file = KEYFILE1;
336 co.device = DEVICE_2;
338 FAIL_(crypt_luksFormat(&co), "wrong slot #");
340 co.key_slot = 7; // last slot
341 OK_(crypt_luksFormat(&co));
343 co.new_key_file = KEYFILE1;
344 co.key_file = KEYFILE1;
346 FAIL_(crypt_luksAddKey(&co), "wrong slot #");
348 FAIL_(crypt_luksAddKey(&co), "slot already used");
351 OK_(crypt_luksAddKey(&co));
353 co.key_file = KEYFILE2 "blah";
355 FAIL_(crypt_luksAddKey(&co), "keyfile not found");
357 co.new_key_file = KEYFILE2; // key to add
358 co.key_file = KEYFILE1;
360 for (i = 0; i < 6; i++)
361 OK_(crypt_luksAddKey(&co)); //FIXME: EQ_(i)?
363 FAIL_(crypt_luksAddKey(&co), "all slots full");
366 co.new_key_file = KEYFILE1; // key to remove
368 co.key_slot = 8; // should be ignored
369 // only 2 slots should use KEYFILE1
370 OK_(crypt_luksRemoveKey(&co));
371 OK_(crypt_luksRemoveKey(&co));
372 FAIL_(crypt_luksRemoveKey(&co), "no slot with this passphrase");
374 co.new_key_file = KEYFILE2 "blah";
376 FAIL_(crypt_luksRemoveKey(&co), "keyfile not found");
379 co.new_key_file = NULL;
382 FAIL_(crypt_luksKillSlot(&co), "wrong slot #");
384 FAIL_(crypt_luksKillSlot(&co), "slot already wiped");
387 OK_(crypt_luksKillSlot(&co));
392 size_t _get_device_size(const char *device)
394 unsigned long size = 0;
397 fd = open(device, O_RDONLY);
400 (void)ioctl(fd, BLKGETSIZE, &size);
406 void DeviceResizeGame(void)
409 struct crypt_options co = {
413 .cipher = "aes-cbc-plain",
420 orig_size = _get_device_size(DEVICE_2);
422 OK_(_prepare_keyfile(KEYFILE2, KEY2));
424 co.key_file = KEYFILE2;
426 OK_(crypt_create_device(&co));
427 EQ_(_get_device_size(DMDIR CDEVICE_2), 1000);
430 OK_(crypt_resize_device(&co));
431 EQ_(_get_device_size(DMDIR CDEVICE_2), 2000);
434 OK_(crypt_resize_device(&co));
435 EQ_(_get_device_size(DMDIR CDEVICE_2), (orig_size - 333));
440 co.cipher = "aes-cbc-benbi";
441 OK_(crypt_update_device(&co));
442 EQ_(_get_device_size(DMDIR CDEVICE_2), (orig_size - 444));
444 memset(&co, 0, sizeof(co));
447 EQ_(crypt_query_device(&co), 1);
448 EQ_(strcmp(co.cipher, "aes-cbc-benbi"), 0);
449 EQ_(co.key_size, 128 / 8);
452 OK_(crypt_remove_device(&co));
454 crypt_put_options(&co);
459 int main (int argc, char *argv[])
463 for (i = 1; i < argc; i++) {
464 if (!strcmp("-v", argv[i]) || !strcmp("--verbose", argv[i]))
466 else if (!strcmp("--debug", argv[i]))
467 _debug = _verbose = 1;
473 #ifdef CRYPT_DEBUG_ALL
474 crypt_set_debug_level(_debug ? CRYPT_DEBUG_ALL : CRYPT_DEBUG_NONE);
477 RUN_(LuksUUID, "luksUUID API call");
478 RUN_(IsLuks, "isLuks API call");
479 RUN_(LuksOpen, "luksOpen API call");
480 RUN_(query_device, "crypt_query_device API call");
481 RUN_(remove_device, "crypt_remove_device API call");
482 RUN_(LuksFormat, "luksFormat API call");
483 RUN_(LuksKeyGame, "luksAddKey, RemoveKey, KillSlot API calls");
484 RUN_(DeviceResizeGame, "regular crypto, resize calls");