#include "internal/cryptlib.h"
#include "internal/provider.h"
#include "internal/thread_once.h"
+#include "internal/threads_common.h"
#include "crypto/rand.h"
#include "crypto/cryptlib.h"
#include "rand_local.h"
char *random_provider_name;
#endif /* !FIPS_MODULE */
- /*
- * The <public> DRBG
- *
- * Used by default for generating random bytes using RAND_bytes().
- *
- * The <public> secondary DRBG is thread-local, i.e., there is one instance
- * per thread.
- */
- CRYPTO_THREAD_LOCAL public;
-
- /*
- * The <private> DRBG
- *
- * Used by default for generating private keys using RAND_priv_bytes()
- *
- * The <private> secondary DRBG is thread-local, i.e., there is one
- * instance per thread.
- */
- CRYPTO_THREAD_LOCAL private;
-
/* Which RNG is being used by default and it's configuration settings */
char *rng_name;
char *rng_cipher;
if (dgbl->lock == NULL)
goto err1;
- if (!CRYPTO_THREAD_init_local(&dgbl->private, NULL))
- goto err1;
-
- if (!CRYPTO_THREAD_init_local(&dgbl->public, NULL))
- goto err2;
-
return dgbl;
- err2:
- CRYPTO_THREAD_cleanup_local(&dgbl->private);
err1:
CRYPTO_THREAD_lock_free(dgbl->lock);
#ifndef FIPS_MODULE
return;
CRYPTO_THREAD_lock_free(dgbl->lock);
- CRYPTO_THREAD_cleanup_local(&dgbl->private);
- CRYPTO_THREAD_cleanup_local(&dgbl->public);
EVP_RAND_CTX_free(dgbl->primary);
EVP_RAND_CTX_free(dgbl->seed);
#ifndef FIPS_MODULE
if (dgbl == NULL)
return;
- rand = CRYPTO_THREAD_get_local(&dgbl->public);
- CRYPTO_THREAD_set_local(&dgbl->public, NULL);
+ rand = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx);
+ CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx, NULL);
EVP_RAND_CTX_free(rand);
- rand = CRYPTO_THREAD_get_local(&dgbl->private);
- CRYPTO_THREAD_set_local(&dgbl->private, NULL);
+ rand = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx);
+ CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx, NULL);
EVP_RAND_CTX_free(rand);
}
static EVP_RAND_CTX *rand_get0_public(OSSL_LIB_CTX *ctx, RAND_GLOBAL *dgbl)
{
EVP_RAND_CTX *rand, *primary;
+ OSSL_LIB_CTX *origctx = ctx;
+
+ ctx = ossl_lib_ctx_get_concrete(ctx);
+
+ if (ctx == NULL)
+ return NULL;
if (dgbl == NULL)
return NULL;
- rand = CRYPTO_THREAD_get_local(&dgbl->public);
+ rand = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx);
if (rand == NULL) {
- primary = rand_get0_primary(ctx, dgbl);
+ primary = rand_get0_primary(origctx, dgbl);
if (primary == NULL)
return NULL;
- ctx = ossl_lib_ctx_get_concrete(ctx);
-
- if (ctx == NULL)
- return NULL;
/*
* If the private is also NULL then this is the first time we've
* used this thread.
*/
- if (CRYPTO_THREAD_get_local(&dgbl->private) == NULL
+ if (CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx) == NULL
&& !ossl_init_thread_start(NULL, ctx, rand_delete_thread_state))
return NULL;
rand = rand_new_drbg(ctx, primary, SECONDARY_RESEED_INTERVAL,
SECONDARY_RESEED_TIME_INTERVAL);
- CRYPTO_THREAD_set_local(&dgbl->public, rand);
+ CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx, rand);
}
return rand;
}
static EVP_RAND_CTX *rand_get0_private(OSSL_LIB_CTX *ctx, RAND_GLOBAL *dgbl)
{
EVP_RAND_CTX *rand, *primary;
+ OSSL_LIB_CTX *origctx = ctx;
+
+ ctx = ossl_lib_ctx_get_concrete(ctx);
+ if (ctx == NULL)
+ return NULL;
- rand = CRYPTO_THREAD_get_local(&dgbl->private);
+ rand = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx);
if (rand == NULL) {
- primary = rand_get0_primary(ctx, dgbl);
+ primary = rand_get0_primary(origctx, dgbl);
if (primary == NULL)
return NULL;
- ctx = ossl_lib_ctx_get_concrete(ctx);
-
- if (ctx == NULL)
- return NULL;
/*
* If the public is also NULL then this is the first time we've
* used this thread.
*/
- if (CRYPTO_THREAD_get_local(&dgbl->public) == NULL
+ if (CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx) == NULL
&& !ossl_init_thread_start(NULL, ctx, rand_delete_thread_state))
return NULL;
rand = rand_new_drbg(ctx, primary, SECONDARY_RESEED_INTERVAL,
SECONDARY_RESEED_TIME_INTERVAL);
- CRYPTO_THREAD_set_local(&dgbl->private, rand);
+ CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx, rand);
}
return rand;
}
if (dgbl == NULL)
return NULL;
- return CRYPTO_THREAD_get_local(&dgbl->private);
+ return CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx);
}
#endif
if (dgbl == NULL)
return 0;
- old = CRYPTO_THREAD_get_local(&dgbl->public);
- if ((r = CRYPTO_THREAD_set_local(&dgbl->public, rand)) > 0)
+ old = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx);
+ if ((r = CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx, rand)) > 0)
EVP_RAND_CTX_free(old);
return r;
}
if (dgbl == NULL)
return 0;
- old = CRYPTO_THREAD_get_local(&dgbl->private);
- if ((r = CRYPTO_THREAD_set_local(&dgbl->private, rand)) > 0)
+ old = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx);
+ if ((r = CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx, rand)) > 0)
EVP_RAND_CTX_free(old);
return r;
}