1 /*- 2 * Copyright (c) 2002-2006 Sam Leffler. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 */ 24 25 #include <sys/cdefs.h> 26 __FBSDID("$FreeBSD$"); 27 28 /* 29 * Cryptographic Subsystem. 30 * 31 * This code is derived from the Openbsd Cryptographic Framework (OCF) 32 * that has the copyright shown below. Very little of the original 33 * code remains. 34 */ 35 36 /*- 37 * The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu) 38 * 39 * This code was written by Angelos D. Keromytis in Athens, Greece, in 40 * February 2000. Network Security Technologies Inc. (NSTI) kindly 41 * supported the development of this code. 42 * 43 * Copyright (c) 2000, 2001 Angelos D. Keromytis 44 * 45 * Permission to use, copy, and modify this software with or without fee 46 * is hereby granted, provided that this entire notice is included in 47 * all source code copies of any software which is or includes a copy or 48 * modification of this software. 49 * 50 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR 51 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY 52 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE 53 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR 54 * PURPOSE. 55 */ 56 57 #define CRYPTO_TIMING /* enable timing support */ 58 59 #include "opt_compat.h" 60 #include "opt_ddb.h" 61 62 #include <sys/param.h> 63 #include <sys/systm.h> 64 #include <sys/eventhandler.h> 65 #include <sys/kernel.h> 66 #include <sys/kthread.h> 67 #include <sys/linker.h> 68 #include <sys/lock.h> 69 #include <sys/module.h> 70 #include <sys/mutex.h> 71 #include <sys/malloc.h> 72 #include <sys/proc.h> 73 #include <sys/refcount.h> 74 #include <sys/sdt.h> 75 #include <sys/smp.h> 76 #include <sys/sysctl.h> 77 #include <sys/taskqueue.h> 78 79 #include <ddb/ddb.h> 80 81 #include <vm/uma.h> 82 #include <crypto/intake.h> 83 #include <opencrypto/cryptodev.h> 84 #include <opencrypto/xform_auth.h> 85 #include <opencrypto/xform_enc.h> 86 87 #include <sys/kobj.h> 88 #include <sys/bus.h> 89 #include "cryptodev_if.h" 90 91 #if defined(__i386__) || defined(__amd64__) || defined(__aarch64__) 92 #include <machine/pcb.h> 93 #endif 94 95 SDT_PROVIDER_DEFINE(opencrypto); 96 97 /* 98 * Crypto drivers register themselves by allocating a slot in the 99 * crypto_drivers table with crypto_get_driverid() and then registering 100 * each asym algorithm they support with crypto_kregister(). 101 */ 102 static struct mtx crypto_drivers_mtx; /* lock on driver table */ 103 #define CRYPTO_DRIVER_LOCK() mtx_lock(&crypto_drivers_mtx) 104 #define CRYPTO_DRIVER_UNLOCK() mtx_unlock(&crypto_drivers_mtx) 105 #define CRYPTO_DRIVER_ASSERT() mtx_assert(&crypto_drivers_mtx, MA_OWNED) 106 107 /* 108 * Crypto device/driver capabilities structure. 109 * 110 * Synchronization: 111 * (d) - protected by CRYPTO_DRIVER_LOCK() 112 * (q) - protected by CRYPTO_Q_LOCK() 113 * Not tagged fields are read-only. 114 */ 115 struct cryptocap { 116 device_t cc_dev; 117 uint32_t cc_hid; 118 u_int32_t cc_sessions; /* (d) # of sessions */ 119 u_int32_t cc_koperations; /* (d) # os asym operations */ 120 u_int8_t cc_kalg[CRK_ALGORITHM_MAX + 1]; 121 122 int cc_flags; /* (d) flags */ 123 #define CRYPTOCAP_F_CLEANUP 0x80000000 /* needs resource cleanup */ 124 int cc_qblocked; /* (q) symmetric q blocked */ 125 int cc_kqblocked; /* (q) asymmetric q blocked */ 126 size_t cc_session_size; 127 volatile int cc_refs; 128 }; 129 130 static struct cryptocap **crypto_drivers = NULL; 131 static int crypto_drivers_size = 0; 132 133 struct crypto_session { 134 struct cryptocap *cap; 135 void *softc; 136 struct crypto_session_params csp; 137 }; 138 139 /* 140 * There are two queues for crypto requests; one for symmetric (e.g. 141 * cipher) operations and one for asymmetric (e.g. MOD)operations. 142 * A single mutex is used to lock access to both queues. We could 143 * have one per-queue but having one simplifies handling of block/unblock 144 * operations. 145 */ 146 static int crp_sleep = 0; 147 static TAILQ_HEAD(cryptop_q ,cryptop) crp_q; /* request queues */ 148 static TAILQ_HEAD(,cryptkop) crp_kq; 149 static struct mtx crypto_q_mtx; 150 #define CRYPTO_Q_LOCK() mtx_lock(&crypto_q_mtx) 151 #define CRYPTO_Q_UNLOCK() mtx_unlock(&crypto_q_mtx) 152 153 static SYSCTL_NODE(_kern, OID_AUTO, crypto, CTLFLAG_RW, 0, 154 "In-kernel cryptography"); 155 156 /* 157 * Taskqueue used to dispatch the crypto requests 158 * that have the CRYPTO_F_ASYNC flag 159 */ 160 static struct taskqueue *crypto_tq; 161 162 /* 163 * Crypto seq numbers are operated on with modular arithmetic 164 */ 165 #define CRYPTO_SEQ_GT(a,b) ((int)((a)-(b)) > 0) 166 167 struct crypto_ret_worker { 168 struct mtx crypto_ret_mtx; 169 170 TAILQ_HEAD(,cryptop) crp_ordered_ret_q; /* ordered callback queue for symetric jobs */ 171 TAILQ_HEAD(,cryptop) crp_ret_q; /* callback queue for symetric jobs */ 172 TAILQ_HEAD(,cryptkop) crp_ret_kq; /* callback queue for asym jobs */ 173 174 u_int32_t reorder_ops; /* total ordered sym jobs received */ 175 u_int32_t reorder_cur_seq; /* current sym job dispatched */ 176 177 struct proc *cryptoretproc; 178 }; 179 static struct crypto_ret_worker *crypto_ret_workers = NULL; 180 181 #define CRYPTO_RETW(i) (&crypto_ret_workers[i]) 182 #define CRYPTO_RETW_ID(w) ((w) - crypto_ret_workers) 183 #define FOREACH_CRYPTO_RETW(w) \ 184 for (w = crypto_ret_workers; w < crypto_ret_workers + crypto_workers_num; ++w) 185 186 #define CRYPTO_RETW_LOCK(w) mtx_lock(&w->crypto_ret_mtx) 187 #define CRYPTO_RETW_UNLOCK(w) mtx_unlock(&w->crypto_ret_mtx) 188 #define CRYPTO_RETW_EMPTY(w) \ 189 (TAILQ_EMPTY(&w->crp_ret_q) && TAILQ_EMPTY(&w->crp_ret_kq) && TAILQ_EMPTY(&w->crp_ordered_ret_q)) 190 191 static int crypto_workers_num = 0; 192 SYSCTL_INT(_kern_crypto, OID_AUTO, num_workers, CTLFLAG_RDTUN, 193 &crypto_workers_num, 0, 194 "Number of crypto workers used to dispatch crypto jobs"); 195 #ifdef COMPAT_FREEBSD12 196 SYSCTL_INT(_kern, OID_AUTO, crypto_workers_num, CTLFLAG_RDTUN, 197 &crypto_workers_num, 0, 198 "Number of crypto workers used to dispatch crypto jobs"); 199 #endif 200 201 static uma_zone_t cryptop_zone; 202 static uma_zone_t cryptoses_zone; 203 204 int crypto_userasymcrypto = 1; 205 SYSCTL_INT(_kern_crypto, OID_AUTO, asym_enable, CTLFLAG_RW, 206 &crypto_userasymcrypto, 0, 207 "Enable user-mode access to asymmetric crypto support"); 208 #ifdef COMPAT_FREEBSD12 209 SYSCTL_INT(_kern, OID_AUTO, userasymcrypto, CTLFLAG_RW, 210 &crypto_userasymcrypto, 0, 211 "Enable/disable user-mode access to asymmetric crypto support"); 212 #endif 213 214 int crypto_devallowsoft = 0; 215 SYSCTL_INT(_kern_crypto, OID_AUTO, allow_soft, CTLFLAG_RW, 216 &crypto_devallowsoft, 0, 217 "Enable use of software crypto by /dev/crypto"); 218 #ifdef COMPAT_FREEBSD12 219 SYSCTL_INT(_kern, OID_AUTO, cryptodevallowsoft, CTLFLAG_RW, 220 &crypto_devallowsoft, 0, 221 "Enable/disable use of software crypto by /dev/crypto"); 222 #endif 223 224 MALLOC_DEFINE(M_CRYPTO_DATA, "crypto", "crypto session records"); 225 226 static void crypto_proc(void); 227 static struct proc *cryptoproc; 228 static void crypto_ret_proc(struct crypto_ret_worker *ret_worker); 229 static void crypto_destroy(void); 230 static int crypto_invoke(struct cryptocap *cap, struct cryptop *crp, int hint); 231 static int crypto_kinvoke(struct cryptkop *krp); 232 static void crypto_task_invoke(void *ctx, int pending); 233 static void crypto_batch_enqueue(struct cryptop *crp); 234 235 static struct cryptostats cryptostats; 236 SYSCTL_STRUCT(_kern_crypto, OID_AUTO, stats, CTLFLAG_RW, &cryptostats, 237 cryptostats, "Crypto system statistics"); 238 239 #ifdef CRYPTO_TIMING 240 static int crypto_timing = 0; 241 SYSCTL_INT(_debug, OID_AUTO, crypto_timing, CTLFLAG_RW, 242 &crypto_timing, 0, "Enable/disable crypto timing support"); 243 #endif 244 245 /* Try to avoid directly exposing the key buffer as a symbol */ 246 static struct keybuf *keybuf; 247 248 static struct keybuf empty_keybuf = { 249 .kb_nents = 0 250 }; 251 252 /* Obtain the key buffer from boot metadata */ 253 static void 254 keybuf_init(void) 255 { 256 caddr_t kmdp; 257 258 kmdp = preload_search_by_type("elf kernel"); 259 260 if (kmdp == NULL) 261 kmdp = preload_search_by_type("elf64 kernel"); 262 263 keybuf = (struct keybuf *)preload_search_info(kmdp, 264 MODINFO_METADATA | MODINFOMD_KEYBUF); 265 266 if (keybuf == NULL) 267 keybuf = &empty_keybuf; 268 } 269 270 /* It'd be nice if we could store these in some kind of secure memory... */ 271 struct keybuf * get_keybuf(void) { 272 273 return (keybuf); 274 } 275 276 static struct cryptocap * 277 cap_ref(struct cryptocap *cap) 278 { 279 280 refcount_acquire(&cap->cc_refs); 281 return (cap); 282 } 283 284 static void 285 cap_rele(struct cryptocap *cap) 286 { 287 288 if (refcount_release(&cap->cc_refs) == 0) 289 return; 290 291 KASSERT(cap->cc_sessions == 0, 292 ("freeing crypto driver with active sessions")); 293 KASSERT(cap->cc_koperations == 0, 294 ("freeing crypto driver with active key operations")); 295 296 free(cap, M_CRYPTO_DATA); 297 } 298 299 static int 300 crypto_init(void) 301 { 302 struct crypto_ret_worker *ret_worker; 303 int error; 304 305 mtx_init(&crypto_drivers_mtx, "crypto", "crypto driver table", 306 MTX_DEF|MTX_QUIET); 307 308 TAILQ_INIT(&crp_q); 309 TAILQ_INIT(&crp_kq); 310 mtx_init(&crypto_q_mtx, "crypto", "crypto op queues", MTX_DEF); 311 312 cryptop_zone = uma_zcreate("cryptop", sizeof (struct cryptop), 313 0, 0, 0, 0, 314 UMA_ALIGN_PTR, UMA_ZONE_ZINIT); 315 cryptoses_zone = uma_zcreate("crypto_session", 316 sizeof(struct crypto_session), NULL, NULL, NULL, NULL, 317 UMA_ALIGN_PTR, UMA_ZONE_ZINIT); 318 319 if (cryptop_zone == NULL || cryptoses_zone == NULL) { 320 printf("crypto_init: cannot setup crypto zones\n"); 321 error = ENOMEM; 322 goto bad; 323 } 324 325 crypto_drivers_size = CRYPTO_DRIVERS_INITIAL; 326 crypto_drivers = malloc(crypto_drivers_size * 327 sizeof(struct cryptocap), M_CRYPTO_DATA, M_NOWAIT | M_ZERO); 328 if (crypto_drivers == NULL) { 329 printf("crypto_init: cannot setup crypto drivers\n"); 330 error = ENOMEM; 331 goto bad; 332 } 333 334 if (crypto_workers_num < 1 || crypto_workers_num > mp_ncpus) 335 crypto_workers_num = mp_ncpus; 336 337 crypto_tq = taskqueue_create("crypto", M_WAITOK|M_ZERO, 338 taskqueue_thread_enqueue, &crypto_tq); 339 if (crypto_tq == NULL) { 340 printf("crypto init: cannot setup crypto taskqueue\n"); 341 error = ENOMEM; 342 goto bad; 343 } 344 345 taskqueue_start_threads(&crypto_tq, crypto_workers_num, PRI_MIN_KERN, 346 "crypto"); 347 348 error = kproc_create((void (*)(void *)) crypto_proc, NULL, 349 &cryptoproc, 0, 0, "crypto"); 350 if (error) { 351 printf("crypto_init: cannot start crypto thread; error %d", 352 error); 353 goto bad; 354 } 355 356 crypto_ret_workers = malloc(crypto_workers_num * sizeof(struct crypto_ret_worker), 357 M_CRYPTO_DATA, M_NOWAIT|M_ZERO); 358 if (crypto_ret_workers == NULL) { 359 error = ENOMEM; 360 printf("crypto_init: cannot allocate ret workers\n"); 361 goto bad; 362 } 363 364 365 FOREACH_CRYPTO_RETW(ret_worker) { 366 TAILQ_INIT(&ret_worker->crp_ordered_ret_q); 367 TAILQ_INIT(&ret_worker->crp_ret_q); 368 TAILQ_INIT(&ret_worker->crp_ret_kq); 369 370 ret_worker->reorder_ops = 0; 371 ret_worker->reorder_cur_seq = 0; 372 373 mtx_init(&ret_worker->crypto_ret_mtx, "crypto", "crypto return queues", MTX_DEF); 374 375 error = kproc_create((void (*)(void *)) crypto_ret_proc, ret_worker, 376 &ret_worker->cryptoretproc, 0, 0, "crypto returns %td", CRYPTO_RETW_ID(ret_worker)); 377 if (error) { 378 printf("crypto_init: cannot start cryptoret thread; error %d", 379 error); 380 goto bad; 381 } 382 } 383 384 keybuf_init(); 385 386 return 0; 387 bad: 388 crypto_destroy(); 389 return error; 390 } 391 392 /* 393 * Signal a crypto thread to terminate. We use the driver 394 * table lock to synchronize the sleep/wakeups so that we 395 * are sure the threads have terminated before we release 396 * the data structures they use. See crypto_finis below 397 * for the other half of this song-and-dance. 398 */ 399 static void 400 crypto_terminate(struct proc **pp, void *q) 401 { 402 struct proc *p; 403 404 mtx_assert(&crypto_drivers_mtx, MA_OWNED); 405 p = *pp; 406 *pp = NULL; 407 if (p) { 408 wakeup_one(q); 409 PROC_LOCK(p); /* NB: insure we don't miss wakeup */ 410 CRYPTO_DRIVER_UNLOCK(); /* let crypto_finis progress */ 411 msleep(p, &p->p_mtx, PWAIT, "crypto_destroy", 0); 412 PROC_UNLOCK(p); 413 CRYPTO_DRIVER_LOCK(); 414 } 415 } 416 417 static void 418 hmac_init_pad(struct auth_hash *axf, const char *key, int klen, void *auth_ctx, 419 uint8_t padval) 420 { 421 uint8_t hmac_key[HMAC_MAX_BLOCK_LEN]; 422 u_int i; 423 424 KASSERT(axf->blocksize <= sizeof(hmac_key), 425 ("Invalid HMAC block size %d", axf->blocksize)); 426 427 /* 428 * If the key is larger than the block size, use the digest of 429 * the key as the key instead. 430 */ 431 memset(hmac_key, 0, sizeof(hmac_key)); 432 if (klen > axf->blocksize) { 433 axf->Init(auth_ctx); 434 axf->Update(auth_ctx, key, klen); 435 axf->Final(hmac_key, auth_ctx); 436 klen = axf->hashsize; 437 } else 438 memcpy(hmac_key, key, klen); 439 440 for (i = 0; i < axf->blocksize; i++) 441 hmac_key[i] ^= padval; 442 443 axf->Init(auth_ctx); 444 axf->Update(auth_ctx, hmac_key, axf->blocksize); 445 } 446 447 void 448 hmac_init_ipad(struct auth_hash *axf, const char *key, int klen, 449 void *auth_ctx) 450 { 451 452 hmac_init_pad(axf, key, klen, auth_ctx, HMAC_IPAD_VAL); 453 } 454 455 void 456 hmac_init_opad(struct auth_hash *axf, const char *key, int klen, 457 void *auth_ctx) 458 { 459 460 hmac_init_pad(axf, key, klen, auth_ctx, HMAC_OPAD_VAL); 461 } 462 463 static void 464 crypto_destroy(void) 465 { 466 struct crypto_ret_worker *ret_worker; 467 int i; 468 469 /* 470 * Terminate any crypto threads. 471 */ 472 if (crypto_tq != NULL) 473 taskqueue_drain_all(crypto_tq); 474 CRYPTO_DRIVER_LOCK(); 475 crypto_terminate(&cryptoproc, &crp_q); 476 FOREACH_CRYPTO_RETW(ret_worker) 477 crypto_terminate(&ret_worker->cryptoretproc, &ret_worker->crp_ret_q); 478 CRYPTO_DRIVER_UNLOCK(); 479 480 /* XXX flush queues??? */ 481 482 /* 483 * Reclaim dynamically allocated resources. 484 */ 485 for (i = 0; i < crypto_drivers_size; i++) { 486 if (crypto_drivers[i] != NULL) 487 cap_rele(crypto_drivers[i]); 488 } 489 free(crypto_drivers, M_CRYPTO_DATA); 490 491 if (cryptoses_zone != NULL) 492 uma_zdestroy(cryptoses_zone); 493 if (cryptop_zone != NULL) 494 uma_zdestroy(cryptop_zone); 495 mtx_destroy(&crypto_q_mtx); 496 FOREACH_CRYPTO_RETW(ret_worker) 497 mtx_destroy(&ret_worker->crypto_ret_mtx); 498 free(crypto_ret_workers, M_CRYPTO_DATA); 499 if (crypto_tq != NULL) 500 taskqueue_free(crypto_tq); 501 mtx_destroy(&crypto_drivers_mtx); 502 } 503 504 uint32_t 505 crypto_ses2hid(crypto_session_t crypto_session) 506 { 507 return (crypto_session->cap->cc_hid); 508 } 509 510 uint32_t 511 crypto_ses2caps(crypto_session_t crypto_session) 512 { 513 return (crypto_session->cap->cc_flags & 0xff000000); 514 } 515 516 void * 517 crypto_get_driver_session(crypto_session_t crypto_session) 518 { 519 return (crypto_session->softc); 520 } 521 522 const struct crypto_session_params * 523 crypto_get_params(crypto_session_t crypto_session) 524 { 525 return (&crypto_session->csp); 526 } 527 528 struct auth_hash * 529 crypto_auth_hash(const struct crypto_session_params *csp) 530 { 531 532 switch (csp->csp_auth_alg) { 533 case CRYPTO_MD5_HMAC: 534 return (&auth_hash_hmac_md5); 535 case CRYPTO_SHA1_HMAC: 536 return (&auth_hash_hmac_sha1); 537 case CRYPTO_SHA2_224_HMAC: 538 return (&auth_hash_hmac_sha2_224); 539 case CRYPTO_SHA2_256_HMAC: 540 return (&auth_hash_hmac_sha2_256); 541 case CRYPTO_SHA2_384_HMAC: 542 return (&auth_hash_hmac_sha2_384); 543 case CRYPTO_SHA2_512_HMAC: 544 return (&auth_hash_hmac_sha2_512); 545 case CRYPTO_NULL_HMAC: 546 return (&auth_hash_null); 547 case CRYPTO_RIPEMD160_HMAC: 548 return (&auth_hash_hmac_ripemd_160); 549 case CRYPTO_MD5_KPDK: 550 return (&auth_hash_key_md5); 551 case CRYPTO_SHA1_KPDK: 552 return (&auth_hash_key_sha1); 553 #ifdef notyet 554 case CRYPTO_MD5: 555 return (&auth_hash_md5); 556 #endif 557 case CRYPTO_SHA1: 558 return (&auth_hash_sha1); 559 case CRYPTO_SHA2_224: 560 return (&auth_hash_sha2_224); 561 case CRYPTO_SHA2_256: 562 return (&auth_hash_sha2_256); 563 case CRYPTO_SHA2_384: 564 return (&auth_hash_sha2_384); 565 case CRYPTO_SHA2_512: 566 return (&auth_hash_sha2_512); 567 case CRYPTO_AES_NIST_GMAC: 568 switch (csp->csp_auth_klen) { 569 case 128 / 8: 570 return (&auth_hash_nist_gmac_aes_128); 571 case 192 / 8: 572 return (&auth_hash_nist_gmac_aes_192); 573 case 256 / 8: 574 return (&auth_hash_nist_gmac_aes_256); 575 default: 576 return (NULL); 577 } 578 case CRYPTO_BLAKE2B: 579 return (&auth_hash_blake2b); 580 case CRYPTO_BLAKE2S: 581 return (&auth_hash_blake2s); 582 case CRYPTO_POLY1305: 583 return (&auth_hash_poly1305); 584 case CRYPTO_AES_CCM_CBC_MAC: 585 switch (csp->csp_auth_klen) { 586 case 128 / 8: 587 return (&auth_hash_ccm_cbc_mac_128); 588 case 192 / 8: 589 return (&auth_hash_ccm_cbc_mac_192); 590 case 256 / 8: 591 return (&auth_hash_ccm_cbc_mac_256); 592 default: 593 return (NULL); 594 } 595 default: 596 return (NULL); 597 } 598 } 599 600 struct enc_xform * 601 crypto_cipher(const struct crypto_session_params *csp) 602 { 603 604 switch (csp->csp_cipher_alg) { 605 case CRYPTO_DES_CBC: 606 return (&enc_xform_des); 607 case CRYPTO_3DES_CBC: 608 return (&enc_xform_3des); 609 case CRYPTO_BLF_CBC: 610 return (&enc_xform_blf); 611 case CRYPTO_CAST_CBC: 612 return (&enc_xform_cast5); 613 case CRYPTO_SKIPJACK_CBC: 614 return (&enc_xform_skipjack); 615 case CRYPTO_RIJNDAEL128_CBC: 616 return (&enc_xform_rijndael128); 617 case CRYPTO_AES_XTS: 618 return (&enc_xform_aes_xts); 619 case CRYPTO_AES_ICM: 620 return (&enc_xform_aes_icm); 621 case CRYPTO_AES_NIST_GCM_16: 622 return (&enc_xform_aes_nist_gcm); 623 case CRYPTO_CAMELLIA_CBC: 624 return (&enc_xform_camellia); 625 case CRYPTO_NULL_CBC: 626 return (&enc_xform_null); 627 case CRYPTO_CHACHA20: 628 return (&enc_xform_chacha20); 629 case CRYPTO_AES_CCM_16: 630 return (&enc_xform_ccm); 631 default: 632 return (NULL); 633 } 634 } 635 636 static struct cryptocap * 637 crypto_checkdriver(u_int32_t hid) 638 { 639 640 return (hid >= crypto_drivers_size ? NULL : crypto_drivers[hid]); 641 } 642 643 /* 644 * Select a driver for a new session that supports the specified 645 * algorithms and, optionally, is constrained according to the flags. 646 */ 647 static struct cryptocap * 648 crypto_select_driver(const struct crypto_session_params *csp, int flags) 649 { 650 struct cryptocap *cap, *best; 651 int best_match, error, hid; 652 653 CRYPTO_DRIVER_ASSERT(); 654 655 best = NULL; 656 for (hid = 0; hid < crypto_drivers_size; hid++) { 657 /* 658 * If there is no driver for this slot, or the driver 659 * is not appropriate (hardware or software based on 660 * match), then skip. 661 */ 662 cap = crypto_drivers[hid]; 663 if (cap == NULL || 664 (cap->cc_flags & flags) == 0) 665 continue; 666 667 error = CRYPTODEV_PROBESESSION(cap->cc_dev, csp); 668 if (error >= 0) 669 continue; 670 671 /* 672 * Use the driver with the highest probe value. 673 * Hardware drivers use a higher probe value than 674 * software. In case of a tie, prefer the driver with 675 * the fewest active sessions. 676 */ 677 if (best == NULL || error > best_match || 678 (error == best_match && 679 cap->cc_sessions < best->cc_sessions)) { 680 best = cap; 681 best_match = error; 682 } 683 } 684 return best; 685 } 686 687 static bool 688 alg_is_compression(int alg) 689 { 690 691 if (alg == CRYPTO_DEFLATE_COMP) 692 return (true); 693 return (false); 694 } 695 696 static bool 697 alg_is_cipher(int alg) 698 { 699 700 if (alg >= CRYPTO_DES_CBC && alg <= CRYPTO_SKIPJACK_CBC) 701 return (true); 702 if (alg >= CRYPTO_AES_CBC && alg <= CRYPTO_ARC4) 703 return (true); 704 if (alg == CRYPTO_NULL_CBC) 705 return (true); 706 if (alg >= CRYPTO_CAMELLIA_CBC && alg <= CRYPTO_AES_ICM) 707 return (true); 708 if (alg == CRYPTO_CHACHA20) 709 return (true); 710 return (false); 711 } 712 713 static bool 714 alg_is_digest(int alg) 715 { 716 717 if (alg >= CRYPTO_MD5_HMAC && alg <= CRYPTO_SHA1_KPDK) 718 return (true); 719 if (alg >= CRYPTO_MD5 && alg <= CRYPTO_SHA1) 720 return (true); 721 if (alg == CRYPTO_NULL_HMAC) 722 return (true); 723 if (alg >= CRYPTO_SHA2_256_HMAC && alg <= CRYPTO_SHA2_512_HMAC) 724 return (true); 725 if (alg == CRYPTO_AES_NIST_GMAC) 726 return (true); 727 if (alg >= CRYPTO_BLAKE2B && alg <= CRYPTO_BLAKE2S) 728 return (true); 729 if (alg >= CRYPTO_SHA2_224_HMAC && alg <= CRYPTO_POLY1305) 730 return (true); 731 if (alg == CRYPTO_AES_CCM_CBC_MAC) 732 return (true); 733 return (false); 734 } 735 736 static bool 737 alg_is_keyed_digest(int alg) 738 { 739 740 if (alg >= CRYPTO_MD5_HMAC && alg <= CRYPTO_SHA1_KPDK) 741 return (true); 742 if (alg >= CRYPTO_SHA2_256_HMAC && alg <= CRYPTO_SHA2_512_HMAC) 743 return (true); 744 if (alg == CRYPTO_AES_NIST_GMAC) 745 return (true); 746 if (alg >= CRYPTO_BLAKE2B && alg <= CRYPTO_BLAKE2S) 747 return (true); 748 if (alg == CRYPTO_SHA2_224_HMAC) 749 return (true); 750 if (alg == CRYPTO_POLY1305) 751 return (true); 752 if (alg == CRYPTO_AES_CCM_CBC_MAC) 753 return (true); 754 return (false); 755 } 756 757 static bool 758 alg_is_aead(int alg) 759 { 760 761 if (alg == CRYPTO_AES_NIST_GCM_16) 762 return (true); 763 if (alg == CRYPTO_AES_CCM_16) 764 return (true); 765 return (false); 766 } 767 768 /* Various sanity checks on crypto session parameters. */ 769 static bool 770 check_csp(const struct crypto_session_params *csp) 771 { 772 struct auth_hash *axf; 773 774 /* Mode-independent checks. */ 775 if (csp->csp_flags != 0) 776 return (false); 777 if (csp->csp_ivlen < 0 || csp->csp_cipher_klen < 0 || 778 csp->csp_auth_klen < 0 || csp->csp_auth_mlen < 0) 779 return (false); 780 if (csp->csp_auth_key != NULL && csp->csp_auth_klen == 0) 781 return (false); 782 if (csp->csp_cipher_key != NULL && csp->csp_cipher_klen == 0) 783 return (false); 784 785 switch (csp->csp_mode) { 786 case CSP_MODE_COMPRESS: 787 if (!alg_is_compression(csp->csp_cipher_alg)) 788 return (false); 789 if (csp->csp_flags != 0) 790 return (false); 791 if (csp->csp_cipher_klen != 0 || csp->csp_ivlen != 0 || 792 csp->csp_auth_alg != 0 || csp->csp_auth_klen != 0 || 793 csp->csp_auth_mlen != 0) 794 return (false); 795 break; 796 case CSP_MODE_CIPHER: 797 if (!alg_is_cipher(csp->csp_cipher_alg)) 798 return (false); 799 if (csp->csp_cipher_alg != CRYPTO_NULL_CBC) { 800 if (csp->csp_cipher_klen == 0) 801 return (false); 802 if (csp->csp_cipher_alg != CRYPTO_ARC4) { 803 if (csp->csp_ivlen == 0) 804 return (false); 805 } 806 } 807 if (csp->csp_ivlen >= EALG_MAX_BLOCK_LEN) 808 return (false); 809 if (csp->csp_auth_alg != 0 || csp->csp_auth_klen != 0 || 810 csp->csp_auth_mlen != 0) 811 return (false); 812 break; 813 case CSP_MODE_DIGEST: 814 if (csp->csp_cipher_alg != 0 || csp->csp_cipher_klen != 0) 815 return (false); 816 817 /* IV is optional for digests (e.g. GMAC). */ 818 if (csp->csp_ivlen >= EALG_MAX_BLOCK_LEN) 819 return (false); 820 if (!alg_is_digest(csp->csp_auth_alg)) 821 return (false); 822 823 /* Key is optional for BLAKE2 digests. */ 824 if (csp->csp_auth_alg == CRYPTO_BLAKE2B || 825 csp->csp_auth_alg == CRYPTO_BLAKE2S) 826 ; 827 else if (alg_is_keyed_digest(csp->csp_auth_alg)) { 828 if (csp->csp_auth_klen == 0) 829 return (false); 830 } else { 831 if (csp->csp_auth_klen != 0) 832 return (false); 833 } 834 if (csp->csp_auth_mlen != 0) { 835 axf = crypto_auth_hash(csp); 836 if (axf == NULL || csp->csp_auth_mlen > axf->hashsize) 837 return (false); 838 } 839 break; 840 case CSP_MODE_AEAD: 841 if (!alg_is_aead(csp->csp_cipher_alg)) 842 return (false); 843 if (csp->csp_cipher_klen == 0) 844 return (false); 845 if (csp->csp_ivlen == 0 || 846 csp->csp_ivlen >= EALG_MAX_BLOCK_LEN) 847 return (false); 848 if (csp->csp_auth_alg != 0 || csp->csp_auth_klen != 0) 849 return (false); 850 851 /* 852 * XXX: Would be nice to have a better way to get this 853 * value. 854 */ 855 switch (csp->csp_cipher_alg) { 856 case CRYPTO_AES_NIST_GCM_16: 857 case CRYPTO_AES_CCM_16: 858 if (csp->csp_auth_mlen > 16) 859 return (false); 860 break; 861 } 862 break; 863 case CSP_MODE_ETA: 864 if (!alg_is_cipher(csp->csp_cipher_alg)) 865 return (false); 866 if (csp->csp_cipher_alg != CRYPTO_NULL_CBC) { 867 if (csp->csp_cipher_klen == 0) 868 return (false); 869 if (csp->csp_cipher_alg != CRYPTO_ARC4) { 870 if (csp->csp_ivlen == 0) 871 return (false); 872 } 873 } 874 if (csp->csp_ivlen >= EALG_MAX_BLOCK_LEN) 875 return (false); 876 if (!alg_is_digest(csp->csp_auth_alg)) 877 return (false); 878 879 /* Key is optional for BLAKE2 digests. */ 880 if (csp->csp_auth_alg == CRYPTO_BLAKE2B || 881 csp->csp_auth_alg == CRYPTO_BLAKE2S) 882 ; 883 else if (alg_is_keyed_digest(csp->csp_auth_alg)) { 884 if (csp->csp_auth_klen == 0) 885 return (false); 886 } else { 887 if (csp->csp_auth_klen != 0) 888 return (false); 889 } 890 if (csp->csp_auth_mlen != 0) { 891 axf = crypto_auth_hash(csp); 892 if (axf == NULL || csp->csp_auth_mlen > axf->hashsize) 893 return (false); 894 } 895 break; 896 default: 897 return (false); 898 } 899 900 return (true); 901 } 902 903 /* 904 * Delete a session after it has been detached from its driver. 905 */ 906 static void 907 crypto_deletesession(crypto_session_t cses) 908 { 909 struct cryptocap *cap; 910 911 cap = cses->cap; 912 913 explicit_bzero(cses->softc, cap->cc_session_size); 914 free(cses->softc, M_CRYPTO_DATA); 915 uma_zfree(cryptoses_zone, cses); 916 917 CRYPTO_DRIVER_LOCK(); 918 cap->cc_sessions--; 919 if (cap->cc_sessions == 0 && cap->cc_flags & CRYPTOCAP_F_CLEANUP) 920 wakeup(cap); 921 CRYPTO_DRIVER_UNLOCK(); 922 cap_rele(cap); 923 } 924 925 /* 926 * Create a new session. The crid argument specifies a crypto 927 * driver to use or constraints on a driver to select (hardware 928 * only, software only, either). Whatever driver is selected 929 * must be capable of the requested crypto algorithms. 930 */ 931 int 932 crypto_newsession(crypto_session_t *cses, 933 const struct crypto_session_params *csp, int crid) 934 { 935 crypto_session_t res; 936 struct cryptocap *cap; 937 int err; 938 939 if (!check_csp(csp)) 940 return (EINVAL); 941 942 res = NULL; 943 944 CRYPTO_DRIVER_LOCK(); 945 if ((crid & (CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE)) == 0) { 946 /* 947 * Use specified driver; verify it is capable. 948 */ 949 cap = crypto_checkdriver(crid); 950 if (cap != NULL && CRYPTODEV_PROBESESSION(cap->cc_dev, csp) > 0) 951 cap = NULL; 952 } else { 953 /* 954 * No requested driver; select based on crid flags. 955 */ 956 cap = crypto_select_driver(csp, crid); 957 } 958 if (cap == NULL) { 959 CRYPTO_DRIVER_UNLOCK(); 960 CRYPTDEB("no driver"); 961 return (EOPNOTSUPP); 962 } 963 cap_ref(cap); 964 cap->cc_sessions++; 965 CRYPTO_DRIVER_UNLOCK(); 966 967 res = uma_zalloc(cryptoses_zone, M_WAITOK | M_ZERO); 968 res->cap = cap; 969 res->softc = malloc(cap->cc_session_size, M_CRYPTO_DATA, M_WAITOK | 970 M_ZERO); 971 res->csp = *csp; 972 973 /* Call the driver initialization routine. */ 974 err = CRYPTODEV_NEWSESSION(cap->cc_dev, res, csp); 975 if (err != 0) { 976 CRYPTDEB("dev newsession failed: %d", err); 977 crypto_deletesession(res); 978 return (err); 979 } 980 981 *cses = res; 982 return (0); 983 } 984 985 /* 986 * Delete an existing session (or a reserved session on an unregistered 987 * driver). 988 */ 989 void 990 crypto_freesession(crypto_session_t cses) 991 { 992 struct cryptocap *cap; 993 994 if (cses == NULL) 995 return; 996 997 cap = cses->cap; 998 999 /* Call the driver cleanup routine, if available. */ 1000 CRYPTODEV_FREESESSION(cap->cc_dev, cses); 1001 1002 crypto_deletesession(cses); 1003 } 1004 1005 /* 1006 * Return a new driver id. Registers a driver with the system so that 1007 * it can be probed by subsequent sessions. 1008 */ 1009 int32_t 1010 crypto_get_driverid(device_t dev, size_t sessionsize, int flags) 1011 { 1012 struct cryptocap *cap, **newdrv; 1013 int i; 1014 1015 if ((flags & (CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE)) == 0) { 1016 device_printf(dev, 1017 "no flags specified when registering driver\n"); 1018 return -1; 1019 } 1020 1021 cap = malloc(sizeof(*cap), M_CRYPTO_DATA, M_WAITOK | M_ZERO); 1022 cap->cc_dev = dev; 1023 cap->cc_session_size = sessionsize; 1024 cap->cc_flags = flags; 1025 refcount_init(&cap->cc_refs, 1); 1026 1027 CRYPTO_DRIVER_LOCK(); 1028 for (;;) { 1029 for (i = 0; i < crypto_drivers_size; i++) { 1030 if (crypto_drivers[i] == NULL) 1031 break; 1032 } 1033 1034 if (i < crypto_drivers_size) 1035 break; 1036 1037 /* Out of entries, allocate some more. */ 1038 1039 if (2 * crypto_drivers_size <= crypto_drivers_size) { 1040 CRYPTO_DRIVER_UNLOCK(); 1041 printf("crypto: driver count wraparound!\n"); 1042 cap_rele(cap); 1043 return (-1); 1044 } 1045 CRYPTO_DRIVER_UNLOCK(); 1046 1047 newdrv = malloc(2 * crypto_drivers_size * 1048 sizeof(*crypto_drivers), M_CRYPTO_DATA, M_WAITOK | M_ZERO); 1049 1050 CRYPTO_DRIVER_LOCK(); 1051 memcpy(newdrv, crypto_drivers, 1052 crypto_drivers_size * sizeof(*crypto_drivers)); 1053 1054 crypto_drivers_size *= 2; 1055 1056 free(crypto_drivers, M_CRYPTO_DATA); 1057 crypto_drivers = newdrv; 1058 } 1059 1060 cap->cc_hid = i; 1061 crypto_drivers[i] = cap; 1062 CRYPTO_DRIVER_UNLOCK(); 1063 1064 if (bootverbose) 1065 printf("crypto: assign %s driver id %u, flags 0x%x\n", 1066 device_get_nameunit(dev), i, flags); 1067 1068 return i; 1069 } 1070 1071 /* 1072 * Lookup a driver by name. We match against the full device 1073 * name and unit, and against just the name. The latter gives 1074 * us a simple widlcarding by device name. On success return the 1075 * driver/hardware identifier; otherwise return -1. 1076 */ 1077 int 1078 crypto_find_driver(const char *match) 1079 { 1080 struct cryptocap *cap; 1081 int i, len = strlen(match); 1082 1083 CRYPTO_DRIVER_LOCK(); 1084 for (i = 0; i < crypto_drivers_size; i++) { 1085 if (crypto_drivers[i] == NULL) 1086 continue; 1087 cap = crypto_drivers[i]; 1088 if (strncmp(match, device_get_nameunit(cap->cc_dev), len) == 0 || 1089 strncmp(match, device_get_name(cap->cc_dev), len) == 0) { 1090 CRYPTO_DRIVER_UNLOCK(); 1091 return (i); 1092 } 1093 } 1094 CRYPTO_DRIVER_UNLOCK(); 1095 return (-1); 1096 } 1097 1098 /* 1099 * Return the device_t for the specified driver or NULL 1100 * if the driver identifier is invalid. 1101 */ 1102 device_t 1103 crypto_find_device_byhid(int hid) 1104 { 1105 struct cryptocap *cap; 1106 device_t dev; 1107 1108 dev = NULL; 1109 CRYPTO_DRIVER_LOCK(); 1110 cap = crypto_checkdriver(hid); 1111 if (cap != NULL) 1112 dev = cap->cc_dev; 1113 CRYPTO_DRIVER_UNLOCK(); 1114 return (dev); 1115 } 1116 1117 /* 1118 * Return the device/driver capabilities. 1119 */ 1120 int 1121 crypto_getcaps(int hid) 1122 { 1123 struct cryptocap *cap; 1124 int flags; 1125 1126 flags = 0; 1127 CRYPTO_DRIVER_LOCK(); 1128 cap = crypto_checkdriver(hid); 1129 if (cap != NULL) 1130 flags = cap->cc_flags; 1131 CRYPTO_DRIVER_UNLOCK(); 1132 return (flags); 1133 } 1134 1135 /* 1136 * Register support for a key-related algorithm. This routine 1137 * is called once for each algorithm supported a driver. 1138 */ 1139 int 1140 crypto_kregister(u_int32_t driverid, int kalg, u_int32_t flags) 1141 { 1142 struct cryptocap *cap; 1143 int err; 1144 1145 CRYPTO_DRIVER_LOCK(); 1146 1147 cap = crypto_checkdriver(driverid); 1148 if (cap != NULL && 1149 (CRK_ALGORITM_MIN <= kalg && kalg <= CRK_ALGORITHM_MAX)) { 1150 /* 1151 * XXX Do some performance testing to determine placing. 1152 * XXX We probably need an auxiliary data structure that 1153 * XXX describes relative performances. 1154 */ 1155 1156 cap->cc_kalg[kalg] = flags | CRYPTO_ALG_FLAG_SUPPORTED; 1157 if (bootverbose) 1158 printf("crypto: %s registers key alg %u flags %u\n" 1159 , device_get_nameunit(cap->cc_dev) 1160 , kalg 1161 , flags 1162 ); 1163 err = 0; 1164 } else 1165 err = EINVAL; 1166 1167 CRYPTO_DRIVER_UNLOCK(); 1168 return err; 1169 } 1170 1171 /* 1172 * Unregister all algorithms associated with a crypto driver. 1173 * If there are pending sessions using it, leave enough information 1174 * around so that subsequent calls using those sessions will 1175 * correctly detect the driver has been unregistered and reroute 1176 * requests. 1177 */ 1178 int 1179 crypto_unregister_all(u_int32_t driverid) 1180 { 1181 struct cryptocap *cap; 1182 1183 CRYPTO_DRIVER_LOCK(); 1184 cap = crypto_checkdriver(driverid); 1185 if (cap == NULL) { 1186 CRYPTO_DRIVER_UNLOCK(); 1187 return (EINVAL); 1188 } 1189 1190 cap->cc_flags |= CRYPTOCAP_F_CLEANUP; 1191 crypto_drivers[driverid] = NULL; 1192 1193 /* 1194 * XXX: This doesn't do anything to kick sessions that 1195 * have no pending operations. 1196 */ 1197 while (cap->cc_sessions != 0 || cap->cc_koperations != 0) 1198 mtx_sleep(cap, &crypto_drivers_mtx, 0, "cryunreg", 0); 1199 CRYPTO_DRIVER_UNLOCK(); 1200 cap_rele(cap); 1201 1202 return (0); 1203 } 1204 1205 /* 1206 * Clear blockage on a driver. The what parameter indicates whether 1207 * the driver is now ready for cryptop's and/or cryptokop's. 1208 */ 1209 int 1210 crypto_unblock(u_int32_t driverid, int what) 1211 { 1212 struct cryptocap *cap; 1213 int err; 1214 1215 CRYPTO_Q_LOCK(); 1216 cap = crypto_checkdriver(driverid); 1217 if (cap != NULL) { 1218 if (what & CRYPTO_SYMQ) 1219 cap->cc_qblocked = 0; 1220 if (what & CRYPTO_ASYMQ) 1221 cap->cc_kqblocked = 0; 1222 if (crp_sleep) 1223 wakeup_one(&crp_q); 1224 err = 0; 1225 } else 1226 err = EINVAL; 1227 CRYPTO_Q_UNLOCK(); 1228 1229 return err; 1230 } 1231 1232 #ifdef INVARIANTS 1233 /* Various sanity checks on crypto requests. */ 1234 static void 1235 crp_sanity(struct cryptop *crp) 1236 { 1237 struct crypto_session_params *csp; 1238 1239 KASSERT(crp->crp_session != NULL, ("incoming crp without a session")); 1240 KASSERT(crp->crp_ilen >= 0, ("incoming crp with -ve input length")); 1241 KASSERT(crp->crp_etype == 0, ("incoming crp with error")); 1242 KASSERT(!(crp->crp_flags & CRYPTO_F_DONE), 1243 ("incoming crp already done")); 1244 1245 csp = &crp->crp_session->csp; 1246 switch (csp->csp_mode) { 1247 case CSP_MODE_COMPRESS: 1248 KASSERT(crp->crp_op == CRYPTO_OP_COMPRESS || 1249 crp->crp_op == CRYPTO_OP_DECOMPRESS, 1250 ("invalid compression op %x", crp->crp_op)); 1251 break; 1252 case CSP_MODE_CIPHER: 1253 KASSERT(crp->crp_op == CRYPTO_OP_ENCRYPT || 1254 crp->crp_op == CRYPTO_OP_DECRYPT, 1255 ("invalid cipher op %x", crp->crp_op)); 1256 break; 1257 case CSP_MODE_DIGEST: 1258 KASSERT(crp->crp_op == CRYPTO_OP_COMPUTE_DIGEST || 1259 crp->crp_op == CRYPTO_OP_VERIFY_DIGEST, 1260 ("invalid digest op %x", crp->crp_op)); 1261 break; 1262 case CSP_MODE_AEAD: 1263 KASSERT(crp->crp_op == 1264 (CRYPTO_OP_ENCRYPT | CRYPTO_OP_COMPUTE_DIGEST) || 1265 crp->crp_op == 1266 (CRYPTO_OP_DECRYPT | CRYPTO_OP_VERIFY_DIGEST), 1267 ("invalid AEAD op %x", crp->crp_op)); 1268 if (csp->csp_cipher_alg == CRYPTO_AES_NIST_GCM_16) 1269 KASSERT(crp->crp_flags & CRYPTO_F_IV_SEPARATE, 1270 ("GCM without a separate IV")); 1271 if (csp->csp_cipher_alg == CRYPTO_AES_CCM_16) 1272 KASSERT(crp->crp_flags & CRYPTO_F_IV_SEPARATE, 1273 ("CCM without a separate IV")); 1274 break; 1275 case CSP_MODE_ETA: 1276 KASSERT(crp->crp_op == 1277 (CRYPTO_OP_ENCRYPT | CRYPTO_OP_COMPUTE_DIGEST) || 1278 crp->crp_op == 1279 (CRYPTO_OP_DECRYPT | CRYPTO_OP_VERIFY_DIGEST), 1280 ("invalid ETA op %x", crp->crp_op)); 1281 break; 1282 } 1283 KASSERT(crp->crp_buf_type >= CRYPTO_BUF_CONTIG && 1284 crp->crp_buf_type <= CRYPTO_BUF_MBUF, 1285 ("invalid crp buffer type %d", crp->crp_buf_type)); 1286 if (csp->csp_mode == CSP_MODE_AEAD || csp->csp_mode == CSP_MODE_ETA) { 1287 KASSERT(crp->crp_aad_start == 0 || 1288 crp->crp_aad_start < crp->crp_ilen, 1289 ("invalid AAD start")); 1290 KASSERT(crp->crp_aad_length != 0 || crp->crp_aad_start == 0, 1291 ("AAD with zero length and non-zero start")); 1292 KASSERT(crp->crp_aad_length == 0 || 1293 crp->crp_aad_start + crp->crp_aad_length <= crp->crp_ilen, 1294 ("AAD outside input length")); 1295 } else { 1296 KASSERT(crp->crp_aad_start == 0 && crp->crp_aad_length == 0, 1297 ("AAD region in request not supporting AAD")); 1298 } 1299 if (csp->csp_ivlen == 0) { 1300 KASSERT((crp->crp_flags & CRYPTO_F_IV_SEPARATE) == 0, 1301 ("IV_SEPARATE set when IV isn't used")); 1302 KASSERT(crp->crp_iv_start == 0, 1303 ("crp_iv_start set when IV isn't used")); 1304 } else if (crp->crp_flags & CRYPTO_F_IV_SEPARATE) { 1305 KASSERT(crp->crp_iv_start == 0, 1306 ("IV_SEPARATE used with non-zero IV start")); 1307 } else { 1308 KASSERT(crp->crp_iv_start < crp->crp_ilen, 1309 ("invalid IV start")); 1310 KASSERT(crp->crp_iv_start + csp->csp_ivlen <= crp->crp_ilen, 1311 ("IV outside input length")); 1312 } 1313 KASSERT(crp->crp_payload_start == 0 || 1314 crp->crp_payload_start < crp->crp_ilen, 1315 ("invalid payload start")); 1316 KASSERT(crp->crp_payload_start + crp->crp_payload_length <= 1317 crp->crp_ilen, ("payload outside input length")); 1318 if (csp->csp_mode == CSP_MODE_DIGEST || 1319 csp->csp_mode == CSP_MODE_AEAD || csp->csp_mode == CSP_MODE_ETA) { 1320 KASSERT(crp->crp_digest_start == 0 || 1321 crp->crp_digest_start < crp->crp_ilen, 1322 ("invalid digest start")); 1323 /* XXX: For the mlen == 0 case this check isn't perfect. */ 1324 KASSERT(crp->crp_digest_start + csp->csp_auth_mlen <= 1325 crp->crp_ilen, 1326 ("digest outside input length")); 1327 } else { 1328 KASSERT(crp->crp_digest_start == 0, 1329 ("non-zero digest start for request without a digest")); 1330 } 1331 if (csp->csp_cipher_klen != 0) 1332 KASSERT(csp->csp_cipher_key != NULL || 1333 crp->crp_cipher_key != NULL, 1334 ("cipher request without a key")); 1335 if (csp->csp_auth_klen != 0) 1336 KASSERT(csp->csp_auth_key != NULL || crp->crp_auth_key != NULL, 1337 ("auth request without a key")); 1338 KASSERT(crp->crp_callback != NULL, ("incoming crp without callback")); 1339 } 1340 #endif 1341 1342 /* 1343 * Add a crypto request to a queue, to be processed by the kernel thread. 1344 */ 1345 int 1346 crypto_dispatch(struct cryptop *crp) 1347 { 1348 struct cryptocap *cap; 1349 int result; 1350 1351 #ifdef INVARIANTS 1352 crp_sanity(crp); 1353 #endif 1354 1355 cryptostats.cs_ops++; 1356 1357 #ifdef CRYPTO_TIMING 1358 if (crypto_timing) 1359 binuptime(&crp->crp_tstamp); 1360 #endif 1361 1362 crp->crp_retw_id = ((uintptr_t)crp->crp_session) % crypto_workers_num; 1363 1364 if (CRYPTOP_ASYNC(crp)) { 1365 if (crp->crp_flags & CRYPTO_F_ASYNC_KEEPORDER) { 1366 struct crypto_ret_worker *ret_worker; 1367 1368 ret_worker = CRYPTO_RETW(crp->crp_retw_id); 1369 1370 CRYPTO_RETW_LOCK(ret_worker); 1371 crp->crp_seq = ret_worker->reorder_ops++; 1372 CRYPTO_RETW_UNLOCK(ret_worker); 1373 } 1374 1375 TASK_INIT(&crp->crp_task, 0, crypto_task_invoke, crp); 1376 taskqueue_enqueue(crypto_tq, &crp->crp_task); 1377 return (0); 1378 } 1379 1380 if ((crp->crp_flags & CRYPTO_F_BATCH) == 0) { 1381 /* 1382 * Caller marked the request to be processed 1383 * immediately; dispatch it directly to the 1384 * driver unless the driver is currently blocked. 1385 */ 1386 cap = crp->crp_session->cap; 1387 if (!cap->cc_qblocked) { 1388 result = crypto_invoke(cap, crp, 0); 1389 if (result != ERESTART) 1390 return (result); 1391 /* 1392 * The driver ran out of resources, put the request on 1393 * the queue. 1394 */ 1395 } 1396 } 1397 crypto_batch_enqueue(crp); 1398 return 0; 1399 } 1400 1401 void 1402 crypto_batch_enqueue(struct cryptop *crp) 1403 { 1404 1405 CRYPTO_Q_LOCK(); 1406 TAILQ_INSERT_TAIL(&crp_q, crp, crp_next); 1407 if (crp_sleep) 1408 wakeup_one(&crp_q); 1409 CRYPTO_Q_UNLOCK(); 1410 } 1411 1412 /* 1413 * Add an asymetric crypto request to a queue, 1414 * to be processed by the kernel thread. 1415 */ 1416 int 1417 crypto_kdispatch(struct cryptkop *krp) 1418 { 1419 int error; 1420 1421 cryptostats.cs_kops++; 1422 1423 krp->krp_cap = NULL; 1424 error = crypto_kinvoke(krp); 1425 if (error == ERESTART) { 1426 CRYPTO_Q_LOCK(); 1427 TAILQ_INSERT_TAIL(&crp_kq, krp, krp_next); 1428 if (crp_sleep) 1429 wakeup_one(&crp_q); 1430 CRYPTO_Q_UNLOCK(); 1431 error = 0; 1432 } 1433 return error; 1434 } 1435 1436 /* 1437 * Verify a driver is suitable for the specified operation. 1438 */ 1439 static __inline int 1440 kdriver_suitable(const struct cryptocap *cap, const struct cryptkop *krp) 1441 { 1442 return (cap->cc_kalg[krp->krp_op] & CRYPTO_ALG_FLAG_SUPPORTED) != 0; 1443 } 1444 1445 /* 1446 * Select a driver for an asym operation. The driver must 1447 * support the necessary algorithm. The caller can constrain 1448 * which device is selected with the flags parameter. The 1449 * algorithm we use here is pretty stupid; just use the first 1450 * driver that supports the algorithms we need. If there are 1451 * multiple suitable drivers we choose the driver with the 1452 * fewest active operations. We prefer hardware-backed 1453 * drivers to software ones when either may be used. 1454 */ 1455 static struct cryptocap * 1456 crypto_select_kdriver(const struct cryptkop *krp, int flags) 1457 { 1458 struct cryptocap *cap, *best; 1459 int match, hid; 1460 1461 CRYPTO_DRIVER_ASSERT(); 1462 1463 /* 1464 * Look first for hardware crypto devices if permitted. 1465 */ 1466 if (flags & CRYPTOCAP_F_HARDWARE) 1467 match = CRYPTOCAP_F_HARDWARE; 1468 else 1469 match = CRYPTOCAP_F_SOFTWARE; 1470 best = NULL; 1471 again: 1472 for (hid = 0; hid < crypto_drivers_size; hid++) { 1473 /* 1474 * If there is no driver for this slot, or the driver 1475 * is not appropriate (hardware or software based on 1476 * match), then skip. 1477 */ 1478 cap = crypto_drivers[hid]; 1479 if (cap->cc_dev == NULL || 1480 (cap->cc_flags & match) == 0) 1481 continue; 1482 1483 /* verify all the algorithms are supported. */ 1484 if (kdriver_suitable(cap, krp)) { 1485 if (best == NULL || 1486 cap->cc_koperations < best->cc_koperations) 1487 best = cap; 1488 } 1489 } 1490 if (best != NULL) 1491 return best; 1492 if (match == CRYPTOCAP_F_HARDWARE && (flags & CRYPTOCAP_F_SOFTWARE)) { 1493 /* sort of an Algol 68-style for loop */ 1494 match = CRYPTOCAP_F_SOFTWARE; 1495 goto again; 1496 } 1497 return best; 1498 } 1499 1500 /* 1501 * Choose a driver for an asymmetric crypto request. 1502 */ 1503 static struct cryptocap * 1504 crypto_lookup_kdriver(struct cryptkop *krp) 1505 { 1506 struct cryptocap *cap; 1507 uint32_t crid; 1508 1509 /* If this request is requeued, it might already have a driver. */ 1510 cap = krp->krp_cap; 1511 if (cap != NULL) 1512 return (cap); 1513 1514 /* Use krp_crid to choose a driver. */ 1515 crid = krp->krp_crid; 1516 if ((crid & (CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE)) == 0) { 1517 cap = crypto_checkdriver(crid); 1518 if (cap != NULL) { 1519 /* 1520 * Driver present, it must support the 1521 * necessary algorithm and, if s/w drivers are 1522 * excluded, it must be registered as 1523 * hardware-backed. 1524 */ 1525 if (!kdriver_suitable(cap, krp) || 1526 (!crypto_devallowsoft && 1527 (cap->cc_flags & CRYPTOCAP_F_HARDWARE) == 0)) 1528 cap = NULL; 1529 } 1530 } else { 1531 /* 1532 * No requested driver; select based on crid flags. 1533 */ 1534 if (!crypto_devallowsoft) /* NB: disallow s/w drivers */ 1535 crid &= ~CRYPTOCAP_F_SOFTWARE; 1536 cap = crypto_select_kdriver(krp, crid); 1537 } 1538 1539 if (cap != NULL) { 1540 krp->krp_cap = cap_ref(cap); 1541 krp->krp_hid = cap->cc_hid; 1542 } 1543 return (cap); 1544 } 1545 1546 /* 1547 * Dispatch an asymmetric crypto request. 1548 */ 1549 static int 1550 crypto_kinvoke(struct cryptkop *krp) 1551 { 1552 struct cryptocap *cap = NULL; 1553 int error; 1554 1555 KASSERT(krp != NULL, ("%s: krp == NULL", __func__)); 1556 KASSERT(krp->krp_callback != NULL, 1557 ("%s: krp->crp_callback == NULL", __func__)); 1558 1559 CRYPTO_DRIVER_LOCK(); 1560 cap = crypto_lookup_kdriver(krp); 1561 if (cap == NULL) { 1562 CRYPTO_DRIVER_UNLOCK(); 1563 krp->krp_status = ENODEV; 1564 crypto_kdone(krp); 1565 return (0); 1566 } 1567 1568 /* 1569 * If the device is blocked, return ERESTART to requeue it. 1570 */ 1571 if (cap->cc_kqblocked) { 1572 /* 1573 * XXX: Previously this set krp_status to ERESTART and 1574 * invoked crypto_kdone but the caller would still 1575 * requeue it. 1576 */ 1577 CRYPTO_DRIVER_UNLOCK(); 1578 return (ERESTART); 1579 } 1580 1581 cap->cc_koperations++; 1582 CRYPTO_DRIVER_UNLOCK(); 1583 error = CRYPTODEV_KPROCESS(cap->cc_dev, krp, 0); 1584 if (error == ERESTART) { 1585 CRYPTO_DRIVER_LOCK(); 1586 cap->cc_koperations--; 1587 CRYPTO_DRIVER_UNLOCK(); 1588 return (error); 1589 } 1590 1591 KASSERT(error == 0, ("error %d returned from crypto_kprocess", error)); 1592 return (0); 1593 } 1594 1595 #ifdef CRYPTO_TIMING 1596 static void 1597 crypto_tstat(struct cryptotstat *ts, struct bintime *bt) 1598 { 1599 struct bintime now, delta; 1600 struct timespec t; 1601 uint64_t u; 1602 1603 binuptime(&now); 1604 u = now.frac; 1605 delta.frac = now.frac - bt->frac; 1606 delta.sec = now.sec - bt->sec; 1607 if (u < delta.frac) 1608 delta.sec--; 1609 bintime2timespec(&delta, &t); 1610 timespecadd(&ts->acc, &t, &ts->acc); 1611 if (timespeccmp(&t, &ts->min, <)) 1612 ts->min = t; 1613 if (timespeccmp(&t, &ts->max, >)) 1614 ts->max = t; 1615 ts->count++; 1616 1617 *bt = now; 1618 } 1619 #endif 1620 1621 static void 1622 crypto_task_invoke(void *ctx, int pending) 1623 { 1624 struct cryptocap *cap; 1625 struct cryptop *crp; 1626 int result; 1627 1628 crp = (struct cryptop *)ctx; 1629 cap = crp->crp_session->cap; 1630 result = crypto_invoke(cap, crp, 0); 1631 if (result == ERESTART) 1632 crypto_batch_enqueue(crp); 1633 } 1634 1635 /* 1636 * Dispatch a crypto request to the appropriate crypto devices. 1637 */ 1638 static int 1639 crypto_invoke(struct cryptocap *cap, struct cryptop *crp, int hint) 1640 { 1641 1642 KASSERT(crp != NULL, ("%s: crp == NULL", __func__)); 1643 KASSERT(crp->crp_callback != NULL, 1644 ("%s: crp->crp_callback == NULL", __func__)); 1645 KASSERT(crp->crp_session != NULL, 1646 ("%s: crp->crp_session == NULL", __func__)); 1647 1648 #ifdef CRYPTO_TIMING 1649 if (crypto_timing) 1650 crypto_tstat(&cryptostats.cs_invoke, &crp->crp_tstamp); 1651 #endif 1652 if (cap->cc_flags & CRYPTOCAP_F_CLEANUP) { 1653 struct crypto_session_params csp; 1654 crypto_session_t nses; 1655 1656 /* 1657 * Driver has unregistered; migrate the session and return 1658 * an error to the caller so they'll resubmit the op. 1659 * 1660 * XXX: What if there are more already queued requests for this 1661 * session? 1662 * 1663 * XXX: Real solution is to make sessions refcounted 1664 * and force callers to hold a reference when 1665 * assigning to crp_session. Could maybe change 1666 * crypto_getreq to accept a session pointer to make 1667 * that work. Alternatively, we could abandon the 1668 * notion of rewriting crp_session in requests forcing 1669 * the caller to deal with allocating a new session. 1670 * Perhaps provide a method to allow a crp's session to 1671 * be swapped that callers could use. 1672 */ 1673 csp = crp->crp_session->csp; 1674 crypto_freesession(crp->crp_session); 1675 1676 /* 1677 * XXX: Key pointers may no longer be valid. If we 1678 * really want to support this we need to define the 1679 * KPI such that 'csp' is required to be valid for the 1680 * duration of a session by the caller perhaps. 1681 * 1682 * XXX: If the keys have been changed this will reuse 1683 * the old keys. This probably suggests making 1684 * rekeying more explicit and updating the key 1685 * pointers in 'csp' when the keys change. 1686 */ 1687 if (crypto_newsession(&nses, &csp, 1688 CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE) == 0) 1689 crp->crp_session = nses; 1690 1691 crp->crp_etype = EAGAIN; 1692 crypto_done(crp); 1693 return 0; 1694 } else { 1695 /* 1696 * Invoke the driver to process the request. 1697 */ 1698 return CRYPTODEV_PROCESS(cap->cc_dev, crp, hint); 1699 } 1700 } 1701 1702 void 1703 crypto_freereq(struct cryptop *crp) 1704 { 1705 1706 if (crp == NULL) 1707 return; 1708 1709 #ifdef DIAGNOSTIC 1710 { 1711 struct cryptop *crp2; 1712 struct crypto_ret_worker *ret_worker; 1713 1714 CRYPTO_Q_LOCK(); 1715 TAILQ_FOREACH(crp2, &crp_q, crp_next) { 1716 KASSERT(crp2 != crp, 1717 ("Freeing cryptop from the crypto queue (%p).", 1718 crp)); 1719 } 1720 CRYPTO_Q_UNLOCK(); 1721 1722 FOREACH_CRYPTO_RETW(ret_worker) { 1723 CRYPTO_RETW_LOCK(ret_worker); 1724 TAILQ_FOREACH(crp2, &ret_worker->crp_ret_q, crp_next) { 1725 KASSERT(crp2 != crp, 1726 ("Freeing cryptop from the return queue (%p).", 1727 crp)); 1728 } 1729 CRYPTO_RETW_UNLOCK(ret_worker); 1730 } 1731 } 1732 #endif 1733 1734 uma_zfree(cryptop_zone, crp); 1735 } 1736 1737 struct cryptop * 1738 crypto_getreq(crypto_session_t cses, int how) 1739 { 1740 struct cryptop *crp; 1741 1742 MPASS(how == M_WAITOK || how == M_NOWAIT); 1743 crp = uma_zalloc(cryptop_zone, how | M_ZERO); 1744 crp->crp_session = cses; 1745 return (crp); 1746 } 1747 1748 /* 1749 * Invoke the callback on behalf of the driver. 1750 */ 1751 void 1752 crypto_done(struct cryptop *crp) 1753 { 1754 KASSERT((crp->crp_flags & CRYPTO_F_DONE) == 0, 1755 ("crypto_done: op already done, flags 0x%x", crp->crp_flags)); 1756 crp->crp_flags |= CRYPTO_F_DONE; 1757 if (crp->crp_etype != 0) 1758 cryptostats.cs_errs++; 1759 #ifdef CRYPTO_TIMING 1760 if (crypto_timing) 1761 crypto_tstat(&cryptostats.cs_done, &crp->crp_tstamp); 1762 #endif 1763 /* 1764 * CBIMM means unconditionally do the callback immediately; 1765 * CBIFSYNC means do the callback immediately only if the 1766 * operation was done synchronously. Both are used to avoid 1767 * doing extraneous context switches; the latter is mostly 1768 * used with the software crypto driver. 1769 */ 1770 if (!CRYPTOP_ASYNC_KEEPORDER(crp) && 1771 ((crp->crp_flags & CRYPTO_F_CBIMM) || 1772 ((crp->crp_flags & CRYPTO_F_CBIFSYNC) && 1773 (crypto_ses2caps(crp->crp_session) & CRYPTOCAP_F_SYNC)))) { 1774 /* 1775 * Do the callback directly. This is ok when the 1776 * callback routine does very little (e.g. the 1777 * /dev/crypto callback method just does a wakeup). 1778 */ 1779 #ifdef CRYPTO_TIMING 1780 if (crypto_timing) { 1781 /* 1782 * NB: We must copy the timestamp before 1783 * doing the callback as the cryptop is 1784 * likely to be reclaimed. 1785 */ 1786 struct bintime t = crp->crp_tstamp; 1787 crypto_tstat(&cryptostats.cs_cb, &t); 1788 crp->crp_callback(crp); 1789 crypto_tstat(&cryptostats.cs_finis, &t); 1790 } else 1791 #endif 1792 crp->crp_callback(crp); 1793 } else { 1794 struct crypto_ret_worker *ret_worker; 1795 bool wake; 1796 1797 ret_worker = CRYPTO_RETW(crp->crp_retw_id); 1798 wake = false; 1799 1800 /* 1801 * Normal case; queue the callback for the thread. 1802 */ 1803 CRYPTO_RETW_LOCK(ret_worker); 1804 if (CRYPTOP_ASYNC_KEEPORDER(crp)) { 1805 struct cryptop *tmp; 1806 1807 TAILQ_FOREACH_REVERSE(tmp, &ret_worker->crp_ordered_ret_q, 1808 cryptop_q, crp_next) { 1809 if (CRYPTO_SEQ_GT(crp->crp_seq, tmp->crp_seq)) { 1810 TAILQ_INSERT_AFTER(&ret_worker->crp_ordered_ret_q, 1811 tmp, crp, crp_next); 1812 break; 1813 } 1814 } 1815 if (tmp == NULL) { 1816 TAILQ_INSERT_HEAD(&ret_worker->crp_ordered_ret_q, 1817 crp, crp_next); 1818 } 1819 1820 if (crp->crp_seq == ret_worker->reorder_cur_seq) 1821 wake = true; 1822 } 1823 else { 1824 if (CRYPTO_RETW_EMPTY(ret_worker)) 1825 wake = true; 1826 1827 TAILQ_INSERT_TAIL(&ret_worker->crp_ret_q, crp, crp_next); 1828 } 1829 1830 if (wake) 1831 wakeup_one(&ret_worker->crp_ret_q); /* shared wait channel */ 1832 CRYPTO_RETW_UNLOCK(ret_worker); 1833 } 1834 } 1835 1836 /* 1837 * Invoke the callback on behalf of the driver. 1838 */ 1839 void 1840 crypto_kdone(struct cryptkop *krp) 1841 { 1842 struct crypto_ret_worker *ret_worker; 1843 struct cryptocap *cap; 1844 1845 if (krp->krp_status != 0) 1846 cryptostats.cs_kerrs++; 1847 CRYPTO_DRIVER_LOCK(); 1848 cap = krp->krp_cap; 1849 KASSERT(cap->cc_koperations > 0, ("cc_koperations == 0")); 1850 cap->cc_koperations--; 1851 if (cap->cc_koperations == 0 && cap->cc_flags & CRYPTOCAP_F_CLEANUP) 1852 wakeup(cap); 1853 CRYPTO_DRIVER_UNLOCK(); 1854 krp->krp_cap = NULL; 1855 cap_rele(cap); 1856 1857 ret_worker = CRYPTO_RETW(0); 1858 1859 CRYPTO_RETW_LOCK(ret_worker); 1860 if (CRYPTO_RETW_EMPTY(ret_worker)) 1861 wakeup_one(&ret_worker->crp_ret_q); /* shared wait channel */ 1862 TAILQ_INSERT_TAIL(&ret_worker->crp_ret_kq, krp, krp_next); 1863 CRYPTO_RETW_UNLOCK(ret_worker); 1864 } 1865 1866 int 1867 crypto_getfeat(int *featp) 1868 { 1869 int hid, kalg, feat = 0; 1870 1871 CRYPTO_DRIVER_LOCK(); 1872 for (hid = 0; hid < crypto_drivers_size; hid++) { 1873 const struct cryptocap *cap = crypto_drivers[hid]; 1874 1875 if (cap == NULL || 1876 ((cap->cc_flags & CRYPTOCAP_F_SOFTWARE) && 1877 !crypto_devallowsoft)) { 1878 continue; 1879 } 1880 for (kalg = 0; kalg < CRK_ALGORITHM_MAX; kalg++) 1881 if (cap->cc_kalg[kalg] & CRYPTO_ALG_FLAG_SUPPORTED) 1882 feat |= 1 << kalg; 1883 } 1884 CRYPTO_DRIVER_UNLOCK(); 1885 *featp = feat; 1886 return (0); 1887 } 1888 1889 /* 1890 * Terminate a thread at module unload. The process that 1891 * initiated this is waiting for us to signal that we're gone; 1892 * wake it up and exit. We use the driver table lock to insure 1893 * we don't do the wakeup before they're waiting. There is no 1894 * race here because the waiter sleeps on the proc lock for the 1895 * thread so it gets notified at the right time because of an 1896 * extra wakeup that's done in exit1(). 1897 */ 1898 static void 1899 crypto_finis(void *chan) 1900 { 1901 CRYPTO_DRIVER_LOCK(); 1902 wakeup_one(chan); 1903 CRYPTO_DRIVER_UNLOCK(); 1904 kproc_exit(0); 1905 } 1906 1907 /* 1908 * Crypto thread, dispatches crypto requests. 1909 */ 1910 static void 1911 crypto_proc(void) 1912 { 1913 struct cryptop *crp, *submit; 1914 struct cryptkop *krp; 1915 struct cryptocap *cap; 1916 int result, hint; 1917 1918 #if defined(__i386__) || defined(__amd64__) || defined(__aarch64__) 1919 fpu_kern_thread(FPU_KERN_NORMAL); 1920 #endif 1921 1922 CRYPTO_Q_LOCK(); 1923 for (;;) { 1924 /* 1925 * Find the first element in the queue that can be 1926 * processed and look-ahead to see if multiple ops 1927 * are ready for the same driver. 1928 */ 1929 submit = NULL; 1930 hint = 0; 1931 TAILQ_FOREACH(crp, &crp_q, crp_next) { 1932 cap = crp->crp_session->cap; 1933 /* 1934 * Driver cannot disappeared when there is an active 1935 * session. 1936 */ 1937 KASSERT(cap != NULL, ("%s:%u Driver disappeared.", 1938 __func__, __LINE__)); 1939 if (cap->cc_flags & CRYPTOCAP_F_CLEANUP) { 1940 /* Op needs to be migrated, process it. */ 1941 if (submit == NULL) 1942 submit = crp; 1943 break; 1944 } 1945 if (!cap->cc_qblocked) { 1946 if (submit != NULL) { 1947 /* 1948 * We stop on finding another op, 1949 * regardless whether its for the same 1950 * driver or not. We could keep 1951 * searching the queue but it might be 1952 * better to just use a per-driver 1953 * queue instead. 1954 */ 1955 if (submit->crp_session->cap == cap) 1956 hint = CRYPTO_HINT_MORE; 1957 break; 1958 } else { 1959 submit = crp; 1960 if ((submit->crp_flags & CRYPTO_F_BATCH) == 0) 1961 break; 1962 /* keep scanning for more are q'd */ 1963 } 1964 } 1965 } 1966 if (submit != NULL) { 1967 TAILQ_REMOVE(&crp_q, submit, crp_next); 1968 cap = submit->crp_session->cap; 1969 KASSERT(cap != NULL, ("%s:%u Driver disappeared.", 1970 __func__, __LINE__)); 1971 CRYPTO_Q_UNLOCK(); 1972 result = crypto_invoke(cap, submit, hint); 1973 CRYPTO_Q_LOCK(); 1974 if (result == ERESTART) { 1975 /* 1976 * The driver ran out of resources, mark the 1977 * driver ``blocked'' for cryptop's and put 1978 * the request back in the queue. It would 1979 * best to put the request back where we got 1980 * it but that's hard so for now we put it 1981 * at the front. This should be ok; putting 1982 * it at the end does not work. 1983 */ 1984 cap->cc_qblocked = 1; 1985 TAILQ_INSERT_HEAD(&crp_q, submit, crp_next); 1986 cryptostats.cs_blocks++; 1987 } 1988 } 1989 1990 /* As above, but for key ops */ 1991 TAILQ_FOREACH(krp, &crp_kq, krp_next) { 1992 cap = krp->krp_cap; 1993 if (cap->cc_flags & CRYPTOCAP_F_CLEANUP) { 1994 /* 1995 * Operation needs to be migrated, 1996 * clear krp_cap so a new driver is 1997 * selected. 1998 */ 1999 krp->krp_cap = NULL; 2000 cap_rele(cap); 2001 break; 2002 } 2003 if (!cap->cc_kqblocked) 2004 break; 2005 } 2006 if (krp != NULL) { 2007 TAILQ_REMOVE(&crp_kq, krp, krp_next); 2008 CRYPTO_Q_UNLOCK(); 2009 result = crypto_kinvoke(krp); 2010 CRYPTO_Q_LOCK(); 2011 if (result == ERESTART) { 2012 /* 2013 * The driver ran out of resources, mark the 2014 * driver ``blocked'' for cryptkop's and put 2015 * the request back in the queue. It would 2016 * best to put the request back where we got 2017 * it but that's hard so for now we put it 2018 * at the front. This should be ok; putting 2019 * it at the end does not work. 2020 */ 2021 krp->krp_cap->cc_kqblocked = 1; 2022 TAILQ_INSERT_HEAD(&crp_kq, krp, krp_next); 2023 cryptostats.cs_kblocks++; 2024 } 2025 } 2026 2027 if (submit == NULL && krp == NULL) { 2028 /* 2029 * Nothing more to be processed. Sleep until we're 2030 * woken because there are more ops to process. 2031 * This happens either by submission or by a driver 2032 * becoming unblocked and notifying us through 2033 * crypto_unblock. Note that when we wakeup we 2034 * start processing each queue again from the 2035 * front. It's not clear that it's important to 2036 * preserve this ordering since ops may finish 2037 * out of order if dispatched to different devices 2038 * and some become blocked while others do not. 2039 */ 2040 crp_sleep = 1; 2041 msleep(&crp_q, &crypto_q_mtx, PWAIT, "crypto_wait", 0); 2042 crp_sleep = 0; 2043 if (cryptoproc == NULL) 2044 break; 2045 cryptostats.cs_intrs++; 2046 } 2047 } 2048 CRYPTO_Q_UNLOCK(); 2049 2050 crypto_finis(&crp_q); 2051 } 2052 2053 /* 2054 * Crypto returns thread, does callbacks for processed crypto requests. 2055 * Callbacks are done here, rather than in the crypto drivers, because 2056 * callbacks typically are expensive and would slow interrupt handling. 2057 */ 2058 static void 2059 crypto_ret_proc(struct crypto_ret_worker *ret_worker) 2060 { 2061 struct cryptop *crpt; 2062 struct cryptkop *krpt; 2063 2064 CRYPTO_RETW_LOCK(ret_worker); 2065 for (;;) { 2066 /* Harvest return q's for completed ops */ 2067 crpt = TAILQ_FIRST(&ret_worker->crp_ordered_ret_q); 2068 if (crpt != NULL) { 2069 if (crpt->crp_seq == ret_worker->reorder_cur_seq) { 2070 TAILQ_REMOVE(&ret_worker->crp_ordered_ret_q, crpt, crp_next); 2071 ret_worker->reorder_cur_seq++; 2072 } else { 2073 crpt = NULL; 2074 } 2075 } 2076 2077 if (crpt == NULL) { 2078 crpt = TAILQ_FIRST(&ret_worker->crp_ret_q); 2079 if (crpt != NULL) 2080 TAILQ_REMOVE(&ret_worker->crp_ret_q, crpt, crp_next); 2081 } 2082 2083 krpt = TAILQ_FIRST(&ret_worker->crp_ret_kq); 2084 if (krpt != NULL) 2085 TAILQ_REMOVE(&ret_worker->crp_ret_kq, krpt, krp_next); 2086 2087 if (crpt != NULL || krpt != NULL) { 2088 CRYPTO_RETW_UNLOCK(ret_worker); 2089 /* 2090 * Run callbacks unlocked. 2091 */ 2092 if (crpt != NULL) { 2093 #ifdef CRYPTO_TIMING 2094 if (crypto_timing) { 2095 /* 2096 * NB: We must copy the timestamp before 2097 * doing the callback as the cryptop is 2098 * likely to be reclaimed. 2099 */ 2100 struct bintime t = crpt->crp_tstamp; 2101 crypto_tstat(&cryptostats.cs_cb, &t); 2102 crpt->crp_callback(crpt); 2103 crypto_tstat(&cryptostats.cs_finis, &t); 2104 } else 2105 #endif 2106 crpt->crp_callback(crpt); 2107 } 2108 if (krpt != NULL) 2109 krpt->krp_callback(krpt); 2110 CRYPTO_RETW_LOCK(ret_worker); 2111 } else { 2112 /* 2113 * Nothing more to be processed. Sleep until we're 2114 * woken because there are more returns to process. 2115 */ 2116 msleep(&ret_worker->crp_ret_q, &ret_worker->crypto_ret_mtx, PWAIT, 2117 "crypto_ret_wait", 0); 2118 if (ret_worker->cryptoretproc == NULL) 2119 break; 2120 cryptostats.cs_rets++; 2121 } 2122 } 2123 CRYPTO_RETW_UNLOCK(ret_worker); 2124 2125 crypto_finis(&ret_worker->crp_ret_q); 2126 } 2127 2128 #ifdef DDB 2129 static void 2130 db_show_drivers(void) 2131 { 2132 int hid; 2133 2134 db_printf("%12s %4s %4s %8s %2s %2s\n" 2135 , "Device" 2136 , "Ses" 2137 , "Kops" 2138 , "Flags" 2139 , "QB" 2140 , "KB" 2141 ); 2142 for (hid = 0; hid < crypto_drivers_size; hid++) { 2143 const struct cryptocap *cap = crypto_drivers[hid]; 2144 if (cap == NULL) 2145 continue; 2146 db_printf("%-12s %4u %4u %08x %2u %2u\n" 2147 , device_get_nameunit(cap->cc_dev) 2148 , cap->cc_sessions 2149 , cap->cc_koperations 2150 , cap->cc_flags 2151 , cap->cc_qblocked 2152 , cap->cc_kqblocked 2153 ); 2154 } 2155 } 2156 2157 DB_SHOW_COMMAND(crypto, db_show_crypto) 2158 { 2159 struct cryptop *crp; 2160 struct crypto_ret_worker *ret_worker; 2161 2162 db_show_drivers(); 2163 db_printf("\n"); 2164 2165 db_printf("%4s %8s %4s %4s %4s %4s %8s %8s\n", 2166 "HID", "Caps", "Ilen", "Olen", "Etype", "Flags", 2167 "Device", "Callback"); 2168 TAILQ_FOREACH(crp, &crp_q, crp_next) { 2169 db_printf("%4u %08x %4u %4u %4u %04x %8p %8p\n" 2170 , crp->crp_session->cap->cc_hid 2171 , (int) crypto_ses2caps(crp->crp_session) 2172 , crp->crp_ilen, crp->crp_olen 2173 , crp->crp_etype 2174 , crp->crp_flags 2175 , device_get_nameunit(crp->crp_session->cap->cc_dev) 2176 , crp->crp_callback 2177 ); 2178 } 2179 FOREACH_CRYPTO_RETW(ret_worker) { 2180 db_printf("\n%8s %4s %4s %4s %8s\n", 2181 "ret_worker", "HID", "Etype", "Flags", "Callback"); 2182 if (!TAILQ_EMPTY(&ret_worker->crp_ret_q)) { 2183 TAILQ_FOREACH(crp, &ret_worker->crp_ret_q, crp_next) { 2184 db_printf("%8td %4u %4u %04x %8p\n" 2185 , CRYPTO_RETW_ID(ret_worker) 2186 , crp->crp_session->cap->cc_hid 2187 , crp->crp_etype 2188 , crp->crp_flags 2189 , crp->crp_callback 2190 ); 2191 } 2192 } 2193 } 2194 } 2195 2196 DB_SHOW_COMMAND(kcrypto, db_show_kcrypto) 2197 { 2198 struct cryptkop *krp; 2199 struct crypto_ret_worker *ret_worker; 2200 2201 db_show_drivers(); 2202 db_printf("\n"); 2203 2204 db_printf("%4s %5s %4s %4s %8s %4s %8s\n", 2205 "Op", "Status", "#IP", "#OP", "CRID", "HID", "Callback"); 2206 TAILQ_FOREACH(krp, &crp_kq, krp_next) { 2207 db_printf("%4u %5u %4u %4u %08x %4u %8p\n" 2208 , krp->krp_op 2209 , krp->krp_status 2210 , krp->krp_iparams, krp->krp_oparams 2211 , krp->krp_crid, krp->krp_hid 2212 , krp->krp_callback 2213 ); 2214 } 2215 2216 ret_worker = CRYPTO_RETW(0); 2217 if (!TAILQ_EMPTY(&ret_worker->crp_ret_q)) { 2218 db_printf("%4s %5s %8s %4s %8s\n", 2219 "Op", "Status", "CRID", "HID", "Callback"); 2220 TAILQ_FOREACH(krp, &ret_worker->crp_ret_kq, krp_next) { 2221 db_printf("%4u %5u %08x %4u %8p\n" 2222 , krp->krp_op 2223 , krp->krp_status 2224 , krp->krp_crid, krp->krp_hid 2225 , krp->krp_callback 2226 ); 2227 } 2228 } 2229 } 2230 #endif 2231 2232 int crypto_modevent(module_t mod, int type, void *unused); 2233 2234 /* 2235 * Initialization code, both for static and dynamic loading. 2236 * Note this is not invoked with the usual MODULE_DECLARE 2237 * mechanism but instead is listed as a dependency by the 2238 * cryptosoft driver. This guarantees proper ordering of 2239 * calls on module load/unload. 2240 */ 2241 int 2242 crypto_modevent(module_t mod, int type, void *unused) 2243 { 2244 int error = EINVAL; 2245 2246 switch (type) { 2247 case MOD_LOAD: 2248 error = crypto_init(); 2249 if (error == 0 && bootverbose) 2250 printf("crypto: <crypto core>\n"); 2251 break; 2252 case MOD_UNLOAD: 2253 /*XXX disallow if active sessions */ 2254 error = 0; 2255 crypto_destroy(); 2256 return 0; 2257 } 2258 return error; 2259 } 2260 MODULE_VERSION(crypto, 1); 2261 MODULE_DEPEND(crypto, zlib, 1, 1, 1); 2262