geom_aes.c revision 143418
11556Srgrimes/*- 21556Srgrimes * Copyright (c) 2002 Poul-Henning Kamp 31556Srgrimes * Copyright (c) 2002 Networks Associates Technology, Inc. 41556Srgrimes * All rights reserved. 51556Srgrimes * 61556Srgrimes * This software was developed for the FreeBSD Project by Poul-Henning Kamp 71556Srgrimes * and NAI Labs, the Security Research Division of Network Associates, Inc. 81556Srgrimes * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the 91556Srgrimes * DARPA CHATS research program. 101556Srgrimes * 111556Srgrimes * Redistribution and use in source and binary forms, with or without 121556Srgrimes * modification, are permitted provided that the following conditions 131556Srgrimes * are met: 141556Srgrimes * 1. Redistributions of source code must retain the above copyright 151556Srgrimes * notice, this list of conditions and the following disclaimer. 161556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 171556Srgrimes * notice, this list of conditions and the following disclaimer in the 181556Srgrimes * documentation and/or other materials provided with the distribution. 191556Srgrimes * 3. The names of the authors may not be used to endorse or promote 201556Srgrimes * products derived from this software without specific prior written 211556Srgrimes * permission. 221556Srgrimes * 231556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 241556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 251556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 261556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 271556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 281556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 291556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 301556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 311556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 321556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 331556Srgrimes * SUCH DAMAGE. 341556Srgrimes */ 351556Srgrimes 361556Srgrimes/* 371556Srgrimes * This method provides AES encryption with a compiled in key (default 388855Srgrimes * all zeroes). 391556Srgrimes * 401556Srgrimes * XXX: This could probably save a lot of code by pretending to be a slicer. 411556Srgrimes */ 421556Srgrimes 431556Srgrimes#include <sys/cdefs.h> 441556Srgrimes__FBSDID("$FreeBSD: head/sys/geom/geom_aes.c 143418 2005-03-11 15:42:51Z ume $"); 451556Srgrimes 461556Srgrimes#include <sys/param.h> 471556Srgrimes#include <sys/systm.h> 481556Srgrimes#include <sys/kernel.h> 491556Srgrimes#include <sys/conf.h> 501556Srgrimes#include <sys/bio.h> 511556Srgrimes#include <sys/malloc.h> 521556Srgrimes#include <sys/lock.h> 531556Srgrimes#include <sys/mutex.h> 541556Srgrimes#include <sys/libkern.h> 551556Srgrimes#include <sys/endian.h> 561556Srgrimes#include <sys/md5.h> 571556Srgrimes#include <sys/errno.h> 581556Srgrimes#include <geom/geom.h> 591556Srgrimes 601556Srgrimes#include <crypto/rijndael/rijndael-api-fst.h> 611556Srgrimes 621556Srgrimes#define AES_CLASS_NAME "AES" 631556Srgrimes 641556Srgrimes#define MASTER_KEY_LENGTH (1024/8) 651556Srgrimes 668855Srgrimesstatic const u_char *aes_magic = "<<FreeBSD-GEOM-AES>>"; 671556Srgrimesstatic const u_char *aes_magic_random = "<<FreeBSD-GEOM-AES-RANDOM>>"; 681556Srgrimesstatic const u_char *aes_magic_test = "<<FreeBSD-GEOM-AES-TEST>>"; 691556Srgrimes 701556Srgrimes 711556Srgrimesstruct g_aes_softc { 721556Srgrimes enum { 731556Srgrimes KEY_ZERO, 741556Srgrimes KEY_RANDOM, 751556Srgrimes KEY_TEST 761556Srgrimes } keying; 771556Srgrimes u_int sectorsize; 781556Srgrimes off_t mediasize; 798855Srgrimes cipherInstance ci; 801556Srgrimes u_char master_key[MASTER_KEY_LENGTH]; 811556Srgrimes}; 821556Srgrimes 831556Srgrimes/* 841556Srgrimes * Generate a sectorkey from the masterkey and the offset position. 851556Srgrimes * 861556Srgrimes * For KEY_ZERO we just return a key of all zeros. 871556Srgrimes * 881556Srgrimes * We feed the sector byte offset, 16 bytes of the master-key and 891556Srgrimes * the sector byte offset once more to MD5. 901556Srgrimes * The sector byte offset is converted to little-endian format first 911556Srgrimes * to support multi-architecture operation. 921556Srgrimes * We use 16 bytes from the master-key starting at the logical sector 931556Srgrimes * number modulus he length of the master-key. If need be we wrap 941556Srgrimes * around to the start of the master-key. 951556Srgrimes */ 961556Srgrimes 971556Srgrimesstatic void 981556Srgrimesg_aes_makekey(struct g_aes_softc *sc, off_t off, keyInstance *ki, int dir) 991556Srgrimes{ 1001556Srgrimes MD5_CTX cx; 1011556Srgrimes u_int64_t u64; 1021556Srgrimes u_int u, u1; 1031556Srgrimes u_char *p, buf[16]; 1041556Srgrimes 1051556Srgrimes if (sc->keying == KEY_ZERO) { 1061556Srgrimes rijndael_makeKey(ki, dir, 128, sc->master_key); 1071556Srgrimes return; 1081556Srgrimes } 1091556Srgrimes MD5Init(&cx); 1101556Srgrimes u64 = htole64(off); 1111556Srgrimes MD5Update(&cx, (u_char *)&u64, sizeof(u64)); 1121556Srgrimes u = off / sc->sectorsize; 1131556Srgrimes u %= sizeof sc->master_key; 1141556Srgrimes p = sc->master_key + u; 1151556Srgrimes if (u + 16 <= sizeof(sc->master_key)) { 1161556Srgrimes MD5Update(&cx, p, 16); 1171556Srgrimes } else { 1181556Srgrimes u1 = sizeof sc->master_key - u; 1191556Srgrimes MD5Update(&cx, p, u1); 1201556Srgrimes MD5Update(&cx, sc->master_key, 16 - u1); 1211556Srgrimes u1 = 0; /* destroy evidence */ 1221556Srgrimes } 1231556Srgrimes u = 0; /* destroy evidence */ 1241556Srgrimes MD5Update(&cx, (u_char *)&u64, sizeof(u64)); 1251556Srgrimes u64 = 0; /* destroy evidence */ 1261556Srgrimes MD5Final(buf, &cx); 1271556Srgrimes bzero(&cx, sizeof cx); /* destroy evidence */ 1281556Srgrimes rijndael_makeKey(ki, dir, 128, buf); 1291556Srgrimes bzero(buf, sizeof buf); /* destroy evidence */ 1301556Srgrimes 1311556Srgrimes} 1321556Srgrimes 1331556Srgrimesstatic void 1341556Srgrimesg_aes_read_done(struct bio *bp) 1351556Srgrimes{ 1361556Srgrimes struct g_geom *gp; 1371556Srgrimes struct g_aes_softc *sc; 1381556Srgrimes u_char *p, *b, *e, *sb; 1391556Srgrimes keyInstance dkey; 1401556Srgrimes off_t o; 1411556Srgrimes 1421556Srgrimes gp = bp->bio_from->geom; 1431556Srgrimes sc = gp->softc; 1441556Srgrimes sb = g_malloc(sc->sectorsize, M_WAITOK); 1451556Srgrimes b = bp->bio_data; 1461556Srgrimes e = bp->bio_data; 1471556Srgrimes e += bp->bio_length; 1481556Srgrimes o = bp->bio_offset - sc->sectorsize; 1491556Srgrimes for (p = b; p < e; p += sc->sectorsize) { 1501556Srgrimes g_aes_makekey(sc, o, &dkey, DIR_DECRYPT); 1511556Srgrimes rijndael_blockDecrypt(&sc->ci, &dkey, p, sc->sectorsize * 8, sb); 1521556Srgrimes bcopy(sb, p, sc->sectorsize); 1531556Srgrimes o += sc->sectorsize; 1541556Srgrimes } 1551556Srgrimes bzero(&dkey, sizeof dkey); /* destroy evidence */ 1561556Srgrimes bzero(sb, sc->sectorsize); /* destroy evidence */ 1571556Srgrimes g_free(sb); 1581556Srgrimes g_std_done(bp); 1591556Srgrimes} 1601556Srgrimes 1611556Srgrimesstatic void 1621556Srgrimesg_aes_write_done(struct bio *bp) 1631556Srgrimes{ 1641556Srgrimes 1651556Srgrimes bzero(bp->bio_data, bp->bio_length); /* destroy evidence */ 1661556Srgrimes g_free(bp->bio_data); 1671556Srgrimes g_std_done(bp); 1681556Srgrimes} 1691556Srgrimes 1701556Srgrimesstatic void 1711556Srgrimesg_aes_start(struct bio *bp) 1721556Srgrimes{ 1731556Srgrimes struct g_geom *gp; 1741556Srgrimes struct g_consumer *cp; 1751556Srgrimes struct g_aes_softc *sc; 1761556Srgrimes struct bio *bp2; 1771556Srgrimes u_char *p1, *p2, *b, *e; 1781556Srgrimes keyInstance ekey; 1791556Srgrimes off_t o; 1801556Srgrimes 1811556Srgrimes gp = bp->bio_to->geom; 1821556Srgrimes cp = LIST_FIRST(&gp->consumer); 1831556Srgrimes sc = gp->softc; 1841556Srgrimes switch (bp->bio_cmd) { 1851556Srgrimes case BIO_READ: 1861556Srgrimes bp2 = g_clone_bio(bp); 1871556Srgrimes if (bp2 == NULL) { 1881556Srgrimes g_io_deliver(bp, ENOMEM); 1891556Srgrimes return; 1901556Srgrimes } 1911556Srgrimes bp2->bio_done = g_aes_read_done; 1921556Srgrimes bp2->bio_offset += sc->sectorsize; 1931556Srgrimes g_io_request(bp2, cp); 1941556Srgrimes break; 1951556Srgrimes case BIO_WRITE: 1961556Srgrimes bp2 = g_clone_bio(bp); 1971556Srgrimes if (bp2 == NULL) { 1981556Srgrimes g_io_deliver(bp, ENOMEM); 1991556Srgrimes return; 2001556Srgrimes } 2011556Srgrimes bp2->bio_done = g_aes_write_done; 2021556Srgrimes bp2->bio_offset += sc->sectorsize; 2031556Srgrimes bp2->bio_data = g_malloc(bp->bio_length, M_WAITOK); 2041556Srgrimes b = bp->bio_data; 2051556Srgrimes e = bp->bio_data; 2061556Srgrimes e += bp->bio_length; 2071556Srgrimes p2 = bp2->bio_data; 2081556Srgrimes o = bp->bio_offset; 2091556Srgrimes for (p1 = b; p1 < e; p1 += sc->sectorsize) { 2108855Srgrimes g_aes_makekey(sc, o, &ekey, DIR_ENCRYPT); 2111556Srgrimes rijndael_blockEncrypt(&sc->ci, &ekey, 2121556Srgrimes p1, sc->sectorsize * 8, p2); 2131556Srgrimes p2 += sc->sectorsize; 2141556Srgrimes o += sc->sectorsize; 2151556Srgrimes } 2161556Srgrimes bzero(&ekey, sizeof ekey); /* destroy evidence */ 2171556Srgrimes g_io_request(bp2, cp); 2181556Srgrimes break; 2191556Srgrimes case BIO_GETATTR: 2201556Srgrimes bp2 = g_clone_bio(bp); 2211556Srgrimes if (bp2 == NULL) { 2221556Srgrimes g_io_deliver(bp, ENOMEM); 2231556Srgrimes return; 2241556Srgrimes } 2251556Srgrimes bp2->bio_done = g_std_done; 2261556Srgrimes bp2->bio_offset += sc->sectorsize; 2271556Srgrimes g_io_request(bp2, cp); 2281556Srgrimes break; 2291556Srgrimes default: 2301556Srgrimes g_io_deliver(bp, EOPNOTSUPP); 2311556Srgrimes return; 2321556Srgrimes } 2331556Srgrimes return; 2341556Srgrimes} 2351556Srgrimes 2361556Srgrimesstatic void 2371556Srgrimesg_aes_orphan(struct g_consumer *cp) 2381556Srgrimes{ 2391556Srgrimes struct g_geom *gp; 240 struct g_aes_softc *sc; 241 242 g_trace(G_T_TOPOLOGY, "g_aes_orphan(%p/%s)", cp, cp->provider->name); 243 g_topology_assert(); 244 KASSERT(cp->provider->error != 0, 245 ("g_aes_orphan with error == 0")); 246 247 gp = cp->geom; 248 sc = gp->softc; 249 g_wither_geom(gp, cp->provider->error); 250 bzero(sc, sizeof(struct g_aes_softc)); /* destroy evidence */ 251 g_free(sc); 252 return; 253} 254 255static int 256g_aes_access(struct g_provider *pp, int dr, int dw, int de) 257{ 258 struct g_geom *gp; 259 struct g_consumer *cp; 260 261 gp = pp->geom; 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) 265 de++; 266 /* ... and let go of it on last close */ 267 if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1) 268 de--; 269 return (g_access(cp, dr, dw, de)); 270} 271 272static struct g_geom * 273g_aes_taste(struct g_class *mp, struct g_provider *pp, int flags __unused) 274{ 275 struct g_geom *gp; 276 struct g_consumer *cp; 277 struct g_aes_softc *sc; 278 int error; 279 u_int sectorsize; 280 off_t mediasize; 281 u_char *buf; 282 283 g_trace(G_T_TOPOLOGY, "aes_taste(%s,%s)", mp->name, pp->name); 284 g_topology_assert(); 285 gp = g_new_geomf(mp, "%s.aes", pp->name); 286 cp = g_new_consumer(gp); 287 g_attach(cp, pp); 288 error = g_access(cp, 1, 0, 0); 289 if (error) { 290 g_detach(cp); 291 g_destroy_consumer(cp); 292 g_destroy_geom(gp); 293 return (NULL); 294 } 295 buf = NULL; 296 g_topology_unlock(); 297 do { 298 if (gp->rank != 2) 299 break; 300 sectorsize = cp->provider->sectorsize; 301 mediasize = cp->provider->mediasize; 302 buf = g_read_data(cp, 0, sectorsize, &error); 303 if (buf == NULL || error != 0) { 304 break; 305 } 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; 315 } else { 316 g_free(sc); 317 break; 318 } 319 g_free(buf); 320 gp->softc = sc; 321 sc->sectorsize = sectorsize; 322 sc->mediasize = mediasize - sectorsize; 323 rijndael_cipherInit(&sc->ci, MODE_CBC, NULL); 324 if (sc->keying == KEY_TEST) { 325 int i; 326 u_char *p; 327 328 p = sc->master_key; 329 for (i = 0; i < (int)sizeof sc->master_key; i ++) 330 *p++ = i; 331 } 332 if (sc->keying == KEY_RANDOM) { 333 int i; 334 u_int32_t u; 335 u_char *p; 336 337 p = sc->master_key; 338 for (i = 0; i < (int)sizeof sc->master_key; i += sizeof u) { 339 u = arc4random(); 340 *p++ = u; 341 *p++ = u >> 8; 342 *p++ = u >> 16; 343 *p++ = u >> 24; 344 } 345 } 346 g_topology_lock(); 347 pp = g_new_providerf(gp, gp->name); 348 pp->mediasize = mediasize - sectorsize; 349 pp->sectorsize = sectorsize; 350 g_error_provider(pp, 0); 351 g_topology_unlock(); 352 } while(0); 353 g_topology_lock(); 354 if (buf) 355 g_free(buf); 356 g_access(cp, -1, 0, 0); 357 if (gp->softc != NULL) 358 return (gp); 359 g_detach(cp); 360 g_destroy_consumer(cp); 361 g_destroy_geom(gp); 362 return (NULL); 363} 364 365static 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, 373}; 374 375DECLARE_GEOM_CLASS(g_aes_class, g_aes); 376