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

Cache object: 33f23d3a071a15ce892c0b6fc033c155


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