Home | History | Annotate | Line # | Download | only in opencrypto
cryptodev.c revision 1.41.2.1
      1  1.41.2.1  wrstuden /*	$NetBSD: cryptodev.c,v 1.41.2.1 2008/06/23 04:32:02 wrstuden 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.36       tls /*-
      6      1.36       tls  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      7      1.36       tls  * All rights reserved.
      8      1.36       tls  *
      9      1.36       tls  * This code is derived from software contributed to The NetBSD Foundation
     10      1.36       tls  * by Coyote Point Systems, Inc.
     11      1.36       tls  *
     12      1.36       tls  * Redistribution and use in source and binary forms, with or without
     13      1.36       tls  * modification, are permitted provided that the following conditions
     14      1.36       tls  * are met:
     15      1.36       tls  * 1. Redistributions of source code must retain the above copyright
     16      1.36       tls  *    notice, this list of conditions and the following disclaimer.
     17      1.36       tls  * 2. Redistributions in binary form must reproduce the above copyright
     18      1.36       tls  *    notice, this list of conditions and the following disclaimer in the
     19      1.36       tls  *    documentation and/or other materials provided with the distribution.
     20      1.36       tls  *
     21      1.36       tls  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     22      1.36       tls  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23      1.36       tls  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24      1.36       tls  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     25      1.36       tls  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26      1.36       tls  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27      1.36       tls  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28      1.36       tls  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29      1.36       tls  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30      1.36       tls  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31      1.36       tls  * POSSIBILITY OF SUCH DAMAGE.
     32      1.36       tls  */
     33      1.36       tls 
     34       1.1  jonathan /*
     35       1.1  jonathan  * Copyright (c) 2001 Theo de Raadt
     36       1.1  jonathan  *
     37       1.1  jonathan  * Redistribution and use in source and binary forms, with or without
     38       1.1  jonathan  * modification, are permitted provided that the following conditions
     39       1.1  jonathan  * are met:
     40       1.1  jonathan  *
     41       1.1  jonathan  * 1. Redistributions of source code must retain the above copyright
     42       1.1  jonathan  *   notice, this list of conditions and the following disclaimer.
     43       1.1  jonathan  * 2. Redistributions in binary form must reproduce the above copyright
     44       1.1  jonathan  *   notice, this list of conditions and the following disclaimer in the
     45       1.1  jonathan  *   documentation and/or other materials provided with the distribution.
     46       1.1  jonathan  * 3. The name of the author may not be used to endorse or promote products
     47       1.1  jonathan  *   derived from this software without specific prior written permission.
     48       1.1  jonathan  *
     49       1.1  jonathan  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     50       1.1  jonathan  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     51       1.1  jonathan  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     52       1.1  jonathan  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     53       1.1  jonathan  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     54       1.1  jonathan  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     55       1.1  jonathan  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     56       1.1  jonathan  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     57       1.1  jonathan  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     58       1.1  jonathan  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     59       1.1  jonathan  *
     60       1.1  jonathan  * Effort sponsored in part by the Defense Advanced Research Projects
     61       1.1  jonathan  * Agency (DARPA) and Air Force Research Laboratory, Air Force
     62       1.1  jonathan  * Materiel Command, USAF, under agreement number F30602-01-2-0537.
     63       1.1  jonathan  *
     64       1.1  jonathan  */
     65       1.1  jonathan 
     66       1.1  jonathan #include <sys/cdefs.h>
     67  1.41.2.1  wrstuden __KERNEL_RCSID(0, "$NetBSD: cryptodev.c,v 1.41.2.1 2008/06/23 04:32:02 wrstuden Exp $");
     68       1.1  jonathan 
     69       1.1  jonathan #include <sys/param.h>
     70       1.1  jonathan #include <sys/systm.h>
     71      1.36       tls #include <sys/kmem.h>
     72       1.1  jonathan #include <sys/malloc.h>
     73       1.1  jonathan #include <sys/mbuf.h>
     74      1.29       tls #include <sys/pool.h>
     75       1.1  jonathan #include <sys/sysctl.h>
     76       1.1  jonathan #include <sys/file.h>
     77       1.1  jonathan #include <sys/filedesc.h>
     78       1.1  jonathan #include <sys/errno.h>
     79       1.2  jonathan #include <sys/md5.h>
     80       1.1  jonathan #include <sys/sha1.h>
     81       1.1  jonathan #include <sys/conf.h>
     82       1.1  jonathan #include <sys/device.h>
     83      1.21      elad #include <sys/kauth.h>
     84      1.36       tls #include <sys/select.h>
     85      1.36       tls #include <sys/poll.h>
     86      1.37    dogcow #include <sys/atomic.h>
     87       1.1  jonathan 
     88      1.33       tls #include "opt_ocf.h"
     89       1.1  jonathan #include <opencrypto/cryptodev.h>
     90       1.1  jonathan #include <opencrypto/xform.h>
     91       1.1  jonathan 
     92       1.1  jonathan struct csession {
     93       1.1  jonathan 	TAILQ_ENTRY(csession) next;
     94       1.1  jonathan 	u_int64_t	sid;
     95       1.1  jonathan 	u_int32_t	ses;
     96       1.1  jonathan 
     97       1.1  jonathan 	u_int32_t	cipher;
     98       1.1  jonathan 	struct enc_xform *txform;
     99       1.1  jonathan 	u_int32_t	mac;
    100       1.1  jonathan 	struct auth_hash *thash;
    101       1.1  jonathan 
    102      1.26  christos 	void *		key;
    103       1.1  jonathan 	int		keylen;
    104       1.1  jonathan 	u_char		tmp_iv[EALG_MAX_BLOCK_LEN];
    105       1.1  jonathan 
    106      1.26  christos 	void *		mackey;
    107       1.1  jonathan 	int		mackeylen;
    108       1.1  jonathan 	u_char		tmp_mac[CRYPTO_MAX_MAC_LEN];
    109       1.1  jonathan 
    110      1.29       tls 	struct iovec	iovec[1];	/* user requests never have more */
    111       1.1  jonathan 	struct uio	uio;
    112       1.1  jonathan 	int		error;
    113       1.1  jonathan };
    114       1.1  jonathan 
    115       1.1  jonathan struct fcrypt {
    116       1.1  jonathan 	TAILQ_HEAD(csessionlist, csession) csessions;
    117      1.36       tls 	TAILQ_HEAD(crprethead, cryptop) crp_ret_mq;
    118      1.36       tls 	TAILQ_HEAD(krprethead, cryptkop) crp_ret_mkq;
    119       1.1  jonathan 	int		sesn;
    120      1.36       tls 	struct selinfo	sinfo;
    121      1.36       tls 	u_int32_t	requestid;
    122       1.1  jonathan };
    123       1.1  jonathan 
    124      1.29       tls /* For our fixed-size allocations */
    125      1.38     rmind static struct pool fcrpl;
    126      1.38     rmind static struct pool csepl;
    127       1.1  jonathan 
    128       1.1  jonathan /* Declaration of master device (fd-cloning/ctxt-allocating) entrypoints */
    129      1.16  christos static int	cryptoopen(dev_t dev, int flag, int mode, struct lwp *l);
    130       1.1  jonathan static int	cryptoread(dev_t dev, struct uio *uio, int ioflag);
    131       1.1  jonathan static int	cryptowrite(dev_t dev, struct uio *uio, int ioflag);
    132      1.16  christos static int	cryptoselect(dev_t dev, int rw, struct lwp *l);
    133       1.1  jonathan 
    134       1.1  jonathan /* Declaration of cloned-device (per-ctxt) entrypoints */
    135      1.36       tls static int	cryptof_read(struct file *, off_t *, struct uio *,
    136  1.41.2.1  wrstuden     kauth_cred_t, int);
    137      1.36       tls static int	cryptof_write(struct file *, off_t *, struct uio *,
    138  1.41.2.1  wrstuden     kauth_cred_t, int);
    139      1.36       tls static int	cryptof_ioctl(struct file *, u_long, void *);
    140      1.36       tls static int	cryptof_close(struct file *);
    141      1.36       tls static int 	cryptof_poll(struct file *, int);
    142       1.1  jonathan 
    143      1.12  christos static const struct fileops cryptofops = {
    144  1.41.2.1  wrstuden 	cryptof_read,
    145  1.41.2.1  wrstuden 	cryptof_write,
    146  1.41.2.1  wrstuden 	cryptof_ioctl,
    147  1.41.2.1  wrstuden 	fnullop_fcntl,
    148  1.41.2.1  wrstuden 	cryptof_poll,
    149  1.41.2.1  wrstuden 	fbadop_stat,
    150  1.41.2.1  wrstuden 	cryptof_close,
    151  1.41.2.1  wrstuden 	fnullop_kqfilter
    152       1.1  jonathan };
    153       1.1  jonathan 
    154       1.1  jonathan static struct	csession *csefind(struct fcrypt *, u_int);
    155       1.1  jonathan static int	csedelete(struct fcrypt *, struct csession *);
    156       1.1  jonathan static struct	csession *cseadd(struct fcrypt *, struct csession *);
    157  1.41.2.1  wrstuden static struct	csession *csecreate(struct fcrypt *, u_int64_t, void *,
    158  1.41.2.1  wrstuden     u_int64_t, void *, u_int64_t, u_int32_t, u_int32_t, struct enc_xform *,
    159       1.1  jonathan     struct auth_hash *);
    160       1.1  jonathan static int	csefree(struct csession *);
    161       1.1  jonathan 
    162  1.41.2.1  wrstuden static int	cryptodev_op(struct csession *, struct crypt_op *,
    163  1.41.2.1  wrstuden     struct lwp *);
    164  1.41.2.1  wrstuden static int	cryptodev_mop(struct fcrypt *, struct crypt_n_op *, int,
    165  1.41.2.1  wrstuden     struct lwp *);
    166       1.1  jonathan static int	cryptodev_key(struct crypt_kop *);
    167      1.36       tls static int	cryptodev_mkey(struct fcrypt *, struct crypt_n_kop *, int);
    168      1.36       tls static int	cryptodev_session(struct fcrypt *, struct session_op *);
    169  1.41.2.1  wrstuden static int	cryptodev_msession(struct fcrypt *, struct session_n_op *, int);
    170      1.36       tls static int	cryptodev_msessionfin(struct fcrypt *, int, u_int32_t *);
    171       1.1  jonathan 
    172       1.1  jonathan static int	cryptodev_cb(void *);
    173       1.1  jonathan static int	cryptodevkey_cb(void *);
    174       1.1  jonathan 
    175      1.36       tls static int	cryptodev_mcb(void *);
    176      1.36       tls static int	cryptodevkey_mcb(void *);
    177      1.36       tls 
    178      1.36       tls static int 	cryptodev_getmstatus(struct fcrypt *, struct crypt_result *,
    179  1.41.2.1  wrstuden     int);
    180      1.36       tls static int	cryptodev_getstatus(struct fcrypt *, struct crypt_result *);
    181      1.36       tls 
    182       1.9  jonathan /*
    183       1.9  jonathan  * sysctl-able control variables for /dev/crypto now defined in crypto.c:
    184       1.9  jonathan  * crypto_usercrypto, crypto_userasmcrypto, crypto_devallowsoft.
    185       1.9  jonathan  */
    186       1.1  jonathan 
    187       1.1  jonathan /* ARGSUSED */
    188       1.1  jonathan int
    189      1.35        ad cryptof_read(file_t *fp, off_t *poff,
    190      1.25  christos     struct uio *uio, kauth_cred_t cred, int flags)
    191       1.1  jonathan {
    192  1.41.2.1  wrstuden 	return EIO;
    193       1.1  jonathan }
    194       1.1  jonathan 
    195       1.1  jonathan /* ARGSUSED */
    196       1.1  jonathan int
    197      1.35        ad cryptof_write(file_t *fp, off_t *poff,
    198      1.25  christos     struct uio *uio, kauth_cred_t cred, int flags)
    199       1.1  jonathan {
    200  1.41.2.1  wrstuden 	return EIO;
    201       1.1  jonathan }
    202       1.1  jonathan 
    203       1.1  jonathan /* ARGSUSED */
    204       1.1  jonathan int
    205      1.36       tls cryptof_ioctl(struct file *fp, u_long cmd, void *data)
    206       1.1  jonathan {
    207      1.35        ad 	struct fcrypt *fcr = fp->f_data;
    208       1.1  jonathan 	struct csession *cse;
    209       1.1  jonathan 	struct session_op *sop;
    210      1.36       tls 	struct session_n_op *snop;
    211       1.1  jonathan 	struct crypt_op *cop;
    212      1.36       tls 	struct crypt_mop *mop;
    213      1.36       tls 	struct crypt_mkop *mkop;
    214      1.36       tls 	struct crypt_n_op *cnop;
    215      1.36       tls 	struct crypt_n_kop *knop;
    216      1.36       tls 	struct crypt_sgop *sgop;
    217      1.36       tls 	struct crypt_sfop *sfop;
    218      1.36       tls 	struct cryptret *crypt_ret;
    219      1.36       tls 	struct crypt_result *crypt_res;
    220       1.1  jonathan 	u_int32_t ses;
    221      1.36       tls 	u_int32_t *sesid;
    222       1.1  jonathan 	int error = 0;
    223      1.36       tls 	size_t count;
    224       1.1  jonathan 
    225      1.29       tls 	/* backwards compatibility */
    226      1.35        ad         file_t *criofp;
    227      1.29       tls 	struct fcrypt *criofcr;
    228      1.29       tls 	int criofd;
    229      1.29       tls 
    230      1.36       tls 	switch (cmd) {
    231      1.29       tls         case CRIOGET:   /* XXX deprecated, remove after 5.0 */
    232      1.36       tls 		if ((error = fd_allocfile(&criofp, &criofd)) != 0)
    233      1.36       tls 			return error;
    234      1.36       tls 		criofcr = pool_get(&fcrpl, PR_WAITOK);
    235      1.33       tls 		mutex_spin_enter(&crypto_mtx);
    236      1.36       tls 		TAILQ_INIT(&criofcr->csessions);
    237      1.36       tls 		TAILQ_INIT(&criofcr->crp_ret_mq);
    238      1.36       tls 		TAILQ_INIT(&criofcr->crp_ret_mkq);
    239      1.36       tls 		selinit(&criofcr->sinfo);
    240      1.36       tls 
    241      1.29       tls                 /*
    242      1.29       tls                  * Don't ever return session 0, to allow detection of
    243      1.29       tls                  * failed creation attempts with multi-create ioctl.
    244      1.29       tls                  */
    245      1.36       tls 		criofcr->sesn = 1;
    246      1.36       tls 		criofcr->requestid = 1;
    247      1.33       tls 		mutex_spin_exit(&crypto_mtx);
    248      1.36       tls 		(void)fd_clone(criofp, criofd, (FREAD|FWRITE),
    249      1.29       tls 			      &cryptofops, criofcr);
    250      1.36       tls 		*(u_int32_t *)data = criofd;
    251      1.29       tls 		return error;
    252      1.36       tls 		break;
    253       1.1  jonathan 	case CIOCGSESSION:
    254       1.1  jonathan 		sop = (struct session_op *)data;
    255      1.36       tls 		error = cryptodev_session(fcr, sop);
    256      1.36       tls 		break;
    257      1.36       tls 	case CIOCNGSESSION:
    258      1.36       tls 		sgop = (struct crypt_sgop *)data;
    259      1.36       tls 		snop = kmem_alloc((sgop->count *
    260      1.36       tls 				  sizeof(struct session_n_op)), KM_SLEEP);
    261      1.36       tls 		error = copyin(sgop->sessions, snop, sgop->count *
    262      1.36       tls 			       sizeof(struct session_n_op));
    263      1.36       tls 		if (error) {
    264      1.36       tls 			goto mbail;
    265       1.1  jonathan 		}
    266       1.1  jonathan 
    267      1.36       tls 		error = cryptodev_msession(fcr, snop, sgop->count);
    268      1.36       tls 		if (error) {
    269      1.36       tls 			goto mbail;
    270       1.1  jonathan 		}
    271       1.1  jonathan 
    272      1.36       tls 		error = copyout(snop, sgop->sessions, sgop->count *
    273  1.41.2.1  wrstuden 		    sizeof(struct session_n_op));
    274      1.36       tls mbail:
    275      1.39       tls 		kmem_free(snop, sgop->count * sizeof(struct session_n_op));
    276       1.1  jonathan 		break;
    277       1.1  jonathan 	case CIOCFSESSION:
    278      1.33       tls 		mutex_spin_enter(&crypto_mtx);
    279       1.1  jonathan 		ses = *(u_int32_t *)data;
    280       1.1  jonathan 		cse = csefind(fcr, ses);
    281       1.1  jonathan 		if (cse == NULL)
    282  1.41.2.1  wrstuden 			return EINVAL;
    283       1.1  jonathan 		csedelete(fcr, cse);
    284       1.1  jonathan 		error = csefree(cse);
    285      1.33       tls 		mutex_spin_exit(&crypto_mtx);
    286       1.1  jonathan 		break;
    287      1.36       tls 	case CIOCNFSESSION:
    288      1.36       tls 		sfop = (struct crypt_sfop *)data;
    289      1.36       tls 		sesid = kmem_alloc((sfop->count * sizeof(u_int32_t)),
    290  1.41.2.1  wrstuden 		    KM_SLEEP);
    291  1.41.2.1  wrstuden 		error = copyin(sfop->sesid, sesid,
    292  1.41.2.1  wrstuden 		    (sfop->count * sizeof(u_int32_t)));
    293      1.36       tls 		if (!error) {
    294      1.36       tls 			error = cryptodev_msessionfin(fcr, sfop->count, sesid);
    295      1.36       tls 		}
    296      1.36       tls 		kmem_free(sesid, (sfop->count * sizeof(u_int32_t)));
    297      1.36       tls 		break;
    298       1.1  jonathan 	case CIOCCRYPT:
    299      1.33       tls 		mutex_spin_enter(&crypto_mtx);
    300       1.1  jonathan 		cop = (struct crypt_op *)data;
    301       1.1  jonathan 		cse = csefind(fcr, cop->ses);
    302      1.33       tls 		mutex_spin_exit(&crypto_mtx);
    303      1.18  christos 		if (cse == NULL) {
    304      1.18  christos 			DPRINTF(("csefind failed\n"));
    305  1.41.2.1  wrstuden 			return EINVAL;
    306      1.18  christos 		}
    307      1.35        ad 		error = cryptodev_op(cse, cop, curlwp);
    308      1.34       tls 		DPRINTF(("cryptodev_op error = %d\n", error));
    309       1.1  jonathan 		break;
    310      1.36       tls 	case CIOCNCRYPTM:
    311      1.36       tls 		mop = (struct crypt_mop *)data;
    312      1.36       tls 		cnop = kmem_alloc((mop->count * sizeof(struct crypt_n_op)),
    313  1.41.2.1  wrstuden 		    KM_SLEEP);
    314  1.41.2.1  wrstuden 		error = copyin(mop->reqs, cnop,
    315  1.41.2.1  wrstuden 		    (mop->count * sizeof(struct crypt_n_op)));
    316      1.36       tls 		if(!error) {
    317  1.41.2.1  wrstuden 			error = cryptodev_mop(fcr, cnop, mop->count, curlwp);
    318      1.36       tls 			if (!error) {
    319      1.36       tls 				error = copyout(cnop, mop->reqs,
    320  1.41.2.1  wrstuden 				    (mop->count * sizeof(struct crypt_n_op)));
    321      1.36       tls 			}
    322      1.36       tls 		}
    323      1.36       tls 		kmem_free(cnop, (mop->count * sizeof(struct crypt_n_op)));
    324      1.36       tls 		break;
    325       1.1  jonathan 	case CIOCKEY:
    326       1.1  jonathan 		error = cryptodev_key((struct crypt_kop *)data);
    327      1.34       tls 		DPRINTF(("cryptodev_key error = %d\n", error));
    328       1.1  jonathan 		break;
    329      1.36       tls 	case CIOCNFKEYM:
    330      1.36       tls 		mkop = (struct crypt_mkop *)data;
    331      1.36       tls 		knop = kmem_alloc((mkop->count * sizeof(struct crypt_n_kop)),
    332  1.41.2.1  wrstuden 		    KM_SLEEP);
    333      1.36       tls 		error = copyin(mkop->reqs, knop,
    334  1.41.2.1  wrstuden 		    (mkop->count * sizeof(struct crypt_n_kop)));
    335      1.36       tls 		if (!error) {
    336      1.36       tls 			error = cryptodev_mkey(fcr, knop, mkop->count);
    337      1.36       tls 			if (!error)
    338      1.36       tls 				error = copyout(knop, mkop->reqs,
    339  1.41.2.1  wrstuden 				    (mkop->count * sizeof(struct crypt_n_kop)));
    340      1.36       tls 		}
    341      1.36       tls 		kmem_free(knop, (mkop->count * sizeof(struct crypt_n_kop)));
    342      1.36       tls 		break;
    343       1.1  jonathan 	case CIOCASYMFEAT:
    344       1.1  jonathan 		error = crypto_getfeat((int *)data);
    345       1.1  jonathan 		break;
    346      1.36       tls 	case CIOCNCRYPTRETM:
    347      1.36       tls 		crypt_ret = (struct cryptret *)data;
    348      1.36       tls 		count = crypt_ret->count;
    349  1.41.2.1  wrstuden 		crypt_res = kmem_alloc((count * sizeof(struct crypt_result)),
    350  1.41.2.1  wrstuden 		    KM_SLEEP);
    351      1.36       tls 		error = copyin(crypt_ret->results, crypt_res,
    352  1.41.2.1  wrstuden 		    (count * sizeof(struct crypt_result)));
    353      1.36       tls 		if (error)
    354      1.36       tls 			goto reterr;
    355      1.36       tls 		crypt_ret->count = cryptodev_getmstatus(fcr, crypt_res,
    356  1.41.2.1  wrstuden 		    crypt_ret->count);
    357      1.36       tls 		/* sanity check count */
    358      1.36       tls 		if (crypt_ret->count > count) {
    359      1.37    dogcow 			printf("%s.%d: error returned count %zd > original "
    360  1.41.2.1  wrstuden 			    " count %zd\n",
    361  1.41.2.1  wrstuden 			    __FILE__, __LINE__, crypt_ret->count, count);
    362      1.36       tls 			crypt_ret->count = count;
    363      1.36       tls 
    364      1.36       tls 		}
    365      1.36       tls 		error = copyout(crypt_res, crypt_ret->results,
    366  1.41.2.1  wrstuden 		    (crypt_ret->count * sizeof(struct crypt_result)));
    367      1.36       tls reterr:
    368  1.41.2.1  wrstuden 		kmem_free(crypt_res, (count * sizeof(struct crypt_result)));
    369      1.36       tls 		break;
    370      1.36       tls 	case CIOCNCRYPTRET:
    371      1.36       tls 		error = cryptodev_getstatus(fcr, (struct crypt_result *)data);
    372      1.36       tls 		break;
    373       1.1  jonathan 	default:
    374      1.18  christos 		DPRINTF(("invalid ioctl cmd %ld\n", cmd));
    375       1.1  jonathan 		error = EINVAL;
    376       1.1  jonathan 	}
    377  1.41.2.1  wrstuden 	return error;
    378       1.1  jonathan }
    379       1.1  jonathan 
    380       1.1  jonathan static int
    381      1.25  christos cryptodev_op(struct csession *cse, struct crypt_op *cop, struct lwp *l)
    382       1.1  jonathan {
    383       1.1  jonathan 	struct cryptop *crp = NULL;
    384       1.1  jonathan 	struct cryptodesc *crde = NULL, *crda = NULL;
    385      1.33       tls 	int error;
    386       1.1  jonathan 
    387       1.1  jonathan 	if (cop->len > 256*1024-4)
    388  1.41.2.1  wrstuden 		return E2BIG;
    389       1.1  jonathan 
    390      1.13  jonathan 	if (cse->txform) {
    391      1.13  jonathan 		if (cop->len == 0 || (cop->len % cse->txform->blocksize) != 0)
    392  1.41.2.1  wrstuden 			return EINVAL;
    393      1.13  jonathan 	}
    394       1.1  jonathan 
    395  1.41.2.1  wrstuden 	(void)memset(&cse->uio, 0, sizeof(cse->uio));
    396       1.1  jonathan 	cse->uio.uio_iovcnt = 1;
    397       1.1  jonathan 	cse->uio.uio_resid = 0;
    398       1.1  jonathan 	cse->uio.uio_rw = UIO_WRITE;
    399       1.1  jonathan 	cse->uio.uio_iov = cse->iovec;
    400      1.17      yamt 	UIO_SETUP_SYSSPACE(&cse->uio);
    401      1.29       tls 	memset(&cse->iovec, 0, sizeof(cse->iovec));
    402       1.1  jonathan 	cse->uio.uio_iov[0].iov_len = cop->len;
    403      1.36       tls 	cse->uio.uio_iov[0].iov_base = kmem_alloc(cop->len, KM_SLEEP);
    404      1.29       tls 	cse->uio.uio_resid = cse->uio.uio_iov[0].iov_len;
    405       1.1  jonathan 
    406       1.1  jonathan 	crp = crypto_getreq((cse->txform != NULL) + (cse->thash != NULL));
    407       1.1  jonathan 	if (crp == NULL) {
    408       1.1  jonathan 		error = ENOMEM;
    409       1.1  jonathan 		goto bail;
    410       1.1  jonathan 	}
    411       1.1  jonathan 
    412       1.1  jonathan 	if (cse->thash) {
    413       1.1  jonathan 		crda = crp->crp_desc;
    414  1.41.2.1  wrstuden 		if (cse->txform && crda)
    415       1.1  jonathan 			crde = crda->crd_next;
    416       1.1  jonathan 	} else {
    417       1.1  jonathan 		if (cse->txform)
    418       1.1  jonathan 			crde = crp->crp_desc;
    419       1.1  jonathan 		else {
    420       1.1  jonathan 			error = EINVAL;
    421       1.1  jonathan 			goto bail;
    422       1.1  jonathan 		}
    423       1.1  jonathan 	}
    424       1.1  jonathan 
    425       1.1  jonathan 	if ((error = copyin(cop->src, cse->uio.uio_iov[0].iov_base, cop->len)))
    426      1.36       tls 	{
    427      1.36       tls 		printf("copyin failed %s %d \n", (char *)cop->src, error);
    428       1.1  jonathan 		goto bail;
    429      1.36       tls 	}
    430       1.1  jonathan 
    431       1.1  jonathan 	if (crda) {
    432       1.1  jonathan 		crda->crd_skip = 0;
    433       1.1  jonathan 		crda->crd_len = cop->len;
    434       1.1  jonathan 		crda->crd_inject = 0;	/* ??? */
    435       1.1  jonathan 
    436       1.1  jonathan 		crda->crd_alg = cse->mac;
    437       1.1  jonathan 		crda->crd_key = cse->mackey;
    438       1.1  jonathan 		crda->crd_klen = cse->mackeylen * 8;
    439       1.1  jonathan 	}
    440       1.1  jonathan 
    441       1.1  jonathan 	if (crde) {
    442       1.1  jonathan 		if (cop->op == COP_ENCRYPT)
    443       1.1  jonathan 			crde->crd_flags |= CRD_F_ENCRYPT;
    444       1.1  jonathan 		else
    445       1.1  jonathan 			crde->crd_flags &= ~CRD_F_ENCRYPT;
    446       1.1  jonathan 		crde->crd_len = cop->len;
    447       1.1  jonathan 		crde->crd_inject = 0;
    448       1.1  jonathan 
    449       1.1  jonathan 		crde->crd_alg = cse->cipher;
    450       1.1  jonathan 		crde->crd_key = cse->key;
    451       1.1  jonathan 		crde->crd_klen = cse->keylen * 8;
    452       1.1  jonathan 	}
    453       1.1  jonathan 
    454       1.1  jonathan 	crp->crp_ilen = cop->len;
    455      1.36       tls 	crp->crp_flags = CRYPTO_F_IOV | (cop->flags & COP_F_BATCH);
    456      1.26  christos 	crp->crp_buf = (void *)&cse->uio;
    457       1.1  jonathan 	crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_cb;
    458       1.1  jonathan 	crp->crp_sid = cse->sid;
    459       1.1  jonathan 	crp->crp_opaque = (void *)cse;
    460       1.1  jonathan 
    461       1.1  jonathan 	if (cop->iv) {
    462       1.1  jonathan 		if (crde == NULL) {
    463       1.1  jonathan 			error = EINVAL;
    464       1.1  jonathan 			goto bail;
    465       1.1  jonathan 		}
    466       1.1  jonathan 		if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
    467       1.1  jonathan 			error = EINVAL;
    468       1.1  jonathan 			goto bail;
    469       1.1  jonathan 		}
    470      1.36       tls 		if ((error = copyin(cop->iv, cse->tmp_iv,
    471  1.41.2.1  wrstuden 		    cse->txform->blocksize)))
    472       1.1  jonathan 			goto bail;
    473  1.41.2.1  wrstuden 		(void)memcpy(crde->crd_iv, cse->tmp_iv, cse->txform->blocksize);
    474       1.1  jonathan 		crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
    475       1.1  jonathan 		crde->crd_skip = 0;
    476       1.1  jonathan 	} else if (crde) {
    477      1.20  christos 		if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
    478      1.20  christos 			crde->crd_skip = 0;
    479      1.20  christos 		} else {
    480      1.20  christos 			crde->crd_flags |= CRD_F_IV_PRESENT;
    481      1.20  christos 			crde->crd_skip = cse->txform->blocksize;
    482      1.20  christos 			crde->crd_len -= cse->txform->blocksize;
    483      1.20  christos 		}
    484       1.1  jonathan 	}
    485       1.1  jonathan 
    486       1.1  jonathan 	if (cop->mac) {
    487       1.1  jonathan 		if (crda == NULL) {
    488       1.1  jonathan 			error = EINVAL;
    489       1.1  jonathan 			goto bail;
    490       1.1  jonathan 		}
    491       1.1  jonathan 		crp->crp_mac=cse->tmp_mac;
    492       1.1  jonathan 	}
    493       1.1  jonathan 
    494      1.33       tls 	/*
    495      1.33       tls 	 * XXX there was a comment here which said that we went to
    496      1.33       tls 	 * XXX splcrypto() but needed to only if CRYPTO_F_CBIMM,
    497      1.33       tls 	 * XXX disabled on NetBSD since 1.6O due to a race condition.
    498      1.33       tls 	 * XXX But crypto_dispatch went to splcrypto() itself!  (And
    499      1.33       tls 	 * XXX now takes the crypto_mtx mutex itself).  We do, however,
    500      1.33       tls 	 * XXX need to hold the mutex across the call to cv_wait().
    501      1.33       tls 	 * XXX     (should we arrange for crypto_dispatch to return to
    502      1.33       tls 	 * XXX      us with it held?  it seems quite ugly to do so.)
    503      1.33       tls 	 */
    504      1.36       tls #ifdef notyet
    505      1.36       tls eagain:
    506      1.36       tls #endif
    507       1.1  jonathan 	error = crypto_dispatch(crp);
    508      1.33       tls 	mutex_spin_enter(&crypto_mtx);
    509      1.36       tls 
    510      1.36       tls 	switch (error) {
    511      1.36       tls #ifdef notyet	/* don't loop forever -- but EAGAIN not possible here yet */
    512      1.36       tls 	case EAGAIN:
    513      1.36       tls 		mutex_spin_exit(&crypto_mtx);
    514      1.36       tls 		goto eagain;
    515      1.36       tls 		break;
    516      1.36       tls #endif
    517      1.36       tls 	case 0:
    518      1.36       tls 		break;
    519      1.36       tls 	default:
    520      1.33       tls 		DPRINTF(("cryptodev_op: not waiting, error.\n"));
    521      1.33       tls 		mutex_spin_exit(&crypto_mtx);
    522       1.1  jonathan 		goto bail;
    523       1.1  jonathan 	}
    524      1.36       tls 
    525      1.33       tls 	while (!(crp->crp_flags & CRYPTO_F_DONE)) {
    526      1.33       tls 		DPRINTF(("cryptodev_op: sleeping on cv %08x for crp %08x\n", \
    527  1.41.2.1  wrstuden 		    (uint32_t)&crp->crp_cv, (uint32_t)crp));
    528      1.33       tls 		cv_wait(&crp->crp_cv, &crypto_mtx);	/* XXX cv_wait_sig? */
    529      1.33       tls 	}
    530      1.33       tls 	if (crp->crp_flags & CRYPTO_F_ONRETQ) {
    531      1.33       tls 		DPRINTF(("cryptodev_op: DONE, not woken by cryptoret.\n"));
    532      1.33       tls 		(void)crypto_ret_q_remove(crp);
    533      1.33       tls 	}
    534      1.33       tls 	mutex_spin_exit(&crypto_mtx);
    535       1.1  jonathan 
    536       1.1  jonathan 	if (crp->crp_etype != 0) {
    537      1.34       tls 		DPRINTF(("cryptodev_op: crp_etype %d\n", crp->crp_etype));
    538       1.1  jonathan 		error = crp->crp_etype;
    539       1.1  jonathan 		goto bail;
    540       1.1  jonathan 	}
    541       1.1  jonathan 
    542       1.1  jonathan 	if (cse->error) {
    543      1.34       tls 		DPRINTF(("cryptodev_op: cse->error %d\n", cse->error));
    544       1.1  jonathan 		error = cse->error;
    545       1.1  jonathan 		goto bail;
    546       1.1  jonathan 	}
    547       1.1  jonathan 
    548       1.1  jonathan 	if (cop->dst &&
    549      1.36       tls 	    (error = copyout(cse->uio.uio_iov[0].iov_base, cop->dst, cop->len)))
    550      1.36       tls 	{
    551      1.34       tls 		DPRINTF(("cryptodev_op: copyout error %d\n", error));
    552       1.1  jonathan 		goto bail;
    553      1.34       tls 	}
    554       1.1  jonathan 
    555       1.1  jonathan 	if (cop->mac &&
    556      1.34       tls 	    (error = copyout(crp->crp_mac, cop->mac, cse->thash->authsize))) {
    557      1.34       tls 		DPRINTF(("cryptodev_op: mac copyout error %d\n", error));
    558       1.1  jonathan 		goto bail;
    559      1.34       tls 	}
    560       1.1  jonathan 
    561       1.1  jonathan bail:
    562       1.1  jonathan 	if (crp)
    563       1.1  jonathan 		crypto_freereq(crp);
    564       1.1  jonathan 	if (cse->uio.uio_iov[0].iov_base)
    565      1.36       tls 		kmem_free(cse->uio.uio_iov[0].iov_base,
    566  1.41.2.1  wrstuden 		    cse->uio.uio_iov[0].iov_len);
    567       1.1  jonathan 
    568  1.41.2.1  wrstuden 	return error;
    569       1.1  jonathan }
    570       1.1  jonathan 
    571       1.1  jonathan static int
    572       1.1  jonathan cryptodev_cb(void *op)
    573       1.1  jonathan {
    574       1.1  jonathan 	struct cryptop *crp = (struct cryptop *) op;
    575       1.1  jonathan 	struct csession *cse = (struct csession *)crp->crp_opaque;
    576      1.33       tls 	int error = 0;
    577       1.1  jonathan 
    578      1.33       tls 	mutex_spin_enter(&crypto_mtx);
    579       1.1  jonathan 	cse->error = crp->crp_etype;
    580      1.33       tls 	if (crp->crp_etype == EAGAIN) {
    581      1.33       tls 		/* always drop mutex to call dispatch routine */
    582      1.33       tls 		mutex_spin_exit(&crypto_mtx);
    583      1.33       tls 		error = crypto_dispatch(crp);
    584      1.33       tls 		mutex_spin_enter(&crypto_mtx);
    585      1.33       tls 	}
    586      1.33       tls 	if (error != 0 || (crp->crp_flags & CRYPTO_F_DONE)) {
    587      1.33       tls 		cv_signal(&crp->crp_cv);
    588      1.33       tls 	}
    589      1.33       tls 	mutex_spin_exit(&crypto_mtx);
    590  1.41.2.1  wrstuden 	return 0;
    591       1.1  jonathan }
    592       1.1  jonathan 
    593       1.1  jonathan static int
    594      1.36       tls cryptodev_mcb(void *op)
    595      1.36       tls {
    596      1.36       tls 	struct cryptop *crp = (struct cryptop *) op;
    597      1.36       tls 	struct csession *cse = (struct csession *)crp->crp_opaque;
    598      1.36       tls 	int  error=0;
    599      1.36       tls 
    600      1.36       tls 	mutex_spin_enter(&crypto_mtx);
    601      1.36       tls 	cse->error = crp->crp_etype;
    602      1.36       tls 	if (crp->crp_etype == EAGAIN) {
    603      1.36       tls 		mutex_spin_exit(&crypto_mtx);
    604      1.36       tls 		error = crypto_dispatch(crp);
    605      1.36       tls 		mutex_spin_enter(&crypto_mtx);
    606      1.36       tls 	}
    607      1.36       tls 	if (error != 0 || (crp->crp_flags & CRYPTO_F_DONE)) {
    608      1.36       tls 		cv_signal(&crp->crp_cv);
    609      1.36       tls 	}
    610      1.36       tls 
    611      1.36       tls 	TAILQ_INSERT_TAIL(&crp->fcrp->crp_ret_mq, crp, crp_next);
    612      1.38     rmind 	selnotify(&crp->fcrp->sinfo, 0, 0);
    613      1.36       tls 	mutex_spin_exit(&crypto_mtx);
    614  1.41.2.1  wrstuden 	return 0;
    615      1.36       tls }
    616      1.36       tls 
    617      1.36       tls static int
    618       1.1  jonathan cryptodevkey_cb(void *op)
    619       1.1  jonathan {
    620  1.41.2.1  wrstuden 	struct cryptkop *krp = op;
    621      1.36       tls 
    622      1.36       tls 	mutex_spin_enter(&crypto_mtx);
    623      1.36       tls 	cv_signal(&krp->krp_cv);
    624      1.36       tls 	mutex_spin_exit(&crypto_mtx);
    625  1.41.2.1  wrstuden 	return 0;
    626      1.36       tls }
    627      1.36       tls 
    628      1.36       tls static int
    629      1.36       tls cryptodevkey_mcb(void *op)
    630      1.36       tls {
    631  1.41.2.1  wrstuden 	struct cryptkop *krp = op;
    632       1.1  jonathan 
    633      1.33       tls 	mutex_spin_enter(&crypto_mtx);
    634      1.33       tls 	cv_signal(&krp->krp_cv);
    635      1.36       tls 	TAILQ_INSERT_TAIL(&krp->fcrp->crp_ret_mkq, krp, krp_next);
    636      1.38     rmind 	selnotify(&krp->fcrp->sinfo, 0, 0);
    637      1.33       tls 	mutex_spin_exit(&crypto_mtx);
    638  1.41.2.1  wrstuden 	return 0;
    639       1.1  jonathan }
    640       1.1  jonathan 
    641       1.1  jonathan static int
    642       1.1  jonathan cryptodev_key(struct crypt_kop *kop)
    643       1.1  jonathan {
    644       1.1  jonathan 	struct cryptkop *krp = NULL;
    645       1.1  jonathan 	int error = EINVAL;
    646       1.1  jonathan 	int in, out, size, i;
    647       1.1  jonathan 
    648  1.41.2.1  wrstuden 	if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM)
    649  1.41.2.1  wrstuden 		return EFBIG;
    650       1.1  jonathan 
    651       1.1  jonathan 	in = kop->crk_iparams;
    652       1.1  jonathan 	out = kop->crk_oparams;
    653       1.1  jonathan 	switch (kop->crk_op) {
    654       1.1  jonathan 	case CRK_MOD_EXP:
    655       1.1  jonathan 		if (in == 3 && out == 1)
    656       1.1  jonathan 			break;
    657  1.41.2.1  wrstuden 		return EINVAL;
    658       1.1  jonathan 	case CRK_MOD_EXP_CRT:
    659       1.1  jonathan 		if (in == 6 && out == 1)
    660       1.1  jonathan 			break;
    661  1.41.2.1  wrstuden 		return EINVAL;
    662       1.1  jonathan 	case CRK_DSA_SIGN:
    663       1.1  jonathan 		if (in == 5 && out == 2)
    664       1.1  jonathan 			break;
    665  1.41.2.1  wrstuden 		return EINVAL;
    666       1.1  jonathan 	case CRK_DSA_VERIFY:
    667       1.1  jonathan 		if (in == 7 && out == 0)
    668       1.1  jonathan 			break;
    669  1.41.2.1  wrstuden 		return EINVAL;
    670       1.1  jonathan 	case CRK_DH_COMPUTE_KEY:
    671       1.1  jonathan 		if (in == 3 && out == 1)
    672       1.1  jonathan 			break;
    673  1.41.2.1  wrstuden 		return EINVAL;
    674      1.27       tls 	case CRK_MOD_ADD:
    675      1.27       tls 		if (in == 3 && out == 1)
    676      1.27       tls 			break;
    677  1.41.2.1  wrstuden 		return EINVAL;
    678      1.27       tls 	case CRK_MOD_ADDINV:
    679      1.27       tls 		if (in == 2 && out == 1)
    680      1.27       tls 			break;
    681  1.41.2.1  wrstuden 		return EINVAL;
    682      1.27       tls 	case CRK_MOD_SUB:
    683      1.27       tls 		if (in == 3 && out == 1)
    684      1.27       tls 			break;
    685  1.41.2.1  wrstuden 		return EINVAL;
    686      1.27       tls 	case CRK_MOD_MULT:
    687      1.27       tls 		if (in == 3 && out == 1)
    688      1.27       tls 			break;
    689  1.41.2.1  wrstuden 		return EINVAL;
    690      1.27       tls 	case CRK_MOD_MULTINV:
    691      1.27       tls 		if (in == 2 && out == 1)
    692      1.27       tls 			break;
    693  1.41.2.1  wrstuden 		return EINVAL;
    694      1.27       tls 	case CRK_MOD:
    695      1.27       tls 		if (in == 2 && out == 1)
    696      1.27       tls 			break;
    697  1.41.2.1  wrstuden 		return EINVAL;
    698       1.1  jonathan 	default:
    699  1.41.2.1  wrstuden 		return EINVAL;
    700       1.1  jonathan 	}
    701       1.1  jonathan 
    702      1.33       tls 	krp = pool_get(&cryptkop_pool, PR_WAITOK);
    703  1.41.2.1  wrstuden 	(void)memset(krp, 0, sizeof *krp);
    704      1.33       tls 	cv_init(&krp->krp_cv, "crykdev");
    705       1.1  jonathan 	krp->krp_op = kop->crk_op;
    706       1.1  jonathan 	krp->krp_status = kop->crk_status;
    707       1.1  jonathan 	krp->krp_iparams = kop->crk_iparams;
    708       1.1  jonathan 	krp->krp_oparams = kop->crk_oparams;
    709       1.1  jonathan 	krp->krp_status = 0;
    710       1.1  jonathan 	krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb;
    711       1.1  jonathan 
    712       1.1  jonathan 	for (i = 0; i < CRK_MAXPARAM; i++)
    713       1.1  jonathan 		krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
    714       1.1  jonathan 	for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
    715       1.1  jonathan 		size = (krp->krp_param[i].crp_nbits + 7) / 8;
    716       1.1  jonathan 		if (size == 0)
    717       1.1  jonathan 			continue;
    718      1.36       tls 		krp->krp_param[i].crp_p = kmem_alloc(size, KM_SLEEP);
    719       1.1  jonathan 		if (i >= krp->krp_iparams)
    720       1.1  jonathan 			continue;
    721  1.41.2.1  wrstuden 		error = copyin(kop->crk_param[i].crp_p,
    722  1.41.2.1  wrstuden 		    krp->krp_param[i].crp_p, size);
    723       1.1  jonathan 		if (error)
    724       1.1  jonathan 			goto fail;
    725       1.1  jonathan 	}
    726       1.1  jonathan 
    727       1.1  jonathan 	error = crypto_kdispatch(krp);
    728      1.33       tls 	if (error != 0) {
    729       1.1  jonathan 		goto fail;
    730      1.33       tls 	}
    731      1.33       tls 
    732      1.33       tls 	mutex_spin_enter(&crypto_mtx);
    733      1.33       tls 	while (!(krp->krp_flags & CRYPTO_F_DONE)) {
    734      1.33       tls 		cv_wait(&krp->krp_cv, &crypto_mtx);	/* XXX cv_wait_sig? */
    735      1.33       tls 	}
    736      1.33       tls 	if (krp->krp_flags & CRYPTO_F_ONRETQ) {
    737      1.33       tls 		DPRINTF(("cryptodev_key: DONE early, not via cryptoret.\n"));
    738      1.33       tls 		(void)crypto_ret_kq_remove(krp);
    739      1.33       tls 	}
    740      1.33       tls 	mutex_spin_exit(&crypto_mtx);
    741       1.1  jonathan 
    742       1.1  jonathan 	if (krp->krp_status != 0) {
    743  1.41.2.1  wrstuden 		DPRINTF(("cryptodev_key: krp->krp_status 0x%08x\n",
    744  1.41.2.1  wrstuden 		    krp->krp_status));
    745       1.1  jonathan 		error = krp->krp_status;
    746       1.1  jonathan 		goto fail;
    747       1.1  jonathan 	}
    748       1.1  jonathan 
    749  1.41.2.1  wrstuden 	for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams;
    750  1.41.2.1  wrstuden 	    i++) {
    751       1.1  jonathan 		size = (krp->krp_param[i].crp_nbits + 7) / 8;
    752       1.1  jonathan 		if (size == 0)
    753       1.1  jonathan 			continue;
    754  1.41.2.1  wrstuden 		error = copyout(krp->krp_param[i].crp_p,
    755  1.41.2.1  wrstuden 		    kop->crk_param[i].crp_p, size);
    756      1.34       tls 		if (error) {
    757  1.41.2.1  wrstuden 			DPRINTF(("cryptodev_key: copyout oparam %d failed, "
    758  1.41.2.1  wrstuden 			    "error=%d\n", i-krp->krp_iparams, error));
    759       1.1  jonathan 			goto fail;
    760      1.34       tls 		}
    761       1.1  jonathan 	}
    762       1.1  jonathan 
    763       1.1  jonathan fail:
    764  1.41.2.1  wrstuden 	kop->crk_status = krp->krp_status;
    765  1.41.2.1  wrstuden 	for (i = 0; i < CRK_MAXPARAM; i++) {
    766  1.41.2.1  wrstuden 		struct crparam *kp = &(krp->krp_param[i]);
    767  1.41.2.1  wrstuden 		if (krp->krp_param[i].crp_p) {
    768  1.41.2.1  wrstuden 			size = (kp->crp_nbits + 7)  / 8;
    769  1.41.2.1  wrstuden 			KASSERT(size > 0);
    770  1.41.2.1  wrstuden 			(void)memset(kp->crp_p, 0, size);
    771  1.41.2.1  wrstuden 			kmem_free(kp->crp_p, size);
    772       1.1  jonathan 		}
    773       1.1  jonathan 	}
    774  1.41.2.1  wrstuden 	pool_put(&cryptkop_pool, krp);
    775      1.34       tls 	DPRINTF(("cryptodev_key: error=0x%08x\n", error));
    776  1.41.2.1  wrstuden 	return error;
    777       1.1  jonathan }
    778       1.1  jonathan 
    779       1.1  jonathan /* ARGSUSED */
    780       1.1  jonathan static int
    781      1.36       tls cryptof_close(struct file *fp)
    782       1.1  jonathan {
    783      1.35        ad 	struct fcrypt *fcr = fp->f_data;
    784       1.1  jonathan 	struct csession *cse;
    785       1.1  jonathan 
    786      1.33       tls 	mutex_spin_enter(&crypto_mtx);
    787       1.1  jonathan 	while ((cse = TAILQ_FIRST(&fcr->csessions))) {
    788       1.1  jonathan 		TAILQ_REMOVE(&fcr->csessions, cse, next);
    789       1.1  jonathan 		(void)csefree(cse);
    790       1.1  jonathan 	}
    791      1.36       tls 	seldestroy(&fcr->sinfo);
    792       1.1  jonathan 	fp->f_data = NULL;
    793      1.33       tls 	mutex_spin_exit(&crypto_mtx);
    794       1.1  jonathan 
    795      1.38     rmind 	pool_put(&fcrpl, fcr);
    796       1.1  jonathan 	return 0;
    797       1.1  jonathan }
    798       1.1  jonathan 
    799      1.33       tls /* csefind: call with crypto_mtx held. */
    800       1.1  jonathan static struct csession *
    801       1.1  jonathan csefind(struct fcrypt *fcr, u_int ses)
    802       1.1  jonathan {
    803      1.33       tls 	struct csession *cse, *ret = NULL;
    804       1.1  jonathan 
    805      1.33       tls 	KASSERT(mutex_owned(&crypto_mtx));
    806       1.1  jonathan 	TAILQ_FOREACH(cse, &fcr->csessions, next)
    807       1.1  jonathan 		if (cse->ses == ses)
    808      1.33       tls 			ret = cse;
    809      1.36       tls 
    810  1.41.2.1  wrstuden 	return ret;
    811       1.1  jonathan }
    812       1.1  jonathan 
    813      1.33       tls /* csedelete: call with crypto_mtx held. */
    814       1.1  jonathan static int
    815       1.1  jonathan csedelete(struct fcrypt *fcr, struct csession *cse_del)
    816       1.1  jonathan {
    817       1.1  jonathan 	struct csession *cse;
    818      1.33       tls 	int ret = 0;
    819       1.1  jonathan 
    820      1.33       tls 	KASSERT(mutex_owned(&crypto_mtx));
    821       1.1  jonathan 	TAILQ_FOREACH(cse, &fcr->csessions, next) {
    822       1.1  jonathan 		if (cse == cse_del) {
    823       1.1  jonathan 			TAILQ_REMOVE(&fcr->csessions, cse, next);
    824      1.33       tls 			ret = 1;
    825       1.1  jonathan 		}
    826       1.1  jonathan 	}
    827  1.41.2.1  wrstuden 	return ret;
    828       1.1  jonathan }
    829       1.1  jonathan 
    830      1.33       tls /* cseadd: call with crypto_mtx held. */
    831       1.1  jonathan static struct csession *
    832       1.1  jonathan cseadd(struct fcrypt *fcr, struct csession *cse)
    833       1.1  jonathan {
    834      1.33       tls 	KASSERT(mutex_owned(&crypto_mtx));
    835      1.33       tls 	/* don't let session ID wrap! */
    836      1.33       tls 	if (fcr->sesn + 1 == 0) return NULL;
    837       1.1  jonathan 	TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
    838       1.1  jonathan 	cse->ses = fcr->sesn++;
    839  1.41.2.1  wrstuden 	return cse;
    840       1.1  jonathan }
    841       1.1  jonathan 
    842      1.33       tls /* csecreate: call with crypto_mtx held. */
    843       1.1  jonathan static struct csession *
    844      1.26  christos csecreate(struct fcrypt *fcr, u_int64_t sid, void *key, u_int64_t keylen,
    845      1.26  christos     void *mackey, u_int64_t mackeylen, u_int32_t cipher, u_int32_t mac,
    846       1.1  jonathan     struct enc_xform *txform, struct auth_hash *thash)
    847       1.1  jonathan {
    848       1.1  jonathan 	struct csession *cse;
    849       1.1  jonathan 
    850      1.33       tls 	KASSERT(mutex_owned(&crypto_mtx));
    851      1.29       tls 	cse = pool_get(&csepl, PR_NOWAIT);
    852       1.1  jonathan 	if (cse == NULL)
    853       1.1  jonathan 		return NULL;
    854       1.1  jonathan 	cse->key = key;
    855       1.1  jonathan 	cse->keylen = keylen/8;
    856       1.1  jonathan 	cse->mackey = mackey;
    857       1.1  jonathan 	cse->mackeylen = mackeylen/8;
    858       1.1  jonathan 	cse->sid = sid;
    859       1.1  jonathan 	cse->cipher = cipher;
    860       1.1  jonathan 	cse->mac = mac;
    861       1.1  jonathan 	cse->txform = txform;
    862       1.1  jonathan 	cse->thash = thash;
    863      1.34       tls 	cse->error = 0;
    864      1.28       tls 	if (cseadd(fcr, cse))
    865  1.41.2.1  wrstuden 		return cse;
    866      1.28       tls 	else {
    867      1.33       tls 		pool_put(&csepl, cse);
    868      1.28       tls 		return NULL;
    869      1.28       tls 	}
    870       1.1  jonathan }
    871       1.1  jonathan 
    872      1.33       tls /* csefree: call with crypto_mtx held. */
    873       1.1  jonathan static int
    874       1.1  jonathan csefree(struct csession *cse)
    875       1.1  jonathan {
    876       1.1  jonathan 	int error;
    877       1.1  jonathan 
    878      1.33       tls 	KASSERT(mutex_owned(&crypto_mtx));
    879       1.1  jonathan 	error = crypto_freesession(cse->sid);
    880       1.1  jonathan 	if (cse->key)
    881      1.36       tls 		free(cse->key, M_XDATA);
    882       1.1  jonathan 	if (cse->mackey)
    883      1.36       tls 		free(cse->mackey, M_XDATA);
    884      1.29       tls 	pool_put(&csepl, cse);
    885  1.41.2.1  wrstuden 	return error;
    886       1.1  jonathan }
    887       1.1  jonathan 
    888       1.1  jonathan static int
    889      1.25  christos cryptoopen(dev_t dev, int flag, int mode,
    890      1.25  christos     struct lwp *l)
    891       1.1  jonathan {
    892      1.35        ad 	file_t *fp;
    893      1.29       tls         struct fcrypt *fcr;
    894      1.29       tls         int fd, error;
    895      1.29       tls 
    896       1.1  jonathan 	if (crypto_usercrypto == 0)
    897  1.41.2.1  wrstuden 		return ENXIO;
    898      1.29       tls 
    899      1.35        ad 	if ((error = fd_allocfile(&fp, &fd)) != 0)
    900      1.29       tls 		return error;
    901      1.29       tls 
    902      1.29       tls 	fcr = pool_get(&fcrpl, PR_WAITOK);
    903      1.33       tls 	mutex_spin_enter(&crypto_mtx);
    904      1.29       tls 	TAILQ_INIT(&fcr->csessions);
    905      1.36       tls 	TAILQ_INIT(&fcr->crp_ret_mq);
    906      1.36       tls 	TAILQ_INIT(&fcr->crp_ret_mkq);
    907      1.36       tls 	selinit(&fcr->sinfo);
    908      1.29       tls 	/*
    909      1.29       tls 	 * Don't ever return session 0, to allow detection of
    910      1.29       tls 	 * failed creation attempts with multi-create ioctl.
    911      1.29       tls 	 */
    912      1.29       tls 	fcr->sesn = 1;
    913      1.36       tls 	fcr->requestid = 1;
    914      1.33       tls 	mutex_spin_exit(&crypto_mtx);
    915      1.35        ad 	return fd_clone(fp, fd, flag, &cryptofops, fcr);
    916       1.1  jonathan }
    917       1.1  jonathan 
    918       1.1  jonathan static int
    919      1.25  christos cryptoread(dev_t dev, struct uio *uio, int ioflag)
    920       1.1  jonathan {
    921  1.41.2.1  wrstuden 	return EIO;
    922       1.1  jonathan }
    923       1.1  jonathan 
    924       1.1  jonathan static int
    925      1.25  christos cryptowrite(dev_t dev, struct uio *uio, int ioflag)
    926       1.1  jonathan {
    927  1.41.2.1  wrstuden 	return EIO;
    928       1.1  jonathan }
    929       1.1  jonathan 
    930       1.1  jonathan int
    931      1.25  christos cryptoselect(dev_t dev, int rw, struct lwp *l)
    932       1.1  jonathan {
    933  1.41.2.1  wrstuden 	return 0;
    934       1.1  jonathan }
    935       1.1  jonathan 
    936       1.1  jonathan /*static*/
    937       1.1  jonathan struct cdevsw crypto_cdevsw = {
    938       1.1  jonathan 	/* open */	cryptoopen,
    939      1.29       tls 	/* close */	noclose,
    940       1.1  jonathan 	/* read */	cryptoread,
    941       1.1  jonathan 	/* write */	cryptowrite,
    942      1.29       tls 	/* ioctl */	noioctl,
    943       1.1  jonathan 	/* ttstop?*/	nostop,
    944       1.1  jonathan 	/* ??*/		notty,
    945       1.1  jonathan 	/* poll */	cryptoselect /*nopoll*/,
    946       1.1  jonathan 	/* mmap */	nommap,
    947       1.1  jonathan 	/* kqfilter */	nokqfilter,
    948      1.23  christos 	/* type */	D_OTHER,
    949       1.1  jonathan };
    950       1.1  jonathan 
    951      1.36       tls static int
    952      1.36       tls cryptodev_mop(struct fcrypt *fcr,
    953      1.36       tls               struct crypt_n_op * cnop,
    954      1.36       tls               int count, struct lwp *l)
    955      1.36       tls {
    956      1.36       tls 	struct cryptop *crp = NULL;
    957      1.36       tls 	struct cryptodesc *crde = NULL, *crda = NULL;
    958      1.36       tls 	int req, error=0;
    959      1.36       tls 	struct csession *cse;
    960      1.36       tls 
    961      1.36       tls 	for (req = 0; req < count; req++) {
    962      1.36       tls 		mutex_spin_enter(&crypto_mtx);
    963      1.36       tls 		cse = csefind(fcr, cnop[req].ses);
    964      1.36       tls 		if (cse == NULL) {
    965      1.36       tls 			DPRINTF(("csefind failed\n"));
    966      1.36       tls 			cnop[req].status = EINVAL;
    967      1.36       tls 			mutex_spin_exit(&crypto_mtx);
    968      1.36       tls 			continue;
    969      1.36       tls 		}
    970      1.36       tls 		mutex_spin_exit(&crypto_mtx);
    971      1.36       tls 
    972      1.36       tls 		if (cnop[req].len > 256*1024-4) {
    973      1.36       tls 			DPRINTF(("length failed\n"));
    974      1.36       tls 			cnop[req].status = EINVAL;
    975      1.36       tls 			continue;
    976      1.36       tls 		}
    977      1.36       tls 		if (cse->txform) {
    978      1.36       tls 			if (cnop[req].len == 0 ||
    979      1.36       tls 			    (cnop[req].len % cse->txform->blocksize) != 0) {
    980      1.36       tls 				cnop[req].status = EINVAL;
    981      1.36       tls 				continue;
    982      1.36       tls 			}
    983      1.36       tls 		}
    984      1.36       tls 
    985  1.41.2.1  wrstuden 		crp = crypto_getreq((cse->txform != NULL) +
    986  1.41.2.1  wrstuden 		    (cse->thash != NULL));
    987      1.36       tls 		if (crp == NULL) {
    988      1.36       tls 			cnop[req].status = ENOMEM;
    989      1.36       tls 			goto bail;
    990      1.36       tls 		}
    991      1.36       tls 
    992  1.41.2.1  wrstuden 		(void)memset(&crp->uio, 0, sizeof(crp->uio));
    993      1.36       tls 		crp->uio.uio_iovcnt = 1;
    994      1.36       tls 		crp->uio.uio_resid = 0;
    995      1.36       tls 		crp->uio.uio_rw = UIO_WRITE;
    996      1.36       tls 		crp->uio.uio_iov = crp->iovec;
    997      1.36       tls 		UIO_SETUP_SYSSPACE(&crp->uio);
    998      1.36       tls 		memset(&crp->iovec, 0, sizeof(crp->iovec));
    999      1.36       tls 		crp->uio.uio_iov[0].iov_len = cnop[req].len;
   1000      1.36       tls 		crp->uio.uio_iov[0].iov_base = kmem_alloc(cnop[req].len,
   1001  1.41.2.1  wrstuden 		    KM_SLEEP);
   1002      1.36       tls 		crp->uio.uio_resid = crp->uio.uio_iov[0].iov_len;
   1003      1.36       tls 
   1004      1.36       tls 		if (cse->thash) {
   1005      1.36       tls 			crda = crp->crp_desc;
   1006  1.41.2.1  wrstuden 			if (cse->txform && crda)
   1007      1.36       tls 				crde = crda->crd_next;
   1008      1.36       tls 		} else {
   1009      1.36       tls 			if (cse->txform)
   1010      1.36       tls 				crde = crp->crp_desc;
   1011      1.36       tls 			else {
   1012      1.36       tls 				cnop[req].status = EINVAL;
   1013      1.36       tls 				goto bail;
   1014      1.36       tls 			}
   1015      1.36       tls 		}
   1016      1.36       tls 
   1017      1.36       tls 		if ((copyin(cnop[req].src,
   1018      1.36       tls 		    crp->uio.uio_iov[0].iov_base, cnop[req].len))) {
   1019      1.36       tls 			cnop[req].status = EINVAL;
   1020      1.36       tls 			goto bail;
   1021      1.36       tls 		}
   1022      1.36       tls 
   1023      1.36       tls 		if (crda) {
   1024      1.36       tls 			crda->crd_skip = 0;
   1025      1.36       tls 			crda->crd_len = cnop[req].len;
   1026      1.36       tls 			crda->crd_inject = 0;	/* ??? */
   1027      1.36       tls 
   1028      1.36       tls 			crda->crd_alg = cse->mac;
   1029      1.36       tls 			crda->crd_key = cse->mackey;
   1030      1.36       tls 			crda->crd_klen = cse->mackeylen * 8;
   1031      1.36       tls 		}
   1032      1.36       tls 
   1033      1.36       tls 		if (crde) {
   1034      1.36       tls 			if (cnop[req].op == COP_ENCRYPT)
   1035      1.36       tls 				crde->crd_flags |= CRD_F_ENCRYPT;
   1036      1.36       tls 			else
   1037      1.36       tls 				crde->crd_flags &= ~CRD_F_ENCRYPT;
   1038      1.36       tls 			crde->crd_len = cnop[req].len;
   1039      1.36       tls 			crde->crd_inject = 0;
   1040      1.36       tls 
   1041      1.36       tls 			crde->crd_alg = cse->cipher;
   1042      1.36       tls #ifdef notyet		/* XXX must notify h/w driver new key, drain */
   1043      1.36       tls 			if(cnop[req].key && cnop[req].keylen) {
   1044      1.36       tls 				crde->crd_key = malloc(cnop[req].keylen,
   1045      1.36       tls 						    M_XDATA, M_WAITOK);
   1046      1.36       tls 				if((error = copyin(cnop[req].key,
   1047      1.36       tls 				    crde->crd_key, cnop[req].keylen))) {
   1048      1.36       tls 					cnop[req].status = EINVAL;
   1049      1.36       tls 					goto bail;
   1050      1.36       tls 				}
   1051      1.36       tls 				crde->crd_klen =  cnop[req].keylen * 8;
   1052      1.36       tls 			} else { ... }
   1053      1.36       tls #endif
   1054      1.36       tls 			crde->crd_key = cse->key;
   1055      1.36       tls 			crde->crd_klen = cse->keylen * 8;
   1056      1.36       tls 		}
   1057      1.36       tls 
   1058      1.36       tls 		crp->crp_ilen = cnop[req].len;
   1059  1.41.2.1  wrstuden 		crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM |
   1060  1.41.2.1  wrstuden 		    (cnop[req].flags & COP_F_BATCH);
   1061      1.36       tls 		crp->crp_buf = (void *)&crp->uio;
   1062      1.36       tls 		crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_mcb;
   1063      1.36       tls 		crp->crp_sid = cse->sid;
   1064      1.36       tls 		crp->crp_opaque = (void *)cse;
   1065      1.36       tls 		crp->fcrp = fcr;
   1066      1.36       tls 		crp->dst = cnop[req].dst;
   1067      1.36       tls 		/* we can use the crp_ilen in cryptop(crp) for this */
   1068      1.36       tls 		crp->len = cnop[req].len;
   1069      1.36       tls 		crp->mac = cnop[req].mac;
   1070      1.36       tls 
   1071      1.36       tls 		if (cnop[req].iv) {
   1072      1.36       tls 			if (crde == NULL) {
   1073      1.36       tls 				cnop[req].status = EINVAL;
   1074      1.36       tls 				goto bail;
   1075      1.36       tls 			}
   1076      1.36       tls 			if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
   1077      1.36       tls 				cnop[req].status = EINVAL;
   1078      1.36       tls 				goto bail;
   1079      1.36       tls 			}
   1080      1.36       tls 			if ((error = copyin(cnop[req].iv, crp->tmp_iv,
   1081      1.36       tls 			    cse->txform->blocksize))) {
   1082      1.36       tls 				cnop[req].status = EINVAL;
   1083      1.36       tls 				goto bail;
   1084      1.36       tls 			}
   1085  1.41.2.1  wrstuden 			(void)memcpy(crde->crd_iv, crp->tmp_iv,
   1086  1.41.2.1  wrstuden 			    cse->txform->blocksize);
   1087      1.36       tls 			crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
   1088      1.36       tls 			crde->crd_skip = 0;
   1089      1.36       tls 		} else if (crde) {
   1090      1.36       tls 			if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
   1091      1.36       tls 				crde->crd_skip = 0;
   1092      1.36       tls 			} else {
   1093      1.36       tls 				crde->crd_flags |= CRD_F_IV_PRESENT;
   1094      1.36       tls 				crde->crd_skip = cse->txform->blocksize;
   1095      1.36       tls 				crde->crd_len -= cse->txform->blocksize;
   1096      1.36       tls 			}
   1097      1.36       tls 		}
   1098      1.36       tls 
   1099      1.36       tls 		if (cnop[req].mac) {
   1100      1.36       tls 			if (crda == NULL) {
   1101      1.36       tls 				cnop[req].status = EINVAL;
   1102      1.36       tls 				goto bail;
   1103      1.36       tls 			}
   1104      1.36       tls 			crp->crp_mac=cse->tmp_mac;
   1105      1.36       tls 		}
   1106      1.36       tls 		cnop[req].reqid = atomic_inc_32_nv(&(fcr->requestid));
   1107      1.36       tls 		crp->crp_reqid = cnop[req].reqid;
   1108      1.36       tls 		crp->crp_usropaque = cnop[req].opaque;
   1109      1.36       tls #ifdef notyet
   1110      1.36       tls eagain:
   1111      1.36       tls #endif
   1112      1.36       tls 		cnop[req].status = crypto_dispatch(crp);
   1113      1.36       tls 		mutex_spin_enter(&crypto_mtx);	/* XXX why mutex? */
   1114      1.36       tls 
   1115      1.36       tls 		switch (cnop[req].status) {
   1116      1.36       tls #ifdef notyet	/* don't loop forever -- but EAGAIN not possible here yet */
   1117      1.36       tls 		case EAGAIN:
   1118      1.36       tls 			mutex_spin_exit(&crypto_mtx);
   1119      1.36       tls 			goto eagain;
   1120      1.36       tls 			break;
   1121      1.36       tls #endif
   1122      1.36       tls 		case 0:
   1123      1.36       tls 			break;
   1124      1.36       tls 		default:
   1125      1.36       tls 			DPRINTF(("cryptodev_op: not waiting, error.\n"));
   1126      1.36       tls 			mutex_spin_exit(&crypto_mtx);
   1127      1.36       tls 			goto bail;
   1128      1.36       tls 		}
   1129      1.36       tls 
   1130      1.36       tls 		mutex_spin_exit(&crypto_mtx);
   1131      1.36       tls bail:
   1132      1.36       tls 		if (cnop[req].status) {
   1133      1.36       tls 			if (crp) {
   1134      1.36       tls 				crypto_freereq(crp);
   1135      1.36       tls 				if(cse->uio.uio_iov[0].iov_base) {
   1136      1.36       tls 					kmem_free(cse->uio.uio_iov[0].iov_base,
   1137  1.41.2.1  wrstuden 					    cse->uio.uio_iov[0].iov_len);
   1138      1.36       tls 				}
   1139      1.36       tls 			}
   1140      1.36       tls 			error = 0;
   1141      1.36       tls 		}
   1142      1.36       tls 	}
   1143  1.41.2.1  wrstuden 	return error;
   1144      1.36       tls }
   1145      1.36       tls 
   1146      1.36       tls static int
   1147      1.36       tls cryptodev_mkey(struct fcrypt *fcr, struct crypt_n_kop *kop, int count)
   1148      1.36       tls {
   1149      1.36       tls 	struct cryptkop *krp = NULL;
   1150      1.36       tls 	int error = EINVAL;
   1151      1.36       tls 	int in, out, size, i, req;
   1152      1.36       tls 
   1153      1.36       tls 	for (req = 0; req < count; req++) {
   1154  1.41.2.1  wrstuden 		if (kop[req].crk_iparams + kop[req].crk_oparams > CRK_MAXPARAM)
   1155  1.41.2.1  wrstuden 			return EFBIG;
   1156      1.36       tls 
   1157      1.36       tls 		in = kop[req].crk_iparams;
   1158      1.36       tls 		out = kop[req].crk_oparams;
   1159      1.36       tls 		switch (kop[req].crk_op) {
   1160      1.36       tls 		case CRK_MOD_EXP:
   1161      1.36       tls 			if (in == 3 && out == 1)
   1162      1.36       tls 				break;
   1163      1.36       tls 			kop[req].crk_status = EINVAL;
   1164      1.36       tls 			continue;
   1165      1.36       tls 		case CRK_MOD_EXP_CRT:
   1166      1.36       tls 			if (in == 6 && out == 1)
   1167      1.36       tls 				break;
   1168      1.36       tls 			kop[req].crk_status = EINVAL;
   1169      1.36       tls 			continue;
   1170      1.36       tls 		case CRK_DSA_SIGN:
   1171      1.36       tls 			if (in == 5 && out == 2)
   1172      1.36       tls 				break;
   1173      1.36       tls 			kop[req].crk_status = EINVAL;
   1174      1.36       tls 			continue;
   1175      1.36       tls 		case CRK_DSA_VERIFY:
   1176      1.36       tls 			if (in == 7 && out == 0)
   1177      1.36       tls 				break;
   1178      1.36       tls 			kop[req].crk_status = EINVAL;
   1179      1.36       tls 			continue;
   1180      1.36       tls 		case CRK_DH_COMPUTE_KEY:
   1181      1.36       tls 			if (in == 3 && out == 1)
   1182      1.36       tls 				break;
   1183      1.36       tls 			kop[req].crk_status = EINVAL;
   1184      1.36       tls 			continue;
   1185      1.36       tls 		case CRK_MOD_ADD:
   1186      1.36       tls 			if (in == 3 && out == 1)
   1187      1.36       tls 				break;
   1188      1.36       tls 			kop[req].crk_status = EINVAL;
   1189      1.36       tls 			continue;
   1190      1.36       tls 		case CRK_MOD_ADDINV:
   1191      1.36       tls 			if (in == 2 && out == 1)
   1192      1.36       tls 				break;
   1193      1.36       tls 			kop[req].crk_status = EINVAL;
   1194      1.36       tls 			continue;
   1195      1.36       tls 		case CRK_MOD_SUB:
   1196      1.36       tls 			if (in == 3 && out == 1)
   1197      1.36       tls 				break;
   1198      1.36       tls 			kop[req].crk_status = EINVAL;
   1199      1.36       tls 			continue;
   1200      1.36       tls 		case CRK_MOD_MULT:
   1201      1.36       tls 			if (in == 3 && out == 1)
   1202      1.36       tls 				break;
   1203      1.36       tls 			kop[req].crk_status = EINVAL;
   1204      1.36       tls 			continue;
   1205      1.36       tls 		case CRK_MOD_MULTINV:
   1206      1.36       tls 			if (in == 2 && out == 1)
   1207      1.36       tls 				break;
   1208      1.36       tls 			kop[req].crk_status = EINVAL;
   1209      1.36       tls 			continue;
   1210      1.36       tls 		case CRK_MOD:
   1211      1.36       tls 			if (in == 2 && out == 1)
   1212      1.36       tls 				break;
   1213      1.36       tls 			kop[req].crk_status = EINVAL;
   1214      1.36       tls 			continue;
   1215      1.36       tls 		default:
   1216      1.36       tls 			kop[req].crk_status = EINVAL;
   1217      1.36       tls 			continue;
   1218      1.36       tls 		}
   1219      1.36       tls 
   1220      1.36       tls 		krp = pool_get(&cryptkop_pool, PR_WAITOK);
   1221  1.41.2.1  wrstuden 		(void)memset(krp, 0, sizeof *krp);
   1222      1.36       tls 		cv_init(&krp->krp_cv, "crykdev");
   1223      1.36       tls 		krp->krp_op = kop[req].crk_op;
   1224      1.36       tls 		krp->krp_status = kop[req].crk_status;
   1225      1.36       tls 		krp->krp_iparams = kop[req].crk_iparams;
   1226      1.36       tls 		krp->krp_oparams = kop[req].crk_oparams;
   1227      1.36       tls 		krp->krp_status = 0;
   1228      1.36       tls 		krp->krp_callback =
   1229      1.36       tls 		    (int (*) (struct cryptkop *)) cryptodevkey_mcb;
   1230  1.41.2.1  wrstuden 		(void)memcpy(krp->crk_param, kop[req].crk_param,
   1231  1.41.2.1  wrstuden 		    sizeof(kop[req].crk_param));
   1232      1.36       tls 
   1233      1.36       tls 		krp->krp_flags = CRYPTO_F_CBIMM;
   1234      1.36       tls 
   1235      1.36       tls 		for (i = 0; i < CRK_MAXPARAM; i++)
   1236      1.36       tls 			krp->krp_param[i].crp_nbits =
   1237      1.36       tls 			    kop[req].crk_param[i].crp_nbits;
   1238      1.36       tls 		for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
   1239      1.36       tls 			size = (krp->krp_param[i].crp_nbits + 7) / 8;
   1240      1.36       tls 			if (size == 0)
   1241      1.36       tls 				continue;
   1242      1.36       tls 			krp->krp_param[i].crp_p =
   1243      1.36       tls 			    kmem_alloc(size, KM_SLEEP);
   1244      1.36       tls 			if (i >= krp->krp_iparams)
   1245      1.36       tls 				continue;
   1246  1.41.2.1  wrstuden 			kop[req].crk_status =
   1247  1.41.2.1  wrstuden 			    copyin(kop[req].crk_param[i].crp_p,
   1248  1.41.2.1  wrstuden 			    krp->krp_param[i].crp_p, size);
   1249      1.36       tls 			if (kop[req].crk_status)
   1250      1.36       tls 				goto fail;
   1251      1.36       tls 		}
   1252      1.36       tls 		krp->fcrp = fcr;
   1253      1.36       tls 
   1254      1.36       tls 		kop[req].crk_reqid = atomic_inc_32_nv(&(fcr->requestid));
   1255      1.36       tls 		krp->krp_reqid = kop[req].crk_reqid;
   1256      1.36       tls 		krp->krp_usropaque = kop[req].crk_opaque;
   1257      1.36       tls 
   1258      1.36       tls 		kop[req].crk_status = crypto_kdispatch(krp);
   1259      1.36       tls 		if (kop[req].crk_status != 0) {
   1260      1.36       tls 			goto fail;
   1261      1.36       tls 		}
   1262      1.36       tls 
   1263      1.36       tls fail:
   1264      1.36       tls 		if(kop[req].crk_status) {
   1265      1.36       tls 			if (krp) {
   1266      1.36       tls 				kop[req].crk_status = krp->krp_status;
   1267      1.36       tls 				for (i = 0; i < CRK_MAXPARAM; i++) {
   1268      1.36       tls 					struct crparam *kp =
   1269      1.36       tls 						&(krp->krp_param[i]);
   1270      1.36       tls 					if (kp->crp_p) {
   1271      1.36       tls 						size = (kp->crp_nbits + 7) / 8;
   1272      1.36       tls 						KASSERT(size > 0);
   1273      1.36       tls 						memset(kp->crp_p, 0, size);
   1274      1.36       tls 						kmem_free(kp->crp_p, size);
   1275      1.36       tls 					}
   1276      1.36       tls 				}
   1277      1.36       tls 				pool_put(&cryptkop_pool, krp);
   1278      1.36       tls 			}
   1279      1.36       tls 		}
   1280      1.36       tls 		error = 0;
   1281      1.36       tls 	}
   1282      1.36       tls 	DPRINTF(("cryptodev_key: error=0x%08x\n", error));
   1283  1.41.2.1  wrstuden 	return error;
   1284      1.36       tls }
   1285      1.36       tls 
   1286      1.36       tls static int
   1287      1.36       tls cryptodev_session(struct fcrypt *fcr, struct session_op *sop) {
   1288      1.36       tls 	struct cryptoini cria, crie;
   1289      1.36       tls 	struct enc_xform *txform = NULL;
   1290      1.36       tls 	struct auth_hash *thash = NULL;
   1291      1.36       tls 	struct csession *cse;
   1292      1.36       tls 	u_int64_t sid;
   1293      1.36       tls 	int error = 0;
   1294      1.36       tls 
   1295      1.36       tls 	/* XXX there must be a way to not embed the list of xforms here */
   1296      1.36       tls 	switch (sop->cipher) {
   1297      1.36       tls 	case 0:
   1298      1.36       tls 		break;
   1299      1.36       tls 	case CRYPTO_DES_CBC:
   1300      1.36       tls 		txform = &enc_xform_des;
   1301      1.36       tls 		break;
   1302      1.36       tls 	case CRYPTO_3DES_CBC:
   1303      1.36       tls 		txform = &enc_xform_3des;
   1304      1.36       tls 		break;
   1305      1.36       tls 	case CRYPTO_BLF_CBC:
   1306      1.36       tls 		txform = &enc_xform_blf;
   1307      1.36       tls 		break;
   1308      1.36       tls 	case CRYPTO_CAST_CBC:
   1309      1.36       tls 		txform = &enc_xform_cast5;
   1310      1.36       tls 	case CRYPTO_SKIPJACK_CBC:
   1311      1.36       tls 		txform = &enc_xform_skipjack;
   1312      1.36       tls 		break;
   1313      1.36       tls 	case CRYPTO_AES_CBC:
   1314      1.36       tls 		txform = &enc_xform_rijndael128;
   1315      1.36       tls 		break;
   1316      1.36       tls 	case CRYPTO_NULL_CBC:
   1317      1.36       tls 		txform = &enc_xform_null;
   1318      1.36       tls 		break;
   1319      1.36       tls 	case CRYPTO_ARC4:
   1320      1.36       tls 		txform = &enc_xform_arc4;
   1321      1.36       tls 		break;
   1322      1.36       tls 	default:
   1323      1.36       tls 		DPRINTF(("Invalid cipher %d\n", sop->cipher));
   1324      1.36       tls 		return EINVAL;
   1325      1.36       tls 	}
   1326      1.36       tls 
   1327      1.36       tls 	switch (sop->mac) {
   1328      1.36       tls 	case 0:
   1329      1.36       tls 		break;
   1330      1.36       tls 	case CRYPTO_MD5_HMAC:
   1331      1.36       tls 		thash = &auth_hash_hmac_md5;
   1332      1.36       tls 		break;
   1333      1.36       tls 	case CRYPTO_SHA1_HMAC:
   1334      1.36       tls 		thash = &auth_hash_hmac_sha1;
   1335      1.36       tls 		break;
   1336      1.36       tls 	case CRYPTO_MD5_HMAC_96:
   1337      1.36       tls 		thash = &auth_hash_hmac_md5_96;
   1338      1.36       tls 		break;
   1339      1.36       tls 	case CRYPTO_SHA1_HMAC_96:
   1340      1.36       tls 		thash = &auth_hash_hmac_sha1_96;
   1341      1.36       tls 		break;
   1342      1.36       tls 	case CRYPTO_SHA2_HMAC:
   1343      1.36       tls 		/* XXX switching on key length seems questionable */
   1344      1.36       tls 		if (sop->mackeylen == auth_hash_hmac_sha2_256.keysize) {
   1345      1.36       tls 			thash = &auth_hash_hmac_sha2_256;
   1346      1.36       tls 		} else if (sop->mackeylen == auth_hash_hmac_sha2_384.keysize) {
   1347      1.36       tls 			thash = &auth_hash_hmac_sha2_384;
   1348      1.36       tls 		} else if (sop->mackeylen == auth_hash_hmac_sha2_512.keysize) {
   1349      1.36       tls 			thash = &auth_hash_hmac_sha2_512;
   1350      1.36       tls 		} else {
   1351      1.36       tls 			DPRINTF(("Invalid mackeylen %d\n", sop->mackeylen));
   1352      1.36       tls 			return EINVAL;
   1353      1.36       tls 		}
   1354      1.36       tls 		break;
   1355      1.36       tls 	case CRYPTO_RIPEMD160_HMAC:
   1356      1.36       tls 		thash = &auth_hash_hmac_ripemd_160;
   1357      1.36       tls 		break;
   1358      1.36       tls 	case CRYPTO_RIPEMD160_HMAC_96:
   1359      1.36       tls 		thash = &auth_hash_hmac_ripemd_160_96;
   1360      1.36       tls 		break;
   1361      1.36       tls 	case CRYPTO_MD5:
   1362      1.36       tls 		thash = &auth_hash_md5;
   1363      1.36       tls 		break;
   1364      1.36       tls 	case CRYPTO_SHA1:
   1365      1.36       tls 		thash = &auth_hash_sha1;
   1366      1.36       tls 		break;
   1367      1.36       tls 	case CRYPTO_NULL_HMAC:
   1368      1.36       tls 		thash = &auth_hash_null;
   1369      1.36       tls 		break;
   1370      1.36       tls 	default:
   1371      1.36       tls 		DPRINTF(("Invalid mac %d\n", sop->mac));
   1372  1.41.2.1  wrstuden 		return EINVAL;
   1373      1.36       tls 	}
   1374      1.36       tls 
   1375      1.36       tls 	memset(&crie, 0, sizeof(crie));
   1376      1.36       tls 	memset(&cria, 0, sizeof(cria));
   1377      1.36       tls 
   1378      1.36       tls 	if (txform) {
   1379      1.36       tls 		crie.cri_alg = txform->type;
   1380      1.36       tls 		crie.cri_klen = sop->keylen * 8;
   1381      1.36       tls 		if (sop->keylen > txform->maxkey ||
   1382      1.36       tls 		    sop->keylen < txform->minkey) {
   1383      1.36       tls 			DPRINTF(("keylen %d not in [%d,%d]\n",
   1384  1.41.2.1  wrstuden 			    sop->keylen, txform->minkey, txform->maxkey));
   1385  1.41.2.1  wrstuden 			error = EINVAL;
   1386      1.36       tls 			goto bail;
   1387      1.36       tls 		}
   1388      1.36       tls 
   1389      1.36       tls 		crie.cri_key = malloc(crie.cri_klen / 8, M_XDATA, M_WAITOK);
   1390  1.41.2.1  wrstuden 		if ((error = copyin(sop->key, crie.cri_key, crie.cri_klen / 8)))
   1391      1.36       tls 			goto bail;
   1392  1.41.2.1  wrstuden 		if (thash)
   1393      1.36       tls 			crie.cri_next = &cria;	/* XXX forces enc then hash? */
   1394      1.36       tls 	}
   1395      1.36       tls 
   1396      1.36       tls 	if (thash) {
   1397      1.36       tls 		cria.cri_alg = thash->type;
   1398      1.36       tls 		cria.cri_klen = sop->mackeylen * 8;
   1399      1.36       tls 		if (sop->mackeylen != thash->keysize) {
   1400      1.36       tls 			DPRINTF(("mackeylen %d != keysize %d\n",
   1401  1.41.2.1  wrstuden 			    sop->mackeylen, thash->keysize));
   1402      1.36       tls 			error = EINVAL;
   1403      1.36       tls 			goto bail;
   1404      1.36       tls 		}
   1405      1.36       tls 		if (cria.cri_klen) {
   1406      1.36       tls 			cria.cri_key = malloc(cria.cri_klen / 8, M_XDATA,
   1407  1.41.2.1  wrstuden 			    M_WAITOK);
   1408      1.36       tls 			if ((error = copyin(sop->mackey, cria.cri_key,
   1409  1.41.2.1  wrstuden 			    cria.cri_klen / 8))) {
   1410      1.36       tls 				goto bail;
   1411      1.36       tls 			}
   1412      1.36       tls 		}
   1413      1.36       tls 	}
   1414      1.36       tls 	/* crypto_newsession requires that we hold the mutex. */
   1415      1.36       tls 	mutex_spin_enter(&crypto_mtx);
   1416      1.36       tls 	error = crypto_newsession(&sid, (txform ? &crie : &cria),
   1417  1.41.2.1  wrstuden 	    crypto_devallowsoft);
   1418      1.36       tls 	if (!error) {
   1419      1.36       tls 		cse = csecreate(fcr, sid, crie.cri_key, crie.cri_klen,
   1420  1.41.2.1  wrstuden 		    cria.cri_key, cria.cri_klen, sop->cipher, sop->mac,
   1421  1.41.2.1  wrstuden 		    txform, thash);
   1422      1.36       tls 		if (cse != NULL) {
   1423      1.36       tls 			sop->ses = cse->ses;
   1424      1.36       tls 		} else {
   1425      1.36       tls 			DPRINTF(("csecreate failed\n"));
   1426      1.36       tls 			crypto_freesession(sid);
   1427      1.36       tls 			error = EINVAL;
   1428      1.36       tls 		}
   1429      1.36       tls 	} else {
   1430      1.36       tls 		DPRINTF(("SIOCSESSION violates kernel parameters %d\n",
   1431  1.41.2.1  wrstuden 		    error));
   1432      1.36       tls 	}
   1433      1.36       tls 	mutex_spin_exit(&crypto_mtx);
   1434      1.36       tls bail:
   1435      1.36       tls 	if (error) {
   1436      1.36       tls 		if (crie.cri_key) {
   1437      1.36       tls 			memset(crie.cri_key, 0, crie.cri_klen / 8);
   1438      1.36       tls 			free(crie.cri_key, M_XDATA);
   1439      1.36       tls 		}
   1440      1.36       tls 		if (cria.cri_key) {
   1441      1.36       tls 			memset(cria.cri_key, 0, cria.cri_klen / 8);
   1442      1.36       tls 			free(cria.cri_key, M_XDATA);
   1443      1.36       tls 		}
   1444      1.36       tls 	}
   1445      1.36       tls 	return error;
   1446      1.36       tls }
   1447      1.36       tls 
   1448      1.36       tls static int
   1449      1.36       tls cryptodev_msession(struct fcrypt *fcr, struct session_n_op *sn_ops,
   1450      1.36       tls 		   int count)
   1451      1.36       tls {
   1452      1.36       tls 	int i;
   1453      1.36       tls 
   1454      1.36       tls 	for (i = 0; i < count; i++, sn_ops++) {
   1455      1.36       tls 		struct session_op s_op;
   1456      1.36       tls 		s_op.cipher =		sn_ops->cipher;
   1457      1.36       tls 		s_op.mac =		sn_ops->mac;
   1458      1.36       tls 		s_op.keylen =		sn_ops->keylen;
   1459      1.36       tls 		s_op.key =		sn_ops->key;
   1460      1.36       tls 		s_op.mackeylen =	sn_ops->mackeylen;
   1461      1.36       tls 		s_op.mackey =		sn_ops->mackey;
   1462      1.36       tls 
   1463      1.36       tls 		sn_ops->status = cryptodev_session(fcr, &s_op);
   1464      1.36       tls 		sn_ops->ses =		s_op.ses;
   1465      1.36       tls 	}
   1466      1.36       tls 
   1467      1.36       tls 	return 0;
   1468      1.36       tls }
   1469      1.36       tls 
   1470      1.36       tls static int
   1471      1.36       tls cryptodev_msessionfin(struct fcrypt *fcr, int count, u_int32_t *sesid)
   1472      1.36       tls {
   1473      1.36       tls 	struct csession *cse;
   1474      1.36       tls 	int req, error = 0;
   1475      1.36       tls 
   1476      1.36       tls 	mutex_spin_enter(&crypto_mtx);
   1477      1.36       tls 	for(req = 0; req < count; req++) {
   1478      1.36       tls 		cse = csefind(fcr, sesid[req]);
   1479      1.36       tls 		if (cse == NULL)
   1480      1.36       tls 			continue;
   1481      1.36       tls 		csedelete(fcr, cse);
   1482      1.36       tls 		error = csefree(cse);
   1483      1.36       tls 	}
   1484      1.36       tls 	mutex_spin_exit(&crypto_mtx);
   1485      1.36       tls 	return 0;
   1486      1.36       tls }
   1487      1.36       tls 
   1488      1.36       tls /*
   1489  1.41.2.1  wrstuden  * collect as many completed requests as are availble, or count completed
   1490  1.41.2.1  wrstuden  * requests whichever is less.
   1491      1.36       tls  * return the number of requests.
   1492      1.36       tls  */
   1493      1.36       tls static int
   1494      1.36       tls cryptodev_getmstatus(struct fcrypt *fcr, struct crypt_result *crypt_res,
   1495  1.41.2.1  wrstuden     int count)
   1496      1.36       tls {
   1497      1.36       tls 	struct cryptop *crp = NULL;
   1498      1.36       tls 	struct cryptkop *krp = NULL;
   1499      1.36       tls 	struct csession *cse;
   1500      1.36       tls 	int i, size, req = 0;
   1501      1.36       tls 	int completed=0;
   1502      1.36       tls 
   1503      1.36       tls 	/* On stack so nobody else can grab them -- no locking */
   1504      1.36       tls 	SLIST_HEAD(, cryptop) crp_delfree_l =
   1505      1.36       tls 	    SLIST_HEAD_INITIALIZER(crp_delfree_l);
   1506      1.36       tls 	SLIST_HEAD(, cryptkop) krp_delfree_l =
   1507      1.36       tls 	    SLIST_HEAD_INITIALIZER(krp_delfree_l);
   1508      1.36       tls 
   1509      1.36       tls 	mutex_spin_enter(&crypto_mtx);
   1510      1.36       tls 
   1511      1.36       tls 	/* at this point we do not know which response user is requesting for
   1512      1.36       tls 	 * (symmetric or asymmetric) so we copyout one from each i.e if the
   1513      1.36       tls 	 * count is 2 then 1 from symmetric and 1 from asymmetric queue and
   1514      1.36       tls 	 * if 3 then 2 symmetric and 1 asymmetric and so on */
   1515      1.36       tls 	for(; req < count ;) {
   1516      1.36       tls 		crp = TAILQ_FIRST(&fcr->crp_ret_mq);
   1517      1.36       tls 		if(crp) {
   1518      1.36       tls 			cse = (struct csession *)crp->crp_opaque;
   1519      1.36       tls 			crypt_res[req].reqid = crp->crp_reqid;
   1520      1.36       tls 			crypt_res[req].opaque = crp->crp_usropaque;
   1521      1.36       tls 			completed++;
   1522      1.36       tls 			cse = csefind(fcr, cse->ses);
   1523      1.36       tls 			if (cse == NULL) {
   1524      1.36       tls 				DPRINTF(("csefind failed\n"));
   1525      1.36       tls 				crypt_res[req].status = EINVAL;
   1526      1.36       tls 				goto bail;
   1527      1.36       tls 			}
   1528      1.36       tls 
   1529      1.36       tls 			if (crp->crp_etype != 0) {
   1530      1.36       tls 				crypt_res[req].status = crp->crp_etype;
   1531      1.36       tls 				goto bail;
   1532      1.36       tls 			}
   1533      1.36       tls 
   1534      1.36       tls 			if (cse->error) {
   1535      1.36       tls 				crypt_res[req].status = cse->error;
   1536      1.36       tls 				goto bail;
   1537      1.36       tls 			}
   1538      1.36       tls 
   1539  1.41.2.1  wrstuden 			if (crp->dst && (crypt_res[req].status =
   1540  1.41.2.1  wrstuden 			    copyout(crp->uio.uio_iov[0].iov_base, crp->dst,
   1541  1.41.2.1  wrstuden 			    crp->len)))
   1542      1.36       tls 				goto bail;
   1543      1.36       tls 
   1544  1.41.2.1  wrstuden 			if (crp->mac && (crypt_res[req].status =
   1545  1.41.2.1  wrstuden 			    copyout(crp->crp_mac, crp->mac,
   1546  1.41.2.1  wrstuden 			    cse->thash->authsize)))
   1547      1.36       tls 				goto bail;
   1548      1.36       tls bail:
   1549      1.36       tls 			TAILQ_REMOVE(&fcr->crp_ret_mq, crp, crp_next);
   1550      1.36       tls 			SLIST_INSERT_HEAD(&crp_delfree_l, crp,
   1551  1.41.2.1  wrstuden 			    crp_qun.crp_lnext);
   1552      1.36       tls 			req++;
   1553      1.36       tls 		}
   1554      1.36       tls 
   1555      1.36       tls 		if(req < count) {
   1556      1.36       tls 			krp = TAILQ_FIRST(&fcr->crp_ret_mkq);
   1557      1.36       tls 			if (krp) {
   1558      1.36       tls 				crypt_res[req].reqid = krp->krp_reqid;
   1559      1.36       tls 				crypt_res[req].opaque = krp->krp_usropaque;
   1560      1.36       tls 				completed++;
   1561      1.36       tls 				if (krp->krp_status != 0) {
   1562      1.36       tls 					DPRINTF(("cryptodev_key: "
   1563  1.41.2.1  wrstuden 					    "krp->krp_status 0x%08x\n",
   1564  1.41.2.1  wrstuden 					    krp->krp_status));
   1565  1.41.2.1  wrstuden 					crypt_res[req].status = krp->krp_status;
   1566      1.36       tls 					goto fail;
   1567      1.36       tls 				}
   1568      1.36       tls 
   1569      1.36       tls 				for (i = krp->krp_iparams; i < krp->krp_iparams
   1570  1.41.2.1  wrstuden 				    + krp->krp_oparams; i++) {
   1571      1.36       tls 					size = (krp->krp_param[i].crp_nbits
   1572  1.41.2.1  wrstuden 					    + 7) / 8;
   1573      1.36       tls 					if (size == 0)
   1574      1.36       tls 						continue;
   1575      1.36       tls 					crypt_res[req].status = copyout
   1576      1.36       tls 					    (krp->krp_param[i].crp_p,
   1577      1.36       tls 					    krp->crk_param[i].crp_p, size);
   1578      1.36       tls 					if (crypt_res[req].status) {
   1579      1.36       tls 						DPRINTF(("cryptodev_key: "
   1580  1.41.2.1  wrstuden 						    "copyout oparam %d failed, "
   1581  1.41.2.1  wrstuden 						    "error=%d\n",
   1582  1.41.2.1  wrstuden 						    i - krp->krp_iparams,
   1583  1.41.2.1  wrstuden 						    crypt_res[req].status));
   1584      1.36       tls 						goto fail;
   1585      1.36       tls 					}
   1586      1.36       tls 				}
   1587      1.36       tls fail:
   1588      1.36       tls 				TAILQ_REMOVE(&fcr->crp_ret_mkq, krp, krp_next);
   1589      1.36       tls 				/* not sure what to do for this */
   1590      1.36       tls 				/* kop[req].crk_status = krp->krp_status; */
   1591      1.36       tls 				SLIST_INSERT_HEAD(&krp_delfree_l, krp,
   1592  1.41.2.1  wrstuden 				    krp_qun.krp_lnext);
   1593      1.36       tls 			}
   1594      1.36       tls 			req++;
   1595      1.36       tls 		}
   1596      1.36       tls 	}
   1597      1.36       tls 	mutex_spin_exit(&crypto_mtx);
   1598      1.36       tls 
   1599      1.36       tls 	while(!SLIST_EMPTY(&crp_delfree_l)) {
   1600      1.36       tls 		crp = SLIST_FIRST(&crp_delfree_l);
   1601      1.36       tls 		SLIST_REMOVE_HEAD(&crp_delfree_l, crp_qun.crp_lnext);
   1602      1.36       tls 		kmem_free(crp->uio.uio_iov[0].iov_base,
   1603  1.41.2.1  wrstuden 		    crp->uio.uio_iov[0].iov_len);
   1604      1.36       tls 		crypto_freereq(crp);
   1605      1.36       tls 	}
   1606      1.36       tls 
   1607      1.36       tls 	while(!SLIST_EMPTY(&krp_delfree_l)) {
   1608      1.36       tls 		krp = SLIST_FIRST(&krp_delfree_l);
   1609      1.36       tls 		for (i = 0; i < CRK_MAXPARAM; i++) {
   1610      1.36       tls 			struct crparam *kp = &(krp->krp_param[i]);
   1611      1.36       tls 			if (kp->crp_p) {
   1612  1.41.2.1  wrstuden 				size = (kp->crp_nbits + 7) / 8;
   1613  1.41.2.1  wrstuden 				KASSERT(size > 0);
   1614  1.41.2.1  wrstuden 				(void)memset(kp->crp_p, 0, size);
   1615  1.41.2.1  wrstuden 				kmem_free(kp->crp_p, size);
   1616      1.36       tls 			}
   1617      1.36       tls 		}
   1618      1.36       tls 		SLIST_REMOVE_HEAD(&krp_delfree_l, krp_qun.krp_lnext);
   1619      1.36       tls 		pool_put(&cryptkop_pool, krp);
   1620      1.36       tls 	}
   1621      1.36       tls 	return completed;
   1622      1.36       tls }
   1623      1.36       tls 
   1624      1.36       tls static int
   1625      1.36       tls cryptodev_getstatus (struct fcrypt *fcr, struct crypt_result *crypt_res)
   1626      1.36       tls {
   1627      1.36       tls         struct cryptop *crp = NULL;
   1628      1.36       tls         struct cryptkop *krp = NULL;
   1629      1.36       tls         struct csession *cse;
   1630      1.36       tls         int i, size, req = 0;
   1631      1.36       tls 
   1632      1.36       tls 	mutex_spin_enter(&crypto_mtx);
   1633      1.36       tls 	/* Here we dont know for which request the user is requesting the
   1634      1.36       tls 	 * response so checking in both the queues */
   1635      1.36       tls 	TAILQ_FOREACH(crp, &fcr->crp_ret_mq, crp_next) {
   1636      1.36       tls 		if(crp && (crp->crp_reqid == crypt_res->reqid)) {
   1637      1.36       tls 			cse = (struct csession *)crp->crp_opaque;
   1638      1.36       tls 		        crypt_res->opaque = crp->crp_usropaque;
   1639      1.36       tls 			cse = csefind(fcr, cse->ses);
   1640      1.36       tls 			if (cse == NULL) {
   1641      1.36       tls 				DPRINTF(("csefind failed\n"));
   1642      1.36       tls 				crypt_res->status = EINVAL;
   1643      1.36       tls 				goto bail;
   1644      1.36       tls 			}
   1645      1.36       tls 
   1646      1.36       tls 			if (crp->crp_etype != 0) {
   1647      1.36       tls 				crypt_res->status = crp->crp_etype;
   1648      1.36       tls 				goto bail;
   1649      1.36       tls 			}
   1650      1.36       tls 
   1651      1.36       tls 			if (cse->error) {
   1652      1.36       tls 				crypt_res->status = cse->error;
   1653      1.36       tls 				goto bail;
   1654      1.36       tls 			}
   1655      1.36       tls 
   1656  1.41.2.1  wrstuden 			if (crp->dst && (crypt_res->status =
   1657  1.41.2.1  wrstuden 			    copyout(crp->uio.uio_iov[0].iov_base,
   1658  1.41.2.1  wrstuden 			    crp->dst, crp->len)))
   1659      1.36       tls 				goto bail;
   1660      1.36       tls 
   1661  1.41.2.1  wrstuden 			if (crp->mac && (crypt_res->status =
   1662  1.41.2.1  wrstuden 			    copyout(crp->crp_mac, crp->mac,
   1663  1.41.2.1  wrstuden 			    cse->thash->authsize)))
   1664      1.36       tls 				goto bail;
   1665      1.36       tls bail:
   1666      1.36       tls 			TAILQ_REMOVE(&fcr->crp_ret_mq, crp, crp_next);
   1667      1.36       tls 
   1668      1.36       tls 			mutex_spin_exit(&crypto_mtx);
   1669      1.36       tls 			crypto_freereq(crp);
   1670      1.36       tls 			return 0;
   1671      1.36       tls 		}
   1672      1.36       tls 	}
   1673      1.36       tls 
   1674      1.36       tls 	TAILQ_FOREACH(krp, &fcr->crp_ret_mkq, krp_next) {
   1675      1.36       tls 		if(krp && (krp->krp_reqid == crypt_res->reqid)) {
   1676      1.36       tls 			crypt_res[req].opaque = krp->krp_usropaque;
   1677      1.36       tls 			if (krp->krp_status != 0) {
   1678      1.36       tls 				DPRINTF(("cryptodev_key: "
   1679  1.41.2.1  wrstuden 				    "krp->krp_status 0x%08x\n",
   1680  1.41.2.1  wrstuden 				    krp->krp_status));
   1681      1.36       tls 				crypt_res[req].status = krp->krp_status;
   1682      1.36       tls 				goto fail;
   1683      1.36       tls 			}
   1684      1.36       tls 
   1685  1.41.2.1  wrstuden 			for (i = krp->krp_iparams; i < krp->krp_iparams +
   1686  1.41.2.1  wrstuden 			    krp->krp_oparams; i++) {
   1687      1.36       tls 				size = (krp->krp_param[i].crp_nbits + 7) / 8;
   1688      1.36       tls 				if (size == 0)
   1689      1.36       tls 					continue;
   1690  1.41.2.1  wrstuden 				crypt_res[req].status = copyout(
   1691  1.41.2.1  wrstuden 				    krp->krp_param[i].crp_p,
   1692  1.41.2.1  wrstuden 				    krp->crk_param[i].crp_p, size);
   1693      1.36       tls 				if (crypt_res[req].status) {
   1694      1.36       tls 					DPRINTF(("cryptodev_key: copyout oparam"
   1695  1.41.2.1  wrstuden 					    "%d failed, error=%d\n",
   1696  1.41.2.1  wrstuden 					    i - krp->krp_iparams,
   1697  1.41.2.1  wrstuden 					    crypt_res[req].status));
   1698      1.36       tls 					goto fail;
   1699      1.36       tls 				}
   1700      1.36       tls 			}
   1701      1.36       tls fail:
   1702      1.36       tls 			TAILQ_REMOVE(&fcr->crp_ret_mkq, krp, krp_next);
   1703      1.36       tls 			mutex_spin_exit(&crypto_mtx);
   1704      1.36       tls 			/* not sure what to do for this */
   1705      1.36       tls 			/* kop[req].crk_status = krp->krp_status; */
   1706      1.36       tls 			for (i = 0; i < CRK_MAXPARAM; i++) {
   1707      1.36       tls 				struct crparam *kp = &(krp->krp_param[i]);
   1708      1.36       tls 				if (kp->crp_p) {
   1709      1.36       tls 					size = (kp->crp_nbits + 7) / 8;
   1710      1.36       tls 					KASSERT(size > 0);
   1711      1.36       tls 					memset(kp->crp_p, 0, size);
   1712      1.36       tls 					kmem_free(kp->crp_p, size);
   1713      1.36       tls 				}
   1714      1.36       tls 			}
   1715      1.36       tls 			pool_put(&cryptkop_pool, krp);
   1716      1.36       tls 			return 0;
   1717      1.36       tls 		}
   1718      1.36       tls 	}
   1719      1.36       tls 	mutex_spin_exit(&crypto_mtx);
   1720      1.36       tls 	return EINPROGRESS;
   1721      1.36       tls }
   1722      1.36       tls 
   1723      1.36       tls static int
   1724      1.36       tls cryptof_poll(struct file *fp, int events)
   1725      1.36       tls {
   1726      1.36       tls 	struct fcrypt *fcr = (struct fcrypt *)fp->f_data;
   1727      1.38     rmind 	int revents = 0;
   1728      1.36       tls 
   1729      1.36       tls 	if (!(events & (POLLIN | POLLRDNORM))) {
   1730      1.36       tls 		/* only support read and POLLIN */
   1731      1.36       tls 		return 0;
   1732      1.36       tls 	}
   1733      1.36       tls 
   1734      1.38     rmind 	mutex_spin_enter(&crypto_mtx);
   1735      1.36       tls 	if (TAILQ_EMPTY(&fcr->crp_ret_mq) && TAILQ_EMPTY(&fcr->crp_ret_mkq)) {
   1736      1.38     rmind 		/* no completed requests pending, save the poll for later */
   1737      1.36       tls 		selrecord(curlwp, &fcr->sinfo);
   1738      1.36       tls 	} else {
   1739      1.36       tls 		/* let the app(s) know that there are completed requests */
   1740      1.38     rmind 		revents = events & (POLLIN | POLLRDNORM);
   1741      1.36       tls 	}
   1742      1.38     rmind 	mutex_spin_exit(&crypto_mtx);
   1743      1.38     rmind 
   1744      1.38     rmind 	return revents;
   1745      1.36       tls }
   1746      1.36       tls 
   1747      1.15   thorpej /*
   1748      1.15   thorpej  * Pseudo-device initialization routine for /dev/crypto
   1749      1.15   thorpej  */
   1750      1.15   thorpej void	cryptoattach(int);
   1751      1.15   thorpej 
   1752      1.15   thorpej void
   1753      1.25  christos cryptoattach(int num)
   1754      1.15   thorpej {
   1755      1.29       tls 	pool_init(&fcrpl, sizeof(struct fcrypt), 0, 0, 0, "fcrpl",
   1756  1.41.2.1  wrstuden 	    NULL, IPL_NET);	/* XXX IPL_NET ("splcrypto") */
   1757      1.29       tls 	pool_init(&csepl, sizeof(struct csession), 0, 0, 0, "csepl",
   1758  1.41.2.1  wrstuden 	    NULL, IPL_NET);	/* XXX IPL_NET ("splcrypto") */
   1759      1.29       tls 
   1760      1.29       tls 	/*
   1761      1.29       tls 	 * Preallocate space for 64 users, with 5 sessions each.
   1762      1.29       tls 	 * (consider that a TLS protocol session requires at least
   1763      1.29       tls 	 * 3DES, MD5, and SHA1 (both hashes are used in the PRF) for
   1764      1.29       tls 	 * the negotiation, plus HMAC_SHA1 for the actual SSL records,
   1765      1.29       tls 	 * consuming one session here for each algorithm.
   1766      1.29       tls 	 */
   1767      1.29       tls 	pool_prime(&fcrpl, 64);
   1768      1.29       tls 	pool_prime(&csepl, 64 * 5);
   1769      1.15   thorpej }
   1770