-/* $OpenBSD: crypto.c,v 1.82 2020/03/30 17:48:39 krw Exp $ */
+/* $OpenBSD: crypto.c,v 1.83 2021/06/30 12:21:02 bluhm Exp $ */
/*
* The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu)
*
#include <crypto/cryptodev.h>
-void crypto_init(void);
+/*
+ * Locks used to protect struct members in this file:
+ * A allocated during driver attach, no hotplug, no detach
+ * I immutable after creation
+ * K kernel lock
+ */
-struct cryptocap *crypto_drivers = NULL;
-int crypto_drivers_num = 0;
+struct cryptocap *crypto_drivers; /* [A] array allocated by driver
+ [K] driver data and session count */
+int crypto_drivers_num = 0; /* [A] attached drivers array size */
-struct pool cryptop_pool;
-struct pool cryptodesc_pool;
+struct pool cryptop_pool; /* [I] set of crypto descriptors */
-struct taskq *crypto_taskq;
-struct taskq *crypto_taskq_mpsafe;
+struct taskq *crypto_taskq; /* [I] run crypto_invoke() and callback
+ with kernel lock */
+struct taskq *crypto_taskq_mpsafe; /* [I] run crypto_invoke()
+ without kernel lock */
/*
* Create a new session.
if (crypto_drivers == NULL)
return EINVAL;
+ KERNEL_ASSERT_LOCKED();
+
s = splvm();
/*
if (hid >= crypto_drivers_num)
return ENOENT;
+ KERNEL_ASSERT_LOCKED();
+
s = splvm();
if (crypto_drivers[hid].cc_sessions)
{
struct cryptocap *newdrv;
int i, s;
+
+ /* called from attach routines */
+ KERNEL_ASSERT_LOCKED();
s = splvm();
}
/* Out of entries, allocate some more. */
- if (i == crypto_drivers_num) {
- if (crypto_drivers_num >= CRYPTO_DRIVERS_MAX) {
- splx(s);
- return -1;
- }
-
- newdrv = mallocarray(crypto_drivers_num,
- 2 * sizeof(struct cryptocap), M_CRYPTO_DATA, M_NOWAIT);
- if (newdrv == NULL) {
- splx(s);
- return -1;
- }
+ if (crypto_drivers_num >= CRYPTO_DRIVERS_MAX) {
+ splx(s);
+ return -1;
+ }
- memcpy(newdrv, crypto_drivers,
- crypto_drivers_num * sizeof(struct cryptocap));
- bzero(&newdrv[crypto_drivers_num],
- crypto_drivers_num * sizeof(struct cryptocap));
+ newdrv = mallocarray(crypto_drivers_num,
+ 2 * sizeof(struct cryptocap), M_CRYPTO_DATA, M_NOWAIT);
+ if (newdrv == NULL) {
+ splx(s);
+ return -1;
+ }
- newdrv[i].cc_sessions = 1; /* Mark */
- newdrv[i].cc_flags = flags;
+ memcpy(newdrv, crypto_drivers,
+ crypto_drivers_num * sizeof(struct cryptocap));
+ bzero(&newdrv[crypto_drivers_num],
+ crypto_drivers_num * sizeof(struct cryptocap));
- free(crypto_drivers, M_CRYPTO_DATA,
- crypto_drivers_num * sizeof(struct cryptocap));
+ newdrv[i].cc_sessions = 1; /* Mark */
+ newdrv[i].cc_flags = flags;
- crypto_drivers_num *= 2;
- crypto_drivers = newdrv;
- splx(s);
- return i;
- }
+ free(crypto_drivers, M_CRYPTO_DATA,
+ crypto_drivers_num * sizeof(struct cryptocap));
- /* Shouldn't really get here... */
+ crypto_drivers_num *= 2;
+ crypto_drivers = newdrv;
splx(s);
- return -1;
+ return i;
}
/*
{
int s, i;
-
if (driverid >= crypto_drivers_num || alg == NULL ||
crypto_drivers == NULL)
return EINVAL;
+ /* called from attach routines */
+ KERNEL_ASSERT_LOCKED();
+
s = splvm();
for (i = 0; i <= CRYPTO_ALGORITHM_MAX; i++) {
int i = CRYPTO_ALGORITHM_MAX + 1, s;
u_int32_t ses;
+ /* may be called from detach routines, but not used */
+ KERNEL_ASSERT_LOCKED();
+
s = splvm();
/* Sanity checks. */