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

Cache object: 902ed109654bcdabb4847578ae6ced8f


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