2 * cryptsetup - setup cryptographic volumes for dm-crypt
4 * Copyright (C) 2004, Christophe Saout <christophe@saout.de>
5 * Copyright (C) 2004-2007, Clemens Fruhwirth <clemens@endorphin.org>
6 * Copyright (C) 2009-2012, Red Hat, Inc. All rights reserved.
7 * Copyright (C) 2009-2012, Milan Broz
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "cryptsetup.h"
25 static const char *opt_cipher = NULL;
26 static const char *opt_hash = NULL;
27 static int opt_verify_passphrase = 0;
29 static const char *opt_key_file = NULL;
30 static int opt_keyfiles_count = 0;
31 static const char *opt_keyfiles[MAX_KEYFILES];
33 static const char *opt_master_key_file = NULL;
34 static const char *opt_header_backup_file = NULL;
35 static const char *opt_uuid = NULL;
36 static const char *opt_header_device = NULL;
37 static const char *opt_type = "luks";
38 static int opt_key_size = 0;
39 static long opt_keyfile_size = 0;
40 static long opt_new_keyfile_size = 0;
41 static long opt_keyfile_offset = 0;
42 static long opt_new_keyfile_offset = 0;
43 static int opt_key_slot = CRYPT_ANY_SLOT;
44 static uint64_t opt_size = 0;
45 static uint64_t opt_offset = 0;
46 static uint64_t opt_skip = 0;
47 static int opt_skip_valid = 0;
48 static int opt_readonly = 0;
49 static int opt_iteration_time = DEFAULT_LUKS1_ITER_TIME;
50 static int opt_version_mode = 0;
51 static int opt_timeout = 0;
52 static int opt_tries = 3;
53 static int opt_align_payload = 0;
54 static int opt_random = 0;
55 static int opt_urandom = 0;
56 static int opt_dump_master_key = 0;
57 static int opt_shared = 0;
58 static int opt_allow_discards = 0;
59 static int opt_test_passphrase = 0;
60 static int opt_hidden = 0;
62 static const char **action_argv;
63 static int action_argc;
64 static const char *null_action_argv[] = {NULL, NULL};
66 static int _verify_passphrase(int def)
68 /* Batch mode switch off verify - if not overrided by -y */
69 if (opt_verify_passphrase)
71 else if (opt_batch_mode)
74 /* Non-tty input doesn't allow verify */
75 if (def && !isatty(STDIN_FILENO)) {
76 if (opt_verify_passphrase)
77 log_err(_("Can't do passphrase verification on non-tty inputs.\n"));
84 static int action_open_plain(void)
86 struct crypt_device *cd = NULL;
87 char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
88 struct crypt_params_plain params = {
89 .hash = opt_hash ?: DEFAULT_PLAIN_HASH,
94 char *password = NULL;
96 size_t key_size = (opt_key_size ?: DEFAULT_PLAIN_KEYBITS) / 8;
97 uint32_t activate_flags = 0;
100 if (params.hash && !strcmp(params.hash, "plain"))
103 /* FIXME: temporary hack */
104 if (opt_key_file && strcmp(opt_key_file, "-"))
107 if ((opt_keyfile_offset || opt_keyfile_size) && opt_key_file)
108 log_std(("Ignoring keyfile offset and size options, keyfile read "
109 "size is always the same as encryption key size.\n"));
111 r = crypt_parse_name_and_mode(opt_cipher ?: DEFAULT_CIPHER(PLAIN),
112 cipher, NULL, cipher_mode);
114 log_err("No known cipher specification pattern detected.\n");
118 if ((r = crypt_init(&cd, action_argv[0])))
121 crypt_set_timeout(cd, opt_timeout);
122 crypt_set_password_retry(cd, opt_tries);
124 r = crypt_format(cd, CRYPT_PLAIN,
133 activate_flags |= CRYPT_ACTIVATE_READONLY;
136 activate_flags |= CRYPT_ACTIVATE_SHARED;
138 if (opt_allow_discards)
139 activate_flags |= CRYPT_ACTIVATE_ALLOW_DISCARDS;
142 /* With hashing, read the whole keyfile */
143 r = crypt_activate_by_keyfile_offset(cd, action_argv[1],
144 CRYPT_ANY_SLOT, opt_key_file,
145 params.hash ? 0 : key_size, 0,
148 r = crypt_get_key(_("Enter passphrase: "),
149 &password, &passwordLen,
150 opt_keyfile_offset, opt_keyfile_size,
152 _verify_passphrase(0),
157 r = crypt_activate_by_passphrase(cd, action_argv[1],
158 CRYPT_ANY_SLOT, password, passwordLen, activate_flags);
162 crypt_safe_free(password);
167 static int action_open_loopaes(void)
169 struct crypt_device *cd = NULL;
170 struct crypt_params_loopaes params = {
171 .hash = opt_hash ?: NULL,
172 .offset = opt_offset,
173 .skip = opt_skip_valid ? opt_skip : opt_offset,
175 unsigned int key_size = (opt_key_size ?: DEFAULT_LOOPAES_KEYBITS) / 8;
176 uint32_t activate_flags = 0;
180 log_err(_("Option --key-file is required.\n"));
185 activate_flags |= CRYPT_ACTIVATE_READONLY;
187 if (opt_allow_discards)
188 activate_flags |= CRYPT_ACTIVATE_ALLOW_DISCARDS;
190 if ((r = crypt_init(&cd, action_argv[0])))
193 r = crypt_format(cd, CRYPT_LOOPAES, opt_cipher ?: DEFAULT_LOOPAES_CIPHER,
194 NULL, NULL, NULL, key_size, ¶ms);
198 r = crypt_activate_by_keyfile_offset(cd, action_argv[1], CRYPT_ANY_SLOT,
199 opt_key_file, opt_keyfile_size,
200 opt_keyfile_size, activate_flags);
207 static int action_open_tcrypt(void)
209 struct crypt_device *cd = NULL;
210 struct crypt_params_tcrypt params = {
211 .keyfiles = opt_keyfiles,
212 .keyfiles_count = opt_keyfiles_count,
213 .flags = CRYPT_TCRYPT_LEGACY_MODES,
215 const char *activated_name;
219 activated_name = opt_test_passphrase ? NULL : action_argv[1];
221 if ((r = crypt_init(&cd, action_argv[0])))
224 /* TCRYPT header is encrypted, get passphrase now */
225 r = crypt_get_key(_("Enter passphrase: "),
226 CONST_CAST(char**)¶ms.passphrase,
227 ¶ms.passphrase_size, 0, 0, NULL, opt_timeout,
228 _verify_passphrase(0), cd);
233 params.flags |= CRYPT_TCRYPT_HIDDEN_HEADER;
235 r = crypt_load(cd, CRYPT_TCRYPT, ¶ms);
240 flags |= CRYPT_ACTIVATE_READONLY;
243 r = crypt_activate_by_volume_key(cd, activated_name, NULL, 0, flags);
246 crypt_safe_free(CONST_CAST(char*)params.passphrase);
250 static int tcryptDump_with_volume_key(struct crypt_device *cd)
257 crypt_set_confirm_callback(cd, yesDialog, NULL);
259 _("Header dump with volume key is sensitive information\n"
260 "which allows access to encrypted partition without passphrase.\n"
261 "This dump should be always stored encrypted on safe place."),
265 vk_size = crypt_get_volume_key_size(cd);
266 vk = crypt_safe_alloc(vk_size);
270 r = crypt_volume_key_get(cd, CRYPT_ANY_SLOT, vk, &vk_size, NULL, 0);
274 log_std("TCRYPT header information for %s\n", crypt_get_device_name(cd));
275 log_std("Cipher chain: \t%s\n", crypt_get_cipher(cd));
276 log_std("Cipher mode: \t%s\n", crypt_get_cipher_mode(cd));
277 log_std("Payload offset:\t%d\n", (int)crypt_get_data_offset(cd));
278 log_std("MK bits: \t%d\n", (int)vk_size * 8);
279 log_std("MK dump:\t");
281 for(i = 0; i < vk_size; i++) {
284 log_std("%02hhx ", (char)vk[i]);
292 static int action_tcryptDump(void)
294 struct crypt_device *cd = NULL;
295 struct crypt_params_tcrypt params = {
296 .keyfiles = opt_keyfiles,
297 .keyfiles_count = opt_keyfiles_count,
298 .flags = CRYPT_TCRYPT_LEGACY_MODES,
302 if ((r = crypt_init(&cd, action_argv[0])))
305 /* TCRYPT header is encrypted, get passphrase now */
306 r = crypt_get_key(_("Enter passphrase: "),
307 CONST_CAST(char**)¶ms.passphrase,
308 ¶ms.passphrase_size, 0, 0, NULL, opt_timeout,
309 _verify_passphrase(0), cd);
314 params.flags |= CRYPT_TCRYPT_HIDDEN_HEADER;
316 r = crypt_load(cd, CRYPT_TCRYPT, ¶ms);
320 if (opt_dump_master_key)
321 r = tcryptDump_with_volume_key(cd);
326 crypt_safe_free(CONST_CAST(char*)params.passphrase);
330 static int action_close(void)
332 struct crypt_device *cd = NULL;
335 r = crypt_init_by_name(&cd, action_argv[0]);
337 r = crypt_deactivate(cd, action_argv[0]);
343 static int action_resize(void)
345 struct crypt_device *cd = NULL;
348 r = crypt_init_by_name_and_header(&cd, action_argv[0], opt_header_device);
350 r = crypt_resize(cd, action_argv[0], opt_size);
356 static int action_status(void)
358 crypt_status_info ci;
359 struct crypt_active_device cad;
360 struct crypt_device *cd = NULL;
366 /* perhaps a path, not a dm device name */
367 if (strchr(action_argv[0], '/') && !stat(action_argv[0], &st))
370 ci = crypt_status(NULL, action_argv[0]);
377 log_std("%s is inactive.\n", action_argv[0]);
379 log_std("%s/%s is inactive.\n", crypt_get_dir(), action_argv[0]);
385 log_std("%s is active%s.\n", action_argv[0],
386 ci == CRYPT_BUSY ? " and is in use" : "");
388 log_std("%s/%s is active%s.\n", crypt_get_dir(), action_argv[0],
389 ci == CRYPT_BUSY ? " and is in use" : "");
391 r = crypt_init_by_name_and_header(&cd, action_argv[0], opt_header_device);
392 if (r < 0 || !crypt_get_type(cd))
395 log_std(" type: %s\n", crypt_get_type(cd));
397 r = crypt_get_active_device(cd, action_argv[0], &cad);
401 log_std(" cipher: %s-%s\n", crypt_get_cipher(cd), crypt_get_cipher_mode(cd));
402 log_std(" keysize: %d bits\n", crypt_get_volume_key_size(cd) * 8);
403 device = crypt_get_device_name(cd);
404 log_std(" device: %s\n", device);
405 if (crypt_loop_device(device)) {
406 backing_file = crypt_loop_backing_file(device);
407 log_std(" loop: %s\n", backing_file);
410 log_std(" offset: %" PRIu64 " sectors\n", cad.offset);
411 log_std(" size: %" PRIu64 " sectors\n", cad.size);
413 log_std(" skipped: %" PRIu64 " sectors\n", cad.iv_offset);
414 log_std(" mode: %s\n", cad.flags & CRYPT_ACTIVATE_READONLY ?
415 "readonly" : "read/write");
416 if (cad.flags & CRYPT_ACTIVATE_ALLOW_DISCARDS)
417 log_std(" flags: discards\n");
426 static int action_benchmark_kdf(const char *hash)
431 r = crypt_benchmark_kdf(NULL, "pbkdf2", hash, "foo", 3, "bar", 3,
434 log_std("PBKDF2-%-9s N/A\n", hash);
436 log_std("PBKDF2-%-9s %7" PRIu64 " iterations per second\n",
441 static int action_benchmark(void)
449 { "aes", "cbc", 16, 16 },
450 { "serpent", "cbc", 16, 16 },
451 { "twofish", "cbc", 16, 16 },
452 { "aes", "cbc", 32, 16 },
453 { "serpent", "cbc", 32, 16 },
454 { "twofish", "cbc", 32, 16 },
455 { "aes", "xts", 32, 16 },
456 { "serpent", "xts", 32, 16 },
457 { "twofish", "xts", 32, 16 },
458 { "aes", "xts", 64, 16 },
459 { "serpent", "xts", 64, 16 },
460 { "twofish", "xts", 64, 16 },
463 char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
464 double enc_mbr = 0, dec_mbr = 0;
465 int key_size = (opt_key_size ?: DEFAULT_PLAIN_KEYBITS);
466 int iv_size = 16, skipped = 0;
467 int buffer_size = 1024 * 1024;
471 log_std("# Tests are approximate using memory only (no storage IO).\n");
473 r = action_benchmark_kdf(opt_hash);
474 } else if (opt_cipher) {
475 r = crypt_parse_name_and_mode(opt_cipher, cipher, NULL, cipher_mode);
477 log_err(_("No known cipher specification pattern detected.\n"));
480 if ((c = strchr(cipher_mode, '-')))
483 /* FIXME: not really clever :) */
484 if (strstr(cipher, "des") ||
485 strstr(cipher, "blowfish") ||
486 strstr(cipher, "cast5"))
489 r = crypt_benchmark(NULL, cipher, cipher_mode,
490 key_size / 8, iv_size, buffer_size,
493 log_std("# Algorithm | Key | Encryption | Decryption\n");
494 log_std("%8s-%s %4db %5.1f MiB/s %5.1f MiB/s\n",
495 cipher, cipher_mode, key_size, enc_mbr, dec_mbr);
496 } else if (r == -ENOENT)
497 log_err(_("Cipher %s is not available.\n"), opt_cipher);
499 action_benchmark_kdf("sha1");
500 action_benchmark_kdf("sha256");
501 action_benchmark_kdf("sha512");
502 action_benchmark_kdf("ripemd160");
503 action_benchmark_kdf("whirlpool");
504 for (i = 0; bciphers[i].cipher; i++) {
505 r = crypt_benchmark(NULL, bciphers[i].cipher, bciphers[i].mode,
506 bciphers[i].key_size, bciphers[i].iv_size,
507 buffer_size, &enc_mbr, &dec_mbr);
513 log_std("# Algorithm | Key | Encryption | Decryption\n");
515 snprintf(cipher, MAX_CIPHER_LEN, "%s-%s",
516 bciphers[i].cipher, bciphers[i].mode);
518 log_std("%12s %4db %5.1f MiB/s %5.1f MiB/s\n",
519 cipher, bciphers[i].key_size*8, enc_mbr, dec_mbr);
521 log_std("%12s %4db %12s %12s\n", cipher,
522 bciphers[i].key_size*8, _("N/A"), _("N/A"));
524 if (skipped && skipped == i)
529 log_err( _("Required kernel crypto interface not available.\n"
530 "Ensure you have algif_skcipher kernel module loaded.\n"));
534 static int _read_mk(const char *file, char **key, int keysize)
538 *key = crypt_safe_alloc(keysize);
542 fd = open(file, O_RDONLY);
544 log_err("Cannot read keyfile %s.\n", file);
547 if ((read(fd, *key, keysize) != keysize)) {
548 log_err("Cannot read %d bytes from keyfile %s.\n", keysize, file);
555 crypt_safe_free(*key);
560 static int action_luksRepair(void)
562 struct crypt_device *cd = NULL;
565 if ((r = crypt_init(&cd, action_argv[0])))
568 /* Currently only LUKS1 allows repair */
569 crypt_set_log_callback(cd, quiet_log, NULL);
570 r = crypt_load(cd, CRYPT_LUKS1, NULL);
571 crypt_set_log_callback(cd, tool_log, NULL);
573 log_verbose( _("No known problems detected for LUKS header.\n"));
577 r = yesDialog(_("Really try to repair LUKS device header?"),
580 r = crypt_repair(cd, CRYPT_LUKS1, NULL);
586 static int action_luksFormat(void)
588 int r = -EINVAL, keysize;
589 const char *header_device;
590 char *msg = NULL, *key = NULL, cipher [MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
591 char *password = NULL;
593 struct crypt_device *cd = NULL;
594 struct crypt_params_luks1 params = {
595 .hash = opt_hash ?: DEFAULT_LUKS1_HASH,
596 .data_alignment = opt_align_payload,
597 .data_device = opt_header_device ? action_argv[0] : NULL,
600 header_device = opt_header_device ?: action_argv[0];
602 if(asprintf(&msg, _("This will overwrite data on %s irrevocably."),
603 header_device) == -1) {
604 log_err(_("memory allocation error in action_luksFormat"));
608 r = yesDialog(msg, NULL) ? 0 : -EINVAL;
613 r = crypt_parse_name_and_mode(opt_cipher ?: DEFAULT_CIPHER(LUKS1),
614 cipher, NULL, cipher_mode);
616 log_err(_("No known cipher specification pattern detected.\n"));
620 if ((r = crypt_init(&cd, header_device))) {
621 if (opt_header_device)
622 log_err(_("Cannot use %s as on-disk header.\n"), header_device);
626 keysize = (opt_key_size ?: DEFAULT_LUKS1_KEYBITS) / 8;
628 crypt_set_timeout(cd, opt_timeout);
629 if (opt_iteration_time)
630 crypt_set_iteration_time(cd, opt_iteration_time);
633 crypt_set_rng_type(cd, CRYPT_RNG_RANDOM);
634 else if (opt_urandom)
635 crypt_set_rng_type(cd, CRYPT_RNG_URANDOM);
637 r = crypt_get_key(_("Enter LUKS passphrase: "), &password, &passwordLen,
638 opt_keyfile_offset, opt_keyfile_size, opt_key_file,
639 opt_timeout, _verify_passphrase(1), cd);
643 if (opt_master_key_file) {
644 r = _read_mk(opt_master_key_file, &key, keysize);
649 r = crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode,
650 opt_uuid, key, keysize, ¶ms);
654 r = crypt_keyslot_add_by_volume_key(cd, opt_key_slot,
656 password, passwordLen);
659 crypt_safe_free(key);
660 crypt_safe_free(password);
665 static int action_open_luks(void)
667 struct crypt_device *cd = NULL;
668 const char *data_device, *header_device, *activated_name;
673 if (opt_header_device) {
674 header_device = uuid_or_device(opt_header_device);
675 data_device = action_argv[0];
677 header_device = uuid_or_device(action_argv[0]);
681 activated_name = opt_test_passphrase ? NULL : action_argv[1];
683 if ((r = crypt_init(&cd, header_device)))
686 if ((r = crypt_load(cd, CRYPT_LUKS1, NULL)))
690 (r = crypt_set_data_device(cd, data_device)))
693 if (!data_device && (crypt_get_data_offset(cd) < 8)) {
694 log_err(_("Reduced data offset is allowed only for detached LUKS header.\n"));
699 crypt_set_timeout(cd, opt_timeout);
700 crypt_set_password_retry(cd, opt_tries);
701 crypt_set_password_verify(cd, _verify_passphrase(0));
703 if (opt_iteration_time)
704 crypt_set_iteration_time(cd, opt_iteration_time);
707 flags |= CRYPT_ACTIVATE_READONLY;
709 if (opt_allow_discards)
710 flags |= CRYPT_ACTIVATE_ALLOW_DISCARDS;
712 if (opt_master_key_file) {
713 keysize = crypt_get_volume_key_size(cd);
714 r = _read_mk(opt_master_key_file, &key, keysize);
717 r = crypt_activate_by_volume_key(cd, activated_name,
718 key, keysize, flags);
719 } else if (opt_key_file) {
720 crypt_set_password_retry(cd, 1);
721 r = crypt_activate_by_keyfile_offset(cd, activated_name,
722 opt_key_slot, opt_key_file, opt_keyfile_size,
723 opt_keyfile_offset, flags);
725 r = crypt_activate_by_passphrase(cd, activated_name,
726 opt_key_slot, NULL, 0, flags);
728 crypt_safe_free(key);
733 static int verify_keyslot(struct crypt_device *cd, int key_slot,
734 char *msg_last, char *msg_pass,
735 const char *key_file, int keyfile_offset,
738 crypt_keyslot_info ki;
739 char *password = NULL;
743 ki = crypt_keyslot_status(cd, key_slot);
744 if (ki == CRYPT_SLOT_ACTIVE_LAST && msg_last && !yesDialog(msg_last, NULL))
747 r = crypt_get_key(msg_pass, &password, &passwordLen,
748 keyfile_offset, keyfile_size, key_file, opt_timeout,
749 _verify_passphrase(0), cd);
753 if (ki == CRYPT_SLOT_ACTIVE_LAST) {
754 /* check the last keyslot */
755 r = crypt_activate_by_passphrase(cd, NULL, key_slot,
756 password, passwordLen, 0);
758 /* try all other keyslots */
759 for (i = 0; i < crypt_keyslot_max(CRYPT_LUKS1); i++) {
762 ki = crypt_keyslot_status(cd, key_slot);
763 if (ki == CRYPT_SLOT_ACTIVE)
764 r = crypt_activate_by_passphrase(cd, NULL, i,
765 password, passwordLen, 0);
772 log_err(_("No key available with this passphrase.\n"));
774 crypt_safe_free(password);
778 static int action_luksKillSlot(void)
780 struct crypt_device *cd = NULL;
783 if ((r = crypt_init(&cd, uuid_or_device(action_argv[0]))))
786 crypt_set_confirm_callback(cd, yesDialog, NULL);
787 crypt_set_timeout(cd, opt_timeout);
789 if ((r = crypt_load(cd, CRYPT_LUKS1, NULL)))
792 switch (crypt_keyslot_status(cd, opt_key_slot)) {
793 case CRYPT_SLOT_ACTIVE_LAST:
794 case CRYPT_SLOT_ACTIVE:
795 log_verbose(_("Key slot %d selected for deletion.\n"), opt_key_slot);
797 case CRYPT_SLOT_INACTIVE:
798 log_err(_("Key %d not active. Can't wipe.\n"), opt_key_slot);
799 case CRYPT_SLOT_INVALID:
804 if (!opt_batch_mode) {
805 r = verify_keyslot(cd, opt_key_slot,
806 _("This is the last keyslot. Device will become unusable after purging this key."),
807 _("Enter any remaining LUKS passphrase: "),
808 opt_key_file, opt_keyfile_offset, opt_keyfile_size);
813 r = crypt_keyslot_destroy(cd, opt_key_slot);
819 static int action_luksRemoveKey(void)
821 struct crypt_device *cd = NULL;
822 char *password = NULL;
826 if ((r = crypt_init(&cd, uuid_or_device(action_argv[0]))))
829 crypt_set_confirm_callback(cd, yesDialog, NULL);
830 crypt_set_timeout(cd, opt_timeout);
832 if ((r = crypt_load(cd, CRYPT_LUKS1, NULL)))
835 r = crypt_get_key(_("Enter LUKS passphrase to be deleted: "),
836 &password, &passwordLen,
837 opt_keyfile_offset, opt_keyfile_size, opt_key_file,
839 _verify_passphrase(0),
844 r = crypt_activate_by_passphrase(cd, NULL, CRYPT_ANY_SLOT,
845 password, passwordLen, 0);
850 log_verbose(_("Key slot %d selected for deletion.\n"), opt_key_slot);
852 if (crypt_keyslot_status(cd, opt_key_slot) == CRYPT_SLOT_ACTIVE_LAST &&
853 !yesDialog(_("This is the last keyslot. "
854 "Device will become unusable after purging this key."),
860 r = crypt_keyslot_destroy(cd, opt_key_slot);
862 crypt_safe_free(password);
867 static int action_luksAddKey(void)
869 int r = -EINVAL, keysize = 0;
871 const char *opt_new_key_file = (action_argc > 1 ? action_argv[1] : NULL);
872 struct crypt_device *cd = NULL;
874 if ((r = crypt_init(&cd, uuid_or_device(action_argv[0]))))
877 crypt_set_confirm_callback(cd, yesDialog, NULL);
879 if ((r = crypt_load(cd, CRYPT_LUKS1, NULL)))
882 keysize = crypt_get_volume_key_size(cd);
883 /* FIXME: lib cannot properly set verification for new/old passphrase */
884 crypt_set_password_verify(cd, _verify_passphrase(0));
885 crypt_set_timeout(cd, opt_timeout);
886 if (opt_iteration_time)
887 crypt_set_iteration_time(cd, opt_iteration_time);
889 if (opt_master_key_file) {
890 r = _read_mk(opt_master_key_file, &key, keysize);
893 //FIXME: process keyfile arg
894 r = crypt_keyslot_add_by_volume_key(cd, opt_key_slot,
895 key, keysize, NULL, 0);
896 } else if (opt_key_file || opt_new_key_file) {
897 r = crypt_keyslot_add_by_keyfile_offset(cd, opt_key_slot,
898 opt_key_file, opt_keyfile_size, opt_keyfile_offset,
899 opt_new_key_file, opt_new_keyfile_size, opt_new_keyfile_offset);
901 r = crypt_keyslot_add_by_passphrase(cd, opt_key_slot,
906 crypt_safe_free(key);
910 static int action_luksChangeKey(void)
912 const char *opt_new_key_file = (action_argc > 1 ? action_argv[1] : NULL);
913 struct crypt_device *cd = NULL;
914 char *password = NULL, *password_new = NULL;
915 size_t password_size = 0, password_new_size = 0;
918 if ((r = crypt_init(&cd, uuid_or_device(action_argv[0]))))
921 if ((r = crypt_load(cd, CRYPT_LUKS1, NULL)))
924 if (opt_iteration_time)
925 crypt_set_iteration_time(cd, opt_iteration_time);
927 r = crypt_get_key(_("Enter LUKS passphrase to be changed: "),
928 &password, &password_size,
929 opt_keyfile_offset, opt_keyfile_size, opt_key_file,
930 opt_timeout, _verify_passphrase(0), cd);
934 /* Check password before asking for new one */
935 r = crypt_activate_by_passphrase(cd, NULL, opt_key_slot,
936 password, password_size, 0);
940 r = crypt_get_key(_("Enter new LUKS passphrase: "),
941 &password_new, &password_new_size,
942 opt_new_keyfile_offset, opt_new_keyfile_size,
944 opt_timeout, _verify_passphrase(0), cd);
948 r = crypt_keyslot_change_by_passphrase(cd, opt_key_slot, opt_key_slot,
949 password, password_size, password_new, password_new_size);
951 crypt_safe_free(password);
952 crypt_safe_free(password_new);
957 static int action_isLuks(void)
959 struct crypt_device *cd = NULL;
962 if ((r = crypt_init(&cd, action_argv[0])))
965 crypt_set_log_callback(cd, quiet_log, NULL);
966 r = crypt_load(cd, CRYPT_LUKS1, NULL);
972 static int action_luksUUID(void)
974 struct crypt_device *cd = NULL;
975 const char *existing_uuid = NULL;
978 if ((r = crypt_init(&cd, action_argv[0])))
981 crypt_set_confirm_callback(cd, yesDialog, NULL);
983 if ((r = crypt_load(cd, CRYPT_LUKS1, NULL)))
987 r = crypt_set_uuid(cd, opt_uuid);
989 existing_uuid = crypt_get_uuid(cd);
990 log_std("%s\n", existing_uuid ?: "");
991 r = existing_uuid ? 0 : 1;
998 static int luksDump_with_volume_key(struct crypt_device *cd)
1000 char *vk = NULL, *password = NULL;
1001 size_t passwordLen = 0;
1006 crypt_set_confirm_callback(cd, yesDialog, NULL);
1008 _("Header dump with volume key is sensitive information\n"
1009 "which allows access to encrypted partition without passphrase.\n"
1010 "This dump should be always stored encrypted on safe place."),
1014 vk_size = crypt_get_volume_key_size(cd);
1015 vk = crypt_safe_alloc(vk_size);
1019 r = crypt_get_key(_("Enter LUKS passphrase: "), &password, &passwordLen,
1020 opt_keyfile_offset, opt_keyfile_size, opt_key_file,
1021 opt_timeout, 0, cd);
1025 r = crypt_volume_key_get(cd, CRYPT_ANY_SLOT, vk, &vk_size,
1026 password, passwordLen);
1030 log_std("LUKS header information for %s\n", crypt_get_device_name(cd));
1031 log_std("Cipher name: \t%s\n", crypt_get_cipher(cd));
1032 log_std("Cipher mode: \t%s\n", crypt_get_cipher_mode(cd));
1033 log_std("Payload offset:\t%d\n", (int)crypt_get_data_offset(cd));
1034 log_std("UUID: \t%s\n", crypt_get_uuid(cd));
1035 log_std("MK bits: \t%d\n", (int)vk_size * 8);
1036 log_std("MK dump:\t");
1038 for(i = 0; i < vk_size; i++) {
1041 log_std("%02hhx ", (char)vk[i]);
1046 crypt_safe_free(password);
1047 crypt_safe_free(vk);
1051 static int action_luksDump(void)
1053 struct crypt_device *cd = NULL;
1056 if ((r = crypt_init(&cd, uuid_or_device(action_argv[0]))))
1059 if ((r = crypt_load(cd, CRYPT_LUKS1, NULL)))
1062 if (opt_dump_master_key)
1063 r = luksDump_with_volume_key(cd);
1071 static int action_luksSuspend(void)
1073 struct crypt_device *cd = NULL;
1076 r = crypt_init_by_name_and_header(&cd, action_argv[0], opt_header_device);
1078 r = crypt_suspend(cd, action_argv[0]);
1084 static int action_luksResume(void)
1086 struct crypt_device *cd = NULL;
1089 if ((r = crypt_init_by_name_and_header(&cd, action_argv[0], opt_header_device)))
1092 crypt_set_timeout(cd, opt_timeout);
1093 crypt_set_password_retry(cd, opt_tries);
1094 crypt_set_password_verify(cd, _verify_passphrase(0));
1097 r = crypt_resume_by_keyfile_offset(cd, action_argv[0], CRYPT_ANY_SLOT,
1098 opt_key_file, opt_keyfile_size, opt_keyfile_offset);
1100 r = crypt_resume_by_passphrase(cd, action_argv[0], CRYPT_ANY_SLOT,
1107 static int action_luksBackup(void)
1109 struct crypt_device *cd = NULL;
1112 if (!opt_header_backup_file) {
1113 log_err(_("Option --header-backup-file is required.\n"));
1117 if ((r = crypt_init(&cd, uuid_or_device(action_argv[0]))))
1120 crypt_set_confirm_callback(cd, yesDialog, NULL);
1122 r = crypt_header_backup(cd, CRYPT_LUKS1, opt_header_backup_file);
1128 static int action_luksRestore(void)
1130 struct crypt_device *cd = NULL;
1133 if (!opt_header_backup_file) {
1134 log_err(_("Option --header-backup-file is required.\n"));
1138 if ((r = crypt_init(&cd, action_argv[0])))
1141 crypt_set_confirm_callback(cd, yesDialog, NULL);
1142 r = crypt_header_restore(cd, CRYPT_LUKS1, opt_header_backup_file);
1148 static int action_open(void)
1153 if (!strcmp(opt_type, "luks") || !strcmp(opt_type, "luks1")) {
1154 if (action_argc < 2 && !opt_test_passphrase)
1156 return action_open_luks();
1157 } else if (!strcmp(opt_type, "plain")) {
1158 if (action_argc < 2)
1160 return action_open_plain();
1161 } else if (!strcmp(opt_type, "loopaes")) {
1162 if (action_argc < 2)
1164 return action_open_loopaes();
1165 } else if (!strcmp(opt_type, "tcrypt")) {
1166 if (action_argc < 2 && !opt_test_passphrase)
1168 return action_open_tcrypt();
1171 log_err(_("Unrecognized metadata device type %s.\n"), opt_type);
1174 log_err(_("Command requires device and mapped name as arguments.\n"));
1178 static struct action_type {
1180 int (*handler)(void);
1181 int required_action_argc;
1182 int required_memlock;
1183 const char *arg_desc;
1185 } action_types[] = {
1186 { "open", action_open, 1, 1, N_("<device> [--type <type>] [<name>]"),N_("open device as mapping <name>") },
1187 { "close", action_close, 1, 1, N_("<name>"), N_("close device (remove mapping)") },
1188 { "resize", action_resize, 1, 1, N_("<name>"), N_("resize active device") },
1189 { "status", action_status, 1, 0, N_("<name>"), N_("show device status") },
1190 { "benchmark", action_benchmark, 0, 0, N_("<name>"), N_("benchmark cipher") },
1191 { "repair", action_luksRepair, 1, 1, N_("<device>"), N_("try to repair on-disk metadata") },
1192 { "luksFormat", action_luksFormat, 1, 1, N_("<device> [<new key file>]"), N_("formats a LUKS device") },
1193 { "luksAddKey", action_luksAddKey, 1, 1, N_("<device> [<new key file>]"), N_("add key to LUKS device") },
1194 { "luksRemoveKey",action_luksRemoveKey,1, 1, N_("<device> [<key file>]"), N_("removes supplied key or key file from LUKS device") },
1195 { "luksChangeKey",action_luksChangeKey,1, 1, N_("<device> [<key file>]"), N_("changes supplied key or key file of LUKS device") },
1196 { "luksKillSlot", action_luksKillSlot, 2, 1, N_("<device> <key slot>"), N_("wipes key with number <key slot> from LUKS device") },
1197 { "luksUUID", action_luksUUID, 1, 0, N_("<device>"), N_("print UUID of LUKS device") },
1198 { "isLuks", action_isLuks, 1, 0, N_("<device>"), N_("tests <device> for LUKS partition header") },
1199 { "luksDump", action_luksDump, 1, 1, N_("<device>"), N_("dump LUKS partition information") },
1200 { "tcryptDump", action_tcryptDump, 1, 1, N_("<device>"), N_("dump TCRYPT device information") },
1201 { "luksSuspend", action_luksSuspend, 1, 1, N_("<device>"), N_("Suspend LUKS device and wipe key (all IOs are frozen).") },
1202 { "luksResume", action_luksResume, 1, 1, N_("<device>"), N_("Resume suspended LUKS device.") },
1203 { "luksHeaderBackup", action_luksBackup,1,1, N_("<device>"), N_("Backup LUKS device header and keyslots") },
1204 { "luksHeaderRestore",action_luksRestore,1,1,N_("<device>"), N_("Restore LUKS device header and keyslots") },
1208 static void help(poptContext popt_context,
1209 enum poptCallbackReason reason __attribute__((unused)),
1210 struct poptOption *key,
1211 const char *arg __attribute__((unused)),
1212 void *data __attribute__((unused)))
1214 if (key->shortName == '?') {
1215 struct action_type *action;
1217 log_std("%s\n",PACKAGE_STRING);
1219 poptPrintHelp(popt_context, stdout, 0);
1222 "<action> is one of:\n"));
1224 for(action = action_types; action->type; action++)
1225 log_std("\t%s %s - %s\n", action->type, _(action->arg_desc), _(action->desc));
1228 "You can also use old <action> syntax aliases:\n"
1229 "\topen: create (plainOpen), luksOpen, loopaesOpen, tcryptOpen\n"
1230 "\tclose: remove (plainClose), luksClose, loopaesClose, tcryptClose\n"));
1232 "<name> is the device to create under %s\n"
1233 "<device> is the encrypted device\n"
1234 "<key slot> is the LUKS key slot number to modify\n"
1235 "<key file> optional key file for the new key for luksAddKey action\n"),
1238 log_std(_("\nDefault compiled-in key and passphrase parameters:\n"
1239 "\tMaximum keyfile size: %dkB, "
1240 "Maximum interactive passphrase length %d (characters)\n"
1241 "Default PBKDF2 iteration time for LUKS: %d (ms)\n"),
1242 DEFAULT_KEYFILE_SIZE_MAXKB, DEFAULT_PASSPHRASE_SIZE_MAX,
1243 DEFAULT_LUKS1_ITER_TIME);
1245 log_std(_("\nDefault compiled-in device cipher parameters:\n"
1246 "\tloop-AES: %s, Key %d bits\n"
1247 "\tplain: %s, Key: %d bits, Password hashing: %s\n"
1248 "\tLUKS1: %s, Key: %d bits, LUKS header hashing: %s, RNG: %s\n"),
1249 DEFAULT_LOOPAES_CIPHER, DEFAULT_LOOPAES_KEYBITS,
1250 DEFAULT_CIPHER(PLAIN), DEFAULT_PLAIN_KEYBITS, DEFAULT_PLAIN_HASH,
1251 DEFAULT_CIPHER(LUKS1), DEFAULT_LUKS1_KEYBITS, DEFAULT_LUKS1_HASH,
1255 usage(popt_context, EXIT_SUCCESS, NULL, NULL);
1258 static void help_args(struct action_type *action, poptContext popt_context)
1262 snprintf(buf, sizeof(buf), _("%s: requires %s as arguments"), action->type, action->arg_desc);
1263 usage(popt_context, EXIT_FAILURE, buf, poptGetInvocationName(popt_context));
1266 static int run_action(struct action_type *action)
1270 log_dbg("Running command %s.", action->type);
1272 if (action->required_memlock)
1273 crypt_memory_lock(NULL, 1);
1275 r = action->handler();
1277 if (action->required_memlock)
1278 crypt_memory_lock(NULL, 0);
1280 /* Some functions returns keyslot # */
1285 return translate_errno(r);
1288 int main(int argc, const char **argv)
1290 static char *popt_tmp;
1291 static struct poptOption popt_help_options[] = {
1292 { NULL, '\0', POPT_ARG_CALLBACK, help, 0, NULL, NULL },
1293 { "help", '?', POPT_ARG_NONE, NULL, 0, N_("Show this help message"), NULL },
1294 { "usage", '\0', POPT_ARG_NONE, NULL, 0, N_("Display brief usage"), NULL },
1297 static struct poptOption popt_options[] = {
1298 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, popt_help_options, 0, N_("Help options:"), NULL },
1299 { "version", '\0', POPT_ARG_NONE, &opt_version_mode, 0, N_("Print package version"), NULL },
1300 { "verbose", 'v', POPT_ARG_NONE, &opt_verbose, 0, N_("Shows more detailed error messages"), NULL },
1301 { "debug", '\0', POPT_ARG_NONE, &opt_debug, 0, N_("Show debug messages"), NULL },
1302 { "cipher", 'c', POPT_ARG_STRING, &opt_cipher, 0, N_("The cipher used to encrypt the disk (see /proc/crypto)"), NULL },
1303 { "hash", 'h', POPT_ARG_STRING, &opt_hash, 0, N_("The hash used to create the encryption key from the passphrase"), NULL },
1304 { "verify-passphrase", 'y', POPT_ARG_NONE, &opt_verify_passphrase, 0, N_("Verifies the passphrase by asking for it twice"), NULL },
1305 { "key-file", 'd', POPT_ARG_STRING, &opt_key_file, 5, N_("Read the key from a file."), NULL },
1306 { "master-key-file", '\0', POPT_ARG_STRING, &opt_master_key_file, 0, N_("Read the volume (master) key from file."), NULL },
1307 { "dump-master-key", '\0', POPT_ARG_NONE, &opt_dump_master_key, 0, N_("Dump volume (master) key instead of keyslots info."), NULL },
1308 { "key-size", 's', POPT_ARG_INT, &opt_key_size, 0, N_("The size of the encryption key"), N_("BITS") },
1309 { "keyfile-size", 'l', POPT_ARG_LONG, &opt_keyfile_size, 0, N_("Limits the read from keyfile"), N_("bytes") },
1310 { "keyfile-offset", '\0', POPT_ARG_LONG, &opt_keyfile_offset, 0, N_("Number of bytes to skip in keyfile"), N_("bytes") },
1311 { "new-keyfile-size", '\0', POPT_ARG_LONG, &opt_new_keyfile_size, 0, N_("Limits the read from newly added keyfile"), N_("bytes") },
1312 { "new-keyfile-offset",'\0', POPT_ARG_LONG, &opt_new_keyfile_offset, 0, N_("Number of bytes to skip in newly added keyfile"), N_("bytes") },
1313 { "key-slot", 'S', POPT_ARG_INT, &opt_key_slot, 0, N_("Slot number for new key (default is first free)"), NULL },
1314 { "size", 'b', POPT_ARG_STRING, &popt_tmp, 1, N_("The size of the device"), N_("SECTORS") },
1315 { "offset", 'o', POPT_ARG_STRING, &popt_tmp, 2, N_("The start offset in the backend device"), N_("SECTORS") },
1316 { "skip", 'p', POPT_ARG_STRING, &popt_tmp, 3, N_("How many sectors of the encrypted data to skip at the beginning"), N_("SECTORS") },
1317 { "readonly", 'r', POPT_ARG_NONE, &opt_readonly, 0, N_("Create a readonly mapping"), NULL },
1318 { "iter-time", 'i', POPT_ARG_INT, &opt_iteration_time, 0, N_("PBKDF2 iteration time for LUKS (in ms)"), N_("msecs") },
1319 { "batch-mode", 'q', POPT_ARG_NONE, &opt_batch_mode, 0, N_("Do not ask for confirmation"), NULL },
1320 { "timeout", 't', POPT_ARG_INT, &opt_timeout, 0, N_("Timeout for interactive passphrase prompt (in seconds)"), N_("secs") },
1321 { "tries", 'T', POPT_ARG_INT, &opt_tries, 0, N_("How often the input of the passphrase can be retried"), NULL },
1322 { "align-payload", '\0', POPT_ARG_INT, &opt_align_payload, 0, N_("Align payload at <n> sector boundaries - for luksFormat"), N_("SECTORS") },
1323 { "header-backup-file",'\0', POPT_ARG_STRING, &opt_header_backup_file, 0, N_("File with LUKS header and keyslots backup."), NULL },
1324 { "use-random", '\0', POPT_ARG_NONE, &opt_random, 0, N_("Use /dev/random for generating volume key."), NULL },
1325 { "use-urandom", '\0', POPT_ARG_NONE, &opt_urandom, 0, N_("Use /dev/urandom for generating volume key."), NULL },
1326 { "shared", '\0', POPT_ARG_NONE, &opt_shared, 0, N_("Share device with another non-overlapping crypt segment."), NULL },
1327 { "uuid", '\0', POPT_ARG_STRING, &opt_uuid, 0, N_("UUID for device to use."), NULL },
1328 { "allow-discards", '\0', POPT_ARG_NONE, &opt_allow_discards, 0, N_("Allow discards (aka TRIM) requests for device."), NULL },
1329 { "header", '\0', POPT_ARG_STRING, &opt_header_device, 0, N_("Device or file with separated LUKS header."), NULL },
1330 { "test-passphrase", '\0', POPT_ARG_NONE, &opt_test_passphrase, 0, N_("Do not activate device, just check passphrase."), NULL },
1331 { "hidden", '\0', POPT_ARG_NONE, &opt_hidden, 0, N_("Use hidden header (hidden TCRYPT device) ."), NULL },
1332 { "type", 'M', POPT_ARG_STRING, &opt_type, 0, N_("Type of device metadata: luks, plain, loopaes, tcrypt."), NULL },
1335 poptContext popt_context;
1336 struct action_type *action;
1340 crypt_set_log_callback(NULL, tool_log, NULL);
1342 setlocale(LC_ALL, "");
1343 bindtextdomain(PACKAGE, LOCALEDIR);
1344 textdomain(PACKAGE);
1346 crypt_fips_self_check(NULL);
1348 popt_context = poptGetContext(PACKAGE, argc, argv, popt_options, 0);
1349 poptSetOtherOptionHelp(popt_context,
1350 _("[OPTION...] <action> <action-specific>"));
1352 while((r = poptGetNextOpt(popt_context)) > 0) {
1353 unsigned long long ull_value;
1357 if (opt_keyfiles_count < MAX_KEYFILES)
1358 opt_keyfiles[opt_keyfiles_count++] = poptGetOptArg(popt_context);
1363 ull_value = strtoull(popt_tmp, &endp, 0);
1364 if (*endp || !*popt_tmp ||
1365 (errno == ERANGE && ull_value == ULLONG_MAX) ||
1366 (errno != 0 && ull_value == 0))
1367 r = POPT_ERROR_BADNUMBER;
1371 opt_size = ull_value;
1374 opt_offset = ull_value;
1377 opt_skip = ull_value;
1387 usage(popt_context, EXIT_FAILURE, poptStrerror(r),
1388 poptBadOption(popt_context, POPT_BADOPTION_NOALIAS));
1389 if (opt_version_mode) {
1390 log_std("%s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
1391 poptFreeContext(popt_context);
1395 if (!(aname = poptGetArg(popt_context)))
1396 usage(popt_context, EXIT_FAILURE, _("Argument <action> missing."),
1397 poptGetInvocationName(popt_context));
1400 action_argv = poptGetArgs(popt_context);
1401 /* Make return values of poptGetArgs more consistent in case of remaining argc = 0 */
1403 action_argv = null_action_argv;
1405 /* Count args, somewhat unnice, change? */
1406 while(action_argv[action_argc] != NULL)
1409 /* Handle aliases */
1410 if (!strcmp(aname, "create")) {
1411 /* create command had historically switched arguments */
1412 if (action_argv[0] && action_argv[1]) {
1413 const char *tmp = action_argv[0];
1414 action_argv[0] = action_argv[1];
1415 action_argv[1] = tmp;
1419 } else if (!strcmp(aname, "plainOpen")) {
1422 } else if (!strcmp(aname, "luksOpen")) {
1425 } else if (!strcmp(aname, "loopaesOpen")) {
1427 opt_type = "loopaes";
1428 } else if (!strcmp(aname, "tcryptOpen")) {
1430 opt_type = "tcrypt";
1431 } else if (!strcmp(aname, "remove") ||
1432 !strcmp(aname, "plainClose") ||
1433 !strcmp(aname, "luksClose") ||
1434 !strcmp(aname, "loopaesClose") ||
1435 !strcmp(aname, "tcryptClose")) {
1439 for(action = action_types; action->type; action++)
1440 if (strcmp(action->type, aname) == 0)
1444 usage(popt_context, EXIT_FAILURE, _("Unknown action."),
1445 poptGetInvocationName(popt_context));
1447 if(action_argc < action->required_action_argc)
1448 help_args(action, popt_context);
1450 /* FIXME: rewrite this from scratch */
1452 if (opt_shared && (strcmp(aname, "open") || strcmp(opt_type, "plain")) )
1453 usage(popt_context, EXIT_FAILURE,
1454 _("Option --shared is allowed only for open of plain device.\n"),
1455 poptGetInvocationName(popt_context));
1457 if (opt_allow_discards && strcmp(aname, "open"))
1458 usage(popt_context, EXIT_FAILURE,
1459 _("Option --allow-discards is allowed only for open operation.\n"),
1460 poptGetInvocationName(popt_context));
1463 strcmp(aname, "luksFormat") &&
1464 strcmp(aname, "open") &&
1465 strcmp(aname, "benchmark"))
1466 usage(popt_context, EXIT_FAILURE,
1467 _("Option --key-size is allowed only for luksFormat, open and benchmark.\n"
1468 "To limit read from keyfile use --keyfile-size=(bytes)."),
1469 poptGetInvocationName(popt_context));
1471 if (opt_test_passphrase && (strcmp(aname, "open") ||
1472 (strcmp(opt_type, "luks") && strcmp(opt_type, "tcrypt"))))
1473 usage(popt_context, EXIT_FAILURE,
1474 _("Option --test-passphrase is allowed only for open of LUKS and TCRYPT devices.\n"),
1475 poptGetInvocationName(popt_context));
1477 if (opt_key_size % 8)
1478 usage(popt_context, EXIT_FAILURE,
1479 _("Key size must be a multiple of 8 bits"),
1480 poptGetInvocationName(popt_context));
1482 if (!strcmp(aname, "luksKillSlot") && action_argc > 1)
1483 opt_key_slot = atoi(action_argv[1]);
1484 if (opt_key_slot != CRYPT_ANY_SLOT &&
1485 (opt_key_slot < 0 || opt_key_slot >= crypt_keyslot_max(CRYPT_LUKS1)))
1486 usage(popt_context, EXIT_FAILURE, _("Key slot is invalid."),
1487 poptGetInvocationName(popt_context));
1489 if ((!strcmp(aname, "luksRemoveKey") ||
1490 !strcmp(aname, "luksFormat")) &&
1493 log_err(_("Option --key-file takes precedence over specified key file argument.\n"));
1495 opt_key_file = action_argv[1];
1498 if (opt_keyfile_size < 0 || opt_new_keyfile_size < 0 || opt_key_size < 0 ||
1499 opt_keyfile_offset < 0 || opt_new_keyfile_offset < 0)
1500 usage(popt_context, EXIT_FAILURE,
1501 _("Negative number for option not permitted."),
1502 poptGetInvocationName(popt_context));
1504 if (opt_random && opt_urandom)
1505 usage(popt_context, EXIT_FAILURE, _("Only one of --use-[u]random options is allowed."),
1506 poptGetInvocationName(popt_context));
1508 if ((opt_random || opt_urandom) && strcmp(aname, "luksFormat"))
1509 usage(popt_context, EXIT_FAILURE, _("Option --use-[u]random is allowed only for luksFormat."),
1510 poptGetInvocationName(popt_context));
1512 if (opt_uuid && strcmp(aname, "luksFormat") && strcmp(aname, "luksUUID"))
1513 usage(popt_context, EXIT_FAILURE, _("Option --uuid is allowed only for luksFormat and luksUUID."),
1514 poptGetInvocationName(popt_context));
1516 if (opt_align_payload && strcmp(aname, "luksFormat"))
1517 usage(popt_context, EXIT_FAILURE, _("Option --align-payload is allowed only for luksFormat."),
1518 poptGetInvocationName(popt_context));
1520 if (opt_skip && (strcmp(aname, "open") ||
1521 (strcmp(opt_type, "plain") && strcmp(opt_type, "loopaes"))))
1522 usage(popt_context, EXIT_FAILURE,
1523 _("Option --skip is supported only for open of plain and loopaes devices.\n"),
1524 poptGetInvocationName(popt_context));
1526 if (opt_offset && (strcmp(aname, "open") ||
1527 (strcmp(opt_type, "plain") && strcmp(opt_type, "loopaes"))))
1528 usage(popt_context, EXIT_FAILURE,
1529 _("Option --offset is supported only for open of plain and loopaes devices.\n"),
1530 poptGetInvocationName(popt_context));
1532 if (opt_hidden && strcmp(aname, "tcryptDump") &&
1533 (strcmp(aname, "open") || strcmp(opt_type, "tcrypt")))
1534 usage(popt_context, EXIT_FAILURE,
1535 _("Option --hidden is supported only for TCRYPT device.\n"),
1536 poptGetInvocationName(popt_context));
1540 crypt_set_debug_level(-1);
1541 dbg_version_and_cmd(argc, argv);
1544 r = run_action(action);
1545 poptFreeContext(popt_context);