Home | History | Annotate | Line # | Download | only in opencrypto
cryptodev.c revision 1.20
      1 /*	$NetBSD: cryptodev.c,v 1.20 2006/04/04 17:30:20 christos 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.20 2006/04/04 17:30:20 christos 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/cryptodev.h>
     54 #include <opencrypto/xform.h>
     55 
     56 #ifdef __NetBSD__
     57   #define splcrypto splnet
     58 #endif
     59 #ifdef CRYPTO_DEBUG
     60 #define DPRINTF(a) uprintf a
     61 #else
     62 #define DPRINTF(a)
     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 lwp *l);
     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 lwp *l);
     99 static int	cryptoselect(dev_t dev, int rw, struct lwp *l);
    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 lwp *l);
    105 static int	cryptof_close(struct file *, struct lwp *);
    106 
    107 static const struct fileops cryptofops = {
    108     cryptof_read,
    109     cryptof_write,
    110     cryptof_ioctl,
    111     fnullop_fcntl,
    112     fnullop_poll,
    113     fbadop_stat,
    114     cryptof_close,
    115     fnullop_kqfilter
    116 };
    117 
    118 static struct	csession *csefind(struct fcrypt *, u_int);
    119 static int	csedelete(struct fcrypt *, struct csession *);
    120 static struct	csession *cseadd(struct fcrypt *, struct csession *);
    121 static struct	csession *csecreate(struct fcrypt *, u_int64_t, caddr_t, u_int64_t,
    122     caddr_t, u_int64_t, u_int32_t, u_int32_t, struct enc_xform *,
    123     struct auth_hash *);
    124 static int	csefree(struct csession *);
    125 
    126 static int	cryptodev_op(struct csession *, struct crypt_op *, struct lwp *);
    127 static int	cryptodev_key(struct crypt_kop *);
    128 int	cryptodev_dokey(struct crypt_kop *kop, struct crparam kvp[]);
    129 
    130 static int	cryptodev_cb(void *);
    131 static int	cryptodevkey_cb(void *);
    132 
    133 /*
    134  * sysctl-able control variables for /dev/crypto now defined in crypto.c:
    135  * crypto_usercrypto, crypto_userasmcrypto, crypto_devallowsoft.
    136  */
    137 
    138 /* ARGSUSED */
    139 int
    140 cryptof_read(struct file *fp, off_t *poff, struct uio *uio,
    141 	     struct ucred *cred, int flags)
    142 {
    143 	return (EIO);
    144 }
    145 
    146 /* ARGSUSED */
    147 int
    148 cryptof_write(struct file *fp, off_t *poff, struct uio *uio,
    149 	      struct ucred *cred, int flags)
    150 {
    151 	return (EIO);
    152 }
    153 
    154 /* ARGSUSED */
    155 int
    156 cryptof_ioctl(struct file *fp, u_long cmd, void* data, struct lwp *l)
    157 {
    158 	struct cryptoini cria, crie;
    159 	struct fcrypt *fcr = (struct fcrypt *)fp->f_data;
    160 	struct csession *cse;
    161 	struct session_op *sop;
    162 	struct crypt_op *cop;
    163 	struct enc_xform *txform = NULL;
    164 	struct auth_hash *thash = NULL;
    165 	u_int64_t sid;
    166 	u_int32_t ses;
    167 	int error = 0;
    168 
    169 	switch (cmd) {
    170 	case CIOCGSESSION:
    171 		sop = (struct session_op *)data;
    172 		switch (sop->cipher) {
    173 		case 0:
    174 			break;
    175 		case CRYPTO_DES_CBC:
    176 			txform = &enc_xform_des;
    177 			break;
    178 		case CRYPTO_3DES_CBC:
    179 			txform = &enc_xform_3des;
    180 			break;
    181 		case CRYPTO_BLF_CBC:
    182 			txform = &enc_xform_blf;
    183 			break;
    184 		case CRYPTO_CAST_CBC:
    185 			txform = &enc_xform_cast5;
    186 			break;
    187 		case CRYPTO_SKIPJACK_CBC:
    188 			txform = &enc_xform_skipjack;
    189 			break;
    190 		case CRYPTO_AES_CBC:
    191 			txform = &enc_xform_rijndael128;
    192 			break;
    193 		case CRYPTO_NULL_CBC:
    194 			txform = &enc_xform_null;
    195 			break;
    196 		case CRYPTO_ARC4:
    197 			txform = &enc_xform_arc4;
    198 			break;
    199 		default:
    200 			DPRINTF(("Invalid cipher %d\n", sop->cipher));
    201 			return (EINVAL);
    202 		}
    203 
    204 		switch (sop->mac) {
    205 		case 0:
    206 			break;
    207 		case CRYPTO_MD5_HMAC:
    208 			thash = &auth_hash_hmac_md5_96;
    209 			break;
    210 		case CRYPTO_SHA1_HMAC:
    211 			thash = &auth_hash_hmac_sha1_96;
    212 			break;
    213 		case CRYPTO_SHA2_HMAC:
    214 			if (sop->mackeylen == auth_hash_hmac_sha2_256.keysize)
    215 				thash = &auth_hash_hmac_sha2_256;
    216 			else if (sop->mackeylen == auth_hash_hmac_sha2_384.keysize)
    217 				thash = &auth_hash_hmac_sha2_384;
    218 			else if (sop->mackeylen == auth_hash_hmac_sha2_512.keysize)
    219 				thash = &auth_hash_hmac_sha2_512;
    220 			else {
    221 				DPRINTF(("Invalid mackeylen %d\n",
    222 				    sop->mackeylen));
    223 				return (EINVAL);
    224 			}
    225 			break;
    226 		case CRYPTO_RIPEMD160_HMAC:
    227 			thash = &auth_hash_hmac_ripemd_160_96;
    228 			break;
    229 		case CRYPTO_MD5:
    230 			thash = &auth_hash_md5;
    231 			break;
    232 		case CRYPTO_SHA1:
    233 			thash = &auth_hash_sha1;
    234 			break;
    235 		case CRYPTO_NULL_HMAC:
    236 			thash = &auth_hash_null;
    237 			break;
    238 		default:
    239 			DPRINTF(("Invalid mac %d\n", sop->mac));
    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 				DPRINTF(("keylen %d not in [%d,%d]\n",
    252 				    sop->keylen, txform->minkey,
    253 				    txform->maxkey));
    254 				error = EINVAL;
    255 				goto bail;
    256 			}
    257 
    258 			crie.cri_key = malloc(crie.cri_klen / 8, M_XDATA,
    259 			    M_WAITOK);
    260 			if ((error = copyin(sop->key, crie.cri_key,
    261 			    crie.cri_klen / 8)))
    262 				goto bail;
    263 			if (thash)
    264 				crie.cri_next = &cria;
    265 		}
    266 
    267 		if (thash) {
    268 			cria.cri_alg = thash->type;
    269 			cria.cri_klen = sop->mackeylen * 8;
    270 			if (sop->mackeylen != thash->keysize) {
    271 				DPRINTF(("mackeylen %d != keysize %d\n",
    272 				    sop->mackeylen, thash->keysize));
    273 				error = EINVAL;
    274 				goto bail;
    275 			}
    276 
    277 			if (cria.cri_klen) {
    278 				cria.cri_key = malloc(cria.cri_klen / 8,
    279 				    M_XDATA, M_WAITOK);
    280 				if ((error = copyin(sop->mackey, cria.cri_key,
    281 				    cria.cri_klen / 8)))
    282 					goto bail;
    283 			}
    284 		}
    285 
    286 		error = crypto_newsession(&sid, (txform ? &crie : &cria),
    287 			    crypto_devallowsoft);
    288 		if (error) {
    289 		  	DPRINTF(("SIOCSESSION violates kernel parameters %d\n",
    290 			    error));
    291 			goto bail;
    292 		}
    293 
    294 		cse = csecreate(fcr, sid, crie.cri_key, crie.cri_klen,
    295 		    cria.cri_key, cria.cri_klen, sop->cipher, sop->mac, txform,
    296 		    thash);
    297 
    298 		if (cse == NULL) {
    299 			DPRINTF(("csecreate failed\n"));
    300 			crypto_freesession(sid);
    301 			error = EINVAL;
    302 			goto bail;
    303 		}
    304 		sop->ses = cse->ses;
    305 
    306 bail:
    307 		if (error) {
    308 			if (crie.cri_key)
    309 				FREE(crie.cri_key, M_XDATA);
    310 			if (cria.cri_key)
    311 				FREE(cria.cri_key, M_XDATA);
    312 		}
    313 		break;
    314 	case CIOCFSESSION:
    315 		ses = *(u_int32_t *)data;
    316 		cse = csefind(fcr, ses);
    317 		if (cse == NULL)
    318 			return (EINVAL);
    319 		csedelete(fcr, cse);
    320 		error = csefree(cse);
    321 		break;
    322 	case CIOCCRYPT:
    323 		cop = (struct crypt_op *)data;
    324 		cse = csefind(fcr, cop->ses);
    325 		if (cse == NULL) {
    326 			DPRINTF(("csefind failed\n"));
    327 			return (EINVAL);
    328 		}
    329 		error = cryptodev_op(cse, cop, l);
    330 		break;
    331 	case CIOCKEY:
    332 		error = cryptodev_key((struct crypt_kop *)data);
    333 		break;
    334 	case CIOCASYMFEAT:
    335 		error = crypto_getfeat((int *)data);
    336 		break;
    337 	default:
    338 		DPRINTF(("invalid ioctl cmd %ld\n", cmd));
    339 		error = EINVAL;
    340 	}
    341 	return (error);
    342 }
    343 
    344 static int
    345 cryptodev_op(struct csession *cse, struct crypt_op *cop, struct lwp *l)
    346 {
    347 	struct cryptop *crp = NULL;
    348 	struct cryptodesc *crde = NULL, *crda = NULL;
    349 	int i, error, s;
    350 
    351 	if (cop->len > 256*1024-4)
    352 		return (E2BIG);
    353 
    354 	if (cse->txform) {
    355 		if (cop->len == 0 || (cop->len % cse->txform->blocksize) != 0)
    356 			return (EINVAL);
    357 	}
    358 
    359 	bzero(&cse->uio, sizeof(cse->uio));
    360 	cse->uio.uio_iovcnt = 1;
    361 	cse->uio.uio_resid = 0;
    362 	cse->uio.uio_rw = UIO_WRITE;
    363 	cse->uio.uio_iov = cse->iovec;
    364 	UIO_SETUP_SYSSPACE(&cse->uio);
    365 	bzero(&cse->iovec, sizeof(cse->iovec));
    366 	cse->uio.uio_iov[0].iov_len = cop->len;
    367 	cse->uio.uio_iov[0].iov_base = malloc(cop->len, M_XDATA, M_WAITOK);
    368 	for (i = 0; i < cse->uio.uio_iovcnt; i++)
    369 		cse->uio.uio_resid += cse->uio.uio_iov[0].iov_len;
    370 
    371 	crp = crypto_getreq((cse->txform != NULL) + (cse->thash != NULL));
    372 	if (crp == NULL) {
    373 		error = ENOMEM;
    374 		goto bail;
    375 	}
    376 
    377 	if (cse->thash) {
    378 		crda = crp->crp_desc;
    379 		if (cse->txform)
    380 			crde = crda->crd_next;
    381 	} else {
    382 		if (cse->txform)
    383 			crde = crp->crp_desc;
    384 		else {
    385 			error = EINVAL;
    386 			goto bail;
    387 		}
    388 	}
    389 
    390 	if ((error = copyin(cop->src, cse->uio.uio_iov[0].iov_base, cop->len)))
    391 		goto bail;
    392 
    393 	if (crda) {
    394 		crda->crd_skip = 0;
    395 		crda->crd_len = cop->len;
    396 		crda->crd_inject = 0;	/* ??? */
    397 
    398 		crda->crd_alg = cse->mac;
    399 		crda->crd_key = cse->mackey;
    400 		crda->crd_klen = cse->mackeylen * 8;
    401 	}
    402 
    403 	if (crde) {
    404 		if (cop->op == COP_ENCRYPT)
    405 			crde->crd_flags |= CRD_F_ENCRYPT;
    406 		else
    407 			crde->crd_flags &= ~CRD_F_ENCRYPT;
    408 		crde->crd_len = cop->len;
    409 		crde->crd_inject = 0;
    410 
    411 		crde->crd_alg = cse->cipher;
    412 		crde->crd_key = cse->key;
    413 		crde->crd_klen = cse->keylen * 8;
    414 	}
    415 
    416 	crp->crp_ilen = cop->len;
    417 	crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM
    418 		       | (cop->flags & COP_F_BATCH);
    419 	crp->crp_buf = (caddr_t)&cse->uio;
    420 	crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_cb;
    421 	crp->crp_sid = cse->sid;
    422 	crp->crp_opaque = (void *)cse;
    423 
    424 	if (cop->iv) {
    425 		if (crde == NULL) {
    426 			error = EINVAL;
    427 			goto bail;
    428 		}
    429 		if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
    430 			error = EINVAL;
    431 			goto bail;
    432 		}
    433 		if ((error = copyin(cop->iv, cse->tmp_iv, cse->txform->blocksize)))
    434 			goto bail;
    435 		bcopy(cse->tmp_iv, crde->crd_iv, cse->txform->blocksize);
    436 		crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
    437 		crde->crd_skip = 0;
    438 	} else if (crde) {
    439 		if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
    440 			crde->crd_skip = 0;
    441 		} else {
    442 			crde->crd_flags |= CRD_F_IV_PRESENT;
    443 			crde->crd_skip = cse->txform->blocksize;
    444 			crde->crd_len -= cse->txform->blocksize;
    445 		}
    446 	}
    447 
    448 	if (cop->mac) {
    449 		if (crda == NULL) {
    450 			error = EINVAL;
    451 			goto bail;
    452 		}
    453 		crp->crp_mac=cse->tmp_mac;
    454 	}
    455 
    456 	s = splcrypto();	/* NB: only needed with CRYPTO_F_CBIMM */
    457 	error = crypto_dispatch(crp);
    458 	if (error == 0 && (crp->crp_flags & CRYPTO_F_DONE) == 0)
    459 		error = tsleep(crp, PSOCK, "crydev", 0);
    460 	splx(s);
    461 	if (error) {
    462 		goto bail;
    463 	}
    464 
    465 	if (crp->crp_etype != 0) {
    466 		error = crp->crp_etype;
    467 		goto bail;
    468 	}
    469 
    470 	if (cse->error) {
    471 		error = cse->error;
    472 		goto bail;
    473 	}
    474 
    475 	if (cop->dst &&
    476 	    (error = copyout(cse->uio.uio_iov[0].iov_base, cop->dst, cop->len)))
    477 		goto bail;
    478 
    479 	if (cop->mac &&
    480 	    (error = copyout(crp->crp_mac, cop->mac, cse->thash->authsize)))
    481 		goto bail;
    482 
    483 bail:
    484 	if (crp)
    485 		crypto_freereq(crp);
    486 	if (cse->uio.uio_iov[0].iov_base)
    487 		free(cse->uio.uio_iov[0].iov_base, M_XDATA);
    488 
    489 	return (error);
    490 }
    491 
    492 static int
    493 cryptodev_cb(void *op)
    494 {
    495 	struct cryptop *crp = (struct cryptop *) op;
    496 	struct csession *cse = (struct csession *)crp->crp_opaque;
    497 
    498 	cse->error = crp->crp_etype;
    499 	if (crp->crp_etype == EAGAIN)
    500 		return crypto_dispatch(crp);
    501 	wakeup_one(crp);
    502 	return (0);
    503 }
    504 
    505 static int
    506 cryptodevkey_cb(void *op)
    507 {
    508 	struct cryptkop *krp = (struct cryptkop *) op;
    509 
    510 	wakeup_one(krp);
    511 	return (0);
    512 }
    513 
    514 static int
    515 cryptodev_key(struct crypt_kop *kop)
    516 {
    517 	struct cryptkop *krp = NULL;
    518 	int error = EINVAL;
    519 	int in, out, size, i;
    520 
    521 	if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
    522 		return (EFBIG);
    523 	}
    524 
    525 	in = kop->crk_iparams;
    526 	out = kop->crk_oparams;
    527 	switch (kop->crk_op) {
    528 	case CRK_MOD_EXP:
    529 		if (in == 3 && out == 1)
    530 			break;
    531 		return (EINVAL);
    532 	case CRK_MOD_EXP_CRT:
    533 		if (in == 6 && out == 1)
    534 			break;
    535 		return (EINVAL);
    536 	case CRK_DSA_SIGN:
    537 		if (in == 5 && out == 2)
    538 			break;
    539 		return (EINVAL);
    540 	case CRK_DSA_VERIFY:
    541 		if (in == 7 && out == 0)
    542 			break;
    543 		return (EINVAL);
    544 	case CRK_DH_COMPUTE_KEY:
    545 		if (in == 3 && out == 1)
    546 			break;
    547 		return (EINVAL);
    548 	default:
    549 		return (EINVAL);
    550 	}
    551 
    552 	krp = (struct cryptkop *)malloc(sizeof *krp, M_XDATA, M_WAITOK);
    553 	if (!krp)
    554 		return (ENOMEM);
    555 	bzero(krp, sizeof *krp);
    556 	krp->krp_op = kop->crk_op;
    557 	krp->krp_status = kop->crk_status;
    558 	krp->krp_iparams = kop->crk_iparams;
    559 	krp->krp_oparams = kop->crk_oparams;
    560 	krp->krp_status = 0;
    561 	krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb;
    562 
    563 	for (i = 0; i < CRK_MAXPARAM; i++)
    564 		krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
    565 	for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
    566 		size = (krp->krp_param[i].crp_nbits + 7) / 8;
    567 		if (size == 0)
    568 			continue;
    569 		krp->krp_param[i].crp_p = malloc(size, M_XDATA, M_WAITOK);
    570 		if (i >= krp->krp_iparams)
    571 			continue;
    572 		error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size);
    573 		if (error)
    574 			goto fail;
    575 	}
    576 
    577 	error = crypto_kdispatch(krp);
    578 	if (error == 0)
    579 		error = tsleep(krp, PSOCK, "crydev", 0);
    580 	if (error)
    581 		goto fail;
    582 
    583 	if (krp->krp_status != 0) {
    584 		error = krp->krp_status;
    585 		goto fail;
    586 	}
    587 
    588 	for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
    589 		size = (krp->krp_param[i].crp_nbits + 7) / 8;
    590 		if (size == 0)
    591 			continue;
    592 		error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size);
    593 		if (error)
    594 			goto fail;
    595 	}
    596 
    597 fail:
    598 	if (krp) {
    599 		kop->crk_status = krp->krp_status;
    600 		for (i = 0; i < CRK_MAXPARAM; i++) {
    601 			if (krp->krp_param[i].crp_p)
    602 				FREE(krp->krp_param[i].crp_p, M_XDATA);
    603 		}
    604 		free(krp, M_XDATA);
    605 	}
    606 	return (error);
    607 }
    608 
    609 /* ARGSUSED */
    610 static int
    611 cryptof_close(struct file *fp, struct lwp *l)
    612 {
    613 	struct fcrypt *fcr = (struct fcrypt *)fp->f_data;
    614 	struct csession *cse;
    615 
    616 	while ((cse = TAILQ_FIRST(&fcr->csessions))) {
    617 		TAILQ_REMOVE(&fcr->csessions, cse, next);
    618 		(void)csefree(cse);
    619 	}
    620 	FREE(fcr, M_XDATA);
    621 
    622 	/* close() stolen from sys/kern/kern_ktrace.c */
    623 
    624 	fp->f_data = NULL;
    625 #if 0
    626 	FILE_UNUSE(fp, l);	/* release file */
    627 	fdrelease(l, fd); 	/* release fd table slot */
    628 #endif
    629 
    630 	return 0;
    631 }
    632 
    633 static struct csession *
    634 csefind(struct fcrypt *fcr, u_int ses)
    635 {
    636 	struct csession *cse;
    637 
    638 	TAILQ_FOREACH(cse, &fcr->csessions, next)
    639 		if (cse->ses == ses)
    640 			return (cse);
    641 	return (NULL);
    642 }
    643 
    644 static int
    645 csedelete(struct fcrypt *fcr, struct csession *cse_del)
    646 {
    647 	struct csession *cse;
    648 
    649 	TAILQ_FOREACH(cse, &fcr->csessions, next) {
    650 		if (cse == cse_del) {
    651 			TAILQ_REMOVE(&fcr->csessions, cse, next);
    652 			return (1);
    653 		}
    654 	}
    655 	return (0);
    656 }
    657 
    658 static struct csession *
    659 cseadd(struct fcrypt *fcr, struct csession *cse)
    660 {
    661 	TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
    662 	cse->ses = fcr->sesn++;
    663 	return (cse);
    664 }
    665 
    666 static struct csession *
    667 csecreate(struct fcrypt *fcr, u_int64_t sid, caddr_t key, u_int64_t keylen,
    668     caddr_t mackey, u_int64_t mackeylen, u_int32_t cipher, u_int32_t mac,
    669     struct enc_xform *txform, struct auth_hash *thash)
    670 {
    671 	struct csession *cse;
    672 
    673 	MALLOC(cse, struct csession *, sizeof(struct csession),
    674 	    M_XDATA, M_NOWAIT);
    675 	if (cse == NULL)
    676 		return NULL;
    677 	cse->key = key;
    678 	cse->keylen = keylen/8;
    679 	cse->mackey = mackey;
    680 	cse->mackeylen = mackeylen/8;
    681 	cse->sid = sid;
    682 	cse->cipher = cipher;
    683 	cse->mac = mac;
    684 	cse->txform = txform;
    685 	cse->thash = thash;
    686 	cseadd(fcr, cse);
    687 	return (cse);
    688 }
    689 
    690 static int
    691 csefree(struct csession *cse)
    692 {
    693 	int error;
    694 
    695 	error = crypto_freesession(cse->sid);
    696 	if (cse->key)
    697 		FREE(cse->key, M_XDATA);
    698 	if (cse->mackey)
    699 		FREE(cse->mackey, M_XDATA);
    700 	FREE(cse, M_XDATA);
    701 	return (error);
    702 }
    703 
    704 static int
    705 cryptoopen(dev_t dev, int flag, int mode, struct lwp *l)
    706 {
    707 	if (crypto_usercrypto == 0)
    708 		return (ENXIO);
    709 	return (0);
    710 }
    711 
    712 static int
    713 cryptoread(dev_t dev, struct uio *uio, int ioflag)
    714 {
    715 	return (EIO);
    716 }
    717 
    718 static int
    719 cryptowrite(dev_t dev, struct uio *uio, int ioflag)
    720 {
    721 	return (EIO);
    722 }
    723 
    724 static int
    725 cryptoioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
    726 {
    727 	struct file *f;
    728 	struct fcrypt *fcr;
    729 	int fd, error;
    730 
    731 	switch (cmd) {
    732 	case CRIOGET:
    733 		MALLOC(fcr, struct fcrypt *,
    734 		    sizeof(struct fcrypt), M_XDATA, M_WAITOK);
    735 		TAILQ_INIT(&fcr->csessions);
    736 		fcr->sesn = 0;
    737 
    738 		error = falloc(l->l_proc, &f, &fd);
    739 		if (error) {
    740 			FREE(fcr, M_XDATA);
    741 			return (error);
    742 		}
    743 		f->f_flag = FREAD | FWRITE;
    744 		f->f_type = DTYPE_CRYPTO;
    745 		f->f_ops = &cryptofops;
    746 		f->f_data = (caddr_t) fcr;
    747 		*(u_int32_t *)data = fd;
    748 		FILE_SET_MATURE(f);
    749 		FILE_UNUSE(f, l);
    750 		break;
    751 	default:
    752 		error = EINVAL;
    753 		break;
    754 	}
    755 	return (error);
    756 }
    757 
    758 int
    759 cryptoselect(dev_t dev, int rw, struct lwp *l)
    760 {
    761 	return (0);
    762 }
    763 
    764 /*static*/
    765 struct cdevsw crypto_cdevsw = {
    766 	/* open */	cryptoopen,
    767 	/* close */	nullclose,
    768 	/* read */	cryptoread,
    769 	/* write */	cryptowrite,
    770 	/* ioctl */	cryptoioctl,
    771 	/* ttstop?*/	nostop,
    772 	/* ??*/		notty,
    773 	/* poll */	cryptoselect /*nopoll*/,
    774 	/* mmap */	nommap,
    775 	/* kqfilter */	nokqfilter,
    776 };
    777 
    778 #ifdef __NetBSD__
    779 /*
    780  * Pseudo-device initialization routine for /dev/crypto
    781  */
    782 void	cryptoattach(int);
    783 
    784 void
    785 cryptoattach(int num)
    786 {
    787 
    788 	/* nothing to do */
    789 }
    790 #endif /* __NetBSD__ */
    791