2 * Copyright (c) 2004-2010 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
32 #include <sys/sysctl.h>
33 #include <sys/resource.h>
34 #include <opencrypto/cryptodev.h>
42 #include <readpassphrase.h>
51 #include <geom/eli/g_eli.h>
52 #include <geom/eli/pkcs5v2.h>
54 #include "core/geom.h"
55 #include "misc/subr.h"
58 uint32_t lib_version = G_LIB_VERSION;
59 uint32_t version = G_ELI_VERSION;
61 #define GELI_BACKUP_DIR "/var/backups/"
62 #define GELI_ENC_ALGO "aes"
64 static void eli_main(struct gctl_req *req, unsigned flags);
65 static void eli_init(struct gctl_req *req);
66 static void eli_attach(struct gctl_req *req);
67 static void eli_configure(struct gctl_req *req);
68 static void eli_setkey(struct gctl_req *req);
69 static void eli_delkey(struct gctl_req *req);
70 static void eli_resume(struct gctl_req *req);
71 static void eli_kill(struct gctl_req *req);
72 static void eli_backup(struct gctl_req *req);
73 static void eli_restore(struct gctl_req *req);
74 static void eli_resize(struct gctl_req *req);
75 static void eli_version(struct gctl_req *req);
76 static void eli_clear(struct gctl_req *req);
77 static void eli_dump(struct gctl_req *req);
79 static int eli_backup_create(struct gctl_req *req, const char *prov,
85 * init [-bgPTv] [-a aalgo] [-B backupfile] [-e ealgo] [-i iterations] [-l keylen] [-J newpassfile] [-K newkeyfile] [-s sectorsize] [-V version] prov
86 * label - alias for 'init'
87 * attach [-dprv] [-j passfile] [-k keyfile] prov
88 * detach [-fl] prov ...
89 * stop - alias for 'detach'
90 * onetime [-d] [-a aalgo] [-e ealgo] [-l keylen] prov
91 * configure [-bBgGtT] prov ...
92 * setkey [-pPv] [-n keyno] [-j passfile] [-J newpassfile] [-k keyfile] [-K newkeyfile] prov
93 * delkey [-afv] [-n keyno] prov
94 * suspend [-v] -a | prov ...
95 * resume [-pv] [-j passfile] [-k keyfile] prov
96 * kill [-av] [prov ...]
97 * backup [-v] prov file
98 * restore [-fv] file prov
99 * resize [-v] -s oldsize prov
101 * clear [-v] prov ...
104 struct g_command class_commands[] = {
105 { "init", G_FLAG_VERBOSE, eli_main,
107 { 'a', "aalgo", "", G_TYPE_STRING },
108 { 'b', "boot", NULL, G_TYPE_BOOL },
109 { 'B', "backupfile", "", G_TYPE_STRING },
110 { 'e', "ealgo", "", G_TYPE_STRING },
111 { 'g', "geliboot", NULL, G_TYPE_BOOL },
112 { 'i', "iterations", "-1", G_TYPE_NUMBER },
113 { 'J', "newpassfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
114 { 'K', "newkeyfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
115 { 'l', "keylen", "0", G_TYPE_NUMBER },
116 { 'P', "nonewpassphrase", NULL, G_TYPE_BOOL },
117 { 's', "sectorsize", "0", G_TYPE_NUMBER },
118 { 'T', "notrim", NULL, G_TYPE_BOOL },
119 { 'V', "mdversion", "-1", G_TYPE_NUMBER },
122 "[-bgPTv] [-a aalgo] [-B backupfile] [-e ealgo] [-i iterations] [-l keylen] [-J newpassfile] [-K newkeyfile] [-s sectorsize] [-V version] prov"
124 { "label", G_FLAG_VERBOSE, eli_main,
126 { 'a', "aalgo", "", G_TYPE_STRING },
127 { 'b', "boot", NULL, G_TYPE_BOOL },
128 { 'B', "backupfile", "", G_TYPE_STRING },
129 { 'e', "ealgo", "", G_TYPE_STRING },
130 { 'g', "geliboot", NULL, G_TYPE_BOOL },
131 { 'i', "iterations", "-1", G_TYPE_NUMBER },
132 { 'J', "newpassfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
133 { 'K', "newkeyfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
134 { 'l', "keylen", "0", G_TYPE_NUMBER },
135 { 'P', "nonewpassphrase", NULL, G_TYPE_BOOL },
136 { 's', "sectorsize", "0", G_TYPE_NUMBER },
137 { 'V', "mdversion", "-1", G_TYPE_NUMBER },
140 "- an alias for 'init'"
142 { "attach", G_FLAG_VERBOSE | G_FLAG_LOADKLD, eli_main,
144 { 'd', "detach", NULL, G_TYPE_BOOL },
145 { 'j', "passfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
146 { 'k', "keyfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
147 { 'p', "nopassphrase", NULL, G_TYPE_BOOL },
148 { 'r', "readonly", NULL, G_TYPE_BOOL },
151 "[-dprv] [-j passfile] [-k keyfile] prov"
155 { 'f', "force", NULL, G_TYPE_BOOL },
156 { 'l', "last", NULL, G_TYPE_BOOL },
163 { 'f', "force", NULL, G_TYPE_BOOL },
164 { 'l', "last", NULL, G_TYPE_BOOL },
167 "- an alias for 'detach'"
169 { "onetime", G_FLAG_VERBOSE | G_FLAG_LOADKLD, NULL,
171 { 'a', "aalgo", "", G_TYPE_STRING },
172 { 'd', "detach", NULL, G_TYPE_BOOL },
173 { 'e', "ealgo", GELI_ENC_ALGO, G_TYPE_STRING },
174 { 'l', "keylen", "0", G_TYPE_NUMBER },
175 { 's', "sectorsize", "0", G_TYPE_NUMBER },
176 { 'T', "notrim", NULL, G_TYPE_BOOL },
179 "[-dT] [-a aalgo] [-e ealgo] [-l keylen] [-s sectorsize] prov"
181 { "configure", G_FLAG_VERBOSE, eli_main,
183 { 'b', "boot", NULL, G_TYPE_BOOL },
184 { 'B', "noboot", NULL, G_TYPE_BOOL },
185 { 'g', "geliboot", NULL, G_TYPE_BOOL },
186 { 'G', "nogeliboot", NULL, G_TYPE_BOOL },
187 { 't', "trim", NULL, G_TYPE_BOOL },
188 { 'T', "notrim", NULL, G_TYPE_BOOL },
193 { "setkey", G_FLAG_VERBOSE, eli_main,
195 { 'i', "iterations", "-1", G_TYPE_NUMBER },
196 { 'j', "passfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
197 { 'J', "newpassfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
198 { 'k', "keyfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
199 { 'K', "newkeyfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
200 { 'n', "keyno", "-1", G_TYPE_NUMBER },
201 { 'p', "nopassphrase", NULL, G_TYPE_BOOL },
202 { 'P', "nonewpassphrase", NULL, G_TYPE_BOOL },
205 "[-pPv] [-n keyno] [-i iterations] [-j passfile] [-J newpassfile] [-k keyfile] [-K newkeyfile] prov"
207 { "delkey", G_FLAG_VERBOSE, eli_main,
209 { 'a', "all", NULL, G_TYPE_BOOL },
210 { 'f', "force", NULL, G_TYPE_BOOL },
211 { 'n', "keyno", "-1", G_TYPE_NUMBER },
214 "[-afv] [-n keyno] prov"
216 { "suspend", G_FLAG_VERBOSE, NULL,
218 { 'a', "all", NULL, G_TYPE_BOOL },
223 { "resume", G_FLAG_VERBOSE, eli_main,
225 { 'j', "passfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
226 { 'k', "keyfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
227 { 'p', "nopassphrase", NULL, G_TYPE_BOOL },
230 "[-pv] [-j passfile] [-k keyfile] prov"
232 { "kill", G_FLAG_VERBOSE, eli_main,
234 { 'a', "all", NULL, G_TYPE_BOOL },
239 { "backup", G_FLAG_VERBOSE, eli_main, G_NULL_OPTS,
242 { "restore", G_FLAG_VERBOSE, eli_main,
244 { 'f', "force", NULL, G_TYPE_BOOL },
249 { "resize", G_FLAG_VERBOSE, eli_main,
251 { 's', "oldsize", NULL, G_TYPE_NUMBER },
254 "[-v] -s oldsize prov"
256 { "version", G_FLAG_LOADKLD, eli_main, G_NULL_OPTS,
259 { "clear", G_FLAG_VERBOSE, eli_main, G_NULL_OPTS,
262 { "dump", G_FLAG_VERBOSE, eli_main, G_NULL_OPTS,
268 static int verbose = 0;
273 eli_protect(struct gctl_req *req)
277 /* Disable core dumps. */
280 if (setrlimit(RLIMIT_CORE, &rl) == -1) {
281 gctl_error(req, "Cannot disable core dumps: %s.",
285 /* Disable swapping. */
286 if (mlockall(MCL_FUTURE) == -1) {
287 gctl_error(req, "Cannot lock memory: %s.", strerror(errno));
294 eli_main(struct gctl_req *req, unsigned int flags)
298 if (eli_protect(req) == -1)
301 if ((flags & G_FLAG_VERBOSE) != 0)
304 name = gctl_get_ascii(req, "verb");
306 gctl_error(req, "No '%s' argument.", "verb");
309 if (strcmp(name, "init") == 0 || strcmp(name, "label") == 0)
311 else if (strcmp(name, "attach") == 0)
313 else if (strcmp(name, "configure") == 0)
315 else if (strcmp(name, "setkey") == 0)
317 else if (strcmp(name, "delkey") == 0)
319 else if (strcmp(name, "resume") == 0)
321 else if (strcmp(name, "kill") == 0)
323 else if (strcmp(name, "backup") == 0)
325 else if (strcmp(name, "restore") == 0)
327 else if (strcmp(name, "resize") == 0)
329 else if (strcmp(name, "version") == 0)
331 else if (strcmp(name, "dump") == 0)
333 else if (strcmp(name, "clear") == 0)
336 gctl_error(req, "Unknown command: %s.", name);
340 eli_is_attached(const char *prov)
342 char name[MAXPATHLEN];
345 * Not the best way to do it, but the easiest.
346 * We try to open provider and check if it is a GEOM provider
347 * by asking about its sectorsize.
349 snprintf(name, sizeof(name), "%s%s", prov, G_ELI_SUFFIX);
350 return (g_get_sectorsize(name) > 0);
354 eli_genkey_files(struct gctl_req *req, bool new, const char *type,
355 struct hmac_ctx *ctxp, char *passbuf, size_t passbufsize)
357 char *p, buf[BUFSIZE], argname[16];
362 assert((strcmp(type, "keyfile") == 0 && ctxp != NULL &&
363 passbuf == NULL && passbufsize == 0) ||
364 (strcmp(type, "passfile") == 0 && ctxp == NULL &&
365 passbuf != NULL && passbufsize > 0));
366 assert(strcmp(type, "keyfile") == 0 || passbuf[0] == '\0');
369 snprintf(argname, sizeof(argname), "%s%s%d",
370 new ? "new" : "", type, i);
372 /* No more {key,pass}files? */
373 if (!gctl_has_param(req, argname))
376 file = gctl_get_ascii(req, "%s", argname);
377 assert(file != NULL);
379 if (strcmp(file, "-") == 0)
382 fd = open(file, O_RDONLY);
384 gctl_error(req, "Cannot open %s %s: %s.",
385 type, file, strerror(errno));
389 if (strcmp(type, "keyfile") == 0) {
390 while ((done = read(fd, buf, sizeof(buf))) > 0)
391 g_eli_crypto_hmac_update(ctxp, buf, done);
392 } else /* if (strcmp(type, "passfile") == 0) */ {
393 assert(strcmp(type, "passfile") == 0);
395 while ((done = read(fd, buf, sizeof(buf) - 1)) > 0) {
397 p = strchr(buf, '\n');
402 if (strlcat(passbuf, buf, passbufsize) >=
405 "Passphrase in %s too long.", file);
406 bzero(buf, sizeof(buf));
414 if (strcmp(file, "-") != 0)
416 bzero(buf, sizeof(buf));
418 gctl_error(req, "Cannot read %s %s: %s.",
419 type, file, strerror(error));
427 eli_genkey_passphrase_prompt(struct gctl_req *req, bool new, char *passbuf,
434 new ? "Enter new passphrase: " : "Enter passphrase: ",
435 passbuf, passbufsize, RPP_ECHO_OFF | RPP_REQUIRE_TTY);
437 bzero(passbuf, passbufsize);
438 gctl_error(req, "Cannot read passphrase: %s.",
444 char tmpbuf[BUFSIZE];
446 p = readpassphrase("Reenter new passphrase: ",
447 tmpbuf, sizeof(tmpbuf),
448 RPP_ECHO_OFF | RPP_REQUIRE_TTY);
450 bzero(passbuf, passbufsize);
452 "Cannot read passphrase: %s.",
457 if (strcmp(passbuf, tmpbuf) != 0) {
458 bzero(passbuf, passbufsize);
459 fprintf(stderr, "They didn't match.\n");
462 bzero(tmpbuf, sizeof(tmpbuf));
470 eli_genkey_passphrase(struct gctl_req *req, struct g_eli_metadata *md, bool new,
471 struct hmac_ctx *ctxp)
473 char passbuf[BUFSIZE];
478 gctl_get_int(req, new ? "nonewpassphrase" : "nopassphrase");
480 if (gctl_has_param(req, new ? "newpassfile0" : "passfile0")) {
482 "Options -%c and -%c are mutually exclusive.",
483 new ? 'J' : 'j', new ? 'P' : 'p');
489 if (!new && md->md_iterations == -1) {
490 gctl_error(req, "Missing -p flag.");
494 nfiles = eli_genkey_files(req, new, "passfile", NULL, passbuf,
498 else if (nfiles == 0) {
499 if (eli_genkey_passphrase_prompt(req, new, passbuf,
500 sizeof(passbuf)) == -1) {
505 * Field md_iterations equal to -1 means "choose some sane
508 if (md->md_iterations == -1) {
511 printf("Calculating number of iterations...\n");
512 md->md_iterations = pkcs5v2_calculate(2000000);
513 assert(md->md_iterations > 0);
515 printf("Done, using %d iterations.\n",
520 * If md_iterations is equal to 0, user doesn't want PKCS#5v2.
522 if (md->md_iterations == 0) {
523 g_eli_crypto_hmac_update(ctxp, md->md_salt,
524 sizeof(md->md_salt));
525 g_eli_crypto_hmac_update(ctxp, passbuf, strlen(passbuf));
526 } else /* if (md->md_iterations > 0) */ {
527 unsigned char dkey[G_ELI_USERKEYLEN];
529 pkcs5v2_genkey(dkey, sizeof(dkey), md->md_salt,
530 sizeof(md->md_salt), passbuf, md->md_iterations);
531 g_eli_crypto_hmac_update(ctxp, dkey, sizeof(dkey));
532 bzero(dkey, sizeof(dkey));
534 bzero(passbuf, sizeof(passbuf));
539 static unsigned char *
540 eli_genkey(struct gctl_req *req, struct g_eli_metadata *md, unsigned char *key,
548 gctl_get_int(req, new ? "nonewpassphrase" : "nopassphrase");
550 g_eli_crypto_hmac_init(&ctx, NULL, 0);
552 nfiles = eli_genkey_files(req, new, "keyfile", &ctx, NULL, 0);
555 else if (nfiles == 0 && nopassphrase) {
556 gctl_error(req, "No key components given.");
560 if (eli_genkey_passphrase(req, md, new, &ctx) == -1)
563 g_eli_crypto_hmac_final(&ctx, key, 0);
569 eli_metadata_read(struct gctl_req *req, const char *prov,
570 struct g_eli_metadata *md)
572 unsigned char sector[sizeof(struct g_eli_metadata)];
575 if (g_get_sectorsize(prov) == 0) {
578 /* This is a file probably. */
579 fd = open(prov, O_RDONLY);
581 gctl_error(req, "Cannot open %s: %s.", prov,
585 if (read(fd, sector, sizeof(sector)) != sizeof(sector)) {
586 gctl_error(req, "Cannot read metadata from %s: %s.",
587 prov, strerror(errno));
593 /* This is a GEOM provider. */
594 error = g_metadata_read(prov, sector, sizeof(sector),
597 gctl_error(req, "Cannot read metadata from %s: %s.",
598 prov, strerror(error));
602 error = eli_metadata_decode(sector, md);
608 "Provider's %s metadata version %u is too new.\n"
609 "geli: The highest supported version is %u.",
610 prov, (unsigned int)md->md_version, G_ELI_VERSION);
613 gctl_error(req, "Inconsistent provider's %s metadata.", prov);
617 "Unexpected error while decoding provider's %s metadata: %s.",
618 prov, strerror(error));
625 eli_metadata_store(struct gctl_req *req, const char *prov,
626 struct g_eli_metadata *md)
628 unsigned char sector[sizeof(struct g_eli_metadata)];
631 eli_metadata_encode(md, sector);
632 if (g_get_sectorsize(prov) == 0) {
635 /* This is a file probably. */
636 fd = open(prov, O_WRONLY | O_TRUNC);
638 gctl_error(req, "Cannot open %s: %s.", prov,
640 bzero(sector, sizeof(sector));
643 if (write(fd, sector, sizeof(sector)) != sizeof(sector)) {
644 gctl_error(req, "Cannot write metadata to %s: %s.",
645 prov, strerror(errno));
646 bzero(sector, sizeof(sector));
652 /* This is a GEOM provider. */
653 error = g_metadata_store(prov, sector, sizeof(sector));
655 gctl_error(req, "Cannot write metadata to %s: %s.",
656 prov, strerror(errno));
657 bzero(sector, sizeof(sector));
661 bzero(sector, sizeof(sector));
666 eli_init(struct gctl_req *req)
668 struct g_eli_metadata md;
669 unsigned char sector[sizeof(struct g_eli_metadata)] __aligned(4);
670 unsigned char key[G_ELI_USERKEYLEN];
671 char backfile[MAXPATHLEN];
672 const char *str, *prov;
673 unsigned int secsize, version;
678 nargs = gctl_get_int(req, "nargs");
680 gctl_error(req, "Invalid number of arguments.");
683 prov = gctl_get_ascii(req, "arg0");
684 mediasize = g_get_mediasize(prov);
685 secsize = g_get_sectorsize(prov);
686 if (mediasize == 0 || secsize == 0) {
687 gctl_error(req, "Cannot get informations about %s: %s.", prov,
692 bzero(&md, sizeof(md));
693 strlcpy(md.md_magic, G_ELI_MAGIC, sizeof(md.md_magic));
694 val = gctl_get_intmax(req, "mdversion");
696 version = G_ELI_VERSION;
697 } else if (val < 0 || val > G_ELI_VERSION) {
699 "Invalid version specified should be between %u and %u.",
700 G_ELI_VERSION_00, G_ELI_VERSION);
705 md.md_version = version;
707 if (gctl_get_int(req, "boot"))
708 md.md_flags |= G_ELI_FLAG_BOOT;
709 if (gctl_get_int(req, "geliboot"))
710 md.md_flags |= G_ELI_FLAG_GELIBOOT;
711 if (gctl_get_int(req, "notrim"))
712 md.md_flags |= G_ELI_FLAG_NODELETE;
713 md.md_ealgo = CRYPTO_ALGORITHM_MIN - 1;
714 str = gctl_get_ascii(req, "aalgo");
716 if (version < G_ELI_VERSION_01) {
718 "Data authentication is supported starting from version %u.",
722 md.md_aalgo = g_eli_str2aalgo(str);
723 if (md.md_aalgo >= CRYPTO_ALGORITHM_MIN &&
724 md.md_aalgo <= CRYPTO_ALGORITHM_MAX) {
725 md.md_flags |= G_ELI_FLAG_AUTH;
728 * For backward compatibility, check if the -a option
729 * was used to provide encryption algorithm.
731 md.md_ealgo = g_eli_str2ealgo(str);
732 if (md.md_ealgo < CRYPTO_ALGORITHM_MIN ||
733 md.md_ealgo > CRYPTO_ALGORITHM_MAX) {
735 "Invalid authentication algorithm.");
738 fprintf(stderr, "warning: The -e option, not "
739 "the -a option is now used to specify "
740 "encryption algorithm to use.\n");
744 if (md.md_ealgo < CRYPTO_ALGORITHM_MIN ||
745 md.md_ealgo > CRYPTO_ALGORITHM_MAX) {
746 str = gctl_get_ascii(req, "ealgo");
748 if (version < G_ELI_VERSION_05)
753 md.md_ealgo = g_eli_str2ealgo(str);
754 if (md.md_ealgo < CRYPTO_ALGORITHM_MIN ||
755 md.md_ealgo > CRYPTO_ALGORITHM_MAX) {
756 gctl_error(req, "Invalid encryption algorithm.");
759 if (md.md_ealgo == CRYPTO_CAMELLIA_CBC &&
760 version < G_ELI_VERSION_04) {
762 "Camellia-CBC algorithm is supported starting from version %u.",
766 if (md.md_ealgo == CRYPTO_AES_XTS &&
767 version < G_ELI_VERSION_05) {
769 "AES-XTS algorithm is supported starting from version %u.",
774 val = gctl_get_intmax(req, "keylen");
776 md.md_keylen = g_eli_keylen(md.md_ealgo, md.md_keylen);
777 if (md.md_keylen == 0) {
778 gctl_error(req, "Invalid key length.");
781 md.md_provsize = mediasize;
783 val = gctl_get_intmax(req, "iterations");
788 * Don't allow to set iterations when there will be no
791 nonewpassphrase = gctl_get_int(req, "nonewpassphrase");
792 if (nonewpassphrase) {
794 "Options -i and -P are mutually exclusive.");
798 md.md_iterations = val;
800 val = gctl_get_intmax(req, "sectorsize");
802 md.md_sectorsize = secsize;
804 if (val < 0 || (val % secsize) != 0 || !powerof2(val)) {
805 gctl_error(req, "Invalid sector size.");
808 if (val > sysconf(_SC_PAGE_SIZE)) {
810 "warning: Using sectorsize bigger than the page size!\n");
812 md.md_sectorsize = val;
816 arc4random_buf(md.md_salt, sizeof(md.md_salt));
817 arc4random_buf(md.md_mkeys, sizeof(md.md_mkeys));
819 /* Generate user key. */
820 if (eli_genkey(req, &md, key, true) == NULL) {
821 bzero(key, sizeof(key));
822 bzero(&md, sizeof(md));
826 /* Encrypt the first and the only Master Key. */
827 error = g_eli_mkey_encrypt(md.md_ealgo, key, md.md_keylen, md.md_mkeys);
828 bzero(key, sizeof(key));
830 bzero(&md, sizeof(md));
831 gctl_error(req, "Cannot encrypt Master Key: %s.",
836 eli_metadata_encode(&md, sector);
837 bzero(&md, sizeof(md));
838 error = g_metadata_store(prov, sector, sizeof(sector));
839 bzero(sector, sizeof(sector));
841 gctl_error(req, "Cannot store metadata on %s: %s.", prov,
846 printf("Metadata value stored on %s.\n", prov);
847 /* Backup metadata to a file. */
848 str = gctl_get_ascii(req, "backupfile");
849 if (str[0] != '\0') {
850 /* Backupfile given be the user, just copy it. */
851 strlcpy(backfile, str, sizeof(backfile));
853 /* Generate file name automatically. */
854 const char *p = prov;
857 if (strncmp(p, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
858 p += sizeof(_PATH_DEV) - 1;
859 snprintf(backfile, sizeof(backfile), "%s%s.eli",
861 /* Replace all / with _. */
862 for (i = strlen(GELI_BACKUP_DIR); backfile[i] != '\0'; i++) {
863 if (backfile[i] == '/')
867 if (strcmp(backfile, "none") != 0 &&
868 eli_backup_create(req, prov, backfile) == 0) {
869 printf("\nMetadata backup can be found in %s and\n", backfile);
870 printf("can be restored with the following command:\n");
871 printf("\n\t# geli restore %s %s\n\n", backfile, prov);
876 eli_attach(struct gctl_req *req)
878 struct g_eli_metadata md;
879 unsigned char key[G_ELI_USERKEYLEN];
884 nargs = gctl_get_int(req, "nargs");
886 gctl_error(req, "Invalid number of arguments.");
889 prov = gctl_get_ascii(req, "arg0");
891 if (eli_metadata_read(req, prov, &md) == -1)
894 mediasize = g_get_mediasize(prov);
895 if (md.md_provsize != (uint64_t)mediasize) {
896 gctl_error(req, "Provider size mismatch.");
900 if (eli_genkey(req, &md, key, false) == NULL) {
901 bzero(key, sizeof(key));
905 gctl_ro_param(req, "key", sizeof(key), key);
906 if (gctl_issue(req) == NULL) {
908 printf("Attached to %s.\n", prov);
910 bzero(key, sizeof(key));
914 eli_configure_detached(struct gctl_req *req, const char *prov, int boot,
915 int geliboot, int trim)
917 struct g_eli_metadata md;
920 if (eli_metadata_read(req, prov, &md) == -1)
923 if (boot == 1 && (md.md_flags & G_ELI_FLAG_BOOT)) {
925 printf("BOOT flag already configured for %s.\n", prov);
926 } else if (boot == 0 && !(md.md_flags & G_ELI_FLAG_BOOT)) {
928 printf("BOOT flag not configured for %s.\n", prov);
929 } else if (boot >= 0) {
931 md.md_flags |= G_ELI_FLAG_BOOT;
933 md.md_flags &= ~G_ELI_FLAG_BOOT;
937 if (geliboot == 1 && (md.md_flags & G_ELI_FLAG_GELIBOOT)) {
939 printf("GELIBOOT flag already configured for %s.\n", prov);
940 } else if (geliboot == 0 && !(md.md_flags & G_ELI_FLAG_GELIBOOT)) {
942 printf("GELIBOOT flag not configured for %s.\n", prov);
943 } else if (geliboot >= 0) {
945 md.md_flags |= G_ELI_FLAG_GELIBOOT;
947 md.md_flags &= ~G_ELI_FLAG_GELIBOOT;
951 if (trim == 0 && (md.md_flags & G_ELI_FLAG_NODELETE)) {
953 printf("TRIM disable flag already configured for %s.\n", prov);
954 } else if (trim == 1 && !(md.md_flags & G_ELI_FLAG_NODELETE)) {
956 printf("TRIM disable flag not configured for %s.\n", prov);
957 } else if (trim >= 0) {
959 md.md_flags &= ~G_ELI_FLAG_NODELETE;
961 md.md_flags |= G_ELI_FLAG_NODELETE;
966 eli_metadata_store(req, prov, &md);
967 bzero(&md, sizeof(md));
971 eli_configure(struct gctl_req *req)
974 bool boot, noboot, geliboot, nogeliboot, trim, notrim;
975 int doboot, dogeliboot, dotrim;
978 nargs = gctl_get_int(req, "nargs");
980 gctl_error(req, "Too few arguments.");
984 boot = gctl_get_int(req, "boot");
985 noboot = gctl_get_int(req, "noboot");
986 geliboot = gctl_get_int(req, "geliboot");
987 nogeliboot = gctl_get_int(req, "nogeliboot");
988 trim = gctl_get_int(req, "trim");
989 notrim = gctl_get_int(req, "notrim");
992 if (boot && noboot) {
993 gctl_error(req, "Options -b and -B are mutually exclusive.");
1002 if (geliboot && nogeliboot) {
1003 gctl_error(req, "Options -g and -G are mutually exclusive.");
1008 else if (nogeliboot)
1012 if (trim && notrim) {
1013 gctl_error(req, "Options -t and -T are mutually exclusive.");
1021 if (doboot == -1 && dogeliboot == -1 && dotrim == -1) {
1022 gctl_error(req, "No option given.");
1026 /* First attached providers. */
1029 for (i = 0; i < nargs; i++) {
1030 prov = gctl_get_ascii(req, "arg%d", i);
1031 if (!eli_is_attached(prov))
1032 eli_configure_detached(req, prov, doboot, dogeliboot, dotrim);
1037 eli_setkey_attached(struct gctl_req *req, struct g_eli_metadata *md)
1039 unsigned char key[G_ELI_USERKEYLEN];
1040 intmax_t val, old = 0;
1043 val = gctl_get_intmax(req, "iterations");
1044 /* Check if iterations number should be changed. */
1046 md->md_iterations = val;
1048 old = md->md_iterations;
1050 /* Generate key for Master Key encryption. */
1051 if (eli_genkey(req, md, key, true) == NULL) {
1052 bzero(key, sizeof(key));
1056 * If number of iterations has changed, but wasn't given as a
1057 * command-line argument, update the request.
1059 if (val == -1 && md->md_iterations != old) {
1060 error = gctl_change_param(req, "iterations", sizeof(intmax_t),
1061 &md->md_iterations);
1065 gctl_ro_param(req, "key", sizeof(key), key);
1067 bzero(key, sizeof(key));
1071 eli_setkey_detached(struct gctl_req *req, const char *prov,
1072 struct g_eli_metadata *md)
1074 unsigned char key[G_ELI_USERKEYLEN], mkey[G_ELI_DATAIVKEYLEN];
1075 unsigned char *mkeydst;
1080 if (md->md_keys == 0) {
1081 gctl_error(req, "No valid keys on %s.", prov);
1085 /* Generate key for Master Key decryption. */
1086 if (eli_genkey(req, md, key, false) == NULL) {
1087 bzero(key, sizeof(key));
1091 /* Decrypt Master Key. */
1092 error = g_eli_mkey_decrypt(md, key, mkey, &nkey);
1093 bzero(key, sizeof(key));
1095 bzero(md, sizeof(*md));
1097 gctl_error(req, "Wrong key for %s.", prov);
1098 else /* if (error > 0) */ {
1099 gctl_error(req, "Cannot decrypt Master Key: %s.",
1105 printf("Decrypted Master Key %u.\n", nkey);
1107 val = gctl_get_intmax(req, "keyno");
1112 ; /* Use the key number which was found during decryption. */
1114 if (nkey >= G_ELI_MAXMKEYS) {
1115 gctl_error(req, "Invalid '%s' argument.", "keyno");
1119 val = gctl_get_intmax(req, "iterations");
1120 /* Check if iterations number should and can be changed. */
1122 if (bitcount32(md->md_keys) != 1) {
1123 gctl_error(req, "To be able to use '-i' option, only "
1124 "one key can be defined.");
1127 if (md->md_keys != (1 << nkey)) {
1128 gctl_error(req, "Only already defined key can be "
1129 "changed when '-i' option is used.");
1132 md->md_iterations = val;
1135 mkeydst = md->md_mkeys + nkey * G_ELI_MKEYLEN;
1136 md->md_keys |= (1 << nkey);
1138 bcopy(mkey, mkeydst, sizeof(mkey));
1139 bzero(mkey, sizeof(mkey));
1141 /* Generate key for Master Key encryption. */
1142 if (eli_genkey(req, md, key, true) == NULL) {
1143 bzero(key, sizeof(key));
1144 bzero(md, sizeof(*md));
1148 /* Encrypt the Master-Key with the new key. */
1149 error = g_eli_mkey_encrypt(md->md_ealgo, key, md->md_keylen, mkeydst);
1150 bzero(key, sizeof(key));
1152 bzero(md, sizeof(*md));
1153 gctl_error(req, "Cannot encrypt Master Key: %s.",
1158 /* Store metadata with fresh key. */
1159 eli_metadata_store(req, prov, md);
1160 bzero(md, sizeof(*md));
1164 eli_setkey(struct gctl_req *req)
1166 struct g_eli_metadata md;
1170 nargs = gctl_get_int(req, "nargs");
1172 gctl_error(req, "Invalid number of arguments.");
1175 prov = gctl_get_ascii(req, "arg0");
1177 if (eli_metadata_read(req, prov, &md) == -1)
1180 if (eli_is_attached(prov))
1181 eli_setkey_attached(req, &md);
1183 eli_setkey_detached(req, prov, &md);
1185 if (req->error == NULL || req->error[0] == '\0') {
1186 printf("Note, that the master key encrypted with old keys "
1187 "and/or passphrase may still exists in a metadata backup "
1193 eli_delkey_attached(struct gctl_req *req, const char *prov __unused)
1200 eli_delkey_detached(struct gctl_req *req, const char *prov)
1202 struct g_eli_metadata md;
1203 unsigned char *mkeydst;
1208 if (eli_metadata_read(req, prov, &md) == -1)
1211 all = gctl_get_int(req, "all");
1213 arc4random_buf(md.md_mkeys, sizeof(md.md_mkeys));
1215 force = gctl_get_int(req, "force");
1216 val = gctl_get_intmax(req, "keyno");
1218 gctl_error(req, "Key number has to be specified.");
1222 if (nkey >= G_ELI_MAXMKEYS) {
1223 gctl_error(req, "Invalid '%s' argument.", "keyno");
1226 if (!(md.md_keys & (1 << nkey)) && !force) {
1227 gctl_error(req, "Master Key %u is not set.", nkey);
1230 md.md_keys &= ~(1 << nkey);
1231 if (md.md_keys == 0 && !force) {
1232 gctl_error(req, "This is the last Master Key. Use '-f' "
1233 "option if you really want to remove it.");
1236 mkeydst = md.md_mkeys + nkey * G_ELI_MKEYLEN;
1237 arc4random_buf(mkeydst, G_ELI_MKEYLEN);
1240 eli_metadata_store(req, prov, &md);
1241 bzero(&md, sizeof(md));
1245 eli_delkey(struct gctl_req *req)
1250 nargs = gctl_get_int(req, "nargs");
1252 gctl_error(req, "Invalid number of arguments.");
1255 prov = gctl_get_ascii(req, "arg0");
1257 if (eli_is_attached(prov))
1258 eli_delkey_attached(req, prov);
1260 eli_delkey_detached(req, prov);
1264 eli_resume(struct gctl_req *req)
1266 struct g_eli_metadata md;
1267 unsigned char key[G_ELI_USERKEYLEN];
1272 nargs = gctl_get_int(req, "nargs");
1274 gctl_error(req, "Invalid number of arguments.");
1277 prov = gctl_get_ascii(req, "arg0");
1279 if (eli_metadata_read(req, prov, &md) == -1)
1282 mediasize = g_get_mediasize(prov);
1283 if (md.md_provsize != (uint64_t)mediasize) {
1284 gctl_error(req, "Provider size mismatch.");
1288 if (eli_genkey(req, &md, key, false) == NULL) {
1289 bzero(key, sizeof(key));
1293 gctl_ro_param(req, "key", sizeof(key), key);
1294 if (gctl_issue(req) == NULL) {
1296 printf("Resumed %s.\n", prov);
1298 bzero(key, sizeof(key));
1302 eli_trash_metadata(struct gctl_req *req, const char *prov, int fd, off_t offset)
1304 unsigned int overwrites;
1305 unsigned char *sector;
1309 size = sizeof(overwrites);
1310 if (sysctlbyname("kern.geom.eli.overwrites", &overwrites, &size,
1311 NULL, 0) == -1 || overwrites == 0) {
1312 overwrites = G_ELI_OVERWRITES;
1315 size = g_sectorsize(fd);
1317 gctl_error(req, "Cannot obtain provider sector size %s: %s.",
1318 prov, strerror(errno));
1321 sector = malloc(size);
1322 if (sector == NULL) {
1323 gctl_error(req, "Cannot allocate %zd bytes of memory.", size);
1329 arc4random_buf(sector, size);
1330 if (pwrite(fd, sector, size, offset) != size) {
1335 } while (--overwrites > 0);
1338 gctl_error(req, "Cannot trash metadata on provider %s: %s.",
1339 prov, strerror(error));
1346 eli_kill_detached(struct gctl_req *req, const char *prov)
1352 * NOTE: Maybe we should verify if this is geli provider first,
1353 * but 'kill' command is quite critical so better don't waste
1357 error = g_metadata_read(prov, (unsigned char *)&md, sizeof(md),
1360 gctl_error(req, "Cannot read metadata from %s: %s.", prov,
1366 fd = g_open(prov, 1);
1368 gctl_error(req, "Cannot open provider %s: %s.", prov,
1372 offset = g_mediasize(fd) - g_sectorsize(fd);
1375 "Cannot obtain media size or sector size for provider %s: %s.",
1376 prov, strerror(errno));
1380 (void)eli_trash_metadata(req, prov, fd, offset);
1385 eli_kill(struct gctl_req *req)
1390 nargs = gctl_get_int(req, "nargs");
1391 all = gctl_get_int(req, "all");
1392 if (!all && nargs == 0) {
1393 gctl_error(req, "Too few arguments.");
1397 * How '-a' option combine with a list of providers:
1398 * Delete Master Keys from all attached providers:
1400 * Delete Master Keys from all attached providers and from
1401 * detached da0 and da1:
1402 * geli kill -a da0 da1
1403 * Delete Master Keys from (attached or detached) da0 and da1:
1407 /* First detached providers. */
1408 for (i = 0; i < nargs; i++) {
1409 prov = gctl_get_ascii(req, "arg%d", i);
1410 if (!eli_is_attached(prov))
1411 eli_kill_detached(req, prov);
1413 /* Now attached providers. */
1418 eli_backup_create(struct gctl_req *req, const char *prov, const char *file)
1420 unsigned char *sector;
1422 int error, filefd, ret;
1429 secsize = g_get_sectorsize(prov);
1431 gctl_error(req, "Cannot get informations about %s: %s.", prov,
1435 sector = malloc(secsize);
1436 if (sector == NULL) {
1437 gctl_error(req, "Cannot allocate memory.");
1440 /* Read metadata from the provider. */
1441 error = g_metadata_read(prov, sector, secsize, G_ELI_MAGIC);
1443 gctl_error(req, "Unable to read metadata from %s: %s.", prov,
1448 filefd = open(file, O_WRONLY | O_TRUNC | O_CREAT, 0600);
1450 gctl_error(req, "Unable to open %s: %s.", file,
1454 /* Write metadata to the destination file. */
1455 if (write(filefd, sector, secsize) != secsize) {
1456 gctl_error(req, "Unable to write to %s: %s.", file,
1458 (void)close(filefd);
1462 (void)fsync(filefd);
1463 (void)close(filefd);
1467 if (sector != NULL) {
1468 bzero(sector, secsize);
1475 eli_backup(struct gctl_req *req)
1477 const char *file, *prov;
1480 nargs = gctl_get_int(req, "nargs");
1482 gctl_error(req, "Invalid number of arguments.");
1485 prov = gctl_get_ascii(req, "arg0");
1486 file = gctl_get_ascii(req, "arg1");
1488 eli_backup_create(req, prov, file);
1492 eli_restore(struct gctl_req *req)
1494 struct g_eli_metadata md;
1495 const char *file, *prov;
1499 nargs = gctl_get_int(req, "nargs");
1501 gctl_error(req, "Invalid number of arguments.");
1504 file = gctl_get_ascii(req, "arg0");
1505 prov = gctl_get_ascii(req, "arg1");
1507 /* Read metadata from the backup file. */
1508 if (eli_metadata_read(req, file, &md) == -1)
1510 /* Obtain provider's mediasize. */
1511 mediasize = g_get_mediasize(prov);
1512 if (mediasize == 0) {
1513 gctl_error(req, "Cannot get informations about %s: %s.", prov,
1517 /* Check if the provider size has changed since we did the backup. */
1518 if (md.md_provsize != (uint64_t)mediasize) {
1519 if (gctl_get_int(req, "force")) {
1520 md.md_provsize = mediasize;
1522 gctl_error(req, "Provider size mismatch: "
1523 "wrong backup file?");
1527 /* Write metadata to the provider. */
1528 (void)eli_metadata_store(req, prov, &md);
1532 eli_resize(struct gctl_req *req)
1534 struct g_eli_metadata md;
1536 unsigned char *sector;
1538 off_t mediasize, oldsize;
1539 int error, nargs, provfd;
1541 nargs = gctl_get_int(req, "nargs");
1543 gctl_error(req, "Invalid number of arguments.");
1546 prov = gctl_get_ascii(req, "arg0");
1552 provfd = g_open(prov, 1);
1554 gctl_error(req, "Cannot open %s: %s.", prov, strerror(errno));
1558 mediasize = g_mediasize(provfd);
1559 secsize = g_sectorsize(provfd);
1560 if (mediasize == -1 || secsize == -1) {
1561 gctl_error(req, "Cannot get information about %s: %s.", prov,
1566 sector = malloc(secsize);
1567 if (sector == NULL) {
1568 gctl_error(req, "Cannot allocate memory.");
1572 oldsize = gctl_get_intmax(req, "oldsize");
1573 if (oldsize < 0 || oldsize > mediasize) {
1574 gctl_error(req, "Invalid oldsize: Out of range.");
1577 if (oldsize == mediasize) {
1578 gctl_error(req, "Size hasn't changed.");
1582 /* Read metadata from the 'oldsize' offset. */
1583 if (pread(provfd, sector, secsize, oldsize - secsize) != secsize) {
1584 gctl_error(req, "Cannot read old metadata: %s.",
1589 /* Check if this sector contains geli metadata. */
1590 error = eli_metadata_decode(sector, &md);
1596 "Provider's %s metadata version %u is too new.\n"
1597 "geli: The highest supported version is %u.",
1598 prov, (unsigned int)md.md_version, G_ELI_VERSION);
1601 gctl_error(req, "Inconsistent provider's %s metadata.", prov);
1605 "Unexpected error while decoding provider's %s metadata: %s.",
1606 prov, strerror(error));
1611 * If the old metadata doesn't have a correct provider size, refuse
1614 if (md.md_provsize != (uint64_t)oldsize) {
1615 gctl_error(req, "Provider size mismatch at oldsize.");
1620 * Update the old metadata with the current provider size and write
1621 * it back to the correct place on the provider.
1623 md.md_provsize = mediasize;
1624 /* Write metadata to the provider. */
1625 (void)eli_metadata_store(req, prov, &md);
1626 /* Now trash the old metadata. */
1627 (void)eli_trash_metadata(req, prov, provfd, oldsize - secsize);
1630 (void)g_close(provfd);
1631 if (sector != NULL) {
1632 bzero(sector, secsize);
1638 eli_version(struct gctl_req *req)
1640 struct g_eli_metadata md;
1642 unsigned int version;
1643 int error, i, nargs;
1645 nargs = gctl_get_int(req, "nargs");
1648 unsigned int kernver;
1651 size = sizeof(kernver);
1652 if (sysctlbyname("kern.geom.eli.version", &kernver, &size,
1654 warn("Unable to obtain GELI kernel version");
1656 printf("kernel: %u\n", kernver);
1658 printf("userland: %u\n", G_ELI_VERSION);
1662 for (i = 0; i < nargs; i++) {
1663 name = gctl_get_ascii(req, "arg%d", i);
1664 error = g_metadata_read(name, (unsigned char *)&md,
1665 sizeof(md), G_ELI_MAGIC);
1667 warn("%s: Unable to read metadata: %s.", name,
1669 gctl_error(req, "Not fully done.");
1672 version = le32dec(&md.md_version);
1673 printf("%s: %u\n", name, version);
1678 eli_clear(struct gctl_req *req)
1681 int error, i, nargs;
1683 nargs = gctl_get_int(req, "nargs");
1685 gctl_error(req, "Too few arguments.");
1689 for (i = 0; i < nargs; i++) {
1690 name = gctl_get_ascii(req, "arg%d", i);
1691 error = g_metadata_clear(name, G_ELI_MAGIC);
1693 fprintf(stderr, "Cannot clear metadata on %s: %s.\n",
1694 name, strerror(error));
1695 gctl_error(req, "Not fully done.");
1699 printf("Metadata cleared on %s.\n", name);
1704 eli_dump(struct gctl_req *req)
1706 struct g_eli_metadata md;
1710 nargs = gctl_get_int(req, "nargs");
1712 gctl_error(req, "Too few arguments.");
1716 for (i = 0; i < nargs; i++) {
1717 name = gctl_get_ascii(req, "arg%d", i);
1718 if (eli_metadata_read(NULL, name, &md) == -1) {
1719 gctl_error(req, "Not fully done.");
1722 printf("Metadata on %s:\n", name);
1723 eli_metadata_dump(&md);