threaded interrupts 1: Rewrite the ICU interrupt code, splz, and doreti code.
[dragonfly.git] / sys / opencrypto / cryptodev.c
1 /*      $FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.4.2.4 2003/06/03 00:09:02 sam Exp $        */
2 /*      $DragonFly: src/sys/opencrypto/cryptodev.c,v 1.3 2003/06/25 03:56:08 dillon Exp $       */
3 /*      $OpenBSD: cryptodev.c,v 1.52 2002/06/19 07:22:46 deraadt 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/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <sys/mbuf.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/kernel.h>
49 #include <sys/fcntl.h>
50 #include <sys/proc.h>
51 #include <sys/file2.h>
52
53 #include <opencrypto/cryptodev.h>
54 #include <opencrypto/xform.h>
55
56 struct csession {
57         TAILQ_ENTRY(csession) next;
58         u_int64_t       sid;
59         u_int32_t       ses;
60
61         u_int32_t       cipher;
62         struct enc_xform *txform;
63         u_int32_t       mac;
64         struct auth_hash *thash;
65
66         caddr_t         key;
67         int             keylen;
68         u_char          tmp_iv[EALG_MAX_BLOCK_LEN];
69
70         caddr_t         mackey;
71         int             mackeylen;
72         u_char          tmp_mac[CRYPTO_MAX_MAC_LEN];
73
74         struct iovec    iovec[UIO_MAXIOV];
75         struct uio      uio;
76         int             error;
77 };
78
79 struct fcrypt {
80         TAILQ_HEAD(csessionlist, csession) csessions;
81         int             sesn;
82 };
83
84 static  int cryptof_rw(struct file *fp, struct uio *uio,
85                     struct ucred *cred, int flags, struct thread *);
86 static  int cryptof_ioctl(struct file *, u_long, caddr_t, struct thread *);
87 static  int cryptof_poll(struct file *, int, struct ucred *, struct thread *);
88 static  int cryptof_kqfilter(struct file *, struct knote *);
89 static  int cryptof_stat(struct file *, struct stat *, struct thread *);
90 static  int cryptof_close(struct file *, struct thread *);
91
92 static struct fileops cryptofops = {
93     cryptof_rw,
94     cryptof_rw,
95     cryptof_ioctl,
96     cryptof_poll,
97     cryptof_kqfilter,
98     cryptof_stat,
99     cryptof_close
100 };
101
102 static struct csession *csefind(struct fcrypt *, u_int);
103 static int csedelete(struct fcrypt *, struct csession *);
104 static struct csession *cseadd(struct fcrypt *, struct csession *);
105 static struct csession *csecreate(struct fcrypt *, u_int64_t, caddr_t,
106     u_int64_t, caddr_t, u_int64_t, u_int32_t, u_int32_t, struct enc_xform *,
107     struct auth_hash *);
108 static int csefree(struct csession *);
109
110 static  int cryptodev_op(struct csession *, struct crypt_op *,
111                         struct thread *td);
112 static  int cryptodev_key(struct crypt_kop *);
113
114 static int
115 cryptof_rw(
116         struct file *fp,
117         struct uio *uio,
118         struct ucred *active_cred,
119         int flags,
120         struct thread *td)
121 {
122
123         return (EIO);
124 }
125
126 /* ARGSUSED */
127 static int
128 cryptof_ioctl(
129         struct file *fp,
130         u_long cmd,
131         caddr_t data,
132         struct thread *td)
133 {
134         struct cryptoini cria, crie;
135         struct fcrypt *fcr = (struct fcrypt *)fp->f_data;
136         struct csession *cse;
137         struct session_op *sop;
138         struct crypt_op *cop;
139         struct enc_xform *txform = NULL;
140         struct auth_hash *thash = NULL;
141         u_int64_t sid;
142         u_int32_t ses;
143         int error = 0;
144
145         switch (cmd) {
146         case CIOCGSESSION:
147                 sop = (struct session_op *)data;
148                 switch (sop->cipher) {
149                 case 0:
150                         break;
151                 case CRYPTO_DES_CBC:
152                         txform = &enc_xform_des;
153                         break;
154                 case CRYPTO_3DES_CBC:
155                         txform = &enc_xform_3des;
156                         break;
157                 case CRYPTO_BLF_CBC:
158                         txform = &enc_xform_blf;
159                         break;
160                 case CRYPTO_CAST_CBC:
161                         txform = &enc_xform_cast5;
162                         break;
163                 case CRYPTO_SKIPJACK_CBC:
164                         txform = &enc_xform_skipjack;
165                         break;
166                 case CRYPTO_AES_CBC:
167                         txform = &enc_xform_rijndael128;
168                         break;
169                 case CRYPTO_NULL_CBC:
170                         txform = &enc_xform_null;
171                         break;
172                 case CRYPTO_ARC4:
173                         txform = &enc_xform_arc4;
174                         break;
175                 default:
176                         return (EINVAL);
177                 }
178
179                 switch (sop->mac) {
180                 case 0:
181                         break;
182                 case CRYPTO_MD5_HMAC:
183                         thash = &auth_hash_hmac_md5_96;
184                         break;
185                 case CRYPTO_SHA1_HMAC:
186                         thash = &auth_hash_hmac_sha1_96;
187                         break;
188                 case CRYPTO_SHA2_HMAC:
189                         if (sop->mackeylen == auth_hash_hmac_sha2_256.keysize)
190                                 thash = &auth_hash_hmac_sha2_256;
191                         else if (sop->mackeylen == auth_hash_hmac_sha2_384.keysize)
192                                 thash = &auth_hash_hmac_sha2_384;
193                         else if (sop->mackeylen == auth_hash_hmac_sha2_512.keysize)
194                                 thash = &auth_hash_hmac_sha2_512;
195                         else
196                                 return (EINVAL);
197                         break;
198                 case CRYPTO_RIPEMD160_HMAC:
199                         thash = &auth_hash_hmac_ripemd_160_96;
200                         break;
201 #ifdef notdef
202                 case CRYPTO_MD5:
203                         thash = &auth_hash_md5;
204                         break;
205                 case CRYPTO_SHA1:
206                         thash = &auth_hash_sha1;
207                         break;
208 #endif
209                 case CRYPTO_NULL_HMAC:
210                         thash = &auth_hash_null;
211                         break;
212                 default:
213                         return (EINVAL);
214                 }
215
216                 bzero(&crie, sizeof(crie));
217                 bzero(&cria, sizeof(cria));
218
219                 if (txform) {
220                         crie.cri_alg = txform->type;
221                         crie.cri_klen = sop->keylen * 8;
222                         if (sop->keylen > txform->maxkey ||
223                             sop->keylen < txform->minkey) {
224                                 error = EINVAL;
225                                 goto bail;
226                         }
227
228                         MALLOC(crie.cri_key, u_int8_t *,
229                             crie.cri_klen / 8, M_XDATA, M_WAITOK);
230                         if ((error = copyin(sop->key, crie.cri_key,
231                             crie.cri_klen / 8)))
232                                 goto bail;
233                         if (thash)
234                                 crie.cri_next = &cria;
235                 }
236
237                 if (thash) {
238                         cria.cri_alg = thash->type;
239                         cria.cri_klen = sop->mackeylen * 8;
240                         if (sop->mackeylen != thash->keysize) {
241                                 error = EINVAL;
242                                 goto bail;
243                         }
244
245                         if (cria.cri_klen) {
246                                 MALLOC(cria.cri_key, u_int8_t *,
247                                     cria.cri_klen / 8, M_XDATA, M_WAITOK);
248                                 if ((error = copyin(sop->mackey, cria.cri_key,
249                                     cria.cri_klen / 8)))
250                                         goto bail;
251                         }
252                 }
253
254                 error = crypto_newsession(&sid, (txform ? &crie : &cria), 1);
255                 if (error)
256                         goto bail;
257
258                 cse = csecreate(fcr, sid, crie.cri_key, crie.cri_klen,
259                     cria.cri_key, cria.cri_klen, sop->cipher, sop->mac, txform,
260                     thash);
261
262                 if (cse == NULL) {
263                         crypto_freesession(sid);
264                         error = EINVAL;
265                         goto bail;
266                 }
267                 sop->ses = cse->ses;
268
269 bail:
270                 if (error) {
271                         if (crie.cri_key)
272                                 FREE(crie.cri_key, M_XDATA);
273                         if (cria.cri_key)
274                                 FREE(cria.cri_key, M_XDATA);
275                 }
276                 break;
277         case CIOCFSESSION:
278                 ses = *(u_int32_t *)data;
279                 cse = csefind(fcr, ses);
280                 if (cse == NULL)
281                         return (EINVAL);
282                 csedelete(fcr, cse);
283                 error = csefree(cse);
284                 break;
285         case CIOCCRYPT:
286                 cop = (struct crypt_op *)data;
287                 cse = csefind(fcr, cop->ses);
288                 if (cse == NULL)
289                         return (EINVAL);
290                 error = cryptodev_op(cse, cop, td);
291                 break;
292         case CIOCKEY:
293                 error = cryptodev_key((struct crypt_kop *)data);
294                 break;
295         case CIOCASYMFEAT:
296                 error = crypto_getfeat((int *)data);
297                 break;
298         default:
299                 error = EINVAL;
300         }
301         return (error);
302 }
303
304 static int cryptodev_cb(void *);
305
306
307 static int
308 cryptodev_op(
309         struct csession *cse,
310         struct crypt_op *cop,
311         struct thread *td)
312 {
313         struct cryptop *crp = NULL;
314         struct cryptodesc *crde = NULL, *crda = NULL;
315         int i, error, s;
316
317         if (cop->len > 256*1024-4)
318                 return (E2BIG);
319
320         if (cse->txform && (cop->len % cse->txform->blocksize) != 0)
321                 return (EINVAL);
322
323         bzero(&cse->uio, sizeof(cse->uio));
324         cse->uio.uio_iovcnt = 1;
325         cse->uio.uio_resid = 0;
326         cse->uio.uio_segflg = UIO_SYSSPACE;
327         cse->uio.uio_rw = UIO_WRITE;
328         cse->uio.uio_td = td;
329         cse->uio.uio_iov = cse->iovec;
330         bzero(&cse->iovec, sizeof(cse->iovec));
331         cse->uio.uio_iov[0].iov_len = cop->len;
332         cse->uio.uio_iov[0].iov_base = malloc(cop->len, M_XDATA, M_WAITOK);
333         for (i = 0; i < cse->uio.uio_iovcnt; i++)
334                 cse->uio.uio_resid += cse->uio.uio_iov[0].iov_len;
335
336         crp = crypto_getreq((cse->txform != NULL) + (cse->thash != NULL));
337         if (crp == NULL) {
338                 error = ENOMEM;
339                 goto bail;
340         }
341
342         if (cse->thash) {
343                 crda = crp->crp_desc;
344                 if (cse->txform)
345                         crde = crda->crd_next;
346         } else {
347                 if (cse->txform)
348                         crde = crp->crp_desc;
349                 else {
350                         error = EINVAL;
351                         goto bail;
352                 }
353         }
354
355         if ((error = copyin(cop->src, cse->uio.uio_iov[0].iov_base, cop->len)))
356                 goto bail;
357
358         if (crda) {
359                 crda->crd_skip = 0;
360                 crda->crd_len = cop->len;
361                 crda->crd_inject = 0;   /* ??? */
362
363                 crda->crd_alg = cse->mac;
364                 crda->crd_key = cse->mackey;
365                 crda->crd_klen = cse->mackeylen * 8;
366         }
367
368         if (crde) {
369                 if (cop->op == COP_ENCRYPT)
370                         crde->crd_flags |= CRD_F_ENCRYPT;
371                 else
372                         crde->crd_flags &= ~CRD_F_ENCRYPT;
373                 crde->crd_len = cop->len;
374                 crde->crd_inject = 0;
375
376                 crde->crd_alg = cse->cipher;
377                 crde->crd_key = cse->key;
378                 crde->crd_klen = cse->keylen * 8;
379         }
380
381         crp->crp_ilen = cop->len;
382         crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM
383                        | (cop->flags & COP_F_BATCH);
384         crp->crp_buf = (caddr_t)&cse->uio;
385         crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_cb;
386         crp->crp_sid = cse->sid;
387         crp->crp_opaque = (void *)cse;
388
389         if (cop->iv) {
390                 if (crde == NULL) {
391                         error = EINVAL;
392                         goto bail;
393                 }
394                 if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
395                         error = EINVAL;
396                         goto bail;
397                 }
398                 if ((error = copyin(cop->iv, cse->tmp_iv, cse->txform->blocksize)))
399                         goto bail;
400                 bcopy(cse->tmp_iv, crde->crd_iv, cse->txform->blocksize);
401                 crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
402                 crde->crd_skip = 0;
403         } else if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
404                 crde->crd_skip = 0;
405         } else if (crde) {
406                 crde->crd_flags |= CRD_F_IV_PRESENT;
407                 crde->crd_skip = cse->txform->blocksize;
408                 crde->crd_len -= cse->txform->blocksize;
409         }
410
411         if (cop->mac) {
412                 if (crda == NULL) {
413                         error = EINVAL;
414                         goto bail;
415                 }
416                 crp->crp_mac=cse->tmp_mac;
417         }
418
419         s = splcrypto();        /* NB: only needed with CRYPTO_F_CBIMM */
420         error = crypto_dispatch(crp);
421         if (error == 0 && (crp->crp_flags & CRYPTO_F_DONE) == 0)
422                 error = tsleep(crp, PSOCK, "crydev", 0);
423         splx(s);
424         if (error)
425                 goto bail;
426
427         if (crp->crp_etype != 0) {
428                 error = crp->crp_etype;
429                 goto bail;
430         }
431
432         if (cse->error) {
433                 error = cse->error;
434                 goto bail;
435         }
436
437         if (cop->dst &&
438             (error = copyout(cse->uio.uio_iov[0].iov_base, cop->dst, cop->len)))
439                 goto bail;
440
441         if (cop->mac &&
442             (error = copyout(crp->crp_mac, cop->mac, cse->thash->authsize)))
443                 goto bail;
444
445 bail:
446         if (crp)
447                 crypto_freereq(crp);
448         if (cse->uio.uio_iov[0].iov_base)
449                 free(cse->uio.uio_iov[0].iov_base, M_XDATA);
450
451         return (error);
452 }
453
454 static int
455 cryptodev_cb(void *op)
456 {
457         struct cryptop *crp = (struct cryptop *) op;
458         struct csession *cse = (struct csession *)crp->crp_opaque;
459
460         cse->error = crp->crp_etype;
461         if (crp->crp_etype == EAGAIN)
462                 return crypto_dispatch(crp);
463         wakeup_one(crp);
464         return (0);
465 }
466
467 static int
468 cryptodevkey_cb(void *op)
469 {
470         struct cryptkop *krp = (struct cryptkop *) op;
471
472         wakeup_one(krp);
473         return (0);
474 }
475
476 static int
477 cryptodev_key(struct crypt_kop *kop)
478 {
479         struct cryptkop *krp = NULL;
480         int error = EINVAL;
481         int in, out, size, i;
482
483         if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
484                 return (EFBIG);
485         }
486
487         in = kop->crk_iparams;
488         out = kop->crk_oparams;
489         switch (kop->crk_op) {
490         case CRK_MOD_EXP:
491                 if (in == 3 && out == 1)
492                         break;
493                 return (EINVAL);
494         case CRK_MOD_EXP_CRT:
495                 if (in == 6 && out == 1)
496                         break;
497                 return (EINVAL);
498         case CRK_DSA_SIGN:
499                 if (in == 5 && out == 2)
500                         break;
501                 return (EINVAL);
502         case CRK_DSA_VERIFY:
503                 if (in == 7 && out == 0)
504                         break;
505                 return (EINVAL);
506         case CRK_DH_COMPUTE_KEY:
507                 if (in == 3 && out == 1)
508                         break;
509                 return (EINVAL);
510         default:
511                 return (EINVAL);
512         }
513
514         krp = (struct cryptkop *)malloc(sizeof *krp, M_XDATA, M_WAITOK);
515         if (!krp)
516                 return (ENOMEM);
517         bzero(krp, sizeof *krp);
518         krp->krp_op = kop->crk_op;
519         krp->krp_status = kop->crk_status;
520         krp->krp_iparams = kop->crk_iparams;
521         krp->krp_oparams = kop->crk_oparams;
522         krp->krp_status = 0;
523         krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb;
524
525         for (i = 0; i < CRK_MAXPARAM; i++)
526                 krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
527         for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
528                 size = (krp->krp_param[i].crp_nbits + 7) / 8;
529                 if (size == 0)
530                         continue;
531                 MALLOC(krp->krp_param[i].crp_p, caddr_t, size, M_XDATA, M_WAITOK);
532                 if (i >= krp->krp_iparams)
533                         continue;
534                 error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size);
535                 if (error)
536                         goto fail;
537         }
538
539         error = crypto_kdispatch(krp);
540         if (error == 0)
541                 error = tsleep(krp, PSOCK, "crydev", 0);
542         if (error)
543                 goto fail;
544         
545         if (krp->krp_status != 0) {
546                 error = krp->krp_status;
547                 goto fail;
548         }
549
550         for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
551                 size = (krp->krp_param[i].crp_nbits + 7) / 8;
552                 if (size == 0)
553                         continue;
554                 error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size);
555                 if (error)
556                         goto fail;
557         }
558
559 fail:
560         if (krp) {
561                 kop->crk_status = krp->krp_status;
562                 for (i = 0; i < CRK_MAXPARAM; i++) {
563                         if (krp->krp_param[i].crp_p)
564                                 FREE(krp->krp_param[i].crp_p, M_XDATA);
565                 }
566                 free(krp, M_XDATA);
567         }
568         return (error);
569 }
570
571 /* ARGSUSED */
572 static int
573 cryptof_poll(
574         struct file *fp,
575         int events,
576         struct ucred *active_cred,
577         struct thread *td)
578 {
579
580         return (0);
581 }
582
583 /* ARGSUSED */
584 static int
585 cryptof_kqfilter(struct file *fp, struct knote *kn)
586 {
587
588         return (0);
589 }
590
591 /* ARGSUSED */
592 static int
593 cryptof_stat(
594         struct file *fp,
595         struct stat *sb,
596         struct thread *td)
597 {
598
599         return (EOPNOTSUPP);
600 }
601
602 /* ARGSUSED */
603 static int
604 cryptof_close(struct file *fp, struct thread *td)
605 {
606         struct fcrypt *fcr = (struct fcrypt *)fp->f_data;
607         struct csession *cse;
608
609         while ((cse = TAILQ_FIRST(&fcr->csessions))) {
610                 TAILQ_REMOVE(&fcr->csessions, cse, next);
611                 (void)csefree(cse);
612         }
613         FREE(fcr, M_XDATA);
614         fp->f_data = NULL;
615         return 0;
616 }
617
618 static struct csession *
619 csefind(struct fcrypt *fcr, u_int ses)
620 {
621         struct csession *cse;
622
623         TAILQ_FOREACH(cse, &fcr->csessions, next)
624                 if (cse->ses == ses)
625                         return (cse);
626         return (NULL);
627 }
628
629 static int
630 csedelete(struct fcrypt *fcr, struct csession *cse_del)
631 {
632         struct csession *cse;
633
634         TAILQ_FOREACH(cse, &fcr->csessions, next) {
635                 if (cse == cse_del) {
636                         TAILQ_REMOVE(&fcr->csessions, cse, next);
637                         return (1);
638                 }
639         }
640         return (0);
641 }
642         
643 static struct csession *
644 cseadd(struct fcrypt *fcr, struct csession *cse)
645 {
646         TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
647         cse->ses = fcr->sesn++;
648         return (cse);
649 }
650
651 struct csession *
652 csecreate(struct fcrypt *fcr, u_int64_t sid, caddr_t key, u_int64_t keylen,
653     caddr_t mackey, u_int64_t mackeylen, u_int32_t cipher, u_int32_t mac,
654     struct enc_xform *txform, struct auth_hash *thash)
655 {
656         struct csession *cse;
657
658         MALLOC(cse, struct csession *, sizeof(struct csession),
659             M_XDATA, M_NOWAIT);
660         if (cse == NULL)
661                 return NULL;
662         cse->key = key;
663         cse->keylen = keylen/8;
664         cse->mackey = mackey;
665         cse->mackeylen = mackeylen/8;
666         cse->sid = sid;
667         cse->cipher = cipher;
668         cse->mac = mac;
669         cse->txform = txform;
670         cse->thash = thash;
671         cseadd(fcr, cse);
672         return (cse);
673 }
674
675 static int
676 csefree(struct csession *cse)
677 {
678         int error;
679
680         error = crypto_freesession(cse->sid);
681         if (cse->key)
682                 FREE(cse->key, M_XDATA);
683         if (cse->mackey)
684                 FREE(cse->mackey, M_XDATA);
685         FREE(cse, M_XDATA);
686         return (error);
687 }
688
689 static int
690 cryptoopen(dev_t dev, int oflags, int devtype, struct thread *td)
691 {
692         if (crypto_usercrypto == 0)
693                 return (ENXIO);
694         return (0);
695 }
696
697 static int
698 cryptoread(dev_t dev, struct uio *uio, int ioflag)
699 {
700         return (EIO);
701 }
702
703 static int
704 cryptowrite(dev_t dev, struct uio *uio, int ioflag)
705 {
706         return (EIO);
707 }
708
709 static int
710 cryptoioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
711 {
712         struct file *f;
713         struct fcrypt *fcr;
714         int fd, error;
715         switch (cmd) {
716         case CRIOGET:
717                 MALLOC(fcr, struct fcrypt *,
718                     sizeof(struct fcrypt), M_XDATA, M_WAITOK);
719                 TAILQ_INIT(&fcr->csessions);
720                 fcr->sesn = 0;
721
722                 KKASSERT(td->td_proc);
723                 error = falloc(td->td_proc, &f, &fd);
724
725                 if (error) {
726                         FREE(fcr, M_XDATA);
727                         return (error);
728                 }
729                 fhold(f);
730                 f->f_flag = FREAD | FWRITE;
731                 f->f_type = DTYPE_CRYPTO;
732                 f->f_ops = &cryptofops;
733                 f->f_data = (caddr_t) fcr;
734                 *(u_int32_t *)data = fd;
735                 fdrop(f, td);
736                 break;
737         default:
738                 error = EINVAL;
739                 break;
740         }
741         return (error);
742 }
743
744 #define CRYPTO_MAJOR    70              /* from openbsd */
745 static struct cdevsw crypto_cdevsw = {
746         /* open */      cryptoopen,
747         /* close */     nullclose,
748         /* read */      cryptoread,
749         /* write */     cryptowrite,
750         /* ioctl */     cryptoioctl,
751         /* poll */      nopoll,
752         /* mmap */      nommap,
753         /* strategy */  nostrategy,
754         /* dev name */  "crypto",
755         /* dev major */ CRYPTO_MAJOR,
756         /* dump */      nodump,
757         /* psize */     nopsize,
758         /* flags */     0,
759         /* kqfilter */  NULL
760 };
761 static dev_t crypto_dev;
762
763 /*
764  * Initialization code, both for static and dynamic loading.
765  */
766 static int
767 cryptodev_modevent(module_t mod, int type, void *unused)
768 {
769         switch (type) {
770         case MOD_LOAD:
771                 if (bootverbose)
772                         printf("crypto: <crypto device>\n");
773                 crypto_dev = make_dev(&crypto_cdevsw, 0, 
774                                       UID_ROOT, GID_WHEEL, 0666,
775                                       "crypto");
776                 return 0;
777         case MOD_UNLOAD:
778                 /*XXX disallow if active sessions */
779                 destroy_dev(crypto_dev);
780                 return 0;
781         }
782         return EINVAL;
783 }
784
785 static moduledata_t cryptodev_mod = {
786         "cryptodev",
787         cryptodev_modevent,
788         0
789 };
790 MODULE_VERSION(cryptodev, 1);
791 DECLARE_MODULE(cryptodev, cryptodev_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
792 #if 0
793 MODULE_DEPEND(cryptodev, crypto, 1, 1, 1);
794 #endif