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