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