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