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/5.4/sys/opencrypto/cryptodev.c 141090 2005-01-31 23:27:04Z imp $");
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 switch (cmd) {
151 case CIOCGSESSION:
152 sop = (struct session_op *)data;
153 switch (sop->cipher) {
154 case 0:
155 break;
156 case CRYPTO_DES_CBC:
157 txform = &enc_xform_des;
158 break;
159 case CRYPTO_3DES_CBC:
160 txform = &enc_xform_3des;
161 break;
162 case CRYPTO_BLF_CBC:
163 txform = &enc_xform_blf;
164 break;
165 case CRYPTO_CAST_CBC:
166 txform = &enc_xform_cast5;
167 break;
168 case CRYPTO_SKIPJACK_CBC:
169 txform = &enc_xform_skipjack;
170 break;
171 case CRYPTO_AES_CBC:
172 txform = &enc_xform_rijndael128;
173 break;
174 case CRYPTO_NULL_CBC:
175 txform = &enc_xform_null;
176 break;
177 case CRYPTO_ARC4:
178 txform = &enc_xform_arc4;
179 break;
180 default:
181 return (EINVAL);
182 }
183
184 switch (sop->mac) {
185 case 0:
186 break;
187 case CRYPTO_MD5_HMAC:
188 thash = &auth_hash_hmac_md5_96;
189 break;
190 case CRYPTO_SHA1_HMAC:
191 thash = &auth_hash_hmac_sha1_96;
192 break;
193 case CRYPTO_SHA2_HMAC:
194 if (sop->mackeylen == auth_hash_hmac_sha2_256.keysize)
195 thash = &auth_hash_hmac_sha2_256;
196 else if (sop->mackeylen == auth_hash_hmac_sha2_384.keysize)
197 thash = &auth_hash_hmac_sha2_384;
198 else if (sop->mackeylen == auth_hash_hmac_sha2_512.keysize)
199 thash = &auth_hash_hmac_sha2_512;
200 else
201 return (EINVAL);
202 break;
203 case CRYPTO_RIPEMD160_HMAC:
204 thash = &auth_hash_hmac_ripemd_160_96;
205 break;
206 #ifdef notdef
207 case CRYPTO_MD5:
208 thash = &auth_hash_md5;
209 break;
210 case CRYPTO_SHA1:
211 thash = &auth_hash_sha1;
212 break;
213 #endif
214 case CRYPTO_NULL_HMAC:
215 thash = &auth_hash_null;
216 break;
217 default:
218 return (EINVAL);
219 }
220
221 bzero(&crie, sizeof(crie));
222 bzero(&cria, sizeof(cria));
223
224 if (txform) {
225 crie.cri_alg = txform->type;
226 crie.cri_klen = sop->keylen * 8;
227 if (sop->keylen > txform->maxkey ||
228 sop->keylen < txform->minkey) {
229 error = EINVAL;
230 goto bail;
231 }
232
233 MALLOC(crie.cri_key, u_int8_t *,
234 crie.cri_klen / 8, M_XDATA, M_WAITOK);
235 if ((error = copyin(sop->key, crie.cri_key,
236 crie.cri_klen / 8)))
237 goto bail;
238 if (thash)
239 crie.cri_next = &cria;
240 }
241
242 if (thash) {
243 cria.cri_alg = thash->type;
244 cria.cri_klen = sop->mackeylen * 8;
245 if (sop->mackeylen != thash->keysize) {
246 error = EINVAL;
247 goto bail;
248 }
249
250 if (cria.cri_klen) {
251 MALLOC(cria.cri_key, u_int8_t *,
252 cria.cri_klen / 8, M_XDATA, M_WAITOK);
253 if ((error = copyin(sop->mackey, cria.cri_key,
254 cria.cri_klen / 8)))
255 goto bail;
256 }
257 }
258
259 error = crypto_newsession(&sid, (txform ? &crie : &cria), 1);
260 if (error)
261 goto bail;
262
263 cse = csecreate(fcr, sid, crie.cri_key, crie.cri_klen,
264 cria.cri_key, cria.cri_klen, sop->cipher, sop->mac, txform,
265 thash);
266
267 if (cse == NULL) {
268 crypto_freesession(sid);
269 error = EINVAL;
270 goto bail;
271 }
272 sop->ses = cse->ses;
273
274 bail:
275 if (error) {
276 if (crie.cri_key)
277 FREE(crie.cri_key, M_XDATA);
278 if (cria.cri_key)
279 FREE(cria.cri_key, M_XDATA);
280 }
281 break;
282 case CIOCFSESSION:
283 ses = *(u_int32_t *)data;
284 cse = csefind(fcr, ses);
285 if (cse == NULL)
286 return (EINVAL);
287 csedelete(fcr, cse);
288 error = csefree(cse);
289 break;
290 case CIOCCRYPT:
291 cop = (struct crypt_op *)data;
292 cse = csefind(fcr, cop->ses);
293 if (cse == NULL)
294 return (EINVAL);
295 error = cryptodev_op(cse, cop, active_cred, td);
296 break;
297 case CIOCKEY:
298 error = cryptodev_key((struct crypt_kop *)data);
299 break;
300 case CIOCASYMFEAT:
301 error = crypto_getfeat((int *)data);
302 break;
303 default:
304 error = EINVAL;
305 }
306 return (error);
307 }
308
309 static int cryptodev_cb(void *);
310
311
312 static int
313 cryptodev_op(
314 struct csession *cse,
315 struct crypt_op *cop,
316 struct ucred *active_cred,
317 struct thread *td)
318 {
319 struct cryptop *crp = NULL;
320 struct cryptodesc *crde = NULL, *crda = NULL;
321 int error;
322
323 if (cop->len > 256*1024-4)
324 return (E2BIG);
325
326 if (cse->txform && (cop->len % cse->txform->blocksize) != 0)
327 return (EINVAL);
328
329 cse->uio.uio_iov = &cse->iovec;
330 cse->uio.uio_iovcnt = 1;
331 cse->uio.uio_offset = 0;
332 cse->uio.uio_resid = cop->len;
333 cse->uio.uio_segflg = UIO_SYSSPACE;
334 cse->uio.uio_rw = UIO_WRITE;
335 cse->uio.uio_td = td;
336 cse->uio.uio_iov[0].iov_len = cop->len;
337 cse->uio.uio_iov[0].iov_base = malloc(cop->len, M_XDATA, M_WAITOK);
338
339 crp = crypto_getreq((cse->txform != NULL) + (cse->thash != NULL));
340 if (crp == NULL) {
341 error = ENOMEM;
342 goto bail;
343 }
344
345 if (cse->thash) {
346 crda = crp->crp_desc;
347 if (cse->txform)
348 crde = crda->crd_next;
349 } else {
350 if (cse->txform)
351 crde = crp->crp_desc;
352 else {
353 error = EINVAL;
354 goto bail;
355 }
356 }
357
358 if ((error = copyin(cop->src, cse->uio.uio_iov[0].iov_base, cop->len)))
359 goto bail;
360
361 if (crda) {
362 crda->crd_skip = 0;
363 crda->crd_len = cop->len;
364 crda->crd_inject = 0; /* ??? */
365
366 crda->crd_alg = cse->mac;
367 crda->crd_key = cse->mackey;
368 crda->crd_klen = cse->mackeylen * 8;
369 }
370
371 if (crde) {
372 if (cop->op == COP_ENCRYPT)
373 crde->crd_flags |= CRD_F_ENCRYPT;
374 else
375 crde->crd_flags &= ~CRD_F_ENCRYPT;
376 crde->crd_len = cop->len;
377 crde->crd_inject = 0;
378
379 crde->crd_alg = cse->cipher;
380 crde->crd_key = cse->key;
381 crde->crd_klen = cse->keylen * 8;
382 }
383
384 crp->crp_ilen = cop->len;
385 crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM
386 | (cop->flags & COP_F_BATCH);
387 crp->crp_buf = (caddr_t)&cse->uio;
388 crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_cb;
389 crp->crp_sid = cse->sid;
390 crp->crp_opaque = (void *)cse;
391
392 if (cop->iv) {
393 if (crde == NULL) {
394 error = EINVAL;
395 goto bail;
396 }
397 if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
398 error = EINVAL;
399 goto bail;
400 }
401 if ((error = copyin(cop->iv, cse->tmp_iv, cse->txform->blocksize)))
402 goto bail;
403 bcopy(cse->tmp_iv, crde->crd_iv, cse->txform->blocksize);
404 crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
405 crde->crd_skip = 0;
406 } else if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
407 crde->crd_skip = 0;
408 } else if (crde) {
409 crde->crd_flags |= CRD_F_IV_PRESENT;
410 crde->crd_skip = cse->txform->blocksize;
411 crde->crd_len -= cse->txform->blocksize;
412 }
413
414 if (cop->mac) {
415 if (crda == NULL) {
416 error = EINVAL;
417 goto bail;
418 }
419 crp->crp_mac=cse->tmp_mac;
420 }
421
422 /*
423 * Let the dispatch run unlocked, then, interlock against the
424 * callback before checking if the operation completed and going
425 * to sleep. This insures drivers don't inherit our lock which
426 * results in a lock order reversal between crypto_dispatch forced
427 * entry and the crypto_done callback into us.
428 */
429 error = crypto_dispatch(crp);
430 mtx_lock(&cse->lock);
431 if (error == 0 && (crp->crp_flags & CRYPTO_F_DONE) == 0)
432 error = msleep(crp, &cse->lock, PWAIT, "crydev", 0);
433 mtx_unlock(&cse->lock);
434
435 if (error != 0)
436 goto bail;
437
438 if (crp->crp_etype != 0) {
439 error = crp->crp_etype;
440 goto bail;
441 }
442
443 if (cse->error) {
444 error = cse->error;
445 goto bail;
446 }
447
448 if (cop->dst &&
449 (error = copyout(cse->uio.uio_iov[0].iov_base, cop->dst, cop->len)))
450 goto bail;
451
452 if (cop->mac &&
453 (error = copyout(crp->crp_mac, cop->mac, cse->thash->authsize)))
454 goto bail;
455
456 bail:
457 if (crp)
458 crypto_freereq(crp);
459 if (cse->uio.uio_iov[0].iov_base)
460 free(cse->uio.uio_iov[0].iov_base, M_XDATA);
461
462 return (error);
463 }
464
465 static int
466 cryptodev_cb(void *op)
467 {
468 struct cryptop *crp = (struct cryptop *) op;
469 struct csession *cse = (struct csession *)crp->crp_opaque;
470
471 cse->error = crp->crp_etype;
472 if (crp->crp_etype == EAGAIN)
473 return crypto_dispatch(crp);
474 mtx_lock(&cse->lock);
475 wakeup_one(crp);
476 mtx_unlock(&cse->lock);
477 return (0);
478 }
479
480 static int
481 cryptodevkey_cb(void *op)
482 {
483 struct cryptkop *krp = (struct cryptkop *) op;
484
485 wakeup(krp);
486 return (0);
487 }
488
489 static int
490 cryptodev_key(struct crypt_kop *kop)
491 {
492 struct cryptkop *krp = NULL;
493 int error = EINVAL;
494 int in, out, size, i;
495
496 if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
497 return (EFBIG);
498 }
499
500 in = kop->crk_iparams;
501 out = kop->crk_oparams;
502 switch (kop->crk_op) {
503 case CRK_MOD_EXP:
504 if (in == 3 && out == 1)
505 break;
506 return (EINVAL);
507 case CRK_MOD_EXP_CRT:
508 if (in == 6 && out == 1)
509 break;
510 return (EINVAL);
511 case CRK_DSA_SIGN:
512 if (in == 5 && out == 2)
513 break;
514 return (EINVAL);
515 case CRK_DSA_VERIFY:
516 if (in == 7 && out == 0)
517 break;
518 return (EINVAL);
519 case CRK_DH_COMPUTE_KEY:
520 if (in == 3 && out == 1)
521 break;
522 return (EINVAL);
523 default:
524 return (EINVAL);
525 }
526
527 krp = (struct cryptkop *)malloc(sizeof *krp, M_XDATA, M_WAITOK);
528 if (!krp)
529 return (ENOMEM);
530 bzero(krp, sizeof *krp);
531 krp->krp_op = kop->crk_op;
532 krp->krp_status = kop->crk_status;
533 krp->krp_iparams = kop->crk_iparams;
534 krp->krp_oparams = kop->crk_oparams;
535 krp->krp_status = 0;
536 krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb;
537
538 for (i = 0; i < CRK_MAXPARAM; i++)
539 krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
540 for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
541 size = (krp->krp_param[i].crp_nbits + 7) / 8;
542 if (size == 0)
543 continue;
544 MALLOC(krp->krp_param[i].crp_p, caddr_t, size, M_XDATA, M_WAITOK);
545 if (i >= krp->krp_iparams)
546 continue;
547 error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size);
548 if (error)
549 goto fail;
550 }
551
552 error = crypto_kdispatch(krp);
553 if (error)
554 goto fail;
555 error = tsleep(krp, PSOCK, "crydev", 0);
556 if (error) {
557 /* XXX can this happen? if so, how do we recover? */
558 goto fail;
559 }
560
561 if (krp->krp_status != 0) {
562 error = krp->krp_status;
563 goto fail;
564 }
565
566 for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
567 size = (krp->krp_param[i].crp_nbits + 7) / 8;
568 if (size == 0)
569 continue;
570 error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size);
571 if (error)
572 goto fail;
573 }
574
575 fail:
576 if (krp) {
577 kop->crk_status = krp->krp_status;
578 for (i = 0; i < CRK_MAXPARAM; i++) {
579 if (krp->krp_param[i].crp_p)
580 FREE(krp->krp_param[i].crp_p, M_XDATA);
581 }
582 free(krp, M_XDATA);
583 }
584 return (error);
585 }
586
587 /* ARGSUSED */
588 static int
589 cryptof_poll(
590 struct file *fp,
591 int events,
592 struct ucred *active_cred,
593 struct thread *td)
594 {
595
596 return (0);
597 }
598
599 /* ARGSUSED */
600 static int
601 cryptof_kqfilter(struct file *fp, struct knote *kn)
602 {
603
604 return (0);
605 }
606
607 /* ARGSUSED */
608 static int
609 cryptof_stat(
610 struct file *fp,
611 struct stat *sb,
612 struct ucred *active_cred,
613 struct thread *td)
614 {
615
616 return (EOPNOTSUPP);
617 }
618
619 /* ARGSUSED */
620 static int
621 cryptof_close(struct file *fp, struct thread *td)
622 {
623 struct fcrypt *fcr = fp->f_data;
624 struct csession *cse;
625
626 while ((cse = TAILQ_FIRST(&fcr->csessions))) {
627 TAILQ_REMOVE(&fcr->csessions, cse, next);
628 (void)csefree(cse);
629 }
630 FREE(fcr, M_XDATA);
631 fp->f_data = NULL;
632 return 0;
633 }
634
635 static struct csession *
636 csefind(struct fcrypt *fcr, u_int ses)
637 {
638 struct csession *cse;
639
640 TAILQ_FOREACH(cse, &fcr->csessions, next)
641 if (cse->ses == ses)
642 return (cse);
643 return (NULL);
644 }
645
646 static int
647 csedelete(struct fcrypt *fcr, struct csession *cse_del)
648 {
649 struct csession *cse;
650
651 TAILQ_FOREACH(cse, &fcr->csessions, next) {
652 if (cse == cse_del) {
653 TAILQ_REMOVE(&fcr->csessions, cse, next);
654 return (1);
655 }
656 }
657 return (0);
658 }
659
660 static struct csession *
661 cseadd(struct fcrypt *fcr, struct csession *cse)
662 {
663 TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
664 cse->ses = fcr->sesn++;
665 return (cse);
666 }
667
668 struct csession *
669 csecreate(struct fcrypt *fcr, u_int64_t sid, caddr_t key, u_int64_t keylen,
670 caddr_t mackey, u_int64_t mackeylen, u_int32_t cipher, u_int32_t mac,
671 struct enc_xform *txform, struct auth_hash *thash)
672 {
673 struct csession *cse;
674
675 #ifdef INVARIANTS
676 /* NB: required when mtx_init is built with INVARIANTS */
677 MALLOC(cse, struct csession *, sizeof(struct csession),
678 M_XDATA, M_NOWAIT | M_ZERO);
679 #else
680 MALLOC(cse, struct csession *, sizeof(struct csession),
681 M_XDATA, M_NOWAIT);
682 #endif
683 if (cse == NULL)
684 return NULL;
685 mtx_init(&cse->lock, "cryptodev", "crypto session lock", MTX_DEF);
686 cse->key = key;
687 cse->keylen = keylen/8;
688 cse->mackey = mackey;
689 cse->mackeylen = mackeylen/8;
690 cse->sid = sid;
691 cse->cipher = cipher;
692 cse->mac = mac;
693 cse->txform = txform;
694 cse->thash = thash;
695 cseadd(fcr, cse);
696 return (cse);
697 }
698
699 static int
700 csefree(struct csession *cse)
701 {
702 int error;
703
704 error = crypto_freesession(cse->sid);
705 mtx_destroy(&cse->lock);
706 if (cse->key)
707 FREE(cse->key, M_XDATA);
708 if (cse->mackey)
709 FREE(cse->mackey, M_XDATA);
710 FREE(cse, M_XDATA);
711 return (error);
712 }
713
714 static int
715 cryptoopen(struct cdev *dev, int oflags, int devtype, struct thread *td)
716 {
717 return (0);
718 }
719
720 static int
721 cryptoread(struct cdev *dev, struct uio *uio, int ioflag)
722 {
723 return (EIO);
724 }
725
726 static int
727 cryptowrite(struct cdev *dev, struct uio *uio, int ioflag)
728 {
729 return (EIO);
730 }
731
732 static int
733 cryptoioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
734 {
735 struct file *f;
736 struct fcrypt *fcr;
737 int fd, error;
738
739 switch (cmd) {
740 case CRIOGET:
741 MALLOC(fcr, struct fcrypt *,
742 sizeof(struct fcrypt), M_XDATA, M_WAITOK);
743 TAILQ_INIT(&fcr->csessions);
744 fcr->sesn = 0;
745
746 error = falloc(td, &f, &fd);
747
748 if (error) {
749 FREE(fcr, M_XDATA);
750 return (error);
751 }
752 /* falloc automatically provides an extra reference to 'f'. */
753 f->f_flag = FREAD | FWRITE;
754 f->f_type = DTYPE_CRYPTO;
755 f->f_ops = &cryptofops;
756 f->f_data = fcr;
757 *(u_int32_t *)data = fd;
758 fdrop(f, td);
759 break;
760 default:
761 error = EINVAL;
762 break;
763 }
764 return (error);
765 }
766
767 #define CRYPTO_MAJOR 70 /* from openbsd */
768 static struct cdevsw crypto_cdevsw = {
769 .d_version = D_VERSION,
770 .d_flags = D_NEEDGIANT,
771 .d_open = cryptoopen,
772 .d_read = cryptoread,
773 .d_write = cryptowrite,
774 .d_ioctl = cryptoioctl,
775 .d_name = "crypto",
776 .d_maj = CRYPTO_MAJOR,
777 };
778 static struct cdev *crypto_dev;
779
780 /*
781 * Initialization code, both for static and dynamic loading.
782 */
783 static int
784 cryptodev_modevent(module_t mod, int type, void *unused)
785 {
786 switch (type) {
787 case MOD_LOAD:
788 if (bootverbose)
789 printf("crypto: <crypto device>\n");
790 crypto_dev = make_dev(&crypto_cdevsw, 0,
791 UID_ROOT, GID_WHEEL, 0666,
792 "crypto");
793 return 0;
794 case MOD_UNLOAD:
795 /*XXX disallow if active sessions */
796 destroy_dev(crypto_dev);
797 return 0;
798 }
799 return EINVAL;
800 }
801
802 static moduledata_t cryptodev_mod = {
803 "cryptodev",
804 cryptodev_modevent,
805 0
806 };
807 MODULE_VERSION(cryptodev, 1);
808 DECLARE_MODULE(cryptodev, cryptodev_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
809 MODULE_DEPEND(cryptodev, crypto, 1, 1, 1);
Cache object: b0beee59b89d3c22c8cd0c770600bd95
|