Home | History | Annotate | Line # | Download | only in opencrypto
cryptodev.c revision 1.3
      1 /*	$NetBSD: cryptodev.c,v 1.3 2003/07/30 18:20:15 jonathan Exp $ */
      2 /*	$FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.4.2.3 2003/02/26 00:14:05 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.3 2003/07/30 18:20:15 jonathan 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 <opencrypto/rmd160.h>
     54 #include <opencrypto/cast.h>
     55 #include <opencrypto/skipjack.h>
     56 #include <opencrypto/blf.h>
     57 #include <opencrypto/cryptodev.h>
     58 #include <opencrypto/xform.h>
     59 
     60 #ifdef __NetBSD__
     61   #define splcrypto splnet
     62 #endif
     63 
     64 struct csession {
     65 	TAILQ_ENTRY(csession) next;
     66 	u_int64_t	sid;
     67 	u_int32_t	ses;
     68 
     69 	u_int32_t	cipher;
     70 	struct enc_xform *txform;
     71 	u_int32_t	mac;
     72 	struct auth_hash *thash;
     73 
     74 	caddr_t		key;
     75 	int		keylen;
     76 	u_char		tmp_iv[EALG_MAX_BLOCK_LEN];
     77 
     78 	caddr_t		mackey;
     79 	int		mackeylen;
     80 	u_char		tmp_mac[CRYPTO_MAX_MAC_LEN];
     81 
     82 	struct iovec	iovec[IOV_MAX];
     83 	struct uio	uio;
     84 	int		error;
     85 };
     86 
     87 struct fcrypt {
     88 	TAILQ_HEAD(csessionlist, csession) csessions;
     89 	int		sesn;
     90 };
     91 
     92 
     93 /* Declaration of master device (fd-cloning/ctxt-allocating) entrypoints */
     94 static int	cryptoopen(dev_t dev, int flag, int mode, struct proc *p);
     95 static int	cryptoread(dev_t dev, struct uio *uio, int ioflag);
     96 static int	cryptowrite(dev_t dev, struct uio *uio, int ioflag);
     97 static int	cryptoioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p);
     98 static int	cryptoselect(dev_t dev, int rw, struct proc *p);
     99 
    100 /* Declaration of cloned-device (per-ctxt) entrypoints */
    101 static int	cryptof_read(struct file *, off_t *, struct uio *, struct ucred *, int);
    102 static int	cryptof_write(struct file *, off_t *, struct uio *, struct ucred *, int);
    103 static int	cryptof_ioctl(struct file *, u_long, void*, struct proc *p);
    104 static int	cryptof_fcntl(struct file *, u_int, void*, struct proc *p);
    105 static int	cryptof_poll(struct file *, int, struct proc *);
    106 static int	cryptof_kqfilter(struct file *, struct knote *);
    107 static int	cryptof_stat(struct file *, struct stat *, struct proc *);
    108 static int	cryptof_close(struct file *, struct proc *);
    109 
    110 static struct fileops cryptofops = {
    111     cryptof_read,
    112     cryptof_write,
    113     cryptof_ioctl,
    114     cryptof_fcntl,
    115     cryptof_poll,
    116     cryptof_stat,
    117     cryptof_close,
    118     cryptof_kqfilter
    119 };
    120 
    121 static struct	csession *csefind(struct fcrypt *, u_int);
    122 static int	csedelete(struct fcrypt *, struct csession *);
    123 static struct	csession *cseadd(struct fcrypt *, struct csession *);
    124 static struct	csession *csecreate(struct fcrypt *, u_int64_t, caddr_t, u_int64_t,
    125     caddr_t, u_int64_t, u_int32_t, u_int32_t, struct enc_xform *,
    126     struct auth_hash *);
    127 static int	csefree(struct csession *);
    128 
    129 static int	cryptodev_op(struct csession *, struct crypt_op *, struct proc *);
    130 static int	cryptodev_key(struct crypt_kop *);
    131 int	cryptodev_dokey(struct crypt_kop *kop, struct crparam kvp[]);
    132 
    133 static int	cryptodev_cb(void *);
    134 static int	cryptodevkey_cb(void *);
    135 
    136 int	usercrypto = 1;		/* userland may do crypto requests */
    137 int	userasymcrypto = 1;	/* userland may do asymmetric crypto reqs */
    138 int	cryptodevallowsoft = 1;	/* only use hardware crypto */
    139 
    140 /* ARGSUSED */
    141 int
    142 cryptof_read(struct file *fp, off_t *poff, struct uio *uio,
    143 	     struct ucred *cred, int flags)
    144 {
    145 	return (EIO);
    146 }
    147 
    148 /* ARGSUSED */
    149 int
    150 cryptof_write(struct file *fp, off_t *poff, struct uio *uio,
    151 	      struct ucred *cred, int flags)
    152 {
    153 	return (EIO);
    154 }
    155 
    156 /* ARGSUSED */
    157 int
    158 cryptof_ioctl(struct file *fp, u_long cmd, void* data, struct proc *p)
    159 {
    160 	struct cryptoini cria, crie;
    161 	struct fcrypt *fcr = (struct fcrypt *)fp->f_data;
    162 	struct csession *cse;
    163 	struct session_op *sop;
    164 	struct crypt_op *cop;
    165 	struct enc_xform *txform = NULL;
    166 	struct auth_hash *thash = NULL;
    167 	u_int64_t sid;
    168 	u_int32_t ses;
    169 	int error = 0;
    170 
    171 	switch (cmd) {
    172 	case CIOCGSESSION:
    173 		sop = (struct session_op *)data;
    174 		switch (sop->cipher) {
    175 		case 0:
    176 			break;
    177 		case CRYPTO_DES_CBC:
    178 			txform = &enc_xform_des;
    179 			break;
    180 		case CRYPTO_3DES_CBC:
    181 			txform = &enc_xform_3des;
    182 			break;
    183 		case CRYPTO_BLF_CBC:
    184 			txform = &enc_xform_blf;
    185 			break;
    186 		case CRYPTO_CAST_CBC:
    187 			txform = &enc_xform_cast5;
    188 			break;
    189 		case CRYPTO_SKIPJACK_CBC:
    190 			txform = &enc_xform_skipjack;
    191 			break;
    192 		case CRYPTO_AES_CBC:
    193 			txform = &enc_xform_rijndael128;
    194 			break;
    195 		case CRYPTO_NULL_CBC:
    196 			txform = &enc_xform_null;
    197 			break;
    198 		case CRYPTO_ARC4:
    199 			txform = &enc_xform_arc4;
    200 			break;
    201 		default:
    202 			return (EINVAL);
    203 		}
    204 
    205 		switch (sop->mac) {
    206 		case 0:
    207 			break;
    208 		case CRYPTO_MD5_HMAC:
    209 			thash = &auth_hash_hmac_md5_96;
    210 			break;
    211 		case CRYPTO_SHA1_HMAC:
    212 			thash = &auth_hash_hmac_sha1_96;
    213 			break;
    214 		case CRYPTO_SHA2_HMAC:
    215 			if (sop->mackeylen == auth_hash_hmac_sha2_256.keysize)
    216 				thash = &auth_hash_hmac_sha2_256;
    217 			else if (sop->mackeylen == auth_hash_hmac_sha2_384.keysize)
    218 				thash = &auth_hash_hmac_sha2_384;
    219 			else if (sop->mackeylen == auth_hash_hmac_sha2_512.keysize)
    220 				thash = &auth_hash_hmac_sha2_512;
    221 			else
    222 				return (EINVAL);
    223 			break;
    224 		case CRYPTO_RIPEMD160_HMAC:
    225 			thash = &auth_hash_hmac_ripemd_160_96;
    226 			break;
    227 #ifdef notdef
    228 		case CRYPTO_MD5:
    229 			thash = &auth_hash_md5;
    230 			break;
    231 		case CRYPTO_SHA1:
    232 			thash = &auth_hash_sha1;
    233 			break;
    234 #endif
    235 		case CRYPTO_NULL_HMAC:
    236 			thash = &auth_hash_null;
    237 			break;
    238 		default:
    239 			return (EINVAL);
    240 		}
    241 
    242 		bzero(&crie, sizeof(crie));
    243 		bzero(&cria, sizeof(cria));
    244 
    245 		if (txform) {
    246 			crie.cri_alg = txform->type;
    247 			crie.cri_klen = sop->keylen * 8;
    248 			if (sop->keylen > txform->maxkey ||
    249 			    sop->keylen < txform->minkey) {
    250 				error = EINVAL;
    251 				goto bail;
    252 			}
    253 
    254 			MALLOC(crie.cri_key, u_int8_t *,
    255 			    crie.cri_klen / 8, M_XDATA, M_WAITOK);
    256 			if ((error = copyin(sop->key, crie.cri_key,
    257 			    crie.cri_klen / 8)))
    258 				goto bail;
    259 			if (thash)
    260 				crie.cri_next = &cria;
    261 		}
    262 
    263 		if (thash) {
    264 			cria.cri_alg = thash->type;
    265 			cria.cri_klen = sop->mackeylen * 8;
    266 			if (sop->mackeylen != thash->keysize) {
    267 				error = EINVAL;
    268 				goto bail;
    269 			}
    270 
    271 			if (cria.cri_klen) {
    272 				MALLOC(cria.cri_key, u_int8_t *,
    273 				    cria.cri_klen / 8, M_XDATA, M_WAITOK);
    274 				if ((error = copyin(sop->mackey, cria.cri_key,
    275 				    cria.cri_klen / 8)))
    276 					goto bail;
    277 			}
    278 		}
    279 
    280 		error = crypto_newsession(&sid, (txform ? &crie : &cria),
    281 			    0);
    282 		if (error) {
    283 			/* this is an auditable security event? */
    284 		  	printf("SIOCSESSION violates kernel parameters\n");
    285 			goto bail;
    286 		}
    287 
    288 		cse = csecreate(fcr, sid, crie.cri_key, crie.cri_klen,
    289 		    cria.cri_key, cria.cri_klen, sop->cipher, sop->mac, txform,
    290 		    thash);
    291 
    292 		if (cse == NULL) {
    293 			crypto_freesession(sid);
    294 			error = EINVAL;
    295 			goto bail;
    296 		}
    297 		sop->ses = cse->ses;
    298 
    299 bail:
    300 		if (error) {
    301 			if (crie.cri_key)
    302 				FREE(crie.cri_key, M_XDATA);
    303 			if (cria.cri_key)
    304 				FREE(cria.cri_key, M_XDATA);
    305 		}
    306 		break;
    307 	case CIOCFSESSION:
    308 		ses = *(u_int32_t *)data;
    309 		cse = csefind(fcr, ses);
    310 		if (cse == NULL)
    311 			return (EINVAL);
    312 		csedelete(fcr, cse);
    313 		error = csefree(cse);
    314 		break;
    315 	case CIOCCRYPT:
    316 		cop = (struct crypt_op *)data;
    317 		cse = csefind(fcr, cop->ses);
    318 		if (cse == NULL)
    319 			return (EINVAL);
    320 		error = cryptodev_op(cse, cop, p);
    321 		break;
    322 	case CIOCKEY:
    323 		error = cryptodev_key((struct crypt_kop *)data);
    324 		break;
    325 	case CIOCASYMFEAT:
    326 		error = crypto_getfeat((int *)data);
    327 		break;
    328 	default:
    329 		error = EINVAL;
    330 	}
    331 	return (error);
    332 }
    333 
    334 /* ARGSUSED */
    335 int
    336 cryptof_fcntl(struct file *fp, u_int cmd, void *data, struct proc *p)
    337 {
    338   return (0);
    339 }
    340 
    341 static int
    342 cryptodev_op(struct csession *cse, struct crypt_op *cop, struct proc *p)
    343 {
    344 	struct cryptop *crp = NULL;
    345 	struct cryptodesc *crde = NULL, *crda = NULL;
    346 	int i, error, s;
    347 
    348 	if (cop->len > 256*1024-4)
    349 		return (E2BIG);
    350 
    351 	if (cse->txform && (cop->len % cse->txform->blocksize) != 0)
    352 		return (EINVAL);
    353 
    354 	bzero(&cse->uio, sizeof(cse->uio));
    355 	cse->uio.uio_iovcnt = 1;
    356 	cse->uio.uio_resid = 0;
    357 	cse->uio.uio_segflg = UIO_SYSSPACE;
    358 	cse->uio.uio_rw = UIO_WRITE;
    359 	cse->uio.uio_procp = p;
    360 	cse->uio.uio_iov = cse->iovec;
    361 	bzero(&cse->iovec, sizeof(cse->iovec));
    362 	cse->uio.uio_iov[0].iov_len = cop->len;
    363 	cse->uio.uio_iov[0].iov_base = malloc(cop->len, M_XDATA, M_WAITOK);
    364 	for (i = 0; i < cse->uio.uio_iovcnt; i++)
    365 		cse->uio.uio_resid += cse->uio.uio_iov[0].iov_len;
    366 
    367 	crp = crypto_getreq((cse->txform != NULL) + (cse->thash != NULL));
    368 	if (crp == NULL) {
    369 		error = ENOMEM;
    370 		goto bail;
    371 	}
    372 
    373 	if (cse->thash) {
    374 		crda = crp->crp_desc;
    375 		if (cse->txform)
    376 			crde = crda->crd_next;
    377 	} else {
    378 		if (cse->txform)
    379 			crde = crp->crp_desc;
    380 		else {
    381 			error = EINVAL;
    382 			goto bail;
    383 		}
    384 	}
    385 
    386 	if ((error = copyin(cop->src, cse->uio.uio_iov[0].iov_base, cop->len)))
    387 		goto bail;
    388 
    389 	if (crda) {
    390 		crda->crd_skip = 0;
    391 		crda->crd_len = cop->len;
    392 		crda->crd_inject = 0;	/* ??? */
    393 
    394 		crda->crd_alg = cse->mac;
    395 		crda->crd_key = cse->mackey;
    396 		crda->crd_klen = cse->mackeylen * 8;
    397 	}
    398 
    399 	if (crde) {
    400 		if (cop->op == COP_ENCRYPT)
    401 			crde->crd_flags |= CRD_F_ENCRYPT;
    402 		else
    403 			crde->crd_flags &= ~CRD_F_ENCRYPT;
    404 		crde->crd_len = cop->len;
    405 		crde->crd_inject = 0;
    406 
    407 		crde->crd_alg = cse->cipher;
    408 		crde->crd_key = cse->key;
    409 		crde->crd_klen = cse->keylen * 8;
    410 	}
    411 
    412 	crp->crp_ilen = cop->len;
    413 	crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM
    414 		       | (cop->flags & COP_F_BATCH);
    415 	crp->crp_buf = (caddr_t)&cse->uio;
    416 	crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_cb;
    417 	crp->crp_sid = cse->sid;
    418 	crp->crp_opaque = (void *)cse;
    419 
    420 	if (cop->iv) {
    421 		if (crde == NULL) {
    422 			error = EINVAL;
    423 			goto bail;
    424 		}
    425 		if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
    426 			error = EINVAL;
    427 			goto bail;
    428 		}
    429 		if ((error = copyin(cop->iv, cse->tmp_iv, cse->txform->blocksize)))
    430 			goto bail;
    431 		bcopy(cse->tmp_iv, crde->crd_iv, cse->txform->blocksize);
    432 		crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
    433 		crde->crd_skip = 0;
    434 	} else if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
    435 		crde->crd_skip = 0;
    436 	} else if (crde) {
    437 		crde->crd_flags |= CRD_F_IV_PRESENT;
    438 		crde->crd_skip = cse->txform->blocksize;
    439 		crde->crd_len -= cse->txform->blocksize;
    440 	}
    441 
    442 	if (cop->mac) {
    443 		if (crda == NULL) {
    444 			error = EINVAL;
    445 			goto bail;
    446 		}
    447 		crp->crp_mac=cse->tmp_mac;
    448 	}
    449 
    450 	s = splcrypto();	/* NB: only needed with CRYPTO_F_CBIMM */
    451 	error = crypto_dispatch(crp);
    452 	if (error == 0) {
    453 		error = tsleep(crp, PSOCK, "crydev", 0);
    454 	}
    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 #define	CRYPTO_MAJOR	70		/* from openbsd */
    783 /*static*/
    784 struct cdevsw crypto_cdevsw = {
    785 	/* open */	cryptoopen,
    786 	/* close */	nullclose,
    787 	/* read */	cryptoread,
    788 	/* write */	cryptowrite,
    789 	/* ioctl */	cryptoioctl,
    790 	/* ttstop?*/	nostop,
    791 	/* ??*/		notty,
    792 	/* poll */	cryptoselect /*nopoll*/,
    793 	/* mmap */	nommap,
    794 	/* kqfilter */	nokqfilter,
    795 };
    796 
    797