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.
19 * 3. The names of the authors may not be used to endorse or promote
20 * products derived from this software without specific prior written
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * This method provides AES encryption with a compiled in key (default
40 * XXX: This could probably save a lot of code by pretending to be a slicer.
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
51 #include <sys/malloc.h>
53 #include <sys/mutex.h>
54 #include <sys/libkern.h>
55 #include <sys/endian.h>
57 #include <sys/errno.h>
58 #include <geom/geom.h>
60 #include <crypto/rijndael/rijndael-api-fst.h>
62 #define AES_CLASS_NAME "AES"
64 #define MASTER_KEY_LENGTH (1024/8)
66 static const u_char *aes_magic = "<<FreeBSD-GEOM-AES>>";
67 static const u_char *aes_magic_random = "<<FreeBSD-GEOM-AES-RANDOM>>";
68 static const u_char *aes_magic_test = "<<FreeBSD-GEOM-AES-TEST>>";
80 u_char master_key[MASTER_KEY_LENGTH];
84 * Generate a sectorkey from the masterkey and the offset position.
86 * For KEY_ZERO we just return a key of all zeros.
88 * We feed the sector byte offset, 16 bytes of the master-key and
89 * the sector byte offset once more to MD5.
90 * The sector byte offset is converted to little-endian format first
91 * to support multi-architecture operation.
92 * We use 16 bytes from the master-key starting at the logical sector
93 * number modulus he length of the master-key. If need be we wrap
94 * around to the start of the master-key.
98 g_aes_makekey(struct g_aes_softc *sc, off_t off, keyInstance *ki, int dir)
105 if (sc->keying == KEY_ZERO) {
106 rijndael_makeKey(ki, dir, 128, sc->master_key);
111 MD5Update(&cx, (u_char *)&u64, sizeof(u64));
112 u = off / sc->sectorsize;
113 u %= sizeof sc->master_key;
114 p = sc->master_key + u;
115 if (u + 16 <= sizeof(sc->master_key)) {
116 MD5Update(&cx, p, 16);
118 u1 = sizeof sc->master_key - u;
119 MD5Update(&cx, p, u1);
120 MD5Update(&cx, sc->master_key, 16 - u1);
121 u1 = 0; /* destroy evidence */
123 u = 0; /* destroy evidence */
124 MD5Update(&cx, (u_char *)&u64, sizeof(u64));
125 u64 = 0; /* destroy evidence */
127 bzero(&cx, sizeof cx); /* destroy evidence */
128 rijndael_makeKey(ki, dir, 128, buf);
129 bzero(buf, sizeof buf); /* destroy evidence */
134 g_aes_read_done(struct bio *bp)
137 struct g_aes_softc *sc;
138 u_char *p, *b, *e, *sb;
142 gp = bp->bio_from->geom;
144 sb = g_malloc(sc->sectorsize, M_WAITOK);
148 o = bp->bio_offset - sc->sectorsize;
149 for (p = b; p < e; p += sc->sectorsize) {
150 g_aes_makekey(sc, o, &dkey, DIR_DECRYPT);
151 rijndael_blockDecrypt(&sc->ci, &dkey, p, sc->sectorsize * 8, sb);
152 bcopy(sb, p, sc->sectorsize);
155 bzero(&dkey, sizeof dkey); /* destroy evidence */
156 bzero(sb, sc->sectorsize); /* destroy evidence */
162 g_aes_write_done(struct bio *bp)
165 bzero(bp->bio_data, bp->bio_length); /* destroy evidence */
166 g_free(bp->bio_data);
171 g_aes_start(struct bio *bp)
174 struct g_consumer *cp;
175 struct g_aes_softc *sc;
177 u_char *p1, *p2, *b, *e;
181 gp = bp->bio_to->geom;
182 cp = LIST_FIRST(&gp->consumer);
184 switch (bp->bio_cmd) {
186 bp2 = g_clone_bio(bp);
188 g_io_deliver(bp, ENOMEM);
191 bp2->bio_done = g_aes_read_done;
192 bp2->bio_offset += sc->sectorsize;
193 g_io_request(bp2, cp);
196 bp2 = g_clone_bio(bp);
198 g_io_deliver(bp, ENOMEM);
201 bp2->bio_done = g_aes_write_done;
202 bp2->bio_offset += sc->sectorsize;
203 bp2->bio_data = g_malloc(bp->bio_length, M_WAITOK);
209 for (p1 = b; p1 < e; p1 += sc->sectorsize) {
210 g_aes_makekey(sc, o, &ekey, DIR_ENCRYPT);
211 rijndael_blockEncrypt(&sc->ci, &ekey,
212 p1, sc->sectorsize * 8, p2);
213 p2 += sc->sectorsize;
216 bzero(&ekey, sizeof ekey); /* destroy evidence */
217 g_io_request(bp2, cp);
220 bp2 = g_clone_bio(bp);
222 g_io_deliver(bp, ENOMEM);
225 bp2->bio_done = g_std_done;
226 bp2->bio_offset += sc->sectorsize;
227 g_io_request(bp2, cp);
230 g_io_deliver(bp, EOPNOTSUPP);
237 g_aes_orphan(struct g_consumer *cp)
240 struct g_aes_softc *sc;
242 g_trace(G_T_TOPOLOGY, "g_aes_orphan(%p/%s)", cp, cp->provider->name);
247 g_wither_geom(gp, ENXIO);
248 bzero(sc, sizeof(struct g_aes_softc)); /* destroy evidence */
254 g_aes_access(struct g_provider *pp, int dr, int dw, int de)
257 struct g_consumer *cp;
260 cp = LIST_FIRST(&gp->consumer);
261 /* On first open, grab an extra "exclusive" bit */
262 if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0)
264 /* ... and let go of it on last close */
265 if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1)
267 return (g_access(cp, dr, dw, de));
270 static struct g_geom *
271 g_aes_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
274 struct g_consumer *cp;
275 struct g_aes_softc *sc;
281 g_trace(G_T_TOPOLOGY, "aes_taste(%s,%s)", mp->name, pp->name);
283 gp = g_new_geomf(mp, "%s.aes", pp->name);
284 cp = g_new_consumer(gp);
286 error = g_access(cp, 1, 0, 0);
289 g_destroy_consumer(cp);
298 sectorsize = cp->provider->sectorsize;
299 mediasize = cp->provider->mediasize;
300 buf = g_read_data(cp, 0, sectorsize, NULL);
304 sc = g_malloc(sizeof(struct g_aes_softc), M_WAITOK | M_ZERO);
305 if (!memcmp(buf, aes_magic, strlen(aes_magic))) {
306 sc->keying = KEY_ZERO;
307 } else if (!memcmp(buf, aes_magic_random,
308 strlen(aes_magic_random))) {
309 sc->keying = KEY_RANDOM;
310 } else if (!memcmp(buf, aes_magic_test,
311 strlen(aes_magic_test))) {
312 sc->keying = KEY_TEST;
319 sc->sectorsize = sectorsize;
320 sc->mediasize = mediasize - sectorsize;
321 rijndael_cipherInit(&sc->ci, MODE_CBC, NULL);
322 if (sc->keying == KEY_TEST) {
327 for (i = 0; i < (int)sizeof sc->master_key; i ++)
330 if (sc->keying == KEY_RANDOM) {
336 for (i = 0; i < (int)sizeof sc->master_key; i += sizeof u) {
345 pp = g_new_providerf(gp, "%s", gp->name);
346 pp->mediasize = mediasize - sectorsize;
347 pp->sectorsize = sectorsize;
348 g_error_provider(pp, 0);
354 g_access(cp, -1, 0, 0);
355 if (gp->softc != NULL)
358 g_destroy_consumer(cp);
363 static struct g_class g_aes_class = {
364 .name = AES_CLASS_NAME,
365 .version = G_VERSION,
366 .taste = g_aes_taste,
367 .start = g_aes_start,
368 .orphan = g_aes_orphan,
369 .spoiled = g_std_spoiled,
370 .access = g_aes_access,
373 DECLARE_GEOM_CLASS(g_aes_class, g_aes);