The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/geom/bde/g_bde_crypt.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    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  *
   20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   30  * SUCH DAMAGE.
   31  *
   32  * $FreeBSD: releng/5.0/sys/geom/bde/g_bde_crypt.c 108891 2003-01-07 18:21:39Z phk $
   33  *
   34  * This source file contains the functions responsible for the crypto, keying
   35  * and mapping operations on the I/O requests.
   36  *
   37  */
   38 
   39 #include <sys/param.h>
   40 #include <sys/stdint.h>
   41 #include <sys/bio.h>
   42 #include <sys/lock.h>
   43 #include <sys/mutex.h>
   44 #include <sys/queue.h>
   45 #include <sys/malloc.h>
   46 #include <sys/libkern.h>
   47 #include <sys/md5.h>
   48 
   49 #include <crypto/rijndael/rijndael.h>
   50 #include <crypto/sha2/sha2.h>
   51 
   52 #include <geom/geom.h>
   53 #include <geom/bde/g_bde.h>
   54 
   55 
   56 /*
   57  * Derive kkey from mkey + sector offset.
   58  *
   59  * Security objective: Derive a potentially very large number of distinct skeys
   60  * from the comparatively small key material in our mkey, in such a way that
   61  * if one, more or even many of the kkeys are compromised, this does not
   62  * significantly help an attack on other kkeys and in particular does not
   63  * weaken or compromised the mkey.
   64  *
   65  * First we MD5 hash the sectornumber with the salt from the lock sector.
   66  * The salt prevents the precalculation and statistical analysis of the MD5
   67  * output which would be possible if we only gave it the sectornumber.
   68  *
   69  * The MD5 hash is used to pick out 16 bytes from the masterkey, which
   70  * are then hashed with MD5 together with the sector number.
   71  *
   72  * The resulting MD5 hash is the kkey.
   73  */
   74 
   75 static void
   76 g_bde_kkey(struct g_bde_softc *sc, keyInstance *ki, int dir, off_t sector)
   77 {
   78         u_int t;
   79         MD5_CTX ct;
   80         u_char buf[16];
   81         u_char buf2[8];
   82 
   83         /* We have to be architecture neutral */
   84         g_enc_le8(buf2, sector);
   85 
   86         MD5Init(&ct);
   87         MD5Update(&ct, sc->key.salt, 8);
   88         MD5Update(&ct, buf2, sizeof buf2);
   89         MD5Update(&ct, sc->key.salt + 8, 8);
   90         MD5Final(buf, &ct);
   91 
   92         MD5Init(&ct);
   93         for (t = 0; t < 16; t++) {
   94                 MD5Update(&ct, &sc->key.mkey[buf[t]], 1);
   95                 if (t == 8)
   96                         MD5Update(&ct, buf2, sizeof buf2);
   97         }
   98         bzero(buf2, sizeof buf2);
   99         MD5Final(buf, &ct);
  100         bzero(&ct, sizeof ct);
  101         AES_makekey(ki, dir, G_BDE_KKEYBITS, buf);
  102         bzero(buf, sizeof buf);
  103 }
  104 
  105 /*
  106  * Encryption work for read operation.
  107  *
  108  * Security objective: Find the kkey, find the skey, decrypt the sector data.
  109  */
  110 
  111 void
  112 g_bde_crypt_read(struct g_bde_work *wp)
  113 {
  114         struct g_bde_softc *sc;
  115         u_char *d;
  116         u_int n;
  117         off_t o;
  118         u_char skey[G_BDE_SKEYLEN];
  119         keyInstance ki;
  120         cipherInstance ci;
  121         
  122 
  123         AES_init(&ci);
  124         sc = wp->softc;
  125         o = 0;
  126         for (n = 0; o < wp->length; n++, o += sc->sectorsize) {
  127                 d = (u_char *)wp->ksp->data + wp->ko + n * G_BDE_SKEYLEN;
  128                 g_bde_kkey(sc, &ki, DIR_DECRYPT, wp->offset + o);
  129                 AES_decrypt(&ci, &ki, d, skey, sizeof skey);
  130                 d = (u_char *)wp->data + o;
  131                 AES_makekey(&ki, DIR_DECRYPT, G_BDE_SKEYBITS, skey);
  132                 AES_decrypt(&ci, &ki, d, d, sc->sectorsize);
  133         }
  134         bzero(skey, sizeof skey);
  135         bzero(&ci, sizeof ci);
  136         bzero(&ki, sizeof ci);
  137 }
  138 
  139 /*
  140  * Encryption work for write operation.
  141  *
  142  * Security objective: Create random skey, encrypt sector data,
  143  * encrypt skey with the kkey.
  144  */
  145 
  146 void
  147 g_bde_crypt_write(struct g_bde_work *wp)
  148 {
  149         u_char *s, *d;
  150         struct g_bde_softc *sc;
  151         u_int n;
  152         off_t o;
  153         u_char skey[G_BDE_SKEYLEN];
  154         keyInstance ki;
  155         cipherInstance ci;
  156 
  157         sc = wp->softc;
  158         AES_init(&ci);
  159         o = 0;
  160         for (n = 0; o < wp->length; n++, o += sc->sectorsize) {
  161 
  162                 s = (u_char *)wp->data + o;
  163                 d = (u_char *)wp->sp->data + o;
  164                 arc4rand(&skey, sizeof skey, 0);
  165                 AES_makekey(&ki, DIR_ENCRYPT, G_BDE_SKEYBITS, skey);
  166                 AES_encrypt(&ci, &ki, s, d, sc->sectorsize);
  167 
  168                 d = (u_char *)wp->ksp->data + wp->ko + n * G_BDE_SKEYLEN;
  169                 g_bde_kkey(sc, &ki, DIR_ENCRYPT, wp->offset + o);
  170                 AES_encrypt(&ci, &ki, skey, d, sizeof skey);
  171                 bzero(skey, sizeof skey);
  172         }
  173         bzero(skey, sizeof skey);
  174         bzero(&ci, sizeof ci);
  175         bzero(&ki, sizeof ci);
  176 }
  177 
  178 /*
  179  * Encryption work for delete operation.
  180  *
  181  * Security objective: Write random data to the sectors.
  182  *
  183  * XXX: At a hit in performance we would trash the encrypted skey as well.
  184  * XXX: This would add frustration to the cleaning lady attack by making
  185  * XXX: deletes look like writes.
  186  */
  187 
  188 void
  189 g_bde_crypt_delete(struct g_bde_work *wp)
  190 {
  191         struct g_bde_softc *sc;
  192         u_char *d;
  193         off_t o;
  194         u_char skey[G_BDE_SKEYLEN];
  195         keyInstance ki;
  196         cipherInstance ci;
  197 
  198         sc = wp->softc;
  199         d = wp->sp->data;
  200         AES_init(&ci);
  201         /*
  202          * Do not unroll this loop!
  203          * Our zone may be significantly wider than the amount of random
  204          * bytes arc4rand likes to give in one reseeding, whereas our
  205          * sectorsize is far more likely to be in the same range.
  206          */
  207         for (o = 0; o < wp->length; o += sc->sectorsize) {
  208                 arc4rand(d, sc->sectorsize, 0);
  209                 arc4rand(&skey, sizeof skey, 0);
  210                 AES_makekey(&ki, DIR_ENCRYPT, G_BDE_SKEYBITS, skey);
  211                 AES_encrypt(&ci, &ki, d, d, sc->sectorsize);
  212                 d += sc->sectorsize;
  213         }
  214         /*
  215          * Having written a long random sequence to disk here, we want to
  216          * force a reseed, to avoid weakening the next time we use random
  217          * data for something important.
  218          */
  219         arc4rand(&o, sizeof o, 1);
  220 }
  221 
  222 /*
  223  * Calculate the total payload size of the encrypted device.
  224  *
  225  * Security objectives: none.
  226  *
  227  * This function needs to agree with g_bde_map_sector() about things.
  228  */
  229 
  230 uint64_t
  231 g_bde_max_sector(struct g_bde_key *kp)
  232 {
  233         uint64_t maxsect;
  234 
  235         maxsect = kp->media_width;
  236         maxsect /= kp->zone_width;
  237         maxsect *= kp->zone_cont;
  238         return (maxsect);
  239 }
  240 
  241 /*
  242  * Convert an unencrypted side offset to offsets on the encrypted side.
  243  *
  244  * Security objective:  Make it harder to identify what sectors contain what
  245  * on a "cold" disk image.
  246  *
  247  * We do this by adding the "keyoffset" from the lock to the physical sector
  248  * number modulus the available number of sectors.  Since all physical sectors
  249  * presumably look the same cold, this will do.
  250  *
  251  * As part of the mapping we have to skip the lock sectors which we know
  252  * the physical address off.  We also truncate the work packet, respecting
  253  * zone boundaries and lock sectors, so that we end up with a sequence of
  254  * sectors which are physically contiguous.
  255  *
  256  * Shuffling things further is an option, but the incremental frustration is
  257  * not currently deemed worth the run-time performance hit resulting from the
  258  * increased number of disk arm movements it would incur.
  259  *
  260  * This function offers nothing but a trivial diversion for an attacker able
  261  * to do "the cleaning lady attack" in its current static mapping form.
  262  */
  263 
  264 void
  265 g_bde_map_sector(struct g_bde_work *wp)
  266 {
  267 
  268         u_int   zone, zoff, u, len;
  269         uint64_t ko;
  270         struct g_bde_softc *sc;
  271         struct g_bde_key *kp;
  272 
  273         sc = wp->softc;
  274         kp = &sc->key;
  275 
  276         /* find which zone and the offset in it */
  277         zone = wp->offset / kp->zone_cont;
  278         zoff = wp->offset % kp->zone_cont;
  279 
  280         /* Calculate the offset of the key in the key sector */
  281         wp->ko = (zoff / kp->sectorsize) * G_BDE_SKEYLEN;
  282 
  283         /* restrict length to that zone */
  284         len = kp->zone_cont - zoff;
  285         if (len < wp->length)
  286                 wp->length = len;
  287 
  288         /* Find physical sector address */
  289         wp->so = zone * kp->zone_width + zoff;
  290         wp->so += kp->keyoffset;
  291         wp->so %= kp->media_width;
  292         wp->so += kp->sector0;
  293 
  294         /* The key sector is the last in this zone. */
  295         wp->kso = zone * kp->zone_width + kp->zone_cont;
  296         wp->kso += kp->keyoffset;
  297         wp->kso %= kp->media_width;
  298         wp->kso += kp->sector0; 
  299 
  300         /* Compensate for lock sectors */
  301         for (u = 0; u < G_BDE_MAXKEYS; u++) {
  302                 /* Find the start of this lock sector */
  303                 ko = kp->lsector[u] & ~(kp->sectorsize - 1);
  304 
  305                 if (wp->kso >= ko)
  306                         wp->kso += kp->sectorsize;
  307 
  308                 if (wp->so >= ko) {
  309                         /* lock sector before work packet */
  310                         wp->so += kp->sectorsize;
  311                 } else if ((wp->so + wp->length) > ko) {
  312                         /* lock sector in work packet, truncate */
  313                         wp->length = ko - wp->so;
  314                 }
  315         }
  316 
  317 #if 0
  318         printf("off %jd len %jd so %jd ko %jd kso %u\n",
  319             (intmax_t)wp->offset,
  320             (intmax_t)wp->length,
  321             (intmax_t)wp->so,
  322             (intmax_t)wp->kso,
  323             wp->ko);
  324 #endif
  325 }

Cache object: 3812a6c3e2bafa07c22fc5654ea4eff5


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.