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