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