2 * SPDX-License-Identifier: BSD-3-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.
21 * 3. The names of the authors may not be used to endorse or promote
22 * products derived from this software without specific prior written
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * This method provides AES encryption with a compiled in key (default
42 * XXX: This could probably save a lot of code by pretending to be a slicer.
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
53 #include <sys/malloc.h>
55 #include <sys/mutex.h>
56 #include <sys/libkern.h>
57 #include <sys/endian.h>
59 #include <sys/errno.h>
60 #include <geom/geom.h>
62 #include <crypto/rijndael/rijndael-api-fst.h>
64 #define AES_CLASS_NAME "AES"
66 #define MASTER_KEY_LENGTH (1024/8)
68 static const u_char *aes_magic = "<<FreeBSD-GEOM-AES>>";
69 static const u_char *aes_magic_random = "<<FreeBSD-GEOM-AES-RANDOM>>";
70 static const u_char *aes_magic_test = "<<FreeBSD-GEOM-AES-TEST>>";
82 u_char master_key[MASTER_KEY_LENGTH];
86 * Generate a sectorkey from the masterkey and the offset position.
88 * For KEY_ZERO we just return a key of all zeros.
90 * We feed the sector byte offset, 16 bytes of the master-key and
91 * the sector byte offset once more to MD5.
92 * The sector byte offset is converted to little-endian format first
93 * to support multi-architecture operation.
94 * We use 16 bytes from the master-key starting at the logical sector
95 * number modulus he length of the master-key. If need be we wrap
96 * around to the start of the master-key.
100 g_aes_makekey(struct g_aes_softc *sc, off_t off, keyInstance *ki, int dir)
107 if (sc->keying == KEY_ZERO) {
108 rijndael_makeKey(ki, dir, 128, sc->master_key);
113 MD5Update(&cx, (u_char *)&u64, sizeof(u64));
114 u = off / sc->sectorsize;
115 u %= sizeof sc->master_key;
116 p = sc->master_key + u;
117 if (u + 16 <= sizeof(sc->master_key)) {
118 MD5Update(&cx, p, 16);
120 u1 = sizeof sc->master_key - u;
121 MD5Update(&cx, p, u1);
122 MD5Update(&cx, sc->master_key, 16 - u1);
123 u1 = 0; /* destroy evidence */
125 u = 0; /* destroy evidence */
126 MD5Update(&cx, (u_char *)&u64, sizeof(u64));
127 u64 = 0; /* destroy evidence */
129 bzero(&cx, sizeof cx); /* destroy evidence */
130 rijndael_makeKey(ki, dir, 128, buf);
131 bzero(buf, sizeof buf); /* destroy evidence */
136 g_aes_read_done(struct bio *bp)
139 struct g_aes_softc *sc;
140 u_char *p, *b, *e, *sb;
144 gp = bp->bio_from->geom;
146 sb = g_malloc(sc->sectorsize, M_WAITOK);
150 o = bp->bio_offset - sc->sectorsize;
151 for (p = b; p < e; p += sc->sectorsize) {
152 g_aes_makekey(sc, o, &dkey, DIR_DECRYPT);
153 rijndael_blockDecrypt(&sc->ci, &dkey, p, sc->sectorsize * 8, sb);
154 bcopy(sb, p, sc->sectorsize);
157 bzero(&dkey, sizeof dkey); /* destroy evidence */
158 bzero(sb, sc->sectorsize); /* destroy evidence */
164 g_aes_write_done(struct bio *bp)
167 bzero(bp->bio_data, bp->bio_length); /* destroy evidence */
168 g_free(bp->bio_data);
173 g_aes_start(struct bio *bp)
176 struct g_consumer *cp;
177 struct g_aes_softc *sc;
179 u_char *p1, *p2, *b, *e;
183 gp = bp->bio_to->geom;
184 cp = LIST_FIRST(&gp->consumer);
186 switch (bp->bio_cmd) {
188 bp2 = g_clone_bio(bp);
190 g_io_deliver(bp, ENOMEM);
193 bp2->bio_done = g_aes_read_done;
194 bp2->bio_offset += sc->sectorsize;
195 g_io_request(bp2, cp);
198 bp2 = g_clone_bio(bp);
200 g_io_deliver(bp, ENOMEM);
203 bp2->bio_done = g_aes_write_done;
204 bp2->bio_offset += sc->sectorsize;
205 bp2->bio_data = g_malloc(bp->bio_length, M_WAITOK);
211 for (p1 = b; p1 < e; p1 += sc->sectorsize) {
212 g_aes_makekey(sc, o, &ekey, DIR_ENCRYPT);
213 rijndael_blockEncrypt(&sc->ci, &ekey,
214 p1, sc->sectorsize * 8, p2);
215 p2 += sc->sectorsize;
218 bzero(&ekey, sizeof ekey); /* destroy evidence */
219 g_io_request(bp2, cp);
222 bp2 = g_clone_bio(bp);
224 g_io_deliver(bp, ENOMEM);
227 bp2->bio_done = g_std_done;
228 bp2->bio_offset += sc->sectorsize;
229 g_io_request(bp2, cp);
232 g_io_deliver(bp, EOPNOTSUPP);
239 g_aes_orphan(struct g_consumer *cp)
242 struct g_aes_softc *sc;
244 g_trace(G_T_TOPOLOGY, "g_aes_orphan(%p/%s)", cp, cp->provider->name);
249 g_wither_geom(gp, ENXIO);
250 bzero(sc, sizeof(struct g_aes_softc)); /* destroy evidence */
256 g_aes_access(struct g_provider *pp, int dr, int dw, int de)
259 struct g_consumer *cp;
262 cp = LIST_FIRST(&gp->consumer);
263 /* On first open, grab an extra "exclusive" bit */
264 if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0)
266 /* ... and let go of it on last close */
267 if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1)
269 return (g_access(cp, dr, dw, de));
272 static struct g_geom *
273 g_aes_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
276 struct g_consumer *cp;
277 struct g_aes_softc *sc;
283 g_trace(G_T_TOPOLOGY, "aes_taste(%s,%s)", mp->name, pp->name);
285 gp = g_new_geomf(mp, "%s.aes", pp->name);
286 cp = g_new_consumer(gp);
288 error = g_access(cp, 1, 0, 0);
291 g_destroy_consumer(cp);
300 sectorsize = cp->provider->sectorsize;
301 mediasize = cp->provider->mediasize;
302 buf = g_read_data(cp, 0, sectorsize, NULL);
306 sc = g_malloc(sizeof(struct g_aes_softc), M_WAITOK | M_ZERO);
307 if (!memcmp(buf, aes_magic, strlen(aes_magic))) {
308 sc->keying = KEY_ZERO;
309 } else if (!memcmp(buf, aes_magic_random,
310 strlen(aes_magic_random))) {
311 sc->keying = KEY_RANDOM;
312 } else if (!memcmp(buf, aes_magic_test,
313 strlen(aes_magic_test))) {
314 sc->keying = KEY_TEST;
321 sc->sectorsize = sectorsize;
322 sc->mediasize = mediasize - sectorsize;
323 rijndael_cipherInit(&sc->ci, MODE_CBC, NULL);
324 if (sc->keying == KEY_TEST) {
329 for (i = 0; i < (int)sizeof sc->master_key; i ++)
332 if (sc->keying == KEY_RANDOM) {
338 for (i = 0; i < (int)sizeof sc->master_key; i += sizeof u) {
347 pp = g_new_providerf(gp, "%s", gp->name);
348 pp->mediasize = mediasize - sectorsize;
349 pp->sectorsize = sectorsize;
350 g_error_provider(pp, 0);
356 g_access(cp, -1, 0, 0);
357 if (gp->softc != NULL)
360 g_destroy_consumer(cp);
365 static struct g_class g_aes_class = {
366 .name = AES_CLASS_NAME,
367 .version = G_VERSION,
368 .taste = g_aes_taste,
369 .start = g_aes_start,
370 .orphan = g_aes_orphan,
371 .spoiled = g_std_spoiled,
372 .access = g_aes_access,
375 DECLARE_GEOM_CLASS(g_aes_class, g_aes);