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