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