cryptodev.c revision 158703
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: head/sys/opencrypto/cryptodev.c 158703 2006-05-17 18:24:17Z pjd $"); 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 57struct 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 81struct fcrypt { 82 TAILQ_HEAD(csessionlist, csession) csessions; 83 int sesn; 84}; 85 86static int cryptof_rw(struct file *fp, struct uio *uio, 87 struct ucred *cred, int flags, struct thread *); 88static int cryptof_ioctl(struct file *, u_long, void *, 89 struct ucred *, struct thread *); 90static int cryptof_poll(struct file *, int, struct ucred *, struct thread *); 91static int cryptof_kqfilter(struct file *, struct knote *); 92static int cryptof_stat(struct file *, struct stat *, 93 struct ucred *, struct thread *); 94static int cryptof_close(struct file *, struct thread *); 95 96static 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 106static struct csession *csefind(struct fcrypt *, u_int); 107static int csedelete(struct fcrypt *, struct csession *); 108static struct csession *cseadd(struct fcrypt *, struct csession *); 109static 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 *); 112static int csefree(struct csession *); 113 114static int cryptodev_op(struct csession *, struct crypt_op *, 115 struct ucred *, struct thread *td); 116static int cryptodev_key(struct crypt_kop *); 117 118static int 119cryptof_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 */ 131static int 132cryptof_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; 194 break; 195 case CRYPTO_SHA1_HMAC: 196 thash = &auth_hash_hmac_sha1; 197 break; 198 case CRYPTO_SHA2_256_HMAC: 199 thash = &auth_hash_hmac_sha2_256; 200 break; 201 case CRYPTO_SHA2_384_HMAC: 202 thash = &auth_hash_hmac_sha2_384; 203 break; 204 case CRYPTO_SHA2_512_HMAC: 205 thash = &auth_hash_hmac_sha2_512; 206 break; 207 case CRYPTO_RIPEMD160_HMAC: 208 thash = &auth_hash_hmac_ripemd_160; 209 break; 210#ifdef notdef 211 case CRYPTO_MD5: 212 thash = &auth_hash_md5; 213 break; 214 case CRYPTO_SHA1: 215 thash = &auth_hash_sha1; 216 break; 217#endif 218 case CRYPTO_NULL_HMAC: 219 thash = &auth_hash_null; 220 break; 221 default: 222 mtx_unlock(&Giant); 223 return (EINVAL); 224 } 225 226 bzero(&crie, sizeof(crie)); 227 bzero(&cria, sizeof(cria)); 228 229 if (txform) { 230 crie.cri_alg = txform->type; 231 crie.cri_klen = sop->keylen * 8; 232 if (sop->keylen > txform->maxkey || 233 sop->keylen < txform->minkey) { 234 error = EINVAL; 235 goto bail; 236 } 237 238 MALLOC(crie.cri_key, u_int8_t *, 239 crie.cri_klen / 8, M_XDATA, M_WAITOK); 240 if ((error = copyin(sop->key, crie.cri_key, 241 crie.cri_klen / 8))) 242 goto bail; 243 if (thash) 244 crie.cri_next = &cria; 245 } 246 247 if (thash) { 248 cria.cri_alg = thash->type; 249 cria.cri_klen = sop->mackeylen * 8; 250 if (sop->mackeylen != thash->keysize) { 251 error = EINVAL; 252 goto bail; 253 } 254 255 if (cria.cri_klen) { 256 MALLOC(cria.cri_key, u_int8_t *, 257 cria.cri_klen / 8, M_XDATA, M_WAITOK); 258 if ((error = copyin(sop->mackey, cria.cri_key, 259 cria.cri_klen / 8))) 260 goto bail; 261 } 262 } 263 264 error = crypto_newsession(&sid, (txform ? &crie : &cria), 1); 265 if (error) { 266 if (crypto_devallowsoft) { 267 error = crypto_newsession(&sid, 268 (txform ? &crie : &cria), 0); 269 } 270 if (error) 271 goto bail; 272 } 273 274 cse = csecreate(fcr, sid, crie.cri_key, crie.cri_klen, 275 cria.cri_key, cria.cri_klen, sop->cipher, sop->mac, txform, 276 thash); 277 278 if (cse == NULL) { 279 crypto_freesession(sid); 280 error = EINVAL; 281 goto bail; 282 } 283 sop->ses = cse->ses; 284 285bail: 286 if (error) { 287 if (crie.cri_key) 288 FREE(crie.cri_key, M_XDATA); 289 if (cria.cri_key) 290 FREE(cria.cri_key, M_XDATA); 291 } 292 break; 293 case CIOCFSESSION: 294 ses = *(u_int32_t *)data; 295 cse = csefind(fcr, ses); 296 if (cse == NULL) { 297 mtx_unlock(&Giant); 298 return (EINVAL); 299 } 300 csedelete(fcr, cse); 301 error = csefree(cse); 302 break; 303 case CIOCCRYPT: 304 cop = (struct crypt_op *)data; 305 cse = csefind(fcr, cop->ses); 306 if (cse == NULL) { 307 mtx_unlock(&Giant); 308 return (EINVAL); 309 } 310 error = cryptodev_op(cse, cop, active_cred, td); 311 break; 312 case CIOCKEY: 313 error = cryptodev_key((struct crypt_kop *)data); 314 break; 315 case CIOCASYMFEAT: 316 error = crypto_getfeat((int *)data); 317 break; 318 default: 319 error = EINVAL; 320 } 321 mtx_unlock(&Giant); 322 return (error); 323} 324 325static int cryptodev_cb(void *); 326 327 328static int 329cryptodev_op( 330 struct csession *cse, 331 struct crypt_op *cop, 332 struct ucred *active_cred, 333 struct thread *td) 334{ 335 struct cryptop *crp = NULL; 336 struct cryptodesc *crde = NULL, *crda = NULL; 337 int error; 338 339 if (cop->len > 256*1024-4) 340 return (E2BIG); 341 342 if (cse->txform) { 343 if (cop->len == 0 || (cop->len % cse->txform->blocksize) != 0) 344 return (EINVAL); 345 } 346 347 cse->uio.uio_iov = &cse->iovec; 348 cse->uio.uio_iovcnt = 1; 349 cse->uio.uio_offset = 0; 350 cse->uio.uio_resid = cop->len; 351 cse->uio.uio_segflg = UIO_SYSSPACE; 352 cse->uio.uio_rw = UIO_WRITE; 353 cse->uio.uio_td = td; 354 cse->uio.uio_iov[0].iov_len = cop->len; 355 cse->uio.uio_iov[0].iov_base = malloc(cop->len, M_XDATA, M_WAITOK); 356 357 crp = crypto_getreq((cse->txform != NULL) + (cse->thash != NULL)); 358 if (crp == NULL) { 359 error = ENOMEM; 360 goto bail; 361 } 362 363 if (cse->thash) { 364 crda = crp->crp_desc; 365 if (cse->txform) 366 crde = crda->crd_next; 367 } else { 368 if (cse->txform) 369 crde = crp->crp_desc; 370 else { 371 error = EINVAL; 372 goto bail; 373 } 374 } 375 376 if ((error = copyin(cop->src, cse->uio.uio_iov[0].iov_base, cop->len))) 377 goto bail; 378 379 if (crda) { 380 crda->crd_skip = 0; 381 crda->crd_len = cop->len; 382 crda->crd_inject = 0; /* ??? */ 383 384 crda->crd_alg = cse->mac; 385 crda->crd_key = cse->mackey; 386 crda->crd_klen = cse->mackeylen * 8; 387 } 388 389 if (crde) { 390 if (cop->op == COP_ENCRYPT) 391 crde->crd_flags |= CRD_F_ENCRYPT; 392 else 393 crde->crd_flags &= ~CRD_F_ENCRYPT; 394 crde->crd_len = cop->len; 395 crde->crd_inject = 0; 396 397 crde->crd_alg = cse->cipher; 398 crde->crd_key = cse->key; 399 crde->crd_klen = cse->keylen * 8; 400 } 401 402 crp->crp_ilen = cop->len; 403 crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM 404 | (cop->flags & COP_F_BATCH); 405 crp->crp_buf = (caddr_t)&cse->uio; 406 crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_cb; 407 crp->crp_sid = cse->sid; 408 crp->crp_opaque = (void *)cse; 409 410 if (cop->iv) { 411 if (crde == NULL) { 412 error = EINVAL; 413 goto bail; 414 } 415 if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */ 416 error = EINVAL; 417 goto bail; 418 } 419 if ((error = copyin(cop->iv, cse->tmp_iv, cse->txform->blocksize))) 420 goto bail; 421 bcopy(cse->tmp_iv, crde->crd_iv, cse->txform->blocksize); 422 crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT; 423 crde->crd_skip = 0; 424 } else if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */ 425 crde->crd_skip = 0; 426 } else if (crde) { 427 crde->crd_flags |= CRD_F_IV_PRESENT; 428 crde->crd_skip = cse->txform->blocksize; 429 crde->crd_len -= cse->txform->blocksize; 430 } 431 432 if (cop->mac) { 433 if (crda == NULL) { 434 error = EINVAL; 435 goto bail; 436 } 437 crp->crp_mac=cse->tmp_mac; 438 } 439 440 /* 441 * Let the dispatch run unlocked, then, interlock against the 442 * callback before checking if the operation completed and going 443 * to sleep. This insures drivers don't inherit our lock which 444 * results in a lock order reversal between crypto_dispatch forced 445 * entry and the crypto_done callback into us. 446 */ 447 error = crypto_dispatch(crp); 448 mtx_lock(&cse->lock); 449 if (error == 0 && (crp->crp_flags & CRYPTO_F_DONE) == 0) 450 error = msleep(crp, &cse->lock, PWAIT, "crydev", 0); 451 mtx_unlock(&cse->lock); 452 453 if (error != 0) 454 goto bail; 455 456 if (crp->crp_etype != 0) { 457 error = crp->crp_etype; 458 goto bail; 459 } 460 461 if (cse->error) { 462 error = cse->error; 463 goto bail; 464 } 465 466 if (cop->dst && 467 (error = copyout(cse->uio.uio_iov[0].iov_base, cop->dst, cop->len))) 468 goto bail; 469 470 if (cop->mac && 471 (error = copyout(crp->crp_mac, cop->mac, cse->thash->hashsize))) 472 goto bail; 473 474bail: 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 483static int 484cryptodev_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 498static int 499cryptodevkey_cb(void *op) 500{ 501 struct cryptkop *krp = (struct cryptkop *) op; 502 503 wakeup(krp); 504 return (0); 505} 506 507static int 508cryptodev_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 593fail: 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 */ 606static int 607cryptof_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 */ 618static int 619cryptof_kqfilter(struct file *fp, struct knote *kn) 620{ 621 622 return (0); 623} 624 625/* ARGSUSED */ 626static int 627cryptof_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 */ 638static int 639cryptof_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 653static struct csession * 654csefind(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 664static int 665csedelete(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 678static struct csession * 679cseadd(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 686struct csession * 687csecreate(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 717static int 718csefree(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 732static int 733cryptoopen(struct cdev *dev, int oflags, int devtype, struct thread *td) 734{ 735 return (0); 736} 737 738static int 739cryptoread(struct cdev *dev, struct uio *uio, int ioflag) 740{ 741 return (EIO); 742} 743 744static int 745cryptowrite(struct cdev *dev, struct uio *uio, int ioflag) 746{ 747 return (EIO); 748} 749 750static int 751cryptoioctl(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 785static 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}; 794static struct cdev *crypto_dev; 795 796/* 797 * Initialization code, both for static and dynamic loading. 798 */ 799static int 800cryptodev_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 818static moduledata_t cryptodev_mod = { 819 "cryptodev", 820 cryptodev_modevent, 821 0 822}; 823MODULE_VERSION(cryptodev, 1); 824DECLARE_MODULE(cryptodev, cryptodev_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 825MODULE_DEPEND(cryptodev, crypto, 1, 1, 1); 826MODULE_DEPEND(cryptodev, zlib, 1, 1, 1); 827