summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatt Caswell <matt@openssl.org>2023-05-12 17:06:10 +0100
committerMatt Caswell <matt@openssl.org>2023-05-30 17:39:29 +0100
commitf53479f98a2f2a6149192c5e3ef4ddf0926dceba (patch)
tree916981427a525f8809f1a70077c4f389165c7519
parent6d15357aeb893c6e8b4c7a8188c18f4db54c0612 (diff)
Optimise locking in rsa_get_blinding()
We optimise locking in rsa_get_blinding() so that we normally take a read lock, and only fallback to a write lock if we need to. This will be very slightly slower in the case of single use RSA objects, but should be significantly better when an RSA object is reused in a multi-threaded environment. It's probably worth the trade off. Partially fixes #20286 Reviewed-by: Tomas Mraz <tomas@openssl.org> Reviewed-by: Paul Dale <pauli@openssl.org> (Merged from https://github.com/openssl/openssl/pull/20953)
-rw-r--r--crypto/rsa/rsa_ossl.c20
1 files changed, 17 insertions, 3 deletions
diff --git a/crypto/rsa/rsa_ossl.c b/crypto/rsa/rsa_ossl.c
index 0dcb02bfa9..8b9ebbb9dd 100644
--- a/crypto/rsa/rsa_ossl.c
+++ b/crypto/rsa/rsa_ossl.c
@@ -186,11 +186,21 @@ static BN_BLINDING *rsa_get_blinding(RSA *rsa, int *local, BN_CTX *ctx)
{
BN_BLINDING *ret;
- if (!CRYPTO_THREAD_write_lock(rsa->lock))
+ if (!CRYPTO_THREAD_read_lock(rsa->lock))
return NULL;
if (rsa->blinding == NULL) {
- rsa->blinding = RSA_setup_blinding(rsa, ctx);
+ /*
+ * This dance with upgrading the lock from read to write will be
+ * slower in cases of a single use RSA object, but should be
+ * significantly better in multi-thread cases (e.g. servers). It's
+ * probably worth it.
+ */
+ CRYPTO_THREAD_unlock(rsa->lock);
+ if (!CRYPTO_THREAD_write_lock(rsa->lock))
+ return NULL;
+ if (rsa->blinding == NULL)
+ rsa->blinding = RSA_setup_blinding(rsa, ctx);
}
ret = rsa->blinding;
@@ -212,7 +222,11 @@ static BN_BLINDING *rsa_get_blinding(RSA *rsa, int *local, BN_CTX *ctx)
*local = 0;
if (rsa->mt_blinding == NULL) {
- rsa->mt_blinding = RSA_setup_blinding(rsa, ctx);
+ CRYPTO_THREAD_unlock(rsa->lock);
+ if (!CRYPTO_THREAD_write_lock(rsa->lock))
+ return NULL;
+ if (rsa->mt_blinding == NULL)
+ rsa->mt_blinding = RSA_setup_blinding(rsa, ctx);
}
ret = rsa->mt_blinding;
}