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$"); 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 69 SDT_PROVIDER_DECLARE(opencrypto); 70 71 SDT_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 77 struct 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 87 struct 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 99 struct 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 109 struct crparam32 { 110 u_int32_t crp_p; 111 u_int crp_nbits; 112 }; 113 114 struct 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 123 struct cryptotstat32 { 124 struct timespec32 acc; 125 struct timespec32 min; 126 struct timespec32 max; 127 u_int32_t count; 128 }; 129 130 struct 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 151 static void 152 session_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 164 static void 165 session2_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 173 static void 174 session_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 186 static void 187 session2_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 195 static void 196 crypt_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 209 static void 210 crypt_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 223 static void 224 crparam_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 231 static void 232 crparam_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 239 static void 240 crypt_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 253 static void 254 crypt_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 268 struct 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 286 struct cryptop_data { 287 struct csession *cse; 288 289 struct iovec iovec[1]; 290 struct uio uio; 291 bool done; 292 }; 293 294 struct fcrypt { 295 TAILQ_HEAD(csessionlist, csession) csessions; 296 int sesn; 297 }; 298 299 static int cryptof_ioctl(struct file *, u_long, void *, 300 struct ucred *, struct thread *); 301 static int cryptof_stat(struct file *, struct stat *, 302 struct ucred *, struct thread *); 303 static int cryptof_close(struct file *, struct thread *); 304 static int cryptof_fill_kinfo(struct file *, struct kinfo_file *, 305 struct filedesc *); 306 307 static struct fileops cryptofops = { 308 .fo_read = invfo_rdwr, 309 .fo_write = invfo_rdwr, 310 .fo_truncate = invfo_truncate, 311 .fo_ioctl = cryptof_ioctl, 312 .fo_poll = invfo_poll, 313 .fo_kqfilter = invfo_kqfilter, 314 .fo_stat = cryptof_stat, 315 .fo_close = cryptof_close, 316 .fo_chmod = invfo_chmod, 317 .fo_chown = invfo_chown, 318 .fo_sendfile = invfo_sendfile, 319 .fo_fill_kinfo = cryptof_fill_kinfo, 320 }; 321 322 static struct csession *csefind(struct fcrypt *, u_int); 323 static int csedelete(struct fcrypt *, struct csession *); 324 static struct csession *cseadd(struct fcrypt *, struct csession *); 325 static struct csession *csecreate(struct fcrypt *, u_int64_t, caddr_t, 326 u_int64_t, caddr_t, u_int64_t, u_int32_t, u_int32_t, struct enc_xform *, 327 struct auth_hash *); 328 static int csefree(struct csession *); 329 330 static int cryptodev_op(struct csession *, struct crypt_op *, 331 struct ucred *, struct thread *td); 332 static int cryptodev_aead(struct csession *, struct crypt_aead *, 333 struct ucred *, struct thread *); 334 static int cryptodev_key(struct crypt_kop *); 335 static int cryptodev_find(struct crypt_find_op *); 336 337 /* 338 * Check a crypto identifier to see if it requested 339 * a software device/driver. This can be done either 340 * by device name/class or through search constraints. 341 */ 342 static int 343 checkforsoftware(int *cridp) 344 { 345 int crid; 346 347 crid = *cridp; 348 349 if (!crypto_devallowsoft) { 350 if (crid & CRYPTOCAP_F_SOFTWARE) { 351 if (crid & CRYPTOCAP_F_HARDWARE) { 352 *cridp = CRYPTOCAP_F_HARDWARE; 353 return 0; 354 } 355 return EINVAL; 356 } 357 if ((crid & CRYPTOCAP_F_HARDWARE) == 0 && 358 (crypto_getcaps(crid) & CRYPTOCAP_F_HARDWARE) == 0) 359 return EINVAL; 360 } 361 return 0; 362 } 363 364 /* ARGSUSED */ 365 static int 366 cryptof_ioctl( 367 struct file *fp, 368 u_long cmd, 369 void *data, 370 struct ucred *active_cred, 371 struct thread *td) 372 { 373 #define SES2(p) ((struct session2_op *)p) 374 struct cryptoini cria, crie; 375 struct fcrypt *fcr = fp->f_data; 376 struct csession *cse; 377 struct session_op *sop; 378 struct crypt_op *cop; 379 struct crypt_aead *caead; 380 struct enc_xform *txform = NULL; 381 struct auth_hash *thash = NULL; 382 struct crypt_kop *kop; 383 u_int64_t sid; 384 u_int32_t ses; 385 int error = 0, crid; 386 #ifdef COMPAT_FREEBSD32 387 struct session2_op sopc; 388 struct crypt_op copc; 389 struct crypt_kop kopc; 390 #endif 391 392 switch (cmd) { 393 case CIOCGSESSION: 394 case CIOCGSESSION2: 395 #ifdef COMPAT_FREEBSD32 396 case CIOCGSESSION32: 397 case CIOCGSESSION232: 398 if (cmd == CIOCGSESSION32) { 399 session_op_from_32(data, (struct session_op *)&sopc); 400 sop = (struct session_op *)&sopc; 401 } else if (cmd == CIOCGSESSION232) { 402 session2_op_from_32(data, &sopc); 403 sop = (struct session_op *)&sopc; 404 } else 405 #endif 406 sop = (struct session_op *)data; 407 switch (sop->cipher) { 408 case 0: 409 break; 410 case CRYPTO_DES_CBC: 411 txform = &enc_xform_des; 412 break; 413 case CRYPTO_3DES_CBC: 414 txform = &enc_xform_3des; 415 break; 416 case CRYPTO_BLF_CBC: 417 txform = &enc_xform_blf; 418 break; 419 case CRYPTO_CAST_CBC: 420 txform = &enc_xform_cast5; 421 break; 422 case CRYPTO_SKIPJACK_CBC: 423 txform = &enc_xform_skipjack; 424 break; 425 case CRYPTO_AES_CBC: 426 txform = &enc_xform_rijndael128; 427 break; 428 case CRYPTO_AES_XTS: 429 txform = &enc_xform_aes_xts; 430 break; 431 case CRYPTO_NULL_CBC: 432 txform = &enc_xform_null; 433 break; 434 case CRYPTO_ARC4: 435 txform = &enc_xform_arc4; 436 break; 437 case CRYPTO_CAMELLIA_CBC: 438 txform = &enc_xform_camellia; 439 break; 440 case CRYPTO_AES_ICM: 441 txform = &enc_xform_aes_icm; 442 break; 443 case CRYPTO_AES_NIST_GCM_16: 444 txform = &enc_xform_aes_nist_gcm; 445 break; 446 case CRYPTO_CHACHA20: 447 txform = &enc_xform_chacha20; 448 break; 449 450 default: 451 CRYPTDEB("invalid cipher"); 452 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 453 return (EINVAL); 454 } 455 456 switch (sop->mac) { 457 case 0: 458 break; 459 case CRYPTO_MD5_HMAC: 460 thash = &auth_hash_hmac_md5; 461 break; 462 case CRYPTO_SHA1_HMAC: 463 thash = &auth_hash_hmac_sha1; 464 break; 465 case CRYPTO_SHA2_256_HMAC: 466 thash = &auth_hash_hmac_sha2_256; 467 break; 468 case CRYPTO_SHA2_384_HMAC: 469 thash = &auth_hash_hmac_sha2_384; 470 break; 471 case CRYPTO_SHA2_512_HMAC: 472 thash = &auth_hash_hmac_sha2_512; 473 break; 474 case CRYPTO_RIPEMD160_HMAC: 475 thash = &auth_hash_hmac_ripemd_160; 476 break; 477 case CRYPTO_AES_128_NIST_GMAC: 478 thash = &auth_hash_nist_gmac_aes_128; 479 break; 480 case CRYPTO_AES_192_NIST_GMAC: 481 thash = &auth_hash_nist_gmac_aes_192; 482 break; 483 case CRYPTO_AES_256_NIST_GMAC: 484 thash = &auth_hash_nist_gmac_aes_256; 485 break; 486 487 #ifdef notdef 488 case CRYPTO_MD5: 489 thash = &auth_hash_md5; 490 break; 491 case CRYPTO_SHA1: 492 thash = &auth_hash_sha1; 493 break; 494 #endif 495 case CRYPTO_NULL_HMAC: 496 thash = &auth_hash_null; 497 break; 498 499 case CRYPTO_BLAKE2B: 500 thash = &auth_hash_blake2b; 501 break; 502 case CRYPTO_BLAKE2S: 503 thash = &auth_hash_blake2s; 504 break; 505 506 default: 507 CRYPTDEB("invalid mac"); 508 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 509 return (EINVAL); 510 } 511 512 bzero(&crie, sizeof(crie)); 513 bzero(&cria, sizeof(cria)); 514 515 if (txform) { 516 crie.cri_alg = txform->type; 517 crie.cri_klen = sop->keylen * 8; 518 if (sop->keylen > txform->maxkey || 519 sop->keylen < txform->minkey) { 520 CRYPTDEB("invalid cipher parameters"); 521 error = EINVAL; 522 SDT_PROBE1(opencrypto, dev, ioctl, error, 523 __LINE__); 524 goto bail; 525 } 526 527 crie.cri_key = malloc(crie.cri_klen / 8, 528 M_XDATA, M_WAITOK); 529 if ((error = copyin(sop->key, crie.cri_key, 530 crie.cri_klen / 8))) { 531 CRYPTDEB("invalid key"); 532 SDT_PROBE1(opencrypto, dev, ioctl, error, 533 __LINE__); 534 goto bail; 535 } 536 if (thash) 537 crie.cri_next = &cria; 538 } 539 540 if (thash) { 541 cria.cri_alg = thash->type; 542 cria.cri_klen = sop->mackeylen * 8; 543 if (thash->keysize != 0 && 544 sop->mackeylen > thash->keysize) { 545 CRYPTDEB("invalid mac key length"); 546 error = EINVAL; 547 SDT_PROBE1(opencrypto, dev, ioctl, error, 548 __LINE__); 549 goto bail; 550 } 551 552 if (cria.cri_klen) { 553 cria.cri_key = malloc(cria.cri_klen / 8, 554 M_XDATA, M_WAITOK); 555 if ((error = copyin(sop->mackey, cria.cri_key, 556 cria.cri_klen / 8))) { 557 CRYPTDEB("invalid mac key"); 558 SDT_PROBE1(opencrypto, dev, ioctl, 559 error, __LINE__); 560 goto bail; 561 } 562 } 563 } 564 565 /* NB: CIOCGSESSION2 has the crid */ 566 if (cmd == CIOCGSESSION2 567 #ifdef COMPAT_FREEBSD32 568 || cmd == CIOCGSESSION232 569 #endif 570 ) { 571 crid = SES2(sop)->crid; 572 error = checkforsoftware(&crid); 573 if (error) { 574 CRYPTDEB("checkforsoftware"); 575 SDT_PROBE1(opencrypto, dev, ioctl, error, 576 __LINE__); 577 goto bail; 578 } 579 } else 580 crid = CRYPTOCAP_F_HARDWARE; 581 error = crypto_newsession(&sid, (txform ? &crie : &cria), crid); 582 if (error) { 583 CRYPTDEB("crypto_newsession"); 584 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 585 goto bail; 586 } 587 588 cse = csecreate(fcr, sid, crie.cri_key, crie.cri_klen, 589 cria.cri_key, cria.cri_klen, sop->cipher, sop->mac, txform, 590 thash); 591 592 if (cse == NULL) { 593 crypto_freesession(sid); 594 error = EINVAL; 595 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 596 CRYPTDEB("csecreate"); 597 goto bail; 598 } 599 sop->ses = cse->ses; 600 if (cmd == CIOCGSESSION2 601 #ifdef COMPAT_FREEBSD32 602 || cmd == CIOCGSESSION232 603 #endif 604 ) { 605 /* return hardware/driver id */ 606 SES2(sop)->crid = CRYPTO_SESID2HID(cse->sid); 607 } 608 bail: 609 if (error) { 610 if (crie.cri_key) 611 free(crie.cri_key, M_XDATA); 612 if (cria.cri_key) 613 free(cria.cri_key, M_XDATA); 614 } 615 #ifdef COMPAT_FREEBSD32 616 else { 617 if (cmd == CIOCGSESSION32) 618 session_op_to_32(sop, data); 619 else if (cmd == CIOCGSESSION232) 620 session2_op_to_32((struct session2_op *)sop, 621 data); 622 } 623 #endif 624 break; 625 case CIOCFSESSION: 626 ses = *(u_int32_t *)data; 627 cse = csefind(fcr, ses); 628 if (cse == NULL) { 629 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 630 return (EINVAL); 631 } 632 csedelete(fcr, cse); 633 error = csefree(cse); 634 break; 635 case CIOCCRYPT: 636 #ifdef COMPAT_FREEBSD32 637 case CIOCCRYPT32: 638 if (cmd == CIOCCRYPT32) { 639 cop = &copc; 640 crypt_op_from_32(data, cop); 641 } else 642 #endif 643 cop = (struct crypt_op *)data; 644 cse = csefind(fcr, cop->ses); 645 if (cse == NULL) { 646 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 647 return (EINVAL); 648 } 649 error = cryptodev_op(cse, cop, active_cred, td); 650 #ifdef COMPAT_FREEBSD32 651 if (error == 0 && cmd == CIOCCRYPT32) 652 crypt_op_to_32(cop, data); 653 #endif 654 break; 655 case CIOCKEY: 656 case CIOCKEY2: 657 #ifdef COMPAT_FREEBSD32 658 case CIOCKEY32: 659 case CIOCKEY232: 660 #endif 661 if (!crypto_userasymcrypto) { 662 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 663 return (EPERM); /* XXX compat? */ 664 } 665 #ifdef COMPAT_FREEBSD32 666 if (cmd == CIOCKEY32 || cmd == CIOCKEY232) { 667 kop = &kopc; 668 crypt_kop_from_32(data, kop); 669 } else 670 #endif 671 kop = (struct crypt_kop *)data; 672 if (cmd == CIOCKEY 673 #ifdef COMPAT_FREEBSD32 674 || cmd == CIOCKEY32 675 #endif 676 ) { 677 /* NB: crypto core enforces s/w driver use */ 678 kop->crk_crid = 679 CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE; 680 } 681 mtx_lock(&Giant); 682 error = cryptodev_key(kop); 683 mtx_unlock(&Giant); 684 #ifdef COMPAT_FREEBSD32 685 if (cmd == CIOCKEY32 || cmd == CIOCKEY232) 686 crypt_kop_to_32(kop, data); 687 #endif 688 break; 689 case CIOCASYMFEAT: 690 if (!crypto_userasymcrypto) { 691 /* 692 * NB: if user asym crypto operations are 693 * not permitted return "no algorithms" 694 * so well-behaved applications will just 695 * fallback to doing them in software. 696 */ 697 *(int *)data = 0; 698 } else { 699 error = crypto_getfeat((int *)data); 700 if (error) 701 SDT_PROBE1(opencrypto, dev, ioctl, error, 702 __LINE__); 703 } 704 break; 705 case CIOCFINDDEV: 706 error = cryptodev_find((struct crypt_find_op *)data); 707 break; 708 case CIOCCRYPTAEAD: 709 caead = (struct crypt_aead *)data; 710 cse = csefind(fcr, caead->ses); 711 if (cse == NULL) { 712 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 713 return (EINVAL); 714 } 715 error = cryptodev_aead(cse, caead, active_cred, td); 716 break; 717 default: 718 error = EINVAL; 719 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 720 break; 721 } 722 return (error); 723 #undef SES2 724 } 725 726 static int cryptodev_cb(struct cryptop *); 727 728 static struct cryptop_data * 729 cod_alloc(struct csession *cse, size_t len, struct thread *td) 730 { 731 struct cryptop_data *cod; 732 struct uio *uio; 733 734 cod = malloc(sizeof(struct cryptop_data), M_XDATA, M_WAITOK | M_ZERO); 735 736 cod->cse = cse; 737 uio = &cod->uio; 738 uio->uio_iov = cod->iovec; 739 uio->uio_iovcnt = 1; 740 uio->uio_resid = len; 741 uio->uio_segflg = UIO_SYSSPACE; 742 uio->uio_rw = UIO_WRITE; 743 uio->uio_td = td; 744 uio->uio_iov[0].iov_len = len; 745 uio->uio_iov[0].iov_base = malloc(len, M_XDATA, M_WAITOK); 746 return (cod); 747 } 748 749 static void 750 cod_free(struct cryptop_data *cod) 751 { 752 753 free(cod->uio.uio_iov[0].iov_base, M_XDATA); 754 free(cod, M_XDATA); 755 } 756 757 static int 758 cryptodev_op( 759 struct csession *cse, 760 struct crypt_op *cop, 761 struct ucred *active_cred, 762 struct thread *td) 763 { 764 struct cryptop_data *cod = NULL; 765 struct cryptop *crp = NULL; 766 struct cryptodesc *crde = NULL, *crda = NULL; 767 int error; 768 769 if (cop->len > 256*1024-4) { 770 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 771 return (E2BIG); 772 } 773 774 if (cse->txform) { 775 if (cop->len == 0 || (cop->len % cse->txform->blocksize) != 0) { 776 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 777 return (EINVAL); 778 } 779 } 780 781 if (cse->thash) 782 cod = cod_alloc(cse, cop->len + cse->thash->hashsize, td); 783 else 784 cod = cod_alloc(cse, cop->len, td); 785 786 crp = crypto_getreq((cse->txform != NULL) + (cse->thash != NULL)); 787 if (crp == NULL) { 788 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 789 error = ENOMEM; 790 goto bail; 791 } 792 793 if (cse->thash && cse->txform) { 794 if (cop->flags & COP_F_CIPHER_FIRST) { 795 crde = crp->crp_desc; 796 crda = crde->crd_next; 797 } else { 798 crda = crp->crp_desc; 799 crde = crda->crd_next; 800 } 801 } else if (cse->thash) { 802 crda = crp->crp_desc; 803 } else if (cse->txform) { 804 crde = crp->crp_desc; 805 } else { 806 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 807 error = EINVAL; 808 goto bail; 809 } 810 811 if ((error = copyin(cop->src, cod->uio.uio_iov[0].iov_base, 812 cop->len))) { 813 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 814 goto bail; 815 } 816 817 if (crda) { 818 crda->crd_skip = 0; 819 crda->crd_len = cop->len; 820 crda->crd_inject = cop->len; 821 822 crda->crd_alg = cse->mac; 823 crda->crd_key = cse->mackey; 824 crda->crd_klen = cse->mackeylen * 8; 825 } 826 827 if (crde) { 828 if (cop->op == COP_ENCRYPT) 829 crde->crd_flags |= CRD_F_ENCRYPT; 830 else 831 crde->crd_flags &= ~CRD_F_ENCRYPT; 832 crde->crd_len = cop->len; 833 crde->crd_inject = 0; 834 835 crde->crd_alg = cse->cipher; 836 crde->crd_key = cse->key; 837 crde->crd_klen = cse->keylen * 8; 838 } 839 840 crp->crp_ilen = cop->len; 841 crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM 842 | (cop->flags & COP_F_BATCH); 843 crp->crp_uio = &cod->uio; 844 crp->crp_callback = cryptodev_cb; 845 crp->crp_sid = cse->sid; 846 crp->crp_opaque = cod; 847 848 if (cop->iv) { 849 if (crde == NULL) { 850 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 851 error = EINVAL; 852 goto bail; 853 } 854 if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */ 855 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 856 error = EINVAL; 857 goto bail; 858 } 859 if ((error = copyin(cop->iv, crde->crd_iv, 860 cse->txform->ivsize))) { 861 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 862 goto bail; 863 } 864 crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT; 865 crde->crd_skip = 0; 866 } else if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */ 867 crde->crd_skip = 0; 868 } else if (crde) { 869 crde->crd_flags |= CRD_F_IV_PRESENT; 870 crde->crd_skip = cse->txform->ivsize; 871 crde->crd_len -= cse->txform->ivsize; 872 } 873 874 if (cop->mac && crda == NULL) { 875 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 876 error = EINVAL; 877 goto bail; 878 } 879 880 again: 881 /* 882 * Let the dispatch run unlocked, then, interlock against the 883 * callback before checking if the operation completed and going 884 * to sleep. This insures drivers don't inherit our lock which 885 * results in a lock order reversal between crypto_dispatch forced 886 * entry and the crypto_done callback into us. 887 */ 888 error = crypto_dispatch(crp); 889 if (error != 0) { 890 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 891 goto bail; 892 } 893 894 mtx_lock(&cse->lock); 895 while (!cod->done) 896 mtx_sleep(cod, &cse->lock, PWAIT, "crydev", 0); 897 mtx_unlock(&cse->lock); 898 899 if (crp->crp_etype == EAGAIN) { 900 crp->crp_etype = 0; 901 crp->crp_flags &= ~CRYPTO_F_DONE; 902 cod->done = false; 903 goto again; 904 } 905 906 if (crp->crp_etype != 0) { 907 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 908 error = crp->crp_etype; 909 goto bail; 910 } 911 912 if (cop->dst && 913 (error = copyout(cod->uio.uio_iov[0].iov_base, cop->dst, 914 cop->len))) { 915 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 916 goto bail; 917 } 918 919 if (cop->mac && 920 (error = copyout((caddr_t)cod->uio.uio_iov[0].iov_base + cop->len, 921 cop->mac, cse->thash->hashsize))) { 922 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 923 goto bail; 924 } 925 926 bail: 927 if (crp) 928 crypto_freereq(crp); 929 if (cod) 930 cod_free(cod); 931 932 return (error); 933 } 934 935 static int 936 cryptodev_aead( 937 struct csession *cse, 938 struct crypt_aead *caead, 939 struct ucred *active_cred, 940 struct thread *td) 941 { 942 struct cryptop_data *cod = NULL; 943 struct cryptop *crp = NULL; 944 struct cryptodesc *crde = NULL, *crda = NULL; 945 int error; 946 947 if (caead->len > 256*1024-4 || caead->aadlen > 256*1024-4) { 948 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 949 return (E2BIG); 950 } 951 952 if (cse->txform == NULL || cse->thash == NULL || caead->tag == NULL || 953 (caead->len % cse->txform->blocksize) != 0) { 954 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 955 return (EINVAL); 956 } 957 958 cod = cod_alloc(cse, caead->aadlen + caead->len + cse->thash->hashsize, 959 td); 960 961 crp = crypto_getreq(2); 962 if (crp == NULL) { 963 error = ENOMEM; 964 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 965 goto bail; 966 } 967 968 if (caead->flags & COP_F_CIPHER_FIRST) { 969 crde = crp->crp_desc; 970 crda = crde->crd_next; 971 } else { 972 crda = crp->crp_desc; 973 crde = crda->crd_next; 974 } 975 976 if ((error = copyin(caead->aad, cod->uio.uio_iov[0].iov_base, 977 caead->aadlen))) { 978 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 979 goto bail; 980 } 981 982 if ((error = copyin(caead->src, (char *)cod->uio.uio_iov[0].iov_base + 983 caead->aadlen, caead->len))) { 984 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 985 goto bail; 986 } 987 988 /* 989 * For GCM, crd_len covers only the AAD. For other ciphers 990 * chained with an HMAC, crd_len covers both the AAD and the 991 * cipher text. 992 */ 993 crda->crd_skip = 0; 994 if (cse->cipher == CRYPTO_AES_NIST_GCM_16) 995 crda->crd_len = caead->aadlen; 996 else 997 crda->crd_len = caead->aadlen + caead->len; 998 crda->crd_inject = caead->aadlen + caead->len; 999 1000 crda->crd_alg = cse->mac; 1001 crda->crd_key = cse->mackey; 1002 crda->crd_klen = cse->mackeylen * 8; 1003 1004 if (caead->op == COP_ENCRYPT) 1005 crde->crd_flags |= CRD_F_ENCRYPT; 1006 else 1007 crde->crd_flags &= ~CRD_F_ENCRYPT; 1008 crde->crd_skip = caead->aadlen; 1009 crde->crd_len = caead->len; 1010 crde->crd_inject = caead->aadlen; 1011 1012 crde->crd_alg = cse->cipher; 1013 crde->crd_key = cse->key; 1014 crde->crd_klen = cse->keylen * 8; 1015 1016 crp->crp_ilen = caead->aadlen + caead->len; 1017 crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM 1018 | (caead->flags & COP_F_BATCH); 1019 crp->crp_uio = &cod->uio; 1020 crp->crp_callback = cryptodev_cb; 1021 crp->crp_sid = cse->sid; 1022 crp->crp_opaque = cod; 1023 1024 if (caead->iv) { 1025 if (caead->ivlen > sizeof(crde->crd_iv)) { 1026 error = EINVAL; 1027 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1028 goto bail; 1029 } 1030 1031 if ((error = copyin(caead->iv, crde->crd_iv, caead->ivlen))) { 1032 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1033 goto bail; 1034 } 1035 crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT; 1036 } else { 1037 crde->crd_flags |= CRD_F_IV_PRESENT; 1038 crde->crd_skip += cse->txform->ivsize; 1039 crde->crd_len -= cse->txform->ivsize; 1040 } 1041 1042 if ((error = copyin(caead->tag, (caddr_t)cod->uio.uio_iov[0].iov_base + 1043 caead->len + caead->aadlen, cse->thash->hashsize))) { 1044 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1045 goto bail; 1046 } 1047 again: 1048 /* 1049 * Let the dispatch run unlocked, then, interlock against the 1050 * callback before checking if the operation completed and going 1051 * to sleep. This insures drivers don't inherit our lock which 1052 * results in a lock order reversal between crypto_dispatch forced 1053 * entry and the crypto_done callback into us. 1054 */ 1055 error = crypto_dispatch(crp); 1056 if (error != 0) { 1057 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1058 goto bail; 1059 } 1060 1061 mtx_lock(&cse->lock); 1062 while (!cod->done) 1063 mtx_sleep(cod, &cse->lock, PWAIT, "crydev", 0); 1064 mtx_unlock(&cse->lock); 1065 1066 if (crp->crp_etype == EAGAIN) { 1067 crp->crp_etype = 0; 1068 crp->crp_flags &= ~CRYPTO_F_DONE; 1069 cod->done = false; 1070 goto again; 1071 } 1072 1073 if (crp->crp_etype != 0) { 1074 error = crp->crp_etype; 1075 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1076 goto bail; 1077 } 1078 1079 if (caead->dst && (error = copyout( 1080 (caddr_t)cod->uio.uio_iov[0].iov_base + caead->aadlen, caead->dst, 1081 caead->len))) { 1082 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1083 goto bail; 1084 } 1085 1086 if ((error = copyout((caddr_t)cod->uio.uio_iov[0].iov_base + 1087 caead->aadlen + caead->len, caead->tag, cse->thash->hashsize))) { 1088 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1089 goto bail; 1090 } 1091 1092 bail: 1093 crypto_freereq(crp); 1094 if (cod) 1095 cod_free(cod); 1096 1097 return (error); 1098 } 1099 1100 static int 1101 cryptodev_cb(struct cryptop *crp) 1102 { 1103 struct cryptop_data *cod = crp->crp_opaque; 1104 1105 /* 1106 * Lock to ensure the wakeup() is not missed by the loops 1107 * waiting on cod->done in cryptodev_op() and 1108 * cryptodev_aead(). 1109 */ 1110 mtx_lock(&cod->cse->lock); 1111 cod->done = true; 1112 mtx_unlock(&cod->cse->lock); 1113 wakeup(cod); 1114 return (0); 1115 } 1116 1117 static int 1118 cryptodevkey_cb(void *op) 1119 { 1120 struct cryptkop *krp = (struct cryptkop *) op; 1121 1122 wakeup_one(krp); 1123 return (0); 1124 } 1125 1126 static int 1127 cryptodev_key(struct crypt_kop *kop) 1128 { 1129 struct cryptkop *krp = NULL; 1130 int error = EINVAL; 1131 int in, out, size, i; 1132 1133 if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) { 1134 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1135 return (EFBIG); 1136 } 1137 1138 in = kop->crk_iparams; 1139 out = kop->crk_oparams; 1140 switch (kop->crk_op) { 1141 case CRK_MOD_EXP: 1142 if (in == 3 && out == 1) 1143 break; 1144 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1145 return (EINVAL); 1146 case CRK_MOD_EXP_CRT: 1147 if (in == 6 && out == 1) 1148 break; 1149 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1150 return (EINVAL); 1151 case CRK_DSA_SIGN: 1152 if (in == 5 && out == 2) 1153 break; 1154 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1155 return (EINVAL); 1156 case CRK_DSA_VERIFY: 1157 if (in == 7 && out == 0) 1158 break; 1159 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1160 return (EINVAL); 1161 case CRK_DH_COMPUTE_KEY: 1162 if (in == 3 && out == 1) 1163 break; 1164 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1165 return (EINVAL); 1166 default: 1167 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1168 return (EINVAL); 1169 } 1170 1171 krp = (struct cryptkop *)malloc(sizeof *krp, M_XDATA, M_WAITOK|M_ZERO); 1172 if (!krp) { 1173 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1174 return (ENOMEM); 1175 } 1176 krp->krp_op = kop->crk_op; 1177 krp->krp_status = kop->crk_status; 1178 krp->krp_iparams = kop->crk_iparams; 1179 krp->krp_oparams = kop->crk_oparams; 1180 krp->krp_crid = kop->crk_crid; 1181 krp->krp_status = 0; 1182 krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb; 1183 1184 for (i = 0; i < CRK_MAXPARAM; i++) { 1185 if (kop->crk_param[i].crp_nbits > 65536) { 1186 /* Limit is the same as in OpenBSD */ 1187 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1188 goto fail; 1189 } 1190 krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits; 1191 } 1192 for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) { 1193 size = (krp->krp_param[i].crp_nbits + 7) / 8; 1194 if (size == 0) 1195 continue; 1196 krp->krp_param[i].crp_p = malloc(size, M_XDATA, M_WAITOK); 1197 if (i >= krp->krp_iparams) 1198 continue; 1199 error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size); 1200 if (error) { 1201 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1202 goto fail; 1203 } 1204 } 1205 1206 error = crypto_kdispatch(krp); 1207 if (error) { 1208 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1209 goto fail; 1210 } 1211 error = tsleep(krp, PSOCK, "crydev", 0); 1212 if (error) { 1213 /* XXX can this happen? if so, how do we recover? */ 1214 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1215 goto fail; 1216 } 1217 1218 kop->crk_crid = krp->krp_crid; /* device that did the work */ 1219 if (krp->krp_status != 0) { 1220 error = krp->krp_status; 1221 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1222 goto fail; 1223 } 1224 1225 for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) { 1226 size = (krp->krp_param[i].crp_nbits + 7) / 8; 1227 if (size == 0) 1228 continue; 1229 error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size); 1230 if (error) { 1231 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1232 goto fail; 1233 } 1234 } 1235 1236 fail: 1237 if (krp) { 1238 kop->crk_status = krp->krp_status; 1239 for (i = 0; i < CRK_MAXPARAM; i++) { 1240 if (krp->krp_param[i].crp_p) 1241 free(krp->krp_param[i].crp_p, M_XDATA); 1242 } 1243 free(krp, M_XDATA); 1244 } 1245 return (error); 1246 } 1247 1248 static int 1249 cryptodev_find(struct crypt_find_op *find) 1250 { 1251 device_t dev; 1252 size_t fnlen = sizeof find->name; 1253 1254 if (find->crid != -1) { 1255 dev = crypto_find_device_byhid(find->crid); 1256 if (dev == NULL) 1257 return (ENOENT); 1258 strncpy(find->name, device_get_nameunit(dev), fnlen); 1259 find->name[fnlen - 1] = '\x0'; 1260 } else { 1261 find->name[fnlen - 1] = '\x0'; 1262 find->crid = crypto_find_driver(find->name); 1263 if (find->crid == -1) 1264 return (ENOENT); 1265 } 1266 return (0); 1267 } 1268 1269 /* ARGSUSED */ 1270 static int 1271 cryptof_stat( 1272 struct file *fp, 1273 struct stat *sb, 1274 struct ucred *active_cred, 1275 struct thread *td) 1276 { 1277 1278 return (EOPNOTSUPP); 1279 } 1280 1281 /* ARGSUSED */ 1282 static int 1283 cryptof_close(struct file *fp, struct thread *td) 1284 { 1285 struct fcrypt *fcr = fp->f_data; 1286 struct csession *cse; 1287 1288 while ((cse = TAILQ_FIRST(&fcr->csessions))) { 1289 TAILQ_REMOVE(&fcr->csessions, cse, next); 1290 (void)csefree(cse); 1291 } 1292 free(fcr, M_XDATA); 1293 fp->f_data = NULL; 1294 return 0; 1295 } 1296 1297 static int 1298 cryptof_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp) 1299 { 1300 1301 kif->kf_type = KF_TYPE_CRYPTO; 1302 return (0); 1303 } 1304 1305 static struct csession * 1306 csefind(struct fcrypt *fcr, u_int ses) 1307 { 1308 struct csession *cse; 1309 1310 TAILQ_FOREACH(cse, &fcr->csessions, next) 1311 if (cse->ses == ses) 1312 return (cse); 1313 return (NULL); 1314 } 1315 1316 static int 1317 csedelete(struct fcrypt *fcr, struct csession *cse_del) 1318 { 1319 struct csession *cse; 1320 1321 TAILQ_FOREACH(cse, &fcr->csessions, next) { 1322 if (cse == cse_del) { 1323 TAILQ_REMOVE(&fcr->csessions, cse, next); 1324 return (1); 1325 } 1326 } 1327 return (0); 1328 } 1329 1330 static struct csession * 1331 cseadd(struct fcrypt *fcr, struct csession *cse) 1332 { 1333 TAILQ_INSERT_TAIL(&fcr->csessions, cse, next); 1334 cse->ses = fcr->sesn++; 1335 return (cse); 1336 } 1337 1338 struct csession * 1339 csecreate(struct fcrypt *fcr, u_int64_t sid, caddr_t key, u_int64_t keylen, 1340 caddr_t mackey, u_int64_t mackeylen, u_int32_t cipher, u_int32_t mac, 1341 struct enc_xform *txform, struct auth_hash *thash) 1342 { 1343 struct csession *cse; 1344 1345 cse = malloc(sizeof(struct csession), M_XDATA, M_NOWAIT | M_ZERO); 1346 if (cse == NULL) 1347 return NULL; 1348 mtx_init(&cse->lock, "cryptodev", "crypto session lock", MTX_DEF); 1349 cse->key = key; 1350 cse->keylen = keylen/8; 1351 cse->mackey = mackey; 1352 cse->mackeylen = mackeylen/8; 1353 cse->sid = sid; 1354 cse->cipher = cipher; 1355 cse->mac = mac; 1356 cse->txform = txform; 1357 cse->thash = thash; 1358 cseadd(fcr, cse); 1359 return (cse); 1360 } 1361 1362 static int 1363 csefree(struct csession *cse) 1364 { 1365 int error; 1366 1367 error = crypto_freesession(cse->sid); 1368 mtx_destroy(&cse->lock); 1369 if (cse->key) 1370 free(cse->key, M_XDATA); 1371 if (cse->mackey) 1372 free(cse->mackey, M_XDATA); 1373 free(cse, M_XDATA); 1374 return (error); 1375 } 1376 1377 static int 1378 cryptoopen(struct cdev *dev, int oflags, int devtype, struct thread *td) 1379 { 1380 return (0); 1381 } 1382 1383 static int 1384 cryptoread(struct cdev *dev, struct uio *uio, int ioflag) 1385 { 1386 return (EIO); 1387 } 1388 1389 static int 1390 cryptowrite(struct cdev *dev, struct uio *uio, int ioflag) 1391 { 1392 return (EIO); 1393 } 1394 1395 static int 1396 cryptoioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td) 1397 { 1398 struct file *f; 1399 struct fcrypt *fcr; 1400 int fd, error; 1401 1402 switch (cmd) { 1403 case CRIOGET: 1404 fcr = malloc(sizeof(struct fcrypt), M_XDATA, M_WAITOK); 1405 TAILQ_INIT(&fcr->csessions); 1406 fcr->sesn = 0; 1407 1408 error = falloc(td, &f, &fd, 0); 1409 1410 if (error) { 1411 free(fcr, M_XDATA); 1412 return (error); 1413 } 1414 /* falloc automatically provides an extra reference to 'f'. */ 1415 finit(f, FREAD | FWRITE, DTYPE_CRYPTO, fcr, &cryptofops); 1416 *(u_int32_t *)data = fd; 1417 fdrop(f, td); 1418 break; 1419 case CRIOFINDDEV: 1420 error = cryptodev_find((struct crypt_find_op *)data); 1421 break; 1422 case CRIOASYMFEAT: 1423 error = crypto_getfeat((int *)data); 1424 break; 1425 default: 1426 error = EINVAL; 1427 break; 1428 } 1429 return (error); 1430 } 1431 1432 static struct cdevsw crypto_cdevsw = { 1433 .d_version = D_VERSION, 1434 .d_flags = D_NEEDGIANT, 1435 .d_open = cryptoopen, 1436 .d_read = cryptoread, 1437 .d_write = cryptowrite, 1438 .d_ioctl = cryptoioctl, 1439 .d_name = "crypto", 1440 }; 1441 static struct cdev *crypto_dev; 1442 1443 /* 1444 * Initialization code, both for static and dynamic loading. 1445 */ 1446 static int 1447 cryptodev_modevent(module_t mod, int type, void *unused) 1448 { 1449 switch (type) { 1450 case MOD_LOAD: 1451 if (bootverbose) 1452 printf("crypto: <crypto device>\n"); 1453 crypto_dev = make_dev(&crypto_cdevsw, 0, 1454 UID_ROOT, GID_WHEEL, 0666, 1455 "crypto"); 1456 return 0; 1457 case MOD_UNLOAD: 1458 /*XXX disallow if active sessions */ 1459 destroy_dev(crypto_dev); 1460 return 0; 1461 } 1462 return EINVAL; 1463 } 1464 1465 static moduledata_t cryptodev_mod = { 1466 "cryptodev", 1467 cryptodev_modevent, 1468 0 1469 }; 1470 MODULE_VERSION(cryptodev, 1); 1471 DECLARE_MODULE(cryptodev, cryptodev_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 1472 MODULE_DEPEND(cryptodev, crypto, 1, 1, 1); 1473 MODULE_DEPEND(cryptodev, zlib, 1, 1, 1); 1474