Home | History | Annotate | Line # | Download | only in opencrypto
cryptodev.c revision 1.6
      1 /*	$NetBSD: cryptodev.c,v 1.6 2003/08/25 04:09:57 thorpej Exp $ */
      2 /*	$FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.4.2.4 2003/06/03 00:09:02 sam Exp $	*/
      3 /*	$OpenBSD: cryptodev.c,v 1.53 2002/07/10 22:21:30 mickey Exp $	*/
      4 
      5 /*
      6  * Copyright (c) 2001 Theo de Raadt
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  *
     12  * 1. Redistributions of source code must retain the above copyright
     13  *   notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *   notice, this list of conditions and the following disclaimer in the
     16  *   documentation and/or other materials provided with the distribution.
     17  * 3. The name of the author may not be used to endorse or promote products
     18  *   derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  *
     31  * Effort sponsored in part by the Defense Advanced Research Projects
     32  * Agency (DARPA) and Air Force Research Laboratory, Air Force
     33  * Materiel Command, USAF, under agreement number F30602-01-2-0537.
     34  *
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: cryptodev.c,v 1.6 2003/08/25 04:09:57 thorpej Exp $");
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/malloc.h>
     43 #include <sys/mbuf.h>
     44 #include <sys/sysctl.h>
     45 #include <sys/file.h>
     46 #include <sys/filedesc.h>
     47 #include <sys/errno.h>
     48 #include <sys/md5.h>
     49 #include <sys/sha1.h>
     50 #include <sys/conf.h>
     51 #include <sys/device.h>
     52 
     53 #include <crypto/ripemd160/rmd160.h>
     54 
     55 #include <opencrypto/cast.h>
     56 #include <opencrypto/skipjack.h>
     57 #include <opencrypto/blf.h>
     58 #include <opencrypto/cryptodev.h>
     59 #include <opencrypto/xform.h>
     60 
     61 #ifdef __NetBSD__
     62   #define splcrypto splnet
     63 #endif
     64 
     65 struct csession {
     66 	TAILQ_ENTRY(csession) next;
     67 	u_int64_t	sid;
     68 	u_int32_t	ses;
     69 
     70 	u_int32_t	cipher;
     71 	struct enc_xform *txform;
     72 	u_int32_t	mac;
     73 	struct auth_hash *thash;
     74 
     75 	caddr_t		key;
     76 	int		keylen;
     77 	u_char		tmp_iv[EALG_MAX_BLOCK_LEN];
     78 
     79 	caddr_t		mackey;
     80 	int		mackeylen;
     81 	u_char		tmp_mac[CRYPTO_MAX_MAC_LEN];
     82 
     83 	struct iovec	iovec[IOV_MAX];
     84 	struct uio	uio;
     85 	int		error;
     86 };
     87 
     88 struct fcrypt {
     89 	TAILQ_HEAD(csessionlist, csession) csessions;
     90 	int		sesn;
     91 };
     92 
     93 
     94 /* Declaration of master device (fd-cloning/ctxt-allocating) entrypoints */
     95 static int	cryptoopen(dev_t dev, int flag, int mode, struct proc *p);
     96 static int	cryptoread(dev_t dev, struct uio *uio, int ioflag);
     97 static int	cryptowrite(dev_t dev, struct uio *uio, int ioflag);
     98 static int	cryptoioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p);
     99 static int	cryptoselect(dev_t dev, int rw, struct proc *p);
    100 
    101 /* Declaration of cloned-device (per-ctxt) entrypoints */
    102 static int	cryptof_read(struct file *, off_t *, struct uio *, struct ucred *, int);
    103 static int	cryptof_write(struct file *, off_t *, struct uio *, struct ucred *, int);
    104 static int	cryptof_ioctl(struct file *, u_long, void*, struct proc *p);
    105 static int	cryptof_fcntl(struct file *, u_int, void*, struct proc *p);
    106 static int	cryptof_poll(struct file *, int, struct proc *);
    107 static int	cryptof_kqfilter(struct file *, struct knote *);
    108 static int	cryptof_stat(struct file *, struct stat *, struct proc *);
    109 static int	cryptof_close(struct file *, struct proc *);
    110 
    111 static struct fileops cryptofops = {
    112     cryptof_read,
    113     cryptof_write,
    114     cryptof_ioctl,
    115     cryptof_fcntl,
    116     cryptof_poll,
    117     cryptof_stat,
    118     cryptof_close,
    119     cryptof_kqfilter
    120 };
    121 
    122 static struct	csession *csefind(struct fcrypt *, u_int);
    123 static int	csedelete(struct fcrypt *, struct csession *);
    124 static struct	csession *cseadd(struct fcrypt *, struct csession *);
    125 static struct	csession *csecreate(struct fcrypt *, u_int64_t, caddr_t, u_int64_t,
    126     caddr_t, u_int64_t, u_int32_t, u_int32_t, struct enc_xform *,
    127     struct auth_hash *);
    128 static int	csefree(struct csession *);
    129 
    130 static int	cryptodev_op(struct csession *, struct crypt_op *, struct proc *);
    131 static int	cryptodev_key(struct crypt_kop *);
    132 int	cryptodev_dokey(struct crypt_kop *kop, struct crparam kvp[]);
    133 
    134 static int	cryptodev_cb(void *);
    135 static int	cryptodevkey_cb(void *);
    136 
    137 int	usercrypto = 1;		/* userland may do crypto requests */
    138 int	userasymcrypto = 1;	/* userland may do asymmetric crypto reqs */
    139 int	cryptodevallowsoft = 1;	/* only use hardware crypto */
    140 
    141 /* ARGSUSED */
    142 int
    143 cryptof_read(struct file *fp, off_t *poff, struct uio *uio,
    144 	     struct ucred *cred, int flags)
    145 {
    146 	return (EIO);
    147 }
    148 
    149 /* ARGSUSED */
    150 int
    151 cryptof_write(struct file *fp, off_t *poff, struct uio *uio,
    152 	      struct ucred *cred, int flags)
    153 {
    154 	return (EIO);
    155 }
    156 
    157 /* ARGSUSED */
    158 int
    159 cryptof_ioctl(struct file *fp, u_long cmd, void* data, struct proc *p)
    160 {
    161 	struct cryptoini cria, crie;
    162 	struct fcrypt *fcr = (struct fcrypt *)fp->f_data;
    163 	struct csession *cse;
    164 	struct session_op *sop;
    165 	struct crypt_op *cop;
    166 	struct enc_xform *txform = NULL;
    167 	struct auth_hash *thash = NULL;
    168 	u_int64_t sid;
    169 	u_int32_t ses;
    170 	int error = 0;
    171 
    172 	switch (cmd) {
    173 	case CIOCGSESSION:
    174 		sop = (struct session_op *)data;
    175 		switch (sop->cipher) {
    176 		case 0:
    177 			break;
    178 		case CRYPTO_DES_CBC:
    179 			txform = &enc_xform_des;
    180 			break;
    181 		case CRYPTO_3DES_CBC:
    182 			txform = &enc_xform_3des;
    183 			break;
    184 		case CRYPTO_BLF_CBC:
    185 			txform = &enc_xform_blf;
    186 			break;
    187 		case CRYPTO_CAST_CBC:
    188 			txform = &enc_xform_cast5;
    189 			break;
    190 		case CRYPTO_SKIPJACK_CBC:
    191 			txform = &enc_xform_skipjack;
    192 			break;
    193 		case CRYPTO_AES_CBC:
    194 			txform = &enc_xform_rijndael128;
    195 			break;
    196 		case CRYPTO_NULL_CBC:
    197 			txform = &enc_xform_null;
    198 			break;
    199 		case CRYPTO_ARC4:
    200 			txform = &enc_xform_arc4;
    201 			break;
    202 		default:
    203 			return (EINVAL);
    204 		}
    205 
    206 		switch (sop->mac) {
    207 		case 0:
    208 			break;
    209 		case CRYPTO_MD5_HMAC:
    210 			thash = &auth_hash_hmac_md5_96;
    211 			break;
    212 		case CRYPTO_SHA1_HMAC:
    213 			thash = &auth_hash_hmac_sha1_96;
    214 			break;
    215 		case CRYPTO_SHA2_HMAC:
    216 			if (sop->mackeylen == auth_hash_hmac_sha2_256.keysize)
    217 				thash = &auth_hash_hmac_sha2_256;
    218 			else if (sop->mackeylen == auth_hash_hmac_sha2_384.keysize)
    219 				thash = &auth_hash_hmac_sha2_384;
    220 			else if (sop->mackeylen == auth_hash_hmac_sha2_512.keysize)
    221 				thash = &auth_hash_hmac_sha2_512;
    222 			else
    223 				return (EINVAL);
    224 			break;
    225 		case CRYPTO_RIPEMD160_HMAC:
    226 			thash = &auth_hash_hmac_ripemd_160_96;
    227 			break;
    228 #ifdef notdef
    229 		case CRYPTO_MD5:
    230 			thash = &auth_hash_md5;
    231 			break;
    232 		case CRYPTO_SHA1:
    233 			thash = &auth_hash_sha1;
    234 			break;
    235 #endif
    236 		case CRYPTO_NULL_HMAC:
    237 			thash = &auth_hash_null;
    238 			break;
    239 		default:
    240 			return (EINVAL);
    241 		}
    242 
    243 		bzero(&crie, sizeof(crie));
    244 		bzero(&cria, sizeof(cria));
    245 
    246 		if (txform) {
    247 			crie.cri_alg = txform->type;
    248 			crie.cri_klen = sop->keylen * 8;
    249 			if (sop->keylen > txform->maxkey ||
    250 			    sop->keylen < txform->minkey) {
    251 				error = EINVAL;
    252 				goto bail;
    253 			}
    254 
    255 			MALLOC(crie.cri_key, u_int8_t *,
    256 			    crie.cri_klen / 8, M_XDATA, M_WAITOK);
    257 			if ((error = copyin(sop->key, crie.cri_key,
    258 			    crie.cri_klen / 8)))
    259 				goto bail;
    260 			if (thash)
    261 				crie.cri_next = &cria;
    262 		}
    263 
    264 		if (thash) {
    265 			cria.cri_alg = thash->type;
    266 			cria.cri_klen = sop->mackeylen * 8;
    267 			if (sop->mackeylen != thash->keysize) {
    268 				error = EINVAL;
    269 				goto bail;
    270 			}
    271 
    272 			if (cria.cri_klen) {
    273 				MALLOC(cria.cri_key, u_int8_t *,
    274 				    cria.cri_klen / 8, M_XDATA, M_WAITOK);
    275 				if ((error = copyin(sop->mackey, cria.cri_key,
    276 				    cria.cri_klen / 8)))
    277 					goto bail;
    278 			}
    279 		}
    280 
    281 		error = crypto_newsession(&sid, (txform ? &crie : &cria),
    282 			    0);
    283 		if (error) {
    284 			/* this is an auditable security event? */
    285 		  	printf("SIOCSESSION violates kernel parameters\n");
    286 			goto bail;
    287 		}
    288 
    289 		cse = csecreate(fcr, sid, crie.cri_key, crie.cri_klen,
    290 		    cria.cri_key, cria.cri_klen, sop->cipher, sop->mac, txform,
    291 		    thash);
    292 
    293 		if (cse == NULL) {
    294 			crypto_freesession(sid);
    295 			error = EINVAL;
    296 			goto bail;
    297 		}
    298 		sop->ses = cse->ses;
    299 
    300 bail:
    301 		if (error) {
    302 			if (crie.cri_key)
    303 				FREE(crie.cri_key, M_XDATA);
    304 			if (cria.cri_key)
    305 				FREE(cria.cri_key, M_XDATA);
    306 		}
    307 		break;
    308 	case CIOCFSESSION:
    309 		ses = *(u_int32_t *)data;
    310 		cse = csefind(fcr, ses);
    311 		if (cse == NULL)
    312 			return (EINVAL);
    313 		csedelete(fcr, cse);
    314 		error = csefree(cse);
    315 		break;
    316 	case CIOCCRYPT:
    317 		cop = (struct crypt_op *)data;
    318 		cse = csefind(fcr, cop->ses);
    319 		if (cse == NULL)
    320 			return (EINVAL);
    321 		error = cryptodev_op(cse, cop, p);
    322 		break;
    323 	case CIOCKEY:
    324 		error = cryptodev_key((struct crypt_kop *)data);
    325 		break;
    326 	case CIOCASYMFEAT:
    327 		error = crypto_getfeat((int *)data);
    328 		break;
    329 	default:
    330 		error = EINVAL;
    331 	}
    332 	return (error);
    333 }
    334 
    335 /* ARGSUSED */
    336 int
    337 cryptof_fcntl(struct file *fp, u_int cmd, void *data, struct proc *p)
    338 {
    339   return (0);
    340 }
    341 
    342 static int
    343 cryptodev_op(struct csession *cse, struct crypt_op *cop, struct proc *p)
    344 {
    345 	struct cryptop *crp = NULL;
    346 	struct cryptodesc *crde = NULL, *crda = NULL;
    347 	int i, error, s;
    348 
    349 	if (cop->len > 256*1024-4)
    350 		return (E2BIG);
    351 
    352 	if (cse->txform && (cop->len % cse->txform->blocksize) != 0)
    353 		return (EINVAL);
    354 
    355 	bzero(&cse->uio, sizeof(cse->uio));
    356 	cse->uio.uio_iovcnt = 1;
    357 	cse->uio.uio_resid = 0;
    358 	cse->uio.uio_segflg = UIO_SYSSPACE;
    359 	cse->uio.uio_rw = UIO_WRITE;
    360 	cse->uio.uio_procp = p;
    361 	cse->uio.uio_iov = cse->iovec;
    362 	bzero(&cse->iovec, sizeof(cse->iovec));
    363 	cse->uio.uio_iov[0].iov_len = cop->len;
    364 	cse->uio.uio_iov[0].iov_base = malloc(cop->len, M_XDATA, M_WAITOK);
    365 	for (i = 0; i < cse->uio.uio_iovcnt; i++)
    366 		cse->uio.uio_resid += cse->uio.uio_iov[0].iov_len;
    367 
    368 	crp = crypto_getreq((cse->txform != NULL) + (cse->thash != NULL));
    369 	if (crp == NULL) {
    370 		error = ENOMEM;
    371 		goto bail;
    372 	}
    373 
    374 	if (cse->thash) {
    375 		crda = crp->crp_desc;
    376 		if (cse->txform)
    377 			crde = crda->crd_next;
    378 	} else {
    379 		if (cse->txform)
    380 			crde = crp->crp_desc;
    381 		else {
    382 			error = EINVAL;
    383 			goto bail;
    384 		}
    385 	}
    386 
    387 	if ((error = copyin(cop->src, cse->uio.uio_iov[0].iov_base, cop->len)))
    388 		goto bail;
    389 
    390 	if (crda) {
    391 		crda->crd_skip = 0;
    392 		crda->crd_len = cop->len;
    393 		crda->crd_inject = 0;	/* ??? */
    394 
    395 		crda->crd_alg = cse->mac;
    396 		crda->crd_key = cse->mackey;
    397 		crda->crd_klen = cse->mackeylen * 8;
    398 	}
    399 
    400 	if (crde) {
    401 		if (cop->op == COP_ENCRYPT)
    402 			crde->crd_flags |= CRD_F_ENCRYPT;
    403 		else
    404 			crde->crd_flags &= ~CRD_F_ENCRYPT;
    405 		crde->crd_len = cop->len;
    406 		crde->crd_inject = 0;
    407 
    408 		crde->crd_alg = cse->cipher;
    409 		crde->crd_key = cse->key;
    410 		crde->crd_klen = cse->keylen * 8;
    411 	}
    412 
    413 	crp->crp_ilen = cop->len;
    414 	crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM
    415 		       | (cop->flags & COP_F_BATCH);
    416 	crp->crp_buf = (caddr_t)&cse->uio;
    417 	crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_cb;
    418 	crp->crp_sid = cse->sid;
    419 	crp->crp_opaque = (void *)cse;
    420 
    421 	if (cop->iv) {
    422 		if (crde == NULL) {
    423 			error = EINVAL;
    424 			goto bail;
    425 		}
    426 		if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
    427 			error = EINVAL;
    428 			goto bail;
    429 		}
    430 		if ((error = copyin(cop->iv, cse->tmp_iv, cse->txform->blocksize)))
    431 			goto bail;
    432 		bcopy(cse->tmp_iv, crde->crd_iv, cse->txform->blocksize);
    433 		crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
    434 		crde->crd_skip = 0;
    435 	} else if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
    436 		crde->crd_skip = 0;
    437 	} else if (crde) {
    438 		crde->crd_flags |= CRD_F_IV_PRESENT;
    439 		crde->crd_skip = cse->txform->blocksize;
    440 		crde->crd_len -= cse->txform->blocksize;
    441 	}
    442 
    443 	if (cop->mac) {
    444 		if (crda == NULL) {
    445 			error = EINVAL;
    446 			goto bail;
    447 		}
    448 		crp->crp_mac=cse->tmp_mac;
    449 	}
    450 
    451 	s = splcrypto();	/* NB: only needed with CRYPTO_F_CBIMM */
    452 	error = crypto_dispatch(crp);
    453 	if (error == 0 && (crp->crp_flags & CRYPTO_F_DONE) == 0)
    454 		error = tsleep(crp, PSOCK, "crydev", 0);
    455 	splx(s);
    456 	if (error) {
    457 		goto bail;
    458 	}
    459 
    460 	if (crp->crp_etype != 0) {
    461 		error = crp->crp_etype;
    462 		goto bail;
    463 	}
    464 
    465 	if (cse->error) {
    466 		error = cse->error;
    467 		goto bail;
    468 	}
    469 
    470 	if (cop->dst &&
    471 	    (error = copyout(cse->uio.uio_iov[0].iov_base, cop->dst, cop->len)))
    472 		goto bail;
    473 
    474 	if (cop->mac &&
    475 	    (error = copyout(crp->crp_mac, cop->mac, cse->thash->authsize)))
    476 		goto bail;
    477 
    478 bail:
    479 	if (crp)
    480 		crypto_freereq(crp);
    481 	if (cse->uio.uio_iov[0].iov_base)
    482 		free(cse->uio.uio_iov[0].iov_base, M_XDATA);
    483 
    484 	return (error);
    485 }
    486 
    487 static int
    488 cryptodev_cb(void *op)
    489 {
    490 	struct cryptop *crp = (struct cryptop *) op;
    491 	struct csession *cse = (struct csession *)crp->crp_opaque;
    492 
    493 	cse->error = crp->crp_etype;
    494 	if (crp->crp_etype == EAGAIN)
    495 		return crypto_dispatch(crp);
    496 	wakeup_one(crp);
    497 	return (0);
    498 }
    499 
    500 static int
    501 cryptodevkey_cb(void *op)
    502 {
    503 	struct cryptkop *krp = (struct cryptkop *) op;
    504 
    505 	wakeup_one(krp);
    506 	return (0);
    507 }
    508 
    509 static int
    510 cryptodev_key(struct crypt_kop *kop)
    511 {
    512 	struct cryptkop *krp = NULL;
    513 	int error = EINVAL;
    514 	int in, out, size, i;
    515 
    516 	if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
    517 		return (EFBIG);
    518 	}
    519 
    520 	in = kop->crk_iparams;
    521 	out = kop->crk_oparams;
    522 	switch (kop->crk_op) {
    523 	case CRK_MOD_EXP:
    524 		if (in == 3 && out == 1)
    525 			break;
    526 		return (EINVAL);
    527 	case CRK_MOD_EXP_CRT:
    528 		if (in == 6 && out == 1)
    529 			break;
    530 		return (EINVAL);
    531 	case CRK_DSA_SIGN:
    532 		if (in == 5 && out == 2)
    533 			break;
    534 		return (EINVAL);
    535 	case CRK_DSA_VERIFY:
    536 		if (in == 7 && out == 0)
    537 			break;
    538 		return (EINVAL);
    539 	case CRK_DH_COMPUTE_KEY:
    540 		if (in == 3 && out == 1)
    541 			break;
    542 		return (EINVAL);
    543 	default:
    544 		return (EINVAL);
    545 	}
    546 
    547 	krp = (struct cryptkop *)malloc(sizeof *krp, M_XDATA, M_WAITOK);
    548 	if (!krp)
    549 		return (ENOMEM);
    550 	bzero(krp, sizeof *krp);
    551 	krp->krp_op = kop->crk_op;
    552 	krp->krp_status = kop->crk_status;
    553 	krp->krp_iparams = kop->crk_iparams;
    554 	krp->krp_oparams = kop->crk_oparams;
    555 	krp->krp_status = 0;
    556 	krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb;
    557 
    558 	for (i = 0; i < CRK_MAXPARAM; i++)
    559 		krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
    560 	for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
    561 		size = (krp->krp_param[i].crp_nbits + 7) / 8;
    562 		if (size == 0)
    563 			continue;
    564 		MALLOC(krp->krp_param[i].crp_p, caddr_t, size, M_XDATA, M_WAITOK);
    565 		if (i >= krp->krp_iparams)
    566 			continue;
    567 		error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size);
    568 		if (error)
    569 			goto fail;
    570 	}
    571 
    572 	error = crypto_kdispatch(krp);
    573 	if (error == 0)
    574 		error = tsleep(krp, PSOCK, "crydev", 0);
    575 	if (error)
    576 		goto fail;
    577 
    578 	if (krp->krp_status != 0) {
    579 		error = krp->krp_status;
    580 		goto fail;
    581 	}
    582 
    583 	for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
    584 		size = (krp->krp_param[i].crp_nbits + 7) / 8;
    585 		if (size == 0)
    586 			continue;
    587 		error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size);
    588 		if (error)
    589 			goto fail;
    590 	}
    591 
    592 fail:
    593 	if (krp) {
    594 		kop->crk_status = krp->krp_status;
    595 		for (i = 0; i < CRK_MAXPARAM; i++) {
    596 			if (krp->krp_param[i].crp_p)
    597 				FREE(krp->krp_param[i].crp_p, M_XDATA);
    598 		}
    599 		free(krp, M_XDATA);
    600 	}
    601 	return (error);
    602 }
    603 
    604 /* ARGSUSED */
    605 static int
    606 cryptof_poll(struct file *fp, int which, struct proc *p)
    607 {
    608 	return (0);
    609 }
    610 
    611 
    612 /* ARGSUSED */
    613 static int
    614 cryptof_kqfilter(struct file *fp, struct knote *kn)
    615 {
    616 
    617 	return (0);
    618 }
    619 
    620 /* ARGSUSED */
    621 static int
    622 cryptof_stat(struct file *fp, struct stat *sb, struct proc *p)
    623 {
    624 	return (EOPNOTSUPP);
    625 }
    626 
    627 /* ARGSUSED */
    628 static int
    629 cryptof_close(struct file *fp, struct proc *p)
    630 {
    631 	struct fcrypt *fcr = (struct fcrypt *)fp->f_data;
    632 	struct csession *cse;
    633 
    634 	while ((cse = TAILQ_FIRST(&fcr->csessions))) {
    635 		TAILQ_REMOVE(&fcr->csessions, cse, next);
    636 		(void)csefree(cse);
    637 	}
    638 	FREE(fcr, M_XDATA);
    639 
    640 	/* close() stolen from sys/kern/kern_ktrace.c */
    641 
    642 	fp->f_data = NULL;
    643 #if 0
    644 	FILE_UNUSE(fp, p);	/* release file */
    645 	fdrelease(p, fd); 	/* release fd table slot */
    646 #endif
    647 
    648 	return 0;
    649 }
    650 
    651 static struct csession *
    652 csefind(struct fcrypt *fcr, u_int ses)
    653 {
    654 	struct csession *cse;
    655 
    656 	TAILQ_FOREACH(cse, &fcr->csessions, next)
    657 		if (cse->ses == ses)
    658 			return (cse);
    659 	return (NULL);
    660 }
    661 
    662 static int
    663 csedelete(struct fcrypt *fcr, struct csession *cse_del)
    664 {
    665 	struct csession *cse;
    666 
    667 	TAILQ_FOREACH(cse, &fcr->csessions, next) {
    668 		if (cse == cse_del) {
    669 			TAILQ_REMOVE(&fcr->csessions, cse, next);
    670 			return (1);
    671 		}
    672 	}
    673 	return (0);
    674 }
    675 
    676 static struct csession *
    677 cseadd(struct fcrypt *fcr, struct csession *cse)
    678 {
    679 	TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
    680 	cse->ses = fcr->sesn++;
    681 	return (cse);
    682 }
    683 
    684 static struct csession *
    685 csecreate(struct fcrypt *fcr, u_int64_t sid, caddr_t key, u_int64_t keylen,
    686     caddr_t mackey, u_int64_t mackeylen, u_int32_t cipher, u_int32_t mac,
    687     struct enc_xform *txform, struct auth_hash *thash)
    688 {
    689 	struct csession *cse;
    690 
    691 	MALLOC(cse, struct csession *, sizeof(struct csession),
    692 	    M_XDATA, M_NOWAIT);
    693 	if (cse == NULL)
    694 		return NULL;
    695 	cse->key = key;
    696 	cse->keylen = keylen/8;
    697 	cse->mackey = mackey;
    698 	cse->mackeylen = mackeylen/8;
    699 	cse->sid = sid;
    700 	cse->cipher = cipher;
    701 	cse->mac = mac;
    702 	cse->txform = txform;
    703 	cse->thash = thash;
    704 	cseadd(fcr, cse);
    705 	return (cse);
    706 }
    707 
    708 static int
    709 csefree(struct csession *cse)
    710 {
    711 	int error;
    712 
    713 	error = crypto_freesession(cse->sid);
    714 	if (cse->key)
    715 		FREE(cse->key, M_XDATA);
    716 	if (cse->mackey)
    717 		FREE(cse->mackey, M_XDATA);
    718 	FREE(cse, M_XDATA);
    719 	return (error);
    720 }
    721 
    722 static int
    723 cryptoopen(dev_t dev, int flag, int mode, struct proc *p)
    724 {
    725 	if (crypto_usercrypto == 0)
    726 		return (ENXIO);
    727 	return (0);
    728 }
    729 
    730 static int
    731 cryptoread(dev_t dev, struct uio *uio, int ioflag)
    732 {
    733 	return (EIO);
    734 }
    735 
    736 static int
    737 cryptowrite(dev_t dev, struct uio *uio, int ioflag)
    738 {
    739 	return (EIO);
    740 }
    741 
    742 static int
    743 cryptoioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
    744 {
    745 	struct file *f;
    746 	struct fcrypt *fcr;
    747 	int fd, error;
    748 
    749 	switch (cmd) {
    750 	case CRIOGET:
    751 		MALLOC(fcr, struct fcrypt *,
    752 		    sizeof(struct fcrypt), M_XDATA, M_WAITOK);
    753 		TAILQ_INIT(&fcr->csessions);
    754 		fcr->sesn = 0;
    755 
    756 		error = falloc(p, &f, &fd);
    757 		if (error) {
    758 			FREE(fcr, M_XDATA);
    759 			return (error);
    760 		}
    761 		f->f_flag = FREAD | FWRITE;
    762 		f->f_type = DTYPE_CRYPTO;
    763 		f->f_ops = &cryptofops;
    764 		f->f_data = (caddr_t) fcr;
    765 		*(u_int32_t *)data = fd;
    766 		FILE_SET_MATURE(f);
    767 		FILE_UNUSE(f, p);
    768 		break;
    769 	default:
    770 		error = EINVAL;
    771 		break;
    772 	}
    773 	return (error);
    774 }
    775 
    776 int
    777 cryptoselect(dev_t dev, int rw, struct proc *p)
    778 {
    779 	return (0);
    780 }
    781 
    782 /*static*/
    783 struct cdevsw crypto_cdevsw = {
    784 	/* open */	cryptoopen,
    785 	/* close */	nullclose,
    786 	/* read */	cryptoread,
    787 	/* write */	cryptowrite,
    788 	/* ioctl */	cryptoioctl,
    789 	/* ttstop?*/	nostop,
    790 	/* ??*/		notty,
    791 	/* poll */	cryptoselect /*nopoll*/,
    792 	/* mmap */	nommap,
    793 	/* kqfilter */	nokqfilter,
    794 };
    795 
    796