]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/geom/bde/g_bde_crypt.c
zfs: merge openzfs/zfs@a94860a6d
[FreeBSD/FreeBSD.git] / sys / geom / bde / g_bde_crypt.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2002 Poul-Henning Kamp
5  * Copyright (c) 2002 Networks Associates Technology, Inc.
6  * All rights reserved.
7  *
8  * This software was developed for the FreeBSD Project by Poul-Henning Kamp
9  * and NAI Labs, the Security Research Division of Network Associates, Inc.
10  * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
11  * DARPA CHATS research program.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 /* This source file contains the functions responsible for the crypto, keying
35  * and mapping operations on the I/O requests.
36  *
37  */
38
39 #include <sys/param.h>
40 #include <sys/bio.h>
41 #include <sys/lock.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>
47 #include <sys/md5.h>
48
49 #include <crypto/rijndael/rijndael-api-fst.h>
50 #include <crypto/sha2/sha512.h>
51
52 #include <geom/geom.h>
53 #include <geom/bde/g_bde.h>
54
55 /*
56  * XXX: Debugging DO NOT ENABLE
57  */
58 #undef MD5_KEY
59
60 /*
61  * Derive kkey from mkey + sector offset.
62  *
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.
68  *
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.
72  *
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.
75  *
76  * The resulting MD5 hash is the kkey.
77  */
78
79 static void
80 g_bde_kkey(struct g_bde_softc *sc, keyInstance *ki, int dir, off_t sector)
81 {
82         u_int t;
83         MD5_CTX ct;
84         u_char buf[16];
85         u_char buf2[8];
86
87         /* We have to be architecture neutral */
88         le64enc(buf2, sector);
89
90         MD5Init(&ct);
91         MD5Update(&ct, sc->key.salt, 8);
92         MD5Update(&ct, buf2, sizeof buf2);
93         MD5Update(&ct, sc->key.salt + 8, 8);
94         MD5Final(buf, &ct);
95
96         MD5Init(&ct);
97         for (t = 0; t < 16; t++) {
98                 MD5Update(&ct, &sc->key.mkey[buf[t]], 1);
99                 if (t == 8)
100                         MD5Update(&ct, buf2, sizeof buf2);
101         }
102         bzero(buf2, sizeof buf2);
103         MD5Final(buf, &ct);
104         bzero(&ct, sizeof ct);
105         AES_makekey(ki, dir, G_BDE_KKEYBITS, buf);
106         bzero(buf, sizeof buf);
107 }
108
109 /*
110  * Encryption work for read operation.
111  *
112  * Security objective: Find the kkey, find the skey, decrypt the sector data.
113  */
114
115 void
116 g_bde_crypt_read(struct g_bde_work *wp)
117 {
118         struct g_bde_softc *sc;
119         u_char *d;
120         u_int n;
121         off_t o;
122         u_char skey[G_BDE_SKEYLEN];
123         keyInstance ki;
124         cipherInstance ci;
125
126         AES_init(&ci);
127         sc = wp->softc;
128         o = 0;
129         for (n = 0; o < wp->length; n++, o += sc->sectorsize) {
130                 d = (u_char *)wp->ksp->data + wp->ko + n * G_BDE_SKEYLEN;
131                 g_bde_kkey(sc, &ki, DIR_DECRYPT, wp->offset + o);
132                 AES_decrypt(&ci, &ki, d, skey, sizeof skey);
133                 d = (u_char *)wp->data + o;
134                 AES_makekey(&ki, DIR_DECRYPT, G_BDE_SKEYBITS, skey);
135                 AES_decrypt(&ci, &ki, d, d, sc->sectorsize);
136         }
137         bzero(skey, sizeof skey);
138         bzero(&ci, sizeof ci);
139         bzero(&ki, sizeof ki);
140 }
141
142 /*
143  * Encryption work for write operation.
144  *
145  * Security objective: Create random skey, encrypt sector data,
146  * encrypt skey with the kkey.
147  */
148
149 void
150 g_bde_crypt_write(struct g_bde_work *wp)
151 {
152         u_char *s, *d;
153         struct g_bde_softc *sc;
154         u_int n;
155         off_t o;
156         u_char skey[G_BDE_SKEYLEN];
157         keyInstance ki;
158         cipherInstance ci;
159
160         sc = wp->softc;
161         AES_init(&ci);
162         o = 0;
163         for (n = 0; o < wp->length; n++, o += sc->sectorsize) {
164                 s = (u_char *)wp->data + o;
165                 d = (u_char *)wp->sp->data + o;
166                 arc4rand(skey, sizeof skey, 0);
167                 AES_makekey(&ki, DIR_ENCRYPT, G_BDE_SKEYBITS, skey);
168                 AES_encrypt(&ci, &ki, s, d, sc->sectorsize);
169
170                 d = (u_char *)wp->ksp->data + wp->ko + n * G_BDE_SKEYLEN;
171                 g_bde_kkey(sc, &ki, DIR_ENCRYPT, wp->offset + o);
172                 AES_encrypt(&ci, &ki, skey, d, sizeof skey);
173                 bzero(skey, sizeof skey);
174         }
175         bzero(skey, sizeof skey);
176         bzero(&ci, sizeof ci);
177         bzero(&ki, sizeof ki);
178 }
179
180 /*
181  * Encryption work for delete operation.
182  *
183  * Security objective: Write random data to the sectors.
184  *
185  * XXX: At a hit in performance we would trash the encrypted skey as well.
186  * XXX: This would add frustration to the cleaning lady attack by making
187  * XXX: deletes look like writes.
188  */
189
190 void
191 g_bde_crypt_delete(struct g_bde_work *wp)
192 {
193         struct g_bde_softc *sc;
194         u_char *d;
195         off_t o;
196         u_char skey[G_BDE_SKEYLEN];
197         keyInstance ki;
198         cipherInstance ci;
199
200         sc = wp->softc;
201         d = wp->sp->data;
202         AES_init(&ci);
203         /*
204          * Do not unroll this loop!
205          * Our zone may be significantly wider than the amount of random
206          * bytes arc4rand likes to give in one reseeding, whereas our
207          * sectorsize is far more likely to be in the same range.
208          */
209         for (o = 0; o < wp->length; o += sc->sectorsize) {
210                 arc4rand(d, sc->sectorsize, 0);
211                 arc4rand(skey, sizeof skey, 0);
212                 AES_makekey(&ki, DIR_ENCRYPT, G_BDE_SKEYBITS, skey);
213                 AES_encrypt(&ci, &ki, d, d, sc->sectorsize);
214                 d += sc->sectorsize;
215         }
216         /*
217          * Having written a long random sequence to disk here, we want to
218          * force a reseed, to avoid weakening the next time we use random
219          * data for something important.
220          */
221         arc4rand(&o, sizeof o, 1);
222 }
223
224 /*
225  * Calculate the total payload size of the encrypted device.
226  *
227  * Security objectives: none.
228  *
229  * This function needs to agree with g_bde_map_sector() about things.
230  */
231
232 uint64_t
233 g_bde_max_sector(struct g_bde_key *kp)
234 {
235         uint64_t maxsect;
236
237         maxsect = kp->media_width;
238         maxsect /= kp->zone_width;
239         maxsect *= kp->zone_cont;
240         return (maxsect);
241 }
242
243 /*
244  * Convert an unencrypted side offset to offsets on the encrypted side.
245  *
246  * Security objective:  Make it harder to identify what sectors contain what
247  * on a "cold" disk image.
248  *
249  * We do this by adding the "keyoffset" from the lock to the physical sector
250  * number modulus the available number of sectors.  Since all physical sectors
251  * presumably look the same cold, this will do.
252  *
253  * As part of the mapping we have to skip the lock sectors which we know
254  * the physical address off.  We also truncate the work packet, respecting
255  * zone boundaries and lock sectors, so that we end up with a sequence of
256  * sectors which are physically contiguous.
257  *
258  * Shuffling things further is an option, but the incremental frustration is
259  * not currently deemed worth the run-time performance hit resulting from the
260  * increased number of disk arm movements it would incur.
261  *
262  * This function offers nothing but a trivial diversion for an attacker able
263  * to do "the cleaning lady attack" in its current static mapping form.
264  */
265
266 void
267 g_bde_map_sector(struct g_bde_work *wp)
268 {
269
270         u_int   zone, zoff, u, len;
271         uint64_t ko;
272         struct g_bde_softc *sc;
273         struct g_bde_key *kp;
274
275         sc = wp->softc;
276         kp = &sc->key;
277
278         /* find which zone and the offset in it */
279         zone = wp->offset / kp->zone_cont;
280         zoff = wp->offset % kp->zone_cont;
281
282         /* Calculate the offset of the key in the key sector */
283         wp->ko = (zoff / kp->sectorsize) * G_BDE_SKEYLEN;
284
285         /* restrict length to that zone */
286         len = kp->zone_cont - zoff;
287
288         /* ... and in general */
289         if (len > DFLTPHYS)
290                 len = DFLTPHYS;
291
292         if (len < wp->length)
293                 wp->length = len;
294
295         /* Find physical sector address */
296         wp->so = zone * kp->zone_width + zoff;
297         wp->so += kp->keyoffset;
298         wp->so %= kp->media_width;
299         if (wp->so + wp->length > kp->media_width)
300                 wp->length = kp->media_width - wp->so;
301         wp->so += kp->sector0;
302
303         /* The key sector is the last in this zone. */
304         wp->kso = zone * kp->zone_width + kp->zone_cont;
305         wp->kso += kp->keyoffset;
306         wp->kso %= kp->media_width;
307         wp->kso += kp->sector0; 
308
309         /* Compensate for lock sectors */
310         for (u = 0; u < G_BDE_MAXKEYS; u++) {
311                 /* Find the start of this lock sector */
312                 ko = rounddown2(kp->lsector[u], (uint64_t)kp->sectorsize);
313
314                 if (wp->kso >= ko)
315                         wp->kso += kp->sectorsize;
316
317                 if (wp->so >= ko) {
318                         /* lock sector before work packet */
319                         wp->so += kp->sectorsize;
320                 } else if ((wp->so + wp->length) > ko) {
321                         /* lock sector in work packet, truncate */
322                         wp->length = ko - wp->so;
323                 }
324         }
325
326 #if 0
327         printf("off %jd len %jd so %jd ko %jd kso %u\n",
328             (intmax_t)wp->offset,
329             (intmax_t)wp->length,
330             (intmax_t)wp->so,
331             (intmax_t)wp->kso,
332             wp->ko);
333 #endif
334         KASSERT(wp->so + wp->length <= kp->sectorN,
335             ("wp->so (%jd) + wp->length (%jd) > EOM (%jd), offset = %jd",
336             (intmax_t)wp->so,
337             (intmax_t)wp->length,
338             (intmax_t)kp->sectorN,
339             (intmax_t)wp->offset));
340
341         KASSERT(wp->kso + kp->sectorsize <= kp->sectorN,
342             ("wp->kso (%jd) + kp->sectorsize > EOM (%jd), offset = %jd",
343             (intmax_t)wp->kso,
344             (intmax_t)kp->sectorN,
345             (intmax_t)wp->offset));
346
347         KASSERT(wp->so >= kp->sector0,
348             ("wp->so (%jd) < BOM (%jd), offset = %jd",
349             (intmax_t)wp->so,
350             (intmax_t)kp->sector0,
351             (intmax_t)wp->offset));
352
353         KASSERT(wp->kso >= kp->sector0,
354             ("wp->kso (%jd) <BOM (%jd), offset = %jd",
355             (intmax_t)wp->kso,
356             (intmax_t)kp->sector0,
357             (intmax_t)wp->offset));
358 }