]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/opencrypto/cryptodev.c
Fix multiple vulnerabilities in sqlite3.
[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         volatile u_int  refs;
272         u_int32_t       ses;
273         struct mtx      lock;           /* for op submission */
274
275         u_int32_t       cipher;
276         struct enc_xform *txform;
277         u_int32_t       mac;
278         struct auth_hash *thash;
279
280         caddr_t         key;
281         int             keylen;
282
283         caddr_t         mackey;
284         int             mackeylen;
285 };
286
287 struct cryptop_data {
288         struct csession *cse;
289
290         struct iovec    iovec[1];
291         struct uio      uio;
292         bool            done;
293 };
294
295 struct fcrypt {
296         TAILQ_HEAD(csessionlist, csession) csessions;
297         int             sesn;
298         struct mtx      lock;
299 };
300
301 static  int cryptof_ioctl(struct file *, u_long, void *,
302                     struct ucred *, struct thread *);
303 static  int cryptof_stat(struct file *, struct stat *,
304                     struct ucred *, struct thread *);
305 static  int cryptof_close(struct file *, struct thread *);
306 static  int cryptof_fill_kinfo(struct file *, struct kinfo_file *,
307                     struct filedesc *);
308
309 static struct fileops cryptofops = {
310     .fo_read = invfo_rdwr,
311     .fo_write = invfo_rdwr,
312     .fo_truncate = invfo_truncate,
313     .fo_ioctl = cryptof_ioctl,
314     .fo_poll = invfo_poll,
315     .fo_kqfilter = invfo_kqfilter,
316     .fo_stat = cryptof_stat,
317     .fo_close = cryptof_close,
318     .fo_chmod = invfo_chmod,
319     .fo_chown = invfo_chown,
320     .fo_sendfile = invfo_sendfile,
321     .fo_fill_kinfo = cryptof_fill_kinfo,
322 };
323
324 static struct csession *csefind(struct fcrypt *, u_int);
325 static int csedelete(struct fcrypt *, u_int);
326 static struct csession *csecreate(struct fcrypt *, u_int64_t, caddr_t,
327     u_int64_t, caddr_t, u_int64_t, u_int32_t, u_int32_t, struct enc_xform *,
328     struct auth_hash *);
329 static int csefree(struct csession *);
330
331 static  int cryptodev_op(struct csession *, struct crypt_op *,
332                         struct ucred *, struct thread *td);
333 static  int cryptodev_aead(struct csession *, struct crypt_aead *,
334                         struct ucred *, struct thread *);
335 static  int cryptodev_key(struct crypt_kop *);
336 static  int cryptodev_find(struct crypt_find_op *);
337
338 /*
339  * Check a crypto identifier to see if it requested
340  * a software device/driver.  This can be done either
341  * by device name/class or through search constraints.
342  */
343 static int
344 checkforsoftware(int *cridp)
345 {
346         int crid;
347
348         crid = *cridp;
349
350         if (!crypto_devallowsoft) {
351                 if (crid & CRYPTOCAP_F_SOFTWARE) {
352                         if (crid & CRYPTOCAP_F_HARDWARE) {
353                                 *cridp = CRYPTOCAP_F_HARDWARE;
354                                 return 0;
355                         }
356                         return EINVAL;
357                 }
358                 if ((crid & CRYPTOCAP_F_HARDWARE) == 0 &&
359                     (crypto_getcaps(crid) & CRYPTOCAP_F_HARDWARE) == 0)
360                         return EINVAL;
361         }
362         return 0;
363 }
364
365 /* ARGSUSED */
366 static int
367 cryptof_ioctl(
368         struct file *fp,
369         u_long cmd,
370         void *data,
371         struct ucred *active_cred,
372         struct thread *td)
373 {
374 #define SES2(p) ((struct session2_op *)p)
375         struct cryptoini cria, crie;
376         struct fcrypt *fcr = fp->f_data;
377         struct csession *cse;
378         struct session_op *sop;
379         struct crypt_op *cop;
380         struct crypt_aead *caead;
381         struct enc_xform *txform = NULL;
382         struct auth_hash *thash = NULL;
383         struct crypt_kop *kop;
384         u_int64_t sid;
385         u_int32_t ses;
386         int error = 0, crid;
387 #ifdef COMPAT_FREEBSD32
388         struct session2_op sopc;
389         struct crypt_op copc;
390         struct crypt_kop kopc;
391 #endif
392
393         switch (cmd) {
394         case CIOCGSESSION:
395         case CIOCGSESSION2:
396 #ifdef COMPAT_FREEBSD32
397         case CIOCGSESSION32:
398         case CIOCGSESSION232:
399                 if (cmd == CIOCGSESSION32) {
400                         session_op_from_32(data, (struct session_op *)&sopc);
401                         sop = (struct session_op *)&sopc;
402                 } else if (cmd == CIOCGSESSION232) {
403                         session2_op_from_32(data, &sopc);
404                         sop = (struct session_op *)&sopc;
405                 } else
406 #endif
407                         sop = (struct session_op *)data;
408                 switch (sop->cipher) {
409                 case 0:
410                         break;
411                 case CRYPTO_DES_CBC:
412                         txform = &enc_xform_des;
413                         break;
414                 case CRYPTO_3DES_CBC:
415                         txform = &enc_xform_3des;
416                         break;
417                 case CRYPTO_BLF_CBC:
418                         txform = &enc_xform_blf;
419                         break;
420                 case CRYPTO_CAST_CBC:
421                         txform = &enc_xform_cast5;
422                         break;
423                 case CRYPTO_SKIPJACK_CBC:
424                         txform = &enc_xform_skipjack;
425                         break;
426                 case CRYPTO_AES_CBC:
427                         txform = &enc_xform_rijndael128;
428                         break;
429                 case CRYPTO_AES_XTS:
430                         txform = &enc_xform_aes_xts;
431                         break;
432                 case CRYPTO_NULL_CBC:
433                         txform = &enc_xform_null;
434                         break;
435                 case CRYPTO_ARC4:
436                         txform = &enc_xform_arc4;
437                         break;
438                 case CRYPTO_CAMELLIA_CBC:
439                         txform = &enc_xform_camellia;
440                         break;
441                 case CRYPTO_AES_ICM:
442                         txform = &enc_xform_aes_icm;
443                         break;
444                 case CRYPTO_AES_NIST_GCM_16:
445                         txform = &enc_xform_aes_nist_gcm;
446                         break;
447
448                 default:
449                         CRYPTDEB("invalid cipher");
450                         SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
451                         return (EINVAL);
452                 }
453
454                 switch (sop->mac) {
455                 case 0:
456                         break;
457                 case CRYPTO_MD5_HMAC:
458                         thash = &auth_hash_hmac_md5;
459                         break;
460                 case CRYPTO_SHA1_HMAC:
461                         thash = &auth_hash_hmac_sha1;
462                         break;
463                 case CRYPTO_SHA2_256_HMAC:
464                         thash = &auth_hash_hmac_sha2_256;
465                         break;
466                 case CRYPTO_SHA2_384_HMAC:
467                         thash = &auth_hash_hmac_sha2_384;
468                         break;
469                 case CRYPTO_SHA2_512_HMAC:
470                         thash = &auth_hash_hmac_sha2_512;
471                         break;
472                 case CRYPTO_RIPEMD160_HMAC:
473                         thash = &auth_hash_hmac_ripemd_160;
474                         break;
475                 case CRYPTO_AES_128_NIST_GMAC:
476                         thash = &auth_hash_nist_gmac_aes_128;
477                         break;
478                 case CRYPTO_AES_192_NIST_GMAC:
479                         thash = &auth_hash_nist_gmac_aes_192;
480                         break;
481                 case CRYPTO_AES_256_NIST_GMAC:
482                         thash = &auth_hash_nist_gmac_aes_256;
483                         break;
484
485 #ifdef notdef
486                 case CRYPTO_MD5:
487                         thash = &auth_hash_md5;
488                         break;
489                 case CRYPTO_SHA1:
490                         thash = &auth_hash_sha1;
491                         break;
492 #endif
493                 case CRYPTO_NULL_HMAC:
494                         thash = &auth_hash_null;
495                         break;
496                 default:
497                         CRYPTDEB("invalid mac");
498                         SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
499                         return (EINVAL);
500                 }
501
502                 bzero(&crie, sizeof(crie));
503                 bzero(&cria, sizeof(cria));
504
505                 if (txform) {
506                         crie.cri_alg = txform->type;
507                         crie.cri_klen = sop->keylen * 8;
508                         if (sop->keylen > txform->maxkey ||
509                             sop->keylen < txform->minkey) {
510                                 CRYPTDEB("invalid cipher parameters");
511                                 error = EINVAL;
512                                 SDT_PROBE1(opencrypto, dev, ioctl, error,
513                                     __LINE__);
514                                 goto bail;
515                         }
516
517                         crie.cri_key = malloc(crie.cri_klen / 8,
518                             M_XDATA, M_WAITOK);
519                         if ((error = copyin(sop->key, crie.cri_key,
520                             crie.cri_klen / 8))) {
521                                 CRYPTDEB("invalid key");
522                                 SDT_PROBE1(opencrypto, dev, ioctl, error,
523                                     __LINE__);
524                                 goto bail;
525                         }
526                         if (thash)
527                                 crie.cri_next = &cria;
528                 }
529
530                 if (thash) {
531                         cria.cri_alg = thash->type;
532                         cria.cri_klen = sop->mackeylen * 8;
533                         if (sop->mackeylen != thash->keysize) {
534                                 CRYPTDEB("invalid mac key length");
535                                 error = EINVAL;
536                                 SDT_PROBE1(opencrypto, dev, ioctl, error,
537                                     __LINE__);
538                                 goto bail;
539                         }
540
541                         if (cria.cri_klen) {
542                                 cria.cri_key = malloc(cria.cri_klen / 8,
543                                     M_XDATA, M_WAITOK);
544                                 if ((error = copyin(sop->mackey, cria.cri_key,
545                                     cria.cri_klen / 8))) {
546                                         CRYPTDEB("invalid mac key");
547                                         SDT_PROBE1(opencrypto, dev, ioctl,
548                                             error, __LINE__);
549                                         goto bail;
550                                 }
551                         }
552                 }
553
554                 /* NB: CIOCGSESSION2 has the crid */
555                 if (cmd == CIOCGSESSION2
556 #ifdef COMPAT_FREEBSD32
557                     || cmd == CIOCGSESSION232
558 #endif
559                         ) {
560                         crid = SES2(sop)->crid;
561                         error = checkforsoftware(&crid);
562                         if (error) {
563                                 CRYPTDEB("checkforsoftware");
564                                 SDT_PROBE1(opencrypto, dev, ioctl, error,
565                                     __LINE__);
566                                 goto bail;
567                         }
568                 } else
569                         crid = CRYPTOCAP_F_HARDWARE;
570                 error = crypto_newsession(&sid, (txform ? &crie : &cria), crid);
571                 if (error) {
572                         CRYPTDEB("crypto_newsession");
573                         SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
574                         goto bail;
575                 }
576
577                 cse = csecreate(fcr, sid, crie.cri_key, crie.cri_klen,
578                     cria.cri_key, cria.cri_klen, sop->cipher, sop->mac, txform,
579                     thash);
580
581                 if (cse == NULL) {
582                         crypto_freesession(sid);
583                         error = EINVAL;
584                         SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
585                         CRYPTDEB("csecreate");
586                         goto bail;
587                 }
588                 sop->ses = cse->ses;
589                 if (cmd == CIOCGSESSION2
590 #ifdef COMPAT_FREEBSD32
591                     || cmd == CIOCGSESSION232
592 #endif
593                     ) {
594                         /* return hardware/driver id */
595                         SES2(sop)->crid = CRYPTO_SESID2HID(cse->sid);
596                 }
597 bail:
598                 if (error) {
599                         if (crie.cri_key)
600                                 free(crie.cri_key, M_XDATA);
601                         if (cria.cri_key)
602                                 free(cria.cri_key, M_XDATA);
603                 }
604 #ifdef COMPAT_FREEBSD32
605                 else {
606                         if (cmd == CIOCGSESSION32)
607                                 session_op_to_32(sop, data);
608                         else if (cmd == CIOCGSESSION232)
609                                 session2_op_to_32((struct session2_op *)sop,
610                                     data);
611                 }
612 #endif
613                 break;
614         case CIOCFSESSION:
615                 ses = *(u_int32_t *)data;
616                 error = csedelete(fcr, ses);
617                 if (error != 0)
618                         SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
619                 break;
620         case CIOCCRYPT:
621 #ifdef COMPAT_FREEBSD32
622         case CIOCCRYPT32:
623                 if (cmd == CIOCCRYPT32) {
624                         cop = &copc;
625                         crypt_op_from_32(data, cop);
626                 } else
627 #endif
628                         cop = (struct crypt_op *)data;
629                 cse = csefind(fcr, cop->ses);
630                 if (cse == NULL) {
631                         SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
632                         return (EINVAL);
633                 }
634                 error = cryptodev_op(cse, cop, active_cred, td);
635                 (void)csefree(cse);
636 #ifdef COMPAT_FREEBSD32
637                 if (error == 0 && cmd == CIOCCRYPT32)
638                         crypt_op_to_32(cop, data);
639 #endif
640                 break;
641         case CIOCKEY:
642         case CIOCKEY2:
643 #ifdef COMPAT_FREEBSD32
644         case CIOCKEY32:
645         case CIOCKEY232:
646 #endif
647                 if (!crypto_userasymcrypto) {
648                         SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
649                         return (EPERM);         /* XXX compat? */
650                 }
651 #ifdef COMPAT_FREEBSD32
652                 if (cmd == CIOCKEY32 || cmd == CIOCKEY232) {
653                         kop = &kopc;
654                         crypt_kop_from_32(data, kop);
655                 } else
656 #endif
657                         kop = (struct crypt_kop *)data;
658                 if (cmd == CIOCKEY
659 #ifdef COMPAT_FREEBSD32
660                     || cmd == CIOCKEY32
661 #endif
662                     ) {
663                         /* NB: crypto core enforces s/w driver use */
664                         kop->crk_crid =
665                             CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE;
666                 }
667                 mtx_lock(&Giant);
668                 error = cryptodev_key(kop);
669                 mtx_unlock(&Giant);
670 #ifdef COMPAT_FREEBSD32
671                 if (cmd == CIOCKEY32 || cmd == CIOCKEY232)
672                         crypt_kop_to_32(kop, data);
673 #endif
674                 break;
675         case CIOCASYMFEAT:
676                 if (!crypto_userasymcrypto) {
677                         /*
678                          * NB: if user asym crypto operations are
679                          * not permitted return "no algorithms"
680                          * so well-behaved applications will just
681                          * fallback to doing them in software.
682                          */
683                         *(int *)data = 0;
684                 } else {
685                         error = crypto_getfeat((int *)data);
686                         if (error)
687                                 SDT_PROBE1(opencrypto, dev, ioctl, error,
688                                     __LINE__);
689                 }
690                 break;
691         case CIOCFINDDEV:
692                 error = cryptodev_find((struct crypt_find_op *)data);
693                 break;
694         case CIOCCRYPTAEAD:
695                 caead = (struct crypt_aead *)data;
696                 cse = csefind(fcr, caead->ses);
697                 if (cse == NULL) {
698                         SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
699                         return (EINVAL);
700                 }
701                 error = cryptodev_aead(cse, caead, active_cred, td);
702                 (void)csefree(cse);
703                 break;
704         default:
705                 error = EINVAL;
706                 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
707                 break;
708         }
709         return (error);
710 #undef SES2
711 }
712
713 static int cryptodev_cb(struct cryptop *);
714
715 static struct cryptop_data *
716 cod_alloc(struct csession *cse, size_t len, struct thread *td)
717 {
718         struct cryptop_data *cod;
719         struct uio *uio;
720
721         cod = malloc(sizeof(struct cryptop_data), M_XDATA, M_WAITOK | M_ZERO);
722
723         cod->cse = cse;
724         uio = &cod->uio;
725         uio->uio_iov = cod->iovec;
726         uio->uio_iovcnt = 1;
727         uio->uio_resid = len;
728         uio->uio_segflg = UIO_SYSSPACE;
729         uio->uio_rw = UIO_WRITE;
730         uio->uio_td = td;
731         uio->uio_iov[0].iov_len = len;
732         uio->uio_iov[0].iov_base = malloc(len, M_XDATA, M_WAITOK);
733         return (cod);
734 }
735
736 static void
737 cod_free(struct cryptop_data *cod)
738 {
739
740         free(cod->uio.uio_iov[0].iov_base, M_XDATA);
741         free(cod, M_XDATA);
742 }
743
744 static int
745 cryptodev_op(
746         struct csession *cse,
747         struct crypt_op *cop,
748         struct ucred *active_cred,
749         struct thread *td)
750 {
751         struct cryptop_data *cod = NULL;
752         struct cryptop *crp = NULL;
753         struct cryptodesc *crde = NULL, *crda = NULL;
754         int error;
755
756         if (cop->len > 256*1024-4) {
757                 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
758                 return (E2BIG);
759         }
760
761         if (cse->txform) {
762                 if (cop->len == 0 || (cop->len % cse->txform->blocksize) != 0) {
763                         SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
764                         return (EINVAL);
765                 }
766         }
767
768         if (cse->thash)
769                 cod = cod_alloc(cse, cop->len + cse->thash->hashsize, td);
770         else
771                 cod = cod_alloc(cse, cop->len, td);
772
773         crp = crypto_getreq((cse->txform != NULL) + (cse->thash != NULL));
774         if (crp == NULL) {
775                 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
776                 error = ENOMEM;
777                 goto bail;
778         }
779
780         if (cse->thash && cse->txform) {
781                 if (cop->flags & COP_F_CIPHER_FIRST) {
782                         crde = crp->crp_desc;
783                         crda = crde->crd_next;
784                 } else {
785                         crda = crp->crp_desc;
786                         crde = crda->crd_next;
787                 }
788         } else if (cse->thash) {
789                 crda = crp->crp_desc;
790         } else if (cse->txform) {
791                 crde = crp->crp_desc;
792         } else {
793                 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
794                 error = EINVAL;
795                 goto bail;
796         }
797
798         if ((error = copyin(cop->src, cod->uio.uio_iov[0].iov_base,
799             cop->len))) {
800                 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
801                 goto bail;
802         }
803
804         if (crda) {
805                 crda->crd_skip = 0;
806                 crda->crd_len = cop->len;
807                 crda->crd_inject = cop->len;
808
809                 crda->crd_alg = cse->mac;
810                 crda->crd_key = cse->mackey;
811                 crda->crd_klen = cse->mackeylen * 8;
812         }
813
814         if (crde) {
815                 if (cop->op == COP_ENCRYPT)
816                         crde->crd_flags |= CRD_F_ENCRYPT;
817                 else
818                         crde->crd_flags &= ~CRD_F_ENCRYPT;
819                 crde->crd_len = cop->len;
820                 crde->crd_inject = 0;
821
822                 crde->crd_alg = cse->cipher;
823                 crde->crd_key = cse->key;
824                 crde->crd_klen = cse->keylen * 8;
825         }
826
827         crp->crp_ilen = cop->len;
828         crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM
829                        | (cop->flags & COP_F_BATCH);
830         crp->crp_uio = &cod->uio;
831         crp->crp_callback = cryptodev_cb;
832         crp->crp_sid = cse->sid;
833         crp->crp_opaque = cod;
834
835         if (cop->iv) {
836                 if (crde == NULL) {
837                         SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
838                         error = EINVAL;
839                         goto bail;
840                 }
841                 if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
842                         SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
843                         error = EINVAL;
844                         goto bail;
845                 }
846                 if ((error = copyin(cop->iv, crde->crd_iv,
847                     cse->txform->blocksize))) {
848                         SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
849                         goto bail;
850                 }
851                 crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
852                 crde->crd_skip = 0;
853         } else if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
854                 crde->crd_skip = 0;
855         } else if (crde) {
856                 crde->crd_flags |= CRD_F_IV_PRESENT;
857                 crde->crd_skip = cse->txform->blocksize;
858                 crde->crd_len -= cse->txform->blocksize;
859         }
860
861         if (cop->mac && crda == NULL) {
862                 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
863                 error = EINVAL;
864                 goto bail;
865         }
866
867 again:
868         /*
869          * Let the dispatch run unlocked, then, interlock against the
870          * callback before checking if the operation completed and going
871          * to sleep.  This insures drivers don't inherit our lock which
872          * results in a lock order reversal between crypto_dispatch forced
873          * entry and the crypto_done callback into us.
874          */
875         error = crypto_dispatch(crp);
876         if (error != 0) {
877                 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
878                 goto bail;
879         }
880
881         mtx_lock(&cse->lock);
882         while (!cod->done)
883                 mtx_sleep(cod, &cse->lock, PWAIT, "crydev", 0);
884         mtx_unlock(&cse->lock);
885
886         if (crp->crp_etype == EAGAIN) {
887                 crp->crp_etype = 0;
888                 crp->crp_flags &= ~CRYPTO_F_DONE;
889                 cod->done = false;
890                 goto again;
891         }
892
893         if (crp->crp_etype != 0) {
894                 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
895                 error = crp->crp_etype;
896                 goto bail;
897         }
898
899         if (cop->dst &&
900             (error = copyout(cod->uio.uio_iov[0].iov_base, cop->dst,
901             cop->len))) {
902                 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
903                 goto bail;
904         }
905
906         if (cop->mac &&
907             (error = copyout((caddr_t)cod->uio.uio_iov[0].iov_base + cop->len,
908             cop->mac, cse->thash->hashsize))) {
909                 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
910                 goto bail;
911         }
912
913 bail:
914         if (crp)
915                 crypto_freereq(crp);
916         if (cod)
917                 cod_free(cod);
918
919         return (error);
920 }
921
922 static int
923 cryptodev_aead(
924         struct csession *cse,
925         struct crypt_aead *caead,
926         struct ucred *active_cred,
927         struct thread *td)
928 {
929         struct cryptop_data *cod = NULL;
930         struct cryptop *crp = NULL;
931         struct cryptodesc *crde = NULL, *crda = NULL;
932         int error;
933
934         if (caead->len > 256*1024-4 || caead->aadlen > 256*1024-4) {
935                 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
936                 return (E2BIG);
937         }
938
939         if (cse->txform == NULL || cse->thash == NULL || caead->tag == NULL ||
940             (caead->len % cse->txform->blocksize) != 0) {
941                 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
942                 return (EINVAL);
943         }
944
945         cod = cod_alloc(cse, caead->aadlen + caead->len + cse->thash->hashsize,
946             td);
947
948         crp = crypto_getreq(2);
949         if (crp == NULL) {
950                 error = ENOMEM;
951                 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
952                 goto bail;
953         }
954
955         if (caead->flags & COP_F_CIPHER_FIRST) {
956                 crde = crp->crp_desc;
957                 crda = crde->crd_next;
958         } else {
959                 crda = crp->crp_desc;
960                 crde = crda->crd_next;
961         }
962
963         if ((error = copyin(caead->aad, cod->uio.uio_iov[0].iov_base,
964             caead->aadlen))) {
965                 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
966                 goto bail;
967         }
968
969         if ((error = copyin(caead->src, (char *)cod->uio.uio_iov[0].iov_base +
970             caead->aadlen, caead->len))) {
971                 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
972                 goto bail;
973         }
974
975         /*
976          * For GCM, crd_len covers only the AAD.  For other ciphers
977          * chained with an HMAC, crd_len covers both the AAD and the
978          * cipher text.
979          */
980         crda->crd_skip = 0;
981         if (cse->cipher == CRYPTO_AES_NIST_GCM_16)
982                 crda->crd_len = caead->aadlen;
983         else
984                 crda->crd_len = caead->aadlen + caead->len;
985         crda->crd_inject = caead->aadlen + caead->len;
986
987         crda->crd_alg = cse->mac;
988         crda->crd_key = cse->mackey;
989         crda->crd_klen = cse->mackeylen * 8;
990
991         if (caead->op == COP_ENCRYPT)
992                 crde->crd_flags |= CRD_F_ENCRYPT;
993         else
994                 crde->crd_flags &= ~CRD_F_ENCRYPT;
995         crde->crd_skip = caead->aadlen;
996         crde->crd_len = caead->len;
997         crde->crd_inject = caead->aadlen;
998
999         crde->crd_alg = cse->cipher;
1000         crde->crd_key = cse->key;
1001         crde->crd_klen = cse->keylen * 8;
1002
1003         crp->crp_ilen = caead->aadlen + caead->len;
1004         crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM
1005                        | (caead->flags & COP_F_BATCH);
1006         crp->crp_uio = &cod->uio;
1007         crp->crp_callback = cryptodev_cb;
1008         crp->crp_sid = cse->sid;
1009         crp->crp_opaque = cod;
1010
1011         if (caead->iv) {
1012                 if (caead->ivlen > sizeof(crde->crd_iv)) {
1013                         error = EINVAL;
1014                         SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1015                         goto bail;
1016                 }
1017
1018                 if ((error = copyin(caead->iv, crde->crd_iv, caead->ivlen))) {
1019                         SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1020                         goto bail;
1021                 }
1022                 crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
1023         } else {
1024                 crde->crd_flags |= CRD_F_IV_PRESENT;
1025                 crde->crd_skip += cse->txform->blocksize;
1026                 crde->crd_len -= cse->txform->blocksize;
1027         }
1028
1029         if ((error = copyin(caead->tag, (caddr_t)cod->uio.uio_iov[0].iov_base +
1030             caead->len + caead->aadlen, cse->thash->hashsize))) {
1031                 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1032                 goto bail;
1033         }
1034 again:
1035         /*
1036          * Let the dispatch run unlocked, then, interlock against the
1037          * callback before checking if the operation completed and going
1038          * to sleep.  This insures drivers don't inherit our lock which
1039          * results in a lock order reversal between crypto_dispatch forced
1040          * entry and the crypto_done callback into us.
1041          */
1042         error = crypto_dispatch(crp);
1043         if (error != 0) {
1044                 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1045                 goto bail;
1046         }
1047
1048         mtx_lock(&cse->lock);
1049         while (!cod->done)
1050                 mtx_sleep(cod, &cse->lock, PWAIT, "crydev", 0);
1051         mtx_unlock(&cse->lock);
1052
1053         if (crp->crp_etype == EAGAIN) {
1054                 crp->crp_etype = 0;
1055                 crp->crp_flags &= ~CRYPTO_F_DONE;
1056                 cod->done = false;
1057                 goto again;
1058         }
1059
1060         if (crp->crp_etype != 0) {
1061                 error = crp->crp_etype;
1062                 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1063                 goto bail;
1064         }
1065
1066         if (caead->dst && (error = copyout(
1067             (caddr_t)cod->uio.uio_iov[0].iov_base + caead->aadlen, caead->dst,
1068             caead->len))) {
1069                 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1070                 goto bail;
1071         }
1072
1073         if ((error = copyout((caddr_t)cod->uio.uio_iov[0].iov_base +
1074             caead->aadlen + caead->len, caead->tag, cse->thash->hashsize))) {
1075                 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1076                 goto bail;
1077         }
1078
1079 bail:
1080         crypto_freereq(crp);
1081         if (cod)
1082                 cod_free(cod);
1083
1084         return (error);
1085 }
1086
1087 static int
1088 cryptodev_cb(struct cryptop *crp)
1089 {
1090         struct cryptop_data *cod = crp->crp_opaque;
1091
1092         /*
1093          * Lock to ensure the wakeup() is not missed by the loops
1094          * waiting on cod->done in cryptodev_op() and
1095          * cryptodev_aead().
1096          */
1097         mtx_lock(&cod->cse->lock);
1098         cod->done = true;
1099         mtx_unlock(&cod->cse->lock);
1100         wakeup(cod);
1101         return (0);
1102 }
1103
1104 static int
1105 cryptodevkey_cb(void *op)
1106 {
1107         struct cryptkop *krp = (struct cryptkop *) op;
1108
1109         wakeup_one(krp);
1110         return (0);
1111 }
1112
1113 static int
1114 cryptodev_key(struct crypt_kop *kop)
1115 {
1116         struct cryptkop *krp = NULL;
1117         int error = EINVAL;
1118         int in, out, size, i;
1119
1120         if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
1121                 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1122                 return (EFBIG);
1123         }
1124
1125         in = kop->crk_iparams;
1126         out = kop->crk_oparams;
1127         switch (kop->crk_op) {
1128         case CRK_MOD_EXP:
1129                 if (in == 3 && out == 1)
1130                         break;
1131                 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1132                 return (EINVAL);
1133         case CRK_MOD_EXP_CRT:
1134                 if (in == 6 && out == 1)
1135                         break;
1136                 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1137                 return (EINVAL);
1138         case CRK_DSA_SIGN:
1139                 if (in == 5 && out == 2)
1140                         break;
1141                 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1142                 return (EINVAL);
1143         case CRK_DSA_VERIFY:
1144                 if (in == 7 && out == 0)
1145                         break;
1146                 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1147                 return (EINVAL);
1148         case CRK_DH_COMPUTE_KEY:
1149                 if (in == 3 && out == 1)
1150                         break;
1151                 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1152                 return (EINVAL);
1153         default:
1154                 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1155                 return (EINVAL);
1156         }
1157
1158         krp = (struct cryptkop *)malloc(sizeof *krp, M_XDATA, M_WAITOK|M_ZERO);
1159         if (!krp) {
1160                 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1161                 return (ENOMEM);
1162         }
1163         krp->krp_op = kop->crk_op;
1164         krp->krp_status = kop->crk_status;
1165         krp->krp_iparams = kop->crk_iparams;
1166         krp->krp_oparams = kop->crk_oparams;
1167         krp->krp_crid = kop->crk_crid;
1168         krp->krp_status = 0;
1169         krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb;
1170
1171         for (i = 0; i < CRK_MAXPARAM; i++) {
1172                 if (kop->crk_param[i].crp_nbits > 65536) {
1173                         /* Limit is the same as in OpenBSD */
1174                         SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1175                         goto fail;
1176                 }
1177                 krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
1178         }
1179         for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
1180                 size = (krp->krp_param[i].crp_nbits + 7) / 8;
1181                 if (size == 0)
1182                         continue;
1183                 krp->krp_param[i].crp_p = malloc(size, M_XDATA, M_WAITOK);
1184                 if (i >= krp->krp_iparams)
1185                         continue;
1186                 error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size);
1187                 if (error) {
1188                         SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1189                         goto fail;
1190                 }
1191         }
1192
1193         error = crypto_kdispatch(krp);
1194         if (error) {
1195                 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1196                 goto fail;
1197         }
1198         error = tsleep(krp, PSOCK, "crydev", 0);
1199         if (error) {
1200                 /* XXX can this happen?  if so, how do we recover? */
1201                 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1202                 goto fail;
1203         }
1204         
1205         kop->crk_crid = krp->krp_crid;          /* device that did the work */
1206         if (krp->krp_status != 0) {
1207                 error = krp->krp_status;
1208                 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1209                 goto fail;
1210         }
1211
1212         for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
1213                 size = (krp->krp_param[i].crp_nbits + 7) / 8;
1214                 if (size == 0)
1215                         continue;
1216                 error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size);
1217                 if (error) {
1218                         SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1219                         goto fail;
1220                 }
1221         }
1222
1223 fail:
1224         if (krp) {
1225                 kop->crk_status = krp->krp_status;
1226                 for (i = 0; i < CRK_MAXPARAM; i++) {
1227                         if (krp->krp_param[i].crp_p)
1228                                 free(krp->krp_param[i].crp_p, M_XDATA);
1229                 }
1230                 free(krp, M_XDATA);
1231         }
1232         return (error);
1233 }
1234
1235 static int
1236 cryptodev_find(struct crypt_find_op *find)
1237 {
1238         device_t dev;
1239         size_t fnlen = sizeof find->name;
1240
1241         if (find->crid != -1) {
1242                 dev = crypto_find_device_byhid(find->crid);
1243                 if (dev == NULL)
1244                         return (ENOENT);
1245                 strncpy(find->name, device_get_nameunit(dev), fnlen);
1246                 find->name[fnlen - 1] = '\x0';
1247         } else {
1248                 find->name[fnlen - 1] = '\x0';
1249                 find->crid = crypto_find_driver(find->name);
1250                 if (find->crid == -1)
1251                         return (ENOENT);
1252         }
1253         return (0);
1254 }
1255
1256 /* ARGSUSED */
1257 static int
1258 cryptof_stat(
1259         struct file *fp,
1260         struct stat *sb,
1261         struct ucred *active_cred,
1262         struct thread *td)
1263 {
1264
1265         return (EOPNOTSUPP);
1266 }
1267
1268 /* ARGSUSED */
1269 static int
1270 cryptof_close(struct file *fp, struct thread *td)
1271 {
1272         struct fcrypt *fcr = fp->f_data;
1273         struct csession *cse;
1274
1275         while ((cse = TAILQ_FIRST(&fcr->csessions))) {
1276                 TAILQ_REMOVE(&fcr->csessions, cse, next);
1277                 KASSERT(cse->refs == 1,
1278                     ("%s: crypto session %p with %d refs", __func__, cse,
1279                     cse->refs));
1280                 (void)csefree(cse);
1281         }
1282         free(fcr, M_XDATA);
1283         fp->f_data = NULL;
1284         return 0;
1285 }
1286
1287 static int
1288 cryptof_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
1289 {
1290
1291         kif->kf_type = KF_TYPE_CRYPTO;
1292         return (0);
1293 }
1294
1295 static struct csession *
1296 csefind(struct fcrypt *fcr, u_int ses)
1297 {
1298         struct csession *cse;
1299
1300         mtx_lock(&fcr->lock);
1301         TAILQ_FOREACH(cse, &fcr->csessions, next) {
1302                 if (cse->ses == ses) {
1303                         refcount_acquire(&cse->refs);
1304                         mtx_unlock(&fcr->lock);
1305                         return (cse);
1306                 }
1307         }
1308         mtx_unlock(&fcr->lock);
1309         return (NULL);
1310 }
1311
1312 static int
1313 csedelete(struct fcrypt *fcr, u_int ses)
1314 {
1315         struct csession *cse;
1316
1317         mtx_lock(&fcr->lock);
1318         TAILQ_FOREACH(cse, &fcr->csessions, next) {
1319                 if (cse->ses == ses) {
1320                         TAILQ_REMOVE(&fcr->csessions, cse, next);
1321                         mtx_unlock(&fcr->lock);
1322                         return (csefree(cse));
1323                 }
1324         }
1325         mtx_unlock(&fcr->lock);
1326         return (EINVAL);
1327 }
1328         
1329 struct csession *
1330 csecreate(struct fcrypt *fcr, u_int64_t sid, caddr_t key, u_int64_t keylen,
1331     caddr_t mackey, u_int64_t mackeylen, u_int32_t cipher, u_int32_t mac,
1332     struct enc_xform *txform, struct auth_hash *thash)
1333 {
1334         struct csession *cse;
1335
1336         cse = malloc(sizeof(struct csession), M_XDATA, M_NOWAIT | M_ZERO);
1337         if (cse == NULL)
1338                 return NULL;
1339         mtx_init(&cse->lock, "cryptodev", "crypto session lock", MTX_DEF);
1340         refcount_init(&cse->refs, 1);
1341         cse->key = key;
1342         cse->keylen = keylen/8;
1343         cse->mackey = mackey;
1344         cse->mackeylen = mackeylen/8;
1345         cse->sid = sid;
1346         cse->cipher = cipher;
1347         cse->mac = mac;
1348         cse->txform = txform;
1349         cse->thash = thash;
1350         mtx_lock(&fcr->lock);
1351         TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
1352         cse->ses = fcr->sesn++;
1353         mtx_unlock(&fcr->lock);
1354         return (cse);
1355 }
1356
1357 static int
1358 csefree(struct csession *cse)
1359 {
1360         int error;
1361
1362         if (!refcount_release(&cse->refs))
1363                 return (0);
1364         error = crypto_freesession(cse->sid);
1365         mtx_destroy(&cse->lock);
1366         if (cse->key)
1367                 free(cse->key, M_XDATA);
1368         if (cse->mackey)
1369                 free(cse->mackey, M_XDATA);
1370         free(cse, M_XDATA);
1371         return (error);
1372 }
1373
1374 static int
1375 cryptoopen(struct cdev *dev, int oflags, int devtype, struct thread *td)
1376 {
1377         return (0);
1378 }
1379
1380 static int
1381 cryptoread(struct cdev *dev, struct uio *uio, int ioflag)
1382 {
1383         return (EIO);
1384 }
1385
1386 static int
1387 cryptowrite(struct cdev *dev, struct uio *uio, int ioflag)
1388 {
1389         return (EIO);
1390 }
1391
1392 static int
1393 cryptoioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
1394 {
1395         struct file *f;
1396         struct fcrypt *fcr;
1397         int fd, error;
1398
1399         switch (cmd) {
1400         case CRIOGET:
1401                 fcr = malloc(sizeof(struct fcrypt), M_XDATA, M_WAITOK | M_ZERO);
1402                 TAILQ_INIT(&fcr->csessions);
1403                 mtx_init(&fcr->lock, "fcrypt", NULL, MTX_DEF);
1404
1405                 error = falloc(td, &f, &fd, 0);
1406
1407                 if (error) {
1408                         mtx_destroy(&fcr->lock);
1409                         free(fcr, M_XDATA);
1410                         return (error);
1411                 }
1412                 /* falloc automatically provides an extra reference to 'f'. */
1413                 finit(f, FREAD | FWRITE, DTYPE_CRYPTO, fcr, &cryptofops);
1414                 *(u_int32_t *)data = fd;
1415                 fdrop(f, td);
1416                 break;
1417         case CRIOFINDDEV:
1418                 error = cryptodev_find((struct crypt_find_op *)data);
1419                 break;
1420         case CRIOASYMFEAT:
1421                 error = crypto_getfeat((int *)data);
1422                 break;
1423         default:
1424                 error = EINVAL;
1425                 break;
1426         }
1427         return (error);
1428 }
1429
1430 static struct cdevsw crypto_cdevsw = {
1431         .d_version =    D_VERSION,
1432         .d_flags =      D_NEEDGIANT,
1433         .d_open =       cryptoopen,
1434         .d_read =       cryptoread,
1435         .d_write =      cryptowrite,
1436         .d_ioctl =      cryptoioctl,
1437         .d_name =       "crypto",
1438 };
1439 static struct cdev *crypto_dev;
1440
1441 /*
1442  * Initialization code, both for static and dynamic loading.
1443  */
1444 static int
1445 cryptodev_modevent(module_t mod, int type, void *unused)
1446 {
1447         switch (type) {
1448         case MOD_LOAD:
1449                 if (bootverbose)
1450                         printf("crypto: <crypto device>\n");
1451                 crypto_dev = make_dev(&crypto_cdevsw, 0, 
1452                                       UID_ROOT, GID_WHEEL, 0666,
1453                                       "crypto");
1454                 return 0;
1455         case MOD_UNLOAD:
1456                 /*XXX disallow if active sessions */
1457                 destroy_dev(crypto_dev);
1458                 return 0;
1459         }
1460         return EINVAL;
1461 }
1462
1463 static moduledata_t cryptodev_mod = {
1464         "cryptodev",
1465         cryptodev_modevent,
1466         0
1467 };
1468 MODULE_VERSION(cryptodev, 1);
1469 DECLARE_MODULE(cryptodev, cryptodev_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
1470 MODULE_DEPEND(cryptodev, crypto, 1, 1, 1);
1471 MODULE_DEPEND(cryptodev, zlib, 1, 1, 1);