1 /* $OpenBSD: cryptodev.c,v 1.52 2002/06/19 07:22:46 deraadt Exp $ */
2
3 /*-
4 * Copyright (c) 2001 Theo de Raadt
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Effort sponsored in part by the Defense Advanced Research Projects
30 * Agency (DARPA) and Air Force Research Laboratory, Air Force
31 * Materiel Command, USAF, under agreement number F30602-01-2-0537.
32 */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD: releng/6.1/sys/opencrypto/cryptodev.c 156187 2006-03-01 21:40:14Z wkoszek $");
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <sys/mbuf.h>
41 #include <sys/lock.h>
42 #include <sys/mutex.h>
43 #include <sys/sysctl.h>
44 #include <sys/file.h>
45 #include <sys/filedesc.h>
46 #include <sys/errno.h>
47 #include <sys/uio.h>
48 #include <sys/random.h>
49 #include <sys/conf.h>
50 #include <sys/kernel.h>
51 #include <sys/module.h>
52 #include <sys/fcntl.h>
53
54 #include <opencrypto/cryptodev.h>
55 #include <opencrypto/xform.h>
56
57 struct csession {
58 TAILQ_ENTRY(csession) next;
59 u_int64_t sid;
60 u_int32_t ses;
61 struct mtx lock; /* for op submission */
62
63 u_int32_t cipher;
64 struct enc_xform *txform;
65 u_int32_t mac;
66 struct auth_hash *thash;
67
68 caddr_t key;
69 int keylen;
70 u_char tmp_iv[EALG_MAX_BLOCK_LEN];
71
72 caddr_t mackey;
73 int mackeylen;
74 u_char tmp_mac[CRYPTO_MAX_MAC_LEN];
75
76 struct iovec iovec;
77 struct uio uio;
78 int error;
79 };
80
81 struct fcrypt {
82 TAILQ_HEAD(csessionlist, csession) csessions;
83 int sesn;
84 };
85
86 static int cryptof_rw(struct file *fp, struct uio *uio,
87 struct ucred *cred, int flags, struct thread *);
88 static int cryptof_ioctl(struct file *, u_long, void *,
89 struct ucred *, struct thread *);
90 static int cryptof_poll(struct file *, int, struct ucred *, struct thread *);
91 static int cryptof_kqfilter(struct file *, struct knote *);
92 static int cryptof_stat(struct file *, struct stat *,
93 struct ucred *, struct thread *);
94 static int cryptof_close(struct file *, struct thread *);
95
96 static struct fileops cryptofops = {
97 .fo_read = cryptof_rw,
98 .fo_write = cryptof_rw,
99 .fo_ioctl = cryptof_ioctl,
100 .fo_poll = cryptof_poll,
101 .fo_kqfilter = cryptof_kqfilter,
102 .fo_stat = cryptof_stat,
103 .fo_close = cryptof_close
104 };
105
106 static struct csession *csefind(struct fcrypt *, u_int);
107 static int csedelete(struct fcrypt *, struct csession *);
108 static struct csession *cseadd(struct fcrypt *, struct csession *);
109 static struct csession *csecreate(struct fcrypt *, u_int64_t, caddr_t,
110 u_int64_t, caddr_t, u_int64_t, u_int32_t, u_int32_t, struct enc_xform *,
111 struct auth_hash *);
112 static int csefree(struct csession *);
113
114 static int cryptodev_op(struct csession *, struct crypt_op *,
115 struct ucred *, struct thread *td);
116 static int cryptodev_key(struct crypt_kop *);
117
118 static int
119 cryptof_rw(
120 struct file *fp,
121 struct uio *uio,
122 struct ucred *active_cred,
123 int flags,
124 struct thread *td)
125 {
126
127 return (EIO);
128 }
129
130 /* ARGSUSED */
131 static int
132 cryptof_ioctl(
133 struct file *fp,
134 u_long cmd,
135 void *data,
136 struct ucred *active_cred,
137 struct thread *td)
138 {
139 struct cryptoini cria, crie;
140 struct fcrypt *fcr = fp->f_data;
141 struct csession *cse;
142 struct session_op *sop;
143 struct crypt_op *cop;
144 struct enc_xform *txform = NULL;
145 struct auth_hash *thash = NULL;
146 u_int64_t sid;
147 u_int32_t ses;
148 int error = 0;
149
150 /*
151 * XXX: Not sure Giant is needed, but better safe than sorry
152 */
153 mtx_lock(&Giant);
154 switch (cmd) {
155 case CIOCGSESSION:
156 sop = (struct session_op *)data;
157 switch (sop->cipher) {
158 case 0:
159 break;
160 case CRYPTO_DES_CBC:
161 txform = &enc_xform_des;
162 break;
163 case CRYPTO_3DES_CBC:
164 txform = &enc_xform_3des;
165 break;
166 case CRYPTO_BLF_CBC:
167 txform = &enc_xform_blf;
168 break;
169 case CRYPTO_CAST_CBC:
170 txform = &enc_xform_cast5;
171 break;
172 case CRYPTO_SKIPJACK_CBC:
173 txform = &enc_xform_skipjack;
174 break;
175 case CRYPTO_AES_CBC:
176 txform = &enc_xform_rijndael128;
177 break;
178 case CRYPTO_NULL_CBC:
179 txform = &enc_xform_null;
180 break;
181 case CRYPTO_ARC4:
182 txform = &enc_xform_arc4;
183 break;
184 default:
185 mtx_unlock(&Giant);
186 return (EINVAL);
187 }
188
189 switch (sop->mac) {
190 case 0:
191 break;
192 case CRYPTO_MD5_HMAC:
193 thash = &auth_hash_hmac_md5_96;
194 break;
195 case CRYPTO_SHA1_HMAC:
196 thash = &auth_hash_hmac_sha1_96;
197 break;
198 case CRYPTO_SHA2_HMAC:
199 if (sop->mackeylen == auth_hash_hmac_sha2_256.keysize)
200 thash = &auth_hash_hmac_sha2_256;
201 else if (sop->mackeylen == auth_hash_hmac_sha2_384.keysize)
202 thash = &auth_hash_hmac_sha2_384;
203 else if (sop->mackeylen == auth_hash_hmac_sha2_512.keysize)
204 thash = &auth_hash_hmac_sha2_512;
205 else {
206 mtx_unlock(&Giant);
207 return (EINVAL);
208 }
209 break;
210 case CRYPTO_RIPEMD160_HMAC:
211 thash = &auth_hash_hmac_ripemd_160_96;
212 break;
213 #ifdef notdef
214 case CRYPTO_MD5:
215 thash = &auth_hash_md5;
216 break;
217 case CRYPTO_SHA1:
218 thash = &auth_hash_sha1;
219 break;
220 #endif
221 case CRYPTO_NULL_HMAC:
222 thash = &auth_hash_null;
223 break;
224 default:
225 mtx_unlock(&Giant);
226 return (EINVAL);
227 }
228
229 bzero(&crie, sizeof(crie));
230 bzero(&cria, sizeof(cria));
231
232 if (txform) {
233 crie.cri_alg = txform->type;
234 crie.cri_klen = sop->keylen * 8;
235 if (sop->keylen > txform->maxkey ||
236 sop->keylen < txform->minkey) {
237 error = EINVAL;
238 goto bail;
239 }
240
241 MALLOC(crie.cri_key, u_int8_t *,
242 crie.cri_klen / 8, M_XDATA, M_WAITOK);
243 if ((error = copyin(sop->key, crie.cri_key,
244 crie.cri_klen / 8)))
245 goto bail;
246 if (thash)
247 crie.cri_next = &cria;
248 }
249
250 if (thash) {
251 cria.cri_alg = thash->type;
252 cria.cri_klen = sop->mackeylen * 8;
253 if (sop->mackeylen != thash->keysize) {
254 error = EINVAL;
255 goto bail;
256 }
257
258 if (cria.cri_klen) {
259 MALLOC(cria.cri_key, u_int8_t *,
260 cria.cri_klen / 8, M_XDATA, M_WAITOK);
261 if ((error = copyin(sop->mackey, cria.cri_key,
262 cria.cri_klen / 8)))
263 goto bail;
264 }
265 }
266
267 error = crypto_newsession(&sid, (txform ? &crie : &cria), 1);
268 if (error)
269 goto bail;
270
271 cse = csecreate(fcr, sid, crie.cri_key, crie.cri_klen,
272 cria.cri_key, cria.cri_klen, sop->cipher, sop->mac, txform,
273 thash);
274
275 if (cse == NULL) {
276 crypto_freesession(sid);
277 error = EINVAL;
278 goto bail;
279 }
280 sop->ses = cse->ses;
281
282 bail:
283 if (error) {
284 if (crie.cri_key)
285 FREE(crie.cri_key, M_XDATA);
286 if (cria.cri_key)
287 FREE(cria.cri_key, M_XDATA);
288 }
289 break;
290 case CIOCFSESSION:
291 ses = *(u_int32_t *)data;
292 cse = csefind(fcr, ses);
293 if (cse == NULL) {
294 mtx_unlock(&Giant);
295 return (EINVAL);
296 }
297 csedelete(fcr, cse);
298 error = csefree(cse);
299 break;
300 case CIOCCRYPT:
301 cop = (struct crypt_op *)data;
302 cse = csefind(fcr, cop->ses);
303 if (cse == NULL) {
304 mtx_unlock(&Giant);
305 return (EINVAL);
306 }
307 error = cryptodev_op(cse, cop, active_cred, td);
308 break;
309 case CIOCKEY:
310 error = cryptodev_key((struct crypt_kop *)data);
311 break;
312 case CIOCASYMFEAT:
313 error = crypto_getfeat((int *)data);
314 break;
315 default:
316 error = EINVAL;
317 }
318 mtx_unlock(&Giant);
319 return (error);
320 }
321
322 static int cryptodev_cb(void *);
323
324
325 static int
326 cryptodev_op(
327 struct csession *cse,
328 struct crypt_op *cop,
329 struct ucred *active_cred,
330 struct thread *td)
331 {
332 struct cryptop *crp = NULL;
333 struct cryptodesc *crde = NULL, *crda = NULL;
334 int error;
335
336 if (cop->len > 256*1024-4)
337 return (E2BIG);
338
339 if (cse->txform) {
340 if (cop->len == 0 || (cop->len % cse->txform->blocksize) != 0)
341 return (EINVAL);
342 }
343
344 cse->uio.uio_iov = &cse->iovec;
345 cse->uio.uio_iovcnt = 1;
346 cse->uio.uio_offset = 0;
347 cse->uio.uio_resid = cop->len;
348 cse->uio.uio_segflg = UIO_SYSSPACE;
349 cse->uio.uio_rw = UIO_WRITE;
350 cse->uio.uio_td = td;
351 cse->uio.uio_iov[0].iov_len = cop->len;
352 cse->uio.uio_iov[0].iov_base = malloc(cop->len, M_XDATA, M_WAITOK);
353
354 crp = crypto_getreq((cse->txform != NULL) + (cse->thash != NULL));
355 if (crp == NULL) {
356 error = ENOMEM;
357 goto bail;
358 }
359
360 if (cse->thash) {
361 crda = crp->crp_desc;
362 if (cse->txform)
363 crde = crda->crd_next;
364 } else {
365 if (cse->txform)
366 crde = crp->crp_desc;
367 else {
368 error = EINVAL;
369 goto bail;
370 }
371 }
372
373 if ((error = copyin(cop->src, cse->uio.uio_iov[0].iov_base, cop->len)))
374 goto bail;
375
376 if (crda) {
377 crda->crd_skip = 0;
378 crda->crd_len = cop->len;
379 crda->crd_inject = 0; /* ??? */
380
381 crda->crd_alg = cse->mac;
382 crda->crd_key = cse->mackey;
383 crda->crd_klen = cse->mackeylen * 8;
384 }
385
386 if (crde) {
387 if (cop->op == COP_ENCRYPT)
388 crde->crd_flags |= CRD_F_ENCRYPT;
389 else
390 crde->crd_flags &= ~CRD_F_ENCRYPT;
391 crde->crd_len = cop->len;
392 crde->crd_inject = 0;
393
394 crde->crd_alg = cse->cipher;
395 crde->crd_key = cse->key;
396 crde->crd_klen = cse->keylen * 8;
397 }
398
399 crp->crp_ilen = cop->len;
400 crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM
401 | (cop->flags & COP_F_BATCH);
402 crp->crp_buf = (caddr_t)&cse->uio;
403 crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_cb;
404 crp->crp_sid = cse->sid;
405 crp->crp_opaque = (void *)cse;
406
407 if (cop->iv) {
408 if (crde == NULL) {
409 error = EINVAL;
410 goto bail;
411 }
412 if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
413 error = EINVAL;
414 goto bail;
415 }
416 if ((error = copyin(cop->iv, cse->tmp_iv, cse->txform->blocksize)))
417 goto bail;
418 bcopy(cse->tmp_iv, crde->crd_iv, cse->txform->blocksize);
419 crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
420 crde->crd_skip = 0;
421 } else if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
422 crde->crd_skip = 0;
423 } else if (crde) {
424 crde->crd_flags |= CRD_F_IV_PRESENT;
425 crde->crd_skip = cse->txform->blocksize;
426 crde->crd_len -= cse->txform->blocksize;
427 }
428
429 if (cop->mac) {
430 if (crda == NULL) {
431 error = EINVAL;
432 goto bail;
433 }
434 crp->crp_mac=cse->tmp_mac;
435 }
436
437 /*
438 * Let the dispatch run unlocked, then, interlock against the
439 * callback before checking if the operation completed and going
440 * to sleep. This insures drivers don't inherit our lock which
441 * results in a lock order reversal between crypto_dispatch forced
442 * entry and the crypto_done callback into us.
443 */
444 error = crypto_dispatch(crp);
445 mtx_lock(&cse->lock);
446 if (error == 0 && (crp->crp_flags & CRYPTO_F_DONE) == 0)
447 error = msleep(crp, &cse->lock, PWAIT, "crydev", 0);
448 mtx_unlock(&cse->lock);
449
450 if (error != 0)
451 goto bail;
452
453 if (crp->crp_etype != 0) {
454 error = crp->crp_etype;
455 goto bail;
456 }
457
458 if (cse->error) {
459 error = cse->error;
460 goto bail;
461 }
462
463 if (cop->dst &&
464 (error = copyout(cse->uio.uio_iov[0].iov_base, cop->dst, cop->len)))
465 goto bail;
466
467 if (cop->mac &&
468 (error = copyout(crp->crp_mac, cop->mac, cse->thash->authsize)))
469 goto bail;
470
471 bail:
472 if (crp)
473 crypto_freereq(crp);
474 if (cse->uio.uio_iov[0].iov_base)
475 free(cse->uio.uio_iov[0].iov_base, M_XDATA);
476
477 return (error);
478 }
479
480 static int
481 cryptodev_cb(void *op)
482 {
483 struct cryptop *crp = (struct cryptop *) op;
484 struct csession *cse = (struct csession *)crp->crp_opaque;
485
486 cse->error = crp->crp_etype;
487 if (crp->crp_etype == EAGAIN)
488 return crypto_dispatch(crp);
489 mtx_lock(&cse->lock);
490 wakeup_one(crp);
491 mtx_unlock(&cse->lock);
492 return (0);
493 }
494
495 static int
496 cryptodevkey_cb(void *op)
497 {
498 struct cryptkop *krp = (struct cryptkop *) op;
499
500 wakeup(krp);
501 return (0);
502 }
503
504 static int
505 cryptodev_key(struct crypt_kop *kop)
506 {
507 struct cryptkop *krp = NULL;
508 int error = EINVAL;
509 int in, out, size, i;
510
511 if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
512 return (EFBIG);
513 }
514
515 in = kop->crk_iparams;
516 out = kop->crk_oparams;
517 switch (kop->crk_op) {
518 case CRK_MOD_EXP:
519 if (in == 3 && out == 1)
520 break;
521 return (EINVAL);
522 case CRK_MOD_EXP_CRT:
523 if (in == 6 && out == 1)
524 break;
525 return (EINVAL);
526 case CRK_DSA_SIGN:
527 if (in == 5 && out == 2)
528 break;
529 return (EINVAL);
530 case CRK_DSA_VERIFY:
531 if (in == 7 && out == 0)
532 break;
533 return (EINVAL);
534 case CRK_DH_COMPUTE_KEY:
535 if (in == 3 && out == 1)
536 break;
537 return (EINVAL);
538 default:
539 return (EINVAL);
540 }
541
542 krp = (struct cryptkop *)malloc(sizeof *krp, M_XDATA, M_WAITOK);
543 if (!krp)
544 return (ENOMEM);
545 bzero(krp, sizeof *krp);
546 krp->krp_op = kop->crk_op;
547 krp->krp_status = kop->crk_status;
548 krp->krp_iparams = kop->crk_iparams;
549 krp->krp_oparams = kop->crk_oparams;
550 krp->krp_status = 0;
551 krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb;
552
553 for (i = 0; i < CRK_MAXPARAM; i++)
554 krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
555 for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
556 size = (krp->krp_param[i].crp_nbits + 7) / 8;
557 if (size == 0)
558 continue;
559 MALLOC(krp->krp_param[i].crp_p, caddr_t, size, M_XDATA, M_WAITOK);
560 if (i >= krp->krp_iparams)
561 continue;
562 error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size);
563 if (error)
564 goto fail;
565 }
566
567 error = crypto_kdispatch(krp);
568 if (error)
569 goto fail;
570 error = tsleep(krp, PSOCK, "crydev", 0);
571 if (error) {
572 /* XXX can this happen? if so, how do we recover? */
573 goto fail;
574 }
575
576 if (krp->krp_status != 0) {
577 error = krp->krp_status;
578 goto fail;
579 }
580
581 for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
582 size = (krp->krp_param[i].crp_nbits + 7) / 8;
583 if (size == 0)
584 continue;
585 error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size);
586 if (error)
587 goto fail;
588 }
589
590 fail:
591 if (krp) {
592 kop->crk_status = krp->krp_status;
593 for (i = 0; i < CRK_MAXPARAM; i++) {
594 if (krp->krp_param[i].crp_p)
595 FREE(krp->krp_param[i].crp_p, M_XDATA);
596 }
597 free(krp, M_XDATA);
598 }
599 return (error);
600 }
601
602 /* ARGSUSED */
603 static int
604 cryptof_poll(
605 struct file *fp,
606 int events,
607 struct ucred *active_cred,
608 struct thread *td)
609 {
610
611 return (0);
612 }
613
614 /* ARGSUSED */
615 static int
616 cryptof_kqfilter(struct file *fp, struct knote *kn)
617 {
618
619 return (0);
620 }
621
622 /* ARGSUSED */
623 static int
624 cryptof_stat(
625 struct file *fp,
626 struct stat *sb,
627 struct ucred *active_cred,
628 struct thread *td)
629 {
630
631 return (EOPNOTSUPP);
632 }
633
634 /* ARGSUSED */
635 static int
636 cryptof_close(struct file *fp, struct thread *td)
637 {
638 struct fcrypt *fcr = fp->f_data;
639 struct csession *cse;
640
641 while ((cse = TAILQ_FIRST(&fcr->csessions))) {
642 TAILQ_REMOVE(&fcr->csessions, cse, next);
643 (void)csefree(cse);
644 }
645 FREE(fcr, M_XDATA);
646 fp->f_data = NULL;
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 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 #ifdef INVARIANTS
691 /* NB: required when mtx_init is built with INVARIANTS */
692 MALLOC(cse, struct csession *, sizeof(struct csession),
693 M_XDATA, M_NOWAIT | M_ZERO);
694 #else
695 MALLOC(cse, struct csession *, sizeof(struct csession),
696 M_XDATA, M_NOWAIT);
697 #endif
698 if (cse == NULL)
699 return NULL;
700 mtx_init(&cse->lock, "cryptodev", "crypto session lock", MTX_DEF);
701 cse->key = key;
702 cse->keylen = keylen/8;
703 cse->mackey = mackey;
704 cse->mackeylen = mackeylen/8;
705 cse->sid = sid;
706 cse->cipher = cipher;
707 cse->mac = mac;
708 cse->txform = txform;
709 cse->thash = thash;
710 cseadd(fcr, cse);
711 return (cse);
712 }
713
714 static int
715 csefree(struct csession *cse)
716 {
717 int error;
718
719 error = crypto_freesession(cse->sid);
720 mtx_destroy(&cse->lock);
721 if (cse->key)
722 FREE(cse->key, M_XDATA);
723 if (cse->mackey)
724 FREE(cse->mackey, M_XDATA);
725 FREE(cse, M_XDATA);
726 return (error);
727 }
728
729 static int
730 cryptoopen(struct cdev *dev, int oflags, int devtype, struct thread *td)
731 {
732 return (0);
733 }
734
735 static int
736 cryptoread(struct cdev *dev, struct uio *uio, int ioflag)
737 {
738 return (EIO);
739 }
740
741 static int
742 cryptowrite(struct cdev *dev, struct uio *uio, int ioflag)
743 {
744 return (EIO);
745 }
746
747 static int
748 cryptoioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
749 {
750 struct file *f;
751 struct fcrypt *fcr;
752 int fd, error;
753
754 switch (cmd) {
755 case CRIOGET:
756 MALLOC(fcr, struct fcrypt *,
757 sizeof(struct fcrypt), M_XDATA, M_WAITOK);
758 TAILQ_INIT(&fcr->csessions);
759 fcr->sesn = 0;
760
761 error = falloc(td, &f, &fd);
762
763 if (error) {
764 FREE(fcr, M_XDATA);
765 return (error);
766 }
767 /* falloc automatically provides an extra reference to 'f'. */
768 f->f_flag = FREAD | FWRITE;
769 f->f_type = DTYPE_CRYPTO;
770 f->f_ops = &cryptofops;
771 f->f_data = fcr;
772 *(u_int32_t *)data = fd;
773 fdrop(f, td);
774 break;
775 default:
776 error = EINVAL;
777 break;
778 }
779 return (error);
780 }
781
782 static struct cdevsw crypto_cdevsw = {
783 .d_version = D_VERSION,
784 .d_flags = D_NEEDGIANT,
785 .d_open = cryptoopen,
786 .d_read = cryptoread,
787 .d_write = cryptowrite,
788 .d_ioctl = cryptoioctl,
789 .d_name = "crypto",
790 };
791 static struct cdev *crypto_dev;
792
793 /*
794 * Initialization code, both for static and dynamic loading.
795 */
796 static int
797 cryptodev_modevent(module_t mod, int type, void *unused)
798 {
799 switch (type) {
800 case MOD_LOAD:
801 if (bootverbose)
802 printf("crypto: <crypto device>\n");
803 crypto_dev = make_dev(&crypto_cdevsw, 0,
804 UID_ROOT, GID_WHEEL, 0666,
805 "crypto");
806 return 0;
807 case MOD_UNLOAD:
808 /*XXX disallow if active sessions */
809 destroy_dev(crypto_dev);
810 return 0;
811 }
812 return EINVAL;
813 }
814
815 static moduledata_t cryptodev_mod = {
816 "cryptodev",
817 cryptodev_modevent,
818 0
819 };
820 MODULE_VERSION(cryptodev, 1);
821 DECLARE_MODULE(cryptodev, cryptodev_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
822 MODULE_DEPEND(cryptodev, crypto, 1, 1, 1);
823 MODULE_DEPEND(cryptodev, zlib, 1, 1, 1);
Cache object: 13418ec7da37c960970ce68eda162ba6
|