2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2002 Poul-Henning Kamp
5 * Copyright (c) 2002 Networks Associates Technology, Inc.
8 * This software was developed for the FreeBSD Project by Poul-Henning Kamp
9 * and NAI Labs, the Security Research Division of Network Associates, Inc.
10 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
11 * DARPA CHATS research program.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * Replace the template file options (-i & -f) with command-line variables
41 * Introduce -e, extra entropy source (XOR with /dev/random)
43 * Introduce -E, alternate entropy source (instead of /dev/random)
45 * Introduce -i take IV from keyboard or
47 * Introduce -I take IV from file/cmd
49 * Introduce -m/-M store encrypted+encoded masterkey in file
51 * Introduce -k/-K get pass-phrase part from file/cmd
53 * Introduce -d add more dest-devices to worklist.
55 * Add key-option: selfdestruct bit.
58 * "onetime" attach with onetime nonstored locksector
59 * "key"/"unkey" to blast memory copy of key without orphaning
60 * "nuke" blow away everything attached, crash/halt/power-off if possible.
61 * "blast" destroy all copies of the masterkey
62 * "destroy" destroy one copy of the masterkey
63 * "backup"/"restore" of masterkey sectors.
65 * Make all verbs work on both attached/detached devices.
69 #include <sys/types.h>
70 #include <sys/queue.h>
71 #include <sys/mutex.h>
73 #include <readpassphrase.h>
85 #include <sys/errno.h>
88 #include <crypto/rijndael/rijndael-api-fst.h>
89 #include <crypto/sha2/sha512.h>
90 #include <sys/param.h>
91 #include <sys/linker.h>
93 #define GBDEMOD "geom_bde"
94 #define KASSERT(foo, bar) do { if(!(foo)) { warn bar ; exit (1); } } while (0)
96 #include <geom/geom.h>
97 #include <geom/bde/g_bde.h>
99 extern const char template[];
104 g_hexdump(void *ptr, int length)
110 for (i = 0; i < length; i+= 16) {
112 for (j = 0; j < 16; j++) {
115 printf(" %02x", cp[k]);
120 for (j = 0; j < 16; j++) {
124 else if (cp[k] >= ' ' && cp[k] <= '~')
138 (void)fprintf(stderr,
139 "usage: gbde attach destination [-k keyfile] [-l lockfile] [-p pass-phrase]\n"
140 " gbde detach destination\n"
141 " gbde init destination [-i] [-f filename] [-K new-keyfile]\n"
142 " [-L new-lockfile] [-P new-pass-phrase]\n"
143 " gbde setkey destination [-n key]\n"
144 " [-k keyfile] [-l lockfile] [-p pass-phrase]\n"
145 " [-K new-keyfile] [-L new-lockfile] [-P new-pass-phrase]\n"
146 " gbde nuke destination [-n key]\n"
147 " [-k keyfile] [-l lockfile] [-p pass-phrase]\n"
148 " gbde destroy destination [-k keyfile] [-l lockfile] [-p pass-phrase]\n");
153 g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error)
163 o2 = lseek(fd, offset, SEEK_SET);
166 i = read(fd, p, length);
175 random_bits(void *p, u_int len)
181 fdr = open("/dev/urandom", O_RDONLY);
183 err(1, "/dev/urandom");
186 i = read(fdr, p, len);
188 err(1, "read from /dev/urandom");
192 static u_char sha2[SHA512_DIGEST_LENGTH];
195 reset_passphrase(struct g_bde_softc *sc)
198 memcpy(sc->sha2, sha2, SHA512_DIGEST_LENGTH);
202 setup_passphrase(struct g_bde_softc *sc, int sure, const char *input,
205 char buf1[BUFSIZ + SHA512_DIGEST_LENGTH];
206 char buf2[BUFSIZ + SHA512_DIGEST_LENGTH];
208 int kfd, klen, bpos = 0;
210 if (keyfile != NULL) {
211 /* Read up to BUFSIZ bytes from keyfile */
212 kfd = open(keyfile, O_RDONLY, 0);
214 err(1, "%s", keyfile);
215 klen = read(kfd, buf1, BUFSIZ);
217 err(1, "%s", keyfile);
220 /* Prepend the passphrase with the hash of the key read */
221 g_bde_hash_pass(sc, buf1, klen);
222 memcpy(buf1, sc->sha2, SHA512_DIGEST_LENGTH);
223 memcpy(buf2, sc->sha2, SHA512_DIGEST_LENGTH);
224 bpos = SHA512_DIGEST_LENGTH;
228 if (strlen(input) >= BUFSIZ)
229 errx(1, "Passphrase too long");
230 strcpy(buf1 + bpos, input);
232 g_bde_hash_pass(sc, buf1, strlen(buf1 + bpos) + bpos);
233 memcpy(sha2, sc->sha2, SHA512_DIGEST_LENGTH);
238 sure ? "Enter new passphrase:" : "Enter passphrase: ",
239 buf1 + bpos, sizeof buf1 - bpos,
240 RPP_ECHO_OFF | RPP_REQUIRE_TTY);
242 err(1, "readpassphrase");
245 p = readpassphrase("Reenter new passphrase: ",
246 buf2 + bpos, sizeof buf2 - bpos,
247 RPP_ECHO_OFF | RPP_REQUIRE_TTY);
249 err(1, "readpassphrase");
251 if (strcmp(buf1 + bpos, buf2 + bpos)) {
252 printf("They didn't match.\n");
256 if (strlen(buf1 + bpos) < 3) {
257 printf("Too short passphrase.\n");
262 g_bde_hash_pass(sc, buf1, strlen(buf1 + bpos) + bpos);
263 memcpy(sha2, sc->sha2, SHA512_DIGEST_LENGTH);
267 encrypt_sector(void *d, int len, int klen, void *key)
273 error = rijndael_cipherInit(&ci, MODE_CBC, NULL);
275 errx(1, "rijndael_cipherInit=%d", error);
276 error = rijndael_makeKey(&ki, DIR_ENCRYPT, klen, key);
278 errx(1, "rijndael_makeKeY=%d", error);
279 error = rijndael_blockEncrypt(&ci, &ki, d, len * 8, d);
281 errx(1, "rijndael_blockEncrypt=%d", error);
285 cmd_attach(const struct g_bde_softc *sc, const char *dest, const char *lfile)
292 r = gctl_get_handle();
293 gctl_ro_param(r, "verb", -1, "create geom");
294 gctl_ro_param(r, "class", -1, "BDE");
295 gctl_ro_param(r, "provider", -1, dest);
296 gctl_ro_param(r, "pass", SHA512_DIGEST_LENGTH, sc->sha2);
298 ffd = open(lfile, O_RDONLY, 0);
302 gctl_ro_param(r, "key", 16, buf);
305 errstr = gctl_issue(r);
307 errx(1, "Attach to %s failed: %s", dest, errstr);
313 cmd_detach(const char *dest)
319 r = gctl_get_handle();
320 gctl_ro_param(r, "verb", -1, "destroy geom");
321 gctl_ro_param(r, "class", -1, "BDE");
322 sprintf(buf, "%s.bde", dest);
323 gctl_ro_param(r, "geom", -1, buf);
324 /* gctl_dump(r, stdout); */
325 errstr = gctl_issue(r);
327 errx(1, "Detach of %s failed: %s", dest, errstr);
332 cmd_open(struct g_bde_softc *sc, int dfd , const char *l_opt, u_int *nkey)
341 error = ioctl(dfd, DIOCGSECTORSIZE, §orsize);
344 error = ioctl(dfd, DIOCGMEDIASIZE, &mediasize);
346 error = fstat(dfd, &st);
347 if (error == 0 && S_ISREG(st.st_mode))
348 mediasize = st.st_size;
353 mediasize = (off_t)-1;
355 ffd = open(l_opt, O_RDONLY, 0);
358 read(ffd, keyloc, sizeof keyloc);
361 memset(keyloc, 0, sizeof keyloc);
364 error = g_bde_decrypt_lock(sc, sc->sha2, keyloc, mediasize,
367 errx(1, "Lock was destroyed.");
369 errx(1, "Lock was nuked.");
370 if (error == ENOTDIR)
371 errx(1, "Lock not found");
373 errx(1, "Error %d decrypting lock", error);
375 printf("Opened with key %u\n", 1 + *nkey);
380 cmd_nuke(struct g_bde_key *gl, int dfd , int key)
384 off_t offset, offset2;
386 sbuf = malloc(gl->sectorsize);
387 memset(sbuf, 0, gl->sectorsize);
388 offset = (gl->lsector[key] & ~(gl->sectorsize - 1));
389 offset2 = lseek(dfd, offset, SEEK_SET);
390 if (offset2 != offset)
392 i = write(dfd, sbuf, gl->sectorsize);
394 if (i != (int)gl->sectorsize)
396 printf("Nuked key %d\n", 1 + key);
400 cmd_write(struct g_bde_key *gl, struct g_bde_softc *sc, int dfd , int key, const char *l_opt)
406 off_t offset, offset2;
408 sbuf = malloc(gl->sectorsize);
410 * Find the byte-offset in the lock sector where we will put the lock
411 * data structure. We can put it any random place as long as the
415 random_bits(off, sizeof off);
416 off[0] &= (gl->sectorsize - 1);
417 if (off[0] + G_BDE_LOCKSIZE > gl->sectorsize)
422 /* Add the sector offset in bytes */
423 off[0] += (gl->lsector[key] & ~(gl->sectorsize - 1));
424 gl->lsector[key] = off[0];
426 i = g_bde_keyloc_encrypt(sc->sha2, off[0], off[1], keyloc);
428 errx(1, "g_bde_keyloc_encrypt()");
430 ffd = open(l_opt, O_WRONLY | O_CREAT | O_TRUNC, 0600);
433 write(ffd, keyloc, sizeof keyloc);
435 } else if (gl->flags & GBDE_F_SECT0) {
436 offset2 = lseek(dfd, 0, SEEK_SET);
439 i = read(dfd, sbuf, gl->sectorsize);
440 if (i != (int)gl->sectorsize)
442 memcpy(sbuf + key * 16, keyloc, sizeof keyloc);
443 offset2 = lseek(dfd, 0, SEEK_SET);
446 i = write(dfd, sbuf, gl->sectorsize);
447 if (i != (int)gl->sectorsize)
450 errx(1, "No -L option and no space in sector 0 for lockfile");
453 /* Allocate a sectorbuffer and fill it with random junk */
456 random_bits(sbuf, gl->sectorsize);
458 /* Fill random bits in the spare field */
459 random_bits(gl->spare, sizeof(gl->spare));
461 /* Encode the structure where we want it */
462 q = sbuf + (off[0] % gl->sectorsize);
463 i = g_bde_encode_lock(sc->sha2, gl, q);
465 errx(1, "programming error encoding lock");
467 encrypt_sector(q, G_BDE_LOCKSIZE, 256, sc->sha2 + 16);
468 offset = gl->lsector[key] & ~(gl->sectorsize - 1);
469 offset2 = lseek(dfd, offset, SEEK_SET);
470 if (offset2 != offset)
472 i = write(dfd, sbuf, gl->sectorsize);
473 if (i != (int)gl->sectorsize)
477 printf("Wrote key %d at %jd\n", key, (intmax_t)offset);
478 printf("s0 = %jd\n", (intmax_t)gl->sector0);
479 printf("sN = %jd\n", (intmax_t)gl->sectorN);
480 printf("l[0] = %jd\n", (intmax_t)gl->lsector[0]);
481 printf("l[1] = %jd\n", (intmax_t)gl->lsector[1]);
482 printf("l[2] = %jd\n", (intmax_t)gl->lsector[2]);
483 printf("l[3] = %jd\n", (intmax_t)gl->lsector[3]);
484 printf("k = %jd\n", (intmax_t)gl->keyoffset);
485 printf("ss = %jd\n", (intmax_t)gl->sectorsize);
490 cmd_destroy(struct g_bde_key *gl, int nkey)
494 bzero(&gl->sector0, sizeof gl->sector0);
495 bzero(&gl->sectorN, sizeof gl->sectorN);
496 bzero(&gl->keyoffset, sizeof gl->keyoffset);
497 gl->flags &= GBDE_F_SECT0;
498 bzero(gl->mkey, sizeof gl->mkey);
499 for (i = 0; i < G_BDE_MAXKEYS; i++)
505 sorthelp(const void *a, const void *b)
507 const uint64_t *oa, *ob;
519 cmd_init(struct g_bde_key *gl, int dfd, const char *f_opt, int i_opt, const char *l_opt)
523 unsigned sector_size;
524 uint64_t first_sector;
525 uint64_t last_sector;
526 uint64_t total_sectors;
530 char *q, cbuf[BUFSIZ];
535 bzero(gl, sizeof *gl);
537 i = open(f_opt, O_RDONLY);
540 params = properties_read(i);
544 asprintf(&q, "%stemp.XXXXXXXXXX", _PATH_TMP);
550 write(i, template, strlen(template));
552 p = getenv("EDITOR");
555 if (snprintf(cbuf, sizeof(cbuf), "%s %s\n", p, q) >=
556 (ssize_t)sizeof(cbuf)) {
558 errx(1, "EDITOR is too long");
561 i = open(q, O_RDONLY);
564 params = properties_read(i);
570 i = open(_PATH_DEVNULL, O_RDONLY);
572 err(1, "%s", _PATH_DEVNULL);
573 params = properties_read(i);
578 p = property_find(params, "sector_size");
579 i = ioctl(dfd, DIOCGSECTORSIZE, &u);
581 sector_size = strtoul(p, &q, 0);
583 errx(1, "sector_size not a proper number");
587 errx(1, "Missing sector_size property");
589 if (sector_size & (sector_size - 1))
590 errx(1, "sector_size not a power of 2");
591 if (sector_size < 512)
592 errx(1, "sector_size is smaller than 512");
593 buf = malloc(sector_size);
595 err(1, "Failed to malloc sector buffer");
596 gl->sectorsize = sector_size;
598 i = ioctl(dfd, DIOCGMEDIASIZE, &off);
601 total_sectors = off / sector_size;
602 last_sector = total_sectors - 1;
610 p = property_find(params, "first_sector");
612 first_sector = strtoul(p, &q, 0);
614 errx(1, "first_sector not a proper number");
618 p = property_find(params, "last_sector");
620 last_sector = strtoul(p, &q, 0);
622 errx(1, "last_sector not a proper number");
623 if (last_sector <= first_sector)
624 errx(1, "last_sector not larger than first_sector");
625 total_sectors = last_sector + 1;
628 /* <total_sectors> */
629 p = property_find(params, "total_sectors");
631 total_sectors = strtoul(p, &q, 0);
633 errx(1, "total_sectors not a proper number");
634 if (last_sector == 0)
635 last_sector = first_sector + total_sectors - 1;
638 if (l_opt == NULL && first_sector != 0)
639 errx(1, "No -L new-lockfile argument and first_sector != 0");
640 else if (l_opt == NULL) {
643 gl->flags |= GBDE_F_SECT0;
645 gl->sector0 = first_sector * gl->sectorsize;
647 if (total_sectors != (last_sector - first_sector) + 1)
648 errx(1, "total_sectors disagree with first_sector and last_sector");
649 if (total_sectors == 0)
650 errx(1, "missing last_sector or total_sectors");
652 gl->sectorN = (last_sector + 1) * gl->sectorsize;
654 /* Find a random keyoffset */
655 random_bits(&o, sizeof o);
656 o %= (gl->sectorN - gl->sector0);
657 o &= ~(gl->sectorsize - 1);
660 /* <number_of_keys> */
661 p = property_find(params, "number_of_keys");
663 nkeys = strtoul(p, &q, 0);
665 errx(1, "number_of_keys not a proper number");
666 if (nkeys < 1 || nkeys > G_BDE_MAXKEYS)
667 errx(1, "number_of_keys out of range");
671 for (u = 0; u < nkeys; u++) {
674 random_bits(&o, sizeof o);
676 o &= ~(gl->sectorsize - 1);
677 } while(o < gl->sector0);
678 for (u2 = 0; u2 < u; u2++)
679 if (o == gl->lsector[u2])
687 for (; u < G_BDE_MAXKEYS; u++) {
689 random_bits(&o, sizeof o);
690 while (o < gl->sectorN);
693 qsort(gl->lsector, G_BDE_MAXKEYS, sizeof gl->lsector[0], sorthelp);
695 /* Flush sector zero if we use it for lockfile data */
696 if (gl->flags & GBDE_F_SECT0) {
697 off2 = lseek(dfd, 0, SEEK_SET);
699 err(1, "lseek(2) to sector 0");
700 random_bits(buf, sector_size);
701 i = write(dfd, buf, sector_size);
702 if (i != (int)sector_size)
703 err(1, "write sector 0");
707 p = property_find(params, "random_flush");
709 off = first_sector * sector_size;
710 off2 = lseek(dfd, off, SEEK_SET);
712 err(1, "lseek(2) to first_sector");
713 off2 = last_sector * sector_size;
714 while (off <= off2) {
715 random_bits(buf, sector_size);
716 i = write(dfd, buf, sector_size);
717 if (i != (int)sector_size)
718 err(1, "write to $device_name");
723 random_bits(gl->mkey, sizeof gl->mkey);
724 random_bits(gl->salt, sizeof gl->salt);
731 ACT_ATTACH, ACT_DETACH,
732 ACT_INIT, ACT_SETKEY, ACT_DESTROY, ACT_NUKE
736 main(int argc, char **argv)
739 const char *k_opt, *K_opt;
740 const char *l_opt, *L_opt;
741 const char *p_opt, *P_opt;
744 int i_opt, n_opt, ch, dfd, doopen;
747 char *q, buf[BUFSIZ];
748 struct g_bde_key *gl;
749 struct g_bde_softc sc;
754 if (modfind("g_bde") < 0) {
755 /* need to load the gbde module */
756 if (kldload(GBDEMOD) < 0 || modfind("g_bde") < 0)
757 err(1, GBDEMOD ": Kernel module not available");
760 if (!strcmp(argv[1], "attach")) {
763 } else if (!strcmp(argv[1], "detach")) {
766 } else if (!strcmp(argv[1], "init")) {
770 } else if (!strcmp(argv[1], "setkey")) {
773 opts = "k:K:l:L:n:p:P:";
774 } else if (!strcmp(argv[1], "destroy")) {
775 action = ACT_DESTROY;
778 } else if (!strcmp(argv[1], "nuke")) {
788 dest = strdup(argv[1]);
802 while((ch = getopt(argc, argv, opts)) != -1)
823 n_opt = strtoul(optarg, &q, 0);
825 errx(1, "-n argument not numeric");
826 if (n_opt < -1 || n_opt > G_BDE_MAXKEYS)
827 errx(1, "-n argument out of range");
840 dfd = open(dest, O_RDWR);
841 if (dfd < 0 && dest[0] != '/') {
842 if (snprintf(buf, sizeof(buf), "%s%s",
843 _PATH_DEV, dest) >= (ssize_t)sizeof(buf))
844 errno = ENAMETOOLONG;
846 dfd = open(buf, O_RDWR);
851 if (!memcmp(dest, _PATH_DEV, strlen(_PATH_DEV)))
852 strcpy(dest, dest + strlen(_PATH_DEV));
855 memset(&sc, 0, sizeof sc);
856 sc.consumer = (void *)&dfd;
860 setup_passphrase(&sc, 0, p_opt, k_opt);
861 cmd_attach(&sc, dest, l_opt);
867 cmd_init(gl, dfd, f_opt, i_opt, L_opt);
868 setup_passphrase(&sc, 1, P_opt, K_opt);
869 cmd_write(gl, &sc, dfd, 0, L_opt);
872 setup_passphrase(&sc, 0, p_opt, k_opt);
873 cmd_open(&sc, dfd, l_opt, &nkey);
876 setup_passphrase(&sc, 1, P_opt, K_opt);
877 cmd_write(gl, &sc, dfd, n_opt - 1, L_opt);
880 setup_passphrase(&sc, 0, p_opt, k_opt);
881 cmd_open(&sc, dfd, l_opt, &nkey);
882 cmd_destroy(gl, nkey);
883 reset_passphrase(&sc);
884 cmd_write(gl, &sc, dfd, nkey, l_opt);
887 setup_passphrase(&sc, 0, p_opt, k_opt);
888 cmd_open(&sc, dfd, l_opt, &nkey);
892 for(i = 0; i < G_BDE_MAXKEYS; i++)
893 cmd_nuke(gl, dfd, i);
895 cmd_nuke(gl, dfd, n_opt - 1);
899 errx(1, "internal error");