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