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