2002-05-26 18:14:38 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2002 Poul-Henning Kamp
|
|
|
|
* Copyright (c) 2002 Networks Associates Technology, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This software was developed for the FreeBSD Project by Poul-Henning Kamp
|
|
|
|
* and NAI Labs, the Security Research Division of Network Associates, Inc.
|
|
|
|
* under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
|
|
|
|
* DARPA CHATS research program.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The names of the authors may not be used to endorse or promote
|
|
|
|
* products derived from this software without specific prior written
|
|
|
|
* permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
2005-01-06 18:27:30 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2002-05-26 18:14:38 +00:00
|
|
|
* This method provides AES encryption with a compiled in key (default
|
|
|
|
* all zeroes).
|
|
|
|
*
|
|
|
|
* XXX: This could probably save a lot of code by pretending to be a slicer.
|
|
|
|
*/
|
|
|
|
|
2003-06-11 06:49:16 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2002-05-26 18:14:38 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/conf.h>
|
|
|
|
#include <sys/bio.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/mutex.h>
|
2002-06-28 21:25:15 +00:00
|
|
|
#include <sys/libkern.h>
|
2002-09-28 11:57:20 +00:00
|
|
|
#include <sys/endian.h>
|
|
|
|
#include <sys/md5.h>
|
2002-05-26 18:14:38 +00:00
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <geom/geom.h>
|
|
|
|
|
2005-03-11 15:42:51 +00:00
|
|
|
#include <crypto/rijndael/rijndael-api-fst.h>
|
2002-05-26 18:14:38 +00:00
|
|
|
|
|
|
|
#define AES_CLASS_NAME "AES"
|
|
|
|
|
2002-06-28 21:25:15 +00:00
|
|
|
#define MASTER_KEY_LENGTH (1024/8)
|
|
|
|
|
2002-12-16 22:33:27 +00:00
|
|
|
static const u_char *aes_magic = "<<FreeBSD-GEOM-AES>>";
|
|
|
|
static const u_char *aes_magic_random = "<<FreeBSD-GEOM-AES-RANDOM>>";
|
|
|
|
static const u_char *aes_magic_test = "<<FreeBSD-GEOM-AES-TEST>>";
|
2002-05-26 18:14:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
struct g_aes_softc {
|
2002-06-28 21:25:15 +00:00
|
|
|
enum {
|
|
|
|
KEY_ZERO,
|
|
|
|
KEY_RANDOM,
|
|
|
|
KEY_TEST
|
|
|
|
} keying;
|
2002-05-26 18:14:38 +00:00
|
|
|
u_int sectorsize;
|
|
|
|
off_t mediasize;
|
|
|
|
cipherInstance ci;
|
2002-06-28 21:25:15 +00:00
|
|
|
u_char master_key[MASTER_KEY_LENGTH];
|
2002-05-26 18:14:38 +00:00
|
|
|
};
|
|
|
|
|
2002-06-28 21:25:15 +00:00
|
|
|
/*
|
|
|
|
* Generate a sectorkey from the masterkey and the offset position.
|
|
|
|
*
|
|
|
|
* For KEY_ZERO we just return a key of all zeros.
|
|
|
|
*
|
|
|
|
* We feed the sector byte offset, 16 bytes of the master-key and
|
|
|
|
* the sector byte offset once more to MD5.
|
|
|
|
* The sector byte offset is converted to little-endian format first
|
|
|
|
* to support multi-architecture operation.
|
|
|
|
* We use 16 bytes from the master-key starting at the logical sector
|
|
|
|
* number modulus he length of the master-key. If need be we wrap
|
|
|
|
* around to the start of the master-key.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_aes_makekey(struct g_aes_softc *sc, off_t off, keyInstance *ki, int dir)
|
|
|
|
{
|
|
|
|
MD5_CTX cx;
|
|
|
|
u_int64_t u64;
|
|
|
|
u_int u, u1;
|
|
|
|
u_char *p, buf[16];
|
|
|
|
|
|
|
|
if (sc->keying == KEY_ZERO) {
|
|
|
|
rijndael_makeKey(ki, dir, 128, sc->master_key);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
MD5Init(&cx);
|
|
|
|
u64 = htole64(off);
|
|
|
|
MD5Update(&cx, (u_char *)&u64, sizeof(u64));
|
|
|
|
u = off / sc->sectorsize;
|
|
|
|
u %= sizeof sc->master_key;
|
|
|
|
p = sc->master_key + u;
|
|
|
|
if (u + 16 <= sizeof(sc->master_key)) {
|
|
|
|
MD5Update(&cx, p, 16);
|
|
|
|
} else {
|
|
|
|
u1 = sizeof sc->master_key - u;
|
|
|
|
MD5Update(&cx, p, u1);
|
|
|
|
MD5Update(&cx, sc->master_key, 16 - u1);
|
|
|
|
u1 = 0; /* destroy evidence */
|
|
|
|
}
|
|
|
|
u = 0; /* destroy evidence */
|
|
|
|
MD5Update(&cx, (u_char *)&u64, sizeof(u64));
|
|
|
|
u64 = 0; /* destroy evidence */
|
|
|
|
MD5Final(buf, &cx);
|
|
|
|
bzero(&cx, sizeof cx); /* destroy evidence */
|
|
|
|
rijndael_makeKey(ki, dir, 128, buf);
|
|
|
|
bzero(buf, sizeof buf); /* destroy evidence */
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2002-05-26 18:14:38 +00:00
|
|
|
static void
|
|
|
|
g_aes_read_done(struct bio *bp)
|
|
|
|
{
|
|
|
|
struct g_geom *gp;
|
|
|
|
struct g_aes_softc *sc;
|
|
|
|
u_char *p, *b, *e, *sb;
|
2002-06-28 21:25:15 +00:00
|
|
|
keyInstance dkey;
|
|
|
|
off_t o;
|
2002-05-26 18:14:38 +00:00
|
|
|
|
|
|
|
gp = bp->bio_from->geom;
|
|
|
|
sc = gp->softc;
|
2003-02-19 05:47:46 +00:00
|
|
|
sb = g_malloc(sc->sectorsize, M_WAITOK);
|
2002-05-26 18:14:38 +00:00
|
|
|
b = bp->bio_data;
|
|
|
|
e = bp->bio_data;
|
|
|
|
e += bp->bio_length;
|
2002-06-28 21:25:15 +00:00
|
|
|
o = bp->bio_offset - sc->sectorsize;
|
2002-05-26 18:14:38 +00:00
|
|
|
for (p = b; p < e; p += sc->sectorsize) {
|
2002-06-28 21:25:15 +00:00
|
|
|
g_aes_makekey(sc, o, &dkey, DIR_DECRYPT);
|
|
|
|
rijndael_blockDecrypt(&sc->ci, &dkey, p, sc->sectorsize * 8, sb);
|
2002-05-26 18:14:38 +00:00
|
|
|
bcopy(sb, p, sc->sectorsize);
|
2002-06-28 21:25:15 +00:00
|
|
|
o += sc->sectorsize;
|
2002-05-26 18:14:38 +00:00
|
|
|
}
|
2002-06-28 21:25:15 +00:00
|
|
|
bzero(&dkey, sizeof dkey); /* destroy evidence */
|
|
|
|
bzero(sb, sc->sectorsize); /* destroy evidence */
|
|
|
|
g_free(sb);
|
2002-05-26 18:14:38 +00:00
|
|
|
g_std_done(bp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_aes_write_done(struct bio *bp)
|
|
|
|
{
|
|
|
|
|
2002-06-28 21:25:15 +00:00
|
|
|
bzero(bp->bio_data, bp->bio_length); /* destroy evidence */
|
2002-05-26 18:14:38 +00:00
|
|
|
g_free(bp->bio_data);
|
|
|
|
g_std_done(bp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_aes_start(struct bio *bp)
|
|
|
|
{
|
|
|
|
struct g_geom *gp;
|
|
|
|
struct g_consumer *cp;
|
|
|
|
struct g_aes_softc *sc;
|
|
|
|
struct bio *bp2;
|
|
|
|
u_char *p1, *p2, *b, *e;
|
2002-06-28 21:25:15 +00:00
|
|
|
keyInstance ekey;
|
|
|
|
off_t o;
|
2002-05-26 18:14:38 +00:00
|
|
|
|
|
|
|
gp = bp->bio_to->geom;
|
|
|
|
cp = LIST_FIRST(&gp->consumer);
|
|
|
|
sc = gp->softc;
|
|
|
|
switch (bp->bio_cmd) {
|
|
|
|
case BIO_READ:
|
|
|
|
bp2 = g_clone_bio(bp);
|
2002-09-27 20:47:23 +00:00
|
|
|
if (bp2 == NULL) {
|
2002-09-30 08:54:46 +00:00
|
|
|
g_io_deliver(bp, ENOMEM);
|
2002-09-27 20:47:23 +00:00
|
|
|
return;
|
|
|
|
}
|
2002-05-26 18:14:38 +00:00
|
|
|
bp2->bio_done = g_aes_read_done;
|
|
|
|
bp2->bio_offset += sc->sectorsize;
|
|
|
|
g_io_request(bp2, cp);
|
|
|
|
break;
|
|
|
|
case BIO_WRITE:
|
|
|
|
bp2 = g_clone_bio(bp);
|
2002-09-27 20:47:23 +00:00
|
|
|
if (bp2 == NULL) {
|
2002-09-30 08:54:46 +00:00
|
|
|
g_io_deliver(bp, ENOMEM);
|
2002-09-27 20:47:23 +00:00
|
|
|
return;
|
|
|
|
}
|
2002-05-26 18:14:38 +00:00
|
|
|
bp2->bio_done = g_aes_write_done;
|
|
|
|
bp2->bio_offset += sc->sectorsize;
|
2003-02-19 05:47:46 +00:00
|
|
|
bp2->bio_data = g_malloc(bp->bio_length, M_WAITOK);
|
2002-05-26 18:14:38 +00:00
|
|
|
b = bp->bio_data;
|
|
|
|
e = bp->bio_data;
|
|
|
|
e += bp->bio_length;
|
|
|
|
p2 = bp2->bio_data;
|
2002-06-28 21:25:15 +00:00
|
|
|
o = bp->bio_offset;
|
2002-05-26 18:14:38 +00:00
|
|
|
for (p1 = b; p1 < e; p1 += sc->sectorsize) {
|
2002-06-28 21:25:15 +00:00
|
|
|
g_aes_makekey(sc, o, &ekey, DIR_ENCRYPT);
|
|
|
|
rijndael_blockEncrypt(&sc->ci, &ekey,
|
2002-05-26 18:14:38 +00:00
|
|
|
p1, sc->sectorsize * 8, p2);
|
|
|
|
p2 += sc->sectorsize;
|
2002-06-28 21:25:15 +00:00
|
|
|
o += sc->sectorsize;
|
2002-05-26 18:14:38 +00:00
|
|
|
}
|
2002-06-28 21:25:15 +00:00
|
|
|
bzero(&ekey, sizeof ekey); /* destroy evidence */
|
2002-05-26 18:14:38 +00:00
|
|
|
g_io_request(bp2, cp);
|
|
|
|
break;
|
|
|
|
case BIO_GETATTR:
|
|
|
|
bp2 = g_clone_bio(bp);
|
2002-09-27 20:47:23 +00:00
|
|
|
if (bp2 == NULL) {
|
2002-09-30 08:54:46 +00:00
|
|
|
g_io_deliver(bp, ENOMEM);
|
2002-09-27 20:47:23 +00:00
|
|
|
return;
|
|
|
|
}
|
2002-05-26 18:14:38 +00:00
|
|
|
bp2->bio_done = g_std_done;
|
|
|
|
bp2->bio_offset += sc->sectorsize;
|
|
|
|
g_io_request(bp2, cp);
|
|
|
|
break;
|
|
|
|
default:
|
2002-09-30 08:54:46 +00:00
|
|
|
g_io_deliver(bp, EOPNOTSUPP);
|
2002-05-26 18:14:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_aes_orphan(struct g_consumer *cp)
|
|
|
|
{
|
|
|
|
struct g_geom *gp;
|
2002-06-28 21:25:15 +00:00
|
|
|
struct g_aes_softc *sc;
|
2002-05-26 18:14:38 +00:00
|
|
|
|
|
|
|
g_trace(G_T_TOPOLOGY, "g_aes_orphan(%p/%s)", cp, cp->provider->name);
|
|
|
|
g_topology_assert();
|
|
|
|
KASSERT(cp->provider->error != 0,
|
|
|
|
("g_aes_orphan with error == 0"));
|
|
|
|
|
|
|
|
gp = cp->geom;
|
2002-06-28 21:25:15 +00:00
|
|
|
sc = gp->softc;
|
2003-05-02 12:52:51 +00:00
|
|
|
g_wither_geom(gp, cp->provider->error);
|
2002-06-28 21:25:15 +00:00
|
|
|
bzero(sc, sizeof(struct g_aes_softc)); /* destroy evidence */
|
2003-05-02 12:52:51 +00:00
|
|
|
g_free(sc);
|
2002-05-26 18:14:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
g_aes_access(struct g_provider *pp, int dr, int dw, int de)
|
|
|
|
{
|
|
|
|
struct g_geom *gp;
|
|
|
|
struct g_consumer *cp;
|
|
|
|
|
|
|
|
gp = pp->geom;
|
|
|
|
cp = LIST_FIRST(&gp->consumer);
|
|
|
|
/* On first open, grab an extra "exclusive" bit */
|
|
|
|
if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0)
|
|
|
|
de++;
|
|
|
|
/* ... and let go of it on last close */
|
|
|
|
if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1)
|
|
|
|
de--;
|
2004-02-12 22:42:11 +00:00
|
|
|
return (g_access(cp, dr, dw, de));
|
2002-05-26 18:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct g_geom *
|
|
|
|
g_aes_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
|
|
|
|
{
|
|
|
|
struct g_geom *gp;
|
|
|
|
struct g_consumer *cp;
|
|
|
|
struct g_aes_softc *sc;
|
|
|
|
int error;
|
|
|
|
u_int sectorsize;
|
|
|
|
off_t mediasize;
|
|
|
|
u_char *buf;
|
|
|
|
|
|
|
|
g_trace(G_T_TOPOLOGY, "aes_taste(%s,%s)", mp->name, pp->name);
|
|
|
|
g_topology_assert();
|
|
|
|
gp = g_new_geomf(mp, "%s.aes", pp->name);
|
|
|
|
cp = g_new_consumer(gp);
|
|
|
|
g_attach(cp, pp);
|
2004-02-12 22:42:11 +00:00
|
|
|
error = g_access(cp, 1, 0, 0);
|
2002-05-26 18:14:38 +00:00
|
|
|
if (error) {
|
2002-06-09 10:57:34 +00:00
|
|
|
g_detach(cp);
|
2002-05-26 18:14:38 +00:00
|
|
|
g_destroy_consumer(cp);
|
|
|
|
g_destroy_geom(gp);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
buf = NULL;
|
2002-09-27 21:37:11 +00:00
|
|
|
g_topology_unlock();
|
2003-05-02 05:26:47 +00:00
|
|
|
do {
|
2002-05-26 18:14:38 +00:00
|
|
|
if (gp->rank != 2)
|
|
|
|
break;
|
2002-10-20 20:28:24 +00:00
|
|
|
sectorsize = cp->provider->sectorsize;
|
|
|
|
mediasize = cp->provider->mediasize;
|
2005-11-30 22:15:00 +00:00
|
|
|
buf = g_read_data(cp, 0, sectorsize, NULL);
|
2005-11-30 19:24:51 +00:00
|
|
|
if (buf == NULL) {
|
2002-05-26 18:14:38 +00:00
|
|
|
break;
|
|
|
|
}
|
2003-02-19 05:47:46 +00:00
|
|
|
sc = g_malloc(sizeof(struct g_aes_softc), M_WAITOK | M_ZERO);
|
2002-06-28 21:25:15 +00:00
|
|
|
if (!memcmp(buf, aes_magic, strlen(aes_magic))) {
|
|
|
|
sc->keying = KEY_ZERO;
|
|
|
|
} else if (!memcmp(buf, aes_magic_random,
|
|
|
|
strlen(aes_magic_random))) {
|
|
|
|
sc->keying = KEY_RANDOM;
|
|
|
|
} else if (!memcmp(buf, aes_magic_test,
|
|
|
|
strlen(aes_magic_test))) {
|
|
|
|
sc->keying = KEY_TEST;
|
|
|
|
} else {
|
|
|
|
g_free(sc);
|
|
|
|
break;
|
|
|
|
}
|
2003-05-02 12:52:51 +00:00
|
|
|
g_free(buf);
|
2002-05-26 18:14:38 +00:00
|
|
|
gp->softc = sc;
|
|
|
|
sc->sectorsize = sectorsize;
|
|
|
|
sc->mediasize = mediasize - sectorsize;
|
|
|
|
rijndael_cipherInit(&sc->ci, MODE_CBC, NULL);
|
2002-06-28 21:25:15 +00:00
|
|
|
if (sc->keying == KEY_TEST) {
|
|
|
|
int i;
|
|
|
|
u_char *p;
|
|
|
|
|
|
|
|
p = sc->master_key;
|
2002-09-28 11:57:20 +00:00
|
|
|
for (i = 0; i < (int)sizeof sc->master_key; i ++)
|
2002-06-28 21:25:15 +00:00
|
|
|
*p++ = i;
|
|
|
|
}
|
|
|
|
if (sc->keying == KEY_RANDOM) {
|
|
|
|
int i;
|
|
|
|
u_int32_t u;
|
|
|
|
u_char *p;
|
|
|
|
|
|
|
|
p = sc->master_key;
|
2002-09-28 11:57:20 +00:00
|
|
|
for (i = 0; i < (int)sizeof sc->master_key; i += sizeof u) {
|
2002-06-28 21:25:15 +00:00
|
|
|
u = arc4random();
|
|
|
|
*p++ = u;
|
|
|
|
*p++ = u >> 8;
|
|
|
|
*p++ = u >> 16;
|
|
|
|
*p++ = u >> 24;
|
|
|
|
}
|
|
|
|
}
|
2002-09-27 21:37:11 +00:00
|
|
|
g_topology_lock();
|
2002-05-26 18:14:38 +00:00
|
|
|
pp = g_new_providerf(gp, gp->name);
|
|
|
|
pp->mediasize = mediasize - sectorsize;
|
2002-10-20 20:28:24 +00:00
|
|
|
pp->sectorsize = sectorsize;
|
2002-05-26 18:14:38 +00:00
|
|
|
g_error_provider(pp, 0);
|
2002-09-27 21:37:11 +00:00
|
|
|
g_topology_unlock();
|
2003-05-02 05:26:47 +00:00
|
|
|
} while(0);
|
2002-09-27 21:37:11 +00:00
|
|
|
g_topology_lock();
|
2002-05-26 18:14:38 +00:00
|
|
|
if (buf)
|
|
|
|
g_free(buf);
|
2004-02-12 22:42:11 +00:00
|
|
|
g_access(cp, -1, 0, 0);
|
2002-05-26 18:14:38 +00:00
|
|
|
if (gp->softc != NULL)
|
|
|
|
return (gp);
|
2002-06-09 10:57:34 +00:00
|
|
|
g_detach(cp);
|
2002-05-26 18:14:38 +00:00
|
|
|
g_destroy_consumer(cp);
|
|
|
|
g_destroy_geom(gp);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct g_class g_aes_class = {
|
2003-03-24 19:30:15 +00:00
|
|
|
.name = AES_CLASS_NAME,
|
2004-08-08 07:57:53 +00:00
|
|
|
.version = G_VERSION,
|
2003-03-24 19:30:15 +00:00
|
|
|
.taste = g_aes_taste,
|
2004-08-08 06:49:07 +00:00
|
|
|
.start = g_aes_start,
|
|
|
|
.orphan = g_aes_orphan,
|
|
|
|
.spoiled = g_std_spoiled,
|
|
|
|
.access = g_aes_access,
|
2002-05-26 18:14:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
DECLARE_GEOM_CLASS(g_aes_class, g_aes);
|