2 * Copyright (c) 2002 Poul-Henning Kamp
3 * Copyright (c) 2002 Networks Associates Technology, Inc.
6 * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7 * and NAI Labs, the Security Research Division of Network Associates, Inc.
8 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9 * DARPA CHATS research program.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * Replace the template file options (-i & -f) with command-line variables
39 * Introduce -e, extra entropy source (XOR with /dev/random)
41 * Introduce -E, alternate entropy source (instead of /dev/random)
43 * Introduce -i take IV from keyboard or
45 * Introduce -I take IV from file/cmd
47 * Introduce -m/-M store encrypted+encoded masterkey in file
49 * Introduce -k/-K get pass-phrase part from file/cmd
51 * Introduce -d add more dest-devices to worklist.
53 * Add key-option: selfdestruct bit.
56 * "onetime" attach with onetime nonstored locksector
57 * "key"/"unkey" to blast memory copy of key without orphaning
58 * "nuke" blow away everything attached, crash/halt/power-off if possible.
59 * "blast" destroy all copies of the masterkey
60 * "destroy" destroy one copy of the masterkey
61 * "backup"/"restore" of masterkey sectors.
63 * Make all verbs work on both attached/detached devices.
67 #include <sys/types.h>
68 #include <sys/queue.h>
69 #include <sys/mutex.h>
71 #include <readpassphrase.h>
83 #include <sys/errno.h>
86 #include <crypto/rijndael/rijndael-api-fst.h>
87 #include <crypto/sha2/sha2.h>
88 #include <sys/param.h>
89 #include <sys/linker.h>
91 #define GBDEMOD "geom_bde"
92 #define KASSERT(foo, bar) do { if(!(foo)) { warn bar ; exit (1); } } while (0)
94 #include <geom/geom.h>
95 #include <geom/bde/g_bde.h>
97 extern const char template[];
102 g_hexdump(void *ptr, int length)
108 for (i = 0; i < length; i+= 16) {
110 for (j = 0; j < 16; j++) {
113 printf(" %02x", cp[k]);
118 for (j = 0; j < 16; j++) {
122 else if (cp[k] >= ' ' && cp[k] <= '~')
136 (void)fprintf(stderr,
137 "usage: gbde attach destination [-k keyfile] [-l lockfile] [-p pass-phrase]\n"
138 " gbde detach destination\n"
139 " gbde init destination [-i] [-f filename] [-K new-keyfile]\n"
140 " [-L new-lockfile] [-P new-pass-phrase]\n"
141 " gbde setkey destination [-n key]\n"
142 " [-k keyfile] [-l lockfile] [-p pass-phrase]\n"
143 " [-K new-keyfile] [-L new-lockfile] [-P new-pass-phrase]\n"
144 " gbde nuke destination [-n key]\n"
145 " [-k keyfile] [-l lockfile] [-p pass-phrase]\n"
146 " gbde destroy destination [-k keyfile] [-l lockfile] [-p pass-phrase]\n");
151 g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error)
161 o2 = lseek(fd, offset, SEEK_SET);
164 i = read(fd, p, length);
173 random_bits(void *p, u_int len)
179 fdr = open("/dev/urandom", O_RDONLY);
181 err(1, "/dev/urandom");
184 i = read(fdr, p, len);
186 err(1, "read from /dev/urandom");
190 static u_char sha2[SHA512_DIGEST_LENGTH];
193 reset_passphrase(struct g_bde_softc *sc)
196 memcpy(sc->sha2, sha2, SHA512_DIGEST_LENGTH);
200 setup_passphrase(struct g_bde_softc *sc, int sure, const char *input,
203 char buf1[BUFSIZ + SHA512_DIGEST_LENGTH];
204 char buf2[BUFSIZ + SHA512_DIGEST_LENGTH];
206 int kfd, klen, bpos = 0;
208 if (keyfile != NULL) {
209 /* Read up to BUFSIZ bytes from keyfile */
210 kfd = open(keyfile, O_RDONLY, 0);
212 err(1, "%s", keyfile);
213 klen = read(kfd, buf1, BUFSIZ);
215 err(1, "%s", keyfile);
218 /* Prepend the passphrase with the hash of the key read */
219 g_bde_hash_pass(sc, buf1, klen);
220 memcpy(buf1, sc->sha2, SHA512_DIGEST_LENGTH);
221 memcpy(buf2, sc->sha2, SHA512_DIGEST_LENGTH);
222 bpos = SHA512_DIGEST_LENGTH;
226 if (strlen(input) >= BUFSIZ)
227 errx(1, "Passphrase too long");
228 strcpy(buf1 + bpos, input);
230 g_bde_hash_pass(sc, buf1, strlen(buf1 + bpos) + bpos);
231 memcpy(sha2, sc->sha2, SHA512_DIGEST_LENGTH);
236 sure ? "Enter new passphrase:" : "Enter passphrase: ",
237 buf1 + bpos, sizeof buf1 - bpos,
238 RPP_ECHO_OFF | RPP_REQUIRE_TTY);
240 err(1, "readpassphrase");
243 p = readpassphrase("Reenter new passphrase: ",
244 buf2 + bpos, sizeof buf2 - bpos,
245 RPP_ECHO_OFF | RPP_REQUIRE_TTY);
247 err(1, "readpassphrase");
249 if (strcmp(buf1 + bpos, buf2 + bpos)) {
250 printf("They didn't match.\n");
254 if (strlen(buf1 + bpos) < 3) {
255 printf("Too short passphrase.\n");
260 g_bde_hash_pass(sc, buf1, strlen(buf1 + bpos) + bpos);
261 memcpy(sha2, sc->sha2, SHA512_DIGEST_LENGTH);
265 encrypt_sector(void *d, int len, int klen, void *key)
271 error = rijndael_cipherInit(&ci, MODE_CBC, NULL);
273 errx(1, "rijndael_cipherInit=%d", error);
274 error = rijndael_makeKey(&ki, DIR_ENCRYPT, klen, key);
276 errx(1, "rijndael_makeKeY=%d", error);
277 error = rijndael_blockEncrypt(&ci, &ki, d, len * 8, d);
279 errx(1, "rijndael_blockEncrypt=%d", error);
283 cmd_attach(const struct g_bde_softc *sc, const char *dest, const char *lfile)
290 r = gctl_get_handle();
291 gctl_ro_param(r, "verb", -1, "create geom");
292 gctl_ro_param(r, "class", -1, "BDE");
293 gctl_ro_param(r, "provider", -1, dest);
294 gctl_ro_param(r, "pass", SHA512_DIGEST_LENGTH, sc->sha2);
296 ffd = open(lfile, O_RDONLY, 0);
300 gctl_ro_param(r, "key", 16, buf);
303 /* gctl_dump(r, stdout); */
304 errstr = gctl_issue(r);
306 errx(1, "Attach to %s failed: %s", dest, errstr);
312 cmd_detach(const char *dest)
318 r = gctl_get_handle();
319 gctl_ro_param(r, "verb", -1, "destroy geom");
320 gctl_ro_param(r, "class", -1, "BDE");
321 sprintf(buf, "%s.bde", dest);
322 gctl_ro_param(r, "geom", -1, buf);
323 /* gctl_dump(r, stdout); */
324 errstr = gctl_issue(r);
326 errx(1, "Detach of %s failed: %s", dest, errstr);
331 cmd_open(struct g_bde_softc *sc, int dfd , const char *l_opt, u_int *nkey)
340 error = ioctl(dfd, DIOCGSECTORSIZE, §orsize);
343 error = ioctl(dfd, DIOCGMEDIASIZE, &mediasize);
345 error = fstat(dfd, &st);
346 if (error == 0 && S_ISREG(st.st_mode))
347 mediasize = st.st_size;
352 mediasize = (off_t)-1;
354 ffd = open(l_opt, O_RDONLY, 0);
357 read(ffd, keyloc, sizeof keyloc);
360 memset(keyloc, 0, sizeof keyloc);
363 error = g_bde_decrypt_lock(sc, sc->sha2, keyloc, mediasize,
366 errx(1, "Lock was destroyed.");
368 errx(1, "Lock was nuked.");
369 if (error == ENOTDIR)
370 errx(1, "Lock not found");
372 errx(1, "Error %d decrypting lock", error);
374 printf("Opened with key %u\n", *nkey);
379 cmd_nuke(struct g_bde_key *gl, int dfd , int key)
383 off_t offset, offset2;
385 sbuf = malloc(gl->sectorsize);
386 memset(sbuf, 0, gl->sectorsize);
387 offset = (gl->lsector[key] & ~(gl->sectorsize - 1));
388 offset2 = lseek(dfd, offset, SEEK_SET);
389 if (offset2 != offset)
391 i = write(dfd, sbuf, gl->sectorsize);
393 if (i != (int)gl->sectorsize)
395 printf("Nuked key %d\n", key);
399 cmd_write(struct g_bde_key *gl, struct g_bde_softc *sc, int dfd , int key, const char *l_opt)
405 off_t offset, offset2;
407 sbuf = malloc(gl->sectorsize);
409 * Find the byte-offset in the lock sector where we will put the lock
410 * data structure. We can put it any random place as long as the
414 random_bits(off, sizeof off);
415 off[0] &= (gl->sectorsize - 1);
416 if (off[0] + G_BDE_LOCKSIZE > gl->sectorsize)
421 /* Add the sector offset in bytes */
422 off[0] += (gl->lsector[key] & ~(gl->sectorsize - 1));
423 gl->lsector[key] = off[0];
425 i = g_bde_keyloc_encrypt(sc->sha2, off[0], off[1], keyloc);
427 errx(1, "g_bde_keyloc_encrypt()");
429 ffd = open(l_opt, O_WRONLY | O_CREAT | O_TRUNC, 0600);
432 write(ffd, keyloc, sizeof keyloc);
434 } else if (gl->flags & GBDE_F_SECT0) {
435 offset2 = lseek(dfd, 0, SEEK_SET);
438 i = read(dfd, sbuf, gl->sectorsize);
439 if (i != (int)gl->sectorsize)
441 memcpy(sbuf + key * 16, keyloc, sizeof keyloc);
442 offset2 = lseek(dfd, 0, SEEK_SET);
445 i = write(dfd, sbuf, gl->sectorsize);
446 if (i != (int)gl->sectorsize)
449 errx(1, "No -L option and no space in sector 0 for lockfile");
452 /* Allocate a sectorbuffer and fill it with random junk */
455 random_bits(sbuf, gl->sectorsize);
457 /* Fill random bits in the spare field */
458 random_bits(gl->spare, sizeof(gl->spare));
460 /* Encode the structure where we want it */
461 q = sbuf + (off[0] % gl->sectorsize);
462 i = g_bde_encode_lock(sc->sha2, gl, q);
464 errx(1, "programming error encoding lock");
466 encrypt_sector(q, G_BDE_LOCKSIZE, 256, sc->sha2 + 16);
467 offset = gl->lsector[key] & ~(gl->sectorsize - 1);
468 offset2 = lseek(dfd, offset, SEEK_SET);
469 if (offset2 != offset)
471 i = write(dfd, sbuf, gl->sectorsize);
472 if (i != (int)gl->sectorsize)
476 printf("Wrote key %d at %jd\n", key, (intmax_t)offset);
477 printf("s0 = %jd\n", (intmax_t)gl->sector0);
478 printf("sN = %jd\n", (intmax_t)gl->sectorN);
479 printf("l[0] = %jd\n", (intmax_t)gl->lsector[0]);
480 printf("l[1] = %jd\n", (intmax_t)gl->lsector[1]);
481 printf("l[2] = %jd\n", (intmax_t)gl->lsector[2]);
482 printf("l[3] = %jd\n", (intmax_t)gl->lsector[3]);
483 printf("k = %jd\n", (intmax_t)gl->keyoffset);
484 printf("ss = %jd\n", (intmax_t)gl->sectorsize);
489 cmd_destroy(struct g_bde_key *gl, int nkey)
493 bzero(&gl->sector0, sizeof gl->sector0);
494 bzero(&gl->sectorN, sizeof gl->sectorN);
495 bzero(&gl->keyoffset, sizeof gl->keyoffset);
496 bzero(&gl->flags, sizeof gl->flags);
497 bzero(gl->mkey, sizeof gl->mkey);
498 for (i = 0; i < G_BDE_MAXKEYS; i++)
504 sorthelp(const void *a, const void *b)
506 const uint64_t *oa, *ob;
518 cmd_init(struct g_bde_key *gl, int dfd, const char *f_opt, int i_opt, const char *l_opt)
522 unsigned sector_size;
523 uint64_t first_sector;
524 uint64_t last_sector;
525 uint64_t total_sectors;
529 char *q, cbuf[BUFSIZ];
534 bzero(gl, sizeof *gl);
536 i = open(f_opt, O_RDONLY);
539 params = properties_read(i);
543 asprintf(&q, "%stemp.XXXXXXXXXX", _PATH_TMP);
549 write(i, template, strlen(template));
551 p = getenv("EDITOR");
554 if (snprintf(cbuf, sizeof(cbuf), "%s %s\n", p, q) >=
555 (ssize_t)sizeof(cbuf)) {
557 errx(1, "EDITOR is too long");
560 i = open(q, O_RDONLY);
563 params = properties_read(i);
569 i = open(_PATH_DEVNULL, O_RDONLY);
571 err(1, "%s", _PATH_DEVNULL);
572 params = properties_read(i);
577 p = property_find(params, "sector_size");
578 i = ioctl(dfd, DIOCGSECTORSIZE, &u);
580 sector_size = strtoul(p, &q, 0);
582 errx(1, "sector_size not a proper number");
586 errx(1, "Missing sector_size property");
588 if (sector_size & (sector_size - 1))
589 errx(1, "sector_size not a power of 2");
590 if (sector_size < 512)
591 errx(1, "sector_size is smaller than 512");
592 buf = malloc(sector_size);
594 err(1, "Failed to malloc sector buffer");
595 gl->sectorsize = sector_size;
597 i = ioctl(dfd, DIOCGMEDIASIZE, &off);
600 total_sectors = off / sector_size;
601 last_sector = total_sectors - 1;
609 p = property_find(params, "first_sector");
611 first_sector = strtoul(p, &q, 0);
613 errx(1, "first_sector not a proper number");
617 p = property_find(params, "last_sector");
619 last_sector = strtoul(p, &q, 0);
621 errx(1, "last_sector not a proper number");
622 if (last_sector <= first_sector)
623 errx(1, "last_sector not larger than first_sector");
624 total_sectors = last_sector + 1;
627 /* <total_sectors> */
628 p = property_find(params, "total_sectors");
630 total_sectors = strtoul(p, &q, 0);
632 errx(1, "total_sectors not a proper number");
633 if (last_sector == 0)
634 last_sector = first_sector + total_sectors - 1;
637 if (l_opt == NULL && first_sector != 0)
638 errx(1, "No -L new-lockfile argument and first_sector != 0");
639 else if (l_opt == NULL) {
642 gl->flags |= GBDE_F_SECT0;
644 gl->sector0 = first_sector * gl->sectorsize;
646 if (total_sectors != (last_sector - first_sector) + 1)
647 errx(1, "total_sectors disagree with first_sector and last_sector");
648 if (total_sectors == 0)
649 errx(1, "missing last_sector or total_sectors");
651 gl->sectorN = (last_sector + 1) * gl->sectorsize;
653 /* Find a random keyoffset */
654 random_bits(&o, sizeof o);
655 o %= (gl->sectorN - gl->sector0);
656 o &= ~(gl->sectorsize - 1);
659 /* <number_of_keys> */
660 p = property_find(params, "number_of_keys");
662 nkeys = strtoul(p, &q, 0);
664 errx(1, "number_of_keys not a proper number");
665 if (nkeys < 1 || nkeys > G_BDE_MAXKEYS)
666 errx(1, "number_of_keys out of range");
670 for (u = 0; u < nkeys; u++) {
673 random_bits(&o, sizeof o);
675 o &= ~(gl->sectorsize - 1);
676 } while(o < gl->sector0);
677 for (u2 = 0; u2 < u; u2++)
678 if (o == gl->lsector[u2])
686 for (; u < G_BDE_MAXKEYS; u++) {
688 random_bits(&o, sizeof o);
689 while (o < gl->sectorN);
692 qsort(gl->lsector, G_BDE_MAXKEYS, sizeof gl->lsector[0], sorthelp);
694 /* Flush sector zero if we use it for lockfile data */
695 if (gl->flags & GBDE_F_SECT0) {
696 off2 = lseek(dfd, 0, SEEK_SET);
698 err(1, "lseek(2) to sector 0");
699 random_bits(buf, sector_size);
700 i = write(dfd, buf, sector_size);
701 if (i != (int)sector_size)
702 err(1, "write sector 0");
706 p = property_find(params, "random_flush");
708 off = first_sector * sector_size;
709 off2 = lseek(dfd, off, SEEK_SET);
711 err(1, "lseek(2) to first_sector");
712 off2 = last_sector * sector_size;
713 while (off <= off2) {
714 random_bits(buf, sector_size);
715 i = write(dfd, buf, sector_size);
716 if (i != (int)sector_size)
717 err(1, "write to $device_name");
722 random_bits(gl->mkey, sizeof gl->mkey);
723 random_bits(gl->salt, sizeof gl->salt);
730 ACT_ATTACH, ACT_DETACH,
731 ACT_INIT, ACT_SETKEY, ACT_DESTROY, ACT_NUKE
735 main(int argc, char **argv)
738 const char *k_opt, *K_opt;
739 const char *l_opt, *L_opt;
740 const char *p_opt, *P_opt;
743 int i_opt, n_opt, ch, dfd, doopen;
746 char *q, buf[BUFSIZ];
747 struct g_bde_key *gl;
748 struct g_bde_softc sc;
753 if (modfind("g_bde") < 0) {
754 /* need to load the gbde module */
755 if (kldload(GBDEMOD) < 0 || modfind("g_bde") < 0)
756 err(1, GBDEMOD ": Kernel module not available");
759 if (!strcmp(argv[1], "attach")) {
762 } else if (!strcmp(argv[1], "detach")) {
765 } else if (!strcmp(argv[1], "init")) {
769 } else if (!strcmp(argv[1], "setkey")) {
772 opts = "k:K:l:L:n:p:P:";
773 } else if (!strcmp(argv[1], "destroy")) {
774 action = ACT_DESTROY;
777 } else if (!strcmp(argv[1], "nuke")) {
787 dest = strdup(argv[1]);
801 while((ch = getopt(argc, argv, opts)) != -1)
822 n_opt = strtoul(optarg, &q, 0);
824 errx(1, "-n argument not numeric");
825 if (n_opt < -1 || n_opt > G_BDE_MAXKEYS)
826 errx(1, "-n argument out of range");
839 dfd = open(dest, O_RDWR);
840 if (dfd < 0 && dest[0] != '/') {
841 if (snprintf(buf, sizeof(buf), "%s%s",
842 _PATH_DEV, dest) >= (ssize_t)sizeof(buf))
843 errno = ENAMETOOLONG;
845 dfd = open(buf, O_RDWR);
850 if (!memcmp(dest, _PATH_DEV, strlen(_PATH_DEV)))
851 strcpy(dest, dest + strlen(_PATH_DEV));
854 memset(&sc, 0, sizeof sc);
855 sc.consumer = (void *)&dfd;
859 setup_passphrase(&sc, 0, p_opt, k_opt);
860 cmd_attach(&sc, dest, l_opt);
866 cmd_init(gl, dfd, f_opt, i_opt, L_opt);
867 setup_passphrase(&sc, 1, P_opt, K_opt);
868 cmd_write(gl, &sc, dfd, 0, L_opt);
871 setup_passphrase(&sc, 0, p_opt, k_opt);
872 cmd_open(&sc, dfd, l_opt, &nkey);
875 setup_passphrase(&sc, 1, P_opt, K_opt);
876 cmd_write(gl, &sc, dfd, n_opt - 1, L_opt);
879 setup_passphrase(&sc, 0, p_opt, k_opt);
880 cmd_open(&sc, dfd, l_opt, &nkey);
881 cmd_destroy(gl, nkey);
882 reset_passphrase(&sc);
883 cmd_write(gl, &sc, dfd, nkey, l_opt);
886 setup_passphrase(&sc, 0, p_opt, k_opt);
887 cmd_open(&sc, dfd, l_opt, &nkey);
891 for(i = 0; i < G_BDE_MAXKEYS; i++)
892 cmd_nuke(gl, dfd, i);
894 cmd_nuke(gl, dfd, n_opt - 1);
898 errx(1, "internal error");