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>
44 #include <sys/vnode.h>
48 #include <geom/geom.h>
49 #include <geom/eli/g_eli.h>
50 #include <geom/eli/pkcs5v2.h>
55 * g_eli_start -> g_eli_crypto_read -> g_io_request -> g_eli_read_done -> g_eli_crypto_run -> g_eli_crypto_read_done -> g_io_deliver
57 * g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
60 MALLOC_DECLARE(M_ELI);
63 * The function is called after we read and decrypt data.
65 * g_eli_start -> g_eli_crypto_read -> g_io_request -> g_eli_read_done -> g_eli_crypto_run -> G_ELI_CRYPTO_READ_DONE -> g_io_deliver
68 g_eli_crypto_read_done(struct cryptop *crp)
70 struct g_eli_softc *sc;
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;
89 sc = bp->bio_to->geom->softc;
90 g_eli_key_drop(sc, crp->crp_desc->crd_key);
92 * Do we have all sectors already?
94 if (bp->bio_inbed < bp->bio_children)
96 free(bp->bio_driver2, M_ELI);
97 bp->bio_driver2 = NULL;
98 if (bp->bio_error != 0) {
99 G_ELI_LOGREQ(0, bp, "Crypto READ request failed (error=%d).",
101 bp->bio_completed = 0;
104 * Read is finished, send it up.
106 g_io_deliver(bp, bp->bio_error);
107 atomic_subtract_int(&sc->sc_inflight, 1);
112 * The function is called after data encryption.
114 * g_eli_start -> g_eli_crypto_run -> G_ELI_CRYPTO_WRITE_DONE -> g_io_request -> g_eli_write_done -> g_io_deliver
117 g_eli_crypto_write_done(struct cryptop *crp)
119 struct g_eli_softc *sc;
121 struct g_consumer *cp;
122 struct bio *bp, *cbp;
124 if (crp->crp_etype == EAGAIN) {
125 if (g_eli_crypto_rerun(crp) == 0)
128 bp = (struct bio *)crp->crp_opaque;
130 if (crp->crp_etype == 0) {
131 G_ELI_DEBUG(3, "Crypto WRITE request done (%d/%d).",
132 bp->bio_inbed, bp->bio_children);
134 G_ELI_DEBUG(1, "Crypto WRITE request failed (%d/%d) error=%d.",
135 bp->bio_inbed, bp->bio_children, crp->crp_etype);
136 if (bp->bio_error == 0)
137 bp->bio_error = crp->crp_etype;
139 gp = bp->bio_to->geom;
141 g_eli_key_drop(sc, crp->crp_desc->crd_key);
143 * All sectors are already encrypted?
145 if (bp->bio_inbed < bp->bio_children)
148 bp->bio_children = 1;
149 cbp = bp->bio_driver1;
150 bp->bio_driver1 = NULL;
151 if (bp->bio_error != 0) {
152 G_ELI_LOGREQ(0, bp, "Crypto WRITE request failed (error=%d).",
154 free(bp->bio_driver2, M_ELI);
155 bp->bio_driver2 = NULL;
157 g_io_deliver(bp, bp->bio_error);
158 atomic_subtract_int(&sc->sc_inflight, 1);
161 cbp->bio_data = bp->bio_driver2;
162 cbp->bio_done = g_eli_write_done;
163 cp = LIST_FIRST(&gp->consumer);
164 cbp->bio_to = cp->provider;
165 G_ELI_LOGREQ(2, cbp, "Sending request.");
167 * Send encrypted data to the provider.
169 g_io_request(cbp, cp);
174 * The function is called to read encrypted data.
176 * g_eli_start -> G_ELI_CRYPTO_READ -> g_io_request -> g_eli_read_done -> g_eli_crypto_run -> g_eli_crypto_read_done -> g_io_deliver
179 g_eli_crypto_read(struct g_eli_softc *sc, struct bio *bp, boolean_t fromworker)
181 struct g_consumer *cp;
186 * We are not called from the worker thread, so check if
187 * device is suspended.
189 mtx_lock(&sc->sc_queue_mtx);
190 if (sc->sc_flags & G_ELI_FLAG_SUSPEND) {
192 * If device is suspended, we place the request onto
193 * the queue, so it can be handled after resume.
195 G_ELI_DEBUG(0, "device suspended, move onto queue");
196 bioq_insert_tail(&sc->sc_queue, bp);
197 mtx_unlock(&sc->sc_queue_mtx);
201 atomic_add_int(&sc->sc_inflight, 1);
202 mtx_unlock(&sc->sc_queue_mtx);
205 bp->bio_driver2 = NULL;
206 cbp = bp->bio_driver1;
207 cbp->bio_done = g_eli_read_done;
208 cp = LIST_FIRST(&sc->sc_geom->consumer);
209 cbp->bio_to = cp->provider;
210 G_ELI_LOGREQ(2, cbp, "Sending request.");
212 * Read encrypted data from provider.
214 g_io_request(cbp, cp);
218 * This is the main function responsible for cryptography (ie. communication
219 * with crypto(9) subsystem).
222 * g_eli_start -> g_eli_crypto_read -> g_io_request -> g_eli_read_done -> G_ELI_CRYPTO_RUN -> g_eli_crypto_read_done -> g_io_deliver
224 * g_eli_start -> G_ELI_CRYPTO_RUN -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
227 g_eli_crypto_run(struct g_eli_worker *wr, struct bio *bp)
229 struct g_eli_softc *sc;
231 struct cryptodesc *crd;
232 u_int i, nsec, secsize;
238 G_ELI_LOGREQ(3, bp, "%s", __func__);
240 bp->bio_pflags = wr->w_number;
242 secsize = LIST_FIRST(&sc->sc_geom->provider)->sectorsize;
243 nsec = bp->bio_length / secsize;
246 * Calculate how much memory do we need.
247 * We need separate crypto operation for every single sector.
248 * It is much faster to calculate total amount of needed memory here and
249 * do the allocation once instead of allocating memory in pieces (many,
252 size = sizeof(*crp) * nsec;
253 size += sizeof(*crd) * nsec;
255 * If we write the data we cannot destroy current bio_data content,
256 * so we need to allocate more memory for encrypted data.
258 if (bp->bio_cmd == BIO_WRITE)
259 size += bp->bio_length;
260 p = malloc(size, M_ELI, M_WAITOK);
263 bp->bio_children = nsec;
266 if (bp->bio_cmd == BIO_READ)
271 bcopy(bp->bio_data, data, bp->bio_length);
275 for (i = 0, dstoff = bp->bio_offset; i < nsec; i++, dstoff += secsize) {
276 crp = (struct cryptop *)p; p += sizeof(*crp);
277 crd = (struct cryptodesc *)p; p += sizeof(*crd);
279 crp->crp_sid = wr->w_sid;
280 crp->crp_ilen = secsize;
281 crp->crp_olen = secsize;
282 crp->crp_opaque = (void *)bp;
283 crp->crp_buf = (void *)data;
285 if (bp->bio_cmd == BIO_WRITE)
286 crp->crp_callback = g_eli_crypto_write_done;
287 else /* if (bp->bio_cmd == BIO_READ) */
288 crp->crp_callback = g_eli_crypto_read_done;
289 crp->crp_flags = CRYPTO_F_CBIFSYNC;
291 crp->crp_flags |= CRYPTO_F_BATCH;
295 crd->crd_len = secsize;
296 crd->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
297 if ((sc->sc_flags & G_ELI_FLAG_SINGLE_KEY) == 0)
298 crd->crd_flags |= CRD_F_KEY_EXPLICIT;
299 if (bp->bio_cmd == BIO_WRITE)
300 crd->crd_flags |= CRD_F_ENCRYPT;
301 crd->crd_alg = sc->sc_ealgo;
302 crd->crd_key = g_eli_key_hold(sc, dstoff, secsize);
303 crd->crd_klen = sc->sc_ekeylen;
304 if (sc->sc_ealgo == CRYPTO_AES_XTS)
306 g_eli_crypto_ivgen(sc, dstoff, crd->crd_iv,
307 sizeof(crd->crd_iv));
308 crd->crd_next = NULL;
311 err = crypto_dispatch(crp);
315 if (bp->bio_error == 0)
316 bp->bio_error = error;