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 */
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/eventhandler.h>
64 #include <sys/kernel.h>
65 #include <sys/kthread.h>
67 #include <sys/module.h>
68 #include <sys/mutex.h>
69 #include <sys/malloc.h>
71 #include <sys/sysctl.h>
76 #include <opencrypto/cryptodev.h>
77 #include <opencrypto/xform.h> /* XXX for M_XDATA */
81 #include "cryptodev_if.h"
84 * Crypto drivers register themselves by allocating a slot in the
85 * crypto_drivers table with crypto_get_driverid() and then registering
86 * each algorithm they support with crypto_register() and crypto_kregister().
88 static struct mtx crypto_drivers_mtx; /* lock on driver table */
89 #define CRYPTO_DRIVER_LOCK() mtx_lock(&crypto_drivers_mtx)
90 #define CRYPTO_DRIVER_UNLOCK() mtx_unlock(&crypto_drivers_mtx)
91 #define CRYPTO_DRIVER_ASSERT() mtx_assert(&crypto_drivers_mtx, MA_OWNED)
94 * Crypto device/driver capabilities structure.
97 * (d) - protected by CRYPTO_DRIVER_LOCK()
98 * (q) - protected by CRYPTO_Q_LOCK()
99 * Not tagged fields are read-only.
102 device_t cc_dev; /* (d) device/driver */
103 u_int32_t cc_sessions; /* (d) # of sessions */
104 u_int32_t cc_koperations; /* (d) # os asym operations */
106 * Largest possible operator length (in bits) for each type of
107 * encryption algorithm. XXX not used
109 u_int16_t cc_max_op_len[CRYPTO_ALGORITHM_MAX + 1];
110 u_int8_t cc_alg[CRYPTO_ALGORITHM_MAX + 1];
111 u_int8_t cc_kalg[CRK_ALGORITHM_MAX + 1];
113 int cc_flags; /* (d) flags */
114 #define CRYPTOCAP_F_CLEANUP 0x80000000 /* needs resource cleanup */
115 int cc_qblocked; /* (q) symmetric q blocked */
116 int cc_kqblocked; /* (q) asymmetric q blocked */
118 static struct cryptocap *crypto_drivers = NULL;
119 static int crypto_drivers_num = 0;
122 * There are two queues for crypto requests; one for symmetric (e.g.
123 * cipher) operations and one for asymmetric (e.g. MOD)operations.
124 * A single mutex is used to lock access to both queues. We could
125 * have one per-queue but having one simplifies handling of block/unblock
128 static int crp_sleep = 0;
129 static TAILQ_HEAD(,cryptop) crp_q; /* request queues */
130 static TAILQ_HEAD(,cryptkop) crp_kq;
131 static struct mtx crypto_q_mtx;
132 #define CRYPTO_Q_LOCK() mtx_lock(&crypto_q_mtx)
133 #define CRYPTO_Q_UNLOCK() mtx_unlock(&crypto_q_mtx)
136 * There are two queues for processing completed crypto requests; one
137 * for the symmetric and one for the asymmetric ops. We only need one
138 * but have two to avoid type futzing (cryptop vs. cryptkop). A single
139 * mutex is used to lock access to both queues. Note that this lock
140 * must be separate from the lock on request queues to insure driver
141 * callbacks don't generate lock order reversals.
143 static TAILQ_HEAD(,cryptop) crp_ret_q; /* callback queues */
144 static TAILQ_HEAD(,cryptkop) crp_ret_kq;
145 static struct mtx crypto_ret_q_mtx;
146 #define CRYPTO_RETQ_LOCK() mtx_lock(&crypto_ret_q_mtx)
147 #define CRYPTO_RETQ_UNLOCK() mtx_unlock(&crypto_ret_q_mtx)
148 #define CRYPTO_RETQ_EMPTY() (TAILQ_EMPTY(&crp_ret_q) && TAILQ_EMPTY(&crp_ret_kq))
150 static uma_zone_t cryptop_zone;
151 static uma_zone_t cryptodesc_zone;
153 int crypto_userasymcrypto = 1; /* userland may do asym crypto reqs */
154 SYSCTL_INT(_kern, OID_AUTO, userasymcrypto, CTLFLAG_RW,
155 &crypto_userasymcrypto, 0,
156 "Enable/disable user-mode access to asymmetric crypto support");
157 int crypto_devallowsoft = 0; /* only use hardware crypto for asym */
158 SYSCTL_INT(_kern, OID_AUTO, cryptodevallowsoft, CTLFLAG_RW,
159 &crypto_devallowsoft, 0,
160 "Enable/disable use of software asym crypto support");
162 MALLOC_DEFINE(M_CRYPTO_DATA, "crypto", "crypto session records");
164 static void crypto_proc(void);
165 static struct proc *cryptoproc;
166 static void crypto_ret_proc(void);
167 static struct proc *cryptoretproc;
168 static void crypto_destroy(void);
169 static int crypto_invoke(struct cryptocap *cap, struct cryptop *crp, int hint);
170 static int crypto_kinvoke(struct cryptkop *krp, int flags);
172 static struct cryptostats cryptostats;
173 SYSCTL_STRUCT(_kern, OID_AUTO, crypto_stats, CTLFLAG_RW, &cryptostats,
174 cryptostats, "Crypto system statistics");
177 static int crypto_timing = 0;
178 SYSCTL_INT(_debug, OID_AUTO, crypto_timing, CTLFLAG_RW,
179 &crypto_timing, 0, "Enable/disable crypto timing support");
187 mtx_init(&crypto_drivers_mtx, "crypto", "crypto driver table",
192 mtx_init(&crypto_q_mtx, "crypto", "crypto op queues", MTX_DEF);
194 TAILQ_INIT(&crp_ret_q);
195 TAILQ_INIT(&crp_ret_kq);
196 mtx_init(&crypto_ret_q_mtx, "crypto", "crypto return queues", MTX_DEF);
198 cryptop_zone = uma_zcreate("cryptop", sizeof (struct cryptop),
200 UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
201 cryptodesc_zone = uma_zcreate("cryptodesc", sizeof (struct cryptodesc),
203 UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
204 if (cryptodesc_zone == NULL || cryptop_zone == NULL) {
205 printf("crypto_init: cannot setup crypto zones\n");
210 crypto_drivers_num = CRYPTO_DRIVERS_INITIAL;
211 crypto_drivers = malloc(crypto_drivers_num *
212 sizeof(struct cryptocap), M_CRYPTO_DATA, M_NOWAIT | M_ZERO);
213 if (crypto_drivers == NULL) {
214 printf("crypto_init: cannot setup crypto drivers\n");
219 error = kproc_create((void (*)(void *)) crypto_proc, NULL,
220 &cryptoproc, 0, 0, "crypto");
222 printf("crypto_init: cannot start crypto thread; error %d",
227 error = kproc_create((void (*)(void *)) crypto_ret_proc, NULL,
228 &cryptoretproc, 0, 0, "crypto returns");
230 printf("crypto_init: cannot start cryptoret thread; error %d",
241 * Signal a crypto thread to terminate. We use the driver
242 * table lock to synchronize the sleep/wakeups so that we
243 * are sure the threads have terminated before we release
244 * the data structures they use. See crypto_finis below
245 * for the other half of this song-and-dance.
248 crypto_terminate(struct proc **pp, void *q)
252 mtx_assert(&crypto_drivers_mtx, MA_OWNED);
257 PROC_LOCK(p); /* NB: insure we don't miss wakeup */
258 CRYPTO_DRIVER_UNLOCK(); /* let crypto_finis progress */
259 msleep(p, &p->p_mtx, PWAIT, "crypto_destroy", 0);
261 CRYPTO_DRIVER_LOCK();
269 * Terminate any crypto threads.
271 CRYPTO_DRIVER_LOCK();
272 crypto_terminate(&cryptoproc, &crp_q);
273 crypto_terminate(&cryptoretproc, &crp_ret_q);
274 CRYPTO_DRIVER_UNLOCK();
276 /* XXX flush queues??? */
279 * Reclaim dynamically allocated resources.
281 if (crypto_drivers != NULL)
282 free(crypto_drivers, M_CRYPTO_DATA);
284 if (cryptodesc_zone != NULL)
285 uma_zdestroy(cryptodesc_zone);
286 if (cryptop_zone != NULL)
287 uma_zdestroy(cryptop_zone);
288 mtx_destroy(&crypto_q_mtx);
289 mtx_destroy(&crypto_ret_q_mtx);
290 mtx_destroy(&crypto_drivers_mtx);
293 static struct cryptocap *
294 crypto_checkdriver(u_int32_t hid)
296 if (crypto_drivers == NULL)
298 return (hid >= crypto_drivers_num ? NULL : &crypto_drivers[hid]);
302 * Compare a driver's list of supported algorithms against another
303 * list; return non-zero if all algorithms are supported.
306 driver_suitable(const struct cryptocap *cap, const struct cryptoini *cri)
308 const struct cryptoini *cr;
310 /* See if all the algorithms are supported. */
311 for (cr = cri; cr; cr = cr->cri_next)
312 if (cap->cc_alg[cr->cri_alg] == 0)
318 * Select a driver for a new session that supports the specified
319 * algorithms and, optionally, is constrained according to the flags.
320 * The algorithm we use here is pretty stupid; just use the
321 * first driver that supports all the algorithms we need. If there
322 * are multiple drivers we choose the driver with the fewest active
323 * sessions. We prefer hardware-backed drivers to software ones.
325 * XXX We need more smarts here (in real life too, but that's
326 * XXX another story altogether).
328 static struct cryptocap *
329 crypto_select_driver(const struct cryptoini *cri, int flags)
331 struct cryptocap *cap, *best;
334 CRYPTO_DRIVER_ASSERT();
337 * Look first for hardware crypto devices if permitted.
339 if (flags & CRYPTOCAP_F_HARDWARE)
340 match = CRYPTOCAP_F_HARDWARE;
342 match = CRYPTOCAP_F_SOFTWARE;
345 for (hid = 0; hid < crypto_drivers_num; hid++) {
346 cap = &crypto_drivers[hid];
348 * If it's not initialized, is in the process of
349 * going away, or is not appropriate (hardware
350 * or software based on match), then skip.
352 if (cap->cc_dev == NULL ||
353 (cap->cc_flags & CRYPTOCAP_F_CLEANUP) ||
354 (cap->cc_flags & match) == 0)
357 /* verify all the algorithms are supported. */
358 if (driver_suitable(cap, cri)) {
360 cap->cc_sessions < best->cc_sessions)
366 if (match == CRYPTOCAP_F_HARDWARE && (flags & CRYPTOCAP_F_SOFTWARE)) {
367 /* sort of an Algol 68-style for loop */
368 match = CRYPTOCAP_F_SOFTWARE;
375 * Create a new session. The crid argument specifies a crypto
376 * driver to use or constraints on a driver to select (hardware
377 * only, software only, either). Whatever driver is selected
378 * must be capable of the requested crypto algorithms.
381 crypto_newsession(u_int64_t *sid, struct cryptoini *cri, int crid)
383 struct cryptocap *cap;
387 CRYPTO_DRIVER_LOCK();
388 if ((crid & (CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE)) == 0) {
390 * Use specified driver; verify it is capable.
392 cap = crypto_checkdriver(crid);
393 if (cap != NULL && !driver_suitable(cap, cri))
397 * No requested driver; select based on crid flags.
399 cap = crypto_select_driver(cri, crid);
401 * if NULL then can't do everything in one session.
402 * XXX Fix this. We need to inject a "virtual" session
403 * XXX layer right about here.
407 /* Call the driver initialization routine. */
408 hid = cap - crypto_drivers;
409 lid = hid; /* Pass the driver ID. */
410 err = CRYPTODEV_NEWSESSION(cap->cc_dev, &lid, cri);
412 (*sid) = (cap->cc_flags & 0xff000000)
413 | (hid & 0x00ffffff);
415 (*sid) |= (lid & 0xffffffff);
420 CRYPTO_DRIVER_UNLOCK();
425 crypto_remove(struct cryptocap *cap)
428 mtx_assert(&crypto_drivers_mtx, MA_OWNED);
429 if (cap->cc_sessions == 0 && cap->cc_koperations == 0)
430 bzero(cap, sizeof(*cap));
434 * Delete an existing session (or a reserved session on an unregistered
438 crypto_freesession(u_int64_t sid)
440 struct cryptocap *cap;
444 CRYPTO_DRIVER_LOCK();
446 if (crypto_drivers == NULL) {
451 /* Determine two IDs. */
452 hid = CRYPTO_SESID2HID(sid);
454 if (hid >= crypto_drivers_num) {
458 cap = &crypto_drivers[hid];
460 if (cap->cc_sessions)
463 /* Call the driver cleanup routine, if available. */
464 err = CRYPTODEV_FREESESSION(cap->cc_dev, sid);
466 if (cap->cc_flags & CRYPTOCAP_F_CLEANUP)
470 CRYPTO_DRIVER_UNLOCK();
475 * Return an unused driver id. Used by drivers prior to registering
476 * support for the algorithms they handle.
479 crypto_get_driverid(device_t dev, int flags)
481 struct cryptocap *newdrv;
484 if ((flags & (CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE)) == 0) {
485 printf("%s: no flags specified when registering driver\n",
486 device_get_nameunit(dev));
490 CRYPTO_DRIVER_LOCK();
492 for (i = 0; i < crypto_drivers_num; i++) {
493 if (crypto_drivers[i].cc_dev == NULL &&
494 (crypto_drivers[i].cc_flags & CRYPTOCAP_F_CLEANUP) == 0) {
499 /* Out of entries, allocate some more. */
500 if (i == crypto_drivers_num) {
501 /* Be careful about wrap-around. */
502 if (2 * crypto_drivers_num <= crypto_drivers_num) {
503 CRYPTO_DRIVER_UNLOCK();
504 printf("crypto: driver count wraparound!\n");
508 newdrv = malloc(2 * crypto_drivers_num *
509 sizeof(struct cryptocap), M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
510 if (newdrv == NULL) {
511 CRYPTO_DRIVER_UNLOCK();
512 printf("crypto: no space to expand driver table!\n");
516 bcopy(crypto_drivers, newdrv,
517 crypto_drivers_num * sizeof(struct cryptocap));
519 crypto_drivers_num *= 2;
521 free(crypto_drivers, M_CRYPTO_DATA);
522 crypto_drivers = newdrv;
525 /* NB: state is zero'd on free */
526 crypto_drivers[i].cc_sessions = 1; /* Mark */
527 crypto_drivers[i].cc_dev = dev;
528 crypto_drivers[i].cc_flags = flags;
530 printf("crypto: assign %s driver id %u, flags %u\n",
531 device_get_nameunit(dev), i, flags);
533 CRYPTO_DRIVER_UNLOCK();
539 * Lookup a driver by name. We match against the full device
540 * name and unit, and against just the name. The latter gives
541 * us a simple widlcarding by device name. On success return the
542 * driver/hardware identifier; otherwise return -1.
545 crypto_find_driver(const char *match)
547 int i, len = strlen(match);
549 CRYPTO_DRIVER_LOCK();
550 for (i = 0; i < crypto_drivers_num; i++) {
551 device_t dev = crypto_drivers[i].cc_dev;
553 (crypto_drivers[i].cc_flags & CRYPTOCAP_F_CLEANUP))
555 if (strncmp(match, device_get_nameunit(dev), len) == 0 ||
556 strncmp(match, device_get_name(dev), len) == 0)
559 CRYPTO_DRIVER_UNLOCK();
560 return i < crypto_drivers_num ? i : -1;
564 * Return the device_t for the specified driver or NULL
565 * if the driver identifier is invalid.
568 crypto_find_device_byhid(int hid)
570 struct cryptocap *cap = crypto_checkdriver(hid);
571 return cap != NULL ? cap->cc_dev : NULL;
575 * Return the device/driver capabilities.
578 crypto_getcaps(int hid)
580 struct cryptocap *cap = crypto_checkdriver(hid);
581 return cap != NULL ? cap->cc_flags : 0;
585 * Register support for a key-related algorithm. This routine
586 * is called once for each algorithm supported a driver.
589 crypto_kregister(u_int32_t driverid, int kalg, u_int32_t flags)
591 struct cryptocap *cap;
594 CRYPTO_DRIVER_LOCK();
596 cap = crypto_checkdriver(driverid);
598 (CRK_ALGORITM_MIN <= kalg && kalg <= CRK_ALGORITHM_MAX)) {
600 * XXX Do some performance testing to determine placing.
601 * XXX We probably need an auxiliary data structure that
602 * XXX describes relative performances.
605 cap->cc_kalg[kalg] = flags | CRYPTO_ALG_FLAG_SUPPORTED;
607 printf("crypto: %s registers key alg %u flags %u\n"
608 , device_get_nameunit(cap->cc_dev)
616 CRYPTO_DRIVER_UNLOCK();
621 * Register support for a non-key-related algorithm. This routine
622 * is called once for each such algorithm supported by a driver.
625 crypto_register(u_int32_t driverid, int alg, u_int16_t maxoplen,
628 struct cryptocap *cap;
631 CRYPTO_DRIVER_LOCK();
633 cap = crypto_checkdriver(driverid);
634 /* NB: algorithms are in the range [1..max] */
636 (CRYPTO_ALGORITHM_MIN <= alg && alg <= CRYPTO_ALGORITHM_MAX)) {
638 * XXX Do some performance testing to determine placing.
639 * XXX We probably need an auxiliary data structure that
640 * XXX describes relative performances.
643 cap->cc_alg[alg] = flags | CRYPTO_ALG_FLAG_SUPPORTED;
644 cap->cc_max_op_len[alg] = maxoplen;
646 printf("crypto: %s registers alg %u flags %u maxoplen %u\n"
647 , device_get_nameunit(cap->cc_dev)
652 cap->cc_sessions = 0; /* Unmark */
657 CRYPTO_DRIVER_UNLOCK();
662 driver_finis(struct cryptocap *cap)
666 CRYPTO_DRIVER_ASSERT();
668 ses = cap->cc_sessions;
669 kops = cap->cc_koperations;
670 bzero(cap, sizeof(*cap));
671 if (ses != 0 || kops != 0) {
673 * If there are pending sessions,
674 * just mark as invalid.
676 cap->cc_flags |= CRYPTOCAP_F_CLEANUP;
677 cap->cc_sessions = ses;
678 cap->cc_koperations = kops;
683 * Unregister a crypto driver. If there are pending sessions using it,
684 * leave enough information around so that subsequent calls using those
685 * sessions will correctly detect the driver has been unregistered and
689 crypto_unregister(u_int32_t driverid, int alg)
691 struct cryptocap *cap;
694 CRYPTO_DRIVER_LOCK();
695 cap = crypto_checkdriver(driverid);
697 (CRYPTO_ALGORITHM_MIN <= alg && alg <= CRYPTO_ALGORITHM_MAX) &&
698 cap->cc_alg[alg] != 0) {
699 cap->cc_alg[alg] = 0;
700 cap->cc_max_op_len[alg] = 0;
702 /* Was this the last algorithm ? */
703 for (i = 1; i <= CRYPTO_ALGORITHM_MAX; i++)
704 if (cap->cc_alg[i] != 0)
707 if (i == CRYPTO_ALGORITHM_MAX + 1)
712 CRYPTO_DRIVER_UNLOCK();
718 * Unregister all algorithms associated with a crypto driver.
719 * If there are pending sessions using it, leave enough information
720 * around so that subsequent calls using those sessions will
721 * correctly detect the driver has been unregistered and reroute
725 crypto_unregister_all(u_int32_t driverid)
727 struct cryptocap *cap;
730 CRYPTO_DRIVER_LOCK();
731 cap = crypto_checkdriver(driverid);
737 CRYPTO_DRIVER_UNLOCK();
743 * Clear blockage on a driver. The what parameter indicates whether
744 * the driver is now ready for cryptop's and/or cryptokop's.
747 crypto_unblock(u_int32_t driverid, int what)
749 struct cryptocap *cap;
753 cap = crypto_checkdriver(driverid);
755 if (what & CRYPTO_SYMQ)
756 cap->cc_qblocked = 0;
757 if (what & CRYPTO_ASYMQ)
758 cap->cc_kqblocked = 0;
770 * Add a crypto request to a queue, to be processed by the kernel thread.
773 crypto_dispatch(struct cryptop *crp)
775 struct cryptocap *cap;
779 cryptostats.cs_ops++;
783 binuptime(&crp->crp_tstamp);
786 hid = CRYPTO_SESID2HID(crp->crp_sid);
788 if ((crp->crp_flags & CRYPTO_F_BATCH) == 0) {
790 * Caller marked the request to be processed
791 * immediately; dispatch it directly to the
792 * driver unless the driver is currently blocked.
794 cap = crypto_checkdriver(hid);
795 /* Driver cannot disappeared when there is an active session. */
796 KASSERT(cap != NULL, ("%s: Driver disappeared.", __func__));
797 if (!cap->cc_qblocked) {
798 result = crypto_invoke(cap, crp, 0);
799 if (result != ERESTART)
802 * The driver ran out of resources, put the request on
808 TAILQ_INSERT_TAIL(&crp_q, crp, crp_next);
816 * Add an asymetric crypto request to a queue,
817 * to be processed by the kernel thread.
820 crypto_kdispatch(struct cryptkop *krp)
824 cryptostats.cs_kops++;
826 error = crypto_kinvoke(krp, krp->krp_crid);
827 if (error == ERESTART) {
829 TAILQ_INSERT_TAIL(&crp_kq, krp, krp_next);
839 * Verify a driver is suitable for the specified operation.
842 kdriver_suitable(const struct cryptocap *cap, const struct cryptkop *krp)
844 return (cap->cc_kalg[krp->krp_op] & CRYPTO_ALG_FLAG_SUPPORTED) != 0;
848 * Select a driver for an asym operation. The driver must
849 * support the necessary algorithm. The caller can constrain
850 * which device is selected with the flags parameter. The
851 * algorithm we use here is pretty stupid; just use the first
852 * driver that supports the algorithms we need. If there are
853 * multiple suitable drivers we choose the driver with the
854 * fewest active operations. We prefer hardware-backed
855 * drivers to software ones when either may be used.
857 static struct cryptocap *
858 crypto_select_kdriver(const struct cryptkop *krp, int flags)
860 struct cryptocap *cap, *best, *blocked;
863 CRYPTO_DRIVER_ASSERT();
866 * Look first for hardware crypto devices if permitted.
868 if (flags & CRYPTOCAP_F_HARDWARE)
869 match = CRYPTOCAP_F_HARDWARE;
871 match = CRYPTOCAP_F_SOFTWARE;
875 for (hid = 0; hid < crypto_drivers_num; hid++) {
876 cap = &crypto_drivers[hid];
878 * If it's not initialized, is in the process of
879 * going away, or is not appropriate (hardware
880 * or software based on match), then skip.
882 if (cap->cc_dev == NULL ||
883 (cap->cc_flags & CRYPTOCAP_F_CLEANUP) ||
884 (cap->cc_flags & match) == 0)
887 /* verify all the algorithms are supported. */
888 if (kdriver_suitable(cap, krp)) {
890 cap->cc_koperations < best->cc_koperations)
896 if (match == CRYPTOCAP_F_HARDWARE && (flags & CRYPTOCAP_F_SOFTWARE)) {
897 /* sort of an Algol 68-style for loop */
898 match = CRYPTOCAP_F_SOFTWARE;
905 * Dispatch an assymetric crypto request.
908 crypto_kinvoke(struct cryptkop *krp, int crid)
910 struct cryptocap *cap = NULL;
913 KASSERT(krp != NULL, ("%s: krp == NULL", __func__));
914 KASSERT(krp->krp_callback != NULL,
915 ("%s: krp->crp_callback == NULL", __func__));
917 CRYPTO_DRIVER_LOCK();
918 if ((crid & (CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE)) == 0) {
919 cap = crypto_checkdriver(crid);
922 * Driver present, it must support the necessary
923 * algorithm and, if s/w drivers are excluded,
924 * it must be registered as hardware-backed.
926 if (!kdriver_suitable(cap, krp) ||
927 (!crypto_devallowsoft &&
928 (cap->cc_flags & CRYPTOCAP_F_HARDWARE) == 0))
933 * No requested driver; select based on crid flags.
935 if (!crypto_devallowsoft) /* NB: disallow s/w drivers */
936 crid &= ~CRYPTOCAP_F_SOFTWARE;
937 cap = crypto_select_kdriver(krp, crid);
939 if (cap != NULL && !cap->cc_kqblocked) {
940 krp->krp_hid = cap - crypto_drivers;
941 cap->cc_koperations++;
942 CRYPTO_DRIVER_UNLOCK();
943 error = CRYPTODEV_KPROCESS(cap->cc_dev, krp, 0);
944 CRYPTO_DRIVER_LOCK();
945 if (error == ERESTART) {
946 cap->cc_koperations--;
947 CRYPTO_DRIVER_UNLOCK();
952 * NB: cap is !NULL if device is blocked; in
953 * that case return ERESTART so the operation
954 * is resubmitted if possible.
956 error = (cap == NULL) ? ENODEV : ERESTART;
958 CRYPTO_DRIVER_UNLOCK();
961 krp->krp_status = error;
969 crypto_tstat(struct cryptotstat *ts, struct bintime *bt)
971 struct bintime now, delta;
977 delta.frac = now.frac - bt->frac;
978 delta.sec = now.sec - bt->sec;
981 bintime2timespec(&delta, &t);
982 timespecadd(&ts->acc, &t);
983 if (timespeccmp(&t, &ts->min, <))
985 if (timespeccmp(&t, &ts->max, >))
994 * Dispatch a crypto request to the appropriate crypto devices.
997 crypto_invoke(struct cryptocap *cap, struct cryptop *crp, int hint)
1000 KASSERT(crp != NULL, ("%s: crp == NULL", __func__));
1001 KASSERT(crp->crp_callback != NULL,
1002 ("%s: crp->crp_callback == NULL", __func__));
1003 KASSERT(crp->crp_desc != NULL, ("%s: crp->crp_desc == NULL", __func__));
1005 #ifdef CRYPTO_TIMING
1007 crypto_tstat(&cryptostats.cs_invoke, &crp->crp_tstamp);
1009 if (cap->cc_flags & CRYPTOCAP_F_CLEANUP) {
1010 struct cryptodesc *crd;
1014 * Driver has unregistered; migrate the session and return
1015 * an error to the caller so they'll resubmit the op.
1017 * XXX: What if there are more already queued requests for this
1020 crypto_freesession(crp->crp_sid);
1022 for (crd = crp->crp_desc; crd->crd_next; crd = crd->crd_next)
1023 crd->CRD_INI.cri_next = &(crd->crd_next->CRD_INI);
1025 /* XXX propagate flags from initial session? */
1026 if (crypto_newsession(&nid, &(crp->crp_desc->CRD_INI),
1027 CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE) == 0)
1030 crp->crp_etype = EAGAIN;
1035 * Invoke the driver to process the request.
1037 return CRYPTODEV_PROCESS(cap->cc_dev, crp, hint);
1042 * Release a set of crypto descriptors.
1045 crypto_freereq(struct cryptop *crp)
1047 struct cryptodesc *crd;
1054 struct cryptop *crp2;
1057 TAILQ_FOREACH(crp2, &crp_q, crp_next) {
1058 KASSERT(crp2 != crp,
1059 ("Freeing cryptop from the crypto queue (%p).",
1064 TAILQ_FOREACH(crp2, &crp_ret_q, crp_next) {
1065 KASSERT(crp2 != crp,
1066 ("Freeing cryptop from the return queue (%p).",
1069 CRYPTO_RETQ_UNLOCK();
1073 while ((crd = crp->crp_desc) != NULL) {
1074 crp->crp_desc = crd->crd_next;
1075 uma_zfree(cryptodesc_zone, crd);
1077 uma_zfree(cryptop_zone, crp);
1081 * Acquire a set of crypto descriptors.
1084 crypto_getreq(int num)
1086 struct cryptodesc *crd;
1087 struct cryptop *crp;
1089 crp = uma_zalloc(cryptop_zone, M_NOWAIT|M_ZERO);
1092 crd = uma_zalloc(cryptodesc_zone, M_NOWAIT|M_ZERO);
1094 crypto_freereq(crp);
1098 crd->crd_next = crp->crp_desc;
1099 crp->crp_desc = crd;
1106 * Invoke the callback on behalf of the driver.
1109 crypto_done(struct cryptop *crp)
1111 KASSERT((crp->crp_flags & CRYPTO_F_DONE) == 0,
1112 ("crypto_done: op already done, flags 0x%x", crp->crp_flags));
1113 crp->crp_flags |= CRYPTO_F_DONE;
1114 if (crp->crp_etype != 0)
1115 cryptostats.cs_errs++;
1116 #ifdef CRYPTO_TIMING
1118 crypto_tstat(&cryptostats.cs_done, &crp->crp_tstamp);
1121 * CBIMM means unconditionally do the callback immediately;
1122 * CBIFSYNC means do the callback immediately only if the
1123 * operation was done synchronously. Both are used to avoid
1124 * doing extraneous context switches; the latter is mostly
1125 * used with the software crypto driver.
1127 if ((crp->crp_flags & CRYPTO_F_CBIMM) ||
1128 ((crp->crp_flags & CRYPTO_F_CBIFSYNC) &&
1129 (CRYPTO_SESID2CAPS(crp->crp_sid) & CRYPTOCAP_F_SYNC))) {
1131 * Do the callback directly. This is ok when the
1132 * callback routine does very little (e.g. the
1133 * /dev/crypto callback method just does a wakeup).
1135 #ifdef CRYPTO_TIMING
1136 if (crypto_timing) {
1138 * NB: We must copy the timestamp before
1139 * doing the callback as the cryptop is
1140 * likely to be reclaimed.
1142 struct bintime t = crp->crp_tstamp;
1143 crypto_tstat(&cryptostats.cs_cb, &t);
1144 crp->crp_callback(crp);
1145 crypto_tstat(&cryptostats.cs_finis, &t);
1148 crp->crp_callback(crp);
1151 * Normal case; queue the callback for the thread.
1154 if (CRYPTO_RETQ_EMPTY())
1155 wakeup_one(&crp_ret_q); /* shared wait channel */
1156 TAILQ_INSERT_TAIL(&crp_ret_q, crp, crp_next);
1157 CRYPTO_RETQ_UNLOCK();
1162 * Invoke the callback on behalf of the driver.
1165 crypto_kdone(struct cryptkop *krp)
1167 struct cryptocap *cap;
1169 if (krp->krp_status != 0)
1170 cryptostats.cs_kerrs++;
1171 CRYPTO_DRIVER_LOCK();
1172 /* XXX: What if driver is loaded in the meantime? */
1173 if (krp->krp_hid < crypto_drivers_num) {
1174 cap = &crypto_drivers[krp->krp_hid];
1175 cap->cc_koperations--;
1176 KASSERT(cap->cc_koperations >= 0, ("cc_koperations < 0"));
1177 if (cap->cc_flags & CRYPTOCAP_F_CLEANUP)
1180 CRYPTO_DRIVER_UNLOCK();
1182 if (CRYPTO_RETQ_EMPTY())
1183 wakeup_one(&crp_ret_q); /* shared wait channel */
1184 TAILQ_INSERT_TAIL(&crp_ret_kq, krp, krp_next);
1185 CRYPTO_RETQ_UNLOCK();
1189 crypto_getfeat(int *featp)
1191 int hid, kalg, feat = 0;
1193 CRYPTO_DRIVER_LOCK();
1194 for (hid = 0; hid < crypto_drivers_num; hid++) {
1195 const struct cryptocap *cap = &crypto_drivers[hid];
1197 if ((cap->cc_flags & CRYPTOCAP_F_SOFTWARE) &&
1198 !crypto_devallowsoft) {
1201 for (kalg = 0; kalg < CRK_ALGORITHM_MAX; kalg++)
1202 if (cap->cc_kalg[kalg] & CRYPTO_ALG_FLAG_SUPPORTED)
1205 CRYPTO_DRIVER_UNLOCK();
1211 * Terminate a thread at module unload. The process that
1212 * initiated this is waiting for us to signal that we're gone;
1213 * wake it up and exit. We use the driver table lock to insure
1214 * we don't do the wakeup before they're waiting. There is no
1215 * race here because the waiter sleeps on the proc lock for the
1216 * thread so it gets notified at the right time because of an
1217 * extra wakeup that's done in exit1().
1220 crypto_finis(void *chan)
1222 CRYPTO_DRIVER_LOCK();
1224 CRYPTO_DRIVER_UNLOCK();
1229 * Crypto thread, dispatches crypto requests.
1234 struct cryptop *crp, *submit;
1235 struct cryptkop *krp;
1236 struct cryptocap *cap;
1243 * Find the first element in the queue that can be
1244 * processed and look-ahead to see if multiple ops
1245 * are ready for the same driver.
1249 TAILQ_FOREACH(crp, &crp_q, crp_next) {
1250 hid = CRYPTO_SESID2HID(crp->crp_sid);
1251 cap = crypto_checkdriver(hid);
1253 * Driver cannot disappeared when there is an active
1256 KASSERT(cap != NULL, ("%s:%u Driver disappeared.",
1257 __func__, __LINE__));
1258 if (cap == NULL || cap->cc_dev == NULL) {
1259 /* Op needs to be migrated, process it. */
1264 if (!cap->cc_qblocked) {
1265 if (submit != NULL) {
1267 * We stop on finding another op,
1268 * regardless whether its for the same
1269 * driver or not. We could keep
1270 * searching the queue but it might be
1271 * better to just use a per-driver
1274 if (CRYPTO_SESID2HID(submit->crp_sid) == hid)
1275 hint = CRYPTO_HINT_MORE;
1279 if ((submit->crp_flags & CRYPTO_F_BATCH) == 0)
1281 /* keep scanning for more are q'd */
1285 if (submit != NULL) {
1286 TAILQ_REMOVE(&crp_q, submit, crp_next);
1287 hid = CRYPTO_SESID2HID(submit->crp_sid);
1288 cap = crypto_checkdriver(hid);
1289 KASSERT(cap != NULL, ("%s:%u Driver disappeared.",
1290 __func__, __LINE__));
1291 result = crypto_invoke(cap, submit, hint);
1292 if (result == ERESTART) {
1294 * The driver ran out of resources, mark the
1295 * driver ``blocked'' for cryptop's and put
1296 * the request back in the queue. It would
1297 * best to put the request back where we got
1298 * it but that's hard so for now we put it
1299 * at the front. This should be ok; putting
1300 * it at the end does not work.
1302 /* XXX validate sid again? */
1303 crypto_drivers[CRYPTO_SESID2HID(submit->crp_sid)].cc_qblocked = 1;
1304 TAILQ_INSERT_HEAD(&crp_q, submit, crp_next);
1305 cryptostats.cs_blocks++;
1309 /* As above, but for key ops */
1310 TAILQ_FOREACH(krp, &crp_kq, krp_next) {
1311 cap = crypto_checkdriver(krp->krp_hid);
1312 if (cap == NULL || cap->cc_dev == NULL) {
1314 * Operation needs to be migrated, invalidate
1315 * the assigned device so it will reselect a
1316 * new one below. Propagate the original
1317 * crid selection flags if supplied.
1319 krp->krp_hid = krp->krp_crid &
1320 (CRYPTOCAP_F_SOFTWARE|CRYPTOCAP_F_HARDWARE);
1321 if (krp->krp_hid == 0)
1323 CRYPTOCAP_F_SOFTWARE|CRYPTOCAP_F_HARDWARE;
1326 if (!cap->cc_kqblocked)
1330 TAILQ_REMOVE(&crp_kq, krp, krp_next);
1331 result = crypto_kinvoke(krp, krp->krp_hid);
1332 if (result == ERESTART) {
1334 * The driver ran out of resources, mark the
1335 * driver ``blocked'' for cryptkop's and put
1336 * the request back in the queue. It would
1337 * best to put the request back where we got
1338 * it but that's hard so for now we put it
1339 * at the front. This should be ok; putting
1340 * it at the end does not work.
1342 /* XXX validate sid again? */
1343 crypto_drivers[krp->krp_hid].cc_kqblocked = 1;
1344 TAILQ_INSERT_HEAD(&crp_kq, krp, krp_next);
1345 cryptostats.cs_kblocks++;
1349 if (submit == NULL && krp == NULL) {
1351 * Nothing more to be processed. Sleep until we're
1352 * woken because there are more ops to process.
1353 * This happens either by submission or by a driver
1354 * becoming unblocked and notifying us through
1355 * crypto_unblock. Note that when we wakeup we
1356 * start processing each queue again from the
1357 * front. It's not clear that it's important to
1358 * preserve this ordering since ops may finish
1359 * out of order if dispatched to different devices
1360 * and some become blocked while others do not.
1363 msleep(&crp_q, &crypto_q_mtx, PWAIT, "crypto_wait", 0);
1365 if (cryptoproc == NULL)
1367 cryptostats.cs_intrs++;
1372 crypto_finis(&crp_q);
1376 * Crypto returns thread, does callbacks for processed crypto requests.
1377 * Callbacks are done here, rather than in the crypto drivers, because
1378 * callbacks typically are expensive and would slow interrupt handling.
1381 crypto_ret_proc(void)
1383 struct cryptop *crpt;
1384 struct cryptkop *krpt;
1388 /* Harvest return q's for completed ops */
1389 crpt = TAILQ_FIRST(&crp_ret_q);
1391 TAILQ_REMOVE(&crp_ret_q, crpt, crp_next);
1393 krpt = TAILQ_FIRST(&crp_ret_kq);
1395 TAILQ_REMOVE(&crp_ret_kq, krpt, krp_next);
1397 if (crpt != NULL || krpt != NULL) {
1398 CRYPTO_RETQ_UNLOCK();
1400 * Run callbacks unlocked.
1403 #ifdef CRYPTO_TIMING
1404 if (crypto_timing) {
1406 * NB: We must copy the timestamp before
1407 * doing the callback as the cryptop is
1408 * likely to be reclaimed.
1410 struct bintime t = crpt->crp_tstamp;
1411 crypto_tstat(&cryptostats.cs_cb, &t);
1412 crpt->crp_callback(crpt);
1413 crypto_tstat(&cryptostats.cs_finis, &t);
1416 crpt->crp_callback(crpt);
1419 krpt->krp_callback(krpt);
1423 * Nothing more to be processed. Sleep until we're
1424 * woken because there are more returns to process.
1426 msleep(&crp_ret_q, &crypto_ret_q_mtx, PWAIT,
1427 "crypto_ret_wait", 0);
1428 if (cryptoretproc == NULL)
1430 cryptostats.cs_rets++;
1433 CRYPTO_RETQ_UNLOCK();
1435 crypto_finis(&crp_ret_q);
1440 db_show_drivers(void)
1444 db_printf("%12s %4s %4s %8s %2s %2s\n"
1452 for (hid = 0; hid < crypto_drivers_num; hid++) {
1453 const struct cryptocap *cap = &crypto_drivers[hid];
1454 if (cap->cc_dev == NULL)
1456 db_printf("%-12s %4u %4u %08x %2u %2u\n"
1457 , device_get_nameunit(cap->cc_dev)
1459 , cap->cc_koperations
1467 DB_SHOW_COMMAND(crypto, db_show_crypto)
1469 struct cryptop *crp;
1474 db_printf("%4s %8s %4s %4s %4s %4s %8s %8s\n",
1475 "HID", "Caps", "Ilen", "Olen", "Etype", "Flags",
1476 "Desc", "Callback");
1477 TAILQ_FOREACH(crp, &crp_q, crp_next) {
1478 db_printf("%4u %08x %4u %4u %4u %04x %8p %8p\n"
1479 , (int) CRYPTO_SESID2HID(crp->crp_sid)
1480 , (int) CRYPTO_SESID2CAPS(crp->crp_sid)
1481 , crp->crp_ilen, crp->crp_olen
1488 if (!TAILQ_EMPTY(&crp_ret_q)) {
1489 db_printf("\n%4s %4s %4s %8s\n",
1490 "HID", "Etype", "Flags", "Callback");
1491 TAILQ_FOREACH(crp, &crp_ret_q, crp_next) {
1492 db_printf("%4u %4u %04x %8p\n"
1493 , (int) CRYPTO_SESID2HID(crp->crp_sid)
1502 DB_SHOW_COMMAND(kcrypto, db_show_kcrypto)
1504 struct cryptkop *krp;
1509 db_printf("%4s %5s %4s %4s %8s %4s %8s\n",
1510 "Op", "Status", "#IP", "#OP", "CRID", "HID", "Callback");
1511 TAILQ_FOREACH(krp, &crp_kq, krp_next) {
1512 db_printf("%4u %5u %4u %4u %08x %4u %8p\n"
1515 , krp->krp_iparams, krp->krp_oparams
1516 , krp->krp_crid, krp->krp_hid
1520 if (!TAILQ_EMPTY(&crp_ret_q)) {
1521 db_printf("%4s %5s %8s %4s %8s\n",
1522 "Op", "Status", "CRID", "HID", "Callback");
1523 TAILQ_FOREACH(krp, &crp_ret_kq, krp_next) {
1524 db_printf("%4u %5u %08x %4u %8p\n"
1527 , krp->krp_crid, krp->krp_hid
1535 int crypto_modevent(module_t mod, int type, void *unused);
1538 * Initialization code, both for static and dynamic loading.
1539 * Note this is not invoked with the usual MODULE_DECLARE
1540 * mechanism but instead is listed as a dependency by the
1541 * cryptosoft driver. This guarantees proper ordering of
1542 * calls on module load/unload.
1545 crypto_modevent(module_t mod, int type, void *unused)
1551 error = crypto_init();
1552 if (error == 0 && bootverbose)
1553 printf("crypto: <crypto core>\n");
1556 /*XXX disallow if active sessions */
1563 MODULE_VERSION(crypto, 1);
1564 MODULE_DEPEND(crypto, zlib, 1, 1, 1);