2 * Copyright (c) 2002 Poul-Henning Kamp
3 * Copyright (c) 2002 Networks Associates Technology, Inc.
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.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 /* This source file contains the functions responsible for the crypto, keying
35 * and mapping operations on the I/O requests.
39 #include <sys/param.h>
42 #include <sys/mutex.h>
43 #include <sys/queue.h>
44 #include <sys/malloc.h>
45 #include <sys/libkern.h>
46 #include <sys/endian.h>
49 #include <crypto/rijndael/rijndael-api-fst.h>
50 #include <crypto/sha2/sha2.h>
52 #include <geom/geom.h>
53 #include <geom/bde/g_bde.h>
56 * XXX: Debugging DO NOT ENABLE
61 * Derive kkey from mkey + sector offset.
63 * Security objective: Derive a potentially very large number of distinct skeys
64 * from the comparatively small key material in our mkey, in such a way that
65 * if one, more or even many of the kkeys are compromised, this does not
66 * significantly help an attack on other kkeys and in particular does not
67 * weaken or compromise the mkey.
69 * First we MD5 hash the sectornumber with the salt from the lock sector.
70 * The salt prevents the precalculation and statistical analysis of the MD5
71 * output which would be possible if we only gave it the sectornumber.
73 * The MD5 hash is used to pick out 16 bytes from the masterkey, which
74 * are then hashed with MD5 together with the sector number.
76 * The resulting MD5 hash is the kkey.
80 g_bde_kkey(struct g_bde_softc *sc, keyInstance *ki, int dir, off_t sector)
87 /* We have to be architecture neutral */
88 le64enc(buf2, sector);
91 MD5Update(&ct, sc->key.salt, 8);
92 MD5Update(&ct, buf2, sizeof buf2);
93 MD5Update(&ct, sc->key.salt + 8, 8);
97 for (t = 0; t < 16; t++) {
98 MD5Update(&ct, &sc->key.mkey[buf[t]], 1);
100 MD5Update(&ct, buf2, sizeof buf2);
102 bzero(buf2, sizeof buf2);
104 bzero(&ct, sizeof ct);
105 AES_makekey(ki, dir, G_BDE_KKEYBITS, buf);
106 bzero(buf, sizeof buf);
110 * Encryption work for read operation.
112 * Security objective: Find the kkey, find the skey, decrypt the sector data.
116 g_bde_crypt_read(struct g_bde_work *wp)
118 struct g_bde_softc *sc;
122 u_char skey[G_BDE_SKEYLEN];
130 for (n = 0; o < wp->length; n++, o += sc->sectorsize) {
131 d = (u_char *)wp->ksp->data + wp->ko + n * G_BDE_SKEYLEN;
132 g_bde_kkey(sc, &ki, DIR_DECRYPT, wp->offset + o);
133 AES_decrypt(&ci, &ki, d, skey, sizeof skey);
134 d = (u_char *)wp->data + o;
135 AES_makekey(&ki, DIR_DECRYPT, G_BDE_SKEYBITS, skey);
136 AES_decrypt(&ci, &ki, d, d, sc->sectorsize);
138 bzero(skey, sizeof skey);
139 bzero(&ci, sizeof ci);
140 bzero(&ki, sizeof ki);
144 * Encryption work for write operation.
146 * Security objective: Create random skey, encrypt sector data,
147 * encrypt skey with the kkey.
151 g_bde_crypt_write(struct g_bde_work *wp)
154 struct g_bde_softc *sc;
157 u_char skey[G_BDE_SKEYLEN];
164 for (n = 0; o < wp->length; n++, o += sc->sectorsize) {
166 s = (u_char *)wp->data + o;
167 d = (u_char *)wp->sp->data + o;
168 arc4rand(skey, sizeof skey, 0);
169 AES_makekey(&ki, DIR_ENCRYPT, G_BDE_SKEYBITS, skey);
170 AES_encrypt(&ci, &ki, s, d, sc->sectorsize);
172 d = (u_char *)wp->ksp->data + wp->ko + n * G_BDE_SKEYLEN;
173 g_bde_kkey(sc, &ki, DIR_ENCRYPT, wp->offset + o);
174 AES_encrypt(&ci, &ki, skey, d, sizeof skey);
175 bzero(skey, sizeof skey);
177 bzero(skey, sizeof skey);
178 bzero(&ci, sizeof ci);
179 bzero(&ki, sizeof ki);
183 * Encryption work for delete operation.
185 * Security objective: Write random data to the sectors.
187 * XXX: At a hit in performance we would trash the encrypted skey as well.
188 * XXX: This would add frustration to the cleaning lady attack by making
189 * XXX: deletes look like writes.
193 g_bde_crypt_delete(struct g_bde_work *wp)
195 struct g_bde_softc *sc;
198 u_char skey[G_BDE_SKEYLEN];
206 * Do not unroll this loop!
207 * Our zone may be significantly wider than the amount of random
208 * bytes arc4rand likes to give in one reseeding, whereas our
209 * sectorsize is far more likely to be in the same range.
211 for (o = 0; o < wp->length; o += sc->sectorsize) {
212 arc4rand(d, sc->sectorsize, 0);
213 arc4rand(skey, sizeof skey, 0);
214 AES_makekey(&ki, DIR_ENCRYPT, G_BDE_SKEYBITS, skey);
215 AES_encrypt(&ci, &ki, d, d, sc->sectorsize);
219 * Having written a long random sequence to disk here, we want to
220 * force a reseed, to avoid weakening the next time we use random
221 * data for something important.
223 arc4rand(&o, sizeof o, 1);
227 * Calculate the total payload size of the encrypted device.
229 * Security objectives: none.
231 * This function needs to agree with g_bde_map_sector() about things.
235 g_bde_max_sector(struct g_bde_key *kp)
239 maxsect = kp->media_width;
240 maxsect /= kp->zone_width;
241 maxsect *= kp->zone_cont;
246 * Convert an unencrypted side offset to offsets on the encrypted side.
248 * Security objective: Make it harder to identify what sectors contain what
249 * on a "cold" disk image.
251 * We do this by adding the "keyoffset" from the lock to the physical sector
252 * number modulus the available number of sectors. Since all physical sectors
253 * presumably look the same cold, this will do.
255 * As part of the mapping we have to skip the lock sectors which we know
256 * the physical address off. We also truncate the work packet, respecting
257 * zone boundaries and lock sectors, so that we end up with a sequence of
258 * sectors which are physically contiguous.
260 * Shuffling things further is an option, but the incremental frustration is
261 * not currently deemed worth the run-time performance hit resulting from the
262 * increased number of disk arm movements it would incur.
264 * This function offers nothing but a trivial diversion for an attacker able
265 * to do "the cleaning lady attack" in its current static mapping form.
269 g_bde_map_sector(struct g_bde_work *wp)
272 u_int zone, zoff, u, len;
274 struct g_bde_softc *sc;
275 struct g_bde_key *kp;
280 /* find which zone and the offset in it */
281 zone = wp->offset / kp->zone_cont;
282 zoff = wp->offset % kp->zone_cont;
284 /* Calculate the offset of the key in the key sector */
285 wp->ko = (zoff / kp->sectorsize) * G_BDE_SKEYLEN;
287 /* restrict length to that zone */
288 len = kp->zone_cont - zoff;
290 /* ... and in general */
294 if (len < wp->length)
297 /* Find physical sector address */
298 wp->so = zone * kp->zone_width + zoff;
299 wp->so += kp->keyoffset;
300 wp->so %= kp->media_width;
301 if (wp->so + wp->length > kp->media_width)
302 wp->length = kp->media_width - wp->so;
303 wp->so += kp->sector0;
305 /* The key sector is the last in this zone. */
306 wp->kso = zone * kp->zone_width + kp->zone_cont;
307 wp->kso += kp->keyoffset;
308 wp->kso %= kp->media_width;
309 wp->kso += kp->sector0;
311 /* Compensate for lock sectors */
312 for (u = 0; u < G_BDE_MAXKEYS; u++) {
313 /* Find the start of this lock sector */
314 ko = kp->lsector[u] & ~((uint64_t)kp->sectorsize - 1);
317 wp->kso += kp->sectorsize;
320 /* lock sector before work packet */
321 wp->so += kp->sectorsize;
322 } else if ((wp->so + wp->length) > ko) {
323 /* lock sector in work packet, truncate */
324 wp->length = ko - wp->so;
329 printf("off %jd len %jd so %jd ko %jd kso %u\n",
330 (intmax_t)wp->offset,
331 (intmax_t)wp->length,
336 KASSERT(wp->so + wp->length <= kp->sectorN,
337 ("wp->so (%jd) + wp->length (%jd) > EOM (%jd), offset = %jd",
339 (intmax_t)wp->length,
340 (intmax_t)kp->sectorN,
341 (intmax_t)wp->offset));
343 KASSERT(wp->kso + kp->sectorsize <= kp->sectorN,
344 ("wp->kso (%jd) + kp->sectorsize > EOM (%jd), offset = %jd",
346 (intmax_t)kp->sectorN,
347 (intmax_t)wp->offset));
349 KASSERT(wp->so >= kp->sector0,
350 ("wp->so (%jd) < BOM (%jd), offset = %jd",
352 (intmax_t)kp->sector0,
353 (intmax_t)wp->offset));
355 KASSERT(wp->kso >= kp->sector0,
356 ("wp->kso (%jd) <BOM (%jd), offset = %jd",
358 (intmax_t)kp->sector0,
359 (intmax_t)wp->offset));