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