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