Deleted Added
full compact
g_eli_privacy.c (214116) g_eli_privacy.c (214118)
1/*-
2 * Copyright (c) 2005-2010 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 2005-2010 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/geom/eli/g_eli_privacy.c 214116 2010-10-20 20:01:45Z pjd $");
28__FBSDID("$FreeBSD: head/sys/geom/eli/g_eli_privacy.c 214118 2010-10-20 20:50:55Z pjd $");
29
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>
35#include <sys/lock.h>
36#include <sys/mutex.h>
37#include <sys/bio.h>
38#include <sys/sysctl.h>
39#include <sys/malloc.h>
40#include <sys/kthread.h>
41#include <sys/proc.h>
42#include <sys/sched.h>
43#include <sys/smp.h>
44#include <sys/uio.h>
45#include <sys/vnode.h>
46
47#include <vm/uma.h>
48
49#include <geom/geom.h>
50#include <geom/eli/g_eli.h>
51#include <geom/eli/pkcs5v2.h>
52
53/*
54 * Code paths:
55 * BIO_READ:
29
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>
35#include <sys/lock.h>
36#include <sys/mutex.h>
37#include <sys/bio.h>
38#include <sys/sysctl.h>
39#include <sys/malloc.h>
40#include <sys/kthread.h>
41#include <sys/proc.h>
42#include <sys/sched.h>
43#include <sys/smp.h>
44#include <sys/uio.h>
45#include <sys/vnode.h>
46
47#include <vm/uma.h>
48
49#include <geom/geom.h>
50#include <geom/eli/g_eli.h>
51#include <geom/eli/pkcs5v2.h>
52
53/*
54 * Code paths:
55 * BIO_READ:
56 * g_eli_start -> g_io_request -> g_eli_read_done -> g_eli_crypto_run -> g_eli_crypto_read_done -> g_io_deliver
56 * 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 * BIO_WRITE:
58 * g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
59 */
60
61MALLOC_DECLARE(M_ELI);
62
63/*
64 * The function is called after we read and decrypt data.
65 *
57 * BIO_WRITE:
58 * g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
59 */
60
61MALLOC_DECLARE(M_ELI);
62
63/*
64 * The function is called after we read and decrypt data.
65 *
66 * g_eli_start -> g_io_request -> g_eli_read_done -> g_eli_crypto_run -> G_ELI_CRYPTO_READ_DONE -> g_io_deliver
66 * 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
67 */
68static int
69g_eli_crypto_read_done(struct cryptop *crp)
70{
67 */
68static int
69g_eli_crypto_read_done(struct cryptop *crp)
70{
71 struct g_eli_softc *sc;
71 struct bio *bp;
72
73 if (crp->crp_etype == EAGAIN) {
74 if (g_eli_crypto_rerun(crp) == 0)
75 return (0);
76 }
77 bp = (struct bio *)crp->crp_opaque;
78 bp->bio_inbed++;
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;
83 } else {
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;
88 }
89 /*
90 * Do we have all sectors already?
91 */
92 if (bp->bio_inbed < bp->bio_children)
93 return (0);
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).",
98 bp->bio_error);
99 bp->bio_completed = 0;
100 }
101 /*
102 * Read is finished, send it up.
103 */
72 struct bio *bp;
73
74 if (crp->crp_etype == EAGAIN) {
75 if (g_eli_crypto_rerun(crp) == 0)
76 return (0);
77 }
78 bp = (struct bio *)crp->crp_opaque;
79 bp->bio_inbed++;
80 if (crp->crp_etype == 0) {
81 G_ELI_DEBUG(3, "Crypto READ request done (%d/%d).",
82 bp->bio_inbed, bp->bio_children);
83 bp->bio_completed += crp->crp_olen;
84 } else {
85 G_ELI_DEBUG(1, "Crypto READ request failed (%d/%d) error=%d.",
86 bp->bio_inbed, bp->bio_children, crp->crp_etype);
87 if (bp->bio_error == 0)
88 bp->bio_error = crp->crp_etype;
89 }
90 /*
91 * Do we have all sectors already?
92 */
93 if (bp->bio_inbed < bp->bio_children)
94 return (0);
95 free(bp->bio_driver2, M_ELI);
96 bp->bio_driver2 = NULL;
97 if (bp->bio_error != 0) {
98 G_ELI_LOGREQ(0, bp, "Crypto READ request failed (error=%d).",
99 bp->bio_error);
100 bp->bio_completed = 0;
101 }
102 /*
103 * Read is finished, send it up.
104 */
105 sc = bp->bio_to->geom->softc;
104 g_io_deliver(bp, bp->bio_error);
106 g_io_deliver(bp, bp->bio_error);
107 atomic_subtract_int(&sc->sc_inflight, 1);
105 return (0);
106}
107
108/*
109 * The function is called after data encryption.
110 *
111 * g_eli_start -> g_eli_crypto_run -> G_ELI_CRYPTO_WRITE_DONE -> g_io_request -> g_eli_write_done -> g_io_deliver
112 */
113static int
114g_eli_crypto_write_done(struct cryptop *crp)
115{
108 return (0);
109}
110
111/*
112 * The function is called after data encryption.
113 *
114 * g_eli_start -> g_eli_crypto_run -> G_ELI_CRYPTO_WRITE_DONE -> g_io_request -> g_eli_write_done -> g_io_deliver
115 */
116static int
117g_eli_crypto_write_done(struct cryptop *crp)
118{
119 struct g_eli_softc *sc;
116 struct g_geom *gp;
117 struct g_consumer *cp;
118 struct bio *bp, *cbp;
119
120 if (crp->crp_etype == EAGAIN) {
121 if (g_eli_crypto_rerun(crp) == 0)
122 return (0);
123 }
124 bp = (struct bio *)crp->crp_opaque;
125 bp->bio_inbed++;
126 if (crp->crp_etype == 0) {
127 G_ELI_DEBUG(3, "Crypto WRITE request done (%d/%d).",
128 bp->bio_inbed, bp->bio_children);
129 } else {
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;
134 }
135 /*
136 * All sectors are already encrypted?
137 */
138 if (bp->bio_inbed < bp->bio_children)
139 return (0);
140 bp->bio_inbed = 0;
141 bp->bio_children = 1;
142 cbp = bp->bio_driver1;
143 bp->bio_driver1 = NULL;
120 struct g_geom *gp;
121 struct g_consumer *cp;
122 struct bio *bp, *cbp;
123
124 if (crp->crp_etype == EAGAIN) {
125 if (g_eli_crypto_rerun(crp) == 0)
126 return (0);
127 }
128 bp = (struct bio *)crp->crp_opaque;
129 bp->bio_inbed++;
130 if (crp->crp_etype == 0) {
131 G_ELI_DEBUG(3, "Crypto WRITE request done (%d/%d).",
132 bp->bio_inbed, bp->bio_children);
133 } else {
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;
138 }
139 /*
140 * All sectors are already encrypted?
141 */
142 if (bp->bio_inbed < bp->bio_children)
143 return (0);
144 bp->bio_inbed = 0;
145 bp->bio_children = 1;
146 cbp = bp->bio_driver1;
147 bp->bio_driver1 = NULL;
148 gp = bp->bio_to->geom;
144 if (bp->bio_error != 0) {
145 G_ELI_LOGREQ(0, bp, "Crypto WRITE request failed (error=%d).",
146 bp->bio_error);
147 free(bp->bio_driver2, M_ELI);
148 bp->bio_driver2 = NULL;
149 g_destroy_bio(cbp);
149 if (bp->bio_error != 0) {
150 G_ELI_LOGREQ(0, bp, "Crypto WRITE request failed (error=%d).",
151 bp->bio_error);
152 free(bp->bio_driver2, M_ELI);
153 bp->bio_driver2 = NULL;
154 g_destroy_bio(cbp);
155 sc = gp->softc;
150 g_io_deliver(bp, bp->bio_error);
156 g_io_deliver(bp, bp->bio_error);
157 atomic_subtract_int(&sc->sc_inflight, 1);
151 return (0);
152 }
153 cbp->bio_data = bp->bio_driver2;
154 cbp->bio_done = g_eli_write_done;
158 return (0);
159 }
160 cbp->bio_data = bp->bio_driver2;
161 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.");
159 /*
160 * Send encrypted data to the provider.
161 */
162 g_io_request(cbp, cp);
163 return (0);
164}
165
166/*
162 cp = LIST_FIRST(&gp->consumer);
163 cbp->bio_to = cp->provider;
164 G_ELI_LOGREQ(2, cbp, "Sending request.");
165 /*
166 * Send encrypted data to the provider.
167 */
168 g_io_request(cbp, cp);
169 return (0);
170}
171
172/*
173 * The function is called to read encrypted data.
174 *
175 * 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
176 */
177void
178g_eli_crypto_read(struct g_eli_softc *sc, struct bio *bp, boolean_t fromworker)
179{
180 struct g_consumer *cp;
181 struct bio *cbp;
182
183 if (!fromworker) {
184 /*
185 * We are not called from the worker thread, so check if
186 * device is suspended.
187 */
188 mtx_lock(&sc->sc_queue_mtx);
189 if (sc->sc_flags & G_ELI_FLAG_SUSPEND) {
190 /*
191 * If device is suspended, we place the request onto
192 * the queue, so it can be handled after resume.
193 */
194 G_ELI_DEBUG(0, "device suspended, move onto queue");
195 bioq_insert_tail(&sc->sc_queue, bp);
196 mtx_unlock(&sc->sc_queue_mtx);
197 wakeup(sc);
198 return;
199 }
200 atomic_add_int(&sc->sc_inflight, 1);
201 mtx_unlock(&sc->sc_queue_mtx);
202 }
203 bp->bio_pflags = 0;
204 bp->bio_driver2 = NULL;
205 cbp = bp->bio_driver1;
206 cbp->bio_done = g_eli_read_done;
207 cp = LIST_FIRST(&sc->sc_geom->consumer);
208 cbp->bio_to = cp->provider;
209 G_ELI_LOGREQ(2, cbp, "Sending request.");
210 /*
211 * Read encrypted data from provider.
212 */
213 g_io_request(cbp, cp);
214}
215
216/*
167 * This is the main function responsible for cryptography (ie. communication
168 * with crypto(9) subsystem).
169 *
170 * BIO_READ:
217 * This is the main function responsible for cryptography (ie. communication
218 * with crypto(9) subsystem).
219 *
220 * BIO_READ:
171 * g_eli_start -> g_io_request -> g_eli_read_done -> G_ELI_CRYPTO_RUN -> g_eli_crypto_read_done -> g_io_deliver
221 * 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
172 * BIO_WRITE:
173 * g_eli_start -> G_ELI_CRYPTO_RUN -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
174 */
175void
176g_eli_crypto_run(struct g_eli_worker *wr, struct bio *bp)
177{
178 struct g_eli_softc *sc;
179 struct cryptop *crp;
180 struct cryptodesc *crd;
181 struct uio *uio;
182 struct iovec *iov;
183 u_int i, nsec, secsize;
184 int err, error;
185 off_t dstoff;
186 size_t size;
187 u_char *p, *data;
188
189 G_ELI_LOGREQ(3, bp, "%s", __func__);
190
191 bp->bio_pflags = wr->w_number;
192 sc = wr->w_softc;
193 secsize = LIST_FIRST(&sc->sc_geom->provider)->sectorsize;
194 nsec = bp->bio_length / secsize;
195
196 /*
197 * Calculate how much memory do we need.
198 * We need separate crypto operation for every single sector.
199 * It is much faster to calculate total amount of needed memory here and
200 * do the allocation once instead of allocating memory in pieces (many,
201 * many pieces).
202 */
203 size = sizeof(*crp) * nsec;
204 size += sizeof(*crd) * nsec;
205 size += sizeof(*uio) * nsec;
206 size += sizeof(*iov) * nsec;
207 /*
208 * If we write the data we cannot destroy current bio_data content,
209 * so we need to allocate more memory for encrypted data.
210 */
211 if (bp->bio_cmd == BIO_WRITE)
212 size += bp->bio_length;
213 p = malloc(size, M_ELI, M_WAITOK);
214
215 bp->bio_inbed = 0;
216 bp->bio_children = nsec;
217 bp->bio_driver2 = p;
218
219 if (bp->bio_cmd == BIO_READ)
220 data = bp->bio_data;
221 else {
222 data = p;
223 p += bp->bio_length;
224 bcopy(bp->bio_data, data, bp->bio_length);
225 }
226
227 error = 0;
228 for (i = 0, dstoff = bp->bio_offset; i < nsec; i++, dstoff += secsize) {
229 crp = (struct cryptop *)p; p += sizeof(*crp);
230 crd = (struct cryptodesc *)p; p += sizeof(*crd);
231 uio = (struct uio *)p; p += sizeof(*uio);
232 iov = (struct iovec *)p; p += sizeof(*iov);
233
234 iov->iov_len = secsize;
235 iov->iov_base = data;
236 data += secsize;
237
238 uio->uio_iov = iov;
239 uio->uio_iovcnt = 1;
240 uio->uio_segflg = UIO_SYSSPACE;
241 uio->uio_resid = secsize;
242
243 crp->crp_sid = wr->w_sid;
244 crp->crp_ilen = secsize;
245 crp->crp_olen = secsize;
246 crp->crp_opaque = (void *)bp;
247 crp->crp_buf = (void *)uio;
248 if (bp->bio_cmd == BIO_WRITE)
249 crp->crp_callback = g_eli_crypto_write_done;
250 else /* if (bp->bio_cmd == BIO_READ) */
251 crp->crp_callback = g_eli_crypto_read_done;
252 crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIFSYNC | CRYPTO_F_REL;
253 if (g_eli_batch)
254 crp->crp_flags |= CRYPTO_F_BATCH;
255 crp->crp_desc = crd;
256
257 crd->crd_skip = 0;
258 crd->crd_len = secsize;
259 crd->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
260 if (sc->sc_nekeys > 1)
261 crd->crd_flags |= CRD_F_KEY_EXPLICIT;
262 if (bp->bio_cmd == BIO_WRITE)
263 crd->crd_flags |= CRD_F_ENCRYPT;
264 crd->crd_alg = sc->sc_ealgo;
265 crd->crd_key = g_eli_crypto_key(sc, dstoff, secsize);
266 crd->crd_klen = sc->sc_ekeylen;
267 if (sc->sc_ealgo == CRYPTO_AES_XTS)
268 crd->crd_klen <<= 1;
269 g_eli_crypto_ivgen(sc, dstoff, crd->crd_iv,
270 sizeof(crd->crd_iv));
271 crd->crd_next = NULL;
272
273 crp->crp_etype = 0;
274 err = crypto_dispatch(crp);
275 if (error == 0)
276 error = err;
277 }
278 if (bp->bio_error == 0)
279 bp->bio_error = error;
280}
222 * BIO_WRITE:
223 * g_eli_start -> G_ELI_CRYPTO_RUN -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
224 */
225void
226g_eli_crypto_run(struct g_eli_worker *wr, struct bio *bp)
227{
228 struct g_eli_softc *sc;
229 struct cryptop *crp;
230 struct cryptodesc *crd;
231 struct uio *uio;
232 struct iovec *iov;
233 u_int i, nsec, secsize;
234 int err, error;
235 off_t dstoff;
236 size_t size;
237 u_char *p, *data;
238
239 G_ELI_LOGREQ(3, bp, "%s", __func__);
240
241 bp->bio_pflags = wr->w_number;
242 sc = wr->w_softc;
243 secsize = LIST_FIRST(&sc->sc_geom->provider)->sectorsize;
244 nsec = bp->bio_length / secsize;
245
246 /*
247 * Calculate how much memory do we need.
248 * We need separate crypto operation for every single sector.
249 * It is much faster to calculate total amount of needed memory here and
250 * do the allocation once instead of allocating memory in pieces (many,
251 * many pieces).
252 */
253 size = sizeof(*crp) * nsec;
254 size += sizeof(*crd) * nsec;
255 size += sizeof(*uio) * nsec;
256 size += sizeof(*iov) * nsec;
257 /*
258 * If we write the data we cannot destroy current bio_data content,
259 * so we need to allocate more memory for encrypted data.
260 */
261 if (bp->bio_cmd == BIO_WRITE)
262 size += bp->bio_length;
263 p = malloc(size, M_ELI, M_WAITOK);
264
265 bp->bio_inbed = 0;
266 bp->bio_children = nsec;
267 bp->bio_driver2 = p;
268
269 if (bp->bio_cmd == BIO_READ)
270 data = bp->bio_data;
271 else {
272 data = p;
273 p += bp->bio_length;
274 bcopy(bp->bio_data, data, bp->bio_length);
275 }
276
277 error = 0;
278 for (i = 0, dstoff = bp->bio_offset; i < nsec; i++, dstoff += secsize) {
279 crp = (struct cryptop *)p; p += sizeof(*crp);
280 crd = (struct cryptodesc *)p; p += sizeof(*crd);
281 uio = (struct uio *)p; p += sizeof(*uio);
282 iov = (struct iovec *)p; p += sizeof(*iov);
283
284 iov->iov_len = secsize;
285 iov->iov_base = data;
286 data += secsize;
287
288 uio->uio_iov = iov;
289 uio->uio_iovcnt = 1;
290 uio->uio_segflg = UIO_SYSSPACE;
291 uio->uio_resid = secsize;
292
293 crp->crp_sid = wr->w_sid;
294 crp->crp_ilen = secsize;
295 crp->crp_olen = secsize;
296 crp->crp_opaque = (void *)bp;
297 crp->crp_buf = (void *)uio;
298 if (bp->bio_cmd == BIO_WRITE)
299 crp->crp_callback = g_eli_crypto_write_done;
300 else /* if (bp->bio_cmd == BIO_READ) */
301 crp->crp_callback = g_eli_crypto_read_done;
302 crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIFSYNC | CRYPTO_F_REL;
303 if (g_eli_batch)
304 crp->crp_flags |= CRYPTO_F_BATCH;
305 crp->crp_desc = crd;
306
307 crd->crd_skip = 0;
308 crd->crd_len = secsize;
309 crd->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
310 if (sc->sc_nekeys > 1)
311 crd->crd_flags |= CRD_F_KEY_EXPLICIT;
312 if (bp->bio_cmd == BIO_WRITE)
313 crd->crd_flags |= CRD_F_ENCRYPT;
314 crd->crd_alg = sc->sc_ealgo;
315 crd->crd_key = g_eli_crypto_key(sc, dstoff, secsize);
316 crd->crd_klen = sc->sc_ekeylen;
317 if (sc->sc_ealgo == CRYPTO_AES_XTS)
318 crd->crd_klen <<= 1;
319 g_eli_crypto_ivgen(sc, dstoff, crd->crd_iv,
320 sizeof(crd->crd_iv));
321 crd->crd_next = NULL;
322
323 crp->crp_etype = 0;
324 err = crypto_dispatch(crp);
325 if (error == 0)
326 error = err;
327 }
328 if (bp->bio_error == 0)
329 bp->bio_error = error;
330}