opencrypto - add twofish support to cryptosoft/dev
[dragonfly.git] / sys / opencrypto / cryptodev.c
1 /*      $FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.41 2009/09/04 09:48:18 pjd Exp $   */
2 /*      $OpenBSD: cryptodev.c,v 1.52 2002/06/19 07:22:46 deraadt Exp $  */
3
4 /*-
5  * Copyright (c) 2001 Theo de Raadt
6  * Copyright (c) 2002-2006 Sam Leffler, Errno Consulting
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *   notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *   notice, this list of conditions and the following disclaimer in the
16  *   documentation and/or other materials provided with the distribution.
17  * 3. The name of the author may not be used to endorse or promote products
18  *   derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  * Effort sponsored in part by the Defense Advanced Research Projects
32  * Agency (DARPA) and Air Force Research Laboratory, Air Force
33  * Materiel Command, USAF, under agreement number F30602-01-2-0537.
34  */
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/malloc.h>
39 #include <sys/device.h>
40 #include <sys/mbuf.h>
41 #include <sys/lock.h>
42 #include <sys/sysctl.h>
43 #include <sys/file.h>
44 #include <sys/filedesc.h>
45 #include <sys/errno.h>
46 #include <sys/uio.h>
47 #include <sys/random.h>
48 #include <sys/conf.h>
49 #include <sys/module.h>
50 #include <sys/kernel.h>
51 #include <sys/fcntl.h>
52 #include <sys/proc.h>
53
54 #include <sys/file2.h>
55 #include <sys/thread2.h>
56 #include <sys/mplock2.h>
57
58 #include <opencrypto/cryptodev.h>
59 #include <opencrypto/xform.h>
60
61 struct csession {
62         TAILQ_ENTRY(csession) next;
63         u_int64_t       sid;
64         u_int32_t       ses;
65         struct lock     lock;
66
67         u_int32_t       cipher;
68         struct enc_xform *txform;
69         u_int32_t       mac;
70         struct auth_hash *thash;
71
72         caddr_t         key;
73         int             keylen;
74         u_char          tmp_iv[EALG_MAX_BLOCK_LEN];
75
76         caddr_t         mackey;
77         int             mackeylen;
78
79         struct iovec    iovec;
80         struct uio      uio;
81         int             error;
82 };
83
84 struct fcrypt {
85         TAILQ_HEAD(csessionlist, csession) csessions;
86         int             sesn;
87 };
88
89 static  int cryptof_rw(struct file *fp, struct uio *uio,
90                     struct ucred *cred, int flags);
91 static  int cryptof_ioctl(struct file *, u_long, caddr_t,
92                     struct ucred *, struct sysmsg *);
93 static  int cryptof_kqfilter(struct file *, struct knote *);
94 static  int cryptof_stat(struct file *, struct stat *, struct ucred *);
95 static  int cryptof_close(struct file *);
96
97 static struct fileops cryptofops = {
98     .fo_read = cryptof_rw,
99     .fo_write = cryptof_rw,
100     .fo_ioctl = cryptof_ioctl,
101     .fo_kqfilter = cryptof_kqfilter,
102     .fo_stat = cryptof_stat,
103     .fo_close = cryptof_close,
104     .fo_shutdown = nofo_shutdown
105 };
106
107 static struct csession *csefind(struct fcrypt *, u_int);
108 static int csedelete(struct fcrypt *, struct csession *);
109 static struct csession *cseadd(struct fcrypt *, struct csession *);
110 static struct csession *csecreate(struct fcrypt *, u_int64_t, caddr_t,
111     u_int64_t, caddr_t, u_int64_t, u_int32_t, u_int32_t, struct enc_xform *,
112     struct auth_hash *);
113 static int csefree(struct csession *);
114
115 static  int cryptodev_op(struct csession *, struct crypt_op *, struct ucred *);
116 static  int cryptodev_key(struct crypt_kop *);
117 static  int cryptodev_find(struct crypt_find_op *);
118
119 static int
120 cryptof_rw(
121         struct file *fp,
122         struct uio *uio,
123         struct ucred *active_cred,
124         int flags)
125 {
126         return (EIO);
127 }
128
129 /*
130  * Check a crypto identifier to see if it requested
131  * a software device/driver.  This can be done either
132  * by device name/class or through search constraints.
133  */
134 static int
135 checkforsoftware(int crid)
136 {
137         if (crid & CRYPTOCAP_F_SOFTWARE)
138                 return EINVAL;          /* XXX */
139         if ((crid & CRYPTOCAP_F_HARDWARE) == 0 &&
140             (crypto_getcaps(crid) & CRYPTOCAP_F_HARDWARE) == 0)
141                 return EINVAL;          /* XXX */
142         return 0;
143 }
144
145 /* ARGSUSED */
146 static int
147 cryptof_ioctl(struct file *fp, u_long cmd, caddr_t data,
148               struct ucred *cred, struct sysmsg *msg)
149 {
150 #define SES2(p) ((struct session2_op *)p)
151         struct cryptoini cria, crie;
152         struct fcrypt *fcr = fp->f_data;
153         struct csession *cse;
154         struct session_op *sop;
155         struct crypt_op *cop;
156         struct enc_xform *txform = NULL;
157         struct auth_hash *thash = NULL;
158         struct crypt_kop *kop;
159         u_int64_t sid;
160         u_int32_t ses;
161         int error = 0, crid;
162
163         switch (cmd) {
164         case CIOCGSESSION:
165         case CIOCGSESSION2:
166                 sop = (struct session_op *)data;
167                 switch (sop->cipher) {
168                 case 0:
169                         break;
170                 case CRYPTO_DES_CBC:
171                         txform = &enc_xform_des;
172                         break;
173                 case CRYPTO_3DES_CBC:
174                         txform = &enc_xform_3des;
175                         break;
176                 case CRYPTO_BLF_CBC:
177                         txform = &enc_xform_blf;
178                         break;
179                 case CRYPTO_CAST_CBC:
180                         txform = &enc_xform_cast5;
181                         break;
182                 case CRYPTO_SKIPJACK_CBC:
183                         txform = &enc_xform_skipjack;
184                         break;
185                 case CRYPTO_AES_CBC:
186                         txform = &enc_xform_rijndael128;
187                         break;
188                 case CRYPTO_AES_XTS:
189                         txform = &enc_xform_aes_xts;
190                         break;
191                 case CRYPTO_AES_CTR:
192                         txform = &enc_xform_aes_ctr;
193                         break;          
194                 case CRYPTO_NULL_CBC:
195                         txform = &enc_xform_null;
196                         break;
197                 case CRYPTO_ARC4:
198                         txform = &enc_xform_arc4;
199                         break;
200                 case CRYPTO_CAMELLIA_CBC:
201                         txform = &enc_xform_camellia;
202                         break;
203                 case CRYPTO_TWOFISH_CBC:
204                         txform = &enc_xform_twofish;
205                         break;
206                 default:
207                         return (EINVAL);
208                 }
209
210                 switch (sop->mac) {
211                 case 0:
212                         break;
213                 case CRYPTO_MD5_HMAC:
214                         thash = &auth_hash_hmac_md5;
215                         break;
216                 case CRYPTO_SHA1_HMAC:
217                         thash = &auth_hash_hmac_sha1;
218                         break;
219                 case CRYPTO_SHA2_256_HMAC:
220                         thash = &auth_hash_hmac_sha2_256;
221                         break;
222                 case CRYPTO_SHA2_384_HMAC:
223                         thash = &auth_hash_hmac_sha2_384;
224                         break;
225                 case CRYPTO_SHA2_512_HMAC:
226                         thash = &auth_hash_hmac_sha2_512;
227                         break;
228                 case CRYPTO_RIPEMD160_HMAC:
229                         thash = &auth_hash_hmac_ripemd_160;
230                         break;
231 #ifdef notdef
232                 case CRYPTO_MD5:
233                         thash = &auth_hash_md5;
234                         break;
235                 case CRYPTO_SHA1:
236                         thash = &auth_hash_sha1;
237                         break;
238 #endif
239                 case CRYPTO_NULL_HMAC:
240                         thash = &auth_hash_null;
241                         break;
242                 default:
243                         return (EINVAL);
244                 }
245
246                 bzero(&crie, sizeof(crie));
247                 bzero(&cria, sizeof(cria));
248
249                 if (txform) {
250                         crie.cri_alg = txform->type;
251                         crie.cri_klen = sop->keylen * 8;
252                         if (sop->keylen > txform->maxkey ||
253                             sop->keylen < txform->minkey) {
254                                 error = EINVAL;
255                                 goto bail;
256                         }
257
258                         crie.cri_key = kmalloc(crie.cri_klen / 8,
259                             M_XDATA, M_WAITOK);
260                         if ((error = copyin(sop->key, crie.cri_key,
261                             crie.cri_klen / 8)))
262                                 goto bail;
263                         if (thash)
264                                 crie.cri_next = &cria;
265                 }
266
267                 if (thash) {
268                         cria.cri_alg = thash->type;
269                         cria.cri_klen = sop->mackeylen * 8;
270                         if (sop->mackeylen != thash->keysize) {
271                                 error = EINVAL;
272                                 goto bail;
273                         }
274
275                         if (cria.cri_klen) {
276                                 cria.cri_key = kmalloc(cria.cri_klen / 8,
277                                     M_XDATA, M_WAITOK);
278                                 if ((error = copyin(sop->mackey, cria.cri_key,
279                                     cria.cri_klen / 8)))
280                                         goto bail;
281                         }
282                 }
283
284                 /* NB: CIOGSESSION2 has the crid */
285                 if (cmd == CIOCGSESSION2) {
286                         crid = SES2(sop)->crid;
287                         error = checkforsoftware(crid);
288                         if (error)
289                                 goto bail;
290                 } else {
291                         crid = CRYPTOCAP_F_HARDWARE;
292                         if (crypto_devallowsoft)
293                                 crid |= CRYPTOCAP_F_SOFTWARE;
294                 }
295                 error = crypto_newsession(&sid, (txform ? &crie : &cria), crid);
296                 if (error)
297                         goto bail;
298
299                 cse = csecreate(fcr, sid, crie.cri_key, crie.cri_klen,
300                     cria.cri_key, cria.cri_klen, sop->cipher, sop->mac, txform,
301                     thash);
302
303                 if (cse == NULL) {
304                         crypto_freesession(sid);
305                         error = EINVAL;
306                         goto bail;
307                 }
308                 sop->ses = cse->ses;
309                 if (cmd == CIOCGSESSION2) {
310                         /* return hardware/driver id */
311                         SES2(sop)->crid = CRYPTO_SESID2HID(cse->sid);
312                 }
313 bail:
314                 if (error) {
315                         if (crie.cri_key) {
316                                 bzero(crie.cri_key, crie.cri_klen / 8);
317                                 kfree(crie.cri_key, M_XDATA);
318                         }
319                         if (cria.cri_key) {
320                                 bzero(crie.cri_key, crie.cri_klen / 8);
321                                 kfree(cria.cri_key, M_XDATA);
322                         }
323                 }
324                 break;
325         case CIOCFSESSION:
326                 ses = *(u_int32_t *)data;
327                 cse = csefind(fcr, ses);
328                 if (cse == NULL)
329                         return (EINVAL);
330                 csedelete(fcr, cse);
331                 error = csefree(cse);
332                 break;
333         case CIOCCRYPT:
334                 cop = (struct crypt_op *)data;
335                 cse = csefind(fcr, cop->ses);
336                 if (cse == NULL)
337                         return (EINVAL);
338                 error = cryptodev_op(cse, cop, cred);
339                 break;
340         case CIOCKEY:
341         case CIOCKEY2:
342                 if (!crypto_userasymcrypto)
343                         return (EPERM);         /* XXX compat? */
344                 get_mplock();
345                 kop = (struct crypt_kop *)data;
346                 if (cmd == CIOCKEY) {
347                         /* NB: crypto core enforces s/w driver use */
348                         kop->crk_crid =
349                             CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE;
350                 }
351                 error = cryptodev_key(kop);
352                 rel_mplock();
353                 break;
354         case CIOCASYMFEAT:
355                 if (!crypto_userasymcrypto) {
356                         /*
357                          * NB: if user asym crypto operations are
358                          * not permitted return "no algorithms"
359                          * so well-behaved applications will just
360                          * fallback to doing them in software.
361                          */
362                         *(int *)data = 0;
363                 } else
364                         error = crypto_getfeat((int *)data);
365                 break;
366         case CIOCFINDDEV:
367                 error = cryptodev_find((struct crypt_find_op *)data);
368                 break;
369         default:
370                 error = EINVAL;
371                 break;
372         }
373         return (error);
374 #undef SES2
375 }
376
377 static int cryptodev_cb(void *);
378
379
380 static int
381 cryptodev_op(struct csession *cse, struct crypt_op *cop,
382     struct ucred *active_cred)
383 {
384         struct cryptop *crp = NULL;
385         struct cryptodesc *crde = NULL, *crda = NULL;
386         int error;
387
388         if (cop->len > 256*1024-4)
389                 return (E2BIG);
390
391         if (cse->txform) {
392                 if (cop->len == 0 || (cop->len % cse->txform->blocksize) != 0)
393                         return (EINVAL);
394         }
395
396         bzero(&cse->uio, sizeof(cse->uio));
397         cse->uio.uio_iov = &cse->iovec;
398         cse->uio.uio_iovcnt = 1;
399         cse->uio.uio_offset = 0;
400         cse->uio.uio_resid = cop->len;
401         cse->uio.uio_segflg = UIO_SYSSPACE;
402         cse->uio.uio_rw = UIO_WRITE;
403         /* XXX: not sure, was td, now curthread? */
404         cse->uio.uio_td = curthread;
405         cse->uio.uio_iov[0].iov_len = cop->len;
406         if (cse->thash) {
407                 cse->uio.uio_iov[0].iov_len += cse->thash->hashsize;
408                 cse->uio.uio_resid += cse->thash->hashsize;
409         }
410         cse->uio.uio_iov[0].iov_base = kmalloc(cse->uio.uio_iov[0].iov_len,
411             M_XDATA, M_WAITOK);
412
413         crp = crypto_getreq((cse->txform != NULL) + (cse->thash != NULL));
414         if (crp == NULL) {
415                 error = ENOMEM;
416                 goto bail;
417         }
418
419         if (cse->thash) {
420                 crda = crp->crp_desc;
421                 if (cse->txform)
422                         crde = crda->crd_next;
423         } else {
424                 if (cse->txform)
425                         crde = crp->crp_desc;
426                 else {
427                         error = EINVAL;
428                         goto bail;
429                 }
430         }
431
432         if ((error = copyin(cop->src, cse->uio.uio_iov[0].iov_base, cop->len)))
433                 goto bail;
434
435         if (crda) {
436                 crda->crd_skip = 0;
437                 crda->crd_len = cop->len;
438                 crda->crd_inject = cop->len;
439
440                 crda->crd_alg = cse->mac;
441                 crda->crd_key = cse->mackey;
442                 crda->crd_klen = cse->mackeylen * 8;
443         }
444
445         if (crde) {
446                 if (cop->op == COP_ENCRYPT)
447                         crde->crd_flags |= CRD_F_ENCRYPT;
448                 else
449                         crde->crd_flags &= ~CRD_F_ENCRYPT;
450                 crde->crd_len = cop->len;
451                 crde->crd_inject = 0;
452
453                 crde->crd_alg = cse->cipher;
454                 crde->crd_key = cse->key;
455                 crde->crd_klen = cse->keylen * 8;
456         }
457
458         crp->crp_ilen = cop->len;
459         crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM
460                        | (cop->flags & COP_F_BATCH);
461         crp->crp_buf = (caddr_t)&cse->uio;
462         crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_cb;
463         crp->crp_sid = cse->sid;
464         crp->crp_opaque = (void *)cse;
465
466         if (cop->iv) {
467                 if (crde == NULL) {
468                         error = EINVAL;
469                         goto bail;
470                 }
471                 if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
472                         error = EINVAL;
473                         goto bail;
474                 }
475                 if ((error = copyin(cop->iv, cse->tmp_iv, cse->txform->blocksize)))
476                         goto bail;
477                 bcopy(cse->tmp_iv, crde->crd_iv, cse->txform->blocksize);
478                 crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
479                 crde->crd_skip = 0;
480         } else if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
481                 crde->crd_skip = 0;
482         } else if (crde) {
483                 crde->crd_flags |= CRD_F_IV_PRESENT;
484                 crde->crd_skip = cse->txform->blocksize;
485                 crde->crd_len -= cse->txform->blocksize;
486         }
487
488         if (cop->mac && crda == NULL) {
489                 error = EINVAL;
490                 goto bail;
491         }
492
493 again:
494         /*
495          * Let the dispatch run unlocked, then, interlock against the
496          * callback before checking if the operation completed and going
497          * to sleep.  This insures drivers don't inherit our lock which
498          * results in a lock order reversal between crypto_dispatch forced
499          * entry and the crypto_done callback into us.
500          */
501         error = crypto_dispatch(crp);
502         lockmgr(&cse->lock, LK_EXCLUSIVE);
503         if (error == 0 && (crp->crp_flags & CRYPTO_F_DONE) == 0)
504                 error = lksleep(crp, &cse->lock, 0, "crydev", 0);
505         lockmgr(&cse->lock, LK_RELEASE);
506
507         if (error != 0)
508                 goto bail;
509
510         if (crp->crp_etype == EAGAIN) {
511                 crp->crp_etype = 0;
512                 crp->crp_flags &= ~CRYPTO_F_DONE;
513                 goto again;
514         }
515
516         if (crp->crp_etype != 0) {
517                 error = crp->crp_etype;
518                 goto bail;
519         }
520
521         if (cse->error) {
522                 error = cse->error;
523                 goto bail;
524         }
525
526         if (cop->dst &&
527             (error = copyout(cse->uio.uio_iov[0].iov_base, cop->dst, cop->len)))
528                 goto bail;
529
530         if (cop->mac &&
531             (error = copyout((caddr_t)cse->uio.uio_iov[0].iov_base + cop->len,
532             cop->mac, cse->thash->hashsize)))
533                 goto bail;
534
535 bail:
536         if (crp)
537                 crypto_freereq(crp);
538         if (cse->uio.uio_iov[0].iov_base)
539                 kfree(cse->uio.uio_iov[0].iov_base, M_XDATA);
540
541         return (error);
542 }
543
544 static int
545 cryptodev_cb(void *op)
546 {
547         struct cryptop *crp = (struct cryptop *) op;
548         struct csession *cse = (struct csession *)crp->crp_opaque;
549
550         lockmgr(&cse->lock, LK_EXCLUSIVE);
551         cse->error = crp->crp_etype;
552         wakeup_one(crp);
553         lockmgr(&cse->lock, LK_RELEASE);
554         return (0);
555 }
556
557 static int
558 cryptodevkey_cb(void *op)
559 {
560         struct cryptkop *krp = (struct cryptkop *) op;
561
562         wakeup_one(krp);
563         return (0);
564 }
565
566 static int
567 cryptodev_key(struct crypt_kop *kop)
568 {
569         struct cryptkop *krp = NULL;
570         int error = EINVAL;
571         int in, out, size, i;
572
573         if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
574                 return (EFBIG);
575         }
576
577         in = kop->crk_iparams;
578         out = kop->crk_oparams;
579         switch (kop->crk_op) {
580         case CRK_MOD_EXP:
581                 if (in == 3 && out == 1)
582                         break;
583                 return (EINVAL);
584         case CRK_MOD_EXP_CRT:
585                 if (in == 6 && out == 1)
586                         break;
587                 return (EINVAL);
588         case CRK_DSA_SIGN:
589                 if (in == 5 && out == 2)
590                         break;
591                 return (EINVAL);
592         case CRK_DSA_VERIFY:
593                 if (in == 7 && out == 0)
594                         break;
595                 return (EINVAL);
596         case CRK_DH_COMPUTE_KEY:
597                 if (in == 3 && out == 1)
598                         break;
599                 return (EINVAL);
600         default:
601                 return (EINVAL);
602         }
603
604         krp = (struct cryptkop *)kmalloc(sizeof *krp, M_XDATA, M_WAITOK | M_ZERO);
605         krp->krp_op = kop->crk_op;
606         krp->krp_status = kop->crk_status;
607         krp->krp_iparams = kop->crk_iparams;
608         krp->krp_oparams = kop->crk_oparams;
609         krp->krp_crid = kop->crk_crid;
610         krp->krp_status = 0;
611         krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb;
612
613         for (i = 0; i < CRK_MAXPARAM; i++) {
614                 if (kop->crk_param[i].crp_nbits > 65536)
615                         /* Limit is the same as in OpenBSD */
616                         goto fail;
617                 krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
618         }
619         for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
620                 size = (krp->krp_param[i].crp_nbits + 7) / 8;
621                 if (size == 0)
622                         continue;
623                 krp->krp_param[i].crp_p = kmalloc(size, M_XDATA, M_WAITOK);
624                 if (i >= krp->krp_iparams)
625                         continue;
626                 error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size);
627                 if (error)
628                         goto fail;
629         }
630
631         error = crypto_kdispatch(krp);
632         if (error)
633                 goto fail;
634         error = tsleep(krp, 0, "crydev", 0);
635         if (error) {
636                 /* XXX can this happen?  if so, how do we recover? */
637                 goto fail;
638         }
639         
640         kop->crk_crid = krp->krp_crid;          /* device that did the work */
641         if (krp->krp_status != 0) {
642                 error = krp->krp_status;
643                 goto fail;
644         }
645
646         for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
647                 size = (krp->krp_param[i].crp_nbits + 7) / 8;
648                 if (size == 0)
649                         continue;
650                 error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size);
651                 if (error)
652                         goto fail;
653         }
654
655 fail:
656         if (krp) {
657                 kop->crk_status = krp->krp_status;
658                 for (i = 0; i < CRK_MAXPARAM; i++) {
659                         if (krp->krp_param[i].crp_p) {
660                                 bzero(krp->krp_param[i].crp_p,
661                                     (krp->krp_param[i].crp_nbits + 7) / 8);
662                                 kfree(krp->krp_param[i].crp_p, M_XDATA);
663                         }
664                 }
665                 kfree(krp, M_XDATA);
666         }
667         return (error);
668 }
669
670 static int
671 cryptodev_find(struct crypt_find_op *find)
672 {
673         device_t dev;
674
675         if (find->crid != -1) {
676                 dev = crypto_find_device_byhid(find->crid);
677                 if (dev == NULL)
678                         return (ENOENT);
679                 strlcpy(find->name, device_get_nameunit(dev),
680                     sizeof(find->name));
681         } else {
682                 find->crid = crypto_find_driver(find->name);
683                 if (find->crid == -1)
684                         return (ENOENT);
685         }
686         return (0);
687 }
688
689 /*
690  * MPSAFE
691  */
692 static int
693 cryptof_kqfilter(struct file *fp, struct knote *kn)
694 {
695
696         return (0);
697 }
698
699 /*
700  * MPSAFE
701  */
702 static int
703 cryptof_stat(struct file *fp, struct stat *sb, struct ucred *cred)
704 {
705         return (EOPNOTSUPP);
706 }
707
708 /*
709  * MPALMOSTSAFE - acquires mplock
710  */
711 static int
712 cryptof_close(struct file *fp)
713 {
714         struct fcrypt *fcr = fp->f_data;
715         struct csession *cse;
716
717         get_mplock();
718         fcr = (struct fcrypt *)fp->f_data;
719         while ((cse = TAILQ_FIRST(&fcr->csessions))) {
720                 TAILQ_REMOVE(&fcr->csessions, cse, next);
721                 (void)csefree(cse);
722         }
723         fp->f_data = NULL;
724         rel_mplock();
725
726         kfree(fcr, M_XDATA);
727         return (0);
728 }
729
730 static struct csession *
731 csefind(struct fcrypt *fcr, u_int ses)
732 {
733         struct csession *cse;
734
735         TAILQ_FOREACH(cse, &fcr->csessions, next)
736                 if (cse->ses == ses)
737                         return (cse);
738         return (NULL);
739 }
740
741 static int
742 csedelete(struct fcrypt *fcr, struct csession *cse_del)
743 {
744         struct csession *cse;
745
746         TAILQ_FOREACH(cse, &fcr->csessions, next) {
747                 if (cse == cse_del) {
748                         TAILQ_REMOVE(&fcr->csessions, cse, next);
749                         return (1);
750                 }
751         }
752         return (0);
753 }
754         
755 static struct csession *
756 cseadd(struct fcrypt *fcr, struct csession *cse)
757 {
758         TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
759         cse->ses = fcr->sesn++;
760         return (cse);
761 }
762
763 struct csession *
764 csecreate(struct fcrypt *fcr, u_int64_t sid, caddr_t key, u_int64_t keylen,
765     caddr_t mackey, u_int64_t mackeylen, u_int32_t cipher, u_int32_t mac,
766     struct enc_xform *txform, struct auth_hash *thash)
767 {
768         struct csession *cse;
769
770         cse = kmalloc(sizeof(struct csession), M_XDATA, M_WAITOK | M_ZERO);
771         if (cse == NULL)
772                 return NULL;
773         lockinit(&cse->lock, "cryptodev", 0, LK_CANRECURSE);
774         cse->key = key;
775         cse->keylen = keylen/8;
776         cse->mackey = mackey;
777         cse->mackeylen = mackeylen/8;
778         cse->sid = sid;
779         cse->cipher = cipher;
780         cse->mac = mac;
781         cse->txform = txform;
782         cse->thash = thash;
783         cseadd(fcr, cse);
784         return (cse);
785 }
786
787 static int
788 csefree(struct csession *cse)
789 {
790         int error;
791
792         error = crypto_freesession(cse->sid);
793         lockuninit(&cse->lock);
794         if (cse->key)
795                 kfree(cse->key, M_XDATA);
796         if (cse->mackey)
797                 kfree(cse->mackey, M_XDATA);
798         kfree(cse, M_XDATA);
799         return (error);
800 }
801
802 static int
803 cryptoopen(struct dev_open_args *ap)
804 {
805         return (0);
806 }
807
808 static int
809 cryptoread(struct dev_read_args *ap)
810 {
811         return (EIO);
812 }
813
814 static int
815 cryptowrite(struct dev_write_args *ap)
816 {
817         return (EIO);
818 }
819
820 static int
821 cryptoioctl(struct dev_ioctl_args *ap)
822 {
823         struct thread *td = curthread;
824         struct file *f;
825         struct fcrypt *fcr;
826         int fd, error;
827
828         switch (ap->a_cmd) {
829         case CRIOGET:
830                 fcr = kmalloc(sizeof(struct fcrypt), M_XDATA, M_WAITOK);
831                 TAILQ_INIT(&fcr->csessions);
832                 fcr->sesn = 0;
833
834                 KKASSERT(td->td_lwp);
835                 error = falloc(td->td_lwp, &f, &fd);
836
837                 if (error) {
838                         kfree(fcr, M_XDATA);
839                         return (error);
840                 }
841                 /* falloc automatically provides an extra reference to 'f'. */
842                 f->f_flag = FREAD | FWRITE;
843                 f->f_type = DTYPE_CRYPTO;
844                 f->f_ops = &cryptofops;
845                 f->f_data = fcr;
846                 fsetfd(td->td_proc->p_fd, f, fd);
847                 *(u_int32_t *)ap->a_data = fd;
848                 fdrop(f);
849
850                 break;
851         case CRIOFINDDEV:
852                 error = cryptodev_find((struct crypt_find_op *)ap->a_data);
853                 break;
854         case CRIOASYMFEAT:
855                 error = crypto_getfeat((int *)ap->a_data);
856                 break;
857         default:
858                 error = EINVAL;
859                 break;
860         }
861         return (error);
862 }
863
864 static struct dev_ops crypto_ops = {
865         { "crypto", 0, D_MPSAFE },
866         .d_open =       cryptoopen,
867         .d_read =       cryptoread,
868         .d_write =      cryptowrite,
869         .d_ioctl =      cryptoioctl,
870 };
871
872 /*
873  * Initialization code, both for static and dynamic loading.
874  */
875 static int
876 cryptodev_modevent(module_t mod, int type, void *unused)
877 {
878         switch (type) {
879         case MOD_LOAD:
880                 if (bootverbose)
881                         kprintf("crypto: <crypto device>\n");
882                 make_dev(&crypto_ops, 0, UID_ROOT, GID_WHEEL,
883                          0666, "crypto");
884                 return 0;
885         case MOD_UNLOAD:
886                 /*XXX disallow if active sessions */
887                 //dev_ops_remove(&crypto_ops, 0, 0);
888                 dev_ops_remove_all(&crypto_ops);
889                 return 0;
890         }
891         return EINVAL;
892 }
893
894 static moduledata_t cryptodev_mod = {
895         "cryptodev",
896         cryptodev_modevent,
897         0
898 };
899 MODULE_VERSION(cryptodev, 1);
900 DECLARE_MODULE(cryptodev, cryptodev_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
901 MODULE_DEPEND(cryptodev, crypto, 1, 1, 1);
902 MODULE_DEPEND(cryptodev, zlib, 1, 1, 1);