1 /* $OpenBSD: cryptodev.c,v 1.52 2002/06/19 07:22:46 deraadt Exp $ */
4 * Copyright (c) 2001 Theo de Raadt
5 * Copyright (c) 2002-2006 Sam Leffler, Errno Consulting
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
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.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include "opt_compat.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
45 #include <sys/mutex.h>
46 #include <sys/sysctl.h>
48 #include <sys/filedesc.h>
49 #include <sys/errno.h>
51 #include <sys/random.h>
53 #include <sys/kernel.h>
54 #include <sys/module.h>
55 #include <sys/fcntl.h>
58 #include <opencrypto/cryptodev.h>
59 #include <opencrypto/xform.h>
61 #ifdef COMPAT_FREEBSD32
62 #include <sys/mount.h>
63 #include <compat/freebsd32/freebsd32.h>
75 struct session2_op32 {
108 struct crparam32 crk_param[CRK_MAXPARAM];
111 struct cryptotstat32 {
112 struct timespec32 acc;
113 struct timespec32 min;
114 struct timespec32 max;
118 struct cryptostats32 {
126 u_int32_t cs_kblocks;
127 struct cryptotstat32 cs_invoke;
128 struct cryptotstat32 cs_done;
129 struct cryptotstat32 cs_cb;
130 struct cryptotstat32 cs_finis;
133 #define CIOCGSESSION32 _IOWR('c', 101, struct session_op32)
134 #define CIOCCRYPT32 _IOWR('c', 103, struct crypt_op32)
135 #define CIOCKEY32 _IOWR('c', 104, struct crypt_kop32)
136 #define CIOCGSESSION232 _IOWR('c', 106, struct session2_op32)
137 #define CIOCKEY232 _IOWR('c', 107, struct crypt_kop32)
140 session_op_from_32(const struct session_op32 *from, struct session_op *to)
143 CP(*from, *to, cipher);
145 CP(*from, *to, keylen);
146 PTRIN_CP(*from, *to, key);
147 CP(*from, *to, mackeylen);
148 PTRIN_CP(*from, *to, mackey);
153 session2_op_from_32(const struct session2_op32 *from, struct session2_op *to)
156 session_op_from_32((const struct session_op32 *)from,
157 (struct session_op *)to);
158 CP(*from, *to, crid);
162 session_op_to_32(const struct session_op *from, struct session_op32 *to)
165 CP(*from, *to, cipher);
167 CP(*from, *to, keylen);
168 PTROUT_CP(*from, *to, key);
169 CP(*from, *to, mackeylen);
170 PTROUT_CP(*from, *to, mackey);
175 session2_op_to_32(const struct session2_op *from, struct session2_op32 *to)
178 session_op_to_32((const struct session_op *)from,
179 (struct session_op32 *)to);
180 CP(*from, *to, crid);
184 crypt_op_from_32(const struct crypt_op32 *from, struct crypt_op *to)
189 CP(*from, *to, flags);
191 PTRIN_CP(*from, *to, src);
192 PTRIN_CP(*from, *to, dst);
193 PTRIN_CP(*from, *to, mac);
194 PTRIN_CP(*from, *to, iv);
198 crypt_op_to_32(const struct crypt_op *from, struct crypt_op32 *to)
203 CP(*from, *to, flags);
205 PTROUT_CP(*from, *to, src);
206 PTROUT_CP(*from, *to, dst);
207 PTROUT_CP(*from, *to, mac);
208 PTROUT_CP(*from, *to, iv);
212 crparam_from_32(const struct crparam32 *from, struct crparam *to)
215 PTRIN_CP(*from, *to, crp_p);
216 CP(*from, *to, crp_nbits);
220 crparam_to_32(const struct crparam *from, struct crparam32 *to)
223 PTROUT_CP(*from, *to, crp_p);
224 CP(*from, *to, crp_nbits);
228 crypt_kop_from_32(const struct crypt_kop32 *from, struct crypt_kop *to)
232 CP(*from, *to, crk_op);
233 CP(*from, *to, crk_status);
234 CP(*from, *to, crk_iparams);
235 CP(*from, *to, crk_oparams);
236 CP(*from, *to, crk_crid);
237 for (i = 0; i < CRK_MAXPARAM; i++)
238 crparam_from_32(&from->crk_param[i], &to->crk_param[i]);
242 crypt_kop_to_32(const struct crypt_kop *from, struct crypt_kop32 *to)
246 CP(*from, *to, crk_op);
247 CP(*from, *to, crk_status);
248 CP(*from, *to, crk_iparams);
249 CP(*from, *to, crk_oparams);
250 CP(*from, *to, crk_crid);
251 for (i = 0; i < CRK_MAXPARAM; i++)
252 crparam_to_32(&from->crk_param[i], &to->crk_param[i]);
257 TAILQ_ENTRY(csession) next;
260 struct mtx lock; /* for op submission */
263 struct enc_xform *txform;
265 struct auth_hash *thash;
269 u_char tmp_iv[EALG_MAX_BLOCK_LEN];
280 TAILQ_HEAD(csessionlist, csession) csessions;
284 static int cryptof_rw(struct file *fp, struct uio *uio,
285 struct ucred *cred, int flags, struct thread *);
286 static int cryptof_truncate(struct file *, off_t, struct ucred *,
288 static int cryptof_ioctl(struct file *, u_long, void *,
289 struct ucred *, struct thread *);
290 static int cryptof_poll(struct file *, int, struct ucred *, struct thread *);
291 static int cryptof_kqfilter(struct file *, struct knote *);
292 static int cryptof_stat(struct file *, struct stat *,
293 struct ucred *, struct thread *);
294 static int cryptof_close(struct file *, struct thread *);
296 static struct fileops cryptofops = {
297 .fo_read = cryptof_rw,
298 .fo_write = cryptof_rw,
299 .fo_truncate = cryptof_truncate,
300 .fo_ioctl = cryptof_ioctl,
301 .fo_poll = cryptof_poll,
302 .fo_kqfilter = cryptof_kqfilter,
303 .fo_stat = cryptof_stat,
304 .fo_close = cryptof_close
307 static struct csession *csefind(struct fcrypt *, u_int);
308 static int csedelete(struct fcrypt *, struct csession *);
309 static struct csession *cseadd(struct fcrypt *, struct csession *);
310 static struct csession *csecreate(struct fcrypt *, u_int64_t, caddr_t,
311 u_int64_t, caddr_t, u_int64_t, u_int32_t, u_int32_t, struct enc_xform *,
313 static int csefree(struct csession *);
315 static int cryptodev_op(struct csession *, struct crypt_op *,
316 struct ucred *, struct thread *td);
317 static int cryptodev_key(struct crypt_kop *);
318 static int cryptodev_find(struct crypt_find_op *);
324 struct ucred *active_cred,
336 struct ucred *active_cred,
344 * Check a crypto identifier to see if it requested
345 * a software device/driver. This can be done either
346 * by device name/class or through search constraints.
349 checkforsoftware(int crid)
351 if (crid & CRYPTOCAP_F_SOFTWARE)
352 return EINVAL; /* XXX */
353 if ((crid & CRYPTOCAP_F_HARDWARE) == 0 &&
354 (crypto_getcaps(crid) & CRYPTOCAP_F_HARDWARE) == 0)
355 return EINVAL; /* XXX */
365 struct ucred *active_cred,
368 #define SES2(p) ((struct session2_op *)p)
369 struct cryptoini cria, crie;
370 struct fcrypt *fcr = fp->f_data;
371 struct csession *cse;
372 struct session_op *sop;
373 struct crypt_op *cop;
374 struct enc_xform *txform = NULL;
375 struct auth_hash *thash = NULL;
376 struct crypt_kop *kop;
380 #ifdef COMPAT_FREEBSD32
381 struct session2_op sopc;
382 struct crypt_op copc;
383 struct crypt_kop kopc;
389 #ifdef COMPAT_FREEBSD32
391 case CIOCGSESSION232:
392 if (cmd == CIOCGSESSION32) {
393 session_op_from_32(data, (struct session_op *)&sopc);
394 sop = (struct session_op *)&sopc;
395 } else if (cmd == CIOCGSESSION232) {
396 session2_op_from_32(data, &sopc);
397 sop = (struct session_op *)&sopc;
400 sop = (struct session_op *)data;
401 switch (sop->cipher) {
405 txform = &enc_xform_des;
407 case CRYPTO_3DES_CBC:
408 txform = &enc_xform_3des;
411 txform = &enc_xform_blf;
413 case CRYPTO_CAST_CBC:
414 txform = &enc_xform_cast5;
416 case CRYPTO_SKIPJACK_CBC:
417 txform = &enc_xform_skipjack;
420 txform = &enc_xform_rijndael128;
423 txform = &enc_xform_aes_xts;
425 case CRYPTO_NULL_CBC:
426 txform = &enc_xform_null;
429 txform = &enc_xform_arc4;
431 case CRYPTO_CAMELLIA_CBC:
432 txform = &enc_xform_camellia;
441 case CRYPTO_MD5_HMAC:
442 thash = &auth_hash_hmac_md5;
444 case CRYPTO_SHA1_HMAC:
445 thash = &auth_hash_hmac_sha1;
447 case CRYPTO_SHA2_256_HMAC:
448 thash = &auth_hash_hmac_sha2_256;
450 case CRYPTO_SHA2_384_HMAC:
451 thash = &auth_hash_hmac_sha2_384;
453 case CRYPTO_SHA2_512_HMAC:
454 thash = &auth_hash_hmac_sha2_512;
456 case CRYPTO_RIPEMD160_HMAC:
457 thash = &auth_hash_hmac_ripemd_160;
461 thash = &auth_hash_md5;
464 thash = &auth_hash_sha1;
467 case CRYPTO_NULL_HMAC:
468 thash = &auth_hash_null;
474 bzero(&crie, sizeof(crie));
475 bzero(&cria, sizeof(cria));
478 crie.cri_alg = txform->type;
479 crie.cri_klen = sop->keylen * 8;
480 if (sop->keylen > txform->maxkey ||
481 sop->keylen < txform->minkey) {
486 crie.cri_key = malloc(crie.cri_klen / 8,
488 if ((error = copyin(sop->key, crie.cri_key,
492 crie.cri_next = &cria;
496 cria.cri_alg = thash->type;
497 cria.cri_klen = sop->mackeylen * 8;
498 if (sop->mackeylen != thash->keysize) {
504 cria.cri_key = malloc(cria.cri_klen / 8,
506 if ((error = copyin(sop->mackey, cria.cri_key,
512 /* NB: CIOCGSESSION2 has the crid */
513 if (cmd == CIOCGSESSION2
514 #ifdef COMPAT_FREEBSD32
515 || cmd == CIOCGSESSION232
518 crid = SES2(sop)->crid;
519 error = checkforsoftware(crid);
523 crid = CRYPTOCAP_F_HARDWARE;
524 error = crypto_newsession(&sid, (txform ? &crie : &cria), crid);
528 cse = csecreate(fcr, sid, crie.cri_key, crie.cri_klen,
529 cria.cri_key, cria.cri_klen, sop->cipher, sop->mac, txform,
533 crypto_freesession(sid);
538 if (cmd == CIOCGSESSION2
539 #ifdef COMPAT_FREEBSD32
540 || cmd == CIOCGSESSION232
543 /* return hardware/driver id */
544 SES2(sop)->crid = CRYPTO_SESID2HID(cse->sid);
549 free(crie.cri_key, M_XDATA);
551 free(cria.cri_key, M_XDATA);
553 #ifdef COMPAT_FREEBSD32
555 if (cmd == CIOCGSESSION32)
556 session_op_to_32(sop, data);
557 else if (cmd == CIOCGSESSION232)
558 session2_op_to_32((struct session2_op *)sop,
564 ses = *(u_int32_t *)data;
565 cse = csefind(fcr, ses);
569 error = csefree(cse);
572 #ifdef COMPAT_FREEBSD32
574 if (cmd == CIOCCRYPT32) {
576 crypt_op_from_32(data, cop);
579 cop = (struct crypt_op *)data;
580 cse = csefind(fcr, cop->ses);
583 error = cryptodev_op(cse, cop, active_cred, td);
584 #ifdef COMPAT_FREEBSD32
585 if (error == 0 && cmd == CIOCCRYPT32)
586 crypt_op_to_32(cop, data);
591 #ifdef COMPAT_FREEBSD32
595 if (!crypto_userasymcrypto)
596 return (EPERM); /* XXX compat? */
597 #ifdef COMPAT_FREEBSD32
598 if (cmd == CIOCKEY32 || cmd == CIOCKEY232) {
600 crypt_kop_from_32(data, kop);
603 kop = (struct crypt_kop *)data;
605 #ifdef COMPAT_FREEBSD32
609 /* NB: crypto core enforces s/w driver use */
611 CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE;
614 error = cryptodev_key(kop);
616 #ifdef COMPAT_FREEBSD32
617 if (cmd == CIOCKEY32 || cmd == CIOCKEY232)
618 crypt_kop_to_32(kop, data);
622 if (!crypto_userasymcrypto) {
624 * NB: if user asym crypto operations are
625 * not permitted return "no algorithms"
626 * so well-behaved applications will just
627 * fallback to doing them in software.
631 error = crypto_getfeat((int *)data);
634 error = cryptodev_find((struct crypt_find_op *)data);
644 static int cryptodev_cb(void *);
649 struct csession *cse,
650 struct crypt_op *cop,
651 struct ucred *active_cred,
654 struct cryptop *crp = NULL;
655 struct cryptodesc *crde = NULL, *crda = NULL;
658 if (cop->len > 256*1024-4)
662 if (cop->len == 0 || (cop->len % cse->txform->blocksize) != 0)
666 cse->uio.uio_iov = &cse->iovec;
667 cse->uio.uio_iovcnt = 1;
668 cse->uio.uio_offset = 0;
669 cse->uio.uio_resid = cop->len;
670 cse->uio.uio_segflg = UIO_SYSSPACE;
671 cse->uio.uio_rw = UIO_WRITE;
672 cse->uio.uio_td = td;
673 cse->uio.uio_iov[0].iov_len = cop->len;
675 cse->uio.uio_iov[0].iov_len += cse->thash->hashsize;
676 cse->uio.uio_resid += cse->thash->hashsize;
678 cse->uio.uio_iov[0].iov_base = malloc(cse->uio.uio_iov[0].iov_len,
681 crp = crypto_getreq((cse->txform != NULL) + (cse->thash != NULL));
688 crda = crp->crp_desc;
690 crde = crda->crd_next;
693 crde = crp->crp_desc;
700 if ((error = copyin(cop->src, cse->uio.uio_iov[0].iov_base, cop->len)))
705 crda->crd_len = cop->len;
706 crda->crd_inject = cop->len;
708 crda->crd_alg = cse->mac;
709 crda->crd_key = cse->mackey;
710 crda->crd_klen = cse->mackeylen * 8;
714 if (cop->op == COP_ENCRYPT)
715 crde->crd_flags |= CRD_F_ENCRYPT;
717 crde->crd_flags &= ~CRD_F_ENCRYPT;
718 crde->crd_len = cop->len;
719 crde->crd_inject = 0;
721 crde->crd_alg = cse->cipher;
722 crde->crd_key = cse->key;
723 crde->crd_klen = cse->keylen * 8;
726 crp->crp_ilen = cop->len;
727 crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM
728 | (cop->flags & COP_F_BATCH);
729 crp->crp_buf = (caddr_t)&cse->uio;
730 crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_cb;
731 crp->crp_sid = cse->sid;
732 crp->crp_opaque = (void *)cse;
739 if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
743 if ((error = copyin(cop->iv, cse->tmp_iv, cse->txform->blocksize)))
745 bcopy(cse->tmp_iv, crde->crd_iv, cse->txform->blocksize);
746 crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
748 } else if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
751 crde->crd_flags |= CRD_F_IV_PRESENT;
752 crde->crd_skip = cse->txform->blocksize;
753 crde->crd_len -= cse->txform->blocksize;
756 if (cop->mac && crda == NULL) {
763 * Let the dispatch run unlocked, then, interlock against the
764 * callback before checking if the operation completed and going
765 * to sleep. This insures drivers don't inherit our lock which
766 * results in a lock order reversal between crypto_dispatch forced
767 * entry and the crypto_done callback into us.
769 error = crypto_dispatch(crp);
770 mtx_lock(&cse->lock);
771 if (error == 0 && (crp->crp_flags & CRYPTO_F_DONE) == 0)
772 error = msleep(crp, &cse->lock, PWAIT, "crydev", 0);
773 mtx_unlock(&cse->lock);
778 if (crp->crp_etype == EAGAIN) {
780 crp->crp_flags &= ~CRYPTO_F_DONE;
784 if (crp->crp_etype != 0) {
785 error = crp->crp_etype;
795 (error = copyout(cse->uio.uio_iov[0].iov_base, cop->dst, cop->len)))
799 (error = copyout((caddr_t)cse->uio.uio_iov[0].iov_base + cop->len,
800 cop->mac, cse->thash->hashsize)))
806 if (cse->uio.uio_iov[0].iov_base)
807 free(cse->uio.uio_iov[0].iov_base, M_XDATA);
813 cryptodev_cb(void *op)
815 struct cryptop *crp = (struct cryptop *) op;
816 struct csession *cse = (struct csession *)crp->crp_opaque;
818 mtx_lock(&cse->lock);
819 cse->error = crp->crp_etype;
821 mtx_unlock(&cse->lock);
826 cryptodevkey_cb(void *op)
828 struct cryptkop *krp = (struct cryptkop *) op;
835 cryptodev_key(struct crypt_kop *kop)
837 struct cryptkop *krp = NULL;
839 int in, out, size, i;
841 if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
845 in = kop->crk_iparams;
846 out = kop->crk_oparams;
847 switch (kop->crk_op) {
849 if (in == 3 && out == 1)
852 case CRK_MOD_EXP_CRT:
853 if (in == 6 && out == 1)
857 if (in == 5 && out == 2)
861 if (in == 7 && out == 0)
864 case CRK_DH_COMPUTE_KEY:
865 if (in == 3 && out == 1)
872 krp = (struct cryptkop *)malloc(sizeof *krp, M_XDATA, M_WAITOK|M_ZERO);
875 krp->krp_op = kop->crk_op;
876 krp->krp_status = kop->crk_status;
877 krp->krp_iparams = kop->crk_iparams;
878 krp->krp_oparams = kop->crk_oparams;
879 krp->krp_crid = kop->crk_crid;
881 krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb;
883 for (i = 0; i < CRK_MAXPARAM; i++) {
884 if (kop->crk_param[i].crp_nbits > 65536)
885 /* Limit is the same as in OpenBSD */
887 krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
889 for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
890 size = (krp->krp_param[i].crp_nbits + 7) / 8;
893 krp->krp_param[i].crp_p = malloc(size, M_XDATA, M_WAITOK);
894 if (i >= krp->krp_iparams)
896 error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size);
901 error = crypto_kdispatch(krp);
904 error = tsleep(krp, PSOCK, "crydev", 0);
906 /* XXX can this happen? if so, how do we recover? */
910 kop->crk_crid = krp->krp_crid; /* device that did the work */
911 if (krp->krp_status != 0) {
912 error = krp->krp_status;
916 for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
917 size = (krp->krp_param[i].crp_nbits + 7) / 8;
920 error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size);
927 kop->crk_status = krp->krp_status;
928 for (i = 0; i < CRK_MAXPARAM; i++) {
929 if (krp->krp_param[i].crp_p)
930 free(krp->krp_param[i].crp_p, M_XDATA);
938 cryptodev_find(struct crypt_find_op *find)
942 if (find->crid != -1) {
943 dev = crypto_find_device_byhid(find->crid);
946 strlcpy(find->name, device_get_nameunit(dev),
949 find->crid = crypto_find_driver(find->name);
950 if (find->crid == -1)
961 struct ucred *active_cred,
970 cryptof_kqfilter(struct file *fp, struct knote *kn)
981 struct ucred *active_cred,
990 cryptof_close(struct file *fp, struct thread *td)
992 struct fcrypt *fcr = fp->f_data;
993 struct csession *cse;
995 while ((cse = TAILQ_FIRST(&fcr->csessions))) {
996 TAILQ_REMOVE(&fcr->csessions, cse, next);
1004 static struct csession *
1005 csefind(struct fcrypt *fcr, u_int ses)
1007 struct csession *cse;
1009 TAILQ_FOREACH(cse, &fcr->csessions, next)
1010 if (cse->ses == ses)
1016 csedelete(struct fcrypt *fcr, struct csession *cse_del)
1018 struct csession *cse;
1020 TAILQ_FOREACH(cse, &fcr->csessions, next) {
1021 if (cse == cse_del) {
1022 TAILQ_REMOVE(&fcr->csessions, cse, next);
1029 static struct csession *
1030 cseadd(struct fcrypt *fcr, struct csession *cse)
1032 TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
1033 cse->ses = fcr->sesn++;
1038 csecreate(struct fcrypt *fcr, u_int64_t sid, caddr_t key, u_int64_t keylen,
1039 caddr_t mackey, u_int64_t mackeylen, u_int32_t cipher, u_int32_t mac,
1040 struct enc_xform *txform, struct auth_hash *thash)
1042 struct csession *cse;
1045 /* NB: required when mtx_init is built with INVARIANTS */
1046 cse = malloc(sizeof(struct csession), M_XDATA, M_NOWAIT | M_ZERO);
1048 cse = malloc(sizeof(struct csession), M_XDATA, M_NOWAIT);
1052 mtx_init(&cse->lock, "cryptodev", "crypto session lock", MTX_DEF);
1054 cse->keylen = keylen/8;
1055 cse->mackey = mackey;
1056 cse->mackeylen = mackeylen/8;
1058 cse->cipher = cipher;
1060 cse->txform = txform;
1067 csefree(struct csession *cse)
1071 error = crypto_freesession(cse->sid);
1072 mtx_destroy(&cse->lock);
1074 free(cse->key, M_XDATA);
1076 free(cse->mackey, M_XDATA);
1082 cryptoopen(struct cdev *dev, int oflags, int devtype, struct thread *td)
1088 cryptoread(struct cdev *dev, struct uio *uio, int ioflag)
1094 cryptowrite(struct cdev *dev, struct uio *uio, int ioflag)
1100 cryptoioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
1108 fcr = malloc(sizeof(struct fcrypt), M_XDATA, M_WAITOK);
1109 TAILQ_INIT(&fcr->csessions);
1112 error = falloc(td, &f, &fd);
1118 /* falloc automatically provides an extra reference to 'f'. */
1119 finit(f, FREAD | FWRITE, DTYPE_CRYPTO, fcr, &cryptofops);
1120 *(u_int32_t *)data = fd;
1124 error = cryptodev_find((struct crypt_find_op *)data);
1127 error = crypto_getfeat((int *)data);
1136 static struct cdevsw crypto_cdevsw = {
1137 .d_version = D_VERSION,
1138 .d_flags = D_NEEDGIANT,
1139 .d_open = cryptoopen,
1140 .d_read = cryptoread,
1141 .d_write = cryptowrite,
1142 .d_ioctl = cryptoioctl,
1145 static struct cdev *crypto_dev;
1148 * Initialization code, both for static and dynamic loading.
1151 cryptodev_modevent(module_t mod, int type, void *unused)
1156 printf("crypto: <crypto device>\n");
1157 crypto_dev = make_dev(&crypto_cdevsw, 0,
1158 UID_ROOT, GID_WHEEL, 0666,
1162 /*XXX disallow if active sessions */
1163 destroy_dev(crypto_dev);
1169 static moduledata_t cryptodev_mod = {
1174 MODULE_VERSION(cryptodev, 1);
1175 DECLARE_MODULE(cryptodev, cryptodev_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
1176 MODULE_DEPEND(cryptodev, crypto, 1, 1, 1);
1177 MODULE_DEPEND(cryptodev, zlib, 1, 1, 1);