Deleted Added
full compact
g_eli_integrity.c (160569) g_eli_integrity.c (213067)
1/*-
2 * Copyright (c) 2005-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 2005-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/geom/eli/g_eli_integrity.c 160569 2006-07-22 10:05:55Z pjd $");
28__FBSDID("$FreeBSD: head/sys/geom/eli/g_eli_integrity.c 213067 2010-09-23 11:49:47Z pjd $");
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/kernel.h>
33#include <sys/linker.h>
34#include <sys/module.h>
35#include <sys/lock.h>
36#include <sys/mutex.h>
37#include <sys/bio.h>
38#include <sys/sysctl.h>
39#include <sys/malloc.h>
40#include <sys/kthread.h>
41#include <sys/proc.h>
42#include <sys/sched.h>
43#include <sys/smp.h>
44#include <sys/uio.h>
45#include <sys/vnode.h>
46
47#include <vm/uma.h>
48
49#include <geom/geom.h>
50#include <geom/eli/g_eli.h>
51#include <geom/eli/pkcs5v2.h>
52
53/*
54 * The data layout description when integrity verification is configured.
55 *
56 * One of the most important assumption here is that authenticated data and its
57 * HMAC has to be stored in the same place (namely in the same sector) to make
58 * it work reliable.
59 * The problem is that file systems work only with sectors that are multiple of
60 * 512 bytes and a power of two number.
61 * My idea to implement it is as follows.
62 * Let's store HMAC in sector. This is a must. This leaves us 480 bytes for
63 * data. We can't use that directly (ie. we can't create provider with 480 bytes
64 * sector size). We need another sector from where we take only 32 bytes of data
65 * and we store HMAC of this data as well. This takes two sectors from the
66 * original provider at the input and leaves us one sector of authenticated data
67 * at the output. Not very efficient, but you got the idea.
68 * Now, let's assume, we want to create provider with 4096 bytes sector.
69 * To output 4096 bytes of authenticated data we need 8x480 plus 1x256, so we
70 * need nine 512-bytes sectors at the input to get one 4096-bytes sector at the
71 * output. That's better. With 4096 bytes sector we can use 89% of size of the
72 * original provider. I find it as an acceptable cost.
73 * The reliability comes from the fact, that every HMAC stored inside the sector
74 * is calculated only for the data in the same sector, so its impossible to
75 * write new data and leave old HMAC or vice versa.
76 *
77 * And here is the picture:
78 *
79 * da0: +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+-----+
80 * |32b |480b| |32b |480b| |32b |480b| |32b |480b| |32b |480b| |32b |480b| |32b |480b| |32b |480b| |32b |256b |
81 * |HMAC|Data| |HMAC|Data| |HMAC|Data| |HMAC|Data| |HMAC|Data| |HMAC|Data| |HMAC|Data| |HMAC|Data| |HMAC|Data |
82 * +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+-----+
83 * |512 bytes| |512 bytes| |512 bytes| |512 bytes| |512 bytes| |512 bytes| |512 bytes| |512 bytes| |288 bytes |
84 * +---------+ +---------+ +---------+ +---------+ +---------+ +---------+ +---------+ +---------+ |224 unused|
85 * +----------+
86 * da0.eli: +----+----+----+----+----+----+----+----+----+
87 * |480b|480b|480b|480b|480b|480b|480b|480b|256b|
88 * +----+----+----+----+----+----+----+----+----+
89 * | 4096 bytes |
90 * +--------------------------------------------+
91 *
92 * PS. You can use any sector size with geli(8). My example is using 4kB,
93 * because it's most efficient. For 8kB sectors you need 2 extra sectors,
94 * so the cost is the same as for 4kB sectors.
95 */
96
97/*
98 * Code paths:
99 * BIO_READ:
100 * g_eli_start -> g_eli_auth_read -> g_io_request -> g_eli_read_done -> g_eli_auth_run -> g_eli_auth_read_done -> g_io_deliver
101 * BIO_WRITE:
102 * g_eli_start -> g_eli_auth_run -> g_eli_auth_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
103 */
104
105MALLOC_DECLARE(M_ELI);
106
107/*
108 * Here we generate key for HMAC. Every sector has its own HMAC key, so it is
109 * not possible to copy sectors.
110 * We cannot depend on fact, that every sector has its own IV, because different
111 * IV doesn't change HMAC, when we use encrypt-then-authenticate method.
112 */
113static void
114g_eli_auth_keygen(struct g_eli_softc *sc, off_t offset, u_char *key)
115{
116 SHA256_CTX ctx;
117
118 /* Copy precalculated SHA256 context. */
119 bcopy(&sc->sc_akeyctx, &ctx, sizeof(ctx));
120 SHA256_Update(&ctx, (uint8_t *)&offset, sizeof(offset));
121 SHA256_Final(key, &ctx);
122}
123
124/*
125 * The function is called after we read and decrypt data.
126 *
127 * g_eli_start -> g_eli_auth_read -> g_io_request -> g_eli_read_done -> g_eli_auth_run -> G_ELI_AUTH_READ_DONE -> g_io_deliver
128 */
129static int
130g_eli_auth_read_done(struct cryptop *crp)
131{
132 struct bio *bp;
133
134 if (crp->crp_etype == EAGAIN) {
135 if (g_eli_crypto_rerun(crp) == 0)
136 return (0);
137 }
138 bp = (struct bio *)crp->crp_opaque;
139 bp->bio_inbed++;
140 if (crp->crp_etype == 0) {
141 bp->bio_completed += crp->crp_olen;
142 G_ELI_DEBUG(3, "Crypto READ request done (%d/%d) (add=%jd completed=%jd).",
143 bp->bio_inbed, bp->bio_children, (intmax_t)crp->crp_olen, (intmax_t)bp->bio_completed);
144 } else {
145 G_ELI_DEBUG(1, "Crypto READ request failed (%d/%d) error=%d.",
146 bp->bio_inbed, bp->bio_children, crp->crp_etype);
147 if (bp->bio_error == 0)
148 bp->bio_error = crp->crp_etype;
149 }
150 /*
151 * Do we have all sectors already?
152 */
153 if (bp->bio_inbed < bp->bio_children)
154 return (0);
155 if (bp->bio_error == 0) {
156 struct g_eli_softc *sc;
157 u_int i, lsec, nsec, data_secsize, decr_secsize, encr_secsize;
158 u_char *srcdata, *dstdata, *auth;
159 off_t coroff, corsize;
160
161 /*
162 * Verify data integrity based on calculated and read HMACs.
163 */
164 sc = bp->bio_to->geom->softc;
165 /* Sectorsize of decrypted provider eg. 4096. */
166 decr_secsize = bp->bio_to->sectorsize;
167 /* The real sectorsize of encrypted provider, eg. 512. */
168 encr_secsize = LIST_FIRST(&sc->sc_geom->consumer)->provider->sectorsize;
169 /* Number of data bytes in one encrypted sector, eg. 480. */
170 data_secsize = sc->sc_data_per_sector;
171 /* Number of sectors from decrypted provider, eg. 2. */
172 nsec = bp->bio_length / decr_secsize;
173 /* Number of sectors from encrypted provider, eg. 18. */
174 nsec = (nsec * sc->sc_bytes_per_sector) / encr_secsize;
175 /* Last sector number in every big sector, eg. 9. */
176 lsec = sc->sc_bytes_per_sector / encr_secsize;
177
178 srcdata = bp->bio_driver2;
179 dstdata = bp->bio_data;
180 auth = srcdata + encr_secsize * nsec;
181 coroff = -1;
182 corsize = 0;
183
184 for (i = 1; i <= nsec; i++) {
185 data_secsize = sc->sc_data_per_sector;
186 if ((i % lsec) == 0)
187 data_secsize = decr_secsize % data_secsize;
188 if (bcmp(srcdata, auth, sc->sc_alen) != 0) {
189 /*
190 * Curruption detected, remember the offset if
191 * this is the first corrupted sector and
192 * increase size.
193 */
194 if (bp->bio_error == 0)
195 bp->bio_error = -1;
196 if (coroff == -1) {
197 coroff = bp->bio_offset +
198 (dstdata - (u_char *)bp->bio_data);
199 }
200 corsize += data_secsize;
201 } else {
202 /*
203 * No curruption, good.
204 * Report previous corruption if there was one.
205 */
206 if (coroff != -1) {
207 G_ELI_DEBUG(0, "%s: %jd bytes "
208 "corrupted at offset %jd.",
209 sc->sc_name, (intmax_t)corsize,
210 (intmax_t)coroff);
211 coroff = -1;
212 corsize = 0;
213 }
214 bcopy(srcdata + sc->sc_alen, dstdata,
215 data_secsize);
216 }
217 srcdata += encr_secsize;
218 dstdata += data_secsize;
219 auth += sc->sc_alen;
220 }
221 /* Report previous corruption if there was one. */
222 if (coroff != -1) {
223 G_ELI_DEBUG(0, "%s: %jd bytes corrupted at offset %jd.",
224 sc->sc_name, (intmax_t)corsize, (intmax_t)coroff);
225 }
226 }
227 free(bp->bio_driver2, M_ELI);
228 bp->bio_driver2 = NULL;
229 if (bp->bio_error != 0) {
230 if (bp->bio_error == -1)
231 bp->bio_error = EINVAL;
232 else {
233 G_ELI_LOGREQ(0, bp,
234 "Crypto READ request failed (error=%d).",
235 bp->bio_error);
236 }
237 bp->bio_completed = 0;
238 }
239 /*
240 * Read is finished, send it up.
241 */
242 g_io_deliver(bp, bp->bio_error);
243 return (0);
244}
245
246/*
247 * The function is called after data encryption.
248 *
249 * g_eli_start -> g_eli_auth_run -> G_ELI_AUTH_WRITE_DONE -> g_io_request -> g_eli_write_done -> g_io_deliver
250 */
251static int
252g_eli_auth_write_done(struct cryptop *crp)
253{
254 struct g_eli_softc *sc;
255 struct g_consumer *cp;
256 struct bio *bp, *cbp, *cbp2;
257 u_int nsec;
258
259 if (crp->crp_etype == EAGAIN) {
260 if (g_eli_crypto_rerun(crp) == 0)
261 return (0);
262 }
263 bp = (struct bio *)crp->crp_opaque;
264 bp->bio_inbed++;
265 if (crp->crp_etype == 0) {
266 G_ELI_DEBUG(3, "Crypto WRITE request done (%d/%d).",
267 bp->bio_inbed, bp->bio_children);
268 } else {
269 G_ELI_DEBUG(1, "Crypto WRITE request failed (%d/%d) error=%d.",
270 bp->bio_inbed, bp->bio_children, crp->crp_etype);
271 if (bp->bio_error == 0)
272 bp->bio_error = crp->crp_etype;
273 }
274 /*
275 * All sectors are already encrypted?
276 */
277 if (bp->bio_inbed < bp->bio_children)
278 return (0);
279 if (bp->bio_error != 0) {
280 G_ELI_LOGREQ(0, bp, "Crypto WRITE request failed (error=%d).",
281 bp->bio_error);
282 free(bp->bio_driver2, M_ELI);
283 bp->bio_driver2 = NULL;
284 cbp = bp->bio_driver1;
285 bp->bio_driver1 = NULL;
286 g_destroy_bio(cbp);
287 g_io_deliver(bp, bp->bio_error);
288 return (0);
289 }
290 sc = bp->bio_to->geom->softc;
291 cp = LIST_FIRST(&sc->sc_geom->consumer);
292 cbp = bp->bio_driver1;
293 bp->bio_driver1 = NULL;
294 cbp->bio_to = cp->provider;
295 cbp->bio_done = g_eli_write_done;
296
297 /* Number of sectors from decrypted provider, eg. 1. */
298 nsec = bp->bio_length / bp->bio_to->sectorsize;
299 /* Number of sectors from encrypted provider, eg. 9. */
300 nsec = (nsec * sc->sc_bytes_per_sector) / cp->provider->sectorsize;
301
302 cbp->bio_length = cp->provider->sectorsize * nsec;
303 cbp->bio_offset = (bp->bio_offset / bp->bio_to->sectorsize) * sc->sc_bytes_per_sector;
304 cbp->bio_data = bp->bio_driver2;
305
306 /*
307 * We write more than what is requested, so we have to be ready to write
308 * more than MAXPHYS.
309 */
310 cbp2 = NULL;
311 if (cbp->bio_length > MAXPHYS) {
312 cbp2 = g_duplicate_bio(bp);
313 cbp2->bio_length = cbp->bio_length - MAXPHYS;
314 cbp2->bio_data = cbp->bio_data + MAXPHYS;
315 cbp2->bio_offset = cbp->bio_offset + MAXPHYS;
316 cbp2->bio_to = cp->provider;
317 cbp2->bio_done = g_eli_write_done;
318 cbp->bio_length = MAXPHYS;
319 }
320 /*
321 * Send encrypted data to the provider.
322 */
323 G_ELI_LOGREQ(2, cbp, "Sending request.");
324 bp->bio_inbed = 0;
325 bp->bio_children = (cbp2 != NULL ? 2 : 1);
326 g_io_request(cbp, cp);
327 if (cbp2 != NULL) {
328 G_ELI_LOGREQ(2, cbp2, "Sending request.");
329 g_io_request(cbp2, cp);
330 }
331 return (0);
332}
333
334void
335g_eli_auth_read(struct g_eli_softc *sc, struct bio *bp)
336{
337 struct g_consumer *cp;
338 struct bio *cbp, *cbp2;
339 size_t size;
340 off_t nsec;
341
342 bp->bio_pflags = 0;
343
344 cp = LIST_FIRST(&sc->sc_geom->consumer);
345 cbp = bp->bio_driver1;
346 bp->bio_driver1 = NULL;
347 cbp->bio_to = cp->provider;
348 cbp->bio_done = g_eli_read_done;
349
350 /* Number of sectors from decrypted provider, eg. 1. */
351 nsec = bp->bio_length / bp->bio_to->sectorsize;
352 /* Number of sectors from encrypted provider, eg. 9. */
353 nsec = (nsec * sc->sc_bytes_per_sector) / cp->provider->sectorsize;
354
355 cbp->bio_length = cp->provider->sectorsize * nsec;
356 size = cbp->bio_length;
357 size += sc->sc_alen * nsec;
358 size += sizeof(struct cryptop) * nsec;
359 size += sizeof(struct cryptodesc) * nsec * 2;
360 size += G_ELI_AUTH_SECKEYLEN * nsec;
361 size += sizeof(struct uio) * nsec;
362 size += sizeof(struct iovec) * nsec;
363 cbp->bio_offset = (bp->bio_offset / bp->bio_to->sectorsize) * sc->sc_bytes_per_sector;
364 bp->bio_driver2 = malloc(size, M_ELI, M_WAITOK);
365 cbp->bio_data = bp->bio_driver2;
366
367 /*
368 * We read more than what is requested, so we have to be ready to read
369 * more than MAXPHYS.
370 */
371 cbp2 = NULL;
372 if (cbp->bio_length > MAXPHYS) {
373 cbp2 = g_duplicate_bio(bp);
374 cbp2->bio_length = cbp->bio_length - MAXPHYS;
375 cbp2->bio_data = cbp->bio_data + MAXPHYS;
376 cbp2->bio_offset = cbp->bio_offset + MAXPHYS;
377 cbp2->bio_to = cp->provider;
378 cbp2->bio_done = g_eli_read_done;
379 cbp->bio_length = MAXPHYS;
380 }
381 /*
382 * Read encrypted data from provider.
383 */
384 G_ELI_LOGREQ(2, cbp, "Sending request.");
385 g_io_request(cbp, cp);
386 if (cbp2 != NULL) {
387 G_ELI_LOGREQ(2, cbp2, "Sending request.");
388 g_io_request(cbp2, cp);
389 }
390}
391
392/*
393 * This is the main function responsible for cryptography (ie. communication
394 * with crypto(9) subsystem).
395 */
396void
397g_eli_auth_run(struct g_eli_worker *wr, struct bio *bp)
398{
399 struct g_eli_softc *sc;
400 struct cryptop *crp;
401 struct cryptodesc *crde, *crda;
402 struct uio *uio;
403 struct iovec *iov;
404 u_int i, lsec, nsec, data_secsize, decr_secsize, encr_secsize;
405 off_t dstoff;
406 int err, error;
407 u_char *p, *data, *auth, *authkey, *plaindata;
408
409 G_ELI_LOGREQ(3, bp, "%s", __func__);
410
411 bp->bio_pflags = wr->w_number;
412 sc = wr->w_softc;
413 /* Sectorsize of decrypted provider eg. 4096. */
414 decr_secsize = bp->bio_to->sectorsize;
415 /* The real sectorsize of encrypted provider, eg. 512. */
416 encr_secsize = LIST_FIRST(&sc->sc_geom->consumer)->provider->sectorsize;
417 /* Number of data bytes in one encrypted sector, eg. 480. */
418 data_secsize = sc->sc_data_per_sector;
419 /* Number of sectors from decrypted provider, eg. 2. */
420 nsec = bp->bio_length / decr_secsize;
421 /* Number of sectors from encrypted provider, eg. 18. */
422 nsec = (nsec * sc->sc_bytes_per_sector) / encr_secsize;
423 /* Last sector number in every big sector, eg. 9. */
424 lsec = sc->sc_bytes_per_sector / encr_secsize;
425 /* Destination offset, used for IV generation. */
426 dstoff = (bp->bio_offset / bp->bio_to->sectorsize) * sc->sc_bytes_per_sector;
427
428 auth = NULL; /* Silence compiler warning. */
429 plaindata = bp->bio_data;
430 if (bp->bio_cmd == BIO_READ) {
431 data = bp->bio_driver2;
432 auth = data + encr_secsize * nsec;
433 p = auth + sc->sc_alen * nsec;
434 } else {
435 size_t size;
436
437 size = encr_secsize * nsec;
438 size += sizeof(*crp) * nsec;
439 size += sizeof(*crde) * nsec;
440 size += sizeof(*crda) * nsec;
441 size += G_ELI_AUTH_SECKEYLEN * nsec;
442 size += sizeof(*uio) * nsec;
443 size += sizeof(*iov) * nsec;
444 data = malloc(size, M_ELI, M_WAITOK);
445 bp->bio_driver2 = data;
446 p = data + encr_secsize * nsec;
447 }
448 bp->bio_inbed = 0;
449 bp->bio_children = nsec;
450
451 error = 0;
452 for (i = 1; i <= nsec; i++, dstoff += encr_secsize) {
453 crp = (struct cryptop *)p; p += sizeof(*crp);
454 crde = (struct cryptodesc *)p; p += sizeof(*crde);
455 crda = (struct cryptodesc *)p; p += sizeof(*crda);
456 authkey = (u_char *)p; p += G_ELI_AUTH_SECKEYLEN;
457 uio = (struct uio *)p; p += sizeof(*uio);
458 iov = (struct iovec *)p; p += sizeof(*iov);
459
460 data_secsize = sc->sc_data_per_sector;
461 if ((i % lsec) == 0)
462 data_secsize = decr_secsize % data_secsize;
463
464 if (bp->bio_cmd == BIO_READ) {
465 /* Remember read HMAC. */
466 bcopy(data, auth, sc->sc_alen);
467 auth += sc->sc_alen;
468 /* TODO: bzero(9) can be commented out later. */
469 bzero(data, sc->sc_alen);
470 } else {
471 bcopy(plaindata, data + sc->sc_alen, data_secsize);
472 plaindata += data_secsize;
473 }
474
475 iov->iov_len = sc->sc_alen + data_secsize;
476 iov->iov_base = data;
477 data += encr_secsize;
478
479 uio->uio_iov = iov;
480 uio->uio_iovcnt = 1;
481 uio->uio_segflg = UIO_SYSSPACE;
482 uio->uio_resid = iov->iov_len;
483
484 crp->crp_sid = wr->w_sid;
485 crp->crp_ilen = uio->uio_resid;
486 crp->crp_olen = data_secsize;
487 crp->crp_opaque = (void *)bp;
488 crp->crp_buf = (void *)uio;
489 crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIFSYNC | CRYPTO_F_REL;
490 if (g_eli_batch)
491 crp->crp_flags |= CRYPTO_F_BATCH;
492 if (bp->bio_cmd == BIO_WRITE) {
493 crp->crp_callback = g_eli_auth_write_done;
494 crp->crp_desc = crde;
495 crde->crd_next = crda;
496 crda->crd_next = NULL;
497 } else {
498 crp->crp_callback = g_eli_auth_read_done;
499 crp->crp_desc = crda;
500 crda->crd_next = crde;
501 crde->crd_next = NULL;
502 }
503
504 crde->crd_skip = sc->sc_alen;
505 crde->crd_len = data_secsize;
506 crde->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
507 if (bp->bio_cmd == BIO_WRITE)
508 crde->crd_flags |= CRD_F_ENCRYPT;
509 crde->crd_alg = sc->sc_ealgo;
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/kernel.h>
33#include <sys/linker.h>
34#include <sys/module.h>
35#include <sys/lock.h>
36#include <sys/mutex.h>
37#include <sys/bio.h>
38#include <sys/sysctl.h>
39#include <sys/malloc.h>
40#include <sys/kthread.h>
41#include <sys/proc.h>
42#include <sys/sched.h>
43#include <sys/smp.h>
44#include <sys/uio.h>
45#include <sys/vnode.h>
46
47#include <vm/uma.h>
48
49#include <geom/geom.h>
50#include <geom/eli/g_eli.h>
51#include <geom/eli/pkcs5v2.h>
52
53/*
54 * The data layout description when integrity verification is configured.
55 *
56 * One of the most important assumption here is that authenticated data and its
57 * HMAC has to be stored in the same place (namely in the same sector) to make
58 * it work reliable.
59 * The problem is that file systems work only with sectors that are multiple of
60 * 512 bytes and a power of two number.
61 * My idea to implement it is as follows.
62 * Let's store HMAC in sector. This is a must. This leaves us 480 bytes for
63 * data. We can't use that directly (ie. we can't create provider with 480 bytes
64 * sector size). We need another sector from where we take only 32 bytes of data
65 * and we store HMAC of this data as well. This takes two sectors from the
66 * original provider at the input and leaves us one sector of authenticated data
67 * at the output. Not very efficient, but you got the idea.
68 * Now, let's assume, we want to create provider with 4096 bytes sector.
69 * To output 4096 bytes of authenticated data we need 8x480 plus 1x256, so we
70 * need nine 512-bytes sectors at the input to get one 4096-bytes sector at the
71 * output. That's better. With 4096 bytes sector we can use 89% of size of the
72 * original provider. I find it as an acceptable cost.
73 * The reliability comes from the fact, that every HMAC stored inside the sector
74 * is calculated only for the data in the same sector, so its impossible to
75 * write new data and leave old HMAC or vice versa.
76 *
77 * And here is the picture:
78 *
79 * da0: +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+-----+
80 * |32b |480b| |32b |480b| |32b |480b| |32b |480b| |32b |480b| |32b |480b| |32b |480b| |32b |480b| |32b |256b |
81 * |HMAC|Data| |HMAC|Data| |HMAC|Data| |HMAC|Data| |HMAC|Data| |HMAC|Data| |HMAC|Data| |HMAC|Data| |HMAC|Data |
82 * +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+-----+
83 * |512 bytes| |512 bytes| |512 bytes| |512 bytes| |512 bytes| |512 bytes| |512 bytes| |512 bytes| |288 bytes |
84 * +---------+ +---------+ +---------+ +---------+ +---------+ +---------+ +---------+ +---------+ |224 unused|
85 * +----------+
86 * da0.eli: +----+----+----+----+----+----+----+----+----+
87 * |480b|480b|480b|480b|480b|480b|480b|480b|256b|
88 * +----+----+----+----+----+----+----+----+----+
89 * | 4096 bytes |
90 * +--------------------------------------------+
91 *
92 * PS. You can use any sector size with geli(8). My example is using 4kB,
93 * because it's most efficient. For 8kB sectors you need 2 extra sectors,
94 * so the cost is the same as for 4kB sectors.
95 */
96
97/*
98 * Code paths:
99 * BIO_READ:
100 * g_eli_start -> g_eli_auth_read -> g_io_request -> g_eli_read_done -> g_eli_auth_run -> g_eli_auth_read_done -> g_io_deliver
101 * BIO_WRITE:
102 * g_eli_start -> g_eli_auth_run -> g_eli_auth_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
103 */
104
105MALLOC_DECLARE(M_ELI);
106
107/*
108 * Here we generate key for HMAC. Every sector has its own HMAC key, so it is
109 * not possible to copy sectors.
110 * We cannot depend on fact, that every sector has its own IV, because different
111 * IV doesn't change HMAC, when we use encrypt-then-authenticate method.
112 */
113static void
114g_eli_auth_keygen(struct g_eli_softc *sc, off_t offset, u_char *key)
115{
116 SHA256_CTX ctx;
117
118 /* Copy precalculated SHA256 context. */
119 bcopy(&sc->sc_akeyctx, &ctx, sizeof(ctx));
120 SHA256_Update(&ctx, (uint8_t *)&offset, sizeof(offset));
121 SHA256_Final(key, &ctx);
122}
123
124/*
125 * The function is called after we read and decrypt data.
126 *
127 * g_eli_start -> g_eli_auth_read -> g_io_request -> g_eli_read_done -> g_eli_auth_run -> G_ELI_AUTH_READ_DONE -> g_io_deliver
128 */
129static int
130g_eli_auth_read_done(struct cryptop *crp)
131{
132 struct bio *bp;
133
134 if (crp->crp_etype == EAGAIN) {
135 if (g_eli_crypto_rerun(crp) == 0)
136 return (0);
137 }
138 bp = (struct bio *)crp->crp_opaque;
139 bp->bio_inbed++;
140 if (crp->crp_etype == 0) {
141 bp->bio_completed += crp->crp_olen;
142 G_ELI_DEBUG(3, "Crypto READ request done (%d/%d) (add=%jd completed=%jd).",
143 bp->bio_inbed, bp->bio_children, (intmax_t)crp->crp_olen, (intmax_t)bp->bio_completed);
144 } else {
145 G_ELI_DEBUG(1, "Crypto READ request failed (%d/%d) error=%d.",
146 bp->bio_inbed, bp->bio_children, crp->crp_etype);
147 if (bp->bio_error == 0)
148 bp->bio_error = crp->crp_etype;
149 }
150 /*
151 * Do we have all sectors already?
152 */
153 if (bp->bio_inbed < bp->bio_children)
154 return (0);
155 if (bp->bio_error == 0) {
156 struct g_eli_softc *sc;
157 u_int i, lsec, nsec, data_secsize, decr_secsize, encr_secsize;
158 u_char *srcdata, *dstdata, *auth;
159 off_t coroff, corsize;
160
161 /*
162 * Verify data integrity based on calculated and read HMACs.
163 */
164 sc = bp->bio_to->geom->softc;
165 /* Sectorsize of decrypted provider eg. 4096. */
166 decr_secsize = bp->bio_to->sectorsize;
167 /* The real sectorsize of encrypted provider, eg. 512. */
168 encr_secsize = LIST_FIRST(&sc->sc_geom->consumer)->provider->sectorsize;
169 /* Number of data bytes in one encrypted sector, eg. 480. */
170 data_secsize = sc->sc_data_per_sector;
171 /* Number of sectors from decrypted provider, eg. 2. */
172 nsec = bp->bio_length / decr_secsize;
173 /* Number of sectors from encrypted provider, eg. 18. */
174 nsec = (nsec * sc->sc_bytes_per_sector) / encr_secsize;
175 /* Last sector number in every big sector, eg. 9. */
176 lsec = sc->sc_bytes_per_sector / encr_secsize;
177
178 srcdata = bp->bio_driver2;
179 dstdata = bp->bio_data;
180 auth = srcdata + encr_secsize * nsec;
181 coroff = -1;
182 corsize = 0;
183
184 for (i = 1; i <= nsec; i++) {
185 data_secsize = sc->sc_data_per_sector;
186 if ((i % lsec) == 0)
187 data_secsize = decr_secsize % data_secsize;
188 if (bcmp(srcdata, auth, sc->sc_alen) != 0) {
189 /*
190 * Curruption detected, remember the offset if
191 * this is the first corrupted sector and
192 * increase size.
193 */
194 if (bp->bio_error == 0)
195 bp->bio_error = -1;
196 if (coroff == -1) {
197 coroff = bp->bio_offset +
198 (dstdata - (u_char *)bp->bio_data);
199 }
200 corsize += data_secsize;
201 } else {
202 /*
203 * No curruption, good.
204 * Report previous corruption if there was one.
205 */
206 if (coroff != -1) {
207 G_ELI_DEBUG(0, "%s: %jd bytes "
208 "corrupted at offset %jd.",
209 sc->sc_name, (intmax_t)corsize,
210 (intmax_t)coroff);
211 coroff = -1;
212 corsize = 0;
213 }
214 bcopy(srcdata + sc->sc_alen, dstdata,
215 data_secsize);
216 }
217 srcdata += encr_secsize;
218 dstdata += data_secsize;
219 auth += sc->sc_alen;
220 }
221 /* Report previous corruption if there was one. */
222 if (coroff != -1) {
223 G_ELI_DEBUG(0, "%s: %jd bytes corrupted at offset %jd.",
224 sc->sc_name, (intmax_t)corsize, (intmax_t)coroff);
225 }
226 }
227 free(bp->bio_driver2, M_ELI);
228 bp->bio_driver2 = NULL;
229 if (bp->bio_error != 0) {
230 if (bp->bio_error == -1)
231 bp->bio_error = EINVAL;
232 else {
233 G_ELI_LOGREQ(0, bp,
234 "Crypto READ request failed (error=%d).",
235 bp->bio_error);
236 }
237 bp->bio_completed = 0;
238 }
239 /*
240 * Read is finished, send it up.
241 */
242 g_io_deliver(bp, bp->bio_error);
243 return (0);
244}
245
246/*
247 * The function is called after data encryption.
248 *
249 * g_eli_start -> g_eli_auth_run -> G_ELI_AUTH_WRITE_DONE -> g_io_request -> g_eli_write_done -> g_io_deliver
250 */
251static int
252g_eli_auth_write_done(struct cryptop *crp)
253{
254 struct g_eli_softc *sc;
255 struct g_consumer *cp;
256 struct bio *bp, *cbp, *cbp2;
257 u_int nsec;
258
259 if (crp->crp_etype == EAGAIN) {
260 if (g_eli_crypto_rerun(crp) == 0)
261 return (0);
262 }
263 bp = (struct bio *)crp->crp_opaque;
264 bp->bio_inbed++;
265 if (crp->crp_etype == 0) {
266 G_ELI_DEBUG(3, "Crypto WRITE request done (%d/%d).",
267 bp->bio_inbed, bp->bio_children);
268 } else {
269 G_ELI_DEBUG(1, "Crypto WRITE request failed (%d/%d) error=%d.",
270 bp->bio_inbed, bp->bio_children, crp->crp_etype);
271 if (bp->bio_error == 0)
272 bp->bio_error = crp->crp_etype;
273 }
274 /*
275 * All sectors are already encrypted?
276 */
277 if (bp->bio_inbed < bp->bio_children)
278 return (0);
279 if (bp->bio_error != 0) {
280 G_ELI_LOGREQ(0, bp, "Crypto WRITE request failed (error=%d).",
281 bp->bio_error);
282 free(bp->bio_driver2, M_ELI);
283 bp->bio_driver2 = NULL;
284 cbp = bp->bio_driver1;
285 bp->bio_driver1 = NULL;
286 g_destroy_bio(cbp);
287 g_io_deliver(bp, bp->bio_error);
288 return (0);
289 }
290 sc = bp->bio_to->geom->softc;
291 cp = LIST_FIRST(&sc->sc_geom->consumer);
292 cbp = bp->bio_driver1;
293 bp->bio_driver1 = NULL;
294 cbp->bio_to = cp->provider;
295 cbp->bio_done = g_eli_write_done;
296
297 /* Number of sectors from decrypted provider, eg. 1. */
298 nsec = bp->bio_length / bp->bio_to->sectorsize;
299 /* Number of sectors from encrypted provider, eg. 9. */
300 nsec = (nsec * sc->sc_bytes_per_sector) / cp->provider->sectorsize;
301
302 cbp->bio_length = cp->provider->sectorsize * nsec;
303 cbp->bio_offset = (bp->bio_offset / bp->bio_to->sectorsize) * sc->sc_bytes_per_sector;
304 cbp->bio_data = bp->bio_driver2;
305
306 /*
307 * We write more than what is requested, so we have to be ready to write
308 * more than MAXPHYS.
309 */
310 cbp2 = NULL;
311 if (cbp->bio_length > MAXPHYS) {
312 cbp2 = g_duplicate_bio(bp);
313 cbp2->bio_length = cbp->bio_length - MAXPHYS;
314 cbp2->bio_data = cbp->bio_data + MAXPHYS;
315 cbp2->bio_offset = cbp->bio_offset + MAXPHYS;
316 cbp2->bio_to = cp->provider;
317 cbp2->bio_done = g_eli_write_done;
318 cbp->bio_length = MAXPHYS;
319 }
320 /*
321 * Send encrypted data to the provider.
322 */
323 G_ELI_LOGREQ(2, cbp, "Sending request.");
324 bp->bio_inbed = 0;
325 bp->bio_children = (cbp2 != NULL ? 2 : 1);
326 g_io_request(cbp, cp);
327 if (cbp2 != NULL) {
328 G_ELI_LOGREQ(2, cbp2, "Sending request.");
329 g_io_request(cbp2, cp);
330 }
331 return (0);
332}
333
334void
335g_eli_auth_read(struct g_eli_softc *sc, struct bio *bp)
336{
337 struct g_consumer *cp;
338 struct bio *cbp, *cbp2;
339 size_t size;
340 off_t nsec;
341
342 bp->bio_pflags = 0;
343
344 cp = LIST_FIRST(&sc->sc_geom->consumer);
345 cbp = bp->bio_driver1;
346 bp->bio_driver1 = NULL;
347 cbp->bio_to = cp->provider;
348 cbp->bio_done = g_eli_read_done;
349
350 /* Number of sectors from decrypted provider, eg. 1. */
351 nsec = bp->bio_length / bp->bio_to->sectorsize;
352 /* Number of sectors from encrypted provider, eg. 9. */
353 nsec = (nsec * sc->sc_bytes_per_sector) / cp->provider->sectorsize;
354
355 cbp->bio_length = cp->provider->sectorsize * nsec;
356 size = cbp->bio_length;
357 size += sc->sc_alen * nsec;
358 size += sizeof(struct cryptop) * nsec;
359 size += sizeof(struct cryptodesc) * nsec * 2;
360 size += G_ELI_AUTH_SECKEYLEN * nsec;
361 size += sizeof(struct uio) * nsec;
362 size += sizeof(struct iovec) * nsec;
363 cbp->bio_offset = (bp->bio_offset / bp->bio_to->sectorsize) * sc->sc_bytes_per_sector;
364 bp->bio_driver2 = malloc(size, M_ELI, M_WAITOK);
365 cbp->bio_data = bp->bio_driver2;
366
367 /*
368 * We read more than what is requested, so we have to be ready to read
369 * more than MAXPHYS.
370 */
371 cbp2 = NULL;
372 if (cbp->bio_length > MAXPHYS) {
373 cbp2 = g_duplicate_bio(bp);
374 cbp2->bio_length = cbp->bio_length - MAXPHYS;
375 cbp2->bio_data = cbp->bio_data + MAXPHYS;
376 cbp2->bio_offset = cbp->bio_offset + MAXPHYS;
377 cbp2->bio_to = cp->provider;
378 cbp2->bio_done = g_eli_read_done;
379 cbp->bio_length = MAXPHYS;
380 }
381 /*
382 * Read encrypted data from provider.
383 */
384 G_ELI_LOGREQ(2, cbp, "Sending request.");
385 g_io_request(cbp, cp);
386 if (cbp2 != NULL) {
387 G_ELI_LOGREQ(2, cbp2, "Sending request.");
388 g_io_request(cbp2, cp);
389 }
390}
391
392/*
393 * This is the main function responsible for cryptography (ie. communication
394 * with crypto(9) subsystem).
395 */
396void
397g_eli_auth_run(struct g_eli_worker *wr, struct bio *bp)
398{
399 struct g_eli_softc *sc;
400 struct cryptop *crp;
401 struct cryptodesc *crde, *crda;
402 struct uio *uio;
403 struct iovec *iov;
404 u_int i, lsec, nsec, data_secsize, decr_secsize, encr_secsize;
405 off_t dstoff;
406 int err, error;
407 u_char *p, *data, *auth, *authkey, *plaindata;
408
409 G_ELI_LOGREQ(3, bp, "%s", __func__);
410
411 bp->bio_pflags = wr->w_number;
412 sc = wr->w_softc;
413 /* Sectorsize of decrypted provider eg. 4096. */
414 decr_secsize = bp->bio_to->sectorsize;
415 /* The real sectorsize of encrypted provider, eg. 512. */
416 encr_secsize = LIST_FIRST(&sc->sc_geom->consumer)->provider->sectorsize;
417 /* Number of data bytes in one encrypted sector, eg. 480. */
418 data_secsize = sc->sc_data_per_sector;
419 /* Number of sectors from decrypted provider, eg. 2. */
420 nsec = bp->bio_length / decr_secsize;
421 /* Number of sectors from encrypted provider, eg. 18. */
422 nsec = (nsec * sc->sc_bytes_per_sector) / encr_secsize;
423 /* Last sector number in every big sector, eg. 9. */
424 lsec = sc->sc_bytes_per_sector / encr_secsize;
425 /* Destination offset, used for IV generation. */
426 dstoff = (bp->bio_offset / bp->bio_to->sectorsize) * sc->sc_bytes_per_sector;
427
428 auth = NULL; /* Silence compiler warning. */
429 plaindata = bp->bio_data;
430 if (bp->bio_cmd == BIO_READ) {
431 data = bp->bio_driver2;
432 auth = data + encr_secsize * nsec;
433 p = auth + sc->sc_alen * nsec;
434 } else {
435 size_t size;
436
437 size = encr_secsize * nsec;
438 size += sizeof(*crp) * nsec;
439 size += sizeof(*crde) * nsec;
440 size += sizeof(*crda) * nsec;
441 size += G_ELI_AUTH_SECKEYLEN * nsec;
442 size += sizeof(*uio) * nsec;
443 size += sizeof(*iov) * nsec;
444 data = malloc(size, M_ELI, M_WAITOK);
445 bp->bio_driver2 = data;
446 p = data + encr_secsize * nsec;
447 }
448 bp->bio_inbed = 0;
449 bp->bio_children = nsec;
450
451 error = 0;
452 for (i = 1; i <= nsec; i++, dstoff += encr_secsize) {
453 crp = (struct cryptop *)p; p += sizeof(*crp);
454 crde = (struct cryptodesc *)p; p += sizeof(*crde);
455 crda = (struct cryptodesc *)p; p += sizeof(*crda);
456 authkey = (u_char *)p; p += G_ELI_AUTH_SECKEYLEN;
457 uio = (struct uio *)p; p += sizeof(*uio);
458 iov = (struct iovec *)p; p += sizeof(*iov);
459
460 data_secsize = sc->sc_data_per_sector;
461 if ((i % lsec) == 0)
462 data_secsize = decr_secsize % data_secsize;
463
464 if (bp->bio_cmd == BIO_READ) {
465 /* Remember read HMAC. */
466 bcopy(data, auth, sc->sc_alen);
467 auth += sc->sc_alen;
468 /* TODO: bzero(9) can be commented out later. */
469 bzero(data, sc->sc_alen);
470 } else {
471 bcopy(plaindata, data + sc->sc_alen, data_secsize);
472 plaindata += data_secsize;
473 }
474
475 iov->iov_len = sc->sc_alen + data_secsize;
476 iov->iov_base = data;
477 data += encr_secsize;
478
479 uio->uio_iov = iov;
480 uio->uio_iovcnt = 1;
481 uio->uio_segflg = UIO_SYSSPACE;
482 uio->uio_resid = iov->iov_len;
483
484 crp->crp_sid = wr->w_sid;
485 crp->crp_ilen = uio->uio_resid;
486 crp->crp_olen = data_secsize;
487 crp->crp_opaque = (void *)bp;
488 crp->crp_buf = (void *)uio;
489 crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIFSYNC | CRYPTO_F_REL;
490 if (g_eli_batch)
491 crp->crp_flags |= CRYPTO_F_BATCH;
492 if (bp->bio_cmd == BIO_WRITE) {
493 crp->crp_callback = g_eli_auth_write_done;
494 crp->crp_desc = crde;
495 crde->crd_next = crda;
496 crda->crd_next = NULL;
497 } else {
498 crp->crp_callback = g_eli_auth_read_done;
499 crp->crp_desc = crda;
500 crda->crd_next = crde;
501 crde->crd_next = NULL;
502 }
503
504 crde->crd_skip = sc->sc_alen;
505 crde->crd_len = data_secsize;
506 crde->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
507 if (bp->bio_cmd == BIO_WRITE)
508 crde->crd_flags |= CRD_F_ENCRYPT;
509 crde->crd_alg = sc->sc_ealgo;
510 crde->crd_key = sc->sc_ekey;
510 crde->crd_key = g_eli_crypto_key(sc, dstoff, encr_secsize);
511 crde->crd_klen = sc->sc_ekeylen;
512 g_eli_crypto_ivgen(sc, dstoff, crde->crd_iv,
513 sizeof(crde->crd_iv));
514
515 crda->crd_skip = sc->sc_alen;
516 crda->crd_len = data_secsize;
517 crda->crd_inject = 0;
518 crda->crd_flags = CRD_F_KEY_EXPLICIT;
519 crda->crd_alg = sc->sc_aalgo;
520 g_eli_auth_keygen(sc, dstoff, authkey);
521 crda->crd_key = authkey;
522 crda->crd_klen = G_ELI_AUTH_SECKEYLEN * 8;
523
524 crp->crp_etype = 0;
525 err = crypto_dispatch(crp);
526 if (err != 0 && error == 0)
527 error = err;
528 }
529 if (bp->bio_error == 0)
530 bp->bio_error = error;
531}
511 crde->crd_klen = sc->sc_ekeylen;
512 g_eli_crypto_ivgen(sc, dstoff, crde->crd_iv,
513 sizeof(crde->crd_iv));
514
515 crda->crd_skip = sc->sc_alen;
516 crda->crd_len = data_secsize;
517 crda->crd_inject = 0;
518 crda->crd_flags = CRD_F_KEY_EXPLICIT;
519 crda->crd_alg = sc->sc_aalgo;
520 g_eli_auth_keygen(sc, dstoff, authkey);
521 crda->crd_key = authkey;
522 crda->crd_klen = G_ELI_AUTH_SECKEYLEN * 8;
523
524 crp->crp_etype = 0;
525 err = crypto_dispatch(crp);
526 if (err != 0 && error == 0)
527 error = err;
528 }
529 if (bp->bio_error == 0)
530 bp->bio_error = error;
531}