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