Home | History | Annotate | Line # | Download | only in opencrypto
cryptodev.c revision 1.34.6.1
      1  1.34.6.1       mjf /*	$NetBSD: cryptodev.c,v 1.34.6.1 2008/04/03 12:43:10 mjf Exp $ */
      2       1.4  jonathan /*	$FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.4.2.4 2003/06/03 00:09:02 sam Exp $	*/
      3       1.1  jonathan /*	$OpenBSD: cryptodev.c,v 1.53 2002/07/10 22:21:30 mickey Exp $	*/
      4       1.1  jonathan 
      5       1.1  jonathan /*
      6       1.1  jonathan  * Copyright (c) 2001 Theo de Raadt
      7       1.1  jonathan  *
      8       1.1  jonathan  * Redistribution and use in source and binary forms, with or without
      9       1.1  jonathan  * modification, are permitted provided that the following conditions
     10       1.1  jonathan  * are met:
     11       1.1  jonathan  *
     12       1.1  jonathan  * 1. Redistributions of source code must retain the above copyright
     13       1.1  jonathan  *   notice, this list of conditions and the following disclaimer.
     14       1.1  jonathan  * 2. Redistributions in binary form must reproduce the above copyright
     15       1.1  jonathan  *   notice, this list of conditions and the following disclaimer in the
     16       1.1  jonathan  *   documentation and/or other materials provided with the distribution.
     17       1.1  jonathan  * 3. The name of the author may not be used to endorse or promote products
     18       1.1  jonathan  *   derived from this software without specific prior written permission.
     19       1.1  jonathan  *
     20       1.1  jonathan  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21       1.1  jonathan  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22       1.1  jonathan  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23       1.1  jonathan  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24       1.1  jonathan  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25       1.1  jonathan  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26       1.1  jonathan  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27       1.1  jonathan  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28       1.1  jonathan  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29       1.1  jonathan  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30       1.1  jonathan  *
     31       1.1  jonathan  * Effort sponsored in part by the Defense Advanced Research Projects
     32       1.1  jonathan  * Agency (DARPA) and Air Force Research Laboratory, Air Force
     33       1.1  jonathan  * Materiel Command, USAF, under agreement number F30602-01-2-0537.
     34       1.1  jonathan  *
     35       1.1  jonathan  */
     36       1.1  jonathan 
     37       1.1  jonathan #include <sys/cdefs.h>
     38  1.34.6.1       mjf __KERNEL_RCSID(0, "$NetBSD: cryptodev.c,v 1.34.6.1 2008/04/03 12:43:10 mjf Exp $");
     39       1.1  jonathan 
     40       1.1  jonathan #include <sys/param.h>
     41       1.1  jonathan #include <sys/systm.h>
     42       1.1  jonathan #include <sys/malloc.h>
     43       1.1  jonathan #include <sys/mbuf.h>
     44      1.29       tls #include <sys/pool.h>
     45       1.1  jonathan #include <sys/sysctl.h>
     46       1.1  jonathan #include <sys/file.h>
     47       1.1  jonathan #include <sys/filedesc.h>
     48       1.1  jonathan #include <sys/errno.h>
     49       1.2  jonathan #include <sys/md5.h>
     50       1.1  jonathan #include <sys/sha1.h>
     51       1.1  jonathan #include <sys/conf.h>
     52       1.1  jonathan #include <sys/device.h>
     53      1.21      elad #include <sys/kauth.h>
     54       1.1  jonathan 
     55      1.33       tls #include "opt_ocf.h"
     56       1.1  jonathan #include <opencrypto/cryptodev.h>
     57       1.1  jonathan #include <opencrypto/xform.h>
     58       1.1  jonathan 
     59       1.1  jonathan struct csession {
     60       1.1  jonathan 	TAILQ_ENTRY(csession) next;
     61       1.1  jonathan 	u_int64_t	sid;
     62       1.1  jonathan 	u_int32_t	ses;
     63       1.1  jonathan 
     64       1.1  jonathan 	u_int32_t	cipher;
     65       1.1  jonathan 	struct enc_xform *txform;
     66       1.1  jonathan 	u_int32_t	mac;
     67       1.1  jonathan 	struct auth_hash *thash;
     68       1.1  jonathan 
     69      1.26  christos 	void *		key;
     70       1.1  jonathan 	int		keylen;
     71       1.1  jonathan 	u_char		tmp_iv[EALG_MAX_BLOCK_LEN];
     72       1.1  jonathan 
     73      1.26  christos 	void *		mackey;
     74       1.1  jonathan 	int		mackeylen;
     75       1.1  jonathan 	u_char		tmp_mac[CRYPTO_MAX_MAC_LEN];
     76       1.1  jonathan 
     77      1.29       tls 	struct iovec	iovec[1];	/* user requests never have more */
     78       1.1  jonathan 	struct uio	uio;
     79       1.1  jonathan 	int		error;
     80       1.1  jonathan };
     81       1.1  jonathan 
     82       1.1  jonathan struct fcrypt {
     83       1.1  jonathan 	TAILQ_HEAD(csessionlist, csession) csessions;
     84       1.1  jonathan 	int		sesn;
     85       1.1  jonathan };
     86       1.1  jonathan 
     87      1.29       tls /* For our fixed-size allocations */
     88      1.29       tls struct pool fcrpl;
     89      1.29       tls struct pool csepl;
     90       1.1  jonathan 
     91       1.1  jonathan /* Declaration of master device (fd-cloning/ctxt-allocating) entrypoints */
     92      1.16  christos static int	cryptoopen(dev_t dev, int flag, int mode, struct lwp *l);
     93       1.1  jonathan static int	cryptoread(dev_t dev, struct uio *uio, int ioflag);
     94       1.1  jonathan static int	cryptowrite(dev_t dev, struct uio *uio, int ioflag);
     95      1.16  christos static int	cryptoselect(dev_t dev, int rw, struct lwp *l);
     96       1.1  jonathan 
     97       1.1  jonathan /* Declaration of cloned-device (per-ctxt) entrypoints */
     98  1.34.6.1       mjf static int	cryptof_read(file_t *, off_t *, struct uio *, kauth_cred_t, int);
     99  1.34.6.1       mjf static int	cryptof_write(file_t *, off_t *, struct uio *, kauth_cred_t, int);
    100  1.34.6.1       mjf static int	cryptof_ioctl(file_t *, u_long, void *);
    101  1.34.6.1       mjf static int	cryptof_close(file_t *);
    102       1.1  jonathan 
    103      1.12  christos static const struct fileops cryptofops = {
    104       1.1  jonathan     cryptof_read,
    105       1.1  jonathan     cryptof_write,
    106       1.1  jonathan     cryptof_ioctl,
    107      1.12  christos     fnullop_fcntl,
    108      1.12  christos     fnullop_poll,
    109      1.12  christos     fbadop_stat,
    110       1.1  jonathan     cryptof_close,
    111      1.12  christos     fnullop_kqfilter
    112       1.1  jonathan };
    113       1.1  jonathan 
    114       1.1  jonathan static struct	csession *csefind(struct fcrypt *, u_int);
    115       1.1  jonathan static int	csedelete(struct fcrypt *, struct csession *);
    116       1.1  jonathan static struct	csession *cseadd(struct fcrypt *, struct csession *);
    117      1.26  christos static struct	csession *csecreate(struct fcrypt *, u_int64_t, void *, u_int64_t,
    118      1.26  christos     void *, u_int64_t, u_int32_t, u_int32_t, struct enc_xform *,
    119       1.1  jonathan     struct auth_hash *);
    120       1.1  jonathan static int	csefree(struct csession *);
    121       1.1  jonathan 
    122      1.16  christos static int	cryptodev_op(struct csession *, struct crypt_op *, struct lwp *);
    123       1.1  jonathan static int	cryptodev_key(struct crypt_kop *);
    124       1.1  jonathan int	cryptodev_dokey(struct crypt_kop *kop, struct crparam kvp[]);
    125       1.1  jonathan 
    126       1.1  jonathan static int	cryptodev_cb(void *);
    127       1.1  jonathan static int	cryptodevkey_cb(void *);
    128       1.1  jonathan 
    129       1.9  jonathan /*
    130       1.9  jonathan  * sysctl-able control variables for /dev/crypto now defined in crypto.c:
    131       1.9  jonathan  * crypto_usercrypto, crypto_userasmcrypto, crypto_devallowsoft.
    132       1.9  jonathan  */
    133       1.1  jonathan 
    134       1.1  jonathan /* ARGSUSED */
    135       1.1  jonathan int
    136  1.34.6.1       mjf cryptof_read(file_t *fp, off_t *poff,
    137      1.25  christos     struct uio *uio, kauth_cred_t cred, int flags)
    138       1.1  jonathan {
    139       1.1  jonathan 	return (EIO);
    140       1.1  jonathan }
    141       1.1  jonathan 
    142       1.1  jonathan /* ARGSUSED */
    143       1.1  jonathan int
    144  1.34.6.1       mjf cryptof_write(file_t *fp, off_t *poff,
    145      1.25  christos     struct uio *uio, kauth_cred_t cred, int flags)
    146       1.1  jonathan {
    147       1.1  jonathan 	return (EIO);
    148       1.1  jonathan }
    149       1.1  jonathan 
    150       1.1  jonathan /* ARGSUSED */
    151       1.1  jonathan int
    152  1.34.6.1       mjf cryptof_ioctl(file_t *fp, u_long cmd, void *data)
    153       1.1  jonathan {
    154       1.1  jonathan 	struct cryptoini cria, crie;
    155  1.34.6.1       mjf 	struct fcrypt *fcr = fp->f_data;
    156       1.1  jonathan 	struct csession *cse;
    157       1.1  jonathan 	struct session_op *sop;
    158       1.1  jonathan 	struct crypt_op *cop;
    159       1.1  jonathan 	struct enc_xform *txform = NULL;
    160       1.1  jonathan 	struct auth_hash *thash = NULL;
    161       1.1  jonathan 	u_int64_t sid;
    162       1.1  jonathan 	u_int32_t ses;
    163       1.1  jonathan 	int error = 0;
    164       1.1  jonathan 
    165      1.29       tls 	/* backwards compatibility */
    166  1.34.6.1       mjf         file_t *criofp;
    167      1.29       tls 	struct fcrypt *criofcr;
    168      1.29       tls 	int criofd;
    169      1.29       tls 
    170      1.29       tls         switch (cmd) {
    171      1.29       tls         case CRIOGET:   /* XXX deprecated, remove after 5.0 */
    172  1.34.6.1       mjf                 if ((error = fd_allocfile(&criofp, &criofd)) != 0)
    173      1.29       tls                         return error;
    174      1.29       tls                 criofcr = pool_get(&fcrpl, PR_WAITOK);
    175      1.33       tls 		mutex_spin_enter(&crypto_mtx);
    176      1.29       tls                 TAILQ_INIT(&criofcr->csessions);
    177      1.29       tls                 /*
    178      1.29       tls                  * Don't ever return session 0, to allow detection of
    179      1.29       tls                  * failed creation attempts with multi-create ioctl.
    180      1.29       tls                  */
    181      1.29       tls                 criofcr->sesn = 1;
    182      1.33       tls 		mutex_spin_exit(&crypto_mtx);
    183  1.34.6.1       mjf                 (void)fd_clone(criofp, criofd, (FREAD|FWRITE),
    184      1.29       tls 			      &cryptofops, criofcr);
    185      1.29       tls                 *(u_int32_t *)data = criofd;
    186      1.29       tls 		return error;
    187      1.29       tls                 break;
    188       1.1  jonathan 	case CIOCGSESSION:
    189       1.1  jonathan 		sop = (struct session_op *)data;
    190       1.1  jonathan 		switch (sop->cipher) {
    191       1.1  jonathan 		case 0:
    192       1.1  jonathan 			break;
    193       1.1  jonathan 		case CRYPTO_DES_CBC:
    194       1.1  jonathan 			txform = &enc_xform_des;
    195       1.1  jonathan 			break;
    196       1.1  jonathan 		case CRYPTO_3DES_CBC:
    197       1.1  jonathan 			txform = &enc_xform_3des;
    198       1.1  jonathan 			break;
    199       1.1  jonathan 		case CRYPTO_BLF_CBC:
    200       1.1  jonathan 			txform = &enc_xform_blf;
    201       1.1  jonathan 			break;
    202       1.1  jonathan 		case CRYPTO_CAST_CBC:
    203       1.1  jonathan 			txform = &enc_xform_cast5;
    204       1.1  jonathan 			break;
    205       1.1  jonathan 		case CRYPTO_SKIPJACK_CBC:
    206       1.1  jonathan 			txform = &enc_xform_skipjack;
    207       1.1  jonathan 			break;
    208       1.1  jonathan 		case CRYPTO_AES_CBC:
    209       1.1  jonathan 			txform = &enc_xform_rijndael128;
    210       1.1  jonathan 			break;
    211       1.1  jonathan 		case CRYPTO_NULL_CBC:
    212       1.1  jonathan 			txform = &enc_xform_null;
    213       1.1  jonathan 			break;
    214       1.1  jonathan 		case CRYPTO_ARC4:
    215       1.1  jonathan 			txform = &enc_xform_arc4;
    216       1.1  jonathan 			break;
    217       1.1  jonathan 		default:
    218      1.18  christos 			DPRINTF(("Invalid cipher %d\n", sop->cipher));
    219       1.1  jonathan 			return (EINVAL);
    220       1.1  jonathan 		}
    221       1.1  jonathan 
    222       1.1  jonathan 		switch (sop->mac) {
    223       1.1  jonathan 		case 0:
    224       1.1  jonathan 			break;
    225       1.1  jonathan 		case CRYPTO_MD5_HMAC:
    226      1.32       tls 			thash = &auth_hash_hmac_md5;
    227      1.32       tls 			break;
    228      1.32       tls 		case CRYPTO_SHA1_HMAC:
    229      1.32       tls 			thash = &auth_hash_hmac_sha1;
    230      1.32       tls 			break;
    231      1.32       tls 		case CRYPTO_MD5_HMAC_96:
    232       1.1  jonathan 			thash = &auth_hash_hmac_md5_96;
    233       1.1  jonathan 			break;
    234      1.32       tls 		case CRYPTO_SHA1_HMAC_96:
    235       1.1  jonathan 			thash = &auth_hash_hmac_sha1_96;
    236       1.1  jonathan 			break;
    237       1.1  jonathan 		case CRYPTO_SHA2_HMAC:
    238       1.1  jonathan 			if (sop->mackeylen == auth_hash_hmac_sha2_256.keysize)
    239       1.1  jonathan 				thash = &auth_hash_hmac_sha2_256;
    240       1.1  jonathan 			else if (sop->mackeylen == auth_hash_hmac_sha2_384.keysize)
    241       1.1  jonathan 				thash = &auth_hash_hmac_sha2_384;
    242       1.1  jonathan 			else if (sop->mackeylen == auth_hash_hmac_sha2_512.keysize)
    243       1.1  jonathan 				thash = &auth_hash_hmac_sha2_512;
    244      1.18  christos 			else {
    245      1.18  christos 				DPRINTF(("Invalid mackeylen %d\n",
    246      1.18  christos 				    sop->mackeylen));
    247       1.1  jonathan 				return (EINVAL);
    248      1.18  christos 			}
    249       1.1  jonathan 			break;
    250       1.1  jonathan 		case CRYPTO_RIPEMD160_HMAC:
    251       1.1  jonathan 			thash = &auth_hash_hmac_ripemd_160_96;
    252       1.1  jonathan 			break;
    253       1.1  jonathan 		case CRYPTO_MD5:
    254       1.1  jonathan 			thash = &auth_hash_md5;
    255       1.1  jonathan 			break;
    256       1.1  jonathan 		case CRYPTO_SHA1:
    257       1.1  jonathan 			thash = &auth_hash_sha1;
    258       1.1  jonathan 			break;
    259       1.1  jonathan 		case CRYPTO_NULL_HMAC:
    260       1.1  jonathan 			thash = &auth_hash_null;
    261       1.1  jonathan 			break;
    262       1.1  jonathan 		default:
    263      1.18  christos 			DPRINTF(("Invalid mac %d\n", sop->mac));
    264       1.1  jonathan 			return (EINVAL);
    265       1.1  jonathan 		}
    266       1.1  jonathan 
    267       1.1  jonathan 		bzero(&crie, sizeof(crie));
    268       1.1  jonathan 		bzero(&cria, sizeof(cria));
    269       1.1  jonathan 
    270       1.1  jonathan 		if (txform) {
    271       1.1  jonathan 			crie.cri_alg = txform->type;
    272       1.1  jonathan 			crie.cri_klen = sop->keylen * 8;
    273       1.1  jonathan 			if (sop->keylen > txform->maxkey ||
    274       1.1  jonathan 			    sop->keylen < txform->minkey) {
    275      1.18  christos 				DPRINTF(("keylen %d not in [%d,%d]\n",
    276      1.18  christos 				    sop->keylen, txform->minkey,
    277      1.18  christos 				    txform->maxkey));
    278       1.1  jonathan 				error = EINVAL;
    279       1.1  jonathan 				goto bail;
    280       1.1  jonathan 			}
    281       1.1  jonathan 
    282      1.19  christos 			crie.cri_key = malloc(crie.cri_klen / 8, M_XDATA,
    283      1.19  christos 			    M_WAITOK);
    284       1.1  jonathan 			if ((error = copyin(sop->key, crie.cri_key,
    285       1.1  jonathan 			    crie.cri_klen / 8)))
    286       1.1  jonathan 				goto bail;
    287       1.1  jonathan 			if (thash)
    288       1.1  jonathan 				crie.cri_next = &cria;
    289       1.1  jonathan 		}
    290       1.1  jonathan 
    291       1.1  jonathan 		if (thash) {
    292       1.1  jonathan 			cria.cri_alg = thash->type;
    293       1.1  jonathan 			cria.cri_klen = sop->mackeylen * 8;
    294       1.1  jonathan 			if (sop->mackeylen != thash->keysize) {
    295      1.18  christos 				DPRINTF(("mackeylen %d != keysize %d\n",
    296      1.18  christos 				    sop->mackeylen, thash->keysize));
    297       1.1  jonathan 				error = EINVAL;
    298       1.1  jonathan 				goto bail;
    299       1.1  jonathan 			}
    300       1.1  jonathan 
    301       1.1  jonathan 			if (cria.cri_klen) {
    302      1.19  christos 				cria.cri_key = malloc(cria.cri_klen / 8,
    303      1.19  christos 				    M_XDATA, M_WAITOK);
    304       1.1  jonathan 				if ((error = copyin(sop->mackey, cria.cri_key,
    305       1.1  jonathan 				    cria.cri_klen / 8)))
    306       1.1  jonathan 					goto bail;
    307       1.1  jonathan 			}
    308       1.1  jonathan 		}
    309      1.33       tls 		/* crypto_newsession requires that we hold the mutex. */
    310      1.33       tls 		mutex_spin_enter(&crypto_mtx);
    311       1.1  jonathan 		error = crypto_newsession(&sid, (txform ? &crie : &cria),
    312      1.30       tls 			    crypto_devallowsoft);
    313      1.34       tls 		if (!error) {
    314      1.34       tls 			cse = csecreate(fcr, sid, crie.cri_key, crie.cri_klen,
    315      1.34       tls 			    cria.cri_key, cria.cri_klen, sop->cipher, sop->mac,
    316      1.34       tls 			    txform, thash);
    317      1.34       tls 			if (cse != NULL) {
    318      1.34       tls 				sop->ses = cse->ses;
    319      1.34       tls 			} else {
    320      1.34       tls 				DPRINTF(("csecreate failed\n"));
    321      1.34       tls 				crypto_freesession(sid);
    322      1.34       tls 				error = EINVAL;
    323      1.34       tls 			}
    324      1.34       tls 		} else {
    325      1.18  christos 		  	DPRINTF(("SIOCSESSION violates kernel parameters %d\n",
    326      1.18  christos 			    error));
    327       1.1  jonathan 		}
    328      1.34       tls 		mutex_spin_exit(&crypto_mtx);
    329       1.1  jonathan bail:
    330       1.1  jonathan 		if (error) {
    331       1.1  jonathan 			if (crie.cri_key)
    332       1.1  jonathan 				FREE(crie.cri_key, M_XDATA);
    333       1.1  jonathan 			if (cria.cri_key)
    334       1.1  jonathan 				FREE(cria.cri_key, M_XDATA);
    335       1.1  jonathan 		}
    336       1.1  jonathan 		break;
    337       1.1  jonathan 	case CIOCFSESSION:
    338      1.33       tls 		mutex_spin_enter(&crypto_mtx);
    339       1.1  jonathan 		ses = *(u_int32_t *)data;
    340       1.1  jonathan 		cse = csefind(fcr, ses);
    341       1.1  jonathan 		if (cse == NULL)
    342       1.1  jonathan 			return (EINVAL);
    343       1.1  jonathan 		csedelete(fcr, cse);
    344       1.1  jonathan 		error = csefree(cse);
    345      1.33       tls 		mutex_spin_exit(&crypto_mtx);
    346       1.1  jonathan 		break;
    347       1.1  jonathan 	case CIOCCRYPT:
    348      1.33       tls 		mutex_spin_enter(&crypto_mtx);
    349       1.1  jonathan 		cop = (struct crypt_op *)data;
    350       1.1  jonathan 		cse = csefind(fcr, cop->ses);
    351      1.33       tls 		mutex_spin_exit(&crypto_mtx);
    352      1.18  christos 		if (cse == NULL) {
    353      1.18  christos 			DPRINTF(("csefind failed\n"));
    354       1.1  jonathan 			return (EINVAL);
    355      1.18  christos 		}
    356  1.34.6.1       mjf 		error = cryptodev_op(cse, cop, curlwp);
    357      1.34       tls 		DPRINTF(("cryptodev_op error = %d\n", error));
    358       1.1  jonathan 		break;
    359       1.1  jonathan 	case CIOCKEY:
    360       1.1  jonathan 		error = cryptodev_key((struct crypt_kop *)data);
    361      1.34       tls 		DPRINTF(("cryptodev_key error = %d\n", error));
    362       1.1  jonathan 		break;
    363       1.1  jonathan 	case CIOCASYMFEAT:
    364       1.1  jonathan 		error = crypto_getfeat((int *)data);
    365       1.1  jonathan 		break;
    366       1.1  jonathan 	default:
    367      1.18  christos 		DPRINTF(("invalid ioctl cmd %ld\n", cmd));
    368       1.1  jonathan 		error = EINVAL;
    369       1.1  jonathan 	}
    370       1.1  jonathan 	return (error);
    371       1.1  jonathan }
    372       1.1  jonathan 
    373       1.1  jonathan static int
    374      1.25  christos cryptodev_op(struct csession *cse, struct crypt_op *cop, struct lwp *l)
    375       1.1  jonathan {
    376       1.1  jonathan 	struct cryptop *crp = NULL;
    377       1.1  jonathan 	struct cryptodesc *crde = NULL, *crda = NULL;
    378      1.33       tls 	int error;
    379       1.1  jonathan 
    380       1.1  jonathan 	if (cop->len > 256*1024-4)
    381       1.1  jonathan 		return (E2BIG);
    382       1.1  jonathan 
    383      1.13  jonathan 	if (cse->txform) {
    384      1.13  jonathan 		if (cop->len == 0 || (cop->len % cse->txform->blocksize) != 0)
    385      1.13  jonathan 			return (EINVAL);
    386      1.13  jonathan 	}
    387       1.1  jonathan 
    388       1.1  jonathan 	bzero(&cse->uio, sizeof(cse->uio));
    389       1.1  jonathan 	cse->uio.uio_iovcnt = 1;
    390       1.1  jonathan 	cse->uio.uio_resid = 0;
    391       1.1  jonathan 	cse->uio.uio_rw = UIO_WRITE;
    392       1.1  jonathan 	cse->uio.uio_iov = cse->iovec;
    393      1.17      yamt 	UIO_SETUP_SYSSPACE(&cse->uio);
    394      1.29       tls 	memset(&cse->iovec, 0, sizeof(cse->iovec));
    395       1.1  jonathan 	cse->uio.uio_iov[0].iov_len = cop->len;
    396       1.1  jonathan 	cse->uio.uio_iov[0].iov_base = malloc(cop->len, M_XDATA, M_WAITOK);
    397      1.29       tls 	cse->uio.uio_resid = cse->uio.uio_iov[0].iov_len;
    398       1.1  jonathan 
    399       1.1  jonathan 	crp = crypto_getreq((cse->txform != NULL) + (cse->thash != NULL));
    400       1.1  jonathan 	if (crp == NULL) {
    401       1.1  jonathan 		error = ENOMEM;
    402       1.1  jonathan 		goto bail;
    403       1.1  jonathan 	}
    404       1.1  jonathan 
    405       1.1  jonathan 	if (cse->thash) {
    406       1.1  jonathan 		crda = crp->crp_desc;
    407       1.1  jonathan 		if (cse->txform)
    408       1.1  jonathan 			crde = crda->crd_next;
    409       1.1  jonathan 	} else {
    410       1.1  jonathan 		if (cse->txform)
    411       1.1  jonathan 			crde = crp->crp_desc;
    412       1.1  jonathan 		else {
    413       1.1  jonathan 			error = EINVAL;
    414       1.1  jonathan 			goto bail;
    415       1.1  jonathan 		}
    416       1.1  jonathan 	}
    417       1.1  jonathan 
    418       1.1  jonathan 	if ((error = copyin(cop->src, cse->uio.uio_iov[0].iov_base, cop->len)))
    419       1.1  jonathan 		goto bail;
    420       1.1  jonathan 
    421       1.1  jonathan 	if (crda) {
    422       1.1  jonathan 		crda->crd_skip = 0;
    423       1.1  jonathan 		crda->crd_len = cop->len;
    424       1.1  jonathan 		crda->crd_inject = 0;	/* ??? */
    425       1.1  jonathan 
    426       1.1  jonathan 		crda->crd_alg = cse->mac;
    427       1.1  jonathan 		crda->crd_key = cse->mackey;
    428       1.1  jonathan 		crda->crd_klen = cse->mackeylen * 8;
    429       1.1  jonathan 	}
    430       1.1  jonathan 
    431       1.1  jonathan 	if (crde) {
    432       1.1  jonathan 		if (cop->op == COP_ENCRYPT)
    433       1.1  jonathan 			crde->crd_flags |= CRD_F_ENCRYPT;
    434       1.1  jonathan 		else
    435       1.1  jonathan 			crde->crd_flags &= ~CRD_F_ENCRYPT;
    436       1.1  jonathan 		crde->crd_len = cop->len;
    437       1.1  jonathan 		crde->crd_inject = 0;
    438       1.1  jonathan 
    439       1.1  jonathan 		crde->crd_alg = cse->cipher;
    440       1.1  jonathan 		crde->crd_key = cse->key;
    441       1.1  jonathan 		crde->crd_klen = cse->keylen * 8;
    442       1.1  jonathan 	}
    443       1.1  jonathan 
    444       1.1  jonathan 	crp->crp_ilen = cop->len;
    445       1.1  jonathan 	crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM
    446       1.1  jonathan 		       | (cop->flags & COP_F_BATCH);
    447      1.26  christos 	crp->crp_buf = (void *)&cse->uio;
    448       1.1  jonathan 	crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_cb;
    449       1.1  jonathan 	crp->crp_sid = cse->sid;
    450       1.1  jonathan 	crp->crp_opaque = (void *)cse;
    451       1.1  jonathan 
    452       1.1  jonathan 	if (cop->iv) {
    453       1.1  jonathan 		if (crde == NULL) {
    454       1.1  jonathan 			error = EINVAL;
    455       1.1  jonathan 			goto bail;
    456       1.1  jonathan 		}
    457       1.1  jonathan 		if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
    458       1.1  jonathan 			error = EINVAL;
    459       1.1  jonathan 			goto bail;
    460       1.1  jonathan 		}
    461       1.1  jonathan 		if ((error = copyin(cop->iv, cse->tmp_iv, cse->txform->blocksize)))
    462       1.1  jonathan 			goto bail;
    463       1.1  jonathan 		bcopy(cse->tmp_iv, crde->crd_iv, cse->txform->blocksize);
    464       1.1  jonathan 		crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
    465       1.1  jonathan 		crde->crd_skip = 0;
    466       1.1  jonathan 	} else if (crde) {
    467      1.20  christos 		if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
    468      1.20  christos 			crde->crd_skip = 0;
    469      1.20  christos 		} else {
    470      1.20  christos 			crde->crd_flags |= CRD_F_IV_PRESENT;
    471      1.20  christos 			crde->crd_skip = cse->txform->blocksize;
    472      1.20  christos 			crde->crd_len -= cse->txform->blocksize;
    473      1.20  christos 		}
    474       1.1  jonathan 	}
    475       1.1  jonathan 
    476       1.1  jonathan 	if (cop->mac) {
    477       1.1  jonathan 		if (crda == NULL) {
    478       1.1  jonathan 			error = EINVAL;
    479       1.1  jonathan 			goto bail;
    480       1.1  jonathan 		}
    481       1.1  jonathan 		crp->crp_mac=cse->tmp_mac;
    482       1.1  jonathan 	}
    483       1.1  jonathan 
    484      1.33       tls 	/*
    485      1.33       tls 	 * XXX there was a comment here which said that we went to
    486      1.33       tls 	 * XXX splcrypto() but needed to only if CRYPTO_F_CBIMM,
    487      1.33       tls 	 * XXX disabled on NetBSD since 1.6O due to a race condition.
    488      1.33       tls 	 * XXX But crypto_dispatch went to splcrypto() itself!  (And
    489      1.33       tls 	 * XXX now takes the crypto_mtx mutex itself).  We do, however,
    490      1.33       tls 	 * XXX need to hold the mutex across the call to cv_wait().
    491      1.33       tls 	 * XXX     (should we arrange for crypto_dispatch to return to
    492      1.33       tls 	 * XXX      us with it held?  it seems quite ugly to do so.)
    493      1.33       tls 	 */
    494       1.1  jonathan 	error = crypto_dispatch(crp);
    495      1.33       tls 	mutex_spin_enter(&crypto_mtx);
    496      1.33       tls 	if (error != 0) {
    497      1.33       tls 		DPRINTF(("cryptodev_op: not waiting, error.\n"));
    498      1.33       tls 		mutex_spin_exit(&crypto_mtx);
    499       1.1  jonathan 		goto bail;
    500       1.1  jonathan 	}
    501      1.33       tls 	while (!(crp->crp_flags & CRYPTO_F_DONE)) {
    502      1.33       tls 		DPRINTF(("cryptodev_op: sleeping on cv %08x for crp %08x\n", \
    503      1.33       tls 			(uint32_t)&crp->crp_cv, (uint32_t)crp));
    504      1.33       tls 		cv_wait(&crp->crp_cv, &crypto_mtx);	/* XXX cv_wait_sig? */
    505      1.33       tls 	}
    506      1.33       tls 	if (crp->crp_flags & CRYPTO_F_ONRETQ) {
    507      1.33       tls 		DPRINTF(("cryptodev_op: DONE, not woken by cryptoret.\n"));
    508      1.33       tls 		(void)crypto_ret_q_remove(crp);
    509      1.33       tls 	}
    510      1.33       tls 	mutex_spin_exit(&crypto_mtx);
    511       1.1  jonathan 
    512       1.1  jonathan 	if (crp->crp_etype != 0) {
    513      1.34       tls 		DPRINTF(("cryptodev_op: crp_etype %d\n", crp->crp_etype));
    514       1.1  jonathan 		error = crp->crp_etype;
    515       1.1  jonathan 		goto bail;
    516       1.1  jonathan 	}
    517       1.1  jonathan 
    518       1.1  jonathan 	if (cse->error) {
    519      1.34       tls 		DPRINTF(("cryptodev_op: cse->error %d\n", cse->error));
    520       1.1  jonathan 		error = cse->error;
    521       1.1  jonathan 		goto bail;
    522       1.1  jonathan 	}
    523       1.1  jonathan 
    524       1.1  jonathan 	if (cop->dst &&
    525      1.34       tls 	    (error = copyout(cse->uio.uio_iov[0].iov_base, cop->dst, cop->len))) {
    526      1.34       tls 		DPRINTF(("cryptodev_op: copyout error %d\n", error));
    527       1.1  jonathan 		goto bail;
    528      1.34       tls 	}
    529       1.1  jonathan 
    530       1.1  jonathan 	if (cop->mac &&
    531      1.34       tls 	    (error = copyout(crp->crp_mac, cop->mac, cse->thash->authsize))) {
    532      1.34       tls 		DPRINTF(("cryptodev_op: mac copyout error %d\n", error));
    533       1.1  jonathan 		goto bail;
    534      1.34       tls 	}
    535       1.1  jonathan 
    536       1.1  jonathan bail:
    537       1.1  jonathan 	if (crp)
    538       1.1  jonathan 		crypto_freereq(crp);
    539       1.1  jonathan 	if (cse->uio.uio_iov[0].iov_base)
    540       1.1  jonathan 		free(cse->uio.uio_iov[0].iov_base, M_XDATA);
    541       1.1  jonathan 
    542       1.1  jonathan 	return (error);
    543       1.1  jonathan }
    544       1.1  jonathan 
    545       1.1  jonathan static int
    546       1.1  jonathan cryptodev_cb(void *op)
    547       1.1  jonathan {
    548       1.1  jonathan 	struct cryptop *crp = (struct cryptop *) op;
    549       1.1  jonathan 	struct csession *cse = (struct csession *)crp->crp_opaque;
    550      1.33       tls 	int error = 0;
    551       1.1  jonathan 
    552      1.33       tls 	mutex_spin_enter(&crypto_mtx);
    553       1.1  jonathan 	cse->error = crp->crp_etype;
    554      1.33       tls 	if (crp->crp_etype == EAGAIN) {
    555      1.33       tls 		/* always drop mutex to call dispatch routine */
    556      1.33       tls 		mutex_spin_exit(&crypto_mtx);
    557      1.33       tls 		error = crypto_dispatch(crp);
    558      1.33       tls 		mutex_spin_enter(&crypto_mtx);
    559      1.33       tls 	}
    560      1.33       tls 	if (error != 0 || (crp->crp_flags & CRYPTO_F_DONE)) {
    561      1.33       tls 		cv_signal(&crp->crp_cv);
    562      1.33       tls 	}
    563      1.33       tls 	mutex_spin_exit(&crypto_mtx);
    564       1.1  jonathan 	return (0);
    565       1.1  jonathan }
    566       1.1  jonathan 
    567       1.1  jonathan static int
    568       1.1  jonathan cryptodevkey_cb(void *op)
    569       1.1  jonathan {
    570       1.1  jonathan 	struct cryptkop *krp = (struct cryptkop *) op;
    571       1.1  jonathan 
    572      1.33       tls 	mutex_spin_enter(&crypto_mtx);
    573      1.33       tls 	cv_signal(&krp->krp_cv);
    574      1.33       tls 	mutex_spin_exit(&crypto_mtx);
    575       1.1  jonathan 	return (0);
    576       1.1  jonathan }
    577       1.1  jonathan 
    578       1.1  jonathan static int
    579       1.1  jonathan cryptodev_key(struct crypt_kop *kop)
    580       1.1  jonathan {
    581       1.1  jonathan 	struct cryptkop *krp = NULL;
    582       1.1  jonathan 	int error = EINVAL;
    583       1.1  jonathan 	int in, out, size, i;
    584       1.1  jonathan 
    585       1.1  jonathan 	if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
    586       1.1  jonathan 		return (EFBIG);
    587       1.1  jonathan 	}
    588       1.1  jonathan 
    589       1.1  jonathan 	in = kop->crk_iparams;
    590       1.1  jonathan 	out = kop->crk_oparams;
    591       1.1  jonathan 	switch (kop->crk_op) {
    592       1.1  jonathan 	case CRK_MOD_EXP:
    593       1.1  jonathan 		if (in == 3 && out == 1)
    594       1.1  jonathan 			break;
    595       1.1  jonathan 		return (EINVAL);
    596       1.1  jonathan 	case CRK_MOD_EXP_CRT:
    597       1.1  jonathan 		if (in == 6 && out == 1)
    598       1.1  jonathan 			break;
    599       1.1  jonathan 		return (EINVAL);
    600       1.1  jonathan 	case CRK_DSA_SIGN:
    601       1.1  jonathan 		if (in == 5 && out == 2)
    602       1.1  jonathan 			break;
    603       1.1  jonathan 		return (EINVAL);
    604       1.1  jonathan 	case CRK_DSA_VERIFY:
    605       1.1  jonathan 		if (in == 7 && out == 0)
    606       1.1  jonathan 			break;
    607       1.1  jonathan 		return (EINVAL);
    608       1.1  jonathan 	case CRK_DH_COMPUTE_KEY:
    609       1.1  jonathan 		if (in == 3 && out == 1)
    610       1.1  jonathan 			break;
    611       1.1  jonathan 		return (EINVAL);
    612      1.27       tls 	case CRK_MOD_ADD:
    613      1.27       tls 		if (in == 3 && out == 1)
    614      1.27       tls 			break;
    615      1.27       tls 		return (EINVAL);
    616      1.27       tls 	case CRK_MOD_ADDINV:
    617      1.27       tls 		if (in == 2 && out == 1)
    618      1.27       tls 			break;
    619      1.27       tls 		return (EINVAL);
    620      1.27       tls 	case CRK_MOD_SUB:
    621      1.27       tls 		if (in == 3 && out == 1)
    622      1.27       tls 			break;
    623      1.27       tls 		return (EINVAL);
    624      1.27       tls 	case CRK_MOD_MULT:
    625      1.27       tls 		if (in == 3 && out == 1)
    626      1.27       tls 			break;
    627      1.27       tls 		return (EINVAL);
    628      1.27       tls 	case CRK_MOD_MULTINV:
    629      1.27       tls 		if (in == 2 && out == 1)
    630      1.27       tls 			break;
    631      1.27       tls 		return (EINVAL);
    632      1.27       tls 	case CRK_MOD:
    633      1.27       tls 		if (in == 2 && out == 1)
    634      1.27       tls 			break;
    635      1.27       tls 		return (EINVAL);
    636       1.1  jonathan 	default:
    637       1.1  jonathan 		return (EINVAL);
    638       1.1  jonathan 	}
    639       1.1  jonathan 
    640      1.33       tls 	krp = pool_get(&cryptkop_pool, PR_WAITOK);
    641       1.1  jonathan 	if (!krp)
    642       1.1  jonathan 		return (ENOMEM);
    643       1.1  jonathan 	bzero(krp, sizeof *krp);
    644      1.33       tls 	cv_init(&krp->krp_cv, "crykdev");
    645       1.1  jonathan 	krp->krp_op = kop->crk_op;
    646       1.1  jonathan 	krp->krp_status = kop->crk_status;
    647       1.1  jonathan 	krp->krp_iparams = kop->crk_iparams;
    648       1.1  jonathan 	krp->krp_oparams = kop->crk_oparams;
    649       1.1  jonathan 	krp->krp_status = 0;
    650       1.1  jonathan 	krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb;
    651       1.1  jonathan 
    652       1.1  jonathan 	for (i = 0; i < CRK_MAXPARAM; i++)
    653       1.1  jonathan 		krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
    654       1.1  jonathan 	for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
    655       1.1  jonathan 		size = (krp->krp_param[i].crp_nbits + 7) / 8;
    656       1.1  jonathan 		if (size == 0)
    657       1.1  jonathan 			continue;
    658      1.19  christos 		krp->krp_param[i].crp_p = malloc(size, M_XDATA, M_WAITOK);
    659       1.1  jonathan 		if (i >= krp->krp_iparams)
    660       1.1  jonathan 			continue;
    661       1.1  jonathan 		error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size);
    662       1.1  jonathan 		if (error)
    663       1.1  jonathan 			goto fail;
    664       1.1  jonathan 	}
    665       1.1  jonathan 
    666       1.1  jonathan 	error = crypto_kdispatch(krp);
    667      1.33       tls 	if (error != 0) {
    668       1.1  jonathan 		goto fail;
    669      1.33       tls 	}
    670      1.33       tls 
    671      1.33       tls 	mutex_spin_enter(&crypto_mtx);
    672      1.33       tls 	while (!(krp->krp_flags & CRYPTO_F_DONE)) {
    673      1.33       tls 		cv_wait(&krp->krp_cv, &crypto_mtx);	/* XXX cv_wait_sig? */
    674      1.33       tls 	}
    675      1.33       tls 	if (krp->krp_flags & CRYPTO_F_ONRETQ) {
    676      1.33       tls 		DPRINTF(("cryptodev_key: DONE early, not via cryptoret.\n"));
    677      1.33       tls 		(void)crypto_ret_kq_remove(krp);
    678      1.33       tls 	}
    679      1.33       tls 	mutex_spin_exit(&crypto_mtx);
    680       1.1  jonathan 
    681       1.1  jonathan 	if (krp->krp_status != 0) {
    682      1.34       tls 		DPRINTF(("cryptodev_key: krp->krp_status 0x%08x\n", krp->krp_status));
    683       1.1  jonathan 		error = krp->krp_status;
    684       1.1  jonathan 		goto fail;
    685       1.1  jonathan 	}
    686       1.1  jonathan 
    687       1.1  jonathan 	for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
    688       1.1  jonathan 		size = (krp->krp_param[i].crp_nbits + 7) / 8;
    689       1.1  jonathan 		if (size == 0)
    690       1.1  jonathan 			continue;
    691       1.1  jonathan 		error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size);
    692      1.34       tls 		if (error) {
    693      1.34       tls 			DPRINTF(("cryptodev_key: copyout oparam %d failed, error=%d\n", i-krp->krp_iparams, error));
    694       1.1  jonathan 			goto fail;
    695      1.34       tls 		}
    696       1.1  jonathan 	}
    697       1.1  jonathan 
    698       1.1  jonathan fail:
    699       1.1  jonathan 	if (krp) {
    700       1.1  jonathan 		kop->crk_status = krp->krp_status;
    701       1.1  jonathan 		for (i = 0; i < CRK_MAXPARAM; i++) {
    702       1.1  jonathan 			if (krp->krp_param[i].crp_p)
    703       1.1  jonathan 				FREE(krp->krp_param[i].crp_p, M_XDATA);
    704       1.1  jonathan 		}
    705      1.33       tls 		pool_put(&cryptkop_pool, krp);
    706       1.1  jonathan 	}
    707      1.34       tls 	DPRINTF(("cryptodev_key: error=0x%08x\n", error));
    708       1.1  jonathan 	return (error);
    709       1.1  jonathan }
    710       1.1  jonathan 
    711       1.1  jonathan /* ARGSUSED */
    712       1.1  jonathan static int
    713  1.34.6.1       mjf cryptof_close(file_t *fp)
    714       1.1  jonathan {
    715  1.34.6.1       mjf 	struct fcrypt *fcr = fp->f_data;
    716       1.1  jonathan 	struct csession *cse;
    717       1.1  jonathan 
    718      1.33       tls 	mutex_spin_enter(&crypto_mtx);
    719       1.1  jonathan 	while ((cse = TAILQ_FIRST(&fcr->csessions))) {
    720       1.1  jonathan 		TAILQ_REMOVE(&fcr->csessions, cse, next);
    721       1.1  jonathan 		(void)csefree(cse);
    722       1.1  jonathan 	}
    723      1.29       tls 	pool_put(&fcrpl, fcr);
    724       1.1  jonathan 
    725       1.1  jonathan 	fp->f_data = NULL;
    726      1.33       tls 	mutex_spin_exit(&crypto_mtx);
    727       1.1  jonathan 
    728       1.1  jonathan 	return 0;
    729       1.1  jonathan }
    730       1.1  jonathan 
    731      1.33       tls /* csefind: call with crypto_mtx held. */
    732       1.1  jonathan static struct csession *
    733       1.1  jonathan csefind(struct fcrypt *fcr, u_int ses)
    734       1.1  jonathan {
    735      1.33       tls 	struct csession *cse, *ret = NULL;
    736       1.1  jonathan 
    737      1.33       tls 	KASSERT(mutex_owned(&crypto_mtx));
    738       1.1  jonathan 	TAILQ_FOREACH(cse, &fcr->csessions, next)
    739       1.1  jonathan 		if (cse->ses == ses)
    740      1.33       tls 			ret = cse;
    741      1.33       tls 	return (ret);
    742       1.1  jonathan }
    743       1.1  jonathan 
    744      1.33       tls /* csedelete: call with crypto_mtx held. */
    745       1.1  jonathan static int
    746       1.1  jonathan csedelete(struct fcrypt *fcr, struct csession *cse_del)
    747       1.1  jonathan {
    748       1.1  jonathan 	struct csession *cse;
    749      1.33       tls 	int ret = 0;
    750       1.1  jonathan 
    751      1.33       tls 	KASSERT(mutex_owned(&crypto_mtx));
    752       1.1  jonathan 	TAILQ_FOREACH(cse, &fcr->csessions, next) {
    753       1.1  jonathan 		if (cse == cse_del) {
    754       1.1  jonathan 			TAILQ_REMOVE(&fcr->csessions, cse, next);
    755      1.33       tls 			ret = 1;
    756       1.1  jonathan 		}
    757       1.1  jonathan 	}
    758      1.33       tls 	return (ret);
    759       1.1  jonathan }
    760       1.1  jonathan 
    761      1.33       tls /* cseadd: call with crypto_mtx held. */
    762       1.1  jonathan static struct csession *
    763       1.1  jonathan cseadd(struct fcrypt *fcr, struct csession *cse)
    764       1.1  jonathan {
    765      1.33       tls 	KASSERT(mutex_owned(&crypto_mtx));
    766      1.33       tls 	/* don't let session ID wrap! */
    767      1.33       tls 	if (fcr->sesn + 1 == 0) return NULL;
    768       1.1  jonathan 	TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
    769       1.1  jonathan 	cse->ses = fcr->sesn++;
    770       1.1  jonathan 	return (cse);
    771       1.1  jonathan }
    772       1.1  jonathan 
    773      1.33       tls /* csecreate: call with crypto_mtx held. */
    774       1.1  jonathan static struct csession *
    775      1.26  christos csecreate(struct fcrypt *fcr, u_int64_t sid, void *key, u_int64_t keylen,
    776      1.26  christos     void *mackey, u_int64_t mackeylen, u_int32_t cipher, u_int32_t mac,
    777       1.1  jonathan     struct enc_xform *txform, struct auth_hash *thash)
    778       1.1  jonathan {
    779       1.1  jonathan 	struct csession *cse;
    780       1.1  jonathan 
    781      1.33       tls 	KASSERT(mutex_owned(&crypto_mtx));
    782      1.29       tls 	cse = pool_get(&csepl, PR_NOWAIT);
    783       1.1  jonathan 	if (cse == NULL)
    784       1.1  jonathan 		return NULL;
    785       1.1  jonathan 	cse->key = key;
    786       1.1  jonathan 	cse->keylen = keylen/8;
    787       1.1  jonathan 	cse->mackey = mackey;
    788       1.1  jonathan 	cse->mackeylen = mackeylen/8;
    789       1.1  jonathan 	cse->sid = sid;
    790       1.1  jonathan 	cse->cipher = cipher;
    791       1.1  jonathan 	cse->mac = mac;
    792       1.1  jonathan 	cse->txform = txform;
    793       1.1  jonathan 	cse->thash = thash;
    794      1.34       tls 	cse->error = 0;
    795      1.28       tls 	if (cseadd(fcr, cse))
    796      1.28       tls 		return (cse);
    797      1.28       tls 	else {
    798      1.33       tls 		pool_put(&csepl, cse);
    799      1.28       tls 		return NULL;
    800      1.28       tls 	}
    801       1.1  jonathan }
    802       1.1  jonathan 
    803      1.33       tls /* csefree: call with crypto_mtx held. */
    804       1.1  jonathan static int
    805       1.1  jonathan csefree(struct csession *cse)
    806       1.1  jonathan {
    807       1.1  jonathan 	int error;
    808       1.1  jonathan 
    809      1.33       tls 	KASSERT(mutex_owned(&crypto_mtx));
    810       1.1  jonathan 	error = crypto_freesession(cse->sid);
    811       1.1  jonathan 	if (cse->key)
    812       1.1  jonathan 		FREE(cse->key, M_XDATA);
    813       1.1  jonathan 	if (cse->mackey)
    814       1.1  jonathan 		FREE(cse->mackey, M_XDATA);
    815      1.29       tls 	pool_put(&csepl, cse);
    816       1.1  jonathan 	return (error);
    817       1.1  jonathan }
    818       1.1  jonathan 
    819       1.1  jonathan static int
    820      1.25  christos cryptoopen(dev_t dev, int flag, int mode,
    821      1.25  christos     struct lwp *l)
    822       1.1  jonathan {
    823  1.34.6.1       mjf 	file_t *fp;
    824      1.29       tls         struct fcrypt *fcr;
    825      1.29       tls         int fd, error;
    826      1.29       tls 
    827       1.1  jonathan 	if (crypto_usercrypto == 0)
    828       1.1  jonathan 		return (ENXIO);
    829      1.29       tls 
    830  1.34.6.1       mjf 	if ((error = fd_allocfile(&fp, &fd)) != 0)
    831      1.29       tls 		return error;
    832      1.29       tls 
    833      1.29       tls 	fcr = pool_get(&fcrpl, PR_WAITOK);
    834      1.33       tls 	mutex_spin_enter(&crypto_mtx);
    835      1.29       tls 	TAILQ_INIT(&fcr->csessions);
    836      1.29       tls 	/*
    837      1.29       tls 	 * Don't ever return session 0, to allow detection of
    838      1.29       tls 	 * failed creation attempts with multi-create ioctl.
    839      1.29       tls 	 */
    840      1.29       tls 	fcr->sesn = 1;
    841      1.33       tls 	mutex_spin_exit(&crypto_mtx);
    842  1.34.6.1       mjf 	return fd_clone(fp, fd, flag, &cryptofops, fcr);
    843       1.1  jonathan }
    844       1.1  jonathan 
    845       1.1  jonathan static int
    846      1.25  christos cryptoread(dev_t dev, struct uio *uio, int ioflag)
    847       1.1  jonathan {
    848       1.1  jonathan 	return (EIO);
    849       1.1  jonathan }
    850       1.1  jonathan 
    851       1.1  jonathan static int
    852      1.25  christos cryptowrite(dev_t dev, struct uio *uio, int ioflag)
    853       1.1  jonathan {
    854       1.1  jonathan 	return (EIO);
    855       1.1  jonathan }
    856       1.1  jonathan 
    857       1.1  jonathan int
    858      1.25  christos cryptoselect(dev_t dev, int rw, struct lwp *l)
    859       1.1  jonathan {
    860       1.1  jonathan 	return (0);
    861       1.1  jonathan }
    862       1.1  jonathan 
    863       1.1  jonathan /*static*/
    864       1.1  jonathan struct cdevsw crypto_cdevsw = {
    865       1.1  jonathan 	/* open */	cryptoopen,
    866      1.29       tls 	/* close */	noclose,
    867       1.1  jonathan 	/* read */	cryptoread,
    868       1.1  jonathan 	/* write */	cryptowrite,
    869      1.29       tls 	/* ioctl */	noioctl,
    870       1.1  jonathan 	/* ttstop?*/	nostop,
    871       1.1  jonathan 	/* ??*/		notty,
    872       1.1  jonathan 	/* poll */	cryptoselect /*nopoll*/,
    873       1.1  jonathan 	/* mmap */	nommap,
    874       1.1  jonathan 	/* kqfilter */	nokqfilter,
    875      1.23  christos 	/* type */	D_OTHER,
    876       1.1  jonathan };
    877       1.1  jonathan 
    878      1.15   thorpej /*
    879      1.15   thorpej  * Pseudo-device initialization routine for /dev/crypto
    880      1.15   thorpej  */
    881      1.15   thorpej void	cryptoattach(int);
    882      1.15   thorpej 
    883      1.15   thorpej void
    884      1.25  christos cryptoattach(int num)
    885      1.15   thorpej {
    886      1.29       tls 	pool_init(&fcrpl, sizeof(struct fcrypt), 0, 0, 0, "fcrpl",
    887      1.29       tls 		  NULL, IPL_NET);	/* XXX IPL_NET ("splcrypto") */
    888      1.29       tls 	pool_init(&csepl, sizeof(struct csession), 0, 0, 0, "csepl",
    889      1.29       tls 		  NULL, IPL_NET);	/* XXX IPL_NET ("splcrypto") */
    890      1.29       tls 
    891      1.29       tls 	/*
    892      1.29       tls 	 * Preallocate space for 64 users, with 5 sessions each.
    893      1.29       tls 	 * (consider that a TLS protocol session requires at least
    894      1.29       tls 	 * 3DES, MD5, and SHA1 (both hashes are used in the PRF) for
    895      1.29       tls 	 * the negotiation, plus HMAC_SHA1 for the actual SSL records,
    896      1.29       tls 	 * consuming one session here for each algorithm.
    897      1.29       tls 	 */
    898      1.29       tls 	pool_prime(&fcrpl, 64);
    899      1.29       tls 	pool_prime(&csepl, 64 * 5);
    900      1.15   thorpej }
    901