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