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