2 * SPDX-License-Identifier: BSD-2-Clause
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
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/sha512.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)
175 arc4random_buf(p, len);
179 static u_char sha2[SHA512_DIGEST_LENGTH];
182 reset_passphrase(struct g_bde_softc *sc)
185 memcpy(sc->sha2, sha2, SHA512_DIGEST_LENGTH);
189 setup_passphrase(struct g_bde_softc *sc, int sure, const char *input,
192 char buf1[BUFSIZ + SHA512_DIGEST_LENGTH];
193 char buf2[BUFSIZ + SHA512_DIGEST_LENGTH];
195 int kfd, klen, bpos = 0;
197 if (keyfile != NULL) {
198 /* Read up to BUFSIZ bytes from keyfile */
199 kfd = open(keyfile, O_RDONLY, 0);
201 err(1, "%s", keyfile);
202 klen = read(kfd, buf1, BUFSIZ);
204 err(1, "%s", keyfile);
207 /* Prepend the passphrase with the hash of the key read */
208 g_bde_hash_pass(sc, buf1, klen);
209 memcpy(buf1, sc->sha2, SHA512_DIGEST_LENGTH);
210 memcpy(buf2, sc->sha2, SHA512_DIGEST_LENGTH);
211 bpos = SHA512_DIGEST_LENGTH;
215 if (strlen(input) >= BUFSIZ)
216 errx(1, "Passphrase too long");
217 strcpy(buf1 + bpos, input);
219 g_bde_hash_pass(sc, buf1, strlen(buf1 + bpos) + bpos);
220 memcpy(sha2, sc->sha2, SHA512_DIGEST_LENGTH);
225 sure ? "Enter new passphrase:" : "Enter passphrase: ",
226 buf1 + bpos, sizeof buf1 - bpos,
227 RPP_ECHO_OFF | RPP_REQUIRE_TTY);
229 err(1, "readpassphrase");
232 p = readpassphrase("Reenter new passphrase: ",
233 buf2 + bpos, sizeof buf2 - bpos,
234 RPP_ECHO_OFF | RPP_REQUIRE_TTY);
236 err(1, "readpassphrase");
238 if (strcmp(buf1 + bpos, buf2 + bpos)) {
239 printf("They didn't match.\n");
243 if (strlen(buf1 + bpos) < 3) {
244 printf("Too short passphrase.\n");
249 g_bde_hash_pass(sc, buf1, strlen(buf1 + bpos) + bpos);
250 memcpy(sha2, sc->sha2, SHA512_DIGEST_LENGTH);
254 encrypt_sector(void *d, int len, int klen, void *key)
260 error = rijndael_cipherInit(&ci, MODE_CBC, NULL);
262 errx(1, "rijndael_cipherInit=%d", error);
263 error = rijndael_makeKey(&ki, DIR_ENCRYPT, klen, key);
265 errx(1, "rijndael_makeKeY=%d", error);
266 error = rijndael_blockEncrypt(&ci, &ki, d, len * 8, d);
268 errx(1, "rijndael_blockEncrypt=%d", error);
272 cmd_attach(const struct g_bde_softc *sc, const char *dest, const char *lfile)
279 r = gctl_get_handle();
280 gctl_ro_param(r, "verb", -1, "create geom");
281 gctl_ro_param(r, "class", -1, "BDE");
282 gctl_ro_param(r, "provider", -1, dest);
283 gctl_ro_param(r, "pass", SHA512_DIGEST_LENGTH, sc->sha2);
285 ffd = open(lfile, O_RDONLY, 0);
289 gctl_ro_param(r, "key", 16, buf);
292 errstr = gctl_issue(r);
294 errx(1, "Attach to %s failed: %s", dest, errstr);
300 cmd_detach(const char *dest)
306 r = gctl_get_handle();
307 gctl_ro_param(r, "verb", -1, "destroy geom");
308 gctl_ro_param(r, "class", -1, "BDE");
309 sprintf(buf, "%s.bde", dest);
310 gctl_ro_param(r, "geom", -1, buf);
311 /* gctl_dump(r, stdout); */
312 errstr = gctl_issue(r);
314 errx(1, "Detach of %s failed: %s", dest, errstr);
319 cmd_open(struct g_bde_softc *sc, int dfd , const char *l_opt, u_int *nkey)
328 error = ioctl(dfd, DIOCGSECTORSIZE, §orsize);
331 error = ioctl(dfd, DIOCGMEDIASIZE, &mediasize);
333 error = fstat(dfd, &st);
334 if (error == 0 && S_ISREG(st.st_mode))
335 mediasize = st.st_size;
340 mediasize = (off_t)-1;
342 ffd = open(l_opt, O_RDONLY, 0);
345 read(ffd, keyloc, sizeof keyloc);
348 memset(keyloc, 0, sizeof keyloc);
351 error = g_bde_decrypt_lock(sc, sc->sha2, keyloc, mediasize,
354 errx(1, "Lock was destroyed.");
356 errx(1, "Lock was nuked.");
357 if (error == ENOTDIR)
358 errx(1, "Lock not found");
360 errx(1, "Error %d decrypting lock", error);
362 printf("Opened with key %u\n", 1 + *nkey);
367 cmd_nuke(struct g_bde_key *gl, int dfd , int key)
371 off_t offset, offset2;
373 sbuf = malloc(gl->sectorsize);
374 memset(sbuf, 0, gl->sectorsize);
375 offset = (gl->lsector[key] & ~(gl->sectorsize - 1));
376 offset2 = lseek(dfd, offset, SEEK_SET);
377 if (offset2 != offset)
379 i = write(dfd, sbuf, gl->sectorsize);
381 if (i != (int)gl->sectorsize)
383 printf("Nuked key %d\n", 1 + key);
387 cmd_write(struct g_bde_key *gl, struct g_bde_softc *sc, int dfd , int key, const char *l_opt)
393 off_t offset, offset2;
395 sbuf = malloc(gl->sectorsize);
397 * Find the byte-offset in the lock sector where we will put the lock
398 * data structure. We can put it any random place as long as the
402 random_bits(off, sizeof off);
403 off[0] &= (gl->sectorsize - 1);
404 if (off[0] + G_BDE_LOCKSIZE > gl->sectorsize)
409 /* Add the sector offset in bytes */
410 off[0] += (gl->lsector[key] & ~(gl->sectorsize - 1));
411 gl->lsector[key] = off[0];
413 i = g_bde_keyloc_encrypt(sc->sha2, off[0], off[1], keyloc);
415 errx(1, "g_bde_keyloc_encrypt()");
417 ffd = open(l_opt, O_WRONLY | O_CREAT | O_TRUNC, 0600);
420 write(ffd, keyloc, sizeof keyloc);
422 } else if (gl->flags & GBDE_F_SECT0) {
423 offset2 = lseek(dfd, 0, SEEK_SET);
426 i = read(dfd, sbuf, gl->sectorsize);
427 if (i != (int)gl->sectorsize)
429 memcpy(sbuf + key * 16, keyloc, sizeof keyloc);
430 offset2 = lseek(dfd, 0, SEEK_SET);
433 i = write(dfd, sbuf, gl->sectorsize);
434 if (i != (int)gl->sectorsize)
437 errx(1, "No -L option and no space in sector 0 for lockfile");
440 /* Allocate a sectorbuffer and fill it with random junk */
443 random_bits(sbuf, gl->sectorsize);
445 /* Fill random bits in the spare field */
446 random_bits(gl->spare, sizeof(gl->spare));
448 /* Encode the structure where we want it */
449 q = sbuf + (off[0] % gl->sectorsize);
450 i = g_bde_encode_lock(sc->sha2, gl, q);
452 errx(1, "programming error encoding lock");
454 encrypt_sector(q, G_BDE_LOCKSIZE, 256, sc->sha2 + 16);
455 offset = gl->lsector[key] & ~(gl->sectorsize - 1);
456 offset2 = lseek(dfd, offset, SEEK_SET);
457 if (offset2 != offset)
459 i = write(dfd, sbuf, gl->sectorsize);
460 if (i != (int)gl->sectorsize)
464 printf("Wrote key %d at %jd\n", key, (intmax_t)offset);
465 printf("s0 = %jd\n", (intmax_t)gl->sector0);
466 printf("sN = %jd\n", (intmax_t)gl->sectorN);
467 printf("l[0] = %jd\n", (intmax_t)gl->lsector[0]);
468 printf("l[1] = %jd\n", (intmax_t)gl->lsector[1]);
469 printf("l[2] = %jd\n", (intmax_t)gl->lsector[2]);
470 printf("l[3] = %jd\n", (intmax_t)gl->lsector[3]);
471 printf("k = %jd\n", (intmax_t)gl->keyoffset);
472 printf("ss = %jd\n", (intmax_t)gl->sectorsize);
477 cmd_destroy(struct g_bde_key *gl, int nkey)
481 bzero(&gl->sector0, sizeof gl->sector0);
482 bzero(&gl->sectorN, sizeof gl->sectorN);
483 bzero(&gl->keyoffset, sizeof gl->keyoffset);
484 gl->flags &= GBDE_F_SECT0;
485 bzero(gl->mkey, sizeof gl->mkey);
486 for (i = 0; i < G_BDE_MAXKEYS; i++)
492 sorthelp(const void *a, const void *b)
494 const uint64_t *oa, *ob;
506 cmd_init(struct g_bde_key *gl, int dfd, const char *f_opt, int i_opt, const char *l_opt)
510 unsigned sector_size;
511 uint64_t first_sector;
512 uint64_t last_sector;
513 uint64_t total_sectors;
517 char *q, cbuf[BUFSIZ];
522 bzero(gl, sizeof *gl);
524 i = open(f_opt, O_RDONLY);
527 params = properties_read(i);
531 asprintf(&q, "%stemp.XXXXXXXXXX", _PATH_TMP);
537 write(i, template, strlen(template));
539 p = getenv("EDITOR");
542 if (snprintf(cbuf, sizeof(cbuf), "%s %s\n", p, q) >=
543 (ssize_t)sizeof(cbuf)) {
545 errx(1, "EDITOR is too long");
548 i = open(q, O_RDONLY);
551 params = properties_read(i);
557 i = open(_PATH_DEVNULL, O_RDONLY);
559 err(1, "%s", _PATH_DEVNULL);
560 params = properties_read(i);
565 p = property_find(params, "sector_size");
566 i = ioctl(dfd, DIOCGSECTORSIZE, &u);
568 sector_size = strtoul(p, &q, 0);
570 errx(1, "sector_size not a proper number");
574 errx(1, "Missing sector_size property");
576 if (sector_size & (sector_size - 1))
577 errx(1, "sector_size not a power of 2");
578 if (sector_size < 512)
579 errx(1, "sector_size is smaller than 512");
580 buf = malloc(sector_size);
582 err(1, "Failed to malloc sector buffer");
583 gl->sectorsize = sector_size;
585 i = ioctl(dfd, DIOCGMEDIASIZE, &off);
588 total_sectors = off / sector_size;
589 last_sector = total_sectors - 1;
597 p = property_find(params, "first_sector");
599 first_sector = strtoul(p, &q, 0);
601 errx(1, "first_sector not a proper number");
605 p = property_find(params, "last_sector");
607 last_sector = strtoul(p, &q, 0);
609 errx(1, "last_sector not a proper number");
610 if (last_sector <= first_sector)
611 errx(1, "last_sector not larger than first_sector");
612 total_sectors = last_sector + 1;
615 /* <total_sectors> */
616 p = property_find(params, "total_sectors");
618 total_sectors = strtoul(p, &q, 0);
620 errx(1, "total_sectors not a proper number");
621 if (last_sector == 0)
622 last_sector = first_sector + total_sectors - 1;
625 if (l_opt == NULL && first_sector != 0)
626 errx(1, "No -L new-lockfile argument and first_sector != 0");
627 else if (l_opt == NULL) {
630 gl->flags |= GBDE_F_SECT0;
632 gl->sector0 = first_sector * gl->sectorsize;
634 if (total_sectors != (last_sector - first_sector) + 1)
635 errx(1, "total_sectors disagree with first_sector and last_sector");
636 if (total_sectors == 0)
637 errx(1, "missing last_sector or total_sectors");
639 gl->sectorN = (last_sector + 1) * gl->sectorsize;
641 /* Find a random keyoffset */
642 random_bits(&o, sizeof o);
643 o %= (gl->sectorN - gl->sector0);
644 o &= ~(gl->sectorsize - 1);
647 /* <number_of_keys> */
648 p = property_find(params, "number_of_keys");
650 nkeys = strtoul(p, &q, 0);
652 errx(1, "number_of_keys not a proper number");
653 if (nkeys < 1 || nkeys > G_BDE_MAXKEYS)
654 errx(1, "number_of_keys out of range");
658 for (u = 0; u < nkeys; u++) {
661 random_bits(&o, sizeof o);
663 o &= ~(gl->sectorsize - 1);
664 } while(o < gl->sector0);
665 for (u2 = 0; u2 < u; u2++)
666 if (o == gl->lsector[u2])
674 for (; u < G_BDE_MAXKEYS; u++) {
676 random_bits(&o, sizeof o);
677 while (o < gl->sectorN);
680 qsort(gl->lsector, G_BDE_MAXKEYS, sizeof gl->lsector[0], sorthelp);
682 /* Flush sector zero if we use it for lockfile data */
683 if (gl->flags & GBDE_F_SECT0) {
684 off2 = lseek(dfd, 0, SEEK_SET);
686 err(1, "lseek(2) to sector 0");
687 random_bits(buf, sector_size);
688 i = write(dfd, buf, sector_size);
689 if (i != (int)sector_size)
690 err(1, "write sector 0");
694 p = property_find(params, "random_flush");
696 off = first_sector * sector_size;
697 off2 = lseek(dfd, off, SEEK_SET);
699 err(1, "lseek(2) to first_sector");
700 off2 = last_sector * sector_size;
701 while (off <= off2) {
702 random_bits(buf, sector_size);
703 i = write(dfd, buf, sector_size);
704 if (i != (int)sector_size)
705 err(1, "write to $device_name");
710 random_bits(gl->mkey, sizeof gl->mkey);
711 random_bits(gl->salt, sizeof gl->salt);
718 ACT_ATTACH, ACT_DETACH,
719 ACT_INIT, ACT_SETKEY, ACT_DESTROY, ACT_NUKE
723 main(int argc, char **argv)
726 const char *k_opt, *K_opt;
727 const char *l_opt, *L_opt;
728 const char *p_opt, *P_opt;
731 int i_opt, n_opt, ch, dfd, doopen;
734 char *q, buf[BUFSIZ];
735 struct g_bde_key *gl;
736 struct g_bde_softc sc;
738 fprintf(stderr, "GBDE disk-encryption is deprecated,\n");
739 fprintf(stderr, "and will be removed in FreeBSD 15.0\n");
740 fprintf(stderr, "(continuing in 5 seconds)\n");
746 if (modfind("g_bde") < 0) {
747 /* need to load the gbde module */
748 if (kldload(GBDEMOD) < 0 || modfind("g_bde") < 0)
749 err(1, GBDEMOD ": Kernel module not available");
752 if (!strcmp(argv[1], "attach")) {
755 } else if (!strcmp(argv[1], "detach")) {
758 } else if (!strcmp(argv[1], "init")) {
762 } else if (!strcmp(argv[1], "setkey")) {
765 opts = "k:K:l:L:n:p:P:";
766 } else if (!strcmp(argv[1], "destroy")) {
767 action = ACT_DESTROY;
770 } else if (!strcmp(argv[1], "nuke")) {
780 dest = strdup(argv[1]);
794 while((ch = getopt(argc, argv, opts)) != -1)
815 n_opt = strtoul(optarg, &q, 0);
817 errx(1, "-n argument not numeric");
818 if (n_opt < -1 || n_opt > G_BDE_MAXKEYS)
819 errx(1, "-n argument out of range");
832 dfd = open(dest, O_RDWR);
833 if (dfd < 0 && dest[0] != '/') {
834 if (snprintf(buf, sizeof(buf), "%s%s",
835 _PATH_DEV, dest) >= (ssize_t)sizeof(buf))
836 errno = ENAMETOOLONG;
838 dfd = open(buf, O_RDWR);
843 if (!memcmp(dest, _PATH_DEV, strlen(_PATH_DEV)))
844 strcpy(dest, dest + strlen(_PATH_DEV));
847 memset(&sc, 0, sizeof sc);
848 sc.consumer = (void *)&dfd;
852 setup_passphrase(&sc, 0, p_opt, k_opt);
853 cmd_attach(&sc, dest, l_opt);
859 cmd_init(gl, dfd, f_opt, i_opt, L_opt);
860 setup_passphrase(&sc, 1, P_opt, K_opt);
861 cmd_write(gl, &sc, dfd, 0, L_opt);
864 setup_passphrase(&sc, 0, p_opt, k_opt);
865 cmd_open(&sc, dfd, l_opt, &nkey);
868 setup_passphrase(&sc, 1, P_opt, K_opt);
869 cmd_write(gl, &sc, dfd, n_opt - 1, L_opt);
872 setup_passphrase(&sc, 0, p_opt, k_opt);
873 cmd_open(&sc, dfd, l_opt, &nkey);
874 cmd_destroy(gl, nkey);
875 reset_passphrase(&sc);
876 cmd_write(gl, &sc, dfd, nkey, l_opt);
879 setup_passphrase(&sc, 0, p_opt, k_opt);
880 cmd_open(&sc, dfd, l_opt, &nkey);
884 for(i = 0; i < G_BDE_MAXKEYS; i++)
885 cmd_nuke(gl, dfd, i);
887 cmd_nuke(gl, dfd, n_opt - 1);
891 errx(1, "internal error");