2 * Copyright (c) 2002-2006 Sam Leffler. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
25 #include <sys/cdefs.h>
26 __FBSDID("$FreeBSD$");
29 * Cryptographic Subsystem.
31 * This code is derived from the Openbsd Cryptographic Framework (OCF)
32 * that has the copyright shown below. Very little of the original
37 * The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu)
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.
43 * Copyright (c) 2000, 2001 Angelos D. Keromytis
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.
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
57 #define CRYPTO_TIMING /* enable timing support */
60 #include "opt_kdtrace.h"
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>
68 #include <sys/module.h>
69 #include <sys/mutex.h>
70 #include <sys/malloc.h>
73 #include <sys/sysctl.h>
78 #include <opencrypto/cryptodev.h>
79 #include <opencrypto/xform.h> /* XXX for M_XDATA */
83 #include "cryptodev_if.h"
85 SDT_PROVIDER_DEFINE(opencrypto);
88 * Crypto drivers register themselves by allocating a slot in the
89 * crypto_drivers table with crypto_get_driverid() and then registering
90 * each algorithm they support with crypto_register() and crypto_kregister().
92 static struct mtx crypto_drivers_mtx; /* lock on driver table */
93 #define CRYPTO_DRIVER_LOCK() mtx_lock(&crypto_drivers_mtx)
94 #define CRYPTO_DRIVER_UNLOCK() mtx_unlock(&crypto_drivers_mtx)
95 #define CRYPTO_DRIVER_ASSERT() mtx_assert(&crypto_drivers_mtx, MA_OWNED)
98 * Crypto device/driver capabilities structure.
101 * (d) - protected by CRYPTO_DRIVER_LOCK()
102 * (q) - protected by CRYPTO_Q_LOCK()
103 * Not tagged fields are read-only.
106 device_t cc_dev; /* (d) device/driver */
107 u_int32_t cc_sessions; /* (d) # of sessions */
108 u_int32_t cc_koperations; /* (d) # os asym operations */
110 * Largest possible operator length (in bits) for each type of
111 * encryption algorithm. XXX not used
113 u_int16_t cc_max_op_len[CRYPTO_ALGORITHM_MAX + 1];
114 u_int8_t cc_alg[CRYPTO_ALGORITHM_MAX + 1];
115 u_int8_t cc_kalg[CRK_ALGORITHM_MAX + 1];
117 int cc_flags; /* (d) flags */
118 #define CRYPTOCAP_F_CLEANUP 0x80000000 /* needs resource cleanup */
119 int cc_qblocked; /* (q) symmetric q blocked */
120 int cc_kqblocked; /* (q) asymmetric q blocked */
122 static struct cryptocap *crypto_drivers = NULL;
123 static int crypto_drivers_num = 0;
126 * There are two queues for crypto requests; one for symmetric (e.g.
127 * cipher) operations and one for asymmetric (e.g. MOD)operations.
128 * A single mutex is used to lock access to both queues. We could
129 * have one per-queue but having one simplifies handling of block/unblock
132 static int crp_sleep = 0;
133 static TAILQ_HEAD(,cryptop) crp_q; /* request queues */
134 static TAILQ_HEAD(,cryptkop) crp_kq;
135 static struct mtx crypto_q_mtx;
136 #define CRYPTO_Q_LOCK() mtx_lock(&crypto_q_mtx)
137 #define CRYPTO_Q_UNLOCK() mtx_unlock(&crypto_q_mtx)
140 * There are two queues for processing completed crypto requests; one
141 * for the symmetric and one for the asymmetric ops. We only need one
142 * but have two to avoid type futzing (cryptop vs. cryptkop). A single
143 * mutex is used to lock access to both queues. Note that this lock
144 * must be separate from the lock on request queues to insure driver
145 * callbacks don't generate lock order reversals.
147 static TAILQ_HEAD(,cryptop) crp_ret_q; /* callback queues */
148 static TAILQ_HEAD(,cryptkop) crp_ret_kq;
149 static struct mtx crypto_ret_q_mtx;
150 #define CRYPTO_RETQ_LOCK() mtx_lock(&crypto_ret_q_mtx)
151 #define CRYPTO_RETQ_UNLOCK() mtx_unlock(&crypto_ret_q_mtx)
152 #define CRYPTO_RETQ_EMPTY() (TAILQ_EMPTY(&crp_ret_q) && TAILQ_EMPTY(&crp_ret_kq))
154 static uma_zone_t cryptop_zone;
155 static uma_zone_t cryptodesc_zone;
157 int crypto_userasymcrypto = 1; /* userland may do asym crypto reqs */
158 SYSCTL_INT(_kern, OID_AUTO, userasymcrypto, CTLFLAG_RW,
159 &crypto_userasymcrypto, 0,
160 "Enable/disable user-mode access to asymmetric crypto support");
161 int crypto_devallowsoft = 0; /* only use hardware crypto for asym */
162 SYSCTL_INT(_kern, OID_AUTO, cryptodevallowsoft, CTLFLAG_RW,
163 &crypto_devallowsoft, 0,
164 "Enable/disable use of software asym crypto support");
166 MALLOC_DEFINE(M_CRYPTO_DATA, "crypto", "crypto session records");
168 static void crypto_proc(void);
169 static struct proc *cryptoproc;
170 static void crypto_ret_proc(void);
171 static struct proc *cryptoretproc;
172 static void crypto_destroy(void);
173 static int crypto_invoke(struct cryptocap *cap, struct cryptop *crp, int hint);
174 static int crypto_kinvoke(struct cryptkop *krp, int flags);
176 static struct cryptostats cryptostats;
177 SYSCTL_STRUCT(_kern, OID_AUTO, crypto_stats, CTLFLAG_RW, &cryptostats,
178 cryptostats, "Crypto system statistics");
181 static int crypto_timing = 0;
182 SYSCTL_INT(_debug, OID_AUTO, crypto_timing, CTLFLAG_RW,
183 &crypto_timing, 0, "Enable/disable crypto timing support");
191 mtx_init(&crypto_drivers_mtx, "crypto", "crypto driver table",
196 mtx_init(&crypto_q_mtx, "crypto", "crypto op queues", MTX_DEF);
198 TAILQ_INIT(&crp_ret_q);
199 TAILQ_INIT(&crp_ret_kq);
200 mtx_init(&crypto_ret_q_mtx, "crypto", "crypto return queues", MTX_DEF);
202 cryptop_zone = uma_zcreate("cryptop", sizeof (struct cryptop),
204 UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
205 cryptodesc_zone = uma_zcreate("cryptodesc", sizeof (struct cryptodesc),
207 UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
208 if (cryptodesc_zone == NULL || cryptop_zone == NULL) {
209 printf("crypto_init: cannot setup crypto zones\n");
214 crypto_drivers_num = CRYPTO_DRIVERS_INITIAL;
215 crypto_drivers = malloc(crypto_drivers_num *
216 sizeof(struct cryptocap), M_CRYPTO_DATA, M_NOWAIT | M_ZERO);
217 if (crypto_drivers == NULL) {
218 printf("crypto_init: cannot setup crypto drivers\n");
223 error = kproc_create((void (*)(void *)) crypto_proc, NULL,
224 &cryptoproc, 0, 0, "crypto");
226 printf("crypto_init: cannot start crypto thread; error %d",
231 error = kproc_create((void (*)(void *)) crypto_ret_proc, NULL,
232 &cryptoretproc, 0, 0, "crypto returns");
234 printf("crypto_init: cannot start cryptoret thread; error %d",
245 * Signal a crypto thread to terminate. We use the driver
246 * table lock to synchronize the sleep/wakeups so that we
247 * are sure the threads have terminated before we release
248 * the data structures they use. See crypto_finis below
249 * for the other half of this song-and-dance.
252 crypto_terminate(struct proc **pp, void *q)
256 mtx_assert(&crypto_drivers_mtx, MA_OWNED);
261 PROC_LOCK(p); /* NB: insure we don't miss wakeup */
262 CRYPTO_DRIVER_UNLOCK(); /* let crypto_finis progress */
263 msleep(p, &p->p_mtx, PWAIT, "crypto_destroy", 0);
265 CRYPTO_DRIVER_LOCK();
273 * Terminate any crypto threads.
275 CRYPTO_DRIVER_LOCK();
276 crypto_terminate(&cryptoproc, &crp_q);
277 crypto_terminate(&cryptoretproc, &crp_ret_q);
278 CRYPTO_DRIVER_UNLOCK();
280 /* XXX flush queues??? */
283 * Reclaim dynamically allocated resources.
285 if (crypto_drivers != NULL)
286 free(crypto_drivers, M_CRYPTO_DATA);
288 if (cryptodesc_zone != NULL)
289 uma_zdestroy(cryptodesc_zone);
290 if (cryptop_zone != NULL)
291 uma_zdestroy(cryptop_zone);
292 mtx_destroy(&crypto_q_mtx);
293 mtx_destroy(&crypto_ret_q_mtx);
294 mtx_destroy(&crypto_drivers_mtx);
297 static struct cryptocap *
298 crypto_checkdriver(u_int32_t hid)
300 if (crypto_drivers == NULL)
302 return (hid >= crypto_drivers_num ? NULL : &crypto_drivers[hid]);
306 * Compare a driver's list of supported algorithms against another
307 * list; return non-zero if all algorithms are supported.
310 driver_suitable(const struct cryptocap *cap, const struct cryptoini *cri)
312 const struct cryptoini *cr;
314 /* See if all the algorithms are supported. */
315 for (cr = cri; cr; cr = cr->cri_next)
316 if (cap->cc_alg[cr->cri_alg] == 0)
322 * Select a driver for a new session that supports the specified
323 * algorithms and, optionally, is constrained according to the flags.
324 * The algorithm we use here is pretty stupid; just use the
325 * first driver that supports all the algorithms we need. If there
326 * are multiple drivers we choose the driver with the fewest active
327 * sessions. We prefer hardware-backed drivers to software ones.
329 * XXX We need more smarts here (in real life too, but that's
330 * XXX another story altogether).
332 static struct cryptocap *
333 crypto_select_driver(const struct cryptoini *cri, int flags)
335 struct cryptocap *cap, *best;
338 CRYPTO_DRIVER_ASSERT();
341 * Look first for hardware crypto devices if permitted.
343 if (flags & CRYPTOCAP_F_HARDWARE)
344 match = CRYPTOCAP_F_HARDWARE;
346 match = CRYPTOCAP_F_SOFTWARE;
349 for (hid = 0; hid < crypto_drivers_num; hid++) {
350 cap = &crypto_drivers[hid];
352 * If it's not initialized, is in the process of
353 * going away, or is not appropriate (hardware
354 * or software based on match), then skip.
356 if (cap->cc_dev == NULL ||
357 (cap->cc_flags & CRYPTOCAP_F_CLEANUP) ||
358 (cap->cc_flags & match) == 0)
361 /* verify all the algorithms are supported. */
362 if (driver_suitable(cap, cri)) {
364 cap->cc_sessions < best->cc_sessions)
370 if (match == CRYPTOCAP_F_HARDWARE && (flags & CRYPTOCAP_F_SOFTWARE)) {
371 /* sort of an Algol 68-style for loop */
372 match = CRYPTOCAP_F_SOFTWARE;
379 * Create a new session. The crid argument specifies a crypto
380 * driver to use or constraints on a driver to select (hardware
381 * only, software only, either). Whatever driver is selected
382 * must be capable of the requested crypto algorithms.
385 crypto_newsession(u_int64_t *sid, struct cryptoini *cri, int crid)
387 struct cryptocap *cap;
391 CRYPTO_DRIVER_LOCK();
392 if ((crid & (CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE)) == 0) {
394 * Use specified driver; verify it is capable.
396 cap = crypto_checkdriver(crid);
397 if (cap != NULL && !driver_suitable(cap, cri))
401 * No requested driver; select based on crid flags.
403 cap = crypto_select_driver(cri, crid);
405 * if NULL then can't do everything in one session.
406 * XXX Fix this. We need to inject a "virtual" session
407 * XXX layer right about here.
411 /* Call the driver initialization routine. */
412 hid = cap - crypto_drivers;
413 lid = hid; /* Pass the driver ID. */
414 err = CRYPTODEV_NEWSESSION(cap->cc_dev, &lid, cri);
416 (*sid) = (cap->cc_flags & 0xff000000)
417 | (hid & 0x00ffffff);
419 (*sid) |= (lid & 0xffffffff);
424 CRYPTO_DRIVER_UNLOCK();
429 crypto_remove(struct cryptocap *cap)
432 mtx_assert(&crypto_drivers_mtx, MA_OWNED);
433 if (cap->cc_sessions == 0 && cap->cc_koperations == 0)
434 bzero(cap, sizeof(*cap));
438 * Delete an existing session (or a reserved session on an unregistered
442 crypto_freesession(u_int64_t sid)
444 struct cryptocap *cap;
448 CRYPTO_DRIVER_LOCK();
450 if (crypto_drivers == NULL) {
455 /* Determine two IDs. */
456 hid = CRYPTO_SESID2HID(sid);
458 if (hid >= crypto_drivers_num) {
462 cap = &crypto_drivers[hid];
464 if (cap->cc_sessions)
467 /* Call the driver cleanup routine, if available. */
468 err = CRYPTODEV_FREESESSION(cap->cc_dev, sid);
470 if (cap->cc_flags & CRYPTOCAP_F_CLEANUP)
474 CRYPTO_DRIVER_UNLOCK();
479 * Return an unused driver id. Used by drivers prior to registering
480 * support for the algorithms they handle.
483 crypto_get_driverid(device_t dev, int flags)
485 struct cryptocap *newdrv;
488 if ((flags & (CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE)) == 0) {
489 printf("%s: no flags specified when registering driver\n",
490 device_get_nameunit(dev));
494 CRYPTO_DRIVER_LOCK();
496 for (i = 0; i < crypto_drivers_num; i++) {
497 if (crypto_drivers[i].cc_dev == NULL &&
498 (crypto_drivers[i].cc_flags & CRYPTOCAP_F_CLEANUP) == 0) {
503 /* Out of entries, allocate some more. */
504 if (i == crypto_drivers_num) {
505 /* Be careful about wrap-around. */
506 if (2 * crypto_drivers_num <= crypto_drivers_num) {
507 CRYPTO_DRIVER_UNLOCK();
508 printf("crypto: driver count wraparound!\n");
512 newdrv = malloc(2 * crypto_drivers_num *
513 sizeof(struct cryptocap), M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
514 if (newdrv == NULL) {
515 CRYPTO_DRIVER_UNLOCK();
516 printf("crypto: no space to expand driver table!\n");
520 bcopy(crypto_drivers, newdrv,
521 crypto_drivers_num * sizeof(struct cryptocap));
523 crypto_drivers_num *= 2;
525 free(crypto_drivers, M_CRYPTO_DATA);
526 crypto_drivers = newdrv;
529 /* NB: state is zero'd on free */
530 crypto_drivers[i].cc_sessions = 1; /* Mark */
531 crypto_drivers[i].cc_dev = dev;
532 crypto_drivers[i].cc_flags = flags;
534 printf("crypto: assign %s driver id %u, flags %u\n",
535 device_get_nameunit(dev), i, flags);
537 CRYPTO_DRIVER_UNLOCK();
543 * Lookup a driver by name. We match against the full device
544 * name and unit, and against just the name. The latter gives
545 * us a simple widlcarding by device name. On success return the
546 * driver/hardware identifier; otherwise return -1.
549 crypto_find_driver(const char *match)
551 int i, len = strlen(match);
553 CRYPTO_DRIVER_LOCK();
554 for (i = 0; i < crypto_drivers_num; i++) {
555 device_t dev = crypto_drivers[i].cc_dev;
557 (crypto_drivers[i].cc_flags & CRYPTOCAP_F_CLEANUP))
559 if (strncmp(match, device_get_nameunit(dev), len) == 0 ||
560 strncmp(match, device_get_name(dev), len) == 0)
563 CRYPTO_DRIVER_UNLOCK();
564 return i < crypto_drivers_num ? i : -1;
568 * Return the device_t for the specified driver or NULL
569 * if the driver identifier is invalid.
572 crypto_find_device_byhid(int hid)
574 struct cryptocap *cap = crypto_checkdriver(hid);
575 return cap != NULL ? cap->cc_dev : NULL;
579 * Return the device/driver capabilities.
582 crypto_getcaps(int hid)
584 struct cryptocap *cap = crypto_checkdriver(hid);
585 return cap != NULL ? cap->cc_flags : 0;
589 * Register support for a key-related algorithm. This routine
590 * is called once for each algorithm supported a driver.
593 crypto_kregister(u_int32_t driverid, int kalg, u_int32_t flags)
595 struct cryptocap *cap;
598 CRYPTO_DRIVER_LOCK();
600 cap = crypto_checkdriver(driverid);
602 (CRK_ALGORITM_MIN <= kalg && kalg <= CRK_ALGORITHM_MAX)) {
604 * XXX Do some performance testing to determine placing.
605 * XXX We probably need an auxiliary data structure that
606 * XXX describes relative performances.
609 cap->cc_kalg[kalg] = flags | CRYPTO_ALG_FLAG_SUPPORTED;
611 printf("crypto: %s registers key alg %u flags %u\n"
612 , device_get_nameunit(cap->cc_dev)
620 CRYPTO_DRIVER_UNLOCK();
625 * Register support for a non-key-related algorithm. This routine
626 * is called once for each such algorithm supported by a driver.
629 crypto_register(u_int32_t driverid, int alg, u_int16_t maxoplen,
632 struct cryptocap *cap;
635 CRYPTO_DRIVER_LOCK();
637 cap = crypto_checkdriver(driverid);
638 /* NB: algorithms are in the range [1..max] */
640 (CRYPTO_ALGORITHM_MIN <= alg && alg <= CRYPTO_ALGORITHM_MAX)) {
642 * XXX Do some performance testing to determine placing.
643 * XXX We probably need an auxiliary data structure that
644 * XXX describes relative performances.
647 cap->cc_alg[alg] = flags | CRYPTO_ALG_FLAG_SUPPORTED;
648 cap->cc_max_op_len[alg] = maxoplen;
650 printf("crypto: %s registers alg %u flags %u maxoplen %u\n"
651 , device_get_nameunit(cap->cc_dev)
656 cap->cc_sessions = 0; /* Unmark */
661 CRYPTO_DRIVER_UNLOCK();
666 driver_finis(struct cryptocap *cap)
670 CRYPTO_DRIVER_ASSERT();
672 ses = cap->cc_sessions;
673 kops = cap->cc_koperations;
674 bzero(cap, sizeof(*cap));
675 if (ses != 0 || kops != 0) {
677 * If there are pending sessions,
678 * just mark as invalid.
680 cap->cc_flags |= CRYPTOCAP_F_CLEANUP;
681 cap->cc_sessions = ses;
682 cap->cc_koperations = kops;
687 * Unregister a crypto driver. If there are pending sessions using it,
688 * leave enough information around so that subsequent calls using those
689 * sessions will correctly detect the driver has been unregistered and
693 crypto_unregister(u_int32_t driverid, int alg)
695 struct cryptocap *cap;
698 CRYPTO_DRIVER_LOCK();
699 cap = crypto_checkdriver(driverid);
701 (CRYPTO_ALGORITHM_MIN <= alg && alg <= CRYPTO_ALGORITHM_MAX) &&
702 cap->cc_alg[alg] != 0) {
703 cap->cc_alg[alg] = 0;
704 cap->cc_max_op_len[alg] = 0;
706 /* Was this the last algorithm ? */
707 for (i = 1; i <= CRYPTO_ALGORITHM_MAX; i++)
708 if (cap->cc_alg[i] != 0)
711 if (i == CRYPTO_ALGORITHM_MAX + 1)
716 CRYPTO_DRIVER_UNLOCK();
722 * Unregister all algorithms associated with a crypto driver.
723 * If there are pending sessions using it, leave enough information
724 * around so that subsequent calls using those sessions will
725 * correctly detect the driver has been unregistered and reroute
729 crypto_unregister_all(u_int32_t driverid)
731 struct cryptocap *cap;
734 CRYPTO_DRIVER_LOCK();
735 cap = crypto_checkdriver(driverid);
741 CRYPTO_DRIVER_UNLOCK();
747 * Clear blockage on a driver. The what parameter indicates whether
748 * the driver is now ready for cryptop's and/or cryptokop's.
751 crypto_unblock(u_int32_t driverid, int what)
753 struct cryptocap *cap;
757 cap = crypto_checkdriver(driverid);
759 if (what & CRYPTO_SYMQ)
760 cap->cc_qblocked = 0;
761 if (what & CRYPTO_ASYMQ)
762 cap->cc_kqblocked = 0;
774 * Add a crypto request to a queue, to be processed by the kernel thread.
777 crypto_dispatch(struct cryptop *crp)
779 struct cryptocap *cap;
783 cryptostats.cs_ops++;
787 binuptime(&crp->crp_tstamp);
790 hid = CRYPTO_SESID2HID(crp->crp_sid);
792 if ((crp->crp_flags & CRYPTO_F_BATCH) == 0) {
794 * Caller marked the request to be processed
795 * immediately; dispatch it directly to the
796 * driver unless the driver is currently blocked.
798 cap = crypto_checkdriver(hid);
799 /* Driver cannot disappeared when there is an active session. */
800 KASSERT(cap != NULL, ("%s: Driver disappeared.", __func__));
801 if (!cap->cc_qblocked) {
802 result = crypto_invoke(cap, crp, 0);
803 if (result != ERESTART)
806 * The driver ran out of resources, put the request on
812 TAILQ_INSERT_TAIL(&crp_q, crp, crp_next);
820 * Add an asymetric crypto request to a queue,
821 * to be processed by the kernel thread.
824 crypto_kdispatch(struct cryptkop *krp)
828 cryptostats.cs_kops++;
830 error = crypto_kinvoke(krp, krp->krp_crid);
831 if (error == ERESTART) {
833 TAILQ_INSERT_TAIL(&crp_kq, krp, krp_next);
843 * Verify a driver is suitable for the specified operation.
846 kdriver_suitable(const struct cryptocap *cap, const struct cryptkop *krp)
848 return (cap->cc_kalg[krp->krp_op] & CRYPTO_ALG_FLAG_SUPPORTED) != 0;
852 * Select a driver for an asym operation. The driver must
853 * support the necessary algorithm. The caller can constrain
854 * which device is selected with the flags parameter. The
855 * algorithm we use here is pretty stupid; just use the first
856 * driver that supports the algorithms we need. If there are
857 * multiple suitable drivers we choose the driver with the
858 * fewest active operations. We prefer hardware-backed
859 * drivers to software ones when either may be used.
861 static struct cryptocap *
862 crypto_select_kdriver(const struct cryptkop *krp, int flags)
864 struct cryptocap *cap, *best, *blocked;
867 CRYPTO_DRIVER_ASSERT();
870 * Look first for hardware crypto devices if permitted.
872 if (flags & CRYPTOCAP_F_HARDWARE)
873 match = CRYPTOCAP_F_HARDWARE;
875 match = CRYPTOCAP_F_SOFTWARE;
879 for (hid = 0; hid < crypto_drivers_num; hid++) {
880 cap = &crypto_drivers[hid];
882 * If it's not initialized, is in the process of
883 * going away, or is not appropriate (hardware
884 * or software based on match), then skip.
886 if (cap->cc_dev == NULL ||
887 (cap->cc_flags & CRYPTOCAP_F_CLEANUP) ||
888 (cap->cc_flags & match) == 0)
891 /* verify all the algorithms are supported. */
892 if (kdriver_suitable(cap, krp)) {
894 cap->cc_koperations < best->cc_koperations)
900 if (match == CRYPTOCAP_F_HARDWARE && (flags & CRYPTOCAP_F_SOFTWARE)) {
901 /* sort of an Algol 68-style for loop */
902 match = CRYPTOCAP_F_SOFTWARE;
909 * Dispatch an assymetric crypto request.
912 crypto_kinvoke(struct cryptkop *krp, int crid)
914 struct cryptocap *cap = NULL;
917 KASSERT(krp != NULL, ("%s: krp == NULL", __func__));
918 KASSERT(krp->krp_callback != NULL,
919 ("%s: krp->crp_callback == NULL", __func__));
921 CRYPTO_DRIVER_LOCK();
922 if ((crid & (CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE)) == 0) {
923 cap = crypto_checkdriver(crid);
926 * Driver present, it must support the necessary
927 * algorithm and, if s/w drivers are excluded,
928 * it must be registered as hardware-backed.
930 if (!kdriver_suitable(cap, krp) ||
931 (!crypto_devallowsoft &&
932 (cap->cc_flags & CRYPTOCAP_F_HARDWARE) == 0))
937 * No requested driver; select based on crid flags.
939 if (!crypto_devallowsoft) /* NB: disallow s/w drivers */
940 crid &= ~CRYPTOCAP_F_SOFTWARE;
941 cap = crypto_select_kdriver(krp, crid);
943 if (cap != NULL && !cap->cc_kqblocked) {
944 krp->krp_hid = cap - crypto_drivers;
945 cap->cc_koperations++;
946 CRYPTO_DRIVER_UNLOCK();
947 error = CRYPTODEV_KPROCESS(cap->cc_dev, krp, 0);
948 CRYPTO_DRIVER_LOCK();
949 if (error == ERESTART) {
950 cap->cc_koperations--;
951 CRYPTO_DRIVER_UNLOCK();
956 * NB: cap is !NULL if device is blocked; in
957 * that case return ERESTART so the operation
958 * is resubmitted if possible.
960 error = (cap == NULL) ? ENODEV : ERESTART;
962 CRYPTO_DRIVER_UNLOCK();
965 krp->krp_status = error;
973 crypto_tstat(struct cryptotstat *ts, struct bintime *bt)
975 struct bintime now, delta;
981 delta.frac = now.frac - bt->frac;
982 delta.sec = now.sec - bt->sec;
985 bintime2timespec(&delta, &t);
986 timespecadd(&ts->acc, &t);
987 if (timespeccmp(&t, &ts->min, <))
989 if (timespeccmp(&t, &ts->max, >))
998 * Dispatch a crypto request to the appropriate crypto devices.
1001 crypto_invoke(struct cryptocap *cap, struct cryptop *crp, int hint)
1004 KASSERT(crp != NULL, ("%s: crp == NULL", __func__));
1005 KASSERT(crp->crp_callback != NULL,
1006 ("%s: crp->crp_callback == NULL", __func__));
1007 KASSERT(crp->crp_desc != NULL, ("%s: crp->crp_desc == NULL", __func__));
1009 #ifdef CRYPTO_TIMING
1011 crypto_tstat(&cryptostats.cs_invoke, &crp->crp_tstamp);
1013 if (cap->cc_flags & CRYPTOCAP_F_CLEANUP) {
1014 struct cryptodesc *crd;
1018 * Driver has unregistered; migrate the session and return
1019 * an error to the caller so they'll resubmit the op.
1021 * XXX: What if there are more already queued requests for this
1024 crypto_freesession(crp->crp_sid);
1026 for (crd = crp->crp_desc; crd->crd_next; crd = crd->crd_next)
1027 crd->CRD_INI.cri_next = &(crd->crd_next->CRD_INI);
1029 /* XXX propagate flags from initial session? */
1030 if (crypto_newsession(&nid, &(crp->crp_desc->CRD_INI),
1031 CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE) == 0)
1034 crp->crp_etype = EAGAIN;
1039 * Invoke the driver to process the request.
1041 return CRYPTODEV_PROCESS(cap->cc_dev, crp, hint);
1046 * Release a set of crypto descriptors.
1049 crypto_freereq(struct cryptop *crp)
1051 struct cryptodesc *crd;
1058 struct cryptop *crp2;
1061 TAILQ_FOREACH(crp2, &crp_q, crp_next) {
1062 KASSERT(crp2 != crp,
1063 ("Freeing cryptop from the crypto queue (%p).",
1068 TAILQ_FOREACH(crp2, &crp_ret_q, crp_next) {
1069 KASSERT(crp2 != crp,
1070 ("Freeing cryptop from the return queue (%p).",
1073 CRYPTO_RETQ_UNLOCK();
1077 while ((crd = crp->crp_desc) != NULL) {
1078 crp->crp_desc = crd->crd_next;
1079 uma_zfree(cryptodesc_zone, crd);
1081 uma_zfree(cryptop_zone, crp);
1085 * Acquire a set of crypto descriptors.
1088 crypto_getreq(int num)
1090 struct cryptodesc *crd;
1091 struct cryptop *crp;
1093 crp = uma_zalloc(cryptop_zone, M_NOWAIT|M_ZERO);
1096 crd = uma_zalloc(cryptodesc_zone, M_NOWAIT|M_ZERO);
1098 crypto_freereq(crp);
1102 crd->crd_next = crp->crp_desc;
1103 crp->crp_desc = crd;
1110 * Invoke the callback on behalf of the driver.
1113 crypto_done(struct cryptop *crp)
1115 KASSERT((crp->crp_flags & CRYPTO_F_DONE) == 0,
1116 ("crypto_done: op already done, flags 0x%x", crp->crp_flags));
1117 crp->crp_flags |= CRYPTO_F_DONE;
1118 if (crp->crp_etype != 0)
1119 cryptostats.cs_errs++;
1120 #ifdef CRYPTO_TIMING
1122 crypto_tstat(&cryptostats.cs_done, &crp->crp_tstamp);
1125 * CBIMM means unconditionally do the callback immediately;
1126 * CBIFSYNC means do the callback immediately only if the
1127 * operation was done synchronously. Both are used to avoid
1128 * doing extraneous context switches; the latter is mostly
1129 * used with the software crypto driver.
1131 if ((crp->crp_flags & CRYPTO_F_CBIMM) ||
1132 ((crp->crp_flags & CRYPTO_F_CBIFSYNC) &&
1133 (CRYPTO_SESID2CAPS(crp->crp_sid) & CRYPTOCAP_F_SYNC))) {
1135 * Do the callback directly. This is ok when the
1136 * callback routine does very little (e.g. the
1137 * /dev/crypto callback method just does a wakeup).
1139 #ifdef CRYPTO_TIMING
1140 if (crypto_timing) {
1142 * NB: We must copy the timestamp before
1143 * doing the callback as the cryptop is
1144 * likely to be reclaimed.
1146 struct bintime t = crp->crp_tstamp;
1147 crypto_tstat(&cryptostats.cs_cb, &t);
1148 crp->crp_callback(crp);
1149 crypto_tstat(&cryptostats.cs_finis, &t);
1152 crp->crp_callback(crp);
1155 * Normal case; queue the callback for the thread.
1158 if (CRYPTO_RETQ_EMPTY())
1159 wakeup_one(&crp_ret_q); /* shared wait channel */
1160 TAILQ_INSERT_TAIL(&crp_ret_q, crp, crp_next);
1161 CRYPTO_RETQ_UNLOCK();
1166 * Invoke the callback on behalf of the driver.
1169 crypto_kdone(struct cryptkop *krp)
1171 struct cryptocap *cap;
1173 if (krp->krp_status != 0)
1174 cryptostats.cs_kerrs++;
1175 CRYPTO_DRIVER_LOCK();
1176 /* XXX: What if driver is loaded in the meantime? */
1177 if (krp->krp_hid < crypto_drivers_num) {
1178 cap = &crypto_drivers[krp->krp_hid];
1179 cap->cc_koperations--;
1180 KASSERT(cap->cc_koperations >= 0, ("cc_koperations < 0"));
1181 if (cap->cc_flags & CRYPTOCAP_F_CLEANUP)
1184 CRYPTO_DRIVER_UNLOCK();
1186 if (CRYPTO_RETQ_EMPTY())
1187 wakeup_one(&crp_ret_q); /* shared wait channel */
1188 TAILQ_INSERT_TAIL(&crp_ret_kq, krp, krp_next);
1189 CRYPTO_RETQ_UNLOCK();
1193 crypto_getfeat(int *featp)
1195 int hid, kalg, feat = 0;
1197 CRYPTO_DRIVER_LOCK();
1198 for (hid = 0; hid < crypto_drivers_num; hid++) {
1199 const struct cryptocap *cap = &crypto_drivers[hid];
1201 if ((cap->cc_flags & CRYPTOCAP_F_SOFTWARE) &&
1202 !crypto_devallowsoft) {
1205 for (kalg = 0; kalg < CRK_ALGORITHM_MAX; kalg++)
1206 if (cap->cc_kalg[kalg] & CRYPTO_ALG_FLAG_SUPPORTED)
1209 CRYPTO_DRIVER_UNLOCK();
1215 * Terminate a thread at module unload. The process that
1216 * initiated this is waiting for us to signal that we're gone;
1217 * wake it up and exit. We use the driver table lock to insure
1218 * we don't do the wakeup before they're waiting. There is no
1219 * race here because the waiter sleeps on the proc lock for the
1220 * thread so it gets notified at the right time because of an
1221 * extra wakeup that's done in exit1().
1224 crypto_finis(void *chan)
1226 CRYPTO_DRIVER_LOCK();
1228 CRYPTO_DRIVER_UNLOCK();
1233 * Crypto thread, dispatches crypto requests.
1238 struct cryptop *crp, *submit;
1239 struct cryptkop *krp;
1240 struct cryptocap *cap;
1247 * Find the first element in the queue that can be
1248 * processed and look-ahead to see if multiple ops
1249 * are ready for the same driver.
1253 TAILQ_FOREACH(crp, &crp_q, crp_next) {
1254 hid = CRYPTO_SESID2HID(crp->crp_sid);
1255 cap = crypto_checkdriver(hid);
1257 * Driver cannot disappeared when there is an active
1260 KASSERT(cap != NULL, ("%s:%u Driver disappeared.",
1261 __func__, __LINE__));
1262 if (cap == NULL || cap->cc_dev == NULL) {
1263 /* Op needs to be migrated, process it. */
1268 if (!cap->cc_qblocked) {
1269 if (submit != NULL) {
1271 * We stop on finding another op,
1272 * regardless whether its for the same
1273 * driver or not. We could keep
1274 * searching the queue but it might be
1275 * better to just use a per-driver
1278 if (CRYPTO_SESID2HID(submit->crp_sid) == hid)
1279 hint = CRYPTO_HINT_MORE;
1283 if ((submit->crp_flags & CRYPTO_F_BATCH) == 0)
1285 /* keep scanning for more are q'd */
1289 if (submit != NULL) {
1290 TAILQ_REMOVE(&crp_q, submit, crp_next);
1291 hid = CRYPTO_SESID2HID(submit->crp_sid);
1292 cap = crypto_checkdriver(hid);
1293 KASSERT(cap != NULL, ("%s:%u Driver disappeared.",
1294 __func__, __LINE__));
1295 result = crypto_invoke(cap, submit, hint);
1296 if (result == ERESTART) {
1298 * The driver ran out of resources, mark the
1299 * driver ``blocked'' for cryptop's and put
1300 * the request back in the queue. It would
1301 * best to put the request back where we got
1302 * it but that's hard so for now we put it
1303 * at the front. This should be ok; putting
1304 * it at the end does not work.
1306 /* XXX validate sid again? */
1307 crypto_drivers[CRYPTO_SESID2HID(submit->crp_sid)].cc_qblocked = 1;
1308 TAILQ_INSERT_HEAD(&crp_q, submit, crp_next);
1309 cryptostats.cs_blocks++;
1313 /* As above, but for key ops */
1314 TAILQ_FOREACH(krp, &crp_kq, krp_next) {
1315 cap = crypto_checkdriver(krp->krp_hid);
1316 if (cap == NULL || cap->cc_dev == NULL) {
1318 * Operation needs to be migrated, invalidate
1319 * the assigned device so it will reselect a
1320 * new one below. Propagate the original
1321 * crid selection flags if supplied.
1323 krp->krp_hid = krp->krp_crid &
1324 (CRYPTOCAP_F_SOFTWARE|CRYPTOCAP_F_HARDWARE);
1325 if (krp->krp_hid == 0)
1327 CRYPTOCAP_F_SOFTWARE|CRYPTOCAP_F_HARDWARE;
1330 if (!cap->cc_kqblocked)
1334 TAILQ_REMOVE(&crp_kq, krp, krp_next);
1335 result = crypto_kinvoke(krp, krp->krp_hid);
1336 if (result == ERESTART) {
1338 * The driver ran out of resources, mark the
1339 * driver ``blocked'' for cryptkop's and put
1340 * the request back in the queue. It would
1341 * best to put the request back where we got
1342 * it but that's hard so for now we put it
1343 * at the front. This should be ok; putting
1344 * it at the end does not work.
1346 /* XXX validate sid again? */
1347 crypto_drivers[krp->krp_hid].cc_kqblocked = 1;
1348 TAILQ_INSERT_HEAD(&crp_kq, krp, krp_next);
1349 cryptostats.cs_kblocks++;
1353 if (submit == NULL && krp == NULL) {
1355 * Nothing more to be processed. Sleep until we're
1356 * woken because there are more ops to process.
1357 * This happens either by submission or by a driver
1358 * becoming unblocked and notifying us through
1359 * crypto_unblock. Note that when we wakeup we
1360 * start processing each queue again from the
1361 * front. It's not clear that it's important to
1362 * preserve this ordering since ops may finish
1363 * out of order if dispatched to different devices
1364 * and some become blocked while others do not.
1367 msleep(&crp_q, &crypto_q_mtx, PWAIT, "crypto_wait", 0);
1369 if (cryptoproc == NULL)
1371 cryptostats.cs_intrs++;
1376 crypto_finis(&crp_q);
1380 * Crypto returns thread, does callbacks for processed crypto requests.
1381 * Callbacks are done here, rather than in the crypto drivers, because
1382 * callbacks typically are expensive and would slow interrupt handling.
1385 crypto_ret_proc(void)
1387 struct cryptop *crpt;
1388 struct cryptkop *krpt;
1392 /* Harvest return q's for completed ops */
1393 crpt = TAILQ_FIRST(&crp_ret_q);
1395 TAILQ_REMOVE(&crp_ret_q, crpt, crp_next);
1397 krpt = TAILQ_FIRST(&crp_ret_kq);
1399 TAILQ_REMOVE(&crp_ret_kq, krpt, krp_next);
1401 if (crpt != NULL || krpt != NULL) {
1402 CRYPTO_RETQ_UNLOCK();
1404 * Run callbacks unlocked.
1407 #ifdef CRYPTO_TIMING
1408 if (crypto_timing) {
1410 * NB: We must copy the timestamp before
1411 * doing the callback as the cryptop is
1412 * likely to be reclaimed.
1414 struct bintime t = crpt->crp_tstamp;
1415 crypto_tstat(&cryptostats.cs_cb, &t);
1416 crpt->crp_callback(crpt);
1417 crypto_tstat(&cryptostats.cs_finis, &t);
1420 crpt->crp_callback(crpt);
1423 krpt->krp_callback(krpt);
1427 * Nothing more to be processed. Sleep until we're
1428 * woken because there are more returns to process.
1430 msleep(&crp_ret_q, &crypto_ret_q_mtx, PWAIT,
1431 "crypto_ret_wait", 0);
1432 if (cryptoretproc == NULL)
1434 cryptostats.cs_rets++;
1437 CRYPTO_RETQ_UNLOCK();
1439 crypto_finis(&crp_ret_q);
1444 db_show_drivers(void)
1448 db_printf("%12s %4s %4s %8s %2s %2s\n"
1456 for (hid = 0; hid < crypto_drivers_num; hid++) {
1457 const struct cryptocap *cap = &crypto_drivers[hid];
1458 if (cap->cc_dev == NULL)
1460 db_printf("%-12s %4u %4u %08x %2u %2u\n"
1461 , device_get_nameunit(cap->cc_dev)
1463 , cap->cc_koperations
1471 DB_SHOW_COMMAND(crypto, db_show_crypto)
1473 struct cryptop *crp;
1478 db_printf("%4s %8s %4s %4s %4s %4s %8s %8s\n",
1479 "HID", "Caps", "Ilen", "Olen", "Etype", "Flags",
1480 "Desc", "Callback");
1481 TAILQ_FOREACH(crp, &crp_q, crp_next) {
1482 db_printf("%4u %08x %4u %4u %4u %04x %8p %8p\n"
1483 , (int) CRYPTO_SESID2HID(crp->crp_sid)
1484 , (int) CRYPTO_SESID2CAPS(crp->crp_sid)
1485 , crp->crp_ilen, crp->crp_olen
1492 if (!TAILQ_EMPTY(&crp_ret_q)) {
1493 db_printf("\n%4s %4s %4s %8s\n",
1494 "HID", "Etype", "Flags", "Callback");
1495 TAILQ_FOREACH(crp, &crp_ret_q, crp_next) {
1496 db_printf("%4u %4u %04x %8p\n"
1497 , (int) CRYPTO_SESID2HID(crp->crp_sid)
1506 DB_SHOW_COMMAND(kcrypto, db_show_kcrypto)
1508 struct cryptkop *krp;
1513 db_printf("%4s %5s %4s %4s %8s %4s %8s\n",
1514 "Op", "Status", "#IP", "#OP", "CRID", "HID", "Callback");
1515 TAILQ_FOREACH(krp, &crp_kq, krp_next) {
1516 db_printf("%4u %5u %4u %4u %08x %4u %8p\n"
1519 , krp->krp_iparams, krp->krp_oparams
1520 , krp->krp_crid, krp->krp_hid
1524 if (!TAILQ_EMPTY(&crp_ret_q)) {
1525 db_printf("%4s %5s %8s %4s %8s\n",
1526 "Op", "Status", "CRID", "HID", "Callback");
1527 TAILQ_FOREACH(krp, &crp_ret_kq, krp_next) {
1528 db_printf("%4u %5u %08x %4u %8p\n"
1531 , krp->krp_crid, krp->krp_hid
1539 int crypto_modevent(module_t mod, int type, void *unused);
1542 * Initialization code, both for static and dynamic loading.
1543 * Note this is not invoked with the usual MODULE_DECLARE
1544 * mechanism but instead is listed as a dependency by the
1545 * cryptosoft driver. This guarantees proper ordering of
1546 * calls on module load/unload.
1549 crypto_modevent(module_t mod, int type, void *unused)
1555 error = crypto_init();
1556 if (error == 0 && bootverbose)
1557 printf("crypto: <crypto core>\n");
1560 /*XXX disallow if active sessions */
1567 MODULE_VERSION(crypto, 1);
1568 MODULE_DEPEND(crypto, zlib, 1, 1, 1);