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