g_bde_crypt.c revision 114167
1105464Sphk/*- 2105464Sphk * Copyright (c) 2002 Poul-Henning Kamp 3105464Sphk * Copyright (c) 2002 Networks Associates Technology, Inc. 4105464Sphk * All rights reserved. 5105464Sphk * 6105464Sphk * This software was developed for the FreeBSD Project by Poul-Henning Kamp 7105464Sphk * and NAI Labs, the Security Research Division of Network Associates, Inc. 8105464Sphk * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the 9105464Sphk * DARPA CHATS research program. 10105464Sphk * 11105464Sphk * Redistribution and use in source and binary forms, with or without 12105464Sphk * modification, are permitted provided that the following conditions 13105464Sphk * are met: 14105464Sphk * 1. Redistributions of source code must retain the above copyright 15105464Sphk * notice, this list of conditions and the following disclaimer. 16105464Sphk * 2. Redistributions in binary form must reproduce the above copyright 17105464Sphk * notice, this list of conditions and the following disclaimer in the 18105464Sphk * documentation and/or other materials provided with the distribution. 19105464Sphk * 20105464Sphk * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 21105464Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22105464Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23105464Sphk * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24105464Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25105464Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26105464Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27105464Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28105464Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29105464Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30105464Sphk * SUCH DAMAGE. 31105464Sphk * 32105464Sphk * $FreeBSD: head/sys/geom/bde/g_bde_crypt.c 114167 2003-04-28 17:57:12Z phk $ 33105464Sphk * 34105464Sphk * This source file contains the functions responsible for the crypto, keying 35105464Sphk * and mapping operations on the I/O requests. 36105464Sphk * 37105464Sphk */ 38105464Sphk 39105464Sphk#include <sys/param.h> 40105464Sphk#include <sys/bio.h> 41105464Sphk#include <sys/lock.h> 42105464Sphk#include <sys/mutex.h> 43105464Sphk#include <sys/queue.h> 44105464Sphk#include <sys/malloc.h> 45105464Sphk#include <sys/libkern.h> 46113010Sphk#include <sys/endian.h> 47105464Sphk#include <sys/md5.h> 48105464Sphk 49106407Sphk#include <crypto/rijndael/rijndael.h> 50106407Sphk#include <crypto/sha2/sha2.h> 51106407Sphk 52105464Sphk#include <geom/geom.h> 53105464Sphk#include <geom/bde/g_bde.h> 54105464Sphk 55114167Sphk/* 56114167Sphk * XXX: Debugging DO NOT ENABLE 57114167Sphk */ 58114167Sphk#undef MD5_KEY 59105464Sphk 60105464Sphk/* 61105464Sphk * Derive kkey from mkey + sector offset. 62105464Sphk * 63105464Sphk * Security objective: Derive a potentially very large number of distinct skeys 64105464Sphk * from the comparatively small key material in our mkey, in such a way that 65105464Sphk * if one, more or even many of the kkeys are compromised, this does not 66105464Sphk * significantly help an attack on other kkeys and in particular does not 67105464Sphk * weaken or compromised the mkey. 68105464Sphk * 69106226Sphk * First we MD5 hash the sectornumber with the salt from the lock sector. 70106226Sphk * The salt prevents the precalculation and statistical analysis of the MD5 71106226Sphk * output which would be possible if we only gave it the sectornumber. 72105464Sphk * 73106226Sphk * The MD5 hash is used to pick out 16 bytes from the masterkey, which 74106226Sphk * are then hashed with MD5 together with the sector number. 75105464Sphk * 76106226Sphk * The resulting MD5 hash is the kkey. 77105464Sphk */ 78105464Sphk 79105464Sphkstatic void 80105464Sphkg_bde_kkey(struct g_bde_softc *sc, keyInstance *ki, int dir, off_t sector) 81105464Sphk{ 82106226Sphk u_int t; 83105464Sphk MD5_CTX ct; 84106226Sphk u_char buf[16]; 85106407Sphk u_char buf2[8]; 86105464Sphk 87106407Sphk /* We have to be architecture neutral */ 88113010Sphk le64enc(buf2, sector); 89106407Sphk 90105464Sphk MD5Init(&ct); 91106226Sphk MD5Update(&ct, sc->key.salt, 8); 92106407Sphk MD5Update(&ct, buf2, sizeof buf2); 93106226Sphk MD5Update(&ct, sc->key.salt + 8, 8); 94106226Sphk MD5Final(buf, &ct); 95106226Sphk 96106226Sphk MD5Init(&ct); 97106226Sphk for (t = 0; t < 16; t++) { 98106226Sphk MD5Update(&ct, &sc->key.mkey[buf[t]], 1); 99106226Sphk if (t == 8) 100106407Sphk MD5Update(&ct, buf2, sizeof buf2); 101105464Sphk } 102106407Sphk bzero(buf2, sizeof buf2); 103105464Sphk MD5Final(buf, &ct); 104105464Sphk bzero(&ct, sizeof ct); 105105464Sphk AES_makekey(ki, dir, G_BDE_KKEYBITS, buf); 106105464Sphk bzero(buf, sizeof buf); 107105464Sphk} 108105464Sphk 109105464Sphk/* 110105464Sphk * Encryption work for read operation. 111105464Sphk * 112105464Sphk * Security objective: Find the kkey, find the skey, decrypt the sector data. 113105464Sphk */ 114105464Sphk 115105464Sphkvoid 116105464Sphkg_bde_crypt_read(struct g_bde_work *wp) 117105464Sphk{ 118105464Sphk struct g_bde_softc *sc; 119105464Sphk u_char *d; 120105464Sphk u_int n; 121105464Sphk off_t o; 122105464Sphk u_char skey[G_BDE_SKEYLEN]; 123105464Sphk keyInstance ki; 124105464Sphk cipherInstance ci; 125105464Sphk 126105464Sphk 127105464Sphk AES_init(&ci); 128105464Sphk sc = wp->softc; 129105464Sphk o = 0; 130105464Sphk for (n = 0; o < wp->length; n++, o += sc->sectorsize) { 131105464Sphk d = (u_char *)wp->ksp->data + wp->ko + n * G_BDE_SKEYLEN; 132105464Sphk g_bde_kkey(sc, &ki, DIR_DECRYPT, wp->offset + o); 133105464Sphk AES_decrypt(&ci, &ki, d, skey, sizeof skey); 134105464Sphk d = (u_char *)wp->data + o; 135114153Sphk#ifdef MD5_KEY 136114153Sphk { 137114153Sphk MD5_CTX ct; 138114153Sphk u_char rkey[16]; 139114153Sphk int i; 140114153Sphk 141114153Sphk MD5Init(&ct); 142114153Sphk MD5Update(&ct, d, sc->sectorsize); 143114153Sphk MD5Final(rkey, &ct); 144114153Sphk if (bcmp(rkey, skey, 16) != 0) { 145114153Sphk#if 0 146114153Sphk printf("MD5_KEY failed at %jd (t=%d)\n", 147114153Sphk (intmax_t)(wp->offset + o), time_second); 148114153Sphk#endif 149114153Sphk for (i = 0; i < sc->sectorsize; i++) 150114153Sphk d[i] = 'A' + i % 26; 151114153Sphk sprintf(d, "MD5_KEY failed at %jd (t=%d)", 152114153Sphk (intmax_t)(wp->offset + o), time_second); 153114153Sphk } 154114153Sphk } 155114153Sphk#else 156105464Sphk AES_makekey(&ki, DIR_DECRYPT, G_BDE_SKEYBITS, skey); 157105464Sphk AES_decrypt(&ci, &ki, d, d, sc->sectorsize); 158114153Sphk#endif 159105464Sphk } 160105464Sphk bzero(skey, sizeof skey); 161105464Sphk bzero(&ci, sizeof ci); 162105464Sphk bzero(&ki, sizeof ci); 163105464Sphk} 164105464Sphk 165105464Sphk/* 166105464Sphk * Encryption work for write operation. 167105464Sphk * 168105464Sphk * Security objective: Create random skey, encrypt sector data, 169105464Sphk * encrypt skey with the kkey. 170105464Sphk */ 171105464Sphk 172105464Sphkvoid 173105464Sphkg_bde_crypt_write(struct g_bde_work *wp) 174105464Sphk{ 175105464Sphk u_char *s, *d; 176105464Sphk struct g_bde_softc *sc; 177105464Sphk u_int n; 178105464Sphk off_t o; 179105464Sphk u_char skey[G_BDE_SKEYLEN]; 180105464Sphk keyInstance ki; 181105464Sphk cipherInstance ci; 182105464Sphk 183105464Sphk sc = wp->softc; 184105464Sphk AES_init(&ci); 185105464Sphk o = 0; 186105464Sphk for (n = 0; o < wp->length; n++, o += sc->sectorsize) { 187105464Sphk 188105464Sphk s = (u_char *)wp->data + o; 189105464Sphk d = (u_char *)wp->sp->data + o; 190114153Sphk#ifdef MD5_KEY 191114153Sphk { 192114153Sphk MD5_CTX ct; 193114153Sphk 194114153Sphk MD5Init(&ct); 195114153Sphk MD5Update(&ct, s, sc->sectorsize); 196114153Sphk MD5Final(skey, &ct); 197114153Sphk bcopy(s, d, sc->sectorsize); 198114153Sphk } 199114153Sphk#else 200105464Sphk arc4rand(&skey, sizeof skey, 0); 201105464Sphk AES_makekey(&ki, DIR_ENCRYPT, G_BDE_SKEYBITS, skey); 202105464Sphk AES_encrypt(&ci, &ki, s, d, sc->sectorsize); 203114153Sphk#endif 204105464Sphk 205105464Sphk d = (u_char *)wp->ksp->data + wp->ko + n * G_BDE_SKEYLEN; 206105464Sphk g_bde_kkey(sc, &ki, DIR_ENCRYPT, wp->offset + o); 207105464Sphk AES_encrypt(&ci, &ki, skey, d, sizeof skey); 208105464Sphk bzero(skey, sizeof skey); 209105464Sphk } 210105464Sphk bzero(skey, sizeof skey); 211105464Sphk bzero(&ci, sizeof ci); 212105464Sphk bzero(&ki, sizeof ci); 213105464Sphk} 214105464Sphk 215105464Sphk/* 216105464Sphk * Encryption work for delete operation. 217105464Sphk * 218105464Sphk * Security objective: Write random data to the sectors. 219105464Sphk * 220105464Sphk * XXX: At a hit in performance we would trash the encrypted skey as well. 221105464Sphk * XXX: This would add frustration to the cleaning lady attack by making 222105464Sphk * XXX: deletes look like writes. 223105464Sphk */ 224105464Sphk 225105464Sphkvoid 226105464Sphkg_bde_crypt_delete(struct g_bde_work *wp) 227105464Sphk{ 228105464Sphk struct g_bde_softc *sc; 229105464Sphk u_char *d; 230105464Sphk off_t o; 231107451Sphk u_char skey[G_BDE_SKEYLEN]; 232107451Sphk keyInstance ki; 233107451Sphk cipherInstance ci; 234105464Sphk 235105464Sphk sc = wp->softc; 236105464Sphk d = wp->sp->data; 237107451Sphk AES_init(&ci); 238105464Sphk /* 239105464Sphk * Do not unroll this loop! 240105464Sphk * Our zone may be significantly wider than the amount of random 241105464Sphk * bytes arc4rand likes to give in one reseeding, whereas our 242105464Sphk * sectorsize is far more likely to be in the same range. 243105464Sphk */ 244105464Sphk for (o = 0; o < wp->length; o += sc->sectorsize) { 245105464Sphk arc4rand(d, sc->sectorsize, 0); 246107451Sphk arc4rand(&skey, sizeof skey, 0); 247107451Sphk AES_makekey(&ki, DIR_ENCRYPT, G_BDE_SKEYBITS, skey); 248107451Sphk AES_encrypt(&ci, &ki, d, d, sc->sectorsize); 249105464Sphk d += sc->sectorsize; 250105464Sphk } 251105464Sphk /* 252105464Sphk * Having written a long random sequence to disk here, we want to 253105464Sphk * force a reseed, to avoid weakening the next time we use random 254105464Sphk * data for something important. 255105464Sphk */ 256105464Sphk arc4rand(&o, sizeof o, 1); 257105464Sphk} 258105464Sphk 259105464Sphk/* 260105464Sphk * Calculate the total payload size of the encrypted device. 261105464Sphk * 262105464Sphk * Security objectives: none. 263105464Sphk * 264105464Sphk * This function needs to agree with g_bde_map_sector() about things. 265105464Sphk */ 266105464Sphk 267105464Sphkuint64_t 268105464Sphkg_bde_max_sector(struct g_bde_key *kp) 269105464Sphk{ 270105464Sphk uint64_t maxsect; 271105464Sphk 272105464Sphk maxsect = kp->media_width; 273105464Sphk maxsect /= kp->zone_width; 274105464Sphk maxsect *= kp->zone_cont; 275105464Sphk return (maxsect); 276105464Sphk} 277105464Sphk 278105464Sphk/* 279105464Sphk * Convert an unencrypted side offset to offsets on the encrypted side. 280105464Sphk * 281105464Sphk * Security objective: Make it harder to identify what sectors contain what 282105464Sphk * on a "cold" disk image. 283105464Sphk * 284105464Sphk * We do this by adding the "keyoffset" from the lock to the physical sector 285108558Sphk * number modulus the available number of sectors. Since all physical sectors 286108558Sphk * presumably look the same cold, this will do. 287105464Sphk * 288108558Sphk * As part of the mapping we have to skip the lock sectors which we know 289108558Sphk * the physical address off. We also truncate the work packet, respecting 290108558Sphk * zone boundaries and lock sectors, so that we end up with a sequence of 291108558Sphk * sectors which are physically contiguous. 292108558Sphk * 293105464Sphk * Shuffling things further is an option, but the incremental frustration is 294105464Sphk * not currently deemed worth the run-time performance hit resulting from the 295105464Sphk * increased number of disk arm movements it would incur. 296105464Sphk * 297105464Sphk * This function offers nothing but a trivial diversion for an attacker able 298105464Sphk * to do "the cleaning lady attack" in its current static mapping form. 299105464Sphk */ 300105464Sphk 301105464Sphkvoid 302108558Sphkg_bde_map_sector(struct g_bde_work *wp) 303105464Sphk{ 304105464Sphk 305108558Sphk u_int zone, zoff, u, len; 306108558Sphk uint64_t ko; 307108558Sphk struct g_bde_softc *sc; 308108558Sphk struct g_bde_key *kp; 309105464Sphk 310108558Sphk sc = wp->softc; 311108558Sphk kp = &sc->key; 312105464Sphk 313108558Sphk /* find which zone and the offset in it */ 314108558Sphk zone = wp->offset / kp->zone_cont; 315108558Sphk zoff = wp->offset % kp->zone_cont; 316105464Sphk 317108558Sphk /* Calculate the offset of the key in the key sector */ 318108558Sphk wp->ko = (zoff / kp->sectorsize) * G_BDE_SKEYLEN; 319105464Sphk 320108558Sphk /* restrict length to that zone */ 321108558Sphk len = kp->zone_cont - zoff; 322111964Sphk 323111964Sphk /* ... and in general */ 324111964Sphk if (len > DFLTPHYS) 325111964Sphk len = DFLTPHYS; 326111964Sphk 327108558Sphk if (len < wp->length) 328108558Sphk wp->length = len; 329105464Sphk 330108558Sphk /* Find physical sector address */ 331108558Sphk wp->so = zone * kp->zone_width + zoff; 332108558Sphk wp->so += kp->keyoffset; 333108558Sphk wp->so %= kp->media_width; 334108558Sphk wp->so += kp->sector0; 335108558Sphk 336105464Sphk /* The key sector is the last in this zone. */ 337108558Sphk wp->kso = zone * kp->zone_width + kp->zone_cont; 338108558Sphk wp->kso += kp->keyoffset; 339108558Sphk wp->kso %= kp->media_width; 340108558Sphk wp->kso += kp->sector0; 341105464Sphk 342108558Sphk /* Compensate for lock sectors */ 343108558Sphk for (u = 0; u < G_BDE_MAXKEYS; u++) { 344108558Sphk /* Find the start of this lock sector */ 345108558Sphk ko = kp->lsector[u] & ~(kp->sectorsize - 1); 346105464Sphk 347108558Sphk if (wp->kso >= ko) 348108558Sphk wp->kso += kp->sectorsize; 349105464Sphk 350108558Sphk if (wp->so >= ko) { 351108558Sphk /* lock sector before work packet */ 352108558Sphk wp->so += kp->sectorsize; 353108558Sphk } else if ((wp->so + wp->length) > ko) { 354108558Sphk /* lock sector in work packet, truncate */ 355108558Sphk wp->length = ko - wp->so; 356108558Sphk } 357108558Sphk } 358108558Sphk 359105464Sphk#if 0 360108558Sphk printf("off %jd len %jd so %jd ko %jd kso %u\n", 361108558Sphk (intmax_t)wp->offset, 362108558Sphk (intmax_t)wp->length, 363108558Sphk (intmax_t)wp->so, 364108558Sphk (intmax_t)wp->kso, 365108558Sphk wp->ko); 366105464Sphk#endif 367105464Sphk} 368