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/geom_aes.c

Version: -  FREEBSD  -  FREEBSD11  -  FREEBSD10  -  FREEBSD9  -  FREEBSD92  -  FREEBSD91  -  FREEBSD90  -  FREEBSD8  -  FREEBSD82  -  FREEBSD81  -  FREEBSD80  -  FREEBSD7  -  FREEBSD74  -  FREEBSD73  -  FREEBSD72  -  FREEBSD71  -  FREEBSD70  -  FREEBSD6  -  FREEBSD64  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
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  * 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: releng/7.3/sys/geom/geom_aes.c 152971 2005-11-30 22:15:00Z sobomax $");
   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 
   71 struct g_aes_softc {
   72         enum {
   73                 KEY_ZERO,
   74                 KEY_RANDOM,
   75                 KEY_TEST
   76         } keying;
   77         u_int   sectorsize;
   78         off_t   mediasize;
   79         cipherInstance ci;
   80         u_char master_key[MASTER_KEY_LENGTH];
   81 };
   82 
   83 /*
   84  * Generate a sectorkey from the masterkey and the offset position.
   85  *
   86  * For KEY_ZERO we just return a key of all zeros.
   87  *
   88  * We feed the sector byte offset, 16 bytes of the master-key and
   89  * the sector byte offset once more to MD5.
   90  * The sector byte offset is converted to little-endian format first
   91  * to support multi-architecture operation.
   92  * We use 16 bytes from the master-key starting at the logical sector
   93  * number modulus he length of the master-key.  If need be we wrap
   94  * around to the start of the master-key.
   95  */
   96 
   97 static void
   98 g_aes_makekey(struct g_aes_softc *sc, off_t off, keyInstance *ki, int dir)
   99 {
  100         MD5_CTX cx;
  101         u_int64_t u64;
  102         u_int u, u1;
  103         u_char *p, buf[16];
  104 
  105         if (sc->keying == KEY_ZERO) {
  106                 rijndael_makeKey(ki, dir, 128, sc->master_key);
  107                 return;
  108         }
  109         MD5Init(&cx);
  110         u64 = htole64(off);
  111         MD5Update(&cx, (u_char *)&u64, sizeof(u64));
  112         u = off / sc->sectorsize;
  113         u %= sizeof sc->master_key;
  114         p = sc->master_key + u;
  115         if (u + 16 <= sizeof(sc->master_key)) {
  116                 MD5Update(&cx, p, 16);
  117         } else {
  118                 u1 = sizeof sc->master_key - u;
  119                 MD5Update(&cx, p, u1);
  120                 MD5Update(&cx, sc->master_key, 16 - u1);
  121                 u1 = 0;                         /* destroy evidence */
  122         }
  123         u = 0;                                  /* destroy evidence */
  124         MD5Update(&cx, (u_char *)&u64, sizeof(u64));
  125         u64 = 0;                                /* destroy evidence */
  126         MD5Final(buf, &cx);
  127         bzero(&cx, sizeof cx);                  /* destroy evidence */
  128         rijndael_makeKey(ki, dir, 128, buf);
  129         bzero(buf, sizeof buf);                 /* destroy evidence */
  130 
  131 }
  132 
  133 static void
  134 g_aes_read_done(struct bio *bp)
  135 {
  136         struct g_geom *gp;
  137         struct g_aes_softc *sc;
  138         u_char *p, *b, *e, *sb;
  139         keyInstance dkey;
  140         off_t o;
  141 
  142         gp = bp->bio_from->geom;
  143         sc = gp->softc;
  144         sb = g_malloc(sc->sectorsize, M_WAITOK);
  145         b = bp->bio_data;
  146         e = bp->bio_data;
  147         e += bp->bio_length;
  148         o = bp->bio_offset - sc->sectorsize;
  149         for (p = b; p < e; p += sc->sectorsize) {
  150                 g_aes_makekey(sc, o, &dkey, DIR_DECRYPT);
  151                 rijndael_blockDecrypt(&sc->ci, &dkey, p, sc->sectorsize * 8, sb);
  152                 bcopy(sb, p, sc->sectorsize);
  153                 o += sc->sectorsize;
  154         }
  155         bzero(&dkey, sizeof dkey);              /* destroy evidence */
  156         bzero(sb, sc->sectorsize);              /* destroy evidence */
  157         g_free(sb);
  158         g_std_done(bp);
  159 }
  160 
  161 static void
  162 g_aes_write_done(struct bio *bp)
  163 {
  164 
  165         bzero(bp->bio_data, bp->bio_length);    /* destroy evidence */
  166         g_free(bp->bio_data);
  167         g_std_done(bp);
  168 }
  169 
  170 static void
  171 g_aes_start(struct bio *bp)
  172 {
  173         struct g_geom *gp;
  174         struct g_consumer *cp;
  175         struct g_aes_softc *sc;
  176         struct bio *bp2;
  177         u_char *p1, *p2, *b, *e;
  178         keyInstance ekey;
  179         off_t o;
  180 
  181         gp = bp->bio_to->geom;
  182         cp = LIST_FIRST(&gp->consumer);
  183         sc = gp->softc;
  184         switch (bp->bio_cmd) {
  185         case BIO_READ:
  186                 bp2 = g_clone_bio(bp);
  187                 if (bp2 == NULL) {
  188                         g_io_deliver(bp, ENOMEM);
  189                         return;
  190                 }
  191                 bp2->bio_done = g_aes_read_done;
  192                 bp2->bio_offset += sc->sectorsize;
  193                 g_io_request(bp2, cp);
  194                 break;
  195         case BIO_WRITE:
  196                 bp2 = g_clone_bio(bp);
  197                 if (bp2 == NULL) {
  198                         g_io_deliver(bp, ENOMEM);
  199                         return;
  200                 }
  201                 bp2->bio_done = g_aes_write_done;
  202                 bp2->bio_offset += sc->sectorsize;
  203                 bp2->bio_data = g_malloc(bp->bio_length, M_WAITOK);
  204                 b = bp->bio_data;
  205                 e = bp->bio_data;
  206                 e += bp->bio_length;
  207                 p2 = bp2->bio_data;
  208                 o = bp->bio_offset;
  209                 for (p1 = b; p1 < e; p1 += sc->sectorsize) {
  210                         g_aes_makekey(sc, o, &ekey, DIR_ENCRYPT);
  211                         rijndael_blockEncrypt(&sc->ci, &ekey,
  212                             p1, sc->sectorsize * 8, p2);
  213                         p2 += sc->sectorsize;
  214                         o += sc->sectorsize;
  215                 }
  216                 bzero(&ekey, sizeof ekey);      /* destroy evidence */
  217                 g_io_request(bp2, cp);
  218                 break;
  219         case BIO_GETATTR:
  220                 bp2 = g_clone_bio(bp);
  221                 if (bp2 == NULL) {
  222                         g_io_deliver(bp, ENOMEM);
  223                         return;
  224                 }
  225                 bp2->bio_done = g_std_done;
  226                 bp2->bio_offset += sc->sectorsize;
  227                 g_io_request(bp2, cp);
  228                 break;
  229         default:
  230                 g_io_deliver(bp, EOPNOTSUPP);
  231                 return;
  232         }
  233         return;
  234 }
  235 
  236 static void
  237 g_aes_orphan(struct g_consumer *cp)
  238 {
  239         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 
  255 static int
  256 g_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 
  272 static struct g_geom *
  273 g_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, NULL);
  303                 if (buf == NULL) {
  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 
  365 static 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 
  375 DECLARE_GEOM_CLASS(g_aes_class, g_aes);

Cache object: 365f70bdf1bd789c161487ca1e25e103


[ 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.