cryptodev.c revision 184205
1/*	$OpenBSD: cryptodev.c,v 1.52 2002/06/19 07:22:46 deraadt Exp $	*/
2
3/*-
4 * Copyright (c) 2001 Theo de Raadt
5 * Copyright (c) 2002-2006 Sam Leffler, Errno Consulting
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 *   notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *   notice, this list of conditions and the following disclaimer in the
15 *   documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 *   derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * Effort sponsored in part by the Defense Advanced Research Projects
31 * Agency (DARPA) and Air Force Research Laboratory, Air Force
32 * Materiel Command, USAF, under agreement number F30602-01-2-0537.
33 */
34
35#include <sys/cdefs.h>
36__FBSDID("$FreeBSD: head/sys/opencrypto/cryptodev.c 184205 2008-10-23 15:53:51Z des $");
37
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/malloc.h>
41#include <sys/mbuf.h>
42#include <sys/lock.h>
43#include <sys/mutex.h>
44#include <sys/sysctl.h>
45#include <sys/file.h>
46#include <sys/filedesc.h>
47#include <sys/errno.h>
48#include <sys/uio.h>
49#include <sys/random.h>
50#include <sys/conf.h>
51#include <sys/kernel.h>
52#include <sys/module.h>
53#include <sys/fcntl.h>
54#include <sys/bus.h>
55
56#include <opencrypto/cryptodev.h>
57#include <opencrypto/xform.h>
58
59struct csession {
60	TAILQ_ENTRY(csession) next;
61	u_int64_t	sid;
62	u_int32_t	ses;
63	struct mtx	lock;		/* for op submission */
64
65	u_int32_t	cipher;
66	struct enc_xform *txform;
67	u_int32_t	mac;
68	struct auth_hash *thash;
69
70	caddr_t		key;
71	int		keylen;
72	u_char		tmp_iv[EALG_MAX_BLOCK_LEN];
73
74	caddr_t		mackey;
75	int		mackeylen;
76
77	struct iovec	iovec;
78	struct uio	uio;
79	int		error;
80};
81
82struct fcrypt {
83	TAILQ_HEAD(csessionlist, csession) csessions;
84	int		sesn;
85};
86
87static	int cryptof_rw(struct file *fp, struct uio *uio,
88		    struct ucred *cred, int flags, struct thread *);
89static	int cryptof_truncate(struct file *, off_t, struct ucred *,
90		    struct thread *);
91static	int cryptof_ioctl(struct file *, u_long, void *,
92		    struct ucred *, struct thread *);
93static	int cryptof_poll(struct file *, int, struct ucred *, struct thread *);
94static	int cryptof_kqfilter(struct file *, struct knote *);
95static	int cryptof_stat(struct file *, struct stat *,
96		    struct ucred *, struct thread *);
97static	int cryptof_close(struct file *, struct thread *);
98
99static struct fileops cryptofops = {
100    .fo_read = cryptof_rw,
101    .fo_write = cryptof_rw,
102    .fo_truncate = cryptof_truncate,
103    .fo_ioctl = cryptof_ioctl,
104    .fo_poll = cryptof_poll,
105    .fo_kqfilter = cryptof_kqfilter,
106    .fo_stat = cryptof_stat,
107    .fo_close = cryptof_close
108};
109
110static struct csession *csefind(struct fcrypt *, u_int);
111static int csedelete(struct fcrypt *, struct csession *);
112static struct csession *cseadd(struct fcrypt *, struct csession *);
113static struct csession *csecreate(struct fcrypt *, u_int64_t, caddr_t,
114    u_int64_t, caddr_t, u_int64_t, u_int32_t, u_int32_t, struct enc_xform *,
115    struct auth_hash *);
116static int csefree(struct csession *);
117
118static	int cryptodev_op(struct csession *, struct crypt_op *,
119			struct ucred *, struct thread *td);
120static	int cryptodev_key(struct crypt_kop *);
121static	int cryptodev_find(struct crypt_find_op *);
122
123static int
124cryptof_rw(
125	struct file *fp,
126	struct uio *uio,
127	struct ucred *active_cred,
128	int flags,
129	struct thread *td)
130{
131
132	return (EIO);
133}
134
135static int
136cryptof_truncate(
137	struct file *fp,
138	off_t length,
139	struct ucred *active_cred,
140	struct thread *td)
141{
142
143	return (EINVAL);
144}
145
146/*
147 * Check a crypto identifier to see if it requested
148 * a software device/driver.  This can be done either
149 * by device name/class or through search constraints.
150 */
151static int
152checkforsoftware(int crid)
153{
154	if (crid & CRYPTOCAP_F_SOFTWARE)
155		return EINVAL;		/* XXX */
156	if ((crid & CRYPTOCAP_F_HARDWARE) == 0 &&
157	    (crypto_getcaps(crid) & CRYPTOCAP_F_HARDWARE) == 0)
158		return EINVAL;		/* XXX */
159	return 0;
160}
161
162/* ARGSUSED */
163static int
164cryptof_ioctl(
165	struct file *fp,
166	u_long cmd,
167	void *data,
168	struct ucred *active_cred,
169	struct thread *td)
170{
171#define	SES2(p)	((struct session2_op *)p)
172	struct cryptoini cria, crie;
173	struct fcrypt *fcr = fp->f_data;
174	struct csession *cse;
175	struct session_op *sop;
176	struct crypt_op *cop;
177	struct enc_xform *txform = NULL;
178	struct auth_hash *thash = NULL;
179	struct crypt_kop *kop;
180	u_int64_t sid;
181	u_int32_t ses;
182	int error = 0, crid;
183
184	switch (cmd) {
185	case CIOCGSESSION:
186	case CIOCGSESSION2:
187		sop = (struct session_op *)data;
188		switch (sop->cipher) {
189		case 0:
190			break;
191		case CRYPTO_DES_CBC:
192			txform = &enc_xform_des;
193			break;
194		case CRYPTO_3DES_CBC:
195			txform = &enc_xform_3des;
196			break;
197		case CRYPTO_BLF_CBC:
198			txform = &enc_xform_blf;
199			break;
200		case CRYPTO_CAST_CBC:
201			txform = &enc_xform_cast5;
202			break;
203		case CRYPTO_SKIPJACK_CBC:
204			txform = &enc_xform_skipjack;
205			break;
206		case CRYPTO_AES_CBC:
207			txform = &enc_xform_rijndael128;
208			break;
209		case CRYPTO_NULL_CBC:
210			txform = &enc_xform_null;
211			break;
212		case CRYPTO_ARC4:
213			txform = &enc_xform_arc4;
214			break;
215 		case CRYPTO_CAMELLIA_CBC:
216 			txform = &enc_xform_camellia;
217 			break;
218		default:
219			return (EINVAL);
220		}
221
222		switch (sop->mac) {
223		case 0:
224			break;
225		case CRYPTO_MD5_HMAC:
226			thash = &auth_hash_hmac_md5;
227			break;
228		case CRYPTO_SHA1_HMAC:
229			thash = &auth_hash_hmac_sha1;
230			break;
231		case CRYPTO_SHA2_256_HMAC:
232			thash = &auth_hash_hmac_sha2_256;
233			break;
234		case CRYPTO_SHA2_384_HMAC:
235			thash = &auth_hash_hmac_sha2_384;
236			break;
237		case CRYPTO_SHA2_512_HMAC:
238			thash = &auth_hash_hmac_sha2_512;
239			break;
240		case CRYPTO_RIPEMD160_HMAC:
241			thash = &auth_hash_hmac_ripemd_160;
242			break;
243#ifdef notdef
244		case CRYPTO_MD5:
245			thash = &auth_hash_md5;
246			break;
247		case CRYPTO_SHA1:
248			thash = &auth_hash_sha1;
249			break;
250#endif
251		case CRYPTO_NULL_HMAC:
252			thash = &auth_hash_null;
253			break;
254		default:
255			return (EINVAL);
256		}
257
258		bzero(&crie, sizeof(crie));
259		bzero(&cria, sizeof(cria));
260
261		if (txform) {
262			crie.cri_alg = txform->type;
263			crie.cri_klen = sop->keylen * 8;
264			if (sop->keylen > txform->maxkey ||
265			    sop->keylen < txform->minkey) {
266				error = EINVAL;
267				goto bail;
268			}
269
270			crie.cri_key = malloc(			    crie.cri_klen / 8, M_XDATA, M_WAITOK);
271			if ((error = copyin(sop->key, crie.cri_key,
272			    crie.cri_klen / 8)))
273				goto bail;
274			if (thash)
275				crie.cri_next = &cria;
276		}
277
278		if (thash) {
279			cria.cri_alg = thash->type;
280			cria.cri_klen = sop->mackeylen * 8;
281			if (sop->mackeylen != thash->keysize) {
282				error = EINVAL;
283				goto bail;
284			}
285
286			if (cria.cri_klen) {
287				cria.cri_key = malloc(				    cria.cri_klen / 8, M_XDATA, M_WAITOK);
288				if ((error = copyin(sop->mackey, cria.cri_key,
289				    cria.cri_klen / 8)))
290					goto bail;
291			}
292		}
293
294		/* NB: CIOGSESSION2 has the crid */
295		if (cmd == CIOCGSESSION2) {
296			crid = SES2(sop)->crid;
297			error = checkforsoftware(crid);
298			if (error)
299				goto bail;
300		} else
301			crid = CRYPTOCAP_F_HARDWARE;
302		error = crypto_newsession(&sid, (txform ? &crie : &cria), crid);
303		if (error)
304			goto bail;
305
306		cse = csecreate(fcr, sid, crie.cri_key, crie.cri_klen,
307		    cria.cri_key, cria.cri_klen, sop->cipher, sop->mac, txform,
308		    thash);
309
310		if (cse == NULL) {
311			crypto_freesession(sid);
312			error = EINVAL;
313			goto bail;
314		}
315		sop->ses = cse->ses;
316		if (cmd == CIOCGSESSION2) {
317			/* return hardware/driver id */
318			SES2(sop)->crid = CRYPTO_SESID2HID(cse->sid);
319		}
320bail:
321		if (error) {
322			if (crie.cri_key)
323				free(crie.cri_key, M_XDATA);
324			if (cria.cri_key)
325				free(cria.cri_key, M_XDATA);
326		}
327		break;
328	case CIOCFSESSION:
329		ses = *(u_int32_t *)data;
330		cse = csefind(fcr, ses);
331		if (cse == NULL)
332			return (EINVAL);
333		csedelete(fcr, cse);
334		error = csefree(cse);
335		break;
336	case CIOCCRYPT:
337		cop = (struct crypt_op *)data;
338		cse = csefind(fcr, cop->ses);
339		if (cse == NULL)
340			return (EINVAL);
341		error = cryptodev_op(cse, cop, active_cred, td);
342		break;
343	case CIOCKEY:
344	case CIOCKEY2:
345		if (!crypto_userasymcrypto)
346			return (EPERM);		/* XXX compat? */
347		mtx_lock(&Giant);
348		kop = (struct crypt_kop *)data;
349		if (cmd == CIOCKEY) {
350			/* NB: crypto core enforces s/w driver use */
351			kop->crk_crid =
352			    CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE;
353		}
354		error = cryptodev_key(kop);
355		mtx_unlock(&Giant);
356		break;
357	case CIOCASYMFEAT:
358		if (!crypto_userasymcrypto) {
359			/*
360			 * NB: if user asym crypto operations are
361			 * not permitted return "no algorithms"
362			 * so well-behaved applications will just
363			 * fallback to doing them in software.
364			 */
365			*(int *)data = 0;
366		} else
367			error = crypto_getfeat((int *)data);
368		break;
369	case CIOCFINDDEV:
370		error = cryptodev_find((struct crypt_find_op *)data);
371		break;
372	default:
373		error = EINVAL;
374		break;
375	}
376	return (error);
377#undef SES2
378}
379
380static int cryptodev_cb(void *);
381
382
383static int
384cryptodev_op(
385	struct csession *cse,
386	struct crypt_op *cop,
387	struct ucred *active_cred,
388	struct thread *td)
389{
390	struct cryptop *crp = NULL;
391	struct cryptodesc *crde = NULL, *crda = NULL;
392	int error;
393
394	if (cop->len > 256*1024-4)
395		return (E2BIG);
396
397	if (cse->txform) {
398		if (cop->len == 0 || (cop->len % cse->txform->blocksize) != 0)
399			return (EINVAL);
400	}
401
402	cse->uio.uio_iov = &cse->iovec;
403	cse->uio.uio_iovcnt = 1;
404	cse->uio.uio_offset = 0;
405	cse->uio.uio_resid = cop->len;
406	cse->uio.uio_segflg = UIO_SYSSPACE;
407	cse->uio.uio_rw = UIO_WRITE;
408	cse->uio.uio_td = td;
409	cse->uio.uio_iov[0].iov_len = cop->len;
410	if (cse->thash)
411		cse->uio.uio_iov[0].iov_len += cse->thash->hashsize;
412	cse->uio.uio_iov[0].iov_base = malloc(cse->uio.uio_iov[0].iov_len,
413	    M_XDATA, M_WAITOK);
414
415	crp = crypto_getreq((cse->txform != NULL) + (cse->thash != NULL));
416	if (crp == NULL) {
417		error = ENOMEM;
418		goto bail;
419	}
420
421	if (cse->thash) {
422		crda = crp->crp_desc;
423		if (cse->txform)
424			crde = crda->crd_next;
425	} else {
426		if (cse->txform)
427			crde = crp->crp_desc;
428		else {
429			error = EINVAL;
430			goto bail;
431		}
432	}
433
434	if ((error = copyin(cop->src, cse->uio.uio_iov[0].iov_base, cop->len)))
435		goto bail;
436
437	if (crda) {
438		crda->crd_skip = 0;
439		crda->crd_len = cop->len;
440		crda->crd_inject = cop->len;
441
442		crda->crd_alg = cse->mac;
443		crda->crd_key = cse->mackey;
444		crda->crd_klen = cse->mackeylen * 8;
445	}
446
447	if (crde) {
448		if (cop->op == COP_ENCRYPT)
449			crde->crd_flags |= CRD_F_ENCRYPT;
450		else
451			crde->crd_flags &= ~CRD_F_ENCRYPT;
452		crde->crd_len = cop->len;
453		crde->crd_inject = 0;
454
455		crde->crd_alg = cse->cipher;
456		crde->crd_key = cse->key;
457		crde->crd_klen = cse->keylen * 8;
458	}
459
460	crp->crp_ilen = cop->len;
461	crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM
462		       | (cop->flags & COP_F_BATCH);
463	crp->crp_buf = (caddr_t)&cse->uio;
464	crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_cb;
465	crp->crp_sid = cse->sid;
466	crp->crp_opaque = (void *)cse;
467
468	if (cop->iv) {
469		if (crde == NULL) {
470			error = EINVAL;
471			goto bail;
472		}
473		if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
474			error = EINVAL;
475			goto bail;
476		}
477		if ((error = copyin(cop->iv, cse->tmp_iv, cse->txform->blocksize)))
478			goto bail;
479		bcopy(cse->tmp_iv, crde->crd_iv, cse->txform->blocksize);
480		crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
481		crde->crd_skip = 0;
482	} else if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
483		crde->crd_skip = 0;
484	} else if (crde) {
485		crde->crd_flags |= CRD_F_IV_PRESENT;
486		crde->crd_skip = cse->txform->blocksize;
487		crde->crd_len -= cse->txform->blocksize;
488	}
489
490	if (cop->mac && crda == NULL) {
491		error = EINVAL;
492		goto bail;
493	}
494
495	/*
496	 * Let the dispatch run unlocked, then, interlock against the
497	 * callback before checking if the operation completed and going
498	 * to sleep.  This insures drivers don't inherit our lock which
499	 * results in a lock order reversal between crypto_dispatch forced
500	 * entry and the crypto_done callback into us.
501	 */
502	error = crypto_dispatch(crp);
503	mtx_lock(&cse->lock);
504	if (error == 0 && (crp->crp_flags & CRYPTO_F_DONE) == 0)
505		error = msleep(crp, &cse->lock, PWAIT, "crydev", 0);
506	mtx_unlock(&cse->lock);
507
508	if (error != 0)
509		goto bail;
510
511	if (crp->crp_etype != 0) {
512		error = crp->crp_etype;
513		goto bail;
514	}
515
516	if (cse->error) {
517		error = cse->error;
518		goto bail;
519	}
520
521	if (cop->dst &&
522	    (error = copyout(cse->uio.uio_iov[0].iov_base, cop->dst, cop->len)))
523		goto bail;
524
525	if (cop->mac &&
526	    (error = copyout((caddr_t)cse->uio.uio_iov[0].iov_base + cop->len,
527	    cop->mac, cse->thash->hashsize)))
528		goto bail;
529
530bail:
531	if (crp)
532		crypto_freereq(crp);
533	if (cse->uio.uio_iov[0].iov_base)
534		free(cse->uio.uio_iov[0].iov_base, M_XDATA);
535
536	return (error);
537}
538
539static int
540cryptodev_cb(void *op)
541{
542	struct cryptop *crp = (struct cryptop *) op;
543	struct csession *cse = (struct csession *)crp->crp_opaque;
544	int error;
545
546	error = crp->crp_etype;
547	if (error == EAGAIN)
548		error = crypto_dispatch(crp);
549	mtx_lock(&cse->lock);
550	if (error != 0 || (crp->crp_flags & CRYPTO_F_DONE)) {
551		cse->error = error;
552		wakeup_one(crp);
553	}
554	mtx_unlock(&cse->lock);
555	return (0);
556}
557
558static int
559cryptodevkey_cb(void *op)
560{
561	struct cryptkop *krp = (struct cryptkop *) op;
562
563	wakeup_one(krp);
564	return (0);
565}
566
567static int
568cryptodev_key(struct crypt_kop *kop)
569{
570	struct cryptkop *krp = NULL;
571	int error = EINVAL;
572	int in, out, size, i;
573
574	if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
575		return (EFBIG);
576	}
577
578	in = kop->crk_iparams;
579	out = kop->crk_oparams;
580	switch (kop->crk_op) {
581	case CRK_MOD_EXP:
582		if (in == 3 && out == 1)
583			break;
584		return (EINVAL);
585	case CRK_MOD_EXP_CRT:
586		if (in == 6 && out == 1)
587			break;
588		return (EINVAL);
589	case CRK_DSA_SIGN:
590		if (in == 5 && out == 2)
591			break;
592		return (EINVAL);
593	case CRK_DSA_VERIFY:
594		if (in == 7 && out == 0)
595			break;
596		return (EINVAL);
597	case CRK_DH_COMPUTE_KEY:
598		if (in == 3 && out == 1)
599			break;
600		return (EINVAL);
601	default:
602		return (EINVAL);
603	}
604
605	krp = (struct cryptkop *)malloc(sizeof *krp, M_XDATA, M_WAITOK|M_ZERO);
606	if (!krp)
607		return (ENOMEM);
608	krp->krp_op = kop->crk_op;
609	krp->krp_status = kop->crk_status;
610	krp->krp_iparams = kop->crk_iparams;
611	krp->krp_oparams = kop->crk_oparams;
612	krp->krp_crid = kop->crk_crid;
613	krp->krp_status = 0;
614	krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb;
615
616	for (i = 0; i < CRK_MAXPARAM; i++) {
617		if (kop->crk_param[i].crp_nbits > 65536)
618			/* Limit is the same as in OpenBSD */
619			goto fail;
620		krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
621	}
622	for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
623		size = (krp->krp_param[i].crp_nbits + 7) / 8;
624		if (size == 0)
625			continue;
626		krp->krp_param[i].crp_p = malloc(size, M_XDATA, M_WAITOK);
627		if (i >= krp->krp_iparams)
628			continue;
629		error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size);
630		if (error)
631			goto fail;
632	}
633
634	error = crypto_kdispatch(krp);
635	if (error)
636		goto fail;
637	error = tsleep(krp, PSOCK, "crydev", 0);
638	if (error) {
639		/* XXX can this happen?  if so, how do we recover? */
640		goto fail;
641	}
642
643	kop->crk_crid = krp->krp_crid;		/* device that did the work */
644	if (krp->krp_status != 0) {
645		error = krp->krp_status;
646		goto fail;
647	}
648
649	for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
650		size = (krp->krp_param[i].crp_nbits + 7) / 8;
651		if (size == 0)
652			continue;
653		error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size);
654		if (error)
655			goto fail;
656	}
657
658fail:
659	if (krp) {
660		kop->crk_status = krp->krp_status;
661		for (i = 0; i < CRK_MAXPARAM; i++) {
662			if (krp->krp_param[i].crp_p)
663				free(krp->krp_param[i].crp_p, M_XDATA);
664		}
665		free(krp, M_XDATA);
666	}
667	return (error);
668}
669
670static int
671cryptodev_find(struct crypt_find_op *find)
672{
673	device_t dev;
674
675	if (find->crid != -1) {
676		dev = crypto_find_device_byhid(find->crid);
677		if (dev == NULL)
678			return (ENOENT);
679		strlcpy(find->name, device_get_nameunit(dev),
680		    sizeof(find->name));
681	} else {
682		find->crid = crypto_find_driver(find->name);
683		if (find->crid == -1)
684			return (ENOENT);
685	}
686	return (0);
687}
688
689/* ARGSUSED */
690static int
691cryptof_poll(
692	struct file *fp,
693	int events,
694	struct ucred *active_cred,
695	struct thread *td)
696{
697
698	return (0);
699}
700
701/* ARGSUSED */
702static int
703cryptof_kqfilter(struct file *fp, struct knote *kn)
704{
705
706	return (0);
707}
708
709/* ARGSUSED */
710static int
711cryptof_stat(
712	struct file *fp,
713	struct stat *sb,
714	struct ucred *active_cred,
715	struct thread *td)
716{
717
718	return (EOPNOTSUPP);
719}
720
721/* ARGSUSED */
722static int
723cryptof_close(struct file *fp, struct thread *td)
724{
725	struct fcrypt *fcr = fp->f_data;
726	struct csession *cse;
727
728	while ((cse = TAILQ_FIRST(&fcr->csessions))) {
729		TAILQ_REMOVE(&fcr->csessions, cse, next);
730		(void)csefree(cse);
731	}
732	free(fcr, M_XDATA);
733	fp->f_data = NULL;
734	return 0;
735}
736
737static struct csession *
738csefind(struct fcrypt *fcr, u_int ses)
739{
740	struct csession *cse;
741
742	TAILQ_FOREACH(cse, &fcr->csessions, next)
743		if (cse->ses == ses)
744			return (cse);
745	return (NULL);
746}
747
748static int
749csedelete(struct fcrypt *fcr, struct csession *cse_del)
750{
751	struct csession *cse;
752
753	TAILQ_FOREACH(cse, &fcr->csessions, next) {
754		if (cse == cse_del) {
755			TAILQ_REMOVE(&fcr->csessions, cse, next);
756			return (1);
757		}
758	}
759	return (0);
760}
761
762static struct csession *
763cseadd(struct fcrypt *fcr, struct csession *cse)
764{
765	TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
766	cse->ses = fcr->sesn++;
767	return (cse);
768}
769
770struct csession *
771csecreate(struct fcrypt *fcr, u_int64_t sid, caddr_t key, u_int64_t keylen,
772    caddr_t mackey, u_int64_t mackeylen, u_int32_t cipher, u_int32_t mac,
773    struct enc_xform *txform, struct auth_hash *thash)
774{
775	struct csession *cse;
776
777#ifdef INVARIANTS
778	/* NB: required when mtx_init is built with INVARIANTS */
779	cse = malloc(sizeof(struct csession),
780	    M_XDATA, M_NOWAIT | M_ZERO);
781#else
782	cse = malloc(sizeof(struct csession),
783	    M_XDATA, M_NOWAIT);
784#endif
785	if (cse == NULL)
786		return NULL;
787	mtx_init(&cse->lock, "cryptodev", "crypto session lock", MTX_DEF);
788	cse->key = key;
789	cse->keylen = keylen/8;
790	cse->mackey = mackey;
791	cse->mackeylen = mackeylen/8;
792	cse->sid = sid;
793	cse->cipher = cipher;
794	cse->mac = mac;
795	cse->txform = txform;
796	cse->thash = thash;
797	cseadd(fcr, cse);
798	return (cse);
799}
800
801static int
802csefree(struct csession *cse)
803{
804	int error;
805
806	error = crypto_freesession(cse->sid);
807	mtx_destroy(&cse->lock);
808	if (cse->key)
809		free(cse->key, M_XDATA);
810	if (cse->mackey)
811		free(cse->mackey, M_XDATA);
812	free(cse, M_XDATA);
813	return (error);
814}
815
816static int
817cryptoopen(struct cdev *dev, int oflags, int devtype, struct thread *td)
818{
819	return (0);
820}
821
822static int
823cryptoread(struct cdev *dev, struct uio *uio, int ioflag)
824{
825	return (EIO);
826}
827
828static int
829cryptowrite(struct cdev *dev, struct uio *uio, int ioflag)
830{
831	return (EIO);
832}
833
834static int
835cryptoioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
836{
837	struct file *f;
838	struct fcrypt *fcr;
839	int fd, error;
840
841	switch (cmd) {
842	case CRIOGET:
843		fcr = malloc(		    sizeof(struct fcrypt), M_XDATA, M_WAITOK);
844		TAILQ_INIT(&fcr->csessions);
845		fcr->sesn = 0;
846
847		error = falloc(td, &f, &fd);
848
849		if (error) {
850			free(fcr, M_XDATA);
851			return (error);
852		}
853		/* falloc automatically provides an extra reference to 'f'. */
854		finit(f, FREAD | FWRITE, DTYPE_CRYPTO, fcr, &cryptofops);
855		*(u_int32_t *)data = fd;
856		fdrop(f, td);
857		break;
858	case CRIOFINDDEV:
859		error = cryptodev_find((struct crypt_find_op *)data);
860		break;
861	case CRIOASYMFEAT:
862		error = crypto_getfeat((int *)data);
863		break;
864	default:
865		error = EINVAL;
866		break;
867	}
868	return (error);
869}
870
871static struct cdevsw crypto_cdevsw = {
872	.d_version =	D_VERSION,
873	.d_flags =	D_NEEDGIANT,
874	.d_open =	cryptoopen,
875	.d_read =	cryptoread,
876	.d_write =	cryptowrite,
877	.d_ioctl =	cryptoioctl,
878	.d_name =	"crypto",
879};
880static struct cdev *crypto_dev;
881
882/*
883 * Initialization code, both for static and dynamic loading.
884 */
885static int
886cryptodev_modevent(module_t mod, int type, void *unused)
887{
888	switch (type) {
889	case MOD_LOAD:
890		if (bootverbose)
891			printf("crypto: <crypto device>\n");
892		crypto_dev = make_dev(&crypto_cdevsw, 0,
893				      UID_ROOT, GID_WHEEL, 0666,
894				      "crypto");
895		return 0;
896	case MOD_UNLOAD:
897		/*XXX disallow if active sessions */
898		destroy_dev(crypto_dev);
899		return 0;
900	}
901	return EINVAL;
902}
903
904static moduledata_t cryptodev_mod = {
905	"cryptodev",
906	cryptodev_modevent,
907	0
908};
909MODULE_VERSION(cryptodev, 1);
910DECLARE_MODULE(cryptodev, cryptodev_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
911MODULE_DEPEND(cryptodev, crypto, 1, 1, 1);
912MODULE_DEPEND(cryptodev, zlib, 1, 1, 1);
913