2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2005-2011 Pawel Jakub Dawidek <pawel@dawidek.net>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/linker.h>
36 #include <sys/module.h>
38 #include <sys/mutex.h>
40 #include <sys/sysctl.h>
41 #include <sys/malloc.h>
42 #include <sys/kthread.h>
44 #include <sys/sched.h>
46 #include <sys/vnode.h>
50 #include <geom/geom.h>
51 #include <geom/geom_dbg.h>
52 #include <geom/eli/g_eli.h>
53 #include <geom/eli/pkcs5v2.h>
56 * The data layout description when integrity verification is configured.
58 * One of the most important assumption here is that authenticated data and its
59 * HMAC has to be stored in the same place (namely in the same sector) to make
61 * The problem is that file systems work only with sectors that are multiple of
62 * 512 bytes and a power of two number.
63 * My idea to implement it is as follows.
64 * Let's store HMAC in sector. This is a must. This leaves us 480 bytes for
65 * data. We can't use that directly (ie. we can't create provider with 480 bytes
66 * sector size). We need another sector from where we take only 32 bytes of data
67 * and we store HMAC of this data as well. This takes two sectors from the
68 * original provider at the input and leaves us one sector of authenticated data
69 * at the output. Not very efficient, but you got the idea.
70 * Now, let's assume, we want to create provider with 4096 bytes sector.
71 * To output 4096 bytes of authenticated data we need 8x480 plus 1x256, so we
72 * need nine 512-bytes sectors at the input to get one 4096-bytes sector at the
73 * output. That's better. With 4096 bytes sector we can use 89% of size of the
74 * original provider. I find it as an acceptable cost.
75 * The reliability comes from the fact, that every HMAC stored inside the sector
76 * is calculated only for the data in the same sector, so its impossible to
77 * write new data and leave old HMAC or vice versa.
79 * And here is the picture:
81 * da0: +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+-----+
82 * |32b |480b| |32b |480b| |32b |480b| |32b |480b| |32b |480b| |32b |480b| |32b |480b| |32b |480b| |32b |256b |
83 * |HMAC|Data| |HMAC|Data| |HMAC|Data| |HMAC|Data| |HMAC|Data| |HMAC|Data| |HMAC|Data| |HMAC|Data| |HMAC|Data |
84 * +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+-----+
85 * |512 bytes| |512 bytes| |512 bytes| |512 bytes| |512 bytes| |512 bytes| |512 bytes| |512 bytes| |288 bytes |
86 * +---------+ +---------+ +---------+ +---------+ +---------+ +---------+ +---------+ +---------+ |224 unused|
88 * da0.eli: +----+----+----+----+----+----+----+----+----+
89 * |480b|480b|480b|480b|480b|480b|480b|480b|256b|
90 * +----+----+----+----+----+----+----+----+----+
92 * +--------------------------------------------+
94 * PS. You can use any sector size with geli(8). My example is using 4kB,
95 * because it's most efficient. For 8kB sectors you need 2 extra sectors,
96 * so the cost is the same as for 4kB sectors.
102 * 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
104 * g_eli_start -> g_eli_auth_run -> g_eli_auth_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
107 MALLOC_DECLARE(M_ELI);
110 * Here we generate key for HMAC. Every sector has its own HMAC key, so it is
111 * not possible to copy sectors.
112 * We cannot depend on fact, that every sector has its own IV, because different
113 * IV doesn't change HMAC, when we use encrypt-then-authenticate method.
116 g_eli_auth_keygen(struct g_eli_softc *sc, off_t offset, u_char *key)
120 /* Copy precalculated SHA256 context. */
121 bcopy(&sc->sc_akeyctx, &ctx, sizeof(ctx));
122 SHA256_Update(&ctx, (uint8_t *)&offset, sizeof(offset));
123 SHA256_Final(key, &ctx);
127 * The function is called after we read and decrypt data.
129 * 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
132 g_eli_auth_read_done(struct cryptop *crp)
134 struct g_eli_softc *sc;
137 if (crp->crp_etype == EAGAIN) {
138 if (g_eli_crypto_rerun(crp) == 0)
141 bp = (struct bio *)crp->crp_opaque;
143 if (crp->crp_etype == 0) {
144 bp->bio_completed += crp->crp_olen;
145 G_ELI_DEBUG(3, "Crypto READ request done (%d/%d) (add=%jd completed=%jd).",
146 bp->bio_inbed, bp->bio_children, (intmax_t)crp->crp_olen, (intmax_t)bp->bio_completed);
148 G_ELI_DEBUG(1, "Crypto READ request failed (%d/%d) error=%d.",
149 bp->bio_inbed, bp->bio_children, crp->crp_etype);
150 if (bp->bio_error == 0)
151 bp->bio_error = crp->crp_etype;
153 sc = bp->bio_to->geom->softc;
154 g_eli_key_drop(sc, crp->crp_desc->crd_next->crd_key);
156 * Do we have all sectors already?
158 if (bp->bio_inbed < bp->bio_children)
160 if (bp->bio_error == 0) {
161 u_int i, lsec, nsec, data_secsize, decr_secsize, encr_secsize;
162 u_char *srcdata, *dstdata, *auth;
163 off_t coroff, corsize;
166 * Verify data integrity based on calculated and read HMACs.
168 /* Sectorsize of decrypted provider eg. 4096. */
169 decr_secsize = bp->bio_to->sectorsize;
170 /* The real sectorsize of encrypted provider, eg. 512. */
171 encr_secsize = LIST_FIRST(&sc->sc_geom->consumer)->provider->sectorsize;
172 /* Number of data bytes in one encrypted sector, eg. 480. */
173 data_secsize = sc->sc_data_per_sector;
174 /* Number of sectors from decrypted provider, eg. 2. */
175 nsec = bp->bio_length / decr_secsize;
176 /* Number of sectors from encrypted provider, eg. 18. */
177 nsec = (nsec * sc->sc_bytes_per_sector) / encr_secsize;
178 /* Last sector number in every big sector, eg. 9. */
179 lsec = sc->sc_bytes_per_sector / encr_secsize;
181 srcdata = bp->bio_driver2;
182 dstdata = bp->bio_data;
183 auth = srcdata + encr_secsize * nsec;
187 for (i = 1; i <= nsec; i++) {
188 data_secsize = sc->sc_data_per_sector;
190 data_secsize = decr_secsize % data_secsize;
191 if (bcmp(srcdata, auth, sc->sc_alen) != 0) {
193 * Curruption detected, remember the offset if
194 * this is the first corrupted sector and
197 if (bp->bio_error == 0)
200 coroff = bp->bio_offset +
201 (dstdata - (u_char *)bp->bio_data);
203 corsize += data_secsize;
206 * No curruption, good.
207 * Report previous corruption if there was one.
210 G_ELI_DEBUG(0, "%s: Failed to authenticate %jd "
211 "bytes of data at offset %jd.",
212 sc->sc_name, (intmax_t)corsize,
217 bcopy(srcdata + sc->sc_alen, dstdata,
220 srcdata += encr_secsize;
221 dstdata += data_secsize;
224 /* Report previous corruption if there was one. */
226 G_ELI_DEBUG(0, "%s: Failed to authenticate %jd "
227 "bytes of data at offset %jd.",
228 sc->sc_name, (intmax_t)corsize, (intmax_t)coroff);
231 free(bp->bio_driver2, M_ELI);
232 bp->bio_driver2 = NULL;
233 if (bp->bio_error != 0) {
234 if (bp->bio_error == -1)
235 bp->bio_error = EINVAL;
238 "Crypto READ request failed (error=%d).",
241 bp->bio_completed = 0;
244 * Read is finished, send it up.
246 g_io_deliver(bp, bp->bio_error);
247 atomic_subtract_int(&sc->sc_inflight, 1);
252 * The function is called after data encryption.
254 * g_eli_start -> g_eli_auth_run -> G_ELI_AUTH_WRITE_DONE -> g_io_request -> g_eli_write_done -> g_io_deliver
257 g_eli_auth_write_done(struct cryptop *crp)
259 struct g_eli_softc *sc;
260 struct g_consumer *cp;
261 struct bio *bp, *cbp, *cbp2;
264 if (crp->crp_etype == EAGAIN) {
265 if (g_eli_crypto_rerun(crp) == 0)
268 bp = (struct bio *)crp->crp_opaque;
270 if (crp->crp_etype == 0) {
271 G_ELI_DEBUG(3, "Crypto WRITE request done (%d/%d).",
272 bp->bio_inbed, bp->bio_children);
274 G_ELI_DEBUG(1, "Crypto WRITE request failed (%d/%d) error=%d.",
275 bp->bio_inbed, bp->bio_children, crp->crp_etype);
276 if (bp->bio_error == 0)
277 bp->bio_error = crp->crp_etype;
279 sc = bp->bio_to->geom->softc;
280 g_eli_key_drop(sc, crp->crp_desc->crd_key);
282 * All sectors are already encrypted?
284 if (bp->bio_inbed < bp->bio_children)
286 if (bp->bio_error != 0) {
287 G_ELI_LOGREQ(0, bp, "Crypto WRITE request failed (error=%d).",
289 free(bp->bio_driver2, M_ELI);
290 bp->bio_driver2 = NULL;
291 cbp = bp->bio_driver1;
292 bp->bio_driver1 = NULL;
294 g_io_deliver(bp, bp->bio_error);
295 atomic_subtract_int(&sc->sc_inflight, 1);
298 cp = LIST_FIRST(&sc->sc_geom->consumer);
299 cbp = bp->bio_driver1;
300 bp->bio_driver1 = NULL;
301 cbp->bio_to = cp->provider;
302 cbp->bio_done = g_eli_write_done;
304 /* Number of sectors from decrypted provider, eg. 1. */
305 nsec = bp->bio_length / bp->bio_to->sectorsize;
306 /* Number of sectors from encrypted provider, eg. 9. */
307 nsec = (nsec * sc->sc_bytes_per_sector) / cp->provider->sectorsize;
309 cbp->bio_length = cp->provider->sectorsize * nsec;
310 cbp->bio_offset = (bp->bio_offset / bp->bio_to->sectorsize) * sc->sc_bytes_per_sector;
311 cbp->bio_data = bp->bio_driver2;
314 * We write more than what is requested, so we have to be ready to write
318 if (cbp->bio_length > MAXPHYS) {
319 cbp2 = g_duplicate_bio(bp);
320 cbp2->bio_length = cbp->bio_length - MAXPHYS;
321 cbp2->bio_data = cbp->bio_data + MAXPHYS;
322 cbp2->bio_offset = cbp->bio_offset + MAXPHYS;
323 cbp2->bio_to = cp->provider;
324 cbp2->bio_done = g_eli_write_done;
325 cbp->bio_length = MAXPHYS;
328 * Send encrypted data to the provider.
330 G_ELI_LOGREQ(2, cbp, "Sending request.");
332 bp->bio_children = (cbp2 != NULL ? 2 : 1);
333 g_io_request(cbp, cp);
335 G_ELI_LOGREQ(2, cbp2, "Sending request.");
336 g_io_request(cbp2, cp);
342 g_eli_auth_read(struct g_eli_softc *sc, struct bio *bp)
344 struct g_consumer *cp;
345 struct bio *cbp, *cbp2;
351 cp = LIST_FIRST(&sc->sc_geom->consumer);
352 cbp = bp->bio_driver1;
353 bp->bio_driver1 = NULL;
354 cbp->bio_to = cp->provider;
355 cbp->bio_done = g_eli_read_done;
357 /* Number of sectors from decrypted provider, eg. 1. */
358 nsec = bp->bio_length / bp->bio_to->sectorsize;
359 /* Number of sectors from encrypted provider, eg. 9. */
360 nsec = (nsec * sc->sc_bytes_per_sector) / cp->provider->sectorsize;
362 cbp->bio_length = cp->provider->sectorsize * nsec;
363 size = cbp->bio_length;
364 size += sc->sc_alen * nsec;
365 size += sizeof(struct cryptop) * nsec;
366 size += sizeof(struct cryptodesc) * nsec * 2;
367 size += G_ELI_AUTH_SECKEYLEN * 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;
412 u_int i, lsec, nsec, data_secsize, decr_secsize, encr_secsize;
414 u_char *p, *data, *auth, *authkey, *plaindata;
417 G_ELI_LOGREQ(3, bp, "%s", __func__);
419 bp->bio_pflags = wr->w_number;
421 /* Sectorsize of decrypted provider eg. 4096. */
422 decr_secsize = bp->bio_to->sectorsize;
423 /* The real sectorsize of encrypted provider, eg. 512. */
424 encr_secsize = LIST_FIRST(&sc->sc_geom->consumer)->provider->sectorsize;
425 /* Number of data bytes in one encrypted sector, eg. 480. */
426 data_secsize = sc->sc_data_per_sector;
427 /* Number of sectors from decrypted provider, eg. 2. */
428 nsec = bp->bio_length / decr_secsize;
429 /* Number of sectors from encrypted provider, eg. 18. */
430 nsec = (nsec * sc->sc_bytes_per_sector) / encr_secsize;
431 /* Last sector number in every big sector, eg. 9. */
432 lsec = sc->sc_bytes_per_sector / encr_secsize;
433 /* Destination offset, used for IV generation. */
434 dstoff = (bp->bio_offset / bp->bio_to->sectorsize) * sc->sc_bytes_per_sector;
436 auth = NULL; /* Silence compiler warning. */
437 plaindata = bp->bio_data;
438 if (bp->bio_cmd == BIO_READ) {
439 data = bp->bio_driver2;
440 auth = data + encr_secsize * nsec;
441 p = auth + sc->sc_alen * nsec;
445 size = encr_secsize * nsec;
446 size += sizeof(*crp) * nsec;
447 size += sizeof(*crde) * nsec;
448 size += sizeof(*crda) * nsec;
449 size += G_ELI_AUTH_SECKEYLEN * nsec;
450 size += sizeof(uintptr_t); /* Space for alignment. */
451 data = malloc(size, M_ELI, M_WAITOK);
452 bp->bio_driver2 = data;
453 p = data + encr_secsize * nsec;
456 bp->bio_children = nsec;
458 #if defined(__mips_n64) || defined(__mips_o64)
459 p = (char *)roundup((uintptr_t)p, sizeof(uintptr_t));
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;
468 data_secsize = sc->sc_data_per_sector;
469 if ((i % lsec) == 0) {
470 data_secsize = decr_secsize % data_secsize;
472 * Last encrypted sector of each decrypted sector is
473 * only partially filled.
475 if (bp->bio_cmd == BIO_WRITE)
476 memset(data + sc->sc_alen + data_secsize, 0,
477 encr_secsize - sc->sc_alen - data_secsize);
480 if (bp->bio_cmd == BIO_READ) {
481 /* Remember read HMAC. */
482 bcopy(data, auth, sc->sc_alen);
484 /* TODO: bzero(9) can be commented out later. */
485 bzero(data, sc->sc_alen);
487 bcopy(plaindata, data + sc->sc_alen, data_secsize);
488 plaindata += data_secsize;
491 crp->crp_session = wr->w_sid;
492 crp->crp_ilen = sc->sc_alen + data_secsize;
493 crp->crp_olen = data_secsize;
494 crp->crp_opaque = (void *)bp;
495 crp->crp_buf = (void *)data;
496 data += encr_secsize;
497 crp->crp_flags = CRYPTO_F_CBIFSYNC;
499 crp->crp_flags |= CRYPTO_F_BATCH;
500 if (bp->bio_cmd == BIO_WRITE) {
501 crp->crp_callback = g_eli_auth_write_done;
502 crp->crp_desc = crde;
503 crde->crd_next = crda;
504 crda->crd_next = NULL;
506 crp->crp_callback = g_eli_auth_read_done;
507 crp->crp_desc = crda;
508 crda->crd_next = crde;
509 crde->crd_next = NULL;
512 crde->crd_skip = sc->sc_alen;
513 crde->crd_len = data_secsize;
514 crde->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
515 if ((sc->sc_flags & G_ELI_FLAG_FIRST_KEY) == 0)
516 crde->crd_flags |= CRD_F_KEY_EXPLICIT;
517 if (bp->bio_cmd == BIO_WRITE)
518 crde->crd_flags |= CRD_F_ENCRYPT;
519 crde->crd_alg = sc->sc_ealgo;
520 crde->crd_key = g_eli_key_hold(sc, dstoff, encr_secsize);
521 crde->crd_klen = sc->sc_ekeylen;
522 if (sc->sc_ealgo == CRYPTO_AES_XTS)
523 crde->crd_klen <<= 1;
524 g_eli_crypto_ivgen(sc, dstoff, crde->crd_iv,
525 sizeof(crde->crd_iv));
527 crda->crd_skip = sc->sc_alen;
528 crda->crd_len = data_secsize;
529 crda->crd_inject = 0;
530 crda->crd_flags = CRD_F_KEY_EXPLICIT;
531 crda->crd_alg = sc->sc_aalgo;
532 g_eli_auth_keygen(sc, dstoff, authkey);
533 crda->crd_key = authkey;
534 crda->crd_klen = G_ELI_AUTH_SECKEYLEN * 8;
537 error = crypto_dispatch(crp);
538 KASSERT(error == 0, ("crypto_dispatch() failed (error=%d)",