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