Home | History | Annotate | Line # | Download | only in opencrypto
cryptodev.c revision 1.16.2.1
      1  1.16.2.1      yamt /*	$NetBSD: cryptodev.c,v 1.16.2.1 2006/02/02 14:45:12 yamt Exp $ */
      2       1.4  jonathan /*	$FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.4.2.4 2003/06/03 00:09:02 sam Exp $	*/
      3       1.1  jonathan /*	$OpenBSD: cryptodev.c,v 1.53 2002/07/10 22:21:30 mickey Exp $	*/
      4       1.1  jonathan 
      5       1.1  jonathan /*
      6       1.1  jonathan  * Copyright (c) 2001 Theo de Raadt
      7       1.1  jonathan  *
      8       1.1  jonathan  * Redistribution and use in source and binary forms, with or without
      9       1.1  jonathan  * modification, are permitted provided that the following conditions
     10       1.1  jonathan  * are met:
     11       1.1  jonathan  *
     12       1.1  jonathan  * 1. Redistributions of source code must retain the above copyright
     13       1.1  jonathan  *   notice, this list of conditions and the following disclaimer.
     14       1.1  jonathan  * 2. Redistributions in binary form must reproduce the above copyright
     15       1.1  jonathan  *   notice, this list of conditions and the following disclaimer in the
     16       1.1  jonathan  *   documentation and/or other materials provided with the distribution.
     17       1.1  jonathan  * 3. The name of the author may not be used to endorse or promote products
     18       1.1  jonathan  *   derived from this software without specific prior written permission.
     19       1.1  jonathan  *
     20       1.1  jonathan  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21       1.1  jonathan  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22       1.1  jonathan  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23       1.1  jonathan  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24       1.1  jonathan  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25       1.1  jonathan  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26       1.1  jonathan  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27       1.1  jonathan  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28       1.1  jonathan  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29       1.1  jonathan  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30       1.1  jonathan  *
     31       1.1  jonathan  * Effort sponsored in part by the Defense Advanced Research Projects
     32       1.1  jonathan  * Agency (DARPA) and Air Force Research Laboratory, Air Force
     33       1.1  jonathan  * Materiel Command, USAF, under agreement number F30602-01-2-0537.
     34       1.1  jonathan  *
     35       1.1  jonathan  */
     36       1.1  jonathan 
     37       1.1  jonathan #include <sys/cdefs.h>
     38  1.16.2.1      yamt __KERNEL_RCSID(0, "$NetBSD: cryptodev.c,v 1.16.2.1 2006/02/02 14:45:12 yamt Exp $");
     39       1.1  jonathan 
     40       1.1  jonathan #include <sys/param.h>
     41       1.1  jonathan #include <sys/systm.h>
     42       1.1  jonathan #include <sys/malloc.h>
     43       1.1  jonathan #include <sys/mbuf.h>
     44       1.1  jonathan #include <sys/sysctl.h>
     45       1.1  jonathan #include <sys/file.h>
     46       1.1  jonathan #include <sys/filedesc.h>
     47       1.1  jonathan #include <sys/errno.h>
     48       1.2  jonathan #include <sys/md5.h>
     49       1.1  jonathan #include <sys/sha1.h>
     50       1.1  jonathan #include <sys/conf.h>
     51       1.1  jonathan #include <sys/device.h>
     52       1.1  jonathan 
     53       1.1  jonathan #include <opencrypto/cryptodev.h>
     54       1.1  jonathan #include <opencrypto/xform.h>
     55       1.1  jonathan 
     56       1.1  jonathan #ifdef __NetBSD__
     57       1.1  jonathan   #define splcrypto splnet
     58       1.1  jonathan #endif
     59       1.1  jonathan 
     60       1.1  jonathan struct csession {
     61       1.1  jonathan 	TAILQ_ENTRY(csession) next;
     62       1.1  jonathan 	u_int64_t	sid;
     63       1.1  jonathan 	u_int32_t	ses;
     64       1.1  jonathan 
     65       1.1  jonathan 	u_int32_t	cipher;
     66       1.1  jonathan 	struct enc_xform *txform;
     67       1.1  jonathan 	u_int32_t	mac;
     68       1.1  jonathan 	struct auth_hash *thash;
     69       1.1  jonathan 
     70       1.1  jonathan 	caddr_t		key;
     71       1.1  jonathan 	int		keylen;
     72       1.1  jonathan 	u_char		tmp_iv[EALG_MAX_BLOCK_LEN];
     73       1.1  jonathan 
     74       1.1  jonathan 	caddr_t		mackey;
     75       1.1  jonathan 	int		mackeylen;
     76       1.1  jonathan 	u_char		tmp_mac[CRYPTO_MAX_MAC_LEN];
     77       1.1  jonathan 
     78       1.1  jonathan 	struct iovec	iovec[IOV_MAX];
     79       1.1  jonathan 	struct uio	uio;
     80       1.1  jonathan 	int		error;
     81       1.1  jonathan };
     82       1.1  jonathan 
     83       1.1  jonathan struct fcrypt {
     84       1.1  jonathan 	TAILQ_HEAD(csessionlist, csession) csessions;
     85       1.1  jonathan 	int		sesn;
     86       1.1  jonathan };
     87       1.1  jonathan 
     88       1.1  jonathan 
     89       1.1  jonathan /* Declaration of master device (fd-cloning/ctxt-allocating) entrypoints */
     90      1.16  christos static int	cryptoopen(dev_t dev, int flag, int mode, struct lwp *l);
     91       1.1  jonathan static int	cryptoread(dev_t dev, struct uio *uio, int ioflag);
     92       1.1  jonathan static int	cryptowrite(dev_t dev, struct uio *uio, int ioflag);
     93      1.16  christos static int	cryptoioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l);
     94      1.16  christos static int	cryptoselect(dev_t dev, int rw, struct lwp *l);
     95       1.1  jonathan 
     96       1.1  jonathan /* Declaration of cloned-device (per-ctxt) entrypoints */
     97       1.1  jonathan static int	cryptof_read(struct file *, off_t *, struct uio *, struct ucred *, int);
     98       1.1  jonathan static int	cryptof_write(struct file *, off_t *, struct uio *, struct ucred *, int);
     99      1.16  christos static int	cryptof_ioctl(struct file *, u_long, void*, struct lwp *l);
    100      1.16  christos static int	cryptof_close(struct file *, struct lwp *);
    101       1.1  jonathan 
    102      1.12  christos static const struct fileops cryptofops = {
    103       1.1  jonathan     cryptof_read,
    104       1.1  jonathan     cryptof_write,
    105       1.1  jonathan     cryptof_ioctl,
    106      1.12  christos     fnullop_fcntl,
    107      1.12  christos     fnullop_poll,
    108      1.12  christos     fbadop_stat,
    109       1.1  jonathan     cryptof_close,
    110      1.12  christos     fnullop_kqfilter
    111       1.1  jonathan };
    112       1.1  jonathan 
    113       1.1  jonathan static struct	csession *csefind(struct fcrypt *, u_int);
    114       1.1  jonathan static int	csedelete(struct fcrypt *, struct csession *);
    115       1.1  jonathan static struct	csession *cseadd(struct fcrypt *, struct csession *);
    116       1.1  jonathan static struct	csession *csecreate(struct fcrypt *, u_int64_t, caddr_t, u_int64_t,
    117       1.1  jonathan     caddr_t, u_int64_t, u_int32_t, u_int32_t, struct enc_xform *,
    118       1.1  jonathan     struct auth_hash *);
    119       1.1  jonathan static int	csefree(struct csession *);
    120       1.1  jonathan 
    121      1.16  christos static int	cryptodev_op(struct csession *, struct crypt_op *, struct lwp *);
    122       1.1  jonathan static int	cryptodev_key(struct crypt_kop *);
    123       1.1  jonathan int	cryptodev_dokey(struct crypt_kop *kop, struct crparam kvp[]);
    124       1.1  jonathan 
    125       1.1  jonathan static int	cryptodev_cb(void *);
    126       1.1  jonathan static int	cryptodevkey_cb(void *);
    127       1.1  jonathan 
    128       1.9  jonathan /*
    129       1.9  jonathan  * sysctl-able control variables for /dev/crypto now defined in crypto.c:
    130       1.9  jonathan  * crypto_usercrypto, crypto_userasmcrypto, crypto_devallowsoft.
    131       1.9  jonathan  */
    132       1.1  jonathan 
    133       1.1  jonathan /* ARGSUSED */
    134       1.1  jonathan int
    135       1.1  jonathan cryptof_read(struct file *fp, off_t *poff, struct uio *uio,
    136       1.1  jonathan 	     struct ucred *cred, int flags)
    137       1.1  jonathan {
    138       1.1  jonathan 	return (EIO);
    139       1.1  jonathan }
    140       1.1  jonathan 
    141       1.1  jonathan /* ARGSUSED */
    142       1.1  jonathan int
    143       1.1  jonathan cryptof_write(struct file *fp, off_t *poff, struct uio *uio,
    144       1.1  jonathan 	      struct ucred *cred, int flags)
    145       1.1  jonathan {
    146       1.1  jonathan 	return (EIO);
    147       1.1  jonathan }
    148       1.1  jonathan 
    149       1.1  jonathan /* ARGSUSED */
    150       1.1  jonathan int
    151      1.16  christos cryptof_ioctl(struct file *fp, u_long cmd, void* data, struct lwp *l)
    152       1.1  jonathan {
    153       1.1  jonathan 	struct cryptoini cria, crie;
    154       1.1  jonathan 	struct fcrypt *fcr = (struct fcrypt *)fp->f_data;
    155       1.1  jonathan 	struct csession *cse;
    156       1.1  jonathan 	struct session_op *sop;
    157       1.1  jonathan 	struct crypt_op *cop;
    158       1.1  jonathan 	struct enc_xform *txform = NULL;
    159       1.1  jonathan 	struct auth_hash *thash = NULL;
    160       1.1  jonathan 	u_int64_t sid;
    161       1.1  jonathan 	u_int32_t ses;
    162       1.1  jonathan 	int error = 0;
    163       1.1  jonathan 
    164       1.1  jonathan 	switch (cmd) {
    165       1.1  jonathan 	case CIOCGSESSION:
    166       1.1  jonathan 		sop = (struct session_op *)data;
    167       1.1  jonathan 		switch (sop->cipher) {
    168       1.1  jonathan 		case 0:
    169       1.1  jonathan 			break;
    170       1.1  jonathan 		case CRYPTO_DES_CBC:
    171       1.1  jonathan 			txform = &enc_xform_des;
    172       1.1  jonathan 			break;
    173       1.1  jonathan 		case CRYPTO_3DES_CBC:
    174       1.1  jonathan 			txform = &enc_xform_3des;
    175       1.1  jonathan 			break;
    176       1.1  jonathan 		case CRYPTO_BLF_CBC:
    177       1.1  jonathan 			txform = &enc_xform_blf;
    178       1.1  jonathan 			break;
    179       1.1  jonathan 		case CRYPTO_CAST_CBC:
    180       1.1  jonathan 			txform = &enc_xform_cast5;
    181       1.1  jonathan 			break;
    182       1.1  jonathan 		case CRYPTO_SKIPJACK_CBC:
    183       1.1  jonathan 			txform = &enc_xform_skipjack;
    184       1.1  jonathan 			break;
    185       1.1  jonathan 		case CRYPTO_AES_CBC:
    186       1.1  jonathan 			txform = &enc_xform_rijndael128;
    187       1.1  jonathan 			break;
    188       1.1  jonathan 		case CRYPTO_NULL_CBC:
    189       1.1  jonathan 			txform = &enc_xform_null;
    190       1.1  jonathan 			break;
    191       1.1  jonathan 		case CRYPTO_ARC4:
    192       1.1  jonathan 			txform = &enc_xform_arc4;
    193       1.1  jonathan 			break;
    194       1.1  jonathan 		default:
    195       1.1  jonathan 			return (EINVAL);
    196       1.1  jonathan 		}
    197       1.1  jonathan 
    198       1.1  jonathan 		switch (sop->mac) {
    199       1.1  jonathan 		case 0:
    200       1.1  jonathan 			break;
    201       1.1  jonathan 		case CRYPTO_MD5_HMAC:
    202       1.1  jonathan 			thash = &auth_hash_hmac_md5_96;
    203       1.1  jonathan 			break;
    204       1.1  jonathan 		case CRYPTO_SHA1_HMAC:
    205       1.1  jonathan 			thash = &auth_hash_hmac_sha1_96;
    206       1.1  jonathan 			break;
    207       1.1  jonathan 		case CRYPTO_SHA2_HMAC:
    208       1.1  jonathan 			if (sop->mackeylen == auth_hash_hmac_sha2_256.keysize)
    209       1.1  jonathan 				thash = &auth_hash_hmac_sha2_256;
    210       1.1  jonathan 			else if (sop->mackeylen == auth_hash_hmac_sha2_384.keysize)
    211       1.1  jonathan 				thash = &auth_hash_hmac_sha2_384;
    212       1.1  jonathan 			else if (sop->mackeylen == auth_hash_hmac_sha2_512.keysize)
    213       1.1  jonathan 				thash = &auth_hash_hmac_sha2_512;
    214       1.1  jonathan 			else
    215       1.1  jonathan 				return (EINVAL);
    216       1.1  jonathan 			break;
    217       1.1  jonathan 		case CRYPTO_RIPEMD160_HMAC:
    218       1.1  jonathan 			thash = &auth_hash_hmac_ripemd_160_96;
    219       1.1  jonathan 			break;
    220       1.1  jonathan 		case CRYPTO_MD5:
    221       1.1  jonathan 			thash = &auth_hash_md5;
    222       1.1  jonathan 			break;
    223       1.1  jonathan 		case CRYPTO_SHA1:
    224       1.1  jonathan 			thash = &auth_hash_sha1;
    225       1.1  jonathan 			break;
    226       1.1  jonathan 		case CRYPTO_NULL_HMAC:
    227       1.1  jonathan 			thash = &auth_hash_null;
    228       1.1  jonathan 			break;
    229       1.1  jonathan 		default:
    230       1.1  jonathan 			return (EINVAL);
    231       1.1  jonathan 		}
    232       1.1  jonathan 
    233       1.1  jonathan 		bzero(&crie, sizeof(crie));
    234       1.1  jonathan 		bzero(&cria, sizeof(cria));
    235       1.1  jonathan 
    236       1.1  jonathan 		if (txform) {
    237       1.1  jonathan 			crie.cri_alg = txform->type;
    238       1.1  jonathan 			crie.cri_klen = sop->keylen * 8;
    239       1.1  jonathan 			if (sop->keylen > txform->maxkey ||
    240       1.1  jonathan 			    sop->keylen < txform->minkey) {
    241       1.1  jonathan 				error = EINVAL;
    242       1.1  jonathan 				goto bail;
    243       1.1  jonathan 			}
    244       1.1  jonathan 
    245       1.1  jonathan 			MALLOC(crie.cri_key, u_int8_t *,
    246       1.1  jonathan 			    crie.cri_klen / 8, M_XDATA, M_WAITOK);
    247       1.1  jonathan 			if ((error = copyin(sop->key, crie.cri_key,
    248       1.1  jonathan 			    crie.cri_klen / 8)))
    249       1.1  jonathan 				goto bail;
    250       1.1  jonathan 			if (thash)
    251       1.1  jonathan 				crie.cri_next = &cria;
    252       1.1  jonathan 		}
    253       1.1  jonathan 
    254       1.1  jonathan 		if (thash) {
    255       1.1  jonathan 			cria.cri_alg = thash->type;
    256       1.1  jonathan 			cria.cri_klen = sop->mackeylen * 8;
    257       1.1  jonathan 			if (sop->mackeylen != thash->keysize) {
    258       1.1  jonathan 				error = EINVAL;
    259       1.1  jonathan 				goto bail;
    260       1.1  jonathan 			}
    261       1.1  jonathan 
    262       1.1  jonathan 			if (cria.cri_klen) {
    263       1.1  jonathan 				MALLOC(cria.cri_key, u_int8_t *,
    264       1.1  jonathan 				    cria.cri_klen / 8, M_XDATA, M_WAITOK);
    265       1.1  jonathan 				if ((error = copyin(sop->mackey, cria.cri_key,
    266       1.1  jonathan 				    cria.cri_klen / 8)))
    267       1.1  jonathan 					goto bail;
    268       1.1  jonathan 			}
    269       1.1  jonathan 		}
    270       1.1  jonathan 
    271       1.1  jonathan 		error = crypto_newsession(&sid, (txform ? &crie : &cria),
    272       1.9  jonathan 			    crypto_devallowsoft);
    273       1.1  jonathan 		if (error) {
    274      1.10  jonathan #ifdef CRYPTO_DEBUG
    275       1.1  jonathan 		  	printf("SIOCSESSION violates kernel parameters\n");
    276      1.10  jonathan #endif
    277       1.1  jonathan 			goto bail;
    278       1.1  jonathan 		}
    279       1.1  jonathan 
    280       1.1  jonathan 		cse = csecreate(fcr, sid, crie.cri_key, crie.cri_klen,
    281       1.1  jonathan 		    cria.cri_key, cria.cri_klen, sop->cipher, sop->mac, txform,
    282       1.1  jonathan 		    thash);
    283       1.1  jonathan 
    284       1.1  jonathan 		if (cse == NULL) {
    285       1.1  jonathan 			crypto_freesession(sid);
    286       1.1  jonathan 			error = EINVAL;
    287       1.1  jonathan 			goto bail;
    288       1.1  jonathan 		}
    289       1.1  jonathan 		sop->ses = cse->ses;
    290       1.1  jonathan 
    291       1.1  jonathan bail:
    292       1.1  jonathan 		if (error) {
    293       1.1  jonathan 			if (crie.cri_key)
    294       1.1  jonathan 				FREE(crie.cri_key, M_XDATA);
    295       1.1  jonathan 			if (cria.cri_key)
    296       1.1  jonathan 				FREE(cria.cri_key, M_XDATA);
    297       1.1  jonathan 		}
    298       1.1  jonathan 		break;
    299       1.1  jonathan 	case CIOCFSESSION:
    300       1.1  jonathan 		ses = *(u_int32_t *)data;
    301       1.1  jonathan 		cse = csefind(fcr, ses);
    302       1.1  jonathan 		if (cse == NULL)
    303       1.1  jonathan 			return (EINVAL);
    304       1.1  jonathan 		csedelete(fcr, cse);
    305       1.1  jonathan 		error = csefree(cse);
    306       1.1  jonathan 		break;
    307       1.1  jonathan 	case CIOCCRYPT:
    308       1.1  jonathan 		cop = (struct crypt_op *)data;
    309       1.1  jonathan 		cse = csefind(fcr, cop->ses);
    310       1.1  jonathan 		if (cse == NULL)
    311       1.1  jonathan 			return (EINVAL);
    312      1.16  christos 		error = cryptodev_op(cse, cop, l);
    313       1.1  jonathan 		break;
    314       1.1  jonathan 	case CIOCKEY:
    315       1.1  jonathan 		error = cryptodev_key((struct crypt_kop *)data);
    316       1.1  jonathan 		break;
    317       1.1  jonathan 	case CIOCASYMFEAT:
    318       1.1  jonathan 		error = crypto_getfeat((int *)data);
    319       1.1  jonathan 		break;
    320       1.1  jonathan 	default:
    321       1.1  jonathan 		error = EINVAL;
    322       1.1  jonathan 	}
    323       1.1  jonathan 	return (error);
    324       1.1  jonathan }
    325       1.1  jonathan 
    326       1.1  jonathan static int
    327      1.16  christos cryptodev_op(struct csession *cse, struct crypt_op *cop, struct lwp *l)
    328       1.1  jonathan {
    329       1.1  jonathan 	struct cryptop *crp = NULL;
    330       1.1  jonathan 	struct cryptodesc *crde = NULL, *crda = NULL;
    331       1.1  jonathan 	int i, error, s;
    332       1.1  jonathan 
    333       1.1  jonathan 	if (cop->len > 256*1024-4)
    334       1.1  jonathan 		return (E2BIG);
    335       1.1  jonathan 
    336      1.13  jonathan 	if (cse->txform) {
    337      1.13  jonathan 		if (cop->len == 0 || (cop->len % cse->txform->blocksize) != 0)
    338      1.13  jonathan 			return (EINVAL);
    339      1.13  jonathan 	}
    340       1.1  jonathan 
    341       1.1  jonathan 	bzero(&cse->uio, sizeof(cse->uio));
    342       1.1  jonathan 	cse->uio.uio_iovcnt = 1;
    343       1.1  jonathan 	cse->uio.uio_resid = 0;
    344       1.1  jonathan 	cse->uio.uio_rw = UIO_WRITE;
    345       1.1  jonathan 	cse->uio.uio_iov = cse->iovec;
    346  1.16.2.1      yamt 	UIO_SETUP_SYSSPACE(&cse->uio);
    347       1.1  jonathan 	bzero(&cse->iovec, sizeof(cse->iovec));
    348       1.1  jonathan 	cse->uio.uio_iov[0].iov_len = cop->len;
    349       1.1  jonathan 	cse->uio.uio_iov[0].iov_base = malloc(cop->len, M_XDATA, M_WAITOK);
    350       1.1  jonathan 	for (i = 0; i < cse->uio.uio_iovcnt; i++)
    351       1.1  jonathan 		cse->uio.uio_resid += cse->uio.uio_iov[0].iov_len;
    352       1.1  jonathan 
    353       1.1  jonathan 	crp = crypto_getreq((cse->txform != NULL) + (cse->thash != NULL));
    354       1.1  jonathan 	if (crp == NULL) {
    355       1.1  jonathan 		error = ENOMEM;
    356       1.1  jonathan 		goto bail;
    357       1.1  jonathan 	}
    358       1.1  jonathan 
    359       1.1  jonathan 	if (cse->thash) {
    360       1.1  jonathan 		crda = crp->crp_desc;
    361       1.1  jonathan 		if (cse->txform)
    362       1.1  jonathan 			crde = crda->crd_next;
    363       1.1  jonathan 	} else {
    364       1.1  jonathan 		if (cse->txform)
    365       1.1  jonathan 			crde = crp->crp_desc;
    366       1.1  jonathan 		else {
    367       1.1  jonathan 			error = EINVAL;
    368       1.1  jonathan 			goto bail;
    369       1.1  jonathan 		}
    370       1.1  jonathan 	}
    371       1.1  jonathan 
    372       1.1  jonathan 	if ((error = copyin(cop->src, cse->uio.uio_iov[0].iov_base, cop->len)))
    373       1.1  jonathan 		goto bail;
    374       1.1  jonathan 
    375       1.1  jonathan 	if (crda) {
    376       1.1  jonathan 		crda->crd_skip = 0;
    377       1.1  jonathan 		crda->crd_len = cop->len;
    378       1.1  jonathan 		crda->crd_inject = 0;	/* ??? */
    379       1.1  jonathan 
    380       1.1  jonathan 		crda->crd_alg = cse->mac;
    381       1.1  jonathan 		crda->crd_key = cse->mackey;
    382       1.1  jonathan 		crda->crd_klen = cse->mackeylen * 8;
    383       1.1  jonathan 	}
    384       1.1  jonathan 
    385       1.1  jonathan 	if (crde) {
    386       1.1  jonathan 		if (cop->op == COP_ENCRYPT)
    387       1.1  jonathan 			crde->crd_flags |= CRD_F_ENCRYPT;
    388       1.1  jonathan 		else
    389       1.1  jonathan 			crde->crd_flags &= ~CRD_F_ENCRYPT;
    390       1.1  jonathan 		crde->crd_len = cop->len;
    391       1.1  jonathan 		crde->crd_inject = 0;
    392       1.1  jonathan 
    393       1.1  jonathan 		crde->crd_alg = cse->cipher;
    394       1.1  jonathan 		crde->crd_key = cse->key;
    395       1.1  jonathan 		crde->crd_klen = cse->keylen * 8;
    396       1.1  jonathan 	}
    397       1.1  jonathan 
    398       1.1  jonathan 	crp->crp_ilen = cop->len;
    399       1.1  jonathan 	crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM
    400       1.1  jonathan 		       | (cop->flags & COP_F_BATCH);
    401       1.1  jonathan 	crp->crp_buf = (caddr_t)&cse->uio;
    402       1.1  jonathan 	crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_cb;
    403       1.1  jonathan 	crp->crp_sid = cse->sid;
    404       1.1  jonathan 	crp->crp_opaque = (void *)cse;
    405       1.1  jonathan 
    406       1.1  jonathan 	if (cop->iv) {
    407       1.1  jonathan 		if (crde == NULL) {
    408       1.1  jonathan 			error = EINVAL;
    409       1.1  jonathan 			goto bail;
    410       1.1  jonathan 		}
    411       1.1  jonathan 		if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
    412       1.1  jonathan 			error = EINVAL;
    413       1.1  jonathan 			goto bail;
    414       1.1  jonathan 		}
    415       1.1  jonathan 		if ((error = copyin(cop->iv, cse->tmp_iv, cse->txform->blocksize)))
    416       1.1  jonathan 			goto bail;
    417       1.1  jonathan 		bcopy(cse->tmp_iv, crde->crd_iv, cse->txform->blocksize);
    418       1.1  jonathan 		crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
    419       1.1  jonathan 		crde->crd_skip = 0;
    420       1.1  jonathan 	} else if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
    421       1.1  jonathan 		crde->crd_skip = 0;
    422       1.1  jonathan 	} else if (crde) {
    423       1.1  jonathan 		crde->crd_flags |= CRD_F_IV_PRESENT;
    424       1.1  jonathan 		crde->crd_skip = cse->txform->blocksize;
    425       1.1  jonathan 		crde->crd_len -= cse->txform->blocksize;
    426       1.1  jonathan 	}
    427       1.1  jonathan 
    428       1.1  jonathan 	if (cop->mac) {
    429       1.1  jonathan 		if (crda == NULL) {
    430       1.1  jonathan 			error = EINVAL;
    431       1.1  jonathan 			goto bail;
    432       1.1  jonathan 		}
    433       1.1  jonathan 		crp->crp_mac=cse->tmp_mac;
    434       1.1  jonathan 	}
    435       1.1  jonathan 
    436       1.1  jonathan 	s = splcrypto();	/* NB: only needed with CRYPTO_F_CBIMM */
    437       1.1  jonathan 	error = crypto_dispatch(crp);
    438       1.4  jonathan 	if (error == 0 && (crp->crp_flags & CRYPTO_F_DONE) == 0)
    439       1.1  jonathan 		error = tsleep(crp, PSOCK, "crydev", 0);
    440       1.1  jonathan 	splx(s);
    441       1.1  jonathan 	if (error) {
    442       1.1  jonathan 		goto bail;
    443       1.1  jonathan 	}
    444       1.1  jonathan 
    445       1.1  jonathan 	if (crp->crp_etype != 0) {
    446       1.1  jonathan 		error = crp->crp_etype;
    447       1.1  jonathan 		goto bail;
    448       1.1  jonathan 	}
    449       1.1  jonathan 
    450       1.1  jonathan 	if (cse->error) {
    451       1.1  jonathan 		error = cse->error;
    452       1.1  jonathan 		goto bail;
    453       1.1  jonathan 	}
    454       1.1  jonathan 
    455       1.1  jonathan 	if (cop->dst &&
    456       1.1  jonathan 	    (error = copyout(cse->uio.uio_iov[0].iov_base, cop->dst, cop->len)))
    457       1.1  jonathan 		goto bail;
    458       1.1  jonathan 
    459       1.1  jonathan 	if (cop->mac &&
    460       1.1  jonathan 	    (error = copyout(crp->crp_mac, cop->mac, cse->thash->authsize)))
    461       1.1  jonathan 		goto bail;
    462       1.1  jonathan 
    463       1.1  jonathan bail:
    464       1.1  jonathan 	if (crp)
    465       1.1  jonathan 		crypto_freereq(crp);
    466       1.1  jonathan 	if (cse->uio.uio_iov[0].iov_base)
    467       1.1  jonathan 		free(cse->uio.uio_iov[0].iov_base, M_XDATA);
    468       1.1  jonathan 
    469       1.1  jonathan 	return (error);
    470       1.1  jonathan }
    471       1.1  jonathan 
    472       1.1  jonathan static int
    473       1.1  jonathan cryptodev_cb(void *op)
    474       1.1  jonathan {
    475       1.1  jonathan 	struct cryptop *crp = (struct cryptop *) op;
    476       1.1  jonathan 	struct csession *cse = (struct csession *)crp->crp_opaque;
    477       1.1  jonathan 
    478       1.1  jonathan 	cse->error = crp->crp_etype;
    479       1.1  jonathan 	if (crp->crp_etype == EAGAIN)
    480       1.1  jonathan 		return crypto_dispatch(crp);
    481       1.1  jonathan 	wakeup_one(crp);
    482       1.1  jonathan 	return (0);
    483       1.1  jonathan }
    484       1.1  jonathan 
    485       1.1  jonathan static int
    486       1.1  jonathan cryptodevkey_cb(void *op)
    487       1.1  jonathan {
    488       1.1  jonathan 	struct cryptkop *krp = (struct cryptkop *) op;
    489       1.1  jonathan 
    490       1.1  jonathan 	wakeup_one(krp);
    491       1.1  jonathan 	return (0);
    492       1.1  jonathan }
    493       1.1  jonathan 
    494       1.1  jonathan static int
    495       1.1  jonathan cryptodev_key(struct crypt_kop *kop)
    496       1.1  jonathan {
    497       1.1  jonathan 	struct cryptkop *krp = NULL;
    498       1.1  jonathan 	int error = EINVAL;
    499       1.1  jonathan 	int in, out, size, i;
    500       1.1  jonathan 
    501       1.1  jonathan 	if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
    502       1.1  jonathan 		return (EFBIG);
    503       1.1  jonathan 	}
    504       1.1  jonathan 
    505       1.1  jonathan 	in = kop->crk_iparams;
    506       1.1  jonathan 	out = kop->crk_oparams;
    507       1.1  jonathan 	switch (kop->crk_op) {
    508       1.1  jonathan 	case CRK_MOD_EXP:
    509       1.1  jonathan 		if (in == 3 && out == 1)
    510       1.1  jonathan 			break;
    511       1.1  jonathan 		return (EINVAL);
    512       1.1  jonathan 	case CRK_MOD_EXP_CRT:
    513       1.1  jonathan 		if (in == 6 && out == 1)
    514       1.1  jonathan 			break;
    515       1.1  jonathan 		return (EINVAL);
    516       1.1  jonathan 	case CRK_DSA_SIGN:
    517       1.1  jonathan 		if (in == 5 && out == 2)
    518       1.1  jonathan 			break;
    519       1.1  jonathan 		return (EINVAL);
    520       1.1  jonathan 	case CRK_DSA_VERIFY:
    521       1.1  jonathan 		if (in == 7 && out == 0)
    522       1.1  jonathan 			break;
    523       1.1  jonathan 		return (EINVAL);
    524       1.1  jonathan 	case CRK_DH_COMPUTE_KEY:
    525       1.1  jonathan 		if (in == 3 && out == 1)
    526       1.1  jonathan 			break;
    527       1.1  jonathan 		return (EINVAL);
    528       1.1  jonathan 	default:
    529       1.1  jonathan 		return (EINVAL);
    530       1.1  jonathan 	}
    531       1.1  jonathan 
    532       1.1  jonathan 	krp = (struct cryptkop *)malloc(sizeof *krp, M_XDATA, M_WAITOK);
    533       1.1  jonathan 	if (!krp)
    534       1.1  jonathan 		return (ENOMEM);
    535       1.1  jonathan 	bzero(krp, sizeof *krp);
    536       1.1  jonathan 	krp->krp_op = kop->crk_op;
    537       1.1  jonathan 	krp->krp_status = kop->crk_status;
    538       1.1  jonathan 	krp->krp_iparams = kop->crk_iparams;
    539       1.1  jonathan 	krp->krp_oparams = kop->crk_oparams;
    540       1.1  jonathan 	krp->krp_status = 0;
    541       1.1  jonathan 	krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb;
    542       1.1  jonathan 
    543       1.1  jonathan 	for (i = 0; i < CRK_MAXPARAM; i++)
    544       1.1  jonathan 		krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
    545       1.1  jonathan 	for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
    546       1.1  jonathan 		size = (krp->krp_param[i].crp_nbits + 7) / 8;
    547       1.1  jonathan 		if (size == 0)
    548       1.1  jonathan 			continue;
    549       1.1  jonathan 		MALLOC(krp->krp_param[i].crp_p, caddr_t, size, M_XDATA, M_WAITOK);
    550       1.1  jonathan 		if (i >= krp->krp_iparams)
    551       1.1  jonathan 			continue;
    552       1.1  jonathan 		error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size);
    553       1.1  jonathan 		if (error)
    554       1.1  jonathan 			goto fail;
    555       1.1  jonathan 	}
    556       1.1  jonathan 
    557       1.1  jonathan 	error = crypto_kdispatch(krp);
    558       1.1  jonathan 	if (error == 0)
    559       1.1  jonathan 		error = tsleep(krp, PSOCK, "crydev", 0);
    560       1.1  jonathan 	if (error)
    561       1.1  jonathan 		goto fail;
    562       1.1  jonathan 
    563       1.1  jonathan 	if (krp->krp_status != 0) {
    564       1.1  jonathan 		error = krp->krp_status;
    565       1.1  jonathan 		goto fail;
    566       1.1  jonathan 	}
    567       1.1  jonathan 
    568       1.1  jonathan 	for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
    569       1.1  jonathan 		size = (krp->krp_param[i].crp_nbits + 7) / 8;
    570       1.1  jonathan 		if (size == 0)
    571       1.1  jonathan 			continue;
    572       1.1  jonathan 		error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size);
    573       1.1  jonathan 		if (error)
    574       1.1  jonathan 			goto fail;
    575       1.1  jonathan 	}
    576       1.1  jonathan 
    577       1.1  jonathan fail:
    578       1.1  jonathan 	if (krp) {
    579       1.1  jonathan 		kop->crk_status = krp->krp_status;
    580       1.1  jonathan 		for (i = 0; i < CRK_MAXPARAM; i++) {
    581       1.1  jonathan 			if (krp->krp_param[i].crp_p)
    582       1.1  jonathan 				FREE(krp->krp_param[i].crp_p, M_XDATA);
    583       1.1  jonathan 		}
    584       1.1  jonathan 		free(krp, M_XDATA);
    585       1.1  jonathan 	}
    586       1.1  jonathan 	return (error);
    587       1.1  jonathan }
    588       1.1  jonathan 
    589       1.1  jonathan /* ARGSUSED */
    590       1.1  jonathan static int
    591      1.16  christos cryptof_close(struct file *fp, struct lwp *l)
    592       1.1  jonathan {
    593       1.1  jonathan 	struct fcrypt *fcr = (struct fcrypt *)fp->f_data;
    594       1.1  jonathan 	struct csession *cse;
    595       1.1  jonathan 
    596       1.1  jonathan 	while ((cse = TAILQ_FIRST(&fcr->csessions))) {
    597       1.1  jonathan 		TAILQ_REMOVE(&fcr->csessions, cse, next);
    598       1.1  jonathan 		(void)csefree(cse);
    599       1.1  jonathan 	}
    600       1.1  jonathan 	FREE(fcr, M_XDATA);
    601       1.1  jonathan 
    602       1.1  jonathan 	/* close() stolen from sys/kern/kern_ktrace.c */
    603       1.1  jonathan 
    604       1.1  jonathan 	fp->f_data = NULL;
    605       1.1  jonathan #if 0
    606      1.16  christos 	FILE_UNUSE(fp, l);	/* release file */
    607      1.16  christos 	fdrelease(l, fd); 	/* release fd table slot */
    608       1.1  jonathan #endif
    609       1.1  jonathan 
    610       1.1  jonathan 	return 0;
    611       1.1  jonathan }
    612       1.1  jonathan 
    613       1.1  jonathan static struct csession *
    614       1.1  jonathan csefind(struct fcrypt *fcr, u_int ses)
    615       1.1  jonathan {
    616       1.1  jonathan 	struct csession *cse;
    617       1.1  jonathan 
    618       1.1  jonathan 	TAILQ_FOREACH(cse, &fcr->csessions, next)
    619       1.1  jonathan 		if (cse->ses == ses)
    620       1.1  jonathan 			return (cse);
    621       1.1  jonathan 	return (NULL);
    622       1.1  jonathan }
    623       1.1  jonathan 
    624       1.1  jonathan static int
    625       1.1  jonathan csedelete(struct fcrypt *fcr, struct csession *cse_del)
    626       1.1  jonathan {
    627       1.1  jonathan 	struct csession *cse;
    628       1.1  jonathan 
    629       1.1  jonathan 	TAILQ_FOREACH(cse, &fcr->csessions, next) {
    630       1.1  jonathan 		if (cse == cse_del) {
    631       1.1  jonathan 			TAILQ_REMOVE(&fcr->csessions, cse, next);
    632       1.1  jonathan 			return (1);
    633       1.1  jonathan 		}
    634       1.1  jonathan 	}
    635       1.1  jonathan 	return (0);
    636       1.1  jonathan }
    637       1.1  jonathan 
    638       1.1  jonathan static struct csession *
    639       1.1  jonathan cseadd(struct fcrypt *fcr, struct csession *cse)
    640       1.1  jonathan {
    641       1.1  jonathan 	TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
    642       1.1  jonathan 	cse->ses = fcr->sesn++;
    643       1.1  jonathan 	return (cse);
    644       1.1  jonathan }
    645       1.1  jonathan 
    646       1.1  jonathan static struct csession *
    647       1.1  jonathan csecreate(struct fcrypt *fcr, u_int64_t sid, caddr_t key, u_int64_t keylen,
    648       1.1  jonathan     caddr_t mackey, u_int64_t mackeylen, u_int32_t cipher, u_int32_t mac,
    649       1.1  jonathan     struct enc_xform *txform, struct auth_hash *thash)
    650       1.1  jonathan {
    651       1.1  jonathan 	struct csession *cse;
    652       1.1  jonathan 
    653       1.1  jonathan 	MALLOC(cse, struct csession *, sizeof(struct csession),
    654       1.1  jonathan 	    M_XDATA, M_NOWAIT);
    655       1.1  jonathan 	if (cse == NULL)
    656       1.1  jonathan 		return NULL;
    657       1.1  jonathan 	cse->key = key;
    658       1.1  jonathan 	cse->keylen = keylen/8;
    659       1.1  jonathan 	cse->mackey = mackey;
    660       1.1  jonathan 	cse->mackeylen = mackeylen/8;
    661       1.1  jonathan 	cse->sid = sid;
    662       1.1  jonathan 	cse->cipher = cipher;
    663       1.1  jonathan 	cse->mac = mac;
    664       1.1  jonathan 	cse->txform = txform;
    665       1.1  jonathan 	cse->thash = thash;
    666       1.1  jonathan 	cseadd(fcr, cse);
    667       1.1  jonathan 	return (cse);
    668       1.1  jonathan }
    669       1.1  jonathan 
    670       1.1  jonathan static int
    671       1.1  jonathan csefree(struct csession *cse)
    672       1.1  jonathan {
    673       1.1  jonathan 	int error;
    674       1.1  jonathan 
    675       1.1  jonathan 	error = crypto_freesession(cse->sid);
    676       1.1  jonathan 	if (cse->key)
    677       1.1  jonathan 		FREE(cse->key, M_XDATA);
    678       1.1  jonathan 	if (cse->mackey)
    679       1.1  jonathan 		FREE(cse->mackey, M_XDATA);
    680       1.1  jonathan 	FREE(cse, M_XDATA);
    681       1.1  jonathan 	return (error);
    682       1.1  jonathan }
    683       1.1  jonathan 
    684       1.1  jonathan static int
    685      1.16  christos cryptoopen(dev_t dev, int flag, int mode, struct lwp *l)
    686       1.1  jonathan {
    687       1.1  jonathan 	if (crypto_usercrypto == 0)
    688       1.1  jonathan 		return (ENXIO);
    689       1.1  jonathan 	return (0);
    690       1.1  jonathan }
    691       1.1  jonathan 
    692       1.1  jonathan static int
    693       1.1  jonathan cryptoread(dev_t dev, struct uio *uio, int ioflag)
    694       1.1  jonathan {
    695       1.1  jonathan 	return (EIO);
    696       1.1  jonathan }
    697       1.1  jonathan 
    698       1.1  jonathan static int
    699       1.1  jonathan cryptowrite(dev_t dev, struct uio *uio, int ioflag)
    700       1.1  jonathan {
    701       1.1  jonathan 	return (EIO);
    702       1.1  jonathan }
    703       1.1  jonathan 
    704       1.1  jonathan static int
    705      1.16  christos cryptoioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
    706       1.1  jonathan {
    707       1.1  jonathan 	struct file *f;
    708       1.1  jonathan 	struct fcrypt *fcr;
    709       1.1  jonathan 	int fd, error;
    710       1.1  jonathan 
    711       1.1  jonathan 	switch (cmd) {
    712       1.1  jonathan 	case CRIOGET:
    713       1.1  jonathan 		MALLOC(fcr, struct fcrypt *,
    714       1.1  jonathan 		    sizeof(struct fcrypt), M_XDATA, M_WAITOK);
    715       1.1  jonathan 		TAILQ_INIT(&fcr->csessions);
    716       1.1  jonathan 		fcr->sesn = 0;
    717       1.1  jonathan 
    718      1.16  christos 		error = falloc(l->l_proc, &f, &fd);
    719       1.1  jonathan 		if (error) {
    720       1.1  jonathan 			FREE(fcr, M_XDATA);
    721       1.1  jonathan 			return (error);
    722       1.1  jonathan 		}
    723       1.1  jonathan 		f->f_flag = FREAD | FWRITE;
    724       1.1  jonathan 		f->f_type = DTYPE_CRYPTO;
    725       1.1  jonathan 		f->f_ops = &cryptofops;
    726       1.1  jonathan 		f->f_data = (caddr_t) fcr;
    727       1.1  jonathan 		*(u_int32_t *)data = fd;
    728       1.1  jonathan 		FILE_SET_MATURE(f);
    729      1.16  christos 		FILE_UNUSE(f, l);
    730       1.1  jonathan 		break;
    731       1.1  jonathan 	default:
    732       1.1  jonathan 		error = EINVAL;
    733       1.1  jonathan 		break;
    734       1.1  jonathan 	}
    735       1.1  jonathan 	return (error);
    736       1.1  jonathan }
    737       1.1  jonathan 
    738       1.1  jonathan int
    739      1.16  christos cryptoselect(dev_t dev, int rw, struct lwp *l)
    740       1.1  jonathan {
    741       1.1  jonathan 	return (0);
    742       1.1  jonathan }
    743       1.1  jonathan 
    744       1.1  jonathan /*static*/
    745       1.1  jonathan struct cdevsw crypto_cdevsw = {
    746       1.1  jonathan 	/* open */	cryptoopen,
    747       1.1  jonathan 	/* close */	nullclose,
    748       1.1  jonathan 	/* read */	cryptoread,
    749       1.1  jonathan 	/* write */	cryptowrite,
    750       1.1  jonathan 	/* ioctl */	cryptoioctl,
    751       1.1  jonathan 	/* ttstop?*/	nostop,
    752       1.1  jonathan 	/* ??*/		notty,
    753       1.1  jonathan 	/* poll */	cryptoselect /*nopoll*/,
    754       1.1  jonathan 	/* mmap */	nommap,
    755       1.1  jonathan 	/* kqfilter */	nokqfilter,
    756       1.1  jonathan };
    757       1.1  jonathan 
    758      1.15   thorpej #ifdef __NetBSD__
    759      1.15   thorpej /*
    760      1.15   thorpej  * Pseudo-device initialization routine for /dev/crypto
    761      1.15   thorpej  */
    762      1.15   thorpej void	cryptoattach(int);
    763      1.15   thorpej 
    764      1.15   thorpej void
    765      1.15   thorpej cryptoattach(int num)
    766      1.15   thorpej {
    767      1.15   thorpej 
    768      1.15   thorpej 	/* nothing to do */
    769      1.15   thorpej }
    770      1.15   thorpej #endif /* __NetBSD__ */
    771