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