2 * Copyright (c) 2005-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
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>
56 * g_eli_start -> g_io_request -> g_eli_read_done -> g_eli_crypto_run -> g_eli_crypto_read_done -> g_io_deliver
58 * g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
61 MALLOC_DECLARE(M_ELI);
64 * The function is called after we read and decrypt data.
66 * g_eli_start -> g_io_request -> g_eli_read_done -> g_eli_crypto_run -> G_ELI_CRYPTO_READ_DONE -> g_io_deliver
69 g_eli_crypto_read_done(struct cryptop *crp)
73 if (crp->crp_etype == EAGAIN) {
74 if (g_eli_crypto_rerun(crp) == 0)
77 bp = (struct bio *)crp->crp_opaque;
79 if (crp->crp_etype == 0) {
80 G_ELI_DEBUG(3, "Crypto READ request done (%d/%d).",
81 bp->bio_inbed, bp->bio_children);
82 bp->bio_completed += crp->crp_olen;
84 G_ELI_DEBUG(1, "Crypto READ request failed (%d/%d) error=%d.",
85 bp->bio_inbed, bp->bio_children, crp->crp_etype);
86 if (bp->bio_error == 0)
87 bp->bio_error = crp->crp_etype;
90 * Do we have all sectors already?
92 if (bp->bio_inbed < bp->bio_children)
94 free(bp->bio_driver2, M_ELI);
95 bp->bio_driver2 = NULL;
96 if (bp->bio_error != 0) {
97 G_ELI_LOGREQ(0, bp, "Crypto READ request failed (error=%d).",
99 bp->bio_completed = 0;
102 * Read is finished, send it up.
104 g_io_deliver(bp, bp->bio_error);
109 * The function is called after data encryption.
111 * g_eli_start -> g_eli_crypto_run -> G_ELI_CRYPTO_WRITE_DONE -> g_io_request -> g_eli_write_done -> g_io_deliver
114 g_eli_crypto_write_done(struct cryptop *crp)
117 struct g_consumer *cp;
118 struct bio *bp, *cbp;
120 if (crp->crp_etype == EAGAIN) {
121 if (g_eli_crypto_rerun(crp) == 0)
124 bp = (struct bio *)crp->crp_opaque;
126 if (crp->crp_etype == 0) {
127 G_ELI_DEBUG(3, "Crypto WRITE request done (%d/%d).",
128 bp->bio_inbed, bp->bio_children);
130 G_ELI_DEBUG(1, "Crypto WRITE request failed (%d/%d) error=%d.",
131 bp->bio_inbed, bp->bio_children, crp->crp_etype);
132 if (bp->bio_error == 0)
133 bp->bio_error = crp->crp_etype;
136 * All sectors are already encrypted?
138 if (bp->bio_inbed < bp->bio_children)
141 bp->bio_children = 1;
142 cbp = bp->bio_driver1;
143 bp->bio_driver1 = NULL;
144 if (bp->bio_error != 0) {
145 G_ELI_LOGREQ(0, bp, "Crypto WRITE request failed (error=%d).",
147 free(bp->bio_driver2, M_ELI);
148 bp->bio_driver2 = NULL;
150 g_io_deliver(bp, bp->bio_error);
153 cbp->bio_data = bp->bio_driver2;
154 cbp->bio_done = g_eli_write_done;
155 gp = bp->bio_to->geom;
156 cp = LIST_FIRST(&gp->consumer);
157 cbp->bio_to = cp->provider;
158 G_ELI_LOGREQ(2, cbp, "Sending request.");
160 * Send encrypted data to the provider.
162 g_io_request(cbp, cp);
167 * This is the main function responsible for cryptography (ie. communication
168 * with crypto(9) subsystem).
171 g_eli_crypto_run(struct g_eli_worker *wr, struct bio *bp)
173 struct g_eli_softc *sc;
175 struct cryptodesc *crd;
178 u_int i, nsec, add, secsize;
183 G_ELI_LOGREQ(3, bp, "%s", __func__);
185 bp->bio_pflags = wr->w_number;
187 secsize = LIST_FIRST(&sc->sc_geom->provider)->sectorsize;
188 nsec = bp->bio_length / secsize;
191 * Calculate how much memory do we need.
192 * We need separate crypto operation for every single sector.
193 * It is much faster to calculate total amount of needed memory here and
194 * do the allocation once instead of allocating memory in pieces (many,
197 size = sizeof(*crp) * nsec;
198 size += sizeof(*crd) * nsec;
199 size += sizeof(*uio) * nsec;
200 size += sizeof(*iov) * nsec;
202 * If we write the data we cannot destroy current bio_data content,
203 * so we need to allocate more memory for encrypted data.
205 if (bp->bio_cmd == BIO_WRITE)
206 size += bp->bio_length;
207 p = malloc(size, M_ELI, M_WAITOK);
210 bp->bio_children = nsec;
213 if (bp->bio_cmd == BIO_READ)
218 bcopy(bp->bio_data, data, bp->bio_length);
222 for (i = 0, add = 0; i < nsec; i++, add += secsize) {
223 crp = (struct cryptop *)p; p += sizeof(*crp);
224 crd = (struct cryptodesc *)p; p += sizeof(*crd);
225 uio = (struct uio *)p; p += sizeof(*uio);
226 iov = (struct iovec *)p; p += sizeof(*iov);
228 iov->iov_len = secsize;
229 iov->iov_base = data;
234 uio->uio_segflg = UIO_SYSSPACE;
235 uio->uio_resid = secsize;
237 crp->crp_sid = wr->w_sid;
238 crp->crp_ilen = secsize;
239 crp->crp_olen = secsize;
240 crp->crp_opaque = (void *)bp;
241 crp->crp_buf = (void *)uio;
242 if (bp->bio_cmd == BIO_WRITE)
243 crp->crp_callback = g_eli_crypto_write_done;
244 else /* if (bp->bio_cmd == BIO_READ) */
245 crp->crp_callback = g_eli_crypto_read_done;
246 crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIFSYNC | CRYPTO_F_REL;
248 crp->crp_flags |= CRYPTO_F_BATCH;
252 crd->crd_len = secsize;
253 crd->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
254 if (bp->bio_cmd == BIO_WRITE)
255 crd->crd_flags |= CRD_F_ENCRYPT;
256 crd->crd_alg = sc->sc_ealgo;
257 crd->crd_key = sc->sc_ekey;
258 crd->crd_klen = sc->sc_ekeylen;
259 g_eli_crypto_ivgen(sc, bp->bio_offset + add, crd->crd_iv,
260 sizeof(crd->crd_iv));
261 crd->crd_next = NULL;
264 err = crypto_dispatch(crp);
268 if (bp->bio_error == 0)
269 bp->bio_error = error;