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