cgd.c revision 1.7 1 /* $NetBSD: cgd.c,v 1.7 2003/02/05 21:38:40 pk 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.7 2003/02/05 21:38:40 pk 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, nokqfilter, 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, (bp->b_flags & B_READ));
308 return;
309 }
310 simple_lock_init(&cbp->cb_buf.b_interlock);
311 cbp->cb_buf.b_data = newaddr;
312 cbp->cb_buf.b_flags = bp->b_flags | B_CALL;
313 cbp->cb_buf.b_iodone = cgdiodone;
314 cbp->cb_buf.b_proc = bp->b_proc;
315 cbp->cb_buf.b_dev = cs->sc_tdev;
316 cbp->cb_buf.b_blkno = bn;
317 cbp->cb_buf.b_vp = cs->sc_tvn;
318 LIST_INIT(&cbp->cb_buf.b_dep);
319 cbp->cb_buf.b_bcount = bp->b_bcount;
320
321 /* context for cgdiodone */
322 cbp->cb_obp = bp;
323 cbp->cb_sc = cs;
324
325 if ((cbp->cb_buf.b_flags & B_READ) == 0)
326 cbp->cb_buf.b_vp->v_numoutput++;
327 VOP_STRATEGY(&cbp->cb_buf);
328 }
329
330 void
331 cgdiodone(struct buf *vbp)
332 {
333 struct cgdbuf *cbp = (struct cgdbuf *)vbp;
334 struct buf *obp = cbp->cb_obp;
335 struct buf *nbp = &cbp->cb_buf;
336 struct cgd_softc *cs = cbp->cb_sc;
337 struct dk_softc *dksc = &cs->sc_dksc;
338 int s;
339
340 DPRINTF_FOLLOW(("cgdiodone(%p)\n", vbp));
341 DPRINTF(CGDB_IO, ("cgdiodone: bp %p bcount %ld resid %ld\n",
342 obp, obp->b_bcount, obp->b_resid));
343 DPRINTF(CGDB_IO, (" dev 0x%x, cbp %p bn %" PRId64 " addr %p bcnt %ld\n",
344 cbp->cb_buf.b_dev, cbp, cbp->cb_buf.b_blkno, cbp->cb_buf.b_data,
345 cbp->cb_buf.b_bcount));
346 s = splbio();
347 if (nbp->b_flags & B_ERROR) {
348 obp->b_flags |= B_ERROR;
349 obp->b_error = nbp->b_error ? nbp->b_error : EIO;
350
351 printf("%s: error %d\n", dksc->sc_xname, obp->b_error);
352 }
353
354 /* Perform the decryption if we need to:
355 * o if we are reading, or
356 * o we wrote and couldn't allocate memory.
357 *
358 * Note: use the blocknumber from nbp, since it is what
359 * we used to encrypt the blocks.
360 */
361
362 if (nbp->b_flags & B_READ || nbp->b_data == obp->b_data)
363 cgd_cipher(cs, obp->b_data, obp->b_data, obp->b_bcount,
364 nbp->b_blkno, DEV_BSIZE, CGD_CIPHER_DECRYPT);
365
366 /* If we managed to allocate memory, free it now... */
367 if (nbp->b_data != obp->b_data)
368 free(nbp->b_data, M_DEVBUF);
369
370 CGD_PUTBUF(cbp);
371
372 /* Request is complete for whatever reason */
373 obp->b_resid = 0;
374 if (obp->b_flags & B_ERROR)
375 obp->b_resid = obp->b_bcount;
376 disk_unbusy(&dksc->sc_dkdev, obp->b_bcount - obp->b_resid,
377 (obp->b_flags & B_READ));
378 biodone(obp);
379 splx(s);
380 }
381
382 /* XXX: we should probably put these into dksubr.c, mostly */
383 int
384 cgdread(dev_t dev, struct uio *uio, int flags)
385 {
386 struct cgd_softc *cs;
387 struct dk_softc *dksc;
388
389 DPRINTF_FOLLOW(("cgdread(%d, %p, %d)\n", dev, uio, flags));
390 GETCGD_SOFTC(cs, dev);
391 dksc = &cs->sc_dksc;
392 if ((dksc->sc_flags & DKF_INITED) == 0)
393 return ENXIO;
394 /* XXX see the comments about minphys in ccd.c */
395 return physio(cgdstrategy, NULL, dev, B_READ, minphys, uio);
396 }
397
398 /* XXX: we should probably put these into dksubr.c, mostly */
399 int
400 cgdwrite(dev_t dev, struct uio *uio, int flags)
401 {
402 struct cgd_softc *cs;
403 struct dk_softc *dksc;
404
405 DPRINTF_FOLLOW(("cgdwrite(%d, %p, %d)\n", dev, uio, flags));
406 GETCGD_SOFTC(cs, dev);
407 dksc = &cs->sc_dksc;
408 if ((dksc->sc_flags & DKF_INITED) == 0)
409 return ENXIO;
410 /* XXX see the comments about minphys in ccd.c */
411 return physio(cgdstrategy, NULL, dev, B_WRITE, minphys, uio);
412 }
413
414 int
415 cgdioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
416 {
417 struct cgd_softc *cs;
418 struct dk_softc *dksc;
419 int ret;
420 int part = DISKPART(dev);
421 int pmask = 1 << part;
422
423 DPRINTF_FOLLOW(("cgdioctl(%d, %ld, %p, %d, %p)\n",
424 dev, cmd, data, flag, p));
425 GETCGD_SOFTC(cs, dev);
426 dksc = &cs->sc_dksc;
427 switch (cmd) {
428 case CGDIOCSET:
429 case CGDIOCCLR:
430 if ((flag & FWRITE) == 0)
431 return EBADF;
432 }
433
434 if ((ret = lockmgr(&dksc->sc_lock, LK_EXCLUSIVE, NULL)) != 0)
435 return ret;
436
437 switch (cmd) {
438 case CGDIOCSET:
439 if (dksc->sc_flags & DKF_INITED)
440 ret = EBUSY;
441 else
442 ret = cgd_ioctl_set(cs, data, p);
443 break;
444 case CGDIOCCLR:
445 if (!(dksc->sc_flags & DKF_INITED)) {
446 ret = ENXIO;
447 break;
448 }
449 if (DK_BUSY(&cs->sc_dksc, pmask)) {
450 ret = EBUSY;
451 break;
452 }
453 ret = cgd_ioctl_clr(cs, data, p);
454 break;
455 default:
456 ret = dk_ioctl(di, dksc, dev, cmd, data, flag, p);
457 break;
458 }
459
460 lockmgr(&dksc->sc_lock, LK_RELEASE, NULL);
461 return ret;
462 }
463
464 int
465 cgddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
466 {
467 struct cgd_softc *cs;
468
469 DPRINTF_FOLLOW(("cgddump(%d, %" PRId64 ", %p, %lu)\n", dev, blkno, va,
470 (unsigned long)size));
471 GETCGD_SOFTC(cs, dev);
472 return dk_dump(di, &cs->sc_dksc, dev, blkno, va, size);
473 }
474
475 /*
476 * XXXrcd:
477 * for now we hardcode the maximum key length.
478 */
479 #define MAX_KEYSIZE 1024
480
481 /* ARGSUSED */
482 static int
483 cgd_ioctl_set(struct cgd_softc *cs, void *data, struct proc *p)
484 {
485 struct cgd_ioctl *ci = data;
486 struct vnode *vp;
487 int ret;
488 char *cp;
489 char inbuf[MAX_KEYSIZE];
490
491 cp = ci->ci_disk;
492 if ((ret = dk_lookup(cp, p, &vp)) != 0)
493 return ret;
494
495 if ((ret = cgdinit(cs, cp, vp, p)) != 0)
496 goto bail;
497
498 memset(inbuf, 0x0, sizeof(inbuf));
499 ret = copyinstr(ci->ci_alg, inbuf, 256, NULL);
500 if (ret)
501 goto bail;
502 cs->sc_cfuncs = cryptfuncs_find(inbuf);
503 if (!cs->sc_cfuncs) {
504 ret = EINVAL;
505 goto bail;
506 }
507
508 /* right now we only support encblkno, so hard-code it */
509 memset(inbuf, 0x0, sizeof(inbuf));
510 ret = copyinstr(ci->ci_ivmethod, inbuf, sizeof(inbuf), NULL);
511 if (ret)
512 goto bail;
513 if (strcmp("encblkno", inbuf)) {
514 ret = EINVAL;
515 goto bail;
516 }
517
518 if (ci->ci_keylen > MAX_KEYSIZE) {
519 ret = EINVAL;
520 goto bail;
521 }
522 memset(inbuf, 0x0, sizeof(inbuf));
523 ret = copyin(ci->ci_key, inbuf, ci->ci_keylen);
524 if (ret)
525 goto bail;
526
527 cs->sc_cdata.cf_blocksize = ci->ci_blocksize;
528 cs->sc_cdata.cf_mode = CGD_CIPHER_CBC_ENCBLKNO;
529 cs->sc_cdata.cf_priv = cs->sc_cfuncs->cf_init(ci->ci_keylen, inbuf,
530 &cs->sc_cdata.cf_blocksize);
531 memset(inbuf, 0x0, sizeof(inbuf));
532 if (!cs->sc_cdata.cf_priv) {
533 printf("cgd: unable to initialize cipher\n");
534 ret = EINVAL; /* XXX is this the right error? */
535 goto bail;
536 }
537
538 cs->sc_dksc.sc_flags |= DKF_INITED;
539
540 /* Attach the disk. */
541 disk_attach(&cs->sc_dksc.sc_dkdev);
542
543 /* Try and read the disklabel. */
544 dk_getdisklabel(di, &cs->sc_dksc, 0 /* XXX ? */);
545
546 return 0;
547
548 bail:
549 (void)vn_close(vp, FREAD|FWRITE, p->p_ucred, p);
550 return ret;
551 }
552
553 /* ARGSUSED */
554 static int
555 cgd_ioctl_clr(struct cgd_softc *cs, void *data, struct proc *p)
556 {
557
558 (void)vn_close(cs->sc_tvn, FREAD|FWRITE, p->p_ucred, p);
559 cs->sc_cfuncs->cf_destroy(cs->sc_cdata.cf_priv);
560 free(cs->sc_tpath, M_DEVBUF);
561 cs->sc_dksc.sc_flags &= ~DKF_INITED;
562 disk_detach(&cs->sc_dksc.sc_dkdev);
563
564 return 0;
565 }
566
567 static int
568 cgdinit(struct cgd_softc *cs, char *cpath, struct vnode *vp,
569 struct proc *p)
570 {
571 struct dk_geom *pdg;
572 struct partinfo dpart;
573 struct vattr va;
574 size_t size;
575 int maxsecsize = 0;
576 int ret;
577 char tmppath[MAXPATHLEN];
578
579 cs->sc_dksc.sc_size = 0;
580 cs->sc_tvn = vp;
581
582 memset(tmppath, 0x0, sizeof(tmppath));
583 ret = copyinstr(cpath, tmppath, MAXPATHLEN, &cs->sc_tpathlen);
584 if (ret)
585 goto bail;
586 cs->sc_tpath = malloc(cs->sc_tpathlen, M_DEVBUF, M_WAITOK);
587 memcpy(cs->sc_tpath, tmppath, cs->sc_tpathlen);
588
589 if ((ret = VOP_GETATTR(vp, &va, p->p_ucred, p)) != 0)
590 goto bail;
591
592 cs->sc_tdev = va.va_rdev;
593
594 ret = VOP_IOCTL(vp, DIOCGPART, (caddr_t)&dpart, FREAD, p->p_ucred, p);
595 if (ret)
596 goto bail;
597
598 maxsecsize =
599 ((dpart.disklab->d_secsize > maxsecsize) ?
600 dpart.disklab->d_secsize : maxsecsize);
601 size = dpart.part->p_size;
602
603 if (!size) {
604 ret = ENODEV;
605 goto bail;
606 }
607
608 cs->sc_dksc.sc_size = size;
609
610 /*
611 * XXX here we should probe the underlying device. If we
612 * are accessing a partition of type RAW_PART, then
613 * we should populate our initial geometry with the
614 * geometry that we discover from the device.
615 */
616 pdg = &cs->sc_dksc.sc_geom;
617 pdg->pdg_secsize = DEV_BSIZE;
618 pdg->pdg_ntracks = 1;
619 pdg->pdg_nsectors = 1024 * (1024 / pdg->pdg_secsize);
620 pdg->pdg_ncylinders = cs->sc_dksc.sc_size / pdg->pdg_nsectors;
621
622 bail:
623 if (ret && cs->sc_tpath)
624 free(cs->sc_tpath, M_DEVBUF);
625 return ret;
626 }
627
628 /*
629 * Our generic cipher entry point. This takes care of the
630 * IV mode and passes off the work to the specific cipher.
631 * We implement here the IV method ``encrypted block
632 * number''.
633 *
634 * For the encryption case, we accomplish this by setting
635 * up a struct uio where the first iovec of the source is
636 * the blocknumber and the first iovec of the dest is a
637 * sink. We then call the cipher with an IV of zero, and
638 * the right thing happens.
639 *
640 * For the decryption case, we use the same basic mechanism
641 * for symmetry, but we encrypt the block number in the
642 * first iovec.
643 *
644 * We mainly do this to avoid requiring the definition of
645 * an ECB mode.
646 *
647 * XXXrcd: for now we rely on our own crypto framework defined
648 * in dev/cgd_crypto.c. This will change when we
649 * get a generic kernel crypto framework.
650 */
651
652 static void
653 blkno2blkno_buf(char *buf, daddr_t blkno)
654 {
655 int i;
656
657 /* Set up the blkno in blkno_buf, here we do not care much
658 * about the final layout of the information as long as we
659 * can guarantee that each sector will have a different IV
660 * and that the endianness of the machine will not affect
661 * the representation that we have chosen.
662 *
663 * We choose this representation, because it does not rely
664 * on the size of buf (which is the blocksize of the cipher),
665 * but allows daddr_t to grow without breaking existing
666 * disks.
667 *
668 * Note that blkno2blkno_buf does not take a size as input,
669 * and hence must be called on a pre-zeroed buffer of length
670 * greater than or equal to sizeof(daddr_t).
671 */
672 for (i=0; i < sizeof(daddr_t); i++) {
673 *buf++ = blkno & 0xff;
674 blkno >>= 8;
675 }
676 }
677
678 static void
679 cgd_cipher(struct cgd_softc *cs, caddr_t dst, caddr_t src,
680 size_t len, daddr_t blkno, size_t secsize, int dir)
681 {
682 cfunc_cipher *cipher = cs->sc_cfuncs->cf_cipher;
683 struct uio dstuio;
684 struct uio srcuio;
685 struct iovec dstiov[2];
686 struct iovec srciov[2];
687 int blocksize = cs->sc_cdata.cf_blocksize;
688 char sink[blocksize];
689 char zero_iv[blocksize];
690 char blkno_buf[blocksize];
691
692 DPRINTF_FOLLOW(("cgd_cipher() dir=%d\n", dir));
693
694 DIAGCONDPANIC(len % blocksize != 0,
695 ("cgd_cipher: len %% blocksize != 0"));
696
697 /* ensure that sizeof(daddr_t) <= blocksize (for encblkno IVing) */
698 DIAGCONDPANIC(sizeof(daddr_t) > blocksize,
699 ("cgd_cipher: sizeof(daddr_t) > blocksize"));
700
701 memset(zero_iv, 0x0, sizeof(zero_iv));
702
703 dstuio.uio_iov = dstiov;
704 dstuio.uio_iovcnt = 2;
705
706 srcuio.uio_iov = srciov;
707 srcuio.uio_iovcnt = 2;
708
709 dstiov[0].iov_base = sink;
710 dstiov[0].iov_len = blocksize;
711 srciov[0].iov_base = blkno_buf;
712 srciov[0].iov_len = blocksize;
713 dstiov[1].iov_len = secsize;
714 srciov[1].iov_len = secsize;
715
716 for (; len > 0; len -= secsize) {
717 dstiov[1].iov_base = dst;
718 srciov[1].iov_base = src;
719
720 memset(blkno_buf, 0x0, sizeof(blkno_buf));
721 blkno2blkno_buf(blkno_buf, blkno);
722 if (dir == CGD_CIPHER_DECRYPT) {
723 dstuio.uio_iovcnt = 1;
724 srcuio.uio_iovcnt = 1;
725 IFDEBUG(CGDB_CRYPTO, hexprint("step 0: blkno_buf",
726 blkno_buf, sizeof(blkno_buf)));
727 cipher(cs->sc_cdata.cf_priv, &dstuio, &srcuio,
728 zero_iv, CGD_CIPHER_ENCRYPT);
729 memcpy(blkno_buf, sink, blocksize);
730 dstuio.uio_iovcnt = 2;
731 srcuio.uio_iovcnt = 2;
732 }
733
734 IFDEBUG(CGDB_CRYPTO, hexprint("step 1: blkno_buf",
735 blkno_buf, sizeof(blkno_buf)));
736 cipher(cs->sc_cdata.cf_priv, &dstuio, &srcuio, zero_iv, dir);
737 IFDEBUG(CGDB_CRYPTO, hexprint("step 2: sink",
738 sink, sizeof(sink)));
739
740 dst += secsize;
741 src += secsize;
742 blkno++;
743 }
744 }
745
746 #ifdef DEBUG
747 static void
748 hexprint(char *start, void *buf, int len)
749 {
750 char *c = buf;
751
752 DIAGCONDPANIC(len < 0, ("hexprint: called with len < 0"));
753 printf("%s: len=%06d 0x", start, len);
754 while (len--)
755 printf("%02x", (unsigned) *c++);
756 }
757 #endif
758