cryptodev.c revision 330129
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 * Copyright (c) 2002-2006 Sam Leffler, Errno Consulting 6 * Copyright (c) 2014 The FreeBSD Foundation 7 * All rights reserved. 8 * 9 * Portions of this software were developed by John-Mark Gurney 10 * under sponsorship of the FreeBSD Foundation and 11 * Rubicon Communications, LLC (Netgate). 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in the 21 * documentation and/or other materials provided with the distribution. 22 * 3. The name of the author may not be used to endorse or promote products 23 * derived from this software without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 * 36 * Effort sponsored in part by the Defense Advanced Research Projects 37 * Agency (DARPA) and Air Force Research Laboratory, Air Force 38 * Materiel Command, USAF, under agreement number F30602-01-2-0537. 39 */ 40 41#include <sys/cdefs.h> 42__FBSDID("$FreeBSD: stable/11/sys/opencrypto/cryptodev.c 330129 2018-02-28 21:41:52Z jhb $"); 43 44#include "opt_compat.h" 45 46#include <sys/param.h> 47#include <sys/systm.h> 48#include <sys/malloc.h> 49#include <sys/mbuf.h> 50#include <sys/lock.h> 51#include <sys/mutex.h> 52#include <sys/sysctl.h> 53#include <sys/file.h> 54#include <sys/filedesc.h> 55#include <sys/errno.h> 56#include <sys/uio.h> 57#include <sys/random.h> 58#include <sys/conf.h> 59#include <sys/kernel.h> 60#include <sys/module.h> 61#include <sys/fcntl.h> 62#include <sys/bus.h> 63#include <sys/user.h> 64#include <sys/sdt.h> 65 66#include <opencrypto/cryptodev.h> 67#include <opencrypto/xform.h> 68 69SDT_PROVIDER_DECLARE(opencrypto); 70 71SDT_PROBE_DEFINE1(opencrypto, dev, ioctl, error, "int"/*line number*/); 72 73#ifdef COMPAT_FREEBSD32 74#include <sys/mount.h> 75#include <compat/freebsd32/freebsd32.h> 76 77struct session_op32 { 78 u_int32_t cipher; 79 u_int32_t mac; 80 u_int32_t keylen; 81 u_int32_t key; 82 int mackeylen; 83 u_int32_t mackey; 84 u_int32_t ses; 85}; 86 87struct session2_op32 { 88 u_int32_t cipher; 89 u_int32_t mac; 90 u_int32_t keylen; 91 u_int32_t key; 92 int mackeylen; 93 u_int32_t mackey; 94 u_int32_t ses; 95 int crid; 96 int pad[4]; 97}; 98 99struct crypt_op32 { 100 u_int32_t ses; 101 u_int16_t op; 102 u_int16_t flags; 103 u_int len; 104 u_int32_t src, dst; 105 u_int32_t mac; 106 u_int32_t iv; 107}; 108 109struct crparam32 { 110 u_int32_t crp_p; 111 u_int crp_nbits; 112}; 113 114struct crypt_kop32 { 115 u_int crk_op; 116 u_int crk_status; 117 u_short crk_iparams; 118 u_short crk_oparams; 119 u_int crk_crid; 120 struct crparam32 crk_param[CRK_MAXPARAM]; 121}; 122 123struct cryptotstat32 { 124 struct timespec32 acc; 125 struct timespec32 min; 126 struct timespec32 max; 127 u_int32_t count; 128}; 129 130struct cryptostats32 { 131 u_int32_t cs_ops; 132 u_int32_t cs_errs; 133 u_int32_t cs_kops; 134 u_int32_t cs_kerrs; 135 u_int32_t cs_intrs; 136 u_int32_t cs_rets; 137 u_int32_t cs_blocks; 138 u_int32_t cs_kblocks; 139 struct cryptotstat32 cs_invoke; 140 struct cryptotstat32 cs_done; 141 struct cryptotstat32 cs_cb; 142 struct cryptotstat32 cs_finis; 143}; 144 145#define CIOCGSESSION32 _IOWR('c', 101, struct session_op32) 146#define CIOCCRYPT32 _IOWR('c', 103, struct crypt_op32) 147#define CIOCKEY32 _IOWR('c', 104, struct crypt_kop32) 148#define CIOCGSESSION232 _IOWR('c', 106, struct session2_op32) 149#define CIOCKEY232 _IOWR('c', 107, struct crypt_kop32) 150 151static void 152session_op_from_32(const struct session_op32 *from, struct session_op *to) 153{ 154 155 CP(*from, *to, cipher); 156 CP(*from, *to, mac); 157 CP(*from, *to, keylen); 158 PTRIN_CP(*from, *to, key); 159 CP(*from, *to, mackeylen); 160 PTRIN_CP(*from, *to, mackey); 161 CP(*from, *to, ses); 162} 163 164static void 165session2_op_from_32(const struct session2_op32 *from, struct session2_op *to) 166{ 167 168 session_op_from_32((const struct session_op32 *)from, 169 (struct session_op *)to); 170 CP(*from, *to, crid); 171} 172 173static void 174session_op_to_32(const struct session_op *from, struct session_op32 *to) 175{ 176 177 CP(*from, *to, cipher); 178 CP(*from, *to, mac); 179 CP(*from, *to, keylen); 180 PTROUT_CP(*from, *to, key); 181 CP(*from, *to, mackeylen); 182 PTROUT_CP(*from, *to, mackey); 183 CP(*from, *to, ses); 184} 185 186static void 187session2_op_to_32(const struct session2_op *from, struct session2_op32 *to) 188{ 189 190 session_op_to_32((const struct session_op *)from, 191 (struct session_op32 *)to); 192 CP(*from, *to, crid); 193} 194 195static void 196crypt_op_from_32(const struct crypt_op32 *from, struct crypt_op *to) 197{ 198 199 CP(*from, *to, ses); 200 CP(*from, *to, op); 201 CP(*from, *to, flags); 202 CP(*from, *to, len); 203 PTRIN_CP(*from, *to, src); 204 PTRIN_CP(*from, *to, dst); 205 PTRIN_CP(*from, *to, mac); 206 PTRIN_CP(*from, *to, iv); 207} 208 209static void 210crypt_op_to_32(const struct crypt_op *from, struct crypt_op32 *to) 211{ 212 213 CP(*from, *to, ses); 214 CP(*from, *to, op); 215 CP(*from, *to, flags); 216 CP(*from, *to, len); 217 PTROUT_CP(*from, *to, src); 218 PTROUT_CP(*from, *to, dst); 219 PTROUT_CP(*from, *to, mac); 220 PTROUT_CP(*from, *to, iv); 221} 222 223static void 224crparam_from_32(const struct crparam32 *from, struct crparam *to) 225{ 226 227 PTRIN_CP(*from, *to, crp_p); 228 CP(*from, *to, crp_nbits); 229} 230 231static void 232crparam_to_32(const struct crparam *from, struct crparam32 *to) 233{ 234 235 PTROUT_CP(*from, *to, crp_p); 236 CP(*from, *to, crp_nbits); 237} 238 239static void 240crypt_kop_from_32(const struct crypt_kop32 *from, struct crypt_kop *to) 241{ 242 int i; 243 244 CP(*from, *to, crk_op); 245 CP(*from, *to, crk_status); 246 CP(*from, *to, crk_iparams); 247 CP(*from, *to, crk_oparams); 248 CP(*from, *to, crk_crid); 249 for (i = 0; i < CRK_MAXPARAM; i++) 250 crparam_from_32(&from->crk_param[i], &to->crk_param[i]); 251} 252 253static void 254crypt_kop_to_32(const struct crypt_kop *from, struct crypt_kop32 *to) 255{ 256 int i; 257 258 CP(*from, *to, crk_op); 259 CP(*from, *to, crk_status); 260 CP(*from, *to, crk_iparams); 261 CP(*from, *to, crk_oparams); 262 CP(*from, *to, crk_crid); 263 for (i = 0; i < CRK_MAXPARAM; i++) 264 crparam_to_32(&from->crk_param[i], &to->crk_param[i]); 265} 266#endif 267 268struct csession { 269 TAILQ_ENTRY(csession) next; 270 u_int64_t sid; 271 u_int32_t ses; 272 struct mtx lock; /* for op submission */ 273 274 u_int32_t cipher; 275 struct enc_xform *txform; 276 u_int32_t mac; 277 struct auth_hash *thash; 278 279 caddr_t key; 280 int keylen; 281 282 caddr_t mackey; 283 int mackeylen; 284 285 struct iovec iovec; 286 struct uio uio; 287 int error; 288}; 289 290struct fcrypt { 291 TAILQ_HEAD(csessionlist, csession) csessions; 292 int sesn; 293}; 294 295static int cryptof_ioctl(struct file *, u_long, void *, 296 struct ucred *, struct thread *); 297static int cryptof_stat(struct file *, struct stat *, 298 struct ucred *, struct thread *); 299static int cryptof_close(struct file *, struct thread *); 300static int cryptof_fill_kinfo(struct file *, struct kinfo_file *, 301 struct filedesc *); 302 303static struct fileops cryptofops = { 304 .fo_read = invfo_rdwr, 305 .fo_write = invfo_rdwr, 306 .fo_truncate = invfo_truncate, 307 .fo_ioctl = cryptof_ioctl, 308 .fo_poll = invfo_poll, 309 .fo_kqfilter = invfo_kqfilter, 310 .fo_stat = cryptof_stat, 311 .fo_close = cryptof_close, 312 .fo_chmod = invfo_chmod, 313 .fo_chown = invfo_chown, 314 .fo_sendfile = invfo_sendfile, 315 .fo_fill_kinfo = cryptof_fill_kinfo, 316}; 317 318static struct csession *csefind(struct fcrypt *, u_int); 319static int csedelete(struct fcrypt *, struct csession *); 320static struct csession *cseadd(struct fcrypt *, struct csession *); 321static struct csession *csecreate(struct fcrypt *, u_int64_t, caddr_t, 322 u_int64_t, caddr_t, u_int64_t, u_int32_t, u_int32_t, struct enc_xform *, 323 struct auth_hash *); 324static int csefree(struct csession *); 325 326static int cryptodev_op(struct csession *, struct crypt_op *, 327 struct ucred *, struct thread *td); 328static int cryptodev_aead(struct csession *, struct crypt_aead *, 329 struct ucred *, struct thread *); 330static int cryptodev_key(struct crypt_kop *); 331static int cryptodev_find(struct crypt_find_op *); 332 333/* 334 * Check a crypto identifier to see if it requested 335 * a software device/driver. This can be done either 336 * by device name/class or through search constraints. 337 */ 338static int 339checkforsoftware(int *cridp) 340{ 341 int crid; 342 343 crid = *cridp; 344 345 if (!crypto_devallowsoft) { 346 if (crid & CRYPTOCAP_F_SOFTWARE) { 347 if (crid & CRYPTOCAP_F_HARDWARE) { 348 *cridp = CRYPTOCAP_F_HARDWARE; 349 return 0; 350 } 351 return EINVAL; 352 } 353 if ((crid & CRYPTOCAP_F_HARDWARE) == 0 && 354 (crypto_getcaps(crid) & CRYPTOCAP_F_HARDWARE) == 0) 355 return EINVAL; 356 } 357 return 0; 358} 359 360/* ARGSUSED */ 361static int 362cryptof_ioctl( 363 struct file *fp, 364 u_long cmd, 365 void *data, 366 struct ucred *active_cred, 367 struct thread *td) 368{ 369#define SES2(p) ((struct session2_op *)p) 370 struct cryptoini cria, crie; 371 struct fcrypt *fcr = fp->f_data; 372 struct csession *cse; 373 struct session_op *sop; 374 struct crypt_op *cop; 375 struct crypt_aead *caead; 376 struct enc_xform *txform = NULL; 377 struct auth_hash *thash = NULL; 378 struct crypt_kop *kop; 379 u_int64_t sid; 380 u_int32_t ses; 381 int error = 0, crid; 382#ifdef COMPAT_FREEBSD32 383 struct session2_op sopc; 384 struct crypt_op copc; 385 struct crypt_kop kopc; 386#endif 387 388 switch (cmd) { 389 case CIOCGSESSION: 390 case CIOCGSESSION2: 391#ifdef COMPAT_FREEBSD32 392 case CIOCGSESSION32: 393 case CIOCGSESSION232: 394 if (cmd == CIOCGSESSION32) { 395 session_op_from_32(data, (struct session_op *)&sopc); 396 sop = (struct session_op *)&sopc; 397 } else if (cmd == CIOCGSESSION232) { 398 session2_op_from_32(data, &sopc); 399 sop = (struct session_op *)&sopc; 400 } else 401#endif 402 sop = (struct session_op *)data; 403 switch (sop->cipher) { 404 case 0: 405 break; 406 case CRYPTO_DES_CBC: 407 txform = &enc_xform_des; 408 break; 409 case CRYPTO_3DES_CBC: 410 txform = &enc_xform_3des; 411 break; 412 case CRYPTO_BLF_CBC: 413 txform = &enc_xform_blf; 414 break; 415 case CRYPTO_CAST_CBC: 416 txform = &enc_xform_cast5; 417 break; 418 case CRYPTO_SKIPJACK_CBC: 419 txform = &enc_xform_skipjack; 420 break; 421 case CRYPTO_AES_CBC: 422 txform = &enc_xform_rijndael128; 423 break; 424 case CRYPTO_AES_XTS: 425 txform = &enc_xform_aes_xts; 426 break; 427 case CRYPTO_NULL_CBC: 428 txform = &enc_xform_null; 429 break; 430 case CRYPTO_ARC4: 431 txform = &enc_xform_arc4; 432 break; 433 case CRYPTO_CAMELLIA_CBC: 434 txform = &enc_xform_camellia; 435 break; 436 case CRYPTO_AES_ICM: 437 txform = &enc_xform_aes_icm; 438 break; 439 case CRYPTO_AES_NIST_GCM_16: 440 txform = &enc_xform_aes_nist_gcm; 441 break; 442 443 default: 444 CRYPTDEB("invalid cipher"); 445 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 446 return (EINVAL); 447 } 448 449 switch (sop->mac) { 450 case 0: 451 break; 452 case CRYPTO_MD5_HMAC: 453 thash = &auth_hash_hmac_md5; 454 break; 455 case CRYPTO_SHA1_HMAC: 456 thash = &auth_hash_hmac_sha1; 457 break; 458 case CRYPTO_SHA2_256_HMAC: 459 thash = &auth_hash_hmac_sha2_256; 460 break; 461 case CRYPTO_SHA2_384_HMAC: 462 thash = &auth_hash_hmac_sha2_384; 463 break; 464 case CRYPTO_SHA2_512_HMAC: 465 thash = &auth_hash_hmac_sha2_512; 466 break; 467 case CRYPTO_RIPEMD160_HMAC: 468 thash = &auth_hash_hmac_ripemd_160; 469 break; 470 case CRYPTO_AES_128_NIST_GMAC: 471 thash = &auth_hash_nist_gmac_aes_128; 472 break; 473 case CRYPTO_AES_192_NIST_GMAC: 474 thash = &auth_hash_nist_gmac_aes_192; 475 break; 476 case CRYPTO_AES_256_NIST_GMAC: 477 thash = &auth_hash_nist_gmac_aes_256; 478 break; 479 480#ifdef notdef 481 case CRYPTO_MD5: 482 thash = &auth_hash_md5; 483 break; 484 case CRYPTO_SHA1: 485 thash = &auth_hash_sha1; 486 break; 487#endif 488 case CRYPTO_NULL_HMAC: 489 thash = &auth_hash_null; 490 break; 491 default: 492 CRYPTDEB("invalid mac"); 493 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 494 return (EINVAL); 495 } 496 497 bzero(&crie, sizeof(crie)); 498 bzero(&cria, sizeof(cria)); 499 500 if (txform) { 501 crie.cri_alg = txform->type; 502 crie.cri_klen = sop->keylen * 8; 503 if (sop->keylen > txform->maxkey || 504 sop->keylen < txform->minkey) { 505 CRYPTDEB("invalid cipher parameters"); 506 error = EINVAL; 507 SDT_PROBE1(opencrypto, dev, ioctl, error, 508 __LINE__); 509 goto bail; 510 } 511 512 crie.cri_key = malloc(crie.cri_klen / 8, 513 M_XDATA, M_WAITOK); 514 if ((error = copyin(sop->key, crie.cri_key, 515 crie.cri_klen / 8))) { 516 CRYPTDEB("invalid key"); 517 SDT_PROBE1(opencrypto, dev, ioctl, error, 518 __LINE__); 519 goto bail; 520 } 521 if (thash) 522 crie.cri_next = &cria; 523 } 524 525 if (thash) { 526 cria.cri_alg = thash->type; 527 cria.cri_klen = sop->mackeylen * 8; 528 if (sop->mackeylen != thash->keysize) { 529 CRYPTDEB("invalid mac key length"); 530 error = EINVAL; 531 SDT_PROBE1(opencrypto, dev, ioctl, error, 532 __LINE__); 533 goto bail; 534 } 535 536 if (cria.cri_klen) { 537 cria.cri_key = malloc(cria.cri_klen / 8, 538 M_XDATA, M_WAITOK); 539 if ((error = copyin(sop->mackey, cria.cri_key, 540 cria.cri_klen / 8))) { 541 CRYPTDEB("invalid mac key"); 542 SDT_PROBE1(opencrypto, dev, ioctl, 543 error, __LINE__); 544 goto bail; 545 } 546 } 547 } 548 549 /* NB: CIOCGSESSION2 has the crid */ 550 if (cmd == CIOCGSESSION2 551#ifdef COMPAT_FREEBSD32 552 || cmd == CIOCGSESSION232 553#endif 554 ) { 555 crid = SES2(sop)->crid; 556 error = checkforsoftware(&crid); 557 if (error) { 558 CRYPTDEB("checkforsoftware"); 559 SDT_PROBE1(opencrypto, dev, ioctl, error, 560 __LINE__); 561 goto bail; 562 } 563 } else 564 crid = CRYPTOCAP_F_HARDWARE; 565 error = crypto_newsession(&sid, (txform ? &crie : &cria), crid); 566 if (error) { 567 CRYPTDEB("crypto_newsession"); 568 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 569 goto bail; 570 } 571 572 cse = csecreate(fcr, sid, crie.cri_key, crie.cri_klen, 573 cria.cri_key, cria.cri_klen, sop->cipher, sop->mac, txform, 574 thash); 575 576 if (cse == NULL) { 577 crypto_freesession(sid); 578 error = EINVAL; 579 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 580 CRYPTDEB("csecreate"); 581 goto bail; 582 } 583 sop->ses = cse->ses; 584 if (cmd == CIOCGSESSION2 585#ifdef COMPAT_FREEBSD32 586 || cmd == CIOCGSESSION232 587#endif 588 ) { 589 /* return hardware/driver id */ 590 SES2(sop)->crid = CRYPTO_SESID2HID(cse->sid); 591 } 592bail: 593 if (error) { 594 if (crie.cri_key) 595 free(crie.cri_key, M_XDATA); 596 if (cria.cri_key) 597 free(cria.cri_key, M_XDATA); 598 } 599#ifdef COMPAT_FREEBSD32 600 else { 601 if (cmd == CIOCGSESSION32) 602 session_op_to_32(sop, data); 603 else if (cmd == CIOCGSESSION232) 604 session2_op_to_32((struct session2_op *)sop, 605 data); 606 } 607#endif 608 break; 609 case CIOCFSESSION: 610 ses = *(u_int32_t *)data; 611 cse = csefind(fcr, ses); 612 if (cse == NULL) { 613 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 614 return (EINVAL); 615 } 616 csedelete(fcr, cse); 617 error = csefree(cse); 618 break; 619 case CIOCCRYPT: 620#ifdef COMPAT_FREEBSD32 621 case CIOCCRYPT32: 622 if (cmd == CIOCCRYPT32) { 623 cop = &copc; 624 crypt_op_from_32(data, cop); 625 } else 626#endif 627 cop = (struct crypt_op *)data; 628 cse = csefind(fcr, cop->ses); 629 if (cse == NULL) { 630 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 631 return (EINVAL); 632 } 633 error = cryptodev_op(cse, cop, active_cred, td); 634#ifdef COMPAT_FREEBSD32 635 if (error == 0 && cmd == CIOCCRYPT32) 636 crypt_op_to_32(cop, data); 637#endif 638 break; 639 case CIOCKEY: 640 case CIOCKEY2: 641#ifdef COMPAT_FREEBSD32 642 case CIOCKEY32: 643 case CIOCKEY232: 644#endif 645 if (!crypto_userasymcrypto) { 646 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 647 return (EPERM); /* XXX compat? */ 648 } 649#ifdef COMPAT_FREEBSD32 650 if (cmd == CIOCKEY32 || cmd == CIOCKEY232) { 651 kop = &kopc; 652 crypt_kop_from_32(data, kop); 653 } else 654#endif 655 kop = (struct crypt_kop *)data; 656 if (cmd == CIOCKEY 657#ifdef COMPAT_FREEBSD32 658 || cmd == CIOCKEY32 659#endif 660 ) { 661 /* NB: crypto core enforces s/w driver use */ 662 kop->crk_crid = 663 CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE; 664 } 665 mtx_lock(&Giant); 666 error = cryptodev_key(kop); 667 mtx_unlock(&Giant); 668#ifdef COMPAT_FREEBSD32 669 if (cmd == CIOCKEY32 || cmd == CIOCKEY232) 670 crypt_kop_to_32(kop, data); 671#endif 672 break; 673 case CIOCASYMFEAT: 674 if (!crypto_userasymcrypto) { 675 /* 676 * NB: if user asym crypto operations are 677 * not permitted return "no algorithms" 678 * so well-behaved applications will just 679 * fallback to doing them in software. 680 */ 681 *(int *)data = 0; 682 } else { 683 error = crypto_getfeat((int *)data); 684 if (error) 685 SDT_PROBE1(opencrypto, dev, ioctl, error, 686 __LINE__); 687 } 688 break; 689 case CIOCFINDDEV: 690 error = cryptodev_find((struct crypt_find_op *)data); 691 break; 692 case CIOCCRYPTAEAD: 693 caead = (struct crypt_aead *)data; 694 cse = csefind(fcr, caead->ses); 695 if (cse == NULL) { 696 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 697 return (EINVAL); 698 } 699 error = cryptodev_aead(cse, caead, active_cred, td); 700 break; 701 default: 702 error = EINVAL; 703 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 704 break; 705 } 706 return (error); 707#undef SES2 708} 709 710static int cryptodev_cb(void *); 711 712 713static int 714cryptodev_op( 715 struct csession *cse, 716 struct crypt_op *cop, 717 struct ucred *active_cred, 718 struct thread *td) 719{ 720 struct cryptop *crp = NULL; 721 struct cryptodesc *crde = NULL, *crda = NULL; 722 int error; 723 724 if (cop->len > 256*1024-4) { 725 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 726 return (E2BIG); 727 } 728 729 if (cse->txform) { 730 if (cop->len == 0 || (cop->len % cse->txform->blocksize) != 0) { 731 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 732 return (EINVAL); 733 } 734 } 735 736 cse->uio.uio_iov = &cse->iovec; 737 cse->uio.uio_iovcnt = 1; 738 cse->uio.uio_offset = 0; 739 cse->uio.uio_resid = cop->len; 740 cse->uio.uio_segflg = UIO_SYSSPACE; 741 cse->uio.uio_rw = UIO_WRITE; 742 cse->uio.uio_td = td; 743 cse->uio.uio_iov[0].iov_len = cop->len; 744 if (cse->thash) { 745 cse->uio.uio_iov[0].iov_len += cse->thash->hashsize; 746 cse->uio.uio_resid += cse->thash->hashsize; 747 } 748 cse->uio.uio_iov[0].iov_base = malloc(cse->uio.uio_iov[0].iov_len, 749 M_XDATA, M_WAITOK); 750 751 crp = crypto_getreq((cse->txform != NULL) + (cse->thash != NULL)); 752 if (crp == NULL) { 753 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 754 error = ENOMEM; 755 goto bail; 756 } 757 758 if (cse->thash) { 759 crda = crp->crp_desc; 760 if (cse->txform) 761 crde = crda->crd_next; 762 } else { 763 if (cse->txform) 764 crde = crp->crp_desc; 765 else { 766 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 767 error = EINVAL; 768 goto bail; 769 } 770 } 771 772 if ((error = copyin(cop->src, cse->uio.uio_iov[0].iov_base, 773 cop->len))) { 774 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 775 goto bail; 776 } 777 778 if (crda) { 779 crda->crd_skip = 0; 780 crda->crd_len = cop->len; 781 crda->crd_inject = cop->len; 782 783 crda->crd_alg = cse->mac; 784 crda->crd_key = cse->mackey; 785 crda->crd_klen = cse->mackeylen * 8; 786 } 787 788 if (crde) { 789 if (cop->op == COP_ENCRYPT) 790 crde->crd_flags |= CRD_F_ENCRYPT; 791 else 792 crde->crd_flags &= ~CRD_F_ENCRYPT; 793 crde->crd_len = cop->len; 794 crde->crd_inject = 0; 795 796 crde->crd_alg = cse->cipher; 797 crde->crd_key = cse->key; 798 crde->crd_klen = cse->keylen * 8; 799 } 800 801 crp->crp_ilen = cop->len; 802 crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM 803 | (cop->flags & COP_F_BATCH); 804 crp->crp_buf = (caddr_t)&cse->uio; 805 crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_cb; 806 crp->crp_sid = cse->sid; 807 crp->crp_opaque = (void *)cse; 808 809 if (cop->iv) { 810 if (crde == NULL) { 811 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 812 error = EINVAL; 813 goto bail; 814 } 815 if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */ 816 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 817 error = EINVAL; 818 goto bail; 819 } 820 if ((error = copyin(cop->iv, crde->crd_iv, 821 cse->txform->blocksize))) { 822 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 823 goto bail; 824 } 825 crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT; 826 crde->crd_skip = 0; 827 } else if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */ 828 crde->crd_skip = 0; 829 } else if (crde) { 830 crde->crd_flags |= CRD_F_IV_PRESENT; 831 crde->crd_skip = cse->txform->blocksize; 832 crde->crd_len -= cse->txform->blocksize; 833 } 834 835 if (cop->mac && crda == NULL) { 836 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 837 error = EINVAL; 838 goto bail; 839 } 840 841again: 842 /* 843 * Let the dispatch run unlocked, then, interlock against the 844 * callback before checking if the operation completed and going 845 * to sleep. This insures drivers don't inherit our lock which 846 * results in a lock order reversal between crypto_dispatch forced 847 * entry and the crypto_done callback into us. 848 */ 849 error = crypto_dispatch(crp); 850 mtx_lock(&cse->lock); 851 if (error == 0 && (crp->crp_flags & CRYPTO_F_DONE) == 0) 852 error = msleep(crp, &cse->lock, PWAIT, "crydev", 0); 853 mtx_unlock(&cse->lock); 854 855 if (error != 0) { 856 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 857 goto bail; 858 } 859 860 if (crp->crp_etype == EAGAIN) { 861 crp->crp_etype = 0; 862 crp->crp_flags &= ~CRYPTO_F_DONE; 863 goto again; 864 } 865 866 if (crp->crp_etype != 0) { 867 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 868 error = crp->crp_etype; 869 goto bail; 870 } 871 872 if (cse->error) { 873 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 874 error = cse->error; 875 goto bail; 876 } 877 878 if (cop->dst && 879 (error = copyout(cse->uio.uio_iov[0].iov_base, cop->dst, 880 cop->len))) { 881 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 882 goto bail; 883 } 884 885 if (cop->mac && 886 (error = copyout((caddr_t)cse->uio.uio_iov[0].iov_base + cop->len, 887 cop->mac, cse->thash->hashsize))) { 888 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 889 goto bail; 890 } 891 892bail: 893 if (crp) 894 crypto_freereq(crp); 895 if (cse->uio.uio_iov[0].iov_base) 896 free(cse->uio.uio_iov[0].iov_base, M_XDATA); 897 898 return (error); 899} 900 901static int 902cryptodev_aead( 903 struct csession *cse, 904 struct crypt_aead *caead, 905 struct ucred *active_cred, 906 struct thread *td) 907{ 908 struct uio *uio; 909 struct cryptop *crp = NULL; 910 struct cryptodesc *crde = NULL, *crda = NULL; 911 int error; 912 913 if (caead->len > 256*1024-4 || caead->aadlen > 256*1024-4) { 914 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 915 return (E2BIG); 916 } 917 918 if (cse->txform == NULL || cse->thash == NULL || caead->tag == NULL || 919 (caead->len % cse->txform->blocksize) != 0) { 920 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 921 return (EINVAL); 922 } 923 924 uio = &cse->uio; 925 uio->uio_iov = &cse->iovec; 926 uio->uio_iovcnt = 1; 927 uio->uio_offset = 0; 928 uio->uio_resid = caead->aadlen + caead->len + cse->thash->hashsize; 929 uio->uio_segflg = UIO_SYSSPACE; 930 uio->uio_rw = UIO_WRITE; 931 uio->uio_td = td; 932 uio->uio_iov[0].iov_len = uio->uio_resid; 933 934 uio->uio_iov[0].iov_base = malloc(uio->uio_iov[0].iov_len, 935 M_XDATA, M_WAITOK); 936 937 crp = crypto_getreq(2); 938 if (crp == NULL) { 939 error = ENOMEM; 940 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 941 goto bail; 942 } 943 944 crda = crp->crp_desc; 945 crde = crda->crd_next; 946 947 if ((error = copyin(caead->aad, cse->uio.uio_iov[0].iov_base, 948 caead->aadlen))) { 949 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 950 goto bail; 951 } 952 953 if ((error = copyin(caead->src, (char *)cse->uio.uio_iov[0].iov_base + 954 caead->aadlen, caead->len))) { 955 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 956 goto bail; 957 } 958 959 crda->crd_skip = 0; 960 crda->crd_len = caead->aadlen; 961 crda->crd_inject = caead->aadlen + caead->len; 962 963 crda->crd_alg = cse->mac; 964 crda->crd_key = cse->mackey; 965 crda->crd_klen = cse->mackeylen * 8; 966 967 if (caead->op == COP_ENCRYPT) 968 crde->crd_flags |= CRD_F_ENCRYPT; 969 else 970 crde->crd_flags &= ~CRD_F_ENCRYPT; 971 crde->crd_skip = caead->aadlen; 972 crde->crd_len = caead->len; 973 crde->crd_inject = caead->aadlen; 974 975 crde->crd_alg = cse->cipher; 976 crde->crd_key = cse->key; 977 crde->crd_klen = cse->keylen * 8; 978 979 crp->crp_ilen = caead->aadlen + caead->len; 980 crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM 981 | (caead->flags & COP_F_BATCH); 982 crp->crp_buf = (caddr_t)&cse->uio.uio_iov; 983 crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_cb; 984 crp->crp_sid = cse->sid; 985 crp->crp_opaque = (void *)cse; 986 987 if (caead->iv) { 988 if (caead->ivlen > sizeof(crde->crd_iv)) { 989 error = EINVAL; 990 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 991 goto bail; 992 } 993 994 if ((error = copyin(caead->iv, crde->crd_iv, caead->ivlen))) { 995 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 996 goto bail; 997 } 998 crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT; 999 } else { 1000 crde->crd_flags |= CRD_F_IV_PRESENT; 1001 crde->crd_skip += cse->txform->blocksize; 1002 crde->crd_len -= cse->txform->blocksize; 1003 } 1004 1005 if ((error = copyin(caead->tag, (caddr_t)cse->uio.uio_iov[0].iov_base + 1006 caead->len + caead->aadlen, cse->thash->hashsize))) { 1007 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1008 goto bail; 1009 } 1010again: 1011 /* 1012 * Let the dispatch run unlocked, then, interlock against the 1013 * callback before checking if the operation completed and going 1014 * to sleep. This insures drivers don't inherit our lock which 1015 * results in a lock order reversal between crypto_dispatch forced 1016 * entry and the crypto_done callback into us. 1017 */ 1018 error = crypto_dispatch(crp); 1019 mtx_lock(&cse->lock); 1020 if (error == 0 && (crp->crp_flags & CRYPTO_F_DONE) == 0) 1021 error = msleep(crp, &cse->lock, PWAIT, "crydev", 0); 1022 mtx_unlock(&cse->lock); 1023 1024 if (error != 0) { 1025 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1026 goto bail; 1027 } 1028 1029 if (crp->crp_etype == EAGAIN) { 1030 crp->crp_etype = 0; 1031 crp->crp_flags &= ~CRYPTO_F_DONE; 1032 goto again; 1033 } 1034 1035 if (crp->crp_etype != 0) { 1036 error = crp->crp_etype; 1037 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1038 goto bail; 1039 } 1040 1041 if (cse->error) { 1042 error = cse->error; 1043 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1044 goto bail; 1045 } 1046 1047 if (caead->dst && (error = copyout( 1048 (caddr_t)cse->uio.uio_iov[0].iov_base + caead->aadlen, caead->dst, 1049 caead->len))) { 1050 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1051 goto bail; 1052 } 1053 1054 if ((error = copyout((caddr_t)cse->uio.uio_iov[0].iov_base + 1055 caead->aadlen + caead->len, caead->tag, cse->thash->hashsize))) { 1056 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1057 goto bail; 1058 } 1059 1060bail: 1061 crypto_freereq(crp); 1062 free(cse->uio.uio_iov[0].iov_base, M_XDATA); 1063 1064 return (error); 1065} 1066 1067static int 1068cryptodev_cb(void *op) 1069{ 1070 struct cryptop *crp = (struct cryptop *) op; 1071 struct csession *cse = (struct csession *)crp->crp_opaque; 1072 1073 mtx_lock(&cse->lock); 1074 cse->error = crp->crp_etype; 1075 wakeup_one(crp); 1076 mtx_unlock(&cse->lock); 1077 return (0); 1078} 1079 1080static int 1081cryptodevkey_cb(void *op) 1082{ 1083 struct cryptkop *krp = (struct cryptkop *) op; 1084 1085 wakeup_one(krp); 1086 return (0); 1087} 1088 1089static int 1090cryptodev_key(struct crypt_kop *kop) 1091{ 1092 struct cryptkop *krp = NULL; 1093 int error = EINVAL; 1094 int in, out, size, i; 1095 1096 if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) { 1097 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1098 return (EFBIG); 1099 } 1100 1101 in = kop->crk_iparams; 1102 out = kop->crk_oparams; 1103 switch (kop->crk_op) { 1104 case CRK_MOD_EXP: 1105 if (in == 3 && out == 1) 1106 break; 1107 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1108 return (EINVAL); 1109 case CRK_MOD_EXP_CRT: 1110 if (in == 6 && out == 1) 1111 break; 1112 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1113 return (EINVAL); 1114 case CRK_DSA_SIGN: 1115 if (in == 5 && out == 2) 1116 break; 1117 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1118 return (EINVAL); 1119 case CRK_DSA_VERIFY: 1120 if (in == 7 && out == 0) 1121 break; 1122 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1123 return (EINVAL); 1124 case CRK_DH_COMPUTE_KEY: 1125 if (in == 3 && out == 1) 1126 break; 1127 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1128 return (EINVAL); 1129 default: 1130 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1131 return (EINVAL); 1132 } 1133 1134 krp = (struct cryptkop *)malloc(sizeof *krp, M_XDATA, M_WAITOK|M_ZERO); 1135 if (!krp) { 1136 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1137 return (ENOMEM); 1138 } 1139 krp->krp_op = kop->crk_op; 1140 krp->krp_status = kop->crk_status; 1141 krp->krp_iparams = kop->crk_iparams; 1142 krp->krp_oparams = kop->crk_oparams; 1143 krp->krp_crid = kop->crk_crid; 1144 krp->krp_status = 0; 1145 krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb; 1146 1147 for (i = 0; i < CRK_MAXPARAM; i++) { 1148 if (kop->crk_param[i].crp_nbits > 65536) { 1149 /* Limit is the same as in OpenBSD */ 1150 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1151 goto fail; 1152 } 1153 krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits; 1154 } 1155 for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) { 1156 size = (krp->krp_param[i].crp_nbits + 7) / 8; 1157 if (size == 0) 1158 continue; 1159 krp->krp_param[i].crp_p = malloc(size, M_XDATA, M_WAITOK); 1160 if (i >= krp->krp_iparams) 1161 continue; 1162 error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size); 1163 if (error) { 1164 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1165 goto fail; 1166 } 1167 } 1168 1169 error = crypto_kdispatch(krp); 1170 if (error) { 1171 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1172 goto fail; 1173 } 1174 error = tsleep(krp, PSOCK, "crydev", 0); 1175 if (error) { 1176 /* XXX can this happen? if so, how do we recover? */ 1177 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1178 goto fail; 1179 } 1180 1181 kop->crk_crid = krp->krp_crid; /* device that did the work */ 1182 if (krp->krp_status != 0) { 1183 error = krp->krp_status; 1184 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1185 goto fail; 1186 } 1187 1188 for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) { 1189 size = (krp->krp_param[i].crp_nbits + 7) / 8; 1190 if (size == 0) 1191 continue; 1192 error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size); 1193 if (error) { 1194 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1195 goto fail; 1196 } 1197 } 1198 1199fail: 1200 if (krp) { 1201 kop->crk_status = krp->krp_status; 1202 for (i = 0; i < CRK_MAXPARAM; i++) { 1203 if (krp->krp_param[i].crp_p) 1204 free(krp->krp_param[i].crp_p, M_XDATA); 1205 } 1206 free(krp, M_XDATA); 1207 } 1208 return (error); 1209} 1210 1211static int 1212cryptodev_find(struct crypt_find_op *find) 1213{ 1214 device_t dev; 1215 size_t fnlen = sizeof find->name; 1216 1217 if (find->crid != -1) { 1218 dev = crypto_find_device_byhid(find->crid); 1219 if (dev == NULL) 1220 return (ENOENT); 1221 strncpy(find->name, device_get_nameunit(dev), fnlen); 1222 find->name[fnlen - 1] = '\x0'; 1223 } else { 1224 find->name[fnlen - 1] = '\x0'; 1225 find->crid = crypto_find_driver(find->name); 1226 if (find->crid == -1) 1227 return (ENOENT); 1228 } 1229 return (0); 1230} 1231 1232/* ARGSUSED */ 1233static int 1234cryptof_stat( 1235 struct file *fp, 1236 struct stat *sb, 1237 struct ucred *active_cred, 1238 struct thread *td) 1239{ 1240 1241 return (EOPNOTSUPP); 1242} 1243 1244/* ARGSUSED */ 1245static int 1246cryptof_close(struct file *fp, struct thread *td) 1247{ 1248 struct fcrypt *fcr = fp->f_data; 1249 struct csession *cse; 1250 1251 while ((cse = TAILQ_FIRST(&fcr->csessions))) { 1252 TAILQ_REMOVE(&fcr->csessions, cse, next); 1253 (void)csefree(cse); 1254 } 1255 free(fcr, M_XDATA); 1256 fp->f_data = NULL; 1257 return 0; 1258} 1259 1260static int 1261cryptof_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp) 1262{ 1263 1264 kif->kf_type = KF_TYPE_CRYPTO; 1265 return (0); 1266} 1267 1268static struct csession * 1269csefind(struct fcrypt *fcr, u_int ses) 1270{ 1271 struct csession *cse; 1272 1273 TAILQ_FOREACH(cse, &fcr->csessions, next) 1274 if (cse->ses == ses) 1275 return (cse); 1276 return (NULL); 1277} 1278 1279static int 1280csedelete(struct fcrypt *fcr, struct csession *cse_del) 1281{ 1282 struct csession *cse; 1283 1284 TAILQ_FOREACH(cse, &fcr->csessions, next) { 1285 if (cse == cse_del) { 1286 TAILQ_REMOVE(&fcr->csessions, cse, next); 1287 return (1); 1288 } 1289 } 1290 return (0); 1291} 1292 1293static struct csession * 1294cseadd(struct fcrypt *fcr, struct csession *cse) 1295{ 1296 TAILQ_INSERT_TAIL(&fcr->csessions, cse, next); 1297 cse->ses = fcr->sesn++; 1298 return (cse); 1299} 1300 1301struct csession * 1302csecreate(struct fcrypt *fcr, u_int64_t sid, caddr_t key, u_int64_t keylen, 1303 caddr_t mackey, u_int64_t mackeylen, u_int32_t cipher, u_int32_t mac, 1304 struct enc_xform *txform, struct auth_hash *thash) 1305{ 1306 struct csession *cse; 1307 1308 cse = malloc(sizeof(struct csession), M_XDATA, M_NOWAIT | M_ZERO); 1309 if (cse == NULL) 1310 return NULL; 1311 mtx_init(&cse->lock, "cryptodev", "crypto session lock", MTX_DEF); 1312 cse->key = key; 1313 cse->keylen = keylen/8; 1314 cse->mackey = mackey; 1315 cse->mackeylen = mackeylen/8; 1316 cse->sid = sid; 1317 cse->cipher = cipher; 1318 cse->mac = mac; 1319 cse->txform = txform; 1320 cse->thash = thash; 1321 cseadd(fcr, cse); 1322 return (cse); 1323} 1324 1325static int 1326csefree(struct csession *cse) 1327{ 1328 int error; 1329 1330 error = crypto_freesession(cse->sid); 1331 mtx_destroy(&cse->lock); 1332 if (cse->key) 1333 free(cse->key, M_XDATA); 1334 if (cse->mackey) 1335 free(cse->mackey, M_XDATA); 1336 free(cse, M_XDATA); 1337 return (error); 1338} 1339 1340static int 1341cryptoopen(struct cdev *dev, int oflags, int devtype, struct thread *td) 1342{ 1343 return (0); 1344} 1345 1346static int 1347cryptoread(struct cdev *dev, struct uio *uio, int ioflag) 1348{ 1349 return (EIO); 1350} 1351 1352static int 1353cryptowrite(struct cdev *dev, struct uio *uio, int ioflag) 1354{ 1355 return (EIO); 1356} 1357 1358static int 1359cryptoioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td) 1360{ 1361 struct file *f; 1362 struct fcrypt *fcr; 1363 int fd, error; 1364 1365 switch (cmd) { 1366 case CRIOGET: 1367 fcr = malloc(sizeof(struct fcrypt), M_XDATA, M_WAITOK); 1368 TAILQ_INIT(&fcr->csessions); 1369 fcr->sesn = 0; 1370 1371 error = falloc(td, &f, &fd, 0); 1372 1373 if (error) { 1374 free(fcr, M_XDATA); 1375 return (error); 1376 } 1377 /* falloc automatically provides an extra reference to 'f'. */ 1378 finit(f, FREAD | FWRITE, DTYPE_CRYPTO, fcr, &cryptofops); 1379 *(u_int32_t *)data = fd; 1380 fdrop(f, td); 1381 break; 1382 case CRIOFINDDEV: 1383 error = cryptodev_find((struct crypt_find_op *)data); 1384 break; 1385 case CRIOASYMFEAT: 1386 error = crypto_getfeat((int *)data); 1387 break; 1388 default: 1389 error = EINVAL; 1390 break; 1391 } 1392 return (error); 1393} 1394 1395static struct cdevsw crypto_cdevsw = { 1396 .d_version = D_VERSION, 1397 .d_flags = D_NEEDGIANT, 1398 .d_open = cryptoopen, 1399 .d_read = cryptoread, 1400 .d_write = cryptowrite, 1401 .d_ioctl = cryptoioctl, 1402 .d_name = "crypto", 1403}; 1404static struct cdev *crypto_dev; 1405 1406/* 1407 * Initialization code, both for static and dynamic loading. 1408 */ 1409static int 1410cryptodev_modevent(module_t mod, int type, void *unused) 1411{ 1412 switch (type) { 1413 case MOD_LOAD: 1414 if (bootverbose) 1415 printf("crypto: <crypto device>\n"); 1416 crypto_dev = make_dev(&crypto_cdevsw, 0, 1417 UID_ROOT, GID_WHEEL, 0666, 1418 "crypto"); 1419 return 0; 1420 case MOD_UNLOAD: 1421 /*XXX disallow if active sessions */ 1422 destroy_dev(crypto_dev); 1423 return 0; 1424 } 1425 return EINVAL; 1426} 1427 1428static moduledata_t cryptodev_mod = { 1429 "cryptodev", 1430 cryptodev_modevent, 1431 0 1432}; 1433MODULE_VERSION(cryptodev, 1); 1434DECLARE_MODULE(cryptodev, cryptodev_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 1435MODULE_DEPEND(cryptodev, crypto, 1, 1, 1); 1436MODULE_DEPEND(cryptodev, zlib, 1, 1, 1); 1437