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