xref: /freebsd-11-stable/sys/geom/geom_aes.c (revision 0543b36fed3002fb2175b16986bf886601ab8dcf)
1 /*-
2  * Copyright (c) 2002 Poul-Henning Kamp
3  * Copyright (c) 2002 Networks Associates Technology, Inc.
4  * All rights reserved.
5  *
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.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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
21  *    permission.
22  *
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
33  * SUCH DAMAGE.
34  */
35 
36 /*
37  * This method provides AES encryption with a compiled in key (default
38  * all zeroes).
39  *
40  * XXX: This could probably save a lot of code by pretending to be a slicer.
41  */
42 
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/conf.h>
50 #include <sys/bio.h>
51 #include <sys/malloc.h>
52 #include <sys/lock.h>
53 #include <sys/mutex.h>
54 #include <sys/libkern.h>
55 #include <sys/endian.h>
56 #include <sys/md5.h>
57 #include <sys/errno.h>
58 #include <geom/geom.h>
59 
60 #include <crypto/rijndael/rijndael-api-fst.h>
61 
62 #define AES_CLASS_NAME "AES"
63 
64 #define MASTER_KEY_LENGTH	(1024/8)
65 
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>>";
69 
70 static int g_aes_once;
71 
72 struct g_aes_softc {
73 	enum {
74 		KEY_ZERO,
75 		KEY_RANDOM,
76 		KEY_TEST
77 	} keying;
78 	u_int	sectorsize;
79 	off_t	mediasize;
80 	cipherInstance ci;
81 	u_char master_key[MASTER_KEY_LENGTH];
82 };
83 
84 /*
85  * Generate a sectorkey from the masterkey and the offset position.
86  *
87  * For KEY_ZERO we just return a key of all zeros.
88  *
89  * We feed the sector byte offset, 16 bytes of the master-key and
90  * the sector byte offset once more to MD5.
91  * The sector byte offset is converted to little-endian format first
92  * to support multi-architecture operation.
93  * We use 16 bytes from the master-key starting at the logical sector
94  * number modulus he length of the master-key.  If need be we wrap
95  * around to the start of the master-key.
96  */
97 
98 static void
g_aes_makekey(struct g_aes_softc * sc,off_t off,keyInstance * ki,int dir)99 g_aes_makekey(struct g_aes_softc *sc, off_t off, keyInstance *ki, int dir)
100 {
101 	MD5_CTX cx;
102 	u_int64_t u64;
103 	u_int u, u1;
104 	u_char *p, buf[16];
105 
106 	if (sc->keying == KEY_ZERO) {
107 		rijndael_makeKey(ki, dir, 128, sc->master_key);
108 		return;
109 	}
110 	MD5Init(&cx);
111 	u64 = htole64(off);
112 	MD5Update(&cx, (u_char *)&u64, sizeof(u64));
113 	u = off / sc->sectorsize;
114 	u %= sizeof sc->master_key;
115 	p = sc->master_key + u;
116 	if (u + 16 <= sizeof(sc->master_key)) {
117 		MD5Update(&cx, p, 16);
118 	} else {
119 		u1 = sizeof sc->master_key - u;
120 		MD5Update(&cx, p, u1);
121 		MD5Update(&cx, sc->master_key, 16 - u1);
122 		u1 = 0;				/* destroy evidence */
123 	}
124 	u = 0;					/* destroy evidence */
125 	MD5Update(&cx, (u_char *)&u64, sizeof(u64));
126 	u64 = 0;				/* destroy evidence */
127 	MD5Final(buf, &cx);
128 	bzero(&cx, sizeof cx);			/* destroy evidence */
129 	rijndael_makeKey(ki, dir, 128, buf);
130 	bzero(buf, sizeof buf);			/* destroy evidence */
131 
132 }
133 
134 static void
g_aes_read_done(struct bio * bp)135 g_aes_read_done(struct bio *bp)
136 {
137 	struct g_geom *gp;
138 	struct g_aes_softc *sc;
139 	u_char *p, *b, *e, *sb;
140 	keyInstance dkey;
141 	off_t o;
142 
143 	gp = bp->bio_from->geom;
144 	sc = gp->softc;
145 	sb = g_malloc(sc->sectorsize, M_WAITOK);
146 	b = bp->bio_data;
147 	e = bp->bio_data;
148 	e += bp->bio_length;
149 	o = bp->bio_offset - sc->sectorsize;
150 	for (p = b; p < e; p += sc->sectorsize) {
151 		g_aes_makekey(sc, o, &dkey, DIR_DECRYPT);
152 		rijndael_blockDecrypt(&sc->ci, &dkey, p, sc->sectorsize * 8, sb);
153 		bcopy(sb, p, sc->sectorsize);
154 		o += sc->sectorsize;
155 	}
156 	bzero(&dkey, sizeof dkey);		/* destroy evidence */
157 	bzero(sb, sc->sectorsize);		/* destroy evidence */
158 	g_free(sb);
159 	g_std_done(bp);
160 }
161 
162 static void
g_aes_write_done(struct bio * bp)163 g_aes_write_done(struct bio *bp)
164 {
165 
166 	bzero(bp->bio_data, bp->bio_length);	/* destroy evidence */
167 	g_free(bp->bio_data);
168 	g_std_done(bp);
169 }
170 
171 static void
g_aes_start(struct bio * bp)172 g_aes_start(struct bio *bp)
173 {
174 	struct g_geom *gp;
175 	struct g_consumer *cp;
176 	struct g_aes_softc *sc;
177 	struct bio *bp2;
178 	u_char *p1, *p2, *b, *e;
179 	keyInstance ekey;
180 	off_t o;
181 
182 	gp = bp->bio_to->geom;
183 	cp = LIST_FIRST(&gp->consumer);
184 	sc = gp->softc;
185 	switch (bp->bio_cmd) {
186 	case BIO_READ:
187 		bp2 = g_clone_bio(bp);
188 		if (bp2 == NULL) {
189 			g_io_deliver(bp, ENOMEM);
190 			return;
191 		}
192 		bp2->bio_done = g_aes_read_done;
193 		bp2->bio_offset += sc->sectorsize;
194 		g_io_request(bp2, cp);
195 		break;
196 	case BIO_WRITE:
197 		bp2 = g_clone_bio(bp);
198 		if (bp2 == NULL) {
199 			g_io_deliver(bp, ENOMEM);
200 			return;
201 		}
202 		bp2->bio_done = g_aes_write_done;
203 		bp2->bio_offset += sc->sectorsize;
204 		bp2->bio_data = g_malloc(bp->bio_length, M_WAITOK);
205 		b = bp->bio_data;
206 		e = bp->bio_data;
207 		e += bp->bio_length;
208 		p2 = bp2->bio_data;
209 		o = bp->bio_offset;
210 		for (p1 = b; p1 < e; p1 += sc->sectorsize) {
211 			g_aes_makekey(sc, o, &ekey, DIR_ENCRYPT);
212 			rijndael_blockEncrypt(&sc->ci, &ekey,
213 			    p1, sc->sectorsize * 8, p2);
214 			p2 += sc->sectorsize;
215 			o += sc->sectorsize;
216 		}
217 		bzero(&ekey, sizeof ekey);	/* destroy evidence */
218 		g_io_request(bp2, cp);
219 		break;
220 	case BIO_GETATTR:
221 		bp2 = g_clone_bio(bp);
222 		if (bp2 == NULL) {
223 			g_io_deliver(bp, ENOMEM);
224 			return;
225 		}
226 		bp2->bio_done = g_std_done;
227 		bp2->bio_offset += sc->sectorsize;
228 		g_io_request(bp2, cp);
229 		break;
230 	default:
231 		g_io_deliver(bp, EOPNOTSUPP);
232 		return;
233 	}
234 	return;
235 }
236 
237 static void
g_aes_orphan(struct g_consumer * cp)238 g_aes_orphan(struct g_consumer *cp)
239 {
240 	struct g_geom *gp;
241 	struct g_aes_softc *sc;
242 
243 	g_trace(G_T_TOPOLOGY, "g_aes_orphan(%p/%s)", cp, cp->provider->name);
244 	g_topology_assert();
245 
246 	gp = cp->geom;
247 	sc = gp->softc;
248 	g_wither_geom(gp, ENXIO);
249 	bzero(sc, sizeof(struct g_aes_softc));	/* destroy evidence */
250 	g_free(sc);
251 	return;
252 }
253 
254 static int
g_aes_access(struct g_provider * pp,int dr,int dw,int de)255 g_aes_access(struct g_provider *pp, int dr, int dw, int de)
256 {
257 	struct g_geom *gp;
258 	struct g_consumer *cp;
259 
260 	gp = pp->geom;
261 	cp = LIST_FIRST(&gp->consumer);
262 	/* On first open, grab an extra "exclusive" bit */
263 	if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0)
264 		de++;
265 	/* ... and let go of it on last close */
266 	if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1)
267 		de--;
268 	return (g_access(cp, dr, dw, de));
269 }
270 
271 static struct g_geom *
g_aes_taste(struct g_class * mp,struct g_provider * pp,int flags __unused)272 g_aes_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
273 {
274 	struct g_geom *gp;
275 	struct g_consumer *cp;
276 	struct g_aes_softc *sc;
277 	int error;
278 	u_int sectorsize;
279 	off_t mediasize;
280 	u_char *buf;
281 
282 	g_trace(G_T_TOPOLOGY, "aes_taste(%s,%s)", mp->name, pp->name);
283 	g_topology_assert();
284 	gp = g_new_geomf(mp, "%s.aes", pp->name);
285 	cp = g_new_consumer(gp);
286 	g_attach(cp, pp);
287 	error = g_access(cp, 1, 0, 0);
288 	if (error) {
289 		g_detach(cp);
290 		g_destroy_consumer(cp);
291 		g_destroy_geom(gp);
292 		return (NULL);
293 	}
294 	buf = NULL;
295 	g_topology_unlock();
296 	do {
297 		if (gp->rank != 2)
298 			break;
299 		sectorsize = cp->provider->sectorsize;
300 		mediasize = cp->provider->mediasize;
301 		buf = g_read_data(cp, 0, sectorsize, NULL);
302 		if (buf == NULL) {
303 			break;
304 		}
305 		sc = g_malloc(sizeof(struct g_aes_softc), M_WAITOK | M_ZERO);
306 		if (!memcmp(buf, aes_magic, strlen(aes_magic))) {
307 			sc->keying = KEY_ZERO;
308 		} else if (!memcmp(buf, aes_magic_random,
309 		    strlen(aes_magic_random))) {
310 			sc->keying = KEY_RANDOM;
311 		} else if (!memcmp(buf, aes_magic_test,
312 		    strlen(aes_magic_test))) {
313 			sc->keying = KEY_TEST;
314 		} else {
315 			g_free(sc);
316 			break;
317 		}
318 		g_free(buf);
319 		gp->softc = sc;
320 		sc->sectorsize = sectorsize;
321 		sc->mediasize = mediasize - sectorsize;
322 		rijndael_cipherInit(&sc->ci, MODE_CBC, NULL);
323 		if (sc->keying == KEY_TEST) {
324 			int i;
325 			u_char *p;
326 
327 			p = sc->master_key;
328 			for (i = 0; i < (int)sizeof sc->master_key; i ++)
329 				*p++ = i;
330 		}
331 		if (sc->keying == KEY_RANDOM) {
332 			int i;
333 			u_int32_t u;
334 			u_char *p;
335 
336 			p = sc->master_key;
337 			for (i = 0; i < (int)sizeof sc->master_key; i += sizeof u) {
338 				u = arc4random();
339 				*p++ = u;
340 				*p++ = u >> 8;
341 				*p++ = u >> 16;
342 				*p++ = u >> 24;
343 			}
344 		}
345 		g_topology_lock();
346 		pp = g_new_providerf(gp, "%s", gp->name);
347 		pp->mediasize = mediasize - sectorsize;
348 		pp->sectorsize = sectorsize;
349 		g_error_provider(pp, 0);
350 		g_topology_unlock();
351 	} while(0);
352 	g_topology_lock();
353 	if (buf)
354 		g_free(buf);
355 	g_access(cp, -1, 0, 0);
356 	if (gp->softc != NULL) {
357 		if (!g_aes_once) {
358 			g_aes_once = 1;
359 			printf("WARNING: geom_aes (geom %s) is deprecated.",
360 			    gp->name);
361 		}
362 		return (gp);
363 	}
364 	g_detach(cp);
365 	g_destroy_consumer(cp);
366 	g_destroy_geom(gp);
367 	return (NULL);
368 }
369 
370 static struct g_class g_aes_class	= {
371 	.name = AES_CLASS_NAME,
372 	.version = G_VERSION,
373 	.taste = g_aes_taste,
374 	.start = g_aes_start,
375 	.orphan = g_aes_orphan,
376 	.spoiled = g_std_spoiled,
377 	.access = g_aes_access,
378 };
379 
380 DECLARE_GEOM_CLASS(g_aes_class, g_aes);
381