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