]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/opencrypto/cryptodev.c
Support AEAD requests with non-GCM algorithms.
[FreeBSD/FreeBSD.git] / sys / opencrypto / cryptodev.c
1 /*      $OpenBSD: cryptodev.c,v 1.52 2002/06/19 07:22:46 deraadt Exp $  */
2
3 /*-
4  * Copyright (c) 2001 Theo de Raadt
5  * Copyright (c) 2002-2006 Sam Leffler, Errno Consulting
6  * Copyright (c) 2014 The FreeBSD Foundation
7  * All rights reserved.
8  *
9  * Portions of this software were developed by John-Mark Gurney
10  * under sponsorship of the FreeBSD Foundation and
11  * Rubicon Communications, LLC (Netgate).
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  *
17  * 1. Redistributions of source code must retain the above copyright
18  *   notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *   notice, this list of conditions and the following disclaimer in the
21  *   documentation and/or other materials provided with the distribution.
22  * 3. The name of the author may not be used to endorse or promote products
23  *   derived from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  *
36  * Effort sponsored in part by the Defense Advanced Research Projects
37  * Agency (DARPA) and Air Force Research Laboratory, Air Force
38  * Materiel Command, USAF, under agreement number F30602-01-2-0537.
39  */
40
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43
44 #include "opt_compat.h"
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/malloc.h>
49 #include <sys/mbuf.h>
50 #include <sys/lock.h>
51 #include <sys/mutex.h>
52 #include <sys/sysctl.h>
53 #include <sys/file.h>
54 #include <sys/filedesc.h>
55 #include <sys/errno.h>
56 #include <sys/uio.h>
57 #include <sys/random.h>
58 #include <sys/conf.h>
59 #include <sys/kernel.h>
60 #include <sys/module.h>
61 #include <sys/fcntl.h>
62 #include <sys/bus.h>
63 #include <sys/user.h>
64 #include <sys/sdt.h>
65
66 #include <opencrypto/cryptodev.h>
67 #include <opencrypto/xform.h>
68
69 SDT_PROVIDER_DECLARE(opencrypto);
70
71 SDT_PROBE_DEFINE1(opencrypto, dev, ioctl, error, "int"/*line number*/);
72
73 #ifdef COMPAT_FREEBSD32
74 #include <sys/mount.h>
75 #include <compat/freebsd32/freebsd32.h>
76
77 struct session_op32 {
78         u_int32_t       cipher;
79         u_int32_t       mac;
80         u_int32_t       keylen;
81         u_int32_t       key;
82         int             mackeylen;
83         u_int32_t       mackey;
84         u_int32_t       ses;
85 };
86
87 struct session2_op32 {
88         u_int32_t       cipher;
89         u_int32_t       mac;
90         u_int32_t       keylen;
91         u_int32_t       key;
92         int             mackeylen;
93         u_int32_t       mackey;
94         u_int32_t       ses;
95         int             crid;
96         int             pad[4];
97 };
98
99 struct crypt_op32 {
100         u_int32_t       ses;
101         u_int16_t       op;
102         u_int16_t       flags;
103         u_int           len;
104         u_int32_t       src, dst;
105         u_int32_t       mac;
106         u_int32_t       iv;
107 };
108
109 struct crparam32 {
110         u_int32_t       crp_p;
111         u_int           crp_nbits;
112 };
113
114 struct crypt_kop32 {
115         u_int           crk_op;
116         u_int           crk_status;
117         u_short         crk_iparams;
118         u_short         crk_oparams;
119         u_int           crk_crid;
120         struct crparam32        crk_param[CRK_MAXPARAM];
121 };
122
123 struct cryptotstat32 {
124         struct timespec32       acc;
125         struct timespec32       min;
126         struct timespec32       max;
127         u_int32_t       count;
128 };
129
130 struct cryptostats32 {
131         u_int32_t       cs_ops;
132         u_int32_t       cs_errs;
133         u_int32_t       cs_kops;
134         u_int32_t       cs_kerrs;
135         u_int32_t       cs_intrs;
136         u_int32_t       cs_rets;
137         u_int32_t       cs_blocks;
138         u_int32_t       cs_kblocks;
139         struct cryptotstat32 cs_invoke;
140         struct cryptotstat32 cs_done;
141         struct cryptotstat32 cs_cb;
142         struct cryptotstat32 cs_finis;
143 };
144
145 #define CIOCGSESSION32  _IOWR('c', 101, struct session_op32)
146 #define CIOCCRYPT32     _IOWR('c', 103, struct crypt_op32)
147 #define CIOCKEY32       _IOWR('c', 104, struct crypt_kop32)
148 #define CIOCGSESSION232 _IOWR('c', 106, struct session2_op32)
149 #define CIOCKEY232      _IOWR('c', 107, struct crypt_kop32)
150
151 static void
152 session_op_from_32(const struct session_op32 *from, struct session_op *to)
153 {
154
155         CP(*from, *to, cipher);
156         CP(*from, *to, mac);
157         CP(*from, *to, keylen);
158         PTRIN_CP(*from, *to, key);
159         CP(*from, *to, mackeylen);
160         PTRIN_CP(*from, *to, mackey);
161         CP(*from, *to, ses);
162 }
163
164 static void
165 session2_op_from_32(const struct session2_op32 *from, struct session2_op *to)
166 {
167
168         session_op_from_32((const struct session_op32 *)from,
169             (struct session_op *)to);
170         CP(*from, *to, crid);
171 }
172
173 static void
174 session_op_to_32(const struct session_op *from, struct session_op32 *to)
175 {
176
177         CP(*from, *to, cipher);
178         CP(*from, *to, mac);
179         CP(*from, *to, keylen);
180         PTROUT_CP(*from, *to, key);
181         CP(*from, *to, mackeylen);
182         PTROUT_CP(*from, *to, mackey);
183         CP(*from, *to, ses);
184 }
185
186 static void
187 session2_op_to_32(const struct session2_op *from, struct session2_op32 *to)
188 {
189
190         session_op_to_32((const struct session_op *)from,
191             (struct session_op32 *)to);
192         CP(*from, *to, crid);
193 }
194
195 static void
196 crypt_op_from_32(const struct crypt_op32 *from, struct crypt_op *to)
197 {
198
199         CP(*from, *to, ses);
200         CP(*from, *to, op);
201         CP(*from, *to, flags);
202         CP(*from, *to, len);
203         PTRIN_CP(*from, *to, src);
204         PTRIN_CP(*from, *to, dst);
205         PTRIN_CP(*from, *to, mac);
206         PTRIN_CP(*from, *to, iv);
207 }
208
209 static void
210 crypt_op_to_32(const struct crypt_op *from, struct crypt_op32 *to)
211 {
212
213         CP(*from, *to, ses);
214         CP(*from, *to, op);
215         CP(*from, *to, flags);
216         CP(*from, *to, len);
217         PTROUT_CP(*from, *to, src);
218         PTROUT_CP(*from, *to, dst);
219         PTROUT_CP(*from, *to, mac);
220         PTROUT_CP(*from, *to, iv);
221 }
222
223 static void
224 crparam_from_32(const struct crparam32 *from, struct crparam *to)
225 {
226
227         PTRIN_CP(*from, *to, crp_p);
228         CP(*from, *to, crp_nbits);
229 }
230
231 static void
232 crparam_to_32(const struct crparam *from, struct crparam32 *to)
233 {
234
235         PTROUT_CP(*from, *to, crp_p);
236         CP(*from, *to, crp_nbits);
237 }
238
239 static void
240 crypt_kop_from_32(const struct crypt_kop32 *from, struct crypt_kop *to)
241 {
242         int i;
243
244         CP(*from, *to, crk_op);
245         CP(*from, *to, crk_status);
246         CP(*from, *to, crk_iparams);
247         CP(*from, *to, crk_oparams);
248         CP(*from, *to, crk_crid);
249         for (i = 0; i < CRK_MAXPARAM; i++)
250                 crparam_from_32(&from->crk_param[i], &to->crk_param[i]);
251 }
252
253 static void
254 crypt_kop_to_32(const struct crypt_kop *from, struct crypt_kop32 *to)
255 {
256         int i;
257
258         CP(*from, *to, crk_op);
259         CP(*from, *to, crk_status);
260         CP(*from, *to, crk_iparams);
261         CP(*from, *to, crk_oparams);
262         CP(*from, *to, crk_crid);
263         for (i = 0; i < CRK_MAXPARAM; i++)
264                 crparam_to_32(&from->crk_param[i], &to->crk_param[i]);
265 }
266 #endif
267
268 struct csession {
269         TAILQ_ENTRY(csession) next;
270         u_int64_t       sid;
271         u_int32_t       ses;
272         struct mtx      lock;           /* for op submission */
273
274         u_int32_t       cipher;
275         struct enc_xform *txform;
276         u_int32_t       mac;
277         struct auth_hash *thash;
278
279         caddr_t         key;
280         int             keylen;
281         u_char          tmp_iv[EALG_MAX_BLOCK_LEN];
282
283         caddr_t         mackey;
284         int             mackeylen;
285
286         struct iovec    iovec;
287         struct uio      uio;
288         int             error;
289 };
290
291 struct fcrypt {
292         TAILQ_HEAD(csessionlist, csession) csessions;
293         int             sesn;
294 };
295
296 static  int cryptof_ioctl(struct file *, u_long, void *,
297                     struct ucred *, struct thread *);
298 static  int cryptof_stat(struct file *, struct stat *,
299                     struct ucred *, struct thread *);
300 static  int cryptof_close(struct file *, struct thread *);
301 static  int cryptof_fill_kinfo(struct file *, struct kinfo_file *,
302                     struct filedesc *);
303
304 static struct fileops cryptofops = {
305     .fo_read = invfo_rdwr,
306     .fo_write = invfo_rdwr,
307     .fo_truncate = invfo_truncate,
308     .fo_ioctl = cryptof_ioctl,
309     .fo_poll = invfo_poll,
310     .fo_kqfilter = invfo_kqfilter,
311     .fo_stat = cryptof_stat,
312     .fo_close = cryptof_close,
313     .fo_chmod = invfo_chmod,
314     .fo_chown = invfo_chown,
315     .fo_sendfile = invfo_sendfile,
316     .fo_fill_kinfo = cryptof_fill_kinfo,
317 };
318
319 static struct csession *csefind(struct fcrypt *, u_int);
320 static int csedelete(struct fcrypt *, struct csession *);
321 static struct csession *cseadd(struct fcrypt *, struct csession *);
322 static struct csession *csecreate(struct fcrypt *, u_int64_t, caddr_t,
323     u_int64_t, caddr_t, u_int64_t, u_int32_t, u_int32_t, struct enc_xform *,
324     struct auth_hash *);
325 static int csefree(struct csession *);
326
327 static  int cryptodev_op(struct csession *, struct crypt_op *,
328                         struct ucred *, struct thread *td);
329 static  int cryptodev_aead(struct csession *, struct crypt_aead *,
330                         struct ucred *, struct thread *);
331 static  int cryptodev_key(struct crypt_kop *);
332 static  int cryptodev_find(struct crypt_find_op *);
333
334 /*
335  * Check a crypto identifier to see if it requested
336  * a software device/driver.  This can be done either
337  * by device name/class or through search constraints.
338  */
339 static int
340 checkforsoftware(int *cridp)
341 {
342         int crid;
343
344         crid = *cridp;
345
346         if (!crypto_devallowsoft) {
347                 if (crid & CRYPTOCAP_F_SOFTWARE) {
348                         if (crid & CRYPTOCAP_F_HARDWARE) {
349                                 *cridp = CRYPTOCAP_F_HARDWARE;
350                                 return 0;
351                         }
352                         return EINVAL;
353                 }
354                 if ((crid & CRYPTOCAP_F_HARDWARE) == 0 &&
355                     (crypto_getcaps(crid) & CRYPTOCAP_F_HARDWARE) == 0)
356                         return EINVAL;
357         }
358         return 0;
359 }
360
361 /* ARGSUSED */
362 static int
363 cryptof_ioctl(
364         struct file *fp,
365         u_long cmd,
366         void *data,
367         struct ucred *active_cred,
368         struct thread *td)
369 {
370 #define SES2(p) ((struct session2_op *)p)
371         struct cryptoini cria, crie;
372         struct fcrypt *fcr = fp->f_data;
373         struct csession *cse;
374         struct session_op *sop;
375         struct crypt_op *cop;
376         struct crypt_aead *caead;
377         struct enc_xform *txform = NULL;
378         struct auth_hash *thash = NULL;
379         struct crypt_kop *kop;
380         u_int64_t sid;
381         u_int32_t ses;
382         int error = 0, crid;
383 #ifdef COMPAT_FREEBSD32
384         struct session2_op sopc;
385         struct crypt_op copc;
386         struct crypt_kop kopc;
387 #endif
388
389         switch (cmd) {
390         case CIOCGSESSION:
391         case CIOCGSESSION2:
392 #ifdef COMPAT_FREEBSD32
393         case CIOCGSESSION32:
394         case CIOCGSESSION232:
395                 if (cmd == CIOCGSESSION32) {
396                         session_op_from_32(data, (struct session_op *)&sopc);
397                         sop = (struct session_op *)&sopc;
398                 } else if (cmd == CIOCGSESSION232) {
399                         session2_op_from_32(data, &sopc);
400                         sop = (struct session_op *)&sopc;
401                 } else
402 #endif
403                         sop = (struct session_op *)data;
404                 switch (sop->cipher) {
405                 case 0:
406                         break;
407                 case CRYPTO_DES_CBC:
408                         txform = &enc_xform_des;
409                         break;
410                 case CRYPTO_3DES_CBC:
411                         txform = &enc_xform_3des;
412                         break;
413                 case CRYPTO_BLF_CBC:
414                         txform = &enc_xform_blf;
415                         break;
416                 case CRYPTO_CAST_CBC:
417                         txform = &enc_xform_cast5;
418                         break;
419                 case CRYPTO_SKIPJACK_CBC:
420                         txform = &enc_xform_skipjack;
421                         break;
422                 case CRYPTO_AES_CBC:
423                         txform = &enc_xform_rijndael128;
424                         break;
425                 case CRYPTO_AES_XTS:
426                         txform = &enc_xform_aes_xts;
427                         break;
428                 case CRYPTO_NULL_CBC:
429                         txform = &enc_xform_null;
430                         break;
431                 case CRYPTO_ARC4:
432                         txform = &enc_xform_arc4;
433                         break;
434                 case CRYPTO_CAMELLIA_CBC:
435                         txform = &enc_xform_camellia;
436                         break;
437                 case CRYPTO_AES_ICM:
438                         txform = &enc_xform_aes_icm;
439                         break;
440                 case CRYPTO_AES_NIST_GCM_16:
441                         txform = &enc_xform_aes_nist_gcm;
442                         break;
443
444                 default:
445                         CRYPTDEB("invalid cipher");
446                         return (EINVAL);
447                 }
448
449                 switch (sop->mac) {
450                 case 0:
451                         break;
452                 case CRYPTO_MD5_HMAC:
453                         thash = &auth_hash_hmac_md5;
454                         break;
455                 case CRYPTO_SHA1_HMAC:
456                         thash = &auth_hash_hmac_sha1;
457                         break;
458                 case CRYPTO_SHA2_256_HMAC:
459                         thash = &auth_hash_hmac_sha2_256;
460                         break;
461                 case CRYPTO_SHA2_384_HMAC:
462                         thash = &auth_hash_hmac_sha2_384;
463                         break;
464                 case CRYPTO_SHA2_512_HMAC:
465                         thash = &auth_hash_hmac_sha2_512;
466                         break;
467                 case CRYPTO_RIPEMD160_HMAC:
468                         thash = &auth_hash_hmac_ripemd_160;
469                         break;
470                 case CRYPTO_AES_128_NIST_GMAC:
471                         thash = &auth_hash_nist_gmac_aes_128;
472                         break;
473                 case CRYPTO_AES_192_NIST_GMAC:
474                         thash = &auth_hash_nist_gmac_aes_192;
475                         break;
476                 case CRYPTO_AES_256_NIST_GMAC:
477                         thash = &auth_hash_nist_gmac_aes_256;
478                         break;
479
480 #ifdef notdef
481                 case CRYPTO_MD5:
482                         thash = &auth_hash_md5;
483                         break;
484                 case CRYPTO_SHA1:
485                         thash = &auth_hash_sha1;
486                         break;
487 #endif
488                 case CRYPTO_NULL_HMAC:
489                         thash = &auth_hash_null;
490                         break;
491                 default:
492                         CRYPTDEB("invalid mac");
493                         return (EINVAL);
494                 }
495
496                 bzero(&crie, sizeof(crie));
497                 bzero(&cria, sizeof(cria));
498
499                 if (txform) {
500                         crie.cri_alg = txform->type;
501                         crie.cri_klen = sop->keylen * 8;
502                         if (sop->keylen > txform->maxkey ||
503                             sop->keylen < txform->minkey) {
504                                 CRYPTDEB("invalid cipher parameters");
505                                 error = EINVAL;
506                                 goto bail;
507                         }
508
509                         crie.cri_key = malloc(crie.cri_klen / 8,
510                             M_XDATA, M_WAITOK);
511                         if ((error = copyin(sop->key, crie.cri_key,
512                             crie.cri_klen / 8))) {
513                                 CRYPTDEB("invalid key");
514                                 goto bail;
515                         }
516                         if (thash)
517                                 crie.cri_next = &cria;
518                 }
519
520                 if (thash) {
521                         cria.cri_alg = thash->type;
522                         cria.cri_klen = sop->mackeylen * 8;
523                         if (sop->mackeylen != thash->keysize) {
524                                 CRYPTDEB("invalid mac key length");
525                                 error = EINVAL;
526                                 goto bail;
527                         }
528
529                         if (cria.cri_klen) {
530                                 cria.cri_key = malloc(cria.cri_klen / 8,
531                                     M_XDATA, M_WAITOK);
532                                 if ((error = copyin(sop->mackey, cria.cri_key,
533                                     cria.cri_klen / 8))) {
534                                         CRYPTDEB("invalid mac key");
535                                         goto bail;
536                                 }
537                         }
538                 }
539
540                 /* NB: CIOCGSESSION2 has the crid */
541                 if (cmd == CIOCGSESSION2
542 #ifdef COMPAT_FREEBSD32
543                     || cmd == CIOCGSESSION232
544 #endif
545                         ) {
546                         crid = SES2(sop)->crid;
547                         error = checkforsoftware(&crid);
548                         if (error) {
549                                 CRYPTDEB("checkforsoftware");
550                                 goto bail;
551                         }
552                 } else
553                         crid = CRYPTOCAP_F_HARDWARE;
554                 error = crypto_newsession(&sid, (txform ? &crie : &cria), crid);
555                 if (error) {
556                         CRYPTDEB("crypto_newsession");
557                         goto bail;
558                 }
559
560                 cse = csecreate(fcr, sid, crie.cri_key, crie.cri_klen,
561                     cria.cri_key, cria.cri_klen, sop->cipher, sop->mac, txform,
562                     thash);
563
564                 if (cse == NULL) {
565                         crypto_freesession(sid);
566                         error = EINVAL;
567                         CRYPTDEB("csecreate");
568                         goto bail;
569                 }
570                 sop->ses = cse->ses;
571                 if (cmd == CIOCGSESSION2
572 #ifdef COMPAT_FREEBSD32
573                     || cmd == CIOCGSESSION232
574 #endif
575                     ) {
576                         /* return hardware/driver id */
577                         SES2(sop)->crid = CRYPTO_SESID2HID(cse->sid);
578                 }
579 bail:
580                 if (error) {
581                         if (crie.cri_key)
582                                 free(crie.cri_key, M_XDATA);
583                         if (cria.cri_key)
584                                 free(cria.cri_key, M_XDATA);
585                 }
586 #ifdef COMPAT_FREEBSD32
587                 else {
588                         if (cmd == CIOCGSESSION32)
589                                 session_op_to_32(sop, data);
590                         else if (cmd == CIOCGSESSION232)
591                                 session2_op_to_32((struct session2_op *)sop,
592                                     data);
593                 }
594 #endif
595                 break;
596         case CIOCFSESSION:
597                 ses = *(u_int32_t *)data;
598                 cse = csefind(fcr, ses);
599                 if (cse == NULL)
600                         return (EINVAL);
601                 csedelete(fcr, cse);
602                 error = csefree(cse);
603                 break;
604         case CIOCCRYPT:
605 #ifdef COMPAT_FREEBSD32
606         case CIOCCRYPT32:
607                 if (cmd == CIOCCRYPT32) {
608                         cop = &copc;
609                         crypt_op_from_32(data, cop);
610                 } else
611 #endif
612                         cop = (struct crypt_op *)data;
613                 cse = csefind(fcr, cop->ses);
614                 if (cse == NULL) {
615                         SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
616                         return (EINVAL);
617                 }
618                 error = cryptodev_op(cse, cop, active_cred, td);
619 #ifdef COMPAT_FREEBSD32
620                 if (error == 0 && cmd == CIOCCRYPT32)
621                         crypt_op_to_32(cop, data);
622 #endif
623                 break;
624         case CIOCKEY:
625         case CIOCKEY2:
626 #ifdef COMPAT_FREEBSD32
627         case CIOCKEY32:
628         case CIOCKEY232:
629 #endif
630                 if (!crypto_userasymcrypto)
631                         return (EPERM);         /* XXX compat? */
632 #ifdef COMPAT_FREEBSD32
633                 if (cmd == CIOCKEY32 || cmd == CIOCKEY232) {
634                         kop = &kopc;
635                         crypt_kop_from_32(data, kop);
636                 } else
637 #endif
638                         kop = (struct crypt_kop *)data;
639                 if (cmd == CIOCKEY
640 #ifdef COMPAT_FREEBSD32
641                     || cmd == CIOCKEY32
642 #endif
643                     ) {
644                         /* NB: crypto core enforces s/w driver use */
645                         kop->crk_crid =
646                             CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE;
647                 }
648                 mtx_lock(&Giant);
649                 error = cryptodev_key(kop);
650                 mtx_unlock(&Giant);
651 #ifdef COMPAT_FREEBSD32
652                 if (cmd == CIOCKEY32 || cmd == CIOCKEY232)
653                         crypt_kop_to_32(kop, data);
654 #endif
655                 break;
656         case CIOCASYMFEAT:
657                 if (!crypto_userasymcrypto) {
658                         /*
659                          * NB: if user asym crypto operations are
660                          * not permitted return "no algorithms"
661                          * so well-behaved applications will just
662                          * fallback to doing them in software.
663                          */
664                         *(int *)data = 0;
665                 } else
666                         error = crypto_getfeat((int *)data);
667                 break;
668         case CIOCFINDDEV:
669                 error = cryptodev_find((struct crypt_find_op *)data);
670                 break;
671         case CIOCCRYPTAEAD:
672                 caead = (struct crypt_aead *)data;
673                 cse = csefind(fcr, caead->ses);
674                 if (cse == NULL)
675                         return (EINVAL);
676                 error = cryptodev_aead(cse, caead, active_cred, td);
677                 break;
678         default:
679                 error = EINVAL;
680                 break;
681         }
682         return (error);
683 #undef SES2
684 }
685
686 static int cryptodev_cb(void *);
687
688
689 static int
690 cryptodev_op(
691         struct csession *cse,
692         struct crypt_op *cop,
693         struct ucred *active_cred,
694         struct thread *td)
695 {
696         struct cryptop *crp = NULL;
697         struct cryptodesc *crde = NULL, *crda = NULL;
698         int error;
699
700         if (cop->len > 256*1024-4) {
701                 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
702                 return (E2BIG);
703         }
704
705         if (cse->txform) {
706                 if (cop->len == 0 || (cop->len % cse->txform->blocksize) != 0) {
707                         SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
708                         return (EINVAL);
709                 }
710         }
711
712         cse->uio.uio_iov = &cse->iovec;
713         cse->uio.uio_iovcnt = 1;
714         cse->uio.uio_offset = 0;
715         cse->uio.uio_resid = cop->len;
716         cse->uio.uio_segflg = UIO_SYSSPACE;
717         cse->uio.uio_rw = UIO_WRITE;
718         cse->uio.uio_td = td;
719         cse->uio.uio_iov[0].iov_len = cop->len;
720         if (cse->thash) {
721                 cse->uio.uio_iov[0].iov_len += cse->thash->hashsize;
722                 cse->uio.uio_resid += cse->thash->hashsize;
723         }
724         cse->uio.uio_iov[0].iov_base = malloc(cse->uio.uio_iov[0].iov_len,
725             M_XDATA, M_WAITOK);
726
727         crp = crypto_getreq((cse->txform != NULL) + (cse->thash != NULL));
728         if (crp == NULL) {
729                 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
730                 error = ENOMEM;
731                 goto bail;
732         }
733
734         if (cse->thash && cse->txform) {
735                 if (cop->flags & COP_F_CIPHER_FIRST) {
736                         crde = crp->crp_desc;
737                         crda = crde->crd_next;
738                 } else {
739                         crda = crp->crp_desc;
740                         crde = crda->crd_next;
741                 }
742         } else if (cse->thash) {
743                 crda = crp->crp_desc;
744         } else if (cse->txform) {
745                 crde = crp->crp_desc;
746         } else {
747                 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
748                 error = EINVAL;
749                 goto bail;
750         }
751
752         if ((error = copyin(cop->src, cse->uio.uio_iov[0].iov_base,
753             cop->len))) {
754                 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
755                 goto bail;
756         }
757
758         if (crda) {
759                 crda->crd_skip = 0;
760                 crda->crd_len = cop->len;
761                 crda->crd_inject = cop->len;
762
763                 crda->crd_alg = cse->mac;
764                 crda->crd_key = cse->mackey;
765                 crda->crd_klen = cse->mackeylen * 8;
766         }
767
768         if (crde) {
769                 if (cop->op == COP_ENCRYPT)
770                         crde->crd_flags |= CRD_F_ENCRYPT;
771                 else
772                         crde->crd_flags &= ~CRD_F_ENCRYPT;
773                 crde->crd_len = cop->len;
774                 crde->crd_inject = 0;
775
776                 crde->crd_alg = cse->cipher;
777                 crde->crd_key = cse->key;
778                 crde->crd_klen = cse->keylen * 8;
779         }
780
781         crp->crp_ilen = cop->len;
782         crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM
783                        | (cop->flags & COP_F_BATCH);
784         crp->crp_buf = (caddr_t)&cse->uio;
785         crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_cb;
786         crp->crp_sid = cse->sid;
787         crp->crp_opaque = (void *)cse;
788
789         if (cop->iv) {
790                 if (crde == NULL) {
791                         SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
792                         error = EINVAL;
793                         goto bail;
794                 }
795                 if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
796                         SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
797                         error = EINVAL;
798                         goto bail;
799                 }
800                 if ((error = copyin(cop->iv, cse->tmp_iv,
801                     cse->txform->blocksize))) {
802                         SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
803                         goto bail;
804                 }
805                 bcopy(cse->tmp_iv, crde->crd_iv, cse->txform->blocksize);
806                 crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
807                 crde->crd_skip = 0;
808         } else if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
809                 crde->crd_skip = 0;
810         } else if (crde) {
811                 crde->crd_flags |= CRD_F_IV_PRESENT;
812                 crde->crd_skip = cse->txform->blocksize;
813                 crde->crd_len -= cse->txform->blocksize;
814         }
815
816         if (cop->mac && crda == NULL) {
817                 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
818                 error = EINVAL;
819                 goto bail;
820         }
821
822 again:
823         /*
824          * Let the dispatch run unlocked, then, interlock against the
825          * callback before checking if the operation completed and going
826          * to sleep.  This insures drivers don't inherit our lock which
827          * results in a lock order reversal between crypto_dispatch forced
828          * entry and the crypto_done callback into us.
829          */
830         error = crypto_dispatch(crp);
831         mtx_lock(&cse->lock);
832         if (error == 0 && (crp->crp_flags & CRYPTO_F_DONE) == 0)
833                 error = msleep(crp, &cse->lock, PWAIT, "crydev", 0);
834         mtx_unlock(&cse->lock);
835
836         if (error != 0) {
837                 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
838                 goto bail;
839         }
840
841         if (crp->crp_etype == EAGAIN) {
842                 crp->crp_etype = 0;
843                 crp->crp_flags &= ~CRYPTO_F_DONE;
844                 goto again;
845         }
846
847         if (crp->crp_etype != 0) {
848                 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
849                 error = crp->crp_etype;
850                 goto bail;
851         }
852
853         if (cse->error) {
854                 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
855                 error = cse->error;
856                 goto bail;
857         }
858
859         if (cop->dst &&
860             (error = copyout(cse->uio.uio_iov[0].iov_base, cop->dst,
861             cop->len))) {
862                 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
863                 goto bail;
864         }
865
866         if (cop->mac &&
867             (error = copyout((caddr_t)cse->uio.uio_iov[0].iov_base + cop->len,
868             cop->mac, cse->thash->hashsize))) {
869                 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
870                 goto bail;
871         }
872
873 bail:
874         if (crp)
875                 crypto_freereq(crp);
876         if (cse->uio.uio_iov[0].iov_base)
877                 free(cse->uio.uio_iov[0].iov_base, M_XDATA);
878
879         return (error);
880 }
881
882 static int
883 cryptodev_aead(
884         struct csession *cse,
885         struct crypt_aead *caead,
886         struct ucred *active_cred,
887         struct thread *td)
888 {
889         struct uio *uio;
890         struct cryptop *crp = NULL;
891         struct cryptodesc *crde = NULL, *crda = NULL;
892         int error;
893
894         if (caead->len > 256*1024-4 || caead->aadlen > 256*1024-4)
895                 return (E2BIG);
896
897         if (cse->txform == NULL || cse->thash == NULL || caead->tag == NULL ||
898             (caead->len % cse->txform->blocksize) != 0)
899                 return (EINVAL);
900
901         uio = &cse->uio;
902         uio->uio_iov = &cse->iovec;
903         uio->uio_iovcnt = 1;
904         uio->uio_offset = 0;
905         uio->uio_resid = caead->aadlen + caead->len + cse->thash->hashsize;
906         uio->uio_segflg = UIO_SYSSPACE;
907         uio->uio_rw = UIO_WRITE;
908         uio->uio_td = td;
909         uio->uio_iov[0].iov_len = uio->uio_resid;
910
911         uio->uio_iov[0].iov_base = malloc(uio->uio_iov[0].iov_len,
912             M_XDATA, M_WAITOK);
913
914         crp = crypto_getreq(2);
915         if (crp == NULL) {
916                 error = ENOMEM;
917                 goto bail;
918         }
919
920         if (caead->flags & COP_F_CIPHER_FIRST) {
921                 crde = crp->crp_desc;
922                 crda = crde->crd_next;
923         } else {
924                 crda = crp->crp_desc;
925                 crde = crda->crd_next;
926         }
927
928         if ((error = copyin(caead->aad, cse->uio.uio_iov[0].iov_base,
929             caead->aadlen)))
930                 goto bail;
931
932         if ((error = copyin(caead->src, (char *)cse->uio.uio_iov[0].iov_base +
933             caead->aadlen, caead->len)))
934                 goto bail;
935
936         /*
937          * For GCM, crd_len covers only the AAD.  For other ciphers
938          * chained with an HMAC, crd_len covers both the AAD and the
939          * cipher text.
940          */
941         crda->crd_skip = 0;
942         if (cse->cipher == CRYPTO_AES_NIST_GCM_16)
943                 crda->crd_len = caead->aadlen;
944         else
945                 crda->crd_len = caead->aadlen + caead->len;
946         crda->crd_inject = caead->aadlen + caead->len;
947
948         crda->crd_alg = cse->mac;
949         crda->crd_key = cse->mackey;
950         crda->crd_klen = cse->mackeylen * 8;
951
952         if (caead->op == COP_ENCRYPT)
953                 crde->crd_flags |= CRD_F_ENCRYPT;
954         else
955                 crde->crd_flags &= ~CRD_F_ENCRYPT;
956         crde->crd_skip = caead->aadlen;
957         crde->crd_len = caead->len;
958         crde->crd_inject = caead->aadlen;
959
960         crde->crd_alg = cse->cipher;
961         crde->crd_key = cse->key;
962         crde->crd_klen = cse->keylen * 8;
963
964         crp->crp_ilen = caead->aadlen + caead->len;
965         crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM
966                        | (caead->flags & COP_F_BATCH);
967         crp->crp_buf = (caddr_t)&cse->uio.uio_iov;
968         crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_cb;
969         crp->crp_sid = cse->sid;
970         crp->crp_opaque = (void *)cse;
971
972         if (caead->iv) {
973                 if (caead->ivlen > sizeof cse->tmp_iv) {
974                         error = EINVAL;
975                         goto bail;
976                 }
977
978                 if ((error = copyin(caead->iv, cse->tmp_iv, caead->ivlen)))
979                         goto bail;
980                 bcopy(cse->tmp_iv, crde->crd_iv, caead->ivlen);
981                 crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
982         } else {
983                 crde->crd_flags |= CRD_F_IV_PRESENT;
984                 crde->crd_skip += cse->txform->blocksize;
985                 crde->crd_len -= cse->txform->blocksize;
986         }
987
988         if ((error = copyin(caead->tag, (caddr_t)cse->uio.uio_iov[0].iov_base +
989             caead->len + caead->aadlen, cse->thash->hashsize)))
990                 goto bail;
991 again:
992         /*
993          * Let the dispatch run unlocked, then, interlock against the
994          * callback before checking if the operation completed and going
995          * to sleep.  This insures drivers don't inherit our lock which
996          * results in a lock order reversal between crypto_dispatch forced
997          * entry and the crypto_done callback into us.
998          */
999         error = crypto_dispatch(crp);
1000         mtx_lock(&cse->lock);
1001         if (error == 0 && (crp->crp_flags & CRYPTO_F_DONE) == 0)
1002                 error = msleep(crp, &cse->lock, PWAIT, "crydev", 0);
1003         mtx_unlock(&cse->lock);
1004
1005         if (error != 0)
1006                 goto bail;
1007
1008         if (crp->crp_etype == EAGAIN) {
1009                 crp->crp_etype = 0;
1010                 crp->crp_flags &= ~CRYPTO_F_DONE;
1011                 goto again;
1012         }
1013
1014         if (crp->crp_etype != 0) {
1015                 error = crp->crp_etype;
1016                 goto bail;
1017         }
1018
1019         if (cse->error) {
1020                 error = cse->error;
1021                 goto bail;
1022         }
1023
1024         if (caead->dst && (error = copyout(
1025             (caddr_t)cse->uio.uio_iov[0].iov_base + caead->aadlen, caead->dst,
1026             caead->len)))
1027                 goto bail;
1028
1029         if ((error = copyout((caddr_t)cse->uio.uio_iov[0].iov_base +
1030             caead->aadlen + caead->len, caead->tag, cse->thash->hashsize)))
1031                 goto bail;
1032
1033 bail:
1034         crypto_freereq(crp);
1035         free(cse->uio.uio_iov[0].iov_base, M_XDATA);
1036
1037         return (error);
1038 }
1039
1040 static int
1041 cryptodev_cb(void *op)
1042 {
1043         struct cryptop *crp = (struct cryptop *) op;
1044         struct csession *cse = (struct csession *)crp->crp_opaque;
1045
1046         mtx_lock(&cse->lock);
1047         cse->error = crp->crp_etype;
1048         wakeup_one(crp);
1049         mtx_unlock(&cse->lock);
1050         return (0);
1051 }
1052
1053 static int
1054 cryptodevkey_cb(void *op)
1055 {
1056         struct cryptkop *krp = (struct cryptkop *) op;
1057
1058         wakeup_one(krp);
1059         return (0);
1060 }
1061
1062 static int
1063 cryptodev_key(struct crypt_kop *kop)
1064 {
1065         struct cryptkop *krp = NULL;
1066         int error = EINVAL;
1067         int in, out, size, i;
1068
1069         if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
1070                 return (EFBIG);
1071         }
1072
1073         in = kop->crk_iparams;
1074         out = kop->crk_oparams;
1075         switch (kop->crk_op) {
1076         case CRK_MOD_EXP:
1077                 if (in == 3 && out == 1)
1078                         break;
1079                 return (EINVAL);
1080         case CRK_MOD_EXP_CRT:
1081                 if (in == 6 && out == 1)
1082                         break;
1083                 return (EINVAL);
1084         case CRK_DSA_SIGN:
1085                 if (in == 5 && out == 2)
1086                         break;
1087                 return (EINVAL);
1088         case CRK_DSA_VERIFY:
1089                 if (in == 7 && out == 0)
1090                         break;
1091                 return (EINVAL);
1092         case CRK_DH_COMPUTE_KEY:
1093                 if (in == 3 && out == 1)
1094                         break;
1095                 return (EINVAL);
1096         default:
1097                 return (EINVAL);
1098         }
1099
1100         krp = (struct cryptkop *)malloc(sizeof *krp, M_XDATA, M_WAITOK|M_ZERO);
1101         if (!krp)
1102                 return (ENOMEM);
1103         krp->krp_op = kop->crk_op;
1104         krp->krp_status = kop->crk_status;
1105         krp->krp_iparams = kop->crk_iparams;
1106         krp->krp_oparams = kop->crk_oparams;
1107         krp->krp_crid = kop->crk_crid;
1108         krp->krp_status = 0;
1109         krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb;
1110
1111         for (i = 0; i < CRK_MAXPARAM; i++) {
1112                 if (kop->crk_param[i].crp_nbits > 65536)
1113                         /* Limit is the same as in OpenBSD */
1114                         goto fail;
1115                 krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
1116         }
1117         for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
1118                 size = (krp->krp_param[i].crp_nbits + 7) / 8;
1119                 if (size == 0)
1120                         continue;
1121                 krp->krp_param[i].crp_p = malloc(size, M_XDATA, M_WAITOK);
1122                 if (i >= krp->krp_iparams)
1123                         continue;
1124                 error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size);
1125                 if (error)
1126                         goto fail;
1127         }
1128
1129         error = crypto_kdispatch(krp);
1130         if (error)
1131                 goto fail;
1132         error = tsleep(krp, PSOCK, "crydev", 0);
1133         if (error) {
1134                 /* XXX can this happen?  if so, how do we recover? */
1135                 goto fail;
1136         }
1137         
1138         kop->crk_crid = krp->krp_crid;          /* device that did the work */
1139         if (krp->krp_status != 0) {
1140                 error = krp->krp_status;
1141                 goto fail;
1142         }
1143
1144         for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
1145                 size = (krp->krp_param[i].crp_nbits + 7) / 8;
1146                 if (size == 0)
1147                         continue;
1148                 error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size);
1149                 if (error)
1150                         goto fail;
1151         }
1152
1153 fail:
1154         if (krp) {
1155                 kop->crk_status = krp->krp_status;
1156                 for (i = 0; i < CRK_MAXPARAM; i++) {
1157                         if (krp->krp_param[i].crp_p)
1158                                 free(krp->krp_param[i].crp_p, M_XDATA);
1159                 }
1160                 free(krp, M_XDATA);
1161         }
1162         return (error);
1163 }
1164
1165 static int
1166 cryptodev_find(struct crypt_find_op *find)
1167 {
1168         device_t dev;
1169         size_t fnlen = sizeof find->name;
1170
1171         if (find->crid != -1) {
1172                 dev = crypto_find_device_byhid(find->crid);
1173                 if (dev == NULL)
1174                         return (ENOENT);
1175                 strncpy(find->name, device_get_nameunit(dev), fnlen);
1176                 find->name[fnlen - 1] = '\x0';
1177         } else {
1178                 find->name[fnlen - 1] = '\x0';
1179                 find->crid = crypto_find_driver(find->name);
1180                 if (find->crid == -1)
1181                         return (ENOENT);
1182         }
1183         return (0);
1184 }
1185
1186 /* ARGSUSED */
1187 static int
1188 cryptof_stat(
1189         struct file *fp,
1190         struct stat *sb,
1191         struct ucred *active_cred,
1192         struct thread *td)
1193 {
1194
1195         return (EOPNOTSUPP);
1196 }
1197
1198 /* ARGSUSED */
1199 static int
1200 cryptof_close(struct file *fp, struct thread *td)
1201 {
1202         struct fcrypt *fcr = fp->f_data;
1203         struct csession *cse;
1204
1205         while ((cse = TAILQ_FIRST(&fcr->csessions))) {
1206                 TAILQ_REMOVE(&fcr->csessions, cse, next);
1207                 (void)csefree(cse);
1208         }
1209         free(fcr, M_XDATA);
1210         fp->f_data = NULL;
1211         return 0;
1212 }
1213
1214 static int
1215 cryptof_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
1216 {
1217
1218         kif->kf_type = KF_TYPE_CRYPTO;
1219         return (0);
1220 }
1221
1222 static struct csession *
1223 csefind(struct fcrypt *fcr, u_int ses)
1224 {
1225         struct csession *cse;
1226
1227         TAILQ_FOREACH(cse, &fcr->csessions, next)
1228                 if (cse->ses == ses)
1229                         return (cse);
1230         return (NULL);
1231 }
1232
1233 static int
1234 csedelete(struct fcrypt *fcr, struct csession *cse_del)
1235 {
1236         struct csession *cse;
1237
1238         TAILQ_FOREACH(cse, &fcr->csessions, next) {
1239                 if (cse == cse_del) {
1240                         TAILQ_REMOVE(&fcr->csessions, cse, next);
1241                         return (1);
1242                 }
1243         }
1244         return (0);
1245 }
1246         
1247 static struct csession *
1248 cseadd(struct fcrypt *fcr, struct csession *cse)
1249 {
1250         TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
1251         cse->ses = fcr->sesn++;
1252         return (cse);
1253 }
1254
1255 struct csession *
1256 csecreate(struct fcrypt *fcr, u_int64_t sid, caddr_t key, u_int64_t keylen,
1257     caddr_t mackey, u_int64_t mackeylen, u_int32_t cipher, u_int32_t mac,
1258     struct enc_xform *txform, struct auth_hash *thash)
1259 {
1260         struct csession *cse;
1261
1262         cse = malloc(sizeof(struct csession), M_XDATA, M_NOWAIT | M_ZERO);
1263         if (cse == NULL)
1264                 return NULL;
1265         mtx_init(&cse->lock, "cryptodev", "crypto session lock", MTX_DEF);
1266         cse->key = key;
1267         cse->keylen = keylen/8;
1268         cse->mackey = mackey;
1269         cse->mackeylen = mackeylen/8;
1270         cse->sid = sid;
1271         cse->cipher = cipher;
1272         cse->mac = mac;
1273         cse->txform = txform;
1274         cse->thash = thash;
1275         cseadd(fcr, cse);
1276         return (cse);
1277 }
1278
1279 static int
1280 csefree(struct csession *cse)
1281 {
1282         int error;
1283
1284         error = crypto_freesession(cse->sid);
1285         mtx_destroy(&cse->lock);
1286         if (cse->key)
1287                 free(cse->key, M_XDATA);
1288         if (cse->mackey)
1289                 free(cse->mackey, M_XDATA);
1290         free(cse, M_XDATA);
1291         return (error);
1292 }
1293
1294 static int
1295 cryptoopen(struct cdev *dev, int oflags, int devtype, struct thread *td)
1296 {
1297         return (0);
1298 }
1299
1300 static int
1301 cryptoread(struct cdev *dev, struct uio *uio, int ioflag)
1302 {
1303         return (EIO);
1304 }
1305
1306 static int
1307 cryptowrite(struct cdev *dev, struct uio *uio, int ioflag)
1308 {
1309         return (EIO);
1310 }
1311
1312 static int
1313 cryptoioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
1314 {
1315         struct file *f;
1316         struct fcrypt *fcr;
1317         int fd, error;
1318
1319         switch (cmd) {
1320         case CRIOGET:
1321                 fcr = malloc(sizeof(struct fcrypt), M_XDATA, M_WAITOK);
1322                 TAILQ_INIT(&fcr->csessions);
1323                 fcr->sesn = 0;
1324
1325                 error = falloc(td, &f, &fd, 0);
1326
1327                 if (error) {
1328                         free(fcr, M_XDATA);
1329                         return (error);
1330                 }
1331                 /* falloc automatically provides an extra reference to 'f'. */
1332                 finit(f, FREAD | FWRITE, DTYPE_CRYPTO, fcr, &cryptofops);
1333                 *(u_int32_t *)data = fd;
1334                 fdrop(f, td);
1335                 break;
1336         case CRIOFINDDEV:
1337                 error = cryptodev_find((struct crypt_find_op *)data);
1338                 break;
1339         case CRIOASYMFEAT:
1340                 error = crypto_getfeat((int *)data);
1341                 break;
1342         default:
1343                 error = EINVAL;
1344                 break;
1345         }
1346         return (error);
1347 }
1348
1349 static struct cdevsw crypto_cdevsw = {
1350         .d_version =    D_VERSION,
1351         .d_flags =      D_NEEDGIANT,
1352         .d_open =       cryptoopen,
1353         .d_read =       cryptoread,
1354         .d_write =      cryptowrite,
1355         .d_ioctl =      cryptoioctl,
1356         .d_name =       "crypto",
1357 };
1358 static struct cdev *crypto_dev;
1359
1360 /*
1361  * Initialization code, both for static and dynamic loading.
1362  */
1363 static int
1364 cryptodev_modevent(module_t mod, int type, void *unused)
1365 {
1366         switch (type) {
1367         case MOD_LOAD:
1368                 if (bootverbose)
1369                         printf("crypto: <crypto device>\n");
1370                 crypto_dev = make_dev(&crypto_cdevsw, 0, 
1371                                       UID_ROOT, GID_WHEEL, 0666,
1372                                       "crypto");
1373                 return 0;
1374         case MOD_UNLOAD:
1375                 /*XXX disallow if active sessions */
1376                 destroy_dev(crypto_dev);
1377                 return 0;
1378         }
1379         return EINVAL;
1380 }
1381
1382 static moduledata_t cryptodev_mod = {
1383         "cryptodev",
1384         cryptodev_modevent,
1385         0
1386 };
1387 MODULE_VERSION(cryptodev, 1);
1388 DECLARE_MODULE(cryptodev, cryptodev_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
1389 MODULE_DEPEND(cryptodev, crypto, 1, 1, 1);
1390 MODULE_DEPEND(cryptodev, zlib, 1, 1, 1);