2 * Copyright (c) 2005-2011 Pawel Jakub Dawidek <pawel@dawidek.net>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
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>
36 #include <sys/mutex.h>
38 #include <sys/sysctl.h>
39 #include <sys/malloc.h>
40 #include <sys/kthread.h>
42 #include <sys/sched.h>
45 #include <sys/vnode.h>
49 #include <geom/geom.h>
50 #include <geom/eli/g_eli.h>
51 #include <geom/eli/pkcs5v2.h>
54 * The data layout description when integrity verification is configured.
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
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.
77 * And here is the picture:
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|
86 * da0.eli: +----+----+----+----+----+----+----+----+----+
87 * |480b|480b|480b|480b|480b|480b|480b|480b|256b|
88 * +----+----+----+----+----+----+----+----+----+
90 * +--------------------------------------------+
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.
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
102 * g_eli_start -> g_eli_auth_run -> g_eli_auth_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
105 MALLOC_DECLARE(M_ELI);
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.
114 g_eli_auth_keygen(struct g_eli_softc *sc, off_t offset, u_char *key)
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);
125 * The function is called after we read and decrypt data.
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
130 g_eli_auth_read_done(struct cryptop *crp)
132 struct g_eli_softc *sc;
135 if (crp->crp_etype == EAGAIN) {
136 if (g_eli_crypto_rerun(crp) == 0)
139 bp = (struct bio *)crp->crp_opaque;
141 if (crp->crp_etype == 0) {
142 bp->bio_completed += crp->crp_olen;
143 G_ELI_DEBUG(3, "Crypto READ request done (%d/%d) (add=%jd completed=%jd).",
144 bp->bio_inbed, bp->bio_children, (intmax_t)crp->crp_olen, (intmax_t)bp->bio_completed);
146 G_ELI_DEBUG(1, "Crypto READ request failed (%d/%d) error=%d.",
147 bp->bio_inbed, bp->bio_children, crp->crp_etype);
148 if (bp->bio_error == 0)
149 bp->bio_error = crp->crp_etype;
151 sc = bp->bio_to->geom->softc;
152 g_eli_key_drop(sc, crp->crp_desc->crd_next->crd_key);
154 * Do we have all sectors already?
156 if (bp->bio_inbed < bp->bio_children)
158 if (bp->bio_error == 0) {
159 u_int i, lsec, nsec, data_secsize, decr_secsize, encr_secsize;
160 u_char *srcdata, *dstdata, *auth;
161 off_t coroff, corsize;
164 * Verify data integrity based on calculated and read HMACs.
166 /* Sectorsize of decrypted provider eg. 4096. */
167 decr_secsize = bp->bio_to->sectorsize;
168 /* The real sectorsize of encrypted provider, eg. 512. */
169 encr_secsize = LIST_FIRST(&sc->sc_geom->consumer)->provider->sectorsize;
170 /* Number of data bytes in one encrypted sector, eg. 480. */
171 data_secsize = sc->sc_data_per_sector;
172 /* Number of sectors from decrypted provider, eg. 2. */
173 nsec = bp->bio_length / decr_secsize;
174 /* Number of sectors from encrypted provider, eg. 18. */
175 nsec = (nsec * sc->sc_bytes_per_sector) / encr_secsize;
176 /* Last sector number in every big sector, eg. 9. */
177 lsec = sc->sc_bytes_per_sector / encr_secsize;
179 srcdata = bp->bio_driver2;
180 dstdata = bp->bio_data;
181 auth = srcdata + encr_secsize * nsec;
185 for (i = 1; i <= nsec; i++) {
186 data_secsize = sc->sc_data_per_sector;
188 data_secsize = decr_secsize % data_secsize;
189 if (bcmp(srcdata, auth, sc->sc_alen) != 0) {
191 * Curruption detected, remember the offset if
192 * this is the first corrupted sector and
195 if (bp->bio_error == 0)
198 coroff = bp->bio_offset +
199 (dstdata - (u_char *)bp->bio_data);
201 corsize += data_secsize;
204 * No curruption, good.
205 * Report previous corruption if there was one.
208 G_ELI_DEBUG(0, "%s: Failed to authenticate %jd "
209 "bytes of data at offset %jd.",
210 sc->sc_name, (intmax_t)corsize,
215 bcopy(srcdata + sc->sc_alen, dstdata,
218 srcdata += encr_secsize;
219 dstdata += data_secsize;
222 /* Report previous corruption if there was one. */
224 G_ELI_DEBUG(0, "%s: Failed to authenticate %jd "
225 "bytes of data at offset %jd.",
226 sc->sc_name, (intmax_t)corsize, (intmax_t)coroff);
229 free(bp->bio_driver2, M_ELI);
230 bp->bio_driver2 = NULL;
231 if (bp->bio_error != 0) {
232 if (bp->bio_error == -1)
233 bp->bio_error = EINVAL;
236 "Crypto READ request failed (error=%d).",
239 bp->bio_completed = 0;
242 * Read is finished, send it up.
244 g_io_deliver(bp, bp->bio_error);
245 atomic_subtract_int(&sc->sc_inflight, 1);
250 * The function is called after data encryption.
252 * g_eli_start -> g_eli_auth_run -> G_ELI_AUTH_WRITE_DONE -> g_io_request -> g_eli_write_done -> g_io_deliver
255 g_eli_auth_write_done(struct cryptop *crp)
257 struct g_eli_softc *sc;
258 struct g_consumer *cp;
259 struct bio *bp, *cbp, *cbp2;
262 if (crp->crp_etype == EAGAIN) {
263 if (g_eli_crypto_rerun(crp) == 0)
266 bp = (struct bio *)crp->crp_opaque;
268 if (crp->crp_etype == 0) {
269 G_ELI_DEBUG(3, "Crypto WRITE request done (%d/%d).",
270 bp->bio_inbed, bp->bio_children);
272 G_ELI_DEBUG(1, "Crypto WRITE request failed (%d/%d) error=%d.",
273 bp->bio_inbed, bp->bio_children, crp->crp_etype);
274 if (bp->bio_error == 0)
275 bp->bio_error = crp->crp_etype;
277 sc = bp->bio_to->geom->softc;
278 g_eli_key_drop(sc, crp->crp_desc->crd_key);
280 * All sectors are already encrypted?
282 if (bp->bio_inbed < bp->bio_children)
284 if (bp->bio_error != 0) {
285 G_ELI_LOGREQ(0, bp, "Crypto WRITE request failed (error=%d).",
287 free(bp->bio_driver2, M_ELI);
288 bp->bio_driver2 = NULL;
289 cbp = bp->bio_driver1;
290 bp->bio_driver1 = NULL;
292 g_io_deliver(bp, bp->bio_error);
293 atomic_subtract_int(&sc->sc_inflight, 1);
296 cp = LIST_FIRST(&sc->sc_geom->consumer);
297 cbp = bp->bio_driver1;
298 bp->bio_driver1 = NULL;
299 cbp->bio_to = cp->provider;
300 cbp->bio_done = g_eli_write_done;
302 /* Number of sectors from decrypted provider, eg. 1. */
303 nsec = bp->bio_length / bp->bio_to->sectorsize;
304 /* Number of sectors from encrypted provider, eg. 9. */
305 nsec = (nsec * sc->sc_bytes_per_sector) / cp->provider->sectorsize;
307 cbp->bio_length = cp->provider->sectorsize * nsec;
308 cbp->bio_offset = (bp->bio_offset / bp->bio_to->sectorsize) * sc->sc_bytes_per_sector;
309 cbp->bio_data = bp->bio_driver2;
312 * We write more than what is requested, so we have to be ready to write
316 if (cbp->bio_length > MAXPHYS) {
317 cbp2 = g_duplicate_bio(bp);
318 cbp2->bio_length = cbp->bio_length - MAXPHYS;
319 cbp2->bio_data = cbp->bio_data + MAXPHYS;
320 cbp2->bio_offset = cbp->bio_offset + MAXPHYS;
321 cbp2->bio_to = cp->provider;
322 cbp2->bio_done = g_eli_write_done;
323 cbp->bio_length = MAXPHYS;
326 * Send encrypted data to the provider.
328 G_ELI_LOGREQ(2, cbp, "Sending request.");
330 bp->bio_children = (cbp2 != NULL ? 2 : 1);
331 g_io_request(cbp, cp);
333 G_ELI_LOGREQ(2, cbp2, "Sending request.");
334 g_io_request(cbp2, cp);
340 g_eli_auth_read(struct g_eli_softc *sc, struct bio *bp)
342 struct g_consumer *cp;
343 struct bio *cbp, *cbp2;
349 cp = LIST_FIRST(&sc->sc_geom->consumer);
350 cbp = bp->bio_driver1;
351 bp->bio_driver1 = NULL;
352 cbp->bio_to = cp->provider;
353 cbp->bio_done = g_eli_read_done;
355 /* Number of sectors from decrypted provider, eg. 1. */
356 nsec = bp->bio_length / bp->bio_to->sectorsize;
357 /* Number of sectors from encrypted provider, eg. 9. */
358 nsec = (nsec * sc->sc_bytes_per_sector) / cp->provider->sectorsize;
360 cbp->bio_length = cp->provider->sectorsize * nsec;
361 size = cbp->bio_length;
362 size += sc->sc_alen * nsec;
363 size += sizeof(struct cryptop) * nsec;
364 size += sizeof(struct cryptodesc) * nsec * 2;
365 size += G_ELI_AUTH_SECKEYLEN * nsec;
366 size += sizeof(struct uio) * nsec;
367 size += sizeof(struct iovec) * nsec;
368 cbp->bio_offset = (bp->bio_offset / bp->bio_to->sectorsize) * sc->sc_bytes_per_sector;
369 bp->bio_driver2 = malloc(size, M_ELI, M_WAITOK);
370 cbp->bio_data = bp->bio_driver2;
373 * We read more than what is requested, so we have to be ready to read
377 if (cbp->bio_length > MAXPHYS) {
378 cbp2 = g_duplicate_bio(bp);
379 cbp2->bio_length = cbp->bio_length - MAXPHYS;
380 cbp2->bio_data = cbp->bio_data + MAXPHYS;
381 cbp2->bio_offset = cbp->bio_offset + MAXPHYS;
382 cbp2->bio_to = cp->provider;
383 cbp2->bio_done = g_eli_read_done;
384 cbp->bio_length = MAXPHYS;
387 * Read encrypted data from provider.
389 G_ELI_LOGREQ(2, cbp, "Sending request.");
390 g_io_request(cbp, cp);
392 G_ELI_LOGREQ(2, cbp2, "Sending request.");
393 g_io_request(cbp2, cp);
398 * This is the main function responsible for cryptography (ie. communication
399 * with crypto(9) subsystem).
402 * 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
404 * g_eli_start -> G_ELI_AUTH_RUN -> g_eli_auth_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
407 g_eli_auth_run(struct g_eli_worker *wr, struct bio *bp)
409 struct g_eli_softc *sc;
411 struct cryptodesc *crde, *crda;
414 u_int i, lsec, nsec, data_secsize, decr_secsize, encr_secsize;
417 u_char *p, *data, *auth, *authkey, *plaindata;
419 G_ELI_LOGREQ(3, bp, "%s", __func__);
421 bp->bio_pflags = wr->w_number;
423 /* Sectorsize of decrypted provider eg. 4096. */
424 decr_secsize = bp->bio_to->sectorsize;
425 /* The real sectorsize of encrypted provider, eg. 512. */
426 encr_secsize = LIST_FIRST(&sc->sc_geom->consumer)->provider->sectorsize;
427 /* Number of data bytes in one encrypted sector, eg. 480. */
428 data_secsize = sc->sc_data_per_sector;
429 /* Number of sectors from decrypted provider, eg. 2. */
430 nsec = bp->bio_length / decr_secsize;
431 /* Number of sectors from encrypted provider, eg. 18. */
432 nsec = (nsec * sc->sc_bytes_per_sector) / encr_secsize;
433 /* Last sector number in every big sector, eg. 9. */
434 lsec = sc->sc_bytes_per_sector / encr_secsize;
435 /* Destination offset, used for IV generation. */
436 dstoff = (bp->bio_offset / bp->bio_to->sectorsize) * sc->sc_bytes_per_sector;
438 auth = NULL; /* Silence compiler warning. */
439 plaindata = bp->bio_data;
440 if (bp->bio_cmd == BIO_READ) {
441 data = bp->bio_driver2;
442 auth = data + encr_secsize * nsec;
443 p = auth + sc->sc_alen * nsec;
447 size = encr_secsize * nsec;
448 size += sizeof(*crp) * nsec;
449 size += sizeof(*crde) * nsec;
450 size += sizeof(*crda) * nsec;
451 size += G_ELI_AUTH_SECKEYLEN * nsec;
452 size += sizeof(*uio) * nsec;
453 size += sizeof(*iov) * nsec;
454 data = malloc(size, M_ELI, M_WAITOK);
455 bp->bio_driver2 = data;
456 p = data + encr_secsize * nsec;
459 bp->bio_children = nsec;
462 for (i = 1; i <= nsec; i++, dstoff += encr_secsize) {
463 crp = (struct cryptop *)p; p += sizeof(*crp);
464 crde = (struct cryptodesc *)p; p += sizeof(*crde);
465 crda = (struct cryptodesc *)p; p += sizeof(*crda);
466 authkey = (u_char *)p; p += G_ELI_AUTH_SECKEYLEN;
467 uio = (struct uio *)p; p += sizeof(*uio);
468 iov = (struct iovec *)p; p += sizeof(*iov);
470 data_secsize = sc->sc_data_per_sector;
472 data_secsize = decr_secsize % data_secsize;
474 if (bp->bio_cmd == BIO_READ) {
475 /* Remember read HMAC. */
476 bcopy(data, auth, sc->sc_alen);
478 /* TODO: bzero(9) can be commented out later. */
479 bzero(data, sc->sc_alen);
481 bcopy(plaindata, data + sc->sc_alen, data_secsize);
482 plaindata += data_secsize;
485 iov->iov_len = sc->sc_alen + data_secsize;
486 iov->iov_base = data;
487 data += encr_secsize;
491 uio->uio_segflg = UIO_SYSSPACE;
492 uio->uio_resid = iov->iov_len;
494 crp->crp_sid = wr->w_sid;
495 crp->crp_ilen = uio->uio_resid;
496 crp->crp_olen = data_secsize;
497 crp->crp_opaque = (void *)bp;
498 crp->crp_buf = (void *)uio;
499 crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIFSYNC | CRYPTO_F_REL;
501 crp->crp_flags |= CRYPTO_F_BATCH;
502 if (bp->bio_cmd == BIO_WRITE) {
503 crp->crp_callback = g_eli_auth_write_done;
504 crp->crp_desc = crde;
505 crde->crd_next = crda;
506 crda->crd_next = NULL;
508 crp->crp_callback = g_eli_auth_read_done;
509 crp->crp_desc = crda;
510 crda->crd_next = crde;
511 crde->crd_next = NULL;
514 crde->crd_skip = sc->sc_alen;
515 crde->crd_len = data_secsize;
516 crde->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
517 if ((sc->sc_flags & G_ELI_FLAG_FIRST_KEY) == 0)
518 crde->crd_flags |= CRD_F_KEY_EXPLICIT;
519 if (bp->bio_cmd == BIO_WRITE)
520 crde->crd_flags |= CRD_F_ENCRYPT;
521 crde->crd_alg = sc->sc_ealgo;
522 crde->crd_key = g_eli_key_hold(sc, dstoff, encr_secsize);
523 crde->crd_klen = sc->sc_ekeylen;
524 if (sc->sc_ealgo == CRYPTO_AES_XTS)
525 crde->crd_klen <<= 1;
526 g_eli_crypto_ivgen(sc, dstoff, crde->crd_iv,
527 sizeof(crde->crd_iv));
529 crda->crd_skip = sc->sc_alen;
530 crda->crd_len = data_secsize;
531 crda->crd_inject = 0;
532 crda->crd_flags = CRD_F_KEY_EXPLICIT;
533 crda->crd_alg = sc->sc_aalgo;
534 g_eli_auth_keygen(sc, dstoff, authkey);
535 crda->crd_key = authkey;
536 crda->crd_klen = G_ELI_AUTH_SECKEYLEN * 8;
539 err = crypto_dispatch(crp);
540 if (err != 0 && error == 0)
543 if (bp->bio_error == 0)
544 bp->bio_error = error;