opencrypto - (minor) sync with OpenBSD
[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                 default:
204                         return (EINVAL);
205                 }
206
207                 switch (sop->mac) {
208                 case 0:
209                         break;
210                 case CRYPTO_MD5_HMAC:
211                         thash = &auth_hash_hmac_md5;
212                         break;
213                 case CRYPTO_SHA1_HMAC:
214                         thash = &auth_hash_hmac_sha1;
215                         break;
216                 case CRYPTO_SHA2_256_HMAC:
217                         thash = &auth_hash_hmac_sha2_256;
218                         break;
219                 case CRYPTO_SHA2_384_HMAC:
220                         thash = &auth_hash_hmac_sha2_384;
221                         break;
222                 case CRYPTO_SHA2_512_HMAC:
223                         thash = &auth_hash_hmac_sha2_512;
224                         break;
225                 case CRYPTO_RIPEMD160_HMAC:
226                         thash = &auth_hash_hmac_ripemd_160;
227                         break;
228 #ifdef notdef
229                 case CRYPTO_MD5:
230                         thash = &auth_hash_md5;
231                         break;
232                 case CRYPTO_SHA1:
233                         thash = &auth_hash_sha1;
234                         break;
235 #endif
236                 case CRYPTO_NULL_HMAC:
237                         thash = &auth_hash_null;
238                         break;
239                 default:
240                         return (EINVAL);
241                 }
242
243                 bzero(&crie, sizeof(crie));
244                 bzero(&cria, sizeof(cria));
245
246                 if (txform) {
247                         crie.cri_alg = txform->type;
248                         crie.cri_klen = sop->keylen * 8;
249                         if (sop->keylen > txform->maxkey ||
250                             sop->keylen < txform->minkey) {
251                                 error = EINVAL;
252                                 goto bail;
253                         }
254
255                         crie.cri_key = kmalloc(crie.cri_klen / 8,
256                             M_XDATA, M_WAITOK);
257                         if ((error = copyin(sop->key, crie.cri_key,
258                             crie.cri_klen / 8)))
259                                 goto bail;
260                         if (thash)
261                                 crie.cri_next = &cria;
262                 }
263
264                 if (thash) {
265                         cria.cri_alg = thash->type;
266                         cria.cri_klen = sop->mackeylen * 8;
267                         if (sop->mackeylen != thash->keysize) {
268                                 error = EINVAL;
269                                 goto bail;
270                         }
271
272                         if (cria.cri_klen) {
273                                 cria.cri_key = kmalloc(cria.cri_klen / 8,
274                                     M_XDATA, M_WAITOK);
275                                 if ((error = copyin(sop->mackey, cria.cri_key,
276                                     cria.cri_klen / 8)))
277                                         goto bail;
278                         }
279                 }
280
281                 /* NB: CIOGSESSION2 has the crid */
282                 if (cmd == CIOCGSESSION2) {
283                         crid = SES2(sop)->crid;
284                         error = checkforsoftware(crid);
285                         if (error)
286                                 goto bail;
287                 } else {
288                         crid = CRYPTOCAP_F_HARDWARE;
289                         if (crypto_devallowsoft)
290                                 crid |= CRYPTOCAP_F_SOFTWARE;
291                 }
292                 error = crypto_newsession(&sid, (txform ? &crie : &cria), crid);
293                 if (error)
294                         goto bail;
295
296                 cse = csecreate(fcr, sid, crie.cri_key, crie.cri_klen,
297                     cria.cri_key, cria.cri_klen, sop->cipher, sop->mac, txform,
298                     thash);
299
300                 if (cse == NULL) {
301                         crypto_freesession(sid);
302                         error = EINVAL;
303                         goto bail;
304                 }
305                 sop->ses = cse->ses;
306                 if (cmd == CIOCGSESSION2) {
307                         /* return hardware/driver id */
308                         SES2(sop)->crid = CRYPTO_SESID2HID(cse->sid);
309                 }
310 bail:
311                 if (error) {
312                         if (crie.cri_key) {
313                                 bzero(crie.cri_key, crie.cri_klen / 8);
314                                 kfree(crie.cri_key, M_XDATA);
315                         }
316                         if (cria.cri_key) {
317                                 bzero(crie.cri_key, crie.cri_klen / 8);
318                                 kfree(cria.cri_key, M_XDATA);
319                         }
320                 }
321                 break;
322         case CIOCFSESSION:
323                 ses = *(u_int32_t *)data;
324                 cse = csefind(fcr, ses);
325                 if (cse == NULL)
326                         return (EINVAL);
327                 csedelete(fcr, cse);
328                 error = csefree(cse);
329                 break;
330         case CIOCCRYPT:
331                 cop = (struct crypt_op *)data;
332                 cse = csefind(fcr, cop->ses);
333                 if (cse == NULL)
334                         return (EINVAL);
335                 error = cryptodev_op(cse, cop, cred);
336                 break;
337         case CIOCKEY:
338         case CIOCKEY2:
339                 if (!crypto_userasymcrypto)
340                         return (EPERM);         /* XXX compat? */
341                 get_mplock();
342                 kop = (struct crypt_kop *)data;
343                 if (cmd == CIOCKEY) {
344                         /* NB: crypto core enforces s/w driver use */
345                         kop->crk_crid =
346                             CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE;
347                 }
348                 error = cryptodev_key(kop);
349                 rel_mplock();
350                 break;
351         case CIOCASYMFEAT:
352                 if (!crypto_userasymcrypto) {
353                         /*
354                          * NB: if user asym crypto operations are
355                          * not permitted return "no algorithms"
356                          * so well-behaved applications will just
357                          * fallback to doing them in software.
358                          */
359                         *(int *)data = 0;
360                 } else
361                         error = crypto_getfeat((int *)data);
362                 break;
363         case CIOCFINDDEV:
364                 error = cryptodev_find((struct crypt_find_op *)data);
365                 break;
366         default:
367                 error = EINVAL;
368                 break;
369         }
370         return (error);
371 #undef SES2
372 }
373
374 static int cryptodev_cb(void *);
375
376
377 static int
378 cryptodev_op(struct csession *cse, struct crypt_op *cop,
379     struct ucred *active_cred)
380 {
381         struct cryptop *crp = NULL;
382         struct cryptodesc *crde = NULL, *crda = NULL;
383         int error;
384
385         if (cop->len > 256*1024-4)
386                 return (E2BIG);
387
388         if (cse->txform) {
389                 if (cop->len == 0 || (cop->len % cse->txform->blocksize) != 0)
390                         return (EINVAL);
391         }
392
393         bzero(&cse->uio, sizeof(cse->uio));
394         cse->uio.uio_iov = &cse->iovec;
395         cse->uio.uio_iovcnt = 1;
396         cse->uio.uio_offset = 0;
397         cse->uio.uio_resid = cop->len;
398         cse->uio.uio_segflg = UIO_SYSSPACE;
399         cse->uio.uio_rw = UIO_WRITE;
400         /* XXX: not sure, was td, now curthread? */
401         cse->uio.uio_td = curthread;
402         cse->uio.uio_iov[0].iov_len = cop->len;
403         if (cse->thash) {
404                 cse->uio.uio_iov[0].iov_len += cse->thash->hashsize;
405                 cse->uio.uio_resid += cse->thash->hashsize;
406         }
407         cse->uio.uio_iov[0].iov_base = kmalloc(cse->uio.uio_iov[0].iov_len,
408             M_XDATA, M_WAITOK);
409
410         crp = crypto_getreq((cse->txform != NULL) + (cse->thash != NULL));
411         if (crp == NULL) {
412                 error = ENOMEM;
413                 goto bail;
414         }
415
416         if (cse->thash) {
417                 crda = crp->crp_desc;
418                 if (cse->txform)
419                         crde = crda->crd_next;
420         } else {
421                 if (cse->txform)
422                         crde = crp->crp_desc;
423                 else {
424                         error = EINVAL;
425                         goto bail;
426                 }
427         }
428
429         if ((error = copyin(cop->src, cse->uio.uio_iov[0].iov_base, cop->len)))
430                 goto bail;
431
432         if (crda) {
433                 crda->crd_skip = 0;
434                 crda->crd_len = cop->len;
435                 crda->crd_inject = cop->len;
436
437                 crda->crd_alg = cse->mac;
438                 crda->crd_key = cse->mackey;
439                 crda->crd_klen = cse->mackeylen * 8;
440         }
441
442         if (crde) {
443                 if (cop->op == COP_ENCRYPT)
444                         crde->crd_flags |= CRD_F_ENCRYPT;
445                 else
446                         crde->crd_flags &= ~CRD_F_ENCRYPT;
447                 crde->crd_len = cop->len;
448                 crde->crd_inject = 0;
449
450                 crde->crd_alg = cse->cipher;
451                 crde->crd_key = cse->key;
452                 crde->crd_klen = cse->keylen * 8;
453         }
454
455         crp->crp_ilen = cop->len;
456         crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM
457                        | (cop->flags & COP_F_BATCH);
458         crp->crp_buf = (caddr_t)&cse->uio;
459         crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_cb;
460         crp->crp_sid = cse->sid;
461         crp->crp_opaque = (void *)cse;
462
463         if (cop->iv) {
464                 if (crde == NULL) {
465                         error = EINVAL;
466                         goto bail;
467                 }
468                 if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
469                         error = EINVAL;
470                         goto bail;
471                 }
472                 if ((error = copyin(cop->iv, cse->tmp_iv, cse->txform->blocksize)))
473                         goto bail;
474                 bcopy(cse->tmp_iv, crde->crd_iv, cse->txform->blocksize);
475                 crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
476                 crde->crd_skip = 0;
477         } else if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
478                 crde->crd_skip = 0;
479         } else if (crde) {
480                 crde->crd_flags |= CRD_F_IV_PRESENT;
481                 crde->crd_skip = cse->txform->blocksize;
482                 crde->crd_len -= cse->txform->blocksize;
483         }
484
485         if (cop->mac && crda == NULL) {
486                 error = EINVAL;
487                 goto bail;
488         }
489
490 again:
491         /*
492          * Let the dispatch run unlocked, then, interlock against the
493          * callback before checking if the operation completed and going
494          * to sleep.  This insures drivers don't inherit our lock which
495          * results in a lock order reversal between crypto_dispatch forced
496          * entry and the crypto_done callback into us.
497          */
498         error = crypto_dispatch(crp);
499         lockmgr(&cse->lock, LK_EXCLUSIVE);
500         if (error == 0 && (crp->crp_flags & CRYPTO_F_DONE) == 0)
501                 error = lksleep(crp, &cse->lock, 0, "crydev", 0);
502         lockmgr(&cse->lock, LK_RELEASE);
503
504         if (error != 0)
505                 goto bail;
506
507         if (crp->crp_etype == EAGAIN) {
508                 crp->crp_etype = 0;
509                 crp->crp_flags &= ~CRYPTO_F_DONE;
510                 goto again;
511         }
512
513         if (crp->crp_etype != 0) {
514                 error = crp->crp_etype;
515                 goto bail;
516         }
517
518         if (cse->error) {
519                 error = cse->error;
520                 goto bail;
521         }
522
523         if (cop->dst &&
524             (error = copyout(cse->uio.uio_iov[0].iov_base, cop->dst, cop->len)))
525                 goto bail;
526
527         if (cop->mac &&
528             (error = copyout((caddr_t)cse->uio.uio_iov[0].iov_base + cop->len,
529             cop->mac, cse->thash->hashsize)))
530                 goto bail;
531
532 bail:
533         if (crp)
534                 crypto_freereq(crp);
535         if (cse->uio.uio_iov[0].iov_base)
536                 kfree(cse->uio.uio_iov[0].iov_base, M_XDATA);
537
538         return (error);
539 }
540
541 static int
542 cryptodev_cb(void *op)
543 {
544         struct cryptop *crp = (struct cryptop *) op;
545         struct csession *cse = (struct csession *)crp->crp_opaque;
546
547         lockmgr(&cse->lock, LK_EXCLUSIVE);
548         cse->error = crp->crp_etype;
549         wakeup_one(crp);
550         lockmgr(&cse->lock, LK_RELEASE);
551         return (0);
552 }
553
554 static int
555 cryptodevkey_cb(void *op)
556 {
557         struct cryptkop *krp = (struct cryptkop *) op;
558
559         wakeup_one(krp);
560         return (0);
561 }
562
563 static int
564 cryptodev_key(struct crypt_kop *kop)
565 {
566         struct cryptkop *krp = NULL;
567         int error = EINVAL;
568         int in, out, size, i;
569
570         if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
571                 return (EFBIG);
572         }
573
574         in = kop->crk_iparams;
575         out = kop->crk_oparams;
576         switch (kop->crk_op) {
577         case CRK_MOD_EXP:
578                 if (in == 3 && out == 1)
579                         break;
580                 return (EINVAL);
581         case CRK_MOD_EXP_CRT:
582                 if (in == 6 && out == 1)
583                         break;
584                 return (EINVAL);
585         case CRK_DSA_SIGN:
586                 if (in == 5 && out == 2)
587                         break;
588                 return (EINVAL);
589         case CRK_DSA_VERIFY:
590                 if (in == 7 && out == 0)
591                         break;
592                 return (EINVAL);
593         case CRK_DH_COMPUTE_KEY:
594                 if (in == 3 && out == 1)
595                         break;
596                 return (EINVAL);
597         default:
598                 return (EINVAL);
599         }
600
601         krp = (struct cryptkop *)kmalloc(sizeof *krp, M_XDATA, M_WAITOK | M_ZERO);
602         krp->krp_op = kop->crk_op;
603         krp->krp_status = kop->crk_status;
604         krp->krp_iparams = kop->crk_iparams;
605         krp->krp_oparams = kop->crk_oparams;
606         krp->krp_crid = kop->crk_crid;
607         krp->krp_status = 0;
608         krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb;
609
610         for (i = 0; i < CRK_MAXPARAM; i++) {
611                 if (kop->crk_param[i].crp_nbits > 65536)
612                         /* Limit is the same as in OpenBSD */
613                         goto fail;
614                 krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
615         }
616         for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
617                 size = (krp->krp_param[i].crp_nbits + 7) / 8;
618                 if (size == 0)
619                         continue;
620                 krp->krp_param[i].crp_p = kmalloc(size, M_XDATA, M_WAITOK);
621                 if (i >= krp->krp_iparams)
622                         continue;
623                 error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size);
624                 if (error)
625                         goto fail;
626         }
627
628         error = crypto_kdispatch(krp);
629         if (error)
630                 goto fail;
631         error = tsleep(krp, 0, "crydev", 0);
632         if (error) {
633                 /* XXX can this happen?  if so, how do we recover? */
634                 goto fail;
635         }
636         
637         kop->crk_crid = krp->krp_crid;          /* device that did the work */
638         if (krp->krp_status != 0) {
639                 error = krp->krp_status;
640                 goto fail;
641         }
642
643         for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
644                 size = (krp->krp_param[i].crp_nbits + 7) / 8;
645                 if (size == 0)
646                         continue;
647                 error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size);
648                 if (error)
649                         goto fail;
650         }
651
652 fail:
653         if (krp) {
654                 kop->crk_status = krp->krp_status;
655                 for (i = 0; i < CRK_MAXPARAM; i++) {
656                         if (krp->krp_param[i].crp_p) {
657                                 bzero(krp->krp_param[i].crp_p,
658                                     (krp->krp_param[i].crp_nbits + 7) / 8);
659                                 kfree(krp->krp_param[i].crp_p, M_XDATA);
660                         }
661                 }
662                 kfree(krp, M_XDATA);
663         }
664         return (error);
665 }
666
667 static int
668 cryptodev_find(struct crypt_find_op *find)
669 {
670         device_t dev;
671
672         if (find->crid != -1) {
673                 dev = crypto_find_device_byhid(find->crid);
674                 if (dev == NULL)
675                         return (ENOENT);
676                 strlcpy(find->name, device_get_nameunit(dev),
677                     sizeof(find->name));
678         } else {
679                 find->crid = crypto_find_driver(find->name);
680                 if (find->crid == -1)
681                         return (ENOENT);
682         }
683         return (0);
684 }
685
686 /*
687  * MPSAFE
688  */
689 static int
690 cryptof_kqfilter(struct file *fp, struct knote *kn)
691 {
692
693         return (0);
694 }
695
696 /*
697  * MPSAFE
698  */
699 static int
700 cryptof_stat(struct file *fp, struct stat *sb, struct ucred *cred)
701 {
702         return (EOPNOTSUPP);
703 }
704
705 /*
706  * MPALMOSTSAFE - acquires mplock
707  */
708 static int
709 cryptof_close(struct file *fp)
710 {
711         struct fcrypt *fcr = fp->f_data;
712         struct csession *cse;
713
714         get_mplock();
715         fcr = (struct fcrypt *)fp->f_data;
716         while ((cse = TAILQ_FIRST(&fcr->csessions))) {
717                 TAILQ_REMOVE(&fcr->csessions, cse, next);
718                 (void)csefree(cse);
719         }
720         fp->f_data = NULL;
721         rel_mplock();
722
723         kfree(fcr, M_XDATA);
724         return (0);
725 }
726
727 static struct csession *
728 csefind(struct fcrypt *fcr, u_int ses)
729 {
730         struct csession *cse;
731
732         TAILQ_FOREACH(cse, &fcr->csessions, next)
733                 if (cse->ses == ses)
734                         return (cse);
735         return (NULL);
736 }
737
738 static int
739 csedelete(struct fcrypt *fcr, struct csession *cse_del)
740 {
741         struct csession *cse;
742
743         TAILQ_FOREACH(cse, &fcr->csessions, next) {
744                 if (cse == cse_del) {
745                         TAILQ_REMOVE(&fcr->csessions, cse, next);
746                         return (1);
747                 }
748         }
749         return (0);
750 }
751         
752 static struct csession *
753 cseadd(struct fcrypt *fcr, struct csession *cse)
754 {
755         TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
756         cse->ses = fcr->sesn++;
757         return (cse);
758 }
759
760 struct csession *
761 csecreate(struct fcrypt *fcr, u_int64_t sid, caddr_t key, u_int64_t keylen,
762     caddr_t mackey, u_int64_t mackeylen, u_int32_t cipher, u_int32_t mac,
763     struct enc_xform *txform, struct auth_hash *thash)
764 {
765         struct csession *cse;
766
767         cse = kmalloc(sizeof(struct csession), M_XDATA, M_WAITOK | M_ZERO);
768         if (cse == NULL)
769                 return NULL;
770         lockinit(&cse->lock, "cryptodev", 0, LK_CANRECURSE);
771         cse->key = key;
772         cse->keylen = keylen/8;
773         cse->mackey = mackey;
774         cse->mackeylen = mackeylen/8;
775         cse->sid = sid;
776         cse->cipher = cipher;
777         cse->mac = mac;
778         cse->txform = txform;
779         cse->thash = thash;
780         cseadd(fcr, cse);
781         return (cse);
782 }
783
784 static int
785 csefree(struct csession *cse)
786 {
787         int error;
788
789         error = crypto_freesession(cse->sid);
790         lockuninit(&cse->lock);
791         if (cse->key)
792                 kfree(cse->key, M_XDATA);
793         if (cse->mackey)
794                 kfree(cse->mackey, M_XDATA);
795         kfree(cse, M_XDATA);
796         return (error);
797 }
798
799 static int
800 cryptoopen(struct dev_open_args *ap)
801 {
802         return (0);
803 }
804
805 static int
806 cryptoread(struct dev_read_args *ap)
807 {
808         return (EIO);
809 }
810
811 static int
812 cryptowrite(struct dev_write_args *ap)
813 {
814         return (EIO);
815 }
816
817 static int
818 cryptoioctl(struct dev_ioctl_args *ap)
819 {
820         struct thread *td = curthread;
821         struct file *f;
822         struct fcrypt *fcr;
823         int fd, error;
824
825         switch (ap->a_cmd) {
826         case CRIOGET:
827                 fcr = kmalloc(sizeof(struct fcrypt), M_XDATA, M_WAITOK);
828                 TAILQ_INIT(&fcr->csessions);
829                 fcr->sesn = 0;
830
831                 KKASSERT(td->td_lwp);
832                 error = falloc(td->td_lwp, &f, &fd);
833
834                 if (error) {
835                         kfree(fcr, M_XDATA);
836                         return (error);
837                 }
838                 /* falloc automatically provides an extra reference to 'f'. */
839                 f->f_flag = FREAD | FWRITE;
840                 f->f_type = DTYPE_CRYPTO;
841                 f->f_ops = &cryptofops;
842                 f->f_data = fcr;
843                 fsetfd(td->td_proc->p_fd, f, fd);
844                 *(u_int32_t *)ap->a_data = fd;
845                 fdrop(f);
846
847                 break;
848         case CRIOFINDDEV:
849                 error = cryptodev_find((struct crypt_find_op *)ap->a_data);
850                 break;
851         case CRIOASYMFEAT:
852                 error = crypto_getfeat((int *)ap->a_data);
853                 break;
854         default:
855                 error = EINVAL;
856                 break;
857         }
858         return (error);
859 }
860
861 static struct dev_ops crypto_ops = {
862         { "crypto", 0, D_MPSAFE },
863         .d_open =       cryptoopen,
864         .d_read =       cryptoread,
865         .d_write =      cryptowrite,
866         .d_ioctl =      cryptoioctl,
867 };
868
869 /*
870  * Initialization code, both for static and dynamic loading.
871  */
872 static int
873 cryptodev_modevent(module_t mod, int type, void *unused)
874 {
875         switch (type) {
876         case MOD_LOAD:
877                 if (bootverbose)
878                         kprintf("crypto: <crypto device>\n");
879                 make_dev(&crypto_ops, 0, UID_ROOT, GID_WHEEL,
880                          0666, "crypto");
881                 return 0;
882         case MOD_UNLOAD:
883                 /*XXX disallow if active sessions */
884                 //dev_ops_remove(&crypto_ops, 0, 0);
885                 dev_ops_remove_all(&crypto_ops);
886                 return 0;
887         }
888         return EINVAL;
889 }
890
891 static moduledata_t cryptodev_mod = {
892         "cryptodev",
893         cryptodev_modevent,
894         0
895 };
896 MODULE_VERSION(cryptodev, 1);
897 DECLARE_MODULE(cryptodev, cryptodev_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
898 MODULE_DEPEND(cryptodev, crypto, 1, 1, 1);
899 MODULE_DEPEND(cryptodev, zlib, 1, 1, 1);