1cd8bffba5ca909862d081bb46ac4f8b36ccaa5f
[dragonfly.git] / sys / opencrypto / cryptodev.c
1 /*      $FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.41 2009/09/04 09:48:18 pjd Exp $   */
2 /*      $OpenBSD: cryptodev.c,v 1.52 2002/06/19 07:22:46 deraadt Exp $  */
3
4 /*-
5  * Copyright (c) 2001 Theo de Raadt
6  * Copyright (c) 2002-2006 Sam Leffler, Errno Consulting
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 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/malloc.h>
39 #include <sys/mbuf.h>
40 #include <sys/lock.h>
41 #include <sys/sysctl.h>
42 #include <sys/file.h>
43 #include <sys/filedesc.h>
44 #include <sys/errno.h>
45 #include <sys/uio.h>
46 #include <sys/random.h>
47 #include <sys/conf.h>
48 #include <sys/module.h>
49 #include <sys/kernel.h>
50 #include <sys/fcntl.h>
51 #include <sys/proc.h>
52 #include <sys/file2.h>
53 #include <sys/thread2.h>
54
55 #include <opencrypto/cryptodev.h>
56 #include <opencrypto/xform.h>
57
58 struct csession {
59         TAILQ_ENTRY(csession) next;
60         u_int64_t       sid;
61         u_int32_t       ses;
62         struct lock     lock;
63
64         u_int32_t       cipher;
65         struct enc_xform *txform;
66         u_int32_t       mac;
67         struct auth_hash *thash;
68
69         caddr_t         key;
70         int             keylen;
71
72         caddr_t         mackey;
73         int             mackeylen;
74
75         struct iovec    iovec;
76         struct uio      uio;
77         int             error;
78 };
79
80 struct fcrypt {
81         TAILQ_HEAD(csessionlist, csession) csessions;
82         int             sesn;
83 };
84
85 static  int cryptof_rw(struct file *fp, struct uio *uio,
86                     struct ucred *cred, int flags);
87 static  int cryptof_ioctl(struct file *, u_long, caddr_t,
88                     struct ucred *, struct sysmsg *);
89 static  int cryptof_poll(struct file *, int, struct ucred *);
90 static  int cryptof_kqfilter(struct file *, struct knote *);
91 static  int cryptof_stat(struct file *, struct stat *, struct ucred *);
92 static  int cryptof_close(struct file *);
93
94 static struct fileops cryptofops = {
95     .fo_read = cryptof_rw,
96     .fo_write = cryptof_rw,
97     .fo_ioctl = cryptof_ioctl,
98     .fo_poll = cryptof_poll,
99     .fo_kqfilter = cryptof_kqfilter,
100     .fo_stat = cryptof_stat,
101     .fo_close = cryptof_close,
102     .fo_shutdown = nofo_shutdown
103 };
104
105 static struct csession *csefind(struct fcrypt *, u_int);
106 static int csedelete(struct fcrypt *, struct csession *);
107 static struct csession *cseadd(struct fcrypt *, struct csession *);
108 static struct csession *csecreate(struct fcrypt *, u_int64_t, caddr_t,
109     u_int64_t, caddr_t, u_int64_t, u_int32_t, u_int32_t, struct enc_xform *,
110     struct auth_hash *);
111 static int csefree(struct csession *);
112
113 static  int cryptodev_op(struct csession *, struct crypt_op *, struct ucred *);
114 static  int cryptodev_key(struct crypt_kop *);
115 static  int cryptodev_find(struct crypt_find_op *);
116
117 static int
118 cryptof_rw(
119         struct file *fp,
120         struct uio *uio,
121         struct ucred *active_cred,
122         int flags)
123 {
124         return (EIO);
125 }
126
127 /*
128  * Check a crypto identifier to see if it requested
129  * a software device/driver.  This can be done either
130  * by device name/class or through search constraints.
131  */
132 static int
133 checkforsoftware(int crid)
134 {
135         if (crid & CRYPTOCAP_F_SOFTWARE)
136                 return EINVAL;          /* XXX */
137         if ((crid & CRYPTOCAP_F_HARDWARE) == 0 &&
138             (crypto_getcaps(crid) & CRYPTOCAP_F_HARDWARE) == 0)
139                 return EINVAL;          /* XXX */
140         return 0;
141 }
142
143 /* ARGSUSED */
144 static int
145 cryptof_ioctl(struct file *fp, u_long cmd, caddr_t data,
146               struct ucred *cred, struct sysmsg *msg)
147 {
148 #define SES2(p) ((struct session2_op *)p)
149         struct cryptoini cria, crie;
150         struct fcrypt *fcr;
151         struct csession *cse;
152         struct session_op *sop;
153         struct crypt_op *cop;
154         struct enc_xform *txform = NULL;
155         struct auth_hash *thash = NULL;
156         struct crypt_kop *kop;
157         u_int64_t sid;
158         u_int32_t ses;
159         int error = 0, crid;
160
161         switch (cmd) {
162         case CIOCGSESSION:
163         case CIOCGSESSION2:
164                 sop = (struct session_op *)data;
165                 switch (sop->cipher) {
166                 case 0:
167                         break;
168                 case CRYPTO_DES_CBC:
169                         txform = &enc_xform_des;
170                         break;
171                 case CRYPTO_3DES_CBC:
172                         txform = &enc_xform_3des;
173                         break;
174                 case CRYPTO_BLF_CBC:
175                         txform = &enc_xform_blf;
176                         break;
177                 case CRYPTO_CAST_CBC:
178                         txform = &enc_xform_cast5;
179                         break;
180                 case CRYPTO_SKIPJACK_CBC:
181                         txform = &enc_xform_skipjack;
182                         break;
183                 case CRYPTO_AES_CBC:
184                         txform = &enc_xform_rijndael128;
185                         break;
186                 case CRYPTO_NULL_CBC:
187                         txform = &enc_xform_null;
188                         break;
189                 case CRYPTO_ARC4:
190                         txform = &enc_xform_arc4;
191                         break;
192                 case CRYPTO_CAMELLIA_CBC:
193                         txform = &enc_xform_camellia;
194                         break;
195                 default:
196                         return (EINVAL);
197                 }
198
199                 switch (sop->mac) {
200                 case 0:
201                         break;
202                 case CRYPTO_MD5_HMAC:
203                         thash = &auth_hash_hmac_md5;
204                         break;
205                 case CRYPTO_SHA1_HMAC:
206                         thash = &auth_hash_hmac_sha1;
207                         break;
208                 case CRYPTO_SHA2_256_HMAC:
209                         thash = &auth_hash_hmac_sha2_256;
210                         break;
211                 case CRYPTO_SHA2_384_HMAC:
212                         thash = &auth_hash_hmac_sha2_384;
213                         break;
214                 case CRYPTO_SHA2_512_HMAC:
215                         thash = &auth_hash_hmac_sha2_512;
216                         break;
217                 case CRYPTO_RIPEMD160_HMAC:
218                         thash = &auth_hash_hmac_ripemd_160;
219                         break;
220 #ifdef notdef
221                 case CRYPTO_MD5:
222                         thash = &auth_hash_md5;
223                         break;
224                 case CRYPTO_SHA1:
225                         thash = &auth_hash_sha1;
226                         break;
227 #endif
228                 case CRYPTO_NULL_HMAC:
229                         thash = &auth_hash_null;
230                         break;
231                 default:
232                         return (EINVAL);
233                 }
234
235                 bzero(&crie, sizeof(crie));
236                 bzero(&cria, sizeof(cria));
237
238                 if (txform) {
239                         crie.cri_alg = txform->type;
240                         crie.cri_klen = sop->keylen * 8;
241                         if (sop->keylen > txform->maxkey ||
242                             sop->keylen < txform->minkey) {
243                                 error = EINVAL;
244                                 goto bail;
245                         }
246
247                         crie.cri_key = kmalloc(crie.cri_klen / 8,
248                             M_XDATA, M_WAITOK);
249                         if ((error = copyin(sop->key, crie.cri_key,
250                             crie.cri_klen / 8)))
251                                 goto bail;
252                         if (thash)
253                                 crie.cri_next = &cria;
254                 }
255
256                 if (thash) {
257                         cria.cri_alg = thash->type;
258                         cria.cri_klen = sop->mackeylen * 8;
259                         if (sop->mackeylen != thash->keysize) {
260                                 error = EINVAL;
261                                 goto bail;
262                         }
263
264                         if (cria.cri_klen) {
265                                 cria.cri_key = malloc(cria.cri_klen / 8,
266                                     M_XDATA, M_WAITOK);
267                                 if ((error = copyin(sop->mackey, cria.cri_key,
268                                     cria.cri_klen / 8)))
269                                         goto bail;
270                         }
271                 }
272
273                 /* NB: CIOGSESSION2 has the crid */
274                 if (cmd == CIOCGSESSION2) {
275                         crid = SES2(sop)->crid;
276                         error = checkforsoftware(crid);
277                         if (error)
278                                 goto bail;
279                 } else
280                         crid = CRYPTOCAP_F_HARDWARE;
281                 error = crypto_newsession(&sid, (txform ? &crie : &cria), crid);
282                 if (error)
283                         goto bail;
284
285                 cse = csecreate(fcr, sid, crie.cri_key, crie.cri_klen,
286                     cria.cri_key, cria.cri_klen, sop->cipher, sop->mac, txform,
287                     thash);
288
289                 if (cse == NULL) {
290                         crypto_freesession(sid);
291                         error = EINVAL;
292                         goto bail;
293                 }
294                 sop->ses = cse->ses;
295                 if (cmd == CIOCGSESSION2) {
296                         /* return hardware/driver id */
297                         SES2(sop)->crid = CRYPTO_SESID2HID(cse->sid);
298                 }
299 bail:
300                 if (error) {
301                         if (crie.cri_key)
302                                 kfree(crie.cri_key, M_XDATA);
303                         if (cria.cri_key)
304                                 kfree(cria.cri_key, M_XDATA);
305                 }
306                 break;
307         case CIOCFSESSION:
308                 ses = *(u_int32_t *)data;
309                 cse = csefind(fcr, ses);
310                 if (cse == NULL)
311                         return (EINVAL);
312                 csedelete(fcr, cse);
313                 error = csefree(cse);
314                 break;
315         case CIOCCRYPT:
316                 cop = (struct crypt_op *)data;
317                 cse = csefind(fcr, cop->ses);
318                 if (cse == NULL)
319                         return (EINVAL);
320                 error = cryptodev_op(cse, cop, active_cred);
321                 break;
322         case CIOCKEY:
323         case CIOCKEY2:
324                 if (!crypto_userasymcrypto)
325                         return (EPERM);         /* XXX compat? */
326                 get_mplock();
327                 kop = (struct crypt_kop *)data;
328                 if (cmd == CIOCKEY) {
329                         /* NB: crypto core enforces s/w driver use */
330                         kop->crk_crid =
331                             CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE;
332                 }
333                 error = cryptodev_key(kop);
334                 rel_mplock();
335                 break;
336         case CIOCASYMFEAT:
337                 if (!crypto_userasymcrypto) {
338                         /*
339                          * NB: if user asym crypto operations are
340                          * not permitted return "no algorithms"
341                          * so well-behaved applications will just
342                          * fallback to doing them in software.
343                          */
344                         *(int *)data = 0;
345                 } else
346                         error = crypto_getfeat((int *)data);
347                 break;
348         case CIOCFINDDEV:
349                 error = cryptodev_find((struct crypt_find_op *)data);
350                 break;
351         default:
352                 error = EINVAL;
353                 break;
354         }
355         return (error);
356 #undef SES2
357 }
358
359 static int cryptodev_cb(void *);
360
361
362 static int
363 cryptodev_op(struct csession *cse, struct crypt_op *cop,
364     struct ucred *active_cred)
365 {
366         struct cryptop *crp = NULL;
367         struct cryptodesc *crde = NULL, *crda = NULL;
368         int error;
369
370         if (cop->len > 256*1024-4)
371                 return (E2BIG);
372
373         if (cse->txform) {
374                 if (cop->len == 0 || (cop->len % cse->txform->blocksize) != 0)
375                         return (EINVAL);
376         }
377
378         bzero(&cse->uio, sizeof(cse->uio));
379         cse->uio.uio_iov = &cse->iovec;
380         cse->uio.uio_iovcnt = 1;
381         cse->uio.uio_offset = 0;
382         cse->uio.uio_resid = cop->len;
383         cse->uio.uio_segflg = UIO_SYSSPACE;
384         cse->uio.uio_rw = UIO_WRITE;
385         cse->uio.uio_td = td;
386         cse->uio.uio_iov[0].iov_len = cop->len;
387         if (cse->thash) {
388                 cse->uio.uio_iov[0].iov_len += cse->thash->hashsize;
389                 cse->uio.uio_resid += cse->thash->hashsize;
390         }
391         cse->uio.uio_iov[0].iov_base = kmalloc(cse->uio.uio_iov[0].iov_len,
392             M_XDATA, M_WAITOK);
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 = cop->len;
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 = (caddr_t)&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 (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
462                 crde->crd_skip = 0;
463         } else if (crde) {
464                 crde->crd_flags |= CRD_F_IV_PRESENT;
465                 crde->crd_skip = cse->txform->blocksize;
466                 crde->crd_len -= cse->txform->blocksize;
467         }
468
469         if (cop->mac && crda == NULL) {
470                 error = EINVAL;
471                 goto bail;
472         }
473
474 again:
475         /*
476          * Let the dispatch run unlocked, then, interlock against the
477          * callback before checking if the operation completed and going
478          * to sleep.  This insures drivers don't inherit our lock which
479          * results in a lock order reversal between crypto_dispatch forced
480          * entry and the crypto_done callback into us.
481          */
482         error = crypto_dispatch(crp);
483         lockmgr(&cse->lock, LK_EXCLUSIVE);
484         if (error == 0 && (crp->crp_flags & CRYPTO_F_DONE) == 0)
485                 error = tsleep(crp, 0, "crydev", 0);
486         lockmgr(&cse->lock, LK_RELEASE);
487
488         if (error != 0)
489                 goto bail;
490
491         if (crp->crp_etype == EAGAIN) {
492                 crp->crp_etype = 0;
493                 crp->crp_flags &= ~CRYPTO_F_DONE;
494                 goto again;
495         }
496
497         if (crp->crp_etype != 0) {
498                 error = crp->crp_etype;
499                 goto bail;
500         }
501
502         if (cse->error) {
503                 error = cse->error;
504                 goto bail;
505         }
506
507         if (cop->dst &&
508             (error = copyout(cse->uio.uio_iov[0].iov_base, cop->dst, cop->len)))
509                 goto bail;
510
511         if (cop->mac &&
512             (error = copyout((caddr_t)cse->uio.uio_iov[0].iov_base + cop->len,
513             cop->mac, cse->thash->hashsize)))
514                 goto bail;
515
516 bail:
517         if (crp)
518                 crypto_freereq(crp);
519         if (cse->uio.uio_iov[0].iov_base)
520                 kfree(cse->uio.uio_iov[0].iov_base, M_XDATA);
521
522         return (error);
523 }
524
525 static int
526 cryptodev_cb(void *op)
527 {
528         struct cryptop *crp = (struct cryptop *) op;
529         struct csession *cse = (struct csession *)crp->crp_opaque;
530
531         lockmgr(&cse->lock, LK_EXCLUSIVE);
532         cse->error = crp->crp_etype;
533         wakeup_one(crp);
534         lockmgr(&cse->lock, LK_RELEASE);
535         return (0);
536 }
537
538 static int
539 cryptodevkey_cb(void *op)
540 {
541         struct cryptkop *krp = (struct cryptkop *) op;
542
543         wakeup_one(krp);
544         return (0);
545 }
546
547 static int
548 cryptodev_key(struct crypt_kop *kop)
549 {
550         struct cryptkop *krp = NULL;
551         int error = EINVAL;
552         int in, out, size, i;
553
554         if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
555                 return (EFBIG);
556         }
557
558         in = kop->crk_iparams;
559         out = kop->crk_oparams;
560         switch (kop->crk_op) {
561         case CRK_MOD_EXP:
562                 if (in == 3 && out == 1)
563                         break;
564                 return (EINVAL);
565         case CRK_MOD_EXP_CRT:
566                 if (in == 6 && out == 1)
567                         break;
568                 return (EINVAL);
569         case CRK_DSA_SIGN:
570                 if (in == 5 && out == 2)
571                         break;
572                 return (EINVAL);
573         case CRK_DSA_VERIFY:
574                 if (in == 7 && out == 0)
575                         break;
576                 return (EINVAL);
577         case CRK_DH_COMPUTE_KEY:
578                 if (in == 3 && out == 1)
579                         break;
580                 return (EINVAL);
581         default:
582                 return (EINVAL);
583         }
584
585         krp = (struct cryptkop *)kmalloc(sizeof *krp, M_XDATA, M_WAITOK | M_ZERO);
586         krp->krp_op = kop->crk_op;
587         krp->krp_status = kop->crk_status;
588         krp->krp_iparams = kop->crk_iparams;
589         krp->krp_oparams = kop->crk_oparams;
590         krp->krp_crid = kop->crk_crid;
591         krp->krp_status = 0;
592         krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb;
593
594         for (i = 0; i < CRK_MAXPARAM; i++) {
595                 if (kop->crk_param[i].crp_nbits > 65536)
596                         /* Limit is the same as in OpenBSD */
597                         goto fail;
598                 krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
599         }
600         for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
601                 size = (krp->krp_param[i].crp_nbits + 7) / 8;
602                 if (size == 0)
603                         continue;
604                 krp->krp_param[i].crp_p = kmalloc(size, M_XDATA, M_WAITOK);
605                 if (i >= krp->krp_iparams)
606                         continue;
607                 error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size);
608                 if (error)
609                         goto fail;
610         }
611
612         error = crypto_kdispatch(krp);
613         if (error)
614                 goto fail;
615         error = tsleep(krp, PSOCK, "crydev", 0);
616         if (error) {
617                 /* XXX can this happen?  if so, how do we recover? */
618                 goto fail;
619         }
620         
621         kop->crk_crid = krp->krp_crid;          /* device that did the work */
622         if (krp->krp_status != 0) {
623                 error = krp->krp_status;
624                 goto fail;
625         }
626
627         for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
628                 size = (krp->krp_param[i].crp_nbits + 7) / 8;
629                 if (size == 0)
630                         continue;
631                 error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size);
632                 if (error)
633                         goto fail;
634         }
635
636 fail:
637         if (krp) {
638                 kop->crk_status = krp->krp_status;
639                 for (i = 0; i < CRK_MAXPARAM; i++) {
640                         if (krp->krp_param[i].crp_p)
641                                 kfree(krp->krp_param[i].crp_p, M_XDATA);
642                 }
643                 kfree(krp, M_XDATA);
644         }
645         return (error);
646 }
647
648 static int
649 cryptodev_find(struct crypt_find_op *find)
650 {
651         device_t dev;
652
653         if (find->crid != -1) {
654                 dev = crypto_find_device_byhid(find->crid);
655                 if (dev == NULL)
656                         return (ENOENT);
657                 strlcpy(find->name, device_get_nameunit(dev),
658                     sizeof(find->name));
659         } else {
660                 find->crid = crypto_find_driver(find->name);
661                 if (find->crid == -1)
662                         return (ENOENT);
663         }
664         return (0);
665 }
666
667 /*
668  * MPSAFE
669  */
670 static int
671 cryptof_poll(struct file *fp, int events, struct ucred *active_cred)
672 {
673         return (0);
674 }
675
676 /*
677  * MPSAFE
678  */
679 static int
680 cryptof_kqfilter(struct file *fp, struct knote *kn)
681 {
682
683         return (0);
684 }
685
686 /*
687  * MPSAFE
688  */
689 static int
690 cryptof_stat(struct file *fp, struct stat *sb, struct ucred *cred)
691 {
692         return (EOPNOTSUPP);
693 }
694
695 /*
696  * MPALMOSTSAFE - acquires mplock
697  */
698 static int
699 cryptof_close(struct file *fp)
700 {
701         struct fcrypt *fcr = fp->f_data;
702         struct csession *cse;
703
704         get_mplock();
705         fcr = (struct fcrypt *)fp->f_data;
706         while ((cse = TAILQ_FIRST(&fcr->csessions))) {
707                 TAILQ_REMOVE(&fcr->csessions, cse, next);
708                 (void)csefree(cse);
709         }
710         fp->f_data = NULL;
711         rel_mplock();
712
713         kfree(fcr, M_XDATA);
714         return (0);
715 }
716
717 static struct csession *
718 csefind(struct fcrypt *fcr, u_int ses)
719 {
720         struct csession *cse;
721
722         TAILQ_FOREACH(cse, &fcr->csessions, next)
723                 if (cse->ses == ses)
724                         return (cse);
725         return (NULL);
726 }
727
728 static int
729 csedelete(struct fcrypt *fcr, struct csession *cse_del)
730 {
731         struct csession *cse;
732
733         TAILQ_FOREACH(cse, &fcr->csessions, next) {
734                 if (cse == cse_del) {
735                         TAILQ_REMOVE(&fcr->csessions, cse, next);
736                         return (1);
737                 }
738         }
739         return (0);
740 }
741         
742 static struct csession *
743 cseadd(struct fcrypt *fcr, struct csession *cse)
744 {
745         TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
746         cse->ses = fcr->sesn++;
747         return (cse);
748 }
749
750 struct csession *
751 csecreate(struct fcrypt *fcr, u_int64_t sid, caddr_t key, u_int64_t keylen,
752     caddr_t mackey, u_int64_t mackeylen, u_int32_t cipher, u_int32_t mac,
753     struct enc_xform *txform, struct auth_hash *thash)
754 {
755         struct csession *cse;
756
757         cse = malloc(sizeof(struct csession), M_XDATA, M_NOWAIT);
758         if (cse == NULL)
759                 return NULL;
760         lockinit(&cse->lock, "cryptodev", LK_CANRECURSE);
761         cse->key = key;
762         cse->keylen = keylen/8;
763         cse->mackey = mackey;
764         cse->mackeylen = mackeylen/8;
765         cse->sid = sid;
766         cse->cipher = cipher;
767         cse->mac = mac;
768         cse->txform = txform;
769         cse->thash = thash;
770         cseadd(fcr, cse);
771         return (cse);
772 }
773
774 static int
775 csefree(struct csession *cse)
776 {
777         int error;
778
779         error = crypto_freesession(cse->sid);
780         lockuninit(&cse->lock);
781         if (cse->key)
782                 kfree(cse->key, M_XDATA);
783         if (cse->mackey)
784                 kfree(cse->mackey, M_XDATA);
785         kfree(cse, M_XDATA);
786         return (error);
787 }
788
789 static int
790 cryptoopen(struct dev_open_args *ap)
791 {
792         return (0);
793 }
794
795 static int
796 cryptoread(struct dev_read_args *ap)
797 {
798         return (EIO);
799 }
800
801 static int
802 cryptowrite(struct dev_write_args *ap)
803 {
804         return (EIO);
805 }
806
807 static int
808 cryptoioctl(struct dev_ioctl_args *ap)
809 {
810         struct file *f;
811         struct fcrypt *fcr;
812         int fd, error;
813
814         switch (ap->a_cmd) {
815         case CRIOGET:
816                 fcr = kmalloc(sizeof(struct fcrypt), M_XDATA, M_WAITOK);
817                 TAILQ_INIT(&fcr->csessions);
818                 fcr->sesn = 0;
819
820                 KKASSERT(curproc);
821                 error = falloc(curproc, &f, &fd);
822
823                 if (error) {
824                         kfree(fcr, M_XDATA);
825                         return (error);
826                 }
827                 /* falloc automatically provides an extra reference to 'f'. */
828                 finit(f, FREAD | FWRITE, DTYPE_CRYPTO, fcr, &cryptofops);
829                 *(u_int32_t *)data = fd;
830                 fdrop(f, td);
831                 break;
832         case CRIOFINDDEV:
833                 error = cryptodev_find((struct crypt_find_op *)data);
834                 break;
835         case CRIOASYMFEAT:
836                 error = crypto_getfeat((int *)data);
837                 break;
838         default:
839                 error = EINVAL;
840                 break;
841         }
842         return (error);
843 }
844
845 static struct dev_ops crypto_ops = {
846         { "crypto", 0, 0 },
847         .d_open =       cryptoopen,
848         .d_read =       cryptoread,
849         .d_write =      cryptowrite,
850         .d_ioctl =      cryptoioctl,
851 };
852
853 /*
854  * Initialization code, both for static and dynamic loading.
855  */
856 static int
857 cryptodev_modevent(module_t mod, int type, void *unused)
858 {
859         switch (type) {
860         case MOD_LOAD:
861                 if (bootverbose)
862                         kprintf("crypto: <crypto device>\n");
863                 make_dev(&crypto_ops, 0, UID_ROOT, GID_WHEEL,
864                          0666, "crypto");
865                 return 0;
866         case MOD_UNLOAD:
867                 /*XXX disallow if active sessions */
868                 //dev_ops_remove(&crypto_ops, 0, 0);
869                 dev_ops_remove_all(&crypto_ops);
870                 return 0;
871         }
872         return EINVAL;
873 }
874
875 static moduledata_t cryptodev_mod = {
876         "cryptodev",
877         cryptodev_modevent,
878         0
879 };
880 MODULE_VERSION(cryptodev, 1);
881 DECLARE_MODULE(cryptodev, cryptodev_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
882 MODULE_DEPEND(cryptodev, crypto, 1, 1, 1);
883 MODULE_DEPEND(cryptodev, zlib, 1, 1, 1);