return (to);
}
-BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, int lock,
+BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, CRYPTO_RWLOCK *lock,
const BIGNUM *mod, BN_CTX *ctx)
{
BN_MONT_CTX *ret;
- CRYPTO_r_lock(lock);
+ CRYPTO_THREAD_read_lock(lock);
ret = *pmont;
- CRYPTO_r_unlock(lock);
+ CRYPTO_THREAD_unlock(lock);
if (ret)
return ret;
}
/* The locked compare-and-set, after the local work is done. */
- CRYPTO_w_lock(lock);
+ CRYPTO_THREAD_write_lock(lock);
if (*pmont) {
BN_MONT_CTX_free(ret);
ret = *pmont;
} else
*pmont = ret;
- CRYPTO_w_unlock(lock);
+ CRYPTO_THREAD_unlock(lock);
return ret;
}
if (dh->flags & DH_FLAG_CACHE_MONT_P) {
mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,
- CRYPTO_LOCK_DH, dh->p, ctx);
+ dh->lock, dh->p, ctx);
if (!mont)
goto err;
}
if (dh->flags & DH_FLAG_CACHE_MONT_P) {
mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,
- CRYPTO_LOCK_DH, dh->p, ctx);
+ dh->lock, dh->p, ctx);
if ((dh->flags & DH_FLAG_NO_EXP_CONSTTIME) == 0) {
/* XXX */
BN_set_flags(dh->priv_key, BN_FLG_CONSTTIME);
if (ret == NULL) {
DHerr(DH_F_DH_NEW_METHOD, ERR_R_MALLOC_FAILURE);
- return (NULL);
+ return NULL;
}
ret->meth = DH_get_default_method();
ret->references = 1;
ret->flags = ret->meth->flags;
+
CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data);
- if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
+
+ ret->lock = CRYPTO_THREAD_lock_new();
+ if (ret->lock == NULL) {
#ifndef OPENSSL_NO_ENGINE
ENGINE_finish(ret->engine);
#endif
CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data);
OPENSSL_free(ret);
+ return NULL;
+ }
+
+ if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
+ DH_free(ret);
ret = NULL;
}
- return (ret);
+
+ return ret;
}
void DH_free(DH *r)
if (r == NULL)
return;
- i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_DH);
+
+ CRYPTO_atomic_add(&r->references, -1, &i, r->lock);
REF_PRINT_COUNT("DH", r);
if (i > 0)
return;
CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data);
+ CRYPTO_THREAD_lock_free(r->lock);
+
BN_clear_free(r->p);
BN_clear_free(r->g);
BN_clear_free(r->q);
int DH_up_ref(DH *r)
{
- int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_DH);
+ int i;
+
+ if (CRYPTO_atomic_add(&r->references, 1, &i, r->lock) <= 0)
+ return 0;
REF_PRINT_COUNT("DH", r);
REF_ASSERT_ISNT(i < 2);
ret = OPENSSL_zalloc(sizeof(*ret));
if (ret == NULL) {
DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
- return (NULL);
+ return NULL;
}
ret->meth = DSA_get_default_method();
#ifndef OPENSSL_NO_ENGINE
ret->references = 1;
ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW;
+
CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data);
- if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
+
+ ret->lock = CRYPTO_THREAD_lock_new();
+ if (ret->lock == NULL) {
#ifndef OPENSSL_NO_ENGINE
ENGINE_finish(ret->engine);
#endif
CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data);
OPENSSL_free(ret);
+ return NULL;
+ }
+
+ if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
+ DSA_free(ret);
ret = NULL;
}
- return (ret);
+ return ret;
}
void DSA_free(DSA *r)
if (r == NULL)
return;
- i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_DSA);
+ CRYPTO_atomic_add(&r->references, -1, &i, r->lock);
REF_PRINT_COUNT("DSA", r);
if (i > 0)
return;
CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data);
+ CRYPTO_THREAD_lock_free(r->lock);
+
BN_clear_free(r->p);
BN_clear_free(r->q);
BN_clear_free(r->g);
int DSA_up_ref(DSA *r)
{
- int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_DSA);
+ int i;
+
+ if (CRYPTO_atomic_add(&r->references, 1, &i, r->lock) <= 0)
+ return 0;
REF_PRINT_COUNT("DSA", r);
REF_ASSERT_ISNT(i < 2);
if (dsa->flags & DSA_FLAG_CACHE_MONT_P) {
if (!BN_MONT_CTX_set_locked(&dsa->method_mont_p,
- CRYPTO_LOCK_DSA, dsa->p, ctx))
+ dsa->lock, dsa->p, ctx))
goto err;
}
if (dsa->flags & DSA_FLAG_CACHE_MONT_P) {
mont = BN_MONT_CTX_set_locked(&dsa->method_mont_p,
- CRYPTO_LOCK_DSA, dsa->p, ctx);
+ dsa->lock, dsa->p, ctx);
if (!mont)
goto err;
}
ENGINE_finish(ret->engine);
#endif
OPENSSL_free(ret);
- return (NULL);
+ return NULL;
}
- if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
+ ret->lock = CRYPTO_THREAD_lock_new();
+ if (ret->lock == NULL) {
#ifndef OPENSSL_NO_ENGINE
ENGINE_finish(ret->engine);
#endif
CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data);
OPENSSL_free(ret);
+ return NULL;
+ }
+
+ if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
+ RSA_free(ret);
ret = NULL;
}
- return (ret);
+
+ return ret;
}
void RSA_free(RSA *r)
if (r == NULL)
return;
- i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_RSA);
+ CRYPTO_atomic_add(&r->references, -1, &i, r->lock);
REF_PRINT_COUNT("RSA", r);
if (i > 0)
return;
CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
+ CRYPTO_THREAD_lock_free(r->lock);
+
BN_clear_free(r->n);
BN_clear_free(r->e);
BN_clear_free(r->d);
int RSA_up_ref(RSA *r)
{
- int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_RSA);
+ int i;
+
+ if (CRYPTO_atomic_add(&r->references, 1, &i, r->lock) <= 0)
+ return 0;
REF_PRINT_COUNT("RSA", r);
REF_ASSERT_ISNT(i < 2);
if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
if (!BN_MONT_CTX_set_locked
- (&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx))
+ (&rsa->_method_mod_n, rsa->lock, rsa->n, ctx))
goto err;
if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
static BN_BLINDING *rsa_get_blinding(RSA *rsa, int *local, BN_CTX *ctx)
{
BN_BLINDING *ret;
- int got_write_lock = 0;
CRYPTO_THREADID cur;
- CRYPTO_r_lock(CRYPTO_LOCK_RSA);
+ CRYPTO_THREAD_write_lock(rsa->lock);
if (rsa->blinding == NULL) {
- CRYPTO_r_unlock(CRYPTO_LOCK_RSA);
- CRYPTO_w_lock(CRYPTO_LOCK_RSA);
- got_write_lock = 1;
-
- if (rsa->blinding == NULL)
- rsa->blinding = RSA_setup_blinding(rsa, ctx);
+ rsa->blinding = RSA_setup_blinding(rsa, ctx);
}
ret = rsa->blinding;
*local = 0;
if (rsa->mt_blinding == NULL) {
- if (!got_write_lock) {
- CRYPTO_r_unlock(CRYPTO_LOCK_RSA);
- CRYPTO_w_lock(CRYPTO_LOCK_RSA);
- got_write_lock = 1;
- }
-
- if (rsa->mt_blinding == NULL)
- rsa->mt_blinding = RSA_setup_blinding(rsa, ctx);
+ rsa->mt_blinding = RSA_setup_blinding(rsa, ctx);
}
ret = rsa->mt_blinding;
}
err:
- if (got_write_lock)
- CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
- else
- CRYPTO_r_unlock(CRYPTO_LOCK_RSA);
+ CRYPTO_THREAD_unlock(rsa->lock);
return ret;
}
if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
if (!BN_MONT_CTX_set_locked
- (&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx)) {
+ (&rsa->_method_mod_n, rsa->lock, rsa->n, ctx)) {
BN_free(local_d);
goto err;
}
if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
if (!BN_MONT_CTX_set_locked
- (&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx)) {
+ (&rsa->_method_mod_n, rsa->lock, rsa->n, ctx)) {
BN_free(local_d);
goto err;
}
if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
if (!BN_MONT_CTX_set_locked
- (&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx))
+ (&rsa->_method_mod_n, rsa->lock, rsa->n, ctx))
goto err;
if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) {
if (!BN_MONT_CTX_set_locked
- (&rsa->_method_mod_p, CRYPTO_LOCK_RSA, p, ctx)
+ (&rsa->_method_mod_p, rsa->lock, p, ctx)
|| !BN_MONT_CTX_set_locked(&rsa->_method_mod_q,
- CRYPTO_LOCK_RSA, q, ctx)) {
+ rsa->lock, q, ctx)) {
BN_free(local_p);
BN_free(local_q);
goto err;
if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
if (!BN_MONT_CTX_set_locked
- (&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx))
+ (&rsa->_method_mod_n, rsa->lock, rsa->n, ctx))
goto err;
/* compute I mod q */
void BN_MONT_CTX_free(BN_MONT_CTX *mont);
int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx);
BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, BN_MONT_CTX *from);
-BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, int lock,
+BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, CRYPTO_RWLOCK *lock,
const BIGNUM *mod, BN_CTX *ctx);
/* BN_BLINDING flags */
# define CRYPTO_LOCK_X509_PKEY 5
# define CRYPTO_LOCK_X509_CRL 6
# define CRYPTO_LOCK_X509_REQ 7
-# define CRYPTO_LOCK_DSA 8
-# define CRYPTO_LOCK_RSA 9
# define CRYPTO_LOCK_EVP_PKEY 10
# define CRYPTO_LOCK_X509_STORE 11
# define CRYPTO_LOCK_SSL_CTX 12
# define CRYPTO_LOCK_BIO 21
# define CRYPTO_LOCK_READDIR 24
# define CRYPTO_LOCK_RSA_BLINDING 25
-# define CRYPTO_LOCK_DH 26
# define CRYPTO_LOCK_MALLOC2 27
# define CRYPTO_LOCK_DSO 28
# define CRYPTO_LOCK_DYNLOCK 29
CRYPTO_EX_DATA ex_data;
const DH_METHOD *meth;
ENGINE *engine;
+ CRYPTO_RWLOCK *lock;
};
# define DH_GENERATOR_2 2
const DSA_METHOD *meth;
/* functional reference if 'meth' is ENGINE-provided */
ENGINE *engine;
+ CRYPTO_RWLOCK *lock;
};
# define d2i_DSAparams_fp(fp,x) (DSA *)ASN1_d2i_fp((char *(*)())DSA_new, \
char *bignum_data;
BN_BLINDING *blinding;
BN_BLINDING *mt_blinding;
+ CRYPTO_RWLOCK *lock;
};
# ifndef OPENSSL_RSA_MAX_MODULUS_BITS