2002-10-19 17:02:17 +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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
2005-01-06 18:27:30 +00:00
|
|
|
*/
|
|
|
|
/* This souce file contains routines which operates on the lock sectors, both
|
2002-10-19 17:02:17 +00:00
|
|
|
* for the kernel and the userland program gbde(1).
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/mutex.h>
|
2003-04-03 11:33:51 +00:00
|
|
|
#include <sys/endian.h>
|
2002-10-19 17:02:17 +00:00
|
|
|
#include <sys/md5.h>
|
|
|
|
|
|
|
|
#ifdef _KERNEL
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#else
|
2002-11-04 09:27:01 +00:00
|
|
|
#include <err.h>
|
|
|
|
#define CTASSERT(foo)
|
|
|
|
#define KASSERT(foo, bar) do { if(!(foo)) { warn bar ; exit (1); } } while (0)
|
2002-10-19 17:02:17 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#define g_free(foo) free(foo)
|
|
|
|
#endif
|
|
|
|
|
2005-03-11 15:42:51 +00:00
|
|
|
#include <crypto/rijndael/rijndael-api-fst.h>
|
2002-11-04 09:27:01 +00:00
|
|
|
#include <crypto/sha2/sha2.h>
|
|
|
|
|
2002-10-19 17:02:17 +00:00
|
|
|
#include <geom/geom.h>
|
|
|
|
#include <geom/bde/g_bde.h>
|
|
|
|
|
|
|
|
/*
|
2002-11-04 09:27:01 +00:00
|
|
|
* Hash the raw pass-phrase.
|
2002-10-19 17:02:17 +00:00
|
|
|
*
|
2002-11-04 09:27:01 +00:00
|
|
|
* Security objectives: produce from the pass-phrase a fixed length
|
|
|
|
* bytesequence with PRN like properties in a reproducible way retaining
|
|
|
|
* as much entropy from the pass-phrase as possible.
|
2002-10-19 17:02:17 +00:00
|
|
|
*
|
2002-11-04 09:27:01 +00:00
|
|
|
* SHA2-512 makes this easy.
|
2002-10-19 17:02:17 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2002-11-04 09:27:01 +00:00
|
|
|
g_bde_hash_pass(struct g_bde_softc *sc, const void *input, u_int len)
|
2002-10-19 17:02:17 +00:00
|
|
|
{
|
2002-11-04 09:27:01 +00:00
|
|
|
SHA512_CTX cx;
|
2002-10-19 17:02:17 +00:00
|
|
|
|
2002-11-04 09:27:01 +00:00
|
|
|
SHA512_Init(&cx);
|
|
|
|
SHA512_Update(&cx, input, len);
|
|
|
|
SHA512_Final(sc->sha2, &cx);
|
2002-10-19 17:02:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-11-04 09:27:01 +00:00
|
|
|
* Encode/Decode the lock structure in byte-sequence format.
|
2002-10-19 17:02:17 +00:00
|
|
|
*
|
2002-11-04 09:27:01 +00:00
|
|
|
* Security objectives: Store in pass-phrase dependent variant format.
|
2002-10-19 17:02:17 +00:00
|
|
|
*
|
2002-11-04 09:27:01 +00:00
|
|
|
* C-structure packing and byte-endianess depends on architecture, compiler
|
|
|
|
* and compiler options. Writing raw structures to disk is therefore a bad
|
|
|
|
* idea in these enlightend days.
|
|
|
|
*
|
|
|
|
* We spend a fraction of the key-material on shuffling the fields around
|
|
|
|
* so they will be stored in an unpredictable sequence.
|
|
|
|
*
|
|
|
|
* For each byte of the key-material we derive two field indexes, and swap
|
|
|
|
* the position of those two fields.
|
|
|
|
*
|
|
|
|
* I have not worked out the statistical properties of this shuffle, but
|
|
|
|
* given that the key-material has PRN properties, the primary objective
|
|
|
|
* of making it hard to figure out which bits are where in the lock sector
|
|
|
|
* is sufficiently fulfilled.
|
|
|
|
*
|
|
|
|
* We include (and shuffle) an extra hash field in the stored version for
|
|
|
|
* identification and versioning purposes. This field contains the MD5 hash
|
|
|
|
* of a version identifier (currently "0000") followed by the stored lock
|
|
|
|
* sector byte-sequence substituting zero bytes for the hash field.
|
|
|
|
*
|
|
|
|
* The stored keysequence is protected by AES/256/CBC elsewhere in the code
|
|
|
|
* so the fact that the generated byte sequence has a much higher than
|
|
|
|
* average density of zero bits (from the numeric fields) is not currently
|
|
|
|
* a concern.
|
|
|
|
*
|
|
|
|
* Should this later become a concern, a simple software update and
|
|
|
|
* pass-phrase change can remedy the situation. One possible solution
|
|
|
|
* could be to XOR the numeric fields with a key-material derived PRN.
|
|
|
|
*
|
|
|
|
* The chosen shuffle algorithm only works as long as we have no more than 16
|
|
|
|
* fields in the stored part of the lock structure (hence the CTASSERT below).
|
2002-10-19 17:02:17 +00:00
|
|
|
*/
|
|
|
|
|
2002-11-04 09:27:01 +00:00
|
|
|
CTASSERT(NLOCK_FIELDS <= 16);
|
|
|
|
|
|
|
|
static void
|
2003-10-07 09:28:07 +00:00
|
|
|
g_bde_shuffle_lock(u_char *sha2, int *buf)
|
2002-10-19 17:02:17 +00:00
|
|
|
{
|
2002-12-01 15:47:49 +00:00
|
|
|
int j, k, l;
|
|
|
|
u_int u;
|
2002-11-04 09:27:01 +00:00
|
|
|
|
|
|
|
/* Assign the fields sequential positions */
|
2002-12-01 15:47:49 +00:00
|
|
|
for(u = 0; u < NLOCK_FIELDS; u++)
|
|
|
|
buf[u] = u;
|
2002-11-04 09:27:01 +00:00
|
|
|
|
|
|
|
/* Then mix it all up */
|
2003-10-07 09:28:07 +00:00
|
|
|
for(u = 48; u < SHA512_DIGEST_LENGTH; u++) {
|
|
|
|
j = sha2[u] % NLOCK_FIELDS;
|
|
|
|
k = (sha2[u] / NLOCK_FIELDS) % NLOCK_FIELDS;
|
2002-11-04 09:27:01 +00:00
|
|
|
l = buf[j];
|
|
|
|
buf[j] = buf[k];
|
|
|
|
buf[k] = l;
|
|
|
|
}
|
2002-10-19 17:02:17 +00:00
|
|
|
}
|
|
|
|
|
2002-11-04 09:27:01 +00:00
|
|
|
int
|
2003-10-07 09:28:07 +00:00
|
|
|
g_bde_encode_lock(u_char *sha2, struct g_bde_key *gl, u_char *ptr)
|
2002-10-19 17:02:17 +00:00
|
|
|
{
|
2002-11-04 09:27:01 +00:00
|
|
|
int shuffle[NLOCK_FIELDS];
|
|
|
|
u_char *hash, *p;
|
|
|
|
int i;
|
|
|
|
MD5_CTX c;
|
|
|
|
|
2002-10-19 17:02:17 +00:00
|
|
|
p = ptr;
|
2002-11-04 09:27:01 +00:00
|
|
|
hash = NULL;
|
2003-10-07 09:28:07 +00:00
|
|
|
g_bde_shuffle_lock(sha2, shuffle);
|
2002-11-04 09:27:01 +00:00
|
|
|
for (i = 0; i < NLOCK_FIELDS; i++) {
|
|
|
|
switch(shuffle[i]) {
|
|
|
|
case 0:
|
2003-04-03 11:33:51 +00:00
|
|
|
le64enc(p, gl->sector0);
|
2002-11-04 09:27:01 +00:00
|
|
|
p += 8;
|
|
|
|
break;
|
|
|
|
case 1:
|
2003-04-03 11:33:51 +00:00
|
|
|
le64enc(p, gl->sectorN);
|
2002-11-04 09:27:01 +00:00
|
|
|
p += 8;
|
|
|
|
break;
|
|
|
|
case 2:
|
2003-04-03 11:33:51 +00:00
|
|
|
le64enc(p, gl->keyoffset);
|
2002-11-04 09:27:01 +00:00
|
|
|
p += 8;
|
|
|
|
break;
|
|
|
|
case 3:
|
2003-04-03 11:33:51 +00:00
|
|
|
le32enc(p, gl->sectorsize);
|
2002-11-04 09:27:01 +00:00
|
|
|
p += 4;
|
|
|
|
break;
|
|
|
|
case 4:
|
2003-04-03 11:33:51 +00:00
|
|
|
le32enc(p, gl->flags);
|
2002-11-04 09:27:01 +00:00
|
|
|
p += 4;
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
case 6:
|
|
|
|
case 7:
|
|
|
|
case 8:
|
2003-04-03 11:33:51 +00:00
|
|
|
le64enc(p, gl->lsector[shuffle[i] - 5]);
|
2002-11-04 09:27:01 +00:00
|
|
|
p += 8;
|
|
|
|
break;
|
|
|
|
case 9:
|
|
|
|
bcopy(gl->spare, p, sizeof gl->spare);
|
|
|
|
p += sizeof gl->spare;
|
|
|
|
break;
|
|
|
|
case 10:
|
|
|
|
bcopy(gl->salt, p, sizeof gl->salt);
|
|
|
|
p += sizeof gl->salt;
|
|
|
|
break;
|
|
|
|
case 11:
|
|
|
|
bcopy(gl->mkey, p, sizeof gl->mkey);
|
|
|
|
p += sizeof gl->mkey;
|
|
|
|
break;
|
|
|
|
case 12:
|
|
|
|
bzero(p, 16);
|
|
|
|
hash = p;
|
|
|
|
p += 16;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(ptr + G_BDE_LOCKSIZE != p)
|
|
|
|
return(-1);
|
|
|
|
if (hash == NULL)
|
|
|
|
return(-1);
|
|
|
|
MD5Init(&c);
|
|
|
|
MD5Update(&c, "0000", 4); /* Versioning */
|
|
|
|
MD5Update(&c, ptr, G_BDE_LOCKSIZE);
|
|
|
|
MD5Final(hash, &c);
|
|
|
|
return(0);
|
2002-10-19 17:02:17 +00:00
|
|
|
}
|
|
|
|
|
2002-11-04 09:27:01 +00:00
|
|
|
int
|
|
|
|
g_bde_decode_lock(struct g_bde_softc *sc, struct g_bde_key *gl, u_char *ptr)
|
2002-10-19 17:02:17 +00:00
|
|
|
{
|
2002-11-04 09:27:01 +00:00
|
|
|
int shuffle[NLOCK_FIELDS];
|
|
|
|
u_char *p;
|
|
|
|
u_char hash[16], hash2[16];
|
|
|
|
MD5_CTX c;
|
|
|
|
int i;
|
2002-10-19 17:02:17 +00:00
|
|
|
|
|
|
|
p = ptr;
|
2003-10-07 09:28:07 +00:00
|
|
|
g_bde_shuffle_lock(sc->sha2, shuffle);
|
2002-11-04 09:27:01 +00:00
|
|
|
for (i = 0; i < NLOCK_FIELDS; i++) {
|
|
|
|
switch(shuffle[i]) {
|
|
|
|
case 0:
|
2003-04-03 11:33:51 +00:00
|
|
|
gl->sector0 = le64dec(p);
|
2002-11-04 09:27:01 +00:00
|
|
|
p += 8;
|
|
|
|
break;
|
|
|
|
case 1:
|
2003-04-03 11:33:51 +00:00
|
|
|
gl->sectorN = le64dec(p);
|
2002-11-04 09:27:01 +00:00
|
|
|
p += 8;
|
|
|
|
break;
|
|
|
|
case 2:
|
2003-04-03 11:33:51 +00:00
|
|
|
gl->keyoffset = le64dec(p);
|
2002-11-04 09:27:01 +00:00
|
|
|
p += 8;
|
|
|
|
break;
|
|
|
|
case 3:
|
2003-04-03 11:33:51 +00:00
|
|
|
gl->sectorsize = le32dec(p);
|
2002-11-04 09:27:01 +00:00
|
|
|
p += 4;
|
|
|
|
break;
|
|
|
|
case 4:
|
2003-04-03 11:33:51 +00:00
|
|
|
gl->flags = le32dec(p);
|
2002-11-04 09:27:01 +00:00
|
|
|
p += 4;
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
case 6:
|
|
|
|
case 7:
|
|
|
|
case 8:
|
2003-04-03 11:33:51 +00:00
|
|
|
gl->lsector[shuffle[i] - 5] = le64dec(p);
|
2002-11-04 09:27:01 +00:00
|
|
|
p += 8;
|
|
|
|
break;
|
|
|
|
case 9:
|
|
|
|
bcopy(p, gl->spare, sizeof gl->spare);
|
|
|
|
p += sizeof gl->spare;
|
|
|
|
break;
|
|
|
|
case 10:
|
|
|
|
bcopy(p, gl->salt, sizeof gl->salt);
|
|
|
|
p += sizeof gl->salt;
|
|
|
|
break;
|
|
|
|
case 11:
|
|
|
|
bcopy(p, gl->mkey, sizeof gl->mkey);
|
|
|
|
p += sizeof gl->mkey;
|
|
|
|
break;
|
|
|
|
case 12:
|
|
|
|
bcopy(p, hash2, sizeof hash2);
|
|
|
|
bzero(p, sizeof hash2);
|
|
|
|
p += sizeof hash2;
|
|
|
|
break;
|
|
|
|
}
|
2002-10-19 17:02:17 +00:00
|
|
|
}
|
2002-11-04 09:27:01 +00:00
|
|
|
if(ptr + G_BDE_LOCKSIZE != p)
|
|
|
|
return(-1);
|
|
|
|
MD5Init(&c);
|
|
|
|
MD5Update(&c, "0000", 4); /* Versioning */
|
|
|
|
MD5Update(&c, ptr, G_BDE_LOCKSIZE);
|
|
|
|
MD5Final(hash, &c);
|
|
|
|
if (bcmp(hash, hash2, sizeof hash2))
|
|
|
|
return (1);
|
|
|
|
return (0);
|
2002-10-19 17:02:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-11-04 09:27:01 +00:00
|
|
|
* Encode/Decode the locksector address ("metadata") with key-material.
|
|
|
|
*
|
|
|
|
* Security objectives: Encode/Decode the metadata encrypted by key-material.
|
|
|
|
*
|
|
|
|
* A simple AES/128/CBC will do. We take care to always store the metadata
|
|
|
|
* in the same endianess to make it MI.
|
|
|
|
*
|
|
|
|
* In the typical case the metadata is stored in encrypted format in sector
|
|
|
|
* zero on the media, but at the users discretion or if the piece of the
|
|
|
|
* device used (sector0...sectorN) does not contain sector zero, it can
|
|
|
|
* be stored in a filesystem or on a PostIt.
|
|
|
|
*
|
|
|
|
* The inability to easily locate the lock sectors makes an attack on a
|
|
|
|
* cold disk much less attractive, without unduly inconveniencing the
|
|
|
|
* legitimate user who can feasibly do a brute-force scan if the metadata
|
|
|
|
* was lost.
|
2002-10-19 17:02:17 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2003-10-07 09:28:07 +00:00
|
|
|
g_bde_keyloc_encrypt(u_char *sha2, uint64_t v0, uint64_t v1, void *output)
|
2002-10-19 17:02:17 +00:00
|
|
|
{
|
2002-11-04 09:27:01 +00:00
|
|
|
u_char buf[16];
|
2002-10-19 17:02:17 +00:00
|
|
|
keyInstance ki;
|
|
|
|
cipherInstance ci;
|
|
|
|
|
2003-10-07 09:28:07 +00:00
|
|
|
le64enc(buf, v0);
|
|
|
|
le64enc(buf + 8, v1);
|
2002-11-04 09:27:01 +00:00
|
|
|
AES_init(&ci);
|
2003-10-07 09:28:07 +00:00
|
|
|
AES_makekey(&ki, DIR_ENCRYPT, G_BDE_KKEYBITS, sha2 + 0);
|
2002-11-04 09:27:01 +00:00
|
|
|
AES_encrypt(&ci, &ki, buf, output, sizeof buf);
|
|
|
|
bzero(buf, sizeof buf);
|
2002-10-19 17:02:17 +00:00
|
|
|
bzero(&ci, sizeof ci);
|
2002-10-20 11:09:58 +00:00
|
|
|
bzero(&ki, sizeof ki);
|
2002-10-19 17:02:17 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2003-10-07 09:28:07 +00:00
|
|
|
g_bde_keyloc_decrypt(u_char *sha2, void *input, uint64_t *output)
|
2002-10-19 17:02:17 +00:00
|
|
|
{
|
|
|
|
keyInstance ki;
|
|
|
|
cipherInstance ci;
|
2002-11-04 09:27:01 +00:00
|
|
|
u_char buf[16];
|
|
|
|
|
|
|
|
AES_init(&ci);
|
2003-10-07 09:28:07 +00:00
|
|
|
AES_makekey(&ki, DIR_DECRYPT, G_BDE_KKEYBITS, sha2 + 0);
|
2002-11-04 09:27:01 +00:00
|
|
|
AES_decrypt(&ci, &ki, input, buf, sizeof buf);
|
2003-10-07 09:28:07 +00:00
|
|
|
*output = le64dec(buf);
|
2002-11-04 09:27:01 +00:00
|
|
|
bzero(buf, sizeof buf);
|
2002-10-19 17:02:17 +00:00
|
|
|
bzero(&ci, sizeof ci);
|
2002-10-20 11:09:58 +00:00
|
|
|
bzero(&ki, sizeof ki);
|
2003-10-07 09:28:07 +00:00
|
|
|
return(0);
|
2002-10-19 17:02:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-11-04 09:27:01 +00:00
|
|
|
* Find and Encode/Decode lock sectors.
|
|
|
|
*
|
|
|
|
* Security objective: given the pass-phrase, find, decrypt, decode and
|
|
|
|
* validate the lock sector contents.
|
|
|
|
*
|
|
|
|
* For ondisk metadata we cannot know beforehand which of the lock sectors
|
|
|
|
* a given pass-phrase opens so we must try each of the metadata copies in
|
|
|
|
* sector zero in turn. If metadata was passed as an argument, we don't
|
|
|
|
* have this problem.
|
|
|
|
*
|
2002-10-19 17:02:17 +00:00
|
|
|
*/
|
|
|
|
|
2002-10-20 11:09:58 +00:00
|
|
|
static int
|
2002-11-04 09:27:01 +00:00
|
|
|
g_bde_decrypt_lockx(struct g_bde_softc *sc, u_char *meta, off_t mediasize, u_int sectorsize, u_int *nkey)
|
2002-10-19 17:02:17 +00:00
|
|
|
{
|
2002-11-04 09:27:01 +00:00
|
|
|
u_char *buf, *q;
|
2002-10-19 17:02:17 +00:00
|
|
|
struct g_bde_key *gl;
|
2003-10-07 09:28:07 +00:00
|
|
|
uint64_t off, q1;
|
2002-10-19 17:02:17 +00:00
|
|
|
int error, m, i;
|
|
|
|
keyInstance ki;
|
|
|
|
cipherInstance ci;
|
|
|
|
|
|
|
|
gl = &sc->key;
|
2002-11-04 09:27:01 +00:00
|
|
|
|
|
|
|
/* Try to decrypt the metadata */
|
2003-10-07 09:28:07 +00:00
|
|
|
error = g_bde_keyloc_decrypt(sc->sha2, meta, &off);
|
2002-10-19 17:02:17 +00:00
|
|
|
if (error)
|
2003-10-07 09:28:07 +00:00
|
|
|
return (error);
|
2002-11-04 09:27:01 +00:00
|
|
|
|
2004-12-15 12:18:41 +00:00
|
|
|
/* If it points into thin blue air, forget it */
|
2003-10-07 09:28:07 +00:00
|
|
|
if (off + G_BDE_LOCKSIZE > (uint64_t)mediasize) {
|
|
|
|
off = 0;
|
2002-10-20 11:09:58 +00:00
|
|
|
return (EINVAL);
|
2002-10-19 17:02:17 +00:00
|
|
|
}
|
2002-11-04 09:27:01 +00:00
|
|
|
|
|
|
|
/* The lock data may span two physical sectors. */
|
|
|
|
|
2002-10-19 17:02:17 +00:00
|
|
|
m = 1;
|
2003-10-07 09:28:07 +00:00
|
|
|
if (off % sectorsize > sectorsize - G_BDE_LOCKSIZE)
|
2002-10-19 17:02:17 +00:00
|
|
|
m++;
|
2002-11-04 09:27:01 +00:00
|
|
|
|
|
|
|
/* Read the suspected sector(s) */
|
2002-10-19 17:02:17 +00:00
|
|
|
buf = g_read_data(sc->consumer,
|
2003-10-07 09:28:07 +00:00
|
|
|
off - (off % sectorsize),
|
2002-10-19 17:02:17 +00:00
|
|
|
m * sectorsize, &error);
|
|
|
|
if (buf == NULL) {
|
2003-10-07 09:28:07 +00:00
|
|
|
off = 0;
|
2002-10-19 17:02:17 +00:00
|
|
|
return(error);
|
|
|
|
}
|
|
|
|
|
2002-11-04 09:27:01 +00:00
|
|
|
/* Find the byte-offset of the stored byte sequence */
|
2003-10-07 09:28:07 +00:00
|
|
|
q = buf + off % sectorsize;
|
2002-10-19 17:02:17 +00:00
|
|
|
|
2002-11-04 09:27:01 +00:00
|
|
|
/* If it is all zero, somebody nuked our lock sector */
|
2003-10-07 09:28:07 +00:00
|
|
|
q1 = 0;
|
2002-10-20 11:09:58 +00:00
|
|
|
for (i = 0; i < G_BDE_LOCKSIZE; i++)
|
2003-10-07 09:28:07 +00:00
|
|
|
q1 += q[i];
|
|
|
|
if (q1 == 0) {
|
|
|
|
off = 0;
|
2002-10-19 17:02:17 +00:00
|
|
|
g_free(buf);
|
|
|
|
return (ESRCH);
|
|
|
|
}
|
|
|
|
|
2002-11-04 09:27:01 +00:00
|
|
|
/* Decrypt the byte-sequence in place */
|
|
|
|
AES_init(&ci);
|
|
|
|
AES_makekey(&ki, DIR_DECRYPT, 256, sc->sha2 + 16);
|
|
|
|
AES_decrypt(&ci, &ki, q, q, G_BDE_LOCKSIZE);
|
2005-11-30 19:07:28 +00:00
|
|
|
|
2002-11-04 09:27:01 +00:00
|
|
|
/* Decode the byte-sequence */
|
|
|
|
i = g_bde_decode_lock(sc, gl, q);
|
|
|
|
q = NULL;
|
|
|
|
if (i < 0) {
|
2003-10-07 09:28:07 +00:00
|
|
|
off = 0;
|
2002-11-04 09:27:01 +00:00
|
|
|
return (EDOOFUS); /* Programming error */
|
|
|
|
} else if (i > 0) {
|
2003-10-07 09:28:07 +00:00
|
|
|
off = 0;
|
2002-11-04 09:27:01 +00:00
|
|
|
return (ENOTDIR); /* Hash didn't match */
|
2002-10-19 17:02:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bzero(buf, sectorsize * m);
|
|
|
|
g_free(buf);
|
|
|
|
|
2002-11-04 09:27:01 +00:00
|
|
|
/* If the masterkey is all zeros, user destroyed it */
|
2003-10-07 09:28:07 +00:00
|
|
|
q1 = 0;
|
2002-10-30 22:13:54 +00:00
|
|
|
for (i = 0; i < (int)sizeof(gl->mkey); i++)
|
2003-10-07 09:28:07 +00:00
|
|
|
q1 += gl->mkey[i];
|
|
|
|
if (q1 == 0)
|
2002-10-19 17:02:17 +00:00
|
|
|
return (ENOENT);
|
2002-11-04 09:27:01 +00:00
|
|
|
|
2002-12-18 19:57:27 +00:00
|
|
|
/* If we have an unsorted lock-sequence, refuse */
|
2004-09-11 17:57:51 +00:00
|
|
|
for (i = 0; i < G_BDE_MAXKEYS - 1; i++)
|
|
|
|
if (gl->lsector[i] >= gl->lsector[i + 1])
|
|
|
|
return (EINVAL);
|
2002-12-18 19:57:27 +00:00
|
|
|
|
2002-11-04 09:27:01 +00:00
|
|
|
/* Finally, find out which key was used by matching the byte offset */
|
2002-10-19 17:02:17 +00:00
|
|
|
for (i = 0; i < G_BDE_MAXKEYS; i++)
|
2003-10-07 09:28:07 +00:00
|
|
|
if (nkey != NULL && off == gl->lsector[i])
|
2002-10-19 17:02:17 +00:00
|
|
|
*nkey = i;
|
2003-10-07 09:28:07 +00:00
|
|
|
off = 0;
|
2002-10-19 17:02:17 +00:00
|
|
|
return (0);
|
|
|
|
}
|
2002-10-20 11:09:58 +00:00
|
|
|
|
|
|
|
int
|
2002-11-04 09:27:01 +00:00
|
|
|
g_bde_decrypt_lock(struct g_bde_softc *sc, u_char *keymat, u_char *meta, off_t mediasize, u_int sectorsize, u_int *nkey)
|
2002-10-20 11:09:58 +00:00
|
|
|
{
|
|
|
|
u_char *buf, buf1[16];
|
|
|
|
int error, e, i;
|
|
|
|
|
2002-11-04 09:27:01 +00:00
|
|
|
/* set up the key-material */
|
|
|
|
bcopy(keymat, sc->sha2, SHA512_DIGEST_LENGTH);
|
|
|
|
|
|
|
|
/* If passed-in metadata is non-zero, use it */
|
2002-10-20 11:09:58 +00:00
|
|
|
bzero(buf1, sizeof buf1);
|
2003-03-25 09:02:41 +00:00
|
|
|
if (meta != NULL && bcmp(buf1, meta, sizeof buf1))
|
2002-11-04 09:27:01 +00:00
|
|
|
return (g_bde_decrypt_lockx(sc, meta, mediasize,
|
2002-10-20 11:09:58 +00:00
|
|
|
sectorsize, nkey));
|
|
|
|
|
2002-11-04 09:27:01 +00:00
|
|
|
/* Read sector zero */
|
2002-10-20 11:09:58 +00:00
|
|
|
buf = g_read_data(sc->consumer, 0, sectorsize, &error);
|
|
|
|
if (buf == NULL)
|
|
|
|
return(error);
|
2002-11-04 09:27:01 +00:00
|
|
|
|
|
|
|
/* Try each index in turn, save indicative errors for final result */
|
|
|
|
error = EINVAL;
|
2002-10-20 11:09:58 +00:00
|
|
|
for (i = 0; i < G_BDE_MAXKEYS; i++) {
|
2002-11-04 09:27:01 +00:00
|
|
|
e = g_bde_decrypt_lockx(sc, buf + i * 16, mediasize,
|
2002-10-20 11:09:58 +00:00
|
|
|
sectorsize, nkey);
|
2002-11-04 09:27:01 +00:00
|
|
|
/* Success or destroyed master key terminates */
|
2002-10-20 11:09:58 +00:00
|
|
|
if (e == 0 || e == ENOENT) {
|
|
|
|
error = e;
|
|
|
|
break;
|
|
|
|
}
|
2002-11-04 09:27:01 +00:00
|
|
|
if (e != 0 && error == EINVAL)
|
2002-10-20 11:09:58 +00:00
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
g_free(buf);
|
|
|
|
return (error);
|
|
|
|
}
|