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 #include <sys/param.h>
41 #include <sys/mutex.h>
42 #include <sys/malloc.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/kthread.h>
46 #include <sys/sysctl.h>
48 #include <crypto/rijndael/rijndael-api-fst.h>
49 #include <crypto/sha2/sha512.h>
50 #include <geom/geom.h>
51 #include <geom/bde/g_bde.h>
52 #define BDE_CLASS_NAME "BDE"
54 FEATURE(geom_bde, "GEOM-based Disk Encryption");
57 g_bde_start(struct bio *bp)
60 switch (bp->bio_cmd) {
67 g_io_deliver(bp, EOPNOTSUPP);
70 g_io_deliver(bp, EOPNOTSUPP);
77 g_bde_orphan(struct g_consumer *cp)
80 struct g_provider *pp;
81 struct g_bde_softc *sc;
83 g_trace(G_T_TOPOLOGY, "g_bde_orphan(%p/%s)", cp, cp->provider->name);
88 gp->flags |= G_GEOM_WITHER;
89 LIST_FOREACH(pp, &gp->provider, provider)
90 g_wither_provider(pp, ENXIO);
91 explicit_bzero(sc, sizeof(struct g_bde_softc)); /* destroy evidence */
96 g_bde_access(struct g_provider *pp, int dr, int dw, int de)
99 struct g_consumer *cp;
102 cp = LIST_FIRST(&gp->consumer);
103 if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0) {
107 /* ... and let go of it on last close */
108 if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1) {
112 return (g_access(cp, dr, dw, de));
116 g_bde_create_geom(struct gctl_req *req, struct g_class *mp, struct g_provider *pp)
119 struct g_consumer *cp;
120 struct g_bde_key *kp;
124 struct g_bde_softc *sc;
128 g_trace(G_T_TOPOLOGY, "g_bde_create_geom(%s, %s)", mp->name, pp->name);
132 gp = g_new_geomf(mp, "%s.bde", pp->name);
133 cp = g_new_consumer(gp);
134 error = g_attach(cp, pp);
136 g_destroy_consumer(cp);
138 gctl_error(req, "could not attach consumer");
141 error = g_access(cp, 1, 1, 1);
144 g_destroy_consumer(cp);
146 gctl_error(req, "could not access consumer");
152 pass = gctl_get_param(req, "pass", &i);
153 if (pass == NULL || i != SHA512_DIGEST_LENGTH) {
154 gctl_error(req, "No usable key presented");
157 key = gctl_get_param(req, "key", &i);
158 if (key != NULL && i != 16) {
159 gctl_error(req, "Invalid key presented");
162 sectorsize = cp->provider->sectorsize;
163 mediasize = cp->provider->mediasize;
164 sc = g_malloc(sizeof(struct g_bde_softc), M_WAITOK | M_ZERO);
169 error = g_bde_decrypt_lock(sc, pass, key,
170 mediasize, sectorsize, NULL);
171 explicit_bzero(sc->sha2, sizeof sc->sha2);
176 /* Initialize helper-fields */
177 kp->keys_per_sector = kp->sectorsize / G_BDE_SKEYLEN;
178 kp->zone_cont = kp->keys_per_sector * kp->sectorsize;
179 kp->zone_width = kp->zone_cont + kp->sectorsize;
180 kp->media_width = kp->sectorN - kp->sector0 -
181 G_BDE_MAXKEYS * kp->sectorsize;
183 /* Our external parameters */
184 sc->zone_cont = kp->zone_cont;
185 sc->mediasize = g_bde_max_sector(kp);
186 sc->sectorsize = kp->sectorsize;
188 TAILQ_INIT(&sc->freelist);
189 TAILQ_INIT(&sc->worklist);
190 mtx_init(&sc->worklist_mutex, "g_bde_worklist", NULL, MTX_DEF);
191 /* XXX: error check */
192 kproc_create(g_bde_worker, gp, &sc->thread, 0, 0,
193 "g_bde %s", gp->name);
194 pp = g_new_providerf(gp, "%s", gp->name);
195 pp->stripesize = kp->zone_cont;
196 pp->stripeoffset = 0;
197 pp->mediasize = sc->mediasize;
198 pp->sectorsize = sc->sectorsize;
199 g_error_provider(pp, 0);
203 explicit_bzero(pass, SHA512_DIGEST_LENGTH);
205 explicit_bzero(key, 16);
208 g_access(cp, -1, -1, -1);
210 g_destroy_consumer(cp);
215 gctl_error(req, "Lock was destroyed");
218 gctl_error(req, "Lock was nuked");
221 gctl_error(req, "Could not open lock");
224 gctl_error(req, "Lock not found");
227 gctl_error(req, "Could not open lock (%d)", error);
234 g_bde_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
236 struct g_consumer *cp;
237 struct g_provider *pp;
238 struct g_bde_softc *sc;
240 g_trace(G_T_TOPOLOGY, "g_bde_destroy_geom(%s, %s)", mp->name, gp->name);
243 * Orderly detachment.
245 KASSERT(gp != NULL, ("NULL geom"));
246 pp = LIST_FIRST(&gp->provider);
247 KASSERT(pp != NULL, ("NULL provider"));
248 if (pp->acr > 0 || pp->acw > 0 || pp->ace > 0)
251 cp = LIST_FIRST(&gp->consumer);
252 KASSERT(cp != NULL, ("NULL consumer"));
255 g_access(cp, -1, -1, -1);
257 g_destroy_consumer(cp);
258 while (sc->dead != 2 && !LIST_EMPTY(&pp->consumers))
259 tsleep(sc, PRIBIO, "g_bdedie", hz);
260 mtx_destroy(&sc->worklist_mutex);
261 explicit_bzero(&sc->key, sizeof sc->key);
263 g_wither_geom(gp, ENXIO);
268 g_bde_ctlreq(struct gctl_req *req, struct g_class *mp, char const *verb)
271 struct g_provider *pp;
273 if (!strcmp(verb, "create geom")) {
274 pp = gctl_get_provider(req, "provider");
276 g_bde_create_geom(req, mp, pp);
277 } else if (!strcmp(verb, "destroy geom")) {
278 gp = gctl_get_geom(req, mp, "geom");
280 g_bde_destroy_geom(req, mp, gp);
282 gctl_error(req, "unknown verb");
286 static struct g_class g_bde_class = {
287 .name = BDE_CLASS_NAME,
288 .version = G_VERSION,
289 .destroy_geom = g_bde_destroy_geom,
290 .ctlreq = g_bde_ctlreq,
291 .start = g_bde_start,
292 .orphan = g_bde_orphan,
293 .access = g_bde_access,
294 .spoiled = g_std_spoiled,
297 DECLARE_GEOM_CLASS(g_bde_class, g_bde);
298 MODULE_VERSION(geom_bde, 0);