cryptodev.c revision 1.8 1 /* $NetBSD: cryptodev.c,v 1.8 2003/11/16 00:16:06 jonathan Exp $ */
2 /* $FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.4.2.4 2003/06/03 00:09:02 sam Exp $ */
3 /* $OpenBSD: cryptodev.c,v 1.53 2002/07/10 22:21:30 mickey Exp $ */
4
5 /*
6 * Copyright (c) 2001 Theo de Raadt
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
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: cryptodev.c,v 1.8 2003/11/16 00:16:06 jonathan Exp $");
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
43 #include <sys/mbuf.h>
44 #include <sys/sysctl.h>
45 #include <sys/file.h>
46 #include <sys/filedesc.h>
47 #include <sys/errno.h>
48 #include <sys/md5.h>
49 #include <sys/sha1.h>
50 #include <sys/conf.h>
51 #include <sys/device.h>
52
53 #include <opencrypto/cryptodev.h>
54 #include <opencrypto/xform.h>
55
56 #ifdef __NetBSD__
57 #define splcrypto splnet
58 #endif
59
60 struct csession {
61 TAILQ_ENTRY(csession) next;
62 u_int64_t sid;
63 u_int32_t ses;
64
65 u_int32_t cipher;
66 struct enc_xform *txform;
67 u_int32_t mac;
68 struct auth_hash *thash;
69
70 caddr_t key;
71 int keylen;
72 u_char tmp_iv[EALG_MAX_BLOCK_LEN];
73
74 caddr_t mackey;
75 int mackeylen;
76 u_char tmp_mac[CRYPTO_MAX_MAC_LEN];
77
78 struct iovec iovec[IOV_MAX];
79 struct uio uio;
80 int error;
81 };
82
83 struct fcrypt {
84 TAILQ_HEAD(csessionlist, csession) csessions;
85 int sesn;
86 };
87
88
89 /* Declaration of master device (fd-cloning/ctxt-allocating) entrypoints */
90 static int cryptoopen(dev_t dev, int flag, int mode, struct proc *p);
91 static int cryptoread(dev_t dev, struct uio *uio, int ioflag);
92 static int cryptowrite(dev_t dev, struct uio *uio, int ioflag);
93 static int cryptoioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p);
94 static int cryptoselect(dev_t dev, int rw, struct proc *p);
95
96 /* Declaration of cloned-device (per-ctxt) entrypoints */
97 static int cryptof_read(struct file *, off_t *, struct uio *, struct ucred *, int);
98 static int cryptof_write(struct file *, off_t *, struct uio *, struct ucred *, int);
99 static int cryptof_ioctl(struct file *, u_long, void*, struct proc *p);
100 static int cryptof_fcntl(struct file *, u_int, void*, struct proc *p);
101 static int cryptof_poll(struct file *, int, struct proc *);
102 static int cryptof_kqfilter(struct file *, struct knote *);
103 static int cryptof_stat(struct file *, struct stat *, struct proc *);
104 static int cryptof_close(struct file *, struct proc *);
105
106 static struct fileops cryptofops = {
107 cryptof_read,
108 cryptof_write,
109 cryptof_ioctl,
110 cryptof_fcntl,
111 cryptof_poll,
112 cryptof_stat,
113 cryptof_close,
114 cryptof_kqfilter
115 };
116
117 static struct csession *csefind(struct fcrypt *, u_int);
118 static int csedelete(struct fcrypt *, struct csession *);
119 static struct csession *cseadd(struct fcrypt *, struct csession *);
120 static struct csession *csecreate(struct fcrypt *, u_int64_t, caddr_t, u_int64_t,
121 caddr_t, u_int64_t, u_int32_t, u_int32_t, struct enc_xform *,
122 struct auth_hash *);
123 static int csefree(struct csession *);
124
125 static int cryptodev_op(struct csession *, struct crypt_op *, struct proc *);
126 static int cryptodev_key(struct crypt_kop *);
127 int cryptodev_dokey(struct crypt_kop *kop, struct crparam kvp[]);
128
129 static int cryptodev_cb(void *);
130 static int cryptodevkey_cb(void *);
131
132 int usercrypto = 1; /* userland may do crypto requests */
133 int userasymcrypto = 1; /* userland may do asymmetric crypto reqs */
134 int cryptodevallowsoft = 1; /* only use hardware crypto */
135
136 /* ARGSUSED */
137 int
138 cryptof_read(struct file *fp, off_t *poff, struct uio *uio,
139 struct ucred *cred, int flags)
140 {
141 return (EIO);
142 }
143
144 /* ARGSUSED */
145 int
146 cryptof_write(struct file *fp, off_t *poff, struct uio *uio,
147 struct ucred *cred, int flags)
148 {
149 return (EIO);
150 }
151
152 /* ARGSUSED */
153 int
154 cryptof_ioctl(struct file *fp, u_long cmd, void* data, struct proc *p)
155 {
156 struct cryptoini cria, crie;
157 struct fcrypt *fcr = (struct fcrypt *)fp->f_data;
158 struct csession *cse;
159 struct session_op *sop;
160 struct crypt_op *cop;
161 struct enc_xform *txform = NULL;
162 struct auth_hash *thash = NULL;
163 u_int64_t sid;
164 u_int32_t ses;
165 int error = 0;
166
167 switch (cmd) {
168 case CIOCGSESSION:
169 sop = (struct session_op *)data;
170 switch (sop->cipher) {
171 case 0:
172 break;
173 case CRYPTO_DES_CBC:
174 txform = &enc_xform_des;
175 break;
176 case CRYPTO_3DES_CBC:
177 txform = &enc_xform_3des;
178 break;
179 case CRYPTO_BLF_CBC:
180 txform = &enc_xform_blf;
181 break;
182 case CRYPTO_CAST_CBC:
183 txform = &enc_xform_cast5;
184 break;
185 case CRYPTO_SKIPJACK_CBC:
186 txform = &enc_xform_skipjack;
187 break;
188 case CRYPTO_AES_CBC:
189 txform = &enc_xform_rijndael128;
190 break;
191 case CRYPTO_NULL_CBC:
192 txform = &enc_xform_null;
193 break;
194 case CRYPTO_ARC4:
195 txform = &enc_xform_arc4;
196 break;
197 default:
198 return (EINVAL);
199 }
200
201 switch (sop->mac) {
202 case 0:
203 break;
204 case CRYPTO_MD5_HMAC:
205 thash = &auth_hash_hmac_md5_96;
206 break;
207 case CRYPTO_SHA1_HMAC:
208 thash = &auth_hash_hmac_sha1_96;
209 break;
210 case CRYPTO_SHA2_HMAC:
211 if (sop->mackeylen == auth_hash_hmac_sha2_256.keysize)
212 thash = &auth_hash_hmac_sha2_256;
213 else if (sop->mackeylen == auth_hash_hmac_sha2_384.keysize)
214 thash = &auth_hash_hmac_sha2_384;
215 else if (sop->mackeylen == auth_hash_hmac_sha2_512.keysize)
216 thash = &auth_hash_hmac_sha2_512;
217 else
218 return (EINVAL);
219 break;
220 case CRYPTO_RIPEMD160_HMAC:
221 thash = &auth_hash_hmac_ripemd_160_96;
222 break;
223 case CRYPTO_MD5:
224 thash = &auth_hash_md5;
225 break;
226 case CRYPTO_SHA1:
227 thash = &auth_hash_sha1;
228 break;
229 case CRYPTO_NULL_HMAC:
230 thash = &auth_hash_null;
231 break;
232 default:
233 return (EINVAL);
234 }
235
236 bzero(&crie, sizeof(crie));
237 bzero(&cria, sizeof(cria));
238
239 if (txform) {
240 crie.cri_alg = txform->type;
241 crie.cri_klen = sop->keylen * 8;
242 if (sop->keylen > txform->maxkey ||
243 sop->keylen < txform->minkey) {
244 error = EINVAL;
245 goto bail;
246 }
247
248 MALLOC(crie.cri_key, u_int8_t *,
249 crie.cri_klen / 8, M_XDATA, M_WAITOK);
250 if ((error = copyin(sop->key, crie.cri_key,
251 crie.cri_klen / 8)))
252 goto bail;
253 if (thash)
254 crie.cri_next = &cria;
255 }
256
257 if (thash) {
258 cria.cri_alg = thash->type;
259 cria.cri_klen = sop->mackeylen * 8;
260 if (sop->mackeylen != thash->keysize) {
261 error = EINVAL;
262 goto bail;
263 }
264
265 if (cria.cri_klen) {
266 MALLOC(cria.cri_key, u_int8_t *,
267 cria.cri_klen / 8, M_XDATA, M_WAITOK);
268 if ((error = copyin(sop->mackey, cria.cri_key,
269 cria.cri_klen / 8)))
270 goto bail;
271 }
272 }
273
274 error = crypto_newsession(&sid, (txform ? &crie : &cria),
275 0);
276 if (error) {
277 /* this is an auditable security event? */
278 printf("SIOCSESSION violates kernel parameters\n");
279 goto bail;
280 }
281
282 cse = csecreate(fcr, sid, crie.cri_key, crie.cri_klen,
283 cria.cri_key, cria.cri_klen, sop->cipher, sop->mac, txform,
284 thash);
285
286 if (cse == NULL) {
287 crypto_freesession(sid);
288 error = EINVAL;
289 goto bail;
290 }
291 sop->ses = cse->ses;
292
293 bail:
294 if (error) {
295 if (crie.cri_key)
296 FREE(crie.cri_key, M_XDATA);
297 if (cria.cri_key)
298 FREE(cria.cri_key, M_XDATA);
299 }
300 break;
301 case CIOCFSESSION:
302 ses = *(u_int32_t *)data;
303 cse = csefind(fcr, ses);
304 if (cse == NULL)
305 return (EINVAL);
306 csedelete(fcr, cse);
307 error = csefree(cse);
308 break;
309 case CIOCCRYPT:
310 cop = (struct crypt_op *)data;
311 cse = csefind(fcr, cop->ses);
312 if (cse == NULL)
313 return (EINVAL);
314 error = cryptodev_op(cse, cop, p);
315 break;
316 case CIOCKEY:
317 error = cryptodev_key((struct crypt_kop *)data);
318 break;
319 case CIOCASYMFEAT:
320 error = crypto_getfeat((int *)data);
321 break;
322 default:
323 error = EINVAL;
324 }
325 return (error);
326 }
327
328 /* ARGSUSED */
329 int
330 cryptof_fcntl(struct file *fp, u_int cmd, void *data, struct proc *p)
331 {
332 return (0);
333 }
334
335 static int
336 cryptodev_op(struct csession *cse, struct crypt_op *cop, struct proc *p)
337 {
338 struct cryptop *crp = NULL;
339 struct cryptodesc *crde = NULL, *crda = NULL;
340 int i, error, s;
341
342 if (cop->len > 256*1024-4)
343 return (E2BIG);
344
345 if (cse->txform && (cop->len % cse->txform->blocksize) != 0)
346 return (EINVAL);
347
348 bzero(&cse->uio, sizeof(cse->uio));
349 cse->uio.uio_iovcnt = 1;
350 cse->uio.uio_resid = 0;
351 cse->uio.uio_segflg = UIO_SYSSPACE;
352 cse->uio.uio_rw = UIO_WRITE;
353 cse->uio.uio_procp = p;
354 cse->uio.uio_iov = cse->iovec;
355 bzero(&cse->iovec, sizeof(cse->iovec));
356 cse->uio.uio_iov[0].iov_len = cop->len;
357 cse->uio.uio_iov[0].iov_base = malloc(cop->len, M_XDATA, M_WAITOK);
358 for (i = 0; i < cse->uio.uio_iovcnt; i++)
359 cse->uio.uio_resid += cse->uio.uio_iov[0].iov_len;
360
361 crp = crypto_getreq((cse->txform != NULL) + (cse->thash != NULL));
362 if (crp == NULL) {
363 error = ENOMEM;
364 goto bail;
365 }
366
367 if (cse->thash) {
368 crda = crp->crp_desc;
369 if (cse->txform)
370 crde = crda->crd_next;
371 } else {
372 if (cse->txform)
373 crde = crp->crp_desc;
374 else {
375 error = EINVAL;
376 goto bail;
377 }
378 }
379
380 if ((error = copyin(cop->src, cse->uio.uio_iov[0].iov_base, cop->len)))
381 goto bail;
382
383 if (crda) {
384 crda->crd_skip = 0;
385 crda->crd_len = cop->len;
386 crda->crd_inject = 0; /* ??? */
387
388 crda->crd_alg = cse->mac;
389 crda->crd_key = cse->mackey;
390 crda->crd_klen = cse->mackeylen * 8;
391 }
392
393 if (crde) {
394 if (cop->op == COP_ENCRYPT)
395 crde->crd_flags |= CRD_F_ENCRYPT;
396 else
397 crde->crd_flags &= ~CRD_F_ENCRYPT;
398 crde->crd_len = cop->len;
399 crde->crd_inject = 0;
400
401 crde->crd_alg = cse->cipher;
402 crde->crd_key = cse->key;
403 crde->crd_klen = cse->keylen * 8;
404 }
405
406 crp->crp_ilen = cop->len;
407 crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM
408 | (cop->flags & COP_F_BATCH);
409 crp->crp_buf = (caddr_t)&cse->uio;
410 crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_cb;
411 crp->crp_sid = cse->sid;
412 crp->crp_opaque = (void *)cse;
413
414 if (cop->iv) {
415 if (crde == NULL) {
416 error = EINVAL;
417 goto bail;
418 }
419 if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
420 error = EINVAL;
421 goto bail;
422 }
423 if ((error = copyin(cop->iv, cse->tmp_iv, cse->txform->blocksize)))
424 goto bail;
425 bcopy(cse->tmp_iv, crde->crd_iv, cse->txform->blocksize);
426 crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
427 crde->crd_skip = 0;
428 } else if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
429 crde->crd_skip = 0;
430 } else if (crde) {
431 crde->crd_flags |= CRD_F_IV_PRESENT;
432 crde->crd_skip = cse->txform->blocksize;
433 crde->crd_len -= cse->txform->blocksize;
434 }
435
436 if (cop->mac) {
437 if (crda == NULL) {
438 error = EINVAL;
439 goto bail;
440 }
441 crp->crp_mac=cse->tmp_mac;
442 }
443
444 s = splcrypto(); /* NB: only needed with CRYPTO_F_CBIMM */
445 error = crypto_dispatch(crp);
446 if (error == 0 && (crp->crp_flags & CRYPTO_F_DONE) == 0)
447 error = tsleep(crp, PSOCK, "crydev", 0);
448 splx(s);
449 if (error) {
450 goto bail;
451 }
452
453 if (crp->crp_etype != 0) {
454 error = crp->crp_etype;
455 goto bail;
456 }
457
458 if (cse->error) {
459 error = cse->error;
460 goto bail;
461 }
462
463 if (cop->dst &&
464 (error = copyout(cse->uio.uio_iov[0].iov_base, cop->dst, cop->len)))
465 goto bail;
466
467 if (cop->mac &&
468 (error = copyout(crp->crp_mac, cop->mac, cse->thash->authsize)))
469 goto bail;
470
471 bail:
472 if (crp)
473 crypto_freereq(crp);
474 if (cse->uio.uio_iov[0].iov_base)
475 free(cse->uio.uio_iov[0].iov_base, M_XDATA);
476
477 return (error);
478 }
479
480 static int
481 cryptodev_cb(void *op)
482 {
483 struct cryptop *crp = (struct cryptop *) op;
484 struct csession *cse = (struct csession *)crp->crp_opaque;
485
486 cse->error = crp->crp_etype;
487 if (crp->crp_etype == EAGAIN)
488 return crypto_dispatch(crp);
489 wakeup_one(crp);
490 return (0);
491 }
492
493 static int
494 cryptodevkey_cb(void *op)
495 {
496 struct cryptkop *krp = (struct cryptkop *) op;
497
498 wakeup_one(krp);
499 return (0);
500 }
501
502 static int
503 cryptodev_key(struct crypt_kop *kop)
504 {
505 struct cryptkop *krp = NULL;
506 int error = EINVAL;
507 int in, out, size, i;
508
509 if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
510 return (EFBIG);
511 }
512
513 in = kop->crk_iparams;
514 out = kop->crk_oparams;
515 switch (kop->crk_op) {
516 case CRK_MOD_EXP:
517 if (in == 3 && out == 1)
518 break;
519 return (EINVAL);
520 case CRK_MOD_EXP_CRT:
521 if (in == 6 && out == 1)
522 break;
523 return (EINVAL);
524 case CRK_DSA_SIGN:
525 if (in == 5 && out == 2)
526 break;
527 return (EINVAL);
528 case CRK_DSA_VERIFY:
529 if (in == 7 && out == 0)
530 break;
531 return (EINVAL);
532 case CRK_DH_COMPUTE_KEY:
533 if (in == 3 && out == 1)
534 break;
535 return (EINVAL);
536 default:
537 return (EINVAL);
538 }
539
540 krp = (struct cryptkop *)malloc(sizeof *krp, M_XDATA, M_WAITOK);
541 if (!krp)
542 return (ENOMEM);
543 bzero(krp, sizeof *krp);
544 krp->krp_op = kop->crk_op;
545 krp->krp_status = kop->crk_status;
546 krp->krp_iparams = kop->crk_iparams;
547 krp->krp_oparams = kop->crk_oparams;
548 krp->krp_status = 0;
549 krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb;
550
551 for (i = 0; i < CRK_MAXPARAM; i++)
552 krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
553 for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
554 size = (krp->krp_param[i].crp_nbits + 7) / 8;
555 if (size == 0)
556 continue;
557 MALLOC(krp->krp_param[i].crp_p, caddr_t, size, M_XDATA, M_WAITOK);
558 if (i >= krp->krp_iparams)
559 continue;
560 error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size);
561 if (error)
562 goto fail;
563 }
564
565 error = crypto_kdispatch(krp);
566 if (error == 0)
567 error = tsleep(krp, PSOCK, "crydev", 0);
568 if (error)
569 goto fail;
570
571 if (krp->krp_status != 0) {
572 error = krp->krp_status;
573 goto fail;
574 }
575
576 for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
577 size = (krp->krp_param[i].crp_nbits + 7) / 8;
578 if (size == 0)
579 continue;
580 error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size);
581 if (error)
582 goto fail;
583 }
584
585 fail:
586 if (krp) {
587 kop->crk_status = krp->krp_status;
588 for (i = 0; i < CRK_MAXPARAM; i++) {
589 if (krp->krp_param[i].crp_p)
590 FREE(krp->krp_param[i].crp_p, M_XDATA);
591 }
592 free(krp, M_XDATA);
593 }
594 return (error);
595 }
596
597 /* ARGSUSED */
598 static int
599 cryptof_poll(struct file *fp, int which, struct proc *p)
600 {
601 return (0);
602 }
603
604
605 /* ARGSUSED */
606 static int
607 cryptof_kqfilter(struct file *fp, struct knote *kn)
608 {
609
610 return (0);
611 }
612
613 /* ARGSUSED */
614 static int
615 cryptof_stat(struct file *fp, struct stat *sb, struct proc *p)
616 {
617 return (EOPNOTSUPP);
618 }
619
620 /* ARGSUSED */
621 static int
622 cryptof_close(struct file *fp, struct proc *p)
623 {
624 struct fcrypt *fcr = (struct fcrypt *)fp->f_data;
625 struct csession *cse;
626
627 while ((cse = TAILQ_FIRST(&fcr->csessions))) {
628 TAILQ_REMOVE(&fcr->csessions, cse, next);
629 (void)csefree(cse);
630 }
631 FREE(fcr, M_XDATA);
632
633 /* close() stolen from sys/kern/kern_ktrace.c */
634
635 fp->f_data = NULL;
636 #if 0
637 FILE_UNUSE(fp, p); /* release file */
638 fdrelease(p, fd); /* release fd table slot */
639 #endif
640
641 return 0;
642 }
643
644 static struct csession *
645 csefind(struct fcrypt *fcr, u_int ses)
646 {
647 struct csession *cse;
648
649 TAILQ_FOREACH(cse, &fcr->csessions, next)
650 if (cse->ses == ses)
651 return (cse);
652 return (NULL);
653 }
654
655 static int
656 csedelete(struct fcrypt *fcr, struct csession *cse_del)
657 {
658 struct csession *cse;
659
660 TAILQ_FOREACH(cse, &fcr->csessions, next) {
661 if (cse == cse_del) {
662 TAILQ_REMOVE(&fcr->csessions, cse, next);
663 return (1);
664 }
665 }
666 return (0);
667 }
668
669 static struct csession *
670 cseadd(struct fcrypt *fcr, struct csession *cse)
671 {
672 TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
673 cse->ses = fcr->sesn++;
674 return (cse);
675 }
676
677 static struct csession *
678 csecreate(struct fcrypt *fcr, u_int64_t sid, caddr_t key, u_int64_t keylen,
679 caddr_t mackey, u_int64_t mackeylen, u_int32_t cipher, u_int32_t mac,
680 struct enc_xform *txform, struct auth_hash *thash)
681 {
682 struct csession *cse;
683
684 MALLOC(cse, struct csession *, sizeof(struct csession),
685 M_XDATA, M_NOWAIT);
686 if (cse == NULL)
687 return NULL;
688 cse->key = key;
689 cse->keylen = keylen/8;
690 cse->mackey = mackey;
691 cse->mackeylen = mackeylen/8;
692 cse->sid = sid;
693 cse->cipher = cipher;
694 cse->mac = mac;
695 cse->txform = txform;
696 cse->thash = thash;
697 cseadd(fcr, cse);
698 return (cse);
699 }
700
701 static int
702 csefree(struct csession *cse)
703 {
704 int error;
705
706 error = crypto_freesession(cse->sid);
707 if (cse->key)
708 FREE(cse->key, M_XDATA);
709 if (cse->mackey)
710 FREE(cse->mackey, M_XDATA);
711 FREE(cse, M_XDATA);
712 return (error);
713 }
714
715 static int
716 cryptoopen(dev_t dev, int flag, int mode, struct proc *p)
717 {
718 if (crypto_usercrypto == 0)
719 return (ENXIO);
720 return (0);
721 }
722
723 static int
724 cryptoread(dev_t dev, struct uio *uio, int ioflag)
725 {
726 return (EIO);
727 }
728
729 static int
730 cryptowrite(dev_t dev, struct uio *uio, int ioflag)
731 {
732 return (EIO);
733 }
734
735 static int
736 cryptoioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
737 {
738 struct file *f;
739 struct fcrypt *fcr;
740 int fd, error;
741
742 switch (cmd) {
743 case CRIOGET:
744 MALLOC(fcr, struct fcrypt *,
745 sizeof(struct fcrypt), M_XDATA, M_WAITOK);
746 TAILQ_INIT(&fcr->csessions);
747 fcr->sesn = 0;
748
749 error = falloc(p, &f, &fd);
750 if (error) {
751 FREE(fcr, M_XDATA);
752 return (error);
753 }
754 f->f_flag = FREAD | FWRITE;
755 f->f_type = DTYPE_CRYPTO;
756 f->f_ops = &cryptofops;
757 f->f_data = (caddr_t) fcr;
758 *(u_int32_t *)data = fd;
759 FILE_SET_MATURE(f);
760 FILE_UNUSE(f, p);
761 break;
762 default:
763 error = EINVAL;
764 break;
765 }
766 return (error);
767 }
768
769 int
770 cryptoselect(dev_t dev, int rw, struct proc *p)
771 {
772 return (0);
773 }
774
775 /*static*/
776 struct cdevsw crypto_cdevsw = {
777 /* open */ cryptoopen,
778 /* close */ nullclose,
779 /* read */ cryptoread,
780 /* write */ cryptowrite,
781 /* ioctl */ cryptoioctl,
782 /* ttstop?*/ nostop,
783 /* ??*/ notty,
784 /* poll */ cryptoselect /*nopoll*/,
785 /* mmap */ nommap,
786 /* kqfilter */ nokqfilter,
787 };
788
789