cgd.c revision 1.1 1 /* $NetBSD: cgd.c,v 1.1 2002/10/04 18:22:35 elric Exp $ */
2
3 /*-
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Roland C. Dowdeswell.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: cgd.c,v 1.1 2002/10/04 18:22:35 elric Exp $");
41
42 #include <sys/types.h>
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/proc.h>
46 #include <sys/errno.h>
47 #include <sys/buf.h>
48 #include <sys/malloc.h>
49 #include <sys/pool.h>
50 #include <sys/ioctl.h>
51 #include <sys/device.h>
52 #include <sys/disk.h>
53 #include <sys/disklabel.h>
54 #include <sys/fcntl.h>
55 #include <sys/vnode.h>
56 #include <sys/lock.h>
57 #include <sys/conf.h>
58
59 #include <dev/dkvar.h>
60 #include <dev/cgdvar.h>
61
62 /* Entry Point Functions */
63
64 void cgdattach(int);
65
66 dev_type_open(cgdopen);
67 dev_type_close(cgdclose);
68 dev_type_read(cgdread);
69 dev_type_write(cgdwrite);
70 dev_type_ioctl(cgdioctl);
71 dev_type_strategy(cgdstrategy);
72 dev_type_dump(cgddump);
73 dev_type_size(cgdsize);
74
75 const struct bdevsw cgd_bdevsw = {
76 cgdopen, cgdclose, cgdstrategy, cgdioctl,
77 cgddump, cgdsize, D_DISK
78 };
79
80 const struct cdevsw cgd_cdevsw = {
81 cgdopen, cgdclose, cgdread, cgdwrite, cgdioctl,
82 nostop, notty, nopoll, nommap, D_DISK
83 };
84
85 /* Internal Functions */
86
87 static void cgdstart(struct dk_softc *, struct buf *);
88 static void cgdiodone(struct buf *);
89
90 static int cgd_ioctl_set(struct cgd_softc *, void *, struct proc *);
91 static int cgd_ioctl_clr(struct cgd_softc *, void *, struct proc *);
92 static int cgdinit(struct cgd_softc *, char *, struct vnode *,
93 struct proc *);
94 static void cgd_cipher(struct cgd_softc *, caddr_t, caddr_t,
95 size_t, daddr_t, size_t, int);
96
97 /* Pseudo-disk Interface */
98
99 static struct dk_intf the_dkintf = {
100 DTYPE_CGD,
101 "cgd",
102 cgdopen,
103 cgdclose,
104 cgdstrategy,
105 cgdstart,
106 };
107 static struct dk_intf *di = &the_dkintf;
108
109 /* DIAGNOSTIC and DEBUG definitions */
110
111 #if defined(CGDDEBUG) && !defined(DEBUG)
112 #define DEBUG
113 #endif
114
115 #ifdef DEBUG
116 int cgddebug = 0;
117
118 #define CGDB_FOLLOW 0x1
119 #define CGDB_IO 0x2
120 #define CGDB_CRYPTO 0x4
121
122 #define IFDEBUG(x,y) if (cgddebug & (x)) y
123 #define DPRINTF(x,y) IFDEBUG(x, printf y)
124 #define DPRINTF_FOLLOW(y) DPRINTF(CGDB_FOLLOW, y)
125
126 static void hexprint(char *, void *, int);
127
128 #else
129 #define IFDEBUG(x,y)
130 #define DPRINTF(x,y)
131 #define DPRINTF_FOLLOW(y)
132 #endif
133
134 #ifdef DIAGNOSTIC
135 #define DIAGPANIC(x) panic x
136 #define DIAGCONDPANIC(x,y) if (x) panic y
137 #else
138 #define DIAGPANIC(x)
139 #define DIAGCONDPANIC(x,y)
140 #endif
141
142 /* Component Buffer Pool structures and macros */
143
144 struct cgdbuf {
145 struct buf cb_buf; /* new I/O buf */
146 struct buf *cb_obp; /* ptr. to original I/O buf */
147 struct cgd_softc *cb_sc; /* pointer to cgd softc */
148 };
149
150 struct pool cgd_cbufpool;
151
152 #define CGD_GETBUF() pool_get(&cgd_cbufpool, PR_NOWAIT)
153 #define CGD_PUTBUF(cbp) pool_put(&cgd_cbufpool, cbp)
154
155 /* Global variables */
156
157 struct cgd_softc *cgd_softc;
158 int numcgd = 0;
159
160 /* Utility Functions */
161
162 #define CGDUNIT(x) DISKUNIT(x)
163 #define GETCGD_SOFTC(_cs, x) if (!((_cs) = getcgd_softc(x))) return ENXIO
164
165 static struct cgd_softc *
166 getcgd_softc(dev_t dev)
167 {
168 int unit = CGDUNIT(dev);
169
170 DPRINTF_FOLLOW(("getcgd_softc(0x%x): unit = %d\n", dev, unit));
171 if (unit >= numcgd)
172 return NULL;
173 return &cgd_softc[unit];
174 }
175
176 /* The code */
177
178 static void
179 cgdsoftc_init(struct cgd_softc *cs, int num)
180 {
181 char buf[DK_XNAME_SIZE];
182
183 memset(cs, 0x0, sizeof(*cs));
184 snprintf(buf, DK_XNAME_SIZE, "cgd%d", num);
185 dk_sc_init(&cs->sc_dksc, cs, buf);
186 }
187
188 void
189 cgdattach(int num)
190 {
191 struct cgd_softc *cs;
192 int i;
193
194 DPRINTF_FOLLOW(("cgdattach(%d)\n", num));
195 if (num <= 0) {
196 DIAGPANIC(("cgdattach: count <= 0"));
197 return;
198 }
199
200 cgd_softc = (void *)malloc(num * sizeof(*cs), M_DEVBUF, M_NOWAIT);
201 if (!cs) {
202 printf("WARNING: unable to malloc(9) memory for crypt disks\n");
203 DIAGPANIC(("cgdattach: cannot malloc(9) enough memory"));
204 return;
205 }
206
207 numcgd = num;
208 for (i=0; i<num; i++)
209 cgdsoftc_init(&cgd_softc[i], i);
210
211 /* Init component buffer pool. XXX, can we put this in dksubr.c? */
212 pool_init(&cgd_cbufpool, sizeof(struct cgdbuf), 0, 0, 0,
213 "cgdpl", NULL);
214 }
215
216 int
217 cgdopen(dev_t dev, int flags, int fmt, struct proc *p)
218 {
219 struct cgd_softc *cs;
220
221 DPRINTF_FOLLOW(("cgdopen(%d, %d)\n", dev, flags));
222 GETCGD_SOFTC(cs, dev);
223 return dk_open(di, &cs->sc_dksc, dev, flags, fmt, p);
224 }
225
226 int
227 cgdclose(dev_t dev, int flags, int fmt, struct proc *p)
228 {
229 struct cgd_softc *cs;
230
231 DPRINTF_FOLLOW(("cgdclose(%d, %d)\n", dev, flags));
232 GETCGD_SOFTC(cs, dev);
233 return dk_close(di, &cs->sc_dksc, dev, flags, fmt, p);
234 }
235
236 void
237 cgdstrategy(struct buf *bp)
238 {
239 struct cgd_softc *cs = getcgd_softc(bp->b_dev);
240
241 DPRINTF_FOLLOW(("cgdstrategy(%p): b_bcount = %ld\n", bp,
242 (long)bp->b_bcount));
243 /* XXXrcd: Should we test for (cs != NULL)? */
244 dk_strategy(di, &cs->sc_dksc, bp);
245 return;
246 }
247
248 int
249 cgdsize(dev_t dev)
250 {
251 struct cgd_softc *cs = getcgd_softc(dev);
252
253 DPRINTF_FOLLOW(("cgdsize(%d)\n", dev));
254 if (!cs)
255 return -1;
256 return dk_size(di, &cs->sc_dksc, dev);
257 }
258
259 static void
260 cgdstart(struct dk_softc *dksc, struct buf *bp)
261 {
262 struct cgd_softc *cs = dksc->sc_osc;
263 struct cgdbuf *cbp;
264 struct partition *pp;
265 caddr_t addr;
266 caddr_t newaddr;
267 daddr_t bn;
268
269 DPRINTF_FOLLOW(("cgdstart(%p, %p)\n", dksc, bp));
270 disk_busy(&dksc->sc_dkdev); /* XXX: put in dksubr.c */
271
272 /* XXXrcd:
273 * Translate partition relative blocks to absolute blocks,
274 * this probably belongs (somehow) in dksubr.c, since it
275 * is independant of the underlying code... This will require
276 * that the interface be expanded slightly, though.
277 */
278 bn = bp->b_blkno;
279 if (DISKPART(bp->b_dev) != RAW_PART) {
280 pp = &cs->sc_dksc.sc_dkdev.dk_label->d_partitions[DISKPART(bp->b_dev)];
281 bn += pp->p_offset;
282 }
283
284 /*
285 * If we are writing, then we need to encrypt the outgoing
286 * block. In the best case scenario, we are able to allocate
287 * enough memory to encrypt the data in a new block, otherwise
288 * we encrypt it in place (noting we'll have to decrypt it after
289 * the write.)
290 */
291 newaddr = addr = bp->b_data;
292 if ((bp->b_flags & B_READ) == 0) {
293 newaddr = malloc(bp->b_bcount, M_DEVBUF, 0);
294 if (!newaddr)
295 newaddr = addr;
296 cgd_cipher(cs, newaddr, addr, bp->b_bcount, bn,
297 DEV_BSIZE, CGD_CIPHER_ENCRYPT);
298 }
299
300 cbp = CGD_GETBUF();
301 if (cbp == NULL) {
302 bp->b_error = ENOMEM;
303 bp->b_flags |= B_ERROR;
304 if (newaddr != addr)
305 free(newaddr, M_DEVBUF);
306 biodone(bp);
307 disk_unbusy(&dksc->sc_dkdev, 0);
308 return;
309 }
310 cbp->cb_buf.b_data = newaddr;
311 cbp->cb_buf.b_flags = bp->b_flags | B_CALL;
312 cbp->cb_buf.b_iodone = cgdiodone;
313 cbp->cb_buf.b_proc = bp->b_proc;
314 cbp->cb_buf.b_dev = cs->sc_tdev;
315 cbp->cb_buf.b_blkno = bn;
316 cbp->cb_buf.b_vp = cs->sc_tvn;
317 LIST_INIT(&cbp->cb_buf.b_dep);
318 cbp->cb_buf.b_bcount = bp->b_bcount;
319
320 /* context for cgdiodone */
321 cbp->cb_obp = bp;
322 cbp->cb_sc = cs;
323
324 if ((cbp->cb_buf.b_flags & B_READ) == 0)
325 cbp->cb_buf.b_vp->v_numoutput++;
326 VOP_STRATEGY(&cbp->cb_buf);
327 }
328
329 void
330 cgdiodone(struct buf *vbp)
331 {
332 struct cgdbuf *cbp = (struct cgdbuf *)vbp;
333 struct buf *obp = cbp->cb_obp;
334 struct buf *nbp = &cbp->cb_buf;
335 struct cgd_softc *cs = cbp->cb_sc;
336 struct dk_softc *dksc = &cs->sc_dksc;
337 int s;
338
339 DPRINTF_FOLLOW(("cgdiodone(%p)\n", vbp));
340 DPRINTF(CGDB_IO, ("cgdiodone: bp %p bcount %ld resid %ld\n",
341 obp, obp->b_bcount, obp->b_resid));
342 DPRINTF(CGDB_IO, (" dev 0x%x, cbp %p bn %d addr %p bcnt %ld\n",
343 cbp->cb_buf.b_dev, cbp, cbp->cb_buf.b_blkno, cbp->cb_buf.b_data,
344 cbp->cb_buf.b_bcount));
345 s = splbio();
346 if (nbp->b_flags & B_ERROR) {
347 obp->b_flags |= B_ERROR;
348 obp->b_error = nbp->b_error ? nbp->b_error : EIO;
349
350 printf("%s: error %d\n", dksc->sc_xname, obp->b_error);
351 }
352
353 /* Perform the decryption if we need to:
354 * o if we are reading, or
355 * o we wrote and couldn't allocate memory.
356 *
357 * Note: use the blocknumber from nbp, since it is what
358 * we used to encrypt the blocks.
359 */
360
361 if (nbp->b_flags & B_READ || nbp->b_data == obp->b_data)
362 cgd_cipher(cs, obp->b_data, obp->b_data, obp->b_bcount,
363 nbp->b_blkno, DEV_BSIZE, CGD_CIPHER_DECRYPT);
364
365 /* If we managed to allocate memory, free it now... */
366 if (nbp->b_data != obp->b_data)
367 free(nbp->b_data, M_DEVBUF);
368
369 CGD_PUTBUF(cbp);
370
371 /* Request is complete for whatever reason */
372 obp->b_resid = 0;
373 if (obp->b_flags & B_ERROR)
374 obp->b_resid = obp->b_bcount;
375 disk_unbusy(&dksc->sc_dkdev, obp->b_bcount - obp->b_resid);
376 biodone(obp);
377 splx(s);
378 }
379
380 /* XXX: we should probably put these into dksubr.c, mostly */
381 int
382 cgdread(dev_t dev, struct uio *uio, int flags)
383 {
384 struct cgd_softc *cs;
385 struct dk_softc *dksc;
386
387 DPRINTF_FOLLOW(("cgdread(%d, %p, %d)\n", dev, uio, flags));
388 GETCGD_SOFTC(cs, dev);
389 dksc = &cs->sc_dksc;
390 if ((dksc->sc_flags & DKF_INITED) == 0)
391 return ENXIO;
392 /* XXX see the comments about minphys in ccd.c */
393 return physio(cgdstrategy, NULL, dev, B_READ, minphys, uio);
394 }
395
396 /* XXX: we should probably put these into dksubr.c, mostly */
397 int
398 cgdwrite(dev_t dev, struct uio *uio, int flags)
399 {
400 struct cgd_softc *cs;
401 struct dk_softc *dksc;
402
403 DPRINTF_FOLLOW(("cgdwrite(%d, %p, %d)\n", dev, uio, flags));
404 GETCGD_SOFTC(cs, dev);
405 dksc = &cs->sc_dksc;
406 if ((dksc->sc_flags & DKF_INITED) == 0)
407 return ENXIO;
408 /* XXX see the comments about minphys in ccd.c */
409 return physio(cgdstrategy, NULL, dev, B_WRITE, minphys, uio);
410 }
411
412 int
413 cgdioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
414 {
415 struct cgd_softc *cs;
416 struct dk_softc *dksc;
417 int ret;
418 int part = DISKPART(dev);
419 int pmask = 1 << part;
420
421 DPRINTF_FOLLOW(("cgdioctl(%d, %ld, %p, %d, %p)\n",
422 dev, cmd, data, flag, p));
423 GETCGD_SOFTC(cs, dev);
424 dksc = &cs->sc_dksc;
425 switch (cmd) {
426 case CGDIOCSET:
427 case CGDIOCCLR:
428 if ((flag & FWRITE) == 0)
429 return EBADF;
430 }
431
432 if ((ret = lockmgr(&dksc->sc_lock, LK_EXCLUSIVE, NULL)) != 0)
433 return ret;
434
435 switch (cmd) {
436 case CGDIOCSET:
437 if (dksc->sc_flags & DKF_INITED)
438 ret = EBUSY;
439 else
440 ret = cgd_ioctl_set(cs, data, p);
441 break;
442 case CGDIOCCLR:
443 if (!(dksc->sc_flags & DKF_INITED)) {
444 ret = ENXIO;
445 break;
446 }
447 if (DK_BUSY(&cs->sc_dksc, pmask)) {
448 ret = EBUSY;
449 break;
450 }
451 ret = cgd_ioctl_clr(cs, data, p);
452 break;
453 default:
454 ret = dk_ioctl(di, dksc, dev, cmd, data, flag, p);
455 break;
456 }
457
458 lockmgr(&dksc->sc_lock, LK_RELEASE, NULL);
459 return ret;
460 }
461
462 int
463 cgddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
464 {
465 struct cgd_softc *cs;
466
467 DPRINTF_FOLLOW(("cgddump(%d, %d, %p, %d)\n", dev, blkno, va, size));
468 GETCGD_SOFTC(cs, dev);
469 return dk_dump(di, &cs->sc_dksc, dev, blkno, va, size);
470 }
471
472 /*
473 * XXXrcd:
474 * for now we hardcode the maximum key length.
475 */
476 #define MAX_KEYSIZE 1024
477
478 /* ARGSUSED */
479 static int
480 cgd_ioctl_set(struct cgd_softc *cs, void *data, struct proc *p)
481 {
482 struct cgd_ioctl *ci = data;
483 struct vnode *vp;
484 int ret;
485 char *cp;
486 char inbuf[MAX_KEYSIZE];
487
488 cp = ci->ci_disk;
489 if ((ret = dk_lookup(cp, p, &vp)) != 0)
490 return ret;
491
492 if ((ret = cgdinit(cs, cp, vp, p)) != 0)
493 goto bail;
494
495 memset(inbuf, 0x0, sizeof(inbuf));
496 ret = copyinstr(ci->ci_alg, inbuf, 256, NULL);
497 if (ret)
498 goto bail;
499 cs->sc_cfuncs = cryptfuncs_find(inbuf);
500 if (!cs->sc_cfuncs) {
501 ret = EINVAL;
502 goto bail;
503 }
504
505 /* right now we only support encblkno, so hard-code it */
506 memset(inbuf, 0x0, sizeof(inbuf));
507 ret = copyinstr(ci->ci_ivmethod, inbuf, sizeof(inbuf), NULL);
508 if (ret)
509 goto bail;
510 if (strcmp("encblkno", inbuf)) {
511 ret = EINVAL;
512 goto bail;
513 }
514
515 if (ci->ci_keylen > MAX_KEYSIZE) {
516 ret = EINVAL;
517 goto bail;
518 }
519 memset(inbuf, 0x0, sizeof(inbuf));
520 ret = copyin(ci->ci_key, inbuf, ci->ci_keylen);
521 if (ret)
522 goto bail;
523
524 cs->sc_cdata.cf_blocksize = ci->ci_blocksize;
525 cs->sc_cdata.cf_mode = CGD_CIPHER_CBC_ENCBLKNO;
526 cs->sc_cdata.cf_priv = cs->sc_cfuncs->cf_init(ci->ci_keylen, inbuf,
527 &cs->sc_cdata.cf_blocksize);
528 memset(inbuf, 0x0, sizeof(inbuf));
529 if (!cs->sc_cdata.cf_priv) {
530 printf("cgd: unable to initialize cipher\n");
531 ret = EINVAL; /* XXX is this the right error? */
532 goto bail;
533 }
534
535 cs->sc_dksc.sc_flags |= DKF_INITED;
536
537 /* Attach the disk. */
538 disk_attach(&cs->sc_dksc.sc_dkdev);
539
540 /* Try and read the disklabel. */
541 dk_getdisklabel(di, &cs->sc_dksc, 0 /* XXX ? */);
542
543 return 0;
544
545 bail:
546 (void)vn_close(vp, FREAD|FWRITE, p->p_ucred, p);
547 return ret;
548 }
549
550 /* ARGSUSED */
551 static int
552 cgd_ioctl_clr(struct cgd_softc *cs, void *data, struct proc *p)
553 {
554
555 (void)vn_close(cs->sc_tvn, FREAD|FWRITE, p->p_ucred, p);
556 cs->sc_cfuncs->cf_destroy(cs->sc_cdata.cf_priv);
557 free(cs->sc_tpath, M_DEVBUF);
558 cs->sc_dksc.sc_flags &= ~DKF_INITED;
559 disk_detach(&cs->sc_dksc.sc_dkdev);
560
561 return 0;
562 }
563
564 static int
565 cgdinit(struct cgd_softc *cs, char *cpath, struct vnode *vp,
566 struct proc *p)
567 {
568 struct dk_geom *pdg;
569 struct partinfo dpart;
570 struct vattr va;
571 size_t size;
572 int maxsecsize = 0;
573 int ret;
574 char tmppath[MAXPATHLEN];
575
576 cs->sc_dksc.sc_size = 0;
577 cs->sc_tvn = vp;
578
579 memset(tmppath, 0x0, sizeof(tmppath));
580 ret = copyinstr(cpath, tmppath, MAXPATHLEN, &cs->sc_tpathlen);
581 if (ret)
582 goto bail;
583 cs->sc_tpath = malloc(cs->sc_tpathlen, M_DEVBUF, M_WAITOK);
584 memcpy(cs->sc_tpath, tmppath, cs->sc_tpathlen);
585
586 if ((ret = VOP_GETATTR(vp, &va, p->p_ucred, p)) != 0)
587 goto bail;
588
589 cs->sc_tdev = va.va_rdev;
590
591 ret = VOP_IOCTL(vp, DIOCGPART, (caddr_t)&dpart, FREAD, p->p_ucred, p);
592 if (ret)
593 goto bail;
594
595 maxsecsize =
596 ((dpart.disklab->d_secsize > maxsecsize) ?
597 dpart.disklab->d_secsize : maxsecsize);
598 size = dpart.part->p_size;
599
600 if (!size) {
601 ret = ENODEV;
602 goto bail;
603 }
604
605 cs->sc_dksc.sc_size = size;
606
607 /*
608 * XXX here we should probe the underlying device. If we
609 * are accessing a partition of type RAW_PART, then
610 * we should populate our initial geometry with the
611 * geometry that we discover from the device.
612 */
613 pdg = &cs->sc_dksc.sc_geom;
614 pdg->pdg_secsize = DEV_BSIZE;
615 pdg->pdg_ntracks = 1;
616 pdg->pdg_nsectors = 1024 * (1024 / pdg->pdg_secsize);
617 pdg->pdg_ncylinders = cs->sc_dksc.sc_size / pdg->pdg_nsectors;
618
619 bail:
620 if (ret && cs->sc_tpath)
621 free(cs->sc_tpath, M_DEVBUF);
622 return ret;
623 }
624
625 /*
626 * Our generic cipher entry point. This takes care of the
627 * IV mode and passes off the work to the specific cipher.
628 * We implement here the IV method ``encrypted block
629 * number''.
630 *
631 * For the encryption case, we accomplish this by setting
632 * up a struct uio where the first iovec of the source is
633 * the blocknumber and the first iovec of the dest is a
634 * sink. We then call the cipher with an IV of zero, and
635 * the right thing happens.
636 *
637 * For the decryption case, we use the same basic mechanism
638 * for symmetry, but we encrypt the block number in the
639 * first iovec.
640 *
641 * We mainly do this to avoid requiring the definition of
642 * an ECB mode.
643 *
644 * XXXrcd: for now we rely on our own crypto framework defined
645 * in dev/cgd_crypto.c. This will change when we
646 * get a generic kernel crypto framework.
647 */
648
649 static void
650 blkno2blkno_buf(char *buf, daddr_t blkno)
651 {
652 int i;
653
654 /* Set up the blkno in blkno_buf, here we do not care much
655 * about the final layout of the information as long as we
656 * can guarantee that each sector will have a different IV
657 * and that the endianness of the machine will not affect
658 * the representation that we have chosen.
659 *
660 * We choose this representation, because it does not rely
661 * on the size of buf (which is the blocksize of the cipher),
662 * but allows daddr_t to grow without breaking existing
663 * disks.
664 *
665 * Note that blkno2blkno_buf does not take a size as input,
666 * and hence must be called on a pre-zeroed buffer of length
667 * greater than or equal to sizeof(daddr_t).
668 */
669 for (i=0; i < sizeof(daddr_t); i++) {
670 *buf++ = blkno & 0xff;
671 blkno >>= 8;
672 }
673 }
674
675 static void
676 cgd_cipher(struct cgd_softc *cs, caddr_t dst, caddr_t src,
677 size_t len, daddr_t blkno, size_t secsize, int dir)
678 {
679 cfunc_cipher *cipher = cs->sc_cfuncs->cf_cipher;
680 struct uio dstuio;
681 struct uio srcuio;
682 struct iovec dstiov[2];
683 struct iovec srciov[2];
684 int blocksize = cs->sc_cdata.cf_blocksize;
685 char sink[blocksize];
686 char zero_iv[blocksize];
687 char blkno_buf[blocksize];
688
689 DPRINTF_FOLLOW(("cgd_cipher() dir=%d\n", dir));
690
691 DIAGCONDPANIC(len % blocksize != 0,
692 ("cgd_cipher: len %% blocksize != 0"));
693
694 /* ensure that sizeof(daddr_t) <= blocksize (for encblkno IVing) */
695 DIAGCONDPANIC(sizeof(daddr_t) > blocksize,
696 ("cgd_cipher: sizeof(daddr_t) > blocksize"));
697
698 memset(zero_iv, 0x0, sizeof(zero_iv));
699
700 dstuio.uio_iov = dstiov;
701 dstuio.uio_iovcnt = 2;
702
703 srcuio.uio_iov = srciov;
704 srcuio.uio_iovcnt = 2;
705
706 dstiov[0].iov_base = sink;
707 dstiov[0].iov_len = blocksize;
708 srciov[0].iov_base = blkno_buf;
709 srciov[0].iov_len = blocksize;
710 dstiov[1].iov_len = secsize;
711 srciov[1].iov_len = secsize;
712
713 for (; len > 0; len -= secsize) {
714 dstiov[1].iov_base = dst;
715 srciov[1].iov_base = src;
716
717 memset(blkno_buf, 0x0, sizeof(blkno_buf));
718 blkno2blkno_buf(blkno_buf, blkno);
719 if (dir == CGD_CIPHER_DECRYPT) {
720 dstuio.uio_iovcnt = 1;
721 srcuio.uio_iovcnt = 1;
722 IFDEBUG(CGDB_CRYPTO, hexprint("step 0: blkno_buf",
723 blkno_buf, sizeof(blkno_buf)));
724 cipher(cs->sc_cdata.cf_priv, &dstuio, &srcuio,
725 zero_iv, CGD_CIPHER_ENCRYPT);
726 memcpy(blkno_buf, sink, blocksize);
727 dstuio.uio_iovcnt = 2;
728 srcuio.uio_iovcnt = 2;
729 }
730
731 IFDEBUG(CGDB_CRYPTO, hexprint("step 1: blkno_buf",
732 blkno_buf, sizeof(blkno_buf)));
733 cipher(cs->sc_cdata.cf_priv, &dstuio, &srcuio, zero_iv, dir);
734 IFDEBUG(CGDB_CRYPTO, hexprint("step 2: sink",
735 sink, sizeof(sink)));
736
737 dst += secsize;
738 src += secsize;
739 blkno++;
740 }
741 }
742
743 #ifdef DEBUG
744 static void
745 hexprint(char *start, void *buf, int len)
746 {
747 char *c = buf;
748
749 DIAGCONDPANIC(len < 0, ("hexprint: called with len < 0"));
750 printf("%s: len=%06d 0x", start, len);
751 while (len--)
752 printf("%02x", (unsigned) *c++);
753 }
754 #endif
755