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/eli/g_eli.h>
52 #include <geom/eli/pkcs5v2.h>
55 * The data layout description when integrity verification is configured.
57 * One of the most important assumption here is that authenticated data and its
58 * HMAC has to be stored in the same place (namely in the same sector) to make
60 * The problem is that file systems work only with sectors that are multiple of
61 * 512 bytes and a power of two number.
62 * My idea to implement it is as follows.
63 * Let's store HMAC in sector. This is a must. This leaves us 480 bytes for
64 * data. We can't use that directly (ie. we can't create provider with 480 bytes
65 * sector size). We need another sector from where we take only 32 bytes of data
66 * and we store HMAC of this data as well. This takes two sectors from the
67 * original provider at the input and leaves us one sector of authenticated data
68 * at the output. Not very efficient, but you got the idea.
69 * Now, let's assume, we want to create provider with 4096 bytes sector.
70 * To output 4096 bytes of authenticated data we need 8x480 plus 1x256, so we
71 * need nine 512-bytes sectors at the input to get one 4096-bytes sector at the
72 * output. That's better. With 4096 bytes sector we can use 89% of size of the
73 * original provider. I find it as an acceptable cost.
74 * The reliability comes from the fact, that every HMAC stored inside the sector
75 * is calculated only for the data in the same sector, so its impossible to
76 * write new data and leave old HMAC or vice versa.
78 * And here is the picture:
80 * da0: +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+-----+
81 * |32b |480b| |32b |480b| |32b |480b| |32b |480b| |32b |480b| |32b |480b| |32b |480b| |32b |480b| |32b |256b |
82 * |HMAC|Data| |HMAC|Data| |HMAC|Data| |HMAC|Data| |HMAC|Data| |HMAC|Data| |HMAC|Data| |HMAC|Data| |HMAC|Data |
83 * +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+-----+
84 * |512 bytes| |512 bytes| |512 bytes| |512 bytes| |512 bytes| |512 bytes| |512 bytes| |512 bytes| |288 bytes |
85 * +---------+ +---------+ +---------+ +---------+ +---------+ +---------+ +---------+ +---------+ |224 unused|
87 * da0.eli: +----+----+----+----+----+----+----+----+----+
88 * |480b|480b|480b|480b|480b|480b|480b|480b|256b|
89 * +----+----+----+----+----+----+----+----+----+
91 * +--------------------------------------------+
93 * PS. You can use any sector size with geli(8). My example is using 4kB,
94 * because it's most efficient. For 8kB sectors you need 2 extra sectors,
95 * so the cost is the same as for 4kB sectors.
101 * 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
103 * g_eli_start -> g_eli_auth_run -> g_eli_auth_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
106 MALLOC_DECLARE(M_ELI);
109 * Here we generate key for HMAC. Every sector has its own HMAC key, so it is
110 * not possible to copy sectors.
111 * We cannot depend on fact, that every sector has its own IV, because different
112 * IV doesn't change HMAC, when we use encrypt-then-authenticate method.
115 g_eli_auth_keygen(struct g_eli_softc *sc, off_t offset, u_char *key)
119 /* Copy precalculated SHA256 context. */
120 bcopy(&sc->sc_akeyctx, &ctx, sizeof(ctx));
121 SHA256_Update(&ctx, (uint8_t *)&offset, sizeof(offset));
122 SHA256_Final(key, &ctx);
126 * The function is called after we read and decrypt data.
128 * 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
131 g_eli_auth_read_done(struct cryptop *crp)
133 struct g_eli_softc *sc;
136 if (crp->crp_etype == EAGAIN) {
137 if (g_eli_crypto_rerun(crp) == 0)
140 bp = (struct bio *)crp->crp_opaque;
142 if (crp->crp_etype == 0) {
143 bp->bio_completed += crp->crp_olen;
144 G_ELI_DEBUG(3, "Crypto READ request done (%d/%d) (add=%jd completed=%jd).",
145 bp->bio_inbed, bp->bio_children, (intmax_t)crp->crp_olen, (intmax_t)bp->bio_completed);
147 G_ELI_DEBUG(1, "Crypto READ request failed (%d/%d) error=%d.",
148 bp->bio_inbed, bp->bio_children, crp->crp_etype);
149 if (bp->bio_error == 0)
150 bp->bio_error = crp->crp_etype;
152 sc = bp->bio_to->geom->softc;
153 g_eli_key_drop(sc, crp->crp_desc->crd_next->crd_key);
155 * Do we have all sectors already?
157 if (bp->bio_inbed < bp->bio_children)
159 if (bp->bio_error == 0) {
160 u_int i, lsec, nsec, data_secsize, decr_secsize, encr_secsize;
161 u_char *srcdata, *dstdata, *auth;
162 off_t coroff, corsize;
165 * Verify data integrity based on calculated and read HMACs.
167 /* Sectorsize of decrypted provider eg. 4096. */
168 decr_secsize = bp->bio_to->sectorsize;
169 /* The real sectorsize of encrypted provider, eg. 512. */
170 encr_secsize = LIST_FIRST(&sc->sc_geom->consumer)->provider->sectorsize;
171 /* Number of data bytes in one encrypted sector, eg. 480. */
172 data_secsize = sc->sc_data_per_sector;
173 /* Number of sectors from decrypted provider, eg. 2. */
174 nsec = bp->bio_length / decr_secsize;
175 /* Number of sectors from encrypted provider, eg. 18. */
176 nsec = (nsec * sc->sc_bytes_per_sector) / encr_secsize;
177 /* Last sector number in every big sector, eg. 9. */
178 lsec = sc->sc_bytes_per_sector / encr_secsize;
180 srcdata = bp->bio_driver2;
181 dstdata = bp->bio_data;
182 auth = srcdata + encr_secsize * nsec;
186 for (i = 1; i <= nsec; i++) {
187 data_secsize = sc->sc_data_per_sector;
189 data_secsize = decr_secsize % data_secsize;
190 if (bcmp(srcdata, auth, sc->sc_alen) != 0) {
192 * Curruption detected, remember the offset if
193 * this is the first corrupted sector and
196 if (bp->bio_error == 0)
199 coroff = bp->bio_offset +
200 (dstdata - (u_char *)bp->bio_data);
202 corsize += data_secsize;
205 * No curruption, good.
206 * Report previous corruption if there was one.
209 G_ELI_DEBUG(0, "%s: Failed to authenticate %jd "
210 "bytes of data at offset %jd.",
211 sc->sc_name, (intmax_t)corsize,
216 bcopy(srcdata + sc->sc_alen, dstdata,
219 srcdata += encr_secsize;
220 dstdata += data_secsize;
223 /* Report previous corruption if there was one. */
225 G_ELI_DEBUG(0, "%s: Failed to authenticate %jd "
226 "bytes of data at offset %jd.",
227 sc->sc_name, (intmax_t)corsize, (intmax_t)coroff);
230 free(bp->bio_driver2, M_ELI);
231 bp->bio_driver2 = NULL;
232 if (bp->bio_error != 0) {
233 if (bp->bio_error == -1)
234 bp->bio_error = EINVAL;
237 "Crypto READ request failed (error=%d).",
240 bp->bio_completed = 0;
243 * Read is finished, send it up.
245 g_io_deliver(bp, bp->bio_error);
246 atomic_subtract_int(&sc->sc_inflight, 1);
251 * The function is called after data encryption.
253 * g_eli_start -> g_eli_auth_run -> G_ELI_AUTH_WRITE_DONE -> g_io_request -> g_eli_write_done -> g_io_deliver
256 g_eli_auth_write_done(struct cryptop *crp)
258 struct g_eli_softc *sc;
259 struct g_consumer *cp;
260 struct bio *bp, *cbp, *cbp2;
263 if (crp->crp_etype == EAGAIN) {
264 if (g_eli_crypto_rerun(crp) == 0)
267 bp = (struct bio *)crp->crp_opaque;
269 if (crp->crp_etype == 0) {
270 G_ELI_DEBUG(3, "Crypto WRITE request done (%d/%d).",
271 bp->bio_inbed, bp->bio_children);
273 G_ELI_DEBUG(1, "Crypto WRITE request failed (%d/%d) error=%d.",
274 bp->bio_inbed, bp->bio_children, crp->crp_etype);
275 if (bp->bio_error == 0)
276 bp->bio_error = crp->crp_etype;
278 sc = bp->bio_to->geom->softc;
279 g_eli_key_drop(sc, crp->crp_desc->crd_key);
281 * All sectors are already encrypted?
283 if (bp->bio_inbed < bp->bio_children)
285 if (bp->bio_error != 0) {
286 G_ELI_LOGREQ(0, bp, "Crypto WRITE request failed (error=%d).",
288 free(bp->bio_driver2, M_ELI);
289 bp->bio_driver2 = NULL;
290 cbp = bp->bio_driver1;
291 bp->bio_driver1 = NULL;
293 g_io_deliver(bp, bp->bio_error);
294 atomic_subtract_int(&sc->sc_inflight, 1);
297 cp = LIST_FIRST(&sc->sc_geom->consumer);
298 cbp = bp->bio_driver1;
299 bp->bio_driver1 = NULL;
300 cbp->bio_to = cp->provider;
301 cbp->bio_done = g_eli_write_done;
303 /* Number of sectors from decrypted provider, eg. 1. */
304 nsec = bp->bio_length / bp->bio_to->sectorsize;
305 /* Number of sectors from encrypted provider, eg. 9. */
306 nsec = (nsec * sc->sc_bytes_per_sector) / cp->provider->sectorsize;
308 cbp->bio_length = cp->provider->sectorsize * nsec;
309 cbp->bio_offset = (bp->bio_offset / bp->bio_to->sectorsize) * sc->sc_bytes_per_sector;
310 cbp->bio_data = bp->bio_driver2;
313 * We write more than what is requested, so we have to be ready to write
317 if (cbp->bio_length > MAXPHYS) {
318 cbp2 = g_duplicate_bio(bp);
319 cbp2->bio_length = cbp->bio_length - MAXPHYS;
320 cbp2->bio_data = cbp->bio_data + MAXPHYS;
321 cbp2->bio_offset = cbp->bio_offset + MAXPHYS;
322 cbp2->bio_to = cp->provider;
323 cbp2->bio_done = g_eli_write_done;
324 cbp->bio_length = MAXPHYS;
327 * Send encrypted data to the provider.
329 G_ELI_LOGREQ(2, cbp, "Sending request.");
331 bp->bio_children = (cbp2 != NULL ? 2 : 1);
332 g_io_request(cbp, cp);
334 G_ELI_LOGREQ(2, cbp2, "Sending request.");
335 g_io_request(cbp2, cp);
341 g_eli_auth_read(struct g_eli_softc *sc, struct bio *bp)
343 struct g_consumer *cp;
344 struct bio *cbp, *cbp2;
350 cp = LIST_FIRST(&sc->sc_geom->consumer);
351 cbp = bp->bio_driver1;
352 bp->bio_driver1 = NULL;
353 cbp->bio_to = cp->provider;
354 cbp->bio_done = g_eli_read_done;
356 /* Number of sectors from decrypted provider, eg. 1. */
357 nsec = bp->bio_length / bp->bio_to->sectorsize;
358 /* Number of sectors from encrypted provider, eg. 9. */
359 nsec = (nsec * sc->sc_bytes_per_sector) / cp->provider->sectorsize;
361 cbp->bio_length = cp->provider->sectorsize * nsec;
362 size = cbp->bio_length;
363 size += sc->sc_alen * nsec;
364 size += sizeof(struct cryptop) * nsec;
365 size += sizeof(struct cryptodesc) * nsec * 2;
366 size += G_ELI_AUTH_SECKEYLEN * nsec;
367 cbp->bio_offset = (bp->bio_offset / bp->bio_to->sectorsize) * sc->sc_bytes_per_sector;
368 bp->bio_driver2 = malloc(size, M_ELI, M_WAITOK);
369 cbp->bio_data = bp->bio_driver2;
372 * We read more than what is requested, so we have to be ready to read
376 if (cbp->bio_length > MAXPHYS) {
377 cbp2 = g_duplicate_bio(bp);
378 cbp2->bio_length = cbp->bio_length - MAXPHYS;
379 cbp2->bio_data = cbp->bio_data + MAXPHYS;
380 cbp2->bio_offset = cbp->bio_offset + MAXPHYS;
381 cbp2->bio_to = cp->provider;
382 cbp2->bio_done = g_eli_read_done;
383 cbp->bio_length = MAXPHYS;
386 * Read encrypted data from provider.
388 G_ELI_LOGREQ(2, cbp, "Sending request.");
389 g_io_request(cbp, cp);
391 G_ELI_LOGREQ(2, cbp2, "Sending request.");
392 g_io_request(cbp2, cp);
397 * This is the main function responsible for cryptography (ie. communication
398 * with crypto(9) subsystem).
401 * 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
403 * g_eli_start -> G_ELI_AUTH_RUN -> g_eli_auth_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
406 g_eli_auth_run(struct g_eli_worker *wr, struct bio *bp)
408 struct g_eli_softc *sc;
410 struct cryptodesc *crde, *crda;
411 u_int i, lsec, nsec, data_secsize, decr_secsize, encr_secsize;
413 u_char *p, *data, *auth, *authkey, *plaindata;
416 G_ELI_LOGREQ(3, bp, "%s", __func__);
418 bp->bio_pflags = wr->w_number;
420 /* Sectorsize of decrypted provider eg. 4096. */
421 decr_secsize = bp->bio_to->sectorsize;
422 /* The real sectorsize of encrypted provider, eg. 512. */
423 encr_secsize = LIST_FIRST(&sc->sc_geom->consumer)->provider->sectorsize;
424 /* Number of data bytes in one encrypted sector, eg. 480. */
425 data_secsize = sc->sc_data_per_sector;
426 /* Number of sectors from decrypted provider, eg. 2. */
427 nsec = bp->bio_length / decr_secsize;
428 /* Number of sectors from encrypted provider, eg. 18. */
429 nsec = (nsec * sc->sc_bytes_per_sector) / encr_secsize;
430 /* Last sector number in every big sector, eg. 9. */
431 lsec = sc->sc_bytes_per_sector / encr_secsize;
432 /* Destination offset, used for IV generation. */
433 dstoff = (bp->bio_offset / bp->bio_to->sectorsize) * sc->sc_bytes_per_sector;
435 auth = NULL; /* Silence compiler warning. */
436 plaindata = bp->bio_data;
437 if (bp->bio_cmd == BIO_READ) {
438 data = bp->bio_driver2;
439 auth = data + encr_secsize * nsec;
440 p = auth + sc->sc_alen * nsec;
444 size = encr_secsize * nsec;
445 size += sizeof(*crp) * nsec;
446 size += sizeof(*crde) * nsec;
447 size += sizeof(*crda) * nsec;
448 size += G_ELI_AUTH_SECKEYLEN * nsec;
449 size += sizeof(uintptr_t); /* Space for alignment. */
450 data = malloc(size, M_ELI, M_WAITOK);
451 bp->bio_driver2 = data;
452 p = data + encr_secsize * nsec;
455 bp->bio_children = nsec;
457 #if defined(__mips_n64) || defined(__mips_o64)
458 p = (char *)roundup((uintptr_t)p, sizeof(uintptr_t));
461 for (i = 1; i <= nsec; i++, dstoff += encr_secsize) {
462 crp = (struct cryptop *)p; p += sizeof(*crp);
463 crde = (struct cryptodesc *)p; p += sizeof(*crde);
464 crda = (struct cryptodesc *)p; p += sizeof(*crda);
465 authkey = (u_char *)p; p += G_ELI_AUTH_SECKEYLEN;
467 data_secsize = sc->sc_data_per_sector;
468 if ((i % lsec) == 0) {
469 data_secsize = decr_secsize % data_secsize;
471 * Last encrypted sector of each decrypted sector is
472 * only partially filled.
474 if (bp->bio_cmd == BIO_WRITE)
475 memset(data + sc->sc_alen + data_secsize, 0,
476 encr_secsize - sc->sc_alen - data_secsize);
479 if (bp->bio_cmd == BIO_READ) {
480 /* Remember read HMAC. */
481 bcopy(data, auth, sc->sc_alen);
483 /* TODO: bzero(9) can be commented out later. */
484 bzero(data, sc->sc_alen);
486 bcopy(plaindata, data + sc->sc_alen, data_secsize);
487 plaindata += data_secsize;
490 crp->crp_session = wr->w_sid;
491 crp->crp_ilen = sc->sc_alen + data_secsize;
492 crp->crp_olen = data_secsize;
493 crp->crp_opaque = (void *)bp;
494 crp->crp_buf = (void *)data;
495 data += encr_secsize;
496 crp->crp_flags = CRYPTO_F_CBIFSYNC;
498 crp->crp_flags |= CRYPTO_F_BATCH;
499 if (bp->bio_cmd == BIO_WRITE) {
500 crp->crp_callback = g_eli_auth_write_done;
501 crp->crp_desc = crde;
502 crde->crd_next = crda;
503 crda->crd_next = NULL;
505 crp->crp_callback = g_eli_auth_read_done;
506 crp->crp_desc = crda;
507 crda->crd_next = crde;
508 crde->crd_next = NULL;
511 crde->crd_skip = sc->sc_alen;
512 crde->crd_len = data_secsize;
513 crde->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
514 if ((sc->sc_flags & G_ELI_FLAG_FIRST_KEY) == 0)
515 crde->crd_flags |= CRD_F_KEY_EXPLICIT;
516 if (bp->bio_cmd == BIO_WRITE)
517 crde->crd_flags |= CRD_F_ENCRYPT;
518 crde->crd_alg = sc->sc_ealgo;
519 crde->crd_key = g_eli_key_hold(sc, dstoff, encr_secsize);
520 crde->crd_klen = sc->sc_ekeylen;
521 if (sc->sc_ealgo == CRYPTO_AES_XTS)
522 crde->crd_klen <<= 1;
523 g_eli_crypto_ivgen(sc, dstoff, crde->crd_iv,
524 sizeof(crde->crd_iv));
526 crda->crd_skip = sc->sc_alen;
527 crda->crd_len = data_secsize;
528 crda->crd_inject = 0;
529 crda->crd_flags = CRD_F_KEY_EXPLICIT;
530 crda->crd_alg = sc->sc_aalgo;
531 g_eli_auth_keygen(sc, dstoff, authkey);
532 crda->crd_key = authkey;
533 crda->crd_klen = G_ELI_AUTH_SECKEYLEN * 8;
536 error = crypto_dispatch(crp);
537 KASSERT(error == 0, ("crypto_dispatch() failed (error=%d)",