[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]

FreeBSD/Linux Kernel Cross Reference
sys/opencrypto/crypto.c

Version: -  FREEBSD  -  FREEBSD7  -  FREEBSD70  -  FREEBSD6  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  OPENSOLARIS  -  minix-3-1-1  -  TRUSTEDBSD-SEBSD  -  FREEBSD-LIBC  -  FREEBSD7-LIBC  -  FREEBSD6-LIBC  -  GLIBC27 
SearchContext: -  none  -  excerpts  -  bigexcerpts 

  1 /*-
  2  * Copyright (c) 2002-2006 Sam Leffler.  All rights reserved.
  3  *
  4  * Redistribution and use in source and binary forms, with or without
  5  * modification, are permitted provided that the following conditions
  6  * are met:
  7  * 1. Redistributions of source code must retain the above copyright
  8  *    notice, this list of conditions and the following disclaimer.
  9  * 2. Redistributions in binary form must reproduce the above copyright
 10  *    notice, this list of conditions and the following disclaimer in the
 11  *    documentation and/or other materials provided with the distribution.
 12  *
 13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 19  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 20  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 22  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 23  */
 24 
 25 #include <sys/cdefs.h>
 26 __FBSDID("$FreeBSD: src/sys/opencrypto/crypto.c,v 1.28 2007/10/20 23:23:22 julian Exp $");
 27 
 28 /*
 29  * Cryptographic Subsystem.
 30  *
 31  * This code is derived from the Openbsd Cryptographic Framework (OCF)
 32  * that has the copyright shown below.  Very little of the original
 33  * code remains.
 34  */
 35 
 36 /*-
 37  * The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu)
 38  *
 39  * This code was written by Angelos D. Keromytis in Athens, Greece, in
 40  * February 2000. Network Security Technologies Inc. (NSTI) kindly
 41  * supported the development of this code.
 42  *
 43  * Copyright (c) 2000, 2001 Angelos D. Keromytis
 44  *
 45  * Permission to use, copy, and modify this software with or without fee
 46  * is hereby granted, provided that this entire notice is included in
 47  * all source code copies of any software which is or includes a copy or
 48  * modification of this software.
 49  *
 50  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
 51  * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
 52  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
 53  * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
 54  * PURPOSE.
 55  */
 56 
 57 #define CRYPTO_TIMING                           /* enable timing support */
 58 
 59 #include "opt_ddb.h"
 60 
 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>
 66 #include <sys/lock.h>
 67 #include <sys/module.h>
 68 #include <sys/mutex.h>
 69 #include <sys/malloc.h>
 70 #include <sys/proc.h>
 71 #include <sys/sysctl.h>
 72 
 73 #include <ddb/ddb.h>
 74 
 75 #include <vm/uma.h>
 76 #include <opencrypto/cryptodev.h>
 77 #include <opencrypto/xform.h>                   /* XXX for M_XDATA */
 78 
 79 #include <sys/kobj.h>
 80 #include <sys/bus.h>
 81 #include "cryptodev_if.h"
 82 
 83 /*
 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().
 87  */
 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)
 92 
 93 /*
 94  * Crypto device/driver capabilities structure.
 95  *
 96  * Synchronization:
 97  * (d) - protected by CRYPTO_DRIVER_LOCK()
 98  * (q) - protected by CRYPTO_Q_LOCK()
 99  * Not tagged fields are read-only.
100  */
101 struct cryptocap {
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 */
105         /*
106          * Largest possible operator length (in bits) for each type of
107          * encryption algorithm. XXX not used
108          */
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];
112 
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 */
117 };
118 static  struct cryptocap *crypto_drivers = NULL;
119 static  int crypto_drivers_num = 0;
120 
121 /*
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
126  * operations.
127  */
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)
134 
135 /*
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.
142  */
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))
149 
150 static  uma_zone_t cryptop_zone;
151 static  uma_zone_t cryptodesc_zone;
152 
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");
161 
162 MALLOC_DEFINE(M_CRYPTO_DATA, "crypto", "crypto session records");
163 
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);
171 
172 static  struct cryptostats cryptostats;
173 SYSCTL_STRUCT(_kern, OID_AUTO, crypto_stats, CTLFLAG_RW, &cryptostats,
174             cryptostats, "Crypto system statistics");
175 
176 #ifdef CRYPTO_TIMING
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");
180 #endif
181 
182 static int
183 crypto_init(void)
184 {
185         int error;
186 
187         mtx_init(&crypto_drivers_mtx, "crypto", "crypto driver table",
188                 MTX_DEF|MTX_QUIET);
189 
190         TAILQ_INIT(&crp_q);
191         TAILQ_INIT(&crp_kq);
192         mtx_init(&crypto_q_mtx, "crypto", "crypto op queues", MTX_DEF);
193 
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);
197 
198         cryptop_zone = uma_zcreate("cryptop", sizeof (struct cryptop),
199                                     0, 0, 0, 0,
200                                     UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
201         cryptodesc_zone = uma_zcreate("cryptodesc", sizeof (struct cryptodesc),
202                                     0, 0, 0, 0,
203                                     UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
204         if (cryptodesc_zone == NULL || cryptop_zone == NULL) {
205                 printf("crypto_init: cannot setup crypto zones\n");
206                 error = ENOMEM;
207                 goto bad;
208         }
209 
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");
215                 error = ENOMEM;
216                 goto bad;
217         }
218 
219         error = kproc_create((void (*)(void *)) crypto_proc, NULL,
220                     &cryptoproc, 0, 0, "crypto");
221         if (error) {
222                 printf("crypto_init: cannot start crypto thread; error %d",
223                         error);
224                 goto bad;
225         }
226 
227         error = kproc_create((void (*)(void *)) crypto_ret_proc, NULL,
228                     &cryptoretproc, 0, 0, "crypto returns");
229         if (error) {
230                 printf("crypto_init: cannot start cryptoret thread; error %d",
231                         error);
232                 goto bad;
233         }
234         return 0;
235 bad:
236         crypto_destroy();
237         return error;
238 }
239 
240 /*
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.
246  */
247 static void
248 crypto_terminate(struct proc **pp, void *q)
249 {
250         struct proc *p;
251 
252         mtx_assert(&crypto_drivers_mtx, MA_OWNED);
253         p = *pp;
254         *pp = NULL;
255         if (p) {
256                 wakeup_one(q);
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);
260                 PROC_UNLOCK(p);
261                 CRYPTO_DRIVER_LOCK();
262         }
263 }
264 
265 static void
266 crypto_destroy(void)
267 {
268         /*
269          * Terminate any crypto threads.
270          */
271         CRYPTO_DRIVER_LOCK();
272         crypto_terminate(&cryptoproc, &crp_q);
273         crypto_terminate(&cryptoretproc, &crp_ret_q);
274         CRYPTO_DRIVER_UNLOCK();
275 
276         /* XXX flush queues??? */
277 
278         /* 
279          * Reclaim dynamically allocated resources.
280          */
281         if (crypto_drivers != NULL)
282                 free(crypto_drivers, M_CRYPTO_DATA);
283 
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);
291 }
292 
293 static struct cryptocap *
294 crypto_checkdriver(u_int32_t hid)
295 {
296         if (crypto_drivers == NULL)
297                 return NULL;
298         return (hid >= crypto_drivers_num ? NULL : &crypto_drivers[hid]);
299 }
300 
301 /*
302  * Compare a driver's list of supported algorithms against another
303  * list; return non-zero if all algorithms are supported.
304  */
305 static int
306 driver_suitable(const struct cryptocap *cap, const struct cryptoini *cri)
307 {
308         const struct cryptoini *cr;
309 
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)
313                         return 0;
314         return 1;
315 }
316 
317 /*
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.
324  *
325  * XXX We need more smarts here (in real life too, but that's
326  * XXX another story altogether).
327  */
328 static struct cryptocap *
329 crypto_select_driver(const struct cryptoini *cri, int flags)
330 {
331         struct cryptocap *cap, *best;
332         int match, hid;
333 
334         CRYPTO_DRIVER_ASSERT();
335 
336         /*
337          * Look first for hardware crypto devices if permitted.
338          */
339         if (flags & CRYPTOCAP_F_HARDWARE)
340                 match = CRYPTOCAP_F_HARDWARE;
341         else
342                 match = CRYPTOCAP_F_SOFTWARE;
343         best = NULL;
344 again:
345         for (hid = 0; hid < crypto_drivers_num; hid++) {
346                 cap = &crypto_drivers[hid];
347                 /*
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.
351                  */
352                 if (cap->cc_dev == NULL ||
353                     (cap->cc_flags & CRYPTOCAP_F_CLEANUP) ||
354                     (cap->cc_flags & match) == 0)
355                         continue;
356 
357                 /* verify all the algorithms are supported. */
358                 if (driver_suitable(cap, cri)) {
359                         if (best == NULL ||
360                             cap->cc_sessions < best->cc_sessions)
361                                 best = cap;
362                 }
363         }
364         if (best != NULL)
365                 return best;
366         if (match == CRYPTOCAP_F_HARDWARE && (flags & CRYPTOCAP_F_SOFTWARE)) {
367                 /* sort of an Algol 68-style for loop */
368                 match = CRYPTOCAP_F_SOFTWARE;
369                 goto again;
370         }
371         return best;
372 }
373 
374 /*
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.
379  */
380 int
381 crypto_newsession(u_int64_t *sid, struct cryptoini *cri, int crid)
382 {
383         struct cryptocap *cap;
384         u_int32_t hid, lid;
385         int err;
386 
387         CRYPTO_DRIVER_LOCK();
388         if ((crid & (CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE)) == 0) {
389                 /*
390                  * Use specified driver; verify it is capable.
391                  */
392                 cap = crypto_checkdriver(crid);
393                 if (cap != NULL && !driver_suitable(cap, cri))
394                         cap = NULL;
395         } else {
396                 /*
397                  * No requested driver; select based on crid flags.
398                  */
399                 cap = crypto_select_driver(cri, crid);
400                 /*
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.
404                  */
405         }
406         if (cap != NULL) {
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);
411                 if (err == 0) {
412                         (*sid) = (cap->cc_flags & 0xff000000)
413                                | (hid & 0x00ffffff);
414                         (*sid) <<= 32;
415                         (*sid) |= (lid & 0xffffffff);
416                         cap->cc_sessions++;
417                 }
418         } else
419                 err = EINVAL;
420         CRYPTO_DRIVER_UNLOCK();
421         return err;
422 }
423 
424 static void
425 crypto_remove(struct cryptocap *cap)
426 {
427 
428         mtx_assert(&crypto_drivers_mtx, MA_OWNED);
429         if (cap->cc_sessions == 0 && cap->cc_koperations == 0)
430                 bzero(cap, sizeof(*cap));
431 }
432 
433 /*
434  * Delete an existing session (or a reserved session on an unregistered
435  * driver).
436  */
437 int
438 crypto_freesession(u_int64_t sid)
439 {
440         struct cryptocap *cap;
441         u_int32_t hid;
442         int err;
443 
444         CRYPTO_DRIVER_LOCK();
445 
446         if (crypto_drivers == NULL) {
447                 err = EINVAL;
448                 goto done;
449         }
450 
451         /* Determine two IDs. */
452         hid = CRYPTO_SESID2HID(sid);
453 
454         if (hid >= crypto_drivers_num) {
455                 err = ENOENT;
456                 goto done;
457         }
458         cap = &crypto_drivers[hid];
459 
460         if (cap->cc_sessions)
461                 cap->cc_sessions--;
462 
463         /* Call the driver cleanup routine, if available. */
464         err = CRYPTODEV_FREESESSION(cap->cc_dev, sid);
465 
466         if (cap->cc_flags & CRYPTOCAP_F_CLEANUP)
467                 crypto_remove(cap);
468 
469 done:
470         CRYPTO_DRIVER_UNLOCK();
471         return err;
472 }
473 
474 /*
475  * Return an unused driver id.  Used by drivers prior to registering
476  * support for the algorithms they handle.
477  */
478 int32_t
479 crypto_get_driverid(device_t dev, int flags)
480 {
481         struct cryptocap *newdrv;
482         int i;
483 
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));
487                 return -1;
488         }
489 
490         CRYPTO_DRIVER_LOCK();
491 
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) {
495                         break;
496                 }
497         }
498 
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");
505                         return -1;
506                 }
507 
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");
513                         return -1;
514                 }
515 
516                 bcopy(crypto_drivers, newdrv,
517                     crypto_drivers_num * sizeof(struct cryptocap));
518 
519                 crypto_drivers_num *= 2;
520 
521                 free(crypto_drivers, M_CRYPTO_DATA);
522                 crypto_drivers = newdrv;
523         }
524 
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;
529         if (bootverbose)
530                 printf("crypto: assign %s driver id %u, flags %u\n",
531                     device_get_nameunit(dev), i, flags);
532 
533         CRYPTO_DRIVER_UNLOCK();
534 
535         return i;
536 }
537 
538 /*
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.
543  */
544 int
545 crypto_find_driver(const char *match)
546 {
547         int i, len = strlen(match);
548 
549         CRYPTO_DRIVER_LOCK();
550         for (i = 0; i < crypto_drivers_num; i++) {
551                 device_t dev = crypto_drivers[i].cc_dev;
552                 if (dev == NULL ||
553                     (crypto_drivers[i].cc_flags & CRYPTOCAP_F_CLEANUP))
554                         continue;
555                 if (strncmp(match, device_get_nameunit(dev), len) == 0 ||
556                     strncmp(match, device_get_name(dev), len) == 0)
557                         break;
558         }
559         CRYPTO_DRIVER_UNLOCK();
560         return i < crypto_drivers_num ? i : -1;
561 }
562 
563 /*
564  * Return the device_t for the specified driver or NULL
565  * if the driver identifier is invalid.
566  */
567 device_t
568 crypto_find_device_byhid(int hid)
569 {
570         struct cryptocap *cap = crypto_checkdriver(hid);
571         return cap != NULL ? cap->cc_dev : NULL;
572 }
573 
574 /*
575  * Return the device/driver capabilities.
576  */
577 int
578 crypto_getcaps(int hid)
579 {
580         struct cryptocap *cap = crypto_checkdriver(hid);
581         return cap != NULL ? cap->cc_flags : 0;
582 }
583 
584 /*
585  * Register support for a key-related algorithm.  This routine
586  * is called once for each algorithm supported a driver.
587  */
588 int
589 crypto_kregister(u_int32_t driverid, int kalg, u_int32_t flags)
590 {
591         struct cryptocap *cap;
592         int err;
593 
594         CRYPTO_DRIVER_LOCK();
595 
596         cap = crypto_checkdriver(driverid);
597         if (cap != NULL &&
598             (CRK_ALGORITM_MIN <= kalg && kalg <= CRK_ALGORITHM_MAX)) {
599                 /*
600                  * XXX Do some performance testing to determine placing.
601                  * XXX We probably need an auxiliary data structure that
602                  * XXX describes relative performances.
603                  */
604 
605                 cap->cc_kalg[kalg] = flags | CRYPTO_ALG_FLAG_SUPPORTED;
606                 if (bootverbose)
607                         printf("crypto: %s registers key alg %u flags %u\n"
608                                 , device_get_nameunit(cap->cc_dev)
609                                 , kalg
610                                 , flags
611                         );
612                 err = 0;
613         } else
614                 err = EINVAL;
615 
616         CRYPTO_DRIVER_UNLOCK();
617         return err;
618 }
619 
620 /*
621  * Register support for a non-key-related algorithm.  This routine
622  * is called once for each such algorithm supported by a driver.
623  */
624 int
625 crypto_register(u_int32_t driverid, int alg, u_int16_t maxoplen,
626     u_int32_t flags)
627 {
628         struct cryptocap *cap;
629         int err;
630 
631         CRYPTO_DRIVER_LOCK();
632 
633         cap = crypto_checkdriver(driverid);
634         /* NB: algorithms are in the range [1..max] */
635         if (cap != NULL &&
636             (CRYPTO_ALGORITHM_MIN <= alg && alg <= CRYPTO_ALGORITHM_MAX)) {
637                 /*
638                  * XXX Do some performance testing to determine placing.
639                  * XXX We probably need an auxiliary data structure that
640                  * XXX describes relative performances.
641                  */
642 
643                 cap->cc_alg[alg] = flags | CRYPTO_ALG_FLAG_SUPPORTED;
644                 cap->cc_max_op_len[alg] = maxoplen;
645                 if (bootverbose)
646                         printf("crypto: %s registers alg %u flags %u maxoplen %u\n"
647                                 , device_get_nameunit(cap->cc_dev)
648                                 , alg
649                                 , flags
650                                 , maxoplen
651                         );
652                 cap->cc_sessions = 0;           /* Unmark */
653                 err = 0;
654         } else
655                 err = EINVAL;
656 
657         CRYPTO_DRIVER_UNLOCK();
658         return err;
659 }
660 
661 static void
662 driver_finis(struct cryptocap *cap)
663 {
664         u_int32_t ses, kops;
665 
666         CRYPTO_DRIVER_ASSERT();
667 
668         ses = cap->cc_sessions;
669         kops = cap->cc_koperations;
670         bzero(cap, sizeof(*cap));
671         if (ses != 0 || kops != 0) {
672                 /*
673                  * If there are pending sessions,
674                  * just mark as invalid.
675                  */
676                 cap->cc_flags |= CRYPTOCAP_F_CLEANUP;
677                 cap->cc_sessions = ses;
678                 cap->cc_koperations = kops;
679         }
680 }
681 
682 /*
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
686  * reroute requests.
687  */
688 int
689 crypto_unregister(u_int32_t driverid, int alg)
690 {
691         struct cryptocap *cap;
692         int i, err;
693 
694         CRYPTO_DRIVER_LOCK();
695         cap = crypto_checkdriver(driverid);
696         if (cap != NULL &&
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;
701 
702                 /* Was this the last algorithm ? */
703                 for (i = 1; i <= CRYPTO_ALGORITHM_MAX; i++)
704                         if (cap->cc_alg[i] != 0)
705                                 break;
706 
707                 if (i == CRYPTO_ALGORITHM_MAX + 1)
708                         driver_finis(cap);
709                 err = 0;
710         } else
711                 err = EINVAL;
712         CRYPTO_DRIVER_UNLOCK();
713 
714         return err;
715 }
716 
717 /*
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
722  * requests.
723  */
724 int
725 crypto_unregister_all(u_int32_t driverid)
726 {
727         struct cryptocap *cap;
728         int err;
729 
730         CRYPTO_DRIVER_LOCK();
731         cap = crypto_checkdriver(driverid);
732         if (cap != NULL) {
733                 driver_finis(cap);
734                 err = 0;
735         } else
736                 err = EINVAL;
737         CRYPTO_DRIVER_UNLOCK();
738 
739         return err;
740 }
741 
742 /*
743  * Clear blockage on a driver.  The what parameter indicates whether
744  * the driver is now ready for cryptop's and/or cryptokop's.
745  */
746 int
747 crypto_unblock(u_int32_t driverid, int what)
748 {
749         struct cryptocap *cap;
750         int err;
751 
752         CRYPTO_Q_LOCK();
753         cap = crypto_checkdriver(driverid);
754         if (cap != NULL) {
755                 if (what & CRYPTO_SYMQ)
756                         cap->cc_qblocked = 0;
757                 if (what & CRYPTO_ASYMQ)
758                         cap->cc_kqblocked = 0;
759                 if (crp_sleep)
760                         wakeup_one(&crp_q);
761                 err = 0;
762         } else
763                 err = EINVAL;
764         CRYPTO_Q_UNLOCK();
765 
766         return err;
767 }
768 
769 /*
770  * Add a crypto request to a queue, to be processed by the kernel thread.
771  */
772 int
773 crypto_dispatch(struct cryptop *crp)
774 {
775         struct cryptocap *cap;
776         u_int32_t hid;
777         int result;
778 
779         cryptostats.cs_ops++;
780 
781 #ifdef CRYPTO_TIMING
782         if (crypto_timing)
783                 binuptime(&crp->crp_tstamp);
784 #endif
785 
786         hid = CRYPTO_SESID2HID(crp->crp_sid);
787 
788         if ((crp->crp_flags & CRYPTO_F_BATCH) == 0) {
789                 /*
790                  * Caller marked the request to be processed
791                  * immediately; dispatch it directly to the
792                  * driver unless the driver is currently blocked.
793                  */
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)
800                                 return (result);
801                         /*
802                          * The driver ran out of resources, put the request on
803                          * the queue.
804                          */
805                 }
806         }
807         CRYPTO_Q_LOCK();
808         TAILQ_INSERT_TAIL(&crp_q, crp, crp_next);
809         if (crp_sleep)
810                 wakeup_one(&crp_q);
811         CRYPTO_Q_UNLOCK();
812         return 0;
813 }
814 
815 /*
816  * Add an asymetric crypto request to a queue,
817  * to be processed by the kernel thread.
818  */
819 int
820 crypto_kdispatch(struct cryptkop *krp)
821 {
822         int error;
823 
824         cryptostats.cs_kops++;
825 
826         error = crypto_kinvoke(krp, krp->krp_crid);
827         if (error == ERESTART) {
828                 CRYPTO_Q_LOCK();
829                 TAILQ_INSERT_TAIL(&crp_kq, krp, krp_next);
830                 if (crp_sleep)
831                         wakeup_one(&crp_q);
832                 CRYPTO_Q_UNLOCK();
833                 error = 0;
834         }
835         return error;
836 }
837 
838 /*
839  * Verify a driver is suitable for the specified operation.
840  */
841 static __inline int
842 kdriver_suitable(const struct cryptocap *cap, const struct cryptkop *krp)
843 {
844         return (cap->cc_kalg[krp->krp_op] & CRYPTO_ALG_FLAG_SUPPORTED) != 0;
845 }
846 
847 /*
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.
856  */
857 static struct cryptocap *
858 crypto_select_kdriver(const struct cryptkop *krp, int flags)
859 {
860         struct cryptocap *cap, *best, *blocked;
861         int match, hid;
862 
863         CRYPTO_DRIVER_ASSERT();
864 
865         /*
866          * Look first for hardware crypto devices if permitted.
867          */
868         if (flags & CRYPTOCAP_F_HARDWARE)
869                 match = CRYPTOCAP_F_HARDWARE;
870         else
871                 match = CRYPTOCAP_F_SOFTWARE;
872         best = NULL;
873         blocked = NULL;
874 again:
875         for (hid = 0; hid < crypto_drivers_num; hid++) {
876                 cap = &crypto_drivers[hid];
877                 /*
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.
881                  */
882                 if (cap->cc_dev == NULL ||
883                     (cap->cc_flags & CRYPTOCAP_F_CLEANUP) ||
884                     (cap->cc_flags & match) == 0)
885                         continue;
886 
887                 /* verify all the algorithms are supported. */
888                 if (kdriver_suitable(cap, krp)) {
889                         if (best == NULL ||
890                             cap->cc_koperations < best->cc_koperations)
891                                 best = cap;
892                 }
893         }
894         if (best != NULL)
895                 return best;
896         if (match == CRYPTOCAP_F_HARDWARE && (flags & CRYPTOCAP_F_SOFTWARE)) {
897                 /* sort of an Algol 68-style for loop */
898                 match = CRYPTOCAP_F_SOFTWARE;
899                 goto again;
900         }
901         return best;
902 }
903 
904 /*
905  * Dispatch an assymetric crypto request.
906  */
907 static int
908 crypto_kinvoke(struct cryptkop *krp, int crid)
909 {
910         struct cryptocap *cap = NULL;
911         int error;
912 
913         KASSERT(krp != NULL, ("%s: krp == NULL", __func__));
914         KASSERT(krp->krp_callback != NULL,
915             ("%s: krp->crp_callback == NULL", __func__));
916 
917         CRYPTO_DRIVER_LOCK();
918         if ((crid & (CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE)) == 0) {
919                 cap = crypto_checkdriver(crid);
920                 if (cap != NULL) {
921                         /*
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.
925                          */
926                         if (!kdriver_suitable(cap, krp) ||
927                             (!crypto_devallowsoft &&
928                              (cap->cc_flags & CRYPTOCAP_F_HARDWARE) == 0))
929                                 cap = NULL;
930                 }
931         } else {
932                 /*
933                  * No requested driver; select based on crid flags.
934                  */
935                 if (!crypto_devallowsoft)       /* NB: disallow s/w drivers */
936                         crid &= ~CRYPTOCAP_F_SOFTWARE;
937                 cap = crypto_select_kdriver(krp, crid);
938         }
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();
948                         return (error);
949                 }
950         } else {
951                 /*
952                  * NB: cap is !NULL if device is blocked; in
953                  *     that case return ERESTART so the operation
954                  *     is resubmitted if possible.
955                  */
956                 error = (cap == NULL) ? ENODEV : ERESTART;
957         }
958         CRYPTO_DRIVER_UNLOCK();
959 
960         if (error) {
961                 krp->krp_status = error;
962                 crypto_kdone(krp);
963         }
964         return 0;
965 }
966 
967 #ifdef CRYPTO_TIMING
968 static void
969 crypto_tstat(struct cryptotstat *ts, struct bintime *bt)
970 {
971         struct bintime now, delta;
972         struct timespec t;
973         uint64_t u;
974 
975         binuptime(&now);
976         u = now.frac;
977         delta.frac = now.frac - bt->frac;
978         delta.sec = now.sec - bt->sec;
979         if (u < delta.frac)
980                 delta.sec--;
981         bintime2timespec(&delta, &t);
982         timespecadd(&ts->acc, &t);
983         if (timespeccmp(&t, &ts->min, <))
984                 ts->min = t;
985         if (timespeccmp(&t, &ts->max, >))
986                 ts->max = t;
987         ts->count++;
988 
989         *bt = now;
990 }
991 #endif
992 
993 /*
994  * Dispatch a crypto request to the appropriate crypto devices.
995  */
996 static int
997 crypto_invoke(struct cryptocap *cap, struct cryptop *crp, int hint)
998 {
999 
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__));
1004 
1005 #ifdef CRYPTO_TIMING
1006         if (crypto_timing)
1007                 crypto_tstat(&cryptostats.cs_invoke, &crp->crp_tstamp);
1008 #endif
1009         if (cap->cc_flags & CRYPTOCAP_F_CLEANUP) {
1010                 struct cryptodesc *crd;
1011                 u_int64_t nid;
1012 
1013                 /*
1014                  * Driver has unregistered; migrate the session and return
1015                  * an error to the caller so they'll resubmit the op.
1016                  *
1017                  * XXX: What if there are more already queued requests for this
1018                  *      session?
1019                  */
1020                 crypto_freesession(crp->crp_sid);
1021 
1022                 for (crd = crp->crp_desc; crd->crd_next; crd = crd->crd_next)
1023                         crd->CRD_INI.cri_next = &(crd->crd_next->CRD_INI);
1024 
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)
1028                         crp->crp_sid = nid;
1029 
1030                 crp->crp_etype = EAGAIN;
1031                 crypto_done(crp);
1032                 return 0;
1033         } else {
1034                 /*
1035                  * Invoke the driver to process the request.
1036                  */
1037                 return CRYPTODEV_PROCESS(cap->cc_dev, crp, hint);
1038         }
1039 }
1040 
1041 /*
1042  * Release a set of crypto